undarray Printing Framework

The exposed user class is class PrintableUndarray, but there exists some more-deep guts like class upy.printable.PrintableElement, class upy.printable.Widths, and the upy.decimal2 module.

The upy.printable.PrintableUndarray class

class upy.printable.PrintableUndarray(undarray=None, sigmas=None, enforce_sign_value=None, enforce_sign_exponent=None, format=None, precision=None, infinite_precision=None)[source]

Prints an undarray. To print subportions, use __getattr__().

Initialisation methods

PrintableUndarray.__init__(undarray=None, sigmas=None, enforce_sign_value=None, enforce_sign_exponent=None, format=None, precision=None, infinite_precision=None)[source]

Print undarray UNDARRAY or printable array PRINTABLE_ARRAY by giving SIGMAS standard deviations as uncertainty.

To enforce the printing of optional ‘+’ signs in the value and the exponent, use ENFORCE_SIGN_VALUE and ENFORCE_SIGN_EXPONENT.

The three FORMATS supported are ‘float’, e.g. 0.120 +- 0.034, ‘exp’, e.g. (1.20 +- 0.34) 10^-1, and ‘int’, e.g. 12300 +- 4500. By default, the format will be determined from the values and the uncertainties. The ‘int’ mode is choosen upon integer type of values and uncertainties. If the uncertainty is all-zero, the printing mode will be determined from the value alone, else both must be integer to enable int printing mode.

PRECISION influences the precision of the output. Generally, the precision is determined from the uncertainty. With PRECISION = 1, the output will look like (1.0 +- 0.3), with PRECISION = 2, like (1.00 +- 0.23). If the uncertainty is zero, INFINITE_PRECISION will be used instead, giving the number of digits beind the point, either in float or exp mode. In int mode all post-point digits are suppressed. If both the value and the uncertainty are zero, only (0 +- 0) is printed. The default PRECISION is 2.

Note that you can affect the way the array is printed also by calling numpy.set_printoptions().

Keying methods

PrintableUndarray.__getitem__(key)[source]

Return a portion of self.

PrintableUndarray.__setitem__(key, value)[source]

Set a portion KEY of self to undarray VALUE.

PrintableUndarray.__len__()[source]

Printing methods

PrintableUndarray.__str__()[source]

First, simulate the conversion, thereby reporting all used elements to .widths_*. Not all elements may be printed because of ellipsis. Then, apply the found maximum widths to self, and print the array.

Public, but not user-intendend methods

PrintableUndarray.report_strings(strings_value, strings_uncertainty, strings_exponent)[source]

The upy.printable.PrintableElement class

PrintableElement.__init__(value, uncertainty, enforce_sign_value=None, enforce_sign_exponent=None, conversion_reporter=None, format=None, precision=None, infinite_precision=None)[source]

VALUE is the nominal value, UNCERTAINTY a value proportional to the standard deviation. The proportionality factor depends on what the user intends to print.

To enforce the printing of optional ‘+’ signs in the value and the exponent, use ENFORCE_SIGN_VALUE and ENFORCE_SIGN_EXPONENT.

The three FORMATS supported are ‘float’, e.g. 0.120 +- 0.034, ‘exp’, e.g. (1.20 +- 0.34) 10^-1, and ‘int’, e.g. 12300 +- 4500. By default, the format will be determined from the values and the uncertainties. The ‘int’ mode is choosen upon integer type of values and uncertainties. If the uncertainty of a value is zero, the printing mode will be determined from the value alone, else both must be integer to enable int printing mode.

PRECISION influences the precision of the output. Generally, the precision is determined from the uncertainty. With PRECISION = 1, the output will look like (1.0 +- 0.3), with PRECISION = 2, like (1.00 +- 0.23). If the uncertainty is zero, INFINITE_PRECISION will be used instead, giving the number of digits beind the point, either in float or exp mode. In int mode all post-point digits are suppressed. If both the value and the uncertainty are zero, only (0 +- 0) is printed. The default PRECISION is 2.

