Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Finally submitting all the work I've done (plus some 2.7 stuff) #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions rhaegal/FromWork/GenericJoin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/python
##########################################################
# join all part files in a dir created by split.py.
# This is roughly like a 'cat fromdir/* > tofile' command
# on unix, but is a bit more portable and configurable,
# and exports the join operation as a reusable function.
# Relies on sort order of file names: must be same length.
# Could extend split/join to popup Tkinter file selectors.
##########################################################

import os, sys
readsize = 1024

def join(fromdir, tofile):
output = open(tofile, 'wb')
parts = os.listdir(fromdir)
parts.sort( )
for filename in parts:
if not "MD5" in filename:
if "part" in filename:
filepath = os.path.join(fromdir, filename)
fileobj = open(filepath, 'rb')
Copy link
Contributor

@mdgregor mdgregor Mar 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to use a context manager here so you don't have to worry about closing your file object:
with open(filepath, 'rb') as fileobj

https://docs.python.org/2/reference/compound_stmts.html#the-with-statement

while 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while True is probably the convention you should use.

filebytes = fileobj.read(readsize)
if not filebytes: break
output.write(filebytes)
fileobj.close( )
output.close( )
join
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-help':
print 'Use: join.py [from-dir-name to-file-name]'
else:
if len(sys.argv) != 3:
interactive = 1
fromdir = raw_input('Directory containing part files? ')
tofile = raw_input('Name of file to be recreated? ')
else:
interactive = 0
fromdir, tofile = sys.argv[1:]
absfrom, absto = map(os.path.abspath, [fromdir, tofile])
print 'Joining', absfrom, 'to make', absto

try:
join(fromdir, tofile)
except:
print 'Error joining files:'
print sys.exc_type, sys.exc_value
else:
print 'Join complete: see', absto
if interactive: raw_input('Press Enter key') # pause if clicked
20 changes: 20 additions & 0 deletions rhaegal/FromWork/genericJoin.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@echo off
if %1.==. (
set /p joinPath="Path to Join: "
set /p outputName="Name of Zip: "
) else (
if "%1"=="-h" (
echo "Usage: join.cmd <Path of split files> <output name>
pause
exit
)
if "%1"=="-help" (
echo "Usage: join.cmd <Path of split files> <output name>
pause
exit
)
set joinPath=%1
set outputName=%2
)

copy /B %joinPath%\part0??? %outputName%.zip
1 change: 1 addition & 0 deletions rhaegal/FromWork/join.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
copy /B .\part0* %outputName%.zip
42 changes: 42 additions & 0 deletions rhaegal/FromWork/join.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/python
##########################################################
# join all part files in a dir created by split.py.
# This is roughly like a 'cat fromdir/* > tofile' command
# on unix, but is a bit more portable and configurable,
# and exports the join operation as a reusable function.
# Relies on sort order of file names: must be same length.
# Could extend split/join to popup Tkinter file selectors.
##########################################################

import os, sys
import glob
readsize = 1024

def join(fromdir, tofile):
output = open(tofile, 'wb')
parts=glob.glob(fromdir+'\part0???')
print parts
for filename in parts:
fileobj = open(filename, 'rb')
while 1:
filebytes = fileobj.read(readsize)
if not filebytes: break
output.write(filebytes)
fileobj.close( )
output.close( )
join
if __name__ == '__main__':

fromdir = "."
tofile = ".\\Output.zip"

absfrom, absto = map(os.path.abspath, [fromdir, tofile])
print 'Joining', absfrom, 'to make', absto

try:
join(fromdir, tofile)
except:
print 'Error joining files:'
print sys.exc_type, sys.exc_value
else:
print 'Join complete: see', absto
59 changes: 59 additions & 0 deletions rhaegal/FromWork/split.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/python
#########################################################
# split a file into a set of portions; join.py puts them
# back together; this is a customizable version of the
# standard unix split command-line utility; because it
# is written in Python, it also works on Windows and can
# be easily tweaked; because it exports a function, it
# can also be imported and reused in other applications;
#########################################################

import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
gigabytes = megabytes * 1000
chunksize = int(gigabytes) # default 1 gig

def split(fromfile, todir, chunksize=chunksize):
if not os.path.exists(todir): # caller handles errors
os.mkdir(todir) # make dir, read/write parts
else:
for fname in os.listdir(todir): # delete any existing files
os.remove(os.path.join(todir, fname))
partnum = 0
input = open(fromfile, 'rb') # use binary mode on Windows
while 1: # eof=empty string from read
chunk = input.read(chunksize) # get next part <= chunksize
if not chunk: break
partnum = partnum+1
filename = os.path.join(todir, ('part%04d' % partnum))
fileobj = open(filename, 'wb')
fileobj.write(chunk)
fileobj.close() # or simply open( ).write( )
input.close( )
assert partnum <= 9999 # join sort fails if 5 digits
return partnum

if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-help':
print 'Use: split.py [file-to-split target-dir [chunksize]]'
else:
if len(sys.argv) < 3:
interactive = 1
fromfile = raw_input('File to be split? ') # input if clicked
todir = raw_input('Directory to store part files? ')
else:
interactive = 0
fromfile, todir = sys.argv[1:3] # args in cmdline
if len(sys.argv) == 4: chunksize = int(sys.argv[3])
absfrom, absto = map(os.path.abspath, [fromfile, todir])
print 'Splitting', absfrom, 'to', absto, 'by', chunksize

try:
parts = split(fromfile, todir, chunksize)
except:
print 'Error during split:'
print sys.exc_type, sys.exc_value
else:
print 'Split finished:', parts, 'parts are in', absto
if interactive: raw_input('Press Enter key') # pause if clicked
48 changes: 48 additions & 0 deletions rhaegal/FromWork/splitDrop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
##Call from the root of the installer
import hashlib
import os
import sys
import split

