es6 style dict/object destructure for python
Project description
destructipy
es6 style dict/object destructure for python
install:
$ pip install destructipy
usage:
# must import this way...
from destructipy import _ as ds
# support dicts
abcd = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
# ds.i()/ds.item() - using operator.itemgetter
# dict safe, will probably raise error if not dict
d, c, \
b, a = ds.i(abcd)
print(a, b, c, d)
# func can be named however you wish...
from destructipy import _ as unpack
# supports objects
class ABCD:
a = 1
b = 2
abcd = ABCD()
abcd.c = 3
abcd.d = 4
# unpack.a()/unpack.attr() - using operator.attrgetter
# notice: dicts can also be passed but it will get their attributes, not items)
d, c, \
b, a = unpack.a(abcd)
print(a, b, c, d)
from destructipy import _ as ds
abcd_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
class ABCD:
a = 5
b = 6
abcd_obj = ABCD()
abcd_obj.c = 7
abcd_obj.d = 8
# ds() - auto decide if it's dict or object
# good for one time or mixed dict-object/small lists
# got minor performance penalty, see benchmark below
d, c, b, a = ds(abcd_dict)
print(a, b, c, d)
d, c, b, a = ds(abcd_obj)
print(a, b, c, d)
caveats:
- Does not work on the Interactive Python Console (no source to analyze...)
- If you plan to compile your
.py
to.pyc
and delete the source, run$ python -m destructipy
in your project root to create.destructipy
cache file before doing so - It is recommended to place a
import destructipy
on your project init for destructipy to keep the initial cwd (current working directory), just incase you switch the cwd later on usingos.chdir
or such... - using sys._getframe which is only implemented at CPython. tested on 2.7 and 3.8
benchmark:
$ python benchmark.py
9 iterations:
regular : 0:00:00.000009
ds : 0:00:00.000100
ds.i/ds.a : 0:00:00.000024
99 iterations:
regular : 0:00:00.000020
ds : 0:00:00.000066
ds.i/ds.a : 0:00:00.000061
999 iterations:
regular : 0:00:00.000164
ds : 0:00:00.000568
ds.i/ds.a : 0:00:00.000521
9999 iterations:
regular : 0:00:00.001409
ds : 0:00:00.005194
ds.i/ds.a : 0:00:00.004448
99999 iterations:
regular : 0:00:00.011635
ds : 0:00:00.045619
ds.i/ds.a : 0:00:00.040462
999999 iterations:
regular : 0:00:00.104921
ds : 0:00:00.396913
ds.i/ds.a : 0:00:00.377507
9999999 iterations:
regular : 0:00:01.034074
ds : 0:00:03.985506
ds.i/ds.a : 0:00:03.883992
99999999 iterations:
regular : 0:00:10.758053
ds : 0:00:46.962093
ds.i/ds.a : 0:00:45.535044
999999999 iterations:
regular : 0:02:00.999255
ds : 0:08:04.748202
ds.i/ds.a : 0:07:42.837535
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
destructipy-0.0.5.tar.gz
(3.5 kB
view hashes)
Built Distributions
Close
Hashes for destructipy-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2a88fada357e3099324a25178ba54aa2bd3b9bb116e9c33d3b27524dc836b73 |
|
MD5 | aef8d876942ab3026a634722adb8fbe3 |
|
BLAKE2b-256 | 9dd5520e8657e8a2bf6ae0e36b051d48c0cacf26290eccfed9b7eab58128dceb |
Close
Hashes for destructipy-0.0.5-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e416f5b54e93f00fa8fdd337223dc553e355ef5281ae85874cec68503e52721 |
|
MD5 | 111239f7560253d43520d825e508bee2 |
|
BLAKE2b-256 | 0739d2514114721e04eb1c0ef3c31c96627afe4c32d25840152485f3f6cf48d3 |