ts_ids_core.mapping_table.mapping_table module

Model Column[source]

Bases: BaseModel

Class for columns of the mapping table header

Show JSON schema
{
   "title": "Column",
   "description": "Class for columns of the mapping table header",
   "type": "object",
   "properties": {
      "key": {
         "title": "Key",
         "type": "string"
      },
      "column_name": {
         "title": "Column Name",
         "type": "string"
      }
   },
   "required": [
      "key",
      "column_name"
   ]
}

field column_name: str
field key: str
Model MappingTable[source]

Bases: BaseModel

Fields and export methods for mapping table data.

The header contains data related to the columns of the mapping table. The body contains data related to the rows of the mapping table (see the Record class).

Show JSON schema
{
   "title": "MappingTable",
   "description": "Fields and export methods for mapping table data.\n\nThe header contains data related to the columns of the mapping table.\nThe body contains data related to the rows of the mapping table (see the `Record`\nclass).",
   "type": "object",
   "properties": {
      "header": {
         "items": {
            "$ref": "#/$defs/Column"
         },
         "minItems": 1,
         "title": "Header",
         "type": "array"
      },
      "body": {
         "items": {
            "$ref": "#/$defs/Record"
         },
         "title": "Body",
         "type": "array"
      }
   },
   "$defs": {
      "Column": {
         "description": "Class for columns of the mapping table header",
         "properties": {
            "key": {
               "title": "Key",
               "type": "string"
            },
            "column_name": {
               "title": "Column Name",
               "type": "string"
            }
         },
         "required": [
            "key",
            "column_name"
         ],
         "title": "Column",
         "type": "object"
      },
      "Record": {
         "additionalProperties": true,
         "description": "Class for a record in the mapping table body.\n\nEach record has two required fields: `path` & `visible`.\n\nExtra fields are allowed, but validated to contain strings.",
         "properties": {
            "path": {
               "title": "Path",
               "type": "string"
            },
            "visible": {
               "default": true,
               "title": "Visible",
               "type": "boolean"
            }
         },
         "required": [
            "path"
         ],
         "title": "Record",
         "type": "object"
      }
   }
}

Validators:
field body: List[Record] [Optional]
field header: Annotated[List[Column], MinLen(min_length=1)] [Optional]
Constraints:
  • min_length = 1

body_to_dataframe()[source]

Export the body to a pandas dataframe.

The dataframe column order is path, visible, then any extra keys in the order they appear in the header.

get_deindexed_path_to_records_map() Dict[str, List[Record]][source]

Construct mapping from deindexed paths to mapping table records.

validator path_is_first_column  »  all fields[source]

Check the first Column.key in the header is “path”

Validates:
  • all fields

property extra_keys: List[str]

Keys in the header that are not defined in the Record class

property key_to_column_map: Dict[str, str]

Mapping from keys to column names for columns in the header

property keys: List[str]

Keys in the header

Model Record[source]

Bases: BaseModel

Class for a record in the mapping table body.

Each record has two required fields: path & visible.

Extra fields are allowed, but validated to contain strings.

Show JSON schema
{
   "title": "Record",
   "description": "Class for a record in the mapping table body.\n\nEach record has two required fields: `path` & `visible`.\n\nExtra fields are allowed, but validated to contain strings.",
   "type": "object",
   "properties": {
      "path": {
         "title": "Path",
         "type": "string"
      },
      "visible": {
         "default": true,
         "title": "Visible",
         "type": "boolean"
      }
   },
   "additionalProperties": true,
   "required": [
      "path"
   ]
}

Validators:
field path: str
field visible: bool
validator extra_field_validator  »  all fields[source]

Check that extra fields contain strings

Validates:
  • all fields

classmethod from_dict(record_dict: Dict[str, Any]) Record[source]

Create a Record instance from a dictionary

Pydantic v2 preserves the insertion order of extra keys when they’re all passed to the constructor at once, so unlike Pydantic v1, there’s no need to set them one by one via setattr after construction.

construct_body_from_dataframe(keys: List[str], dataframe: DataFrame) List[Record][source]

Construct the body array from a pandas dataframe Arguments:

keys – keys of the columns in the mapping table dataframe – the table

construct_header_from_columns(column_names: List[str]) List[Column][source]

Construct the header array from a list of column names Arguments:

column_names – List of column names

construct_mapping_table_body_from_paths(schema_paths: List[str], mapping_table: MappingTable, default_record_visibility: bool = True) List[Record][source]

Create the body of the mapping table.

Arguments:

schema_paths – List of schema field_paths mapping_table – MappingTable instance default_record_visibility – Visibility of new records in the body

create_record_from_existing_record(existing_record: Dict, mapping_table: MappingTable) Record[source]

Create a new record from an existing record. This function doesn’t just copy the existing record because, for instance, the mapping table may have new columns that are not in the previous existing record.

Parameters:
  • existing_record – existing record as a dictionary

  • mapping_table – MappingTable instance corresponding to mapping_table.json

Returns:

new record corresponding to the existing record

default_header() List[Column][source]

The default MappingTable header

sort_records(records: List[Record]) List[Record][source]

Sort records by indices in the path. Here are some example inputs and outputs: input –> output [

Record(path=”a[0].b[0].c”, visible=True), Record(path=”a[0].b[1].c”, visible=True), Record(path=”a[*].b[1].c”, visible=True),

] –> [

Record(path=”a[0].b[0].c”, visible=True), Record(path=”a[0].b[1].c”, visible=True), Record(path=”a[*].b[1].c”, visible=True),

]

sync_mapping_table_with_schema(schema: dict, original_mapping_table: MappingTable, default_record_visibility: bool = True) MappingTable[source]

Sync a MappingTable instance with the paths in an IDS Arguments:

schema – IDS json object original_mapping_table – MappingTable instance

Keyword Arguments:

default_record_visibility – Visibility of new records in the body

to_snakecase(string: str) str[source]

Convert a string to snake case