Run a python program from the command line Force close a program run from the command line CTRL+Z
All posts by
Exiting a python program
Exit an endless loop Python program This method uses an interrupt to capture keyboard CTRL+C entered on the command line, but then cleanly exits from the program’s main loop.
Paths
Handling path slash character for Windows and Linux compatibility Windows uses “\\”Linux uses “/” Although using the Linux forwardslash will often work on Windows too, its a bit hacky and isn’t guaranteed to work. For correct Python code you should use the pathlib to solve it Detecting Windows or Linux for OS file system dependant […]
Read File
Read a line from a text file example
Heartbeat Timer
A Example Heartbeat Timer In your apps main loop The Heartbeat Function
Python timers
System timer The time module in python provides various methods and functions related to time. Elapsed Seconds The time.time() method returns the current CPU time in seconds (the time since the start of the epoch which is system dependant, but its some date in the past). Elapsed milliseconds Because the time.time() method returns a floating […]
Debugging Arrays, Lists, Dictionaries, etc
Print array Print list
State Machine
A Mode State Machine
static variables inside a function
In C a static variable inside a function is only visible inside that function’s scope, but its lifetime is the entire life of the program, and it’s only initialized once. There is NO equivalence in Python. So if you want to be a proper python programmer you should just accept that! Use a class, use […]
Using error catching
Catching specific errors See the different exception types here If no errors occurred You can use the else keyword to define a block of code to be executed if no errors were raised: Raising an error