Python之namedtuple源码分析

namedtuple()函数根据提供的参数创建一个新类,这个类会有一个类名,一些字段名和一个可选的用于定义类行为的关键字,具体实现如下

namedtuple函数源码

from keyword import iskeyword as _iskeyword
import sys as _sys

import logging
logging.basicConfig(level=logging.INFO, filename="logging.txt", filemode="w+",                     format = ‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
logger = logging.getLogger(__name__)

_class_template = """from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict

class {typename}(tuple):
    ‘{typename}({arg_list})‘

    __slots__ = ()

    _fields = {field_names!r}

    def __new__(_cls, {arg_list}):
        ‘Create new instance of {typename}({arg_list})‘
        return _tuple.__new__(_cls, ({arg_list}))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        ‘Make a new {typename} object from a sequence or iterable‘
        result = new(cls, iterable)
        if len(result) != {num_fields:d}:
            raise TypeError(‘Expected {num_fields:d} arguments, got %d‘ % len(result))
        return result

    def _replace(_self, **kwds):
        ‘Return a new {typename} object replacing specified fields with new values‘
        result = _self._make(map(kwds.pop, {field_names!r}, _self))
        if kwds:
            raise ValueError(‘Got unexpected field names: %r‘ % list(kwds))
        return result

    def __repr__(self):
        ‘Return a nicely formatted representation string‘
        return self.__class__.__name__ + ‘({repr_fmt})‘ % self

    def _asdict(self):
        ‘Return a new OrderedDict which maps field names to their values.‘
        return OrderedDict(zip(self._fields, self))

    def __getnewargs__(self):
        ‘Return self as a plain tuple.  Used by copy and pickle.‘
        return tuple(self)

{field_defs}
"""

_repr_template = ‘{name}=%r‘

_field_template = ‘‘‘    {name} = _property(_itemgetter({index:d}), doc=‘Alias for field number {index:d}‘)
‘‘‘

def namedtuple(typename, field_names, *, verbose=False, rename=False, module=None):
    """Returns a new subclass of tuple with named fields.

    >>> Point = namedtuple(‘Point‘, [‘x‘, ‘y‘])
    >>> Point.__doc__                   # docstring for the new class
    ‘Point(x, y)‘
    >>> p = Point(11, y=22)             # instantiate with positional args or keywords
    >>> p[0] + p[1]                     # indexable like a plain tuple
    33
    >>> x, y = p                        # unpack like a regular tuple
    >>> x, y
    (11, 22)
    >>> p.x + p.y                       # fields also accessible by name
    33
    >>> d = p._asdict()                 # convert to a dictionary
    >>> d[‘x‘]
    11
    >>> Point(**d)                      # convert from a dictionary
    Point(x=11, y=22)
    >>> p._replace(x=100)               # _replace() is like str.replace() but targets named fields
    Point(x=100, y=22)

    """

    # Validate the field names.  At the user‘s option, either generate an error
    # message or automatically replace the field name with a valid name.
    if isinstance(field_names, str):
        field_names = field_names.replace(‘,‘, ‘ ‘).split()
    field_names = list(map(str, field_names))
    typename = str(typename)
    logging.info("%s:   %s" %(typename, field_names))
    if rename:
        seen = set()
        for index, name in enumerate(field_names):
            if (not name.isidentifier()
                or _iskeyword(name)
                or name.startswith(‘_‘)
                or name in seen):
                field_names[index] = ‘_%d‘ % index
            seen.add(name)
    for name in [typename] + field_names:
        logging.info(name)
        if type(name) is not str:
            raise TypeError(‘Type names and field names must be strings‘)
        #判断是否为标识符,标识符必须以字母或者“_”开头
        #标识符用于作为变量,函数名、类名、方法名等
        if not name.isidentifier():
            raise ValueError(‘Type names and field names must be valid ‘
                             ‘identifiers: %r‘ % name)
        #判断是否为关键字,关键字为python内部已经使用了的标识符
        if _iskeyword(name):
            raise ValueError(‘Type names and field names cannot be a ‘
                             ‘keyword: %r‘ % name)
    seen = set()
    for name in field_names:
        if name.startswith(‘_‘) and not rename:
            raise ValueError(‘Field names cannot start with an underscore: ‘
                             ‘%r‘ % name)
        if name in seen:
            raise ValueError(‘Encountered duplicate field name: %r‘ % name)
        seen.add(name)

    # Fill-in the class template
    class_definition = _class_template.format(
        typename = typename,
        field_names = tuple(field_names),
        num_fields = len(field_names),
        arg_list = repr(tuple(field_names)).replace("‘", "")[1:-1],
        repr_fmt = ‘, ‘.join(_repr_template.format(name=name)
                             for name in field_names),
        field_defs = ‘\n‘.join(_field_template.format(index=index, name=name)
                               for index, name in enumerate(field_names))
    )
    logging.info(class_definition)

    # Execute the template string in a temporary namespace and support
    # tracing utilities by setting a value for frame.f_globals[‘__name__‘]
    namespace = dict(__name__=‘namedtuple_%s‘ % typename)
    exec(class_definition, namespace)
    result = namespace[typename]
    result._source = class_definition
    if verbose:
        print(result._source)

    # For pickling to work, the __module__ variable needs to be set to the frame
    # where the named tuple is created.  Bypass this step in environments where
    # sys._getframe is not defined (Jython for example) or sys._getframe is not
    # defined for arguments greater than 0 (IronPython), or where the user has
    # specified a particular module.
    if module is None:
        try:
            module = _sys._getframe(1).f_globals.get(‘__name__‘, ‘__main__‘)
        except (AttributeError, ValueError):
            pass
    if module is not None:
        result.__module__ = module

    return result

  通过函数模板字符串_class_template.format()会生成我们需要的实例类:

  eg: people = namedtuple("person","name,age,sex") 

class person(tuple) 分析

#coding=utf-8

from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict

import logging
logging.basicConfig(level=logging.INFO, filename="logging.txt", filemode="w+",                     format = ‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
logger = logging.getLogger(__name__)

class person(tuple):
    ‘person(name, age, sex)‘

    __slots__ = ()

    _fields = (‘name‘, ‘age‘, ‘sex‘)

    def __new__(_cls, name, age, sex):
        ‘Create new instance of person(name, age, sex)‘
        logger.info("__new__")
        return _tuple.__new__(_cls, (name, age, sex))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        ‘Make a new person object from a sequence or iterable‘
        result = new(cls, iterable)
        if len(result) != 3:
            raise TypeError(‘Expected 3 arguments, got %d‘ % len(result))
        return result

    def _replace(_self, **kwds):
        ‘Return a new person object replacing specified fields with new values‘

        #需要深刻理解该代码的精髓
        logger.info(type(_self))
        for item in _self:
            logger.info(item)
        str = ‘‘‘
            how to replace dict_keyvalue
            li = map({"age":99}.pop, (‘name‘, ‘age‘, ‘sex‘), ("zhanglin", "11", "man"))
        ‘‘‘
        logger.info(str)
        result = _self._make(map(kwds.pop, (‘name‘, ‘age‘, ‘sex‘), _self))
        if kwds:
            raise ValueError(‘Got unexpected field names: %r‘ % list(kwds))
        return result

    def __repr__(self):
        ‘Return a nicely formatted representation string‘
        return self.__class__.__name__ + ‘(name=%r, age=%r, sex=%r)‘ % self

    def _asdict(self):
        ‘Return a new OrderedDict which maps field names to their values.‘
        return OrderedDict(zip(self._fields, self)) #打包为元组列表

    def __getnewargs__(self):
        ‘Return self as a plain tuple.  Used by copy and pickle.‘
        return tuple(self)

    name = _property(_itemgetter(0), doc=‘Alias for field number 0‘)

    age = _property(_itemgetter(1), doc=‘Alias for field number 1‘)

    sex = _property(_itemgetter(2), doc=‘Alias for field number 2‘)

if __name__ == "__main__":

    p1 = person("zhanglin", "30", "man")
    logger.info("{0}:{1}".format("p1", p1))
    #_replace验证
    p2 = p1._replace(name ="zhangsan", age=99)
    logger.info(p2)
    logger.info("{0}:{1}".format("p2", p2))
    

测试结果:

2018-03-21 15:10:46,197 - __main__ - INFO - __new__
2018-03-21 15:10:46,197 - __main__ - INFO - p1:person(name=‘zhanglin‘, age=‘30‘, sex=‘man‘)
2018-03-21 15:10:46,197 - __main__ - INFO - <class ‘__main__.person‘>
2018-03-21 15:10:46,197 - __main__ - INFO - zhanglin
2018-03-21 15:10:46,197 - __main__ - INFO - 30
2018-03-21 15:10:46,197 - __main__ - INFO - man
2018-03-21 15:10:46,197 - __main__ - INFO -
            how to replace dict_keyvalue
            li = map({"age":99}.pop, (‘name‘, ‘age‘, ‘sex‘), ("zhanglin", "11", "man"))

2018-03-21 15:10:46,197 - __main__ - INFO - person(name=‘zhangsan‘, age=99, sex=‘man‘)
2018-03-21 15:10:46,197 - __main__ - INFO - p2:person(name=‘zhangsan‘, age=99, sex=‘man‘)

原文地址:https://www.cnblogs.com/xiaobingqianrui/p/8617272.html

时间: 2024-10-04 22:42:48

Python之namedtuple源码分析的相关文章

K-近邻算法的Python实现 : 源码分析

网上介绍K-近邻算法的例子很多,其Python实现版本基本都是来自于机器学习的入门书籍<机器学习实战>,虽然K-近邻算法本身很简单,但很多初学者对其Python版本的源代码理解不够,所以本文将对其源代码进行分析. 什么是K-近邻算法? 简单的说,K-近邻算法采用不同特征值之间的距离方法进行分类.所以它是一个分类算法. 优点:无数据输入假定,对异常值不敏感 缺点:复杂度高 好了,直接先上代码,等会在分析:(这份代码来自<机器学习实战>) def classify0(inx, data

Python 进阶之源码分析:如何将一个类方法变为多个方法?

前一篇文章<Python 中如何实现参数化测试?>中,我提到了在 Python 中实现参数化测试的几个库,并留下一个问题: 它们是如何做到把一个方法变成多个方法,并且将每个方法与相应的参数绑定起来的呢? 我们再提炼一下,原问题等于是:在一个类中,如何使用装饰器把一个类方法变成多个类方法(或者产生类似的效果)? # 带有一个方法的测试类 class TestClass: def test_func(self): pass # 使用装饰器,生成多个类方法 class TestClass: def

python int的源码分析

在intobject.h中找到整数的定义 Python的整数对象的数据实际保存在ob_ival中,是c中的long类型.PyObject_HEAD就应该是整数对象的描述了.从源码的描述中知道PyObject_HEAD定义了整数对象的初始化信息 其中typedef struct _typeobject{}PyTypeObject,PyTypeObject可以被认为对象的最通用,最原始的类,被其他整数,字符串等对象继承使用. PyAPI_DATA(PyTypeObject)  PyInt_Type;

python列表部分源码分析

1 def append(self, p_object): # real signature unknown; restored from __doc__ 2 """ L.append(object) -- append object to end """ 3 pass #1.练习(添加元素): li = [11,22,33,"aabc","cddg",234,"alex",] prin

Python编程_Selenium2Library源码分析

[Package] keywords 所有关键字封装 [Class] _waiting.py 等待函数的条件判断, 替换sleep, 在依次以条件逻辑执行较多用例时可以有效节省执行等待时间, 快速定位问题 _wait_until(self, timeout, error, function, *args) 概述: 直到function(*args)为True时return None, 为False则return error, 超时时间timeout 参数: error: 初始化为超时异常 fun

Python字典部分源码分析,字典是无序的

1 def clear(self): # real signature unknown; restored from __doc__ 2 """ D.clear() -> None. Remove all items from D. """ 3 pass 1 #练习1.清空字典(置空) 2 li = {"key1":"value1","key2":"value2"

Python之美[从菜鸟到高手]--浅拷贝、深拷贝完全解读(copy源码分析)

可悲的我一直以为copy模块是用C写的,有时候需要深入了解deepcopy,文档描述的实在太简单,还是不知所云. 比如说最近看sqlmap源码中AttribDict的_deepcopy__有些疑惑, def __deepcopy__(self, memo): retVal = self.__class__() memo[id(self)] = retVal for attr in dir(self): if not attr.startswith('_'): value = getattr(se

zg手册 之 python2.7.7源码分析(5)-- python的作用域和名空间

在 python 中, module,作用域,名空间这几个概念与虚拟机的运行机制有紧密的联系, 这里先了解 module,作用域,和名空间,为后面分析虚拟机的运行做准备. module 在python中一个文件对应是一个module,每个py文件被导入后都对应一个module对象. 这个对象包含有一个dict对象,保存着本py文件中对应的变量和函数的引用, 也保存从其他python文件(module)导入的变量或函数的引用. 名空间和作用域 python 有三个独立的名空间, local, gl

Python:线程、进程与协程(3)——Queue模块及源码分析

Queue模块是提供队列操作的模块,队列是线程间最常用的交换数据的形式.该模块提供了三种队列: Queue.Queue(maxsize):先进先出,maxsize是队列的大小,其值为非正数时为无线循环队列 Queue.LifoQueue(maxsize):后进先出,相当于栈 Queue.PriorityQueue(maxsize):优先级队列. 其中LifoQueue,PriorityQueue是Queue的子类.三者拥有以下共同的方法: qsize():返回近似的队列大小.为什么要加"近似&q