{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Algoritmusok Python nyelven\n", "\n", "## 5. Előadás: Modulok bevezetés\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Modulok importálása\n", "\n", "- Az `import` két műveletet hajt végre\n", " 1. megkeresi a megadott név alapján a modult, \n", " 2. elérhetővi teszi a lokális scopeban\n", "\n", "### Teljes modul importálása\n", "`import modulnév` " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "\", \".join(dir(sys)) # a dir függvény megadja az objektum attribútumait és metódusait. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "type(sys)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "importálás után `modulnev.attribútum` alakban érjük el a modul tartalmát. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "random.choice([3,4,5]) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### almodulok importálása\n", "`from modulnév import almodul`\n", "\n", "Az almodult ezután `almodul` néven érjük el és nem `modulnév.almodul` néven." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from os import path\n", "\n", "try:\n", " os\n", "except NameError:\n", " print(\"os nincs definiálva\")\n", " \n", "try:\n", " path\n", " print(\"path megtalálva\")\n", "except NameError:\n", " print(\"path nincs definiálva\")\n", "try:\n", " os.path\n", " print(\"os.path megtalálva\")\n", "except NameError:\n", " print(\"os.path nincs definiálva\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Az `as` kulcssszó segítségével megadhatunk egy másik nevet is:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os as os_module\n", "\n", "try:\n", " os\n", "except NameError:\n", " print(\"os nincs definiálva\")\n", " \n", "try:\n", " os_module\n", " print(\"os_module megtalálva\")\n", "except NameError:\n", " print(\"os_module nincs definiálva\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Egyszerre több mindent is importálhatunk" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os, sys\n", "from sys import stdin, stderr, stdout" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Konkrét függvényeket és osztályokat is importálhatunk" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from argparse import ArgumentParser\n", "import inspect\n", "\n", "inspect.isclass(ArgumentParser)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Minden importálása egy modulból\n", "\n", "`from modulnév import *` után a modul teljes tartalma elérhető a `modulnév.` nélkül. \n", "\n", "**NAGYON NEM AJÁNLOTT** mert tudtunkon kívül felülírhatunk dolgokat. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def stat():\n", " print(\"Én egy statisztika vagyok\")\n", "stat()\n", "from os import *\n", "stat()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Modulok instalálása" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import wikipedia" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ha `ModuleNotFoundError`-t kapunk, akkor a Python nem találta meg a számítógépünkön a modult. Ekkor általában az a gond, hogy tényleg nincs is a gépünkön, így instalálni kell. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### pip (pip installs packages)\n", "A pip program automatikusan telepítődik az Anaconda keretében. Windowson az Anaconda Promptból érjük el, Linuxon pedig a terminálból. Használat:\n", "- `pip install modulnev` Modul instalálása \n", "- `pip uninstall modulnev` Modul törlése\n", "- `pip list` Instalált modulok listája \n", "\n", "Olyan packageket tudunk installálni amik megtalálhatóak a PyPi-ben. (https://pypi.org/)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Package Version \n", "---------------------------------- ------------\n", "-umpy 1.16.5 \n", "absl-py 0.9.0 \n", "alabaster 0.7.12 \n", "anaconda-client 1.7.2 \n", "anaconda-navigator 1.9.7 \n", "anaconda-project 0.8.3 \n", "asn1crypto 1.0.1 \n", "astor 0.8.1 \n", "astroid 2.3.1 \n", "astropy 3.2.1 \n", "atomicwrites 1.3.0 \n", "attrs 19.2.0 \n", "audioread 2.1.8 \n", "Babel 2.7.0 \n", "backcall 0.1.0 \n", "backports.functools-lru-cache 1.5 \n", "backports.os 0.1.1 \n", "backports.shutil-get-terminal-size 1.0.0 \n", "backports.tempfile 1.0 \n", "backports.weakref 1.0.post1 \n", "beautifulsoup4 4.8.0 \n", "bitarray 1.0.1 \n", "bkcharts 0.2 \n", "bleach 3.1.0 \n", "bokeh 1.3.4 \n", "boto 2.49.0 \n", "Bottleneck 1.2.1 \n", "cachetools 4.0.0 \n", "certifi 2019.9.11 \n", "cffi 1.12.3 \n", "chardet 3.0.4 \n", "Click 7.0 \n", "cloudpickle 1.2.2 \n", "clyent 1.2.2 \n", "colorama 0.4.1 \n", "comtypes 1.1.7 \n", "conda 4.7.12 \n", "conda-build 3.18.9 \n", "conda-package-handling 1.6.0 \n", "conda-verify 3.4.2 \n", "contextlib2 0.6.0 \n", "coverage 3.7.1 \n", "coveralls 1.1 \n", "cryptography 2.7 \n", "cycler 0.10.0 \n", "Cython 0.29.13 \n", "cytoolz 0.10.0 \n", "dask 2.5.2 \n", "decorator 4.4.0 \n", "defusedxml 0.6.0 \n", "distributed 2.5.2 \n", "docopt 0.6.2 \n", "docutils 0.15.2 \n", "entrypoints 0.3 \n", "et-xmlfile 1.0.1 \n", "fastcache 1.1.0 \n", "filelock 3.0.12 \n", "Flask 1.1.1 \n", "fsspec 0.5.2 \n", "future 0.17.1 \n", "gast 0.2.2 \n", "gevent 1.4.0 \n", "glob2 0.7 \n", "google-auth 1.11.3 \n", "google-auth-oauthlib 0.4.1 \n", "google-pasta 0.2.0 \n", "greenlet 0.4.15 \n", "grpcio 1.27.2 \n", "h5py 2.9.0 \n", "HeapDict 1.0.1 \n", "html5lib 1.0.1 \n", "idna 2.8 \n", "imageio 2.6.0 \n", "imageio-ffmpeg 0.4.1 \n", "imagesize 1.1.0 \n", "importlib-metadata 0.23 \n", "ipykernel 5.1.2 \n", "ipython 7.8.0 \n", "ipython-genutils 0.2.0 \n", "ipywidgets 7.5.1 \n", "isort 4.3.21 \n", "itsdangerous 1.1.0 \n", "jdcal 1.4.1 \n", "jedi 0.15.1 \n", "Jinja2 2.10.3 \n", "joblib 0.13.2 \n", "json5 0.8.5 \n", "jsonschema 3.0.2 \n", "jupyter 1.0.0 \n", "jupyter-client 5.3.3 \n", "jupyter-console 6.0.0 \n", "jupyter-contrib-core 0.3.3 \n", "jupyter-contrib-nbextensions 0.5.1 \n", "jupyter-core 4.5.0 \n", "jupyter-highlight-selected-word 0.2.0 \n", "jupyter-latex-envs 1.4.6 \n", "jupyter-nbextensions-configurator 0.4.1 \n", "jupyterlab 1.1.4 \n", "jupyterlab-server 1.0.6 \n", "jupyterthemes 0.20.0 \n", "Keras-Applications 1.0.8 \n", "Keras-Preprocessing 1.1.0 \n", "keyring 18.0.0 \n", "kiwisolver 1.1.0 \n", "lazy-object-proxy 1.4.2 \n", "lesscpy 0.14.0 \n", "libarchive-c 2.8 \n", "librosa 0.7.2 \n", "llvmlite 0.29.0 \n", "locket 0.2.0 \n", "lxml 4.4.1 \n", "Markdown 3.2.1 \n", "MarkupSafe 1.1.1 \n", "matplotlib 3.1.1 \n", "mccabe 0.6.1 \n", "menuinst 1.4.16 \n", "mistune 0.8.4 \n", "mkl-fft 1.0.14 \n", "mkl-random 1.1.0 \n", "mkl-service 2.3.0 \n", "mock 3.0.5 \n", "more-itertools 7.2.0 \n", "moviepy 1.0.1 \n", "mpmath 1.1.0 \n", "msgpack 0.6.1 \n", "multipledispatch 0.6.0 \n", "navigator-updater 0.2.1 \n", "nbconvert 5.6.0 \n", "nbformat 4.4.0 \n", "nbopen 0.6 \n", "networkx 2.3 \n", "nltk 3.4.5 \n", "noisereduce 1.1.0 \n", "nose 1.3.7 \n", "notebook 6.0.1 \n", "numba 0.45.1 \n", "numexpr 2.7.0 \n", "numpy 1.18.2 \n", "numpydoc 0.9.1 \n", "oauthlib 3.1.0 \n", "olefile 0.46 \n", "openpyxl 3.0.0 \n", "opt-einsum 3.2.0 \n", "packaging 19.2 \n", "pandas 0.25.1 \n", "pandocfilters 1.4.2 \n", "parso 0.5.1 \n", "partd 1.0.0 \n", "path.py 12.0.1 \n", "pathlib2 2.3.5 \n", "patsy 0.5.1 \n", "pep8 1.7.1 \n", "pickleshare 0.7.5 \n", "Pillow 7.0.0 \n", "pip 19.2.3 \n", "pkginfo 1.5.0.1 \n", "pluggy 0.13.0 \n", "ply 3.11 \n", "proglog 0.1.9 \n", "prometheus-client 0.7.1 \n", "prompt-toolkit 2.0.10 \n", "protobuf 3.11.3 \n", "psutil 5.6.3 \n", "py 1.8.0 \n", "pyasn1 0.4.8 \n", "pyasn1-modules 0.2.8 \n", "pycodestyle 2.5.0 \n", "pycosat 0.6.3 \n", "pycparser 2.19 \n", "pycrypto 2.6.1 \n", "pycurl 7.43.0.3 \n", "pyflakes 2.1.1 \n", "pygame 1.9.6 \n", "Pygments 2.4.2 \n", "pylint 2.4.2 \n", "pyodbc 4.0.27 \n", "pyOpenSSL 19.0.0 \n", "pyparsing 2.4.2 \n", "pyreadline 2.1 \n", "pyrsistent 0.15.4 \n", "PySocks 1.7.1 \n", "pytest 5.2.1 \n", "pytest-arraydiff 0.3 \n", "pytest-astropy 0.5.0 \n", "pytest-cov 2.5.1 \n", "pytest-doctestplus 0.4.0 \n", "pytest-openfiles 0.4.0 \n", "pytest-remotedata 0.3.2 \n", "python-dateutil 2.8.0 \n", "pytz 2019.3 \n", "PyWavelets 1.0.3 \n", "pywin32 223 \n", "pywinpty 0.5.5 \n", "PyYAML 5.1.2 \n", "pyzmq 18.1.0 \n", "QtAwesome 0.6.0 \n", "qtconsole 4.5.5 \n", "QtPy 1.9.0 \n", "requests 2.22.0 \n", "requests-oauthlib 1.3.0 \n", "resampy 0.2.2 \n", "rope 0.14.0 \n", "rsa 4.0 \n", "ruamel-yaml 0.15.46 \n", "scikit-image 0.15.0 \n", "scikit-learn 0.21.3 \n", "scipy 1.4.1 \n", "seaborn 0.9.0 \n", "selenium 3.141.0 \n", "Send2Trash 1.5.0 \n", "setuptools 41.4.0 \n", "simplegeneric 0.8.1 \n", "singledispatch 3.4.0.3 \n", "six 1.14.0 \n", "snowballstemmer 2.0.0 \n", "sortedcollections 1.1.2 \n", "sortedcontainers 2.1.0 \n", "SoundFile 0.10.3.post1\n", "soupsieve 1.9.3 \n", "Sphinx 2.2.0 \n", "sphinxcontrib-applehelp 1.0.1 \n", "sphinxcontrib-devhelp 1.0.1 \n", "sphinxcontrib-htmlhelp 1.0.2 \n", "sphinxcontrib-jsmath 1.0.1 \n", "sphinxcontrib-qthelp 1.0.2 \n", "sphinxcontrib-serializinghtml 1.1.3 \n", "sphinxcontrib-websupport 1.1.2 \n", "spyder 3.3.6 \n", "spyder-kernels 0.5.2 \n", "SQLAlchemy 1.3.9 \n", "statsmodels 0.10.1 \n", "sympy 1.4 \n", "tables 3.5.2 \n", "tblib 1.4.0 \n", "tensorboard 2.1.1 \n", "tensorflow 2.1.0 \n", "tensorflow-estimator 2.1.0 \n", "termcolor 1.1.0 \n", "terminado 0.8.2 \n", "testpath 0.4.2 \n", "TexSoup 0.2.0 \n", "toolz 0.10.0 \n", "tornado 6.0.3 \n", "tqdm 4.36.1 \n", "traitlets 4.3.3 \n", "unicodecsv 0.14.1 \n", "urllib3 1.24.2 \n", "wcwidth 0.1.7 \n", "webbot 0.1.4 \n", "webencodings 0.5.1 \n", "Werkzeug 0.16.0 \n", "wheel 0.33.6 \n", "widgetsnbextension 3.5.1 \n", "wikipedia 1.4.0 \n", "win-inet-pton 1.1.0 \n", "win-unicode-console 0.5 \n", "wincertstore 0.2 \n", "wrapt 1.11.2 \n", "wtfpython 0.2.1 \n", "xlrd 1.2.0 \n", "XlsxWriter 1.2.1 \n", "xlwings 0.15.10 \n", "xlwt 1.3.0 \n", "zict 1.0.0 \n", "zipp 0.6.0 \n" ] } ], "source": [ "!pip list # a ! a mögötte lévő parancsot lefuttatja a terminálban" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hasznos beépített packagek, standard library\n", "Részletek: https://docs.python.org/3/library/\n", "\n", "Numeric and Mathematical Modules\n", "- numbers — Numeric abstract base classes\n", "- math — Mathematical functions\n", "- cmath — Mathematical functions for complex numbers\n", "- decimal — Decimal fixed point and floating point arithmetic\n", "- fractions — Rational numbers\n", "- random — Generate pseudo-random numbers\n", "- statistics — Mathematical statistics functions\n", "\n", "Functional Programming Modules\n", "- itertools — Functions creating iterators for efficient looping\n", "- functools — Higher-order functions and operations on callable objects\n", "- operator — Standard operators as functions\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hasznos külső packagek\n", "- matek és tudományos modulok \n", " - numpy \n", " - scipy\n", " - sympy\n", "- vizualizáció\n", " - Matplotlib\n", " - bokeh \n", "- deep, machine és mindenféle learning, adatbányászat\n", " - Keras\n", " - TensorFlow\n", " - PyTorch\n", " - Pandas\n", "- nyelvi feldolgozás\n", " - NLTK \n", "- parszolás\n", " - BeautifulSoup" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Kiegészítő anyagok\n", "- Saját package létrehozása https://packaging.python.org/tutorials/distributing-packages/\n", "- Virtualenv: Komolyabb Python használat esetén különböző projektekhez különböző verizói lehetnek szükségesek egy adott packagenek. A Virtualenv ennek kezelésében (is) segít.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }