EE250Unit1_Technologies
EE250Unit1_Technologies
EE 250 Unit 1
Technologies
(Python, Git, Raspberry Pi, Linux)
1.2
PYTHON
1.3
Credits
• Many of the examples below are taken from the
online Python tutorial at:
– http://docs.python.org/tutorial/introduction.html
• Other tutorials:
– https://www.learnpython.org/
– Many others online
1.4
Python in Context
• Two major versions with some language differences
– Python 2.x
– Python 3.x (we will focus on this version)
• Interpreted, not compiled like C++
– Can type in single commands at a time and have them
execute in "real time"
– Somewhat slower
– Better protection (no memory faults)
1.5
# python3.x
>>> print("Hello world")
myscript.py
1.6
Types
# python 2.x
>>> 3 / 2
1 # python2.x
• Types 1.5 # python3.x
– Bool: True/False (not true/false) # python 3.x
– Integers >>> 3 // 2
1
• Integer division => see examples
>>> 1.25 / 0.5
– Floats 2.5
– Complex
>>> 2+4j + 3-2j
– Strings (5+2j)
Strings
• Enclosed in either double >>> 'spam eggs'
'spam eggs'
or single quotes
>>> 'doesn\'t'
– The unused quote type "doesn't"
can be used within the >>> "doesn't"
string "doesn't"
Selection Structures
• if…elif…else #! /usr/bin/python3
Iterative Structures
• while <cond>: #!/usr/bin/python3
Lists
>>> x = ['Hi', 5, 6.5]
• Lists are like arrays from C++ >>> print(x[1])
5
but can have different
>>> y = x[2] + 1.25
(heterogenous) types in a single 7.75
list object >>> x[2] = 9.5
• Comma separated values >>> x
['Hi', 5, 9.5]
between square brackets >>> x.append(11)
• Basic operations/functions: ['Hi', 5, 9.5, 11]
>>> len(x)
2
1.13
Iterative Structures
• for <item> in <collection>: #! /usr/bin/env python3
• collection can be list or some other # Prints 0 through 5 on separate lines
collection we will see later (tuple, x = [0,1,2,3,4,5] # equiv to x = range(6)
dictionary, etc.) for i in x:
• For a specific range of integers just print(i)
use range() function to generate a list # Prints 0 through 4 on separate lines
– Start is inclusive, stop is exclusive x = 5
– range(stop) for i in range(x):
• 0 through stop-1 print(i)
– range(start, stop) # Prints 2 through 5 on separate lines
• start through stop-1 for i in range(2,6):
– range(start, stop, step) print(i)
• start through stop in increments of # Prints 0,2,4,6,8 on separate lines
stepsize
for i in range(0,10,2):
print(i)
1.14
Exercise 1a
• Continue to get input from Some text
the user until they type in Other input
quit
"quit" (exactly). Count 2
Import Modules
myscript.py
• To use Python libraries/modules use #!/usr/bin/python3
import sys
the import keyword
print("Num args is " + str(len(sys.argv)))
• sys module contains system access
print("Command line args are:")
methods including access to for arg in sys.argv:
command line arguments print(arg)
More Modules
• To call function and create instances >>> import math
>>> log10(100)
of classes (i.e. objects) defined in a
Traceback (most recent call last):
module, you must use hierarchy File "<stdin>", line 1, in <module>
indicating module.function( ) or NameError: name 'log10' is not defined
module.classname >>> math.log10(100)
into the top level namespace with >>> from math import log10
the from statement >>> log10(1000)
• Examples: 3.0
Exercise 1b
myscript.py
Exercise 2
• Write a script that takes 1 command line
argument (call it quitVal)
– $ python3 ex2.py 5 # 5 is quitVal
• The script should read in integers one at a
time (one per line) and sum all the positive
integers, ignoring negative numbers, until the
user types the quitVal.
1.24
memory y
Object/Memory
• Instead a variable name Namespace
Space
references an object >>> x[1] = 7
x [4,7,9]
• When we assign a >>> print(y[1])
7
variable we are changing y
the association between
the variable name and Namespace
Object/Memory
which object it Space
>>> x = [4,5,9]
references x [4,5,9]
>>> y = list(x)
• Copies are not made >>> a = 5 y [4,5,9]
unless explicitly >>> b = a
performed via a "copy >>> b = 7 a 5
constructor" >>> print(a)
5
b 7
1.25
Variable/Reference Gotcha
Object/Memory
Namespace
Space
• Be sure to understand >>> x = [4,5,9]
x [4,5,9]
what assignment does >>> y = x
y
Object/Memory
Namespace
Space
>>> x = []
>>> print(len(y))
x []
y
You MAY think this is what you have…
Object/Memory
Namespace
Space
>>> xx == []
>>> []
>>> print(len(y))
print(len(y))
x []
>>>
3
y [4,5,9]
Argument Passing
1 def f1(a, b):
• Not pass-by-value, not pass-by-reference! 2
a = 5
b.append(3)
x = 7
– Arguments are new names that reference the original y = [1,2]
argument references f1(x,y)
print(x)
– Reassigning associates a new object with the name print(y)
1 2 3
Object/Memory Object/Memory Object/Memory
__main__ __main__ __main__
Space Space Space
Namespace Namespace Namespace
x 7 x 7 x 7
f1 f1 f1
Namespace Namespace Namespace
a a 5 a 5
b b b []
1.27
Tuples
• Like an array but can >>> x = ('Hi', 5, 6.5)
• Value can be anything (int, list, dict) >>> for k,v in grades.items():
print(k + " got the score " + str(v))
• Keyword in checks membership
1.29
Exercise 3
• Write a script that reads in zip codes (e.g. 90007,
90089, etc.) one per line until the user enters 0 and
then outputs how many occurrences of each unique
zip code was entered
– Use a dictionary to track the occurrences of each zip code
$ python3 ex3.py
90007
User input
90089
90007
0
Occurrences:
Desired output
90007 : 2
90089 : 1
1.30
More "Humor"
https://imgs.xkcd.com/comics/universal_install_script_2x.png
1.33
or }
#include<iostream>
Output File
using namespace std;
int main(int argc, char *argv[]) Hello
{ Hello
int dummy, x; Bad input
cin >> dummy >> x; Error File
} Bad input
1.34
Source/Version Control
• Have you ever made backups of backups of source files to
save your code at various states of development (so you can
recover to an earlier working version)?
• Have you ever worked on the same code with a partner and
tried to integrate changes they made?
• These tasks can be painful without help
• Source/version control tools make this task easy
– Allows one codebase (no separate folders or copies of files) that can
be "checkpointed" (committed) at various times and then return back
to a previous checkpoint/commit if desired
– Can help merge differences between two versions of the same code
• Common source/version control tools are:
– Git, Subversion, and a few older ones (cvs, rcs, clearcase, etc.)
1.37
Repositories
• We generally organize our code repo
Git
• Git is a version control system github.com
Cloning Repos
github.com
file1.cpp
file1.cpp'
file2.cpp
C1 C2
This Photo by Unknown Author is licensed under CC BY-SA
1.41
git commit
git add
Git "Locations"
https://git-scm.com/book/en/v2/Getting-Started-Git-Basics
git add
git commit
Git File Lifecycle
https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository
1.42
Branches Motivation
• Branches are useful when you are time
C1
adding some new feature/fix,
especially when other developers
may also be doing the same by giving C2
a separate sandbox to work in
• Branches allow you to
– Grab the code from a particular starting feature1 C3 main branch
point (i.e. commit) branch C4 (other
(your updates developers or
– Modify code, add, delete and commit for feature 1) C5 fixes to C2)
without affecting others viewpoint unless
they explicitly desire
– Merge the code back into the main C5
branch
1.44
Branches (1)
• Each commit has one parent
• Branches are just names that can be C1
associated with a commit
– 'main' is the default branch feat1 main
– Created using: C2
git checkout -b branch-name
• You can only be working on one particular feat1
branch at a time C3
• Any commits are applied to the current
feat1
branch
C5
• Example:
– git checkout -b feat1
– git commit -m "Added part1"
– git commit -m "Added part2"
1.45
Conflicts
C1
• If the merge encounters updates that it is
not sure how to combine, it will leave the feat1 main
file in a conflicted state C2
• Can find conflicted files via:
– git status feat1
C3
• Contents of conflicted files must be main branch
C4 (other
manually combined feat1 developers or
– Conflicted areas are highlight with <<<<, C5 fixes to C2)
====, >>>> with the contents of each
branch
– Edit the file to your desired final contents C5
– Then add and commit
If you have questions, please
<<<<<<< HEAD
open an issue
Sample =======
Conflicted File ask your question in IRC.
>>>>>>> feat1
1.47
Remotes
upstream
• Remotes are just like their name
github.com:usc-ee250/repo
repo repo
file1.cpp file1.cpp
indicates: remote locations where we file2.cpp file2.cpp
Forks
• A fork is a "copy" of a repository
– Essentially a new repo whose starting point is the current
state of the original, "forked" repo
– Allows changes to be made (like a branch) or starting a new
project based on some current codebase
• If the original fork changes, there are means to pull those updates into
your fork
– It is possible to fork a fork ☺
• Example
– The sensors we use have Python library support available on Icons made by Dave Gandy
from Flaticon is licensed by
Github Creative Commons CC 3.0 BY
– We have forked that repo and made some changes for EE
250
– You will then fork our repo (i.e. a fork of a fork) and modify
it with your lab group
• If we make changes in our repo, you can easily bring them into your fork
1.49
Upstreams
• Common definitions
– upstream: The parent repository from which you forked
– downstream: The forked ("child") repository (i.e. your repo)
• Common usage
– The upstream fork can be thought of as just another remote
– While the remote named origin usually refers to your fork on github, the
remote named upstream usually refers to the parent of your fork
• Setting up access to the upstream fork
– See https://help.github.com/articles/fork-a-repo/
– git remote -v Icons made by Dave Gandy
from Flaticon is licensed by
– git remote add upstream parent-fork-url Creative Commons CC 3.0 BY
An Example
• Suppose we create a repo for you: p1-ttrojan
– It comes preloaded (because of actions we took) with some code that was from
our own repo: p1-skel
– git clone git@github.com:usc-ee250-fall2023/p1-ttrojan-fork
– cd p1-ttrojan
– # You make changes; add, commit, push
• Now we make changes to p1-skel, how can you get and merge those
changes in?
– git remote -v # list the remotes
– git remote add upstream git@github.com:usc-ee250-
fall2023/p1-skel
– git fetch upstream # d/l changes to a temp area
– git checkout main # make sure you're in your main branch
– git merge upstream/main # Update your code
1.51
Summary
• git add file(s)
– Stage a file to be committed
• git commit -m "Change summary"
– Makes a snapshot of the code you added
• git checkout -b branch-name
– Create a branch and switch to it
• git pull
– Download commits from your remote repository
• git push
– Upload your local commits to the remote repository
• git checkout branch-name
– Switch to a new branch
• git merge other-branch-name
– Merge the commits from other-branch-name into current branch
• HEAD is synonymous with the current branch's latest commit
• origin is usually the remote name for your repo on github
• upstream is usually the remote your repo was forked from (must be added)
1.52
Helpful Links
• https://help.github.com/
• http://rogerdudler.github.io/git-guide/files/git_cheat_sheet.pdf
• https://learngitbranching.js.org/
– Main: Level 1 - Intro to Git Commits
– Main: Level 2 - Ramping Up
– Remotes: Level 1: Push & Pull – Git Remotes
– Remotes: Level 2: To Origin and Beyond
• https://services.github.com/on-demand/downloads/github-git-cheat-
sheet/ (web version)
• https://services.github.com/on-demand/downloads/github-git-cheat-
sheet.pdf (print version)
1.53
Summary
• You should…
– Feel generally comfortable writing basic Python
scripts that manipulate strings, integers, etc. and
call library functions
– Understand the basic git add, commit, push, pull
process
– Understand branches/remotes
– Know to use sudo apt-get install or pip3 to install
Linux packages or python-specific packages
1.54
LINUX
1.55
The Linux OS
• Based on the Unix operating system
• Developed as an open-source ("free") alternative by
Linux Torvalds and several others starting in 1991
• Commonly used in industry and in embedded
devices
• Both your VM and your RPi will be running Linux so
we want you to feel comfortable using it
rpi-ttrojan
1.56
ssh Connections
• ssh stands for Secure Shell
– Encrypted protocol to run a terminal
(command line) on a remote server
• Usage This Photo by Unknown Author is
licensed under CC BY-NC-SA
– $ ssh username@server
– $ ssh ttrojan@aludra.usc.edu
• Use your USC password first, then try 10-
digit ID rpi-ttrojan
– $ ssh pi@rpi-ttrojan
1.57
Terminal/Command Line
• No more GUI!
– ssh connections provide only command line
interfaces to the remote machine
• Have to be comfortable navigating the file
system, using command line utilities and text-
based editors
– Paths (e.g. ../ee250/Grove-Pi/examples)
– Utilities (cd, mv, cp, rm, mkdir, cat, more, grep)
– Text-based editors (vi, emacs, nano)
1.58
sudo Humor
https://imgs.xkcd.com/comics/sandwich.png
https://imgs.xkcd.com/comics/incident.png
1.61
Environment Variables
• Contain values that can be accessed by
other programs that provide system and
other info Makefile
– PATH OBJECTS = lcd.o adc.o proj.o
CC = gcc
• Where to look for executables
– LD_LIBRARY_PATH proj.elf: $(OBJECTS)
$(CC) –o proj.elf $(OBJECTS)
• Where to look for libraries lcd.o: lcd.h lcd.c