Re: XML-XSD Processing/Creation.

2008-01-03 Thread Stefan Behnel
paul wrote:
 Can i create python classes based off the XSD files? What else can I
 do with the XSD files? 
 This might be worth looking at: http://www.rexx.com/~dkuhlman/#generateDS

If it's really such a complex XML language, the tool above might or might not
be of any help, as it doesn't support the whole XSD standard (and XML Schema
is very complex). It's worth a try, but don't expect too much.

The lxml way of dealing with XML languages is namespace implementation:

http://codespeak.net/lxml/dev/element_classes.html#id1

However, there isn't currently a way to automatically bootstrap an
implementation, especially not in XSD, so it depends on the language how much
work it will be to get this to a usable state.

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


Re: ElementTree should parse string and file in the same way

2008-01-03 Thread Stefan Behnel
Hi,

Chris Mellon wrote:
 On that note, I really don't like APIs that take either a file name or
 a file object - I can open my own files, thanks.

... and HTTP URLs, and FTP URLs. In lxml, there is a performance difference
between passing an open file (which is read in Python space using the read()
method) and passing a file name or URL, which is passed on to libxml2 (and
thus doesn't require the GIL at parse time). That's only one reason why I like
APIs that allow me to pass anything that points to a file - be it an open file
object, a local file path or a URL - and they just Do The Right Thing with it.

I find that totally pythonic.


 open(fname) is even shorter than StringIO(somedata).

It doesn't serve the same purpose, though.


 My take on the API decision in question was always that a file is
 inherently an XML *document*, while a string is inherently an XML
 *fragment*.

Not inherently, no. I know some people who do web processing with an XML
document coming in as a string (from an HTTP request) and a result XML
document going out as a string. I don't think that's an uncommon use case.

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


shelve and nested dictionaries

2008-01-03 Thread Matthew Schibler
I'm a newbie to Python, with some experience using perl (where I used
nested arrays and hashes extensively). I am building a script in
python for a MUD I play, and I want to use the shelve module to store
persistent information between script executions. The following code
does not work for me,

import shelve, sys, os, string
db = shelve.open(os.path.abspath(os.path.dirname(sys.argv[0])) + '/' +
'sandbox.dat', 'c')
db['JustSomeVariable'] = 'apple'
db['subdb'] = {}
db['subdb']['anotherdict'] = {}
db['subdb']['anotherdict']['bleh'] = 'hello world'
db.close()

of course, that's just a working example but it illustrates the
problem i'm having. I think shelve objects act like dictionaries in a
way, at least they seem to have dictionary keys beneath them. And I
don't seem to have this problem when I use a normal dictionary as
opposed to shelve for nesting other dictionaries.

So i'm now confused, i've hit a brick wall and i'm not sure how to
solve this problem.

Can anyone explain what i'm doing wrong?

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


Re: sending commands in body of HTTP with urllib2

2008-01-03 Thread Astan Chee
Astan Chee wrote:
 Hi,
 Im trying to implement the logic from 
 http://www.hypothetic.org/docs/msn/general/http_connections.php to a 
 simple python code using urllib2 and some parts of urllib. Im behind a 
 http proxy that requires authentication that is why Im using urllib2. Im 
 asking for help on how to send commands in a body of a HTTP before 
 requesting for response. What am I doing wrong? I only get the response 
 from the server but my commands never seem to be acknowledged or 
 properly handled. 
   
I've tried a httplib implementation for it, and now it keeps giving me 
an Error 400 (bad request) / Invalid header name as a response. What am 
I doing wrong? is the server depreciated? or not working like the 
document describes it? Thanks again for any help. Below is my code


import httplib
import base64
import urllib

USER='user'
PASS='pass'
url = 
'http://gateway.messenger.hotmail.com/gateway/gateway.dll?Action=openServer=NSIP=messenger.hotmail.com'

values = 'VER 5 MSNP8 CVR0\r\n'
user_pass = base64.encodestring('%s:%s' % 
(urllib.unquote(USER),urllib.unquote(PASS)))
authheader =  Basic %s % user_pass
proxy_authorization='Proxy-authorization: Basic '+user_pass+'\r\n'

conn = httplib.HTTPConnection(proxy.com.au, 8080)
conn.connect()
conn.putrequest(POST, url)
conn.putheader('Accept','*/*')
conn.putheader('Accept-Language','en-us')
conn.putheader('Accept-Encoding','gzip, deflate')
conn.putheader('User-agent','MSMSGS')
conn.putheader('Host','gateway.messenger.hotmail.com')
conn.putheader('Proxy-Connection','Keep-Alive')
conn.putheader('Pragma','no-cache')
conn.putheader('Content-Type','application/x-msn-messenger')
conn.putheader('content-length',str(len(values)))
conn.putheader('Proxy-authorization',authheader)
conn.endheaders()
conn.send(values)
r = conn.getresponse()
print r.status, r.reason
print r.msg
print r.read()
   

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


Re: shelve and nested dictionaries

2008-01-03 Thread Fredrik Lundh
Matthew Schibler wrote:

 I'm a newbie to Python, with some experience using perl (where I used
 nested arrays and hashes extensively). I am building a script in
 python for a MUD I play, and I want to use the shelve module to store
 persistent information between script executions. The following code
 does not work for me,
 
 import shelve, sys, os, string
 db = shelve.open(os.path.abspath(os.path.dirname(sys.argv[0])) + '/' +
 'sandbox.dat', 'c')
 db['JustSomeVariable'] = 'apple'
 db['subdb'] = {}
 db['subdb']['anotherdict'] = {}
 db['subdb']['anotherdict']['bleh'] = 'hello world'
 db.close()
 
 of course, that's just a working example but it illustrates the
 problem i'm having. I think shelve objects act like dictionaries in a
 way, at least they seem to have dictionary keys beneath them.

the shelve module only tracks changes to the shelf itself (i.e. 
db[key]), not changes to to mutable objects stored in the shelve).

to change a mutable object, you have to fetch it, modify it, and then 
write it back:

 value = db[key]
 ... update value ...
 db[key] = value

in Python 2.3 and later, the shelve can help you with this, to some 
extent; from the help page:

   To avoid the problem with mutable entries, you may pass
   the keyword argument writeback=True in the call to
   shelve.open.  When you use:

   d = shelve.open(filename, writeback=True)

   then d keeps a cache of all entries you access, and writes
   them all back to the persistent mapping when you call
   d.close().  This ensures that such usage as
   d[key].append(anitem) works as intended.

   However, using keyword argument writeback=True may consume
   vast amount of memory for the cache, and it may make
   d.close() very slow, if you access many of d's entries
   after opening it in this way: d has no way to check which
   of the entries you access are mutable and/or which ones
   you actually mutate, so it must cache, and write back at
   close, all of the entries that you access.  You can call
   d.sync() to write back all the entries in the cache, and
   empty the cache (d.sync() also synchronizes the persistent
   dictionary on disk, if feasible).

/F

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


Re: Manually installing PIL

2008-01-03 Thread Fredrik Lundh
Jose Ignacio Gisbert wrote:

 Does somebody install PIL manually??, I mean, copy directories manually 
 without executing setup.py. I saw an identical message from Guirai, but 
 I didn’t see any response. Thanks in advance!

PIL's just a bunch of modules in a single PIL directory; you can put 
that directory (or the modules themselves) wherever you want.

(if you're on windows, unzip the EXE installer to get the files)

/F

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


Re: Extracting files from an ISO image?

2008-01-03 Thread Ant
On Jan 2, 7:07 pm, Rob Williscroft [EMAIL PROTECTED] wrote:
 Ant wrote in news:34a84caa-5387-40a2-a808-
  1) Is there a module out there for extracting files from an ISO?

 There are command line programs that can do this:

  http://cdrecord.berlios.de/old/private/cdrecord.html
...
 One problem you may have is daemon tools will mount cd images that
 aren't iso images, where as isoinfo appears to handle only genuine
 iso file systems.

Cheers for the pointers. I'll try out the isoinfo tool, as my
intention was to extract the images on the fly. If it turns out to be
slow though (or problematic - I assume you mean that images in e.g.
joliet format may not be parsed properly), I'll probably end up
extracting the images and caching them on disk on the addition of new
iso's by simply mounting the image as Grant pointed out.

And it looks like wxPython may have support for icons in an img2py
script, so that may sway me toward wxPython over TkInter.

Cheers,

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


Re: Cloning Environments

2008-01-03 Thread Diez B. Roggisch
gamename wrote:

 Hi,
 
 I have several machines running Linux (mostly fedora6) and Windows
 (mostly XP).  I'm thinking of using easy_install to create as uniform
 an environment as possible for all of them. Cloning the environment,
 to put it another way.
 
 Is there a good example somewhere showing how to do this? I'm new to
 easy_install and relatively new to python.

You might be interested in workingenv.py/virtualenv.py

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


PyObject_CallObject code dump after calling 4 times

2008-01-03 Thread grbgooglefan
I have a following C++ code which uses PyObject_CallObject to evaluate
expressions dynamically. This code sets the input parameters for the
function also dynamically. After calling this function 4 times (with
these shown values), PyObject_CallObject  causes application to crash
in frame_dealloc.
1) Can some one please help me understand why is this crash happening
in frame_dealloc  how to solve it?
2) Is there anything wrong I am doing about incrementing or
decrementing the reference counts of the object passed to
PyObject_CallObject?
3) Is it because of the big value (2299265.50) I am trying to
convert from double to float using PyFloat_FromDouble?

//= code reduced for readability
===
switch(ndtyp){
 case(INT_T):
 {
 printf(PyInt_FromLong val %d, var %s
\n,inputVar.nionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
 val = PyInt_FromLong(inputVar.nionum);
 break;
 }
  case(LONG_T):
 {
 printf(PyLong_FromLong val %ld, var %s
\n,inputVar.lionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
 val = PyLong_FromLong(inputVar.lionum);
 break;
 }
  case(FLOAT_T):
 {
 printf(PyFloat_FromDouble val %f, var %s
\n,inputVar.fionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
 val = PyFloat_FromDouble(inputVar.fionum);
 break;
 }
  case(DOUBLE_T):
 {
 printf(PyFloat_FromDouble val %f, var %s
\n,inputVar.dionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
 val = PyFloat_FromDouble(inputVar.dionum);
 break;
 }
   case(STRING_T):
 {
 printf(PyString_FromString val %s, var %s
\n,inputVar.ioString,pEvalFunc-pExprVarsArray[nCtr].szVarName);
 val = PyString_FromString(inputVar.ioString);
 break;
 }
   default:
 printf(Unknown data type [%d] for %s\n,ndtyp,pEvalFunc-
pExprVarsArray[nCtr].szVarName);
}
if(!val){
   ret = -1;
   printf(Failed to convert %d %s to PyObject\n,ndtyp,pEvalFunc-
pExprVarsArray[nCtr].szVarName); fflush(stdout);
   Py_XDECREF(pTuple);
   break;
}
  PyTuple_SetItem(pTuple, nCtr, val);
  Py_XDECREF(val);
}
// all variables are set, call Python function
Py_XINCREF(pTuple);
  PyObject *pResult = PyObject_CallObject(pEvalFunc-
pPyEvalFunction,pTuple);
Py_XDECREF(pTuple);

if(PyErr_Occurred()){
 PyErr_Print();
} else {
  char* plevel = NULL;
  if(NULL != (plevel = PyString_AsString(pResult))){
ret = 0;
sprintf(szEvalResult,%s,plevel);
  }
}
Py_XDECREF(pResult);

Following crash (back trace) appears when I run this in GDB. The
expression that was getting evaluated at this time is:
def DangerousQuantity(size,maxvol,adv):
 if((size1000 and (maxvol1) and (size0.001*adv))):
   return Dangerous
 else:
   return FAIL

//--- Crash dump information  values of variables passed
to this expression
Categorizing the order
pyParserEvaluator evaluating category function DangerousTactic
PyString_FromString val R, var aestactic
PyLong_FromLong val 1139, var endtime
PyLong_FromLong val 599, var starttime
PyLong_FromLong val 23, var Aggr
PyObject_CallObject done, do Py_XDECREF-pTuple
Final result FAIL
doing Py_XDECREF(pResult
pyParserEvaluator evaluating category function MediumTactic
PyString_FromString val R, var aestactic
PyLong_FromLong val 1139, var endtime
PyLong_FromLong val 599, var starttime
PyLong_FromLong val 23, var Aggr
PyObject_CallObject done, do Py_XDECREF-pTuple
Final result FAIL
doing Py_XDECREF(pResult
pyParserEvaluator evaluating category function SafeTactic
PyString_FromString val R, var aestactic
PyLong_FromLong val 1139, var endtime
PyLong_FromLong val 599, var starttime
PyLong_FromLong val 23, var Aggr
PyObject_CallObject done, do Py_XDECREF-pTuple
Final result FAIL
doing Py_XDECREF(pResult
pyParserEvaluator evaluating category function DangerousQuantity
PyLong_FromLong val 1, var size
PyLong_FromLong val 0, var maxvol
PyFloat_FromDouble val 2299265.50, var adv

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 82336688 (LWP 27652)]
0xc000 in ?? ()
(gdb) where
#0  0xc000 in ?? ()
#1  0x0285e59e in frame_dealloc (f=0xf5a2f68c) at Objects/
frameobject.c:106
#2  0x0281a4b1 in PyEval_EvalCodeEx (co=0xf5a69990, globals=0x2884120,
locals=0x0, args=0x288bca0, argcount=3, kws=0x0, kwcount=0,
defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:2609
#3  0x0285f551 in function_call (func=0xf5a694c4, arg=0xf5a47c3c,
kw=0x0) at Objects/funcobject.c:476
#4  0x027e1e04 in PyObject_Call (func=0xf5a2003c, arg=0xf5a47c3c,
kw=0x0) at Objects/abstract.c:1688
#5  0x0281b3eb in PyEval_CallObjectWithKeywords (func=0xf5a694c4,
arg=0xf5a47c3c, kw=0x0) at Python/ceval.c:3058
#6  0x027e1de3 in PyObject_CallObject (o=0xf5a694c4, a=0xf5a47c3c) at
Objects/abstract.c:1679
#7  0x027dd6fd in pyParserEvaluator::EvaluateCategory

Re: PyObject_CallObject code dump after calling 4 times

2008-01-03 Thread Fredrik Lundh
grbgooglefan wrote:

 I have a following C++ code which uses PyObject_CallObject to evaluate
 expressions dynamically. This code sets the input parameters for the
 function also dynamically. After calling this function 4 times (with
 these shown values), PyObject_CallObject  causes application to crash
 in frame_dealloc.
 1) Can some one please help me understand why is this crash happening
 in frame_dealloc  how to solve it?
 2) Is there anything wrong I am doing about incrementing or
 decrementing the reference counts of the object passed to
 PyObject_CallObject?

if something crashes after a while, it's almost always a reference count 
error.

default:
  printf(Unknown data type [%d] for %s\n,ndtyp,pEvalFunc-
 pExprVarsArray[nCtr].szVarName);
 }

what's val set to if this happens?

 if(!val){
ret = -1;
printf(Failed to convert %d %s to PyObject\n,ndtyp,pEvalFunc-
 pExprVarsArray[nCtr].szVarName); fflush(stdout);
Py_XDECREF(pTuple);
break;
 }
   PyTuple_SetItem(pTuple, nCtr, val);
   Py_XDECREF(val);
 }

PyTuple_SetItem steals a reference

 http://docs.python.org/api/refcountDetails.html

so you shouldn't DECREF val yourself.

 // all variables are set, call Python function
 Py_XINCREF(pTuple);

this isn't necessary, and will (most likely) result in a leak.

   PyObject *pResult = PyObject_CallObject(pEvalFunc-
 pPyEvalFunction,pTuple);
 Py_XDECREF(pTuple);
 
 if(PyErr_Occurred()){
  PyErr_Print();
 } else {
   char* plevel = NULL;
   if(NULL != (plevel = PyString_AsString(pResult))){
 ret = 0;
 sprintf(szEvalResult,%s,plevel);

shouldn't you check the size of plevel here, before copying it to 
szEvalResult?  (and what's wrong with using strcpy to do the copy,
btw?)

/F

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


Re: PyObject_CallObject code dump after calling 4 times

2008-01-03 Thread Phil Thompson
On Thursday 03 January 2008, grbgooglefan wrote:
 I have a following C++ code which uses PyObject_CallObject to evaluate
 expressions dynamically. This code sets the input parameters for the
 function also dynamically. After calling this function 4 times (with
 these shown values), PyObject_CallObject  causes application to crash
 in frame_dealloc.
 1) Can some one please help me understand why is this crash happening
 in frame_dealloc  how to solve it?
 2) Is there anything wrong I am doing about incrementing or
 decrementing the reference counts of the object passed to
 PyObject_CallObject?

