Person_maker.py


#1

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"
        }
    ]
}

#2

Nice, good quick little hack to get something done.


#3

ok, Lonnie is my favorite.
json looks like SQL, are all databases set up this way?
Curious if there’d be an exercise in the LMPTHW to set up our own database, outside of the linked lists, and trees.


#4

@nellietobey I personally feel JSON is closer to a NoSQL database, and SQL is more like an Excel spreadsheet. But that is probably because I learned mongoDB before I learned SQL, so take what I say with a grain of salt.

there is a section dedicated to getting started with SQL in LMPTHW. It’s pretty great.

Lonnie is my favorite, too.


#5

@nellietobey SQL is all about tables (columns, rows) and is structured. JSON deals with unstructured data in a list/dict approach, which is particularly useful for dealing with things like nesting, etc.

Check out LSQLTHW.


#6

I always thought JSON is a replacement of XML but better readable for humans. But I could be wrong. It has a lot of similarities to XML in the way you use it. It’s a way to structure your data in a textfile.



#7

I recall someone saying that json was assisting in Big Data as it’s language independent and as you say, very readable. If it became standard it would be easy for all kinds of data systems to communicate with a simple api’s.


#8

That makes sense. I think it is already something of a quasi standard, because in new projects it’s often choosen instead of xml in some areas. But I think it would be hard to draft a document (like an open office writer document) in json. So I think XML will remain in some areas and diminish in some others (like Big Data).