-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreusability.qmd
More file actions
129 lines (91 loc) · 3.79 KB
/
reusability.qmd
File metadata and controls
129 lines (91 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
filters:
- pyodide
---
# Reusability
{{< video https://youtu.be/sPpbyXyRlQI >}}
Let’s look at an example. Here’s a file I created called Penchord_Wizardry.py.
```{python}
import random
# Class defining a Penchordian
class Penchordian :
def __init__(self, name):
self.name = name
self.is_a_wizard = False
def write_model(self, type_of_model):
print (f"{self.name} is now writing a {type_of_model} model.")
def tell_joke(self, prob_success):
if random.uniform(0,1) < prob_success:
print (f"{self.name} attempted a joke. People loved it!")
else:
print (f"{self.name} attempted a joke. It fell flat.")
# Function to turn someone into a wizard
# Input subject must be an object with a "name" string attribute and a
# "is_a_wizard" boolean attribute
def turn_into_a_wizard(subject):
subject.is_a_wizard = True
print (f"{subject.name} is now a wizard.")
```
Then I'm going to create a new file in the **same folder** as `Penchord_Wizardry.py`.
:::{.callout-tip}
It is possible to call in functions from a Python file stored in a different folder or subfolder - but it starts getting quite complicated to navigate to them in the code, so we won't cover that today.
:::
At the beginning of my new file I'm going to `import Penchord_Wizardry` to get access to the functions from `Penchord_Wizardry.py`.
```{python}
# import the entire Penchord_Wizardry module (the Penchordian class and the
# turn_into_a_wizard function)
import Penchord_Wizardry
import random
list_of_penchordian_names = ["Dan", "Sammi", "Kerry", "Mike", "Anna", "Tom",
"Amy C", "Amy H", "Chrissie"]
# Randomly select three PenCHORDian names
# random.sample selects three elements from a list without replacement
# (if you want values to be able to be repicked (replacement), use
# random.choices)
chosen_penchordian_names = random.sample(list_of_penchordian_names, 3)
list_of_penchordians = []
# Create some Penchordian objects using the definition from the imported
# module
for name in chosen_penchordian_names:
list_of_penchordians.append(Penchord_Wizardry.Penchordian(name))
# Call a couple of the class methods on the three created Penchordian objects
for penchordian in list_of_penchordians:
penchordian.write_model("Discrete Event Simulation")
penchordian.tell_joke(0.1)
```
I can also choose just to import the bit(s) of the module I need, rather than the whole thing.
Here we don't import the class `Penchordian`, but we do import the function `turn_into_a_wizard`.
:::{.callout-tip}
Note that because we've imported the specific function by name, we can just then use `turn_into_a_wizard`.
:::
```{python}
# just import the turn_into_a_wizard function
from Penchord_Wizardry import turn_into_a_wizard
# Define a new class called HSMA, which has two attributes - a name, and an
# is_a_wizard boolean
class HSMA:
def __init__(self, name):
self.name = name
self.is_a_wizard = False
# Create a new HSMA object, whose name is Gandalf
my_promising_HSMA = HSMA("Gandalf")
# Turn Gandalf into a wizard using the function we imported from the
# Penchord_Wizardry module
turn_into_a_wizard(my_promising_HSMA)
```
In comparison, if we imported the whole module instead, our code would look like this:
```{python}
# just import the turn_into_a_wizard function
import Penchord_Wizardry
# Define a new class called HSMA, which has two attributes - a name, and an
# is_a_wizard boolean
class HSMA:
def __init__(self, name):
self.name = name
self.is_a_wizard = False
# Create a new HSMA object, whose name is Gandalf
my_promising_HSMA = HSMA("Gandalf")
# Turn Gandalf into a wizard using the function we imported from the
# Penchord_Wizardry module
Penchord_Wizardry.turn_into_a_wizard(my_promising_HSMA)
```