I like the basic script template that the author provides.
However, when I start development of new code I always use a script that starts off with traceback and pdb, something like this:
#!/usr/bin/env python
import traceback
import pdb
import sys
def main():
# some WIP code that maybe raises an exception
raise BaseException("oh no, exception!")
return 0
if __name__ == "__main__":
try:
ret = main()
except:
traceback.print_exc()
pdb.post_mortem()
sys.exit(ret)
This means that whenever an uncaught exception gets raised, I immediately get told what happened (full backtrace) plus I get dumped into the debugger, from where I can inspect why this happened. I liberally sprinkle assert()s through my code, so this gives me a good edit-run-debug cycle to work with.
This is pretty smart. When I'm making one-off scripts I use the interactive flag
python -i script.py
I've programmed Python for years and never knew this. I'm not sure how well this handles errors. Tab complete works, only thing that is missing is needing to wrap help(<func>) to see signatures. I'm enjoying the standard lib interpreter.
Is it just me or is dict#setdefualt a terribly named function? I just learned of it here, and I was excited that it would set the default value for the dictionary, when instead it performs something like:
Agreed, although I'll note here that this was posted to /r/python under the clickbait version of the title, so the HN submitter was likely just passing it on from there.
Yes! And implementing this help text for your library is free. It just assembles the docstrings found in your code. And then your favourite text editor can use this for autocomplete and such as well.
Treating docstrings as an actual reflectable part of a class/function and not just "comments to be ignored by a parser" is brilliant.
I could not help but comment to add Elixir to the list of languages with amazing discoverability. For things in the standard library, you essentially get a condensed version of Python web documentation, with examples!
Seems to me that perhaps this should be a Show HN since it's submitted by the creator.
Also, for me the main link won't actually display and I can't be bothered to track down why, but the Github source (https://github.com/gto76/python-cheatsheet) displays just fine.
Because it's rendered directly from README.md.
That way it's easier for me, because I don't have to render it every time I make a change (I make a lot of little edits all the time), and project's Github page (https://github.com/gto76/python-cheatsheet) always has the same content as webpage.
I like the clean look and the overall way it is presented. The examples using angle brackets (eg.: <list>.append(<el>) ) is highly legible and the one column format is faster to scan.
I came here to say exactly the opposite. The angle brackets mean that every single example is a syntax error.
If you are going to write a cheatsheet with samples of Python code, why not write it in valid Python?
This is clearly aimed at beginners as the samples are or very basic things, yet this is going to be confusing to them as they will think that `<list>` is syntax.
Have a look at the Python docs for much better ways of doing this
So to get a list of unique values you suggest I convert to a dictionary and then convert back to a list? Is this the Python way? I dunno it seems like acrobatics
Yes, although there is a saying that "every generator is an iterator, but not every iterator is a generator". But then again generator has a bunch of methods (close, gi_frame, gi_yieldfrom, throw, gi_code, gi_running, send) that iterators don't have... I really don't know if <iter> is correct enough here, or should I use <genr> (that I don't use anywhere else and could be confusing).
[+] [-] gorb314|7 years ago|reply
However, when I start development of new code I always use a script that starts off with traceback and pdb, something like this:
This means that whenever an uncaught exception gets raised, I immediately get told what happened (full backtrace) plus I get dumped into the debugger, from where I can inspect why this happened. I liberally sprinkle assert()s through my code, so this gives me a good edit-run-debug cycle to work with.[+] [-] smittywerben|7 years ago|reply
python -i script.py
I've programmed Python for years and never knew this. I'm not sure how well this handles errors. Tab complete works, only thing that is missing is needing to wrap help(<func>) to see signatures. I'm enjoying the standard lib interpreter.
[+] [-] basil-rash|7 years ago|reply
[+] [-] ac29|7 years ago|reply
[+] [-] BerislavLopac|7 years ago|reply
[+] [-] unknown|7 years ago|reply
[deleted]
[+] [-] omegote|7 years ago|reply
[+] [-] pizzaburek|7 years ago|reply
[+] [-] tyingq|7 years ago|reply
[+] [-] woadwarrior01|7 years ago|reply
[+] [-] dvfjsdhgfv|7 years ago|reply
[+] [-] timdellinger|7 years ago|reply
[+] [-] gjvc|7 years ago|reply
[+] [-] Nanocurrency|7 years ago|reply
[+] [-] avip|7 years ago|reply
[+] [-] legends2k|7 years ago|reply
With Python, many times I vaguely know something but dunno what it is exactly. This cheatsheet solves that!
[+] [-] Waterluvian|7 years ago|reply
Treating docstrings as an actual reflectable part of a class/function and not just "comments to be ignored by a parser" is brilliant.
[+] [-] nestorD|7 years ago|reply
[+] [-] vageli|7 years ago|reply
[+] [-] legends2k|7 years ago|reply
With Python, many times I vaguely know something but dunno what it is exactly. This cheatsheet solves that!
[+] [-] fencepost|7 years ago|reply
Also, for me the main link won't actually display and I can't be bothered to track down why, but the Github source (https://github.com/gto76/python-cheatsheet) displays just fine.
[+] [-] sametmax|7 years ago|reply
[+] [-] pizzaburek|7 years ago|reply
[+] [-] TBurette|7 years ago|reply
[+] [-] Singletoned|7 years ago|reply
If you are going to write a cheatsheet with samples of Python code, why not write it in valid Python?
This is clearly aimed at beginners as the samples are or very basic things, yet this is going to be confusing to them as they will think that `<list>` is syntax.
Have a look at the Python docs for much better ways of doing this
[+] [-] reacharavindh|7 years ago|reply
[+] [-] udev|7 years ago|reply
For me this is the best quick reference for Python (2.7 unfortunately): http://rgruet.free.fr/PQR27/PQR2.7.html
[+] [-] plg|7 years ago|reply
[+] [-] occams_chainsaw|7 years ago|reply
[+] [-] v4ult|7 years ago|reply
Are there any available for Ruby or Linux?
[+] [-] elmo2you|7 years ago|reply
[+] [-] nszceta|7 years ago|reply
[+] [-] BerislavLopac|7 years ago|reply
[+] [-] pizzaburek|7 years ago|reply
[+] [-] alexanderdmitri|7 years ago|reply
[+] [-] ngcc_hk|7 years ago|reply
[+] [-] vram22|7 years ago|reply
[+] [-] pizzaburek|7 years ago|reply