-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
302 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Copyright © 2017 Erez Shinan | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
# Pattern matching PEP 634 | ||
|
||
This is a python package implementing pattern-matching similar to what [PEP-634](https://www.python.org/dev/peps/pep-0634/) proposes. | ||
|
||
## Installation | ||
|
||
Use the package manager [pip](https://pip.pypa.io/en/stable/) to install pattern-matching-pep634. | ||
|
||
```bash | ||
pip install pattern-matching-pep634 | ||
``` | ||
|
||
## Usage | ||
|
||
There are 4 different implementations of pattern matching in here. | ||
|
||
The safest one is this: | ||
```python | ||
from pattern_matching import Matcher | ||
|
||
match = Matcher(Click, KeyPress, Quit) | ||
|
||
with match(event.get()) as m: | ||
if m.case('Click(position=(x, y))'): | ||
handle_click_at(m.x, m.y) | ||
elif m.case('KeyPress(key_name="Q") | Quit()'): | ||
m.game.quit() | ||
elif m.case('KeyPress(key_name="up arrow")'): | ||
m.game.go_north() | ||
elif m.case('KeyPress()'): | ||
pass # Ignore other keystrokes | ||
elif m.case('other_event'): | ||
raise ValueError(f"Unrecognized event: {m.other_event}") | ||
``` | ||
|
||
(This is based on an example in [PEP-636](https://www.python.org/dev/peps/pep-0636/)) | ||
|
||
Note how you have to specify which names you will be using, and how you always have to use `m.` to access the capture values. | ||
This is the so called `no_magic` implementation. This implementation will work even in Python3.7 and other implementations than CPython that support the same features as 3.7 | ||
|
||
### auto_lookup | ||
|
||
If you don't want to specify which classes you will be using, but don't want to have the pattern matching messing with the locals, you can use `auto_lookup` | ||
|
||
```python | ||
from pattern_matching.auto_lookup import match | ||
|
||
|
||
with match(event.get()) as m: | ||
if m.case('Click(position=(x, y))'): | ||
handle_click_at(m.x, m.y) | ||
elif m.case('KeyPress(key_name="Q") | Quit()'): | ||
m.game.quit() | ||
elif m.case('KeyPress(key_name="up arrow")'): | ||
m.game.go_north() | ||
elif m.case('KeyPress()'): | ||
pass # Ignore other keystrokes | ||
elif m.case('other_event'): | ||
raise ValueError(f"Unrecognized event: {m.other_event}") | ||
``` | ||
|
||
You still have to use `m.`, but at least you don't have to duplicate the class names and carry around a `Matcher` instance. | ||
|
||
Note that this might have weird edge cases and fail for some reason sometimes. (Most notably conflict between what you think is visible in a function vs. what really is) | ||
|
||
### injecting | ||
|
||
This is for those who don't want to type `m.` everywhere. | ||
```python | ||
from pattern_matching.injecting import match | ||
|
||
|
||
with match(event.get()): | ||
if case('Click(position=(x, y))'): | ||
handle_click_at(x, y) | ||
elif case('KeyPress(key_name="Q") | Quit()'): | ||
game.quit() | ||
elif case('KeyPress(key_name="up arrow")'): | ||
game.go_north() | ||
elif case('KeyPress()'): | ||
pass # Ignore other keystrokes | ||
elif case('other_event'): | ||
raise ValueError(f"Unrecognized event: {other_event}") | ||
``` | ||
|
||
This will 'infect' the name space and put the captured names into it. It also does `auto_lookup`. | ||
|
||
This is will only work on Python3.9. It might also randomly break with debuggers/coverage/tracing tools. | ||
|
||
Note that this heavily suffers from the problem of what locals are defined and what aren't, e.g. the problem of where names are looked up. | ||
|
||
|
||
### full_magic | ||
|
||
This is the one that is as close as possible to the syntax proposed in PEP-634 | ||
|
||
```python | ||
from pattern_matching.full_magic import match | ||
|
||
|
||
with match(event.get()): | ||
if Click(position=(x, y)): | ||
handle_click_at(x, y) | ||
elif KeyPress(key_name="Q") | Quit(): | ||
game.quit() | ||
elif KeyPress(key_name="up arrow"): | ||
game.go_north() | ||
elif KeyPress(): | ||
pass # Ignore other keystrokes | ||
elif other_event: | ||
raise ValueError(f"Unrecognized event: {other_event}") | ||
``` | ||
|
||
(only differences to PEP-634 is `with match` instead of match, `if`/`elif` instead of `case` and `:=` instead of `as`) | ||
|
||
This does source-code analysis to figure out what cases to take and which names do bind. This is also Python3.9 only, and might break with any minor release of python. But that is unlikely. | ||
|
||
Note: Sometimes, the small amount of optimization that python does can still break this: | ||
|
||
```python | ||
from pattern_matching.full_magic import match | ||
|
||
|
||
with match(n): | ||
if 1 | 2: | ||
print("Smaller than three") | ||
elif 3: | ||
print("Equal to three") | ||
elif _: | ||
print("Bigger than three") | ||
``` | ||
|
||
Python's peephole optimizer will (normally) correctly figure out that only the first branch can be taken and throw away the rest of the code. | ||
This means that `match` can not correctly jump to the other lines. To circumvent the optimizer, `c@` ('case') can be added to prevent the optimization | ||
|
||
```python | ||
from pattern_matching.full_magic import match | ||
|
||
|
||
with match(n): | ||
if c@ 1 | 2: | ||
print("Smaller than three") | ||
elif c@ 3: | ||
print("Equal to three") | ||
elif c@ _: | ||
print("Bigger than three") | ||
``` | ||
This doesn't have to be done always, but it is a good first attempt if you get weird error messages. | ||
|
||
## License | ||
[MIT](https://choosealicense.com/licenses/mit/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
from .no_magic import Matcher | ||
|
||
from pattern_matching.pattern_engine import str2pattern | ||
|
||
|
||
class _Match: | ||
def __init__(self, matcher: Matcher, value: Any): | ||
self.__matcher__ = matcher | ||
self.__value__ = value | ||
self.__vars__ = None | ||
|
||
def __getattr__(self, item): | ||
return self.__vars__[item] | ||
|
||
def __matmul__(self, other: str): | ||
return self.case(other) | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
return False | ||
|
||
def case(self, pattern: str): | ||
assert isinstance(pattern, str) | ||
pt = str2pattern(pattern) | ||
var = pt.match(self.__value__, self.__matcher__._get) | ||
if var is not None: | ||
self.__vars__ = var | ||
return True | ||
else: | ||
return False | ||
|
||
def __call__(self, pattern: str): | ||
return self.case(pattern) | ||
|
||
|
||
class Matcher: | ||
""" | ||
A portable match statement. | ||
This is what should always be used. It forces you to clearly define all accessible names, including constants. | ||
""" | ||
def __init__(self, *args, **kwargs): | ||
self.__names__ = kwargs | ||
for a in args: | ||
try: | ||
self.__names__[a.__name__] = a | ||
except AttributeError: | ||
raise ValueError("Can not use values that don't have a `__name__` attribute (functions, classes) without specifying a name") | ||
|
||
def _get(self, name: str): | ||
try: | ||
return self.__names__[name] | ||
except KeyError: | ||
raise NameError | ||
|
||
def __call__(self, value: Any): | ||
return _Match(self, value) | ||
__version__ = "0.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import annotations | ||
from typing import Any | ||
|
||
from pattern_matching.pattern_engine import str2pattern | ||
|
||
|
||
class _Match: | ||
def __init__(self, matcher: Matcher, value: Any): | ||
self.__matcher__ = matcher | ||
self.__value__ = value | ||
self.__vars__ = None | ||
|
||
def __getattr__(self, item): | ||
return self.__vars__[item] | ||
|
||
def __matmul__(self, other: str): | ||
return self.case(other) | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
return False | ||
|
||
def case(self, pattern: str): | ||
assert isinstance(pattern, str) | ||
pt = str2pattern(pattern) | ||
var = pt.match(self.__value__, self.__matcher__._get) | ||
if var is not None: | ||
self.__vars__ = var | ||
return True | ||
else: | ||
return False | ||
|
||
def __call__(self, pattern: str): | ||
return self.case(pattern) | ||
|
||
|
||
class Matcher: | ||
""" | ||
A portable match statement. | ||
This is what should always be used. It forces you to clearly define all accessible names, including constants. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.__names__ = kwargs | ||
for a in args: | ||
try: | ||
self.__names__[a.__name__] = a | ||
except AttributeError: | ||
raise ValueError("Can not use values that don't have a `__name__` attribute (functions, classes) without specifying a name") | ||
|
||
def _get(self, name: str): | ||
try: | ||
return self.__names__[name] | ||
except KeyError: | ||
raise NameError | ||
|
||
def __call__(self, value: Any): | ||
return _Match(self, value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[metadata] | ||
version = attr: pattern_matching.__version__ |
Oops, something went wrong.