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:
|
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 |
|
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:
|
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 |
|
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:
|
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 |
|
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:
|
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 |
|
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:
|
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 |
|
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:
|
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 |
|