partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
train
QA_SU_save_stock_day
save stock_day 保存日线数据 :param client: :param ui_log: 给GUI qt 界面使用 :param ui_progress: 给GUI qt 界面使用 :param ui_progress_int_value: 给GUI qt 界面使用
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_stock_day(client=DATABASE, ui_log=None, ui_progress=None): ''' save stock_day 保存日线数据 :param client: :param ui_log: 给GUI qt 界面使用 :param ui_progress: 给GUI qt 界面使用 :param ui_progress_int_value: 给GUI qt 界面使用 ''' stock_list = QA_fetch_get_stock_list().code.unique().tolist() coll_stock_day = client.stock_day coll_stock_day.create_index( [("code", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING)] ) err = [] def __saving_work(code, coll_stock_day): try: QA_util_log_info( '##JOB01 Now Saving STOCK_DAY==== {}'.format(str(code)), ui_log ) # 首选查找数据库 是否 有 这个代码的数据 ref = coll_stock_day.find({'code': str(code)[0:6]}) end_date = str(now_time())[0:10] # 当前数据库已经包含了这个代码的数据, 继续增量更新 # 加入这个判断的原因是因为如果股票是刚上市的 数据库会没有数据 所以会有负索引问题出现 if ref.count() > 0: # 接着上次获取的日期继续更新 start_date = ref[ref.count() - 1]['date'] QA_util_log_info( 'UPDATE_STOCK_DAY \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log ) if start_date != end_date: coll_stock_day.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_stock_day( str(code), QA_util_get_next_day(start_date), end_date, '00' ) ) ) # 当前数据库中没有这个代码的股票数据, 从1990-01-01 开始下载所有的数据 else: start_date = '1990-01-01' QA_util_log_info( 'UPDATE_STOCK_DAY \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log ) if start_date != end_date: coll_stock_day.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_stock_day( str(code), start_date, end_date, '00' ) ) ) except Exception as error0: print(error0) err.append(str(code)) for item in range(len(stock_list)): QA_util_log_info('The {} of Total {}'.format(item, len(stock_list))) strProgressToLog = 'DOWNLOAD PROGRESS {} {}'.format( str(float(item / len(stock_list) * 100))[0:4] + '%', ui_log ) intProgressToLog = int(float(item / len(stock_list) * 100)) QA_util_log_info( strProgressToLog, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intProgressToLog ) __saving_work(stock_list[item], coll_stock_day) if len(err) < 1: QA_util_log_info('SUCCESS save stock day ^_^', ui_log) else: QA_util_log_info('ERROR CODE \n ', ui_log) QA_util_log_info(err, ui_log)
def QA_SU_save_stock_day(client=DATABASE, ui_log=None, ui_progress=None): ''' save stock_day 保存日线数据 :param client: :param ui_log: 给GUI qt 界面使用 :param ui_progress: 给GUI qt 界面使用 :param ui_progress_int_value: 给GUI qt 界面使用 ''' stock_list = QA_fetch_get_stock_list().code.unique().tolist() coll_stock_day = client.stock_day coll_stock_day.create_index( [("code", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING)] ) err = [] def __saving_work(code, coll_stock_day): try: QA_util_log_info( '##JOB01 Now Saving STOCK_DAY==== {}'.format(str(code)), ui_log ) # 首选查找数据库 是否 有 这个代码的数据 ref = coll_stock_day.find({'code': str(code)[0:6]}) end_date = str(now_time())[0:10] # 当前数据库已经包含了这个代码的数据, 继续增量更新 # 加入这个判断的原因是因为如果股票是刚上市的 数据库会没有数据 所以会有负索引问题出现 if ref.count() > 0: # 接着上次获取的日期继续更新 start_date = ref[ref.count() - 1]['date'] QA_util_log_info( 'UPDATE_STOCK_DAY \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log ) if start_date != end_date: coll_stock_day.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_stock_day( str(code), QA_util_get_next_day(start_date), end_date, '00' ) ) ) # 当前数据库中没有这个代码的股票数据, 从1990-01-01 开始下载所有的数据 else: start_date = '1990-01-01' QA_util_log_info( 'UPDATE_STOCK_DAY \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log ) if start_date != end_date: coll_stock_day.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_stock_day( str(code), start_date, end_date, '00' ) ) ) except Exception as error0: print(error0) err.append(str(code)) for item in range(len(stock_list)): QA_util_log_info('The {} of Total {}'.format(item, len(stock_list))) strProgressToLog = 'DOWNLOAD PROGRESS {} {}'.format( str(float(item / len(stock_list) * 100))[0:4] + '%', ui_log ) intProgressToLog = int(float(item / len(stock_list) * 100)) QA_util_log_info( strProgressToLog, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intProgressToLog ) __saving_work(stock_list[item], coll_stock_day) if len(err) < 1: QA_util_log_info('SUCCESS save stock day ^_^', ui_log) else: QA_util_log_info('ERROR CODE \n ', ui_log) QA_util_log_info(err, ui_log)
[ "save", "stock_day", "保存日线数据", ":", "param", "client", ":", ":", "param", "ui_log", ":", "给GUI", "qt", "界面使用", ":", "param", "ui_progress", ":", "给GUI", "qt", "界面使用", ":", "param", "ui_progress_int_value", ":", "给GUI", "qt", "界面使用" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L82-L184
[ "def", "QA_SU_save_stock_day", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "stock_list", "=", "QA_fetch_get_stock_list", "(", ")", ".", "code", ".", "unique", "(", ")", ".", "tolist", "(", ")", "...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_stock_week
save stock_week Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_stock_week(client=DATABASE, ui_log=None, ui_progress=None): """save stock_week Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ stock_list = QA_fetch_get_stock_list().code.unique().tolist() coll_stock_week = client.stock_week coll_stock_week.create_index( [("code", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING)] ) err = [] def __saving_work(code, coll_stock_week): try: QA_util_log_info( '##JOB01 Now Saving STOCK_WEEK==== {}'.format(str(code)), ui_log=ui_log ) ref = coll_stock_week.find({'code': str(code)[0:6]}) end_date = str(now_time())[0:10] if ref.count() > 0: # 加入这个判断的原因是因为如果股票是刚上市的 数据库会没有数据 所以会有负索引问题出现 start_date = ref[ref.count() - 1]['date'] QA_util_log_info( 'UPDATE_STOCK_WEEK \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log=ui_log ) if start_date != end_date: coll_stock_week.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_stock_day( str(code), QA_util_get_next_day(start_date), end_date, '00', frequence='week' ) ) ) else: start_date = '1990-01-01' QA_util_log_info( 'UPDATE_STOCK_WEEK \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log=ui_log ) if start_date != end_date: coll_stock_week.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_stock_day( str(code), start_date, end_date, '00', frequence='week' ) ) ) except: err.append(str(code)) for item in range(len(stock_list)): QA_util_log_info( 'The {} of Total {}'.format(item, len(stock_list)), ui_log=ui_log ) strProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(item / len(stock_list) * 100))[0:4] + '%' ) intProgress = int(float(item / len(stock_list) * 100)) QA_util_log_info( strProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intProgress ) __saving_work(stock_list[item], coll_stock_week) if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
def QA_SU_save_stock_week(client=DATABASE, ui_log=None, ui_progress=None): """save stock_week Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ stock_list = QA_fetch_get_stock_list().code.unique().tolist() coll_stock_week = client.stock_week coll_stock_week.create_index( [("code", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING)] ) err = [] def __saving_work(code, coll_stock_week): try: QA_util_log_info( '##JOB01 Now Saving STOCK_WEEK==== {}'.format(str(code)), ui_log=ui_log ) ref = coll_stock_week.find({'code': str(code)[0:6]}) end_date = str(now_time())[0:10] if ref.count() > 0: # 加入这个判断的原因是因为如果股票是刚上市的 数据库会没有数据 所以会有负索引问题出现 start_date = ref[ref.count() - 1]['date'] QA_util_log_info( 'UPDATE_STOCK_WEEK \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log=ui_log ) if start_date != end_date: coll_stock_week.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_stock_day( str(code), QA_util_get_next_day(start_date), end_date, '00', frequence='week' ) ) ) else: start_date = '1990-01-01' QA_util_log_info( 'UPDATE_STOCK_WEEK \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log=ui_log ) if start_date != end_date: coll_stock_week.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_stock_day( str(code), start_date, end_date, '00', frequence='week' ) ) ) except: err.append(str(code)) for item in range(len(stock_list)): QA_util_log_info( 'The {} of Total {}'.format(item, len(stock_list)), ui_log=ui_log ) strProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(item / len(stock_list) * 100))[0:4] + '%' ) intProgress = int(float(item / len(stock_list) * 100)) QA_util_log_info( strProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intProgress ) __saving_work(stock_list[item], coll_stock_week) if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
[ "save", "stock_week" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L196-L292
[ "def", "QA_SU_save_stock_week", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "stock_list", "=", "QA_fetch_get_stock_list", "(", ")", ".", "code", ".", "unique", "(", ")", ".", "tolist", "(", ")", ...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_stock_xdxr
[summary] Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_stock_xdxr(client=DATABASE, ui_log=None, ui_progress=None): """[summary] Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ stock_list = QA_fetch_get_stock_list().code.unique().tolist() # client.drop_collection('stock_xdxr') try: coll = client.stock_xdxr coll.create_index( [('code', pymongo.ASCENDING), ('date', pymongo.ASCENDING)], unique=True ) except: client.drop_collection('stock_xdxr') coll = client.stock_xdxr coll.create_index( [('code', pymongo.ASCENDING), ('date', pymongo.ASCENDING)], unique=True ) err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB02 Now Saving XDXR INFO ==== {}'.format(str(code)), ui_log=ui_log ) try: coll.insert_many( QA_util_to_json_from_pandas(QA_fetch_get_stock_xdxr(str(code))), ordered=False ) except: err.append(str(code)) for i_ in range(len(stock_list)): QA_util_log_info( 'The {} of Total {}'.format(i_, len(stock_list)), ui_log=ui_log ) strLogInfo = 'DOWNLOAD PROGRESS {} '.format( str(float(i_ / len(stock_list) * 100))[0:4] + '%' ) intLogProgress = int(float(i_ / len(stock_list) * 100)) QA_util_log_info( strLogInfo, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) __saving_work(stock_list[i_], coll)
def QA_SU_save_stock_xdxr(client=DATABASE, ui_log=None, ui_progress=None): """[summary] Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ stock_list = QA_fetch_get_stock_list().code.unique().tolist() # client.drop_collection('stock_xdxr') try: coll = client.stock_xdxr coll.create_index( [('code', pymongo.ASCENDING), ('date', pymongo.ASCENDING)], unique=True ) except: client.drop_collection('stock_xdxr') coll = client.stock_xdxr coll.create_index( [('code', pymongo.ASCENDING), ('date', pymongo.ASCENDING)], unique=True ) err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB02 Now Saving XDXR INFO ==== {}'.format(str(code)), ui_log=ui_log ) try: coll.insert_many( QA_util_to_json_from_pandas(QA_fetch_get_stock_xdxr(str(code))), ordered=False ) except: err.append(str(code)) for i_ in range(len(stock_list)): QA_util_log_info( 'The {} of Total {}'.format(i_, len(stock_list)), ui_log=ui_log ) strLogInfo = 'DOWNLOAD PROGRESS {} '.format( str(float(i_ / len(stock_list) * 100))[0:4] + '%' ) intLogProgress = int(float(i_ / len(stock_list) * 100)) QA_util_log_info( strLogInfo, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) __saving_work(stock_list[i_], coll)
[ "[", "summary", "]" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L494-L555
[ "def", "QA_SU_save_stock_xdxr", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "stock_list", "=", "QA_fetch_get_stock_list", "(", ")", ".", "code", ".", "unique", "(", ")", ".", "tolist", "(", ")", ...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_stock_min
save stock_min Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_stock_min(client=DATABASE, ui_log=None, ui_progress=None): """save stock_min Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ stock_list = QA_fetch_get_stock_list().code.unique().tolist() coll = client.stock_min coll.create_index( [ ('code', pymongo.ASCENDING), ('time_stamp', pymongo.ASCENDING), ('date_stamp', pymongo.ASCENDING) ] ) err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB03 Now Saving STOCK_MIN ==== {}'.format(str(code)), ui_log=ui_log ) try: for type in ['1min', '5min', '15min', '30min', '60min']: ref_ = coll.find({'code': str(code)[0:6], 'type': type}) end_time = str(now_time())[0:19] if ref_.count() > 0: start_time = ref_[ref_.count() - 1]['datetime'] QA_util_log_info( '##JOB03.{} Now Saving {} from {} to {} =={} '.format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_stock_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data)[1::] ) else: start_time = '2015-01-01' QA_util_log_info( '##JOB03.{} Now Saving {} from {} to {} =={} '.format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_stock_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data) ) except Exception as e: QA_util_log_info(e, ui_log=ui_log) err.append(code) QA_util_log_info(err, ui_log=ui_log) executor = ThreadPoolExecutor(max_workers=4) # executor.map((__saving_work, stock_list[i_], coll),URLS) res = { executor.submit(__saving_work, stock_list[i_], coll) for i_ in range(len(stock_list)) } count = 0 for i_ in concurrent.futures.as_completed(res): QA_util_log_info( 'The {} of Total {}'.format(count, len(stock_list)), ui_log=ui_log ) strProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(count / len(stock_list) * 100))[0:4] + '%' ) intProgress = int(count / len(stock_list) * 10000.0) QA_util_log_info( strProgress, ui_log, ui_progress=ui_progress, ui_progress_int_value=intProgress ) count = count + 1 if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
def QA_SU_save_stock_min(client=DATABASE, ui_log=None, ui_progress=None): """save stock_min Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ stock_list = QA_fetch_get_stock_list().code.unique().tolist() coll = client.stock_min coll.create_index( [ ('code', pymongo.ASCENDING), ('time_stamp', pymongo.ASCENDING), ('date_stamp', pymongo.ASCENDING) ] ) err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB03 Now Saving STOCK_MIN ==== {}'.format(str(code)), ui_log=ui_log ) try: for type in ['1min', '5min', '15min', '30min', '60min']: ref_ = coll.find({'code': str(code)[0:6], 'type': type}) end_time = str(now_time())[0:19] if ref_.count() > 0: start_time = ref_[ref_.count() - 1]['datetime'] QA_util_log_info( '##JOB03.{} Now Saving {} from {} to {} =={} '.format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_stock_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data)[1::] ) else: start_time = '2015-01-01' QA_util_log_info( '##JOB03.{} Now Saving {} from {} to {} =={} '.format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_stock_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data) ) except Exception as e: QA_util_log_info(e, ui_log=ui_log) err.append(code) QA_util_log_info(err, ui_log=ui_log) executor = ThreadPoolExecutor(max_workers=4) # executor.map((__saving_work, stock_list[i_], coll),URLS) res = { executor.submit(__saving_work, stock_list[i_], coll) for i_ in range(len(stock_list)) } count = 0 for i_ in concurrent.futures.as_completed(res): QA_util_log_info( 'The {} of Total {}'.format(count, len(stock_list)), ui_log=ui_log ) strProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(count / len(stock_list) * 100))[0:4] + '%' ) intProgress = int(count / len(stock_list) * 10000.0) QA_util_log_info( strProgress, ui_log, ui_progress=ui_progress, ui_progress_int_value=intProgress ) count = count + 1 if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
[ "save", "stock_min" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L558-L679
[ "def", "QA_SU_save_stock_min", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "stock_list", "=", "QA_fetch_get_stock_list", "(", ")", ".", "code", ".", "unique", "(", ")", ".", "tolist", "(", ")", "...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_index_day
save index_day Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_index_day(client=DATABASE, ui_log=None, ui_progress=None): """save index_day Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ __index_list = QA_fetch_get_stock_list('index') coll = client.index_day coll.create_index( [('code', pymongo.ASCENDING), ('date_stamp', pymongo.ASCENDING)] ) err = [] def __saving_work(code, coll): try: ref_ = coll.find({'code': str(code)[0:6]}) end_time = str(now_time())[0:10] if ref_.count() > 0: start_time = ref_[ref_.count() - 1]['date'] QA_util_log_info( '##JOB04 Now Saving INDEX_DAY==== \n Trying updating {} from {} to {}' .format(code, start_time, end_time), ui_log=ui_log ) if start_time != end_time: coll.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_index_day( str(code), QA_util_get_next_day(start_time), end_time ) ) ) else: try: start_time = '1990-01-01' QA_util_log_info( '##JOB04 Now Saving INDEX_DAY==== \n Trying updating {} from {} to {}' .format(code, start_time, end_time), ui_log=ui_log ) coll.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_index_day( str(code), start_time, end_time ) ) ) except: start_time = '2009-01-01' QA_util_log_info( '##JOB04 Now Saving INDEX_DAY==== \n Trying updating {} from {} to {}' .format(code, start_time, end_time), ui_log=ui_log ) coll.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_index_day( str(code), start_time, end_time ) ) ) except Exception as e: QA_util_log_info(e, ui_log=ui_log) err.append(str(code)) QA_util_log_info(err, ui_log=ui_log) for i_ in range(len(__index_list)): # __saving_work('000001') QA_util_log_info( 'The {} of Total {}'.format(i_, len(__index_list)), ui_log=ui_log ) strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(i_ / len(__index_list) * 100))[0:4] + '%' ) intLogProgress = int(float(i_ / len(__index_list) * 10000.0)) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) __saving_work(__index_list.index[i_][0], coll) if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
def QA_SU_save_index_day(client=DATABASE, ui_log=None, ui_progress=None): """save index_day Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ __index_list = QA_fetch_get_stock_list('index') coll = client.index_day coll.create_index( [('code', pymongo.ASCENDING), ('date_stamp', pymongo.ASCENDING)] ) err = [] def __saving_work(code, coll): try: ref_ = coll.find({'code': str(code)[0:6]}) end_time = str(now_time())[0:10] if ref_.count() > 0: start_time = ref_[ref_.count() - 1]['date'] QA_util_log_info( '##JOB04 Now Saving INDEX_DAY==== \n Trying updating {} from {} to {}' .format(code, start_time, end_time), ui_log=ui_log ) if start_time != end_time: coll.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_index_day( str(code), QA_util_get_next_day(start_time), end_time ) ) ) else: try: start_time = '1990-01-01' QA_util_log_info( '##JOB04 Now Saving INDEX_DAY==== \n Trying updating {} from {} to {}' .format(code, start_time, end_time), ui_log=ui_log ) coll.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_index_day( str(code), start_time, end_time ) ) ) except: start_time = '2009-01-01' QA_util_log_info( '##JOB04 Now Saving INDEX_DAY==== \n Trying updating {} from {} to {}' .format(code, start_time, end_time), ui_log=ui_log ) coll.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_index_day( str(code), start_time, end_time ) ) ) except Exception as e: QA_util_log_info(e, ui_log=ui_log) err.append(str(code)) QA_util_log_info(err, ui_log=ui_log) for i_ in range(len(__index_list)): # __saving_work('000001') QA_util_log_info( 'The {} of Total {}'.format(i_, len(__index_list)), ui_log=ui_log ) strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(i_ / len(__index_list) * 100))[0:4] + '%' ) intLogProgress = int(float(i_ / len(__index_list) * 10000.0)) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) __saving_work(__index_list.index[i_][0], coll) if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
[ "save", "index_day" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L682-L790
[ "def", "QA_SU_save_index_day", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "__index_list", "=", "QA_fetch_get_stock_list", "(", "'index'", ")", "coll", "=", "client", ".", "index_day", "coll", ".", "...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_index_min
save index_min Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_index_min(client=DATABASE, ui_log=None, ui_progress=None): """save index_min Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ __index_list = QA_fetch_get_stock_list('index') coll = client.index_min coll.create_index( [ ('code', pymongo.ASCENDING), ('time_stamp', pymongo.ASCENDING), ('date_stamp', pymongo.ASCENDING) ] ) err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB05 Now Saving Index_MIN ==== {}'.format(str(code)), ui_log=ui_log ) try: for type in ['1min', '5min', '15min', '30min', '60min']: ref_ = coll.find({'code': str(code)[0:6], 'type': type}) end_time = str(now_time())[0:19] if ref_.count() > 0: start_time = ref_[ref_.count() - 1]['datetime'] QA_util_log_info( '##JOB05.{} Now Saving {} from {} to {} =={} '.format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_index_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data[1::]) ) else: start_time = '2015-01-01' QA_util_log_info( '##JOB05.{} Now Saving {} from {} to {} =={} '.format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_index_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data) ) except: err.append(code) executor = ThreadPoolExecutor(max_workers=4) res = { executor.submit(__saving_work, __index_list.index[i_][0], coll) for i_ in range(len(__index_list)) } # multi index ./. count = 0 for i_ in concurrent.futures.as_completed(res): strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(count / len(__index_list) * 100))[0:4] + '%' ) intLogProgress = int(float(count / len(__index_list) * 10000.0)) QA_util_log_info( 'The {} of Total {}'.format(count, len(__index_list)), ui_log=ui_log ) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) count = count + 1 if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
def QA_SU_save_index_min(client=DATABASE, ui_log=None, ui_progress=None): """save index_min Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ __index_list = QA_fetch_get_stock_list('index') coll = client.index_min coll.create_index( [ ('code', pymongo.ASCENDING), ('time_stamp', pymongo.ASCENDING), ('date_stamp', pymongo.ASCENDING) ] ) err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB05 Now Saving Index_MIN ==== {}'.format(str(code)), ui_log=ui_log ) try: for type in ['1min', '5min', '15min', '30min', '60min']: ref_ = coll.find({'code': str(code)[0:6], 'type': type}) end_time = str(now_time())[0:19] if ref_.count() > 0: start_time = ref_[ref_.count() - 1]['datetime'] QA_util_log_info( '##JOB05.{} Now Saving {} from {} to {} =={} '.format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_index_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data[1::]) ) else: start_time = '2015-01-01' QA_util_log_info( '##JOB05.{} Now Saving {} from {} to {} =={} '.format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_index_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data) ) except: err.append(code) executor = ThreadPoolExecutor(max_workers=4) res = { executor.submit(__saving_work, __index_list.index[i_][0], coll) for i_ in range(len(__index_list)) } # multi index ./. count = 0 for i_ in concurrent.futures.as_completed(res): strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(count / len(__index_list) * 100))[0:4] + '%' ) intLogProgress = int(float(count / len(__index_list) * 10000.0)) QA_util_log_info( 'The {} of Total {}'.format(count, len(__index_list)), ui_log=ui_log ) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) count = count + 1 if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
[ "save", "index_min" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L793-L916
[ "def", "QA_SU_save_index_min", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "__index_list", "=", "QA_fetch_get_stock_list", "(", "'index'", ")", "coll", "=", "client", ".", "index_min", "coll", ".", "...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_stock_list
save stock_list Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_stock_list(client=DATABASE, ui_log=None, ui_progress=None): """save stock_list Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ client.drop_collection('stock_list') coll = client.stock_list coll.create_index('code') try: # 🛠todo 这个应该是第一个任务 JOB01, 先更新股票列表!! QA_util_log_info( '##JOB08 Now Saving STOCK_LIST ====', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=5000 ) stock_list_from_tdx = QA_fetch_get_stock_list() pandas_data = QA_util_to_json_from_pandas(stock_list_from_tdx) coll.insert_many(pandas_data) QA_util_log_info( "完成股票列表获取", ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=10000 ) except Exception as e: QA_util_log_info(e, ui_log=ui_log) print(" Error save_tdx.QA_SU_save_stock_list exception!") pass
def QA_SU_save_stock_list(client=DATABASE, ui_log=None, ui_progress=None): """save stock_list Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ client.drop_collection('stock_list') coll = client.stock_list coll.create_index('code') try: # 🛠todo 这个应该是第一个任务 JOB01, 先更新股票列表!! QA_util_log_info( '##JOB08 Now Saving STOCK_LIST ====', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=5000 ) stock_list_from_tdx = QA_fetch_get_stock_list() pandas_data = QA_util_to_json_from_pandas(stock_list_from_tdx) coll.insert_many(pandas_data) QA_util_log_info( "完成股票列表获取", ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=10000 ) except Exception as e: QA_util_log_info(e, ui_log=ui_log) print(" Error save_tdx.QA_SU_save_stock_list exception!") pass
[ "save", "stock_list" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L1140-L1171
[ "def", "QA_SU_save_stock_list", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "client", ".", "drop_collection", "(", "'stock_list'", ")", "coll", "=", "client", ".", "stock_list", "coll", ".", "create_...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_etf_list
save etf_list Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_etf_list(client=DATABASE, ui_log=None, ui_progress=None): """save etf_list Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ try: QA_util_log_info( '##JOB16 Now Saving ETF_LIST ====', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=5000 ) etf_list_from_tdx = QA_fetch_get_stock_list(type_="etf") pandas_data = QA_util_to_json_from_pandas(etf_list_from_tdx) if len(pandas_data) > 0: # 获取到数据后才进行drop collection 操作 client.drop_collection('etf_list') coll = client.etf_list coll.create_index('code') coll.insert_many(pandas_data) QA_util_log_info( "完成ETF列表获取", ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=10000 ) except Exception as e: QA_util_log_info(e, ui_log=ui_log) print(" Error save_tdx.QA_SU_save_etf_list exception!") pass
def QA_SU_save_etf_list(client=DATABASE, ui_log=None, ui_progress=None): """save etf_list Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ try: QA_util_log_info( '##JOB16 Now Saving ETF_LIST ====', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=5000 ) etf_list_from_tdx = QA_fetch_get_stock_list(type_="etf") pandas_data = QA_util_to_json_from_pandas(etf_list_from_tdx) if len(pandas_data) > 0: # 获取到数据后才进行drop collection 操作 client.drop_collection('etf_list') coll = client.etf_list coll.create_index('code') coll.insert_many(pandas_data) QA_util_log_info( "完成ETF列表获取", ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=10000 ) except Exception as e: QA_util_log_info(e, ui_log=ui_log) print(" Error save_tdx.QA_SU_save_etf_list exception!") pass
[ "save", "etf_list" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L1174-L1205
[ "def", "QA_SU_save_etf_list", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "try", ":", "QA_util_log_info", "(", "'##JOB16 Now Saving ETF_LIST ===='", ",", "ui_log", "=", "ui_log", ",", "ui_progress", "=",...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_stock_block
save stock_block Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_stock_block(client=DATABASE, ui_log=None, ui_progress=None): """save stock_block Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ client.drop_collection('stock_block') coll = client.stock_block coll.create_index('code') try: QA_util_log_info( '##JOB09 Now Saving STOCK_BlOCK ====', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=5000 ) coll.insert_many( QA_util_to_json_from_pandas(QA_fetch_get_stock_block('tdx')) ) QA_util_log_info( 'tdx Block ====', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=5000 ) # 🛠todo fixhere here 获取同花顺板块, 还是调用tdx的 coll.insert_many( QA_util_to_json_from_pandas(QA_fetch_get_stock_block('ths')) ) QA_util_log_info( 'ths Block ====', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=8000 ) QA_util_log_info( '完成股票板块获取=', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=10000 ) except Exception as e: QA_util_log_info(e, ui_log=ui_log) print(" Error save_tdx.QA_SU_save_stock_block exception!") pass
def QA_SU_save_stock_block(client=DATABASE, ui_log=None, ui_progress=None): """save stock_block Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ client.drop_collection('stock_block') coll = client.stock_block coll.create_index('code') try: QA_util_log_info( '##JOB09 Now Saving STOCK_BlOCK ====', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=5000 ) coll.insert_many( QA_util_to_json_from_pandas(QA_fetch_get_stock_block('tdx')) ) QA_util_log_info( 'tdx Block ====', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=5000 ) # 🛠todo fixhere here 获取同花顺板块, 还是调用tdx的 coll.insert_many( QA_util_to_json_from_pandas(QA_fetch_get_stock_block('ths')) ) QA_util_log_info( 'ths Block ====', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=8000 ) QA_util_log_info( '完成股票板块获取=', ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=10000 ) except Exception as e: QA_util_log_info(e, ui_log=ui_log) print(" Error save_tdx.QA_SU_save_stock_block exception!") pass
[ "save", "stock_block" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L1208-L1257
[ "def", "QA_SU_save_stock_block", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "client", ".", "drop_collection", "(", "'stock_block'", ")", "coll", "=", "client", ".", "stock_block", "coll", ".", "crea...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_stock_info
save stock_info Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_stock_info(client=DATABASE, ui_log=None, ui_progress=None): """save stock_info Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ client.drop_collection('stock_info') stock_list = QA_fetch_get_stock_list().code.unique().tolist() coll = client.stock_info coll.create_index('code') err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB10 Now Saving STOCK INFO ==== {}'.format(str(code)), ui_log=ui_log ) try: coll.insert_many( QA_util_to_json_from_pandas(QA_fetch_get_stock_info(str(code))) ) except: err.append(str(code)) for i_ in range(len(stock_list)): # __saving_work('000001') strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(i_ / len(stock_list) * 100))[0:4] + '%' ) intLogProgress = int(float(i_ / len(stock_list) * 10000.0)) QA_util_log_info('The {} of Total {}'.format(i_, len(stock_list))) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) __saving_work(stock_list[i_], coll) if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
def QA_SU_save_stock_info(client=DATABASE, ui_log=None, ui_progress=None): """save stock_info Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ client.drop_collection('stock_info') stock_list = QA_fetch_get_stock_list().code.unique().tolist() coll = client.stock_info coll.create_index('code') err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB10 Now Saving STOCK INFO ==== {}'.format(str(code)), ui_log=ui_log ) try: coll.insert_many( QA_util_to_json_from_pandas(QA_fetch_get_stock_info(str(code))) ) except: err.append(str(code)) for i_ in range(len(stock_list)): # __saving_work('000001') strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(i_ / len(stock_list) * 100))[0:4] + '%' ) intLogProgress = int(float(i_ / len(stock_list) * 10000.0)) QA_util_log_info('The {} of Total {}'.format(i_, len(stock_list))) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) __saving_work(stock_list[i_], coll) if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
[ "save", "stock_info" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L1260-L1306
[ "def", "QA_SU_save_stock_info", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "client", ".", "drop_collection", "(", "'stock_info'", ")", "stock_list", "=", "QA_fetch_get_stock_list", "(", ")", ".", "cod...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_stock_transaction
save stock_transaction Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_stock_transaction( client=DATABASE, ui_log=None, ui_progress=None ): """save stock_transaction Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ stock_list = QA_fetch_get_stock_list().code.unique().tolist() coll = client.stock_transaction coll.create_index('code') err = [] def __saving_work(code): QA_util_log_info( '##JOB11 Now Saving STOCK_TRANSACTION ==== {}'.format(str(code)), ui_log=ui_log ) try: coll.insert_many( QA_util_to_json_from_pandas( # 🛠todo str(stock_list[code]) 参数不对? QA_fetch_get_stock_transaction( str(code), '1990-01-01', str(now_time())[0:10] ) ) ) except: err.append(str(code)) for i_ in range(len(stock_list)): # __saving_work('000001') QA_util_log_info( 'The {} of Total {}'.format(i_, len(stock_list)), ui_log=ui_log ) strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(i_ / len(stock_list) * 100))[0:4] + '%' ) intLogProgress = int(float(i_ / len(stock_list) * 10000.0)) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) __saving_work(stock_list[i_]) if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
def QA_SU_save_stock_transaction( client=DATABASE, ui_log=None, ui_progress=None ): """save stock_transaction Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ stock_list = QA_fetch_get_stock_list().code.unique().tolist() coll = client.stock_transaction coll.create_index('code') err = [] def __saving_work(code): QA_util_log_info( '##JOB11 Now Saving STOCK_TRANSACTION ==== {}'.format(str(code)), ui_log=ui_log ) try: coll.insert_many( QA_util_to_json_from_pandas( # 🛠todo str(stock_list[code]) 参数不对? QA_fetch_get_stock_transaction( str(code), '1990-01-01', str(now_time())[0:10] ) ) ) except: err.append(str(code)) for i_ in range(len(stock_list)): # __saving_work('000001') QA_util_log_info( 'The {} of Total {}'.format(i_, len(stock_list)), ui_log=ui_log ) strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(i_ / len(stock_list) * 100))[0:4] + '%' ) intLogProgress = int(float(i_ / len(stock_list) * 10000.0)) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) __saving_work(stock_list[i_]) if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
[ "save", "stock_transaction" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L1309-L1368
[ "def", "QA_SU_save_stock_transaction", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "stock_list", "=", "QA_fetch_get_stock_list", "(", ")", ".", "code", ".", "unique", "(", ")", ".", "tolist", "(", ...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_option_commodity_day
:param client: :return:
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_option_commodity_day( client=DATABASE, ui_log=None, ui_progress=None ): ''' :param client: :return: ''' _save_option_commodity_cu_day( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_m_day( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_sr_day( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_ru_day( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_cf_day( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_c_day( client=client, ui_log=ui_log, ui_progress=ui_progress )
def QA_SU_save_option_commodity_day( client=DATABASE, ui_log=None, ui_progress=None ): ''' :param client: :return: ''' _save_option_commodity_cu_day( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_m_day( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_sr_day( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_ru_day( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_cf_day( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_c_day( client=client, ui_log=ui_log, ui_progress=ui_progress )
[ ":", "param", "client", ":", ":", "return", ":" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L2292-L2331
[ "def", "QA_SU_save_option_commodity_day", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "_save_option_commodity_cu_day", "(", "client", "=", "client", ",", "ui_log", "=", "ui_log", ",", "ui_progress", "=",...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_option_commodity_min
:param client: :return:
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_option_commodity_min( client=DATABASE, ui_log=None, ui_progress=None ): ''' :param client: :return: ''' # 测试中发现, 一起回去,容易出现错误,每次获取一个品种后 ,更换服务ip继续获取 ? _save_option_commodity_cu_min( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_sr_min( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_m_min( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_ru_min( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_cf_min( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_c_min( client=client, ui_log=ui_log, ui_progress=ui_progress )
def QA_SU_save_option_commodity_min( client=DATABASE, ui_log=None, ui_progress=None ): ''' :param client: :return: ''' # 测试中发现, 一起回去,容易出现错误,每次获取一个品种后 ,更换服务ip继续获取 ? _save_option_commodity_cu_min( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_sr_min( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_m_min( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_ru_min( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_cf_min( client=client, ui_log=ui_log, ui_progress=ui_progress ) _save_option_commodity_c_min( client=client, ui_log=ui_log, ui_progress=ui_progress )
[ ":", "param", "client", ":", ":", "return", ":" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L3384-L3427
[ "def", "QA_SU_save_option_commodity_min", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "# 测试中发现, 一起回去,容易出现错误,每次获取一个品种后 ,更换服务ip继续获取 ?", "_save_option_commodity_cu_min", "(", "client", "=", "client", ",", "ui_log",...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_option_min
:param client: :return:
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_option_min(client=DATABASE, ui_log=None, ui_progress=None): ''' :param client: :return: ''' option_contract_list = QA_fetch_get_option_contract_time_to_market() coll_option_min = client.option_day_min coll_option_min.create_index( [("code", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING)] ) err = [] # 索引 code err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB13 Now Saving Option 50ETF MIN ==== {}'.format(str(code)), ui_log=ui_log ) try: for type in ['1min', '5min', '15min', '30min', '60min']: ref_ = coll.find({'code': str(code)[0:8], 'type': type}) end_time = str(now_time())[0:19] if ref_.count() > 0: start_time = ref_[ref_.count() - 1]['datetime'] QA_util_log_info( '##JOB13.{} Now Saving Option 50ETF {} from {} to {} =={} ' .format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_future_min( str(code), start_time, end_time, type ) if len(__data) > 1: QA_util_log_info( " 写入 新增历史合约记录数 {} ".format(len(__data)) ) coll.insert_many( QA_util_to_json_from_pandas(__data[1::]) ) else: start_time = '2015-01-01' QA_util_log_info( '##JOB13.{} Now Option 50ETF {} from {} to {} =={} ' .format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_future_min( str(code), start_time, end_time, type ) if len(__data) > 1: QA_util_log_info( " 写入 新增合约记录数 {} ".format(len(__data)) ) coll.insert_many( QA_util_to_json_from_pandas(__data) ) except: err.append(code) executor = ThreadPoolExecutor(max_workers=4) res = { executor.submit( __saving_work, option_contract_list[i_]["code"], coll_option_min ) for i_ in range(len(option_contract_list)) } # multi index ./. count = 0 for i_ in concurrent.futures.as_completed(res): QA_util_log_info( 'The {} of Total {}'.format(count, len(option_contract_list)), ui_log=ui_log ) strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(count / len(option_contract_list) * 100))[0:4] + '%' ) intLogProgress = int(float(count / len(option_contract_list) * 10000.0)) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) count = count + 1 if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
def QA_SU_save_option_min(client=DATABASE, ui_log=None, ui_progress=None): ''' :param client: :return: ''' option_contract_list = QA_fetch_get_option_contract_time_to_market() coll_option_min = client.option_day_min coll_option_min.create_index( [("code", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING)] ) err = [] # 索引 code err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB13 Now Saving Option 50ETF MIN ==== {}'.format(str(code)), ui_log=ui_log ) try: for type in ['1min', '5min', '15min', '30min', '60min']: ref_ = coll.find({'code': str(code)[0:8], 'type': type}) end_time = str(now_time())[0:19] if ref_.count() > 0: start_time = ref_[ref_.count() - 1]['datetime'] QA_util_log_info( '##JOB13.{} Now Saving Option 50ETF {} from {} to {} =={} ' .format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_future_min( str(code), start_time, end_time, type ) if len(__data) > 1: QA_util_log_info( " 写入 新增历史合约记录数 {} ".format(len(__data)) ) coll.insert_many( QA_util_to_json_from_pandas(__data[1::]) ) else: start_time = '2015-01-01' QA_util_log_info( '##JOB13.{} Now Option 50ETF {} from {} to {} =={} ' .format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_future_min( str(code), start_time, end_time, type ) if len(__data) > 1: QA_util_log_info( " 写入 新增合约记录数 {} ".format(len(__data)) ) coll.insert_many( QA_util_to_json_from_pandas(__data) ) except: err.append(code) executor = ThreadPoolExecutor(max_workers=4) res = { executor.submit( __saving_work, option_contract_list[i_]["code"], coll_option_min ) for i_ in range(len(option_contract_list)) } # multi index ./. count = 0 for i_ in concurrent.futures.as_completed(res): QA_util_log_info( 'The {} of Total {}'.format(count, len(option_contract_list)), ui_log=ui_log ) strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(count / len(option_contract_list) * 100))[0:4] + '%' ) intLogProgress = int(float(count / len(option_contract_list) * 10000.0)) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) count = count + 1 if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
[ ":", "param", "client", ":", ":", "return", ":" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L3430-L3563
[ "def", "QA_SU_save_option_min", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "option_contract_list", "=", "QA_fetch_get_option_contract_time_to_market", "(", ")", "coll_option_min", "=", "client", ".", "optio...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_option_day
:param client: :return:
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_option_day(client=DATABASE, ui_log=None, ui_progress=None): ''' :param client: :return: ''' option_contract_list = QA_fetch_get_option_50etf_contract_time_to_market() coll_option_day = client.option_day coll_option_day.create_index( [("code", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING)] ) err = [] # 索引 code def __saving_work(code, coll_option_day): try: QA_util_log_info( '##JOB12 Now Saving OPTION_DAY==== {}'.format(str(code)), ui_log=ui_log ) # 首选查找数据库 是否 有 这个代码的数据 # 期权代码 从 10000001 开始编码 10001228 ref = coll_option_day.find({'code': str(code)[0:8]}) end_date = str(now_time())[0:10] # 当前数据库已经包含了这个代码的数据, 继续增量更新 # 加入这个判断的原因是因为如果是刚上市的 数据库会没有数据 所以会有负索引问题出现 if ref.count() > 0: # 接着上次获取的日期继续更新 start_date = ref[ref.count() - 1]['date'] QA_util_log_info( ' 上次获取期权日线数据的最后日期是 {}'.format(start_date), ui_log=ui_log ) QA_util_log_info( 'UPDATE_OPTION_DAY \n 从上一次下载数据开始继续 Trying update {} from {} to {}' .format(code, start_date, end_date), ui_log=ui_log ) if start_date != end_date: start_date0 = QA_util_get_next_day(start_date) df0 = QA_fetch_get_option_day( code=code, start_date=start_date0, end_date=end_date, frequence='day', ip=None, port=None ) retCount = df0.iloc[:, 0].size QA_util_log_info( "日期从开始{}-结束{} , 合约代码{} , 返回了{}条记录 , 准备写入数据库".format( start_date0, end_date, code, retCount ), ui_log=ui_log ) coll_option_day.insert_many( QA_util_to_json_from_pandas(df0) ) else: QA_util_log_info( "^已经获取过这天的数据了^ {}".format(start_date), ui_log=ui_log ) else: start_date = '1990-01-01' QA_util_log_info( 'UPDATE_OPTION_DAY \n 从新开始下载数据 Trying update {} from {} to {}' .format(code, start_date, end_date), ui_log=ui_log ) if start_date != end_date: df0 = QA_fetch_get_option_day( code=code, start_date=start_date, end_date=end_date, frequence='day', ip=None, port=None ) retCount = df0.iloc[:, 0].size QA_util_log_info( "日期从开始{}-结束{} , 合约代码{} , 获取了{}条记录 , 准备写入数据库^_^ ".format( start_date, end_date, code, retCount ), ui_log=ui_log ) coll_option_day.insert_many( QA_util_to_json_from_pandas(df0) ) else: QA_util_log_info( "*已经获取过这天的数据了* {}".format(start_date), ui_log=ui_log ) except Exception as error0: print(error0) err.append(str(code)) for item in range(len(option_contract_list)): QA_util_log_info( 'The {} of Total {}'.format(item, len(option_contract_list)), ui_log=ui_log ) strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(item / len(option_contract_list) * 100))[0:4] + '%' ) intLogProgress = int(float(item / len(option_contract_list) * 10000.0)) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) __saving_work(option_contract_list[item].code, coll_option_day) if len(err) < 1: QA_util_log_info('SUCCESS save option day ^_^ ', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
def QA_SU_save_option_day(client=DATABASE, ui_log=None, ui_progress=None): ''' :param client: :return: ''' option_contract_list = QA_fetch_get_option_50etf_contract_time_to_market() coll_option_day = client.option_day coll_option_day.create_index( [("code", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING)] ) err = [] # 索引 code def __saving_work(code, coll_option_day): try: QA_util_log_info( '##JOB12 Now Saving OPTION_DAY==== {}'.format(str(code)), ui_log=ui_log ) # 首选查找数据库 是否 有 这个代码的数据 # 期权代码 从 10000001 开始编码 10001228 ref = coll_option_day.find({'code': str(code)[0:8]}) end_date = str(now_time())[0:10] # 当前数据库已经包含了这个代码的数据, 继续增量更新 # 加入这个判断的原因是因为如果是刚上市的 数据库会没有数据 所以会有负索引问题出现 if ref.count() > 0: # 接着上次获取的日期继续更新 start_date = ref[ref.count() - 1]['date'] QA_util_log_info( ' 上次获取期权日线数据的最后日期是 {}'.format(start_date), ui_log=ui_log ) QA_util_log_info( 'UPDATE_OPTION_DAY \n 从上一次下载数据开始继续 Trying update {} from {} to {}' .format(code, start_date, end_date), ui_log=ui_log ) if start_date != end_date: start_date0 = QA_util_get_next_day(start_date) df0 = QA_fetch_get_option_day( code=code, start_date=start_date0, end_date=end_date, frequence='day', ip=None, port=None ) retCount = df0.iloc[:, 0].size QA_util_log_info( "日期从开始{}-结束{} , 合约代码{} , 返回了{}条记录 , 准备写入数据库".format( start_date0, end_date, code, retCount ), ui_log=ui_log ) coll_option_day.insert_many( QA_util_to_json_from_pandas(df0) ) else: QA_util_log_info( "^已经获取过这天的数据了^ {}".format(start_date), ui_log=ui_log ) else: start_date = '1990-01-01' QA_util_log_info( 'UPDATE_OPTION_DAY \n 从新开始下载数据 Trying update {} from {} to {}' .format(code, start_date, end_date), ui_log=ui_log ) if start_date != end_date: df0 = QA_fetch_get_option_day( code=code, start_date=start_date, end_date=end_date, frequence='day', ip=None, port=None ) retCount = df0.iloc[:, 0].size QA_util_log_info( "日期从开始{}-结束{} , 合约代码{} , 获取了{}条记录 , 准备写入数据库^_^ ".format( start_date, end_date, code, retCount ), ui_log=ui_log ) coll_option_day.insert_many( QA_util_to_json_from_pandas(df0) ) else: QA_util_log_info( "*已经获取过这天的数据了* {}".format(start_date), ui_log=ui_log ) except Exception as error0: print(error0) err.append(str(code)) for item in range(len(option_contract_list)): QA_util_log_info( 'The {} of Total {}'.format(item, len(option_contract_list)), ui_log=ui_log ) strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(item / len(option_contract_list) * 100))[0:4] + '%' ) intLogProgress = int(float(item / len(option_contract_list) * 10000.0)) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) __saving_work(option_contract_list[item].code, coll_option_day) if len(err) < 1: QA_util_log_info('SUCCESS save option day ^_^ ', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
[ ":", "param", "client", ":", ":", "return", ":" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L3566-L3710
[ "def", "QA_SU_save_option_day", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "option_contract_list", "=", "QA_fetch_get_option_50etf_contract_time_to_market", "(", ")", "coll_option_day", "=", "client", ".", ...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_future_day
save future_day 保存日线数据 :param client: :param ui_log: 给GUI qt 界面使用 :param ui_progress: 给GUI qt 界面使用 :param ui_progress_int_value: 给GUI qt 界面使用 :return:
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_future_day(client=DATABASE, ui_log=None, ui_progress=None): ''' save future_day 保存日线数据 :param client: :param ui_log: 给GUI qt 界面使用 :param ui_progress: 给GUI qt 界面使用 :param ui_progress_int_value: 给GUI qt 界面使用 :return: ''' future_list = [ item for item in QA_fetch_get_future_list().code.unique().tolist() if str(item)[-2:] in ['L8', 'L9'] ] coll_future_day = client.future_day coll_future_day.create_index( [("code", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING)] ) err = [] def __saving_work(code, coll_future_day): try: QA_util_log_info( '##JOB12 Now Saving Future_DAY==== {}'.format(str(code)), ui_log ) # 首选查找数据库 是否 有 这个代码的数据 ref = coll_future_day.find({'code': str(code)[0:4]}) end_date = str(now_time())[0:10] # 当前数据库已经包含了这个代码的数据, 继续增量更新 # 加入这个判断的原因是因为如果股票是刚上市的 数据库会没有数据 所以会有负索引问题出现 if ref.count() > 0: # 接着上次获取的日期继续更新 start_date = ref[ref.count() - 1]['date'] QA_util_log_info( 'UPDATE_Future_DAY \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log ) if start_date != end_date: coll_future_day.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_future_day( str(code), QA_util_get_next_day(start_date), end_date ) ) ) # 当前数据库中没有这个代码的股票数据, 从1990-01-01 开始下载所有的数据 else: start_date = '2001-01-01' QA_util_log_info( 'UPDATE_Future_DAY \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log ) if start_date != end_date: coll_future_day.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_future_day( str(code), start_date, end_date ) ) ) except Exception as error0: print(error0) err.append(str(code)) for item in range(len(future_list)): QA_util_log_info('The {} of Total {}'.format(item, len(future_list))) strProgressToLog = 'DOWNLOAD PROGRESS {} {}'.format( str(float(item / len(future_list) * 100))[0:4] + '%', ui_log ) intProgressToLog = int(float(item / len(future_list) * 100)) QA_util_log_info( strProgressToLog, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intProgressToLog ) __saving_work(future_list[item], coll_future_day) if len(err) < 1: QA_util_log_info('SUCCESS save future day ^_^', ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log) QA_util_log_info(err, ui_log)
def QA_SU_save_future_day(client=DATABASE, ui_log=None, ui_progress=None): ''' save future_day 保存日线数据 :param client: :param ui_log: 给GUI qt 界面使用 :param ui_progress: 给GUI qt 界面使用 :param ui_progress_int_value: 给GUI qt 界面使用 :return: ''' future_list = [ item for item in QA_fetch_get_future_list().code.unique().tolist() if str(item)[-2:] in ['L8', 'L9'] ] coll_future_day = client.future_day coll_future_day.create_index( [("code", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING)] ) err = [] def __saving_work(code, coll_future_day): try: QA_util_log_info( '##JOB12 Now Saving Future_DAY==== {}'.format(str(code)), ui_log ) # 首选查找数据库 是否 有 这个代码的数据 ref = coll_future_day.find({'code': str(code)[0:4]}) end_date = str(now_time())[0:10] # 当前数据库已经包含了这个代码的数据, 继续增量更新 # 加入这个判断的原因是因为如果股票是刚上市的 数据库会没有数据 所以会有负索引问题出现 if ref.count() > 0: # 接着上次获取的日期继续更新 start_date = ref[ref.count() - 1]['date'] QA_util_log_info( 'UPDATE_Future_DAY \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log ) if start_date != end_date: coll_future_day.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_future_day( str(code), QA_util_get_next_day(start_date), end_date ) ) ) # 当前数据库中没有这个代码的股票数据, 从1990-01-01 开始下载所有的数据 else: start_date = '2001-01-01' QA_util_log_info( 'UPDATE_Future_DAY \n Trying updating {} from {} to {}' .format(code, start_date, end_date), ui_log ) if start_date != end_date: coll_future_day.insert_many( QA_util_to_json_from_pandas( QA_fetch_get_future_day( str(code), start_date, end_date ) ) ) except Exception as error0: print(error0) err.append(str(code)) for item in range(len(future_list)): QA_util_log_info('The {} of Total {}'.format(item, len(future_list))) strProgressToLog = 'DOWNLOAD PROGRESS {} {}'.format( str(float(item / len(future_list) * 100))[0:4] + '%', ui_log ) intProgressToLog = int(float(item / len(future_list) * 100)) QA_util_log_info( strProgressToLog, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intProgressToLog ) __saving_work(future_list[item], coll_future_day) if len(err) < 1: QA_util_log_info('SUCCESS save future day ^_^', ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log) QA_util_log_info(err, ui_log)
[ "save", "future_day", "保存日线数据", ":", "param", "client", ":", ":", "param", "ui_log", ":", "给GUI", "qt", "界面使用", ":", "param", "ui_progress", ":", "给GUI", "qt", "界面使用", ":", "param", "ui_progress_int_value", ":", "给GUI", "qt", "界面使用", ":", "return", ":" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L3804-L3909
[ "def", "QA_SU_save_future_day", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "future_list", "=", "[", "item", "for", "item", "in", "QA_fetch_get_future_list", "(", ")", ".", "code", ".", "unique", "...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_SU_save_future_min
save future_min Keyword Arguments: client {[type]} -- [description] (default: {DATABASE})
QUANTAXIS/QASU/save_tdx.py
def QA_SU_save_future_min(client=DATABASE, ui_log=None, ui_progress=None): """save future_min Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ future_list = [ item for item in QA_fetch_get_future_list().code.unique().tolist() if str(item)[-2:] in ['L8', 'L9'] ] coll = client.future_min coll.create_index( [ ('code', pymongo.ASCENDING), ('time_stamp', pymongo.ASCENDING), ('date_stamp', pymongo.ASCENDING) ] ) err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB13 Now Saving Future_MIN ==== {}'.format(str(code)), ui_log=ui_log ) try: for type in ['1min', '5min', '15min', '30min', '60min']: ref_ = coll.find({'code': str(code)[0:6], 'type': type}) end_time = str(now_time())[0:19] if ref_.count() > 0: start_time = ref_[ref_.count() - 1]['datetime'] QA_util_log_info( '##JOB13.{} Now Saving Future {} from {} to {} =={} ' .format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_future_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data[1::]) ) else: start_time = '2015-01-01' QA_util_log_info( '##JOB13.{} Now Saving Future {} from {} to {} =={} ' .format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_future_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data) ) except: err.append(code) executor = ThreadPoolExecutor(max_workers=4) res = { executor.submit(__saving_work, future_list[i_], coll) for i_ in range(len(future_list)) } # multi index ./. count = 0 for i_ in concurrent.futures.as_completed(res): QA_util_log_info( 'The {} of Total {}'.format(count, len(future_list)), ui_log=ui_log ) strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(count / len(future_list) * 100))[0:4] + '%' ) intLogProgress = int(float(count / len(future_list) * 10000.0)) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) count = count + 1 if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
def QA_SU_save_future_min(client=DATABASE, ui_log=None, ui_progress=None): """save future_min Keyword Arguments: client {[type]} -- [description] (default: {DATABASE}) """ future_list = [ item for item in QA_fetch_get_future_list().code.unique().tolist() if str(item)[-2:] in ['L8', 'L9'] ] coll = client.future_min coll.create_index( [ ('code', pymongo.ASCENDING), ('time_stamp', pymongo.ASCENDING), ('date_stamp', pymongo.ASCENDING) ] ) err = [] def __saving_work(code, coll): QA_util_log_info( '##JOB13 Now Saving Future_MIN ==== {}'.format(str(code)), ui_log=ui_log ) try: for type in ['1min', '5min', '15min', '30min', '60min']: ref_ = coll.find({'code': str(code)[0:6], 'type': type}) end_time = str(now_time())[0:19] if ref_.count() > 0: start_time = ref_[ref_.count() - 1]['datetime'] QA_util_log_info( '##JOB13.{} Now Saving Future {} from {} to {} =={} ' .format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_future_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data[1::]) ) else: start_time = '2015-01-01' QA_util_log_info( '##JOB13.{} Now Saving Future {} from {} to {} =={} ' .format( ['1min', '5min', '15min', '30min', '60min'].index(type), str(code), start_time, end_time, type ), ui_log=ui_log ) if start_time != end_time: __data = QA_fetch_get_future_min( str(code), start_time, end_time, type ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data) ) except: err.append(code) executor = ThreadPoolExecutor(max_workers=4) res = { executor.submit(__saving_work, future_list[i_], coll) for i_ in range(len(future_list)) } # multi index ./. count = 0 for i_ in concurrent.futures.as_completed(res): QA_util_log_info( 'The {} of Total {}'.format(count, len(future_list)), ui_log=ui_log ) strLogProgress = 'DOWNLOAD PROGRESS {} '.format( str(float(count / len(future_list) * 100))[0:4] + '%' ) intLogProgress = int(float(count / len(future_list) * 10000.0)) QA_util_log_info( strLogProgress, ui_log=ui_log, ui_progress=ui_progress, ui_progress_int_value=intLogProgress ) count = count + 1 if len(err) < 1: QA_util_log_info('SUCCESS', ui_log=ui_log) else: QA_util_log_info(' ERROR CODE \n ', ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
[ "save", "future_min" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QASU/save_tdx.py#L4016-L4146
[ "def", "QA_SU_save_future_min", "(", "client", "=", "DATABASE", ",", "ui_log", "=", "None", ",", "ui_progress", "=", "None", ")", ":", "future_list", "=", "[", "item", "for", "item", "in", "QA_fetch_get_future_list", "(", ")", ".", "code", ".", "unique", "...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
CLI.do_shell
run a shell commad
QUANTAXIS/QACmd/__init__.py
def do_shell(self, arg): "run a shell commad" print(">", arg) sub_cmd = subprocess.Popen(arg, shell=True, stdout=subprocess.PIPE) print(sub_cmd.communicate()[0])
def do_shell(self, arg): "run a shell commad" print(">", arg) sub_cmd = subprocess.Popen(arg, shell=True, stdout=subprocess.PIPE) print(sub_cmd.communicate()[0])
[ "run", "a", "shell", "commad" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QACmd/__init__.py#L84-L88
[ "def", "do_shell", "(", "self", ",", "arg", ")", ":", "print", "(", "\">\"", ",", "arg", ")", "sub_cmd", "=", "subprocess", ".", "Popen", "(", "arg", ",", "shell", "=", "True", ",", "stdout", "=", "subprocess", ".", "PIPE", ")", "print", "(", "sub_...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_stock_min_adv
'获取股票分钟线' :param code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param frequence: 字符串str 分钟线的类型 支持 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m 类型 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return: QA_DataStruct_Stock_min 类型
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_stock_min_adv( code, start, end=None, frequence='1min', if_drop_index=True, # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉 collections=DATABASE.stock_min): ''' '获取股票分钟线' :param code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param frequence: 字符串str 分钟线的类型 支持 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m 类型 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return: QA_DataStruct_Stock_min 类型 ''' if frequence in ['1min', '1m']: frequence = '1min' elif frequence in ['5min', '5m']: frequence = '5min' elif frequence in ['15min', '15m']: frequence = '15min' elif frequence in ['30min', '30m']: frequence = '30min' elif frequence in ['60min', '60m']: frequence = '60min' else: print("QA Error QA_fetch_stock_min_adv parameter frequence=%s is none of 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m" % frequence) return None # __data = [] 未使用 end = start if end is None else end if len(start) == 10: start = '{} 09:30:00'.format(start) if len(end) == 10: end = '{} 15:00:00'.format(end) if start == end: # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_stock_min, 不支持start end是相等的 print("QA Error QA_fetch_stock_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! " % ( code, start, end)) return None # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 res = QA_fetch_stock_min( code, start, end, format='pd', frequence=frequence) if res is None: print("QA Error QA_fetch_stock_min_adv parameter code=%s , start=%s, end=%s frequence=%s call QA_fetch_stock_min return None" % ( code, start, end, frequence)) return None else: res_set_index = res.set_index(['datetime', 'code'], drop=if_drop_index) # if res_set_index is None: # print("QA Error QA_fetch_stock_min_adv set index 'datetime, code' return None") # return None return QA_DataStruct_Stock_min(res_set_index)
def QA_fetch_stock_min_adv( code, start, end=None, frequence='1min', if_drop_index=True, # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉 collections=DATABASE.stock_min): ''' '获取股票分钟线' :param code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param frequence: 字符串str 分钟线的类型 支持 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m 类型 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return: QA_DataStruct_Stock_min 类型 ''' if frequence in ['1min', '1m']: frequence = '1min' elif frequence in ['5min', '5m']: frequence = '5min' elif frequence in ['15min', '15m']: frequence = '15min' elif frequence in ['30min', '30m']: frequence = '30min' elif frequence in ['60min', '60m']: frequence = '60min' else: print("QA Error QA_fetch_stock_min_adv parameter frequence=%s is none of 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m" % frequence) return None # __data = [] 未使用 end = start if end is None else end if len(start) == 10: start = '{} 09:30:00'.format(start) if len(end) == 10: end = '{} 15:00:00'.format(end) if start == end: # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_stock_min, 不支持start end是相等的 print("QA Error QA_fetch_stock_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! " % ( code, start, end)) return None # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 res = QA_fetch_stock_min( code, start, end, format='pd', frequence=frequence) if res is None: print("QA Error QA_fetch_stock_min_adv parameter code=%s , start=%s, end=%s frequence=%s call QA_fetch_stock_min return None" % ( code, start, end, frequence)) return None else: res_set_index = res.set_index(['datetime', 'code'], drop=if_drop_index) # if res_set_index is None: # print("QA Error QA_fetch_stock_min_adv set index 'datetime, code' return None") # return None return QA_DataStruct_Stock_min(res_set_index)
[ "获取股票分钟线", ":", "param", "code", ":", "字符串str", "eg", "600085", ":", "param", "start", ":", "字符串str", "开始日期", "eg", "2011", "-", "01", "-", "01", ":", "param", "end", ":", "字符串str", "结束日期", "eg", "2011", "-", "05", "-", "01", ":", "param", "frequen...
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L123-L182
[ "def", "QA_fetch_stock_min_adv", "(", "code", ",", "start", ",", "end", "=", "None", ",", "frequence", "=", "'1min'", ",", "if_drop_index", "=", "True", ",", "# 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉", "collections", "=", "DATABASE", ".", "stock_min", ")", ...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_stock_day_full_adv
'返回全市场某一天的数据' :param date: :return: QA_DataStruct_Stock_day类 型数据
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_stock_day_full_adv(date): ''' '返回全市场某一天的数据' :param date: :return: QA_DataStruct_Stock_day类 型数据 ''' # 🛠 todo 检查日期data参数 res = QA_fetch_stock_full(date, 'pd') if res is None: print("QA Error QA_fetch_stock_day_full_adv parameter date=%s call QA_fetch_stock_full return None" % (date)) return None else: res_set_index = res.set_index(['date', 'code']) # if res_set_index is None: # print("QA Error QA_fetch_stock_day_full set index 'date, code' return None") return QA_DataStruct_Stock_day(res_set_index)
def QA_fetch_stock_day_full_adv(date): ''' '返回全市场某一天的数据' :param date: :return: QA_DataStruct_Stock_day类 型数据 ''' # 🛠 todo 检查日期data参数 res = QA_fetch_stock_full(date, 'pd') if res is None: print("QA Error QA_fetch_stock_day_full_adv parameter date=%s call QA_fetch_stock_full return None" % (date)) return None else: res_set_index = res.set_index(['date', 'code']) # if res_set_index is None: # print("QA Error QA_fetch_stock_day_full set index 'date, code' return None") return QA_DataStruct_Stock_day(res_set_index)
[ "返回全市场某一天的数据", ":", "param", "date", ":", ":", "return", ":", "QA_DataStruct_Stock_day类", "型数据" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L185-L200
[ "def", "QA_fetch_stock_day_full_adv", "(", "date", ")", ":", "# 🛠 todo 检查日期data参数", "res", "=", "QA_fetch_stock_full", "(", "date", ",", "'pd'", ")", "if", "res", "is", "None", ":", "print", "(", "\"QA Error QA_fetch_stock_day_full_adv parameter date=%s call QA_fetch_sto...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_index_day_adv
:param code: code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return:
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_index_day_adv( code, start, end=None, if_drop_index=True, # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉 collections=DATABASE.index_day): ''' :param code: code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return: ''' '获取指数日线' end = start if end is None else end start = str(start)[0:10] end = str(end)[0:10] # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 # 🛠 todo 如果相等 res = QA_fetch_index_day(code, start, end, format='pd') if res is None: print("QA Error QA_fetch_index_day_adv parameter code=%s start=%s end=%s call QA_fetch_index_day return None" % ( code, start, end)) return None else: res_set_index = res.set_index(['date', 'code'], drop=if_drop_index) # if res_set_index is None: # print("QA Error QA_fetch_index_day_adv set index 'date, code' return None") # return None return QA_DataStruct_Index_day(res_set_index)
def QA_fetch_index_day_adv( code, start, end=None, if_drop_index=True, # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉 collections=DATABASE.index_day): ''' :param code: code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return: ''' '获取指数日线' end = start if end is None else end start = str(start)[0:10] end = str(end)[0:10] # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 # 🛠 todo 如果相等 res = QA_fetch_index_day(code, start, end, format='pd') if res is None: print("QA Error QA_fetch_index_day_adv parameter code=%s start=%s end=%s call QA_fetch_index_day return None" % ( code, start, end)) return None else: res_set_index = res.set_index(['date', 'code'], drop=if_drop_index) # if res_set_index is None: # print("QA Error QA_fetch_index_day_adv set index 'date, code' return None") # return None return QA_DataStruct_Index_day(res_set_index)
[ ":", "param", "code", ":", "code", ":", "字符串str", "eg", "600085", ":", "param", "start", ":", "字符串str", "开始日期", "eg", "2011", "-", "01", "-", "01", ":", "param", "end", ":", "字符串str", "结束日期", "eg", "2011", "-", "05", "-", "01", ":", "param", "if_...
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L203-L235
[ "def", "QA_fetch_index_day_adv", "(", "code", ",", "start", ",", "end", "=", "None", ",", "if_drop_index", "=", "True", ",", "# 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉", "collections", "=", "DATABASE", ".", "index_day", ")", ":", "'获取指数日线'", "end", "=", "st...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_index_min_adv
'获取股票分钟线' :param code: :param start: :param end: :param frequence: :param if_drop_index: :param collections: :return:
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_index_min_adv( code, start, end=None, frequence='1min', if_drop_index=True, collections=DATABASE.index_min): ''' '获取股票分钟线' :param code: :param start: :param end: :param frequence: :param if_drop_index: :param collections: :return: ''' if frequence in ['1min', '1m']: frequence = '1min' elif frequence in ['5min', '5m']: frequence = '5min' elif frequence in ['15min', '15m']: frequence = '15min' elif frequence in ['30min', '30m']: frequence = '30min' elif frequence in ['60min', '60m']: frequence = '60min' # __data = [] 没有使用 end = start if end is None else end if len(start) == 10: start = '{} 09:30:00'.format(start) if len(end) == 10: end = '{} 15:00:00'.format(end) # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 # if start == end: # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_index_min_adv, 不支持start end是相等的 #print("QA Error QA_fetch_index_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! " % (code, start, end)) # return None res = QA_fetch_index_min( code, start, end, format='pd', frequence=frequence) if res is None: print("QA Error QA_fetch_index_min_adv parameter code=%s start=%s end=%s frequence=%s call QA_fetch_index_min return None" % ( code, start, end, frequence)) else: res_reset_index = res.set_index( ['datetime', 'code'], drop=if_drop_index) # if res_reset_index is None: # print("QA Error QA_fetch_index_min_adv set index 'date, code' return None") return QA_DataStruct_Index_min(res_reset_index)
def QA_fetch_index_min_adv( code, start, end=None, frequence='1min', if_drop_index=True, collections=DATABASE.index_min): ''' '获取股票分钟线' :param code: :param start: :param end: :param frequence: :param if_drop_index: :param collections: :return: ''' if frequence in ['1min', '1m']: frequence = '1min' elif frequence in ['5min', '5m']: frequence = '5min' elif frequence in ['15min', '15m']: frequence = '15min' elif frequence in ['30min', '30m']: frequence = '30min' elif frequence in ['60min', '60m']: frequence = '60min' # __data = [] 没有使用 end = start if end is None else end if len(start) == 10: start = '{} 09:30:00'.format(start) if len(end) == 10: end = '{} 15:00:00'.format(end) # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 # if start == end: # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_index_min_adv, 不支持start end是相等的 #print("QA Error QA_fetch_index_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! " % (code, start, end)) # return None res = QA_fetch_index_min( code, start, end, format='pd', frequence=frequence) if res is None: print("QA Error QA_fetch_index_min_adv parameter code=%s start=%s end=%s frequence=%s call QA_fetch_index_min return None" % ( code, start, end, frequence)) else: res_reset_index = res.set_index( ['datetime', 'code'], drop=if_drop_index) # if res_reset_index is None: # print("QA Error QA_fetch_index_min_adv set index 'date, code' return None") return QA_DataStruct_Index_min(res_reset_index)
[ "获取股票分钟线", ":", "param", "code", ":", ":", "param", "start", ":", ":", "param", "end", ":", ":", "param", "frequence", ":", ":", "param", "if_drop_index", ":", ":", "param", "collections", ":", ":", "return", ":" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L238-L290
[ "def", "QA_fetch_index_min_adv", "(", "code", ",", "start", ",", "end", "=", "None", ",", "frequence", "=", "'1min'", ",", "if_drop_index", "=", "True", ",", "collections", "=", "DATABASE", ".", "index_min", ")", ":", "if", "frequence", "in", "[", "'1min'"...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_stock_list_adv
'获取股票列表' :param collections: mongodb 数据库 :return: DataFrame
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_stock_list_adv(collections=DATABASE.stock_list): ''' '获取股票列表' :param collections: mongodb 数据库 :return: DataFrame ''' stock_list_items = QA_fetch_stock_list(collections) if len(stock_list_items) == 0: print("QA Error QA_fetch_stock_list_adv call item for item in collections.find() return 0 item, maybe the DATABASE.stock_list is empty!") return None return stock_list_items
def QA_fetch_stock_list_adv(collections=DATABASE.stock_list): ''' '获取股票列表' :param collections: mongodb 数据库 :return: DataFrame ''' stock_list_items = QA_fetch_stock_list(collections) if len(stock_list_items) == 0: print("QA Error QA_fetch_stock_list_adv call item for item in collections.find() return 0 item, maybe the DATABASE.stock_list is empty!") return None return stock_list_items
[ "获取股票列表", ":", "param", "collections", ":", "mongodb", "数据库", ":", "return", ":", "DataFrame" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L319-L329
[ "def", "QA_fetch_stock_list_adv", "(", "collections", "=", "DATABASE", ".", "stock_list", ")", ":", "stock_list_items", "=", "QA_fetch_stock_list", "(", "collections", ")", "if", "len", "(", "stock_list_items", ")", "==", "0", ":", "print", "(", "\"QA Error QA_fet...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_index_list_adv
'获取股票列表' :param collections: mongodb 数据库 :return: DataFrame
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_index_list_adv(collections=DATABASE.index_list): ''' '获取股票列表' :param collections: mongodb 数据库 :return: DataFrame ''' index_list_items = QA_fetch_index_list(collections) if len(index_list_items) == 0: print("QA Error QA_fetch_index_list_adv call item for item in collections.find() return 0 item, maybe the DATABASE.index_list is empty!") return None return index_list_items
def QA_fetch_index_list_adv(collections=DATABASE.index_list): ''' '获取股票列表' :param collections: mongodb 数据库 :return: DataFrame ''' index_list_items = QA_fetch_index_list(collections) if len(index_list_items) == 0: print("QA Error QA_fetch_index_list_adv call item for item in collections.find() return 0 item, maybe the DATABASE.index_list is empty!") return None return index_list_items
[ "获取股票列表", ":", "param", "collections", ":", "mongodb", "数据库", ":", "return", ":", "DataFrame" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L332-L342
[ "def", "QA_fetch_index_list_adv", "(", "collections", "=", "DATABASE", ".", "index_list", ")", ":", "index_list_items", "=", "QA_fetch_index_list", "(", "collections", ")", "if", "len", "(", "index_list_items", ")", "==", "0", ":", "print", "(", "\"QA Error QA_fet...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_future_day_adv
:param code: code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return:
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_future_day_adv( code, start, end=None, if_drop_index=True, # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉 collections=DATABASE.index_day): ''' :param code: code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return: ''' '获取期货日线' end = start if end is None else end start = str(start)[0:10] end = str(end)[0:10] # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 # 🛠 todo 如果相等 res = QA_fetch_future_day(code, start, end, format='pd') if res is None: print("QA Error QA_fetch_future_day_adv parameter code=%s start=%s end=%s call QA_fetch_future_day return None" % ( code, start, end)) else: res_set_index = res.set_index(['date', 'code']) # if res_set_index is None: # print("QA Error QA_fetch_index_day_adv set index 'date, code' return None") # return None return QA_DataStruct_Future_day(res_set_index)
def QA_fetch_future_day_adv( code, start, end=None, if_drop_index=True, # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉 collections=DATABASE.index_day): ''' :param code: code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return: ''' '获取期货日线' end = start if end is None else end start = str(start)[0:10] end = str(end)[0:10] # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 # 🛠 todo 如果相等 res = QA_fetch_future_day(code, start, end, format='pd') if res is None: print("QA Error QA_fetch_future_day_adv parameter code=%s start=%s end=%s call QA_fetch_future_day return None" % ( code, start, end)) else: res_set_index = res.set_index(['date', 'code']) # if res_set_index is None: # print("QA Error QA_fetch_index_day_adv set index 'date, code' return None") # return None return QA_DataStruct_Future_day(res_set_index)
[ ":", "param", "code", ":", "code", ":", "字符串str", "eg", "600085", ":", "param", "start", ":", "字符串str", "开始日期", "eg", "2011", "-", "01", "-", "01", ":", "param", "end", ":", "字符串str", "结束日期", "eg", "2011", "-", "05", "-", "01", ":", "param", "if_...
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L344-L375
[ "def", "QA_fetch_future_day_adv", "(", "code", ",", "start", ",", "end", "=", "None", ",", "if_drop_index", "=", "True", ",", "# 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉", "collections", "=", "DATABASE", ".", "index_day", ")", ":", "'获取期货日线'", "end", "=", "s...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_future_min_adv
'获取股票分钟线' :param code: :param start: :param end: :param frequence: :param if_drop_index: :param collections: :return:
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_future_min_adv( code, start, end=None, frequence='1min', if_drop_index=True, collections=DATABASE.future_min): ''' '获取股票分钟线' :param code: :param start: :param end: :param frequence: :param if_drop_index: :param collections: :return: ''' if frequence in ['1min', '1m']: frequence = '1min' elif frequence in ['5min', '5m']: frequence = '5min' elif frequence in ['15min', '15m']: frequence = '15min' elif frequence in ['30min', '30m']: frequence = '30min' elif frequence in ['60min', '60m']: frequence = '60min' # __data = [] 没有使用 end = start if end is None else end if len(start) == 10: start = '{} 00:00:00'.format(start) if len(end) == 10: end = '{} 15:00:00'.format(end) # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 # if start == end: # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_index_min_adv, 不支持start end是相等的 #print("QA Error QA_fetch_index_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! " % (code, start, end)) # return None res = QA_fetch_future_min( code, start, end, format='pd', frequence=frequence) if res is None: print("QA Error QA_fetch_future_min_adv parameter code=%s start=%s end=%s frequence=%s call QA_fetch_future_min return None" % ( code, start, end, frequence)) else: res_reset_index = res.set_index( ['datetime', 'code'], drop=if_drop_index) # if res_reset_index is None: # print("QA Error QA_fetch_index_min_adv set index 'date, code' return None") return QA_DataStruct_Future_min(res_reset_index)
def QA_fetch_future_min_adv( code, start, end=None, frequence='1min', if_drop_index=True, collections=DATABASE.future_min): ''' '获取股票分钟线' :param code: :param start: :param end: :param frequence: :param if_drop_index: :param collections: :return: ''' if frequence in ['1min', '1m']: frequence = '1min' elif frequence in ['5min', '5m']: frequence = '5min' elif frequence in ['15min', '15m']: frequence = '15min' elif frequence in ['30min', '30m']: frequence = '30min' elif frequence in ['60min', '60m']: frequence = '60min' # __data = [] 没有使用 end = start if end is None else end if len(start) == 10: start = '{} 00:00:00'.format(start) if len(end) == 10: end = '{} 15:00:00'.format(end) # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 # if start == end: # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_index_min_adv, 不支持start end是相等的 #print("QA Error QA_fetch_index_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! " % (code, start, end)) # return None res = QA_fetch_future_min( code, start, end, format='pd', frequence=frequence) if res is None: print("QA Error QA_fetch_future_min_adv parameter code=%s start=%s end=%s frequence=%s call QA_fetch_future_min return None" % ( code, start, end, frequence)) else: res_reset_index = res.set_index( ['datetime', 'code'], drop=if_drop_index) # if res_reset_index is None: # print("QA Error QA_fetch_index_min_adv set index 'date, code' return None") return QA_DataStruct_Future_min(res_reset_index)
[ "获取股票分钟线", ":", "param", "code", ":", ":", "param", "start", ":", ":", "param", "end", ":", ":", "param", "frequence", ":", ":", "param", "if_drop_index", ":", ":", "param", "collections", ":", ":", "return", ":" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L378-L430
[ "def", "QA_fetch_future_min_adv", "(", "code", ",", "start", ",", "end", "=", "None", ",", "frequence", "=", "'1min'", ",", "if_drop_index", "=", "True", ",", "collections", "=", "DATABASE", ".", "future_min", ")", ":", "if", "frequence", "in", "[", "'1min...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_future_list_adv
'获取股票列表' :param collections: mongodb 数据库 :return: DataFrame
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_future_list_adv(collections=DATABASE.future_list): ''' '获取股票列表' :param collections: mongodb 数据库 :return: DataFrame ''' future_list_items = QA_fetch_future_list() if len(future_list_items) == 0: print("QA Error QA_fetch_future_list_adv call item for item in collections.find() return 0 item, maybe the DATABASE.future_list is empty!") return None return future_list_items
def QA_fetch_future_list_adv(collections=DATABASE.future_list): ''' '获取股票列表' :param collections: mongodb 数据库 :return: DataFrame ''' future_list_items = QA_fetch_future_list() if len(future_list_items) == 0: print("QA Error QA_fetch_future_list_adv call item for item in collections.find() return 0 item, maybe the DATABASE.future_list is empty!") return None return future_list_items
[ "获取股票列表", ":", "param", "collections", ":", "mongodb", "数据库", ":", "return", ":", "DataFrame" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L433-L443
[ "def", "QA_fetch_future_list_adv", "(", "collections", "=", "DATABASE", ".", "future_list", ")", ":", "future_list_items", "=", "QA_fetch_future_list", "(", ")", "if", "len", "(", "future_list_items", ")", "==", "0", ":", "print", "(", "\"QA Error QA_fetch_future_li...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_stock_block_adv
返回板块 ❌ :param code: :param blockname: :param collections: 默认数据库 stock_block :return: QA_DataStruct_Stock_block
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_stock_block_adv(code=None, blockname=None, collections=DATABASE.stock_block): ''' 返回板块 ❌ :param code: :param blockname: :param collections: 默认数据库 stock_block :return: QA_DataStruct_Stock_block ''' if code is not None and blockname is None: # 返回这个股票代码所属的板块 data = pd.DataFrame([item for item in collections.find( {'code': {'$in': code}})]) data = data.drop(['_id'], axis=1) return QA_DataStruct_Stock_block(data.set_index(['blockname', 'code'], drop=True).drop_duplicates()) elif blockname is not None and code is None: # # 🛠 todo fnished 返回 这个板块所有的股票 # 返回该板块所属的股票 # print("QA Error blockname is Not none code none, return all code from its block name have not implemented yet !") items_from_collections = [item for item in collections.find( {'blockname': re.compile(blockname)})] data = pd.DataFrame(items_from_collections).drop(['_id'], axis=1) data_set_index = data.set_index(['blockname', 'code'], drop=True) return QA_DataStruct_Stock_block(data_set_index) else: # 🛠 todo 返回 判断 这个股票是否和属于该板块 data = pd.DataFrame( [item for item in collections.find()]).drop(['_id'], axis=1) data_set_index = data.set_index(['blockname', 'code'], drop=True) return QA_DataStruct_Stock_block(data_set_index)
def QA_fetch_stock_block_adv(code=None, blockname=None, collections=DATABASE.stock_block): ''' 返回板块 ❌ :param code: :param blockname: :param collections: 默认数据库 stock_block :return: QA_DataStruct_Stock_block ''' if code is not None and blockname is None: # 返回这个股票代码所属的板块 data = pd.DataFrame([item for item in collections.find( {'code': {'$in': code}})]) data = data.drop(['_id'], axis=1) return QA_DataStruct_Stock_block(data.set_index(['blockname', 'code'], drop=True).drop_duplicates()) elif blockname is not None and code is None: # # 🛠 todo fnished 返回 这个板块所有的股票 # 返回该板块所属的股票 # print("QA Error blockname is Not none code none, return all code from its block name have not implemented yet !") items_from_collections = [item for item in collections.find( {'blockname': re.compile(blockname)})] data = pd.DataFrame(items_from_collections).drop(['_id'], axis=1) data_set_index = data.set_index(['blockname', 'code'], drop=True) return QA_DataStruct_Stock_block(data_set_index) else: # 🛠 todo 返回 判断 这个股票是否和属于该板块 data = pd.DataFrame( [item for item in collections.find()]).drop(['_id'], axis=1) data_set_index = data.set_index(['blockname', 'code'], drop=True) return QA_DataStruct_Stock_block(data_set_index)
[ "返回板块", "❌", ":", "param", "code", ":", ":", "param", "blockname", ":", ":", "param", "collections", ":", "默认数据库", "stock_block", ":", "return", ":", "QA_DataStruct_Stock_block" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L446-L478
[ "def", "QA_fetch_stock_block_adv", "(", "code", "=", "None", ",", "blockname", "=", "None", ",", "collections", "=", "DATABASE", ".", "stock_block", ")", ":", "if", "code", "is", "not", "None", "and", "blockname", "is", "None", ":", "# 返回这个股票代码所属的板块", "data"...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_stock_realtime_adv
返回当日的上下五档, code可以是股票可以是list, num是每个股票获取的数量 :param code: :param num: :param collections: realtime_XXXX-XX-XX 每天实时时间 :return: DataFrame
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_stock_realtime_adv(code=None, num=1, collections=DATABASE.get_collection('realtime_{}'.format(datetime.date.today()))): ''' 返回当日的上下五档, code可以是股票可以是list, num是每个股票获取的数量 :param code: :param num: :param collections: realtime_XXXX-XX-XX 每天实时时间 :return: DataFrame ''' if code is not None: # code 必须转换成list 去查询数据库 if isinstance(code, str): code = [code] elif isinstance(code, list): pass else: print( "QA Error QA_fetch_stock_realtime_adv parameter code is not List type or String type") items_from_collections = [item for item in collections.find( {'code': {'$in': code}}, limit=num*len(code), sort=[('datetime', pymongo.DESCENDING)])] if items_from_collections is None: print("QA Error QA_fetch_stock_realtime_adv find parameter code={} num={} collection={} return NOne".format( code, num, collections)) return data = pd.DataFrame(items_from_collections) data_set_index = data.set_index( ['datetime', 'code'], drop=False).drop(['_id'], axis=1) return data_set_index else: print("QA Error QA_fetch_stock_realtime_adv parameter code is None")
def QA_fetch_stock_realtime_adv(code=None, num=1, collections=DATABASE.get_collection('realtime_{}'.format(datetime.date.today()))): ''' 返回当日的上下五档, code可以是股票可以是list, num是每个股票获取的数量 :param code: :param num: :param collections: realtime_XXXX-XX-XX 每天实时时间 :return: DataFrame ''' if code is not None: # code 必须转换成list 去查询数据库 if isinstance(code, str): code = [code] elif isinstance(code, list): pass else: print( "QA Error QA_fetch_stock_realtime_adv parameter code is not List type or String type") items_from_collections = [item for item in collections.find( {'code': {'$in': code}}, limit=num*len(code), sort=[('datetime', pymongo.DESCENDING)])] if items_from_collections is None: print("QA Error QA_fetch_stock_realtime_adv find parameter code={} num={} collection={} return NOne".format( code, num, collections)) return data = pd.DataFrame(items_from_collections) data_set_index = data.set_index( ['datetime', 'code'], drop=False).drop(['_id'], axis=1) return data_set_index else: print("QA Error QA_fetch_stock_realtime_adv parameter code is None")
[ "返回当日的上下五档", "code可以是股票可以是list", "num是每个股票获取的数量", ":", "param", "code", ":", ":", "param", "num", ":", ":", "param", "collections", ":", "realtime_XXXX", "-", "XX", "-", "XX", "每天实时时间", ":", "return", ":", "DataFrame" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L481-L513
[ "def", "QA_fetch_stock_realtime_adv", "(", "code", "=", "None", ",", "num", "=", "1", ",", "collections", "=", "DATABASE", ".", "get_collection", "(", "'realtime_{}'", ".", "format", "(", "datetime", ".", "date", ".", "today", "(", ")", ")", ")", ")", ":...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_financial_report_adv
高级财务查询接口 Arguments: code {[type]} -- [description] start {[type]} -- [description] Keyword Arguments: end {[type]} -- [description] (default: {None})
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_financial_report_adv(code, start, end=None, ltype='EN'): """高级财务查询接口 Arguments: code {[type]} -- [description] start {[type]} -- [description] Keyword Arguments: end {[type]} -- [description] (default: {None}) """ if end is None: return QA_DataStruct_Financial(QA_fetch_financial_report(code, start, ltype=ltype)) else: series = pd.Series( data=month_data, index=pd.to_datetime(month_data), name='date') timerange = series.loc[start:end].tolist() return QA_DataStruct_Financial(QA_fetch_financial_report(code, timerange, ltype=ltype))
def QA_fetch_financial_report_adv(code, start, end=None, ltype='EN'): """高级财务查询接口 Arguments: code {[type]} -- [description] start {[type]} -- [description] Keyword Arguments: end {[type]} -- [description] (default: {None}) """ if end is None: return QA_DataStruct_Financial(QA_fetch_financial_report(code, start, ltype=ltype)) else: series = pd.Series( data=month_data, index=pd.to_datetime(month_data), name='date') timerange = series.loc[start:end].tolist() return QA_DataStruct_Financial(QA_fetch_financial_report(code, timerange, ltype=ltype))
[ "高级财务查询接口", "Arguments", ":", "code", "{", "[", "type", "]", "}", "--", "[", "description", "]", "start", "{", "[", "type", "]", "}", "--", "[", "description", "]", "Keyword", "Arguments", ":", "end", "{", "[", "type", "]", "}", "--", "[", "descrip...
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L516-L532
[ "def", "QA_fetch_financial_report_adv", "(", "code", ",", "start", ",", "end", "=", "None", ",", "ltype", "=", "'EN'", ")", ":", "if", "end", "is", "None", ":", "return", "QA_DataStruct_Financial", "(", "QA_fetch_financial_report", "(", "code", ",", "start", ...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_stock_financial_calendar_adv
获取股票日线
QUANTAXIS/QAFetch/QAQuery_Advance.py
def QA_fetch_stock_financial_calendar_adv(code, start="all", end=None, format='pd', collections=DATABASE.report_calendar): '获取股票日线' #code= [code] if isinstance(code,str) else code end = start if end is None else end start = str(start)[0:10] end = str(end)[0:10] # code checking if start == 'all': start = '1990-01-01' end = str(datetime.date.today()) if end is None: return QA_DataStruct_Financial(QA_fetch_stock_financial_calendar(code, start, str(datetime.date.today()))) else: series = pd.Series( data=month_data, index=pd.to_datetime(month_data), name='date') timerange = series.loc[start:end].tolist() return QA_DataStruct_Financial(QA_fetch_stock_financial_calendar(code, start, end))
def QA_fetch_stock_financial_calendar_adv(code, start="all", end=None, format='pd', collections=DATABASE.report_calendar): '获取股票日线' #code= [code] if isinstance(code,str) else code end = start if end is None else end start = str(start)[0:10] end = str(end)[0:10] # code checking if start == 'all': start = '1990-01-01' end = str(datetime.date.today()) if end is None: return QA_DataStruct_Financial(QA_fetch_stock_financial_calendar(code, start, str(datetime.date.today()))) else: series = pd.Series( data=month_data, index=pd.to_datetime(month_data), name='date') timerange = series.loc[start:end].tolist() return QA_DataStruct_Financial(QA_fetch_stock_financial_calendar(code, start, end))
[ "获取股票日线" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QAQuery_Advance.py#L572-L591
[ "def", "QA_fetch_stock_financial_calendar_adv", "(", "code", ",", "start", "=", "\"all\"", ",", "end", "=", "None", ",", "format", "=", "'pd'", ",", "collections", "=", "DATABASE", ".", "report_calendar", ")", ":", "#code= [code] if isinstance(code,str) else code", ...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_fetch_get_tdxtraderecord
QUANTAXIS 读取历史交易记录 通达信 历史成交-输出-xlsfile--转换csvfile
QUANTAXIS/QAFetch/QATradeFile.py
def QA_fetch_get_tdxtraderecord(file): """ QUANTAXIS 读取历史交易记录 通达信 历史成交-输出-xlsfile--转换csvfile """ try: with open('./20180606.csv', 'r') as f: l = csv.reader(f) data = [item for item in l] res = pd.DataFrame(data[1:], columns=data[0]) return res except: raise IOError('QA CANNOT READ THIS RECORD')
def QA_fetch_get_tdxtraderecord(file): """ QUANTAXIS 读取历史交易记录 通达信 历史成交-输出-xlsfile--转换csvfile """ try: with open('./20180606.csv', 'r') as f: l = csv.reader(f) data = [item for item in l] res = pd.DataFrame(data[1:], columns=data[0]) return res except: raise IOError('QA CANNOT READ THIS RECORD')
[ "QUANTAXIS", "读取历史交易记录", "通达信", "历史成交", "-", "输出", "-", "xlsfile", "--", "转换csvfile" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAFetch/QATradeFile.py#L39-L51
[ "def", "QA_fetch_get_tdxtraderecord", "(", "file", ")", ":", "try", ":", "with", "open", "(", "'./20180606.csv'", ",", "'r'", ")", "as", "f", ":", "l", "=", "csv", ".", "reader", "(", "f", ")", "data", "=", "[", "item", "for", "item", "in", "l", "]...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
AROON
阿隆指标 Arguments: DataFrame {[type]} -- [description] Keyword Arguments: N {int} -- [description] (default: {14}) Returns: [type] -- [description]
QUANTAXIS/QAIndicator/talib_indicators.py
def AROON(DataFrame, N=14): """阿隆指标 Arguments: DataFrame {[type]} -- [description] Keyword Arguments: N {int} -- [description] (default: {14}) Returns: [type] -- [description] """ ar_up, ar_down = talib.AROON(DataFrame.high.values, DataFrame.low.values, N) return pd.DataFrame({'AROON_UP': ar_up,'AROON_DOWN': ar_down}, index=DataFrame.index)
def AROON(DataFrame, N=14): """阿隆指标 Arguments: DataFrame {[type]} -- [description] Keyword Arguments: N {int} -- [description] (default: {14}) Returns: [type] -- [description] """ ar_up, ar_down = talib.AROON(DataFrame.high.values, DataFrame.low.values, N) return pd.DataFrame({'AROON_UP': ar_up,'AROON_DOWN': ar_down}, index=DataFrame.index)
[ "阿隆指标", "Arguments", ":", "DataFrame", "{", "[", "type", "]", "}", "--", "[", "description", "]", "Keyword", "Arguments", ":", "N", "{", "int", "}", "--", "[", "description", "]", "(", "default", ":", "{", "14", "}", ")", "Returns", ":", "[", "type...
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAIndicator/talib_indicators.py#L55-L69
[ "def", "AROON", "(", "DataFrame", ",", "N", "=", "14", ")", ":", "ar_up", ",", "ar_down", "=", "talib", ".", "AROON", "(", "DataFrame", ".", "high", ".", "values", ",", "DataFrame", ".", "low", ".", "values", ",", "N", ")", "return", "pd", ".", "...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
MARKET_PRESET.get_commission_coeff
当前无法区分是百分比还是按手数收费,不过可以拿到以后自行判断
QUANTAXIS/QAARP/market_preset.py
def get_commission_coeff(self, code): """ 当前无法区分是百分比还是按手数收费,不过可以拿到以后自行判断 """ return max(self.get_code(code).get('commission_coeff_peramount'), self.get_code(code).get('commission_coeff_pervol'))
def get_commission_coeff(self, code): """ 当前无法区分是百分比还是按手数收费,不过可以拿到以后自行判断 """ return max(self.get_code(code).get('commission_coeff_peramount'), self.get_code(code).get('commission_coeff_pervol'))
[ "当前无法区分是百分比还是按手数收费", "不过可以拿到以后自行判断" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAARP/market_preset.py#L638-L643
[ "def", "get_commission_coeff", "(", "self", ",", "code", ")", ":", "return", "max", "(", "self", ".", "get_code", "(", "code", ")", ".", "get", "(", "'commission_coeff_peramount'", ")", ",", "self", ".", "get_code", "(", "code", ")", ".", "get", "(", "...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QAAnalysis_trade.import_trade
trade是一个可迭代的list/generator
QUANTAXIS/QAAnalysis/QAAnalysis_trade.py
def import_trade(self, trade): """ trade是一个可迭代的list/generator """ for item in trade: self.make_deal(item.code, item.datetime, item.amount, item.towards, item.price.item.order_model, item.amount_model)
def import_trade(self, trade): """ trade是一个可迭代的list/generator """ for item in trade: self.make_deal(item.code, item.datetime, item.amount, item.towards, item.price.item.order_model, item.amount_model)
[ "trade是一个可迭代的list", "/", "generator" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAAnalysis/QAAnalysis_trade.py#L54-L60
[ "def", "import_trade", "(", "self", ",", "trade", ")", ":", "for", "item", "in", "trade", ":", "self", ".", "make_deal", "(", "item", ".", "code", ",", "item", ".", "datetime", ",", "item", ".", "amount", ",", "item", ".", "towards", ",", "item", "...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QAAnalysis_trade.make_deal
这是一个一定会成交,并且立刻结转(及t+0)的交易入口
QUANTAXIS/QAAnalysis/QAAnalysis_trade.py
def make_deal(self, code, datetime, amount=100, towards=ORDER_DIRECTION.BUY, price=0, order_model=ORDER_MODEL.MARKET, amount_model=AMOUNT_MODEL.BY_AMOUNT): """ 这是一个一定会成交,并且立刻结转(及t+0)的交易入口 """ self.account.receive_deal(self.backtest_broker.receive_order(QA_Event(order=self.account.send_order( code=code, time=datetime, amount=amount, towards=towards, price=price, order_model=order_model, amount_model=amount_model )))) self.account.settle()
def make_deal(self, code, datetime, amount=100, towards=ORDER_DIRECTION.BUY, price=0, order_model=ORDER_MODEL.MARKET, amount_model=AMOUNT_MODEL.BY_AMOUNT): """ 这是一个一定会成交,并且立刻结转(及t+0)的交易入口 """ self.account.receive_deal(self.backtest_broker.receive_order(QA_Event(order=self.account.send_order( code=code, time=datetime, amount=amount, towards=towards, price=price, order_model=order_model, amount_model=amount_model )))) self.account.settle()
[ "这是一个一定会成交", "并且立刻结转", "(", "及t", "+", "0", ")", "的交易入口" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAAnalysis/QAAnalysis_trade.py#L62-L69
[ "def", "make_deal", "(", "self", ",", "code", ",", "datetime", ",", "amount", "=", "100", ",", "towards", "=", "ORDER_DIRECTION", ".", "BUY", ",", "price", "=", "0", ",", "order_model", "=", "ORDER_MODEL", ".", "MARKET", ",", "amount_model", "=", "AMOUNT...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_Order.cancel
撤单 Arguments: amount {int} -- 撤单数量
QUANTAXIS/QAMarket/QAOrder.py
def cancel(self): """撤单 Arguments: amount {int} -- 撤单数量 """ self.cancel_amount = self.amount - self.trade_amount if self.trade_amount == 0: # 未交易 直接订单全撤 self._status = ORDER_STATUS.CANCEL_ALL else: # 部分交易 剩余订单全撤 self._status = ORDER_STATUS.CANCEL_PART
def cancel(self): """撤单 Arguments: amount {int} -- 撤单数量 """ self.cancel_amount = self.amount - self.trade_amount if self.trade_amount == 0: # 未交易 直接订单全撤 self._status = ORDER_STATUS.CANCEL_ALL else: # 部分交易 剩余订单全撤 self._status = ORDER_STATUS.CANCEL_PART
[ "撤单" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAMarket/QAOrder.py#L246-L259
[ "def", "cancel", "(", "self", ")", ":", "self", ".", "cancel_amount", "=", "self", ".", "amount", "-", "self", ".", "trade_amount", "if", "self", ".", "trade_amount", "==", "0", ":", "# 未交易 直接订单全撤", "self", ".", "_status", "=", "ORDER_STATUS", ".", "CAN...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_Order.failed
失败订单(未成功创建入broker) Arguments: reason {str} -- 失败原因
QUANTAXIS/QAMarket/QAOrder.py
def failed(self, reason=None): """失败订单(未成功创建入broker) Arguments: reason {str} -- 失败原因 """ # 订单创建失败(如废单/场外废单/价格高于涨停价/价格低于跌停价/通讯失败) self._status = ORDER_STATUS.FAILED self.reason = str(reason)
def failed(self, reason=None): """失败订单(未成功创建入broker) Arguments: reason {str} -- 失败原因 """ # 订单创建失败(如废单/场外废单/价格高于涨停价/价格低于跌停价/通讯失败) self._status = ORDER_STATUS.FAILED self.reason = str(reason)
[ "失败订单", "(", "未成功创建入broker", ")" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAMarket/QAOrder.py#L261-L269
[ "def", "failed", "(", "self", ",", "reason", "=", "None", ")", ":", "# 订单创建失败(如废单/场外废单/价格高于涨停价/价格低于跌停价/通讯失败)", "self", ".", "_status", "=", "ORDER_STATUS", ".", "FAILED", "self", ".", "reason", "=", "str", "(", "reason", ")" ]
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_Order.trade
trade 状态 Arguments: amount {[type]} -- [description]
QUANTAXIS/QAMarket/QAOrder.py
def trade(self, trade_id, trade_price, trade_amount, trade_time): """trade 状态 Arguments: amount {[type]} -- [description] """ if self.status in [ORDER_STATUS.SUCCESS_PART, ORDER_STATUS.QUEUED]: trade_amount = int(trade_amount) trade_id = str(trade_id) if trade_amount < 1: self._status = ORDER_STATUS.NEXT else: if trade_id not in self.trade_id: trade_price = float(trade_price) trade_time = str(trade_time) self.trade_id.append(trade_id) self.trade_price = ( self.trade_price * self.trade_amount + trade_price * trade_amount ) / ( self.trade_amount + trade_amount ) self.trade_amount += trade_amount self.trade_time.append(trade_time) self.callback( self.code, trade_id, self.order_id, self.realorder_id, trade_price, trade_amount, self.towards, trade_time ) else: pass else: raise RuntimeError( 'ORDER STATUS {} CANNNOT TRADE'.format(self.status) )
def trade(self, trade_id, trade_price, trade_amount, trade_time): """trade 状态 Arguments: amount {[type]} -- [description] """ if self.status in [ORDER_STATUS.SUCCESS_PART, ORDER_STATUS.QUEUED]: trade_amount = int(trade_amount) trade_id = str(trade_id) if trade_amount < 1: self._status = ORDER_STATUS.NEXT else: if trade_id not in self.trade_id: trade_price = float(trade_price) trade_time = str(trade_time) self.trade_id.append(trade_id) self.trade_price = ( self.trade_price * self.trade_amount + trade_price * trade_amount ) / ( self.trade_amount + trade_amount ) self.trade_amount += trade_amount self.trade_time.append(trade_time) self.callback( self.code, trade_id, self.order_id, self.realorder_id, trade_price, trade_amount, self.towards, trade_time ) else: pass else: raise RuntimeError( 'ORDER STATUS {} CANNNOT TRADE'.format(self.status) )
[ "trade", "状态" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAMarket/QAOrder.py#L271-L314
[ "def", "trade", "(", "self", ",", "trade_id", ",", "trade_price", ",", "trade_amount", ",", "trade_time", ")", ":", "if", "self", ".", "status", "in", "[", "ORDER_STATUS", ".", "SUCCESS_PART", ",", "ORDER_STATUS", ".", "QUEUED", "]", ":", "trade_amount", "...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_Order.to_otgdict
{ "aid": "insert_order", # //必填, 下单请求 # //必填, 需要与登录用户名一致, 或为登录用户的子账户(例如登录用户为user1, 则报单 user_id 应当为 user1 或 user1.some_unit) "user_id": account_cookie, # //必填, 委托单号, 需确保在一个账号中不重复, 限长512字节 "order_id": order_id if order_id else QA.QA_util_random_with_topic('QAOTG'), "exchange_id": exchange_id, # //必填, 下单到哪个交易所 "instrument_id": code, # //必填, 下单合约代码 "direction": order_direction, # //必填, 下单买卖方向 # //必填, 下单开平方向, 仅当指令相关对象不支持开平机制(例如股票)时可不填写此字段 "offset": order_offset, "volume": volume, # //必填, 下单手数 "price_type": "LIMIT", # //必填, 报单价格类型 "limit_price": price, # //当 price_type == LIMIT 时需要填写此字段, 报单价格 "volume_condition": "ANY", "time_condition": "GFD", }
QUANTAXIS/QAMarket/QAOrder.py
def to_otgdict(self): """{ "aid": "insert_order", # //必填, 下单请求 # //必填, 需要与登录用户名一致, 或为登录用户的子账户(例如登录用户为user1, 则报单 user_id 应当为 user1 或 user1.some_unit) "user_id": account_cookie, # //必填, 委托单号, 需确保在一个账号中不重复, 限长512字节 "order_id": order_id if order_id else QA.QA_util_random_with_topic('QAOTG'), "exchange_id": exchange_id, # //必填, 下单到哪个交易所 "instrument_id": code, # //必填, 下单合约代码 "direction": order_direction, # //必填, 下单买卖方向 # //必填, 下单开平方向, 仅当指令相关对象不支持开平机制(例如股票)时可不填写此字段 "offset": order_offset, "volume": volume, # //必填, 下单手数 "price_type": "LIMIT", # //必填, 报单价格类型 "limit_price": price, # //当 price_type == LIMIT 时需要填写此字段, 报单价格 "volume_condition": "ANY", "time_condition": "GFD", } """ return { "aid": "insert_order", # //必填, 下单请求 # //必填, 需要与登录用户名一致, 或为登录用户的子账户(例如登录用户为user1, 则报单 user_id 应当为 user1 或 user1.some_unit) "user_id": self.account_cookie, # //必填, 委托单号, 需确保在一个账号中不重复, 限长512字节 "order_id": self.order_id, "exchange_id": self.exchange_id, # //必填, 下单到哪个交易所 "instrument_id": self.code, # //必填, 下单合约代码 "direction": self.direction, # //必填, 下单买卖方向 # //必填, 下单开平方向, 仅当指令相关对象不支持开平机制(例如股票)时可不填写此字段 "offset": self.offset, "volume": self.amount, # //必填, 下单手数 "price_type": self.order_model, # //必填, 报单价格类型 "limit_price": self.price, # //当 price_type == LIMIT 时需要填写此字段, 报单价格 "volume_condition": "ANY", "time_condition": "GFD", }
def to_otgdict(self): """{ "aid": "insert_order", # //必填, 下单请求 # //必填, 需要与登录用户名一致, 或为登录用户的子账户(例如登录用户为user1, 则报单 user_id 应当为 user1 或 user1.some_unit) "user_id": account_cookie, # //必填, 委托单号, 需确保在一个账号中不重复, 限长512字节 "order_id": order_id if order_id else QA.QA_util_random_with_topic('QAOTG'), "exchange_id": exchange_id, # //必填, 下单到哪个交易所 "instrument_id": code, # //必填, 下单合约代码 "direction": order_direction, # //必填, 下单买卖方向 # //必填, 下单开平方向, 仅当指令相关对象不支持开平机制(例如股票)时可不填写此字段 "offset": order_offset, "volume": volume, # //必填, 下单手数 "price_type": "LIMIT", # //必填, 报单价格类型 "limit_price": price, # //当 price_type == LIMIT 时需要填写此字段, 报单价格 "volume_condition": "ANY", "time_condition": "GFD", } """ return { "aid": "insert_order", # //必填, 下单请求 # //必填, 需要与登录用户名一致, 或为登录用户的子账户(例如登录用户为user1, 则报单 user_id 应当为 user1 或 user1.some_unit) "user_id": self.account_cookie, # //必填, 委托单号, 需确保在一个账号中不重复, 限长512字节 "order_id": self.order_id, "exchange_id": self.exchange_id, # //必填, 下单到哪个交易所 "instrument_id": self.code, # //必填, 下单合约代码 "direction": self.direction, # //必填, 下单买卖方向 # //必填, 下单开平方向, 仅当指令相关对象不支持开平机制(例如股票)时可不填写此字段 "offset": self.offset, "volume": self.amount, # //必填, 下单手数 "price_type": self.order_model, # //必填, 报单价格类型 "limit_price": self.price, # //当 price_type == LIMIT 时需要填写此字段, 报单价格 "volume_condition": "ANY", "time_condition": "GFD", }
[ "{", "aid", ":", "insert_order", "#", "//", "必填", "下单请求", "#", "//", "必填", "需要与登录用户名一致", "或为登录用户的子账户", "(", "例如登录用户为user1", "则报单", "user_id", "应当为", "user1", "或", "user1", ".", "some_unit", ")", "user_id", ":", "account_cookie", "#", "//", "必填", "委托单号", ...
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAMarket/QAOrder.py#L365-L400
[ "def", "to_otgdict", "(", "self", ")", ":", "return", "{", "\"aid\"", ":", "\"insert_order\"", ",", "# //必填, 下单请求", "# //必填, 需要与登录用户名一致, 或为登录用户的子账户(例如登录用户为user1, 则报单 user_id 应当为 user1 或 user1.some_unit)", "\"user_id\"", ":", "self", ".", "account_cookie", ",", "# //必填, 委托单号, ...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_Order.from_otgformat
[summary] Arguments: otgOrder {[type]} -- [description] {'seqno': 6, 'user_id': '106184', 'order_id': 'WDRB_QA01_FtNlyBem', 'exchange_id': 'SHFE', 'instrument_id': 'rb1905', 'direction': 'SELL', 'offset': 'OPEN', 'volume_orign': 50, #(总报单手数) 'price_type': 'LIMIT', # "LIMIT" (价格类型, ANY=市价, LIMIT=限价) 'limit_price': 3432.0, # 4500.0 (委托价格, 仅当 price_type = LIMIT 时有效) 'time_condition': 'GFD',# "GFD" (时间条件, IOC=立即完成,否则撤销, GFS=本节有效, GFD=当日有效, GTC=撤销前有效, GFA=集合竞价有效) 'volume_condition': 'ANY', # "ANY" (手数条件, ANY=任何数量, MIN=最小数量, ALL=全部数量) 'insert_date_time': 1545656460000000000,# 1501074872000000000 (下单时间(按北京时间),自unix epoch(1970-01-01 00:00:00 GMT)以来的纳秒数) 'exchange_order_id': ' 3738', 'status': 'FINISHED', # "ALIVE" (委托单状态, ALIVE=有效, FINISHED=已完) 'volume_left': 0, 'last_msg': '全部成交报单已提交'} # "报单成功" (委托单状态信息)
QUANTAXIS/QAMarket/QAOrder.py
def from_otgformat(self, otgOrder): """[summary] Arguments: otgOrder {[type]} -- [description] {'seqno': 6, 'user_id': '106184', 'order_id': 'WDRB_QA01_FtNlyBem', 'exchange_id': 'SHFE', 'instrument_id': 'rb1905', 'direction': 'SELL', 'offset': 'OPEN', 'volume_orign': 50, #(总报单手数) 'price_type': 'LIMIT', # "LIMIT" (价格类型, ANY=市价, LIMIT=限价) 'limit_price': 3432.0, # 4500.0 (委托价格, 仅当 price_type = LIMIT 时有效) 'time_condition': 'GFD',# "GFD" (时间条件, IOC=立即完成,否则撤销, GFS=本节有效, GFD=当日有效, GTC=撤销前有效, GFA=集合竞价有效) 'volume_condition': 'ANY', # "ANY" (手数条件, ANY=任何数量, MIN=最小数量, ALL=全部数量) 'insert_date_time': 1545656460000000000,# 1501074872000000000 (下单时间(按北京时间),自unix epoch(1970-01-01 00:00:00 GMT)以来的纳秒数) 'exchange_order_id': ' 3738', 'status': 'FINISHED', # "ALIVE" (委托单状态, ALIVE=有效, FINISHED=已完) 'volume_left': 0, 'last_msg': '全部成交报单已提交'} # "报单成功" (委托单状态信息) """ self.order_id = otgOrder.get('order_id') self.account_cookie = otgOrder.get('user_id') self.exchange_id = otgOrder.get('exchange_id') self.code = str(otgOrder.get('instrument_id')).upper() self.offset = otgOrder.get('offset') self.direction = otgOrder.get('direction') self.towards = eval('ORDER_DIRECTION.{}_{}'.format( self.direction, self.offset )) self.amount = otgOrder.get('volume_orign') self.trade_amount = self.amount - otgOrder.get('volume_left') self.price = otgOrder.get('limit_price') self.order_model = eval( 'ORDER_MODEL.{}'.format(otgOrder.get('price_type')) ) self.time_condition = otgOrder.get('time_condition') if otgOrder.get('insert_date_time') == 0: self.datetime = 0 else: self.datetime = QA_util_stamp2datetime( int(otgOrder.get('insert_date_time')) ) self.sending_time = self.datetime self.volume_condition = otgOrder.get('volume_condition') self.message = otgOrder.get('last_msg') self._status = ORDER_STATUS.NEW if '已撤单' in self.message or '拒绝' in self.message or '仓位不足' in self.message: # 仓位不足: 一般是平今/平昨仓位不足 self._status = ORDER_STATUS.FAILED self.realorder_id = otgOrder.get('exchange_order_id') return self
def from_otgformat(self, otgOrder): """[summary] Arguments: otgOrder {[type]} -- [description] {'seqno': 6, 'user_id': '106184', 'order_id': 'WDRB_QA01_FtNlyBem', 'exchange_id': 'SHFE', 'instrument_id': 'rb1905', 'direction': 'SELL', 'offset': 'OPEN', 'volume_orign': 50, #(总报单手数) 'price_type': 'LIMIT', # "LIMIT" (价格类型, ANY=市价, LIMIT=限价) 'limit_price': 3432.0, # 4500.0 (委托价格, 仅当 price_type = LIMIT 时有效) 'time_condition': 'GFD',# "GFD" (时间条件, IOC=立即完成,否则撤销, GFS=本节有效, GFD=当日有效, GTC=撤销前有效, GFA=集合竞价有效) 'volume_condition': 'ANY', # "ANY" (手数条件, ANY=任何数量, MIN=最小数量, ALL=全部数量) 'insert_date_time': 1545656460000000000,# 1501074872000000000 (下单时间(按北京时间),自unix epoch(1970-01-01 00:00:00 GMT)以来的纳秒数) 'exchange_order_id': ' 3738', 'status': 'FINISHED', # "ALIVE" (委托单状态, ALIVE=有效, FINISHED=已完) 'volume_left': 0, 'last_msg': '全部成交报单已提交'} # "报单成功" (委托单状态信息) """ self.order_id = otgOrder.get('order_id') self.account_cookie = otgOrder.get('user_id') self.exchange_id = otgOrder.get('exchange_id') self.code = str(otgOrder.get('instrument_id')).upper() self.offset = otgOrder.get('offset') self.direction = otgOrder.get('direction') self.towards = eval('ORDER_DIRECTION.{}_{}'.format( self.direction, self.offset )) self.amount = otgOrder.get('volume_orign') self.trade_amount = self.amount - otgOrder.get('volume_left') self.price = otgOrder.get('limit_price') self.order_model = eval( 'ORDER_MODEL.{}'.format(otgOrder.get('price_type')) ) self.time_condition = otgOrder.get('time_condition') if otgOrder.get('insert_date_time') == 0: self.datetime = 0 else: self.datetime = QA_util_stamp2datetime( int(otgOrder.get('insert_date_time')) ) self.sending_time = self.datetime self.volume_condition = otgOrder.get('volume_condition') self.message = otgOrder.get('last_msg') self._status = ORDER_STATUS.NEW if '已撤单' in self.message or '拒绝' in self.message or '仓位不足' in self.message: # 仓位不足: 一般是平今/平昨仓位不足 self._status = ORDER_STATUS.FAILED self.realorder_id = otgOrder.get('exchange_order_id') return self
[ "[", "summary", "]" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAMarket/QAOrder.py#L419-L476
[ "def", "from_otgformat", "(", "self", ",", "otgOrder", ")", ":", "self", ".", "order_id", "=", "otgOrder", ".", "get", "(", "'order_id'", ")", "self", ".", "account_cookie", "=", "otgOrder", ".", "get", "(", "'user_id'", ")", "self", ".", "exchange_id", ...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_Order.from_dict
从字段类型的字段 填充 对象的字段 :param order_dict: dict 类型 :return: self QA_Order
QUANTAXIS/QAMarket/QAOrder.py
def from_dict(self, order_dict): ''' 从字段类型的字段 填充 对象的字段 :param order_dict: dict 类型 :return: self QA_Order ''' try: # QA_util_log_info('QA_ORDER CHANGE: from {} change to {}'.format( # self.order_id, order['order_id'])) self.price = order_dict['price'] self.date = order_dict['date'] self.datetime = order_dict['datetime'] self.sending_time = order_dict['sending_time'] # 下单时间 self.trade_time = order_dict['trade_time'] self.amount = order_dict['amount'] self.frequence = order_dict['frequence'] self.market_type = order_dict['market_type'] self.towards = order_dict['towards'] self.code = order_dict['code'] self.user = order_dict['user'] self.account_cookie = order_dict['account_cookie'] self.strategy = order_dict['strategy'] self.type = order_dict['type'] self.order_model = order_dict['order_model'] self.amount_model = order_dict['amount_model'] self.order_id = order_dict['order_id'] self.realorder_id = order_dict['realorder_id'] self.trade_id = order_dict['trade_id'] self.callback = order_dict['callback'] self.commission_coeff = order_dict['commission_coeff'] self.tax_coeff = order_dict['tax_coeff'] self.money = order_dict['money'] self._status = order_dict['_status'] self.cancel_amount = order_dict['cancel_amount'] self.trade_amount = order_dict['trade_amount'] self.trade_price = order_dict['trade_price'] self.reason = order_dict['reason'] return self except Exception as e: QA_util_log_info('Failed to tran from dict {}'.format(e))
def from_dict(self, order_dict): ''' 从字段类型的字段 填充 对象的字段 :param order_dict: dict 类型 :return: self QA_Order ''' try: # QA_util_log_info('QA_ORDER CHANGE: from {} change to {}'.format( # self.order_id, order['order_id'])) self.price = order_dict['price'] self.date = order_dict['date'] self.datetime = order_dict['datetime'] self.sending_time = order_dict['sending_time'] # 下单时间 self.trade_time = order_dict['trade_time'] self.amount = order_dict['amount'] self.frequence = order_dict['frequence'] self.market_type = order_dict['market_type'] self.towards = order_dict['towards'] self.code = order_dict['code'] self.user = order_dict['user'] self.account_cookie = order_dict['account_cookie'] self.strategy = order_dict['strategy'] self.type = order_dict['type'] self.order_model = order_dict['order_model'] self.amount_model = order_dict['amount_model'] self.order_id = order_dict['order_id'] self.realorder_id = order_dict['realorder_id'] self.trade_id = order_dict['trade_id'] self.callback = order_dict['callback'] self.commission_coeff = order_dict['commission_coeff'] self.tax_coeff = order_dict['tax_coeff'] self.money = order_dict['money'] self._status = order_dict['_status'] self.cancel_amount = order_dict['cancel_amount'] self.trade_amount = order_dict['trade_amount'] self.trade_price = order_dict['trade_price'] self.reason = order_dict['reason'] return self except Exception as e: QA_util_log_info('Failed to tran from dict {}'.format(e))
[ "从字段类型的字段", "填充", "对象的字段", ":", "param", "order_dict", ":", "dict", "类型", ":", "return", ":", "self", "QA_Order" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAMarket/QAOrder.py#L478-L520
[ "def", "from_dict", "(", "self", ",", "order_dict", ")", ":", "try", ":", "# QA_util_log_info('QA_ORDER CHANGE: from {} change to {}'.format(", "# self.order_id, order['order_id']))", "self", ".", "price", "=", "order_dict", "[", "'price'", "]", "self", ".", "date", ...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_OrderQueue.insert_order
:param order: QA_Order类型 :return:
QUANTAXIS/QAMarket/QAOrder.py
def insert_order(self, order): ''' :param order: QA_Order类型 :return: ''' #print(" *>> QAOrder!insert_order {}".format(order)) # QUEUED = 300 # queued 用于表示在order_queue中 实际表达的意思是订单存活 待成交 #order.status = ORDER_STATUS.QUEUED # 🛠 todo 是为了速度快把order对象转换成 df 对象的吗? #self.queue_df = self.queue_df.append(order.to_df(), ignore_index=True) #self.queue_df.set_index('order_id', drop=True, inplace=True) if order is not None: self.order_list[order.order_id] = order return order else: print('QAERROR Wrong for get None type while insert order to Queue')
def insert_order(self, order): ''' :param order: QA_Order类型 :return: ''' #print(" *>> QAOrder!insert_order {}".format(order)) # QUEUED = 300 # queued 用于表示在order_queue中 实际表达的意思是订单存活 待成交 #order.status = ORDER_STATUS.QUEUED # 🛠 todo 是为了速度快把order对象转换成 df 对象的吗? #self.queue_df = self.queue_df.append(order.to_df(), ignore_index=True) #self.queue_df.set_index('order_id', drop=True, inplace=True) if order is not None: self.order_list[order.order_id] = order return order else: print('QAERROR Wrong for get None type while insert order to Queue')
[ ":", "param", "order", ":", "QA_Order类型", ":", "return", ":" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAMarket/QAOrder.py#L557-L572
[ "def", "insert_order", "(", "self", ",", "order", ")", ":", "#print(\" *>> QAOrder!insert_order {}\".format(order))", "# QUEUED = 300 # queued 用于表示在order_queue中 实际表达的意思是订单存活 待成交", "#order.status = ORDER_STATUS.QUEUED", "# 🛠 todo 是为了速度快把order对象转换成 df 对象的吗?", "#self.queue_df = self.queu...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_OrderQueue.pending
600 废单 未委托成功 200 委托成功,完全交易 203 委托成功,未完全成功 300 委托队列 待成交 400 已撤单 500 服务器撤单/每日结算 订单生成(100) -- 废单(600) 订单生成(100) -- 进入待成交队列(300) -- 完全成交(200) -- 每日结算(500)-- 死亡 订单生成(100) -- 进入待成交队列(300) -- 部分成交(203) -- 未成交(300) -- 每日结算(500) -- 死亡 订单生成(100) -- 进入待成交队列(300) -- 主动撤单(400) -- 每日结算(500) -- 死亡 选择待成交列表 :return: dataframe
QUANTAXIS/QAMarket/QAOrder.py
def pending(self): ''' 600 废单 未委托成功 200 委托成功,完全交易 203 委托成功,未完全成功 300 委托队列 待成交 400 已撤单 500 服务器撤单/每日结算 订单生成(100) -- 废单(600) 订单生成(100) -- 进入待成交队列(300) -- 完全成交(200) -- 每日结算(500)-- 死亡 订单生成(100) -- 进入待成交队列(300) -- 部分成交(203) -- 未成交(300) -- 每日结算(500) -- 死亡 订单生成(100) -- 进入待成交队列(300) -- 主动撤单(400) -- 每日结算(500) -- 死亡 选择待成交列表 :return: dataframe ''' try: return [ item for item in self.order_list.values() if item.status in [ ORDER_STATUS.QUEUED, ORDER_STATUS.NEXT, ORDER_STATUS.SUCCESS_PART ] ] except: return []
def pending(self): ''' 600 废单 未委托成功 200 委托成功,完全交易 203 委托成功,未完全成功 300 委托队列 待成交 400 已撤单 500 服务器撤单/每日结算 订单生成(100) -- 废单(600) 订单生成(100) -- 进入待成交队列(300) -- 完全成交(200) -- 每日结算(500)-- 死亡 订单生成(100) -- 进入待成交队列(300) -- 部分成交(203) -- 未成交(300) -- 每日结算(500) -- 死亡 订单生成(100) -- 进入待成交队列(300) -- 主动撤单(400) -- 每日结算(500) -- 死亡 选择待成交列表 :return: dataframe ''' try: return [ item for item in self.order_list.values() if item.status in [ ORDER_STATUS.QUEUED, ORDER_STATUS.NEXT, ORDER_STATUS.SUCCESS_PART ] ] except: return []
[ "600", "废单", "未委托成功", "200", "委托成功", "完全交易", "203", "委托成功", "未完全成功", "300", "委托队列", "待成交", "400", "已撤单", "500", "服务器撤单", "/", "每日结算" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAMarket/QAOrder.py#L593-L619
[ "def", "pending", "(", "self", ")", ":", "try", ":", "return", "[", "item", "for", "item", "in", "self", ".", "order_list", ".", "values", "(", ")", "if", "item", ".", "status", "in", "[", "ORDER_STATUS", ".", "QUEUED", ",", "ORDER_STATUS", ".", "NEX...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
_QA_data_stock_to_fq
使用数据库数据进行复权
QUANTAXIS/QAData/data_fq.py
def _QA_data_stock_to_fq(bfq_data, xdxr_data, fqtype): '使用数据库数据进行复权' info = xdxr_data.query('category==1') bfq_data = bfq_data.assign(if_trade=1) if len(info) > 0: data = pd.concat( [ bfq_data, info.loc[bfq_data.index[0]:bfq_data.index[-1], ['category']] ], axis=1 ) data['if_trade'].fillna(value=0, inplace=True) data = data.fillna(method='ffill') data = pd.concat( [ data, info.loc[bfq_data.index[0]:bfq_data.index[-1], ['fenhong', 'peigu', 'peigujia', 'songzhuangu']] ], axis=1 ) else: data = pd.concat( [ bfq_data, info. loc[:, ['category', 'fenhong', 'peigu', 'peigujia', 'songzhuangu']] ], axis=1 ) data = data.fillna(0) data['preclose'] = ( data['close'].shift(1) * 10 - data['fenhong'] + data['peigu'] * data['peigujia'] ) / (10 + data['peigu'] + data['songzhuangu']) if fqtype in ['01', 'qfq']: data['adj'] = (data['preclose'].shift(-1) / data['close']).fillna(1)[::-1].cumprod() else: data['adj'] = (data['close'] / data['preclose'].shift(-1)).cumprod().shift(1).fillna(1) for col in ['open', 'high', 'low', 'close', 'preclose']: data[col] = data[col] * data['adj'] data['volume'] = data['volume'] / \ data['adj'] if 'volume' in data.columns else data['vol']/data['adj'] try: data['high_limit'] = data['high_limit'] * data['adj'] data['low_limit'] = data['high_limit'] * data['adj'] except: pass return data.query('if_trade==1 and open != 0').drop( ['fenhong', 'peigu', 'peigujia', 'songzhuangu', 'if_trade', 'category'], axis=1, errors='ignore' )
def _QA_data_stock_to_fq(bfq_data, xdxr_data, fqtype): '使用数据库数据进行复权' info = xdxr_data.query('category==1') bfq_data = bfq_data.assign(if_trade=1) if len(info) > 0: data = pd.concat( [ bfq_data, info.loc[bfq_data.index[0]:bfq_data.index[-1], ['category']] ], axis=1 ) data['if_trade'].fillna(value=0, inplace=True) data = data.fillna(method='ffill') data = pd.concat( [ data, info.loc[bfq_data.index[0]:bfq_data.index[-1], ['fenhong', 'peigu', 'peigujia', 'songzhuangu']] ], axis=1 ) else: data = pd.concat( [ bfq_data, info. loc[:, ['category', 'fenhong', 'peigu', 'peigujia', 'songzhuangu']] ], axis=1 ) data = data.fillna(0) data['preclose'] = ( data['close'].shift(1) * 10 - data['fenhong'] + data['peigu'] * data['peigujia'] ) / (10 + data['peigu'] + data['songzhuangu']) if fqtype in ['01', 'qfq']: data['adj'] = (data['preclose'].shift(-1) / data['close']).fillna(1)[::-1].cumprod() else: data['adj'] = (data['close'] / data['preclose'].shift(-1)).cumprod().shift(1).fillna(1) for col in ['open', 'high', 'low', 'close', 'preclose']: data[col] = data[col] * data['adj'] data['volume'] = data['volume'] / \ data['adj'] if 'volume' in data.columns else data['vol']/data['adj'] try: data['high_limit'] = data['high_limit'] * data['adj'] data['low_limit'] = data['high_limit'] * data['adj'] except: pass return data.query('if_trade==1 and open != 0').drop( ['fenhong', 'peigu', 'peigujia', 'songzhuangu', 'if_trade', 'category'], axis=1, errors='ignore' )
[ "使用数据库数据进行复权" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAData/data_fq.py#L102-L176
[ "def", "_QA_data_stock_to_fq", "(", "bfq_data", ",", "xdxr_data", ",", "fqtype", ")", ":", "info", "=", "xdxr_data", ".", "query", "(", "'category==1'", ")", "bfq_data", "=", "bfq_data", ".", "assign", "(", "if_trade", "=", "1", ")", "if", "len", "(", "i...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
QA_data_stock_to_fq
股票 日线/分钟线 动态复权接口
QUANTAXIS/QAData/data_fq.py
def QA_data_stock_to_fq(__data, type_='01'): def __QA_fetch_stock_xdxr( code, format_='pd', collections=DATABASE.stock_xdxr ): '获取股票除权信息/数据库' try: data = pd.DataFrame( [item for item in collections.find({'code': code})] ).drop(['_id'], axis=1) data['date'] = pd.to_datetime(data['date']) return data.set_index(['date', 'code'], drop=False) except: return pd.DataFrame( data=[], columns=[ 'category', 'category_meaning', 'code', 'date', 'fenhong', 'fenshu', 'liquidity_after', 'liquidity_before', 'name', 'peigu', 'peigujia', 'shares_after', 'shares_before', 'songzhuangu', 'suogu', 'xingquanjia' ] ) '股票 日线/分钟线 动态复权接口' code = __data.index.remove_unused_levels().levels[1][0] if isinstance( __data.index, pd.core.indexes.multi.MultiIndex ) else __data['code'][0] return _QA_data_stock_to_fq( bfq_data=__data, xdxr_data=__QA_fetch_stock_xdxr(code), fqtype=type_ )
def QA_data_stock_to_fq(__data, type_='01'): def __QA_fetch_stock_xdxr( code, format_='pd', collections=DATABASE.stock_xdxr ): '获取股票除权信息/数据库' try: data = pd.DataFrame( [item for item in collections.find({'code': code})] ).drop(['_id'], axis=1) data['date'] = pd.to_datetime(data['date']) return data.set_index(['date', 'code'], drop=False) except: return pd.DataFrame( data=[], columns=[ 'category', 'category_meaning', 'code', 'date', 'fenhong', 'fenshu', 'liquidity_after', 'liquidity_before', 'name', 'peigu', 'peigujia', 'shares_after', 'shares_before', 'songzhuangu', 'suogu', 'xingquanjia' ] ) '股票 日线/分钟线 动态复权接口' code = __data.index.remove_unused_levels().levels[1][0] if isinstance( __data.index, pd.core.indexes.multi.MultiIndex ) else __data['code'][0] return _QA_data_stock_to_fq( bfq_data=__data, xdxr_data=__QA_fetch_stock_xdxr(code), fqtype=type_ )
[ "股票", "日线", "/", "分钟线", "动态复权接口" ]
QUANTAXIS/QUANTAXIS
python
https://github.com/QUANTAXIS/QUANTAXIS/blob/bb1fe424e4108b62a1f712b81a05cf829297a5c0/QUANTAXIS/QAData/data_fq.py#L179-L228
[ "def", "QA_data_stock_to_fq", "(", "__data", ",", "type_", "=", "'01'", ")", ":", "def", "__QA_fetch_stock_xdxr", "(", "code", ",", "format_", "=", "'pd'", ",", "collections", "=", "DATABASE", ".", "stock_xdxr", ")", ":", "'获取股票除权信息/数据库'", "try", ":", "data"...
bb1fe424e4108b62a1f712b81a05cf829297a5c0
train
ChatBot.get_response
Return the bot's response based on the input. :param statement: An statement object or string. :returns: A response to the input. :rtype: Statement :param additional_response_selection_parameters: Parameters to pass to the chat bot's logic adapters to control response selection. :type additional_response_selection_parameters: dict :param persist_values_to_response: Values that should be saved to the response that the chat bot generates. :type persist_values_to_response: dict
chatterbot/chatterbot.py
def get_response(self, statement=None, **kwargs): """ Return the bot's response based on the input. :param statement: An statement object or string. :returns: A response to the input. :rtype: Statement :param additional_response_selection_parameters: Parameters to pass to the chat bot's logic adapters to control response selection. :type additional_response_selection_parameters: dict :param persist_values_to_response: Values that should be saved to the response that the chat bot generates. :type persist_values_to_response: dict """ Statement = self.storage.get_object('statement') additional_response_selection_parameters = kwargs.pop('additional_response_selection_parameters', {}) persist_values_to_response = kwargs.pop('persist_values_to_response', {}) if isinstance(statement, str): kwargs['text'] = statement if isinstance(statement, dict): kwargs.update(statement) if statement is None and 'text' not in kwargs: raise self.ChatBotException( 'Either a statement object or a "text" keyword ' 'argument is required. Neither was provided.' ) if hasattr(statement, 'serialize'): kwargs.update(**statement.serialize()) tags = kwargs.pop('tags', []) text = kwargs.pop('text') input_statement = Statement(text=text, **kwargs) input_statement.add_tags(*tags) # Preprocess the input statement for preprocessor in self.preprocessors: input_statement = preprocessor(input_statement) # Make sure the input statement has its search text saved if not input_statement.search_text: input_statement.search_text = self.storage.tagger.get_bigram_pair_string(input_statement.text) if not input_statement.search_in_response_to and input_statement.in_response_to: input_statement.search_in_response_to = self.storage.tagger.get_bigram_pair_string(input_statement.in_response_to) response = self.generate_response(input_statement, additional_response_selection_parameters) # Update any response data that needs to be changed if persist_values_to_response: for response_key in persist_values_to_response: response_value = persist_values_to_response[response_key] if response_key == 'tags': input_statement.add_tags(*response_value) response.add_tags(*response_value) else: setattr(input_statement, response_key, response_value) setattr(response, response_key, response_value) if not self.read_only: self.learn_response(input_statement) # Save the response generated for the input self.storage.create(**response.serialize()) return response
def get_response(self, statement=None, **kwargs): """ Return the bot's response based on the input. :param statement: An statement object or string. :returns: A response to the input. :rtype: Statement :param additional_response_selection_parameters: Parameters to pass to the chat bot's logic adapters to control response selection. :type additional_response_selection_parameters: dict :param persist_values_to_response: Values that should be saved to the response that the chat bot generates. :type persist_values_to_response: dict """ Statement = self.storage.get_object('statement') additional_response_selection_parameters = kwargs.pop('additional_response_selection_parameters', {}) persist_values_to_response = kwargs.pop('persist_values_to_response', {}) if isinstance(statement, str): kwargs['text'] = statement if isinstance(statement, dict): kwargs.update(statement) if statement is None and 'text' not in kwargs: raise self.ChatBotException( 'Either a statement object or a "text" keyword ' 'argument is required. Neither was provided.' ) if hasattr(statement, 'serialize'): kwargs.update(**statement.serialize()) tags = kwargs.pop('tags', []) text = kwargs.pop('text') input_statement = Statement(text=text, **kwargs) input_statement.add_tags(*tags) # Preprocess the input statement for preprocessor in self.preprocessors: input_statement = preprocessor(input_statement) # Make sure the input statement has its search text saved if not input_statement.search_text: input_statement.search_text = self.storage.tagger.get_bigram_pair_string(input_statement.text) if not input_statement.search_in_response_to and input_statement.in_response_to: input_statement.search_in_response_to = self.storage.tagger.get_bigram_pair_string(input_statement.in_response_to) response = self.generate_response(input_statement, additional_response_selection_parameters) # Update any response data that needs to be changed if persist_values_to_response: for response_key in persist_values_to_response: response_value = persist_values_to_response[response_key] if response_key == 'tags': input_statement.add_tags(*response_value) response.add_tags(*response_value) else: setattr(input_statement, response_key, response_value) setattr(response, response_key, response_value) if not self.read_only: self.learn_response(input_statement) # Save the response generated for the input self.storage.create(**response.serialize()) return response
[ "Return", "the", "bot", "s", "response", "based", "on", "the", "input", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/chatterbot.py#L57-L133
[ "def", "get_response", "(", "self", ",", "statement", "=", "None", ",", "*", "*", "kwargs", ")", ":", "Statement", "=", "self", ".", "storage", ".", "get_object", "(", "'statement'", ")", "additional_response_selection_parameters", "=", "kwargs", ".", "pop", ...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
ChatBot.generate_response
Return a response based on a given input statement. :param input_statement: The input statement to be processed.
chatterbot/chatterbot.py
def generate_response(self, input_statement, additional_response_selection_parameters=None): """ Return a response based on a given input statement. :param input_statement: The input statement to be processed. """ Statement = self.storage.get_object('statement') results = [] result = None max_confidence = -1 for adapter in self.logic_adapters: if adapter.can_process(input_statement): output = adapter.process(input_statement, additional_response_selection_parameters) results.append(output) self.logger.info( '{} selected "{}" as a response with a confidence of {}'.format( adapter.class_name, output.text, output.confidence ) ) if output.confidence > max_confidence: result = output max_confidence = output.confidence else: self.logger.info( 'Not processing the statement using {}'.format(adapter.class_name) ) class ResultOption: def __init__(self, statement, count=1): self.statement = statement self.count = count # If multiple adapters agree on the same statement, # then that statement is more likely to be the correct response if len(results) >= 3: result_options = {} for result_option in results: result_string = result_option.text + ':' + (result_option.in_response_to or '') if result_string in result_options: result_options[result_string].count += 1 if result_options[result_string].statement.confidence < result_option.confidence: result_options[result_string].statement = result_option else: result_options[result_string] = ResultOption( result_option ) most_common = list(result_options.values())[0] for result_option in result_options.values(): if result_option.count > most_common.count: most_common = result_option if most_common.count > 1: result = most_common.statement response = Statement( text=result.text, in_response_to=input_statement.text, conversation=input_statement.conversation, persona='bot:' + self.name ) response.confidence = result.confidence return response
def generate_response(self, input_statement, additional_response_selection_parameters=None): """ Return a response based on a given input statement. :param input_statement: The input statement to be processed. """ Statement = self.storage.get_object('statement') results = [] result = None max_confidence = -1 for adapter in self.logic_adapters: if adapter.can_process(input_statement): output = adapter.process(input_statement, additional_response_selection_parameters) results.append(output) self.logger.info( '{} selected "{}" as a response with a confidence of {}'.format( adapter.class_name, output.text, output.confidence ) ) if output.confidence > max_confidence: result = output max_confidence = output.confidence else: self.logger.info( 'Not processing the statement using {}'.format(adapter.class_name) ) class ResultOption: def __init__(self, statement, count=1): self.statement = statement self.count = count # If multiple adapters agree on the same statement, # then that statement is more likely to be the correct response if len(results) >= 3: result_options = {} for result_option in results: result_string = result_option.text + ':' + (result_option.in_response_to or '') if result_string in result_options: result_options[result_string].count += 1 if result_options[result_string].statement.confidence < result_option.confidence: result_options[result_string].statement = result_option else: result_options[result_string] = ResultOption( result_option ) most_common = list(result_options.values())[0] for result_option in result_options.values(): if result_option.count > most_common.count: most_common = result_option if most_common.count > 1: result = most_common.statement response = Statement( text=result.text, in_response_to=input_statement.text, conversation=input_statement.conversation, persona='bot:' + self.name ) response.confidence = result.confidence return response
[ "Return", "a", "response", "based", "on", "a", "given", "input", "statement", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/chatterbot.py#L135-L206
[ "def", "generate_response", "(", "self", ",", "input_statement", ",", "additional_response_selection_parameters", "=", "None", ")", ":", "Statement", "=", "self", ".", "storage", ".", "get_object", "(", "'statement'", ")", "results", "=", "[", "]", "result", "="...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
ChatBot.learn_response
Learn that the statement provided is a valid response.
chatterbot/chatterbot.py
def learn_response(self, statement, previous_statement=None): """ Learn that the statement provided is a valid response. """ if not previous_statement: previous_statement = statement.in_response_to if not previous_statement: previous_statement = self.get_latest_response(statement.conversation) if previous_statement: previous_statement = previous_statement.text previous_statement_text = previous_statement if not isinstance(previous_statement, (str, type(None), )): statement.in_response_to = previous_statement.text elif isinstance(previous_statement, str): statement.in_response_to = previous_statement self.logger.info('Adding "{}" as a response to "{}"'.format( statement.text, previous_statement_text )) # Save the input statement return self.storage.create(**statement.serialize())
def learn_response(self, statement, previous_statement=None): """ Learn that the statement provided is a valid response. """ if not previous_statement: previous_statement = statement.in_response_to if not previous_statement: previous_statement = self.get_latest_response(statement.conversation) if previous_statement: previous_statement = previous_statement.text previous_statement_text = previous_statement if not isinstance(previous_statement, (str, type(None), )): statement.in_response_to = previous_statement.text elif isinstance(previous_statement, str): statement.in_response_to = previous_statement self.logger.info('Adding "{}" as a response to "{}"'.format( statement.text, previous_statement_text )) # Save the input statement return self.storage.create(**statement.serialize())
[ "Learn", "that", "the", "statement", "provided", "is", "a", "valid", "response", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/chatterbot.py#L208-L233
[ "def", "learn_response", "(", "self", ",", "statement", ",", "previous_statement", "=", "None", ")", ":", "if", "not", "previous_statement", ":", "previous_statement", "=", "statement", ".", "in_response_to", "if", "not", "previous_statement", ":", "previous_stateme...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
StatementMixin.serialize
:returns: A dictionary representation of the statement object. :rtype: dict
chatterbot/conversation.py
def serialize(self): """ :returns: A dictionary representation of the statement object. :rtype: dict """ data = {} for field_name in self.get_statement_field_names(): format_method = getattr(self, 'get_{}'.format( field_name ), None) if format_method: data[field_name] = format_method() else: data[field_name] = getattr(self, field_name) return data
def serialize(self): """ :returns: A dictionary representation of the statement object. :rtype: dict """ data = {} for field_name in self.get_statement_field_names(): format_method = getattr(self, 'get_{}'.format( field_name ), None) if format_method: data[field_name] = format_method() else: data[field_name] = getattr(self, field_name) return data
[ ":", "returns", ":", "A", "dictionary", "representation", "of", "the", "statement", "object", ".", ":", "rtype", ":", "dict" ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/conversation.py#L44-L61
[ "def", "serialize", "(", "self", ")", ":", "data", "=", "{", "}", "for", "field_name", "in", "self", ".", "get_statement_field_names", "(", ")", ":", "format_method", "=", "getattr", "(", "self", ",", "'get_{}'", ".", "format", "(", "field_name", ")", ",...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
import_module
Imports the specified module based on the dot notated import path for the module.
chatterbot/utils.py
def import_module(dotted_path): """ Imports the specified module based on the dot notated import path for the module. """ import importlib module_parts = dotted_path.split('.') module_path = '.'.join(module_parts[:-1]) module = importlib.import_module(module_path) return getattr(module, module_parts[-1])
def import_module(dotted_path): """ Imports the specified module based on the dot notated import path for the module. """ import importlib module_parts = dotted_path.split('.') module_path = '.'.join(module_parts[:-1]) module = importlib.import_module(module_path) return getattr(module, module_parts[-1])
[ "Imports", "the", "specified", "module", "based", "on", "the", "dot", "notated", "import", "path", "for", "the", "module", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/utils.py#L6-L17
[ "def", "import_module", "(", "dotted_path", ")", ":", "import", "importlib", "module_parts", "=", "dotted_path", ".", "split", "(", "'.'", ")", "module_path", "=", "'.'", ".", "join", "(", "module_parts", "[", ":", "-", "1", "]", ")", "module", "=", "imp...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
initialize_class
:param data: A string or dictionary containing a import_path attribute.
chatterbot/utils.py
def initialize_class(data, *args, **kwargs): """ :param data: A string or dictionary containing a import_path attribute. """ if isinstance(data, dict): import_path = data.get('import_path') data.update(kwargs) Class = import_module(import_path) return Class(*args, **data) else: Class = import_module(data) return Class(*args, **kwargs)
def initialize_class(data, *args, **kwargs): """ :param data: A string or dictionary containing a import_path attribute. """ if isinstance(data, dict): import_path = data.get('import_path') data.update(kwargs) Class = import_module(import_path) return Class(*args, **data) else: Class = import_module(data) return Class(*args, **kwargs)
[ ":", "param", "data", ":", "A", "string", "or", "dictionary", "containing", "a", "import_path", "attribute", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/utils.py#L20-L33
[ "def", "initialize_class", "(", "data", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "isinstance", "(", "data", ",", "dict", ")", ":", "import_path", "=", "data", ".", "get", "(", "'import_path'", ")", "data", ".", "update", "(", "kwarg...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
validate_adapter_class
Raises an exception if validate_class is not a subclass of adapter_class. :param validate_class: The class to be validated. :type validate_class: class :param adapter_class: The class type to check against. :type adapter_class: class :raises: Adapter.InvalidAdapterTypeException
chatterbot/utils.py
def validate_adapter_class(validate_class, adapter_class): """ Raises an exception if validate_class is not a subclass of adapter_class. :param validate_class: The class to be validated. :type validate_class: class :param adapter_class: The class type to check against. :type adapter_class: class :raises: Adapter.InvalidAdapterTypeException """ from chatterbot.adapters import Adapter # If a dictionary was passed in, check if it has an import_path attribute if isinstance(validate_class, dict): if 'import_path' not in validate_class: raise Adapter.InvalidAdapterTypeException( 'The dictionary {} must contain a value for "import_path"'.format( str(validate_class) ) ) # Set the class to the import path for the next check validate_class = validate_class.get('import_path') if not issubclass(import_module(validate_class), adapter_class): raise Adapter.InvalidAdapterTypeException( '{} must be a subclass of {}'.format( validate_class, adapter_class.__name__ ) )
def validate_adapter_class(validate_class, adapter_class): """ Raises an exception if validate_class is not a subclass of adapter_class. :param validate_class: The class to be validated. :type validate_class: class :param adapter_class: The class type to check against. :type adapter_class: class :raises: Adapter.InvalidAdapterTypeException """ from chatterbot.adapters import Adapter # If a dictionary was passed in, check if it has an import_path attribute if isinstance(validate_class, dict): if 'import_path' not in validate_class: raise Adapter.InvalidAdapterTypeException( 'The dictionary {} must contain a value for "import_path"'.format( str(validate_class) ) ) # Set the class to the import path for the next check validate_class = validate_class.get('import_path') if not issubclass(import_module(validate_class), adapter_class): raise Adapter.InvalidAdapterTypeException( '{} must be a subclass of {}'.format( validate_class, adapter_class.__name__ ) )
[ "Raises", "an", "exception", "if", "validate_class", "is", "not", "a", "subclass", "of", "adapter_class", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/utils.py#L36-L70
[ "def", "validate_adapter_class", "(", "validate_class", ",", "adapter_class", ")", ":", "from", "chatterbot", ".", "adapters", "import", "Adapter", "# If a dictionary was passed in, check if it has an import_path attribute", "if", "isinstance", "(", "validate_class", ",", "di...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
get_response_time
Returns the amount of time taken for a given chat bot to return a response. :param chatbot: A chat bot instance. :type chatbot: ChatBot :returns: The response time in seconds. :rtype: float
chatterbot/utils.py
def get_response_time(chatbot, statement='Hello'): """ Returns the amount of time taken for a given chat bot to return a response. :param chatbot: A chat bot instance. :type chatbot: ChatBot :returns: The response time in seconds. :rtype: float """ import time start_time = time.time() chatbot.get_response(statement) return time.time() - start_time
def get_response_time(chatbot, statement='Hello'): """ Returns the amount of time taken for a given chat bot to return a response. :param chatbot: A chat bot instance. :type chatbot: ChatBot :returns: The response time in seconds. :rtype: float """ import time start_time = time.time() chatbot.get_response(statement) return time.time() - start_time
[ "Returns", "the", "amount", "of", "time", "taken", "for", "a", "given", "chat", "bot", "to", "return", "a", "response", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/utils.py#L73-L90
[ "def", "get_response_time", "(", "chatbot", ",", "statement", "=", "'Hello'", ")", ":", "import", "time", "start_time", "=", "time", ".", "time", "(", ")", "chatbot", ".", "get_response", "(", "statement", ")", "return", "time", ".", "time", "(", ")", "-...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
print_progress_bar
Print progress bar :param description: Training description :type description: str :param iteration_counter: Incremental counter :type iteration_counter: int :param total_items: total number items :type total_items: int :param progress_bar_length: Progress bar length :type progress_bar_length: int :returns: void :rtype: void
chatterbot/utils.py
def print_progress_bar(description, iteration_counter, total_items, progress_bar_length=20): """ Print progress bar :param description: Training description :type description: str :param iteration_counter: Incremental counter :type iteration_counter: int :param total_items: total number items :type total_items: int :param progress_bar_length: Progress bar length :type progress_bar_length: int :returns: void :rtype: void """ import sys percent = float(iteration_counter) / total_items hashes = '#' * int(round(percent * progress_bar_length)) spaces = ' ' * (progress_bar_length - len(hashes)) sys.stdout.write('\r{0}: [{1}] {2}%'.format(description, hashes + spaces, int(round(percent * 100)))) sys.stdout.flush() if total_items == iteration_counter: print('\r')
def print_progress_bar(description, iteration_counter, total_items, progress_bar_length=20): """ Print progress bar :param description: Training description :type description: str :param iteration_counter: Incremental counter :type iteration_counter: int :param total_items: total number items :type total_items: int :param progress_bar_length: Progress bar length :type progress_bar_length: int :returns: void :rtype: void """ import sys percent = float(iteration_counter) / total_items hashes = '#' * int(round(percent * progress_bar_length)) spaces = ' ' * (progress_bar_length - len(hashes)) sys.stdout.write('\r{0}: [{1}] {2}%'.format(description, hashes + spaces, int(round(percent * 100)))) sys.stdout.flush() if total_items == iteration_counter: print('\r')
[ "Print", "progress", "bar", ":", "param", "description", ":", "Training", "description", ":", "type", "description", ":", "str" ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/utils.py#L93-L119
[ "def", "print_progress_bar", "(", "description", ",", "iteration_counter", ",", "total_items", ",", "progress_bar_length", "=", "20", ")", ":", "import", "sys", "percent", "=", "float", "(", "iteration_counter", ")", "/", "total_items", "hashes", "=", "'#'", "*"...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
UnitConversion.get_unit
Get the first match unit metric object supported by pint library given a variation of unit metric names (Ex:['HOUR', 'hour']). :param ureg: unit registry which units are defined and handled :type ureg: pint.registry.UnitRegistry object :param unit_variations: A list of strings with names of units :type unit_variations: str
chatterbot/logic/unit_conversion.py
def get_unit(self, ureg, unit_variations): """ Get the first match unit metric object supported by pint library given a variation of unit metric names (Ex:['HOUR', 'hour']). :param ureg: unit registry which units are defined and handled :type ureg: pint.registry.UnitRegistry object :param unit_variations: A list of strings with names of units :type unit_variations: str """ for unit in unit_variations: try: return getattr(ureg, unit) except Exception: continue return None
def get_unit(self, ureg, unit_variations): """ Get the first match unit metric object supported by pint library given a variation of unit metric names (Ex:['HOUR', 'hour']). :param ureg: unit registry which units are defined and handled :type ureg: pint.registry.UnitRegistry object :param unit_variations: A list of strings with names of units :type unit_variations: str """ for unit in unit_variations: try: return getattr(ureg, unit) except Exception: continue return None
[ "Get", "the", "first", "match", "unit", "metric", "object", "supported", "by", "pint", "library", "given", "a", "variation", "of", "unit", "metric", "names", "(", "Ex", ":", "[", "HOUR", "hour", "]", ")", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/logic/unit_conversion.py#L67-L83
[ "def", "get_unit", "(", "self", ",", "ureg", ",", "unit_variations", ")", ":", "for", "unit", "in", "unit_variations", ":", "try", ":", "return", "getattr", "(", "ureg", ",", "unit", ")", "except", "Exception", ":", "continue", "return", "None" ]
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
UnitConversion.get_valid_units
Returns the firt match `pint.unit.Unit` object for from_unit and target_unit strings from a possible variation of metric unit names supported by pint library. :param ureg: unit registry which units are defined and handled :type ureg: `pint.registry.UnitRegistry` :param from_unit: source metric unit :type from_unit: str :param from_unit: target metric unit :type from_unit: str
chatterbot/logic/unit_conversion.py
def get_valid_units(self, ureg, from_unit, target_unit): """ Returns the firt match `pint.unit.Unit` object for from_unit and target_unit strings from a possible variation of metric unit names supported by pint library. :param ureg: unit registry which units are defined and handled :type ureg: `pint.registry.UnitRegistry` :param from_unit: source metric unit :type from_unit: str :param from_unit: target metric unit :type from_unit: str """ from_unit_variations = [from_unit.lower(), from_unit.upper()] target_unit_variations = [target_unit.lower(), target_unit.upper()] from_unit = self.get_unit(ureg, from_unit_variations) target_unit = self.get_unit(ureg, target_unit_variations) return from_unit, target_unit
def get_valid_units(self, ureg, from_unit, target_unit): """ Returns the firt match `pint.unit.Unit` object for from_unit and target_unit strings from a possible variation of metric unit names supported by pint library. :param ureg: unit registry which units are defined and handled :type ureg: `pint.registry.UnitRegistry` :param from_unit: source metric unit :type from_unit: str :param from_unit: target metric unit :type from_unit: str """ from_unit_variations = [from_unit.lower(), from_unit.upper()] target_unit_variations = [target_unit.lower(), target_unit.upper()] from_unit = self.get_unit(ureg, from_unit_variations) target_unit = self.get_unit(ureg, target_unit_variations) return from_unit, target_unit
[ "Returns", "the", "firt", "match", "pint", ".", "unit", ".", "Unit", "object", "for", "from_unit", "and", "target_unit", "strings", "from", "a", "possible", "variation", "of", "metric", "unit", "names", "supported", "by", "pint", "library", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/logic/unit_conversion.py#L85-L104
[ "def", "get_valid_units", "(", "self", ",", "ureg", ",", "from_unit", ",", "target_unit", ")", ":", "from_unit_variations", "=", "[", "from_unit", ".", "lower", "(", ")", ",", "from_unit", ".", "upper", "(", ")", "]", "target_unit_variations", "=", "[", "t...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
UnitConversion.handle_matches
Returns a response statement from a matched input statement. :param match: It is a valid matched pattern from the input statement :type: `_sre.SRE_Match`
chatterbot/logic/unit_conversion.py
def handle_matches(self, match): """ Returns a response statement from a matched input statement. :param match: It is a valid matched pattern from the input statement :type: `_sre.SRE_Match` """ response = Statement(text='') from_parsed = match.group("from") target_parsed = match.group("target") n_statement = match.group("number") if n_statement == 'a' or n_statement == 'an': n_statement = '1.0' n = mathparse.parse(n_statement, self.language.ISO_639.upper()) ureg = UnitRegistry() from_parsed, target_parsed = self.get_valid_units(ureg, from_parsed, target_parsed) if from_parsed is None or target_parsed is None: response.confidence = 0.0 else: from_value = ureg.Quantity(float(n), from_parsed) target_value = from_value.to(target_parsed) response.confidence = 1.0 response.text = str(target_value.magnitude) return response
def handle_matches(self, match): """ Returns a response statement from a matched input statement. :param match: It is a valid matched pattern from the input statement :type: `_sre.SRE_Match` """ response = Statement(text='') from_parsed = match.group("from") target_parsed = match.group("target") n_statement = match.group("number") if n_statement == 'a' or n_statement == 'an': n_statement = '1.0' n = mathparse.parse(n_statement, self.language.ISO_639.upper()) ureg = UnitRegistry() from_parsed, target_parsed = self.get_valid_units(ureg, from_parsed, target_parsed) if from_parsed is None or target_parsed is None: response.confidence = 0.0 else: from_value = ureg.Quantity(float(n), from_parsed) target_value = from_value.to(target_parsed) response.confidence = 1.0 response.text = str(target_value.magnitude) return response
[ "Returns", "a", "response", "statement", "from", "a", "matched", "input", "statement", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/logic/unit_conversion.py#L106-L135
[ "def", "handle_matches", "(", "self", ",", "match", ")", ":", "response", "=", "Statement", "(", "text", "=", "''", ")", "from_parsed", "=", "match", ".", "group", "(", "\"from\"", ")", "target_parsed", "=", "match", ".", "group", "(", "\"target\"", ")",...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
LogicAdapter.get_default_response
This method is called when a logic adapter is unable to generate any other meaningful response.
chatterbot/logic/logic_adapter.py
def get_default_response(self, input_statement): """ This method is called when a logic adapter is unable to generate any other meaningful response. """ from random import choice if self.default_responses: response = choice(self.default_responses) else: try: response = self.chatbot.storage.get_random() except StorageAdapter.EmptyDatabaseException: response = input_statement self.chatbot.logger.info( 'No known response to the input was found. Selecting a random response.' ) # Set confidence to zero because a random response is selected response.confidence = 0 return response
def get_default_response(self, input_statement): """ This method is called when a logic adapter is unable to generate any other meaningful response. """ from random import choice if self.default_responses: response = choice(self.default_responses) else: try: response = self.chatbot.storage.get_random() except StorageAdapter.EmptyDatabaseException: response = input_statement self.chatbot.logger.info( 'No known response to the input was found. Selecting a random response.' ) # Set confidence to zero because a random response is selected response.confidence = 0 return response
[ "This", "method", "is", "called", "when", "a", "logic", "adapter", "is", "unable", "to", "generate", "any", "other", "meaningful", "response", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/logic/logic_adapter.py#L103-L125
[ "def", "get_default_response", "(", "self", ",", "input_statement", ")", ":", "from", "random", "import", "choice", "if", "self", ".", "default_responses", ":", "response", "=", "choice", "(", "self", ".", "default_responses", ")", "else", ":", "try", ":", "...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
TimeLogicAdapter.time_question_features
Provide an analysis of significant features in the string.
chatterbot/logic/time_adapter.py
def time_question_features(self, text): """ Provide an analysis of significant features in the string. """ features = {} # A list of all words from the known sentences all_words = " ".join(self.positive + self.negative).split() # A list of the first word in each of the known sentence all_first_words = [] for sentence in self.positive + self.negative: all_first_words.append( sentence.split(' ', 1)[0] ) for word in text.split(): features['first_word({})'.format(word)] = (word in all_first_words) for word in text.split(): features['contains({})'.format(word)] = (word in all_words) for letter in 'abcdefghijklmnopqrstuvwxyz': features['count({})'.format(letter)] = text.lower().count(letter) features['has({})'.format(letter)] = (letter in text.lower()) return features
def time_question_features(self, text): """ Provide an analysis of significant features in the string. """ features = {} # A list of all words from the known sentences all_words = " ".join(self.positive + self.negative).split() # A list of the first word in each of the known sentence all_first_words = [] for sentence in self.positive + self.negative: all_first_words.append( sentence.split(' ', 1)[0] ) for word in text.split(): features['first_word({})'.format(word)] = (word in all_first_words) for word in text.split(): features['contains({})'.format(word)] = (word in all_words) for letter in 'abcdefghijklmnopqrstuvwxyz': features['count({})'.format(letter)] = text.lower().count(letter) features['has({})'.format(letter)] = (letter in text.lower()) return features
[ "Provide", "an", "analysis", "of", "significant", "features", "in", "the", "string", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/logic/time_adapter.py#L56-L82
[ "def", "time_question_features", "(", "self", ",", "text", ")", ":", "features", "=", "{", "}", "# A list of all words from the known sentences", "all_words", "=", "\" \"", ".", "join", "(", "self", ".", "positive", "+", "self", ".", "negative", ")", ".", "spl...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
MathematicalEvaluation.can_process
Determines whether it is appropriate for this adapter to respond to the user input.
chatterbot/logic/mathematical_evaluation.py
def can_process(self, statement): """ Determines whether it is appropriate for this adapter to respond to the user input. """ response = self.process(statement) self.cache[statement.text] = response return response.confidence == 1
def can_process(self, statement): """ Determines whether it is appropriate for this adapter to respond to the user input. """ response = self.process(statement) self.cache[statement.text] = response return response.confidence == 1
[ "Determines", "whether", "it", "is", "appropriate", "for", "this", "adapter", "to", "respond", "to", "the", "user", "input", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/logic/mathematical_evaluation.py#L28-L35
[ "def", "can_process", "(", "self", ",", "statement", ")", ":", "response", "=", "self", ".", "process", "(", "statement", ")", "self", ".", "cache", "[", "statement", ".", "text", "]", "=", "response", "return", "response", ".", "confidence", "==", "1" ]
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
MathematicalEvaluation.process
Takes a statement string. Returns the equation from the statement with the mathematical terms solved.
chatterbot/logic/mathematical_evaluation.py
def process(self, statement, additional_response_selection_parameters=None): """ Takes a statement string. Returns the equation from the statement with the mathematical terms solved. """ from mathparse import mathparse input_text = statement.text # Use the result cached by the process method if it exists if input_text in self.cache: cached_result = self.cache[input_text] self.cache = {} return cached_result # Getting the mathematical terms within the input statement expression = mathparse.extract_expression(input_text, language=self.language.ISO_639.upper()) response = Statement(text=expression) try: response.text += ' = ' + str( mathparse.parse(expression, language=self.language.ISO_639.upper()) ) # The confidence is 1 if the expression could be evaluated response.confidence = 1 except mathparse.PostfixTokenEvaluationException: response.confidence = 0 return response
def process(self, statement, additional_response_selection_parameters=None): """ Takes a statement string. Returns the equation from the statement with the mathematical terms solved. """ from mathparse import mathparse input_text = statement.text # Use the result cached by the process method if it exists if input_text in self.cache: cached_result = self.cache[input_text] self.cache = {} return cached_result # Getting the mathematical terms within the input statement expression = mathparse.extract_expression(input_text, language=self.language.ISO_639.upper()) response = Statement(text=expression) try: response.text += ' = ' + str( mathparse.parse(expression, language=self.language.ISO_639.upper()) ) # The confidence is 1 if the expression could be evaluated response.confidence = 1 except mathparse.PostfixTokenEvaluationException: response.confidence = 0 return response
[ "Takes", "a", "statement", "string", ".", "Returns", "the", "equation", "from", "the", "statement", "with", "the", "mathematical", "terms", "solved", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/logic/mathematical_evaluation.py#L37-L67
[ "def", "process", "(", "self", ",", "statement", ",", "additional_response_selection_parameters", "=", "None", ")", ":", "from", "mathparse", "import", "mathparse", "input_text", "=", "statement", ".", "text", "# Use the result cached by the process method if it exists", ...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
get_recent_repeated_responses
A filter that eliminates possibly repetitive responses to prevent a chat bot from repeating statements that it has recently said.
chatterbot/filters.py
def get_recent_repeated_responses(chatbot, conversation, sample=10, threshold=3, quantity=3): """ A filter that eliminates possibly repetitive responses to prevent a chat bot from repeating statements that it has recently said. """ from collections import Counter # Get the most recent statements from the conversation conversation_statements = list(chatbot.storage.filter( conversation=conversation, order_by=['id'] ))[sample * -1:] text_of_recent_responses = [ statement.text for statement in conversation_statements ] counter = Counter(text_of_recent_responses) # Find the n most common responses from the conversation most_common = counter.most_common(quantity) return [ counted[0] for counted in most_common if counted[1] >= threshold ]
def get_recent_repeated_responses(chatbot, conversation, sample=10, threshold=3, quantity=3): """ A filter that eliminates possibly repetitive responses to prevent a chat bot from repeating statements that it has recently said. """ from collections import Counter # Get the most recent statements from the conversation conversation_statements = list(chatbot.storage.filter( conversation=conversation, order_by=['id'] ))[sample * -1:] text_of_recent_responses = [ statement.text for statement in conversation_statements ] counter = Counter(text_of_recent_responses) # Find the n most common responses from the conversation most_common = counter.most_common(quantity) return [ counted[0] for counted in most_common if counted[1] >= threshold ]
[ "A", "filter", "that", "eliminates", "possibly", "repetitive", "responses", "to", "prevent", "a", "chat", "bot", "from", "repeating", "statements", "that", "it", "has", "recently", "said", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/filters.py#L1-L26
[ "def", "get_recent_repeated_responses", "(", "chatbot", ",", "conversation", ",", "sample", "=", "10", ",", "threshold", "=", "3", ",", "quantity", "=", "3", ")", ":", "from", "collections", "import", "Counter", "# Get the most recent statements from the conversation"...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
get_most_frequent_response
:param input_statement: A statement, that closely matches an input to the chat bot. :type input_statement: Statement :param response_list: A list of statement options to choose a response from. :type response_list: list :param storage: An instance of a storage adapter to allow the response selection method to access other statements if needed. :type storage: StorageAdapter :return: The response statement with the greatest number of occurrences. :rtype: Statement
chatterbot/response_selection.py
def get_most_frequent_response(input_statement, response_list, storage=None): """ :param input_statement: A statement, that closely matches an input to the chat bot. :type input_statement: Statement :param response_list: A list of statement options to choose a response from. :type response_list: list :param storage: An instance of a storage adapter to allow the response selection method to access other statements if needed. :type storage: StorageAdapter :return: The response statement with the greatest number of occurrences. :rtype: Statement """ matching_response = None occurrence_count = -1 logger = logging.getLogger(__name__) logger.info('Selecting response with greatest number of occurrences.') for statement in response_list: count = len(list(storage.filter( text=statement.text, in_response_to=input_statement.text) )) # Keep the more common statement if count >= occurrence_count: matching_response = statement occurrence_count = count # Choose the most commonly occuring matching response return matching_response
def get_most_frequent_response(input_statement, response_list, storage=None): """ :param input_statement: A statement, that closely matches an input to the chat bot. :type input_statement: Statement :param response_list: A list of statement options to choose a response from. :type response_list: list :param storage: An instance of a storage adapter to allow the response selection method to access other statements if needed. :type storage: StorageAdapter :return: The response statement with the greatest number of occurrences. :rtype: Statement """ matching_response = None occurrence_count = -1 logger = logging.getLogger(__name__) logger.info('Selecting response with greatest number of occurrences.') for statement in response_list: count = len(list(storage.filter( text=statement.text, in_response_to=input_statement.text) )) # Keep the more common statement if count >= occurrence_count: matching_response = statement occurrence_count = count # Choose the most commonly occuring matching response return matching_response
[ ":", "param", "input_statement", ":", "A", "statement", "that", "closely", "matches", "an", "input", "to", "the", "chat", "bot", ".", ":", "type", "input_statement", ":", "Statement" ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/response_selection.py#L8-L41
[ "def", "get_most_frequent_response", "(", "input_statement", ",", "response_list", ",", "storage", "=", "None", ")", ":", "matching_response", "=", "None", "occurrence_count", "=", "-", "1", "logger", "=", "logging", ".", "getLogger", "(", "__name__", ")", "logg...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
get_first_response
:param input_statement: A statement, that closely matches an input to the chat bot. :type input_statement: Statement :param response_list: A list of statement options to choose a response from. :type response_list: list :param storage: An instance of a storage adapter to allow the response selection method to access other statements if needed. :type storage: StorageAdapter :return: Return the first statement in the response list. :rtype: Statement
chatterbot/response_selection.py
def get_first_response(input_statement, response_list, storage=None): """ :param input_statement: A statement, that closely matches an input to the chat bot. :type input_statement: Statement :param response_list: A list of statement options to choose a response from. :type response_list: list :param storage: An instance of a storage adapter to allow the response selection method to access other statements if needed. :type storage: StorageAdapter :return: Return the first statement in the response list. :rtype: Statement """ logger = logging.getLogger(__name__) logger.info('Selecting first response from list of {} options.'.format( len(response_list) )) return response_list[0]
def get_first_response(input_statement, response_list, storage=None): """ :param input_statement: A statement, that closely matches an input to the chat bot. :type input_statement: Statement :param response_list: A list of statement options to choose a response from. :type response_list: list :param storage: An instance of a storage adapter to allow the response selection method to access other statements if needed. :type storage: StorageAdapter :return: Return the first statement in the response list. :rtype: Statement """ logger = logging.getLogger(__name__) logger.info('Selecting first response from list of {} options.'.format( len(response_list) )) return response_list[0]
[ ":", "param", "input_statement", ":", "A", "statement", "that", "closely", "matches", "an", "input", "to", "the", "chat", "bot", ".", ":", "type", "input_statement", ":", "Statement" ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/response_selection.py#L44-L63
[ "def", "get_first_response", "(", "input_statement", ",", "response_list", ",", "storage", "=", "None", ")", ":", "logger", "=", "logging", ".", "getLogger", "(", "__name__", ")", "logger", ".", "info", "(", "'Selecting first response from list of {} options.'", "."...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
get_random_response
:param input_statement: A statement, that closely matches an input to the chat bot. :type input_statement: Statement :param response_list: A list of statement options to choose a response from. :type response_list: list :param storage: An instance of a storage adapter to allow the response selection method to access other statements if needed. :type storage: StorageAdapter :return: Choose a random response from the selection. :rtype: Statement
chatterbot/response_selection.py
def get_random_response(input_statement, response_list, storage=None): """ :param input_statement: A statement, that closely matches an input to the chat bot. :type input_statement: Statement :param response_list: A list of statement options to choose a response from. :type response_list: list :param storage: An instance of a storage adapter to allow the response selection method to access other statements if needed. :type storage: StorageAdapter :return: Choose a random response from the selection. :rtype: Statement """ from random import choice logger = logging.getLogger(__name__) logger.info('Selecting a response from list of {} options.'.format( len(response_list) )) return choice(response_list)
def get_random_response(input_statement, response_list, storage=None): """ :param input_statement: A statement, that closely matches an input to the chat bot. :type input_statement: Statement :param response_list: A list of statement options to choose a response from. :type response_list: list :param storage: An instance of a storage adapter to allow the response selection method to access other statements if needed. :type storage: StorageAdapter :return: Choose a random response from the selection. :rtype: Statement """ from random import choice logger = logging.getLogger(__name__) logger.info('Selecting a response from list of {} options.'.format( len(response_list) )) return choice(response_list)
[ ":", "param", "input_statement", ":", "A", "statement", "that", "closely", "matches", "an", "input", "to", "the", "chat", "bot", ".", ":", "type", "input_statement", ":", "Statement" ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/response_selection.py#L66-L86
[ "def", "get_random_response", "(", "input_statement", ",", "response_list", ",", "storage", "=", "None", ")", ":", "from", "random", "import", "choice", "logger", "=", "logging", ".", "getLogger", "(", "__name__", ")", "logger", ".", "info", "(", "'Selecting a...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
LevenshteinDistance.compare
Compare the two input statements. :return: The percent of similarity between the text of the statements. :rtype: float
chatterbot/comparisons.py
def compare(self, statement_a, statement_b): """ Compare the two input statements. :return: The percent of similarity between the text of the statements. :rtype: float """ # Return 0 if either statement has a falsy text value if not statement_a.text or not statement_b.text: return 0 # Get the lowercase version of both strings statement_a_text = str(statement_a.text.lower()) statement_b_text = str(statement_b.text.lower()) similarity = SequenceMatcher( None, statement_a_text, statement_b_text ) # Calculate a decimal percent of the similarity percent = round(similarity.ratio(), 2) return percent
def compare(self, statement_a, statement_b): """ Compare the two input statements. :return: The percent of similarity between the text of the statements. :rtype: float """ # Return 0 if either statement has a falsy text value if not statement_a.text or not statement_b.text: return 0 # Get the lowercase version of both strings statement_a_text = str(statement_a.text.lower()) statement_b_text = str(statement_b.text.lower()) similarity = SequenceMatcher( None, statement_a_text, statement_b_text ) # Calculate a decimal percent of the similarity percent = round(similarity.ratio(), 2) return percent
[ "Compare", "the", "two", "input", "statements", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/comparisons.py#L32-L57
[ "def", "compare", "(", "self", ",", "statement_a", ",", "statement_b", ")", ":", "# Return 0 if either statement has a falsy text value", "if", "not", "statement_a", ".", "text", "or", "not", "statement_b", ".", "text", ":", "return", "0", "# Get the lowercase version...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
SpacySimilarity.compare
Compare the two input statements. :return: The percent of similarity between the closest synset distance. :rtype: float
chatterbot/comparisons.py
def compare(self, statement_a, statement_b): """ Compare the two input statements. :return: The percent of similarity between the closest synset distance. :rtype: float """ document_a = self.nlp(statement_a.text) document_b = self.nlp(statement_b.text) return document_a.similarity(document_b)
def compare(self, statement_a, statement_b): """ Compare the two input statements. :return: The percent of similarity between the closest synset distance. :rtype: float """ document_a = self.nlp(statement_a.text) document_b = self.nlp(statement_b.text) return document_a.similarity(document_b)
[ "Compare", "the", "two", "input", "statements", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/comparisons.py#L70-L80
[ "def", "compare", "(", "self", ",", "statement_a", ",", "statement_b", ")", ":", "document_a", "=", "self", ".", "nlp", "(", "statement_a", ".", "text", ")", "document_b", "=", "self", ".", "nlp", "(", "statement_b", ".", "text", ")", "return", "document...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
JaccardSimilarity.compare
Return the calculated similarity of two statements based on the Jaccard index.
chatterbot/comparisons.py
def compare(self, statement_a, statement_b): """ Return the calculated similarity of two statements based on the Jaccard index. """ # Make both strings lowercase document_a = self.nlp(statement_a.text.lower()) document_b = self.nlp(statement_b.text.lower()) statement_a_lemmas = set([ token.lemma_ for token in document_a if not token.is_stop ]) statement_b_lemmas = set([ token.lemma_ for token in document_b if not token.is_stop ]) # Calculate Jaccard similarity numerator = len(statement_a_lemmas.intersection(statement_b_lemmas)) denominator = float(len(statement_a_lemmas.union(statement_b_lemmas))) ratio = numerator / denominator return ratio
def compare(self, statement_a, statement_b): """ Return the calculated similarity of two statements based on the Jaccard index. """ # Make both strings lowercase document_a = self.nlp(statement_a.text.lower()) document_b = self.nlp(statement_b.text.lower()) statement_a_lemmas = set([ token.lemma_ for token in document_a if not token.is_stop ]) statement_b_lemmas = set([ token.lemma_ for token in document_b if not token.is_stop ]) # Calculate Jaccard similarity numerator = len(statement_a_lemmas.intersection(statement_b_lemmas)) denominator = float(len(statement_a_lemmas.union(statement_b_lemmas))) ratio = numerator / denominator return ratio
[ "Return", "the", "calculated", "similarity", "of", "two", "statements", "based", "on", "the", "Jaccard", "index", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/comparisons.py#L114-L135
[ "def", "compare", "(", "self", ",", "statement_a", ",", "statement_b", ")", ":", "# Make both strings lowercase", "document_a", "=", "self", ".", "nlp", "(", "statement_a", ".", "text", ".", "lower", "(", ")", ")", "document_b", "=", "self", ".", "nlp", "(...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
MongoDatabaseAdapter.get_statement_model
Return the class for the statement model.
chatterbot/storage/mongodb.py
def get_statement_model(self): """ Return the class for the statement model. """ from chatterbot.conversation import Statement # Create a storage-aware statement statement = Statement statement.storage = self return statement
def get_statement_model(self): """ Return the class for the statement model. """ from chatterbot.conversation import Statement # Create a storage-aware statement statement = Statement statement.storage = self return statement
[ "Return", "the", "class", "for", "the", "statement", "model", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/mongodb.py#L44-L54
[ "def", "get_statement_model", "(", "self", ")", ":", "from", "chatterbot", ".", "conversation", "import", "Statement", "# Create a storage-aware statement", "statement", "=", "Statement", "statement", ".", "storage", "=", "self", "return", "statement" ]
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
MongoDatabaseAdapter.mongo_to_object
Return Statement object when given data returned from Mongo DB.
chatterbot/storage/mongodb.py
def mongo_to_object(self, statement_data): """ Return Statement object when given data returned from Mongo DB. """ Statement = self.get_model('statement') statement_data['id'] = statement_data['_id'] return Statement(**statement_data)
def mongo_to_object(self, statement_data): """ Return Statement object when given data returned from Mongo DB. """ Statement = self.get_model('statement') statement_data['id'] = statement_data['_id'] return Statement(**statement_data)
[ "Return", "Statement", "object", "when", "given", "data", "returned", "from", "Mongo", "DB", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/mongodb.py#L59-L68
[ "def", "mongo_to_object", "(", "self", ",", "statement_data", ")", ":", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "statement_data", "[", "'id'", "]", "=", "statement_data", "[", "'_id'", "]", "return", "Statement", "(", "*", "*", ...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
MongoDatabaseAdapter.filter
Returns a list of statements in the database that match the parameters specified.
chatterbot/storage/mongodb.py
def filter(self, **kwargs): """ Returns a list of statements in the database that match the parameters specified. """ import pymongo page_size = kwargs.pop('page_size', 1000) order_by = kwargs.pop('order_by', None) tags = kwargs.pop('tags', []) exclude_text = kwargs.pop('exclude_text', None) exclude_text_words = kwargs.pop('exclude_text_words', []) persona_not_startswith = kwargs.pop('persona_not_startswith', None) search_text_contains = kwargs.pop('search_text_contains', None) if tags: kwargs['tags'] = { '$in': tags } if exclude_text: if 'text' not in kwargs: kwargs['text'] = {} elif 'text' in kwargs and isinstance(kwargs['text'], str): text = kwargs.pop('text') kwargs['text'] = { '$eq': text } kwargs['text']['$nin'] = exclude_text if exclude_text_words: if 'text' not in kwargs: kwargs['text'] = {} elif 'text' in kwargs and isinstance(kwargs['text'], str): text = kwargs.pop('text') kwargs['text'] = { '$eq': text } exclude_word_regex = '|'.join([ '.*{}.*'.format(word) for word in exclude_text_words ]) kwargs['text']['$not'] = re.compile(exclude_word_regex) if persona_not_startswith: if 'persona' not in kwargs: kwargs['persona'] = {} elif 'persona' in kwargs and isinstance(kwargs['persona'], str): persona = kwargs.pop('persona') kwargs['persona'] = { '$eq': persona } kwargs['persona']['$not'] = re.compile('^bot:*') if search_text_contains: or_regex = '|'.join([ '{}'.format(word) for word in search_text_contains.split(' ') ]) kwargs['search_text'] = re.compile(or_regex) mongo_ordering = [] if order_by: # Sort so that newer datetimes appear first if 'created_at' in order_by: order_by.remove('created_at') mongo_ordering.append(('created_at', pymongo.DESCENDING, )) for order in order_by: mongo_ordering.append((order, pymongo.ASCENDING)) total_statements = self.statements.find(kwargs).count() for start_index in range(0, total_statements, page_size): if mongo_ordering: for match in self.statements.find(kwargs).sort(mongo_ordering).skip(start_index).limit(page_size): yield self.mongo_to_object(match) else: for match in self.statements.find(kwargs).skip(start_index).limit(page_size): yield self.mongo_to_object(match)
def filter(self, **kwargs): """ Returns a list of statements in the database that match the parameters specified. """ import pymongo page_size = kwargs.pop('page_size', 1000) order_by = kwargs.pop('order_by', None) tags = kwargs.pop('tags', []) exclude_text = kwargs.pop('exclude_text', None) exclude_text_words = kwargs.pop('exclude_text_words', []) persona_not_startswith = kwargs.pop('persona_not_startswith', None) search_text_contains = kwargs.pop('search_text_contains', None) if tags: kwargs['tags'] = { '$in': tags } if exclude_text: if 'text' not in kwargs: kwargs['text'] = {} elif 'text' in kwargs and isinstance(kwargs['text'], str): text = kwargs.pop('text') kwargs['text'] = { '$eq': text } kwargs['text']['$nin'] = exclude_text if exclude_text_words: if 'text' not in kwargs: kwargs['text'] = {} elif 'text' in kwargs and isinstance(kwargs['text'], str): text = kwargs.pop('text') kwargs['text'] = { '$eq': text } exclude_word_regex = '|'.join([ '.*{}.*'.format(word) for word in exclude_text_words ]) kwargs['text']['$not'] = re.compile(exclude_word_regex) if persona_not_startswith: if 'persona' not in kwargs: kwargs['persona'] = {} elif 'persona' in kwargs and isinstance(kwargs['persona'], str): persona = kwargs.pop('persona') kwargs['persona'] = { '$eq': persona } kwargs['persona']['$not'] = re.compile('^bot:*') if search_text_contains: or_regex = '|'.join([ '{}'.format(word) for word in search_text_contains.split(' ') ]) kwargs['search_text'] = re.compile(or_regex) mongo_ordering = [] if order_by: # Sort so that newer datetimes appear first if 'created_at' in order_by: order_by.remove('created_at') mongo_ordering.append(('created_at', pymongo.DESCENDING, )) for order in order_by: mongo_ordering.append((order, pymongo.ASCENDING)) total_statements = self.statements.find(kwargs).count() for start_index in range(0, total_statements, page_size): if mongo_ordering: for match in self.statements.find(kwargs).sort(mongo_ordering).skip(start_index).limit(page_size): yield self.mongo_to_object(match) else: for match in self.statements.find(kwargs).skip(start_index).limit(page_size): yield self.mongo_to_object(match)
[ "Returns", "a", "list", "of", "statements", "in", "the", "database", "that", "match", "the", "parameters", "specified", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/mongodb.py#L70-L149
[ "def", "filter", "(", "self", ",", "*", "*", "kwargs", ")", ":", "import", "pymongo", "page_size", "=", "kwargs", ".", "pop", "(", "'page_size'", ",", "1000", ")", "order_by", "=", "kwargs", ".", "pop", "(", "'order_by'", ",", "None", ")", "tags", "=...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
MongoDatabaseAdapter.create
Creates a new statement matching the keyword arguments specified. Returns the created statement.
chatterbot/storage/mongodb.py
def create(self, **kwargs): """ Creates a new statement matching the keyword arguments specified. Returns the created statement. """ Statement = self.get_model('statement') if 'tags' in kwargs: kwargs['tags'] = list(set(kwargs['tags'])) if 'search_text' not in kwargs: kwargs['search_text'] = self.tagger.get_bigram_pair_string(kwargs['text']) if 'search_in_response_to' not in kwargs: if kwargs.get('in_response_to'): kwargs['search_in_response_to'] = self.tagger.get_bigram_pair_string(kwargs['in_response_to']) inserted = self.statements.insert_one(kwargs) kwargs['id'] = inserted.inserted_id return Statement(**kwargs)
def create(self, **kwargs): """ Creates a new statement matching the keyword arguments specified. Returns the created statement. """ Statement = self.get_model('statement') if 'tags' in kwargs: kwargs['tags'] = list(set(kwargs['tags'])) if 'search_text' not in kwargs: kwargs['search_text'] = self.tagger.get_bigram_pair_string(kwargs['text']) if 'search_in_response_to' not in kwargs: if kwargs.get('in_response_to'): kwargs['search_in_response_to'] = self.tagger.get_bigram_pair_string(kwargs['in_response_to']) inserted = self.statements.insert_one(kwargs) kwargs['id'] = inserted.inserted_id return Statement(**kwargs)
[ "Creates", "a", "new", "statement", "matching", "the", "keyword", "arguments", "specified", ".", "Returns", "the", "created", "statement", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/mongodb.py#L151-L172
[ "def", "create", "(", "self", ",", "*", "*", "kwargs", ")", ":", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "if", "'tags'", "in", "kwargs", ":", "kwargs", "[", "'tags'", "]", "=", "list", "(", "set", "(", "kwargs", "[", "'...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
MongoDatabaseAdapter.create_many
Creates multiple statement entries.
chatterbot/storage/mongodb.py
def create_many(self, statements): """ Creates multiple statement entries. """ create_statements = [] for statement in statements: statement_data = statement.serialize() tag_data = list(set(statement_data.pop('tags', []))) statement_data['tags'] = tag_data if not statement.search_text: statement_data['search_text'] = self.tagger.get_bigram_pair_string(statement.text) if not statement.search_in_response_to and statement.in_response_to: statement_data['search_in_response_to'] = self.tagger.get_bigram_pair_string(statement.in_response_to) create_statements.append(statement_data) self.statements.insert_many(create_statements)
def create_many(self, statements): """ Creates multiple statement entries. """ create_statements = [] for statement in statements: statement_data = statement.serialize() tag_data = list(set(statement_data.pop('tags', []))) statement_data['tags'] = tag_data if not statement.search_text: statement_data['search_text'] = self.tagger.get_bigram_pair_string(statement.text) if not statement.search_in_response_to and statement.in_response_to: statement_data['search_in_response_to'] = self.tagger.get_bigram_pair_string(statement.in_response_to) create_statements.append(statement_data) self.statements.insert_many(create_statements)
[ "Creates", "multiple", "statement", "entries", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/mongodb.py#L174-L193
[ "def", "create_many", "(", "self", ",", "statements", ")", ":", "create_statements", "=", "[", "]", "for", "statement", "in", "statements", ":", "statement_data", "=", "statement", ".", "serialize", "(", ")", "tag_data", "=", "list", "(", "set", "(", "stat...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
MongoDatabaseAdapter.get_random
Returns a random statement from the database
chatterbot/storage/mongodb.py
def get_random(self): """ Returns a random statement from the database """ from random import randint count = self.count() if count < 1: raise self.EmptyDatabaseException() random_integer = randint(0, count - 1) statements = self.statements.find().limit(1).skip(random_integer) return self.mongo_to_object(list(statements)[0])
def get_random(self): """ Returns a random statement from the database """ from random import randint count = self.count() if count < 1: raise self.EmptyDatabaseException() random_integer = randint(0, count - 1) statements = self.statements.find().limit(1).skip(random_integer) return self.mongo_to_object(list(statements)[0])
[ "Returns", "a", "random", "statement", "from", "the", "database" ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/mongodb.py#L235-L250
[ "def", "get_random", "(", "self", ")", ":", "from", "random", "import", "randint", "count", "=", "self", ".", "count", "(", ")", "if", "count", "<", "1", ":", "raise", "self", ".", "EmptyDatabaseException", "(", ")", "random_integer", "=", "randint", "("...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
Statement.add_tags
Add a list of strings to the statement as tags.
chatterbot/ext/sqlalchemy_app/models.py
def add_tags(self, *tags): """ Add a list of strings to the statement as tags. """ self.tags.extend([ Tag(name=tag) for tag in tags ])
def add_tags(self, *tags): """ Add a list of strings to the statement as tags. """ self.tags.extend([ Tag(name=tag) for tag in tags ])
[ "Add", "a", "list", "of", "strings", "to", "the", "statement", "as", "tags", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/ext/sqlalchemy_app/models.py#L108-L114
[ "def", "add_tags", "(", "self", ",", "*", "tags", ")", ":", "self", ".", "tags", ".", "extend", "(", "[", "Tag", "(", "name", "=", "tag", ")", "for", "tag", "in", "tags", "]", ")" ]
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
Trainer.get_preprocessed_statement
Preprocess the input statement.
chatterbot/trainers.py
def get_preprocessed_statement(self, input_statement): """ Preprocess the input statement. """ for preprocessor in self.chatbot.preprocessors: input_statement = preprocessor(input_statement) return input_statement
def get_preprocessed_statement(self, input_statement): """ Preprocess the input statement. """ for preprocessor in self.chatbot.preprocessors: input_statement = preprocessor(input_statement) return input_statement
[ "Preprocess", "the", "input", "statement", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L30-L37
[ "def", "get_preprocessed_statement", "(", "self", ",", "input_statement", ")", ":", "for", "preprocessor", "in", "self", ".", "chatbot", ".", "preprocessors", ":", "input_statement", "=", "preprocessor", "(", "input_statement", ")", "return", "input_statement" ]
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
Trainer.export_for_training
Create a file from the database that can be used to train other chat bots.
chatterbot/trainers.py
def export_for_training(self, file_path='./export.json'): """ Create a file from the database that can be used to train other chat bots. """ import json export = {'conversations': self._generate_export_data()} with open(file_path, 'w+') as jsonfile: json.dump(export, jsonfile, ensure_ascii=False)
def export_for_training(self, file_path='./export.json'): """ Create a file from the database that can be used to train other chat bots. """ import json export = {'conversations': self._generate_export_data()} with open(file_path, 'w+') as jsonfile: json.dump(export, jsonfile, ensure_ascii=False)
[ "Create", "a", "file", "from", "the", "database", "that", "can", "be", "used", "to", "train", "other", "chat", "bots", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L66-L74
[ "def", "export_for_training", "(", "self", ",", "file_path", "=", "'./export.json'", ")", ":", "import", "json", "export", "=", "{", "'conversations'", ":", "self", ".", "_generate_export_data", "(", ")", "}", "with", "open", "(", "file_path", ",", "'w+'", "...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
ListTrainer.train
Train the chat bot based on the provided list of statements that represents a single conversation.
chatterbot/trainers.py
def train(self, conversation): """ Train the chat bot based on the provided list of statements that represents a single conversation. """ previous_statement_text = None previous_statement_search_text = '' statements_to_create = [] for conversation_count, text in enumerate(conversation): if self.show_training_progress: utils.print_progress_bar( 'List Trainer', conversation_count + 1, len(conversation) ) statement_search_text = self.chatbot.storage.tagger.get_bigram_pair_string(text) statement = self.get_preprocessed_statement( Statement( text=text, search_text=statement_search_text, in_response_to=previous_statement_text, search_in_response_to=previous_statement_search_text, conversation='training' ) ) previous_statement_text = statement.text previous_statement_search_text = statement_search_text statements_to_create.append(statement) self.chatbot.storage.create_many(statements_to_create)
def train(self, conversation): """ Train the chat bot based on the provided list of statements that represents a single conversation. """ previous_statement_text = None previous_statement_search_text = '' statements_to_create = [] for conversation_count, text in enumerate(conversation): if self.show_training_progress: utils.print_progress_bar( 'List Trainer', conversation_count + 1, len(conversation) ) statement_search_text = self.chatbot.storage.tagger.get_bigram_pair_string(text) statement = self.get_preprocessed_statement( Statement( text=text, search_text=statement_search_text, in_response_to=previous_statement_text, search_in_response_to=previous_statement_search_text, conversation='training' ) ) previous_statement_text = statement.text previous_statement_search_text = statement_search_text statements_to_create.append(statement) self.chatbot.storage.create_many(statements_to_create)
[ "Train", "the", "chat", "bot", "based", "on", "the", "provided", "list", "of", "statements", "that", "represents", "a", "single", "conversation", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L83-L117
[ "def", "train", "(", "self", ",", "conversation", ")", ":", "previous_statement_text", "=", "None", "previous_statement_search_text", "=", "''", "statements_to_create", "=", "[", "]", "for", "conversation_count", ",", "text", "in", "enumerate", "(", "conversation", ...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
UbuntuCorpusTrainer.is_downloaded
Check if the data file is already downloaded.
chatterbot/trainers.py
def is_downloaded(self, file_path): """ Check if the data file is already downloaded. """ if os.path.exists(file_path): self.chatbot.logger.info('File is already downloaded') return True return False
def is_downloaded(self, file_path): """ Check if the data file is already downloaded. """ if os.path.exists(file_path): self.chatbot.logger.info('File is already downloaded') return True return False
[ "Check", "if", "the", "data", "file", "is", "already", "downloaded", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L203-L211
[ "def", "is_downloaded", "(", "self", ",", "file_path", ")", ":", "if", "os", ".", "path", ".", "exists", "(", "file_path", ")", ":", "self", ".", "chatbot", ".", "logger", ".", "info", "(", "'File is already downloaded'", ")", "return", "True", "return", ...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
UbuntuCorpusTrainer.is_extracted
Check if the data file is already extracted.
chatterbot/trainers.py
def is_extracted(self, file_path): """ Check if the data file is already extracted. """ if os.path.isdir(file_path): self.chatbot.logger.info('File is already extracted') return True return False
def is_extracted(self, file_path): """ Check if the data file is already extracted. """ if os.path.isdir(file_path): self.chatbot.logger.info('File is already extracted') return True return False
[ "Check", "if", "the", "data", "file", "is", "already", "extracted", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L213-L221
[ "def", "is_extracted", "(", "self", ",", "file_path", ")", ":", "if", "os", ".", "path", ".", "isdir", "(", "file_path", ")", ":", "self", ".", "chatbot", ".", "logger", ".", "info", "(", "'File is already extracted'", ")", "return", "True", "return", "F...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
UbuntuCorpusTrainer.download
Download a file from the given url. Show a progress indicator for the download status. Based on: http://stackoverflow.com/a/15645088/1547223
chatterbot/trainers.py
def download(self, url, show_status=True): """ Download a file from the given url. Show a progress indicator for the download status. Based on: http://stackoverflow.com/a/15645088/1547223 """ import requests file_name = url.split('/')[-1] file_path = os.path.join(self.data_directory, file_name) # Do not download the data if it already exists if self.is_downloaded(file_path): return file_path with open(file_path, 'wb') as open_file: print('Downloading %s' % url) response = requests.get(url, stream=True) total_length = response.headers.get('content-length') if total_length is None: # No content length header open_file.write(response.content) else: download = 0 total_length = int(total_length) for data in response.iter_content(chunk_size=4096): download += len(data) open_file.write(data) if show_status: done = int(50 * download / total_length) sys.stdout.write('\r[%s%s]' % ('=' * done, ' ' * (50 - done))) sys.stdout.flush() # Add a new line after the download bar sys.stdout.write('\n') print('Download location: %s' % file_path) return file_path
def download(self, url, show_status=True): """ Download a file from the given url. Show a progress indicator for the download status. Based on: http://stackoverflow.com/a/15645088/1547223 """ import requests file_name = url.split('/')[-1] file_path = os.path.join(self.data_directory, file_name) # Do not download the data if it already exists if self.is_downloaded(file_path): return file_path with open(file_path, 'wb') as open_file: print('Downloading %s' % url) response = requests.get(url, stream=True) total_length = response.headers.get('content-length') if total_length is None: # No content length header open_file.write(response.content) else: download = 0 total_length = int(total_length) for data in response.iter_content(chunk_size=4096): download += len(data) open_file.write(data) if show_status: done = int(50 * download / total_length) sys.stdout.write('\r[%s%s]' % ('=' * done, ' ' * (50 - done))) sys.stdout.flush() # Add a new line after the download bar sys.stdout.write('\n') print('Download location: %s' % file_path) return file_path
[ "Download", "a", "file", "from", "the", "given", "url", ".", "Show", "a", "progress", "indicator", "for", "the", "download", "status", ".", "Based", "on", ":", "http", ":", "//", "stackoverflow", ".", "com", "/", "a", "/", "15645088", "/", "1547223" ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L223-L261
[ "def", "download", "(", "self", ",", "url", ",", "show_status", "=", "True", ")", ":", "import", "requests", "file_name", "=", "url", ".", "split", "(", "'/'", ")", "[", "-", "1", "]", "file_path", "=", "os", ".", "path", ".", "join", "(", "self", ...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
UbuntuCorpusTrainer.extract
Extract a tar file at the specified file path.
chatterbot/trainers.py
def extract(self, file_path): """ Extract a tar file at the specified file path. """ import tarfile print('Extracting {}'.format(file_path)) if not os.path.exists(self.extracted_data_directory): os.makedirs(self.extracted_data_directory) def track_progress(members): sys.stdout.write('.') for member in members: # This will be the current file being extracted yield member with tarfile.open(file_path) as tar: tar.extractall(path=self.extracted_data_directory, members=track_progress(tar)) self.chatbot.logger.info('File extracted to {}'.format(self.extracted_data_directory)) return True
def extract(self, file_path): """ Extract a tar file at the specified file path. """ import tarfile print('Extracting {}'.format(file_path)) if not os.path.exists(self.extracted_data_directory): os.makedirs(self.extracted_data_directory) def track_progress(members): sys.stdout.write('.') for member in members: # This will be the current file being extracted yield member with tarfile.open(file_path) as tar: tar.extractall(path=self.extracted_data_directory, members=track_progress(tar)) self.chatbot.logger.info('File extracted to {}'.format(self.extracted_data_directory)) return True
[ "Extract", "a", "tar", "file", "at", "the", "specified", "file", "path", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L263-L285
[ "def", "extract", "(", "self", ",", "file_path", ")", ":", "import", "tarfile", "print", "(", "'Extracting {}'", ".", "format", "(", "file_path", ")", ")", "if", "not", "os", ".", "path", ".", "exists", "(", "self", ".", "extracted_data_directory", ")", ...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
SQLStorageAdapter.count
Return the number of entries in the database.
chatterbot/storage/sql_storage.py
def count(self): """ Return the number of entries in the database. """ Statement = self.get_model('statement') session = self.Session() statement_count = session.query(Statement).count() session.close() return statement_count
def count(self): """ Return the number of entries in the database. """ Statement = self.get_model('statement') session = self.Session() statement_count = session.query(Statement).count() session.close() return statement_count
[ "Return", "the", "number", "of", "entries", "in", "the", "database", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L70-L79
[ "def", "count", "(", "self", ")", ":", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "session", "=", "self", ".", "Session", "(", ")", "statement_count", "=", "session", ".", "query", "(", "Statement", ")", ".", "count", "(", ")"...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
SQLStorageAdapter.remove
Removes the statement that matches the input text. Removes any responses from statements where the response text matches the input text.
chatterbot/storage/sql_storage.py
def remove(self, statement_text): """ Removes the statement that matches the input text. Removes any responses from statements where the response text matches the input text. """ Statement = self.get_model('statement') session = self.Session() query = session.query(Statement).filter_by(text=statement_text) record = query.first() session.delete(record) self._session_finish(session)
def remove(self, statement_text): """ Removes the statement that matches the input text. Removes any responses from statements where the response text matches the input text. """ Statement = self.get_model('statement') session = self.Session() query = session.query(Statement).filter_by(text=statement_text) record = query.first() session.delete(record) self._session_finish(session)
[ "Removes", "the", "statement", "that", "matches", "the", "input", "text", ".", "Removes", "any", "responses", "from", "statements", "where", "the", "response", "text", "matches", "the", "input", "text", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L81-L95
[ "def", "remove", "(", "self", ",", "statement_text", ")", ":", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "session", "=", "self", ".", "Session", "(", ")", "query", "=", "session", ".", "query", "(", "Statement", ")", ".", "fi...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
SQLStorageAdapter.filter
Returns a list of objects from the database. The kwargs parameter can contain any number of attributes. Only objects which contain all listed attributes and in which all values match for all listed attributes will be returned.
chatterbot/storage/sql_storage.py
def filter(self, **kwargs): """ Returns a list of objects from the database. The kwargs parameter can contain any number of attributes. Only objects which contain all listed attributes and in which all values match for all listed attributes will be returned. """ from sqlalchemy import or_ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() page_size = kwargs.pop('page_size', 1000) order_by = kwargs.pop('order_by', None) tags = kwargs.pop('tags', []) exclude_text = kwargs.pop('exclude_text', None) exclude_text_words = kwargs.pop('exclude_text_words', []) persona_not_startswith = kwargs.pop('persona_not_startswith', None) search_text_contains = kwargs.pop('search_text_contains', None) # Convert a single sting into a list if only one tag is provided if type(tags) == str: tags = [tags] if len(kwargs) == 0: statements = session.query(Statement).filter() else: statements = session.query(Statement).filter_by(**kwargs) if tags: statements = statements.join(Statement.tags).filter( Tag.name.in_(tags) ) if exclude_text: statements = statements.filter( ~Statement.text.in_(exclude_text) ) if exclude_text_words: or_word_query = [ Statement.text.ilike('%' + word + '%') for word in exclude_text_words ] statements = statements.filter( ~or_(*or_word_query) ) if persona_not_startswith: statements = statements.filter( ~Statement.persona.startswith('bot:') ) if search_text_contains: or_query = [ Statement.search_text.contains(word) for word in search_text_contains.split(' ') ] statements = statements.filter( or_(*or_query) ) if order_by: if 'created_at' in order_by: index = order_by.index('created_at') order_by[index] = Statement.created_at.asc() statements = statements.order_by(*order_by) total_statements = statements.count() for start_index in range(0, total_statements, page_size): for statement in statements.slice(start_index, start_index + page_size): yield self.model_to_object(statement) session.close()
def filter(self, **kwargs): """ Returns a list of objects from the database. The kwargs parameter can contain any number of attributes. Only objects which contain all listed attributes and in which all values match for all listed attributes will be returned. """ from sqlalchemy import or_ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() page_size = kwargs.pop('page_size', 1000) order_by = kwargs.pop('order_by', None) tags = kwargs.pop('tags', []) exclude_text = kwargs.pop('exclude_text', None) exclude_text_words = kwargs.pop('exclude_text_words', []) persona_not_startswith = kwargs.pop('persona_not_startswith', None) search_text_contains = kwargs.pop('search_text_contains', None) # Convert a single sting into a list if only one tag is provided if type(tags) == str: tags = [tags] if len(kwargs) == 0: statements = session.query(Statement).filter() else: statements = session.query(Statement).filter_by(**kwargs) if tags: statements = statements.join(Statement.tags).filter( Tag.name.in_(tags) ) if exclude_text: statements = statements.filter( ~Statement.text.in_(exclude_text) ) if exclude_text_words: or_word_query = [ Statement.text.ilike('%' + word + '%') for word in exclude_text_words ] statements = statements.filter( ~or_(*or_word_query) ) if persona_not_startswith: statements = statements.filter( ~Statement.persona.startswith('bot:') ) if search_text_contains: or_query = [ Statement.search_text.contains(word) for word in search_text_contains.split(' ') ] statements = statements.filter( or_(*or_query) ) if order_by: if 'created_at' in order_by: index = order_by.index('created_at') order_by[index] = Statement.created_at.asc() statements = statements.order_by(*order_by) total_statements = statements.count() for start_index in range(0, total_statements, page_size): for statement in statements.slice(start_index, start_index + page_size): yield self.model_to_object(statement) session.close()
[ "Returns", "a", "list", "of", "objects", "from", "the", "database", ".", "The", "kwargs", "parameter", "can", "contain", "any", "number", "of", "attributes", ".", "Only", "objects", "which", "contain", "all", "listed", "attributes", "and", "in", "which", "al...
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L97-L174
[ "def", "filter", "(", "self", ",", "*", "*", "kwargs", ")", ":", "from", "sqlalchemy", "import", "or_", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "Tag", "=", "self", ".", "get_model", "(", "'tag'", ")", "session", "=", "self"...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
SQLStorageAdapter.create
Creates a new statement matching the keyword arguments specified. Returns the created statement.
chatterbot/storage/sql_storage.py
def create(self, **kwargs): """ Creates a new statement matching the keyword arguments specified. Returns the created statement. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() tags = set(kwargs.pop('tags', [])) if 'search_text' not in kwargs: kwargs['search_text'] = self.tagger.get_bigram_pair_string(kwargs['text']) if 'search_in_response_to' not in kwargs: in_response_to = kwargs.get('in_response_to') if in_response_to: kwargs['search_in_response_to'] = self.tagger.get_bigram_pair_string(in_response_to) statement = Statement(**kwargs) for tag_name in tags: tag = session.query(Tag).filter_by(name=tag_name).first() if not tag: # Create the tag tag = Tag(name=tag_name) statement.tags.append(tag) session.add(statement) session.flush() session.refresh(statement) statement_object = self.model_to_object(statement) self._session_finish(session) return statement_object
def create(self, **kwargs): """ Creates a new statement matching the keyword arguments specified. Returns the created statement. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() tags = set(kwargs.pop('tags', [])) if 'search_text' not in kwargs: kwargs['search_text'] = self.tagger.get_bigram_pair_string(kwargs['text']) if 'search_in_response_to' not in kwargs: in_response_to = kwargs.get('in_response_to') if in_response_to: kwargs['search_in_response_to'] = self.tagger.get_bigram_pair_string(in_response_to) statement = Statement(**kwargs) for tag_name in tags: tag = session.query(Tag).filter_by(name=tag_name).first() if not tag: # Create the tag tag = Tag(name=tag_name) statement.tags.append(tag) session.add(statement) session.flush() session.refresh(statement) statement_object = self.model_to_object(statement) self._session_finish(session) return statement_object
[ "Creates", "a", "new", "statement", "matching", "the", "keyword", "arguments", "specified", ".", "Returns", "the", "created", "statement", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L176-L217
[ "def", "create", "(", "self", ",", "*", "*", "kwargs", ")", ":", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "Tag", "=", "self", ".", "get_model", "(", "'tag'", ")", "session", "=", "self", ".", "Session", "(", ")", "tags", ...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
SQLStorageAdapter.create_many
Creates multiple statement entries.
chatterbot/storage/sql_storage.py
def create_many(self, statements): """ Creates multiple statement entries. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() create_statements = [] create_tags = {} for statement in statements: statement_data = statement.serialize() tag_data = statement_data.pop('tags', []) statement_model_object = Statement(**statement_data) if not statement.search_text: statement_model_object.search_text = self.tagger.get_bigram_pair_string(statement.text) if not statement.search_in_response_to and statement.in_response_to: statement_model_object.search_in_response_to = self.tagger.get_bigram_pair_string(statement.in_response_to) new_tags = set(tag_data) - set(create_tags.keys()) if new_tags: existing_tags = session.query(Tag).filter( Tag.name.in_(new_tags) ) for existing_tag in existing_tags: create_tags[existing_tag.name] = existing_tag for tag_name in tag_data: if tag_name in create_tags: tag = create_tags[tag_name] else: # Create the tag if it does not exist tag = Tag(name=tag_name) create_tags[tag_name] = tag statement_model_object.tags.append(tag) create_statements.append(statement_model_object) session.add_all(create_statements) session.commit()
def create_many(self, statements): """ Creates multiple statement entries. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() create_statements = [] create_tags = {} for statement in statements: statement_data = statement.serialize() tag_data = statement_data.pop('tags', []) statement_model_object = Statement(**statement_data) if not statement.search_text: statement_model_object.search_text = self.tagger.get_bigram_pair_string(statement.text) if not statement.search_in_response_to and statement.in_response_to: statement_model_object.search_in_response_to = self.tagger.get_bigram_pair_string(statement.in_response_to) new_tags = set(tag_data) - set(create_tags.keys()) if new_tags: existing_tags = session.query(Tag).filter( Tag.name.in_(new_tags) ) for existing_tag in existing_tags: create_tags[existing_tag.name] = existing_tag for tag_name in tag_data: if tag_name in create_tags: tag = create_tags[tag_name] else: # Create the tag if it does not exist tag = Tag(name=tag_name) create_tags[tag_name] = tag statement_model_object.tags.append(tag) create_statements.append(statement_model_object) session.add_all(create_statements) session.commit()
[ "Creates", "multiple", "statement", "entries", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L219-L267
[ "def", "create_many", "(", "self", ",", "statements", ")", ":", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "Tag", "=", "self", ".", "get_model", "(", "'tag'", ")", "session", "=", "self", ".", "Session", "(", ")", "create_statem...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
SQLStorageAdapter.update
Modifies an entry in the database. Creates an entry if one does not exist.
chatterbot/storage/sql_storage.py
def update(self, statement): """ Modifies an entry in the database. Creates an entry if one does not exist. """ Statement = self.get_model('statement') Tag = self.get_model('tag') if statement is not None: session = self.Session() record = None if hasattr(statement, 'id') and statement.id is not None: record = session.query(Statement).get(statement.id) else: record = session.query(Statement).filter( Statement.text == statement.text, Statement.conversation == statement.conversation, ).first() # Create a new statement entry if one does not already exist if not record: record = Statement( text=statement.text, conversation=statement.conversation, persona=statement.persona ) # Update the response value record.in_response_to = statement.in_response_to record.created_at = statement.created_at record.search_text = self.tagger.get_bigram_pair_string(statement.text) if statement.in_response_to: record.search_in_response_to = self.tagger.get_bigram_pair_string(statement.in_response_to) for tag_name in statement.get_tags(): tag = session.query(Tag).filter_by(name=tag_name).first() if not tag: # Create the record tag = Tag(name=tag_name) record.tags.append(tag) session.add(record) self._session_finish(session)
def update(self, statement): """ Modifies an entry in the database. Creates an entry if one does not exist. """ Statement = self.get_model('statement') Tag = self.get_model('tag') if statement is not None: session = self.Session() record = None if hasattr(statement, 'id') and statement.id is not None: record = session.query(Statement).get(statement.id) else: record = session.query(Statement).filter( Statement.text == statement.text, Statement.conversation == statement.conversation, ).first() # Create a new statement entry if one does not already exist if not record: record = Statement( text=statement.text, conversation=statement.conversation, persona=statement.persona ) # Update the response value record.in_response_to = statement.in_response_to record.created_at = statement.created_at record.search_text = self.tagger.get_bigram_pair_string(statement.text) if statement.in_response_to: record.search_in_response_to = self.tagger.get_bigram_pair_string(statement.in_response_to) for tag_name in statement.get_tags(): tag = session.query(Tag).filter_by(name=tag_name).first() if not tag: # Create the record tag = Tag(name=tag_name) record.tags.append(tag) session.add(record) self._session_finish(session)
[ "Modifies", "an", "entry", "in", "the", "database", ".", "Creates", "an", "entry", "if", "one", "does", "not", "exist", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L269-L318
[ "def", "update", "(", "self", ",", "statement", ")", ":", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "Tag", "=", "self", ".", "get_model", "(", "'tag'", ")", "if", "statement", "is", "not", "None", ":", "session", "=", "self",...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
SQLStorageAdapter.get_random
Returns a random statement from the database.
chatterbot/storage/sql_storage.py
def get_random(self): """ Returns a random statement from the database. """ import random Statement = self.get_model('statement') session = self.Session() count = self.count() if count < 1: raise self.EmptyDatabaseException() random_index = random.randrange(0, count) random_statement = session.query(Statement)[random_index] statement = self.model_to_object(random_statement) session.close() return statement
def get_random(self): """ Returns a random statement from the database. """ import random Statement = self.get_model('statement') session = self.Session() count = self.count() if count < 1: raise self.EmptyDatabaseException() random_index = random.randrange(0, count) random_statement = session.query(Statement)[random_index] statement = self.model_to_object(random_statement) session.close() return statement
[ "Returns", "a", "random", "statement", "from", "the", "database", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L320-L339
[ "def", "get_random", "(", "self", ")", ":", "import", "random", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "session", "=", "self", ".", "Session", "(", ")", "count", "=", "self", ".", "count", "(", ")", "if", "count", "<", "...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
SQLStorageAdapter.drop
Drop the database.
chatterbot/storage/sql_storage.py
def drop(self): """ Drop the database. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() session.query(Statement).delete() session.query(Tag).delete() session.commit() session.close()
def drop(self): """ Drop the database. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() session.query(Statement).delete() session.query(Tag).delete() session.commit() session.close()
[ "Drop", "the", "database", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L341-L354
[ "def", "drop", "(", "self", ")", ":", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "Tag", "=", "self", ".", "get_model", "(", "'tag'", ")", "session", "=", "self", ".", "Session", "(", ")", "session", ".", "query", "(", "State...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
SQLStorageAdapter.create_database
Populate the database with the tables.
chatterbot/storage/sql_storage.py
def create_database(self): """ Populate the database with the tables. """ from chatterbot.ext.sqlalchemy_app.models import Base Base.metadata.create_all(self.engine)
def create_database(self): """ Populate the database with the tables. """ from chatterbot.ext.sqlalchemy_app.models import Base Base.metadata.create_all(self.engine)
[ "Populate", "the", "database", "with", "the", "tables", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L356-L361
[ "def", "create_database", "(", "self", ")", ":", "from", "chatterbot", ".", "ext", ".", "sqlalchemy_app", ".", "models", "import", "Base", "Base", ".", "metadata", ".", "create_all", "(", "self", ".", "engine", ")" ]
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
ChatterBotApiView.post
Return a response to the statement in the posted data. * The JSON data should contain a 'text' attribute.
examples/django_app/example_app/views.py
def post(self, request, *args, **kwargs): """ Return a response to the statement in the posted data. * The JSON data should contain a 'text' attribute. """ input_data = json.loads(request.body.decode('utf-8')) if 'text' not in input_data: return JsonResponse({ 'text': [ 'The attribute "text" is required.' ] }, status=400) response = self.chatterbot.get_response(input_data) response_data = response.serialize() return JsonResponse(response_data, status=200)
def post(self, request, *args, **kwargs): """ Return a response to the statement in the posted data. * The JSON data should contain a 'text' attribute. """ input_data = json.loads(request.body.decode('utf-8')) if 'text' not in input_data: return JsonResponse({ 'text': [ 'The attribute "text" is required.' ] }, status=400) response = self.chatterbot.get_response(input_data) response_data = response.serialize() return JsonResponse(response_data, status=200)
[ "Return", "a", "response", "to", "the", "statement", "in", "the", "posted", "data", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/examples/django_app/example_app/views.py#L20-L39
[ "def", "post", "(", "self", ",", "request", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "input_data", "=", "json", ".", "loads", "(", "request", ".", "body", ".", "decode", "(", "'utf-8'", ")", ")", "if", "'text'", "not", "in", "input_data...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
get_file_path
Reads a dotted file path and returns the file path.
chatterbot/corpus.py
def get_file_path(dotted_path, extension='json'): """ Reads a dotted file path and returns the file path. """ # If the operating system's file path seperator character is in the string if os.sep in dotted_path or '/' in dotted_path: # Assume the path is a valid file path return dotted_path parts = dotted_path.split('.') if parts[0] == 'chatterbot': parts.pop(0) parts[0] = DATA_DIRECTORY corpus_path = os.path.join(*parts) if os.path.exists(corpus_path + '.{}'.format(extension)): corpus_path += '.{}'.format(extension) return corpus_path
def get_file_path(dotted_path, extension='json'): """ Reads a dotted file path and returns the file path. """ # If the operating system's file path seperator character is in the string if os.sep in dotted_path or '/' in dotted_path: # Assume the path is a valid file path return dotted_path parts = dotted_path.split('.') if parts[0] == 'chatterbot': parts.pop(0) parts[0] = DATA_DIRECTORY corpus_path = os.path.join(*parts) if os.path.exists(corpus_path + '.{}'.format(extension)): corpus_path += '.{}'.format(extension) return corpus_path
[ "Reads", "a", "dotted", "file", "path", "and", "returns", "the", "file", "path", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/corpus.py#L11-L30
[ "def", "get_file_path", "(", "dotted_path", ",", "extension", "=", "'json'", ")", ":", "# If the operating system's file path seperator character is in the string", "if", "os", ".", "sep", "in", "dotted_path", "or", "'/'", "in", "dotted_path", ":", "# Assume the path is a...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
read_corpus
Read and return the data from a corpus json file.
chatterbot/corpus.py
def read_corpus(file_name): """ Read and return the data from a corpus json file. """ with io.open(file_name, encoding='utf-8') as data_file: return yaml.load(data_file)
def read_corpus(file_name): """ Read and return the data from a corpus json file. """ with io.open(file_name, encoding='utf-8') as data_file: return yaml.load(data_file)
[ "Read", "and", "return", "the", "data", "from", "a", "corpus", "json", "file", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/corpus.py#L33-L38
[ "def", "read_corpus", "(", "file_name", ")", ":", "with", "io", ".", "open", "(", "file_name", ",", "encoding", "=", "'utf-8'", ")", "as", "data_file", ":", "return", "yaml", ".", "load", "(", "data_file", ")" ]
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
list_corpus_files
Return a list of file paths to each data file in the specified corpus.
chatterbot/corpus.py
def list_corpus_files(dotted_path): """ Return a list of file paths to each data file in the specified corpus. """ corpus_path = get_file_path(dotted_path, extension=CORPUS_EXTENSION) paths = [] if os.path.isdir(corpus_path): paths = glob.glob(corpus_path + '/**/*.' + CORPUS_EXTENSION, recursive=True) else: paths.append(corpus_path) paths.sort() return paths
def list_corpus_files(dotted_path): """ Return a list of file paths to each data file in the specified corpus. """ corpus_path = get_file_path(dotted_path, extension=CORPUS_EXTENSION) paths = [] if os.path.isdir(corpus_path): paths = glob.glob(corpus_path + '/**/*.' + CORPUS_EXTENSION, recursive=True) else: paths.append(corpus_path) paths.sort() return paths
[ "Return", "a", "list", "of", "file", "paths", "to", "each", "data", "file", "in", "the", "specified", "corpus", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/corpus.py#L41-L54
[ "def", "list_corpus_files", "(", "dotted_path", ")", ":", "corpus_path", "=", "get_file_path", "(", "dotted_path", ",", "extension", "=", "CORPUS_EXTENSION", ")", "paths", "=", "[", "]", "if", "os", ".", "path", ".", "isdir", "(", "corpus_path", ")", ":", ...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
load_corpus
Return the data contained within a specified corpus.
chatterbot/corpus.py
def load_corpus(*data_file_paths): """ Return the data contained within a specified corpus. """ for file_path in data_file_paths: corpus = [] corpus_data = read_corpus(file_path) conversations = corpus_data.get('conversations', []) corpus.extend(conversations) categories = corpus_data.get('categories', []) yield corpus, categories, file_path
def load_corpus(*data_file_paths): """ Return the data contained within a specified corpus. """ for file_path in data_file_paths: corpus = [] corpus_data = read_corpus(file_path) conversations = corpus_data.get('conversations', []) corpus.extend(conversations) categories = corpus_data.get('categories', []) yield corpus, categories, file_path
[ "Return", "the", "data", "contained", "within", "a", "specified", "corpus", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/corpus.py#L57-L70
[ "def", "load_corpus", "(", "*", "data_file_paths", ")", ":", "for", "file_path", "in", "data_file_paths", ":", "corpus", "=", "[", "]", "corpus_data", "=", "read_corpus", "(", "file_path", ")", "conversations", "=", "corpus_data", ".", "get", "(", "'conversati...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
PosLemmaTagger.get_bigram_pair_string
Return a string of text containing part-of-speech, lemma pairs.
chatterbot/tagging.py
def get_bigram_pair_string(self, text): """ Return a string of text containing part-of-speech, lemma pairs. """ bigram_pairs = [] if len(text) <= 2: text_without_punctuation = text.translate(self.punctuation_table) if len(text_without_punctuation) >= 1: text = text_without_punctuation document = self.nlp(text) if len(text) <= 2: bigram_pairs = [ token.lemma_.lower() for token in document ] else: tokens = [ token for token in document if token.is_alpha and not token.is_stop ] if len(tokens) < 2: tokens = [ token for token in document if token.is_alpha ] for index in range(1, len(tokens)): bigram_pairs.append('{}:{}'.format( tokens[index - 1].pos_, tokens[index].lemma_.lower() )) if not bigram_pairs: bigram_pairs = [ token.lemma_.lower() for token in document ] return ' '.join(bigram_pairs)
def get_bigram_pair_string(self, text): """ Return a string of text containing part-of-speech, lemma pairs. """ bigram_pairs = [] if len(text) <= 2: text_without_punctuation = text.translate(self.punctuation_table) if len(text_without_punctuation) >= 1: text = text_without_punctuation document = self.nlp(text) if len(text) <= 2: bigram_pairs = [ token.lemma_.lower() for token in document ] else: tokens = [ token for token in document if token.is_alpha and not token.is_stop ] if len(tokens) < 2: tokens = [ token for token in document if token.is_alpha ] for index in range(1, len(tokens)): bigram_pairs.append('{}:{}'.format( tokens[index - 1].pos_, tokens[index].lemma_.lower() )) if not bigram_pairs: bigram_pairs = [ token.lemma_.lower() for token in document ] return ' '.join(bigram_pairs)
[ "Return", "a", "string", "of", "text", "containing", "part", "-", "of", "-", "speech", "lemma", "pairs", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/tagging.py#L15-L53
[ "def", "get_bigram_pair_string", "(", "self", ",", "text", ")", ":", "bigram_pairs", "=", "[", "]", "if", "len", "(", "text", ")", "<=", "2", ":", "text_without_punctuation", "=", "text", ".", "translate", "(", "self", ".", "punctuation_table", ")", "if", ...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
DjangoStorageAdapter.filter
Returns a list of statements in the database that match the parameters specified.
chatterbot/storage/django_storage.py
def filter(self, **kwargs): """ Returns a list of statements in the database that match the parameters specified. """ from django.db.models import Q Statement = self.get_model('statement') kwargs.pop('page_size', 1000) order_by = kwargs.pop('order_by', None) tags = kwargs.pop('tags', []) exclude_text = kwargs.pop('exclude_text', None) exclude_text_words = kwargs.pop('exclude_text_words', []) persona_not_startswith = kwargs.pop('persona_not_startswith', None) search_text_contains = kwargs.pop('search_text_contains', None) # Convert a single sting into a list if only one tag is provided if type(tags) == str: tags = [tags] if tags: kwargs['tags__name__in'] = tags statements = Statement.objects.filter(**kwargs) if exclude_text: statements = statements.exclude( text__in=exclude_text ) if exclude_text_words: or_query = [ ~Q(text__icontains=word) for word in exclude_text_words ] statements = statements.filter( *or_query ) if persona_not_startswith: statements = statements.exclude( persona__startswith='bot:' ) if search_text_contains: or_query = Q() for word in search_text_contains.split(' '): or_query |= Q(search_text__contains=word) statements = statements.filter( or_query ) if order_by: statements = statements.order_by(*order_by) for statement in statements.iterator(): yield statement
def filter(self, **kwargs): """ Returns a list of statements in the database that match the parameters specified. """ from django.db.models import Q Statement = self.get_model('statement') kwargs.pop('page_size', 1000) order_by = kwargs.pop('order_by', None) tags = kwargs.pop('tags', []) exclude_text = kwargs.pop('exclude_text', None) exclude_text_words = kwargs.pop('exclude_text_words', []) persona_not_startswith = kwargs.pop('persona_not_startswith', None) search_text_contains = kwargs.pop('search_text_contains', None) # Convert a single sting into a list if only one tag is provided if type(tags) == str: tags = [tags] if tags: kwargs['tags__name__in'] = tags statements = Statement.objects.filter(**kwargs) if exclude_text: statements = statements.exclude( text__in=exclude_text ) if exclude_text_words: or_query = [ ~Q(text__icontains=word) for word in exclude_text_words ] statements = statements.filter( *or_query ) if persona_not_startswith: statements = statements.exclude( persona__startswith='bot:' ) if search_text_contains: or_query = Q() for word in search_text_contains.split(' '): or_query |= Q(search_text__contains=word) statements = statements.filter( or_query ) if order_by: statements = statements.order_by(*order_by) for statement in statements.iterator(): yield statement
[ "Returns", "a", "list", "of", "statements", "in", "the", "database", "that", "match", "the", "parameters", "specified", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/django_storage.py#L31-L90
[ "def", "filter", "(", "self", ",", "*", "*", "kwargs", ")", ":", "from", "django", ".", "db", ".", "models", "import", "Q", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "kwargs", ".", "pop", "(", "'page_size'", ",", "1000", ")...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
train
DjangoStorageAdapter.create
Creates a new statement matching the keyword arguments specified. Returns the created statement.
chatterbot/storage/django_storage.py
def create(self, **kwargs): """ Creates a new statement matching the keyword arguments specified. Returns the created statement. """ Statement = self.get_model('statement') Tag = self.get_model('tag') tags = kwargs.pop('tags', []) if 'search_text' not in kwargs: kwargs['search_text'] = self.tagger.get_bigram_pair_string(kwargs['text']) if 'search_in_response_to' not in kwargs: if kwargs.get('in_response_to'): kwargs['search_in_response_to'] = self.tagger.get_bigram_pair_string(kwargs['in_response_to']) statement = Statement(**kwargs) statement.save() tags_to_add = [] for _tag in tags: tag, _ = Tag.objects.get_or_create(name=_tag) tags_to_add.append(tag) statement.tags.add(*tags_to_add) return statement
def create(self, **kwargs): """ Creates a new statement matching the keyword arguments specified. Returns the created statement. """ Statement = self.get_model('statement') Tag = self.get_model('tag') tags = kwargs.pop('tags', []) if 'search_text' not in kwargs: kwargs['search_text'] = self.tagger.get_bigram_pair_string(kwargs['text']) if 'search_in_response_to' not in kwargs: if kwargs.get('in_response_to'): kwargs['search_in_response_to'] = self.tagger.get_bigram_pair_string(kwargs['in_response_to']) statement = Statement(**kwargs) statement.save() tags_to_add = [] for _tag in tags: tag, _ = Tag.objects.get_or_create(name=_tag) tags_to_add.append(tag) statement.tags.add(*tags_to_add) return statement
[ "Creates", "a", "new", "statement", "matching", "the", "keyword", "arguments", "specified", ".", "Returns", "the", "created", "statement", "." ]
gunthercox/ChatterBot
python
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/django_storage.py#L92-L121
[ "def", "create", "(", "self", ",", "*", "*", "kwargs", ")", ":", "Statement", "=", "self", ".", "get_model", "(", "'statement'", ")", "Tag", "=", "self", ".", "get_model", "(", "'tag'", ")", "tags", "=", "kwargs", ".", "pop", "(", "'tags'", ",", "[...
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2