0% found this document useful (0 votes)
4 views

Python_21_Sample_Codes_And_Advance_Crash_Course_Guide_In_Python

The document is a comprehensive guide to Python programming, featuring 21 sample codes and advanced concepts across various chapters, including data structures, binary arithmetic, game development, and web development. It assumes a prior understanding of Python and includes practical coding examples, extensions, and documentation for each topic. The guide emphasizes the use of Python3 and provides insights into algorithms like A* and applications in areas such as SQL interfacing and desktop app creation.

Uploaded by

jdstrydom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python_21_Sample_Codes_And_Advance_Crash_Course_Guide_In_Python

The document is a comprehensive guide to Python programming, featuring 21 sample codes and advanced concepts across various chapters, including data structures, binary arithmetic, game development, and web development. It assumes a prior understanding of Python and includes practical coding examples, extensions, and documentation for each topic. The guide emphasizes the use of Python3 and provides insights into algorithms like A* and applications in areas such as SQL interfacing and desktop app creation.

Uploaded by

jdstrydom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 97

Python:

21 Sample Codes and


Advanced Crash Course Guide
in Python Programming
Table of Contents
Python:
Table of Contents
Introduction
Chapter 1: Data Structures and the A* Algorithm
Example Coding #1: A Mock File-System
# ch1a.py
# ch1b.py
Example Coding # 3: A* Algorithm
#ch1c.py
Extensions for Chapter 1
Documentation
Chapter 2: Binary Arithmetic
Example Coding # 4: Binary Class
#ch2a.py
Extensions for Chapter 2
Documentation
Chapter 3: Poker Hand Evaluator
Example Coding # 6: A Poker Hand Library
#ch3a.py
Extensions for Chapter 3
Documentation
Chapter 4: Extracting Data from eBooks
Example Coding # 7: Utility Functions for PDF and EPUB
#ch4a.py
Chapter 5: Rouge-Like Games in Console
Example Coding # 8: Python Rouge-like
#ch5a
Extensions for Chapter 5
Chapter 6: Creating Games Using Pygame
Example Coding # 9: Simon Says
#ch9a.py
Example Coding # 10: Bubble Simulation
#ch6b.py
Example Coding # 11: A* Visualization
#ch6c.py
Extensions for Chapter 6
Documentation
Chapter 7: Interfacing SQL in Python
Example Coding # 12: The SQLite Connector
#ch7a
Example Coding #13: SQLite API
#ch7b.py
Extensions for Chapter 7
Documentation
Chapter 8: Creating Desktop Apps with Tkinter
Example Coding # 14: Console Logger
#ch8a.py
Example Coding #15: Binary Calculator
#ch8b.py
Example Coding # 16: Mock Text Editor
#ch8c.py
Extensions for Chapter 8
Documentation
Chapter 9: Data Munging and Analysis with Pandas
Example Coding # 17 Merging Data
#Ch9a.py
Example Coding # 18: Basic Statistics and Reporting
#ch9b.py
Chapter 10 Extensions
Documentation
Chapter 10: Web Development in Flask
Example Coding # 19: Introduction to Flask
Example Coding # 20: User Logins in Flask
#ch11b.py
Example Coding # 21: Adding Database, Templates
templates/index.html
Extensions for Chapter 11
Documentation
Conclusion
Ó Copyright 2018 by ______Zach Webber__________- All rights reserved.

The following eBook is reproduced below with the goal of providing


information that is as accurate and reliable as possible. Regardless, purchasing
this eBook can be seen as consent to the fact that both the publisher and the
author of this book are in no way experts on the topics discussed within and that
any recommendations or suggestions that are made herein are for entertainment
purposes only. Professionals should be consulted as needed prior to undertaking
any of the action endorsed herein.

This declaration is deemed fair and valid by both the American Bar Association
and the Committee of Publishers Association and is legally binding throughout
the United States.

Furthermore, the transmission, duplication, or reproduction of any of the


following work including specific information will be considered an illegal act
irrespective of if it is done electronically or in print. This extends to creating a
secondary or tertiary copy of the work or a recorded copy and is only allowed
with the express written consent from the Publisher. All additional right
reserved.

The information in the following pages is broadly considered a truthful and


accurate account of facts and as such, any inattention, use, or misuse of the
information in question by the reader will render any resulting actions solely
under their purview. There are no scenarios in which the publisher or the original
author of this work can be in any fashion deemed liable for any hardship or
damages that may befall them after undertaking information described herein.

Additionally, the information in the following pages is intended only for


informational purposes and should thus be thought of as universal. As befitting
its nature, it is presented without assurance regarding its prolonged validity or
interim quality. Trademarks that are mentioned are done without written consent
and can in no way be considered an endorsement from the trademark holder.
Introduction

Congratulations on downloading Python: 21 Sample Codes and Advanced Crash


Course Guide in Python Programming. Thank you for doing so.

This is not a beginner’s book. It assumes that you are comfortable using Python,
the command line, and any text editor of your choice. All examples covered in
this book use Python3 so make sure you set up your environment accordingly.

