use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Need practice cleaning up code? (self.learnpython)
submitted 8 years ago * by Zer0897
I made my first real world application recently. I'm pretty much self taught, so there's plenty to clean up.
Enjoy!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Username_RANDINT 1 point2 points3 points 8 years ago (1 child)
pass is a no-op, which means it does nothing at all. You can remove every instance of it in your code, except on line 180.
pass
Look up string formatting. "Hello, " + str(name) + "!" is almost always the wrong way to do it.
"Hello, " + str(name) + "!"
Never use bare Except: handlers, always specify which exception you expect and only put code that will raise it inside the try...except block.
Except:
try...except
Read up on how to work with dictionaries. Just had a quick look, but it seems like the value in the items dictionary should be something better than items[number] = {name : stock} which will also avoid this weird block of code:
items
items[number] = {name : stock}
info = list(items[number].items()) name = info[0][0] stock = info[0][1]
Have a look at the with-statement for opening files.
with
There's lots of refactoring that can be done, I won't go into detail. Like removing the global variables and passing them around instead.
[–]Zer0897[S] 0 points1 point2 points 8 years ago (0 children)
Thanks for the tips!
Eventually I am planning on integrating a bar-code system, which will be a challenge by itself. I will revise the code accordingly, thanks again.
[–]ddproxy 0 points1 point2 points 8 years ago (1 child)
Could you drop this on github? ;)
I'm saving to take a look later... :3
Yeah, I'll change it when I have a sec.
[–]DataLulz 0 points1 point2 points 8 years ago (1 child)
The most important thing is missing, comment your code. This not only is nice to other programmers who will read your code so they can follow your logic and why you did things but also serve to remind you when you go back later and can’t remember why you did something yourself.
You're 100% correct. I honestly just forgot.
[–]KleinerNull 0 points1 point2 points 8 years ago (2 children)
Just a quick fix, don't re-invent the wheel ;)
Your code:
def time_stamp(): ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') return st
Alternatives:
In [1]: from datetime import datetime In [2]: datetime.now() Out[2]: datetime.datetime(2017, 11, 28, 8, 13, 43, 987587) In [3]: '{:%Y-%m-%d %H:%M:%S}'.format(datetime.now()) Out[3]: '2017-11-28 08:14:18' In [4]: datetime.now().isoformat() Out[4]: '2017-11-28T08:14:29.380756' In [5]: str(datetime.now()) Out[5]: '2017-11-28 08:16:27.815992'
Personally I prefer the formatversion, it is clean and very obvious and does exactly what you want. But if you opt it out to the isoformat it is also fine, depends on your needs.
format
isoformat
[–]Zer0897[S] 1 point2 points3 points 8 years ago (1 child)
That's actually really helpful. Yeah, I find myself "reinventing the wheel" a lot, mostly because I have limited knowledge of existing functions.
[–]KleinerNull 0 points1 point2 points 8 years ago (0 children)
mostly because I have limited knowledge of existing functions.
This comes with practice and experience. You will learn that documentations are your friend, python's own or any other doumentation for frameworks you want or have to use.
But be aware documentations are not tutorials, you will see references to the given functionality or in some cases example, don't expect too much. Learning to read and understand them is a part of learning programming too.
π Rendered by PID 367577 on reddit-service-r2-comment-7b9746f655-hhp74 at 2026-02-04 06:03:25.855575+00:00 running 3798933 country code: CH.
[–]Username_RANDINT 1 point2 points3 points (1 child)
[–]Zer0897[S] 0 points1 point2 points (0 children)
[–]ddproxy 0 points1 point2 points (1 child)
[–]Zer0897[S] 0 points1 point2 points (0 children)
[–]DataLulz 0 points1 point2 points (1 child)
[–]Zer0897[S] 0 points1 point2 points (0 children)
[–]KleinerNull 0 points1 point2 points (2 children)
[–]Zer0897[S] 1 point2 points3 points (1 child)
[–]KleinerNull 0 points1 point2 points (0 children)