Skip to content

Commit f52b756

Browse files
Fix snapshot functionality by adjusting execution order
1 parent e9ec4a2 commit f52b756

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

pydatastructs/graphs/graph.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,12 @@ def __new__(cls, *args, **kwargs):
7575
cls, kwargs.get('backend', Backend.PYTHON))
7676
default_impl = args[0]._impl if args else 'adjacency_list'
7777
implementation = kwargs.get('implementation', default_impl)
78-
if implementation == 'adjacency_list':
79-
from pydatastructs.graphs.adjacency_list import AdjacencyList
80-
obj = AdjacencyList(*args)
81-
obj._impl = implementation
82-
return obj
83-
elif implementation == 'adjacency_matrix':
84-
from pydatastructs.graphs.adjacency_matrix import AdjacencyMatrix
85-
obj = AdjacencyMatrix(*args)
86-
obj._impl = implementation
87-
return obj
88-
else:
89-
raise NotImplementedError("%s implementation is not a part "
90-
"of the library currently."%(implementation))
91-
obj.snapshots = {}
78+
obj._impl = implementation
79+
obj.snapshots = {}
80+
9281
def add_snapshot(self):
9382
"""Automatically assigns timestamps using system time."""
94-
timestamp = int(time.time()) # Secure real-time timestamp
83+
timestamp = int(time.time())
9584
self.snapshots[timestamp] = copy.deepcopy(self)
9685
def get_snapshot(self, timestamp: int):
9786
"""Retrieves a past version of the graph if the timestamp exists."""
@@ -104,12 +93,26 @@ def list_snapshots(self):
10493
"""Returns all stored timestamps in sorted order."""
10594
return sorted(self.snapshots.keys())
10695

107-
# Attach functions to the object
96+
10897
obj.add_snapshot = add_snapshot.__get__(obj)
10998
obj.get_snapshot = get_snapshot.__get__(obj)
11099
obj.list_snapshots = list_snapshots.__get__(obj)
111100

112-
return obj
101+
102+
if implementation == 'adjacency_list':
103+
from pydatastructs.graphs.adjacency_list import AdjacencyList
104+
obj = AdjacencyList(*args)
105+
obj._impl = implementation
106+
return obj
107+
elif implementation == 'adjacency_matrix':
108+
from pydatastructs.graphs.adjacency_matrix import AdjacencyMatrix
109+
obj = AdjacencyMatrix(*args)
110+
obj._impl = implementation
111+
return obj
112+
else:
113+
raise NotImplementedError("%s implementation is not a part "
114+
"of the library currently."%(implementation))
115+
113116

114117
def is_adjacent(self, node1, node2):
115118
"""

0 commit comments

Comments
 (0)