Exemple #1
0
def frames_to_hyper(
    dict_of_frames: Dict[pantab_types.TableType, pd.DataFrame],
    database: Union[str, pathlib.Path],
    table_mode: str = "w",
    *,
    hyper_process: Optional[tab_api.HyperProcess] = None,
) -> None:
    """See api.rst for documentation."""
    _validate_table_mode(table_mode)

    with ensure_hyper_process(hyper_process) as hpe:
        tmp_db = pathlib.Path(tempfile.gettempdir()) / f"{uuid.uuid4()}.hyper"

        if table_mode == "a" and pathlib.Path(database).exists():
            shutil.copy(database, tmp_db)

        with tab_api.Connection(
                hpe.endpoint, tmp_db,
                tab_api.CreateMode.CREATE_IF_NOT_EXISTS) as connection:
            for table, df in dict_of_frames.items():
                _insert_frame(df,
                              connection=connection,
                              table=table,
                              table_mode=table_mode)

        # In Python 3.9+ we can just pass the path object, but due to bpo 32689
        # and subsequent typeshed changes it is easier to just pass as str for now
        shutil.move(str(tmp_db), database)
Exemple #2
0
def frames_from_hyper(
    source: Union[str, pathlib.Path, tab_api.Connection],
    *,
    hyper_process: Optional[tab_api.HyperProcess] = None,
) -> Dict[tab_api.TableName, pd.DataFrame]:
    """See api.rst for documentation."""
    result: Dict[TableType, pd.DataFrame] = {}

    if isinstance(source, tab_api.Connection):
        forbid_hyper_process(hyper_process)
        connection = source
        for schema in connection.catalog.get_schema_names():
            for table in connection.catalog.get_table_names(schema=schema):
                result[table] = _read_table(connection=connection, table=table)
    else:
        with tempfile.TemporaryDirectory() as tmp_dir, ensure_hyper_process(
                hyper_process) as hpe:
            tmp_db = shutil.copy(source, tmp_dir)
            with tab_api.Connection(hpe.endpoint, tmp_db) as connection:
                for schema in connection.catalog.get_schema_names():
                    for table in connection.catalog.get_table_names(
                            schema=schema):
                        result[table] = _read_table(connection=connection,
                                                    table=table)

    return result
Exemple #3
0
def frame_from_hyper(
    database: Union[str, pathlib.Path],
    *,
    table: TableType,
    hyper_process: Optional[tab_api.HyperProcess] = None,
) -> pd.DataFrame:
    """See api.rst for documentation"""

    with tempfile.TemporaryDirectory() as tmp_dir, ensure_hyper_process(
            hyper_process) as hpe:
        tmp_db = shutil.copy(database, tmp_dir)
        with tab_api.Connection(hpe.endpoint, tmp_db) as connection:
            return _read_table(connection=connection, table=table)
Exemple #4
0
def frame_from_hyper_query(
    database: Union[str, pathlib.Path],
    query: str,
    *,
    hyper: Optional[tab_api.HyperProcess] = None,
) -> pd.DataFrame:
    """See api.rst for documentation."""
    with tempfile.TemporaryDirectory() as tmp_dir, ensure_hyper_process(
            hyper) as hpe:
        tmp_db = shutil.copy(database, tmp_dir)
        with tab_api.Connection(hpe.endpoint, tmp_db) as connection:
            with connection.execute_query(query) as result:
                return _read_query_result(result, None)