Re: Class Help

2005-10-01 Thread marduk
On Sat, 2005-10-01 at 18:58 -0400, Ivan Shevanski wrote:
> To continue with my previous problems, now I'm trying out classes.  But I 
> have a problem (which I bet is easily solveable) that I really don't get.  
> The numerous tutorials I've looked at just confsed me.For intance:
> 
> >>>class Xyz:
> ... def y(self):
> ... q = 2
> ...
> >>>Xyz.y()
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: unbound method y() must be called with Xyz instance as first 
> argument
> (got nothing instead)
> 
> 
> So. . .What do I have to do? I know this is an extremley noob question but I 
> think maybe if a person explained it to me I would finally get it =/


When you define a class, say Xyz, your are defining your own type.  Say
that Person is a class.  And person has a method walk():

class Person:
def walk(self):
...

now to use the Person class, you need to create an instance of it.  You
can't just say Person.walk() because Person is a "class"ification, not a
real object.  You need an instance of person.

jane = Person()

This creates a new person called "jane".  "jane" is an instance of the
class Person.

>>> jane
 <__main__.Person instance at 0x2c723710>

Now we can tell jane to walk:

jane.walk()

So what the error is telling you (in a bit confusing way if you're a
newbie) is that you are calling a method y() but you have not created an
instance of your Xyz class to do y().  Or, to use my analogy, you are
telling Person to walk, but you can't make Person walk, you have to
create a person, jane, and have jane walk.

Hope this helps, but I recommend you read a good intro to
object-oriented programming.

-a

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


Re: "no variable or argument declarations are necessary."

2005-10-03 Thread marduk

>   egold = 0:
>   while egold < 10:
> if test():
>   ego1d = egold + 1
> 

Both pylint and pychecker pick this up.  I wrapped the code in a
function (to prevent importing from running in an infinite loop) and ran
both pylint and pychecker:

plyint: W:  5:myfunc: Unused variable 'ego1d'
pychecker: test.py:4: Local variable (ego1d) not used


I make a habit to run pylint or pychecker on my code often.  They pick
up a lot of stuff like unused variables, etc.

But you can also do this:

/* initialize variables i'm gonna use */
int vara = 0; 
int varb = 0;
while (vara < 10) {
varb = vara + 1;
}

So we can make a similar mistake in C if you type the wrong (declared)
variable name.  Moreover, "gcc -Wall" did not report the "unused"
variable so it might be even more difficult to track down the problem. 


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


Re: "no variable or argument declarations are necessary."

2005-10-04 Thread marduk
On Tue, 2005-10-04 at 11:43 -0700, Paul Rubinhttp: wrote:
> What's the big deal?  Perl has an option for flagging undeclared
> variables with warnings ("perl -w") or errors ("use strict") and Perl
> docs I've seen advise using at least "perl -w" routinely.  Those
> didn't have much impact.  Python already has a "global" declaration;
> how does it de-Pythonize the language if there's also a "local"
> declaration and an option to flag any variable that's not declared as
> one or the other? 

I would be happy with a "local" option. e.g.

def myfunc():
   local spam = ...
   local eggs = ...
   global juice

   breakfast = juice + spam + eggs # raises an exception (undeclared
breakfast)


What I'm *afraid* of is:

def myfunc(MyClass myparam):
   int spam = 6
   str eggs

   # etc

i.e. typed declarations and type checking.  This would annoy the heck
out of me. 

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


Re: Jargons of Info Tech industry

2005-10-04 Thread marduk
[Removed X-posting]

On Tue, 2005-10-04 at 17:14 +, Roedy Green wrote:
> On Tue, 23 Aug 2005 08:32:09 -0500, l v <[EMAIL PROTECTED]> wrote or quoted :
> 
> >I think e-mail should be text only. 
> 
> I disagree.  Your problem  is spam, not HTML. Spam is associated with
> HTML and people have in Pavlovian fashion come to hate HTML.
> 
> But HTML is not the problem! 

And guns don't kill people: people kill people!

Seriously though, plain-text is just plain [n]etiquette (for most
newsgroups/mailing lists (at least the technical ones)).  Follow the
rules and avoid becoming a social outcast.  If your particular forum
allows/encourages HTML then  away!  

( This post is neither x-posted, HTML-ized, closed-captioned nor
simulcast in Spanish or HD )

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


how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
I couldn't think of a good subject..

Basically, say I have a class

