00001 ############################################################################## 00002 # 00003 # Copyright (c) 2007-2008 Albert Cervera i Areny <albert@nan-tic.com> 00004 # 00005 # WARNING: This program as such is intended to be used by professional 00006 # programmers who take the whole responsability of assessing all potential 00007 # consequences resulting from its eventual inadequacies and bugs 00008 # End users who are looking for a ready-to-use solution with commercial 00009 # garantees and support are strongly adviced to contract a Free Software 00010 # Service Company 00011 # 00012 # This program is Free Software; you can redistribute it and/or 00013 # modify it under the terms of the GNU General Public License 00014 # as published by the Free Software Foundation; either version 2 00015 # of the License, or (at your option) any later version. 00016 # 00017 # This program is distributed in the hope that it will be useful, 00018 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 # GNU General Public License for more details. 00021 # 00022 # You should have received a copy of the GNU General Public License 00023 # along with this program; if not, write to the Free Software 00024 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00025 # 00026 ############################################################################## 00027 00028 import locale 00029 00030 ## @brief This function converts a string into an integer allowing 00031 # operations (+, -, /, *). 00032 # 00033 # The formula is calculated and the output is returned by 00034 # the function. If the formula contains floating point 00035 # values or results they're converted into integer at the end. 00036 def textToInteger(text): 00037 chars = ['+', '-', '/', '*', '.', '(', ')', ','] 00038 chars = chars + [str(x) for x in range(10)] 00039 text = text.replace(',', '.') 00040 try: 00041 return int(eval(text)) 00042 except: 00043 return False 00044 00045 ## @brief This function converts a string into a float allowing 00046 # operations (+, -, /, *). 00047 # 00048 # The formula is calculated and the output is returned by 00049 # the function. 00050 def textToFloat(text): 00051 chars = ['+', '-', '/', '*', '.', '(', ')', ','] 00052 chars = chars + [str(x) for x in range(10)] 00053 newtext = text.replace(',', '.') 00054 value = False 00055 try: 00056 value = float(eval(newtext)) 00057 except: 00058 if '.' in text and ',' in text: 00059 if text.rindex('.') > text.rindex(','): 00060 newtext = text.replace(',','') 00061 else: 00062 newtext = text.replace('.','') 00063 newtext = newtext.replace(',','.') 00064 try: 00065 value = float(newtext) 00066 except: 00067 pass 00068 return value 00069 00070 ## @brief This function converts a float into text. By default the number 00071 # of decimal digits is 2. 00072 def floatToText(number, digits=None, thousands=False): 00073 if isinstance(number, int): 00074 number = float(number) 00075 if not isinstance(number, float): 00076 number = 0.0 00077 if digits: 00078 # Digits might come from the server as a tuple, list or a string 00079 # So: (14,2), [14,2], '(14,2)' and '[14,2]' are all valid forms 00080 if isinstance(digits,list) or isinstance(digits,tuple): 00081 d=str(digits[1]) 00082 else: 00083 d=digits.split(',')[1].strip(' )]') 00084 else: 00085 d='2' 00086 00087 if thousands: 00088 return locale.format('%.' + d + 'f', number, True, True) 00089 else: 00090 return ('%.' + d + 'f') % number 00091 00092 00093 ## @brief This function converts an integer into text. 00094 def integerToText(number): 00095 if isinstance(number, float): 00096 number = int(number) 00097 if not isinstance(number, int): 00098 number = 0 00099 return '%d' % number 00100 00101 ## @brief This function returns True if the given value can be converted into 00102 # a float number. Otherwise it returns False. 00103 def isNumeric(value): 00104 try: 00105 return float(value) or True 00106 except (ValueError, TypeError), e: 00107 return False 00108 00109 ## @brief This function converts the given paramter (which should be a number) into 00110 # a human readable storage value, ie. bytes, Kb, Mb, Gb, Tb. 00111 def bytesToText(number): 00112 number = float(number) 00113 texts = [ _('%d bytes'), _('%.2f Kb'), _('%.2f Mb'), _('%.2f Gb'), _('%.2f Tb') ] 00114 i = 0 00115 while number >= 1024 and i < len(texts) - 1: 00116 number = number / 1024 00117 i += 1 00118 return texts[i] % number