描述:使用函数式编程语言特性伪糖扩展python

pseudosugar的Python项目详细描述


description:伪糖-使用函数式编程语言功能扩展python

REQUIRES: LINUX OS AND PYTHON3.1

QUICK TEST: $ python3.1 setup.py build dev –quicktest

SUMMARY: pseudosugar is a pure python module. pseudosugar is a python ast tree hack, adding the following syntax sugars:

function<<<< aa, bb, cc, … -> function(aa, bb, cc, …) aa, bb, cc, … >>>>function -> function(aa, bb, cc, …)

xx ..function(aa, bb, cc) -> function(xx, aa, bb, cc) xx …function(aa, bb, cc) -> function(aa, xx, bb, cc) xx ….function(aa, bb, cc) -> function(aa, bb, xx, cc)

最近的更改日志:
20091231-添加糖 20091224-添加了pseumethod交互控制台-改进了pseumethod导入挂钩 20091224-模块化包-修复安装问题-添加sdist检查 20091209-改进文档 20091205-将源代码移到c++ 20091116-集成包

演示用法:

>>> ## start up the interactive console
>>> from pseudosugar import *
>>> pseudo_console().interact()

Python3.1.1(R311:74480,2009年9月13日,17:17:12) [GCC 4.3.2]关于Linux2 键入“帮助”、“版权”、“信用”或“许可证”以获取详细信息。 (虚拟控制台) pseudo庘u importer-将位于0xb7ac754c>;的hook<;pseudo sugar.pseudo庘u importer对象添加到sys.meta庘u路径 >>>>来自伪糖进口*

>>> #### QUICK EXAMPLES
>>> ## prefix operator
>>> print<<<< 'hello', 'world'
hello world
>>> ## postfix operator
>>> 'hello', 'world' >>>>print
hello world
>>> ## pseudomethod
>>> def function(aa, bb, cc): return (aa, bb, cc)
>>> 1 ..function(0, 0) >>>>print
(1, 0, 0)
>>> 2 ...function(0, 0) >>>>print
(0, 2, 0)
>>> 3 ....function(0, 0) >>>>print
(0, 0, 3)
>>> ## '<<<<' CONVERTS FUNCTIONS INTO PREFIX OPERATORS
>>> ## foo<<<< turns foo into a prefix operator
>>> ## foo<<<< will take in everything to its right that is comma delimited
>>> ## print<<<< is useful for making print statements
>>> print<<<< 'bob says', 'hello ' + re.sub<<<< re.compile('(\w+)'), '\\1!', 'world'
bob says hello world!
>>> ## '>>>>' CONVERTS FUNCTIONS INTO POSTFIX OPERATORS
>>> ## it behaves almost exactly like '>>>>' except in reverse
>>> ## it is useful for chaining together multiple operators
>>> 'qwerty' >>>>list >>>>sorted >>>>enumerate >>>>dict >>>>print
{0: 'e', 1: 'q', 2: 'r', 3: 't', 4: 'w', 5: 'y'}
>>> ## OPERATOR PRECEDENCE
>>> ## '>>>>' has higher operator precedence than '<<<<'
>>> print( list<<<< 'abcd' >>>>tuple ) ## list(tuple('abcd'))
['a', 'b', 'c', 'd']
>>> #### PSEUDOMETHOD SYNTAX
>>> ## DYNAMICALLY BIND FUNCTION CALLS TO OBJECTS
>>> ## bind the function call print() to 'hello'
>>> print('hello')
hello
>>> 'hello' ..print()
hello
>>> 'hello' ..print('world')
hello world
>>> 'hello' ..print('world', '!')
hello world !
>>> 'hello' ..print('world', '!', file = sys.stdout)
hello world !
>>> ## create a string pseudomethod which adds an exclamation or other endings
>>> def add_ending(self, end = '!'): return self + end
>>> 'hello' ..add_ending() ..print()
hello!
>>> 'hello'.upper() ..add_ending() ..print()
HELLO!
>>> 'hello'.upper() ..add_ending(' world') ..print()
HELLO world
>>> 'hello'.upper() ..add_ending(' world').lower() ..print()
hello world
>>> 'hello'.upper() ..add_ending(' world').lower() ..add_ending('!') ..print()
hello world!
>>> 'hello'.upper() ..add_ending(' world').lower() ..add_ending('!') ..add_ending(end = '!') ..print()
hello world!!
>>> ## OPERATOR PRECEDENCE
>>> ## 'aa ..bb()' has the same operator precedence as the attribute operator 'a.b'
>>> def add(aa, bb): return aa + bb
>>> print( 2 * 3 ..add(4) + 5 == 2 * (3 + 4) + 5 )
True
>>> print( 3 == 1 ..add(2) )
True
>>> print( 0, 0 ..add(1), 0 )
0 1 0
>>> ## EXTEND RESTRICTED TYPES
>>> ## the python code object type <class 'code'> cannot be subtyped nor will it accept any method binding.
>>> ## however, we can extend it by dynamically binding ordinary functions.
>>> ## here's a pseudomethod which disassembles an instance of the type to a specified output
>>> import dis, io, sys
>>> def disassemble(self, file):
...   backup_stdout = sys.stdout ## backup sys.stdout
...   try:
...     sys.stdout = file
...     dis.dis(self) ## disassemble self
...     return file
...   finally:
...     sys.stdout = backup_stdout ## restore sys.stdout
>>> code_source = 'print( "hello" )'; code_object = compile(code_source, '', 'exec'); exec( code_object )
hello
>>> code_object ..disassemble(file = io.StringIO()).getvalue() ..print()
  1           0 LOAD_NAME                0 (print)
              3 LOAD_CONST               0 ('hello')
              6 CALL_FUNCTION            1
              9 POP_TOP
             10 LOAD_CONST               1 (None)
             13 RETURN_VALUE
