blog.humaneguitarist.org

object oriented smoothies ... mmm.

[Sun, 27 Jul 2014 15:19:37 +0000]
I'm at the coffee shop and it's raining like cats and dogs ... which is a happy situation considering that it wasn't "supposed" to rain for another three hours or so and I like rainy Sunday mornings especially while reading or writing. Anyway, I was talking to someone the other day about object oriented programming. Also, at work we're likely to start a project for which I'll be coding most of the backend and I've been asking myself "more OO or more functional?". Given that I really don't write in OO (neither does anyone on staff) even though I understand the concepts, I decided the best way to decide was really to do some light OO scripting in Python to help me decide. For the project at work, this little Python experiment - as well as the light readings I've done the last two days - has led me to decide on a more functional style. I'll be able to get the job done faster and test easier. The speed thing is very much an issue since we'll be on a very tight deadline. Moreover, the project's not the time for me to learn new things unless I have to in order to get the job done effectively. Anyway, the hardest part of this Python experiment was to think of a situation where OO would clearly be the better choice. So I started thinking about changing states of things. That's another reason I think OO is bad for the work project - I just need to do one-time input/output operations; things won't be changing states - at least not in the backend. As I was thinking, I was thinking about food. Because I was hungry at the time (like I am now). I eat a lot. And yes, I do exercise and lift weights regularly so my eating is quite intentional. I have two protein smoothies a day and so ... I started thinking about my blender as an object with properties. By attributing state ... and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. source: What Is an Object? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts. Retrieved July 27, 2014, from docs.oracle.com/javase/tutorial/java/concepts/object.html My blend is either "on" or "off". It consumes wattage to run, etc. And with a digital blender, I can record "usage" and, knowing the wattage used per second, calculate how much the blender has cost me to run both over time and, if I wanted, each individual time. This "usage" can be reset, too. If one has a ballpoint pen, that is. So "usage" and "cost" are other properties - although as I'm writing I could see how "cost", being based on a variable (cost per watt) and "usage", should perhaps not be a method but rather an external function, especially given how cost per watt is going to change over time and how the concept isn't unique to a blender. But for now, it is watt (pun!) it is. So, I created a "blender" class which has some simple specification metadata and I created an "appliance" class which "blender" inherits. The code is below as are the IDLE ouput results of me "using" the blender. In the IDLE shell, I also created a "toaster" class as that, too, inherits the "appliance" class. p.s. If you want my smoothie recipe, leave a comment. p.p.s. If you think my programming sucks, leave a comment. p.p.p.s: If you think smoothies suck, your comment will not get published. :) ######################### # smOOthie.py (Object Oriented smoothie). ##### appliance class. class appliance(): # establish the state of the appliance. def __init__(self): self.state = "off" self.reset() # toggle the power state of the appliance, # record the on/off times as "start"/"stop", # return the new state of the appliance. def toggle(self): from time import time now = time() # if toggled "on", set the "start" time. if self.state == "off": self.state = "on" self._start = now # if toggled "off", set the "stop" time and record "start"/"stop" times. else: self.state = "off" self._stop = now self.usage.append({"start": self._start, "stop": self._stop}) return # return usage cost for appliance since last reset. def cost(self): total_seconds = 0 for use in self.usage: if len(use) == 2: # make sure there is a "start" and "stop" time. subtotal_seconds = use["stop"] - use["start"] total_seconds = total_seconds + subtotal_seconds total_watts = total_seconds * self.specifications["watts_used_per_second"] total_cost_in_cents = total_watts * .012 # cost per watt based on "http://www.eia.gov/electricity/monthly/update/end_use.cfm#tabs_prices-3" total_cost_in_cents = round(total_cost_in_cents, 2) return total_cost_in_cents # reset usage counter. def reset(self): self.usage = [] ######################### ##### blender class. class blender(appliance): kind = "blender" specifications = {"manufacturer": "generic", "model": "generic", "price": "n/a", "watts_used_per_second": .0834} # "watts_used_per_second" based on "http://www.indoorgenerator.com/0p/Power-Consumption-Table.pdf" for a blender. ######################### ##### use the blender appliance - i.e. make a smoothie. from time import sleep myBlender = blender() # setting specifications (especially watts used) for Acme blender. myBlender.specifications = {"manufacturer": "Acme Corporation", "model": "Illudium Q-36", "price": "$1,000,000.00 (space dollars)", "watts_used_per_second": 1000} # this is a VERY expensive blender to buy AND use! def useBlender(duration=5): print "\nMy blender was: " + myBlender.state myBlender.toggle() print "\nMy blender is now: " + myBlender.state print "\n\t... using blender for this many seconds: " + str(duration) sleep(duration) # making a smoothie usually takes a little longer ... myBlender.toggle() print "\nMy blender is now: " + myBlender.state print "\nMy blender has been used this many times: " + \ str(len(myBlender.usage)) print "\nMy blender has been used during the following times (UNIX):" for i in myBlender.usage: print "\t", i print "\nUsing my blender has cost this (cents): " + \ str(myBlender.cost()) print "_" * 25 useBlender() # blend smoothie. useBlender(2) # just a quick pulse. # fin. And here's the output ... Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> My blender was: off My blender is now: on ... using blender for this many seconds: 5 My blender is now: off My blender has been used this many times: 1 My blender has been used during the following times (UNIX): {'start': 1406470729.349, 'stop': 1406470734.357} Using my blender has cost this (cents): 60.1 _________________________ My blender was: off My blender is now: on ... using blender for this many seconds: 2 My blender is now: off My blender has been used this many times: 2 My blender has been used during the following times (UNIX): {'start': 1406470729.349, 'stop': 1406470734.357} {'start': 1406470734.388, 'stop': 1406470736.416} Using my blender has cost this (cents): 84.43 _________________________ >>> class toaster(appliance): kind = "toaster" specifications = {"manufacturer": "?", "model": "?", "price": "?", "watts_used_per_second": .4167} >>> myToaster = toaster() >>> myToaster.toggle(); sleep(10), myToaster.toggle() (None, None) >>> myToaster.cost() 0.05 >>> myToaster.state 'off' >>> myToaster.toggle(); sleep(10), myToaster.toggle() (None, None) >>> myToaster.cost() 0.1