Re: Python bindings tutorial

2010-03-19 Thread Alf P. Steinbach

* Tim Roberts:

Dave Angel  wrote:
There's no real reason parts of an exe cannot be exported, same as a 
dll.  They are in fact the same structure.  And in fact many other files 
in the Windows environment are also the same structure, from fonts to ocx's



This is a bit off-topic, but your explanation is incorrect in some key respects, 
so (no offense) to avoid readers getting an incorrect impression:




Well, there IS a fundamental difference.  EXEs and DLLs and the like do all
have the same format.  They all have a "transfer address"


Commonly called an "entry point".

This is the term you need to know about when e.g. linking object files.




, where execution
begins.  That's the key problem.  With a DLL, the transfer address goes to
a DllMain, where certain DLL initialization is done, preparing the other
entry points for use.


Right, modulo terminology: there's only one "entry point" in a PE format file.



 With an EXE, the transfer address goes to "main".


Sorry, no, the EXE entry point goes to a routine of no arguments.

Typically, in C and C++ that's a run time library routine (e.g. with Microsoft's 
run time library mainCRTStartup or one of its cousins) which in turn calls the 
C/C++ "main", while in Pascal it runs the main program, so on.


Note that the EXE entry point is probably still incorrectly documented as 
requiring WinMain signature  --  it's the most infamous Microsoft documentation 
screw-up.




So, when you load an EXE as a DLL, you will be RUNNING the program.


Sorry, no, that's not what happens.

The Windows API LoadLibrary(Ex) knows the difference between an EXE and a DLL.

It's documented as handling both sub-formats, and it does.



 That's is usually not what you want.


If that had happened then it would be a problem, yes. Happily it doesn't happen. 
I've discussed the real problems else-thread.



Cheers & hth.,

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


Re: sqlite3, memory db and multithreading

2010-03-19 Thread Expo
On Mar 18, 1:58 pm, królewna  wrote:
> The problem is simple: I have multiple threads within one program. At
> least 2 threads have to have access to in-memory sqlite database. It is
> not possible to pass sqlite objects to those threads because an
> exception is rised:
>
> ProgrammingError: SQLite objects created in a thread can only be used in
> that same thread.The object was created in thread id -1219066176 and
> this is thread id -1224475792
>
> Is there any EASY way to use this in-memory db in many threads? Creating
> another connection is not a solution as it creates completely new db
> instead of connecting to the existing one.
>

You can put the SQLite database into a Singleton class and use a
semaphore to serialize the access to methods which writes to the
database.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re:Re: C++ code generation

2010-03-19 Thread CHEN Guang
Plug? Any evidence to say that? As long as this thread concerned, is there any 
answer more closed to the topic "C++ code generation" than mine? 

I think Stefan was telling you, in a nice way, to stop spamming every thread 
about code generation with a plug for your project. 


2010/3/17 CHEN Guang 

>> - Original Message -
>> From: "Dan Goodman"
>>
>>> I'm doing some C++ code generation using Python, and would be interested
>>> in any comments on the approach I'm taking.
>>
>> PythoidC ( http://pythoidc.googlecode.com ) is a C code generator (not C++)
> 
> It would be nice if you could start reading the posts before you answer, 
> and then try to give an answer that fits the question.
> 
> Stefan
 
I have read the post, may be I lost some words and made you misunderstand. I 
meant:
PythoidC ( http://pythoidc.googlecode.com ) is a C code generator (but not 
C++), if you find it useful, 
welcome to take a look.



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


Re: logging: local functions ==> loss of lineno

2010-03-19 Thread Peter Otten
Hellmut Weber wrote:

> your hack is exactly what I was looking for.
> It permits to configure my logging messages as I want, e.g. using
> different colors for different classes of messages.
> 
> I do not yet understand all details WHY it is working but suppose some
> study of the logging module will help me to understand.

Have  a look at Logger.findCaller() in logging/__init__.py. To find the 
calling function it does something like

for frame in walk_callstack():
if filename_of(frame) == _srcfile:
continue
return filename_lineno_and_functionname_of(frame)

The _srcfile is normally logging/__init__.py, and it is skipped because a 
user typically is interested only in what happens outside the logging 
package.

My hack under the hood changes

filename_of(frame) == _srcfile

from a string comparison to a

filename_of(frame) in set_of_files

containment test (see SrcFile.__eq__() posted above).

Peter



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


installing something to a virtualenv when it's already in site-packages

2010-03-19 Thread nbv4
I have ipython installed via apt. I can go to the command line and
type 'ipython' and it will work. If I try to install ipython to a
virtualenv, I get this:

$ pip install -E env/ ipython
Requirement already satisfied: ipython in /usr/share/pyshared
Installing collected packages: ipython
Successfully installed ipython

I want ipython in both site-packages as well as in my virtualenv. This
is bad because when I activate the virtualenv, site-packages
disappears and ipython is not available. A work around is to uninstall
ipython from apt, install to the virtualenv, then reinstall in apt. Is
there a better way?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: installing something to a virtualenv when it's already in site-packages

2010-03-19 Thread Rolando Espinoza La Fuente
On Fri, Mar 19, 2010 at 4:05 AM, nbv4  wrote:
> I have ipython installed via apt. I can go to the command line and
> type 'ipython' and it will work. If I try to install ipython to a
> virtualenv, I get this:
>
> $ pip install -E env/ ipython
> Requirement already satisfied: ipython in /usr/share/pyshared
> Installing collected packages: ipython
> Successfully installed ipython
>
> I want ipython in both site-packages as well as in my virtualenv. This
> is bad because when I activate the virtualenv, site-packages
> disappears and ipython is not available. A work around is to uninstall
> ipython from apt, install to the virtualenv, then reinstall in apt. Is
> there a better way?

I use -U (--upgrade) to force the installation within virtualenv. e.g:

$ pip install -E env/ -U ipython

Regards,

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


exp_continue

2010-03-19 Thread Pankaj
I am using pexpect module in python

I would like to know how I can use exp_continue here.

what is the alternative of exp_continue of perl/tcl in pexpect.
-- 
http://mail.python.org/mailman/listinfo/python-list


Why this compile error?

2010-03-19 Thread Jimbo
Hello

Can you help me figure out why I am getting this compile error with my
program. The error occurs right at the bottom of my code & I have
commented where it occurs.

The error is:
[QUOTE]Expected an indented block[/QUOTE]

[CODE]"""
 *Stock Data Builder*
   Algorithm:
  - Search website for stock
  - Get website HTML source code
  - Search code for target stock data(price,dividends)
  - Add data to text file
"""

import sys

# Functions
def getSource(URL, sourceBuffer):
""" Retrieve HTML source code from websitr URL &
save in sourceBuffer   """

return sourceBuffer

def getData(targetData, dataBuffer):
""" Searches the string dataBuffer for the occurence of target
data & returns that whole line"""

def writeToFile(textFile, dataBuffer):
""" Writes data in string dataBuffer to text file """

# CHANGE this to try except to catch errors
fileT = open(textFile,'a')
fileT.write(dataBuffer)
return True; # function succeeded

def writeToFile(textFile, dataList):
""" Writes data in List dataList to text file """

# CHANGE this to try except to catch errors
fileT = open(textFile,'a')

for element in dataList:
fileT.write(element)
return True; # function succeeded



# Main program loop
def main():
programEnd = False;

while (programEnd == False):
#ERROR HERE?? - Error="Expected an indented block"
main()[/CODE]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this compile error?

2010-03-19 Thread Bruno Desthuilliers

Jimbo a écrit :

Hello

Can you help me figure out why I am getting this compile error with my
program. The error occurs right at the bottom of my code & I have
commented where it occurs.

The error is:
[QUOTE]Expected an indented block[/QUOTE]

[CODE]

(snip)

# Main program loop
def main():
programEnd = False;

while (programEnd == False):
#ERROR HERE?? - Error="Expected an indented block"


Indeed. You *need* a stamement in the while block. If you want a noop, 
then use the 'pass' statement:


 while programEnd == False:
 pass


But note that as-is, this will go into an infinite loop.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why this compile error?

2010-03-19 Thread Ulrich Eckhardt
Jimbo wrote:
> Can you help me figure out why I am getting this compile error with my
> program. The error occurs right at the bottom of my code & I have
> commented where it occurs.
[...]
> def main():
> programEnd = False;
> 
> while (programEnd == False):
> #ERROR HERE?? - Error="Expected an indented block"
> main()[/CODE]

Was the comment there before or not? In any case, I believe a comment
doesn't qualify as indented block, if you want to leave the block empty
use 'pass' instead. Also, make sure you don't mix tabs and spaces, which is
also a popular way to confuse Python.

BTW: "while" doesn't need brackets, that's a C-ism. ;)

Cheers!

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: logging: local functions ==> loss of lineno

2010-03-19 Thread Jean-Michel Pichavant

Hellmut Weber wrote:

Am 11.03.2010 12:14, schrieb Peter Otten:

Hellmut Weber wrote:

Logging works very well giving the filename and line number of the 
point

where it is called. As long as I use the loggers directly.
BUT when I have to wrap the logger call in some other function, I 
always

get file name and line number of the call of the logger inside the
wrapping function.

Is there a possibility to get this information in this situation too?


The official way is probably to write a custom Logger class that 
overrides

the findCaller() method.

Below is a hack that monkey-patches the logging._srcfile attribute to 
ignore

user-specified modules in the call stack:

$ cat wrapper.py
import logging
import os
import sys

logger = logging.getLogger()

class SrcFile(object):
 def __init__(self, exclude_files):
 self.files = set(exclude_files)
 def __eq__(self, other):
 return other in self.files

def fixname(filename):
 if filename.lower().endswith((".pyc", ".pyo")):
 filename = filename[:-4] + ".py"
 return os.path.normcase(filename)

if "--monkey" in sys.argv:
 print "patching"
 logging._srcfile = SrcFile([logging._srcfile, fixname(__file__)])

def warn(*args, **kw):
 logger.warn(*args, **kw)

$ cat main.py
import logging
logging.basicConfig(format="%(filename)s<%(lineno)s>: %(message)s")
import wrapper
wrapper.warn("foo")
wrapper.warn("bar")
wrapper.warn("baz")

$ python main.py
wrapper.py<23>: foo
wrapper.py<23>: bar
wrapper.py<23>: baz

$ python main.py --monkey
patching
main.py<4>: foo
main.py<5>: bar
main.py<6>: baz

$ python -V
Python 2.6.4

Peter


Hi Peter,
your hack is exactly what I was looking for.
It permits to configure my logging messages as I want, e.g. using 
different colors for different classes of messages.


I do not yet understand all details WHY it is working but suppose some 
study of the logging module will help me to understand.


Thank you very much


_scrFile is a private attribute of the logging module. Don't change it.

As you said yourself, 'The official way is probably to write a custom 
Logger class that overrides

the findCaller() method'

JM

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


Importing modules

2010-03-19 Thread Peyman Askari
I want to write a function which imports modules the first time, and reloads 
them afterwards, but I am running into problems with global variables and exec. 
I will include a full script, but let me elaborate first.

Essentially what you need is

def import_or_reload():
 """assume we want to load or reload sys"""
 if 'sys' in dir():
  reload(sys)
else: 
  import sys

but this runs into the problem that sys is imported within the local scope of 
the function, so you insert a global statement



def import_or_reload2():

 """Add 'global sys'"""
 global sys

 if 'sys' in dir():

  reload(sys)

else: 

  import sys

'sys' is still not in dir() as dir() pertains to the local scope of the 
function, but one can get around this by creating a local modules list and 
adding the imported modules to it





def import_or_reload3():


 """Add 'global modules'"""

 global sys
 global modules



 if 'sys' in modules:


  reload(sys)


else: 


  import sys
  modules.append('sys')

now lets add a parameter to the function signature, so any module name can be 
passed as an argument and loaded





def import_or_reload4(module_name):



 """Add exec"""
 

exec 'global %s'%module_name

 global modules





 if module_name in modules:



  exec 'reload(%s)'%module_name



else: 



  exec 'import %s'%module_name

  exec 'modules.append(\'%s\')'%module_name

but this doesn't work as global does not cooperate with exec
is there a __reload__('X') function like there is an __import__(‘X’) function? 

Also is there a better way to import modules at run time?

Cheers and here is the test script in case you can't access the attachment

def a():
    global modules
    global sys
    import sys

    modules.append('sys')

def b():
    global modules
    global sys

    reload(sys)

def c(module_name):
    global modules
    exec 'global %s'%module_name
    exec 'import %s'%module_name

    modules.append(module_name)

def test():
    global modules
    global sys

    #create the module list to contain all the modules
    modules=[]

    print 'originally dir() returns:'
    print dir()

    a()
    print 'function a() properly imports the following module:'
    print sys
    print 'is %s in %s->%s'%('sys',modules,'sys' in modules)
    
    b()
    print 'function b() properly reloads the following module:'
    print sys
    print 'is %s still in %s->%s'%('sys',modules,'sys' in modules)

    try:
        c('os')
        print 'function c() properly imports the following module:'
    except:
        print 'function c() failed to import module os'
        print 'is %s in %s->%s'%('os',modules,'os' in modules)

    try:
        print os
        print 'is %s still in %s->%s'%('os',modules,'os' in modules)
    except:
        print 'os was loaded, but is not visible outside of the scope of c()'
--- On Fri, 3/19/10, python-list-requ...@python.org 
 wrote:

From: python-list-requ...@python.org 
Subject: Python-list Digest, Vol 78, Issue 192
To: python-list@python.org
Received: Friday, March 19, 2010, 7:05 AM

Send Python-list mailing list submissions to
    python-list@python.org

To subscribe or unsubscribe via the World Wide Web, visit
    http://mail.python.org/mailman/listinfo/python-list
or, via email, send a message with subject or body 'help' to
    python-list-requ...@python.org

You can reach the person managing the list at
    python-list-ow...@python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Python-list digest..."
Today's Topics:

   1. Re: import antigravity (John Bokma)
   2. Re: Truoble With A Search Script for ImageBin.org
      (Cameron Simpson)
   3. example of ssl with SimpleXMLRPCServer in 2.6? (Rowland Smith)
   4. Re: multiprocessing on freebsd (Tim Arnold)
   5. Re: Python bindings tutorial (Tim Roberts)
   6. Re: sqlite3, memory db and multithreading (John Nagle)
   7. Re: GC is very expensive: am I doing something wrong?
      (Patrick Maupin)
   8. Bug in Python APscheduler module. (anand jeyahar)
   9. Re: Bug in Python APscheduler module. (Terry Reedy)
  10. Re: Python bindings tutorial (Alf P. Steinbach)
-- 
http://mail.python.org/mailman/listinfo/python-list


  __
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/
def a():
	global modules
	global sys
	import sys

	modules.append('sys')

def b():
	global modules
	global sys

	reload(sys)

def c(module_name):
	global modules
	exec 'global %s'%module_name
	exec 'import %s'%module_name

	modules.append(module_name)

def test():
	global modules
	global sys

	#create the module list to contain all the modules
	modules=[]

	print 'originally dir() returns:'
	print dir()

	a()
	print 'function a() properly imports the following module:'
	print sys
	print 'is %s in %s->%s'%('sys',modules,'sys' in modules)
	
	b()
	print 'function b() properly reloads the following module:'
	print sys
	print 'is %s still in %s->%s'%('s

Re: sqlite3, memory db and multithreading

2010-03-19 Thread królewna

W dniu 18.03.2010 15:49, DreiJane pisze:


Principally sqlite connections (sqlite3 objects in the C-API) can be
used over multiple threads - and connections to :memory: make no
difference. There are additional parameters to open() giving fine-
tuned control. And apsw is promising a true reflection of sqlite's C-
API.


It's my fault not saying what am I using to connect to sqlite db. It's 
sqlite3 module. So there is no open() function etc. As for now I'm not 
interested in rewriting my program to use apsw so is there any possible 
way of working this out with sqlite3?


--
best regards
princess

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


Re: sqlite3, memory db and multithreading

2010-03-19 Thread królewna

W dniu 18.03.2010 23:06, Aahz pisze:


You probably need to serialize access to the database through one thread.


sqlite3 objects are not pickable so it's not proper way.

--
best regards
princess

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


Re: sqlite3, memory db and multithreading

2010-03-19 Thread królewna

W dniu 19.03.2010 08:20, Expo pisze:


You can put the SQLite database into a Singleton class and use a
semaphore to serialize the access to methods which writes to the
database.


I've tried this out but doesnt work. Still gives an error like:

ProgrammingError: SQLite objects created in a thread can only be used in 
that same thread.The object was created in thread id -1216280896 and 
this is thread id -1217107088


--
best regards
princess

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


Re: sqlite3, memory db and multithreading

2010-03-19 Thread Tim Golden

On 19/03/2010 10:56, królewna wrote:

W dniu 18.03.2010 23:06, Aahz pisze:


You probably need to serialize access to the database through one thread.


sqlite3 objects are not pickable so it's not proper way.


Is it possible you've misunderstood the meaning of the word "serialize"
here? What's being suggested isn't serialising (ie marshalling, pickling)
the data; rather, serialising the *access*, ie pushing all db requests into
a queue which is read by one thread which manages the only db connection.

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


Re: sqlite3, memory db and multithreading

2010-03-19 Thread królewna

W dniu 19.03.2010 12:10, Tim Golden pisze:

Is it possible you've misunderstood the meaning of the word "serialize"
here?

It's not possible, it just happened :)


What's being suggested isn't serialising (ie marshalling, pickling)
the data; rather, serialising the *access*, ie pushing all db requests into
a queue which is read by one thread which manages the only db connection.


That would make structure of program much more complicated. I would have 
to create queue for putting there queries and some other extra 
variables/structure to receive output from db and some more for 
controlling the execution flow of awaiting threads.

--
best regards
princess

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


python-daemon PID file

2010-03-19 Thread Kamil Wasilewski
Hi all,

Im trying to get a daemon up and running but have been tripping on every
stone along the way, this last one however i cant manage to get by.
versions:
Python 2.5.2
python-daemon-1.5.5
Debian Linux 2.6.26-2-686

Some of the problems ive run into:

import daemon
with daemon.DaemonContext():
do_main_program()

doesnt work on my version of Python 2.5.2 so im using

import daemon
import grp
import pwd

diablo.uid = pwd.getpwnam('nobody').pw_uid
diablo.gid = grp.getgrnam('nogroup').gr_gid

daemon.__enter__()
main()
daemon.__exit__()

Next I had problems with Initd starting the daemon because i forgot to chmod
it to 755.

The final problem is with the PID file and getting a correct PID written.
Initially there was no file being created, so i thought i needed the
--make-pidfile option in start-stop-daemon, but ive since learned that the
daemon should create and edit the file, not s-s-d as it inserts the PID of
the parent process, which is not the correct PID of the final daemon.

So i added to my daemon:

import lockfile
daemon.DaemonContext(pidfile=lockfile.FileLock('/var/run/mydaemon.pid'),)


which didnt start the daemon at all as i created the pid file as root, and
was setting the Daemon to run as nobody,nogroup so it was dieing silently.
Im running it as root for now to see if i can get a correct pid in the pid
file.
However no PID file is created, and if i create one its not populated with a
PID.

In summary: I cant get a PID file created or pupulated with a PID by
python-daemon.
Anyone see something that im doing wrong? ive been sitting on this way too
long. I looked at the python-daemon source and i cant see where the pid is
inserted into the pid file, only where it __enter__'s the lockfile so is
this missing?
Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: should writing Unicode files be so slow

2010-03-19 Thread djc
Ben Finney wrote:
> djc  writes:
> 
>> I have a simple program to read a text (.csv) file
> 
> Could you please:
> 
> * simplify it further: make a minimal version that demonstrates the
>   difference you're seeing, without any extraneous stuff that doesn't
>   appear to affect the result.
> 
> * make it complete: the code you've shown doesn't do anything except
>   define some functions.
> 
> In other words: please reduce it to a complete, minimal example that we
> can run to see the same behaviour you're seeing.
> 


It is the minimal example. The only thing omited is the opt.parse code that
 calls _file_to_files(infile, outfile_prefix, column, sep):


-- 
David Clark, MSc, PhD.  UCL Centre for Publishing
Gower Str London WCIE 6BT
What sort of web animal are you?

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


Re: Importing modules

2010-03-19 Thread Dave Angel



Peyman Askari wrote:

I want to write a function which imports modules the first time, and reloads 
them afterwards, but I am running into problems with global variables and exec. 
I will include a full script, but let me elaborate first.

Essentially what you need is

def import_or_reload():
 """assume we want to load or reload sys"""
 if 'sys' in dir():
  reload(sys)
else: 
  import sys


but this runs into the problem that sys is imported within the local scope of 
the function, so you insert a global statement



def import_or_reload2():

 """Add 'global sys'"""
 global sys

 if 'sys' in dir():

  reload(sys)

else: 


  import sys

'sys' is still not in dir() as dir() pertains to the local scope of the 
function, but one can get around this by creating a local modules list and 
adding the imported modules to it





def import_or_reload3():


 """Add 'global modules'"""

 global sys
 global modules



 if 'sys' in modules:


  reload(sys)


else: 



  import sys
  modules.append('sys')

now lets add a parameter to the function signature, so any module name can be 
passed as an argument and loaded





def import_or_reload4(module_name):



 """Add exec"""
 


exec 'global %s'%module_name

 global modules





 if module_name in modules:



  exec 'reload(%s)'%module_name



else: 




  exec 'import %s'%module_name

  exec 'modules.append(\'%s\')'%module_name

but this doesn't work as global does not cooperate with exec
is there a __reload__('X') function like there is an __import__(‘X’) function? 


Also is there a better way to import modules at run time?

Cheers and here is the test script in case you can't access the attachment

def a():
global modules
global sys
import sys

modules.append('sys')

def b():
global modules
global sys

reload(sys)

def c(module_name):
global modules
exec 'global %s'%module_name
exec 'import %s'%module_name

modules.append(module_name)

def test():
global modules
global sys

#create the module list to contain all the modules
modules=[]

print 'originally dir() returns:'
print dir()

a()
print 'function a() properly imports the following module:'
print sys
print 'is %s in %s->%s'%('sys',modules,'sys' in modules)

b()

print 'function b() properly reloads the following module:'
print sys
print 'is %s still in %s->%s'%('sys',modules,'sys' in modules)

try:
c('os')
print 'function c() properly imports the following module:'
except:
print 'function c() failed to import module os'
print 'is %s in %s->%s'%('os',modules,'os' in modules)

try:
print os
print 'is %s still in %s->%s'%('os',modules,'os' in modules)
except:
print 'os was loaded, but is not visible outside of the scope of c()'
--- On Fri, 3/19/10, python-list-requ...@python.org 
 wrote:

From: python-list-requ...@python.org 
Subject: Python-list Digest, Vol 78, Issue 192
To: python-list@python.org
Received: Friday, March 19, 2010, 7:05 AM


(When starting a new thread, create a new message addressed to   
python-list@python.org, do not just reply to an existing message, (or 
digest, which you did here).  Some people actually try to follow 
threads, and the software to do that uses more information than just the 
message subject)


First comment.  I would seriously try to avoid using reload() in 
production code.  The problems that can result are subtle.  I use it for 
debugging sessions, but not in real programs.


But I'll assume you have a use case (which would have been good to 
explain), and have rejected the other possibilities.


Next, I'll point out that reloading sys isn't desirable, and it's one of 
the specifically proscribed modules for reloading.  But probably you 
weren't really using sys, you were just sanitizing the code since you 
knew we all had sys.


Next, if your reason for reloading is that you just changed the module 
programmatically, and it might have been loaded by some other module (as 
sys is, for example, long before your code starts), then you're not 
checking in the right place.  Instead of looking at your own global 
space, you should be looking at sys.modules to decide whether something 
has been loaded.


Perhaps the function you're looking for is imp.load_module()

DaveA

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


Re: logging: local functions ==> loss of lineno

2010-03-19 Thread Peter Otten
Jean-Michel Pichavant wrote:

> Hellmut Weber wrote:
>> Am 11.03.2010 12:14, schrieb Peter Otten:
>>> Hellmut Weber wrote:
>>>
 Logging works very well giving the filename and line number of the
 point
 where it is called. As long as I use the loggers directly.
 BUT when I have to wrap the logger call in some other function, I
 always
 get file name and line number of the call of the logger inside the
 wrapping function.

 Is there a possibility to get this information in this situation too?
>>>
>>> The official way is probably to write a custom Logger class that
>>> overrides
>>> the findCaller() method.
>>>
>>> Below is a hack that monkey-patches the logging._srcfile attribute to
>>> ignore
>>> user-specified modules in the call stack:
>>>
>>> $ cat wrapper.py
>>> import logging
>>> import os
>>> import sys
>>>
>>> logger = logging.getLogger()
>>>
>>> class SrcFile(object):
>>>  def __init__(self, exclude_files):
>>>  self.files = set(exclude_files)
>>>  def __eq__(self, other):
>>>  return other in self.files
>>>
>>> def fixname(filename):
>>>  if filename.lower().endswith((".pyc", ".pyo")):
>>>  filename = filename[:-4] + ".py"
>>>  return os.path.normcase(filename)
>>>
>>> if "--monkey" in sys.argv:
>>>  print "patching"
>>>  logging._srcfile = SrcFile([logging._srcfile, fixname(__file__)])
>>>
>>> def warn(*args, **kw):
>>>  logger.warn(*args, **kw)
>>>
>>> $ cat main.py
>>> import logging
>>> logging.basicConfig(format="%(filename)s<%(lineno)s>: %(message)s")
>>> import wrapper
>>> wrapper.warn("foo")
>>> wrapper.warn("bar")
>>> wrapper.warn("baz")
>>>
>>> $ python main.py
>>> wrapper.py<23>: foo
>>> wrapper.py<23>: bar
>>> wrapper.py<23>: baz
>>>
>>> $ python main.py --monkey
>>> patching
>>> main.py<4>: foo
>>> main.py<5>: bar
>>> main.py<6>: baz
>>>
>>> $ python -V
>>> Python 2.6.4
>>>
>>> Peter
>>
>> Hi Peter,
>> your hack is exactly what I was looking for.
>> It permits to configure my logging messages as I want, e.g. using
>> different colors for different classes of messages.
>>
>> I do not yet understand all details WHY it is working but suppose some
>> study of the logging module will help me to understand.
>>
>> Thank you very much
>>
> _scrFile is a private attribute of the logging module. Don't change it.
> 
> As you said yourself, 'The official way is probably to write a custom
> Logger class that overrides
> the findCaller() method'

OK, I tried the this approach, too:

import logging
import os
import sys

from logging import currentframe

def fixname(filename):
if filename.lower().endswith((".pyc", ".pyo")):
filename = filename[:-4] + ".py"
return os.path.normcase(filename)

class MyLogger(logging.Logger):
exclude_files = set([logging._srcfile, fixname(__file__)])

def findCaller(self):
"""
Find the stack frame of the caller so that we can note the source
file name, line number and function name.
"""
f = currentframe()
#On some versions of IronPython, currentframe() returns None if
#IronPython isn't run with -X:Frames.
if f is not None:
f = f.f_back
rv = "(unknown file)", 0, "(unknown function)"
while hasattr(f, "f_code"):
co = f.f_code
filename = os.path.normcase(co.co_filename)
if filename in self.exclude_files:
f = f.f_back
continue
rv = (filename, f.f_lineno, co.co_name)
break
return rv

if "--custom-logger" in sys.argv:
print "setting custom logger"
logging.setLoggerClass(MyLogger)
logging.root = MyLogger("root", logging.WARNING)

logger = logging.getLogger()
def warn(*args, **kw):
logger.warn(*args, **kw)

I had to duplicate the original findCaller() method with only one line 
changed. This means I have now some code duplication and I still have to 
monitor future changes in the logging source.

In this case the "official way" seems to be more intrusive than the "hack".

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


Re: should writing Unicode files be so slow

2010-03-19 Thread Ben Finney
djc  writes:

> Ben Finney wrote:
> > Could you please:
> > 
> > * simplify it further: make a minimal version that demonstrates the
> >   difference you're seeing, without any extraneous stuff that doesn't
> >   appear to affect the result.
> > 
> > * make it complete: the code you've shown doesn't do anything except
> >   define some functions.
> > 
> > In other words: please reduce it to a complete, minimal example that we
> > can run to see the same behaviour you're seeing.
>
> It is the minimal example. The only thing omited is the opt.parse code
> that calls _file_to_files(infile, outfile_prefix, column, sep):

What happens, then, when you make a smaller program that deals with only
one file?

What happens when you make a smaller program that only reads the file,
and doesn't write any? Or a different program that only writes a file,
and doesn't read any?

It's these sort of reductions that will help narrow down exactly what
the problem is. Do make sure that each example is also complete (i.e.
can be run as is by someone who uses only that code with no additions).

-- 
 \   “To have the choice between proprietary software packages, is |
  `\  being able to choose your master. Freedom means not having a |
_o__)master.” —Richard M. Stallman, 2007-05-16 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C-API PyObject_Call

2010-03-19 Thread moerchendiser2k3
In my case I call a funcion and I would like to get the line
where the function returned.

for instance:


def my_function():
return 3

So I would like to get line 2(in C) somehow.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: logging: local functions ==> loss of lineno

2010-03-19 Thread Jean-Michel Pichavant

Peter Otten wrote:

Jean-Michel Pichavant wrote:

  

Hellmut Weber wrote:


Am 11.03.2010 12:14, schrieb Peter Otten:
  

Hellmut Weber wrote:



Logging works very well giving the filename and line number of the
point
where it is called. As long as I use the loggers directly.
BUT when I have to wrap the logger call in some other function, I
always
get file name and line number of the call of the logger inside the
wrapping function.

Is there a possibility to get this information in this situation too?
  

The official way is probably to write a custom Logger class that
overrides
the findCaller() method.

Below is a hack that monkey-patches the logging._srcfile attribute to
ignore
user-specified modules in the call stack:

$ cat wrapper.py
import logging
import os
import sys

logger = logging.getLogger()

class SrcFile(object):
 def __init__(self, exclude_files):
 self.files = set(exclude_files)
 def __eq__(self, other):
 return other in self.files

def fixname(filename):
 if filename.lower().endswith((".pyc", ".pyo")):
 filename = filename[:-4] + ".py"
 return os.path.normcase(filename)

if "--monkey" in sys.argv:
 print "patching"
 logging._srcfile = SrcFile([logging._srcfile, fixname(__file__)])

def warn(*args, **kw):
 logger.warn(*args, **kw)

$ cat main.py
import logging
logging.basicConfig(format="%(filename)s<%(lineno)s>: %(message)s")
import wrapper
wrapper.warn("foo")
wrapper.warn("bar")
wrapper.warn("baz")

$ python main.py
wrapper.py<23>: foo
wrapper.py<23>: bar
wrapper.py<23>: baz

$ python main.py --monkey
patching
main.py<4>: foo
main.py<5>: bar
main.py<6>: baz

$ python -V
Python 2.6.4

Peter


Hi Peter,
your hack is exactly what I was looking for.
It permits to configure my logging messages as I want, e.g. using
different colors for different classes of messages.

I do not yet understand all details WHY it is working but suppose some
study of the logging module will help me to understand.

Thank you very much

  

_scrFile is a private attribute of the logging module. Don't change it.

As you said yourself, 'The official way is probably to write a custom
Logger class that overrides
the findCaller() method'



OK, I tried the this approach, too:

import logging
import os
import sys

from logging import currentframe

def fixname(filename):
if filename.lower().endswith((".pyc", ".pyo")):
filename = filename[:-4] + ".py"
return os.path.normcase(filename)

class MyLogger(logging.Logger):
exclude_files = set([logging._srcfile, fixname(__file__)])

def findCaller(self):
"""
Find the stack frame of the caller so that we can note the source
file name, line number and function name.
"""
f = currentframe()
#On some versions of IronPython, currentframe() returns None if
#IronPython isn't run with -X:Frames.
if f is not None:
f = f.f_back
rv = "(unknown file)", 0, "(unknown function)"
while hasattr(f, "f_code"):
co = f.f_code
filename = os.path.normcase(co.co_filename)
if filename in self.exclude_files:
f = f.f_back
continue
rv = (filename, f.f_lineno, co.co_name)
break
return rv

if "--custom-logger" in sys.argv:
print "setting custom logger"
logging.setLoggerClass(MyLogger)
logging.root = MyLogger("root", logging.WARNING)

logger = logging.getLogger()
def warn(*args, **kw):
logger.warn(*args, **kw)

I had to duplicate the original findCaller() method with only one line 
changed. This means I have now some code duplication and I still have to 
monitor future changes in the logging source.


In this case the "official way" seems to be more intrusive than the "hack".

Peter

You are still accessing the private attribute of the  module logging.
My previous remark was misleading, in fact there's nothing you can do 
about it.
_srcfile is not meant to be used elsewhere than in the logging module 
itself.


However, I don't wanna sound like I'm rejecting this solution, 1st the 
OP is satisified with it and since this solution is working, it is still 
more helpful than anyone noticing that you've accessed a private 
attribute (some will successfully argue that python allows to do so).


At the very begining of this thread I've provided a complete different 
approach, instead of using the builtin 'filename' and 'lineno' field of 
the logging, use custom fileds with the extra parameter.


It has also some drawbacks but could be a possible alternative.

Cheers,

JM

in test.py:

import logging
import inspect

_logger = logging.getLogger(__name__)

class Foo:
  def __init__(self):
  self._logger = _logger

  def info(self, msg):
  # this is the method you don't want to appear in the logs, 
instead the lineno and filename of this method caller

  previousFrame = inspect.currentframe

Re: SOLUTION

2010-03-19 Thread królewna
I have found the solution. I dont know why in python documentation there 
is not a single word about this option. I've found it in pysqlite doc 
site. So we have to add a new keyword argument to connection function 
and we will be able to create cursors out of it in different thread. So use:


sqlite.connect(":memory:", check_same_thread = False)

works out perfectly for me. Of course from now on me need to take care 
of safe multithreading access to the db. Anyway thx all for trying to help.


--
best regards
princess
--
http://mail.python.org/mailman/listinfo/python-list


Re: C-API PyObject_Call

2010-03-19 Thread Stefan Behnel

moerchendiser2k3, 19.03.2010 14:12:

In my case I call a funcion and I would like to get the line
where the function returned.

for instance:


def my_function():
 return 3

So I would like to get line 2(in C) somehow.


Ok, so you're looking for the C-level trace function in CPython, I guess.

Could you explain why you want to know the line number?

Stefan

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


What is pkg-config for ?

2010-03-19 Thread Johny
Can anyone explain what is pkg-config  for?ANd how can I find it in
Windows?
Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Importing v reloading modules modules

2010-03-19 Thread Peter Peyman Puk

Hello

I submitted this earlier today, but I was not clear enough, so I am  
posting it again.


I am running a simulator written in python. The simulator has a small  
TextView (actually a SourceView) widget which lets the user writes  
scripts, and when they are satisfied they can execute that script to  
get results. For arguments sake, we write a simple script and save it  
as A.py and we import it and execute it more or less like so.


import A

#assume there is a function called test() in module A
A.test()


Then the user modifies the contents of A.py and saves it again (to  
A.py) now all we have to do is the following


if 'A' in dir():
 reload(A)
else:
 import A

A.test()

But since the user chooses the file name, and not me, the programmer,  
the module names will vary. Let's assume the module names are loaded  
and stored in the list module_names, and we iterate over them, and  
pass them as arguments to a function to import or reload each model as  
appropriate


def import_or_reload(module_name):

 if module_name in sys.modules:
  #somehow reload
 else:
  #somehow import

does anyone know how to deal with the reload and import as they both  
present problems since module_name is a string, and to reload  
something along the lines of the below must be executed


exec 'reload(%s)'%module_name

and then we also have to deal with the scope issue since the loaded  
module will be local and not global. I can execute something like so


exec 'global %s'%module_name

but that does not work very well with exec

any suggestions?

Cheers


Peyman

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


Re: should writing Unicode files be so slow

2010-03-19 Thread djc
Ben Finney wrote:

> What happens, then, when you make a smaller program that deals with only
> one file?
> 
> What happens when you make a smaller program that only reads the file,
> and doesn't write any? Or a different program that only writes a file,
> and doesn't read any?
> 
> It's these sort of reductions that will help narrow down exactly what
> the problem is. Do make sure that each example is also complete (i.e.
> can be run as is by someone who uses only that code with no additions).
> 


The program reads one csv file of 9,293,271 lines.
869M wb.csv
  It  creates  set  of  files containing the  same  lines  but  where  each
 output file in the set  contains only those lines where the value of a
particular column is the same, the number of  output files will depend on
the number of distinct values  in that column In the example that results
in 19 files

74M tt_11696870405.txt
94M tt_18762175493.txt
15M  tt_28668070915.txt
12M tt_28673313795.txt
15M  tt_28678556675.txt
11M  tt_28683799555.txt
12M  tt_28689042435.txt
15M  tt_28694285315.txt
7.3M  tt_28835845125.txt
6.8M tt_28842136581.txt
12M  tt_28848428037.txt
11M  tt_28853670917.txt
12M  tt_28858913797.txt
15M  tt_28864156677.txt
11M  tt_28869399557.txt
11M  tt_28874642437.txt
283M  tt_31002203141.txt
259M  tt_5282691.txt
45 2010-03-19 17:00 tt_taskid.txt

changing
with open(filename, 'rU') as tabfile:
to
with codecs.open(filename, 'rU', 'utf-8', 'backslashreplace') as tabfile:

and
with open(outfile, 'wt') as out_part:
to
with codecs.open(outfile, 'w', 'utf-8') as out_part:

causes a program that runs  in
43 seconds to take 4 minutes to process the same data. In this particular
case  that is  not very  important, any unicode strings in the data are not
worth troubling over and I have already  spent more  time satisfying
curiousity  that  will  ever  be  required  to  process  the dataset  in
future.  But  I have another  project  in  hand where not only is the
unicode significant but the files are very much larger. Scale up the
problem and the difference between 4 hours and 24 become a matter worth
some attention.



-- 
David Clark, MSc, PhD.  UCL Centre for Publishing
Gower Str London WCIE 6BT
What sort of web animal are you?

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


[python3]

2010-03-19 Thread Kevin Adams
Greetings!

Please forgive me if im posting this to the wrong group.

I'm new to Python, learning Python3 from the O'rielly "Learning
Python" book.  Reading
about operator overloading, specifically __getitem__.   I put together
a small bit of code to
do some experimenting and threw in a timer so i can see it do its
thing.  For some reason
the time.sleep(x) function doesnt work when my print function includes
"end=''".

Thanks in advance for any help.


---code---

class TestClass():
def __init__(self):
self.data = "I was far from home and the spell of the eastern
sea was upon me."

def __getitem__(self,i):
return self.data[i]


import time

if __name__ == "__main__":

me = TestClass()
for x in me:
print(x,end='')  #if i remove the 'end='''  it performs as i'd
expect
time.sleep(int(2))

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


Re: Importing v reloading modules modules

2010-03-19 Thread Terry Reedy
Reload is effectively deprecated in 2.x and removed in 3.x, so I 
recommend not using it.


Deleting an entry from sys.modules should force recreation with an 
import. Just make sure all other references are also gone so that the 
module object can be deleted.


The following shows how to import by name in string and globally from 
within a function.


def f():
global itertools
itertools = __import__('itertools')

f()
print(itertools) # 3.1

#

Terry Jan Reedy

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


Re: [python3]

2010-03-19 Thread Steve Holden
Kevin Adams wrote:
> Greetings!
> 
> Please forgive me if im posting this to the wrong group.
> 
> I'm new to Python, learning Python3 from the O'rielly "Learning
> Python" book.  Reading
> about operator overloading, specifically __getitem__.   I put together
> a small bit of code to
> do some experimenting and threw in a timer so i can see it do its
> thing.  For some reason
> the time.sleep(x) function doesnt work when my print function includes
> "end=''".
> 
> Thanks in advance for any help.
> 
> 
Try the following changes:

> ---code---
> 
> class TestClass():
> def __init__(self):
> self.data = "I was far from home and the spell of the eastern
> sea was upon me."
> 
> def __getitem__(self,i):
> return self.data[i]
> 
> 
> import time

import sys
> 
> if __name__ == "__main__":
> 
> me = TestClass()
> for x in me:
> print(x,end='')  #if i remove the 'end='''  it performs as i'd
> expect
  sys.stdout.flush()

> time.sleep(int(2))
> 

It may just be that the output is being held in buffers until the
program terminates. the fluch() methof pushes it straight out.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: [python3]

2010-03-19 Thread Krister Svanlund
On Fri, Mar 19, 2010 at 6:42 PM, Kevin Adams  wrote:
> Greetings!
>
> Please forgive me if im posting this to the wrong group.
>
> I'm new to Python, learning Python3 from the O'rielly "Learning
> Python" book.  Reading
> about operator overloading, specifically __getitem__.   I put together
> a small bit of code to
> do some experimenting and threw in a timer so i can see it do its
> thing.  For some reason
> the time.sleep(x) function doesnt work when my print function includes
> "end=''".
>
> Thanks in advance for any help.
>
>
> ---code---
>
> class TestClass():
>    def __init__(self):
>        self.data = "I was far from home and the spell of the eastern
> sea was upon me."
>
>    def __getitem__(self,i):
>        return self.data[i]
>
>
> import time
>
> if __name__ == "__main__":
>
>    me = TestClass()
>    for x in me:
>        print(x,end='')  #if i remove the 'end='''  it performs as i'd
> expect
>        time.sleep(int(2))
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I'm guessing wildly here but I think you have to flush the output.
Can't remember how right now but it won't take much googling for it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [python3]

2010-03-19 Thread Terry Reedy

On 3/19/2010 1:42 PM, Kevin Adams wrote:


Please forgive me if im posting this to the wrong group.


Just the right place. I would suggest a more informative title in the 
future though. 'Problem with print and sleep.', for instance.




I'm new to Python, learning Python3 from the O'rielly "Learning
Python" book.  Reading
about operator overloading, specifically __getitem__.   I put together
a small bit of code to
do some experimenting and threw in a timer so i can see it do its
thing.


Good idea. Welcome to Python.


 For some reason
the time.sleep(x) function doesnt work when my print function includes
"end=''".


I cut your code, pasted it into IDLE edit window, and made two minor 
modifications. For me (3.1, winxp) it works perfectly either way -- 
chars print at 1 sec intervals either vertically or horizonatally. So I 
am not sure what you are claiming.


Thanks in advance for any help.


---code---

class TestClass():
 def __init__(self):
 self.data = "I was far from home and the spell of the eastern
sea was upon me."


I shortened string


 def __getitem__(self,i):
 return self.data[i]

import time

if __name__ == "__main__":

 me = TestClass()
 for x in me:
 print(x,end='')  #if i remove the 'end='''  it performs as i'd
expect
 time.sleep(int(2))


int(2) == 2, so delete irrelevant call, and 1 sec is enough

Terry Jan Reedy


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


Re: [python3]

2010-03-19 Thread Kevin Adams
On Mar 19, 2:21 pm, Terry Reedy  wrote:
> On 3/19/2010 1:42 PM, Kevin Adams wrote:
>
> > Please forgive me if im posting this to the wrong group.
>
> Just the right place. I would suggest a more informative title in the
> future though. 'Problem with print and sleep.', for instance.
>
>
>
> > I'm new to Python, learning Python3 from the O'rielly "Learning
> > Python" book.  Reading
> > about operator overloading, specifically __getitem__.   I put together
> > a small bit of code to
> > do some experimenting and threw in a timer so i can see it do its
> > thing.
>
> Good idea. Welcome to Python.
>
> >  For some reason
> > the time.sleep(x) function doesnt work when my print function includes
> > "end=''".
>
> I cut your code, pasted it into IDLE edit window, and made two minor
> modifications. For me (3.1, winxp) it works perfectly either way --
> chars print at 1 sec intervals either vertically or horizonatally. So I
> am not sure what you are claiming.
>
>
>
> > Thanks in advance for any help.
>
> > ---code---
>
> > class TestClass():
> >      def __init__(self):
> >          self.data = "I was far from home and the spell of the eastern
> > sea was upon me."
>
> I shortened string
>
>
>
> >      def __getitem__(self,i):
> >          return self.data[i]
>
> > import time
>
> > if __name__ == "__main__":
>
> >      me = TestClass()
> >      for x in me:
> >          print(x,end='')  #if i remove the 'end='''  it performs as i'd
> > expect
> >          time.sleep(int(2))
>
> int(2) == 2, so delete irrelevant call, and 1 sec is enough
>
> Terry Jan Reedy

Thanks to all!!!

It did indeed turn out to be a need to flush the stdout.

K

ps.  sorry for the brief subject, i wasnt finish and hit send before
comming back to it.

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


Reserving binary Buffer for struct.pack_into() of exact length

2010-03-19 Thread Hans Müller

Hello,

I need some buffers, which are beeing concatenated an then send via 
the socket() function.
My problem is that create_string_buffer() creates a buffer which ist 
one byte too long (for a \0 char). But when I concatenate some of 
these buffers to a long one to be transfered, the extra byte is 
corrupting the resulting buffer.

Of course I could slice the buffer before being concatended, but
this looks ugly to me.
Is there a way to create a writeable buffer object without the \0 byte ?

Thanks a lot

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


Re: [python3]

2010-03-19 Thread Terry Reedy

On 3/19/2010 2:17 PM, Steve Holden wrote:

Kevin Adams wrote:

Greetings!

Please forgive me if im posting this to the wrong group.

I'm new to Python, learning Python3 from the O'rielly "Learning
Python" book.  Reading
about operator overloading, specifically __getitem__.   I put together
a small bit of code to
do some experimenting and threw in a timer so i can see it do its
thing.  For some reason
the time.sleep(x) function doesnt work when my print function includes
"end=''".

Thanks in advance for any help.



Try the following changes:


---code---

class TestClass():
 def __init__(self):
 self.data = "I was far from home and the spell of the eastern
sea was upon me."

 def __getitem__(self,i):
 return self.data[i]


import time


import sys


if __name__ == "__main__":

 me = TestClass()
 for x in me:
 print(x,end='')  #if i remove the 'end='''  it performs as i'd
expect

   sys.stdout.flush()


 time.sleep(int(2))


It may just be that the output is being held in buffers until the
program terminates. the fluch() methof pushes it straight out.


The IDLE Shell window must get print output back from the pythonw 
process without buffering. When I cut and pasted from IDLE editor to 
standard interpreter window, output was bunched after several seconds. 
OP should have started with short string and minimal delay so output 
would not take a whole minute. Flushing fixed problem in standard 
interpreter. I will have to remember that testing partial line output in 
IDLE does not show how it will act elsewhere.


Kevin: when reporting a problem, be more specific as to what 'does not 
work' means.


Terry Jan Reedy

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


Re: Reserving binary Buffer for struct.pack_into() of exact length

2010-03-19 Thread Emile van Sebille

On 3/19/2010 11:46 AM Hans Müller said...

Hello,

I need some buffers, which are beeing concatenated an then send via the
socket() function.
My problem is that create_string_buffer() creates a buffer which ist one
byte too long (for a \0 char). But when I concatenate some of these
buffers to a long one to be transfered, the extra byte is corrupting the
resulting buffer.
Of course I could slice the buffer before being concatended, but
this looks ugly to me.
Is there a way to create a writeable buffer object without the \0 byte ?

Thanks a lot

Hans


>>> p = create_string_buffer(5)
>>> p.raw='hello'
>>> p.value
'hello'

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


"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI Girls" "Sexy CHINESE Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL Girls" ON

2010-03-19 Thread saima81
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON
www.sexyandpretty-girls.blogspot.com   "Sexy Girls" "Sexy
Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI
Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls"
"Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON www.sexyandpretty-girls.blogspot.com
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON
www.sexyandpretty-girls.blogspot.com   "Sexy Girls" "Sexy
Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI
Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls"
"Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON www.sexyandpretty-girls.blogspot.com
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON
www.sexyandpretty-girls.blogspot.com   "Sexy Girls" "Sexy
Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI
Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls"
"Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON www.sexyandpretty-girls.blogspot.com
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON
www.sexyandpretty-girls.blogspot.com   "Sexy Girls" "Sexy
Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI
Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls"
"Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON www.sexyandpretty-girls.blogspot.com
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON
www.sexyandpretty-girls.blogspot.com   "Sexy Girls" "Sexy
Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI
Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls"
"Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON www.sexyandpretty-girls.blogspot.com
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON
www.sexyandpretty-girls.blogspot.com   "Sexy Girls" "Sexy
Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI
Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls"
"Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON www.sexyandpretty-girls.blogspot.com
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON
www.sexyandpretty-girls.blogspot.com   "Sexy Girls" "Sexy
Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI
Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls"
"Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON www.sexyandpretty-girls.blogspot.com
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON
www.sexyandpretty-girls.blogspot.com   "Sexy Girls" "Sexy
Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI
Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls"
"Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON www.sexyandpretty-girls.blogspot.com
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON
www.sexyandpretty-girls.blogspot.com   "Sexy Girls" "Sexy
Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI
Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls"
"Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON www.sexyandpretty-girls.blogspot.com
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON
www.sexyandpretty-girls.blogspot.com   "Sexy Girls" "Sexy
Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls" "Sexy PAKISTANI
Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy JAPANESE Girls"
"Sexy KOREAN Girls" "Sexy MODEL  Girls"  ON www.sexyandpretty-girls.blogspot.com
"Sexy Girls" "Sexy Indian Girls" "Sexy USA Girls" "Sexy RUSSIAN Girls"
"Sexy PAKISTANI Girls" "Sexy CHINESE  Girls" "Sexy THAI Girls" "Sexy
JAPANESE Girls" "Se

Re: Truoble With A Search Script for ImageBin.org

2010-03-19 Thread Brandon C
*grumble* *grumble* *grumble*

oops, sorry ... my mistake

it turns out that i misspelled one of the values and left another one out.
 here's the working script now for anyone who may care:

import urllib
import urllib2
url = 'http://imagebin.org/index.php?page=search'
values = {'search_for' : 'blah', 'field' : 'nickname', 'mode' : 'search'}
data = urllib.urlencode(values)
request = urllib2.Request(url, data)
response = urllib2.urlopen(request)
page = response.read()
print page

the value of 'field' is 'nickname, not 'Nickname';
and i left out the 'mode' one

tks for yalls help
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is pkg-config for ?

2010-03-19 Thread Gabriel Genellina

En Fri, 19 Mar 2010 12:32:32 -0300, Johny  escribió:


Can anyone explain what is pkg-config  for?ANd how can I find it in
Windows?


Go to http://www.google.com/
Enter "pkg-config windows" (without quotes) in the input box.
Press the Search button.
You'll get this site at the top, or near it:
http://pkg-config.freedesktop.org/wiki/

I fail to see how is this relevant to Python...

--
Gabriel Genellina

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


script to upload file using POST form

2010-03-19 Thread Brandon C
got another question for yall wonderful minds; how can i upload a file to a
server that uses a POST form?

here's the link to the form that i'd like to be able to upload a file:
http://imagebin.org/index.php?page=add

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


Re: should writing Unicode files be so slow

2010-03-19 Thread Gabriel Genellina

En Fri, 19 Mar 2010 14:18:17 -0300, djc  escribió:

Ben Finney wrote:


What happens, then, when you make a smaller program that deals with only
one file?

What happens when you make a smaller program that only reads the file,
and doesn't write any? Or a different program that only writes a file,
and doesn't read any?

It's these sort of reductions that will help narrow down exactly what
the problem is. Do make sure that each example is also complete (i.e.
can be run as is by someone who uses only that code with no additions).




The program reads one csv file of 9,293,271 lines.
869M wb.csv
  It  creates  set  of  files containing the  same  lines  but  where   
each

 output file in the set  contains only those lines where the value of a
particular column is the same, the number of  output files will depend on
the number of distinct values  in that column In the example that results
in 19 files

changing
with open(filename, 'rU') as tabfile:
to
with codecs.open(filename, 'rU', 'utf-8', 'backslashreplace') as tabfile:

and
with open(outfile, 'wt') as out_part:
to
with codecs.open(outfile, 'w', 'utf-8') as out_part:

causes a program that runs  in
43 seconds to take 4 minutes to process the same data. In this particular
case  that is  not very  important, any unicode strings in the data are  
not

worth troubling over and I have already  spent more  time satisfying
curiousity  that  will  ever  be  required  to  process  the dataset  in
future.  But  I have another  project  in  hand where not only is the
unicode significant but the files are very much larger. Scale up the
problem and the difference between 4 hours and 24 become a matter worth
some attention.


Ok. Your test program is too large to determine what's going on. Try to  
determine first *which* part is slow:


- reading: measure the time it takes only to read a file, with open() and  
codecs.open()
It might be important the density of non-ascii characters and their  
relative code points (as utf-8 is much more efficient for ASCII data than,  
say, Hanzi)
- processing: measure the time it takes the processing part (fed with str  
vs unicode data)
- writing: measure the time it takes only to write a file, with open() and  
codecs.open()


Only then one can focus on optimizing the bottleneck.

--
Gabriel Genellina

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


RELEASED Python 2.6.5

2010-03-19 Thread Barry Warsaw
On behalf of the Python community, I'm happy to announce the availability of
Python 2.6.5 final.  This is the latest production-ready version in the Python
2.6 series.

Python 2.6.5 fixes dozens of issues in the core, built-in modules, libraries,
and documentation since Python 2.6.4 was released back in October 2009.  We
highly recommend that you upgrade to Python 2.6.5.

Please see the NEWS file for all the gory details.

http://www.python.org/download/releases/2.6.5/NEWS.txt

Source tarballs and the Windows installers can be downloaded from the Python
2.6.5 page.  The Mac OS X disk image will be uploaded soon.

http://www.python.org/download/releases/2.6.5/

For more information on Python 2.6 in general, please see

http://docs.python.org/whatsnew/2.6.html

Please report bugs for any Python version in the Python tracker.

http://bugs.python.org

Enjoy,
-Barry

Barry Warsaw
ba...@python.org
Python 2.6 Release Manager
(on behalf of the entire python-dev team)


signature.asc
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: script to upload file using POST form

2010-03-19 Thread Gabriel Genellina
En Fri, 19 Mar 2010 19:02:25 -0300, Brandon C   
escribió:


got another question for yall wonderful minds; how can i upload a file  
to a

server that uses a POST form?

here's the link to the form that i'd like to be able to upload a file:
http://imagebin.org/index.php?page=add


Unfortunately it isn't as easy as it should be:
http://bugs.python.org/issue3244

--
Gabriel Genellina

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


Re: should writing Unicode files be so slow

2010-03-19 Thread Ben Finney
"Gabriel Genellina"  writes:

> Ok. Your test program is too large to determine what's going on. Try
> to determine first *which* part is slow:

Right. This is done by the diagnostic technique of writing *new*,
minimal, complete programs that exercise each piece of the functionality
separately.

You're not tinkering with the existing program that's misbehaving;
you're trying to *recreate* the misbehaviour under a simpler
environment.

Hope that helps. Feel free to continue posting complete minimal programs
that exercise one thing and show behaviour you're unsure about.

-- 
 \ “We now have access to so much information that we can find |
  `\  support for any prejudice or opinion.” —David Suzuki, 2008-06-27 |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Documentation website layout changed?

2010-03-19 Thread Steve Holden
Steve Holden wrote:
> Andrej Mitrovic wrote:
>> On Mar 17, 6:41 pm, Andrej Mitrovic 
>> wrote:
>>> Hi,
>>>
>>> What happened to the sidebar on the left of the documentation website?
>>> It seems to be gone:
>>>
>>> http://docs.python.org/py3k/index.html
>>>
>>> I found it quite useful since I can quickly swap between Python2/3
>>> documentation, and between other parts of the documentation as well.
>> Edit: It looks like only the Python 3 pages are affected, the Python 2
>> pages are the same as before:
>>
>> http://docs.python.org/index.html
>>
>> Might be a bug?
> 
> I'll ask. Georg - is this known behavior or a temporary problem?
> 
It's a bug. Georg is on it.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: should writing Unicode files be so slow

2010-03-19 Thread Antoine Pitrou
Le Fri, 19 Mar 2010 17:18:17 +, djc a écrit :
> 
> changing
> with open(filename, 'rU') as tabfile: to
> with codecs.open(filename, 'rU', 'utf-8', 'backslashreplace') as
> tabfile:
> 
> and
> with open(outfile, 'wt') as out_part: to
> with codecs.open(outfile, 'w', 'utf-8') as out_part:
> 
> causes a program that runs  in
> 43 seconds to take 4 minutes to process the same data.

codecs.open() (and the object it returns) is slow as it is written in 
pure Python.

Accelerated reading and writing of unicode files is available in Python 
2.7 and 3.1, using the new `io` module.

Regards

Antoine.


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


Tuples vs. variable-length argument lists

2010-03-19 Thread Spencer Pearson
Hi!

This might be more of a personal-preference question than anything,
but here goes: when is it appropriate for a function to take a list or
tuple as input, and when should it allow a varying number of
arguments? It seems as though the two are always interchangeable. For
a simple example...

def subtract( x, nums ):
  return x - sum( nums )

... works equally well if you define it as "subtract( x, *nums )" and
put an asterisk in front of any lists/tuples you pass it. I can't
think of any situation where you couldn't convert from one form to the
other with just a star or a pair of parentheses.

Is there a generally accepted convention for which method to use? Is
there ever actually a big difference between the two that I'm not
seeing?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Importing v reloading modules modules

2010-03-19 Thread Gabriel Genellina
En Fri, 19 Mar 2010 14:09:09 -0300, Peter Peyman Puk  
 escribió:


I am running a simulator written in python. The simulator has a small  
TextView (actually a SourceView) widget which lets the user writes  
scripts, and when they are satisfied they can execute that script to get  
results. For arguments sake, we write a simple script and save it as  
A.py and we import it and execute it more or less like so.


import A

#assume there is a function called test() in module A
A.test()


Then the user modifies the contents of A.py and saves it again (to A.py)  
now all we have to do is the following


if 'A' in dir():
  reload(A)
else:
  import A

A.test()


Terry Reedy already gave you an answer for this import problem.
I'd like to go one step back and question whether using import/modules is  
the right thing here.
Clearly A.py is not a module but a script (that's the word you used) - and  
one does not *import* a script but *executes* it.
That is, instead of using import/__import__, use exec (or execfile) within  
a known namespace:


import __builtin__
ns = {'__builtins__': __builtin__.__dict__}
exec contents_of_textview in ns
# any change made to ns is visible here

--
Gabriel Genellina

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


Re: Castrated traceback in sys.exc_info()

2010-03-19 Thread Gabriel Genellina
En Wed, 17 Mar 2010 09:42:06 -0300, Pascal Chambon  
 escribió:


traceback functions indeed allow the manipulation of exception  
tracebacks,

but the root problem is that anyway, since that traceback is incomplete,
your "traceback.format_exc().splitlines()" will only provide frames for
callee (downward) functions, not caller (upward) ones, starting from the
exception catching frame.


Either I don't understand what you mean, or I can't reproduce it:


import logging

def a(): return b()
def b(): return c()
def c(): return d()
def d(): raise ValueError

def main():
  logging.basicConfig(level=logging.DEBUG)
  try: a()
  except: logging.exception("An error")

main()


Output:

D:\temp>python test_logging.py
ERROR:root:An error
Traceback (most recent call last):
  File "test_logging.py", line 10, in main
try: a()
  File "test_logging.py", line 3, in a
def a(): return b()
  File "test_logging.py", line 4, in b
def b(): return c()
  File "test_logging.py", line 5, in c
def c(): return d()
  File "test_logging.py", line 6, in d
def d(): raise ValueError
ValueError

--
Gabriel Genellina

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


Simple lock

2010-03-19 Thread moerchendiser2k3
Hi,

I have a common question about locks:

class SetPointer
{
private:
void *ptr;

MY_LOCK lock;


public:
void SetPointer(void *p)
{
Lock(this->lock);
this->ptr = p;
}

void *GetPointer()
{
Lock(this->lock);
return this->ptr;
}
};


Just a question, is this lock redundant, when the Pointer can be set/
get from different threads?
Thanks a lot!! Bye, moerchendiser2k3
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C-API PyObject_Call

2010-03-19 Thread moerchendiser2k3
Yes, the user is able to set a file which contains a function that
does what the user wants.
But in a case, I expect a special return value of this function.

So, I need to tell him, in which file/line the error occured,
otherwise he dont know
where to look.

Imagine he set 20 files, and all of them have implemented a special
function. So he
wouldnt know where to check for the error.

Bye, moerchendiser2k3
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple lock

2010-03-19 Thread Ben Finney
moerchendiser2k3  writes:

> I have a common question about locks:

You'd be best to ask in a forum related to the language you're using.
This (‘comp.lang.python’) is a forum for users of the Python language.

-- 
 \ “If you can do no good, at least do no harm.” —_Slapstick_, |
  `\ Kurt Vonnegut |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple lock

2010-03-19 Thread Chris Rebert
On Fri, Mar 19, 2010 at 6:58 PM, moerchendiser2k3
 wrote:

>
> Thanks a lot!! Bye, moerchendiser2k3

This is the **Python** mailinglist/newsgroup; and your question isn't
about Python.

The C++ one is over there:
http://groups.google.com/group/comp.lang.c++.moderated/topics

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


Re: Simple lock

2010-03-19 Thread MRAB

moerchendiser2k3 wrote:

Hi,

I have a common question about locks:

class SetPointer
{
private:
void *ptr;

MY_LOCK lock;


public:
void SetPointer(void *p)
{
Lock(this->lock);
this->ptr = p;
}

void *GetPointer()
{
Lock(this->lock);
return this->ptr;
}
};


Just a question, is this lock redundant, when the Pointer can be set/
get from different threads?
Thanks a lot!! Bye, moerchendiser2k3


1. That's C++. What does it have to do with Python?

2. The value you're accessing is a simple pointer which you're either
setting or getting, so a lock shouldn't be necessary.

3. You're locking, but never unlocking. The sequence should be: lock, do
stuff, unlock.
--
http://mail.python.org/mailman/listinfo/python-list


Best Way to extract Numbers from String

2010-03-19 Thread Jimbo
Hello

I am trying to grab some numbers from a string containing HTML text.
Can you suggest any good functions that I could use to do this? What
would be the easiest way to extract the following numbers from this
string...

My String has this layout & I have commented what I want to grab:
[CODE] """
43.200 
0.040 

   43.150  #
I need to grab this number only
43.200 
   43.130  #
I need to grab this number only
43.290 
 43.100  # I need to
grab this number only
7,450,447 
Options
Warrants & Structured
Products
CFDs
http://hfgapps.hubb.com/asxtools/
Charts.aspx?
TimeFrame=D6&compare=comp_index&indicies=XJO&pma1=20&pma2=20&asxCode=BHP">

XD

Recent

"""[/CODE]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple lock

2010-03-19 Thread Gabriel Genellina
En Fri, 19 Mar 2010 23:31:23 -0300, MRAB   
escribió:

moerchendiser2k3 wrote:



 class SetPointer
{
private:
void *ptr;
MY_LOCK lock;
  public:
void SetPointer(void *p)
{
Lock(this->lock);
this->ptr = p;
}



3. You're locking, but never unlocking. The sequence should be: lock, do
stuff, unlock.


Just FYI: C++ doesn't have try/finally, and such behavior is usually  
emulated using a local object. When it goes out of scope, it is  
automatically destroyed, meaning that the object destructor is called.  
Whatever you would write in a "finally" clause, in C++ goes into a  
destructor.


Of course C++ guys would never say they're "emulating" try/finally,  
instead they declare RAII as *the* Only and Right Way :)


--
Gabriel Genellina

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


Re: Best Way to extract Numbers from String

2010-03-19 Thread Gabriel Genellina

En Sat, 20 Mar 2010 00:04:08 -0300, Jimbo  escribió:


I am trying to grab some numbers from a string containing HTML text.
Can you suggest any good functions that I could use to do this? What
would be the easiest way to extract the following numbers from this
string...

My String has this layout & I have commented what I want to grab:
[CODE] """
43.200 
0.040 

   43.150  #
I need to grab this number only
43.200 
   43.130  #
I need to grab this number only


I'd use BeautifulSoup [1] to handle bad formed HTML like that.

[1] http://www.crummy.com/software/BeautifulSoup/

--
Gabriel Genellina

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


Re: Tuples vs. variable-length argument lists

2010-03-19 Thread Steven D'Aprano
On Fri, 19 Mar 2010 17:20:31 -0700, Spencer Pearson wrote:

> Hi!
> 
> This might be more of a personal-preference question than anything, but
> here goes: when is it appropriate for a function to take a list or tuple
> as input, and when should it allow a varying number of arguments? 

That depends on the function as well as your personal preference.

Of course, you can also follow the lead of max and min and accept both:

>>> max(1, 2, 3, 4) == max([1, 2, 3, 4])
True

You need to consider which is more "natural" for the semantics of the 
function.


> It seems as though the two are always interchangeable. 

Not always. Consider sum(a, b, c, d).

Should that be "sum the sequence [a, b, c, d]" or "sum the sequence [a, 
b, c] with initial value d"? You might ask what difference it makes, but 
it may make a big difference:

* if a...d are floats, there may be differences in rounding errors 
  between a+b+c+d and d+a+b+c

* if a...d are lists, the order that you do the addition matters:
  ["a", "b", "c"] + ["d"] != ["d"] + ["a", "b", "c"]

* even if a...d are integers, it may make a big difference for
  performance. Perhaps not for a mere four arguments, but watch:

>>> n = 10**100
>>> seq = [n] + range(10001)
>>> from timeit import Timer
>>> t1 = Timer("sum(seq)", "from __main__ import n, seq; seq.append(-n)")
>>> t2 = Timer("sum(seq, -n)", "from __main__ import n, seq")
>>> min(t1.repeat(number=1))
6.1270790100097656
>>> min(t2.repeat(number=1))
0.012988805770874023

In the first case, all the intermediate calculations are done using a 
million digit long int, in the second case it is not.

[...]
> I can't think
> of any situation where you couldn't convert from one form to the other
> with just a star or a pair of parentheses.

Of course you can convert, but consider that this is doing a conversion. 
Perhaps that's unnecessary work for your specific function? You are 
packing and unpacking a sequence into a tuple. Whether this is a good 
thing or a bad thing depends on the function.


> Is there a generally accepted convention for which method to use? Is
> there ever actually a big difference between the two that I'm not
> seeing?

If you think of the function as operating on a single argument which is a 
sequence of arbitrary length, then it is best written to take a single 
sequence argument.

If you think of the function as operating on an arbitrary number of 
arguments, then it is best written to take an arbitrary number of 
arguments.

If you think it doesn't matter, then choose whichever model is less work 
for you.

If neither seems better than the other, then choose arbitrarily.

If you don't like the idea of making an arbitrary choice, or if your 
users complain, then support both models (if possible).


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


Re: Simple lock

2010-03-19 Thread MRAB

Gabriel Genellina wrote:
En Fri, 19 Mar 2010 23:31:23 -0300, MRAB  
escribió:

moerchendiser2k3 wrote:



 class SetPointer
{
private:
void *ptr;
 MY_LOCK lock;
  public:
void SetPointer(void *p)
{
Lock(this->lock);
this->ptr = p;
}



3. You're locking, but never unlocking. The sequence should be: lock, do
stuff, unlock.


Just FYI: C++ doesn't have try/finally, and such behavior is usually 
emulated using a local object. When it goes out of scope, it is 
automatically destroyed, meaning that the object destructor is called. 
Whatever you would write in a "finally" clause, in C++ goes into a 
destructor.


Of course C++ guys would never say they're "emulating" try/finally, 
instead they declare RAII as *the* Only and Right Way :)



Lock() doesn't look like a declaration, but a function call (although
it's been a while since I last did C++!).

In the past I've written some C++ code where try..finally... would've
been useful... *sigh*
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple lock

2010-03-19 Thread Alf P. Steinbach

* MRAB:

Gabriel Genellina wrote:
En Fri, 19 Mar 2010 23:31:23 -0300, MRAB  
escribió:

moerchendiser2k3 wrote:



 class SetPointer
{
private:
void *ptr;
 MY_LOCK lock;
  public:
void SetPointer(void *p)
{
Lock(this->lock);
this->ptr = p;
}



3. You're locking, but never unlocking. The sequence should be: lock, do
stuff, unlock.


Just FYI: C++ doesn't have try/finally, and such behavior is usually 
emulated using a local object. When it goes out of scope, it is 
automatically destroyed, meaning that the object destructor is called. 
Whatever you would write in a "finally" clause, in C++ goes into a 
destructor.


Of course C++ guys would never say they're "emulating" try/finally, 
instead they declare RAII as *the* Only and Right Way :)



Lock() doesn't look like a declaration, but a function call (although
it's been a while since I last did C++!).


Right. But the OP is clearly a beginner who has yet to learn conventions (like 
all uppercase reserved for macros) and good versus bad programming constructs 
(like using void*), and who is even confusing C++ with Python. So it might just 
be that 'Lock' is a macro, and then it can expand to anything.




In the past I've written some C++ code where try..finally... would've
been useful... *sigh*


Check out Marginean and Alexandrescu's ScopeGuard. Old DDJ-article. For MSVC you 
may want to change use of __LINE__ in that code, to MS-specific __COUNTER__.


Fwiw. I can't remember ever needing 'finally' in C++ code.

It indicates a lack of encapsulation of entitites that should clean up 
themselves. Simple smart pointers etc. can help. E.g., check out Boost library.



Cheers & hth., even if a little off-topic,

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


Re: (updated) Choosing a collection of common modules/packages for a general purpose reusable PY2EXE runtime

2010-03-19 Thread Aahz
In article ,
  wrote:
>
>We've decided to build a re-usable *general purpose* PY2EXE "runtime"
>that can be shared by a number of scripts vs. distributing a monolithic
>EXE for each of our scripts.

It's not clear what the purpose of this is.  You can build several
scripts against the same py2exe backend.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com "SEXY RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK G

2010-03-19 Thread Brian J Mingus
On Fri, Mar 19, 2010 at 10:18 PM, Naeem  wrote:

> "EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY
> FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com   "SEXY
> RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS"
> "SEXY HOLLYWOOD GIRLS" "SEXY BOLLYWOOD""EURO GIRLS" "MISS EUROPE"
> "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on
> www.sexyandpretty-girls.blogspot.com   "SEXY RUSSIAN GIRLS" "SEXY
> GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS" "SEXY HOLLYWOOD GIRLS"
> "SEXY BOLLYWOOD""EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH"
> "PRETTY GIRLS" "SEXY FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com
> "SEXY RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK
> GIRLS" "SEXY HOLLYWOOD GIRLS" "SEXY BOLLYWOOD""EURO GIRLS" "MISS
> EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on
> www.sexyandpretty-girls.blogspot.com   "SEXY RUSSIAN GIRLS" "SEXY
> GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS" "SEXY HOLLYWOOD GIRLS"
> "SEXY BOLLYWOOD""EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH"
> "PRETTY GIRLS" "SEXY FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com
> "SEXY RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK
> GIRLS" "SEXY HOLLYWOOD GIRLS" "SEXY BOLLYWOOD""EURO GIRLS" "MISS
> EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on
> www.sexyandpretty-girls.blogspot.com   "SEXY RUSSIAN GIRLS" "SEXY
> GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS" "SEXY HOLLYWOOD GIRLS"
> "SEXY BOLLYWOOD""EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH"
> "PRETTY GIRLS" "SEXY FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com
> "SEXY RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK
> GIRLS" "SEXY HOLLYWOOD GIRLS" "SEXY BOLLYWOOD""EURO GIRLS" "MISS
> EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on
> www.sexyandpretty-girls.blogspot.com   "SEXY RUSSIAN GIRLS" "SEXY
> GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS" "SEXY HOLLYWOOD GIRLS"
> "SEXY BOLLYWOOD""EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH"
> "PRETTY GIRLS" "SEXY FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com
> "SEXY RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK
> GIRLS" "SEXY HOLLYWOOD GIRLS" "SEXY BOLLYWOOD""EURO GIRLS" "MISS
> EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on
> www.sexyandpretty-girls.blogspot.com   "SEXY RUSSIAN GIRLS" "SEXY
> GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS" "SEXY HOLLYWOOD GIRLS"
> "SEXY BOLLYWOOD""EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH"
> "PRETTY GIRLS" "SEXY FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com
> "SEXY RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK
> GIRLS" "SEXY HOLLYWOOD GIRLS" "SEXY BOLLYWOOD""EURO GIRLS" "MISS
> EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on
> www.sexyandpretty-girls.blogspot.com   "SEXY RUSSIAN GIRLS" "SEXY
> GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS" "SEXY HOLLYWOOD GIRLS"
> "SEXY BOLLYWOOD""EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH"
> "PRETTY GIRLS" "SEXY FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com
> "SEXY RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK
> GIRLS" "SEXY HOLLYWOOD GIRLS" "SEXY BOLLYWOOD""EURO GIRLS" "MISS
> EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on
> www.sexyandpretty-girls.blogspot.com   "SEXY RUSSIAN GIRLS" "SEXY
> GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS" "SEXY HOLLYWOOD GIRLS"
> "SEXY BOLLYWOOD""EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH"
> "PRETTY GIRLS" "SEXY FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com
> "SEXY RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK
> GIRLS" "SEXY HOLLYWOOD GIRLS" "SEXY BOLLYWOOD""EURO GIRLS" "MISS
> EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on
> www.sexyandpretty-girls.blogspot.com   "SEXY RUSSIAN GIRLS" "SEXY
> GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS" "SEXY HOLLYWOOD GIRLS"
> "SEXY BOLLYWOOD""EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH"
> "PRETTY GIRLS" "SEXY FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com
> "SEXY RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK
> GIRLS" "SEXY HOLLYWOOD GIRLS" "SEXY BOLLYWOOD""EURO GIRLS" "MISS
> EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on
> www.sexyandpretty-girls.blogspot.com   "SEXY RUSSIAN GIRLS" "SEXY
> GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS" "SEXY HOLLYWOOD GIRLS"
> "SEXY BOLLYWOOD""EURO GIRLS" "MISS EUROPE" "MISS FRENCH" "FRENCH"
> "PRETTY GIRLS" "SEXY FRENCH GIRLS" on www.sexyandpretty-girls.blogspot.com
> "SEXY RUSSIAN GIRLS" "SEXY GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK
> GIRLS" "SEXY HOLLYWOOD GIRLS" "SEXY BOLLYWOOD""EURO GIRLS" "MISS
> EUROPE" "MISS FRENCH" "FRENCH" "PRETTY GIRLS" "SEXY FRENCH GIRLS" on
> www.sexyandpretty-girls.blogspot.com   "SEXY RUSSIAN GIRLS" "SEXY
> GREEK GIRLS" "SEXY DUTCH GIRLS" "SEXY UK GIRLS" "SE

Writing tests for the Python bug tracker

2010-03-19 Thread Steven D'Aprano
I have two reported bugs in the bug tracker waiting on tests:

http://bugs.python.org/issue8128
http://bugs.python.org/issue4037

Are there any guidelines for writing tests for the standard library and 
language? I've googled, but found nothing useful: lots of guidelines for 
writing tests, and of course I've read PEP 8, but I'm not sure if there 
are conventions for tests I'm missing.



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


Re: Writing tests for the Python bug tracker

2010-03-19 Thread Steven D'Aprano
On Sat, 20 Mar 2010 06:23:14 +, Steven D'Aprano wrote:

> Are there any guidelines for writing tests for the standard library and
> language? I've googled, but found nothing useful: lots of guidelines for
> writing tests, and of course I've read PEP 8, but I'm not sure if there
> are conventions for tests I'm missing.

I've found this:

http://docs.python.org/library/test.html

and I've written a small test:

$ cat test_unicode_interpolation.py
# For testing http://bugs.python.org/issue8128

import test.test_support
import unittest

class K(unicode):
def __str__(self): return "Surprise!"

class UnicodeInterpolationTest(unittest.TestCase):
def test_interpolation(self):
self.assertEquals(u'%s' % K('some text'), 'Surprise!')

def test_main():
test.test_support.run_unittest(UnicodeInterpolationTest)

if __name__ == "__main__":
test_main()


but when I try running the test, I get an error:

$ python test_unicode_interpolation.py
Options: {'delimiter': None}
str of options.delimiter = None
repr of options.delimiter = None
len of options.delimiter
Traceback (most recent call last):
  File "test_unicode_interpolation.py", line 3, in 
import test.test_support
  File "/home/steve/python/test.py", line 8, in 
print "len of options.delimiter", len(options.delimiter)
TypeError: object of type 'NoneType' has no len()


What am I doing wrong?



(By the way, I'm expecting the unit test above to fail.)




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