class Spam:
def __init__(self, x):
self.x = x


then if I create two instances:

a = Spam('foo')
b = Spam('foo')

a == b # False

What I *really* want is to keep a collection of all the Spam instances,
and if i try to create a new Spam instance with the same contructor
parameters, then return the existing Spam instance.  I thought new-style
classes would do it:

class Spam(object):
cache = {}
def __new__(cls, x):
if cls.cache.has_key(x):
return cls.cache[x]
def __init__(self, x):
self.x = x
self.cache[x] = self

a = Spam('foo')
b = Spam('foo')

Well, in this case a and b are identical... to None!  I assume this is
because the test in __new__ fails so it returns None, I need to then
create a new Spam.. but how do I do that without calling __new__ again?
I can't call __init__ because there's no self...

So what is the best/preferred way to do this?

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
On Wed, 2005-10-05 at 12:56 -0400, Jonathan LaCour wrote:
> Oops, you forgot to return object.__new__(cls, x) in the case the  
> object isn't in the cache.  That should fix it. 

Ahh, that did it.  I didn't even think of calling object...

so the new class looks like:

class Spam(object):
cache = {}
def __new__(cls, x):
if cls.cache.has_key(x):
return cls.cache[x]
else:
new_Spam = object.__new__(cls, x)
cls.cache[x] = new_Spam
return new_Spam
def __init__(self, x):
self.x = x

a = Spam(2)
b = Spam(2)

a == b # => True
id(a) == id(b) # => True

Thanks for all your help.

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
On Wed, 2005-10-05 at 18:28 +0200, Diez B. Roggisch wrote:
> Use the BORG-pattern. See
> 
> 
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531
> 
> Together with your caching, that should do the trick.
> 

I looked at the Borg Pattern, but I don't think it was exactly what I
want.

The Borg patten appears to be if you want multiple instances that point
to the same "data".

What I wanted is multiple calls to create a new object with the same
parameters points to the "original" object instead of creating a new
one.

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
On Wed, 2005-10-05 at 12:56 -0400, Jonathan LaCour wrote:
> > class Spam(object):
> > cache = {}
> > def __new__(cls, x):
> > if cls.cache.has_key(x):
> > return cls.cache[x]
> > def __init__(self, x):
> > self.x = x
> > self.cache[x] = self
> >
> > a = Spam('foo')
> > b = Spam('foo')
> >
> > Well, in this case a and b are identical... to None!  I assume this is
> > because the test in __new__ fails so it returns None, I need to then
> > create a new Spam.. but how do I do that without calling __new__  
> > again?
> > I can't call __init__ because there's no self...
> >
> >
> 
> Oops, you forgot to return object.__new__(cls, x) in the case the  
> object isn't in the cache.  That should fix it.

Okay, one more question... say I then

c = Spam('bar')
del a
del b

I've removed all references to the object, except for the cache.  Do I
have to implement my own garbage collecting is or there some "magical"
way of doing this within Python?  I pretty much want to get rid of the
cache as soon as there are no other references (other than the cache).

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
On Wed, 2005-10-05 at 19:37 +0200, Diez B. Roggisch wrote:
> > What I wanted is multiple calls to create a new object with the same
> > parameters points to the "original" object instead of creating a new
> > one.
> 
> Read the comments. What you say is essentially the same - the data 
> matters, after all. What do you care if there are several instances
> around?
> 
> Diez 

In my case it matters more that the objects are the same.

