Re: [Tutor] Help with iterators

2013-03-27 Thread Matthew Johnson
Dear list,

Sorry for the delay -- it has taken some time for me to get these emails.

It appears i made some dumb error when typing out the description.

Mitya Sirenef was correct to ignore my words and to focus on my code.

Thanks for your help. I may ask again / for more help when i feel i
have tried sufficiently hard to absorb the answers below.

Thanks again

mj

On 22/03/2013, at 6:24 PM, "tutor-requ...@python.org"
 wrote:

> Send Tutor mailing list submissions to
>tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>tutor-requ...@python.org
>
> You can reach the person managing the list at
>tutor-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>   1. Re: Help with iterators (Mitya Sirenef)
>   2. Re: Help with iterators (Steven D'Aprano)
>   3. Re: Help with iterators (Steven D'Aprano)
>   4. Re: Help with iterators (Mitya Sirenef)
>   5. Please Help (Arijit Ukil)
>
>
> --
>
> Message: 1
> Date: Thu, 21 Mar 2013 21:39:12 -0400
> From: Mitya Sirenef 
> To: tutor@python.org
> Subject: Re: [Tutor] Help with iterators
> Message-ID: <514bb640.5050...@lightbird.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 03/21/2013 08:39 PM, Matthew Johnson wrote:
>> Dear list,
>>
>> I have been trying to understand out how to use iterators and in
>> particular groupby statements. I am, however, quite lost.
>>
>> I wish to subset the below list, selecting the observations that have
>> an ID ('realtime_start') value that is greater than some date (i've
>> used the variable name maxDate), and in the case that there is more
>> than one such record, returning only the one that has the largest ID
>> ('realtime_start').
>>
>> The code below does the job, however i have the impression that it
>> might be done in a more python way using iterators and groupby
>> statements.
>>
>> could someone please help me understand how to go from this code to
>> the pythonic idiom?
>>
>> thanks in advance,
>>
>> Matt Johnson
>>
>> _
>>
>> ## Code example
>>
>> import pprint
>>
>> obs = [{'date': '2012-09-01',
>> 'realtime_end': '2013-02-18',
>> 'realtime_start': '2012-10-15',
>> 'value': '231.951'},
>> {'date': '2012-09-01',
>> 'realtime_end': '2013-02-18',
>> 'realtime_start': '2012-11-15',
>> 'value': '231.881'},
>> {'date': '2012-10-01',
>> 'realtime_end': '2013-02-18',
>> 'realtime_start': '2012-11-15',
>> 'value': '231.751'},
>> {'date': '2012-10-01',
>> 'realtime_end': '-12-31',
>> 'realtime_start': '2012-12-19',
>> 'value': '231.623'},
>> {'date': '2013-02-01',
>> 'realtime_end': '-12-31',
>> 'realtime_start': '2013-03-21',
>> 'value': '231.157'},
>> {'date': '2012-11-01',
>> 'realtime_end': '2013-02-18',
>> 'realtime_start': '2012-12-14',
>> 'value': '231.025'},
>> {'date': '2012-11-01',
>> 'realtime_end': '-12-31',
>> 'realtime_start': '2013-01-19',
>> 'value': '231.071'},
>> {'date': '2012-12-01',
>> 'realtime_end': '2013-02-18',
>> 'realtime_start': '2013-01-16',
>> 'value': '230.979'},
>> {'date': '2012-12-01',
>> 'realtime_end': '-12-31',
>> 'realtime_start': '2013-02-19',
>> 'value': '231.137'},
>> {'date': '2012-12-01',
>> 'realtime_end': '-12-31',
>> 'realtime_start': '2013-03-19',
>> 'value': '231.197'},
>> {'date': '2013-01-01',
>> 'realtime_end': '-12-31',
>> 'realtime_start': '2013-02-21',
>> 'value': '231.198'},
>> {'date': '2013-01-01',
>> 'realtime_end': '-12-31',
>> 'realtime_start': '2013-03-21',
>> 'value': '231.222'}]
>>
>> maxDate = "2013-03-21"
>>
>> dobs = dict([(d, []) for d in set([e['date'] for e in obs])])
>>
>> for o in obs:
>> dobs[o['date']].append(o)
>>
>> dobs_subMax = dict([(k, [d for d in v if d['realtime_start'] <= maxDate])
>> for k, v in dobs.items()])
>>
>> rts = lambda x: x['realtime_start']
>>
>> mmax = [sorted(e, key=rts)[-1] for e in dobs_subMax.values() if e]
>>
>> mmax.sort(key = lambda x: x['date'])
>>
>> pprint.pprint(mmax)
>> ___
>> Tutor maillist - Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
>
> You can do it with groupby like so:
>
>
> from itertools import groupby
> from operator import itemgetter
>
>
> maxDate = "2013-03-21"
> mmax= list()
>
> obs.sort(key=itemgetter('date'))
>
> for k, group in groupby(obs, key=itemgetter('date')):
> group = [dob for dob in group if dob['realtime_start'] <= maxDate]
> if group:
> group.sort(key=itemgetter('realtime_start'))
> mmax.append(group[-1])
>
> pprint.pprint(mmax)
>
>
> Note that writing multiply-nested comprehensions like you did results in
> very unreadable code. Do you find this code more readable?
>
>  -

Re: [Tutor] Modules

2013-03-27 Thread eryksun
On Wed, Mar 27, 2013 at 5:09 PM, Alan Gauld  wrote:
>
> Tkinter is an unusual module in that it requires support for Tcl/Tk to be
> compiled into the interpreter. This is not always the case.

The _tkinter module is a C extension that links to Tcl/Tk. On Debian
Linux, _tkinter (_tkinter.so) is in the package python-tk /
python3-tk. The ImportError raised by "import tkinter" suggests the
required package. Except for Python 3.2 it incorrectly suggests the
2.x package:

ImportError: No module named _tkinter, please install the
python-tk package

IIRC the official Python installer on Windows defaults to installing
tkinter, but it is optional.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Python 2.7.3 on RHEL 5.8 x86_64 -- Syntax Error

2013-03-27 Thread Amit Saha
On Wed, Mar 27, 2013 at 11:05 PM, Sean Carolan  wrote:
>
>> But, where did you get the idea that you could build Python RPMs using
>> $python setup.py bdist_rpm ? I thought that was only limited to
>> building RPMs for python packages (including extensions), but not the
>> Python interpreter itself. Please correct me if i am wrong.
>
>
> Ok, so it's only for module distributions?  I assumed it could package
> Python itself as well, because it creates a *.spec file that reads like
> this:
>
> %define name Python
> %define version 2.7.3
> %define unmangled_version 2.7.3
> %define release 1
>
> Summary: A high-level object-oriented programming language

Hmm. Let's see, it was a guess on my part as well.

>
>>
>> Okay, here is something for you to try in the meantime. Download the
>> Python 2.7 SRPM (source RPM) from
>> http://koji.fedoraproject.org/koji/packageinfo?packageID=130. May be
>> the F17 version.
>> Extract it to get the source files, patches and the SPEC file.
>
>
> Thank you, I will try this today.  In the meantime I have started a thread
> on the "distutils" mailing list, so as not to spam "Tutor" with my build
> woes.

I will follow the thread there and see what comes out of it. This is
interesting!

FWIW, I tried to get the SRPM and build Python 2.7 on RHEL 5. Quickly
realized that it lacks, yum-builddep, yumdownloader, etc :-/

So, just left it there for then. I will have to try again the manual way.


-Amit.

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


Re: [Tutor] IndexError: index out of bounds

2013-03-27 Thread Peter Otten
Sayan Chatterjee wrote:

> Yes, when handled as a numpy array, it's working fine!
> 
> Traceback (most recent call last):
>   File "ZA.py", line 59, in 
> if temp_za == j:
> ValueError: The truth value of an array with more than one element is
> ambiguous. Use a.any() or a.all()

>From the attached script:

>if temp_za == j:
>counter += 1

Do you want to count the entries?

>>> temp_za = numpy.array([1, 2, 3, 2, 1, 1])
>>> (temp_za == 1).sum()
3

> This error occurs when the temp_za ( a numpy array, print temp_za works
> fine) is compared with j. I am attaching the code.

I'm sorry, I am lacking the domain knowledge to make sense of it. Just one 
more remark:

[] < 0.0

is always False, regardless of the contents of the list

 
> Another question  how do I get an integer value for  p_za / 2 . Type
> casting with int says:
> TypeError: only length-1 arrays can be converted to Python scalars.

I'd use

numpy.array(p_za//2, dtype=int)

but I'm not a numpy expert. As you dig deeper the tutor mailing list may not 
be the best place to ask -- numpy has a dedicated mailing list of its own.

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


Re: [Tutor] Modules

2013-03-27 Thread Alan Gauld

On 27/03/13 19:34, Cor Heisterkamp wrote:


My problem starts with the chapter "Introduction to Modules".
The first line in the program is:
  from tkinter import *
and here I'm getting an error:
  ImportError: No module named tkinter


Thats an unfortunate exampole the author has chosen.

Tkinter is an unusual module in that it requires support for Tcl/Tk to 
be compiled into the interpreter. This is not always the case.


So there are 3 possible causes of the error:

1) Your python does not have support for Tcl/Tk built in.
Which version of Python and which OS are you using? If your version does 
not support Tcl you need to either build a version from source or find a 
copy for your OS with Tkinter support. The default downloads for MacOS, 
Windows and most Linuxes have it but the OS installed versions often don't.


2) The tutor is aimed at Python v3 but you are using v2
Which version of Python are you running? What does it say
when you start the Python interpreter >>> prompt?

3) The tutor and you are both on V2 but you mistyped (or it has a typo) 
that says tkinter when it should say Tkinter.


Try typing

import Tkinter


HTH
--
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] Modules

2013-03-27 Thread Joel Goldstick
On Wed, Mar 27, 2013 at 4:12 PM, Mark Lawrence wrote:

> On 27/03/2013 19:34, Cor Heisterkamp wrote:
>
>> Hi,
>>
>> I just started with Python and found a course named "Python programming"
>> by Jody S. Gunther.
>> My problem starts with the chapter "Introduction to Modules".
>> The first line in the program is:
>>   from tkinter import *
>> and here I'm getting an error:
>>   ImportError: No module named tkinter
>>
>> What to do?
>>
>> google it and you find:  http://wiki.python.org/moin/TkInter
>>
>
Step 2 - can Tkinter be imported?

Try the correct command for your version at the Python prompt:

>>> import Tkinter # no underscore, uppercase 'T' for versions prior to V3.0

>>> import tkinter # no underscore, lowercase 't' for V3.0 and later


>
> This works for me with Python 3.3 on Windows Vista.  What OS and Python
> version are you using, as I believe that this was tKinter in Python 2?
>
> --
> If you're using GoogleCrap™ please read this http://wiki.python.org/moin/*
> *GoogleGroupsPython .
>
> Mark Lawrence
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



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


Re: [Tutor] Modules

2013-03-27 Thread Mark Lawrence

On 27/03/2013 19:34, Cor Heisterkamp wrote:

Hi,

I just started with Python and found a course named "Python programming"
by Jody S. Gunther.
My problem starts with the chapter "Introduction to Modules".
The first line in the program is:
  from tkinter import *
and here I'm getting an error:
  ImportError: No module named tkinter

What to do?




This works for me with Python 3.3 on Windows Vista.  What OS and Python 
version are you using, as I believe that this was tKinter in Python 2?


--
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.


Mark Lawrence

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


Re: [Tutor] Modules

2013-03-27 Thread Danny Yoo
> I just started with Python and found a course named "Python programming" by
> Jody S. Gunther.
> My problem starts with the chapter "Introduction to Modules".
> The first line in the program is:
>  from tkinter import *
> and here I'm getting an error:
>  ImportError: No module named tkinter


Case matters.  "Tkinter" should be capitalized.


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


[Tutor] Modules

2013-03-27 Thread Cor Heisterkamp
Hi,

I just started with Python and found a course named "Python programming" by
Jody S. Gunther.
My problem starts with the chapter "Introduction to Modules".
The first line in the program is:
 from tkinter import *
and here I'm getting an error:
 ImportError: No module named tkinter

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


Re: [Tutor] IndexError: index out of bounds

2013-03-27 Thread Sayan Chatterjee
Hi Peter,

Thanks!!

Yes, when handled as a numpy array, it's working fine!

Traceback (most recent call last):
  File "ZA.py", line 59, in 
if temp_za == j:
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()

This error occurs when the temp_za ( a numpy array, print temp_za works
fine) is compared with j. I am attaching the code.

Another question  how do I get an integer value for  p_za / 2 . Type
casting with int says:
TypeError: only length-1 arrays can be converted to Python scalars.




On 27 March 2013 23:37, Alan Gauld  wrote:

> On 27/03/13 17:36, Sayan Chatterjee wrote:
>
>  2) Traceback (most recent call last):
>>File "ZA.py", line 43, in 
>>  if p_za[i] > 4.0:
>> ValueError: The truth value of an array with more than one element is
>> ambiguous. Use a.any() or a.all()
>>
>
> This implies that p_za[i] is actually an array.
> So maybe p_za is a list (of arrays)?
> Try printing it to see.
>
>
>
> --
> 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
>



-- 


--
*Sayan  Chatterjee*
Dept. of Physics and Meteorology
IIT Kharagpur
Lal Bahadur Shastry Hall of Residence
Room AB 205
Mob: +91 9874513565
blog: www.blissprofound.blogspot.com

Volunteer , Padakshep
www.padakshep.org


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


Re: [Tutor] IndexError: index out of bounds

2013-03-27 Thread Alan Gauld

On 27/03/13 17:36, Sayan Chatterjee wrote:


2) Traceback (most recent call last):
   File "ZA.py", line 43, in 
 if p_za[i] > 4.0:
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()


This implies that p_za[i] is actually an array.
So maybe p_za is a list (of arrays)?
Try printing it to see.



--
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] IndexError: index out of bounds

2013-03-27 Thread Peter Otten
Sayan Chatterjee wrote:

> Hi Walter,
> Thanks a lot!
> 
> Yes, now I get your point. append is working perfectly fine.
> 
> Hi Peter:
> 
> Exactly. It's very nice. Indices needn't have to be mentioned explicitly.
> No explicit looping and the thing is done!
> 
> But I have a question, whenever we want to do operations on the individual
> array elements, don't we have to mention the indices explicitly i.e
> p_za[i]?

For Python's built-in list, yes, but not for numpy arrays.
 
> 1) Traceback (most recent call last):
>   File "ZA.py", line 44, in 
> p_za = p_za % 4
> TypeError: unsupported operand type(s) for %: 'list' and 'int'

>>> items = [3, 4, 5]
>>> items % 2
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for %: 'list' and 'int'
>>> items = numpy.array(items)
>>> items % 2
array([1, 0, 1])

Even for lists you can do better than using an explicit index, you can 
iterate over its members:

>>> items = [3, 4, 5]
>>> [v % 2 for v in items]
[1, 0, 1]

> 2) Traceback (most recent call last):
>   File "ZA.py", line 43, in 
> if p_za[i] > 4.0:
> ValueError: The truth value of an array with more than one element is
> ambiguous. Use a.any() or a.all()
> 
> When the i indices are removed * (1) * error message is showing up and
> when i is included *(2) *is shown.* *

You are probably seeing that error because p_za[i] is a numpy.array, i. e. 
you have a list of arrays:

>>> items = [numpy.array([1,2]), numpy.array([3,4])]
>>> items[0] > 4
array([False, False], dtype=bool)
>>> if items[0] > 4: pass
... 
Traceback (most recent call last):
  File "", line 1, in 
ValueError: The truth value of an array with more than one element is 
ambiguous. Use a.any() or a.all()

How you managed to get there and what you actually want to achieve -- I 
can't tell from what you provide. Perhaps you can give a little more 
context, in code, but more importantly in prose.


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


Re: [Tutor] IndexError: index out of bounds

2013-03-27 Thread Sayan Chatterjee
Hi Walter,
Thanks a lot!

Yes, now I get your point. append is working perfectly fine.

Hi Peter:

Exactly. It's very nice. Indices needn't have to be mentioned explicitly.
No explicit looping and the thing is done!

But I have a question, whenever we want to do operations on the individual
array elements, don't we have to mention the indices explicitly i.e p_za[i]?

1) Traceback (most recent call last):
  File "ZA.py", line 44, in 
p_za = p_za % 4
TypeError: unsupported operand type(s) for %: 'list' and 'int'



2) Traceback (most recent call last):
  File "ZA.py", line 43, in 
if p_za[i] > 4.0:
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()

When the i indices are removed * (1) * error message is showing up and when
i is included *(2) *is shown.* *








On 27 March 2013 22:29, Walter Prins  wrote:

> Hi Sayan,
>
> On 27 March 2013 16:31, Sayan Chatterjee wrote:
>
>> p_za = [None]*N is not giving away the error message.
>>
>> for i in range(0,N):
>> p_za.append = p_initial[i] + t*K*cos(K*p_initial[i]); is also not
>> working.
>>
>
> append() is a method, so using append you want something like:
>
> for i in range(0,N):
> p_za.append( p_initial[i] + t*K*cos(K*p_initial[i]) );
>
> After every loop iteration, the list grows by having one item appended to
> it, being the result of the expression: p_initial[i] +
> t*K*cos(K*p_initial[i])
>
>
>> Could you please redirect me to a link where the example is demonstrated?
>>
>
> http://courses.cms.caltech.edu/cs11/material/python/misc/python_idioms.html
>
> See the paragraph on "Sequence multiplication".
>
>
>
>> What is the simplest way to assign an array element a value?
>>
>
> What you have is fine for assignment to a particular slot in the list.
> What you've missed and has already been pointed out, is to initialise/set
> the length of your list first, before trying to set the value of arbitrary
> slots.  In the C example you posted the array is declared with length 200
> up front.  In your Python code however you assign [], which is a list of
> length 0.   By contrast, the expression I gave you before, e.g. [None] * N,
> generates a list of length N, with each element in the list being the None
> object, thus initialising the list, ensuring that you can later assign to
> arbitrary slots when needed.
>
> Walter
>
>
>



-- 


--
*Sayan  Chatterjee*
Dept. of Physics and Meteorology
IIT Kharagpur
Lal Bahadur Shastry Hall of Residence
Room AB 205
Mob: +91 9874513565
blog: www.blissprofound.blogspot.com

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


Re: [Tutor] IndexError: index out of bounds

2013-03-27 Thread Peter Otten
Sayan Chatterjee wrote:

> When trying to print or assign array elements, getting the following
> error:
> 
> Traceback (most recent call last):
>   File "ZA.py", line 32, in 
> p_za[i] = p_initial[i] + t*K*cos(K*p_initial[i]);
> IndexError: index out of bounds
> 
> I am using Numpy, is it due to that? I am attaching the code herewith.

If you are using numpy it is likely that you don't need to loop over the 
index explicitly. Assuming t and K are scalars, and p_initial is a numpy 
array you can write

p_za = p_initial + t * K * numpy.cos(K*p_initial)

For example:

>>> import numpy
>>> p_initial = numpy.array([1.2, 3.4, 5.6])
>>> t = 1.1
>>> K = 2.2
>>> p_initial + t*K*numpy.cos(K*p_initial)
array([-0.92189929,  4.28408588,  7.94692559])

Quite powerful, once you get the knack of it.

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


Re: [Tutor] IndexError: index out of bounds

2013-03-27 Thread Walter Prins
Hi Sayan,

On 27 March 2013 16:31, Sayan Chatterjee  wrote:

> p_za = [None]*N is not giving away the error message.
>
> for i in range(0,N):
> p_za.append = p_initial[i] + t*K*cos(K*p_initial[i]); is also not
> working.
>

append() is a method, so using append you want something like:

for i in range(0,N):
p_za.append( p_initial[i] + t*K*cos(K*p_initial[i]) );

After every loop iteration, the list grows by having one item appended to
it, being the result of the expression: p_initial[i] +
t*K*cos(K*p_initial[i])


> Could you please redirect me to a link where the example is demonstrated?
>

http://courses.cms.caltech.edu/cs11/material/python/misc/python_idioms.html

See the paragraph on "Sequence multiplication".



> What is the simplest way to assign an array element a value?
>

What you have is fine for assignment to a particular slot in the list.
What you've missed and has already been pointed out, is to initialise/set
the length of your list first, before trying to set the value of arbitrary
slots.  In the C example you posted the array is declared with length 200
up front.  In your Python code however you assign [], which is a list of
length 0.   By contrast, the expression I gave you before, e.g. [None] * N,
generates a list of length N, with each element in the list being the None
object, thus initialising the list, ensuring that you can later assign to
arbitrary slots when needed.

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


Re: [Tutor] HELP: Creating animation from multiple plots

2013-03-27 Thread Peter Otten
Sayan Chatterjee wrote:

> for t in range(0,200):
>   fname = 'file_' + str(t)
> 
> So it will assign fname values file_0, file_1 so on. Dropping the quotes
> is giving me
> 
> IOError: [Errno 2] No such file or directory: 'file_0'
> 
> 
> Indeed the file is not present. In C we write,if we have to record data in
> a file
> 
> FILE *fp
> 
> fp = fopen("file.dat","w")
> 
> 
> Here I want to write different data sets in files having different name
> i.e I want to create the files with the data sets. I am quite new to
> Python, so you can assume zero knowledge while answering. Thanks for your
> support. :)

If you try to open a non-existent file in "r+" mode in C you should get an 
error, too. The following C code

FILE * f;
int i;
char filename[100];

for (i=0; i<10; i++) {
sprintf(filename, "foo%d.dat", i);
FILE * f = fopen(filename, "w");
/* write stuff to file */
...
fclose(f);
}

translates into this piece of Python:

for i in range(10):
filename = "foo%d.dat" % i
with open(filename, "w") as f:
# write stuff to file
...


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


Re: [Tutor] HELP: Creating animation from multiple plots

2013-03-27 Thread Sayan Chatterjee
Oh yes, thanks. That worked. :)


On 27 March 2013 21:58, Bod Soutar  wrote:

> You were opening the file for reading, rather than writing. It
> therefore was expecting to find a file.
> Change
> fo = open('fname','r+')
> to
> fo = open('fname','w')
>
> Bodsda
>
> On 27 March 2013 16:17, Sayan Chatterjee 
> wrote:
> > for t in range(0,200):
> >   fname = 'file_' + str(t)
> >
> > So it will assign fname values file_0, file_1 so on. Dropping the quotes
> is
> > giving me
> >
> > IOError: [Errno 2] No such file or directory: 'file_0'
> >
> >
> > Indeed the file is not present. In C we write,if we have to record data
> in a
> > file
> >
> > FILE *fp
> >
> > fp = fopen("file.dat","w")
> >
> >
> > Here I want to write different data sets in files having different name
> i.e
> > I want to create the files with the data sets. I am quite new to Python,
> so
> > you can assume zero knowledge while answering. Thanks for your support.
> :)
> >
> >
> > On 27 March 2013 21:38, Walter Prins  wrote:
> >>
> >> Hello,
> >>
> >> On 27 March 2013 15:59, Sayan Chatterjee 
> >> wrote:
> >>>
> >>> Hi Amit,
> >>>
> >>> fo = fopen('fname','r+')
> >>> fo.write("%d   %d",j,counter)
> >>>
> >>>
> >>> Is giving the following error:
> >>>
> >>> File "ZA.py", line 30, in 
> >>> fo = open('fname','r+')
> >>> IOError: [Errno 2] No such file or directory: 'fname'
> >>>
> >>> Where is the mistake?
> >>
> >>
> >> You are trying to open a file named literally "fname" due to putting it
> in
> >> quotes, you probably want to drop the quotes.
> >>
> >> Walter
> >
> >
> >
> >
> > --
> >
> >
> >
> --
> > Sayan  Chatterjee
> > Dept. of Physics and Meteorology
> > IIT Kharagpur
> > Lal Bahadur Shastry Hall of Residence
> > Room AB 205
> > Mob: +91 9874513565
> > blog: www.blissprofound.blogspot.com
> >
> > Volunteer , Padakshep
> > www.padakshep.org
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> >
>



-- 


--
*Sayan  Chatterjee*
Dept. of Physics and Meteorology
IIT Kharagpur
Lal Bahadur Shastry Hall of Residence
Room AB 205
Mob: +91 9874513565
blog: www.blissprofound.blogspot.com

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


Re: [Tutor] IndexError: index out of bounds

2013-03-27 Thread Sayan Chatterjee
p_za = [None]*N is not giving away the error message.

for i in range(0,N):
p_za.append = p_initial[i] + t*K*cos(K*p_initial[i]); is also not
working.

Could you please redirect me to a link where the example is demonstrated?
What is the simplest way to assign an array element a value?

i.e the C analogue of:

int array[200]

for(i=0;i<200;i++)
  array[i] = 2*i + 5;






On 27 March 2013 21:44, Walter Prins  wrote:

> Hi,
>
>
> On 27 March 2013 15:50, Sayan Chatterjee wrote:
>
>> Dear all,
>>
>> When trying to print or assign array elements, getting the following
>> error:
>>
>> Traceback (most recent call last):
>>   File "ZA.py", line 32, in 
>> p_za[i] = p_initial[i] + t*K*cos(K*p_initial[i]);
>> IndexError: index out of bounds
>>
>> I am using Numpy, is it due to that? I am attaching the code herewith.
>>
>
>  Not Numpy no.  The p_za list appears to be empty at the point where the
> above assgnment to p_za[i] is done, hence you get the "IndexError" message.
>  You should initialise p_za to be as long as needed first.  Maybe by simply
> using p_za = [None] * N instead of assigning [], or alternately perhaps by
> appending instead at the point where you first reference p_ze[i].
>
> Walter
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 


--
*Sayan  Chatterjee*
Dept. of Physics and Meteorology
IIT Kharagpur
Lal Bahadur Shastry Hall of Residence
Room AB 205
Mob: +91 9874513565
blog: www.blissprofound.blogspot.com

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


Re: [Tutor] HELP: Creating animation from multiple plots

2013-03-27 Thread Bod Soutar
You were opening the file for reading, rather than writing. It
therefore was expecting to find a file.
Change
fo = open('fname','r+')
to
fo = open('fname','w')

Bodsda

On 27 March 2013 16:17, Sayan Chatterjee  wrote:
> for t in range(0,200):
>   fname = 'file_' + str(t)
>
> So it will assign fname values file_0, file_1 so on. Dropping the quotes is
> giving me
>
> IOError: [Errno 2] No such file or directory: 'file_0'
>
>
> Indeed the file is not present. In C we write,if we have to record data in a
> file
>
> FILE *fp
>
> fp = fopen("file.dat","w")
>
>
> Here I want to write different data sets in files having different name i.e
> I want to create the files with the data sets. I am quite new to Python, so
> you can assume zero knowledge while answering. Thanks for your support. :)
>
>
> On 27 March 2013 21:38, Walter Prins  wrote:
>>
>> Hello,
>>
>> On 27 March 2013 15:59, Sayan Chatterjee 
>> wrote:
>>>
>>> Hi Amit,
>>>
>>> fo = fopen('fname','r+')
>>> fo.write("%d   %d",j,counter)
>>>
>>>
>>> Is giving the following error:
>>>
>>> File "ZA.py", line 30, in 
>>> fo = open('fname','r+')
>>> IOError: [Errno 2] No such file or directory: 'fname'
>>>
>>> Where is the mistake?
>>
>>
>> You are trying to open a file named literally "fname" due to putting it in
>> quotes, you probably want to drop the quotes.
>>
>> Walter
>
>
>
>
> --
>
>
> --
> Sayan  Chatterjee
> Dept. of Physics and Meteorology
> IIT Kharagpur
> Lal Bahadur Shastry Hall of Residence
> Room AB 205
> Mob: +91 9874513565
> blog: www.blissprofound.blogspot.com
>
> Volunteer , Padakshep
> www.padakshep.org
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HELP: Creating animation from multiple plots

2013-03-27 Thread Sayan Chatterjee
Putting "w" instead of "r+" probably solves the problem. The error is not
showing now.


On 27 March 2013 21:47, Sayan Chatterjee  wrote:

> for t in range(0,200):
>   fname = 'file_' + str(t)
>
> So it will assign fname values file_0, file_1 so on. Dropping the quotes
> is giving me
>
> IOError: [Errno 2] No such file or directory: 'file_0'
>
>
> Indeed the file is not present. In C we write,if we have to record data in
> a file
>
> FILE *fp
>
> fp = fopen("file.dat","w")
>
>
> Here I want to write different data sets in files having different name
> i.e I want to create the files with the data sets. I am quite new to
> Python, so you can assume zero knowledge while answering. Thanks for your
> support. :)
>
>
> On 27 March 2013 21:38, Walter Prins  wrote:
>
>> Hello,
>>
>> On 27 March 2013 15:59, Sayan Chatterjee wrote:
>>
>>> Hi Amit,
>>>
>>> fo = fopen('fname','r+')
>>> fo.write("%d   %d",j,counter)
>>>
>>>
>>> Is giving the following error:
>>>
>>> File "ZA.py", line 30, in 
>>> fo = open('fname','r+')
>>> IOError: [Errno 2] No such file or directory: 'fname'
>>>
>>> Where is the mistake?
>>>
>>
>> You are trying to open a file named literally "fname" due to putting it
>> in quotes, you probably want to drop the quotes.
>>
>> Walter
>>
>
>
>
> --
>
>
> --
> *Sayan  Chatterjee*
> Dept. of Physics and Meteorology
> IIT Kharagpur
> Lal Bahadur Shastry Hall of Residence
> Room AB 205
> Mob: +91 9874513565
> blog: www.blissprofound.blogspot.com
>
> Volunteer , Padakshep
> www.padakshep.org
>



-- 


--
*Sayan  Chatterjee*
Dept. of Physics and Meteorology
IIT Kharagpur
Lal Bahadur Shastry Hall of Residence
Room AB 205
Mob: +91 9874513565
blog: www.blissprofound.blogspot.com

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


Re: [Tutor] HELP: Creating animation from multiple plots

2013-03-27 Thread Sayan Chatterjee
for t in range(0,200):
  fname = 'file_' + str(t)

So it will assign fname values file_0, file_1 so on. Dropping the quotes is
giving me

IOError: [Errno 2] No such file or directory: 'file_0'


Indeed the file is not present. In C we write,if we have to record data in
a file

FILE *fp

fp = fopen("file.dat","w")


Here I want to write different data sets in files having different name i.e
I want to create the files with the data sets. I am quite new to Python, so
you can assume zero knowledge while answering. Thanks for your support. :)


On 27 March 2013 21:38, Walter Prins  wrote:

> Hello,
>
> On 27 March 2013 15:59, Sayan Chatterjee wrote:
>
>> Hi Amit,
>>
>> fo = fopen('fname','r+')
>> fo.write("%d   %d",j,counter)
>>
>>
>> Is giving the following error:
>>
>> File "ZA.py", line 30, in 
>> fo = open('fname','r+')
>> IOError: [Errno 2] No such file or directory: 'fname'
>>
>> Where is the mistake?
>>
>
> You are trying to open a file named literally "fname" due to putting it in
> quotes, you probably want to drop the quotes.
>
> Walter
>



-- 


--
*Sayan  Chatterjee*
Dept. of Physics and Meteorology
IIT Kharagpur
Lal Bahadur Shastry Hall of Residence
Room AB 205
Mob: +91 9874513565
blog: www.blissprofound.blogspot.com

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


Re: [Tutor] IndexError: index out of bounds

2013-03-27 Thread Walter Prins
Hi,


