[Tutor] XML-RPC data transfers.

2006-12-28 Thread Chris Hengge

I'm trying to figure out how to send data using XML-RPC. Currently my target
is to send a PIL imagegrab.grab() from one side of the connection to the
other. I've got stuff like numbers and strings going back and forth, so I
know my connection is working, but I'm not sure how to send the image.

'Server and Client Connect'
'Server asks client for screen capture, client captures and sends to
server.'

I've been trying to figure it out using this bit of information I found, but
I'm not sure how to map it to what I need:
http://www.velocityreviews.com/forums/t343990-xmlrpc-send-file.html

Using this example I get error's about 'expected binary .read(), but got
instance instead. ' when trying to convert the image data to binary to
transfer, I've also just tried passing the image data directly, but that
gets a return 'None" exception.

I've just been using xmlrpclib and simplexmlrpcserver for this, but I'm
wondering if I should perhaps use twisted instead.

Happy Holiday's and Thanks.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] subprocess.Popen.communicate()[]

2006-12-28 Thread Isaac

Hello tutors!

I have the following in my PYTHONSTARTUP file

def clear():
   x = subprocess.Popen("clear").communicate(None)[0]
   return x

so that when I type:

clear()

at the prompt, my terminal screen clears and I get only my prompt at the top
of the screen.

without the index at the end of .communicate() I get a tuple '(None, None)'
before my prompt at the top of the cleared screen. If I call either x[0] or
x[1] the command will return a single None, which is not displayed in the
interpreter but if I leave off the index".communicate(None)"- so that it
returns both  stdout and sterr then
clear() will return a tuple which will be displayed by the interpreter.

Am in correct in my interpretation of the interpreter?
Thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XML-RPC data transfers.

2006-12-28 Thread Alan Gauld
"Chris Hengge" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> I'm trying to figure out how to send data using XML-RPC. Currently 
> my target
> is to send a PIL imagegrab.grab() from one side of the connection to 
> the
> other.

Any particular reason to use RPC here?
This will likely be a slow process and not really what I expect
to see RPCs being used for. Normally I'd expect to send a url
to the server and it would then fetch the file using ftp or similar.

Tying up an XML-RPC server and blocking the sending side
for a long time(10-30s?) is a risky procedure, especially given the
fragile nature of XML-RPC (compared to traditional techniques,
or even to SOAP)

Just a thought,

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen.communicate()[]

2006-12-28 Thread Alan Gauld

"Isaac" <[EMAIL PROTECTED]> wrote in 
> I have the following in my PYTHONSTARTUP file
> 
> def clear():
>x = subprocess.Popen("clear").communicate(None)[0]
>return x
> 
> Am in correct in my interpretation of the interpreter?

Yes, but can't you simplify this to just:


import subprocess
def clear():
subProcess.Popen('clear')

if all you want to do is clear the screen...


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python and rpy

2006-12-28 Thread Geoframer

Hey Kent,

Well i've gotten exactly no response on the Rpy-list on both my questions
:-(.

However i switched to Ubuntu 6.10 today (from WinXP) and to my suprise it
does work under linux! :-)

RHOME= /usr/lib/R
RVERSION= 2.3.1
RVER= 2031
RUSER= /home/geofram
Loading Rpy version 2031 .. Done.
Creating the R object 'r' ..  Done

In [2]: r.diag(2)
Out[2]:
array([[ 1.,  0.],
  [ 0.,  1.]])

In [3]: r.kmeans(r.diag(2),1,2,5,"Forgy")
Out[3]:
{'centers': array([   [ 0.5,  0.5]]),
'cluster': [1, 1],
'size': 2,
'withinss': 1.0}

Older version somewhat but apparently this works, so it might just be a bug
under windows or in the newer version from Rpy... For now at least I can
work with it.

Thanx a bunch for all your help Kent.

Regards - Geofram

On 12/25/06, Kent Johnson <[EMAIL PROTECTED]> wrote:


Geoframer wrote:

> As i stated i think the conversion from Python to R is going wrong, but
> i have no clue on how to properly address that.

I agree. If you have Numeric installed (which I do) then r.diag() should
return a Numeric array, not a list of lists. This looks like the same
problem discussed in this thread:
http://sourceforge.net/mailarchive/message.php?msg_id=15307721
which does not seem to have been resolved.

I don't want to get involved in the Rpy mailing list but I would suggest
posting a very simple question asking why the result of r.diag() is not
a Numeric array. Perhaps this will lead to a solution.

> The code snippet i was talking about is on page 15 and 16 of the rpy
> reference guide http://rpy.sourceforge.net/rpy/doc/rpy.pdf ; the
> examples just don't work and i am lacking enough python experience to
> see why :-S.
>
> What i'm trying to do now is :
>
> 
> from rpy import *
>
> class Test:
> def as_r(self):
> return
[[1,2,3,4,5],[2,3,4,5,1],[3,4,5,1,2],[4,5,1,2,3],[5,1,2,3,4]]
>
> if __name__ == "__main__":
> a=with_mode(NO_CONVERSION, Test)()
> r.kmeans(a, 2, 5, 10, "Forgy")

I don't see how this will help. My understanding is that as_r() should
return an Robj, not a Python object. I think this code is the same as
passing a list of lists directly to kmeans.

Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why a global?

2006-12-28 Thread Carlos
Chris,

Sheesh, that solved it, guess that I got to learn OO next. Unles it is 
not related... :-[

Thanks a lot!
Carlos
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why a global?

2006-12-28 Thread Alan Gauld

"Carlos" <[EMAIL PROTECTED]> wrote
>
> Sheesh, that solved it, guess that I got to learn OO next. Unles it 
> is
> not related... :-[

The problem you had was about reassigning a name from a
module to an object, but it could have been any kind of
object - a variable, a function, another module, anything.
So really it had nothing to do with OO.

If  you do:

import sys as s
s = 42
s.exit()

you will get an error because you renamed sys to s then
used s as a local varuiable, thus overwriting (and thus losing)
the module reference. You need to ensure that your names
don't collide.

Alan G 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Length of longest item in a list, using a list comp

2006-12-28 Thread Tony Cappellini

I want to use a list comp to get the length of the longest string in a list,
but can't quite get the syntax right.

l1=['abc', 'abcde', 'abcfdtea']

longest=0
[x for x in l1 if len(x) > longest]

The problem is I can't add the true clause to the if statement in a list
comp as in
if len(x) > longest:
  longest = len(x)


Is this possible using a list comp?


thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Length of longest item in a list, using a list comp

2006-12-28 Thread Python
On Thu, 2006-12-28 at 11:27 -0800, Tony Cappellini wrote:
> 
> 
> I want to use a list comp to get the length of the longest string in a
> list, but can't quite get the syntax right.
> 
> l1=['abc', 'abcde', 'abcfdtea']
> 
> longest=0
> [x for x in l1 if len(x) > longest] 

Use max to get the longest

longest = max([len(x) for x in ll])

With versions >= 2.4 you can omit the []

> 
> The problem is I can't add the true clause to the if statement in a
> list comp as in
> if len(x) > longest:
>longest = len(x)
> 
> 
> Is this possible using a list comp?
> 
> 
> thanks
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Length of longest item in a list, using a list comp

2006-12-28 Thread Andreas Kostyrka
* Python <[EMAIL PROTECTED]> [061228 20:44]:
> On Thu, 2006-12-28 at 11:27 -0800, Tony Cappellini wrote:
> > 
> > 
> > I want to use a list comp to get the length of the longest string in a
> > list, but can't quite get the syntax right.
> > 
> > l1=['abc', 'abcde', 'abcfdtea']
> > 
> > longest=0
> > [x for x in l1 if len(x) > longest] 
> 
> Use max to get the longest
> 
> longest = max([len(x) for x in ll])
> 
> With versions >= 2.4 you can omit the []
With 2.5 you can even do stuff like that:

>>> x=[range(5), range(3), range(7)]
>>> max(x, key=lambda i: len(i))
[0, 1, 2, 3, 4, 5, 6]
>>> 

Andreas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Length of longest item in a list, using a list comp

2006-12-28 Thread Tony Cappellini

Thanks,
but I am restricted to using 2.3.4 for now, so

longest = max([len(x) for x in ll])

works for me


On 12/28/06, Andreas Kostyrka <[EMAIL PROTECTED]> wrote:


* Python <[EMAIL PROTECTED]> [061228 20:44]:
> On Thu, 2006-12-28 at 11:27 -0800, Tony Cappellini wrote:
> >
> >
> > I want to use a list comp to get the length of the longest string in a
> > list, but can't quite get the syntax right.
> >
> > l1=['abc', 'abcde', 'abcfdtea']
> >
> > longest=0
> > [x for x in l1 if len(x) > longest]
>
> Use max to get the longest
>
> longest = max([len(x) for x in ll])
>
> With versions >= 2.4 you can omit the []
With 2.5 you can even do stuff like that:

>>> x=[range(5), range(3), range(7)]
>>> max(x, key=lambda i: len(i))
[0, 1, 2, 3, 4, 5, 6]
>>>

Andreas

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Length of longest item in a list, using a list comp

2006-12-28 Thread Kent Johnson
Andreas Kostyrka wrote:
> With 2.5 you can even do stuff like that:
> 
 x=[range(5), range(3), range(7)]
 max(x, key=lambda i: len(i))
> [0, 1, 2, 3, 4, 5, 6]

No need for the lambda, just use
max(x, key=len)

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] OT: Python 2.5 (Was Re: Length of longest item in a list, using a list comp)

2006-12-28 Thread Alan Gauld
"Andreas Kostyrka" <[EMAIL PROTECTED]> wrote 

>> With versions >= 2.4 you can omit the []
> With 2.5 you can even do stuff like that:

I missed the announcement somewhere but 2.5 seems to 
have been out for a spell now.

What are the must-have new features? (I could read the what's-new 
doc but that tells me about stuff I may not see the value in!)

What are the features people are actually using regularly and find 
an improvement?

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: Python 2.5 (Was Re: Length of longest item in a list, using a list comp)

2006-12-28 Thread Chris Hengge

I hope this is related enough for this thread, but I'm curious why people
didn't seem to unanimously jump into 2.5 upon release. Python seems very
good about holding its backward compatibility vs some other languages I've
dealt with like C# that seems to require applications rewritten with every
patch. Was there just nothing that "grand" about the new version? I've
personally held back just because most of the documentation I've come across
is for 2.4, and until I get a firmer feel for the language I'm trying to not
mix things up.

On 12/28/06, Alan Gauld <[EMAIL PROTECTED]> wrote:


"Andreas Kostyrka" <[EMAIL PROTECTED]> wrote

>> With versions >= 2.4 you can omit the []
> With 2.5 you can even do stuff like that:

I missed the announcement somewhere but 2.5 seems to
have been out for a spell now.

What are the must-have new features? (I could read the what's-new
doc but that tells me about stuff I may not see the value in!)

What are the features people are actually using regularly and find
an improvement?

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: Python 2.5 (Was Re: Length of longest item in a list, using a list comp)

2006-12-28 Thread Terry Carroll
On Thu, 28 Dec 2006, Chris Hengge wrote:

> I hope this is related enough for this thread, but I'm curious why people
> didn't seem to unanimously jump into 2.5 upon release. 

I find the Activestate distribution particularly convenient, so I'm 
awaiting that.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: Python 2.5 (Was Re: Length of longest item in a list, using a list comp)

2006-12-28 Thread wesley chun
> What are the must-have new features? (I could read the what's-new
> doc but that tells me about stuff I may not see the value in!)
>
> What are the features people are actually using regularly and find
> an improvement?

below is a quick summary of the 2.5 highlights.  of these, i like/use
the following:
1, 2, 4, 5, 6, 7, (8), 9a, 9b.  of course, my workplace is still on
2.3, and i've been "dying" to use generator expressions (2.4),
conditional expressions, and executing modules as scripts (easier to
run pdb directly on a module).

Python 2.5 Summary
=
1) Conditional Expressions/Ternary Operators (PEP 308) -- finally!  :-)
C ? T : F --> (C and [T] or [F])[0] --> T if C else F

2) Partial Function Application (PEP 309) -- adds to Python support
for currying and generalized partial function application:

from...

from operator import add
def add2(x):
return add(2, x)

to...

from operator import add
from functools import partial
add2 = partial(add, 2)

demo... (this is a short example of what it does... anyone have a more
*useful* example?)

>>> from operator import add
>>> def add2(x): return add(2, x)

>>> add2(5)
7
>>> from functools import partial
>>> add2b = partial(add, 2)
>>> add2b(5)
7

3) Absolute and Relative Imports (PEP 328) -- meant to address
intra-package import problems, esp. with std lib module name conflicts

4) Executing Modules as Scripts (PEP 338) -- new -m option very useful
for executing std lib modules as scripts

5) Unified try/except/finally (PEP 341) -- probably will be used a lot
since it turns a multi-level try-try-except-finally block into a
single-level try-except-finally block

6) The 'with' statement (PEP 343) - somewhat related to just above...
simplifies try-except-finally even further but "subsuming" the finally
clause (see context mgrs below) so you need just the single with
clause (and try-except if nec.).  popular uses:
- need to have an open file (open it now and to close it later
automagically)
- need to acquire a lock (now and release it later automagically)
- need to obtain a database pool cxn (now and release it later
automagically)

7) Enhanced Generator Features (PEP 342) -- now you can "talk-back" to
a generator, meaning you can send data into it as you resume it...
.next() sends None, .send(obj) sends obj, .throw(exc) throws exception
exc

8) Exceptions as New-Style Classes -- all exceptions converted to new classes

9) new modules:
a) sqlite3 - probly used heavily due to interest in this lightweight RDBMS
b) ElementTree - probly used even *more* heavily due to interest
in this lightweight XML DOMish processor
c) ctypes - useful for those that want to interface directly to a
.so or .dll without writing extension wrappers
d) wsgiref - some reference tools for the new WSGI standard (for web apps)
e) hashlib - replaces most encryption modules
f) contextlib - used to write "context mgrs" for use with the 'with' stmt

for more demos and information about many of these features, check out
"Core Python Programming."  :-)

cheers,
-wesley


-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen.communicate()[]

2006-12-28 Thread Isaac
> "Isaac"
> > I have the following in my PYTHONSTARTUP file
> >
> > def clear():
> >x = subprocess.Popen("clear").communicate(None)[0]
> >return x
> >
>
> Yes, but can't you simplify this to just:
>
>
> import subprocess
> def clear():
> subProcess.Popen('clear')
>
> if all you want to do is clear the screen...
>

Yes, but this way the screen more unix like after calling the
function- it is cleared and only my prompt is shown at the top of the
screen; which is currently ' python > '
Without communicate()[0] the screen is blank and no prompt.
Thanks for your reply.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor