473,378 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Dispatcher experiment

As an exercise in the use of dispatcher
I concocted a Zoo with some Animals, see below.
It works, but I think it is convoluted, obfuscated.

The idea is that in the Zoo each animal needs its own type of food,
and the Zoo should acknowledge a wish for that food,
as soon as the animal says it wants food.
The number and kind of animals (and their food) may vary.

The general problem is that you may have an unspecified number
of instances of some type, and that each instance must be recognized
and handled by the specific signal it sends into the world.

I would appeciate any comments or improvements on my design.
In a python shell you may test it with:
import dip
zoo = dip.Zoo()
zoo.animals["fish"].send_food_wish()

egbert

#!/usr/bin/env python
# dip.py := experiment with Patrick O'Brien's dispatcher

import wx.py.dispatcher as disp

class Animal(object):
def __init__(self, animal_food):
self.animal_food = animal_food

def send_food_wish(self):
# started by some event outside this class.
disp.send(signal=self.animal_food, sender=self)

class Zoo(object):
def __init__(self):
# dummies for some gui that accepts names of animals and their food:
animal_list = ["bird", "lion", "fish"]
food_list = ["bread", "meat", "plankton"]

# zoo administration: register animals and their food;
self.animals = {}
for animal,food in zip(animal_list, food_list):
animal_food_signal = (animal, food)
self.animals[animal] = Animal(animal_food_signal)
disp.connect(self.listen_to_food_wish, signal=animal_food_signal)

def listen_to_food_wish(self, signal, sender=disp.Any):
print "sender %s is %s and wishes to eat %s now" % \
(sender, signal[0], signal[1])

if __name__ == '__main__':
zoo = Zoo()
for animal in zoo.animals.values():
animal.send_food_wish()
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
================================================== ======================
Nov 8 '06 #1
0 1056

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sancha | last post by:
Hi, i am in a bit of a delima here. I need to submit an action through javascript so i used document.forms.action='/search.do?&submitaction=Add Minus' document.forms.submit(); since i am...
1
by: buells | last post by:
Hello Group, I'm pretty new to C-Sharp, so perhaps it's a well known problem and i didn't get the right words to search for in google etc. My Problem is: I wrote (well adopted it from Java) a...
1
by: CrazyCube | last post by:
does any body got some files about Orthogonal Experiment?? i really need it now ,if anybody got ,please send them to me my mailbox is : yuxh312@hotmail.com
54
by: CoreyWhite | last post by:
The following experiment is a demonstration of TIME TRAVEL. When writing this program, and testing it out I found that sometimes the program would engage itself in time travel but other times it...
0
by: Daniel Walton | last post by:
I have been working on a problem for a full week now. If someone could please help me it would be great. Even a clue would be great at this point. What I am seeing is quite a mystery. I have...
2
by: Indy | last post by:
Greetings. I am writing an asynchronous server, and I use the standard library's module asyncore. I subclass asyncore.dispatcher. handle_accept works just right, that is, when a client socket...
2
by: matt | last post by:
Could someone point me in the direction of creating my own C++ URL dispatcher? I'm looking for something similar to the Rails dispatcher so that I can do my own URL mapping. mod_rewrite is not...
4
by: hufaunder | last post by:
I have a WPF application that spins off a thread. That thread needs to show a dialog (InputDlg) to get some user input. The From my thread I am displaying the dialog as shown bellow. The problem is...
8
by: webster5u | last post by:
I'm design MVC structure for current developing system. Initially, the jsp will post data to servlet class. After processing, servlet will use the requestDispatcher to return processed data back to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.