On 27 March 2013 15:50, Sayan Chatterjee  wrote:

> Dear all,
>
> When trying to print or assign array elements, getting the following error:
>
> Traceback (most recent call last):
>   File "ZA.py", line 32, in 
> p_za[i] = p_initial[i] + t*K*cos(K*p_initial[i]);
> IndexError: index out of bounds
>
> I am using Numpy, is it due to that? I am attaching the code herewith.
>

Not Numpy no.  The p_za list appears to be empty at the point where the
above assgnment to p_za[i] is done, hence you get the "IndexError" message.
 You should initialise p_za to be as long as needed first.  Maybe by simply
using p_za = [None] * N instead of assigning [], or alternately perhaps by
appending instead at the point where you first reference p_ze[i].

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


Re: [Tutor] IndexError: index out of bounds

2013-03-27 Thread Joel Goldstick
On Wed, Mar 27, 2013 at 11:50 AM, Sayan Chatterjee <
sayanchatter...@gmail.com> wrote:

> Dear all,
>
> When trying to print or assign array elements, getting the following error:
>
> Traceback (most recent call last):
>   File "ZA.py", line 32, in 
> p_za[i] = p_initial[i] + t*K*cos(K*p_initial[i]);
>

You declare p_za = [] above.  So there is no p_za[i].  You should use
append since you are adding elements to the end of the list.

> IndexError: index out of bounds
>
> I am using Numpy, is it due to that? I am attaching the code herewith.
>
>
>
> --
>
>
> --
> *Sayan  Chatterjee*
> Dept. of Physics and Meteorology
> IIT Kharagpur
> Lal Bahadur Shastry Hall of Residence
> Room AB 205
> Mob: +91 9874513565
> blog: www.blissprofound.blogspot.com
>
> Volunteer , Padakshep
> www.padakshep.org
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


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


Re: [Tutor] HELP: Creating animation from multiple plots

2013-03-27 Thread Walter Prins
Hello,

On 27 March 2013 15:59, Sayan Chatterjee  wrote:

> Hi Amit,
>
> fo = fopen('fname','r+')
> fo.write("%d   %d",j,counter)
>
>
> Is giving the following error:
>
> File "ZA.py", line 30, in 
> fo = open('fname','r+')
> IOError: [Errno 2] No such file or directory: 'fname'
>
> Where is the mistake?
>

You are trying to open a file named literally "fname" due to putting it in
quotes, you probably want to drop the quotes.

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


Re: [Tutor] HELP: Creating animation from multiple plots

2013-03-27 Thread Joel Goldstick
On Wed, Mar 27, 2013 at 11:59 AM, Sayan Chatterjee <
sayanchatter...@gmail.com> wrote:

> Hi Amit,
>
> fo = fopen('fname','r+')
> fo.write("%d   %d",j,counter)
>
>
> Is giving the following error:
>
> File "ZA.py", line 30, in 
> fo = open('fname','r+')
> IOError: [Errno 2] No such file or directory: 'fname'
>
> Where is the mistake?
>

Where is the file called 'fname'?  It must exist and  be in the current
directory

>
> Cheers,
> Sayan
>
>
>
>
>
>
> On 27 March 2013 12:20, Amit Saha  wrote:
>
>> On Wed, Mar 27, 2013 at 4:49 PM, Sayan Chatterjee
>>  wrote:
>> > Yes, ffmpeg will do if multiple plots can be generated using
>> mathplotlib .
>> > I'll look up the links you provided and get back to you, if I can't
>> figure
>> > it out. :)
>>
>> Sure, good luck! :)
>>
>>
>> >
>> >
>> >
>> >
>> > On 27 March 2013 12:12, Amit Saha  wrote:
>> >>
>> >> On Wed, Mar 27, 2013 at 4:36 PM, Sayan Chatterjee
>> >>  wrote:
>> >> > Thanks a lot for your prompt reply.
>> >> >
>> >> > 1. Yes. This is exactly what I wanted. Creating a bunch of data sets
>> and
>> >> > then writing script to plot them using gnuplot, but if something can
>> >> > produce
>> >> > directly 'plots' it will certainly be helpful.
>> >>
>> >> Yes, indeed it is possible. You may want to explore matplotlib a bit.
>> >> You can start with this tutorial [1].
>> >>
>> >> [1] http://www.loria.fr/~rougier/teaching/matplotlib/
>> >>
>> >> >
>> >> > 2. Yes. By stitching them up I meant an animation.Sorry for the
>> >> > ambiguity.
>> >> > Exactly how we can do it Octave.
>> >> >
>> >> > Pls see this link:
>> >> > http://www.krizka.net/2009/11/06/creating-animations-with-octave/
>> >>
>> >> Right, yes, if you see it uses mencoder/ffmpeg to create the
>> >> animation. So, if you save your individual plots and then use one of
>> >> these tools, you should be able to get the animation done.
>> >>
>> >> Matplotlib itself seems to have some Animated plotting capabilities,
>> >> but I haven't had any experience with them.
>> >>
>> >>
>> >> Best,
>> >> Amit.
>> >>
>> >>
>> >> --
>> >> http://amitsaha.github.com/
>> >
>> >
>> >
>> >
>> > --
>> >
>> >
>> >
>> --
>> > Sayan  Chatterjee
>> > Dept. of Physics and Meteorology
>> > IIT Kharagpur
>> > Lal Bahadur Shastry Hall of Residence
>> > Room AB 205
>> > Mob: +91 9874513565
>> > blog: www.blissprofound.blogspot.com
>> >
>> > Volunteer , Padakshep
>> > www.padakshep.org
>>
>>
>>
>> --
>> http://amitsaha.github.com/
>>
>
>
>
> --
>
>
> --
> *Sayan  Chatterjee*
> Dept. of Physics and Meteorology
> IIT Kharagpur
> Lal Bahadur Shastry Hall of Residence
> Room AB 205
> Mob: +91 9874513565
> blog: www.blissprofound.blogspot.com
>
> Volunteer , Padakshep
> www.padakshep.org
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


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


