Chapter 14 and further Flashcards
What are a few ways of logging information for diagnostics?
print your variables out during multiple stages of the code to see what they start as and end as
You can also have the program print a statement to see the checkpoint:
print(“first function call”)
print(“made it into the loop”)
How would you create a debug function? Basically this should be something you can turn on and off to show comments of checkpoints and variable data. This works best in a file or something so it doesn’t fly past the screen and you can’t read it.
debug = True
def debug_msg(msg)
if debug:
print(msg)
Python has a neat logging tool that makes all this easy.
What is it called?
What fields does it accept?
What levels are there and how do they work?
Remember that info in these is appended
logging
filename
encoding
level
logging.DEBUG
logging.INFO
logging.WARNING
logging.ERROR
logging.CRITICAL
You create these in a file by specifying the level and then adding a string at different parts of the code:
logging.critical(“critical error”)
The level you set logging to will show that level message as well as everything higher. So if you used logging.INFO it wouldn’t show logging.DEBUG
Create a log file using that reports all the levels and view it.
import logging
logging.basicConfig (
filename = “test.log”,
encoding = “utf-8”,
level = logging.DEBUG
)
logging.debug(“This is a debug message”)
logging.info(“This is an info message”)
logging.warning(“This is a warning message”)
logging.error(“This is an error message”)
logging.critical(“This is a critical message”)
Creating a logging variable that creates a new logger named after the current module.
Have these logs sent to “test.log” and instead of appending it, have them rewrite it every time.
Lastly, configure your logs to only send the time the log happened, followed by a hyphen “-“, and then followed by the log message.
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(
filename = “test.log”,
encoding = “utf-8”,
level = logging.WARNING,
filemode = “w”,
format = “%(asctime)s - %(message)s”
)
logger.debug(“debug”)
logger.info(“info”)
logger.warning(“warning”)
logger.error(“error”)
logger.critical(“critical”)
Have run and debug open on the side menu
Create a program that has a variable “a” containing 0,
Create an infinite while loop that says:
“hello world”
“a is 1”
have it wait for one second and continue forever adding one each iteration.
Pause the program
Click step over and look at variables on the left. Change the number that a is on.
What do you do if everything is blank in variables?
import time
running = True
a = 0
while running:
print(“Hello, World”)
print(“a is “ + str(a))
a += 1
time.sleep(1)
If everything is black, click the Call Stack drop down menu and select your module.
On your infinite loop program add a breakpoint
What does this do?
This will pause execution when the program hits that line of code
install weby.py
pip3 install web.py
Using web.py, create a “Hello, World page via web
It should turn on if __name__ (this references the name of your module to python) matches your module main file (__main__)
import web
urls = (
‘/(.*)’,
‘hello’
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
web.header(“Content-Type”, “text/html”)
if not name:
name = ‘World’
return ‘Hello, ‘ + name + ‘!’
if __name__ == “__main__”:
app.run()
/(.*) <- this is regex and it means any url that matches this, which is any.
home < - this is who we tell to manage the urls
global() <- global variable for the module, which is a dictionary of all variables, classes, and objects, etc in the global scope (main program scope __main__)
install flask
pip3 install flask
Run Flask without doing it the moronic way the book teaches
from flask import Flask
app = Flask(__name__)
@app.route(“/”)
def hello():
return “<h1>Hello, World!</h1>”
app.run(host=”0.0.0.0”, port=9090)
– app <- this variable tell flask to just look at this module for the info it needs
What are the two types of databases?
Relational - stored in tables like a spreadsheet with columns defining fields of data and rows containing a new item to be stored. These are queried (read) using SQL
Document - stores data in collections of documents
Faster at creating and updating large numbers of records but slower at retrieval of large number of records.
What would you need to install in order to communicate with a sql database?
pip3 install mysql-connector-python