def find_or_create(cls, session, text, language, url, article): """ :param text: string :param language: Language (object) :param url: Url (object) :return: """ try: return cls.query.filter( cls.content_hash == text_hash(text)).filter( cls.article == article).one() except sqlalchemy.orm.exc.NoResultFound or sqlalchemy.exc.InterfaceError: try: new = cls(text, language, url, article) session.add(new) session.commit() return new except sqlalchemy.exc.IntegrityError or sqlalchemy.exc.DatabaseError: for i in range(10): try: session.rollback() t = cls.query.filter( cls.content_hash == text_hash(text)).one() print("found text after recovering from race") return t except: print("exception of second degree in find text..." + str(i)) time.sleep(0.3) continue break
def find(cls, text, language): try: query = (cls.query.filter(cls.language == language).filter( cls.content_hash == util.text_hash(text))) if query.count() > 0: query = query.filter(cls.content == text) try: return query.one() except sqlalchemy.orm.exc.NoResultFound: pass return cls(text, language) except: import traceback traceback.print_exc()
def find(cls, text, language): try: query = ( cls.query.filter(cls.language == language) .filter(cls.content_hash == util.text_hash(text)) ) if query.count() > 0: query = query.filter(cls.content == text) try: return query.one() except sqlalchemy.orm.exc.NoResultFound: pass return cls(text, language) except: import traceback traceback.print_exc()
def find_or_create(cls, text, language, url): """ :param text: string :param language: Language (object) :param url: Url (object) :return: """ try: query = ( cls.query.filter(cls.content_hash == util.text_hash(text)) # For some reason, here we can't filter by the url... ) if query.count() > 0: query = query.filter(cls.content == text) try: return query.one() except sqlalchemy.orm.exc.NoResultFound: pass return cls(text, language, url) except: import traceback traceback.print_exc()
def __init__(self, content, language, url): self.content = content self.language = language self.url = url self.content_hash = util.text_hash(content)
def __init__(self, content, language): self.content = content self.language = language self.content_hash = util.text_hash(content)
def __init__(self, content, language, url, article): self.content = content self.language = language self.url = url self.content_hash = text_hash(content) self.article = article
# script used to convert the old binary hashes # to their hex counterparts. binary can not be # insured UNIQUE by a mysql constraint from zeeguu.model import Text from zeeguu import util import zeeguu session = zeeguu.db.session texts = session.query(Text).all() for t in texts: t.content_hash = util.text_hash(t.content) session.add(t) session.commit() #input ("next?/")