Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
locale Namespace Reference

Functions

def localeconv
 
def setlocale
 
def strcoll
 
def strxfrm
 
def format
 
def str
 
def atof
 
def atoi
 
def normalize
 
def getdefaultlocale
 
def getlocale
 
def resetlocale
 

Variables

list __all__
 
int CHAR_MAX = 127
 
int LC_ALL = 6
 
int LC_COLLATE = 3
 
int LC_CTYPE = 0
 
int LC_MESSAGES = 5
 
int LC_MONETARY = 4
 
int LC_NUMERIC = 1
 
int LC_TIME = 2
 
 Error = ValueError
 
 _setlocale = setlocale
 Locale name aliasing engine. More...
 
dictionary encoding_alias
 Database. More...
 
dictionary locale_alias
 
dictionary windows_locale
 

Detailed Description

Locale support.

The module provides low-level access to the C lib's locale APIs
and adds high level number formatting APIs as well as a locale
aliasing engine to complement these.

The aliasing engine includes support for many commonly used locale
names and maps them to values suitable for passing to the C lib's
setlocale() function. It also includes default encodings for all
supported locale names.

Function Documentation

def locale.atof (   str,
  func = float 
)

Definition at line 162 of file locale.py.

References dospath.join(), and localeconv().

163 def atof(str,func=float):
164  "Parses a string as a float according to the locale settings."
165  #First, get rid of the grouping
166  ts = localeconv()['thousands_sep']
167  if ts:
168  s=str.split(ts)
169  str="".join(s)
170  #next, replace the decimal point with a dot
171  dd = localeconv()['decimal_point']
172  if dd:
173  s=str.split(dd)
174  str='.'.join(s)
175  #finally, parse the string
176  return func(str)
def locale.atoi (   str)

Definition at line 177 of file locale.py.

References atof(), format(), setlocale(), and str().

178 def atoi(str):
179  "Converts a string to an integer according to the locale settings."
180  return atof(str, int)
def locale.format (   f,
  val,
  grouping = 0 
)
Formats a value in the same way that the % formatting would use,
but takes the current locale into account.
Grouping is applied if the third parameter is true.

Definition at line 129 of file locale.py.

References localeconv().

130 def format(f,val,grouping=0):
131  """Formats a value in the same way that the % formatting would use,
132  but takes the current locale into account.
133  Grouping is applied if the third parameter is true."""
134  result = f % val
135  fields = result.split(".")
136  seps = 0
137  if grouping:
138  fields[0],seps=_group(fields[0])
139  if len(fields)==2:
140  result = fields[0]+localeconv()['decimal_point']+fields[1]
141  elif len(fields)==1:
142  result = fields[0]
143  else:
144  raise Error, "Too many decimal points in result string"
145 
146  while seps:
147  # If the number was formatted for a specific width, then it
148  # might have been filled with spaces to the left or right. If
149  # so, kill as much spaces as there where separators.
150  # Leading zeroes as fillers are not yet dealt with, as it is
151  # not clear how they should interact with grouping.
152  sp = result.find(" ")
153  if sp==-1:break
154  result = result[:sp]+result[sp+1:]
155  seps -= 1
156 
157  return result
def locale.getdefaultlocale (   envvars = ('LANGUAGE', 'LC_ALL',
  LC_CTYPE,
  LANG 
)
Tries to determine the default locale settings and returns
    them as tuple (language code, encoding).

    According to POSIX, a program which has not called
    setlocale(LC_ALL, "") runs using the portable 'C' locale.
    Calling setlocale(LC_ALL, "") lets it use the default locale as
    defined by the LANG variable. Since we don't want to interfere
    with the current locale setting we thus emulate the behavior
    in the way described above.

    To maintain compatibility with other platforms, not only the
    LANG variable is tested, but a list of variables given as
    envvars parameter. The first found to be defined will be
    used. envvars defaults to the search path used in GNU gettext;
    it must always contain the variable name 'LANG'.

    Except for the code 'C', the language code corresponds to RFC
    1766.  code and encoding can be None in case the values cannot
    be determined.

Definition at line 289 of file locale.py.

References cgitb.lookup().

290 def getdefaultlocale(envvars=('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')):
291 
292  """ Tries to determine the default locale settings and returns
293  them as tuple (language code, encoding).
294 
295  According to POSIX, a program which has not called
296  setlocale(LC_ALL, "") runs using the portable 'C' locale.
297  Calling setlocale(LC_ALL, "") lets it use the default locale as
298  defined by the LANG variable. Since we don't want to interfere
299  with the current locale setting we thus emulate the behavior
300  in the way described above.
301 
302  To maintain compatibility with other platforms, not only the
303  LANG variable is tested, but a list of variables given as
304  envvars parameter. The first found to be defined will be
305  used. envvars defaults to the search path used in GNU gettext;
306  it must always contain the variable name 'LANG'.
307 
308  Except for the code 'C', the language code corresponds to RFC
309  1766. code and encoding can be None in case the values cannot
310  be determined.
311 
312  """
313 
314  try:
315  # check if it's supported by the _locale module
316  import _locale
317  code, encoding = _locale._getdefaultlocale()
318  except (ImportError, AttributeError):
319  pass
320  else:
321  # make sure the code/encoding values are valid
322  if sys.platform == "win32" and code and code[:2] == "0x":
323  # map windows language identifier to language name
324  code = windows_locale.get(int(code, 0))
325  # ...add other platform-specific processing here, if
326  # necessary...
327  return code, encoding
328 
329  # fall back on POSIX behaviour
330  import os
331  lookup = os.environ.get
332  for variable in envvars:
333  localename = lookup(variable,None)
334  if localename is not None:
335  break
336  else:
337  localename = 'C'
338  return _parse_localename(localename)
339 
def locale.getlocale (   category = LC_CTYPE)
Returns the current setting for the given locale category as
    tuple (language code, encoding).

    category may be one of the LC_* value except LC_ALL. It
    defaults to LC_CTYPE.

    Except for the code 'C', the language code corresponds to RFC
    1766.  code and encoding can be None in case the values cannot
    be determined.

Definition at line 340 of file locale.py.

References _setlocale, normalize(), and setlocale().

341 def getlocale(category=LC_CTYPE):
342 
343  """ Returns the current setting for the given locale category as
344  tuple (language code, encoding).
345 
346  category may be one of the LC_* value except LC_ALL. It
347  defaults to LC_CTYPE.
348 
349  Except for the code 'C', the language code corresponds to RFC
350  1766. code and encoding can be None in case the values cannot
351  be determined.
352 
353  """
354  localename = _setlocale(category)
355  if category == LC_ALL and ';' in localename:
356  raise TypeError, 'category LC_ALL is not supported'
357  return _parse_localename(localename)
def locale.localeconv ( )
localeconv() -> dict.
    Returns numeric and monetary locale-specific parameters.

Definition at line 44 of file locale.py.

44 
45  def localeconv():
46  """ localeconv() -> dict.
47  Returns numeric and monetary locale-specific parameters.
48  """
49  # 'C' locale default values
50  return {'grouping': [127],
51  'currency_symbol': '',
52  'n_sign_posn': 127,
53  'p_cs_precedes': 127,
54  'n_cs_precedes': 127,
55  'mon_grouping': [],
56  'n_sep_by_space': 127,
57  'decimal_point': '.',
58  'negative_sign': '',
59  'positive_sign': '',
60  'p_sep_by_space': 127,
61  'int_curr_symbol': '',
62  'p_sign_posn': 127,
63  'thousands_sep': '',
64  'mon_thousands_sep': '',
65  'frac_digits': 127,
66  'mon_decimal_point': '',
67  'int_frac_digits': 127}
def locale.normalize (   localename)
Returns a normalized locale code for the given locale
    name.

    The returned locale code is formatted for use with
    setlocale().

    If normalization fails, the original name is returned
    unchanged.

    If the given encoding is not known, the function defaults to
    the default encoding for the locale code just like setlocale()
    does.

Definition at line 199 of file locale.py.

200 def normalize(localename):
201 
202  """ Returns a normalized locale code for the given locale
203  name.
204 
205  The returned locale code is formatted for use with
206  setlocale().
207 
208  If normalization fails, the original name is returned
209  unchanged.
210 
211  If the given encoding is not known, the function defaults to
212  the default encoding for the locale code just like setlocale()
213  does.
214 
215  """
216  # Normalize the locale name and extract the encoding
217  fullname = localename.lower()
218  if ':' in fullname:
219  # ':' is sometimes used as encoding delimiter.
220  fullname = fullname.replace(':', '.')
221  if '.' in fullname:
222  langname, encoding = fullname.split('.')[:2]
223  fullname = langname + '.' + encoding
224  else:
225  langname = fullname
226  encoding = ''
227 
228  # First lookup: fullname (possibly with encoding)
229  code = locale_alias.get(fullname, None)
230  if code is not None:
231  return code
232 
233  # Second try: langname (without encoding)
234  code = locale_alias.get(langname, None)
235  if code is not None:
236  if '.' in code:
237  langname, defenc = code.split('.')
238  else:
239  langname = code
240  defenc = ''
241  if encoding:
242  encoding = encoding_alias.get(encoding, encoding)
243  else:
244  encoding = defenc
245  if encoding:
246  return langname + '.' + encoding
247  else:
248  return langname
249 
250  else:
251  return localename
def locale.resetlocale (   category = LC_ALL)
Sets the locale for category to the default setting.

    The default setting is determined by calling
    getdefaultlocale(). category defaults to LC_ALL.

Definition at line 374 of file locale.py.

References _setlocale, and getdefaultlocale().

375 def resetlocale(category=LC_ALL):
376 
377  """ Sets the locale for category to the default setting.
378 
379  The default setting is determined by calling
380  getdefaultlocale(). category defaults to LC_ALL.
381 
382  """
383  _setlocale(category, _build_localename(getdefaultlocale()))
def setlocale (   category,
  locale = None 
)
setlocale(integer,string=None) -> string.
    Activates/queries locale processing.
Set the locale for the given category.  The locale can be
    a string, a locale tuple (language code, encoding), or None.

    Locale tuples are converted to strings the locale aliasing
    engine.  Locale strings are passed directly to the C lib.

    category may be given as one of the LC_* values.

Definition at line 68 of file locale.py.

68 
69  def setlocale(category, value=None):
70  """ setlocale(integer,string=None) -> string.
71  Activates/queries locale processing.
72  """
73  if value is not None and value != 'C':
74  raise Error, '_locale emulation only supports "C" locale'
75  return 'C'
def locale.str (   val)
Convert float to integer, taking the locale into account.

Definition at line 158 of file locale.py.

References format().

159 def str(val):
160  """Convert float to integer, taking the locale into account."""
161  return format("%.12g",val)
def locale.strcoll (   a,
  b 
)
strcoll(string,string) -> int.
    Compares two strings according to the locale.

Definition at line 76 of file locale.py.

References filecmp.cmp().

76 
77  def strcoll(a,b):
78  """ strcoll(string,string) -> int.
79  Compares two strings according to the locale.
80  """
81  return cmp(a,b)
def locale.strxfrm (   s)
strxfrm(string) -> string.
    Returns a string that behaves for cmp locale-aware.

Definition at line 82 of file locale.py.

References localeconv().

82 
83  def strxfrm(s):
84  """ strxfrm(string) -> string.
85  Returns a string that behaves for cmp locale-aware.
86  """
87  return s

Variable Documentation

list __all__
Initial value:
1 = ["setlocale","Error","localeconv","strcoll","strxfrm",
2  "format","str","atof","atoi","LC_CTYPE","LC_COLLATE",
3  "LC_TIME","LC_MONETARY","LC_NUMERIC", "LC_ALL","CHAR_MAX"]

Definition at line 22 of file locale.py.

_setlocale = setlocale

Locale name aliasing engine.

Definition at line 197 of file locale.py.

int CHAR_MAX = 127

Definition at line 34 of file locale.py.

dictionary encoding_alias

Database.

The following data was extracted from the locale.alias file which comes with X11 and then hand edited removing the explicit encoding definitions and adding some more aliases. The file is usually available as /usr/lib/X11/locale/locale.alias.

Definition at line 396 of file locale.py.

Error = ValueError

Definition at line 42 of file locale.py.

int LC_ALL = 6

Definition at line 35 of file locale.py.

int LC_COLLATE = 3

Definition at line 36 of file locale.py.

int LC_CTYPE = 0

Definition at line 37 of file locale.py.

int LC_MESSAGES = 5

Definition at line 38 of file locale.py.

int LC_MONETARY = 4

Definition at line 39 of file locale.py.

int LC_NUMERIC = 1

Definition at line 40 of file locale.py.

int LC_TIME = 2

Definition at line 41 of file locale.py.

dictionary locale_alias

Definition at line 437 of file locale.py.

dictionary windows_locale

Definition at line 630 of file locale.py.