Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:53

0001 """
0002 
0003 This file holds decorator functions that can rearrange data returned from data sources.
0004 They should be used to decorate the method that holds the script that is being passed to the framework.
0005 
0006 Note: may also contain a decorator that can wrap a class around a function that contains a script (future development).
0007 
0008 """
0009 
0010 from .data_sources import json_data_node, json_list, json_dict, json_basic
0011 
0012 # decorators
0013 
0014 # will convert {headers:[], data:[[]]} to {{header:value}, ..., {header:value}}
0015 # will not take any arguments in decorator syntax at the moment - 
0016 # only adjust the output data from the decorated function
0017 def to_array_of_dicts(script):
0018     def new_script(self, connection):
0019         try:
0020             data = script(self, connection)
0021             array_of_dicts = _to_array_of_dicts(data)
0022             return json_data_node.make(array_of_dicts)
0023         except (KeyError, TypeError) as e:
0024             raise Exception("The data you gave wasn't in the correct format: %s" % str(e))
0025     return new_script
0026 
0027 # convert {{header:value}, ..., {header:value}} to {headers:[], data:[[]]}
0028 def to_datatables(script):
0029     def new_script(self, connection):
0030         try:
0031             data = script(self, connection)
0032             if(type(data) == list):
0033                 data = _json_data_node.make(data)
0034             return to_datatables(data)
0035         except (KeyError, TypeError) as e:
0036             raise Exception("The data you gave wasn't in the correct format: %s" % str(e))
0037     return new_script
0038 
0039 def query(script):
0040     def new_script(self, connection):
0041         try:
0042             data = script(self, connection)
0043             return _to_sql_query(data)
0044         except (KeyError, TypeError) as e:
0045             raise Exception("The data you gave wasn't in the correct format: %s" % str(e))
0046     return new_script
0047 
0048 def objects_to_dicts(script):
0049     def new_script(self, connection):
0050         try:
0051             data = script(self, connection)
0052             return _objects_to_dicts(data)
0053         except (KeyError, TypeError) as e:
0054             raise Exception("The data you gave wasn't in the correct format: %s" % str(e))
0055     return new_script
0056 
0057 # functions used in decorators
0058 
0059 def _to_array_of_dicts(data):
0060     # check to see if the user has returned a data source, instead of a json data node
0061     if not(data.__class__.__name__ in ["json_list", "json_dict", "json_basic"]):
0062         data = json_data_node.make(data)
0063     headers = data.get("headers").data()
0064     data_list = data.get("data").data()
0065     def unicode_to_str(string):
0066         return str(string) if type(string) == str else string
0067     headers = list(map(unicode_to_str, headers))
0068     def row_to_dict(row):
0069         row = list(map(unicode_to_str, row))
0070         return dict(list(zip(headers, row)))
0071     array_of_dicts = list(map(row_to_dict, data_list))
0072     return json_data_node.make(array_of_dicts)
0073 
0074 def _to_datatables(data):
0075     headers = list(map(str, list(data.get(0).data().keys())))
0076     new_data = []
0077     for n in range(0, len(data.data())):
0078         new_data.append([str(entry) if type(entry) == str else entry for entry in list(data.get(n).data().values())])
0079     return json_data_node.make({
0080         "headers" : headers,
0081         "data" : new_data
0082     })
0083 
0084 def to_sql_query(data):
0085     return data.to_sql()
0086 
0087 # apply function to specific column of data, assuming data
0088 def apply_function(data, function, key):
0089     data = data.data()
0090     def apply_function_to_key(row):
0091         row[key] = function(row[key])
0092         return row
0093     new_data = [apply_function_to_key(data[n]) for n in range(0, len(data))]
0094     return json_data_node(new_data)
0095 
0096 def _objects_to_dicts(data):
0097     if data.__class__.__name__ in ["json_list", "json_dict", "json_basic"]:
0098         data = data.data()
0099     new_data = [data[n].as_dicts() for n in range(0, len(data))]
0100     return json_data_node.make(new_data)
0101 
0102 def _dicts_to_orm_objects(model, data):
0103     if data.__class__.__name__ in ["json_list", "json_dict", "json_basic"]:
0104         data = data.data()
0105     new_data = [model(data[n]) for n in range(0, len(data))]
0106     return new_data