Could you please provide some function using the Python LDL's get_data for lots of data, specifically, lots of instruments?
Could you please provide some function using the Python LDL's get_data for lots of data, specifically, lots of instruments?
Hi @danieluphromes, how about this?
def get_lots_of_data( req_univ: list[str], fields: list[str], batch_of: int = 50, warns: bool = True, messages: bool = True): err = {'Error': [], 'ErrorUniverse': []} if not warns: warnings.filterwarnings("ignore", category=FutureWarning, module="lseg.data._tools._dataframe") def Chunks(lst, n): """Yield successive n-sized chunks from lst.""" for i in range(0, len(lst), n): yield lst[i:i + n] df_list = [] for i, j in enumerate(Chunks(lst=req_univ, n=batch_of)): if messages: print(f"Batch of {batch_of} requests no. {str(i+1)}/{str(len([i for i in Chunks(req_univ, batch_of)]))} started") try: _df = ld.get_data( universe=j, fields=fields) df_list.append(_df) df = pd.concat(df_list, ignore_index=True) except ld.errors.LDError as e: print("There was an error, please note that the following were not added to the df:") # print("LDError:") print(e) err['Error'].append(e) err['ErrorUniverse'].append(j) if messages: print(f"Batch of {batch_of} requests no. {str(i+1)}/{str(len([i for i in Chunks(req_univ, batch_of)]))} ended") return df, err
.
If you get an error, you can find info about it with `err['Error'][0].__dict__`. The object `err['ErrorUniverse']` will give you all the universe items missed in the call.
.
def loop_get_lots_of_data( req_univ: list[str], fields: list[str], batch_of: int = 50, warns: bool = True, messages: bool = True, no_of_trials: int = 5 ): df, err = get_lots_of_data( req_univ=req_univ, fields=fields, warns=warns, messages=messages, batch_of=batch_of) trial = 0 while trial < no_of_trials: if len(err['ErrorUniverse']) != 0: # It's possible to have more than one list in the err['ErrorUniverse'] list, let's flatten it. err_univ = [item for row in err['ErrorUniverse'] for item in row] _df, _err = get_lots_of_data( req_univ=err_univ, fields=fields, warns=warns, messages=messages, batch_of=batch_of) if len(_df) > 0: df = pd.concat([df, _df], ignore_index=True) break trial += 1 return df
.