Re: [Tutor] String Attribute

2015-08-01 Thread ltc.hotspot







Sent from Surface





From: Alan Gauld
Sent: ‎Friday‎, ‎July‎ ‎31‎, ‎2015 ‎4‎:‎54‎ ‎PM
To: Tutor@python.org





On 31/07/15 19:57, ltc.hots...@gmail.com wrote:

> for line in fh:
>   line2 = line.strip()
>   line3 = line2.split()
>   line4 = line3[0]

You need to check that there actually is something
in the list to access. If you get a line with only
one word in it, or even a blank line this will fail.


→→Apparently, the data content in the  file is lost from  the address sort 
function to line2? :






In [46]: print line3
[]




In [47]: print line2.split()
[]




In [48]: print line2


In [49]: print line.strip()


In [50]: print fh





In [51]: print addresses
set(['1.0', 'sou...@collab.sakaiproject.org;', 'Jan', 'mail.umich.edu', 'Innocen
t', '0.', 'CMU', 'frankenstein.mail.umich.edu', '0.8475', 'from', 'source@co
llab.sakaiproject.org', '05', '<200801051412.m05eciah010...@nakamura.uits.iupui.
edu>', 'flawless.mail.umich.edu', '5', 'nakamura.uits.iupui.edu:', 'shmi.uhi.ac.
uk', '7bit', 'text/plain;', ';', 'Sat,', 'nakamu
ra.uits.iupui.edu', 'paploo.uhi.ac.uk', 'FROM', 'holes.mr.itd.umich.edu', '(from
', '', '[sakai]', 'stephen.marqu...@uct.ac.z
a', 'Sat'])




In [52]:




→ Latest code printout:




fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for line in fh:
 line2 = line.strip()
 line3 = line2.split()
 line4 = line3[1]
 addresses.add(line4)
 count = count + 1
print "There were", count, "lines in the file with From as the first word"
print addresses



>   addresses.add(line4)
>   count = count + 1
> print "There were", count, "lines in the file with From as the first word"

Despite what you print you don't know that its true anymore.
You have removed the code that tested for the first
word being "From". You should put that check back in your code.

> →I entered different index ranges from  [] to [5]

I'm not sure what [] means in this case? It should be a syntax error
as you show below.

> In [34]: print line3[]
>File "", line 1
>  print line3[]
>  ^
> SyntaxError: invalid syntax


  →→ OK

See, that's not an IndexError. They are different and have different 
causes. A syntax error means your code is not valid Python. An
IndexError means the code is valid but its trying to access
something that doesn't exist.


→→ OK



 →Question: I think the problem is in the placement of the address set: The 
addresses = set()?

No it has nothing to do with that. The set is not
involved in this operation at this point.

To debug these kinds of errors insert a print statement
above the error line. In this case:

print line3



→→ Read printout above 


That will show you what the data looks like and you can tell
whether line3[1] makes any kind of sense.




→→id.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Mark,

Desired output on execution of the script:

stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
zq...@umich.edu
rjl...@iupui.edu
zq...@umich.edu
rjl...@iupui.edu



[...]

Regards,
Hal

On Fri, Jul 31, 2015 at 5:21 PM, Ltc Hotspot  wrote:

> Mark:
> Is this any better, message sent from GMail?
> Regards,
> Hal
>
> On Fri, Jul 31, 2015 at 5:02 PM, Mark Lawrence 
> wrote:
>
>> On 31/07/2015 19:57, ltc.hots...@gmail.com wrote:
>>
>> I believe that this is the third time that you've been asked to do
>> something about the amount of whitespace that you're sending to this list.
>>
>> --
>> My fellow Pythonistas, ask not what our language can do for you, ask
>> what you can do for our language.
>>
>> Mark Lawrence
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Mark:
Is this any better, message sent from GMail?
Regards,
Hal

On Fri, Jul 31, 2015 at 5:02 PM, Mark Lawrence 
wrote:

> On 31/07/2015 19:57, ltc.hots...@gmail.com wrote:
>
> I believe that this is the third time that you've been asked to do
> something about the amount of whitespace that you're sending to this list.
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-08-01 Thread Alan Gauld

On 01/08/15 00:59, ltc.hots...@gmail.com wrote:


for line in fh:
   line2 = line.strip()
   line3 = line2.split()
   line4 = line3[0]


→→Apparently, the data content in the  file is lost from  the address sort 
function to line2? :


It is not lost, it is an empty line.


In [47]: print line2.split()
[]


split has returned no content.
The line must have been empty (or full of whitespace
which strip() removed).


In [48]: print line2
In [49]: print line.strip()


Again it shows an empty line.


In [51]: print addresses
set(['1.0', 'sou...@collab.sakaiproject.org;', 'Jan', 'mail.umich.edu', 'Innocen
t', '0.', 'CMU', 'frankenstein.mail.umich.edu', '0.8475', 'from', 'source@co
llab.sakaiproject.org', '05', '<200801051412.m05eciah010...@nakamura.uits.iupui.
edu>', 'flawless.mail.umich.edu', '5', 'nakamura.uits.iupui.edu:', 'shmi.uhi.ac.
uk', '7bit', 'text/plain;', ';', 'Sat,', 'nakamu
ra.uits.iupui.edu', 'paploo.uhi.ac.uk', 'FROM', 'holes.mr.itd.umich.edu', '(from
', '', '[sakai]', 'stephen.marqu...@uct.ac.z
a', 'Sat'])


But this is odd since it shows the set containing the full line which 
suggests you maybe did an add(line) instead of add(line4) at some point?



You have removed the code that tested for the first
word being "From". You should put that check back in your code.


If you do this it should fix the IndexError problem too,
since empty lines will not start with From

ie your loop should look like

for line in fh:
   if line.startswith('From'):
  # the loop body as it currently is


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] String Attribute

2015-08-01 Thread Cameron Simpson

On 31Jul2015 17:21, Ltc Hotspot  wrote:

Mark:
Is this any better, message sent from GMail?
Regards,
Hal


Looks better to me.

Cheers,
Cameron Simpson 


On Fri, Jul 31, 2015 at 5:02 PM, Mark Lawrence 
wrote:


On 31/07/2015 19:57, ltc.hots...@gmail.com wrote:

I believe that this is the third time that you've been asked to do
something about the amount of whitespace that you're sending to this list.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


[Tutor] OT: Test to see if ibisMail app is truly sending in plain text

2015-08-01 Thread boB Stepp
I apologize for the noise, but I felt it better to get this question answered 
definitively prior to posting questions from my iPad.

I am on a brief vacation and only brought my iPad.  I have been having a devil 
of a time searching the Internet for an iPad app that will truly send and 
display in plain text emails.  If someone would tell me if I have been 
successful or not, I would be very appreciative!  If successful, Python 
questions will soon follow.

Thanks!

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


Re: [Tutor] OT: Test to see if ibisMail app is truly sending in plain text

2015-08-01 Thread Mark Lawrence

On 01/08/2015 16:27, boB Stepp wrote:

I apologize for the noise, but I felt it better to get this question answered 
definitively prior to posting questions from my iPad.

I am on a brief vacation and only brought my iPad.  I have been having a devil 
of a time searching the Internet for an iPad app that will truly send and 
display in plain text emails.  If someone would tell me if I have been 
successful or not, I would be very appreciative!  If successful, Python 
questions will soon follow.

Thanks!

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



LGTM.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Tutor] String Attribute

2015-08-01 Thread Emile van Sebille

Hi Hal,

Seeing now that the output is only extracted from six address blocks, 
can you paste in the full contents of the file mbox-short.txt?  (or the 
first 5-10 address sets if this is only representative) I think if we 
have a better understanding of the structure of the content you're 
parsing it'll help us identify what the program will need to be prepared 
to handle.


Emile


On 7/31/2015 5:26 PM, Ltc Hotspot wrote:

Hi Mark,

Desired output on execution of the script:

stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
zq...@umich.edu
rjl...@iupui.edu
zq...@umich.edu
rjl...@iupui.edu



[...]

Regards,
Hal

On Fri, Jul 31, 2015 at 5:21 PM, Ltc Hotspot  wrote:


Mark:
Is this any better, message sent from GMail?
Regards,
Hal

On Fri, Jul 31, 2015 at 5:02 PM, Mark Lawrence 
wrote:


On 31/07/2015 19:57, ltc.hots...@gmail.com wrote:

I believe that this is the third time that you've been asked to do
something about the amount of whitespace that you're sending to this list.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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





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




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


[Tutor] How to design object interactions with an SQLite db?

2015-08-01 Thread boB Stepp
I have never written programs to interact with a db.  I have never written an 
OO program.  So this is getting interesting rather quickly!

As I continue to ponder my project design, I see many of the classes I wish to 
create map naturally to db tables.  For instance the class Student can 
potentially have many data attributes, which fit naturally into its own db 
table.

My current thoughts are that I have two main ways of approaching this:

1)  Create my various objects normally, but have their data attributes fetched 
through some sort of db manager class I would design.

2)  Use an ORM (Object-Relational Manager) such as SQLAlchemy to manage 
interactions between my objects and the SQLite db.

Both routes will be quite educational for me.  Option (2), if I am 
understanding things correctly, would be more likely to make it relatively easy 
to change from SQLite to a more sophisticated server-based db in the future 
incarnations of this project.

Thoughts?


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


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-08-01 Thread Steven D'Aprano
Hi Deb,

On Fri, Jul 31, 2015 at 10:20:50AM -0700, D Wyatt wrote:
> 
> >
> > This matches the precedence rules for written mathematics, where negation
> > has a lower precedence than exponentiation as well.  So python is doing the
> > correct thing here mathematically.  See, for example,
> > http://mathforum.org/library/drmath/view/53194.html
[...]
> That is just so counterintuitive, and I've never run into this in any
> mathematics I have taken.  Now I'm going to have to research this
> further, from a mathematics standpoint.

You have inspired me to do a bit more research.

I've found at least three programming languages that behave as you 
expect: the programming language "bc", Xion, and Microsoft Excel 
formulae. For instance, Xion evaluates -3^2 as 9.

And proving that you're damned if you do and damned if you don't, here 
is a bug report filed against Excel, stating that -2^2 returns 4 instead 
of the expected result -4:

https://support.microsoft.com/en-gb/kb/kbview/132686

My favourite scientific calculator, the HP 48GX, uses Reverse Polish 
Notation by default and so the question of operator precedence doesn't 
come up. But it also has an optional algebraic mode, and '-2^2' 
evaulates as -4.

Javascript doesn't have a power operator. Neither does C, one of the 
most widely-used languages in the world.

Ruby agrees with Python:

irb(main):001:0> -3**2
=> -9

According to Wikipedia:

https://en.wikipedia.org/wiki/Order_of_operations

some scientific journals now treat multiplication as a higher precedence 
than division with a / so that 1/2x equals 1/(2x), not (1/2)x.

There's an interesting study done here:

"Developer beliefs about binary operator precedence"

http://www.knosof.co.uk/cbook/accu06.html

which suggests that even professional programmers get operator 
precedence wrong at a high rate. (The study found a 33% error rate.)

The bottom line is, there is no universal right or wrong answer for the 
precedence rules for operators, although some rules are less right than 
others.



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


Re: [Tutor] OT: Test to see if ibisMail app is truly sending in plain text

2015-08-01 Thread Steven D'Aprano
On Sat, Aug 01, 2015 at 10:27:27AM -0500, boB Stepp wrote:

> I apologize for the noise, but I felt it better to get this question 
> answered definitively prior to posting questions from my iPad.
> 
> I am on a brief vacation and only brought my iPad.  I have been having 
> a devil of a time searching the Internet for an iPad app that will 
> truly send and display in plain text emails.  If someone would tell me 
> if I have been successful or not, I would be very appreciative!  If 
> successful, Python questions will soon follow.

Yes, plain text.

According to the full headers of your email, you are sending from:

X-Mailer: ibisMail for iPhone ver.4.0.9

with content:

Content-Type: text/plain; charset="us-ascii"


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


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-08-01 Thread Emile van Sebille

On 8/1/2015 9:43 AM, Steven D'Aprano wrote:

The bottom line is, there is no universal right or wrong answer for the
precedence rules for operators, although some rules are less right than
others.


My bottom line is that the liberal use of parens reconciles them all, 
particularly for any humans reading the code that need to change it.


Emile





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


[Tutor] FETCH URLs FROM WEBSITE

2015-08-01 Thread Gaurav Lathwal
Hello everyone, I am new to Python, so please forgive me if my question is
too dumb.
I want to write a script that automatically downloads all the videos hosted
on this site :-

http://www.toonova.com/batman-beyond

Now, the problem I am having is, I am unable to fetch the video urls of all
the videos.
I mean I can manually fetch the video urls using the chrome developer's
console, but it's too time consuming.
Is there any way to just fetch all the video urls using BeautifulSoup ?

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


Re: [Tutor] OT: Test to see if ibisMail app is truly sending in plain text

2015-08-01 Thread Steven D'Aprano
On Sat, Aug 01, 2015 at 04:53:45PM +0100, Mark Lawrence wrote:

> LGTM.

Let's Get The Money?

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


Re: [Tutor] OT: Test to see if ibisMail app is truly sending in plain text

2015-08-01 Thread boB Stepp
Well, in replying to your message, Steve, I see that this is not the perfect 
iPad solution.  After hitting reply all, it failed to give attribution to your 
comments.  But as I have not been able to find anything better, I guess I can 
manually add attribution names, and make this work on the rare times when I 
must communicate with programming fora from my iPad.  At least you have 
demonstrated that it is truly in plain text!

I show in its entirety what I see just for the record:


> On Sat, Aug 01, 2015 at 10:27:27AM -0500, boB Stepp wrote:
> 
> > I apologize for the noise, but I felt it better to get this question 
> > answered definitively prior to posting questions from my iPad.
> > 
> > I am on a brief vacation and only brought my iPad.  I have been having 
> > a devil of a time searching the Internet for an iPad app that will 
> > truly send and display in plain text emails.  If someone would tell me 
> > if I have been successful or not, I would be very appreciative!  If 
> > successful, Python questions will soon follow.
> 
> Yes, plain text.
> 
> According to the full headers of your email, you are sending from:
> 
> X-Mailer: ibisMail for iPhone ver.4.0.9
> 
> with content:
> 
> Content-Type: text/plain; charset="us-ascii"
> 
> 
> -- 
> Steve
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 



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


Re: [Tutor] How to design object interactions with an SQLite db?

2015-08-01 Thread Alan Gauld

On 01/08/15 17:34, boB Stepp wrote:

I have never written programs to interact with a db.  I have never written an 
OO program.


The mapping opf OOP to Relational DB is one of the on going debates
in OOP land, and has been since I started with OOP in 1984...


1)  Create my various objects normally, but have their data attributes

> fetched through some sort of db manager class I would design.

Personally I tend to create a load() method that is used like a 
constructor but fetches the data from the database


myObj = MyClass().load(ID)

Where load() returns self if successful.
Alternatively in Python you could define the ID as a parameter of init 
with a None default


def __init__(self,att1=None,att2=SomeDefault,...,ID=None):
if ID
   self.load(ID)
else:
   self.att1 = att1 # etc...

Its conceptually simple and gives you full control of the SQL, but 
things like inheritance can get tricky.



2)  Use an ORM (Object-Relational Manager) such as SQLAlchemy to manage

> interactions between my objects and the SQLite db.

This is ultimately the best solution since a lot of the hard work has 
been done for you and it can handle (I assume) inheritance etc. But

it's yet another framework to learn.


Option (2), if I am understanding things correctly, would be more

> likely to make it relatively easy to change from SQLite to a more

sophisticated server-based db in the future incarnations of

> this project.

Possibly, although IMHO its rarely seamless. But YMMV :-)

Equally, its rarely a huge job, even SQLite to Oracle say,
is not a massive hit - especially if you know which server
you will aim for because SQLite SQL has lots of options
for types that are compatible with various servers.
So strings can be represented as TEXT, VARCHAR(N), CHARACTER(N) NCHAR(N) 
etc depending on what server database you are trying

to emulate(or are accustomed to. Similarly with integers
(INT, INTEGER, INT2, etc). Look up Affinity types in the
SQLite docs.

http://sqlite.org/datatype3.html#affinity

The more 'clever' stuff you put in the harder the translation.
Stick to standard SQL and it should be fairly painless.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] How to design object interactions with an SQLite db?

2015-08-01 Thread Mark Lawrence

On 01/08/2015 17:34, boB Stepp wrote:

I have never written programs to interact with a db.  I have never written an 
OO program.  So this is getting interesting rather quickly!

As I continue to ponder my project design, I see many of the classes I wish to 
create map naturally to db tables.  For instance the class Student can 
potentially have many data attributes, which fit naturally into its own db 
table.

My current thoughts are that I have two main ways of approaching this:

1)  Create my various objects normally, but have their data attributes fetched 
through some sort of db manager class I would design.


This is the interface that I use on my own personal, mission critical, 
cashflow forecast.


con = sqlite3.connect(sqliteFileName, 
detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)

con.row_factory = sqlite3.Row

What this gives you is documented here 
https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.row_factory 
and here https://docs.python.org/3/library/sqlite3.html#sqlite3.Row


I also create views in the database and select from them rather than do 
the join within code.


All very simple but very effective.



2)  Use an ORM (Object-Relational Manager) such as SQLAlchemy to manage 
interactions between my objects and the SQLite db.


Take a look at the comparison here 
http://www.pythoncentral.io/sqlalchemy-vs-orms/  It mentions peewee 
which I played with in my cashflow forecast but found it to be overkill 
for my simple needs. I've also heard good things about Storm and 
PonyORM.  SQLAlchemy and SQLObject are the big names, the rest I can't 
comment on.




Both routes will be quite educational for me.  Option (2), if I am 
understanding things correctly, would be more likely to make it relatively easy 
to change from SQLite to a more sophisticated server-based db in the future 
incarnations of this project.

Thoughts?


Start prototyping with my simple approach.  If that works for you just 
stick with it.  If not try one of the simpler ORMs like peewee or 
PonyORM.  Stick with it if it's good enough, else go for one of the 
heavyweights.  I prefer this approach as it's fairly easy in Python to 
throw something away and have another go, and I like to keep things as 
simple and straight forward as possible.  I'm sure others would take the 
opposite approach and start with a heavyweight.  For you I'd suggest the 
best path to take depends on the number of tables you'll actually be 
creating.  If it's small cutting over from SQLite to some other db 
should be relatively easy.  If it's medium to large perhaps you're 
better off starting off with the heavyweight of your choice at the 
start.  I do know one thing, you won't find out until you try it :)



--
boB


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Tutor] FETCH URLs FROM WEBSITE

2015-08-01 Thread Alan Gauld

On 01/08/15 11:48, Gaurav Lathwal wrote:


I want to write a script that automatically downloads all the videos hosted
on this site :-

http://www.toonova.com/batman-beyond


The first thing to ask is whether they allow robotic downloads
from the site. If they are funded by advertising then they may
not permit it and it would be self defeating to try since you
would be helping close down your source!


Now, the problem I am having is, I am unable to fetch the video urls of all
the videos.


I assume you want to fetch the videos not just the URLs?
Fetching the URLs is easy enough and I doubt the site would object
too strongly. But fetching the videos is much harder since:

a) The page you give only has links to separate pages for each
   video.
b) The separate pages have a download link which is to a
   tiny url which may well change.
c) The separate page is not static HTML (or even server
   generated HTML) it is created in part by the Javascript
   code when the page loads. That means it is very likely to
   change on each load (possibly deliberately so to foil robots!)


I mean I can manually fetch the video urls using the chrome developer's
console, but it's too time consuming.
Is there any way to just fetch all the video urls using BeautifulSoup ?


It's probably possible for a one-off, but it may not work reliably for 
future use. Assuming the site allows it in the first place.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] FETCH URLs FROM WEBSITE

2015-08-01 Thread Válas Péter
2015-08-01 12:48 GMT+02:00 Gaurav Lathwal :

> Hello everyone, I am new to Python, so please forgive me if my question is
> too dumb.
> I want to write a script that automatically downloads all the videos hosted
> on this site :-
>
> http://www.toonova.com/batman-beyond
>
> Now, the problem I am having is, I am unable to fetch the video urls of all
> the videos.
> I mean I can manually fetch the video urls using the chrome developer's
> console, but it's too time consuming.
> Is there any way to just fetch all the video urls using BeautifulSoup ?
>

I am not familiar wit BS, I like to arrange things myself.
The first step is always to have a look at the source by naked eye and try
to guess a rule.
You should also observe the encoding in the page header, we will need it
(in this case UTF-8).

If I want to download things only once, and the rule is strict, sometimes I
simply generate the links by MS Excel with string functions. :-)
A more powerful way is to learn regular expressions which are miracles of
programmers" world when it turns to text processing.

In this case the base of your program will be (save it in an editor with
utf-8 encoding):

# -*- coding: UTF-8 -*-
"""
Extract video links from http://www.toonova.com/batman-beyond
Python 3 syntax
"""

import urllib.request, re, codecs

batmanpage = 'http://www.toonova.com/batman-beyond'
videolinkpattern = re.compile('http://www
\.toonova\.com/batman\-beyond.*?episode-\d+')
# Either has a season string before episode or not

#Opening the page
page = urllib.request.urlopen(batmanpage).read().decode('utf-8')
for link in videolinkpattern.finditer(page):
url = link.group()
print(url)

Now you have them in url and you may download them in the loop after or
instead of print.
Not that there is a 2nd page with 2 links only, so the simplest thing is to
download those 2 manually. If they were more, you could arrange an outer
loop for web pages, but in this case this is not neccessary.
The program won't work in Python 2 this way, because it has a different
urllib module.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FETCH URLs FROM WEBSITE

2015-08-01 Thread Válas Péter
2015-08-01 19:42 GMT+02:00 Alan Gauld :


> c) The separate page is not static HTML (or even server
>generated HTML) it is created in part by the Javascript
>code when the page loads. That means it is very likely to
>change on each load (possibly deliberately so to foil robots!)
>

I didn't go far enough to notice this. :-( So my previous script is just an
outline, how to approach such tasks, but not a whole solution.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Alan,

There is an indent message in the revised code.
Question: Where should I indent the code line for the loop?

View the revised codes with loop indents, below:

--->Revised Code v.2  wo/indent from lines 8-12:

fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for line in fh:
if line.startswith('From'):
line2 = line.strip()
line3 = line2.split()
line4 = line3[1]
addresses.add(line)
count = count + 1
print "There were", count, "lines in the file with From as the first word"
print addresses


---> Message output reads:
In [62]: %run _8_5_v_25.py
  File "C:\Users\vm\Desktop\apps\docs\Python\_8_5_v_25.py", line 8
line2 = line.strip()
^
IndentationError: expected an indented block


--->Revised Code v.3  w/indent from lines 8-12:

fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for line in fh:
if line.startswith('From'):
line2 = line.strip()
line3 = line2.split()
line4 = line3[1]
addresses.add(line)
count = count + 1
print "There were", count, "lines in the file with From as the first word"
print addresses

---> Message output reads:

...pi/component/src/java/org/sakaiproject/component/util/RecordWriter.java\n',
'Dat
e: 2008-01-04 11:09:12 -0500 (Fri, 04 Jan 2008)\n', '\t 4 Jan 2008 11:12:30
-050
0\n', '\tby nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11/Submit) id
m03M5Ea
7005273\n', 'New Revision: 39755\n', 'X-DSPAM-Processed: Thu Jan  3
16:23:48 200
8\n', 'Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39754\n',
'
\t Fri, 04 Jan 2008 11:35:08 -0500\n', '\tfor <
sou...@collab.sakaiproject.org>;
Fri, 4 Jan 2008 04:05:54 -0500\n', 'Received: from carrie.mr.itd.umich.edu
(carr
ie.mr.itd.umich.edu [141.211.93.152])\n', 'Message-ID:
<200801042044.m04Kiem3007
8...@nakamura.uits.iupui.edu>\n', '\tfor ;
Fri,
4 Jan 2008 16:36:37 + (GMT)\n', '\t Fri, 04 Jan 2008 15:03:18 -0500\n',
'\tF
ri,  4 Jan 2008 16:11:31 + (GMT)\n', '  by paploo.uhi.ac.uk
(JAMES S
MTP Server 2.1.3) with SMTP ID 960\n', 'From lo...@media.berkeley.edu Fri
Jan  4
 18:10:48 2008\n', '  Thu, 3 Jan 2008 22:06:34 + (GMT)\n',
'\tfor so
u...@collab.sakaiproject.org; Fri, 4 Jan 2008 10:15:57 -0500\n', 'Received:
from
 eyewitness.mr.itd.umich.edu (eyewitness.mr.itd.umich.edu
[141.211.93.142])\n',
'Subject: [sakai] svn commit: r39743 -
gradebook/branches/oncourse_2-4-2/app/ui/
src/java/org/sakaiproject/tool/gradebook/ui\n', 'Date: 2008-01-04 10:15:54
-0500
 (Fri, 04 Jan 2008)\n', 'New Revision: 39761\n', '\tBY
salemslot.mr.itd.umich.ed
u ID 477DF74E.49493.30415 ; \n', 'X-DSPAM-Processed: Sat Jan  5 09:14:16
2008\n'
, '\tfor ; Fri,  4 Jan 2008 21:10:14 +
(GMT)
\n', '\tby paploo.uhi.ac.uk (Postfix) with ESMTP id 88598BA5B6;\n',
'X-DSPAM-Pro
cessed: Fri Jan  4 04:07:34 2008\n', 'r39558 | h...@iupui.edu | 2007-12-20
15:25:
38 -0500 (Thu, 20 Dec 2007) | 3 lines\n', 'From gsil...@umich.edu Fri Jan
4 11:
10:22 2008\n', '\tby nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11)
with ESM
TP id m04N8vHG008127\n', '\tSat,  5 Jan 2008 14:10:05 + (GMT)\n', '\tby
naka
mura.uits.iupui.edu (8.12.11.20060308/8.12.11/Submit) id m049W2i5006493\n',
'\tT
hu,  3 Jan 2008 22:06:57 + (GMT)\n', '  Fri, 4 Jan 2008
19:46:50 +00
00 (GMT)\n', 'Message-ID: <
200801041609.m04g9eux007...@nakamura.uits.iupui.edu>\
n', 'Subject: [sakai] svn commit: r39756 - in
component/branches/SAK-12166/compo
nent-api/component/src/java/org/sakaiproject/component: impl
impl/spring/support
 impl/spring/support/dynamic impl/support util\n',
'site/trunk/site-tool/tool/sr
c/bundle/admin.properties\n', 'Author: gopal.ramasammyc...@gmail.com\n',
'From d
avid.horw...@uct.ac.za Fri Jan  4 04:33:44 2008\n', '\tby
nakamura.uits.iupui.ed
u (8.12.11.20060308/8.12.11) with ESMTP id m04E3pQS006928\n', '\tfor
source@coll
ab.sakaiproject.org; Fri, 4 Jan 2008 16:09:02 -0500\n', 'X-DSPAM-Processed:
Fri
Jan  4 09:05:31 2008\n', '\t 4 Jan 2008 16:10:33 -0500\n', '\tfor
source@collab.
sakaiproject.org; Fri, 4 Jan 2008 11:09:14 -0500\n', 'merge fix to SAK-9996
into
 2-5-x branch: svn merge -r 39687:39688
https://source.sakaiproject.org/svn/site
-manage/trunk/\n', 'Subject: [sakai] svn commit: r39751 - in
podcasts/branches/s
akai_2-5-x/podcasts-app/src/webapp: css images podcasts\n', 'Subject:
[sakai] sv
n commit: r39757 - in assignment/trunk:
assignment-impl/impl/src/java/org/sakaip
roject/assignment/impl assignment-tool/tool/src/webapp/vm/assignment\n',
'From w
agne...@iupui.edu Fri Jan  4 10:38:42 2008\n', 'Date: 2008-01-03 17:16:39
-0500
(Thu, 03 Jan 2008)\n', '  by paploo.uhi.ac.uk (JAMES SMTP Server
2.1.3)
with SMTP ID 906\n', 'U
podcasts/podcasts-app/src/webapp/podcasts/podOptions.
jsp\n', 'svn merge -c 35014
https://source.sakaiproject.org/svn/gradebook/trunk\
n', 'Received: from galaxyquest.mr.itd.umich.edu (
gala

Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Emile,


I just noticed there are duplicates

Here is the complete line output as requested, below:

stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
zq...@umich.edu
rjl...@iupui.edu
zq...@umich.edu
rjl...@iupui.edu
c...@iupui.edu
c...@iupui.edu
gsil...@umich.edu
gsil...@umich.edu
zq...@umich.edu
gsil...@umich.edu
wagne...@iupui.edu
zq...@umich.edu
antra...@caret.cam.ac.uk
gopal.ramasammyc...@gmail.com
david.horw...@uct.ac.za
david.horw...@uct.ac.za
david.horw...@uct.ac.za
david.horw...@uct.ac.za
stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
lo...@media.berkeley.edu
r...@media.berkeley.edu
c...@iupui.edu
c...@iupui.edu
c...@iupui.edu
There were 27 lines in the file with From as the first word


Hal

On Sat, Aug 1, 2015 at 9:18 AM, Emile van Sebille  wrote:

> Hi Hal,
>
> Seeing now that the output is only extracted from six address blocks, can
> you paste in the full contents of the file mbox-short.txt?  (or the first
> 5-10 address sets if this is only representative) I think if we have a
> better understanding of the structure of the content you're parsing it'll
> help us identify what the program will need to be prepared to handle.
>
> Emile
>
>
>
> On 7/31/2015 5:26 PM, Ltc Hotspot wrote:
>
>> Hi Mark,
>>
>> Desired output on execution of the script:
>>
>> stephen.marqu...@uct.ac.za
>> lo...@media.berkeley.edu
>> zq...@umich.edu
>> rjl...@iupui.edu
>> zq...@umich.edu
>> rjl...@iupui.edu
>>
>>
>>
>> [...]
>>
>> Regards,
>> Hal
>>
>> On Fri, Jul 31, 2015 at 5:21 PM, Ltc Hotspot 
>> wrote:
>>
>> Mark:
>>> Is this any better, message sent from GMail?
>>> Regards,
>>> Hal
>>>
>>> On Fri, Jul 31, 2015 at 5:02 PM, Mark Lawrence 
>>> wrote:
>>>
>>> On 31/07/2015 19:57, ltc.hots...@gmail.com wrote:

 I believe that this is the third time that you've been asked to do
 something about the amount of whitespace that you're sending to this
 list.

 --
 My fellow Pythonistas, ask not what our language can do for you, ask
 what you can do for our language.

 Mark Lawrence

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


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


[Tutor] infix to postfix exponent handling

2015-08-01 Thread Quiles, Stephanie
My assignment calls for the program to be edited to handle the “^” symbol. the 
hint is that it should be done with just one line of code. Here is the 
assignment:
Modify the infix-to-postfix algorithm to handle exponentiation. Use the ^ 
symbol as the input token for testing.

Q-14: Modify the infixToPostfix function so that it can convert the following 
expression: 5 * 3 ^ (4 - 2)


Here is the code :

class Stack:
def __init__(self):
self.items = []

def isEmpty(self):
return self.items == []

def push(self, item):
self.items.insert(0,item)

def pop(self):
return self.items.pop(0)

def peek(self):
return self.items[0]

def size(self):
return len(self.items)


def infixToPostfix(infixexpr):
prec = {}
prec["^"] = 3
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = infixexpr.split()

for token in tokenList:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
postfixList.append(token)
elif token == '(':
opStack.push(token)
elif token == ')':
topToken = opStack.pop()
while topToken != '(':
postfixList.append(topToken)
topToken = opStack.pop()
else:
while (not opStack.isEmpty()) and \
   (prec[opStack.peek()] >= prec[token]):
  postfixList.append(opStack.pop())
opStack.push(token)

while not opStack.isEmpty():
postfixList.append(opStack.pop())
return " ".join(postfixList)

print(infixToPostfix("5 * 3 ^ (4 - 2)"))
print(infixToPostfix("( A + B ) * C - ( D - E ) * ( F + G )”))

this is the lien that i added:

prec["^"] = 3

i also replaced the infixtopostfix to the problem:

("5 * 3 ^ (4 - 2)”))

here is the error I am getting :

Traceback (most recent call last):
  File "/Users/stephaniequiles/Downloads/Listings/listing_3_7.py", line 53, in 

print(infixToPostfix("5 * 3 ^ (4 - 2)"))
  File "/Users/stephaniequiles/Downloads/Listings/listing_3_7.py", line 45, in 
infixToPostfix
(prec[opStack.peek()] >= prec[token]):
KeyError: '(4'

Process finished with exit code 1

Please advise. not sure where i am failing with this

Thanks!!
Stephanie

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


Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Everyone:


Let me repost the question:

You will parse the From line using split() and print out the second word in
the line (i.e. the entire address of the person who sent the message). Then
print out a count at the end.

*Hint:* make sure not to include the lines that start with 'From:'.

You can download the sample data at
http://www.pythonlearn.com/code/mbox-short.txt



Regards,

Hal

On Sat, Aug 1, 2015 at 9:18 AM, Emile van Sebille  wrote:

> Hi Hal,
>
> Seeing now that the output is only extracted from six address blocks, can
> you paste in the full contents of the file mbox-short.txt?  (or the first
> 5-10 address sets if this is only representative) I think if we have a
> better understanding of the structure of the content you're parsing it'll
> help us identify what the program will need to be prepared to handle.
>
> Emile
>
>
>
> On 7/31/2015 5:26 PM, Ltc Hotspot wrote:
>
>> Hi Mark,
>>
>> Desired output on execution of the script:
>>
>> stephen.marqu...@uct.ac.za
>> lo...@media.berkeley.edu
>> zq...@umich.edu
>> rjl...@iupui.edu
>> zq...@umich.edu
>> rjl...@iupui.edu
>>
>>
>>
>> [...]
>>
>> Regards,
>> Hal
>>
>> On Fri, Jul 31, 2015 at 5:21 PM, Ltc Hotspot 
>> wrote:
>>
>> Mark:
>>> Is this any better, message sent from GMail?
>>> Regards,
>>> Hal
>>>
>>> On Fri, Jul 31, 2015 at 5:02 PM, Mark Lawrence 
>>> wrote:
>>>
>>> On 31/07/2015 19:57, ltc.hots...@gmail.com wrote:

 I believe that this is the third time that you've been asked to do
 something about the amount of whitespace that you're sending to this
 list.

 --
 My fellow Pythonistas, ask not what our language can do for you, ask
 what you can do for our language.

 Mark Lawrence

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


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


[Tutor] email validation

2015-08-01 Thread Quiles, Stephanie
Hello All, 

I have a python assignment. I have to make sure that when user inputs email 
that the program verifies that the address as a @ and a “.” in the entry or 
else return an invalid email error. 
A Very rudimentary form of email validation. i cannot get the program to work. 
Here is what i have so far: 

import pickle


def main():
cont = True
emails = open_existing_file()
print(emails)

# Get data...
while cont:
name = input("Enter your name :")
email1 = input("Enter your email address :")
email2 = input("Enter alternate email address :")
phone  = input("Enter your phone number :")
contactlist = [email1,email2,phone]
emails[name] = contactlist
c = input("Enter another? [y]/n :")
if c == 'n' or c == 'N':
cont = False

def email1():
if '@' not in email and '.' not in email:
print('email needs @ and . at the same time')
# Save data...
outfile = open("emails.dat","wb")
pickle.dump(emails,outfile)
outfile.close
print("Your data has been saved to emails.dat")

def open_existing_file():
# returns an empty dictionary or one that has data from a file
emails = {}
# Load the dictionary
try:
infile = open("emails.dat","rb")
emails = pickle.load(infile)
infile.close()
except:
print("No file to open. Starting with no data.")
return emails

main()

Here is the error message : 

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 
/Users/stephaniequiles/Downloads/emailsupdate.py
Traceback (most recent call last):
  File "/Users/stephaniequiles/Downloads/emailsupdate.py", line 42, in 
main()
  File "/Users/stephaniequiles/Downloads/emailsupdate.py", line 6, in main
emails = open_existing_file()
UnboundLocalError: local variable 'open_existing_file' referenced before 
assignment

Process finished with exit code 1

not sure why it is not recognizing that the open_existing_file() function needs 
to be returned to the “emails” variable? I am guessing it has to do with my 
syntax? any suggestions, please? 

Thank you 

Stephanie Quiles 



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


Re: [Tutor] String Attribute

2015-08-01 Thread Alan Gauld

On 01/08/15 19:48, Ltc Hotspot wrote:

There is an indent message in the revised code.
Question: Where should I indent the code line for the loop?


Do you understand the role of indentation in Python?
Everything in the indented block is part of the structure,
so you need to indent everything that should be executed
as part of the logical block.


fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for line in fh:
 if line.startswith('From'):
 line2 = line.strip()
 line3 = line2.split()
 line4 = line3[1]
 addresses.add(line)
 count = count + 1


Everything after the if line should be indented an extra level
because you only want to do those things if the line
startswith From.

And note that, as I suspected, you are adding the whole line
to the set when you should only be adding the address.
(ie line4). This would be more obvious if you had
used meaningful variable names such as:

strippedLine = line.strip()
tokens = strippedLine.split()
addr = tokens[1]
addresses.add(addr)

PS.
Could you please delete the extra lines from your messages.
Some people pay by the byte and don't want to receive kilobytes
of stuff they have already seen multiple times.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] infix to postfix exponent handling

2015-08-01 Thread Danny Yoo
> i also replaced the infixtopostfix to the problem:
>
> ("5 * 3 ^ (4 - 2)”))
>
> here is the error I am getting :
>
> Traceback (most recent call last):
>   File "/Users/stephaniequiles/Downloads/Listings/listing_3_7.py", line 53, 
> in 
> print(infixToPostfix("5 * 3 ^ (4 - 2)"))
>   File "/Users/stephaniequiles/Downloads/Listings/listing_3_7.py", line 45, 
> in infixToPostfix
> (prec[opStack.peek()] >= prec[token]):
> KeyError: '(4'
>

Ok.  The immediate problem you are seeing here actually has nothing to do with
exponentiation, but with a lower-level issue.


Answering the following two questions should help you figure out
what's going on.

Given the string:

"5 * 3 ^ (4 - 2)"

1.  What do you expect the list of tokens to be?  Say concretely what
you expect it to be.

2.  What does your program think the list of tokens is?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] email validation

2015-08-01 Thread Válas Péter
Hi Stephanie,

the function should be defined first, and used after. So put it before
main().

Also, "if '@' not in email and '.' not in email:" seems to be erroneous.
You want both be present; this is an AND if you state it and becomes OR
when you deny.
if '@' not in email or '.' not in email:
In the present form your if comes true only when none of @ and . occur in
the string.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] email validation

2015-08-01 Thread Danny Yoo
All your function definitions should be defined with 'def' at the
leftmost margin.

However, the line in your program that starts with "def
open_existing_file()..." is not flush with the margin.  Python has,
subsequently, thought that the definition of the function is scoped
locally.  Move the beginning of the definition line "def
open_existing_file()" so that it's not indented.

Conceptually, what's happening is that you've accidentally written a
locally-scoped function definition, which beginner programs do not
typically do.  The following program demonstrates:

---
def bigFunction():
def nestedFunction():
print("nested")

## at this point forward, nestedFunction can be accessed only
## here:
nestedFunction()
nestedFunction()

## Try calling bigFunction:
bigFunction()
## Try calling nestedFunction (and expect it to fail!)
nestedFunction()
---

Try calling bigFunction() from the toplevel.  Then try calling
nestedFunction() directly from the toplevel.  You'll find that you
can't: the definition of nestedFunction is scoped so that its
accessible only after the point commented.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] email validation

2015-08-01 Thread Danny Yoo
On Sat, Aug 1, 2015 at 2:03 PM, Válas Péter  wrote:
> Hi Stephanie,
>
> the function should be defined first, and used after. So put it before
> main().



It's perfectly legal and ok to say:

###
def main():
 callHelper()

def callHelper():
 print("I am the helper")

main()
###



Rather, the problem is due to putting the helper function accidentally
nested *within* main:


def main():
 callHelper()

def callHelper():
 print("I am the helper but can't be called until after the definition")

main()
#



One technical way to "fix" this is to move it up a bit:

#
def main():
def callHelper():
 print("I am the helper but can't be called until after the definition")

callHelper()

main()
#



But this is usually unsatisfactory because we can't then access
callHelper from outside.  There can be valid reasons to hide function
definitions at times, but this isn't one of those situations.


Válas's suggestion, to move the helper's definition above, does make sense:

#
def callHelper():
 print("I am the helper but can't be called until after the definition")

def main():
callHelper()

main()
#

but a key point needs to be made: don't just move it *up*, but move it *out*.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-08-01 Thread Emile van Sebille

On 8/1/2015 12:00 PM, Ltc Hotspot wrote:

Hi Everyone:


Let me repost the question:

You will parse the From line using split() and print out the second word in
the line (i.e. the entire address of the person who sent the message). Then
print out a count at the end.

*Hint:* make sure not to include the lines that start with 'From:'.

You can download the sample data at
http://www.pythonlearn.com/code/mbox-short.txt


Cool - thanks.  That's an mbox file.

Can you explain the apparent dichotomy of the question directing you to 
'parse the from line' and the hint?  I'm going to guess they mean that 
you're not to print that line in the output?  Aah, I see -- there're two 
different lines that start From -- both with and without a trailing 
colon.  So then, we can split on 'From ' and recognizing the split eats 
the split-on portion


>>> '1234567'.split('4')
['123', '567']

... and leaves an empty entry when splitting on the first characters of 
the line


>>> '1234567'.split('1')
['', '234567']

... we get to:

for addr in [ fromline.split()[0]
  for fromline in mbox.split('From ')
  if fromline ]:
print addr

stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
zq...@umich.edu
rjl...@iupui.edu
zq...@umich.edu
rjl...@iupui.edu
c...@iupui.edu
gsil...@umich.edu
gsil...@umich.edu
zq...@umich.edu
gsil...@umich.edu
wagne...@iupui.edu
zq...@umich.edu
antra...@caret.cam.ac.uk
gopal.ramasammyc...@gmail.com
david.horw...@uct.ac.za
david.horw...@uct.ac.za
stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
lo...@media.berkeley.edu
r...@media.berkeley.edu
c...@iupui.edu
c...@iupui.edu
c...@iupui.edu
>>>



Emile

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


Re: [Tutor] email validation

2015-08-01 Thread Emile van Sebille

On 8/1/2015 10:54 AM, Quiles, Stephanie wrote:

Hello All,

I have a python assignment. I have to make sure that when user inputs email 
that the program verifies that the address as a @ and a “.” in the entry or 
else return an invalid email error.
A Very rudimentary form of email validation. i cannot get the program to work. 
Here is what i have so far:

import pickle


def main():
 cont = True
 emails = open_existing_file()
 print(emails)

 # Get data...
 while cont:
 name = input("Enter your name :")
 email1 = input("Enter your email address :")
 email2 = input("Enter alternate email address :")
 phone  = input("Enter your phone number :")
 contactlist = [email1,email2,phone]
 emails[name] = contactlist
 c = input("Enter another? [y]/n :")
 if c == 'n' or c == 'N':
 cont = False

 def email1():
 if '@' not in email and '.' not in email:
 print('email needs @ and . at the same time')
 # Save data...
 outfile = open("emails.dat","wb")
 pickle.dump(emails,outfile)
 outfile.close
 print("Your data has been saved to emails.dat")

 def open_existing_file():
 # returns an empty dictionary or one that has data from a file
 emails = {}
 # Load the dictionary
 try:
 infile = open("emails.dat","rb")
 emails = pickle.load(infile)
 infile.close()
 except:
 print("No file to open. Starting with no data.")
 return emails

main()

Here is the error message :

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 
/Users/stephaniequiles/Downloads/emailsupdate.py
Traceback (most recent call last):
   File "/Users/stephaniequiles/Downloads/emailsupdate.py", line 42, in 
 main()
   File "/Users/stephaniequiles/Downloads/emailsupdate.py", line 6, in main
 emails = open_existing_file()
UnboundLocalError: local variable 'open_existing_file' referenced before 
assignment

Process finished with exit code 1

not sure why it is not recognizing that the open_existing_file() function needs 
to be returned to the “emails” variable? I am guessing it has to do with my 
syntax? any suggestions, please?


Python executes as it processes the file, so that open_existing_file 
must have been previously defined before you can refer to it.  Try 
moving that def block in front of main and you'll likely be OK (assuming 
no other issues)


Emile



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


Re: [Tutor] a puzzle about -3**2 vs (-3)**2

2015-08-01 Thread D Wyatt

>
> According to Wikipedia:
>
> https://en.wikipedia.org/wiki/Order_of_operations
>
> some scientific journals now treat multiplication as a higher precedence
> than division with a / so that 1/2x equals 1/(2x), not (1/2)x.
>
> There's an interesting study done here:
>
> "Developer beliefs about binary operator precedence"
>
> http://www.knosof.co.uk/cbook/accu06.html
>
> which suggests that even professional programmers get operator
> precedence wrong at a high rate. (The study found a 33% error rate.)
>
> The bottom line is, there is no universal right or wrong answer for the
> precedence rules for operators, although some rules are less right than
> others.
>
>
>
> --
> Steve

Interesting info.  Thanks for following up on this.


-- 
Deb Wyatt in WA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Emile,
Question: What is the source of the line 7 syntax: mbox.split?

Here is a copy of the Traceback message:
NameError
Traceback (most recent call last)
C:\Users\vm\Desktop\apps\docs\Python\8_5_v_26.py in ()
  5 addresses = set()
  6 for addr in [ fromline.split()[0]
> 7 for fromline in mbox.split('From ')
  8 if fromline ]:
  9 count = count + 1
NameError: name 'mbox' is not defined


Revised code:
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for addr in [ fromline.split()[0]
for fromline in mbox.split('From ')
if fromline ]:
count = count + 1
print addr
print "There were", count, "lines in the file with From as the first word"

Regards,
Hal

On Sat, Aug 1, 2015 at 2:18 PM, Emile van Sebille  wrote:

> On 8/1/2015 12:00 PM, Ltc Hotspot wrote:
>
>> Hi Everyone:
>>
>>
>> Let me repost the question:
>>
>> You will parse the From line using split() and print out the second word
>> in
>> the line (i.e. the entire address of the person who sent the message).
>> Then
>> print out a count at the end.
>>
>> *Hint:* make sure not to include the lines that start with 'From:'.
>>
>> You can download the sample data at
>> http://www.pythonlearn.com/code/mbox-short.txt
>>
>
> Cool - thanks.  That's an mbox file.
>
> Can you explain the apparent dichotomy of the question directing you to
> 'parse the from line' and the hint?  I'm going to guess they mean that
> you're not to print that line in the output?  Aah, I see -- there're two
> different lines that start From -- both with and without a trailing colon.
> So then, we can split on 'From ' and recognizing the split eats the
> split-on portion
>
> >>> '1234567'.split('4')
> ['123', '567']
>
> ... and leaves an empty entry when splitting on the first characters of
> the line
>
> >>> '1234567'.split('1')
> ['', '234567']
>
> ... we get to:
>
> for addr in [ fromline.split()[0]
>   for fromline in mbox.split('From ')
>   if fromline ]:
> print addr
>
> stephen.marqu...@uct.ac.za
> lo...@media.berkeley.edu
> zq...@umich.edu
> rjl...@iupui.edu
> zq...@umich.edu
> rjl...@iupui.edu
> c...@iupui.edu
> gsil...@umich.edu
> gsil...@umich.edu
> zq...@umich.edu
> gsil...@umich.edu
> wagne...@iupui.edu
> zq...@umich.edu
> antra...@caret.cam.ac.uk
> gopal.ramasammyc...@gmail.com
> david.horw...@uct.ac.za
> david.horw...@uct.ac.za
> stephen.marqu...@uct.ac.za
> lo...@media.berkeley.edu
> lo...@media.berkeley.edu
> r...@media.berkeley.edu
> c...@iupui.edu
> c...@iupui.edu
> c...@iupui.edu
> >>>
>
>
>
> Emile
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Alan,

Question1: The output result is an address or line?
Question2: Why are there 54 lines as compared to 27 line in the desired
output?

Here is the latest revised code:
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for line in fh:
if line.startswith('From'):
line2 = line.strip()
line3 = line2.split()
line4 = line3[1]
addresses.add(line4)
count = count + 1
print addresses
print "There were", count, "lines in the file with From as the first word"

The output result:
set(['stephen.marqu...@uct.ac.za', 'lo...@media.berkeley.edu', '
zq...@umich.edu', 'rjl...@iupui.edu', 'c...@iupui.edu', 'gsil...@umich.edu',
'wagne...@iupui.edu', 'antra...@caret.cam.ac.uk', '
gopal.ramasammyc...@gmail.com', 'david.horw...@uct.ac.za', '
r...@media.berkeley.edu']) ← Mismatch
There were 54 lines in the file with From as the first word


The desired output result:
stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
zq...@umich.edu
rjl...@iupui.edu
zq...@umich.edu
rjl...@iupui.edu
c...@iupui.edu
c...@iupui.edu
gsil...@umich.edu
gsil...@umich.edu
zq...@umich.edu
gsil...@umich.edu
wagne...@iupui.edu
zq...@umich.edu
antra...@caret.cam.ac.uk
gopal.ramasammyc...@gmail.com
david.horw...@uct.ac.za
david.horw...@uct.ac.za
david.horw...@uct.ac.za
david.horw...@uct.ac.za
stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
lo...@media.berkeley.edu
r...@media.berkeley.edu
c...@iupui.edu
c...@iupui.edu
c...@iupui.edu
There were 27 lines in the file with From as the first word

Regards,
Hal









On Sat, Aug 1, 2015 at 1:40 PM, Alan Gauld 
wrote:

> On 01/08/15 19:48, Ltc Hotspot wrote:
>
>> There is an indent message in the revised code.
>> Question: Where should I indent the code line for the loop?
>>
>
> Do you understand the role of indentation in Python?
> Everything in the indented block is part of the structure,
> so you need to indent everything that should be executed
> as part of the logical block.
>
> fname = raw_input("Enter file name: ")
>> if len(fname) < 1 : fname = "mbox-short.txt"
>> fh = open(fname)
>> count = 0
>> addresses = set()
>> for line in fh:
>>  if line.startswith('From'):
>>  line2 = line.strip()
>>  line3 = line2.split()
>>  line4 = line3[1]
>>  addresses.add(line)
>>  count = count + 1
>>
>
> Everything after the if line should be indented an extra level
> because you only want to do those things if the line
> startswith From.
>
> And note that, as I suspected, you are adding the whole line
> to the set when you should only be adding the address.
> (ie line4). This would be more obvious if you had
> used meaningful variable names such as:
>
> strippedLine = line.strip()
> tokens = strippedLine.split()
> addr = tokens[1]
> addresses.add(addr)
>
> PS.
> Could you please delete the extra lines from your messages.
> Some people pay by the byte and don't want to receive kilobytes
> of stuff they have already seen multiple times.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: Test to see if ibisMail app is truly sending in plain text

2015-08-01 Thread D Wyatt
I just looked it up.  it means Looks Good to Me.


On Sat, Aug 1, 2015 at 10:16 AM, Steven D'Aprano 
wrote:

> On Sat, Aug 01, 2015 at 04:53:45PM +0100, Mark Lawrence wrote:
>
> > LGTM.
>
> Let's Get The Money?
>
> --
> Steve
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Deb Wyatt in WA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Attribute

2015-08-01 Thread Alan Gauld

On 02/08/15 00:07, Ltc Hotspot wrote:

Question1: The output result is an address or line?


Its your assignment,. you tell me.
But from your previous mails I'm assuming you want addresses?


Question2: Why are there 54 lines as compared to 27 line in the desired
output?


Because the set removes duplicates? So presumably there were 27 
duplicates? (Which is a suspicious coincidence!)



fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for line in fh:
 if line.startswith('From'):
 line2 = line.strip()
 line3 = line2.split()
 line4 = line3[1]
 addresses.add(line4)
 count = count + 1
print addresses
print "There were", count, "lines in the file with From as the first word"


That looks right in that it does what I think you want it to do.


The output result:
set(['stephen.marqu...@uct.ac.za', 'lo...@media.berkeley.edu', '
zq...@umich.edu', 'rjl...@iupui.edu', 'c...@iupui.edu', 'gsil...@umich.edu',
'wagne...@iupui.edu', 'antra...@caret.cam.ac.uk','
gopal.ramasammyc...@gmail.com', 'david.horw...@uct.ac.za', '
r...@media.berkeley.edu']) ← Mismatch


That is the set of unique addresses, correct?


There were 54 lines in the file with From as the first word


And that seems to be the number of lines in the original file
starting with From. Can you check manually if that is correct?


The desired output result:
stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
zq...@umich.edu
rjl...@iupui.edu
zq...@umich.edu
rjl...@iupui.edu

...

Now I'm confused again. This has duplicates but you said you
did not want duplicates? Which is it?

...

c...@iupui.edu
c...@iupui.edu
There were 27 lines in the file with From as the first word


And this is reporting the number of lines in the output
rather than the file (I think). Which do you want?

Its easy enough to change the code to govre the output
you demonstrate, but that's not what you originally asked
for. So just make up your mind exactly what it is you want
out and we can make it work for you.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] String Attribute

2015-08-01 Thread Emile van Sebille

On 8/1/2015 4:07 PM, Ltc Hotspot wrote:

Hi Alan,

Question1: The output result is an address or line?


It's a set actually.  Ready to be further processed I imagine.  Or to 
print out line by line if desired.



Question2: Why are there 54 lines as compared to 27 line in the desired
output?


Because there are 54 lines that start with 'From'.

As I noted in looking at your source data, for each email there's a 
'From ' and a 'From:' -- you'd get the right answer checking only for 
startswith('From ')


Emile





Here is the latest revised code:
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for line in fh:
 if line.startswith('From'):
 line2 = line.strip()
 line3 = line2.split()
 line4 = line3[1]
 addresses.add(line4)
 count = count + 1
print addresses
print "There were", count, "lines in the file with From as the first word"

The output result:
set(['stephen.marqu...@uct.ac.za', 'lo...@media.berkeley.edu', '
zq...@umich.edu', 'rjl...@iupui.edu', 'c...@iupui.edu', 'gsil...@umich.edu',
'wagne...@iupui.edu', 'antra...@caret.cam.ac.uk','
gopal.ramasammyc...@gmail.com', 'david.horw...@uct.ac.za', '
r...@media.berkeley.edu']) ← Mismatch
There were 54 lines in the file with From as the first word


The desired output result:
stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
zq...@umich.edu
rjl...@iupui.edu
zq...@umich.edu
rjl...@iupui.edu
c...@iupui.edu
c...@iupui.edu
gsil...@umich.edu
gsil...@umich.edu
zq...@umich.edu
gsil...@umich.edu
wagne...@iupui.edu
zq...@umich.edu
antra...@caret.cam.ac.uk
gopal.ramasammyc...@gmail.com
david.horw...@uct.ac.za
david.horw...@uct.ac.za
david.horw...@uct.ac.za
david.horw...@uct.ac.za
stephen.marqu...@uct.ac.za
lo...@media.berkeley.edu
lo...@media.berkeley.edu
r...@media.berkeley.edu
c...@iupui.edu
c...@iupui.edu
c...@iupui.edu
There were 27 lines in the file with From as the first word

Regards,
Hal









On Sat, Aug 1, 2015 at 1:40 PM, Alan Gauld 
wrote:


On 01/08/15 19:48, Ltc Hotspot wrote:


There is an indent message in the revised code.
Question: Where should I indent the code line for the loop?



Do you understand the role of indentation in Python?
Everything in the indented block is part of the structure,
so you need to indent everything that should be executed
as part of the logical block.

fname = raw_input("Enter file name: ")

if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for line in fh:
  if line.startswith('From'):
  line2 = line.strip()
  line3 = line2.split()
  line4 = line3[1]
  addresses.add(line)
  count = count + 1



Everything after the if line should be indented an extra level
because you only want to do those things if the line
startswith From.

And note that, as I suspected, you are adding the whole line
to the set when you should only be adding the address.
(ie line4). This would be more obvious if you had
used meaningful variable names such as:

 strippedLine = line.strip()
 tokens = strippedLine.split()
 addr = tokens[1]
 addresses.add(addr)

PS.
Could you please delete the extra lines from your messages.
Some people pay by the byte and don't want to receive kilobytes
of stuff they have already seen multiple times.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



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




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


Re: [Tutor] String Attribute

2015-08-01 Thread Emile van Sebille

On 8/1/2015 4:21 PM, Ltc Hotspot wrote:

Hi Emile,
Question: What is the source of the line 7 syntax: mbox.split?



I read mbox from the file.  eg,

mbox = open("mbox-short.txt",'r').read()

and it looks to me that if you insert the above in front of the for loop 
below you'll get further.


Emile




Here is a copy of the Traceback message:
NameError
Traceback (most recent call last)
C:\Users\vm\Desktop\apps\docs\Python\8_5_v_26.py in ()
   5 addresses = set()
   6 for addr in [ fromline.split()[0]
> 7 for fromline in mbox.split('From ')
   8 if fromline ]:
   9 count = count + 1
NameError: name 'mbox' is not defined


Revised code:
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for addr in [ fromline.split()[0]
 for fromline in mbox.split('From ')
 if fromline ]:
 count = count + 1
 print addr
print "There were", count, "lines in the file with From as the first word"



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