工具模块

用以提供全局通用工具方法&类

class mohand.utils.MohandDict[源代码]

基类:dict

允许通过查找/赋值属性来操作键值的字典子类

举个栗子:

>>> m = MohandDict({'foo': 'bar'})
>>> m.foo
'bar'
>>> m.foo = 'not bar'
>>> m['foo']
'not bar'

MohandDict 对象还提供了一个 .first() 方法,起功能类似 .get() ,但接受多个键名作为列表多参,并返回第一个命中的键名的值 再举个栗子:

>>> m = MohandDict({'foo': 'bar', 'biz': 'baz'})
>>> m.first('wrong', 'incorrect', 'foo', 'biz')
'bar'
__getattribute__(key)[源代码]

重载获取属性方法,使字典支持点语法取值

__setattr__(key, value)[源代码]

重载设置属性方法,使字典支持点语法赋值

first(*names)[源代码]

返回列表key在字典中第一个不为空的值

__weakref__

list of weak references to the object (if defined)

class mohand.utils.Singleton[源代码]

基类:type

单例类实现(线程安全)

注解

参考实现 singleton

static __new__(mcs, *args, **kwargs)[源代码]

元类msc通过__new__组建类对象,其中msc指Singleton

参数:
  • args (list) – 可以包含类构建所需要三元素, 类名父类命名空间, 其中命名空间中 __qualname__ 和函数的 __qualname__ 均含有 classname 做为前缀,在这里,如果想替换类名,需要把以上全部替换才可以。
  • kwargs (dict) – 可以自定义传递一些参数
返回:

返回类对象,通过super(Singleton, mcs).__new__此时已经组装好了类

返回类型:

class

__call__(*args, **kwargs)[源代码]

在调用时在线程锁中实现单例实例化