Giter Site home page Giter Site logo

stlbs's Issues

Overload `__mul__()` to allow multiplication of `StLb` objects

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:

  • A new object is created from the result: new_instance = StLb(0, instance.in_lbs * factor)
  • The existing instance has its whole_stones, remainder_lbs and in_lbs values updated in place

I can't imagine a use case where I'd need to multiply two StLb objects together.

Remove duplicated code

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)

adding tuple to StLb instance results in incorrect whole_stones and remainder_lbs values

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.

Simply `StLb` constructor

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.

Sort out pluralisation in `__repr()__` and `__str__()`

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.