Skip to main content

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 using os.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


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)

Uploaded Source

Built Distributions

destructipy-0.0.5-py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 3

destructipy-0.0.5-py2-none-any.whl (4.5 kB view hashes)

Uploaded Python 2

Supported by

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