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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | #Replacing Text. (REGEX) import re #Defining the 're' module def main(): #Defining a function 'main' - (can be anything) try: #Trying function below, if it is true, it will execute. v = input('What file would you like to run: ') a = input('What text would you like to highlight: ') b = input('What would you like to change it to: ') fh = open (v) #Assigning the variable 'fh' to opening a file 'rotf.txt' pattern = re.compile(a, re.IGNORECASE) #Assigning 'pattern' to a function, which chooses the text and ignores case. for line in fh: #Printing a chosen object in a way defined below. if re.search(pattern, line): #If it finds 'pattern' excute code below it. print(pattern.sub(b, line), end='') #Prints the sentece containing 'pattern' and swaps chosen word with 'pattern.sub' except IOError as e: #If file is not found, print text below and type of error. print('Sorry, file not found!', e) #main() #Calling the 'main' function written above. #________________________________________________________________________________________________# #Checking file name, and if it exists. (RECURSION) def open_file(): #Defining the 're' module try: #If code below is true, try will excute file = input('What file would you like to open: ') #Assigning file to user's choice for line in readlines(file): print(line.strip()) #Printing the file except IOError as e: #If no such file, print this error print('sorry file not found!', e) #What will execute if 'file' is False except ValueError as e: #If bad file name. e.g. '.dic' when it's '.doc' print('bad filename!', e) #What will execute if bad file name def readlines(filename): #A function that will let the user choose a file if filename.endswith('.txt'): #Making sure the filename ends with '.txt' fh = open(filename) #Assigning 'fh' to what the users file choice is return fh.readlines() #Returning the file, with the readlines function else: raise ValueError('File name must end in .txt!') #If bad file name, this executes #open_file() #Calling the function above. #________________________________________________________________________________________________# #Classes class Animal: def talk(self): print('i have something to say') #Default function for 'talk' def walk(self): print('hey, im walking here') #Default function for 'walk' def clothes(self): print('i have nice clothes') #Default function for 'clothes' class Dog(Animal): #Calling a class named 'Dog' and telling it it's an animal def bark(self): #Default result for 'bark' print('woof!') def walk(self): #Default resault for 'walk' (in Dog() only!) super().walk() #Tells the function to use the Default result for walk print('strut!') class Duck(Animal): #Calling a class names dog and assigning it to 'Animal' def clothes(self): #Defining it's 'clothes' print('I have brown and white fur') def Main(): Max = Dog() #Assigning 'Max' to Dog() which can be used later on. Max.bark() #Max's bark Max.walk() #Max's walk Max.clothes() #Max's clothes donald = Duck() #Assigning 'donald' to Duck() donald.walk() #donald's walk donald.talk() #donald's talk donald.clothes() #donald's clothes #Main() #Calling the Main() function #________________________________________________________________________________________________# #Classes class Duck(): #Introduces the definition of a class. def __init__(self, **kwargs): #This is a constructor self.variables = kwargs def quack(self): #Function that prints a line of text. print('Quaaack!') def walk(self): #Function that prints a line of text. print('Walks like a duck.') def set_color(self, color): #Function that sets the color self.variables['color'] = color def get_color(self): #Function that gets the color return self.variables.get('color', None) def set_variable(self, k, v): #K and V represent the var and it's value self.variables[k] = v def get_variable(self, k): #Function that returns what is written return self.variables.get(k, None) def duck(): donald = Duck(feet = 2) #Setting the VAR 'feet' to '2' donald.set_variable('color', 'blue') #Setting the VAR 'color' to 'blue' print(donald.get_variable('feet')) #Printing 'feet' print(donald.get_variable('color')) #Printing 'color' #duck() #________________________________________________________________________________________________# #Classes class Duck: def quack(self): print('Quaaack!') def walk(self): print('Walks like a duck!') def bark(self): print('the duck can not bark!') def fur(self): print('the duck has feathers!') class Dog: def bark(self): print('woof') def fur(self): print('The dog has brown and black fur!') def walk(self): print('Walks like a dog!') def quack(self): print('The dog barks') def animal(): donald = Duck() fido = Dog() in_the_forest(donald) in_the_pond(fido) def in_the_forest(dog): dog.bark() dog.fur() def in_the_pond(duck): duck.quack() duck.walk() #animal() #________________________________________________________________________________________________# #Classes(generator) class inc_range: def __init__(self, *args): numargs = len(args) if numargs < 1: raise TypeError('requires at least one arg') elif numargs == 1: self.stop = args[0] self.start = 0 self.step = 1 elif numargs == 2: (self.start, self.stop) = args self.step = 1 elif numargs == 3: (self.start, self.stop, self.step) = args else: raise TypeError('expected at most 3 arguments, got {}'.format(numargs)) def __iter__(self): i = self.start while i <= self.stop: yield i i += self.step def main(): o = inc_range(0, 30000) for i in o: print(i, end = ' ') #main() #________________________________________________________________________________________________# #Decorators class Duck: def __init__(self, **kwargs): self.properties = kwargs def quack(self): print('Quaaack!') def walk(self): print('Walks like a duck.') def get_properties(self): return self.properties def get_property(self, key): return self.properties.get(key, None) @property def color(self): self.properties.get('color', None) @color.setter def color(self, c): self.properties['color'] = c @color.deleter def color(self): del self.properties['color'] def main(): donald = Duck() donald.color = 'blue' print(donald.color) if __name__ == "__main__": main() #main() #________________________________________________________________________________________________# |
Python | Notes/Classes
This is just something I might print when I get the chance.. ok