Module aws_lambda_powertools.shared.functions

Functions

def abs_lambda_path(relative_path: str = '') ‑> str

Return the absolute path from the given relative path to lambda handler.

Parameters

relative_path : str, optional
The relative path to the lambda handler, by default an empty string.

Returns

str
The absolute path generated from the given relative path. If the environment variable LAMBDA_TASK_ROOT is set, it will use that value. Otherwise, it will use the current working directory. If the path is empty, it will return the current working directory.
def base64_decode(value: str) ‑> bytes
def bytes_to_base64_string(value: bytes) ‑> str
def bytes_to_string(value: bytes) ‑> str
def dataclass_to_dict(data) ‑> dict

Dump standard dataclass as dict.

Note we use lazy import to prevent bloating other code parts.

Parameters

data : dataclass
Dataclass

Returns

dict:
Pydantic model serialized to dict
def extract_event_from_common_models(data: Any) ‑> Union[Dict, Any]

Extract raw event from common types used in Powertools

If event cannot be extracted, return received data as is.

Common models:

- Event Source Data Classes (DictWrapper)
- Python Dataclasses
- Pydantic Models (BaseModel)

Parameters

data : Any
Original event, a potential instance of DictWrapper/BaseModel/Dataclass

Notes

Why not using static type for function argument?

DictWrapper would cause a circular import. Pydantic BaseModel could cause a ModuleNotFound or trigger init reflection worsening cold start.

def is_dataclass(data) ‑> bool

Whether data is a dataclass

Parameters

data : dataclass
Dataclass obj

Returns

bool
Whether it's a Dataclass
def is_pydantic(data) ‑> bool

Whether data is a Pydantic model by checking common field available in v1/v2

Parameters

data : BaseModel
Pydantic model

Returns

bool
Whether it's a Pydantic model
def powertools_debug_is_set() ‑> bool
def powertools_dev_is_set() ‑> bool
def pydantic_to_dict(data) ‑> dict

Dump Pydantic model v1 and v2 as dict.

Note we use lazy import since Pydantic is an optional dependency.

Parameters

data : BaseModel
Pydantic model

Returns

dict:
Pydantic model serialized to dict
def resolve_env_var_choice(env: Optional[str] = None, choice: Optional[Union[str, float]] = None) ‑> Union[str, float, ForwardRef(None)]

Pick explicit choice over env, if available, otherwise return env value received

NOTE: Environment variable should be resolved by the caller.

Parameters

env : str, Optional
environment variable actual value
choice : str|float, optional
explicit choice

Returns

choice : str, Optional
resolved choice as either bool or environment value
def resolve_max_age(env: str, choice: Optional[int]) ‑> int

Resolve max age value

def resolve_truthy_env_var_choice(env: str, choice: Optional[bool] = None) ‑> bool

Pick explicit choice over truthy env value, if available, otherwise return truthy env value

NOTE: Environment variable should be resolved by the caller.

Parameters

env : str
environment variable actual value
choice : bool
explicit choice

Returns

choice : str
resolved choice as either bool or environment value
def sanitize_xray_segment_name(name: str) ‑> str
def slice_dictionary(data: Dict, chunk_size: int) ‑> Generator[Dict, None, None]
def strtobool(value: str) ‑> bool

Convert a string representation of truth to True or False.

True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if 'value' is anything else.

note:: Copied from distutils.util.