Skip to content

Commit a56bfa2

Browse files
committed
modules and basics of packages
1 parent 122e425 commit a56bfa2

5 files changed

Lines changed: 106 additions & 16 deletions

File tree

python/06-modules.py

Lines changed: 94 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,44 +63,122 @@
6363

6464
########## From module import Something ##########
6565

66+
6667
#in the previous section we showed how to import something.
6768
import random
6869

6970
print(type(random))
7071
#Doing this requires us to access the module using the dot operator
7172

73+
#As an alternative, we can import one specific thing from the module
74+
75+
from random import randint, seed
76+
print("from random import randit:", randint(5,5)) #always 5 lol
77+
78+
#This replaces anything locally called randint:
79+
randint = "SUPER IMPORTANT DATA DON'T DELETE!!!!"
80+
from random import randint
7281

82+
print("RIP data:", randint)
7383

7484

7585
########## Alias an import ##########
86+
87+
88+
import random as r
89+
90+
print("r.randint:", r.randint(6,6))
91+
92+
#This can be used to preserve data locally:
93+
94+
randint = "SUPER IMPORTANT DATA DON'T DELETE!!!!"
95+
from random import randint as ri
96+
97+
print("ri:", ri(7,7))
98+
print("NOT LOST!", randint)
99+
76100
########## import * ##########
101+
102+
#We now we can bring one specific item from a module using
103+
from random import randint
104+
#This puts it directly in our symbol table replacing anything called randint
105+
#You will see sometimes importing everything, even though it's not recommended
106+
#100% guarentee 30% of the time to yeet out something important:
107+
108+
seed = "watermelon"
109+
from random import *
110+
111+
print("Today we are going to plant", seed, "seeds! Yay!") #Not what we wanted.
112+
113+
#We can get a better picture of this by printing the output of dir()
114+
#which shows the identifiers in scope
115+
116+
a, b, c, e, f, g = 0, 0, 0, 0, 0, 0 #find these:
117+
print(dir())
118+
77119
########## Creating a Module ##########
120+
121+
122+
#We can easily create our own module by just creating a python file
123+
#Any variables or functions will be imported
124+
#See utils.py
125+
import utils
126+
print("Range (high-low):", utils.range([5, 3, 5, 1, 10]))
127+
128+
78129
########## sys path ##########
79-
########## Packages ##########
80130

81-
import sys
82131

83-
import math
84-
print(math.pi)
132+
#When we created the utils module, it came from the same directory.
133+
#This is part of the default search path
134+
#You can see the places modules are searched for by importing a special module
135+
#https://docs.python.org/3/tutorial/modules.html#standard-modules
136+
137+
import sys
85138

86-
#print (pi) NOPE
87-
from math import pi
88-
print(pi)
139+
#always available, work directly with interpreter
140+
#unlike some other modules.
141+
#import winreg doesn't work for me (im on mac...maybe on windows)
142+
#docs give this example:
143+
sys.ps1 = 'C> ' #(Try it in interactive mode if needed)
89144

90145
print(sys.path)
91146

92-
sys.path.append('/Users/calebcurry/Python')
147+
#path prints locations searched for modules.
93148

94-
import utils
149+
#We could move utils up a directory (or anywhere we want)
150+
#and import it like so:
151+
sys.path.append("/Users/calebcurry/Python")
95152

96-
print("Range:", utils.range([5, 3, 5, 1, 10]))
153+
#There are ways you can do this more dynamically
154+
#https://stackoverflow.com/questions/30218802/get-parent-of-current-directory-from-python-script/39618108
155+
from os.path import dirname, abspath
156+
d = dirname(dirname(abspath(__file__)))
157+
sys.path.append(d)
97158

98-
a, b, c, e, f, g = 0, 0, 0, 0, 0, 0
99159

100-
pi = 3.2
101-
from math import pi
102-
print(globals())
103-
print(dir())
160+
########## Packages ##########
104161

105162

106-
import json
163+
#If you look in the directory of modules and take a gander
164+
#You'll notice some are directories while others are .py files
165+
#The ones that are directories are known as packages.
166+
#importing and using is same, creation is slightly different as mult. files combined
167+
168+
#for example, tkinter, a GUI tool, is a folder.
169+
#open it up and see a bunch of components and __init__.py to wrap it up.
170+
import ultimate_utils.list_utils
171+
import ultimate_utils.math_utils
172+
173+
ultimate_utils.list_utils.print_2d([[1,2, 3], [4, 5, 6], [7, 8, 9]])
174+
175+
#It would be ideal if we could work with list_utils directly.
176+
from ultimate_utils import * #doesn't work by default.
177+
#This is where __all__ comes in inside __init__.py
178+
list_utils.print_2d([[9, 9, 9],[9, 9, 9]])
179+
180+
#Brief bit on compilation
181+
#going through this process you may have noticed a __psycache__ folder
182+
#https://docs.python.org/3/tutorial/modules.html#compiled-python-files
183+
#"To speed up loading modules, Python caches the compiled version of each module"
184+
#enough on this tho, my brain hurts.

python/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def range(data):
2+
sorted_data = sorted(data)
3+
return sorted_data[-1] - sorted_data[0]

ultimate_utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__all__ = ["list_utils", "math_utils"]

ultimate_utils/list_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def print_2d(grades):
2+
for inner_list in grades:
3+
for grade in inner_list:
4+
print(grade, end=" ")
5+
print()

ultimate_utils/math_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def range(data):
2+
sorted_data = sorted(data)
3+
return sorted_data[-1] - sorted_data[0]

0 commit comments

Comments
 (0)