From 0a345e2101514f22d1272f069d22b69adf83fef3 Mon Sep 17 00:00:00 2001 From: Tomasz Dwojak Date: Sun, 3 Dec 2017 11:08:00 +0100 Subject: [PATCH 1/5] Add klasy notebook --- labs04/Klasy.ipynb | 453 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 453 insertions(+) create mode 100644 labs04/Klasy.ipynb diff --git a/labs04/Klasy.ipynb b/labs04/Klasy.ipynb new file mode 100644 index 0000000..5c34339 --- /dev/null +++ b/labs04/Klasy.ipynb @@ -0,0 +1,453 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Wprowadzenie do Pythona: część 2\n", + "\n", + "## Tomasz Dwojak\n", + "\n", + "## 2 grudnia 2017" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Plan na dziś:\n", + " * ciąg dalszy podstaw,\n", + " * klasy,\n", + " * moduły," + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Python jest językiem obiektowym \n", + " * Wszystko jest obiektem: liczby, napisy, None, funkcje, moduły (biblioteki)..." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "Python da się lubić !\n" + ] + } + ], + "source": [ + "print((2017).imag)\n", + "print(' '.join(['Python', 'da', 'się', 'lubić', '!']))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Konstrukcja" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "class NajprostszaKlasa:\n", + " pass\n", + "\n", + "nasza_klasa = NajprostszaKlasa() # Uwaga na nawiasy na końcu!\n", + "print(type(nasza_klasa))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## (Pseudo) Konstruktor \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "class Punkt:\n", + " def __init__(self, x, y):\n", + " self.x = x\n", + " self.y = y\n", + "\n", + "punkt = Punkt(2, 3)\n", + "print(punkt.x)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "class Figura:\n", + " def __init__(self, vertexes):\n", + " self.vertexes = vertexes\n", + " \n", + " def liczba_wierzcholkow(self):\n", + " return len(self.vertexes)\n", + " \n", + " def dodaj_wierzcholek(self, x):\n", + " self.vertexes.append(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "class Prostokat(Figura):\n", + " def __init__(self, vertexes):\n", + " super().__init__(vertexes)\n", + " \n", + " def czy_jestem_kwadratem(self):\n", + " pass\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "class Figura:\n", + " def __init__(self, vertexes):\n", + " self.vertexes = vertexes\n", + " \n", + " def liczba_wierzcholkow(self):\n", + " return len(self.vertexes)\n", + " \n", + " def __len__(self):\n", + " return self.liczba_wierzcholkow()\n", + " \n", + "len(Figura([Punkt(2,3), Punkt(3,4), Punkt(0, 0)]))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Dobre praktyki: komentarze" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "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" + ] + } + ], + "source": [ + "class Punkt(object):\n", + " \"\"\"Klasa reprezentująca punkt w 2D.\"\"\"\n", + " def __init__(self, x, y):\n", + " \"\"\"opis argumentów x i y.\"\"\"\n", + " self.x = x\n", + " self.y = y" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "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" + ] + } + ], + "source": [ + "help(Punkt)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Enkapsulacja: publiczne czy prywatne?\n", + " * Prywatne zaczynają się od dwóch podkreśleń: ``__``, np. ``def __policz(self)``\n", + " * chronione tylko w konwencji, zaczynają się od '\\_', np. ``def _parse(self)``\n", + " * publiczne jest wszystko co nie zaczyna się od '\\_'." + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "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\u001b[0m in \u001b[0;36m\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'" + ] + } + ], + "source": [ + "class Parser(object):\n", + " def __parse(self): pass\n", + " def _get(self): pass\n", + "parser = Parser()\n", + "parser._get()\n", + "parser.__parse()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Iteratory" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<__main__.Punkt object at 0x7feb78045908>\n", + "<__main__.Punkt object at 0x7feb78045828>\n", + "<__main__.Punkt object at 0x7feb780457f0>\n" + ] + } + ], + "source": [ + "class Figura:\n", + " def __init__(self, vertexes):\n", + " self.vertexes = vertexes \n", + " \n", + " def __iter__(self):\n", + " self.index = -1\n", + " return self\n", + " \n", + " def __next__(self):\n", + " self.index += 1\n", + " if self.index == len(self.vertexes):\n", + " raise StopIteration\n", + " return self.vertexes[self.index]\n", + " \n", + "for v in Figura([Punkt(2,3), Punkt(3,4), Punkt(0, 0)]):\n", + " print(v)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Co zostało pominięte?\n", + " * pola klas i metody statyczne,\n", + " * dekoratory,\n", + " * mutable i immutable \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 9765333eaecf990f171fbec4d83a1606c31723fc Mon Sep 17 00:00:00 2001 From: Tomasz Dwojak Date: Sun, 3 Dec 2017 11:09:57 +0100 Subject: [PATCH 2/5] Add gitignore --- .gitignore | 5 + labs03/Podstawy 2.ipynb | 230 ++++++++++++++++++++++++++++++++++------ 2 files changed, 200 insertions(+), 35 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..947a68b --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# wiki files +Python2017.wiki/* +*/.ipynb_checkpoints/* +.ropeproject +*/.ropeproject diff --git a/labs03/Podstawy 2.ipynb b/labs03/Podstawy 2.ipynb index 53da94e..adf5795 100644 --- a/labs03/Podstawy 2.ipynb +++ b/labs03/Podstawy 2.ipynb @@ -49,22 +49,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": { "slideshow": { "slide_type": "slide" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "[1, 2, 3, 1, 2, 3]\n", + "123\n" + ] + } + ], "source": [ - "def dwojak(x): x *= 2\n", + "def dwojak(x): \n", + " x *= 2\n", + " return x\n", " \n", "l = [1, 2, 3]\n", "s = \"123\"\n", "\n", "dwojak(l)\n", "dwojak(s)\n", - "\n", + "print(dwojak(1))\n", "print(l)\n", "print(s)" ] @@ -104,16 +116,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "slideshow": { "slide_type": "slide" } }, - "outputs": [], + "outputs": [ + { + "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" + ] + } + ], "source": [ "def dwojak1(x): x *= 2\n", - "def dwojak2(x): x = x * 2\n", + "def dwojak2(x): \n", + " x = x * 2\n", + " print(\"F:\", x)\n", "\n", "l = [1,2, 3]\n", "dwojak1(l)\n", @@ -126,29 +150,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": { "slideshow": { "slide_type": "slide" } }, - "outputs": [], + "outputs": [ + { + "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)" + "print(l)\n", + "print(e)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": { "slideshow": { "slide_type": "slide" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1], [1], [1]]\n" + ] + } + ], "source": [ "e = []\n", "f = [e for i in range(3)]\n", @@ -172,18 +214,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { "slideshow": { "slide_type": "slide" } }, - "outputs": [], + "outputs": [ + { + "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\u001b[0m in \u001b[0;36m\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'" + ] + } + ], "source": [ - "t = (1, \"napis\", None)\n", - "elem = t[0]\n", + "t = (1, \"napis\", [])\n", + "t[-1].append(0)\n", "print(t)\n", - "print(len(t))" + "print(len(t))\n", + "print({t: None})" ] }, { @@ -199,19 +262,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "a == 1\n", + "b == (3, 4)\n" + ] + } + ], "source": [ "def suma(*args):\n", " return sum(args)\n", "print(suma(1,2,3,4,5))\n", "\n", - "def greet_me(**kwargs):\n", + "def greet_me(z=None,**kwargs):\n", " if kwargs is not None:\n", " for key, value in kwargs.items():\n", " print(\"%s == %s\" %(key,value))\n", @@ -231,16 +304,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "97\n", + "a\n", + "98\n", + "b\n", + "99\n", + "c\n", + "100\n", + "d\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", @@ -260,40 +349,74 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "W Paryżu najlepsze kasztany są na placu Pigalle\n", + "Zuzanna lubi je tylko jesienią.\n", + "\n", + ">>\n" + ] + } + ], "source": [ "plik = open(\"haslo.txt\", 'r')\n", "for linia in plik.readlines():\n", " print(linia.strip())\n", "print(plik.read())\n", + "print(\">>\")\n", "plik.close()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": { "slideshow": { "slide_type": "slide" } }, - "outputs": [], + "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\u001b[0m in \u001b[0;36m\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." + ] + } + ], "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": null, + "execution_count": 48, "metadata": { "collapsed": true, "slideshow": { @@ -334,13 +457,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "posix\n", + "Nazwa uzytkownika: tomaszd\n" + ] + } + ], "source": [ "import os\n", "print(os.name)\n", @@ -351,13 +483,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": { "slideshow": { "slide_type": "slide" } }, - "outputs": [], + "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" + } + ], "source": [ "from collections import *\n", "print(Counter(\"konstantynopolitańczykowianeczka\"))\n", @@ -394,13 +544,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "What's your name?\n", + "Tomasz\n", + "Welcome home, Tomasz.\n" + ] + } + ], "source": [ "name = input(\"What's your name?\\n\")\n", "print(\"Welcome home, {}.\".format(name))" From 8231e67c8f369886a9a414a244203487388065fb Mon Sep 17 00:00:00 2001 From: Tomasz Dwojak Date: Sun, 3 Dec 2017 11:10:26 +0100 Subject: [PATCH 3/5] add missed file example --- labs03/haslo.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 labs03/haslo.txt diff --git a/labs03/haslo.txt b/labs03/haslo.txt new file mode 100644 index 0000000..f269661 --- /dev/null +++ b/labs03/haslo.txt @@ -0,0 +1,2 @@ +W Paryżu najlepsze kasztany są na placu Pigalle +Zuzanna lubi je tylko jesienią. From f9c3661884a1d7a57844515e58a257021d8ce1e5 Mon Sep 17 00:00:00 2001 From: Tomasz Dwojak Date: Sun, 3 Dec 2017 11:14:29 +0100 Subject: [PATCH 4/5] Organize gitignore file --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.gitignore b/.gitignore index 947a68b..fad4507 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,12 @@ # wiki files Python2017.wiki/* + +# Jupyter Files */.ipynb_checkpoints/* + +# Rope files .ropeproject */.ropeproject + +# Labs temp files +labs03/haslo2.txt From 8c76a052001eb7efe6cb0610a238cad5181c7325 Mon Sep 17 00:00:00 2001 From: Tomasz Dwojak Date: Sun, 3 Dec 2017 13:05:05 +0100 Subject: [PATCH 5/5] Add labs4 --- labs04/Klasy.ipynb | 297 ++++++++++++++++++++++++++++++++++++++------- labs04/README.md | 18 +++ labs04/task01.py | 3 + labs04/task02.py | 3 + labs04/task03.py | 3 + 5 files changed, 280 insertions(+), 44 deletions(-) create mode 100644 labs04/README.md create mode 100644 labs04/task01.py create mode 100644 labs04/task02.py create mode 100644 labs04/task03.py diff --git a/labs04/Klasy.ipynb b/labs04/Klasy.ipynb index 5c34339..1cd0a99 100644 --- a/labs04/Klasy.ipynb +++ b/labs04/Klasy.ipynb @@ -9,11 +9,11 @@ } }, "source": [ - "# Wprowadzenie do Pythona: część 2\n", + "# Wprowadzenie do Pythona: Klasy\n", "\n", "## Tomasz Dwojak\n", "\n", - "## 2 grudnia 2017" + "### 3 grudnia 2017" ] }, { @@ -25,9 +25,8 @@ }, "source": [ "# Plan na dziś:\n", - " * ciąg dalszy podstaw,\n", " * klasy,\n", - " * moduły," + " * wyjątki." ] }, { @@ -229,39 +228,13 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 4, "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [ - { - "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" - ] - } - ], + "outputs": [], "source": [ "class Punkt(object):\n", " \"\"\"Klasa reprezentująca punkt w 2D.\"\"\"\n", @@ -273,7 +246,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 5, "metadata": { "slideshow": { "slide_type": "slide" @@ -326,7 +299,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 6, "metadata": { "slideshow": { "slide_type": "slide" @@ -340,7 +313,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\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;32m\u001b[0m in \u001b[0;36m\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'" ] } @@ -367,7 +340,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 7, "metadata": { "slideshow": { "slide_type": "fragment" @@ -378,9 +351,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "<__main__.Punkt object at 0x7feb78045908>\n", - "<__main__.Punkt object at 0x7feb78045828>\n", - "<__main__.Punkt object at 0x7feb780457f0>\n" + "<__main__.Punkt object at 0x7f728015b358>\n", + "<__main__.Punkt object at 0x7f728015b4a8>\n", + "<__main__.Punkt object at 0x7f728015b438>\n" ] } ], @@ -406,16 +379,252 @@ { "cell_type": "markdown", "metadata": { + "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ - "## Co zostało pominięte?\n", - " * pola klas i metody statyczne,\n", - " * dekoratory,\n", - " * mutable i immutable \n", - " " + "## Atrybuty i metody statyczne" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "0\n" + ] + } + ], + "source": [ + "class Klasa:\n", + " atrybut = 0\n", + "\n", + "klasa = Klasa()\n", + "print(Klasa.atrybut)\n", + "print(klasa.atrybut)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Jestem statyczna!\n" + ] + } + ], + "source": [ + "class Klasa:\n", + " def __init__(self):\n", + " self.t = 0\n", + " def metoda():\n", + " print(\"Jestem statyczna!\")\n", + "\n", + "Klasa.metoda()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Wyjątki" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "ename": "FileNotFoundError", + "evalue": "[Errno 2] No such file or directory: 'nieistniejący_plik.txt'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\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'" + ] + } + ], + "source": [ + "with open(\"nieistniejący_plik.txt\") as plik:\n", + " content = plik.read()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "try:\n", + " with open(\"nieistniejący_plik.txt\") as plik:\n", + " content = plik.read()\n", + "except FileNotFoundError:\n", + " contenct = \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Warning [Errno 2] No such file or directory: 'nieistniejący_plik.txt'\n" + ] + } + ], + "source": [ + "try:\n", + " with open(\"nieistniejący_plik.txt\") as plik:\n", + " content = plik.read()\n", + "except FileNotFoundError as e:\n", + " print(\"Warning {}\".format(e))\n", + " contenct = \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Warning [Errno 2] No such file or directory: 'nieistniejący_plik.txt'\n" + ] + } + ], + "source": [ + "try:\n", + " with open(\"nieistniejący_plik.txt\") as plik:\n", + " content = plik.read()\n", + "except Exception as e:\n", + " print(\"Warning {}\".format(e))\n", + " contenct = \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "try:\n", + " with open(\"nieistniejący_plik.txt\") as plik:\n", + " content = plik.read()\n", + "except:\n", + " contenct = \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "class Figura:\n", + " def __init__(self, vertexes):\n", + " if len(vertexes) == 0:\n", + " raise Exception(\"Empty list of vertexes\")\n", + " self.vertexes = vertexes" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "class MyError(Exception):\n", + " def __init__(self, text):\n", + " self.text = text\n", + " def __str__(self):\n", + " return self.text" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "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\u001b[0m in \u001b[0;36m\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!" + ] + } + ], + "source": [ + "raise MyError(\"Coś poszło nie tak!\")" ] }, { diff --git a/labs04/README.md b/labs04/README.md new file mode 100644 index 0000000..cce5ca4 --- /dev/null +++ b/labs04/README.md @@ -0,0 +1,18 @@ +**ćwiczenie 1** +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). + +**ćwiczenie 2** +Napisz prostą hierarchię klas: + * Klasa bazowa ``Employee``, która będzie zawierać informacje o imieniu i nazwisku pracownika. Ponadto każdy pracownik otrzyma numer ``id``, który będzie unikatowy. Wykorzystaj do tego atrybut statyczny. Napisz metodę ``get_id``, która zwraca identyfikator pracownika. + * Klasy pochodna: ``Recruiter``, która ma dodatkową mtodę ``recruit``, która jako parament przyjmuje obiekt ``Employee`` i zapisuje jego ``id`` w liście ``self.recruited``. + * Klasa pochodna ``Programmer``. Klasa ``Programmer`` ma przyjąć w konstruktorze podstawowe informacje (imię i nazwisko) oraz obiekt rekturera. Ponadto stwórz atrybut ``recruiter``, który będzie przechowywać ``id`` rekrutera. + +**ćwiczenie 3 (zadanie domowe) ** +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. + + diff --git a/labs04/task01.py b/labs04/task01.py new file mode 100644 index 0000000..88741a4 --- /dev/null +++ b/labs04/task01.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + diff --git a/labs04/task02.py b/labs04/task02.py new file mode 100644 index 0000000..88741a4 --- /dev/null +++ b/labs04/task02.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + diff --git a/labs04/task03.py b/labs04/task03.py new file mode 100644 index 0000000..88741a4 --- /dev/null +++ b/labs04/task03.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- +