Skip to content

Commit

Permalink
Manual ruff lint and format fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanconn committed Dec 17, 2024
1 parent 3c3e71a commit 3e5feae
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
10 changes: 8 additions & 2 deletions ska_dbi/DBI.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
ska_dbi provides simple methods for database access and data insertion.
Features:
- Sqlite connections are supported.
Expand All @@ -15,11 +16,13 @@

def _denumpy(x):
"""
Convert from numpy type to native python type.
Try using the numpy.tolist() to convert to native python type.
DBI's can't typically handle numpy vals."""
try:
return x.tolist()
except:
except Exception:
return x


Expand Down Expand Up @@ -150,7 +153,10 @@ def fetchone(
expr,
vals=None,
):
"""Fetch one row after executing args. This always gets the first row of the
"""
Fetch one row.
Fetch one row after executing args. This always gets the first row of the
SQL query. Use ska_dbi.fetch() to get multiple rows one at a time.
Example usage::
Expand Down
4 changes: 1 addition & 3 deletions ska_dbi/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@

class NoPasswordError(Exception):
"""
Special Error for the case when password is neither supplied nor available
from a file.
Error when password is neither supplied nor available from a file.
"""

7 changes: 5 additions & 2 deletions ska_dbi/sqsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from ska_dbi.common import DEFAULT_CONFIG, NoPasswordError

SYBASE = "/soft/SYBASE16.0"
LD_LIBRARY_PATH = "/soft/SYBASE16.0/OCS-16_0/lib:/soft/SYBASE16.0/OCS-16_0/lib3p64:/soft/SYBASE16.0/OCS-16_0/lib3p"
LD_LIBRARY_PATH = (
f"{SYBASE}/OCS-16_0/lib:{SYBASE}/OCS-16_0/lib3p64:{SYBASE}/OCS-16_0/lib3p"
)
SQSH_BIN = "/usr/local/bin/sqsh.bin"


Expand All @@ -22,7 +24,8 @@ class Sqsh(object):
:param server: Server name (default = sqlsao)
:param user: User name (default = aca_ops)
:param database: Database name (default = axafapstat)
:param sqshrc: sqshrc file (optional). Read from aspect authorization dir if required and not supplied.
:param sqshrc: sqshrc file (optional).
Read from aspect authorization dir if required and not supplied.
:param authdir: Directory containing authorization files
:rtype: Sqsh object
Expand Down
49 changes: 25 additions & 24 deletions ska_dbi/tests/test_dbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import os
import sqlite3
import tempfile

import numpy as np
Expand All @@ -29,7 +30,7 @@ def teardown_class(cls):
# fail as a result of test_55.
try:
cls.db.execute("drop table ska_dbi_test_table")
except:
except Exception:
pass
cls.db.cursor.close()
cls.db.conn.close()
Expand All @@ -46,15 +47,15 @@ def test_10_create_table(self):

def test_15_insert_data(self):
for id_ in range(3):
data = dict(
id=id_,
tstart=2.0 + id_,
tstop=3.0 + id_,
obsid=4 + id_,
pcad_mode="npnt",
aspect_mode="kalm",
sim_mode="stop",
)
data = {
"id": id_,
"tstart": 2.0 + id_,
"tstop": 3.0 + id_,
"obsid": 4 + id_,
"pcad_mode": "npnt",
"aspect_mode": "kalm",
"sim_mode": "stop",
}
self.db.insert(data, "ska_dbi_test_table")

def test_20_fetchall(self):
Expand All @@ -78,8 +79,8 @@ def test_35_fetch(self):
assert np.allclose(row["tstart"], 2.0 + i)

def test_40_fetch_null(self):
for row in self.db.fetch("select * from ska_dbi_test_table where id=100000"):
assert False
for _ in self.db.fetch("select * from ska_dbi_test_table where id=100000"):
raise AssertionError

def test_45_fetchone_null(self):
row = self.db.fetchone("select * from ska_dbi_test_table where id=100000")
Expand All @@ -94,31 +95,31 @@ def test_55_drop_table(self):


class TestSqliteWithNumpy(DBI_BaseTests):
db_config = dict(dbi="sqlite", numpy=True)
db_config = {"dbi": "sqlite", "numpy": True}


class TestSqliteWithoutNumpy(DBI_BaseTests):
db_config = dict(dbi="sqlite", numpy=False)
db_config = {"dbi": "sqlite", "numpy": False}


def test_context_manager():
with DBI(dbi="sqlite", server=":memory:") as db:
db.execute(TEST_TABLE_SQL)
for id_ in range(3):
data = dict(
id=id_,
tstart=2.0 + id_,
tstop=3.0 + id_,
obsid=4 + id_,
pcad_mode="npnt",
aspect_mode="kalm",
sim_mode="stop",
)
data = {
"id": id_,
"tstart": 2.0 + id_,
"tstop": 3.0 + id_,
"obsid": 4 + id_,
"pcad_mode": "npnt",
"aspect_mode": "kalm",
"sim_mode": "stop",
}
db.insert(data, "ska_dbi_test_table")
rows = db.fetchall("select * from ska_dbi_test_table")
assert len(rows) == 3
assert rows[1]["id"] == 1

# check that access fails now
with pytest.raises(Exception):
with pytest.raises(sqlite3.ProgrammingError):
rows = db.fetchall("select * from ska_dbi_test_table")
10 changes: 1 addition & 9 deletions ska_dbi/tests/test_sqsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,4 @@ def test_fetchone_axafapstat_context():
@pytest.mark.skipif("not ON_HEAD_NETWORK", reason="Test only runs on HEAD network")
def test_no_passwd():
with pytest.raises(NoPasswordError):
s = Sqsh(server="sqlsao", user="aca_nonexistent", database="axafapstat")


@pytest.mark.skipif("not ON_HEAD_NETWORK", reason="Test only runs on HEAD network")
def test_fetch_axafapstat_lines():
s = Sqsh()
query = "select * from aspect_1 where obsid=5438"
lines = s.fetch(query)
assert len(lines) >= 5
Sqsh(server="sqlsao", user="aca_nonexistent", database="axafapstat")

0 comments on commit 3e5feae

Please sign in to comment.