Chapter 1 will begin with a few abstract data types and how to implement them.
These include the binary search tree and nth-dimensional tree. Additionally, you
will learn about the A* star pathfinding algorithm and how abstract data types
play a role in its execution

Chapter 2 will give an example of Binary arithmetic using a Python class along
with an introduction to unit testing your code. Chapter 3 is a poker-hand
evaluator that you can extend to a full-blown poker game.

Chapter 4 provides utility functions for reading eBook data and Chapter 5
teaches you how to write a simple command line game.

Chapter 6 extends further, showing you how to make graphical games using the
Pygame library.

Chapter 7 explains the basics of connecting to SQL databases in Python.

Next, in chapter eight, you will learn how to create desktop apps using the
Tkinter library.
Chapter 9 is a brief introduction to data science, and finally, chapter ten will
teach you the basics of web development with Python and how to set up Python
web servers.

There are plenty of books on this subject on the market, so thanks again for
choosing this one! Every effort was made to ensure it is full of as much useful
information and code samples as possible. Please enjoy!
Chapter 1: Data Structures and the A* Algorithm

In this chapter, you will learn how to create abstract data structures using the
same Python data types you already know. Abstract data structures allow your
programs to process data in intuitive ways and rely on the Don’t Repeat Yourself
(DRY) principle. That is, using less code, and not typing out the same operations
repeatedly for each case. As you study the examples given, you will begin to
notice a pattern emerging: the use of classes that complement each other with
one acting as a node and another as a container of nodes. In computer science, a
data structure that uses nodes is generally referred to as a tree. There are many
different types of trees, each with specialized use cases. You may have already
heard of binary trees if you are interested in programming or computer science at
all.

One possible type of tree is called an n-ary tree, or n-dimensional tree. Unlike
the binary tree, the n-ary tree contains nodes that have an arbitrary number of
children. A child is simply another instance of a node that is linked to another
node, sometimes called a parent. The parent must have some mechanism for
linking up to child nodes. The easiest way to do this is with a list of objects.
Example Coding #1: A Mock File-System
A natural application of the n-ary tree is a traditional windows or UNIX file
system. Nodes can be either folders, directories or individual files. To keep
things simple the following program assumes a single directory as the tree’s root.

# ch1a.py
The FileSystem acts as the tree, and the Node class does most of the work,
which is common with tree data structures. Notice also that FileSystem keeps
track of individual ID’s for each node. The ID’s can be used as a way to quantify
the number of nodes in the file system or to provide lookup functionality.

When it comes to trees, the most onerous task is usually programming a solution
for traversal. The usual way a tree is structured is with a single node as a root,
and from that single node, the rest of the tree can be accessed. Here the function
look_up_parent uses a loop to traverse the mock directory structure, but it can
easily be adapted to a recursive solution as well.

General usage of the program is as follows: initiate the FileSystem class, declare
Node objects with the directory syntax (in this case backslash so Python won’t
mistake it for escape characters), and then calling the add method on them.
Example Coding # 2: Binary Search Tree (BST)

The binary search tree gets its name from the fact that a node can contain at most
two children. While this may sound like a restriction, it is actually a good one
because the tree becomes intuitive to traverse. An n-ary tree, in contrast, can be
messy.
# ch1b.py
As before, the Node class does most of the heavy lifting. This program uses a
BST primarily to sort a list of numbers but can be generalized to sorting any data
type. There are also a number of auxiliary methods for finding out the size of the
tree and which nodes are childless (leaves).
This implementation of a tree better illustrates the role that recursion takes when
traversing a tree at each node calls a method (for example insert) and creates a
chain until a base case is reached.
Example Coding # 3: A* Algorithm
The A* star search algorithm is considered the same as the Dijkstra algorithm
but with brains. Whereas Dijkstra searches almost exhaustedly until the path is
found, A* uses what is called a heuristic, which is a fancy way of saying
“educated guess.” A* is fast because it is able to point an arrow at the target
(using the heuristic) and find steps on that path.

First, here’s a brief explanation of the algorithm. To simplify things, we will be


using a square grid with orthogonal movement only (no diagonals). The object
of A* is to find the shortest path between point A and point B. That is, we know
the position of point B. This will be the end node, and A the start. In order to get
from A to B, the algorithm must calculate distances of nodes between A and B
such that each node gets closer to B or is discarded. An easy way to program this
is by using a heap or priority queue and using some measure of distance to sort
order.

After the first node is added to the heap, each neighbor node will be evaluated
for distance and the closest one to B is added to the heap. The process repeats
until the node is equal to B.
#ch1c.py
In this case, the heuristic is called Manhattan distance, which is just the absolute
value between the current node and the target. The heapq library is being used to
create a priority queue with f as the priority. Note that the backtrace function is
simply traversing a tree of nodes that each has a single parent.

You can think of the g variable is the cost of moving from the starting point to
somewhere along the path. Since we are using a grid with no variation in
movement cost g can be constant. The h variable is the estimated distance
between the current node and the target. Adding these two together gives you the
f variable, which is what controls the order of nodes on the path.

Extensions for Chapter 1


