Skip to main content

ordered, dynamically-expandable dot-access dictionary

Project description

DotMap

Build Status

DotMap is a dot-access dict subclass that

  • has dynamic hierarchy creation
  • 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.

A note on unpacking (using the ** operator)

Unpacking DotMap can be done like this

m = DotMap()
m.a = 1
simple_unpack = dict(**m.toDict())
print(simple_unpack)
# {'a': 1}

I've given multiple tries to getting the syntax to work with just **m and it's just not worth the effort when the workaround is this simple. If you can figure out a way to fully ace the dict subclass this way and still keep all the unit tests functioning, submit a PR and I'll be happy to review.

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.7.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dotmap-1.3.7-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dotmap-1.3.7.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4

File hashes

Hashes for dotmap-1.3.7.tar.gz
Algorithm Hash digest
SHA256 8dd2bc0ad7fa0dfe00f7b774af0041e7499521d4adc054ea29d8d9d6fc27a8bd
MD5 976f2a3bc0ae79f52caa40ba7db741de
BLAKE2b-256 4848f3a23bb5e88175a4c114ab5a166be29b907f0da16e897a80628569a3a238

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dotmap-1.3.7-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4

File hashes

Hashes for dotmap-1.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a2de107c62e5b525231b8cd7277374161e793d6e12a1b11261cafd5c7743865c
MD5 0a4951c61c9abe344437b79424047325
BLAKE2b-256 45e31b931132e1b2cc87308335c180ea5d4d2535433bea62fb40632326efbd24

See more details on using hashes here.

Supported by

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