小编And*_*zov的帖子

Python 动态类型提示

我希望能够提供一个带有类型元组的函数,然后将其用于反序列化从事务中返回的数据。如果事务成功,该函数将返回这些类型的实例。例如:

T = TypeVar('T')

class Base:
    @classmethod
    def deserialize(cls: Type[T], bts: bytes) -> T:
        return cls(**json.loads(bts))

    @classmethod
    def transaction(cls, *types):
        items = self.db.transact([t.generate_load_operation() for t in types])
        items = [t.deserialize(item) for item in items]

        # how do I type-hint transaction, so that it would imply that
        # it will always return a tuple (or a list) of instances of classes 
        # contained in variable types?
        return items

class A(Base):
    pass

class B(Base):
    pass

a_inst, b_inst = Base.transaction(A, B)
Run Code Online (Sandbox Code Playgroud)

我应该如何注释事务,以便类型检查器可以正确推断从它返回的值的类型?

dynamic-typing python-3.x

11
推荐指数
1
解决办法
3090
查看次数

GeoIP表连接MySQL中的IP表

我有一个问题,找到一个快速的方式加入表看起来像这样:

mysql> explain geo_ip;
+--------------+------------------+------+-----+---------+-------+
| Field        | Type             | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| ip_start     | varchar(32)      | NO   |     | ""      |       |
| ip_end       | varchar(32)      | NO   |     | ""      |       |
| ip_num_start | int(64) unsigned | NO   | PRI | 0       |       |
| ip_num_end   | int(64) unsigned | NO   |     | 0       |       |
| country_code | varchar(3)       | NO   |     | ""      |       |
| country_name | …
Run Code Online (Sandbox Code Playgroud)

mysql join geoip

6
推荐指数
1
解决办法
2996
查看次数

Python 重写子类中的类方法

我正在编写一个简单的 ORM,它需要我重写子类中的方法。根据上下文,我要么期望通过类方法访问模型,要么通过实例方法访问模型,因此我不能简单地重写它们。我相信这段代码很好地描述了这个问题:

class A:
    @classmethod
    def key_fn(cls, id):
        raise NotImplementedError('')

    @classmethod
    def load_all(cls):
        yield from db_fetch_prefix(cls.key_fn('')):

class B(A):
    @classmethod
    def key_fn(cls, id):
        return f'/keys/{id}'

# how do I make sure B.key_fn is called here?
B.load_all()
Run Code Online (Sandbox Code Playgroud)

python-3.6

5
推荐指数
1
解决办法
9815
查看次数

标签 统计

dynamic-typing ×1

geoip ×1

join ×1

mysql ×1

python-3.6 ×1

python-3.x ×1