From 340d2a46ff8b83186f8f75017fde87c5cc201b1a Mon Sep 17 00:00:00 2001 From: Randy Cork Date: Tue, 14 Mar 2017 17:22:37 +0000 Subject: [PATCH] Finally submitting all the work I've done --- rhaegal/FromWork/GenericJoin.py | 51 +++++++++++++++++++++++ rhaegal/FromWork/genericJoin.cmd | 20 +++++++++ rhaegal/FromWork/join.cmd | 1 + rhaegal/FromWork/join.py | 42 +++++++++++++++++++ rhaegal/FromWork/split.py | 59 +++++++++++++++++++++++++++ rhaegal/FromWork/splitDrop.py | 48 ++++++++++++++++++++++ rhaegal/FromWork/verifyDrop.cmd | 39 ++++++++++++++++++ rhaegal/Session3/mailroom.py | 15 +++++-- rhaegal/Session5/exceptions_lab.py | 10 +++++ rhaegal/Session6/Sparse.py | 11 +++++ rhaegal/Session6/cigar_party.py | 7 ++++ rhaegal/Session6/test_cigar_party.py | 61 ++++++++++++++++++++++++++++ 12 files changed, 361 insertions(+), 3 deletions(-) create mode 100755 rhaegal/FromWork/GenericJoin.py create mode 100755 rhaegal/FromWork/genericJoin.cmd create mode 100755 rhaegal/FromWork/join.cmd create mode 100755 rhaegal/FromWork/join.py create mode 100755 rhaegal/FromWork/split.py create mode 100755 rhaegal/FromWork/splitDrop.py create mode 100755 rhaegal/FromWork/verifyDrop.cmd create mode 100644 rhaegal/Session5/exceptions_lab.py create mode 100644 rhaegal/Session6/Sparse.py create mode 100644 rhaegal/Session6/cigar_party.py create mode 100755 rhaegal/Session6/test_cigar_party.py diff --git a/rhaegal/FromWork/GenericJoin.py b/rhaegal/FromWork/GenericJoin.py new file mode 100755 index 0000000..c9980e9 --- /dev/null +++ b/rhaegal/FromWork/GenericJoin.py @@ -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') + while 1: + 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 \ No newline at end of file diff --git a/rhaegal/FromWork/genericJoin.cmd b/rhaegal/FromWork/genericJoin.cmd new file mode 100755 index 0000000..266fbd5 --- /dev/null +++ b/rhaegal/FromWork/genericJoin.cmd @@ -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 + pause + exit + ) + if "%1"=="-help" ( + echo "Usage: join.cmd + pause + exit + ) + set joinPath=%1 + set outputName=%2 +) + +copy /B %joinPath%\part0??? %outputName%.zip diff --git a/rhaegal/FromWork/join.cmd b/rhaegal/FromWork/join.cmd new file mode 100755 index 0000000..c873f77 --- /dev/null +++ b/rhaegal/FromWork/join.cmd @@ -0,0 +1 @@ +copy /B .\part0* %outputName%.zip diff --git a/rhaegal/FromWork/join.py b/rhaegal/FromWork/join.py new file mode 100755 index 0000000..cf0ade2 --- /dev/null +++ b/rhaegal/FromWork/join.py @@ -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 \ No newline at end of file diff --git a/rhaegal/FromWork/split.py b/rhaegal/FromWork/split.py new file mode 100755 index 0000000..617dae0 --- /dev/null +++ b/rhaegal/FromWork/split.py @@ -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 \ No newline at end of file diff --git a/rhaegal/FromWork/splitDrop.py b/rhaegal/FromWork/splitDrop.py new file mode 100755 index 0000000..9d36c74 --- /dev/null +++ b/rhaegal/FromWork/splitDrop.py @@ -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") \ No newline at end of file diff --git a/rhaegal/FromWork/verifyDrop.cmd b/rhaegal/FromWork/verifyDrop.cmd new file mode 100755 index 0000000..12bf174 --- /dev/null +++ b/rhaegal/FromWork/verifyDrop.cmd @@ -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 +for /f "delims=: skip=1" %%G in (hash.txt) do if not defined line set "line=%%G" +echo %line%>hash.txt +set line= \ No newline at end of file diff --git a/rhaegal/Session3/mailroom.py b/rhaegal/Session3/mailroom.py index f06e1cc..9eddcf0 100644 --- a/rhaegal/Session3/mailroom.py +++ b/rhaegal/Session3/mailroom.py @@ -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) @@ -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"): diff --git a/rhaegal/Session5/exceptions_lab.py b/rhaegal/Session5/exceptions_lab.py new file mode 100644 index 0000000..11be45a --- /dev/null +++ b/rhaegal/Session5/exceptions_lab.py @@ -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") \ No newline at end of file diff --git a/rhaegal/Session6/Sparse.py b/rhaegal/Session6/Sparse.py new file mode 100644 index 0000000..ca8756e --- /dev/null +++ b/rhaegal/Session6/Sparse.py @@ -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]) \ No newline at end of file diff --git a/rhaegal/Session6/cigar_party.py b/rhaegal/Session6/cigar_party.py new file mode 100644 index 0000000..42fc60b --- /dev/null +++ b/rhaegal/Session6/cigar_party.py @@ -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) \ No newline at end of file diff --git a/rhaegal/Session6/test_cigar_party.py b/rhaegal/Session6/test_cigar_party.py new file mode 100755 index 0000000..260d5f4 --- /dev/null +++ b/rhaegal/Session6/test_cigar_party.py @@ -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