跳转到主要内容

愚蠢Python模块集合

项目描述

stupidity.py - 愚蠢Python代码的工具包

Build Status

安装

pip install 愚蠢性

switch语句

你是否怀念Python中的经典switch语句?

from stupidity import switch

for case in switch('b'):
    if case('a'):
        print('this is not printed')
    if case('b'):
        print('this is printed')
    if case('c'):
        print('fallthrough is also supported')
        break
    if case('d'):
        print('this is not printed either')
    print('default value at bottom, but this is not reached')

认为if语句愚蠢?我们为你解决了这个问题!

from stupidity import switch

for case in switch('b'):
    with case('a'):
        print('this is not printed')
    with case('b'):
        print('this is printed')
    with case('c'):
        print('fallthrough is also supported')
        break
    with case('d'):
        print('this is not printed either')
    print('default value at bottom, but this is not reached')

认为for循环也愚蠢?我们也可以去除它们

from stupidity import switch

with switch('b') as case:
    with case('a'):
        print('this is not printed')
    with case('b'):
        print('this is printed')
    with case('c'):
        print('fallthrough is also supported')
        BREAK  # NOTE: We have to yell here
    with case('d'):
        print('this is not printed either')
    print('default value at bottom, but this is not reached')

处理闭包变量

你是否对那些似乎就在你触手可及之外,但让你烦恼的闭包内部的变量感到烦恼?不必再担心了!

from stupidity import replace_closure_variables

def foo(): 
    x = 42 
    def f(): 
        return x 
    return f 

f = foo()
f()  # 42

replace_closure_variables(f, x=123)
f()  # 123

花括号

众所周知,从__future__导入braces会失败得很惨

from __future__ import braces
#   File "<stdin>", line 1
# SyntaxError: not a chance

幸运的是,我们提供了一个可行的实现

from stupidity import braces

x = 42;
if (x == 123): {
    print('nope')
}
elif (x == 42): {
    print('yep')
}
# prints 'yep'

项目详情


下载文件

下载适用于您的平台文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

stupidity-0.0.1.tar.gz (4.5 kB 查看哈希值)

上传时间:

由以下支持