PrintableElement.__str__()[source]

Note that the return value ends on a whitespace, to make the space between elements in array printing two spaces wide.

The upy.printable.Widths class

Widths.__init__()[source]

Initialise to zero.

Widths.report(strings)[source]

Report a upy.decimal2.DecimalStrings instance to be printed.

Widths.apply_to(decimal_number)[source]

Apply this widths to a upy.decimal2.DecimalNumber DECIMAL_NUMBER.

class upy.decimal2.DecimalNumber

class upy.decimal2.DecimalNumber(value, precision=None, exponent=None, infinite_precision=None, enforce_sign=None, ceil=None, width_sign=None, width_left=None, width_point=None, width_right=None)[source]

Calculates the leftmost digit, and formats real numbers. All values given to __init__() are read-write, except for .precision, which must be set via .set_precision(), and .exponent, which must be set via .set_exponent().

DecimalNumber.__init__(value, precision=None, exponent=None, infinite_precision=None, enforce_sign=None, ceil=None, width_sign=None, width_left=None, width_point=None, width_right=None)[source]

VALUE is the value to be formatted. INFINITE_PRECISION is the number of digits used when emulating infinite precision (default 15). EXPONENT is the power of ten separated up from the VALUE. Example: VALUE = 12.3 and EXPONENT = 1 -> 1.23e1. Strings returned represent .value_without_exponent up to digit with weight 10 ** PRECISION. If ENFORCE_SIGN is True, return a ‘+’ in front of positive numbers. If CEIL is True, discarded portions of the number will always increase the value represented by the string. When calculating the parts of the string, WIDTH_* are used. By default IntPlus(infinite = True) is used as PRECISION. WIDTH_SIGN forces the sign to have a certain width, this means, the sign string will be padded with whitespace, right justified. WIDTH_LEFT is the width of the sign string plus the string before the point, right justified. WIDTH_POINT is the width of the point, center justified. WIDTH_RIGHT is the width of the post-point string, left justified.

DecimalNumber.set_exponent(exponent)[source]

Set the exponent to EXPONENT. It must be integer.

DecimalNumber.set_precision(precision)[source]

Set the precision to PRECISION. If it is an IntPlus instance, it will be taken over, else an IntPlus instance will be created.

DecimalNumber.get_leftmost_digit(guess=None)[source]

The position of the leftmost digit of .value_without_exponent in decimal representation. A position of zero means, that the leftmost digit is the digit with weight 10 ** 0. Other return values RETURN mean, that the leftmost non-zero digit has weight 10 ** RETURN. Thus nonnegative RETURNs are before the point in “fixed-point” representation, and negative RETURNs will be behind the point.

To obtain a real value with the leftmost non-zero digit at weight 1, use the expression VALUE * 10 ** (-RETURN).

Note that the return value is an instance of class IntPlus, which may represent infinity too (in case .value_without_exponent == 0).

You can supply an initial position of the search via GUESS.

DecimalNumber.get_strings()[source]

Return the string representing this DecimalNumber. Note that the string will represent .value_without_exponent, and not .value.

DecimalNumber.__str__()[source]

class upy.decimal2.IntPlus

class upy.decimal2.IntPlus(z=None, infinite=None)[source]

Represents number in Z+ = Z + {Infinity}. Read-only attributes:

.z - The number. .infinite - The instance represents Infinity.

IntPlus.__init__(z=None, infinite=None)[source]

Initialise from number Z, or specify infinite as true.

IntPlus.__str__()[source]
IntPlus.__repr__()[source]

class upy.decimal2.DecimalStrings

class upy.decimal2.DecimalStrings(str_left, str_point, str_right)[source]

Holds the result of formatting a number.

DecimalStrings.__init__(str_left, str_point, str_right)[source]
DecimalStrings.__str__()[source]