Git Product home page Git Product logo

py-context's Introduction

Py-Context

https://badge.fury.io/py/py-context.png https://travis-ci.org/dealertrack/py-context.png?branch=master https://coveralls.io/repos/dealertrack/py-context/badge.png?branch=master

Python dict with stacked context data

Installing

You can install py-context using pip:

$ pip install py-context

Example

>>> context = Context({'user': 'Fred', 'city': 'Bedrock'})
>>> context['user']
'Fred'
>>> context['city']
'Bedrock'
>>> context.push({'user': 'Barney'})
>>> context['user']
'Barney'
>>> context['city']
'Bedrock'
>>> context.pop()
{'user': 'Barney'}
>>> context['user']
'Fred'

Context also supports signals. Signal handler can be attached globally:

>>> @context_key_changed.connect
... def handler(sender, context, key, new, old):
...     print(key, new, old)

>>> context = Context()
>>> context['hello'] = 'world'
hello world <Missing>

Or to individual context instances:

>>> def handler(sender, context, key, new, old):
...     print(key, new, old)
>>> context = Context()
>>> context_key_changed.connect(handler, sender=context)

Supported signals:

>>> @context_initialized.connect
... def handler(sender, context):
...     pass

>>> @pre_context_changed.connect
... def handler(sender, context):
...     pass

>>> @post_context_changed.connect
... def handler(sender, context):
...     pass

>>> @context_key_changed.connect
... def handler(sender, context, key, new, old):
...     pass

Additionally, ClassSignallingContext can be used to subscribe signals by sender classes, not instances:

>>> class TestContext(ClassSignallingContext):
...     pass
>>> def context_key_changed_handler(sender, context, key, new, old):
...     print(key, new, old)
>>> _ = context_key_changed.connect(context_key_changed_handler, sender=TestContext)

>>> context = Context()
>>> class_context = TestContext()

>>> context['foo'] = 'bar'
>>> class_context['foo'] = 'bar'
foo bar <Missing>

Testing

To run the tests you need to install testing requirements first:

$ make install

Then to run tests, you can use nosetests or simply use Makefile command:

$ nosetests -sv
# or
$ make test

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.