From cdfb130080e660a95363f8a491570816948695dc Mon Sep 17 00:00:00 2001 From: ehemdal Date: Sat, 25 Dec 2021 13:59:58 -0500 Subject: [PATCH 1/7] initial work --- Ch2 - Basics/classes_start.py | 47 ++++++++++++++++++++++++++++++ Ch2 - Basics/conditionals_start.py | 28 ++++++++++++++++-- Ch2 - Basics/functions_start.py | 33 +++++++++++++++++++-- Ch2 - Basics/helloworld_start.py | 8 +++-- Ch2 - Basics/loops_start.py | 20 +++++++++---- 5 files changed, 125 insertions(+), 11 deletions(-) diff --git a/Ch2 - Basics/classes_start.py b/Ch2 - Basics/classes_start.py index de3226d..f822801 100644 --- a/Ch2 - Basics/classes_start.py +++ b/Ch2 - Basics/classes_start.py @@ -2,4 +2,51 @@ # Example file for working with classes # LinkedIn Learning Python course by Joe Marini # +class Vehicle(): + def __init__(self, bodystyle): + self.bodystyle = bodystyle + + def drive(self, speed): + self.mode = "driving" + self.speed = speed + +class Car(Vehicle): + def __init__(self, enginetype): + super().__init__("Car") + self.wheels = 4 + self.doors = 4 + self.enginetype = enginetype + + def drive(self, speed): + super().drive(speed) + print("Driving my", self.enginetype,"car at speed",self.speed) + + +class Motorcycle(Vehicle): + def __init__(self, enginetype, hassidecar): + super().__init__("Motorcycle") + self.hassidecare = hassidecar + if(hassidecar): + self.wheels = 3 + else: + self.wheels = 2 + self.doors = 0 + self.enginetype = enginetype + + def drive(self, speed): + super().drive(speed) + print("Driving my", self.enginetype,"motorcycle at speed",self.speed) + + +car1 = Car("gas") +car2 = Car("electric") +mc1 = Motorcycle("gas", True) +print(mc1.wheels) +print(car1.wheels) +print(mc1.enginetype) +car1.drive(30) +car2.drive(40) +mc1.drive(50) + + diff --git a/Ch2 - Basics/conditionals_start.py b/Ch2 - Basics/conditionals_start.py index f6b58d6..f255411 100644 --- a/Ch2 - Basics/conditionals_start.py +++ b/Ch2 - Basics/conditionals_start.py @@ -5,15 +5,39 @@ +from types import ClassMethodDescriptorType + + def main(): - x, y = 10, 100 + x, y = 100, 100 # conditional flow uses if, elif, else + if x < y: + result = "x is less than y" + elif x == y: + result = "x is the same as y" + else: + result = "x is greater than y" + + print(result) # conditional statements let you use "a if C else b" + result = "x is less than y" if x < y else "x is greater or equal to y" + print(result) # match-case makes it easy to compare multiple values - value = "one" + value = "George" + match value: + case "one": + result = 1 + case "two": + result = 2 + case "three" | "four": + result = (3, 4) + case _: # default + result = -1 + + print(result) if __name__ == "__main__": main() diff --git a/Ch2 - Basics/functions_start.py b/Ch2 - Basics/functions_start.py index 56cb247..7091d09 100644 --- a/Ch2 - Basics/functions_start.py +++ b/Ch2 - Basics/functions_start.py @@ -5,17 +5,46 @@ # TODO: define a basic function - +def func1(): + print("I am in func1") # TODO: function that takes arguments - +def func2(arg1, arg2): + print(arg1," ",arg2) # TODO: function that returns a value +def cube(x): + return x * x * x # TODO: function with default value for an argument +def power(num, x=1): + result = 1 + for i in range(x): + result = result * num + return result # TODO: function with variable number of arguments +def multi_add(*args): + result = 0 + for x in args: + result = result + x + return result + + + + +func1() +print(func1()) +print(func1) + +func2(10, 20) +print(func2(10, 20)) +print(cube(3)) +print(power(2)) +print(power(2,3)) +print(power(x = 3, num = 2)) +print(multi_add(4,5,10,4)) diff --git a/Ch2 - Basics/helloworld_start.py b/Ch2 - Basics/helloworld_start.py index 7d6b753..1dcef0c 100644 --- a/Ch2 - Basics/helloworld_start.py +++ b/Ch2 - Basics/helloworld_start.py @@ -1,6 +1,10 @@ # # Example file for HelloWorld # LinkedIn Learning Python course by Joe Marini -# - +def Hello(): + print("Hello World!") + name = input("What is your name?") + print("Nice to meet you", name) +if __name__ == "__main__": + Hello() diff --git a/Ch2 - Basics/loops_start.py b/Ch2 - Basics/loops_start.py index f7d2e75..20a69db 100644 --- a/Ch2 - Basics/loops_start.py +++ b/Ch2 - Basics/loops_start.py @@ -8,20 +8,30 @@ def main(): x = 0 # TODO: define a while loop - + while(x < 5): + print(x) + x += 1 # TODO: define a for loop - + for x in range(5, 10): + print(x) # TODO: use a for loop over a collection days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] - + for d in days: + print(d) + # TODO: use the break and continue statements - + for x in range(5, 10): + if (x % 2 == 0): + continue + print(x) + # TODO: using the enumerate() function to get index - + for i,d in enumerate(days): + print(i, d) if __name__ == "__main__": main() From 159c6abb721b0458e0baaf9a7bd581959a80b85e Mon Sep 17 00:00:00 2001 From: ehemdal Date: Sat, 25 Dec 2021 14:49:38 -0500 Subject: [PATCH 2/7] Adding my challenge and a missed file. --- Ch2 - Basics/challenge.py | 25 +++++++++++++++++++++++++ Ch2 - Basics/modules_start.py | 10 +++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 Ch2 - Basics/challenge.py diff --git a/Ch2 - Basics/challenge.py b/Ch2 - Basics/challenge.py new file mode 100644 index 0000000..8e61078 --- /dev/null +++ b/Ch2 - Basics/challenge.py @@ -0,0 +1,25 @@ +# Challenge program + +def palindrome(teststring): + newstring = teststring[::-1] + return teststring == newstring + +running = True +while(running): + teststring = input("Input a string: ") + + # Should we exit? + if teststring == 'exit': + break + + # Force to lowercase + teststring = teststring.lower() + + # Remove punctuation + newstr = "" + for x in teststring: + if x.isalnum(): + newstr += x + + # Return the result + print("Palindrome?", palindrome(newstr)) \ No newline at end of file diff --git a/Ch2 - Basics/modules_start.py b/Ch2 - Basics/modules_start.py index 8c8bf6c..532912a 100644 --- a/Ch2 - Basics/modules_start.py +++ b/Ch2 - Basics/modules_start.py @@ -3,12 +3,16 @@ # TODO: import the math module, which contains features for working with mathematics - +import math # TODO: the math module contains lots of pre-built functions - +print("The square root of 16 is:", math.sqrt(16)) # TODO: in addition to functions, some modules contain useful constants - +print("Pi is",math.pi) # TODO: try some of the math functions for yourself here: +print(math.exp(0)) +print(math.exp(1)) +print(math.factorial(300)) + From 377b08aecc5474cf94b399aab81612a9e120ba80 Mon Sep 17 00:00:00 2001 From: ehemdal Date: Wed, 29 Dec 2021 16:27:17 -0500 Subject: [PATCH 3/7] Adding file and ospath exercises --- Ch3 - Files/file.txt | 80 ++++++++++++++++++ Ch3 - Files/files_start.py | 15 +++- Ch3 - Files/ospathutils_start.py | 19 +++-- file.txt | 140 +++++++++++++++++++++++++++++++ 4 files changed, 246 insertions(+), 8 deletions(-) create mode 100644 Ch3 - Files/file.txt create mode 100644 file.txt diff --git a/Ch3 - Files/file.txt b/Ch3 - Files/file.txt new file mode 100644 index 0000000..8f4befd --- /dev/null +++ b/Ch3 - Files/file.txt @@ -0,0 +1,80 @@ +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text diff --git a/Ch3 - Files/files_start.py b/Ch3 - Files/files_start.py index fb026bb..222aca4 100644 --- a/Ch3 - Files/files_start.py +++ b/Ch3 - Files/files_start.py @@ -9,15 +9,24 @@ def main(): # Open the file for appending text to the end - + file = open("file.txt","a+") # write some lines of data to the file - + for i in range(10): + file.write("This is a line of text\n") # close the file when done - + file.close() # Open the file back up and read the contents + myfile = open("file.txt", "r") + if myfile.mode == "r": + contents = myfile.read() + # contents = myfile.readlines() + # for x in contents: + # print(x) + + print(contents) if __name__ == "__main__": diff --git a/Ch3 - Files/ospathutils_start.py b/Ch3 - Files/ospathutils_start.py index 3384cbd..3f5bbb1 100644 --- a/Ch3 - Files/ospathutils_start.py +++ b/Ch3 - Files/ospathutils_start.py @@ -12,19 +12,28 @@ def main(): # Print the name of the OS - + print(os.name) # Check for item existence and type - - + print("Item exists: ", path.exists("file.txt")) + print("Item is a file: ", path.isfile("file.txt")) + print("Item is a directory: ", path.isdir("file.txt")) # Work with file paths + print("Item's path: ", path.realpath("file.txt")) + print("Item's relative path: ", path.relpath("file.txt")) + print("File's path and name: ", path.split(path.realpath("file.txt"))) # Get the modification time + # Convert (ctime) to a readable string, print it out + t = time.ctime(path.getmtime("file.txt")) + print(t) + print(datetime.datetime.fromtimestamp(path.getmtime("file.txt"))) - # Calculate how long ago the item was modified - + td = datetime.datetime.now() - datetime.datetime.fromtimestamp(path.getmtime("file.txt")) + print("It has been", td, "since the file was modified") + print("Or,", td.total_seconds(), "seconds") if __name__ == "__main__": main() diff --git a/file.txt b/file.txt new file mode 100644 index 0000000..97a5716 --- /dev/null +++ b/file.txt @@ -0,0 +1,140 @@ +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text From 57964715fa6f8b7dd62a644b12bd9912c1ec0dfb Mon Sep 17 00:00:00 2001 From: ehemdal Date: Wed, 29 Dec 2021 17:34:54 -0500 Subject: [PATCH 4/7] Committing all chapter 3 exercises --- Ch3 - Files/archive.zip | Bin 0 -> 4540 bytes Ch3 - Files/challenge.py | 29 +++++++++ Ch3 - Files/{file.txt => file.txt.bak} | 0 Ch3 - Files/newfile.txt | 80 +++++++++++++++++++++++++ Ch3 - Files/results/results.txt | 16 +++++ Ch3 - Files/shell_start.py | 21 +++++-- Ch3 - Files/testzip.zip | Bin 0 -> 6080 bytes Ch3 - Files/textfile.txt | 80 +++++++++++++++++++++++++ 8 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 Ch3 - Files/archive.zip create mode 100644 Ch3 - Files/challenge.py rename Ch3 - Files/{file.txt => file.txt.bak} (100%) create mode 100644 Ch3 - Files/newfile.txt create mode 100644 Ch3 - Files/results/results.txt create mode 100644 Ch3 - Files/testzip.zip create mode 100644 Ch3 - Files/textfile.txt diff --git a/Ch3 - Files/archive.zip b/Ch3 - Files/archive.zip new file mode 100644 index 0000000000000000000000000000000000000000..30d7a53147df00efecc2ddf0e54fb14b5c135588 GIT binary patch literal 4540 zcmbVQXH*mG77e{u73oMvnn>tyFN7i>QiJr4p@tq15UCggF!xr8f609h68&-bR+w-qo;5Pg zcDGe}XC}dCf8|wj`TY)d;%%yowuXwZ5=qOb3XqI{xUs~-U&r=yMjdG!*KINvHeIIa z)*SB>;2)7$DRkaf!I*O9Yi44DH@jyef~y=y*ZF$^yb4adQN&D?Fe%byfH)z5YCL;u?9#0{t%s#4S*l`Zw7gkKHT*I~(Yr1eU%X80v3$&=nwF3*qtuh7D_0?O% z_()vIu}zijYP4=uDR_2Qk#z_q-QewgeBx(i(m$`r10Qn|%CTt4P8 z@HOGYbC;-Q)ToCCLGiX?ua0)WznKsg5X3dm*cXhH)bps}NZBB3BLif?t(2 zBDXH@U7MMUYOFLKV#-t@>?UQ%g!X#7`Y>*Fx>vs8e>Q*OaMRrJ&{u7yd0<*nW1@TS z@n>5MY-Z39L38!6hyh~pb@QPp9r7eP>=>Cc6 zW9{JV?(E|PxrZZ7BzVf@gBa~YYitN3v~x0F^37Uju#&Q}@``E!fok;viGU-v&HhgZ;S6J` zD47?8WI55U3W7%JbZ9hKo^qEKPo3P0OUx7h^<^~%uP2=bJvSY!Yu4P1bku_fc*e_{ z#v&VFf|)2|D z8d0gL;XOq&@{WciRDAIFN~s7If#uzb2{LT$3~2M_3`=XQSfuu1LGw)%dG8BqM2u zQHLldJ(m!O<)a^BxKzGv!hBF(UCUDIlI~hU)=p*Z&&qvy04Z7T3~EpY^UH=%uNenY zXacrA1SPQ>N;kkW*oM3sbUu%4=uf&eYrlZJG3c!WLTxO~$L%fSB26s^Dh(#yQ^JGc zKQ{Q!$iyFv0H+a$bNS;E&xlJr_5Tv@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_hr6O4 z+GwN|@HcelJIa`vNkn&()ugIYtpeomDNsj6?Uv%~4k}{ZUMKHXfu17XHC=3P5^KX1 zo|!Jl!gXG5Tg(_`mtOzAKT@A{Zh&dy9E@l{`inMPgMt4eGXBe$U#-Orj7rE-qCE8@ z46+iXg+WRMtPil`Mw31_obdUcgk~hahAOwW?E+Yu&8SgKii$@8>`@Bx9hI+`x$dtDH=1sYIK#Q;NvpeTL+P4!w8#35*8-v0Y%RH*MZ6xhJZxjZxzU~(pL)2D zrS2ip!W9O9^2EB7R-|Uzd4yH$L|xPBfBIdTvh5KLk^0~iSVlRHc&HA6R;2;g5_A=J zC>LaTNEw|9#julN;T|ZNj#u$z_9ewcu2W+TcaZ+V65wsbK-@}PyeDTX7s37Kn;n1l zI6Gnr8ndH^Z%U+BIq2@>$rw;A70~4{N%;RIm~GgTHxzm-;!ZDGxKVVCf~vM+^igPq zK3ODnWBjMV=m)WUHQz=nKIUi`?7oK}3cT5gd<^!Bm{gG*B9-bVgH9FoR)qa~`|9-xN*eoQQ=K92{e%2X zYn_K*q|DFYmbk`ydSf5!UnO)Ndy!&1!{QPDrXSA(PuKi!U3dmG!~xF>gLB^Se2|Ou z#90tq(%*wzwEl}m%vppa@(Tve`2}3mZ_ePII8y%Jea>SqV(=NZ4Cjx|^I1=e2=`7- Q|C6Br6yh=ppgH~ZA0mB-8~^|S literal 0 HcmV?d00001 diff --git a/Ch3 - Files/challenge.py b/Ch3 - Files/challenge.py new file mode 100644 index 0000000..a08b9ba --- /dev/null +++ b/Ch3 - Files/challenge.py @@ -0,0 +1,29 @@ +import os +from os import path + +# Get a list of all the files in the current directory +dirlist = os.listdir() + +# Start a running total of byte count for all files +bytecount = 0 +# For each file, add its byte count to the total. Skip directories +for file in dirlist: + if path.isfile(file): + bytecount += path.getsize(path.realpath(file)) +print("Bytecount = " + str(bytecount)) + +# Then create a new subdirectory called "results" +if path.exists("results") == False: + os.mkdir("results") + +# In this directory, create a file called "results.txt" +outfile = open("./results/results.txt", "w+") + +# In results.txt, print the "Total bytecount" and a list of the filenames +outfile.write("Total Bytecount: " + str(bytecount) + "\n") +outfile.write("File List\n---------\n") +for file in dirlist: + if path.isfile(file): + outfile.write(file + "\n") + +outfile.close() diff --git a/Ch3 - Files/file.txt b/Ch3 - Files/file.txt.bak similarity index 100% rename from Ch3 - Files/file.txt rename to Ch3 - Files/file.txt.bak diff --git a/Ch3 - Files/newfile.txt b/Ch3 - Files/newfile.txt new file mode 100644 index 0000000..8f4befd --- /dev/null +++ b/Ch3 - Files/newfile.txt @@ -0,0 +1,80 @@ +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text diff --git a/Ch3 - Files/results/results.txt b/Ch3 - Files/results/results.txt new file mode 100644 index 0000000..0d18299 --- /dev/null +++ b/Ch3 - Files/results/results.txt @@ -0,0 +1,16 @@ +Total Bytecount: 24752 +File List +--------- +archive.zip +challenge.py +challenge_solution.py +file.txt.bak +files_finished.py +files_start.py +newfile.txt +ospathutils_finished.py +ospathutils_start.py +shell_finished.py +shell_start.py +testzip.zip +textfile.txt diff --git a/Ch3 - Files/shell_start.py b/Ch3 - Files/shell_start.py index 5cb9ec5..522906f 100644 --- a/Ch3 - Files/shell_start.py +++ b/Ch3 - Files/shell_start.py @@ -5,20 +5,33 @@ import os from os import path +import shutil +from shutil import make_archive +from zipfile import ZipFile def main(): # make a duplicate of an existing file - if path.exists("textfile.txt"): + if path.exists("file.txt.bak"): # get the path to the file in the current directory - + src = path.realpath("file.txt") + # let's make a backup copy by appending "bak" to the name - + # dst = src + ".bak" + # shutil.copy(src, dst) + # rename the original file + # os.rename("file.txt", "textfile.txt") + # shutil.copy("textfile.txt", "newfile.txt") # now put things into a ZIP archive + # root_dir, tail = path.split(src) + # shutil.make_archive("archive", "zip", root_dir) # more fine-grained control over ZIP files - + with ZipFile("testzip.zip","w") as newzip: + newzip.write("textfile.txt") + newzip.write("newfile.txt") + newzip.write("file.txt.bak") if __name__ == "__main__": main() diff --git a/Ch3 - Files/testzip.zip b/Ch3 - Files/testzip.zip new file mode 100644 index 0000000000000000000000000000000000000000..5a6c8410ce8037e1cda21773bb4d536599d7cdeb GIT binary patch literal 6080 zcmWIWW@Zs#0D+R$xxt~#m(m;985lsA2PBqSQIeLKld4xzQ4*4oS*!p=i3&NHd8rEd zX$l}&UanE`(Qp_|52N{Fv>X^M2S&>Q+LZ$V@S3^`t)}J%*3@~a<)an#Xv1W*fi~J8 z9c`G8mII^Zz`&IQh>AM51+z;HtEKgl60=8}%%e@_(IzwPn#`m5Yczk2=C9HGg{vG0 z@MdHZVZc574H^~)fo+W-5|o! Date: Sun, 2 Jan 2022 13:12:37 -0500 Subject: [PATCH 5/7] Completing ch. 4 files. --- Ch4 - Dates and Times/challenge_start.py | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Ch4 - Dates and Times/challenge_start.py b/Ch4 - Dates and Times/challenge_start.py index 9da42cb..487d900 100644 --- a/Ch4 - Dates and Times/challenge_start.py +++ b/Ch4 - Dates and Times/challenge_start.py @@ -3,3 +3,42 @@ # import calendar + +def countdays(year, month, day): + daycount = 0 + c = calendar.monthcalendar(year, month) + for row in c: + if row[day] != 0: + daycount += 1 + return daycount + +running = True +try: + while(running == True): + print("Enter the day of the week:") + print("0 - Monday") + print("1 - Tuesday") + print("2 - Wednesday") + print("3 - Thursday") + print("4 - Friday") + print("5 - Saturday") + print("6 - Sunday") + + entry = input("? ") + + if entry == "exit": + running = False + break + day = int(entry) + # Get the month and year + month = input("Enter the month 1 - 12: ") + month = int(month) + year = input("Enter the year: ") + year = int(year) + + result = countdays(year, month, day) + print("There are " + str(result) + " days in the month") +except Exception as e: + print("Invalid input") + print(e) + From a9d273fbd2779c78787277a64e365e9c998eb856 Mon Sep 17 00:00:00 2001 From: ehemdal Date: Sun, 2 Jan 2022 13:13:02 -0500 Subject: [PATCH 6/7] Committing chapter 4 files. --- .vs/ProjectSettings.json | 3 +++ .vs/VSWorkspaceState.json | 6 ++++++ .vs/learning-python/v16/.suo | Bin 0 -> 21504 bytes .vs/slnx.sqlite | Bin 0 -> 90112 bytes Ch4 - Dates and Times/calendars_start.py | 9 +++++++-- Ch4 - Dates and Times/dates_start.py | 19 +++++++++++++++---- Ch4 - Dates and Times/formatting_start.py | 7 ++++--- Ch4 - Dates and Times/timedeltas_start.py | 17 +++++++++++++---- 8 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 .vs/ProjectSettings.json create mode 100644 .vs/VSWorkspaceState.json create mode 100644 .vs/learning-python/v16/.suo create mode 100644 .vs/slnx.sqlite diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 0000000..f8b4888 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": null +} \ No newline at end of file diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 0000000..6b61141 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,6 @@ +{ + "ExpandedNodes": [ + "" + ], + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/.vs/learning-python/v16/.suo b/.vs/learning-python/v16/.suo new file mode 100644 index 0000000000000000000000000000000000000000..4cdb5f6009bf70092e0bf95cc2d7d65a1f38467e GIT binary patch literal 21504 zcmeHP+in|07#^n-D94tTP|Bg9gi@eRZO3uq0HsaRBTWx+nj&N&`Ix#n*s_y05#j+5 zFTgE4021O2xU3NEMFqDYAuf0WglPD_ogI6;+1;!UB(}R_{kuD}GxN_s{~W&gXT10C z?fdPU?+^VhEbgS(B_3|<5sz!(0X+99|6U;;!!yGkZftBYcoztdzU!hIm=Q(%R>Xif zBbIS5i*>5fv+EbTt2%#ei&T62Z~b-m?_UE45bxBBGV)b$EQ(v=0=Vb#bVjUgkxTvS zezZuvy6fNC`(_Y~=4&{`pL|PV5hW;#94LXhNP%)@Jg3{B=nFK;|12^qnHgRYtKhOI z3Xq>PuW_D2NZE<88BYG}hr2;6#}?6z*H>^={dy0c|J4Q1o>@oi+fU*C1W3JN*t76I z2YMbvzj9P-Kjs;P{NOmC`n%qz8Rr?iXJ3CBL_fz5_J2Opsrc>g|1W4fBq#sc&KjWJ zoajl^J>PNsr{38H^l_hkh~qPLv>(KIUDy9X_&EpY<3BON>!8D+0ni(uBcL}ygP^xS zM?st?IR3u_dKdH_=r||{8UmdFg+O6Y1T+kaf<{0w&?smO6bFri-UodE`Vgecds_2P z;rWc|Kc|H={>SiNvV`gRsONU-W;^nl0OBc%1+fIQQ^wsZ?m<-;?p5HA0G@~=SY8?G ze!Yy>)GggC_*TI860lAMNGKt08W?OvWDI0fkN%25Eyg1qv-h`3*xStP(cW6F9Pe6mJe3_%Y?)B~}p91k)mL8;pE-MDkEYJC0q5qPWd$Bw@oWwIzm z))=4M$`gb9T$`!2mMNC&?WD$E&0BVNmXYE9{O@i4XB%XFb(8bw|3`iPub<~7&dFos zoyV-enO-3P`yu7HIsS5O$aZfZt3LDsyu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s(eMN>YasJL~7!tVpY0+J1OvugTE7^4uQI;1Gzk0lyVXb z1{rBhJHqWj4LsJ`h9>TA)mbzReN(#KMN0#hR#rA?`rO%G@A7(8CZ(MF3r)yJij znA2gw#wI_KIPyykEsc{FE$`IWg9p z0fqoWfFZyTUvFa#I^3;~7!Lx3Uhnj*mF|6fzMS!N6Y eh5$o=A;1t|2rvW~0t^9$07HNwzz}#{5ct0?Q6-`P literal 0 HcmV?d00001 diff --git a/Ch4 - Dates and Times/calendars_start.py b/Ch4 - Dates and Times/calendars_start.py index 2963f70..eba080d 100644 --- a/Ch4 - Dates and Times/calendars_start.py +++ b/Ch4 - Dates and Times/calendars_start.py @@ -5,12 +5,17 @@ # TODO: import the calendar module - +import calendar # TODO: create a plain text calendar - +c = calendar.TextCalendar(calendar.MONDAY) +str = c.formatmonth(2022, 1, 0, 0) +print(str) # TODO: create an HTML formatted calendar +hc = calendar.HTMLCalendar(calendar.SUNDAY) +str = hc.formatmonth(2022, 1) +print(str) # TODO: loop over the days of a month diff --git a/Ch4 - Dates and Times/dates_start.py b/Ch4 - Dates and Times/dates_start.py index 9091c40..0d5a9a0 100644 --- a/Ch4 - Dates and Times/dates_start.py +++ b/Ch4 - Dates and Times/dates_start.py @@ -3,24 +3,35 @@ # LinkedIn Learning Python course by Joe Marini # - +from datetime import date +from datetime import time +from datetime import datetime def main(): ## DATE OBJECTS # TODO: Get today's date from the simple today() method from the date class + today = date.today() + print("Today's date is ", today) + - # TODO: print out the date's individual components + # TODO: print out the date's individual components + print("Date compoents: ", today.day, today.month, today.year) # TODO: retrieve today's weekday (0=Monday, 6=Sunday) - + print("Today's weekday number: ", today.weekday()) + days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] + print("which is a ", days[today.weekday()]) ## DATETIME OBJECTS # TODO: Get today's date from the datetime class - + today = datetime.now() + print(today) # TODO: Get the current time + t = datetime.time(datetime.now()) + print("Current time is ", t) diff --git a/Ch4 - Dates and Times/formatting_start.py b/Ch4 - Dates and Times/formatting_start.py index 6c40839..e8da541 100644 --- a/Ch4 - Dates and Times/formatting_start.py +++ b/Ch4 - Dates and Times/formatting_start.py @@ -12,12 +12,13 @@ def main(): #### Date Formatting #### - - # %y/%Y - Year, %a/%A - weekday, %b/%B - month, %d - day of month + now = datetime.now() + # %y/%Y - Year, %a/%A - weekday, %b/%B - month, %d - day of month + print(now.strftime("%a %B %d %Y")) # %c - locale's date and time, %x - locale's date, %X - locale's time - + print(now.strftime("%c which is %x and %X")) #### Time Formatting #### diff --git a/Ch4 - Dates and Times/timedeltas_start.py b/Ch4 - Dates and Times/timedeltas_start.py index a6b62bc..54d352d 100644 --- a/Ch4 - Dates and Times/timedeltas_start.py +++ b/Ch4 - Dates and Times/timedeltas_start.py @@ -7,27 +7,36 @@ from datetime import date from datetime import time from datetime import datetime +from datetime import timedelta # TODO: construct a basic timedelta and print it - +print(timedelta(days = 365, hours = 5, minutes = 1)) # TODO: print today's date +now = datetime.now() +print(now) # TODO: print today's date one year from now - +print("365 days from now it will be ", str(now + timedelta(days = 365))) # TODO: create a timedelta that uses more than one argument - +print("In two weeks and three days it will be ", str(now + timedelta(weeks = 2, days = 3))) # TODO: calculate the date 1 week ago, formatted as a string - +print("One week ago it was "+ str(now - timedelta(weeks =1))) ### How many days until April Fools' Day? +today = date.today() +afd = date(today.year, 4, 1) # TODO: use date comparison to see if April Fool's has already gone for this year +if (afd < today): + afd.replace(year = today.year + 1) +time_to_afd = afd - today +print("There are ", time_to_afd.days, "to the next AFD") # if it has, use the replace() function to get the date for next year From c13f2d33bd5a107658af3ddc211634af7ba6387b Mon Sep 17 00:00:00 2001 From: ehemdal Date: Tue, 4 Jan 2022 16:29:31 -0500 Subject: [PATCH 7/7] Last of the exercises --- Ch5 - Internet Data/htmlparsing_start.py | 26 ++++++++++++++++++--- Ch5 - Internet Data/inetdata_start.py | 7 +++++- Ch5 - Internet Data/jsondata_start.py | 29 ++++++++++++++++++++---- Ch5 - Internet Data/xmlparsing_start.py | 20 ++++++++++++---- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/Ch5 - Internet Data/htmlparsing_start.py b/Ch5 - Internet Data/htmlparsing_start.py index a759ac3..73594a7 100644 --- a/Ch5 - Internet Data/htmlparsing_start.py +++ b/Ch5 - Internet Data/htmlparsing_start.py @@ -4,16 +4,35 @@ # from html.parser import HTMLParser +# global +paragraphs = 0 class MyHTMLParser(HTMLParser): def handle_comment(self, data): - pass + print("Encountered a comment: ", data) + pos = self.getpos() + print("at line ", pos[0], "position ", pos[1]) def handle_starttag(self, tag, attrs): - pass + print("Encountered a starttag: ", tag) + pos = self.getpos() + print("at line ", pos[0], "position ", pos[1]) + #use the global + global paragraphs + if tag == "p": + paragraphs += 1 + if len(attrs) > 0: + print("Tag attributes:") + for a in attrs: + print("\t", a[0], "=", a[1]) + def handle_data(self, data): - pass + if data.isspace(): + return + print("Encountered data: ", data) + pos = self.getpos() + print("at line ", pos[0], "position ", pos[1]) def main(): # instantiate the parser and feed it some HTML @@ -23,6 +42,7 @@ def main(): if f.mode == "r": contents = f.read() # read the entire file parser.feed(contents) + print("Paragraph tags: ", paragraphs) if __name__ == "__main__": main() diff --git a/Ch5 - Internet Data/inetdata_start.py b/Ch5 - Internet Data/inetdata_start.py index 86dc094..f574379 100644 --- a/Ch5 - Internet Data/inetdata_start.py +++ b/Ch5 - Internet Data/inetdata_start.py @@ -2,9 +2,14 @@ # Example file for retrieving data from the internet # LinkedIn Learning Python course by Joe Marini # +import urllib.request def main(): - pass # this is a placeholder, do-nothing statement + weburl = urllib.request.urlopen("http://www.google.com") + print("Result code: ", weburl.getcode()) + data = weburl.read() + print("Page data:\n", data) + if __name__ == "__main__": main() diff --git a/Ch5 - Internet Data/jsondata_start.py b/Ch5 - Internet Data/jsondata_start.py index e0da623..c995fb5 100644 --- a/Ch5 - Internet Data/jsondata_start.py +++ b/Ch5 - Internet Data/jsondata_start.py @@ -4,25 +4,39 @@ # import urllib.request +import json def printResults(data): # Use the json module to load the string data into a dictionary theJSON = json.loads(data) # now we can access the contents of the JSON like any other Python object - + if "title" in theJSON["metadata"]: + print(theJSON["metadata"]["title"]) # output the number of events, plus the magnitude and each event name - + count = theJSON["metadata"]["count"] + print(count, "events recorded") # for each event, print the place where it occurred + for i in theJSON["features"]: + print(i["properties"]["place"]) + print("..............\n") # print the events that only have a magnitude greater than 4 - - + for i in theJSON["features"]: + if i["properties"]["mag"] >= 4.0: + print(i["properties"]["place"]) + print("..............\n") # print only the events where at least 1 person reported feeling something - + print("Events that were felt and reported:") + for i in theJSON["features"]: + feltReports = i["properties"]["felt"] + if feltReports != None: + if feltReports > 0: + print(i["properties"]["place"], feltReports, "times") + print("..............\n") def main(): # define a variable to hold the source URL @@ -33,6 +47,11 @@ def main(): # Open the URL and read the data webUrl = urllib.request.urlopen(urlData) print ("result code: " + str(webUrl.getcode())) + if (webUrl.getcode() == 200): + data = webUrl.read() + printResults(data) + else: + print("Error from the server, cannot print results: ", webUrl.getcode()) if __name__ == "__main__": diff --git a/Ch5 - Internet Data/xmlparsing_start.py b/Ch5 - Internet Data/xmlparsing_start.py index 3129b0c..985a2ca 100644 --- a/Ch5 - Internet Data/xmlparsing_start.py +++ b/Ch5 - Internet Data/xmlparsing_start.py @@ -2,21 +2,31 @@ # Example file for parsing and processing XML # LinkedIn Learning Python course by Joe Marini # - +import xml.dom.minidom def main(): # use the parse() function to load and parse an XML file - + doc = xml.dom.minidom.parse("samplexml.xml") # print out the document node and the name of the first child tag - + print(doc.nodeName) + print(doc.firstChild.tagName) # get a list of XML tags from the document and print each one + skills = doc.getElementsByTagName("skill") + print("There are ", skills.length, "skills listed") + for skill in skills: + print(skill.getAttribute("name")) # create a new XML tag and add it into the document - - + newskill = doc.createElement("skill") + newskill.setAttribute("name","jQuery") + doc.firstChild.appendChild(newskill) + skills = doc.getElementsByTagName("skill") + print("There are now ", skills.length, "skills listed") + for skill in skills: + print(skill.getAttribute("name")) if __name__ == "__main__": main()