From 4e7617bda06cc8951bd7edf34afae30a3080b4f8 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Fri, 6 Sep 2024 16:44:38 +0200 Subject: [PATCH 1/4] Sample code for the article on syntactic sugar --- syntactic-sugar-python/README.md | 3 +++ syntactic-sugar-python/assertions.py | 9 +++++++++ syntactic-sugar-python/balance.py | 12 ++++++++++++ syntactic-sugar-python/circle.py | 18 ++++++++++++++++++ syntactic-sugar-python/membership.py | 5 +++++ syntactic-sugar-python/stack.py | 19 +++++++++++++++++++ syntactic-sugar-python/timing.py | 21 +++++++++++++++++++++ syntactic-sugar-python/user_input.py | 5 +++++ 8 files changed, 92 insertions(+) create mode 100644 syntactic-sugar-python/README.md create mode 100644 syntactic-sugar-python/assertions.py create mode 100644 syntactic-sugar-python/balance.py create mode 100644 syntactic-sugar-python/circle.py create mode 100644 syntactic-sugar-python/membership.py create mode 100644 syntactic-sugar-python/stack.py create mode 100644 syntactic-sugar-python/timing.py create mode 100644 syntactic-sugar-python/user_input.py diff --git a/syntactic-sugar-python/README.md b/syntactic-sugar-python/README.md new file mode 100644 index 0000000000..71228b1e5f --- /dev/null +++ b/syntactic-sugar-python/README.md @@ -0,0 +1,3 @@ +# Syntactic Sugar in Python: Make Your Code Pythonic + +This folder provides the code examples for the Real Python tutorial [Syntactic Sugar in Python: Make Your Code Pythonic](https://realpython.com/python-syntactic-sugar/). diff --git a/syntactic-sugar-python/assertions.py b/syntactic-sugar-python/assertions.py new file mode 100644 index 0000000000..a5a774dadd --- /dev/null +++ b/syntactic-sugar-python/assertions.py @@ -0,0 +1,9 @@ +number = 42 +if __debug__: + if not number > 0: + raise AssertionError("number must be positive") + +number = -42 +if __debug__: + if not number > 0: + raise AssertionError("number must be positive") diff --git a/syntactic-sugar-python/balance.py b/syntactic-sugar-python/balance.py new file mode 100644 index 0000000000..3e84d19133 --- /dev/null +++ b/syntactic-sugar-python/balance.py @@ -0,0 +1,12 @@ +debit = 300.00 +credit = 450.00 + +print( + f"Debit: ${debit:.2f}, Credit: ${credit:.2f}, Balance: ${credit - debit:.2f}" +) + +print( + "Debit: ${:.2f}, Credit: ${:.2f}, Balance: ${:.2f}".format( + debit, credit, credit - debit + ) +) diff --git a/syntactic-sugar-python/circle.py b/syntactic-sugar-python/circle.py new file mode 100644 index 0000000000..90baeceace --- /dev/null +++ b/syntactic-sugar-python/circle.py @@ -0,0 +1,18 @@ +from math import pi + + +class Circle: + def __init__(self, radius): + self.radius = radius + + def area(self): + return pi * self.radius**2 + + def perimeter(self): + return 2 * pi * self.radius + + +circle = Circle(10) +print(circle.radius) +print(circle.area()) +print(circle.perimeter()) diff --git a/syntactic-sugar-python/membership.py b/syntactic-sugar-python/membership.py new file mode 100644 index 0000000000..49af2d913f --- /dev/null +++ b/syntactic-sugar-python/membership.py @@ -0,0 +1,5 @@ +def is_member(value, iterable): + for current_value in iterable: + if current_value == value: + return True + return False diff --git a/syntactic-sugar-python/stack.py b/syntactic-sugar-python/stack.py new file mode 100644 index 0000000000..a551895da3 --- /dev/null +++ b/syntactic-sugar-python/stack.py @@ -0,0 +1,19 @@ +class Stack: + def __init__(self): + self.items = [] + + def push(self, item): + self.items.append(item) + + def pop(self): + return self.items.pop() + + def __iter__(self): + yield from self.items + + # def __iter__(self): + # return iter(self.items) + + # def __iter__(self): + # for item in self.items: + # yield item diff --git a/syntactic-sugar-python/timing.py b/syntactic-sugar-python/timing.py new file mode 100644 index 0000000000..52da01c230 --- /dev/null +++ b/syntactic-sugar-python/timing.py @@ -0,0 +1,21 @@ +import time + + +def timer(func): + def _timer(*args, **kwargs): + start = time.time() + result = func(*args, **kwargs) + end = time.time() + print(f"Execution time: {end - start:.4f} seconds") + return result + + return _timer + + +@timer +def delayed_mean(sample): + time.sleep(1) + return sum(sample) / len(sample) + + +delayed_mean([10, 2, 4, 7, 9, 3, 9, 8, 6, 7]) diff --git a/syntactic-sugar-python/user_input.py b/syntactic-sugar-python/user_input.py new file mode 100644 index 0000000000..1b0acd68ef --- /dev/null +++ b/syntactic-sugar-python/user_input.py @@ -0,0 +1,5 @@ +line = input("Type some text: ") + +while line != "stop": + print(line) + line = input("Type some text: ") From d60d0a946968836a219aceeff134a77363ff23de Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Mon, 16 Sep 2024 17:23:58 +0200 Subject: [PATCH 2/4] TR updates, first round --- syntactic-sugar-python/README.md | 4 ++-- syntactic-sugar-python/timing.py | 2 ++ syntactic-sugar-python/twice.py | 12 ++++++++++++ syntactic-sugar-python/user_input.py | 7 +++++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 syntactic-sugar-python/twice.py diff --git a/syntactic-sugar-python/README.md b/syntactic-sugar-python/README.md index 71228b1e5f..3808952bbd 100644 --- a/syntactic-sugar-python/README.md +++ b/syntactic-sugar-python/README.md @@ -1,3 +1,3 @@ -# Syntactic Sugar in Python: Make Your Code Pythonic +# Syntactic Sugar in Python: Making Your Code Pythonic -This folder provides the code examples for the Real Python tutorial [Syntactic Sugar in Python: Make Your Code Pythonic](https://realpython.com/python-syntactic-sugar/). +This folder provides the code examples for the Real Python tutorial [Syntactic Sugar in Python: Making Your Code Pythonic](https://realpython.com/python-syntactic-sugar/). diff --git a/syntactic-sugar-python/timing.py b/syntactic-sugar-python/timing.py index 52da01c230..0be2f7d75a 100644 --- a/syntactic-sugar-python/timing.py +++ b/syntactic-sugar-python/timing.py @@ -1,7 +1,9 @@ +import functools import time def timer(func): + @functools.wraps(func) def _timer(*args, **kwargs): start = time.time() result = func(*args, **kwargs) diff --git a/syntactic-sugar-python/twice.py b/syntactic-sugar-python/twice.py new file mode 100644 index 0000000000..463a27a568 --- /dev/null +++ b/syntactic-sugar-python/twice.py @@ -0,0 +1,12 @@ +class Twice: + def __init__(self, items): + self.items = list(items) + + def __iter__(self): + yield from self.items + print("Halfway there!") + yield from self.items + + +for number in Twice([1, 2, 3]): + print(f"-> {number}") diff --git a/syntactic-sugar-python/user_input.py b/syntactic-sugar-python/user_input.py index 1b0acd68ef..da135869be 100644 --- a/syntactic-sugar-python/user_input.py +++ b/syntactic-sugar-python/user_input.py @@ -3,3 +3,10 @@ while line != "stop": print(line) line = input("Type some text: ") + + +while True: + line = input("Type some text: ") + if line == "stop": + break + print(line) From c5edc5c31e435d7e167b522b007e3ced6b2f5a32 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Tue, 17 Sep 2024 12:47:19 +0200 Subject: [PATCH 3/4] TR updates, second round --- syntactic-sugar-python/README.md | 4 ++-- syntactic-sugar-python/circle.py | 4 ++-- syntactic-sugar-python/timing.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/syntactic-sugar-python/README.md b/syntactic-sugar-python/README.md index 3808952bbd..9890415043 100644 --- a/syntactic-sugar-python/README.md +++ b/syntactic-sugar-python/README.md @@ -1,3 +1,3 @@ -# Syntactic Sugar in Python: Making Your Code Pythonic +# Syntactic Sugar: Why Python is Sweet and Pythonic -This folder provides the code examples for the Real Python tutorial [Syntactic Sugar in Python: Making Your Code Pythonic](https://realpython.com/python-syntactic-sugar/). +This folder provides the code examples for the Real Python tutorial [Syntactic Sugar: Why Python is Sweet and Pythonic](https://realpython.com/python-syntactic-sugar/). diff --git a/syntactic-sugar-python/circle.py b/syntactic-sugar-python/circle.py index 90baeceace..8cb9e94273 100644 --- a/syntactic-sugar-python/circle.py +++ b/syntactic-sugar-python/circle.py @@ -8,11 +8,11 @@ def __init__(self, radius): def area(self): return pi * self.radius**2 - def perimeter(self): + def circumference(self): return 2 * pi * self.radius circle = Circle(10) print(circle.radius) print(circle.area()) -print(circle.perimeter()) +print(circle.circumference()) diff --git a/syntactic-sugar-python/timing.py b/syntactic-sugar-python/timing.py index 0be2f7d75a..484ea8a4c3 100644 --- a/syntactic-sugar-python/timing.py +++ b/syntactic-sugar-python/timing.py @@ -5,9 +5,9 @@ def timer(func): @functools.wraps(func) def _timer(*args, **kwargs): - start = time.time() + start = time.perf_counter() result = func(*args, **kwargs) - end = time.time() + end = time.perf_counter() print(f"Execution time: {end - start:.4f} seconds") return result From 61d47865347c77f814532674b08c2ba8c2b3f98c Mon Sep 17 00:00:00 2001 From: gahjelle Date: Wed, 18 Sep 2024 08:44:04 +0200 Subject: [PATCH 4/4] Add more output --- syntactic-sugar-python/balance.py | 13 +++++++++++-- syntactic-sugar-python/membership.py | 6 ++++++ syntactic-sugar-python/stack.py | 11 +++++++++++ syntactic-sugar-python/timing.py | 2 +- syntactic-sugar-python/user_input.py | 7 +++++++ 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/syntactic-sugar-python/balance.py b/syntactic-sugar-python/balance.py index 3e84d19133..83fcaf468d 100644 --- a/syntactic-sugar-python/balance.py +++ b/syntactic-sugar-python/balance.py @@ -1,5 +1,5 @@ -debit = 300.00 -credit = 450.00 +debit = 300 +credit = 450 print( f"Debit: ${debit:.2f}, Credit: ${credit:.2f}, Balance: ${credit - debit:.2f}" @@ -10,3 +10,12 @@ debit, credit, credit - debit ) ) + +print( + "Debit: $" + + format(debit, ".2f") + + ", Credit: $" + + format(credit, ".2f") + + ", Balance: $" + + format(credit - debit, ".2f") +) diff --git a/syntactic-sugar-python/membership.py b/syntactic-sugar-python/membership.py index 49af2d913f..0178e01c83 100644 --- a/syntactic-sugar-python/membership.py +++ b/syntactic-sugar-python/membership.py @@ -3,3 +3,9 @@ def is_member(value, iterable): if current_value == value: return True return False + + +print(is_member(5, [1, 2, 3, 4, 5])) +print(not is_member(5, [1, 2, 3, 4, 5])) +print(is_member(100, [1, 2, 3, 4, 5])) +print(not is_member(100, [1, 2, 3, 4, 5])) diff --git a/syntactic-sugar-python/stack.py b/syntactic-sugar-python/stack.py index a551895da3..edb8180808 100644 --- a/syntactic-sugar-python/stack.py +++ b/syntactic-sugar-python/stack.py @@ -17,3 +17,14 @@ def __iter__(self): # def __iter__(self): # for item in self.items: # yield item + + +stack = Stack() +stack.push("one") +stack.push("two") +stack.push("three") + +for item in stack: + print(item) + +print(stack.pop()) diff --git a/syntactic-sugar-python/timing.py b/syntactic-sugar-python/timing.py index 484ea8a4c3..ed2ed08678 100644 --- a/syntactic-sugar-python/timing.py +++ b/syntactic-sugar-python/timing.py @@ -20,4 +20,4 @@ def delayed_mean(sample): return sum(sample) / len(sample) -delayed_mean([10, 2, 4, 7, 9, 3, 9, 8, 6, 7]) +print(delayed_mean([10, 2, 4, 7, 9, 3, 9, 8, 6, 7])) diff --git a/syntactic-sugar-python/user_input.py b/syntactic-sugar-python/user_input.py index da135869be..8b997ed357 100644 --- a/syntactic-sugar-python/user_input.py +++ b/syntactic-sugar-python/user_input.py @@ -1,3 +1,9 @@ +# With assignment operator +while (line := input("Type some text: ")) != "stop": + print(line) + + +# With assignment before loop line = input("Type some text: ") while line != "stop": @@ -5,6 +11,7 @@ line = input("Type some text: ") +# With condition inside loop while True: line = input("Type some text: ") if line == "stop":