# chapter6.py # imports ''' Python Core python.org -> fully available in your code print(), input(), int(),... thousands 3.14 ( class 3.13 ) Python Standard Libraries (chapter 6) -> These modules are INSTALLED with your core BUT you have to use the import keyword in your code Custom Modules -> your defined objects (import) (chapter 3) Package Installation Programs (pip install packagename) When you need to use the import keyword, 3 variations to import modules option 1 import ModuleName Chapter5.py import Chapter5 option 2 from ModuleName import ObjName or names from Chapter5 import Product, customer1 option 3 import ModuleName as NameSpace import Chapter5 as c5 -> access all objects using c5.objName With a namespace, you CAN NOT access the objects thru the Module Name Bob string High Level char B char o char B ascii B 0100 0010 o 0110 1111 b 0110 0010 Low Level 1 byte -> 8 bits 0/1 OS/Inter 01000010 01101111 01100010 --- VL -> processor ''' # Standard Libraries # 0 easy 10 hard # print 0 core # datetime 2 standard page 67 # os 4 standard page 69 # subprocess (processor threads) 10 standard page 71 # collections (data models) page 77 # graphic on page 64 standard modules # read pages 64-66 concepts ''' DateTime Codes Blue Murach book -> page 307 %a -> Abbreviated weekday name %A -> Full weekday name %b -> Abbreviated month name %B -> Full month name %d -> Zero-padded day of month as a number %m -> Zero-padded month as a number %Y -> 4-digit year %y -> 2-digit year %H -> Hour of day in 24-hour format %I -> Hour of day in 12-hour format %M -> Minute as number %S -> Second as number %p -> am/pm %f -> microsecond ''' # datetime Module import datetime as dt / from datetime import datetime ## datetime class -> dt.datetime.methods() / datetime.methods() ### YYYY/MM/DD HH:MM:SS.mmmmmm ## date class ### YYYY/MM/DD 12:00:00.000000 or 00:00:00.000000 ## time class ### HH:MM:SS.mmmmmm ## timedelta -> calculates datetime invoicedate + terms<30> = duedate # pip install holidays -> a good holiday module import datetime # import ModuleName # Current timestamp currentDateTime = datetime.datetime.now() # datetime type print(currentDateTime) # 2025-11-14 10:35:50.846490 # Very common to have to format the datetime class type to string OR even take form data (string, int, etc) and format to datetime class. ## format datetime to string -> strftime() ## format string to datetime -> strptime() # 2025-11-14 10:35:50.846490 datetime.now() # 11.14.2025 report date ## "%m.%d.%Y" shortNumberDTFormat = "%m.%d.%Y" # November 14, 2025 ## "%B %d, %Y" fullMonthDTFormat = "%B %d, %Y" # Friday November 14, 2025 ## "%A %B %d, %Y" fullMonthDTFormatWeekday = "%A %B %d, %Y" reportDate = currentDateTime.strftime(shortNumberDTFormat) print(reportDate) # 11.14.2025 # timedelta example invoiceDate = currentDateTime # datetime class terms = 30 dueDate = invoiceDate + datetime.timedelta(days=terms) formatDueDate = dueDate.strftime(shortNumberDTFormat) print(formatDueDate) # 12.14.2025 # os module ## path and file creation / navigation import os # Get the current working directory -> python is execute at runtime "root" currentWorkingDirectory = os.getcwd() print(currentWorkingDirectory) # C:\Users\Student\Desktop\PythonLevel2_1113 # logfile directories per month # \Nov2025LogFiles\ dirDTformat = "%b%Y" folderName = currentDateTime.strftime(dirDTformat) + "LogFiles" # pip install pyinstaller or exe try: os.mkdir(folderName) except FileExistsError: print(f"Directory {folderName} exists.") # template variable prefer option print("Directory " + folderName + " exists.") # join operator # subprocess for thread/task management ## cpu/memory/disk/network "resource allocation" ## a part of being pythonic (readability/framework/resource allocation) # Sales Order Class # variable name program, parameter name and the property name class SAME class SalesOrder: def __init__(self, orderId, customerName, reportDate): self.orderId = orderId # 1001 self.customerName = customerName self.reportDate = reportDate # Work Order Class class WorkOrder: def __init__(self, workId, description, reportDate): self.workId = workId self.description = description self.reportDate = reportDate # Purchase Order Class class PurchaseOrder: def __init__(self, purchaseId, description, vendor, reportDate): self.purchaseId = purchaseId self.description = description self.vendor = vendor self.reportDate = reportDate # RuntimeModule -> Main -> Sub Processes # chapter6 -> SO -> WO/PO orderId = 1001 customerName = "Bob Smith" workId = f"WO_{orderId}" # WO_1001 purchaseId = f"PO_{orderId}" # PO_1001 description = f"Sales Order for {customerName}. Date Ordered by Customer: {reportDate}. Materials ordered from vendors on purchase order #{purchaseId}. Deliver and install onsite. Bill customer invoice using {workId}, due Net 30 on {formatDueDate}." vendor = "Home Depot" # Create a "new instance of" class objects AND pass values expected so = SalesOrder(orderId, customerName, reportDate) wo = WorkOrder(workId, description, reportDate) po = PurchaseOrder(purchaseId, description, vendor, reportDate) # Task Manager for subprocesses import subprocess print("==== Sales Order Process ====") print(f"Start of SO process: {datetime.datetime.now()}.") print(f"Sales Order ID: {so.orderId}.") # define the wo task wo_cmd = [ # 4 elements exe,options,outputs,inputs "python", # py instead of python -> run python.exe "-c", ( "import sys;" "print('Work Order Process');" "workId, reportDate = sys.argv[1], sys.argv[2];" "print(f'Work Order ID: {workId}');" "print(f'Report Date: {reportDate}');" ), # wrap the python syntax in a tuple for the output() wo.workId, wo.reportDate # sys.argvs[1], sys.args[2] ] # wo_cmd # define the po task po_cmd = [ "python", "-c", ( "import sys;" "print('Purchase Order Process');" "vendor, purchaseId = sys.argv[3], sys.argv[1];" "print(f'Purchase ID: {purchaseId}');" "print(f'Vendor Name: {vendor}');" ), po.purchaseId, po.description, po.vendor #[1],[2],[3] ] # po_cmd # start the wo task ## 2 seconds print(f"Start of WO process: {datetime.datetime.now()}.") wo_process = subprocess.Popen(wo_cmd) # start the wo_cmd process as a subprocess of chapter6.so # start the po task ## 7 seconds print(f"Start of PO process: {datetime.datetime.now()}.") po_process = subprocess.Popen(po_cmd) # wait for all the subprocesses to exit (0,1) ## wait -> return code print() wo_returnCode = wo_process.wait() # Exit code 0 or 1 po_returnCode = po_process.wait() # Exit code 0 or 1 print(f"End of subprocesses: {datetime.datetime.now()}.") print(f"Work Order SubProcess exit code: {wo_returnCode}") print(f"Purchase Order SubProcess exit code: {po_returnCode}") # Finish the main process print(f"Main SO process finished at: {datetime.datetime.now()}. ") ''' ==== Sales Order Process ==== Start of SO process: 2025-11-14 15:08:01.245457. Sales Order ID: 1001. Start of WO process: 2025-11-14 15:08:01.245706. Start of PO process: 2025-11-14 15:08:01.250848. Work Order Process Work Order ID: WO_1001 Report Date: 11.14.2025 Purchase Order Process Purchase ID: PO_1001 Vendor Name: Home Depot End of subprocesses: 2025-11-14 15:08:01.309447. Work Order SubProcess exit code: 0 Purchase Order SubProcess exit code: 0 Main SO process finished at: 2025-11-14 15:08:01.310185. ''' # EOF