pseudosugar

Extend Python with functional programming language features
Download

pseudosugar Ranking & Summary

Advertisement

  • Rating:
  • License:
  • GPL
  • Price:
  • FREE
  • Publisher Name:
  • Kai Zhu
  • Publisher web site:
  • http://www-rcf.usc.edu/~kaizhu/

pseudosugar Tags


pseudosugar Description

Extend Python with functional programming language features pseudosugar is a pure Python module adding the following syntax sugars:function>>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)DEMO USAGE:>>> ## start up the interactive console>>> from pseudosugar import *>>> pseudo_console().interact()Python 3.1.1 (r311:74480, Sep 13 2009, 17:17:12) on linux2 Type "help", "copyright", "credits" or "license" for more information. (pseudo_console) pseudo_importer - adding hook < pseudosugar.pseudo_importer object at 0xb7ac754c > to sys.meta_path >>> from pseudosugar import *>>> #### QUICK EXAMPLES>>> ## prefix operator>>> print ## postfix operator>>> 'hello', 'world' >>>>printhello 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 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 'tuple ) ## list(tuple('abcd'))>>> #### 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 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 ) ) ) ) ){0: ('a', 'cab'), 1: ('b', 'abc'), 2: ('c', 'bca')}>>> 'abc bca cab'.split(' ') ..sorted(key = lambda x: x) ...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() ') ..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_modulehello Requirements: · Python


pseudosugar Related Software