Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Jan 22, 2021
1 parent 4f91c51 commit ac50242
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
rev: stable
hooks:
- id: black
language_version: python3.8
language_version: python3.9
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ virtualenv:
matrix:
fast_finish: true
include:
- python: 3.8
env: DISTRIB="ubuntu" TOX_PYTHON_VERSION="py38" COVERAGE="true"
- env: DISTRIB="conda" PYTHON_VERSION="3.8" COVERAGE="false"
- python: 3.9
env: DISTRIB="ubuntu" TOX_PYTHON_VERSION="py39" COVERAGE="true"
- env: DISTRIB="conda" PYTHON_VERSION="3.9" COVERAGE="false"
install:
- source tests/travis_install.sh
- pip install -r requirements.txt
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[mypy]
python_version = 3.8
python_version = 3.9
warn_return_any = True
warn_unused_configs = True
4 changes: 3 additions & 1 deletion src/shillelagh/adapters/api/weatherapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@


requests_cache.install_cache(
cache_name="weatherapi_cache", backend="sqlite", expire_after=180,
cache_name="weatherapi_cache",
backend="sqlite",
expire_after=180,
)


Expand Down
16 changes: 10 additions & 6 deletions src/shillelagh/adapters/file/csvfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class RowTracker:
def __init__(self, iterable: Iterator[Row]):
self.iterable = iterable
self.law_row: Optional[Row] = None
self.last_row: Optional[Row] = None

def __iter__(self) -> Iterator[Row]:
for row in self.iterable:
Expand All @@ -46,7 +46,9 @@ def __init__(self, path: str):

self.columns = {
column_name: types[column_name](
filters=[Range], order=order[column_name], exact=True,
filters=[Range],
order=order[column_name],
exact=True,
)
for column_name in column_names
}
Expand Down Expand Up @@ -74,15 +76,17 @@ def get_data(self, bounds: Dict[str, Filter]) -> Iterator[Row]:
op = operator.ge if filter_.include_start else operator.gt
filters.append(
lambda row, value=filter_.start, i=column_index, op=op: op(
row[i], value,
row[i],
value,
),
)

if filter_.end is not None:
op = operator.le if filter_.include_end else operator.lt
filters.append(
lambda row, value=filter_.end, i=column_index, op=op: op(
row[i], value,
row[i],
value,
),
)

Expand All @@ -101,17 +105,17 @@ def insert_row(self, row: Row) -> int:
with open(self.path, "a") as fp:
writer = csv.writer(fp, quoting=csv.QUOTE_NONNUMERIC)
writer.writerow([row[column_name] for column_name in column_names])
self.num_rows += 1

# update order
for column_name, column_type in self.columns.items():
column_type.order = update_order(
current_order=column_type.order,
previous=self.last_row[column_name],
previous=self.last_row[column_name] if self.last_row else None,
current=row[column_name],
num_rows=self.num_rows,
)
self.last_row = row
self.num_rows += 1

return row_id

Expand Down
17 changes: 13 additions & 4 deletions src/shillelagh/backends/apsw/vt.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def get_create_table(self, tablename: str) -> str:
return f'CREATE TABLE "{tablename}" ({formatted_columns})'

def BestIndex(
self, constraints: List[Tuple[int, int]], orderbys: List[Tuple[int, bool]],
self,
constraints: List[Tuple[int, int]],
orderbys: List[Tuple[int, bool]],
) -> Tuple[List[Constraint], int, str, bool, int]:
column_types = list(self.adapter.get_columns().values())

Expand Down Expand Up @@ -120,7 +122,10 @@ def UpdateDeleteRow(self, rowid: int) -> None:
self.adapter.delete_row(rowid)

def UpdateChangeRow(
self, rowid: int, newrowid: int, fields: Tuple[Any, ...],
self,
rowid: int,
newrowid: int,
fields: Tuple[Any, ...],
) -> None:
column_names = ["rowid"] + list(self.adapter.get_columns().keys())
row = dict(zip(column_names, [newrowid] + list(fields)))
Expand All @@ -132,15 +137,19 @@ def __init__(self, adapter: Adapter):
self.adapter = adapter

def Filter(
self, indexnumber: int, indexname: str, constraintargs: List[Any],
self,
indexnumber: int,
indexname: str,
constraintargs: List[Any],
) -> None:
columns: Dict[str, Field] = self.adapter.get_columns()
column_names: List[str] = list(columns.keys())
indexes: List[Index] = json.loads(indexname)

all_bounds: DefaultDict[str, Set[Tuple[Operator, Any]]] = defaultdict(set)
for (column_index, sqlite_index_constraint), constraint in zip(
indexes, constraintargs,
indexes,
constraintargs,
):
operator = operator_map.get(sqlite_index_constraint)
column_name = column_names[column_index]
Expand Down
3 changes: 1 addition & 2 deletions src/shillelagh/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def check(self, value: Any) -> bool:


class Impossible(Filter):
"""Custom Filter return when impossible conditions are passed."""
"""Custom Filter returned when impossible conditions are passed."""

pass

Expand Down Expand Up @@ -110,7 +110,6 @@ def build(cls, operations: Set[Tuple[int, Any]]) -> Filter:
# update start and end by tightening up range
if start is None or new_start >= start:
if new_start == start:
print(include_start, new_include_start)
if include_start and not new_include_start:
include_start = False
else:
Expand Down
8 changes: 7 additions & 1 deletion src/shillelagh/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ def analyse(data: Iterator[Row]) -> Tuple[int, Dict[str, Order], Dict[str, Field


def update_order(
current_order: Order, previous: Any, current: Any, num_rows: int,
current_order: Order,
previous: Any,
current: Any,
num_rows: int,
) -> Order:
if num_rows < 2 or previous is None:
return Order.NONE

try:
if num_rows == 2:
return Order.ASCENDING if current >= previous else Order.DESCENDING
Expand Down
19 changes: 11 additions & 8 deletions tests/adapters/file/test_csvfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ def test_csvfile_get_data(mocker):
{"rowid": 1, "index": 11.0, "temperature": 13.1, "site": "Blacktail_Loop"},
]

assert list(
adapter.get_data(
{
"index": Range(None, 11, False, True),
"temperature": Range(14, None, False, False),
},
),
) == [{"rowid": 0, "index": 10.0, "temperature": 15.2, "site": "Diamond_St"}]
assert (
list(
adapter.get_data(
{
"index": Range(None, 11, False, True),
"temperature": Range(14, None, False, False),
},
),
)
== [{"rowid": 0, "index": 10.0, "temperature": 15.2, "site": "Diamond_St"}]
)


def test_csvfile_get_data_invalid_filter(mocker):
Expand Down

0 comments on commit ac50242

Please sign in to comment.