[issue20496] function definition tutorial encourages bad practice

2014-02-02 Thread Alan Isaac

New submission from Alan Isaac:

Section 4.6 of the tutorial introduces function definition:
http://docs.python.org/3/tutorial/controlflow.html#defining-functions

The first example defines a function that *prints* a Fibonacci series.

A basic mistake made by students new to programming is to use a function to 
print values rather than to return them.  In this sense, the example encourages 
bad practice and misses an opportunity to instruct.  Since they have already 
met lists in Section 3, I suggest that returning a list of the values and then 
printing the list would enhance the tutorial.

--
assignee: docs@python
components: Documentation
messages: 210077
nosy: aisaac, docs@python
priority: normal
severity: normal
status: open
title: function definition tutorial encourages bad practice
type: enhancement

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20496
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18844] allow weights in random.choice

2013-08-26 Thread Alan Isaac

New submission from Alan Isaac:

The need for weighted random choices is so common that it is addressed as a 
common task in the docs:
http://docs.python.org/dev/library/random.html

This enhancement request is to add an optional argument to random.choice, which 
must be a sequence of non-negative numbers (the weights) having the same length 
as the main argument.

--
messages: 196229
nosy: aisaac
priority: normal
severity: normal
status: open
title: allow weights in random.choice
type: enhancement

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11023] pep 227 missing text

2011-01-30 Thread Alan Isaac

Alan Isaac alan.is...@gmail.com added the comment:

Bear with my confusion about your response.  Are you saying that CPython 
documentation bugs cannot be submitted here, or that this does not constitute a 
CPython documentation bug?  I assume the latter.  But then, can you tell me 
where to find the correct CPython documentation after I read the following 
quote from PEP 277?

http://www.python.org/dev/peps/pep-0227/
Implementation

XXX Jeremy, is this still the case?

The implementation for C Python uses flat closures [1].

In case you might care about a user confusion ...  Your reply left me puzzled 
that PEPs that have reached the standards track would not be considered part of 
the CPython documentation distribution.

Thank you,
Alan Isaac

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11023
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11023] pep 227 missing text

2011-01-27 Thread Alan Isaac

New submission from Alan Isaac alan.is...@gmail.com:

In PEP 227 missing text is marked with XXX.  Most of this is just calls for 
examples and elaboration.  However under the Implementation section XXX marks a 
substantive question about the documentation.

Fixing this may be low priority, but a tracker search suggested it is currently 
not even being tracked ...

--
assignee: docs@python
components: Documentation
messages: 127184
nosy: aisaac, docs@python
priority: normal
severity: normal
status: open
title: pep 227 missing text
type: feature request

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11023
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue5716] Overzealous deprecation of BaseException.message

2009-12-11 Thread Alan Isaac

Alan Isaac alan.is...@gmail.com added the comment:

FYI a patch has been committed that should fix this. For discussion see
http://bugs.python.org/issue6844

--
nosy: +aisaac

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue5716
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6844] BaseException DeprecationError raises inappropriately

2009-09-16 Thread Alan Isaac

Alan Isaac alan.is...@gmail.com added the comment:

I hope it is not too annoying to link these ...

I asked thhis of Jean-Paul but now I'll ask it of George.
Since you are working on this, can you see if 
http://bugs.python.org/issue6108 
is related or in any case can be fixed at the same time?

Thanks.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6844] BaseException DeprecationError raises inappropriately

2009-09-12 Thread Alan Isaac

Alan Isaac alan.is...@gmail.com added the comment:

Since you are working on this, can you see if 
http://bugs.python.org/issue6108 
is related or in any case can be fixed at the same time?

Thanks.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6844] BaseException DeprecationError raises inappropriately

2009-09-07 Thread Alan Isaac

Alan Isaac alan.is...@gmail.com added the comment:

 The 'message' attribute itself is deprecated
 as it didn't exist prior to being introduced in 2.5.

That seems to me to be the wrong way to phrase it, and indeed that kind
of phrasing implies the current bug.  For example, it leads to the
incorrect statement that The 'message' attribute ... didn't exist prior
to being introduced in 2.5.  But looking at the docutils and Twisted
code bases, to take two examples, tells us that it **did** exist: a slew
of instances had this attribute.  The correct statement, that
BaseException did not initialize a `message` attribute, is an entirely
different matter.

Imo, deprecating setting and accessing an **instance attribute** is just
flat out wrong and grossly violates inheritability promises.  As we have
seen.

I think (?) that what was desired to be deprecated is the combination of

- setting a message attribute via BaseException.__init__, AND
- accessing an instances message attribute that was set *this way*

But in fact the setting cannot really be deprecated because it is
implicit: it is something currently done by BaseException, not by the
user.  So I think (?) the best we can do is look at whether the user
initializes BaseException (**not** the derived classes) with a single
argument, which is a nonempty string, and then tries to access this as a
`message` attribute.  Which is why I originally proposed setting a flag
when BaseException.__init__ is called and conditioning the deprecation
warning on this flag.  (E.g., the flag could be set if and only if the
user calls BaseException.__init__(instance, string).)

In any case, I think Jean-Paul is asking exactly the right question
(i.e., just what exactly is being deprecated?).  A more careful answer
will hopefully lead to less buggy DeprecationWarning.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6844] BaseException DeprecationError raises inappropriately

2009-09-05 Thread Alan Isaac

New submission from Alan Isaac alan.is...@gmail.com:

In Python 2.6 if I subclass Exception and intialize my instances with a
`message` attribute, I get a DeprecationError via BaseException.

Of course there is no problem in Py3, because adding a `message`
attribute to instances of a subclass is in fact **not** a problem and
will **not** be disallowed.  I.e., the DeprecationError is raised
incorrectly in Python 2.6.

I am not sure how to raise a DeprecationError *only* in the correct
cases, but certainly the current behavior can be much improved.  As a
crude example, if the `args` passed to BaseException has length 0, then
a flag could be set not to raise this DeprecationError.  Even if not
perfect, this would be **much** better than the current behavior, since
it is common practice not to pass the subclass's initialization
arguments on to Exception.__init__.

This behavior can be expected to affect entire libraries and therefore
should be fixed.  (Eg., it affects docutils.)  I.e., change the name of
your variable is the wrong answer to this bug report.

--
components: Interpreter Core
messages: 92281
nosy: aisaac
severity: normal
status: open
title: BaseException DeprecationError raises inappropriately
type: behavior
versions: Python 2.6

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: function factory question: embed current values of object attributes

2009-02-21 Thread Alan Isaac
Terry Reedy wrote: 
You are now describing a function closure.  Here is an example that 
might help.



It does.
Thanks,
Alan
--
http://mail.python.org/mailman/listinfo/python-list


Re: function factory question: embed current values of object attributes

2009-02-21 Thread Alan Isaac
Gabriel Genellina wrote: 
If you want a frozen function (that is, a function already set-up with  
the parameters taken from the current values of x.a, x.b) use  
functools.partial:



OK, that's also a nice idea.
Thanks!
Alan
--
http://mail.python.org/mailman/listinfo/python-list


Re: is it possible to add a property to an instance?

2009-02-20 Thread Alan Isaac

Darren Dale wrote to GHUM:

Sorry, that's an attribute, not a property.



This is a question about terminology.
In contrast to Darren's recommended usage,
I have run into the following.

If hasattr(x,'a') is True, for instance object `x`,
then `a` is an attribute of `x`.
Attributes are data attributes or callable attributes.
Data attributes are variables or properties.
Callable attributes are usually method attributes.

This seemed about right to me, but a better
(or official) taxonomy would be welcome.

Thanks,
Alan Isaac
--
http://mail.python.org/mailman/listinfo/python-list


function factory question: embed current values of object attributes

2009-02-20 Thread Alan Isaac

I have a class `X` where many parameters are set
at instance initialization.  The parameter values
of an instance `x` generally remain unchanged,
but is there way to communicate to a method that
it depends only on the initial values of these parameters
(and does not need to worry about any changes)?

The behavior of method `m` depends on these parameter values.
It turns out `m` gets called a lot, which means
that the pameter values are accessed over and over
(self.p0, self.p1, etc).  I would like to
manufacture a function equivalent to the method
that simply uses fixed values (the values at the
time it is manufactured).  I do not care if this
function is attached to `x` or not.

I have a feeling that I am turning something simple
into something complex, perhaps for lack of an
afternoon coffee or lack of CS training.  Suggestions
appreciated.

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


Re: Python with Ecmascript

2008-07-11 Thread Alan Isaac

NJSModule?
http://en.wikipedia.org/wiki/NJS



Daniel Fetchinson wrote: 

This seems to be very good indeed. Just downloaded njs but the only
njsmodule version I could find was for python 2.1. Does anyone have a
recent copy?


1. You might ask here:
http://lists.njs-javascript.org/cgi-bin/mailman/listinfo/users

2. Did you try to compile it?
Is there anything obviously 2.5 incompatible?

Cheers,
Alan Isaac
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python with Ecmascript

2008-07-10 Thread Alan Isaac

Daniel Fetchinson wrote:

Is there a way to do similar things on linux?


NJSModule?
http://en.wikipedia.org/wiki/NJS

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


Re: Market simulations with Python

2008-06-28 Thread Alan Isaac

xamdam wrote:

I am interested in market simulation with Python, simulating buyers
and sellers arriving with goods at random times. I looked at SimPy,
it's pretty nice, but all the examples are around congestion problems.
Should I a) dig deeper b) write something from scratch c) look at
another library?


You could use SimPy.

Also see:

http://gnosis.cx/publish/programming/charming_python_b10.html

http://www.mech.kuleuven.be/lce2006/147.pdf

If you plan to share you efforts, please post updates here.

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


Re: Dynamically naming objects.

2008-06-06 Thread Alan Isaac
On Jun 7, 1:20 pm, Hans Nowak 

  [user() for i in range(n)]



Kalibr wrote:
or does it somehow work? how would I address them if they all have the 
name 'u'? 



users = list(User() for i in range(n))
for user in users:
   user.do_something()

hth,
Alan Isaac

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


Re: parser recommendation

2008-06-05 Thread Alan Isaac

One other possibility:
SimpleParse (for speed).
URL:http://simpleparse.sourceforge.net/
It is very nice.
Alan Isaac
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ideas for master's thesis

2008-06-02 Thread Alan Isaac

Filip Gruszczyński wrote:
I am student of CS at the University of Warsaw, currently 4th year. I 
am attending Object Oriented Programming seminar and it is about time, 
I started looking for an idea of my master's degree project. As I like 
Python very much, I would like to do something with this language, yet 
I don't know if there are any needs/science fields, that could be used 
as a basis for a thesis. Therefore I would like to ask, if there is 
any way to access any person involved in development, who would help 
me find some field that need to be developed/researched (rather the 
first one) and would be close enough to the OOP, so I could present it 
to a mentor (he is cool with doing something for OSS community). 


There has been discussion of replacing/supplementing the 
NumPy matrix class.  The newmatrix class would still 
subclass ndarray but would handle indexing differently.
I am confident that you could get a lot of guidance on the 
NumPy list if you were interested in taking this on.


Cheers,
Alan Isaac
--
http://mail.python.org/mailman/listinfo/python-list

Re: should I put old or new style classes in my book?

2008-05-29 Thread Alan Isaac

This thread raises two questions for me.



1. I take it from this thread that in Python 3 the

following are equivalent:



   class Test: pass



   class Test(object): pass



Is that correct, and if so, where is it stated explicitly?

(I know about the all classes are new style classes statement.)



2. I take it from this thread that in Python 2.2+

  if I put the following at the top of a module ::



   __metaclass__ = type



then all the classes defined in that module will be newstyle 

classes.  Is that correct?  Somehow I did not grok that from 


URL:http://docs.python.org/ref/metaclasses.html

but it seems right.



Thank you,

Alan Isaac




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


Re: make a string a list

2008-05-29 Thread Alan Isaac

Nikhil wrote:

or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made 
into a list and I can easily see if the required value (its a numeral)

is present in it or not after some position or after some characters'
position. 


Why dont the ``find`` or ``index`` methods work for you?
http://docs.python.org/lib/string-methods.html

Cheers,
Alan Isaac
--
http://mail.python.org/mailman/listinfo/python-list


Re: code of a function

2008-05-29 Thread Alan Isaac

Anand Patil wrote:
If you're using IPython, you can do svd?? . 



http://www.scipy.org/doc/numpy_api_docs/numpy.linalg.linalg.html

hth,
Alan Isaac
--
http://mail.python.org/mailman/listinfo/python-list


Re: observer pattern (notification chain synchronization)

2008-05-10 Thread Alan Isaac

J. Cliff Dyer wrote:


looks like a good approach to me




OK, thanks.

Another approach is to begin with a set of stocks

and remove them as they report.  You can then trigger

a report with the empty set instead of repeatedly

calling ``all``. After a report the set can be

refilled.



Cheers,

Alan Isaac


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


observer pattern (notification chain synchronization)

2008-05-09 Thread Alan Isaac

A question related to the observer pattern...

Suppose I have a variant: there are stocks, mutual funds, and investors.  Let us say that funds are observers for multiple stocks, and investors are observers for funds.  Once a month all stocks notify their observing funds of their end-of-month price, and *then* all fund to notify their observing investors of their end-of-month investment value. 


What is a good way to enforce this timing?  (I.e., a fund should not notify an 
its investors until all stocks have reported.)

Here is one way:

- for each fund, create a ``reportreceived`` dict that maps stocks to booleans 
(initially False)
- as each stock notifies its funds, the fund changes False to True and checks 
``all(reportreceived.values())`` to determine whether it is ok to notify 
investors.
- When it is ok, the fund notifies investors and resets all the ``reportreceived`` values. 


Is this sensible enough? What are standard and better ways?

Thank you, 
Alan Isaac 


PS I am drawing on the description of the observer pattern at
URL:http://www.dofactory.com/Patterns/PatternObserver.aspx#_self1
The real world aspects are just to add some concreteness.
--
http://mail.python.org/mailman/listinfo/python-list


observer pattern question #1 (reference to subject)

2008-05-08 Thread Alan Isaac

I have two questions about the observer pattern in Python.

This is question #1.  (I'll put the other is a separate post.)



Here is a standard example of the observer pattern in Python:



http://en.wikipedia.org/wiki/Observer_pattern



Contrast with this rather standard discussion:



http://www.dofactory.com/Patterns/PatternObserver.aspx#_self1



The difference I am focusing on is that in the latter,

the observer (investor) maintains a reference to the

subject (stock).  


(Many questions can be raised of course: see the discussion at

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/131499).



Is anything lost by not maintaining this reference (other 

than error checking ...)?  If I feel the observer needs 

access to the subject, what is wrong with just having the 


subject pass itself as part of the notification?



Thank you,

Alan Isaac


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


observer pattern question #2 (notification chain)

2008-05-08 Thread Alan Isaac

I have two questions about the observer pattern in Python.

This is question #2.  (I'll put the other is a separate post.)



Consider this standard example of the observer pattern in Python:



URL:http://en.wikipedia.org/wiki/Observer_pattern



Now suppose I have a variant.

(I am not a programmer, so this may be a separate related pattern.)

To make it a little concrete and relate it to the discussion at

URL:http://www.dofactory.com/Patterns/PatternObserver.aspx#_self1,

suppose there are stocks, mutual funds, and investors.

Let us say that funds are observers for stocks,

and investors are observers for funds.

Once a month I want all stocks to notify their observing funds

of their end-of-month price, and *then* all fund to notify their

observing investors of their end-of-month investment value.



I see a couple differences from the standard pattern referenced above,

and one problem.



Differences:



- observer/subject linked hierarchy (funds are both observers and subjects)

- many-many relation (a fund is an observer of many stocks)



I think (?) these differences are inessential, but the 

second does create the following problem: if I do not want 

a fund to notify an its investors until all stocks have 

reported, what is the best way to determine the last report 


in event?



Here is one way: create a ``reportreceived`` dict that maps 

stocks to True or False, and after each stock notification 


change False to True and check ``all(reportreceived.values())``

to determine whether it is ok to notify investors.  When it 


is ok, then notify investors and reset all the

``reportreceived`` values.



This is meant to be a general description so please do not 

focus on the real world aspects, which are only for 


illustration.



Thank you,

Alan Isaac 
--

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


Re: observer pattern question #1 (reference to subject)

2008-05-08 Thread Alan Isaac

Alan Isaac [EMAIL PROTECTED] writes:


Is anything lost by not maintaining this reference (other 


than error checking ...)?  If I feel the observer needs 


