def from_dict(cls, obj, reference_list): parsed_dimensions = {} data_dimension_map = DataDimension._value2member_map_ for key, value in obj.get('dimensions', {}).items(): if key in data_dimension_map: parsed_dimensions[DataDimension(key)] = value else: parsed_dimensions[key] = value return DimensionsOverride(column_names=obj.get('columnNames', []), dimensions=parsed_dimensions, coordinate=DataCoordinate.from_dict(obj.get('coordinate', {})))
def from_dict(cls, obj: Dict, reference_list: List): processor_name: str = obj.get(PROCESSOR_NAME) # Dynamically import the processor to for instantiation. processor = getattr( __import__('gs_quant.analytics.processors', fromlist=['']), processor_name, None) parameters = obj.get(PARAMETERS, {}) local_reference_list = [] arguments = {} for parameter, parameters_dict in parameters.items(): # Loop through all the parameters and turned them into objects based off their dictionary values. # Will recursively handle the more complex objects such as DataCoordinate and Processors. parameter_type: str = parameters_dict.get(TYPE) if parameter_type == DATA_COORDINATE: # Handle the DataCoordinate parameters arguments[parameter] = DataCoordinate.from_dict( parameters_dict) elif parameter_type == PROCESSOR: # Handle the BaseProcessor parameters arguments[parameter] = BaseProcessor.from_dict( parameters_dict, reference_list) elif parameter_type == ENTITY: # Handle the entity parameter list and put into the reference mapped to be resolved later local_reference_list.append({ TYPE: PROCESSOR, ENTITY_ID: parameters_dict.get(ENTITY_ID), ENTITY_TYPE: parameters_dict.get(ENTITY_TYPE), PARAMETER: parameter }) arguments[parameter] = None elif parameter_type in (DATE, DATETIME, RELATIVE_DATE): # Handle date/datetime parameters if parameter_type == DATE: arguments[parameter] = datetime.strptime( parameters_dict.get(VALUE), '%Y-%m-%d').date() elif parameter_type == RELATIVE_DATE: val = parameters_dict.get(VALUE) base_date = val.get('baseDate') base_date = datetime.strptime( base_date, '%Y-%m-%d').date() if base_date else None arguments[parameter] = RelativeDate(rule=val['rule'], base_date=base_date) else: arguments[parameter] = datetime.strptime( parameters_dict.get(VALUE)[0:-1], '%Y-%m-%dT%H:%M:%S.%f') else: # Handle all other object which should be mapped in the PARSABLE_OBJECT_MAP if parameter_type in PARSABLE_OBJECT_MAP: parameter_obj = PARSABLE_OBJECT_MAP[parameter_type] if isinstance(parameter_obj, (Enum, EnumMeta)): arguments[parameter] = parameter_obj( parameters_dict.get(VALUE, {})) else: arguments[parameter] = parameter_obj.from_dict( parameters_dict.get(VALUE, {})) else: # Handles built in types that are stored natively arguments[parameter] = parameters_dict.get(VALUE) processor = processor( **arguments) # Instantiate the processor with all arguments # Add all the references to entities to the list which will be resolved later for reference in local_reference_list: reference[REFERENCE] = processor reference_list.extend(local_reference_list) return processor