• Stars
    star
    152
  • Rank 243,168 (Top 5 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created about 6 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Safari Live Training - Python by Example

Build Status

Safari Live Training - Python By Example

Source code for the training course. Please contact me with any questions. Before beginning, be sure you know how to use git at a basic level on your computer (Windows, Mac OS, or Linux).

Contact information:
Email: [email protected]
Twitter: @nickrusso42518

Download Instructions

The easiest way to consume this code is to clone it using SSH or HTTPS.

SSH: git clone [email protected]:nickrusso42518/slt-py-example.git

or

HTTPS: git clone https://github.com/nickrusso42518/slt-py-example.git

After cloning, you should see the following file system structure:

$ tree
.
β”œβ”€β”€ Makefile
β”œβ”€β”€ README.md
β”œβ”€β”€ complete.py
β”œβ”€β”€ fundamental.py
β”œβ”€β”€ inputs
β”‚Β Β  β”œβ”€β”€ circle.yml
β”‚Β Β  └── rectangle.json
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ shape_pytest.py
β”œβ”€β”€ shape_unittest.py
β”œβ”€β”€ shapes
β”‚Β Β  β”œβ”€β”€ __init__.py
β”‚Β Β  β”œβ”€β”€ circle.py
β”‚Β Β  β”œβ”€β”€ rectangle.py
β”‚Β Β  └── shape.py
β”œβ”€β”€ small
β”‚Β Β  β”œβ”€β”€ (many files ...)
└── unittests
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ circle_test.py
    └── rectangle_test.py

Ensure you have Python 3.6 or newer installed along with pip. Ideally, you should have 3.10 to be on par with the instructor. Note that some minor features require 3.7 or 3.8, but the main programs can run on 3.6 at a minimum.

$ python --version
Python 3.10.5

Visit https://www.python.org/downloads/ to download Python.

sudo python -m ensurepip

or

sudo easy_install pip

No need to install any packages via pip; this is done during the course.

To get setup, first run make setup which will install the required Python packages and create the outputs/ directory. Failing to take this step could result in errors later in the course.

Optionally, run make to run a full suite of testing on the code to ensure everything works. After a fresh git clone there should be no errors.

Usage

There are two main programs to run, fundamentals.py and complete.py. Each program performs some computations on basic shapes, such as area and perimeter. The input data is included in the inputs/ folder and uses YAML for circle radii and JSON for rectangle lengths/widths. Outputs are written to outputs/ as JSON files.

The fundamentals.py code is a halfway point through the course used to illustrate the fundamental concepts introduced earlier in the course. It takes zero or one arguments. When no arguments are supplied, the script is interactive and asks the user whether the units are inches (in) or centimeters (cm).

$ python fundamental.py
Choose unit of measure (cm or in): in
radius 5 -> area 78.54 in sq
radius 8 -> area 201.06 in sq
radius 11 -> area 380.13 in sq
radius 5 -> perim 31.42 in
radius 8 -> perim 50.27 in
radius 11 -> perim 69.12 in
rectangle1
 8x2 -> area 16 in sq
 (8+2)x2 -> perim 20 in
rectangle2
 3x3 -> area 9 in sq
 (3+3)x2 -> perim 12 in
rectangle3
 1x6 -> area 6 in sq
 (1+6)x2 -> perim 14 in

When one argument is supplied, the script is non-interactive, as in or cm has been specified.

$ python fundamental.py cm
radius 5 -> area 78.54 cm sq
radius 8 -> area 201.06 cm sq
radius 11 -> area 380.13 cm sq
radius 5 -> perim 31.42 cm
radius 8 -> perim 50.27 cm
radius 11 -> perim 69.12 cm
rectangle1
 8x2 -> area 16 cm sq
 (8+2)x2 -> perim 20 cm
rectangle2
 3x3 -> area 9 cm sq
 (3+3)x2 -> perim 12 cm
rectangle3
 1x6 -> area 6 cm sq
 (1+6)x2 -> perim 14 cm

If the interactive or command line argument is bogus, the program keeps asking for the current input (case insensitive):

$ python fundamental.py dog
Choose unit of measure (cm or in): cat
Choose unit of measure (cm or in): monkey
Choose unit of measure (cm or in): CM
radius 5 -> area 78.54 cm sq
radius 8 -> area 201.06 cm sq
radius 11 -> area 380.13 cm sq
radius 5 -> perim 31.42 cm
radius 8 -> perim 50.27 cm
radius 11 -> perim 69.12 cm
rectangle1
 8x2 -> area 16 cm sq
 (8+2)x2 -> perim 20 cm
rectangle2
 3x3 -> area 9 cm sq
 (3+3)x2 -> perim 12 cm
rectangle3
 1x6 -> area 6 cm sq
 (1+6)x2 -> perim 14 cm

The complete.py code wraps up almost everything in this class within a simple small program. It relies on the shapes package and its component modules. The user input is similar to fundamental.py in that either in or cm must be specified with some formatting exceptions described previously. The code uses object-oriented programming, abstract classes, polymorphism, and YAML/JSON interaction for execution.

$ python complete.py cm
Type:  Rectangle
 Area:  21 cm sq
 Perim: 20 cm

Type:  Rectangle
 Area:  25 cm sq
 Perim: 20 cm

Type:  Rectangle
 Area:  6 cm sq
 Perim: 14 cm

Type:  Circle
 Area:  201.06 cm sq
 Perim: 50.27 cm

Type:  Circle
 Area:  78.54 cm sq
 Perim: 31.42 cm

Testing

A GNU Makefile with phony targets is used for testing this codebase. There are currently three steps:

  • setup: Installs required Python packages in the requirements.txt file using pip. Creates outputs/ directory.
  • lint: Runs YAML and Python linters. This captures any syntax or styling errors with the code.
  • unit: Runs unittest and pytest unit tests for the rectangle and circle classes. This ensures the methods in each classes are operating correctly.
  • run: Runs the two programs with both in and cm as inputs. The default input files should have no failures.

You can run make or make all to run all the testing in series when doing manual regression testing from the shell. As mentioned earlier in the README, this is a good idea after first cloning the repository.

More Repositories

1

nots

[Ansible] Nick's OSPF TroubleShooter
Python
175
star
2

racc

[Ansible] Run Arbitrary CLI Commands on a variety of network devices
Jinja
129
star
3

natm

[Ansible] Network Address Translation Manager
Jinja
99
star
4

slt-netdevops

Safari Live Training - Fundamentals of Network DevOps
80
star
5

narc

Nornir/Netmiko ASA Rule Checker - Validate firewall rule compliance
Python
59
star
6

net-templates

Network Configuration Templates
58
star
7

cisco-etech

Free Cisco CCIE/CCDE Evolving Technologies book
TeX
51
star
8

stig

Offline config file scanner to test for STIG compliance with flexible rule sets
Python
49
star
9

postman

Assortment of Postman collections/environments
47
star
10

vpnm

[Ansible] Declarative MPLS L3VPN route-target management
Python
40
star
11

ospf_brkrst3310

Lab files for Cisco Live "Troubleshooting OSPF" - BRKRST-3310
HTML
37
star
12

slt-techwrite

O'Reilly Technical Writing Course
35
star
13

pluralsight

Course content
Python
32
star
14

perf

[Ansible] Variety of performance testers for short, midterm, and long tests
Python
31
star
15

mkfd

[Ansible] Role to Make text Files and Documentation PDFs locally
Makefile
31
star
16

net-tools

Miscellaneous and lightweight network tools
Python
29
star
17

slt-py-requests

Safari Live Training - Python Requests
Python
28
star
18

slt-ans-networks

Safari Live Training - Ansible for Managing Network Devices
Jinja
25
star
19

ml-qrg

Machine Learning Quick Reference Guide
Jupyter Notebook
25
star
20

slt-py-httpx

Safari Live Training - Web Service Interaction with Python httpx
Python
18
star
21

py-auphonic

Python client library for the Auphonic REST API
Python
17
star
22

tweeter-lite

Tweet validation
Python
16
star
23

bts

BGP Traffic Server automation
Python
13
star
24

zhong

Chinese Language Trainer
Python
7
star
25

ospf_digrst2337

OSPF Deployment in Modern Networks
HTML
7
star
26

homelab

Home lab validation/setup
Python
7
star
27

ccdeve

DevNet Expert studies
Python
6
star
28

nsla

Nornir-based IP SLA Performance Tester
Python
5
star
29

polls

RCP Average Collector
Python
5
star
30

vtlcli-docker

Docker wrapper for VTL template development
Dockerfile
4
star
31

basic-algorithms

searches, sorts, etc
Python
4
star
32

ancc

AI-based Network Configuration Converter
Python
4
star
33

ipv6-tools

Lightweight tools to work with IPv6 EUI-64 addressing
3
star
34

ans-netbox

Ansible/Netbox testing
Jinja
3
star
35

sauto3

Python
2
star
36

hangman

Lua
1
star
37

fiftypieces

Book written in markua
1
star
38

jan27

netdevops training
1
star
39

ps-talks

Python
1
star
40

cdpc

Makefile
1
star