Convert between various sizes.
Project description
Overview
The File Size Converter module provides functions for converting file sizes between different units in both IEC (binary) and SI (decimal) formats. It includes functionality for getting the full name of a unit from its abbreviation and converting sizes between units.
Features
- Convert sizes between different IEC units (e.g., Bytes to Kibibytes).
- Convert sizes between different SI units (e.g., Bytes to Kilobytes).
- Retrieve the full name of a unit from its abbreviation.
- Support for both IEC (binary) and SI (decimal) unit systems.
Installation
pip install wolfsoftware.convert-size
Usage
Importing the Module
import wolfsoftware.convert_size as fsc
Converting Sizes
To convert a size between different units, use the convert_size
function. You can specify whether to use SI units by setting the si_units
parameter.
# IEC conversion example
size_in_gib = fsc.convert_size(1024, 'MiB', 'GiB')
print(size_in_gib) # Output: 1.0
# SI conversion example
size_in_gb = fsc.convert_size(1000, 'MB', 'GB', si_units=True)
print(size_in_gb) # Output: 1.0
Getting the Full Name of a Unit
To get the full name of a unit from its abbreviation, use the get_name_from_code
function.
# IEC unit name example
unit_name = fsc.get_name_from_code('KiB')
print(unit_name) # Output: Kibibyte
# SI unit name example
unit_name = fsc.get_name_from_code('KB', si_units=True)
print(unit_name) # Output: Kilobyte
Functions
convert_size(size: float, start_unit: str, end_unit: str, si_units: bool = False) -> float
Convert a size between units, either IEC or SI.
size
(float): The original size.start_unit
(str): The starting unit abbreviation.end_unit
(str): The ending unit abbreviation.si_units
(bool): If True, use SI units; otherwise, use IEC units.- Returns (float): The converted size.
- Raises
ValueError
: If the unit abbreviations are not valid.
get_name_from_code(unit: str, si_units: bool = False) -> str
Get the full name of a unit from its abbreviation.
unit
(str): The unit abbreviation.si_units
(bool): If True, use SI units; otherwise, use IEC units.- Returns (str): The full name of the unit.
- Raises
ValueError
: If the unit abbreviation is not valid.
get_name_from_code_iec(unit: str) -> str
Get the full name of an IEC unit from its abbreviation.
unit
(str): The IEC unit abbreviation.- Returns (str): The full name of the IEC unit.
- Raises
ValueError
: If the unit abbreviation is not valid.
get_name_from_code_si(unit: str) -> str
Get the full name of an SI unit from its abbreviation.
unit
(str): The SI unit abbreviation.- Returns (str): The full name of the SI unit.
- Raises
ValueError
: If the unit abbreviation is not valid.
Error Handling
The module raises ValueError
exceptions for invalid unit abbreviations. Ensure that you handle these exceptions in your code:
try:
size_in_gib = fsc.convert_size(1024, 'MiB', 'GiB')
except ValueError as e:
print(e)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file wolfsoftware_convert_size-0.1.2.tar.gz
.
File metadata
- Download URL: wolfsoftware_convert_size-0.1.2.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25157ceb88610a92c5ffefad53e0d6c2507e3082eb68b5963d8df2009f746019 |
|
MD5 | e759e7a3d5392c9cbce1af87ba66d8e1 |
|
BLAKE2b-256 | 3ffa7e9aa2c888747c5cb511d1ac84e819c7718dd0bbf3c0e3a39388df9766d6 |
File details
Details for the file wolfsoftware.convert_size-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: wolfsoftware.convert_size-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a8209f935f7132f0bd94502bdb43f6307f27a4dc322d447b210c22f42274c0a |
|
MD5 | 09e0f064bab62b152c26f75f511182c1 |
|
BLAKE2b-256 | 6cf31283ca2e04b9cbc1781eb443d39227e880576ca34dbdf8f8d5013e888b5a |