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 details)

Uploaded Source

Built Distributions

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

Uploaded Python 3

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

Uploaded Python 2

File details

Details for the file pycope-0.1.1.tar.gz.

File metadata

  • Download URL: pycope-0.1.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.5.6

File hashes

Hashes for pycope-0.1.1.tar.gz
Algorithm Hash digest
SHA256 da8ea434e0e112c56dc4434eb3be81cff55c2299f9ca48087342e71be0912fff
MD5 2aec753fbf381729e19de0e04ed8b163
BLAKE2b-256 4d821783b2c51480b140d54085323c7520cc00dd43ca471de654db2639b94a30

See more details on using hashes here.

File details

Details for the file pycope-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pycope-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.5.6

File hashes

Hashes for pycope-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 be6f797207e6d68cb8d636c0888c2b84abd0178061e388646f843ee7751e88f7
MD5 50347bab3401999ffc659d4c3ed25695
BLAKE2b-256 04c1b8d1321b06736b4552b8ea956813ffaad07df3b4c3386cfc0fd2a362e83c

See more details on using hashes here.

File details

Details for the file pycope-0.1.1-py2-none-any.whl.

File metadata

  • Download URL: pycope-0.1.1-py2-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.5.6

File hashes

Hashes for pycope-0.1.1-py2-none-any.whl
Algorithm Hash digest
SHA256 eb1da5c8078dd8086cc26c9e650e2c58ceb59ca3f5884e57055795296bd9cc08
MD5 a21ca3de030e4ad2f3f256a0eb2552f8
BLAKE2b-256 241aac94dd0b9ec3a1fbc7c416cc3b91fe30cad2d6c8e46fd631a279c4ee0a27

See more details on using hashes here.

Supported by

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