This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Finally submitting all the work I've done (plus some 2.7 stuff) #93
Open
cardinalorange
wants to merge
1
commit into
UWPCE-PythonCert:master
Choose a base branch
from
cardinalorange:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
while 1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
copy /B .\part0* %outputName%.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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