Я пытаюсь использовать 'mixin' Unique Object в моем проекте SQL Alchemy. Тем не менее, я явно делаю что-то неправильно, потому что мое приложение разбивается со следующей ошибкой Python.Crash: метод, кажется, принимает 3 аргумента, но только два переданы
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 588, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/home/user/proj/dir/a/pipelines.py", line 26, in process_item
deck = self.extract_deck(item['md'], item['sb'], current_session)
File "/home/user/proj/dir/a/pipelines.py", line 62, in extract_deck
card = Card.as_unique(session, name=name)
File "/home/user/proj/dir/unique_mixin.py", line 39, in as_unique
arg, kw)
File "/home/user/proj/dir/unique_mixin.py", line 9, in _unique
key = (cls, hashfunc(*arg, **kw))
TypeError: unique_hash() takes exactly 3 arguments (2 given)
Похоже я передаю меньше параметров, чем ожидалось, к методу unique_hash
... но я не знаю точно, где проблема.
Вот код, который я использую для уникальности. Он определяет кеш словаря для данной модели SQLAlchemy.
def _unique(session, cls, hashfunc, queryfunc, constructor, arg, kw):
cache = getattr(session, '_unique_cache', None)
if cache is None:
session._unique_cache = cache = {}
key = (cls, hashfunc(*arg, **kw))
if key in cache:
return cache[key]
else:
with session.no_autoflush:
q = session.query(cls)
q = queryfunc(q, *arg, **kw)
obj = q.first()
if not obj:
obj = constructor(*arg, **kw)
session.add(obj)
cache[key] = obj
return obj
class UniqueMixin(object):
@classmethod
def unique_hash(cls, *arg, **kw):
raise NotImplementedError()
@classmethod
def unique_filter(cls, query, *arg, **kw):
raise NotImplementedError()
@classmethod
def as_unique(cls, session, *arg, **kw):
return _unique(session,
cls,
cls.unique_hash,
cls.unique_filter,
cls,
arg, kw)
Вот как мой Card модель наследует функциональность.
class Card(UniqueMixin, Base):
__tablename__ = 'cards'
id = Column(Integer, primary_key=True)
name = Column(String(50), unique=True)
cards_deck = relationship("DeckCardCount", back_populates='card', cascade='all, delete-orphan')
# For uniqueness
@classmethod
def unique_hash(cls, query, name):
return name
@classmethod
def unique_filter(cls, query, name):
return query.filter(Clard.name == name)
Вот как я пытаюсь запросить уникальную Карту.
name = 'Some Unique Name'
card = Card.as_unique(session, name=name)