count strangeness

2011-05-22 Thread James Stroud

tal 65% python2.7
Python 2.7.1 (r271:86832, May 21 2011, 22:52:14)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type help, copyright, credits or license for more information.
py class C(object):
...   def __init__(self):
... self.data = []
...   def doit(self, count=0):
... for c in self.data:
...   count += c.doit(count)
... count += 1
... print count
... return count
...
py c = C()
py c.data.extend([C() for i in xrange(10)])
py c.doit()
1
2
4
8
16
32
64
128
256
512
1024
1024



WTF?


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


Re: count strangeness

2011-05-22 Thread James Stroud

Chris Rebert wrote:

WTF?


Assuming your question is Why is 1024 there twice?, the answer is


The question is Why is 1024 there at all? It should be 10.

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


Re: count strangeness

2011-05-22 Thread James Stroud

Peter Otten wrote:

James Stroud wrote:

WTF?


Put the code into a file, run it -- and be enlightened ;)



tal 72% python2.7 eraseme.py
1
2
4
8tal 73% cat eraseme.py
#! /usr/bin/env python

class C:
  def __init__(self):
self.data = []
  def doit(self, count=0):
for c in self.data:
  count += c.doit(count)
count += 1
print count
return count

c = C()
c.data.extend([C() for i in xrange(10)])
c.doit()
tal 74% python2.7 eraseme.py
1
2
4
8
16
32
64
128
256
512
1024


Hmmm. It's still 1024.

What am I missing?

James


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


Re: count strangeness

2011-05-22 Thread James Stroud

James Stroud wrote:

Chris Rebert wrote:

WTF?


Assuming your question is Why is 1024 there twice?, the answer is


The question is Why is 1024 there at all? It should be 10.

James


I mean 11, not 10--but you get the point.

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


Re: count strangeness

2011-05-22 Thread James Stroud

Peter Otten wrote:

James Stroud wrote:

WTF?


Put the code into a file, run it -- and be enlightened ;)


Compare the follower to the last.

tal 77% cat eraseme.py
#! /usr/bin/env python

class C:
  def __init__(self):
self.data = []
  def doit(self, count=[0]):
for c in self.data:
  c.doit()
count[0] += 1
print count[0]

c = C()
c.data.extend([C() for i in xrange(10)])
c.doit()
tal 78% python2.7 eraseme.py
1
2
3
4
5
6
7
8
9
10
11

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


Re: count strangeness

2011-05-22 Thread James Stroud

Chris Rebert wrote:

On Sat, May 21, 2011 at 11:32 PM, James Stroud jstr...@mbi.ucla.edu wrote:

Chris Rebert wrote:

WTF?

Assuming your question is Why is 1024 there twice?, the answer is

The question is Why is 1024 there at all? It should be 10.


Ah. This is why it's better to be more explicit about what your
question is than a mere WTF?.

In that case, I believe you meant to write:
count += c.doit()
rather than:
count += c.doit(count)


I get it. Thank you.

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


Re: Regex driving me crazy...

2010-04-07 Thread James Stroud

Patrick Maupin wrote:

BTW, although I find it annoying when people say don't do that when
that is a perfectly good thing to do, and although I also find it
annoying when people tell you what not to do without telling you what
*to* do, and although I find the regex solution to this problem to be
quite clean, the equivalent non-regex solution is not terrible


I propose a new way to answer questions on c.l.python that will (1) give 
respondents the pleasure of vague admonishment and (2) actually answer the 
question. The way I propose utilizes the double negative. For example:

You are doing it wrong! Don't not do codere.split('\s{2,}', s[2])/code.

Please answer this way in the future.

Thank you,
James


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


s-expression parser in python

2010-04-06 Thread James Stroud

Hello All,

I want to use an s-expression based configuration file format for a 
python program I'm writing. Does anyone have a favorite parser?


I'm currently using sexpy.parse() (http://pypi.python.org/pypi/sexpy) 
which works very well but I don't know how secure it is. Does anyone 
have experience with sexpy or another parser? I'm mostly concerned with 
 safety when the parser evaluates scalar data types. Sexpy is not well 
documented in this regard.


Also, I don't want a lisp/scheme interpreter, just the parser.

For example,

   
   (and
 (or ( uid 1000)
 (!= gid 20)
 )
 ( quota 5.0e+03)
   )
   

Should be parsed to

   [and,
 [or, [=, uid, 1405],
[!=, gid, 20]],
 [, quota, 5000.0]
   ]

Note proper parsing of ints, floats, etc.

I've already wrote the evaluator for the parsed lists, so I'm not 
interested in alternatives to S-expressions. Also, I'm really not 
interested in json, yaml, etc., for the configuration format because I'm 
quite fond of the brevity and clarity of S-expressions (your opinion may 
vary).


Thanks in advance for any insight.

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


Re: python as pen and paper substitute

2010-04-06 Thread James Stroud

Manuel Graune wrote:

Hello everyone,

I am looking for ways to use a python file as a substitute for simple
pen and paper calculations. At the moment I mainly use a combination
of triple-quoted strings, exec and print (Yes, I know it's not exactly
elegant). To clarify, I just start an editor, write a file that
might look something like this:


I wrote a module called pylave, which is meant to be a 
parser-evaluator-solver for scratch paper-like calculations. It has 
strict operator precedence, knows a variety of common operations, and 
respects parenthetic grouping. It is designed to evaluate ini files, 
which can have syntax highlighting depending on your text editor. 
Pyparsing is required.



Here is a pylave example:


% cat test.ini
factor = 2
   = minor_axis / 2
www= 69.69 + 
height = 10
apex   = epicenter // height
major_axis = epicenter + 2*radius / sin big
minor_axis = epicenter + (total / 14) % (length-1)
epicenter  = 44
radius = ((14 + length)/2 + height)*breadth
length = width + 5
width  = 2**2 - factor
total  = big*(radius + 14)
big= abs (sin (5e+8))
breadth= 2*length + height
overlap= abs (1 + 1)
nexus  = sin (5e-8)
plexus = sin (5e8)


% ./pylave.py test.ini
breadth : 24
www : 93.8350093235
nexus : 5e-08
big : 0.284704073238
major_axis : 3547.35712408
height : 10
radius : 492.0
plexus : -0.284704073238
total : 144.060261058
epicenter : 44
apex : 4
overlap : 2
 : 24.1450093235
width : 2
length : 7
factor : 2
minor_axis : 48.290018647


It is not perfect but if it will help, I'll cheeseshop it.

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


Re: Frustrated with scopes

2009-08-12 Thread James Stroud

James Stroud wrote:
 def stream_factory:
   class Line(object):
 __source = source
 __join = join
   # etc.
   return Line


of course I meant def stream_factory(lines, source, join=''.join):

James

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


Re: hashability

2009-08-12 Thread James Stroud

Asun Friere wrote:

On Aug 12, 3:32 pm, James Stroud nospamjstroudmap...@mbi.ucla.edu
wrote:


You should be more imaginative.


I'm by no means discounting that there might be some actual problem
you're trying to solve here, but I honestly can't see it.

There really is no need to get personal about this, so rather than
asking for a level of imagination from me, (which I apparently lack),
please just explain to me how {one_instance_of_a_hashable_class : val,
another_instance_of_a_hashable_class :val} is conceptually different
{one_instance_of_class_str: val, another_instance_of_class_str: val},
in terms of persistence.


Sorry for being a twit. This list used to be quite nice but some people 
on this list got pretty abrasive. I couldn't tell you weren't one of 
these abrasive people from your post. I stopped giving the benefit of 
the doubt many moons ago and promptly enter attack mode at the slightest 
hint of abrasiveness. My attitude probably exacerbates the problem--but 
it sure does make me feel better.



Anyway, I think the problem has been described better than I'm able, but 
once an object goes to the file system, one can not take its hash value 
for granted. It is not possible to rely on the ability to recreate any 
arbitrary object de-novo and use the recreation as a key in proxy for 
the original object. I'm after this je ne sais pas que l'appeler 
quality of objects to use as keys (or to generate keys) for persistent 
dictionaries. Carl Banks suggested that this quality should not be 
called hashable, so I'm calling it keyable.

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


Re: hashability

2009-08-12 Thread James Stroud

Dennis Lee Bieber wrote:

On Tue, 11 Aug 2009 17:54:36 -0700, James Stroud jstr...@mbi.ucla.edu
declaimed the following in gmane.comp.python.general:


   ...
   py {C():4}[C()]
   
   Traceback (most recent call last):
 File ipython console, line 1, in module
   type 'exceptions.KeyError': __main__.C object at 0xe21610

The basis for the exception is that the two instances do not have the 
same hash() although conceptually they might seem equal to the 
unitiated. Were I to re-design python, I'd throw an exception in this 
case because of the ill-defined behavior one might expect if a C() 
serves as a key for a dict.



A C()... How would you expect

c1 = C()
c2 = C()

{c1:4}[c2] 


to behave?



This seems like a subjective question. I think I demonstrated how it 
*does* behave, which is purely objective--and I know I can't do anything 
about that. But the subjective answer to the subjective question is that 
I would like an exception to be raised when the dictionary is 
constructed. I know an exception doesn't get thrown in the current 
incarnation of the python language. That is the objective reality of the 
situation which affronts my subjective notions of how reality should be.


That IS the equivalent of your statement -- two instances are

two distinctly different entities...


Tell that to two different machines on two different days. Then bet the 
life of yourself and your nearest and dearest family on that fact and 
see whether you really want to take a hash value for granted. If a 
property of the python language fails the bet the lives of your nearest 
and dearest on a consistent result test, I call it ill defined and, 
subjectively speaking, I prefer exceptions to be thrown--And, by damned, 
I'll throw them myself if I have to.


If it saves one life, it's worth it all.

James


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


Re: hashability

2009-08-12 Thread James Stroud

Steven D'Aprano wrote:
Well there you go -- why on earth would you prohibit None as a dictionary 
key??? That's a serious failure.



roentgen 1% python
Python 2.5 (r25:51908, Sep 20 2006, 17:36:21) 
[GCC 3.4.2] on linux2

Type help, copyright, credits or license for more information.
py hash(None)
135543872


mbi136-176 98% /usr/bin/python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin

Type help, copyright, credits or license for more information.
py hash(None)
2030240



That's why. Read the whole thread. You are one of the abrasive ones.
--
http://mail.python.org/mailman/listinfo/python-list


hashability

2009-08-11 Thread James Stroud

Hello All,

I wrote the function to test hashability of arbitrary objects. My reason 
is that the built-in python (2.5) hashing is too permissive for some 
uses. A symptom of this permissiveness comes from the ability to 
successfully hash() arbitrary objects:


  py class C(object): pass
  ...
  py {C():4}[C()]
  
  Traceback (most recent call last):
File ipython console, line 1, in module
  type 'exceptions.KeyError': __main__.C object at 0xe21610

The basis for the exception is that the two instances do not have the 
same hash() although conceptually they might seem equal to the 
unitiated. Were I to re-design python, I'd throw an exception in this 
case because of the ill-defined behavior one might expect if a C() 
serves as a key for a dict.


To prevent users of one of my libraries from falling into this and 
similar traps (which have potentially problematic consequences), I came 
up with this test for hashability:


def hashable(k):
  try:
hash(k)
  except TypeError:
good = False
  else:
good = (hasattr(k, '__hash__') and
(hasattr(k, '__eq__') or hasattr(k, '__cmp__')))
  return good

It works as I would like for most of the cases I can invent:

  py all(map(hashable, [1,1.0,,(1,2,3)]))
  True
  py any(map(hashable, [None, [1,2], {}, C(), __import__('sys')]))
  False

Can anyone think of boundary cases I might be missing with this approach?


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


Re: hashability

2009-08-11 Thread James Stroud

Carl Banks wrote:

On Aug 11, 5:54 pm, James Stroud jstr...@mbi.ucla.edu wrote:


To prevent users of one of my libraries from falling into this and
similar traps (which have potentially problematic consequences),


Even so, I would consider whether some of your users might, like me,
also find this terribly useful, and if so (probably a few will unless
they are all novices), allow them to disable or disregard this check.


I realize I left out my use. The intent of the function is to flag 
objects that will make useful keys for a persistent dictionary. The 
{C():4}[C()] example demonstrates why I want to avoid certain types of 
keys--I don't want users to do things like {C():4, C():4}, which python 
happily allows but falls apart at the level of persistence.


However, I also want to avoid isinstance() and/or type checking in order 
to allow maximal flexibility so that users don't have to work too hard 
to make their keys.



I wouldn't call the function hashable.  Class instances like C() are
hashable whether you approve or not.  Something like
deliberately_hashable would be a better name.


I chose keyable.


Can anyone think of boundary cases I might be missing with this approach?


It is possible to redefine == operator by defining __ne__ instead of
__eq__, at least on Python 2.5, so you should keep that in mind.


Thanks for this hint. I've already put it in.

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


Re: hashability

2009-08-11 Thread James Stroud

Asun Friere wrote:

On Aug 12, 12:15 pm, James Stroud jstr...@mbi.ucla.edu wrote:


I realize I left out my use. The intent of the function is to flag
objects that will make useful keys for a persistent dictionary. The
{C():4}[C()] example demonstrates why I want to avoid certain types of
keys--I don't want users to do things like {C():4, C():4}, which python
happily allows but falls apart at the level of persistence.


What am I missing here?  How, in terms of persistence, is {C():4, C():
4} conceptually different from {'spam':4, 'ham':4}?


You should be more imaginative.
--
http://mail.python.org/mailman/listinfo/python-list


Re: hashability

2009-08-11 Thread James Stroud

Asun Friere wrote:

Perhaps the best solution would be for the unitiated to correct their
misaprehensions


Can you come give a class to my users?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Frustrated with scopes

2009-08-11 Thread James Stroud

andrew cooke wrote:

Is there a way to make this work (currently scope and join are
undefined at runtime when the inner class attributes are defined):

class _StreamFactory(object):

@staticmethod
def __call__(lines, source, join=''.join):

class Line(object):

__source = source
__join = join
[...]



It would be helpful if you were to describe the type of behavior you expect.

I assume you will return the newly created Line class when you call an 
instance of _StreamFactory? There may be some things about the above 
that you might be overlooking:


1. __call__ does not supersede the __init__ constructor of _StreamFactory.
2. __source and __join are name-mangled in later versions of python. 
They will be attributes of the returned Line class named _Line_source 
and _Line_join respectively.


Rather than make _StreamFactory a class, you will probably get the 
behavior you desire if you simply make it a function:


def stream_factory:
  class Line(object):
__source = source
__join = join
  # etc.
  return Line

And then don't forget that double underscores produce name mangling and 
you'll be set.


James


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


Python quirk in evaluation order

2009-07-31 Thread James Stroud

Python 2.5:

mbi136-176 211% python
*** Pasting of code with  or ... has been enabled.

 ## ipython  ##

py b = 4 if True else b
py b
4


Isn't the right side supposed to be evaluated first?
--
http://mail.python.org/mailman/listinfo/python-list


Re: TkInter: Problem with propagation of resize events through geometry manager hierarchy?

2009-02-08 Thread James Stroud

Randy Smith wrote:

The cropping and scrolling works fine.  But when I try to add
responding to resize events, I get into trouble.  Specifically:
* When I naively change the size of the image shown to be borderwidth
  less than the size indicated in the configure event, the size of the
  image shown grows gradually but inexorably when I start the test
  app.  (Sorta scary, actually :-})
* When I fiddle a bit to figure out what the actual difference in size
  is between the Configure event and the image that can be displayed,
  I get a vibrating, jagged display of the image.

Investigation suggests that multiple configure events are hitting the
label in response to each user resize with different sizes.  I'm
guessing that when I resize the image in response to those different
events, that creates new resize events propagating through the window
manager hierarchy, which creates new configure events, which means my
handler changes the image size, which ... you get the idea.


I can't test your code because I don't have the test image and for some 
reason it does not recognize a tiff of my own. But, just glancing at 
your code, it looks like a quick-fix would be to set self.zoom to a 
sentinel at the end of refresh() and return 'break' at the top of the 
methods that use self.zoom if it is said sentinel value (e.g. if 
self.zoom == WHATEVER_SENTINEL: return 'break'). You may also want to 
return 'break' for event responders that should terminate the event 
chain. This is a general technique to stop a lot of unwanted event 
propagation.


James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: TkInter: Problem with propagation of resize events through geometry manager hierarchy?

2009-02-08 Thread James Stroud

curiouserra...@gmail.com wrote:

Thanks!  I hadn't known about the return 'break' technique.  But
I don't follow your sentinel suggestion; how would that sentinel
ever get reset?


Presumably you would set it from some kind of input. Basically, if you 
don't need to zoom, you wouldn't bother scaling the image. It is the 
scaling step that is expanding your image.


You might want to set any borders to 0.

It seems as if the first time through the event

chain it'd be set to the sentinel, and the routines that pay
attention to it would never execute.  What am I missing?
I tried simply returning 'break' at the end of refresh() and
that made no change in behavior.


You will want to return 'break' at the end of callbacks, like 
display_tag_and_size()



One thing I've been looking for (including in the source :-J) is
a description of the precise process that the geometry manager
goes through in figuring out and then setting sizes for the
various widgets (on resize or, apparently, startup).  I suspect
with that + the return 'break' technique I could get this to
work.  But I haven't been having any luck finding that documentation.


I've never found that either. I usually tweak things by trial and error. 
Tkinter (Tk) has some bad habits. Most of the time it is very hard to 
get the precise dimensions of widgets by asking them. The problem is 
exacerbated by platform specific modifications like TkAqua. Ive found 
the best approach is to tweak the math so that the sizing comes out 
consistent and then test and patch for every platform you support. 
Sometimes, if you experiment enough, you can figure out by inference 
what Tk is actually doing under the hood.


The other, more sane option, is to attempt to make guis that don't rely 
on precise size information. Its less satisfying than the pixel level 
control one hopes for, but makes for a faster development process.


Also, you might want to look into Pmw. It has scrolled canvas and 
scrolled frame widgets that might be helpful to you. There is no need to 
reinvent the wheel except for your own enlightenment.


James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is c.l.py becoming less friendly?

2009-02-07 Thread James Stroud

Tim Chase wrote:

Is this where we tell you to shut up? gdr ;-)


Don't you mean STFU?



--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: what IDE is the best to write python?

2009-02-01 Thread James Stroud

mcheun...@hotmail.com wrote:

Hi all
   what IDE is the best to write python?
thanks
from Peter (mcheun...@hotmail.com)


vim in one terminal, ipython in the other.

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: dicts,instances,containers, slotted instances, et cetera.

2009-01-29 Thread James Stroud

ocsch...@gmail.com wrote:

I can't port the entire app to be a stored database procedure.


Perhaps I underestimate what you mean by this, but you may want to look 
at pyTables (http://www.pytables.org/moin/HowToUse).



ctypes, maybe. I just find it odd that there's no quick answer on the
fastest way in Python to implement a mapping in this context.


Your explanation of where your prototype is slow is a little unclear. If 
your data is largely numerical, you may want to rethink your 
organization and use a numeric package. I did something similar and saw 
an order of magnitude speed increase by switching from python data types 
to numpy combined with careful tuning of how I managed the data.


You may have to spend more time on this than you would like, but if you 
really put some thought into it and grind at your organization, you can 
 probably get a significant performance increase.


James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: New to python, open source Mac OS X IDE?

2009-01-27 Thread James Stroud

joseph.a.mar...@gmail.com wrote:

Greetings! I've heard enough raving about Python, I'm going to see for
myself what all the praise is for!

I'm on a Mac. I use Netbeans for Java, PHP, and C if needed. Do you
even use an IDE for Python?

Any recommendations on open source Python environments?

Thanks!


Try open komodo. I haven't used it because vim is my IDE, but it looks 
pretty good.


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


Re: Start Python at client side from web app

2009-01-21 Thread James Stroud

Thomas Guettler wrote:

Hi,

I want to start Python at the client side from a web application. The
app is an intranet application, and all client PCs are under our control (we
can install software on them).

But I don't want to update the installation too often. Here is my idea:

We create a custom mime-type and register it on the client PC. The web 
application
can send signed python code to the client PC. If the signature is correct,
the code will be executed at the client. The signature prevents others from 
executing
code.

Has someone seen or done something like this before?

I can code this myself, but prefer to use some open source project, if it 
exists.


Thanks in advance,
  Thomas Güttler



You are better off using a cron job (or similar) on the client side, 
getting the client to hit the web server for the code at regular 
intervals, and if code is ready, execute. If code isn't ready, wait for 
the next interval. Use https for security and have a shared secret 
message to identify legitimate clients.


If you try to push code the other way, you will need a perpetual socket 
open on the client side, making the client the server.


James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Start Python at client side from web app

2009-01-21 Thread James Stroud

Thomas Guettler wrote:

Sorry, I described my problem not well. Here is more information:

The main application is the intranet web application used with IE (ms windows 
client).
But some action needs to be done on the client since you can't do it with html 
or javascript.

1. The user pushes a button in the web app.
2. Webserver sends signed python code to the client with own mime type
3. IE sends code to the python application.
4. Signature gets checked, Python code on the client gets executed.
5. Maybe send some data to the server with http.


You need to write an IE plugin for this, otherwise you can't get out of 
the browser sandbox to execute anything on the client side.


In fact, if you just make your IE application a plugin, you can take 
advantage of updating facilities that IE should have for plugins. If IE 
doesn't have updating facilities, then just write a firefox plugin, 
which does have these facilities.

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


Re: defining class functions

2009-01-19 Thread James Stroud

Astan Chee wrote:

Hi,
I have two classes in python that are in two different files/python 
scripts. Class A uses Class B like this:

class B(object):
   def function1(self,something):
   pass
   def function2(self,something):
   print hello one
   print something

class A(object):
   def __init__(self):
 instance = B()
 instance.function2(hello two)
 self.function3()
   def function3(self):
 print hello three

What I want to do here is to (re)define function1 from function3. Is 
that possible? Is there any way of redefining a function of another 
class without inheriting it? Does this make sense?


I know what you mean, but it doesn't make a lot of sense.

Best practice is to make the function module level, especially if self 
is not referenced beyond the function definition. If self is referenced, 
then all selves must share the same behavior if not the same heredity, 
and using a module level function is still best:


def function(quacker):
  print hello there
  quacker.quack()

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


Re: dynamic module import?

2009-01-18 Thread James Stroud

Lawson Hanson wrote:


So is there any way to get Python to import the named module
without just doing from dummy import *, because I will
not know that the user wants to use the dummy module
until run-time ... I'm trying to import control data
for different run scenarios which will be defined in
differently named Python modules which the user will
specify at run-time with a command-line option

And help with this would be most appreciated


For the sake of humanity, I must try to talk you out of this. Well, it's 
not that serious, but dynamic import confuses programs that inspect code 
like py2exe, etc. I think it is likely that you will find the day that 
you regret trying so hard to 'import *' dynamically. A more maintainable 
way is to simply map the command line argument to an import statement 
and keep your namespaces clean:


importer_module
import sys
import dummy
import bonafide
modules = {dummy : dummy, bonafide : bonafide}
module = modules[sys.argv[1]]


If you have several modules that themselves might need the conditional 
imports, simply put the import statements in a separate module (e.g. 
importer_module and do something like



another_module
from importer_module import module


If you simply don't want to import a bunch of modules, use an if/then 
statement. In any event, I advise you to not design your code or usage 
around dynamic imports using the __import__() statement or exec().


James



--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic way to handle coordinates

2009-01-17 Thread James Stroud

Pierre-Alain Dorange wrote:

What is the elegant way to handle coordinates ?
Do i need to continue using tuples or do i need to write a Point class.
I feel a point class would be nice, because i could implement operators
with it ? But i think Point class must exist allready ? 


My instinctive advice is to use a point class. My wise advice is to put 
it in a library you will maintain and reuse--or else you will find 
yourself re-writing point classes all of the time.


A Point class will also give you a place to consolidate all of those 
methods specific to manipulating points. Thanks to python magic methods, 
you can also design some nifty shortcuts to access the attributes of 
your point.


Here is a fun example for no other reason than I'm too tired to do 
anything important right now:



class Point(object):
  def __init__(self, axes):
self.axes_names = axes.split()
self.axes = dict(enumerate(self.axes_names))
for axis in self.axes_names:
  setattr(self, axis, 0.0)
  def check_axis(self, i):
if i not in self.axes:
  raise ValueError, No such axis %s % i
  def __setitem__(self, i, v):
self.check_axis(i)
setattr(self, self.axes[i], v)
  def __getitem__(self, i):
self.check_axis(i)
return getattr(self, self.axes[i])
  def __iter__(self):
return (getattr(self, i) for i in self.axes_names)
  def move(self, other):
for i, v in enumerate(other):
  self[i] += v
  def magnitude(self):
import math
return math.sqrt(sum(v**2 for v in self))


E.g.:


py p = Point('x y z')
py list(p)
[0.0, 0.0, 0.0]
py p.move([2, 3, 4])
py p.x, p.y, p.z
(2, 3, 4)
py p[0], p[1], p[2]
(2, 3, 4)
py list(p)
[2, 3, 4]
py tuple(p)
(2, 3, 4)
py p.magnitude()
5.3851648071345037
py q = Point('x y z')
py q.move([1, 2, 3])
py p.move(q)
py list(p)
[3.0, 5.0, 7.0]
py p[1] = 15.0
py list(p)
[3.0, 15.0, 7.0]


With the introduction of the second point, q, one can begin to see the 
necessity for a class factory that produces Point classes of the desired 
dimensionalities in desired coordinate systems:



def point(axes):
  class P(Point):
def __init__(self, values):
  Point.__init__(self, axes)
  self.move(values)
  return P


Note that we have have not used metaclasses--which would require messier 
code but produce classes that would potentially behave a better as super 
classes.


The point function is used as simply as it is written:

py Point3D = point('x y z')
py p = Point3D([0.0, 0.0, 0.0])
py list(p)
[0.0, 0.0, 0.0]
py q = Point3D([5.0, 6.0, 7.0])
py list(q)
[5.0, 6.0, 7.0]
py p.move(q)
py list(p)
[5.0, 6.0, 7.0]


Incidentally, the classes returned by point are heritable, etc.


class Point3D(point('x y z')):
  def __repr__(self):
return Point3D(%s) % list(self)
  def __add__(self, other):
return Point3D(sum(t) for t in zip(self, other))


py p = Point3D([8, 7, 6])
py p
Point3D([8.0, 7.0, 6.0])
py str(p)
'Point3D([8.0, 7.0, 6.0])'
py q = Point3D([5, 4, 3])
py r = p + q
py r
Point3D([13.0, 11.0, 9.0])


Now have fun with this stuff. I need to go to sleep.

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: lazy evaluation is sometimes too lazy... help please.

2009-01-16 Thread James Stroud

Ken Pu wrote:

Hi,  below is the code I thought should create two generates, it[0] =
0,1,2,3,4,5, and it[1] = 0,10,20,30,..., but they turn out to be the
same!!!

from itertools import *
itlist = [0,0]
for i in range(2):
  itlist[i] = (x+(i*10) for x in count())

print what's in the bags:
print list(islice(itlist[0], 5))
print list(islice(itlist[1], 5))

The output is:
[10, 11, 12, 13, 14]
[10, 11, 12, 13, 14]

I see what Python is doing -- lazy evaluation doesn't evaluate
(x+(i*10) for x in count()) until the end.


It doesn't evaluate it until you ask it to, which is the right behavior. 
However, when evaluated, it evaluates i also, which is the last value 
to which i was assigned, namely the integer 1. I'm going to get flamed 
pretty hard for this, but it doesn't seem to be the intuitive behavior 
to me either. However, in a purely functional language, you wouldn't be 
able to construct a list of generators in this way.


With python, you have to remember to adopt a purely functional design 
and then pray for best results. You can store generators in a list, but 
they need to be constructed properly. I can't perfectly transmogrify 
your code into functional code because I don't think making the 
particular anonymous generator you want is possible in python. However 
this is how I would make a close approximation:



from itertools import *

def make_gen(i):
  for x in count():
yield x + (i * 10)

itlist = [make_gen(i) for i in xrange(2)]

print what's in the bags:
print list(islice(itlist[0], 5))
print list(islice(itlist[1], 5))


James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: General direction

2009-01-15 Thread James Stroud

MLT wrote:

Hello all: I'm a beginner to Python (and programming altogether), and
am now looking to create a program of my own.  What I've got in mind
is a very basic pest control program that would keep track of 1) trap
findings and 2) pesticides.  My thought is to make each of these
classes.  Looking at the general outline written below, can anyone
give general impression about whether I'm somewhat going in the right
direction or if I need to approach it entirely differently?  Thanks
for any suggestions or help!

[data structures snipped]

Programs generally *do* something. Here you have described data 
structures, which just sit there being full of data. They aren't very 
active and hence are not terribly interesting.


Here are some bullet points for your consideration:

1. What will your program do?
2. Understand databases (http://tinyurl.com/6flhbz) before you start.
3. Consider PySQlite (http://trac.edgewall.org/wiki/PySqlite)
   before you think about a heavyweight database backend (once
   you realize that you will want a database for this project).

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Suggested improvements for IDLE (non-official)

2009-01-14 Thread James Stroud

r wrote:

I actually like the IDLE, but it could use a few improvements. If
anybody else has suggestions by all means post them.

1.) The text widget and the prompt() should be separated. Trying to
write a conditional in the interactive IDLE is a real PITA. Not to
mention if you copy the working code snippet to the IDLE editor window
the indention is 8 spaces instead 4 AND you've got that prompt()
stuck in there. I have a solution for the problem though.( I hope you
all are using fixed-width font)

frm |-text widget -

|
|if this == 1:

... |if that == 2:
... |#do
... |elif that ==3:
... |#do
... |else:
... |pass

|
|x = 10


Brilliantly put. This is probably the main reason that IDLE is not worth 
using. Good luck on getting this changed. Best is to find yourself 
another IDE. Try vim.



Basically you have a Listbox on the left for the prompt and a Text on
the right. Disable the Listbox highlight and key press events and now
we have a very friendly interactive IDLE! No more prompt hijacking
your snippets, and no more 8 space indention!


Excellent suggestions are a dime a dozen. Finding people to implement 
them is slightly harder. Getting said suggestions accepted into the 
python distribution is nearly impossible. Accept what you are given and 
try vim.


If you really want a cool IDE, try Leo. It had some bugs about 4 years 
ago when I tried it, but I'm sure they have been worked out now. I'm 
addicted to vim, but if I wasn't, I'd probably be using Leo.


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


Re: hlep: a text search and rename question

2009-01-12 Thread James Stroud

sensen wrote:

matter description:

when a use an tools to do the ape to flac convert, i can use the cue
file attached with ape, but the problem is the converted flac file
don't name by the title in the cue file but like Track_1.flac,
Track_2.flac ... , so i want to write a script to do this work, system
is xp sp2, the python is 2.44, i have a plone site in this computer,
that is why i want to use python to do this work.

so simplify and example

CDImage.cue, Track_1.flac, Track_2.flac, Track_3.flac, Track_4.flac
all above files in the same folder.

the cue file is just a text file like bellow:


PERFORMER Dido
TITLE Life For Rent
FILE Dido - Life for Rent.ape WAVE
  TRACK 01 AUDIO
TITLE White Flag
PERFORMER Dido
INDEX 01 00:00:00
  TRACK 02 AUDIO
TITLE Stoned
PERFORMER Dido
INDEX 00 04:00:03
INDEX 01 04:01:45
  TRACK 03 AUDIO
TITLE Life For Rent
PERFORMER Dido
INDEX 00 09:56:47
INDEX 01 09:56:53
  TRACK 04 AUDIO
TITLE Mary's In India
PERFORMER Dido
INDEX 01 13:37:57

the things i want to do

1. search the current folder cue file (only one cue file) contents.
find the TITLE White Flag and get the White Flag and maybe we
can save it to a var.
2. then rename the Track_1.flac to the White Flag.flac
3. search the next title TITLE Stoned and save ti to var.
4. then rename the Track_2.flac to the Stoned.flac.
5. do the same things to the end title.

someone can give some outline, or code is more wonderful.  thanks in
advance.


This isn't much work in python:

import os, glob, re
cue = iter(open('CDImage.cue').readlines())
titles = []
for line in cue:
  if line.strip().startswith('FILE'):
break
for line in cue:
  if line.strip().startswith('TITLE'):
titles.append(line.split('')[-2])
flacs = glob.glob('*.flac')
flacs.sort(key=lambda f: int(re.search(r'\d+', f).group(0)))
for old, new in zip(flacs, titles):
  os.rename(old, new)


There is no error checking for properly formatted cue file, etc.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: hlep: a text search and rename question

2009-01-12 Thread James Stroud

James Stroud wrote:

cue = iter(open('CDImage.cue').readlines())


It just occurred to me that this could simply be:

cue = open('CDImage.cue')

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unbinding Tkinter default bindings for Listbox

2009-01-12 Thread James Stroud

Roger wrote:

Hi Everyone,

I have a behavior associated with a default binding with Tkinter
Listbox that I want to get rid of but I can't no matter if I return
break on the binding or unbind it directly. If you have a Listbox
where the bounding box is not completely revealed in the window that
holds it and you use the mouse to drag the list box, the contents of
the listbox will move in the X direction to reveal the area that's
hidden.  After searching the internet for hours I found the behavior I
want to remove is a default binding as described here:

http://tcltk.free.fr/man/TkCmd/listbox.php3

[3] If the mouse leaves the listbox window with button 1 down, the
window scrolls away from the mouse, making information visible that
used to be off-screen on the side of the mouse. The scrolling
continues until the mouse re-enters the window, the button is
released, or the end of the listbox is reached. 

After further searching I found that the code for this in tcl is
described here:

http://www.openmash.org/lxr/source/library/listbox.tcl?c=tk8.3

 50 bind Listbox B1-Motion {
 51 set tkPriv(x) %x
 52 set tkPriv(y) %y
 53 tkListboxMotion %W [%W index @%x,%y]
 54 }

Now I've found no way to successfully unbind B1-Motion from the
listbox, as I said above.  Nor return break on receiving the event.
I do want to eventually have my own B1-Motion binding applied to the
listbox for a different reason (with add='+' if necessary).  My next
step was to see if I could just unbind B1-Motion then programmatically
delete tkListboxMotion in my code but tkListboxMotion is not available
in the Tkinter source (it's part of a compiled binary I can't reach?).

Any help would be greatly appreciated.  Thanks!
Roger.


You can directly send commands to the Tcl interpreter via the call 
method of the tk attribute of any Tkinter widget. For example:



py from Tkinter import *
py tk = Tk()
py b = Button(tk)
py b.pack()
py b.tk
tkapp object at 0xd57720
py b.tk.call('bind', 'Listbox', 'B1-Motion')
'\nset tk::Priv(x) %x\nset tk::Priv(y) %y\ntk::ListboxMotion 
%W [%W index @%x,%y]\n'

py b.tk.call('bind', 'Listbox', 'B1-Motion', _)
''


James



--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unbinding Tkinter default bindings for Listbox

2009-01-12 Thread James Stroud

James Stroud wrote:

py b.tk.call('bind', 'Listbox', 'B1-Motion', _)


You want b.tk.call('bind', 'Listbox', 'B1-Motion', ) of course.

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unbinding Tkinter default bindings for Listbox

2009-01-12 Thread James Stroud

Roger wrote:

I'm sorry for harassing the list but any suggestions would be greatly
appreciated. =)


I just checked the behavior you are describing. It seems that you want 
to unbind the Leave event to stop the autoscrolling when you leave the 
listbox. E.g.:


from Tkinter import *
tk = Tk()
lb = Listbox(tk)
lb.pack()
tk.tk.call('bind', str(lb), 'Leave', break)
for i in range(12):
  lb.insert(END, str(i))


Sorry for not reading more carefully.

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


Re: Unbinding Tkinter default bindings for Listbox

2009-01-12 Thread James Stroud

James Stroud wrote:

tk.tk.call('bind', str(lb), 'Leave', break)


Which is equivalent to

lb.bind('Leave', break)

But I checked and overriding the default behavior of Listbox does not 
work (tk.tk.call('bind', Listbox, 'Leave', break)).


So I would subclass Listbox and do the bindings in the __init__:

class MyListbox(Listbox):
  def __init__(self, *args, **kwargs):
Listbox.__init__(self, *args, **kwargs)
self.bind('Leave', break)


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


Re: Unexpected behavior with dictionary keys containment and a user-defined class

2009-01-12 Thread James Stroud

Rob Clewley wrote:

Hi, the short version of my question is: when is a dictionary's
__contains__ method behavior different to using the 'in' idiom?
(because I have an example of a difference in my code).


Never.


Longer version: I have a user-defined class with a few overrides of
special methods, particularly __eq__ and __ne__. I also have a
dictionary keyed by instances of these classes, and I'm confused about
the unexpected behavior trying to test whether an instance is in the
dictionary's keys. The instance is i and the dictionary is d. I have
been using the idiom

i in d



which I understood to be the pythonic way to test the keys, but it
doesn't work. However, when I debug my code I see the instance in the
list of keys, and in fact

i in d.keys()  and  d.keys()[10] == i

both return True. But

d.__contains__(i)
d.has_key(i)
d.keys()[10] is i

return False. I put a print statement in my class's __eq__ method and
it is being called. It tests equality of some of my class instance's
attributes. I didn't realize there was any situation where you could
expect different results from   i in d   versus   i in d.keys()   --
am I misunderstanding something?


Well, the only conclusion is that dict uses the hash of an object to 
test containment while lists use id.


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


Re: drive a desktop app from python?

2009-01-09 Thread James Stroud

Tim Arnold wrote:
Hi, I don't even know what to google for on this one. I need to drive a 
commercial desktop app (on windows xp) since the app doesn't have a batch 
interface.  It's intended to analyze one file at a time and display a 
report.


I can get the thing to write out the report an html browser, but I have 
thousands of files I need it to analyze every night.


Is there any lib or recipe(s) for doing something like this via python?


You are a little thin on details here. My only advice at this point is 
to check out os.system, which is the simplest option. From there you can 
move to the popen2 module for greater control, but if you are talking 
about a gui app, you may run into hang-ups.


A quick recipe would be the following pretend application that 
recursively descends from the current directory calling the utility 
named dosapp on every file it finds that ends with jpg (what 
dosapp does is left to your imagination):




import os

def doit(suffix, adir, filenames):
  for afile in filenames:
if afile.endswith(suffix):
  pathname = os.path.join(adir, afile)
  os.system('dosapp %s' % pathname)


top = '.'
suffix = '.jpg'

os.path.walk(top, doit, suffix)


Read the docs on os.path.walk, of course.

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Oh! The people of all over the world!

2009-01-07 Thread James Stroud

Obaid R. wrote:

So an illegal occupation is NOT a provocation but fighting the
occupation IS? By which scales do you judge?

I would argue that a person following such backwards logic and who
sees these pictures[1] and STILL claims that showing sympathy is
difficult has a serious problem. After all the logic is simple: for
every action (read occupation) there is a reaction (read resistance)
equal to it in force and opposite to it in direction.

You can of course argue that the reaction is the cause of the action,
but that is clearly illogical and proves your iniquity in this matter
and your bias against the victim. It might take a humanitarian
catastrophe of sizable proportions and a biased person would still not
feel any sympathy. Not that there is no catastrophe, but rather
because bias is by nature practiced in spite of good reason and
available evidence, not because of it.



I'm much more apt to listen now that you aren't invoking references to 
religion at every sentence. If Israel is guilty of inhumanity, then it 
is more appropriate to appeal to one's humanity.


By the way, its obvious to most rational people that the Israeli 
reaction is overwrought and likely morally unjust--but I can't stand 
antisemitism just like I can't stand hatred of Islam just like I can't 
stand hatred of Christians.


James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to store passwords?

2009-01-07 Thread James Stroud

Oltmans wrote:

I'm writing a program in which I will ask users to enter user name and
password once only. It's a console based program that will run on
Windows XP. Actually, I'm trying to provide the similar functionality
as Remember me thing in browsers. For that, I will need to store
user name and passwords on the disk. I don't have a background in
Crypto so how do you suggest I do that? What algorithms shall I be
using? Moreover, I cannot use a whole library to do that due to
certain issues. However, I can use like 1--2 files that will be
shipped along with the main script. Any ideas? Any help will be really
appreciated. Thanks.


There is a pure python implementation of blowfish out there. Google will 
help you. I can't remember which, if any, types of block chaining it 
supports. In some cases, it is important to use a block chaining 
protocol, but for passwords with high entropy (ie good passwords), block 
chaining is not really necessary.


256 bit Blowfish or AES are adequate for storage of sensitive passwords. 
You would be well advised to read a manual like Schneier before you use 
cryptography for sensitive applications. Pitfalls exist even when you 
use a strong algorithm and think you know what you are doing. Stay away 
from stream ciphers. They are easy to screw up.


Don't attempt to use DES, etc., for this either, they are not secure 
enough. Don't pretend that you can invent your own cipher either just in 
case the thought might cross your mind. Google adacrypt for some 
hilarity in this area.


If you check out sf.passerby.net and download the source, you will see a 
pure python module in there called jenncrypt which can help with 
buffering and has minimal fileIO type emulation for block ciphers, which 
you will appreciate when you try to use your block cipher for plaintexts 
of irregular sizes.


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


Re: How to store passwords?

2009-01-07 Thread James Stroud

James Stroud wrote:

Oltmans wrote:

I'm writing a program in which I will ask users to enter user name and
password once only. It's a console based program that will run on
Windows XP. Actually, I'm trying to provide the similar functionality
as Remember me thing in browsers. For that, I will need to store
user name and passwords on the disk. I don't have a background in
Crypto so how do you suggest I do that? What algorithms shall I be
using? Moreover, I cannot use a whole library to do that due to
certain issues. However, I can use like 1--2 files that will be
shipped along with the main script. Any ideas? Any help will be really
appreciated. Thanks.


There is a pure python implementation of blowfish out there. Google will 
help you. I can't remember which, if any, types of block chaining it 
supports. In some cases, it is important to use a block chaining 
protocol, but for passwords with high entropy (ie good passwords), block 
chaining is not really necessary.


256 bit Blowfish or AES are adequate for storage of sensitive passwords. 
You would be well advised to read a manual like Schneier before you use 
cryptography for sensitive applications. Pitfalls exist even when you 
use a strong algorithm and think you know what you are doing. Stay away 
from stream ciphers. They are easy to screw up.


Don't attempt to use DES, etc., for this either, they are not secure 
enough. Don't pretend that you can invent your own cipher either just in 
case the thought might cross your mind. Google adacrypt for some 
hilarity in this area.


If you check out sf.passerby.net and download the source, you will see a 
pure python module in there called jenncrypt which can help with 
buffering and has minimal fileIO type emulation for block ciphers, which 
you will appreciate when you try to use your block cipher for plaintexts 
of irregular sizes.


James


Before anyone jumps me, I just realized the point is authentication. Use 
a hash as others have suggested.


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


Re: How to store passwords?

2009-01-07 Thread James Stroud

James Stroud wrote:
If you check out sf.passerby.net and download the source, you will see a 


passerby.sf.net

Shuffle things I did.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regular Expressions...

2009-01-07 Thread James Stroud

Ken D'Ambrosio wrote:

Hi, all.  As a recovering Perl guy,

[snip]

In Perl, I'd so something like
m/^1(...)(...)/;


Indeed it seems you are recovering from an especially bad case. I 
recommend two doses of the python cookbook per day for one to two 
months. Report back here after your first cycle and we'll tell you how 
you are doing. I'm very optimistic about the prognosis.


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


Re: Is it ok to type check a boolean argument?

2009-01-07 Thread James Stroud

Adal Chiriliuc wrote:

Hello,

Me and my colleagues are having an discussion about the best way to
code a function (more Pythonic).

Here is the offending function:

def find(field, order):
if not isinstance(order, bool):
raise ValueError(order must be a bool)
order_by = asc if order else desc
return _find(field + + + order_by)

We are not sure what's the best practice here. Should we or should we
not check the type of the order variable, which should be a bool?


None of the above. You have put unnecessary constraints on find by 
requiring a certain data type.


def find(field, order_by='desc'):
  return _find(field + + + order_by)

Here we are deferring error checking to the _find function. Otherwise:

def find(field, order_by='desc'):
  if order_by not in ['asc', 'desc']:
raise ValueError, 'Bad order_by parameter.'
  else:
return _find(field + + + order_by)

Now you have achieved the exact same affect without any sort of explicit 
type checking. Moreover, you have eliminated an if statement. Also, 
you leave room for exotic orderings should you ever want them.


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


Re: Is it ok to type check a boolean argument?

2009-01-07 Thread James Stroud

Ben Finney wrote:

Why are people so reluctant to make error message templates clearer
with named placeholders?


Because they can never remember they even exist. Thanks for the reminder.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Generator metadata/attributes

2009-01-07 Thread James Stroud

acooke@gmail.com wrote:

Hi,

(I searched back and found some previous discussion on generator
attributes that seemed to be related to callable generators - this is
NOT that, as far as I can tell)

I want to associate some data with a generator.  This is in a
decorator function, as it happens; the generator is being returned and
I want to add some info that is useful for debugging later.  I have
tried modifying both .__doc__ and an arbitrary attribute, but the
first is read-only, and the second does not exist (ie you cant just
add one by writing to it; same error with setattr).

I realise I could create my own wrapper that implements __next__ 


That is what you are going to have to do. Immutable python builtins work 
 that way:


py i = 4
py i.bob = 2

Traceback (most recent call last):
  File ipython console, line 1, in module
type 'exceptions.AttributeError': 'int' object has no attribute 'bob'

py i.__doc__ = 'I am an int'

Traceback (most recent call last):
  File ipython console, line 1, in module
type 'exceptions.AttributeError': 'int' object attribute '__doc__' is 
read-only



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


Re: What would you like to see in a book about Matplotlib?

2009-01-06 Thread James Stroud

Sandro Tosi wrote:

Hello and Happy 2009!

I received the interesting proposal to author a book on Matplotlib,
the powerful 2D plotting library for Python.

While preparing the arguments list, I'd like to hear even your
opinion, because different points-of-view will lead to a better
product.

Some basic question I'd like to ask are:

- what are you using matplotlib for?


Matplot lib shines for interactive environments wherein graphs and 
charts must be produced. I have incorporated it into a simple program 
for analysis of fluorometric data: http://fluorit.bravais.net/


(The forthcoming version 1.0 is a quantum improvement in speed and 
function over the SLOW *prototype* version 0.9 currently available--see 
the wish list, feature requests, and known issues for details. Version 
1.0 is very fast.)



- what are the things you like the most of matplotlib, that you want
to give emphasis to? And why?


The ability to embed a figure (composed of subplots) into a custom 
window is my favorite aspect. Also: massive configurability of plots, 
event driven plot canvas, antigrain geometry antialiasing are also all 
very important. Additionally the toolbar of standard interactive 
functions and the ability to export plots in a variety of formats are 
also invaluable features.



- what are the (basic) things that, when you were beginning to use
matplotlib, you wanted to see grouped up but couldn't find?


The documentation for basic plot configuration, especially for embedded 
plots, and also tapping into the event model is very weak currently. 
These need the most improvement.



- what would you like to see in a book about matplotlib?


A big, huge, gigantic, chapter---no make that 3 chapters--on programming 
 interactivity with the plot canvas.



- what are some those advanced feature that made you yell WOW!! ?


I listed them above, but my most WOW moment was when I saw the Tkinter 
backend doing beautiful antialiasing. I was also very happy to 
seamlessly add new functionality to the toolbar.



- what are the things you'd like to explore of matplotlib and never
had time to do?


I want to program a lot of interactivity with the plots in my current 
application.



Your suggestions are really appreciated :) And wish me good luck!


Good luck! I can't wait to read your book!


James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: __builtin__ quote

2009-01-06 Thread James Stroud

Steven Woody wrote:

Hi,

I am a new leaner and I get a question:  abs() is a member of
__builtin__ module, but why should I use abs() rather than
__builtin__.abs() ? Thanks.


It saves typing.

This might help your understanding:

py import __builtin__
py __builtin__.abs is abs
True


James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: __builtin__ quote

2009-01-06 Thread James Stroud

Steven Woody wrote:

On Tue, Jan 6, 2009 at 4:42 PM, James Stroud jstr...@mbi.ucla.edu wrote:

py import __builtin__
py __builtin__.abs is abs
   True


Does that mean someone did 'import * from __builtin__'  when python startup?


In terms of the exact implementation of the cPython interpreter, I don't 
know. But the interpreter behaves as if someone did just that. So there 
is nothing wrong with thinking of it this way if it helps you understand 
the interpreter.


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: subclassing 'list'

2009-01-06 Thread James Stroud

akineko wrote:

Hello everyone,

I'm creating a class which is subclassed from list (Bulit-in type).

It works great.
However, I'm having a hard time finding a way to set a new value to
the object (within the class).
There are methods that alter a part of the object (ex. __setitem__()).
But I couldn't find any method that can replace the value of the
object.
I wanted to do something like the following:

class Mylist(list):

def arrange(self):
new_value = 
list.self.__assign__.(self, new_value)

I serached the newsgroup and found that assignment operator ('=')
cannot be overridden because it is not an operator. But it shouldn't
stop Python to provide a way to re-assign the value internally.

Any suggestions will be highly appreciated.

Best regards,
Aki-


Slice assignment will behave as you like:

py class Mylist(list):
... def arrange(self):
... new_value = [1, 2, 3]
... self[:] = new_value
...
py m = Mylist()
py m.arrange()
py m
[1, 2, 3]

Your other option is to arrange the list in-place, depending on what it 
means to arrange the list. You may want to provide a more explicit example.

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


Re: assignment with [:]

2008-12-28 Thread James Stroud

Ben Bush wrote:

On Dec 26, 4:46 pm, Tim Chase python.l...@tim.thechases.com wrote:

What does *not* work is
3 * [0,1,2]
As you know, this gives
[0,1,2,0,1,2,0,1,2]
What I am hoping for is
[0,3,6]
I see that I can use
numpy.multiply(3,range(3))
but this seems overkill to me.  Can you tell I am coming to Python from
Matlab?

The common way to do this is just

  a1 = [0,1,2]
  a2 = [x * 3 for x in a1]

or, if you need a1 to be done in place:

  a1[:] = [x*3 for x in a1]


There is some subtlety here. The latter says to empty the list assigned 
to the name a1 and refill it with the products. Other references to 
the same list will now reflect this operation. This procedure is not 
equivalent to reassigning the name a1. For example:


py a = [1, 2, 3]
py a1 = a
py a1[:] = [x*3 for x in a1]
py a1
[3, 6, 9]
py a1
[3, 6, 9]

Whereas:

py a = [1, 2, 3]
py a1 = a
py a1
[1, 2, 3]
py a1 = [x*3 for x in a1]
py a1
[3, 6, 9]
py a
[1, 2, 3]

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: assignment with [:]

2008-12-28 Thread James Stroud

James Stroud wrote:

py a = [1, 2, 3]
py a1 = a
py a1[:] = [x*3 for x in a1]
py a1
[3, 6, 9]
py a1
[3, 6, 9]



This should have been:

py a = [1, 2, 3]
py a1 = a
py a1[:] = [x*3 for x in a1]
py a
[3, 6, 9]
py a1
[3, 6, 9]


James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a function to remove escape characters from a string ?

2008-12-25 Thread James Stroud

Stef Mientki wrote:

hello,

Is there a function to remove escape characters from a string ?
(preferable all escape characters except \n).

thanks,
Stef



import string

WANTED = string.printable[:-5] + \n

def descape(s, w=WANTED):
  return .join(c for c in s if c in w)


James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multi-dimension list

2008-12-24 Thread James Stroud

Steven Woody wrote:

Hi,

In the book Python Essential Reference, Chapter 3, when talking about
extended slicing, it gives an example:  a = m[0:10, 3:20].  But I
don't understand how the 'm' was defined.  What should it looks like?


m could be an instance of the Krayzee class.

py class Krayzee(object):
...   def __getitem__(self, i):
... try:
...   r = ['WTF?' for j in i]
... except:
...   r = 'WTF?'
... return r
...
py m = Krayzee()
py m[1:2:3, 4:5:6]
['WTF?', 'WTF?']
py m['your moms']
['WTF?', 'WTF?', 'WTF?', 'WTF?', 'WTF?', 'WTF?', 'WTF?', 'WTF?', 'WTF?']


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Strategy for determing difference between 2 very large dictionaries

2008-12-24 Thread James Stroud

Marc 'BlackJack' Rintsch wrote:

On Wed, 24 Dec 2008 03:23:00 -0500, python wrote:


Hi Gabriel,

Thank you very much for your feedback!


k1 = set(dict1.iterkeys())

I noticed you suggested .iterkeys() vs. .keys(). Is there any advantage
to using an iterator vs. a list as the basis for creating a set? I
understand that an iterator makes sense if you're working with a large
set of items one at a time, but if you're creating a non-filtered
collection, I don't see the advantage of using an iterator or a list.
I'm sure I'm missing a subtle point here :)


`keys()` creates a list in memory, `iterkeys()` does not.  With
``set(dict.keys())`` there is a point in time where the dictionary, the 
list, and the set co-exist in memory.  With ``set(dict.iterkeys())`` only 
the set and the dictionary exist in memory.


For the purpose of perpetuating the annoying pedantry that has made 
usenet great:



http://docs.python.org/dev/3.0/whatsnew/3.0.html#views-and-iterators-instead-of-lists



James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: On Whose Desktop

2008-12-23 Thread James Stroud

Steve Holden wrote:

Thanks to Barry Warsaw the On Your Desktop blog now has a new entry:


Note the thumb trackball. It's the shiznizzle. Give one two weeks of 
your patience and you'll seriously consider having it implanted in your 
thigh so you don't have to worry about missing it when you travel.

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


Re: alt.possessive.its.has.no.apostrophe

2008-12-15 Thread James Stroud

Ben Finney wrote:

James Stroud jstr...@mbi.ucla.edu writes:


Ben Finney wrote:

James Stroud jstr...@mbi.ucla.edu writes:


Yes. I think it was the British who decided that the apostrophe
rule for it would be reversed from normal usage relative to
just about every other noun.


It also seems an indefensible claim to say that anyone “decided” it
would be that way, especially “the British”.


Remember that “it” is a pronoun. I see no reversal:

Ok. Pronouns are reversed.


Or, more generally: Pronouns, which are different in just about every
other way from other nouns, are different in this way also. Is that
about right?



Can we start talking about python again?

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help improving number guessing game

2008-12-15 Thread James Stroud

feba wrote:


I don't see the aim of your changes to setup(). I can kinda understand
checking to make sure that you didn't make the minimum higher than the
maximum, but I think where you put minr/maxr would make it use the
same minr/maxr as the end of the previous game, wouldn't it?


No. Each function call creates its own name space. The function as Bruno 
has written it will have default values (mini  maxi). A call to this 
function can set different values optionally:



setup(2)  #== range from 2 to 99
setup(maxi=101)   #== range from 1 to 101
setup(5, 10)  #== range from 5 to 10
setup(10, 5)  #== throws an error


Minor point for a short program, but still good practice : use
constants. IE :


I had done this initially, but it seemed wasteful and needlessly
confusing in this situation.


No. Tracking down and changing hard-coded values within a module is 
wasteful and needlessly confusing. Creating well named and well 
documented module level constants is good style and will make your life 
easier in the long run.



I assume 'p1sc' means player_1_score ?
If so, you may want to use a dict for scores:


I don't really understand dicts yet; actually, the tutorial I'm
following (http://www.briggs.net.nz/log/writing/snake-wrangling-for-
kids/ , designed for tweens, but other than the pointless anecdote and
joke here and there, I've found it a very good guide) doesn't even
seem to mention them, from a search of the pdf. Actually, apparently I
stopped and started working on this just before the chapter on
functions and modules.


scores = {'player 1' : 0, 'player 2' : 0 }
scores['player 2'] = 10#== now player 2's score is 10
scores['player 1'] += 1#== now player 1's score is 1
print scores['player 0']   #== prints 0
print scores['player 2'] * 2   #== prints 20

If you get that, you'll have about all you need to know about dicts to 
use them for keeping track of scores (and other values) in your game.



I'll look into that later on, but for now I'm pretty happy with how it
works.


Try it sooner rather than later. I didn't get this kind of advice when I 
was first learning. It would have shaved months from my learning curve.




You should either put this in it's own function (could be named 'main'),
or at least protect it with an if __name__ == '__main__': test.


Could you go into a bit more detail on this? I don't understand what
should be its own function, nor do I understand what that line would
do or how to use it.


The idea is that everything you write is reusable and can be imported as 
a module. Upon importing a module, it's code is executed. So, as 
written, if someone imports it as a library module, they will start 
playing the game. Wrapping in the if __name__ == '__main__': test 
prevents the main loop of the game from executing on import. Only when 
the module is __main__ will that test evaluate to true and its 
commands execute.


For small programs like you have here, I'd do it like this (again 
combining ideas):


def num_players(game, prompt='1 or 2 Players?\n '):
  while True:
num = input(prompt)
try:
  num = int(num)
except ValueError:
  print Bad Value
else:
  break
  game['pnum'] = num

def main(game=None):
  if game is None:
game = {}
  print(WELCOME TO THE SUPER NUMBER GUESSING GAME!)
  num_players(game)
  game['play'] = 1
  # P1 goes first
  game['player'] = P1
  #Scores, keep track of times player guessed first.
  game['p1sc'], game['p2sc'] = 0, 0
  setup(game)

  while game['play'] == 1:
guessing(game)

if __name__ == __main__:
  main()


I just threw a little advanced stuff at you. But you would be doing 
yourself a huge favor if you struggled to understand it. The idea is 
that since game is a dict, you can modify game inside of the functions, 
and all you do is pass the game around to functions that need the values 
of its contents.


For example:

def setup(game):
game['a'], game['b'] = 1, 99
game['target'] = random.randint(a, b)

def playagain(game):
playover = input(PLAY AGAIN? Y/N: )
if playover.strip().lower() == y:
game['play'] = 1
setup(game)
else:
print(GOOD BYE. PLAY AGAIN SOON!)
quit()


Notice that I just made return values obsolete.

You will also notice that management of all of your values now becomes 
more tractable using the mutability of dict (game). Long, ugly lines 
now become simple function calls. See if you can understand what I'm 
doing here and try to propagate the idea through your game using the 
game dict.


If you can do it, you'll be at least 80% of the way to understanding 
object oriented programming, by the way.


James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Optimizing methods away or not?

2008-12-14 Thread James Stroud

Steven D'Aprano wrote:

class Parrot:
def __init__(self, *args):
print Initialising instance...
if __debug__:
self.verify()  # check internal program state, not args
if __debug__:
def verify(self):
print Verifying...


+1 It looks good to me and the intent is much clearer than the other.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help improving number guessing game

2008-12-14 Thread James Stroud
It looks much better. But as Bruno suggests and as I alluded to earlier, 
you should get in the habit of forming verification loops on input 
channels that aren't already guaranteed to provide valid input messages. 
I'm not sure how to say that in English, but in python my example was:


while True:
  try:
guess1 = int(input(\n ))
  except ValueError:
print 'Bad value.'
  else:
break


