Regarding sort()

2009-05-25 Thread Dhananjay
Hello All,

I have data set as follows:

24   GLU3   47  LYS 6   3.9092331
42   PRO5   785 VAL 74  4.145114 1
54   LYS6   785 VAL 74  4.305017  1
55   LYS6   785 VAL 74  4.291098  1
56   LYS7   785 VAL 74  3.968647  1
58   LYS7   772 MET 73  4.385121   1
58   LYS7   778 MET 73  4.422980   1
58   LYS7   779 MET 73  3.954990   1
58   LYS7   785 VAL 74  3.420554   1
59   LYS7   763 GLN 72  4.431955   1
59   LYS7   767 GLN 72  3.844037   1
59   LYS7   785 VAL 74  3.725048   1




I want to sort the data on the basis of 3rd column first and latter want to
sort the sorted data (in first step) on the basis of 6th column.

I tried sort() function but could not get the way how to use it.

I am new to programming, please tell me how can I sort.

Thanking you in advance 

regards




-- 
--
Dhananjay C Joshi
Project Assistant
Lab of Structural Biology,
CDFD, Bldg.7, Gruhakalpa
5-4-399/B, Nampally
Hyderabad- 51, India
Tel: +91-40-24749404
Fax: +91-40-24749448
--
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: select.poll returning strange file descriptors.

2009-05-25 Thread Simon Wittber
Solved.

I was using poll.register(fileno) without any flags specfied, which
means "tell me when _anything_ happens".

Because of this, I was receiving OOB data, which seems to use strange
fileno values.

to fix this, I now use this:

poll.register(sock.fileno(), select.POLLIN|select.POLLOUT|
select.POLLERR|select.POLLHUP)

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


Re: Optimizing math functions

2009-05-25 Thread Lawrence D'Oliveiro
In message <0033dace$0$9725$c3e8...@news.astraweb.com>, Steven D'Aprano 
wrote:

> Minimizing functions of two variables is difficult, as a general rule.
> Nevertheless, there are tools for doing so. Check out SciPy.

The name "Marquadt-Levenberg" comes to mind. As I recall, it involved 
finding zeroes of the various partial derivatives.

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


Re: PureData py/pyExt into standalone python

2009-05-25 Thread responsible7
Hi again,

Comments inline:
> > responsib...@gmail.com wrote:
> > Hi guys,
> > I've written some python classes for py/pyExt extensions for the "dataflow" 
> > graphical programming environment PureData.
> > My classes talk to eachother via the PureData system, and they talk to the 
> > outside world via pyPortMIDI (which enables raw MIDI data communication).

edexter wrote:
> This might be usefull do you intend to release this code??

I do, but I want to release it as self contained Python code, at the
moment there are many dependencies. Communication between the classes
relies on py/pyExt, which relies on Flext, which relies on PureData,
which relies on Tk and Wish84, and so on and so on...

The bits of code I have written are just simple "if this string of
bytes arrived, send this string to this other blob of code" so they
don't pull in any other python extensions, it's all core stuff. only
the "send this string" part involves the py/pyExt stuff.

> There are some midi libraries that may be helpful, it is hard to tell
> what you need without seeing the code

I'm already using pyPortMidi for the midi communication. I'm just
floundering a bit when it comes to understanding how to get the
classes I've written to all exist as objects in "python-land" and
communicate between one another.
-- 
http://mail.python.org/mailman/listinfo/python-list


SendMessage question

2009-05-25 Thread zhouhaifeng
Hi,I want to send "ctrl + A" and "ctrl + C" to a window,
but my code can not work, who can help me ?

Thanks a lot!

hWnd = win32gui.FindWindow(None, "“pad")
print hWnd
if hWnd <> 0:
point = (555, 175)
x, y = point
win32api.SetCursorPos(point)

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x, y, 0, 0)
win32api.SetCursorPos(point)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)

#ctrl + A
win32api.SendMessage(hWnd, 0, win32con.VK_CONTROL, 0)
win32api.Sleep(10)
win32api.SendMessage(hWnd, 0, 65, 0)
win32api.SendMessage(hWnd, win32con.KEYEVENTF_KEYUP, 65, 0)
win32api.SendMessage(hWnd, win32con.KEYEVENTF_KEYUP, win32con.VK_CONTROL, 0)
win32api.Sleep(10)

#ctrl + C
win32api.SendMessage(hWnd, win32con.WM_KEYDOWN, win32con.VK_CONTROL, 0)
win32api.Sleep(10)
win32api.SendMessage(hWnd, win32con.WM_KEYDOWN, 67, 0)
win32api.SendMessage(hWnd, win32con.WM_KEYUP, 67, 0)
win32api.SendMessage(hWnd, win32con.WM_KEYUP, win32con.VK_CONTROL, 0)
win32api.Sleep(10)
-- 
http://mail.python.org/mailman/listinfo/python-list


[OT] Python hunting

2009-05-25 Thread Iñigo Serna
Hi,

excuse me for the off-topic, but today I've seen this video and...
and... well, it's amazing

http://dailymotion.virgilio.it/search/chasse%2Ben%2Bafrique/video/xz3rz_les-pythons-aiment-les-jambes_animals

It's in French, but the audio it's not important.

Kind regards,
Iñigo Serna
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help! Troubled when embed python into C++

2009-05-25 Thread A. Cavallo
You need the:

int main(int argc, char * argv[])
{
Py_Initialize();
PySys_SetArgv(argc, argv);
PyRun_SimpleString("execfile(r'1.py')");
Py_Finalize();
return 0;
}


Regards,
Antonio

On Sunday 24 May 2009 11:42:13 孟炜 wrote:
> I have the following codes in C++:
> #include 
> void main(){
> Py_Initialize();
> PyRun_SimpleString("execfile(r'1.py')");
> Py_Finalize();
> return;
> }
>
> the following is in 1.py  :
> import Tkinter
> root=Tkinter.Tk()
> root2=Tkinter.Tk()
> root.mainloop()
> root2.mainloop()
>
> this is the output after I run the c++ program:
> Traceback (most recent call last):
>   File " ", line 1, in 
>   File "g:\volatile\1.py", line 2, in 
> root=Tkinter.Tk()
>   File "C:\Python26\lib\lib-tk\Tkinter.py", line 1638, i
> baseName = os.path.basename(sys.argv[0])
> AttributeError: 'module' object has no attribute 'argv'
>
> I am quite new to python ,anyone know what shoud i do to solve it?
> Thanks a lot!

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


Re: Can I get a technical explanation on the following error

2009-05-25 Thread Chris Rebert
On Sun, May 24, 2009 at 1:14 PM, grocery_stocker  wrote:
> On May 24, 11:47 am, Hans Müller  wrote:
>> Try this:
>>
>> print "\\"
>>
>> \ is the escape character, it masks the meaning of the next chararcter.
>>
>> If you write print "\" python tries to print " (the meaning of " as
>> the string delimiter is beeing masked) and finds no closing "
>> This is why you got the error.
>>
>
> So something like
>
> "\"
>
> changes the meaning of " ? How? Does it just shift the ASCII bit(s)?

No, the Python parser handles it when it parses string literals.

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


Re: Re: 4 hundred quadrillonth?

2009-05-25 Thread Dave Angel

Lawrence D'Oliveiro wrote:
In message , Christian 
Heimes wrote:


  

Welcome to IEEE 754 floating point land! :)



It used to be worse in the days before IEEE 754 became widespread. Anybody 
remember a certain Prof William Kahan from Berkeley, and the foreword he 
wrote to the Apple Numerics Manual, 2nd Edition, published in 1988? It's 
such a classic piece that I think it should be posted somewhere...



  
I remember the professor.  He was responsible for large parts of the 
Intel 8087 specification, which later got mostly codified as IEEE 754.  
In those days, the 8087 was a couple hundred extra dollars, so few 
machines had one.  And the software simulation was horribly slow (on a 
4.7 mhz machine).  So most compilers would have two math libraries.  If 
you wanted 8087 equivalence, and the hardware wasn't there, it was dog 
slow.  On the other hand, if you specified the other math package, it 
didn't benefit at all from the presence of the 8087.



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


Re: Re: Set a variable as in setter

2009-05-25 Thread Dave Angel

Kless wrote:

On 24 mayo, 12:27, Duncan Booth  wrote:
  

Kless  wrote:


Is there any way to simplify the next code? Because I'm setting a
variable by default of the same way than it's set in the setter.
  
---

class Foo(object):
   def __init__(self, bar):
  self._bar =elf._change(bar)  # !!! as setter
  

What's wrong with just doing this?:
self.bar =ar



Because 'bar' is going to be modified before of be saved.

  

You can't modify bar, it's immutable (string).

Did you try the suggestion, or just assume it won't work?  self.bar will 
call your setter method just as well from __init__() as it does outside 
the class.  So that eliminates the need for a separate _change() method.


class Foo(object):
   def __init__(self, bar):
   self.bar = bar

   @property
   def bar(self):
   return self._bar

   @bar.setter
   def bar(self, text):
   self._bar = text + "any change"

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


Re: Python -> R?

2009-05-25 Thread Dan Yamins
>
> If so, what are you using? I have read about RPy, is that a good
> solution?



Yes, I think it's quite a good solution.  It's not exactly 100% as
convenient as working directly in R, but that's IMO more than compensated by
the ability to use Python's syntax.

Make sure you use Rpy2 (the successor to Rpy) -- I find it significantly
better.   However, the online documentation is in IMHO a little thin -- at
least, in terms of examples -- so I once wrote up a little 5-minute tutorial
to show a friend how to use Rpy2.  I've attached it to this message -- you
may find it useful.

best
Dan


R_stuff.py
Description: Binary data
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to reuse TCP listening socket immediately after it was connected at least once?

2009-05-25 Thread Lawrence D'Oliveiro
In message , Igor Katson 
wrote:

> I have written a socket server and some arbitrary clients. When I
> shutdown the server, and do socket.close(), I cannot immediately start
> it again cause it has some open sockets in TIME_WAIT state. It throws
> address already in use exception at me.

There's a reason for that. It's to ensure that there are no leftover packets 
floating around the Internet somewhere, that you might mistakenly receive 
and think they were part of a new connection, when they were in fact part of 
an old one.

The right thing to do is try to ensure that all your connections are 
properly closed at shutdown. That may not be enough (if your server crashes 
due to bugs), so the other thing you need to do is retry the socket open, 
say, at 30-second intervals, until it succeeds.

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


Ted Dziuba

2009-05-25 Thread Lawrence D'Oliveiro
:

If you've ever had to build C extensions to Python on Windows, you can
join me in a feeling of satisfaction that someone at Microsoft is going
to have to figure this out. Let's call it retribution for Internet
Explorer 6.

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


Re: How to reuse TCP listening socket immediately after it was connected at least once?

2009-05-25 Thread Дамјан Георгиевски


> I have written a socket server and some arbitrary clients. When I
> shutdown the server, and do socket.close(), I cannot immediately start
> it again cause it has some open sockets in TIME_WAIT state. It throws
> address already in use exception at me. I have searched for that in
> google but haven't found a way to solve that.
> 
> Tried
> setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> but that does not help.

This should work, AFAIK you only need to do it before you call .bind(..) 
on the accept-ing socket



-- 
дамјан ( http://softver.org.mk/damjan/ )

Give me the knowledge to change the code I do not accept, 
the wisdom not to accept the code I cannot change, 
and the freedom to choose my preference.

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


Re: Regarding sort()

2009-05-25 Thread Jaime Fernandez del Rio
Hi Dhananjay,

Sort has several optional arguments, the function's signature is as follows:

s.sort([cmp[, key[, reverse]]])

If you store your data as a list of lists, to sort by the third column
you could do something like:

data.sort(None, lambda x : x[2])

For more complex sortings, as the one you propose, you need to define
a compare function, that determines if an object preceeds another or
not. In your case, something like:

def list_cmp( a, b) :
if a[2] > b[2] :
return 1
elif a[2] < b[2] :
return -1
else : # a[2] == b[2]
if a[5] > b[5] :
return 1
elif a[5] < b[5] :
return -1
else : # a[5] == b[5]
return 0

data.sort(list_cmp)

should do the trick for you...

Jaime


On Mon, May 25, 2009 at 9:29 AM, Dhananjay  wrote:
> Hello All,
>
> I have data set as follows:
>
> 24   GLU    3   47  LYS 6   3.909233    1
> 42   PRO    5   785 VAL 74  4.145114 1
> 54   LYS    6   785 VAL 74  4.305017  1
> 55   LYS    6   785 VAL 74  4.291098  1
> 56   LYS    7   785 VAL 74  3.968647  1
> 58   LYS    7   772 MET 73  4.385121   1
> 58   LYS    7   778 MET 73  4.422980   1
> 58   LYS    7   779 MET 73  3.954990   1
> 58   LYS    7   785 VAL 74  3.420554   1
> 59   LYS    7   763 GLN 72  4.431955   1
> 59   LYS    7   767 GLN 72  3.844037   1
> 59   LYS    7   785 VAL 74  3.725048   1
>
>
>
>
> I want to sort the data on the basis of 3rd column first and latter want to
> sort the sorted data (in first step) on the basis of 6th column.
>
> I tried sort() function but could not get the way how to use it.
>
> I am new to programming, please tell me how can I sort.
>
> Thanking you in advance 
>
> regards
>
>
>
>
> --
> --
> Dhananjay C Joshi
> Project Assistant
> Lab of Structural Biology,
> CDFD, Bldg.7, Gruhakalpa
> 5-4-399/B, Nampally
> Hyderabad- 51, India
> Tel: +91-40-24749404
> Fax: +91-40-24749448
> --
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



-- 
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus
planes de dominación mundial.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendMessage question

2009-05-25 Thread David Lyon

you might have more luck with http://pypi.python.org/pypi/SendKeys/0.3


On Sat, 23 May 2009 08:58:14 +0800, zhouhaifeng  wrote:
> Hi,I want to send "ctrl + A" and "ctrl + C" to a window,
> but my code can not work, who can help me ?
> 
> Thanks a lot!
> 
> hWnd = win32gui.FindWindow(None, "“pad")
> print hWnd
> if hWnd <> 0:
> point = (555, 175)
> x, y = point
> win32api.SetCursorPos(point)
> 
> win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x, y, 0, 0)
> win32api.SetCursorPos(point)
> win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
> 
> #ctrl + A
> win32api.SendMessage(hWnd, 0, win32con.VK_CONTROL, 0)
> win32api.Sleep(10)
> win32api.SendMessage(hWnd, 0, 65, 0)
> win32api.SendMessage(hWnd, win32con.KEYEVENTF_KEYUP, 65, 0)
> win32api.SendMessage(hWnd, win32con.KEYEVENTF_KEYUP, win32con.VK_CONTROL,
> 0)
> win32api.Sleep(10)
> 
> #ctrl + C
> win32api.SendMessage(hWnd, win32con.WM_KEYDOWN, win32con.VK_CONTROL, 0)
> win32api.Sleep(10)
> win32api.SendMessage(hWnd, win32con.WM_KEYDOWN, 67, 0)
> win32api.SendMessage(hWnd, win32con.WM_KEYUP, 67, 0)
> win32api.SendMessage(hWnd, win32con.WM_KEYUP, win32con.VK_CONTROL, 0)
> win32api.Sleep(10)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: 4 hundred quadrillonth?

