Skip to content

Commit df5edc3

Browse files
authored
Merge branch 'master' into python-protocol
2 parents a4560d6 + cfdaba7 commit df5edc3

File tree

17 files changed

+283
-7
lines changed

17 files changed

+283
-7
lines changed

python-built-in-functions/fibonacci.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
class Fibonacci:
2-
def __init__(self):
3-
self._cache = [0, 1]
1+
class Fibonaccish:
2+
def __init__(self, initial_value=1):
3+
self._cache = [0, initial_value]
44

55
def __call__(self, index):
66
if index < len(self._cache):
77
fib_number = self._cache[index]
8-
print(f"{fib_number} id = {id(fib_number)}")
8+
print(f"{index} {fib_number} id = {id(fib_number)}")
99
else:
1010
fib_number = self(index - 1) + self(index - 2)
1111
self._cache.append(fib_number)

python-built-in-functions/progress.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
def progress(percent=0, width=30):
2-
end = ""
3-
if percent == 100:
4-
end = "\n"
2+
end = "" if percent < 100 else "\n"
53
left = width * percent // 100
64
right = width - left
75
print(

python-function-names/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# How Do You Choose Python Function Names?
2+
3+
This folder contains source code for the Real Python tutorial [How Do You Choose Python Function Names?](https://realpython.com/python-function-names/).
4+
5+
For additional explanation, check out the associated tutorial.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class BankAccount:
2+
def __init__(self, account_number, balance):
3+
self.account_number = account_number
4+
self.balance = balance
5+
6+
def __repr__(self):
7+
return f"{type(self).__name__}({self.account_number}, {self.balance})"
8+
9+
def _verify_funds(self, amount):
10+
return self.balance >= amount
11+
12+
def _deduct_funds(self, amount):
13+
self.balance -= amount
14+
15+
def _add_funds(self, amount):
16+
self.balance += amount
17+
18+
def get_balance(self):
19+
return self.balance
20+
21+
def withdraw(self, amount):
22+
if self._verify_funds(amount):
23+
self._deduct_funds(amount)
24+
return True
25+
return False
26+
27+
def deposit(self, amount):
28+
self._add_funds(amount)
29+
return True
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Poorly-named function, parameter, and variable
2+
def init(n):
3+
return " ".join(f"{i[0]}." for i in n.split())
4+
5+
6+
# Descriptive names improve readability
7+
def get_initials(full_name):
8+
return " ".join(f"{name[0]}." for name in full_name.split())
9+
10+
11+
print(get_initials("James Clerk Maxwell"))
12+
# OUTPUT: J. C. M.
13+
print(get_initials("Richard Feynman"))
14+
# OUTPUT: R. F.

python-json/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Working With JSON Data in Python
2+
3+
This folder provides the code examples for the article [Working With JSON Data in Python](https://realpython.com/python-json).

python-json/dog_friend.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "Mitch",
3+
"age": 6.5
4+
}

python-json/hello_frieda.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name": "Frieda", "is_dog": true, "hobbies": ["eating", "sleeping", "barking"], "age": 8, "address": {"work": null, "home": ["Berlin", "Germany"]}, "friends": [{"name": "Philipp", "hobbies": ["eating", "sleeping", "reading"]}, {"name": "Mitch", "hobbies": ["running", "snacking"]}]}

python-json/hello_frieda.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import json
2+
3+
dog_data = {
4+
"name": "Frieda",
5+
"is_dog": True,
6+
"hobbies": [
7+
"eating",
8+
"sleeping",
9+
"barking",
10+
],
11+
"age": 8,
12+
"address": {
13+
"work": None,
14+
"home": (
15+
"Berlin",
16+
"Germany",
17+
),
18+
},
19+
"friends": [
20+
{
21+
"name": "Philipp",
22+
"hobbies": [
23+
"eating",
24+
"sleeping",
25+
"reading",
26+
],
27+
},
28+
{
29+
"name": "Mitch",
30+
"hobbies": [
31+
"running",
32+
"snacking",
33+
],
34+
},
35+
],
36+
}
37+
38+
with open("hello_frieda.json", mode="w") as write_file:
39+
json.dump(dog_data, write_file)

python-json/hello_world.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"greeting": "Hello, world!"
3+
}

0 commit comments

Comments
 (0)