Skip to content

Commit

Permalink
Simplifications to "archive" results format
Browse files Browse the repository at this point in the history
  • Loading branch information
jlumpe committed Jul 14, 2024
1 parent 48ca065 commit a96c47d
Showing 1 changed file with 5 additions and 26 deletions.
31 changes: 5 additions & 26 deletions src/gambit/results/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@
from typing import Union, IO, Any
from functools import singledispatchmethod

from attr import attrs, attrib, asdict, has as has_attrs
from attr import attrs, has as has_attrs
from sqlalchemy.orm import Session

from gambit.query import QueryResultItem, QueryResults
from gambit.classify import ClassifierResult, GenomeMatch
from gambit.classify import GenomeMatch
from gambit.db import ReferenceGenomeSet, Taxon, AnnotatedGenome, Genome
import gambit.util.json as gjson
from gambit.util.io import FilePath, maybe_open
from gambit.util.misc import type_singledispatchmethod
from gambit.util.typing import is_optional, unwrap_optional
from .base import asdict_default, BaseJSONResultsExporter


def _todict(obj, attrs):
return {a: getattr(obj, a) for a in attrs}
from .base import BaseJSONResultsExporter, _todict


@attrs()
Expand All @@ -29,29 +25,12 @@ class ResultsArchiveWriter(BaseJSONResultsExporter):
The exported data can be read and converted back into an identical :class:`QueryResults`
object using :class:`.ResultsArchiveReader`.
Attributes
----------
install_info
Add results of :func:`gambit.util.dev.install_info` to the ``QueryResults.extra`` dict.
Only the ID attributes of database models are saved, when loading the saved results the models
are recreated by database queries.
"""
install_info: bool = attrib(default=False)

to_json = singledispatchmethod(BaseJSONResultsExporter.to_json)

to_json.register(ClassifierResult, asdict_default)
to_json.register(GenomeMatch, asdict_default)
to_json.register(QueryResultItem, asdict_default)

@to_json.register(QueryResults)
def _queryresults_to_json(self, results):
data = asdict(results)

if self.install_info:
from gambit.util.dev import install_info
data['extra']['install_info'] = install_info()

return data

@to_json.register(ReferenceGenomeSet)
def _genomeset_to_json(self, gset: ReferenceGenomeSet):
return _todict(gset, ['key', 'version'])
Expand Down

0 comments on commit a96c47d

Please sign in to comment.