how two join and arrange two files together

2009-07-18 Thread amrita

Hi,

I have two files having entries like:--
fileA
8  ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39
15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18
23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93
and
fileB
ChainA: ALA8-67.217297  -37.131330
ChainA: ALA21   -69.822977  -48.871282
ChainA: ALA23   -59.148095  -46.540043
ChainA: ALA33   -65.459303  -43.269718

i want to join thses two files in such a way that the output file will
contain column of both the files and the enties of similar position of ALA
will be together.so the output file should look something like:
fileC
8  ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 ChainA:
ALA8-67.217297  -37.131330
15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18
   ChainA:
ALA21   -69.822977  -48.871282
23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 ChainA:
ALA23   -59.148095  -46.540043
   ChainA:
ALA33   -65.459303  -43.269718



Thanks,
Amrita Kumari
Research Fellow
IISER Mohali
Chandigarh
INDIA

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


Python import Error

2009-07-18 Thread Kalyan Chakravarthy
Hi All,
   I am using* Python 2.6, MySQL 4.0* , I have successfully
Instaled MySQLdb (*MySQL-python-1.2.3c1.win32-py2.6*) in my system. I tested
through command prompt with "import MySQLdb ", its not shwing any errors
(means its instaled successfully), I set Eneceranment variable for Python

I  am running simple application with import "MySQLdb" to get the connection
to MySQL, But its throwing "No module named MySQLdb", Please any one tell me
what couled be the reasion

is there any version miss match with python and MySQL ?

Since last 3days I am strugling on this

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


Re: how two join and arrange two files together

2009-07-18 Thread Chris Rebert
On Sat, Jul 18, 2009 at 12:09 AM,  wrote:
>
> Hi,
>
> I have two files having entries like:--
> fileA
> 8  ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39
> 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18
> 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93
> and
> fileB
> ChainA: ALA8    -67.217297      -37.131330
> ChainA: ALA21   -69.822977      -48.871282
> ChainA: ALA23   -59.148095      -46.540043
> ChainA: ALA33   -65.459303      -43.269718
>
> i want to join thses two files in such a way that the output file will
> contain column of both the files and the enties of similar position of ALA
> will be together.so the output file should look something like:
> fileC
> 8  ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 ChainA:
> ALA8    -67.217297      -37.131330
> 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18
>                                                           ChainA:
> ALA21   -69.822977      -48.871282
> 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 ChainA:
> ALA23   -59.148095      -46.540043
>                                                           ChainA:
> ALA33   -65.459303      -43.269718

This mailinglist is not a collection of free code monkeys. Show us
you've at least /tried/ to write this yourself, and tell us where
you're running into problems or what error you're getting.

See also http://catb.org/esr/faqs/smart-questions.html

Additionally, you might consider asking on the Indian Python
mailinglist instead:
http://mail.python.org/mailman/listinfo/bangpypers

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


Re: how two join and arrange two files together

2009-07-18 Thread amrita
I tried to join these two files together using command...

from itertools import izip
from os.path import exists

def parafiles(*files):
vec = (open(f) for f in files if exists(f))
data = izip(*vec)
[f.close() for f in vec]
return data

for data in parafiles('/home/amrita/alachems/chem1.txt',
'/home/amrita/secstr/secstr.txt'):
print ' '.join(d.strip() for d in data)

it just joined the column of two files.

> On Sat, Jul 18, 2009 at 12:09 AM,  wrote:
>>
>> Hi,
>>
>> I have two files having entries like:--
>> fileA
>> 8  ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39
>> 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18
>> 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93
>> and
>> fileB
>> ChainA: ALA8    -67.217297      -37.131330
>> ChainA: ALA21   -69.822977      -48.871282
>> ChainA: ALA23   -59.148095      -46.540043
>> ChainA: ALA33   -65.459303      -43.269718
>>
>> i want to join thses two files in such a way that the output file will
>> contain column of both the files and the enties of similar position of
>> ALA
>> will be together.so the output file should look something like:
>> fileC
>> 8  ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 ChainA:
>> ALA8    -67.217297      -37.131330
>> 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18
>>                                                
>>           ChainA:
>> ALA21   -69.822977      -48.871282
>> 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 ChainA:
>> ALA23   -59.148095      -46.540043
>>                                                
>>           ChainA:
>> ALA33   -65.459303      -43.269718
>
> This mailinglist is not a collection of free code monkeys. Show us
> you've at least /tried/ to write this yourself, and tell us where
> you're running into problems or what error you're getting.
>
> See also http://catb.org/esr/faqs/smart-questions.html
>
> Additionally, you might consider asking on the Indian Python
> mailinglist instead:
> http://mail.python.org/mailman/listinfo/bangpypers
>
> - Chris
>


Amrita Kumari
Research Fellow
IISER Mohali
Chandigarh
INDIA

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


Re: how two join and arrange two files together

2009-07-18 Thread Peter Otten
amr...@iisermohali.ac.in wrote:

> I tried to join these two files together using command...
> 
> from itertools import izip
> from os.path import exists
> 
> def parafiles(*files):
> vec = (open(f) for f in files if exists(f))
> data = izip(*vec)
> [f.close() for f in vec]
> return data
> 
> for data in parafiles('/home/amrita/alachems/chem1.txt',
> '/home/amrita/secstr/secstr.txt'):
> print ' '.join(d.strip() for d in data)