access to the subject, what is wrong with just having the 


subject pass itself as part of the notification? 






Ville M. Vainio wrote:

It reduces the number of places the observer can be called 


from, because now the event source is part of the function 


signature. If you omit the event source in the signature, 


you gain looser coupling - it's the observer business to 


create the dependency. 




As I said, I'm not a programmer, and perhaps as a result, 

I have questions about both of your points.  I'll stick with 


the stock example, since it is concrete.



Note that the observor/listener in the example at

URL:http://en.wikipedia.org/wiki/Observer_pattern

does not create a reference to the subject.

So in this context I take you to be saying something like 

the following: OK, here's the pattern, now your listener 

wants to know the event source, do not ask something new the 

subject to respond to that need.  That is unnecessary 

coupling.  Instead, just rewrite your listener to maintain 


a reference to the subject.



Do I have that right?



It seems to me that this response begs the question.

That is, an appropriate reply seems to be: here is a related 

pattern, call it observer2.  In the observer2 pattern, the 


subject has a getState method and always passes itself when

it notifies the observer. Now: in what sense does this 


require closer coupling?





Also, consider the situation where you want all investors 


to refresh their idea about stock prices (because of, 


short network failure). It's easy to run through a list of 


them and call update() for everybody, while it's not so 


easy to find out what stock the investor is observing 


(that's the investors business!) and pass that object to 


the investor in the call. 




I seem to be missing your point here.

Let's keep using the stock/investor example.

When the network comes up, each stock should notifies 


everyone in its list of listeners.  It either passes itself

or does not.  In either case, the investor took care of hir 


side of the business by registering as a listener.

If the stock does not notify all its registered investors, 


it is breaking its contract.  So, what is the problem?



Or to put it a different way, your suggestion that the 

investors should have to *pull* this information from the 

stocks seems out of step with the observer pattern, where 

the stocks should be pushing this information to the 


investor.



Again, talk of stocks and investors is just a convenient 

short hand for discussing the observer pattern.  I do not 


care about stocks/investors more than other applications.




Are these school questions btw?




No, sorry.  But I still appreciate the feedback.



Cheers,

Alan Isaac


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


Re: observer pattern question #1 (reference to subject)

2008-05-08 Thread Alan Isaac

Ville M. Vainio wrote:

in case of stocks, you are probably monitoring several 


stock objects, so the stock should probably pass itself to 



the observer




OK.  This is related to my question #2 (in a separate 


thread), where I'd also appreciate your comments.





analogous to a typical UI scenario where you have several 


widgets forwarding events to one big observer - and they 


*do* provide the event source and event type. 




That is a helpful comparison.





All in all, though, the right solution should be obvious 


from the problem at hand. 




For CS types, maybe.  The rest of us are just trying to get 


things done without as much background and context.



Thanks,

Alan


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


Re: python vs. grep

2008-05-08 Thread Alan Isaac

Anton Slesarev wrote:

I've read great paper about generators:
http://www.dabeaz.com/generators/index.html 
Author say that it's easy to write analog of common linux tools such 
as awk,grep etc. He say that performance could be even better. 
But I have some problem with writing performance grep analog. 



https://svn.enthought.com/svn/sandbox/grin/trunk/

hth,
Alan Isaac
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help me on function definition

2008-04-04 Thread Alan Isaac
aeneng wrote:

 WHAT IS WRONG WITH MY CODE? 

 def cross(u,v)



Missing colon.



hth,

Alan Isaac


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


Re: method to create class property

2008-03-18 Thread Alan Isaac
Diez B. Roggisch wrote:

 In python 3.0, there will be an even nicer way - propset:



 @property 

 def foo(self):

  return self._foo



 @propset 

 def foo(self, value):

  self._value = value





Isn't that::



  @propset(foo)

  def foo(self, value):

  self._value = value



Cheers,

Alan Isaac


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


Re: no more comparisons

2008-03-13 Thread Alan Isaac
Dan Bishop wrote:

 def cmp_key(cmp_fn):

 class CmpWrapper(object):

 def __init__(self, obj):

 self.obj = obj

 def __cmp__(self, other):

 return cmp_fn(self.obj, other.obj)

 return CmpWrapper



Apparently I'm overlooking something obvious ...

how is this supposed to work if __cmp__ is no longer

being called?  (Which was my understanding.)



Thank you,

Alan Isaac




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


Re: no more comparisons

2008-03-13 Thread Alan Isaac
 Dan Bishop wrote:

 def cmp_key(cmp_fn):

class CmpWrapper(object):

def __init__(self, obj):

self.obj = obj

def __cmp__(self, other):

return cmp_fn(self.obj, other.obj)

return CmpWrapper







 On Mar 13, 12:38 pm, Alan Isaac  wrote:

 how is this supposed to work if __cmp__ is no longer 

 being called?  (Which was my understanding.)





Carl Banks wrote:

 It won't.  In Python 3.0 you'd have to write this class in terms of 

 rich comparisons (__lt__, __gt__, etc.). 





Exactly.  So something simple (define an anonymous function)

has become a bit of a pain.



On the other hand, I've looked through my extant code and

have not found a use of ``cmp`` that I cannot work around.

So maybe this is not as bad as I feared.  What are some use

cases that will clearly be harder (i.e., at least require

a slightly elaborate wrapper) after this change?



Cheers,

Alan Isaac


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


Re: no more comparisons

2008-03-13 Thread Alan Isaac
Mark Dickinson wrote:

 Sorting tuples, where the second item in the tuple should 

 have the opposite ordering to the first is going to be 

 a bit of a pain.  Or even worse, where the ordering of the 

 second item depends on the value of the first item in the 

 tuple. 



This is like some examples where I had used cmp,

but if I understand correctly I think it is not a problem.



 For example, suppose that (for whatever contrived reason)

 you're representing integers in (sign, magnitude) format 

 by tuples (s, i), where s = 0 or 1 (0 for positive, 1 for 

 negative) and i is a string representing the absolute 

 value of the integer.  So 



Does this do it? ::



key= lambda x: (-x[1],int(x2))



Here I am depending on the lexicographic sorting of tuples.
Without that there would be real trouble.



Cheers,

Alan Isaac


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


Re: no more comparisons

2008-03-13 Thread Alan Isaac
Dan Bishop wrote:

 Even assuming you meant (-x[0], int(x[1])), that sorts negative 

 numbers in the wrong order.  You want 

 key = lambda x: (-1 if x[0] else 1) * int(x[1])





Sorry, was speed typing (very badly) and the question 

actually had two different problem descriptions.

As you suggest, what I meant was::



key= lambda x: (-x[0], int(x[1]))



which was meant to address:



Sorting tuples, where the second item in the tuple 

should have the opposite ordering to the first is going 

to be a bit of a pain.



But you are quite right, the problem was then specified to 

numerical order, for which I like your solution.  Or even::



key= lambda x: -int(x[1]) if x[0] else int(x[1])





The key point being, if you will, that this use case does 

not support the idea that relying on ``key`` will be so bad.  

So, what is a case that is really uncomfortable?



Thanks,

Alan Isaac


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


no more comparisons

2008-03-12 Thread Alan Isaac
I was surprised to see that
comparison is slated for death
in Python 3000.

For example:
http://www.python.org/dev/peps/pep-3100/
list.sort() and builtin.sorted() methods: eliminate cmp parameter [27] 
[done]

But there is a rumor of a PEP to restore comparisons.
http://mail.python.org/pipermail/python-3000/2008-January/011764.html

Is that going anywhere?

Also, what is the core motivation for removing this functionality?

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


Re: no more comparisons

2008-03-12 Thread Alan Isaac
Paul Rubin wrote:

 The cmp option should not be removed.  However, requiring 

 it to be specified as a keyword parameter instead of just 

 passed as an unlabelled arg is fine. 



Sure; I would have no problem with that.

But that is not what is happening.

As for Carl's suggestion to use ``key``:
this is already possible when it is convenient,
but it is not always convenient.  (Even aside
from memory considerations.)



By the way, I even saw mention of even removing the

``cmp`` built-in.



Cheers,

Alan Isaac


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


Re: Can't get items out of a set?

2008-03-08 Thread Alan Isaac
Cruxic wrote:

 people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )

 ... 

 p = people.get_equivalent(2)  #method doesn't exist as far as I know 

 print p.name  #prints Sue 



def get_equivalent(test, container):

  for p in container:

if p == test:

  return p



hth,

Alan Isaac





#example (note change in __eq__ to match your case; fix if nec)



class Person:

  def __init__(self, id, name):

self.id = id

self.name = name

  def __hash__(self):

return self.id

  def __eq__(self, other):

return self.id == other



people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )



get_equivalent(2,people)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to return a variable and use it...?

2008-03-03 Thread Alan Isaac
Nathan Pinno wrote:

 Is it possible to return a variable and then use it



I think you are asking about the ``global`` statement.

URL:http://docs.python.org/ref/global.html



 like the following:



Presumably not.  ;-)



Cheers,

Alan Isaac


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


Re: tuples, index method, Python's design

2008-03-02 Thread Alan Isaac
On April 12th, 2007 at 10:05 PM Alan Isaac wrote:

 The avoidance of tuples, so carefully defended in other 

 terms, is often rooted (I claim) in habits formed from 

 need for list methods like ``index`` and ``count``. 

 Indeed, I predict that Python tuples will eventually have 

 these methods and that these same people will then defend 

 *that* status quo.





URL:http://python.org/download/releases/2.6/NEWS.txt



- Issue #2025 :  Add tuple.count() and tuple.index() 

  methods to comply with the collections.Sequence API.



Cheers,

Alan Isaac




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


Re: tuples, index method, Python's design

2008-03-02 Thread Alan Isaac
Paul Boddie wrote:

 Here's the tracker item that may have made it happen:

 http://bugs.python.org/issue1696444 

 I think you need to thank Raymond Hettinger for championing the 

 cause. ;-)





Yes indeed!

Alan Isaac


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


Re: more pythonic

2008-02-29 Thread Alan Isaac
Paul McGuire wrote:

 In general, whenever you have:

 someNewList = []

 for smthg in someSequence:

 if condition(smthg):

 someNewList.append( elementDerivedFrom(smthg) )



 replace it with:

 someNewList = [ elementDerivedFrom(smthg)

   for smthg in someSequence

 if condition(smthg) ]







What is the gain?  (Real question.)

I think the first is often easier to read.

Is the second more efficient?



Also, I think list comprehensions are often easier to read

as equivalent generator expressions:



  someNewList = list( elementDerivedFrom(smthg)

for smthg in someSequence

  if condition(smthg) )



Tastes vary of course.



Cheers,

Alan Isaac


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


Re: Pythons Ladders

2008-02-28 Thread Alan Isaac
Benoit wrote:
 Give
 me something to do! 


Help to improve one of the experimental
writers for docutils.

IIRC, the ODT writer does not yet support figures
and tables.

http://www.rexx.com/~dkuhlman/odtwriter.html

The rst2wordml writer appears to be getting its
features in place, but when I last used it,
some tinkering was required.  You could work
on adding features.

http://docutils.sourceforge.net/sandbox/rst2wordml/readme.html

If you get good enough a docutils, provide an option
to have the number for the note be superscripted, like
the note reference can be.

fwiw,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asynchronous alarm

2008-02-24 Thread Alan Isaac
Paul Rubin wrote:
 a = Event()
 Thread(target=f, args=(a,)).start()
 raw_input('hit return when done: ')
 a.set()

Simple and elegant.
Thank you.
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


asynchronous alarm

2008-02-23 Thread Alan Isaac
Goal: turn off an audible alarm without
terminating the program.  For example,
suppose a console program is running::

while True:
sys.stdout.write('\a')
sys.stdout.flush()
time.sleep(0.5)

I want to add code to allow me to turn off
this alarm and then interact with the
program in its new state (which the alarm
alerts me to).

Question: how best to do this mostly simply
in a console application and in a Tkinter application?

I realize this must be a standard problem so that there
is a good standard answer.  Here are some naive solutions
that occured to me.

Solution C1 (console): poll keyboard inside the loop.
E.g., URL:http://effbot.org/librarybook/msvcrt.htm
Problem: no platform independent way to do this?

Solution C2 (console): handle KeyboardInterrupt.
An ugly hack. But works fine.

Solution C3 (console): start alarm in one thread
and wait for raw_input.  (Should that be in another
thread?  It does not seem to matter.)
This seems plausible, but I know nothing about threads
except that nonprogrammers tend to make mistakes
with them, so I hesitate.

Solution G1 (gui): start alarm in a thread but
include a test for a variable that can be set
by a button push?  (Sounds plausible etc.)

Solution G2 (gui): start alarm but
somehow let Tkinter listen for an event
without programming any threads. Possible??

Thanks,
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic output question

2008-01-25 Thread Alan Isaac
John Deas wrote: 
 My problem is that f.read() outputs nothing

Since ``open`` did not give you an IOError,
you did get a handle to the files,
so this suggests that the files you read
are empty...

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


Re: find minimum associated values

2008-01-25 Thread Alan Isaac
[EMAIL PROTECTED] wrote:
 I'd use the first solution.

It can be speeded up a bit with
a try/except:

for k,v in kv:
try:
if d[k]  v:
d[k] = v
except KeyError:
d[k] = v

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: find minimum associated values

2008-01-25 Thread Alan Isaac
Alan Isaac wrote:
 
 #sort by id and then value
 kv_sorted = sorted(kv, key=lambda x: (id(x[0]),x[1]))
 #groupby: first element in each group is object and its min value
 d =dict( g.next() for k,g in groupby( kv_sorted, key=lambda x: x[0] ) )
 
 Yes, that appears to be fastest and is
 pretty easy to read.

On average.
For the specified problem.
;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: find minimum associated values

2008-01-25 Thread Alan Isaac
Steven Bethard wrote:
 [3rd approach] Seems pretty enough to me. ;-)

I find it most attractive of the lot.
But its costs would rise if the number
of values per object were large.

Anyway, I basically agree.

Thanks,
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


find minimum associated values

2008-01-25 Thread Alan Isaac
I have a small set of objects associated with a larger
set of values, and I want to map each object to its
minimum associated value.  The solutions below work,
but I would like to see prettier solutions...

Thank you,
Alan Isaac

===

import time, random
from itertools import groupby
from collections import defaultdict

class Pass:
pass

# arbitrary setup
keys = [Pass() for i in range(10)]*3
vals = [random.random() for i in range(30)]
kv = zip(keys,vals)
random.shuffle(kv)

#OBJECTIVE:
# find minimum val associated with each key in kv

print method 1: brute force
t=time.clock()
d = dict()
for k,v in kv:
if k in d:
if d[k]  v:
d[k] = v
else:
d[k] = v
print time.clock()-t
print d
print

print method 2: groupby
t=time.clock()
d = dict()
kv_sorted = sorted(kv, key=lambda x: id(x[0]))
for k, g in groupby( kv_sorted, key=lambda x: x[0] ):
d[k] = min(gi[1] for gi in g)
print time.clock()-t
print d
print

print method 3: defaultdict
t=time.clock()
d = defaultdict(list)
for k,v in kv:
d[k].append(v)
for k in d:
d[k] = min(d[k])
print time.clock()-t
print d


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


Re: find minimum associated values

2008-01-25 Thread Alan Isaac
Paul Rubin wrote: 
 How about something like:
 
   kv_sorted = sorted(kv, key=lambda x: (id(x[0]), x[1]))


You mean like this?

#sort by id and then value
kv_sorted = sorted(kv, key=lambda x: (id(x[0]),x[1]))
#groupby: first element in each group is object and its min value
d =dict( g.next() for k,g in groupby( kv_sorted, key=lambda x: x[0] ) )

Yes, that appears to be fastest and is
pretty easy to read.

Thanks,
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairs from a list

2008-01-22 Thread Alan Isaac
I suppose my question should have been,
is there an obviously faster way?
Anyway, of the four ways below, the
first is substantially fastest.  Is
there an obvious reason why?

Thanks,
Alan Isaac

PS My understanding is that the behavior
of the last is implementation dependent
and not guaranteed.

def pairs1(x):
for x12 in izip(islice(x,0,None,2),islice(x,1,None,2)):
yield x12

def pairs2(x):
xiter = iter(x)
while True:
yield xiter.next(), xiter.next()

