Skip to content

Pure Python extensions library that includes Scala-like streams, Json with attribute access syntax, and other common use stuff

License

Notifications You must be signed in to change notification settings

adzilla/pyxtension

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyxtension

image

image

pyxtension is a pure Python GNU-licensed library that includes Scala-like streams, Json with attribute access syntax, and other common use stuff.

Json

Never again will you have to write code like this:

body = {
    'query': {
        'filtered': {
            'query': {
                'match': {'description': 'addictive'}
            },
            'filter': {
                'term': {'created_by': 'Mats'}
            }
        }
    }
}

From now on, you may simply write the following three lines:

body = Dict()
body.query.filtered.query.match.description = 'addictive'
body.query.filtered.filter.term.created_by = 'Mats'

Json is a module that provides mapping objects that allow their elements to be accessed both as keys and as attributes:

> from Json import Json
> a = Json({'foo': 'bar'})
> a.foo
'bar'
> a['foo']
'bar'

Attribute access makes it easy to create convenient, hierarchical settings objects:

with open('settings.yaml') as fileobj:
    settings = Json(yaml.safe_load(fileobj))

cursor = connect(**settings.db.credentials).cursor()

cursor.execute("SELECT column FROM table;")

Installation

from Github:

$ git clone https://github.com/asuiu/pyxtension.git

or

$ git submodule add https://github.com/asuiu/pyxtension.git

Basic Usage

Json comes with two different classes, Json, and JsonList. Json is fairly similar to native dict() as it extends it an is a mutable mapping that allow creating, accessing, and deleting key-value pairs as attributes. JsonList is similar to native list() as it extends it and offers a way to transform the dict() objects from inside also in Json() instances.

Valid Names

Any key can be used as an attribute as long as:

  1. The key represents a valid attribute (i.e., it is a string comprised only of alphanumeric characters and underscores that doesn't start with a number)
  2. The key does not shadow a class attribute (e.g., get).

Attributes vs. Keys

There is a minor difference between accessing a value as an attribute vs. accessing it as a key, is that when a dict is accessed as an attribute, it will automatically be converted to a Json object. This allows you to recursively access keys:

> attr = Json({'foo': {'bar': 'baz'}})
> attr.foo.bar
'baz'

Relatedly, by default, sequence types that aren't bytes, str, or unicode (e.g., lists, tuples) will automatically be converted to tuples, with any mappings converted to Json:

> attr = Json({'foo': [{'bar': 'baz'}, {'bar': 'qux'}]})
> for sub_attr in attr.foo:
>     print(subattr.bar)
'baz'
'qux'

To get this recursive functionality for keys that cannot be used as attributes, you can replicate the behavior by using dict syntax on Json object:

> attr = AttrDict({1: {'two': 3}})
> attr[1].two
3

Classes

AttrDict comes with two different objects, Json and JsonList.

Json

An Attr object that subclasses dict. You should be able to use this absolutely anywhere you can use a dict. While this is probably the class you want to use, there are a few caveats that follow from this being a dict under the hood.

Assignment as keys will still work:

> attr = AttrDict('foo': {})
> attr['foo']['bar'] = 'baz'
> attr.foo
{'bar': 'baz'}

License

pyxtension is released under a GNU Public license. The idea for Json module was inspired from addict and AttrDict , but this is a completely new and improved code.

About

Pure Python extensions library that includes Scala-like streams, Json with attribute access syntax, and other common use stuff

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%