Skip to content

CRUDAction

CRUDAction

Bases: CRUDBase[ActionScriptModel]

Create, Read, Update and Delete Field Models. Usually instantiated as part of a CrosswalkDefinition and accessed as .actions.

Base CRUD operations are common for both CRUDField and CRUDAction.

Example
import whyqd as qd

crosswalk = qd.CrosswalkDefinition()
crosswalk.set(schema_source=SCHEMA_SOURCE, schema_destination=SCHEMA_DESTINATION)
# Create the crosswalk
schema_scripts = [
    "DEBLANK",
    "DEDUPE",
    "DELETE_ROWS < [0, 1, 2, 3]",
    f"PIVOT_LONGER > ['year', 'values'] < {datasource.model[0].names[4:]}",
    "RENAME > 'indicator_code' < ['Indicator Code']",
    "RENAME > 'indicator_name' < ['Indicator Name']",
    "RENAME > 'country_code' < ['Country Code']",
    "RENAME > 'country_name' < ['Country Name']",
]
crosswalk.actions.add_multi(terms=schema_scripts)

Parameters:

Name Type Description Default
schema_source SchemaDefinition

A SchemaDefinition for access to source schema definitions and operations.

None
schema_destination SchemaDefinition

A SchemaDefinition for access to destination schema definitions and operations.

None

add(*, term)

Add the string term for an action script. Validate as well. Does not test for uniqueness.

Parameters:

Name Type Description Default
term str | ActionScriptModel

A string term, or ActionScriptModel conforming to the script structure for a specific action.

required

Raises:

Type Description
ValueError

If script is invalid. In most cases, you should get reasonable feedback from the error message as to how to fix the problem.

get(*, name)

Get a specific script from the list of scripts, called by a unique id.

Parameters:

Name Type Description Default
name str | UUID

Scripts may be duplicated, but UUIDs will be unique.

required

Returns:

Type Description
ActionScriptModel | None

An ActionScriptModel or None, of no such script is found.

get_action(*, script)

Return the first action term from a script as its Model type.

Parameters:

Name Type Description Default
script str

A string term conforming to the script structure for a specific action.

required

Raises:

Type Description
ValueError

If not a valid ACTION.

Returns:

Type Description
BaseSchemaAction | BaseMorphAction | BaseCategoryAction

The action model type conforming to the requirements for a script.

get_action_parser(*, script, action=None)

Return the ACTION parser for a script.

Parameters:

Name Type Description Default
script str

A string term conforming to the script structure for a specific action.

required
action BaseSchemaAction | BaseMorphAction | BaseCategoryAction

The action model type conforming to the requirements for a script.

None

Raises:

Type Description
ValueError

If the script can't be parsed.

Returns:

Type Description
ActionParser | MorphParser | CategoryParser

Parser for Action type, and used in TransformDefinition and CrosswalkDefinition.

parse(*, script)

Parse a script for any action, of any type, and return the corresponding transformation dictionary structure. Can also be used as a validation step.

Parameters:

Name Type Description Default
script str | ActionScriptModel

A string term, or ActionScriptModel conforming to the script structure for a specific action.

required

Raises:

Type Description
ValueError

If the script term is not valid, or if the schemas are not set.

Returns:

Type Description
dict
# A dictionary of the appropriate form for the ACTION
{
    "action": BaseSchemaAction,
    "destination": FieldModel,
    "source": list[ModifierModel | FieldModel]
}

{
    "action": BaseMorphAction,
    "destination": FieldModel | list[FieldModel],
    "source": list[FieldModel],
    "rows": list[int],
    "source_param": str,
    "destination_param": str
}

{
    "action": BaseCategoryAction,
    "destination": FieldModel,
    "category": CategoryModel,
    "source": list[FieldModel],
    "assigned": list[CategoryModel],
    "unassigned": list[CategoryModel],
}

remove(*, name)

Remove a specific term, called by a unique UUID.

Parameters:

Name Type Description Default
name str | UUID

Unique model UUID for the script.

required

set_schema(*, schema_source=None, schema_destination=None)

Set SchemaDefinitions.

Parameters:

Name Type Description Default
schema_source SchemaDefinition

A SchemaDefinition for access to source schema definitions and operations.

None
schema_destination SchemaDefinition

A SchemaDefinition for access to destination schema definitions and operations.

None

transform(*, df, script)

Return a transformed dataframe according to a single script.

Parameters:

Name Type Description Default
df DataFrame

A Pandas (Modin) dataframe.

required
script str | ActionScriptModel

A string term, or ActionScriptModel conforming to the script structure for a specific action.

required

Returns:

Type Description
DataFrame

Transformed dataframe.

transform_all(*, df)

Return a transformed dataframe after processing all scripts.

Parameters:

Name Type Description Default
df DataFrame

A Pandas (Modin) dataframe.

required

Returns:

Type Description
DataFrame

Transformed dataframe.

update(*, term)

Update the parameters for a specific term, called by a unique UUID. If the UUID does not exist, then this will raise a ValueError.

Parameters:

Name Type Description Default
term ActionScriptModel | dict

A dictionary conforming to the ActionScriptModel.

required

Raises:

Type Description
ValueError

If the term does not exist.

validate(*, required=False)

Return the list of destination schema fields which are still to be crosswalked.

Parameters:

Name Type Description Default
required bool

Limit the unreconciled crosswalk fields to only those which are required.

False

Returns:

Type Description
list[FieldModel]

List of FieldModels which are required but are still undefined by an ActionScriptModel.