Chapter 14 and further Flashcards

1
Q

What are a few ways of logging information for diagnostics?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A

debug = True

def debug_msg(msg)
if debug:
print(msg)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create a log file using that reports all the levels and view it.

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

On your infinite loop program add a breakpoint

What does this do?

A

This will pause execution when the program hits that line of code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

install weby.py

A

pip3 install web.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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__)

A

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__)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

install flask

A

pip3 install flask

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Run Flask without doing it the moronic way the book teaches

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the two types of databases?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What would you need to install in order to communicate with a sql database?

A

pip3 install mysql-connector-python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly