text stringlengths 0 828 |
|---|
obj = topLevelPackage |
for n in names[1:]: |
obj = getattr(obj, n) |
return obj" |
1190,"def for_name(modpath, classname): |
''' |
Returns a class of ""classname"" from module ""modname"". |
''' |
module = __import__(modpath, fromlist=[classname]) |
classobj = getattr(module, classname) |
return classobj()" |
1191,"def _convert(self, val): |
"""""" Convert the type if necessary and return if a conversion happened. """""" |
if isinstance(val, dict) and not isinstance(val, DotDict): |
return DotDict(val), True |
elif isinstance(val, list) and not isinstance(val, DotList): |
return DotList(val), True |
return val, False" |
1192,"def full_subgraph(self, vertices): |
"""""" |
Return the subgraph of this graph whose vertices |
are the given ones and whose edges are the edges |
of the original graph between those vertices. |
"""""" |
obj_map = {vertex.id: vertex for vertex in vertices} |
edges = [ |
edge for vertex_id in obj_map |
for edge in self._out_edges[vertex_id] |
if edge.head in obj_map |
] |
return AnnotatedGraph( |
vertices=obj_map.values(), |
edges=edges, |
)" |
1193,"def to_json(self): |
"""""" |
Convert to a JSON string. |
"""""" |
obj = { |
""vertices"": [ |
{ |
""id"": vertex.id, |
""annotation"": vertex.annotation, |
} |
for vertex in self.vertices |
], |
""edges"": [ |
{ |
""id"": edge.id, |
""annotation"": edge.annotation, |
""head"": edge.head, |
""tail"": edge.tail, |
} |
for edge in self._edges |
], |
} |
# Ensure that we always return unicode output on Python 2. |
return six.text_type(json.dumps(obj, ensure_ascii=False))" |
1194,"def from_json(cls, json_graph): |
"""""" |
Reconstruct the graph from a graph exported to JSON. |
"""""" |
obj = json.loads(json_graph) |
vertices = [ |
AnnotatedVertex( |
id=vertex[""id""], |
annotation=vertex[""annotation""], |
) |
for vertex in obj[""vertices""] |
] |
edges = [ |
AnnotatedEdge( |
id=edge[""id""], |
annotation=edge[""annotation""], |
head=edge[""head""], |
tail=edge[""tail""], |
) |
for edge in obj[""edges""] |
] |
return cls(vertices=vertices, edges=edges)" |
1195,"def export_json(self, filename): |
"""""" |
Export graph in JSON form to the given file. |
"""""" |
json_graph = self.to_json() |
with open(filename, 'wb') as f: |
f.write(json_graph.encode('utf-8'))" |
1196,"def import_json(cls, filename): |
"""""" |
Import graph from the given file. The file is expected |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.