|
| 1 | +""" |
| 2 | +String convert functions |
| 3 | +
|
| 4 | +The MIT License (MIT) |
| 5 | +
|
| 6 | +Copyright (c) 2015 Taka Okunishi |
| 7 | +
|
| 8 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +of this software and associated documentation files (the "Software"), to deal |
| 10 | +in the Software without restriction, including without limitation the rights |
| 11 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +copies of the Software, and to permit persons to whom the Software is |
| 13 | +furnished to do so, subject to the following conditions: |
| 14 | +
|
| 15 | +The above copyright notice and this permission notice shall be included in all |
| 16 | +copies or substantial portions of the Software. |
| 17 | +
|
| 18 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +SOFTWARE. |
| 25 | +""" |
| 26 | + |
| 27 | +# The "stringcase" package appears to unmaintained |
| 28 | +# https://github.com/okunishinishi/python-stringcase/issues/42 |
| 29 | +# Relevant parts have been vendored here so we can fix warnings |
| 30 | +# that will eventually become errors |
| 31 | +# Code here was copied from the 1.2.0 version uploaded to PyPI |
| 32 | +# https://pypi.org/project/stringcase/1.2.0/ |
| 33 | + |
| 34 | +import re |
| 35 | + |
| 36 | + |
| 37 | +def camelcase(string: str): |
| 38 | + """Convert string into camel case. |
| 39 | +
|
| 40 | + Args: |
| 41 | + string: String to convert. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + string: Camel case string. |
| 45 | +
|
| 46 | + """ |
| 47 | + |
| 48 | + string = re.sub(r"\w[\s\W]+\w", "", str(string)) |
| 49 | + if not string: |
| 50 | + return string |
| 51 | + return lowercase(string[0]) + re.sub( |
| 52 | + r"[\-_\.\s]([a-z])", lambda matched: uppercase(matched.group(1)), string[1:] |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +def lowercase(string: str): |
| 57 | + """Convert string into lower case. |
| 58 | +
|
| 59 | + Args: |
| 60 | + string: String to convert. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + string: Lowercase case string. |
| 64 | +
|
| 65 | + """ |
| 66 | + |
| 67 | + return str(string).lower() |
| 68 | + |
| 69 | + |
| 70 | +def snakecase(string: str): |
| 71 | + """Convert string into snake case. |
| 72 | + Join punctuation with underscore |
| 73 | +
|
| 74 | + Args: |
| 75 | + string: String to convert. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + string: Snake cased string. |
| 79 | +
|
| 80 | + """ |
| 81 | + |
| 82 | + string = re.sub(r"[\-\.\s]", "_", str(string)) |
| 83 | + if not string: |
| 84 | + return string |
| 85 | + return lowercase(string[0]) + re.sub( |
| 86 | + r"[A-Z]", lambda matched: "_" + lowercase(matched.group(0)), string[1:] |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def uppercase(string: str): |
| 91 | + """Convert string into upper case. |
| 92 | +
|
| 93 | + Args: |
| 94 | + string: String to convert. |
| 95 | +
|
| 96 | + Returns: |
| 97 | + string: Uppercase case string. |
| 98 | +
|
| 99 | + """ |
| 100 | + |
| 101 | + return str(string).upper() |
0 commit comments