parafiles has a bug: vec is a generator and hence cannot be run twice. 
Therefore the odd [f.close()...] list comprehension (don't use a list comp 
if you don't care about the result!) has no effect. 

If you change vec into a list you will be hit by another problem -- the for 
loop trying to operate on closed files. While the correct approach probably 
involves a contextlib.contextmanager I recommend that you concentrate on 
your real problem and keep parafiles() simple:

def parafiles(*files):
open_files = (open(f) for f in files if exits(f))
return izip(*open_files)

> it just joined the column of two files.

Can you make an effort to express clearly what you want, preferrably with a 
simple and unambiguous example?

Please keep the line widths below the threshold of 78 characters to avoid 
messing it up on its way to the reader.

Peter

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


Re: how two join and arrange two files together

2009-07-18 Thread amrita
I want to join column of two different data file but i want that the
entries will match (example i mentioned in my first mail, the position of
ALA eill match) if its not matching then it will get printed as such.

> amr...@iisermohali.ac.in wrote:
>
>> I tried to join these two files together using command...
>>
>> from itertools import izip
>> from os.path import exists
>>
>> def parafiles(*files):
>> vec = (open(f) for f in files if exists(f))
>> data = izip(*vec)
>> [f.close() for f in vec]
>> return data
>>
>> for data in parafiles('/home/amrita/alachems/chem1.txt',
>> '/home/amrita/secstr/secstr.txt'):
>> print ' '.join(d.strip() for d in data)
>
> parafiles has a bug: vec is a generator and hence cannot be run twice.
> Therefore the odd [f.close()...] list comprehension (don't use a list comp
> if you don't care about the result!) has no effect.
>
> If you change vec into a list you will be hit by another problem -- the
> for
> loop trying to operate on closed files. While the correct approach
> probably
> involves a contextlib.contextmanager I recommend that you concentrate on
> your real problem and keep parafiles() simple:
>
> def parafiles(*files):
> open_files = (open(f) for f in files if exits(f))
> return izip(*open_files)
>
>> it just joined the column of two files.
>
> Can you make an effort to express clearly what you want, preferrably with
> a
> simple and unambiguous example?
>
> Please keep the line widths below the threshold of 78 characters to avoid
> messing it up on its way to the reader.
>
> Peter
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Amrita Kumari
Research Fellow
IISER Mohali
Chandigarh
INDIA

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


Re: missing 'xor' Boolean operator

2009-07-18 Thread Mark Dickinson
On Jul 17, 12:06 pm, Jean-Michel Pichavant 
wrote:
> I was saying that using boolean operators with object instead of boolean
> values is error prone,

I agree with this to some extent.  After all, Python conditional
expressions were eventually introduced in response to buggy uses of
the 'a and b or c' idiom.  See PEP 308, and:

http://mail.python.org/pipermail/python-dev/2005-September/056546.html

In my own code, I'm finding myself increasingly using conditional
expressions where I would once have used 'and' or 'or':

daysInAdvance = int(inputVar) if inputVar is not None else 0

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


difference in printing to screen Mac / Windows

2009-07-18 Thread Mark Bakker
Hello list

I notice a difference between running the following script on my Mac and on
a PC:

from time import sleep
for i in range(10):
   print i,
   sleep(2)

On my PC this prints a number every 2 seconds. This is the behavior I want.

On my Mac Python waits 10*2 = 20 seconds, and then prints 0 1 2 3 4 5 6 7 8
9

Any thoughts on how I can make Python behave to get the Python behavior of
my PC on my Mac?

Thanks,

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


Re: how two join and arrange two files together

2009-07-18 Thread Peter Otten
amr...@iisermohali.ac.in wrote:

>> Can you make an effort to express clearly what you want, preferrably with
>> a simple and unambiguous example?

> I want to join column of two different data file but i want that the
> entries will match (example i mentioned in my first mail, the position of
> ALA eill match) if its not matching then it will get printed as such.

Just say "No" if you mean it.

My best guess:

from collections import defaultdict

def merge(sources):
blanks = [blank for items, blank, keyfunc in sources]
d = defaultdict(lambda: blanks[:])
for index, (items, blank, keyfunc) in enumerate(sources):
for item in items:
d[keyfunc(item)][index] = item
for key in sorted(d):
yield d[key]

if __name__ == "__main__":
from StringIO import StringIO
a = StringIO("""\
a alpha
c beta
d gamma
""")

b = StringIO("""\
a one
b two
d three
e four
""")

c = StringIO("""\
a 111
b 222
f 333
""")

def key(line):
return line[:1]
def source(stream, blank="", key=key):
return (line.strip() for line in stream), blank, key
for m in merge([source(x) for x in [a,b,c]]):
print "|".join(c.ljust(10) for c in m)



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


Re: difference in printing to screen Mac / Windows

2009-07-18 Thread Tim Chase

I notice a difference between running the following script on my Mac and on
a PC:

from time import sleep
for i in range(10):
   print i,
   sleep(2)

On my PC this prints a number every 2 seconds. This is the behavior I want.

On my Mac Python waits 10*2 = 20 seconds, and then prints 0 1 2 3 4 5 6 7 8
9


This sounds like a buffered vs. non-buffered output issue.  My 
guess would be that if you increased 10 to something larger, the 
output buffer would flush at various intervals.  The solution on 
the Mac (or other *nix-like OSes) would be to either start python 
in unbuffered mode:


  python -u mycode.py

Alternatively, you can force a flush of the output buffer at each 
iteration:


  import sys
  for i in range(10):
print i
sys.stdout.flush()
sleep(2)

Lastly, you can force all standard-output in your program to be 
unbuffered without the "-u" parameter:


  sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

Hope this helps,

-tkc


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


Python graphics / imaging library

2009-07-18 Thread Peter Chant
Chaps,

what's the most appropriate (maintained) graphics library to use?  PIL seems
to have last been updated in 2006 http://www.pythonware.com/products/pil/
and GD seems to be even older.  Don't want to go down a dead end.

Pete

-- 
http://www.petezilla.co.uk
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python graphics / imaging library

2009-07-18 Thread Michiel Overtoom

Peter Chant wrote:


what's the most appropriate (maintained) graphics library to use?  PIL seems
to have last been updated in 2006 http://www.pythonware.com/products/pil/
and GD seems to be even older.  Don't want to go down a dead end.


Contrary to organic material, software doesn't rot when it gets older.

PIL is pretty complete for the task it was designed to do, pretty 
debugged during the past years, and pretty much 'finished' -- it doesn't 
need frequent updates anymore.


Greetings,

--
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Valloppillil
http://www.catb.org/~esr/halloween/halloween4.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python graphics / imaging library

2009-07-18 Thread Peter Chant
Michiel Overtoom wrote:

> Peter Chant wrote:
> 
>> what's the most appropriate (maintained) graphics library to use?  PIL
>> seems to have last been updated in 2006
>> http://www.pythonware.com/products/pil/
>> and GD seems to be even older.  Don't want to go down a dead end.
> 
> Contrary to organic material, software doesn't rot when it gets older.
> 
> PIL is pretty complete for the task it was designed to do, pretty
> debugged during the past years, and pretty much 'finished' -- it doesn't
> need frequent updates anymore.
> 
> Greetings,
> 

No, it does not.  However, if PIL was updated last in 2006.  Python in 2009
has gone to version 3.1.  If PIL is compatible with 3.1 then I'm fine.  But
I don't want to have to stick with Python 2.5 as the rest of the world
moves on.

Pete


-- 
http://www.petezilla.co.uk
-- 
http://mail.python.org/mailman/listinfo/python-list


Auto Send URL

2009-07-18 Thread Victor Subervi
Hi;
I am trying to script code that automatically sends a Web site visitor to an
URL. Specifically, when they enter a value in a search box, I have that form
sent to a script that writes an URL acceptable to Google, then I want to
send the visitor off without him having to click another button. How do I do
this?
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python graphics / imaging library

2009-07-18 Thread Benjamin Kaplan
On Sat, Jul 18, 2009 at 7:50 AM, Peter Chant <
rempete...@cappetezilla.italsco.uk> wrote:

> Michiel Overtoom wrote:
>
> > Peter Chant wrote:
> >
> >> what's the most appropriate (maintained) graphics library to use?  PIL
> >> seems to have last been updated in 2006
> >> http://www.pythonware.com/products/pil/
> >> and GD seems to be even older.  Don't want to go down a dead end.
> >
> > Contrary to organic material, software doesn't rot when it gets older.
> >
> > PIL is pretty complete for the task it was designed to do, pretty
> > debugged during the past years, and pretty much 'finished' -- it doesn't
> > need frequent updates anymore.
> >
> > Greetings,
> >
>
> No, it does not.  However, if PIL was updated last in 2006.  Python in 2009
> has gone to version 3.1.  If PIL is compatible with 3.1 then I'm fine.  But
> I don't want to have to stick with Python 2.5 as the rest of the world
> moves on.
>

The rest of the world hasn't moved on yet. Most people are still using
Python 2.6 and the 2.x series will continue to be actively developed for
another couple years.


> Pete
>
>
> --
> http://www.petezilla.co.uk
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python graphics / imaging library

2009-07-18 Thread Peter Chant
Peter Chant wrote:


> 
> No, it does not.  However, if PIL was updated last in 2006.  Python in
> 2009
> has gone to version 3.1.  If PIL is compatible with 3.1 then I'm fine. 
> But I don't want to have to stick with Python 2.5 as the rest of the world
> moves on.

BTW, this was not a critisism of PIL or GD, rather what do people generally
use now?

-- 
http://www.petezilla.co.uk
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python graphics / imaging library

2009-07-18 Thread Michiel Overtoom

Peter Chant wrote:


what do people generally use now?


I can only speak for myself... I use PIL ;-)

Greetings,

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


Re: Python graphics / imaging library

2009-07-18 Thread Max Erickson
Peter Chant  wrote:

> No, it does not.  However, if PIL was updated last in 2006. 
> Python in 2009 has gone to version 3.1.  If PIL is compatible
> with 3.1 then I'm fine.  But I don't want to have to stick with
> Python 2.5 as the rest of the world moves on.
> 
> Pete
> 
> 

Various messages to the Image-SIG mailing list indicate that support 
for 3.x is coming after a release of 1.1.7:

http://mail.python.org/pipermail/image-sig/2009-March/005498.html

More here:

http://mail.python.org/pipermail/image-sig/2009-March/thread.html

More recent months contain updates to the status of 1.1.7, it is 
headed towards a release. Preliminary tarballs and binaries are 
available on effbot.org:

http://effbot.org/downloads/#imaging
http://effbot.org/downloads/#pil


max

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


Re: Beginners question

2009-07-18 Thread Rhodri James
On Sat, 18 Jul 2009 03:03:32 +0100, gabrielmonnerat  
 wrote:



Ronn Ross wrote:
How do you define a global variable in a class. I tried this with do  
success:

class ClassName:
global_var = 1
   def some_methos():
print global_var

This doesn't work. What am I doing wrong?
You need pass "self" to the function and "global_var" needs be called  
with self.


class ClassName:
global_var = 1
   def some_methos(self):
print self.global_var


If the OP really wants global_var to be global to the class, this
approach will only work as long as he never assigns to self.global_var.

The terminology is a bit confusing here.  "Global variables" refers to
variables which are global to an entire module, ones that you would
use "global" to declare inside a function.  Something like this:

x = 5
def set_x(y):
global x
x = y

set_x(3)
print x

...gives the result "3"

The idea of defining a global variable in a class is a bit of a
nonsense, since globals exist for the entire module.  There are,
however, variables that exist and are the same for every instance
of a class, or "class attributes" as they are normally called.  You
use them either by assignment in the class definition, or in a
method by prefixing them with the class name.

class ClassName:
global_var = 1
def some_method(self):
print ClassName.global_var
ClassName.global_var = 2

"Instance attributes", the ones prefixed by "self", are what get
used for most purposes.  These are separate for each different
instance of the class.

class ClassName:
def __init__(self, val):
   self.var = val

a = ClassName(1)
b = ClassName(2)

print a.var, b.var

...gives the result "1 2"

The wrinkle is that if you read from an instance attribute that
doesn't exist, Python will use a class attribute of the same name
if there is one.  This is often exploited for setting default
values.  Beware, though: the moment you assign, an instance
attribute is created -- the underlying class attribute is *not*
changed.

class ClassName:
global_var = 1
def some_method(self):
self.global_var = 2

a = ClassName()
b = ClassName()
b.some_method()
print a.global_var, b.global_var

...gives the result "1 2" again!

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python import Error

2009-07-18 Thread David Stanek
On Sat, Jul 18, 2009 at 3:14 AM, Kalyan
Chakravarthy wrote:
> Hi All,
>    I am using Python 2.6, MySQL 4.0 , I have successfully
> Instaled MySQLdb (MySQL-python-1.2.3c1.win32-py2.6) in my system. I tested
> through command prompt with "import MySQLdb ", its not shwing any errors
> (means its instaled successfully), I set Eneceranment variable for Python
>
> I  am running simple application with import "MySQLdb" to get the connection
> to MySQL, But its throwing "No module named MySQLdb", Please any one tell me
> what couled be the reasion
>
> is there any version miss match with python and MySQL ?
>
> Since last 3days I am strugling on this
>

Off the top of my head I would say that maybe you are using one python
binary from the command-line and another in your script or possibly
your python path is foobar.

-- 
David
blog: http://www.traceback.org
twitter: http://twitter.com/dstanek
-- 
http://mail.python.org/mailman/listinfo/python-list


Unpack Expects the Wrong Number of Bytes

2009-07-18 Thread Timothy Crone
Hello,

I have noticed that for certain format strings, struct.unpack expects
the wrong number of bytes. For example, this works fine

header = "4si4s4si2h2i3h4s"
data = list(unpack(header,f.read(42)))

however, this

header = "4si4s4si2h2i3h4si"
data = list(unpack(header,f.read(46)))

returns the following error:

struct.error: unpack requires a string argument of length 48

So even though my format string includes an additional 4-byte integer,
unpack is expecting 6 additional bytes. Here's a simpler example:

This works:

header = "s"
data = list(unpack(header,f.read(1)))

however this:

header = "si"
data = list(unpack(header,f.read(5)))

throws

struct.error: unpack requires a string argument of length 8

So unpack expects 7 additional bytes when an integer is added to the
format string. Does anyone know what is going on? I am using Debian
stable, so my Python version is 2.5.2. But I have replicated this with
2.6.2. Here's my proc/version: Linux version 2.6.30-bpo.1-amd64
(Debian 2.6.30-1~bpo50+1) (no...@debian.org) (gcc version 4.3.2
(Debian 4.3.2-1.1) ) #1 SMP Fri Jun 26 09:41:55 UTC 2009

Any help would be greatly appreciated.

Cheers,
Tim
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: setup.py not found

2009-07-18 Thread Aahz
In article <3be2bdce-680d-4b32-ad0c-ef46caf55...@f10g2000vbf.googlegroups.com>,
larry.mart...@gmail.com  wrote:
>
>I'm trying to install a package (cx_Oracle) on a mac running 10.5.7.
>I've done this on other platforms, but never on a mac. I followed the
>instructions given, but when I try and run setup I get:
>
>Apollo:instantclient_10_2 user$ python setup.py build
>/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/
>Python.app/Contents/MacOS/Python: can't open file 'setup.py': [Errno
>2] No such file or directory
>
>Is there something else I have to install first to have this?

Do you have a setup.py in the current working directory?
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"The volume of a pizza of thickness 'a' and radius 'z' is
given by pi*z*z*a"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unpack Expects the Wrong Number of Bytes

2009-07-18 Thread John Machin
On Jul 19, 12:34 am, Timothy Crone  wrote:
> Hello,
>
> I have noticed that for certain format strings, struct.unpack expects
> the wrong number of bytes.
[snip]
> header = "si"
> data = list(unpack(header,f.read(5)))
>
> throws
>
> struct.error: unpack requires a string argument of length 8
>
> So unpack expects 7 additional bytes when an integer is added to the
> format string. Does anyone know what is going on?

Alignment. 1 byte for the "s", 3 pad bytes to get to the next "i"
boundary, 4 bytes for the "i".

http://docs.python.org/library/struct.html
Then Ctrl-F search for "align"

Choose a prefix character whose MO matches the struct that you need to
work with.

Cheers,
John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Try... except....Try again?

2009-07-18 Thread MRAB

Xavier Ho wrote:

Darn it.

On Sat, Jul 18, 2009 at 8:55 AM, Dave Angel > wrote:



You don't need a counter.  len() will tell you the size of the list
of primes.


Does len() go through and count by itself, or does it actually keep 
track of the size and just return the memory? I always thought it would 
go through and count, which may take some time. I could be wrong. 


[snip]
Lists (and strings, tuples, dicts and sets) know how many items they
contain.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unpack Expects the Wrong Number of Bytes

2009-07-18 Thread Ross Ridge
Timothy Crone   wrote:
>header = "s"
>data = list(unpack(header,f.read(1)))
>
>however this:
>
>header = "si"
>data = list(unpack(header,f.read(5)))
>
>throws
>
>struct.error: unpack requires a string argument of length 8
>
>So unpack expects 7 additional bytes when an integer is added to the
>format string. Does anyone know what is going on?

It's adding pad bytes so that the format matches the equivilent C
structure on your machine.  You should use either the "<" little-endian
or the ">" big-endian prefix depending on the byte order used in the file
you're trying to unpack.  You can also use the "=" native byte-order
flag if endianness of the file format changes according to the machine
your Python program runs on.  If you use any of these flags, no extra
padding will be inserted.

Ross Ridge

-- 
 l/  //   Ross Ridge -- The Great HTMU
[oo][oo]  rri...@csclub.uwaterloo.ca
-()-/()/  http://www.csclub.uwaterloo.ca/~rridge/ 
 db  //   
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: invoke method on many instances

2009-07-18 Thread Alan G Isaac
> On Fri, 17 Jul 2009 05:19:50 +, Alan G Isaac wrote:
>> def apply2(itr, methodname, *args, **kwargs):
>> f = operator.methodcaller(methodname, *args, **kwargs)
>> for item in itr:
>> f(item)


On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote:
> for obj in objects:
> getattr(obj, methodname)(*args, **kwargs)


Are there any obvious considerations in choosing
between those two?



> See also these recipes from the "Python Cookbook": 
> http://code.activestate.com/recipes/52289/
> http://code.activestate.com/recipes/87370/

Interesting.

Thanks,
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Bug By Any Other Name ...

2009-07-18 Thread Tom Kermode
Maybe the IDE is the best place to warn you of something like that.

You could have an IDE where you specify which language you're more
familiar with and then have it display warnings likely to be relevant
to you.  People could collaborate to add support for gradually more
niche languages.

Python could continue to work the way it was designed to and people
documenting python wouldn't have to worry about this kind of issue. :)

2009/7/6 Lawrence D'Oliveiro :
> I wonder how many people have been tripped up by the fact that
>
>    ++n
>
> and
>
>    --n
>
> fail silently for numeric-valued n.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.kiloday.com
http://www.fourstopspast.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python graphics / imaging library

2009-07-18 Thread Peter Chant
Max Erickson wrote:

> More recent months contain updates to the status of 1.1.7, it is
> headed towards a release. Preliminary tarballs and binaries are
> available on effbot.org:
> 
> http://effbot.org/downloads/#imaging
> http://effbot.org/downloads/#pil

Excellent.  From a very brief look it seems like it will be quite simple to
use.

Pete


-- 
http://www.petezilla.co.uk
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python graphics / imaging library

2009-07-18 Thread Marcus Wanner

On 7/18/2009 11:41 AM, Peter Chant wrote:

Max Erickson wrote:


More recent months contain updates to the status of 1.1.7, it is
headed towards a release. Preliminary tarballs and binaries are
available on effbot.org:

http://effbot.org/downloads/#imaging
http://effbot.org/downloads/#pil


Excellent.  From a very brief look it seems like it will be quite simple to
use.

Pete



Yes, it was fun to work with (for me at least).

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


Rus Python script interactively

2009-07-18 Thread Gnarlodious
In an interactive session (I am using iPython), what is the most
elegant way to run a Python script from Terminal? Right now I am
saying:

import subprocess
subprocess.call("python /path/to/scriptname.py", shell=True)

But I am calling a shell process and I'd rather not. The script just
runs, no inputs are needed. Is there a more Pythonesque method?

-- Gnarlie
http://Gnarlodious.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rus Python script interactively

2009-07-18 Thread Diez B. Roggisch

Gnarlodious schrieb:

In an interactive session (I am using iPython), what is the most
elegant way to run a Python script from Terminal? Right now I am
saying:

import subprocess
subprocess.call("python /path/to/scriptname.py", shell=True)

But I am calling a shell process and I'd rather not. The script just
runs, no inputs are needed. Is there a more Pythonesque method?


If it's in the path, you can just import it. That will of course only be 
possible once per ipython-session.


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


Re: Rus Python script interactively

2009-07-18 Thread Lie Ryan
Gnarlodious wrote:
> In an interactive session (I am using iPython), what is the most
> elegant way to run a Python script from Terminal? Right now I am
> saying:
> 
> import subprocess
> subprocess.call("python /path/to/scriptname.py", shell=True)
> 
> But I am calling a shell process and I'd rather not. The script just
> runs, no inputs are needed. Is there a more Pythonesque method?
> 
> -- Gnarlie
> http://Gnarlodious.com/

IMHO, starting a new terminal and runs the script from there.
Or quitting/pausing the current interactive session and run it like
normal python scripts.

Alternatively, you may also want to look at `execfile` built-in function.
>>> execfile('myscript.py', {}, {})

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


Re: Rus Python script interactively

2009-07-18 Thread Marcus Wanner

On 7/18/2009 12:32 PM, Gnarlodious wrote:

In an interactive session (I am using iPython), what is the most
elegant way to run a Python script from Terminal? Right now I am
saying:

import subprocess
subprocess.call("python /path/to/scriptname.py", shell=True)

But I am calling a shell process and I'd rather not. The script just
runs, no inputs are needed. Is there a more Pythonesque method?

-- Gnarlie
http://Gnarlodious.com/
You could put the code of the script in a main() function and have an if 
__name__ == '__main__': around the internal call. That way, it will 
still run the code if you run it normally, and you can also run it 
several times from the interactive session, ie:

file you want to run:
{{{
def somefunction():
print 'hello world'

somefunction()
}}}
file after modifications:
{{{
def somefunction():
print 'hello world'

def main():
somefunction()

if __name__ == '__main__': #only true if the file is being run normally, 
not imported.

main()
}}}
Both of those, if run normally, will print "hello world". If the first 
one is imported, it will run once and not me runnable in that session 
again. If the second one is imported, it will not do anything until you 
call the main() function, and then you can call it again as many times 
as you want:

{{{
>>> import thefile
>>> for i in range(5):
... thefile.main()
...
hello world
hello world
hello world
hello world
hello world
>>> exit()
}}}
Hope this helps you!

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


Re: Rus Python script interactively

2009-07-18 Thread Peter Otten
Gnarlodious wrote:

> In an interactive session (I am using iPython), what is the most
> elegant way to run a Python script from Terminal? Right now I am
> saying:
> 
> import subprocess
> subprocess.call("python /path/to/scriptname.py", shell=True)
> 
> But I am calling a shell process and I'd rather not. The script just
> runs, no inputs are needed. Is there a more Pythonesque method?

After a quick glance over ipython's %quickref:

In [1]: !mkdir -p path/to

In [2]: !echo 'print "works!"' > path/to/scriptname.py

In [3]: %run path/to/scriptname.py
works!


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


python command running old version

2009-07-18 Thread Tim Edwards
My brain is running in n00b mode this morning...must find coffee.

I upgraded python this morning and entering python from the command
line runs the old version.

Just looked and it appears the old version is in /usr/bin while the
new one is in /usr/local/bin/

Besides changing the path order, how do I ensure it runs the new
version? I'm sure I'll bang my head on the desk in shame as soon as
I'm reminded.

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


Re: python command running old version

2009-07-18 Thread Chris Rebert
On Sat, Jul 18, 2009 at 10:25 AM, Tim Edwards wrote:
> My brain is running in n00b mode this morning...must find coffee.
>
> I upgraded python this morning and entering python from the command
> line runs the old version.

Which OS? How did you install it?

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Auto Send URL

2009-07-18 Thread Chris Rebert
On Fri, Jul 17, 2009 at 6:02 AM, Victor Subervi wrote:
> Hi;
> I am trying to script code that automatically sends a Web site visitor to an
> URL. Specifically, when they enter a value in a search box, I have that form
> sent to a script that writes an URL acceptable to Google, then I want to
> send the visitor off without him having to click another button. How do I do
> this?

Send back a webpage with a Refresh meta tag:



http://YOUR-URL-HERE/";>



Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python command running old version

2009-07-18 Thread Christian Heimes
Tim Edwards wrote:
> Besides changing the path order, how do I ensure it runs the new
> version? I'm sure I'll bang my head on the desk in shame as soon as
> I'm reminded.

hash -r

Christian

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


Re: python command running old version

2009-07-18 Thread Tim Edwards
> Which OS? How did you install it?

Sorry (see I need that coffee)

Installed on redhat from source
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python command running old version

2009-07-18 Thread Tim Edwards
>> Besides changing the path order, how do I ensure it runs the new
>> version? I'm sure I'll bang my head on the desk in shame as soon as
>> I'm reminded.
>
> hash -r

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


Re: python command running old version

2009-07-18 Thread Lie Ryan
Tim Edwards wrote:
>> Which OS? How did you install it?
> 
> Sorry (see I need that coffee)
> 
> Installed on redhat from source

You should consult the distro's (i.e. RedHat's) documentation/mailing
list about changing the default python interpreter. In most cases, you
will need to make sure all python-related packages are compatible with
the new python version. In popular distros, there should be a script
that will do that for you.

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


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-18 Thread akhil1988

Thanks Nobody-38, it solved my problem immediately.

--Thanks Again,
Akhil

Nobody-38 wrote:
> 
> On Thu, 16 Jul 2009 20:26:39 -0700, akhil1988 wrote:
> 
>> Well, you were write: unintentionally I removed strip(). But the problem
>> does
>> not ends here:
>> 
>> I get this error now:
>> 
>>  File "./temp.py", line 488, in 
>> main()
>>   File "./temp.py", line 475, in main
>> for line in sys.stdin:
>>   File "/usr/local/lib/python3.1/codecs.py", line 300, in decode
>> (result, consumed) = self._buffer_decode(data, self.errors, final)
>> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2:
>> invalid
>> data
>> 
>> for this line:
>> â
> 
> Right. You're running in a locale whose encoding is UTF-8, but feeding
> data which isn't valid UTF-8 to stdin. If you want to use data with a
> different encoding, you need to replace sys.stdin, e.g.:
> 
> import sys
> import io
> sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding = 'iso-8859-1')
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: 
http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24550497.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-18 Thread akhil1988

Thanks David, it solved my problem immediately. 

I will follow your advise from next time but honestly I am new to python
with not much knowledge about text formats. And the main portion of my
project was not to deal with these, so I just wanted to get this solved as I
was already struck at this for 2 days. If you think I am wrong in my
approach to getting problems solved, please let me know. Your advise would
be helpful in future for me.

--Thanks Again,
Akhil 

Scott David Daniels wrote:
> 
> akhil1988 wrote:
> >
>> Nobody-38 wrote:
>>> On Thu, 16 Jul 2009 15:43:37 -0700, akhil1988 wrote:
> ...
> In Python 3 you can't decode strings because they are Unicode strings
> and it doesn't make sense to decode a Unicode string. You can only
> decode encoded things which are byte strings. So you are mixing up
> byte
> strings and Unicode strings.
 ... I read a byte string from sys.stdin which needs to converted to
 unicode
 string for further processing.
>>> In 3.x, sys.stdin (stdout, stderr) are text streams, which means that
>>> they
>>> read and write Unicode strings, not byte strings.
>>>
 I cannot just remove the decode statement and proceed?
 This is it what it looks like:
 for line in sys.stdin:
 line = line.decode('utf-8').strip()
 if line == '': #do something here
 
 If I remove the decode statement, line == '' never gets true. 
>>> Did you inadvertently remove the strip() as well?
>> ... unintentionally I removed strip()
>> I get this error now:
>>  File "./temp.py", line 488, in 
>> main()
>>   File "./temp.py", line 475, in main
>> for line in sys.stdin:
>>   File "/usr/local/lib/python3.1/codecs.py", line 300, in decode
>> (result, consumed) = self._buffer_decode(data, self.errors, final)
>> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2:
>> invalid
>> data
> 
> (1) Do not top post.
> (2) Try to fully understand the problem and proposed solution, rather
>  than trying to get people to tell you just enough to get your code
>  going.
> (3) The only way sys.stdin can possibly return unicode is to do some
>  decoding of its own.  your job is to make sure it uses the correct
>  decoding.  So, if you know your source is always utf-8, try
>  something like:
> 
>  import sys
>  import io
> 
>  sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf8')
> 
>  for line in sys.stdin:
>  line = line.strip()
>  if line == '':
>  #do something here
>  
> 
> --Scott David Daniels
> scott.dani...@acm.org
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: 
http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24550540.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: python command running old version

2009-07-18 Thread Tim Edwards
> You should consult the distro's (i.e. RedHat's) documentation/mailing
> list about changing the default python interpreter. In most cases, you
> will need to make sure all python-related packages are compatible with
> the new python version. In popular distros, there should be a script
> that will do that for you.

Thanks, I'll look into that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing and freezing on Windows

2009-07-18 Thread SK
Thanks Gabriel.

Posted as: http://bugs.python.org/issue6461

The multiprocessing author has tentatively confirmed the bug.
-- 
http://mail.python.org/mailman/listinfo/python-list


How to receive a data file of unknown length using a python socket?

2009-07-18 Thread twgray
I am attempting to send a jpeg image file created on an embedded
device over a wifi socket to a Python client running on a Linux pc
(Ubuntu).  All works well, except I don't know, on the pc client side,
what the file size is?  The following is a snippet:

[code]
f = open("frame.jpg",mode = 'wb')
   while True:
data = self.s.recv(MAXPACKETLEN)
if len(data) == 0:
break
recvd += len(data)
f.write(data)
f.close()
[end]

It appears to be locking up in  'data=self.s.recv(MAXPACKETLEN)' on
the final packet, which will always be less than MAXPACKETLEN.

I guess my question is, how do I detect end of data on the client side?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread Irmen de Jong

twgray wrote:

I am attempting to send a jpeg image file created on an embedded
device over a wifi socket to a Python client running on a Linux pc
(Ubuntu).  All works well, except I don't know, on the pc client side,
what the file size is?  


You don't. Sockets are just endless streams of bytes. You will have to design some form 
of 'wire protocol' that includes the length of the message that is to be read.

For instance a minimalistic protocol could be the following:
Send 4 bytes that contain the length (an int) then the data itself. The client reads 4 
bytes, decodes it into the integer that tells it the length, and then reads the correct 
amount of bytes from the socket.



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


Re: Rus Python script interactively

2009-07-18 Thread Gnarlodious
Thanks for all the suggestions! The last solution is the one I was
ooking for, I am really starting to like iPython. Learning all kinds
of new tricks!.

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


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread Tycho Andersen
On Sat, Jul 18, 2009 at 4:43 PM, Irmen de Jong wrote:
> twgray wrote:
>>
>> I am attempting to send a jpeg image file created on an embedded
>> device over a wifi socket to a Python client running on a Linux pc
>> (Ubuntu).  All works well, except I don't know, on the pc client side,
>> what the file size is?
>
> You don't. Sockets are just endless streams of bytes. You will have to
> design some form of 'wire protocol' that includes the length of the message
> that is to be read.
> For instance a minimalistic protocol could be the following:
> Send 4 bytes that contain the length (an int) then the data itself. The
> client reads 4 bytes, decodes it into the integer that tells it the length,
> and then reads the correct amount of bytes from the socket.

Exactly, sending the length first is the only way to know ahead of
time. Alternatively, if you know what the end of the data looks like,
you can look for that 'flag' as well, and stop trying to recv() after
that.

Some things that may be useful, though, are socket.settimeout() and
socket.setblocking(). More information is availible in the docs:
http://docs.python.org/library/socket.html.

You need to be careful with this, though, since network latency may
cause problems. Using these methods will keep your program from
sitting in recv() forever, though.

\t
--
http://tycho.ws
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread twgray
On Jul 18, 4:43 pm, Irmen de Jong  wrote:
> twgray wrote:
> > I am attempting to send a jpeg image file created on an embedded
> > device over a wifi socket to a Python client running on a Linux pc
> > (Ubuntu).  All works well, except I don't know, on the pc client side,
> > what the file size is?  
>
> You don't. Sockets are just endless streams of bytes. You will have to design 
> some form
> of 'wire protocol' that includes the length of the message that is to be read.
> For instance a minimalistic protocol could be the following:
> Send 4 bytes that contain the length (an int) then the data itself. The 
> client reads 4
> bytes, decodes it into the integer that tells it the length, and then reads 
> the correct
> amount of bytes from the socket.
>
> --irmen

Thanks for the reply.  But, now I have a newbie Python question.  If I
send a 4 byte address from the embedded device, how do I convert that,
in Python, to a 4 byte, or long, number?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: invoke method on many instances

2009-07-18 Thread Terry Reedy

Alan G Isaac wrote:

On Fri, 17 Jul 2009 05:19:50 +, Alan G Isaac wrote:

def apply2(itr, methodname, *args, **kwargs):
f = operator.methodcaller(methodname, *args, **kwargs)
for item in itr:
f(item)



On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote:

for obj in objects:
getattr(obj, methodname)(*args, **kwargs)



Are there any obvious considerations in choosing
between those two?


I would use the straightforward getattr idiom.

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


uniicode and executing a process with subprocess.call, or os.system

2009-07-18 Thread Rick King

Hello,
I want to copy files using subprocess.call or os.system where the file 
names are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. 
Windows stores all the file names in unicode so they are displayed ok in 
explorer, and I can read them into my program with listdir(u'.'), etc. 
and work with the names in the program.


os.rename()

can be used to rename such files successfully.

But I want to be able to copy files using:

cmdstr = u'copy' +u' /Y "'+pair[0]+u'" "'+pair[1]+u'"\n'
cmdstr = cmdstr.encode(sys.getfilesystemencoding())
try: retcode = sp.call(cmdstr, shell=True) #SP=SUBPROCESS

but the encoding can't handle all the characters and so the file isn't 
found to be copied. sp.call() returns 1. 'mbcs' encoding doesn't work 
either. 'utf-8' doesn't work.


I am very confused about unicode. Can someone point me in the right 
direction?


windows xp sp2
python 2.6.2 unicode

Thanks!
Rick King
Southfield MI




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


Re: uniicode and executing a process with subprocess.call, or os.system

2009-07-18 Thread MRAB

Rick King wrote:

Hello,
I want to copy files using subprocess.call or os.system where the file 
names are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. 
Windows stores all the file names in unicode so they are displayed ok in 
explorer, and I can read them into my program with listdir(u'.'), etc. 
and work with the names in the program.


os.rename()

can be used to rename such files successfully.

But I want to be able to copy files using:

cmdstr = u'copy' +u' /Y "'+pair[0]+u'" "'+pair[1]+u'"\n'
cmdstr = cmdstr.encode(sys.getfilesystemencoding())
try: retcode = sp.call(cmdstr, shell=True) #SP=SUBPROCESS

but the encoding can't handle all the characters and so the file isn't 
found to be copied. sp.call() returns 1. 'mbcs' encoding doesn't work 
either. 'utf-8' doesn't work.


I am very confused about unicode. Can someone point me in the right 
direction?


windows xp sp2
python 2.6.2 unicode


Use the shutil module.
--
http://mail.python.org/mailman/listinfo/python-list


Re: uniicode and executing a process with subprocess.call, or os.system

2009-07-18 Thread Chris Rebert
On Sat, Jul 18, 2009 at 3:30 PM, Rick King wrote:
> Hello,
> I want to copy files using subprocess.call or os.system where the file names
> are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. Windows
> stores all the file names in unicode so they are displayed ok in explorer,
> and I can read them into my program with listdir(u'.'), etc. and work with
> the names in the program.

You should try one of the copying functions in the shutil module
instead, it'll be much simpler than using subprocess:
http://docs.python.org/library/shutil.html

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread John Machin
On Jul 19, 8:04 am, twgray  wrote:

> send a 4 byte address from the embedded device, how do I convert that,
> in Python, to a 4 byte, or long, number?

struct.unpack() is your friend. Presuming the embedded device is
little-endian, you do:

the_int = struct.unpack('http://docs.python.org/library/struct.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Newbie question - running a command and looking at output

2009-07-18 Thread Dave
I'm trying to run a command (arch -k) and check if the value returned is 
'sun4v' or not.



kir...@t2:[~] $ arch -k
sun4v

In fact, I want to do 3 three things

1) Check if the system is Solaris.
2) If it is Solaris, check if 'arch -k' prints 'sun4v'
3) If both 1 and 2 are true, copy a file.


Since arch -k is not supported on all systems, the test for Solaris must 
be made first.


I have a test for Solaris (aka SunOS), which copies a file if the 
systems is running Solaris:


import os, shutil

if os.uname()[0] == 'SunOS':
   shutil.copy2('foo.c','bar.c')

How can I change that, so the copy is only performed if 'arch -k' prints 
'sun4v'?




--
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange'  take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
--
http://mail.python.org/mailman/listinfo/python-list


Re: python command running old version

2009-07-18 Thread Aahz
In article ,
Tim Edwards   wrote:
>
>I upgraded python this morning and entering python from the command
>line runs the old version.
>
>Just looked and it appears the old version is in /usr/bin while the
>new one is in /usr/local/bin/
>
>Besides changing the path order, how do I ensure it runs the new
>version? I'm sure I'll bang my head on the desk in shame as soon as
>I'm reminded.