Yes.

 3) Is it because of the big value (2299265.50) I am trying to
 convert from double to float using PyFloat_FromDouble?

 //= code reduced for readability
 ===
 switch(ndtyp){
  case(INT_T):
  {
  printf(PyInt_FromLong val %d, var %s
 \n,inputVar.nionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
  val = PyInt_FromLong(inputVar.nionum);
  break;
  }
   case(LONG_T):
  {
  printf(PyLong_FromLong val %ld, var %s
 \n,inputVar.lionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
  val = PyLong_FromLong(inputVar.lionum);
  break;
  }
   case(FLOAT_T):
  {
  printf(PyFloat_FromDouble val %f, var %s
 \n,inputVar.fionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
  val = PyFloat_FromDouble(inputVar.fionum);
  break;
  }
   case(DOUBLE_T):
  {
  printf(PyFloat_FromDouble val %f, var %s
 \n,inputVar.dionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
  val = PyFloat_FromDouble(inputVar.dionum);
  break;
  }
case(STRING_T):
  {
  printf(PyString_FromString val %s, var %s
 \n,inputVar.ioString,pEvalFunc-pExprVarsArray[nCtr].szVarName);
  val = PyString_FromString(inputVar.ioString);
  break;
  }
default:
  printf(Unknown data type [%d] for %s\n,ndtyp,pEvalFunc-

 pExprVarsArray[nCtr].szVarName);

 }
 if(!val){
ret = -1;
printf(Failed to convert %d %s to PyObject\n,ndtyp,pEvalFunc-

 pExprVarsArray[nCtr].szVarName); fflush(stdout);

Py_XDECREF(pTuple);
break;
 }
   PyTuple_SetItem(pTuple, nCtr, val);
   Py_XDECREF(val);

Don't do this - PyTuple_SetItem() steals a reference to val.

 }
 // all variables are set, call Python function
 Py_XINCREF(pTuple);

Why do this?

   PyObject *pResult = PyObject_CallObject(pEvalFunc-

 pPyEvalFunction,pTuple);

 Py_XDECREF(pTuple);

Why do this?

 if(PyErr_Occurred()){
  PyErr_Print();
 } else {
   char* plevel = NULL;
   if(NULL != (plevel = PyString_AsString(pResult))){
 ret = 0;
 sprintf(szEvalResult,%s,plevel);
   }
 }
 Py_XDECREF(pResult);

pTuple will now have the same number of references as when you started the 
above code, so you may want to Py_DECREF() it.

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


Re: ElementTree should parse string and file in the same way

2008-01-03 Thread Fredrik Lundh
Stefan Behnel wrote:

 My take on the API decision in question was always that a file is
 inherently an XML *document*, while a string is inherently an XML
 *fragment*.
 
 Not inherently, no. I know some people who do web processing with an XML
 document coming in as a string (from an HTTP request)  /.../

in which case you probably want to stream the raw XML through the parser 
*as it arrives*, to reduce latency (to do that, either parse from a 
file-like object, or feed data directly to a parser instance, via the 
consumer protocol).

also, putting large documents in a *single* Python string can be quite 
inefficient.  it's often more efficient to use lists of string fragments.

/F

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


Installing pcaplib

2008-01-03 Thread ashish
Hi All,

I am trying to install pylibpcap-0.6.1 but i am getting these errors .


python ./setup.py install

.
.
.
.
.

constants.c:172: (near initialization for `pcapmodule_DLT[52]')
pcap.c: In function `init_pcap':
pcap.c:4246: structure has no member named `value'
pcap.c:4260: warning: passing arg 3 of `PyModule_AddStringConstant' 
discards qualifiers from pointer target type
error: command 'gcc' failed with exit status 1

Please tell me how to solve this problem.Do i have to install anything 
else before installing this library.

Thanks in Advance
Ashish


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


Re: PyObject_CallObject code dump after calling 4 times

2008-01-03 Thread grbgooglefan
On Jan 3, 8:02 pm, Phil Thompson [EMAIL PROTECTED]
wrote:
 On Thursday 03 January 2008, grbgooglefan wrote:

  I have a following C++ code which uses PyObject_CallObject to evaluate
  expressions dynamically. This code sets the input parameters for the
  function also dynamically. After calling this function 4 times (with
  these shown values), PyObject_CallObject  causes application to crash
  in frame_dealloc.
  1) Can some one please help me understand why is this crash happening
  in frame_dealloc  how to solve it?
  2) Is there anything wrong I am doing about incrementing or
  decrementing the reference counts of the object passed to
  PyObject_CallObject?

 Yes.





  3) Is it because of the big value (2299265.50) I am trying to
  convert from double to float using PyFloat_FromDouble?

  //= code reduced for readability
  ===
  switch(ndtyp){
       case(INT_T):
           {
           printf(PyInt_FromLong val %d, var %s
  \n,inputVar.nionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
           val = PyInt_FromLong(inputVar.nionum);
           break;
           }
        case(LONG_T):
           {
           printf(PyLong_FromLong val %ld, var %s
  \n,inputVar.lionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
           val = PyLong_FromLong(inputVar.lionum);
           break;
           }
        case(FLOAT_T):
           {
           printf(PyFloat_FromDouble val %f, var %s
  \n,inputVar.fionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
           val = PyFloat_FromDouble(inputVar.fionum);
           break;
           }
        case(DOUBLE_T):
           {
           printf(PyFloat_FromDouble val %f, var %s
  \n,inputVar.dionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
           val = PyFloat_FromDouble(inputVar.dionum);
           break;
           }
         case(STRING_T):
           {
           printf(PyString_FromString val %s, var %s
  \n,inputVar.ioString,pEvalFunc-pExprVarsArray[nCtr].szVarName);
           val = PyString_FromString(inputVar.ioString);
           break;
           }
         default:
           printf(Unknown data type [%d] for %s\n,ndtyp,pEvalFunc-

  pExprVarsArray[nCtr].szVarName);

  }
  if(!val){
     ret = -1;
     printf(Failed to convert %d %s to PyObject\n,ndtyp,pEvalFunc-

  pExprVarsArray[nCtr].szVarName); fflush(stdout);

     Py_XDECREF(pTuple);
     break;
  }
    PyTuple_SetItem(pTuple, nCtr, val);
    Py_XDECREF(val);

 Don't do this - PyTuple_SetItem() steals a reference to val.

  }
  // all variables are set, call Python function
  Py_XINCREF(pTuple);

 Why do this?

    PyObject *pResult = PyObject_CallObject(pEvalFunc-

  pPyEvalFunction,pTuple);

  Py_XDECREF(pTuple);

 Why do this?

  if(PyErr_Occurred()){
   PyErr_Print();
  } else {
        char* plevel = NULL;
        if(NULL != (plevel = PyString_AsString(pResult))){
          ret = 0;
          sprintf(szEvalResult,%s,plevel);
        }
  }
  Py_XDECREF(pResult);

 pTuple will now have the same number of references as when you started the
 above code, so you may want to Py_DECREF() it.

 Phil- Hide quoted text -

 - Show quoted text -

Thanks for all the responses.
These help me.
I could simulate this crash in my small test program  I think (I
could be wrong also) it is because of extraneous Py_XDECREF of
PyObject *val which I am using to convert variables to tuple.
When I change the code to simple do like this, it works fine.
PyTuple_SetItem(pytuple,0,PyLong_FromLong(size));
PyTuple_SetItem(pytuple,1,PyLong_FromLong(maxvol));
PyTuple_SetItem(pytuple,2,PyFloat_FromDouble(adv));
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cloning Environments

2008-01-03 Thread gamename

 You might be interested in workingenv.py/virtualenv.py

Thanks! That looks promising.
-T
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installing pcaplib

2008-01-03 Thread Diez B. Roggisch
ashish wrote:

 Hi All,
 
 I am trying to install pylibpcap-0.6.1 but i am getting these errors .
 
 
 python ./setup.py install
 
 .
 .
 .
 .
 .
 
 constants.c:172: (near initialization for `pcapmodule_DLT[52]')
 pcap.c: In function `init_pcap':
 pcap.c:4246: structure has no member named `value'
 pcap.c:4260: warning: passing arg 3 of `PyModule_AddStringConstant'
 discards qualifiers from pointer target type
 error: command 'gcc' failed with exit status 1
 
 Please tell me how to solve this problem.Do i have to install anything
 else before installing this library.

Seems like a version-conflict. See which structure pcap.c:4246 refers to,
and from what include it stems. If it's a 3rd-party-lib, install the proper
version.

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


Re: reassign to builtin possible !?

2008-01-03 Thread Diez B. Roggisch
Bernhard Merkle wrote:

 Hi there,
 
 I am reading Learning Python 3e from Mark Lutz and just found out that
 reassigning to builtins is possible.
 What is the reason, why Python allows this ? IMO this is very risky
 and can lead to hard to find errors.
 (see also Learning Python 3e, Chapter 16, Page 315)
 
 True
 True
 False
 False
 True = 1
 True
 1
 True = 0
 True
 0

This hal always been possible. But it's not reassigning, it's shadowing -
which is a totally different beast. Shadowing builtins is bad style, but
lokal to your context. Which can get nasty of course, if you do the above
on e.g. module level.

But you can't alter the values for True/False globally with this.

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


reassign to builtin possible !?

2008-01-03 Thread Bernhard Merkle
Hi there,

I am reading Learning Python 3e from Mark Lutz and just found out that
reassigning to builtins is possible.
What is the reason, why Python allows this ? IMO this is very risky
and can lead to hard to find errors.
(see also Learning Python 3e, Chapter 16, Page 315)

 True
True
 False
False
 True = 1
 True
1
 True = 0
 True
0

TIA,
Berni
-- 
http://mail.python.org/mailman/listinfo/python-list


Treating a unicode string as latin-1

2008-01-03 Thread Simon Willison
Hello,

I'm using ElementTree to parse an XML file which includes some data
encoded as cp1252, for example:

nameBob\x92s Breakfast/name

If this was a regular bytestring, I would convert it to utf8 using the
following:

 print 'Bob\x92s Breakfast'.decode('cp1252').encode('utf8')
Bob's Breakfast

But ElementTree gives me back a unicode string, so I get the following
error:

 print u'Bob\x92s Breakfast'.decode('cp1252').encode('utf8')
Traceback (most recent call last):
  File stdin, line 1, in module
  File /Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/encodings/cp1252.py, line 15, in decode
return codecs.charmap_decode(input,errors,decoding_table)
UnicodeEncodeError: 'ascii' codec can't encode character u'\x92' in
position 3: ordinal not in range(128)

How can I tell Python I know this says it's a unicode string, but I
need you to treat it like a bytestring?

Thanks,

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


Re: reassign to builtin possible !?

2008-01-03 Thread Bernhard Merkle
On Jan 3, 2:07 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote:
 This hal always been possible. But it's not reassigning, it's shadowing -
 which is a totally different beast. Shadowing builtins is bad style, but
 lokal to your context. Which can get nasty of course, if you do the above
 on e.g. module level.

 But you can't alter the values for True/False globally with this.

Are you sure ? what about the following example ?
Is this also shadowing ?

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type help, copyright, credits or license for more information.
 import __builtin__
 __builtin__.True = False
 __builtin__.True
False
 True
False

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


Re: Treating a unicode string as latin-1

2008-01-03 Thread Duncan Booth
Simon Willison [EMAIL PROTECTED] wrote:

 How can I tell Python I know this says it's a unicode string, but I
 need you to treat it like a bytestring?

Can you not just fix your xml file so that it uses the same encoding as it 
claims to use? If the xml says it contains utf8 encoded data then it should 
not contain cp1252 encoded data, period.

If you really must, then try encoding with latin1 and then decoding with 
cp1252:

 print u'Bob\x92s Breakfast'.encode('latin1').decode('cp1252')
Bob’s Breakfast

The latin1 codec will convert unicode characters in the range 0-255 to the 
same single-byte value.
-- 
http://mail.python.org/mailman/listinfo/python-list


Trying to build python2.5.msi

2008-01-03 Thread abhishek
Hello Group,

I have compile the python source code using MSVC 8 (a.k.a VS .NET
2005) .

Can i create an MSI ?? similar to one provided by the official python
website.

What can be the possible procedure to achieve this.

I have looked into Tools/msi folder. But i dont know how it works.

Thank you

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


Re: ElementTree should parse string and file in the same way

2008-01-03 Thread Stefan Behnel
Fredrik Lundh wrote:
 Stefan Behnel wrote:
 
 My take on the API decision in question was always that a file is
 inherently an XML *document*, while a string is inherently an XML
 *fragment*.

 Not inherently, no. I know some people who do web processing with an XML
 document coming in as a string (from an HTTP request)  /.../
 
 in which case you probably want to stream the raw XML through the parser
 *as it arrives*, to reduce latency (to do that, either parse from a
 file-like object, or feed data directly to a parser instance, via the
 consumer protocol).

It depends on the abstraction the web framework provides. If it allows you to
do that, especially in an event driven way, that's obviously the most
efficient implementation (and both ElementTree and lxml support this use
pattern just fine). However, some frameworks just pass the request content
(such as a POSTed document) in a dictionary or as callback parameters, in
which case there's little room for optimisation.


 also, putting large documents in a *single* Python string can be quite
 inefficient.  it's often more efficient to use lists of string fragments.

That's a pretty general statement. Do you mean in terms of reading from that
string (which at least in lxml is a straight forward extraction of a char*/len
pair which is passed into libxml2), constructing that string (possibly from
partial strings, which temporarily *is* expensive) or just keeping the string
in memory?

At least lxml doesn't benefit from iterating over a list of strings and
passing it to libxml2 step-by-step, compared to reading from a straight
in-memory string. Here are some numbers:

$$ cat listtest.py
from lxml import etree

# a list of strings is more memory expensive than a straight string
doc_list = [root] + [atest/a] * 2000 + [/root]
# document construction temporarily ~doubles memory size
doc = .join(doc_list)

def readlist():
tree = etree.fromstringlist(doc_list)

def readdoc():
tree = etree.fromstring(doc)

$$ python -m timeit -s 'from listtest import readlist,readdoc' 'readdoc()'
1000 loops, best of 3: 1.74 msec per loop

$$ python -m timeit -s 'from listtest import readlist,readdoc' 'readlist()'
100 loops, best of 3: 2.46 msec per loop

The performance difference stays somewhere around 20-30% even for larger
documents. So, as expected, there's a trade-off between temporary memory size,
long-term memory size and parser performance here.

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


Re: reassign to builtin possible !?

2008-01-03 Thread Diez B. Roggisch
Bernhard Merkle wrote:

 On Jan 3, 2:07 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote:
 This hal always been possible. But it's not reassigning, it's shadowing -
 which is a totally different beast. Shadowing builtins is bad style, but
 lokal to your context. Which can get nasty of course, if you do the above
 on e.g. module level.

 But you can't alter the values for True/False globally with this.
 
 Are you sure ? what about the following example ?
 Is this also shadowing ?
 
 Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
 (Intel)] on win32
 Type help, copyright, credits or license for more information.
 import __builtin__
 __builtin__.True = False
 __builtin__.True
 False
 True
 False

I'm not entirely sure what happens there, but that seems to only work in the
interactive prompt.

- test.py 


print True

if __builtins__.True == 10:
print I'm reassigned globally
- test.py -

Then, in the interpreter do:

[EMAIL PROTECTED]:/tmp$ python
Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type help, copyright, credits or license for more information.
Welcome to rlcompleter2 0.96
for nice experiences hit tab multiple times
 __builtins__.True = 10
 import test
10
Traceback (most recent call last):
  File stdin, line 1, in module
  File test.py, line 5, in module
if __builtins__.True == 10:
AttributeError: 'dict' object has no attribute 'True'
 


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


Re: Treating a unicode string as latin-1

2008-01-03 Thread Paul Hankin
On Jan 3, 1:31 pm, Simon Willison [EMAIL PROTECTED] wrote:
 How can I tell Python I know this says it's a unicode string, but I
 need you to treat it like a bytestring?

u'Bob\x92s Breakfast'.encode('latin-1')

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


Re: Treating a unicode string as latin-1

2008-01-03 Thread Diez B. Roggisch
Simon Willison wrote:

 Hello,
 
 I'm using ElementTree to parse an XML file which includes some data
 encoded as cp1252, for example:
 
 nameBob\x92s Breakfast/name
 
 If this was a regular bytestring, I would convert it to utf8 using the
 following:
 
 print 'Bob\x92s Breakfast'.decode('cp1252').encode('utf8')
 Bob's Breakfast
 
 But ElementTree gives me back a unicode string, so I get the following
 error:
 
 print u'Bob\x92s Breakfast'.decode('cp1252').encode('utf8')
 Traceback (most recent call last):
   File stdin, line 1, in module
   File /Library/Frameworks/Python.framework/Versions/2.5/lib/
 python2.5/encodings/cp1252.py, line 15, in decode
 return codecs.charmap_decode(input,errors,decoding_table)
 UnicodeEncodeError: 'ascii' codec can't encode character u'\x92' in
 position 3: ordinal not in range(128)
 
 How can I tell Python I know this says it's a unicode string, but I
 need you to treat it like a bytestring?

I don't get your problem. You get a unicode-object. Which means that it got
decoded by ET for you, as any XML-parser must do.

So - why don't you get rid of that .decode('cp1252') and happily encode it
to utf-8?

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


Re: reassign to builtin possible !?

2008-01-03 Thread Jeroen Ruigrok van der Werven
-On [20080103 14:47], Bernhard Merkle ([EMAIL PROTECTED]) wrote:
Are you sure ? what about the following example ?
Is this also shadowing ?

It is, as it is local to your current executing interpreter. Any other Python
process that is currently running is unaffected by your shadowing. So as Diez
says, you are not tampering with it on a persistent global level.

-- 
Jeroen Ruigrok van der Werven asmodai(-at-)in-nomine.org / asmodai
イェルーン ラウフロック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/
Any fool can make a rule. And every fool will mind it...
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: reassign to builtin possible !?

2008-01-03 Thread Tim Chase
 But you can't alter the values for True/False globally with this.
 
 Are you sure ? what about the following example ?
 Is this also shadowing ?
 
 import __builtin__
 __builtin__.True = False
 __builtin__.True
 False

It doesn't seem to screw things up globally

  import __builtin__
  t = __builtin__.True
  __builtin__.True = False
  __builtin__.False = t
  True
False
  False
True
  1 == 1
True
  import os
  os.path.isdir('.')
True
  #if they were globally redefined, this would be False
  #you'd have to actually reference __builtin__.True

My thought would be if you do something as daft as 
redefining/shadowing True and False, you get the headaches that 
ensue.  Fortunately, since Python is explicit, you can trace back 
through the code and see where the inanity occurred. 
Additionally, any scoping rules mean that programmer stupidity 
can't leak too badly outside the scope of the block containing 
the stupidity.

It's the old DIHWIDT! WDDT! (Doctor, it hurts when I do 
this!, well don't do that!) syndrome.

-tkc




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


Re: ElementTree should parse string and file in the same way

2008-01-03 Thread Fredrik Lundh
Stefan Behnel wrote:

 also, putting large documents in a *single* Python string can be quite
 inefficient.  it's often more efficient to use lists of string fragments.
 
 That's a pretty general statement. Do you mean in terms of reading from that
 string (which at least in lxml is a straight forward extraction of a char*/len
 pair which is passed into libxml2), constructing that string (possibly from
 partial strings, which temporarily *is* expensive) or just keeping the string
 in memory?

overall I/O throughput.  it's of course construction and internal 
storage that are the main issues here; every extra copy has a cost, and 
if you're working with multi-megabyte resources, the extra expenses 
quickly become noticeable.

/F

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


CSV

2008-01-03 Thread Beema shafreen
Hi all,

I have written a script to parse a CSV file:
import csv
def get_lines(fname):
fhandle = csv.reader(open(fname,rb))
for line in fhandle:
while fhandle.next()[0] == prot_hit_num:
continue
for row in fhandle:
print  row




result =  get_lines(file.csv)
print result

I need to print the data from prot_hit_num and before the line peptide
sequence.
I am able to print the whole from prot_hit_num to the end of the file but
I need to break before line peptide sequence. How should i do
this.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Treating a unicode string as latin-1

2008-01-03 Thread Jeroen Ruigrok van der Werven
-On [20080103 14:36], Simon Willison ([EMAIL PROTECTED]) wrote:
How can I tell Python I know this says it's a unicode string, but I
need you to treat it like a bytestring?

Although it does not address the exact question it does raise the issue how
you are using ElementTree. When I use the following:

test.xml

entry
  nameBob\x92s Breakfast/name
/entry

parse.py

from xml.etree.ElementTree import ElementTree

xmlfile = open('test.xml')

tree = ElementTree()
tree.parse(xmlfile)
elem = tree.find('name')

print type(elem.text)

I get a string type back and not a unicode string.

However, if you are mixing encodings within the same file, e.g. cp1252 in an
UTF8 encoded file, then you are creating a ton of problems.

-- 
Jeroen Ruigrok van der Werven asmodai(-at-)in-nomine.org / asmodai
イェルーン ラウフロック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/
When moved to complain about others, remember that karma is endless and it
is loving that leads to love...
-- 
http://mail.python.org/mailman/listinfo/python-list

problem with global var

2008-01-03 Thread Bruno Ferreira
Hi,

I wrote a very simple python program to generate a sorted list of
lines from a squid access log file.

Here is a simplified version:

##
1  logfile = open (squid_access.log, r)
2  topsquid = [[0, 0, 0, 0, 0, 0, 0]]
3
4  def add_sorted (list):
5 for i in range(50):
6  if int(list[4])  int(topsquid[i][4]):
7  topsquid.insert(i,list)
8  break
8  # Max len = 50
10 if len(topsquid)  50:
11 topsquid = topsquid[0:50]
12
13 while True:
14 logline = logfile.readline()
15 linefields = logline.split()
16
17 if logline != :
18 add_sorted (linefields)
19 else:
20 break
21
22 for i in range (len(topsquid)):
23 print topsquid[i][4]


When I execute the program _without_ the lines 10 and 11:

10 if len(topsquid)  50:
11 topsquid = topsquid[0:50]

it runs perfectly.

But if I execute the program _with_ those lines, this exception is thrown:

[EMAIL PROTECTED]:~$ python topsquid.py
Traceback (most recent call last):
  File topsquid.py, line 20, in module
add_sorted (linefields)
  File topsquid.py, line 6, in add_sorted
if int(list[4])  int(topsquid[i][4]):
UnboundLocalError: local variable 'topsquid' referenced before assignment


Note that now the error shown is not related with the lines 10 and 11,
but wiht a line prior to them.

Any hints?

-- 
Bruno A. C. Ferreira
Linux Registered User #181386
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pivot Table/Groupby/Sum question

2008-01-03 Thread patrick . waldo
Yes in the sense that the top part will have merged cells so that
Horror and Classics don't need to be repeated every time, but the
headers aren't the important part.  At this point I'm more interested
in organizing the data itself and i can worry about putting it into a
new excel file later.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with global var

2008-01-03 Thread Matt Nordhoff
Bruno Ferreira wrote:
 Hi,
 
 I wrote a very simple python program to generate a sorted list of
 lines from a squid access log file.
 
 Here is a simplified version:
 
 ##
 1  logfile = open (squid_access.log, r)
 2  topsquid = [[0, 0, 0, 0, 0, 0, 0]]
 3
 4  def add_sorted (list):

Don't call your variable list. There's already the built-in type list.

 5 for i in range(50):

You should probably use xrange here.

 6  if int(list[4])  int(topsquid[i][4]):
 7  topsquid.insert(i,list)
 8  break
 8  # Max len = 50
 10 if len(topsquid)  50:
 11 topsquid = topsquid[0:50]

I'd just use [:50], the 0 is implied.

 13 while True:
 14 logline = logfile.readline()
 15 linefields = logline.split()
 16
 17 if logline != :
 18 add_sorted (linefields)
 19 else:
 20 break

for logline in logfile:
if logline:
linefields = logline.split()
add_sorted(linefields)
else:
break

 22 for i in range (len(topsquid)):
 23 print topsquid[i][4]

for i in topsquid:
print i[4]

(You probably want to use a name other than i then.)

 

 When I execute the program _without_ the lines 10 and 11:
 
 10 if len(topsquid)  50:
 11 topsquid = topsquid[0:50]
 
 it runs perfectly.
 
 But if I execute the program _with_ those lines, this exception is thrown:
 
 [EMAIL PROTECTED]:~$ python topsquid.py
 Traceback (most recent call last):
   File topsquid.py, line 20, in module
 add_sorted (linefields)
   File topsquid.py, line 6, in add_sorted
 if int(list[4])  int(topsquid[i][4]):
 UnboundLocalError: local variable 'topsquid' referenced before assignment
 
 
 Note that now the error shown is not related with the lines 10 and 11,
 but wiht a line prior to them.
 
 Any hints?

Basically, you're trying to read the global variable topsquid, and
then you're trying to define a local variable topsquid. Python doesn't
like that. Declare it as global by adding global topsquid to the top
of the function.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Who's to blame?

2008-01-03 Thread Nicola Musatti
Hallo,
First of all I apologize for the longish example at the bottom, but
the biggest source file is automatically generated and I didn't want
to modify more than strictly necessary. Also, it would be shorter if
XML wasn't so verbose ;-)

The following is a wxPython/XRC toy program with a form with a button
which, when pressed, causes a simple dialog to be displayed. The
problem lies in the fact that apparently ShowModal() does not return
when either the Yes or the No buttons are pressed. Curiously, if you
change the Yes and No buttons with the OK and Cancel ones that are
currently commented everything works as expected.

As the sbs_test_xrc.py file below is automatically generated by
wxPython 2.8.6.1's XRCed tool from a XRC file which in turn is
generated by wxFormBuilder (http://wxformbuilder.org/), I really cant
figure out to whom I should report this problem, assuming I'm not
missing some obvious mistake of mine, that is.

Thanks for your help.

Cheers,
Nicola Musatti

# sbs_test.py
import wx
import sbs_test_xrc

class MainFrame(sbs_test_xrc.xrcMainFrame):
def __init__(self, parent):
sbs_test_xrc.xrcMainFrame.__init__(self, parent)
self.button.Bind(wx.EVT_BUTTON, self.OnButton)

def OnButton(self, event=None):
d = sbs_test_xrc.xrcDialog(self)
##if d.ShowModal() == wx.ID_OK:
if d.ShowModal() == wx.ID_YES:
self.Close()

class Application(wx.App):
def OnInit(self):
self.frame = MainFrame(None)
self.frame.Show()
self.SetTopWindow(self.frame)
return True

if __name__ == '__main__':
app = Application()
app.MainLoop()

# sbs_test_xrc.py
# This file was automatically generated by pywxrc, do not edit by
hand.
# -*- coding: UTF-8 -*-

import wx
import wx.xrc as xrc

__res = None

def get_resources():
 This function provides access to the XML resources in this
module.
global __res
if __res == None:
__init_resources()
return __res

class xrcDialog(wx.Dialog):
def PreCreate(self, pre):
 This function is called during the class's initialization.

Override it for custom setup before the window is created
usually to
set additional window styles using SetWindowStyle() and
SetExtraStyle().
pass

def __init__(self, parent):
# Two stage creation (see 
http://wiki.wxpython.org/index.cgi/TwoStageCreation)
pre = wx.PreDialog()
self.PreCreate(pre)
get_resources().LoadOnDialog(pre, parent, Dialog)
self.PostCreate(pre)

# create attributes for the named items in this container
self.wxID_YES = xrc.XRCCTRL(self, wxID_YES)
self.wxID_NO = xrc.XRCCTRL(self, wxID_NO)

class xrcMainFrame(wx.Frame):
def PreCreate(self, pre):
 This function is called during the class's initialization.

Override it for custom setup before the window is created
usually to
set additional window styles using SetWindowStyle() and
SetExtraStyle().
pass

def __init__(self, parent):
# Two stage creation (see 
http://wiki.wxpython.org/index.cgi/TwoStageCreation)
pre = wx.PreFrame()
self.PreCreate(pre)
get_resources().LoadOnFrame(pre, parent, MainFrame)
self.PostCreate(pre)

# create attributes for the named items in this container
self.button = xrc.XRCCTRL(self, button)

#  Resource data --

def __init_resources():
global __res
__res = xrc.EmptyXmlResource()

wx.FileSystem.AddHandler(wx.MemoryFSHandler())

sbs_test_xrc = '''\
?xml version=1.0 ?resource version=2.3.0.1 xmlns=http://
www.wxwindows.org/wxxrc
object class=wxDialog name=Dialog
stylewxDEFAULT_DIALOG_STYLE/style
title/
object class=wxFlexGridSizer
rows2/rows
cols2/cols
vgap0/vgap
hgap0/hgap
growablecols/
growablerows/
object class=sizeritem
option1/option
flagwxEXPAND/flag
border5/border
object class=wxStdDialogButtonSizer
object class=button

flagwxALIGN_CENTER_HORIZONTAL|wxALL/flag
border5/border
!--
object class=wxButton 
name=wxID_OK
labelamp;OK/label
/object
--
object class=wxButton 
name=wxID_YES
labelamp;Yes/label
/object
   

Re: unicode(s, enc).encode(enc) == s ?

2008-01-03 Thread mario
On Jan 2, 9:34 pm, Martin v. Löwis [EMAIL PROTECTED] wrote:
  In any case, it goes well beyond the situation that triggered my
  original question in the first place, that basically was to provide a
  reasonable check on whether round-tripping a string is successful --
  this is in the context of a small utility to guess an encoding and to
  use it to decode a byte string. This utility module was triggered by
  one that Skip Montanaro had written some time ago, but I wanted to add
  and combine several ideas and techniques (and support for my usage
  scenarios) for guessing a string's encoding in one convenient place.

 Notice that this algorithm is not capable of detecting the ISO-2022
 encodings - they look like ASCII to this algorithm. This is by design,
 as the encoding was designed to only use 7-bit bytes, so that you can
 safely transport them in Email and such (*)

Well, one could specify decode_heuristically(s, enc=iso-2022-jp) and
that
encoding will be checked before ascii or any other encoding in the
list.

 If you want to add support for ISO-2022, you should look for escape
 characters, and then check whether the escape sequences are among
 the ISO-2022 ones:
 - ESC (  - 94-character graphic character set, G0
 - ESC )  - 94-character graphic character set, G1
 - ESC *  - 94-character graphic character set, G2
 - ESC +  - 94-character graphic character set, G3
 - ESC -  - 96-character graphic character set, G1
 - ESC .  - 96-character graphic character set, G2
 - ESC /  - 96-character graphic character set, G3
 - ESC $  - Multibyte
( G0
) G1
* G2
+ G3
 - ESC %   - Non-ISO-2022 (e.g. UTF-8)

 If you see any of these, it should be ISO-2022; see
 the Wiki page as to what subset may be in use.

 G0..G3 means what register the character set is loaded
 into; when you have loaded a character set into a register,
 you can switch between registers through ^N (to G1),
 ^O (to G0), ESC n (to G2), ESC o (to G3) (*)

OK, suppose we do not know the string is likely to be iso-2022, but we
still want to detect it if it is. I have added a may_do_better
mechanism to the algorithm, to add special checks on a *guessed*
algorithm. I am not sure this will not however introduce more or other
problems than the one it is addressing...

I have re-instated checks for iso-8859-1 control chars (likely to be
cp1252), for special symbols in iso-8859-15 when they occur in
iso-8859-1 and cp1252, and for the iso-2022-jp escape sequences. To
flesh out with other checks is mechanical work...

If you could take a look at the updated page:

 http://gizmojo.org/code/decodeh/

I still have issues with what happens in situations when for example a
file contains iso-2022  esc sequences but is anyway actally in ascii
or utf-8? e.g. this mail message! I'll let this issue turn for a
little while...

  I will be very interested in any remarks any of you may have!

 From a shallow inspection, it looks right. I would have spelled
 losses as loses.

Yes, corrected.

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


Re: unicode(s, enc).encode(enc) == s ?

2008-01-03 Thread mario
Thanks again. I will chunk my responses as your message has too much
in it for me to process all at once...

On Jan 2, 9:34 pm, Martin v. Löwis [EMAIL PROTECTED] wrote:
  Thanks a lot Martin and Marc for the really great explanations! I was
  wondering if it would be reasonable to imagine a utility that will
  determine whether, for a given encoding, two byte strings would be
  equivalent.

 But that is much easier to answer:

   s1.decode(enc) == s2.decode(enc)

 Assuming Unicode's unification, for a single encoding, this should
 produce correct results in all cases I'm aware of.

 If the you also have different encodings, you should add

   def normal_decode(s, enc):
   return unicode.normalize(NFKD, s.decode(enc))

   normal_decode(s1, enc) == normal_decode(s2, enc)

 This would flatten out compatibility characters, and ambiguities
 left in Unicode itself.

Hmmn, true, it would be that easy.

I am now not sure why I needed that check, or how to use this version
of it... I am always starting from one string, and decoding it... that
may be lossy when that is re-encoded, and compared to original.
However it is clear that the test above should always pass in this
case, so doing it seems superfluos.

Thanks for the unicodedata.normalize() tip.

mario

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


Re: problem with global var

2008-01-03 Thread wes
Bruno Ferreira wrote:
 Hi,
 
 I wrote a very simple python program to generate a sorted list of
 lines from a squid access log file.
 
 Here is a simplified version:
 
 ##
 1  logfile = open (squid_access.log, r)
 2  topsquid = [[0, 0, 0, 0, 0, 0, 0]]
 3
 4  def add_sorted (list):
 global topsquid
 5 for i in range(50):
 6  if int(list[4])  int(topsquid[i][4]):
 7  topsquid.insert(i,list)
 8  break
 8  # Max len = 50
 10 if len(topsquid)  50:
 11 topsquid = topsquid[0:50]
 12
 13 while True:
 14 logline = logfile.readline()
 15 linefields = logline.split()
 16
 17 if logline != :
 18 add_sorted (linefields)
 19 else:
 20 break
 21
 22 for i in range (len(topsquid)):
 23 print topsquid[i][4]
 
 
 When I execute the program _without_ the lines 10 and 11:
 
 10 if len(topsquid)  50:
 11 topsquid = topsquid[0:50]
 
 it runs perfectly.
 
 But if I execute the program _with_ those lines, this exception is thrown:
 
 [EMAIL PROTECTED]:~$ python topsquid.py
 Traceback (most recent call last):
   File topsquid.py, line 20, in module
 add_sorted (linefields)
   File topsquid.py, line 6, in add_sorted
 if int(list[4])  int(topsquid[i][4]):
 UnboundLocalError: local variable 'topsquid' referenced before assignment
 
 
 Note that now the error shown is not related with the lines 10 and 11,
 but wiht a line prior to them.
 
 Any hints?
 

Try line 4 add.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with global var

2008-01-03 Thread Diez B. Roggisch
Bruno Ferreira wrote:

 Hi,
 
 I wrote a very simple python program to generate a sorted list of
 lines from a squid access log file.
 
 Here is a simplified version:
 
 ##
 1  logfile = open (squid_access.log, r)
 2  topsquid = [[0, 0, 0, 0, 0, 0, 0]]
 3
 4  def add_sorted (list):
 5 for i in range(50):
 6  if int(list[4])  int(topsquid[i][4]):
 7  topsquid.insert(i,list)
 8  break
 8  # Max len = 50
 10 if len(topsquid)  50:
 11 topsquid = topsquid[0:50]
 12
 13 while True:
 14 logline = logfile.readline()
 15 linefields = logline.split()
 16
 17 if logline != :
 18 add_sorted (linefields)
 19 else:
 20 break
 21
 22 for i in range (len(topsquid)):
 23 print topsquid[i][4]
 
 
 When I execute the program _without_ the lines 10 and 11:
 
 10 if len(topsquid)  50:
 11 topsquid = topsquid[0:50]
 
 it runs perfectly.
 
 But if I execute the program _with_ those lines, this exception is thrown:
 
 [EMAIL PROTECTED]:~$ python topsquid.py
 Traceback (most recent call last):
   File topsquid.py, line 20, in module
 add_sorted (linefields)
   File topsquid.py, line 6, in add_sorted
 if int(list[4])  int(topsquid[i][4]):
 UnboundLocalError: local variable 'topsquid' referenced before assignment
 
 
 Note that now the error shown is not related with the lines 10 and 11,
 but wiht a line prior to them.
 
 Any hints?

Use 

def add_sorted(list):
   global topsquid
   ...

to make topsquid a global variable to add_sorted. Otherwise python sees that
it gets referred by in the if-statement before assigning to it, thus
resulting in the error you see. 

The reason for this is that a (limited) static analysis of python-code is
performed to determine which variables are local to a function and which
not. The criteria essentially is the appearance on the left-hand-side of an
expression makes a variable (or name) local to that function. Which makes
it require the explicit global declaration.

Apart from that there are quite a few things worth mentioning in your code:

 - don't shadow built-in names like list

 - it's superfluous to do

for i in xrange(len(some_list)):
.. some_list[i] ..

as you do, unless you need the index. Instead do

for element in some_list:
   ... element ...

If you need an index, do 

for i, element in enumerate(some_list):
   ...

 - don't use range, use xrange if you don't need a list but rather 
   want to enumerate indices.

 - the while-loop is superfluous as well, just do

for line in logfile:
  ...

or if your python is older do

for line in logfile.xreadlines():
  ...

Diez

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


Re: Treating a unicode string as latin-1

2008-01-03 Thread Fredrik Lundh
Simon Willison wrote:

 But ElementTree gives me back a unicode string, so I get the following
 error:
 
 print u'Bob\x92s Breakfast'.decode('cp1252').encode('utf8')
 Traceback (most recent call last):
   File stdin, line 1, in module
   File /Library/Frameworks/Python.framework/Versions/2.5/lib/
 python2.5/encodings/cp1252.py, line 15, in decode
 return codecs.charmap_decode(input,errors,decoding_table)
 UnicodeEncodeError: 'ascii' codec can't encode character u'\x92' in
 position 3: ordinal not in range(128)
 
 How can I tell Python I know this says it's a unicode string, but I
 need you to treat it like a bytestring?

ET has already decoded the CP1252 data for you.  If you want UTF-8, all 
you need to do is to encode it:

  u'Bob\x92s Breakfast'.encode('utf8')
'Bob\xc2\x92s Breakfast'

/F

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


Re: urllib2 disable proxy

2008-01-03 Thread Dimitrios Apostolou


On Wed, 2 Jan 2008, Rob Wolfe wrote:

 Dimitrios Apostolou [EMAIL PROTECTED] writes:

 Hello list,

 I've been looking for a way to explicitly disable the use of proxies with
 urllib2, no matter what the environment dictates. Unfortunately I can't find
 a way in the documentation, and reading the source leads me to believe that
 something like the following does the job:

 req.set_proxy(None,None)

 Where req is a urllib2.Request instance. So is there an official way of doing
 this? Perhaps it should be added in the documentation?

 I believe that the recommended way is to use `urllib2.ProxyHandler`.
 Take a look at:
 http://www.voidspace.org.uk/python/articles/urllib2.shtml

Thanks for the pointer, I will use that way. However it seems rather 
non-elegant way to do something so simple and I was hoping not to mess 
with ProxyHandler, especially since I want *no* proxy... IMHO something 
like the following would be more elegant:

req.set_proxy('','http')

or

req.set_proxy(None,'http')


However these ways *don't* work. You think I should file a feature request 
somewhere or send this to the python-dev list?


Thank you for the help,
Dimitris



 HTH,
 Rob
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: problem with global var

2008-01-03 Thread Fredrik Lundh
Bruno Ferreira wrote:

 When I execute the program _without_ the lines 10 and 11:
 
 10 if len(topsquid)  50:
 11 topsquid = topsquid[0:50]
 
 it runs perfectly.
 
 But if I execute the program _with_ those lines, this exception is thrown:
 
 [EMAIL PROTECTED]:~$ python topsquid.py
 Traceback (most recent call last):
   File topsquid.py, line 20, in module
 add_sorted (linefields)
   File topsquid.py, line 6, in add_sorted
 if int(list[4])  int(topsquid[i][4]):
 UnboundLocalError: local variable 'topsquid' referenced before assignment

Python uses static analysis to determine if a variable is local to a 
function; somewhat simplified, if you assign to the variable inside the 
function, *all* uses of that variable inside the function will be 
considered local.  for the full story, see:

 http://docs.python.org/ref/naming.html

to fix this, you can insert a global declaration at the top of the

 def add_sorted (list):
 global topsquid # mark topsquid as global in this function
 ...

in this case, you can also avoid the local assignment by modifying the 
list in place;

 if len(topsquid)  50:
 topsquid[:] = topsquid[0:50]

or, as a one-liner:

 del topsquid[50:]

/F

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


Re: Choosing a new language

2008-01-03 Thread Joachim Durchholz
Tim Roberts schrieb:
 Joachim Durchholz [EMAIL PROTECTED] wrote:
 
 Xah Lee [EMAIL PROTECTED] wrote:
 [...] PHP and Perl are practically identical in their
 high-levelness or expressiveness or field of application (and
 syntax),
 That must have been a very, very distant point of view with narrowly 
 squinted eyes.
 
 Do you really think so?  It seems clear to me that the syntax of PHP was
 heavily influenced by Perl.  PHP lacks the @array and %hash weirdnesses,
 but most PHP code will work just fine as Perl.

Quite unlikely.
It won't even parse. PHP code starts with ?php, which is AFAIK not 
valid Perl. (Anything before ?php will be copied out to stdout, which, 
again, isn't Perl semantics. Anything between ? and the next ?php will 
be copied through, too.)

I'm not sure whether a PHP function definition would parse in Perl or 
not. It's possible that it may - but then, I don't think it will run 
unless you use a humongous compatibility library.

Taking another step back, Perl has namespaces and can access shared 
libraries directly. PHP has no namespaces, and you need to recompile it 
to access yet another shared lib.
Libraries are very, very different. The thing that I last stumbled over 
is that both have an extremely different approach to FastCGI: PHP 
strives to isolate the programmer from it, Perl gives the direct 
approach. (This is a philosophical difference. PHP library functions 
tend to cover up for platform differences, Perl gives you the 
definitions needed to detect and handle differences.)

If you take another step back, yes both languages are procedural, 
interpreted languages and use $ in front of every variable.

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


how to use bool

2008-01-03 Thread jimgardener
hi, i have some code where i set a bool type variable and if the value
is false i would like to return from the method with an error msg..
being a beginner I wd like some help here

class myclass:
 .
def  mymethod(self):
 success=True
 msg=all validation OK
 success=validateSthing()
 if(success==False):
   msg=sthing failed
   return (success,msg)

 dosomeprocessing()
 .
 success=validateSthingelse()
 if(success==False):
   msg=sthingelse  failed
   return (success,msg)
 domoreprocessing()
  
   return(success,msg)

i would like to know if this way of doing this is OK..I have need of
many kinds of validations in this ..is there a better way of doing
this ?

thank you

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


Re: how to use bool

2008-01-03 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

 hi, i have some code where i set a bool type variable and if the value
 is false i would like to return from the method with an error msg..
 being a beginner I wd like some help here
 
 class myclass:
  .
 def  mymethod(self):
  success=True
  msg=all validation OK
  success=validateSthing()
  if(success==False):
msg=sthing failed
return (success,msg)
 
  dosomeprocessing()
  .
  success=validateSthingelse()
  if(success==False):
msg=sthingelse  failed
return (success,msg)
  domoreprocessing()
   
return(success,msg)
 
 i would like to know if this way of doing this is OK..I have need of
 many kinds of validations in this ..is there a better way of doing
 this ?

to test boolean values, it's usually better to use plain if or if 
not statements:

 if success:
 ... handle success here ...

 if not success:
 ... handle failure here ...

to report failures, use exceptions (the raise and try/except 
statements).  see the tutorial for more on this topic.

/F

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


Re: Insert to a clob field using cx_Oracle via a stored procedure

2008-01-03 Thread hinds . ja
On Jan 2, 2:01 pm, [EMAIL PROTECTED] wrote:
 Hello,

 Does anyone have experience using cx_Oracle to call a stored procedure
 that inserts to a clob field?  We have done this successfully via
 straight SQL, but we are at a loss on how to do the same insert using
 a stored procedure call.  Any assistance would be much appreciated.
 Thanks.

 Jason

Found a solution to this - see the following thread if you interested.

http://forums.oracle.com/forums/thread.jspa?forumID=376threadID=601700
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Treating a unicode string as latin-1

2008-01-03 Thread Duncan Booth
Fredrik Lundh [EMAIL PROTECTED] wrote:

 ET has already decoded the CP1252 data for you.  If you want UTF-8, all 
 you need to do is to encode it:
 
  u'Bob\x92s Breakfast'.encode('utf8')
 'Bob\xc2\x92s Breakfast'
 
I think he is claiming that the encoding information in the file is 
incorrect and therefore it has been decoded incorrectly.

I would think it more likely that he wants to end up with u'Bob\u2019s 
Breakfast' rather than u'Bob\x92s Breakfast' although u'Dog\u2019s dinner' 
seems a probable consequence.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to use bool

2008-01-03 Thread tinnews
[EMAIL PROTECTED] wrote:
 hi, i have some code where i set a bool type variable and if the value
 is false i would like to return from the method with an error msg..
 being a beginner I wd like some help here
 
 class myclass:
  .
 def  mymethod(self):
  success=True
  msg=all validation OK
  success=validateSthing()
  if(success==False):
msg=sthing failed
return (success,msg)
 
  dosomeprocessing()
  .
  success=validateSthingelse()
  if(success==False):
msg=sthingelse  failed
return (success,msg)
  domoreprocessing()
   
return(success,msg)
 
 i would like to know if this way of doing this is OK..I have need of
 many kinds of validations in this ..is there a better way of doing
 this ?
 
With my philosophical programming hat on the first thing I'd say (as a
fairly beginning python programmer) is avoid multiple returns from a
function/method if at all possible.  They breed all sorts of problems
and errors, in particular if there's any clearing up to do you have to
do it in lots of places (or you forget it in some places).

So:-

def  mymethod(self):
msg=sthing failed
success=validateSthing()
if success:
dosomeprocessing()
.
success=validateSthingelse()
if success:
domoreprocessing()

msg=all validation OK
return (success,msg)

I've lost the different messages for different errors but you get the
idea.


if success: rather than if (success==True), more readable.  For
the opposite if not success:.



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


How do you pass compiler option to setup.py install?

2008-01-03 Thread Emin.shopper Martinian.shopper
Dear Experts,

How do you pass the -c option to setup.py install? Specifically, when I try
to install zope.interfaces version 3.3 from source on a windows machine, I
get a message about using -c mingw32. That works fine for setup.py build,
but it does not work for setup.py install.

Note: I would have just used the binary installer for windows but couldn't
find one for version 3.3.

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

Re: How do you pass compiler option to setup.py install?

2008-01-03 Thread Christian Heimes
Emin.shopper Martinian.shopper wrote:
 Dear Experts,
 
 How do you pass the -c option to setup.py install? Specifically, when I try
 to install zope.interfaces version 3.3 from source on a windows machine, I
 get a message about using -c mingw32. That works fine for setup.py build,
 but it does not work for setup.py install.

python setup.py build -c mingw32 install

You can also change the distutils.cfg file to set mingw32 as the default
compiler. Please refer to the documentation for more information.

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


Re: How do you pass compiler option to setup.py install?

2008-01-03 Thread Emin.shopper Martinian.shopper
On Jan 3, 2008 11:24 AM, Emin.shopper Martinian.shopper 
[EMAIL PROTECTED] wrote:

 How do you pass the -c option to setup.py install?


After some fiddling, I figured out that you can put the following two lines
in setup.cfg:

[build]
compiler=mingw32

It would be nice if you could somehow pass this on the command line or if
some of the help messages mentioned it
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Fate of itertools.dropwhile() and itertools.takewhile()

2008-01-03 Thread [EMAIL PROTECTED]
On Dec 29 2007, 11:10 pm, Raymond Hettinger [EMAIL PROTECTED] wrote:
 I'm considering deprecating these two functions and would like some
 feedback from the community or from people who have a background in
 functional programming.

Well I have just this minute used dropwhile in anger, to find the next
suitable filename when writing database dumps using date.count names:

filename = %02d-%02d-%d % (now.day, now.month, now.year)
if os.path.exists(filename):
candidates = (%s.%d % (filename, x) for x in count(1))
filename = dropwhile(os.path.exists, candidates).next()

Much clearer than the alternatives I think, please keep dropwhile and
takewhile in itertools ;)

Cheers,

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


Adding a HTTP header to a SOAPpy request

2008-01-03 Thread Matias Surdi
Hi,

Could anybody tell me which is the easier way to do a SOAP call to a web 
service wich requires an http header to be present?

I can't figure it out.

Thanks a lot

Some code I'm using:



import SOAPpy
s = 
SOAPpy.SOAPProxy(http://10.3.5.128:10560/SERVICES,namespace=http://ws.mysite.com;)

s.some_method()




Thanks a lot.

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


Re: how to use bool

2008-01-03 Thread Chris Mellon
On 03 Jan 2008 16:09:53 GMT,  [EMAIL PROTECTED] wrote:

 [EMAIL PROTECTED] wrote:
  hi, i have some code where i set a bool type variable and if the value
  is false i would like to return from the method with an error msg..
  being a beginner I wd like some help here
 
  class myclass:
   .
  def  mymethod(self):
   success=True
   msg=all validation OK
   success=validateSthing()
   if(success==False):
 msg=sthing failed
 return (success,msg)
 
   dosomeprocessing()
   .
   success=validateSthingelse()
   if(success==False):
 msg=sthingelse  failed
 return (success,msg)
   domoreprocessing()

 return(success,msg)
 
  i would like to know if this way of doing this is OK..I have need of
  many kinds of validations in this ..is there a better way of doing
  this ?
 
 With my philosophical programming hat on the first thing I'd say (as a
 fairly beginning python programmer) is avoid multiple returns from a
 function/method if at all possible.  They breed all sorts of problems
 and errors, in particular if there's any clearing up to do you have to
 do it in lots of places (or you forget it in some places).


This advice is highly controversial, and in the presence of exceptions
it is, at best, voodoo coding. Since your function can exit at any
point whether you do it intentionally or not, if you have crucial
cleanup it's best to write your code in a way that does it correctly
even if you return early. Following this style also often leads to odd
contortions, like extra layers of indentation, and a proliferation of
temporary flags and value-holders that aren't necessary if you write
the code in a more straight forward manner.

Make your decisions on a case by case basis of complexity,
readability, and reliability instead of following pronouncements from
on high (especially decades old pronouncements made in a different
context). Forcing a single return site in the code below adds
complexity, arguable harms readability, and provides *zero* benefit in
the code at hand.

 So:-

 def  mymethod(self):
 msg=sthing failed
 success=validateSthing()
 if success:
 dosomeprocessing()
 .
 success=validateSthingelse()
 if success:
 domoreprocessing()
 
 msg=all validation OK
 return (success,msg)

 I've lost the different messages for different errors but you get the
 idea.


 if success: rather than if (success==True), more readable.  For
 the opposite if not success:.



 --
 Chris Green

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

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


Re: Who's to blame?

2008-01-03 Thread Rob Williscroft
Nicola Musatti wrote in news:92dfc2fc-0677-43c0-b34f-4f240fa40205
@e4g2000hsg.googlegroups.com in comp.lang.python:

Note there is a wxpython mailinglist/newsgroup:

news:gmane.comp.python.wxpython

[snip]
 problem lies in the fact that apparently ShowModal() does not return
 when either the Yes or the No buttons are pressed. Curiously, if you
 change the Yes and No buttons with the OK and Cancel ones that are
 currently commented everything works as expected.

This is because the wx.Dialog class has handlers for the wxID_OK
and wxID_CANCEL button identifiers, IIRC in windows the MS supplied
dialog procedure behaves this way.

 
 As the sbs_test_xrc.py file below is automatically generated by
 wxPython 2.8.6.1's XRCed tool from a XRC file which in turn is
 generated by wxFormBuilder (http://wxformbuilder.org/), I really cant
 figure out to whom I should report this problem, assuming I'm not
 missing some obvious mistake of mine, that is.

 class MainFrame(sbs_test_xrc.xrcMainFrame):
 def __init__(self, parent):
 sbs_test_xrc.xrcMainFrame.__init__(self, parent)
 self.button.Bind(wx.EVT_BUTTON, self.OnButton)

First you can make the dialog a member, you are showing and hiding it
so there is no need to create a new one every time OnButton is fired.

  self.dialog = sbs_test_xrc.xrcDialog(self)

  # now replace the defaults of ID_OK and ID_CANCEL
  #
  self.dialog.SetAffirmativeId( wxID_YES )
  self.dialog.SetEscapeId( wxID_NO )

Alternativly you could derive from xrcDialog as you are with
xrcMainFrame and do the above in the derived classes __init__.

 def OnButton(self, event=None):
 d = sbs_test_xrc.xrcDialog(self)
 ##if d.ShowModal() == wx.ID_OK:
 if d.ShowModal() == wx.ID_YES:
 self.Close()
 

http://www.wxpython.org/docs/api/wx.Dialog-class.html

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


C++ equivalent of comp.lang.python?

2008-01-03 Thread brzrkr0
Hopefully this isn't too OT.

One thing I like about comp.lang.python is the breadth of topics
discussed here.  People can ask about Python installation and
configuration issues on specific platforms, compare third party
libraries, ask for book recommendations, and discuss current Python
projects.  Lurking here has greatly increased my understanding of
Python over the last year or so.

I also do a lot of C++ development, but I've never found a similar
discussion group for that language.  comp.lang.c++ isn't what I'm
looking for.  I find it hard to get practical advice on that group
because its focus is so narrow.  I frequently see posters there
redirect people to one of the OS-specific C++ groups, but most of my
projects are cross-platform, so hanging out on one of those doesn't
make sense either.  As an example, I was recently trying to get
information about writing cross-platform code for dynamic linking, but
I couldn't find anywhere appropriate to ask about it.

For those of you who work in C++, where do you go to discuss it
online?  I'm interested in any newsgroups, mailing lists, or web
boards you can recommend.

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


[OT] How is AI implemented

2008-01-03 Thread Martin Marcher
Hi,

I know it's not a trivial field but I had some readings about
artificial intelligence lately and my personal conclusion is that it's
mostly just statistics.

Naively explained:

continiously gather and store information and apply a default rating

1) answer questions with gathered information according to rating
2) store/calculate rating based upon input (be it an interface or user input)
3) goto 1 (sorry for the goto)

So I think that in general there hasn't yet been any artificial
intelligence programmed (Note: I believe I'm aware of the difference
between artificial intelligence and artificial conscusiness where the
second would be able to answer things like: How are you today and
the first can answer factual knowledge)

Am I thinking right here or is there some (preferrably) web reading
available on that or in depth links about the topic?

thanks and sorry for OT posting
martin

-- 
http://noneisyours.marcher.name
http://feeds.feedburner.com/NoneIsYours
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pivot Table/Groupby/Sum question

2008-01-03 Thread petr . jakes . tpc
On Jan 3, 3:41 pm, [EMAIL PROTECTED] wrote:
 Yes in the sense that the top part will have merged cells so that
 Horror and Classics don't need to be repeated every time, but the
 headers aren't the important part.  At this point I'm more interested
 in organizing the data itself and i can worry about putting it into a
 new excel file later.

What I am able to do is to use SQL, I think it is quite simple, about
50 rows of code including sample data and comments. It works for me
and IMHO it is easy tu understand and I think you can use as it is.
Otherwise you can control Excel from Python using win32api, win32com
( http://tinyurl.com/2m3x3v )

HTH

Petr Jakes

#!/usr/bin/env python
# -*- coding: cp1250 -*-

import sqlite3
con = sqlite3.connect(:memory:)
cur = con.cursor()

normalizedInputData=[]
subCategories=[]

rawData = [['Italy', 'Horror', '70s', 'Suspiria','Dario Argento', 4],
['Italy', 'Classics', 'Neo-Realist', 'Otto eMezzo', 'Fellini',
3],
['GB', 'Classics', 'Neo-Humoristic', 'Otto eMezzo', 'Fellini',
3],
['Fr', 'Horror', 'Neo-Realist', 'Otto eMezzo', 'Fellini', 8],
['Fr', 'Classics', 'Neo-Realist', 'Otto eMezzo', 'Fellini',
55],
['GB', 'Horror', 'Neo-Realist', 'Otto eMezzo', 'Fellini', 5],
['Italy', 'Horror', '70s', 'Profondo Rosso','Dario Argento',
4]]

def alphanum(s):
only letters, numbers and '_' are acceptable for column names
by SQL
filtered=''
for ch in s:
if ch.isalnum() or ch in '_':
filtered+=ch
return filtered

for myRow in rawData :
cat_SubCat = alphanum(_.join(myRow[1:3]))
if cat_SubCat not in subCategories:
subCategories.append(cat_SubCat)
myRow[1:3] = [cat_SubCat]
normalizedInputData.append(myRow)

def data_generator(dataSet):
for dataSetRow in dataSet:
 yield dataSetRow

subCategories=sorted(subCategories)

# create SQL table named MOVIES with the apropriate fields (the tabe
is store in the memory only)
cur.execute(create table MOVIES(COUNTRY, CATEGORY, TITLE, DIRECTOR,
QUANTITY))

# fill the table with data
cur.executemany(insert into MOVIES(COUNTRY, CATEGORY, TITLE,
DIRECTOR, QUANTITY) values (?,?,?,?,?),
data_generator(normalizedInputData))

# assemble dynamic SQL SELECT query, which returns PIVOT TABLE
prologue = select COUNTRY, SUM(QUANTITY) AS TOTAL, 
template = SUM (CASE CATEGORY WHEN '%s' THEN QUANTITY ELSE 0 END) %s
epilogue =  FROM MOVIES GROUP BY 1 ORDER BY 1
pivotSelect = prologue + , .join([template % (x, x) for x in
subCategories]) + epilogue

# execute SQL SELECT and return data row by row
cur.execute(pivotSelect)
for row in cur.fetchall():
print row
-- 
http://mail.python.org/mailman/listinfo/python-list


PyOpenGL, wxPython weird behaviour

2008-01-03 Thread Adeola Bannis
Hi everyone,

I'm doing a project using wxPython and pyopengl, and I seem to have a
problem rendering textures. This is code that worked before my hard
drive had a meltdown, but not since I re-installed everything.

I've determined the problem is in the OpenGL part of my program. I do
some calculations to generate a 2D numpy array that holds the image
data, and pylab.imshow() shows me the image as it is meant to be. I
used the same algorithm in Octave and MATLAB, and all are giving me
the right picture.

However, using pyOpenGL and the numpyhandler functions (http://cours-
info.iut-bm.univ-fcomte.fr/docs/python/OpenGL/
OpenGL.arrays.numpymodule.NumpyHandler-class.html) doesn't seem to
work. I get a garbled screen pocked with black pixels. I am including
my openGL code below. What am I doing wrong?

And yes, I did make the dtype of my array 'float32'.

---code snippets--

import wx
from wx.glcanvas import GLCanvas

from OpenGL.GLU import *
from OpenGL.GL import *
from OpenGL.arrays.numpymodule import NumpyHandler


PC = 1
RI = 0

class myGLCanvas(GLCanvas):
def __init__(self, parent):
GLCanvas.__init__(self, parent,-1)
wx.EVT_PAINT(self, self.OnPaint)
self.init = 0
self.mode = -1
# making a texture for the range image
self.texture = glGenTextures(1)
# making a spot for the point cloud points
self.cloud = None
return

def OnPaint(self,event):
dc = wx.PaintDC(self)
self.SetCurrent()
if not self.init:
self.InitGL()
self.init = 1
self.OnDraw()
return

def OnDraw(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
if self.mode == RI:
self.drawRange()
elif self.mode == PC:
self.drawCloud()


def InitGL(self):
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glClear(GL_COLOR_BUFFER_BIT)

glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glPixelStorei(GL_PACK_ALIGNMENT, 1)

#NTSC colour scales...
glPixelTransferf(GL_RED_SCALE, 0.299);
glPixelTransferf(GL_GREEN_SCALE, 0.587);
glPixelTransferf(GL_BLUE_SCALE, 0.114);

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0,1.0,0,1.0,-1.0,1.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

return

def rangeImage(self, image):

glBindTexture(GL_TEXTURE_2D, self.texture)
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE )


glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR)
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
GL_LINEAR)
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

# flatten it into a list so the OpenGL calls work
n = NumpyHandler()
fI = image.flatten()
flatImage = n.dataPointer(n.contiguous(fI))

print n.contiguous(fI)

gluBuild2DMipmaps(GL_TEXTURE_2D, 1, image.shape[0]+1,
image.shape[1]+1,
GL_LUMINANCE, GL_FLOAT, 
flatImage)
self.mode = RI
self.OnDraw()


def drawRange(self):
''' Controls the actual drawing of the range image'''

glMatrixMode(GL_MODELVIEW)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glColor3f(1.0,1.0,1.0)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture)
glBegin(GL_TRIANGLE_FAN)
glTexCoord2d(1,1); glVertex3f(0.0, 0.0, 0.0)
glTexCoord2d(1,0); glVertex3f(0.0, 1.0, 0.0)
glTexCoord2d(0,0); glVertex3f(1.0, 1.0, 0.0)
glTexCoord2d(0,1); glVertex3f(1.0, 0.0, 0.0)
glEnd()
self.SwapBuffers()

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


Re: Treating a unicode string as latin-1

2008-01-03 Thread Diez B. Roggisch
Duncan Booth schrieb:
 Fredrik Lundh [EMAIL PROTECTED] wrote:
 
 ET has already decoded the CP1252 data for you.  If you want UTF-8, all 
 you need to do is to encode it:

 u'Bob\x92s Breakfast'.encode('utf8')
 'Bob\xc2\x92s Breakfast'

 I think he is claiming that the encoding information in the file is 
 incorrect and therefore it has been decoded incorrectly.
 
 I would think it more likely that he wants to end up with u'Bob\u2019s 
 Breakfast' rather than u'Bob\x92s Breakfast' although u'Dog\u2019s dinner' 
 seems a probable consequence.

If that's the case, he should read the file as string, de- and encode it 
(probably into a StringIO) and then feed it to the parser.

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


urllib timeout hole - long timeout if site doesn't send headers.

2008-01-03 Thread John Nagle
   urllib has a hole in its timeout protection.

Using socket.setdefaulttimeout will make urllib time out if a
site doesn't open a TCP connection in the indicated time.  But if the site
opens the TCP connection and never sends HTTP headers, it takes about
20 minutes for the read in urllib's open to time out.

There are some web servers that produce this behavior, and
many seem to be associated with British universities and nonprofits.
With these, requesting http://example.com; opens a TCP connection
on which nothing is ever sent, while http://www.example.com;
yields a proper web page.

Even Firefox doesn't time this out properly.  Try http://soton.ac.uk;
in Firefox, and be prepared for a long wait.

   There was some active work in the urllib timeout area last summer.
What happened to that?

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


Re: Treating a unicode string as latin-1

2008-01-03 Thread Fredrik Lundh
Diez B. Roggisch wrote:

 I would think it more likely that he wants to end up with u'Bob\u2019s 
 Breakfast' rather than u'Bob\x92s Breakfast' although u'Dog\u2019s dinner' 
 seems a probable consequence.
 
 If that's the case, he should read the file as string, de- and encode it 
 (probably into a StringIO) and then feed it to the parser.

some alternatives:

- clean up the offending strings:

 http://effbot.org/zone/unicode-gremlins.htm

- turn the offending strings back to iso-8859-1, and decode them again:

 u = u'Bob\x92s Breakfast'
 u = u.encode(iso-8859-1).decode(cp1252)

- upgrade to ET 1.3 (available in alpha) and use the parser's encoding 
option to override the file's encoding:

 parser = ET.XMLParser(encoding=cp1252)
 tree = ET.parse(source, parser)

/F

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


Re: reassign to builtin possible !?

2008-01-03 Thread Benjamin
On Jan 3, 7:04 am, Bernhard Merkle [EMAIL PROTECTED]
wrote:
 Hi there,

 I am reading Learning Python 3e from Mark Lutz and just found out that
 reassigning to builtins is possible.
 What is the reason, why Python allows this ? IMO this is very risky
 and can lead to hard to find errors.
I don't think it's a huge issue. In fact, I think it's a feature. For
example, it'd be extremely issue to reassign open, if you wanted to
implement a virtual file system, and you couldn't modify the module
the used open.
 (see also Learning Python 3e, Chapter 16, Page 315)

  True
 True
  False
 False
  True = 1
  True
 1
  True = 0
  True

 0

 TIA,
 Berni

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


Re: Fate of itertools.dropwhile() and itertools.takewhile()

2008-01-03 Thread Arnaud Delobelle
On Jan 3, 4:39 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 On Dec 29 2007, 11:10 pm, Raymond Hettinger [EMAIL PROTECTED] wrote:

  I'm considering deprecating these two functions and would like some
  feedback from the community or from people who have a background in
  functional programming.

 Well I have just this minute used dropwhile in anger, to find the next
 suitable filename when writing database dumps using date.count names:

     filename = %02d-%02d-%d % (now.day, now.month, now.year)
     if os.path.exists(filename):
         candidates = (%s.%d % (filename, x) for x in count(1))
         filename = dropwhile(os.path.exists, candidates).next()

 Much clearer than the alternatives I think, please keep dropwhile and
 takewhile in itertools ;)

Wouldn't using ifilterfalse instead of dropwhile produce the same
result?

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


Re: reassign to builtin possible !?

2008-01-03 Thread Chris Mellon
On Jan 3, 2008 8:05 AM, Tim Chase [EMAIL PROTECTED] wrote:
  But you can't alter the values for True/False globally with this.
 
  Are you sure ? what about the following example ?
  Is this also shadowing ?
 
  import __builtin__
  __builtin__.True = False
  __builtin__.True
  False

 It doesn't seem to screw things up globally

   import __builtin__
   t = __builtin__.True
   __builtin__.True = False
   __builtin__.False = t
   True
 False
   False
 True
   1 == 1
 True
   import os
   os.path.isdir('.')
 True
   #if they were globally redefined, this would be False
   #you'd have to actually reference __builtin__.True

 My thought would be if you do something as daft as
 redefining/shadowing True and False, you get the headaches that
 ensue.  Fortunately, since Python is explicit, you can trace back
 through the code and see where the inanity occurred.
 Additionally, any scoping rules mean that programmer stupidity
 can't leak too badly outside the scope of the block containing
 the stupidity.

 It's the old DIHWIDT! WDDT! (Doctor, it hurts when I do
 this!, well don't do that!) syndrome.


In Py3k this will be a syntax error, like assigning to None is now.
Possibly also in 2.6.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyOpenGL, wxPython weird behaviour

2008-01-03 Thread kyosohma
On Jan 3, 11:50 am, Adeola Bannis [EMAIL PROTECTED] wrote:
 Hi everyone,

 I'm doing a project using wxPython and pyopengl, and I seem to have a
 problem rendering textures. This is code that worked before my hard
 drive had a meltdown, but not since I re-installed everything.

 I've determined the problem is in the OpenGL part of my program. I do
 some calculations to generate a 2D numpy array that holds the image
 data, and pylab.imshow() shows me the image as it is meant to be. I
 used the same algorithm in Octave and MATLAB, and all are giving me
 the right picture.

 However, using pyOpenGL and the numpyhandler functions (http://cours-
 info.iut-bm.univ-fcomte.fr/docs/python/OpenGL/
 OpenGL.arrays.numpymodule.NumpyHandler-class.html) doesn't seem to
 work. I get a garbled screen pocked with black pixels. I am including
 my openGL code below. What am I doing wrong?

 And yes, I did make the dtype of my array 'float32'.

 ---code snippets--

 import wx
 from wx.glcanvas import GLCanvas

 from OpenGL.GLU import *
 from OpenGL.GL import *
 from OpenGL.arrays.numpymodule import NumpyHandler

 PC = 1
 RI = 0

 class myGLCanvas(GLCanvas):
 def __init__(self, parent):
 GLCanvas.__init__(self, parent,-1)
 wx.EVT_PAINT(self, self.OnPaint)
 self.init = 0
 self.mode = -1
 # making a texture for the range image
 self.texture = glGenTextures(1)
 # making a spot for the point cloud points
 self.cloud = None
 return

 def OnPaint(self,event):
 dc = wx.PaintDC(self)
 self.SetCurrent()
 if not self.init:
 self.InitGL()
 self.init = 1
 self.OnDraw()
 return

 def OnDraw(self):
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
 if self.mode == RI:
 self.drawRange()
 elif self.mode == PC:
 self.drawCloud()

 def InitGL(self):
 glClearColor(0.0, 0.0, 0.0, 0.0);
 glClearDepth(1.0)
 glEnable(GL_DEPTH_TEST)
 glDepthFunc(GL_LEQUAL)
 glClear(GL_COLOR_BUFFER_BIT)

 glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
 glPixelStorei(GL_PACK_ALIGNMENT, 1)

 #NTSC colour scales...
 glPixelTransferf(GL_RED_SCALE, 0.299);
 glPixelTransferf(GL_GREEN_SCALE, 0.587);
 glPixelTransferf(GL_BLUE_SCALE, 0.114);

 glMatrixMode(GL_PROJECTION)
 glLoadIdentity()
 glOrtho(0.0,1.0,0,1.0,-1.0,1.0)
 glMatrixMode(GL_MODELVIEW)
 glLoadIdentity()

 return

 def rangeImage(self, image):

 glBindTexture(GL_TEXTURE_2D, self.texture)
 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE )

 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
 GL_LINEAR)
 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
 GL_LINEAR)
 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

 # flatten it into a list so the OpenGL calls work
 n = NumpyHandler()
 fI = image.flatten()
 flatImage = n.dataPointer(n.contiguous(fI))

 print n.contiguous(fI)

 gluBuild2DMipmaps(GL_TEXTURE_2D, 1, image.shape[0]+1,
 image.shape[1]+1,
 GL_LUMINANCE, 
 GL_FLOAT, flatImage)
 self.mode = RI
 self.OnDraw()

 def drawRange(self):
 ''' Controls the actual drawing of the range image'''

 glMatrixMode(GL_MODELVIEW)
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

 glColor3f(1.0,1.0,1.0)
 glEnable(GL_TEXTURE_2D)
 glBindTexture(GL_TEXTURE_2D, self.texture)
 glBegin(GL_TRIANGLE_FAN)
 glTexCoord2d(1,1); glVertex3f(0.0, 0.0, 0.0)
 glTexCoord2d(1,0); glVertex3f(0.0, 1.0, 0.0)
 glTexCoord2d(0,0); glVertex3f(1.0, 1.0, 0.0)
 glTexCoord2d(0,1); glVertex3f(1.0, 0.0, 0.0)
 glEnd()
 self.SwapBuffers()

 end snippet---

I've never messed with pyOpenGL, but it seems that they have their own
user's group, which would probably be better at answering your
question:

http://sourceforge.net/mail/?group_id=5988

Of course, it could be that you upgraded your wxPython to the latest
version and as I recall, they were 

Re: urllib2 disable proxy

2008-01-03 Thread Rob Wolfe
Dimitrios Apostolou [EMAIL PROTECTED] writes:

 On Wed, 2 Jan 2008, Rob Wolfe wrote:

 Dimitrios Apostolou [EMAIL PROTECTED] writes:

 Hello list,

 I've been looking for a way to explicitly disable the use of proxies with
 urllib2, no matter what the environment dictates. Unfortunately I can't find
 a way in the documentation, and reading the source leads me to believe that
 something like the following does the job:

 req.set_proxy(None,None)

 Where req is a urllib2.Request instance. So is there an official way of 
 doing
 this? Perhaps it should be added in the documentation?

 I believe that the recommended way is to use `urllib2.ProxyHandler`.
 Take a look at:
 http://www.voidspace.org.uk/python/articles/urllib2.shtml

 Thanks for the pointer, I will use that way. However it seems rather
 non-elegant way to do something so simple and I was hoping not to mess
 with ProxyHandler, especially since I want *no* proxy... IMHO
 something like the following would be more elegant:

 req.set_proxy('','http')

 or

 req.set_proxy(None,'http')


 However these ways *don't* work. You think I should file a feature
 request somewhere or send this to the python-dev list?

Actually, I like this idea of handlers and openers 
and find it simple and _elegant_, so I can't second that request.
Besides disabling proxies despite environmental settings
is a special case, so imho using `instal_opener` is justified.

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


Re: Who's to blame?

2008-01-03 Thread kyosohma
On Jan 3, 8:48 am, Nicola Musatti [EMAIL PROTECTED] wrote:
 Hallo,
 First of all I apologize for the longish example at the bottom, but
 the biggest source file is automatically generated and I didn't want
 to modify more than strictly necessary. Also, it would be shorter if
 XML wasn't so verbose ;-)

 The following is a wxPython/XRC toy program with a form with a button
 which, when pressed, causes a simple dialog to be displayed. The
 problem lies in the fact that apparently ShowModal() does not return
 when either the Yes or the No buttons are pressed. Curiously, if you
 change the Yes and No buttons with the OK and Cancel ones that are
 currently commented everything works as expected.

 As the sbs_test_xrc.py file below is automatically generated by
 wxPython 2.8.6.1's XRCed tool from a XRC file which in turn is
 generated by wxFormBuilder (http://wxformbuilder.org/), I really cant
 figure out to whom I should report this problem, assuming I'm not
 missing some obvious mistake of mine, that is.

 Thanks for your help.

 Cheers,
 Nicola Musatti

 # sbs_test.py
 import wx
 import sbs_test_xrc

 class MainFrame(sbs_test_xrc.xrcMainFrame):
 def __init__(self, parent):
 sbs_test_xrc.xrcMainFrame.__init__(self, parent)
 self.button.Bind(wx.EVT_BUTTON, self.OnButton)

 def OnButton(self, event=None):
 d = sbs_test_xrc.xrcDialog(self)
 ##if d.ShowModal() == wx.ID_OK:
 if d.ShowModal() == wx.ID_YES:
 self.Close()

 class Application(wx.App):
 def OnInit(self):
 self.frame = MainFrame(None)
 self.frame.Show()
 self.SetTopWindow(self.frame)
 return True

 if __name__ == '__main__':
 app = Application()
 app.MainLoop()

 # sbs_test_xrc.py
 # This file was automatically generated by pywxrc, do not edit by
 hand.
 # -*- coding: UTF-8 -*-

 import wx
 import wx.xrc as xrc

 __res = None

 def get_resources():
  This function provides access to the XML resources in this
 module.
 global __res
 if __res == None:
 __init_resources()
 return __res

 class xrcDialog(wx.Dialog):
 def PreCreate(self, pre):
  This function is called during the class's initialization.

 Override it for custom setup before the window is created
 usually to
 set additional window styles using SetWindowStyle() and
 SetExtraStyle().
 pass

 def __init__(self, parent):
 # Two stage creation 
 (seehttp://wiki.wxpython.org/index.cgi/TwoStageCreation)
 pre = wx.PreDialog()
 self.PreCreate(pre)
 get_resources().LoadOnDialog(pre, parent, Dialog)
 self.PostCreate(pre)

 # create attributes for the named items in this container
 self.wxID_YES = xrc.XRCCTRL(self, wxID_YES)
 self.wxID_NO = xrc.XRCCTRL(self, wxID_NO)

 class xrcMainFrame(wx.Frame):
 def PreCreate(self, pre):
  This function is called during the class's initialization.

 Override it for custom setup before the window is created
 usually to
 set additional window styles using SetWindowStyle() and
 SetExtraStyle().
 pass

 def __init__(self, parent):
 # Two stage creation 
 (seehttp://wiki.wxpython.org/index.cgi/TwoStageCreation)
 pre = wx.PreFrame()
 self.PreCreate(pre)
 get_resources().LoadOnFrame(pre, parent, MainFrame)
 self.PostCreate(pre)

 # create attributes for the named items in this container
 self.button = xrc.XRCCTRL(self, button)

 #  Resource data --

 def __init_resources():
 global __res
 __res = xrc.EmptyXmlResource()

 wx.FileSystem.AddHandler(wx.MemoryFSHandler())

 sbs_test_xrc = '''\
 ?xml version=1.0 ?resource version=2.3.0.1 
 xmlns=http://www.wxwindows.org/wxxrc;
 object class=wxDialog name=Dialog
 stylewxDEFAULT_DIALOG_STYLE/style
 title/
 object class=wxFlexGridSizer
 rows2/rows
 cols2/cols
 vgap0/vgap
 hgap0/hgap
 growablecols/
 growablerows/
 object class=sizeritem
 option1/option
 flagwxEXPAND/flag
 border5/border
 object class=wxStdDialogButtonSizer
 object class=button
 
 flagwxALIGN_CENTER_HORIZONTAL|wxALL/flag
 border5/border
 !--
 object class=wxButton 
 name=wxID_OK
 labelamp;OK/label
 /object
 --
 

Re: PyOpenGL, wxPython weird behaviour

2008-01-03 Thread Adeola Bannis
Thanks, will do...
On Jan 3, 2:07 pm, [EMAIL PROTECTED] wrote:
 On Jan 3, 11:50 am, Adeola Bannis [EMAIL PROTECTED] wrote:



  Hi everyone,

  I'm doing a project using wxPython and pyopengl, and I seem to have a
  problem rendering textures. This is code that worked before my hard
  drive had a meltdown, but not since I re-installed everything.

  I've determined the problem is in the OpenGL part of my program. I do
  some calculations to generate a 2D numpy array that holds the image
  data, and pylab.imshow() shows me the image as it is meant to be. I
  used the same algorithm in Octave and MATLAB, and all are giving me
  the right picture.

  However, using pyOpenGL and the numpyhandler functions (http://cours-
  info.iut-bm.univ-fcomte.fr/docs/python/OpenGL/
  OpenGL.arrays.numpymodule.NumpyHandler-class.html) doesn't seem to
  work. I get a garbled screen pocked with black pixels. I am including
  my openGL code below. What am I doing wrong?

  And yes, I did make the dtype of my array 'float32'.

  ---code snippets--

  import wx
  from wx.glcanvas import GLCanvas

  from OpenGL.GLU import *
  from OpenGL.GL import *
  from OpenGL.arrays.numpymodule import NumpyHandler

  PC = 1
  RI = 0

  class myGLCanvas(GLCanvas):
  def __init__(self, parent):
  GLCanvas.__init__(self, parent,-1)
  wx.EVT_PAINT(self, self.OnPaint)
  self.init = 0
  self.mode = -1
  # making a texture for the range image
  self.texture = glGenTextures(1)
  # making a spot for the point cloud points
  self.cloud = None
  return

  def OnPaint(self,event):
  dc = wx.PaintDC(self)
  self.SetCurrent()
  if not self.init:
  self.InitGL()
  self.init = 1
  self.OnDraw()
  return

  def OnDraw(self):
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  if self.mode == RI:
  self.drawRange()
  elif self.mode == PC:
  self.drawCloud()

  def InitGL(self):
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glClearDepth(1.0)
  glEnable(GL_DEPTH_TEST)
  glDepthFunc(GL_LEQUAL)
  glClear(GL_COLOR_BUFFER_BIT)

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
  glPixelStorei(GL_PACK_ALIGNMENT, 1)

  #NTSC colour scales...
  glPixelTransferf(GL_RED_SCALE, 0.299);
  glPixelTransferf(GL_GREEN_SCALE, 0.587);
  glPixelTransferf(GL_BLUE_SCALE, 0.114);

  glMatrixMode(GL_PROJECTION)
  glLoadIdentity()
  glOrtho(0.0,1.0,0,1.0,-1.0,1.0)
  glMatrixMode(GL_MODELVIEW)
  glLoadIdentity()

  return

  def rangeImage(self, image):

  glBindTexture(GL_TEXTURE_2D, self.texture)
  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE 
  )

  glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  GL_LINEAR)
  glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
  GL_LINEAR)
  glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 
  GL_REPEAT)
  glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 
  GL_REPEAT)

  # flatten it into a list so the OpenGL calls work
  n = NumpyHandler()
  fI = image.flatten()
  flatImage = n.dataPointer(n.contiguous(fI))

  print n.contiguous(fI)

  gluBuild2DMipmaps(GL_TEXTURE_2D, 1, image.shape[0]+1,
  image.shape[1]+1,
  GL_LUMINANCE, 
  GL_FLOAT, flatImage)
  self.mode = RI
  self.OnDraw()

  def drawRange(self):
  ''' Controls the actual drawing of the range image'''

  glMatrixMode(GL_MODELVIEW)
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

  glColor3f(1.0,1.0,1.0)
  glEnable(GL_TEXTURE_2D)
  glBindTexture(GL_TEXTURE_2D, self.texture)
  glBegin(GL_TRIANGLE_FAN)
  glTexCoord2d(1,1); glVertex3f(0.0, 0.0, 0.0)
  glTexCoord2d(1,0); glVertex3f(0.0, 1.0, 0.0)
  glTexCoord2d(0,0); glVertex3f(1.0, 1.0, 0.0)
  glTexCoord2d(0,1); glVertex3f(1.0, 0.0, 0.0)
  glEnd()
  self.SwapBuffers()

  end snippet---

 I've never messed with pyOpenGL, but it seems that they have their own
 user's group, which would probably be 

