Re: [Tutor] Parsing RSS Feeds

2010-08-15 Thread Nitin Das
http://www.feedparser.org/

--nitin

On Mon, Aug 16, 2010 at 3:50 AM, aug dawg  wrote:

> Hey all,
>
> Does anyone know of any modules to help my program parse RSS feeds?
>
> Thanks!
>
> ___
> 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] reading from 2 file output to 1

2010-08-17 Thread Nitin Das
I think in both the while loops , for loops are iterating over the new
lines, as a result col1,col2,col3,col4,col5,col6,col7 as a result after both
the while loop finishes these colums would only be having the values of the
last lines from both the files. as a result the new file3 would only be
having last lines of the both the files appended together, because u r
combining the columns after both the while loops.

--nitin

On Tue, Aug 17, 2010 at 2:09 PM, nitin chandra wrote:

> Hello All,
>
> I am trying to read from 2 CSV files, where first 4 ([0,1,2,3])
> columns are read from 'file1' and 3 columns ([1,2,3]) from 'file2' and
> write them into a 3rd file 'file3', 7 columns string. The data is
> Numeric values both, +ve and -ve.
>
> this is how i was trying.
>
> **
> import sys,os, fileinput
>
>
> file11 = raw_input('Enter PR1 File name :')
> fp1 = open(file11,'r')
>
> file12 = raw_input('Enter PR3 File Name :')
> fp2 = open(file12,'r')
>
>
> while 1:
>fp1 = fp1.readline()
>  for line in fp1:
> line2 = line.split(",")
> col1 = line2[0]
> col2 = line2[1]
> col3 = line2[2]
> col4 = line2[3]
> print col1
> print col2
> FL1 = '%s,%s,%s,%s' % (col1,col2,col3,col4)
> print FL1
>
> while 1:
>fp2 = fp2.readline()
>   for line1 in fp2:
>  line3 = line1.split(",")
>  col5 = line3[1]
>  col6 = line3[2]
>  col7 = line3[3]
>
>  FL2 = '%s,%s,%s,' % (col5,col6,col7)
>  print FL2
>
> file3=raw_input('Enter PR2 OUTPUT File Name :')
> fp3 = open(file3,'w')
>
> print col1,col2,col3,col4 + ',' + col5,col6,col7
>
> str3 = '%s,%s,%s,%s,%s,%s,%s\n' % (col1,col2,col3,col4,col5,col6,col7)
> fp3.write(str3)
>
> **
>
> I am getting the following error :
>
> Traceback (most recent call last):
>  File "mer5Pr2.py", line 16, in 
>col2 = line2[1]
> IndexError: list index out of range
>
> *
>
> There is data in the file at 'col2'. So what am i missing / doing wrong ?
>
> And is there better / optimized way of doing it?
>
> Thanks
>
> Nitin
> ___
> 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] Getting confusing NameError

2010-08-18 Thread Nitin Das
use raw_input instead of input . as using input is unsafe as it evaluates
your input as python command.
for e,g.
x = input("enter command") and u entered "1 + 2" as input., then it will
evaluate 1+2 = 3 = x.

 -nitin

On Wed, Aug 18, 2010 at 12:45 PM, shane brennan wrote:

> Hi Tutors
>
> This is my first mail to this list so firstly thank you for reading and
> apologies in advance for any noob mistakes i may have inadvertantly made:).
> Ive only started learning python as my first programming language and its
> all going well so far, working my way through a couple of books one of which
> is programming python: for absolute beginners by Michael Dawson
>
> In one of the code samples he supplies while doing loops ( namely if and
> else loops) im getting a NameError i dont understand and cannot find any
> help anywhere about it. here is the code he supplied and the error im
> getting
>
> # Password
> # Demonstrates the if statement
>
> print("Welcome to System Security Inc.")
> print("-- where security is our middle name\n")
>
> password = input("Enter your password: ")
>
> if password == "secret":
> print("Access Granted")
>
> input("\n\nPress the enter key to exit.")
>
>
> and the traceback error im getting
>
> Traceback (most recent call last):
>   File "G:\Programming\Python\programming python\chapter3\password.py",
> line 7,
> in 
> password = input("Enter your password: ")
>   File "", line 1, in 
> NameError: name 'secret' is not defined
>
>
> I know this is probably very simple but for some reason i cannot get my
> head around it.
>
> thank you for any help in advance
>
> shane
>
> ___
> 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


[Tutor] Immutable objects

2010-08-18 Thread Nitin Das
Hello,

 Can somebody help me how to make immutable objects in python. I have
seen overriding the __new__, __setattr__ methods.. but not comfortable with
how to use them to make the object immutable.

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


Re: [Tutor] Immutable objects

2010-08-18 Thread Nitin Das
class mymut(object):

  def __setattr__(self,k,v):
  if hasattr(self,k):
  if self.__dict__.get(k) == None:
  self.__dict__[k] = v
  else:
  raise TypeError("Cant Modify Attribute Value")
  else:
  raise TypeError("Immutable Object")


class mm(mymut):
x = ''
y = ''
def __init__(self,x,y):
self.x = x
self.y = y



p = mm(10,11)
print p.x
print p.y


I have created this immutable object.Is there any other better
implementation?

thanks
-- nitin




On Thu, Aug 19, 2010 at 8:54 AM, Nitin Das  wrote:

> Hello,
>
>  Can somebody help me how to make immutable objects in python. I have
> seen overriding the __new__, __setattr__ methods.. but not comfortable with
> how to use them to make the object immutable.
>
> thanks in advance
> --nitin
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Immutable objects

2010-08-19 Thread Nitin Das
Thanks guys,
NamedTuple implementation is preety nice solution.

--nitin

On Thu, Aug 19, 2010 at 6:28 PM, Peter Otten <__pete...@web.de> wrote:

> Peter Otten wrote:
>
> > Nitin Das wrote:
> >
> >> class mymut(object):
> >>
> >>   def __setattr__(self,k,v):
> >>   if hasattr(self,k):
> >>   if self.__dict__.get(k) == None:
> >>   self.__dict__[k] = v
> >>   else:
> >>   raise TypeError("Cant Modify Attribute Value")
> >>   else:
> >>   raise TypeError("Immutable Object")
> >>
> >>
> >> class mm(mymut):
> >> x = ''
> >> y = ''
> >> def __init__(self,x,y):
> >> self.x = x
> >> self.y = y
> >>
> >>
> >>
> >> p = mm(10,11)
> >> print p.x
> >> print p.y
> >>
> >>
> >> I have created this immutable object.Is there any other better
> >> implementation?
> >
> > How about
> >
> >>>> from collections import namedtuple
> >>>> Mm = namedtuple("Mm", "x y")
> >>>> p = Mm(10, 11)
> >>>> p.x
> > 10
> >>>> p.y
> > 11
> >>>> p.x = 42
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > AttributeError: can't set attribute
> >>>> p.z = 42
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > AttributeError: 'Mm' object has no attribute 'z'
> >>>>
>
> By the way, you can see the class definition with
>
> namedtuple("Mm", "x y", verbose=True)
>
> Peter
>
> ___
> 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] List comprehension for dicts?

2010-08-20 Thread Nitin Das
result = [i%2 for i in itertools.takewhile(lamda x:i<  10, seq)]
is a good approach but it is taking little more time than the for loop over
iterator.

def takewhile(predicate, iterable):
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
for x in iterable:
if predicate(x):
yield x
else:
break

I don't know why? As it seems that this for loop and the other for loop
doing the same thing.

--nitin


On Fri, Aug 20, 2010 at 9:17 PM, bob gailer  wrote:

>  On 8/20/2010 5:44 AM, Steven D'Aprano wrote:
>
>> On Fri, 20 Aug 2010 06:10:59 pm Alan Gauld wrote:
>>
>>> "Steven D'Aprano"  wrote
>>>
>>>  the purpose). No matter how fast you can perform a loop, it's
 always faster to avoid it altogether, so this:

 seq = xrange(1000)
 result = []
 for i in seq:
if i>= 10: break
result.append(i%2)


 will be much faster than this:

 seq = xrange(1000)
 result = [i%2 for i in seq if i<  10]

