Re: [Tutor] saving webpage as webarchive

2016-03-01 Thread Danny Yoo
> I want to save a webpage as a webarchive, and not just get the text.
> I hope there’s a way to do it without saving all of the images separately.
> And even if I do have to download them separately, then how would I combine 
> everything into the HTM webarchive?


If I understand your question properly, I think you're asking for
something like the use of the 'wget' utility, which knows how to
download an entire web site:

http://www.linuxjournal.com/content/downloading-entire-web-site-wget


Trying to do this as a Python program is not a simple task; it's
equivalent to writing a web crawler.
http://www-rohan.sdsu.edu/~gawron/python_for_ss/course_core/book_draft/web/web_intro.html
explains some basic ideas.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] saving webpage as webarchive

2016-03-01 Thread Alan Gauld
On 01/03/16 04:32, Benjamin Fishbein wrote:
> This seems like it should be simple, but I can’t find any answer with the 
> docs or google.
> I’m on a Mac. OSX Yosemite 10.10.5
> I want to save a webpage as a webarchive, and not just get the text.
> I hope there’s a way to do it without saving all of the images separately.

The Webarchive format is unique to Apple Safari. As such
you will likely have to use some kind of Apple/MacOS
library to create it. I don't know what that interface
looks like but you might find something on the Apple
developer site.

You should try asking on the MacPython forums too,
since they may know if the required API exists in
a Python module.

-- 
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] If loop conditions

2016-03-01 Thread Dimitar Ivanov
Hello everyone!

First time using this mailing list so please excuse me in advance if this
mail is not structured properly.

I'm going through a Python course by Google, so far I've gotten to the
lists chapter and I've been trying to wrap my head around an exercise all
afternoon long, eventually was forced to look up the answer.

In the following exercise, Google requires you to look up words from a list
and count how many of the words are longer or equal to 2 characters where
the first and last letter match.

I've been trying to assign string slices to variables and then use those
variables inside the following if statement:

def match_ends(words):
+  for word in words:
+length=len(word)
+  newList=[]
+  firstChar=word[0:]
+  lastChar=word[:-1]
+if [ length >= 2 and firstChar == lastChar ]:
+newList.append(word)
+else:
+  break
+  newListCount=len(newList)
+  return newListCount

This was returning quite funky results such as counting each letter within
a word as a different word, not picking up the start/end of string etc.

Eventually I looked up the solution since I tracked down my problem to how
I made my if statement and, of course, it turned out to be much simpler
than usual.

+  if len(word) >= 2 and word[0] == word[-1]:

I tried putting brackets around the conditions and that broke the result
again so I came to wonder what's the difference between statements written
without brackets and those within brackets (such as my original code block)?

I'm sorry if I didn't make it completely clear, please let me know if you
need any further information.

Thank you!

Dimitar



-- 
Thanks,
Dimitar

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


Re: [Tutor] If loop conditions

2016-03-01 Thread Alan Gauld
On 01/03/16 16:28, Dimitar Ivanov wrote:
> First time using this mailing list so please excuse me in advance if this
> mail is not structured properly.

No worries, you've done a pretty good job of telling us
what we need to know.

> In the following exercise, Google requires you to look up words from a list
> and count how many of the words are longer or equal to 2 characters where
> the first and last letter match.
> 
> I've been trying to assign string slices to variables and then use those
> variables inside the following if statement:

You probably don't need slices here. They are cool features
but not always the right thing to use.

> def match_ends(words):
> +  for word in words:
> +length=len(word)
> +  newList=[]
> +  firstChar=word[0:]
> +  lastChar=word[:-1]

You only want the first and last character, not a slice.
So just use indexes.

  firstChar=word[0]
  lastChar=word[-1]

> +if [ length >= 2 and firstChar == lastChar ]:

You don;t need the brackets. And in fact in this case
you are creating a list containing the bollean result
of your test. A  non empty list (regardless of the
value inside) is always going to be "true"...

Just write it as:

if length >= 2 and (firstChar == lastChar):

The () just make it clear that its

a and (b == c)

rather than

(a and b) == c

> +newList.append(word)
> +else:
> +  break

break exits the loop, you don't want that, instead you want
to go back to the start of the loop to process the next word.
The command for that is

continue

> +  newListCount=len(newList)
> +  return newListCount

> Eventually I looked up the solution since I tracked down my problem to how
> I made my if statement and, of course, it turned out to be much simpler
> than usual.
> 
> +  if len(word) >= 2 and word[0] == word[-1]:
> 
> I tried putting brackets around the conditions and that broke the result
> again so I came to wonder what's the difference between statements written
> without brackets and those within brackets (such as my original code block)?

It all depends on the type of brackets.
Using square brackets as you did creates a list with a single value

like: [42]  or in your case, either [True] or [False]

The if statement(not loop) then treats any non empty list as True.

But if you had used () instead of [] then it would probably
work as you expected.

if (len(word) >= 2 and word[0] == word[-1]):

But you don;t really need them in Python (unlike C or Java)

You can however make the intention clearer for your readers
by grouping sub tests with params like:

if (length >= 2) and (firstChar == lastChar):

HTH
-- 
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] recursivity and lists

2016-03-01 Thread Gaston

Hello everyone !

I was trying to do this little exercise:
# E. Given two lists sorted in increasing order, create and return a merged
# list of all the elements in sorted order. You may modify the passed in 
lists.

# Ideally, the solution should work in "linear" time, making a single
# pass of both lists.

I wanted to try doing it in a recursive way. Here is my function:

def linear_merge(list1, list2):
  if list1==[]: return list2 #initial case 1
  elif list2==[]:return list1 #initial case 2
  elif list1[-1]>list2[-1]: #recursive case 1
a=list1.pop()
return linear_merge(list1,list2).append(a)
  else: #recursive case 2
a=list2.pop()
return linear_merge(list1,list2).append(a)

So I add the biggest element of the two list to the end of the sorted 
couple of lists with this element removed.


Problem is that python complains that he cannot know the type of the 
result of this function (list). In C++ I would specify the type, but 
from what I understood, Python should not need this. What did I miss ?


I hope I made myself clear and thank you for your help,

Cheers,

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


Re: [Tutor] recursivity and lists

2016-03-01 Thread Danny Yoo
>
> Problem is that python complains that he cannot know the type of the
result of this function (list). In C++ I would specify the type, but from
what I understood, Python should not need this. What did I miss ?

Can you copy the exact stack trace of the error?  It'll help: you've
interpreted what the error means, which is useful, but it's also important
to show the unvarnished output too.  It will let us look and make our own
interpretations.

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


Re: [Tutor] recursivity and lists

2016-03-01 Thread Danny Yoo
Also, as a quick note: Python list append is not functional: it mutates
rather than returns a useful return value.  You may need to revisit the
parts in the code where it assumes a different behavior from functional
append.
On Mar 1, 2016 12:36 PM, "Danny Yoo"  wrote:

>
> >
> > Problem is that python complains that he cannot know the type of the
> result of this function (list). In C++ I would specify the type, but from
> what I understood, Python should not need this. What did I miss ?
>
> Can you copy the exact stack trace of the error?  It'll help: you've
> interpreted what the error means, which is useful, but it's also important
> to show the unvarnished output too.  It will let us look and make our own
> interpretations.
>
> Good luck!
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor