Skip to content

Commit

Permalink
Merge pull request #33 from aarhusstadsarkiv/dev-matca
Browse files Browse the repository at this point in the history
Version 1.1.6
  • Loading branch information
clausjuhl authored May 29, 2024
2 parents d1d5bb7 + f095c74 commit c49e0bd
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 219 deletions.
2 changes: 1 addition & 1 deletion acacore/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.5"
__version__ = "1.1.6"
49 changes: 47 additions & 2 deletions acacore/database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,43 @@ def fetchall(self, model: Optional[Type[M]] = None) -> Generator[Union[dict[str,
model.model_validate(
{c.alias or c.name: c.from_entry(v) for c, v in zip(select_columns, vs)},
)
for vs in self.cursor.fetchall()
for vs in self.cursor
)

return ({c.alias or c.name: c.from_entry(v) for c, v in zip(select_columns, vs)} for vs in self.cursor)

@overload
def fetchmany(self, size: int) -> Generator[dict[str, Any], None, None]:
...

@overload
def fetchmany(self, size: int, model: Type[M]) -> Generator[M, None, None]:
...

def fetchmany(self, size: int, model: Optional[Type[M]] = None) -> Generator[Union[dict[str, Any], M], None, None]:
"""
Fetch `size` results from the cursor and return them as dicts, with the columns' names/aliases used as keys.
Args:
size: The amount of results to fetch.
model: Optionally, a pydantic.BaseModel class to use instead of a dict.
Returns:
Generator: A generator for converted dicts (or models).
"""
select_columns: list[SelectColumn] = [SelectColumn.from_column(c) for c in self.columns]

if model:
return (
model.model_validate(
{c.alias or c.name: c.from_entry(v) for c, v in zip(select_columns, vs)},
)
for vs in self.cursor.fetchmany(size)
)

return (
{c.alias or c.name: c.from_entry(v) for c, v in zip(select_columns, vs)} for vs in self.cursor.fetchall()
{c.alias or c.name: c.from_entry(v) for c, v in zip(select_columns, vs)}
for vs in self.cursor.fetchmany(size)
)

@overload
Expand Down Expand Up @@ -179,6 +211,19 @@ def fetchall(self, model: Optional[Type[M]] = None) -> Generator[M, None, None]:
"""
return super().fetchall(model or self.model)

def fetchmany(self, size: int, model: Optional[Type[M]] = None) -> Generator[Union[dict[str, Any], M], None, None]:
"""
Fetch `size` results from the cursor and return them as model objects.
Args:
size: The amount of results to fetch.
model: Optionally, a different pydantic.BaseModel class to use instead of the one in the ModelCursor.
Returns:
Generator: A generator for converted objects.
"""
return super().fetchmany(size, model or self.model)

def fetchone(self, model: Optional[Type[M]] = None) -> Optional[M]:
"""
Fetch one result from the cursor and return it as model object.
Expand Down
4 changes: 2 additions & 2 deletions acacore/database/files_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ def __init__(
self.history,
HistoryEntryPath,
select_columns=[
SelectColumn("Files.relative_path", str, "relative_path"),
SelectColumn("F.relative_path", str, "relative_path"),
*model_to_columns(HistoryEntry),
],
joins=["left join Files on History.UUID = Files.uuid"],
joins=[f"left join {self.files.name} F on {self.files.name}.UUID = F.uuid"],
)
self.identification_warnings = self.create_view(
"_IdentificationWarnings",
Expand Down
4 changes: 2 additions & 2 deletions acacore/reference_files/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

download_url: str = "https://github.com/aarhusstadsarkiv/reference-files/releases/latest/download/"
actions_file: str = "fileformats.yml"
custom_signatures_file: str = "custom_signatures.json"
custom_signatures_file: str = "custom_signatures.yml"


@lru_cache
Expand All @@ -30,7 +30,7 @@ def _get_custom_signatures(url: str) -> list[CustomSignature]:
if response.getcode() != 200:
raise HTTPError(url, response.getcode(), "", response.headers, response)

return TypeAdapter(list[CustomSignature]).validate_json(response.read())
return TypeAdapter(list[CustomSignature]).validate_python(load(response.read(), Loader))


def get_actions(use_cache: bool = True) -> dict[str, Action]:
Expand Down
4 changes: 2 additions & 2 deletions acacore/siegfried/siegfried.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def best_match(self) -> Optional[SiegfriedMatch]:
A SiegfriedMatch object or None if there are no known matches.
"""
matches: list[SiegfriedMatch] = [m for m in self.matches if m.id]
matches.sort(key=SiegfriedMatch.sort_tuple)
matches.sort(key=lambda m: m.sort_tuple())
return matches[-1] if matches else None

def best_matches(self) -> list[SiegfriedMatch]:
Expand All @@ -212,7 +212,7 @@ def best_matches(self) -> list[SiegfriedMatch]:
Returns:
A list of SiegfriedMatch objects.
"""
return sorted([m for m in self.matches if m.id], key=SiegfriedMatch.sort_tuple, reverse=True)
return sorted([m for m in self.matches if m.id], key=lambda m: m.sort_tuple(), reverse=True)


class SiegfriedResult(BaseModel):
Expand Down
Loading

0 comments on commit c49e0bd

Please sign in to comment.