Re: binary literal

2009-07-23 Thread Hendrik van Rooyen
On Wednesday 22 July 2009 12:03:44 superpollo wrote:

> can i do something like the above, but using a *binary* number? (e.g.
> 00101101 instead of 45) ?

00101101 is not hex 45.
hex 45 is 01000101

>>> chr(int('01000101',2))
'E'
>>>

- Hendrik



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


[Unblocking VoIP in UAE] for freedom!

2009-07-23 Thread cindy
Please view this newsletter online at:
http://www.mynewsletterbuilder.com/tools/view_newsletter.php?newsletter_id=1409985533
SpeedVoIP Communication Technology Co., LTD. - 600, 6th Avenue S.W. - Calgary
- Alberta - T2P 0S5

Subscribe to this newsletter:
https://www.mynewsletterbuilder.com/tools/subscription.php?username=svsales&send_id=712043215&l=s&newsletter_id=1409985533
Unsubscribe python-list@python.org:
https://www.mynewsletterbuilder.com/tools/subscription.php?username=svsales&send_id=712043215&l=u&email=python-l...@python.org
Change your preferences:
https://www.mynewsletterbuilder.com/tools/subscription.php?username=svsales&send_id=712043215&l=p&email=python-l...@python.org
Forward to a friend:
http://www.mynewsletterbuilder.com/tools/forward.php?username=svsales&newsletter_id=1409985533&email=python-l...@python.org&send_id=712043215
Report this email as spam:
https://www.mynewsletterbuilder.com/tools/abuse_report.php?username=svsales&send_id=712043215&email=python-l...@python.org

This email was sent using MyNewsletterBuilder.com.

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


Re: Changing the private variables content

2009-07-23 Thread Ethan Furman

Ryniek90 wrote:


Got it:

exec('self.' + attr + '=\'' + val + '\'')

That worked. I think it'll do what you want now ;)

Ching-Yun "Xavier" Ho, Technical Artist

Contact Information
Mobile: (+61) 04 3335 4748
Skype ID: SpaXe85
Email: cont...@xavierho.com 
Website: http://xavierho.com/



To bad, that didn't worked in my class. Still the same error:
"
 >>> mod.print_module('socket')

Traceback (most recent call last):
 File "", line 1, in 
   mod.print_module('socket')
 File "", line 51, in print_module
   module_open = open(self._this_module, 'rb')
IOError: [Errno 2] No such file or directory: ''
 >>>
"

:-/


What is the point of the _SetVar method?

Instead of:

self._SetVar(self._this_module, os.path.join(root, f))

just do:

self._this_module = os.path.join(root, f)

(unless I'm misunderstanding what you're trying to do!)



Of course i;ve tried, but still get the same error:

"
 >>> mod.print_module('socket')

Traceback (most recent call last):
 File "", line 1, in 
   mod.print_module('socket')
 File "", line 51, in print_module
   module_open = open(self.this_module, 'rb')
IOError: [Errno 2] No such file or directory: ''
 >>>
"

It looks like private variable have specific naure, that prevent from 
traditional editing them.

Still searching for some tuts about private methods and variables.


No.  There is nothing special about variables with a leading underscore.

_number

is treated by Python *exactly* the same as

number

.  The specific problem in your code above is your _SetVar function, 
among the more general problem of not yet having a good understanding of 
classes in Python.  Keep studying, Python is an awesome language.


I was able to make this work -- hope it helps.

8<---
import os
import sys

class ModPrint(object):
u"""This will be the doc."""
def __init__(self):
self._default_search_path = sys.exec_prefix

def _search_for_module(self, chosen_module, search_path):
for root, dirs, files in os.walk(search_path):
for f in files:
if f == ("%s.py" % chosen_module):
return os.path.join(root, f)

def print_module(self, chosen_module, user_search_path=''):
search_path = user_search_path or self._default_search_path
module = self._search_for_module(chosen_module, search_path)
if module is not None:
module_open = open(module, 'rb')
module_text = module_open.read()
module_open.close()
return module_text
return 'Module not found...'
8<---
--
http://mail.python.org/mailman/listinfo/python-list


how two join and arrange two files together

2009-07-23 Thread amrita

Hi,

I have two large files:

FileA
15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C =
21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35
23 ALA H = 8.78 N =  CA =  HA = C = 179.93.

and

FileB
21 ALA  helix (helix_alpha, helix2)
23 ALA  helix (helix_alpha, helix3)
38 ALA  helix (helix_alpha, helix3)...

now what i want that i will make another file in which i will join the two
file in such a way that only matching entries will come like here 21 and
23 ALA is in both files, so the output will be something like:-

21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA  helix
(helix_alpha, helix2)
23 ALA H = 8.78 N =  CA =  HA = C = 179.93|23 ALA  helix (helix_alpha,
helix3)

and further i will make another file in which i will be able to put those
lines form this file based on the missing atom value, like for 21 ALA HA
is not defined so i will put it another file based on its HA missing value
similarly i will put 23 ALA on another file based on its missing N,CA and
HA value.

I tried to join the two file based on their matching entries by:---
>>>from collections import defaultdict
>>>
>>> if __name__ == "__main__":
...  a = open("/home/amrita/alachems/chem100.txt")
...  c = open("/home/amrita/secstr/secstr100.txt")
...
>>> def source(stream):
... return (line.strip() for line in stream)
...
...
>>> def merge(sources):
... for m in merge([source(a),source(c)]):
... print "|".join(c.ljust(10) for c in m)
...

but it is not giving any value.






Thanks,
Amrita Kumari
Research Fellow
IISER Mohali
Chandigarh
INDIA

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


Re: Help understanding the decisions *behind* python?

2009-07-23 Thread Hendrik van Rooyen
On Wednesday 22 July 2009 16:36:51 Inky 788 wrote:
> On Jul 22, 2:36 am, Hendrik van Rooyen 
>
> wrote:

> > The good reason is the immutability, which lets you use
> > a tuple as a dict key.  
>
> Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm
> just not sophisticated enough, but I've never wanted to use a list/
> tuple as a dict key. This sounds like obscure usage, and a bit
> contrived as a reason for having *both* lists and tuples.

Steven showed why you cannot have a mutable thing
as a key in a dict.

if you think it is contrived, then please consider how you would 
keep track of say the colour of a pixel on a screen at position
(x,y) - this is about the simplest "natural" tuple format and
example.

There are other equally valid examples, as has been pointed
out.  (may have been in another thread - am a bit confused
about that right now)

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


Re: Ideas for problem with chat server application!

2009-07-23 Thread Martin P. Hellwig

David Adamo Jr. wrote:


My
attempt was to create a windows service that start automatically and
runs this batch file using a Network Service account on the server
system. Although, I'm having a hard time with this (temporarily), I
would love to ask if there are any alternatives to using a windows
service. Suggestions are highly appreciated.


This is definitely the right way to go, perhaps reviewing a template 
which I use for windows services may help you:

http://blog.dcuktec.com/2009/07/python-windows-service-template.html

--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: how two join and arrange two files together

2009-07-23 Thread Chris Rebert
On Thu, Jul 23, 2009 at 12:22 AM,  wrote:
>
> Hi,
>
> I have two large files:
>
> FileA
> 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C =
> 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35
> 23 ALA H = 8.78 N =  CA =  HA = C = 179.93.
>
> and
>
> FileB
> 21 ALA  helix (helix_alpha, helix2)
> 23 ALA  helix (helix_alpha, helix3)
> 38 ALA  helix (helix_alpha, helix3)...
>
> now what i want that i will make another file in which i will join the two
> file in such a way that only matching entries will come like here 21 and
> 23 ALA is in both files, so the output will be something like:-
>
> 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA  helix
> (helix_alpha, helix2)
> 23 ALA H = 8.78 N =  CA =  HA = C = 179.93|23 ALA  helix (helix_alpha,
> helix3)
>
> and further i will make another file in which i will be able to put those
> lines form this file based on the missing atom value, like for 21 ALA HA
> is not defined so i will put it another file based on its HA missing value
> similarly i will put 23 ALA on another file based on its missing N,CA and
> HA value.
>
> I tried to join the two file based on their matching entries by:---
from collections import defaultdict

 if __name__ == "__main__":
> ...      a = open("/home/amrita/alachems/chem100.txt")
> ...      c = open("/home/amrita/secstr/secstr100.txt")
> ...
 def source(stream):
> ...     return (line.strip() for line in stream)
> ...
> ...
 def merge(sources):
> ...     for m in merge([source(a),source(c)]):
> ...         print "|".join(c.ljust(10) for c in m)
> ...
>
> but it is not giving any value.

You never actually called any of your  functions.

Slightly corrected version:

from collections import defaultdict

def source(stream):
return (line.strip() for line in stream)

def merge(sources):
for m in sources:
print "|".join(c.ljust(10) for c in m)

if __name__ == "__main__":
a = open("/home/amrita/alachems/chem100.txt")
c = open("/home/amrita/secstr/secstr100.txt")
merge([source(a), source(c)])


It's still not sophisticated enough to give the exact output you're
looking for, but it is a step in the right direction.

You really should try asking someone from your CS Dept to help you. It
would seriously take a couple hours, at most.

- Chris
-- 
Still brandishing a cluestick a vain...
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: comments? storing a function in an object

2009-07-23 Thread Carl Banks
On Jul 22, 8:38 pm, a...@pythoncraft.com (Aahz) wrote:
> In article 
> ,
> Carl Banks   wrote:
>
> >You have to be REALLY REALLY careful not to pass any user-supplied
> >data to it if this is a server running on your computer, of course.
>
> Unless, of course, your users are paying for this service.

Well, yes, but I assume that by the time you're deliberately letting
users pay to run their programs on your server, you will already have
deployed a full-blown, multi-tiered security strategy that includes
validation by the server process.  That was sort of beyond the scope
of the OP's question.


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


Re: PyQt GUI

2009-07-23 Thread Helvin
I believe I now have vtkpython.exe. However, my 'import vtk' statement
in my python code is not working. The error says something like "no
module named vtk".
Where do I find modules for vtk in pyqt? Do they exist?

They must, right? Because there are people using vtk in pyqt? Or do I
have to use OpenGL?

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


Re: How to document Python code properly for Pydoc

2009-07-23 Thread jorma kala
Thanks very much for your help

On Wed, Jul 22, 2009 at 6:43 PM, Lie Ryan  wrote:

>  jorma kala wrote:
> > Hi,
> > Do you know where I can find the rules for documenting Python code, so
> > that automatic document generation with Pydoc makes the most of the
> > comments inserted in the code?
> > I know about documenting class and method through triple quote just
> > under the class definition. But how do you comment a specific field or
> > variable, or how do you document function arguments so that they are
> > extracted like in javadoc?
> > Thanks very much
>
> pydoc is a simple tool, and doesn't do much. You write in freeform,
> although generally you'll do something like this:
>
> def myfunc(a, b):
>'''
>short description of myfunc
>
>longer description of myfunc, if necessary, and typically includes
>description of the arguments and the behaviors. Also includes the
>description of the return value.
>'''
>
>pass
>
> pydoc doesn't recognize any special markups. If you want to get more
> from the docstring, you need other documentation generator such as
> epydoc, Doxygen, or Sphinx.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: binary literal

2009-07-23 Thread superpollo

Hendrik van Rooyen wrote:

On Wednesday 22 July 2009 12:03:44 superpollo wrote:



can i do something like the above, but using a *binary* number? (e.g.
00101101 instead of 45) ?



00101101 is not hex 45.
hex 45 is 01000101



whoopsie daisie!




chr(int('01000101',2))


'E'



much obliged.

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


strange error when trying to log something

2009-07-23 Thread Ryszard Szopa
Hi,

I've recently reinstalled Python 2.6 (from DMG) on my Mac, and I am
running into very strage errors. Namely, logging seems to be badly
broken. When I open the interpreter through Django's manage.py shell
and try to use logging, I get the following error:

>>> logging.critical('ala')

