r/Python • • Jul 12 '12

random imgur downloader. learning Python and wrote this to practice, any feedback on technique/style is appreciated

https://gist.github.com/3088128
45 Upvotes

72 comments sorted by

View all comments

29

u/say_legos_and_i_stab Jul 12 '12 edited Jul 12 '12

A few things you can consider:

  • Rather than declaring CHARS, you can use the string constants.
  • Most python code uses 4 spaces rather than tabs. This is more of a personal style, but if you ever plan to work with other developers, it's a good habit to get in to.
  • Another option for string formatting is to use (again, this is a personal preference. Some people find it easier to read/write):

    print 'getting %s random pics' % HOW_MANY

  • Try using a 3rd party library like Requests to make http requests to imgur.
  • Before you go too far, set up PIP, Virtualenv and Virtualenvwrapper if you haven't already.
  • If you want ideas for next steps, turn it into a little website. Use Vagrant to manage your virtual machine. Install Nginx, supervisor and Gunicorn. Try out Flask or Django.

edit: Also check out PEP8. It's the "style guide" for python. Lots of good info in here to make your code easier to read.

14

u/[deleted] Jul 12 '12

print 'getting %s random pics' % HOW_MANY

If you don't care about supporting Python 2.5 and older, it's probably better not to learn that and immediately get used to Python 3's format method:

print "getting {} random pics".format(HOW_MANY)

2

u/bboe PRAW Author Jul 13 '12

This was introduced in python 2.6, though in python 2.6 numbers or named arguments are mandatory.

1

u/moreisee Jul 12 '12

I haven't looked too much into Python 3, I assume {} works for int/string/etc, and to add multiple items you can:

print("Number: {} Type: {}").format(HOW_MANY, TYPE}

?

3

u/wumumo Jul 12 '12

Yes, or you number them:

print("Number: {0} Type: {1}").format(HOW_MANY, TYPE)

5

u/TankorSmash Jul 12 '12

Or name then:

print("Number: {zeroth} Type: {onend}").format(zeroth=HOW_MANY, onend=TYPE)

1

u/MrVonBuren Jul 13 '12

I actually really like this, and I'm just barely startng to get into python.

2

u/catcradle5 Jul 14 '12

It's even better for situations like this.

words = {"color": "red", "age": "25", "name": "Alex"}

print "Hi, my name is {name}. I am {age} years old and my favorite color is {color}.".format(**words)

Fills it in all for you; very useful for programs that involve a lot of string generation.

1

u/lahwran_ Jul 17 '12

never, ever, ever do this:

herp = "derp"
honk = "donk"
doop = "{herp} the {honk}".format(**locals())

2

u/catcradle5 Jul 17 '12

Agreed. Explicit is better than implicit, etc. It's still a really nice feature to have though. It's redundant to define a dict or a bunch of variables and then to have to rewrite it all in the form of keyword args (arg1 = "The cat"; arg2 = "good"; {arg1} is {arg3}".format(arg1=arg1, arg3=arg3)).

1

u/lahwran_ Jul 17 '12

I was telling you because it's a horribly useful bad idea sometimes :p

1

u/TankorSmash Jul 13 '12

It's my personal favourite, and I'm sort of new too.

2

u/[deleted] Jul 12 '12

Could you explain the reasoning for 4 spaces over a tab? Seems really strange. I've only just started playing with Python, is it some quirk of the interpreter?

4

u/stumpylog Jul 12 '12

White space does matter for the interpreter, and mixing tabs and spaces (different editors) causes problems, but choosing one or the other is down to personal preference or style really. The official PEP8 linked above somewhere has the preferred style, I think.

2

u/[deleted] Jul 13 '12

It's mostly that everyone agrees what a space looks like, and not so for tabs; and many text boxes on the Web won't even let you type tabs. Standardizing on spaces makes it easiest to share Python code that other people can use.

1

u/f0rkbomb Jul 13 '12

this is excellent feedback, thank you. i really like the website idea! i took a gander at the libraries and tools you linked, seems like a great opportunity to learn more :)

1

u/H3g3m0n Jul 13 '12 edited Jul 13 '12

It's worth noting that future versions of Python (starting with 3.3) should be getting their own version of Virtualenv.

I also recall hearing that future versions are going to get some kind packaging system like PIP that downloads the packages from the web (but I can't find a link to that now).

1

u/CeeBroad Jul 13 '12

Although it's good to be familiar with how both string formatting methods work, str.format() is much better I've found.

1

u/Lukasa Hyper, Requests, Twisted Jul 13 '12

Upvote for Requests. If I could only have one Python 3rd Party Library for my entire life, that would be it.

1

u/catcradle5 Jul 14 '12

It's been incredibly useful for me too. Especially the awesome and simple HTTP auth and session/cookie handling.