Coder Social home page Coder Social logo

atdict's Introduction

PyPI version Anaconda-Server Badge DOI Test Status codecov

atdict

An attribute-access ordered dictionary


atdict is an attribute-access ordered dictionary. You can use a key name as an attribute to access the value of the dictionary for a key, for example, o.key_name rather than o['key_name']. Only a minimum set of methods are implemented so as to minimize the chances of name conflicts.



Requirement

  • Python 3.6, 3.7, 3.8, or 3.9

Install

You can install with conda from conda-forge:

conda install -c conda-forge atdict

or with pip:

$ pip install -U atdict

How to use

Import atdict

Import atdict from the package atdict.

from atdict import atdict

Initialize atdict

You can initialize an atdict with any arguments that can initialize collections.OrderedDict.

For example:

o1 = atdict([('foo', 40), ('bar', 'ham')])
print(o1)

It will print.

atdict(foo=40, bar='ham')

An atdict can be also initialized with another atdict.

o2 = atdict(o1)
print(o2)

The o2 is initialized with a (shallow) copy of the contents of o1.

atdict(foo=40, bar='ham')

Access to a value

Yon can use a key name as an attribute of atdict.

print(o1.foo)

This will print the value for the key foo, which is 40.

40

Modify a value

To modify a value, you can assign a new value to the attribute.

o1.foo = 50
print(o1)
atdict(foo=50, bar='ham')

The value for the key foo changed from 40 to 50.

Add a key

To add a key, you can also assign a value to the attribute

o1.baz = 'eggs'
print(o1)
atdict(foo=50, bar='ham', baz='eggs')

Delete a key

del deletes a key.

del o1.bar
print(o1)
atdict(foo=50, baz='eggs')

Copy and deepcopy

A copy will be created if atdict is initialized with another atdict. However, this will be a shallow copy.

l = [1, 2]
o1 = atdict([('foo', l)])
o2 = atdict(o1)
print(o2.foo is o1.foo)
True

To make a deep copy, you can use copy.deepcopy().

import copy
l = [1, 2]
o1 = atdict([('foo', l)])
o2 = copy.deepcopy(o1)
print(o2)
atdict(foo=[1, 2])

o2.foo and o1.foo are not the same object.

print(o2.foo is o1.foo)
False

Pickle

An atdict is picklable as long as all values are picklable.

import pickle
o1 = atdict([('foo', 40), ('bar', 'ham')])
p1 = pickle.dumps(o1)
o2 = pickle.loads(p1)
print(o2)
atdict(foo=40, bar='ham')

License

  • atdict is licensed under the BSD license.

Contact

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.