列表批处理支持

z3c.batching的Python项目详细描述


详细文档

内容

  • 更改
  • 简单的批处理

    此模块实现了一个简单的批处理机制,允许您拆分 大序列分成小批量。我们先创建一个简单的列表, 这将是我们的完整序列:

    空根上的批处理:

    < Buff行情>
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    >>> batch[0]
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    >>> batch.next is None
    True
    
    >>> batch.previous is None
    True
    
    >>> sequence = ['one', 'two', 'three', 'four', 'five', 'six', 'seven',
    ...             'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen']
    

    我们现在可以为这个序列创建一个批处理。让我们的批量大小为3:

    < Buff行情>
    >>> batch = Batch(sequence, size=3)
    

    批处理的第一个参数始终是完整序列。如果没有开始 元素被指定,批处理从第一个元素开始:

    < Buff行情>
    >>> list(batch)
    ['one', 'two', 'three']
    

    开始索引通常在构造函数中指定,但是:

    < Buff行情>
    >>> batch = Batch(sequence, start=6, size=3)
    >>> list(batch)
    ['seven', 'eight', 'nine']
    

    注意,开始是一个索引,从零开始。如果开始索引是 大于序列的最大索引时,将引发索引错误:

    < Buff行情>
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    0

    批处理实现了有限序列接口,因此支持 标准方法。例如,您可以询问批次的长度:

    < Buff行情>
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    1

    注意,长度返回批的实际大小,而不是我们要求的大小 对于:

    < Buff行情>
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    2

    与任何序列一样,非空批处理在布尔上下文中也是真的。

    < Buff行情>
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    3

    您还可以按索引获取一个元素,该元素与批处理相关:

    < Buff行情>
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    4

    切片:

    < Buff行情>
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    5
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    6
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    7
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    8
    >>> from z3c.batching.batch import Batch
    >>> batch = Batch([], size=3)
    >>> len(batch)
    0
    >>> bool(batch)
    False
    >>> batch.firstElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    9

    如果您要求的索引超出范围,则会产生索引错误:

    < Buff行情>
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    0

    您还可以遍历批:

    < Buff行情>
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    1

    批处理还实现了一些ireadsequence接口:

    < Buff行情>
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    2
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    3
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    4
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    5
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    6

    除了所有这些常见的api方法之外,还有一些属性 让你的生活更简单。指定开始和大小:

    < Buff行情>
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    7

    立即计算批次的结束索引:

    < Buff行情>
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    8

    ui通常要求批处理的数量和 计算批次:

    < Buff行情>
    >>> batch.lastElement
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    9

    您还可以要求下一批:

    < Buff行情>
    >>> batch[0]
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    0

    如果当前批次是最后一批,则下一批为无:

    < Buff行情>
    >>> batch[0]
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    1

    前一批显示前一批:

    < Buff行情>
    >>> batch[0]
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    2

    如果当前批是第一批,则前一批为无:

    < Buff行情>
    >>> batch[0]
    Traceback (most recent call last):
    ...
    IndexError: ...
    
    3

    最后两个属性处理ba中的元素嘘。他们要求 批处理的第一个和最后一个元素:

    <阻塞率> 啊! 啊!

    总批次:

    <阻塞率> 啊!

    我们可以访问所有批次:

    <阻塞率> AAAAAAA 37 AAAAAAAAA 38 啊! 啊! AAAAAAA 41 AAAAAAA 42

    切片:

    <阻塞率> 啊! 啊! 啊!45! 啊! 啊! AAAAAAA 48

    大型批次列表的批次邻域

    当批次的完整列表太大而无法在用户界面中显示时, 我们只想显示所有批次的一个子集。 为此提供了一个助手函数:

    首先建立一个大的批序列(或任何其他序列):

    <阻塞率> AAAAAAA 49

    然后只提取第一个和最后一个项目,以及 第46项(索引=45)。我们要三个邻居在左边,五个在右边:

    <阻塞率> 啊!

    "none"可用于在用户界面中显示分隔符(请参见z3c.table)

    子集批处理 <阻塞率> 啊!

    有时(出于性能原因),即使用户需要 对于批处理用户界面,我们希望将计算限制在 实际显示给用户的值的子集。

    因为我们用数据子集初始化批处理,所以我们还 需要明确提供完整数据集的长度。

    让我们创建数据的子集:

    AAAAAAA 52

    我们将其用作较长数据集的一部分:

    AAAAAAA 53

    完整的API检查:

    啊!

    上面已经看到,连续的批处理是 EmptyBatch 类。由于这些实例不包含数据,因此我们会引发错误,以确保没有批处理提供程序尝试显示项数据:

    啊!

    欢迎加入QQ群-->: 979659372 Python中文网_新手群

    推荐PyPI第三方库


    热门话题
    java如何使用从PreparedStatement返回的RowId。getGeneratedKeys()?   selenium chrome驱动程序中下一个网站url的java空白页   java如何将参数化匿名类转换为lambda?   java JUnit在AfterClass上获取测试结果   java将动态XML/JSON内容与静态标记化负载进行比较,并检索标记值   java共享一个需要数据持久性的项目[数据库]   java在调用方法时获取意外的参数类型。getParameterTypes()   java如何用jdbc在swing中用另一个字段替换外键?   需要java Jersey Tomcat CDI依赖项解释   java如何生成UML图   java如何编写Jersey rest服务可以通过给定的spring代码访问   SpringMaven存储库管理器Nexus与Java依赖项的Artifactory   java将包从另一个项目导入eclipse中的当前项目   加密Java使用密码加密文件