Other than that, you still have some points where you can break lines 
down to improve clarity. Think of it like trying to avoid run-on sentences.


Over all, it looks like you have picked up some good technique that you 
could only get by experimentation over the course of about 5 or 6 months.


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


Re: Looking for the best way to translate an idiom

2008-12-14 Thread James Stroud

Paul Moore wrote:

I'm translating some code from another language (Lua) which has
multiple function return values. So, In Lua, it's possible to define a
function

function f()
return 1,2,3
end

which returns 3 values. These can then be used/assigned by the caller:

a,b,c = f()

So far, much like Python, but the key difference is that in Lua,
excess arguments are ignored - so you can do


py class mytuple(tuple):
  def magic(self, astr):
names = astr.split()
for name, val in zip(names, self):
  globals()[name] = val
...
py t = mytuple((1,2,3))
py t.magic('a b')
py a
1
py b
2

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


Re: Looking for the best way to translate an idiom

2008-12-14 Thread James Stroud

James Stroud wrote:

py class mytuple(tuple):
  def magic(self, astr):
names = astr.split()
for name, val in zip(names, self):
  globals()[name] = val
...
py t = mytuple((1,2,3))
py t.magic('a b')
py a
1
py b
2

James



In case its not obvious:

def f():
  return mytuple((1,2,3))

f().magic('a b')

You can parameterize lobbing off values, or use more magic:

py class mytuple(tuple):
...   def magic(self, astr):
... names = astr.split()
... for name, val in zip(names, self):
...   globals()[name] = val
...   def __add__(self, other):
... if isinstance(other, tuple):
...   return mytuple(tuple.__add__(self, other))
... else:
...   return mytuple(self[other:])
...
py t = mytuple((1,2,3))
py t + 1
(2, 3)
py def f():
  return mytuple((1,2,3))
...
py (f() + 1).magic('a b')
py a
2
py b
3

It's not as bad as a lot of the cynics are about to tell you it is. If 
it has any badness at all, it's because of the need (according to what I 
infer from your specification) to use the global namespace. If you want 
to modify a more local namespace, you can use some stack frame 
inspection to do some real magic:


py import inspect
py class mytuple(tuple):
  def magic(self, astr):
names = astr.split()
for name, val in zip(names, self):
  inspect.stack()[1][0].f_locals[name] = val
  def __add__(self, other):
if isinstance(other, tuple):
  return mytuple(tuple.__add__(self, other))
else:
  return mytuple(self[other:])
...
py def f():
  return mytuple((6,7,8))
...
py (f() + 1).magic('a b')
py a
7
py b
8

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


Re: Looking for the best way to translate an idiom

2008-12-14 Thread James Stroud

drobi...@gmail.com wrote:


I'm baffled by this discussion.
What's wrong with
   a, dontcare, dontcare2 = f()
   a = a + 1

 Simple, clear, and correct.


1. This can't apply to a generalized f() that may return an arbitrary 
number of arguments = len(num_assignments_you_care_about).
2. The example chosen was misleading. You don't want to add 1 to the 
first element of the tuple, you want to move the start of the returned 
tuple up 1 and start assignment on the left from there.


James

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


Re: Looking for the best way to translate an idiom

2008-12-14 Thread James Stroud

James Stroud wrote:

  inspect.stack()[1][0].f_locals[name] = val


I just double checked this. Because of the way locals are implemented in 
cPython, this won't have the desired affect.


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


Re: Looking for the best way to translate an idiom

2008-12-14 Thread James Stroud

Aahz wrote:

In article gi4834$la...@zinnia.noc.ucla.edu,
James Stroud  jstr...@mbi.ucla.edu wrote:

In case its not obvious:


Ah, so that's where Bruno's extra apostrophe came from!  ;-)


(Sorry about the spelling flame, but seeing three posts in quick
succession with incorrect spelling of its/it's pushed me into making a
public comment.)


Yes. I think it was the British who decided that the apostrophe rule for 
it would be reversed from normal usage relative to just about every 
other noun. I'm not sure the purpose--maybe it was to give compulsive 
proofreaders a raison d'etre.

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


Re: alt.possessive.its.has.no.apostrophe

2008-12-14 Thread James Stroud

Ben Finney wrote:

James Stroud jstr...@mbi.ucla.edu writes:


Yes. I think it was the British who decided that the apostrophe rule
for it would be reversed from normal usage relative to just about
every other noun.


Remember that “it” is a pronoun. I see no reversal:


Ok. Pronouns are reversed.

Bob's
Its
--
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for the best way to translate an idiom

2008-12-14 Thread James Stroud

I V wrote:

On Sun, 14 Dec 2008 21:08:33 -0800, James Stroud wrote:

Yes. I think it was the British who decided that the apostrophe rule for
it would be reversed from normal usage relative to just about every
other noun. I'm not sure the purpose--maybe it was to give compulsive
proofreaders a raison d'etre.


It's because it is a pronoun; you don't put an apostrophe in his, 
either.


Its called humor.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help improving number guessing game

2008-12-13 Thread James Stroud
, but 
these inefficiencies build up in larger applications. This isn't 
premature optimization, but simply good coding style [ed: put in to 
fend off the premature optimization is bad puppets who infest this list].


Notice that the sequence of commands follows a logical sequence 
corresponding to decisions in the game. It makes the code more sensible, 
readable, and a little faster some times to boot. Good logic has the 
happy consequence of good speed most of the time.



My code is typed real-time so is untested. No promise that there aren't 
typos but you will probably get the idea. But see if you can plug the 
these functions into the proper places and apply some of these techniques.


James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help improving number guessing game

2008-12-13 Thread James Stroud

James Stroud wrote:
1. Refactor. You should look at your code and see where you repeat the 
same or similar patterns, see where they differ, make functions, and 
make the differences parameters to the function call:


def guess(player, p1score, p2score):
  guess1 = int(input(\n ))
  if guess1  100:
print(ONLY NUMBERS FROM 1 TO 99)
  elif guess1  target:
print(TOO HIGH)
  elif guess1 == target:
print(GOOD JOB, PLAYER %s! THE SCORE IS: % player)
print(P1: %s --- P2: %s  % (p1score, p2score)))
print(PLAY AGAIN?)
#Set up the game again
play = int(input(0 TO END: ))
if play == 0:
  print(GOOD BYE. PLAY AGAIN SOON!)
  quit()
else:
  target = random.randint(1, 99)
  else:
print(TOO LOW)


I realized this has a bug. The target is locked in the scope of the 
function. I wouldn't use global, though:


def guess(player, p1score, p2score):
  target = None
  guess1 = int(input(\n ))
  if guess1  100:
print(ONLY NUMBERS FROM 1 TO 99)
  elif guess1  target:
print(TOO HIGH)
  elif guess1 == target:
print(GOOD JOB, PLAYER %s! THE SCORE IS: % player)
print(P1: %s --- P2: %s  % (p1score, p2score)))
print(PLAY AGAIN?)
#Set up the game again
play = int(input(0 TO END: ))
if play == 0:
  print(GOOD BYE. PLAY AGAIN SOON!)
  quit()
else:
  target = random.randint(1, 99)
  else:
print(TOO LOW)

Use it like this:

new_target = gues(player, p1score, p2score)
if new_target is not None:
  target = new_target

I officially declare that I can't guarantee no more bugs in my previous 
post. I just fixed this one because my conscience was bothering me.


James



--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help improving number guessing game

2008-12-13 Thread James Stroud

I forgot to return target:


def guess(player, p1score, p2score):
  target = None
  guess1 = int(input(\n ))
  if guess1  100:
print(ONLY NUMBERS FROM 1 TO 99)
  elif guess1  target:
print(TOO HIGH)
  elif guess1 == target:
print(GOOD JOB, PLAYER %s! THE SCORE IS: % player)
print(P1: %s --- P2: %s  % (p1score, p2score)))
print(PLAY AGAIN?)
#Set up the game again
play = int(input(0 TO END: ))
if play == 0:
  print(GOOD BYE. PLAY AGAIN SOON!)
  quit()
else:
  target = random.randint(1, 99)
  else:
print(TOO LOW)
  return target

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help improving number guessing game

2008-12-13 Thread James Stroud

feba wrote:

This is what I have so far. better? worse?


Much better. I didn't check if it works. But you need to figure out a 
way to give up on your reliance on global variables. They will end up 
stifling you in the long run when you move to substantial projects.


Also, you should start moving the nested if: then: blocks to functions.

Finally, you should limit your lines to no more than 80 chars. I do 71. 
Don't be afraid to use transient variables to help.


Be assured that it takes on special intelligence to write unintelligible 
code at will. Also be assured that fitting everything on one line is not 
a good way to save time.


Aim for clarity.

If you want to start off on the right foot, you'll grind on your 
guessing game until all variables are insulated within local scope and 
the logic of the program becomes apparent from its architecture as a 
collection of functions.


This is an example of how to combine these concepts:


def update_scores(player, p1sc, p2sc):
  if player == P1:
plsc +=1
  else:
p2sc +=1
  tmplt = CONGRATULATIONS %s! SCORE -- P1:%s P2:%s
  print(tmplt % (player, p1sc, p2sc))
  return p1sc, p2sc

p1sc, p2sc = update_scores(player, p1sc, p2sc)


Finally, put comments on their own lines. Google python style guide.

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


Re: Need help improving number guessing game

2008-12-13 Thread James Stroud

James Stroud wrote:
Be assured that it takes on special intelligence to write unintelligible 


I meant *no* special intelligence.
--
http://mail.python.org/mailman/listinfo/python-list


Re: filter iterable based on predicate take from another iterable

2008-12-10 Thread James Stroud

Peter Otten wrote:

[EMAIL PROTECTED] wrote:


is there is a neat way to select items from an iterable based on
predicates stored in another iterable without zipping? I can do
something like this:

import itertools
foo = range(10)
# select even numbers
bar = map(lambda i: i%2, foo)
foobarselected = itertools.ifilterfalse(lambda t: t[0], itertools.izip
(bar,foo))
# for simplicity I want to work with the single item list, not the
zipped one
fooselected = list(t[1] for t in foobarselected)

However, it would be nice to have a function combining the last two
instructions. Something like
itertools.ifilterother(bar, foo) - yield iterator with items from foo
where bar is true


I think it's a good approach to keep the number of primitives low. I find
the list comprehension combined with izip() quite readable:

[v for f, v in izip(bar, foo) if not f(v)]

Peter


If you want an iterator without requiring making a list, you can simply 
use parentheses instead of brackets:


agenerator = (v for f, v in izip(bar, foo) if not f(v))

This is perfectly lazy but not immune to problems when foo or bar is 
changed before the generator is fully consumed.


James



--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass out the result from iterated function

2008-12-10 Thread James Stroud

JD wrote:

I got a iterated function like this:

def iterSomething(list):
has_something = False
for cell in list:
if something in cell:
has_something = True
output = something
   if has_something:
   iterSomething(output)
   else:
   final_out = outupt

The problem is how can I read this final_out outside of the function.
I tried the global statement, it seems not work. Any idea?

JD


I don't think this function will iterate the way you intend. You 
probably won't get many helpful suggestions until you define more 
precisely the nature of alist. Right now the function assumes single 
item lists only and will fail with an exception if something is not 
contained in the deepest list. E.g., only this type of situation is allowed:


 [[somethign]]

or

 [[[something]]]

or

 something

etc.

Anything else gives an exception.

For example, this will fail with an exception:

 [[1,2,3], [4,5,6], [7,8,9]]

Is that really what you want?

Hint: you need to test whether you can iterate over the elements alist.

Also, use something like alist and not list because list is a 
reserved word.


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


Re: internal circular class references

2008-12-10 Thread James Stroud

Ethan Furman wrote:

Greetings List!

I'm writing a wrapper to the datetime.date module to support having no 
date.  Its intended use is to hold a date value from a dbf file, which 
can be empty.


The class is functional at this point, but there is one thing I would 
like to change -- datetime.date.max and datetime.date.min are class 
attributes of datetime.date, and hold datetime.date values.  At this 
point I have to have two lines outside the actual class definition to do 
the same thing, e.g.:


