列表批处理支持

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什么会导致程序在它似乎拥有的监视器上被阻止?   java Android studio设置视图的背景色   java我可以保存一个文本文件而不给用户修改它的能力吗?   pdfbox PDFBOX2。0:java堆堆栈错误   java是维护和操作AllowList的有效方法   JAVAsql。SQLException:找不到适合jdbc的驱动程序:mysql://localhost:3306/asd性爱   如何使用java。lang.NullPointerException:void 安卓。支持v7。应用程序。ActionBar。setElevation(float)“”在空对象引用上'   java调试空指针异常   java正则表达式,以按令牌的特定匹配项拆分,同时忽略其他匹配项   java为JPanel设置边框上的笔划   并发@Schedule方法的java行为   如何在Java中使用泛型与语言运算符和泛型类扩展数   java Rhino Javascript如何为异常堆栈跟踪标记字符串源   运行可执行jar时发生java错误,无法找到或加载主类