list comprehension if else logger isinstance raise error try except set intro unittest list comprehension: use: a way to loop through a list quickly in a single line of code things = ['a', 'b', 'a'] result = [let for let in things] would give back the same list that we started with: ['a', 'b', 'a'] now if result = [let for let in things if let == 'a'] would give back: ['a', 'a'] so we can add if statements to the list comprehension to only get certain items of the list. where not used: if need to do multiple things on each item of the loop a for loop would be better. for example if needed to create an object of a class using the letter and call a class method. then for loop could be. result = [] for let in things: y = Yaay(let) res = y.fun() result.append(res) the above for loop would be harder to do in list comprehension. if else statement if else is helpful for getting something if a condition holds and something else if it doesn't. for example. useA = True result = 'a' if useA else 'b' #should give result #'a' because useA boolean is True if it was like this: useA = False result = 'a' if useA else 'b' then result would be: 'b' instead. try except helpful when not sure if a piece of code could potentially error and we want our tool to continue working even if it errors. for example: import traceback def funThatCouldError(x): return 1/x try: funThatCouldError(2) except Exception as e: print(traceback.format_exc()) #so we can print detailed error above shouldn’t error but if we do below it should: try: funThatCouldError(0) except Exception as e: print(traceback.format_exc()) #so we can print detailed error so if we had a situation like below where we have a list of things to process and we want to get our last item we could use try except. import traceback def funThatCouldError(x): return 1/x nums = [2.0, 0.0, 4.0] for n in nums: try: print(funThatCouldError(n)) except Exception as e: print(traceback.format_exc()) #prints 0.5 the traceback error 0.25 but if we didn’t have the try except and ran: import traceback def funThatCouldError(x): return 1/x nums = [2.0, 0.0, 4.0] for n in nums: print(funThatCouldError(n)) #we wouldn’t get to the third element 0.5 the traceback error logger when writing or debugging tools whether they be big or small, loggers are essential. they allow us to see how the tool is doing and if there are bugs with specific data we can see where in tool the bug is happening and with what specific data. a very simple example import logging logging.basicConfig(level=logging.DEBUG) logging.info("yaay a log message") logging.warn("yaay a log warn message") the warn message is helpful to put in the tool for places we want to warn the artist about something happening in tool. raise error useful for debugging in python where want to stop the script at a certain point. could raise an error. ex: raise RuntimeError("exiting at this point") isinstance helpful for checking where an object is of a given type. ex: a = 'yeah' isinstance(a, str) #True above checks where the a object is of type string. could use similar if had a method that excepts a particular class type could add an isinstance in method to check whether user passed the correct object type. set here creating two sets out of lists: setA = set(['a', 'b']) setB = set(['b', 'c', 'd']) here printing intersection of two sets setA.intersection(setB) gives result: set(['b']) also can use sets to get a unique list list(set(['a', 'b', 'a'])) gives: ['a', 'b'] unittest tests can be used to test example classes. here's an example of writing a test using unittest: import unittest class TestStuff(unittest.TestCase): def test_a(self): self.assertTrue(10==10) def test_b(self): self.assertEqual(21, 21) class TestStuffB(unittest.TestCase): def setUp(self): self.param = 10 def test_a(self): self.assertTrue(self.param==10) def test_b(self): self.assertEqual(self.param+11, 21) if __name__ == '__main__': unittest.main() and can test it by saving the python file and running in terminal: python nameOfFile.py -vThanks for looking
Pages
▼