Skip to main content

Common Python utilities: cache, captcha, date, eval, http, string, thread

Project description

common-utils

A collection of common Python utility modules: cache, captcha, date, safe expression evaluation, HTTP requests, string helpers, and threading helpers.

Install

From source (editable, recommended for development):

pip install -e .

From a built distribution:

pip install dist/common_utils-0.1.0-py3-none-any.whl

Requires Python 3.8+.

Modules

common_utils.cache — Local in-memory cache

from common_utils.cache.Cache import LocalCache

LocalCache.set_data("user:1", {"name": "alice"}, expire=60)
user = LocalCache.get_data("user:1")

common_utils.captcha — Math captcha image

from common_utils.captcha.captcha_util import generate_captcha_image

img_bytes, answer = generate_captcha_image()
# answer 是形如 "12" 的字符串,前端展示 img_bytes,校验用户输入

common_utils.date — Date/time helpers (UTC+8)

from common_utils.date.date_utils import current_date_time_str, current_date_str

print(current_date_time_str())  # '2024-05-20 18:30:00'
print(current_date_str())       # '2024-05-20'

common_utils.eval — Safe arithmetic evaluator

from common_utils.eval.eval_utils import safe_eval_expr

safe_eval_expr("60*60*7")  # 25200

common_utils.http — HTTP helpers

from common_utils.http.http_request_utils import http_post_json
from common_utils.http.http_param_utils import build_timeout_param

connect_t, read_t = build_timeout_param(data_count=500)
resp = http_post_json("https://api.example.com/echo", {"a": 1}, timeout_param=(connect_t, read_t))

common_utils.string — String helpers

from common_utils.string.string_utils import is_empty_common, contains_chinese

is_empty_common("  ")        # True
is_empty_common([])          # True
is_empty_common("hello")     # False
contains_chinese("你好")      # True

common_utils.thread — Threading helpers

from common_utils.thread.thread_utils import run_function_in_thread

t = run_function_in_thread(my_func, (arg1, arg2))
t.join()

Build a distribution

pip install build
python -m build
# 产物在 dist/

License

MIT

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

mini_common_utils-0.1.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

mini_common_utils-0.1.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file mini_common_utils-0.1.0.tar.gz.

File metadata

  • Download URL: mini_common_utils-0.1.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.6

File hashes

Hashes for mini_common_utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e42303a1ea69ae5a7cfff676f63fae065be892b949087882092107d6b993ecf6
MD5 99dfdf79b99d0ca18d8410527b3c8b92
BLAKE2b-256 4ac48a75b3146118ffcfe87b58417b37ea695fbe44e6fc53a2cf926636fe01e8

See more details on using hashes here.

File details

Details for the file mini_common_utils-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mini_common_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c63bad8cabd2c9462b19050de41401e565b19a712fa56d84458ba7118871c11
MD5 5f56bf26244bb9feaad120ed95798d03
BLAKE2b-256 f33a1d18644c430876434a02ddf2b8a1a02f4da6e56a0305c0d41b98ad093bb8

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