forked from tdwojak/Python2017
praca domowa - labs03 & labs04
This commit is contained in:
parent
940596a57f
commit
b4dd74a5ca
15
labs03/task03.py
Normal file
15
labs03/task03.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# **ćwiczenie 3**
|
||||||
|
# Strona ``https://api.fixer.io/latest`` udostępnia kursy różnych walut w stosunku do euro. Napisz skrypt, który:
|
||||||
|
# * pobierze zawartość JSONa. Wykorzystaj bibliotekę ``requests`` (http://docs.python-requests.org/en/master/).
|
||||||
|
# * korzystając z biblioteki ``json`` przekształć go do obiketu typu JSON.
|
||||||
|
# * Wyświetl wartość kursu EUR do PLN.
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
|
response = requests.get('https://api.fixer.io/latest')
|
||||||
|
content = response.content
|
||||||
|
json_object = json.loads(content)
|
||||||
|
currencies_vs_euro_dict = json_object.get("rates")
|
||||||
|
pln = currencies_vs_euro_dict.get("PLN")
|
||||||
|
print(pln)
|
31
labs03/task04.py
Normal file
31
labs03/task04.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
from weather import Weather
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
|
def fahrenheit_to_celsius(temperature_in_fahrenheit):
|
||||||
|
temperature_in_celsius = (float(temperature_in_fahrenheit - 32) / (9/5))
|
||||||
|
return round(temperature_in_celsius, 2)
|
||||||
|
|
||||||
|
weather = Weather()
|
||||||
|
city = 'Poznan'
|
||||||
|
weather_by_location = weather.lookup_by_location(city)
|
||||||
|
condition = weather_by_location.condition()
|
||||||
|
forecasts = weather_by_location.forecast()
|
||||||
|
new_dict = {}
|
||||||
|
polish_days = ['Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela']
|
||||||
|
|
||||||
|
for forecast in forecasts:
|
||||||
|
data = forecast.date()
|
||||||
|
lowest = forecast.low()
|
||||||
|
new_dict.update({data: lowest})
|
||||||
|
min_temp_day = min(new_dict, key=new_dict.get)
|
||||||
|
new_date = datetime.datetime.strptime(min_temp_day, "%d %b %Y")
|
||||||
|
day_of_week = new_date.weekday()
|
||||||
|
min_temp = new_dict[min_temp_day]
|
||||||
|
min_temp_in_celsius = fahrenheit_to_celsius(float(min_temp))
|
||||||
|
|
||||||
|
print('Current temperature in {0}: {1} Fahrenheit. It is {2}.'.format(city, condition.temp(), condition.text().lower()))
|
||||||
|
|
||||||
|
print('Najzimniejszy dzień: {0}. Temperatura w tym dniu to {1} Celsjusza'.format(polish_days[day_of_week], min_temp_in_celsius))
|
||||||
|
|
||||||
|
|
30
labs03/task05.py
Normal file
30
labs03/task05.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import glob
|
||||||
|
import pathlib as p
|
||||||
|
|
||||||
|
|
||||||
|
# set the path to directory
|
||||||
|
my_path = 'C:/Users/**/*.bleu'
|
||||||
|
new_dict = {}
|
||||||
|
# match files in given directory
|
||||||
|
for name in glob.glob(my_path, recursive=True):
|
||||||
|
# for each file in directory set it's path and open the file
|
||||||
|
path_to_file = p.Path(name)
|
||||||
|
with path_to_file.open() as f:
|
||||||
|
# read 1st line in each file
|
||||||
|
line = f.readline()
|
||||||
|
# split this line using ',' as separator
|
||||||
|
line_splitted = line.split(',')
|
||||||
|
# in this case BLUE = YY.YY is the first element of the list,
|
||||||
|
# now we have to split it by ' ' and add it to dictionary
|
||||||
|
# which key will be the file name and value will be that value
|
||||||
|
for i, j in enumerate(line_splitted):
|
||||||
|
if i == 0:
|
||||||
|
blue_splitted = j.split(' ')
|
||||||
|
second_element = float(blue_splitted[2])
|
||||||
|
# in that case searched value is on 3rd position (2nd list element)
|
||||||
|
new_dict.update({path_to_file: second_element})
|
||||||
|
print(new_dict)
|
||||||
|
# find max key (path) searching by values
|
||||||
|
maximum = max(new_dict, key=new_dict.get)
|
||||||
|
print(maximum)
|
||||||
|
|
@ -5,23 +5,27 @@
|
|||||||
# Napisz funckję ``is_numeric``, która sprawdzi, czy każdy element z przekazanej listy jest typu int lub float. Wykorzystaj funcję ``isinstance()`` (https://docs.python.org/2/library/functions.html#isinstance).
|
# Napisz funckję ``is_numeric``, która sprawdzi, czy każdy element z przekazanej listy jest typu int lub float. Wykorzystaj funcję ``isinstance()`` (https://docs.python.org/2/library/functions.html#isinstance).
|
||||||
|
|
||||||
def is_numeric(another_list):
|
def is_numeric(another_list):
|
||||||
n = []
|
"""
|
||||||
|
Check whether each element of the list is an instance of int or float
|
||||||
|
:param another_list:
|
||||||
|
:return: True or False
|
||||||
|
"""
|
||||||
for i in another_list:
|
for i in another_list:
|
||||||
if isinstance(i, (int, float)):
|
if isinstance(i, bool):
|
||||||
n.append(True)
|
return False
|
||||||
|
elif isinstance(i, (int, float)):
|
||||||
|
i += 1
|
||||||
else:
|
else:
|
||||||
n.append(False)
|
return False
|
||||||
g = set(n)
|
return True
|
||||||
if len(g) != 1:
|
|
||||||
return False
|
# 2nd option
|
||||||
else:
|
|
||||||
return True
|
# if type(i) in (float, int):
|
||||||
|
# i += 1
|
||||||
|
# else:
|
||||||
|
# return False
|
||||||
|
# return True
|
||||||
|
|
||||||
|
|
||||||
|
# print(is_numeric([2, 1.8797, 43654354354354354354354354354354354325879]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
print(is_numeric([1, 1.02]))
|
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
class Employee:
|
class Employee:
|
||||||
_id = 0
|
_id = 0
|
||||||
|
|
||||||
def __init__(self, name, surname):
|
def __init__(self, name, surname):
|
||||||
Employee._id += 1
|
Employee._id += 1
|
||||||
self.id = Employee._id
|
self.id = Employee._id
|
||||||
@ -20,16 +21,48 @@ class Employee:
|
|||||||
return self.id
|
return self.id
|
||||||
|
|
||||||
|
|
||||||
# j = Employee('Kamil', 'Rakowicz')
|
class Recruiter(Employee):
|
||||||
# g = Employee('dsd', 'sdsd')
|
recruited = []
|
||||||
#
|
|
||||||
# print (j.get_id())
|
def recruit(self, Employee):
|
||||||
# print (g.get_id())
|
emp_id = Employee.id
|
||||||
|
self.recruited.append(emp_id)
|
||||||
|
|
||||||
|
|
||||||
|
class Programmer(Employee):
|
||||||
|
def __init__(self, name, surname, Recruiter):
|
||||||
|
super().__init__(name, surname)
|
||||||
|
self.recruiter = Recruiter.id
|
||||||
|
|
||||||
|
|
||||||
|
# setup new employees
|
||||||
|
emp_1 = Employee('Janusz', 'Kowalski')
|
||||||
|
emp_2 = Employee('Marek', 'Marecki')
|
||||||
|
|
||||||
|
print (emp_1.get_id())
|
||||||
|
print (emp_2.get_id())
|
||||||
|
|
||||||
|
# setup new recruiter
|
||||||
|
rec_1 = Recruiter('Random', 'Recruiter')
|
||||||
|
rec_1.recruit(emp_2)
|
||||||
|
|
||||||
|
# check for Recruiter
|
||||||
|
print(rec_1.id, rec_1.name, rec_1.surname, rec_1.recruited)
|
||||||
|
|
||||||
|
# setup new Programmer
|
||||||
|
prog_1 = Programmer('Python', 'Programmer', rec_1)
|
||||||
|
|
||||||
|
# check for new programmer
|
||||||
|
print(prog_1.id, prog_1.name, prog_1.surname, prog_1.recruiter)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Recruiter():
|
|
||||||
|
|
||||||
|
|
||||||
def recruit(Employee):
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,3 +1,57 @@
|
|||||||
#!/usr/bin/env python2
|
# **ćwiczenie 3 (zadanie domowe) **
|
||||||
# -*- coding: utf-8 -*-
|
# Stwórz klasę ``Point``, która będzie reprezentować punkt w przestrzeni wielowymiarowej:
|
||||||
|
# * Konstruktor ma przyjąc tylko 1 parametr: listę współrzednych. Wykorzystaj funkcję z pierwszego zadania, żeby sprawdzić, czy lista zawiera wyłącznie liczby.
|
||||||
|
# * Napisz metodę add, która dida dwa punkty po współrzędnych i zwróci obiekt typu ``Punkt``. Zaimplementuj własny wyjątek ``DimensionError``, który zostaje wyrzucony, jeżeli dodawany punkt ma inny wymiar.
|
||||||
|
# * Napisz metodę ``to\_string``, która zwróci łancuch znakowy, który w czytelny sposób przedstawi punkt.
|
||||||
|
# * Napisz metodę __len__, która zwróci liczbę współrzędnych punktu. Zobacz, czy możesz teraz wywołać funkcję len na obiekcie typy punkt.
|
||||||
|
# * Napisz metodę __str__, która bedzie działać dokładnie tak samo jak metoda ``to_string``. Wyświetl obiekt typy Point korzystając z funkcji print.
|
||||||
|
|
||||||
|
from labs04.task01 import is_numeric
|
||||||
|
|
||||||
|
|
||||||
|
class Point:
|
||||||
|
coordinates = []
|
||||||
|
|
||||||
|
def __init__(self, coordinates):
|
||||||
|
if is_numeric(coordinates):
|
||||||
|
self.coordinates = coordinates
|
||||||
|
else:
|
||||||
|
raise Exception("Coordinates are not numeric")
|
||||||
|
|
||||||
|
def to_string(self):
|
||||||
|
return str(self.coordinates)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.coordinates)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.to_string()
|
||||||
|
|
||||||
|
|
||||||
|
class DimensionError(Exception):
|
||||||
|
def __init__(self, text):
|
||||||
|
self.text = text
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.text
|
||||||
|
|
||||||
|
|
||||||
|
def add(point_1, point_2):
|
||||||
|
if len(point_1.coordinates) != len(point_2.coordinates):
|
||||||
|
raise DimensionError("Coordinates have different dimensions")
|
||||||
|
else:
|
||||||
|
new_coordinates = [x + y for x, y in zip(point_1.coordinates, point_2.coordinates)]
|
||||||
|
return Point(new_coordinates)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
new_point = Point([5, 10, 12, 6])
|
||||||
|
print(new_point.__len__())
|
||||||
|
new_point_2 = Point([12, 23, 21, 16])
|
||||||
|
print(new_point_2.__str__())
|
||||||
|
new_point_3 = add(new_point, new_point_2)
|
||||||
|
|
||||||
|
print('1st point =', new_point)
|
||||||
|
print('2nd point =', new_point_2)
|
||||||
|
print('1st point + 2nd point = ', new_point_3)
|
||||||
|
Loading…
Reference in New Issue
Block a user