python dark magic
Project description
ambiguous
because magic is fun
Install
pip install ambiguous
Usage
decorator: because decorators should accept args too
@decorator
def suffix(fn, str_='xyz'):
'''add a suffix to the result of the wrapped fn'''
def wrapper(*args, **kwargs):
return '%s_%s' % (fn(*args, **kwargs), str_)
return wrapper
@suffix
def abc(): return 'abc'
abc()
> 'abc_xyz'
@suffix('123')
def count(repeat=1): return '0' * repeat
count()
> '0_123'
count(3)
> '000_123'
thing_or_things: merges gets and multigets
@thing_or_things
def itself(args):
return { x : x for x in args }
itself(1)
> 1
itself([1, 2])
> { 1 : 1, 2 : 2 }
# specified argument
@thing_or_things('args')
def prefix(prefix, args):
return { x : "%s_%s" % (prefix, x) for x in args }
prefix('abc', [1, 2])
> { 1 : 'abc_1', 2 : 'abc_2' }
# works with default args
@thing_or_things
def multiply(args, factor=1):
return { x : x * factor for x in args }
multiply(2)
> 2
multiply(2, factor=2)
> 4
multiply([1, 2], factor=3)
> { 1 : 3, 2 : 6 }
what, parentheses optional?! (warning: still highly experimental)
import ambiguous
@ambiguous
def foo():
return 'foo'
print foo
> 'foo'
print foo()
> 'foo'
foo + 'abc'
> 'fooabc'
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ambiguous-0.4.1.tar.gz
(7.2 kB
view hashes)
Built Distribution
Close
Hashes for ambiguous-0.4.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e8d1165642ec96f58c20c8edc42de2b63bef92d5ce12d2933b44b6484c31d8c |
|
MD5 | c64e25d9e0519c1813eeaa4401214267 |
|
BLAKE2b-256 | 2ecf7f2424111ce060b8a29609175ffc4b6a2ac59c200ff6423ae52e4afa5eb9 |