raymondhによるPython tips

raymondhが時々ツイートするPython tipがためになるのでまとめてみました。
1
前へ 1 2 ・・ 5 次へ
Raymond Hettinger @raymondh

#python tip: sqlite3 is so widely supported, it's not just a database, it is a data interchange format.

2011-08-05 00:45:18
Raymond Hettinger @raymondh

#python tip: build a simple flattener with nested for-loops in a list comprehension: [char for string in strings for char in string]

2011-08-09 15:21:41
Raymond Hettinger @raymondh

#python tip: Periodically revisit the fundamentals of object oriented design: http://t.co/Zsp9RKL

2011-08-12 08:41:37
Raymond Hettinger @raymondh

#python tip: textwrap.dedent() is great for unindenting triple-quoted string literals found in indented code blocks.

2011-08-17 05:48:12
Raymond Hettinger @raymondh

#python tip: start using argparse and forget about optparse.

2011-08-18 05:23:25
Raymond Hettinger @raymondh

#python tip: if a function has multiple decorators, the decorators closest to the function definition get applied first.

2011-08-27 03:36:14
Raymond Hettinger @raymondh

#python tip: spend ten minutes experimenting with xmlrpclib -- it's worth it :-)

2011-08-28 18:15:59
Raymond Hettinger @raymondh

#python tip: conditional expressions can be chained: 'A' if s>=90 else 'B' if s>=80 else 'C' if s>=70 else 'F'

2011-08-29 02:41:42
Raymond Hettinger @raymondh

#python tip: Chained comparisons are linked with a short-circuiting "and". Subexpressions are evaluated no more than once: a < b+c < d

2011-09-01 06:57:32
Raymond Hettinger @raymondh

#python tip: logging.exception(msg) automatically captures, formats, and records the current exception being handled. Nice!

2011-09-02 04:23:51
Raymond Hettinger @raymondh

#python tip: contextlib.contextmanager() nicely simplifies some of the arcane details needed to write a context manager.

2011-09-03 03:08:49
Raymond Hettinger @raymondh

#python tip: Some use the shelve module would be better-off with a simple persistent dictionary: http://t.co/CYTDrJt

2011-09-04 07:15:50
Raymond Hettinger @raymondh

#python tip: bisect.insort(seq, value) is an O(n) operation. Sure, the search is O(log n) but the insertion step is slow.

2011-09-04 11:42:44
tomviner @tomviner

@raymondh would be cool to do a context manager of that DBDict.

2011-09-04 18:04:05
Raymond Hettinger @raymondh

@tomviner Until then, use "with contextlib.closing(dbopen('mydict.pkl', 'c', 'pickle')) as d: ..."

2011-09-05 03:36:27
Raymond Hettinger @raymondh

#python tip: For complex regexes, the re.VERBOSE flag is your friend. It is possible to make readable, maintainable, commented regexes.

2011-09-05 06:26:39
Raymond Hettinger @raymondh

#python regexes don't allow two groups with the same name. This makes sense but keeps you from easily combining multiple regexes into one.

2011-09-06 05:24:07
Daniel Neuhäuser @DasIch

@raymondh imho you might want to take a step back and consider what you're doing if you hit that problem you're probably doing it wrong

2011-09-06 05:29:03
Raymond Hettinger @raymondh

@DasIch The problem is combining itty's regex dispatchers into a single, fast regex. How would *you* do it? http://t.co/fNCf3Aq

2011-09-06 06:20:38
Raymond Hettinger @raymondh

@NorthernBoy71 list.append() with an over-allocation strategy is faster than a list.append() implementation that always calls realloc().

2011-09-07 04:58:06
Raymond Hettinger @raymondh

#cpython note: list.append() is fast because lists over-allocate space. The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...

2011-09-07 04:22:02
Raymond Hettinger @raymondh

Unlike #python lists which grow using realloc(), deques make links to new memory blocks. Once added, deque data never moves.

2011-09-08 02:30:58
Raymond Hettinger @raymondh

#python tip: None is a singleton so you can test it with: "if x is None: ..."

2011-09-13 00:11:08
Raymond Hettinger @raymondh

#python tip: There are many ways to put a default value in a dictionary, but only dict.setdefault() makes an atomic compare and update.

2011-09-16 01:54:50
Raymond Hettinger @raymondh

@joncle With dict.setdefault() the default value is created before the call. With defaultdict() the function factory is called as needed.

2011-09-16 02:31:44
前へ 1 2 ・・ 5 次へ