Utils#

Common utility functions for data manipulation, string operations, and type conversions.

Quick Examples#

String Utilities#

from core_mixins.utils import to_snake_case, random_string, to_one_line

# Convert camelCase / PascalCase / acronyms to snake_case
result = to_snake_case("myVariableName")   # "my_variable_name"
result = to_snake_case("HTTPSRequest")     # "https_request"
result = to_snake_case("parseHTTPSUrl")    # "parse_https_url"

# Generate random string
token = random_string(length=32)

# Convert multiline to single line
text = """Line 1
Line 2
Line 3"""
single = to_one_line(text)  # "Line 1 Line 2 Line 3"

Dictionary Utilities#

from core_mixins.utils import (
    remove_attributes,
    rename_attributes,
    add_attributes,
    flatten_json
)

data = {"name": "John", "age": 30, "email": "john@example.com"}

# Remove attributes
remove_attributes(data, ["email"])

# Rename attributes
rename_attributes(data, {"age": "years"})

# Add missing attributes
add_attributes(data, ["phone", "address"])

# Flatten nested JSON
nested = {"user": {"name": "John", "details": {"age": 30}}}
flat = flatten_json(nested)  # {"user_name": "John", "user_details_age": 30}

List Processing#

from core_mixins.utils import get_batches

items = list(range(10))
for batch in get_batches(items, n=3):
    print(batch)
# [0, 1, 2]
# [3, 4, 5]
# [6, 7, 8]
# [9]

Data Type Conversion#

from core_mixins.utils import convert_data_type, bytes_to_str

# Convert types in dictionary
data = {"age": "30", "score": "95.5", "active": "true"}
convert_data_type(data, {"age": "int", "score": "float", "active": "bool"})
# data = {"age": 30, "score": 95.5, "active": True}

# Convert bytes to strings
byte_data = {b"key": b"value", b"nested": [b"a", b"b"]}
string_data = bytes_to_str(byte_data)
# {"key": "value", "nested": ["a", "b"]}

API Reference#

Common utility functions for data manipulation, string operations, and type conversions.

core_mixins.utils.random_string(vocabulary: str | None = None, length: int = 20, exclude: str | None = None) str[source]#

Create random string

core_mixins.utils.get_batches(it: Iterable[_T], n: int) Iterator[List[_T]][source]#

Divide an iterable into chunks of size n.

Parameters:
  • it – Iterable to divide.

  • n – Number of elements by chunk.

Returns:

core_mixins.utils.remove_attributes(record: Dict, attrs: List[str]) None[source]#

Remove attributes from a Dict

core_mixins.utils.rename_attributes(record: Dict, mapper: Dict[str, str]) None[source]#

Rename the object attributes using the column_mapper

core_mixins.utils.add_attributes(record: Dict, expected_attrs: List[str])[source]#

The function add to the dictionary the list of attributes (if not exists) expected using None…

Parameters:
  • record – Dictionary to update.

  • expected_attrs – Attributes to add.

Returns:

core_mixins.utils.convert_data_type(data: Dict[str, str], columns_type_mapper: Dict[str, Any]) None[source]#

Update (cast) the value depending on the specified type…

Parameters:
  • data – Dictionary to update.

  • columns_type_mapper – Dictionary that specify the data types.

core_mixins.utils.to_snake_case(string: str) str[source]#

It converts a string from camel-case to snake-case

core_mixins.utils.flatten_json(data: Dict, flatten_sublist: bool = False)[source]#

Utility function for flattening dictionary objects…

Parameters:
  • data – Object to flatten.

  • flatten_sublist – Set as True if you want to flatten sublist objects.

Returns:

Flatten data (dictionary).

core_mixins.utils.to_one_line(multiline_string: str) str[source]#

It converts a multiline string to a single line one

core_mixins.utils.bytes_to_str(data: List | Dict | bytes, encoding: str = 'utf-8')[source]#

Convert bytes to str in keys and values…

Example

{b”key”: b”value”}) -> {“key”: “value”} {b”key”: [1, 2, b”3”]} -> {“key”: [1, 2, “3”]} [1, 2, b”3”, {b”a”: b”a”}] -> [1, 2, “3”, {“a”: “a”}] b”test”) -> “test”