source: trunk/GSASIIpy3.py @ 1162

Last change on this file since 1162 was 1077, checked in by toby, 12 years ago

cleanup plot & svn bugs; set missing keywords; CIF export done; update docs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 2.7 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: 1077 $")
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):
42    '''Format a float to fit in ``maxdigits`` spaces, showing as much
43    precision as possible, more or less.
44
45    :param float val: number to be formatted.
46
47    :param int maxdigits: the number of digits to be used for display of the
48      number (defaults to 10).
49
50    :returns: a string with <= maxdigits characters (I hope). 
51    '''
52    # does the standard str() conversion fit?
53    string = str(val)
54    if len(string) <= maxdigits: return string.strip()
55    # negative numbers, leave room for a sign
56    if val < 0: maxdigits -= 1
57    decimals = maxdigits - 2
58    if abs(val) < 1e-99 or abs(val) > 1e99:
59        decimals = maxdigits - 6
60        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}" # create format string
61    elif abs(val) < 1e-9 or abs(val) > 1e9:
62        decimals = maxdigits - 5
63        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
64    elif abs(val) < 10**(4-decimals): # make sure at least 4 decimals show
65        decimals = maxdigits - 5
66        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
67    elif abs(val) >= 10**decimals: # deal with large numbers in smaller spaces
68        decimals = maxdigits - 5
69        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
70    elif abs(val) < 1: # use f format for small numbers
71        decimals = maxdigits - 2
72        fmt = "{" + (":{:d}.{:d}f".format(maxdigits,decimals))+"}"
73    else: # in range where g formatting should do what I want
74        decimals = maxdigits - 1
75        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
76    return fmt.format(val).strip()
Note: See TracBrowser for help on using the repository browser.