Module API - Cast
superconf.casts
Support value casting
AbstractCast
Base class for all cast operations.
This abstract class defines the interface that all cast implementations must follow. Subclasses must implement the call method to perform the actual casting operation.
Source code in superconf/casts.py
AsBest
Bases: AbstractCast
Cast a value to its most appropriate Python type.
Attempts to intelligently determine and convert the input value to the most suitable Python type by trying different conversions in order: 1. None for null/empty values 2. Boolean for true/false-like strings 3. Integer for numeric strings without decimals 4. Float for numeric strings with decimals 5. List for comma-separated strings or sequence-like inputs 6. Dict for mapping-like inputs 7. Original string if no other conversion succeeds
Examples:
>>> cast = AsBest()
>>> cast('123') # Returns: 123 (int)
>>> cast('3.14') # Returns: 3.14 (float)
>>> cast('true') # Returns: True (bool)
>>> cast('a,b,c') # Returns: ['a', 'b', 'c'] (list)
Source code in superconf/casts.py
AsBoolean
Bases: AbstractCast
Cast a value to a boolean using predefined string mappings.
Converts various string representations to boolean values using a configurable mapping dictionary. By default, supports common boolean string representations like 'true', 'yes', 'on', '1' for True and their counterparts for False.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
dict
|
A dictionary mapping strings to boolean values. If provided, updates the default mapping dictionary. |
None
|
Raises:
Type | Description |
---|---|
InvalidCastConfiguration
|
If the input value cannot be cast to a boolean. |
Source code in superconf/casts.py
AsDict
Bases: AbstractCast
Cast a value to a dictionary.
Currently supports: - Empty values become empty dictionaries - Mapping objects are converted directly to dictionaries
Parameters:
Name | Type | Description | Default |
---|---|---|---|
delimiter
|
str
|
Reserved for future string parsing. Defaults to ','. |
','
|
quotes
|
str
|
Reserved for future string parsing. Defaults to '"''. |
'"\''
|
Note
String parsing is not yet implemented.
Source code in superconf/casts.py
AsIdentity
Bases: AbstractCast
Return the input value unchanged.
A no-operation cast that simply returns the input value without modification. Useful as a default cast or when you need to maintain the original type.
Source code in superconf/casts.py
AsInt
Bases: AbstractCast
Cast a value to an integer.
Attempts to convert the input value to an integer using Python's built-in int() function.
Raises:
Type | Description |
---|---|
InvalidCastConfiguration
|
If the value cannot be converted to an integer. |
Source code in superconf/casts.py
AsList
Bases: AbstractCast
Cast a value to a list with support for delimited strings.
Converts various input types to a list: - Empty values become empty lists - Strings are split by delimiter, with support for quoted elements - Sequences are converted directly to lists
Parameters:
Name | Type | Description | Default |
---|---|---|---|
delimiter
|
str
|
The character used to split strings. Defaults to ','. |
','
|
quotes
|
str
|
String containing valid quote characters. Defaults to '"''. |
'"\''
|
Examples:
>>> cast = AsList()
>>> cast('a,b,c') # Returns: ['a', 'b', 'c']
>>> cast('"a,b",c') # Returns: ['a,b', 'c']
>>> cast(['a', 'b']) # Returns: ['a', 'b']
Source code in superconf/casts.py
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 |
|
AsOption
Bases: AbstractCast
Cast a value by selecting from predefined options.
Maps input values to predefined options using a dictionary mapping. Optionally supports a default option when the input doesn't match any defined option.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
options
|
dict
|
A dictionary mapping input values to their corresponding options. |
required |
default_option
|
any
|
The key to use when the input value isn't found. If FAIL (default), raises an exception for invalid inputs. |
FAIL
|
Raises:
Type | Description |
---|---|
InvalidCastConfiguration
|
If the input value is not in options and no valid default_option is provided. |
Example
cast = AsOption({'dev': ['debug'], 'prod': ['optimize']}, 'dev') cast('prod') # Returns: ['optimize'] cast('invalid') # Returns: ['debug'] (default option)
Source code in superconf/casts.py
AsString
Bases: AbstractCast
Cast a value to a string.
Attempts to convert the input value to a string using Python's built-in str() function.
Raises:
Type | Description |
---|---|
InvalidCastConfiguration
|
If the value cannot be converted to a string. |
Source code in superconf/casts.py
AsTuple
Bases: AsList
Cast a value to a tuple.
Inherits from AsList but converts the final result to a tuple instead of a list. Accepts the same arguments and follows the same parsing rules as AsList.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
delimiter
|
str
|
The character used to split strings. Defaults to ','. |
','
|
quotes
|
str
|
String containing valid quote characters. Defaults to '"''. |
'"\''
|