Ejemplo n.º 1
0
 def parseCSV(self, specfile):
     csv_dictionary = ip.loadSpec(specfile, csv_flag=True)
     file1 = csv_dictionary['csv1']
     file2 = csv_dictionary['csv2']
     key1 = csv_dictionary['csv1_key']
     key2 = csv_dictionary['csv2_key']
     join = csv_dictionary['join']
     object_name = csv_dictionary['objects']
     attribute_name = csv_dictionary['attributes']
     dic = ip.constructDictFromCSVFiles(file1, file2, key1, key2,
                                        object_name, attribute_name, join)
     self.attributes = list(set(itertools.chain.from_iterable(
         dic.values())))
     self.num_attributes = len(self.attributes)
     precontext = [(k, [self.attributes.index(e) for e in v])
                   for k, v in dic.items()]
     self.objects, self.context = zip(*[(c[0], ''.join(
         ['1' if i in c[1] else '0' for i in range(self.num_attributes)]))
                                        for c in precontext])
     self.num_objects = len(self.objects)
     return self
Ejemplo n.º 2
0
 def parseQueryRes(
         self, filepath,
         query_res):  #generates context object from json output of a query
     #print('----------------------------------------')
     #print('parsing query result json',filepath,query_res)
     spec = ip.loadSpec(filepath, csv_flag=False)
     query = spec['query']
     obj_name = spec['objects']
     att_name = (spec['attributes'].strip() if ',' not in spec['attributes']
                 else [s.strip() for s in spec['attributes'].split(',')])
     query_content = json.load(open(query_res, 'r'))
     dic = collections.defaultdict(list)
     self.attributes = (list({e[att_name]
                              for e in query_content})
                        if type(att_name) == str else list({
                            str(a) + '#' + str(e[a])
                            for a in att_name for e in query_content
                        }))
     self.num_attributes = len(self.attributes)
     attributes_index = dict(
         (self.attributes[i], i) for i in range(self.num_attributes))
     if type(att_name) == str:
         for e in query_content:
             dic[e[obj_name]].append(attributes_index[e[att_name]])
     elif type(att_name) == list:
         for e in query_content:
             dic[e[obj_name]].extend([
                 attributes_index[str(a) + '#' + str(e[a])]
                 for a in att_name
             ])
     self.objects, self.context = zip(*[(k, ''.join(
         ['1' if i in v else '0' for i in range(self.num_attributes)]))
                                        for k, v in dic.items()])
     self.num_objects = len(self.objects)
     #print('parseQueryRes done')
     #print('----------------------------------------')
     return self
Ejemplo n.º 3
0
 def parseQuery(
     self, filepath
 ):  #generates context object from json query specification file
     spec = ip.loadSpec(filepath, csv_flag=False)
     query = spec['query']
     obj_name = spec['objects']
     att_name = (spec['attributes'].strip() if ',' not in spec['attributes']
                 else [s.strip() for s in spec['attributes'].split(',')])
     if 'port' in spec.keys():
         port = spec['port']
     else:
         port = 8080
     query_res = ip.getQuery(query, port)
     dic = collections.defaultdict(list)
     self.attributes = (list({e[att_name]
                              for e in query_res})
                        if type(att_name) == str else list({
                            str(a) + '#' + str(e[a])
                            for a in att_name for e in query_res
                        }))
     self.num_attributes = len(self.attributes)
     attributes_index = dict(
         (self.attributes[i], i) for i in range(self.num_attributes))
     if type(att_name) == str:
         for e in query_res:
             dic[e[obj_name]].append(attributes_index[e[att_name]])
     elif type(att_name) == list:
         for e in query_res:
             dic[e[obj_name]].extend([
                 attributes_index[str(a) + '#' + str(e[a])]
                 for a in att_name
             ])
     self.objects, self.context = zip(*[(k, ''.join(
         ['1' if i in v else '0' for i in range(self.num_attributes)]))
                                        for k, v in dic.items()])
     self.num_objects = len(self.objects)
     return self