1. Write an index() method for the FileSystem class that takes an id as
an argument and returns the directory associated with the id
2. Write a draw() method for the FileSystem Class that outputs a
representation of the directory to the console
3. Instead of sorting random integers, rewrite the BST to sort words
(strings)
4. Add a few “unwalkable” nodes and see how A* reacts
5. Write a method that visualizes the path returned by backtrace.

Documentation
Heapq: https://docs.Python.org/2/library/heapq.html
Chapter 2: Binary Arithmetic

Computers represent signed integers using what is called two’s complement


arithmetic. The following program implements this using a Python class and
string representation of binary numbers. A two’s complement signed integer
always has a leading bit, either one or zero, that determines the sign of the
number. The Binary class takes any string representation of a binary number and
adds it to the 16th bit using the leading bit. For example, adding two binary
numbers is a simple case of arithmetic, but it does have some peculiarities. A ‘1’
bit and another ‘1’ bit results in a carry bit and a ‘0’ bit is left in their place.
Example Coding # 4: Binary Class
#ch2a.py
The class makes extensive use of operator overloading, especially in the
__sub__ method. Also, note that whenever integer overflow occurs the program
raises a RuntimeError. This program assumes a max of 16-bit representation,
overflow happens whenever a number greater than 32,767 or less than -32,768 is
trying to be represented. If you try to add 1 to 32,767 there will be an error, same
for subtracting one from -32,768
Example Coding # 5: Unit Tests

