Giter Club home page Giter Club logo

tupledict's Introduction

Tupledict

build

Classes

  • DictList
  • TupleDict

You can use add this using poetry by running (it's not pushed to PyPI):

$ poetry add git+https://github.com/cammacrae/tupledict.git

Some examples:

>>> from tupledict import TupleDict

Constructor

The constructor accepts no arguments:

>>> td = TupleDict()

Or a dict:

>>> td = TupleDict({"one": 1, "two": 2})

Or an iterable containing key, value pairs:

>>> td = TupleDict([("one", 1), ("two", 2)])

It also accepts keyword arguments:

>>> td = TupleDict(one=1, two=2)

Or a mixed bag:

>>> td = TupleDict({"one": 1, "two": 2}, three=3, four=4)

Retrieving values

We can then retrieve our values as we would with a dict:

>>> td["one"]
1

TupleDict also implements a select method for this purpose:

>>> td.select("one")
1

select() also works with a list of keys:

>>> td.select(["one", "two"])
[1, 2]

which returns a list of values matching the keys. The keys needn't exist:

>>> td.select(["one", "three"])
[1]

and wildcards denoted by "*" are accepted:

>>> td.select("*")
[1, 2]

Setting values and more advanced retrieval

Values are set in the usual way:

>>> td["three"] = 3
>>> td["three"]
3
>>> td.select(["one", "three"])
[1, 3]
>>> td.select("*")
[1, 2, 3]

Using a tuple to set a composite key makes select() a lot more powerful:

>>> td2 = TupleDict([(("one", "first"), 1), (("one", "second"), 1.5), (("two", "first"), 2)])
>>> td2["one"]
KeyError: 'Key length is 1 but this dict has key length 2.'
>>> td2[("one", "first")]
1
>>> td2.select("one", "*")
[1, 1.5]
>>> td2.select("*", "first")
[1, 2]
>>> td2.select(["one", "two"], "*")
[1, 1.5, 2]
>>> td2.select(["one", "two"], "third")
[]
>>> td2.select("*", ["first", "third"])
[1, 2]

tupledict's People

Contributors

cammacrae avatar

Watchers

 avatar

tupledict's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.