A forward-oriented programming paradigm for Python.
Project description
pyfop
Implements forward-oriented programming in Python. This shares configuration arguments across multiple components and determines their values after the main business logic.
Dependencies: makefun
Developer: Emmanouil (Manios) Krasanakis
Contant: maniospas@hotmail.com
Features
:alembic: Adapt arguments to usage context
:surfer: Argument sharing between methods
:rocket: Speed up library development
:hammer_and_wrench: Easy adoption with decorators
Quickstart
Enable lazy execution and automatically annotate arguments with defaults as aspects:
@lazy
@autoaspects
def affine(x, scale=1, offset=0):
return x*scale + offset
Produce results with python code:
GM = (affine(2)*affine(8))**0.5
Set aspect values to previous code:
print(GM(scale=3)) # 12
Advanced features
Lazy methods calling each other
@lazy
@autoaspects
def gm(x, y, affine=affine): # pass the method as an argument
return (affine(x)*affine(y))**0.5
GM = gm(2, 8)
print(GM(scale=3)) # 12
List aspects
print(GM.get_input_context(scale=3))
# context:
# - scale:
# value: 3,
# priority: Priority.HIGH
# shares: 1
# - offset:
# value: 1,
# priority: Priority.INCREASED
# shares: 4
Aspects are shared between everything contributing to the result
@lazy
@autoaspects
def square(x, scale=1):
return scale*x*x
print(affine(2)(scale=2)) # 4
print((affine(2)+square(1))(scale=2)) # 5
Priority-based selection between defaults
@lazy
def logpp(x, offset=Aspect(1, Priority.INCREASED)):
import math
return math.log(x+offset)/math.log(2)
result = affine(2)+log(3)
print(result(scale=2)) # 5+2=7
Lazy execution caching or not
@lazy # sets up caching
def inc(x):
print("running")
return x+1
print(inc(2)())
# running
# 3
print(inc(2)())
# 3
print(inc(3)())
# running
# 4
@lazy_no_cache # disables cache
def inc(x):
print("running")
return x+1
print(inc(2)())
# running
# 3
print(inc(2)())
# running
# 3
print(inc(3)())
# running
# 4
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.