Skip to content

Commit

Permalink
Adding examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dluman committed Jul 31, 2024
1 parent d48ea36 commit e4aeff8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,57 @@ Find this tool on`PyPI`: `pip install graffito`

## Usage

`TODO`
Given a program:
```python
from graffito.tags import graffiti, get_tags

func_test = graffiti

@func_test
def test():
pass

class TestClass:

method_test = graffiti

@method_test
def class_test(self):
pass

print(get_tags(TestClass))
print(get_tags(test))
```
Executing the program will return:
```bash
{'class_test': ['method_test']}
{'test': ['func_test']}
```
This is a bespoke module. However, let's say that we wanted to characterize functions of a certain
signature with a decorator so that we could handle them the same way. Let's say:
```python
from graffito.tags import graffiti, get_tags

class TestClass:

two_var_sig = graffiti

@two_var_sig
def function_a(self, input_a, input_b):
print(f"{input_a}, {input_b}")

@two_var_sig
def function_b(self, input_a, input_b):
print(f"{input_b}, {input_a}")

def function_not_like_them(self, input_a):
print(f"SIKE!")

t = TestClass()
tags = get_tags(TestClass)
for tag in tags:
if "two_var_sig" in tags[tag]:
getattr(t, tag)(1,2)
```
I know it seems really arbitrary, but it helped me write a simple computer emulator and I think I
can develop it to be useful outside of my silly context. Let's find out.
6 changes: 2 additions & 4 deletions src/graffito/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@
# of wrapped functions:
# https://stackoverflow.com/questions/4887081/get-the-name-of-a-decorated-function

# The following only happens for classes if the class itself is decorated. That's a
# bit of a bummer. Cant we force it?
def graffiti(f):
@wraps(f)
def wrapper(*args, **kwargs):
return f(*args, **kwargs)
try:
tags = set_tags(f)
tags = get_tags(f)
setattr(wrapper, "graffiti", tags)
except IndentationError as e:
pass
return wrapper

def set_tags(obj):
def get_tags(obj):
# Is it polymorphic? Kinda.
target = obj
tags = {}
Expand Down

0 comments on commit e4aeff8

Please sign in to comment.