Skip to main content

Package for converting data units

Project description

Pypi MIT licensed GitHub Release Date

Simple package for managing data units including conversions and operations

Installation

# PyPi Installation
pip install datavalues
# GitHub Installation
pip install git+'https://github.com/Scraps23/datavalues.git'

Getting Started

The package is a collection of sub-classed object classes for each data unit. To import all the object classes, use the import below in your code:

from data.units import *

This is the import method used for all examples below. ### Object Classes There are nine 9 object classes, each named for the unit they represent. - Byte - KiloByte - MegaByte - GigaByte - TeraByte - PetaByte - ExaByte - YottaByte - ZettaByte ## Usage Each of the object classes sub-classes the BaseDataModel which allows for arithmetic and comparison operators, and a shared convert method. ### Conversion The conversion method is non-destructive and relies upon the class name of the data object. Changing the names of the class(es) on import will break functionality.

# Converting the object does not alter it, it returns a new object
as_mb = MegaByte(100)
as_gb = as_mb.convert('gb')
print(as_mb, as_gb)
## RETURNS:
100 MB 0.1 GB
import os
# Conversion can be done in-line to reduce memory usage
as_gb = MegaByte(100).convert('gb')
# This is especially useful for bytes-based systems and human-readable input being merged
selected_size = GigaByte(float(input('How many gigabytes? : ')))
os.environ['disk_size_var'] = selected_size.convert('b')

Operators

Data objects can have math applied against them, and be compared to each other and int/float objects to simplify operations like calculating total disk usage, RAID viability, and more.

Arithmetic Operators

The mathematical operators allow objects of different classes to interact; if another data object is supplied as the other value, both values are converted to bytes, evaluated, and converted back to the original unit. Otherwise, if an integer or float is supplied, it is assumed that value is in the same unit as the original unit.

current_disk = GigaByte(1270)
end_goal = TeraByte(2)
# Will return in Terabytes (0.73 TB)
print(end_goal - current_disk)
# Will return in Gigabytes (730.0 GB)
print((end_goal - current_disk).convert('gb'))

Comparison Operators

The data objects can also be compared to each other using the comparison operators (i.e. >, <, >=, etc). In this case, they similarly convert both values to bytes and compare that float object. Otherwise, if a float or int is supplied as the comparator, then it is assumed the number is in the same unit as the object being compared.

current_disk = GigaByte(1270)

if current_disk > 1000:
    print(current_disk.convert('tb'))
else:
    print(current_disk)

Returning Values

The __str__ method returns the value in a human-readable format which allows for clean output in code.
The __repr__ method returns the creation string for the object.
disk1 = Byte(150000000000).convert('gb')
disk2 = MegaByte(328000).convert('gb')

# Printing the disks returns the human-readable value
print(','.join([disk1, disk2]))
## RETURNS:
150.0 GB 328.0 GB

# The object itself returns its creation string
disk1
## RETURNS:
GigaByte(150.0)

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

datavalues-0.3.1-py3.8.egg (9.0 kB view details)

Uploaded Source

datavalues-0.3.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file datavalues-0.3.1-py3.8.egg.

File metadata

  • Download URL: datavalues-0.3.1-py3.8.egg
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for datavalues-0.3.1-py3.8.egg
Algorithm Hash digest
SHA256 8505730e7eda97bd533b8d4978998e8e968827e88a8310b092cd926e5d13a5ad
MD5 9f35ca5a9344f51920947e0b38d40250
BLAKE2b-256 6ffd7a8be01593737a50139d925bb7f948c7742d73838c8467a46bc9984243ac

See more details on using hashes here.

File details

Details for the file datavalues-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: datavalues-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for datavalues-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4783794a76c4decb4eb5788dee2533945c046fc9a48bddb4e7208d1420880275
MD5 6887ff9afae3c54b146268ee8877d8d5
BLAKE2b-256 445f4f2a83f4917e03874da0e5abb3c909f756d3968f1196491e7c7e9dadfce1

See more details on using hashes here.

Supported by

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