Skip to content

Commit 8e4cc3f

Browse files
lpozogahjelle
andauthored
Python constants: code examples (#275)
* Python constants: code examples * Fix linter issues * More linter issues * Add a README.md file * Final QA updates Co-authored-by: Geir Arne Hjelle <[email protected]>
1 parent 38cdf0d commit 8e4cc3f

20 files changed

+270
-0
lines changed

python-constants/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Constants: Improve Your Code's Maintainability
2+
3+
This folder provides the code examples for the article [Python Constants: Improve Your Code's Maintainability](https://realpython.com/python-constants/).

python-constants/calculations/calc/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# calculations.py
2+
3+
"""This module implements custom calculations."""
4+
5+
# Imports go here...
6+
# import numpy as np
7+
8+
from . import constants
9+
10+
11+
# Your custom calculations start here...
12+
def circular_land_area(radius):
13+
return constants.PI * radius**2
14+
15+
16+
def future_value(present_value, interest_rate, years):
17+
return present_value * constants.EULER_NUMBER ** (interest_rate * years)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# constants.py
2+
3+
"""This module defines project-level constants."""
4+
5+
PI = 3.141592653589793
6+
EULER_NUMBER = 2.718281828459045
7+
TAU = 6.283185307179586
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# calculations1.py
2+
3+
"""This module implements custom calculations."""
4+
5+
# Imports go here...
6+
# import numpy as np
7+
8+
# Constants go here...
9+
PI = 3.141592653589793
10+
EULER_NUMBER = 2.718281828459045
11+
TAU = 6.283185307179586
12+
13+
14+
# Your custom calculations start here...
15+
def circular_land_area(radius):
16+
return PI * radius**2
17+
18+
19+
def future_value(present_value, interest_rate, years):
20+
return present_value * EULER_NUMBER ** (interest_rate * years)
21+
22+
23+
# ...
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# calculations3.py
2+
3+
"""This module implements custom calculations."""
4+
5+
# Imports go here...
6+
from configparser import ConfigParser
7+
8+
# import numpy as np
9+
10+
constants = ConfigParser()
11+
constants.read("path/to/constants.ini")
12+
13+
14+
# Your custom calculations start here...
15+
def circular_land_area(radius):
16+
return float(constants.get("CONSTANTS", "PI")) * radius**2
17+
18+
19+
def future_value(present_value, interest_rate, years):
20+
return (
21+
present_value * float(constants.get("CONSTANTS", "EULER_NUMBER"))
22+
) ** (interest_rate * years)
23+
24+
25+
# ...
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; constants.ini
2+
3+
[CONSTANTS]
4+
PI=3.141592653589793
5+
EULER_NUMBER=2.718281828459045
6+
TAU=6.283185307179586

python-constants/circle1.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# circle1.py
2+
3+
4+
class Circle:
5+
def __init__(self, radius):
6+
self.radius = radius
7+
8+
def area(self):
9+
return 3.14 * self.radius**2
10+
11+
def perimeter(self):
12+
return 2 * 3.14 * self.radius
13+
14+
def projected_volume(self):
15+
return 4 / 3 * 3.14 * self.radius**3
16+
17+
def __repr__(self):
18+
return f"{self.__class__.__name__}(radius={self.radius})"

python-constants/circle2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# circle2.py
2+
3+
PI = 3.14
4+
5+
6+
class Circle:
7+
def __init__(self, radius):
8+
self.radius = radius
9+
10+
def area(self):
11+
return PI * self.radius**2
12+
13+
def perimeter(self):
14+
return 2 * PI * self.radius
15+
16+
def projected_volume(self):
17+
return 4 / 3 * PI * self.radius**3
18+
19+
def __repr__(self):
20+
return f"{self.__class__.__name__}(radius={self.radius})"

python-constants/circle3.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# circle3.py
2+
3+
import math
4+
5+
6+
class Circle:
7+
def __init__(self, radius):
8+
self.radius = radius
9+
10+
def area(self):
11+
return math.pi * self.radius**2
12+
13+
def perimeter(self):
14+
return 2 * math.pi * self.radius
15+
16+
def projected_volume(self):
17+
return 4 / 3 * math.pi * self.radius**3
18+
19+
def __repr__(self):
20+
return f"{self.__class__.__name__}(radius={self.radius})"

0 commit comments

Comments
 (0)