For example I want set([Spam(1), Spam(2),
Spam(3)]).intersect(set([Spam(1), Spam(2)]) to contain two items instead
of 0.

For this and many other reasons it's important that Spam(n) is Spam(n).

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
On Wed, 2005-10-05 at 19:24 +0200, Peter Otten wrote:
> Use a weakref.WeakValueDictionary as the cache instead of a normal
> dict.
> 
> Peter 

Thanks for the reference to the weakref module.  Until now I've never
had a use for it, but it sounds like what I'm looking for.

-m

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


Re: Let My Terminal Go

2005-10-10 Thread marduk
On Mon, 2005-10-10 at 22:58 -0700, [EMAIL PROTECTED] wrote:
> Hello,
> 
> A user of my application points me to a behavior in gVim,
> the text editor, that I would like to implement in my
> application.
> 
> When gVim is launched from a shell terminal, it completely
> frees the terminal. You can continue to use the terminal for
> whatever purpose you wish, including closing and exiting it,
> without any effect on the running gVim instance.
> 
> How do I implement this in my application written in python?
> I would like to believe it does not involve me forking my
> application in a new process. Maybe there is signal I can
> send to the operating system to achieve this, right?

gvim forks.  Why do you want to avoid it?

import os, sys

pid = os.fork()
if pid !=0:
# exit parent
sys.exit(0)
# child continues


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


Re: piping out binaries properly

2005-10-11 Thread marduk
On Wed, 2005-10-12 at 00:16 -0400, Mike Meyer wrote:
[...]
> It's not normal to write binary content to stdout - you normally write
> it to a file. Open the file with open(name, 'wb') to write binaries.
> 

It is interesting that as a "Unix consultant" you should make that
claim.  Especially since

>>> sys.stdout
', mode 'w' at 0x2aac9198>
  

Indeed there would be a lot of broken Unix systems out there if that
were not the case.

As for the OP, you may want to check how stdout is opened on your
system.  In Windows there are two "write" modes for files, 'w', and 'wb'
where apparently 'w' is text mode and 'wb' is binary.  You may want to
check what mode your stdout is in.  I don't have a Windows box handy
right now to verify.

> There doesn't appear to be any way to retroactively change the mode on
> a file. Which is probably a good thing.
> 
>  Mike Meyer <[EMAIL PROTECTED]>
http://www.mired.org/home/mwm/
> Independent WWW/Perforce/FreeBSD/Unix consultant, email for more
information.


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


Re: grep

2005-10-25 Thread marduk
On Tue, 2005-10-25 at 06:45 +, David Isaac wrote:
> What's the standard replacement for the obsolete grep module?

AFAIK there never was a "grep" module.  There does, however exist a
deprecated "regex" module:

>>> import regex
__main__:1: DeprecationWarning: the regex module is deprecated; please
use the re module


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


Re: pickle

2005-10-25 Thread marduk
On Mon, 2005-10-24 at 23:55 -0700, Shi Mu wrote:
> I got a sample code and tested it but really can not understand the
> use of pickle and dump:
> 
> >>> import pickle
> >>> f = open("try.txt", "w")
> >>> pickle.dump(3.14, f)
> >>> pickle.dump([1,2,3,4], f)
> >>> f.close()

The pickle module "serializes" python objects.  You can "dump" a python
object that can later be loaded:

>>> x = complex(2,3.5)
>>> f = open("cnumber.pickle", "w")
>>> import pickle
>>> pickle.dump(x, f)
>>> f.close()
>>> y = pickle.load(file("cnumber.pickle", "r"))
>>> y
(2+3.5j)


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


Re: I Need Motivation Part 2

2005-11-04 Thread marduk
On Fri, 2005-11-04 at 17:06 +0100, Magnus Lycka wrote:
> If Python appears more complex
> than C++, you must be using a really weird approach.

Or a really weird editor ;-)

-m
> 

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


Re: strip not working on strings?

2005-11-13 Thread marduk
On Sun, 2005-11-13 at 13:16 -0800, [EMAIL PROTECTED] wrote:
> I'm using Python 2.3.5 and when I type the following in the interactive
> prompt I see that strip() is not working as advertised:
> 
> >>>s = 'p p:p'
> >>>s.strip(' :')
> 'p p:p'
> 
> Is this just me or does it not work? I want to get rid of all ' ' and
> ':' in the string. I've checked the doc and from what I can tell this
> is what strip() is supposed to do. Thanks for any help.
> 

According to my docs it says "Return a copy of the string with the
leading and trailing characters removed."  There are no leading or
trailing spaces or colons in 'p p:p'.

What your probably looking for is the .replace() method.

-m

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


Re: is parameter an iterable?

2005-11-15 Thread marduk
On Tue, 2005-11-15 at 11:01 -0800, py wrote:
> I have function which takes an argument.  My code needs that argument
> to be an iterable (something i can loop over)...so I dont care if its a
> list, tuple, etc.  So I need a way to make sure that the argument is an
> iterable before using it.  I know I could do...
> 
> def foo(inputVal):
> if isinstance(inputVal, (list, tuple)):
> for val in inputVal:
> # do stuff
> 
> ...however I want to cover any iterable since i just need to loop over
> it.
> 
> any suggestions?
> 

You could probably get away with 

if hasattr(inputVal, '__getitem__')


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


Re: Problems with os.system

2005-09-02 Thread marduk
On Fri, 2005-09-02 at 13:45 -0700, alexLIGO wrote:
> Hi,
> 
> I am trying to run a python script that executes several other programs
> on the bash (under linux) and reads some of the output of those
> programs. This works fine for a couple of os.system() calls, but then
> it does not work anymore. os.system() returns always -1, but when
> executing exactly the same program directly on the linux-bash it works!
> 
> Could this be a memory problem? It happens when there is much data
> stored in a python list. How can I check the memory usage in my python
> script? Can I force python to execute the program on the bash? What can
> I do?

Are you trying to read the output (stdout) of the program?  If so you
should be using os.popen() not os.system().  

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


Re: FTP status problems. (Again)

2005-09-16 Thread marduk
On Fri, 2005-09-16 at 19:27 -0700, Nainto wrote:
> Hello, I have posted before about trying to find the status of an FTP
> uplaod but couldn't get anything to work. After some more searching I
> found
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/76be9a994547db4/91917c906cdc04d4?q=ftp+progress&rnum=1#91917c906cdc04d4
> but it does not seem to work because it just uploads the file and does
> not print a . onto the screen. HEre is the code I have when I'm using
> the code from that link.
> import ftplib
> import os
> class dot_FTP(ftplib.FTP):
>   def storbinary(self, cmd, fp, blocksize=8192):
>   self.voidcmd('TYPE I')
>   conn = self.transfercmd(cmd)
>   while 1:
>   buf = fp.read(blocksize)
>   if not buf: break
>   conn.send(buf)
>   sys.stdout.write('.')
>   sys.stdout.flush()
>   conn.close()
>   return self.voidresp()
> 
> 
> ftp = ftplib.FTP("FTPADDRESS")
> ftp.login("user","pass")
> file = "/file"
> ftp.storbinary("STOR " + file, open(file, "rb"), 1024)
> ftp.quit()
> Does anyone know why this is not working? IS there any other way to
> find out when a chunc has been sent or the bytes uploaded of a file?
> Thanks.
> 

... and I haven't tried this myself, but you should be able to subclass
the builtin file object and prepare your own read() method.  Something
like

class ProgressFile(file):

def read(self, size = None):
print '.',

if size is not None:
return file.read(self, size)
else:
return file.read()

May need some tweaking.. then store the file as

ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)

Give it a try..

-m

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


Re: FTP status problems. (Again)

2005-09-16 Thread marduk
On Sat, 2005-09-17 at 04:42 +, marduk wrote:
> 
> ... and I haven't tried this myself, but you should be able to subclass
> the builtin file object and prepare your own read() method.  Something
> like
> 
> class ProgressFile(file):
> 
> def read(self, size = None):
> print '.',
> 
> if size is not None:
> return file.read(self, size)
> else:
> return file.read()
> 
> May need some tweaking.. then store the file as
> 
> ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)
> 
> Give it a try..
> 
> -m
> 

I corrected some errors and made some modifications to my previous post:

class ProgressFile(file):
def read(self, size = None):
from sys import stdout

if size is not None:
buff = file.read(self, size)
if buff:
stdout.write('.')
else:
stdout.write('\n')
return buff
else:
buff = ''
while True:
new_str = file.read(self, 1024)
stdout.write('.')
if new_str:
buff = buff + new_str
else:
stdout.write('\n')
break
return buff


if __name__ == '__main__':
import sys

fname = sys.argv[1]
f = ProgressFile(fname)
f.read()


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


Re: What is "self"?

2005-09-22 Thread marduk
On Thu, 2005-09-22 at 21:36 -0400, Wayne Sutton wrote:
> OK, I'm a newbie...
> I'm trying to learn Python & have had fun with it so far.  But I'm having 
> trouble following the many code examples with the object "self."  Can 
> someone explain this usage in plain english?

"self" references the object itself.  It's usually also called "this" on
other languages (C++ & Java I believe).  In Python, when you define a
class method, the reference to the object is passed explicitly rather
than implicitly.

Also, the name "self" is used by convention.  You could use any name if
you wanted, but if you want other people to understand your code then
use "self".

Is that plain English enough?  

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


Re: How to pop random item from a list?

2006-03-09 Thread marduk
On Thu, 2006-03-09 at 21:59 -0800, flamesrock wrote:
> Hi,
> 
> It's been a while since I've played with python.
> 
> My question is... whats the best way to pop a random item from a list??

import random
# ...

item = mylist.pop(random.randint(0,len(mylist)))


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