Initial transfer of larger repo from git. IF full history and branches are needed must go to github at https://github.com/DulcisL/CCUCoding.git
This commit is contained in:
commit
c09ce40be0
4048 files changed
+603256
No files matched your search
Binary file not shown.
@@ -0,0 +1,17 @@
|
||||
class Car ():
|
||||
_name = str
|
||||
|
||||
def __init__ (self):
|
||||
self._name = 'No name'
|
||||
|
||||
def setName (self, name: str):
|
||||
self._name = name
|
||||
|
||||
def getName (self):
|
||||
return self._name
|
||||
|
||||
def driveCar(self):
|
||||
print ('car drove 1 mile.')
|
||||
|
||||
def __str__(self):
|
||||
return f'The car is named {self._name}'
|
||||
@@ -0,0 +1,99 @@
|
||||
import os
|
||||
class FileServices():
|
||||
#variables (attributes)
|
||||
_mode = ''
|
||||
_data = ''
|
||||
_fileName = ''
|
||||
_filePath = ''
|
||||
_myTextFile = None
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
self._mode = 'w'
|
||||
self._data = 'Test Data'
|
||||
self._fileName = 'thisTestFile.txt'
|
||||
self._filePath = 'docs/files/'
|
||||
self._myTextFile = None
|
||||
#set (Mutator)
|
||||
def setMode(self, mode):
|
||||
self._mode = mode
|
||||
def setData(self, data):
|
||||
self._data = data
|
||||
def setFileName(self, fileName):
|
||||
self._fileName = fileName
|
||||
#get (Accessory)
|
||||
def getMode(self):
|
||||
return self._mode
|
||||
def getData(self):
|
||||
return self._data
|
||||
def getFileName(self):
|
||||
return self._fileName
|
||||
#Behaviors
|
||||
#errors thrown by writing to a file
|
||||
#io.UnsupportedOperation
|
||||
#TypeError
|
||||
#ValueError
|
||||
#FileNotFoundError
|
||||
def writeToFile(self):
|
||||
#local scope to method
|
||||
try:
|
||||
#local scope to try
|
||||
self._myTextFile = open(self._fileName, self._mode)
|
||||
# write data to the text file after open is completed
|
||||
self._myTextFile.write(f'{self._data}\n')
|
||||
# close
|
||||
# close will end the programs connection to the file
|
||||
# will save the data in the text file.
|
||||
# will cause the file to be unusable and may corrupt the file
|
||||
# completes the file
|
||||
except TypeError:
|
||||
print(f'Check your mode for correct data type for mode: {self._mode}, must be a string.')
|
||||
except ValueError:
|
||||
print(f'Check your mode you entered and invalid mode: {self._mode}.')
|
||||
except FileNotFoundError:
|
||||
print('You file does not exist.')
|
||||
except:
|
||||
print('Error: check your mode and data entered. Mode is string, Data is string')
|
||||
finally:
|
||||
self._closeFile()
|
||||
|
||||
def readFromFile(self):
|
||||
#open file in a mode to read
|
||||
self._myTextFile = open(self._fileName, self._mode)
|
||||
content = self._myTextFile.read()
|
||||
print(content)
|
||||
self._closeFile()
|
||||
|
||||
def readLinesFromFile(self):
|
||||
#open file in a mode to read
|
||||
self._myTextFile = open(self._fileName, self._mode)
|
||||
contentList = self._myTextFile.readlines()
|
||||
|
||||
for line in contentList:
|
||||
print(line)
|
||||
|
||||
self._closeFile()
|
||||
return contentList
|
||||
|
||||
def createDir(self):
|
||||
newDirectory = 'MyNewDir'
|
||||
while not os.path.exists(newDirectory):
|
||||
os.mkdir(newDirectory)
|
||||
|
||||
#to create a file and keep open to write multiple times in your program
|
||||
def createFile(self):
|
||||
pass
|
||||
|
||||
def moveFileOrDirectory(self):
|
||||
pass
|
||||
|
||||
def deleteFileOrDirectory(self):
|
||||
pass
|
||||
|
||||
#a private method that can only be used in this class
|
||||
#this is call a helper method
|
||||
def _closeFile(self):
|
||||
self._myTextFile.close()
|
||||
|
||||
#str method
|
||||
def __str__(self):
|
||||
return f'Mode: {self._mode} Data: {self._data} FileName: {self._fileName} Path: {self._filePath}'
|
||||
@@ -0,0 +1,75 @@
|
||||
#Write to file
|
||||
#create a text file called testDataFile and write test data and date
|
||||
#create a new variable to hold the file descriptor object (file instance)
|
||||
#default mode is r only to read
|
||||
#modes to write r+, w, w+, a, a+
|
||||
testFileInstance = open('testDataFile.txt', 'w')
|
||||
testFileInstance.write('Test Data\n')
|
||||
testFileInstance.write('10/21/2022')
|
||||
#you must close your file to save
|
||||
testFileInstance.close()
|
||||
|
||||
#read a file
|
||||
#change the mode of the file to a mode to read r, r+ and w+
|
||||
testFileInstanceRead = open('testDataFile.txt', 'r')
|
||||
myDataFromTextFile = testFileInstanceRead.read()
|
||||
print(myDataFromTextFile)
|
||||
#close File
|
||||
testFileInstanceRead.close()
|
||||
|
||||
#create directory
|
||||
#named dirCreateFiles2
|
||||
#import the os from python (allows python access files on computer)
|
||||
import os
|
||||
directoryToCreate = 'dirCreateFiles2'
|
||||
directoryToCreateMove = 'dirCreateFilesMove'
|
||||
#check if directory already exist if so skip else create
|
||||
while not os.path.exists(directoryToCreate):
|
||||
os.mkdir(directoryToCreate)
|
||||
while not os.path.exists(directoryToCreateMove):
|
||||
os.mkdir(directoryToCreateMove)
|
||||
|
||||
#move a file
|
||||
#move testDataFile.txt to dir dirCreateFiles2
|
||||
#source file (file you want to move)
|
||||
#destination file (Were you want the file to be moved to)
|
||||
#Will raise exception if file exists already
|
||||
'''sourceFile = 'testDataFile.txt'
|
||||
destination = 'dirCreateFiles2/testDataFile.txt'
|
||||
os.rename(sourceFile, destination)
|
||||
|
||||
#Move to the other directory
|
||||
sourceFile = 'dirCreateFiles2/testDataFile.txt'
|
||||
destination = 'dirCreateFilesMove/testDataFile.txt'
|
||||
os.rename(sourceFile, destination)
|
||||
|
||||
#Note will raise exception if file doesn't exist beforehand
|
||||
fileToRemove = 'myData.txt'
|
||||
#delete a file
|
||||
os.remove(fileToRemove)
|
||||
'''
|
||||
|
||||
#Save an object to a file to store
|
||||
import pickle
|
||||
import CarService
|
||||
|
||||
carClassFile = 'CarData.txt'
|
||||
carFileObject = open(carClassFile, 'wb')
|
||||
myNewCar = CarService.Car()
|
||||
myNewCar.setName('Mustang GT')
|
||||
#Store in the file
|
||||
pickle.dump(myNewCar, carFileObject)
|
||||
|
||||
#Restore the car object and all data from the text file
|
||||
#destroy car object in memory
|
||||
del myNewCar
|
||||
|
||||
carFileObject.close()
|
||||
#Restore
|
||||
#create a file descriptor object in read mode
|
||||
carFileInRead = open(carClassFile, 'rb')
|
||||
myNewCar = pickle.load(carFileInRead)
|
||||
|
||||
print(myNewCar)
|
||||
|
||||
carFileInRead.close()
|
||||
@@ -0,0 +1,33 @@
|
||||
#CSV File - Comma Seporated Value
|
||||
#text table format
|
||||
#file rows are equal to table rows
|
||||
|
||||
#example
|
||||
#ClassName, ClassNumber, ClassRoom
|
||||
#Intermediate Programming, CSCI 145, AOC2 240
|
||||
#Into to Multimedia Apps, CSCI 375, AOC2 217
|
||||
#Human Computer Interaction, CSCI 444, Online
|
||||
#Software Engineering II, CSCI 490, AOC2 240
|
||||
#Fundamentails of Data-Driven Programming, IST 600, Online
|
||||
|
||||
#create a CSV File
|
||||
myCSVFile = open('classList.csv', 'w')
|
||||
myCSVFile.write('ClassName, ClassNumber, ClassRoom\n')
|
||||
myCSVFile.write('Intermediate Programming, CSCI 145, AOC2 240\n')
|
||||
myCSVFile.write('Into to Multimedia Apps, CSCI 375, AOC2 217\n')
|
||||
myCSVFile.write('Human Computer Interaction, CSCI 444, Online\n')
|
||||
myCSVFile.write('Software Engineering II, CSCI 490, AOC2 240\n')
|
||||
myCSVFile.write('Fundamentails of Data-Driven Programming, IST 600, Online\n')
|
||||
myCSVFile.close()
|
||||
#JSON File
|
||||
|
||||
#create a JSON file
|
||||
import json
|
||||
myJSONFile = open('testJSONData.json', 'w')
|
||||
myJSONFile.write('{ "fruit": "Apple", "size": "Large", "color": "Red"}')
|
||||
myJSONFile.close()
|
||||
|
||||
jsonString = '{ "fruit": "Apple", "size": "Large", "color": "Red"}'
|
||||
jsonObject = json.loads(jsonString)
|
||||
print(jsonObject['size'])
|
||||
#convert JSON string to a python object.
|
||||
@@ -0,0 +1,87 @@
|
||||
#working with files
|
||||
#why write to files
|
||||
#no database required
|
||||
#the ability to save data when program has ended
|
||||
#the ability to restore data when program is started
|
||||
|
||||
#create a text file
|
||||
#create a file object variable
|
||||
#open takes a file path
|
||||
#R - Read
|
||||
#W = Write
|
||||
#O - Overwrites the file
|
||||
#Ab - Appends to beginning
|
||||
#Ae - Appends to end
|
||||
#E - Generates errors
|
||||
#mode r - RE
|
||||
#mode r+ - RWAe
|
||||
#mode w+ - RWAb
|
||||
#mode a - WAeE
|
||||
#mode w - WOAb
|
||||
|
||||
#load file to use class and methods
|
||||
import FileServices
|
||||
fs = FileServices.FileServices()
|
||||
#create a Directory
|
||||
fs.createDir()
|
||||
|
||||
|
||||
data = 'This is my first text file\nThis is the second line of my first text file'
|
||||
fs.setData(data)
|
||||
fs.setFileName('myData.txt')
|
||||
fs.setMode('a')
|
||||
fs.writeToFile()
|
||||
print(fs)
|
||||
fs.setData(fs)
|
||||
fs.writeToFile()
|
||||
fs.setData("line 2 of data")
|
||||
fs.writeToFile()
|
||||
fs.setData("line 3 of data")
|
||||
fs.writeToFile()
|
||||
fs.setData("line 4 of data")
|
||||
fs.writeToFile()
|
||||
print('Data from file')
|
||||
fs.setMode('r')
|
||||
fs.readFromFile()
|
||||
print('Data from file using read lines')
|
||||
myListFromFile = fs.readLinesFromFile()
|
||||
print("This is the 5th line from the text document")
|
||||
print(myListFromFile[4])
|
||||
|
||||
|
||||
|
||||
#open file again
|
||||
# myTextFile = open('myData.txt', 'a')
|
||||
# myTextFile.write('This will append to the top of the file\n')
|
||||
# myTextFile.write('---------------------------------------\n')
|
||||
#close file again
|
||||
# myTextFile.close()
|
||||
|
||||
#change modes to write to file
|
||||
myTextFile = open('myData.txt', 'r')
|
||||
lines = myTextFile.readlines()
|
||||
countLine = 1
|
||||
for line in lines:
|
||||
print(f'{countLine} -> {line}')
|
||||
countLine += 1
|
||||
myTextFile.close()
|
||||
|
||||
#where is my file
|
||||
findFile = open('myData.txt', 'a')
|
||||
print(findFile.tell())
|
||||
findFile.close()
|
||||
|
||||
#find data in a location in a file
|
||||
locateData = open('myData.txt', 'r')
|
||||
#first argument is position, second argument is option
|
||||
#options
|
||||
#0 - absolute value
|
||||
#1 - relative to current position
|
||||
#2 - Relative to the end
|
||||
# print(locateData.seek(12, 0))
|
||||
# findDataAtlocation = locateData.readlines()
|
||||
# print(f'Data at location 2 {findDataAtlocation[locateData.seek(2,0)]}')
|
||||
|
||||
#+f causes an error FileNotFoundError
|
||||
#catch the error and create the file if error occured
|
||||
#Import the file with the class
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
|
||||
Test Data
|
||||
10/21/2022
|
||||
@@ -0,0 +1,2 @@
|
||||
Test Data
|
||||
10/21/2022
|
||||
@@ -0,0 +1,32 @@
|
||||
#working with files
|
||||
#why write to files
|
||||
#no database required
|
||||
#the ability to save data when program has ended
|
||||
#the ability to restore data when program is started
|
||||
|
||||
#create a text file
|
||||
#create a file object variable
|
||||
#open takes a file path
|
||||
#R - Read
|
||||
#W = Write
|
||||
#O - Overwrites the file
|
||||
#Ab - Appends to beginning
|
||||
#Ae - Appends to end
|
||||
#E - Generates errors
|
||||
#mode r - RE
|
||||
#mode r+ - RWAe
|
||||
#mode w+ - RWAb
|
||||
#mode a - WAeE
|
||||
#mode w - WOAb
|
||||
|
||||
#+f causes an error FileNotFoundError
|
||||
#catch the error and create the file if error occurred
|
||||
#Import the file with the class
|
||||
|
||||
#exceptions - a process to control errors that may happen in your program
|
||||
#in programming this is how you estimate and handle error to keep your program from crashing.
|
||||
|
||||
#ask user to enter numbers (enter any character to stop)
|
||||
|
||||
#Get an items from a list at index 5 and index 7
|
||||
#catch any errors
|
||||
@@ -0,0 +1,2 @@
|
||||
Test Data
|
||||
10/21/2022
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"quiz": {
|
||||
"sport": {
|
||||
"q1": {
|
||||
"question": "Which one is correct team name in NBA?",
|
||||
"options": [
|
||||
"New York Bulls",
|
||||
"Los Angeles Kings",
|
||||
"Golden State Warriros",
|
||||
"Huston Rocket"
|
||||
],
|
||||
"answer": "Huston Rocket"
|
||||
}
|
||||
},
|
||||
"maths": {
|
||||
"q1": {
|
||||
"question": "5 + 7 = ?",
|
||||
"options": [
|
||||
"10",
|
||||
"11",
|
||||
"12",
|
||||
"13"
|
||||
],
|
||||
"answer": "12"
|
||||
},
|
||||
"q2": {
|
||||
"question": "12 - 8 = ?",
|
||||
"options": [
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
"4"
|
||||
],
|
||||
"answer": "4"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,37 @@
|
||||
[
|
||||
{
|
||||
"Name": "Edward the Elder",
|
||||
"Country": "United Kingdom",
|
||||
"House": "House of Wessex",
|
||||
"Reign": "899-925",
|
||||
"ID": 1
|
||||
},
|
||||
{
|
||||
"Name": "Athelstan",
|
||||
"Country": "United Kingdom",
|
||||
"House": "House of Wessex",
|
||||
"Reign": "925-940",
|
||||
"ID": 2
|
||||
},
|
||||
{
|
||||
"Name": "Edmund",
|
||||
"Country": "United Kingdom",
|
||||
"House": "House of Wessex",
|
||||
"Reign": "940-946",
|
||||
"ID": 3
|
||||
},
|
||||
{
|
||||
"Name": "Edred",
|
||||
"Country": "United Kingdom",
|
||||
"House": "House of Wessex",
|
||||
"Reign": "946-955",
|
||||
"ID": 4
|
||||
},
|
||||
{
|
||||
"Name": "Edwy",
|
||||
"Country": "United Kingdom",
|
||||
"House": "House of Wessex",
|
||||
"Reign": "955-959",
|
||||
"ID": 5
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"fruit": "Apple",
|
||||
"size": "Large",
|
||||
"color": "Red"
|
||||
}
|
||||
Reference in new issue
Block a user