Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Feedback on my simple Python script?

I'm working on creating a basic Python app that helps you remember people's names. I'd love to get some feedback on the code.

  • Anything I could do more efficiently?
  • Am I not following any 'standard' coding conventions?
  • etc

Next, I'll be

  1. Hooking it up to a database to persistently store the data
  2. Using OOP
  3. Creating a web interface
  4. Creating an API so I can write an iOS app

Here's the code.

Love the project, I like the principle of a memory palace but always felt like I need a tool to aid me with it.

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher
  1. You should be using four spaces and not use two to comply with PEP 8. Also, holy line length, Batman! Try to stick to 79 characters per line. You can break up strings inside of parentheses like:
print("this is a really "
    "long string on"
    " multiple lines.")
  1. Good job using namedtuple. Convention is that the object created by namedtuple is CamelCased, though, so it should be Memory instead of memory.

  2. You're going to get some fun effects when you import this somewhere else. I'd suggest putting the whole thing into a function and then preventing import side effects by putting the following at the bottom:

if __name__ == '__main__':
    memory_palace()

This assumes the function name is memory_palace and it takes no arguments. This will make it so the function is only called automatically if the script is run directly (python memory_palace.py) and won't run when it's imported (which you'll be doing when you tie it into other scripts).

It's a great start (especially with just what you got from docs and the course)! I can't wait to see more.

Ricky Catron
Ricky Catron
13,023 Points

Additonally why should he use a named tuple instead of a dictionary?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Really it should probably be a class or information pulled out of a database, but to answer the question, any sort of tuple is going to be more memory efficient than a dictionary. Dictionaries are not cheap objects, even though they look and act like it. Named tuples are a pretty good in-between, though.

Ricky Catron
Ricky Catron
13,023 Points

Thanks! I will be sure to look into them! Plus a kind of related question. If he was going to use a database how would he go about protecting the database from a malicious user. The database password would be in his code and they could go in a mess with any users data? This is a problem I have run into in my own work.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Ricky Catron that's way more than we should probably get into in this thread. There are lots of ways to approach it, though. I generally prefer a twelve-factor app approach, though.

Ricky Catron
Ricky Catron
13,023 Points

Thanks for the link at least it looks great!

Thanks Kenneth, Ricky and Andrew. I've made some changes and pushed them up to GitHub! :)

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Ryan Carson only one problem now, your import should be at the top of the file.

Ricky Catron
Ricky Catron
13,023 Points

Seems good to me! The only code i didn't like was

for s in rooms:

s is not a very descriptive variable name. I know it doesn't really matter but personally I would change it. Plus you could turn it into an app for IOS, Android, Mac, Linux, or Windows using the Kivy library!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yes! It's pretty much always worth the extra keystrokes to use more descriptive names.

The only exception I can think of is holding onto throwaway index variables like when using enumerate.

Kenneth Love, when are you covering these things?

  • OOP
  • Django
  • DBs

Thanks!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

OOP is in the third course that's well underway.

Django and DBs will come after...2?...more Python courses.

Gracias sir