Re: loop until keypress (Windows XP)

2006-08-09 Thread Andy Terrel
If you did want a linux version you could just make people send a
KeyboardInterupt.


try:
print "Press ^C to stop"
loop
except KeyboardInterrupt:
some stop action or just pass

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


Re: how do you get the name of a dictionary?

2006-08-18 Thread Andy Terrel

jojoba wrote:
> Hello!
>
> Does anyone know how to find the name of a python data type.
>
> Conside a dictionary:
>
> Banana = {}
>
> Then, how do i ask python for a string representing the name of the
> above dictionary (i.e. 'Banana')?
>
> thanks to anyone who has time to answer this nube question!
> jojoba

here is an easy hack,  I don't know if there is an explicit function.


for i in dir():
 if eval(i) == Banana:
 print i

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


Re: how do you get the name of a dictionary?

2006-08-18 Thread Andy Terrel
Why bang your head?  It was a stupid hack that has lots of problems,
but done in a way that is readable.  Sure I could do something more
functional or one lined like:

Banana={}
names = filter(lambda x:id(eval(x))==id(Banana),dir())

but I am guessing that it is harder to read by many.  Anywho I can
think of plenty of reasons it would fail, but it really depends on the
app.


Fredrik Lundh wrote:
> Andy Terrel wrote:
>
> > for i in dir():
> >  if eval(i) == Banana:
> >  print i
> 
> (sound of head hitting desk)
> 
> 

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


Re: how do you get the name of a dictionary?

2006-08-18 Thread Andy Terrel

Georg Brandl wrote:
> Andy Terrel wrote:
> > Why bang your head?
>
> Because there's no chance that the original request is sane.
>
> If you want your objects to know their name, give them a name as an attribute.
> 

This is true but sometimes it is just fun to hack around.

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


Re: find, replace and save string in ascii file

2006-08-23 Thread Andy Terrel
Take your code, pretend it is in file:

 $
NAME='ALFA'
CODE='x'
$


a python functions could be:

def change(filename):
fp = open(filename, 'r')
lines = fp.readlines()
fp.close()
for i in range(len(lines)):
if lines[i].find('NAME') >= 0:
  if lines[i+1].find('CODE') >= 0:
  code = lines[i+1].split('=')[1]
  lines[i] = 'NAME='+
 fp2 = open(filename,'w')
 fp2.write("".join(lines))



So this is pretty crude and not very robust.  But should give you about
some idea of how to write something.  I'm not sure if there is a way
around opening two file pointers but  I think python has no problems
with this.  Also if you files can't fit into memory you probably want
to do something a lot more, that is read only a block at a time and
then mess with stuff, probably writing out to a temp file and then
overwriting your original file.


peter wrote:
> Thank you for your advice.
>
> I'm not so good in python yet, so could you be so kind and write me a
> piece of code for the part
>
>  If you want the output to be written to same file just 'move' this
>  temperory file to the input file once you are done.
>
> Because that's what I don't know how to do it. How to replace a string
> in the middle of the file and than save this file.
>
> Thanks a lot.
>
>
> Amit Khemka wrote:
> > On 23 Aug 2006 05:48:37 -0700, peter <[EMAIL PROTECTED]> wrote:
> > > Hello all,
> > >
> > > I'm looking for an advice.
> > >
> > > Example (one block in ascii file):
> > > $
> > > NAME='ALFA'
> > > CODE='x'
> > > $
> > >
> > > There are many similar blocks in the file with different NAMEs and
> > > different CODEs. What I'm looking for is a script that searchs through
> > > whole file and finds all strings with name ALFA and based on what CODE
> > > is after each ALFA (can be x, y or z) the ALFA name is replaced by
> > > BETAx,BETAy or BETAz and so changed file saves.
> > >
> > > What I did is that I can find all strings which I need, next I change
> > > these strings based on CODE, but what I can't is to replace old string
> > > with new one, on the same position in the file. It always writes new
> > > string at the end of the file. Here is my code
> >
> > A simpler way can be:
> >
> > 1. Read a 'block' from the input file, ( you can simply read a line
> > starting with 'NAME' and keep on reading till you find a line with
> > starting 'CODE')
> > 2. Once you have read a 'block', make whatever you want changes to the
> > NAME and then write the 'block' to a temperory file.
> >
> > If you want the output to be written to same file just 'move' this
> > temperory file to the input file once you are done.
> >
> > Note: if there is other stuff in the input file, apart from such
> > 'blocks' that you want to preserve, a small modification in step 1
> > would take care of it.
> >
> > hth,
> > amit.
> >
> > --
> > 
> > Amit Khemka -- onyomo.com
> > Home Page: www.cse.iitd.ernet.in/~csd00377
> > Endless the world's turn, endless the sun's Spinning, Endless the quest;
> > I turn again, back to my own beginning, And here, find rest.

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