It is important to test your code every now and then. The program on the
following page was used to make sure that the Binary class worked as intended.
The unittest module comes with the standard library so there is no need to install
via pip. To use it simply define a class that inherits from unittest.TestCase that
includes the setup() method as well as any additional methods you want to be
tested. Whenever you test code, there needs to be a clear result so that calling the
assert functions don’t return false positives.
Extensions for Chapter 2
1. Implement the __abs__ and __lt__ methods for the Binary class (note
that there is a class decorator from the functools module named
functools.total_ordering that implements the other comparison
operators if you define __eq__ and __lt__.
2. Run the tests until you get 100% completion
3. Division and multiplication are just extensions of subtracting and
adding. See if you can implement __mul__ and __div__
Documentation
Two’s complement: https://en.wikipedia.org/wiki/Two%27s_complement
Unittest: https://docs.Python.org/3/library/unittest.html
Functools: https://docs.Python.org/3/library/functools.html
Chapter 3: Poker Hand Evaluator

Poker is a card game that uses a standard deck and five card hands. If you
abstract a hand into some data structure, it is easy to evaluate it for hand
strength. The following program uses the BitArray class from the bitstring
library to represent hands.

A hand is 64-bit data type made up of four 16-bit words. Each word represents a
suit, and each bit represents a card rank from left to right (only 13 of these bits
will be used as there are only 13 different ranks, resulting in 3 empty leading
bits)
Each hand has a calculable value, implemented in a 32-bit data type made up of
8 4-bit words. Two words are empty, followed by one word indicating hand-type,
followed by 5 words representing a hand. The hexadecimal value of this data
type will be used to compare two hands, such that the higher the hexadecimal
value the higher the hand strength.

Example Coding # 6: A Poker Hand Library


#ch3a.py
The Card and Deck classes should be straightforward. Card defines operator
overload methods to easily sort cards by rank. This comes in handy when high
cards need to be determined. Hand does all of the work. The algorithm for
determining hand value is as follows: first a BitArray object is created
representing the hand, then the hand-type is determined by running checks
depending on the number of unique ranks in the hand, and finally the hand value
is determined with hand-type leading the sorted list of cards (turned into a
hexadecimal value stored in a bytearray type.

Checking for hand-type follows a set of functions based on the number of ranks.
A number of ranks is important because they implicate certain hand-types (4
unique ranks in a hand of 5 cards means that there is at least a pair). A hand-type
corresponds to a poker had (one pair, three of a kind, straight, etc.). The lowest
hand-type being 0 for high card and the highest being 9 for a royal flush.

Checking for flushes is a bit trickier. Instead of calling a function that returns
True or False, flushes are stored in a pre-computed lookup table. Remember that
in our hand data-type each suit is represented by a 16-bit word of possible ranks
(three of these are never used). Generating flushes is a matter of generating the
possible ways that one 13-bit word can have 5 bits turned on (five cards in one
suit is a flush). Straight flushes are determined by a string of 5 bits turned on in a
row, and royal flushes are determined by a string of 5 bits turned on descending
from the 13th bit. The lookup table is implemented with strings and a dictionary
key pointing to a hand-type.

In certain types of poker games, ties are dealt with by splitting the pot. Other
times the strength of remaining cards besides the significant ones (kickers) are
used to determine the stronger hand. This program uses kickers to determine
strength but ignores suit. Sometimes suits are given a strength in order to break
ties where hands and kickers are identical. For example, a royal flush of
diamonds is inferior to a royal flush of spades. This program, however, has all
royal flushes being of equal strength, since only kickers are used to break ties in
the final hand value calculation.
Extensions for Chapter 3
1. Using the unittest module write tests for a few different hand-types
and see if the program fails. If it does, find out why and fix it.
2. Implement tie-breaking functionality by taking into account strength
for ranks
3. Integrate the program into a two-player game, or a video-poker
application using the Tkinter library. (See chapter 8)
Documentation
Bitstring: https://pypi.org/project/bitstring/
Itertools: https://docs.Python.org/3/library/itertools.html
Chapter 4: Extracting Data from eBooks

Mining text from eBook formats is often difficult to code from scratch.
Thankfully, there exists a number of Python libraries designed to do just that.
The ones used in this chapter are PyPDF2 and epub, both easily installable with
pip. As far as file formats go, .epub and .pdf couldn’t be more different. While
.epub is essentially XML, the pdf spec is several hundred pages long and has to
cover things like graphics and even embedded JavaScript.

The following program shows three simple utility functions for reading data
from both formats and outputting to a text file.
Example Coding # 7: Utility Functions for PDF and EPUB
#ch4a.py

Notice that both scrape_pdf and scrape_epub output to a file using UTF-8
encoding. This is because eBooks may contain characters from a different
language or ones that can’t be represented using the default encoding scheme.
Generally, it is a good idea to go with UTF-8 if you are unsure what encoding an
eBook is using (you will receive an error if you get the encoding wrong).
The epub module needs some extra sanitization compared to PyPDF2. The
bleach module designed to remove dangerous code from HTML documents, but
it does a somewhat good job of also removing extra formatting from the raw
epub text. Next, a regular expression pattern is used to substitute any non-
alphanumeric characters with whitespace. If you find it desirable to parse the
XML, you may want to write the raw data instead of bleaching and then using
the regex substitute. However, if you are interested in just the text, this solution
might be a good fit.

Finally, the scrape_directory function can take an entire folder of ebooks in .pdf
or .epub format and extract the text using the previous two functions. It uses a
fun little utility that prints a progress bar to the console while you wat.

Extensions for Chapter 4:


1. Read the PyPDF2 documentation online and find out how to extract
metadata from pdf files.
2. Instead of extracting data to a text file add it to a SQL database (see
chapter 7)

Documentation
Pypdf2: https://Pythonhosted.org/PyPDF2/
Ebup: https://pypi.org/project/epub/
Chapter 5: Rouge-Like Games in Console

Back in the days before computer graphics where a thing, bored programmers
designed text-based games that they could play from their terminals. Usually,
these games used an element of randomness to keep things interesting and were
heavily story oriented. Today any game that is played in the terminal is said to be
‘Rouge-like’ but the genre has extended itself to graphical games as well.

A reoccurring theme in rouge-like games is turned based combat and monster


encounters. However, for the sake of keeping things simple the following
program focuses on movement alone. Note that creating games for the command
line is difficult to do with the Python standard library. One of the chief
difficulties arises when it comes to printing text to the screen and erasing it.
There are no shortages of tricks that allow you to do this (like printing a bunch
of newline characters until the screen is blank) but for this implementation, a
simple system call to clear the screen is used.
Example Coding # 8: Python Rouge-like
#ch5a
In any game whether text-based or graphical, the chief mechanism you want to
use is the main loop that runs while the game is still active. A while loop usually
works best. Notice that a system call to ‘cls’ or ‘clear’ if on a UNIX-like system
is used every iteration of the loop after the main logic and display is executed.
The call itself might be a little slow, but it gets the job done nicely.

The board is a simple 2-d array backed structure that can be easily printed to the
console using a series of if statements. Since arrays are indexable player
movement can be dictated by changing the indices.

This little game is completely luck based and therefore probably not that fun, but
it is a nice example of how one might begin to program a rouge like using
Python. There are other more sophisticated libraries that allow you to work with
the command line, most notably the curses library for those using UNIX like
systems and Pygcurse for windows.
Extensions for Chapter 5
1. Add a turn counter that lets the player know for how many turns they
survived. Alternatively, use the counter to activate events later in the
game that progress the story.
2. Add your own set of messages. See if you can program a probability
distribution so that some events occur more than others do.
3. Add enemies or index specific events that move around the board
each turn
4. Add more levels, maybe some that aren’t randomly generated
5. Add Walls or blocked passageways
6. Add food or some other item that heals the player or maybe an
inventory system as well.
7. Add a flashlight or other light source that lets the player see more
than the tile they are on or have visited. Alternatively, implement fog
so that the player doesn’t see the path they have walked.
Chapter 6: Creating Games Using Pygame

The next logical step from writing command-line games is to use a graphics
library like Pygame to make things interesting. Pygame has several graphics
rendering capabilities including 3D support as well as a sound library.
Unfortunately, CPython is notoriously slower than other languages and
sometimes not well suited for intensive graphics processing. Even so great
games have been written using Pygame (even a few commercial ones) and you
will likely have a blast making games of your own, even if they are simple.
Pygame is easily installable using pip.

As mentioned in the previous chapter, any game needs to have a main loop. Each
Pygame program must also include a call to the event loop inside of the main
loop. Otherwise, it will freeze. There are a few other things that need to happen,
such as the initiation of the pygame module using pygame.init while not
required, it is considered good practice. This method will load pygame modules
for you. Next, set up your display with pygame.display.set_mode. It takes a
tuple or list containing screen dimensions for an argument. You should also
create a clock object using pg.time.Clock to set up a frame per second timer.
Also not needed, but will make your games easier to animate.

The rest of the program is a matter of calling the event loop, doing any
calculations, and drawing objects to the screen. For Simon Says only primitives
are needed. These are built-in shapes that the pygame engine can draw to the
screen using pygame.draw but you may use your own sprites as well. Make
sure to include a call to pygame.display.update or pygame.display.flip. These
methods actually update the screen on each game tick (execution of a loop).
Without these functions, the screen will not change. You can also call them
whenever you draw to the screen and want to see immediate results, rather than
wait for the entire loop to finish. Finally, for pygame to diplsay fluid animations,
the scene needs to be wiped every frame with a call to display.fill. If the entire
screen is being used by your program, this color can be black (0,0,0). Otherwise,
you might want to use a color that fits the background of your game.
Example Coding # 9: Simon Says
#ch9a.py
Simon Says is a simple game. It relies on an array that has a preloaded sequence
of colors to be displayed at each stage of play. The program above displays the
pattern to the user and then waits for input. Notice that you cannot stop the main
loop without making calls to time.sleep but this function should be avoided if
possible. Instead, use conditional checks that only run sometimes. The main
control in the program is the waiting_on_input variable. However, in practice
conditionals are rarely adequate for controlling the flow of a complex game.
Instead, there are techniques like using finite-state machines to keep track of the
state. Here it isn’t necessary. When the player fails to recall a pattern correctly is
a game over. Instead of shifting to the game over state, the game simply resets.
Example Coding # 10: Bubble Simulation
Pygame isn’t good for only programming games. Anything that uses graphics,
(think a screen saver) can also be modeled using it. This program is a simulation
of sorts that mimics a surge of bubbles underwater. Again, only pygame
primitives are used to draw to the screen.

The program is essentially a particle system. Each particle is a circle or a bubble


that has an update and display method called once per loop for each particle.
Particles are controlled by a class Emitter that has the sole purpose of spawning
and keeping track of particles that go off screen. The update function simply
moves each particle up with a random velocity given at instantiation. More
complex simulations may have different behavior.
#ch6b.py
Example Coding # 11: A* Visualization
This program uses the A* algorithm from chapter 1, so make sure to import the
code wherever you saved it. Like the previous two examples, this program also
uses conditional statements to control state. The pygame.Rect class is a logical
construct used to handle position and collision detection. Rects aren’t game
objects that can be drawn on the screen; instead, they are representations of the
objects. Our grid is made of rects, and the main loop draws rectangles using their
position. If you didn’t quite understand the A* algorithm in chapter 1 this
program should give you a better idea.
#ch6c.py
Extensions for Chapter 6
1. Add states to the Simon game. This includes a game over and startup
screen.
2. Find out how to display text to the screen using pygame and show the
stage number the user is on.
3. Integrate sound to the game (you can find game sounds online)
4. Add additional behavior to the bubble particles. Some possible
behaviors are clumping and changing size
5. The a* module you wrote already has support for obstacles, simply
set the node.walkable attribute to False. Add walls to the
visualization.
6. Modify the visualization so that the path is revealed square by square,
rather than all at once.
Documentation
Pygame: https://www.pygame.org/docs/
Chapter 7: Interfacing SQL in Python

Relational databases use a language called SQL to issue commands to the


database engine. The basics of it should make intuitive sense, but if not feel free
to look up a tutorial online. Otherwise, go straight on to the program.

The examples in this chapter use SQLite, an in-memory database that should
come already installed in Mac. If you use Windows, you can install the SQLite
interpreter from https://www. SQLite.org/download.html. Click on SQLite-
tools-win32-x86-3240000.zip. Extract the contents to some directory, preferably
the C:\ drive. To run it, simply navigate to that directory and type SQLite3. You
can mess around with things similar to how you might in a Python interpreter.

You don’t need to stay in that same directory for the examples. Feel free to
navigate back to the directory with your other Python examples for this book.
Example Coding # 12: The SQLite Connector
Instead of typing everything out in the interpreter, the following program can
execute SQL commands using pure Python. When you connect to an in-memory
database using the SQLite3 Python library a new database will be created if one
doesn’t exist in that name. The cursor object is the main interface into the
database.

To execute commands, simply use the executescript or execute methods,


followed by a call to db.commit. At the end of the program, make sure to close
the database.
#ch7a
Example Coding #13: SQLite API
While the previous exampled used raw SQL queries, this program demonstrates
how to wrap functionality into functions. The syntax is the same, but instead of
having to call execute every time, functions allow you to abstract the process.
Also, note that the previous example dealt exclusively with writing to the
database, and this example shows how to update, delete and fetch data. These
operations are known as CRUD, for create, read, update, and delete.
#ch7b.py
Extensions for Chapter 7
1. Create a new database that mimics the functionality of an online store
or business. Make sure to include a Costumer, Item, and Location
table.
2. Write a simple application that uses a CRUD interface to access the
database you just created. Allow for the purchasing of items and
creation of new costumes. To make things fun, pretend the store is for
an RPG game or set in a fictional universe.
Documentation
SQLite: https://www. SQLite.org/index.html
SQL tutorial: http://www.sqltutorial.org/
Chapter 8: Creating Desktop Apps with Tkinter

The Tkinter module provides access to Tcl/Tk bindings that allow you to
program desktop windows. It is part of the standard library so there is no need to
install anything outright. The programs in this chapter show how to write Python
scripts that call the tcl bindings to display widgets. Beware that Tkinter
documentation is sometimes difficult to find. One source for Python
documentation can be found at http://effbot.org/tkinterbook/ and the official
Tcl/tk documentation is always available at
https://www.tcl.tk/man/tcl8.6/TclCmd/contents.htm.

Tkinter programs follow the general pattern of instantiating a root window using
the tkinter.Tk class. Usually, this is passed to a class such as Window. All other
widgets are children of the root window in one way or another. The program
runs by calling mainloop on the Tk object.

Three geometry managers come with Tkinter. They are the pack, grid, and
place managers. We will only look at the former two as place is less useful. The
pack manager places widgets on the main window almost like an elastic band
that can be stretched to fit the screen. These widgets are placed in the middle of
the window and can be aligned to corners. The grid manager has you define a
grid with rows and columns and explicitly state what rows and columns a widget
should span.
Example Coding # 14: Console Logger
This program uses the grid manager to place widgets. A number of different widgets are introduced here;
they are the Frame, Label, Button, Entry and Text widgets. Notice that each widget takes a window as
first argument, usually the root window but sometimes a Frame. The grid manager is tricky to understand at
first, because the grid is defined as you add widgets and not before. If you try placing a widget on column
10 when there is nothing defined in that range, Tkinter will still place it where you expect column 0 to be.
The sticky argument lets you choose which side the widget can expand to fill the entire cell. The grid
manager takes into account the heights and widths of widgets, so be careful placing large ones as they will
morph the size of cells.

#ch8a.py
Button widgets have a command argument that allows you to pass a callback
function. The callback is executed when the button is pressed. Make sure that all
commands passed are just pointers to functions rather than function calls with
parentheses.

Normally the Text widget allows the user to type but in this example, it is
disabled using the configure method to set state to ‘disabled’. We don’t want the
user writing on it. Text widgets use an indexing system in the form of
“line.character”. Alternatively, indices can be reserved words, in this case, ‘end’
prints to the last character index. The END constant means the same thing.
Example Coding #15: Binary Calculator
This program uses the same Binary class from Chapter 2 so make sure to import
it. Instead of using the grid manager, this example shows how to use the pack
manager. You can use either one in your programs and it is mostly a matter of
preference. However, you can’t mix both or you will get an error.

Packed widgets can be moved to a side using the side argument given that there
is space. Notice that the Frame widget creates an empty widget or window that
other widgets can be placed on. The example uses empty frames to group Button
and Entry widgets. If you are wondering what StringVar is, they are objects that
correspond to text inside of other widgets. They are the only means to add data
to Entry widgets and later retrieve it.
#ch8b.py
Example Coding # 16: Mock Text Editor
This is a longer entry than the previous one but is still not a full program.
Creating a full-text editor is possible with Tkinter. In fact, the Python IDLE is
programmed using it. The following is more like a skeleton of a full text editor.

Two new widgets are introduced, the PanedWindow and TreeView from the ttk
module. PanedWindow widgets allow you to group widgets together and change
their size. TreeView widgets are used for listing items like files in a directory.
#ch8c.py
The TreeBox class serves as a tree structure of sorts, with each node
representing a file or folder similar to the first example in this book. However,
unlike the first example, TreeBox uses a recursive solution to finding parents in
the tree.
Extensions for Chapter 8
1. Tkinter supports styling of fonts. For Example # 14, add a drop down
menu type of widget that lists different levels of severity. For each
level style the text outputted to the console with a different color (i.e.
read for warning)
2. If you programmed support for multiplication and division on the
Binary code, add two buttons for both of them. Alternatively,
redesign the interface to also accept integers like a traditional
calculator app.
3. With a little work, the mock text editor can be extended to a full-
blown application. Some things you might consider adding: a menu
system for opening files, commands for saving your work, and font
styling.
Documentation
Tkinter: http://effbot.org/tkinterbook/
Tcl/TK: https://www.tcl.tk/
Chapter 9: Data Munging and Analysis with Pandas

Data science is a premier topic in the programming world. If you are a


programmer, it is worth knowing at least the basics. For this, we have the pandas
library, installable through pip. Pandas is useful because it has a data structure
called the data frame, a Numpy array backed collection of variables (think Excel
spreadsheets).

For this chapter, you want to download the 2016 Fatality Analysis Reporting
System (FARS) data off the NHTSA FTP site ftp://ftp.nhtsa.dot.gov/fars/. Scroll
down to the year 2016 and download the FARS2016NationalCSV file. If you
extract the contents, you will see dozens of different files. Relax; this is what a
production dataset looks like. The examples in this chapter will only use the
accident.csv, vehicle.csv, person.csv, and parkwork.csv files so make sure you
extract at least those four.
Example Coding # 17 Merging Data
A common task in data science is to preprocess data, to put it a format that is
easy to analyze. This is called data munging. One form of data munging is to
merge data into a single collection.

We first define a list of variables that we are interested in and only load those
from the .csv files. Notice that some of these repeats, they are used for merging.
You can think of the FARS data as having three levels: accident -> vehicle ->
person. Each row in accident.csv corresponds to a single accident, each row in
vehicle.csv corresponds to a single vehicle in an accident, and each row in
person.csv corresponds to a single person. A large dataset like this comes with a
user manual that can be downloaded from ftp://ftp.nhtsa.dot.gov/fars/FARS-
DOC/Analytical%20User%20Guide/
#Ch9a.py
After merging the data, the result is a single pandas data frame per state. Note
that since the lowest level is the person.csv file, each row in the new data frames
refers to a person. The additional variables from the other files are added in. Due
to the awkward shape of the data, some missing values result from the merge,
which can promptly be dropped using the dropna method.
Example Coding # 18: Basic Statistics and Reporting
This program focuses more on analysis. We use the matplotlib module that you
can install via pip. Still using the same data from before, import the data frames
from the previous example.

You may be tempted to index data frames like regular lists, don’t. pandas data
frames have their own ways of indexing. For example, you can index by column
name to get the entire column or use .iloc to index by row number. You can get a
quick introduction to these and other methods by googling ‘Python pandas
cheatsheet’. If you print the fatals and drunks variables, you should see a
dictionary mapping for the number of fatal accidents and drunk driver involved
accidents per state.
#ch9b.py
To display graphs you can either call plt.show or save the plot to disk by using
plt.savefig. Refer to the user manual if you are unsure what variable values
mean. Many insights require some sort of visualization of data. For example, the
age distribution graph shows a disproportionate amount of fatalities belong to
young drivers (under 25) and the graph about travel speed shows that most
fatalities occur at moderate, rather than high speeds.
Chapter 10 Extensions
1. If you look over the user manual, you will notice there are hundreds
of different variables. See if any catch your eye and try to run your
own analyses.
2. The FARS dataset spans as far back as 1975. A real data munging
challenge would be consolidating several years of data into a single
file. The challenge lies in that certain variables change over years;
both their names and values are different (refer to the manual). A
little warning, you already know that the dataset has three main levels
(accident, vehicle, person) it may be less than ideal to merge these
into a single file. A better idea would be to merge them into three
separate files that span years. You will need a unique identifier for
each record. This can be achieved by combining year and st_case.
3. Add the entire FARS dataset (accident, vehicle, person) into an
SQLite database. If you do this, make sure you have at least 3GB of
free space lying around, as records will add up.
Documentation
Pandas: https://pandas.pydata.org/
FARS: https://www.nhtsa.gov/research-data/fatality-analysis-reporting-system-
fars
Chapter 10: Web Development in Flask

When people talk about web development using Python, they are referring to
backend systems rather than the visual aspects of websites. If you are new to
web development in general, you may consider taking a brief tour on google to
find out more about HTML, CSS, and JavaScript. This chapter focuses mainly
on website backends but will include some snippets of HTML code, so be
prepared for that.

Flask is touted as a microframework meaning that it has a bare bones approach to


setting up web servers. Other Python web frameworks such as Django offer a
more integrated, batteries included approach. Because it is small, Flask allows
you greater freedom to do things. It is “unopinionated.” At the same time, it is
extensible, meaning you can grab the code from other projects that fulfill some
need, such as a database connection as you will see in example # 21.

Example Coding # 19: Introduction to Flask


The following program is the absolute minimum flask application. To instantiate
flask you need to pass the __name__ variable which you may already be
familiar with if you know imports. What __name__ does is tell flask you are
running it inside a single module, that way it can easily access templates and
other static files (more on that in example # 21)

For now, focus on the HTML string that is returned by the index function. The
resulting web page is what the flask program returns to the browser when a
connection is established. The @route decorator stems from the name of the
flask variable, in this case, @application.route. Also known as the routing
decorator, this is what tells flask what to send to the browser when the address ‘/’
is entered into the search bar.

Since we are running flask in a development server, there won’t be a fancy


website address like www.google.com to navigate to. Instead, flask provides a
default local address of 127.0.0.1:5000. You can change this by using the host
and port arguments when calling application.run. The IP address 127.0.0.1
means this machine, and the number after the colon is the port.

Go ahead and open a browser and point it to 127.0.0.1:5000. You should see the
example web page (Note that the ‘/’ route is synonymous with 127.0.0.1:5000/)

The debug argument tells flask to run in debug mode and will automatically
reload the script when a change is detected to the file. No more having to type
out Python file.py!
#ch10a.py

Example Coding # 20: User Logins in Flask


This is a little more complex example that shows how to integrate a login
function into your sites. Werkzeug.security should have been installed along
with Flask if you used pip. A note about web security, never ever try to
implement your own password hashing functions unless you know what you are
doing. There are countless tried and tested off the shelve libraries that you can
use instead.

We still haven’t covered how to connect a database to your flask application, so


user information can simply be stored in a dictionary.
#ch11b.py
There are a few more routes in this application, and all of them return a
webpage. Whenever you use the route decorator, it expects some sort of return
statement or will throw an error. In this case, we simply reuse the HTML strings.
In the next example, you will see that sometimes you need to send back arbitrary
data instead.
Request.form is used to access the text entered into the HTML forms. If the
HTML forms are written correctly, request.form can access them using the
name variable from the HTML tag. Also, note that the form action resolves to a
flask route. We won’t go too into the HTTP protocol but it is worth learning on
your own time. Essentially, whenever a web browser needs to grab information it
issues out a ‘get’ request. Loading the home route, for example, is a form of a
get request. The same thing happens anytime you load a website. In contrast, a
‘post’ request occurs when a browser sends information back to a server. In this
case, we get username and password information and the routes need to be
configured with the methods argument to include ‘POST’.
Example Coding # 21: Adding Database, Templates
This is a more complete application using both templates and a database
connection. In it, you will program a simple webapp that can store notes written
by the user.

All you need to know about templates is that they are .html files that Python can
preprocess. Flask uses a templating engine called jinja2 whose documentation
you can read at http://jinja.pocoo.org/docs/2.10/. Thankfully, the jinja2 syntax is
very similar to Python. If you look at templates/index.htnl below, anything that
is inside of curly braces count as jinja2 syntax. Jinja2 can access variables that
you pass to the render_template function. The previous examples mimicked
this functionality by using format strings. Hopefully, you will agree that
templates are far superior. Make sure your template file is located in the same
directory as your application script, or flask won’t be able to locate it. Also,
make sure it is named templates.

Again, there is a lot of HTML in this section. If something looks unfamiliar, feel
free to look it up.
templates/index.html

templates/login.html
templates/signup.html

You can install flask_sqlalchemy by typing pip install flask-sqlalchemy.


Sqlalchemy, as you may guess, is a module used to connect to SQL databases.
However, unlike the connector from Chapter 8, sqlalchemy is what is called an
object-relational mapper, or ORM. ORM’s expose a set of class objects that are
equivalent to executing SQL commands. Note that flask-sqlalchemy and
sqlalchemy are too different things. Flask-sqlalchemy is a flask extension and
sqlalchemy is the stand-alone software that can be used with other programs.

Connecting to the database should feel a little familiar. Using the


application.config attribute, you can specify the location of the database (in this
case an SQLite one named ‘new.db’). The code essentially creates the database
in the same directory. Now you can define models. These will be your database
tables. The User model defines a one-to-many relationship with the Note model.
In practice, this means that querying the User table with the ORM will expose a
Python list that includes every note for that user.
Before running the application, make sure to fire up a Python interpreter and call
db.create_all() in order to load the tables and relational schema. You can
otherwise expect an error.
#ch11c.py
As before, there is a list of routes, but now they return a render_template call
rather than plain HTML. Additionally, we have removed the user dictionary for a
database connection and a local session. The flask session stores temporary
information such as the current username that is lost on logout. For security
reasons, flask won’t let you initiate session without supplying a secret key
otherwise known as a random string.

Some of the routes return a redirect instead of render_template. In many ways,


this is desirable because you don’t want the web browser to be stuck on the same
address bar. Previously when only returning HTML strings, if you tried to log in
the webpage address would remain at ‘/login’ even though you were really on
the home page! Redirects fix this issue.

If you still aren’t convinced about the power of templates, you will be after
reading the above code. Templates not only allow you to send information to the
HTML file, but they can also change the state of the webpage or application by
displaying information if and only if a user is logged in. Finally, the database
connection ensures that all data remains with persistence.
Extensions for Chapter 11
1. If you hadn’t noticed, the applications you wrote in flask are UGLY.
While programmers aren’t renowned for their design abilities, it is
worth brushing up on some basics. Look up HTML and CSS styling
to make your websites look prettier.
2. Using an HTML table for storing notes looks god-awful. See if you
can think of a better way to render these. Remember that all you need
to do is run a loop with jinja2 and process each note.
3. Feel free to add more content to the final website, either images,
additional text, menus—you name it.
Documentation
Flask: http://flask.pocoo.org/
Flask-sqlalchemy: http://flask-sqlalchemy.pocoo.org/2.3/
Sqlalchemy: https://www.sqlalchemy.org/
Conclusion

Thank you for making it through to the end of Python: 21 Sample Codes and
Advanced Crash Course Guide in Python Programming. Let’s hope it was
informative and able to provide you with all of the tools you need to achieve
your goals whatever they may be.

Where do you go from now? When picking up a book such as this, one thing is
clear: you know the basics and are hungry for learning how to improve your
coding skills. Part of this comes down to learning new libraries and techniques,
and another part comes down to honing your craft. The 21 examples in this book
are all extensible in their own right. Even if you follow the suggested extensions,
you can still think of novel ones on your own.

Don’t be scared to mess around with the code once you have it working. Break
things, build them up again, and break them some more. Truly understand
things. Go back and modify the examples, chances are you will find better ways
of doing the same things.

There are hundreds of books out there, and true to the open source spirit, there
are hundreds of large open source projects that you can look at. Many of the
libraries used in this book, for example, are all open source. Find code,
understand it, and improve on it.

Finally, if you found this book useful in any way, a review on Amazon is always
appreciated!

You might also like