[wxpython-users] Babel Fish translation ...

wxPython user mailing list, post #18,432
Author:
Date:
Subject:
 Stef Mientki
 2008-07-17 13:07:26
 [wxpython-users] Babel Fish translation ...
hello,

I've build a translation tool, to translate all strings in a python
source file.
As a extra gadget I added translation through Babel Fish,
using beautifulsoup.

Although it works functionally,
it can take lots of time waiting for the translation.

What I basically do is, after selecting a new string to be translated:

kwds = { 'trtext' : line_to_be_translated, 'lp' :'en_nl'}
soup = BeautifulSoup (urlopen(url, urlencode ( kwds ) ) )
translation= soup.find ( 'div', style='padding:0.6em;' ).string
self.Editor_Babel.SetLabel ( translation )

I'm using Python 2.5 and wxPython.

Probably I should use a separate thread, but that's above my knowledge.
I could also use a timer to check at regualr intervals when the
translation is ready,
but I've no idea how to implement that with the above code.

thanks,
Stef Mientki
_______________________________________________
wxpython-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Author:
Date:
Subject:
 Dominique
 2008-07-18 09:41:13
 [wxpython-users] Re: Babel Fish translation ...
Stef Mientki <s.mientki <at> ru.nl> writes:

>

>
> Probably I should use a separate thread, but that's above my knowledge.
> I could also use a timer to check at regualr intervals when the
> translation is ready,
> but I've no idea how to implement that with the above code.
>
> thanks,
> Stef Mientki

Hi Stef,

It was also beyond my knowledge some weeks ago... Someone better than me will
correct me or give you additional infos.

Have a look in the demo and in the wiki. DelayedResult is fine.

Basically, what you have to do is for example:
- import wx.lib.delayedresult as delayedresult

- call in your code the method that you want to get. You call it through:

self.handleGet(fn_Producer = self._resultProducer,
argProd = anArgumentForYourProducerFunction,
fn_Consumer= self._resultConsumer).
fn_Producer will do the long-running work.
fn_Consumer will provide you with the results that fn_Producer will send.

- You define handleGet:
def handleGet(self, fn_Producer, argProd, fn_Consumer):
"""Compute result in separate thread, doesn't affect GUI response.

delayedresult.startWorker(
consumer, workerFn, cargs=(), ckwargs={}, wargs=(), wkwargs={},
jobID=None, group=None, daemon=False, sendReturn=True, senderArg=None)
Convenience function to send data produced by workerFn(*wargs, **wkwargs)
running in separate thread, to a consumer(*cargs, **ckwargs) running
in the main thread.
"""
self.abortEvent.clear()
self.jobID += 1
print "Starting job %s in producer thread: GUI remains responsive" %
self.jobID
delayedresult.startWorker(fn_Consumer, fn_Producer,
wargs=(self.jobID,self.abortEvent,argProd),
jobID=self.jobID)

- Then you define fn_Consumer and fn_Producer:

def _resultProducer(self, jobID, abortEvent, argProd):
"""Pretend to be a complex worker function or something that takes
long time to run due to network access etc. GUI will freeze if this
method is not called in separate thread."""
#import time
count = 0
longRunningTask = None
while not abortEvent() and count < 1:
count += 1
# You will put your long-running code here:
longRunningTask = self.AnyMethodYouWishToCall()
return longRunningTask #jobID

def _resultConsumer(self, delayedResult):
"""
delayedResult.getJobID():
Return the jobID given when Sender initialized, or None if none given.

delayedResult.get():
Get the result. If an exception was sent instead of a result,
(via Sender's sendExcept()), that exception is raised.
Otherwise the result is simply returned.
"""
jobID = delayedResult.getJobID()
print "From Consumer self.jobID = ", self.jobID
assert jobID == self.jobID
try:
result = delayedResult.get()
except Exception, exc:
print "Result for job %s raised exception: %s" % (jobID, exc)
return
# output result
print "Got result for job %s: %s" % (jobID, result)
self.YouCanUseYourResultHereByCallingAnotherMethod(arg=result)


Although it looks a little bit complex at first glance, in fact, it is really
easy once you have made a few tries.

Let us know if you managed to use it.
Hope that helps...

Dominique



_______________________________________________
wxpython-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Author:
Date:
Subject:
 Stef Mientki
 2008-07-18 13:25:57
 Re: [wxpython-users] Re: Babel Fish translation ...
thanks Dominque,

it rather looks a bit difficult.
From the Python list I got a simpeler solution,
but as I don't know anything of threads,
I don't know if this is completely correct.
I'll start a new thread to show the solution.

cheers,
Stef


Dominique wrote:
> Stef Mientki <s.mientki <at> ru.nl> writes:
>
>
>
>
>> Probably I should use a separate thread, but that's above my knowledge.
>> I could also use a timer to check at regualr intervals when the
>> translation is ready,
>> but I've no idea how to implement that with the above code.
>>
>> thanks,
>> Stef Mientki
>>
>
> Hi Stef,
>
> It was also beyond my knowledge some weeks ago... Someone better than me will
> correct me or give you additional infos.
>
> Have a look in the demo and in the wiki. DelayedResult is fine.
>
> Basically, what you have to do is for example:
> - import wx.lib.delayedresult as delayedresult
>
> - call in your code the method that you want to get. You call it through:
>
> self.handleGet(fn_Producer = self._resultProducer,
> argProd = anArgumentForYourProducerFunction,
> fn_Consumer= self._resultConsumer).
> fn_Producer will do the long-running work.
> fn_Consumer will provide you with the results that fn_Producer will send.
>
> - You define handleGet:
> def handleGet(self, fn_Producer, argProd, fn_Consumer):
> """Compute result in separate thread, doesn't affect GUI response.
>
> delayedresult.startWorker(
> consumer, workerFn, cargs=(), ckwargs={}, wargs=(), wkwargs={},
> jobID=None, group=None, daemon=False, sendReturn=True, senderArg=None)
> Convenience function to send data produced by workerFn(*wargs, **wkwargs)
> running in separate thread, to a consumer(*cargs, **ckwargs) running
> in the main thread.
> """
> self.abortEvent.clear()
> self.jobID += 1
> print "Starting job %s in producer thread: GUI remains responsive" %
> self.jobID
> delayedresult.startWorker(fn_Consumer, fn_Producer,
> wargs=(self.jobID,self.abortEvent,argProd),
> jobID=self.jobID)
>
> - Then you define fn_Consumer and fn_Producer:
>
> def _resultProducer(self, jobID, abortEvent, argProd):
> """Pretend to be a complex worker function or something that takes
> long time to run due to network access etc. GUI will freeze if this
> method is not called in separate thread."""
> #import time
> count = 0
> longRunningTask = None
> while not abortEvent() and count < 1:
> count += 1
> # You will put your long-running code here:
> longRunningTask = self.AnyMethodYouWishToCall()
> return longRunningTask #jobID
>
> def _resultConsumer(self, delayedResult):
> """
> delayedResult.getJobID():
> Return the jobID given when Sender initialized, or None if none given.
>
> delayedResult.get():
> Get the result. If an exception was sent instead of a result,
> (via Sender's sendExcept()), that exception is raised.
> Otherwise the result is simply returned.
> """
> jobID = delayedResult.getJobID()
> print "From Consumer self.jobID = ", self.jobID
> assert jobID == self.jobID
> try:
> result = delayedResult.get()
> except Exception, exc:
> print "Result for job %s raised exception: %s" % (jobID, exc)
> return
> # output result
> print "Got result for job %s: %s" % (jobID, result)
> self.YouCanUseYourResultHereByCallingAnotherMethod(arg=result)
>
>
> Although it looks a little bit complex at first glance, in fact, it is really
> easy once you have made a few tries.
>
> Let us know if you managed to use it.
> Hope that helps...
>
> Dominique
>
>
>
> _______________________________________________
> wxpython-users mailing list
> [email protected]
> http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
>
>
>
_______________________________________________
wxpython-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users