| |||
Tap simulation example (Mk 1) in Python
this example from a Well House Consultants training course
Source code: taps.py Module: Y301
#!/usr/bin/env python
"""In this example, we have defined a number of taps and "flow change" objects and have then turned the taps on and off in a test application with the flows limited within the class. There's a complete "Mk2" example which is also available from the same source as this. See links from http://www.wellho.net/resources/Y301.html """ class change: def __init__(self): self.summat = 0 def act_on(self,dowhat): self.temperature, self.destinedstate = dowhat.split(",") def gettap(self): return self.temperature def getdirn(self): return self.destinedstate class tap: def __init__(self,room): self.title = room self.hot = 0 self.cold = 0 self.maxflow = 1 def getname(self): return self.title def gethotflow(self): return self.hot def getcoldflow(self): return self.cold def getmaxflow(self): return self.maxflow def apply(self,doingwhat): whichtap = doingwhat.gettap() whichway = doingwhat.getdirn() maxup = tap.mainsmax - tap.mainsnow if maxup >= 1: if whichway == "on": if whichtap == "hot": if self.hot < self.maxflow: self.hot += 1 tap.mainsnow += 1 else: if self.cold < self.maxflow: self.cold += 1 tap.mainsnow += 1 else: if whichtap == "hot": if self.hot > 0: self.hot -= 1 tap.mainsnow -= 1 else: if self.cold > 0: self.cold -= 1 tap.mainsnow -= 1 def report(self): amount1 = self.gethotflow() amount2 = self.getcoldflow() print "The flow is "+str(amount1)+" hot plus cold "+str(amount2) # This example supports a single water connection # See the "mk 2" example that's enhanced to use an extra class # and can support multiple connections each with different maximum flows tap.mainsmax = 4 tap.mainsnow = 0 class supertap(tap): def __init__(self,room): # Some of this code is repeated from tap class. In Mk2 example # we use an internally called function to avoid the duplication self.title = room self.hot = 0 self.cold = 0 self.maxflow = 2 # ########################################### # define a number of taps users = [] users.append(supertap("kitchen")) users.append(tap("utility room")) users.append(tap("shower room")) # Following line is a demo to show that users is a list of objects # and you would not "normally" print out such a thing - or if you # did, you would redefine the __str__ method to make it print neatly! print users # Define what Lisa and Leah would like to do with taps leahdoes = change() lisadoes = change() action1 = "hot,on" action2 = "cold,on" # Encapsulate the action within the "change" object because # there's going to be some complex logic to hide leahdoes.act_on(action1) lisadoes.act_on(action2) # Have leah and lisa visit each tap in turn and apply their changes for flow in users: # Better say which tap we're looking at to help the reader where = flow.getname() upto = flow.getmaxflow() print "The tap in the "+where+" has a maximum flow of "+str(upto) # Leah visits each tap twice and does her thing and the Lisa # then visits each tap once and does hers! flow.apply(leahdoes) flow.report() flow.apply(leahdoes) flow.report() flow.apply(lisadoes) flow.report() Learn about this subject
This module and example are covered as required on private courses.
Should you wish to cover this example and associated subjects, and you're attending a public course
to cover other topics with us, please see our extra topic program.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering Python are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "this" training module. You'll find a description of the topic and some
other closely related examples on the "this" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. | |||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |