473,396 Members | 1,892 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,396 software developers and data experts.

events/callbacks - best practices

Greetings:

I'm been programming Delphi for a while and am trying to get into the
Python mindset. In Delphi, you can have a component class that has
properties, methods, and events. Properties and methods translate
pretty easily to Python but I'm not sure about the best way to handle
events. For example, from fxn retrbinary in ftplib there is:

while 1:
data = conn.recv(blocksize)
if not data:
break
callback(data)
conn.close()

In delphi this would be something like:

while 1:
data = conn.recv(blocksize)
if not data:
break
if assigned(OnProgress) then
callback(data)
conn.close()

In other words, the class can handle a number of events but passing a
callback function is not mandatory. If you want to handle an event,
it is assigned and dealt with.

I'm especially interested in non-visual components (like the ftplib)
and the examples I can find all deal with the events of visual
components that interjects more complexity than I'd like at this stage
of my knowledge.

My goal is to rewrite a number of non visual components in Python.
What I'd like to find is a 'non visual component design in Python'
guide somewhere. So... I'm more than willing to study if I can find
the resources - if you have any please lay them on me! Part of my
problem, I'm sure, is poor terminology use.

Of course, any general comments are welcomed - I'm not really
expecting a tutorial here, though :-)

Thanks!
Jul 18 '05 #1
3 3242
HankC wrote:
In delphi this would be something like:

while 1:
data = conn.recv(blocksize)
if not data:
break
if assigned(OnProgress) then
callback(data)
conn.close()

In other words, the class can handle a number of events but passing a
callback function is not mandatory. If you want to handle an event,
it is assigned and dealt with.


It's not fully clear to me exactly what you're asking, but it sounds
like the general answer is that the "callback" that you pass in can be
as complex as you want. Call it `handler':

handler.startProcessing()
while True:
data = ... get more
if not data and handler.notFinished():
break
if handler.acceptsData():
handler.accept(data)
conn.cloes()
handler.doneProcessing()

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ It's a winter day / Five years too late
\__/ En Vogue
Jul 18 '05 #2
HankC wrote:
Greetings:

I'm been programming Delphi for a while and am trying to get into the
Python mindset. In Delphi, you can have a component class that has
properties, methods, and events. Properties and methods translate
pretty easily to Python but I'm not sure about the best way to handle
events. For example, from fxn retrbinary in ftplib there is:

while 1:
data = conn.recv(blocksize)
if not data:
break
callback(data)
conn.close()

In delphi this would be something like:

while 1:
data = conn.recv(blocksize)
if not data:
break
if assigned(OnProgress) then
callback(data)
conn.close()

In other words, the class can handle a number of events but passing a
callback function is not mandatory. If you want to handle an event,
it is assigned and dealt with.

I'm especially interested in non-visual components (like the ftplib)
and the examples I can find all deal with the events of visual
components that interjects more complexity than I'd like at this stage
of my knowledge.

My goal is to rewrite a number of non visual components in Python.
What I'd like to find is a 'non visual component design in Python'
guide somewhere. So... I'm more than willing to study if I can find
the resources - if you have any please lay them on me! Part of my
problem, I'm sure, is poor terminology use.

Of course, any general comments are welcomed - I'm not really
expecting a tutorial here, though :-)

Thanks!


As of C++ Builder 3, Delphi events are just function pointers with IDE
support. IMHO it is useless to replicate this mechanism in Python. Just
make a base class with empty methods instead. Override instead of using
function pointers, if the behaviour need not change at runtime.

# delphi style, as far as I remember.
# now say this isn't bloated with a straight face :-)
class TDelphi:
def __init__(self):
self.OnStart = None
def start(self):
if self.OnStart:
self.OnStart(self)
d = TDelphi() # IDE

# callback would typically be a method of a form
class Form:
def MyOnStart(self, sender): # prototype generated by IDE
print "start 0"

f = Form() # IDE
d.OnStart = f.MyOnStart # IDE
d.start()

# oo style
class OO:
def start(self):
pass

# use case 1
o = OO()
def start():
print "start 1"
o.start = start
o.start()

# use case 2
class Override(OO):
def start(self):
print "start 2"
o = Override()
o.start()

Peter
Jul 18 '05 #3
HankC wrote:
Greetings:

I'm been programming Delphi for a while and am trying to get into the
Python mindset. In Delphi, you can have a component class that has
properties, methods, and events. Properties and methods translate
pretty easily to Python but I'm not sure about the best way to handle
events. For example, from fxn retrbinary in ftplib there is:

while 1:
data = conn.recv(blocksize)
if not data:
break
callback(data)
conn.close()

In delphi this would be something like:

while 1:
data = conn.recv(blocksize)
if not data:
break
if assigned(OnProgress) then
callback(data)
conn.close()

In other words, the class can handle a number of events but passing a
callback function is not mandatory. If you want to handle an event,
it is assigned and dealt with.

I'm especially interested in non-visual components (like the ftplib)
and the examples I can find all deal with the events of visual
components that interjects more complexity than I'd like at this stage
of my knowledge.

My goal is to rewrite a number of non visual components in Python.
What I'd like to find is a 'non visual component design in Python'
guide somewhere. So... I'm more than willing to study if I can find
the resources - if you have any please lay them on me! Part of my
problem, I'm sure, is poor terminology use.

Of course, any general comments are welcomed - I'm not really
expecting a tutorial here, though :-)

Thanks!


It's not clear what precisely you are looking for, but just some ideas:

class Foo(object):
def __init__(self):
self.__callback = None

def set_callback(self, callback):
self.__callback = callback

def fire(self, *args, **kw):
if self.__callback:
return self.__callback(*args, **kw)
else:
return self.default_callback(*args, **kw)

def default_callback(self, *args, **kw):
return "default_callback(%s, %s)" % (args, kw)

foo = Foo()
print foo.fire("me", bar = "oops")

def callback(*args, **kw):
return "callback(%s, %s)" % (args, kw)

foo.set_callback(callback)
print foo.fire("you", oops = "foo")
Properties may be even fancier. __call__ method might be useful too.

hth,
anton.

Jul 18 '05 #4

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

Similar topics

136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
8
by: Michael McDowell | last post by:
I'm confused: "why do we need to assign a method to a delegate then assign the delegate to an event why not just assign the method to the events event handler" and "how does making a...
1
by: Niklas | last post by:
Hi I have a remoting client/server application. The clients subscribes to a server event. All works fine if client is used as a window application, but when the client is used with No-Touch...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
4
by: Frank | last post by:
Hello. In the early stages of developing a server application that will be required to send events to multiple client applications that will be running on different PC's. I'm wondering what the...
5
by: sleepinglord | last post by:
An example: I have a select input. I wanna catch those onclick events which is not a onchange events. How to implement it? And in general, there's some basic kinds of events, and I wanna catch...
20
by: David Levine | last post by:
I ran into a problem this morning with event accessor methods that appears to be a bug in C# and I am wondering if this a known issue. public event SomeDelegateSignature fooEvent; public event...
6
by: Smithers | last post by:
Just looking to compile a list of "all the ways to implement events". I'm NOT looking to get into the merits or mechanics of each in this thread... just want to identify them all - good, bad, and...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.