diff --git a/Podstawy 2.ipynb b/Podstawy 2.ipynb new file mode 100644 index 0000000..cb6f6c0 --- /dev/null +++ b/Podstawy 2.ipynb @@ -0,0 +1,630 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Python: podstaw ciąg dalszy\n", + "\n", + "## Tomasz Dwojak\n", + "\n", + "### 2 grudnia 2017" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Co już znamy?\n", + " * podstawowe typy danych i operacje na nich\n", + " * Instrukcje sterujące: ``if``, ``for``\n", + " * pisanie funkcji\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "## Co na dziś?\n", + "\n", + " * ``tuple`` i ``set``,\n", + " * operacje na plikach,\n", + " * coś więcej o funkcjach\n", + " * korzystanie z bibliotek\n", + " * przegląd najważniejszych bibliotek" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "[1, 2, 3, 1, 2, 3]\n", + "123\n" + ] + } + ], + "source": [ + "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", + "print(dwojak(1))\n", + "print(l)\n", + "print(s)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Mutable i Immutable\n", + "\n", + "### Mutable\n", + " * listy,\n", + " * słowniki,\n", + " * sety,\n", + " * własnoręcznie zdefiniowane klasy.\n", + " \n", + "### Immutable\n", + " * liczby: inty i floaty,\n", + " * napisy,\n", + " * tuple." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Nieoczywistości" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "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): \n", + " x = x * 2\n", + " print(\"F:\", x)\n", + "\n", + "l = [1,2, 3]\n", + "dwojak1(l)\n", + "print(l)\n", + "\n", + "l = [1,2, 3]\n", + "dwojak2(l)\n", + "print(l)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "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.append(4)\n", + "print(l)\n", + "print(e)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1], [1], [1]]\n" + ] + } + ], + "source": [ + "e = []\n", + "f = [e for i in range(3)]\n", + "f[0].append(1)\n", + "print(f)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## To może ``tuple``?\n", + " * stały rozmiar,\n", + " * immutable,\n", + " * mogą być kluczami w słownikach" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "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\", [])\n", + "t[-1].append(0)\n", + "print(t)\n", + "print(len(t))\n", + "print({t: None})" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Funkcje cz. 2" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "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(z=None,**kwargs):\n", + " if kwargs is not None:\n", + " for key, value in kwargs.items():\n", + " print(\"%s == %s\" %(key,value))\n", + "greet_me(a=1, b=(3,4))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Generatory" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "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", + " print(c)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Operacje na plikach" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "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": 47, + "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\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())" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "with open(\"haslo2.txt\", 'w') as plik:\n", + " for word in ('corect', 'horse', 'battery', 'staple'):\n", + " plik.write(word)\n", + " plik.write('\\n')\n", + "with open(\"haslo2.txt\", 'w+') as plik:\n", + " plik.writelines([' '.join(('corect', 'horse', 'battery', 'staple'))])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Korzystanie z modułów" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Importowanie" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "posix\n", + "Nazwa uzytkownika: tomaszd\n" + ] + } + ], + "source": [ + "import os\n", + "print(os.name)\n", + "\n", + "from os import getenv\n", + "print('Nazwa uzytkownika: {}'.format(getenv(\"USER\")))" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "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" + } + ], + "source": [ + "from collections import *\n", + "print(Counter(\"konstantynopolitańczykowianeczka\"))\n", + "\n", + "import numpy as np\n", + "np.array([[1, 3, 4, 5]], dtype='float32')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Instalacja\n", + "\n", + " * lokalnie (per użytkownik) lub globalnie\n", + " * pyCharm lub linia komend, np. ``pip install --user flask`` lub ``python -m pip install --user flask``" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Wczytywanie z klawiatury" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "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))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "liczby = eval(input(\"Podaj liczby\"))\n", + "print(sum(liczby))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Co ominąłem?\n", + "\n", + " * klasy (jutro)\n", + " * podział programu na pliki (jutro)\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 +} diff --git a/README.md b/README.md index d63cc53..45dc541 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,36 @@ -Link do arkusza z obecnością: - -https://docs.google.com/spreadsheets/d/1TxJSYM-voKJ7siCgtkCS77pSF0l7I-OtzfCrQdq9RtE/edit?usp=sharing \ No newline at end of file +# Laboratoria 3 + +## Zadania + +**ćwiczenie 0** +Sklonuj repozytorium ``https://github.com/realpython/python-scripts``, które różne, przydatne skrypty. Przejrzyj je i zobacz na ile jesteś w stanie zrozumieć co i jak robią. Uruchom kilka z nich, np. ``27_send_sms.py``. + +**ćwiczenie 1** +Każdy obiekt w Pythonie na wbudowaną funkcję ``id()``, która zwraca liczbę, która jest unikatowa i stała dla obiektu. Pozwala ona w prosty sposób sprawdzić, który obiekt jest *mutable*a, który *immutable*: jeżeli po wykonaniu operacji, zwracana liczba jest stała, to oznacza, że obiekt jest *mutable*. Sprawdź zachowanie funkcji na obiektach typy: + * lista, + * napis (string), + * liczba zmiennoprzecinkowa. + +**ćwiczenie 2** +Napisz generator, który będzie zwracać ``n`` kolejnych liczb ciągu Fibonacciego (``F(0)=1, F(1)=1, FN=F(N-1) + F(N-2)``). + +**ć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. + +**ćwiczenie 4** +Zainstaluj bibliotekę ``weather-api`` (https://pypi.python.org/pypi/weather-api). Korzystając z niej: + * Wypisz informacje o aktualnej pogodzie. + * Napisz funkcję, która zamieni stopnie ``F`` na ``C``. + * Korzystając z prognozy, znajdź dzień, w którym będzie najzimniej. Wypisz nazwę tygodnia (w języku polskim) i temperaturę w C. + +**ćwiczenie 5** +Katalog scores zawiera 64 pliki tekstowe, które posiadają informacje o wysokości miary ``BLEU`` na różnych etapach trenowania modelu. Nazwa każdego pliku na postać ``model.iterXXXXXXX.npz.bleu``, gdzie ``XXXXXXX``, to liczba iteracji.Zawartość każdego pliku jest podobna i ma następującą formę: *BLEU = YY.YY, 44.4/18.5/9.3/5.0 (BP=1.000, ratio=1.072, hyp_len=45976, ref_len=42903)*, gdzie ``YY.YY`` to wartość miary ``BLEU``. Znajdź plik, który zawiera najwyższą wartość miary ``BLEU``. + * Wykorzystaj bibliotekę ``glob`` (https://docs.python.org/2/library/glob.html) + * Wyświetl tylko pełną nazwe pliku (wraz z ścieżką). + + + + diff --git a/haslo.txt b/haslo.txt new file mode 100644 index 0000000..0bed6b6 --- /dev/null +++ b/haslo.txt @@ -0,0 +1,2 @@ +W Paryżu najlepsze kasztany są na placu Pigalle +Zuzanna lubi je tylko jesienią. diff --git a/model.iter10000.npz.bleu b/model.iter10000.npz.bleu new file mode 100644 index 0000000..df3dc94 --- /dev/null +++ b/model.iter10000.npz.bleu @@ -0,0 +1 @@ +BLEU = 2.02, 17.1/3.6/1.0/0.3 (BP=1.000, ratio=1.872, hyp_len=80326, ref_len=42903) diff --git a/model.iter100000.npz.bleu b/model.iter100000.npz.bleu new file mode 100644 index 0000000..9743be5 --- /dev/null +++ b/model.iter100000.npz.bleu @@ -0,0 +1 @@ +BLEU = 13.99, 44.4/18.5/9.3/5.0 (BP=1.000, ratio=1.072, hyp_len=45976, ref_len=42903)