Re: What is "self"?

2005-09-23 Thread Sion Arrowsmith
Rick Wotnaz  <[EMAIL PROTECTED]> wrote:
>I've long thought that Guido missed an opportunity by not choosing 
>to use 'i' as the instance identifier, and making it a reserved 
>word. For one thing, it would resonate with the personal pronoun 
>'I', and so carry essentially the same meaning as 'self'. It could 
>also be understood as an initialism for 'instance'. And, because it 
>is shorter, the number of objections to its existence *might* have 
>been smaller than seems to be the case with 'self' as the 
>convention.

My first serious forays into Python, where no-one else was expected
to be maintaining the code, used 'I' instead of 'self' -- it's
shorter, stands out better, and 'I.do_something()' reads more like
English than 'self.do_something()' (unless, I suppose, you're
thinking in terms of message passing). Then I started working on
code which other people might need to look at, and got an editor
whose Python syntax highlighting pretended that 'self' was a
reserved word, and now all my old code looks odd. (But still
perfectly readable -- this is Python after all.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: What python idioms for private, protected and public?

2005-09-30 Thread Sion Arrowsmith
Michael Schneider  <[EMAIL PROTECTED]> wrote:
>I have been coding in C++ since the late 80's and Java since the late 90's.
>
>I do use private in these languages, with accessors to get at internal 
>data.
>
>This has become an ingrained idiom for me.

The question is, has it become a purely instinctive idiom, or are
you getting real benefit from it?

My experience over a similar length of time (well, maybe not quite
so long with C++) is that private is far more likely to hinder than
help, and that protected is a more sensible state for things which
aren't on the public interface. And since working seriously with
Python, I've come round to the point of view that the public
interface should be defined by what you document it as, and not
enforced by language gimmicks. Which is just duck typing from
another angle.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: [Info] PEP 308 accepted - new conditional expressions

2005-10-03 Thread Sion Arrowsmith
Ron Adam  <[EMAIL PROTECTED]> wrote:
>So ...
>
>   A if B else C + X * Y
>
>Would evaluate as... ?
>
>   A if B else (C + X * Y)

"""In general, 'if' and 'else' bind less tight than everything except
lambda."""

http://mail.python.org/pipermail/python-dev/2005-September/056846.html

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to send a SIGINT to a Python process?

2005-10-03 Thread Sion Arrowsmith
Michele Simionato <[EMAIL PROTECTED]> wrote:
>Is there a way to send a SIGINT/KeyboardInterrupt to a
>Python process (knowing the pid) that works both on Unix and Windows?

No. Windows doesn't support the sending of signals between processes
(with the exception of an equivalent of SIGKILL). I guess this explains
why server processes running on Windows need to be shutdown and
restarted (at least) if you change their configuration -- there's no
(convenient or standardised) way of doing the equivalent of SIGHUP.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Python vs Ruby

2005-10-21 Thread Sion Arrowsmith
bruno modulix  <[EMAIL PROTECTED]> wrote:
>> "Casey Hawthorne" <[EMAIL PROTECTED]> wrote in message 
>> news:[EMAIL PROTECTED]
>>>I have heard, but have not been able to verify that if a program is
>>>about
>>>10,000 lines in C++
>>>it is about
>>>5,000 lines in Java
>>>and it is about
>>>3,000 lines in Python (Ruby to?)
>
>For a whole lot of common tasks (like file IO etc), the Java/Python loc
>ratio is between 5/1 and 10/1. Also, not having the dumbest type system
>in the world, Python is naturally much more generic than Java, which
>saves a lot of boilerplate code. I think that the real numbers would be
>much like 5000 lines in Java -> 1000 lines in Python - and probably 5000
>-> 500 in some cases.

I have here a library (it's the client side of a client-server
interface including a pile of class definitions) which has
implementations in pure C++, Java and Python, taking about 3000,
3500 and 1500 loc respectively. And there's an associated module
(with no C++ implementation) that I remember being particular
"impressed" while writing it to find being 3 times as long in Java
as Python despite (a) extensive (and pretty much common) Javadoc/
docstrings and (b) implementing in the Python version a feature
present in the standard Java library (scheduling a thread to run
at specified intervals and time out). Strip the Javadoc/docstrings
out and it's about at that 5:1 ratio.

As for Ruby, looking at some of the comparisons given elsewhere
in this thread, my intuition is that it would be more loc than
Python due to all the explicit end s.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Python and MySQL

2005-11-03 Thread Sion Arrowsmith
Aquarius <[EMAIL PROTECTED]> wrote:
>I want to know if there is a way to interface a MySQL database without
>Python-MySQL or without installing anything that has C files that need
>to be compiled. The reason for this, is that I want to develop a
>certain web application, but my hosting provider ([EMAIL PROTECTED]@#%) isn't 
>very
>eager to supply Python-MySQL (or any modules to python). Is there an
>alternative approach I could use to pass around this ridiculos lack of
>functionality?

If you've got MySQL installed, you probably have a command-line
mysql client, and you could use subprocess or popen to run that.
That's a little desperate though, and the error handling will suck.
(I've seen this trick used with other databases.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: strange sockets

2005-11-04 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>, Skink  <[EMAIL PROTECTED]> wrote:
>% python client.py client.py client.py client.py server.py server.py
>init 0.00066089630127
>client.py 0.000954866409302
>client.py 0.0408389568329
>client.py 0.0409188270569
>server.py 0.0409059524536
>server.py 0.0409259796143
>
>what's wrong here?

That smells of a Nagle/delayed ACK problem to me (see, for instance,
http://www.port80software.com/200ok/archive/2005/01/31/317.aspx). 40ms
is the default delayed ACK timeout on Linux, IIRC (pretty much
everything else uses 200ms). I *think* what's happening from the
server's point of view is:

receive request 1
send length (first undersized packet is sent immediately by Nagle)
(client delays ack #1)
send data (larger than 1 packet, send immediately)
(client delays acks #2--#n)
receive request 2 with ack #1
buffer sending length (undersized packet, not received last ack)
-> 40ms passes <-
(client timesout delayed acks and sends)
send length
send data (as before)

although why the undersized packet at the end of the first chunk of
data isn't buffered, I don't know.

Solutions: either change

>conn.sendall(struct.pack("!i", len(data)))
>conn.sendall(data)

to

conn.sendall(struct.pack("!i", len(data)) + data)

or after creating conn

conn.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)

to disable Nagle.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: which feature of python do you like most?

2005-11-08 Thread Sion Arrowsmith
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>which feature of python do you like most?

A different thing every time I encounter the corresponding misfeature
in another language. But a lot of it boils down to the cleanliness of
syntax when handling complex datastructures aggregated from basic
types, especially for use of functions(/bound methods) as first class
objects and the ease of iterating over collections. And much of the
rest is not having to jump through the hoops and boilerplate of static
typing.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: What do you use as symbols for Python ?

2005-11-11 Thread Sion Arrowsmith
Gary Herron  <[EMAIL PROTECTED]> wrote:
>Another similar approach that keeps those values together in a single 
>namespace is this (my favorite):
>
>  class State:
>  OPENED, CLOSED, ERROR = range(3)
>
>Then you can refer to the values as
>State.OPENED
>State.CLOSED
>State.ERROR
>
>The extra clarity (and slight wordiness) of the dotted notation seems, 
>somehow, quite Pythonic to me.

I have here an implementation (written by a colleague) of a whole pile
of such -- in this particular case it's helpful to do it in this style
rather than the class OPENED: pass because the values are coming from/
going to a database. And it goes a little further, with

class State:
Enum = range(3)
OPENED, CLOSED, ERROR = Enum
Names = { OPENED: "OPENED", CLOSED: "CLOSED", ERROR: "ERROR" }

so you can used State.Names[state] to provide something user-readable,
and state in State.Enum to check data consistency. (OK, that probably
doesn't make much sense with this particular State, but it does when
your getting value-as-number from an external source.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: help make it faster please

2005-11-11 Thread Sion Arrowsmith
 <[EMAIL PROTECTED]> wrote:
>Oh sorry indentation was messed here...the
>wordlist = countDict.keys()
>wordlist.sort()
>should be outside the word loop now
>def create_words(lines):
>cnt = 0
>spl_set = '[",;<>{}_&?!():-[\.=+*\t\n\r]+'
>for content in lines:
>words=content.split()
>countDict={}
>wordlist = []
>for w in words:
>w=string.lower(w)
>if w[-1] in spl_set: w = w[:-1]
>if w != '':
>if countDict.has_key(w):
>countDict[w]=countDict[w]+1
>else:
>countDict[w]=1
>wordlist = countDict.keys()
>wordlist.sort()
>cnt += 1
>if countDict != {}:
>for word in wordlist: print (word+' '+
>str(countDict[word])+'\n')
>
>ok now this is the correct question I am asking...

(a) You might be better off doing:
words = words.lower()
for w in words:
...
instead of calling lower() on each separate word (and note that most
functions from string are deprecated in favour of string methods).

(b) spl_set isn't doing what you might think it is -- it looks like
you've written it as a regexp but your using it as a character set.
What you might want is:
spl_set = '",;<>{}_&?!():-[\.=+*\t\n\r'
and
while w[-1] in spl_set: w = w[:-1]
That loop can be written:
w = w.rstrip(spl_set)
(which by my timings is faster if you have multiple characters from
spl_set at the end of your word, but slower if you have 0 or 1).

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to convert a "long in a string" to a "long"?

2005-11-18 Thread Sion Arrowsmith
Steven Bethard  <[EMAIL PROTECTED]> wrote:
>[EMAIL PROTECTED] wrote:
>> s = long("0xL")
>> ValueError: invalid literal for long(): 0xL
> >>> int("0x", 0)
>4294967295L

So why does the base argument to int() (or long()) default to
10 and not 0?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: about sort and dictionary

2005-11-23 Thread Sion Arrowsmith
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>OKB (not okblacke) wrote:
>> Fredrik Lundh wrote:
>> > [EMAIL PROTECTED] wrote:
[ ... ]
>> >> > so what would an entry-level Python programmer expect from this
>> >> > piece of code?
>> >> >
>> >> > for item in a.reverse():
>> >> > print item
>> >> > for item in a.reverse():
>> >> > print item
>> >> >
>> >> I would expect it to first print a in reverse then a as it was.
>> >>
>> >> a=[1,2,3]
>> >>
>> >> I expect it to print
>> >>
>> >> 3
>> >> 2
>> >> 1
>> >> 1
>> >> 2
>> >> 3
>> > really?  wouldn't
>> >
>> > 3
>> > 2
>> > 1
>> > 3
>> > 2
>> > 1
>> >
>> > make a lot more sense ?
>>  Yes.  The unintuitive thing is that the list is sorted in place at
>> all.
>intuitive seems to be a very subjective matter, depends on once
>background etc :-)

A quick straw-poll of some non-Pythonistas (two sysadmins, two
programmers) suggests that reversing in place is unintuitive --
all four expected:

3
2
1
3
2
1

as suggested by Fredrik. It was less clear-cut, talking through
it, whether they found sorting inplace was intuitive or not, but
it was agreed that it would be "odd" if sort() and reverse()
behaved differently. All of which is to say I'm convinced by the
current behaviour:

1. sort() in place makes sense in terms of space, and is not
   completely unintuitive.
2. reverse() should do what sort() does.
3. The inexperienced user is most likely to expect the above
   code to print 3 2 1 3 2 1, and is more likely to have
   difficulty tracking down the problem if reverse() returns
   self and they get unexpected results than if it returns
   None and they get a TypeError: iteration over non-sequence.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: i=2; lst=[i**=2 while i<1000]

2005-12-06 Thread Sion Arrowsmith
 <[EMAIL PROTECTED]> wrote:
>Bengt Richter wrote:
>>  >>> list(iter(lambda b=[2]:b.append(b[0]**2) or b[0]<1000 and b.pop(0) or 
>> None, None))
>>  [2, 4, 16, 256]
>out of curiosity, what stops the iterator ?

:

iter(o, sentinel) [ ... ]
The iterator created in this case will call o with no arguments for
each call to its next() method; if the value returned is equal to
sentinel, StopIteration will be raised, otherwise the value will be
returned.

In this case, o is:

lambda b=[2]:b.append(b[0]**2) or b[0]<1000 and b.pop(0) or None

which returns None when b[0]>=1000 (None or (False and _) or None
evaluates to the last None).

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Bitching about the documentation...

2005-12-08 Thread Sion Arrowsmith
Steven D'Aprano  <[EMAIL PROTECTED]> wrote:
>>> "Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo."
>
>S
>P
>O
>I
>L
>E
>R
> 
>S
>P
>A
>C
>E
>


(Good grief, I've not done that in *years*.)

>Buffalo from the city of Buffalo, which are intimidated by buffalo
>from Buffalo, also intimidate buffalo from Buffalo.
>
>I didn't say it was *good* English, but it is *legal* English.

I *think* that's similar to the one I know about the cannibalistic
behaviour of some oysters, which split open other oysters (to eat
them). It starts:

"Oysters oysters split split."

Oysters which oysters split become split.

But there's nothing to stop a third set of oysters predating on the
ones doing the splitting:

"Oysters oysters oysters split split split."

And so on. My brain hurts too much to work out if you can do the
same to the buffaloes.

And here endeth today's lesson in recursion.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: What YAML engine do you use?

2005-01-24 Thread Sion Arrowsmith
Paul Rubin   wrote:
>YAML looks to me to be completely insane, even compared to Python
>lists.  I think it would be great if the Python library exposed an
>interface for parsing constant list and dict expressions, e.g.:
>   [1, 2, 'Joe Smith', 8237972883334L,   # comment
>  {'Favorite fruits': ['apple', 'banana', 'pear']},  # another comment
>  'xyzzy', [3, 5, [3.14159, 2.71828, [
> [ ... ]
>Note that all the values in the above have to be constant literals.
>Don't suggest using eval.  That would be a huge security hole.

I'm probably not thinking deviously enough here, but how are you
going to exploit an eval() which has very tightly controlled
globals and locals (eg. eval(x, {"__builtins__": None}, {}) ?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is print? A function?

2005-01-24 Thread Sion Arrowsmith
Michael Hoffman  <[EMAIL PROTECTED]> wrote:
>Frans Englich wrote:
>> Nah, I don't think it's a function, but rather a builtin "statement". But 
>> it's 
>> possible to invoke it as an function; print( "test" ) works fine.
>That is not invoking it as a function. The parentheses are only for 
>ordering the expression on the right
>
>You can do this too:
>
> >>> print("abc"),("def"),("ghi")
>abc def ghi

And for further enlightenment:

.>>> print("abc", "def", "ghi")
('abc', 'def', 'ghi')

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What YAML engine do you use?

2005-01-25 Thread Sion Arrowsmith
Fredrik Lundh <[EMAIL PROTECTED]> wrote:
>Sion Arrowsmith wrote:
>> I'm probably not thinking deviously enough here, but how are you
>> going to exploit an eval() which has very tightly controlled
>> globals and locals (eg. eval(x, {"__builtins__": None}, {}) ?
>try this:
>
>eval("'*'*100*2*2*2*2*2*2*2*2*2")

No thanks.

I guess my problem is a tendency view security issues from the
point of view of access to data rather than access to processing.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RE:"private" variables a.k.a. name mangling (WAS: What is print? A function?)

2005-01-25 Thread Sion Arrowsmith
Jeremy Bowers  <[EMAIL PROTECTED]> wrote:
> [ ... ] the Python community, and in general the dynamic language
>community, has become increasingly confident that private variables don't
>solve *real* problems.

Years of writing and maintaining others' C++ and Java code (plus
one year of maintaining Python code and rather more writing) has
led me to believe that there is no justification for truly private
variables. "protected" yes, I can see the argument for, but
denying derived classes full access to your inner workings just
leads to clumsier (less readable and more bug-prone) implementations
derivations. (The same applies to Java's "final".) And it's based on
the hubris that you are a better programmer than anyone who might
want to extend your class and can forsee all circumstances in which
it might be subclassed.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inheriting from object

2005-06-30 Thread Sion Arrowsmith
Bruno Desthuilliers  <[EMAIL PROTECTED]> wrote:
>Fuzzyman a écrit :
>> *Should* I in fact write :
>> 
>> class foo(object):
>> def __init__(self, *args, **kwargs):
>> object.__init__(self)
>> 
>> ?
>Nope.

And if you were to do so, surely:

class foo(object):
def __init__(self, *args, **kwargs):
super(foo, self).__init__(self)

would be the preferred way to go?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: map vs. list-comprehension

2005-07-01 Thread Sion Arrowsmith
Tom Anderson  <[EMAIL PROTECTED]> wrote:
>On Thu, 30 Jun 2005, Roy Smith wrote:
>> Even some of the relatively recent library enhancements have been kind 
>> of complicated.  The logging module, for example, seems way over the 
>> top.
>Exactly the same thing happened with Java.

I was under the impression that Python's logging module (like unittest)
was based on a common Java one, and it's complexity could be blamed on
that.

> if you look at the libraries 
>that were in 1.1, they're very clean and simple (perhaps with the 
>exception of AWT). 1.2 added a load of stuff that was much less 
>well-designed (with the notable exception of the collections stuff, which 
>is beautiful)

There are very many adjectives I could (and have) used to describe
the Collection framework. "Beautiful" is not among them. I think
the closest I could manage is "baroque".

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Strange os.path.exists() behaviour

2005-07-07 Thread Sion Arrowsmith
Jeff Epler  <[EMAIL PROTECTED]> wrote:
>Pierre wrote:
>> Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on wi=
>n32
>   =
>^^^
>Here's the bug.  You're using Windows.  It's a filesystem, but not as we kn=
>ow it...
> [ ... ]
>As you can see, not only does Windows think that "exist" exists, but it=
> can
>successfully "type" its contents too!

The "reason" for this behaviour is that the concept of a file
extension is firmly embedded in the operating system. exists is a
file with no extension, as is exists. (no, it doesn't make a
distinction between "no extension" and "an empty extension").
>From there it's not too great a leap of induction to suppose that
exists is the same file called exists with no extension. It's
not unlike being able to stick an arbitrary number of /s onto the
end of a directory name on a sane OS/filesystem.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-07 Thread Sion Arrowsmith
=?ISO-8859-2?Q?Pawe=B3?= Sakowski  <[EMAIL PROTECTED]> wrote:
 ll=[[1,2],[3,4,5],[6]]
 sum(ll,[])
>[1, 2, 3, 4, 5, 6]

That's a great argument for list.__add__ having the semantics of
extend rather than append 8-)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: more newbie list questions

2005-07-14 Thread Sion Arrowsmith
Bernhard Holzmayer  <[EMAIL PROTECTED]> wrote:
>googleboy wrote:
>> I have a cell.txt file that looks like this:
>> 
>> ++
>> The title is %title%.  
>> The author is %author1% %author2% 
>> The Publisher is %publisher1% %publisher2% 
>> The ISBN is %ISBN% 
>> ++
>
>This looks like a DOS-batch-file. Maybe you'd better just leave it to
>DOS to populate it, just by exec-uting it  in an environment which 
>has title, authort1, ... set. ??
>
>On the other hand, if you need not relate to the cell.txt file, 
>you could just use something like
> 
>sAuth = "The author is %s" % author1

Or

sAuth = "The author is %(author1)s" % locals()

so cell.txt could be replaced by something like

++
The title is %(title)s.  
The author is %(author1)s %(author2)s 
The Publisher is %(publisher1)s %(publisher2)s 
The ISBN is %(ISBN)s 
++

and you could do everything at once. Obviously you'd be better
off sticking author1 etc. into a dict instead of making them
local variables. This might also solve your problems with
"getting fields into Python" (as someone else said, it's not
entirely clear what your difficulties are):

field_names = ["title", "author1", "author2", "publisher", "ISBN"]
fields_dict = dict(zip(field_names, fields))
sTemplate % field_dict

>> I know how to do something like sAuth = re.sub('%author1%', author1,
>> sTemplate)

re.sub() is massive overkill. If you can't get where you want
with % as above, use str.replace instead:

sTemplate.replace('%author1%', author1)

(Er, "you" above refers to OP, not Bernhard.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Consecutive Character Sequences

2005-07-14 Thread Sion Arrowsmith
Aries Sun <[EMAIL PROTECTED]> wrote:
>I used Python 2.4.1, the following are the command lines.
>But the reslut was still False. Is there anything wrong with below
>codes?
 import itertools as it
 def hasConsequent(aString, minConsequent):
>   for _,group in it.groupby(aString):
>   if len(list(group)) >= minConsequent:
>   return True
>   return False

Yes: return False is at the wrong indentation level.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: multiple inheritance super()

2005-07-28 Thread Sion Arrowsmith
Reinhold Birkenfeld  <[EMAIL PROTECTED]> wrote:
>Michele Simionato wrote:
>>  was I
>> going to design a new language
>> I would implement it *without* multiple inheritance).

That way lies Java. The number of times I've wished an interface
were actually a mixin *shudder*

>Multiple inheritance can be very useful if not used in an unresponsible way.

Exactly. I think the correct approach is to regard multiple
inheritence as a very advanced OO topic, leave it out of tutorials as
much as possible, and when finally introduced reinforce the idea that
it should be used only with caution. Speaking as someone who has used
it to commit monstrosities in C++ which I have grown to regret.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Dabo in 30 seconds?

2005-08-02 Thread Sion Arrowsmith
James Stroud  <[EMAIL PROTECTED]> wrote:
>Perhaps some of us are writing software with non-developer end-users in mind
>and we kind of keep that mentality when evaluating modules our code uses.

In the commercial environment I'm working in, non-developer end-users
get a frozen executable. They don't know that there are eight packages
not included in the standard library (including wxPython) in there,
unless they go looking in the licences directory. They don't even have
to install Python. Using a swarm of potentially awkward third-party
modules and keeping things simple for the user are not incompatible.

Of course, going back to Dabo there is the question of whether it has
such a thing as a non-developer end-user.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: searching a list of dictionaries for an element in a list.

2005-08-10 Thread Sion Arrowsmith
Dan  <[EMAIL PROTECTED]> wrote:
>> [ someone else wrote: ]
>> I want to search list1, and the result should be all dictionaries where
>> primarycolor is in input. I can do this using a double for-loop, but is
>> there a more efficent way?
>
>Of course.:-)
>
>L = [dict for dict in list1 if dict['primarycolor'] in input]

Note that (1) shadowing builtin dict is a bad idea and (2) that's
still two nested loops -- I think you do theoretically better with

input_set = set(input)
output_list = [ d for d in dict_list if d['primarycolor'] in input_set ]

but I suspect input would have to be a rather large list to favour
this.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: What are modules really for?

2005-08-10 Thread Sion Arrowsmith
infidel <[EMAIL PROTECTED]> wrote:
>> [ somebody else wrote: ]
>> To my mind, although one CAN put many classes in a file, it is better to
>> put one class per file, for readability and maintainability.
>Personally I find it easier to maintain a set of related classes when
>they're all in the same file.

Real world example: I have here an API I'm one of the maintainers of
which is available in C++, Java and Python versions. Part of the
nature of this beast is that it has about 20 exception classes derived
from the same base class. So that's one .py for the Python interface,
a .h and a .cxx for the C++ (there is some logic in the base class
which counter-indicates stuffing it all in one .h), and 21 .java files
(plus their corresponding .classes). Take a guess which ones[*] are
easiest to maintain. ([*] hint)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Newbie Question

2005-08-19 Thread Sion Arrowsmith
Tom Strickland <[EMAIL PROTECTED]> wrote:
>I have a file that contains many lines, each of which consists of a string 
>of comma-separated variables, mostly floats but some strings. Each line 
>looks like an obvious tuple to me. How do I save each line of this file as a 
>tuple rather than a string? Or, is that the right way to go?

Depending on exactly what format you've got, either .split(',') on
the line, or if this is insufficient look at the csv module. You'll
then need some way of turning the list of strings both of these
will give you into the mixed float/string tuple you want, which
could be somewhat tedious. Without knowing what these lines look
like, or what they represent, I can't begin to guess how you might
go about it. Actually, I can -- I'd start by considering whether a
dict might be more appropriate than a tuple and use csv.DictReader.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Network performance

2005-08-23 Thread Sion Arrowsmith
Roland Hedberg  <[EMAIL PROTECTED]> wrote:
> [ ... ]
>The client sends a number of lines (each ending with \n) and ends one  
>set of lines with a empty line.
>When the client sends a line with only a "." it means "I'm done close  
>the connection".
>
>Letting the client open the connection and sending a number of sets I  
>was surprised to find that the performance was equal to what Twisted/ 
>XMLRPC did. Around 200 ms per set, not significantly less.
>
>So what can be done to speed up things or is Python incapable of  
>supporting fast networking (just a teaser).

That 200ms is a dead giveaway (if you've run into this issue before).
If you've got a Linux box to hand, try sticking the server on there
and see what happens.

The key to what is going on is here:
http://www.port80software.com/200ok/archive/2005/01/31/317.aspx

The easy solutions are to either change:

>   def send( self, rdf ):
>   self.s.send( rdf )
>   self.s.send( "\n" )

to

def send( self, rdf ):
self.s.send( rdf+"\n" )

or to self.s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 0) after
connecting self.s .

What's going on is something like this:

1. Client sends rdf

2. Server receives rdf. But it hasn't received the \n yet, so it doesn't
have a response to send yet. And because of delayed ACK, it won't send
the ACK without the DATA packet containing that response.

3. Meanwhile, the client tries to send \n, but because Nagle is on, it
would make an underful packet, and it hasn't received the ACK from the
last packet yet, so it holds on to the \n.

4. Eventually (after 200ms on Windows and, I believe, BSDs, hence OSX,
but much less on Linux) the Server gives up waiting for a DATA packet
and sends the ACK anyway.

5. Client gets this ACK and sends the \n.

6. At which point the Server has a complete line it can process, and
sends its reponse in a DATA with the ACK to the \n "piggybacked".

So the solutions are either send rdf+"\n" as a single packet in 1, and
the exchange will skip straight to 6, avoiding the 200ms delay; or
disable Nagle so 3 doesn't hold and the Client sends the \n
immediately instead of waiting for the ACK in 5.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: newbie question: convert a list to one string

2005-08-25 Thread Sion Arrowsmith
 <[EMAIL PROTECTED]> wrote:
>I'm searching for the fastest way to convert a list to one big string.

The join() method of strings. The string instance in question being
the separator you want, so:

" ".join(test)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: RE Despair - help required

2005-08-25 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>, Yoav  <[EMAIL PROTECTED]> wrote:
>Fredrik Lundh wrote:
>> "Yoav" wrote:
>>>I am trying the following:
>>>
>>>re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files")
>> instead of struggling with weird REs, why not use Python's standard
>> filename manipulation library instead?
>> 
>> http://docs.python.org/lib/module-os.path.html
>Don't think it will do much good. I need to get them from  a file and 
>extract the last folder in the path. For example:
>if I get "c:\dos\util"
>I want to extract the string "\util"

Did you actually look at the docs Fredrik pointed you at? Did you,
in particular, notice os.path.basename, which does (almost) exactly
what you want?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is my thread safe from premature garbage collection?

2005-09-01 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>, Benjamin Niemann  <[EMAIL PROTECTED]> wrote:
>[EMAIL PROTECTED] wrote:
>> However, in my current project I'm creating a bunch of threads which
>> are supposed to run until they've completed their run() method, and I'm
>> worried that if I do not keep references to these thread objects
>> around, the GC might happily delete them (and thereby kill my thread
>> routines maybe?) while they're not done yet. Is this fear justified?
>The threading module does already take care of keeping references to all
>running threads,

The implementation of threading.enumerate() would be entertaining if it
didn't.

Quite apart from which, I presume the OP's run() method looks something
like:
class MyThread(threading.Thread):
def run(self):
...
So what is self if not a reference to the Thread object which is kept
around until run() has completed?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Creating custom event in WxPython

2005-09-01 Thread Sion Arrowsmith
 <[EMAIL PROTECTED]> wrote:
>Now when my socket thread detects an incoming message, I need my main
>thread to interpret the message and react to it by updating the GUI.
>IMO the best way to achieve this is by having my socket thread send a
>custom event to my application's event loop for the main thread to
>process. However, I'm at a total loss as far as creating custom events
>is concerned. The WxWindows documentation isn't very helpful on this
>either.
>I think I need to derive my own event class from the wxEvent class, but
>I have no idea HOW (i.e. what methods do I need to override, etc.) Can
>anyone help me?

(a) Consider whether you can do what you want with CallAfter().

(b) If you do have to go with a custom event, the idiom I've got here
is:

EVT_CUSTOM_WITH_DATA_ID = wxNewId()

def EVT_CUSTOM_WITH_DATA(win, func):
win.Connect(-1, -1, EVT_CUSTOM_WITH_DATA_ID, func)

class CustomWithDataEvent(wxPyEvent):
def __init__(self, data=None):
wxPyEvent.__init__(self)
self.data = data
self.SetEventType(EVT_CUSTOM_WITH_DATA_ID)

def Clone(self):
return CustomWithDataEvent(self.data)

but do note that this is for wxPython2.4 -- amongst other differences,
EVT_CUSTOM_WITH_DATA() should be unnecessary in 2.6 (used Bind()
instead).

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: round() wrong in Python 2.4?

2005-09-13 Thread Sion Arrowsmith
Nils Grimsmo <[EMAIL PROTECTED]> wrote:
>Why did round() change in Python 2.4?
>
>$ python2.3
>Python 2.3.5 (#2, Jun 19 2005, 13:28:00)
>[GCC 3.3.6 (Debian 1:3.3.6-6)] on linux2
 round(0.0225, 3)
>0.023
 "%.3f" % round(0.0225, 3)
>'0.023'

>$ python2.4
>Python 2.4.1 (#2, Jul 12 2005, 09:22:25)
>[GCC 4.0.1 (Debian 4.0.1-1)] on linux2
 round(0.0225, 3)
>0.021999
 "%.3f" % round(0.0225, 3)
>'0.022'

>
>(Is this due to the different GCC used?)

That would look like a good guess to me:

$ python
Python 2.4.1 (#2, May  5 2005, 11:32:06)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.0225, 3)
0.023
>>> "%.3f" % round(0.0225, 3)
'0.023'
>>>

Is that python2.4 of yours from the python2.4 package or one
you compiled up yourself?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: turing machine in an LC

2005-02-10 Thread Sion Arrowsmith
Greg Ewing  <[EMAIL PROTECTED]> wrote:
>As a fellow named Church once pointed out, lambdas are really
>*all* you need in a language...

... where as others argue that it is impractical not to have
some form of runtime data storage, thereby giving rise to the
separation of Church and state.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Install MySQLdb on Mac OS X (10.3)

2005-02-11 Thread Sion Arrowsmith
Skip Montanaro  <[EMAIL PROTECTED]> wrote:
>Try a more recent version of mysql-python.  I think 1.1.7 is the latest.

1.2.0 -- it appears to be moving extremely rapidly (especially given
how long it was at 0.9.2 -- although "Waiting for MySQL 4.1 to become
stable" would be a good explanation for that).

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Stable GUI + wxPython memory leak

2005-02-17 Thread Sion Arrowsmith
Peter Hansen  <[EMAIL PROTECTED]> wrote:
>Viktor wrote:
>> I just noticed that wxPython is leaking memory?! Playing with
>> wxPython-demo, I started with 19MB used, and ended whith almost 150MB
>> used?!
>> It's wxPython 2.5.3.1 running on Python 2.4.
>On which platform?
>
>And how are you measuring this apparent memory consumption?

And what are you doing to the demo to provoke it? (I've had a
wxPython application significantly increase its memory footprint
on Windows just from opening a file dialog.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why doesn't join() call str() on its arguments?

2005-02-17 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>,

!

Nick Vargish  <[EMAIL PROTECTED]> wrote:
> If a non-string-type has managed to
>get into my list-of-strings, then something has gone wrong and I would
>like to know about this potential problem.

Thinking about where I use join(), I agree. If there's something
other than a string in my list, either I know about it and can
explicitly convert it ("Explicit is better than implicit.") or
it's an error, and "Errors should never pass silently."

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with the sort() function

2005-02-22 Thread Sion Arrowsmith
clementine <[EMAIL PROTECTED]> wrote:
>Thanx Nick...I forgot to mention im using python 2.2 and along with a host
>of other things it doesnt seem to have the enumarate built in function
>:(:(:(...is it possible to replace it by something else? I dont think
>simulating it will be feasible

Here's one I prepared earlier:

if sys.version_info < (2,3):
def enumerate(l):
return zip(range(len(l)), l)

which will suck somewhat on large lists compared to being able to
do it with iterators.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: injecting "set" into 2.3's builtins?

2005-03-11 Thread Sion Arrowsmith
Stephen Thorne  <[EMAIL PROTECTED]> wrote:
>I have:
>try:
>set
>except NameError:
>from sets import Set as set
>
>in my code in a few places.

Is there any reason to prefer this over the idiom I have:
if sys.version_info < (2, 4):
from sets import Set as set

? (I've also used the same kind of trick to provide enumerate when a
script is being run with pre-2.3.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: wxPython bug

2004-12-10 Thread Sion Arrowsmith
Jive <[EMAIL PROTECTED]> wrote:
>In wxPython 2.5, run the demo, samples/wxProject/wxProject.py
> [ ... ]
>TypeError: TreeCtrl_GetFirstChild() takes exactly 2 arguments (3 given)

GetFirstChild() changed from taking 2 arguments in wxPython 2.4 to
(the more sensible) 1 in wxPython 2.5. Clearly wxProject hasn't
been thoroughly 2.5ified. (Looking at it, it's not using the wx
namespace, so I'd say it's not been 2.5ified at all.)

>What to do?

(a) Fix your copy of wxProject.py (someone else has already
pointed this out).

(b) File a bug report 
(http://sourceforge.net/tracker/?group_id=9863&atid=109863)
with a "wxPython specific" category.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why no list heritable type?

2004-12-17 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>, Mike Meyer  <[EMAIL PROTECTED]> wrote:
>And before Python 2.2 there was the UserList class in the standard
>library. Which is still there in 2.4. Shouldn't it be depreciated by
>this point?

Apart from compatibility issues as mentioned in the UserList
documentation, deriving from it will give you a classic class,
whereas deriving from list will give you a new style class.
There may be circumstances under which this is important
(exercise left to more twisted minds than mine). Same applies
to dict/UserDict.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: curious problem with large numbers

2005-04-08 Thread Sion Arrowsmith
Michael Spencer  <[EMAIL PROTECTED]> wrote:
>Terry Reedy wrote:
>> "Chris Fonnesbeck" <[EMAIL PROTECTED]> wrote
>>>However, on Windows (have tried on Mac, Linux) I get the following 
>>>behaviour:
>>inf = 1e1
>>inf
>>>1.0
>> On my Windows machine with 2.2.1, I get exactly what you expected:
>1e1
>> 1.#INF
>On my Windows machine, both 2.3.3 and 2.4 give the same (expected) result:
>  >>> 1e1
>  1.#INF

Likewise for me with 2.3.2 (W2K on a PIII). I'm not going to do a
cut and paste because it's exactly the same as everyone else's,
and anyway I'm typing this at my Linux machine.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Python does *SLICING* the way it does??

2005-04-20 Thread Sion Arrowsmith
Raymond Hettinger <[EMAIL PROTECTED]> wrote:
><[EMAIL PROTECTED]>
>> Many people I know ask why Python does slicing the way it does.
>Python's way has some useful properties:
>
>* s == s[:i] + s[i:]
>
>* len(s[i:j]) == j-i # if s is long enough

The latter being particularly helpful when i = 0 -- the first n
elements are s[:n] . (Similarly elegantly, although of no
practical significance, s == s[0:len(s)] .)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what is the best way to determine system OS?

2005-04-25 Thread Sion Arrowsmith
Philippe C. Martin <[EMAIL PROTECTED]> wrote:
>How about popen of 'uname -r' ?

os.uname()[2] is probably a better way (ie it doesn't spawning
another process) of getting this information. I don't think it
will help the original poster though (depending on *what* it is
about FC3 which is breaking things) since it only provides the
kernel version.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a level one list

2006-01-12 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>,
Paul Rubin   wrote:
>Robin Becker <[EMAIL PROTECTED]> writes:
>> >reduce(operator.add,a)
>> ...
>That's what I hoped "sum" would do, but instead it barfs with a type
>error.  So much for duck typing.

sum(...)
sum(sequence, start=0) -> value

If you're using sum() as a 1-level flatten you need to give it
start=[].

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Python linker

2006-07-18 Thread Sion Arrowsmith
 <[EMAIL PROTECTED]> wrote:
>I love python - I use it as a utility language to complement my C#
>programming every day. However, the reason I do not use it as my
>primary language is - surprise, surprise - not its lack of static type
>checking, but the size of standalone executes  (which embed the python
>runtime).

Er, what? How are you generating your standalone executables? What
size is "acceptable"? python24.dll is only 1.8M -- surely on any
non-embedded platform these days 1.8M isn't worth bothering about.
And since you mention wx (all of another 4.8M) I'd guess we're
talking about desktop applications. Who's going to notice if your
executable is a couple of M slimmer?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: CSV with comments

2006-07-19 Thread Sion Arrowsmith
Daniel Dittmar  <[EMAIL PROTECTED]> wrote:
> if line [:1] == '#':

What's wrong with line[0] == '#' ? (For one thing, it's fractionally
faster than [:1].)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: [Newbie] List from a generator function

2006-07-24 Thread Sion Arrowsmith
Paul Rubin   wrote:
> print list(islice(starmap(random.choice, repeat((possible_notes,))), length))

Why the use of starmap() rather than
imap(random.choice, repeat(possible_notes))
?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Possible error in 'dive into Python' book, help!

2006-07-31 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>,
Ben Edwards (lists) <[EMAIL PROTECTED]> wrote:
>I have been going through Dive into Python which up to now has been
>excellent.  I am now working through Chapter 9, XML Processing.  I am 9
>pages in (p182) in the 'Parsing XML section.  The following code is
>supposed to return the whole XML document (I have put ti at the end of
>this email):
>
>from xml.dom import minidom
> 
>xmldoc =
>minidom.parse('/home/ben/diveintopython-5.4/py/kgp/binary.xml') 
>grammerNode = xmldoc.firstChild
> 
>print grammerNode.toxml()
>
>But it only returns:
>
>  PUBLIC '-//diveintopython.org//DTD Kant Generator Pro v1.0//EN'
>  'kgp.dtd'>
>
>The next line is then 
>
>grammerNode.childNodes
>
>And it returns an empty tuples;(
>
>Kind of stuck here as I don't really want to continue.  Has anyone any
>idea what is going on?

OK, your xmldoc has two child nodes. The first is the 
you're getting above. Which of course doesn't have any children, so
grammarNode.childNodes is naturally empty. The  element is
the *second* child of the xmldoc, so of course isn't xmldoc.firstChild.
Probably what you want is:

grammarNode = xmldoc.documentElement

Dive Into Python's assertion that "A Document always has only one
child node, the root element of the XML document" looks a little
off from where I'm standing:

>>> print xmldoc.toxml()




  0
  1


  


>>> xmldoc.childNodes
[, ]
>>>

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: What is the best way to print the usage string ?

2006-08-04 Thread Sion Arrowsmith
There's been a good lot of response to the problem originally stated,
but no-one's pointed out that:

>print reduce(lambda x, y: x + ':' + y, sys.argv[1:])

is a confusing (and slow) way of writing:

print ':'.join(sys.argv[1:])

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to reverse tuples in a list?

2006-08-09 Thread Sion Arrowsmith
Robert Kern  <[EMAIL PROTECTED]> wrote:
>Python 2.4+:
>
>   y = [tuple(reversed(t)) for t in y]
>

Python 2.3:

   y = [ t[::-1] for t in y ]

Obviously works in 2.4 as well, where I make it faster than using
tuple(reversed(t)). Which isn't surprising, as it's not constructing
the intermediate list.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Class data being zapped by method

2006-08-09 Thread Sion Arrowsmith
Bruno Desthuilliers  <[EMAIL PROTECTED]> wrote:
>fpath = os.path.join('datafiles', filename + ".tabdata")

fpath = os.path.join('datafiles', filename + os.path.extsep + "tabdata")

8-) I'm a bit bemused by extsep -- it didn't appear until 2.2, by
which time there can't have been many people with an OS where it was
anything other than '.'.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Two Classes In Two Files

2006-08-10 Thread Sion Arrowsmith
Pedro Werneck  <[EMAIL PROTECTED]> wrote:
>"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>> Wasn't so much a worry, just trying to figure out how to think the
>> python way.
>Seems like you're thinking the Java way... if you don't want to do it,
>put both classes in the same file.

OP: think of a .py file as being more akin to Java package than a .java
file. Don't worry about the resulting files getting too large -- Python
is a lot more compact than Java.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: datetime to timestamp

2006-08-11 Thread Sion Arrowsmith
John Machin  <[EMAIL PROTECTED]> wrote:
>On 11/08/2006 11:35 PM, John Machin wrote:
>> On 11/08/2006 11:10 PM, Simen Haugen wrote:
>>> How can I convert a python datetime to a timestamp? It's easy to convert
>>> a timestamp to datetime (datetime.datetime.fromtimestamp(), but the
>>> other way around...?)
>> Is the timetuple() method what you want?
> [ ... ]
>Aaaarrrggghhh no it's not what you want -- [ ... ]

It is if you only want it to the second. It just needs a time.mktime():

>>> n = time.time()
>>> n
1155307613.4550381
>>> d = datetime.datetime.fromtimestamp(n)
>>> time.mktime(d.timetuple())
1155307613.0

(timetuple() is responsible for the loss of the fractional part.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: dictionary update

2006-08-15 Thread Sion Arrowsmith
Chris  <[EMAIL PROTECTED]> wrote:
>I have a instance attribute self.xds and a local variable xds in the 
>instance . Both are dictionaries. I understand that I can update 
>self.xds with xds as follows: self.xds.update(xds)
>
>However I have many instance attributes, self.i, and local variables i.
>I'd like to be able to do this:
>
>for i in list_of_i's:
> self.i.update(i)
>
>How can this be done (I'm assuming it can be).

I think what you want is:

for i in list_of_is:
getattr(self, i).update(locals()[i])

assuming list_of_is is a list of names not a list of the local
dicts (ie ["xds", ...] not [xds, ...]).

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Printing n elements per line in a list

2006-08-16 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>, Dan Sommers  <[EMAIL PROTECTED]> wrote:
>Perhaps not the prettiest, but I can't think of anything simpler to read
>six months from now:
>
>counter = 0
>for an_element in the_list:
>print an_element,
>counter = counter + 1
>if counter == n:
>print
>counter = 0

for counter, an_element in enumerate(the_list):
print an_element,
if not (counter+1) % n:
print
# Plus the final print fixed in an earlier follow-up:
if len(the_list) % n:
print

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: what is the keyword "is" for?

2006-08-16 Thread Sion Arrowsmith
Simon Forman <[EMAIL PROTECTED]> wrote:
>Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
>[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
>
>|>> a = b = 1e1000 / 1e1000
>|>> a is b
>True
>|>> a == b
>False

I agree with you:

$ python
Python 2.4.1 (#2, May  5 2005, 11:32:06)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = b = 1e1000 / 1e1000
>>> a is b
True
>>> a == b
False

Or maybe I don't:

$ python2.3
Python 2.3.5 (#2, Sep  4 2005, 22:01:42)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = b = 1e1000 / 1e1000
>>> a is b
True
>>> a == b
True

See:

http://mail.python.org/pipermail/python-bugs-list/2004-February/022133.html

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Problem of function calls from map()

2006-08-22 Thread Sion Arrowsmith
Dasn  <[EMAIL PROTECTED]> wrote:
># size of 'dict.txt' is about 3.6M, 154563 lines
>f = open('dict.txt', 'r')
>print "Reading lines..."
>lines = f.readlines()
>print "Done."
 [ ... ]
>def sp1(lines):
>   """> sp1() -- List-comprehension"""
>   return [s.split('\t') for s in lines]
 [ ... ]
>def sp4(lines):
>   """> sp4() -- Not correct, but very fast"""
>   return map(str.split, lines)
>
>for num in xrange(5):
>   fname = 'sp%(num)s' % locals()
>   print eval(fname).__doc__
>   profile.run(fname+'(lines)')

>> sp1() -- List-comprehension
> 154567 function calls in 12.240 CPU seconds
 [ ... ]
>> sp4() -- Not correct, but very fast
> 5 function calls in 3.090 CPU seconds
 [ ... ]
>The problem is the default behavior of str.split should be more complex
>than str.split('\t'). If we could use the str.split('\t') in map(), the
>result would be witty. What do u guys think?

I think there's something weird going on -- sp4 should be making
154563 calls to str.split. So no wonder it goes faster -- it's
not doing any work.

How does [s.split() for s in lines] compare to sp2's
[s.split('\t') for s in lines] ?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Problem of function calls from map()

2006-08-22 Thread Sion Arrowsmith
Fredrik Lundh <[EMAIL PROTECTED]> wrote:
>Sion Arrowsmith wrote:
>> I think there's something weird going on -- sp4 should be making
>> 154563 calls to str.split. So no wonder it goes faster -- it's not doing
>> any work.
>the problem is that he's using a Python-level profiler to benchmark things 
>written
>in C.
>
>(you cannot really use "profile" to *benchmark* things written in Python 
>either; the
>profiler tells you where a given program spends the time, not how fast it is 
>in com-
>parision with other programs)

Hmm. Playing around with timeit suggests that although split() *is*
faster than split("\t"), it's fractional, rather than the OP's four
times faster. Is the overhead of profile keeping track of calls in
Python getting in the way? (Not having used profile -- hence my
confusion.) And why can map() keep everything at the C level when
the list comprehension can't?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: key not found in dictionary

2006-08-23 Thread Sion Arrowsmith
Chaz Ginger  <[EMAIL PROTECTED]> wrote:
>KraftDiner wrote:
>> desc = self.numericDict[k][2]
>> KeyError: 589824   < This is the error that is being produced,
>As stated you can wrap the access in the try - except - else statement, 
>as in
>
>try:
>  foo['bar']
>except :
>  # Handle the error.

Bare "except" is generally a bad idea. Here, it could be letting
through whole truckloads of other errors. Suppose the OP typos:

try:
desc = self.numericDict[j][2]
except:
# handle missing key

but the except isn't handling a KeyError, it's got a NameError
(assuming j doesn't exist). Or what if self.numericDict[k] exists
but self.numericDict[k][2] gives a TypeError or IndexError? It
really needs to be:

except KeyError:

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Output from subprocess.Popen()

2006-10-16 Thread Sion Arrowsmith
Clodoaldo Pinto Neto <[EMAIL PROTECTED]> wrote:
>Fredrik Lundh wrote:
>> this works for me:
>>
>>  >>> f = subprocess.Popen("set | grep IFS", shell=True,
>> stdout=subprocess.PIPE)
>>  >>> f.stdout.readlines()
>> ["IFS=$' \\t\\n'\n"]
>>
>> what does the above return on your machine?
>
 f = subprocess.Popen("set | grep IFS", shell=True, stdout=subprocess.PIPE)
 f.stdout.readlines()
>["BASH_EXECUTION_STRING='set | grep IFS'\n", "IFS=' \t\n"]
>
>I'm on FC5

OK, there's something going on here:
$ set | grep IFS
IFS=$' \t\n'
$ python
Python 2.4.1 (#2, May  5 2005, 11:32:06)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> f = subprocess.Popen("set | grep IFS", shell=True, stdout=subprocess.PIPE)
>>> f.stdout.readlines()
["IFS=' \t\n"]
>>>

(Hmph, I don't have any other non-Windows boxes with >=2.4 to hand.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: implementation of "in" that returns the object.

2006-10-23 Thread Sion Arrowsmith
Jorge Vargas <[EMAIL PROTECTED]> wrote:
>I need to check if an object is in a list AND keep a reference to the
>object I have done it this way but is there a better one?
>
 def inplusplus(value,listObj):
>... for i in listObj:
>... if i is value:
>... return value
>... return False
>...

try:
obj = listObj[listObj.index(value)]
# do something with obj
except ValueError:
# do whatever you're going to do if inplusplus returns False

Assuming you meant "if i == value" (as others have pointed out).

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Sorted and reversed on huge dict ?

2006-11-06 Thread Sion Arrowsmith
 <[EMAIL PROTECTED]> wrote:
>so i just have tried, even if i think it will not go to the end => i
>was wrong : it is around 1.400.000 entries by dict...
>
>but maybe if keys of dicts are not duplicated in memory it can be done
>(as all dicts will have the same keys, with different (count) values)?

I've had programs handling dicts with 1.7million items and it isn't
a great problem, providing you're careful not to duplicate data.
Creating a copy of keys() in a separate list, for example, will
inflate memory usage noticably.

>memory is 4Gb of ram, [ ... ]

Unless you're on a 64bit OS, that's irrelevant. You'll hit the 2G
per-process limit before you start putting a strain on real memory.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Really strange behavior

2006-11-06 Thread Sion Arrowsmith
IloChab  <[EMAIL PROTECTED]> wrote:
[this works]
>def main():
>app = QtGui.QApplication(sys.argv)
>qt4reactor.install(app)
>MainWindow = QtGui.QMainWindow()
>win = Window(MainWindow)
>MainWindow.show()
>from twisted.internet import reactor
>reactor.run()
[this doesn't]
>def creApp():
>app = QtGui.QApplication(sys.argv)
>qt4reactor.install(app)
>retrun app
>def creWin():
>MainWindow = QtGui.QMainWindow()
>win = Window(MainWindow)
>MainWindow.show()
>def main():
>app = creApp()
>creWin()
>from twisted.internet import reactor
>reactor.run()

I don't know if this is the problem or not (knowing neither Qt nor
Twisted), but creWin() creates a window (or two) then throws it
(them?) away on returning to main() (I assume you've chopped
off the bit where main() is actually called). So it's not too
surprising your window doesn't show: by the time you get to
running anything, you don't have a window object to show. (Unless
a Qt application object is a discoverable global and windows
inject a reference to themselves into it.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: httplib.InvalidURL: nonnumeric port: For characters in the proxypassword in URL

2006-11-13 Thread Sion Arrowsmith
Fredrik Lundh <[EMAIL PROTECTED]> wrote:
 urllib.quote
>
 urllib2.quote
>

>>> urllib.quote

>>> urllib2.quote
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: 'module' object has no attribute 'quote'
>>> sys.version
'2.4.1 (#2, May  5 2005, 11:32:06) \n[GCC 3.3.5 (Debian 1:3.3.5-12)]'

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: ini files and plugins

2006-11-16 Thread Sion Arrowsmith
tool69  <[EMAIL PROTECTED]> wrote:
>I've made a basic LaTeX file editor in wxPython, but now I wanted to add 
>it some features :

>1 -  create a sort of ini file where I can put the user configuration 
>that will load itself on the application startup ;

wx.Config? (If it absolutely has to be a .ini file on Windows,
not the registry, you can force that if you want.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

[OT] whitespace (was Re: stupid perl question)

2006-05-31 Thread Sion Arrowsmith
John Machin  <[EMAIL PROTECTED]> wrote:
> Whitespace is a silly term, anyway (IMHO); is there such a 
>thing as a space that is not white?

Yes, if you go back to the term's origins in printshops. A solid
block of ink is space, not print, but it's not whitespace. We've
kept the word alive, even if the meaning's drifted (see also "tab").
And if we didn't say "whitespace", how would we distinguish between
"space characters" meaning " " and "whitespace characters"
meaning " \t\r\n\v"? (OK, has anyone met a "\v" in the wild?)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: DB-API: how can I find the column names in a cursor?

2006-06-01 Thread Sion Arrowsmith
A.M <[EMAIL PROTECTED]> wrote:
>The other problem is accessing data in each row by column name. In Ruby I
>can say:
>
>Print row["ColName"]
>
>In Python; however, I must access to row contents by integer index, like
>PRINT ROW[0], which reduces my program's readability.
>
>Can I access to row's contents by column name?

columns = dict((name, col) for col, name in enumerate(cursor.description))
print row[columns["ColName"]]

And please don't top-post.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: integer to binary...

2006-06-02 Thread Sion Arrowsmith
Tim Chase  <[EMAIL PROTECTED]> wrote:
>bitCount = len([c for c in "01001010101" if c=="1"])

bitCount = "01001010101".count("1")

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: New to Python: Do we have the concept of Hash in Python?

2006-06-02 Thread Sion Arrowsmith
gregarican <[EMAIL PROTECTED]> wrote:
>I came from using Ruby about a year or so [ ... ]

That's an interesting way round. Why did you consider Python if
you already knew Ruby, and which is now your preferred language?
(I've no interest in learning Ruby, but from what I've seen of it
I similarly can't imagine what would motivate me to learn Python.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Using print instead of file.write(str)

2006-06-02 Thread Sion Arrowsmith
A.M <[EMAIL PROTECTED]> wrote:
>I found print much more flexible that write method.

"more flexible"? More convenient, yes. More powerful, maybe. But I
don't see more flexible. Everything print can to stdout.write() can
do. The reverse isn't true. eg (this appears to be a FAQ on this
group, although I can't find it in the FAQ):

for x in range(10):
sys.stdout.write(str(x))

to print:

0123456789

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Bug in list comprehensions?

2006-06-07 Thread Sion Arrowsmith
Fredrik Lundh  <[EMAIL PROTECTED]> wrote:
>Iain King wrote:
>> I'm guessing I'm the one confused here... but I'm confused!  What's 
>> going on?
>the clauses nest from left to right, not from right to left, so "[x for 
>x in y for y in beta]" is equivalent to
>
>  out = []
>  for x in y:
>  for y in beta:
>  out.append(x)

And a list comprehension doesn't get a namespace to itself (cf.
generator comprehensions) so "leaks" its variables. Exactly as
above. So the y being iterated over in "for x in y" is the y
from the previous inner iteration ("for y in beta").

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Instead of saving text files i need as html

2006-06-09 Thread Sion Arrowsmith
Tim Chase  <[EMAIL PROTECTED]> wrote:
>> [ ... ]
>> urllib.urlretrieve(lines.strip('/n'), 'c:\\temp\\' \
>> + outfilename.strip('\n')[7:] + '.html')
> [ ... ]  I'm not sure what the odd 
>slicing is for, but I'll presume the OP knows what they're doing.

It's taking the "http://"; off the front of the URL.

>>> len("http://";)
7

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to extract 2 integers from a string in python?

2006-06-09 Thread Sion Arrowsmith
Stephen Prinster  <[EMAIL PROTECTED]> wrote:
 a, b, c, d = 'Total size: 173233 (371857)'.split()
 first_int, second_int = int(c), int(d[1:-1])

int(d[1:-1]) can be replaced by d.strip("()"), which may or
may not be clearer in intent.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Searching and manipulating lists of tuples

2006-06-12 Thread Sion Arrowsmith
Steve Holden  <[EMAIL PROTECTED]> wrote:
>def algorith(d, s):
> if s in d:
> d[s] += 1
> else:
> d[s] = 1

def algorith(d, s):
d[s] = d.get(s, 0) + 1

And the OP should note that converting between dict d and list of
pairs L is simply a matter of L = d.items() and d = dict(L) (assuming
some other part of the program wants that list representation).

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: determining file type

2006-06-14 Thread Sion Arrowsmith
Ritesh Raj Sarraf <[EMAIL PROTECTED]> wrote:
>Also,
>f =3D file ('some_file.jpg')
>
>throws an error.
>"str object is not callable"

You know all the times people say in this group "Don't use list
or str or file[HINT] or anything else that shadows a built in as
a variable name"? Now you know why.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Having problems with strings in HTML

2006-06-27 Thread Sion Arrowsmith
Lawrence D'Oliveiro  <[EMAIL PROTECTED]> wrote:
> Kiana Toufighi <[EMAIL PROTECTED]> wrote:
>
>>print '''
>>>href="http://bbc.botany.utoronto.ca/ntools/cgi-bin/ntools_treeview_word.cgi?inp
>>ut=&max=2&values_off=no&remove_bracket=no&show_line_nr=yes&show_link_out=yes
>> [ ... ] ">GraphicalOutput
>> ''' [ ... ]
>
>By the way, you _do_ realize that your "&" characters should be escaped 
>as "&", don't you?

No they shouldn't. They part of the url, which is (IIRC) a CDATA
attribute of the A element, not PCDATA.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Having problems with strings in HTML

2006-06-27 Thread Sion Arrowsmith
Richard Brodie <[EMAIL PROTECTED]> wrote:
>"Sion Arrowsmith" <[EMAIL PROTECTED]> wrote in message 
>news:[EMAIL PROTECTED]
> [ ... ]
>>>By the way, you _do_ realize that your "&" characters should be escaped
>>>as "&", don't you?
>> No they shouldn't. They part of the url, which is (IIRC) a CDATA
>> attribute of the A element, not PCDATA.
>It is CDATA but ampersands still need to be escaped. 

I'll take back that "shouldn't" as per Fredrik's post. Maybe they
*should* be escaped, but they don't *need* to be. I've never, and
never seen, anyone doing that in a decade of writing cgi; a straw poll
of peers suggests that the majority would write the former; and I've
never encountred a browser getting tripped up by it. I suppose you
might need it if you've got parameters called quot or nbsp, but it's
hardly the most broken way of writing HTML.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Immutability

2006-06-28 Thread Sion Arrowsmith
Nick Maclaren <[EMAIL PROTECTED]> wrote:
>Georg Brandl <[EMAIL PROTECTED]> writes:
>|> [ attributions lost ]
>|> > Thanks very much.  And, what's more, I have even found its documentation!
>|> > Whatsnew2.2.  The 2.4.2 reference is, er, unhelpful.
>|> Is it?
>|> http://docs.python.org/lib/built-in-funcs.html
>|> documents "property" quite well.
>Sigh.  No.  It's terrible.  What it documents is the use of the property
>FUNCTION.  It does not document what properties ARE, and how they interact
>with the rest of the language.  Until you know that, it is so ambiguous
>as to be almost totally useless - and it is THAT information that needs to
>be in the reference manual, but is only in whatsnew2.2.

Actually, there's an almost throw-away mention in
http://docs.python.org/ref/descriptor-invocation.html
which gives you what you need (although not, I have to say, in an
easily digestible form).

What I've not seen documented anywhere is the:
@property
def fset(self, value):
...
idiom. It's not obvious from the documentation of the property
function that it can be used as a decorator like this. (cf.
classmethod and staticmethod.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Immutability

2006-06-28 Thread Sion Arrowsmith
Fredrik Lundh <[EMAIL PROTECTED]> wrote:
>Sion Arrowsmith wrote:
>> What I've not seen documented anywhere is the:
>>@property
>>def fset(self, value):
>>...
>> idiom. It's not obvious from the documentation of the property
>> function that it can be used as a decorator like this.
>probably because it cannot be used in that way: the "property" function
>takes the *getter* as its first argument, so you can only use this for read-
>only properties...

Ahem. Yes. What I obviously meant to write was:

What I've not seen documented anywhere is the:
   @property
   def fget(self):
   ...
idiom. [ ... ]

(As correctly written by someone else further upthread.) It is possible
to find it described by Googling, but the Cookbook (and python.org) hits
provide much more complicated general-purpose (ie setters as well)
property decorators.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: eval to dict problems NEWB going crazy !

2006-07-10 Thread Sion Arrowsmith
Fredrik Lundh  <[EMAIL PROTECTED]> wrote:
>Ant wrote:
>> It seems that there must be a way to use eval safely, as there are
>> plenty of apps that embed python as a scripting language - and what's
>> the point of an eval function if impossible to use safely, and you have
>> to write your own Python parser!!
>embedding python != accepting scripts from anywhere.

And also using eval (or exec or execfile) != accepting scripts from
anywhere. You've got to consider where the data can have come from
and what (broad) context it's being eval()'d in. Last time I did
something like this was with execfile for advanced configuration of
a server, and if a hostile party were in a position to inject
malicious code into *that* then subversion of our program would be
the least of anyone's concern.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: function that modifies a string

2006-07-10 Thread Sion Arrowsmith
Steven D'Aprano  <[EMAIL PROTECTED]> wrote:
>Of course, another right way would be to have mutable strings in Python.

What significant advantage would mutable strings have over StringIO
and wrapping list manipulation in list(s) and ''.join(l). Other than
that pleasing symmetry with sets/frozensets etc.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: split a line, respecting double quotes

2006-07-10 Thread Sion Arrowsmith
Jim <[EMAIL PROTECTED]> wrote:
>Is there some easy way to split a line, keeping together double-quoted
>strings?
>
>I'm thinking of
>  'a b c "d e"'  --> ['a','b','c','d e']
>.  I'd also like
>  'a b c "d \" e"'  --> ['a','b','c','d " e']
>which omits any s.split('"')-based construct that I could come up with.

>>> csv.reader(StringIO.StringIO('a b c "d e"'), delimiter=' ').next()
['a', 'b', 'c', 'd e']

It can't quite do the second one, but:
>>> csv.reader(StringIO.StringIO('a b c "d "" e"'), delimiter=' ').next()
['a', 'b', 'c', 'd " e']
isn't far off.

On the other hand, it's kind of a stupid solution. I'd really go with
shlex as someone suggested up thread.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is there an elegant way to dir() module from inside?

2006-08-24 Thread Sion Arrowsmith
volcano <[EMAIL PROTECTED]> wrote:
>I am looking for a way to discover which classes a module contains from
>"inside". I am building a testing class that should, when instatntiated
>within any module, locate certain classes within the containing module.

globals().keys()

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: efficient text file search -solution

2006-09-12 Thread Sion Arrowsmith
noro <[EMAIL PROTECTED]> wrote:
>OK, am not sure why, but
>
>fList=file('somefile').read()
>if fList.find('string') != -1:
>   print 'FOUND'
>
>works much much faster.
>
>it is strange since i thought 'for line in file('somefile')' is
>optemized and read pages to the memory,

Step back and think about what each is doing at a high level of
description: file.read reads the contents of the file into memory
in one go, end of story. file.[x]readlines reads (some or all of)
the contents of the file into memeory, does a linear searches on it
for end of line characters, and copies out the line(s) into some
new bits of memory. Line-by-line processing has a *lot* more work
to do (unless you're read()ing a really big file which is going to
make heavy demands on memory allocation) and it should be no
surprise that it's slower.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

"Directory this source file is in (and a sibling)"

2006-09-22 Thread Sion Arrowsmith
I have a module which needs to know what directory it's in, and to
refer to files in a sibling directory, something like App/src/foo.py
wants to read App/data/conf.xml . But I have no idea in what context
foo.py is going to be run -- it could be being run as a script, it
could be being imported as a module by another script from anywhere in
the directory structure, it's even possible someone will have called
execfile on it. The following works for everything I've tried:

thisdir = os.path.dirname(os.path.normpath(__file__))
siblingdir = os.path.normpath(os.path.join(testdir, os.path.pardir, "sibling"))

However, a colleague expressed disgust at this code, but not really
being a Python programmer had no better suggestions. Is there a neater
way of getting what I want?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: A critique of cgi.escape

2006-09-26 Thread Sion Arrowsmith
Jon Ribbens  <[EMAIL PROTECTED]> wrote:
>In article <[EMAIL PROTECTED]>, Duncan Booth wrote:
>> I guess you've never seen anyone write tests which retrieve some generated 
>> html and compare it against the expected value. If the page contains any 
>> unescaped quotes then this change would break it.
>You're right - I've never seen anyone do such a thing. It sounds like
>a highly dubious and very fragile sort of test to me, of very limited
>use.

So what sort of test would you use, that doesn't involve comparing
actual output against expected output?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: where are isinstance types documented?

2006-09-26 Thread Sion Arrowsmith
Fredrik Lundh  <[EMAIL PROTECTED]> wrote:
>based on http://docs.python.org/ref/types.html, here's a list of the 
>most commonly used core types:
> [ ... ]
>Sequences:
> str
> unicode
> tuple
> list

And set, presumably.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Top and Bottom Values [PEP: 326]

2006-09-27 Thread Sion Arrowsmith
Lawrence D'Oliveiro  <[EMAIL PROTECTED]> wrote:
>In message <[EMAIL PROTECTED]>, Antoon Pardon wrote:
>> I need this Top value in
>> a context where it can be used as a start or stop value
>> in a slice.
>But the only valid values allowed for indices are 0

>>> range(5)[:-1]
[0, 1, 2, 3]

>  up to the length of the
>array inclusive. Larger integers are not allowed, [ ... ]

>>> range(5)[:sys.maxint]
[0, 1, 2, 3, 4]

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Finding the file referred to in a Python traceback

2006-09-28 Thread Sion Arrowsmith
 <[EMAIL PROTECTED]> wrote:
>In this traceback, the path to 3 different SQL Alchemy source files is
>a relative directory. However, no such directory is below my current
>working directory.
>
>Traceback (most recent call last):
>  File "", line 1, in ?
>  File "conf/__init__.py", line 34, in __init__
>self.table = tables(self.metadata)
>  File "conf/__init__.py", line 38, in __init__
>self.users = Table('Users', metadata, autoload=True)
>  File "build/bdist.darwin-8.7.1-i386/egg/sqlalchemy/schema.py", line
>138, in __call__
[etc]
 print sys.path
>['', '/sw/lib/python2.4/site-packages/Dabo-0.6.5s-py2.4.egg',
>'/sw/lib/python2.4/site-packages/SQLAlchemy-0.2.8dev_r1898-py2.4.egg',
[etc]

Paths of files contained in eggs aren't always helpful (as was pointed
out to me recently). If you unzip the SQLAlchemy egg (eggs are just
zip files) (to somewhere other than site-packages if you want to avoid
confusion) you'll probably find schema.py etc. in there.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Whither binary search?

2006-09-28 Thread Sion Arrowsmith
John Machin <[EMAIL PROTECTED]> wrote:
>Fredrik Lundh wrote:
>> well, people tend to use dictionaries when they need to look things up
>> quickly...
>... like those paper dictionaries with the words in alphabetical order
>:-)

... where you'll notice that the really big ones are divided up into
buckets (which just happen to be keyed on initial letter).

Truth is that humans are lot better than computers at general
insertion sort, and its lookup equivalent, whereas computers are much
better at calculating hashes. So we each use a dictionary
implementation that plays to our strengths.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: for: else: - any practical uses for the else clause?

2006-09-28 Thread Sion Arrowsmith
Ben Sizer <[EMAIL PROTECTED]> wrote:
>[EMAIL PROTECTED] wrote:
>> Yeah, I use it from time to time:
>>
>> for foo in bar:
>> if foo matches some condition:
>> print "sail to tahiti!"
>> break
>> else:
>> print "abandon ship!"
>
>As a C++ programmer (which I'm sure undermines my argument before
>you've even read it...), this feels 'backwards' to me. Although I am no
>purist, the 'else' typically implies failure of a previous explicit
>condition, yet in this case, it's executed by default, when the
>previous clause was successfully executed. It would seem more natural
>if the else clause was triggered by 'bar' being empty, [ ... ]

It does:

>>> for foo in []:
... print foo
... else:
... print 'else'
...
else

I think it's clearer to see by comparing while with if:

if False:
do nothing
else:
do something

while False:
do nothing
else:
do something

and getting to the for behaviour from while is trivial.

That said, I've not had much call for it and was kind of surprised to
find myself writing a genuine for ... else the other week. But it was
the obvious way to do the task at hand, and I was happy it was there.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: The del statement

2006-12-05 Thread Sion Arrowsmith
Marco Aschwanden <[EMAIL PROTECTED]> wrote:
[ ... ]
>> so what about
>>
>>  del x
>
>Ups. I never used it for an object. So far I only used it for deletion of  
>elements of a container. In that case del has two purposes:
>
>1. Deletes an item from a container (and of course destructs it) -->  
>list.remove(elem)
>2. Calls the destructor of an object --> list.destruct()
>
>One statement and two distinct purposes.

Leaving aside the discussion about whether Python has destructors or
not, you are mistaken on two counts:

1. del *is not responsible for an object being destroyed*. That is
done by the garbage collector.

2. del is only ever used to "delete an item from a container". It's
just that in the plain "del x" case, the container is implicitly the
current namespace. (In any case, it's not really "deleting an item
from a container", it's removing a reference to the item from the
container.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Fall of Roman Empire

2006-12-20 Thread Sion Arrowsmith
Ben Finney  <[EMAIL PROTECTED]> wrote:
>"John Machin" <[EMAIL PROTECTED]> writes:
>> Ben Finney wrote:
>> >  \  "...one of the main causes of the fall of the Roman Empire was |
>> >   `\that, lacking zero, they had no way to indicate successful |
>> > _o__)   termination of their C programs."  -- Robert Firth |
>> [ ... ] in many cases successful
>> termination of their C programs would have been unlikely.
>Yet historically proven: the 'imperium' process they were running
>terminated many centuries ago.
>
>Or did it fork and exec a different process?

Pretty much. Except they would argue that the child process
(Byzantium) never exec'd.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Backreferences in python ?

2006-01-24 Thread Sion Arrowsmith
Pankaj <[EMAIL PROTECTED]> wrote:
>search for :for ( i = 0; i < 10; i++)
>Replace with:  for( printf( "10" ), i =0; i < 10; i++)
>Where 10 is the line no.

>f = open( "./1.c", "r")
>fNew = open( "./1_new.c", "w")
>for l in f:
>print l
>lineno = lineno + 1
>strToFind = "for\((.*)\;(.*)"
[etc.]
>search for :for ( i = 0; i < 10; i++)
>Replace with:  for( printf( "10" ), i =0; i < 10; i++)

Ah, the dangers of thinking of all string manipulation as requiring
regexps, thanks to their ubiquity in Perl. Observe:
>search for :  for ( i = 0; i < 10; i++)
>Replace with:  for( printf( "10" ), i = 0; i < 10; i++)

All you need is:
strToFind = "for ("
strToReplace = 'for (printf( "+str(lineno)+'" ),'
# Note the use of '' to avoid the need to escape the "s
fNew.write(l.replace(strToFind, strToReplace)

(OK, maybe you do need the regexp if you've got any "for (;" loops,
or inconsitencies as to whether it's "for(" or "for (". But if a
simple string replace will do the job, use it.)

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: append to the end of a dictionary

2006-01-24 Thread Sion Arrowsmith
Tim Chase  <[EMAIL PROTECTED]> wrote:
>unless you have a way of doing an in-line sort, in which you 
>would be able to do something like
>
>   orderedDict = [(k,mydict[k]) for k in mydict.keys().sort()]
>
>Unfortunately, the version I've got here doesn't seem to support 
>a sort() method for the list returned by keys(). :(

I bet it does, but it doesn't do what you think it does. See
http://docs.python.org/lib/typesseq-mutable.html , in particular
note 7.

What you want is the sorted() function (introduced in 2.4 IIRC).

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: append to the end of a dictionary

2006-01-25 Thread Sion Arrowsmith
Magnus Lycka  <[EMAIL PROTECTED]> wrote:
>orderedListOfTuples = [(k,mydict[k]) for k in sorted(mydict.keys())]

Or:

orderedListOfTuples = [(k,v) for k,v in sorted(mydict.items())]

(k, mydict[k]) sets off warning bells for me that mydict.items()
should be in use, which are nearly as loud as the ones that
range(len(mylist)) triggers.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: append to the end of a dictionary

2006-01-25 Thread Sion Arrowsmith
I wrote:
>orderedListOfTuples = [(k,v) for k,v in sorted(mydict.items())]
>
>(k, mydict[k]) sets off warning bells for me that mydict.items()
>should be in use [ ... ]

But apparently [ x for x in mylist ] doesn't yet trigger such a
warning.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

  1   2   3   4   >