Is it possible to connect an awaitable to a Future, basically turning it into a Task?

2018-10-27 Thread Russell Owen

I’m using asyncio and I’d like to add an item to an object that others 
can wait on immediately and which eventually I will want to use to track a 
coroutine. In other words I want something like:

class Info:
def __init__(self):
self.done_task = asyncio.Future()

info = Info()
# do other stuff, but eventually
coro = ...
asyncio.connect_future(coro, info.done_task)

I can certainly live without this, it simply requires adding an additional 
task made with asyncio.ensure_future and using that to set the result of 
done_task.

But it would be a lot more elegant to just have the one future (especially if 
I have to cancel the wait, as I have to keep the extra task around so I can 
cancel it). So...just wondering if I missed something.

Regards,

Russell


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: asyncio await different coroutines on the same socket?

2018-10-05 Thread Russell Owen
On Oct 3, 2018, Ian Kelly wrote
(in 
article):

> On Wed, Oct 3, 2018 at 7:47 AM Russell Owen  wrote:
> > Using asyncio I am looking for a simple way to await multiple events where
> > notification comes over the same socket (or other serial stream) in
> > arbitrary
> > order. For example, suppose I am communicating with a remote device that can
> > run different commands simultaneously and I don't know which command will
> > finish first. I want to do this:
> >
> > coro1 = start(command1)
> > coro2 = start(command2)
> > asyncio.gather(coro1, coro2)
> >
> > where either command may finish first. I’m hoping for a simple and
> > idiomatic way to read the socket and tell each coroutine it is done. So far
> > everything I have come up with is ugly, using multiple layers of "async
> > def”, keeping a record of Tasks that are waiting and calling "set_result"
> > on those Tasks when finished. Also Task isn’t even documented to have the
> > set_result method (though "future" is)
>
> Because Tasks are used to wrap coroutines, and the result of the Task
> should be determined by the coroutine, not externally.
>
> Instead of tracking tasks (that's what the event loop is for) I would
> suggest tracking futures instead. Have start(command1) return a future
> (or create a future that it will await on itself) that is not a task.
> Whenever a response from the socket is parsed, that code would then
> look up the corresponding future and call set_result on it. It might
> look something like this:
>
> class Client:
> async def open(self, host, port):
> self.reader, self.writer = await asyncio.open_connection(host, port)
> asyncio.create_task(self.read_loop())
>
> async def read_loop(self):
> while not self.reader.at_eof():
> response = self.reader.read()
> id = get_response_id(response)
> self._futures.pop(id).set_result(response)
>
> def start(self, command):
> future = asyncio.Future()
> self._futures[get_command_id(command)] = future
> self.writer.write(command)
> return future
>
> In this case start() is not a coroutine but its result is a future and
> can be awaited.

That is exactly what I was looking for. Thank you very much!

-- Russell

(My apologies for double posting -- I asked this question again today because 
I did not think my original question -- this one -- had gone through).


-- 
https://mail.python.org/mailman/listinfo/python-list


How to await multiple replies in arbitrary order (one coroutine per reply)?

2018-10-05 Thread Russell Owen


I am using asyncio and am fairly new to it. I have a stream to which I write 
commands and from which I read replies. (In this case the stream is custom 
wrapper around DDS written in C++ and pybind11). Multiple commands can run at 
the same time and I cannot predict which will finish first. I need aa 
different coroutine (or asyncio.Task or other awaitable object) for each 
command that is running.

Is there a simple way to handle this in asyncio? So far the best I have come 
up is the following (greatly simplified), which works but has some 
misfeatures:

import asyncio
from iolib import read_reply, write_command, TIMED_OUT

class RemoteCommand:
def __init__(self):
self._tasks = dict()

def start(self, cmd, timeout):
"""Start a command"""
cmd_id = write_command(cmd)
task = asyncio.ensure_future(self._wait_for_command(cmd_id=cmd_id, 
timeout=timeout))
self._tasks[cmd_id] = task
if len(self._tasks) == 1:
asyncio.ensure_future(self._handle_replies())
return task

async def _wait_for_command(self, cmd_id, timeout):
"""Wait for a command to finish"""
await asyncio.sleep(timeout)
if cmd_id in self._tasks:
del self._tasks[cmd_id]
return TIMED_OUT # our standard end code for timeouts

async def _handle_replies(self):
while True:
cmd_id, end_code = read_reply()
if cmd_id in self._tasks:
task = self._tasks.pop(cmd_id)
task.set_result(end_code)
if not self._tasks:
return
await asyncio.sleep(0.1)

Misfeatures include:
- asyncio.Task is not documented to have a "set_result" method. The 
documentation says that Task is "A Future-like object that runs a Python 
coroutine" and Future does have such a method.
- When "_handle_replies" calls "task.set_result(data)" this does not seem to 
cancel the "await asyncio.sleep(timeout)" in the task, resulting in scary 
messages to stdout. I have tried saving *that* as another task and canceling 
it, but it seems clumsy and I still see scary messages.

I think what I'm looking for is a task-like thing I can create that I can end 
when *I* say it's time to end, and if I'm not quick enough then it will time 
out gracefully. But maybe there's a simpler way to do this. It doesn't seem 
like it should be difficult, but I'm stumped. Any advice would be 
appreciated.

-- Russell


-- 
https://mail.python.org/mailman/listinfo/python-list


asyncio await different coroutines on the same socket?

2018-10-03 Thread Russell Owen

Using asyncio I am looking for a simple way to await multiple events where 
notification comes over the same socket (or other serial stream) in arbitrary 
order. For example, suppose I am communicating with a remote device that can 
run different commands simultaneously and I don't know which command will 
finish first. I want to do this:

coro1 = start(command1)
coro2 = start(command2)
asyncio.gather(coro1, coro2)

where either command may finish first. I’m hoping for a simple and 
idiomatic way to read the socket and tell each coroutine it is done. So far 
everything I have come up with is ugly, using multiple layers of "async 
def”, keeping a record of Tasks that are waiting and calling "set_result" 
on those Tasks when finished. Also Task isn’t even documented to have the 
set_result method (though "future" is)

Is there a simple, idiomatic way to do this?

-- Russell


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Anaconda with Python 3.7

2018-09-28 Thread Russell Owen
On Sep 3, 2018, gvim wrote
(in article <5b8d0122.1030...@gmail.com>):

> Anyone have any idea when Anaconda might ship a version compatible with
> Python 3.7. I sent them 2 emails but no reply.

I heard a rumor today that it will be a few more months. They are short on 
resources and are also dealing with issues with dependency management.

In any case miniconda is available for 3.7 so it is worth checking to see if 
it has the packages that you need. (And if it’s just missing a few you can 
see if pip will install those).

-- Russell


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-11 Thread Russell Owen

On 8/10/16 3:44 PM, Juan Pablo Romero Méndez wrote:

As to why I asked that, there are several reasons: I have a very concrete
need right now to find pragmatic ways to increase code quality, reduce
number of defects, etc. in a Python code base. But also I want to
understand better the mind set and culture of Python's community.


I am late to this thread, so my apologies for duplicated answers, but I 
have two concrete suggestions:
- Unit tests. These are a hassle to write, but pay huge dividends in 
robustness of your existing code and making it safer to modify the code 
later. There are also tools to measure test coverage which are worth 
considering. I don't think it is possible to write robust code in any 
language (even compiled languages) without a good test suite.
- Always run a linter such as flake8. Most source code editors can be 
configured to do this automatically. This will not catch everything that 
a compiler would catch in a compiled language, but it will catch many 
common errors.


-- Russell

--
https://mail.python.org/mailman/listinfo/python-list


Re: tkinter resize question

2015-07-17 Thread Russell Owen

On 7/17/15 12:17 PM, nickgeova...@gmail.com wrote:

On Friday, July 17, 2015 at 1:53:19 PM UTC-5, nickge...@gmail.com wrote:

Resizing a tkinter window which contains a frame which contains a button 
widget, will not change the current size of the window, frame or button as 
recorded in their height and width attributes (at least not if they are 
resizable). What is the correct way to detect their current size?


Ok, partially answering my own question:
The geometry of the window will change (win.geometry()), but the changes do not appear to 
"propagate" to the retrieved width/height of the child widgets, frames, etc. Or 
am I incorrect with this?


I'm not seeing it. If I try the following script I see that resizing the 
widget does update frame.winfo_width() and winfo_height. (I also see 
that the requested width and height are ignored; you can omit those).


-- Russell


#!/usr/bin/env python
import Tkinter
root = Tkinter.Tk()

frame = Tkinter.Frame(root, width=100, height=50)
frame.pack(expand=True, fill="both")
def doReport(*args):
print "frame actualwidth=%s, height=%s" % (frame.winfo_width(), 
frame.winfo_height())
print "frame requested width=%s, height=%s" % 
(frame.winfo_reqwidth(), frame.winfo_reqheight())

button = Tkinter.Button(frame, text="Report", command=doReport)
button.pack()

root.mainloop()


--
https://mail.python.org/mailman/listinfo/python-list


Re: Quick question, if you please

2015-03-31 Thread Russell Owen

On 3/31/15 10:09 AM, John Kelly wrote:

Pythonites,

I received Python with another install and my update software keeps
signaling I need to install a newer version, and once I do, the older
version is still there, so I keep getting told I need to update. Should
I be able to uninstall the old version each time?

Thanks for your kind attention,
John Kelly


I would need more information to help. What operating system are you on? 
How and where are you installing Python (and what do you mean by 
"received Python with another install"?).


-- Russell

--
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to calculate fraction part of x?

2015-03-26 Thread Russell Owen

On 3/24/15 6:39 PM, Jason Swails wrote:



On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille mailto:em...@fenx.com>> wrote:

On 3/23/2015 5:52 AM, Steven D'Aprano wrote:

Are there any other, possibly better, ways to calculate the
fractional part
of a number?


float (("%6.3f" % x)[-4:])


​In general you lose a lot of precision this way...​


I suggest modf in the math library:

math.modf(x)
Return the fractional and integer parts of x. Both results carry the 
sign of x and are floats.




--
https://mail.python.org/mailman/listinfo/python-list


Re: Picking apart a text line

2015-03-02 Thread Russell Owen

On 2/26/15 7:53 PM, memilanuk wrote:

So... okay.  I've got a bunch of PDFs of tournament reports that I want
to sift thru for information.  Ended up using 'pdftotext -layout
file.pdf file.txt' to extract the text from the PDF.  Still have a few
little glitches to iron out there, but I'm getting decent enough results
for the moment to move on.


...

So back to the lines of text I have stored as strings in a list.  I
think I want to convert that to a list of lists, i.e. split each line
up, store that info in another list and ditch the whitespace.  Or would
I be better off using dicts?  Originally I was thinking of how to
process each line and split it them up based on what information was
where - some sort of nested for/if mess.  Now I'm starting to think that
the lines of text are pretty uniform in structure i.e. the same field is
always in the same location, and that list slicing might be the way to
go, if a bit tedious to set up initially...?

Any thoughts or suggestions from people who've gone down this particular
path would be greatly appreciated.  I think I have a general
idea/direction, but I'm open to other ideas if the path I'm on is just
blatantly wrong.


It sounds to me as if the best way to handle all this is keep the 
information it in a database, preferably one available from the network 
and centrally managed, so whoever enters the information in the first 
place enters it there. But I admit that setting such a thing up requires 
some overhead.


Simpler alternatives include using SQLite, a simple file-based database 
system, or numpy structured arrays (arrays with named fields). Python 
includes a standard library module for sqlite and numpy is easy to install.


-- Russell

--
https://mail.python.org/mailman/listinfo/python-list


Re: How to install PIL or PILLOW on OS X Yosemite?

2015-02-19 Thread Russell Owen

On 2/15/15 8:17 PM, Ned Deily wrote:

In article ,
  KP  wrote:

just upgraded my Mac Mini to Yosemite and have never dabbled in Python on
this OS.

I see it has Python 2.7.6 installed.

When I do something like

from PIL import ImageFont, ImageDraw

it tells me that it cannot find PIL

How do I install this on Yosemite?


Suggestions: stick with Pillow which is the current, maintained fork of
the venerable PIL. Decide whether you want to use Python 3 or Python 2.
PIL/Pillow installation on OS X is more involved than on some other
platforms because it depends on a number of third-party C libraries that
are not shipped by Apple in OS X so you need to find another source for
them.  Rather than trying to build and install everything yourself or
downloading a Pillow or PIL installer, I suggest picking one of the
several fine distributors of open source packages for OS X and
installing everything you need from them (including an up-to-date Python
2 or 3) and for your future needs beyond Pillow; options include
Homebrew, MacPorts, Anaconda, Fink, and others.  Once you've installed
the base framework for the package manager you choose, installing
something like Pillow and all of its dependencies is often just a
one-line command.  It may take a little while to get used to the quirks
of the package manager you choose but, if you are going to use OS X for
development with Python or many other languages, that time spent will be
repaid many times over.


I agree that Pillow is preferable to PIL and that you may want to 
consider a 3rd party system.


If you are primarily interested in Python (and not unix-based C/C++ 
libraries and utilities then I suggest you try anaconda python.


Homebrew, MacPorts and Fink are mostly aimed at people who want to add 
missing unix libraries and tools.


If you want to stick with python.org python then a binary PIL installer 
is available here:


(I am not aware of any Pillow binaries).

-- Russell

--
https://mail.python.org/mailman/listinfo/python-list


Re: Generator problem: parent class not seen

2012-02-01 Thread Russell Owen
On Feb 1, 2012, at 3:35 PM, Arnaud Delobelle wrote:
> On Feb 1, 2012 9:01 PM, "Russell E. Owen"  wrote:
> >
> > I have an odd and very intermittent problem in Python script.
> > Occasionally it fails with this error:
> >
> > Traceback (most recent call last):
> >  File
> > "/Applications/APO/TTUI.app/Contents/Resources/lib/python2.7/TUI/Base/Bas
> > eFocusScript.py", line 884, in run
> >  File
> > "/Applications/APO/TTUI.app/Contents/Resources/lib/python2.7/TUI/Base/Bas
> > eFocusScript.py", line 1690, in initAll
> > TypeError: unbound method initAll() must be called with BaseFocusScript
> > instance as first argument (got ScriptClass instance instead)
> > self=; class hierarchy=[( > 'TUI.Base.BaseFocusScript.ImagerFocusScript'>, ( > 'TUI.Base.BaseFocusScript.BaseFocusScript'>,)), [(,
> > (,))]]
> >
> 
> Looks like you have loaded the same module twice.  So you have two versions 
> of your class hierarchies. You can check by printing the ids of your classes. 
> You will get classes with the same name but different ids.
> 
> Arnaud
> 

Yes! I was reloading BaseFocusScript. Oops.

In detail: script files are dynamically loaded when first requested and can be 
reloaded for debugging. I think that's safe because script files are 
self-contained (e.g. the classes in them are never subclassed or anything like 
that). But I went too far: I had my focus scripts reload BaseFocusScript, which 
is shared code, so that I could tweak BaseFocusScript while debugging focus 
scripts.

Thank you very much!

-- Russell-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generator problem: parent class not seen

2012-02-01 Thread Russell Owen
On Feb 1, 2012, at 2:34 PM, Chris Rebert wrote:

> On Wed, Feb 1, 2012 at 1:00 PM, Russell E. Owen  wrote:
>> I have an odd and very intermittent problem in Python script.
>> Occasionally it fails with this error:
>> 
>> Traceback (most recent call last):
>>  File
>> "/Applications/APO/TTUI.app/Contents/Resources/lib/python2.7/TUI/Base/Bas
>> eFocusScript.py", line 884, in run
>>  File
>> "/Applications/APO/TTUI.app/Contents/Resources/lib/python2.7/TUI/Base/Bas
>> eFocusScript.py", line 1690, in initAll
>> TypeError: unbound method initAll() must be called with BaseFocusScript
>> instance as first argument (got ScriptClass instance instead)
> 
>> The code looks like this:
>> 
>>def run(self, sr):
>>try:
>>self.initAll()
> 
>> I am puzzled why Python thinks the class type is wrong, given the output
>> of inspect.getclasstree. Any ideas on what might be wrong and how to
>> track it down (and why it would be so intermittent)?
> 
> What's the offending line of initAll() [#1690 in BaseFocusScript.py]
> look like? The lines preceding it would also be helpful for context.

Here you go. The offending line #169 is marked with ***

-- Russell

class ImagerFocusScript(BaseFocusScript):
   """..."""
def __init__(self,
sr,
instName,
imageViewerTLName = None,
defRadius = 5.0,
defBinFactor = 1,
maxFindAmpl = None,
doWindow = False,
windowOrigin = 1,
windowIsInclusive = True,
doZeroOverscan = False,
helpURL = None,
debug = False,
):
...
BaseFocusScript.__init__(self,
sr = sr,
gcamActor = gcamActor,
instName = instName,
imageViewerTLName = imageViewerTLName,
defRadius = defRadius,
defBinFactor = defBinFactor,
maxFindAmpl = maxFindAmpl,
doWindow = doWindow,
windowOrigin = windowOrigin,
windowIsInclusive = windowIsInclusive,
helpURL = helpURL,
debug = debug,
)
self.doZeroOverscan = bool(doZeroOverscan)


def initAll(self):
"""Override the default initAll to record initial bin factor, if 
relevant
"""
***   BaseFocusScript.initAll(self)
if self.exposeModel.instInfo.numBin > 0:
self.finalBinFactor = self.exposeModel.bin.getInd(0)[0]


Also, here is BaseFocusScript:

class BaseFocusScript(object):
"""Basic focus script object.

This is a virtual base class. The inheritor must:
- Provide widgets
- Provide a "run" method
"""
cmd_Find = "find"
cmd_Measure = "measure"
cmd_Sweep = "sweep"

# constants
#DefRadius = 5.0 # centroid radius, in arcsec
#NewStarRad = 2.0 # amount of star position change to be considered a new 
star
DefFocusNPos = 5  # number of focus positions
DefFocusRange = 200 # default focus range around current focus
FocusWaitMS = 1000 # time to wait after every focus adjustment (ms)
BacklashComp = 0 # amount of backlash compensation, in microns (0 for none)
WinSizeMult = 2.5 # window radius = centroid radius * WinSizeMult
FocGraphMargin = 5 # margin on graph for x axis limits, in um
MaxFocSigmaFac = 0.5 # maximum allowed sigma of best fit focus as a 
multiple of focus range
MinFocusIncr = 10 # minimum focus increment, in um
def __init__(self,
sr,
gcamActor,
instName,
tccInstPrefix = None,
imageViewerTLName = None,
defRadius = 5.0,
defBinFactor = 1,
finalBinFactor = None,
canSetStarPos = True,
maxFindAmpl = None,
doWindow = True,
windowOrigin = 0,
windowIsInclusive = True,
helpURL = None,
debug = False,
):
""""""
self.sr = sr
self.sr.debug = bool(debug)
self.gcamActor = gcamActor


def initAll(self):
"""Initialize variables, table and graph.
"""
# initialize shared variables
self.doTakeFinalImage = False
self.focDir = None
self.currBoreXYDeg = None
self.begBoreXYDeg = None
self.instScale = None
self.arcsecPerPixel = None
self.instCtr = None
self.instLim = None
self.cmdMode = None
self.focPosToRestore = None
self.expTime = None
self.absStarPos = None
self.relStarPos = None
self.binFactor = None
self.window = None # LL pixel is 0, UR pixel is included

self.enableCmdBtns(False)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] RELEASED Python 2.6.2

2009-04-17 Thread Russell Owen

On Apr 16, 2009, at 11:17 PM, Ronald Oussoren wrote:


On 16 Apr, 2009, at 20:58, Russell Owen wrote:

I installed the Mac binary on my Intel 10.5.6 system and it works,  
except it still uses Apple's system Tcl/Tk 8.4.7 instead of my  
ActiveState 8.4.19 (which is in /Library/Frameworks where one would  
expect).


That's very string. I had ActiveState 8.4 installed (whatever was  
current about a month ago).


I agree. (For what it's worth, you probably have Tcl/Tk 8.4.19 -- a  
version I've found to be very robust. 8.4.19 was released awhile ago  
and is probably the last version of 8.4 we will see, since all  
development is happening on 8.5 now).


Could you try a simple experiment (assuming you still have ActiveState  
Tcl/Tk installed): run python from the command line and enter these  
commands:

import Tkinter
root = Tkinter.Tk()

Then go to the application that comes up and select About Tcl/Tk...  
(in the Python menu) and see what version it reports. When I run with  
the Mac binary of 2.6.2 it reports 8.4.7 (Apple's built-in python).  
When I build python 2.6.2 from source it reports 8.4.19 (my  
ActiveState Tclc/Tk).


Just out of curiosity: which 3rd party Tcl/Tk did you have  
installed when you made the installer? Perhaps if it was 8.5 that  
would explain it. If so I may try updating my Tcl/Tk -- I've been  
wanting some of the bug fixes in 8.5 anyway.


Tcl 8.5 won't happen in 2.6, and might not happen in 2.7 either.   
Tkinter needs to work with the system version of Tcl, which is some  
version of 8.4,  Tkinter will not work when the major release of Tcl  
is different than during the compile. That makes it rather hard to  
support both 8.4 and 8.5 in the same installer.


Perfect. I agree.

-- Russell

--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] RELEASED Python 2.6.2

