Quantcast
Viewing latest article 9
Browse Latest Browse All 40

Answer by ted for How to use a dot "." to access members of dictionary?

My 2 cents: for my own purposes I developed minydra, a simple command-line parser which includes a custom class MinyDict (inspired by addict):

In [1]: from minydra import MinyDictIn [2]: args = MinyDict({"foo": "bar", "yes.no.maybe": "idontknow"}).pretty_print(); args╭──────────────────────────────╮│ foo          : bar           ││ yes.no.maybe : idontknow     │╰──────────────────────────────╯Out[2]: {'foo': 'bar', 'yes.no.maybe': 'idontknow'}In [3]: args.resolve().pretty_print(); args╭──────────────────────────╮│ foo : bar                ││ yes                      │││no                      ││││maybe : idontknow     │╰──────────────────────────╯Out[3]: {'foo': 'bar', 'yes': {'no': {'maybe': 'idontknow'}}}In [4]: args.yes.no.maybeOut[4]: "idontknow"In [5]: "foo" in argsOut[5]: TrueIn [6]: "rick" in argsOut[6]: FalseIn [7]: args.morty is NoneOut[7]: TrueIn [8]: args.items()Out[8]: dict_items([('foo', 'bar'), ('yes', {'no': {'maybe': 'idontknow'}})])

It goes further than addict by adding dumping/loading methods to/from jsonyaml and pickle and also has a strict mode in MinyDict.update() to prevent the creation of new keys (this is useful to prevent typos in the command-line)


Viewing latest article 9
Browse Latest Browse All 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>