python表达式中堆栈使用的工具
stackfull的Python项目详细描述
python stackfull
这个小实用程序包提供了要使用的函数 在提供功能的python表达式内部 类似于基于堆栈的语言 (例如forth和postscript)。
它在当前 running frame,这是一个简单的python列表- 呼叫普通的“推,弹出,重复,重发”等… 将只推/恢复列表中的元素。
其目的是无论何时 一个值很难检索—(或 计算广泛)-而不是 必须在前一行中检索 将所述值存储在变量中, 一个人有能力“推动”价值 在这个隐式堆栈中-并检索它 在同一表达式的另一部分中:
`result = pop().upper() if not push(get_my+expensive_value()).isdigit() else pop()`
为了方便起见,大多数函数返回传递的值 对他们来说(就像上面的推动一样)。
兼容性
从0.20版开始,并引入窗口 函数,stackfull不再与python 2.x兼容。
可用功能:
清除
Clears the stack
清除标记
Clears the stack up to the special sentinel value MARK - this allows for clean-up of the stack after a block of code, preserving the older values. If the stack is not empty, returns the last value on the stack non destructively, else, MARK itself is returned.
如果错误
Removes the last value in the stack if the expression is false. Made to be used in comprehensions, in the if part:
Ex.: [pop().name for image in values if pop_if_false(push(image) is not None)]
dup
Dplicates the last value on the stack It also returns the value duplicated in a non-destructive way
波普
Pops the last value from the stack
通俗易懂
Pops the last value from the stack, and clears the stack. This allows stackfull to be used inside generator expressions and comprehensions, using a ‘push’ in the filtering expression, and ‘popclear’ on the result expression.
推动
Pushes a value into the stack, and returns the value itself Along with ‘pop’ and ‘popclear’ this is the heart of stackfull - as it allows an expensive function to be used in a ‘if’ or ‘while’ test, and still have its value available to use inside the defined block - without the need of an explicit auxiliar variable
如果为真,则推送
Returns the value itself, and Pushes a value into the stack, if it is truthy. Otherwise does not touch the stack. Nice to use inside comprehensions in the “if” part: if the expession is not True, it is never pushed, and extraneous values don’t pile up on the stack.
Ex.: [pop().name for image in values if push_if_true(image)]
retr
Peeks the last value on the stack without consuming it
滚动
Rolls the last ‘items’ values on the stack by ‘amount’ positions, changing their order. Returns the value on the top of the stack after the changes in a non destructive way.
堆栈
Retrieves the stack as an ordinary Python list (which it actually is), allowing one to perform extra desired operations, such as ‘len’ or ‘insert’
窗口
Pre-populates a frame stack with the seed values, and then iterates over the iterable -
This allows one to use the stack with initial values in a simple way in a generator-expression context - like
fib = [push(stack()[-2] + stack()[-1]) for i in window(range(2, 10), 1,1)]
Jo_o S.O.Bueno<;gwidion@gmail.com>;
模块堆叠帮助:
- 名称
- stackfull-堆栈表达式实用程序
- 说明
这些函数在调用中使用“hidden”变量 保存堆栈的代码框架上下文(python列表) 使用值-这里的简单功能允许 有很多stack的功能 基础语言,如后记和四种 可用于任何python表达式。
(可能)todo:创建一个decorator,该decorator在这里转换操作 在python vm堆栈中的实际操作中, 通过将调用这些实用程序函数的字节码修改为 实际的python vm堆栈操作。
- 函数
- 清除()
- 清除堆栈
- 清除标记()
- 清除堆栈直到特殊的sentinel值 标记-这允许在 保留旧值的代码块。 如果堆栈不为空,则返回 以非破坏性方式堆叠,否则返回标记本身。
- dup()
- 复制堆栈上的最后一个值 它还以非破坏性方式返回重复的值
- pop()
- 弹出堆栈中的最后一个值
- popclear()
- 弹出堆栈中的最后一个值,并清除堆栈。 这允许在生成器表达式中使用stackfull 理解,在过滤表达式中使用“push”, 结果表达式上的“popclear”。
- 推(值)
- 将值推入堆栈,并返回值本身 与“pop”和“popclear”一起,这是 stackfull-因为它允许使用昂贵的函数 在“if”或“while”测试中,仍然有其值 可在定义的块内使用-不 一个显式辅助变量 的必要性
- retr()
- 查看堆栈上的最后一个值而不使用它
- 滚动(项目,金额)
- 按 “数量”位置,改变它们的顺序。 返回堆栈顶部的值 以非破坏性的方式进行更改。
- 堆栈()
- 将堆栈检索为普通的python列表 (实际上是),允许一个人表演 额外需要的操作,如“len”或“insert”
- 数据
- 标记=<;对象对象>; sn='.stackfull'
- 文件
- /home/gwidin/projetos/stackfull/stackfull.py