string
CaseStyle
- class confirm.utils.string.CaseStyle(string='')
A string class which can be used to access a different case style via instance properties.
- Parameters:
string (str) – The string
- property camel_case
The camel case style of the string.
- Returns:
The camel case
- Return type:
str
Hint
Examples:
>>> CaseStyle('foo bar').camel_case # lower case to camelCase 'fooBar' >>> CaseStyle('foobar').camel_case # lowercase to camelCase 'foobar' >>> CaseStyle('FOO BAR').camel_case # UPPER CASE to camelCase 'fooBar' >>> CaseStyle('FOOBAR').camel_case # UPPERCASE to camelCase 'foobar' >>> CaseStyle('Foo Bar').camel_case # Title Case to camelCase 'fooBar' >>> CaseStyle('foo_bar').camel_case # snake_case to camelCase 'fooBar' >>> CaseStyle('foo-bar').camel_case # kebap-case to camelCase 'fooBar' >>> CaseStyle('FooBar').camel_case # PascalCase to camelCase 'fooBar' >>> CaseStyle('fooBar').camel_case # camelCase to camelCase 'fooBar'
- property kebap_case
The kebap case style of the string.
- Returns:
The kebap case
- Return type:
str
Hint
Examples:
>>> CaseStyle('foo bar').kebap_case # lower case to kebap-case 'foo-bar' >>> CaseStyle('foobar').kebap_case # lowercase to kebap-case 'foobar' >>> CaseStyle('FOO BAR').kebap_case # UPPER CASE to kebap-case 'foo-bar' >>> CaseStyle('FOOBAR').kebap_case # UPPERCASE to kebap-case 'foobar' >>> CaseStyle('Foo Bar').kebap_case # Title Case to kebap-case 'foo-bar' >>> CaseStyle('foo_bar').kebap_case # snake_case to kebap-case 'foo-bar' >>> CaseStyle('foo-bar').kebap_case # kebap-case to kebap-case 'foo-bar' >>> CaseStyle('FooBar').kebap_case # PascalCase to kebap-case 'foo-bar' >>> CaseStyle('fooBar').kebap_case # camelCase to kebap-case 'foo-bar'
- property pascal_case
The pascal case style of the string.
- Returns:
The pascal case
- Return type:
str
Hint
Examples:
>>> CaseStyle('foo bar').pascal_case # lower case to PascalCase 'FooBar' >>> CaseStyle('foobar').pascal_case # lowercase to PascalCase 'Foobar' >>> CaseStyle('FOO BAR').pascal_case # UPPER CASE to PascalCase 'FooBar' >>> CaseStyle('FOOBAR').pascal_case # UPPERCASE to PascalCase 'Foobar' >>> CaseStyle('Foo Bar').pascal_case # Title Case to PascalCase 'FooBar' >>> CaseStyle('foo_bar').pascal_case # snake_case to PascalCase 'FooBar' >>> CaseStyle('foo-bar').pascal_case # kebap-case to PascalCase 'FooBar' >>> CaseStyle('FooBar').pascal_case # PascalCase to PascalCase 'FooBar' >>> CaseStyle('fooBar').pascal_case # camelCase to PascalCase 'FooBar'
- property snake_case
The snake case style of the string.
- Returns:
The snake case
- Return type:
str
Hint
Examples:
>>> CaseStyle('foo bar').snake_case # lower case to snake_case 'foo_bar' >>> CaseStyle('foobar').snake_case # lowercase to snake_case 'foobar' >>> CaseStyle('FOO BAR').snake_case # UPPER CASE to snake_case 'foo_bar' >>> CaseStyle('FOOBAR').snake_case # UPPERCASE to snake_case 'foobar' >>> CaseStyle('Foo Bar').snake_case # Title Case to snake_case 'foo_bar' >>> CaseStyle('foo_bar').snake_case # snake_case to snake_case 'foo_bar' >>> CaseStyle('foo-bar').snake_case # kebap-case to snake_case 'foo_bar' >>> CaseStyle('FooBar').snake_case # PascalCase to snake_case 'foo_bar' >>> CaseStyle('fooBar').snake_case # camelCase to snake_case 'foo_bar'
- property title_case
The title case style of the string.
- Returns:
The title case
- Return type:
str
Hint
Examples
>>> CaseStyle('foo bar').title_case # lower case to Title Case 'Foo Bar' >>> CaseStyle('foobar').title_case # lowercase to Title Case 'Foobar' >>> CaseStyle('FOO BAR').title_case # UPPER CASE to Title Case 'Foo Bar' >>> CaseStyle('FOOBAR').title_case # UPPERCASE to Title Case 'Foobar' >>> CaseStyle('Foo Bar').title_case # Title Case to Title Case 'Foo Bar' >>> CaseStyle('foo_bar').title_case # snake_case to Title Case 'Foo Bar' >>> CaseStyle('foo-bar').title_case # kebap-case to Title Case 'Foo Bar' >>> CaseStyle('FooBar').title_case # PascalCase to Title Case 'Foo Bar' >>> CaseStyle('fooBar').title_case # camelCase to Title Case 'Foo Bar' >>> CaseStyle('FOO BAR').title_case # UPPER CASE to Title Case 'Foo Bar'
IntegerEncoder
- class confirm.utils.string.IntegerEncoder(charset='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
A string class which can be used to encode & decode integers into strings.
- Parameters:
charset (str) – The charset
Hint
In case there’s an intention to manually communicate the encoded integer string (like publishing the string in hard copies), it’s recommended to use the
SAFE_CHARSET
rather than theDEFAULT_CHARSET
. This will avoid confusion due to the fact that similar looking characters are already eliminated. Also, because there are only uppercase characters contained in theSAFE_CHARSET
, the input string can easily be uppercase’d, so that it’s becoming case-insensitive.- DEFAULT_CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
The default charset, containing lower- and uppercase alphanumeric letters.
- SAFE_CHARSET = '379CDEFHJKLMNPQRTUVWXY'
A charset which only contains non-similar looking uppercase alphanumeric characters.
- decode(string)
Decode an encoded string into an integer.
- Parameters:
string (str) – The encoded string
- Returns:
The decoded integer
- Return type:
int
- Raises:
ValueError – When an invalid number is provided
Hint
Examples with the
DEFAULT_CHARSET
:>>> IntegerEncoder().decode('0') 0 >>> IntegerEncoder().decode('1') 1 >>> IntegerEncoder().decode('1C') 100 >>> IntegerEncoder().decode('q0U') 100000 >>> IntegerEncoder().decode('6LAze') 100000000
Examples with the
SAFE_CHARSET
:>>> IntegerEncoder(IntegerEncoder.SAFE_CHARSET).decode('3') 0 >>> IntegerEncoder(IntegerEncoder.SAFE_CHARSET).decode('7') 1 >>> IntegerEncoder(IntegerEncoder.SAFE_CHARSET).decode('DN') 100 >>> IntegerEncoder(IntegerEncoder.SAFE_CHARSET).decode('KJPL') 100000 >>> IntegerEncoder(IntegerEncoder.SAFE_CHARSET).decode('WJWKNN') 100000000
Examples with an alternative charset
ABCDEF
:>>> IntegerEncoder('ABCDEF').decode('A') 0 >>> IntegerEncoder('ABCDEF').decode('B') 1 >>> IntegerEncoder('ABCDEF').decode('CEE') 100 >>> IntegerEncoder('ABCDEF').decode('CAFAFEE') 100000 >>> IntegerEncoder('ABCDEF').decode('BDFDBCACFEE') 100000000
When providing an invalid character, a
ValueError
is raised:>>> IntegerEncoder('ABCDEF').decode('ABCXDEF') Traceback (most recent call last): ... ValueError: Invalid character "X" in "ABCXDEF" string
- encode(number)
Encode an number into a string.
- Parameters:
number (int or str) – The number
- Returns:
The encoded string
- Return type:
str
- Raises:
ValueError – When an invalid number is provided
Hint
Examples with the
DEFAULT_CHARSET
:>>> IntegerEncoder().encode(0) '0' >>> IntegerEncoder().encode(1) '1' >>> IntegerEncoder().encode(100) '1C' >>> IntegerEncoder().encode(100000) 'q0U' >>> IntegerEncoder().encode(100000000) '6LAze'
Examples with the
SAFE_CHARSET
:>>> IntegerEncoder(IntegerEncoder.SAFE_CHARSET).encode(0) '3' >>> IntegerEncoder(IntegerEncoder.SAFE_CHARSET).encode(1) '7' >>> IntegerEncoder(IntegerEncoder.SAFE_CHARSET).encode(100) 'DN' >>> IntegerEncoder(IntegerEncoder.SAFE_CHARSET).encode(100000) 'KJPL' >>> IntegerEncoder(IntegerEncoder.SAFE_CHARSET).encode(100000000) 'WJWKNN'
Examples with an alternative charset
ABCDEF
:>>> IntegerEncoder('ABCDEF').encode(0) 'A' >>> IntegerEncoder('ABCDEF').encode(1) 'B' >>> IntegerEncoder('ABCDEF').encode(100) 'CEE' >>> IntegerEncoder('ABCDEF').encode(100000) 'CAFAFEE' >>> IntegerEncoder('ABCDEF').encode(100000000) 'BDFDBCACFEE'
Using hexadecimal numbers will work:
>>> IntegerEncoder().encode(0x5f5e100) '6LAze'
Stringified integers will also work:
>>> IntegerEncoder().encode('100000000') '6LAze'
Even stringified hexadecimal numbers will work:
>>> IntegerEncoder().encode('0x5f5e100') '6LAze'
When providing an invalid value, a
ValueError
is raised:>>> IntegerEncoder().encode('invalid') Traceback (most recent call last): ... ValueError: invalid literal for int() with base 10: 'invalid'
truncate
- confirm.utils.string.truncate(string, length=50, break_words=False, ellipsis='…')
Truncate a string to a maximum length of
length
characters.- Parameters:
string (str) – The string
length (int) – The desired length
break_words (bool) – Shall words be broken
ellipsis (str) – The ellipsis character(s) to add
- Returns:
The truncated string
- Return type:
str
Hint
A short string:
>>> short_string = 'Lorem ipsum dolor' >>> truncate(short_string) 'Lorem ipsum dolor'
A long string:
>>> long_string = '(Lorem ipsum) dolor sit amet, consectetur adipiscing elit.' >>> truncate(long_string) '(Lorem ipsum) dolor sit amet, consectetur…'
Advanced option usage:
>>> truncate(long_string, length=15) '(Lorem ipsum)…' >>> truncate(long_string, break_words=True) '(Lorem ipsum) dolor sit amet, consectetur adipisci…' >>> truncate(long_string, ellipsis=' etc.') '(Lorem ipsum) dolor sit amet, consectetur etc.'
Using a combination of options:
>>> truncate(long_string, length=15, break_words=True) '(Lorem ipsum) d…' >>> truncate(long_string, length=15, ellipsis=' etc.') '(Lorem ipsum) etc.' >>> truncate(long_string, break_words=True, ellipsis=' etc.') '(Lorem ipsum) dolor sit amet, consectetur adipisci etc.' >>> truncate(long_string, length=15, break_words=True, ellipsis=' etc.') '(Lorem ipsum) d etc.'