Module aws_lambda_powertools.shared.functions
Functions
-
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.
-
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
-
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.
-
Whether data is a dataclass
Parameters
data
:dataclass
- Dataclass obj
Returns
bool
- Whether it's a Dataclass
-
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
-
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
-
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
-
Resolve max age value
-
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
-
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.