Re: [Tutor] multiple assignments when reading a file

2013-07-11 Thread Alan Gauld

On 11/07/13 04:24, Sivaram Neelakantan wrote:


I'm aware of

var1, var2  = lines.split()

kind of assignments

How do I get to do

x1..xn = lines.split()



x = lines.split()

then you access the fields with x[0], x[1]...x[n]

It's the default behaviour.

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] multiple assignments when reading a file

2013-07-11 Thread Dave Angel

On 07/11/2013 08:18 AM, Sivaram Neelakantan wrote:


Thanks for the detailed explanation below.  I've replied below.

On Thu, Jul 11 2013,Dave Angel wrote:



third choice?  Put them in a list.  Whaddya know, they already are.
So just assign a name to that list.  Referencing them is now by
subscript. And you can actually do slices and loops readily, which you
can't easily do on "var names."


This seems the easiest but I already foresee hardcoded subscripts all
over the code which I will promptly forget the very next day of what
it stands for.



Good.  Then you're changing the requirements spec from unreasonable to 
something attainable.  You're the one who had all these numeric 
variables on the left hand side of the assignment.  If you want them to 
be real names, then you're going to make progress.


Next question is where do these names come from?  In the text file, 
there seem to be just a bunch of values, separated by whitespace.  So 
the only characteristic they have at that moment is sequence.  You can 
refer to the 19th item in the text file, or the 4th.


One example of a name correlation would be the traditional 
comma-separated file (csv).  The first line lists the names, separated 
by commas, and subsequent line(s) contain values for those names.  it's 
one way of import/export from a spreadsheet, database, etc.


So if you can put the names in the text file, then you can easily make a 
dict (see below), or a namespace with those names (see below).




Next choice?  Put them in a dict.  This works just like a list, except
the subscript doesn't have to be continguous ints.


alright.



Final choice?  Put them in a namespace.  Something like:

class NewSpace:
 pass

data = NewSpace()
for index, item in enumerate(lines.split()):
 data.__dict__["x" + str(index)] = item



OO, I'd stay away till I get the basics right.



Chris Warrick corrected me on this.  I knew about setattr(), but forgot 
it could be used in this way.


So if you now have a list of names (each a str, and one that's a legal 
name in Python), you could do:


data = NewSpace()
for name, item in zip(names, lines.split()):
setattr(data, name, item)

And now you can have say  print(data.address1) to print out the address1 
string from that file.


Try it with some simple data, it's not very hard.


--
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] multiple assignments when reading a file

2013-07-11 Thread Alan Gauld

On 11/07/13 13:18, Sivaram Neelakantan wrote:
>>> How do I get to do
>>>
>>> x1..xn = lines.split()

third choice?  Put them in a list.


This seems the easiest but I already foresee hardcoded subscripts all
over the code which I will promptly forget the very next day of what
it stands for.


So you would remember what  x1, x7 and x23 were for, but wouldn't 
remember x[1], x[7] and x[23]?


The whole premise here was that you wanted Python to create the numeric 
suffixes for you. The reason that's a bad idea is exactly as you said: 
you don't have a clue what the numbers mean. But if you must use 
numbers, list indices are no worse than suffixed names.



Next choice?  Put them in a dict.  This works just like a list, except
the subscript doesn't have to be continguous ints.


I don't know how you would do this because you still need to
create n unique keys...

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] importing into a function

2013-07-11 Thread Alan Gauld

On 11/07/13 19:47, Jim Mooney wrote:


in the DOS box, but there's not much sense in using an IDE at all if
it doesn't have everything to make life easy, very sensibly arranged,
when you Do need something like a debugger or some snippets (an
essential since I have a truly rotten memory ;')


You can always use a standalone debugger like winpdb.
I find it better than any of the IDEs I've tried.
But then I don't much like IDEs of any kind (unless you
count Unix or emacs as an IDE).

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] renaming input works intermittently

2013-07-11 Thread Steven D'Aprano

On 12/07/13 10:44, Jim Mooney wrote:

When I tried a simple rename of input, it worked - in python 2.7 and python 3.3

import sys
if int(sys.version[0]) < 3:
 input = raw_input

x = input('type something ')
print(x)  # this works in both Py versions

But when I tried that in my numbers program, I got an error:
UnboundLocalError: local variable 'input' referenced before assignment
for the below:


When you assign to a name *anywhere* inside a function, Python treats that name 
as a local variable regardless of whether it is before, or after, the 
assignment. So unlike the language Lua, you can't read a global variable, then 
create a local variable of the same name in the same function.

In your case, the fix is to pull the renaming of input out of the function, so it is performed once 
only, when the module first begins to run, instead of every time you call the function. Put the 
renaming code at the top of the module, after the "import sys", then just unconditionally 
use "input" inside the function.




 try:
 if int(sys.version[0]) < 3:
 input = raw_input
 numbers_str = original = input('Enter a positive'
'integer, space separated if desired.') # error occurs here

Oddly, the error only occurs in Python 3.3 - the above works in Python 2.7



The rules for local variables are rather more complicated in Python 2 and it 
may be that you're somehow slipping through the cracks.


--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] renaming input works intermittently

2013-07-11 Thread Steven D'Aprano

On 12/07/13 11:07, eryksun wrote:

On Thu, Jul 11, 2013 at 9:01 PM, Steven D'Aprano  wrote:

  try:
  if int(sys.version[0]) < 3:
  input = raw_input
  numbers_str = original = input('Enter a positive'
'integer, space separated if desired.') # error occurs here

Oddly, the error only occurs in Python 3.3 - the above works in Python 2.7


The rules for local variables are rather more complicated in Python 2 and it
may be that you're somehow slipping through the cracks.


In 2.x the if statement executes, so the local variable "input" gets assigned.



/facepalm


Ah, I mean, I knew that!


--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor