rogfrich / stlbs Goto Github PK
View Code? Open in Web Editor NEWA datatype to easily represent stones and pounds (lbs) in Python
License: MIT License
A datatype to easily represent stones and pounds (lbs) in Python
License: MIT License
Update README.md
with an intro and examples of use.
The constructor for the StLb
object can be simplified if, regardless of the whole_stones, remainder_lbs
values in the initial call, they are converted to lbs and then converted back to whole_stones, remainder_lbs
- this will remove the need to handle cases where the input is invalid but mathematically possible, such as: (0, 15)
and will remove the need for the current code branching.
The remainder_lbs
attribute of a StLb
instance should never be more than 13, because at 14 it should increment whole_stones
. Updating the values via the overloaded addition operator can result in such a case occurring:
>>> spam = StLb([1, 0])
>>> spam += (0, 15)
>>> spam
StLb object: 1 stones and 15lbs [29lbs]
# Should be 2 stones and 1 lb
This is a bug that needs to be fixed, and we need tests to ensure that remainder_lbs
is never more than 13, and that whole_stones
ticks up appropriately.
Currently the repr
and str
magic methods are hard coded in the format:
"StLb object: {self.whole_stones} stones and {self.remainder_lbs} lbs [{self.in_lbs} lbs]"
This means that the output is inappropriately pluralised when the value of whole_stones
, remainder_lbs
or in_lbs
is 1.
Add some logic to handle this circumstance.
As well as overloading the +
and -
operators, StLb
should have the ability to multiply as well:
The multiplication factor should be a number - instance.in_lbs
should be multiplied by the factor with two possible outcomes depending on context:
new_instance = StLb(0, instance.in_lbs * factor)
whole_stones
, remainder_lbs
and in_lbs
values updated in placeI can't imagine a use case where I'd need to multiply two StLb
objects together.
Dividing an StLb
object by a numeric type returns a numeric type: StLb(8, 0) / 4 == 2
Dividing an StLb
object by another StLb object returns a numeric type: StLb(8, 0) / StLb(2, 0) == 2
The __init__()
, __add__()
and __sub__()
methods of StLb()
have the same code duplicated in them.
From __init__()
:
self.in_lbs = (stones_lbs[0] * 14) + stones_lbs[1]
From __add__()
:
other_in_lbs = (other[0] * 14) + other[1]
From __sub__()
:
other_in_lbs = (other[0] * 14) + other[1]
Each of them has a requirement to take representation stones and lbs in the format whole_stones, remainder_lbs
and convert it to lbs using the formula (whole_stones * 14) + remainder_lbs
.
There should be a private function to do this. Given a whole_stones, remainder_lbs
tuple, it should return another tuple in the format (0, value_in_lbs)
:
def _convert_stones_and_lbs_to_lbs((whole_stones, remainder_lbs)):
in_lbs = (whole_stones * 14) + remainder_lbs
return (0, in_lbs)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.