-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Move locale code out of tm, into _config #25757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
99c5b71
implement _config.localization to avoid runtime import in ccalendar
jbrockmendel 72eced6
move a couple more funcs to localization, move test_locale to test_lo…
jbrockmendel 056c0fc
whitespace fixup
jbrockmendel a2d506b
isort fixup
jbrockmendel aba2e19
Merge branch 'master' of https://github.com/pandas-dev/pandas into lo…
jbrockmendel a0c6fa2
Merge branch 'master' of https://github.com/pandas-dev/pandas into lo…
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
pandas._config is considered explicitly upstream of everything else in pandas, | ||
should have no intra-pandas dependencies. | ||
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
""" | ||
Helpers for configuring locale settings. | ||
|
||
Name `localization` is chosen to avoid overlap with builtin `locale` module. | ||
""" | ||
from contextlib import contextmanager | ||
import locale | ||
|
||
|
||
@contextmanager | ||
def set_locale(new_locale, lc_var=locale.LC_ALL): | ||
""" | ||
Context manager for temporarily setting a locale. | ||
|
||
Parameters | ||
---------- | ||
new_locale : str or tuple | ||
A string of the form <language_country>.<encoding>. For example to set | ||
the current locale to US English with a UTF8 encoding, you would pass | ||
"en_US.UTF-8". | ||
lc_var : int, default `locale.LC_ALL` | ||
The category of the locale being set. | ||
|
||
Notes | ||
----- | ||
This is useful when you want to run a particular block of code under a | ||
particular locale, without globally setting the locale. This probably isn't | ||
thread-safe. | ||
""" | ||
current_locale = locale.getlocale() | ||
|
||
try: | ||
locale.setlocale(lc_var, new_locale) | ||
normalized_locale = locale.getlocale() | ||
if all(x is not None for x in normalized_locale): | ||
yield '.'.join(normalized_locale) | ||
else: | ||
yield new_locale | ||
finally: | ||
locale.setlocale(lc_var, current_locale) | ||
|
||
|
||
def can_set_locale(lc, lc_var=locale.LC_ALL): | ||
""" | ||
Check to see if we can set a locale, and subsequently get the locale, | ||
without raising an Exception. | ||
|
||
Parameters | ||
---------- | ||
lc : str | ||
The locale to attempt to set. | ||
lc_var : int, default `locale.LC_ALL` | ||
The category of the locale being set. | ||
|
||
Returns | ||
------- | ||
is_valid : bool | ||
Whether the passed locale can be set | ||
""" | ||
|
||
try: | ||
with set_locale(lc, lc_var=lc_var): | ||
pass | ||
except (ValueError, locale.Error): | ||
# horrible name for a Exception subclass | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
def _valid_locales(locales, normalize): | ||
""" | ||
Return a list of normalized locales that do not throw an ``Exception`` | ||
when set. | ||
|
||
Parameters | ||
---------- | ||
locales : str | ||
A string where each locale is separated by a newline. | ||
normalize : bool | ||
Whether to call ``locale.normalize`` on each locale. | ||
|
||
Returns | ||
------- | ||
valid_locales : list | ||
A list of valid locales. | ||
""" | ||
if normalize: | ||
normalizer = lambda x: locale.normalize(x.strip()) | ||
else: | ||
normalizer = lambda x: x.strip() | ||
|
||
return list(filter(can_set_locale, map(normalizer, locales))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the noqa for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some of the imports are not used in this file, but are retained so that we can still use them as
tm.foo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should try to completely remove these from testing at some point