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

2005-10-16 Thread Andrew Koenig
"Peter Hansen" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dave Hansen wrote:
>> So lose the "if."
>>
>>R = C then A else B

I think that part of the argument for the "A if C else B" syntax is that 
"then" is not currently a reserved word.


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


Re: wxPython Licence vs GPL

2005-11-23 Thread Andrew Koenig
"John Perks and Sarah Mount" <[EMAIL PROTECTED]> wrote 
in message news:[EMAIL PROTECTED]
> we have some Python code we're planning to GPL. However, bits of it were

> (This assumes the wxPython Licence is compatible with the GPL -- if not,
> do we just cosmetically change any remaining lines, so none remain from
> the orignal?)

What makes you think that cosmetic changes in a copyrighted work affect the 
copyright?


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


Re: Which License Should I Use?

2005-11-27 Thread Andrew Koenig
"mojosam" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> I will be doing the bulk of the coding on my own time, because I need
> to be able to take these tools with me when I change employers.
> However, I'm sure that in the course of using these tools, I will need
> to spend time on the job debugging or tweaking them.  I do not want my
> current employer to have any claim on my code in any way.  Usually if
> you program on company time, that makes what you do a "work for hire".
> I can't contaminate my code like that.  Does that mean the GPL is the
> strongest defense in this situation?

It probably means that the only reliable defense is to get a written release 
from your employer.  If you want to be more confident about the situation, 
consult a lawyer.



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


Re: Which license should I use?

2005-11-27 Thread Andrew Koenig
""Björn Lindström"" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Mike Meyer <[EMAIL PROTECTED]> writes:

> If they have the rights to the code, they can sell it, under the GPL or
> any license of their choosing. In addition, if you GPL it, your employer
> will be able to sell it, just like anyone else.

If they have the rights to the code, you don't get to decide on the terms 
under which it will be distributed (if at all) -- they do.


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

Re: Which License Should I Use?

2005-11-27 Thread Andrew Koenig
"mojosam" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> I would have to talk to a lawyer to be sure, but right now, I think I
> can argue that anything I do on my own time belongs to me.  I'm
> technically a consultant right now (even though I'm spending 40
> hours/week with the one "client").  I can take on other clients, as
> long as they don't directly compete.  This means they're hiring my
> expertise.  If I bring my own tools, that's part of my expertise.  I do
> recall there was a clause in the contract that anything I did on their
> time belonged to them.  For my next client, I should definitely include
> a clause about rereleasing open source changes.

Yup.  If you're not an employee (that is, if you get a 1099 form rather than 
a W-2 form from your client), then any work you do belongs to you *except* 
for what you agree in writing belongs to them.  So if you write code that's 
not part of any deliverable, it's yours.

Of course, they might object to your using their facilities, or working on 
their time, on stuff that isn't part of a deliverable.  But that's a 
separate problem entirely.


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


Re: Which License Should I Use?

2005-11-27 Thread Andrew Koenig
"Robert Kern" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> You're in something of a gray area, but one that has seen a lot of
> litigation. Although you are "technically" a consultant, you are
> probably considered an employee with regards to the "work made for hire"
> doctrine. You should probably have a chat with a lawyer soon (I am not
> one! TINLA!).

I'm pretty sure that there was a change to the copyright laws a few years 
ago (perhaps as part of the DMCA), that made it clear that you own 
everything you produce, unless you're a W-2 employee or there is a written 
agreement to the contrary.



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


Re: Which License Should I Use?

2005-11-29 Thread Andrew Koenig
"Robert Kern" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Andrew Koenig wrote:

>> I'm pretty sure that there was a change to the copyright laws a few years
>> ago (perhaps as part of the DMCA), that made it clear that you own
>> everything you produce, unless you're a W-2 employee or there is a 
>> written
>> agreement to the contrary.
>
> The US Copyright Office does not agree with you.
>
>  http://www.copyright.gov/circs/circ09.pdf

Well, it comes pretty close to agreeing with me--the only issue is whether 
the definition of "employee" extends beyond the notion of "W-2 employee" and 
that issue is not really relevant to the original posting.

Here's the relevant quote:

If a work is created by an employee, part 1 of the statutory definition 
applies, and generally the work would be considered a work made for hire. 
Important: The term "employee" here is not really the same as the common 
understanding of the term; for copyright purposes, it means an employee 
under the general common law of agency. This is explained in further detail 
below. Please read about this at "Employer-Employee Relationship Under 
Agency Law." If a work is created by an independent contractor (that is, 
someone who is not an employee under the general common law of agency), then 
the work is a specially ordered or commissioned work, and part 2 of the 
statutory definition applies. Such a work can be a work made for hire only 
if both of the following conditions are met: (1) it comes within one of the 
nine categories of works listed in part 2 of the definition and (2) there is 
a written agreement between the parties specifying that the work is a work 
made for hire.



