Re: [Tutor] Stack unwind using exceptions.

2009-07-05 Thread Noufal Ibrahim

Kent Johnson wrote:
[..]

Why not just return the value from the function and pass it up the
call chain? If a call fails return None. Something like this:


That's what I ended up doing but the first thing occurred to me and I 
was just wondering if there's any production code that relies on the 
technique.


--
~noufal
http://nibrahim.net.in/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading variables in a data set?

2009-07-05 Thread Kent Johnson
On Sat, Jul 4, 2009 at 12:09 PM, Steven Buckbucks...@gmail.com wrote:

 I've used a module (StataTools) from (http://presbrey.mit.edu/PyDTA ) to get
 a Stata .dta file into Python. In Stata the data set is an NXK matrix
 where N is the number of observations (households) and K is the number of
 variables.
 I gather it's now a list where each element of the list is an observation (a
 vector) for one household.  The name of my list is data; I gather Python
 recognizes the first observation by: data[1] .
 Example,
 data = [X_1, X_2, X_3, . . . . , X_N]  where each X_i for all i, is vector
 of household characteristics, eg X_1 = (age_1, wage_1, . . . , residence_1).

 I also have a list for variable names called varname; although I'm not
 sure the module I used to extract the .dta into Python also created a
 correspondence between the varname list and the data list--the python
 interpreter won't print anything when I type one of the variable names, I
 was hoping it would print out a vector of ages or the like.

varname is probably just a list of strings without any direct
connection to the data.

 In anycase, I'd like to make a scatter plot in pylab, but don't know how to
 identify a variable in data (i.e.  I'd like a vector listing the ages and
 another vector listing the wages of  households).  Perhaps, I need to run
 subroutine to collect each relevant data point to create a new list which I
 define as my variable of interest?  From the above example, I'd like to
 create a list such as: age = [age_1, age_2, . . . , age_N] and likewise for
 wages.

You can use a list comprehension to collect columns from the data. If
age is the first element of each observation (index 0), and wages the
second (index 1), then
ages = [ observation[0] for observation in data ]
wages = [ observation[1] for observation in data ]

 Any help you could offer would be very much appreciated.  Also, this is my
 first time using the python tutor, so let me know if I've used it
 appropriately or if I should change/narrow the structure of my question.

It's very helpful if you show us the code you have so far.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Stack unwind using exceptions.

2009-07-05 Thread bob gailer

Noufal Ibrahim wrote:

Kent Johnson wrote:
[..]

Why not just return the value from the function and pass it up the
call chain? If a call fails return None. Something like this:


That's what I ended up doing but the first thing occurred to me and I 
was just wondering if there's any production code that relies on the 
technique.




I use the exception technique in my Python Pipelines parser. The 
recursive routine has 6 raise statements. The exceptions are caught by 
the program that calls the recursive routine. There would be more code 
and complexity to work back up the call chain.



--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Poor style to use list as array?

2009-07-05 Thread Angus Rodgers
The problem this time is, given an amount of US currency in cents
(and less than a dollar), to print out a description of the coins
needed to represent it, using the smallest number of coins.  E.g.:

How many cents this time? 59
2 quarters, 1 nickel and 4 pennies.

How many cents this time? 55
2 quarters and 1 nickel.

How many cents this time? 75
3 quarters.

My program relaxes the restriction to amounts less than a dollar,
and initialises the necessary data structures as follows:

denom = ['dollar', 'quarter', 'dime', 'nickel', 'penny']
value = [100, 25, 10, 5, 1]
LEN = 5
plural = [None] * LEN
plural[-1] = 'pennies'
count = [None] * LEN   # Kludge

I use the list 'count' as a kind of array, so that e.g. count[-1]
is the number of pennies needed, count[2] is the number of dimes
needed, and so on.  Any initial values I might store as elements
of the list are never used, hence the use of 'None'. (The use of
'None' in the list 'plural' is much more defensible, I think, as
it is used to denote the formation of a regular plural by adding
's' to the name.) Instead, the actual values are assigned in a
'for' loop, as follows:

for i in range(LEN - 1):
(count[i], amnt) = divmod(amnt, value[i])

This feels like a bit of a cheat, as if I am trying to program in
Python as if it were some other more familiar language.  Should I
have coded this differently?

The full source code is here, in case anyone wants to look at it
(but I'm not soliciting any more attention, as I've already been
given quite a lot of it, and fear being offered the comfy chair!):
http://python.pastebin.com/d5e321ae0   (retention: 1 day)

Could I have used dictionaries instead, with the denomination names
as keys?  Is it possible to guarantee a sequence in which the keys
of a dictionary are iterated through? (If not, I suppose I could
keep the list 'denom' as it is here, and iterate through it with
for key in denom:, although this seems a bit redundant.)
-- 
Angus Rodgers
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Poor style to use list as array?

2009-07-05 Thread Noufal Ibrahim

Angus Rodgers wrote:
[..]


This feels like a bit of a cheat, as if I am trying to program in
Python as if it were some other more familiar language.  Should I
have coded this differently?


I can't offer anything concrete but here are some things that occur to 
me at a glance.


0. You can try to use the collections module in 2.6 (especially the 
namedtuple object which gives you access to things through attributes as 
well as through order.
1. You have 3 lists (denom, plural and count) which you will iterate 
through in lockstep as far as I can tell. It's an option to use a list 
of 3-tuples or a list of dictionaries with three keys (denomination, 
plural and count).


Thanks.

--
~noufal
http://nibrahim.net.in/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is my style OK in this elementary student exercise?

2009-07-05 Thread Tim Peters
[Angus Rogers, suffering eval-angst]
 ...
 On the other hand, so long as I AM only executing the function
 myself, I am no more at risk than I already am every single time
 I type a command into a Python interpreter, of any description.
 (A somewhat Existentialist thought, perhaps!  Virtual suicide
 is always a possibility.) -

 Does that seem reasonable?  You've made me clearly aware of a
 risk that I was only vaguely aware of before (I ruminated only
 briefly as to whether any harm might come from entering general
 Python expressions, but my main feeling about this facility was
 that it would probably be useful - in some future exercise),
 but isn't there a role for functions that one can use oneself,
 but never ever distribute to the general public?

Certainly!  I use eval() and input() all the time in my private code
(but never in released code), and you're never going to screw yourself
by accident doing so.  Especially if you have an interest in writing
mathematical code, it's a tremendous convenience for prompted input to
support arbitrary computation.


 If so, are the precautions I have suggested above sufficient?

In private code, any precautions are probably just a waste of time.
You really don't, for example, need to remind /yourself/ not to enter
a convoluted expression that emails your credit card info to a hacker
site in Nigeria.  Or if you do need to remind yourself not to do
things like that, you probably shouldn't be using a computer to begin
with ;-)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Stack unwind using exceptions.

2009-07-05 Thread Eike Welk
On Sunday 05 July 2009, Noufal Ibrahim wrote:
 Kent Johnson wrote:
 [..]

  Why not just return the value from the function and pass it up
  the call chain? If a call fails return None. Something like this:

 That's what I ended up doing but the first thing occurred to me and
 I was just wondering if there's any production code that relies on
 the technique.

The Pyparsing library uses exceptions a lot internally. 

If I understood it right, exceptions are used to tell that a pattern 
does not match. If the pattern matches the results are transported 
with a regular 'return'. As it happens quite often that a pattern 
does not match, exceptions can be considered a regular mechanism for 
information transport in Pyparsing.

There are two types of exceptions in Pyparsing. An exception that 
means: try the next pattern; and there are exceptions that mean: 
there was a fatal error, stop parsing.

Pyparsing:
http://pyparsing.wikispaces.com/


Kind regards,
Eike.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Poor style to use list as array?

2009-07-05 Thread Kent Johnson
On Sun, Jul 5, 2009 at 2:48 PM, Angus Rodgersang...@bigfoot.com wrote:

 for i in range(LEN - 1):
    (count[i], amnt) = divmod(amnt, value[i])

How about this:
counts = []
for val in value:
  count, amnt = divmod(amnt, val)
  counts.append(count)

 This feels like a bit of a cheat, as if I am trying to program in
 Python as if it were some other more familiar language.  Should I
 have coded this differently?

Generally it's more straighforward to iterate over a list directly
rather than using an index.

 The full source code is here, in case anyone wants to look at it
 (but I'm not soliciting any more attention, as I've already been
 given quite a lot of it, and fear being offered the comfy chair!):

Don't worry, we have retired the comfy chair.

 Could I have used dictionaries instead, with the denomination names
 as keys?  Is it possible to guarantee a sequence in which the keys
 of a dictionary are iterated through?

In general no. Python 2.7 and 3.1 do have an ordered dictionary:
http://www.python.org/dev/peps/pep-0372/

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Poor style to use list as array?

2009-07-05 Thread Alan Gauld


Angus Rodgers ang...@bigfoot.com wrote 


as keys?  Is it possible to guarantee a sequence in which the keys
of a dictionary are iterated through? 


Indirectly yes:

for key in sorted( dct ):
   print key

I would definitely tend to go with a dictionary for the 
denominations/values in this case  (Actually I'd probably 
go for a class but that's another ball game entirely!)



--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] append question

2009-07-05 Thread Steven Buck
Hi Python Tutors:

I have a data structure that looks like:

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

I want to define a new variable that captures the second element of each
sublist from above:

 testvar2 = []

Next I try to capture the aforementioned elements:

 for i in len(test):
testvar2.append(test[i][2])

I want testvar2 = [2,5,8] but instead I get the following error message:

Traceback (most recent call last):
  File pyshell#34, line 1, in module
for i in len(test):
TypeError: 'int' object is not iterable

Any insight would be appreciated.
Thanks
Steve








-- 
Steven Buck
Ph.D. Student
Department of Agricultural and Resource Economics
University of California, Berkeley
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Poor style to use list as array?

2009-07-05 Thread Angus Rodgers
On Sun, 5 Jul 2009 18:49:32 -0400, Kent Johnson wrote:

On Sun, Jul 5, 2009 at 2:48 PM, Angus Rodgersang...@bigfoot.com wrote:

 for i in range(LEN - 1):
    (count[i], amnt) = divmod(amnt, value[i])

Incidentally, I forgot to quote the next line:

count[-1] = m

Of course, this is much better incorporated into the loop, thus:

for i in range(LEN):
(count[i], amnt) = divmod(amnt, value[i])

and this lends itself to being rewritten in terms of some other
kind of iteration (as below).

How about this:
counts = []
for val in value:
  count, amnt = divmod(amnt, val)
  counts.append(count)

I like that very much, because it is in the nature of the problem
that the numbers in the list 'value' are all distinct, and so can
be used as keys.  However, as this remark suggests, I think I am
going to need 'count' to be a dictionary, rather than a list, and
the same goes for 'denom', and 'plural' (although this should be
keyed by the strings in 'denom', and we don't need the 'None's).

So it looks like we should have something like:

plural = {'penny':'pennies'}

counts = {}
for val in value:
(count, amnt) = divmod(amnt, val)
counts[val] = count

and, later in the program, something like this (comments stripped
for brevity, and one new comment added):

for val in value:
amnt = counts[val]
name = denom[val]
if amnt:
to_print -= 1
if printed:
if to_print:
buff += , 
else:
buff +=  and 
printed += 1
if amnt  1:
# This should become a function
if name in plural:
name = plural[name]
else:
name += 's'
buff += %d %s % (amnt, name)

I haven't run this, but I'll try rewriting the program tomorrow.
It looks like there's nothing to it.

I'm much happier now, thanks!
-- 
Angus Rodgers
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] append question

2009-07-05 Thread Luke Paireepinart
Read your error message... It highlighted the first line of your for
loop ansd said ints aren't iterable. len(list) returns an integer. You
want a list of items... for i in range(len(list)):

On 7/5/09, Steven Buck bucks...@gmail.com wrote:
 Hi Python Tutors:

 I have a data structure that looks like:

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

 I want to define a new variable that captures the second element of each
 sublist from above:

 testvar2 = []

 Next I try to capture the aforementioned elements:

 for i in len(test):
 testvar2.append(test[i][2])

 I want testvar2 = [2,5,8] but instead I get the following error message:

 Traceback (most recent call last):
   File pyshell#34, line 1, in module
 for i in len(test):
 TypeError: 'int' object is not iterable

 Any insight would be appreciated.
 Thanks
 Steve








 --
 Steven Buck
 Ph.D. Student
 Department of Agricultural and Resource Economics
 University of California, Berkeley


-- 
Sent from my mobile device
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Poor style to use list as array?

2009-07-05 Thread Angus Rodgers
On Mon, 06 Jul 2009 01:02:10 +0100, I hastily wrote:

Incidentally, I forgot to quote the next line:

count[-1] = m

That was copied-and-pasted from an older version of the program,
with less descriptive identifiers.  'm' should be 'amnt'.

Of course, this is much better incorporated into the loop, thus:

for i in range(LEN):
(count[i], amnt) = divmod(amnt, value[i])

[...]

So it looks like we should have something like:

plural = {'penny':'pennies'}

counts = {}
for val in value:
(count, amnt) = divmod(amnt, val)
counts[val] = count

Better, of course (I still haven't run it, but it should work):

counts = {}
for val in value:
(counts[val], amnt) = divmod(amnt, val)

I'm much happier now, thanks!

... but also rather annoyed with myself.
-- 
Angus Rodgers
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] append question

2009-07-05 Thread Rich Lovely
2009/7/5 Steven Buck bucks...@gmail.com:
 for i in len(test):
     testvar2.append(test[i][2])

 I want testvar2 = [2,5,8] but instead I get the following error message:

 Traceback (most recent call last):
   File pyshell#34, line 1, in module
     for i in len(test):
 TypeError: 'int' object is not iterable

 Any insight would be appreciated.
 Thanks
 Steve
 --
 Steven Buck
 Ph.D. Student
 Department of Agricultural and Resource Economics
 University of California, Berkeley

This sounds like a homework assignment, and we're not supposed to give
out answers to homework.

The error message and the docs explain what you're doing wrong if you
take a moment to look.
from http://www.python.org/doc/2.6/reference/compound_stmts.html#for

for_stmt ::=  for target_list in expression_list : suite
  [else : suite]

The expression list is evaluated once; it should yield an iterable
object. An iterator is created for the result of the expression_list.
The suite is then executed once for each item provided by the
iterator, in the order of ascending indices. Each item in turn is
assigned to the target list using the standard rules for assignments,
and then the suite is executed.

As Luke said, len returns an int, which as your error tells you, is
not iterable.  From the same page:
The for statement is used to iterate over the elements of a
sequence (such as a string, tuple or list) or other iterable
object:

Therefore you have an iterable, there is no need to try and construct a new one.

Does that help?

It is extremly unpythonic to iterate over range(len(...)), as it adds
in the overhead of two function calls, and ruins the readability of
code.  The latter is probably the most important of the two.

An even more pythonic way to do this would be a list comprehension,
http://www.python.org/doc/2.6/tutorial/datastructures.html#list-comprehensions

If it's not homework, let us know, and we'll be more than willing to
give you code if you still need it.

-- 
Richard Roadie Rich Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] append question

2009-07-05 Thread Robert Berman
In [1]: test=[[1,2,3],[4,5,6],[7,8,9]]
In [3]: testvar2 = []

In [16]: for i in range(len(test)):
   : testvar2.append(test[i][1])
   : 
   : 

In [17]: testvar2
Out[17]: [2, 5, 8]

Robert


On Sun, 2009-07-05 at 15:57 -0700, Steven Buck wrote:
 Hi Python Tutors:
  
 I have a data structure that looks like:
  
  test=[[1,2,3],[4,5,6],[7,8,9]]
  
 I want to define a new variable that captures the second element of
 each sublist from above:
  
  testvar2 = []
  
 Next I try to capture the aforementioned elements:
  
  for i in len(test):
 testvar2.append(test[i][2])
  
 I want testvar2 = [2,5,8] but instead I get the following error
 message:
  
 Traceback (most recent call last):
   File pyshell#34, line 1, in module
 for i in len(test):
 TypeError: 'int' object is not iterable
  
 Any insight would be appreciated.
 Thanks
 Steve
  
  
  
  
  
 
  
 
 
 -- 
 Steven Buck
 Ph.D. Student
 Department of Agricultural and Resource Economics
 University of California, Berkeley
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Poor style to use list as array?

2009-07-05 Thread Rich Lovely
                if name in plural:
                    name = plural[name]
                else:
                    name += 's'
This could be written more cleanly (although arguably not as readably) as

name = plural.get(name, name + s)

d.get(key, default) returns the value from d mapped to key if it
exists, or default otherwise.

You might also want to split your calculation and display code into
two separate loops.  This might seem wasteful, but it will make your
code easier to read and maintain, and the waste is only marginal with
the loops you're running - there is a maximum of only 17 passes (once
for each value of coin and note)

-- 
Richard Roadie Rich Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Poor style to use list as array?

2009-07-05 Thread bob gailer

Angus Rodgers wrote:

The problem this time is, given an amount of US currency in cents
(and less than a dollar), to print out a description of the coins
needed to represent it, using the smallest number of coins.  E.g.:

How many cents this time? 59
2 quarters, 1 nickel and 4 pennies.

How many cents this time? 55
2 quarters and 1 nickel.

How many cents this time? 75
3 quarters.

My program relaxes the restriction to amounts less than a dollar,
and initialises the necessary data structures as follows:

denom = ['dollar', 'quarter', 'dime', 'nickel', 'penny']
value = [100, 25, 10, 5, 1]
LEN = 5
plural = [None] * LEN
plural[-1] = 'pennies'
count = [None] * LEN   # Kludge
  


I'm inclined to use a class. I omitted the input and loop for simplicity.

