~pyspeed-dev/pyspeed/trunk

« back to all changes in this revision

Viewing changes to __init__.py

  • Committer: Amit Aronovitch
  • Date: 2010-08-17 18:37:21 UTC
  • Revision ID: amit@porpoise.localdomain-20100817183721-drczmhv9mlexgrg1
Added __init__.py from Amit M.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
The speed package: wraps iterators for proeress tracking.
 
3
Also wraps built-in xrange().
 
4
 
 
5
Written by Rani Hod, August 2005
 
6
"""
 
7
 
 
8
import sys, os, __builtin__, __main__
 
9
 
 
10
from base import *
 
11
from context import TextualIterationContext
 
12
 
 
13
__all__ = ['speedConfig', 'speed', 'xrange', 'tqdm', 'trange', 'gtqdm', 'grange', 'btqdm']
 
14
 
 
15
theManager = IterationManager(TextualIterationContext)
 
16
 
 
17
def speedConfig(mode=None, vis=None, delay=None, refresh=None):
 
18
    """
 
19
    Configure the speed package.
 
20
 
 
21
    mode can be one of the following:
 
22
    'auto' - replace range() and xrange() [default]
 
23
    'manual' - use speed() explicitly to wrap iterators.
 
24
 
 
25
    vis can be one of the following:
 
26
    'none' - disable the progress indicator
 
27
    'text' - a single-line textual progress indicator
 
28
         (previously known as gtqdm)
 
29
 
 
30
    delay is the minimum time (in seconds) after which the progress
 
31
    indicator is shown [default = 3 seconds]
 
32
 
 
33
    refresh is the minimum elapsed time (in seconds) between progress
 
34
    indicator visual updates [default = 0.5 seconds].
 
35
    """
 
36
    anything_changed = False
 
37
 
 
38
    if mode is None:
 
39
        pass
 
40
    elif mode == 'auto':
 
41
        anything_changed = True
 
42
        __main__.xrange = sys.modules['speed'].xrange
 
43
    elif mode == 'manual':
 
44
        anything_changed = True
 
45
        try:
 
46
            del __main__.xrange
 
47
        except AttributeError:
 
48
            # Mode is already manual
 
49
            pass
 
50
    else:
 
51
        raise ValueError, "mode must be 'auto' or 'manual'"
 
52
 
 
53
    if vis is None:
 
54
        pass
 
55
    elif vis == 'none':
 
56
        anything_changed = True
 
57
        theManager.set_context(IterationContextBase)
 
58
    elif vis == 'text':
 
59
        anything_changed = True
 
60
        theMAnager.set_context(TextualIterationContext)
 
61
    elif vis == 'graph':
 
62
        anything_changed = True
 
63
        from gtk_widget import GTKIterationContext
 
64
        theManager.set_context(GTKIterationContext)
 
65
    else:
 
66
        raise ValueError, "vis must be 'none', 'text' or 'graph'"
 
67
 
 
68
    if delay is None:
 
69
        pass
 
70
    elif not isinstance(delay, (int, float)):
 
71
        raise TypeError, "delay must be non-negative"
 
72
    else:
 
73
        anything_changed = True
 
74
        IteratorWrapper.BEGIN_AFTER = delay
 
75
 
 
76
    if refresh is None:
 
77
        pass
 
78
    elif not isinstance(refresh, (int, float)):
 
79
        raise TypeError, "refresh must be an integer or a floating-point number"
 
80
    elif refresh < 0.05:
 
81
        raise ValueError, "refresh must be at least 0.05"
 
82
    else:
 
83
        anything_changed = True
 
84
        IteratorWrapper.UPDATE_EVERY = refresh
 
85
 
 
86
    if not anything_changed:
 
87
        mode_is_auto = getattr(__main__, 'xrange', None) is sys.modules['speed'].xrange
 
88
        print 'mode = %s' % ('auto' if mode_is_auto else 'manual')
 
89
        print 'vis = %s' % theManager._iter_context_type.NAME
 
90
        print 'delay = %.1f seconds' % IteratorWrapper.BEGIN_AFTER
 
91
        print 'refresh = %.1f seconds' % IteratorWrapper.UPDATE_EVERY
 
92
 
 
93
def speed(iterable, size=None):
 
94
    """
 
95
    Speed wrapper for a generic iterable.
 
96
    If a size is not given it will be taken from the iterable.
 
97
    """
 
98
    if not isinstance(iterable, IteratorWrapper):
 
99
        iterable = IteratorWrapper(iterable, theManager, size)
 
100
    return iterable
 
101
 
 
102
def xrange(*args):
 
103
    """
 
104
    speed wrapper for xrange.
 
105
 
 
106
    xrange([start,] stop[, step]) -> xrange object
 
107
 
 
108
    Like range(), but instead of returning a list, returns an object that
 
109
    generates the numbers in the range on demand. This is sligtly slower
 
110
    than range() bu more memory efficient.
 
111
    """
 
112
 
 
113
    return speed(__builtin__.xrange(*args))
 
114
 
 
115
 
 
116
tqdmManager = IterationManager(TextualIterationContext)
 
117
def tqdm(iterable, delay=0, refresh=None, size=None):
 
118
    """
 
119
    Speed textual wrapper for a generic iterable.
 
120
    Ignores the settings set by speedConfig ans is always textual.
 
121
    """
 
122
    if not isinstance(iterable, IteratorWrapper):
 
123
        iterable = IteratorWrapper(iterable, tqdmManager, size)
 
124
 
 
125
    if delay is not None:
 
126
        iterable.BEGIN_AFTER = delay
 
127
 
 
128
    if refresh is not None:
 
129
        iterable.UPDATE_EVERY = refresh
 
130
 
 
131
    return iterable
 
132
 
 
133
def trange(*args):
 
134
    """
 
135
    speed textual wrapper for xrange.
 
136
    """
 
137
    return tqdm(__builtin__.xrange(*args))
 
138
 
 
139
 
 
140
# Don't import GTK unless required
 
141
gtqdmManager = None
 
142
def gtqdm(iterable, delay=0, refresh=None, size=None, repr=None):
 
143
    """
 
144
    speed graphic wrapper for a generic iterable.
 
145
    Ignores the settings set by speedConfig and is always graphic.
 
146
    """
 
147
    global gtqdmManager
 
148
    if gtqdmManager is None:
 
149
        from gtk_widget import GTKIterationContext
 
150
        gtqdmManager = IterationManager(GTKIterationContext)
 
151
    if not isinstance(iterable, IteratorWrapper):
 
152
        iterable = IteratorWrapper(iterable, gtqdmManager, size, repr=repr)
 
153
 
 
154
    if delay is not None:
 
155
        iterable.BEGIN_AFTER = delay
 
156
    if refresh is not None:
 
157
        iterable.UPDATE_EVERY = refresh
 
158
    return iterable
 
159
 
 
160
def grange(*args):
 
161
    """
 
162
    speed graphic wrapper for xrange
 
163
    """
 
164
    return gtqdm(__buildint__.xrange(*args))
 
165
 
 
166
def btqdm(*args, **kwargs):
 
167
    """
 
168
    frozen-bubble tqdm
 
169
    """
 
170
    a = os.system('frozen-bubble -nm&')
 
171
    return tqdm(*args, **kwargs)
 
172