Jonnie Grieve Digital Media: Blog

Home
by on 26th July, 2021 - 3:35pm (0)

Blog: SQLAlchemy – Finishing Option 1: Adding items to a database (More Posts)

Recap:

This blog post continues on in my learning journal series about SQLAlchemy.  At this point, I have an application that takes in data from a CSV file, converts that data, and presents the user with a CLI that offers a number of options. The first of these options I will have implemented by the time I finish this post.

In this series of blogs, I will be attempting to create a CLI Application based on Megan Amendola’s excellent teaching on Treehouse but changed ever so slightly to expand the scope of the database to create records for all kinds of media, not just books. This is her wisdom I’m putting into text form to try and help me understand it better. There are one or 2 adaptions to the original material. I’m making a Media Library table of data as an example rather than just a books example and I have added one or 2 of my own columns to the schema.

I want to thank her in particular for helping me get to the bottom of a TypeError: str is not callable error I encounter in this blog. A Python Exception that is ery specific in nature.

Key takeaways

# add book/media
title=input('Media Title: ')
media_type=input('Media Type: ')
author=input('Media Author: ')
genre=input('Genre: ')

date_error = True
while date_error:
date = input('Published Date (Exmp): ')
date = clean_date(date)

if type(date) == datetime.date:
date_error = False

price_error = True
while price_error:
price = input('Price (Exmp): ')
price = clean_price(price)

if type(price) == int:
price_error = False

# add data to db
new_media_add = Media(
media_title=title,
media_type=media_type,
artist=author,
genre=genre,
published_date=date,
price=price
)

session.add(new_media_add)
session.commit()

After the user selects “option 1” from the main menu, the user is asked for various inputs as a series strings. They represent information about a single piece of media that is added 1 by one to the database. A call to the media class is then used to capture the input from the user where before committing that information via SQLAlcheny.

date_error = True
while date_error:
date = input('Published Date (Exmp): ')
date = clean_date(date)

if type(date) == datetime.date:
date_error = False

For the price and date columns, they are “cleaned” to ensure that the data is formatted according to the data type required by the schema. And the program will continue to add this information as long as there is an error in the returned data. That is to say that the data will only accept price and date information if it is provided in the proper format.

This is also where you have to be careful about using variable names reserved by another object. The biggest issue I had with this part of the app was a TypeError in which Python was seeing a string rather than a date object,

Traceback (most recent call last):
File "C:\\python\flask\sqlalchemy\cli_app\app.py", line 190, in
app()
File "C:\python\flask\sqlalchemy\cli_app\app.py", line 137, in app
if type(date) == datetime.date:
TypeError: 'str' object is not callable

These kinds of conflicts can occur when a variable name overrides another variable or a function which causes issues with variable type. The code was expecting a DateTime object and instead was finding a string (str)

This post has been assigned to the following categories

    Leave a Reply

    Your email address will not be published. Required fields are marked *