Beispiel #1
0
def generate_records_from_df(
    df: "pd.DataFrame", record_info: RecordInfo
) -> Generator[RecordCreator, None, None]:
    """Generate record creators from a dataframe."""
    fill_df_nulls_with_blackbird_nulls(df)
    columns = list(df)
    field_map = {field.name: FieldProxy(field) for field in record_info}
    fields = [field_map[column_name] for column_name in columns]

    record_creator = record_info.construct_record_creator()

    col_range = range(len(fields))
    for row in df.itertuples():
        record_creator.reset()
        for col_idx in col_range:
            fields[col_idx].set(record_creator, row[col_idx + 1])

        yield record_creator
def build_ayx_record_from_list(
    values_list: List[Any],
    metadata_list: List[dict],
    record_info: sdk.RecordInfo,
    record_creator: Optional[sdk.RecordCreator] = None,
) -> Tuple[object, object]:
    """
    Build a record from a list of values.

    Takes a list of values that represents a single row of data, along with metadata
    and a blank or already populated Alteryx RecordInfo object, and returns a tuple
    containing a populated Alteryx RecordRef object and an already initialized
    RecordCreator object.
    The returned RecordCreator object can optionally be passed back into the function,
    allowing for improved performance when looping through a list of new values.

    Parameters
    ----------
    values_list : List[Any]
        A list of Python objects of any type that represents a single record of
        data.  The 0th index of the list represents data in the first column
        of the record, and so on.

    metadata_list : List[dict]
        (This might not be a list)
        A list of the names, types, sizes, sources, and descriptions
        for each respective column. These are used to generate
        the Alteryx RecordInfo object (if it doesn't already exist) for the names
        of each respective Field object.

    record_info : object
        An Alteryx RecordInfo object.
        Alteryx RecordInfo objects act as containers for the necessary metadata
        needed by the Alteryx engine to generate, identify, and manipulate
        each record of data passing through the tool.

    record_creator : Optional[object]
        An optional Alteryx RecordCreator object. The RecordCreator object is created
        by calling the construct_record_creator method on an Alteryx RecordInfo
        object.  It is a stateful object which is populated with values as a
        side-effect of this function.  When its finalize method is called, it
        returns an actual reference to the record's data, in the form of an
        Alteryx RecordRef object.
        If no record_creator object is passed into the function, one will be created
        using the record_info object.
        The function will automatically reset the record_creator if one is passed in.

    Returns
    -------
    Tuple(object, object)
        First value in tuple:
            Alteryx RecordRef object, with each Field populated with the respective
            values in the values_list parameter.
        Second value in tuple:
            Alteryx RecordCreator object.  If one was passed in as a parameter, it
            returns it after creating a record with it.
            If one is not passed in, it creates a new one from the RecordInfo param,
            uses it to create a record, and returns it.
    """
    columns = [
        Column(
            metadata_list[i].name,
            metadata_list[i].type,
            metadata_list[i].size,
            metadata_list[i].source,
            metadata_list[i].description,
            values_list[i],
        ) for i in range(len(metadata_list))
    ]

    if record_info.num_fields == 0:
        for column in columns:
            add_output_column_to_record_info(column, record_info)
    if record_creator:
        record_creator.reset()
    else:
        record_creator = record_info.construct_record_creator()

    for column in columns:
        field = record_info.get_field_by_name(column.name)
        set_field_value(field, column.value, record_creator)

    ayx_record = record_creator.finalize_record()

    return (ayx_record, record_creator)