Programming Flashcards
What are functions used for?
Encapsulating code to do a particular job.
They create modularity
Promote good variable scoping and crearer control flow.
What is an argument of a function?
The bit in brackets
‘def function(argument):’
A function can have many arguments
What is the scope of a variable
The area of the code from which the variable can be used. Global variable have global scope (can be used anywhere), but function declared in a module can only be used in that module unless they are passed into another function as an argument.
Unknown and unlimited arguments
‘*args’ is passed into a function as the argument to represent an unknown number of arguments.
‘**kwargs’ is passed into a function as the argument to represent key work arguments, e.g. “conf.xml”. These arguments are accessed by their key rather than position
Pep8
PEP8 is a general style guide for commenting
PEP257
PEP257 specifies docstrings that describe functions and modules
RegEx characters
., ^, $, *, +, ?, []
. -Matches any characters except new line
^ -Matches the start of a string and immediately after new line
$ -Matches end of a string
* -Resulting RegEx matches 0 or more repetitions of given character(s)
+ -Resulting RegEx matches 1 or more repetitions of given character(s)
? -Resulting RegEx matches 0 or 1 repetition of given character(s) [not greedy]
[] -Used to indicate a set of characters in a set . E.g. [0-9A-Fa-f]
Environment variables
import time
print(time.gmtime())
the gmtime bit is an environment variable
Scripts - Inline direct execution
- Start to end execution
- Threads are created and managed per run or job
- The route through the program is largely predetermined and independent of other programs
- Idempotent programs – output should be reproducible (except for intentional randomness) with the same input
Asynchronous execution
- Asynchronous jobs are run from a queue and don’t have to be executed in the order they occur
- Crucial for automation
- This can improve load management by reducing bottlenecks although can create concurrency issues
Example of asynchronous event driven architecture
Websites use this
Flask
Python Path
Environment variable where additional modules are stored - pip installs to here.
Module used to schedule commands for a specific time
Sched module
What is ‘@app.route(“/home”)’
Code for flsk to show code run when the /home page of your website is run
Restful API calls
Using API key with a url to use data from a website in a program.
Data usually received in a .json file, and relevant parts can be taken from this.
Software reliability - Defensive programming
Used for edge cases to stop program crashing
Software reliability - Exceptions
An exception is raised when one one of these errors occurs:
IndexError I ValueError I EOFError I AssertionError I SyntaxError I
IndentationError I KeyboardInterrupt
or you can define your own exceptions
Try-except (try-catch)
The try: … except: statement attempts to run code, but if it would raise an exception (crash the program), the code below except is executed instead
try … except … finally
The try: … except: … finally: overrides all error handling as final block. The code under ‘finally’ will run regardless of any raised exceptions.
‘raise’ keyword
To raise an exception in your code use the keyword raise - this is beneficial for defensive programming.