Skip to main content

ordered, dynamically-expandable dot-access dictionary

Project description

DotMap

Build Status

Donate

Install

pip3 install dotmap

Upgrade

Get updates for current installation

pip3 install --upgrade dotmap

Features

DotMap is a dot-access dict subclass that

  • has dynamic hierarchy creation (autovivification)
  • can be initialized with keys
  • easily initializes from dict
  • easily converts to dict
  • is ordered by insertion

The key feature is exactly what you want: dot-access

from dotmap import DotMap
m = DotMap()
m.name = 'Joe'
print('Hello ' + m.name)
# Hello Joe

However, DotMap is a dict and you can treat it like a dict as needed

print(m['name'])
# Joe
m.name += ' Smith'
m['name'] += ' Jr'
print(m.name)
# Joe Smith Jr

It also has fast, automatic hierarchy (which can be deactivated by initializing with DotMap(_dynamic=False))

m = DotMap()
m.people.steve.age = 31

And key initialization

m = DotMap(a=1, b=2)

You can initialize it from dict and convert it to dict

d = {'a':1, 'b':2}

m = DotMap(d)
print(m)
# DotMap(a=1, b=2)

print(m.toDict())
# {'a': 1, 'b': 2}

And it has iteration that is ordered by insertion

m = DotMap()

m.people.john.age = 32
m.people.john.job = 'programmer'
m.people.mary.age = 24
m.people.mary.job = 'designer'
m.people.dave.age = 55
m.people.dave.job = 'manager'

for k, v in m.people.items():
	print(k, v)
print

# john DotMap(age=32, job='programmer')
# mary DotMap(age=24, job='designer')
# dave DotMap(age=55, job='manager')

It also has automatic counter initialization

m = DotMap()
for i in range(7):
	m.counter += 1
print(m.counter)
# 7

And automatic addition initializations of any other type

m = DotMap()
m.quote += 'lions'
m.quote += ' and tigers'
m.quote += ' and bears'
m.quote += ', oh my'
print(m.quote)
# lions and tigers and bears, oh my

There is also built-in pprint as dict or json for debugging a large DotMap

m.pprint()
# {'people': {'dave': {'age': 55, 'job': 'manager'},
#             'john': {'age': 32, 'job': 'programmer'},
#             'mary': {'age': 24, 'job': 'designer'}}}
m.pprint(pformat='json')
# {
#     "people": {
#         "dave": {
#	      "age": 55,
#	      "job": "manager"
# 	  },
# 	  "john": {
#	      "age": 32,
#	      "job": "programmer"
# 	  },
# 	  "mary": {
#	      "age": 24,
#	      "job": "designer"
# 	  }
#     }
# }

And many other features involving dots and dictionaries that will be immediately intuitive when used.

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

dotmap-1.3.30.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

dotmap-1.3.30-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file dotmap-1.3.30.tar.gz.

File metadata

  • Download URL: dotmap-1.3.30.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.1

File hashes

Hashes for dotmap-1.3.30.tar.gz
Algorithm Hash digest
SHA256 5821a7933f075fb47563417c0e92e0b7c031158b4c9a6a7e56163479b658b368
MD5 7c9ca326427eb42ff3680740814e06db
BLAKE2b-256 bc68c186606e4f2bf731abd18044ea201e70c3c244bf468f41368820d197fca5

See more details on using hashes here.

File details

Details for the file dotmap-1.3.30-py3-none-any.whl.

File metadata

  • Download URL: dotmap-1.3.30-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.1

File hashes

Hashes for dotmap-1.3.30-py3-none-any.whl
Algorithm Hash digest
SHA256 bd9fa15286ea2ad899a4d1dc2445ed85a1ae884a42effb87c89a6ecce71243c6
MD5 6ec8546fc804658647549ab9700b895f
BLAKE2b-256 4df9976d6813c160d6c89196d81e9466dca1503d20e609d8751f3536daf37ec6

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