trimmed down class code
  class NullDate(object):
  adds null capable DateTime.Date constructs
  __slots__ = ['_date']
  def __new__(cls, date='', month=0, day=0):
  nulldate = object.__new__(cls)
  nulldate._date = 
  .
.
.
return nulldate
  def __getattr__(self, name):
  if self:
  attribute = self._date.__getattribute__(name)
  return attribute
  else:
  if callable(dt.date.__dict__[name]):
  return int
  else:
  return 0
  def __nonzero__(self):
  if self._date:
  return True
  return False
  @classmethod
  def fromordinal(cls, number):
  if number:
  return cls(dt.date.fromordinal(number))
  else:
  return cls()
  NullDate.max = NullDate(dt.date.max)
  NullDate.min = NullDate(dt.date.min)
/trimmed down class code

How can I move those last two lines into the class definition so that:
  1) they are class attributes (not instance), and
  2) they are NullDate type objects?

~ethan~


I resisted posting a similar question recently. After much 
consideration, I realized that the inability to reference a class inside 
its own definition must have been a deliberate design of the language. 
So the short answer is you can't.


The way you have done it is best--its not a hack and is good style.

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


Re: Rich Comparisons Gotcha

2008-12-08 Thread James Stroud

Robert Kern wrote:

James Stroud wrote:
I think it skips straight to __eq__ if the element is not the first in 
the list.


No, it doesn't skip straight to __eq__(). y is 1 returns False, so 
(y==1) is checked. When y is a numpy array, this returns an array of 
bools. list.__contains__() tries to convert this array to a bool and 
ndarray.__nonzero__() raises the exception.


list.__contains__() checks is then __eq__() for each element before 
moving on to the next element. It does not try is for all elements, 
then try __eq__() for all elements.


Ok. Thanks for the explanation.


  That no one acknowledges this makes me feel like a conspiracy
  is afoot.

I don't know what you think I'm not acknowledging.


Sorry. That was a failed attempt at humor.

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


Re: Brain going crazy with recursive functions

2008-12-07 Thread James Stroud

[EMAIL PROTECTED] wrote:

I'm trying to solve the 9-tile puzzle using as functional an approach
as possible.  I've recently finished reading SICP and am deliberately
avoiding easy python-isms for the more convoluted scheme/functional
methods.  The following function is trivial to do with for loops and
directly accessing arrays with [] syntax.  I'm trying to limit myself
to the types of idioms/semantics one finds in minimal scheme, such as
in SICP.  I want eventually to port this to scheme, but I know python
better, so that's where I'm starting.

My current problem is the following.  The 9-tile puzzle consists of a
list of lists like this [[1,2,3],[4,5,6],[7,8,0]], where the numbers
can be jumbled up.  I'm looking for the location of the zero using
*only* recursion and operators that are similar to car/cdr.  The
return value should be the row,col of the zero.  For example above the
return value would be (2,2).

I'm also trying to define a single linear_search(...) function which
does the search for within the row (an inner list above) and within
the whole list.  linear_search takes as an argument a truth_function
which does the actual work.  What's tricky is that the truth function
for the array-as-a-whole is also the linear_search for a row.  Once
I'm in linear_search for the array, I also call linear_search for each
row.

The problem is the line where it says acc.insert(0,idx) looks fishy to
me.  It works fine and returns the row,col of the location of the zero
tile, but it seems to be mutating a variable, and that's the thing I'm
trying to avoid.  In a sense, it's not hard enough and python is
making this too easy :)  (although it took a bit of mind-wrenching to
get to this point.  Recursion makes you either dumber or smarter, I'm
not sure which).

How do I accumulate the inner value of the search so it pops out at
the end, without resorting to a mutable variable?  I did a bit of
search and the word monad came up, but I'm not sure what that is (I
know it relates to haskell and some other purely functional stuff, but
I get very lost when trying to read that stuff).

def linear_search(array, truth_func, acc):
# Goes through each element of array and applies truth_func.
# Returns index if found, otherwise returns None
def linear_search_iter(idx, truth_func, arr, acc):
if arr:
tf = truth_func(arr[0])
if tf or type(tf) is int:
acc.insert(0,idx)
return idx, acc+[idx]
else:
return linear_search_iter(idx+1, truth_func, 
arr[1:], acc)
return linear_search_iter(0, truth_func, array, acc)



def locate_zero(p):
# Locates empty tile.  Returns (r,c) tuple
def find_zero_in_row(row):
return linear_search(row, lambda x: x==0, acc)

acc = []
ls = linear_search(p, find_zero_in_row, acc)
print acc
return acc



thanks
Michael


I am honestly getting lost in your code. The following fulfills your 
requirements as far as I can tell. It uses None as a sentinel for the 
truth function matching no elements of the array. Some assignments are 
made within the code simply to make it more readable. They are not 
necessary. The first element that the truth function evaluates to True 
is returned.


I hope it helps.

James

def linear_search(array, truth_func, loc=(0,0)):
  idx1, idx2 = loc
  if idx1 = len(array):
return None
  if idx2 = len(array[idx1]):
return linear_search(array, truth_func, (idx1+1, 0))
  value = array[idx1][idx2]
  tf = truth_func(value)
  if tf:
return loc
  else:
return linear_search(array, truth_func, (idx1, idx2+1))

a = [[5, 3, 4], [2, 0, 1], [8, 6, 7]]
linear_search(a, lambda x: x==0)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Brain going crazy with recursive functions

2008-12-07 Thread James Stroud

James Stroud wrote:

def linear_search(array, truth_func, loc=(0,0)):
  idx1, idx2 = loc
  if idx1 = len(array):
return None
  if idx2 = len(array[idx1]):
return linear_search(array, truth_func, (idx1+1, 0))
  value = array[idx1][idx2]
  tf = truth_func(value)
  if tf:
return loc
  else:
return linear_search(array, truth_func, (idx1, idx2+1))

a = [[5, 3, 4], [2, 0, 1], [8, 6, 7]]
linear_search(a, lambda x: x==0)


PS: If I just made it to easy for you, you can practice by generalizing 
this to an N-dimensional array using recursion. I'm tempted to do it 
myself but I'm going to try to resist instead and do some work that pays.

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


Re: can graphs be made in python as we make in java

2008-12-07 Thread James Stroud

suku wrote:

HI folks...

 i need some suggestion on making graphs. Will this be possible
with normal python setup file or do i need to download add ons for
that..

   help me out


I like pychart. It has the advantage of being pure python and makes very 
nice looking plots. You might also check out matplotlib if you are into 
heavyweight plotting and interactive application development.


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


Re: Rich Comparisons Gotcha

2008-12-07 Thread James Stroud

Rasmus Fogh wrote:

Dear All,

For the first time I have come across a Python feature that seems
completely wrong. After the introduction of rich comparisons, equality
comparison does not have to return a truth value, and may indeed return
nothing at all and throw an error instead. As a result, code like
  if foo == bar:
or
  foo in alist
cannot be relied on to work.

This is clearly no accident. According to the documentation all comparison
operators are allowed to return non-booleans, or to throw errors. There is
explicitly no guarantee that x == x is True.


I'm not a computer scientist, so my language and perspective on the 
topic may be a bit naive, but I'll try to demonstrate my caveman 
understanding example.


First, here is why the ability to throw an error is a feature:

class Apple(object):
  def __init__(self, appleness):
self.appleness = appleness
  def __cmp__(self, other):
assert isinstance(other, Apple), 'must compare apples to apples'
return cmp(self.appleness, other.appleness)

class Orange(object): pass

Apple(42) == Orange()


Second, consider that any value in python also evaluates to a truth 
value in boolean context.


Third, every function returns something. A function's returning nothing 
is not a possibility in the python language. None is something but 
evaluates to False in boolean context.



But surely you can define an equal/unequal classification for all
types of object, if you want to?


This reminds me of complex numbers: would 4 + 4i be equal to sqrt(32)? 
Even in the realm of pure mathematics, the generality of objects (i.e. 
numbers) can not be assumed.



James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Rich Comparisons Gotcha

2008-12-07 Thread James Stroud

Luis Zarrabeitia wrote:


Quoting James Stroud [EMAIL PROTECTED]:


First, here is why the ability to throw an error is a feature:

class Apple(object):
   def __init__(self, appleness):
 self.appleness = appleness
   def __cmp__(self, other):
 assert isinstance(other, Apple), 'must compare apples to apples'
 return cmp(self.appleness, other.appleness)

class Orange(object): pass

Apple(42) == Orange()


I beg to disagree.
The right answer for the question Am I equal to this chair right here? is not
I don't know, nor I can't compare. The answer is No, I'm not a chair, thus
I'm not equal to this chair right here. If someone comes to my house, looking
for me, he will not run away because he sees a chair before he sees me. Your
assert doesn't belong inside the methot, it should be up to the caller to decide
if the human-chair comparisons make sense or not. I certainly don't want to be
type-checking when looking for an object within a mixed-type collection. 

This reminds me of complex numbers: would 4 + 4i be equal to sqrt(32)? 


I assume you meant sqrt(32i).


No, I definitely didn't mean sqrt(32i). I'm using sqrt() to represent 
the mathematical square root, and not an arbitrary function one might 
define, by the way.


My point is that 4 + 4i, sqrt(32), and sqrt(-32) all exist in different 
spaces. They are not comparable, even when testing for equality in a 
pure mathematical sense. If when encounter these values in our programs, 
we might like the power to decide the results of these comparisons. In 
one context it might make sense to throw an exception, in another, it 
might make sense to return False based on the fact that we consider them 
different types, in yet another context, it might make sense to look 
at complex plane values as vectors and return their scalar magnitude for 
comparison to real numbers. I think this ability to define the results 
of comparisons is not a shortcoming of the language but a strength.


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Rich Comparisons Gotcha

2008-12-07 Thread James Stroud

Rasmus Fogh wrote:

Current behaviour is both inconsistent and counterintuitive, as these
examples show.


x = float('NaN')
x == x

False


Perhaps this should raise an exception? I think the problem is not with 
comparisons in general but with the fact that nan is type float:


py type(float('NaN'))
type 'float'

No float can be equal to nan, but nan is a float. How can something be 
not a number and a float at the same time? The illogicality of nan's 
type creates the possibility for the illogical results of comparisons to 
nan including comparing nan to itself.



ll = [x]
x in ll

True

x == ll[0]

False


But there is consistency on the basis of identity which is the test for 
containment (in):


py x is x
True
py x in [x]
True

Identity and equality are two different concepts. Comparing identity to 
equality is like comparing apples to oranges ;o)





import numpy
y = numpy.zeros((3,))
y

array([ 0.,  0.,  0.])

bool(y==y)

Traceback (most recent call last):
  File stdin, line 1, in module
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()


But the equality test is not what fails here. It's the cast to bool that 
fails, which for numpy works like a unary ufunc. The designers of numpy 
thought that this would be a more desirable behavior. The test for 
equality likewise is a binary ufunc and the behavior was chosen in numpy 
for practical reasons. I don't know if you can overload the == operator 
in C, but if you can, you would be able to achieve the same behavior.



ll1 = [y,1]
y in ll1

True

ll2 = [1,y]
y in ll2

Traceback (most recent call last):
  File stdin, line 1, in module
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()


I think you could be safe calling this a bug with numpy. But the fact 
that someone can create a bug with a language is not a condemnation of 
the language. For example, C makes it real easy to crash a program by 
overrunning the limits of an array, but no one would suggest to remove 
arrays from C.



Can anybody see a way this could be fixed (please)? I may well have to
live with it, but I would really prefer not to.


Your only hope is to somehow convince the language designers to remove 
the ability to overload == then get them to agree on what you think the 
proper behavior should be for comparisons. I think the probability of 
that happening is about zero, though, because such a change would run 
counter to the dynamic nature of the language.


James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Rich Comparisons Gotcha

2008-12-07 Thread James Stroud

James Stroud wrote:

[cast to bool] for numpy works like a unary ufunc.


Scratch that. Not thinking and typing at same time.


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Rich Comparisons Gotcha