Re: different encodings for unicode() and u''.encode(), bug?

2008-01-03 Thread mario
On Jan 2, 2:25 pm, Piet van Oostrum [EMAIL PROTECTED] wrote:

 Apparently for the empty string the encoding is irrelevant as it will not
 be used. I guess there is an early check for this special case in the code.

In the module I an working on [*] I am remembering a failed encoding
to allow me, if necessary, to later re-process fewer encodings. In the
case of an empty string AND an unknown encoding this strategy
failed...

Anyhow, the question is, should the behaviour be the same for these
operations, and if so what should it be:

u.encode(non-existent)
unicode(, non-existent)

mario

[*] a module to decode heuristically, that imho is actually starting
to look quite good, it is at http://gizmojo.org/code/decodeh/ and any
comments very welcome.
-- 
http://mail.python.org/mailman/listinfo/python-list


New-style objects are not instances, apparently

2008-01-03 Thread [EMAIL PROTECTED]
I have a class that derives from Exception.  In Python 2.4,
isinstance(MyClass(), types.InstanceType) was True.  In 2.5, it's
False.

Further experimentation showed that derivation from object was the
culprit; new-style objects are not considered instances in the above
sense.  I wasn't able to figure out a workaround.  Is there one, or is
the distinction between traditional classes and built-in types only
going to get more and more hazy?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New-style objects are not instances, apparently

