Skip to main content

pseudomethod - adds 'a..b()' syntax feature for dynamically calling ordinary functions as bound methods

Project description

pseudomethod - adds ‘a..b()’ syntax feature for dynamically calling ordinary functions as bound methods

REQUIRES LINUX OS AND PYTHON3.1

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

DESCRIPTION: pseudomethod - adds ‘a..b()’ syntax feature for dynamically calling ordinary functions as bound methods

SUMMARY: pseudomethod is a pure python module. pseudomethod is a python ast tree hack, adding the following syntax sugars: ‘..’ ‘…’ ‘….’. pseudomethod can extend restricted, unsubclassable python types like types.CodeType (python code object type). pseudomethod can flatten ugly nested function calls by sequentially binding their return values.

RECENT CHANGELOG: 20091231 - added lambda __printop__: sugar 20091224 - added pseudomethod interactive console - revamped pseudomethod import hook 20091224 - modularized package - fix install issues - added sdist check 20091209 - improved documentation 20091205 - moved source code to c++ 20091116 - package integrated

DEMO USAGE:

>>> ## start up the interactive console
>>> import pseudomethod
>>> pseudomethod.pseudomethod_console().interact()

Python 3.1.1 (r311:74480, Sep 13 2009, 17:17:12) [GCC 4.3.2] on linux2 Type “help”, “copyright”, “credits” or “license” for more information. (pseudomethod_console)

>>> from pseudomethod import *
>>> ## 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 appends an exclamation or another ending
>>> def add_end(self, end = '!'): return self + end
>>> 'hello' ..add_end() ..print()
hello!
>>> 'hello'.upper() ..add_end() ..print()
HELLO!
>>> 'hello'.upper() ..add_end(' world') ..print()
HELLO world
>>> 'hello'.upper() ..add_end(' world').lower() ..print()
hello world
>>> 'hello'.upper() ..add_end(' world').lower() ..add_end('!') ..print()
hello world!
>>> 'hello'.upper() ..add_end(' world').lower() ..add_end('!') ..add_end(end = '!') ..print()
hello world!!
>>> ## OPERATOR PRECEDENCE
>>> ## 'a..b()' has the same operator precedence as 'a.b()'  which precedes <and or not + - * /> but not <= == ,>
>>> 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
>>> ## in fact, this package was written with pseudomethod syntax
>>> ## enable pseudomethod import hook
>>> import pseudomethod
>>> pseudomethod.IMPORTER.add_hook()
pseudomethod_importer - adding hook <pseudomethod.pseudomethod_importer object at 0x870ddec> to sys.meta_path
<pseudomethod.pseudomethod_importer object at 0x870ddec>
>>> ## create test_module.py
>>> open('test_module.py', 'w').write('"hello" ..print()\n')
35
>>> ## during import, add the magic prefix 'pseudomethod.' to the beginning of the module name
>>> import pseudomethod.test_module
hello

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pseudomethod-2009.12.30.py3k.cpp.tar.gz (89.1 kB view details)

Uploaded Source

File details

Details for the file pseudomethod-2009.12.30.py3k.cpp.tar.gz.

File metadata

File hashes

Hashes for pseudomethod-2009.12.30.py3k.cpp.tar.gz
Algorithm Hash digest
SHA256 1a255343ea1dde0e7ea7b5009cc6680fc5c1919dbcf1b922ab29f7fb18798bc6
MD5 ec9caa79cc553ed6bc0758988212b006
BLAKE2b-256 3165fb19b818f037b547578409073b32b04fd16950661739008a0703c6b5109d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page