2008-12-07 Thread James Stroud

Steven D'Aprano wrote:

On Sun, 07 Dec 2008 13:57:54 -0800, James Stroud wrote:


Rasmus Fogh wrote:



ll1 = [y,1]
y in ll1

True

ll2 = [1,y]
y in ll2

Traceback (most recent call last):
  File stdin, line 1, in module
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
I think you could be safe calling this a bug with numpy. 


Only in the sense that there are special cases where the array elements 
are all true, or all false, and numpy *could* safely return a bool. But 
special cases are not special enough to break the rules. Better for the 
numpy caller to write this:


a.all() # or any()

instead of:

try:
bool(a)
except ValueError:
a.all()

as they would need to do if numpy sometimes returned a bool and sometimes 
raised an exception.


I'm missing how a.all() solves the problem Rasmus describes, namely that 
the order of a python *list* affects the results of containment tests by 
numpy.array. E.g. y in ll1 and y in ll2 evaluate to different 
results in his example. It still seems like a bug in numpy to me, even 
if too much other stuff is broken if you fix it (in which case it 
apparently becomes an issue).


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


Re: Rich Comparisons Gotcha

2008-12-07 Thread James Stroud

Robert Kern wrote:

James Stroud wrote:
I'm missing how a.all() solves the problem Rasmus describes, namely 
that the order of a python *list* affects the results of containment 
tests by numpy.array. E.g. y in ll1 and y in ll2 evaluate to 
different results in his example. It still seems like a bug in numpy 
to me, even if too much other stuff is broken if you fix it (in which 
case it apparently becomes an issue).


It's an issue, if anything, not a bug. There is no consistent 
implementation of bool(some_array) that works in all cases. numpy's 
predecessor Numeric used to implement this as returning True if at least 
one element was non-zero. This works well for bool(x!=y) (which is 
equivalent to (x!=y).any()) but does not work well for bool(x==y) (which 
should be (x==y).all()), but many people got confused and thought that 
bool(x==y) worked. When we made numpy, we decided to explicitly not 
allow bool(some_array) so that people will not write buggy code like 
this again.


The deficiency is in the feature of rich comparisons, not numpy's 
implementation of it. __eq__() is allowed to return non-booleans; 
however, there are some parts of Python's implementation like 
list.__contains__() that still expect the return value of __eq__() to be 
meaningfully cast to a boolean.




You have explained

py 112 = [1, y]
py y in 112
Traceback (most recent call last):
  File stdin, line 1, in module
ValueError: The truth value of an array with more than one element is...

but not

py ll1 = [y,1]
py y in ll1
True

It's this discrepancy that seems like a bug, not that a ValueError is 
raised in the former case, which is perfectly reasonable to me.



All I can imagine is that something like the following lives in the 
bowels of the python code for list:


def __contains__(self, other):
  foundit = False
  for i, v in enumerate(self):
if i == 0:
  # evaluates to bool numpy array
  foundit = one_kind_of_test(v, other)
else:
  # raises exception for numpy array
  foundit = another_kind_of_test(v, other)
if foundit:
  break
  return foundit

I'm trying to imagine some other way to get the results mentioned but I 
honestly can't. It's beyond me why someone would do such a thing, but 
perhaps it's an optimization of some sort.


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


Re: Rich Comparisons Gotcha

2008-12-07 Thread James Stroud

Robert Kern wrote:

James Stroud wrote:

py 112 = [1, y]
py y in 112
Traceback (most recent call last):
  File stdin, line 1, in module
ValueError: The truth value of an array with more than one element is...

but not

py ll1 = [y,1]
py y in ll1
True

It's this discrepancy that seems like a bug, not that a ValueError is 
raised in the former case, which is perfectly reasonable to me.


Nothing to do with numpy. list.__contains__() checks for identity with 
is before it goes to __eq__().


...but only for the first element of the list:

py import numpy
py y = numpy.array([1,2,3])
py y
array([1, 2, 3])
py y in [1, y]

Traceback (most recent call last):
  File ipython console, line 1, in module
type 'exceptions.ValueError': The truth value of an array with more 
than one element is ambiguous. Use a.any() or a.all()

py y is [1, y][1]
True

I think it skips straight to __eq__ if the element is not the first in 
the list. That no one acknowledges this makes me feel like a conspiracy 
is afoot.

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


Re: Guido's new method definition idea

2008-12-06 Thread James Stroud

Steven D'Aprano wrote:

On Fri, 05 Dec 2008 20:35:07 -0800, James Stroud wrote:



Daniel Fetchinson wrote:

I'd like this new way of defining methods, what do you guys think?



Consider the maverick who insists on

class C:
   def me.method(arg):
 self.value = arg


Replace self with me.


Yes, I corrected myself one minute after I made the typo.


which should be equivalent to

class C:
   def method(me, arg):
 me.value = arg

What's the interpreter going to do with our maverick's code?


I don't see why you think this is a problem.


The behavior was unspecified as far as I could tell and I was curious as 
to whether me would still be allowed as a reference to self. Allowing 
alternatives to self would maintain backwards compatibility as the use 
of self has been a convention and not enforced by the language.


James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: operators as variables

2008-12-06 Thread James Stroud

macc_200 wrote:

Hi,
just starting programming and have an elementary question after 
playing around with lists but cannot find the answer with googling.
I have a list of variables and I would like some of those variables to 
be integers and some to be operators so the list would look something 
like [5 * 4 - 4 + 6] and then be able to evaluate the result (i.e. get 
10).  How do you make the interpreter see the operator as that instead 
of a string and just echo the list back to me.


Your specification is ambiguous because you mention lists but wrote an 
evaluable expression inside of brackets. Others have answered the 
question you probably really are asking. For fun, though, I'm going to 
pretend you meant list and not whatever [5 * 4 - 4 + 6] is:


import operator

opdict = {'+' : operator.add,
  '-' : operator.sub,
  '*' : operator.mul,
  '/' : operator.div}

def take_two(aniterable):
  assert len(aniterable) % 2 == 0
  aniter = iter(aniterable)
  while True:
try:
  yield aniter.next(), aniter.next()
except StopIteration:
  break

def reductifier(alist):
  value = alist.pop(0)
  for op, operand in take_two(alist):
value = opdict[op](value, operand)
  return value

reductifier([5, *, 4, -, 4, +, 6])
--
http://mail.python.org/mailman/listinfo/python-list


dict subclass and pickle bug (?)

2008-12-05 Thread James Stroud

Hello All,

I subclassed dict and overrode __setitem__. When instances are 
unpickled, the __setstate__ is not called before the keys are assigned 
via __setitem__ in the unpickling protocol.


I googled a bit and found that this a bug filed in 2003:

http://bugs.python.org/issue826897

It is still open with normal priority.

Am I missing something? Is there a workaround for this bug that makes 
fixing it pointless or has it just fallen through the cracks for the 
last 5 years?


Here is an example:

class DictPlus(dict):
  def __init__(self, *args, **kwargs):
self.extra_thing = ExtraThingClass()
dict.__init__(self, *args, **kwargs)
  def __setitem__(self, k, v):
do_something_with(self.extra_thing, k, v)
dict.__setitem__(self, k, v)

Upon unpickling, the error would be:

AttributeError: 'DictPlus' object has no attribute 'extra_thing'


I'm still using python 2.5.1.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: dict subclass and pickle bug (?)

2008-12-05 Thread James Stroud

James Stroud wrote:

Hello All,

I subclassed dict and overrode __setitem__. When instances are 
unpickled, the __setstate__ is not called before the keys are assigned 
via __setitem__ in the unpickling protocol.


I googled a bit and found that this a bug filed in 2003:

http://bugs.python.org/issue826897

It is still open with normal priority.


Here is the ugly fix I'm basically going to have to live with, it seems:

class DictPlus(dict):
  def __init__(self, *args, **kwargs):
self.extra_thing = ExtraThingClass()
dict.__init__(self, *args, **kwargs)
  def __setitem__(self, k, v):
try:
  do_something_with(self.extra_thing, k, v)
except AttributeError:
  self.extra_thing = ExtraThingClass()
  do_something_with(self.extra_thing, k, v)
dict.__setitem__(self, k, v)
  def __setstate__(self, adict):
pass


This violates this:

  Beautiful is better than ugly.


I can't imagine this bug has survived but I also can't imagine any 
better way to do this without specifying a different protocol, which 
would probably break other pickling I'm doing. I don't feel like finding 
out right now.


Maybe repeal pep 307 ;o)


James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: To Troll or Not To Troll (aka: as keyword woes)

2008-12-05 Thread James Stroud

Andreas Waldenburger wrote:

Is it me, or has c.l.p. developed a slightly harsher tone recently?
(Haven't been following for a while.)


Yep. I can only post here for about a week or two until someone blows a 
cylinder and gets ugly because they interpreted something I said as a 
criticism of the language and took it personally by extension. Then I 
have to take a 4 month break because I'm VERY prone to 
reciprocating--nastily. I think its a symptom of the language's 
maturing, getting popular, and a minority fraction* of the language's 
most devout advocates developing an egotism that complements their 
python worship in a most unsavory way.


I wish they would instead spend their energy volunteering to moderate 
this list and culling out some of the spam.


*No names were mentioned in the making of this post.
--
http://mail.python.org/mailman/listinfo/python-list


Re: dict subclass and pickle bug (?)

2008-12-05 Thread James Stroud

Terry Reedy wrote:

because there is no bug to fix.  I have suggesting closing.


May I suggest to add something to this effect within the issue itself so 
others won't spend time trying to figure out why the bug is still 
open? If this is a more general feature of issues, then perhaps it would 
be helpful to provide a footnote at the bottom of all issue pages via 
the page template that explains why they are not bugs and suggests a 
general course of action for the programmer.


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


Re: dict subclass and pickle bug (?)

2008-12-05 Thread James Stroud

James Stroud wrote:

Terry Reedy wrote:

because there is no bug to fix.  I have suggesting closing.


May I suggest to add something to this effect within the issue itself so 
others won't spend time trying to figure out why the bug is still 
open?


Sorry, you did that.

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


Re: To Troll or Not To Troll (aka: as keyword woes)

2008-12-05 Thread James Stroud

alex23 wrote:

On Dec 6, 8:00 am, James Stroud [EMAIL PROTECTED] wrote:

I think its a symptom of the language's
maturing, getting popular, and a minority fraction* of the language's
most devout advocates developing an egotism that complements their
python worship in a most unsavory way.


It's hard to see how anyone could ever take offence at your posts
given such unbiased objectivity ;)


I know you are winking, but I literally keep a sock stuffed in my mouth 
at work to keep my own python advocacy at bay so I can get some work 
done and not drive my coworkers crazy.

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


Re: Guido's new method definition idea

2008-12-05 Thread James Stroud

Daniel Fetchinson wrote:

http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html

The proposal is to allow this:

class C:
def self.method( arg ):
self.value = arg
return self.value

instead of this:

class C:
def method( self, arg ):
self.value = arg
return self.value



I'd like this new way of defining methods, what do you guys think?


Consider the maverick who insists on

class C:
  def me.method(arg):
self.value = arg

which should be equivalent to

class C:
  def method(me, arg):
me.value = arg

What's the interpreter going to do with our maverick's code?

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


Re: Guido's new method definition idea

2008-12-05 Thread James Stroud


Of course I meant

class C:
  def me.method(arg):
me.value = arg

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


Re: How can I do this (from Perl) in Python? (closures)

2008-12-04 Thread James Stroud

[EMAIL PROTECTED] wrote:

from_ten = make_counter(10)
from_three = make_counter(3)

print from_ten()   # 10
print from_ten()   # 11
print from_three() # 3
print from_ten()   # 12
print from_three() # 4


The error message is: UnboundLocalError: local variable 'start'
referenced before assignment. The same thing happens if I omit start
and just use start_num directly.

How can I do it in Python?


Since no one has suggested it:

class make_counter(object):
  def __init__(self, i):
self.i = i
  def __call__(self):
i = self.i
self.i += 1
return i

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   8   9   10   >