Python Flashcards
(20 cards)
How is Python interpreted?
Dynamically, which means the code is interpreted at runtime. There is no compiling before execution and no linking.
What is the type system for Python?
Duck typing, which means it doesn’t matter if you tell me it is a duck, if it quacks, it is a duck. So the code is not constrained by data types, but it executes an action based on the type of built-in objects being processed.
In other words, the type of an object/expression is determined by the operations involved.
What is the cons of duck typing?
No compile time and runs right away, so there are very few checks that can be done. Need a lot of testing just to be safe.
What are the other implementations of Python (CPython)?
PyPy, Cython, Jython
What is IPython?
An interactive shell for Python. Can run in shell with $ipython
What is the pros of using Jupyter Notebook?
Collaboration on computational documents, markdown for documentation, many new libraries for data processing,
What is a unit test?
A unit test is used to test the behaviour of a unit of functionality.
What is the module for unit test
unittest
If we want to test whether add(3, 5) returns 8, what would the unit test look like?
def test1(self):
self.assertEqual(add(3, 5), 8)
If we want to test whether fat(“Benson”) returns false, what would the unit test look like?
def test2(self):
self.assertFalse(fat(“Benson”)
How to express not a-z in regex?
[^a-z]
What is + in regex?
1 or more
What is * in regex?
0 or more
How to find “ethelia0” from a string?
import re
r = re.compile(r”ethelia[0-9]+”)
v = r.findall(“My name is ethelia”)
What is virtualenv?
It is a tool to create isolated python virtual environments. It is not a virtual machine! The benefits of having this is when your code needs specific libraries and versions of these.
Different environments provide different libraries and these can change when the system administer updates the system. Using virtualenv ensures that if it works on this machine, it works on others.
What is a tool that we can use to identify how much of a program has been executed by a given test?
coverage.py
What does coverage.py do?
It is used to monitor the code coverage of a program, noting parts of the program that is executed and parts that are not executed but could be. Coverage measurement is used to gauge the effectiveness of tests.
What is a statement coverage?
It tracks if each individual line or statement in the code is executed at least once during a test run.
What is a branch coverage?
It is a metric that measures whether each possible path (branch) in the code has been executed during testing. Specifically, it ensures that all the branches of conditional statements (like if, else, while, for, try, etc.) are executed at least once.
Branch coverage tracks every potential path that the code could take in conditional statements. For example, in an if statement, branch coverage ensures that both the True and False branches are tested.
How to run coverage.py
coverage run PYTHON_PROGRAM