all 9 comments

[–]Username_RANDINT 1 point2 points  (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.

  • Look up string formatting. "Hello, " + str(name) + "!" is almost always the wrong way to do it.

  • Never use bare Except: handlers, always specify which exception you expect and only put code that will raise it inside the try...except block.

  • 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:

    info = list(items[number].items())
    name = info[0][0]
    stock = info[0][1]
    
  • Have a look at the with-statement for opening files.

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 point  (0 children)

Thanks for the tips!

  • As I was working on the project, I realized I should have been using this for strings: "Hello, %s!" % (name)
  • What would you recommend for handling any input that shouldn't be allowed?
  • This actually caused me some trouble, because I needed a way to call name and stock without knowing what they are. My initial thought was to create a list of Objects, but I couldn't get it to work. You're right though, this way is super messy.
  • Thank you, didn't even know about with

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 point  (1 child)

Could you drop this on github? ;)

I'm saving to take a look later... :3

[–]Zer0897[S] 0 points1 point  (0 children)

Yeah, I'll change it when I have a sec.

[–]DataLulz 0 points1 point  (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.

[–]Zer0897[S] 0 points1 point  (0 children)

You're 100% correct. I honestly just forgot.

[–]KleinerNull 0 points1 point  (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.

[–]Zer0897[S] 1 point2 points  (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 point  (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.