def pairs3(x):
for i in range( len(x)//2 ):
yield x[2*i], x[2*i+1],

def pairs4(x):
xiter = iter(x)
for x12 in izip(xiter,xiter):
yield x12
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairs from a list

2008-01-22 Thread Alan Isaac
Arnaud Delobelle wrote:
 According to the docs [1], izip is defined to be equivalent to:
 
  def izip(*iterables):
  iterables = map(iter, iterables)
  while iterables:
  result = [it.next() for it in iterables]
  yield tuple(result)
 
 This guarantees that it.next() will be performed from left to right,
 so there is no risk that e.g. pairs4([1, 2, 3, 4]) returns [(2, 1),
 (4, 3)].
 
 Is there anything else that I am overlooking?
 
 [1] http://docs.python.org/lib/itertools-functions.html


URL:http://bugs.python.org/issue1121416

fwiw,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairs from a list

2008-01-22 Thread Alan Isaac
Arnaud Delobelle wrote:
 pairs4 wins.


Oops. I see a smaller difference,
but yes, pairs4 wins.

Alan Isaac

import time
from itertools import islice, izip

x = range(51)

def pairs1(x):
return izip(islice(x,0,None,2),islice(x,1,None,2))

def pairs2(x):
xiter = iter(x)
while True:
yield xiter.next(), xiter.next()

def pairs3(x):
for i in range( len(x)//2 ):
yield x[2*i], x[2*i+1],

def pairs4(x):
xiter = iter(x)
return izip(xiter,xiter)

t = time.clock()
for x1, x2 in pairs1(x):
pass
t1 = time.clock() - t

t = time.clock()
for x1, x2 in pairs2(x):
pass
t2 = time.clock() - t

t = time.clock()
for x1, x2 in pairs3(x):
pass
t3 = time.clock() - t

t = time.clock()
for x1, x2 in pairs4(x):
pass
t4 = time.clock() - t

print t1, t2, t3, t4

Output:
0.317524154606 1.13436847421 1.07100930426 0.262926712753
-- 
http://mail.python.org/mailman/listinfo/python-list


pairs from a list

2008-01-21 Thread Alan Isaac
I want to generate sequential pairs from a list.
Here is a way::

from itertools import izip, islice
for x12 in izip(islice(x,0,None,2),islice(x,1,None,2)):
print x12

(Of course the print statement is just illustrative.)
What is the fastest way? (Ignore the import time.)

Thanks,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


simple try/except question

2007-10-29 Thread Alan Isaac
Is the behavior below expected?
If so, why is the exception not caught?
Thanks,
Alan Isaac

 x,y='',''
 try: x/y
... except TypeError: print 'oops'
...
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: unsupported operand type(s) for /: 'str' and 'str'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple try/except question

2007-10-29 Thread Alan Isaac
Tim Chase wrote:
 It works for me(tm)...
 Python 2.4.3

Sorry to have left out that detail.
Yes, it works for me in Python 2.4,
but not in 2.5.1.

The code I posted was copyied from the interpreter.

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


Re: simple try/except question

2007-10-29 Thread Alan Isaac
False alarm.
Fresh start of interpreter and all is well.
Apologies.
Still tracking.
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple try/except question

2007-10-29 Thread Alan Isaac
Gabriel Genellina wrote:
 Perhaps you reassigned TypeError?

Yes, that was it.
Sheesh.

Thanks!
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python and Combinatorics

2007-10-25 Thread Alan Isaac
none wrote:
   Is there some package to calculate combinatorical stuff like (n over 
 k), i.e., n!/(k!(n - k!) ?

Yes, in SciPy.
Alan Isaac

 from scipy.misc.common import comb
 help(comb)
Help on function comb in module scipy.misc.common:

comb(N, k, exact=0)
Combinations of N things taken k at a time.

If exact==0, then floating point precision is used, otherwise
exact long integer is computed.

Notes:
  - Array arguments accepted only for exact=0 case.
  - If k  N, N  0, or k  0, then a 0 is returned.
-- 
http://mail.python.org/mailman/listinfo/python-list


unpickle from URL problem

2007-10-10 Thread Alan Isaac
I am on a Windows box.

I pickle a tuple of 2 simple objects with the pickle module.
It pickles fine.  It unpickles fine.

I upload to a server.
I try to unpickle from the URL.  No luck.  Try it:
x1, x2 = 
pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example1'))

I change the filetype to unix.  I upload again.
I try to unpickle from the URL.  Now it works.  Try it:
x1, x2 = 
pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example2'))

Why the difference?

Thank you,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unpickle from URL problem

2007-10-10 Thread Alan Isaac
Marc 'BlackJack' Rintsch wrote:
 Pickles are *binary* files, not text files

Actually not:
http://docs.python.org/lib/node316.html

These were created with protocol 0.

But my question is about the different outcomes
I observed.

Thank you,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unpickle from URL problem

2007-10-10 Thread Alan Isaac
Jean-Paul Calderone wrote:
 You shouldn't unpickle things you get from the network, since pickle can
 execute arbitrary code: http://jcalderone.livejournal.com/15864.html


Yes, but I have my reasons.
(This is not library code: I just want students
to all be using the same objects for an exercise,
and I need not to worry about their directory structure.)

Can you explain the differences I see?

Thank you,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unpickle from URL problem

2007-10-10 Thread Alan Isaac
Hrvoje Niksic wrote:
 The first upload breaks the file.  You uploaded it in (presumably
 FTP's) text mode, which changes \n - \r\n.  But you download it using
 http, which specifies no such conversion in the opposite direction.

No: I used binary upload both time.
(Unless my ftp client is broken, and I think not.)

The first example simply keeps the Windows eols,
which are present in the pickled file on my Windows machine.
This is the one created by pickle.dump.
This file unpickles just fine.

I altered the 2nd file before upload,
changing to the Unix eol convention.
This also unpickles just fine on my machine,
but in addition the urllib download of this
file unpickles just fine.
 
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


announcement: OpenOpt and GenericOpt

2007-09-10 Thread Alan Isaac
OpenOpt and GenericOpt
==

Introducing two new optimization packages.
OpenOpt and GenericOpt are 100% Python
with a single dependency: NumPy.
For more detail see below and also
URL:https://projects.scipy.org/scipy/scikits/wiki/Optimization

OpenOpt
---

OpenOpt is new open source optimization framework.
OpenOpt is released under the BSD license.

The primary author and current maintainer of
OpenOpt is Dmitrey Kroshko email:openopt AT ukr.net
(Optimization Department, Cybernetics Institute, Ukrainian Science Academy)

OpenOpt goal: provide an open source alternative
to TomOpt TOMLAB (optimization framework for MATLAB)
and related optimization frameworks.  Currently OpenOpt
offers connections to a variety of open source solvers,
primarily for unconstrained optimization. (See below.)

OpenOpt provides provides connections a vraity of solvers,
including those in GenericOpt, which are included.
(Users will need to download other solvers;
we provide URLs for the downloads.)


GenericOpt
--

GenericOpt is a toolkit for building specialized optimizers.
GenericOpt is released under the BSD license.

The primary author and current maintainer of GenericOpt is
Matthieu Brucher email:matthieu.brucher AT gmail.com

GenericOpt goal: provide an open source, extensible
toolit for component-wise construction of specialized
optimizers.  GenericOpt allows users who want detailed control to construct
their own solvers by choosing among a variety of algorithm components
(currently, most choices are among step and line-search algorithms.)

Usage: see Matthieu Brucher's tutorial 
URL:https://projects.scipy.org/scipy/scikits/wiki/Optimization/tutorial.  

Limitation: currently GenericOpt provides only unconstrained solvers. 

SciKits
---

The SciPy project is developing a collection of open source packages for
scientific computing which are allowed to have more dependencies and
more varied licenses than those allowed for SciPy proper.
In contrast to SciPy, the related scikits may host any 
OSI-approved licensed code.  See
URL:https://projects.scipy.org/scipy/scikits/

OpenOpt and GenericOpt are available together as a
SciPy scikit.  The provides a unified optimization
framework along with a collection of solvers.
However, neither depends on the other.


OpenOpt Details
---

Key feature: a unified calling interface for all solvers,
a variety of pure Python solvers, and connections to 
numerous external solvers.

Example::

from scikits.openopt import NLP
p = NLP(lambda x: (x-1)**2, 4)
r = p.solve('ralg')

In this example,
the objective function is (x-1)^2,
the start point is x0=4,
and 'ralg' specifies the name of solver involved.

See much more detailed example here 
URL:http://projects.scipy.org/scipy/scikits/browser/trunk/openopt/scikits/openopt/examples/nlp_ALGENCAN.py
 




OpenOpt Connected External Solvers
--

Non-linear problems (NLP)
~

- ALGENCAN http://www.ime.usp.br/%7Eegbirgin/tango/py.php 
  (GPL)
- lincher (BSD) (all types of constraints and 1st derivatives),
- ralg (BSD) (currently unconstrained only)
- scipy_tnc and scipy_lbfgsb (box-bounded, requires scipy 
  installed, BSD)

Non-smooth problems (NSP)
~

- ralg (BSD)
- ShorEllipsoid (BSD)
  (for small-scale problems with nVars = 1..10, former for 
  medium-scaled problems with nVars = 1...1000, requires r0)
  
Both are unconstrained for now.


Linear problems (LP)


- lp_solve http://sourceforge.net/projects/lpsolve (LGPL)
- glpk http://www.gnu.org/software/glpk/ (GPL)
- CVXOPT http://www.ee.ucla.edu/%7Evandenbe/cvxopt/ (GPL) 
  (currently glpk requires CVXOPT installed)


Mixed-integer problems (MILP)
~

- lp_solve http://sourceforge.net/projects/lpsolve 


Quadratic problems (QP)
~~~
- CVXOPT http://www.ee.ucla.edu/%7Evandenbe/cvxopt/ (GPL) 
  (please note - NLP lincher solver requires QP solver, and 
  the only one for now is CVXOPT one)

Here you can look at examples for NLP 
http://projects.scipy.org/scipy/scikits/browser/trunk/openopt/scikits/openopt/examples/nlp_1.py,
 
NSP 
http://projects.scipy.org/scipy/scikits/browser/trunk/openopt/scikits/openopt/examples/nsp_1.py,
 
QP 
http://projects.scipy.org/scipy/scikits/browser/trunk/openopt/scikits/openopt/examples/qp_1.py,
 
LP 
http://projects.scipy.org/scipy/scikits/browser/trunk/openopt/scikits/openopt/examples/lp_1.py,
 
MILP 
http://projects.scipy.org/scipy/scikits/browser/trunk/openopt/scikits/openopt/examples/milp_1.py
 


Acknowledgements


Development of OpenOpt was supported by Google through the 
Google Summer of Code (GSoC) program, with Alan G. Isaac
as mentor.  Additonal mentorship was provided by Jarrod Milman. 
Debts to the SciPy community are many, but we would particularly
like to thank Nils Wagner.


Appeal
==

The primary author and current maintainer of OpenOpt is 

Re: Any syntactic cleanup likely for Py3? And what about doc standards?

2007-09-05 Thread Alan Isaac
Torsten Bronger wrote: 
 I like to use properties.  However, Python already
 has properties.  Their syntax is quite nice in my opinion, and
 rather explicit, too.  

Yes.

 Their only flaw is that they are not
 virtual (in C++ speak).  In other words, you can't pass a self
 parameter to them.

http://www.kylev.com/2004/10/13/fun-with-python-properties/

fwiw,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: create Powerpoint via com

2007-09-02 Thread Alan Isaac
Well, my needs were very limited so the
result is too, but in case someone else
just needs to get started:
http://econpy.googlecode.com/svn/trunk/utilities/mso.py

Comments, suggestions, additions welcom.

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


Re: create Powerpoint via com

2007-08-31 Thread Alan Isaac
How about chart creation (in Ppt 2003)?
I do not see how to do this with Python.

[EMAIL PROTECTED] wrote:
 You probably need to browse the COM object using PythonWin, which is a
 part of the ActiveState distro. You can also use Python's builtin
 function, dir, to find out various methods of COM.
 
 Here's some info in messing with charts in Excel, which should be
 similar to chart manipulation in PowerPoint.
 
 http://mail.python.org/pipermail/python-win32/2005-June/003511.html
 http://mail.python.org/pipermail/python-win32/2003-March/000839.html
 http://www.thescripts.com/forum/thread21565.html
 http://mathieu.fenniak.net/plotting-in-excel-through-pythoncom/


Thanks!
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


create Powerpoint via com

2007-08-30 Thread Alan Isaac
Can someone point me to a simple example
or better yet tutorial for creating
a Powerpoint using Python.

Thanks,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: create Powerpoint via com

2007-08-30 Thread Alan Isaac
[EMAIL PROTECTED] wrote:
 Hope that helps!

Yes indeed.
Thanks!
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: create Powerpoint via com

2007-08-30 Thread Alan Isaac
[EMAIL PROTECTED] wrote:
 code

OK, creating bulleted lists, or tables,
or adding pictures is all straightforward.
How about chart creation (in Ppt 2003)?
I do not see how to do this with Python.

Thanks,
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fastest way to convert a byte of integer into a list

2007-07-12 Thread Alan Isaac
 On Jul 13, 9:54 am, Matimus [EMAIL PROTECTED] wrote:
num = 255
numlist = [num  i  1 for i in range(8)]
 

Godzilla wrote:
 Thanks matimus! I will look into it...
 

Watch out for the order, which might
or might not match your intent.

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bool behavior in Python 3000?

2007-07-12 Thread Alan Isaac
 Alan Isaac skrev:
http://www.python.org/dev/peps/pep-0285/


Nis Jørgensen wrote:
 You forgot to quote this bit: [4)]


Actually not.  That is a different point.
Ben seems bothered by this, but not me.
I do not mind that True+1 is 2.
I won't do it, but I do not object to it
being possible.

I do not like that True+True is 2.
I do not like that bool(False-True) is True.
I do not like that True and False are assignable,
which clearly begs for bugs to pass unseen.

 True, False = False, True
 print True, False
False True

Who can like that

I also generally agree with Steve, whose points
keep being twisted beyond recognition.

Also, tah is right about my underlying interest
in arrays of bools (and more specifically,
boolean matrices).

I think Python 3000 is the right time to reconsider
the ideal world that Guido mentions in PEP 285.

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bool behavior in Python 3000?

2007-07-11 Thread Alan Isaac
Stargaming wrote:
 I think Bjoern just wanted to point out that all those binary 
 boolean operators already work *perfectly*. 


 bool(False-True)
True

But reread Steven.

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bool behavior in Python 3000?

2007-07-11 Thread Alan Isaac
Bjoern Schliessmann wrote:
 Is there any type named bool in standard Python?

 type(True)
type 'bool'

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bool behavior in Python 3000?

2007-07-11 Thread Alan Isaac
Miles wrote:
 What boolean operation does '-' represent?

Complementation.
And as usual, a-b is to be interpreted as a+(-b).
In which case the desired behavior is
 False-True = False+(-True)=False+False = False

In response to Stargaming, Steve is making
a point about the incoherence of certain arguments,
not proposing an implementation.

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bool behavior in Python 3000?

2007-07-11 Thread Alan Isaac
Since it is seemingly ignored in most of the comments
on this thread, I just want to remind that PEP 285
http://www.python.org/dev/peps/pep-0285/
says this:

   In an ideal world, bool might be better implemented as a
   separate integer type that knows how to perform mixed-mode
   arithmetic.

I mentioned Python 3000 since that is an opportunity for an ideal world.

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


bool behavior in Python 3000?

2007-07-10 Thread Alan Isaac
Is there any discussion of having real booleans
in Python 3000?  Say something along the line
of the numpy implementation for arrays of type 'bool'?

Hoping the bool type will be fixed will be fixed,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: allow scripts to use .pth files?

2007-07-10 Thread Alan Isaac
John Machin wrote:
 I got the impression that the OP was suggesting that the interpreter
 look in the directory in which it found the script.

Right.

 
 I got the impression that the problem was that the package was not
 only not on sys.path but also not in the same directory as the script
 that wanted to import it.

Right.


 I'm curious whether you think that the OP's use of .pth was a typo,
 and whether you have read this:
 http://docs.python.org/lib/module-site.html

You seem to understand what I'm getting at.
Thanks John.

Alan Isaac (the OP above)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: disappearing documentation of `coerce`

2007-07-05 Thread Alan Isaac
 On 2007-07-05, Alan Isaac [EMAIL PROTECTED] wrote:
Once upon a time, `coerce` was documented
with the other built-ins.


Neil Cerutti wrote:
 It's now documented in Library Reference 2.2 Non-essential
 Built-in Functions.
 Apparently it is no longer needed or useful, but only kept for
 backward compatibility.


Thanks.
I now see that it is going away:
http://www.python.org/dev/peps/pep-3100/

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: allow scripts to use .pth files?

2007-07-04 Thread Alan Isaac
 On Jul 3, 7:35 am, Alan Isaac [EMAIL PROTECTED] wrote:
Suppose I have a directory `scripts`.
I'd like the scripts to have access to a package
that is not installed, i.e., it is not on sys.path.
On this list, various people have described a variety
of tricks they use, but nobody has proposed a
pretty way to allow this.
I am therefore assuming there is not one. (?)

How about allowing a `scripts.pth` file in such a `scripts`
directory, to work like a path configuration file?
(But to be used only when __name__==__main__.)
Drawbacks?



[EMAIL PROTECTED] wrote:
 import sys
 sys.path.append(../scripts)
 import Module_from_scripts_dir


That is not actually an answer to the question.  In any case,
path manipulation in scripts is often pronounced not pretty.
(And would also have to be done in every script file.)

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


disappearing documentation of `coerce`

2007-07-04 Thread Alan Isaac
Once upon a time, `coerce` was documented
with the other built-ins.
http://pydoc.org/1.5.2/__builtin__.html 
Now it is not.
http://docs.python.org/lib/built-in-funcs.html
Reason?

Thanks,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


allow scripts to use .pth files?

2007-07-03 Thread Alan Isaac
Suppose I have a directory `scripts`.
I'd like the scripts to have access to a package
that is not installed, i.e., it is not on sys.path.
On this list, various people have described a variety
of tricks they use, but nobody has proposed a 
pretty way to allow this.
I am therefore assuming there is not one. (?)

How about allowing a `scripts.pth` file in such a `scripts`
directory, to work like a path configuration file?
(But to be used only when __name__==__main__.)
Drawbacks?

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


Re: equality comparison by default (was Re: Too many 'self' in python.That's a big flaw in this language.)

2007-07-01 Thread Alan Isaac
A.T.Hofkamp wrote:
 Hmm, maybe numbers in sets are broken then?
a = 12345
b = 12345
a == b
 
 True
 
a is b
 
 False
 
set([a,b])
 
 set([12345])
 
 Numbers and my Car2 objects behave the same w.r.t. '==' and 'is', yet I get a
 set with 1 number, and a set with 2 cars.
 Something is wrong here imho.
 
 The point I intended to make was that having a default __hash__ method on
 objects give weird results that not everybody may be aware of.
 In addition, to get useful behavior of objects in sets one should override
 __hash__ anyway, so what is the point of having a default object.__hash__ ?


The point is: let us have good default behavior.
Generally, two equal numbers are two conceptual
references to the same thing.  (Say, the Platonic
form of the number.) So it is good that the hash value
is determined by the number.  Similarly for strings.
Two equal numbers or strings are **also** identical,
in the sense of having the same conceptual reference.
In contrast, two equal cars are generally not identical
in this sense.  Of course you can make them so if you wish,
but it is odd.  So *nothing* is wrong here, imo.

Btw:
 a = 12
 b = 12
 a == b
True
 a is b
True

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: guidance needed: best practice for script packaging

2007-06-28 Thread Alan Isaac
My thanks to Gabriel and Josiah.
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: equality comparison by default (was Re: Too many 'self' in python.That's a big flaw in this language.)

2007-06-28 Thread Alan Isaac
A.T.Hofkamp wrote:

a = Car2(123)
b = Car2(123)
a == b
 
 True
 
set([a,b])
 
 set([Car2(123), Car2(123)])
 
 I get a set with two equal cars, something that never happens with a set
 my math teacher once told me.


Then your math teacher misspoke.
You have two different cars in the set,
just as expected.  Use `is`.
http://docs.python.org/ref/comparisons.html

This is good behavior.

Cheers,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


guidance needed: best practice for script packaging

2007-06-26 Thread Alan Isaac
This is a simple question about actual practice.
I just want to know how you (yes you) are
approaching this problem.  

The problem:
What is the recommended packaging of
demo scripts or test scripts for a package
to be distributed to others to play with.
(I.e., without installing.)

Example:
Suppose I have the package structure:

package/
__init__.py
subpackage1/
__init__.py
moduleXX.py
subpackage2/
__init__.py
moduleYY.py

Important detail:
moduleXX uses a relative import to access moduleYY.

The goal:
I have a script test.py that I want to
distribute with the package.  This script will import
moduleXX to illustrate or test the module's use.

Is it the case that this script cannot reasonably be
bundled with `package`?  (I.e., within its directory
structure.)

Note:
If I put it in the `subpackage1` directory and
just import moduleXX, I will get
ValueError: Attempted relative import in non-package

Note:
If I put it in the `package` directory and
import subpackage1.moduleXX, I will get
ValueError: Attempted relative import beyond toplevel package

Here is one hack, based on a suggestion of Alex Martelli
http://mail.python.org/pipermail/python-list/2007-May/438250.html
and others.
- add a `scripts` subdirectory to `package`
- use path manipulation to find the directory holding `package`
- insert this directory in sys.path

This hack works.
However it has also been claimed that this approach is an
insane for any shared code.  Is it?  If so, what is best practice?

Note:
I do not want to assume the package will be installed:
a user should be able to play with it without installing it.

This is a repeat question.  The usual rule is, if you do not get an answer, you
are asking the question in an unhelpful way.  If my question is still unclear, I
would appreciate any leads on how to clarify it.

Thank you,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


relative import question: packaging scripts

2007-06-23 Thread Alan Isaac
What is the recommended packaging of
demo scripts or test scripts for a package
that has modules that use relative imports?

Example:
Suppose I have the package structure:

package/
__init__.py
subpackage1/
__init__.py
moduleY.py
subpackage2/
__init__.py
moduleZ.py

Important detail:
moduleZ uses a relative import to access moduleY.

The problem:
I have a script test.py that I want to
distribute with the package.  It will import
moduleZ to illustrate or test the module's use.

Is it the case that this script cannot reasonably be
bundled with `package`?  (I.e., within its directory
structure.)

I cannot put it in the `subpackage2` directory and
just import moduleZ, because then I will get
ValueError: Attempted relative import in non-package

I cannot put it in the `package` directory and
import subpackage2.moduleZ, because then I will get
ValueError: Attempted relative import beyond toplevel package

The script could use path manipulation to
find `package`, as suggested by Alex Martelli
http://mail.python.org/pipermail/python-list/2007-May/438250.html
and others.  However it has also been claimed that this approach is an
insane for any shared code.  Is it?

I do not want to assume the package will be installed:
a user should be able to play with it without installing it.
In this case, does the only sane thing to become to
require any user to take the step of inserting the
package location into sys.path and have
test.py rely on the user having done this?

Thank you,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python plain-text database or library that supports joins?

2007-06-22 Thread Alan Isaac
Not Python, but maybe relevant:
http://www.scriptaworks.com/cgi-bin/wiki.sh/NoSQL/HomePage

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


cannot have test scripts in packages?

2007-06-21 Thread Alan Isaac
This is really a repackaging of an earlier question,
probably illustrating that I still do not understand relative imports.

Suppose I have the package structure (taken from the example at
http://www.python.org/dev/peps/pep-0328/)

package/
__init__.py
subpackage1/
__init__.py
moduleY.py
subpackage2/
__init__.py
moduleZ.py

Important detail:
moduleZ uses a relative import to access moduleY.

The problem:
I have a script test.py that I want to
distribute with the package.  It will import
moduleZ to illustrate or test the module's use.

Is it the case that this script cannot reasonably be
bundled with `package`?  (I.e., within its directory
structure.)

I cannot put it in the `subpackage2` directory and
just import moduleZ, because then I will get
ValueError: Attempted relative import in non-package

I cannot put it in the `package` directory and
import subpackage2.moduleZ, because then I will get
ValueError: Attempted relative import beyond toplevel package

The script could then use path manipulation to
find `package`, as suggested by Alex Martelli
http://mail.python.org/pipermail/python-list/2007-May/438250.html
and others.  However it has also been claimed that this approach is an
insane for any shared code.

Btw, I do not want to assume the package will be installed:
a user should be able to play with it without installing it.

So if the package is not installed, is the only sane
thing to do is require any user to take the step of
inserting the package location into sys.path and have
test.py rely on the user having done this?

What is the recommended handling of demo or test scripts
for a package?

Thank you,
Alan Isaac


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


Re: File processing - is Python suitable?

2007-06-19 Thread Alan Isaac
ferrad wrote:
 I have large text files containing text, numbers, and junk.  I want to
 delete large chunks process other bits, etc, much like I'd do in an
 editor, but want to do it automatically.  
 Question: can I translate these types of rules into programmatical
 constructs that Python can use to process these files?

Someone can.  ;-)
However if the file is structured,
awk may be faster, since this sounds
like the kind of report generation it
was designed for.

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


Re: docs patch: dicts and sets

2007-05-20 Thread Alan Isaac
Raymond Hettinger [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Another way to put it is that the docs are sufficient
 when they say that set ordering is arbitrary.  That should be a cue to
 not have *any* expectations about the internal ordering of sets and
 dicts.

You are usually more careful.

1. Please do not conflate two issues here.
It confuses people like Richard T.

Did *anyone* who participated in the initial conversation
express an expectation that set ordering is not arbitrary?
No. Not one.

What surprised people was that this ordering
could vary between two *sequential* executions of
an *unchanged* source.

Martin dismisses this by simply asserting (on what basis?)
that anyone who was surprised lacks Python experience,
and that to address this in any way would make the
reference library assume the role of a tutorial.
Not very plausible, IMO, given the rest of the library
documentation.

2. You say it the existing docs should be a cue,
and yet they clearly did not provide enough guidance
to an ordinary user (me) and some more sophisticated users.
So the docs should be a cue to people who do not need a cue.
Do I understand you correctly?

3. Finally, please do not claim that the docs say that set ordering is
arbitrary.
At least not the docs we have benn talking about:
http://docs.python.org/lib/types-set.html
It is fascinating that you would confuse this, since it is the core
of the proposed documentation patch (although the proposed
language was indeterminate rather than arbitrary).

So it also seems you are now claiming that the patch should not be in
because of the presence of language that is in fact not there.

Look, I was just trying to help other users who might be
as surprised as I was.  As I said, I am not attached to any
language, and in fact I just used the proposals of others.
I just wanted there to be some clue for users who read the docs.
If you prefer to leave such users baffled, so be it.
My effort is exhausted.

Cheers,
Alan Isaac


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


Re: docs patch: dicts and sets

2007-05-19 Thread Alan Isaac
I submitted the language based on Bill and Carsten's proposals:

https://sourceforge.net/tracker/?func=detailatid=105470aid=1721372group_id=5470

That language has been rejected.
You many want to read the discussion and see if
acceptible language still seems discoverable.

Alan Isaac


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


docs patch: dicts and sets

2007-05-15 Thread Alan Isaac
This discussion ended abruptly, and I'd like to see it reach a
conclusion.  I will attempt to synthesize Bill and Carsten's
proposals.

There are two proposed patches.  The first is to
http://docs.python.org/lib/typesmapping.html
where it is proposed for footnote (3) to state:

Keys and values are listed in an arbitrary order.  This order is
indeterminate and generally depends on factors outside the scope
of the containing program.  However, if items(), keys(), values(),
iteritems(), iterkeys(), and itervalues() are called with no
intervening modifications to the dictionary, the lists will
directly correspond.

The second is for http://docs.python.org/lib/types-set.html
where the proposal is to append a new sentence to the 2nd paragraph:

Iteration over a set returns elements in an indeterminate
order,which generally depends on factors outside the scope of the
containing program.

Alan Isaac


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


Re: change of random state when pyc created??

2007-05-11 Thread Alan Isaac
This is an attempt to synthesize Bill and Carsten's proposals.

http://docs.python.org/lib/typesmapping.html: for footnote (3)

Keys and values are listed in an arbitrary order.  This order is
indeterminate and generally depends on factors outside the scope of
the
containing program.  However, if items(), keys(), values(),
iteritems(), iterkeys(), and itervalues() are called with no
intervening modifications to the dictionary, the lists will directly
correspond.

http://docs.python.org/lib/types-set.html: append a new sentence to 2nd par.

Iteration over a set returns elements in an indeterminate order,
which
generally depends on factors outside the scope of the containing
program.

Alan Isaac


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


docs patch: dicts and sets

2007-05-11 Thread Alan Isaac
This is an attempt to synthesize Bill and Carsten's proposals.
(I'm changing the subject line to better match the topic.)

http://docs.python.org/lib/typesmapping.html: for footnote (3)

Keys and values are listed in an arbitrary order.  This order is
indeterminate and generally depends on factors outside the scope of
the
containing program.  However, if items(), keys(), values(),
iteritems(), iterkeys(), and itervalues() are called with no
intervening modifications to the dictionary, the lists will directly
correspond.

http://docs.python.org/lib/types-set.html: append a new sentence to 2nd par.

Iteration over a set returns elements in an indeterminate
order,which
generally depends on factors outside the scope of the containing
program.

Alan Isaac



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


Re: change of random state when pyc created??

2007-05-10 Thread Alan Isaac

Carsten Haese [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 I was simply pointing out all the ways in which you made it difficult for
the
 community to explain your problem.

And without that community, I would still not have a clue.
Thanks to all!

 Please feel free to suggest specific wording changes to make the
documentation
 more useful.

I'm sure my first pass will be flawed, but here goes:

http://docs.python.org/lib/typesmapping.html:
to footnote (3), add phrase which may depend on the memory location of the
keys to get:

Keys and values are listed in an arbitrary order,
which may depend on the memory location of the keys.
This order is non-random, varies across Python implementations,
and depends on the dictionary's history of insertions and deletions.

http://docs.python.org/lib/types-set.html: append a new sentence to 2nd
paragraph

Iteration over a set returns elements in an arbitrary order,
which may depend on the memory location of the elements.

fwiw,
Alan Isaac


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


Re: change of random state when pyc created??

2007-05-10 Thread Alan Isaac
 Alan Isaac requested:
 http://docs.python.org/lib/typesmapping.html: to footnote (3), add phrase
 http://docs.python.org/lib/types-set.html: append a new sentence to 2nd
paragraph


Hamilton, William  [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Keys and values are listed in an arbitrary order.  This order is
 non-random, varies across Python implementations, and depends on the
 dictionary's history of insertions and deletions as well as factors
outside
 the scope of the containing program.

 Iteration over a set returns elements in an arbitrary order, which may
 depend on factors outside the scope of the containing program.


I think this is good and might have clued me in.
At least I'd have had a fighting chance this way.

Alan Isaac


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


Re: change of random state when pyc created??

2007-05-09 Thread Alan Isaac

Peter Otten [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Alan Isaac wrote:
 There is nothing wrong with the random module -- you get the same numbers
on
 every run. When there is no pyc-file Python uses some RAM to create it and
 therefore your GridPlayer instances are located in different memory
 locations and get different hash values. This in turn affects the order in
 which they occur when you iterate over the GridPlayer.players_played set.

Thanks!!
This also explains Steven's results.

If  I sort the set before iterating over it,
the anomaly disappears.

This means that currently the use of sets
(and, I assume, dictionaries) as iterators
compromises replicability.  Is that a fair
statement?

For me (and apparently for a few others)
this was a very subtle problem.  Is there
a warning anywhere in the docs?  Should
there be?

Thanks again!!

Alan Isaac


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


Re: change of random state when pyc created??

2007-05-09 Thread Alan Isaac
 Robert Kern wrote:
 http://docs.python.org/lib/typesmapping.html
 
 Keys and values are listed in an arbitrary order which is non-random,
varies
 across Python implementations, and depends on the dictionary's history
of
 insertions and deletions.
 

 Alan G Isaac wrote:
 Even this does not tell me that if I use a specified implementation
 that my results can vary from run to run.  That is, it still does
 not communicate that rerunning an *unchanged* program with an
 *unchanged* implementation can produce a change in results.


Robert Kern [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 The last clause does tell me that.

1. About your reading of the current language:
I believe you, of course, but can you tell me **how** it tells you that?
To be concrete, let us suppose parallel language were added to
the description of sets.  What about that language should allow
me to anticipate Peter's example (in this thread)?

2. About possibly changing the docs:
You are much more sophisticated than ordinary users.
Did this thread not demonstrate that even sophisticated users
do not see into this implication immediately?  Replicability
of results is a huge deal in some circles.  I think the docs
for sets and dicts should include a red flag: do not use
these as iterators if you want replicable results.
(Side note to Carsten: this does not require listing every little thing.)

Cheers,
Alan Isaac


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


  1   2   >