Beispiel #1
0
def visit_integer_list_count_elements_clause(element, compiler, **kwargs):
    expr = case({
        element.operand == None: None,
        element.operand == '': 0
    },
                else_=char_length(element.operand) - 1 -
                char_length(func.replace(element.operand, ',', '')))
    return compiler.process(expr)
Beispiel #2
0
def generate(crawler):
    """Output the list of pages without a title."""
    session = Session()
    links = session.query(Link).filter_by(is_page=True, is_internal=True)
    links = links.filter((char_length(Link.title) == 0)
                         | (Link.title == None)).order_by(Link.url)
    render(__outputfile__, crawler=crawler, title=__title__, links=links)
    session.close()
Beispiel #3
0
def generate(crawler):
    """Output the list of pages without a title."""
    session = Session()
    links = session.query(Link).filter_by(is_page=True, is_internal=True)
    links = links.filter((char_length(Link.title) == 0) |
                         (Link.title == None)).order_by(Link.url)
    render(__outputfile__, crawler=crawler, title=__title__,
           links=links)
    session.close()
Beispiel #4
0
def postprocess(crawler):
    """Add page problems for all pages without a title."""
    session = Session()
    # get all internal pages without a title
    links = session.query(Link).filter_by(is_page=True, is_internal=True)
    links = links.filter((char_length(Link.title) == 0) | (Link.title == None))
    for link in links:
        link.add_pageproblem('missing title')
    session.commit()
    session.close()
def get_random_fortune(db):
    s = select(
        [fortunes.c.id, fortunes.c.body]
    ).where(
        char_length(fortunes.c.body) <= 128
    ).order_by(
        random()
    ).limit(1)

    return db.execute(s).fetchone()
Beispiel #6
0
def postprocess(crawler):
    """Add page problems for all pages without a title."""
    session = Session()
    # get all internal pages without a title
    links = session.query(Link).filter_by(is_page=True, is_internal=True)
    links = links.filter((char_length(Link.title) == 0) |
                         (Link.title == None))
    for link in links:
        link.add_pageproblem('missing title')
    session.commit()
    session.close()
def get_fortune_body_by_id(db, fortune_id):
    s = select(
        [fortunes.c.body]
    ).where(
        and_(
            fortunes.c.id == fortune_id,
            char_length(fortunes.c.body) <= 128
        )
    )

    result = db.execute(s).fetchone()

    if result != None:
        return result[0]
    else:
        return None
Beispiel #8
0
def matchlistandupdate():
    try:
        session = connectDBgetSesion()
        # getstandardicddict
        # sql = "SELECT icd,word_desc FROM icd_desc ORDER BY LENGTH(word_desc) DESC"
        standardicddict = session.query(
            Icd_desc.icd, Icd_desc.word_desc).order_by(
                desc(char_length(Icd_desc.word_desc))).all()

        # getcustomicddict
        # sql = "SELECT icd,word_desc FROM custom_icd_desc ORDER BY LENGTH(word_desc) DESC"
        customicddict = session.query(
            Custom_icd_desc.icd, Custom_icd_desc.word_desc).order_by(
                desc(char_length(Custom_icd_desc.word_desc))).all()

        # getthediagnosis
        # sql = "SELECT id,diagnosis_desc FROM diagnosis"
        thediagnosis = session.query(DiagnosisNoRepeat.id,
                                     DiagnosisNoRepeat.diagnosis_desc).all()
        thediagnosisquery = session.query(DiagnosisNoRepeat)
        standardic = []
        customicd = []

        dict = {"可能": "A", "待排": "B", "疑似": "C"}
        begin_time = time.time()
        # i=0
        for id, diagnosis in thediagnosis:
            # i+=1
            # print(id, diagnosis)
            if len(diagnosis) < 1:
                continue

            subtyle = str_change(diagnosis,
                                 dict,
                                 repl_mode=0,
                                 mode=0,
                                 cut_str=1,
                                 duplicate=0,
                                 final_result=1)
            # print('subtyle:' + subtyle)
            thediagnosisquery.filter(DiagnosisNoRepeat.id == id).update(
                {DiagnosisNoRepeat.subtyle: subtyle})

            for icd, word_desc in standardicddict:
                # print(word_desc)
                if len(diagnosis) < 1:
                    break
                if str(diagnosis).__contains__(str(word_desc)):
                    # standardic.append(word_desc)
                    standardic.append(icd)
                    diagnosis = str(diagnosis).replace(word_desc, '')
                    continue
            for icd, word_desc in customicddict:
                # print(word_desc)
                if len(diagnosis) < 1:
                    break
                if str(diagnosis).__contains__(str(word_desc)):
                    # customicd.append(word_desc)
                    customicd.append(icd)
                    diagnosis = str(diagnosis).replace(word_desc, '')
                    continue

            if standardic.__len__() > 0 or customicd.__len__() > 0:
                # sql = "UPDATE diagnosisnorepeat SET standardicd=%s,customicd=%s WHERE id = %s"
                # cursor.execute(sql, [','.join(standardic), ','.join(customicd), id])
                thediagnosisquery.filter(DiagnosisNoRepeat.id == id).update({
                    DiagnosisNoRepeat.standardicd:
                    ','.join(standardic),
                    DiagnosisNoRepeat.customicd:
                    ','.join(customicd)
                })
                # print(standardic)
                # print(customicd)

            standardic = []
            customicd = []

        begin_time2 = time.time()
        # print('time:',(begin_time2-begin_time))
        # 0.017 time: 293.01399993896484 time: 282.0129997730255time: 281.6879999637604  time: 327.47899985313416time: 316.5099997520447

    except Exception as e:
        print(e)
        return None
    finally:
        close(session)
        setDiagnosissNoRepeatDesc()
        resetDiagnosis()
Beispiel #9
0
class Move(db.Model):
    """This object contain general move information."""
    __tablename__ = 'move'
    #: move's hash
    id = db.Column(db.String, primary_key=True)
    #: move's block id. if the move isn't confirmed yet, this will be null
    block_id = db.Column(db.Integer,
                         db.ForeignKey('block.id'),
                         nullable=True,
                         index=True)
    #: move's block
    block = db.relationship('Block', uselist=False, backref='moves')
    #: 33 bytes long form (i.e., compressed from) of user's public key.
    user_public_key = db.Column(db.LargeBinary, nullable=False, index=True)
    #: user's address ("0x"-prefixed 40 hexdecimal string; total 42 chars)
    user_address = db.Column(db.String, nullable=False, index=True)
    #: move's signature (71 bytes)
    signature = db.Column(db.LargeBinary, nullable=False)
    #: move name
    name = db.Column(db.String, nullable=False, index=True)
    #: move details. it contains parameters of move
    details = association_proxy(
        'move_details',
        'value',
        creator=lambda k, v: MoveDetail(key=k, value=v))
    #: move tax (not implemented yet)
    tax = db.Column(db.BigInteger, default=0, nullable=False)
    #: move creation datetime.
    created_at = db.Column(db.DateTime,
                           nullable=False,
                           default=datetime.datetime.utcnow)

    __table_args__ = (
        db.CheckConstraint(
            db.func.lower(user_address).like('0x%') &
            (char_length(user_address) == 42)
            # TODO: it should has proper test if 40-hex string
        ),
        db.CheckConstraint(char_length(user_public_key) == 33),
        db.CheckConstraint((char_length(signature) >= 68)
                           & (char_length(signature) <= 71)),
    )
    __mapper_args__ = {
        'polymorphic_identity': 'move',
        'polymorphic_on': name,
    }

    @classmethod
    def deserialize(cls, serialized: dict, block_id=None) -> 'Move':
        if block_id is None and serialized.get('block'):
            block_id = serialized['block'].get('id')
        return cls(
            id=serialized['id'],
            user_address=serialized['user_address'],
            name=serialized['name'],
            user_public_key=bytes.fromhex(serialized['user_public_key']),
            signature=bytes.fromhex(serialized['signature']),
            tax=serialized['tax'],
            details=serialized['details'],
            created_at=datetime.datetime.strptime(serialized['created_at'],
                                                  '%Y-%m-%d %H:%M:%S.%f'),
            block_id=block_id,
        )

    @property
    def valid(self):
        """Check if this object is valid or not"""
        if not self.signature:
            return False

        assert isinstance(self.signature, bytes)
        assert 68 <= len(self.signature) <= 71
        assert isinstance(self.user_public_key, bytes)
        assert len(self.user_public_key) == 33
        assert isinstance(self.user_address, str)
        assert re.match(r'^(?:0[xX])?[0-9a-fA-F]{40}$', self.user_address)
        public_key = PublicKey(self.user_public_key)
        verified = public_key.verify(
            self.signature,
            self.serialize(include_signature=False),
        )
        if not verified:
            return False

        if get_address(public_key) != self.user_address:
            return False

        return self.id == self.hash

    @property
    def confirmed(self):
        """Check if this object is confirmed or not"""
        return self.block and self.block.hash is not None

    def serialize(self,
                  use_bencode=True,
                  include_signature=False,
                  include_id=False,
                  include_block=False):
        """
        This function serialize block.

        :param       use_bencode: check if you want to encode using bencode.
        :param include_signature: check if you want to include signature.
        :param        include_id: check if you want to include linked moves.
        :param     include_block: check if you want to include block.
        """
        binary = (lambda x: x) if use_bencode else bytes.hex
        serialized = dict(
            user_address=self.user_address,
            name=self.name,
            details={k: str(v)
                     for k, v in dict(self.details).items()},
            tax=self.tax,
            created_at=str(self.created_at),
        )
        if include_signature:
            serialized.update(
                signature=binary(self.signature),
                user_public_key=binary(self.user_public_key),
            )
        if include_id:
            serialized['id'] = self.id
        if include_block:
            if self.block:
                serialized['block'] = self.block.serialize(False)
            else:
                serialized['block'] = None
        if use_bencode:
            serialized = bencode(serialized)
        return serialized

    def broadcast(self, sent_node=None, my_node=None, session=db.session):
        """
        It broadcast this move to every nodes you know.

       :param       sent_node: sent :class:`nekoyume.models.Node`.
                               this node ignore sent node.
       :param         my_node: my :class:`nekoyume.models.Node`.
                               received node ignore my node when they
                               broadcast received object.
        """
        Node.broadcast(Node.post_move_endpoint,
                       self.serialize(False, True, True), sent_node, my_node,
                       session)

    @property
    def hash(self) -> str:
        """ Get move hash """
        return hashlib.sha256(
            self.serialize(include_signature=True)).hexdigest()

    def get_randoms(self) -> list:
        """ get random numbers by :doc:`Hash random <white_paper>` """
        if not (self.block and self.block.hash and self.id):
            return []
        result = [ord(a) ^ ord(b) for a, b in zip(self.block.hash, self.id)]
        result = result[int(self.block.difficulty / 4):]
        return result

    def roll(self, randoms: list, dice: str, combine=True):
        """
        Roll dices based on given randoms

            >>> from nekoyume.models import Move
            >>> move = Move()
            >>> move.roll([1, 7, 3], '2d6')
            6

        :params randoms: random numbers from
                         :func:`nekoyume.models.Move.get_randoms`
        :params    dice: dice to roll (e.g. 2d6)
        :params combine: return combined result or not if rolling it multiple.
        """
        result = []
        if dice.find('+') > 0:
            dice, plus = dice.split('+')
            plus = int(plus)
        else:
            plus = 0
        cnt, dice_type = (int(i) for i in dice.split('d'))
        for i in range(cnt):
            try:
                result.append(randoms.pop() % dice_type + 1)
            except IndexError:
                raise OutOfRandomError
        if combine:
            return sum(result) + plus
        else:
            return result
Beispiel #10
0
 class A2(self.Base):
     id = Column(Integer, primary_key=True)
     data = Column(String)
     expr = column_property(data+' '+data)
     func = column_property(char_length(data))