Package pytils :: Module utils
[hide private]

Source Code for Module pytils.utils

  1  # -*- coding: utf-8 -*- 
  2  # -*- test-case-name: pytils.test.test_utils -*- 
  3  # PyTils - simple processing for russian strings 
  4  # Copyright (C) 2006-2007  Yury Yurevich 
  5  # 
  6  # http://gorod-omsk.ru/blog/pythy/projects/pytils/ 
  7  # 
  8  # This program is free software; you can redistribute it and/or 
  9  # modify it under the terms of the GNU General Public License 
 10  # as published by the Free Software Foundation, version 2 
 11  # of the License. 
 12  # 
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  """ 
 18  Misc utils for internal use 
 19  """ 
 20   
 21  __id__ = __revision__ = "$Id: utils.py 63 2007-01-02 09:22:16Z the.pythy $" 
 22  __url__ = "$URL: https://pythy.googlecode.com/svn/trunk/pytils/pytils/utils.py $" 
 23   
 24  import sys 
 25   
 26   
27 -def provide_unicode(stext, encoding, default=u"неизвестно"):
28 """ 29 Provide Unicode from text 30 31 @param stext: text 32 @type stext: C{str} 33 34 @param encoding: encoding if input text 35 @type encoding: C{str} 36 37 @return: C{unicode} 38 """ 39 try: 40 utext = str(stext).decode(encoding) 41 except UnicodeDecodeError, err: 42 utext = default % {'error': err, 'value': u""} 43 return utext
44 45
46 -def provide_str(utext, encoding, default="unknown"):
47 """ 48 Provide text from Unicode 49 50 @param utext: unicode text 51 @type utext: C{unicode} 52 53 @param encoding: encoding of output text 54 @type encoding: C{str} 55 56 @return: C{str} 57 """ 58 try: 59 stext = unicode(utext).encode(encoding) 60 except UnicodeEncodeError, err: 61 stext = default % {'error': err, 'value': ""} 62 return stext
63 64
65 -def get_value_by_name(variable_name, depth=1):
66 """ 67 Return value of variable by it's name 68 69 @param variable_name: name of variable 70 @type variable_name: C{str} 71 72 @param depth: stack depth 73 @type depth: C{int} 74 75 @raise RuntimeError: when unable to fetch variable 76 """ 77 try: 78 variable_value = sys._getframe(depth).f_locals[variable_name] 79 except KeyError: 80 raise RuntimeError("Unable to fetch variable %s (depth %d)" % \ 81 (variable_name, depth)) 82 return variable_value
83 84
85 -def check_type(variable_name, typ):
86 """ 87 Checks type of variable 88 89 @param variable_name: name of variable 90 @type variable_name: C{str} 91 92 @param typ: type checking for 93 @type typ: C{type} or C{tuple} of types 94 95 @return: None when check successful 96 97 @raise TypeError: check failed 98 """ 99 variable_value = get_value_by_name(variable_name, 2) 100 if not isinstance(variable_value, typ): 101 raise TypeError("%s must be %s, not %s" % \ 102 (variable_name, str(typ), type(variable_value)))
103 104
105 -def check_length(variable_name, length):
106 """ 107 Checks length of variable's value 108 109 @param variable_name: name of variable 110 @type variable_name: C{str} 111 112 @param length: length checking for 113 @type length: C{int} 114 115 @return: None when check successful 116 117 @raise ValueError: check failed 118 """ 119 variable_value = get_value_by_name(variable_name, 2) 120 _length = len(variable_value) 121 if _length != length: 122 raise ValueError("%s's length must be %d, but it %d" % \ 123 (variable_name, length, _length))
124 125
126 -def check_positive(variable_name, strict=False):
127 """ 128 Checks if variable is positive 129 130 @param variable_name: name of variable 131 @type variable_name: C{str} 132 133 @return: None when check successful 134 135 @raise ValueError: check failed 136 """ 137 variable_value = get_value_by_name(variable_name, 2) 138 if not strict and variable_value < 0: 139 raise ValueError("%s must be positive or zero, not %s" % \ 140 (variable_name, str(variable_value))) 141 if strict and variable_value <= 0: 142 raise ValueError("%s must be positive, not %s" % \ 143 (variable_name, str(variable_value)))
144