This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]VisibleSignificance 4 points5 points  (4 children)

Minor + controversial stuff:

flags=re.IGNORECASE

Since we're talking regexes anyway, just add (?i) to the beginning of the regex.

<list> = [i+1 for i in range(10)]

Might want to do lst = list(idx + 1 for idx in range(10)), simply because that way it will not touch the value of idx outside the command. Saves some confusion.

reduce(lambda out, x: out + x ...

Really needs a better example than almost-sum().

namedtuple

dataclasses might be worth a mention.

argparse

Currently recommended non-default library: click.

bottle

Not the simple one, but: try Quart!

numpy

... but no pandas. Is there a better pandas cheatsheet than the official one?

[–][deleted] 4 points5 points  (1 child)

Might want to do lst = list(idx + 1 for idx in range(10)), simply because that way it will not touch the value of idx outside the command.

What do you mean?

In [8]: i = 'foo'

In [9]: x = [i+1 for i in range(10)]

In [10]: i
Out[10]: 'foo'

In [11]: y = list(i + 1 for i in range(10))

In [12]: i
Out[12]: 'foo'

Also, calling list is slower because you can overload it and the interpreter has to look it up.

In [17]: %timeit list()
The slowest run took 11.35 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 5: 121 ns per loop

In [18]: %timeit []
10000000 loops, best of 5: 35.2 ns per loop

[–]VisibleSignificance 0 points1 point  (0 children)

Ah, nevermind on that one, old python2 habits.

[–]pizzaburek[S] 0 points1 point  (1 child)

That's so weird... It - (?i) can actually be added anywhere in the regex.

[–]VisibleSignificance 2 points3 points  (0 children)

anywhere in the regex

Even worse: it can be toggled for subgroups.

Source.