2009-05-25 Thread Lawrence D'Oliveiro
In message , Dave Angel 
wrote:

> Lawrence D'Oliveiro wrote:
>
>> Anybody remember a certain Prof William Kahan from Berkeley ...
>>   
> I remember the professor.  He was responsible for large parts of the
> Intel 8087 specification, which later got mostly codified as IEEE 754.

The 8087 was poorly designed. It was stack-based, which caused all kinds of 
performance problems that never really went away, though I think Intel tried 
to patch over them with various SSE extensions. I believe AMD64 does have 
proper floating-point registers, at last.

Apple's implementation of IEEE 754 was so rigorous that, when Motorola 
introduced the 68881, which implemented a few of the "shortcuts" that Kahan 
reviled in his foreword, Apple added a patch to its SANE library to restore 
correct results, with the usual controversy over whether the performance 
loss was worth it. If you didn't think it was, you could always use the 
68881 instructions directly.

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


Re: How to send a compsite key to window

2009-05-25 Thread Gabriel Genellina

En Thu, 21 May 2009 09:38:26 -0300, zhouhaifeng 
escribió:


just like "ctrl + A",

I want to select all the text in a window, now , I operate right menu to
get it, but it can not work all the time.
so I want to send "ctrl + a" to the window


I'd do it in a different way: asuming it is an edit control, send the
control an EM_SETSEL message with start=0, end=-1.
http://msdn.microsoft.com/en-us/library/bb761661(VS.85).aspx

--
Gabriel Genellina

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


Re: Regarding sort()

2009-05-25 Thread Peter Otten
Dhananjay wrote:

> Hello All,
> 
> I have data set as follows:
> 
> 24   GLU3   47  LYS 6   3.9092331
> 42   PRO5   785 VAL 74  4.145114 1
> 54   LYS6   785 VAL 74  4.305017  1
> 55   LYS6   785 VAL 74  4.291098  1
> 56   LYS7   785 VAL 74  3.968647  1
> 58   LYS7   772 MET 73  4.385121   1
> 58   LYS7   778 MET 73  4.422980   1
> 58   LYS7   779 MET 73  3.954990   1
> 58   LYS7   785 VAL 74  3.420554   1
> 59   LYS7   763 GLN 72  4.431955   1
> 59   LYS7   767 GLN 72  3.844037   1
> 59   LYS7   785 VAL 74  3.725048   1
> 
> 
> 
> 
> I want to sort the data on the basis of 3rd column first and latter want
> to sort the sorted data (in first step) on the basis of 6th column.
> 
> I tried sort() function but could not get the way how to use it.
> 
> I am new to programming, please tell me how can I sort.
> 
> Thanking you in advance 

>>> data = """24   GLU3   47  LYS 6   3.909233
1
... 42   PRO5   785 VAL 74  4.145114 1
... 54   LYS6   785 VAL 74  4.305017  1
... 55   LYS6   785 VAL 74  4.291098  1
... 56   LYS7   785 VAL 74  3.968647  1
... 58   LYS7   772 MET 73  4.385121   1
... 58   LYS7   778 MET 73  4.422980   1
... 58   LYS7   779 MET 73  3.954990   1
... 58   LYS7   785 VAL 74  3.420554   1
... 59   LYS7   763 GLN 72  4.431955   1
... 59   LYS7   767 GLN 72  3.844037   1
... 59   LYS7   785 VAL 74  3.725048   1
... """
>>> rows = data.splitlines()
>>> rows.sort(key=lambda line: int(line.split()[5]))
>>> rows.sort(key=lambda line: int(line.split()[2]))
>>> print "\n".join(rows)
24   GLU3   47  LYS 6   3.9092331
42   PRO5   785 VAL 74  4.145114 1
54   LYS6   785 VAL 74  4.305017  1
55   LYS6   785 VAL 74  4.291098  1
59   LYS7   763 GLN 72  4.431955   1
59   LYS7   767 GLN 72  3.844037   1
58   LYS7   772 MET 73  4.385121   1
58   LYS7   778 MET 73  4.422980   1
58   LYS7   779 MET 73  3.954990   1
56   LYS7   785 VAL 74  3.968647  1
58   LYS7   785 VAL 74  3.420554   1
59   LYS7   785 VAL 74  3.725048   1

Python's list.sort() is "stable". Therefore you can sort by the 6th column 
first, and then by the third. Rows with the same value in the third column 
will not change their relative position.

To calculate the value for the n-th row you can either use the above lambda 
or the equivalent function:

def sixth_column(row):
columns = row.split() # split by whitespace
column = columns[5] # sixth column
return int(column) # convert to integer

If you don't convert to integer a row with "10" comes before a row with "2" 
in the column which is probably not what you want.

Peter

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


Re: Regarding sort()

2009-05-25 Thread Peter Otten
Peter Otten wrote:

 rows = data.splitlines()
 rows.sort(key=lambda line: int(line.split()[5]))
 rows.sort(key=lambda line: int(line.split()[2]))
 print "\n".join(rows)

Of course you can also sort in a single step:

>>> rows = data.splitlines()
>>> def key(row):
... columns = row.split()
... return int(columns[2]), int(columns[5])
...
>>> rows.sort(key=key)
>>> print "\n".join(rows)

Peter

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


Re: Regarding sort()

2009-05-25 Thread Chris Rebert
On Mon, May 25, 2009 at 12:29 AM, Dhananjay  wrote:
> Hello All,
>
> I have data set as follows:
>
> 24   GLU    3   47  LYS 6   3.909233    1
> 42   PRO    5   785 VAL 74  4.145114 1
> 54   LYS    6   785 VAL 74  4.305017  1
> 55   LYS    6   785 VAL 74  4.291098  1
> 56   LYS    7   785 VAL 74  3.968647  1
> 58   LYS    7   772 MET 73  4.385121   1
> 58   LYS    7   778 MET 73  4.422980   1
> 58   LYS    7   779 MET 73  3.954990   1
> 58   LYS    7   785 VAL 74  3.420554   1
> 59   LYS    7   763 GLN 72  4.431955   1
> 59   LYS    7   767 GLN 72  3.844037   1
> 59   LYS    7   785 VAL 74  3.725048   1
>
>
>
>
> I want to sort the data on the basis of 3rd column first and latter want to
> sort the sorted data (in first step) on the basis of 6th column.
>
> I tried sort() function but could not get the way how to use it.
>
> I am new to programming, please tell me how can I sort.

Well, first you need to actually *parse* the data. Right now, the
lines are (apparently) just unstructured strings, which makes them
quite hard to manipulate.

#completely untested code:
afile = open("path/to/the/file", 'r')
data = []
for line in afile:
fields = line.strip().split()
for i in (0,2,3,5,6): #integer field indices
fields[i] = int(fields[i])
fields[-2] = float(fields[-2]) #float field index
data.append(fields)
data.sort(key = lambda item: (item[2], item[5]) )


I would highly recommend you go through one or more Python tutorials
and/or read a book on Python (or programming generally).

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


Re: Regarding sort()

2009-05-25 Thread Chris Rebert
On Mon, May 25, 2009 at 12:51 AM, Jaime Fernandez del Rio
 wrote:
> Hi Dhananjay,
>
> Sort has several optional arguments, the function's signature is as follows:
>
> s.sort([cmp[, key[, reverse]]])
>
> If you store your data as a list of lists, to sort by the third column
> you could do something like:
>
> data.sort(None, lambda x : x[2])
>
> For more complex sortings, as the one you propose, you need to define
> a compare function, that determines if an object preceeds another or
> not. In your case, something like:

Erm, using a compare function rather than a key function slows down
sorting /significantly/. In fact, the `cmp` parameter to list.sort()
has been *removed* in Python 3.0 because of this.

Also, top-posting is evil. Please avoid it in the future.

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


Re: writing on file not until the end

2009-05-25 Thread Alessandro
On May 25, 8:38 am, Peter Otten <__pete...@web.de> wrote:
> Alexzive wrote:
> > I am a newby with python. I wrote the following code to extract a text
> > from a file and write it to another file:
>
> > linestring = open(path, 'r').read() #read all the inp file in
> > linestring
>
> > i=linestring.index("*NODE")
> > i=linestring.index("E",i)
> > e=linestring.index("*",i+10)
> > textN = linestring[i+2:e-1] # crop the ELement+nodes list
> > Nfile = open("N.txt", "w")
> > Nfile.write(textN)
>
> > unfortunately when I check N.txt some lines are missing (it only crop
> > until "57, 0.231749688431, 0.0405121944142" but I espect the final
> > line to be "242, 0.2979675, 0.224605896461". I check textN and it has
> > all the lines until "242..")
>
> > when I try Nfile.write(textN) again it writes some more lines but
> > still not all!
> > what is wrong whit this?
>
> Do you check the contents of the resulting file by feeding it to another
> program?
>
> > 53, 0.170973146505, 0.0466686190136
> > 57, 0.231749688431, 0.0405121944142
> > t60, 0.250420691759, 0.0399644155193
> > 58, 0.234883810317, 0.0488399925217
> > 61, 0.2666025, 0.03541845
>
> There's a "t" at the start of the line after the last line you claim to see,
> so maybe your input file is corrupt and your reader stumbles over that line.
>
> Peter

Thank you all for replying.

- yes, I oversaw the "t" at "60, 0.250420691759, 0.0399644155193". I
then removed it for the following tests
- changing from "e-1" to "e" apparently solved the problem, BUT I then
realized it was just because a second access to the file (still open)
seems to be the real solution --> after Nfile.close() e retrying with
"e" instead of "e-1" the problem is still there.
- I also tryied to inizialize e and i to zero --> nothing changes
- until now, the only solution which works is to repeat the code while
the file is still open, which means a quite redundant code:

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

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


Re: Need Help

2009-05-25 Thread abosalim
On May 25, 5:39 am, Steven D'Aprano
 wrote:
> On Mon, 25 May 2009 00:16:19 +0200, Piet van Oostrum wrote:
> > By the way, it is better to add python code as attachment instead of
> > inline text because some news software might fold the lines like in your
> > posting, making it difficult to reconstruct the code.
>
> Except that some news software might not show the attachment at all.
>
> --
> Steven

I modified the method,but it can't identified it.(self.res=textcorrect
(self.string)
NameError: global name 'textcorrect' is not defined)
This is what i done:


import re, collections
from Tkinter import *
from nltk_lite import tokenize
class Ex3:
def __init__(self, master):
self.frame = Frame(master)
self.frame.pack()
self.field = Entry(self.frame)
self.field.pack(side=TOP)
self.contents = StringVar()
self.field.config(text=self.contents)
self.stop = Button(self.frame, text="Exit", fg="red",
command=self.frame.quit)
self.stop.pack(side=RIGHT)
self.enter = Button(self.frame, text="Correct", fg="black",
command=self.new)
self.enter.pack(side=LEFT)



def words(text): return re.findall('[a-z]+', text.lower())
def train(features):
model = collections.defaultdict(lambda: 1)
for f in features:
model[f] += 1
return model
NWORDS = train(words(file('big1.txt').read()))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def edits1(word):
s = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [a + b[1:] for a, b in s if b]
transposes = [a + b[1] + b[0] + b[2:] for a, b in s if len(b)
>1]
replaces = [a + c + b[1:] for a, b in s for c in alphabet if
b]
inserts = [a + c + b for a, b in s for c in alphabet]
return set(deletes + transposes + replaces + inserts)
def known_edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if
e2 in NWORDS)
def known(words):
return set(w for w in words if w in NWORDS)
def correct(word):
candidates = known([word]) or known(edits1(word)) or
known_edits2(word) or [word]
return max(candidates, key=lambda w: NWORDS[w])
def textcorrect(str):
s=[]
for i in tokenize.whitespace(str):
s.append(correct(i))
result= " ".join(s)
return result
def new(self):
self.string = self.contents.get()
#pass contents of textfield to textcorrect()method above
self.res=textcorrect(self.string)
self.frame2 = Frame()
self.frame2.pack()
self.words = Label(self.frame2, text=self.res, fg="blue", font=
("Arial", 16))
self.words.pack()

root = Tk()
Ex3 = Ex3(root)
root.mainloop()













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


Re: Multiprocessing and file I/O

2009-05-25 Thread Infinity77
Hi Paul & All,

On May 24, 4:16 pm, Paul Boddie  wrote:
> On 24 Mai, 16:13, Infinity77  wrote:
>
>
>
> > No, the processing of the data is fast enough, as it is very simple.
> > What I was asking is if anyone could share an example of using
> > multiprocessing to read a file, along the lines I described above.
>
> Take a look at this section in an article about multi-threaded
> processing of large files:
>
> http://effbot.org/zone/wide-finder.htm#a-multi-threaded-python-solution

Thank you for the pointer, I have read the article and the follow-ups
with much interest... it's unfortunate Python is no more on the first
place though :-D
I'll see if I can come up with a faster implementation of my (f2py-
fortran-based) Python module using multiprocessing.

Thank you.

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


Re: Optimizing math functions

2009-05-25 Thread Charlie
Esmail  hotmail.com> writes:

> 
> Charlie wrote:
> > 
> > You might also look at:
> > http://pyparasol.sourceforge.net/example_1.html
> 
> Thanks for this lead, I had never heard of parasol before. Do you know
> if this also works under Linux? The docs mention only the Windows platform,
> but given that this is Python perhaps it is save to assume this would also
> work under Linux?
> 
> Esmail
> 

It might work under Linux, however, it was developed under Windows and, to my
knowledge, has never been tested on a Linux machine.  Basic operation only
depends on installations of matplotlib, numpy, and scipy.  Those packages are
all available on Linux.

If you try it, I'd like to know the outcome.

The parasol options to launch Microsoft Office apps Excel, Power Point, and
Word; or the ray tracing app POV-Ray, will very likely fail.

Charlie

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


Re: writing on file not until the end

2009-05-25 Thread Peter Otten
Alessandro wrote:

> - until now, the only solution which works is to repeat the code while
> the file is still open, which means a quite redundant code:
> 
> linestring = open(path, 'r').read()
> i=linestring.index("*NODE")
> i=linestring.index("E",i)
> e=linestring.index("*",i+10)
> textN = linestring[i+2:e-1] # crop the ELement+nodes list
> Nfile = open("N.txt", "w")
> Nfile.write(textN)
> 
> linestring = open(path, 'r').read()
> i=linestring.index("*NODE")
> i=linestring.index("E",i)
> e=linestring.index("*",i+10)
> textN = linestring[i+2:e-1] # crop the ELement+nodes list
> Nfile = open("N.txt", "w")
> Nfile.write(textN)

Is this the complete script? The only effect of the second copy of your code 
above is that it implicitly closes the first Nfile. Otherwise it is cargo 
cult.

Peter 

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


Re: writing on file not until the end

2009-05-25 Thread John Machin
On May 25, 6:30 pm, Alessandro  wrote:
> On May 25, 8:38 am, Peter Otten <__pete...@web.de> wrote:
>
>
>
> > Alexzive wrote:
> > > I am a newby with python. I wrote the following code to extract a text
> > > from a file and write it to another file:
>
> > > linestring = open(path, 'r').read() #read all the inp file in
> > > linestring
>
> > > i=linestring.index("*NODE")
> > > i=linestring.index("E",i)
> > > e=linestring.index("*",i+10)
> > > textN = linestring[i+2:e-1] # crop the ELement+nodes list
> > > Nfile = open("N.txt", "w")
> > > Nfile.write(textN)
>
> > > unfortunately when I check N.txt some lines are missing (it only crop
> > > until "57, 0.231749688431, 0.0405121944142" but I espect the final
> > > line to be "242, 0.2979675, 0.224605896461". I check textN and it has
> > > all the lines until "242..")
>
> > > when I try Nfile.write(textN) again it writes some more lines but
> > > still not all!
> > > what is wrong whit this?
>
> > Do you check the contents of the resulting file by feeding it to another
> > program?
>
> > > 53, 0.170973146505, 0.0466686190136
> > > 57, 0.231749688431, 0.0405121944142
> > > t60, 0.250420691759, 0.0399644155193
> > > 58, 0.234883810317, 0.0488399925217
> > > 61, 0.2666025, 0.03541845
>
> > There's a "t" at the start of the line after the last line you claim to see,
> > so maybe your input file is corrupt and your reader stumbles over that line.
>
> > Peter
>
> Thank you all for replying.
>
> - yes, I oversaw the "t" at "60, 0.250420691759, 0.0399644155193". I
> then removed it for the following tests
> - changing from "e-1" to "e" apparently solved the problem, BUT I then
> realized it was just because a second access to the file (still open)
> seems to be the real solution --> after Nfile.close() e retrying with
> "e" instead of "e-1" the problem is still there.
> - I also tryied to inizialize e and i to zero --> nothing changes
> - until now, the only solution which works is to repeat the code while
> the file is still open, which means a quite redundant code:
>
> linestring = open(path, 'r').read()
> i=linestring.index("*NODE")
> i=linestring.index("E",i)
> e=linestring.index("*",i+10)
> textN = linestring[i+2:e-1] # crop the ELement+nodes list
> Nfile = open("N.txt", "w")
> Nfile.write(textN)

Insert here:
Nfile.close()

Remove the redundant second pass.

> linestring = open(path, 'r').read()
> i=linestring.index("*NODE")
> i=linestring.index("E",i)
> e=linestring.index("*",i+10)
> textN = linestring[i+2:e-1] # crop the ELement+nodes list
> Nfile = open("N.txt", "w")
> Nfile.write(textN)


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


Re: Can I get a technical explanation on the following error

2009-05-25 Thread Niklas Norrthon
On 25 Maj, 05:29, Jim Garrison  wrote:
> And as an interesting exercise, try
>
> print r'test \'
> print r'test \\'
>
> Because of the way raw string parsing is defined, neither of these will
> pass the parser.  In fact, a raw string cannot have a backslash as
> its last character.

Tried it:
>>> r'test \'
SyntaxError: EOL while scanning string literal

>>> r'test \\'
'test '

Second one is accepted. See the language reference section 2.4.1 as of
why; 
http://www.python.org/doc/current/reference/lexical_analysis.html?highlight=raw%20strings#string-literals

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


Re: writing on file not until the end

2009-05-25 Thread Alessandro
On May 25, 10:58 am, John Machin  wrote:
> On May 25, 6:30 pm, Alessandro  wrote:
>
>
>
> > On May 25, 8:38 am, Peter Otten <__pete...@web.de> wrote:
>
> > > Alexzive wrote:
> > > > I am a newby with python. I wrote the following code to extract a text
> > > > from a file and write it to another file:
>
> > > > linestring = open(path, 'r').read() #read all the inp file in
> > > > linestring
>
> > > > i=linestring.index("*NODE")
> > > > i=linestring.index("E",i)
> > > > e=linestring.index("*",i+10)
> > > > textN = linestring[i+2:e-1] # crop the ELement+nodes list
> > > > Nfile = open("N.txt", "w")
> > > > Nfile.write(textN)
>
> > > > unfortunately when I check N.txt some lines are missing (it only crop
> > > > until "57, 0.231749688431, 0.0405121944142" but I espect the final
> > > > line to be "242, 0.2979675, 0.224605896461". I check textN and it has
> > > > all the lines until "242..")
>
> > > > when I try Nfile.write(textN) again it writes some more lines but
> > > > still not all!
> > > > what is wrong whit this?
>
> > > Do you check the contents of the resulting file by feeding it to another
> > > program?
>
> > > > 53, 0.170973146505, 0.0466686190136
> > > > 57, 0.231749688431, 0.0405121944142
> > > > t60, 0.250420691759, 0.0399644155193
> > > > 58, 0.234883810317, 0.0488399925217
> > > > 61, 0.2666025, 0.03541845
>
> > > There's a "t" at the start of the line after the last line you claim to 
> > > see,
> > > so maybe your input file is corrupt and your reader stumbles over that 
> > > line.
>
> > > Peter
>
> > Thank you all for replying.
>
> > - yes, I oversaw the "t" at "60, 0.250420691759, 0.0399644155193". I
> > then removed it for the following tests
> > - changing from "e-1" to "e" apparently solved the problem, BUT I then
> > realized it was just because a second access to the file (still open)
> > seems to be the real solution --> after Nfile.close() e retrying with
> > "e" instead of "e-1" the problem is still there.
> > - I also tryied to inizialize e and i to zero --> nothing changes
> > - until now, the only solution which works is to repeat the code while
> > the file is still open, which means a quite redundant code:
>
> > linestring = open(path, 'r').read()
> > i=linestring.index("*NODE")
> > i=linestring.index("E",i)
> > e=linestring.index("*",i+10)
> > textN = linestring[i+2:e-1] # crop the ELement+nodes list
> > Nfile = open("N.txt", "w")


I closed and restarted the python console. Now this code (with added
"Nfile.close()" at the end) seems to work properly:

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1]
Nfile = open("N.txt", "w")
Nfile.write(textN)
Nfile.close()

thanks, Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regarding sort()

2009-05-25 Thread Jaime Fernandez del Rio
On Mon, May 25, 2009 at 10:15 AM, Chris Rebert  wrote:
> Erm, using a compare function rather than a key function slows down
> sorting /significantly/. In fact, the `cmp` parameter to list.sort()
> has been *removed* in Python 3.0 because of this.

Makes a lot of sense, as you only have to run the slow python function
N times, rather than doing it N log N times...

So I guess I have also learned today that the internal cmp can handle
lists and tuples properly, i.e (1,7) > (1,5), definitely a much neater
way of doing things: the reasons are starting to pile to fare 2.6
goodbye and move on to 3.0...

>
> Also, top-posting is evil. Please avoid it in the future.

Must be the mad scientist wanting to take over the world in me. ;-)

Note taken...

Jaime

>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>



-- 
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus
planes de dominación mundial.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I get a technical explanation on the following error

2009-05-25 Thread pdpi
On May 24, 6:41 pm, grocery_stocker  wrote:
> How come something like '\'  causes an error? Here is what I mean.
>
> [cdal...@localhost ~]$ python
> Python 2.6.2 (r262:71600, May  3 2009, 17:04:44)
> [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> 
> print "test \"
>
>   File "", line 1
>     print "test \"
>                  ^
> SyntaxError: EOL while scanning string literal
>
>
>
> I mean, isn't the '\' a character just like the letter 't'?

Ask yourself: 'How would I tell Python to print the " character when
it's used as the string delimitation character?'. Unlike us (you
probably didn't even blink at the usage of ' in "it's" versus its
usage as quote delimiters in that sentence), computers can't quite
tell usage from context. So you have to come up with a way to express
ourselves. Many languages use the \ character as an escape character,
which modifies the meaning of the next character in a string. \n, \t,
\" are all common. Other languages use other conventions -- I program
ABAP professionally, and it uses ' as the string delimiter, and '' as
a literal -- so a string with one single ' is expressed as .

The python interpreter isn't doing any bit magic here, it's just
reading a \ character followed by a " character and changing its
interpretation of " appropriately.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regarding sort()

2009-05-25 Thread Neil Crighton
Dhananjay  gmail.com> writes:

> I want to sort the data on the basis of 3rd column first and latter want to 
> sort the sorted data (in first step) on the basis of 6th column.I tried 
> sort() function but could not get the way how to use it.I am new to 
> programming, please tell me how can I sort.Thanking you in advance 


If you want to use numpy, you can do this (in version 1.2 or above):

Assuming the columns of data you posted are in a text file called temp.txt,

>>> import numpy as np

Read the file to a numpy array and convert to int, float or string as
appropriate. Each column is given a name - 'f0' is the first column, 'f1' is the
second column, and so on.

>>> data = np.genfromtxt('temp.txt', dtype=None)

Sort rows by the 3rd column, and if the 3rd column values are the same, sort by
the 6th column:

>>> data.sort(order=['f2', 'f5'])



Neil



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


Re: 4 hundred quadrillonth?

2009-05-25 Thread Hendrik van Rooyen
"Dennis Lee Bieber"  wrote:


> On Sun, 24 May 2009 22:47:51 +1200, Lawrence D'Oliveiro
>  declaimed the following in
> gmane.comp.python.general:
> 
> 
> > As for exactitude in physics, Gregory Chaitin among others has been trying 
> > to rework physics to get rid of real numbers altogether.
> 
> By decreeing that the value of PI is 3?

naah - that would be too crude, even for a physicist - pi is 22//7..

;-)

- Hendrik

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


INVITATION FLIB: Fringe/Functional Languages In Barcelona user group.

2009-05-25 Thread jos koot
INVITATION
We, (Jose A. Ortega Ruiz j...@gnu.org, Andy Wingo wi...@pobox.com and
Jos Koot jos.k...@telefonica.net) have scheduled a meeting in
Barcelona (Spain) with the intention to form a user group FLIB: Fringe/
Functional Languages In Barcelona. Everyone who is interested in
shaping this group is invited to share us.

June 10 2009 at 7.30 PM
Calle del Pi 3 Principal Interior (first floor)
Barcelona
Spain

Preliminary program

Welcome.
Short discussion about what FLIB should be and should not be.
Jose: communication about FUEL (Factors Ultimate Emacs Library)
Jos : 5 minute click/lightning talk on his recent experiences with
PLT's redex library.
You: 5 minut click/lightning talk.
Many more of you: 5 minut click/lightning talks.
Informal meeting in the conference room or in a bar or simple
restaurant within walking distance.

The preferred language is English, but click talks can also be in
Castiliano (Spanish) or in Catala.
If you intend to come, let us know by email, please, in order that we
can estimate the number of participants.
If you intend to do a click talk, please let us know too.

Please forward this invitation to relevant mailing lists we may have
missed.
Hope to meet you on June the tenth.

j...@gnu.org
wi...@pobox.com
jos.k...@telefonica.net
-- 
http://mail.python.org/mailman/listinfo/python-list


what I would like python.el to do (and maybe it does)

2009-05-25 Thread Giovanni Gherdovich

Hello everybody,

basically I'm writing here since I cannot
make my python.el work (a major mode for writing
python with emacs), but I would also like to share
my user experience and tell you what I think
an emacs mode should do, why do I like them
and hopefully have some feedbacks to see if I
misunderstood/underestimate something.


== 1) my python.el doesn't behave very well ==

I learnt somewhere
http://www.emacswiki.org/emacs/PythonMode
that there are two major emacs mode for python around:
python-mode.el and python.el.
Asking my Emacs 22.3.1 about the variable load-path
issuing
'C-h v load-path RET
I see that
/usr/share/emacs/22.3/lisp/progmodes
is in that path; there I find a file named
python.elc
which I assume to be some kind of emacs lisp bytecode
since is pretty much unreadable.

So I searched the web for a plain version of it,
finding that the feature I use more, i.e.
sending a line of a text file to the
python buffer for evaluation (see below), correspond
to the key sequence

\C-c\C-c

(well, it's python-send-buffer, so maybe not a single
line but the whole buffer; the closest to my needs, anyway).
However: I open my Emacs, issue M-x python-mode,
then M-x run-python to have the interpreter in
a second buffer, I type something in the
first buffer and then C-c C-c, but nothing happens.

Am I missing something?
Do I have any hope of having some sort of
send-line-to-python-buffer function working?


== 2) How do I use emacs modes for interpreted languages ==

Please note that what follows is just the personal perspective
of an unexperienced user.

Syntax highlighting is a great thing, but is not as critical
to me as the feature I describe below.

When I work with interpreted languages, I really hate doing it
in the shell; after 20 commands I easily lose control on
what happens and on which definitions are around.

I use Emacs instead, so that I can have two buffers; in the
first I type my expressions, in the second I evaluate them
using some key bindings so that I can easily send the text
from the first buffer to the second one line by line.

In this way I can easily refactor my code, and eventually package it
in a script if I like.
Usually after a while the intepreter buffer is a big mess,
so I restart it but my code is safe and sound in the first buffer.

To do so, I don't really need a major mode, I admit; I just need
to put the following code in my .emacs:

(fset 'send-line-other-window
 [?\C-a ?\C- ?\C-e ?M-w right
  ?C-x ?o ?C-y return ?\C-x ?o])
(global-set-key [f11] 'send-line-other-window)

Then I open emacs, C-x 2 to have a second buffer,
C-x o to switch to it and M-x shell to run bash in it.
Then, in the case of python, I run "python" in the
bash buffer. Then I type my code in the first and with F11
I send lines to the interpreter.

But since i like to do it The Right Way, I would
like to let the python-mode worry about this...

Sorry if this is just a bunch of obvious thoughts to most of you.

Regards,
Giovanni

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


Re: Python -> R?

2009-05-25 Thread Esmail

Dan Yamins wrote:



If so, what are you using? I have read about RPy, is that a good
solution?



Yes, I think it's quite a good solution.  It's not exactly 100% as 
convenient as working directly in R, but that's IMO more than 
compensated by the ability to use Python's syntax.   

Make sure you use Rpy2 (the successor to Rpy) -- I find it significantly 
better.   However, the online documentation is in IMHO a little thin -- 
at least, in terms of examples -- so I once wrote up a little 5-minute 
tutorial to show a friend how to use Rpy2.  I've attached it to this 
message -- you may find it useful.


thanks! I played around just a bit with RPy yesterday, but I had no
idea there was Rpy2 .. I'll be sure to take a look.

Best,
Esmail

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


Re: Python -> R?

2009-05-25 Thread Esmail

edexter wrote:


I was playing around with a r sound module and looking at the graphic
stuff and I have downloaded the rpy thing but I have never used
it  I have been looking for a good excuse to use it...  It looks
like it could be useful for graphing stuff or maybe alogrithmic
(spelling I am sure) composition...  The R people have a publication
somewhat like the python papers where you might find something
intresting.


Thanks .. I tried out a few simple things with RPy yesterday (calling
a few functions and some simple plots) and it seemed to work ok.

Esmail

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


Re: email scanning for X-Spam-Score

2009-05-25 Thread Peter Otten
Helmut Jarausch wrote:

> my emails received from our mailing system contain a field like
> 
> X-Spam-Score: -2.2
> 
> Given the full email message in 'msg'
> I've tried
>mailmsg = email.message_from_string(msg)
>SPAM_CORE = mailmsg['X-Spam-Score']
> but it doesn't work.

What do you mean by "doesn't work"?

> What am I missing?

No idea. It seems to "work" over here:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import email
>>> spam = open("spam.txt").read()
>>> m = email.message_from_string(spam)
>>> m["X-Spam-Score"]
'15.0 (+++)'

Peter

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


Re: email scanning for X-Spam-Score

2009-05-25 Thread Helmut Jarausch

Peter Otten wrote:

Helmut Jarausch wrote:


my emails received from our mailing system contain a field like

X-Spam-Score: -2.2

Given the full email message in 'msg'
I've tried
   mailmsg = email.message_from_string(msg)
   SPAM_CORE = mailmsg['X-Spam-Score']
but it doesn't work.


What do you mean by "doesn't work"?


What am I missing?


No idea. It seems to "work" over here:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import email
spam = open("spam.txt").read()
m = email.message_from_string(spam)
m["X-Spam-Score"]

'15.0 (+++)'

Peter



Oh yes, thanks,
my test email was quite long and so I overlooked it contained 2 emails instead
of just one.

Sorry for the noise,
Helmut.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to reuse TCP listening socket immediately after it was connected at least once?

2009-05-25 Thread Roy Smith
In article ,
 Lawrence D'Oliveiro  wrote:

> In message , Roy Smith wrote:
> 
> > In article ,
> >  Lawrence D'Oliveiro  wrote:
> > 
> >> The right thing to do is try to ensure that all your connections are
> >> properly closed at shutdown. That may not be enough (if your server
> >> crashes due to bugs), so the other thing you need to do is retry the
> >> socket open, say, at 30-second intervals, until it succeeds.
> > 
> > That may be a reasonable thing to do for production code, but when you're
> > building and debugging a server, it's a real pain to not be able to
> > restart it quickly whenever you want (or need) to.
> 
> On the contrary, I run exactly the same logic--and that includes socket-
> handling logic--in both test and production servers. How else can I be sure 
> it'll work properly in production?

If running without SO_REUASEADDR works for you, that's great.  I was just 
pointing out how it can be useful in cases such as the OP's, where he's 
getting bind errors when he restarts his server.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what I would like python.el to do (and maybe it does)

2009-05-25 Thread Arnaud Delobelle
Giovanni Gherdovich  writes:

> Hello everybody,

Hi

> (well, it's python-send-buffer, so maybe not a single
> line but the whole buffer; the closest to my needs, anyway).
> However: I open my Emacs, issue M-x python-mode,
> then M-x run-python to have the interpreter in
> a second buffer, I type something in the
> first buffer and then C-c C-c, but nothing happens.

What platform are you on?

I use Aquamacs (which is a packaged Emacs for macos X, based on GNU
Emacs 22.3.1) and for me C-c C-c works.  It executes the content of the
current buffer in a Python interactive buffer called *Python* (which it
creates if it doesn't exist).

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


formating query with empty parameter

2009-05-25 Thread someone
Hello!

if one of parameter in values is empty, I'm getting
TypeError: not enough arguments for format string

But how to handle such situation? It is ok for DB, that some of values
are empty.



def __insert(self, data):
query = """
BEGIN;
INSERT INTO table
(a,  b,  c,  d,  e,  f,  g)
VALUES
(%s, %s, %s, %s, %s, %s, %s);
COMMIT;
"""
values = [
data['a'],
data['b'],
data['c'],
data['d'],
data['e'],
data['f'],
data['g']
]
self.db.execute(query, *values)



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


Re: formating query with empty parameter

2009-05-25 Thread Diez B. Roggisch
someone wrote:

> Hello!
> 
> if one of parameter in values is empty, I'm getting
> TypeError: not enough arguments for format string
> 
> But how to handle such situation? It is ok for DB, that some of values
> are empty.
> 
> 
> 
> def __insert(self, data):
> query = """
> BEGIN;
> INSERT INTO table
> (a,  b,  c,  d,  e,  f,  g)
> VALUES
> (%s, %s, %s, %s, %s, %s, %s);
> COMMIT;
> """
> values = [
> data['a'],
> data['b'],
> data['c'],
> data['d'],
> data['e'],
> data['f'],
> data['g']
> ]
> self.db.execute(query, *values)

You need to pass 

None

then as that parameter.

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


Re: formating query with empty parameter

2009-05-25 Thread Pet
On May 25, 2:15 pm, "Diez B. Roggisch"  wrote:
> someone wrote:
> > Hello!
>
> > if one of parameter in values is empty, I'm getting
> > TypeError: not enough arguments for format string
>
> > But how to handle such situation? It is ok for DB, that some of values
> > are empty.
>
> > def __insert(self, data):
> >         query = """
> >             BEGIN;
> >                 INSERT INTO table
> >                     (a,  b,  c,  d,  e,  f,  g)
> >                     VALUES
> >                     (%s, %s, %s, %s, %s, %s, %s);
> >             COMMIT;
> >             """
> >         values = [
> >             data['a'],
> >             data['b'],
> >             data['c'],
> >             data['d'],
> >             data['e'],
> >             data['f'],
> >             data['g']
> >             ]
> >         self.db.execute(query, *values)
>
> You need to pass
>
> None

Hi,

thanks for reply.
Unfortunately, it doesn't work. Still getting TypeError: not enough
arguments for format string


>
> then as that parameter.
>
> Diez

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


Re: formating query with empty parameter

2009-05-25 Thread Pet
On May 25, 2:25 pm, Pet  wrote:
> On May 25, 2:15 pm, "Diez B. Roggisch"  wrote:
>
>
>
>
>
> > someone wrote:
> > > Hello!
>
> > > if one of parameter in values is empty, I'm getting
> > > TypeError: not enough arguments for format string
>
> > > But how to handle such situation? It is ok for DB, that some of values
> > > are empty.
>
> > > def __insert(self, data):
> > >         query = """
> > >             BEGIN;
> > >                 INSERT INTO table
> > >                     (a,  b,  c,  d,  e,  f,  g)
> > >                     VALUES
> > >                     (%s, %s, %s, %s, %s, %s, %s);
> > >             COMMIT;
> > >             """
> > >         values = [
> > >             data['a'],
> > >             data['b'],
> > >             data['c'],
> > >             data['d'],
> > >             data['e'],
> > >             data['f'],
> > >             data['g']
> > >             ]
> > >         self.db.execute(query, *values)
>
> > You need to pass
>
> > None
>
> Hi,
>
> thanks for reply.
> Unfortunately, it doesn't work. Still getting TypeError: not enough
> arguments for format string
>
>
>
>
>
> > then as that parameter.
>
> > Diez

Sorry, for previous quick post. Actually it works now, I've missed
some other parameter in list

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


Re: formating query with empty parameter

2009-05-25 Thread Peter Otten
Pet wrote:

> > someone wrote:
> > > Hello!
> >
> > > if one of parameter in values is empty, I'm getting
> > > TypeError: not enough arguments for format string
> >
> > > But how to handle such situation? It is ok for DB, that some of values
> > > are empty.
> >
> > > def __insert(self, data):
> > > query = """
> > > BEGIN;
> > > INSERT INTO table
> > > (a,  b,  c,  d,  e,  f,  g)
> > > VALUES
> > > (%s, %s, %s, %s, %s, %s, %s);
> > > COMMIT;
> > > """
> > > values = [
> > > data['a'],
> > > data['b'],
> > > data['c'],
> > > data['d'],
> > > data['e'],
> > > data['f'],
> > > data['g']
> > > ]
> > > self.db.execute(query, *values)
> >
> > You need to pass
> >
> > None
> 
> Hi,
> 
> thanks for reply.
> Unfortunately, it doesn't work. Still getting TypeError: not enough
> arguments for format string
> 

The code you posted doesn't match that error message. You have to invoke 
cursor.execute() as 

cursor.execute(query, values) # correct

, not 

cursor.execute(query, *values) # wrong

or

cursor.execute(query % values) # wrong

The length of values must match the number of "%s" occurences in the sql 
query, but as Diez indicated you may pass None for every field that allows a 
NULL value in the table.

Peter


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


Re: formating query with empty parameter

2009-05-25 Thread Tim Chase

if one of parameter in values is empty, I'm getting
TypeError: not enough arguments for format string
But how to handle such situation? It is ok for DB, that some of values
are empty.
def __insert(self, data):
query = """
BEGIN;
INSERT INTO table
(a,  b,  c,  d,  e,  f,  g)
VALUES
(%s, %s, %s, %s, %s, %s, %s);
COMMIT;
"""
values = [
data['a'],
data['b'],
data['c'],
data['d'],
data['e'],
data['f'],
data['g']
]
self.db.execute(query, *values)


Sorry, for previous quick post. Actually it works now, I've missed
some other parameter in list


To stave off this problem, I often use:

  values = [
   data['a'],
   data['b'],
   data['c'],
   data['d'],
   data['e'],
   data['f'],
   data['g'],
   ]
  params = ', '.join('%s' for _ in values)
  query = """
BEGIN;
  INSERT INTO table
(a,b,c,d,e,f,g)
  VALUES (%s);
COMMIT;
""" % params
  self.db.execute(query, values)

If the indexes are named the same as the fieldnames, or you have 
a mapping of them, I tend to use something like


  field_map = {
# dictionary_index: database_fieldname
# data['a'] -> table.f1
'a': 'f1',
'b': 'f2',
'c': 'f3',
# ...
}
  name_value_pairs = (
(data[k], v)
for k,v
in fieldmap.iteritems())
  values, fieldnames = zip(*name_value_pairs)
  # may want to do fieldname escaping here:
  fieldname_string = ', '.join(fieldnames)
  params = ', '.join('%s' for _ in ordering)

  query = """
BEGIN;
  INSERT INTO table (%s) VALUES (%s);
COMMIT;
""" % (fieldname_string, params)
  self.db.execute(query, values)

-tkc




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


Re: what I would like python.el to do (and maybe it does)

2009-05-25 Thread J Kenneth King
Giovanni Gherdovich  writes:

> Hello everybody,
>
> basically I'm writing here since I cannot
> make my python.el work (a major mode for writing
> python with emacs), but I would also like to share
> my user experience and tell you what I think
> an emacs mode should do, why do I like them
> and hopefully have some feedbacks to see if I
> misunderstood/underestimate something.
>
>
> == 1) my python.el doesn't behave very well ==
>
> I learnt somewhere
> http://www.emacswiki.org/emacs/PythonMode
> that there are two major emacs mode for python around:
> python-mode.el and python.el.
> Asking my Emacs 22.3.1 about the variable load-path
> issuing
> 'C-h v load-path RET
> I see that
> /usr/share/emacs/22.3/lisp/progmodes
> is in that path; there I find a file named
> python.elc
> which I assume to be some kind of emacs lisp bytecode
> since is pretty much unreadable.
>
> So I searched the web for a plain version of it,
> finding that the feature I use more, i.e.
> sending a line of a text file to the
> python buffer for evaluation (see below), correspond
> to the key sequence
>
> \C-c\C-c
>
> (well, it's python-send-buffer, so maybe not a single
> line but the whole buffer; the closest to my needs, anyway).
> However: I open my Emacs, issue M-x python-mode,
> then M-x run-python to have the interpreter in
> a second buffer, I type something in the
> first buffer and then C-c C-c, but nothing happens.
>
> Am I missing something?
> Do I have any hope of having some sort of
> send-line-to-python-buffer function working?
>
>
> == 2) How do I use emacs modes for interpreted languages ==
>
> Please note that what follows is just the personal perspective
> of an unexperienced user.
>
> Syntax highlighting is a great thing, but is not as critical
> to me as the feature I describe below.
>
> When I work with interpreted languages, I really hate doing it
> in the shell; after 20 commands I easily lose control on
> what happens and on which definitions are around.
>
> I use Emacs instead, so that I can have two buffers; in the
> first I type my expressions, in the second I evaluate them
> using some key bindings so that I can easily send the text
> from the first buffer to the second one line by line.
>
> In this way I can easily refactor my code, and eventually package it
> in a script if I like.
> Usually after a while the intepreter buffer is a big mess,
> so I restart it but my code is safe and sound in the first buffer.
>
> To do so, I don't really need a major mode, I admit; I just need
> to put the following code in my .emacs:
>
> (fset 'send-line-other-window
>  [?\C-a ?\C- ?\C-e ?M-w right
>   ?C-x ?o ?C-y return ?\C-x ?o])
> (global-set-key [f11] 'send-line-other-window)
>
> Then I open emacs, C-x 2 to have a second buffer,
> C-x o to switch to it and M-x shell to run bash in it.
> Then, in the case of python, I run "python" in the
> bash buffer. Then I type my code in the first and with F11
> I send lines to the interpreter.
>
> But since i like to do it The Right Way, I would
> like to let the python-mode worry about this...
>
> Sorry if this is just a bunch of obvious thoughts to most of you.
>
> Regards,
> Giovanni

I find that it does work, but unlike SLIME for lisp, it just imports the 
statement.

It confused me at first, but basically the interpreter doesn't provide
any feedback to emacs.

Try opening a python source file (start python-mode if you don't have
an autoload hook) and do C-c C-z to bring up the Python
interpreter. Type in a simple assignment statement (like "a = 1 + 2"
without the quotes) into the source file. Then just C-c C-c as
usual. I never get any feedback. Just C-x o to the interpreter and
print out the variable you just defined. It should be there.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writing on file not until the end

2009-05-25 Thread Dave Angel

Alessandro wrote:



I closed and restarted the python console. Now this code (with added
"Nfile.close()" at the end) seems to work properly:

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1]
Nfile = open("N.txt", "w")
Nfile.write(textN)
Nfile.close()

thanks, Alex

  
Others had already mentioned close(), but I didn't bother, since it 
would be automatically closed when you exited the function, or finished 
the script.  I never dreamed you were running all this from the 
interpreter.  If so, why didn't you copy the prompts?


The close is best done explicitly, but it is implicit when the Nfile 
goes out of scope, or gets reassigned (like the new open).


However, the bug I described is still there.  Study the following, and 
try it:


def test():
   buf = "abcdefghijklmnopqrstg000"
   i = buf.index("d")
   e = buf.index("g", i+10)
   buf2 = buf[i+2:e-1]
   print buf2

running it produces the string:

fghijklmnopqrs

Notice the 't' is missing.  If you're deliberately doing that, fine.  
But I doubt it.




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


exec statement with/without prior compile...

2009-05-25 Thread Jaime Fernandez del Rio
These weekend I've been tearing down to pieces Michele Simionato's
decorator module, that builds signature preserving decorators. At the
heart of it all there is a dynamically generated function, which works
something similar to this...

...
src = """def function(a,b,c) :\nreturn _caller_(a,b,c)\n"""
evaldict = {'_caller_' : _caller_}
code = compile(src, '', 'single')
exec code in evaldict
new_func = evaldict[function]
...

I have found fooling around with this code, that the compile step can
be completely avoided and go for a single:

exec src in evaldict

Now, I'm sure there is a good reason for that additional step, but I
haven't been able to find what the difference between both approaches
is.

And since I'm asking, could something similar, i.e. defining a new
function and get a handle to it, be achieved with eval?

Thanks in advance,

Jaime Fernández
http://numericalrecipes.blogspot.com

-- 
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus
planes de dominación mundial.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need Help

2009-05-25 Thread Piet van Oostrum
> abosalim  (a) wrote:

>a> I modified the method,but it can't identified it.(self.res=textcorrect
>a> (self.string)
>a> NameError: global name 'textcorrect' is not defined)
>a> This is what i done:

[...]
>a> class Ex3:
[...]
>a> def textcorrect(str):
>a> s=[]
>a> for i in tokenize.whitespace(str):
>a> s.append(correct(i))
>a> result= " ".join(s)
>a> return result
>a> def new(self):
>a> self.string = self.contents.get()
>a> #pass contents of textfield to textcorrect()method above
>a> self.res=textcorrect(self.string)
>a> self.frame2 = Frame()
>a> self.frame2.pack()
>a> self.words = Label(self.frame2, text=self.res, fg="blue", font=
>a> ("Arial", 16))
>a> self.words.pack()

So now you have put textcorrect inside the class Ex3. But that means
that it is an instance method now. So you need to call 
 self.textcorrect(self.string) 
otherwise it is not known. But then you must also give it a self
parameter. The same applies to the other methods, so in textcorrect you
have to call self.correct(i)  and give correct a self parameter. etc.

Or you make them all static methods and use Ex3.textcorrect().

I don't think putting all the methods in a class is a good idea, as you
only need one instance of it and most methods are static-like anyway.
Putting it in a class is a Java-ism. A module for this is more Pythonic.

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I get a technical explanation on the following error

2009-05-25 Thread Piet van Oostrum
> Jim Garrison  (JG) wrote:

>JG> And as an interesting exercise, try
>JG> print r'test \'
>JG> print r'test \\'

>JG> Because of the way raw string parsing is defined, neither of these will
>JG> pass the parser.  In fact, a raw string cannot have a backslash as
>JG> its last character.

Cannot have an odd number of backslashes as its last characters.

>>> print r'test \\'
test \\
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [ANN] vim patch to support python3 interface

2009-05-25 Thread Roland Puntaier
vim_...@googlegroups.com schrieb am 21.05.2009 14:27:13:

> 
> On 12/05/09 18:35, Roland Puntaier wrote:
> > Hello,
> >
> > I have ported vim's python interface (if_python.c) to the python3 
C-API.
> >
> > The changed files are:
> > - Makefile (for linux)
> > - Make_mvc.mak (for windows)
> > - if_python3.c is a new file for the python3 related sources. it is 
based
> > on if_python.c.
> >
> > All of them are in the ``src`` subdirectory.
> [...]
> 
> Make_mvc.mak is not, and by far, the only makefile used by people who 
> build Vim on Windows.
> [...]

My solution regarding the build process is rather provisionary.
I'll work out a solution that encompasses all makefiles.

I'll send a follow-up.

Best regards,
Roland

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


How to pickle a 'PySwigObject' ?

2009-05-25 Thread Barak, Ron
Hi,

I have a wxPython application that is creating a subprocess that performs a 
lengthy log analysis.
Currently, the subprocess is displaying its results using its own MainLoop().
I want to display the subprocess' results using the parent process' MainLoop().

I tried to pickle the class instance that is displaying the subprocess results 
(so the subclass could be sent to the parent process for display), but I get 
the following error:

cPickle.PicklingError: Can't pickle : attribute lookup 
__builtin__.PySwigObject failed
A code that demonstrate the problem is:

$ cat pickle_test.py
#!/usr/bin/env python

#import pickle
import cPickle as pickle
import wx

class ListControl(wx.Frame):

def __init__(self, parent, id):
wx.Frame.__init__(self,parent,id)

if __name__ == "__main__":

app = wx.App(redirect=False)
output = pickle.dumps(ListControl(None, -1),2)

Any ideas what could be changed to let me pickle the class ?

Thanks,
Ron.

$ cat -n pickle_test.py
 1  #!/usr/bin/env python
 2
 3  #import pickle
 4  import cPickle as pickle
 5  import wx
 6
 7  class ListControl(wx.Frame):
 8
 9  def __init__(self, parent, id):
10  wx.Frame.__init__(self,parent,id)
11
12  if __name__ == "__main__":
13
14  app = wx.App(redirect=False)
15  output = pickle.dumps(ListControl(None, -1),2)

$ python -u pickle_test.py
Traceback (most recent call last):
  File "pickle_test.py", line 15, in 
output = pickle.dumps(ListControl(None, -1),2)
cPickle.PicklingError: Can't pickle : attribute lookup 
__builtin__.PySwigObject failed

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


Re: formating query with empty parameter

2009-05-25 Thread Pet
On May 25, 2:50 pm, Peter Otten <__pete...@web.de> wrote:
> Pet wrote:
> > > someone wrote:
> > > > Hello!
>
> > > > if one of parameter in values is empty, I'm getting
> > > > TypeError: not enough arguments for format string
>
> > > > But how to handle such situation? It is ok for DB, that some of values
> > > > are empty.
>
> > > > def __insert(self, data):
> > > >         query = """
> > > >             BEGIN;
> > > >                 INSERT INTO table
> > > >                     (a,  b,  c,  d,  e,  f,  g)
> > > >                     VALUES
> > > >                     (%s, %s, %s, %s, %s, %s, %s);
> > > >             COMMIT;
> > > >             """
> > > >         values = [
> > > >             data['a'],
> > > >             data['b'],
> > > >             data['c'],
> > > >             data['d'],
> > > >             data['e'],
> > > >             data['f'],
> > > >             data['g']
> > > >             ]
> > > >         self.db.execute(query, *values)
>
> > > You need to pass
>
> > > None
>
> > Hi,
>
> > thanks for reply.
> > Unfortunately, it doesn't work. Still getting TypeError: not enough
> > arguments for format string
>
> The code you posted doesn't match that error message. You have to invoke
> cursor.execute() as
>
> cursor.execute(query, values) # correct
>
> , not
>
> cursor.execute(query, *values) # wrong

as far as I know it is not wrong, at least for pyPgSQL it takes values
and escapes properly preventing sql injections

>
> or
>
> cursor.execute(query % values) # wrong
>
> The length of values must match the number of "%s" occurences in the sql
> query, but as Diez indicated you may pass None for every field that allows a
> NULL value in the table.
>
> Peter

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


Re: formating query with empty parameter

2009-05-25 Thread Pet
On May 25, 3:26 pm, Tim Chase  wrote:
>  if one of parameter in values is empty, I'm getting
>  TypeError: not enough arguments for format string
>  But how to handle such situation? It is ok for DB, that some of values
>  are empty.
>  def __insert(self, data):
>          query = """
>              BEGIN;
>                  INSERT INTO table
>                      (a,  b,  c,  d,  e,  f,  g)
>                      VALUES
>                      (%s, %s, %s, %s, %s, %s, %s);
>              COMMIT;
>              """
>          values = [
>              data['a'],
>              data['b'],
>              data['c'],
>              data['d'],
>              data['e'],
>              data['f'],
>              data['g']
>              ]
>          self.db.execute(query, *values)
>
> > Sorry, for previous quick post. Actually it works now, I've missed
> > some other parameter in list
>
> To stave off this problem, I often use:
>
>    values = [
>     data['a'],
>     data['b'],
>     data['c'],
>     data['d'],
>     data['e'],
>     data['f'],
>     data['g'],
>     ]
>    params = ', '.join('%s' for _ in values)
>    query = """
>      BEGIN;
>        INSERT INTO table
>          (a,b,c,d,e,f,g)
>        VALUES (%s);
>      COMMIT;
>      """ % params
>    self.db.execute(query, values)
>

