Skip to content

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
pveigadecamargo committed Dec 16, 2023
1 parent 533b992 commit 9c9d181
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 27 deletions.
29 changes: 13 additions & 16 deletions aequilibrae/project/network/period.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .safe_class import SafeClass
from .connector_creation import connector_creation
from ...utils.db_utils import commit_and_close
from ...utils.spatialite_utils import connect_spatialite


class Period(SafeClass):
Expand Down Expand Up @@ -33,18 +34,15 @@ def __init__(self, dataset, project):

def save(self):
"""Saves period to database"""
conn = self.connect_db()

if self.period_id != self.__original__["period_id"]:
raise ValueError("One cannot change the period_id")
with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn:
if self.period_id != self.__original__["period_id"]:
raise ValueError("One cannot change the period_id")

data, sql = self.__save_period()
data, sql = self.__save_period()

if data:
conn.execute(sql, data)

conn.commit()
conn.close()
if data:
conn.execute(sql, data)

def data_fields(self) -> list:
"""Lists all data fields for the period, as available in the database
Expand Down Expand Up @@ -73,12 +71,11 @@ def renumber(self, new_id: int):
self._logger.warning("This is already the period number")
return

conn = self.connect_db()
try:
conn.execute("Update periods set period_id=? where period_id=?", [new_id, self.period_id])
finally:
conn.commit()
conn.close()
with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn:
try:
conn.execute("Update periods set period_id=? where period_id=?", [new_id, self.period_id])
finally:
conn.commit()
self._logger.info(f"Period {self.period_id} was renumbered to {new_id}")
self.__dict__["period_id"] = new_id
self.__original__["period_id"] = new_id
Expand Down
5 changes: 2 additions & 3 deletions aequilibrae/project/network/periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def new_period(self, period_id: int, start: int, end: int, description: str = No

with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn:
dt = conn.execute("SELECT COUNT(*) FROM periods WHERE period_id=?", [period_id]).fetchone()[0]
if dt[0] > 0:
if dt > 0:
raise Exception("period_id already exists. Failed to create it")

data = {key: None for key in self.__fields}
Expand All @@ -127,8 +127,7 @@ def data(self) -> pd.DataFrame:
:Returns:
**table** (:obj:`DataFrame`): Pandas DataFrame with all the periods
"""
with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn:
dl = DataLoader(conn, "periods")
dl = DataLoader(self.project.path_to_file, "periods")
return dl.load_table()

def __del__(self):
Expand Down
1 change: 0 additions & 1 deletion tests/aequilibrae/matrix/test_aequilibraeMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ def test_get_matrix(self):

a = AequilibraeMatrix()
a.load(self.name_test)
print(np.array_equal(a.get_matrix("seed"), a.matrix["seed"]))

del a

Expand Down
1 change: 0 additions & 1 deletion tests/aequilibrae/paths/test_conical.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,3 @@ def test_conial_funtion(self):
# We check if the analytical solution matches the numerical differentiation
dydx = (congested_times[1] - congested_times[0]) / dx
self.assertAlmostEqual(dydx, delta[1], 6, "Problems with derivative for the conical vdf")
print(dydx, delta[1])
2 changes: 0 additions & 2 deletions tests/aequilibrae/paths/test_inrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def test_inrets_funtion(self):
link_flows.fill(1 * 0.1001 * i)

link_flows += np.arange(11) * dx
print(link_flows)
inrets(congested_times, link_flows, capacity, fftime, alpha, beta, cores)
delta_inrets(delta, link_flows, capacity, fftime, alpha, beta, cores)

Expand All @@ -57,4 +56,3 @@ def test_inrets_funtion(self):
for j in range(10):
dydx = (congested_times[j + 1] - congested_times[j]) / dx
self.assertAlmostEqual(dydx, delta[j + 1], 6, "Problems with derivative for the inrets vdf")
print(j)
5 changes: 1 addition & 4 deletions tests/aequilibrae/project/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ def setUp(self) -> None:
self.project = Project()
self.project.open(self.proj_dir)
self.network = self.project.network
self.curr = self.project.conn.cursor()

for num in range(2, 6):
self.project.network.periods.new_period(num, num, num, "test")

def tearDown(self) -> None:
self.curr.close()
self.project.close()
try:
rmtree(self.proj_dir)
Expand Down Expand Up @@ -73,8 +71,7 @@ def test_data_fields(self):
period = periods.get(1)

fields = sorted(period.data_fields())
self.curr.execute("pragma table_info(periods)")
dt = self.curr.fetchall()
dt = self.project.conn.execute("pragma table_info(periods)").fetchall()

actual_fields = sorted([x[1] for x in dt if x[1] != "ogc_fid"])

Expand Down

0 comments on commit 9c9d181

Please sign in to comment.