You probably want to create an alias for command-line usage; scripts
should use the full path to the interpreter you want.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"The volume of a pizza of thickness 'a' and radius 'z' is
given by pi*z*z*a"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread Nobody
On Sat, 18 Jul 2009 14:33:48 -0700, twgray wrote:

> It appears to be locking up in  'data=self.s.recv(MAXPACKETLEN)' on
> the final packet, which will always be less than MAXPACKETLEN.
> 
> I guess my question is, how do I detect end of data on the client side?

recv() should return zero when the sender closes its end of the connection.

Is the sender actually closing its end? If you are unsure, use a packet
sniffer such as tcpdump to look for a packet with the FIN flag.

If you need to keep the connection open for further transfers, you need to
incorporate some mechanism for identifying the end of the data into the
protocol. As others have suggested, prefixing the data by its length is
one option. Another is to use an end-of-data marker, but then you need a
mechanism to "escape" the marker if it occurs in the data. A length prefix
is probably simpler to implement, but has the disadvantage that you can't
start sending the data until you know how long it is going to be.

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


Re: Newbie question - running a command and looking at output

2009-07-18 Thread Dave

Dave wrote:
I'm trying to run a command (arch -k) and check if the value returned is 
'sun4v' or not.



kir...@t2:[~] $ arch -k
sun4v

In fact, I want to do 3 three things

1) Check if the system is Solaris.
2) If it is Solaris, check if 'arch -k' prints 'sun4v'
3) If both 1 and 2 are true, copy a file.


Since arch -k is not supported on all systems, the test for Solaris must 
be made first.


I have a test for Solaris (aka SunOS), which copies a file if the 
systems is running Solaris:


import os, shutil

if os.uname()[0] == 'SunOS':
   shutil.copy2('foo.c','bar.c')

How can I change that, so the copy is only performed if 'arch -k' prints 
'sun4v'?






Actually, when I look at oluname, it does print sunnv:


>>> import os
>>> os.uname()
('SunOS', 't2', '5.10', 'Generic_141414-02', 'sun4v')


I guess all I need to do is check for the value of os.uname()[4] too.


--
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange'  take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread MRAB

Nobody wrote:

On Sat, 18 Jul 2009 14:33:48 -0700, twgray wrote:


It appears to be locking up in  'data=self.s.recv(MAXPACKETLEN)' on
the final packet, which will always be less than MAXPACKETLEN.

I guess my question is, how do I detect end of data on the client side?


recv() should return zero when the sender closes its end of the connection.

Is the sender actually closing its end? If you are unsure, use a packet
sniffer such as tcpdump to look for a packet with the FIN flag.

If you need to keep the connection open for further transfers, you need to
incorporate some mechanism for identifying the end of the data into the
protocol. As others have suggested, prefixing the data by its length is
one option. Another is to use an end-of-data marker, but then you need a
mechanism to "escape" the marker if it occurs in the data. A length prefix
is probably simpler to implement, but has the disadvantage that you can't
start sending the data until you know how long it is going to be.


You could send it in chunks, ending with a chunk length of zero.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread MRAB

twgray wrote:

On Jul 18, 4:43 pm, Irmen de Jong  wrote:

twgray wrote:

I am attempting to send a jpeg image file created on an embedded
device over a wifi socket to a Python client running on a Linux pc
(Ubuntu).  All works well, except I don't know, on the pc client side,
what the file size is?  

You don't. Sockets are just endless streams of bytes. You will have to design 
some form
of 'wire protocol' that includes the length of the message that is to be read.
For instance a minimalistic protocol could be the following:
Send 4 bytes that contain the length (an int) then the data itself. The client 
reads 4
bytes, decodes it into the integer that tells it the length, and then reads the 
correct
amount of bytes from the socket.


Thanks for the reply.  But, now I have a newbie Python question.  If I
send a 4 byte address from the embedded device, how do I convert that,
in Python, to a 4 byte, or long, number?


If you send the length as 4 bytes then you'll have to decide whether
it's big-endian or little-endian. An alternative is to send the length
as characters, terminated by, say, '\n' or chr(0).
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread John Machin
On Jul 19, 7:43 am, Irmen de Jong  wrote:
> twgray wrote:
> > I am attempting to send a jpeg image file created on an embedded
> > device over a wifi socket to a Python client running on a Linux pc
> > (Ubuntu).  All works well, except I don't know, on the pc client side,
> > what the file size is?  
>
> You don't. Sockets are just endless streams of bytes. You will have to design 
> some form
> of 'wire protocol' that includes the length of the message that is to be read.

Apologies in advance for my ignorance -- the last time I dipped my toe
in that kind of water, protocols like zmodem and Kermit were all the
rage -- but I would have thought there would have been an off-the-
shelf library for peer-to-peer file transfer over a socket
interface ... not so?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread twgray
On Jul 18, 7:33 pm, MRAB  wrote:
> Nobody wrote:
> > On Sat, 18 Jul 2009 14:33:48 -0700, twgray wrote:
>
> >> It appears to be locking up in  'data=self.s.recv(MAXPACKETLEN)' on
> >> the final packet, which will always be less than MAXPACKETLEN.
>
> >> I guess my question is, how do I detect end of data on the client side?
>
> > recv() should return zero when the sender closes its end of the connection.
>
> > Is the sender actually closing its end? If you are unsure, use a packet
> > sniffer such as tcpdump to look for a packet with the FIN flag.
>
> > If you need to keep the connection open for further transfers, you need to
> > incorporate some mechanism for identifying the end of the data into the
> > protocol. As others have suggested, prefixing the data by its length is
> > one option. Another is to use an end-of-data marker, but then you need a
> > mechanism to "escape" the marker if it occurs in the data. A length prefix
> > is probably simpler to implement, but has the disadvantage that you can't
> > start sending the data until you know how long it is going to be.
>
> You could send it in chunks, ending with a chunk length of zero.

Thanks for the help!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread Aahz
In article ,
MRAB   wrote:
>
>If you send the length as 4 bytes then you'll have to decide whether
>it's big-endian or little-endian. An alternative is to send the length
>as characters, terminated by, say, '\n' or chr(0).

Alternatively, make it a fixed-length string of bytes, zero-padded in
front.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"The volume of a pizza of thickness 'a' and radius 'z' is
given by pi*z*z*a"
-- 
http://mail.python.org/mailman/listinfo/python-list