Why do you pass values to execute() if you already have your query
formatted?

> If the indexes are named the same as the fieldnames, or you have
> a mapping of them, I tend to use something like
>
>    field_map = {
>      # dictionary_index: database_fieldname
>      # data['a'] -> table.f1
>      'a': 'f1',
>      'b': 'f2',
>      'c': 'f3',
>      # ...
>      }
>    name_value_pairs = (
>      (data[k], v)
>      for k,v
>      in fieldmap.iteritems())
>    values, fieldnames = zip(*name_value_pairs)
>    # may want to do fieldname escaping here:
>    fieldname_string = ', '.join(fieldnames)
>    params = ', '.join('%s' for _ in ordering)
>
>    query = """
>      BEGIN;
>        INSERT INTO table (%s) VALUES (%s);
>      COMMIT;
>      """ % (fieldname_string, params)
>    self.db.execute(query, values)
>
> -tkc

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


unicode confusing

2009-05-25 Thread someone
Hi,

reading content of webpage (encoded in utf-8) with urllib2, I can't
get parsed data into DB

Exception:

  File "/usr/lib/python2.5/site-packages/pyPgSQL/PgSQL.py", line 3111,
in execute
raise OperationalError, msg
libpq.OperationalError: ERROR:  invalid UTF-8 byte sequence detected
near byte 0xe4

I've already checked several python unicode tutorials, but I have no
idea how to solve my problem.

Appreciating your help
Pet
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I need help building a data structure for a state diagram

2009-05-25 Thread Paul McGuire
On May 24, 1:16 pm, Matthew Wilson  wrote:
> I'm working on a really simple workflow for my bug tracker.  I want
> filed bugs to start in an UNSTARTED status.  From there, they can go to
> STARTED.
>

I just wrote an article for the April issue of Python Magazine on how
to add embedded DSL code to your Python scripts using Python's imputil
module, and I used a state pattern for my example.  Two state machine
examples I used to illustrate the work were a traffic light and a
library book checkin/checkout.  The traffic light state machine is
just a simple cycle through the 3 light states.  But the library book
state machine is more complex (your bug tracking example made me think
of it), with different transitions allowed one state into multiple
different states.  Here is how the code looks for these examples:

==
# trafficLight.pystate
statemachine TrafficLight:
Red -> Green
Green -> Yellow
Yellow -> Red

Red.carsCanGo= False
Yellow.carsCanGo = True
Green.carsCanGo  = True

# ... other class definitions for state-specific behavior ...

==
# trafficLightDemo.py

# add support for .pystate files, with
# embedded state machine DSL
import stateMachine

import trafficLight

tlight = trafficLight.Red()
while 1:
print tlight, "GO" if tlight.carsCanGo else "STOP"
tlight.delay()
tlight = tlight.next_state()


==
# libraryBook.pystate

statemachine BookCheckout:
New   -(create)->   Available
Available -(reserve)->  Reserved
Available -(checkout)-> CheckedOut
Reserved  -(cancel)->   Available
Reserved  -(checkout)-> CheckedOut
CheckedOut -(checkin)->  Available
CheckedOut -(renew)->   CheckedOut


You don't need to adopt this whole DSL implementation, but the article
might give you some other ideas.

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


Re: unicode confusing

2009-05-25 Thread Paul Boddie
On 25 Mai, 17:39, someone  wrote:
> Hi,
>
> reading content of webpage (encoded in utf-8) with urllib2, I can't
> get parsed data into DB
>
> Exception:
>
>   File "/usr/lib/python2.5/site-packages/pyPgSQL/PgSQL.py", line 3111,
> in execute
>     raise OperationalError, msg
> libpq.OperationalError: ERROR:  invalid UTF-8 byte sequence detected
> near byte 0xe4
>
> I've already checked several python unicode tutorials, but I have no
> idea how to solve my problem.

With pyPgSQL, there are a few tricks that you have to take into
account:

1. With PostgreSQL, it would appear advantageous to create databases
using the "-E unicode" option.

2. When connecting, use the client_encoding and unicode_results
arguments for the connect function call:

  connection = PgSQL.connect(client_encoding="utf-8",
unicode_results=1)

3. After connecting, it appears necessary to set the client encoding
explicitly:

  connection.cursor().execute("set client_encoding to unicode")

I'd appreciate any suggestions which improve on the above, but what
this should allow you to do is to present Unicode objects to the
database and to receive such objects from queries. Whether you can
relax this and pass UTF-8-encoded strings instead of Unicode objects
is not something I can guarantee, but it's usually recommended that
you manipulate Unicode objects in your program where possible, and
here you should be able to let pyPgSQL deal with the encodings
preferred by the database.

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


Re: formating query with empty parameter

2009-05-25 Thread Tim Chase

To stave off this problem, I often use:

   values = [
data['a'],
data['b'],
data['c'],
data['d'],
data['e'],
data['f'],
data['g'],
]
   params = ', '.join('%s' for _ in values)
   query = """
 BEGIN;
   INSERT INTO table
 (a,b,c,d,e,f,g)
   VALUES (%s);
 COMMIT;
 """ % params
   self.db.execute(query, values)



Why do you pass values to execute() if you already have your query
formatted?


The "params" might be better named "placeholders".  So after the

   query = "..." % params

the query looks like your original (go ahead and print "query" to 
see), only the number of placeholders ("%s") is guaranteed to 
match the number of values you pass in during the execute() call. 
 The second iteration I gave goes one step further to ensure 
that the "(a,b,c,d,e,f,g)" portion also matches in count to the 
number of values and place-holders to be used.


Once you have a SQL query that matches what you plan to pass 
(based on your initial data-structure:  a list/tuple or a 
dictionary), then you call execute(query, values) to have the 
database then associate the parameter-placeholders ("%s") with 
the corresponding value from "values".


-tkc


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


Re: formating query with empty parameter

2009-05-25 Thread Peter Otten
Pet wrote:

> On May 25, 2:50 pm, Peter Otten <__pete...@web.de> wrote:

>> cursor.execute(query, *values) # wrong
> 
> as far as I know it is not wrong, at least for pyPgSQL it takes values
> and escapes properly preventing sql injections

If so replace "# wrong" with "# superfluous" ;)

Peter

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


Re: formating query with empty parameter

2009-05-25 Thread Pet
On 25 Mai, 18:16, Tim Chase  wrote:
> >> To stave off this problem, I often use:
>
> >>    values = [
> >>     data['a'],
> >>     data['b'],
> >>     data['c'],
> >>     data['d'],
> >>     data['e'],
> >>     data['f'],
> >>     data['g'],
> >>     ]
> >>    params = ', '.join('%s' for _ in values)
> >>    query = """
> >>      BEGIN;
> >>        INSERT INTO table
> >>          (a,b,c,d,e,f,g)
> >>        VALUES (%s);
> >>      COMMIT;
> >>      """ % params
> >>    self.db.execute(query, values)
>
> > Why do you pass values to execute() if you already have your query
> > formatted?
>
> The "params" might be better named "placeholders".  So after the

O, thanks for clarification, I've completely missed the point of
params = ', '.join
>
>     query = "..." % params
>
> the query looks like your original (go ahead and print "query" to
> see), only the number of placeholders ("%s") is guaranteed to
> match the number of values you pass in during the execute() call.
>   The second iteration I gave goes one step further to ensure
> that the "(a,b,c,d,e,f,g)" portion also matches in count to the
> number of values and place-holders to be used.
>
> Once you have a SQL query that matches what you plan to pass
> (based on your initial data-structure:  a list/tuple or a
> dictionary), then you call execute(query, values) to have the
> database then associate the parameter-placeholders ("%s") with
> the corresponding value from "values".
>
> -tkc

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


Re: from __future__ import absolute_import issue

2009-05-25 Thread LittleGrasshopper
On May 23, 6:39 pm, "Gabriel Genellina" 
wrote:
> En Sat, 23 May 2009 12:32:24 -0300, LittleGrasshopper  
>  escribió:
>
>
>
> > On May 22, 12:42 am, "Gabriel Genellina" 
> > wrote:
> >> En Wed, 20 May 2009 20:18:02 -0300, LittleGrasshopper  
> >>  escribió:
>
> >> > It appears that either absolute imports (or my brain) aren't working.
> >> > Given a module string.py which is in the same directory as a.py:
>
> >> > #File a.py
> >> > from __future__ importabsolute_import
> >> > import string
> >> > print string # Module imported is string.py in current directory, not
> >> > standard library module
>
> >>absolute_importhelps with imports inside a package (those are  
> >> "relative"  
> >> imports, relative to the package). If a module is located in a  
> >> directory  
> >> listed in sys.path, an absolute import will find it. I don't think  
> >> there  
> >> is a way to avoid shadowing a standard module (other than ensuring the  
> >> standard directories come first in sys.path).
>
> > You are right. If you have a directory in your PYTHONPATH before the
> > standard library directories that has a string module, for example,
> >absolute_importwill not help you. I was getting confused by the fact
> > that when I print sys.path from the python shell, the first entry is
> > an empty string.
> > This empty string must denote the current directory though, because:
>
> > ['', '/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/ ...]
>
> > When I have this sys.path, doing an "import string" from a module
> > where I have absolute imports enabled will still import the string
> > module in the package (which also means it is in the same directory in
> > this case.)
>
> Mmm, I don't understand this. Do you mean that the current directory is a  
> package (that is, it contains an __init__.py file? And the main script is  
> there? Then, from Python's POV, it is NOT a package; packages are  
> recognized only at import time; if you execute a script that happens to be  
> inside a package directory, Python is not aware of the "packageness" of  
> such directory.
> In that case, all imports are absolute.
> The current directory should not be a package (at least, not a package  
> accessible from sys.path), nasty things may happen.
>
> > So I think my string.py is being imported not because it
> > is in the same package, but because the home directory is searched
> > first.
>
> Yes.
>
> > The empty string doesn't make much sense though (at least in a
> > Unix system, you would imagine it would be something like './') but I
> > guess that synce Python is platform independent, the empty string is
> > taken to denote the current directory.
>
> Maybe you're surprised, but I don't think it's strange at all. Nobody  
> writes open("./foo.txt") instead of open("foo.txt"), and  
> os.path.normpath("./foo.txt") is actually "foo.txt". Having no path  
> specification means "use the current directory" (at least in all OS that I  
> know of, where "process' current directory" makes any sense)
>
> --
> Gabriel Genellina

Thank you for the insightful discussion, Gabriel. I was conducting
some testing with serious flaws. I think you have pointed the problem
exactly. What I was doing is: I had a directory with an __init__.py
file on it, and inside I had a test module, plus a string.py module.
My test was flawed because what I was doing is starting a python shell
from the directory in question, and then importing the test module in
the shell. I was expecting that when string was imported from within
the test module (in which I was using absolute_import) the standard
string module would be imported, but the string module in the same
package was being imported instead. The key is that it was being
imported not because it was in the same package, but because it was in
the home (current) directory. I think that explains everything, let me
know if you see any flaw in my reasoning.

And I agree with you about the current directory being denoted by an
empty string being expected. My mind was a little warped at the moment
I wrote that.
-- 
http://mail.python.org/mailman/listinfo/python-list


What text editor is everyone using for Python

2009-05-25 Thread LittleGrasshopper
With so many choices, I was wondering what editor is the one you
prefer when coding Python, and why. I normally use vi, and just got
into Python, so I am looking for suitable syntax files for it, and
extra utilities. I dabbled with emacs at some point, but couldn't get
through the key bindings for commands. I've never tried emacs with vi
keybindings (I forgot the name of it) but I've been tempted.

So what do you guys use, and why? Hopefully we can keep this civil.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-25 Thread J Kenneth King
LittleGrasshopper  writes:

> With so many choices, I was wondering what editor is the one you
> prefer when coding Python, and why. I normally use vi, and just got
> into Python, so I am looking for suitable syntax files for it, and
> extra utilities. I dabbled with emacs at some point, but couldn't get
> through the key bindings for commands. I've never tried emacs with vi
> keybindings (I forgot the name of it) but I've been tempted.
>
> So what do you guys use, and why? Hopefully we can keep this civil.

Google should provide you with millions of responses.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-25 Thread LittleGrasshopper
On May 25, 10:44 am, J Kenneth King  wrote:
> LittleGrasshopper  writes:
> > With so many choices, I was wondering what editor is the one you
> > prefer when coding Python, and why. I normally use vi, and just got
> > into Python, so I am looking for suitable syntax files for it, and
> > extra utilities. I dabbled with emacs at some point, but couldn't get
> > through the key bindings for commands. I've never tried emacs with vi
> > keybindings (I forgot the name of it) but I've been tempted.
>
> > So what do you guys use, and why? Hopefully we can keep this civil.
>
> Google should provide you with millions of responses.

I know Google (as a matter I read this group from Google.) I was
hoping for some new insightful responses.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-25 Thread Mr.SpOOn
2009/5/25 LittleGrasshopper :
> I know Google (as a matter I read this group from Google.) I was
> hoping for some new insightful responses.

It's just too hot to start a flame :P
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-25 Thread Esmail

LittleGrasshopper wrote:


So what do you guys use, and why? Hopefully we can keep this civil.


I use Emacs, just because I have been using this editor for
all sorts of things in the last 20+ years.

I haven't been able to get the python mode to work for Windows
(do most of my work under Linux anyway), but other than that I'm
pretty happy with it :-)

HTH,
Esmail

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


Re: [Python-Dev] PEP 384: Defining a Stable ABI

2009-05-25 Thread M.-A. Lemburg
Martin v. Löwis wrote:
> Thomas Wouters reminded me of a long-standing idea; I finally
> found the time to write it down.
> 
> Please comment!
> ...
>

Up until this PEP proposal, we had a very simple scheme for
the Python C-API: all documented functions and variables with
a "Py" prefix were part of the C-API, everything else was not
and could change between releases (in particular the private
"_Py" prefix APIs).

Changing the published APIs was considered a bad thing in the
2.x development process and generally required a good reason
to get supported.

Changing private functions or ones that were not documented
was generally never a big problem.

Now, with the PEP, I have a feeling that the Python C-API
will in effect be limited to what's in the PEP's idea of
a usable ABI and open up the non-inluded public C-APIs
to the same rate of change as the private APIs.

If that's the case, the PEP should be discussed on the C-API
list first, in order to identify a complete list of APIs that
is supposed to define the Python C-API. Ideally, all other
APIs would then need to be made private. However, I doubt that
this is possible before switching to Python 4.0.

Then again, I'm not sure whether that's what you're aiming for...

An optional cross-version ABI would certainly be a good thing.

Limiting the Python C-API would be counterproductive.

> During the compilation of applications, the preprocessor macro
> Py_LIMITED_API must be defined. Doing so will hide all definitions
> that are not part of the ABI.

So extensions wanting to use the full Python C-API as documented
in the C-API docs will still be able to do this, right ?

> Type Objects
> 
> 
> The structure of type objects is not available to applications;
> declaration of "static" type objects is not possible anymore
> (for applications using this ABI).

Hmm, that's going to create big problems for extensions that
want to expose a C-API for their types: Type checks are normally
done by pointer comparison using those static type objects.

> Functions and function-like Macros
> --
> 
> Function-like macros (in particular, field access macros) remain
> available to applications, but get replaced by function calls
> (unless their definition only refers to features of the ABI, such
> as the various _Check macros)

Including Py_INCREF()/Py_DECREF() ?

> Excluded Functions
> --
> 
> Functions declared in the following header files are not part
> of the ABI:
> - cellobject.h
> - classobject.h
> - code.h
> - frameobject.h
> - funcobject.h
> - genobject.h
> - pyarena.h
> - pydebug.h
> - symtable.h
> - token.h
> - traceback.h

I don't think that's feasable: you basically remove all introspection
functions that way.

This will need a more fine-grained approach.

> Linkage
> ---
> 
> On Windows, applications shall link with python3.dll;

You mean: extensions that were compiled with Py_LIMITED_API, right ?

> an import
> library python3.lib will be available. This DLL will redirect all of
> its API functions through /export linker options to the full
> interpreter DLL, i.e. python3y.dll.

What if you mix extensions that use the full C-API with ones
that restrict themselves to the limited version ?

Would creating a Python object in a full-API extension and
free'ing it in a limited-API extension cause problems ?

> Implementation Strategy
> ===
> 
> This PEP will be implemented in a branch, allowing users to check
> whether their modules conform to the ABI. To simplify this testing, an
> additional macro Py_LIMITED_API_WITH_TYPES will expose the existing
> type object layout, to let users postpone rewriting all types. When
> the this branch is merged into the 3.2 code base, this macro will
> be removed.

Now I'm confused again: this sounds a lot like you do want all extension
writers to only use the limited API.

[And in another post]
>> Something I haven't seen explicitly mentioned as yet (in the PEP or the
>> > python-dev list discussion) are the memory management APIs and the FILE*
>> > APIs which can cause the MSVCRT versioning issues on Windows.
>> > 
>> > Those would either need to be excluded from the stable ABI or else
>> > changed to use opaque pointers.
> 
> Good point. As a separate issue, I would actually like to deprecate,
> then remove these APIs. I had originally hoped that this would happen
> for 3.0 already, alas, nobody worked on it.
> 
> In any case, I have removed them from the ABI now.

How do you expect Python extensions to allocate memory and objects
in a platform independent way without those APIs ?

And as an aside: Which API families are you referring to ? PyMem_Malloc,
PyObject_Malloc, or PyObject_New ?

Thanks,
-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, May 25 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>

Re: What text editor is everyone using for Python

2009-05-25 Thread Ryniek90






Temat:
Re: What text editor is everyone using for Python
Od:
Esmail 
Data:
Mon, 25 May 2009 14:07:24 -0400
Do:
python-list@python.org

Do:
python-list@python.org


LittleGrasshopper wrote:


So what do you guys use, and why? Hopefully we can keep this civil.


I use Emacs, just because I have been using this editor for
all sorts of things in the last 20+ years.

I haven't been able to get the python mode to work for Windows
(do most of my work under Linux anyway), but other than that I'm
pretty happy with it :-)

HTH,
Esmail




I'm using Geany (sort of small IDE) and I'm glad using it. But for big 
projects, NetBeans or Eclipse are better. There are also Komodo EDIT or 
Stani's Python Editor. Of course if someone like GUI editors/IDE's. 
Otherwise, use Vi/Emacs. Like someone previously said, search the Google.

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


Re: What text editor is everyone using for Python

2009-05-25 Thread Marcelo de Moraes Serpa
emacs is a swiss-knife, good for small, medium, big or huge projects, or
single files

On Mon, May 25, 2009 at 1:24 PM, Ryniek90  wrote:

>
>
>> 
>>
>> Temat:
>> Re: What text editor is everyone using for Python
>> Od:
>> Esmail 
>> Data:
>> Mon, 25 May 2009 14:07:24 -0400
>> Do:
>> python-list@python.org
>>
>> Do:
>> python-list@python.org
>>
>>
>> LittleGrasshopper wrote:
>>
>>>
>>> So what do you guys use, and why? Hopefully we can keep this civil.
>>>
>>
>> I use Emacs, just because I have been using this editor for
>> all sorts of things in the last 20+ years.
>>
>> I haven't been able to get the python mode to work for Windows
>> (do most of my work under Linux anyway), but other than that I'm
>> pretty happy with it :-)
>>
>> HTH,
>> Esmail
>>
>>
>>
> I'm using Geany (sort of small IDE) and I'm glad using it. But for big
> projects, NetBeans or Eclipse are better. There are also Komodo EDIT or
> Stani's Python Editor. Of course if someone like GUI editors/IDE's.
> Otherwise, use Vi/Emacs. Like someone previously said, search the Google.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Are Python-based web frameworks reliable enough?

2009-05-25 Thread Gilles Ganault
Hello

Until now, the modest web apps I wrote were all in PHP because it's
available on just about any hosted server.

I now have a couple of ideas for applications where I would deploy my
own servers, so that I'd rather write them in Python because I find
the language more pleasant to write with.

I read that the traction for the mod_python module isn't as strong as
mod_php, so I guess I should consider the alternative, which is
writing a long-running process with some framework like TurboGears or
Django.

To make an informed choice, I'd like your feedback on this:

1. Is mod_python a bad choice today? If yes, are there other PHP-like
modules available, ie. the Python code is hosted in pages that are
loaded every time a user calls them?

2. If you think I should consider long-running processes, are the
frameworks reliable enough so that I shouldn't fear crashes every so
often?

3. What about performance as compared to equivalent PHP/MySQL apps?

Thank  you for any feedback.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are Python-based web frameworks reliable enough?

2009-05-25 Thread Krishnakant
On Mon, 2009-05-25 at 20:49 +0200, Gilles Ganault wrote:
> Hello
> 
>   Until now, the modest web apps I wrote were all in PHP because it's
> available on just about any hosted server.
> 
> I now have a couple of ideas for applications where I would deploy my
> own servers, so that I'd rather write them in Python because I find
> the language more pleasant to write with.
> Certainly, You will be much much more productive if you use python provided 
> you are not going to do it the old "cgi " way.

> I read that the traction for the mod_python module isn't as strong as
> mod_php, so I guess I should consider the alternative, which is
> writing a long-running process with some framework like TurboGears or
> Django.
> 
Why not give a try to pylons?  While turbogears is great and very
powerful, it any ways uses most of pylons and pylons is not just rock
solid and robust, it also has very very good documentation.
Not to mention the fact that heavy production applications are today
running pylons.

> To make an informed choice, I'd like your feedback on this:
> 
> 1. Is mod_python a bad choice today? If yes, are there other PHP-like
> modules available, ie. the Python code is hosted in pages that are
> loaded every time a user calls them?
> Even if it was a good choice, cgi method of programming is no more suitable 
> in today's www, because the requirements of a web developer today are much 
> beyond just oepning a connection to a database and sending some processed 
> response in plain old html.
Today's web applications do much more than that so a web framework is
almost always a better choice.

>   
> 2. If you think I should consider long-running processes, are the
> frameworks reliable enough so that I shouldn't fear crashes every so
> often?
> 
No not really, although it is always a better idea to run your
production web app behind apache.  This will keep the server up on very
heavy loads and you will not have to worry about http requests and
responses.  let that be taken care by the best tool (apache ) and let
your application handle how to manage the logic and send out views.

> 3. What about performance as compared to equivalent PHP/MySQL apps?
> 
Performance is very good if you use the right tool in the right place.
This is the biggest advantage of some web frameworks like pylons or tg
over other.
happy hacking.
Krishnakant.



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


Re: how to get rid of pyc files ?

2009-05-25 Thread pythoncurious
On May 25, 12:08 am, Dave Angel  wrote:

>
> Is Clearcase still around?  I hope it works better than it did in 1992.
>

I don't know how it worked back then, but if it's worse than today, I
don't know how they ever managed to get people to use it. I'm not a
fan and I don't have a choice about using it :)

> Somebody else has already pointed out that you can tell Python not to
> create thosefiles(during your development stages).
>

Yes, that's probably the best way to sort out the problems.
It would of course be nice if python could detect that these files
aren't usable and just regenerate them. I realise it might not be a
trivial feature to implement though.
>
> Perhaps Clearcase supports some form of "exclusion" parameter, wherein
> you say not to do version control onfileswith certain patterns, like .pyc

They're not actually version controlled, but "view private" as someone
described. This means I'll see them whenever I'm using the view they
were created in.

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


Re: how to get rid of pyc files ?

2009-05-25 Thread pythoncurious
On May 25, 12:07 am, John Machin  wrote:

> "switching" scarcely seems to be the right description. You appear to
> be running the same code from one repository simultaneously available
> to two different platforms.
>
> Try this: Instead of running your code straight from your repository,
> set up a run-time "launch pad" in an appropriate place for each
> platform. When there is a new release of your app, install it in each
> launchpad.

For releases, that's a good enough idea. But while developing and
wanting to run units tests, it's sort of a big overhead. Especially
since I tend to run the unit test quite frequently.

>
> I'm a bit lost here ... your clearcase repository uses a mysterious
> "some sort of network file system" to which you have access
> permissions which allow you to delete individualfiles??

Apparently I don't explain things well. :)
Others have explained it better in this thread or you can have a look
at http://en.wikipedia.org/wiki/Rational_ClearCase.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-25 Thread Günther Dietrich
LittleGrasshopper  wrote:

>With so many choices, I was wondering what editor is the one you
>prefer when coding Python, and why.

[...]

>So what do you guys use, and why? Hopefully we can keep this civil.

I found 'EasyEclipse for Python' soon after I began using Python. With 
PyDev, it even contains a Python debugger.
After adding a Mercurial plug-in, it has all I need for Python 
programming.
One disadvantage of EasyEclipse is, that they mangled the install/update 
system, so that it is very difficult to find the original Eclipse 
plug-ins/features.

So, in between, I replaced EasyEclipse by Eclipse, with all the plug-ins 
that came bundled with 'EasyEclipse for Python' installed by hand, but 
more actual versions.
In my opinion, it is a very convenient system for Python development, 
especially due to the debugger.
If you don't use the free PyDev, but the commercial one, you even get a 
debugger for remote debugging. Useful e.g. for debugging of blender 
scripts.



Best regards,

Günther
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-25 Thread Roy Smith
LittleGrasshopper  wrote:

> I normally use vi
> [...]
> I dabbled with emacs at some point
> [...]
> So what do you guys use, and why? Hopefully we can keep this civil.

I'm confused.  You put emacs and vi in the same paragraph and then expect 
the conversation to remain civil? :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Determining 32 bit vs 64 bit Python and numpy

2009-05-25 Thread mmanns
Hi

I am looking for a robust, cross-platform way to determine if I am on a
32 bit or a 64 bit Python and if the numpy installation is also 32 bit
or 64 bit.

I have googled a bit and found some platform specific solutions but
nothing general.

The solution should work with different versions of Python (>= 2.4).

Thanks in advance

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


Re: Determining 32 bit vs 64 bit Python and numpy

2009-05-25 Thread Martin v. Löwis
> I am looking for a robust, cross-platform way to determine if I am on a
> 32 bit or a 64 bit Python and if the numpy installation is also 32 bit
> or 64 bit.

You can find out the size of a pointer with struct.calcsize("P") * 8.
Numpy will have the same configuration if you can import it.

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


Re: Determining 32 bit vs 64 bit Python and numpy

2009-05-25 Thread mmanns
On Mon, 25 May 2009 23:54:45 +0200
"Martin v. Löwis"  wrote:

> > I am looking for a robust, cross-platform way to determine if I am
> > on a 32 bit or a 64 bit Python and if the numpy installation is
> > also 32 bit or 64 bit.
> 
> You can find out the size of a pointer with struct.calcsize("P") * 8.
> Numpy will have the same configuration if you can import it.


That works great. Thank you.

Best Regards

Martin

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


Re: Optimizing math functions

2009-05-25 Thread Esmail

Charlie wrote:


It might work under Linux, however, it was developed under Windows and, to my
knowledge, has never been tested on a Linux machine.  Basic operation only
depends on installations of matplotlib, numpy, and scipy.  Those packages are
all available on Linux.

If you try it, I'd like to know the outcome.


For sure, if I end up trying it I will post a message about my
results.


The parasol options to launch Microsoft Office apps Excel, Power Point, and
Word; or the ray tracing app POV-Ray, will very likely fail.


:-)

thanks again for the information about Parasol,

Esmail


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


Re: how to get rid of pyc files ?

2009-05-25 Thread John Machin
On May 26, 6:04 am, pythoncuri...@gmail.com wrote:
> On May 25, 12:07 am, John Machin  wrote:
>
> > "switching" scarcely seems to be the right description. You appear to
> > be running the same code from one repository simultaneously available
> > to two different platforms.
>
> > Try this: Instead of running your code straight from your repository,
> > set up a run-time "launch pad" in an appropriate place for each
> > platform. When there is a new release of your app, install it in each
> > launchpad.
>
> For releases, that's a good enough idea. But while developing and
> wanting to run units tests, it's sort of a big overhead. Especially
> since I tend to run the unit test quite frequently.

Oh .. so there's no concept of copying only the changed files??

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


Re: A fast way to read last line of gzip archive ?

2009-05-25 Thread David Bolen
"Barak, Ron"  writes:

> I couldn't really go with the shell utilities approach, as I have no
> say in my user environment, and thus cannot assume which binaries
> are install on the user's machine.

I suppose if you knew your target you could just supply the external
binaries to go with your application, but I agree that would probably
be more of a pain than its worth for the performance gain in real
world time.

> I'll try and implement your last suggestion, and see if the
> performance is acceptable to (human) users.

In terms of tuning the third option a bit, I'd play with the tracking
of the final two chunk (as mentioned in my first response), perhaps
shrinking the chunk size or only processing a smaller chunk of it for
lines (assuming a reasonable line size) to minimize the final loop.
You could also try using splitlines() on the final buffer rather than
a StringIO wrapper, although that'll have a memory hit for the
constructed list but doing a small portion of the buffer would
minimize that.

I was curious what I could actually achieve, so here are three variants
that I came up with.

First, this just fine tunes slightly tracking the chunks and then only
processes enough final data based on anticipated maximum length length
(so if the final line is longer than that you'll only get the final
MAX_LINE bytes of that line).  I also found I got better performance
using a smaller 1024 chunk size with GZipFile.read() than a MB - not
entirely sure why although it perhaps matches the internal buffer size
better:

# last-chunk-2.py

import gzip
import sys

CHUNK_SIZE = 1024
MAX_LINE = 255

in_file = gzip.open(sys.argv[1],'r')

chunk = prior_chunk = ''
while 1:
prior_chunk = chunk
# Note that CHUNK_SIZE here is in terms of decompressed data
chunk = in_file.read(CHUNK_SIZE)
if len(chunk) < CHUNK_SIZE:
break

if len(chunk) < MAX_LINE:
chunk = prior_chunk + chunk

line = chunk.splitlines(True)[-1]
print 'Last:', line


On the same test set as my last post, this reduced the last-chunk
timing from about 2.7s to about 2.3s.

Now, if you're willing to play a little looser with the gzip module,
you can gain quite a bit more.  If you directly call the internal _read()
method you can bypass some of the unnecessary processing read() does, and
go back to larger I/O chunks:

# last-gzip.py

import gzip
import sys

CHUNK_SIZE = 1024*1024
MAX_LINE = 255

in_file = gzip.open(sys.argv[1],'r')

chunk = prior_chunk = ''
while 1:
try:
# Note that CHUNK_SIZE here is raw data size, not decompressed
in_file._read(CHUNK_SIZE)
except EOFError:
if in_file.extrasize < MAX_LINE:
chunk = chunk + in_file.extrabuf
else:
chunk = in_file.extrabuf
break

chunk = in_file.extrabuf
in_file.extrabuf = ''
in_file.extrasize = 0

line = chunk[-MAX_LINE:].splitlines(True)[-1]
print 'Last:', line

Note that in this case since I was able to bump up CHUNK_SIZE, I take
a slice to limit the work splitlines() has to do and the size of the
resulting list.  Using the larger CHUNK_SIZE (and it being raw size) will
use more memory, so could be tuned down if necessary.

Of course, the risk here is that you are dependent on the _read()
method, and the internal use of the extrabuf/extrasize attributes,
which is where _read() places the decompressed data.  In looking back
I'm pretty sure this code is safe at least for Python 2.4 through 3.0,
but you'd have to accept some risk in the future.

This approach got me down to 1.48s.

Then, just for the fun of it, once you're playing a little looser with
the gzip module, it's also doing work to compute the crc of the
original data for comparison with the decompressed data.  If you don't
mind so much about that (depends on what you're using the line for)
you can just do your own raw decompression with the zlib module, as in
the following code, although I still start with a GzipFile() object to
avoid having to rewrite the header processing:

# last-decompress.py

import gzip
import sys
import zlib

CHUNK_SIZE = 1024*1024
MAX_LINE = 255

decompress = zlib.decompressobj(-zlib.MAX_WBITS)

in_file = gzip.open(sys.argv[1],'r')
in_file._read_gzip_header()

chunk = prior_chunk = ''
while 1:
buf = in_file.fileobj.read(CHUNK_SIZE)
if not buf:
break
d_buf = decompress.decompress(buf)
# We might not have been at EOF in the read() but still have no
# decompressed data if the only remaining data was not original data
if d_buf:
prior_chunk = chunk
chunk = d_buf

if len(chunk) < MAX_LINE:
chunk = prior_chunk + chunk

line = chunk[-MAX_LINE:].splitlines(True)[-1]
print 'Last:', line

This version got me down to 1.15s.

So in summar

Re: What text editor is everyone using for Python

2009-05-25 Thread John Yeung
On May 25, 4:28 pm, Roy Smith  wrote:
>
> I'm confused.  You put emacs and vi in the same paragraph
> and then expect the conversation to remain civil? :-)

I know!  He is really asking a lot!

Ultimately, I think if you are comfortable with vi, stick with vi.
There are plenty of people who are using Python-specific
configurations of vi to good effect.

Personally, I like SciTE.  Very small, very fast text editor based on
the Scintilla editor component.  It doesn't autoindent Python
perfectly out of the box, but I wrote a patch for myself (which I'm
happy to share with anyone who's interested).

I also have dabbled with Geany, which someone mentioned earlier.  I
already don't use all the features of SciTE, so I'm nowhere near
needing Geany's capabilities, but what I have seen of it I like, so I
am comfortable recommending it.  It also is very actively maintained
and supported, if that makes a difference.

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


Re: Searching for pyvm

2009-05-25 Thread A. Cavallo
http://students.ceid.upatras.gr/~sxanth/pyvm/

Does this help?
Regards,
Antonio

On Monday 25 May 2009 00:19:57 Vladimir G. Ivanovic wrote:
> Hello,
>
> I'm looking for the sources to pyvm, a python virtual machine
> implementation which can run Python 2.4 bytecode.
>
> If someone could point me in the right direction, I would appreciate it.
>
> Thanks.
>
> --- Vladimir
>
> P.S. The link on the author's pyvm page is broken, and I have been
> unable to find a copy elsewhere (Google, FTP servers, All-the-Web
> search, etc.)

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


email scanning for X-Spam-Score

2009-05-25 Thread Helmut Jarausch

Hi,

my emails received from our mailing system contain a field like

X-Spam-Score: -2.2

Given the full email message in 'msg'
I've tried
  mailmsg = email.message_from_string(msg)
  SPAM_CORE = mailmsg['X-Spam-Score']
but it doesn't work.
What am I missing?

Many thanks for a hint,
Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Network programming ?

2009-05-25 Thread thushianthan15

Hi everyone,

I am planning to develop a chatting software in Python, for my college
project. I am using Windows Vista. Is it possible to do sockets
programming in Python ? Any books or websites ?  Also, i want to
develop a gui for that program. What are the gui tool kits available
for windows? I already knew about PyGtk and PyQT, but will they work
properly in Windows platform? Any suggestions?

Thank you. Excuse my English.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Network programming ?

2009-05-25 Thread Ralf Schoenian

thushiantha...@gmail.com wrote:

Hi everyone,

I am planning to develop a chatting software in Python, for my college
project. I am using Windows Vista. Is it possible to do sockets
programming in Python ? Any books or websites ?  Also, i want to
develop a gui for that program. What are the gui tool kits available
for windows? I already knew about PyGtk and PyQT, but will they work
properly in Windows platform? Any suggestions?

Thank you. Excuse my English.


You forgot to mention the other big gui toolkits tkinter and wxPython. 
Years ago, I decided for wxPython because of its licence and that it is 
useable at windows and linux. For the socket programming I would like to 
suggest using the xmlrpclib. You can find the official documentation 
here: http://docs.python.org/library/xmlrpclib.html


Regards,
Ralf Schoenian
--
http://mail.python.org/mailman/listinfo/python-list


any lib to extract pages form pdf and then merge?

2009-05-25 Thread oyster
I want to extract some pages from vary pdf files, then write them
with/witout rotation into one new pdf file. something likes this
[py]
import gfx
doc = gfx.open("pdf", r"Theory.pdf")
pdf = gfx.PDF()
for pagenr in [1,5,7]:
page = doc.getPage(pagenr)

if pagenr==1:
page.rotate(90) #for some pages

pdf.startpage(page.width, page.height)
page.render(pdf)
pdf.endpage()
pdf.save("new pdf.pdf")
[/py]

I have tried pypdf, but it errs and exits on some of my pdfs(no, the
files have no password)

can someone suggest on such a lib for python on windows/or a pure C-dll?
(I mean pdf page->pdf, not pdf page->pic->pdf)

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


Re: What text editor is everyone using for Python

2009-05-25 Thread Steven D'Aprano
On Mon, 25 May 2009 10:35:03 -0700, LittleGrasshopper wrote:

> With so many choices, I was wondering what editor is the one you prefer
> when coding Python, and why.

I use kwrite when on a GUI. When I can't avoid editing files remotely 
over ssh, I use nano.

Why? I dislike Gnome's user-interface, and I find gedit slightly too 
underpowered and dumbed down for my taste. (Although it has a couple of 
nice features.) Of the KDE editors, kedit is too basic and I've never got 
into kate, although perhaps I should. kwrite has a nice clean, consistent 
UI that matches other KDE apps, instead of being hideously ugly like some 
apps I won't mention.

As for nano, I dislike having to memorize obscure ctrl-alt-shift key 
combos to do the simplest thing. For a handful of small edits, nano is 
perfectly adequate, doesn't tax my memory, and does the job.



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


Re: any lib to extract pages form pdf and then merge?

2009-05-25 Thread CTO
On May 26, 12:47 am, oyster  wrote:
> I want to extract some pages from vary pdf files, then write them
> with/witout rotation into one new pdf file. something likes this
> [py]
> import gfx
> doc = gfx.open("pdf", r"Theory.pdf")
> pdf = gfx.PDF()
> for pagenr in [1,5,7]:
>     page = doc.getPage(pagenr)
>
>     if pagenr==1:
>         page.rotate(90)                                         #for some 
> pages
>
>     pdf.startpage(page.width, page.height)
>     page.render(pdf)
>     pdf.endpage()
> pdf.save("new pdf.pdf")
> [/py]
>
> I have tried pypdf, but it errs and exits on some of my pdfs(no, the
> files have no password)
>
> can someone suggest on such a lib for python on windows/or a pure C-dll?
> (I mean pdf page->pdf, not pdf page->pic->pdf)
>
> thanx

I'd recommend reportlab http://www.reportlab.org/>. It is mostly
geared towards creating new PDFs, but it is pretty much best-of-breed
AFAICT.

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


Re: 4 hundred quadrillonth?

2009-05-25 Thread Scott David Daniels

Steven D'Aprano wrote:

On Mon, 25 May 2009 16:21:19 +1200, Lawrence D'Oliveiro wrote:
... (0) "Opposite" is not well-defined unless you have a dichotomy. In the 
... (1/3) Why do you jump to the conclusion that "pi=3" implies that only 
... (1/2) If you "get rid of real numbers", then obviously you must have a 
... (2/3) There is *no* point (2/3).

... (1) I thought about numbering my points as consecutive increasing
integers, but decided that was an awfully boring convention. A shiny 
banananana for the first person to recognise the sequence.


I'd call it F_3, but using a Germanic F (Farey sequence limit 3).

Do I get a banana or one with a few more ans?

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-25 Thread alex23
On May 26, 3:01 pm, Steven D'Aprano
 wrote:
> I dislike Gnome's user-interface, and I find gedit slightly too
> underpowered and dumbed down for my taste. (Although it has a couple of
> nice features.)

Gedit is also nicely extensible: 
http://www.instructables.com/id/Using-Gedit-as-a-Python-IDE/

Not that I'm trying to change your editor, preferences are just
that :) (I tend to alternate between gvim, SPE and Komodo Edit...)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-25 Thread Lawrence D'Oliveiro
In message , LittleGrasshopper wrote:

> ... I am looking for suitable syntax files for [editor of choice] ...

I don't understand why people need "syntax files" to use a text editor.

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


Re: What text editor is everyone using for Python

2009-05-25 Thread Steven D'Aprano
On Tue, 26 May 2009 18:31:56 +1200, Lawrence D'Oliveiro wrote:

> In message  b201-4b2445732...@v35g2000pro.googlegroups.com>, LittleGrasshopper
> wrote:
> 
>> ... I am looking for suitable syntax files for [editor of choice] ...
> 
> I don't understand why people need "syntax files" to use a text editor.

Do you want syntax highlighting?

If so, then your editor needs to either have an external config file 
describing the syntax you want highlighted, or a built-in internal config 
to do the same thing. For various reasons, it's better to have such 
config as an external file rather than compiled into the editor.

If you've never used syntax highlighting, then you don't know what you're 
missing. I can do without it, but it's a bother.



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