Re: [Tutor] Why do I not get the same results for these two functions when I pass 7 as an argument?

2013-05-03 Thread Mark Lawrence

On 03/05/2013 07:10, Nonso Ibenegbu wrote:

Hello everyone,
Wonder if someone can help me understand why these two codes do not give
the same results for what looks essentially the same ("?") code. The
argument passed is
7.

def rental_car_cost(days):
 payment = days * 40
 if days >= 7:
 return payment - 50
 elif days >= 3 < 7:
 return payment - 20
 else:
 return payment

and...

def rental_car_cost(days):
 payment = days * 40
 if days >= 3 < 7:
 return payment - 20
 elif days >= 7:
 return payment - 50
 else:
 return payment



Python comparisons are chained.  days >= 3 < 7 is saying "days is 
greater or equal to 3 and 3 is less than 7".  You need 3 <= days < 7.


--
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] query from sqlalchemy returns AttributeError: 'NoneType' object

2013-05-03 Thread Alan Gauld

On 02/05/13 23:13, Karthik Sharma wrote:

This doesn't have much to do with learning Python and a lot to do with 
SqlAlchemy so you'd be better off asking on a SqlAlchemy forum I suspect.


However, some basic debugging investigation first may help your cause.
For example...


 Traceback (most recent call last):

...

   File "/home/karthik/pox/tutorial.py", line 118, in _handle_PacketIn
 self.act_like_switch(packet, packet_in)
   File "/home/karthik/pox/tutorial.py", line 86, in act_like_switch
 self.send_packet(packet_in.buffer_id,
packet_in.data,q_res.port_no, packet_in.in_port)
 AttributeError: 'NoneType' object has no attribute 'port_no'


Have you checked to see what q_res is supposed to be?
And where it comes from? And why it's apparently a NoneType?

Some print statements might be in order?

That will help anyone who can help you to so do.

--
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] Why do I not get the same results for these two functions when I pass 7 as an argument?

2013-05-03 Thread Kengesbayev, Askar

You need to change the condition statement:

  if days >= 3 < 7:  to  thisif days >= 3 and days < 7:

When it goes through the function it sees this statement days >= 3 as true and 
execute - payment -20.

Askar

From: Nonso Ibenegbu [mailto:jollyn...@gmail.com]
Sent: Friday, May 03, 2013 2:10 AM
To: Tutor@python.org
Subject: [Tutor] Why do I not get the same results for these two functions when 
I pass 7 as an argument?

Hello everyone,
Wonder if someone can help me understand why these two codes do not give the 
same results for what looks essentially the same ("?") code. The argument 
passed is
7.

def rental_car_cost(days):
payment = days * 40
if days >= 7:
return payment - 50
elif days >= 3 < 7:
return payment - 20
else:
return payment

and...

def rental_car_cost(days):
payment = days * 40
if days >= 3 < 7:
return payment - 20
elif days >= 7:
return payment - 50
else:
return payment
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why do I not get the same results for these two functions when I pass 7 as an argument?

2013-05-03 Thread Nonso Ibenegbu
Great! Many thanks for the help offered. The background of the problem is
that if I book a certain service for 1 or 2 days I pay $40 per day, if I
book 3,  4, 5 or 6 days I get $20 off but if I book above 7 days I get $50
off.

The first function does the job!

The second function breaks down when the argument is 7 or above.

Yet the only difference is that the condition "if days >= 7:" comes first
in the first function but comes second (as "elif days >= 7:") in the second
code.

I think essentially that the difference lies in the fact that the
conditions were swapped. But how are they different?


On Fri, May 3, 2013 at 8:07 AM, Mark Lawrence wrote:

> On 03/05/2013 07:10, Nonso Ibenegbu wrote:
>
>> Hello everyone,
>> Wonder if someone can help me understand why these two codes do not give
>> the same results for what looks essentially the same ("?") code. The
>> argument passed is
>> 7.
>>
>> def rental_car_cost(days):
>>  payment = days * 40
>>  if days >= 7:
>>  return payment - 50
>>  elif days >= 3 < 7:
>>  return payment - 20
>>  else:
>>  return payment
>>
>> and...
>>
>> def rental_car_cost(days):
>>  payment = days * 40
>>  if days >= 3 < 7:
>>  return payment - 20
>>  elif days >= 7:
>>  return payment - 50
>>  else:
>>  return payment
>>
>>
> Python comparisons are chained.  days >= 3 < 7 is saying "days is greater
> or equal to 3 and 3 is less than 7".  You need 3 <= days < 7.
>
> --
> 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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why do I not get the same results for these two functions when I pass 7 as an argument?

