CMS 3D CMS Logo

data_formats.py
Go to the documentation of this file.
1 """
2 
3 This file holds decorator functions that can rearrange data returned from data sources.
4 They should be used to decorate the method that holds the script that is being passed to the framework.
5 
6 Note: may also contain a decorator that can wrap a class around a function that contains a script (future development).
7 
8 """
9 
10 from data_sources import json_data_node, json_list, json_dict, json_basic
11 
12 # decorators
13 
14 # will convert {headers:[], data:[[]]} to {{header:value}, ..., {header:value}}
15 # will not take any arguments in decorator syntax at the moment -
16 # only adjust the output data from the decorated function
17 def to_array_of_dicts(script):
18  def new_script(self, connection):
19  try:
20  data = script(self, connection)
21  array_of_dicts = _to_array_of_dicts(data)
22  return json_data_node.make(array_of_dicts)
23  except (KeyError, TypeError) as e:
24  raise Exception("The data you gave wasn't in the correct format: %s" % str(e))
25  return new_script
26 
27 # convert {{header:value}, ..., {header:value}} to {headers:[], data:[[]]}
28 def to_datatables(script):
29  def new_script(self, connection):
30  try:
31  data = script(self, connection)
32  if(type(data) == list):
33  data = _json_data_node.make(data)
34  return to_datatables(data)
35  except (KeyError, TypeError) as e:
36  raise Exception("The data you gave wasn't in the correct format: %s" % str(e))
37  return new_script
38 
39 def query(script):
40  def new_script(self, connection):
41  try:
42  data = script(self, connection)
43  return _to_sql_query(data)
44  except (KeyError, TypeError) as e:
45  raise Exception("The data you gave wasn't in the correct format: %s" % str(e))
46  return new_script
47 
48 def objects_to_dicts(script):
49  def new_script(self, connection):
50  try:
51  data = script(self, connection)
52  return _objects_to_dicts(data)
53  except (KeyError, TypeError) as e:
54  raise Exception("The data you gave wasn't in the correct format: %s" % str(e))
55  return new_script
56 
57 # functions used in decorators
58 
60  # check to see if the user has returned a data source, instead of a json data node
61  if not(data.__class__.__name__ in ["json_list", "json_dict", "json_basic"]):
62  data = json_data_node.make(data)
63  headers = data.get("headers").data()
64  data_list = data.get("data").data()
65  def unicode_to_str(string):
66  return str(string) if type(string) == unicode else string
67  headers = map(unicode_to_str, headers)
68  def row_to_dict(row):
69  row = map(unicode_to_str, row)
70  return dict(zip(headers, row))
71  array_of_dicts = map(row_to_dict, data_list)
72  return json_data_node.make(array_of_dicts)
73 
74 def _to_datatables(data):
75  headers = map(str, data.get(0).data().keys())
76  new_data = []
77  for n in range(0, len(data.data())):
78  new_data.append(map(lambda entry : str(entry) if type(entry) == unicode else entry, data.get(n).data().values()))
79  return json_data_node.make({
80  "headers" : headers,
81  "data" : new_data
82  })
83 
84 def to_sql_query(data):
85  return data.to_sql()
86 
87 # apply function to specific column of data, assuming data
88 def apply_function(data, function, key):
89  data = data.data()
90  def apply_function_to_key(row):
91  row[key] = function(row[key])
92  return row
93  new_data = [apply_function_to_key(data[n]) for n in range(0, len(data))]
94  return json_data_node(new_data)
95 
97  if data.__class__.__name__ in ["json_list", "json_dict", "json_basic"]:
98  data = data.data()
99  new_data = [data[n].as_dicts() for n in range(0, len(data))]
100  return json_data_node.make(new_data)
101 
102 def _dicts_to_orm_objects(model, data):
103  if data.__class__.__name__ in ["json_list", "json_dict", "json_basic"]:
104  data = data.data()
105  new_data = [model(data[n]) for n in range(0, len(data))]
106  return new_data
def _dicts_to_orm_objects(model, data)
Definition: vlib.h:256
def apply_function(data, function, key)
Definition: data_formats.py:88
def query(script)
Definition: data_formats.py:39
def to_array_of_dicts(script)
Definition: data_formats.py:17
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def to_sql_query(data)
Definition: data_formats.py:84
def objects_to_dicts(script)
Definition: data_formats.py:48
def to_datatables(script)
Definition: data_formats.py:28
def _to_array_of_dicts(data)
Definition: data_formats.py:59
def _objects_to_dicts(data)
Definition: data_formats.py:96
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
if(dp >Float(M_PI)) dp-
def _to_datatables(data)
Definition: data_formats.py:74