acept.acept_utils

Module containing utility functions for ACEPT

Module Contents

Functions

uppath(→ str)

Climb directory path upwards

absolute_path_from_relative_posix(→ str)

Returns the absolute path to the given relative POSIX path to a file or directory (relative to /src/acept)

derive_output_path_from_filepath(→ str)

Derives output file path for modified file from input file path

copy_file_or_directory_recursively(→ str)

Copy file or directories including subdirectories and files recursively. The copying operation will continue

rename_files_in_directory(path, old_substring, ...)

Replace a sub string of all files names in a directory

delete_files_or_directory_recursively_with_pattern(...)

Deletes all files with the given pattern in the given directory

combine_csv_profiles_with_pattern(→ pandas.DataFrame)

Combines all csv files with the given pattern in the given directory into a single csv file.

concat_csv_profiles_columnwise(file_1, file_2, output_file)

Appends file_2 to file_1 column-wise and writes the result to the CSV file output_file.

get_bid_from_uhp_building_specific_files(→ int)

Key function to extract the number after the last "_" in the filename for sorting.

acept.acept_utils.uppath(filepath: str, n: int) str[source]

Climb directory path upwards

Parameters:
  • filepath (str) – Path to a file or directory

  • n (int) – Number of path levels to climb upwards

Returns:

Path to the directory n levels upwards

Return type:

str

Example

>>> uppath("/parent/temp/dir/file.txt", 1)
'/parent/temp/dir'
acept.acept_utils.absolute_path_from_relative_posix(posix_relative_to_src_files: str) str[source]

Returns the absolute path to the given relative POSIX path to a file or directory (relative to /src/acept)

Example

>>> absolute_path_from_relative_posix("../../data/plz/plz-5stellig.shp")
'path/to/the/acept/repository/data/plz/plz-5stellig.shp'
Parameters:

posix_relative_to_src_files (str) – Path to a file or directory, given as a POSIX path, relative to /src/acept.

Returns:

Absolute path to the given path relative_to_src_files

Return type:

str

acept.acept_utils.derive_output_path_from_filepath(output_base: str, filename: str, file_extension, mod_filename_suffix='_mod', up=1) str[source]

Derives output file path for modified file from input file path

Parameters:
  • output_base (str) – base directory path for modified files

  • filename (str) – path of input file

  • file_extension – filename extension of input file e.g. ‘.shp’

  • mod_filename_suffix – suffix for the modified filename before the filename extension. Default: ‘_mod’

  • up – path levels above file. Default: 1 = Parent directory of input file

Returns:

output path of modified shape file

Return type:

str

acept.acept_utils.copy_file_or_directory_recursively(src_path: str, dst_path: str) str[source]

Copy file or directories including subdirectories and files recursively. The copying operation will continue if it encounters existing directories, and files within the dst tree will be overwritten by corresponding files from the src tree.

Parameters:
  • src_path (str) – Source path of the file or directory to copy

  • dst_path (str) – Destination path.

Return type:

str

acept.acept_utils.rename_files_in_directory(path: str, old_substring: str, new_substring: str)[source]

Replace a sub string of all files names in a directory

Parameters:
  • path (str) – Path to the directory with the files.

  • old_substring (str) – Substring in filename to replace.

  • new_substring (str) – Substring to replace the old_substring with.

acept.acept_utils.delete_files_or_directory_recursively_with_pattern(directory: str, pattern: str)[source]

Deletes all files with the given pattern in the given directory

Parameters:
  • directory (str) – Path to the directory with the files

  • pattern (str) – Filename pattern of the files to delete

acept.acept_utils.combine_csv_profiles_with_pattern(src_directory: str, pattern: str, csv_profiles_path: str, new_header: list = None, key_function: callable = None, skip_rows: int = 0, column_name: str = None, in_delimiter: str = ';', debug: bool = False) pandas.DataFrame[source]

Combines all csv files with the given pattern in the given directory into a single csv file.

Parameters:
  • src_directory (str) – Path to the directory with the csv files

  • pattern (str) – Filename pattern of the csv files to combine

  • csv_profiles_path (str) – Path to the csv file to write the combined profile to

  • new_header (list) – New header for the combined profile

  • key_function (callable) – Function to extract the number after the last “_” in the filename for sorting. If None, the files are sorted alphabetically.

  • skip_rows (int) – Number of rows to skip in the csv files

  • column_name (str) – Name of the column to extract from the csv files

  • in_delimiter (str) – Delimiter of the input csv files

  • debug (bool) – Print debug information

Returns:

Combined profile as pandas dataframe

Return type:

pandas.DataFrame

acept.acept_utils.concat_csv_profiles_columnwise(file_1: str, file_2: str, output_file: str, in_delimiter_1: str = ';', in_delimiter_2: str = ';', out_delimiter: str = ';', add_index: bool = True)[source]

Appends file_2 to file_1 column-wise and writes the result to the CSV file output_file.

Parameters:
  • file_1 (str) – Path to the first csv file

  • file_2 (str) – Path to the second csv file

  • output_file (str) – Path to the output csv file

  • in_delimiter_1 (str) – Delimiter of the first csv file

  • in_delimiter_2 (str) – Delimiter of the second csv file

  • out_delimiter (str) – Delimiter of the output csv file

  • add_index (bool) – Add an index to the output csv file

acept.acept_utils.get_bid_from_uhp_building_specific_files(filename: str) int[source]

Key function to extract the number after the last “_” in the filename for sorting.

Parameters:

filename (str) – Filename

Returns:

Number after the last “_”

Return type:

int