Skip to main content

OeCommon

Project description

OE Common

Simple module with some functions such as generate password (get_random_string), fix unicode strings, size converter, dynamic console, read/write speed checker, etc.

Prerequisites

- Python3.7 or newer

Installion

$ python3 -m pip install oe_common

OR

$ python3 -m pip install git+https://github.com/jok4r/oe_common.git

Usage / Eng

Functions

fix_block_encoding_errors(text) - Decode block of text from bytes. In some cases, .decode() can raise exception (bad encoding, etc.). This function resolves the problem. Unreaded symbols will be replaced by "?" symbol.

fix_unicode_string(line) - Its the same function as above, but do this only for one line.

convert_size(size_bytes, prefix='B') - Convert input value to human readable format (KB, MB, GB, etc.).

get_rnd_string(length=20, complexity=None) - Generates random string (password) with given size. If complexity is True, password will be strength, with 2 letters uppercase, 3 letters lowercase and 2 digits (minimum).

generate_password - Alias of function above.

check_create_dir(*filename) - Almost the same as os.makedirs, but input value is full path to filename. Can specify multiple values: check_create_dir(file1, file2)

replace_string_in_file(path, regex, replaced) - Search and replace string in file. Example: replace_string_in_file("file.txt", r'string', r'replaced')

get_directory_size(path) - Get size of specified directory. This function uses C++ module for work faster. If directory is not exists, not raises an exeption, just return zero size.

Classes

Logger

Simple logger, can write output to console and file. In begin of log string, adds current datetime. Argument file - path to log file (will be created if not exists).

  • Logger(to_console=True, file=None)
    • log(*data) - Write to log. Can input multiple values: logger.log('foo', 'bar')

DinConsole / Dynamic Console

With this object, you can print text to console, then update it. Can be used to print progress bar, or another data, which can be updated (files copy, etc.)

  • DinConsole()
    • update(text) - Update text in console.
    • stay() - Stay text in console. Usually used before printing another data, which not need to be updated.
    • clear() - Clear the current string in console.

Speed Checker

Check read/write speed.

  • SpeedChecker(name, bts)
    • get_speed() - Every time when call this method, class update current read/write speed and return it. Example: we reading file by part, block size is 1024. In loop we can add get_speed() method, to run every cycle.

Usage / Rus

Функции

fix_block_encoding_errors(text) - В некоторых случаях обычный .decode() может выдать исключение, из-за каких-то некорректных символов и т.д. Данная функция устраняет эту проблему. В случае нечитаемых символов, выдает вместо них знаки вопроса.

fix_unicode_string(line) - То же самое что функция выше, но делает это только для одной строки. Если сюда подать целый блок текста, то обработка займет больше времени, чем у функции выше.

convert_size(size_bytes, prefix='B') - Конвертирует входящее значение в более удобно читаемое. Например оно конвертирует 1000000 Байт в 976 Кбайт. Если число еще больше, то будут мегабайты и т.д.

get_rnd_string(length=20, complexity=None) - Генерирует случайную строку (пароль) заданной длины. В случае если установить complexity=True, будет генерировать более сложный пароль, который обязательно должен включать 2 буквы верхнего регистра, 3 нижнего и 2 цифры.

generate_password - То же самое что функция выше, отличается только название (для удобства при написании скриптов).

check_create_dir(*filename) - Функционал аналогичен os.makedirs, но на вход мы подаем путь до создаваемого файла, и путь до него будет создан. На вход можно подавать сразу несколько значений: check_create_dir(file1, file2)

replace_string_in_file(path, regex, replaced) - Поиск и замена строки в файле. Пример: replace_string_in_file("file.txt", r'string', r'replaced')

get_directory_size(path) - Получить размер директории (папки). Выполняется с помощью модуля на C++ для ускорения работы. В случае если папка не существует, не выдает исключение, выдает нулевой размер.

Классы

Logger

Простой логгер, может писать вывод в консоль и в файл. В начале строки лога пишет текущую дату и время. Аргумент file - путь до файла, в который необходимо писать лог.

  • Logger(to_console=True, file=None)
    • log(*data) - Записать лог. На вход можно подавать сразу несколько значений: logger.log('foo', 'bar')

DinConsole / Dynamic Console

Создает возможность вывода в консоль строки, с возможностью обновления этой строки. Таким образом можно выводить прогресс в процентах и прочее, чтобы не спамить кучу текста в консоль.

  • DinConsole()
    • update(text) - Обновить текст в консоли.
    • stay() - Оставить текст в консоли, чтобы он не стирался. Обычно используется в конце работы класса.
    • clear() - Очистить консоль.

Speed Checker

Позволяет замерить скорость чтения/записи.

  • SpeedChecker(name, bts)
    • get_speed() - Каждый раз вызывая этот метод, обновляется скорость передачи и возвращает текущую скорость.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oe_common-1.2.6.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oe_common-1.2.6-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file oe_common-1.2.6.tar.gz.

File metadata

  • Download URL: oe_common-1.2.6.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for oe_common-1.2.6.tar.gz
Algorithm Hash digest
SHA256 8fb973ca4cdef40ff3ea6c0e9d123dd64bffb5c726b7f87940282fc118acc317
MD5 e7f060cfd3d206bd7283dec21869f843
BLAKE2b-256 b10bc1ff799e09b2d3fe1ad8b0e7bce6851e6aaa02f063a3c5348276ad139393

See more details on using hashes here.

File details

Details for the file oe_common-1.2.6-py3-none-any.whl.

File metadata

  • Download URL: oe_common-1.2.6-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for oe_common-1.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0eba03f28a7d7dab56ecceecc53756f677691bdb7d5e4c6ebc64645190a03520
MD5 b1fed2bfa6e390e62f2b06d820ac6b2d
BLAKE2b-256 c2972cfe628486bdae3951207918cf3bd2d73730157bb10c130d0b937543ef0a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page