_string_helpers.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """
  2. String-handling utilities to avoid locale-dependence.
  3. Used primarily to generate type name aliases.
  4. """
  5. # "import string" is costly to import!
  6. # Construct the translation tables directly
  7. # "A" = chr(65), "a" = chr(97)
  8. _all_chars = [chr(_m) for _m in range(256)]
  9. _ascii_upper = _all_chars[65:65+26]
  10. _ascii_lower = _all_chars[97:97+26]
  11. LOWER_TABLE = "".join(_all_chars[:65] + _ascii_lower + _all_chars[65+26:])
  12. UPPER_TABLE = "".join(_all_chars[:97] + _ascii_upper + _all_chars[97+26:])
  13. def english_lower(s):
  14. """ Apply English case rules to convert ASCII strings to all lower case.
  15. This is an internal utility function to replace calls to str.lower() such
  16. that we can avoid changing behavior with changing locales. In particular,
  17. Turkish has distinct dotted and dotless variants of the Latin letter "I" in
  18. both lowercase and uppercase. Thus, "I".lower() != "i" in a "tr" locale.
  19. Parameters
  20. ----------
  21. s : str
  22. Returns
  23. -------
  24. lowered : str
  25. Examples
  26. --------
  27. >>> from numpy.core.numerictypes import english_lower
  28. >>> english_lower('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
  29. 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789_'
  30. >>> english_lower('')
  31. ''
  32. """
  33. lowered = s.translate(LOWER_TABLE)
  34. return lowered
  35. def english_upper(s):
  36. """ Apply English case rules to convert ASCII strings to all upper case.
  37. This is an internal utility function to replace calls to str.upper() such
  38. that we can avoid changing behavior with changing locales. In particular,
  39. Turkish has distinct dotted and dotless variants of the Latin letter "I" in
  40. both lowercase and uppercase. Thus, "i".upper() != "I" in a "tr" locale.
  41. Parameters
  42. ----------
  43. s : str
  44. Returns
  45. -------
  46. uppered : str
  47. Examples
  48. --------
  49. >>> from numpy.core.numerictypes import english_upper
  50. >>> english_upper('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
  51. 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  52. >>> english_upper('')
  53. ''
  54. """
  55. uppered = s.translate(UPPER_TABLE)
  56. return uppered
  57. def english_capitalize(s):
  58. """ Apply English case rules to convert the first character of an ASCII
  59. string to upper case.
  60. This is an internal utility function to replace calls to str.capitalize()
  61. such that we can avoid changing behavior with changing locales.
  62. Parameters
  63. ----------
  64. s : str
  65. Returns
  66. -------
  67. capitalized : str
  68. Examples
  69. --------
  70. >>> from numpy.core.numerictypes import english_capitalize
  71. >>> english_capitalize('int8')
  72. 'Int8'
  73. >>> english_capitalize('Int8')
  74. 'Int8'
  75. >>> english_capitalize('')
  76. ''
  77. """
  78. if s:
  79. return english_upper(s[0]) + s[1:]
  80. else:
  81. return s