Skip to content

Commit b1d99b0

Browse files
committed
disaster work in progress.
1 parent 90ea73f commit b1d99b0

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

python/06-modules.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
########## Intro to modules ##########
2+
3+
4+
#A module is just a fancy name for a python file
5+
#By convention, they are often imported at the top of a file
6+
#but you can do it anywhere
7+
import math
8+
import pickle
9+
import queue
10+
import heapq
11+
import json
12+
import random
13+
14+
print(pickle)
15+
#You will get a path like so (I'm on a mac)
16+
#/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pickle.py
17+
#Explore this folder to see some other options.
18+
#Or, check out this: https://docs.python.org/3/py-modindex.html
19+
20+
#We use the module with dot notation to access the functions or variables
21+
#Similar to an object, but not quite exactly the same thing
22+
print(math.pi)
23+
24+
#Here is another example
25+
#Pseudorandom number 0-100 (inclusive)
26+
print(random.randint(0, 100))
27+
28+
#You can see how this works by opening random.py
29+
#from random.py line 244:
30+
#def randint(self, a, b):
31+
# """Return random integer in range [a, b], including both end points.
32+
# """
33+
# return self.randrange(a, b+1)
34+
35+
36+
"""
37+
This is a side note that we may get into in more detail later
38+
When you create a file to be imported,
39+
you can expose certain pieces as seen in random.py
40+
41+
line 786...
42+
_inst = Random()
43+
seed = _inst.seed
44+
random = _inst.random
45+
...
46+
randrange = _inst.randrange
47+
48+
Then when we import random, we prefix with that module name:
49+
random.randrange
50+
51+
This will access .random on an instantiated Random object
52+
Which is then exposed through __all__
53+
54+
"...is easier for the casual user than making them
55+
# instantiate their own Random() instance."
56+
57+
As opposed to something like this:
58+
59+
test = random.Random()
60+
print(test.randint(5,10))
61+
"""
62+
63+
64+
########## From module import Something ##########
65+
66+
#in the previous section we showed how to import something.
67+
import random
68+
69+
print(type(random))
70+
#Doing this requires us to access the module using the dot operator
71+
72+
73+
74+
75+
########## Alias an import ##########
76+
########## import * ##########
77+
########## Creating a Module ##########
78+
########## sys path ##########
79+
########## Packages ##########
80+
81+
import sys
82+
83+
import math
84+
print(math.pi)
85+
86+
#print (pi) NOPE
87+
from math import pi
88+
print(pi)
89+
90+
print(sys.path)
91+
92+
sys.path.append('/Users/calebcurry/Python')
93+
94+
import utils
95+
96+
print("Range:", utils.range([5, 3, 5, 1, 10]))
97+
98+
a, b, c, e, f, g = 0, 0, 0, 0, 0, 0
99+
100+
pi = 3.2
101+
from math import pi
102+
print(globals())
103+
print(dir())
104+
105+
106+
import json
107+
108+
json.

0 commit comments

Comments
 (0)