demux / demux/simplegexf

Project restructuring

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
6
Forks
4
PR merge metrics
No merged PRs in 30d

Description

Should I go for more magic, or less?
### Ideas:

``` python
from simplegexf import Gexf, Graph, Edge

class MyGraph(simplegexf.Graph):
defaultedgetype='directed'

node_attributes = [
('name', 'string'),
('description', 'string'),
('weight', 'integer'),
]

edge_attributes = [
('rel_type', 'string'),
]

gexf = simplegexf.Gexf('/path/to/file.gexf')

try:
graph = gexf.graphs[0]
except KeyError:
graph = MyGraph()
gexf.graphs.append(graph)

tag_list = [
{
'id': 'c6fa996d-8593-484d-b067-69bc828bbeba',
'name': 'Test Tag',
'description': "Fairy penguins citylink, east brunswick club trams",
'weight': 13,
'parents': [], # List of UUIDs
}
...
]

# Create Nodes:
for tag in tag_list:
try:
# graph.nodes behaves like an OrderedDict, and nodes get mapped by id,
# so it will throw `KeyError` if the key does not exist:
node = graph.nodes[tag['id']]
# or, graph.nodes should behave like a list, and we use a method to assist:
node = graph.nodes.get(tag['id']) # As in get_by_id()
except KeyError:
node = Node(tag['id'])
# graph.nodes behaves like an OrderedDict, but also like a list,
# so we can append.
graph.nodes.append(node)
# I'm not sure about this. Maybe it's best if graph.nodes behaves
# like a list.

# node.attributes is an OrderedDict, so .update takes a list of tuples:
node.attributes.update([
('name', tag['name']),
('description', tag['description']),
('weight', tag['weight']),
])

# Create Edges:
for tag in tag_list:
for parent_id in tag.get('parents', []):
try:
edge = graph.edges.get(parent_id, tag['id'])
except KeyError:
edge = Edge(parent_id, tag['id'])
graph.edges.push(edge)

edge.attributes['rel_type'] = 'parents'
```
### Less Python Magic:

``` python
from simplegexf import Gexf

gexf = simplegexf.Gexf('/path/to/file.gexf')

try:
graph = gexf.graphs[0]
except KeyError:
graph = gexf.create_graph()

graph.define_attributes([
('name', 'string'),
('description', 'string'),
('weight', 'integer'),
])

graph.define_attributes([
('rel_type', 'string'),
], _class='edge')

tag_list = [
{
'id': 'c6fa996d-8593-484d-b067-69bc828bbeba',
'name': 'Test Tag',
'description': "Fairy penguins citylink, east brunswick club trams",
'weight': 13,
'parents': [], # List of UUIDs
}
...
]

# Create Nodes:
for tag in tag_list:
try:
node = graph.get_node(tag['id'])
except KeyError:
node = graph.create_node(tag['id'])

# even...
node = graph.get_or_create_node(tag['id'])

node.attributes.update([
('name', tag['name']),
('description', tag['description']),
('weight', tag['weight']),
])

# Create Edges:
for tag in tag_list:
for parent_id in tag.get('parents', []):
try:
edge = graph.get_edge(parent_id, tag['id'])
except KeyError:
edge = graph.create_edge(parent_id, tag['id'])

# even...
edge = graph.get_or_create_edge(parent_id, tag['id'])

edge.attributes['rel_type'] = 'parents'
```

Contributor guide

No contributing guide indexed for this repository

Research direction

No files or tests are named. Start by reviewing the existing simplegexf public API and compare it with the two example designs; clarify whether the project should favor class-based or explicit helper methods before defining a scoped restructuring plan and acceptance criteria.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.