Traceback (most recent call last):
  File "", line 1, in 
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/logging/__init__.py", line 1416, in critical
root.critical(*((msg,)+args), **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/logging/__init__.py", line 1074, in critical
self._log(CRITICAL, msg, args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/logging/__init__.py", line 1142, in _log
record = self.makeRecord(self.name, level, fn, lno, msg, args,
exc_info, func, extra)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/logging/__init__.py", line 1117, in makeRecord
rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/logging/__init__.py", line 272, in __init__
from multiprocessing import current_process
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/multiprocessing/__init__.py", line 64, in 
from multiprocessing.util import SUBDEBUG, SUBWARNING
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/multiprocessing/util.py", line 121, in 
_afterfork_registry = weakref.WeakValueDictionary()
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/weakref.py", line 51, in __init__
UserDict.UserDict.__init__(self, *args, **kw)
TypeError: unbound method __init__() must be called with UserDict
instance as first argument (got WeakValueDictionary instance instead)

I was able to silence the error (and be able to work normally) by
making UserDict.UserDict inherit from object.

Any ideas what is causing the error? Before I updated Python
everything was fine. Am I breaking a lot of things by making
UserDict.UserDict a new style class?

Thanks in advance for any insight.

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


Re: What is file.encoding convention?

2009-07-23 Thread Naoki INADA
> What is file.encoding convention?
> If I want to write a unicode string to a file(-like) that have
> encoding attribute, I should do
> (1) try: file.write(unicode_str),
> (2) except UnicodeEncodeError: file.write(unicode_str.encode
> (file.encoding))
> like logging?
> It seems agly.

s/agly/ugly/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strange error when trying to log something

2009-07-23 Thread Peter Otten
Ryszard Szopa wrote:

> Hi,
> 
> I've recently reinstalled Python 2.6 (from DMG) on my Mac, and I am
> running into very strage errors. Namely, logging seems to be badly
> broken. When I open the interpreter through Django's manage.py shell
> and try to use logging, I get the following error:
> 
 logging.critical('ala')
> 
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/logging/__init__.py", line 1416, in critical
> root.critical(*((msg,)+args), **kwargs)
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/logging/__init__.py", line 1074, in critical
> self._log(CRITICAL, msg, args, **kwargs)
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/logging/__init__.py", line 1142, in _log
> record = self.makeRecord(self.name, level, fn, lno, msg, args,
> exc_info, func, extra)
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/logging/__init__.py", line 1117, in makeRecord
> rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/logging/__init__.py", line 272, in __init__
> from multiprocessing import current_process
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/multiprocessing/__init__.py", line 64, in 
> from multiprocessing.util import SUBDEBUG, SUBWARNING
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/multiprocessing/util.py", line 121, in 
> _afterfork_registry = weakref.WeakValueDictionary()
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/weakref.py", line 51, in __init__
> UserDict.UserDict.__init__(self, *args, **kw)
> TypeError: unbound method __init__() must be called with UserDict
> instance as first argument (got WeakValueDictionary instance instead)
> 
> I was able to silence the error (and be able to work normally) by
> making UserDict.UserDict inherit from object.
> 
> Any ideas what is causing the error? Before I updated Python
> everything was fine. Am I breaking a lot of things by making
> UserDict.UserDict a new style class?
> 
> Thanks in advance for any insight.
> 
>   -- Ryszard Szopa

I have a hunch that you are triggering a reload() somewhere. Example:

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 weakref
>>> weakref.WeakValueDictionary()

>>> import UserDict
>>> reload(UserDict)

>>> weakref.WeakValueDictionary()
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.6/weakref.py", line 51, in __init__
UserDict.UserDict.__init__(self, *args, **kw)
TypeError: unbound method __init__() must be called with UserDict instance 
as first argument (got WeakValueDictionary instance instead)

Try restarting the interpreter after any change to source files.

Peter

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


Re: If Scheme is so good why MIT drops it?

2009-07-23 Thread Nobody
On Wed, 22 Jul 2009 15:17:52 -0700, Carl Banks wrote:

> So do all these OSes have some kind of __mega_unifying_poll system
> call that works for anything that might possibly block, that you can
> exploit from a user process?

Threads ;)

They also have the advantage that one thread can run while another is
waiting on disk I/O, which isn't something which can be done with a
select/poll interface (even if select/poll worked for files, it doesn't
help for mapped files).


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


Re: challenging problem for changing to a dedicated non-privileged user within a script.

2009-07-23 Thread Krishnakant
On Thu, 2009-07-23 at 00:17 +0200, Piet van Oostrum wrote:
> Being a sudoer is not a privilege to issue the os.setuid system call. It
> is only a permission to use the sudo command.
> 
Yes, So I would like to know if python can change the user to some other
non-privileged user during the script execution?

> >K> I tryed using subprocess but that did not help me either.  I tryed sudo
> >K> su into the Popen command but it throws me into the terminal (shell)
> >K> with postgres as the user.
> 
> You could execute the command:
> sudo -u postgres required_command
> with subprocess.
> 
Ok, but the problem is much more complex.
What if I want to do the following.
1, change the user for a particular script to the postgres user.
2. now execute the python code for connecting to the postgresql
database.
In the second point I actually want to execute python code not shell
level command so will the sudo -u in the subprocess.Popen change the
user in the script?
In short I would just like to have the script run under another user
let's say postgres as long as a certain action is going on, for example
connecting to the postgresql database.

 
> You have another problem then: your password must be supplied unless the
> NOPASSWD flag is set in the sudoers file.
> 
That is clear, the only problem is that I want the script to run as
postgres user although it was started by the user kk.


happy hacking.
Krishnakant.


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


available formats and params for Image.save()

2009-07-23 Thread News123
Hi,

Somehow I have difficulties reading the documentation for PIL (Image)

Is there an easy way to know which formats are supported and what their
names are?

Is there an easy way to know which parameters are supported by
Image.save(). How can I list them where are they documented?


Somehow I consider the documentation to be a little weak on this points.
all the documentation says (at least where I looked) is:

> class Image
>  |  Methods defined here:
> . . .
>  save(self, fp, format=None, **params)
>  |  Save image to file or stream

In order to find out how the format name for a .jpg file I did following:

import Image
img = Image.open("afile.jpg")
print img.format


Then I saw, that the result was 'JPEG' and not 'JPG' as I tried first

I'm at a complete loss at finding out what parameters the save function
accepts for saving a JPG file or a PNG file

Is there an easy way of finding this in any doc or do I have to dive
into the sources.


If there's no easy way:
Wouldn't a better documentation increase the 'user experience'?



Thanks in advance for any pointers


N



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


Re: Multiple versions of python

2009-07-23 Thread Nobody
On Tue, 21 Jul 2009 10:19:42 -0400, Dave Angel wrote:

> The other thing you may want to do in a batch file is to change the file 
> associations so that you can run the .py file directly, without typing 
> "python" or "pythonw" in front of it.
> 
> The relevant Windows commands are: assoc and ftype

However: assoc and ftype modify the registry keys under
HKLM\Software\Classes. This works fine if these are the only relevant
keys, but if you also have settings under HKCU\Software\Classes, those
will take precedence, so assoc and/or ftype won't have any effect.

Also, you typically need at least "Power User" status in order to modify
the keys under HKLM, while any user should be able to modify those under
HKCU (HKCU is a "virtual" key, corresponding to HKU\).

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


raster (PIL)

2009-07-23 Thread superpollo

hi.

i wrote a program which transforms a string of zeroes ando ones into a 
png file.


#!/usr/bin/env python
import Image
import sys
bits_in_a_byte = 8
raster_string = """\

0010010000101000
00100100101010100100
00001110001010100100
00100100101010100100
0010010000000000


0010
10101001
1010
10101000
00001000

"""
raster_lines = raster_string.splitlines()
high = len(raster_lines)
wide = len(raster_lines[0])
bytes_in_a_row = wide/bits_in_a_byte
bitmap = ""
for raster_line in raster_lines:
for byte_count in range(bytes_in_a_row):
first_bit = byte_count*bits_in_a_byte
bitmap += 
chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2))

im = Image.fromstring("1", (wide , high) , bitmap)
im.save(sys.stdout , "PNG")

any suggestions for improvement?

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


Re: Help understanding the decisions *behind* python?

2009-07-23 Thread Tim Rowe
2009/7/22 Inky 788 :

> Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm
> just not sophisticated enough, but I've never wanted to use a list/
> tuple as a dict key. This sounds like obscure usage, and a bit
> contrived as a reason for having *both* lists and tuples.

If you are used to working in a language that doesn't allow it then
you'll probably carry on using the work-arounds that you have always
used. It almost certainly only seems obscure because you're not
considering it when it would be a natural solution. In a language that
builds *very* heavily on the concept of dictionaries it's not obscure
at all!

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


Re: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler

2009-07-23 Thread Nick Craig-Wood
srepmub  wrote:
>  please send any program that doesn't work with shedskin (it still is
>  experimental after all) to me, or create an issue at
>  shedskin.googlecode.com, and I will have a look at the problem.

I divided and conquered the program as suggested and eventually I got
it to compile and run correctly :-)

I learnt that if you have lots of variables with indeterminate type
then shedskin takes a very long time indeed before blowing up!

I also learnt that shedskin doesn't support the idiom I'd been using
for creating shallow copies, namely the Board.__new__(Board) below.
shedskin compiles it ok, but the C++ won't compile complaning about
not being able to find __init__ methods

Producing these warnings

*WARNING* rush_hour_solver_cut_down.py:71: class 'Vehicle' has no method 
'__new__'
*WARNING* rush_hour_solver_cut_down.py:72: variable 'new' has no type
*WARNING* rush_hour_solver_cut_down.py:236: variable 'new_vehicle' has no type

And these compile errors

rush_hour_solver_cut_down.cpp:94: error: ‘__new__’ is not a member of 
‘__rush_hour_solver_cut_down__::Vehicle’
rush_hour_solver_cut_down.cpp:95: error: expected type-specifier before ‘;’ 
token
rush_hour_solver_cut_down.cpp: In member function ‘void* 
__rush_hour_solver_cut_down__::Board::move(int, int)’:
rush_hour_solver_cut_down.cpp:276: error: ‘void*’ is not a pointer-to-object 
type
rush_hour_solver_cut_down.cpp:276: error: ‘void*’ is not a pointer-to-object 
type
rush_hour_solver_cut_down.cpp:279: error: ‘void*’ is not a pointer-to-object 
type
rush_hour_solver_cut_down.cpp:279: error: ‘void*’ is not a pointer-to-object 
type
rush_hour_solver_cut_down.cpp:281: error: invalid conversion from ‘void*’ to 
‘__rush_hour_solver_cut_down__::Vehicle*’


def copy(self):
new = Board.__new__(Board)
new.me_x = self.me_x
new.me_y = self.me_y
new.depth = self.depth
new.parent = self
new.best_child = None
new.board = [self.board[i][:] for i in range(WIDTH)]
new.rep = self.rep[:]
new.vehicles = self.vehicles[:]
return new

I changed to using copy.copy which did work, but I couldn't name my
copy methods "copy" otherwise I got this error from the C++ compile

rush_hour_solver_cut_down.cpp: In member function 
'__rush_hour_solver_cut_down__::Vehicle* 
__rush_hour_solver_cut_down__::Vehicle::copy()':
rush_hour_solver_cut_down.cpp:94: error: no matching function for call to 
'__rush_hour_solver_cut_down__::Vehicle::copy(__rush_hour_solver_cut_down__::Vehicle*
 const)'
rush_hour_solver_cut_down.cpp:89: note: candidates are: 
__rush_hour_solver_cut_down__::Vehicle* 
__rush_hour_solver_cut_down__::Vehicle::copy()
rush_hour_solver_cut_down.cpp: In member function 
'__rush_hour_solver_cut_down__::Board* 
__rush_hour_solver_cut_down__::Board::copy()':
rush_hour_solver_cut_down.cpp:135: error: no matching function for call to 
'__rush_hour_solver_cut_down__::Board::copy(__rush_hour_solver_cut_down__::Board*
 const)'
rush_hour_solver_cut_down.cpp:129: note: candidates are: 
__rush_hour_solver_cut_down__::Board* 
__rush_hour_solver_cut_down__::Board::copy()

So I renamed them to pycopy, and they ended up looking like

def pycopy(self):
new = copy(self)
new.parent = self
new.best_child = None
new.board = [self.board[i][:] for i in range(WIDTH)]
new.rep = self.rep[:]
new.vehicles = self.vehicles[:]
return new

After all that - some timing results!

Python:   9.3 seconds
Psyco:5.8 seconds
ShedSkin: 1.0 seconds

Impressive!

I put the code http://www.craig-wood.com/nick/pub/rush_hour_solver_cut_down.py

I left in the commented out bits of code I had to change.

This is only part of the project (375 lines) - it solves Rush Hour
boards.  There is another part which I haven't attempted to compile
yet which finds the most difficult possible boards using a combination
of back tracking and a genetic algorithm.

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


sqlite3 performance problems only in python

2009-07-23 Thread Stef Mientki

hello,

until now I used only small / simple databases in Python with sqlite3.
Now I've a large and rather complex database.

The most simple query (with just a result of 100 rows),
takes about 70 seconds.
And all that time is consumed in "cursor.fetchall"

Using the same database in Delphi,
using the same query,
takes less than 5 seconds (including displaying the full table in a grid).

Are there in Python faster ways to get the query results ?
Would it be faster if I used an ODBC coupling and PyODBC to interface 
the database ?


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


Re: challenging problem for changing to a dedicated non-privileged user within a script.

2009-07-23 Thread paul

Krishnakant schrieb:

On Thu, 2009-07-23 at 00:17 +0200, Piet van Oostrum wrote:

Being a sudoer is not a privilege to issue the os.setuid system call. It
is only a permission to use the sudo command.


Yes, So I would like to know if python can change the user to some other
non-privileged user during the script execution?

If the user running python program is allowed to call setuid() then yes.




K> I tryed using subprocess but that did not help me either.  I tryed sudo
K> su into the Popen command but it throws me into the terminal (shell)
K> with postgres as the user.

You could execute the command:
sudo -u postgres required_command
with subprocess.


Ok, but the problem is much more complex.

No.


What if I want to do the following.
1, change the user for a particular script to the postgres user.