>>> ## '...' AND '....' SYNTAX
>>> ## sometimes we instead want the 2nd or 3rd argument of a function bound to an object.
>>> ## '...' and '....' will do this respectively
>>> '2nd' ...print(0, 0)
0 2nd 0
>>> '3rd' ....print(0, 0)
0 0 3rd
>>> ## '....' is useful for chaining re.sub
>>> ss = 'file = io.StringIO(); print 1, 2, 3 >> file; print file.getvalue()'; print( ss )
file = io.StringIO(); print 1, 2, 3 >> file; print file.getvalue()
>>> print(
...   re.sub('print (.*?)$', 'print( \\1 )',
...          re.sub('print (.*) >> (.*?);', 'print( \\1, file = \\2 );', ss)
...          )
...   )
file = io.StringIO(); print( 1, 2, 3, file = file ); print( file.getvalue() )
>>> ss ....re.sub('print (.*) >> (.*?);', 'print( \\1, file = \\2 );') \
...    ....re.sub('print (.*?)$', 'print( \\1 )') \
...    ..print()
file = io.StringIO(); print( 1, 2, 3, file = file ); print( file.getvalue() )
>>> ## in fact, another primary use of pseudomethod is to flatten ugly, hard-to-read, lisp-like nested function calls
>>> print( dict( enumerate( zip( 'abc',  sorted( 'abc bca cab'.split(' '), key = lambda x: x[1] ) ) ) ) )
{0: ('a', 'cab'), 1: ('b', 'abc'), 2: ('c', 'bca')}
>>> 'abc bca cab'.split(' ') ..sorted(key = lambda x: x[1]) ...zip('abc') ..enumerate() ..dict() ..print()
{0: ('a', 'cab'), 1: ('b', 'abc'), 2: ('c', 'bca')}
>>> ## IMPORT MODULES WRITTEN WITH PSEUDOMETHOD SYNTAX
>>> ## create test_module.py
>>> open('test_module.py', 'w').write('"hello" ..print()\n') ..print('bytes written')
18 bytes written
>>> ## during import, insert the magic prefix 'pseudosugar.' before the last module
>>> ## import pseudosugar.a
>>> ## import a.pseudosugar.b
>>> ## import a.b.pseudosugar.c
>>> import pseudosugar.test_module
hello

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

推荐PyPI第三方库


热门话题
java Midlet即使按下设备的“剪切”按钮也不能关闭   JavaSpring环境。getProperty未解析   用Hibernate在Java中实现序列   具有重复键和文件写入的java映射   java显示控制台输出到JavaFXML应用程序中的TextArea   java Cucumber在编译为时无法在类路径上找到功能文件。战争   java过滤器中的servlet问题   安卓中消息和字符串之间的java转换?   java Apache POI读取单元(下午12:35:00)时间值返回1899年12月31日   Java游戏中的碰撞检测?   JSF2.0中的JavajQuery   java在setValue之后使用不同的值   java如何修复可选文件的StackOverflow错误?   如何在不使用服务器设置的情况下使用java从Outlook mail下载附件?   YAxis上Java BoxLayout中单个字符的摆动对齐偏离中心