Skip to main content

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)

Release files for pycope 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycope 0.1.1
File Size Uploaded
pycope-0.1.1.tar.gz 4.9 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pycope 0.1.1
File Interpreter ABI Platform
pycope-0.1.1-py3-none-any.whl Python 3 none any Details
pycope-0.1.1-py2-none-any.whl Python 2 none any Details

Total release size: 17.9 kB

Release files / pycope-0.1.1.tar.gz

Download URL pycope-0.1.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
da8ea434e0e112c56dc4434eb3be81cff55c2299f9ca48087342e71be0912fff
BLAKE2b-256 checksum
How to use checksums
4d821783b2c51480b140d54085323c7520cc00dd43ca471de654db2639b94a30
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / pycope-0.1.1-py3-none-any.whl

Download URL pycope-0.1.1-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be6f797207e6d68cb8d636c0888c2b84abd0178061e388646f843ee7751e88f7
BLAKE2b-256 checksum
How to use checksums
04c1b8d1321b06736b4552b8ea956813ffaad07df3b4c3386cfc0fd2a362e83c
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / pycope-0.1.1-py2-none-any.whl

Download URL pycope-0.1.1-py2-none-any.whl
Size 6.5 kB
Tags Python 2
SHA-256 checksum
How to use checksums
eb1da5c8078dd8086cc26c9e650e2c58ceb59ca3f5884e57055795296bd9cc08
BLAKE2b-256 checksum
How to use checksums
241aac94dd0b9ec3a1fbc7c416cc3b91fe30cad2d6c8e46fd631a279c4ee0a27
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.1 This release

3 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page