Here is silly tool I made. It lets you create people and then saves them to a json file.
The file had some mock people already when I made it, and then modify and update the file so I could in have some level of persistence between each time the program was run.
I know normally I would just use a Database.
My biggest takeaway from making it:
I started to get a better grasp of how files and directories work in Python.
Just because of where I was working on the script, I had to add extra info that I wouldn’t have otherwise done if this were a script I was planning on putting out in the wild.
EDIT: Also I didn’t put in any error handling, that’s something else I would want to do.
# person_maker.py
import json, os
data_path = os.path.abspath('.\\Python\\data.json')
data = open(data_path)
data_contents = data.read()
parsed_data = json.loads(data_contents)
def create_new_person():
print("What is the persons name?")
name = input('> ')
print("And how old are they?")
age = input('> ')
done = False
jobs = []
while not done:
print("What is their job(s)?")
job = input('> ')
jobs.append(job)
print("Do they have another job [y/n]?")
more = input('> ')
if more != 'y':
done = True
person = {
"name" : name,
"age" : int(age),
"jobs": jobs
}
return person
new_person = create_new_person()
parsed_data['people'].append(new_person)
new_data = open(".\\Python\\data.json", 'w')
new_data.write(json.dumps(parsed_data, indent=4, sort_keys=True))
data.close()
new_data.close()
Here is the output json file:
# data.json
{
"people": [
{
"age": 25,
"job": [
"Astronaut",
"Mathematician"
],
"name": "Doug"
},
{
"age": 18,
"job": [
"Student"
],
"name": "Ally"
},
{
"age": 32,
"job": [
"Chef",
"Accountant"
],
"name": "Luke"
},
{
"age": 32,
"job": [
"Chef",
"Accountant"
],
"name": "Luke"
},
{
"age": 29,
"jobs": [
"Programmer"
],
"name": "Zach"
},
{
"age": 87,
"jobs": [
"Grandma",
"Dress Maker",
"Vacuum Saleswoman"
],
"name": "Lindsey"
},
{
"age": 47,
"jobs": [
"Doomsday Preper",
"Salmon Descaler",
"Perpetual Virgin"
],
"name": "Lonnie"
}
]
}