Tuesday, November 25, 2014

Unpacking a List


Word Frequency Counter

This program makes a count of the words on a webpage. The basic outline of the program is as follows:

(1) Use Beautiful Soup to  crawl a webpage.
(2) Put words from webpage into list.
(3) Pass this word list into a method that cleans up word list.
(4) Pass this clean word list into method that creates a dictionary with key of word and value of number of times word occurs.

See program below:

Creating a Dictionary


Threading


Monday, November 24, 2014

More about Classes and Objects


Errors: Syntax and Exceptions

A syntax error is a problem with the code. An exception error is a problem with the input.


Making a Web Crawler


Downloading CSV File From Web


An outline of the steps to download file from the web:
(1) Import necessary library
(2) Create function that passes url of csv file.
(3) Open url
(4) Read url
(5) Convert what is read to a string
(6) Create CSV file.
(7) Open csv file to write.
(8) write read url to csv file
(9) Close opened csv file




Tuesday, November 4, 2014

Flexible number of arguments


Variable Scope, Another Example

Here is another example of a local variable inside a function that can not be used outside the function.The value area has to be passed to a variable outside the function. I have used a '#' to make the code that does that into a comment and thus we the error that area is not defined.




Variable Scope

In the example below the variable, a , is created outside both functions and can be used by both functions.


In the example below the variable a is created inside a function and only that function can use the variable.

 

Setting Default Values

Value is defaulted to unknown.


Return Statement

The return statement is used to return a value to the caller of the function. It returns a value that can be used outside the function. See example below.

Here is a similar program where the area value is not returned. Thus I have embedded the print statement within the function get a similar output.