Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/dutree.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | fcfe324663a6 |
| children |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
43015
diff
changeset
|
1 #! /usr/bin/env python3 |
| 687 | 2 # Format du output in a tree shape |
| 3 | |
|
25219
8a7a2f1f88f0
Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents:
15932
diff
changeset
|
4 import os, sys, errno |
| 687 | 5 |
| 6 def main(): | |
|
25219
8a7a2f1f88f0
Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents:
15932
diff
changeset
|
7 p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
8 total, d = None, {} |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
9 for line in p.readlines(): |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
10 i = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
11 while line[i] in '0123456789': i = i+1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
12 size = eval(line[:i]) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
13 while line[i] in ' \t': i = i+1 |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
14 filename = line[i:-1] |
|
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
15 comps = filename.split('/') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
16 if comps[0] == '': comps[0] = '/' |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
17 if comps[len(comps)-1] == '': del comps[len(comps)-1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
18 total, d = store(size, comps, total, d) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
19 try: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
20 display(total, d) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
33074
diff
changeset
|
21 except IOError as e: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
22 if e.errno != errno.EPIPE: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
23 raise |
| 687 | 24 |
| 25 def store(size, comps, total, d): | |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
26 if comps == []: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
27 return size, d |
|
43015
0cd401bf2aa7
Make it work with Py3k.
Guido van Rossum <guido@python.org>
parents:
42427
diff
changeset
|
28 if comps[0] not in d: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
29 d[comps[0]] = None, {} |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
30 t1, d1 = d[comps[0]] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
31 d[comps[0]] = store(size, comps[1:], t1, d1) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
32 return total, d |
| 687 | 33 |
| 34 def display(total, d): | |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
35 show(total, d, '') |
| 687 | 36 |
| 37 def show(total, d, prefix): | |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
38 if not d: return |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
39 list = [] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
40 sum = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
41 for key in d.keys(): |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
42 tsub, dsub = d[key] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
43 list.append((tsub, key)) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
44 if tsub is not None: sum = sum + tsub |
|
15932
1881b968989b
Hand repair of cases where reindent changed lines of the form
Tim Peters <tim.peters@gmail.com>
parents:
15930
diff
changeset
|
45 ## if sum < total: |
|
1881b968989b
Hand repair of cases where reindent changed lines of the form
Tim Peters <tim.peters@gmail.com>
parents:
15930
diff
changeset
|
46 ## list.append((total - sum, os.curdir)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
47 list.sort() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
48 list.reverse() |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29107
diff
changeset
|
49 width = len(repr(list[0][0])) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
50 for tsub, key in list: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
51 if tsub is None: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
52 psub = prefix |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
53 else: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
54 print(prefix + repr(tsub).rjust(width) + ' ' + key) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
55 psub = prefix + ' '*(width-1) + '|' + ' '*(len(key)+1) |
|
43015
0cd401bf2aa7
Make it work with Py3k.
Guido van Rossum <guido@python.org>
parents:
42427
diff
changeset
|
56 if key in d: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
10503
diff
changeset
|
57 show(tsub, d[key][1], psub) |
| 687 | 58 |
|
33074
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
31515
diff
changeset
|
59 if __name__ == '__main__': |
|
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
31515
diff
changeset
|
60 main() |
