第10章--序列的修改、散列和切片

第10章–序列的修改、散列和切片

这一章主要是通过一个例子–Vector类的实现来讲解序列的序列化、哈希化、格式化、和获取修改类属性的一些方法与技巧,下面是书本的源代码,我在代码的对应函数前面用###加了注释,并在最后列出了一些重要的知识点,我觉得这样有利于对书中概念的理解

# BEGIN VECTOR_V5
"""
A multi-dimensional ``Vector`` class, take 5


A ``Vector`` is built from an iterable of numbers::

    >>> Vector([3.1, 4.2])
    Vector([3.1, 4.2])
    >>> Vector((3, 4, 5))
    Vector([3.0, 4.0, 5.0])
    >>> Vector(range(10))
    Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...])



Tests with 2-dimensions (same results as ``vector2d_v1.py``)::

    >>> v1 = Vector([3, 4])
    >>> x, y = v1
    >>> x, y
    (3.0, 4.0)
    >>> v1
    Vector([3.0, 4.0])
    >>> v1_clone = eval(repr(v1))
    >>> v1 == v1_clone
    True
    >>> print(v1)
    (3.0, 4.0)
    >>> octets = bytes(v1)
    >>> octets
    b'd\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
    >>> abs(v1)
    5.0
    >>> bool(v1), bool(Vector([0, 0]))
    (True, False)


Test of ``.frombytes()`` class method:

    >>> v1_clone = Vector.frombytes(bytes(v1))
    >>> v1_clone
    Vector([3.0, 4.0])
    >>> v1 == v1_clone
    True


Tests with 3-dimensions::

    >>> v1 = Vector([3, 4, 5])
    >>> x, y, z = v1
    >>> x, y, z
    (3.0, 4.0, 5.0)
    >>> v1
    Vector([3.0, 4.0, 5.0])
    >>> v1_clone = eval(repr(v1))
    >>> v1 == v1_clone
    True
    >>> print(v1)
    (3.0, 4.0, 5.0)
    >>> abs(v1)  # doctest:+ELLIPSIS
    7.071067811...
    >>> bool(v1), bool(Vector([0, 0, 0]))
    (True, False)


Tests with many dimensions::

    >>> v7 = Vector(range(7))
    >>> v7
    Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
    >>> abs(v7)  # doctest:+ELLIPSIS
    9.53939201...


Test of ``.__bytes__`` and ``.frombytes()`` methods::

    >>> v1 = Vector([3, 4, 5])
    >>> v1_clone = Vector.frombytes(bytes(v1))
    >>> v1_clone
    Vector([3.0, 4.0, 5.0])
    >>> v1 == v1_clone
    True


Tests of sequence behavior::

    >>> v1 = Vector([3, 4, 5])
    >>> len(v1)
    3
    >>> v1[0], v1[len(v1)-1], v1[-1]
    (3.0, 5.0, 5.0)


Test of slicing::

    >>> v7 = Vector(range(7))
    >>> v7[-1]
    6.0
    >>> v7[1:4]
    Vector([1.0, 2.0, 3.0])
    >>> v7[-1:]
    Vector([6.0])
    >>> v7[1,2]
    Traceback (most recent call last):
      ...
    TypeError: Vector indices must be integers


Tests of dynamic attribute access::

    >>> v7 = Vector(range(10))
    >>> v7.x
    0.0
    >>> v7.y, v7.z, v7.t
    (1.0, 2.0, 3.0)

Dynamic attribute lookup failures::

    >>> v7.k
    Traceback (most recent call last):
      ...
    AttributeError: 'Vector' object has no attribute 'k'
    >>> v3 = Vector(range(3))
    >>> v3.t
    Traceback (most recent call last):
      ...
    AttributeError: 'Vector' object has no attribute 't'
    >>> v3.spam
    Traceback (most recent call last):
      ...
    AttributeError: 'Vector' object has no attribute 'spam'


Tests of hashing::

    >>> v1 = Vector([3, 4])
    >>> v2 = Vector([3.1, 4.2])
    >>> v3 = Vector([3, 4, 5])
    >>> v6 = Vector(range(6))
    >>> hash(v1), hash(v3), hash(v6)
    (7, 2, 1)


Most hash values of non-integers vary from a 32-bit to 64-bit CPython build::

    >>> import sys
    >>> hash(v2) == (384307168202284039 if sys.maxsize > 2**32 else 357915986)
    True


Tests of ``format()`` with Cartesian coordinates in 2D::

    >>> v1 = Vector([3, 4])
    >>> format(v1)
    '(3.0, 4.0)'
    >>> format(v1, '.2f')
    '(3.00, 4.00)'
    >>> format(v1, '.3e')
    '(3.000e+00, 4.000e+00)'


Tests of ``format()`` with Cartesian coordinates in 3D and 7D::

    >>> v3 = Vector([3, 4, 5])
    >>> format(v3)
    '(3.0, 4.0, 5.0)'
    >>> format(Vector(range(7)))
    '(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0)'


Tests of ``format()`` with spherical coordinates in 2D, 3D and 4D::

    >>> format(Vector([1, 1]), 'h')  # doctest:+ELLIPSIS
    '<1.414213..., 0.785398...>'
    >>> format(Vector([1, 1]), '.3eh')
    '<1.414e+00, 7.854e-01>'
    >>> format(Vector([1, 1]), '0.5fh')
    '<1.41421, 0.78540>'
    >>> format(Vector([1, 1, 1]), 'h')  # doctest:+ELLIPSIS
    '<1.73205..., 0.95531..., 0.78539...>'
    >>> format(Vector([2, 2, 2]), '.3eh')
    '<3.464e+00, 9.553e-01, 7.854e-01>'
    >>> format(Vector([0, 0, 0]), '0.5fh')
    '<0.00000, 0.00000, 0.00000>'
    >>> format(Vector([-1, -1, -1, -1]), 'h')  # doctest:+ELLIPSIS
    '<2.0, 2.09439..., 2.18627..., 3.92699...>'
    >>> format(Vector([2, 2, 2, 2]), '.3eh')
    '<4.000e+00, 1.047e+00, 9.553e-01, 7.854e-01>'
    >>> format(Vector([0, 1, 0, 0]), '0.5fh')
    '<1.00000, 1.57080, 0.00000, 0.00000>'
"""

from array import array
import reprlib
import math
import numbers
import functools
import operator
import itertools  # <1>


class Vector:
    typecode = 'd'

    ### 接受可迭代的对象作为构造参数,而不是用*args传入多个参数
    
    def __init__(self, components):
        self._components = array(self.typecode, components)
    
    
    ### 让Verctor实现可迭代化
    
    def __iter__(self):
        return iter(self._components)


    ### 使用 reprlib.repr() 函数获取 self._components 的有限长度表示形式(如 array('d',[0.0, 1.0, 2.0, 3.0, 4.0, ...]))
    
    def __repr__(self):
        components = reprlib.repr(self._components)
        components = components[components.find('['):-1]
        return 'Vector({})'.format(components)

    def __str__(self):
        return str(tuple(self))

    def __bytes__(self):
        return (bytes([ord(self.typecode)]) +
                bytes(self._components))

    ### 用zip模块方便了两个对象中不同向量的并行迭代
    
    def __eq__(self, other):
        return (len(self) == len(other) and
                all(a == b for a, b in zip(self, other)))

    ### 用functools.reduce和operator.xor实现对每一维值的哈希值进行异或操作,设置默认值为0
    
    def __hash__(self):
        hashes = (hash(x) for x in self)
        return functools.reduce(operator.xor, hashes, 0)

    def __abs__(self):
        return math.sqrt(sum(x * x for x in self))

    def __bool__(self):
        return bool(abs(self))

    def __len__(self):
        return len(self._components)

    ### 让Vercor实现切片操作,即vector[1] vector[3:4]这种,同时用isinstance判断index是否为slice类型,如果是的话用获得的多个维度值重新构造一个新的Vector并返回,如果index是整数类型,则直接返回数值
    ### numbers.Integral,这是一个抽象基类(Abstract Base Class,ABC)。在 isinstance 中使用抽象基类做测试能让 API 更灵活且更容易更新,

    
    def __getitem__(self, index):
        cls = type(self)
        if isinstance(index, slice):
            return cls(self._components[index])
        elif isinstance(index, numbers.Integral):
            return self._components[index]
        else:
            msg = '{.__name__} indices must be integers'
            raise TypeError(msg.format(cls))

    shortcut_names = 'xyzt'
    
    ### 实现用vector.x vector.y等来读向量的前几个维度值,只实现getattr的话只有读功能,如果此时vector.x=10,不会实现通过x来改变vector的第一个维度值,需要同时实现setattr才可以

    def __getattr__(self, name):
        cls = type(self)
        if len(name) == 1:
            pos = cls.shortcut_names.find(name)
            if 0 <= pos < len(self._components):
                return self._components[pos]
        msg = '{.__name__!r} object has no attribute {!r}'
        raise AttributeError(msg.format(cls, name))
        
     ### 通过setattr实现了用vector.x vector.y vector.z vector.t来修改vectoir的前几个维度值
        
    def __setattr__(self, name, value):
        cls = type(self)
        if len(name) == 1:
            if name in cls.shortcut_names:
                error = 'readonly attribute {attr_name!r}'
            elif name.islower():
                error = "can't set attributes 'a' to 'z' in {cls_name!r}"
            else:
                error = ''
            if error:
                msg = error.format(cls_name=cls.__name__, attr_name=name)
                raise AttributeError(msg)
        super().__setattr__(name, value)

    def angle(self, n):  # <2>
        r = math.sqrt(sum(x * x for x in self[n:]))
        a = math.atan2(r, self[n-1])
        if (n == len(self) - 1) and (self[-1] < 0):
            return math.pi * 2 - a
        else:
            return a
            


    def angles(self):  # <3>
        return (self.angle(n) for n in range(1, len(self)))

    ### 自定义format函数中h参数的表示
    
    def __format__(self, fmt_spec=''):
        if fmt_spec.endswith('h'):  # hyperspherical coordinates
            fmt_spec = fmt_spec[:-1]
            coords = itertools.chain([abs(self)],
                                     self.angles())  # <4>
            outer_fmt = '<{}>'  # <5>
        else:
            coords = self
            outer_fmt = '({})'  # <6>
        components = (format(c, fmt_spec) for c in coords)  # <7>
        return outer_fmt.format(', '.join(components))  # <8>

    @classmethod
    def frombytes(cls, octets):
        typecode = chr(octets[0])
        memv = memoryview(octets[1:]).cast(typecode)
        return cls(memv)
