Skip to content

Commit ced1603

Browse files
authored
Merge branch 'master' into python-namedtuple
2 parents 521a921 + f9d23e8 commit ced1603

File tree

12 files changed

+199
-0
lines changed

12 files changed

+199
-0
lines changed

python-print/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Your Guide to the Python print() Function
2+
3+
This folder contains sample code for the Real Python tutorial [Your Guide to the Python print() Function](https://realpython.com/python-print/).

python-print/ansi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def esc(*codes):
2+
return f"\033[{';'.join(str(code) for code in codes)}m"
3+
4+
5+
print(esc(31, 1, 4) + "really" + esc(0) + " important")

python-print/calculator.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import readline # noqa
2+
3+
print('Type "help", "exit", "add a [b [c ...]]"')
4+
while True:
5+
command, *arguments = input("~ ").split(" ")
6+
if len(command) > 0:
7+
if command.lower() == "exit":
8+
break
9+
elif command.lower() == "help":
10+
print("This is help.")
11+
elif command.lower() == "add":
12+
print(sum(map(int, arguments)))
13+
else:
14+
print("Unknown command")

python-print/countdown.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import time
2+
3+
num_seconds = 3
4+
for countdown in reversed(range(num_seconds + 1)):
5+
if countdown > 0:
6+
print(countdown, end="...", flush=True)
7+
time.sleep(1)
8+
else:
9+
print("Go!")

python-print/custom_class.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Person:
2+
def __init__(self, name, age):
3+
self.name = name
4+
self.age = age
5+
6+
def __str__(self):
7+
class_name = type(self).__name__
8+
return f"{class_name}(name={self.name!r}, age={self.age!r})"
9+
10+
11+
jdoe = Person("John Doe", 42)
12+
print(jdoe)

python-print/morse_code.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# flake8: noqa
2+
3+
from time import sleep
4+
5+
speed = 0.1
6+
7+
8+
def signal(duration, symbol):
9+
sleep(duration)
10+
print(symbol, end="", flush=True)
11+
12+
13+
dot = lambda: signal(speed, \a")
14+
dash = lambda: signal(3 * speed, "−\a")
15+
symbol_space = lambda: signal(speed, "")
16+
letter_space = lambda: signal(3 * speed, "")
17+
word_space = lambda: signal(7 * speed, " ")
18+
19+
while True:
20+
dot()
21+
symbol_space()
22+
dot()
23+
symbol_space()
24+
dot()
25+
letter_space()
26+
27+
dash()
28+
symbol_space()
29+
dash()
30+
symbol_space()
31+
dash()
32+
letter_space()
33+
34+
dot()
35+
symbol_space()
36+
dot()
37+
symbol_space()
38+
dot()
39+
word_space()

python-print/progress.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from time import sleep
2+
3+
4+
def progress(percent=0, width=30):
5+
left = width * percent // 100
6+
right = width - left
7+
print(
8+
"\r[",
9+
"#" * left,
10+
" " * right,
11+
"]",
12+
f" {percent:.0f}%",
13+
sep="",
14+
end="",
15+
flush=True,
16+
)
17+
18+
19+
for i in range(101):
20+
progress(i)
21+
sleep(0.1)

python-print/snake.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import curses
2+
import time
3+
4+
5+
def main(screen):
6+
curses.curs_set(0) # Hide the cursor
7+
screen.nodelay(True) # Don't block I/O calls
8+
9+
directions = {
10+
curses.KEY_UP: (-1, 0),
11+
curses.KEY_DOWN: (1, 0),
12+
curses.KEY_LEFT: (0, -1),
13+
curses.KEY_RIGHT: (0, 1),
14+
}
15+
16+
direction = directions[curses.KEY_RIGHT]
17+
snake = [(0, i) for i in reversed(range(20))]
18+
19+
while True:
20+
screen.erase()
21+
22+
# Draw the snake
23+
screen.addstr(*snake[0], "@")
24+
for segment in snake[1:]:
25+
screen.addstr(*segment, "*")
26+
27+
# Move the snake
28+
snake.pop()
29+
snake.insert(0, tuple(map(sum, zip(snake[0], direction))))
30+
31+
# Change direction on arrow keystroke
32+
direction = directions.get(screen.getch(), direction)
33+
34+
screen.refresh()
35+
time.sleep(0.1)
36+
37+
38+
if __name__ == "__main__":
39+
curses.wrapper(main)

python-print/spinning_wheel.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from itertools import cycle
2+
from time import sleep
3+
4+
for frame in cycle(r"-\|/-\|/"):
5+
print("\r", frame, sep="", end="", flush=True)
6+
sleep(0.2)

python-print/test_print.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
4+
5+
class TestPrint(TestCase):
6+
@patch("builtins.print")
7+
def test_print(self, mock_print):
8+
print("Not a real print()")
9+
mock_print.assert_called_with("Not a real print()")
10+
11+
@patch("builtins.print")
12+
def test_greet(self, mock_print):
13+
greet("John")
14+
mock_print.assert_called_with("Hello, John!")
15+
16+
17+
def greet(name):
18+
print(f"Hello, {name}!")

0 commit comments

Comments
 (0)