Did you try running "sudo -u postgres blabla" with subprocess?


2. now execute the python code for connecting to the postgresql
database.
In the second point I actually want to execute python code not shell
level command so will the sudo -u in the subprocess.Popen change the
user in the script?
No, as the name "subprocess" suggests you are spawning a new process 
which gets another uid through sudo. This does not affect the parent 
process.


hth
 Paul


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


Re: raster (PIL)

2009-07-23 Thread Diez B. Roggisch
superpollo wrote:

> hi.
> 
> i wrote a program which transforms a string of zeroes ando ones into a
> png file.
> 
> #!/usr/bin/env python
> import Image
> import sys
> bits_in_a_byte = 8
> raster_string = """\
> 
> 0010010000101000
> 00100100101010100100
> 00001110001010100100
> 00100100101010100100
> 0010010000000000
> 
> 
> 0010
> 10101001
> 1010
> 10101000
> 00001000
> 
> """
> raster_lines = raster_string.splitlines()
> high = len(raster_lines)
> wide = len(raster_lines[0])
> bytes_in_a_row = wide/bits_in_a_byte

This will give you the wrong result if not divideable by bits_in_a_byte. 

> bitmap = ""
> for raster_line in raster_lines:
>  for byte_count in range(bytes_in_a_row):
>  first_bit = byte_count*bits_in_a_byte
>  bitmap +=
> chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2))
> im = Image.fromstring("1", (wide , high) , bitmap)
> im.save(sys.stdout , "PNG")
> 
> any suggestions for improvement?

Instead of 

res = ""
for ...:
res += ...

use 

res = []
for ...:
res.append(...)

"".join(res)

There are some optimizations for the +=-op on strings, but I'm not sure how
far they go, and the other form is safer.

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


Re: Copy/paste through LAN

2009-07-23 Thread Piet van Oostrum
> Jun  (J) wrote:

>J> Hello,
>J> I've a client/server application which uses Pyro to communicate each
>J> other.
>J> Now i try to implement copy/paste through LAN between server
>J> controlled
>J> filesystem to my local windows machine (I could list files in client's
>J> window).
>J> How can i pass the url information through Pyro ?

What do you mean? Do you want to copy a file or copy file names? Copying
a file between two machines can best be done by a specialized protocol,
like HTTP or scp. Pasting a file doesn't make sense IMHO.

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


Re: challenging problem for changing to a dedicated non-privileged user within a script.

2009-07-23 Thread Piet van Oostrum
> Krishnakant  (K) wrote:

>K> On Thu, 2009-07-23 at 00:17 +0200, Piet van Oostrum wrote:
>>> Being a sudoer is not a privilege to issue the os.setuid system call. It
>>> is only a permission to use the sudo command.
>>> 
>K> Yes, So I would like to know if python can change the user to some other
>K> non-privileged user during the script execution?

As I said you can't (unless you are root). It would be a security leak if
an arbitrary user could suddenly run as another user. Sudo is the escape
mechanism but it runs commands, and is not for changing the uid in the
middle of a process.

>>> >K> I tryed using subprocess but that did not help me either.  I tryed sudo
>>> >K> su into the Popen command but it throws me into the terminal (shell)
>>> >K> with postgres as the user.
>>> 
>>> You could execute the command:
>>> sudo -u postgres required_command
>>> with subprocess.
>>> 
>K> Ok, but the problem is much more complex.
>K> What if I want to do the following.
>K> 1, change the user for a particular script to the postgres user.
>K> 2. now execute the python code for connecting to the postgresql
>K> database.
>K> In the second point I actually want to execute python code not shell
>K> level command so will the sudo -u in the subprocess.Popen change the
>K> user in the script?

You can run another python script as the other user (o even the same
python script). You said you tried subprocess. If that is acceptable
then running another python script should also be acceptable, becaus eit
is basically the same.

>K> In short I would just like to have the script run under another user
>K> let's say postgres as long as a certain action is going on, for example
>K> connecting to the postgresql database.

Why would you have to be another user for connecting to a postgres
database? The DBMS takes care of the permissions at the DB level.

Otherwise you would have to do the DB access in another script. The
script could even communicate withe the original script, e.g by pipes or
some protocol like XMLRPC.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: challenging problem for changing to a dedicated non-privileged user within a script.

2009-07-23 Thread Krishnakant
On Thu, 2009-07-23 at 13:50 +0200, paul wrote:

> If the user running python program is allowed to call setuid() then yes.
> 
NO, i don't think i can do that.  I am getting opperation not permitted.

Any ways I think probably subprocess will have to sort it out.

> Did you try running "sudo -u postgres blabla" with subprocess?
> 
Yes, but still not got the intended result which is now obvious.
> > 2. now execute the python code for connecting to the postgresql
> > database.
> > In the second point I actually want to execute python code not shell
> > level command so will the sudo -u in the subprocess.Popen change the
> > user in the script?
> No, as the name "subprocess" suggests you are spawning a new process 
> which gets another uid through sudo. This does not affect the parent 
> process.
> 
Ok then here is the work-around which I am thinking to try, Plese tell
me if it is correct.
I will let that subprocess start python inthe background and execute the
connecting code to postgresql including importing the pygresql library.
Then I will create the connection and cursor objcts in that subprocess.
But my concern is, will the connection object in the child process
(subprocess) be available to the parrent process?


happy hacking.
Krishnakant.


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


Re: how two join and arrange two files together

2009-07-23 Thread Jyoti Sharma

On Thu, 23 Jul 2009 12:52:15 +0530,  wrote:



Hi,

I have two large files:

FileA
15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C =
21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35
23 ALA H = 8.78 N =  CA =  HA = C = 179.93.

and

FileB
21 ALA  helix (helix_alpha, helix2)
23 ALA  helix (helix_alpha, helix3)
38 ALA  helix (helix_alpha, helix3)...

now what i want that i will make another file in which i will join the  
two

file in such a way that only matching entries will come like here 21 and
23 ALA is in both files, so the output will be something like:-

21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA  helix
(helix_alpha, helix2)
23 ALA H = 8.78 N =  CA =  HA = C = 179.93|23 ALA  helix (helix_alpha,
helix3)

and further i will make another file in which i will be able to put those
lines form this file based on the missing atom value, like for 21 ALA HA
is not defined so i will put it another file based on its HA missing  
value

similarly i will put 23 ALA on another file based on its missing N,CA and
HA value.

I tried to join the two file based on their matching entries by:---


(snip)

I believe there are packages available for doing such things mostly  
written in perl. But if the aim is not to develop suitable applications  
but only to obtain the desired formatted file then doing this with the  
help of something like Excel would be easiest. This is my opinion from the  
experience I have from my bioinfo programming.


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


Looking for os.listdir() generator

2009-07-23 Thread Christian Heimes
Hello,

I'm looking for a generator version of os.listdir() for Python 2.5 and
newer. I know somebody has worked on it because I've seen a generator
version in a posting on some list or blog a while ago. I can't find it
anymore. It seems my Google fu is lacking today. All I can find is a
very old version of xlistdir. A Cython based solution is appreciated but
please no ctypes version.

Christian

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


Re: sqlite3 performance problems only in python

2009-07-23 Thread Tim Chase

until now I used only small / simple databases in Python with sqlite3.
Now I've a large and rather complex database.

The most simple query (with just a result of 100 rows),
takes about 70 seconds.
And all that time is consumed in "cursor.fetchall"

Using the same database in Delphi,
using the same query,
takes less than 5 seconds (including displaying the full table in a grid).


While it may seem obvious, are you doing anything time-consuming 
with those results?  Or have you tested just doing the fetchall() 
without doing any further processing?  I'm curious on the timing of


  sql = "..."
  start = time()
  cursor.execute(sql)
  rows = cursor.fetchall()
  end = time()
  print end-start

with no other processing.  I regularly write sql that's fairly 
complex and brings back somewhat large datasets (sometimes in 
sqlite), and have never experienced problems with "simple 
quer[ies] (with just a result of 100 rows" taking such 
extrordinary times


The answer from the above code will help determine whether it's 
the sqlite portion that's crazy (and might need some well-placed 
index statements; though if your Delphi code is fine, I suspect 
not), or if it's your application code that goes off into left 
field with the resulting data.


-tkc




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


Re: raster (PIL)

2009-07-23 Thread Peter Otten
superpollo wrote:

> i wrote a program which transforms a string of zeroes ando ones into a
> png file.
> 
> #!/usr/bin/env python
> import Image
> import sys
> bits_in_a_byte = 8
> raster_string = """\
> 
> 0010010000101000
> 00100100101010100100
> 00001110001010100100
> 00100100101010100100
> 0010010000000000
> 
> 
> 0010
> 10101001
> 1010
> 10101000
> 00001000
> 
> """
> raster_lines = raster_string.splitlines()
> high = len(raster_lines)
> wide = len(raster_lines[0])
> bytes_in_a_row = wide/bits_in_a_byte
> bitmap = ""
> for raster_line in raster_lines:
>  for byte_count in range(bytes_in_a_row):
>  first_bit = byte_count*bits_in_a_byte
>  bitmap +=
> chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2))
> im = Image.fromstring("1", (wide , high) , bitmap)
> im.save(sys.stdout , "PNG")
> 
> any suggestions for improvement?

You can simplify the inner loop:

for first_bit in range(0, wide, bits_in_a_byte):
bitmap += ...

and get rid of a few helper variables.

Here's a different approach:

#!/usr/bin/env python
import Image
import string
import sys

mapping = string.maketrans("01", "\x00\xff")

raster_string = ...

width = raster_string.index("\n")
height = raster_string.count("\n")

raster_string = raster_string.translate(mapping, "\n")

im = Image.fromstring("L", (width, height), raster_string)
im.convert("1").save(sys.stdout, "PNG")

The idea is to move the bit-twiddling from python to code written in C, 
pointless for such a tiny picture but crucial for the performance when you 
want to manipulate larger images.

Peter

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


Re: raster (PIL)

2009-07-23 Thread superpollo

Diez B. Roggisch wrote:

superpollo wrote:

...

high = len(raster_lines)
wide = len(raster_lines[0])
bytes_in_a_row = wide/bits_in_a_byte



This will give you the wrong result if not divideable by bits_in_a_byte. 



then maybe:

#!/usr/bin/env python
import Image
import sys
bits_in_a_byte = 8
raster_string = """\
00
0010010000101000
00100100101010100100
00001110001010100100
00100100101010100100
0010010000000000


0010
10101001
1010
10101000
00001000

"""
raster_lines = raster_string.splitlines()
high = len(raster_lines)
wide = len(raster_lines[0])
bytes_in_a_row = wide/bits_in_a_byte
wide_for_real = bytes_in_a_row*bits_in_a_byte
if wide_for_real:
bitmap = ""
for raster_line in raster_lines:
for byte_count in range(bytes_in_a_row):
first_bit = byte_count*bits_in_a_byte
bitmap += 
chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] ,

2))
im = Image.fromstring("1", (wide_for_real , high) , bitmap)
im.save(sys.stdout , "PNG")

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


Re: python function for retrieving key and encryption

2009-07-23 Thread Piet van Oostrum
> jayshree  (j) wrote:

>j> On Jul 21, 8:59 pm, Piet van Oostrum  wrote:

>>> The recipient_public_key.pem file is the public key of the recipient
>>> which means the person that is going to receive the encrypted message.
>>> You should get it from the recipient him/herself or from some key store
>>> where s/he has deposited it.
[...]

>j> error coming like - IOError: [Errno 2] No such file or directory:
>j> 'recipient_public_key.pem'

>j> Is this not the inbuilt file.
>j> How should i create such type of file.

You don't create it. See above. If you understand what is is you know
why it can't be builtin. If you don't understand it is better if you
first learn about OpenSSL, otherwise you run the risk to make serious
errors. 

If you are just experimenting to create a message for yourself then you
have to create the public key because you are then the recipient
yourself. That has been answered on
http://stackoverflow.com/questions/1169798/m2crypto-package 
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: raster (PIL)

2009-07-23 Thread superpollo

Peter Otten wrote:

superpollo wrote:



i wrote a program which transforms a string of zeroes ando ones into a
png file.

...

any suggestions for improvement?

...

Here's a different approach:

...
The idea is to move the bit-twiddling from python to code written in C, 
pointless for such a tiny picture but crucial for the performance when you 
want to manipulate larger images.


very very interesting... you know i come from a lower level language 
approach (C/Pascal) so i find it difficult (but full of fascination) to 
adapt to such a different and much simpler way of thinking.


anyways, thanks a lot.

bye

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


Re: sqlite3 performance problems only in python

2009-07-23 Thread Stef Mientki

Tim Chase wrote:

until now I used only small / simple databases in Python with sqlite3.
Now I've a large and rather complex database.

The most simple query (with just a result of 100 rows),
takes about 70 seconds.
And all that time is consumed in "cursor.fetchall"

Using the same database in Delphi,
using the same query,
takes less than 5 seconds (including displaying the full table in a 
grid).


While it may seem obvious, are you doing anything time-consuming with 
those results?  Or have you tested just doing the fetchall() without 
doing any further processing?  I'm curious on the timing of


  sql = "..."
  start = time()
  cursor.execute(sql)
  rows = cursor.fetchall()
  end = time()
  print end-start

No this is exactly what I did,
I timed the execute and fetchall seperatly:
execute: 125 msec
fetchall: 71000 msec  (returning 100 rows and 25 columns)
pysqlite:  version 2.3.2

btw, I don't know if it's of any importance, the SQL-statement I perform is
select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.*
 from OPNAMEN
   inner join POID_VLID  on OPNAMEN.POID= 
POID_VLID.POID
   inner join VRAAGLST   on VRAAGLST.VLID   = 
POID_VLID.VLID
   inner join VLID_SSID  on VRAAGLST.VLID   = 
VLID_SSID.VLID
   inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = 
VLID_SSID.SSID
   inner join POID_SSID_SCID on ( OPNAMEN.POID= 
POID_SSID_SCID.POID ) and
( SUBSCHAAL_GEGEVENS.SSID = 
POID_SSID_SCID.SSID )
   inner join SCORES on SCORES.SCID = 
POID_SSID_SCID.SCID

   inner join PID_POID   on OPNAMEN.POID= PID_POID.POID
   inner join PATIENTon PATIENT.PID = PID_POID.PID
 where substr ( lower( NAME) , 1, 6)  = 'cis20r'
   and lower ( NAME_ ) = 'fatigue'
   and TEST_COUNT in (3,4)
   and DATETIME > 39814.0
   and SCORE < 30

cheers,
Stef



with no other processing.  I regularly write sql that's fairly complex 
and brings back somewhat large datasets (sometimes in sqlite), and 
have never experienced problems with "simple quer[ies] (with just a 
result of 100 rows" taking such extrordinary times


The answer from the above code will help determine whether it's the 
sqlite portion that's crazy (and might need some well-placed index 
statements; though if your Delphi code is fine, I suspect not), or if 
it's your application code that goes off into left field with the 
resulting data.


-tkc






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


Re: Pyserial and pyQt

2009-07-23 Thread Robert Franke
On Tue, Jul 21, 2009 at 9:37 PM, Seth  wrote:

>
> I have used pyserial in the past but this is my first experience with
> pyQt.  I am using the Python xy package for windows current but might
> move to linux.  I have a small device that is outputting a basic text
> string.  I want to be able to read this string(from the comm port) and
> update a text box and eventually a graph in pyQt.  I can't find any
> documentation or tutorials on how to do this.  If anyone can point me
> in the right direction or give me some tips I would be grateful.
>


As I need to do something similar I looked around and found the following
recipe at activestate:

http://code.activestate.com/recipes/82965/

In the comments there is an example for PyQt.


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


Re: raster (PIL)

2009-07-23 Thread superpollo

Peter Otten wrote:
...

Here's a different approach:

...

raster_string = ...

width = raster_string.index("\n")
height = raster_string.count("\n")


your approach has a funny side-effect: try to remove just one zero from 
the first line of the raster ;-)


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


Re: raster (PIL)

2009-07-23 Thread superpollo

Peter Otten wrote:
...

im.convert("1").save(sys.stdout, "PNG")

...

a q about pil:

im.convert("1")

is different from:

im2 = im.convert("1")

right?

in the former im is changed (the method applies to im) but in the latter 
im is unchanged (first im is copied unto im2 and then the method is 
applied to im2)... am i right?


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


Re: Detect target name in descriptor __set__ method

2009-07-23 Thread DG
On Jul 22, 6:05 pm, "Gabriel Genellina" 
wrote:
> En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James  
>  escribió:
>
> > On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina  
> >  wrote:
>
> >> class X(object):
> >>    foo = descriptor()
>
> >> x = X()
> >> x.foo = "value"
>
> > Isn't this going to create a brand new instance attribute x.foo that has  
> > nothing to do with the descriptor anyway?
>
> No, it's up to the descriptor __set__ method what happens in this case.  
> Think of the standard 'property' descriptor, the fset function can do  
> whatever it wants.
> Also, a data descriptor takes precedence over any instance attribute of  
> the same name that might exist.
>
> --
> Gabriel Genellin

You might've already thought of this (and it is annoying), but you
could pass the name through the descriptor's init method.  I believe
this is the only way besides assigning a metaclass that will look for
that type of descriptor upon class creation and set the descriptor's
name at that time.

class A(object):
def __init__(self, attr_name):
self._name = attr_name
def __set__(self, instance, value):
self.instance.__dict__[self._name] = value
# or something like that...

class B(object):
foo = A('foo')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regex: multiple matching for one string

2009-07-23 Thread Mark Lawrence

scriptlear...@gmail.com wrote:

For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I
will like to take out the values (valuea, valueb, and valuec).  How do
I do that in Python?  The group method will only return the matched
part.  Thanks.

p = re.compile('#a=*;b=*;c=*;')
m = p.match(line)
if m:
 print m.group(),


IMHO a regex for this is overkill, a combination of string methods such 
as split and find should suffice.


Regards.

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


Re: raster (PIL)

2009-07-23 Thread Diez B. Roggisch
superpollo wrote:

> Diez B. Roggisch wrote:
>> superpollo wrote:
> ...
>>>high = len(raster_lines)
>>>wide = len(raster_lines[0])
>>>bytes_in_a_row = wide/bits_in_a_byte
>> 
>> 
>> This will give you the wrong result if not divideable by bits_in_a_byte.
>> 
> 
> then maybe:
> 
> #!/usr/bin/env python
> import Image
> import sys
> bits_in_a_byte = 8
> raster_string = """\
> 00
> 0010010000101000
> 00100100101010100100
> 00001110001010100100
> 00100100101010100100
> 0010010000000000
> 
> 
> 0010
> 10101001
> 1010
> 10101000
> 00001000
> 
> """
> raster_lines = raster_string.splitlines()
> high = len(raster_lines)
> wide = len(raster_lines[0])
> bytes_in_a_row = wide/bits_in_a_byte
> wide_for_real = bytes_in_a_row*bits_in_a_byte

No. Because that would skip up to 7 bits.

Instead, do

bytes_in_a_row = wide/bits_in_a_byte
if wide % bits_in_a_byte:
   bytes_in_a_row += 1

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


Re: Predefined Variables

2009-07-23 Thread Diez B. Roggisch
Fred Atkinson wrote:

> Is there a pre-defined variable that returns the GET line
> (http://www.php.net/index.php?everythingafterthequestionmark) as a
> single variable (rather than individual variables)?

Variables don't return things. Functions do. And additionally the answer
depends on what and with what you actually do.

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


Re: challenging problem for changing to a dedicated non-privileged user within a script.

2009-07-23 Thread paul

Krishnakant schrieb:

On Thu, 2009-07-23 at 13:50 +0200, paul wrote:


If the user running python program is allowed to call setuid() then yes.


NO, i don't think i can do that.  I am getting opperation not permitted.

Any ways I think probably subprocess will have to sort it out.


Did you try running "sudo -u postgres blabla" with subprocess?


Yes, but still not got the intended result which is now obvious.

Why is that obvious? Works for me:

 test.py -
#!/usr/bin/python

from subprocess import Popen, PIPE

cmd = Popen('sudo -u vboxadd /home/pkoelle/Documents/whoami.sh', 
shell=True, stdout=PIPE, stderr=PIPE)

print "OUT: "+cmd.stdout.read()
print "ERR: "+cmd.stderr.read()

 whoami.sh -
#!/bin/bash
echo $UID
logger "whoami script called for $UID"

Of course, you need to adapt path and user values to your situation. The 
user you use in your 'sudo -u ...' call needs execute permissions 
for whoami.sh. The relevant entry in /etc/sudoers:


pkoelle ALL=NOPASSWD: /home/pkoelle/Documents/whoami.sh

hth
 Paul

PS: This has absolutely nothing to do with "connecting to postgresql". A 
"postgres user" is not a "system user" (Piet already asked the right 
questions here ;)




2. now execute the python code for connecting to the postgresql
database.
In the second point I actually want to execute python code not shell
level command so will the sudo -u in the subprocess.Popen change the
user in the script?
No, as the name "subprocess" suggests you are spawning a new process 
which gets another uid through sudo. This does not affect the parent 
process.



Ok then here is the work-around which I am thinking to try, Plese tell
me if it is correct.
I will let that subprocess start python inthe background and execute the
connecting code to postgresql including importing the pygresql library.
Then I will create the connection and cursor objcts in that subprocess.
But my concern is, will the connection object in the child process
(subprocess) be available to the parrent process?


happy hacking.
Krishnakant.




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


Re: raster (PIL)

2009-07-23 Thread Peter Otten
superpollo wrote:

> Peter Otten wrote:
> ...
>> im.convert("1").save(sys.stdout, "PNG")
> ...
> 
> a q about pil:
> 
> im.convert("1")
> 
> is different from:
> 
> im2 = im.convert("1")
> 
> right?
> 
> in the former im is changed (the method applies to im) but in the latter
> im is unchanged (first im is copied unto im2 and then the method is
> applied to im2)... am i right?

No. A method has no clue whether its result is used or discarded. Therefore

im.convert("1")

creates a new image in the specified mode, too, which is discarded 
immediately. If you don't need the result you probably shouldn't call the 
method at all.

Peter


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


Re: raster (PIL)

2009-07-23 Thread superpollo

Peter Otten wrote:

superpollo wrote:



Peter Otten wrote:
...


im.convert("1").save(sys.stdout, "PNG")


...

a q about pil:

im.convert("1")

is different from:

im2 = im.convert("1")

right?

in the former im is changed (the method applies to im) but in the latter
im is unchanged (first im is copied unto im2 and then the method is
applied to im2)... am i right?



No. A method has no clue whether its result is used or discarded. Therefore

im.convert("1")

creates a new image in the specified mode, too, which is discarded 
immediately.


but in:

im.convert("1").save(sys.stdout, "PNG")

the new (anonymous) image created by .convert is not discarded 
*immediately*, i mean *before* the .save method is called on it, right?


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


Re: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!)

2009-07-23 Thread Paul Boddie
On 23 Jul, 05:55, a...@pythoncraft.com (Aahz) wrote:
> In article 
> <1c994086-8c58-488f-b3b3-6161c4b2b...@k30g2000yqf.googlegroups.com>,
> Paul Boddie   wrote:
>
> >http://www.boddie.org.uk/python/XSLTools.html
>
> Thanks!  I'll take a look after OSCON.

The JavaScript parts of the framework are a bit complicated, I'll
admit: you have to write some nasty-looking function calls with
awkward arguments to send AJAX-style requests to the server. I've been
meaning to employ a more declarative "signals and slots" approach so
that you don't have to write in the page templates where the in-page
updates should be expected: it's really the server code that
determines this kind of thing, and so the server code should be able
to say where it wants the page to be updated.

Again, I'll try and put up some examples in the relatively near future
that will make it easier for you to see if it's your kind of thing.

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


Re: raster (PIL)

2009-07-23 Thread Peter Otten
superpollo wrote:

> Peter Otten wrote:
>> superpollo wrote:
>> 
>> 
>>>Peter Otten wrote:
>>>...
>>>
im.convert("1").save(sys.stdout, "PNG")
>>>
>>>...
>>>
>>>a q about pil:
>>>
>>>im.convert("1")
>>>
>>>is different from:
>>>
>>>im2 = im.convert("1")
>>>
>>>right?
>>>
>>>in the former im is changed (the method applies to im) but in the latter
>>>im is unchanged (first im is copied unto im2 and then the method is
>>>applied to im2)... am i right?
>> 
>> 
>> No. A method has no clue whether its result is used or discarded.
>> Therefore
>> 
>> im.convert("1")
>> 
>> creates a new image in the specified mode, too, which is discarded
>> immediately.
> 
> but in:
> 
> im.convert("1").save(sys.stdout, "PNG")
> 
> the new (anonymous) image created by .convert is not discarded
> *immediately*, i mean *before* the .save method is called on it, right?

Of course. Think of it as a shortcut for

tmp = im.convert(...)
tmp.save(...)
del tmp

Peter


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


Re: how two join and arrange two files together

2009-07-23 Thread amrita
I tried to print those lines having C value missing by:

import re
expr = re.compile("C = None")
f = open("/home/amrita/helix.dat")
for line in f:
if expr.search(line):
   print line

but it is not giving any value.

>
> Hi,
>
> I have two large files:
>
> FileA
> 15 ALA H = 8.05   N = 119.31 CA = 52.18 HA = 4.52 C =
> 21 ALA H = 7.66   N = 123.58 CA = 54.33 HA = C = 179.35
> 23 ALA H = 8.78   N =  CA =  HA = C = 179.93.
>
> and
>
> FileB
> 21 ALA  helix (helix_alpha, helix2)
> 23 ALA  helix (helix_alpha, helix3)
> 38 ALA  helix (helix_alpha, helix3)...
>
> now what i want that i will make another file in which i will join the two
> file in such a way that only matching entries will come like here 21 and
> 23 ALA is in both files, so the output will be something like:-
>
> 21 ALA H = 7.66   N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA  helix
> (helix_alpha, helix2)
> 23 ALA H = 8.78   N =  CA =  HA = C = 179.93|23 ALA  helix (helix_alpha,
> helix3)
>
> and further i will make another file in which i will be able to put those
> lines form this file based on the missing atom value, like for 21 ALA HA
> is not defined so i will put it another file based on its HA missing value
> similarly i will put 23 ALA on another file based on its missing N,CA and
> HA value.
>
> I tried to join the two file based on their matching entries by:---
from collections import defaultdict

 if __name__ == "__main__":
> ...  a = open("/home/amrita/alachems/chem100.txt")
> ...  c = open("/home/amrita/secstr/secstr100.txt")
> ...
 def source(stream):
> ... return (line.strip() for line in stream)
> ...
> ...
 def merge(sources):
> ... for m in merge([source(a),source(c)]):
> ... print "|".join(c.ljust(10) for c in m)
> ...
>
> but it is not giving any value.
>
>
>
>
>
>
> Thanks,
> Amrita Kumari
> Research Fellow
> IISER Mohali
> Chandigarh
> INDIA
>


Amrita Kumari
Research Fellow
IISER Mohali
Chandigarh
INDIA

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


Re: Detect target name in descriptor __set__ method

2009-07-23 Thread DG
On Jul 23, 7:19 am, DG  wrote:
> On Jul 22, 6:05 pm, "Gabriel Genellina" 
> wrote:
>
>
>
> > En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James  
> >  escribió:
>
> > > On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina  
> > >  wrote:
>
> > >> class X(object):
> > >>    foo = descriptor()
>
> > >> x = X()
> > >> x.foo = "value"
>
> > > Isn't this going to create a brand new instance attribute x.foo that has  
> > > nothing to do with the descriptor anyway?
>
> > No, it's up to the descriptor __set__ method what happens in this case.  
> > Think of the standard 'property' descriptor, the fset function can do  
> > whatever it wants.
> > Also, a data descriptor takes precedence over any instance attribute of  
> > the same name that might exist.
>
> > --
> > Gabriel Genellin
>
> You might've already thought of this (and it is annoying), but you
> could pass the name through the descriptor's init method.  I believe
> this is the only way besides assigning a metaclass that will look for
> that type of descriptor upon class creation and set the descriptor's
> name at that time.
>
> class A(object):
>     def __init__(self, attr_name):
>         self._name = attr_name
>     def __set__(self, instance, value):
>         self.instance.__dict__[self._name] = value
>         # or something like that...
>
> class B(object):
>     foo = A('foo')

Well of course I think of more alternatives after I post.
1) still annoying... pass the class through a 'registering' function
that will examine all of it's attrs and find the ones that are of your
descriptor type, and assign the attr_name upon that descriptor.name
attribute.

2) I just thought of, and it's kind of a hack is to do this
examination within the '__set__' method.
e.g.

class A(object):
def __get__(self, instance, owner):
if instance is None:
return self
else:
# do something different here for instances' access
return self

def __set__(self, instance, value):
cls = instance.__class__
name = None
for attr in dir(cls):
if getattr(cls, attr) is self:
name = attr
print name
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Detect target name in descriptor __set__ method

2009-07-23 Thread Rhodri James
On Thu, 23 Jul 2009 01:05:55 +0100, Gabriel Genellina  
 wrote:


En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James  
 escribió:
On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina  
 wrote:




class X(object):
   foo = descriptor()

x = X()
x.foo = "value"


Isn't this going to create a brand new instance attribute x.foo that  
has nothing to do with the descriptor anyway?


No, it's up to the descriptor __set__ method what happens in this case.  
Think of the standard 'property' descriptor, the fset function can do  
whatever it wants.


If it gets called, that is.

Also, a data descriptor takes precedence over any instance attribute of  
the same name that might exist.


This was the bit I wasn't clear on.  Thanks!

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


import vs imp and friends.

2009-07-23 Thread Emanuele D'Arrigo
Greetings,

I was looking in the archive of this newsgroup and I found this
snippet:

import imp
sourcecode = 'def foo(x): return 11*x'
mod = imp.new_module('foo')
exec sourcecode in mod.__dict__
mod.foo(16)

Together with similar and sometimes more complete snippets available
they show how a module can be created out of string, plain text files
and compiled files. Neat!

Now the question. Apart from checking sys.module first and eventually
adding the new module to it if it isn't there already, and apart from
setting __file__, is there anything else that import does and this
snippet doesn't?

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


Re: ANN: psyco V2

2009-07-23 Thread Neuruss
It seems psyco.org is still in the transfer process...
Is there any charitable soul with a link to a Windows binary? :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import vs imp and friends.

2009-07-23 Thread Christian Heimes
Emanuele D'Arrigo wrote:
> Now the question. Apart from checking sys.module first and eventually
> adding the new module to it if it isn't there already, and apart from
> setting __file__, is there anything else that import does and this
> snippet doesn't?

The import statement does several things. For instance it holds the
import lock to stop other threads from importing the same module again.
It also does lots of additional work for packages like relative imports,
checking __path__, setting attributes on parent packages and so on. The
import system also does a lot of work in order to find and load a
module, too.

Christian



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


Re: Detect target name in descriptor __set__ method

2009-07-23 Thread Rainer Mansfeld

Gabriel Genellina schrieb:
I have a class attribute 'foo' which is a data descriptor. I create an 
instance of such class. When I say instance.foo = value, the descriptor 
__set__ method is called. Is there any way to obtain the name being 
assigned to? ('foo' in this example). That is, I want to know the target 
name for the assignment that triggered the __set__ method call.




class descriptor(object):
def __get__(self, instance, owner):
  return self

def __set__(self, instance, value):
# I want to know the *name* this value is being assigned to
for name in instance.__class__.__dict__:
if getattr(instance, name) is self:
print "assigning to %s" % name
break


class X(object):
foo = descriptor()
bar = descriptor()

class Y(object):
foo = descriptor()
baz = descriptor()

x = X()
y = Y()

x.foo = "value"
x.bar = "value"
y.foo = "value"
y.baz = "value"

Does this work for you?

   Rainer

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


Re: ANN: psyco V2

2009-07-23 Thread Christian Heimes
Neuruss wrote:
> It seems psyco.org is still in the transfer process...
> Is there any charitable soul with a link to a Windows binary? :-)

It seems like Christian is already working on Windows binaries. We are
having a discussing about an obscure MinGW bug on the Python developer
list. It looks like MinGW isn't fully ABI compatible with MSVC.

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


Re: ANN: psyco V2

2009-07-23 Thread Christian Heimes
Christian Tismer wrote:
> Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows,
> and Mac OS X. Psyco is not supporting 64 bit, yet. But it
> is well being considered.

Can you estimate how much work needs to be done in order to get Psyco
working on 64bit POSIX (Linux) systems?

Christian

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


Re: regex: multiple matching for one string

2009-07-23 Thread Nick Dumas
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Agreed. Two string.split()s, first at the semi-colon and then at the
equal sign, will yield you your value, without having to fool around
with regexes.

On 7/23/2009 9:23 AM, Mark Lawrence wrote:
> scriptlear...@gmail.com wrote:
>> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I
>> will like to take out the values (valuea, valueb, and valuec).  How do
>> I do that in Python?  The group method will only return the matched
>> part.  Thanks.
>>
>> p = re.compile('#a=*;b=*;c=*;')
>> m = p.match(line)
>> if m:
>>  print m.group(),
> 
> IMHO a regex for this is overkill, a combination of string methods such
> as split and find should suffice.
> 
> Regards.
> 
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkpogb0ACgkQLMI5fndAv9jtOwCgj3+YOLfKGvAdyCMOhh4NGgfy
x5YAn1ydhUYxGlvC4Z4WlWKaa1gwviSh
=jnp1
-END PGP SIGNATURE-
-- 
http://mail.python.org/mailman/listinfo/python-list


Office COM automatisation - calling python from VBA

2009-07-23 Thread Dushku, Aaron - Amherst, MA
I'd like a copy of that code.  Thanks for taking the time for all of us.


Sincerely,
Aaron Dushku


**
Aaron Dushku
GIS Specialist
USDA-NRCS
Amherst, Massachusetts
(413) 253-4379
Email:  aaron.dushku at ma.usda.gov


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


win32clipboard operation

2009-07-23 Thread LeeRisq
Hi all,

Newbie question. I've written a script that outputs to a text file.

Now, I just want to copy the content to win32clipboard for access to
other applications. Here's the mess I've come up with so far:)

import xlrd
import win32clipboard



def program_uno():
ofile = open(r"C:\Query\DQL.txt", "w")
book = xlrd.open_workbook("C:\DocLoader\MCL_Drawing and Legacy
Docloader Sheet.xls")
sh = book.sheet_by_index(0)
e = sh.cell_value(1, 0)
a = sh.col_values(0, start_rowx=2, end_rowx=200)
b = r'%' + e
c = r'%Draft%'
y = r"some text..." %(b, c)
w = r"some more text..."
ofile.writelines(y)
for x in a:
d = r'%' + x
z = r" loop text..." %(d, c)
f = ofile.writelines(z)
ofile.writelines(w)


def copy_text():
ifile = open(r"C:\Query\DQL.txt", "r")


win32clipboard.OpenClipboard(0)
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText()
win32clipboard.CloseClipboard

program_uno()
copy_text()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is file.encoding convention?

2009-07-23 Thread Vinay Sajip
On Jul 23, 4:06 am, Naoki INADA  wrote:
> In document  stdtypes.html#file.encoding>:
>
> >> The encoding that this file uses. When Unicode strings are written to a 
> >> file,
> >>  they will be converted to byte strings using this encoding. In addition,
> >> when the file is connected to a terminal, the attribute gives the encoding
> >> that the terminal is likely to use
>
> But inlogging.StreamHandler.emit() ::
>
> try:
> if (isinstance(msg, unicode) and
> getattr(stream, 'encoding', None)):
> #fs = fs.decode(stream.encoding)
> try:
> stream.write(fs % msg)
> except UnicodeEncodeError:
> #Printing to terminals sometimes fails.
> For example,
> #with an encoding of 'cp1251', the above
> write will
> #work if written to a stream opened or
> wrapped by
> #the codecs module, but fail when writing
> to a
> #terminal even when the codepage is set to
> cp1251.
> #An extra encoding step seems to be
> needed.
> stream.write((fs % msg).encode
> (stream.encoding))
> else:
> stream.write(fs % msg)
> except UnicodeError:
> stream.write(fs % msg.encode("UTF-8"))
>
> And behavior of sys.stdout in Windows::>>> import sys
> >>> sys.stdout.encoding
> 'cp932'
> >>> u = u"あいう"
> >>> u
>
> u'\u3042\u3044\u3046'>>> print >>sys.stdout, u
> あいう
> >>> sys.stderr.write(u)
>
> Traceback (most recent call last):
>   File "", line 1, in 
> UnicodeEncodeError: 'ascii' codec can't encode characters in position
> 0-2: ordinal not in range(128)
>
> What is file.encoding convention?
> If I want to write a unicode string to a file(-like) that have
> encoding attribute, I should do
> (1) try: file.write(unicode_str),
> (2) except UnicodeEncodeError: file.write(unicode_str.encode
> (file.encoding))
> likelogging?
> It seems agly.

If you are writing a Unicode string to a stream which has been opened
with e.g. codecs.open with a specific encoding, then the stream is
actually a wrapper. You can write Unicode strings directly to it, and
the wrapper stream will encode the Unicode to bytes using the specific
encoding and write those bytes to the underlyting stream. In your
example you didn't show sys.stderr.encoding - you showed
sys.stdout.encoding and printed out something to it which seemed to
give the correct result, but then wrote to sys.stderr which gave a
UnicodeEncodeError. What is the encoding of sys.stderr in your
example? Also note that logging had to handle what appeared to be an
oddity with terminals - they (at least sometimes) have an encoding
attribute but appear to expect to have bytes written to them, and not
Unicode. Hence the logging kludge, which should not be needed and so
has been carefully commented.

Regards,

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


Re: regex: multiple matching for one string

2009-07-23 Thread Bill Davy
"Mark Lawrence"  wrote in message 
news:mailman.3588.1248355389.8015.python-l...@python.org...
> scriptlear...@gmail.com wrote:
>> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I
>> will like to take out the values (valuea, valueb, and valuec).  How do
>> I do that in Python?  The group method will only return the matched
>> part.  Thanks.
>>
>> p = re.compile('#a=*;b=*;c=*;')
>> m = p.match(line)
>> if m:
>>  print m.group(),
>
> IMHO a regex for this is overkill, a combination of string methods such as 
> split and find should suffice.
>
> Regards.
>


For the OP, it can be done with regex by grouping:

p = re.compile(r'#a=(*);b=(*);c=(*);')
m = p.match(line)
   if m:
 print m.group(1),

m.group(1) has valuea in it, etc.

But this may not be the best way, but it is reasonably terse. 


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


extract c/cpp include file with regular expression

2009-07-23 Thread tiefeng wu
Hi all!
I need to parse c/cpp source files, one requirement is to extract
included header file name.
here is my solution:
>>> p = re.compile(r'#\s*include\s+(?:(<)|("))(.*)(?(1)>)(?(2)")')
>>> m = re.search(p, '#include ')
>>> m.group(3)
'header.h'
>>> m = re.search(p, '#include "header.h"')
>>> m.group(3)
'header.h'
>>> m = re.search(p, '#include >> print(m)
None
>>> m = re.search(p, '#include "header.h>')
>>> print(m)
None

Pretty ugly! And I know for a valid c/cpp source file, it will be not
necessary to check and match '<' with '>' and " with ",
but I'm wondering to see more elegant way to do such thing.

tiefeng wu
2009-07-23
-- 
http://mail.python.org/mailman/listinfo/python-list


Cannot Get Form To Work

2009-07-23 Thread Victor Subervi
Hi:
When the form comes up the first time, there is the default value for num.
When I fill in a number in the form and press send, even though the form
sends to itself (same page name), I would think it would read the number
sent. Here again is the code:

from primeNumbers import primeNumbers

try:
 lang = form.getfirst('lang', 'en')
 browser = form.getfirst('browser', 'all')
 site = form.getfirst('site', 'bridge')
 num = form.getfirst('num','')
except:
 pass

ourFile = string.split(__file__, "/")
p = ourFile[len(ourFile) - 1]
p = p[: - 9]
site = ourFile[4][:-10]
if site != '':
 site = site[:-1]

print "Content-Type: text/html"
print
print """
http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd";>
http://www.w3.org/1999/xhtml";>

"""
if num != '':
 num = round(float(num))
 roots = primeNumbers(num)
 print roots
if num == '':
 print """
http://13gems.com/test-Calculators_frame.py"; method="post">

http://13gems.com/images/search.jpg"; name="search"
id="search" />

"""
print '\n'
-- 
http://mail.python.org/mailman/listinfo/python-list


strange python scripting error

2009-07-23 Thread Mark Tarver
I have a very strange error.  I have two test python files test.py and
python.py which contain the following code

#!/usr/bin/python
print "Content-type: text/html"
print
print ""
print "Hello, Linux.com!"
print ""

One file (test.py) works; you call it up and it shows a web page with

Hello, Linux.com

The other fails with a server configuration error.  Both are running
under Linux, same server, same permissions.  Running a character scan
shows that both files contain the same printable characters and are
therefore typographically identical.   They are absolutely the same.

The only hint at a difference I can see is that my ftp program says
the files are of unequal lengths.  test.py is 129 bytes long.
python.py 134 bytes long.

A zipped folder containing both files is at

www.lambdassociates.org/weird.zip

Any ideas welcome.

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


Re: regex: multiple matching for one string

2009-07-23 Thread tiefeng wu
2009/7/23 scriptlear...@gmail.com :
> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I
> will like to take out the values (valuea, valueb, and valuec).  How do
> I do that in Python?  The group method will only return the matched
> part.  Thanks.
>
> p = re.compile('#a=*;b=*;c=*;')
> m = p.match(line)
>        if m:
>             print m.group(),
> --
> http://mail.python.org/mailman/listinfo/python-list
>

maybe like this:
>>> p = re.compile(r'#?\w+=(\w+);')
>>> l = re.findall(p, '#a=valuea;b=valueb;c=valuec;')
>>> for r in l: print(r)
...
valuea
valueb
valuec

tiefeng wu
2009-07-23
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extract c/cpp include file with regular expression

2009-07-23 Thread Philip Semanchuk


On Jul 23, 2009, at 11:46 AM, tiefeng wu wrote:


Hi all!
I need to parse c/cpp source files, one requirement is to extract
included header file name.
here is my solution:

p = re.compile(r'#\s*include\s+(?:(<)|("))(.*)(?(1)>)(?(2)")')
m = re.search(p, '#include ')
m.group(3)

'header.h'

m = re.search(p, '#include "header.h"')
m.group(3)

'header.h'

m = re.search(p, '#include 
None

m = re.search(p, '#include "header.h>')
print(m)

None

Pretty ugly! And I know for a valid c/cpp source file, it will be not
necessary to check and match '<' with '>' and " with ",
but I'm wondering to see more elegant way to do such thing.


Hi tiefeng,
Regexes are always a little ugly IMO. =)

A side note -- does your parser need to handle /* comments like this  
one*/?  If so, then regular expressions are not going be sufficient.


Good luck
Philip

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


Re: strange python scripting error

2009-07-23 Thread Diez B. Roggisch
Mark Tarver wrote:

> I have a very strange error.  I have two test python files test.py and
> python.py which contain the following code
> 
> #!/usr/bin/python
> print "Content-type: text/html"
> print
> print ""
> print "Hello, Linux.com!"
> print ""
> 
> One file (test.py) works; you call it up and it shows a web page with
> 
> Hello, Linux.com
> 
> The other fails with a server configuration error.  Both are running
> under Linux, same server, same permissions.  Running a character scan
> shows that both files contain the same printable characters and are
> therefore typographically identical.   They are absolutely the same.
> 
> The only hint at a difference I can see is that my ftp program says
> the files are of unequal lengths.  test.py is 129 bytes long.
> python.py 134 bytes long.
> 
> A zipped folder containing both files is at
> 
> www.lambdassociates.org/weird.zip
> 
> Any ideas welcome.

They have different line-ending-conventions. Not sure if and why that makes
a difference.

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


Re: win32clipboard operation

2009-07-23 Thread MRAB

LeeRisq wrote:

Hi all,

Newbie question. I've written a script that outputs to a text file.

Now, I just want to copy the content to win32clipboard for access to
other applications. Here's the mess I've come up with so far:)


[snip]

def copy_text():
ifile = open(r"C:\Query\DQL.txt", "r")
text = ifile.read()
ifile.close()

win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(text)
win32clipboard.CloseClipboard()

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


Re: sqlite3 performance problems only in python

2009-07-23 Thread David Stanek
On Thu, Jul 23, 2009 at 9:02 AM, Stef Mientki wrote:
>
> btw, I don't know if it's of any importance, the SQL-statement I perform is
> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.*
>  from OPNAMEN
>   inner join POID_VLID          on OPNAMEN.POID            = POID_VLID.POID
>   inner join VRAAGLST           on VRAAGLST.VLID           = POID_VLID.VLID
>   inner join VLID_SSID          on VRAAGLST.VLID           = VLID_SSID.VLID
>   inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID
>   inner join POID_SSID_SCID     on ( OPNAMEN.POID            =
> POID_SSID_SCID.POID ) and
>                                    ( SUBSCHAAL_GEGEVENS.SSID =
> POID_SSID_SCID.SSID )
>   inner join SCORES             on SCORES.SCID             =
> POID_SSID_SCID.SCID
>   inner join PID_POID           on OPNAMEN.POID            = PID_POID.POID
>   inner join PATIENT            on PATIENT.PID             = PID_POID.PID
>  where substr ( lower( NAME) , 1, 6)  = 'cis20r'
>   and lower ( NAME_ ) = 'fatigue'
>   and TEST_COUNT in (3,4)
>   and DATETIME > 39814.0
>   and SCORE < 30

Warning: I suck at SQL and hate it with a passion...

By using lower() on the left side of the where expressions I believe
that you are table scanning. So it is not the size of the data
returned, but the size of the data that needs to be scanned.

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


Re: extract c/cpp include file with regular expression

2009-07-23 Thread MRAB

tiefeng wu wrote:

Hi all!
I need to parse c/cpp source files, one requirement is to extract
included header file name.
here is my solution:

p = re.compile(r'#\s*include\s+(?:(<)|("))(.*)(?(1)>)(?(2)")')
m = re.search(p, '#include ')
m.group(3)

'header.h'

m = re.search(p, '#include "header.h"')
m.group(3)

'header.h'

m = re.search(p, '#include 
None

m = re.search(p, '#include "header.h>')
print(m)

None

Pretty ugly! And I know for a valid c/cpp source file, it will be not
necessary to check and match '<' with '>' and " with ",
but I'm wondering to see more elegant way to do such thing.


I'd probably do:

>>> p = re.compile(r'#\s*include\s+(?:<([^>]*)>|"([^"]*)")')
>>> m = p.search('#include ')
>>> m.group(1) or m.group(2)
'header.h'
--
http://mail.python.org/mailman/listinfo/python-list


Re: strange python scripting error

2009-07-23 Thread Richard Brodie

"Diez B. Roggisch"  wrote in message 
news:7crfjof29e4g...@mid.uni-berlin.de...

> They have different line-ending-conventions. Not sure if and why that makes
> a difference.

Depends on your setup. Shells can be a bit dumb about it, so
it will likely break simple cgi-style hosting.

-bash: ./python.py: /usr/bin/python^M: bad interpreter: 


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


Re: strange python scripting error

2009-07-23 Thread Peter Otten
Diez B. Roggisch wrote:

> Mark Tarver wrote:
> 
>> I have a very strange error.  I have two test python files test.py and
>> python.py which contain the following code
>> 
>> #!/usr/bin/python
>> print "Content-type: text/html"
>> print
>> print ""
>> print "Hello, Linux.com!"
>> print ""
>> 
>> One file (test.py) works; you call it up and it shows a web page with
>> 
>> Hello, Linux.com
>> 
>> The other fails with a server configuration error.  Both are running
>> under Linux, same server, same permissions.  Running a character scan
>> shows that both files contain the same printable characters and are
>> therefore typographically identical.   They are absolutely the same.
>> 
>> The only hint at a difference I can see is that my ftp program says
>> the files are of unequal lengths.  test.py is 129 bytes long.
>> python.py 134 bytes long.
>> 
>> A zipped folder containing both files is at
>> 
>> www.lambdassociates.org/weird.zip
>> 
>> Any ideas welcome.
> 
> They have different line-ending-conventions. Not sure if and why that
> makes a difference.

Looks like the shell treats the CR as part of the interpreter name:

$ cat python.py
#!/usr/bin/python
print "Content-type: text/html"
print
print ""
print "Hello, Linux.com!"
print ""$
$ python python.py
Content-type: text/html


Hello, Linux.com!

$ chmod u+x python.py
$ ./python.py
bash: ./python.py: /usr/bin/python^M: bad interpreter: No such file or 
directory

Peter

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


Re: extract c/cpp include file with regular expression

2009-07-23 Thread tiefeng wu
MRAB wrote:
> I'd probably do:
>
 p = re.compile(r'#\s*include\s+(?:<([^>]*)>|"([^"]*)")')
 m = p.search('#include ')
 m.group(1) or m.group(2)
> 'header.h'
>

yes, it's easier to understand.
thanks, MRAB!
I always make things complicated :P

tiefeng wu
2009-07-23
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Detect target name in descriptor __set__ method

2009-07-23 Thread DG
On Jul 23, 8:44 am, Rainer Mansfeld  wrote:
> Gabriel Genellina schrieb:
>
> > I have a class attribute 'foo' which is a data descriptor. I create an
> > instance of such class. When I say instance.foo = value, the descriptor
> > __set__ method is called. Is there any way to obtain the name being
> > assigned to? ('foo' in this example). That is, I want to know the target
> > name for the assignment that triggered the __set__ method call.
>
> class descriptor(object):
>      def __get__(self, instance, owner):
>        return self
>
>      def __set__(self, instance, value):
>          # I want to know the *name* this value is being assigned to
>          for name in instance.__class__.__dict__:
>              if getattr(instance, name) is self:
>                  print "assigning to %s" % name
>                  break
>
> class X(object):
>      foo = descriptor()
>      bar = descriptor()
>
> class Y(object):
>      foo = descriptor()
>      baz = descriptor()
>
> x = X()
> y = Y()
>
> x.foo = "value"
> x.bar = "value"
> y.foo = "value"
> y.baz = "value"
>
> Does this work for you?
>
>     Rainer

The reason I wasn't checking the class' '__dict__' attribute in my
solution was because this won't show any descriptors that were
inherited from base classes.  Example with some optimizations below
(sorry for the long code):

builtin_methods = dir(object)

class descriptor(object):
def __init__(self):
self.name = None

def __get__(self, instance, owner):
# if you want a 'useful' data descriptor only return self upon
# non-instance access
if instance is None:
return self
else:
# do something besides the below for the usefulness
return self

def __set__(self, instance, value):
if self.name is None:
cls = instance.__class__
dir_attrs = [m for m in dir(cls) if m not in
builtin_methods]
# 'foo' is printed here
print 'dir(cls):', dir_attrs
# 'foo' is not printed here
print 'cls.__dict__:', cls.__dict__
for name in dir_attrs:
if getattr(cls, name) is self:
self.name = name
print "setting %s to %s" % (self.name, value)


class baseX(object):
foo = descriptor()

class X(baseX):
pass

x = X()
x.foo = 'bar'

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


Re: extract c/cpp include file with regular expression

2009-07-23 Thread tiefeng wu
2009/7/24 Philip Semanchuk :
>
> I know this will sound like a sarcastic comment, but it is sincere: my
> suggestion is that if you want to parse C/C++ (or Python, or Perl, or
> Fortran, etc.), use a real parser, not regexes unless you're willing to
> sacrifice some accuracy. Sooner or later you'll come across some code that
> your regexes won't handle, like this --
>
> #ifdef FOO_BAR
> #include 
> /* #else */
> #include 
> #endif
>
>
> Parsing code is difficult...
>
I understand your point, thanks for your suggestion, Philip. And I've
met the problem like in your example
The reason I choose regex because I barely know about "real parser",
for me it still in some "dark area" :)
But I'll find something to learn.

tiefeng wu
2009-07-23
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: psyco V2

2009-07-23 Thread Christian Tismer

On 7/23/09 8:22 AM, Christian Heimes wrote:

Christian Tismer wrote:

Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows,
and Mac OS X. Psyco is not supporting 64 bit, yet. But it
is well being considered.


Can you estimate how much work needs to be done in order to get Psyco
working on 64bit POSIX (Linux) systems?


This is not easy to tell. I'm in the process of estimating
this, because my sponsor wants to know as well. They are
very interested, but it has to be somehow affordable in
time and money.

There are different paths that can be taken. Simply hacking away,
trying to go straight to 64 bit is obvious, but probably a bad
approach. Half of the system needs to be rewritten and augmented
with extra size info, and this goes very deep. I think this
way I would produce a nightmare of even more complicated code,
and would kill myself debugging-wise.

I believe I need to simplify psyco and make many parts more
abstract and more general, to become able to make it flexible.
This also means slowing the compiler down quite a lot.

Slowing it down will again become no problem, when my new
compiler strategy is ready. The number of compilations
will reduce so drastically, that the slowdown is neglectible.

Yes, I did not give an answer. I have the vague feeling of
three months full-time work. My problem right now is to
ensure that it will become less and not more :-)

cheers - chris
--
Christian Tismer :^)   
tismerysoft GmbH : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9A :*Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 802 86 56  mobile +49 173 24 18 776  fax +49 30 80 90 57 05
PGP 0x57F3BF04   9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
  whom do you want to sponsor today?   http://www.stackless.com/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Changing the private variables content

2009-07-23 Thread Ethan Furman
Or, in other words, what Steven D'Aprano had already said.  Guess I 
should read the whole thread before madly posting!  :)


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


Re: Balanced binary tree implementation

2009-07-23 Thread M.-A. Lemburg
Lucas P Melo wrote:
> Hello,
> 
> I would like to use a balanced binary tree implementation (preferably
> within some API).
> Any hints about where I could find it?
> 
> I am looking for something that implements insertion, deletion, search
> and a special search that returns the lesser element bigger than a given
> key [1].
> 
> A nice possibility would be an extensible API that allows me to inherit
> its classes and to add operations myself.
> 
> Thanks in advance.
> 
> [1] Ex: 1 2 3 4 5 6 are elements of the bbt. If I use this operation
> given 4 as the parameter, the value returned would be 5.

You might want to have a look at the btree implementation we
have in mxBeeBase:

http://www.egenix.com/products/python/mxBase/mxBeeBase/

It's written in C and optimized for on-disk operations.

-- 
Marc-Andre Lemburg
eGenix.com

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


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extract c/cpp include file with regular expression

2009-07-23 Thread Philip Semanchuk


On Jul 23, 2009, at 12:36 PM, tiefeng wu wrote:


2009/7/24 Philip Semanchuk :


I know this will sound like a sarcastic comment, but it is sincere:  
my

suggestion is that if you want to parse C/C++ (or Python, or Perl, or
Fortran, etc.), use a real parser, not regexes unless you're  
willing to
sacrifice some accuracy. Sooner or later you'll come across some  
code that

your regexes won't handle, like this --

#ifdef FOO_BAR
#include 
/* #else */
#include 
#endif


Parsing code is difficult...


I understand your point, thanks for your suggestion, Philip. And I've
met the problem like in your example
The reason I choose regex because I barely know about "real parser",
for me it still in some "dark area" :)
But I'll find something to learn.


Yes! Learning is always good. And as I said, if you don't mind missing  
some unusual cases, regexes are fine. I don't know how accurate you  
want your results to be.


As for real parsers, there's lots of them out there, although they may  
be overkill for what you want to do. Here's one written entirely in  
Python:

http://www.dabeaz.com/ply/

Whatever you choose, good luck with it.

Cheers
Philip

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


Re: PyQt GUI

2009-07-23 Thread Robert Kern

On 2009-07-23 03:55, Helvin wrote:

I believe I now have vtkpython.exe. However, my 'import vtk' statement
in my python code is not working. The error says something like "no
module named vtk".
Where do I find modules for vtk in pyqt? Do they exist?


There are no VTK modules in PyQt itself. The PyQt support is in the vtk package 
which can be built with VTK itself. You will need to install VTK and the vtk 
package correctly in order to achieve this. vtkpython.exe is not going to help 
you. Ignore it. After you have built VTK, you need to do an extra step to 
install the vtk package to where your Python interpreter will be able to find it.


Let's say that your build directory is c:\vtkbuild.

  cd \vtkbuild\Wrapping\Python
  python setup.py install
  cd \

Now you should be able to import vtk from your normal Python interpreter. If you 
are still having problems, you will need to copy-and-paste exactly what you did 
and what error messages you got. Do not paraphrase error messages.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: ANN: psyco V2

2009-07-23 Thread Christian Tismer

On 7/17/09 4:11 AM, Bearophile wrote:

Very good, thank you. I'll try it when I can.

Is Psyco3 going to borrow/steal some ideas/code from Unladen Swallow?


Psyco3: nice typo! :-)

Well, I haven't so far found a new idea there that I'd want
to borrow and did not know from PyPy, before.
Wasn't the project plan saying the opposite, borrowing
some ideas from psyco? :-)
http://code.google.com/p/unladen-swallow/wiki/ProjectPlan


The problem I have with Psyco1.6 is that you can't use the normal
profilers to know how much seconds of running time is taken by each
function/method of your code.


Yes, the profiler hooks are not useful with psyco. You need
to write extra functions for timing, as we do in the benchmark
directory.


Psyco1.6 has a profile() function, but I am not much able to use it
yet.


The profile() function is used for profile driven compilation,
as opposed to psyco.full(). This will go away, pretty soon.
Psyco will only be switched on or off.

Maybe I will add an option for profiling the compiled code.
Interesting idea!

cheers - chris

--
Christian Tismer :^)   
tismerysoft GmbH : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9A :*Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 802 86 56  mobile +49 173 24 18 776  fax +49 30 80 90 57 05
PGP 0x57F3BF04   9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
  whom do you want to sponsor today?   http://www.stackless.com/
--
http://mail.python.org/mailman/listinfo/python-list


installing 2.6 on vista64

2009-07-23 Thread DwBear75
I just downloaded and attempted to install python 2.6.2.  The
installer proceeds to do its work then dies, leaving an entry in the
eventlog:

Windows Installer installed the product. Product Name: Python 2.6.2.
Product Version: 2.6.2150. Product Language: 1033. Installation
success or error status: 1602.

Googling for this I wasn't able to narrow the results down to
something usable. Anyone know of issues and how to fix them installing
on vista 64 (yes, I have 8 gigs of ram)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: win32clipboard operation

2009-07-23 Thread LeeRisq
On Jul 23, 9:05 am, MRAB  wrote:
> LeeRisq wrote:
> > Hi all,
>
> > Newbie question. I've written a script that outputs to a text file.
>
> > Now, I just want to copy the content to win32clipboard for access to
> > other applications. Here's the mess I've come up with so far:)
>
> [snip]
>
> def copy_text():
>      ifile = open(r"C:\Query\DQL.txt", "r")
>      text = ifile.read()
>      ifile.close()
>
>      win32clipboard.OpenClipboard()
>      win32clipboard.EmptyClipboard()
>      win32clipboard.SetClipboardText(text)
>      win32clipboard.CloseClipboard()

I've actually tried this configuration, but I did it again just to be
sure. The program executes without exception, but the text still isn't
copied to the clipboard. Any ideas?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import vs imp and friends.

2009-07-23 Thread Robert Kern

On 2009-07-23 09:44, Emanuele D'Arrigo wrote:

Greetings,

I was looking in the archive of this newsgroup and I found this
snippet:

import imp
sourcecode = 'def foo(x): return 11*x'
mod = imp.new_module('foo')
exec sourcecode in mod.__dict__
mod.foo(16)

Together with similar and sometimes more complete snippets available
they show how a module can be created out of string, plain text files
and compiled files. Neat!

Now the question. Apart from checking sys.module first and eventually
adding the new module to it if it isn't there already, and apart from
setting __file__, is there anything else that import does and this
snippet doesn't?


Brett Cannon has a good presentation that covers basically the entirety of the 
import mechanism:


http://us.pycon.org/2008/conference/schedule/event/12/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: If Scheme is so good why MIT drops it?

2009-07-23 Thread Isaac Gouy
On Jul 21, 10:09 pm, Raffael Cavallaro
 wrote:
> On 2009-07-21 19:06:02 -0400, Neil Hodgson
>  said:
>
> >    Python uses native threads.
>
> So it can be teh-slowness on all ur cores!
>
> 
>
> The global interpreter lock doesn't help much either.


As you've linked to programs that /have not/ been written to use
threading or multiple cores (look at the "~ CPU Load" column) I get
the feeling I'm missing the joke?



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


Re: Help understanding the decisions *behind* python?

2009-07-23 Thread Mark Lawrence

Phillip B Oldham wrote:

My colleagues and I have been working with python for around 6 months
now, and while we love a lot of what python has done for us and what
it enables us to do some of the decisions behind such certain
data-types and their related methods baffle us slightly (when compared
to the decisions made in other, similarly powerful languages).

Specifically the "differences" between lists and tuples have us
confused and have caused many "discussions" in the office. We
understand that lists are mutable and tuples are not, but we're a
little lost as to why the two were kept separate from the start. They
both perform a very similar job as far as we can tell.



[rest of original snipped as already discussed]

Sorry if this has been discussed and I've missed it, but how about 
memory allocation.  An immutable tuple has a fixed memory allocation 
whereas that for the mutable list must be liable to change.  You might 
like to look at the recent thread on this ng 'List insertion cost' and 
follow the links to Raymond Hettinger's power point presentation.


Kindest regards.

Mark Lawrence.

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


Re: challenging problem for changing to a dedicated non-privileged user within a script.

2009-07-23 Thread Piet van Oostrum
> Krishnakant  (K) wrote:

>K> On Thu, 2009-07-23 at 13:50 +0200, paul wrote:
>>> If the user running python program is allowed to call setuid() then yes.
>>> 
>K> NO, i don't think i can do that.  I am getting opperation not permitted.

>K> Any ways I think probably subprocess will have to sort it out.

>>> Did you try running "sudo -u postgres blabla" with subprocess?
>>> 
>K> Yes, but still not got the intended result which is now obvious.
>>> > 2. now execute the python code for connecting to the postgresql
>>> > database.
>>> > In the second point I actually want to execute python code not shell
>>> > level command so will the sudo -u in the subprocess.Popen change the
>>> > user in the script?
>>> No, as the name "subprocess" suggests you are spawning a new process 
>>> which gets another uid through sudo. This does not affect the parent 
>>> process.
>>> 
>K> Ok then here is the work-around which I am thinking to try, Plese tell
>K> me if it is correct.
>K> I will let that subprocess start python inthe background and execute the
>K> connecting code to postgresql including importing the pygresql library.
>K> Then I will create the connection and cursor objcts in that subprocess.
>K> But my concern is, will the connection object in the child process
>K> (subprocess) be available to the parrent process?

No. However it is still not clear why you want to run under the postgres
user id. Why can't the original process not do the postgres connection?

If that is really impossible, then you might start the new process with
sudo and let it do a socket tunnelling to postgress, i.e. make a
connection to the postgres server and a socket connection to the
original Python script, and copy everything from one socket to the other
- in both directions. However this can also be done with a ssh tunnel
which might be simpler.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: If Scheme is so good why MIT drops it?

2009-07-23 Thread game_designer
Perhaps like Xah Lee I find, after many years of Lisp programming,
these discussions increasingly frustrating and even, in some sense,
amazing. We can speculate all we want about syntax and semantics of
programing languages. What counts in the end are really the PRAGMATICS
of programming languages. How can I do something with a language that
is USEFUL to me? Will the result be good looking, and snappy or some
ugly, dated looking, crashing application? For instance, last time I
played with Scheme (drScheme) to explore some OpenGL 3D issue I was
not impressed at all. One can debate the syntax and semantics of
Scheme but in that particular instance all that was important to me
was the fact that the Scheme example performed terrible and the
threading fell completely apart when running more that a single OpenGL
window. Perhaps this was coded poorly but I don't care. Scheme left a
pretty bad impression.


alex

Prof. Alexander Repenning

University of Colorado
Computer Science Department
Boulder, CO 80309-430
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sqlite3 performance problems only in python

2009-07-23 Thread Piet van Oostrum
> Stef Mientki  (SM) wrote:

>SM> btw, I don't know if it's of any importance, the SQL-statement I perform is
>SM> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.*
>SM>  from OPNAMEN
>SM>inner join POID_VLID  on OPNAMEN.POID= 
>POID_VLID.POID
>SM>inner join VRAAGLST   on VRAAGLST.VLID   = 
>POID_VLID.VLID
>SM>inner join VLID_SSID  on VRAAGLST.VLID   = 
>VLID_SSID.VLID
>SM>inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = 
>VLID_SSID.SSID
>SM>inner join POID_SSID_SCID on ( OPNAMEN.POID=
>SM> POID_SSID_SCID.POID ) and
>SM> ( SUBSCHAAL_GEGEVENS.SSID =
>SM> POID_SSID_SCID.SSID )
>SM>inner join SCORES on SCORES.SCID =
>SM> POID_SSID_SCID.SCID
>SM>inner join PID_POID   on OPNAMEN.POID= PID_POID.POID
>SM>inner join PATIENTon PATIENT.PID = PID_POID.PID
>SM>  where substr ( lower( NAME) , 1, 6)  = 'cis20r'
>SM>and lower ( NAME_ ) = 'fatigue'
>SM>and TEST_COUNT in (3,4)
>SM>and DATETIME > 39814.0
>SM>and SCORE < 30

1) Do you have indices on the join fields?
2) Look at the ANALYZE command
3) Look at the EXPLAIN command
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: If Scheme is so good why MIT drops it?

2009-07-23 Thread Paul Donnelly
game_designer  writes:

> Perhaps like Xah Lee I find, after many years of Lisp programming,
> these discussions increasingly frustrating and even, in some sense,
> amazing. We can speculate all we want about syntax and semantics of
> programing languages. What counts in the end are really the PRAGMATICS
> of programming languages. How can I do something with a language that
> is USEFUL to me? Will the result be good looking, and snappy or some
> ugly, dated looking, crashing application? For instance, last time I
> played with Scheme (drScheme) to explore some OpenGL 3D issue I was
> not impressed at all. One can debate the syntax and semantics of
> Scheme but in that particular instance all that was important to me
> was the fact that the Scheme example performed terrible and the
> threading fell completely apart when running more that a single OpenGL
> window. Perhaps this was coded poorly but I don't care. Scheme left a
> pretty bad impression.

One implementation of one dialect of Lisp worked poorly for one
particular project some unspecified number of years ago, judging by code
(written by you, no less) that may or may not have been terrible? I
appreciate that this was a frustrating experience, but I don't see what
lesson about Lisp programming we're supposed to be getting from this.
-- 
http://mail.python.org/mailman/listinfo/python-list


Creating xml

2009-07-23 Thread Greg Lindstrom
It's been a while since I've played with XML using Python but I've been
asked to create XML using data from our postgres database.  Currently we are
generating XML directly from the database using a set of stored procedures
but it is too slow (yes, I have numbers).  I have been asked to create a
routine to generate the XML to see if it will be faster that the sprocs
(which I think it will be, based on other routines we have running).  But I
digress...

Using my favorite search engine I see that there is a lot of material on
Python and XML.  At the risk of starting something (which is not my
intention), what are my options if I want to create xml?  Ceratinly writing
my own routine is an option, but I bet there are better ones :-)

How about if I need/want to parse or process an XML file?

Thanks!

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


Problems in commands.getoutput(cmd) with sox

2009-07-23 Thread bbarbero

Hello to all!

I am a new researcher, new to Python as well, and I have a problem,  
when I try to get commands from sox in a python script. I am going  
crazy, because this code has been working before.. so I dont really  
know whats going on..


Ive already seen some solutions that match with what I have. I am  
running this:




if os.path.splitext(file_au)[1] == ".au":
resampled_file_path = os.path.join(resampled_path, file_au)
cmd = "sox " + dir_file + " -r 44100 " + resampled_file_path
print cmd
output = commands.getoutput(cmd)
print output

What i do, is just to resample a song from dir_file to  
resampled_file_path. As a result of "cmd" I get:



sox /Volumes/HAL/Datasets/Audio/gtzan_genres/rock/rock.00097.au -r  
44100  
/Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resampled/rock/rock.00097.au

.

If I run this on the command line, it will work perfectly!

But The program stops at

 File  
"/Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resamplingto44.py",  
line 35

output = commands.getoutput(cmd)
^
IndentationError: unexpected indent

I dont have any idea, whats going on, I am trying lot of things, but I  
can understand where is the error as it has been running perfectly  
before.


Any suggestions will be more than welcome! Thanks in advance for your help!

Best regards,
Beatriz Mora.




This message was sent using IMP, the Internet Messaging Program.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating xml

2009-07-23 Thread Chris Rebert
On Thu, Jul 23, 2009 at 12:30 PM, Greg Lindstrom wrote:

> How about if I need/want to parse or process an XML file?

xml.etree.ElementTree in the standard library:
http://docs.python.org/library/xml.etree.elementtree.html

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


Re: extract c/cpp include file with regular expression

2009-07-23 Thread Nick Craig-Wood
tiefeng wu  wrote:
>  I need to parse c/cpp source files, one requirement is to extract
>  included header file name.

If you are serious about parsing C code then you'll want to
investigate gcc-xml

http://www.gccxml.org/HTML/Index.html

This runs the gcc frontend over the code but instead of producing an
object file, it produces a machine readable xml file describing the
source.

It is used by h2xml.py / xml2py.py to make ctypes header file
automatically.

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: win32clipboard operation

2009-07-23 Thread LeeRisq
> I've actually tried this configuration, but I did it again just to be
> sure. The program executes without exception, but the text still isn't
> copied to the clipboard. Any ideas?

So, I think I've figured out the issue. Which brings me to another
question...why is it that I can manually copy and paste the text
without issue, but when I run a python script that copies the same
text to the clipboard, it won't accept the amount of text I am placing
on the clipboard. Is there some kind of setting I am missing? Buffer
size maybe?

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


Re: Looking for os.listdir() generator

2009-07-23 Thread Piet van Oostrum
> Christian Heimes  (CH) wrote:

>CH> Hello,
>CH> I'm looking for a generator version of os.listdir() for Python 2.5 and
>CH> newer. I know somebody has worked on it because I've seen a generator
>CH> version in a posting on some list or blog a while ago. I can't find it
>CH> anymore. It seems my Google fu is lacking today. All I can find is a
>CH> very old version of xlistdir. A Cython based solution is appreciated but
>CH> please no ctypes version.

Nick Craig-Wood  posted these about 5 weeks ago in
the thread `waling a directory with very many files' [sic!]

Message id's: 
 (ctypes)
 (Cython)

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


Re: What is file.encoding convention?

2009-07-23 Thread Vinay Sajip
On Jul 23, 4:06 am, Naoki INADA  wrote:
> In document  stdtypes.html#file.encoding>:
>
> >> The encoding that this file uses. When Unicode strings are written to a 
> >> file,
> >>  they will be converted to byte strings using this encoding. In addition,
> >> when the file is connected to a terminal, the attribute gives the encoding
> >> that the terminal is likely to use
>
> But inlogging.StreamHandler.emit() ::
>
> try:
> if (isinstance(msg, unicode) and
> getattr(stream, 'encoding', None)):
> #fs = fs.decode(stream.encoding)
> try:
> stream.write(fs % msg)
> except UnicodeEncodeError:
> #Printing to terminals sometimes fails.
> For example,
> #with an encoding of 'cp1251', the above
> write will
> #work if written to a stream opened or
> wrapped by
> #the codecs module, but fail when writing
> to a
> #terminal even when the codepage is set to
> cp1251.
> #An extra encoding step seems to be
> needed.
> stream.write((fs % msg).encode
> (stream.encoding))
> else:
> stream.write(fs % msg)
> except UnicodeError:
> stream.write(fs % msg.encode("UTF-8"))
>
> And behavior of sys.stdout in Windows::>>> import sys
> >>> sys.stdout.encoding
> 'cp932'
> >>> u = u"あいう"
> >>> u
>
> u'\u3042\u3044\u3046'>>> print >>sys.stdout, u
> あいう
> >>> sys.stderr.write(u)
>
> Traceback (most recent call last):
>   File "", line 1, in 
> UnicodeEncodeError: 'ascii' codec can't encode characters in position
> 0-2: ordinal not in range(128)
>
> What is file.encoding convention?
> If I want to write a unicode string to a file(-like) that have
> encoding attribute, I should do
> (1) try: file.write(unicode_str),
> (2) except UnicodeEncodeError: file.write(unicode_str.encode
> (file.encoding))
> likelogging?
> It seems agly.

Further to my earlier mail, please have a look at the following
screenshot:

http://imgur.com/FtAi0.png

As you can see, the codepage is set to 1251 (Cyrillic) at the
beginning. A Unicode string is initialised with Cyrillic code points.
Then sys.stdout.encoding shows 'cp1251', but writing the string to it
gives a UnicodeEncodeError. Explicitly encoding the string and writing
it works. Next, we get a wrapper from the codecs module for the same
encoding and use it to wrap sys.stdout. Writing the Unicode string to
the wrapped string works, too.

So the problem is essentially this: if a stream has an encoding
attribute, sometimes it is a wrapped stream which encodes Unicode
(e.g. a stream obtained via the codecs module) and sometimes it is not
(e.g. sys.stdout, sys.stderr).

Regards,

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


effbot.org broken (WAS: Problems in commands.getoutput(cmd) with sox)

2009-07-23 Thread Chris Rebert
On Thu, Jul 23, 2009 at 12:42 PM, Chris Rebert wrote:
> You can use tabnanny to help diagnose the problem:
> http://74.125.155.132/search?q=cache:QtxvZm3QDLsJ:effbot.org/librarybook/tabnanny.htm+tabnanny&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a

Anyone know what's the deal with effbot.org? It seems to be broken at
present, forcing me to use Google's cached version.
It's a real shame since it has lots of handy Python info.

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


Re: sqlite3 performance problems only in python

2009-07-23 Thread Stef Mientki

Piet van Oostrum wrote:

Stef Mientki  (SM) wrote:



  

SM> btw, I don't know if it's of any importance, the SQL-statement I perform is
SM> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.*
SM>  from OPNAMEN
SM>inner join POID_VLID  on OPNAMEN.POID= POID_VLID.POID
SM>inner join VRAAGLST   on VRAAGLST.VLID   = POID_VLID.VLID
SM>inner join VLID_SSID  on VRAAGLST.VLID   = VLID_SSID.VLID
SM>inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID
SM>inner join POID_SSID_SCID on ( OPNAMEN.POID=
SM> POID_SSID_SCID.POID ) and
SM> ( SUBSCHAAL_GEGEVENS.SSID =
SM> POID_SSID_SCID.SSID )
SM>inner join SCORES on SCORES.SCID =
SM> POID_SSID_SCID.SCID
SM>inner join PID_POID   on OPNAMEN.POID= PID_POID.POID
SM>inner join PATIENTon PATIENT.PID = PID_POID.PID
SM>  where substr ( lower( NAME) , 1, 6)  = 'cis20r'
SM>and lower ( NAME_ ) = 'fatigue'
SM>and TEST_COUNT in (3,4)
SM>and DATETIME > 39814.0
SM>and SCORE < 30



1) Do you have indices on the join fields?
  

well I'm happily surprised, you came up with this suggestion
- I thought that sqlite created indexes on all primairy key and unique 
fields
- but after explicitly creating the indices, a gained a speed of about a 
factor 10
After checking the database creation, it seemed I forgot to make these 
fields the primary key

so thanks very much.

I gained another factor of 10 speed by updating to version 2.5.5 of 
pysqlite.


cheers,
Stef

2) Look at the ANALYZE command
3) Look at the EXPLAIN command
  


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


Re: sqlite3 performance problems only in python

2009-07-23 Thread Stef Mientki

David Stanek wrote:

On Thu, Jul 23, 2009 at 9:02 AM, Stef Mientki wrote:
  

btw, I don't know if it's of any importance, the SQL-statement I perform is
select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.*
 from OPNAMEN
  inner join POID_VLID  on OPNAMEN.POID= POID_VLID.POID
  inner join VRAAGLST   on VRAAGLST.VLID   = POID_VLID.VLID
  inner join VLID_SSID  on VRAAGLST.VLID   = VLID_SSID.VLID
  inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID
  inner join POID_SSID_SCID on ( OPNAMEN.POID=
POID_SSID_SCID.POID ) and
   ( SUBSCHAAL_GEGEVENS.SSID =
POID_SSID_SCID.SSID )
  inner join SCORES on SCORES.SCID =
POID_SSID_SCID.SCID
  inner join PID_POID   on OPNAMEN.POID= PID_POID.POID
  inner join PATIENTon PATIENT.PID = PID_POID.PID
 where substr ( lower( NAME) , 1, 6)  = 'cis20r'
  and lower ( NAME_ ) = 'fatigue'
  and TEST_COUNT in (3,4)
  and DATETIME > 39814.0
  and SCORE < 30



Warning: I suck at SQL and hate it with a passion...
  

+1,
but I couldn't find anything better.
I guess you have some better suggestions ?
(I looked at Dee, but from my first view, it looks a bit premature)

thanks,
Stef

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


  1   2   >