source: orange/docs/myinspect.py @ 11028:009ba5a75e30

Revision 11028:009ba5a75e30, 1.5 KB checked in by Miha Stajdohar <miha.stajdohar@…>, 17 months ago (diff)

Added a common documentation index.

Line 
1from inspect import *
2ofas = formatargspec
3
4def delete_while_accurate(s, accuracy=0.0000001):
5    """ Shortens a string containing a float
6    while still keeping the desired accuracy. """
7    orig = float(s)
8    cur = s
9    try:
10        while abs((float(cur[:-1])-orig)/orig) < accuracy:
11            cur = cur[:-1]
12    except:
13        pass
14    return cur
15
16def nfas(*args, **kwargs):
17
18    def fv(value):
19        if isinstance(value, float):
20            cand = str(value)
21            value = delete_while_accurate(cand)
22        return "=" + str(value)
23
24    if "formatvalue" not in kwargs and len(args) < 8:
25        kwargs["formatvalue"] = fv
26
27    return ofas(*args, **kwargs)
28
29formatargspec = nfas
30
31# inspect.getmembers from Python 2.7
32# In Python 2.6 there is try/except missing and function fails on C++-based classes if they define attributes
33# Example: AttributeError: 'Plot' object attribute 'animate_points' is an instance attribute
34def getmembers27(object, predicate=None):
35    """Return all members of an object as (name, value) pairs sorted by name.
36    Optionally, only return members that satisfy a given predicate."""
37    results = []
38    for key in dir(object):
39        try:
40            value = getattr(object, key)
41        except AttributeError:
42            continue
43        if not predicate or predicate(value):
44            results.append((key, value))
45    results.sort()
46    return results
47
48getmembers = getmembers27
49
50if __name__ == "__main__":
51    for a in [ "0", "0.", "-1.3", "1.30000000001" ]:
52        print a, "shorter:", delete_while_accurate(a)
Note: See TracBrowser for help on using the repository browser.