source: trunk/GSASIIpy3.py @ 937

Last change on this file since 937 was 937, checked in by toby, 8 years ago

More improvements to ValidatedTxtCtrl?

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1'''
2*GSASIIpy3: Python 3.x Routines*
3================================
4
5Module to hold python 3-compatible code, to keep it separate from
6code that will break with __future__ options.
7
8'''
9from __future__ import division
10import numpy as np
11# de
12sind = sin = s = lambda x: np.sin(x*np.pi/180.)
13cosd = cos = c = lambda x: np.cos(x*np.pi/180.)
14tand = tan = t = lambda x: np.tan(x*np.pi/180.)
15sqrt = sq = lambda x: np.sqrt(x)
16pi = np.pi
17
18def FormulaEval(string):
19    '''Evaluates a algebraic formula into a float, if possible. Works
20    properly on fractions e.g. 2/3 only with python 3.0+ division.
21
22    Expressions such as 2/3, 3*pi, sin(45)/2, 2*sqrt(2), 2**10 can all
23    be evaluated.
24
25    :param str string: Character string containing a Python expression
26      to be evaluated.
27
28    :returns: the value for the expression as a float or None if the expression does not
29      evaluate to a valid number.
30   
31    '''
32    try:
33        val = float(eval(string))
34        if np.isnan(val) or np.isinf(val): return None
35    except:
36        return None
37    return val
38
39def FormatValue(val,maxdigits=10):
40    '''Format a float to fit in ``maxdigits`` spaces, showing as much
41    precision as possible, more or less.
42
43    :param float val: number to be formatted.
44
45    :param int maxdigits: the number of digits to be used for display of the
46      number (defaults to 10).
47
48    :returns: a string with <= maxdigits characters (I hope). 
49    '''
50    # does the standard str() conversion fit?
51    string = str(val)
52    if len(string) <= maxdigits: return string.strip()
53    # negative numbers, leave room for a sign
54    if val < 0: maxdigits -= 1
55    decimals = maxdigits - 2
56    if abs(val) < 1e-99 or abs(val) > 1e99:
57        decimals = maxdigits - 6
58        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}" # create format string
59    elif abs(val) < 1e-9 or abs(val) > 1e9:
60        decimals = maxdigits - 5
61        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
62    elif abs(val) < 10**(4-decimals): # make sure at least 4 decimals show
63        decimals = maxdigits - 5
64        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
65    elif abs(val) >= 10**decimals: # deal with large numbers in smaller spaces
66        decimals = maxdigits - 5
67        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
68    elif abs(val) < 1: # use f format for small numbers
69        decimals = maxdigits - 2
70        fmt = "{" + (":{:d}.{:d}f".format(maxdigits,decimals))+"}"
71    else: # in range where g formatting should do what I want
72        decimals = maxdigits - 1
73        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
74    return fmt.format(val).strip()
Note: See TracBrowser for help on using the repository browser.