Skip to content

Commit ff4f6f9

Browse files
add examples
1 parent 3515258 commit ff4f6f9

File tree

3 files changed

+87
-17
lines changed

3 files changed

+87
-17
lines changed

examples/example_1_calculator.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
example_1_calculator.py
3+
4+
Basic example usage for the Calculator and Operation classes. This script demonstrates:
5+
6+
- Initializing a Calculator instance.
7+
- Registering and applying a custom unary operation (square).
8+
- Registering and applying a binary operation (addition) from the API.
9+
- Chaining multiple operations together.
10+
- Composing unary operations to create a new callable.
11+
12+
Each operation is tested and the results are printed to verify correct behavior.
13+
"""
14+
15+
from python_project_template import Calculator, Operation
16+
17+
from python_project_template import ADD, NEG
18+
19+
20+
# Initialize new calculator
21+
calc = Calculator()
22+
23+
24+
# Adding and using a unary opeartion
25+
def square(x):
26+
return x * x
27+
28+
29+
sqr_op = Operation(name="sqr", func=square)
30+
calc.register(sqr_op)
31+
sq_n = calc.apply("sqr", 4)
32+
sq_str = 'calc.apply("sqr", 4)'
33+
print(f"{sq_str} = {sq_n}")
34+
35+
# Adding an opeartion from the API to current calculator
36+
add_op = Operation(name="add", func=ADD, arity=2)
37+
calc.register(add_op)
38+
add_result = calc.apply("add", 2, 3)
39+
add_str = 'calc.apply("add", 2, 3)'
40+
print(f"{add_str} = {add_result}")
41+
42+
# Chaining opeartions
43+
chain_res = calc.chain(["add", 5, "sqr"], initial=2)
44+
chain_str = "calc.chain(['add', 5, 'sqr'], initial=2)"
45+
print(f"{chain_str} = {chain_res}")
46+
47+
# Generate a Callable from composing unary operations (computed right-to-left)
48+
neg_op = Operation(name="neg", func=NEG, arity=1)
49+
calc.register(neg_op)
50+
sqr_neg = calc.compose(["neg", "sqr"], left_to_right=False)
51+
comp_test = sqr_neg(16)
52+
comp_str = "sqr_neg(16)"
53+
print(f"{comp_str} = {comp_test}")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
example_2_replace_operation.py
3+
4+
Example on registering and replacing an operation with a new one (by the same name) in the Calculator class.
5+
"""
6+
7+
from python_project_template.calculator import Calculator
8+
from python_project_template.operations import Operation
9+
10+
11+
if __name__ == "__main__":
12+
calc = Calculator()
13+
14+
def add_v1(a, b):
15+
return a + b
16+
17+
def add_v2(a, b):
18+
return (a + b) * 10
19+
20+
calc.register(Operation(name="add", func=add_v1))
21+
assert calc.apply("add", 1, 2) == 3
22+
23+
# replace existing operation
24+
calc.register(Operation(name="add", func=add_v2), replace=True)
25+
assert calc.apply("add", 1, 2) == 30

examples/write_logo.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
1-
#!/usr/bin/env python3
21
"""
3-
Generate a simple rectangular SVG logo that fits the text with symmetric padding.
4-
5-
This is intentionally dependency-free (writes plain SVG XML).
6-
Tweak `text`, `font_size`, `padding`, `rx`, and `fill` as needed.
2+
Generate the py-project-template logo.
73
"""
84

5+
# SETTINGS
96
text = "py-project-template"
107
font_family = "Segoe UI, Roboto, Arial, sans-serif"
11-
font_size = 28 # px
12-
padding = 16 # px on left and right
13-
height = 64 # svg height in px
8+
font_size = 28 # in px
9+
padding = 16 # on left and right
10+
height = 64 # final image height in px
1411
rx = 6 # corner radius
1512
fill = "#2b6cb0"
1613
text_color = "#ffffff"
1714

18-
# Simple width estimate: average character width ~ 0.6 * font_size
19-
avg_char_width = 0.45 * font_size
20-
text_width = int(len(text) * avg_char_width)
21-
15+
# GENERATE
16+
text_width = int(len(text) * 0.45 * font_size) # Simple width estimate
2217
canvas_width = padding * 2 + text_width
23-
# Add a little extra safety margin
24-
canvas_width += 4
2518

26-
text_x = padding
27-
# baseline: approximate vertical position so text sits nicely in the rectangle
28-
text_y = int(height * 0.7)
19+
text_x = padding # horizontal text position
20+
text_y = int(height * 0.7) # vertical text position
2921

3022
svg = f"""<svg xmlns="http://www.w3.org/2000/svg" width="{canvas_width}" height="{height}" viewBox="0 0 {canvas_width} {height}" role="img" aria-label="python project template logo">
3123
<rect width="{canvas_width}" height="{height}" rx="{rx}" fill="{fill}" />

0 commit comments

Comments
 (0)