# END VECTOR_V5

知识点总结

  • reprlib.repr 这个函数用于生成大型结构或递归结构的安全表示形式,它会限制输出字符串的长度,用 ‘…’ 表示截断的部分 https://docs.python.org/3.1/library/reprlib.html
  • slice类 __getitem__(self,index)中如果是访问单个元素,比如vector[1]那么index就是整数类型,如果是切片访问多个元素,比如vector[1:3]那么index就是slice类型,slice的indices方法用于对slice中的负数参数和缺省值进行处理,让参数变为整数和补充缺省值
    https://docs.python.org/3/c-api/slice.html
>>> slice                                                    #表明slice是内置类
<class 'slice'>
>>> s=slice(2,5)                                           # slice的构造参数有三个start,end,step,其中start,可以省略,只传一个参数的话默认是end,start,step默认为None(实际序列中表现为start=0 step=1) 传两个参数的话默认step为None
>>> s
slice(2, 5, None)
>>> l=list(range(11))                                    # 构造0-10的列表
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> l[s]                                                    # 把slice实例传进去,输出跟l[2:5]一样
[2, 3, 4]
>>> l[2:5]
[2, 3, 4]
>>> s=slice(-2,5,-1)                                    
>>> s.indices(len(l))                                    # indices方法对slice中的负数参数和缺省值进行处理,让参数变为整数和补充缺省值
(9, 5, -1)
>>> l[s]
[9, 8, 7, 6]
>>> l[9:5:-1]
[9, 8, 7, 6]
  • 多个哈希值进行合并规约建议用异或操作
  • operator 模块以函数的形式提供了 Python 的全部中缀运算符,从而减少使用 lambda 表达式。
  • zip 函数生成一个由元组构成的生成器,元组中的元素来自参数传入的各个可迭代对象。如果传入的各个可迭代对象长短不一,则在处理完最短的可迭代对象后就会结束,不会报错,如果想要按最长的可迭代对象来处理可以用itertools.zip_longest函数,参数fillvalue会填充缺失的值
>>> a=list(range(5))
>>> b=list(range(7))
>>> c=list(range(10))
>>> for x,y,z in zip(a,b,c):
...     print(x,y,z)
...
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
>>> from itertools import zip_longest
>>> for x,y,z in zip_longest(a,b,c):
...     print(x,y,z)
...
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
None 5 5
None 6 6
None None 7
None None 8
None None 9

  • format相关 扩展格式规范微语言(https://docs.python.org/3/library/string.html#formatspec)时,最好避免重用内置类型支持的格式代码。这里对微语言的扩展还会用到浮点数的格式代码 ‘eEfFgGn%’,而且保持原意,因此绝对要避免重用代码。整数使用的格式代码有’bcdoxXn’,字符串使用的是 ‘s’。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值