>>> Although it should be pointed out that these are doing very different
>>> things.
>>>
>> Well yes, but I pointed out that you *can* bail out early of for-loops,
>> but not list comprehensions. The whole point is with a list comp,
>> you're forced to iterate over the entire sequence, even if you know
>> that you're done and would like to exit.
>>
>>  Take a look at itertools.takewhile!
>
> result = [i%2 for i in itertools.takewhile(lamda x:i<  10, seq)]
>
>
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
> ___
> 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] prime test problem

2010-08-21 Thread Nitin Das
For this problem u will get lots of solutions on the net.
e.g wilson's theorem , sieve of eranthoses etc.

--nitin

On Sat, Aug 21, 2010 at 7:05 PM, Roelof Wobben  wrote:

>  Hello,
>
> I have to make a programm which can test if a number is a prime.
> I know a prime is a number which can only be diveded by 1 and itself.
>
> One way is was thinking about is to make a loop which try if % has output
> 0.
> But that don't work.
>
> Can someone give me a hint what's the best approach is.
>
> Roelof
>
>
> ___
> 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] check the terminal keyword

2010-08-21 Thread Nitin Das
You can use raw_input like for e.g.
u_input = raw_input("user_command")
n then u can check u_input against anything. if doesn't matches then u can
throw an error.
I hope it shall help you.

--nitin


On Sat, Aug 21, 2010 at 6:37 PM, ANKUR AGGARWAL wrote:

> i m making a app in which i launch application using os.system("input from
> user"). I want to check whether the input entered by the user matches with
> the exact keyword in terminal or not.
> like i want to launch vlc so user should provide me input as " vlc" in
> order to launch the app just like he did from terminal. I want to make a
> check on the input spell so that if he type "wlc" it shows an error to it. I
> have an little idea abt sys.agrv but don't know how to use it... Plz help me
> guys.
> Thanks in advance
>
> ___
> 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] Adding all numbers in a file or list

2010-08-24 Thread Nitin Das
alternatively you can use the lambda , reduce function for summing up all
the numbers in a list for e.g:-

lis = [1,2,3,4,5]

p = reduce(lambda x,y : x+y, lis)

p will have the value = 15.


--nitin

On Mon, Aug 23, 2010 at 9:05 PM, aug dawg  wrote:

> Oh okay, sorry about that.
>
> Thanks for the help!
>
>
>
> On Mon, Aug 23, 2010 at 11:33 AM, Sander Sweers 
> wrote:
>
>> On 23 August 2010 17:24, aug dawg  wrote:
>> > So it's sum(list_name) ?
>>
>> Correct, but it is not limited to lists. Any itterable with
>> ints/floats will do, for example a tuple is also accepted.
>>
>> Greets
>> Sander
>>
>> PS: Please use reply to all so others on this list may benefit from
>> the questions/answers ;-)
>>
>
>
> ___
> 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] continuous running of a method

2010-08-24 Thread Nitin Das
The problem with this while loop is if your random value doesn't lie between
the mentioned range then ur 100% cpu would be utilized. The one thing u can
do is to sleep for some time lets say 0.5 sec after every while loop
iteration , in this case ur cpu utilization will go down.

--nitin

On Mon, Aug 23, 2010 at 8:21 PM, bob gailer  wrote:

>  On 8/23/2010 1:00 AM, Greg Bair wrote:
>
>> I have a method (I'll call it foo) that will either return None or an
>> object depending on a random value generated.  What I want to happen is that
>> if I call foo(), i.e, f = foo() and it returns None, to re-call it until it
>> returns something else.  I would think this would be done with a while loop,
>> but can't seem to get the details right.
>>
>
> Even though a while will work, you will have tied up the CPU until the loop
> terminates. This is never a good idea.
>
> What is your higher level goal?
>
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
> ___
> 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] Trouble with exercise regarding classes

2010-08-25 Thread Nitin Das
There are 2 ways to get the ypos value one is cball.ypos and another is
call.getY() both will give u the current ypos.

--nitin

On Thu, Aug 26, 2010 at 7:26 AM, Andrew Martin wrote:

> All I want to do is add a line that displays that maximum height the
> cannonball reaches. I created a variable zenith to store the highest y
> value. I then wanted to compare the current y value of the cannonball to
> zenith while the cannonballs y value is greater than zero. If the
> cannonballs current y value is greater than zenith, I want to have the
> current value replace zenith. Finally, once the cannonball has reaches y =
> 0, I wanted the program to write out the value for zenith.
>
> I want to compare zenith, a floating point number, with the current y
> value? I thought the current y value could be retrieved by Projectile.getY.
> And how do I call the getY using the instance?
>
>
>
>
> On Wed, Aug 25, 2010 at 7:24 PM, Alan Gauld wrote:
>
>>
>> "Andrew Martin"  wrote
>>
>>
>>  However, when I did so I got this error: "TypeError: unbound method
>>> getY()
>>> must be called with Projectile instance as first argument (got nothing
>>> instead) "
>>>
>>
>>  def main():
angle, vel, h0, time = getInputs()
cball = Projectile(angle, vel, h0)

>>>
>> cball is a Projectile instance
>>
>>
>> zenith = 0.0
while cball.getY() >= 0:

>>>
>> So this is fine
>>
>>
>> cball.update(time)

>>>
>>
>> if Projectile.getY > zenith:
zenith = Projectile.getY()

>>>
>> But what are you doing here?
>> You are trying to compare the getY method of the class with a floating
>> point number?
>> Then you call getY using the class rather than the instance?
>> I'm confused - and so is Python...
>>
>>
>> --
>> Alan Gauld
>> 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
>>
>
>
> ___
> 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] question about lists and doctest

2010-08-26 Thread Nitin Das
The doctest module searches for pieces of text that look like interactive
Python sessions, and then executes those sessions to verify that they work
exactly as shown.
Here ur a_list[3] in docstring is 42 then doctest expects it to be 42 for
success. Since u changed 42 to 16 then it is not = 42 as per the
a_list[3]=42 in docstring hence it raises error.

--nitin

On Thu, Aug 26, 2010 at 9:11 PM, Roelof Wobben  wrote:

>  hello,
>
> I have this programm
>
> #  Add your doctests here:
> """
>   >>> a_list[3]
>   42
>   >>> a_list[6]
>   'Ni!'
>   >>> len(a_list)
>   8
> """
> a_list = ['test', 'test','test',42,'test','test','Ni!','test']
> print a_list[3]
> print a_list[6]
> print len(a_list)
> if __name__ == '__main__':
> import doctest
> doctest.testmod()
>
> If I change lets say 42 to 16 then the doctest fails with
>
> Expecting
>
> 42
>
> Got
>
> 16.
>
>
> How does doctest now which values are right or not ?
>
> Roelof
>
>
>
>
> ___
> 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] import problem

2010-08-28 Thread Nitin Das
it is >>> import calendar not calender

--nitin

On Sat, Aug 28, 2010 at 10:51 PM,
wrote:

>
>
> tutor-bounces+christopher.henk=allisontransmission@python.org wrote on
> 08/28/2010 12:54:28 PM:
>
> > Hello,
> >
> > Im trying to do a import on a linux machine.
> > But now Im gettting this message :
> >
> > Python 2.6.5 (r265:79063, Apr  1 2010, 05:28:39)
> > [GCC 4.4.3 20100316 (prerelease)] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> import calender
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > ImportError: No module named calender
> > >>>
>
> Did you try calendar (with an 'a')?  it might have just been a spelling
> mistake in your import
>
> Can you do other imports from site-packages?
>
>
> Chris
> ___
> 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] function error

2010-09-28 Thread Nitin Das
It seems that ur turtle.position doesn't return a list because of this when
indexing is done on that u get this kind of error.

--nitin

On Tue, Sep 28, 2010 at 3:39 PM, Alan Gauld wrote:

>
> "roberto"  wrote
>
>
>  i have the following error when i call this function:
>>  20 def outOfBounds():
>> ---> 21 if abs(turtle.position()[0]) >
>> turtle.window_height()/2 or abs(turtle.position()[1]) >
>> turtle.window_width()/2:
>>22 return "true"
>>23 else:
>>
>> TypeError: 'function' object is unsubscriptable
>>
>> but i can't really figure out where is the problem with this function
>> 'unsubscriptable';
>>
>
> This means you are trying to use [] on a function.
> eg you might be doing turtle.position[1] instead of turtle.position()[1]
>
> I can't see it in your code sample but I'd check carefully that your
> parens() all balance correctly and are in the right place...
>
> HTH,
>
>
> --
> Alan Gauld
> 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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor