Re: Terminating python script easily

2009-10-22 Thread Bahadir Balban
On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant
 wrote:
> Balban wrote:
>>
>> Hi,
>>
>> I have a python build script that calls various commands, some using
>> os.system().
>>
>> Often, if I want to terminate the script prematurely, I press ctrl-c,
>> but I have to do this many times before I can kill the script for
>> good. I was wondering is there a way that I define a signal handler
>> and kill the whole thing at once with a single ctrl-c? Perhaps I
>> should  also call my other scripts with a method other than os.system
>> () as well?
>>
>> Thank you,
>>
>> Bahadir
>>
>
> you may want to use subprocess instead of os.system.
> On catching CTRL+C, you kill all the pid started with subprocess and exit
> the script smoothly.
>
> JM
>

Hmm. OK, this is what I suspected I needed. So no explicit signal
catching is required I guess.

I will look into it, thanks.


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


Re: Format string with single quotes in it

2009-09-26 Thread Bahadir
On Sep 26, 6:33 am, "Gabriel Genellina" 
wrote:
>
> So, look in your file for lines ending in %
>
> --
> Gabriel Genellina


Hello All,

I fixed it with your help. Sometimes you need that extra insight from
comp.lang.python.

Thank you so much,

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


Format string with single quotes in it

2009-09-25 Thread Bahadir
Hi there,

My question is simple, but I've been spending some hours over the web
and still struggling to get this right: How do I format a string that
contains single quotes in it?

I am reading a file with lines of the form:

CONT%d_VIRTMEM_REGIONS  'Container %d number of virtual regions'

and trying to format this as follows:

str % (0, 0)

I get the error:

ValueError: unsupported format character '
' (0xa) at index 5541

I also tried:

# Replace single quotes with \'
str = str.replace("'", "\'")

and doubling the backward slash as well.

What am I doing wrong?

Thank you,

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


Re: Array of objects lost in unpickling

2009-09-13 Thread Bahadir
On Sep 13, 5:48 pm, Jon Clements  wrote:
> On 13 Sep, 15:19, Bahadir  wrote:
>
>
>
> > Hi,
>
> > I have a class:
>
> > class second:
> >     a = None
> >     b = None
>
> > class first:
> >     array = []
>
> > I populate the array in first class with instances of second, then
> > save by:
>
> > shelve = shelve.open(),
> > shelve["first"] = myfirst
> > shelve.close()
>
> > When I reopen the shelve from another script, the first class is
> > there, but array has no elements. If I reopen the shelve in the same
> > script right after shelve.close(), the elements are there.
>
> > Also there are no errors printed out.
>
> > Any idea why the array of instances are lost?
>
> > Thanks,
>
> > Bahadir
>
> You most likely want 'array' to be an instance level and not class
> level attribute.
>
> class first(object):
>     def __init__(self):
>         self.array = []
>
> myfirst = first()
> myfirst.array.append(23423)
>
> etc...
>
> hth,
> Jon.

Hmm, OK. New to python. Got it. Thanks a lot!

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


Array of objects lost in unpickling

2009-09-13 Thread Bahadir
Hi,

I have a class:

class second:
a = None
b = None

class first:
array = []

I populate the array in first class with instances of second, then
save by:

shelve = shelve.open(),
shelve["first"] = myfirst
shelve.close()

When I reopen the shelve from another script, the first class is
there, but array has no elements. If I reopen the shelve in the same
script right after shelve.close(), the elements are there.

Also there are no errors printed out.

Any idea why the array of instances are lost?

Thanks,

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