class Coin:

 def __init__(self, name, value, plural=None):
   self.name = name
   self.value = value
   if plural:
 self.plural = plural
   else:
 self.plural = self.name + 's'
   self.count = 0

 def display(self):
   if self.count == 0:
 return None
   if self.count == 1:
 return %d %s % (self.count, name)
   else:
 return %d %s % (self.count, self.plural)
   
coins = Coin('dollar', 100), Coin('quarter', 25), Coin('dime', 10), 
Coin('nickel', 5), Coin('penny', 1, 'pennies')

amnt = 99
buff = []
for coin in coins:
 (coin.count, amnt) = divmod(amnt, coin.value)
 d = coin.display()
 if d:
   buff.append(d)
if len(buff)  2:
 print buff
else:
 print ', '.join(buff[:-1]) +  and  + buff[-1]


I use the list 'count' as a kind of array, so that e.g. count[-1]
is the number of pennies needed, count[2] is the number of dimes
needed, and so on.  Any initial values I might store as elements
of the list are never used, hence the use of 'None'. (The use of
'None' in the list 'plural' is much more defensible, I think, as
it is used to denote the formation of a regular plural by adding
's' to the name.) Instead, the actual values are assigned in a
'for' loop, as follows:

for i in range(LEN - 1):
(count[i], amnt) = divmod(amnt, value[i])

This feels like a bit of a cheat, as if I am trying to program in
Python as if it were some other more familiar language.  Should I
have coded this differently?

The full source code is here, in case anyone wants to look at it
(but I'm not soliciting any more attention, as I've already been
given quite a lot of it, and fear being offered the comfy chair!):
http://python.pastebin.com/d5e321ae0   (retention: 1 day)

Could I have used dictionaries instead, with the denomination names
as keys?  Is it possible to guarantee a sequence in which the keys
of a dictionary are iterated through? (If not, I suppose I could
keep the list 'denom' as it is here, and iterate through it with
for key in denom:, although this seems a bit redundant.)
  



--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] append question

2009-07-05 Thread Rich Lovely
2009/7/6 Steven Buck bucks...@gmail.com:
 Thanks for the previous responses.  This isn't homework--I'm beyond
 coursework, although I am a newbie to Python (and I've never had to do much
 real programming since I've just used Stata for econometric analysis).  I'm
 testing Python as a more powerful alternative to Stata.

 I've learned from the responses I received, although now see my problem
 differently.  The data structure I have uses a dictionary and know now
 that the append command doesn't work.  Having said that, perhaps my
 variables of interest have already been created--perhaps I just don't know
 how to identify them.  I've been using some borrowed code to get me started;
 my modified version is below:


 import sys

 # The modules below help me get a .dta file into Python.
 # Although I'm not sure what form they take; I suppose a list of lists???
 from StataTools import Reader
 from StataTypes import MissingValue

 # I call my data set the psid (Panel Study of Income Dynamics)
 # In Stata this would look like and NXK matrix (N observations and K
 variables)
 psid=Reader(file('data3.dta'))

 # I gather this next just creates a list of the variable names.
 varnames=[x.name for x in psid.variables()]

 # It's not clear what these next two lines gain me.
 labels=psid.file_headers()['vlblist']
 Labels=dict(zip(varnames,labels))


  From here, I'd like Python to identify the Nx1 vectors (or n-tuples) that
 correspond to the varnames list defined above.  I can't seem grab the
 vectors representing age, wage, etc..  I've tried things like
 age, psid['age'], psid.age.  My last email was an attempt to create the
 vectors myself, although the Reader module puts the data in a dictionary
 structure so the append command I was trying to use doesn't work.

 Hopefully once I learn to create and call on my own vectors and matrices
 I'll be better off--I'm comfortable working with these in MATLAB and Stata.

 Bottom line:  Given the above data I've imported/extracted from Stata .dta
 file, how do I create an Nx1 vector  which I call 'age'?

 Thanks for your patience with this newbie.
 Steve

Sorry about suggesting this was homework... I noticed the word
University, but not the line above it saying PhD student...

If you're new to python, the standard path we recommend is to take an
online tutorial.

Dive into Python (http://www.diveintopython.org/) has been recommended
for people with some programming experience, and Think Python for
those without, although I'm sure that if you ask 10 members of this
list, you'll get 20 different suggestions.  Real veterans get pointed
at the standard libraries, which are extremly well commented in the
most part.  I personally learned from the tutorial in the python
documentation, but it does leave a fair bit of the thought processes
involved in programming out.

See what I mean?  One person, three recomendations.

-- 
Richard Roadie Rich Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor