<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=I719_Fundamentals_of_Python%2Flecture5</id>
	<title>I719 Fundamentals of Python/lecture5 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=I719_Fundamentals_of_Python%2Flecture5"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture5&amp;action=history"/>
	<updated>2026-06-21T21:56:19Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture5&amp;diff=118350&amp;oldid=prev</id>
		<title>Eroman: add lecture 5</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture5&amp;diff=118350&amp;oldid=prev"/>
		<updated>2017-03-04T14:16:04Z</updated>

		<summary type="html">&lt;p&gt;add lecture 5&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Advanced Testing =&lt;br /&gt;
&lt;br /&gt;
See&amp;lt;br /&amp;gt;&lt;br /&gt;
https://wiki.itcollege.ee/index.php/I719_Fundamentals_of_Python/testing&lt;br /&gt;
&lt;br /&gt;
= Linting =&lt;br /&gt;
&lt;br /&gt;
Python has a style guide called PEP8.&lt;br /&gt;
&lt;br /&gt;
you can check if your code follows PEP8 using the PEP8 linter.&lt;br /&gt;
&lt;br /&gt;
(you may also install this using your OS&amp;#039;s package manager)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;shell&amp;quot;&amp;gt;pip3 install pep8&lt;br /&gt;
python3 -m pep8 ~/path/to/your_file.py&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will print out each error as a line in stdout&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;path/to/your_file.py:LINE:COLUMN PEP8_ERROR EXPLANATION&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
for example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;shell&amp;quot;&amp;gt;btc/btc_price_tests.py:8:80: E501 line too long (648 &amp;amp;gt; 79 characters)&lt;br /&gt;
btc/btc_price_tests.py:11:1: E302 expected 2 blank lines, found 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is another tool called &amp;lt;code&amp;gt;flake8&amp;lt;/code&amp;gt;, which is installed the same way and checks for problems in your code that will cause runtime problems, as well as the pep8 checking.&lt;br /&gt;
&lt;br /&gt;
I recommend you configure your editor to use flake8&lt;br /&gt;
&lt;br /&gt;
Please see http://blog.sideci.com/2015/12/14/style-guide-of-python-and-lint-tool/ for more information on linting&lt;br /&gt;
&lt;br /&gt;
= Datetime =&lt;br /&gt;
&lt;br /&gt;
see https://docs.python.org/3/library/datetime.html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;from datetime import datetime&lt;br /&gt;
&lt;br /&gt;
# convert a timestamp to python native datetime object&lt;br /&gt;
timestamp = 1488430906&lt;br /&gt;
d = datetime.fromtimestamp(timestamp)&amp;lt;/source&amp;gt;&lt;br /&gt;
= TASK 1 =&lt;br /&gt;
&lt;br /&gt;
Print the titles of all the frontpage articles on reddit, with the time in normal format (i.e 11:23) next to it.&lt;br /&gt;
&lt;br /&gt;
resource: https://www.reddit.com/.json&amp;lt;br /&amp;gt;&lt;br /&gt;
example output&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;11:23 Sessions spoke twice with Russian ambassador during Trump\u2019s presidential campaign, Justice officials say \/\/ CAN WE HAVE AN INDEPENDENT INVESTIGATION NOW? Call your Representative and get them on the record, Country or party?&amp;lt;/pre&amp;gt;&lt;br /&gt;
== NOTE ==&lt;br /&gt;
&lt;br /&gt;
reddit has rate limiting and does not give consistent responses!&amp;lt;br /&amp;gt;&lt;br /&gt;
download the json data as a json file and use the following code&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import json&lt;br /&gt;
&lt;br /&gt;
json_file = open(&amp;#039;frontpage.json&amp;#039;)&lt;br /&gt;
data = json.load(json_file)&lt;br /&gt;
json_file.close()&amp;lt;/source&amp;gt;&lt;br /&gt;
== Solution ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import argparse&lt;br /&gt;
import json&lt;br /&gt;
&lt;br /&gt;
from datetime import datetime&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def get_time_with_title(article):&lt;br /&gt;
    timestamp = article[&amp;#039;data&amp;#039;][&amp;#039;created_utc&amp;#039;]&lt;br /&gt;
    d = datetime.fromtimestamp(timestamp)&lt;br /&gt;
    time = d.strftime(&amp;#039;%H:%M&amp;#039;)&lt;br /&gt;
    title = article[&amp;#039;data&amp;#039;][&amp;#039;title&amp;#039;]&lt;br /&gt;
    result = &amp;#039;{time} {title}&amp;#039;.format(time=time, title=title)&lt;br /&gt;
    return result&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def print_headlines(path):&lt;br /&gt;
    with open(path) as json_file:&lt;br /&gt;
        data = json.load(json_file)&lt;br /&gt;
    articles = data[&amp;#039;data&amp;#039;][&amp;#039;children&amp;#039;]&lt;br /&gt;
    for article in articles:&lt;br /&gt;
        line = get_time_with_title(article)&lt;br /&gt;
        print(line)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    parser = argparse.ArgumentParser()&lt;br /&gt;
    parser.add_argument(&amp;#039;file_name&amp;#039;)&lt;br /&gt;
    args = parser.parse_args()&lt;br /&gt;
    print_headlines(args.file_name)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    main()&amp;lt;/source&amp;gt;&lt;br /&gt;
= Classes part 2 =&lt;br /&gt;
&lt;br /&gt;
see &amp;amp;quot;6. Classes&amp;amp;quot; section of https://learnxinyminutes.com/docs/python3/&lt;br /&gt;
&lt;br /&gt;
= lamdbas =&lt;br /&gt;
&lt;br /&gt;
Lamdbas are like functions, but are not necessarily defined with a name. You can assign a lamdba expression to a variable, but if you do this, then you should define a function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;# do this&lt;br /&gt;
def plus_one(x):&lt;br /&gt;
    return x + 1&lt;br /&gt;
&lt;br /&gt;
# do not do this&lt;br /&gt;
plus_one_lambda = lamdba x: x + 1&lt;br /&gt;
&lt;br /&gt;
plus_one(1)  # 2&lt;br /&gt;
plus_one_lambda(1)  # 2&amp;lt;/source&amp;gt;&lt;br /&gt;
Lamdba expressions are used when a short function is needed as an argument for another function.&lt;br /&gt;
&lt;br /&gt;
Function that take lambda expression often work on some iterable and need to be called on each iteration. For example &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is a function found in many programming languages. It take an iterable and returns an iterable of equal dimension (i.e. length), but with each element modified in a some way. In python, the first argument of the map function is a function, which is called on each element in the iterable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def square(x):&lt;br /&gt;
    return x**2&lt;br /&gt;
&lt;br /&gt;
# map returns a special map type that must be converted to a list to print&lt;br /&gt;
print(list(map(square, range(4))))&lt;br /&gt;
print(list(map(lambda x: x**2, range(4))))&amp;lt;/source&amp;gt;&lt;br /&gt;
prints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[0, 1, 4, 9]&lt;br /&gt;
[0, 1, 4, 9]&amp;lt;/pre&amp;gt;&lt;br /&gt;
= list comprehension =&lt;br /&gt;
&lt;br /&gt;
in modern python, the map function is rarely used. The preferred syntax is the list comprehension. it is like a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; loop.&amp;lt;br /&amp;gt;&lt;br /&gt;
the syntax is the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;[x + 1 for x in iterable]&amp;lt;/source&amp;gt;&lt;br /&gt;
The first part, &amp;lt;code&amp;gt;x + 1&amp;lt;/code&amp;gt; in this example, can be anything. Whatever this evaluates to, becomes an element in the resulting list. The rest of the comprehension is like a for loop, but the &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; variable is used in the first part.&lt;br /&gt;
&lt;br /&gt;
All of the following print the same values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;# Non-functional way, generate a new list&lt;br /&gt;
squares = []&lt;br /&gt;
for x in range(4):&lt;br /&gt;
    squares.append(x**2)&lt;br /&gt;
print(squares)&lt;br /&gt;
&lt;br /&gt;
# map() functional way&lt;br /&gt;
squares = map(lambda x: x**2, range(4))&lt;br /&gt;
print(list(squares))&lt;br /&gt;
&lt;br /&gt;
# modern way&lt;br /&gt;
squares = [x**2 for x in range(4)]&lt;br /&gt;
print(squares)&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eroman</name></author>
	</entry>
</feed>