Skip to content

Case Converters

sensei.cases

Module containing case converters.

Case Converter is a function that takes the string of one case and converts it to the string of another case and similar structure.

Import them directly from Sensei:

from sensei import camel_case, snake_case, pascal_case, constant_case, kebab_case, header_case

They can be applied at different levels:

from sensei import Router, camel_case, snake_case

router = Router(
    'https://api.example.com',
    body_case=camel_case,
    response_case=snake_case
)

@router.post('/users')
def create_user(first_name: str, birth_city: str, ...) -> User:
    pass
from sensei import Router, camel_case, snake_case

router = Router('https://api.example.com')

@router.post('/users', body_case=camel_case, response_case=snake_case)
def create_user(first_name: str, birth_city: str, ...) -> User:
    pass
router = Router(host, response_case=camel_case)

class User(APIModel):
    def __header_case__(self, s: str) -> str:
        return kebab_case(s)

    @staticmethod
    def __response_case__(s: str) -> str:
        return snake_case(s)

    @classmethod
    @router.get('/users/{id_}')
    def get(cls, id_: Annotated[int, Path(alias='id')]) -> Self: pass

snake_case

snake_case(s)

Convert a string to the snake_case.

Example
print(snake_case('myParam'))
my_param
PARAMETER DESCRIPTION
s

The string to convert.

TYPE: str

Source code in sensei/cases.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def snake_case(s: str) -> str:
    """
    Convert a string to the snake_case.

    Example:
        ```python
        print(snake_case('myParam'))
        ```

        ```text
        my_param
        ```

    Args:
          s (str): The string to convert.
    """
    s = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', s)
    s = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s)
    s = re.sub(r'\W+', '_', s).lower()
    s = re.sub(r'_+', '_', s)
    return s

camel_case

camel_case(s)

Convert a string to the camelCase.

Example
print(snake_case('my_param'))
myParam
PARAMETER DESCRIPTION
s

The string to convert.

TYPE: str

Source code in sensei/cases.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def camel_case(s: str) -> str:
    """
    Convert a string to the camelCase.

    Example:
        ```python
        print(snake_case('my_param'))
        ```

        ```text
        myParam
        ```

    Args:
          s (str): The string to convert.
    """
    s = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', s)
    s = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s)
    s = re.sub(r'\W+', '_', s)
    words = s.split('_')
    capitalized_words = [word.capitalize() for word in words]
    return capitalized_words[0].lower() + ''.join(capitalized_words[1:])

pascal_case

pascal_case(s)

Convert a string to the PascalCase.

Example
print(snake_case('my_param'))
MyParam
PARAMETER DESCRIPTION
s

The string to convert.

TYPE: str

Source code in sensei/cases.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def pascal_case(s: str) -> str:
    """
    Convert a string to the PascalCase.

    Example:
        ```python
        print(snake_case('my_param'))
        ```

        ```text
        MyParam
        ```

    Args:
          s (str): The string to convert.
    """
    s = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', s)
    s = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s)
    s = re.sub(r'\W+', '_', s)
    words = s.split('_')
    capitalized_words = [word.capitalize() for word in words]
    return ''.join(capitalized_words)

constant_case

constant_case(s)

Convert a string to the CONSTANT_CASE.

Example
print(snake_case('myParam'))
MY_PARAM
PARAMETER DESCRIPTION
s

The string to convert.

TYPE: str

Source code in sensei/cases.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def constant_case(s: str) -> str:
    """
    Convert a string to the CONSTANT_CASE.

    Example:
        ```python
        print(snake_case('myParam'))
        ```

        ```text
        MY_PARAM
        ```

    Args:
          s (str): The string to convert.
    """
    s = re.sub(r'(.)([A-Z][a-z]+)', r'\1_\2', s)
    s = re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', s)
    s = re.sub(r'[\W_]+', '_', s)
    return s.upper()

kebab_case

kebab_case(s)

Convert a string to the kebab-case.

Example
print(snake_case('myParam'))
my-param
PARAMETER DESCRIPTION
s

The string to convert.

TYPE: str

Source code in sensei/cases.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def kebab_case(s: str) -> str:
    """
    Convert a string to the kebab-case.

    Example:
        ```python
        print(snake_case('myParam'))
        ```

        ```text
        my-param
        ```

    Args:
          s (str): The string to convert.
    """
    s = re.sub(r"(\s|_|-)+", " ", s)
    s = re.sub(r"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+",
               lambda mo: ' ' + mo.group(0).lower(), s)
    s = '-'.join(s.split())
    return s

header_case

header_case(s)

Convert a string to Header-Case.

Example
print(snake_case('myParam'))
My-Param
PARAMETER DESCRIPTION
s

The string to convert.

TYPE: str

Source code in sensei/cases.py
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def header_case(s: str) -> str:
    """
    Convert a string to Header-Case.

    Example:
        ```python
        print(snake_case('myParam'))
        ```

        ```text
        My-Param
        ```

    Args:
          s (str): The string to convert.
    """
    s = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', s)
    s = re.sub('([a-z0-9])([A-Z])', r'\1 \2', s)
    s = re.sub(r'[_\W]+', ' ', s)
    words = s.split()
    capitalized_words = [word.capitalize() for word in words]
    return '-'.join(capitalized_words)