Table of contents Index

Module PyLaTeX.UnitPrint

Declared in module PyLaTeX

def LaTeXGenerateUnit(value, *data, **kw)
def __format_digit_string(value, thousand_sep, reverse=0)
def __str_reverse(val)
def formatFloat(val, *data, **kw)
def formatInt(val, *data, **kw)
def generateUnitList(value, *data, **kw)
string __author__ = 'Berthold H\366llmann, bhoel@starship.python.net'
string __file__ = '../PyLaTeX/UnitPrint.pyc'
tuple __unit_print = (None, '%s', '\\unit[%s]{%s}', '\\unitfrac[%s]{%s}{%s}')
string __version__ = '1.6'
string decimal_point = ','
instance digit_group = re.RegexObject instance
string fact_sep = ''
string group_off = ''
string group_on = ''
string pow_char = ''
string thousand_sep = ''
alias p = PhysicalQuantities.PhysicalQuantity (type class)

Description

This module provides the functions formatInt, formatFloat, generateUnitList, and LaTeXGenerateUnit. Therfor it uses the values thousand_sep and decimal_point. thousand_sep is determinded at startup using locale.localeconv()["thousands_sep"], decimal_point is taken from locale.localeconv()["decimal_point"]. If locale is not avaible, thousand_sep is set to "" and decimal_point is set to ".". The regular expression digit_group is used to split into the chunks divided by thousand_sep. digit_group usually generates a chunk length of three digits using re.compile(r"([0-9]{3,3}?)").

formatInt
Format integers. Insert thousand_sep after each 3 digits beginning at the right side.
formatFloat
Format float. Replace the decimal points by decimal_point. Insert thousand_sep after each 3 digits beginning at the right side for the part before the decimal point and beginning at the left side for the part after the decimal point.
generateUnitList
This splits an instance of Konrad Hinsens PhysicalQuantities into a list of one up to three elements. First element is the value of the instance. Second element is the nominator of the instances unit if it exists. If no nominator exists but a deniminator does exist this will be a 1. The third element is the denominator of the instances unit if it exists.
LaTeXGenerateUnit
This function uses the output of generateUnitList to be typesetted with LaTeX and Axel Reichert's units.sty. It chooses the apropriate command to use, which is none, \unit, or \unitfrac.

