-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from spapa013/main
Bug fixes, feature additions
- Loading branch information
Showing
5 changed files
with
61 additions
and
449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,68 @@ | ||
""" | ||
Motif tables for DataJointPlus | ||
Base motif classes | ||
""" | ||
from .logging import getLogger | ||
import inspect | ||
from .base import Base | ||
from datajoint.expression import Projection | ||
from .utils import classproperty | ||
|
||
from datajoint_plus.user_tables import UserTable | ||
from datajoint_plus.utils import classproperty | ||
import datajoint as dj | ||
logger = getLogger(__name__) | ||
|
||
|
||
class Motif(UserTable): | ||
@classmethod | ||
def is_motif_table(cls): | ||
return issubclass(Motif) | ||
|
||
class StrToTable: | ||
""" | ||
Abstract class -1 | ||
""" | ||
def __init__(self, **kwargs): | ||
if isinstance(self.table, Base): | ||
self.base_table = self.table.__class__() | ||
|
||
elif isinstance(self.table, Projection): | ||
self.base_table = self.table._arg.__class__() | ||
|
||
else: | ||
msg = f'Unable to instantiate table of type {self.table.__class__.__qualname__}.' | ||
logger.error(msg) | ||
raise NotImplementedError(msg) | ||
|
||
@property | ||
def table(self): | ||
try: | ||
table = eval(self._table, self.declaration_context) | ||
if inspect.isclass(table): | ||
return table() | ||
else: | ||
return table | ||
except: | ||
msg = f'Unable to instantiate {self.__class__.__qualname__}.' | ||
logger.exception(msg) | ||
raise NotImplementedError(msg) | ||
|
||
def __repr__(self): | ||
return f'{self.__class__.__qualname__}({self._table})' | ||
|
||
def __call__(self): | ||
return self.table | ||
|
||
class MotifMaster(Motif): | ||
@classproperty | ||
def definition(cls): | ||
return f""" | ||
#{cls().class_name} | ||
{cls.hash_name} : varchar({cls.hash_len}) # hash | ||
""" | ||
|
||
class Motif: | ||
""" | ||
Class level: -2 | ||
""" | ||
|
||
class Nested(Motif): | ||
@classmethod | ||
def is_nested(cls): | ||
return issubclass(cls, Nested) | ||
def _init_validation(cls, **kwargs): | ||
if (cls.hash_name is None) and (cls.lookup_name is None): | ||
raise NotImplementedError('Subclasses of Motif must implement "lookup_name" or "hash_name".') | ||
|
||
def delete_from_master(self): | ||
# with dj.conn().transaction: | ||
keys = self.fetch('KEY') | ||
(self.master & keys).delete() | ||
|
||
def delete_quick_from_master(self): | ||
# with dj.conn().transaction: | ||
keys = self.fetch('KEY') | ||
self.delete_quick() | ||
(self.master & keys).delete_quick() | ||
|
||
def delete_quick(self, delete_from_master=True): | ||
if delete_from_master: | ||
self.delete_quick_from_master() | ||
else: | ||
super().delete_quick() | ||
|
||
def delete(self, delete_from_master=True): | ||
if delete_from_master: | ||
self.delete_from_master() | ||
else: | ||
super().delete() | ||
|
||
def drop(self, force=False): | ||
if force: | ||
super(UserTable, self).drop() | ||
else: | ||
raise dj.DataJointError('Cannot drop a Part directly. Delete from master instead') | ||
@classproperty | ||
def lookup_name(cls): | ||
return cls.hash_name if cls.hash_name is not None else logger.error('"lookup_name" not defined.') | ||
|
||
def drop_quick(self, force=False): | ||
if force: | ||
return super(UserTable, self).drop_quick() | ||
else: | ||
raise dj.DataJointError('Cannot drop a Part directly. Delete from master instead') | ||
@classmethod | ||
def lookup_as(cls, table): | ||
return cls.proj(..., **{table.lookup_name: cls.lookup_name}) | ||
|
||
|
||
|
Oops, something went wrong.