source: trunk/GSASIIpy3.py @ 1177

Last change on this file since 1177 was 1177, checked in by vondreele, 8 years ago

try to add more control over number format from ValidatedTxtCtrl?
not entirely successful so far

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