Skip to content

Instantly share code, notes, and snippets.

@spiechu
Created September 28, 2011 19:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save spiechu/1249078 to your computer and use it in GitHub Desktop.
Shaker launch script
#!/usr/bin/env python
import unicodedata
from numbers import Number
class Shaker:
def __init__(self, shakerCapacity = 1000000):
# Zakladamy, ze pojemnosc shakera jest nieograniczona ;-)
self._shakerCapacity = shakerCapacity
# Inicjalizuje dictionary dla przechowywania klucz => wartosc
# klucz dla alkoholu jest w skladnikach zawsze
self._drinkIngredients = {'alcohol' : 0}
def addIngredient(self, name, amount, alcoholPercentage = 0):
# Gdyby komus zachcialo sie wprowadzac inne wartosci niz numerki i wartosci dodatnie
if not isinstance(amount, Number) or not isinstance(alcoholPercentage, Number) or alcoholPercentage < 0 or amount < 0:
raise ValueError('Podano {} i {} dla {}'.format(amount, alcoholPercentage, name))
# Sprawdzam czy jest wystarczajaco duzo miejsca w shakerze
spaceInShakerLeft = self._shakerCapacity - self.totalLiquidAmount()
if spaceInShakerLeft >= amount:
self._storeNewIngredient(name, amount, alcoholPercentage)
else:
# Jak za malo to dolewam tylko tyle ile sie zmiesci
self._storeNewIngredient(name, spaceInShakerLeft, alcoholPercentage)
def _storeNewIngredient(self, name, amount, alcoholPercentage):
# Licze objetosc czystego alkoholu w plynie
liquidAlcohol = 0
if alcoholPercentage > 0:
liquidAlcohol = (amount * alcoholPercentage) / 100
self._drinkIngredients['alcohol'] += liquidAlcohol
# Sprawdzam czy w drinku jest juz dany skladnik i dodaje
if self._drinkIngredients.get(name) == None:
self._drinkIngredients[name] = amount - liquidAlcohol
else:
self._drinkIngredients[name] += amount - liquidAlcohol
def getAllDrinkIngredients(self):
# Wypluwam czysty dictionary ze skladnikami
return self._drinkIngredients
def totalLiquidAmount(self):
# Zliczam objetosc wszystkich skladnikow w shakerze
total = 0
for value in self._drinkIngredients.values():
total += value
return total
def getTotalLiquidAlcohol(self):
return self._drinkIngredients['alcohol']
def getTotalAlcoholPercentage(self):
# Licze calkowity procent alkoholu w drinku
return (self.getTotalLiquidAlcohol() / self.totalLiquidAmount()) * 100
def getTotalAlcoholInGrams(self):
# Przeliczam ml alkoholu na gramy
return 0.79 * self.getTotalLiquidAlcohol()
def computeAlcoholPromillesPerWeight(self, weight, man = True):
# PRZYBLIZONA zawartosc procentowa plynow ustrojowych w zaleznosci od plci
humanFluidsPercentage = 0.7 if man == True else 0.6
return self.getTotalAlcoholInGrams() / (humanFluidsPercentage * weight)
def pourOutDrink(self, amount):
"""
Metoda wlasciwie niepotrzebna. Mozna wylac (wypic!) czesc zawartosci
shakera i sobie potem dolewac kolejne skladniki.
"""
if amount >= self.totalLiquidAmount():
self._drinkIngredients = {'alcohol' : 0}
else:
percentage = amount / self.totalLiquidAmount()
for key, value in self._drinkIngredients.items():
self._drinkIngredients[key] -= self._drinkIngredients[key] * percentage
class ShakerDisplayer:
def displayShakerDetails(shaker, weight, man = True):
if not isinstance(shaker, Shaker):
raise ValueError('Podano argument klasy {}'.format(shaker.__class__))
print('Skladniki drinka:')
for key,value in shaker.getAllDrinkIngredients().items():
print('{:.2f}ml {}'.format(value,key))
print('Calkowita zawartosc alkoholu w drinku: {:.2f}%'.format(shaker.getTotalAlcoholPercentage()))
print('Calkowita zawartosc alkoholu w drinku (wyrazona w gramach): {:.2f}g'.format(shaker.getTotalAlcoholInGrams()))
print('Prawdopodobna liczba promili dla {} o wadze {}kg wynosi {:.2f}{}'.format('faceta' if man == True else 'kobiety', weight, shaker.computeAlcoholPromillesPerWeight(weight,man), unicodedata.lookup('PER MILLE SIGN')))
#!/usr/bin/env python
from shaker import Shaker, ShakerDisplayer
shaker = Shaker()
shaker.addIngredient('piwo', 1000, 5.7)
shaker.addIngredient('wodka', 500, 40)
shaker.addIngredient('sprite', 1000, 0)
ShakerDisplayer.displayShakerDetails(shaker, 89, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment