Learning Python: Beyond the Basics

Python as a REST WebService Client

The World Bank offers access to large amounts of data relating to financial and geographic indicators via it's REST Web Service.
For example this URL will return details about Brazil.

Here is a Python client (a package w/ modules and classes) to access the World Bank REST API: WorldBank.zip.

Python using JSON

JSON is a popular data exchange format, the Python Standard Library include JSON support.

Here is a Python script to generate fake data and write that data to a JSON text file: json_writer.py.
You will need to install the faker module (pip install faker) to run this code.

Here is a Python script to read a JSON file and build a List of Dictionaries out of that data: json_reader.py.

Python accessing a NTP (Network Time Protocol) Server

The NTP Pool Project offers access to a number of NTP servers around the world.
This code accesses NTP Servers in the US, Thailand and Austria.

You will need to install the ntplib module (pip install nptlib) to run this code.
Here is the Python code: ntp_client.py.

Python GUI Application

Python supports multiple GUI libraries, see GUI Programming in Python for more details.

The TkInter library is installed with the Python Standard Library.
This GUI application uses TkInter to play a simple card game: Snap.zip.

This GUI application uses TkInter to display the time: clock.py.

Python for Data Scientists

There are a number of add-on to the Python Standard Library to support Data Science.
These include NumPy, Pandas, Matplotlib, scikit-learn, and SciPy.
These libraries provide access to data manipulation and extractions that are the daily tasks of a Data Scientist.

Here is a simple example using Pandas (Python DataFrames) to access a large data file.
The file is a World Health Organization .csv file with 202 rows and 357 columns of data.
The data file is here. You will need install the pandas module (pip install pandas) to run this code.

The pandas example is panda_who.py.

Python Database Access

Python supports multiple Databases, there are a number of framework to support DB Access in Python, including ORM Frameworks see DB Access in Python for more details.
Support for SQLite3 is provided in the Standard Library, other Database require installing a Python driver, for example Oracle provide the cx_Oracle module (see: cx_Oracle).

Here is a simple example using SQLLite3 to load then access a DB Table.
The data file is here.

The code to load the DB is SQLBuild.py.
The code to query the DB is SQLTest.py.

Python Object Oriented

Python 3 supports Object-Oriented design for data structures. There are a number of classes within the Standard Library.
For example the Date class in the datetime module (see: date-objects) holds the year, month and day fields, and methods to build, use and format these fields.

Here is a simple example OO Example to show the Python OO Basics: OOExample.py.

Here is the GUI Clock rewritten as a class oo_clock.py.

Python Automated testing

Using Python to automate testing is very common. Python has a number of test related packages, and tool. This example uses Python to run Selenium tests against a web site.
The setup for this example is a little more complicated. The setps include:
  1. Decide which Browser(s) you will testing with, you will need also to know the Browser version.
  2. Download the driver(s) for your Browsers. (see: Python Bindings for Selenium)
  3. Decide on the Python test framework, UnitTest, PyTest, etc (see: Python Test Frameworks)
  4. Write your tests
  5. Run your test, in PyCharm, you will need to configure the Settings to select the correct Test Framework. (File -> Settings -> Tools -> Python Integrated Tools -> Testing)

Here are three examples using PyTest and Selenium (pip install pytest selenium):