Re: Python bindings for picasaweb ...

2006-08-23 Thread Andy Terrel
sweet.  I'll definitely be trying to use this.


[EMAIL PROTECTED] wrote:
> Just a post to announce some python bindings for picasaweb (photo's
> service of google).
> ---> PycasaWeb (GPL), http://manatlan.infogami.com/pycasaweb
> I think it may be usefull for linux users, because it's one of the only
> way to post pictures on picasaweb. And can be usefull to script batch
> with it ... and our wonderful python box ;-)
>
> It's inspired from the recent google-sharp module :
> http://svn.myrealbox.com/viewcvs/trunk/google-sharp/?rev=63077
>
> I will try to implement others api, but contributions are accepted ;-)

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


Re: split string problems

2006-09-07 Thread Andy Terrel
try str(p).split()[2]

your class is using the __str__ attribute to print and I am guessing
that is what you are seeing.

Tempo wrote:
> Hey. I am trying to grab the prices from the string below but I get a
> few errors when I try to do it: Take a look at the code and error
> messages below for me and thanks you in advanced to all that help.
> Thank you. Here's the code & error messages:
>
> >>> p
> [
>   $14.99
>   , 
>   $27.99
>   , 
>   $66.99
>   , 
>   $129.99
>   , 
>   $254.99
>   ]
> >>> p.split()[2]
>
> Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> p.split()[2]
> AttributeError: 'ResultSet' object has no attribute 'split'
> >>>

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


Decorating class member functions

2007-05-03 Thread Andy Terrel
Okay does anyone know how to decorate class member functions?

The following code gives me an error:

Traceback (most recent call last):
  File "decorators2.py", line 33, in 
s.update()
  File "decorators2.py", line 13, in __call__
retval = self.fn.__call__(*args,**kws)
TypeError: update() takes exactly 1 argument (0 given)

--


#! /usr/bin/env python

class Bugger (object):
def __init__ (self, module, fn):
self.module = module
self.fn = fn

def __call__ (self,*args, **kws):
ret_val = self.fn(*args,**kws)
return ret_val

def instrument (module_name):
ret_val = lambda x: Bugger(module_name, x)
return ret_val

class Stupid:
def __init__(self):
self.val = 1

@instrument("xpd.spam")
def update(self):
self.val += 1


s = Stupid()
s.update()

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


Re: Decorating class member functions

2007-05-03 Thread Andy Terrel
Oh I should mention the decorator needs to have some notion of state
(such as with the above class)

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


Re: How do I import a variable from another module?

2007-05-03 Thread Andy Terrel
are you sure your variable isn't in some code block that wouldn't be
read on import?  Such as:

if __name__ == "__main___":
 actions = 1


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


Re: Decorating class member functions

2007-05-03 Thread Andy Terrel
I just need to keep the state around. I make a call to some function
that is pretty expensive so I want to save it as a member during the
__init__ of the decorator.

Yeah I'm afraid it can't be done either, that's why I asked the group.




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


Re: Decorating class member functions

2007-05-03 Thread Andy Terrel
not quite as elegant but here is a workaround... Thanks Virgil for
taking some time to think about it.

---

class Bugger (object):
def __init__ (self, module):
print "Entering __init__"
self.module = module
self.verb = 0

def instrument (module_name):
def wrapper(f):
def _wrap(*args,**kws):
ret_val = f(*args,**kws)
return ret_val
return _wrap
b = Bugger(module_name)
if b.verb == 0:
ret_val = wrapper
else:
ret_val = lambda x:x
return ret_val

class Stupid:
def __init__(self):
self.val = 1

@instrument("spam")
def update(self):
self.val += 1


s = Stupid()
s.update()
s.update()
s.update()
s.update()
print s.val

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


Re: Decorating class member functions

2007-05-04 Thread Andy Terrel
 Thanks Peter and 7stud.  That is the solution that really works for
me.

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


Re: c++ for python programmers

2007-02-12 Thread Andy Terrel
On Feb 12, 4:11 pm, "Thomas Nelson" <[EMAIL PROTECTED]> wrote:
> On Feb 12, 1:35 pm, andrew clarke <[EMAIL PROTECTED]> wrote:
>
> > Thomas, I sent you a message off-list but it bounced due to your mailbox
> > being full.
>
> > Short answer: Subscribe to the [EMAIL PROTECTED] mailing list and
> > ask your C/C++ questions there.
>
> > Regards
> > Andrew
>
> I have to edit a large C++ project written by someone else.  My email
> address
> above is incorrect; replace mail with cs.  Thanks for the help.
>
> Thomas

To learn C I recommend K&R (Kernigahn and Richie), for C++ I like
Savitch's book, Absolute C++. I too learned python then c/c++.  Also I
would disagree with the people saying never to use C++, it will run
much faster for computationally intensive programs.  It will make you
a better python programmer, you get to see how things are done "under
the hood".

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