Re: [Tutor] HELP: Creating animation from multiple plots

2013-03-27 Thread Sayan Chatterjee
Hi Amit,

fo = fopen('fname','r+')
fo.write("%d   %d",j,counter)


Is giving the following error:

File "ZA.py", line 30, in 
fo = open('fname','r+')
IOError: [Errno 2] No such file or directory: 'fname'

Where is the mistake?

Cheers,
Sayan






On 27 March 2013 12:20, Amit Saha  wrote:

> On Wed, Mar 27, 2013 at 4:49 PM, Sayan Chatterjee
>  wrote:
> > Yes, ffmpeg will do if multiple plots can be generated using mathplotlib
> .
> > I'll look up the links you provided and get back to you, if I can't
> figure
> > it out. :)
>
> Sure, good luck! :)
>
> >
> >
> >
> >
> > On 27 March 2013 12:12, Amit Saha  wrote:
> >>
> >> On Wed, Mar 27, 2013 at 4:36 PM, Sayan Chatterjee
> >>  wrote:
> >> > Thanks a lot for your prompt reply.
> >> >
> >> > 1. Yes. This is exactly what I wanted. Creating a bunch of data sets
> and
> >> > then writing script to plot them using gnuplot, but if something can
> >> > produce
> >> > directly 'plots' it will certainly be helpful.
> >>
> >> Yes, indeed it is possible. You may want to explore matplotlib a bit.
> >> You can start with this tutorial [1].
> >>
> >> [1] http://www.loria.fr/~rougier/teaching/matplotlib/
> >>
> >> >
> >> > 2. Yes. By stitching them up I meant an animation.Sorry for the
> >> > ambiguity.
> >> > Exactly how we can do it Octave.
> >> >
> >> > Pls see this link:
> >> > http://www.krizka.net/2009/11/06/creating-animations-with-octave/
> >>
> >> Right, yes, if you see it uses mencoder/ffmpeg to create the
> >> animation. So, if you save your individual plots and then use one of
> >> these tools, you should be able to get the animation done.
> >>
> >> Matplotlib itself seems to have some Animated plotting capabilities,
> >> but I haven't had any experience with them.
> >>
> >>
> >> Best,
> >> Amit.
> >>
> >>
> >> --
> >> http://amitsaha.github.com/
> >
> >
> >
> >
> > --
> >
> >
> >
> --
> > Sayan  Chatterjee
> > Dept. of Physics and Meteorology
> > IIT Kharagpur
> > Lal Bahadur Shastry Hall of Residence
> > Room AB 205
> > Mob: +91 9874513565
> > blog: www.blissprofound.blogspot.com
> >
> > Volunteer , Padakshep
> > www.padakshep.org
>
>
>
> --
> http://amitsaha.github.com/
>



-- 


--
*Sayan  Chatterjee*
Dept. of Physics and Meteorology
IIT Kharagpur
Lal Bahadur Shastry Hall of Residence
Room AB 205
Mob: +91 9874513565
blog: www.blissprofound.blogspot.com

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


[Tutor] IndexError: index out of bounds

2013-03-27 Thread Sayan Chatterjee
Dear all,

When trying to print or assign array elements, getting the following error:

Traceback (most recent call last):
  File "ZA.py", line 32, in 
p_za[i] = p_initial[i] + t*K*cos(K*p_initial[i]);
IndexError: index out of bounds

I am using Numpy, is it due to that? I am attaching the code herewith.



-- 


--
*Sayan  Chatterjee*
Dept. of Physics and Meteorology
IIT Kharagpur
Lal Bahadur Shastry Hall of Residence
Room AB 205
Mob: +91 9874513565
blog: www.blissprofound.blogspot.com

Volunteer , Padakshep
www.padakshep.org


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


Re: [Tutor] Building Python 2.7.3 on RHEL 5.8 x86_64 -- Syntax Error

2013-03-27 Thread Sean Carolan
> But, where did you get the idea that you could build Python RPMs using
> $python setup.py bdist_rpm ? I thought that was only limited to
> building RPMs for python packages (including extensions), but not the
> Python interpreter itself. Please correct me if i am wrong.
>

Ok, so it's only for module distributions?  I assumed it could package
Python itself as well, because it creates a *.spec file that reads like
this:

%define name Python
%define version 2.7.3
%define unmangled_version 2.7.3
%define release 1

Summary: A high-level object-oriented programming language


> Okay, here is something for you to try in the meantime. Download the
> Python 2.7 SRPM (source RPM) from
> http://koji.fedoraproject.org/koji/packageinfo?packageID=130. May be
> the F17 version.
> Extract it to get the source files, patches and the SPEC file.
>

Thank you, I will try this today.  In the meantime I have started a thread
on the "distutils" mailing list, so as not to spam "Tutor" with my build
woes.

regards,

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