2013-05-03 Thread Alan Gauld

On 03/05/13 17:51, Nonso Ibenegbu wrote:


The second function breaks down when the argument is 7 or above.

Yet the only difference is that the condition "if days >= 7:" comes
first in the first function but comes second (as "elif days >= 7:") in
the second code.


Yes but that's because the other expression is nonsense (to python).

You cannot write

if days >= 3 < 7:

It doesn't make any sense.

What you mean is

if days >=3 and days < 7

If you fix that then the two functions will have similar logic.

In Python 9unlike most languages) you can abbreviate that but not the 
way you did it.

You need to use

if 3 <= days < 7:

Note that the variable is in the middle and the test for 3
is now reversed (3<=days). If in doubt use the double test
combined by 'and' as above.



--
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] Why do I not get the same results for these two functions when I pass 7 as an argument?

2013-05-03 Thread Danny Yoo
> elif days >= 3 < 7:

This condition here, as well as:


> if days >= 3 < 7:


this condition here, looks very suspicious.  Can you say what you
trying to express here?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] creating a corpus from a csv file

2013-05-03 Thread Treder, Robert
Hi, 
 
I'm very new to python and am trying to figure out how to make a corpus from a 
text file. I have a csv file (actually pipe '|' delimited) where each row 
corresponds to a different text document. Each row contains a communication 
note. Other columns correspond to categories of types of communications. I am 
able to read the csv file and print the notes column as follows: 
 
import csv
with open('notes.txt', 'rb') as infile:
reader = csv.reader(infile, delimiter = '|')
i = 0
for row in reader:
if i <= 25: print row[8]
i = i+1

I would like to convert this to a categorized corpus with some of the other 
columns corresponding to the categories. All of the columns are text (i.e., 
strings). I have looked for documentation on how to use csv.reader with 
PlaintextCorpusReader but have been unsuccessful in finding a  example similar 
to what I want to do. Can someone please help?  
 
Thanks, 
Bob




NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or 
views contained herein are not intended to be, and do not constitute, advice 
within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and 
Consumer Protection Act. If you have received this communication in error, 
please destroy all electronic and paper copies and notify the sender 
immediately. Mistransmission is not intended to waive confidentiality or 
privilege. Morgan Stanley reserves the right, to the extent permitted under 
applicable law, to monitor electronic communications. This message is subject 
to terms available at the following link: 
http://www.morganstanley.com/disclaimers. If you cannot access these links, 
please notify us by reply message and we will send the contents to you. By 
messaging with Morgan Stanley you consent to the foregoing.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating a corpus from a csv file

2013-05-03 Thread Alan Gauld

On 03/05/13 21:48, Treder, Robert wrote:


I'm very new to python and am trying to figure out how to

> make a corpus from a text file.

Hi, I for one have no idea what a corpus is or looks like
so you will need to help us out a little before we can help you.


I have a csv file (actually pipe '|' delimited) where each
row corresponds to a different text document.



Each row contains a communication note.

> Other columns correspond to categories of types of communications.


I am able to read the csv file and print the notes column as follows:

import csv
with open('notes.txt', 'rb') as infile:
 reader = csv.reader(infile, delimiter = '|')
 i = 0
 for row in reader:
 if i <= 25: print row[8]
 i = i+1


You don't need to manually manage 'i'.

you could do this instead:

with open('notes.txt', 'rb') as infile:
 reader = csv.reader(infile, delimiter = '|')
 for count, row in enumerate(reader):
 if count <= 25: print row[8]  # I assume indented?
 else: break   # save time if its a big file


I would like to convert this to a categorized corpus with

> some of the other columns corresponding to the categories.

You might be able to use a dictionary but for now
I'm still not clear what you mean. Can you show us
some sample input and output data?

> documentation on how to use csv.reader with PlaintextCorpusReader

never heard of the latter - is it an external module?

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] Why do I not get the same results for these two functions when I pass 7 as an argument?

2013-05-03 Thread Dave Angel

On 05/03/2013 12:51 PM, Nonso Ibenegbu wrote:

Great! Many thanks for the help offered. The background of the problem is
that if I book a certain service for 1 or 2 days I pay $40 per day, if I
book 3,  4, 5 or 6 days I get $20 off but if I book above 7 days I get $50
off.

The first function does the job!


By accident only.
The elif on the first one doesn't compare days to 7 at all.  So it's 
simply equivalent to:


elif days >= 3:

If you can't understand that, then comparing the two functions is useless.


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


[Tutor] changing char list to int list isn't working

2013-05-03 Thread Jim Mooney
I'm turning an integer into a string so I can make a list of separate
chars, then turn those chars back into individual ints, but the
resulting list still looks like string chars when I print it. What am
I doing wrong?


listOfNumChars = list(str(intNum))
for num in listOfNumChars:
num = int(num)

print(listOfNumChars)

# result of 455 entered is ['4', '5', '5']

--
Jim Mooney

“For anything that matters, the timing is never quite right, the
resources are always a little short, and the people who affect the
outcome are always ambivalent.”
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing char list to int list isn't working

2013-05-03 Thread eryksun
On Sat, May 4, 2013 at 12:13 AM, Jim Mooney  wrote:
> I'm turning an integer into a string so I can make a list of separate
> chars, then turn those chars back into individual ints, but the
> resulting list still looks like string chars when I print it. What am
> I doing wrong?
>
> listOfNumChars = list(str(intNum))
> for num in listOfNumChars:
> num = int(num)
>
> print(listOfNumChars)
>
> # result of 455 entered is ['4', '5', '5']

The body of your for loop only rebinds the loop variable. It's not
appending to a new list or modifying listOfNumChars. As to the latter
list, it's redundant since a string is iterable.

The following snippet creates the list [4, 5, 5]:

num = 455

numlist = []
for c in str(num):
numlist.append(int(c))

or using a list comprehension:

numlist = [int(c) for c in str(num)]

or using map:

numlist = list(map(int, str(num)))

iterators
http://docs.python.org/3/library/stdtypes.html#typeiter

built-in functions
http://docs.python.org/3/library/functions.html#iter
http://docs.python.org/3/library/functions.html#map

for statement
http://docs.python.org/3/reference/compound_stmts.html#the-for-statement

comprehensions
http://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing char list to int list isn't working

2013-05-03 Thread Steven D'Aprano

On 04/05/13 14:13, Jim Mooney wrote:

I'm turning an integer into a string so I can make a list of separate
chars, then turn those chars back into individual ints, but the
resulting list still looks like string chars when I print it. What am
I doing wrong?

listOfNumChars = list(str(intNum))


This creates a list of characters.



for num in listOfNumChars:
 num = int(num)


This walks over the list, setting the variable "num" to each character in turn, then inside 
the loop you set the variable "num" to the converted char->int. But num isn't linked to 
the list in any way -- num has no memory that the value it got came from a list. Reassigning num 
inside the loop doesn't touch the list in any way, so naturally the list doesn't change.



print(listOfNumChars)

# result of 455 entered is ['4', '5', '5']



To split a number into digits, the shortest way is to use a list comprehension:


digits = [int(c) for c in str(num)]


We can expand that list comp into a for-loop:


digits = []
for c in str(num):
digits.append(int(c))



Notice that there is no need to convert the string into a list. You can iterate 
over the characters of a string just fine.

If you prefer to create a list, then modify it in place, we can do this:


digits = list(str(num))
for position, char in enumerate(digits):
digits[position] = int(char)



Here we use enumerate() to iterate over pairs of (position, value) instead of 
just value:

py> list("abcd")
['a', 'b', 'c', 'd']
py> list(enumerate("abcd"))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]



Here's another way, using map:


digits = map(int, str(num))  # Python 2.x only

digits = list(map(int, str(num)))  # Python 3.x or better



Why the difference between Python 2.x and 3.x? In 2.x, map is "eager", it runs all the 
way through the string as soon as you call it, returning a list. In 3.x, map is "lazy", 
and only generates values when and as needed. By wrapping the map generator in a call to list, that 
forces it to run all the way through the string.



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