Skip to content

Commit

Permalink
Updated README.md + minifix
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaIng committed Nov 9, 2020
1 parent 4dc1e22 commit 2bab93c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ with match(event.get()) as m:
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
#### 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`

Expand All @@ -63,11 +63,11 @@ You still have to use `m.`, but at least you don't have to duplicate the class n

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
#### injecting

This is for those who don't want to type `m.` everywhere.
```python
from pattern_matching.injecting import match
from pattern_matching.injecting import match, case


with match(event.get()):
Expand All @@ -90,7 +90,7 @@ This is will only work on Python3.9. It might also randomly break with debuggers
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
#### full_magic

This is the one that is as close as possible to the syntax proposed in PEP-634

Expand Down Expand Up @@ -147,5 +147,22 @@ with match(n):
```
This doesn't have to be done always, but it is a good first attempt if you get weird error messages.


### Guards

Guards are not directly implemented, but are supported via a simple `and` that can be added to a case:

```python
from pattern_matching.injecting import match, case

with match(p):
if case('(x, y)') and x == y:
print("X=Y, at", x)
else:
print("Not on a diagonal")
```

This works similar for all options

## License
[MIT](https://choosealicense.com/licenses/mit/)
2 changes: 2 additions & 0 deletions pattern_matching/withhacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def __call__(self, frame, event, arg):
exc_to_reraise = e
with _trace_lock:
del self.injected_functions[event]
if event == 'opcode':
self.frame.f_trace_opcodes = self.orig_trace_opcodes
if len(self.injected_functions) > 0:
if exc_to_reraise is not None:
raise exc_to_reraise
Expand Down

0 comments on commit 2bab93c

Please sign in to comment.