#Get the MD5 of a file using chunk size 4k
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()

if __name__ == '__main__':
#Make sure that the MD5 file doesn't exist
try:
os.remove("MD5.txt")
except OSError:
pass
#Walk through all files and split those over 2gb
for root, dirs, files in os.walk("."):
for name in files:
file=os.path.join(root, name)
if (os.path.getsize(file) > 2147483648): #hardcode 2gb
#create a directory of the same name as the file for the splitfiles, fail if it exists (something is wrong)
dirSplit=file.split(".")
dirName="."+dirSplit[1]
os.makedirs(dirName) #it will split the first period (cwd)
#Call split.py on the file and put it in the created directory
split.split(file, dirName, 1000000000)
os.remove(file)

MD5File=open("MD5.txt", 'w+')
#Walk through all the files in CWD and get the MD5s
for root, dirs, files in os.walk("."):
for name in files:
file=os.path.join(root, name)
MD5Sum=md5(file)
unsplitLine=file+"="+MD5Sum
splitLine=""
addSpace=False
#Add spaces every 2 characters like certutil does
for charIndex in range(unsplitLine.index("=")+1,len(unsplitLine)):
splitLine+=unsplitLine[charIndex]
if addSpace: splitLine+=" "
addSpace=not addSpace
MD5File.write(file +"="+splitLine.strip()+"\n")
39 changes: 39 additions & 0 deletions rhaegal/FromWork/verifyDrop.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@echo off

REM
REM :: Check the health of all downloaded files in MD5.txt
REM

setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
set exitCode=0

if exist MD5.txt (
for /F "tokens=1,2 delims==" %%I in (MD5.txt) do (
if NOT "%%I" == ".\MD5.txt" (
if NOT "%%I" == ".\FullSystemInstaller\DONTINDX.MSA" (
call :GetValue %%I
set /P value=<hash.txt
if NOT "!value!" == "%%J" (
echo The following file is corrupt: %%I
set exitCode=1
)
)
)
)
del hash.txt
) else (
echo "The file containing the MD5s of the packages is not present: MD5.txt"
pause
exit 1
)
if %exitCode%==1 echo The installer is not healthy. Please see above errors.
if not %exitCode%==1 echo The installer is healthy.
pause
exit %exitCode%
goto :EOF
:GetValue
@echo off
call certutil -hashfile %1 MD5>hash.txt
for /f "delims=: skip=1" %%G in (hash.txt) do if not defined line set "line=%%G"
echo %line%>hash.txt
set line=
15 changes: 12 additions & 3 deletions rhaegal/Session3/mailroom.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
def safe_input(str):
while 1:
try:
user_input=input(str)
break
except (EOFError, KeyboardInterrupt):
print("That's not a valid input")
return user_input

def sendThankYou():
response2=input("Full Name?")
response2=safe_input("Full Name?")
while response2=="list":
print(donorList.keys())
response2=input("Full Name?")
if not response2 in donorList:
donorList[response2]=[]
response3=input("Donation Amount?")
response3=safe_input("Donation Amount?")
while not response3.isdigit():
response3=input("I'm sorry, that is not a valid number. Please try again:")
donorList[response2].append(response3)
Expand Down Expand Up @@ -63,7 +72,7 @@ def maxSize(dict):
donorList[value].append(random.randrange(1,1000))

while 1:
response=input("Would you like to \"Create a report\" or \"Send a thank you\"?")
response=safe_input("Would you like to \"Create a report\" or \"Send a thank you\"?")
if (response.lower()=="create a report" or response.lower()=="report"):
createReport()
elif (response.lower()=="send a thank you" or response.lower()=="thank you"):
Expand Down
10 changes: 10 additions & 0 deletions rhaegal/Session5/exceptions_lab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def safe_input(str):
while 1:
try:
user_input=input(str)
break
except (EOFError, KeyboardInterrupt):
print("That's not a valid input")
return user_input

safe_input("Please provide input")
11 changes: 11 additions & 0 deletions rhaegal/Session6/Sparse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def SparseArray (array):
pass

sa = SparseArray([1,2,0,0,0,0,3,0,0,4])
len(sa)
sa[5]=12
sa[3]=0
del sa[4]
print(sa)
sa.append(12)
print(sa[2:4])
7 changes: 7 additions & 0 deletions rhaegal/Session6/cigar_party.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def cigar_party(cigars, is_weekend):
if cigars>39:
if cigars<61 or is_weekend:
return True
return False

cigar_party(2,False)
61 changes: 61 additions & 0 deletions rhaegal/Session6/test_cigar_party.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python

"""
When squirrels get together for a party, they like to have cigars.
A squirrel party is successful when the number of cigars is between
40 and 60, inclusive. Unless it is the weekend, in which case there
is no upper bound on the number of cigars.

Return True if the party with the given values is successful,
or False otherwise.
"""


# you can change this import to test different versions
from cigar_party import cigar_party
# from cigar_party import cigar_party2 as cigar_party
# from cigar_party import cigar_party3 as cigar_party


def test_1():
assert cigar_party(30, False) is False


def test_2():
assert cigar_party(50, False) is True


def test_3():
assert cigar_party(70, True) is True


def test_4():
assert cigar_party(30, True) is False


def test_5():
assert cigar_party(50, True) is True


def test_6():
assert cigar_party(60, False) is True


def test_7():
assert cigar_party(61, False) is False


def test_8():
assert cigar_party(40, False) is True


def test_9():
assert cigar_party(39, False) is False


def test_10():
assert cigar_party(40, True) is True


def test_11():
assert cigar_party(39, True) is False