Skip to main content

A datatype to make using stones and pounds easier

Project description

stlbs

The stlbs package (pronounced stonepounds) is the home of the StLb datatype.

In the UK, people often measure their weight in stones and pounds - it's common to hear people describe themselves as weighing, for example, "thirteen stones and four pounds". Working in code with these units can be annoying. You need to treat the stones and pounds as separate values somehow; no-one will thank you for presenting a weight as 1.29 stones when they expected to see 1 stone and 4 lbs.

The StLb datatype (pronounced stonepound) makes working with stones and pounds much easier. An instance of StLb is single object which represents a weight in the form of stones and pounds (lbs). It gives an easy way to display such a weight as numbers or text, and offers a simple way to do arithmatic with weights in stones and pounds. stlbs has no dependencies outside the Python Standard Library.

Basic usage

Installation

Install stlbs from pip:

pip install stlbs

stlbs has no third party dependencies, and supports Python 3.9 and later.

The StLb object - initialisation

The StLb() class is a data type to hold weights in stones and lbs.

>>> from stlbs import StLb

StLb() is initialised with either a list or a tuple to represent whole stones and the remainder lbs:

>>> my_weight = StLb([10, 7])
>>> my_weight
StLb object: 10st and 7 lb [147 lb]

As you can see, we get a nice textual representation of the object.

You can also initialise StLb by passing only lbs:

>>> my_weight = StLb([0, 147])
>>> my_weight
StLb object: 10st and 7 lb [147 lb]

When initialised with lbs only, such as in the example above, the StLb object will correctly convert to stones and lbs.

StLb attributes

An instance of StLb has a number of useful attributes. Given an instance of:

>>> my_weight = StLb([10, 7])

We can access the whole_stones and remainder_lbs values separately:

>>> my_weight.whole_stones
10

>>> my_weight.remainder_lbs
7

We can get the value in lbs:

>>> my_weight.in_lbs
147

We can get a nice textual representation:

>>> my_weight.text
'10st and 7 lb [147 lb]'

Doing sums with StLb objects

Addition

You can add two instances of StLb together:

>>> my_first_weight = StLb((4, 7))
>>> my_second_weight = StLb((1, 0))
>>> my_first_weight + my_second_weight
StLb object: 5st and 7 lb [77 lb]

You can also add in place to a single StLb object:

>>> my_weight = StLb((4, 7))
>>> my_weight
StLb object: 4st and 7 lb [63 lb]

>>> my_weight += (1, 0)  # Add 1st and zero lbs
>>> my_weight
StLb object: 5st and 7 lb [77 lb]

Subtraction

Subtraction works in the same way as addition. You can subtract one instance of StLb from another:

>>> my_first_weight = StLb((4, 7))
>>> my_second_weight = StLb((1, 0))
>>> my_first_weight - my_second_weight
StLb object: 3st and 7 lb [49 lb]

You can also subtract in place:

>>> my_weight = StLb([4, 7])
>>> my_weight -= (1, 0)
>>> my_weight
StLb object: 3st and 7 lb [49 lb]

Note that the result of subtraction from an instance of StLb cannot be negative. Any mathematical operation that would result in a negative number raises a SubtractionBelowZeroError exception.

Multiplication

You can multiply two StLb objects together:

>>> weight_1 = StLb((2, 0))
>>> weight_2 = StLb((2, 0))
>>> weight_1 * weight_2
StLb object: 56st and 0 lb [784 lb]

That answer might look surprising if you were expecting 2st * 2st to equal 4st. Multiplying two StLb objects together multiplies their in_lbs values and converts back to stones and lbs. Our example of 2st * 2st is the same as saying 28lbs * 28lbs, which gives 784lbs or 56st.

If you just want to multiply a StLb object by a number (rather than another StLb instance), you can:

>>> my_weight = StLb((2, 0))
>>> my_weight * 2
StLb object: 4st and 0 lb [56 lb]

Division

Lastly, it is also possible to divide instances of StLb:

>>> weight_1 = StLb((10, 0))
>>> weight_2 = StLb((5, 0))
>>> weight_1 / weight_2
2.0

You can also divide a StLb object by a number:

>>> weight_1 = StLb((4, 0))
>>> weight_1
StLb object: 4st and 0 lb [56 lb]
>>> weight_1 / 2
StLb object: 2.0st and 0.0 lb [28.0 lb]

A note on values for doing maths with StLb

Any mathematical operation that can be performed on a StLb instance with another instance (such as adding them together) will also work if you use a list or tuple in the format (whole_stones, remainder_lbs). For example, these two expressions will give the same result:

>>> StLb((10, 7)) + StLb((1, 0))
StLb object: 11st and 7 lb [161 lb]

>>> StLb((10, 7)) + (1, 0)
StLb object: 11st and 7 lb [161 lb]

Constraints

A StLb object has two constraints on it:

  • It can't be initialised with negative values for whole_stones or remainder_lbs. Doing so will raise a ValueError.
  • Subtracting from it can't result in a negative number. Doing so will result in a SubtractionBelowZeroError.

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

stlbs-1.0.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

stlbs-1.0.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file stlbs-1.0.1.tar.gz.

File metadata

  • Download URL: stlbs-1.0.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.6

File hashes

Hashes for stlbs-1.0.1.tar.gz
Algorithm Hash digest
SHA256 54e021a0fe952d93a11b42e633598ea0e4da79e122d8b7543f4f79e43db140eb
MD5 3ec1fb5176476781ca2bf2813158ce3e
BLAKE2b-256 ba7d7b52b8b92b570362ab8c3da607dd55e34e0449811da6b1045eb0b18881d2

See more details on using hashes here.

File details

Details for the file stlbs-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: stlbs-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.6

File hashes

Hashes for stlbs-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f4f4c51359469406143eb0314b13c7eecace7fbb47a2ef0ef1b8a80741d2f0f2
MD5 2a44070f5ed6d0acd752aacf5559ae3a
BLAKE2b-256 983320f943c5be8df83d716e4f044805eed55f06c086038144c6d164e52b15f8

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