Re: Accessing class variable at class creation time

2005-09-23 Thread Simon Percivall
It might be that I'm complicating something easy here, but I
immediately thought of

import sys

class A:
X = 2
def F():
f = sys._getframe().f_back
print f.f_locals["X"]
F()

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


Re: bug or feature?

2005-10-05 Thread Simon Percivall
Python.org General FAQ 1.4.21: Why are default values shared between
objects?
(http://www.python.org/doc/faq/general.html#why-are-default-values-shared-between-objects)

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


Re: Python interpreter bug

2005-10-07 Thread Simon Percivall
Why would it be a bug? You've made it so that every instance of OBJ is
equal to every other instance of OBJ. The behaviour is as expected.

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


Re: subtle side effect of generator/generator expression

2005-10-16 Thread Simon Percivall
If you find that you want to iterate over an iterable multiple times,
have a look at the solution that the tee() function in the itertools
module provides (http://docs.python.org/lib/itertools-functions.html).
(Have a look at the rest of the itertools module as well, for that
matter.)

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


Re: Simple server/client application

2005-10-24 Thread Simon Percivall
You're calling the grid() method on the Entry object you're
instanciating. Are you sure that the grid() method returns the Entry
object so that you're actually binding it to self.myAddress?

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


Re: ElementTree - Why not part of the core?

2005-12-07 Thread Simon Percivall
Before that can happen we'll need some better management of co-existing
different versions of a package. You'll want to be able to use newer
versions of external packages without breakage in the standard library.

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


Re: What are __slots__ used for?

2005-07-05 Thread Simon Percivall
Most have already been said, but have a look at
http://docs.python.org/ref/slots.html for authoritative documentation.

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


Re: python classes taught

2005-08-19 Thread Simon Percivall
Yeha, sure. The Royal Institute of Technology in Stockholm, Sweden
teaches Python for some of its introductory programming and algorithm
courses.

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


Re: Release of PyPy 0.7.0

2005-08-28 Thread Simon Percivall
That's great! Congratulations!

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


Re: sizeof(long) from python

2005-09-04 Thread Simon Percivall
Take a look at the struct module
(http://docs.python.org/lib/module-struct.html), it does what you want.

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


Re: Possible bug in "metaclass resolution order" ?

2005-09-17 Thread Simon Percivall
Have you read the "Metaclasses" part of "Unifying types and classes in
Python 2.2"? (http://www.python.org/2.2.3/descrintro.html#metaclasses)

It discusses and explains the issues you seem to have.

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


Re: Possible bug in "metaclass resolution order" ?

2005-09-18 Thread Simon Percivall
If you have read the document I referred you to, did you also read the
example where classes M1, M2, M3 and M4 were defined?

A quote from the discussion of that example:
"For class D, the explicit metaclass M1 is not a subclass of the base
metaclasses (M2, M3), but choosing M3 satisfies the constraint, so
D.__class__ is M3."

Isn't that exactly what you are doing?

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


Re: Possible bug in "metaclass resolution order" ?

2005-09-18 Thread Simon Percivall
I definitely think that it's the intended behaviour: the example shows
how and why it works; and I definitely agree that it should be
documented better.

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


Re: threads/sockets quick question.

2005-09-19 Thread Simon Percivall
Why do you check if the module threading is less than 50? (this is why
nothing happens, it's always false).

>From where do you get port_counter in method run() of scanThread? (this
would make every call to run() raise an exception.

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


Re: programmatically calling a function

2005-03-05 Thread Simon Percivall
You might also want to take a peek at the getattr() function:

   http://docs.python.org/lib/built-in-funcs.html#l2h-31

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


Re: function expression with 2 arguments

2005-03-06 Thread Simon Percivall
Actually, lambda forms are quite precisely documented at
http://docs.python.org/ref/lambdas.html if you feel than reading
the tutorial (specifically http://docs.python.org/tut/node6.html
section 4.7.5) is too base for you.

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


Re: issues installing readine-1.12 on WinXP

2005-03-13 Thread Simon Percivall
That shouldn't happen AFAICT. Check line 108 in keysyms.py and make
sure it says "vk = VkKeyScan(ord(char))".

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


Re: issues installing readine-1.12 on WinXP

2005-03-13 Thread Simon Percivall
Possibly. Is the ` sign available as an unmodified key?

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


Re: issues installing readine-1.12 on WinXP

2005-03-13 Thread Simon Percivall
Well, just modify the source in that case.

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


Re: __getitem__ method on (meta)classes

2005-03-14 Thread Simon Percivall
Well, they're not synonymous. At least not in that context. If you
haven't already tried it, what you're doing will fail for instances as
well. Look in typeobject.c to see why. The gist of it is that the
special methods are looked up on the type rather than the instance (on
the metaclass rather than on the class, in your case), so the internal
lookup mechanism ignore instance attributes completely in this case.

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


Re: Python info

2005-03-14 Thread Simon Percivall
Well, the source code is pretty well documented if you want to get to
know the implementation. Read the "Extending and Embedding" tutorial
and the "Python/C API" reference, then start digging through the code.

Performance comparisons are broadly available, and always suspect.

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


Re: decorating classes with metaclass

2005-03-14 Thread Simon Percivall
Class decoration was discussed back when (you can search for the thread
in python-dev); not as an alias to metaclasses but discussed as having
exactly the same semantics as function decoration. Maybe the idea has
more merit as being another way of setting the __metaclass__ attribute;
on the other hand: it doesn't increase visibility and what is does and
how it does it is very different from that of function decoration; so
perhaps not.

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


Re: readonly class attribute ?

2005-03-15 Thread Simon Percivall
Start the attribute name with "_" and don't document it. If clients
mess with it, they're to blame.

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


Re: How to create stuffit files on Linux?

2005-03-15 Thread Simon Percivall
Stuffit Expander can handle zip, rar, tar, gz, etc, etc, etc. Don't
worry.

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


Re: os.path query functions behavior incorrect?

2005-04-05 Thread Simon Percivall
> These to me are I/O errors that should result in an exception.
> Doing a command line dir a:\ reports "The system cannot find
> the path specified."

The functions use the underlying C library, and in this case, the
result is not guaranteed by the standard.

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


Re: def a((b,c,d),e):

2005-04-18 Thread Simon Percivall
You can always unpack a tuple that way, like in:

.>>> import sys
.>>> for (index, (key, value)) in enumerate(sys.modules.iteritems()):
pass

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


Re: Inheritance problem?

2006-01-06 Thread Simon Percivall
Don't use self.__class__, use the name of the class.

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


Re: Help in using introspection to simplify repetitive code

2006-08-20 Thread Simon Percivall

[EMAIL PROTECTED] wrote:
> Hello.
> I'm writing a proxy class, i.e: a class whose methods mostly delegate
> their functionality to other class object. Most of the methods (which
> are quite a lot) defined in the class would end up being:
>
> def thisIsTheMethodName(self):
> self._handlerClass.thisIsTheMethodName()
>
> The handler object is the same in all methods.
>
> I was wondering if there is a way to simplify this proxy class, maybe
> using some pythonic technique like metaclasses, introspection... any
> suggestion is appreciated.
>
> Thanks,
>
> Javier Sanz

http://docs.python.org/ref/attribute-access.html#l2h-206

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


Re: Python less error-prone than Java

2006-06-03 Thread Simon Percivall
Actually, you're wrong on all levels.

First: It's perfectly simple in Java to create a binary sort that sorts
all arrays that contain objects; so wrong there.

Secondly: The bug has nothing to do with static typing (I'm guessing
that's what you meant. Both Python and Java are strongly typed). The
problem is that ints are bounded in Java. They could easily have been
ints and then automatically coerced to (equivalent to) longs when they
got bigger; that they aren't is more a design fault than anything to do
with static typing. The equivalent in Python would have been if an
overflow exception was raised when the int got too big. It might have
been that way, typing or no typing.

Christoph Zwerschke wrote:
> You will often hear that for reasons of fault minimization, you should
> use a programming language with strict typing:
> http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html
>
> I just came across a funny example in which the opposite is the case.
>
> The following is a binary search algorithm in Java. It searches a value
> in an ordered array a of ints:
>
> public static int binarySearch(int[] a, int key) {
>  int low = 0;
>  int high = a.length - 1;
>  while (low <= high) {
>  int mid = (low + high) / 2;
>  int midVal = a[mid];
>  if (midVal < key)
>  low = mid + 1;
>  else if (midVal > key)
>  high = mid - 1;
>  else
>  return mid; // key found
>  }
>  return -(low + 1);  // key not found.
> }
>
> Now the same thing, directly converted to Python:
>
> def binarySearch(a, key):
>  low = 0
>  high = len(a) - 1
>  while low <= high:
>  mid = (low + high) / 2
>  midVal = a[mid]
>  if midVal < key:
>  low = mid + 1
>  elif midVal > key:
>  high = mid - 1;
>  else:
>  return mid # key found
>  return -(low + 1) # key not found.
>
> What's better about the Python version? First, it will operate on *any*
> sorted array, no matter which type the values have.
>
> But second, there is a hidden error in the Java version that the Python
> version does not have.
>
> See the following web page if you dont find it ;-)
> http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
> 
> -- Christoph

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


Re: numeric/numpy/numarray

2006-06-13 Thread Simon Percivall

Bryan wrote:
> hi,
>
> what is the difference among numeric, numpy and numarray?  i'm going to start
> using matplotlib soon and i'm not sure which one i should use.
>
>
> this page says, "Numarray is a re-implementation of an older Python array 
> module
> called Numeric"
> http://www.stsci.edu/resources/software_hardware/numarray
>
> this page says, "NumPy derives from the old Numeric code base and can be used 
> as
> a replacement for Numeric."
> http://numeric.scipy.org/
>
> i looked at the matplotlib examples today and if i remember correctly, the
> examples didn't use numarray.
>
> so i'm a bit confused.
>
> thanks,
>
> bryan

Look again at numeric.scipy.org, and this time: read the whole page,
especially the section called "Older Array Packages".

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


Re: FIXED: Re: optparse multiple arguments

2006-06-30 Thread Simon Percivall

Ritesh Raj Sarraf wrote:
> Ritesh Raj Sarraf wrote:
> > I just noticed that the args variable is holding values b and c.
> > the args variables comes from:
> > (options, args) = parser.parse_args()
> >
> > I guess I only need to figure out now is why args isn't storing
> > argument "a" also...
> >
> > Ritesh
>
> I fixed it, I guess.
>
> parser.add_option("", "--my-option", dest="my_option",
> action="store_true")
>
> sets my_option to True and the arguments are all stored in the list
> "args". :-)
>
> Ritesh

It might do you good to read the documentation instead of blindly
experimenting.

Anyway,

parser.add_option("", "--my-option", nargs=3)

http://docs.python.org/lib/optparse-standard-option-actions.html

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


Re: where the extra space comes from on the stdout

2006-10-02 Thread Simon Percivall

alf wrote:
> Hi,
>
> I can not find out where the extra space comes from. Run following:
>
> import os,sys
> while 1:
>  print 'Question [Y/[N]]?',
>  if sys.stdin.readline().strip() in ('Y','y'):
>  #do something
>  pass
>
> $ python q.py
> Question [Y/[N]]?y
>   Question [Y/[N]]?y
>   Question [Y/[N]]?y
>   Question [Y/[N]]?y
>   Question [Y/[N]]?n
>   Question [Y/[N]]?
>   Question [Y/[N]]?
>
>
> There is a space evrywhere just before Q
>
> Any insight?

You already got the answer, but as for the rest: It's really easier for
you if you use raw_input() for your question/input pair instead.

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


Re: TypeError when subclassing 'list'

2006-02-26 Thread Simon Percivall
The error you're seeing is because you've rebound 'list' to something
else. Try putting "list = type([])" somewhere above your code.

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


Re: calling a class method

2006-03-01 Thread Simon Percivall
Read this: http://users.rcn.com/python/download/Descriptor.htm

Long story short: The type of the instance is passed along together
with the instance itself.

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


Re: handling more databases with ZEO

2005-05-07 Thread Simon Percivall
You should take a look at
http://www.zope.org/Wikis/ZODB/FrontPage/guide/index.html

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


Re: MacOS X drag & drop?

2005-05-15 Thread Simon Percivall
Take a look at Platypus at http://sveinbjorn.sytes.net/platypus. It
will make it easier for you.

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


Re: readline module and white-space

2005-05-19 Thread Simon Percivall
Take a look at readline.get_completer_delims() and
readline.set_completer_delims().

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


Re: line-by-line output from a subprocess

2005-05-23 Thread Simon Percivall
Okay, so the reason what you're trying to do doesn't work is that the
readahead buffer used by the file iterator is 8192 bytes, which clearly
might be too much. It also might be because the output from the
application you're running is buffered, so you might have to do
something about that as well.

Anyway, if the output from the child application is unbuffered, writing
a generator like this would work:

def iterread(fobj):
stdout = fobj.stdout.read(1) # or what you like
data = ""
while stdout:
data += stdout
while "\n" in data:
line, data = data.split("\n", 1)
yield line
stdout = fobj.stdout.read(1)
if data:
yield data,

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


Re: overhead of starting threads

2005-05-23 Thread Simon Percivall
How much you gain by starting threads is also determined by what you're
doing in those threads. Remember (or learn): In CPython only one thread
at a time can execute python code, so depending on your task threading
might gain you little. If you're doing I/O or calling functions written
in C (and if they release the Global Intepreter Lock [GIL]) you might
gain a lot by using threads.

As for overhead: profile, profile, profile. You'll have to do sample
runs and find your sweet-spot. It all depends on what you're doing in
the threads.

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


Re: line-by-line output from a subprocess

2005-05-23 Thread Simon Percivall
Jp Calderone wrote:
> Or, doing the same thing, but with less code:

Hmm ... What have I been smoking?

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


Re: vim configuration for python

2005-05-25 Thread Simon Percivall
I don't know if the binary editions include the Misc directory, but if
you download the Python source you'll find a directory called Misc. In
it, there's a vimrc file.

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


Re: __call__

2005-05-28 Thread Simon Percivall
Look at http://docs.python.org/ref/callable-types.html

>>> class Test(object):
... def __call__(self):
... print "the instance was called"
...
>>> t = Test()
>>> t()
the instance was called

Is this what you wanted?

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


Re: PYTHONSTARTUP and the -i command line option

2005-06-03 Thread Simon Percivall
After entering the interpreter, you could do an execfile on the
.pythonrc file.

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


Re: socket programming related.

2007-07-11 Thread Simon Percivall
On Jul 12, 2:35 am, [EMAIL PROTECTED] wrote:
> On Jul 11, 7:32 pm, [EMAIL PROTECTED] wrote:
>
> > I have just started working in network programming using python.
> > written code for socket connection between client and server. Client
> > sent data to server for server processing (also server echoing back
> > rcvd data to client). When there is ("if no data": break ) no data
> > from client then the while loops break in server. The server program
> > process recvd data and my requirement is to send back the *results* to
> > client program for user sake. I did many trials but no use, as these
> > socket calls are blocking, i could not make my req.
>
> I just need to add a line. The while loop in server breaks when the
> sockObj.close() happens in client program indicating there is no data
> from client.

Well, you shouldn't do a close() on the client socket, you should do a
shutdown(1).

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


Re: pytz giving incorrect offset and timezone

2007-07-12 Thread Simon Percivall
On Jul 12, 11:47 am, Sanjay <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> Using pytz, I am facing a problem with Asia/Calcutta, described below.
>
> Asia/Calcutta is actually IST, which is GMT + 5:30. But while using
> pytz, it was recognized as HMT (GMT + 5:53). While I digged into the
> oslan database, I see the following:
>
> # Zone NAME GMTOFF RULES FORMAT [UNTIL]
> Zone Asia/Calcutta 5:53:28 - LMT 1880 # Kolkata
>5:53:20 - HMT 1941 Oct # Howrah Mean Time?
>6:30 - BURT 1942 May 15 # Burma Time
>5:30 - IST 1942 Sep
>5:30 1:00 IST 1945 Oct 15
>5:30 - IST
>
> Searching in this group, I saw a similar problem posted 
> athttp://groups.google.co.in/group/comp.lang.python/browse_thread/threa...
> without any solutions.
>
> I mailed to Stuart and also posted it at the launchpad of pytz, but
> did not get any response.
>
> Unable to know how to proceed further. Any suggestion will be of vital
> help.
>
> thanks
> Sanjay

I don't use pytz myself that often so I can't be sure, but I don't
think it's a bug in pytz.

The problem seems to be that the timezone has changed for the
location. Now, without a date as reference, pytz can't know what
timezone to use when constructing the tzinfo; you might want a date
from the 1800's.

When you're constructing the datetime with the tzinfo argument, you're
saying: use this timezone as the local timezone. datetime_new (the
constructor in C) never calls the tzinfo to verify that the timezone
is still valid, it just uses it.

On the other hand: When you construct a datetime with datetime.now()
and pass a timezone, datetime_now (again, in C) calls the method
fromutz() on the tzinfo object. Now the pytz tzinfo object has a
reference by which to choose the current timezone for the location,
and that's why it's correct when you use datetime.now() but not for a
manual construction.

A "workaround" (or maybe the proper way to do it) is to construct the
datetime without a tzinfo set, and then use the localize() method on
the tzinfo object, this will give you the correct result.

>>> tz = pytz.timezone("Asia/Calcutta")
>>> mydate = datetime.datetime(2007, 2, 18, 15, 35)
>>> print tz.localize(mydate)
2007-02-18 15:35:00+05:30

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


Re: gcc errors

2006-04-27 Thread Simon Percivall
It doesn't think you're on an intel box, it thinks you want to compile
universal libraries, since you installed a universal python.

The problem is likely to be that you haven't installed SDK's for intel
as well as powerpc when you installed Apple's Developer Tools. Do that,
and it should work ... I think.

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


Re: elementtidy, \0 chars and parsing from a string

2006-05-09 Thread Simon Percivall
Well, it seems you can do:

parser = elementtidy.TidyHTMLTreeBuilder.TidyHTMLTreeBuilder()
parser.feed(your_str)
tree = elementtree.ElementTree.ElementTree(element=parser.close())

Look at the parse() method in the ElementTree class.

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


Re: Pyrex speed

2006-05-27 Thread Simon Percivall
You can gain substantial speed-ups in very certain cases, but the main
point of Pyrex is ease of wrapping, not of speeding-up.

Depending on what you're doing, rewriting in Pyrex or even in C, using
the Python/C API directly, might not gain you much.

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