def test_lc_numeric_basic(self): # Test nl_langinfo against localeconv tested = False for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) setlocale(LC_CTYPE, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): nl_radixchar = nl_langinfo(li) li_radixchar = localeconv()[lc] try: set_locale = setlocale(LC_NUMERIC) except Error: set_locale = "<not able to determine>" self.assertEqual(nl_radixchar, li_radixchar, "%s (nl_langinfo) != %s (localeconv) " "(set to %s, using %s)" % ( nl_radixchar, li_radixchar, loc, set_locale)) tested = True if not tested: self.skipTest('no suitable locales')
def test_localeconv(self): import _locale lconv_c = { "currency_symbol": "", "decimal_point": ".", "frac_digits": 127, "grouping": [], "int_curr_symbol": "", "int_frac_digits": 127, "mon_decimal_point": "", "mon_grouping": [], "mon_thousands_sep": "", "n_cs_precedes": 127, "n_sep_by_space": 127, "n_sign_posn": 127, "negative_sign": "", "p_cs_precedes": 127, "p_sep_by_space": 127, "p_sign_posn": 127, "positive_sign": "", "thousands_sep": "" } _locale.setlocale(_locale.LC_ALL, "C") lconv = _locale.localeconv() for k, v in lconv_c.items(): assert lconv[k] == v
def test_float_parsing(self): # Bug #1391872: Test whether float parsing is okay on European # locales. tested = False for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) setlocale(LC_CTYPE, loc) except Error: continue # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs) if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ": continue self.assertEqual(int(eval('3.14') * 100), 314, "using eval('3.14') failed for %s" % loc) self.assertEqual(int(float('3.14') * 100), 314, "using float('3.14') failed for %s" % loc) if localeconv()['decimal_point'] != '.': self.assertRaises(ValueError, float, localeconv()['decimal_point'].join(['1', '23'])) tested = True if not tested: self.skipTest('no suitable locales')
def _compile(pattern, flags): # internal: compile pattern try: p, loc = _cache[type(pattern), pattern, flags] if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE): return p except KeyError: pass if isinstance(pattern, _pattern_type): if flags: raise ValueError( "cannot process flags argument with a compiled pattern") return pattern if not sre_compile.isstring(pattern): raise TypeError("first argument must be string or compiled pattern") p = sre_compile.compile(pattern, flags) if not (flags & DEBUG): if len(_cache) >= _MAXCACHE: _cache.clear() if p.flags & LOCALE: if not _locale: return p loc = _locale.setlocale(_locale.LC_CTYPE) else: loc = None _cache[type(pattern), pattern, flags] = p, loc return p
def test_str_float(self): import _locale import locale _locale.setlocale(_locale.LC_ALL, self.language_en) assert locale.str(1.1) == '1.1' _locale.setlocale(_locale.LC_ALL, self.language_pl) assert locale.str(1.1) == '1,1'
def test_lc_numeric_localeconv(self): # Test localeconv against known values for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): self.numeric_tester("localeconv", localeconv()[lc], lc, loc)
def test_lc_numeric_nl_langinfo(self): # Test nl_langinfo against known values for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): self.numeric_tester("nl_langinfo", nl_langinfo(li), lc, loc)
def test_setlocale(self): import _locale raises(TypeError, _locale.setlocale, "", self.language_en) raises(TypeError, _locale.setlocale, _locale.LC_ALL, 6) raises(_locale.Error, _locale.setlocale, 123456, self.language_en) assert _locale.setlocale(_locale.LC_ALL, None) assert _locale.setlocale(_locale.LC_ALL)
def test_lc_numeric_localeconv(self): # Test localeconv against known values for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue for lc in ("decimal_point", "thousands_sep"): self.numeric_tester('localeconv', localeconv()[lc], lc, loc)
def test_strcoll_unicode(self): import _locale _locale.setlocale(_locale.LC_ALL, self.language_pl) assert _locale.strcoll(u"b", u"b") == 0 assert _locale.strcoll(u"a", u"b") < 0 assert _locale.strcoll(u"b", u"a") > 0 raises(TypeError, _locale.strcoll, 1, u"b") raises(TypeError, _locale.strcoll, u"b", 1)
def setup_class(cls): cls.space = space = gettestobjspace(usemodules=['_locale']) if sys.platform != 'win32': cls.w_language_en = cls.space.wrap("en_US") cls.w_language_utf8 = cls.space.wrap("en_US.UTF-8") cls.w_language_pl = cls.space.wrap("pl_PL.UTF-8") cls.w_encoding_pl = cls.space.wrap("utf-8") else: cls.w_language_en = cls.space.wrap("English_US") cls.w_language_utf8 = cls.space.wrap("English_US.65001") cls.w_language_pl = cls.space.wrap("Polish_Poland.1257") cls.w_encoding_pl = cls.space.wrap("cp1257") import _locale # check whether used locales are installed, otherwise the tests will # fail current = _locale.setlocale(_locale.LC_ALL) try: try: _locale.setlocale(_locale.LC_ALL, space.str_w(cls.w_language_en)) _locale.setlocale(_locale.LC_ALL, space.str_w(cls.w_language_pl)) except _locale.Error: py.test.skip("necessary locales not installed") # Windows forbids the UTF-8 character set since Windows XP. try: _locale.setlocale(_locale.LC_ALL, space.str_w(cls.w_language_utf8)) except _locale.Error: del cls.w_language_utf8 finally: _locale.setlocale(_locale.LC_ALL, current)
def test_string_ulcase(self): if not hasattr(self, 'language_utf8'): skip("No utf8 locale on this platform") import _locale, string lcase = "abcdefghijklmnopqrstuvwxyz" ucase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" _locale.setlocale(_locale.LC_ALL, self.language_utf8) assert string.lowercase == lcase assert string.uppercase == ucase _locale.setlocale(_locale.LC_ALL, self.language_en)
def test_strcoll(self): import _locale _locale.setlocale(_locale.LC_ALL, self.language_pl) assert _locale.strcoll("a", "b") < 0 assert _locale.strcoll("ą", "b") < 0 assert _locale.strcoll("ć", "b") > 0 assert _locale.strcoll("c", "b") > 0 assert _locale.strcoll("b", "b") == 0 raises(TypeError, _locale.strcoll, 1, "b") raises(TypeError, _locale.strcoll, "b", 1)
def test_lc_numeric_nl_langinfo(self): # Test nl_langinfo against known values tested = False for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc): tested = True if not tested: self.skipTest('no suitable locales')
def test_lc_numeric_localeconv(self): # Test localeconv against known values tested = False for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue formatting = localeconv() for lc in ("decimal_point", "thousands_sep"): if self.numeric_tester('localeconv', formatting[lc], lc, loc): tested = True if not tested: self.skipTest('no suitable locales')
def test_string_ulcase(self): import _locale, string lcase = "abcdefghijklmnopqrstuvwxyz" ucase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" _locale.setlocale(_locale.LC_ALL, self.language_utf8) assert string.lowercase == lcase assert string.uppercase == ucase _locale.setlocale(_locale.LC_ALL, self.language_en) assert string.lowercase != lcase assert string.uppercase != ucase
def test_strxfrm(self): # TODO more tests would be nice import _locale _locale.setlocale(_locale.LC_ALL, "C") a = "1234" b = _locale.strxfrm(a) assert a is not b assert a == b raises(TypeError, _locale.strxfrm, 1) _locale.setlocale(_locale.LC_ALL, self.language_pl) a = "1234" b = _locale.strxfrm(a) assert a is not b
def test_float_parsing(self): # Bug #1391872: Test whether float parsing is okay on European # locales. for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs) if loc == "eu_ES" and localeconv()["decimal_point"] == "' ": continue self.assertEquals(int(eval("3.14") * 100), 314, "using eval('3.14') failed for %s" % loc) self.assertEquals(int(float("3.14") * 100), 314, "using float('3.14') failed for %s" % loc) if localeconv()["decimal_point"] != ".": self.assertRaises(ValueError, float, localeconv()["decimal_point"].join(["1", "23"]))
def test_strcoll(self): import _locale _locale.setlocale(_locale.LC_ALL, self.language_pl) assert _locale.strcoll("a", "b") < 0 assert _locale.strcoll( u"\N{LATIN SMALL LETTER A WITH OGONEK}".encode(self.encoding_pl), "b") < 0 assert _locale.strcoll( u"\N{LATIN SMALL LETTER C WITH ACUTE}".encode(self.encoding_pl), "b") > 0 assert _locale.strcoll("c", "b") > 0 assert _locale.strcoll("b", "b") == 0 raises(TypeError, _locale.strcoll, 1, "b") raises(TypeError, _locale.strcoll, "b", 1)
def test_setlocale_negative(self): #the locale is empty string c= _locale.LC_ALL locale ='' _locale.setlocale(c,locale) #the locale is None locale = _locale.setlocale(c) resultLocale =_locale.setlocale(c) self.assertEqual(locale,resultLocale) #set Numeric as a unknown locale,should thorw a _locale.Error locale ="11-22" self.assertRaises(_locale.Error,_locale.setlocale,c,locale) locale ="@^#^&%" self.assertRaises(_locale.Error,_locale.setlocale,c,locale) locale ="xxxxxx" self.assertRaises(_locale.Error,_locale.setlocale,c,locale)
def test_setlocale_negative(): # the locale is empty string c = _locale.LC_ALL locale = "" _locale.setlocale(c, locale) # the locale is None locale = _locale.setlocale(c) resultLocale = _locale.setlocale(c) AreEqual(locale, resultLocale) # set Numeric as a unknown locale,should thorw a _locale.Error locale = "11-22" AssertError(_locale.Error, _locale.setlocale, c, locale) locale = "@^#^&%" AssertError(_locale.Error, _locale.setlocale, c, locale) locale = "xxxxxx" AssertError(_locale.Error, _locale.setlocale, c, locale)
def numeric_tester(self, calc_type, calc_value, data_type, used_locale): """Compare calculation against known value, if available""" try: set_locale = setlocale(LC_NUMERIC) except Error: set_locale = "<not able to determine>" known_value = known_numerics.get(used_locale, ("", ""))[data_type == "thousands_sep"] if known_value and calc_value: self.assertEquals( calc_value, known_value, self.lc_numeric_err_msg % (calc_value, known_value, calc_type, data_type, set_locale, used_locale), )
def test_lc_numeric(self): for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): nl_radixchar = nl_langinfo(li) li_radixchar = localeconv()[lc] # Both with seeing what the locale is set to in order to detect # when setlocale lies and says it accepted the locale setting # but in actuality didn't use it (as seen in OS X 10.3) try: set_locale = setlocale(LC_NUMERIC) except Error: set_locale = "<not able to determine>" self.assertEquals(nl_radixchar, li_radixchar, "%s != %s (%s); " "supposed to be %s, set to %s" % (nl_radixchar, li_radixchar, lc, loc, set_locale))
def _compile(*key): # internal: compile pattern pattern, flags = key bypass_cache = flags & DEBUG if not bypass_cache: cachekey = (type(key[0]),) + key try: p, loc = _cache[cachekey] if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE): return p except KeyError: pass if isinstance(pattern, _pattern_type): if flags: raise ValueError('Cannot process flags argument with a compiled pattern') return pattern if not sre_compile.isstring(pattern): raise TypeError, "first argument must be string or compiled pattern" try: p = sre_compile.compile(pattern, flags) except error, v: raise error, v # invalid expression
def test_setlocale(self): c_list = [ _locale.LC_ALL, _locale.LC_COLLATE, _locale.LC_CTYPE, _locale.LC_MONETARY, _locale.LC_NUMERIC, _locale.LC_TIME, ] for c in c_list: resultLocale = None _locale.setlocale(c,"English") resultLocale = _locale.setlocale(c) self.assertEqual(resultLocale,"English_United States.1252") for c in c_list: resultLocale = None _locale.setlocale(c,"French") resultLocale = _locale.setlocale(c) self.assertEqual(resultLocale,"French_France.1252")
def test_strcoll(): _locale.setlocale(_locale.LC_COLLATE, "English") validcollate() _locale.setlocale(_locale.LC_COLLATE, "French") validcollate()
raise ValueError( 'Cannot process flags argument with a compiled pattern') return pattern if not sre_compile.isstring(pattern): raise TypeError, "first argument must be string or compiled pattern" try: p = sre_compile.compile(pattern, flags) except error, v: raise error, v # invalid expression if not bypass_cache: if len(_cache) >= _MAXCACHE: _cache.clear() if p.flags & LOCALE: if not _locale: return p loc = _locale.setlocale(_locale.LC_CTYPE) else: loc = None _cache[cachekey] = p, loc return p def _compile_repl(*key): # internal: compile replacement pattern p = _cache_repl.get(key) if p is not None: return p repl, pattern = key try: p = sre_parse.parse_template(repl, pattern) except error, v:
def teardown_class(cls): import _locale _locale.setlocale(_locale.LC_ALL, cls.oldlocale)
def setup_class(cls): cls.space = space = gettestobjspace( usemodules=['_locale', 'unicodedata']) if sys.platform != 'win32': cls.w_language_en = cls.space.wrap("C") cls.w_language_utf8 = cls.space.wrap("en_US.utf8") cls.w_language_pl = cls.space.wrap("pl_PL.utf8") cls.w_encoding_pl = cls.space.wrap("utf-8") else: cls.w_language_en = cls.space.wrap("English_US") cls.w_language_utf8 = cls.space.wrap("English_US.65001") cls.w_language_pl = cls.space.wrap("Polish_Poland.1257") cls.w_encoding_pl = cls.space.wrap("cp1257") import _locale # check whether used locales are installed, otherwise the tests will # fail current = _locale.setlocale(_locale.LC_ALL) try: try: # some systems are only UTF-8 oriented try: _locale.setlocale(_locale.LC_ALL, space.str_w(cls.w_language_en)) except _locale.Error: _locale.setlocale(_locale.LC_ALL, space.str_w(cls.w_language_utf8)) cls.w_language_en = cls.w_language_utf8 _locale.setlocale(_locale.LC_ALL, space.str_w(cls.w_language_pl)) except _locale.Error: py.test.skip("necessary locales not installed") # Windows forbids the UTF-8 character set since Windows XP. try: _locale.setlocale(_locale.LC_ALL, space.str_w(cls.w_language_utf8)) except _locale.Error: del cls.w_language_utf8 finally: _locale.setlocale(_locale.LC_ALL, current)
def setUp(self): self.oldlocale = setlocale(LC_NUMERIC)
clear_eol = "" move_up = "" def length(self, s): return len(s) def move_x(self, n): return "" # Save current locale settings try: _lls = [] for i in range(100): ll = _locale.setlocale(i) _lls.append(ll) except _locale.Error: pass # Instantiate a terminal if sys.__stdin__ and sys.__stdout__: term = MyTerminal() else: term = NoTerminal() # Restore previous locale settings for i, ll in enumerate(_lls): _locale.setlocale(i, ll)
def test_nl_langinfo(self): import sys if sys.platform == 'win32': skip("No langinfo on Windows") import _locale langinfo_consts = [ 'ABDAY_1', 'ABDAY_2', 'ABDAY_3', 'ABDAY_4', 'ABDAY_5', 'ABDAY_6', 'ABDAY_7', 'ABMON_1', 'ABMON_10', 'ABMON_11', 'ABMON_12', 'ABMON_2', 'ABMON_3', 'ABMON_4', 'ABMON_5', 'ABMON_6', 'ABMON_7', 'ABMON_8', 'ABMON_9', 'CODESET', 'CRNCYSTR', 'DAY_1', 'DAY_2', 'DAY_3', 'DAY_4', 'DAY_5', 'DAY_6', 'DAY_7', 'D_FMT', 'D_T_FMT', 'MON_1', 'MON_10', 'MON_11', 'MON_12', 'MON_2', 'MON_3', 'MON_4', 'MON_5', 'MON_6', 'MON_7', 'MON_8', 'MON_9', 'NOEXPR', 'RADIXCHAR', 'THOUSEP', 'T_FMT', 'YESEXPR', 'AM_STR', 'PM_STR', ] for constant in langinfo_consts: assert hasattr(_locale, constant) _locale.setlocale(_locale.LC_ALL, "C") assert _locale.nl_langinfo(_locale.ABDAY_1) == "Sun" assert _locale.nl_langinfo(_locale.ABMON_1) == "Jan" assert _locale.nl_langinfo(_locale.T_FMT) == "%H:%M:%S" assert _locale.nl_langinfo(_locale.YESEXPR) == '^[yY]' assert _locale.nl_langinfo(_locale.NOEXPR) == "^[nN]" assert _locale.nl_langinfo(_locale.THOUSEP) == '' raises(ValueError, _locale.nl_langinfo, 12345) raises(TypeError, _locale.nl_langinfo, None)
import _locale # set locale for use in currency formatting result = _locale.setlocale(_locale.LC_ALL, '') if result == 'C': _locale.setlocale(_locale.LC_ALL, 'en_US') print("Welcome to future value calculator") print() choice = "y" while choice.lower() == "y": mon_inv = float(input("Enter monthly investment: \t")) yr_int_rate = float(input("Enter yearly interest rate: \t")) years = int(input("Enter Number of Years: \t")) # convert yearly values to monthly values mon_int_rate = yr_int_rate / 12 / 100 months = years * 12 # calculate future_value = 0 for i in range(months): future_value = future_value + mon_inv mon_int_amount = future_value * mon_int_rate future_value = future_value + mon_int_amount print("Future value : \t\t\t", int(future_value))
def setUp(self): self.oldlocale = setlocale(LC_ALL)
def tearDown(self): setlocale(LC_ALL, self.oldlocale)
def tearDown(self): for lc, setting in self.saved_lc: _locale.setlocale(lc, setting) super().tearDown()
def tearDown(self): setlocale(LC_NUMERIC, self.oldlocale)