★ wanayoo — archive 1999 https://github.com/realpython/materials/pull/99Nouvelle recherche | Portail wanayoo
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python sqlite sqlalchemy #99

Open
wants to merge 60 commits into
base: master
from
Open

Python sqlite sqlalchemy #99

wants to merge 60 commits into from

Conversation

@writeson
Copy link
Contributor

writeson commented Jan 20, 2020

Where to put new files:

  • New files should go into a top-level subfolder, named after the article slug. For example: my-awesome-article

How to merge your changes:

  1. Make sure the CI code style tests all pass (+ run the automatic code formatter if necessary).
  2. Find an RP Team member on Slack and ask them to review & approve your PR.
  3. Once the PR has one positive ("approved") review, GitHub lets you merge the PR.
  4. 🎉
@writeson writeson requested a review from dbader Jan 20, 2020
@writeson writeson self-assigned this Jan 20, 2020
writeson added 13 commits Jan 20, 2020
…ith an uppercased word
@writeson writeson closed this Apr 6, 2020
@writeson writeson reopened this Apr 6, 2020
@writeson writeson requested review from gahjelle and dbader and removed request for dbader Apr 6, 2020
writeson added 7 commits Apr 6, 2020
writeson added 4 commits Jun 29, 2020
…e some code formatting consistent
Copy link
Contributor

gahjelle left a comment

@writeson This looks quite solid!

I have a few comments and suggestions, mainly to make the code more consistent between example 1 and example 2.

I'm currently working through example 3, and will send that over in a separate review.

python-sqlite-sqlalchemy/README.md Outdated Show resolved Hide resolved
python-sqlite-sqlalchemy/README.md Outdated Show resolved Hide resolved
python-sqlite-sqlalchemy/README.md Outdated Show resolved Hide resolved
Copy link
Contributor

gahjelle left a comment

@writeson

I think the Chinook-app looks quite nice. The only big issue I found was a small logic error in detecting duplicates when adding new albums or tracks.

I'm also wondering if there is a way to remind the readers that they need to add in the .env file from the article. Otherwise, I'm worried we'll have many confused users.

One way to do it could be to add a README file for that particular example, and use a try/except to catch the error raised by config.py if the file is not there, and rewrite the message to tell the user to add the .env file, instead of the cryptic "NoneType object has no attribute lower" that is currently being raised if .env is not there.

Take care!

Copy link
Contributor

gahjelle left a comment

@writeson Sorry for the delay. I think this is a great update. Unfortunately, I found a few other bugs we need to straighten out.

Also, I'll repeat the request to have something that reminds users to create a .env file. For example, by using try/except to rewrite the cryptic 'NoneType' object has no attribute 'lower' error that is currently raised if .env is missing.

Take care!

return data.groupby("publisher")
.size()
.sort_values(ascending=ascending)
Comment on lines 33 to 35

This comment has been minimized.

@gahjelle

gahjelle Aug 13, 2020

Contributor

This is a syntax error. In the article, we use parentheses to allow for linebreaks.

However, Black will format this on one line since that line is still short enough to fit. I think it's okay to leave it on one line here in the repo.

Spending multiple lines in the article is probably good for readability though. I think it's also okay to keep that small difference as long as we feel it improves the readability of the code in the article.

Suggested change
return data.groupby("publisher")
.size()
.sort_values(ascending=ascending)
return data.groupby("publisher").size().sort_values(ascending=ascending)
data = get_author_book_publisher_data(csv_filepath)
author_book_publisher_data = data
Comment on lines +77 to +78

This comment has been minimized.

@gahjelle

gahjelle Aug 13, 2020

Contributor

I think this looks very smelly. I guess the reason you read into a temporary data variable and rename it later is to avoid Black adding a linebreak? I'd argue that having the linebreak is better - although I agree it's less than ideal - because then the code is straight-forward, it's just the formatting that's a bit off.

Seeing the current code, I stopped and started looking for how data would be used and trying to figure out why we need two names pointing to the same object.

In the examples, we have shortened these names, which could be done also here.

db_path = base_path / "data" / "chinook.db"

SECRET_KEY = os.getenv("SECRET_KEY")
SQLALCHEMY_DATABASE_URI = f"sqlite:///{str(db_path)}"

This comment has been minimized.

@gahjelle

gahjelle Aug 13, 2020

Contributor

str() is unnecessary here, the formatting converts the path to a string.

Suggested change
SQLALCHEMY_DATABASE_URI = f"sqlite:///{str(db_path)}"
SQLALCHEMY_DATABASE_URI = f"sqlite:///{db_path}"
[metadata]
name = local_project
version = 0.1.0

[options]
packages = structure
Comment on lines +1 to +6

This comment has been minimized.

@gahjelle

gahjelle Aug 13, 2020

Contributor

I think I recognize this 😄

However, we should either use setup.cfg and only a "shim" setup.py that calls setup() without any options, or only use a setup.py specifying all the options. The latter is probably the easiest. However, if you want to go with setup.cfg (which is somewhat recommended: https://snarky.ca/what-the-heck-is-pyproject-toml/) there are a few changes we should make:

  • packages should point to the directory of the code, so it would be project and possibly some of the subdirectories
  • You should also add a install_requires key in the [options] section listing the dependencies.
book = session.query(Book).filter(Book.title == book_title).one_or_none()

# Get the publisher if exists
publisher = (
session.query(Publisher)
.filter(Publisher.name == publisher_name)
.one_or_none()
)
# Does book, author and publisher already exist?
if book is not None and author is not None and publisher is not None:
raise ValueError(
"New item exists", author_name, book_title, publisher_name
)
Comment on lines 87 to 99

This comment has been minimized.

@gahjelle

gahjelle Aug 13, 2020

Contributor

Unfortunately, this seems to have the same duplication issue we found in the Chinook example. Different authors are not allowed to write books with the same name, or different publishers can't publish the same book.

>>> add_new_book(session, author_name="Tom Clancy", book_title="The Good Earth", publisher_name="Berkley")
ValueError: ('New item exists', 'Tom Clancy', 'The Good Earth', 'Berkley')
# Get the artist
artist = (
db.session.query(Artist)
.filter(Artist.artist_id == artist_id)
.one_or_none()
)

form.artist.data = artist.name
Comment on lines 41 to 48

This comment has been minimized.

@gahjelle

gahjelle Aug 13, 2020

Contributor

If you click on Albums in the top menu, no particular artist is chosen, so artist will be None after the query and artist.name will raise an AttributeError: 'NoneType' object has no attribute 'name'.

I would probably have expected clicking on Albums to show me a list of all albums in the database. If that is not trivial to fix/implement, one option could be to simply remove Albums from the top menu. Accessing albums through the artist pages seem to work fine.

@jablonskidev
Copy link
Contributor

jablonskidev commented Sep 1, 2020

@writeson I made you a Didactic Review video. Please also check out this course.
I've updated the due date on Trello. Please let me know when you're done making changes, and then we'll need to move the article to the next stage in the publishing process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.