1
0
Fork 0

Zadania 03-04

This commit is contained in:
szwedek 2017-12-14 12:28:39 +01:00
parent 55206606d2
commit a64b4abb4a
12 changed files with 487 additions and 240 deletions

View File

@ -54,7 +54,7 @@
"print(3.14)\n",
"print(123456789 * 987654321)\n",
"print(10 ** 20)\n",
"print(12 ** (3 + 4 *(567 % 8) / 9))\n",
"print(12 ** (3 + 4 *(567 % 8) / 9)) # % modulo\n",
"print(\"PI\", \"jest równe \", 3.1415)"
]
},
@ -159,6 +159,27 @@
"print(type(czy_lubie_pythona))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
@ -621,6 +642,13 @@
" print(znak, ord(znak))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,

View File

@ -31,21 +31,6 @@
" * Prawie 10-letnia przygoda z Pythonem"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Warunki zaliczenia\n",
" * Od tej edycji zajęcia z Pythona są osobnym przedmiotem.\n",
" * Ocena na podstawie zadań domowych.\n",
" * Termin na zadania domowe to prawie 2 tygodnie.\n",
" * Więcej na drugiej godzinie zajęć."
]
},
{
"cell_type": "markdown",
"metadata": {
@ -113,6 +98,31 @@
" Motywacje zaczerpnięte m.in. z [5 Reasons why Python is Powerful Enough for Google](https://www.codefellows.org/blog/5-reasons-why-python-is-powerful-enough-for-google)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Warunki zaliczenia\n",
" * Od tej edycji zajęcia z Pythona są osobnym przedmiotem.\n",
" * Ocena na podstawie zadań domowych.\n",
" * Termin na zadania domowe to prawie 2 tygodnie.\n",
" * Więcej na drugiej godzinie zajęć."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,

View File

@ -49,34 +49,22 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"[1, 2, 3, 1, 2, 3]\n",
"123\n"
]
}
],
"outputs": [],
"source": [
"def dwojak(x): \n",
" x *= 2\n",
" return x\n",
"def dwojak(x): x *= 2\n",
" \n",
"l = [1, 2, 3]\n",
"s = \"123\"\n",
"\n",
"dwojak(l)\n",
"dwojak(s)\n",
"print(dwojak(1))\n",
"\n",
"print(l)\n",
"print(s)"
]
@ -116,7 +104,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -127,17 +115,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 1, 2, 3]\n",
"F: [1, 2, 3, 1, 2, 3]\n",
"[1, 2, 3]\n"
"[1, 2, 3, 1, 2, 3]\n[1, 2, 3]\n"
]
}
],
"source": [
"def dwojak1(x): x *= 2\n",
"def dwojak2(x): \n",
" x = x * 2\n",
" print(\"F:\", x)\n",
"def dwojak2(x): x = x * 2\n",
"\n",
"l = [1,2, 3]\n",
"dwojak1(l)\n",
@ -150,7 +134,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -161,22 +145,20 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3]\n",
"[1, 2, 3, 4]\n"
]
}
],
"source": [
"l = [1, 2, 3]\n",
"e = l[:]\n",
"e = l\n",
"e.append(4)\n",
"print(l)\n",
"print(e)"
"print(l)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -214,7 +196,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -225,28 +207,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 'napis', [0])\n",
"3\n"
]
},
{
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-25-2bd2fa17fbf5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
"(1, 'napis', None)\n3\n"
]
}
],
"source": [
"t = (1, \"napis\", [])\n",
"t[-1].append(0)\n",
"t = (1, \"napis\", None)\n",
"elem = t[0]\n",
"print(t)\n",
"print(len(t))\n",
"print({t: None})"
"print(len(t))"
]
},
{
@ -262,7 +231,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -273,9 +242,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"15\n",
"a == 1\n",
"b == (3, 4)\n"
"15\na == 1\nb == (3, 4)\n"
]
}
],
@ -284,7 +251,7 @@
" return sum(args)\n",
"print(suma(1,2,3,4,5))\n",
"\n",
"def greet_me(z=None,**kwargs):\n",
"def greet_me(**kwargs):\n",
" if kwargs is not None:\n",
" for key, value in kwargs.items():\n",
" print(\"%s == %s\" %(key,value))\n",
@ -304,7 +271,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -315,21 +282,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"97\n",
"a\n",
"98\n",
"b\n",
"99\n",
"c\n",
"100\n",
"d\n"
"a\nb\nc\nd\n"
]
}
],
"source": [
"def alfaRange(x, y):\n",
" for i in range(ord(x), ord(y)):\n",
" print(i)\n",
" yield chr(i)\n",
"\n",
"for c in alfaRange('a', 'e'):\n",
@ -349,7 +308,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -360,63 +319,37 @@
"name": "stdout",
"output_type": "stream",
"text": [
"W Paryżu najlepsze kasztany są na placu Pigalle\n",
"Zuzanna lubi je tylko jesienią.\n",
"\n",
">>\n"
"W Paryżu najlepsze kasztany są na placu Pigalle\nZuzanna lubi je tylko jesienią.\n\n"
]
}
],
"source": [
"plik = open(\"haslo.txt\", 'r')\n",
"plik = open(\"J:\\PycharmProjects\\Python2017\\labs03\\haslo.txt\", 'r')\n",
"for linia in plik.readlines():\n",
" print(linia.strip())\n",
"print(plik.read())\n",
"print(\">>\")\n",
"print(plik.read()) # odczytuje \n",
"plik.close()"
]
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"W Paryżu najlepsze kasztany są na placu Pigalle\n",
"\n",
"Zuzanna lubi je tylko jesienią.\n",
"\n"
]
},
{
"ename": "ValueError",
"evalue": "I/O operation on closed file.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-47-f06513c1bbec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mlinia\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mplik\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreadlines\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlinia\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mplik\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: I/O operation on closed file."
]
}
],
"outputs": [],
"source": [
"with open(\"haslo.txt\", 'r') as plik:\n",
" for linia in plik.readlines():\n",
" print(linia)\n",
"print(plik.read())"
"# print(plik.read())"
]
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": null,
"metadata": {
"collapsed": true,
"slideshow": {
@ -457,22 +390,13 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"posix\n",
"Nazwa uzytkownika: tomaszd\n"
]
}
],
"outputs": [],
"source": [
"import os\n",
"print(os.name)\n",
@ -483,31 +407,13 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Counter({'o': 4, 'n': 4, 'a': 4, 'k': 3, 't': 3, 'y': 2, 'i': 2, 'c': 2, 'z': 2, 's': 1, 'p': 1, 'l': 1, 'ń': 1, 'w': 1, 'e': 1})\n"
]
},
{
"data": {
"text/plain": [
"array([[ 1., 3., 4., 5.]], dtype=float32)"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"from collections import *\n",
"print(Counter(\"konstantynopolitańczykowianeczka\"))\n",
@ -544,23 +450,13 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What's your name?\n",
"Tomasz\n",
"Welcome home, Tomasz.\n"
]
}
],
"outputs": [],
"source": [
"name = input(\"What's your name?\\n\")\n",
"print(\"Welcome home, {}.\".format(name))"

23
labs03/task01.py Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
lista = [1,2,3,4,5]
print("lista ma id: {}".format(id(lista)))
lista *= 10
lista.append(6)
print("lista ma id: {}".format(id(lista)))
lista.pop(2)
print("lista ma id: {}".format(id(lista)))
lancuch = "a string"
print("lancuch ma id: {}".format(id(lancuch)))
lancuch.capitalize()
print("lancuch ma id: {}".format(id(lancuch)))
lancuch += "!"
print("lancuch ma id: {}".format(id(lancuch)))
liczba = 5.2
print("liczba ma id: {}".format(id(liczba)))
liczba += 2.3
print("liczba ma id: {}".format(id(liczba)))

14
labs03/task02.py Normal file
View File

@ -0,0 +1,14 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
def Fibb(n):
f_1 = 1
f_2 = 1
for i in range(0, n):
f_3 = f_1 + f_2
yield f_1
f_1 = f_2
f_2 = f_3
for c in Fibb(10):
print(c)

13
labs03/task03.py Normal file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import requests,json
# pobieram zawartość JSON wykorzystując bibliotekę 'requests'
r = requests.get('https://api.fixer.io/latest')
if r.status_code == 200:
# korzystając z biblioteki ``json`` przekształcamy go do obiektu typu JSON.
decoded_js = json.loads(r.text)
print('Kurs EUR do PLN wynosi {}'.format(decoded_js['rates']['PLN']))

70
labs03/task04.py Normal file
View File

@ -0,0 +1,70 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import weather,datetime
def FahrenheitToCelsius(fahrenheit):
return (fahrenheit - 32) / 1.8
#return round((fahrenheit - 32) / 1.8, 1)
def dzien_tygodnia(data):
dni_tygodnia = ["poniedziałek",
"wtorek",
"środa",
"czwartek",
"piatek",
"sobotę",
"niedzielę"]
return dni_tygodnia[data.weekday()]
def miesiaca(data):
miesiace = ["stycznia",
"lutego",
"marca",
"kwietnia",
"maja",
"czerwca",
"lipca",
"śierpnia",
"września",
"października",
"listopada",
"grudnia"]
return miesiace[data.month-1]
pogoda = weather.Weather()
# Yahoo! WOEID dla miasta Poznań 514048
#miejsce = pogoda.lookup(514048)
#stan = miejsce.condition()
#print(stan.text())
# Lookup poprzez podanie nazwy miasta
miejsce = pogoda.lookup_by_location('Poznań')
stan = miejsce.condition()
#print(stan.text())
# prognozy na nadchodzące dni
prognozy = miejsce.forecast()
#print(prognozy)
# wybieram dzień z prognozy pogody w którym bedzie najzimniej
najzimniejszy = min(prognozy, key = lambda prognozy : prognozy.low())
# konwerstuję string na format czasu
datetime_object = datetime.datetime.strptime(najzimniejszy.date(), '%d %b %Y')
#print(datetime_object.get_weekday())
#print(FahrenheitToCelsius(float(najzimniejszy.high())))
#print(FahrenheitToCelsius(float(najzimniejszy.low())))
# korzystając z prognozy, wypisuję dzień, w którym będzie najzimniej.
#
#print 'W {}'.format(dzien_tygodnia(datetime_object))+ ' ' + str(datetime_object.day) + ' ' + miesiaca(datetime_object) + ' ' + str(datetime_object.year) + ' będzie najchłodniej, najniższa możliwa temperatura może wynieść {}'.format(round(FahrenheitToCelsius(float(najzimniejszy.low())),1)) + ' °C'
wydruk = 'W {dt} {d} {m} {r} będzie najchłodniej, najniższa możliwa temperatura może wynieść {t:.1f} °C'
print wydruk.format(dt=dzien_tygodnia(datetime_object),d=str(datetime_object.day), m=miesiaca(datetime_object), r=str(datetime_object.year), t=FahrenheitToCelsius(float(najzimniejszy.low())))

20
labs03/task05.py Normal file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import glob
# funkcja, która wydobywa wartości miary BLEU z pliku o podanej ścieżce
def BLEU(sciezka):
with open(sciezka, 'r') as plik:
for linia in plik.readlines():
lancuch = linia.strip()
#print sciezka + lancuch
return float(lancuch.partition(',')[0].partition('=')[2])
# znajduję nazwę pliku z maksymalną wartościa miary BLEU korzystając z iteratora ścieżek
# do plików o podanej postaci oraz funkcji BLEU
#
#print max(glob.iglob('scores/model.iter*.npz.bleu'), key = BLEU).partition('\\')[2]
#
sciezka = max(glob.iglob('scores/model.iter*.npz.bleu'), key = BLEU)
print 'Największa wartość mary BLEU wynosząca {1} znajduje się w pliku {0}'.format(sciezka.partition('\\')[2],BLEU(sciezka))

View File

@ -43,7 +43,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -54,8 +54,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"Python da się lubić !\n"
"0\nPython da się lubić !\n"
]
}
],
@ -77,7 +76,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -88,7 +87,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<class '__main__.NajprostszaKlasa'>\n"
"<type 'instance'>\n<type 'instance'>\n"
]
}
],
@ -97,7 +96,10 @@
" pass\n",
"\n",
"nasza_klasa = NajprostszaKlasa() # Uwaga na nawiasy na końcu!\n",
"print(type(nasza_klasa))"
"print(type(nasza_klasa))\n",
"\n",
"nasza_klasa = NajprostszaKlasa # Uwaga na nawiasy na końcu!\n",
"print(type(nasza_klasa()))"
]
},
{
@ -114,7 +116,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 14,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -125,23 +127,26 @@
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
"2\n<__main__.Punkt instance at 0x00000000049B3CC8>\n"
]
}
],
"source": [
"class Punkt:\n",
" def __init__(self, x, y):\n",
" def __init__(self, x, y=0):\n",
" self.x = x\n",
" self.y = y\n",
" \n",
"# self jak this w C\n",
"\n",
"punkt = Punkt(2, 3)\n",
"print(punkt.x)"
"punkt = Punkt(2)\n",
"print(punkt.x)\n",
"print(punkt)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 20,
"metadata": {
"collapsed": true,
"slideshow": {
@ -157,13 +162,16 @@
" def liczba_wierzcholkow(self):\n",
" return len(self.vertexes)\n",
" \n",
" #metoda - jedynym argumentem jest self\n",
" #funkcja - \n",
" \n",
" def dodaj_wierzcholek(self, x):\n",
" self.vertexes.append(x)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 22,
"metadata": {
"collapsed": true,
"slideshow": {
@ -172,10 +180,12 @@
},
"outputs": [],
"source": [
"#dziedziczenie\n",
"\n",
"class Prostokat(Figura):\n",
" def __init__(self, vertexes):\n",
" super().__init__(vertexes)\n",
" \n",
" #lub self.vertexes = vertexes\n",
" def czy_jestem_kwadratem(self):\n",
" pass\n",
" "
@ -183,7 +193,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 41,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -196,7 +206,7 @@
"3"
]
},
"execution_count": 28,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
@ -212,7 +222,11 @@
" def __len__(self):\n",
" return self.liczba_wierzcholkow()\n",
" \n",
"len(Figura([Punkt(2,3), Punkt(3,4), Punkt(0, 0)]))"
"len(Figura([Punkt(2,3), Punkt(3,4), Punkt(0, 0)]))\n",
"o = Figura([Punkt(2,3), Punkt(3,4), Punkt(0, 0)])\n",
"len(o)\n",
"o.__len__()\n",
"o.liczba_wierzcholkow()"
]
},
{
@ -228,7 +242,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 33,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -246,7 +260,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 34,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -257,25 +271,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class Punkt in module __main__:\n",
"\n",
"class Punkt(builtins.object)\n",
" | Klasa reprezentująca punkt w 2D.\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __init__(self, x, y)\n",
" | opis argumentów x i y.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors defined here:\n",
" | \n",
" | __dict__\n",
" | dictionary for instance variables (if defined)\n",
" | \n",
" | __weakref__\n",
" | list of weak references to the object (if defined)\n",
"\n"
"Help on class Punkt in module __main__:\n\nclass Punkt(__builtin__.object)\n | Klasa reprezentująca punkt w 2D.\n | \n | Methods defined here:\n | \n | __init__(self, x, y)\n | opis argumentów x i y.\n | \n | ----------------------------------------------------------------------\n | Data descriptors defined here:\n | \n | __dict__\n | dictionary for instance variables (if defined)\n | \n | __weakref__\n | list of weak references to the object (if defined)\n\n"
]
}
],
@ -309,13 +305,13 @@
{
"ename": "AttributeError",
"evalue": "'Parser' object has no attribute '__parse'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-80ee186598d3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mparser\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mParser\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mparser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mparser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__parse\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'Parser' object has no attribute '__parse'"
]
],
"output_type": "error"
}
],
"source": [
@ -340,7 +336,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 42,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -348,13 +344,15 @@
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.Punkt object at 0x7f728015b358>\n",
"<__main__.Punkt object at 0x7f728015b4a8>\n",
"<__main__.Punkt object at 0x7f728015b438>\n"
]
"ename": "TypeError",
"evalue": "instance has no next() method",
"traceback": [
"\u001b[1;31m\u001b[0m",
"\u001b[1;31mTypeError\u001b[0mTraceback (most recent call last)",
"\u001b[1;32m<ipython-input-42-c424f51c19dc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvertexes\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 15\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mv\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mFigura\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mPunkt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mPunkt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mPunkt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: instance has no next() method"
],
"output_type": "error"
}
],
"source": [
@ -390,7 +388,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -401,8 +399,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"0\n"
"79384200\n48959560\n0\n0\n"
]
}
],
@ -411,13 +408,23 @@
" atrybut = 0\n",
"\n",
"klasa = Klasa()\n",
"print(Klasa.atrybut)\n",
"print(klasa.atrybut)"
"#klasa2 = Klasa\n",
"#klasa2.atrybut=1\n",
"print(id(Klasa))\n",
"klasa2 = Klasa()\n",
"print(id(klasa2))\n",
"klasa2.atrybut=1\n",
"\n",
"print(Klasa.atrybut) \n",
"#atrybut wspólny dla wszystkich instancji\n",
"print(klasa.atrybut)\n",
"\n",
"klasa.temp = []"
]
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 62,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -425,11 +432,15 @@
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Jestem statyczna!\n"
]
"ename": "TypeError",
"evalue": "unbound method metoda() must be called with Klasa instance as first argument (got nothing instead)",
"traceback": [
"\u001b[1;31m\u001b[0m",
"\u001b[1;31mTypeError\u001b[0mTraceback (most recent call last)",
"\u001b[1;32m<ipython-input-62-63152cc2cac6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Jestem statyczna!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mKlasa\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmetoda\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: unbound method metoda() must be called with Klasa instance as first argument (got nothing instead)"
],
"output_type": "error"
}
],
"source": [
@ -437,6 +448,7 @@
" def __init__(self):\n",
" self.t = 0\n",
" def metoda():\n",
" # nie będzie działać self.t = 0\n",
" print(\"Jestem statyczna!\")\n",
"\n",
"Klasa.metoda()"
@ -455,7 +467,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -463,15 +475,15 @@
},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'nieistniejący_plik.txt'",
"output_type": "error",
"ename": "IOError",
"evalue": "[Errno 2] No such file or directory: 'nieistniej\\xc4\\x85cy_plik.txt'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-20-41928d542bef>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"nieistniejący_plik.txt\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mplik\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mplik\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'nieistniejący_plik.txt'"
]
"\u001b[1;31m\u001b[0m",
"\u001b[1;31mIOError\u001b[0mTraceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-1bebecf2f5c5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"nieistniejący_plik.txt\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplik\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mcontent\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mplik\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'nieistniej\\xc4\\x85cy_plik.txt'"
],
"output_type": "error"
}
],
"source": [
@ -481,7 +493,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 31,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -489,6 +501,8 @@
},
"outputs": [],
"source": [
"from pandas.compat import FileNotFoundError\n",
"\n",
"try:\n",
" with open(\"nieistniejący_plik.txt\") as plik:\n",
" content = plik.read()\n",
@ -498,7 +512,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 32,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -509,7 +523,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Warning [Errno 2] No such file or directory: 'nieistniejący_plik.txt'\n"
"Warning [Errno 2] No such file or directory: 'nieistniej\\xc4\\x85cy_plik.txt'\n"
]
}
],
@ -524,7 +538,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 13,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -535,7 +549,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Warning [Errno 2] No such file or directory: 'nieistniejący_plik.txt'\n"
"Warning [Errno 2] No such file or directory: 'nieistniej\\xc4\\x85cy_plik.txt'\n"
]
}
],
@ -550,7 +564,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 14,
"metadata": {
"collapsed": true,
"slideshow": {
@ -568,25 +582,40 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 19,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"outputs": [
{
"ename": "Exception",
"evalue": "Empty list of vertexes",
"traceback": [
"\u001b[1;31m\u001b[0m",
"\u001b[1;31mException\u001b[0mTraceback (most recent call last)",
"\u001b[1;32m<ipython-input-19-0884e4d4e2f2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvertexes\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvertexes\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mfigura\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mFigura\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-19-0884e4d4e2f2>\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, vertexes)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvertexes\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvertexes\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Empty list of vertexes\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvertexes\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvertexes\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mException\u001b[0m: Empty list of vertexes"
],
"output_type": "error"
}
],
"source": [
"class Figura:\n",
" def __init__(self, vertexes):\n",
" if len(vertexes) == 0:\n",
" raise Exception(\"Empty list of vertexes\")\n",
" self.vertexes = vertexes"
" self.vertexes = vertexes\n",
" \n",
"figura = Figura([])"
]
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 27,
"metadata": {
"collapsed": true,
"slideshow": {
@ -604,7 +633,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 34,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -614,13 +643,13 @@
{
"ename": "MyError",
"evalue": "Coś poszło nie tak!",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mMyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-36-4fb306b42ebc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mMyError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Coś poszło nie tak!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mMyError\u001b[0m: Coś poszło nie tak!"
]
"\u001b[1;31m\u001b[0m",
"\u001b[1;31mMyError\u001b[0mTraceback (most recent call last)",
"\u001b[1;32m<ipython-input-34-99f654d8334c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mMyError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Coś poszło nie tak!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mMyError\u001b[0m: Coś poszło nie tak!"
],
"output_type": "error"
}
],
"source": [

View File

@ -1,3 +1,21 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import math
def is_numeric(lista):
bool = False
for i in lista:
bool = isinstance(i, int) or isinstance(i, float)
if not bool:
return False
return bool
print(is_numeric([-1,2.1,3]))
print(is_numeric([-1,2.1, '3',3]))
print(is_numeric(['2']))
print(is_numeric([None]))

View File

@ -1,3 +1,57 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import random
random.seed(a=0)
# Klasa bazowa, która zawiera informacje o imieniu i nazwisku pracownika.
# Każdy pracownik posiada unikatowy numer 'id'.
class Employee:
# atrybut statyczny
id = 0
def __init__(self, firstname = 'Anonim', lastname = 'Gal'):
self.firstname = firstname
self.lastname = lastname
self.id = random.randrange(10000000, 50000000, 1)
# Metodę 'get_id', która zwraca identyfikator pracownika.
def get_id(self):
return self.id
# Klasy pochodna posiadająca dodatkową metodę 'recruit', która jako parament przyjmuje
# obiekt 'Employee' i zapisuje jego 'id' w liście 'self.recruited'.
class Recruiter(Employee):
def recruit(self, employee):
self.recruited = employee.get_id()
# Klasa pochodna przyjmuje w konstruktorze podstawowe informacje (imię i nazwisko)
# oraz obiekt rekturera. Ponadto posiada ona atrybut 'recruiter', który będzie
# przechowywał 'id' rekrutera.
class Programmer(Recruiter):
def __init__(self, firstname, lastname, recruiter_obj):
self.firstname = firstname
self.lastname = lastname
self.recruiter = recruiter_obj
recruiter = recruiter_obj.recruited
self.id = random.randrange(50000000, 100000000, 1)
pracownik = Employee('Jurek', 'P')
print pracownik.get_id()
rekruter = Recruiter('Kasia', 'P')
print rekruter.get_id()
rekruter.recruit(pracownik)
print rekruter.get_id()
programista = Programmer('Maciej', 'W', rekruter)
print programista.get_id()
rekruter.recruit(programista)
print rekruter.get_id()
print Employee.id

View File

@ -1,3 +1,75 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import math
# klasa, która reprezentuje punkt w przestrzeni wielowymiarowej.
class Point:
# konstruktor, który ma przyjmyje tylko 1 parametr: listę współrzednych.
def __init__(self,coordinates=[]):
if self.is_numeric(coordinates):
self.coordinates = coordinates
else:
self.coordinates = None
# funkcja sprawdzająca czy lista składa się z samych liczb.
def is_numeric(self, lista):
bool = False
for i in lista:
bool = isinstance(i, int) or isinstance(i, float)
if not bool:
return False
return bool
# metoda, która dodaje dwa punkty po współrzędnych i zwróci obiekt typu 'Punkt'.
def __add__(self, other):
if len(self) == len(other):
return Point(map((lambda x,y: x+y), self.coordinates, other.coordinates))
else:
raise DimensionError("Dodawany punkt ma inny wymiar!")
# metoda zwracająca łancuch znakowy, który w czytelny sposób przedstawi punkt.
def to_string(self):
if self.coordinates == None:
return "()"
else:
return "(" + ", ".join(str(c) for c in self.coordinates) + ")"
# metoda, która zwróci liczbę współrzędnych punktu.
def __len__(self):
if self.coordinates is not None:
return len(self.coordinates)
else:
return 0
# metoda, która działa dokładnie tak samo jak metoda `to_string`.
def __str__(self):
return self.to_string()
# wyjątek `DimensionError`, który zostaje wyrzucony, jeżeli dodawany punkt ma inny wymiar.
class DimensionError(Exception):
def __init__(self, text="Błąd wymiaru!"):
self.text = text
def __str__(self):
return self.text
p_0 = Point([])
print(str(p_0))
print(len(p_0))
p_1 = Point([ 1, 3, 5])
print(p_1)
print(len(p_1))
p_2 = Point([ 1, 2, 4])
print(p_2)
print(len(p_2))
try:
p_sum = p_1.__add__(p_2)
print(p_sum)
print(len(p_sum))
#print(len(str(p_1.__add__(p_2))))
p_sum = p_0.__add__(p_1)
except Exception as ex:
print("Warning: {}".format(ex))