GithubHelp home page GithubHelp logo

trinkey / dotindex Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 18 KB

allows you to use dot notation to index dicts in python, like how you can in javascript

License: GNU Affero General Public License v3.0

Python 100.00%

dotindex's Introduction

Note

This repository has been migrated to git.gay. The Github version will no longer be maintained.

DotIndex

Allows you to use dot notation to index dicts, like how you can in javascript

When printed, DotIndex objects are printed as a normal dict would be, however the curly braces have a full stop before them, to signify that it is a DotIndex object. (.{} would be an empty object)

Code Examples

Basic usage:

from DotIndex import DotIndex

x = {
  "foo": "bar"
}

y = DotIndex(x)

print(y) # Expected output: ".{'foo': 'bar'}"
print(y.foo) # Expected output: "bar"
print(y["foo"]) # Expected output: "bar"

y["cat"] = "dog"

print(y.cat) # Expected output: "dog"

Adding together objects:

from DotIndex import DotIndex

x = DotIndex({
  "foo": "bar"
})

y = DotIndex({
  "cat": "dog"
})

# Adds all values in y to x
# Also works if you do `x = x + y`
x += y

print(x) # Expected output: ".{'cat': 'dog', 'foo': 'bar'}"

# Adding a DotIndex and a dict
z = x + {
  "dicts": True
}

print(z) # Expected output: ".{'cat': 'dog', 'dicts': True, 'foo': 'bar'}"

# The second value overwrites any duplicate keys from the original object
z += {
  "cat": "not dog"
}

print(z) # Expected output: ".{'cat': 'not dog', 'dicts': True, 'foo': 'bar'}"

Documentation

Creating the object

  • Parameters
    • obj (dict[str, Any]) - the dict object that should be turned into a DotIndex object
    • recursive (bool, default: True) - whether or not to DotIndex-ify any dicts inside the original object
    • verbose_logs (bool, default: False) - whether or not to print out extra logs
    • ignore_errors (bool, default: False) - whether or not to ignore non-critical errors
  • Other information:
    • Keys in obj must be strings, any other types will throw a TypeError (ignored with ignore_errors)
    • Keys in obj cannot start with two underscores ("__") as those could be used to overwrite preexisting instance variables used for the class itself
    • If recursive is true, the values of verbose_logs and ignore_errors will be passed through into any new DotIndex objects created

Converting types

  • iter(...) - Returns all keys in alphabetical order
  • str(...) - Returns a string formatted like the str(...) value of a dict object, with a full stop (".") at the start to show that it is a DotIndex type. Example: ".{'foo': 'bar'}"
  • repr(...) - Same as str(...)
  • int(...) - Returns the amount of keys in the object
  • float(...) - Same as int(...) except as a float type
  • bool(...) - Returns True if there are any keys in the object, and if there aren't it returns False

Comparisons

  • == - Checks if the two objects have all the values the same. Works with both DotIndex objects and dict objects
  • != - Opposite of ==
  • >, <, >=, <= - Compares the amount of keys in the object

Misc.

  • + - Takes the keys from the object on the right and adds them to the object on the left, overwriting any duplicates
  • len(...) - Returns the amount of keys in the object
  • 'val' in ... - Returns true if "val" is a key in the object

dotindex's People

Contributors

trinkey avatar

Watchers

 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.