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 details)

Uploaded Source

Built Distributions

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

Uploaded Python 3

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

Uploaded Python 2

File details

Details for the file destructipy-0.0.5.tar.gz.

File metadata

  • Download URL: destructipy-0.0.5.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.6.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for destructipy-0.0.5.tar.gz
Algorithm Hash digest
SHA256 e6d4c80988c935ab46eeeede34d904cfa2ac759d3fbb5ad2435cfa09f3a1a63f
MD5 5df8b585d722afe448a8ee014190ed3e
BLAKE2b-256 d9ba22c45b43750fc959536fd863e51a9ac1194244bc25b77317e06ab0c01beb

See more details on using hashes here.

File details

Details for the file destructipy-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: destructipy-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.6.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for destructipy-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f2a88fada357e3099324a25178ba54aa2bd3b9bb116e9c33d3b27524dc836b73
MD5 aef8d876942ab3026a634722adb8fbe3
BLAKE2b-256 9dd5520e8657e8a2bf6ae0e36b051d48c0cacf26290eccfed9b7eab58128dceb

See more details on using hashes here.

File details

Details for the file destructipy-0.0.5-py2-none-any.whl.

File metadata

  • Download URL: destructipy-0.0.5-py2-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.6.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for destructipy-0.0.5-py2-none-any.whl
Algorithm Hash digest
SHA256 4e416f5b54e93f00fa8fdd337223dc553e355ef5281ae85874cec68503e52721
MD5 111239f7560253d43520d825e508bee2
BLAKE2b-256 0739d2514114721e04eb1c0ef3c31c96627afe4c32d25840152485f3f6cf48d3

See more details on using hashes here.

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