The reason I say that the distinction between W-2 employment and agency 
employment isn't really relevant is that in the kind of situation we're 
talking about, there is generally a written agreement specifying scope and 
nature of work.



So I'll amend my statement slightly:



If someone pays you to produce a specific piece of work, or you're an 
employee, any work you do for hire belongs to your employer.  Otherwise, 
it's yours unless there's a written agreement to the contrary.



I think that's a fair paraphrase of the paragraph I cited.  If you disagree, 
please say why.




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


Re: Which License Should I Use?

2005-11-29 Thread Andrew Koenig
"Mike Meyer" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Definitely not. The most recent change to the copyright laws made
> works of music recorded to fullfill a contract "work for hire" by
> default.

If there's a contract -- i.e., a written agreement, then why does it matter?


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


Re: counting items

2005-01-12 Thread Andrew Koenig
"It's me" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> What's the best way to count number of items in a list?
>
> For instance,
>
> a=[[1,2,4],4,5,[2,3]]
>
> I want to know how many items are there in a (answer should be 7 - I don't
> want it to be 4)

How about this?

def totallen(x):
if isinstance(x, (list, tuple, dict)):
return sum(map(totallen, x))
return 1


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


Re: Adjusting the 1024 byte stdin buffer limit

2005-01-17 Thread Andrew Koenig
"brucoder" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Are there runtime settings that can be used to adjust the default 1024
> byte stdin buffer limit or a buildtime setting in pyconfig.h?  I have a
> need to pump this up to permit input of a large data block via stdin.

What do you think you are being prevented from doing?


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


Re: a question

2005-01-19 Thread Andrew Koenig
"Steve Holden" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> The error you get is NOT a syntax error:
>
>  >>> cmd = '%s format %s \
>  ... over %d lines' % ('my', 'string', 2)
>  >>> cmd
> 'my format string over 2 lines'
>  >>>
>
> The interpreter is probably complaining because it needs six values to 
> fill out the format and you only provided four.

Also, I'm dubious about the idea of splitting a string literal across 
multiple lines, as it's impossible to indent such a literal nicely without 
putting stray spaces into its contents.  So instead of writing

'this is a\
 long string'

and not making it clear how many spaces you intend between 'a' and 'long', 
how about writing this instead?

('this is a '
 'long string')

in which the contents are not in doubt.  This code takes advantage of two 
properties of Python:

1) Multiple string literals with only whitespace between them are 
automatically concatenated;

2) Ending a line inside unbalanced parentheses implicitly makes the next 
line part of the same statement.


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


Re: [OT] Good C++ book for a Python programmer

2005-01-19 Thread Andrew Koenig
<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> The 4th edition of the well-known "C++ Primer", with Moo as a new 
> co-author, will soon be published. It is a
> more comprehensive and much longer book.

It is also organized more traditionally than "Accelerated C++."

"Accelerated C++" is mostly example-driven: It presents problems, shows how 
to solve them, and introduces language and library features as needed for 
particular parts of the solutions.  Of course the problems are carefully 
chosen so that the solutions cover the most important parts of the language 
and library, but that fact is not immediately obvious from the nature of the 
problems themselves.

"C++ Primer" follows the classical approach of treating each part of the 
language and library separately in a single place.  For example, there are 
chapters on expressions, statements, functions, templates, object-oriented 
programming, and so on.  It is also much more systematic than "Accelerated 
C++."  It is also nearly three times the size.

Which of these books you prefer will depend on your learning style more than 
anything else.  If you are willing to read the entire book sequentially, you 
will probably learn C++ faster from "Accelerated C++" than from "C++ 
Primer."  On the other hand, if you want to see in one place what all the 
different kinds of statements are, so that you can learn about them all at 
once, then you will be more comfortable with "C++ Primer."

I'm biased, of course, but I believe that either of these books is a better 
starting point for someone unfamiliar with C than any other book I can think 
of.



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


Re: Zen of Python

2005-01-22 Thread Andrew Koenig
"Fredrik Lundh" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> in some early C++ compilers, the scope for "x" was limited to the scope
> containing the for loop, not the for loop itself.  some commercial 
> compilers
> still default to that behaviour.

Indeed--and the standards committee dithered far too long before correcting 
it.

The argument that finally swayed them was this:

If you change it, you will be ridiculed for a few years.

If you do not change it, you will be ridiculed for the rest of your 
careers.


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


Re: Zen of Python

2005-01-22 Thread Andrew Koenig
"Paul Rubin"  wrote in message 
news:[EMAIL PROTECTED]

> It's really irrelevant whether anyone is using a feature or not.  If
> the feature is documented as being available, it means that removing
> it is an incompatible change that can break existing code which
> currently conforms to the spec.  If the "feature" is described as the
> bug that it is, anything that relies on it is nonconformant.

In this case, I think the right solution to the problem is two-fold:

1) from __future__ import lexical_comprehensions

2) If you don't import the feature, and you write a program that depends 
on
a list-comprehension variable remaining in scope, the compiler should
issue a diagnostic along the lines of

Warning: This program should be taken out and shot.


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


Re: Zen of Python

2005-01-22 Thread Andrew Koenig
"Paul Rubin"  wrote in message 
news:[EMAIL PROTECTED]

> It's not obvious to me how the compiler can tell.  Consider:
>
>x = 3
>if frob():
>   frobbed = True
>   squares = [x*x for x in range(9)]
>if blob():
>   z = x
>
> Should the compiler issue a warning saying the program should be taken
> out and shot?  With lexical comprehensions, the program is perfectly
> valid and sets z to 3 if blob() is true.  The whole point of lexical
> comprhensions is to make Python safe for such programs.
>
> Without lexical comprehensions, the program still doesn't depend on
> the listcomp leakage if frob() and blob() aren't simultaneously true
> (envision "assert not frobbed" before the "z = x").  So "should be
> taken out and shot" is maybe a little bit extreme.

Actually, I don't think so.  If you intend for it to be impossible for "z = 
x" to refer to the x in the list comprehension, you shouldn't mind putting 
in "from __future__ import lexical_comprehensions."  If you don't intend for 
it to be impossible, then the program *should* be taken out and shot.


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


Re: Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code

2005-07-02 Thread Andrew Koenig
"Ralf W. Grosse-Kunstleve" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

>class grouping:
>
>def __init__(self, .x, .y, .z):
># real code right here

> Emulation using existing syntax::

>def __init__(self, x, y, z):
>self.x = x
>del x
>self.y = y
>del y
>self.z = z
>del z

I think this is a bad idea, for a subtle reason.

In Python, unlike many other languages, the names of formal parameters are 
part of a function's interface. For example:

def f(x, y):
return x-y

Now f(3, 4) is -1 and f(y=3,x=4) is 1.

The names of instance variables are generally not part of a class' 
interface--they are part of its implementation.

This proposed feature, whenever used, would tie a class' implementation to 
the interface of every method that uses the feature.  As far as I can see, 
it is impossible to use the feature without constraining the implementation 
in this way.

For this reason, I would much rather have the mapping between parameter 
names and instance variables be explicit.


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


Re: breaking out of nested loop

2005-07-12 Thread Andrew Koenig
"rbt" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> What is the appropriate way to break out of this while loop if the for
> loop finds a match?

Make it a function and use a "return" statement to break out.


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


Re: iterative lambda construction

2005-02-21 Thread Andrew Koenig
"markscottwright" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Just for the hell of it, I've been going through the old Scheme-based
> textbook "Structure and Interpretation of Computer Programs" and seeing
> what I can and can't do with python.  I'm trying to create a function
> that returns the function (not the results of the function, but a
> function object) that results from applying function f to it's (single)
> argument N times.  For example, if you have "def sq(x): return x*x",
> then repeated(sq, 2)(2) = 16, repeated(sq, 3)(2) = 256, etc.
>
> I can do it recursively, like this:
>
> def repeated(f, count):
>   if count == 1:
>   return f
>   else:
>   return lambda x: f(repeated(f, count - 1)(x)
>
> But when I try to do it iteratively, it just hangs when I try to
> evaluate the results (for count > 1):
>
> def repeated2(f, count):
>newfun = f
>for i in range(count-1):
>newfun = lambda x: newfun(f(x))
>return newfun

The trouble is that Python's scoping rules are subtly different from 
Schemes, so you're binding to the wrong instance of newfun.  You should do 
this:

def repeated2(f, count):
newfun = f
for i in range(count-1):
newfun = lambda x, g=newfun: g(f(x))
return newfun

Alternatively, you could do it this way:

def repeated3(f, count):
def g(x):
for i in range(count):
x = f(x)
return x
return g


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


Re: function with a state

2005-03-06 Thread Andrew Koenig
"Xah Lee" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> globe=0;
> def myFun():
>  globe=globe+1
>  return globe
>
> apparently it can't be done like that. I thought it can probably be
> done by prefixing the variable with some package context...

You can do this:

globe=0
def myFun():
global globe
globe=globe+1
return globe

The question you should ask yourself, however, is why you want to do this. 
Do you really want to tie your function to a single global variable?  Are 
you sure you will never need more than one?

For example, the function you've written represents a counter.  Are you sure 
you will never need more than one such counter?


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


Re: how to drop all thread ??

2004-11-29 Thread Andrew Koenig
This reply is off topic but I couldn't resist:

The best way to get rid of thread is to adopt a kitten.

In fact, one of my cats is named Snobol because she is such a good string 
manipulator.


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


Re: Help With Hiring Python Developers

2004-12-06 Thread Andrew Koenig
"Aahz" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> APL.  I've heard programmers making similar comments (including possibly
> Michael's nameless acquaintance).

Especially programmers who've never used it.

To me, grousing about APL's unusual character set sounds a lot like grousing 
about Python's unusual lexical structure.  Both language aspects seem weird 
at first, but the weirdness rapidly goes away with experience.


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


MIDI library recommendations, please?

2004-12-20 Thread Andrew Koenig
Are there widely used and recommended Python libraries that will let me

1) Interpret and generate MIDI messages easily?

2) Allow me to select and communicate with MIDI devices attached to my 
computer?

I know that (2) is platform-dependent, so if there isn't a multiplatform 
version of (2) I would need it for Windows (2000 and XP).

Obviously there are many ways of accomplishing (2).  The easiest is to write 
stuff into a file and use a sequencer to play it :-)  Nevertheless, what I"m 
ultimately after is programmatic real-time control of several MIDI devices, 
and if there are already some giants around on whose shoulders I can 
stand... 


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


Re: objects as mutable dictionary keys

2004-12-27 Thread Andrew Koenig
"Peter Maas" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> This strikes me because if one can do this with instances of user
> defined classes why not with lists? Trying to use lists as dict
> keys yields "TypeError: list objects are unhashable". So why are
> list objects unhashable and user defined objects hashable? For
> user defined objects hash(x1) = id(x1), why not do the same
> with lists?

If d is a dict and t1 and t2 are tuples, and t1 == t2, then d[t1] and d[t2] 
are the same element.

If lists used the id as the hash, this property would not hold for lists.

Can you give me an example of a program for which you consider such behavior 
to be useful?


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


Re: objects as mutable dictionary keys

2004-12-27 Thread Andrew Koenig
"Peter Maas" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> This leads to the question:
>
> Why does (t1 == t2 => d[t1] identical to d[t2]) hold for user defined
> objects and not for lists? My answer: because the cmp function looks at
> id() for user defined objects and at list content for lists.

Yes.

> Why does the cmp function _have_ to look at lists contents?
> My answer: because of the existence of list literals.

No.  The reason is that lists are containers, and it it is reasonable to 
define container equality recursively as element equality.  There are no set 
literals, but that doesn't mean that set equality should be identity.


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


Re: Suggesting a new feature - "Inverse Generators"

2005-03-25 Thread Andrew Koenig
"Jordan Rastrick" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> def combineIntoRecord(): # This is an acceptor function
>   optionalline = None # We may not get given a value for this line
>   accept firstline
>   accept secondline
>   if condition(secondline):
>  accept optionalline
>   accept lastline
>   r = createRecord(firstline, secondline, lastline, optionalline)
>   return r

> recordlist = []
> for line in lines:
> recordlist.append(combineIntoRecord(line))

How about doing it this way?

class Acceptor:
def __init__(self, gen):
self.gen = gen
def next(self):
firstline = self.gen.next()
secondline = self.gen.next()
if condition(secondline):
optionalline = self.gen.next()
accept lastline
r = createRecord(firstline, secondline, lastline, optinalline)
return r

This is just a generator done longhand.  If anything in Acceptor.next raises 
StopIteration, so will Acceptor.next itself.  Which means that you can now 
write this:

for r in Acceptor(line):
recordlist.append(r)

or, for that matter,

recordlist = list(Acceptor(line))

Incidentally, I did not try to fix the bug in your code that if 
condition(secondline) is false, optionalline never gets set so the program 
will crash :-)


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


Re: Suggesting a new feature - "Inverse Generators"

2005-03-26 Thread Andrew Koenig
"Jordan Rastrick" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> But I'm not so much interested in alternate solutions to the problem
> itself, which is to be honest trivial. I'm intereseted in the
> implications of the imaginary solution of the Acceptor function.

Of course.

But you'll get more people interested in your alternatives if you can come 
up with some use cases that the existing language can't handle easily.


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


Stylistic question about inheritance

2005-03-31 Thread Andrew Koenig
Suppose I want to define a class hierarchy that represents expressions, for 
use in a compiler or something similar.

We might imagine various kinds of expressions, classified by their top-level 
operator (if any).  So, an expression might be a primary (which, in turn, 
might be a variable or a constant), a unary expression (i.e. the result of 
applying a unary operator to an expression), a binary expression, and so on.

If I were solving such a problem in C++, I would define a base class for all 
expressions, then derive the various kinds of expression classes from that 
base class.  However, I would not anticipate ever creating objects of the 
base class, so I would make it abstract.

In Python, I can imagine doing the same thing:

class Expr(object):
pass

class UnaryExpr(Expr):
# ...

class BinaryExpr(Expr):
# ...

and so on.  However, although I don't have a choice in C++ about having a 
base class--you can't use dynamic binding without it--in Python I do have 
that choice.  That is, I don't need to have the base class at all unless I 
want to have some operations that are common to all derived classes.

Of course, there are reasons to have a base class anyway.  For example, I 
might want it so that type queries such as isinstance(foo, Expr) work.  My 
question is: Are there other reasons to create a base class when I don't 
really need it right now?


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


Re: Stylistic question about inheritance

