Skip to main content

A Context-Oriented Programming Extension for Python.

Project description

PyCOPE Build Status codecov

This is a Context-Oriented Programming Extension for the Python language. It provides that allow programmers to utilize Context-Oriented techniques in an Object-Oriented manner.

Motivation

This library is the fruit of my Context-Oriented Programming (COP) research and experimentation. My approach is a new take on COP that can potentially be implemented in any Object Oriented supported language. I'll be writing a paper on it soon.

Installation

This library is available in Pypi. Execute the following command to istall it:

pip install pycope

Description

To make this library work at minimum you need a context, a group of strategies with the same method signature and layers.

Context

It's unconventional, but the context is stored as a stack of dictionaries. When you create a COP "layer" you are adding/removing to/from the context's dictionary stack. Each item in the stack represents a layer's alterations to the previous item. This was made possible with a functional styled immutable dictionary, where a new dictionary is made after each set of alterations.

On a side note with contexts, there should be at least one context foreach thread of execution. They are thread-safe, but you will probably get unexpected results if multiple threads are applying layers to the same context.

Layers

A 'with' statement must be used on a pycope layer. Layers effects are not permanent and must be reversed after the layer reaches the end of its execution scope.

Strategies

A single group of strategies is used to define a contextual execution. The strategies specify their priority based on the available context's map of fields. The strategy with the highest priority is the one chosen for execution. This is essentially an advanced if/elif/else chain.

Executable

An executable defines a single contextual executable. The main goal is to minimize boilerplate for the user whilst still allowing flexibility and power.

Example

import context
import strategy
import prioritizers
import executable

def is_red(red, green, blue):
    return red > 50 and green < 30 and blue < 30:

def is_green(red, green, blue):
    return red < 30 and green > 50 and blue < 30:

def is_blue(red, green, blue):
    return red < 30 and green < 30 and blue > 50:

color_ctx = context.Context()
color_strategies = [strategy.Strategy(is_red, [prioritizers.contains_field("color": "red")]),
                    strategy.Strategy(is_green, [prioritizers.contains_field("color": "green")]),
                    strategy.Strategy(is_blue, [prioritizers.contains_field("color": "blue")])]
color_tester = executable.Executable(color_ctx, color_strategies)

with color_tester.new_layer({"color":"red"}, []) as layer1:
    assert color_tester.execute(255, 0, 0)
    assert not color_tester.execute(0, 255, 0)
    assert not color_tester.execute(0, 0, 255)

    with color_tester.new_layer({"color":"blue"}, []) as layer2:
        assert not color_tester.execute(255, 0, 0)
        assert not color_tester.execute(0, 255, 0)
        assert color_tester.execute(0, 0, 255)

    assert color_tester.execute(255, 0, 0)
    assert not color_tester.execute(0, 255, 0)
    assert not color_tester.execute(0, 0, 255)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycope-0.1.1.tar.gz (4.9 kB view hashes)

Uploaded Source

Built Distributions

pycope-0.1.1-py3-none-any.whl (6.5 kB view hashes)

Uploaded Python 3

pycope-0.1.1-py2-none-any.whl (6.5 kB view hashes)

Uploaded Python 2

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page