Extend your string powers with Str+¶
Features ✨️
- Wrapper Class
- +464 test covered in 36 Tests files!
- Simple use!
- Made with A.I. contribution 🤖
Install 📀¶
pip install strplus
Main Class: 🚀¶
Str
¶
Bases: str
Str Class
The Str Class is a Wrapper class that extends the str Class, giving it superpowers to handle strings and making it easy to use the strplus functions! You don't need to import, only if you want! All methods from Str are recursive and return the Str object itself, so you always have the same features in the result.
Easy and simple no parentheses!
my_string = Str("Cast_this_StringToSnake")
my_string.snake
my_string = Str("cast_this_string_to_camel")
my_string.camel
my_string = Str("Cast_this_string_TO_Pascal!")
my_string.pascal
Use parentheses if you prefer!
my_string = Str("HelloWorld")
my_string.to_snake()
Source code in strplus/strplus.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
|
print
property
¶
Print itself!
This attribute is an alias for str.print
separator_as_comma
property
¶
Looks for a separator in your own value and cast it to comma!
separator_as_comma is an alias for cast_separator_to_comma
split_by_sep
property
¶
Splits itself into a list of strings based on the built-in common separators.
This attribute is an alias for the functionsplit_by_separator
Use
- If you need to to inform a different list in can call the the function split_by_separator from this class and inform your custom list of separators!
- You can find examples of use in menu examples.str_class
cast(input_value, join_sep=',', type_constraint=True)
staticmethod
¶
Casts the input value to Str+. If it's a list will be joined by comma or you can define you custom separator to join the elements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_value |
any
|
The input value to cast. |
required |
join_sep |
str
|
The separator to join list elements with. Defaults to ",". |
','
|
type_constraint |
bool
|
If True (default), raise a ValueError if the input value is not a string or a list. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Str | A string or a string representation of a list of values joined by the specified separator. |
This example shows how to use cast()
to cast an input value to a string
Str.cast("Hello, world!")
"Hello, world!"
Str.cast(["apple", "banana", "orange"], join_sep="-")
"apple-banana-orange"
Str.cast(123, type_constraint=False)
Skipping: It's not possible to cast from type: <class 'int'>
123
Raises:
Type | Description |
---|---|
ValueError
|
If the input value is not a string or a list and |
Tips
- If the input value is a list, the resulting string will be a string representation of the list elements joined by the specified separator.
Source code in strplus/strplus.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
|
cast_separator_to_comma(separator=None)
¶
Replaces a specified separator or the automatically detected one with a comma in the input string. Implementation of strplus.functions.cast_separator_to_comma
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_string |
str
|
The input string to replace separators in. |
required |
separator |
Optional[str]
|
The separator to replace with a comma. If None, the function will attempt to determine the appropriate separator based on the input string. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str | A string resulting from replacing the specified or detected separator with a comma. |
This example shows how to use cast_separator_to_comma()
to replace a separator in a string
my_string = Str("one-two-three", "-")
my_string.cast_separator_to_comma("-")
"one,two,three"
my_string = Str("one two three")
my_string.cast_separator_to_comma()
"one,two,three"
Warning
The function will only attempt to detect the separator when the separator
argument is None.
Tips
- If the input string does not contain the specified or detected separator, the function will return the original input string unchanged.
- See the
get_separator
function for more details about how the function will attempt to detect the separator.
Source code in strplus/strplus.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
split_by_separator(separator=None, type_constraint=True)
¶
Splits a string into a list of strings using the specified separator(s), base in the built-in common separators. Implementation of strplus.functions.split_by_separator
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_string |
str
|
The input string to split. |
required |
separator |
Optional[Union[List[str], str]]
|
The separator(s) to use when splitting the input string. This can be a single string, a list of strings, or None. If None, the function will attempt to determine the appropriate separator based on the input string. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
List[str]: A list of strings resulting from splitting the input string using the specified separator(s). |
This example shows how to use split_by_separator()
to split a string using a single separator
my_string = Str("one,two,three")
my_string.split_by_separator(",")
["one", "two", "three"]
my_string = Str("one-two three|four")
my_string.split_by_separator(["-", " ", "|"])
['one', 'two three|four']
Warning
Only one separator frequency found in the list provided, so the priority will be respect!
my_string = Str("one two three four")
my_string.split_by_separator()
["one", "two", "three", "four"]
Tips
- If the input string contains multiple consecutive instances of the specified separator(s), the resulting list may contain empty strings. To remove empty strings from the resulting list, you can use a list comprehension to filter out any empty strings.
- See the
get_separator
for mor details about how the function will attempt to determine the appropriate separator.
Important
- If the separator is a list of strings, the function will attempt to determine the appropriate separator to use based on the input string. If no appropriate separator is found, the function will return the original input string as a single-element list.
Source code in strplus/strplus.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
to_camel()
¶
Converts a string from any case to CamelCase. Implementation of strplus.cases.to_camel
Returns:
Name | Type | Description |
---|---|---|
str | The converted string in CamelCase. |
Example
my_string = Str('this_is-an_example')
my_string.to_camel()
my_string = Str('This is a test!')
my_string.to_camel()
Source code in strplus/strplus.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
to_list()
¶
Converts a string to a list of strings, where each word is a separate element in the list. Implementation of strplus.functions.to_list
Returns:
Type | Description |
---|---|
List[str]: A list of strings, where each word in the input string is a separate element in the list. |
Converting a string to a list
This example shows how to use to_list()
to convert a string to a list.
my_string = Str('hello world')
my_string.to_list
my_string = Str('some-mixed_string With spaces_underscores-and-hyphens')
my_string.to_list
my_string = Str('123abc')
my_string.to_list
Use tips
- If you need to convert a string to a list of integers or floats, you can use list comprehension to convert each element to the desired type.
- If you need to remove duplicates from the list, you can convert it to a set and then back to a list.
Important
- For best results, avoid using punctuation or non-alphanumeric characters in the input string.
- This function uses regular expressions to identify words in the input string.
Source code in strplus/strplus.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
to_pascal()
¶
Simple method to converts a string to PascalCase. Implementation of strplus.cases.to_pascal
Returns:
Name | Type | Description |
---|---|---|
str | The PascalCase version of the input string. |
Example
my_string = Str('some-mixed_string With spaces_underscores-and-hyphens')
my_string.to_pascal()
Source code in strplus/strplus.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
to_snake()
¶
Converts a string to snake_case. Implementation of strplus.cases.to_snake
Returns:
Name | Type | Description |
---|---|---|
str | The string converted to snake_case. |
Examples
my_string = Str("HelloWorld")
my_string.snake("HelloWorld")
my_string = Str(" AnotherString! ")
my_string.to_snake()
my_string = Str("hello-world")
my_string.to_snake()
Source code in strplus/strplus.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|