Example:

    >>> import PhysicalQuantities
    >>> p = PhysicalQuantities.PhysicalQuantity
    >>> import UnitPrint
    >>> from UnitPrint import *
    >>> print "got: ->%s<-" % formatInt(1)
    got: ->1<-
    >>> print "got: ->%s<-" % formatInt(12)
    got: ->12<-
    >>> print "got: ->%s<-" % formatInt(123)
    got: ->123<-
    >>> print "got: ->%s<-" % formatInt(1234)
    got: ->1234<-
    >>> print "got: ->%s<-" % formatInt(12345)
    got: ->12345<-
    >>> print "got: ->%s<-" % formatInt(123456)
    got: ->123456<-
    >>> print "got: ->%s<-" % formatInt(1234567)
    got: ->1234567<-
    >>> print "got: ->%s<-" % formatInt(12345678)
    got: ->12345678<-
    >>> print "got: ->%s<-" % formatInt(123456789)
    got: ->123456789<-
    >>> UnitPrint.thousand_sep = r"\,"
    >>> print "got: ->%s<-" % formatInt(1234567890)
    got: ->1\,234\,567\,890<-
    >>> UnitPrint.thousand_sep = r""
    >>> print "got: ->%s<-" % formatInt(1234567890,
    ...                                 {'thousand_sep': '-'})
    got: ->1-234-567-890<-
    >>> print "got: ->%s<-" % formatInt(1.)
    got: ->1<-
    >>> print "got: ->%s<-" % formatInt(12.)
    got: ->12<-
    >>> print "got: ->%s<-" % formatInt(123.)
    got: ->123<-
    >>> print "got: ->%s<-" % formatInt(1234.)
    got: ->1234<-
    >>> print "got: ->%s<-" % formatInt(12345.)
    got: ->12345<-
    >>> print "got: ->%s<-" % formatInt(123456.)
    got: ->123456<-
    >>> print "got: ->%s<-" % formatInt(1234567.)
    got: ->1234567<-
    >>> print "got: ->%s<-" % formatInt(12345678.)
    got: ->12345678<-
    >>> print "got: ->%s<-" % formatInt(123456789.)
    got: ->123456789<-
    >>> print "got: ->%s<-" % formatInt(1234567890.)
    got: ->1234567890<-
    >>> value = 1234.567891234
    >>> print "got: ->%s<-" % formatFloat(value, 1)
    got: ->1234.6<-
    >>> print "got: ->%s<-" % formatFloat(value, format=2)
    got: ->1234.57<-
    >>> print "got: ->%s<-" % formatFloat(value, 3)
    got: ->1234.568<-
    >>> print "got: ->%s<-" % formatFloat(value, 4)
    got: ->1234.5679<-
    >>> print "got: ->%s<-" % formatFloat(value, 5)
    got: ->1234.56789<-
    >>> print "got: ->%s<-" % formatFloat(value, 6)
    got: ->1234.567891<-
    >>> UnitPrint.thousand_sep = r"\,"
    >>> print "got: ->%s<-" % UnitPrint.formatFloat(value, 7)
    got: ->1\,234.567\,891\,2<-
    >>> print "got: ->%s<-" % formatFloat(value, 7)
    got: ->1\,234.567\,891\,2<-
    >>> print "got: ->%s<-" % formatFloat(value, 8,
    ...                                   {'thousand_sep': -,
    ...                                    decimal_point: '#'})
    got: ->1-234#567-891-23<-
    >>> print generateUnitList(1)
    ('1.0',)
    >>> print generateUnitList(p("1 m/m"))
    ('1.0',)
    >>> print generateUnitList(1, formatInt)
    ('1',)
    >>> print generateUnitList(1, formatFloat)
    ('1.000',)
    >>> print generateUnitList(p("1m"))
    ('1.0', 'm')
    >>> print generateUnitList(p("1m/s"))
    ('1.0', m, 's')
    >>> print generateUnitList(p("1kg*m/s**2"))
    ('1.0', kgm, 's2')
    >>> print generateUnitList(1/p("1kg*m/s**2"))
    ('1.0', s2, 'mkg')
    >>> print generateUnitList(p(1, "1/kg/m/s**2"))
    ('1.0', 1, 'kgms2')
    >>> print generateUnitList(p("1m"), formatInt)
    ('1', 'm')
    >>> print generateUnitList(p("1m"), formatFloat, 2)
    ('1.00', 'm')
    >>> print generateUnitList(p("1m"), formatFloat, 3)
    ('1.000', 'm')
    >>> print generateUnitList(p("12345.678901m"), formatFloat, 5)
    ('12\\,345.678\\,90', 'm')
    >>> print generateUnitList(p("1m/s"), formatInt)
    ('1', m, 's')
    >>> print generateUnitList(p("1kg*m/s**2"), formatInt)
    ('1', kgm, 's2')
    >>> print generateUnitList(1/p("1kg*m/s**2"), formatInt)
    ('1', s2, 'mkg')
    >>> print generateUnitList(p(1, "1/kg/m/s**2"), formatInt)
    ('1', 1, 'kgms2')
    >>> print LaTeXGenerateUnit(1)
    {1.0}
    >>> print LaTeXGenerateUnit(1, formatInt)
    {1}
    >>> print LaTeXGenerateUnit(1, formatFloat)
    {1.000}
    >>> print LaTeXGenerateUnit(p("1m"))
    \unit[{1.0}]{{m}}
    >>> print LaTeXGenerateUnit(p("1m/s"))
    \unitfrac[{1.0}]{{m}}{{s}}
    >>> print LaTeXGenerateUnit(p("1kg*m/s**2"))
    \unitfrac[{1.0}]{{kg}\,{m}}{{s}^{2}}
    >>> print LaTeXGenerateUnit(1/p("1kg*m/s**2"))
    \unitfrac[{1.0}]{{s}^{2}}{{m}\,{kg}}
    >>> print LaTeXGenerateUnit(p(1, "1/kg/m/s**2"))
    \unitfrac[{1.0}]{{1}}{{kg}\,{m}\,{s}^{2}}
    >>> print LaTeXGenerateUnit(p("1m"), formatInt)
    \unit[{1}]{{m}}
    >>> print LaTeXGenerateUnit(p("1m"), formatFloat, 2)
    \unit[{1.00}]{{m}}
    >>> print LaTeXGenerateUnit(p("1m"), formatFloat, 3)
    \unit[{1.000}]{{m}}
    >>> print LaTeXGenerateUnit(p("12345.6789012m"), formatFloat, 5)
    \unit[{12\,345.678\,90}]{{m}}
    >>> print LaTeXGenerateUnit(p("1m/s"), formatInt)
    \unitfrac[{1}]{{m}}{{s}}
    >>> print LaTeXGenerateUnit(p("1kg*m/s**2"), formatInt)
    \unitfrac[{1}]{{kg}\,{m}}{{s}^{2}}
    >>> print LaTeXGenerateUnit(1/p("1kg*m/s**2"), formatInt)
    \unitfrac[{1}]{{s}^{2}}{{m}\,{kg}}
    >>> print LaTeXGenerateUnit(p(1, "1/kg/m/s**2"), formatInt)
    \unitfrac[{1}]{{1}}{{kg}\,{m}\,{s}^{2}}
    >>> 

LaTeXGenerateUnit(value, *data, **kw)

Process an instance of PhysicalQuantity and return a string ready to print the value and unit using LaTeX and Axel Reichert's units.sty. The function has one argument:

func
function to process the value part with, default is float.

__format_digit_string(value, thousand_sep, reverse=0)

Return value with thousand_sep insert each three digits.

__str_reverse(val)

Return the string val reverted.

formatFloat(val, *data, **kw)

Format an floating point number inserting thousand_sep each three digits in both parts and insert decimal_point. The possible arguments are:

format
The number of digits after the decimal point, Default is 3.
sep
This is a dictionary of seperators. formatFloat uses the values:
thousand_sep
String to insert each three digits. Default is thousand_sep
decimal_point
String to insert as decimal point. Default is decimal_point.

formatInt(val, *data, **kw)

Format an integer inserting thousand_sep each three digits. The function has one argumnt:

sep
This is a dictionary of seperators. formatInt only uses thousand_sep as the string to insert each three digits. Default is thousand_sep

generateUnitList(value, *data, **kw)

Return a tuple of value, unit nominator, and unit denominator from a PhysicalQuantity instance. The returned tuple has the length 1, 2, or 3. Avaible options are:

func
function to process the value part with, default is float.
args
additional arguments to func, default is {}.
format
A dictionary containing some format changing values, possible entry's are:
fact_sep
String used to seperate nominators and denominators entries. Default is fact_sep.
group_on
String used to start a grouping. Default is group_on.
group_off
String used to end a grouping. Default is group_off.
pow_char
String used to indicate that a unit in nominator or denominator is raises to any power. Default is pow_char.

Author: Berthold H\366llmann
Version: 1.6