In this chapter, we explore a few more things about programming; specifically, tools used to write, store (version control), build, debug, and document code. In addition, we explore miscellaneous topics like exceptions.
Note: The examples below are abridged; the book contains more details.
# Example 1 - config
git config --global user.name "John Smith"
git config --global user.email "[email protected]"
# Example 2 - init
# change to parent directory where repository should lie
cd dir_name
# create a new directory representing repository
git init respository_name
# enter directory representng new repository
cd respository_name
# Example 3 - add
touch new_file # create an empty file
git add new_file # add new file to staging area
git commit -m "demo commit: adding an empty file new_file"
# Example 4 - status
git status
# Example 5 - another commit
echo 123456 >>! new_file # add some text to empty file
# commit new version of new_file
git commit -m "added 123456 to file" new_file
# Example 6 - another commit
git commit -a -m "updating multiple files"
# Example 7 - branch
git branch new_branch # create a new branch
git branch # list all branches
# Example 8 - rev-parse
git rev-parse master~2 # display SHA-1 codename
git rev-parse --short master~2 # display shortened SHA-1 codename
# Example 9 - reverts to the previous version
git init demo_repos
cd demo_repos
echo 123 > demo_file
git add demo_file
git commit -a -m "adding demo_file containing 123"
echo 456 >>! demo_file
git commit -a -m "adding 456 to demo_file"
cat demo_file
git checkout master~1 # change version to previous version
cat demo_file
# Example 10 - merge
git branch feature1
git checkout feature1 ## Switched to branch 'feature1'
echo abc > feature_file
git add feature_file
git commit -a -m "added feature_file with abc"
git checkout master ## Switched to branch 'master'
git merge feature1 # merge feature1 branch into master
ls # both files appear in master after the merge
# Example 11 - rebase
git checkout feature1
git rebase master # rebase master into feature1 branch
# Example 12 - clone
git clone ssh://[email protected]/path/projectName.git
# Example 13 - log
git log | head
# Example 14 - diff
git diff master~1 master | head
# Example 1 - copy
svn copy trunk path-to-branch/branch_name
# Example 2 - merge
svn merge path-to-branch/branch_name
<?xml version="1.0"?>
<project name="hello" default="build">
<target name="build" description="build project">
<mkdir dir="classes"/>
<javac destdir="classes">
<src path="src"/>
</javac>
</target>
<target name="clean" description="clean class files">
<delete>
<fileset dir="classes">
<include name="*.class"/>
</fileset>
</delete>
</target>
</project>
import math
def foo(x):
if not isinstance(x, int):
raise TypeError("arguments to foo must be integers")
if x <= 0:
raise ValueError("arguments to foo must be positive")
return(x + math.log(x))
foo(-3) # exception triggered and program halted
foo(3) # never executed
import math
def foo(x):
if not isinstance(x, int):
raise TypeError("arguments to foo must be integers")
if x <= 0:
raise ValueError("arguments to foo must be positive")
return(x + math.log(x))
try:
foo(-3) # exception triggered and program halted
foo(3) # never executed
except:
print("exception in sequence of foo() calls")
print("program resumes execution and is not terminated")
import math
def foo(x):
if not isinstance(x, int):
raise TypeError("arguments to foo must be integers")
if x <= 0:
raise ValueError("arguments to foo must be positive")
return(x + math.log(x))
try:
foo(-3) # exception triggered and program halted
foo(3) # never executed
except TypeError: # handle TypeError exceptions
print("Incorrect Type in sequence of foo() calls")
except ValueError: # handle ValueError exceptions
print("Non-positive value in sequence of foo() calls")
else:
print("no exceptions handled")
finally:
print("always handled")
print("program resumes execution and is not terminated")
import math
def foo(x):
if not isinstance(x, int):
raise TypeError("arguments to foo must be integers")
if x <= 0:
raise ValueError(f"arguments to foo must be positive: {x}")
return(x + math.log(x))
try:
foo(-3) # exception triggered and program halted
foo(3) # never executed
except TypeError: # handle TypeError exceptions
print("Incorrect Type in sequence of foo() calls")
except ValueError as e: # handle ValueError exceptions
print("Non-positive value in sequence of foo() calls:")
print(e.args)
print("program resumes execution and is not terminated")
import math
# Custom exception: either type or value error
class InvalidTypeOrValueError(Exception):
def __init__(self, a_val, a_type): # custom constructor
# calls super class constructor (Python 3 syntax)
super().__init__("incorrect type and value. " +
"val: " + str(a_val) + " " + "type: " + str(a_type))
self.val = a_val
self.type = a_type
def foo(x):
if not isinstance(x, int) or x<=0:
raise InvalidTypeOrValueError(x,type(x))
return(x + math.log(x))
try:
foo(-3)
foo(3)
except InvalidTypeOrValueError as e:
print("Incorrect type or value in sequence of foo() calls: ")
print(e.args)
class Point(object):
"A point in a two-dimensional space"
def __init__(self, x=0, y=0):
"""
Initializes the point object to the origin by default,
or otherwise the initializes to the passed x,y arguments.
"""
self.x = x
self.y = y
def __del__(self):
"Prints a message when an object is destroyed."
print("destructing a point object")
def displayPoint(self):
"Prints the object by displaying its x and y coordinates."
print("x: %f, y: %f" % (self.x, self.y))
help(Point) # shows class doctring
help(Point.displayPoint) # shows method docstring
# Example 1
foo2 = function(i) {
a = i + 1;
b = a + 1;
browser();
a = b + 1;
return(b)
}
foo2(3)
# Example 2
foo3 = function(i) {
a = i + 1;
b = a + 1;
a = b + 1;
return(b)
}
debug(foo3)
foo3(1)
undebug(foo3)
# Example 3
# start profiling
Rprof("diagonsisFile.out")
A = runif(1000) # generate a vector of random numbers in [0,1]
B = runif(1000) # generate another random vector
C = eigen(outer(A,B))
Rprof(NULL) # end profiling
summaryRprof("diagonsisFile.out")