1
0
forked from tdwojak/Python2017

labs03 - after review

This commit is contained in:
s45157 2017-12-16 02:41:43 +01:00
parent 9f8ecbfdb6
commit 932a6637e0
5 changed files with 62 additions and 5 deletions

View File

@ -1,3 +1,8 @@
#lab03 task01 #lab03 task01
#Returns mutable type names #Returns mutable type names
[t for t, oldid, newid in map( lambda (t, obj, objid): (t, id(obj*1), objid), map(lambda x : (type(x), x, id(x)), [[], '', 3.141592]) ) if oldid == newid] [t for t, oldid, newid in map( lambda (t, obj, objid): (t, id(obj*1), objid), map(lambda x : (type(x), x, id(x)), [[], '', 3.141592]) ) if oldid == newid]
for type_name, type_mutable in map( lambda t: (type(t), id(t)==id(t*1)), [[], '', 3.141592] ):
print("%s is %s" % (str(type_name), 'mutable' if type_mutable else 'immutable') )

View File

@ -1,10 +1,7 @@
# lab03, task02 # lab03, task02
def fibN(n): def fibN(n):
prev, current = 0, 1 prev, current = 1, 1
for n in range(n): for n in range(n):
yield prev yield prev
prev, current = current, prev + current prev, current = current, prev + current
for x in fibN(10):
print(x)

15
labs03/task03.py Normal file
View File

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
import requests, json
# try:
# print('EUR:PLN %3.4f' % (requests.get('https://api.fixer.io/latest').json()['rates']['PLN']))
# except:
# print("Something went wrong. Couldn't display EUR:PLN exchange rate.")
#
# Niestety nie znalazłem w pakiecie json dla pythona 2.7 operacji zwracającej wspomniany w treści zadania
# 'obiekt typu JSON' -czytanie streamu zamienia dane na zwykłe pythonowe słowniki. Nie wnosi to więc wartości dodanej
# do powyższego kodu, który w metodzie.json() zwraca obiekt typu dict, podobnie jak przyjmująca łańcuch znaków
# funkcja json.loads.
print(json.loads(requests.get('https://api.fixer.io/latest').text))['rates']['PLN']
# assert type(json.loads(requests.get('https://api.fixer.io/latest').text)) == type(dict())
#

22
labs03/task04.py Normal file
View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from weather import Weather
import datetime
weather = Weather()
loc = weather.lookup_by_location('Poznan')
print('Informacja o aktualnej pogodzie: %s' % loc.condition().text())
def ftoc(f):
return (f-32.0)*5.0/9.0
def date_to_pl_weekday(date_string):
"""
input: string of form '10 Dec 2017'
output: weekday in polish language
"""
idx = datetime.datetime.strptime(date_string, '%d %b %Y').weekday()
return [u'poniedziałek', u'wtorek', u'środa', u'czwartek', u'piątek', u'sobota', u'niedziela'][idx]
min_day = min(loc.forecast(), key = lambda f: f.low())
print(u'Najbliższy najzimniejszy dzień to %s. Będzie %s\u00B0C. ' % (date_to_pl_weekday(min_day.date()), ftoc(int(min_day.low()))))

18
labs03/task05.py Normal file
View File

@ -0,0 +1,18 @@
import glob, os
# print(glob.glob('./scores/model.iter[0-9][0-9][0-9][0-9][0-9][0-9][0-9].npz.bleu'))
def extract_BLEU_score(fn):
'''
:param fn - path to file with :
:return:
'''
with open(fn, 'r') as fd:
line = fd.readline()
return (os.path.abspath(fn), float(line[6:line.index(',')]))
print max([ t for t in map(extract_BLEU_score, glob.iglob('./scores/model.iter[0-9][0-9][0-9][0-9][0-9]*.npz.bleu'))], key = lambda tpl: tpl[1])[0]