Giter Site home page Giter Site logo

gang's Introduction

Gang

Build Status Coverage Status Code Health
Latest Version Supported Python versions Supported Python implementations License Downloads
Python dictionary with dot-style access

from gang import subclass_object

d = dict(hello="world", lorem=list(dict(ipsum="dolor"), dict(sit="amet")))

g = subclass_object(d)

print(g.hello)  # world
print(g.lorem[0].ipsum)  # dolor
print(g.lorem[1].sit)  # amet

Another example

from gang import subclass_object

grades = dict(
    Math=dict(
        all=[5,5,5,5],
        avg=5.0
    ),
    Biology=dict(
        all=[4,4,5,5],
        avg=4.5
    )
)

t = subclass_object(grades)

for klass, grades in t.iteritems():
    print('{0}\n  All: {1}\n  Average: {2:f}'.format(
        klass,
        str(grades.all),
        grades.avg
    ))
    
# Output
# Biology
#   All: [4, 4, 5, 5]
#   Average: 4.500000
# Math
#   All: [5, 5, 5, 5]
#   Average: 5.000000

subclass_object returns GangDict or GangList, which are inherited from dict and list. So you can use all method for standard dictionaries and lists.

Note that transformation to subclassed dict or list is "lazy". In the example above, the list in d['lorem'] is not transformed until you call g.lorem. Unfortunately, it is not cached also. So each call to g.lorem will make new transformation. This can affect performance seriously.

I find this method useful to work with big config files in JSON or YAML. If you don't want to deal with lots of config['some_value_1']['some_value_2']... and make code more readable, you can give it a try.

Bitdeli Badge

gang's People

Contributors

aspyatkin avatar bitdeli-chef avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.