rasguanabana / function-pattern-matching Goto Github PK
View Code? Open in Web Editor NEWPattern matching and guards for Python functions
License: MIT License
Pattern matching and guards for Python functions
License: MIT License
Works:
@fpm.case
@fpm.guard(msg = fpm.eq("Ping"), sender = fpm.isoftype(Actor))
def handle(self, sender, msg, payload):
sender.tell("Pong", sender=self)
Doesn't work:
@fpm.case(msg = "Ping")
@fpm.guard(sender = fpm.isoftype(Actor))
def handle(self, sender, msg, payload):
sender.tell("Pong", sender=self)
The second case raises the following error:
Traceback (most recent call last):
File "./ping_pong.py", line 14, in <module>
class PingPong(Actor):
File "./ping_pong.py", line 20, in PingPong
@fpm.guard(sender = fpm.isoftype(Actor))
File "/Users/mklemm/compile/python-arago-actors/lib/python3.7/site-packages/function_pattern_matching.py", line 551, in decorator
decoratee._argument_guards[arg_name] = match & decoratee._argument_guards[arg_name]
File "/Users/mklemm/compile/python-arago-actors/lib/python3.7/site-packages/function_pattern_matching.py", line 141, in checks
raise TypeError("The right-hand operand has to be instance of GuardFunc")
TypeError: The right-hand operand has to be instance of GuardFunc
Module may need some optimisation.
fpm works under 2.7 and 3.5. Older versions should be checked also, especially pre 3.3
Not sure what about anything different from CPython
Make complete and accurate.
Hi,
I'm writing a small actor system based on gevent and I really like your pattern matching library. It allows me keep the functions that handle the various messages that can be sent to an actor rather short because of the multi-dispatch feature.
But there seems to be a problem when decorating methods instead of functions with @guard and @case: Because inspect.signature skips the "self" argument, the signature doesn't match and a MatchError is raised
My current workaround is this:
from function_pattern_matching import MultiFunc
if isinstance(obj.method, MultiFunc):
obj.method(obj, argument)
else:
obj.method(argument)
It works and isn't too bad but I wonder if you have an idea howto improve this.
But anyway, thanks for the great work!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.