2009-04-16 Thread Russell Owen
I installed the Mac binary on my Intel 10.5.6 system and it works,  
except it still uses Apple's system Tcl/Tk 8.4.7 instead of my  
ActiveState 8.4.19 (which is in /Library/Frameworks where one would  
expect).


I just built python from source and that version does use ActiveState  
8.4.19.


I wish I knew what's going on. Not being able to use the binary  
distros is a bit of a pain.


Just out of curiosity: which 3rd party Tcl/Tk did you have installed  
when you made the installer? Perhaps if it was 8.5 that would explain  
it. If so I may try updating my Tcl/Tk -- I've been wanting some of  
the bug fixes in 8.5 anyway.


-- Russell

On Apr 16, 2009, at 5:35 AM, Ronald Oussoren wrote:



On 15 Apr, 2009, at 22:47, Russell E. Owen wrote:


Thank you for 2.6.2.

I see the Mac binary installer isn't out yet (at least it is not  
listed
on the downloads page). Any chance that it will be compatible with  
3rd

party Tcl/Tk?


The Mac installer is late because I missed the pre-announcement of  
the 2.6.2 tag. I sent the installer to Barry earlier today.


The installer was build using a 3th-party installation of Tcl/Tk.

Ronald


--
http://mail.python.org/mailman/listinfo/python-list


Re: static object

2007-01-03 Thread Russell Owen
In article <[EMAIL PROTECTED]>,
 meelab <[EMAIL PROTECTED]> wrote:

> Dear All,
> 
> I am looking for a way to create a "static object" or a "static class" -
> terms might be inappropriate - having for instance:
> 
> class StaticClass:
> .
> .
> 
> and then
> staticObject1 = StaticClass()
> staticObject2 = StaticClass()
> 
> so that staticObject1 and staticObject2 refers exactly to the same
> instance of object.

Personally I do the following (in its own module). There may be a better 
way, but this is simple and it works:

_theSingleton = None

def getSingleton():
   global _theSingleton
   if not _theSingleton:
  _theSingleton = _Singleton()
   return _theSingleton

class _Singleton:
   def __init__(self, ...):
  ...


-- Russell
-- 
http://mail.python.org/mailman/listinfo/python-list