Word to the wise... as of Python 3.7, the regular dictionary data structure guarantees order. Declaring an OrderedDict can still be worthwhile for readability (to let code reviewers/maintainers know that order is important) but I don't know of any other reason to use it anymore.
fractions.Fraction(numerator=1, denominator=3)
fractions.Fraction(1) / 3
ChainMap is maybe better described/used as inheritance for dicts, where something like settings = ChainMap(instance_settings, region_settings, global_settings)
would give you one object to look in.
The stdlib is full of goodies.
Now I always appreciated the battery included logic in python. But I noticed this week that LLM diminish that need. It's so easy to prompt for small utilities and saves you from using entire libraries for a few tools.
And the AI can create doc and tests for them as quickly.
So while I was really enthusiastic things like pairwise() were added to itertools, it's not as revolutionary as before.
https://docs.python.org/3/library/types.html#types.MappingPr...
python -m http.server
Pass -h for more options.
Good example of the latter use case is the statistics module.
There is a price to pay though: its performance is 10x slower than numpy. So its mostly useful when the required calculation is not a bottleneck.
The benefit is you are good to go (batteries included) without any virtual environmemts, pip's etc.
Nice! When you need it, you need it. It's nice not to have to implement it oneself.
You have to encode the file name!
file_url = 'file://' + urllib.parse.quote(os.path.realpath('test.html'))
That said, Go has those things so it's crept in a little bit into my quick programming, but I'll always love python.
I don't think this is true. It allows you to specify and calculate parameters for normal distributions, what allows you to jury rig a naive bayes classifier, what is shown as a doc example. This is not the same as providing a built in classifier.
export PYTHONPATH="package.zip"
python3 -m packagename
will work just fine.(PS. I document this technique in one of my python-template projects: https://git.sr.ht/~tpapastylianou/python-self-contained-runn...)
I suppose, if the intent is to package something in a manner that attempts to make it newbie-proof, then requiring a PYTHONPATH incantation before the python part might be one step too far ... but then again, one could argue the same about people not quite knowing what to do with a .pyz file and getting stuck.
I assume the `namedtuple` syntax is more pleasing for Functional favorable programmers, but this makes me wonder if the stdlib should choose one of them?
header = Header._make(unpack(f.read(64), header_format))
print(header)
My other favourite parts of the stdlib are functools and itertools. They are both full of stuff that gives you superpowers. I always find it a shame when I see developers do an ad hoc reimplantation of something in functools/itertools.
Everyone these days is using ast [1] but the might be room for dis instead in some cases.
a['foo'] = 20
a['bar'] = 9
where you want to be able to do: a.foo = 20
a.bar = 9
Another module that's packaged with the stdlib that's immensely useful is itertools. I especially find takewhile, cycle, and chain to be incredibly useful building blocks for list-related functions. I highly recommend a quick read.
EDIT: functools is also great! Fantastic module for higher-order functions on callable objects.
https://docs.python.org/3/library/itertools.html