2005-03-31 Thread Andrew Koenig
"Carl Banks" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Well, Python seems to get along fine without the ability to do
> isinstance(foo,file_like_object); probably better off in the end for
> it.  So I'd say you should generally not do it.  Inheritence is for
> when different classes need to share functionality.

That's really the question:  Is it for when they need to share 
functionality, or when they are conceptually related in ways that might lead 
to shared functionality later?


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


Re: Stylistic question about inheritance

2005-03-31 Thread Andrew Koenig
""Martin v. Löwis"" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> You would normally try to avoid type queries, and rely on virtual
> methods instead, if possible.

Of course.

> It seems likely for the application
> that code can be shared across different subclasses, for example,
> you might be able to define
>
> def Expr:
>   def __str__(self):
> return '%s(%s)' % (self.__class__.__name__,
>", ".join(map(str, self.operands()))
>
> requiring you only to implement .operands() in the subclasses.

Indeed.

> If you can anticipate such common code, it is easier to add
> a base class right away. If you cannot think of a specific
> use case, there is little point in having a common base class.

So, for example, you don't think it's worth including the base class as a 
way of indicating future intent?


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


Re: Stylistic question about inheritance

2005-03-31 Thread Andrew Koenig
"Lonnie Princehouse" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> If you try this sort of inheritance, I'd recommend writing down the
> formal grammar before you start writing classes.  Don't try to define
> the grammar through the inheritance hierarchy; it's too easy to
> accidentally build a hierarchy that can't be translated into a
> single-pass-parsable grammar...

Understood.  I was using expression trees as a contrived example, and really 
want to know about the Python community's stylistic preferences for defing 
such hierarchies that don't absolutely need a root.

> I usually skip the inheritance and make everything an instance of the
> same class, e.g.
>
> class ASTNode(object): ...
>
> class Stmt(ASTNode): ...
> class Expr(ASTNode): ...
> class UnaryExpr(ASTNode): ...
> class BinaryExpr(ASTNode): ...

Eh?  There's still inheritance here: Everything is derived from ASTNode.  I 
understand that there is a separate design issue whether to make the 
hierarchy deep or shallow, but it's still a hierarchy.



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


Re: instance name

2005-04-02 Thread Andrew Koenig
"max(01)*" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> is there a way to define a class method which prints the instance name?

The term "the instance name" is misleading, because it assumes, without 
saying so explicitly, that every instance has a unique name.

In fact, there is no reason that an instance needs to have a name at all, or 
that it should have only one.

You gave this example:

instance_1 = class_1()
instance_1.myName()

but what if I did this instead?

class_1().myName()

Or this?

instance_1 = class_1()
instance_2 = instance_1
instance_2.myName()


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


I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Andrew Koenig
Downloaded and installed 64-bit Python 3.4 and pywin32-219. Both installed 
smoothly on my 64-bit Win7 machine. I added C:\Python34 to the search path.

If I launch a Windows command window and run

python -m ensurepip

I get the following:

Ignoring indexes: https://pypi.python.org/simple/
Downloading/unpacking setuptools
Cleaning up...
Exception:
Traceback (most recent call last):
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\basecommand.py", line 122, in main
status = self.run(options, args)
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\commands\install.py", line 278, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundl
e=self.bundle)
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\req.py", line 1177, in prepare_files
url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\index.py", line 209, in find_requirement
file_locations, url_locations = self._sort_locations(locations)
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\index.py", line 128, in _sort_locations
sort_path(os.path.join(path, item))
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\index.py", line 109, in sort_path
if mimetypes.guess_type(url, strict=False)[0] == 'text/html':
  File "C:\Python34\lib\mimetypes.py", line 287, in guess_type
init()
  File "C:\Python34\lib\mimetypes.py", line 348, in init
db.read_windows_registry()
  File "C:\Python34\lib\mimetypes.py", line 255, in read_windows_registry
with _winreg.OpenKey(hkcr, subkeyname) as subkey:
TypeError: OpenKey() argument 2 must be str without null characters or None, not
 str

The pip.log file contains the same text.

I'm sure I'm doing something obviously wrong here--but what?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Andrew Koenig
python --version reports 3.4.2, which is what I expected. I see no PYTHONPATH 
variable, or any environment variable with a name beginning PY (either upper or 
lower case).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Andrew Koenig
It runs and creates a classes.txt file with 803 lines. The first few:

 -> '$cpfile12'
 -> '$crfile12'
 -> '$cxfile12'
 -> '*'
 -> '.$cp'
 -> '.$cr'
 -> '.$cx'
 -> '.386'
 -> '.3ds'

A few lines in the middle that might be relevant:

 -> '.py'
 -> '.pyc'
 -> '.pyo'
 -> '.pys'
 -> '.pyw'

And the last few:

 -> '{C7A40493-BF23-4B53-AB2A-4A923B3EE34B}\x00'
 -> '{E14E55A7-29C8-4389-8E5A-3EF964510FCA}\x00'
 -> '{F5E30566-7C8F-4037-A8FF-A7382E251C56}\x00'

Anything in particular I should be looking for?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Andrew Koenig
Not sure that would be a good idea: There are 22 such keys, as opposed to only 
two keys with Windows ID strings that don't end in nulls. I found this article:

http://www.swarley.me.uk/blog/2014/04/23/python-pip-and-windows-registry-corruption/

with the comment "If you are happy to completely remove any problematic keys, 
and accept any associated breakages that may occur, then this is the easiest 
solution."

I am concerned that if I change these keys, something else might break and I 
have no easy way of figuring out what.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Andrew Koenig
Fixed it!

The aforementioned article is correct. I downloaded the RegDelNull program 
mentioned in the article 
(http://technet.microsoft.com/en-us/sysinternals/bb897448.aspx) and ran it on 
hkcr, hkcu, hklm, hku, and hkcc (short for HKEY_CLASSES_ROOT, 
HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, and HKEY_CURRENT_CONFIG), 
respectively. It deleted a bunch of keys. Rerunning the program you posted 
earlier revealed no keys with embedded nulls in their names, and ensurepip now 
works.

I have no idea how these keys got there. For all I know they are the result of 
malware.

I think it would be worthwhile changing the Python code to detect nulls and 
perhaps issue a warning that directs people to this article or something like 
it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: in Python

2005-12-15 Thread Andrew Koenig
"Andy Leszczynski" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> How can do elegantly in Python:
>
> if condition:
>a=1
> else:
>a=2

I believe that before long Python will support

a=1 if condition else 2



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


Augmented generators?

2006-01-10 Thread Andrew Koenig
Can anyone think of an easy technique for creating an object that acts like 
a generator but has additional methods?

For example, it might be nice to be able to iterate through an associative 
container without having to index it for each element.  Right now, I can say

i = iter(d)

and then repeatedly calling i.next() gives us the keys for the elements. 
But to get the corresponding value requires us to look up the key.

Of course one could define a generator that yields key-value pairs, along 
the following lines:

def kviter(d):
for i in d:
yield i, d[i]

to hide the lookup.  But this yields a tuple even when you don't want it. 
In other words, I must now write

for k, v in kviter(d):
# whatever

and I can think of situations in which I don't really want both the key and 
the value all the time.

So what I really want is something like this:

it = augiter(d)
for i in it:
if :
foo(it.value())

In other words, I want "it" to support both the next and value methods (or 
next and something else)

Of course I can write such a beast as a class, but that prevents me from 
taking advantage of the yield statement in its implementation.

So my question is: Can you think of an easy way to write something that 
looks like a generator (using yield), but can also incorporate methods other 
than next?


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


Re: How do I pass a list to a __init__ value/definition?

2006-07-25 Thread Andrew Koenig
<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> class MultipleRegression:
>def __init__(self, dbh, regressors, fund):
>self.dbh = dbh
>self.regressors = regressors
>
> and I want to be able to enter regressors as a list like
> MultipleRegression(dbh, [1,2,3,4], 5).  But when I do this only the 1
> gets passed to regressors and thus to self.regressors.

Really?

class MultipleRegression:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = regressors
foo = MultipleRegression(42, [1,2,3,4], 5)
print foo.regressors

prints [1,2,3,4]

Try it and see.


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


Re: Job advert: Your help needed

2006-02-17 Thread Andrew Koenig
"Paul Boots" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Paul Boots
> [EMAIL PROTECTED]
> +354 664 1748

Iceland?  really??


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


Re: __init__() not called automatically

2005-05-26 Thread Andrew Koenig
"Sakesun Roykiattisak" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Does c++ call base class constructor automatically ??
> If I'm not wrong, in c++ you also have to call base class constructor 
> explicitly.

In C++, if you don't call a base-class constructor (I am saying "a" rather 
than "the" because there might be more than one direct base class), it is 
called automatically with no arguments.  The only case in which you must 
call it explicitly is if it will not accept an empty argument list.


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


Re: software testing articles

2007-05-11 Thread Andrew Koenig
<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Have you ever been interested in software testing? Giving you an in
> depth analysis/knowledge on software testing!!

Looking around the site at random, I saw no "in depth analysis/knowledge" of 
anything.


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


Re: how to convert an integer to a float?

2007-03-05 Thread Andrew Koenig
<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hi, I have the following functions,  but ' dx = abs(i2 - i1)/min(i2,
> i1)' always return 0, can you please tell me how can i convert it from
> an integer to float?

I don't think that's what you really want to do.

What you really want is for dx to be a float rather than being truncated to 
an integer.  Division is going to behave that way in the future, so if you 
want it to behave that way now, you can write

from __future__ import division

at the beginning of your program.

If for some reason you don't want to rely on using a version of Python that 
knows about this future behavior, you might consider

dx = float(abs(i2 - i1))/min(i2, i1)

as an alternative.


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


How to copy a file on Windows while preserving permissions

2007-04-12 Thread Andrew Koenig
The answer to this question probably involves pywin32 or a similar library.

I would like to copy a file from one place to another on a Windows machine 
while preserving as much of the file permissions as it is possible to 
preserve with whatever my program's privileges happen to be.  If the file is 
a directory (i.e. folder), this might require temporarily granting myself 
write permission for the directory so I can (recursively) create files in 
it--again, assuming that it is possible to do so.

Obviously it would be necessary to determine whether my program's privileges 
are sufficient to solve this problem for the particular files in question.

Can someone who is more familiar than I with the vagaries of Windows file 
protection and the Python interfaces available to it point me in a useful 
direction?


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


Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Andrew Koenig
"redawgts" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

>I keep getting this error "local variable 'f' referenced before
> assignment" in the finally block when I run the following code.
>
>try:
>f = file(self.filename, 'rb')
>f.seek(DATA_OFFSET)
>self.__data = f.read(DATA_SIZE)
>self.isDataLoaded = True
>except:
>self.isDataLoaded = False
>finally:
>f.close()
>
> Can someone tell me what's wrong with the code? Am I doing something
> wrong? I'm somewhat new to python but this makes sense to me.

If the call to file raises an exception, then variable f won't have been 
created.

Here's a simple example:

def f():
throw 42
try:
x = f()
except:
print x

Try it and see what happens.  While you're at it, you might try to figure 
out what you would like it to print.



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


Re: Sub-sort after sort

2007-11-02 Thread Andrew Koenig
<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> I would want to sort by name first, then sub sort by location. Any
> ideas? Thanks!

In Python 2.3 and later, sorting is stable -- so you can sort successively 
in reverse order.  In other words, sort the list by location, then sort the 
sorted result again by name and you should get what you want.


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


Re: the annoying, verbose self

2007-11-25 Thread Andrew Koenig
"Colin J. Williams" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Alternatively, as someone else suggested, an analogue of the Pascal "with"
> could be used:
>
> def abs(self):
>   with self:
> return math.sqrt(x**2 + y**2 + z**2)

How does your suggested "with" statement know to transform x into self.x but 
not transform math.sqrt into self.math.sqrt?


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


RE: the annoying, verbose self

2007-11-25 Thread Andrew Koenig
> I am not advocating this, but this could be:

> def abs(self):
>with self:
>  with math:
>return sqrt(x**2 + y**2 + z**2)

> The idea being that "with self" use
> creates a new namespace:
>newGlobal= oldGlobal + oldLocal
>newLocal= names from self

You don't know what those names are until runtime.  Suppose, for example,
that self happens to have acquired an attribute named "math"?  Then

with self:
with math:
 return sqrt(x**2 + y**2 + z**2)

doesn't do what you expected, because "with math" turns out to have meant
"with self.math"


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


Re: How to Teach Python "Variables"

2007-11-25 Thread Andrew Koenig
"Aurélien Campéas" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> I mean : aren't C variables also bindings from names to objects ? Or what 
> ?

No, they're not.

In C, when you execute

x = y;

you cause x to become a copy of y.  In Python, when you execute

x = y

you cause x and y to be two different names for the same object.

It is difficult to distinguish these cases unless the objects in question 
are mutable, so consider this C example:

struct {int a, b;} x = {3, 4}, y = x;
x.a = 42
printf ("%d, %d\n", y.a, y.b);

and the following apparently analogous Python example:

x = [3, 4]
y = x
x[0] = 42
print y

Try them both and see how they behave.



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

Plotting package?

2006-04-25 Thread Andrew Koenig
This may be a foolish question, but what's the most straightforward way to 
plot a bunch of data in Python?

That is, I want to write a program that does some number crunching, and then 
I want to change some parameters and watch how the changes affect the 
results.  I could produce a file to hand to gnuplot, but that's a bit of a 
pain; so I'm wondering if there is a widely used package to which I can give 
my x-y pairs and have it produce a graph for me with axes, scaling, etc.


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


Re: list example

2006-04-25 Thread Andrew Koenig
"PAolo" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> for i in range(1,10):
>if i%2:
>odd.append(i)
>else:
>even.append(i)

In 2.5 you'll be able to say

for i in range(1,10):
(odd if i%2 else even).append(i)

Whether you want to do this is another question entirely, of course.


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


Re: bug in modulus?

2006-05-02 Thread Andrew Koenig
"Christophe" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> [EMAIL PROTECTED] a écrit :

> Floating point numbers just don't have the required precision to represent 
> 2.0 - 1e-050. For your specific problem, if you really want the result to 
> be < 2.0, the the best you can do is admit that floating point operations 
> have errors and return 0.0 when the modulus operation equals 2.0.

I disagree.  For any two floating-point numbers a and b, with b != 0, it is 
always possible to represent the exact value of a mod b as a floating-point 
number--at least on every floating-point system I have ever encountered. 
The implementation is not even that difficult.


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

Re: bug in modulus?

2006-05-02 Thread Andrew Koenig
"Andrew Koenig" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> I disagree.  For any two floating-point numbers a and b, with b != 0, it 
> is always possible to represent the exact value of a mod b as a 
> floating-point number--at least on every floating-point system I have ever 
> encountered. The implementation is not even that difficult.

Oops... This statement is true for the Fortran definition of modulus (result 
has the sign of the dividend) but not the Python definition (result has the 
sign of the divisor).  In the Python world, it's true only when the dividend 
and divisor have the same sign.


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


Re: Checking if a variable is a dictionary

2008-03-10 Thread Andrew Koenig
>> if type(a) is dict:
>> print "a is a dictionnary!"

> class MyDict(dict):
> pass

> a = MyDict()

> type(a) is dict
> => False

isinstance(a, dict)
=> True

So the question you need to answer is whether you want to determine whether 
an object is exactly of type dict, or whether it you are willing to accept 
types derived from dict also.


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


Re: Reference or Value?

2009-02-22 Thread Andrew Koenig
"andrew cooke"  wrote in message 
news:mailman.464.1235320654.11746.python-l...@python.org...

> as far as i understand things, the best model is:
>
> 1 - everything is an object
> 2 - everything is passed by reference
> 3 - some objects are immutable
> 4 - some (immutable?) objects are cached/reused by the system

0 - Assignment rebinds the reference on its left-hand side; it does not 
change the object to which that reference refers.

Example:

x = 42
y = x

Now x and y are bound to the same object

x = x + 1

This statement computes the value of x + 1, which is a new object with value 
43.  It then rebinds x to refer to this object, so x and y now refer to 
different objects.  Therefore:

def f(a):
a = a + 1
x = 42
f(x)

This example behaves analogously to the previous one: The assignment a = a + 
1 binds a to a new object, so it does not affect the object to which x is 
bound.

z = [3]
y = z
z[0] = z[0] + 1

The assignment rebinds z[0] to refer to a new object that has the value 4. 
This rebinding does not affect the object formerly bound to z[0].  It does, 
however, affect the value of the object to which z is bound, because it 
changes the value of its list element.  By analogy:

def g(b):
b[0] = b[0] + 1
w = [42]
g(w)

Now w[0] will be 43.


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


Re: What are the syntax for &&, ||

2008-11-25 Thread Andrew Koenig

"Peter Otten" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

>> How do I do in Python?

> if condition1 and condition2: # &&
> doThis
> elif condition3 or condition4: # ||
> doThat

> See the pattern?

if condition1 and condition2:
doThis
elif condition3 or condition4:
doThat


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


Re: Avoiding local variable declarations?

2008-11-13 Thread Andrew Koenig
"Gary Herron" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

>return chr( random.randrange(0, 26) + [26,97][random.randrange(0,
> 100) > 50]

return chr(random.randrange(0, 26) + (97 if random.randrange(0,100) > 50 
else 26))


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


Re: Python's doc problems: sort

2008-06-01 Thread Andrew Koenig
<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> I want to emphasize a point here, as i have done quite emphatically in
> the past. The Python documentation, is the world's worst technical
> writing. As far as technical writing goes, it is even worse than
> Perl's in my opinion.

I think that this claim says more about its author than it does about its 
subject.

Welcome to my killfile.


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



Re: Given a string - execute a function by the same name

2008-05-08 Thread Andrew Koenig
<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> I'm parsing a simple file and given a line's keyword, would like to call
> the equivalently named function.

No, actually, you woudn't :-)  Doing so means that if your programs input 
specification ever changes, you have to rename all of the relevant 
functions.  Moreover, it leaves open the possibility that you might wind up 
calling a function you didn't intend.

The right way to solve this kind of problem is to list all the functions you 
wish to be able to call in this way, and then explicitly define a mapping 
from keywords to the appropriate functions.  Which is exactly what you're 
doing in

> 3. Place all my functions in dictionary and lookup the function to be 
> called


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


What has become of the Python 2004 papers?

2006-03-11 Thread Andrew Koenig
http://www.python.org/community/pycon/dc2004 seems to have vanished...


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


RE: What has become of the Python 2004 papers?

2006-03-11 Thread Andrew Koenig
> Try here:
> 
> http://us.pycon.org/zope/original/pycon/pastevents/dc2004
> 

I see summaries of the paper, but when I follow the link for the papers
themselves, it leads to the same dead end.



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


Re: Multiplying sequences with floats

2006-03-24 Thread Andrew Koenig
"Christoph Zwerschke" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Anyway this would be an argument only against the variant of typecasting a 
> float with a fractional part. But what about the other variant which 
> raises an error if there is a fractional part, but works if the float is 
> actually an exact integer, like the result of 4.0//2.

I think that's a really bad idea, because of the possibility that the result 
might happen to be an exact integer on one implementation but not another. 
In such situations, the fact that it might fail on implementation X may well 
be impossible to detect by any amount of testing on implementation Y.  Such 
untestable errors are such a nuisance that it would better for the language 
to encourage programmers to give them a wide berth.


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