Skip to main content

Lightweight Python package for converting.

Project description

Python minimum version Test-pytest Format-Ruff Lint-Ruff

unic

unic is a lightweight Python package for converting:

  • time units (hour, min, sec, msec)
  • Unix timestamps to datetime / date
  • datetime strings to Unix timestamps
  • metric length units (nm to Tm)

It is designed for simple scripting, data processing, analytics, and timestamp handling.

Installation

pip install unic

Quick Start

import unic

# Time conversion
time_model = unic.load_model("time")
print(time_model.convert(2, from_unit="hour", to_unit="min"))  # 120

# Unixtime -> datetime
datetime_model = unic.load_model("datetime")
print(datetime_model.convert(1657985494, format="datetime"))

# String datetime -> unixtime
unixtime_model = unic.load_model("unixtime")
print(unixtime_model.convert("2022-07-18 13:49:00", tz="Asia/Tokyo"))

# Metric system conversion
metric_model = unic.load_model("metric_system")
print(metric_model.convert(1200, from_unit="mm", to_unit="m"))  # 1.2

Supported Conversions

TimeModel

  • hour / min / sec / msec

DatetimeModel

  • unixtime -> datetime.datetime
  • unixtime -> datetime.date

UnixtimeModel

  • datetime string -> unixtime
  • supported formats:
    • yyyy-mm-dd hh:mm:ss
    • yyyy/mm/dd hh:mm:ss

MetricSystemModel

  • metric length units:
    • nm, um, mm, cm, m, km, Mm, Gm, Tm

Examples

Convert a single value

import unic

model = unic.load_model("time")
result = model.convert(7.5, from_unit="hour", to_unit="min")
print(result)  # 450

Convert a list of values

import unic

model = unic.load_model("time")
result = model.convert([2, 4, 6], from_unit="hour", to_unit="min")
print(result)  # [120, 240, 360]

Convert unixtime to datetime

import unic

model = unic.load_model("datetime")
result = model.convert(1657985494, format="datetime", tz="Asia/Tokyo")
print(result)

Convert datetime string to unixtime

import unic

model = unic.load_model("unixtime")
result = model.convert("2022-07-18 13:49:00.123", unit="msec")
print(result)

Notes

  • convert() supports both single values and lists.
  • convert_batch() is deprecated. Please use convert() with a list instead.
  • If unit is omitted in UnixtimeModel, the default is sec.

Documentation

  • See docs/UserGuide.md
  • Add future pages:
    • docs/QuickStart.md
    • docs/SupportedConversions.md
    • docs/FAQ.md
    • docs/MigrationGuide.md

Why unic?

  • Simple API
  • Lightweight package
  • Supports list conversion
  • Supports timezone-aware conversion
  • Tested with multiple Python versions

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

unic-2.2.2.tar.gz (58.0 kB view details)

Uploaded Source

Built Distribution

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

unic-2.2.2-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file unic-2.2.2.tar.gz.

File metadata

  • Download URL: unic-2.2.2.tar.gz
  • Upload date:
  • Size: 58.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for unic-2.2.2.tar.gz
Algorithm Hash digest
SHA256 8ff508ed2672b7cef6e51fcc9e122de89c2c96e22839b55f56febf7eebdacc57
MD5 eebb0a2a6a5cd728b0d71c74cd17b93a
BLAKE2b-256 81637b60e49dfd0afe40c393669513ccad3c86ddc386399622fc4a78797dda76

See more details on using hashes here.

Provenance

The following attestation bundles were made for unic-2.2.2.tar.gz:

Publisher: publish-to-pypi.yml on subretu/unic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file unic-2.2.2-py3-none-any.whl.

File metadata

  • Download URL: unic-2.2.2-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for unic-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dc5d61808af9b9a77f5bd2f0d1f03596ea4c64d68f820a6402e4427083208451
MD5 de6babba9ef3d9dd81c235fd8bfd94ce
BLAKE2b-256 db22bb403d459f1418b749758af28d327b6f5cb5ac6968ac0d559a97347ca71e

See more details on using hashes here.

Provenance

The following attestation bundles were made for unic-2.2.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on subretu/unic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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