|
63 | 63 |
|
64 | 64 | ########## From module import Something ########## |
65 | 65 |
|
| 66 | + |
66 | 67 | #in the previous section we showed how to import something. |
67 | 68 | import random |
68 | 69 |
|
69 | 70 | print(type(random)) |
70 | 71 | #Doing this requires us to access the module using the dot operator |
71 | 72 |
|
| 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 |
72 | 81 |
|
| 82 | +print("RIP data:", randint) |
73 | 83 |
|
74 | 84 |
|
75 | 85 | ########## 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 | + |
76 | 100 | ########## 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 | + |
77 | 119 | ########## 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 | + |
78 | 129 | ########## sys path ########## |
79 | | -########## Packages ########## |
80 | 130 |
|
81 | | -import sys |
82 | 131 |
|
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 |
85 | 138 |
|
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) |
89 | 144 |
|
90 | 145 | print(sys.path) |
91 | 146 |
|
92 | | -sys.path.append('/Users/calebcurry/Python') |
| 147 | +#path prints locations searched for modules. |
93 | 148 |
|
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") |
95 | 152 |
|
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) |
97 | 158 |
|
98 | | -a, b, c, e, f, g = 0, 0, 0, 0, 0, 0 |
99 | 159 |
|
100 | | -pi = 3.2 |
101 | | -from math import pi |
102 | | -print(globals()) |
103 | | -print(dir()) |
| 160 | +########## Packages ########## |
104 | 161 |
|
105 | 162 |
|
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. |
0 commit comments