2008-01-03 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

 Further experimentation showed that derivation from object was the
 culprit; new-style objects are not considered instances in the above
 sense.  I wasn't able to figure out a workaround.  Is there one, or is
 the distinction between traditional classes and built-in types only
 going to get more and more hazy?

new-style classes *are* types.

one way to test for a new-style object is to compare type(obj) to 
obj.__class__; if they point to the same object, it's a new-style object.

/F

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


Re: New-style objects are not instances, apparently

2008-01-03 Thread Arnaud Delobelle
On Jan 3, 9:15 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 I have a class that derives from Exception.  In Python 2.4,
 isinstance(MyClass(), types.InstanceType) was True.  In 2.5, it's
 False.

 Further experimentation showed that derivation from object was the
 culprit; new-style objects are not considered instances in the above
 sense.  I wasn't able to figure out a workaround.

IIRC, this is because since 2.5 Exception is a new style class.  New
style objects are instances of their class, not of InstanceType as was
the case with instances of old-style classes.  So in your case
isinstance(MyClass(), Exception) will return True.

 Is there one, or is
 the distinction between traditional classes and built-in types only
 going to get more and more hazy?

I'm not sure what you mean here.

--
Arnaud

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


Re: How is AI implemented

2008-01-03 Thread kyosohma
On Jan 3, 11:49 am, Martin Marcher [EMAIL PROTECTED] wrote:
 Hi,

 I know it's not a trivial field but I had some readings about
 artificial intelligence lately and my personal conclusion is that it's
 mostly just statistics.

 Naively explained:

 continiously gather and store information and apply a default rating

 1) answer questions with gathered information according to rating
 2) store/calculate rating based upon input (be it an interface or user input)
 3) goto 1 (sorry for the goto)

 So I think that in general there hasn't yet been any artificial
 intelligence programmed (Note: I believe I'm aware of the difference
 between artificial intelligence and artificial conscusiness where the
 second would be able to answer things like: How are you today and
 the first can answer factual knowledge)

 Am I thinking right here or is there some (preferrably) web reading
 available on that or in depth links about the topic?

 thanks and sorry for OT posting
 martin

 --http://noneisyours.marcher.namehttp://feeds.feedburner.com/NoneIsYours


Some readings:

http://www-formal.stanford.edu/jmc/whatisai/whatisai.html
http://www.sciencedaily.com/news/computers_math/artificial_intelligence/
http://www.jair.org/
http://dir.yahoo.com/Science/computer_science/artificial_intelligence/

Fuzzy Logic usually crops up as a related topic:

http://www.seattlerobotics.org/encoder/mar98/fuz/flindex.html
http://www.austinlinks.com/Fuzzy/

I'm not involved in this field, but I think saying that AI is just
statistics is a pretty sweeping statement. It's more like super
complicated stats using algorithms worthy of Calculus with branch
logic thrown in for good measure.

How's that for a load of buzz words!?

Hope those links give you lots of info. Let us know when you've got a
cool talking Python program!

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


Re: How is AI implemented

2008-01-03 Thread montyphyton
On Jan 3, 6:49 pm, Martin Marcher [EMAIL PROTECTED] wrote:
 Hi,

 I know it's not a trivial field but I had some readings about
 artificial intelligence lately and my personal conclusion is that it's
 mostly just statistics.

 Naively explained:

 continiously gather and store information and apply a default rating

 1) answer questions with gathered information according to rating
 2) store/calculate rating based upon input (be it an interface or user input)
 3) goto 1 (sorry for the goto)

really naively :)


 So I think that in general there hasn't yet been any artificial
 intelligence programmed (Note: I believe I'm aware of the difference
 between artificial intelligence and artificial conscusiness where the
 second would be able to answer things like: How are you today and
 the first can answer factual knowledge)


What you want to do is look up the difference between weak AI and
strong AI.
Everything we have to this day is weak AI, and there is still a debate
between
various scientists as to whether strong AI is even possible.  Be sure
to look
at Chinese room argument to see if strong AI is really what you need.
(http://en.wikipedia.org/wiki/Chinese_room)

 Am I thinking right here or is there some (preferrably) web reading
 available on that or in depth links about the topic?

 thanks and sorry for OT posting
 martin

 --http://noneisyours.marcher.namehttp://feeds.feedburner.com/NoneIsYours

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


Re: New-style objects are not instances, apparently

2008-01-03 Thread Martin v. Löwis
 Further experimentation showed that derivation from object was the
 culprit; new-style objects are not considered instances in the above
 sense.  I wasn't able to figure out a workaround.  Is there one, or is
 the distinction between traditional classes and built-in types only
 going to get more and more hazy?

In the long run (ie. Python 3), the distinction is going to be very
hazy, very dark: it will entirely disappear. There will be only one
concept of type/class, not two, so there will be no point
distinguishing between types and classes.

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


choosing random dynamic port number

2008-01-03 Thread Emin.shopper Martinian.shopper
Dear Experts,

Is there a good way to choose/assign random dynamic port numbers in python?

I had in mind something like the following, but if multiple programs are
generating random port numbers, is there a way to check if a given port
number is already taken?

def GenerateDynamicPortNumber():
Generate a random dynamic port number and return it.
# port numbers between 49152 to 65535 are dynamic port numbers
return 49152 + random.randrange(15000)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: choosing random dynamic port number

2008-01-03 Thread Fredrik Lundh
Emin.shopper Martinian.shopper wrote:

 Is there a good way to choose/assign random dynamic port numbers in python?
 
 I had in mind something like the following, but if multiple programs are 
 generating random port numbers, is there a way to check if a given port 
 number is already taken?
 
 def GenerateDynamicPortNumber():
 Generate a random dynamic port number and return it.
 # port numbers between 49152 to 65535 are dynamic port numbers
 return 49152 + random.randrange(15000)

def GenerateDynamicPortNumber():
return 0

(to get the actual number, use getsockname() on the socket after you've 
called bind on it)

/F

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


Re: XML-XSD Processing/Creation.

2008-01-03 Thread xkenneth
On Jan 3, 1:55 am, Stefan Behnel [EMAIL PROTECTED] wrote:
 paul wrote:
  Can i create python classes based off the XSD files? What else can I
  do with the XSD files?
  This might be worth looking at:http://www.rexx.com/~dkuhlman/#generateDS

 If it's really such a complex XML language, the tool above might or might not
 be of any help, as it doesn't support the whole XSD standard (and XML Schema
 is very complex). It's worth a try, but don't expect too much.

 The lxml way of dealing with XML languages is namespace implementation:

 http://codespeak.net/lxml/dev/element_classes.html#id1

 However, there isn't currently a way to automatically bootstrap an
 implementation, especially not in XSD, so it depends on the language how much
 work it will be to get this to a usable state.

 Stefan

Just a bump in an attempt to get some more help.
-- 
http://mail.python.org/mailman/listinfo/python-list


dictionary/hash and '1' versus 1

2008-01-03 Thread Reedick, Andrew
As a Perl monkey in the process of learning Python, I just stepped on
the '1' (string) is not the same as 1 (integer) in regards to keys for
dictionaries/hashes landmine.  Is there a good way to ensure that
numbers represented as strings or ints do not get mixed up as keys?

Example of the problem:
 h2 = { 1 : ''}
 print h2.has_key(1)
True
 print h2.has_key('1')
False

The problem occurred because a method used to generate keys was
returning a string instead of a number without an explicit conversion
taking place.  And since I was using hash.get(i, default_value) to avoid
having to pair every key lookup with a hash.has_key(), no exception was
thrown when the key wasn't found.

It's fugly to wrap every key reference in str(), ex:
foo[str(some_func(i))].  It's tedious to add a has_key before every key
lookup.  And I have no real desire to stuff every hash inside a class in
order to ensure that keys are converted to strings.

Any good solutions or accepted practices to prevent the intermixing of
number strings and integers as hash keys?  A hash wrapper class seems to
be the best bet so far.



*

The information transmitted is intended only for the person or entity to which 
it is addressed and may contain confidential, proprietary, and/or privileged 
material. Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you received this in error, 
please contact the sender and delete the material from all computers. GA623


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


Re: different encodings for unicode() and u''.encode(), bug?

2008-01-03 Thread John Machin
On Jan 4, 8:03 am, mario [EMAIL PROTECTED] wrote:
 On Jan 2, 2:25 pm, Piet van Oostrum [EMAIL PROTECTED] wrote:

  Apparently for the empty string the encoding is irrelevant as it will not
  be used. I guess there is an early check for this special case in the code.

 In the module I an working on [*] I am remembering a failed encoding
 to allow me, if necessary, to later re-process fewer encodings.

If you were in fact doing that, you would not have had a problem. What
you appear to have been doing is (a) remembering a NON-failing
encoding, and assuming that it would continue not to fail (b) not
differentiating between failure reasons (codec doesn't exist, input
not consistent with specified encoding).

A good strategy when dealing with encodings that are unknown (in the
sense that they come from user input, or a list of encodings you got
out of the manual, or are constructed on the fly (e.g. encoding = 'cp'
+ str(code_page_number) # old MS Excel files)) is to try to decode
some vanilla ASCII alphabetic text, so that you can give an immemdiate
in-context error message.

 In the
 case of an empty string AND an unknown encoding this strategy
 failed...


 Anyhow, the question is, should the behaviour be the same for these
 operations, and if so what should it be:

 u.encode(non-existent)
 unicode(, non-existent)

Perhaps you should make TWO comparisons:
(1)
unistrg = strg.decode(encoding)
with
unistrg = unicode(strg, encoding)
[the latter optimises the case where strg is ''; the former can't
because its output may be '', not u'', depending on the encoding, so
ut must do the lookup]
(2)
unistrg = strg.decode(encoding)
with
strg = unistrg.encode(encoding)
[both always do the lookup]

In any case, a pointless question (IMHO); the behaviour is extremely
unlikely to change, as the chance of breaking existing code outvotes
any desire to clean up a minor inconsistency that is easily worked
around.
-- 
http://mail.python.org/mailman/listinfo/python-list


ctypes - pointer to array of structs?

2008-01-03 Thread skip

(Is this the right place to ask ctypes questions?  There's a mailing list
but the last post to it seems to have been in November 2006.)

Using ctypes I reference a structure which contains a pointer to an array of
another structure:

class SYMBOL(Structure):
_fields_ = [(symbol, c_char_p),
(num, c_int),
(units, c_int),
(baseprice, c_int),
(active, c_int)]
SYMBOL_PTR = POINTER(SYMBOL)

class TABLE(Structure):
_fields_ = [(map, SYMBOL_PTR),
(nsymbols, c_uint),
...]

Effectively, TABLE.map is an array of TABLE.nsymbols SYMBOLS.  How to I
reference elements in that array?  In C I would just treat TABLE.map like an
array and index into it (for i=0; i TABLE.nsymbols; i++) ...).  This is
data returned from a C library, not something I'm building in Python to pass
into C.

Thx,

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


Re: dictionary/hash and '1' versus 1

2008-01-03 Thread John Machin
On Jan 4, 9:56 am, Reedick, Andrew [EMAIL PROTECTED] wrote:
 As a Perl monkey in the process of learning Python, I just stepped on
 the '1' (string) is not the same as 1 (integer) in regards to keys for
 dictionaries/hashes landmine.

Congratulations. You have just stepped off the '1' (string) is the
same as 1 (integer) in regards to several purposes landmine.

Welcome to the awk-free world :-)

  Is there a good way to ensure that
 numbers represented as strings or ints do not get mixed up as keys?

 Example of the problem:
  h2 = { 1 : ''}
  print h2.has_key(1)
 True
  print h2.has_key('1')
 False

 The problem occurred because a method used to generate keys was
 returning a string instead of a number without an explicit conversion
 taking place.  And since I was using hash.get(i, default_value) to avoid
 having to pair every key lookup with a hash.has_key(), no exception was
 thrown when the key wasn't found.

has_key is a has_been ... use key in dict instead of
dict.has_key(key)

 It's fugly to wrap every key reference in str(), ex:
 foo[str(some_func(i))].

Fugliness is in the eye of the beholder.

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


Re: ctypes - pointer to array of structs?

2008-01-03 Thread Diez B. Roggisch
[EMAIL PROTECTED] schrieb:
 (Is this the right place to ask ctypes questions?  There's a mailing list
 but the last post to it seems to have been in November 2006.)

No, it's active.

 Using ctypes I reference a structure which contains a pointer to an array of
 another structure:
 
 class SYMBOL(Structure):
 _fields_ = [(symbol, c_char_p),
 (num, c_int),
 (units, c_int),
 (baseprice, c_int),
 (active, c_int)]
 SYMBOL_PTR = POINTER(SYMBOL)
 
 class TABLE(Structure):
 _fields_ = [(map, SYMBOL_PTR),
 (nsymbols, c_uint),
 ...]
 
 Effectively, TABLE.map is an array of TABLE.nsymbols SYMBOLS.  How to I
 reference elements in that array?  In C I would just treat TABLE.map like an
 array and index into it (for i=0; i TABLE.nsymbols; i++) ...).  This is
 data returned from a C library, not something I'm building in Python to pass
 into C.

I think you should be able to create an array-type with the required 
number of entries, and cast map to that. Along these lines (untested)

ap = POINTER(SYMBOL(table.nsymbols))

map = cast(table.map, ap)

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


py2exe command prompt whenr run

2008-01-03 Thread SMALLp
HY!

I'm using py2exe to port my applications on windows so user won't have 
to install python and other dependencies. Everything works file except 
when i run any of programs it star's command prompt before program starts.

How can i avoid this to happen, and is there any other way of porting my 
applications on windows?


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


Re: py2exe command prompt whenr run

2008-01-03 Thread Fredrik Lundh
SMALLp wrote:
 I'm using py2exe to port my applications on windows so user won't have 
 to install python and other dependencies. Everything works file except 
 when i run any of programs it star's command prompt before program starts.
 
 How can i avoid this to happen

use windows= instead of console= in your setup script. or pass in the 
corresponding option to the setup.py command; see e.g.

http://mail.python.org/pipermail/python-list/2003-December/241319.html

/F

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


Re: dictionary/hash and '1' versus 1

2008-01-03 Thread Stephen Hansen
 As a Perl monkey in the process of learning Python, I just stepped on
 the '1' (string) is not the same as 1 (integer) in regards to keys for
 dictionaries/hashes landmine.  Is there a good way to ensure that
 numbers represented as strings or ints do not get mixed up as keys?


Well one important thing to learn while learning Python is that while the
language is dynamically typed-- it is also /strongly/ typed. Every piece
of data has an explicit type and it doesn't change unless you change it.
It relies on duck typing a lot, and doesn't care if you mix and match
(even partially) compatible types as long as the operations are there,
but one type will always be distinct and remain that type until you
explicitly convert it.

A single integer is distinctly different from a sequence of characters in
some encoding that may just happen to contain representations of a
number so they'll hash differently :)

One type will basically never implicitly convert into another type.

To me, this sounds like the function should have converted the type
explicitly on return. Or maybe you need to convert it explicitly on
receipt.

But if you are in a use-case where you really don't care and only
want to hash strings, you can create a dict subclass easily that
overrides __setitem__ to always str() the input. Check out the
UserDict class.

A similar method lets you make 'case-insensitive' dicts, for example.

Were such a thing to happen automagically, you could get some
weird situations, such as assert (key in dict) == (key in dict.keys())
failing.

Also, do 'if key in dict' instead of 'if dict.has_key(key)'. :)

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

Cursors in a Loop

2008-01-03 Thread t_rectenwald
I have a python script that uses the cx_Oracle module.  I have a list
of values that I iterate through via a for loop and then insert into
the database.  This works okay, but I'm not sure whether I can use one
cursor for all inserts, and define it outside of the loop, or
instantiate and close the cursor within the loop itself.  For example,
I have:

for i in hostlist:
cursor = connection.cursor()
sql= insert into as_siebel_hosts_temp values('%s') % (i)
cursor.execute(sql)
cursor.close()

And I've also tried:

cursor = connection.cursor()
for i in hostlist:
sql= insert into as_siebel_hosts_temp values('%s') % (i)
cursor.execute(sql)
cursor.close()

Both work fine, and execute in the same amount of time.  I'm just
trying to understand what is the correct approach to use.

Thanks,
Tom

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


Re: ctypes - pointer to array of structs?

2008-01-03 Thread Skip Montanaro
  (Is this the right place to ask ctypes questions?  There's a mailing list
  but the last post to it seems to have been in November 2006.)
 
 No, it's active.

Thanks.  I guess the official ASPN-based archive must be dead.

I managed to sort of get access to the array just using indexing
as I would in C, but I'm having some problems referencing
elements of the SYMBOL struct.  I'll keep plugging away.

Skip



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


Re: Cursors in a Loop

2008-01-03 Thread t_rectenwald
On Jan 3, 7:47 pm, t_rectenwald [EMAIL PROTECTED] wrote:
 I have a python script that uses the cx_Oracle module.  I have a list
 of values that I iterate through via a for loop and then insert into
 the database.  This works okay, but I'm not sure whether I can use one
 cursor for all inserts, and define it outside of the loop, or
 instantiate and close the cursor within the loop itself.  For example,
 I have:

 for i in hostlist:
     cursor = connection.cursor()
     sql= insert into as_siebel_hosts_temp values('%s') % (i)
     cursor.execute(sql)
     cursor.close()

 And I've also tried:

 cursor = connection.cursor()
 for i in hostlist:
     sql= insert into as_siebel_hosts_temp values('%s') % (i)
     cursor.execute(sql)
 cursor.close()

 Both work fine, and execute in the same amount of time.  I'm just
 trying to understand what is the correct approach to use.

 Thanks,
 Tom

I think I have this one figured out.  The answer would be the second
option, i.e. keep the cursor instantion and close outside of the
loop.  I wasn't aware that one cursor could be used for multiple
executes.

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


calling system command in window is very slow in python 2.5.1

2008-01-03 Thread wang frank

Hi,
 
I am running a python script that will change the attribute of a directory and 
its subdiretory by command:
 
os.system(chmod -R  .)
 
or 
 
os.system(attrib -R * /S)
 
Both commands chmod and attrib run quite fast in dos command shell. However, 
inside python, they are very slow and I have to kill them by Control-C. I do 
not know why?
 
Can anyone help me to figure it out? 
 
Thanks
 
Frank
_
Hotmailがお届けする、幸せになるためのメールマガジン「ビジネス幸福論」実施中
http://go.windowslive.jp/-- 
http://mail.python.org/mailman/listinfo/python-list

lucas ares

2008-01-03 Thread lucas
visitame www.lucasares.com.ar
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary/hash and '1' versus 1

2008-01-03 Thread Steven D'Aprano
On Thu, 03 Jan 2008 16:56:00 -0600, Reedick, Andrew wrote:

 The problem occurred because a method used to generate keys was
 returning a string instead of a number without an explicit conversion
 taking place.  And since I was using hash.get(i, default_value) to avoid
 having to pair every key lookup with a hash.has_key(), no exception was
 thrown when the key wasn't found.


# How to fix this broken function without modifying the source?
def foo(arg):
Returns a string instead of a number.
return 1  # oops I meant 1



_foo = foo  # save a reference to original broken function
foo = lambda *args, **kwargs: int(_foo(*args, **kwargs))  # and patch it


And now you can use foo(arg) confident that it will always return an int 
like you expect.

Modifications of this technique should be obvious.



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


Re: problem with global var

2008-01-03 Thread Steven D'Aprano
On Thu, 03 Jan 2008 11:38:48 -0300, Bruno Ferreira wrote:

 Hi,
 
 I wrote a very simple python program to generate a sorted list of lines
 from a squid access log file.
 
 Here is a simplified version:
 
 ##
 1  logfile = open (squid_access.log, r) 
 2  topsquid = [[0, 0, 0, 0, 0, 0, 0]]

[snip]


Others have already solved the immediate problem, but a much better 
design would be to avoid using a global variable in the first place.



def add_sorted(alist, data):
Add figures from alist to collated data and return data.
# do your processing here...
return data



topsquid=[[0, 0, 0, 0, 0, 0, 0]]
for line in logfile:
linefields = logline.split()
topsquid = add_sorted(linefields, topsquid)




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


linecache and glob

2008-01-03 Thread jo3c
hi everyone happy new year!
im a newbie to python
i have a question
by using linecache and glob
how do i read a specific line from a file in a batch and then insert
it into database?

because it doesn't work! i can't use glob wildcard with linecache

 import linecache
 linecache.getline(glob.glob('/etc/*', 4)

doens't work

is there any better methods??? thank you very much in advance

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


Information on PyGMT?

2008-01-03 Thread Alaric
Hello - I'm seeking info on the PyGMT python wrapper for the Generic Mapping
Tools package.

Unfortunately, the only site (forge.nesc.ac.uk) that seems to offer the code
(written by Magnus Hagdorn) is not responding. I don't know if that's a
temporary condition or if that site is out of commission.

Google isn't revealing much recent discussion either which is sad, as I
would imagine this to be a valuable tool!

Can anyone shed any light??

Better yet, does anyone have the latest tarball, and an FTP site they can
offer it on? :)

Many thanks!
Alaric
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cursors in a Loop

2008-01-03 Thread Carsten Haese
On Thu, 2008-01-03 at 17:25 -0800, t_rectenwald wrote:
 On Jan 3, 7:47 pm, t_rectenwald [EMAIL PROTECTED] wrote:
  I have a python script that uses the cx_Oracle module.  I have a list
  of values that I iterate through via a for loop and then insert into
  the database.  This works okay, but I'm not sure whether I can use one
  cursor for all inserts, and define it outside of the loop, or
  instantiate and close the cursor within the loop itself.  For example,
  I have:
 
  for i in hostlist:
  cursor = connection.cursor()
  sql= insert into as_siebel_hosts_temp values('%s') % (i)
  cursor.execute(sql)
  cursor.close()
 
  And I've also tried:
 
  cursor = connection.cursor()
  for i in hostlist:
  sql= insert into as_siebel_hosts_temp values('%s') % (i)
  cursor.execute(sql)
  cursor.close()
 
  Both work fine, and execute in the same amount of time.  I'm just
  trying to understand what is the correct approach to use.

Actually, the correct approach would be neither. You should NEVER use
string formatting to fill values into an SQL query. (Doing so causes
security vulnerabilities and performance problems. See, for example,
http://informixdb.blogspot.com/2007/07/filling-in-blanks.html for
detailed explanations.) Instead, you should use a parametrized query.

With a parametrized query, your code becomes this:

cursor = connection.cursor()
for i in hostlist:
cursor.execute(insert into as_siebel_hosts_temp values(?), (i,) )
cursor.close()

Since this will save the database engine from having to re-parse the
query every time, it will run much faster if the list is long.

Even better would be to use executemany:

cursor = connection.cursor()
cursor.executemany(insert into as_siebel_hosts_temp values(?),
[(i,) for i in hostlist] )
cursor.close()

Depending on whether cx_Oracle allows this, the list comprehension in
that example could be replaced by the generator expression
((i,) for i in hostlist), but I don't know if cx_Oracle allows
executemany with an arbitrary iterable.

Hope this helps,

-- 
Carsten Haese
http://informixdb.sourceforge.net


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


Re: linecache and glob

2008-01-03 Thread Jeremy Dillworth
Hello,

Welcome to Python!

glob returns a list of filenames, but getline is made to work on just
one filename.
So you'll need to iterate over the list returned by glob.

 import linecache, glob
 for filename in glob.glob('/etc/*'):
 print linecache.getline(filename, 4)

Maybe you could explain more about what you are trying to do and we
could help more?

Hope this helps,

Jeremy



On Jan 3, 10:02 pm, jo3c [EMAIL PROTECTED] wrote:
 hi everyone happy new year!
 im a newbie to python
 i have a question
 by using linecache and glob
 how do i read a specific line from a file in a batch and then insert
 it into database?

 because it doesn't work! i can't use glob wildcard with linecache

  import linecache
  linecache.getline(glob.glob('/etc/*', 4)

 doens't work

 is there any better methods??? thank you very much in advance

 jo3c

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


Im back...

2008-01-03 Thread Sam Garson
Hi there same project I am afraid...

I want to put the text from the selection of a listbox into a Label when the
the selection is clicked.

I have it so it is put in, but it is put in when I click on the
*next*selection...as in it defines the variable when I click on the
desired the
selection, but it puts it into the label when i click on the *next* item.
It is best if you  have a look. Here is the whole program

[start python code; possibly wrapped by browser]


#!/user/bin/python

from Tkinter import *

def insert():
name = ent.get()
box.insert(0, name)
ent.delete(0, END)

def DeleteCurrent(event):
box.delete(ANCHOR)

def putLabel(event):
selection = box.get(ANCHOR)
v.set(str(selection))

root = Tk()

ent = Entry(root, fg = '#3a3a3a', bg = 'white', relief = 'groove')
ent.grid(row = 0, padx = 3, pady = 3)

button = Button(root, text = Remember, command = insert, relief =
'groove', fg = '#3a3a3a')
button.grid(row = 0, column = 1, padx = 3, pady = 3)

box = Listbox(root, bg = '#ebe9ed', relief = 'groove', height = 15)
box.selectmode = BROWSE
box.grid(row = 2, columnspan = 2, sticky = W+E, padx = 3)
box.bind(Double-Button-1, DeleteCurrent)
box.bind(Button-1, putLabel)

v = StringVar()

current = Label(root, textvariable = v)
current.grid(row = 3, columnspan = 2, sticky = W+E, padx = 3)


root.mainloop()


[end python code]

how do i make it so it puts it into the variable *when* i click it? Any help
will be greatly appreciated!


Thanks,

Sam
-- 
I intend to live forever - so far, so good.

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

Re: PyObject_CallObject code dump after calling 4 times

2008-01-03 Thread grbgooglefan
On Jan 3, 8:49 pm, grbgooglefan [EMAIL PROTECTED] wrote:
 On Jan 3, 8:02 pm, Phil Thompson [EMAIL PROTECTED]
 wrote:





  On Thursday 03 January 2008, grbgooglefan wrote:

   I have a following C++ code which uses PyObject_CallObject to evaluate
   expressions dynamically. This code sets the input parameters for the
   function also dynamically. After calling this function 4 times (with
   these shown values), PyObject_CallObject  causes application to crash
   in frame_dealloc.
   1) Can some one please help me understand why is this crash happening
   in frame_dealloc  how to solve it?
   2) Is there anything wrong I am doing about incrementing or
   decrementing the reference counts of the object passed to
   PyObject_CallObject?

  Yes.

   3) Is it because of the big value (2299265.50) I am trying to
   convert from double to float using PyFloat_FromDouble?

   //= code reduced for readability
   ===
   switch(ndtyp){
        case(INT_T):
            {
            printf(PyInt_FromLong val %d, var %s
   \n,inputVar.nionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
            val = PyInt_FromLong(inputVar.nionum);
            break;
            }
         case(LONG_T):
            {
            printf(PyLong_FromLong val %ld, var %s
   \n,inputVar.lionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
            val = PyLong_FromLong(inputVar.lionum);
            break;
            }
         case(FLOAT_T):
            {
            printf(PyFloat_FromDouble val %f, var %s
   \n,inputVar.fionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
            val = PyFloat_FromDouble(inputVar.fionum);
            break;
            }
         case(DOUBLE_T):
            {
            printf(PyFloat_FromDouble val %f, var %s
   \n,inputVar.dionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
            val = PyFloat_FromDouble(inputVar.dionum);
            break;
            }
          case(STRING_T):
            {
            printf(PyString_FromString val %s, var %s
   \n,inputVar.ioString,pEvalFunc-pExprVarsArray[nCtr].szVarName);
            val = PyString_FromString(inputVar.ioString);
            break;
            }
          default:
            printf(Unknown data type [%d] for %s\n,ndtyp,pEvalFunc-

   pExprVarsArray[nCtr].szVarName);

   }
   if(!val){
      ret = -1;
      printf(Failed to convert %d %s to PyObject\n,ndtyp,pEvalFunc-

   pExprVarsArray[nCtr].szVarName); fflush(stdout);

      Py_XDECREF(pTuple);
      break;
   }
     PyTuple_SetItem(pTuple, nCtr, val);
     Py_XDECREF(val);

  Don't do this - PyTuple_SetItem() steals a reference to val.

   }
   // all variables are set, call Python function
   Py_XINCREF(pTuple);

  Why do this?

     PyObject *pResult = PyObject_CallObject(pEvalFunc-

   pPyEvalFunction,pTuple);

   Py_XDECREF(pTuple);

  Why do this?

   if(PyErr_Occurred()){
    PyErr_Print();
   } else {
         char* plevel = NULL;
         if(NULL != (plevel = PyString_AsString(pResult))){
           ret = 0;
           sprintf(szEvalResult,%s,plevel);
         }
   }
   Py_XDECREF(pResult);

  pTuple will now have the same number of references as when you started the
  above code, so you may want to Py_DECREF() it.

  Phil- Hide quoted text -

  - Show quoted text -

 Thanks for all the responses.
 These help me.
 I could simulate this crash in my small test program  I think (I
 could be wrong also) it is because of extraneous Py_XDECREF of
 PyObject *val which I am using to convert variables to tuple.
 When I change the code to simple do like this, it works fine.
             PyTuple_SetItem(pytuple,0,PyLong_FromLong(size));
             PyTuple_SetItem(pytuple,1,PyLong_FromLong(maxvol));
             PyTuple_SetItem(pytuple,2,PyFloat_FromDouble(adv));- Hide quoted 
 text -

 - Show quoted text -

Now my new code looks like this.
Do you think this is correct way to handle ref counts? Do you forsee
any issues of memory leaks, etc. here?

//

  switch(ndtyp){
case(INT_T):
   //printf(PyInt_FromLong val %d, var %s
\n,inputVar.nionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
 
PyTuple_SetItem(pTuple,nCtr,PyInt_FromLong(inputVar.nionum));
   break;
case(LONG_T):
   //printf(PyLong_FromLong val %ld, var %s
\n,inputVar.lionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
 
PyTuple_SetItem(pTuple,nCtr,PyLong_FromLong(inputVar.lionum));
   break;
case(FLOAT_T):
   //printf(PyFloat_FromDouble val %f, var %s
\n,inputVar.fionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
 
PyTuple_SetItem(pTuple,nCtr,PyFloat_FromDouble(inputVar.fionum));
   break;
case(DOUBLE_T):
   //printf(PyFloat_FromDouble val %f, var %s
\n,inputVar.dionum,pEvalFunc-pExprVarsArray[nCtr].szVarName);
 

  1   2   3   >