Package: coinhandler
Coinhandler :moneybag:A Python module to handle interacting with coins>>>fromcoinhandlerimportCoinHandler>>>handler=CoinHandler(...starting_float=(2.00,1.00,0.50,0.20,0.05)...)>>>handler.insert(0.50)>>>handler.insert(1.00)>>>handler.purchase(1.25)>>>handler.return_coins()CoinCollection(TwentyPence(1),FivePence(1))Installationpipenv*pipenvinstallcoinhandlerpippipinstallcoinhandlerFrom sourcepipinstallgit+git://github.com/alxwrd/coinhandler.git*recommendedUseageCoinHandlerACoinHandlerobject can be used to manage a transaction, or a series
of transactions. To create a handler instance, it should be provided
with a startingfloat(money supply).Thestarting_floatshould be aniterableof eitherfloatorintfromcoinhandlerimportCoinHandlerhandler=CoinHandler(starting_float=(2.00,1.00,0.50,0.20,0.05))ACoinHandlerobject provides a simple interface for making trasactions
against the float it started with..available_coins->CoinCollectionProvides access to the current supply ofCoins in in the handler.>>>handler.available_coinsCoinCollection(TwoPound(1),OnePound(1),FiftyPence(1),TwentyPence(20),FivePence(5)).current_transaction->TransactionProvides access to theCoins that are part of the current transaction.>>>handler.insert(0.50)>>>handler.current_transactionTransaction(FiftyPence(1)).total()->floatReturns the handlers current total value as a float.>>>handler.total()3.75Also equivalent to:>>>handler.available_coins.total()/1003.75.insert(value)Add a coin ofvalueto thecurrent_transaction.>>>handler.insert(0.50)>>>handler.current_transactionTransaction(FiftyPence(1)).purchase(value)Moves the coins fromcurrent_transactioninto theavailable_coinsand
makes the difference in coins between the purchasevalueand thecurrent_transation.total()available in.current_transaction.>>>handler.available_coinsCoinCollection(TwentyPence(1),FivePence(1))>>>handler.insert(0.50)>>>handler.purchase(0.25)>>>handler.current_transactionTransaction(TwentyPence(1),FivePence(1))>>>handler.available_coinsCoinCollection(FiftyPence(1)).return_coins()->CoinCollectionEmpties out thecurrent_transactionand returns those coins as aCoinCollection.CoinCollectionACoinCollectionobject represents a collection ofCoins. It functions
similar to a pythonlist, and provides some similar methods.fromcoinhandlerimportCoinCollectioncollection=CoinCollection(2.00,1.00,0.50)You can also create aCoinCollectionfrom a value. This will return the
smallest amount ofCoins needed to create that value.>>>CoinCollection.from_value(1.25)CoinCollection(OnePound(1),TwentyPence(1),FivePence(1))TransactionATransactionobject is a subclass ofCoinCollection, and functions
identically..remove_by_value(value)->CoinCollectionRemoves coins from the collection by a value, and returns new collection
with valid coins from the original collection.>>>collection=CoinCollection(2.00,1.00,0.20,0.05)>>>collection.remove_by_value(1.25)CoinCollection(OnePound(1),TwentyPence(1),FivePence(1))NOTE:.remove_by_valuewill only remove available coins from
the original collection. So for the example:>>>collection=CoinCollection(2.00,1.00,0.20,0.05)>>>collection.remove_by_value(1.30)CoinCollection(OnePound(1),TwentyPence(1),FivePence(0.05))Only '1.25' is returned..total()Returns the total value of the collection as afloat.>>>collection=CoinCollection(2.00,1.00,0.50)>>>coins.total()3.75.append(value)Adds thevalueto the collection.>>>collection=CoinCollection(2.00,1.00,0.50)>>>collection.append(1.00)>>>collection.total()4.50.extend(iterable[values])Extends the collection by aniterableofvalues.>>>collection=CoinCollection(2.00,1.00,0.50)>>>collection.extend([1.00,1.00])>>>collection.total()5.50.remove(value)Removes aCoinrepresented byvaluefrom the collection.>>>collection=CoinCollection(2.00,1.00,0.50)>>>collection.remove(1.00)>>>collection.total()2.50.clear()->CoinCollectionRemoves allCoins from the collection, and returns them as a new collection.>>>collection=CoinCollection(2.00,1.00,0.50)>>>coins=collection.clear()>>>collection.total()0.0>>>coins.total()3.50.pop(index=-1)->CoinRemoves theCoinlocatedindexout of the collection and returns it.>>>collection=CoinCollection(2.00,1.00,0.50)>>>collection.pop()FiftyPence(1)>>>collection.pop(0)TwoPound(1)>>>coins.total()1.00As alistFor basic useage, aCoinCollectioncan beduck typedas a list. It can be:Compared to aniterableofvalues,>>>collection=CoinCollection(2.00,1.00,0.50)>>>assertcollection=[2.00,1.00,0.50]>>>assertcollection=(2.00,1.00,0.50)Iterated over,>>>collection=CoinCollection(2.00,1.00,0.50)>>>forcoinincollection:...print(coin)'£2.00''£1.00''50p'And accessed by index.>>>collection=CoinCollection(2.00,1.00,0.50)>>>collection[1]OnePound(1)CoinACoinobject represents avalue. Its use allows representing money
usingintvs.float.TheCoinclass is a factory class for all otherCoinsthat have been defined.
It will return the highest value coin of a givenvalue.fromcoinhandlerimportCoinfromcoinhandler.coinsimportOnePound,FiftyPenceassertCoin(100)==OnePound(1)assertCoin(50)==FiftyPence(1)When using theCoinfactory class, a valid coinvalueshould be used. Not doing so
can create undesirableCoinobjects.>>>coin=Coin(23)OnePence(23)# Use a CoinColletion instead!>>>CoinCollection.from_value(23)CoinCollection(TwentyPence(1),TwoPence(1),OnePence(1))Coinsof a specfic type can be created by just by creating an instance of them.>>>fromcoinhandler.coinsimportOnePound>>>OnePound()OnePound()All coins have a value, which is the represented as aninteger.>>>fromcoinhandler.coinsimportOnePound>>>OnePound().value100Subclassing fromCoinwill add that coin to the available coins to be created.>>>fromcoinhandlerimportCoin>>>Coin(25)FivePence(25)>>>classQuarter(Coin):...multiplier=25...def__str__(self):...returnf"{self.value}¢">>>coin=Coin(25)>>>print(coin)'25¢'>>>coin.value25