├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── advanced ├── 0_Iterators_generators_and_coroutines.ipynb ├── 1_databases_mysql_and_mongo.ipynb ├── 2_Testing.ipynb ├── 3_Profiling_and_networking.ipynb ├── 4_Packaging_and_debugging.ipynb ├── 5_decorators.ipynb ├── fib_fac.py ├── fichero.txt ├── lib_to_test.py ├── mod_debugging.py ├── mod_debugging_2.py ├── simcache.py └── tests_lib.py ├── basic ├── 0_What_is_python.ipynb ├── 1_Numbers_Strings.ipynb ├── 2_Collections_and_dictionaries.ipynb ├── 3_Booleans_Flow_Control_and_Comprehension.ipynb ├── 4_Functions_classes_and_modules.ipynb ├── basic_tmp │ ├── __init__.py │ └── my_modules.py ├── my_modules.py └── tmp_file.txt ├── best-practices ├── 0_What_is_python.ipynb ├── 1_Duck_Typing.ipynb ├── 2_wow.ipynb ├── 3_common_java_errors.ipynb ├── 4_django_django_rest_framework.ipynb ├── 5_functional_programming.ipynb └── file.txt ├── docker-entrypoint.sh ├── requirements.txt └── sysadmin ├── 03_parsing_benchmarks.ipynb ├── 0_Path_Management_Encoding.ipynb ├── 1_Gathering_system_data.ipynb ├── 2_logging.ipynb ├── logger.yml ├── parsing-lifecycle.png ├── proc ├── 1 │ ├── attr │ │ ├── current │ │ ├── exec │ │ ├── fscreate │ │ ├── keycreate │ │ ├── prev │ │ └── sockcreate │ ├── autogroup │ ├── cgroup │ ├── cmdline │ ├── comm │ ├── coredump_filter │ ├── cpuset │ ├── loginuid │ ├── mountinfo │ ├── mounts │ ├── net │ │ ├── anycast6 │ │ ├── arp │ │ ├── connector │ │ ├── dev │ │ ├── dev_mcast │ │ ├── dev_snmp6 │ │ │ ├── eth0 │ │ │ ├── eth1 │ │ │ ├── eth2 │ │ │ └── lo │ │ ├── icmp │ │ ├── if_inet6 │ │ ├── igmp │ │ ├── igmp6 │ │ ├── ip6_flowlabel │ │ ├── ip6_mr_cache │ │ ├── ip6_mr_vif │ │ ├── ip_mr_cache │ │ ├── ip_mr_vif │ │ ├── ipv6_route │ │ ├── mcfilter │ │ ├── mcfilter6 │ │ ├── netfilter │ │ │ ├── nf_log │ │ │ └── nf_queue │ │ ├── netlink │ │ ├── netstat │ │ ├── packet │ │ ├── protocols │ │ ├── psched │ │ ├── ptype │ │ ├── raw │ │ ├── raw6 │ │ ├── route │ │ ├── rt6_stats │ │ ├── rt_acct │ │ ├── rt_cache │ │ ├── snmp │ │ ├── snmp6 │ │ ├── sockstat │ │ ├── sockstat6 │ │ ├── softnet_stat │ │ ├── stat │ │ │ ├── arp_cache │ │ │ ├── ndisc_cache │ │ │ ├── nf_conntrack │ │ │ └── rt_cache │ │ ├── tcp │ │ ├── tcp6 │ │ ├── udp │ │ ├── udp6 │ │ ├── udplite │ │ ├── udplite6 │ │ ├── unix │ │ ├── wireless │ │ └── xfrm_stat │ ├── oom_adj │ ├── oom_score │ ├── oom_score_adj │ ├── sched │ ├── schedstat │ ├── sessionid │ ├── stat │ ├── statm │ ├── status │ ├── task │ │ └── 1 │ │ │ ├── attr │ │ │ ├── current │ │ │ ├── exec │ │ │ ├── fscreate │ │ │ ├── keycreate │ │ │ ├── prev │ │ │ └── sockcreate │ │ │ ├── cgroup │ │ │ ├── cmdline │ │ │ ├── comm │ │ │ ├── cpuset │ │ │ ├── loginuid │ │ │ ├── mountinfo │ │ │ ├── mounts │ │ │ ├── oom_adj │ │ │ ├── oom_score │ │ │ ├── oom_score_adj │ │ │ ├── sched │ │ │ ├── schedstat │ │ │ ├── sessionid │ │ │ ├── stat │ │ │ ├── statm │ │ │ ├── status │ │ │ └── wchan │ └── wchan └── diskstats ├── solutions.py ├── test.py └── windows.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # editors files 2 | .idea 3 | *.iml 4 | 5 | # Byte-compiled / optimized / DLL files 6 | __pycache__/ 7 | *.py[cod] 8 | *$py.class 9 | 10 | # C extensions 11 | *.so 12 | 13 | # Distribution / packaging 14 | .Python 15 | env/ 16 | env2/ 17 | build/ 18 | develop-eggs/ 19 | dist/ 20 | downloads/ 21 | eggs/ 22 | .eggs/ 23 | lib/ 24 | lib64/ 25 | parts/ 26 | sdist/ 27 | var/ 28 | *.egg-info/ 29 | .installed.cfg 30 | *.egg 31 | tmp_eggs/ 32 | 33 | # PyInstaller 34 | # Usually these files are written by a python script from a template 35 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 36 | *.manifest 37 | *.spec 38 | 39 | # Installer logs 40 | pip-log.txt 41 | pip-delete-this-directory.txt 42 | 43 | # Unit test / coverage reports 44 | htmlcov/ 45 | .tox/ 46 | .coverage 47 | .coverage.* 48 | .cache 49 | nosetests.xml 50 | coverage.xml 51 | *,cover 52 | .hypothesis/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | 62 | # Flask stuff: 63 | instance/ 64 | .webassets-cache 65 | 66 | # Scrapy stuff: 67 | .scrapy 68 | .project 69 | .pydevproject 70 | .settings 71 | 72 | # Sphinx documentation 73 | docs/_build/ 74 | 75 | # PyBuilder 76 | target/ 77 | 78 | # IPython Notebook 79 | .ipynb_checkpoints 80 | 81 | # pyenv 82 | .python-version 83 | 84 | # celery beat schedule file 85 | celerybeat-schedule 86 | 87 | # dotenv 88 | .env 89 | 90 | # virtualenv 91 | venv/ 92 | ENV/ 93 | 94 | # Spyder project settings 95 | .spyderproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # Docker image for ipython notebook and various 3 | # python courses 4 | # 5 | FROM python:2.7 6 | 7 | RUN apt-get -y update && apt-get -y install gcc make python-dev python-pip 8 | RUN apt-get -y update && apt-get -y install build-essential libblas-dev liblapack-dev gfortran libfreetype6-dev libpng-dev 9 | RUN apt-get -y update && apt-get -y install tree 10 | 11 | # Install MySQL 12 | RUN echo mysql-server mysql-server/root_password password pass | debconf-set-selections;\ 13 | echo mysql-server mysql-server/root_password_again password pass | debconf-set-selections;\ 14 | apt-get -y update && apt-get install -y mysql-server mysql-client libmysqlclient-dev 15 | 16 | # Install mongodb 17 | RUN apt-get -y update && apt-get install -y mongodb 18 | RUN apt-get -y clean 19 | 20 | RUN pip install -U pip 21 | 22 | COPY requirements.txt /requirements.txt 23 | COPY advanced /curso-python/advanced 24 | COPY basic /curso-python/basic 25 | COPY sysadmin /curso-python/sysadmin 26 | COPY best-practices /curso-python/best-practices 27 | 28 | # install requirements 29 | RUN pip install -r /requirements.txt 30 | 31 | EXPOSE 8888 32 | 33 | COPY docker-entrypoint.sh /docker-entrypoint.sh 34 | RUN chmod +x /docker-entrypoint.sh 35 | 36 | ENTRYPOINT ["./docker-entrypoint.sh"] 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Curso python 2 | 3 | Browse this course with the [online notebook viewer!](http://nbviewer.jupyter.org/github/ealogar/curso-python/tree/master/) 4 | 5 | ## Setup 6 | If you use docker, build and run the image: 7 | 8 | You can build the container 9 | ```sh 10 | $docker build -t curso-python . 11 | ``` 12 | 13 | or you can get it from artifactory (TID only): 14 | ```sh 15 | $docker login dockerhub.hi.inet 16 | # It requests your TID user name and your TID password 17 | $docker pull dockerhub.hi.inet/calba/curso-python 18 | ``` 19 | 20 | once you have it 21 | 22 | 23 | ```sh 24 | $docker run -d --name curso-python -p 8888:8888 curso-python 25 | ``` 26 | 27 | Start the container (not after first run): 28 | After build and start you can simply start or stop your container: 29 | 30 | ```sh 31 | $docker start curso-python 32 | ``` 33 | 34 | If you are asked by a token to access jupyter notebook: 35 | 36 | ```sh 37 | $docker logs curso-python 38 | ``` 39 | 40 | And use the url to get access to the notebooks. 41 | 42 | You can also run the course locally if you have python already installed (and pip) 43 | 44 | ```sh 45 | $pip install -r requirements.txt 46 | ``` 47 | 48 | 49 | ### Install jupyterhub with python3 to share locally with multiple users 50 | 51 | See 52 | 53 | https://github.com/jupyterhub/jupyterhub#prerequisites 54 | 55 | https://github.com/jupyterhub/jupyterhub/wiki/Installation-of-Jupyterhub-on-remote-server 56 | 57 | -------------------------------------------------------------------------------- /advanced/2_Testing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "# EXERCISE:\n", 12 | "# Execute nosetests in this folder:\n", 13 | "!nosetests -sv\n", 14 | "# Exercise: review tests_lib and make the tests to pass\n", 15 | "# Use this cell" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | " - Regarding mocking, in Python monkey patching classes is trivial. It is really\n", 23 | " easy to replace instances methods or modules functions with your own stuff.\n", 24 | " - To use real full-featured mocks use the 'mock' library:\n", 25 | " - http://www.voidspace.org.uk/python/mock/\n", 26 | " - It was added to Python standard library in 3.3 as 'unittest.mock'\n", 27 | " - http://docs.python.org/3/library/unittest.mock.html\n", 28 | "\n", 29 | " - mock library allows to replace parts of our code in a safe and easy way with \n", 30 | " - mock objects. You can assert how they are called too.\n", 31 | " - Base class is Mock although is advisable to use MagicMock subclass\n", 32 | " - MagicMock has all \"magic\" methods already pre-created\n", 33 | " - patch utility allow to monkey patching at module and class level within the \n", 34 | " scope of test\n", 35 | " - Let's see a quick example " 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": { 42 | "collapsed": false 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "from mock import MagicMock\n", 47 | "from lib_to_test import ProductionClass" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": { 54 | "collapsed": false 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "from mock import MagicMock\n", 59 | "from lib_to_test import ProductionClass\n", 60 | "prod = ProductionClass()\n", 61 | "prod.prod_method = MagicMock(return_value=3)\n", 62 | "print prod.prod_method(40, 3)\n", 63 | "prod.prod_method.assert_called_once_with(40, 3)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": { 70 | "collapsed": false 71 | }, 72 | "outputs": [], 73 | "source": [ 74 | "# with side_effect we can return several values and raise exceptions too\n", 75 | "prod.prod_method = MagicMock(side_effect=ValueError(\"not number\"))\n", 76 | "prod.prod_method(\"my_string\")" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": { 83 | "collapsed": false 84 | }, 85 | "outputs": [], 86 | "source": [ 87 | "prod.prod_method = MagicMock(side_effect=[2, 3, 4])\n", 88 | "prod.prod_method(34, 2)\n", 89 | "prod.prod_method(34, 2)\n", 90 | "prod.prod_method(34, 2)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": { 97 | "collapsed": true 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "# But we are modifying our source code, we better use patch\n", 102 | "from mock import patch" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": { 109 | "collapsed": true 110 | }, 111 | "outputs": [], 112 | "source": [ 113 | "import lib_to_test\n", 114 | "# patch as decorator, provides MagicMock in function decorated\n", 115 | "@patch('lib_to_test.ProductionClass')\n", 116 | "def test(mockClass):\n", 117 | " lib_to_test.ProductionClass() # Already imported in module..\n", 118 | " print 'ProductionClass {}'.format(lib_to_test.ProductionClass)\n", 119 | " assert mockClass is lib_to_test.ProductionClass\n", 120 | " print mockClass.called" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [], 130 | "source": [ 131 | "test()" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": { 138 | "collapsed": false 139 | }, 140 | "outputs": [], 141 | "source": [ 142 | "print lib_to_test.ProductionClass" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": { 149 | "collapsed": true 150 | }, 151 | "outputs": [], 152 | "source": [ 153 | "# We can also use patch for system libraries...\n", 154 | "import sys\n", 155 | "\n", 156 | "# patch as context manager\n", 157 | "with patch('sys.exit') as exitMock:\n", 158 | " try:\n", 159 | " sys.exit()\n", 160 | " except SystemExit:\n", 161 | " print \"exiting programm\"\n", 162 | " exitMock.assert_called_once_with()" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": null, 168 | "metadata": { 169 | "collapsed": false 170 | }, 171 | "outputs": [], 172 | "source": [ 173 | "try:\n", 174 | " sys.exit()\n", 175 | "except SystemExit:\n", 176 | " print \"exiting programm\"" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": null, 182 | "metadata": { 183 | "collapsed": false 184 | }, 185 | "outputs": [], 186 | "source": [ 187 | "# we can patch some object, dict, open and magic methods....\n", 188 | "\n", 189 | "my_patch = patch.object(prod, 'prod_method')\n", 190 | "method_mock = my_patch.start()\n", 191 | "method_mock.return_value = 5000\n", 192 | "print prod.prod_method(30, 3)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": { 199 | "collapsed": false 200 | }, 201 | "outputs": [], 202 | "source": [ 203 | "# we stop the patch now ...\n", 204 | "my_patch.stop()\n", 205 | "print prod.prod_method\n", 206 | "print prod.prod_method(30, 3)" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": null, 212 | "metadata": { 213 | "collapsed": false 214 | }, 215 | "outputs": [], 216 | "source": [ 217 | "# WTF ??, yes before we change ProductionClass inside the module....\n", 218 | "print prod.prod_method\n", 219 | "prod = ProductionClass()\n", 220 | "my_patch = patch.object(prod, 'prod_method')\n", 221 | "method_mock = my_patch.start()\n", 222 | "method_mock.return_value = 5000\n", 223 | "print prod.prod_method(30, 3)\n", 224 | "my_patch.stop()\n", 225 | "print prod.prod_method(30, 3)" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": null, 231 | "metadata": { 232 | "collapsed": true 233 | }, 234 | "outputs": [], 235 | "source": [ 236 | "# magic methods\n", 237 | "mock = MagicMock()\n", 238 | "mock.__str__.return_value = 'foobarbaz'\n", 239 | "cad = str(mock)\n", 240 | "mock.__str__.assert_called_with()\n", 241 | "print cad" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": null, 247 | "metadata": { 248 | "collapsed": false 249 | }, 250 | "outputs": [], 251 | "source": [ 252 | "# dictionary\n", 253 | "foo = {'key': 'value'}\n", 254 | "original = foo.copy()\n", 255 | "with patch.dict(foo, {'newkey': 'newvalue'}, clear=True):\n", 256 | " print \"dict foo is now: {}\".format(foo)\n", 257 | " assert foo == {'newkey': 'newvalue'}" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "metadata": { 264 | "collapsed": false 265 | }, 266 | "outputs": [], 267 | "source": [ 268 | "print \"dict foo is {}\".format(foo)\n", 269 | "assert foo == original" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": { 276 | "collapsed": false 277 | }, 278 | "outputs": [], 279 | "source": [ 280 | "# open function can be patched tooo....\n", 281 | "from mock import mock_open\n", 282 | "\n", 283 | "# write\n", 284 | "with patch('__builtin__.open', mock_open()) as m:\n", 285 | " with open('foo.txt', 'w') as f:\n", 286 | " f.write('something')\n", 287 | " print m.mock_calls" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": null, 293 | "metadata": { 294 | "collapsed": false 295 | }, 296 | "outputs": [], 297 | "source": [ 298 | "import os\n", 299 | "os.path.exists('foo.txt')" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": null, 305 | "metadata": { 306 | "collapsed": false 307 | }, 308 | "outputs": [], 309 | "source": [ 310 | "# read\n", 311 | "with patch('__builtin__.open', mock_open( read_data='foo')) as m:\n", 312 | " with open('foo.txt') as f:\n", 313 | " file = f.read()\n", 314 | " print file" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": null, 320 | "metadata": { 321 | "collapsed": false 322 | }, 323 | "outputs": [], 324 | "source": [ 325 | "!pip install nose-cov\n", 326 | "!nosetests -s -v --with-cover --cover-package=lib_to_test" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "## SOURCES:\n", 334 | " - http://docs.python.org/2/library/unittest.html\n", 335 | " - http://pymotw.com/2/unittest/index.html\n", 336 | " - http://www.voidspace.org.uk/python/mock/" 337 | ] 338 | } 339 | ], 340 | "metadata": { 341 | "kernelspec": { 342 | "display_name": "Python 2", 343 | "language": "python", 344 | "name": "python2" 345 | }, 346 | "language_info": { 347 | "codemirror_mode": { 348 | "name": "ipython", 349 | "version": 2.0 350 | }, 351 | "file_extension": ".py", 352 | "mimetype": "text/x-python", 353 | "name": "python", 354 | "nbconvert_exporter": "python", 355 | "pygments_lexer": "ipython2", 356 | "version": "2.7.12" 357 | } 358 | }, 359 | "nbformat": 4, 360 | "nbformat_minor": 0 361 | } -------------------------------------------------------------------------------- /advanced/3_Profiling_and_networking.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Profiling" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "# EXERCISE:\n", 19 | "# Execute the following command:\n", 20 | "!python -m timeit '\"-\".join([str(n) for n in range(100)])'\n", 21 | "\n", 22 | "# Now execute the following:\n", 23 | "!python -m timeit '\"-\".join(map(str, range(100)))'\n", 24 | "\n", 25 | "# Now execute:\n", 26 | "!python -m timeit --setup 'func = lambda n: \"-\".join(map(str, range(n)))' 'func(100)'\n", 27 | "\n", 28 | "# And finally:\n", 29 | "!python -m timeit --setup 'func = lambda n: \"-\".join(map(str, xrange(n)))' 'func(100)'" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "### timeit module:\n", 37 | " - Provides a simple way to time the execution of Python statements.\n", 38 | " - Provides both command line and programatic interfaces." 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": { 45 | "collapsed": false 46 | }, 47 | "outputs": [], 48 | "source": [ 49 | "import timeit\n", 50 | "print timeit.timeit(stmt='func(100)', setup='func = lambda n: \"-\".join(map(str, xrange(n)))', number=10000)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": { 57 | "collapsed": true 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "def fibonacci(n):\n", 62 | " \"\"\"Return the nth fibonacci number\"\"\"\n", 63 | " if n < 2:\n", 64 | " return n\n", 65 | " return fibonacci(n - 1) + fibonacci(n - 2)\n", 66 | "\n", 67 | "\n", 68 | "def fib_15():\n", 69 | " return fibonacci(15)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": { 76 | "collapsed": false 77 | }, 78 | "outputs": [], 79 | "source": [ 80 | "print timeit.timeit(stmt=fib_15, number=100)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "metadata": { 87 | "collapsed": false 88 | }, 89 | "outputs": [], 90 | "source": [ 91 | "# Actually, a Timer class is provided inside timeit module\n", 92 | "\n", 93 | "t = timeit.Timer(stmt=fib_15)\n", 94 | "print t.repeat(repeat=3, number=100)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": { 101 | "collapsed": false 102 | }, 103 | "outputs": [], 104 | "source": [ 105 | "# EXERCISE:\n", 106 | "# Execute the following command:\n", 107 | "!python -m cProfile fib_fac.py\n", 108 | "\n", 109 | "# Now execute the following:\n", 110 | "!python -m cProfile -s time fib_fac.py\n", 111 | "\n", 112 | "# Now execute:\n", 113 | "!python -m cProfile -s cumulative fib_fac.py\n", 114 | "\n", 115 | "# And finally:\n", 116 | "!python -m cProfile -s calls fib_fac.py" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "### cProfile:\n", 124 | "- Deterministic profiling of Python programs.\n", 125 | "- C extension with reasonable overhead.\n", 126 | "- Provides both command line and programatic interfaces.\n", 127 | "\n", 128 | "- There is a pure Python alternative module with the same interface: profile" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": { 135 | "collapsed": true 136 | }, 137 | "outputs": [], 138 | "source": [ 139 | "import cProfile\n", 140 | "import pstats\n", 141 | "\n", 142 | "\n", 143 | "filename = \"cprofile_fib_fac.log\"\n", 144 | "max_num_lines = 3" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": null, 150 | "metadata": { 151 | "collapsed": false 152 | }, 153 | "outputs": [], 154 | "source": [ 155 | "# Note that in normal execution the import is not needed inside the statement string (incompatibility with pydemo)\n", 156 | "cProfile.run(statement=\"from fib_fac import fib_fac; fib_fac()\", filename=filename)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": { 163 | "collapsed": false 164 | }, 165 | "outputs": [], 166 | "source": [ 167 | "stats = pstats.Stats(filename)\n", 168 | "stats.strip_dirs().sort_stats('time').print_stats(max_num_lines)\n", 169 | "stats.strip_dirs().sort_stats('cumulative').print_stats(max_num_lines)\n", 170 | "stats.strip_dirs().sort_stats('calls').print_stats(max_num_lines)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "### Use pstats.Stats to parse and print cProfile output\n", 178 | "- You can sort the records:\n", 179 | " - time: single execution time of a function\n", 180 | " - cumulative: accumulated execution time of a function\n", 181 | " - calls: number of times a function was called\n", 182 | " - Others: http://docs.python.org/2/library/profile.html#pstats.Stats.sort_stats" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": { 189 | "collapsed": false 190 | }, 191 | "outputs": [], 192 | "source": [ 193 | "# Exercise: which option is better\n", 194 | "def opc1():\n", 195 | " fruits = tuple(str(i) for i in xrange(100))\n", 196 | " out = ''\n", 197 | " for fruit in fruits:\n", 198 | " out += fruit +':'\n", 199 | " return out\n", 200 | "\n", 201 | "def opc2():\n", 202 | " format_str = '%s:' * 100\n", 203 | " fruits = tuple(str(i) for i in xrange(100))\n", 204 | " out = format_str % fruits\n", 205 | " return out\n", 206 | "\n", 207 | "def opc3():\n", 208 | " format_str = '{}:' * 100\n", 209 | " fruits = tuple(str(i) for i in xrange(100))\n", 210 | " out = format_str.format(*fruits)\n", 211 | " return out\n", 212 | "\n", 213 | "def opc4():\n", 214 | " fruits = tuple(str(i) for i in xrange(100))\n", 215 | " out = ':'.join(fruits)\n", 216 | " return out" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": {}, 222 | "source": [ 223 | "# Networking" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "### Standard library provides some modules for network operation:\n", 231 | "\n", 232 | "- **socket**: provides access to the low-level C BSD socket interface, includes\n", 233 | " a 'socket' class and some useful functions\n", 234 | "\n", 235 | "- **urllib2**: a library to perform HTTP requests (get, post, multipart...)\n", 236 | "\n", 237 | "- **httplib**: client side libraries of HTTP and HTTPS protocols, used by urllib2\n", 238 | "\n", 239 | "- **urlparse**: library with functions to parse URLs\n", 240 | "\n", 241 | "- Note that in Py3k urlparse, urllib and urllib2 have been merged in package urllib" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": null, 247 | "metadata": { 248 | "collapsed": false 249 | }, 250 | "outputs": [], 251 | "source": [ 252 | "import socket\n", 253 | "\n", 254 | "\n", 255 | "# In addition to typical socket class, some useful functions are provided\n", 256 | "print socket.gethostname()\n", 257 | "print socket.getfqdn()\n", 258 | "print socket.gethostbyname(socket.getfqdn())" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "metadata": { 265 | "collapsed": true 266 | }, 267 | "outputs": [], 268 | "source": [ 269 | "#Let's see how to perform HTTP requests\n", 270 | "\n", 271 | "\n", 272 | "import requests # Requests is much better than any other standard library alternative" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": null, 278 | "metadata": { 279 | "collapsed": true 280 | }, 281 | "outputs": [], 282 | "source": [ 283 | "location = \"41.41,2.22\"\n", 284 | "key = \"5nrhptjvus6gdnf9e6x75as9\"\n", 285 | "num_days = 3\n", 286 | "url_pattern = \"http://api.worldweatheronline.com/free/v1/weather.ashx?q={loc}&format=json&num_of_days={days}&key={key}\"\n", 287 | "r = requests.get(url=url_pattern.format(loc=location, days=num_days, key=key),\n", 288 | " headers={'content-type': 'application/json'}) # It supports all HTTP methods, auth, proxies, post multipart..." 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "metadata": { 295 | "collapsed": false 296 | }, 297 | "outputs": [], 298 | "source": [ 299 | "# Let's check the response\n", 300 | "print r.status_code\n", 301 | "print r.encoding\n", 302 | "print r.text" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": null, 308 | "metadata": { 309 | "collapsed": false 310 | }, 311 | "outputs": [], 312 | "source": [ 313 | "# And of course it parses the JSON\n", 314 | "print type(r.json()) # Uses simplejson or std lib json" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": null, 320 | "metadata": { 321 | "collapsed": false 322 | }, 323 | "outputs": [], 324 | "source": [ 325 | "from pprint import pprint\n", 326 | "pprint(r.json()[\"data\"][\"current_condition\"][0])" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "### compare it with using urllib2\n", 334 | "https://gist.github.com/kennethreitz/973705\n", 335 | "\n", 336 | "- For low level socket operations use 'socket'\n", 337 | "- Use 'requests' always if possible for HTTP operation\n", 338 | "- Use 'urllib2' or 'httplib' as a fallback for special behaviour" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": null, 344 | "metadata": { 345 | "collapsed": false 346 | }, 347 | "outputs": [], 348 | "source": [ 349 | "# Implement a connection pool with requests\n", 350 | "requestsSession = requests.session()\n", 351 | "httpAdapter = requests.adapters.HTTPAdapter(pool_connections=10,\n", 352 | " pool_maxsize=15)\n", 353 | "requestsSession.mount('http://', httpAdapter)" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": null, 359 | "metadata": { 360 | "collapsed": false 361 | }, 362 | "outputs": [], 363 | "source": [ 364 | "requestsSession.get(url=url_pattern.format(loc=location, days=num_days, key=key),\n", 365 | " headers={'content-type': 'application/json'})" 366 | ] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": {}, 371 | "source": [ 372 | "## SOURCES:\n", 373 | " - http://docs.python.org/2/library/timeit.html\n", 374 | " - http://pymotw.com/2/timeit/\n", 375 | " - http://docs.python.org/2/library/profile.html\n", 376 | " - http://pymotw.com/2/profile/\n", 377 | " - http://docs.python.org/2/library/socket.html\n", 378 | " - http://pymotw.com/2/socket/\n", 379 | " - http://docs.python.org/2/library/urllib2.html\n", 380 | " - http://pymotw.com/2/urllib2/\n", 381 | " - http://docs.python.org/2/library/httplib.html\n", 382 | " - http://docs.python-requests.org/en/latest/" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": null, 388 | "metadata": { 389 | "collapsed": true 390 | }, 391 | "outputs": [], 392 | "source": [] 393 | } 394 | ], 395 | "metadata": { 396 | "kernelspec": { 397 | "display_name": "Python 2", 398 | "language": "python", 399 | "name": "python2" 400 | }, 401 | "language_info": { 402 | "codemirror_mode": { 403 | "name": "ipython", 404 | "version": 2 405 | }, 406 | "file_extension": ".py", 407 | "mimetype": "text/x-python", 408 | "name": "python", 409 | "nbconvert_exporter": "python", 410 | "pygments_lexer": "ipython2", 411 | "version": "2.7.5" 412 | } 413 | }, 414 | "nbformat": 4, 415 | "nbformat_minor": 1 416 | } 417 | -------------------------------------------------------------------------------- /advanced/4_Packaging_and_debugging.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Packaging\n", 8 | "- **Distribute** is a fork of the Setuptools project.\n", 9 | " - Distribute is intended to replace Setuptools as the standard method for\n", 10 | " working with Python module distributions.\n", 11 | "\n", 12 | "- **Setuptools**: download, build, install, upgrade, and uninstall Python packages -- easily!\n", 13 | "\n", 14 | " - The main feature is the setup.py scripts calling 'setuptools.setup' function\n", 15 | "\n", 16 | "- There is an initiative to create a standard packaging and distribution tool\n", 17 | " integrated in the core in Python 3.4 ( http://www.python.org/dev/peps/pep-0426/ )\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": { 24 | "collapsed": false 25 | }, 26 | "outputs": [], 27 | "source": [ 28 | "# EXERCISE:\n", 29 | "# Download and decompress pep8 and requests eggs:\n", 30 | "!mkdir -p tmp_eggs\n", 31 | "#git clone git@github.com:PyCQA/pycodestyle.git\n", 32 | "#git clone git@github.com:kennethreitz/requests.git\n", 33 | "\n", 34 | "# - Check their setup.py and how it calls the setup function:\n", 35 | "#\n", 36 | "# - Check...\n", 37 | "# - All the '*_requires' arguments\n", 38 | "# - The 'entry_points' argument in pep8\n", 39 | "# - The auxiliary functions used\n", 40 | "\n", 41 | "# - Create pep8 egg and RPM\n", 42 | "# $ python setup.py bdist_egg\n", 43 | "# $ python setup.py bdist_rpm # Requires rpm-build\n", 44 | "#\n" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "# Debugging" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "- **pdb** is Python's interactive debugger\n", 59 | "- It provides command line and programmatic interfaces" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": { 66 | "collapsed": true 67 | }, 68 | "outputs": [], 69 | "source": [ 70 | "# EXERCISE:\n", 71 | "# - Execute mod_debugging.py with pdb from command line:\n", 72 | "# $ python -m pdb mod_debugging.py\n", 73 | "#\n", 74 | "# - Type 'h' or 'help' to check available debugger commands\n", 75 | "# - Type 'next' several times and check the backtrace" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [], 85 | "source": [ 86 | "# EXERCISE:\n", 87 | "# - Execute mod_debugging.caller_func with pdb from a Python interpreter:\n", 88 | "# $ python\n", 89 | "# >>> import pdb\n", 90 | "# >>> import mod_debugging\n", 91 | "# >>> pdb.run('mod_debugging.caller_func()')\n", 92 | "# > (1)()\n", 93 | "# (Pdb)\n", 94 | "#\n", 95 | "# - Type 'h' or 'help' to check available debugger commands\n", 96 | "# - Type 'next' and check the backtrace" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": { 103 | "collapsed": false 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "import ipdb # Much better than standard library debugger" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": { 114 | "collapsed": true 115 | }, 116 | "outputs": [], 117 | "source": [ 118 | "# EXERCISE:\n", 119 | "# - Execute this file with Python\n", 120 | "# $ python mod_debugging_2.py\n", 121 | "#\n", 122 | "# - Type 'h' or 'help' to check available debugger commands\n", 123 | "# - Besides debugger commands you can execute Python code, check variables...\n", 124 | "# - Try to call another_func(123, 456)\n", 125 | "# - Check the backtrace" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": null, 131 | "metadata": { 132 | "collapsed": true 133 | }, 134 | "outputs": [], 135 | "source": [] 136 | } 137 | ], 138 | "metadata": { 139 | "kernelspec": { 140 | "display_name": "Python 2", 141 | "language": "python", 142 | "name": "python2" 143 | }, 144 | "language_info": { 145 | "codemirror_mode": { 146 | "name": "ipython", 147 | "version": 2 148 | }, 149 | "file_extension": ".py", 150 | "mimetype": "text/x-python", 151 | "name": "python", 152 | "nbconvert_exporter": "python", 153 | "pygments_lexer": "ipython2", 154 | "version": "2.7.12" 155 | } 156 | }, 157 | "nbformat": 4, 158 | "nbformat_minor": 1 159 | } 160 | -------------------------------------------------------------------------------- /advanced/fib_fac.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | 4 | def factorial(n): 5 | """Return the factorial of n""" 6 | if n < 2: 7 | return 1 8 | return n * factorial(n - 1) 9 | 10 | 11 | def fibonacci(n): 12 | """Return the nth fibonacci number""" 13 | if n < 2: 14 | return n 15 | return fibonacci(n - 1) + fibonacci(n - 2) 16 | 17 | 18 | def fib_fac(x=30, y=900): 19 | fib = fibonacci(x) 20 | fac = factorial(y) 21 | print "fibonacci({}):".format(x), fib 22 | print "factorial({}):".format(y), fac 23 | 24 | 25 | if __name__ == "__main__": 26 | def opc1(): 27 | fruits = tuple(str(i) for i in xrange(100)) 28 | out = '' 29 | for fruit in fruits: 30 | out += fruit +':' 31 | return out 32 | 33 | def opc2(): 34 | format_str = '%s:' * 100 35 | fruits = tuple(str(i) for i in xrange(100)) 36 | out = format_str % fruits 37 | return out 38 | 39 | def opc3(): 40 | format_str = '{}:' * 100 41 | fruits = tuple(str(i) for i in xrange(100)) 42 | out = format_str.format(*fruits) 43 | return out 44 | 45 | def opc4(): 46 | fruits = tuple(str(i) for i in xrange(100)) 47 | out = ':'.join(fruits) 48 | return out 49 | import timeit 50 | print timeit.timeit(stmt=opc4, number=100) 51 | fib_fac() 52 | -------------------------------------------------------------------------------- /advanced/fichero.txt: -------------------------------------------------------------------------------- 1 | Python is cool\n 2 | Python is cool\n 3 | python is cool\n 4 | -------------------------------------------------------------------------------- /advanced/lib_to_test.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | u""" 3 | MOD: Testing, library to test 4 | """ 5 | 6 | 7 | def sum(x, y): 8 | """Sums two numbers 9 | """ 10 | return x + y 11 | 12 | 13 | def subs(x, y): 14 | """Substract two numbers 15 | """ 16 | return x - y 17 | 18 | 19 | def mul(x, y): 20 | """Multiply two numbers 21 | """ 22 | return x * y 23 | 24 | 25 | def div(x, y): 26 | """Divide two numbers 27 | """ 28 | return x / y 29 | 30 | 31 | def fail(x, y): 32 | """Raises ValueError(x, y) 33 | """ 34 | raise ValueError(x, y) 35 | 36 | class ProductionClass(object): 37 | def prod_method(self, arg1, arg2): 38 | return arg1 / arg2 39 | 40 | class ProductionClass2(object): 41 | def prod_method(self, arg1, arg2): 42 | print "prod_method called" 43 | return arg1 / arg2 44 | -------------------------------------------------------------------------------- /advanced/mod_debugging.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | u""" 3 | MOD: Debugging 4 | """ 5 | import pdb 6 | 7 | 8 | def another_func(arg1, arg2): 9 | print "Another func was called:", arg1, arg2 10 | 11 | 12 | def func_to_fail(): 13 | my_string = "This is a string" 14 | my_float = 123.456 15 | pdb.set_trace() 16 | another_func(my_string, my_float) 17 | raise ValueError(13) 18 | 19 | 20 | def caller_func(): 21 | func_to_fail() 22 | 23 | 24 | if __name__ == "__main__": 25 | caller_func() 26 | -------------------------------------------------------------------------------- /advanced/mod_debugging_2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #-*- coding: utf-8 -*- 3 | u""" 4 | MOD: Debugging 5 | """ 6 | import ipdb 7 | 8 | #=========================================================================== 9 | # EXERCISE: 10 | # - Execute this file with ipthon debugger 11 | # $ ipython --pdb mod_debugging_2.py 12 | # 13 | # - Type 'h' or 'help' to check available debugger commands 14 | #=========================================================================== 15 | 16 | 17 | def another_func(arg1, arg2): 18 | print "Another func was called:", arg1, arg2 19 | 20 | 21 | def func_to_fail(): 22 | my_string = "This is a string" 23 | my_float = 123.456 24 | ipdb.set_trace() 25 | another_func(my_string, my_float) 26 | raise ValueError(13) 27 | 28 | 29 | def caller_func(): 30 | func_to_fail() 31 | 32 | 33 | if __name__ == "__main__": 34 | caller_func() 35 | -------------------------------------------------------------------------------- /advanced/simcache.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | u""" 3 | 4 | >>> import simcache 5 | 6 | >>> simcache.set_key("my_key", "my_value", ttl=600) 7 | 8 | >>> print simcache.CACHE["my_key"] 9 | (1366544507.87305, 'my_value') # (expiration time, value) 10 | 11 | >>> print simcache.get_key("my_key") 12 | my_value 13 | 14 | >>> print simcache.clear() 15 | 16 | >>> print simcache.get_key("my_key") 17 | None 18 | """ 19 | import time 20 | from collections import OrderedDict 21 | 22 | 23 | CACHE = OrderedDict() 24 | CACHE_SIZE = 300 25 | CACHE_TTL = 3600 26 | 27 | 28 | def set_key(key, value, ttl=None): 29 | """Set a key value in the cache with its expiration time. 30 | If no ttl (in seconds) is provided CACHE_TTL is taken by default. 31 | If cache length exceeds CACHE_SIZE when adding a key, the oldest (first inserted) key is removed (FIFO) 32 | """ 33 | CACHE[key] = (time.time() + (ttl or CACHE_TTL), value) 34 | if len(CACHE) > CACHE_SIZE: 35 | CACHE.popitem(last=False) 36 | 37 | 38 | def get_key(key): 39 | """Retrieve a key value from the cache. 40 | Returns None if does not exist or the key expired. 41 | If the key expired it is removed from the cache. 42 | """ 43 | content = CACHE.get(key, None) 44 | if content: 45 | if content[0] > time.time(): 46 | return content[1] 47 | else: 48 | del CACHE[key] 49 | return None 50 | 51 | 52 | def clear_keys(): 53 | """Remove all cache keys content 54 | """ 55 | global CACHE 56 | CACHE = OrderedDict() 57 | -------------------------------------------------------------------------------- /advanced/tests_lib.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | from __future__ import unicode_literals, print_function 3 | import unittest 4 | import lib_to_test 5 | 6 | 7 | # Some nosetests versions require 'test' to appear in the class name 8 | class TestCaseExample(unittest.TestCase): 9 | """This is a tests class, grouping a bunch of tests 10 | """ 11 | @classmethod 12 | def setUpClass(cls): 13 | """setUpClass is executed BEFORE all class tests and setUps 14 | """ 15 | print("SET_UP_CLASS") 16 | super(TestCaseExample, cls).setUpClass() 17 | 18 | @classmethod 19 | def tearDownClass(cls): 20 | """tearDownClass is executed AFTER all class tests and tearDowns 21 | """ 22 | print("TEAR_DOWN_CLASS") 23 | super(TestCaseExample, cls).tearDownClass() 24 | 25 | def setUp(self): 26 | """setUp is executed BEFORE each test 27 | """ 28 | print("SET_UP") 29 | 30 | def tearDown(self): 31 | """tearDown is executed AFTER each test 32 | """ 33 | print("TEAR_DOWN") 34 | 35 | # Some nosetests versions require 'test' to appear in the function name 36 | def test_sum(self): 37 | """This is an actual empty test 38 | """ 39 | print("TESTING") 40 | 41 | 42 | class TestMyMathLib(unittest.TestCase): 43 | """This is another tests class 44 | """ 45 | 46 | def setUp(self): 47 | """setUp is executed BEFORE each test 48 | """ 49 | self.x = 7 50 | self.y = 2 51 | 52 | # Some nosetests versions require 'test' to appear in the function name 53 | def test_sum(self): 54 | """This is an actual test 55 | """ 56 | expected = 9 57 | # unittest.TestCase provides several assertion methods 58 | self.assertEqual(expected, lib_to_test.sum(self.x, self.y), 59 | "Sum results differ") 60 | self.assertTrue(expected == lib_to_test.sum(self.x, self.y)) # Message is optional 61 | self.assertGreaterEqual(lib_to_test.sum(self.x, self.y), self.x) 62 | self.assertFalse(lib_to_test.sum(0, 0)) 63 | self.assertIsNotNone(lib_to_test.sum(0, 0)) 64 | 65 | # We can add as tests methods and classes as desired 66 | def test_subs(self): 67 | """Second test 68 | """ 69 | x = [10, 20, 30] 70 | y = [1, 2, 3] 71 | expected = [9, 18, 27] 72 | expected_unordered = [9, 27, 18] 73 | # We can compare collections or sequences 74 | self.assertEqual(expected, map(lib_to_test.subs, x, y)) 75 | self.assertItemsEqual(expected_unordered, map(lib_to_test.subs, x, y)) 76 | 77 | # Classes and its tests are not executed in strict order 78 | def test_errors_raised(self): 79 | """Another test 80 | """ 81 | # Check an exception is raised 82 | self.assertRaises(ZeroDivisionError, lib_to_test.div, self.x, 0) 83 | # Check an exception is raised and check exception content 84 | with self.assertRaises(ValueError) as exc_ctxt_mgr: 85 | lib_to_test.fail(self.x, self.y) 86 | self.assertEqual((self.x, self.y), exc_ctxt_mgr.exception.args) 87 | 88 | def test_which_fails(self): 89 | """This test will fail 90 | """ 91 | self.assertTrue(True) 92 | 93 | def test_which_crashes(self): 94 | """This test will crash 95 | """ 96 | self.assertTrue(lib_to_test.sum()) 97 | 98 | 99 | def setUpModule(): 100 | """setUpModule is executed BEFORE all test classes 101 | """ 102 | # Typically open connections, change settings 103 | print("SET_UP_MODULE") 104 | 105 | 106 | def tearDownModule(): 107 | """tearDownModule is executed AFTER all test classes 108 | """ 109 | print("TEAR_DOWN_MODULE") 110 | 111 | 112 | if __name__ == "__main__": 113 | #import sys;sys.argv = ['', 'Test.testName'] 114 | unittest.main() 115 | -------------------------------------------------------------------------------- /basic/0_What_is_python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Run the following:" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "#Run this from command line\n", 19 | "!python -c \"import this\"" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": { 26 | "collapsed": false 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "#Inside a python console\n", 31 | "import this" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "## From official website ( http://www.python.org/ ):\n", 39 | "\n", 40 | "Python is a programming language that lets you work more quickly and integrate\n", 41 | "your systems more effectively. You can learn to use Python and see almost\n", 42 | "immediate gains in productivity and lower maintenance costs.\n", 43 | "\n", 44 | "## Executive summary from official website ( http://www.python.org/doc/essays/blurb.html )\n", 45 | " \n", 46 | " Python is an interpreted, object-oriented, high-level programming language\n", 47 | " with dynamic semantics. Its high-level built in data structures, combined\n", 48 | " with dynamic typing and dynamic binding, make it very attractive for Rapid\n", 49 | " Application Development, as well as for use as a scripting or glue language\n", 50 | " to connect existing components together. Python's simple, easy to learn syntax\n", 51 | " emphasizes readability and therefore reduces the cost of program maintenance.\n", 52 | " Python supports modules and packages, which encourages program modularity and\n", 53 | " code reuse. The Python interpreter and the extensive standard library are\n", 54 | " available in source or binary form without charge for all major platforms,\n", 55 | " and can be freely distributed.\n" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "### TO SUM UP\n", 63 | " - quick development\n", 64 | " - simple, readable, easy to learn syntax\n", 65 | " - general purpose\n", 66 | " - interpreted (not compiled)\n", 67 | " - object-oriented\n", 68 | " - high-level\n", 69 | " - dynamic semantics (aka execution semantics)\n", 70 | " - fully dynamic typing\n", 71 | " - dynamic binding\n", 72 | " - low programs manteinance cost\n", 73 | " - modularity and code reuse\n", 74 | " - no licensing costs\n", 75 | " - extensive standard library, \"batteries included\"\n", 76 | " - imperative and functional programming\n", 77 | " - automatic memory management" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": { 84 | "collapsed": false 85 | }, 86 | "outputs": [], 87 | "source": [ 88 | "# execution semantics\n", 89 | "for i in range(10):\n", 90 | " print i**2\n", 91 | "print \"Outside for loop\"" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": { 98 | "collapsed": false 99 | }, 100 | "outputs": [], 101 | "source": [ 102 | "# dynamic binding\n", 103 | "my_str = \"hola\"\n", 104 | "type(my_str)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": { 111 | "collapsed": false 112 | }, 113 | "outputs": [], 114 | "source": [ 115 | "# dynamic binding\n", 116 | "my_str = 90\n", 117 | "type(my_str)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": { 124 | "collapsed": false 125 | }, 126 | "outputs": [], 127 | "source": [ 128 | "# fully dynamic typing\n", 129 | "4 +5 +6" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": { 136 | "collapsed": false 137 | }, 138 | "outputs": [], 139 | "source": [ 140 | "# fully dynamic typing\n", 141 | "4 + \"hola\"" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "## HISTORY\n", 149 | " - http://docs.python.org/2/license.html\n", 150 | " - http://en.wikipedia.org/wiki/History_of_Python\n", 151 | " - http://en.wikipedia.org/wiki/Benevolent_Dictator_For_Life\n", 152 | " - http://www.python.org/dev/peps/pep-0001/\n", 153 | " - http://docs.python.org/3.0/whatsnew/3.0.html" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "## TOOLS\n", 161 | "\n", 162 | "**CPython** is the real name of default standard Python implementation\n", 163 | " - The interpreter is deployed together with standard library\n", 164 | " - It can take a file as argument to run. Otherwise it opens and interactive console\n", 165 | " - With '-m' parameter you can execute directly certain modules (debugging, profiling)\n", 166 | " - Implemented in C\n", 167 | " \n", 168 | "There are Python implementations in other languages:\n", 169 | " - Jython: Python 2.5 interpreter written in Java which runs bytecode in the JVM\n", 170 | " - IronPython: Similar approach for .NET Common Language Runtime\n", 171 | " - JS, C++, CIL...\n", 172 | " - Stackless Python: CPython fork with microthreads concurrency\n", 173 | " - PyPy: Python 2.7 interpreter implemented in Python. Really fast, multi-core...!!!\n", 174 | "\n", 175 | "\n", 176 | "**IPython**: create a comprehensive environment for interactive and exploratory computing \n", 177 | "An enhanced interactive Python shell \n", 178 | "An architecture for interactive parallel computing:\n", 179 | " - Other powerful interactive shells (terminal and Qt-based)\n", 180 | " - A browser-based notebook with support for code, text, mathematical expressions\n", 181 | " inline plots and other rich media\n", 182 | " - Support for interactive data visualization and use of GUI toolkits\n", 183 | " - Flexible, embeddable interpreters to load into your own projects\n", 184 | " - Easy to use, high performance tools for parallel computing\n", 185 | " - Recently funded with $1.15M from the Alfred P. Sloan Foundation\n", 186 | "\n", 187 | "\n", 188 | "**virtualenv**: a tool to create isolated Python environments \n", 189 | "It simply changes your PATH environment var to point to a different folder\n", 190 | "\n", 191 | "\n", 192 | "**PyPi**: The Python Package Index is a repository of software for the Python programming language. \n", 193 | "There are currently 89324 packages here. \n", 194 | "The packages are 'eggs'.\n", 195 | "\n", 196 | "**pip**: A tool for installing and managing Python packages\n", 197 | "It installs packages from PyPi, local folders or Git and other repositories\n", 198 | "It can read a list of packages from a file or generate the list of installed packages" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "### Installing pip and virtualenv for a python interpreter\n", 206 | "\n", 207 | "```sh\n", 208 | "wget https://bootstrap.pypa.io/get-pip.py\n", 209 | "python get-pip.py\n", 210 | "pip install virtualenv\n", 211 | "virtualenv env\n", 212 | "source env/bin/activate\n", 213 | "# finish virtualenv running deactivate\n", 214 | "```\n" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "**IDE**?\n", 222 | " - PyCharm (we have licenses, ask EPG)\n", 223 | " - Eclipse + Pydev\n", 224 | " - NetBeans\n", 225 | " - Eric\n", 226 | " - NINJA IDE\n", 227 | " - Aptana Studio 3\n", 228 | " - SPE\n", 229 | " - Python's IDLE (not recommendable at all)\n", 230 | " - ...\n", 231 | " - Emacs\n", 232 | " - Vi\n", 233 | "http://wiki.python.org/moin/IntegratedDevelopmentEnvironments \n", 234 | "Lots of good IDEs, it's up to you!" 235 | ] 236 | } 237 | ], 238 | "metadata": { 239 | "kernelspec": { 240 | "display_name": "Python 2", 241 | "language": "python", 242 | "name": "python2" 243 | }, 244 | "language_info": { 245 | "codemirror_mode": { 246 | "name": "ipython", 247 | "version": 2 248 | }, 249 | "file_extension": ".py", 250 | "mimetype": "text/x-python", 251 | "name": "python", 252 | "nbconvert_exporter": "python", 253 | "pygments_lexer": "ipython2", 254 | "version": "2.7.12" 255 | } 256 | }, 257 | "nbformat": 4, 258 | "nbformat_minor": 1 259 | } 260 | -------------------------------------------------------------------------------- /basic/basic_tmp/__init__.py: -------------------------------------------------------------------------------- 1 | from my_modules import func 2 | 3 | -------------------------------------------------------------------------------- /basic/basic_tmp/my_modules.py: -------------------------------------------------------------------------------- 1 | print "'__name__' value:", __name__ 2 | 3 | def func(): 4 | print "Called func in", __name__ 5 | 6 | print "'func.__module__' value:", func.__module__ 7 | 8 | 9 | if __name__ == "__main__": 10 | print "Inside if. This is evaluated only when the module is executed." 11 | 12 | print "Outside if. This is evaluated always." 13 | -------------------------------------------------------------------------------- /basic/my_modules.py: -------------------------------------------------------------------------------- 1 | print "'__name__' value:", __name__ 2 | 3 | def func(): 4 | print "Called func in", __name__ 5 | 6 | print "'func.__module__' value:", func.__module__ 7 | 8 | 9 | if __name__ == "__main__": 10 | print "Inside if. This is evaluated only when the module is executed." 11 | 12 | print "Outside if. This is evaluated always." 13 | -------------------------------------------------------------------------------- /basic/tmp_file.txt: -------------------------------------------------------------------------------- 1 | I'm writing to a file... 2 | I'm writing to a file... 3 | -------------------------------------------------------------------------------- /best-practices/0_What_is_python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Run the following:" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "#Inside a python console\n", 19 | "import this" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "## From official website ( http://www.python.org/ ):\n", 27 | "\n", 28 | "Python is a programming language that lets you work more quickly and integrate\n", 29 | "your systems more effectively. You can learn to use Python and see almost\n", 30 | "immediate gains in productivity and lower maintenance costs.\n", 31 | "\n", 32 | "## Executive summary from official website ( http://www.python.org/doc/essays/blurb.html )\n", 33 | " \n", 34 | " Python is an interpreted, object-oriented, high-level programming language\n", 35 | " with dynamic semantics. Its high-level built in data structures, combined\n", 36 | " with dynamic typing and dynamic binding, make it very attractive for Rapid\n", 37 | " Application Development, as well as for use as a scripting or glue language\n", 38 | " to connect existing components together. Python's simple, easy to learn syntax\n", 39 | " emphasizes readability and therefore reduces the cost of program maintenance.\n", 40 | " Python supports modules and packages, which encourages program modularity and\n", 41 | " code reuse. The Python interpreter and the extensive standard library are\n", 42 | " available in source or binary form without charge for all major platforms,\n", 43 | " and can be freely distributed.\n" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "### TO SUM UP\n", 51 | " - quick development\n", 52 | " - simple, readable, easy to learn syntax\n", 53 | " - general purpose\n", 54 | " - interpreted (not compiled)\n", 55 | " - object-oriented\n", 56 | " - high-level\n", 57 | " - dynamic semantics (aka execution semantics)\n", 58 | " - fully dynamic typing\n", 59 | " - dynamic binding\n", 60 | " - low programs manteinance cost\n", 61 | " - modularity and code reuse\n", 62 | " - no licensing costs\n", 63 | " - extensive standard library, \"batteries included\"\n", 64 | " - imperative and functional programming\n", 65 | " - automatic memory management" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [], 75 | "source": [ 76 | "# execution semantics\n", 77 | "for i in range(10):\n", 78 | " print i**2\n", 79 | "print \"Outside for loop\"" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": { 86 | "collapsed": false 87 | }, 88 | "outputs": [], 89 | "source": [ 90 | "# dynamic binding\n", 91 | "my_str = \"hola\"\n", 92 | "type(my_str)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "metadata": { 99 | "collapsed": false 100 | }, 101 | "outputs": [], 102 | "source": [ 103 | "# dynamic binding\n", 104 | "my_str = 90\n", 105 | "type(my_str)" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": { 112 | "collapsed": false 113 | }, 114 | "outputs": [], 115 | "source": [ 116 | "# fully dynamic typing\n", 117 | "4 +5 +6" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": { 124 | "collapsed": false 125 | }, 126 | "outputs": [], 127 | "source": [ 128 | "# fully dynamic typing\n", 129 | "4 + \"hola\"" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "## HISTORY\n", 137 | " - http://docs.python.org/2/license.html\n", 138 | " - http://en.wikipedia.org/wiki/History_of_Python\n", 139 | " - http://en.wikipedia.org/wiki/Benevolent_Dictator_For_Life\n", 140 | " - http://www.python.org/dev/peps/pep-0001/\n", 141 | " - http://docs.python.org/3.0/whatsnew/3.0.html" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "## TOOLS\n", 151 | "\n", 152 | "**CPython** is the real name of default standard Python implementation\n", 153 | " - The interpreter is deployed together with standard library\n", 154 | " - It can take a file as argument to run. Otherwise it opens and interactive console\n", 155 | " - With '-m' parameter you can execute directly certain modules (debugging, profiling)\n", 156 | " - Implemented in C\n", 157 | " \n", 158 | "There are Python implementations in other languages:\n", 159 | " - Jython: Python 2.5 interpreter written in Java which runs bytecode in the JVM\n", 160 | " - IronPython: Similar approach for .NET Common Language Runtime\n", 161 | " - JS, C++, CIL...\n", 162 | " - Stackless Python: CPython fork with microthreads concurrency\n", 163 | " - PyPy: Python 2.7 interpreter implemented in Python. Really fast, multi-core...!!!\n", 164 | "\n", 165 | "\n", 166 | "**IPython**: create a comprehensive environment for interactive and exploratory computing \n", 167 | "An enhanced interactive Python shell \n", 168 | "An architecture for interactive parallel computing:\n", 169 | " - Other powerful interactive shells (terminal and Qt-based)\n", 170 | " - A browser-based notebook with support for code, text, mathematical expressions\n", 171 | " inline plots and other rich media\n", 172 | " - Support for interactive data visualization and use of GUI toolkits\n", 173 | " - Flexible, embeddable interpreters to load into your own projects\n", 174 | " - Easy to use, high performance tools for parallel computing\n", 175 | " - Recently funded with $1.15M from the Alfred P. Sloan Foundation\n", 176 | "\n", 177 | "\n", 178 | "**virtualenv**: a tool to create isolated Python environments \n", 179 | "It simply changes your PATH environment var to point to a different folder\n", 180 | "\n", 181 | "\n", 182 | "**PyPi**: The Python Package Index is a repository of software for the Python programming language. \n", 183 | "There are currently 89324 packages here. \n", 184 | "The packages are 'eggs'.\n", 185 | "\n", 186 | "**pip**: A tool for installing and managing Python packages\n", 187 | "It installs packages from PyPi, local folders or Git and other repositories\n", 188 | "It can read a list of packages from a file or generate the list of installed packages" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "### Installing pip and virtualenv for a python interpreter\n", 196 | "\n", 197 | "```sh\n", 198 | "wget https://bootstrap.pypa.io/get-pip.py\n", 199 | "python get-pip.py\n", 200 | "pip install virtualenv\n", 201 | "virtualenv env\n", 202 | "source env/bin/activate\n", 203 | "# finish virtualenv running deactivate\n", 204 | "```\n" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "**IDE**?\n", 212 | " - IntelliJ IDEA\n", 213 | " - PyCharm\n", 214 | " - Eclipse + Pydev\n", 215 | " - NetBeans\n", 216 | " - Eric\n", 217 | " - NINJA IDE\n", 218 | " - Aptana Studio 3\n", 219 | " - SPE\n", 220 | " - Python's IDLE (not recommendable at all)\n", 221 | " - ...\n", 222 | " - Emacs\n", 223 | " - Vi\n", 224 | "http://wiki.python.org/moin/IntegratedDevelopmentEnvironments \n", 225 | "Lots of good IDEs, it's up to you!" 226 | ] 227 | } 228 | ], 229 | "metadata": { 230 | "kernelspec": { 231 | "display_name": "Python 2", 232 | "language": "python", 233 | "name": "python2" 234 | }, 235 | "language_info": { 236 | "codemirror_mode": { 237 | "name": "ipython", 238 | "version": 2 239 | }, 240 | "file_extension": ".py", 241 | "mimetype": "text/x-python", 242 | "name": "python", 243 | "nbconvert_exporter": "python", 244 | "pygments_lexer": "ipython2", 245 | "version": "2.7.12" 246 | } 247 | }, 248 | "nbformat": 4, 249 | "nbformat_minor": 1 250 | } 251 | -------------------------------------------------------------------------------- /best-practices/1_Duck_Typing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "spam = [0, 1, 2, 3, 4]\n", 12 | "print spam[3]\n", 13 | "\n", 14 | "eggs = {\"zero\": 0, \"one\": 1, \"two\": 2}\n", 15 | "print eggs[\"one\"]" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": { 22 | "collapsed": true 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "print spam[\"one\"] # Uuuups" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "def my_sum(x, y):\n", 38 | " return x + y\n", 39 | "\n", 40 | "print my_sum(2, 3)\n", 41 | "\n", 42 | "print my_sum(\"spam\", \"eggs\")" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": { 49 | "collapsed": true 50 | }, 51 | "outputs": [], 52 | "source": [ 53 | "print my_sum(\"spam\", 3) # Uuuups" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": { 60 | "collapsed": true 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "# A less abstract example\n", 65 | "\n", 66 | "class Duck(object):\n", 67 | " def __init__(self, name):\n", 68 | " self.name = name\n", 69 | "\n", 70 | " def quack(self):\n", 71 | " print self.name, \"quack\"\n", 72 | "\n", 73 | " def swim(self):\n", 74 | " print self.name, \"swim\"\n", 75 | "\n", 76 | "\n", 77 | "def let_duck_swim_and_quack(d):\n", 78 | " if type(d) == Duck:\n", 79 | " d.swim()\n", 80 | " d.quack()\n", 81 | " else:\n", 82 | " raise TypeError\n", 83 | "\n", 84 | "donald = Duck(\"Donald Duck\")\n", 85 | "\n", 86 | "let_duck_swim_and_quack(donald)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": { 93 | "collapsed": true 94 | }, 95 | "outputs": [], 96 | "source": [ 97 | "class Mallard(Duck):\n", 98 | " pass\n", 99 | "\n", 100 | "advice_mallard = Mallard(\"Advice Mallard\")\n", 101 | "\n", 102 | "let_duck_swim_and_quack(advice_mallard) # But... it is a duck!" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": { 109 | "collapsed": true 110 | }, 111 | "outputs": [], 112 | "source": [ 113 | "# Let's try again\n", 114 | "\n", 115 | "def let_duck_swim_and_quack(d):\n", 116 | " d.swim()\n", 117 | " d.quack()\n", 118 | "\n", 119 | "let_duck_swim_and_quack(advice_mallard)\n", 120 | "\n", 121 | "let_duck_swim_and_quack(donald)\n" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": { 128 | "collapsed": true 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "# And there is still more\n", 133 | "\n", 134 | "class Fish():\n", 135 | " def __init__(self, name):\n", 136 | " self.name = name\n", 137 | "\n", 138 | " def swim(self):\n", 139 | " print self.name, \"swim\"\n", 140 | "\n", 141 | "nemo = Fish(\"Nemo\")\n", 142 | "\n", 143 | "let_duck_swim_and_quack(nemo) # Sorry Nemo, you are not a duck" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "This is duck typing, and it is everywhere although you did not notice it\n", 151 | "\n", 152 | "Duck typing:\n", 153 | "\n", 154 | " When I see a bird that walks like a duck and swims like a duck and quacks\n", 155 | " like a duck, I call that bird a duck.\n", 156 | "\n", 157 | "In other words\n", 158 | "\n", 159 | "\n", 160 | "Duck typing:\n", 161 | "\n", 162 | " - You have to worry and care about the methods and attributes of an used object,\n", 163 | " rather than about its exact type\n", 164 | " - You make your code more extendable, portable, reusable, mantenible...\n", 165 | " - It requires testing, ofc\n", 166 | " - Typical approach: treat your system as black box and only check inputs and outputs\n", 167 | "\n", 168 | "\n", 169 | "Here we also applied another Pythonic principle\n", 170 | "EAFP:\n", 171 | " *It is Easier to Ask for Forgiveness than Permission*\n" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": { 178 | "collapsed": true 179 | }, 180 | "outputs": [], 181 | "source": [ 182 | "# Compare\n", 183 | "\n", 184 | "def let_duck_swim_and_quack(d):\n", 185 | " if hasattr(d, \"swim\") and hasattr(d, \"quack\"):\n", 186 | " d.swim()\n", 187 | " d.quack()\n", 188 | " else:\n", 189 | " print \"It does not look like a duck\"\n", 190 | " raise AttributeError\n", 191 | "\n", 192 | "def let_duck_swim_and_quack(d):\n", 193 | " try:\n", 194 | " d.swim()\n", 195 | " d.quack()\n", 196 | " except AttributeError:\n", 197 | " print \"It does not look like a duck\"\n", 198 | " raise\n" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "EAFP:\n", 206 | " - In positive cases try / except is faster than if / else\n", 207 | " - More understandable\n", 208 | " - You don't have to know all cases to check\n" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": { 215 | "collapsed": true 216 | }, 217 | "outputs": [], 218 | "source": [ 219 | "# However... there is always THAT case\n", 220 | "\n", 221 | "def get_three_keys_value(d):\n", 222 | " it = iter(d)\n", 223 | " try:\n", 224 | " key1 = it.next()\n", 225 | " key2 = it.next()\n", 226 | " key3 = it.next()\n", 227 | " return d[key1], d[key2], d[key3]\n", 228 | " except (StopIteration, KeyError, IndexError):\n", 229 | " return None\n", 230 | "\n", 231 | "eggs = {0: \"zero\", 1: \"one\", 2: \"two\", 3: \"three\", 4: \"four\"}\n", 232 | "print get_three_keys_value(eggs)\n", 233 | "\n", 234 | "spam = [0, 1, 2, 3, 4]\n", 235 | "print get_three_keys_value(spam) # Uuuups" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [ 244 | "def multi_upper(texts):\n", 245 | " return map(str.upper, texts)\n", 246 | "\n", 247 | "spam = ['zero', 'one', 'two', 'three', 'four']\n", 248 | "print multi_upper(spam)\n", 249 | "\n", 250 | "eggs = \"eggs\"\n", 251 | "print multi_upper(eggs) # Uuuups" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "metadata": { 258 | "collapsed": true 259 | }, 260 | "outputs": [], 261 | "source": [ 262 | "# Sadly, in some cases you may need type checking\n", 263 | "\n", 264 | "\n", 265 | "def multi_upper(texts):\n", 266 | " if isinstance(texts, basestring): # basestring is the superclass of str and unicode\n", 267 | " texts = [texts]\n", 268 | " return map(str.upper, texts)\n", 269 | "\n", 270 | "print multi_upper(spam)\n", 271 | "print multi_upper(eggs)\n", 272 | "\n" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": null, 278 | "metadata": { 279 | "collapsed": true 280 | }, 281 | "outputs": [], 282 | "source": [ 283 | "def get_three_keys_value(d):\n", 284 | " if isinstance(d, (tuple, list)): # You can provide several types inside a tuple\n", 285 | " return None\n", 286 | " it = iter(d)\n", 287 | " try:\n", 288 | " key1 = it.next()\n", 289 | " key2 = it.next()\n", 290 | " key3 = it.next()\n", 291 | " return d[key1], d[key2], d[key3]\n", 292 | " except (StopIteration, KeyError, IndexError):\n", 293 | " return None\n", 294 | "\n", 295 | "eggs = {0: \"zero\", 1: \"one\", 2: \"two\", 3: \"three\", 4: \"four\"}\n", 296 | "print get_three_keys_value(eggs)\n", 297 | "\n", 298 | "spam = [0, 1, 2, 3, 4]\n", 299 | "print get_three_keys_value(spam)\n", 300 | "\n", 301 | "\n", 302 | "print isinstance(advice_mallard, Duck) # You can provide also classes instead of types" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "- Use 'isinstance' to efficiently check objects type\n", 310 | "- Provide one or several (in a tuple) types or classes\n", 311 | " - It supports subclassing!" 312 | ] 313 | } 314 | ], 315 | "metadata": { 316 | "kernelspec": { 317 | "display_name": "Python 2", 318 | "language": "python", 319 | "name": "python2" 320 | }, 321 | "language_info": { 322 | "codemirror_mode": { 323 | "name": "ipython", 324 | "version": 2 325 | }, 326 | "file_extension": ".py", 327 | "mimetype": "text/x-python", 328 | "name": "python", 329 | "nbconvert_exporter": "python", 330 | "pygments_lexer": "ipython2", 331 | "version": "2.7.12" 332 | } 333 | }, 334 | "nbformat": 4, 335 | "nbformat_minor": 1 336 | } 337 | -------------------------------------------------------------------------------- /best-practices/2_wow.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "Install pip\n", 10 | "\n", 11 | "- pip is used to list, search, install, upgrade and uninstall packages from\n", 12 | " PyPi, GitHub, or other VCSs\n", 13 | "- easy_install is a simple installer from PyPi. It comes with setuptools \n", 14 | "\n", 15 | "- setuptools has recently merged distribute, which was a fork of setuptools. \n", 16 | " this versions is Py3k compatible.\n", 17 | "\n", 18 | "Both are a library to build, package and install Python projects.\n" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "Install virtualenv\n", 26 | "- virtualenv is used to isolate environments\n", 27 | " different projects, POCs and spikes, different Python versions\n", 28 | "- When Python interpreter starts it loads os and site-packages using relative paths\n", 29 | " regarding the binary location\n", 30 | "- For virtualenv it is enough to copy all this relative paths together with the binary\n", 31 | " - It comes with pip installed\n", 32 | " - Install it with pip\n", 33 | "\n", 34 | "```sh\n", 35 | "sudo pip install virtualenv\n", 36 | "```" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "Use virtualenv\n", 44 | "- Create a new environment:\n", 45 | "```sh\n", 46 | "virtualenv venvs/pycourse\n", 47 | "```\n", 48 | "\n", 49 | "Create a new environment with different Python version:\n", 50 | "\n", 51 | "```sh\n", 52 | "which python3.3\n", 53 | "/usr/local/bin/python3.3\n", 54 | "virtualenv venvs/pycourse3.3 -p /usr/local/bin/python3.3\n", 55 | "```\n", 56 | "\n", 57 | "Enable the virtualenv\n", 58 | "```sh\n", 59 | "echo $PATH\n", 60 | "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:...\n", 61 | "source venvs/pycourse/bin/activate\n", 62 | "echo $PATH\n", 63 | "/Users/pev/venvs/pycourse/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:...\n", 64 | "```\n", 65 | "\n", 66 | "Disable the virtualenv:\n", 67 | "```sh\n", 68 | "deactivate\n", 69 | "```" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "Use pip (inside virtualenvs)\n", 77 | "- List installed packages:\n", 78 | "\n", 79 | "```sh\n", 80 | "pip freeze\n", 81 | " wsgiref==0.1.2\n", 82 | "```\n", 83 | "\n", 84 | "- Install new packages:\n", 85 | "\n", 86 | "```sh\n", 87 | "pip search pep8\n", 88 | " wsgiref==0.1.2\n", 89 | " autopep8 - A tool that automatically formats Python code...\n", 90 | " pep8 - Python style guide checker\n", 91 | "\n", 92 | "pip install pep8\n", 93 | "Downloading/unpacking pep8\n", 94 | " Downloading pep8-1.4.5.tar.gz (63kB): 63kB downloaded\n", 95 | " Running setup.py egg_info for package pep8\n", 96 | " ...\n", 97 | " Successfully installed pep8\n", 98 | " Cleaning up...\n", 99 | "\n", 100 | "pip freeze\n", 101 | " wsgiref==0.1.2\n", 102 | " pep8==1.4.4\n", 103 | "```\n", 104 | "\n", 105 | "- Install your project's requirements:\n", 106 | "```sh\n", 107 | "pip install -r requirements.txt\n", 108 | "```\n", 109 | "\n", 110 | "- Create your project's requirements file:\n", 111 | "```sh\n", 112 | "pip freeze > requirements.txt\n", 113 | "```" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "Use PEP8\n", 121 | "- PEP8 is the style guide for Python code\n", 122 | "- Most IDEs integrate it\n", 123 | "- Normally we accept 120 charactes per line (instead of 80)\n", 124 | "\n", 125 | "- You can run and check code style from console\n", 126 | "```sh\n", 127 | "pep8 --max-line-length=119 .\n", 128 | " ./file1.py:7:1: E302 expected 2 blank lines, found 1\n", 129 | " ./file1.py:9:5: E301 expected 1 blank line, found 0\n", 130 | " ./file2.py:11:5: E301 expected 1 blank line, found 0\n", 131 | " ./file2.py:13:1: W293 blank line contains whitespace\n", 132 | "```" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": null, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "def func(arg1, arg2=None):\n", 142 | " '''This is the docstring of the function, explaining what it does\n", 143 | " its arguments, its exceptions raised and its return value\n", 144 | " It may be multiline or just a single line\n", 145 | " '''\n", 146 | " pass\n", 147 | "\n", 148 | "\n", 149 | "class MyClass(object):\n", 150 | " '''This is the docstring of the class, explaining what it does\n", 151 | " '''\n", 152 | " def method(self, arg1, arg2=None):\n", 153 | " '''This is the docstring of the method, explaining what it does\n", 154 | " '''\n", 155 | " pass\n" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "print func.__doc__\n", 165 | "print\n", 166 | "print MyClass.__doc__\n", 167 | "print\n", 168 | "print MyClass.method.__doc__ # Even you can access docstring" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "Use docstrings\n", 176 | "- PEP8 defines this code documentation 'protocol'\n", 177 | "- It consists on a string block below modules, classes and functions declaration\n", 178 | "- It is possible to insert reStructuredText or EpiDoc markup\n", 179 | "- There are tools to extract, display, format and convert it:\n", 180 | "\t- Sphinx, pydoc (released with the interpreter)\n", 181 | "\n", 182 | "http://www.python.org/dev/peps/pep-0008/" 183 | ] 184 | } 185 | ], 186 | "metadata": { 187 | "kernelspec": { 188 | "display_name": "Python 2", 189 | "language": "python", 190 | "name": "python2" 191 | }, 192 | "language_info": { 193 | "codemirror_mode": { 194 | "name": "ipython", 195 | "version": 2 196 | }, 197 | "file_extension": ".py", 198 | "mimetype": "text/x-python", 199 | "name": "python", 200 | "nbconvert_exporter": "python", 201 | "pygments_lexer": "ipython2", 202 | "version": "2.7.6" 203 | } 204 | }, 205 | "nbformat": 4, 206 | "nbformat_minor": 0 207 | } 208 | -------------------------------------------------------------------------------- /best-practices/3_common_java_errors.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "- Getters and setters are not needed\n", 10 | "- There is always direct attribute access\n", 11 | "- Use properties or decorators if you really need to intercept attribute access" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "- Abuse of @staticmethod and @classmethod\n", 19 | "\t- @staticmethod has no sense. Only justification is organization of code\n", 20 | " You can define functions and attributes directly in modules\n", 21 | " \t- @classmethod only used in constructors. Try to avoid class methods and attributes" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "- Abuse of @staticmethod and @classmethod\n", 29 | "\t- @staticmethod has no sense. Only justification is organization of code\n", 30 | " You can define functions and attributes directly in modules\n", 31 | " \t- @classmethod only used in constructors. Try to avoid class methods and attributes" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "- Define only one class per module\n", 39 | " - You can define several \"helper\" or internal classes in the same module\n", 40 | " - And even functions and attributes" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "- Fear to multiple inheritance\n", 48 | "\t- Mixins and multiple inheritance are widely used in Python\n", 49 | "\t- You must understand what you are implementing, that's your work\n", 50 | "\t- \"we're all grown-ups here\"" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "- Abuse of type checking\n", 58 | "\t- Duck typing and EAFP (It is Easier to Ask for Forgiveness than Permission)\n", 59 | "\t- Get used to the \"uncertainity\" of the exact type of your arguments" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "a = []\n", 69 | "for i in range(10):\n", 70 | " a.append(i*i)\n", 71 | "\n", 72 | "a = [i*i for i in range(10)]" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "with open(\"file.txt\", \"w\") as f:\n", 82 | " f.write(\"nee\\n\")\n", 83 | " f.write(\"arms\\n\")\n", 84 | "\n", 85 | "[line.replace(\"spam\",\"eggs\") for line in open(\"file.txt\") if line.startswith(\"nee\")]" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "# Functions are first class objects\n", 95 | "def int_validator(param):\n", 96 | " if isinstance(param, int):\n", 97 | " print \"Int value\"\n", 98 | " else:\n", 99 | " print \"Not int value\"\n", 100 | "\n", 101 | "def str_validator(param):\n", 102 | " if isinstance(param, str):\n", 103 | " print \"Str value\"\n", 104 | " else:\n", 105 | " print \"Not str value\"\n", 106 | "\n", 107 | "def validate(param, validators):\n", 108 | " for validator in validators:\n", 109 | " validator(param)\n", 110 | " \n", 111 | "validate(34, [int_validator, str_validator])\n", 112 | "validate(\"34\", [int_validator, str_validator])" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "# properties\n", 122 | "class Results(object):\n", 123 | " _long_time_property = None\n", 124 | " @property\n", 125 | " def long_time_property(self):\n", 126 | " if not _long_time_property:\n", 127 | " _long_time_property = long_time_operation()\n", 128 | " return _long_time_property\n", 129 | " \n", 130 | " def long_time_operation(self):\n", 131 | " time.sleep(2)\n", 132 | " return 4" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": null, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "r = Results()\n", 142 | "r.long_time_property\n", 143 | "print \"First calculation\"" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "r.long_time_property\n", 153 | "print \"second calculation\"" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": {}, 160 | "outputs": [], 161 | "source": [ 162 | "#Default and keyword arguments.\n", 163 | "def my_func(arg1, arg2=2, arg3=3, arg4=4):\n", 164 | " return arg1 ** arg2 + arg3 ** arg4\n", 165 | "\n", 166 | "print my_func(3, arg3=2) # Use keyword arguments to call skip some of the arguments with default value\n", 167 | "\n", 168 | "# Better than multiple constructors ?" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [ 177 | "def my_func(arg1=1, arg2=2, **kwargs): # This arbitrary 'args' list is a (kind-off) tuple of positional arguments\n", 178 | " print kwargs\n", 179 | " return arg1 + arg2\n", 180 | "\n", 181 | "my_func(2, 3)\n", 182 | "\n", 183 | "my_func(2, 3, param3=5, param4=7)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "spam = {\"param3\": 5, \"param4\": 7}\n", 193 | "my_func(2, 3, **spam) # It is possible to unpack a tuple or list as an arbitrary list of arguments" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "metadata": {}, 200 | "outputs": [], 201 | "source": [ 202 | "def nee():\n", 203 | " return 1\n", 204 | "spam = nee()\n", 205 | "print spam" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": {}, 212 | "outputs": [], 213 | "source": [ 214 | "# refactor\n", 215 | "def nee():\n", 216 | " return 1, \"uno\"\n", 217 | "spam, eggs = nee()\n", 218 | "print spam\n", 219 | "print eggs" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": null, 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "# Built-in syntax for lists and dictionaries.\n", 229 | "# See basic module" 230 | ] 231 | } 232 | ], 233 | "metadata": { 234 | "kernelspec": { 235 | "display_name": "Python 2", 236 | "language": "python", 237 | "name": "python2" 238 | }, 239 | "language_info": { 240 | "codemirror_mode": { 241 | "name": "ipython", 242 | "version": 2 243 | }, 244 | "file_extension": ".py", 245 | "mimetype": "text/x-python", 246 | "name": "python", 247 | "nbconvert_exporter": "python", 248 | "pygments_lexer": "ipython2", 249 | "version": "2.7.6" 250 | } 251 | }, 252 | "nbformat": 4, 253 | "nbformat_minor": 0 254 | } 255 | -------------------------------------------------------------------------------- /best-practices/4_django_django_rest_framework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "Django was invented to meet fast-moving newsroom deadlines, while satisfying the tough requirements of experienced Web developers.\n", 10 | "\n", 11 | "- Fast development\n", 12 | "- Fully loaded\n", 13 | "- Reassuringly secure\n", 14 | "- Scalable\n", 15 | "\n", 16 | "https://www.djangoproject.com/start/" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "Django REST framework is a powerful and flexible toolkit for building Web APIs.\n", 24 | "\n", 25 | "- Web browsable API is a huge usability win for your developers.\n", 26 | "- Authentication policies including packages for OAuth1a and OAuth2.\n", 27 | "- Serialization that supports both ORM and non-ORM data sources.\n", 28 | "- Customizable.\n", 29 | "- Extensive documentation, and great community support.\n", 30 | "\n", 31 | "http://www.django-rest-framework.org/" 32 | ] 33 | }, 34 | { 35 | "cell_type": "heading", 36 | "metadata": {}, 37 | "level": 1, 38 | "source": [ 39 | "http://wsgi.readthedocs.io/en/latest/frameworks.html" 40 | ] 41 | } 42 | ], 43 | "metadata": { 44 | "kernelspec": { 45 | "display_name": "Python 2", 46 | "language": "python", 47 | "name": "python2" 48 | }, 49 | "language_info": { 50 | "codemirror_mode": { 51 | "name": "ipython", 52 | "version": 2 53 | }, 54 | "file_extension": ".py", 55 | "mimetype": "text/x-python", 56 | "name": "python", 57 | "nbconvert_exporter": "python", 58 | "pygments_lexer": "ipython2", 59 | "version": "2.7.6" 60 | } 61 | }, 62 | "nbformat": 4, 63 | "nbformat_minor": 0 64 | } 65 | -------------------------------------------------------------------------------- /best-practices/5_functional_programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "Let's start with filter" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "spam_eggs = \"sPaM EggS\"\n", 19 | "print str.isupper\n", 20 | "print filter(str.isupper, spam_eggs) # filter returns elements evaluated as True by first argument" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "def my_filtering_func(input):\n", 30 | " return input % 2 == 0\n", 31 | "\n", 32 | "spam = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 33 | "print filter(my_filtering_func, spam) # You can provide your own functions" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "# It's time to use lambda\n", 43 | "\n", 44 | "\n", 45 | "spam = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 46 | "print filter(lambda x: x % 2 == 0, spam) # lambda keyword is used to create one-liner simple anonymous functions" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "def get_power(y):\n", 56 | " return lambda x: x ** y\n", 57 | "\n", 58 | "cube = get_power(3)\n", 59 | "print cube(2)\n", 60 | "print cube(3)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "spam = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 70 | "print map(lambda x: x ** 2, spam) # map applies the first argument function to all elements of iterable" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "spam_eggs = \"sPaM EggS\"\n", 80 | "print map(str.upper, spam_eggs) # map always returns a list" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "eggs = (1, 2, 3, None, 5, 6, None, 8)\n", 90 | "print map(None, eggs)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [ 99 | "print map(lambda x, y: x * y, spam_eggs, spam) # map can accept several iterables" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "print map(lambda x, y: (y, x), \"spam\", spam) # map adds None in case one iterable has less elements" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "\n", 118 | "# Let's check reduce\n", 119 | "\n", 120 | "spam = [1, 2, 3]\n", 121 | "print reduce(lambda x, y: x + y, spam) # reduce applies consecutively the function to each element of iterable" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "spam = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 131 | "print reduce(lambda x, y: x + y, spam) # reduce accumulates the result of each iteration" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "spam = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 141 | "print reduce(lambda x, y: x + y, spam, 1000) # Optionally reduce accepts an initializer as initial accumulated value" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "# Let's continue with any and all\n", 151 | "\n", 152 | "spam = [1, 2, 3, 4, 5]\n", 153 | "print any(spam)\n", 154 | "\n", 155 | "spam = [0, 1, 2, 3, 4, 5]\n", 156 | "print any(spam)\n", 157 | "\n", 158 | "spam = [None, 1, 2, 3, 4, 5]\n", 159 | "print any(spam)\n", 160 | "\n", 161 | "spam = []\n", 162 | "print any(spam)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": null, 168 | "metadata": {}, 169 | "outputs": [], 170 | "source": [ 171 | "spam = [1, 2, 3, 4, 5]\n", 172 | "print all(spam)\n", 173 | "\n", 174 | "spam = [0, 1, 2, 3, 4, 5]\n", 175 | "print all(spam)\n", 176 | "\n", 177 | "spam = [None, 1, 2, 3, 4, 5]\n", 178 | "print all(spam)\n", 179 | "\n", 180 | "spam = []\n", 181 | "print all(spam)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": null, 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [ 190 | "# enumerate\n", 191 | "\n", 192 | "spam = \"spam\"\n", 193 | "print list(enumerate(spam)) # Return an iterator of pairs (number, element) for elements in the iterable" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "metadata": {}, 200 | "outputs": [], 201 | "source": [ 202 | "num = 0\n", 203 | "spam = \"spam\"\n", 204 | "for num, item in enumerate(spam, 1): # Useful in for loops\n", 205 | " print item, \"at index\", num - 1\n", 206 | "\n", 207 | "print \"Number of items processed:\", num" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": null, 213 | "metadata": {}, 214 | "outputs": [], 215 | "source": [ 216 | "# zip\n", 217 | "\n", 218 | "spam = ['monday', 'tuesday',\n", 219 | " 'wednesday', 'thursday',\n", 220 | " 'friday']\n", 221 | "eggs = [1, 2, 3, 4, 5]\n", 222 | "fooo = (\"MO\", \"TU\", \"WD\",\n", 223 | " \"TH\", \"FR\")\n", 224 | "print zip(spam, eggs, fooo) # Return a list of tuples taking one element of each iterable at the same time" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [ 233 | "spam = ['monday', 'tuesday',\n", 234 | " 'wednesday', 'thursday',\n", 235 | " 'friday']\n", 236 | "eggs = [0, 1, 2, 3, 4,\n", 237 | " 5, 6, 7, 8, 9]\n", 238 | "fooo = (\"MO\", \"TU\", \"WD\")\n", 239 | "print zip(spam, eggs, fooo) # The resulting list is as long as the shortest input iterable" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": null, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [ 248 | "# sum\n", 249 | "\n", 250 | "spam = [1, 2, 3, 4, 5]\n", 251 | "print sum(spam) # No explanation needed\n", 252 | "\n", 253 | "print sum(spam, 100) # Optional second argument with initial sum value" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [ 262 | "# range\n", 263 | "\n", 264 | "print range(10) # Returns a list of numbers lower than provided value\n", 265 | "\n", 266 | "print range(1, 10) # By default first number is 0, but you may specify it\n", 267 | "\n", 268 | "print range(0, 10, 2) # You may also specify the step to increase\n", 269 | "\n", 270 | "print range(0, -10, -2) # You may also specify negative values" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "print xrange(0, 10, 2)\n", 280 | "print list(xrange(0, 10, 2)) # It is a generator, no resources waste, no list created" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "metadata": {}, 287 | "outputs": [], 288 | "source": [ 289 | "# sorted\n", 290 | "\n", 291 | "spam = [2, 3, 1, 5, 4]\n", 292 | "print sorted(spam) # Returns a new list with iterable content sorted" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [ 301 | "spam = [2, 3, 1, 5, 4]\n", 302 | "print sorted(spam, reverse=True) # Set reverse=True to get decremental sorting" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": null, 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "spam = ['monday', 'tuesday',\n", 312 | " 'wednesday', 'thursday',\n", 313 | " 'friday']\n", 314 | "print sorted(spam, key=lambda x: len(x)) # You may provide a function to return a weight or comparison key" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": null, 320 | "metadata": {}, 321 | "outputs": [], 322 | "source": [ 323 | "spam = ['monday', 'tuesday',\n", 324 | " 'wednesday', 'thursday',\n", 325 | " 'friday']\n", 326 | "print sorted(spam, cmp=lambda x, y: len(x) - len(y)) # Alternatively you may provide a function to compare pairs of elements" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": null, 332 | "metadata": {}, 333 | "outputs": [], 334 | "source": [ 335 | "# reversed\n", 336 | "\n", 337 | "spam = [2, 3, 1, 5, 4]\n", 338 | "print reversed(spam) # Returns an iterator. Only works with sequences, collections which have index access\n", 339 | "print list(reversed(spam))" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": null, 345 | "metadata": {}, 346 | "outputs": [], 347 | "source": [ 348 | "sentences = ['Mary read a story to Sam and Isla.',\n", 349 | " 'Isla cuddled Sam.',\n", 350 | " 'Sam chortled.']\n", 351 | "\n", 352 | "sam_count = 0\n", 353 | "for sentence in sentences:\n", 354 | " sam_count += sentence.count('Sam')\n", 355 | "\n", 356 | "print sam_count" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "metadata": {}, 363 | "outputs": [], 364 | "source": [ 365 | "sentences = ['Mary read a story to Sam and Isla.',\n", 366 | " 'Isla cuddled Sam.',\n", 367 | " 'Sam chortled.']\n", 368 | "\n", 369 | "sam_count = reduce(lambda a, x: a + x.count('Sam'),\n", 370 | " sentences,\n", 371 | " 0)\n", 372 | "\n" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "metadata": {}, 379 | "outputs": [], 380 | "source": [ 381 | "# Imperative style: actions that change state from initial state to result\n", 382 | "expr, res = \"28+32+++32++39\", 0\n", 383 | "for t in expr.split(\"+\"):\n", 384 | " if t != \"\":\n", 385 | " res += int(t)\n", 386 | "\n", 387 | "print res" 388 | ] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "execution_count": null, 393 | "metadata": {}, 394 | "outputs": [], 395 | "source": [ 396 | "# Functional style = apply transformation (and compositions)\n", 397 | "from operator import add\n", 398 | "expr = \"28+32+++32++39\"\n", 399 | "print reduce(add, map(int, filter(bool, expr.split(\"+\"))))" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "- Readability VS. conciseness\n", 407 | "- Code reusage (\"pluggability\")\n" 408 | ] 409 | } 410 | ], 411 | "metadata": { 412 | "kernelspec": { 413 | "display_name": "Python 2", 414 | "language": "python", 415 | "name": "python2" 416 | }, 417 | "language_info": { 418 | "codemirror_mode": { 419 | "name": "ipython", 420 | "version": 2 421 | }, 422 | "file_extension": ".py", 423 | "mimetype": "text/x-python", 424 | "name": "python", 425 | "nbconvert_exporter": "python", 426 | "pygments_lexer": "ipython2", 427 | "version": "2.7.6" 428 | } 429 | }, 430 | "nbformat": 4, 431 | "nbformat_minor": 0 432 | } 433 | -------------------------------------------------------------------------------- /best-practices/file.txt: -------------------------------------------------------------------------------- 1 | nee spam 2 | arms spam 3 | -------------------------------------------------------------------------------- /docker-entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | service mysql start 3 | service mongodb start 4 | /usr/local/bin/jupyter-notebook --ip 0.0.0.0 --allow-root --notebook-dir /curso-python 5 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | jupyter==1.0.0 2 | chardet==2.3.0 3 | psutil==4.3.1 4 | MySQL-python==1.2.5 5 | pymongo==2.7.2 6 | requests==2.11.1 7 | nose==1.3.7 8 | mock==2.0.0 9 | ipdb==0.10.1 10 | -------------------------------------------------------------------------------- /sysadmin/03_parsing_benchmarks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Parsing\n", 8 | "\n", 9 | "Goals:\n", 10 | "\n", 11 | " - Plan a parsing strategy\n", 12 | " - Use basic regular expressions: match, search, sub\n", 13 | " - Benchmarking a parser\n", 14 | " - Running nosetests\n", 15 | " - Write a simple parser\n", 16 | " \n", 17 | "# Modules:\n", 18 | "\n", 19 | "\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "import re\n", 31 | "import nose\n", 32 | "# %timeit" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "# Parsing is hard...\n", 40 | "\n", 41 | "

\n", 42 | "\"System Administrators spent $24.3\\%$ of\n", 43 | " their work-life parsing files.\"\n", 44 | "

\n", 45 | " \n", 46 | " Independent analysis by The GASP* Society ;)
\n", 47 | "

\n", 48 | "

\n", 49 | " *(Grep Awk Sed Perl)\n", 50 | "

" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "# strategy!\n", 58 | "\n", 59 | "\n", 60 | "\n", 69 | "
\n", 61 | "
  1. Collect parsing samples\n", 62 | "
  2. Play in ipython and collect %history\n", 63 | "
  3. Write tests, then the parser\n", 64 | "
  4. Eventually benchmark\n", 65 | "
\n", 66 | "
\n", 67 | "\n", 68 | "
\n" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "# Parsing postfix logs" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": { 83 | "collapsed": false 84 | }, 85 | "outputs": [], 86 | "source": [ 87 | "from __future__ import print_function\n", 88 | "# Before writing the parser, collect samples of\n", 89 | "# the interesting lines. For now just\n", 90 | "mail_sent = 'May 31 08:00:00 test-fe1 postfix/smtp[16669]: 7CD8E730020: to=, relay=examplemx2.doe.it[222.33.44.555]:25, delay=0.8, delays=0.17/0.01/0.43/0.19, dsn=2.0.0, status=sent(250 ok: Message 2108406157 accepted)'\n", 91 | "mail_delivered = 'May 31 08:00:00 test-fe1 postfix/smtp[16669]: 7CD8E730020: removed'\n", 92 | "\n", 93 | "print(\"I'm goint to parse the following line\", mail_sent, sep=\"\\n\\n\")" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": { 100 | "collapsed": true 101 | }, 102 | "outputs": [], 103 | "source": [ 104 | "def test_sent():\n", 105 | " hour, host, to = parse_line(mail_sent)\n", 106 | " assert hour == '08:00:00'\n", 107 | " assert to == 'jon@doe.it'" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": { 114 | "collapsed": false 115 | }, 116 | "outputs": [], 117 | "source": [ 118 | "# Play with mail_sent\n", 119 | "mail_sent.split()" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "# You can number fields with enumerate. \n", 131 | "# Remember that ipython puts the last returned value in `_`\n", 132 | "# in our case: _ = mail_sent.split()\n", 133 | "# which is useful in interactive mode!\n", 134 | "fields, counting = _, enumerate(_)\n", 135 | "print(*counting, sep=\"\\n\")\n", 136 | "#counting = enumerate(mail_sent.split())\n", 137 | "#for it in counting:\n", 138 | "# print(it)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [], 148 | "source": [ 149 | "# Now we can pick fields singularly...\n", 150 | "hour, host, dest = fields[2], fields[3], fields[6]\n", 151 | "print(\"Hour: {}, host: {}, dest: {}\".format(hour, host, dest))" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "## Exercise I\n", 159 | " - complete the parse_line(line) function\n", 160 | " - run the tests until all pass\n", 161 | "\n" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": null, 167 | "metadata": { 168 | "collapsed": false 169 | }, 170 | "outputs": [], 171 | "source": [ 172 | "test_str_1 = 'Nov 31 08:00:00 test-fe1 postfix/smtp[16669]: 7CD8E730020: to=, relay=examplemx2.doe.it[222.33.44.555]:25, delay=0.8, delays=0.17/0.01/0.43/0.19, dsn=2.0.0, status=sent(250 ok: Message 2108406157 accepted)'\n", 173 | "test_str_2 = 'Nov 31 08:00:00 test-fe1 postfix/smtp[16669]: 7CD8E730020: removed'\n", 174 | "\n", 175 | "\n", 176 | "def test_sent():\n", 177 | " hour, host, destination = parse_line(test_str_1)\n", 178 | " assert hour == '08:00:00'\n", 179 | " assert host == 'test-fe1'\n", 180 | " assert destination == 'to=,'\n", 181 | "\n", 182 | "\n", 183 | "def test_delivered():\n", 184 | " hour, host, destination = parse_line(test_str_2)\n", 185 | " print(destination)\n", 186 | " assert hour == '08:00:00'\n", 187 | " assert host == 'test-fe1'\n", 188 | " assert destination is None\n", 189 | "\n", 190 | "\n", 191 | "def parse_line(line):\n", 192 | " \"\"\" Complete the parse line function.\n", 193 | " \"\"\"\n", 194 | " # Hint: \"you can\".split()\n", 195 | " # Hint: \"\"[1:-1] or use re.split\n", 196 | " pass\n", 197 | "test_sent()\n", 198 | "test_delivered()" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "# Python Regexp\n" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": { 212 | "collapsed": true 213 | }, 214 | "outputs": [], 215 | "source": [ 216 | "# Python supports regular expressions via\n", 217 | "import re\n", 218 | "\n", 219 | "# We start showing a grep-reloaded function\n", 220 | "def grep(expr, fpath):\n", 221 | " one = re.compile(expr) # ...has two lookup methods...\n", 222 | " assert ( one.match # which searches from ^ the beginning\n", 223 | " and one.search ) # that searches $\\pyver{anywhere}$\n", 224 | "\n", 225 | " with open(fpath) as fp:\n", 226 | " return [x for x in fp if one.search(x)]" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": null, 232 | "metadata": { 233 | "collapsed": false 234 | }, 235 | "outputs": [], 236 | "source": [ 237 | "# The function seems to work as expected ;)\n", 238 | "assert not grep(r'^localhost', '/etc/hosts')\n", 239 | "\n", 240 | "# And some more tests\n", 241 | "ret = grep('127.0.0.1', '/etc/hosts')\n", 242 | "assert ret, \"ret should not be empty\"\n", 243 | "print(*ret)" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "### Achieve more complex splitting using regular expressions. " 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "metadata": { 257 | "collapsed": false 258 | }, 259 | "outputs": [], 260 | "source": [ 261 | "# Splitting with re.findall\n", 262 | "\n", 263 | "from re import findall # can be misused too;\n", 264 | "\n", 265 | "# eg for adding the \":\" to a\n", 266 | "mac = \"00\"\"24\"\"e8\"\"b4\"\"33\"\"20\"\n", 267 | "\n", 268 | "# ...using this\n", 269 | "re_hex = \"[0-9a-fA-F]{2}\"\n", 270 | "mac_address = ':'.join(findall(re_hex, mac))\n", 271 | "print(\"The mac address is \", mac_address)\n", 272 | "\n", 273 | "# Actually this does a bit of validation, requiring all chars to be in the 0-F range" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "# Benchmarking in iPython - I\n", 281 | "\n", 282 | " - Parsing big files needs benchmarks. timeit module is a good starting point\n", 283 | " \n", 284 | "We are going to measure the execution time of various tasks, using different strategies like regexp, join and split. " 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": null, 290 | "metadata": { 291 | "collapsed": false 292 | }, 293 | "outputs": [], 294 | "source": [ 295 | "# Run the following cell many times. \n", 296 | "# Do you always get the same results?\n", 297 | "import timeit\n", 298 | "test_all_regexps = (\"..\", \"[a-fA-F0-9]{2}\")\n", 299 | "for re_s in test_all_regexps:\n", 300 | " print(timeit.timeit(stmt=\"':'.join(findall(re_s, mac))\",\n", 301 | " setup=\"from re import findall;re_s='{}';mac='{}'\".format(re_s, mac))) " 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": { 308 | "collapsed": false 309 | }, 310 | "outputs": [], 311 | "source": [ 312 | "# We can even compare compiled vs inline regexp\n", 313 | "import re\n", 314 | "from time import sleep\n", 315 | "for re_s in test_all_regexps:\n", 316 | " print(timeit.timeit(stmt=\"':'.join(re_c.findall(mac))\",\n", 317 | " setup=\"from re import findall, compile;re_c=compile('{}');mac='{}'\".format(re_s, mac)))" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": { 324 | "collapsed": false 325 | }, 326 | "outputs": [], 327 | "source": [ 328 | "# ...or simple\n", 329 | "print(timeit.timeit(stmt=\"':'.join([mac[i:i+2] for i in range(0,12,2)])\",\n", 330 | " setup=\"from re import findall;mac='{}'\".format(mac))) " 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": {}, 336 | "source": [ 337 | "# Parsing: Exercise II\n", 338 | "\n", 339 | "Now another test for the delivered messages\n", 340 | " - Improve the parse_line function to make the tests pass\n" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": null, 346 | "metadata": { 347 | "collapsed": false 348 | }, 349 | "outputs": [], 350 | "source": [ 351 | "#\n", 352 | "# Use this cell for Exercise II\n", 353 | "#\n", 354 | "\n", 355 | "test_str_1 = 'Nov 31 08:00:00 test-fe1 postfix/smtp[16669]: 7CD8E730020: to=, relay=examplemx2.doe.it[222.33.44.555]:25, delay=0.8, delays=0.17/0.01/0.43/0.19, dsn=2.0.0, status=sent(250 ok: Message 2108406157 accepted)'\n", 356 | "test_str_2 = 'Nov 31 08:00:00 test-fe1 postfix/smtp[16669]: 7CD8E730020: removed'\n", 357 | "\n", 358 | "\n", 359 | "def test_sent():\n", 360 | " hour, host, destination = parse_line(test_str_1)\n", 361 | " assert hour == '08:00:00'\n", 362 | " assert host == 'test-fe1'\n", 363 | " assert destination == 'jon@doe.it'\n", 364 | "\n", 365 | "\n", 366 | "def test_delivered():\n", 367 | " hour, host, destination = parse_line(test_str_2)\n", 368 | " assert hour == '08:00:00'\n", 369 | " assert host == 'test-fe1'\n", 370 | " assert destination is None\n", 371 | "\n", 372 | "\n", 373 | "def parse_line(line):\n", 374 | " \"\"\" Complete the parse line function.\n", 375 | " \"\"\"\n", 376 | " # Hint: \"you can\".split()\n", 377 | " # Hint: \"\"[1:-1] or use re.split\n", 378 | " pass\n", 379 | "\n", 380 | "test_sent()\n", 381 | "test_delivered()" 382 | ] 383 | } 384 | ], 385 | "metadata": { 386 | "kernelspec": { 387 | "display_name": "Python 2", 388 | "language": "python", 389 | "name": "python2" 390 | }, 391 | "language_info": { 392 | "codemirror_mode": { 393 | "name": "ipython", 394 | "version": 2 395 | }, 396 | "file_extension": ".py", 397 | "mimetype": "text/x-python", 398 | "name": "python", 399 | "nbconvert_exporter": "python", 400 | "pygments_lexer": "ipython2", 401 | "version": "2.7.5" 402 | } 403 | }, 404 | "nbformat": 4, 405 | "nbformat_minor": 0 406 | } 407 | -------------------------------------------------------------------------------- /sysadmin/0_Path_Management_Encoding.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Path Management\n", 8 | "\n", 9 | "\n", 10 | "## Goal\n", 11 | " \n", 12 | " - Normalize paths on different platform\n", 13 | " - Create, copy and remove folders\n", 14 | " - Handle errors\n", 15 | " \n", 16 | "## Modules\n", 17 | " " 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": { 24 | "collapsed": false 25 | }, 26 | "outputs": [], 27 | "source": [ 28 | "import os\n", 29 | "import os.path\n", 30 | "import shutil\n", 31 | "import errno\n", 32 | "import glob\n", 33 | "import sys" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "## See also:\n", 41 | "\n", 42 | " - pathlib on Python 3.4+" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": { 49 | "collapsed": true 50 | }, 51 | "outputs": [], 52 | "source": [ 53 | "# Be python3 ready\n", 54 | "from __future__ import unicode_literals, print_function" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "## Multiplatform Path Management\n", 62 | "\n", 63 | "- The os.path module seems verbose but it's the *best* way to manage paths. It's:\n", 64 | " - safe\n", 65 | " - multiplatform\n", 66 | "\n", 67 | " \n", 68 | "- Here we check the operating system and prepend the right path\n" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": { 75 | "collapsed": true 76 | }, 77 | "outputs": [], 78 | "source": [ 79 | "import os\n", 80 | "import sys\n", 81 | "basedir, hosts = \"/\", \"etc/hosts\"" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [], 91 | "source": [ 92 | "# sys.platform shows the current operating system\n", 93 | "if sys.platform.startswith('win'):\n", 94 | " basedir = 'c:/windows/system32/drivers'\n", 95 | "print(basedir)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": { 102 | "collapsed": false 103 | }, 104 | "outputs": [], 105 | "source": [ 106 | "# Join removes redundant \"/\"\n", 107 | "hosts = os.path.join(basedir, hosts)\n", 108 | "print(hosts)" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": { 115 | "collapsed": false 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "# normpath fixes \"/\" orientation \n", 120 | "# and redundant \"..\"\n", 121 | "hosts = os.path.normpath(hosts)\n", 122 | "print(\"Normalized path is\", hosts)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": { 129 | "collapsed": false 130 | }, 131 | "outputs": [], 132 | "source": [ 133 | "# realpath resolves symlinks (on unix)\n", 134 | "! mkdir -p /tmp/course\n", 135 | "! ln -sf /etc/hosts /tmp/course/hosts\n", 136 | "realfile = os.path.realpath(\"/tmp/course/hosts\") \n", 137 | "print(realfile)" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": { 144 | "collapsed": false 145 | }, 146 | "outputs": [], 147 | "source": [ 148 | "# Exercise: given the following path\n", 149 | "base, path = \"/usr\", \"bin/foo\"\n", 150 | "# Which is the expected output of result?\n", 151 | "result = os.path.join(base, path)\n", 152 | "print(result)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "## Manage trees\n", 160 | "\n", 161 | "Python modules can:\n", 162 | " - manage directory trees\n", 163 | " - and basic errors\n", 164 | "\n" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": { 171 | "collapsed": false 172 | }, 173 | "outputs": [], 174 | "source": [ 175 | "# os and shutil supports basic file operations\n", 176 | "# like recursive copy and tree creation.\n", 177 | "!rm -rf /tmp/course/foo\n", 178 | "from os import makedirs\n", 179 | "makedirs(\"/tmp/course/foo/bar\")" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "metadata": { 186 | "collapsed": true 187 | }, 188 | "outputs": [], 189 | "source": [ 190 | "# while os.path can be used to test file existence\n", 191 | "from os.path import isdir\n", 192 | "assert isdir(\"/tmp/course/foo/bar\")" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": { 199 | "collapsed": false 200 | }, 201 | "outputs": [], 202 | "source": [ 203 | "# Check the directory content with either one of\n", 204 | "!tree /tmp/course || find /tmp/course" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": null, 210 | "metadata": { 211 | "collapsed": false 212 | }, 213 | "outputs": [], 214 | "source": [ 215 | "# We can use exception handlers to check\n", 216 | "# what happened.\n", 217 | "\n", 218 | "try:\n", 219 | " # python2 does not allow to ignore\n", 220 | " # already existing directories\n", 221 | " # and raises an OSError\n", 222 | " makedirs(\"/tmp/course/foo/bar\")\n", 223 | "except OSError as e:\n", 224 | " # Just use the errno module to\n", 225 | " # check the error value\n", 226 | " print(e)\n", 227 | " import errno\n", 228 | " assert e.errno == errno.EEXIST" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": null, 234 | "metadata": { 235 | "collapsed": true 236 | }, 237 | "outputs": [], 238 | "source": [ 239 | "from shutil import copytree, rmtree\n", 240 | "# Now copy recursively two directories\n", 241 | "# and check the result\n", 242 | "copytree(\"/tmp/course/foo\", \"/tmp/course/foo2\")\n", 243 | "assert isdir(\"/tmp/course/foo2/bar\")" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": { 250 | "collapsed": false 251 | }, 252 | "outputs": [], 253 | "source": [ 254 | "#This command should work on both unix and windows \n", 255 | "!ls /tmp/course/foo2/" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": null, 261 | "metadata": { 262 | "collapsed": true 263 | }, 264 | "outputs": [], 265 | "source": [ 266 | "# Now remove it and check the outcome\n", 267 | "rmtree(\"/tmp/course/foo\")\n", 268 | "assert not isdir(\"/tmp/course/foo/bar\")" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": null, 274 | "metadata": { 275 | "collapsed": false 276 | }, 277 | "outputs": [], 278 | "source": [ 279 | "#This command should work on both unix and windows \n", 280 | "!ls /tmp/course/" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "metadata": { 287 | "collapsed": true 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "# Cleanup created files\n", 292 | "rmtree(\"/tmp/course\")" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": {}, 298 | "source": [ 299 | "# Encoding\n", 300 | "\n", 301 | "## Goals\n", 302 | "\n", 303 | " - A string is more than a sequence of bytes\n", 304 | " - A string is a couple (bytes, encoding)\n", 305 | " - Use unicode_literals in python2\n", 306 | " - Manage differently encoded filenames\n", 307 | " - A string is not a sequence of bytes" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": null, 313 | "metadata": { 314 | "collapsed": true 315 | }, 316 | "outputs": [], 317 | "source": [ 318 | "import os\n", 319 | "import os.path\n", 320 | "import glob" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": null, 326 | "metadata": { 327 | "collapsed": true 328 | }, 329 | "outputs": [], 330 | "source": [ 331 | "from os.path import isdir\n", 332 | "basedir = \"/tmp/course\"\n", 333 | "if not isdir(basedir):\n", 334 | " os.makedirs(basedir) " 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": null, 340 | "metadata": { 341 | "collapsed": false 342 | }, 343 | "outputs": [], 344 | "source": [ 345 | "# Py3 doesn't need the 'u' prefix before the string.\n", 346 | "the_string = u\"S\\u00fcd\" # Sued\n", 347 | "print(the_string)\n", 348 | "print type(the_string)" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": null, 354 | "metadata": { 355 | "collapsed": false 356 | }, 357 | "outputs": [], 358 | "source": [ 359 | "# the_string Sued can be encoded in different...\n", 360 | "in_utf8 = the_string.encode('utf-8')\n", 361 | "in_win = the_string.encode('cp1252')\n", 362 | "\n", 363 | "# ...byte-sequences\n", 364 | "assert type(in_utf8) == bytes\n", 365 | "print type(in_utf8)" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": null, 371 | "metadata": { 372 | "collapsed": false 373 | }, 374 | "outputs": [], 375 | "source": [ 376 | "# Now you can see the differences between \n", 377 | "print(repr(in_utf8))\n", 378 | "# and\n", 379 | "print(repr(in_win))" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": null, 385 | "metadata": { 386 | "collapsed": false 387 | }, 388 | "outputs": [], 389 | "source": [ 390 | "# Decoding bytes using the wrong map...\n", 391 | "# ...gives Sad results\n", 392 | "print(in_utf8.decode('cp1252'))\n", 393 | "print (in_utf8.decode('utf-8'))" 394 | ] 395 | }, 396 | { 397 | "cell_type": "markdown", 398 | "metadata": {}, 399 | "source": [ 400 | "# To make Py2 encoding-aware we must\n", 401 | "from __future__ import unicode_literals, print_function" 402 | ] 403 | } 404 | ], 405 | "metadata": { 406 | "kernelspec": { 407 | "display_name": "Python 2", 408 | "language": "python", 409 | "name": "python2" 410 | }, 411 | "language_info": { 412 | "codemirror_mode": { 413 | "name": "ipython", 414 | "version": 2 415 | }, 416 | "file_extension": ".py", 417 | "mimetype": "text/x-python", 418 | "name": "python", 419 | "nbconvert_exporter": "python", 420 | "pygments_lexer": "ipython2", 421 | "version": "2.7.12" 422 | } 423 | }, 424 | "nbformat": 4, 425 | "nbformat_minor": 0 426 | } 427 | -------------------------------------------------------------------------------- /sysadmin/2_logging.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Creating and processing logs\n", 8 | "\n", 9 | "## Goals\n", 10 | " \n", 11 | " - importance of logging\n", 12 | " - configuring logger module\n", 13 | " - open log files\n", 14 | " \n", 15 | "## modules" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": { 22 | "collapsed": false 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "from __future__ import print_function\n", 27 | "\n", 28 | "import logging\n", 29 | "import logging.config\n", 30 | "import logging.handlers\n", 31 | "import mimetypes\n", 32 | "import gzip, bz2\n", 33 | "import yaml\n", 34 | "\n" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "## Importance of Logging\n", 42 | "\n", 43 | "All automation and scripting activity should be carefully logged.\n", 44 | "\n", 45 | "The ```logging``` module:\n", 46 | "\n", 47 | " - can stream logs to files and network\n", 48 | " - is configurable via yml, dict, ini (deprecated)\n", 49 | " - optimizes output via log levels\n", 50 | " - takes care of rotation\n", 51 | " \n", 52 | "Hint: manage logs with syslog.\n", 53 | " " 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": { 60 | "collapsed": false 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "# Logs to stdout|err with a default loglevel.\n", 65 | "# Logs are not printed on jupyter cells but you can \n", 66 | "# check them in another terminal\n", 67 | "logging.basicConfig(level=logging.DEBUG)\n", 68 | "\n", 69 | "# Create a logger.\n", 70 | "log = logging.getLogger()\n", 71 | "\n", 72 | "# Logs supports a print-like syntax, and doesn't \n", 73 | "# build unneeded message strings!\n", 74 | "# still using old string interpolation in this casse is useful\n", 75 | "log.info(\"Use %r instead of %s to avoid\", [u\"Serialization\"], \"issues\")\n", 76 | "\n" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": { 83 | "collapsed": false 84 | }, 85 | "outputs": [], 86 | "source": [ 87 | "\n", 88 | "%cat logger.yml\n" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": { 95 | "collapsed": false 96 | }, 97 | "outputs": [], 98 | "source": [ 99 | "with open('logger.yml') as logger_config:\n", 100 | " logging.config.dictConfig(yaml.safe_load(logger_config))\n", 101 | " \n", 102 | "# The ```os``` module goes to syslog\n", 103 | "log.info(\"To syslog?\")\n", 104 | "log.error(\"To syslog\")\n", 105 | "log.debug(\"test debug\")\n" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": { 112 | "collapsed": true 113 | }, 114 | "outputs": [], 115 | "source": [ 116 | "# To process compressed files, use an helper function. \n", 117 | "import mimetools\n", 118 | "import gzip\n", 119 | "import bz2\n", 120 | "\n", 121 | "def log_open(path):\n", 122 | " \"\"\"Open log files using its mimetype to choose the correct method\"\"\"\n", 123 | " l_type, l_encoding = mimetypes.guess_type(path)\n", 124 | " if l_encoding == 'gzip':\n", 125 | " return gzip.open(path, 'rb')\n", 126 | " elif l_encoding == 'bzip2':\n", 127 | " return bz2.BZ2File(path, 'rb')\n", 128 | " else:\n", 129 | " return open(path, 'rb')\n" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": { 136 | "collapsed": false 137 | }, 138 | "outputs": [], 139 | "source": [ 140 | "# Exercise:\n", 141 | "# log some messages modifying the default format string.\n", 142 | "# use log_open to open gzip files or plain texts" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": { 149 | "collapsed": true 150 | }, 151 | "outputs": [], 152 | "source": [] 153 | } 154 | ], 155 | "metadata": { 156 | "kernelspec": { 157 | "display_name": "Python 2", 158 | "language": "python", 159 | "name": "python2" 160 | }, 161 | "language_info": { 162 | "codemirror_mode": { 163 | "name": "ipython", 164 | "version": 2 165 | }, 166 | "file_extension": ".py", 167 | "mimetype": "text/x-python", 168 | "name": "python", 169 | "nbconvert_exporter": "python", 170 | "pygments_lexer": "ipython2", 171 | "version": "2.7.5" 172 | } 173 | }, 174 | "nbformat": 4, 175 | "nbformat_minor": 0 176 | } 177 | -------------------------------------------------------------------------------- /sysadmin/logger.yml: -------------------------------------------------------------------------------- 1 | version: 1 2 | formatters: 3 | detailed: 4 | class: logging.Formatter 5 | format: '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s' 6 | 7 | handlers: 8 | console: 9 | class: logging.StreamHandler 10 | level: INFO 11 | syslog: 12 | class: logging.handlers.SysLogHandler 13 | formatter: detailed 14 | level: DEBUG 15 | # This is for mac osx 16 | address: /var/run/syslog 17 | facility: local1 18 | 19 | loggers: 20 | os: 21 | handlers: [syslog] 22 | 23 | root: 24 | handlers: [console, syslog] 25 | level: DEBUG 26 | -------------------------------------------------------------------------------- /sysadmin/parsing-lifecycle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/parsing-lifecycle.png -------------------------------------------------------------------------------- /sysadmin/proc/1/attr/current: -------------------------------------------------------------------------------- 1 | system_u:system_r:init_t:s0 -------------------------------------------------------------------------------- /sysadmin/proc/1/attr/exec: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/attr/exec -------------------------------------------------------------------------------- /sysadmin/proc/1/attr/fscreate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/attr/fscreate -------------------------------------------------------------------------------- /sysadmin/proc/1/attr/keycreate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/attr/keycreate -------------------------------------------------------------------------------- /sysadmin/proc/1/attr/prev: -------------------------------------------------------------------------------- 1 | system_u:system_r:kernel_t:s0 -------------------------------------------------------------------------------- /sysadmin/proc/1/attr/sockcreate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/attr/sockcreate -------------------------------------------------------------------------------- /sysadmin/proc/1/autogroup: -------------------------------------------------------------------------------- 1 | /autogroup-0 nice 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/cgroup: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/cgroup -------------------------------------------------------------------------------- /sysadmin/proc/1/cmdline: -------------------------------------------------------------------------------- 1 | /sbin/init -------------------------------------------------------------------------------- /sysadmin/proc/1/comm: -------------------------------------------------------------------------------- 1 | init 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/coredump_filter: -------------------------------------------------------------------------------- 1 | 00000033 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/cpuset: -------------------------------------------------------------------------------- 1 | / 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/loginuid: -------------------------------------------------------------------------------- 1 | 4294967295 -------------------------------------------------------------------------------- /sysadmin/proc/1/mountinfo: -------------------------------------------------------------------------------- 1 | 16 21 0:3 / /proc rw,relatime - proc proc rw 2 | 17 21 0:0 / /sys rw,relatime - sysfs sysfs rw,seclabel 3 | 18 23 0:5 / /dev rw,relatime - devtmpfs devtmpfs rw,seclabel,size=951920k,nr_inodes=237980,mode=755 4 | 19 18 0:11 / /dev/pts rw,relatime - devpts devpts rw,seclabel,gid=5,mode=620,ptmxmode=000 5 | 20 18 0:16 / /dev/shm rw,relatime - tmpfs tmpfs rw,seclabel 6 | 21 1 252:1 / / rw,relatime - ext4 /dev/vda1 rw,seclabel,barrier=1,data=ordered 7 | 22 21 0:14 / /selinux rw,relatime - selinuxfs none rw 8 | 23 21 0:5 / /dev rw,relatime - devtmpfs devtmpfs rw,seclabel,size=951920k,nr_inodes=237980,mode=755 9 | 24 16 0:15 / /proc/bus/usb rw,relatime - usbfs /proc/bus/usb rw 10 | 25 16 0:17 / /proc/sys/fs/binfmt_misc rw,relatime - binfmt_misc none rw 11 | -------------------------------------------------------------------------------- /sysadmin/proc/1/mounts: -------------------------------------------------------------------------------- 1 | rootfs / rootfs rw 0 0 2 | proc /proc proc rw,relatime 0 0 3 | sysfs /sys sysfs rw,seclabel,relatime 0 0 4 | devtmpfs /dev devtmpfs rw,seclabel,relatime,size=951920k,nr_inodes=237980,mode=755 0 0 5 | devpts /dev/pts devpts rw,seclabel,relatime,gid=5,mode=620,ptmxmode=000 0 0 6 | tmpfs /dev/shm tmpfs rw,seclabel,relatime 0 0 7 | /dev/vda1 / ext4 rw,seclabel,relatime,barrier=1,data=ordered 0 0 8 | none /selinux selinuxfs rw,relatime 0 0 9 | devtmpfs /dev devtmpfs rw,seclabel,relatime,size=951920k,nr_inodes=237980,mode=755 0 0 10 | /proc/bus/usb /proc/bus/usb usbfs rw,relatime 0 0 11 | none /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0 12 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/anycast6: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/net/anycast6 -------------------------------------------------------------------------------- /sysadmin/proc/1/net/arp: -------------------------------------------------------------------------------- 1 | IP address HW type Flags HW address Mask Device 2 | 172.16.99.2 0x1 0x2 fa:16:3e:66:de:69 * eth2 3 | 172.16.3.132 0x1 0x2 fa:16:3e:9d:31:65 * eth1 4 | 172.16.3.21 0x1 0x2 fa:16:3e:16:a9:1e * eth1 5 | 172.16.3.150 0x1 0x2 fa:16:3e:33:ec:54 * eth1 6 | 192.168.3.21 0x1 0x2 fa:16:3e:63:fc:da * eth0 7 | 172.16.3.149 0x1 0x2 fa:16:3e:a9:00:11 * eth1 8 | 172.16.3.134 0x1 0x2 fa:16:3e:3f:e8:b2 * eth1 9 | 172.16.3.1 0x1 0x2 fa:16:3e:9f:bf:4f * eth1 10 | 172.16.3.133 0x1 0x2 fa:16:3e:af:2c:49 * eth1 11 | 192.168.3.1 0x1 0x2 fa:16:3e:fa:bd:55 * eth0 12 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/connector: -------------------------------------------------------------------------------- 1 | Name ID 2 | cn_proc 1:1 3 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/dev: -------------------------------------------------------------------------------- 1 | Inter-| Receive | Transmit 2 | face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed 3 | lo: 140127945 815843 0 0 0 0 0 0 140127945 815843 0 0 0 0 0 0 4 | eth0: 884865676 519301 0 0 0 0 0 0 42042478 526817 0 0 0 0 0 0 5 | eth1: 2566775006 10959780 0 0 0 0 0 0 1698046004 16045927 0 0 0 0 0 0 6 | eth2: 6558114 63976 0 0 0 0 0 0 6480089 63850 0 0 0 0 0 0 7 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/dev_mcast: -------------------------------------------------------------------------------- 1 | 2 eth0 1 0 3333ff290975 2 | 2 eth0 1 0 333300000001 3 | 2 eth0 1 0 333300000202 4 | 2 eth0 1 0 01005e000001 5 | 3 eth1 1 0 3333ff1b930d 6 | 3 eth1 1 0 333300000001 7 | 3 eth1 1 0 333300000202 8 | 3 eth1 1 0 01005e000001 9 | 4 eth2 1 0 3333ff3aba73 10 | 4 eth2 1 0 333300000001 11 | 4 eth2 1 0 333300000202 12 | 4 eth2 1 0 01005e000001 13 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/dev_snmp6/eth0: -------------------------------------------------------------------------------- 1 | ifIndex 2 2 | Ip6InReceives 5 3 | Ip6InHdrErrors 0 4 | Ip6InTooBigErrors 0 5 | Ip6InNoRoutes 0 6 | Ip6InAddrErrors 0 7 | Ip6InUnknownProtos 0 8 | Ip6InTruncatedPkts 0 9 | Ip6InDiscards 0 10 | Ip6InDelivers 0 11 | Ip6OutForwDatagrams 0 12 | Ip6OutRequests 14 13 | Ip6OutDiscards 0 14 | Ip6OutNoRoutes 0 15 | Ip6ReasmTimeout 0 16 | Ip6ReasmReqds 0 17 | Ip6ReasmOKs 0 18 | Ip6ReasmFails 0 19 | Ip6FragOKs 0 20 | Ip6FragFails 0 21 | Ip6FragCreates 0 22 | Ip6InMcastPkts 0 23 | Ip6OutMcastPkts 20 24 | Ip6InOctets 320 25 | Ip6OutOctets 960 26 | Ip6InMcastOctets 0 27 | Ip6OutMcastOctets 1456 28 | Ip6InBcastOctets 0 29 | Ip6OutBcastOctets 0 30 | Icmp6InMsgs 0 31 | Icmp6InErrors 0 32 | Icmp6OutMsgs 14 33 | Icmp6InDestUnreachs 0 34 | Icmp6InPktTooBigs 0 35 | Icmp6InTimeExcds 0 36 | Icmp6InParmProblems 0 37 | Icmp6InEchos 0 38 | Icmp6InEchoReplies 0 39 | Icmp6InGroupMembQueries 0 40 | Icmp6InGroupMembResponses 0 41 | Icmp6InGroupMembReductions 0 42 | Icmp6InRouterSolicits 0 43 | Icmp6InRouterAdvertisements 0 44 | Icmp6InNeighborSolicits 0 45 | Icmp6InNeighborAdvertisements 0 46 | Icmp6InRedirects 0 47 | Icmp6InMLDv2Reports 0 48 | Icmp6OutDestUnreachs 0 49 | Icmp6OutPktTooBigs 0 50 | Icmp6OutTimeExcds 0 51 | Icmp6OutParmProblems 0 52 | Icmp6OutEchos 0 53 | Icmp6OutEchoReplies 0 54 | Icmp6OutGroupMembQueries 0 55 | Icmp6OutGroupMembResponses 0 56 | Icmp6OutGroupMembReductions 0 57 | Icmp6OutRouterSolicits 6 58 | Icmp6OutRouterAdvertisements 0 59 | Icmp6OutNeighborSolicits 2 60 | Icmp6OutNeighborAdvertisements 0 61 | Icmp6OutRedirects 0 62 | Icmp6OutMLDv2Reports 6 63 | Icmp6OutType133 6 64 | Icmp6OutType135 2 65 | Icmp6OutType143 6 66 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/dev_snmp6/eth1: -------------------------------------------------------------------------------- 1 | ifIndex 3 2 | Ip6InReceives 4 3 | Ip6InHdrErrors 0 4 | Ip6InTooBigErrors 0 5 | Ip6InNoRoutes 0 6 | Ip6InAddrErrors 0 7 | Ip6InUnknownProtos 0 8 | Ip6InTruncatedPkts 0 9 | Ip6InDiscards 0 10 | Ip6InDelivers 0 11 | Ip6OutForwDatagrams 0 12 | Ip6OutRequests 14 13 | Ip6OutDiscards 0 14 | Ip6OutNoRoutes 0 15 | Ip6ReasmTimeout 0 16 | Ip6ReasmReqds 0 17 | Ip6ReasmOKs 0 18 | Ip6ReasmFails 0 19 | Ip6FragOKs 0 20 | Ip6FragFails 0 21 | Ip6FragCreates 0 22 | Ip6InMcastPkts 0 23 | Ip6OutMcastPkts 20 24 | Ip6InOctets 244 25 | Ip6OutOctets 960 26 | Ip6InMcastOctets 0 27 | Ip6OutMcastOctets 1456 28 | Ip6InBcastOctets 0 29 | Ip6OutBcastOctets 0 30 | Icmp6InMsgs 0 31 | Icmp6InErrors 0 32 | Icmp6OutMsgs 14 33 | Icmp6InDestUnreachs 0 34 | Icmp6InPktTooBigs 0 35 | Icmp6InTimeExcds 0 36 | Icmp6InParmProblems 0 37 | Icmp6InEchos 0 38 | Icmp6InEchoReplies 0 39 | Icmp6InGroupMembQueries 0 40 | Icmp6InGroupMembResponses 0 41 | Icmp6InGroupMembReductions 0 42 | Icmp6InRouterSolicits 0 43 | Icmp6InRouterAdvertisements 0 44 | Icmp6InNeighborSolicits 0 45 | Icmp6InNeighborAdvertisements 0 46 | Icmp6InRedirects 0 47 | Icmp6InMLDv2Reports 0 48 | Icmp6OutDestUnreachs 0 49 | Icmp6OutPktTooBigs 0 50 | Icmp6OutTimeExcds 0 51 | Icmp6OutParmProblems 0 52 | Icmp6OutEchos 0 53 | Icmp6OutEchoReplies 0 54 | Icmp6OutGroupMembQueries 0 55 | Icmp6OutGroupMembResponses 0 56 | Icmp6OutGroupMembReductions 0 57 | Icmp6OutRouterSolicits 6 58 | Icmp6OutRouterAdvertisements 0 59 | Icmp6OutNeighborSolicits 2 60 | Icmp6OutNeighborAdvertisements 0 61 | Icmp6OutRedirects 0 62 | Icmp6OutMLDv2Reports 6 63 | Icmp6OutType133 6 64 | Icmp6OutType135 2 65 | Icmp6OutType143 6 66 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/dev_snmp6/eth2: -------------------------------------------------------------------------------- 1 | ifIndex 4 2 | Ip6InReceives 4 3 | Ip6InHdrErrors 0 4 | Ip6InTooBigErrors 0 5 | Ip6InNoRoutes 0 6 | Ip6InAddrErrors 0 7 | Ip6InUnknownProtos 0 8 | Ip6InTruncatedPkts 0 9 | Ip6InDiscards 0 10 | Ip6InDelivers 0 11 | Ip6OutForwDatagrams 0 12 | Ip6OutRequests 14 13 | Ip6OutDiscards 0 14 | Ip6OutNoRoutes 0 15 | Ip6ReasmTimeout 0 16 | Ip6ReasmReqds 0 17 | Ip6ReasmOKs 0 18 | Ip6ReasmFails 0 19 | Ip6FragOKs 0 20 | Ip6FragFails 0 21 | Ip6FragCreates 0 22 | Ip6InMcastPkts 0 23 | Ip6OutMcastPkts 20 24 | Ip6InOctets 244 25 | Ip6OutOctets 960 26 | Ip6InMcastOctets 0 27 | Ip6OutMcastOctets 1456 28 | Ip6InBcastOctets 0 29 | Ip6OutBcastOctets 0 30 | Icmp6InMsgs 0 31 | Icmp6InErrors 0 32 | Icmp6OutMsgs 14 33 | Icmp6InDestUnreachs 0 34 | Icmp6InPktTooBigs 0 35 | Icmp6InTimeExcds 0 36 | Icmp6InParmProblems 0 37 | Icmp6InEchos 0 38 | Icmp6InEchoReplies 0 39 | Icmp6InGroupMembQueries 0 40 | Icmp6InGroupMembResponses 0 41 | Icmp6InGroupMembReductions 0 42 | Icmp6InRouterSolicits 0 43 | Icmp6InRouterAdvertisements 0 44 | Icmp6InNeighborSolicits 0 45 | Icmp6InNeighborAdvertisements 0 46 | Icmp6InRedirects 0 47 | Icmp6InMLDv2Reports 0 48 | Icmp6OutDestUnreachs 0 49 | Icmp6OutPktTooBigs 0 50 | Icmp6OutTimeExcds 0 51 | Icmp6OutParmProblems 0 52 | Icmp6OutEchos 0 53 | Icmp6OutEchoReplies 0 54 | Icmp6OutGroupMembQueries 0 55 | Icmp6OutGroupMembResponses 0 56 | Icmp6OutGroupMembReductions 0 57 | Icmp6OutRouterSolicits 6 58 | Icmp6OutRouterAdvertisements 0 59 | Icmp6OutNeighborSolicits 2 60 | Icmp6OutNeighborAdvertisements 0 61 | Icmp6OutRedirects 0 62 | Icmp6OutMLDv2Reports 6 63 | Icmp6OutType133 6 64 | Icmp6OutType135 2 65 | Icmp6OutType143 6 66 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/dev_snmp6/lo: -------------------------------------------------------------------------------- 1 | ifIndex 1 2 | Ip6InReceives 155737 3 | Ip6InHdrErrors 0 4 | Ip6InTooBigErrors 0 5 | Ip6InNoRoutes 0 6 | Ip6InAddrErrors 0 7 | Ip6InUnknownProtos 0 8 | Ip6InTruncatedPkts 0 9 | Ip6InDiscards 0 10 | Ip6InDelivers 155737 11 | Ip6OutForwDatagrams 0 12 | Ip6OutRequests 155737 13 | Ip6OutDiscards 0 14 | Ip6OutNoRoutes 0 15 | Ip6ReasmTimeout 0 16 | Ip6ReasmReqds 0 17 | Ip6ReasmOKs 0 18 | Ip6ReasmFails 0 19 | Ip6FragOKs 0 20 | Ip6FragFails 0 21 | Ip6FragCreates 0 22 | Ip6InMcastPkts 0 23 | Ip6OutMcastPkts 0 24 | Ip6InOctets 20994704 25 | Ip6OutOctets 20994704 26 | Ip6InMcastOctets 0 27 | Ip6OutMcastOctets 0 28 | Ip6InBcastOctets 0 29 | Ip6OutBcastOctets 0 30 | Icmp6InMsgs 1400 31 | Icmp6InErrors 0 32 | Icmp6OutMsgs 1400 33 | Icmp6InDestUnreachs 1400 34 | Icmp6InPktTooBigs 0 35 | Icmp6InTimeExcds 0 36 | Icmp6InParmProblems 0 37 | Icmp6InEchos 0 38 | Icmp6InEchoReplies 0 39 | Icmp6InGroupMembQueries 0 40 | Icmp6InGroupMembResponses 0 41 | Icmp6InGroupMembReductions 0 42 | Icmp6InRouterSolicits 0 43 | Icmp6InRouterAdvertisements 0 44 | Icmp6InNeighborSolicits 0 45 | Icmp6InNeighborAdvertisements 0 46 | Icmp6InRedirects 0 47 | Icmp6InMLDv2Reports 0 48 | Icmp6OutDestUnreachs 1400 49 | Icmp6OutPktTooBigs 0 50 | Icmp6OutTimeExcds 0 51 | Icmp6OutParmProblems 0 52 | Icmp6OutEchos 0 53 | Icmp6OutEchoReplies 0 54 | Icmp6OutGroupMembQueries 0 55 | Icmp6OutGroupMembResponses 0 56 | Icmp6OutGroupMembReductions 0 57 | Icmp6OutRouterSolicits 0 58 | Icmp6OutRouterAdvertisements 0 59 | Icmp6OutNeighborSolicits 0 60 | Icmp6OutNeighborAdvertisements 0 61 | Icmp6OutRedirects 0 62 | Icmp6OutMLDv2Reports 0 63 | Icmp6InType1 1400 64 | Icmp6OutType1 1400 65 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/icmp: -------------------------------------------------------------------------------- 1 | sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/if_inet6: -------------------------------------------------------------------------------- 1 | fe80000000000000f8163efffe3aba73 04 40 20 80 eth2 2 | fe80000000000000f8163efffe1b930d 03 40 20 80 eth1 3 | 00000000000000000000000000000001 01 80 10 80 lo 4 | fe80000000000000f8163efffe290975 02 40 20 80 eth0 5 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/igmp: -------------------------------------------------------------------------------- 1 | Idx Device : Count Querier Group Users Timer Reporter 2 | 1 lo : 1 V3 3 | 010000E0 1 0:00000000 0 4 | 2 eth0 : 1 V3 5 | 010000E0 1 0:00000000 0 6 | 3 eth1 : 1 V3 7 | 010000E0 1 0:00000000 0 8 | 4 eth2 : 1 V3 9 | 010000E0 1 0:00000000 0 10 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/igmp6: -------------------------------------------------------------------------------- 1 | 1 lo ff020000000000000000000000000001 1 0000000C 0 2 | 2 eth0 ff0200000000000000000001ff290975 1 00000004 0 3 | 2 eth0 ff020000000000000000000000000202 1 00000004 0 4 | 2 eth0 ff020000000000000000000000000001 1 0000000C 0 5 | 3 eth1 ff0200000000000000000001ff1b930d 1 00000004 0 6 | 3 eth1 ff020000000000000000000000000202 1 00000004 0 7 | 3 eth1 ff020000000000000000000000000001 1 0000000C 0 8 | 4 eth2 ff0200000000000000000001ff3aba73 1 00000004 0 9 | 4 eth2 ff020000000000000000000000000202 1 00000004 0 10 | 4 eth2 ff020000000000000000000000000001 1 0000000C 0 11 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/ip6_flowlabel: -------------------------------------------------------------------------------- 1 | Label S Owner Users Linger Expires Dst Opt 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/ip6_mr_cache: -------------------------------------------------------------------------------- 1 | Group Origin Iif Pkts Bytes Wrong Oifs 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/ip6_mr_vif: -------------------------------------------------------------------------------- 1 | Interface BytesIn PktsIn BytesOut PktsOut Flags 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/ip_mr_cache: -------------------------------------------------------------------------------- 1 | Group Origin Iif Pkts Bytes Wrong Oifs 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/ip_mr_vif: -------------------------------------------------------------------------------- 1 | Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/ipv6_route: -------------------------------------------------------------------------------- 1 | 00000000000000000000000000000000 60 00000000000000000000000000000000 00 00000000000000000000000000000000 00000400 00000000 00000000 00200200 lo 2 | 00000000000000000000ffff00000000 60 00000000000000000000000000000000 00 00000000000000000000000000000000 00000400 00000000 00000000 00200200 lo 3 | 20020a00000000000000000000000000 18 00000000000000000000000000000000 00 00000000000000000000000000000000 00000400 00000000 00000000 00200200 lo 4 | 20027f00000000000000000000000000 18 00000000000000000000000000000000 00 00000000000000000000000000000000 00000400 00000000 00000000 00200200 lo 5 | 2002a9fe000000000000000000000000 20 00000000000000000000000000000000 00 00000000000000000000000000000000 00000400 00000000 00000000 00200200 lo 6 | 2002ac10000000000000000000000000 1c 00000000000000000000000000000000 00 00000000000000000000000000000000 00000400 00000000 00000000 00200200 lo 7 | 2002c0a8000000000000000000000000 20 00000000000000000000000000000000 00 00000000000000000000000000000000 00000400 00000000 00000000 00200200 lo 8 | 2002e000000000000000000000000000 13 00000000000000000000000000000000 00 00000000000000000000000000000000 00000400 00000000 00000000 00200200 lo 9 | 3ffeffff000000000000000000000000 20 00000000000000000000000000000000 00 00000000000000000000000000000000 00000400 00000000 00000000 00200200 lo 10 | fe800000000000000000000000000000 40 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000000 00000000 00000001 eth0 11 | fe800000000000000000000000000000 40 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000000 00000000 00000001 eth1 12 | fe800000000000000000000000000000 40 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000000 00000000 00000001 eth2 13 | 00000000000000000000000000000000 00 00000000000000000000000000000000 00 00000000000000000000000000000000 ffffffff 00000001 000007d6 00200200 lo 14 | 00000000000000000000000000000001 80 00000000000000000000000000000000 00 00000000000000000000000000000000 00000000 00000001 0001bf64 80200001 lo 15 | fe80000000000000f8163efffe1b930d 80 00000000000000000000000000000000 00 00000000000000000000000000000000 00000000 00000001 00000000 80200001 lo 16 | fe80000000000000f8163efffe290975 80 00000000000000000000000000000000 00 00000000000000000000000000000000 00000000 00000001 00000000 80200001 lo 17 | fe80000000000000f8163efffe3aba73 80 00000000000000000000000000000000 00 00000000000000000000000000000000 00000000 00000001 00000000 80200001 lo 18 | ff000000000000000000000000000000 08 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000000 00000000 00000001 eth0 19 | ff000000000000000000000000000000 08 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000000 00000000 00000001 eth1 20 | ff000000000000000000000000000000 08 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000000 00000000 00000001 eth2 21 | 00000000000000000000000000000000 00 00000000000000000000000000000000 00 00000000000000000000000000000000 ffffffff 00000001 000007d6 00200200 lo 22 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/mcfilter: -------------------------------------------------------------------------------- 1 | Idx Device MCA SRC INC EXC 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/mcfilter6: -------------------------------------------------------------------------------- 1 | Idx Device Multicast Address Source Address INC EXC 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/netfilter/nf_log: -------------------------------------------------------------------------------- 1 | 0 NONE () 2 | 1 NONE () 3 | 2 NONE () 4 | 3 NONE () 5 | 4 NONE () 6 | 5 NONE () 7 | 6 NONE () 8 | 7 NONE () 9 | 8 NONE () 10 | 9 NONE () 11 | 10 NONE () 12 | 11 NONE () 13 | 12 NONE () 14 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/netfilter/nf_queue: -------------------------------------------------------------------------------- 1 | 0 NONE 2 | 1 NONE 3 | 2 NONE 4 | 3 NONE 5 | 4 NONE 6 | 5 NONE 7 | 6 NONE 8 | 7 NONE 9 | 8 NONE 10 | 9 NONE 11 | 10 NONE 12 | 11 NONE 13 | 12 NONE 14 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/netlink: -------------------------------------------------------------------------------- 1 | sk Eth Pid Groups Rmem Wmem Dump Locks Drops 2 | ffff88007e4d7000 0 0 00000000 0 0 (null) 2 0 3 | ffff8800375a8800 0 15631 00000771 0 0 (null) 2 0 4 | ffff880037a2f400 6 0 00000000 0 0 (null) 2 0 5 | ffff88007d655c00 7 7577 00000001 0 0 (null) 2 0 6 | ffff88007abc7000 7 0 00000000 0 0 (null) 2 0 7 | ffff88007ab5dc00 9 0 00000000 0 0 (null) 2 0 8 | ffff880037aa9400 9 1255 00000000 0 0 (null) 2 0 9 | ffff88007e23f400 10 0 00000000 0 0 (null) 2 0 10 | ffff88007e756c00 11 0 00000000 0 0 (null) 2 0 11 | ffff880037e36400 15 393 00000000 0 0 (null) 2 0 12 | ffff880037c38c00 15 392 00000001 0 0 (null) 2 0 13 | ffff88007e4db800 15 0 00000000 0 0 (null) 2 0 14 | ffff880037d53400 15 -4144 00000000 0 0 (null) 2 0 15 | ffff88007e7f8c00 16 0 00000000 0 0 (null) 2 0 16 | ffff88007e756000 18 0 00000000 0 0 (null) 2 0 17 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/netstat: -------------------------------------------------------------------------------- 1 | TcpExt: SyncookiesSent SyncookiesRecv SyncookiesFailed EmbryonicRsts PruneCalled RcvPruned OfoPruned OutOfWindowIcmps LockDroppedIcmps ArpFilter TW TWRecycled TWKilled PAWSPassive PAWSActive PAWSEstab DelayedACKs DelayedACKLocked DelayedACKLost ListenOverflows ListenDrops TCPPrequeued TCPDirectCopyFromBacklog TCPDirectCopyFromPrequeue TCPPrequeueDropped TCPHPHits TCPHPHitsToUser TCPPureAcks TCPHPAcks TCPRenoRecovery TCPSackRecovery TCPSACKReneging TCPFACKReorder TCPSACKReorder TCPRenoReorder TCPTSReorder TCPFullUndo TCPPartialUndo TCPDSACKUndo TCPLossUndo TCPLoss TCPLostRetransmit TCPRenoFailures TCPSackFailures TCPLossFailures TCPFastRetrans TCPForwardRetrans TCPSlowStartRetrans TCPTimeouts TCPRenoRecoveryFail TCPSackRecoveryFail TCPSchedulerFailed TCPRcvCollapsed TCPDSACKOldSent TCPDSACKOfoSent TCPDSACKRecv TCPDSACKOfoRecv TCPAbortOnData TCPAbortOnClose TCPAbortOnMemory TCPAbortOnTimeout TCPAbortOnLinger TCPAbortFailed TCPMemoryPressures TCPSACKDiscard TCPDSACKIgnoredOld TCPDSACKIgnoredNoUndo TCPSpuriousRTOs TCPMD5NotFound TCPMD5Unexpected TCPSackShifted TCPSackMerged TCPSackShiftFallback TCPBacklogDrop TCPMinTTLDrop TCPChallengeACK TCPSYNChallenge BusyPollRxPackets TCPFromZeroWindowAdv TCPToZeroWindowAdv TCPWantZeroWindowAdv 2 | TcpExt: 0 0 82 0 0 0 0 0 0 0 690814 0 0 0 0 4 1293628 948 1472 0 0 161098 19969246 303309514 0 4850017 75618 1113568 4071407 0 2559 109 30 1760 0 168 2016 655 0 75 6 2 0 958 40 2737 817 2784 885 0 27 0 0 1478 37 2503 0 15887 27 0 1 0 0 0 0 0 1331 0 0 0 263 3256 138195 0 0 10 10 0 0 0 19 3 | IpExt: InNoRoutes InTruncatedPkts InMcastPkts OutMcastPkts InBcastPkts OutBcastPkts InOctets OutOctets InMcastOctets OutMcastOctets InBcastOctets OutBcastOctets 4 | IpExt: 0 0 0 0 0 0 3412827434 1631453048 0 0 0 0 5 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/packet: -------------------------------------------------------------------------------- 1 | sk RefCnt Type Proto Iface R Rmem User Inode 2 | ffff880037bf4400 3 3 0003 4 1 0 0 15170 3 | ffff880037e33c00 3 3 0003 3 1 0 0 15076 4 | ffff880037b7e000 3 3 0003 2 1 0 0 14959 5 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/protocols: -------------------------------------------------------------------------------- 1 | protocol size sockets memory press maxhdr slab module cl co di ac io in de sh ss gs se re sp bi br ha uh gp em 2 | RAWv6 960 0 -1 NI 0 yes ipv6 y y y n y y y n y y y y n y y y y n n 3 | UDPLITEv6 952 0 -1 NI 0 yes ipv6 y y y n y y y n y y y y n n y y y y n 4 | UDPv6 952 8 2 NI 0 yes ipv6 y y y n y n y n y y y y n n y y y y n 5 | TCPv6 1824 10 4 no 272 yes ipv6 y y y y y y y y y y n y n n y y y y y 6 | PACKET 856 3 -1 NI 0 no kernel n n n n n n n n n n n n n n n n n n n 7 | UNIX 760 65 -1 NI 0 yes kernel n n n n n n n n n n n n n n n n n n n 8 | UDP-Lite 784 0 -1 NI 0 yes kernel y y y n y y y n y y y y y n y y y y n 9 | PING 752 0 -1 NI 0 yes kernel y y y n n y n n y y y y n y y y y y n 10 | RAW 760 0 -1 NI 0 yes kernel y y y n y y y n y y y y n y y y y n n 11 | UDP 784 12 2 NI 0 yes kernel y y y n y n y n y y y y y n y y y y n 12 | TCP 1656 44 4 no 272 yes kernel y y y y y y y y y y n y n n y y y y y 13 | NETLINK 720 7 -1 NI 0 no kernel n n n n n n n n n n n n n n n n n n n 14 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/psched: -------------------------------------------------------------------------------- 1 | 000003e8 00000040 000f4240 3b9aca00 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/ptype: -------------------------------------------------------------------------------- 1 | Type Device Function 2 | ALL eth2 packet_rcv+0x0/0x440 3 | ALL eth1 packet_rcv+0x0/0x440 4 | ALL eth0 packet_rcv+0x0/0x440 5 | 0800 ip_rcv+0x0/0x350 6 | 0806 arp_rcv+0x0/0x140 7 | dada edsa_rcv+0x0/0x2b0 8 | 001b dsa_rcv+0x0/0x270 9 | 001c trailer_rcv+0x0/0x1b0 10 | 86dd ipv6_rcv+0x0/0x460 [ipv6] 11 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/raw: -------------------------------------------------------------------------------- 1 | sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/raw6: -------------------------------------------------------------------------------- 1 | sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/route: -------------------------------------------------------------------------------- 1 | Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT 2 | eth0 08065C0A 0103A8C0 0007 0 0 0 FFFFFFFF 0 0 0 3 | eth0 0003A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0 4 | eth1 000310AC 00000000 0001 0 0 0 00FFFFFF 0 0 0 5 | eth2 006310AC 00000000 0001 0 0 0 00FFFFFF 0 0 0 6 | eth0 00005F0A 0103A8C0 0003 0 0 0 0000FFFF 0 0 0 7 | eth1 00000000 010310AC 0003 0 0 0 00000000 0 0 0 8 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/rt6_stats: -------------------------------------------------------------------------------- 1 | 0000 000f 0000 0013 0000 fffffffc 0013 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/rt_acct: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/rt_cache: -------------------------------------------------------------------------------- 1 | Iface Destination Gateway Flags RefCnt Use Metric Source MTU Window IRTT TOS HHRef HHUptod SpecDst 2 | eth0 B4795F0A 0103A8C0 0 0 18 0 8E03A8C0 1500 0 0 00 7 1 8E03A8C0 3 | eth0 B4795F0A 0103A8C0 0 0 26 0 8E03A8C0 1500 0 0 00 7 1 8E03A8C0 4 | eth1 840310AC 840310AC 0 6 5 0 900310AC 1500 0 0 00 2 1 900310AC 5 | eth1 0F0F91C1 010310AC 0 0 1 0 900310AC 1500 0 0 00 -1 0 900310AC 6 | lo 900310AC 900310AC 84000000 0 858 0 840310AC 0 0 0 00 -1 0 900310AC 7 | lo 0B6310AC 0B6310AC 84000000 0 219 0 026310AC 0 0 0 00 -1 0 0B6310AC 8 | eth1 A388A8C0 010310AC 0 0 319 0 900310AC 1500 0 0 00 -1 0 900310AC 9 | eth1 0F0F91C1 010310AC 0 0 1 0 900310AC 1500 0 0 00 -1 0 900310AC 10 | eth1 850310AC 850310AC 0 6 5 0 900310AC 1500 0 0 00 2 1 900310AC 11 | lo 900310AC 900310AC 84000000 0 2462 0 860310AC 0 0 0 00 -1 0 900310AC 12 | lo 8E03A8C0 8E03A8C0 80000000 0 864 0 BAE95F0A 0 0 0 00 -1 0 8E03A8C0 13 | eth0 BAE95F0A 0103A8C0 0 1 1 0 8E03A8C0 1500 0 87 08 7 1 8E03A8C0 14 | eth0 BAE95F0A 0103A8C0 0 0 5 0 8E03A8C0 1500 0 481 00 7 1 8E03A8C0 15 | lo 8E03A8C0 8E03A8C0 80000000 0 27 0 B4795F0A 0 0 0 00 -1 0 8E03A8C0 16 | eth1 860310AC 860310AC 0 0 15 0 900310AC 1500 0 0 00 -1 0 900310AC 17 | lo 0100007F 0100007F 80000000 0 66 0 0100007F 65535 0 0 00 -1 0 0100007F 18 | eth1 591E1234 010310AC 0 1 0 0 900310AC 1500 0 0 00 3 1 900310AC 19 | eth1 15838CC2 010310AC 0 0 1 0 900310AC 1500 0 0 00 -1 0 900310AC 20 | lo 0100007F 0100007F 80000000 0 233 0 0100007F 65535 0 48 00 5 1 0100007F 21 | eth1 15838CC2 010310AC 0 0 1 0 900310AC 1500 0 0 00 -1 0 900310AC 22 | lo 0100007F 0100007F 80000000 0 10 0 0100007F 65535 0 0 00 -1 0 0100007F 23 | lo 900310AC 900310AC 84000000 0 15 0 010310AC 0 0 0 00 -1 0 900310AC 24 | lo 900310AC 900310AC 84000000 0 882 0 850310AC 0 0 0 00 -1 0 900310AC 25 | eth2 026310AC 026310AC 0 0 219 0 0B6310AC 1500 0 0 00 2 1 0B6310AC 26 | lo 8E03A8C0 8E03A8C0 84000000 0 7 0 0103A8C0 0 0 0 00 -1 0 8E03A8C0 27 | eth1 A388A8C0 010310AC 0 0 639 0 900310AC 1500 0 18 00 3 1 900310AC 28 | eth2 026310AC 026310AC 0 0 219 0 0B6310AC 1500 0 0 00 -1 0 0B6310AC 29 | lo 900310AC 900310AC 80000000 0 261 0 591E1234 0 0 0 00 -1 0 900310AC 30 | eth1 860310AC 860310AC 0 20 43 0 900310AC 1500 0 11 00 2 1 900310AC 31 | lo 900310AC 900310AC 80000000 0 1279 0 A388A8C0 0 0 0 00 -1 0 900310AC 32 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/snmp: -------------------------------------------------------------------------------- 1 | Ip: Forwarding DefaultTTL InReceives InHdrErrors InAddrErrors ForwDatagrams InUnknownProtos InDiscards InDelivers OutRequests OutDiscards OutNoRoutes ReasmTimeout ReasmReqds ReasmOKs ReasmFails FragOKs FragFails FragCreates 2 | Ip: 2 64 12133727 0 9 0 0 0 12133718 17247918 0 0 0 0 0 0 0 0 0 3 | Icmp: InMsgs InErrors InDestUnreachs InTimeExcds InParmProbs InSrcQuenchs InRedirects InEchos InEchoReps InTimestamps InTimestampReps InAddrMasks InAddrMaskReps OutMsgs OutErrors OutDestUnreachs OutTimeExcds OutParmProbs OutSrcQuenchs OutRedirects OutEchos OutEchoReps OutTimestamps OutTimestampReps OutAddrMasks OutAddrMaskReps 4 | Icmp: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 5 | IcmpMsg: OutType3 6 | IcmpMsg: 1 7 | Tcp: RtoAlgorithm RtoMin RtoMax MaxConn ActiveOpens PassiveOpens AttemptFails EstabResets CurrEstab InSegs OutSegs RetransSegs InErrs OutRsts 8 | Tcp: 1 200 120000 -1 715899 86326 8883 15317 35 12204767 17309900 8590 10 25977 9 | Udp: InDatagrams NoPorts InErrors OutDatagrams RcvbufErrors SndbufErrors 10 | Udp: 81887 1 0 82364 0 0 11 | UdpLite: InDatagrams NoPorts InErrors OutDatagrams RcvbufErrors SndbufErrors 12 | UdpLite: 0 0 0 0 0 0 13 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/snmp6: -------------------------------------------------------------------------------- 1 | Ip6InReceives 155750 2 | Ip6InHdrErrors 0 3 | Ip6InTooBigErrors 0 4 | Ip6InNoRoutes 13 5 | Ip6InAddrErrors 0 6 | Ip6InUnknownProtos 0 7 | Ip6InTruncatedPkts 0 8 | Ip6InDiscards 0 9 | Ip6InDelivers 155737 10 | Ip6OutForwDatagrams 0 11 | Ip6OutRequests 155779 12 | Ip6OutDiscards 0 13 | Ip6OutNoRoutes 833 14 | Ip6ReasmTimeout 0 15 | Ip6ReasmReqds 0 16 | Ip6ReasmOKs 0 17 | Ip6ReasmFails 0 18 | Ip6FragOKs 0 19 | Ip6FragFails 0 20 | Ip6FragCreates 0 21 | Ip6InMcastPkts 0 22 | Ip6OutMcastPkts 60 23 | Ip6InOctets 20995512 24 | Ip6OutOctets 20997584 25 | Ip6InMcastOctets 0 26 | Ip6OutMcastOctets 4368 27 | Ip6InBcastOctets 0 28 | Ip6OutBcastOctets 0 29 | Icmp6InMsgs 1400 30 | Icmp6InErrors 0 31 | Icmp6OutMsgs 1442 32 | Icmp6InDestUnreachs 1400 33 | Icmp6InPktTooBigs 0 34 | Icmp6InTimeExcds 0 35 | Icmp6InParmProblems 0 36 | Icmp6InEchos 0 37 | Icmp6InEchoReplies 0 38 | Icmp6InGroupMembQueries 0 39 | Icmp6InGroupMembResponses 0 40 | Icmp6InGroupMembReductions 0 41 | Icmp6InRouterSolicits 0 42 | Icmp6InRouterAdvertisements 0 43 | Icmp6InNeighborSolicits 0 44 | Icmp6InNeighborAdvertisements 0 45 | Icmp6InRedirects 0 46 | Icmp6InMLDv2Reports 0 47 | Icmp6OutDestUnreachs 1400 48 | Icmp6OutPktTooBigs 0 49 | Icmp6OutTimeExcds 0 50 | Icmp6OutParmProblems 0 51 | Icmp6OutEchos 0 52 | Icmp6OutEchoReplies 0 53 | Icmp6OutGroupMembQueries 0 54 | Icmp6OutGroupMembResponses 0 55 | Icmp6OutGroupMembReductions 0 56 | Icmp6OutRouterSolicits 18 57 | Icmp6OutRouterAdvertisements 0 58 | Icmp6OutNeighborSolicits 6 59 | Icmp6OutNeighborAdvertisements 0 60 | Icmp6OutRedirects 0 61 | Icmp6OutMLDv2Reports 18 62 | Icmp6InType1 1400 63 | Icmp6OutType1 1400 64 | Icmp6OutType133 18 65 | Icmp6OutType135 6 66 | Icmp6OutType143 18 67 | Udp6InDatagrams 0 68 | Udp6NoPorts 1400 69 | Udp6InErrors 0 70 | Udp6OutDatagrams 1400 71 | UdpLite6InDatagrams 0 72 | UdpLite6NoPorts 0 73 | UdpLite6InErrors 0 74 | UdpLite6OutDatagrams 0 75 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/sockstat: -------------------------------------------------------------------------------- 1 | sockets: used 165 2 | TCP: inuse 44 orphan 0 tw 44 alloc 56 mem 4 3 | UDP: inuse 12 mem 2 4 | UDPLITE: inuse 0 5 | RAW: inuse 0 6 | FRAG: inuse 0 memory 0 7 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/sockstat6: -------------------------------------------------------------------------------- 1 | TCP6: inuse 10 2 | UDP6: inuse 8 3 | UDPLITE6: inuse 0 4 | RAW6: inuse 0 5 | FRAG6: inuse 0 memory 0 6 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/softnet_stat: -------------------------------------------------------------------------------- 1 | 00c909f7 00000000 00000007 00000000 00000000 00000000 00000000 00000000 00000000 00000000 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/tcp6: -------------------------------------------------------------------------------- 1 | sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode 2 | 0: 00000000000000000000000000000000:0FA0 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 491 0 232608 1 ffff880037572840 99 0 0 2 -1 3 | 1: 00000000000000000000000000000000:0FA1 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 491 0 232609 1 ffff8800375720c0 99 0 0 2 -1 4 | 2: 00000000000000000000000000000000:24E3 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 494 0 9744710 1 ffff88007a98d840 99 0 0 2 -1 5 | 3: 00000000000000000000000000000000:0FAB 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 493 0 9734583 1 ffff88007a98d0c0 99 0 0 2 -1 6 | 4: 00000000000000000000000000000000:006F 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 10638 1 ffff88007ab3e7c0 99 0 0 2 -1 7 | 5: 00000000000000000000000000000000:0050 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 9716543 1 ffff88007b3087c0 99 0 0 2 -1 8 | 6: 00000000000000000000000000000000:1D13 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 494 0 9744713 1 ffff88007d63d0c0 99 0 0 2 -1 9 | 7: 00000000000000000000000000000000:A8B5 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 29 0 10753 1 ffff88007ab3e040 99 0 0 2 -1 10 | 8: 00000000000000000000000000000000:0016 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 34508 1 ffff88007d63d840 99 0 0 2 -1 11 | 9: 0000000000000000FFFF0000900310AC:1D13 0000000000000000FFFF0000960310AC:BD44 01 00000000:00000000 00:00000000 00000000 494 0 9752486 1 ffff88007a9e3040 20 3 33 10 -1 12 | 10: 0000000000000000FFFF00000100007F:0FA0 0000000000000000FFFF00000100007F:C33B 06 00000000:00000000 03:00001328 00000000 0 0 0 3 ffff88007c809bc0 13 | 11: 0000000000000000FFFF00000100007F:0FA0 0000000000000000FFFF00000100007F:C321 06 00000000:00000000 03:00000523 00000000 0 0 0 3 ffff88007c809300 14 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/udp: -------------------------------------------------------------------------------- 1 | sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops 2 | 22: 0100007F:0293 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 10737 2 ffff88007aad5480 0 3 | 71: 00000000:0044 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 15171 2 ffff88007aaa1740 0 4 | 71: 00000000:0044 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 15077 2 ffff88007aaa1ac0 0 5 | 71: 00000000:0044 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 14960 2 ffff88007aaa1040 0 6 | 82: 00000000:984F 00000000:0000 07 00000000:00000000 00:00000000 00000000 29 0 10741 2 ffff880037aa8780 0 7 | 114: 00000000:006F 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 10630 2 ffff88007aaa13c0 0 8 | 118: 00000000:0273 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 10632 2 ffff880037aa8b00 0 9 | 126: 0B6310AC:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 9699363 2 ffff880037aa8400 0 10 | 126: 900310AC:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 9699362 2 ffff88007db31840 0 11 | 126: 8E03A8C0:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 9699361 2 ffff88007aad5100 0 12 | 126: 0100007F:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 9699360 2 ffff88007aad5800 0 13 | 126: 00000000:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 9699353 2 ffff88007db31bc0 0 14 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/udp6: -------------------------------------------------------------------------------- 1 | sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops 2 | 55: 00000000000000000000000000000000:E034 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 29 0 10749 2 ffff88007aafa000 0 3 | 114: 00000000000000000000000000000000:006F 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 10635 2 ffff88007aafac00 0 4 | 118: 00000000000000000000000000000000:0273 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 10637 2 ffff88007aafa800 0 5 | 126: 000080FE00000000FF3E16F8750929FE:007B 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 9699367 2 ffff88007d7a4c00 0 6 | 126: 00000000000000000000000001000000:007B 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 9699366 2 ffff88007d7a4800 0 7 | 126: 000080FE00000000FF3E16F80D931BFE:007B 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 9699365 2 ffff88007d7a4400 0 8 | 126: 000080FE00000000FF3E16F873BA3AFE:007B 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 9699364 2 ffff88007d7a4000 0 9 | 126: 00000000000000000000000000000000:007B 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 9699354 2 ffff88007d77bc00 0 10 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/udplite: -------------------------------------------------------------------------------- 1 | sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/udplite6: -------------------------------------------------------------------------------- 1 | sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/unix: -------------------------------------------------------------------------------- 1 | Num RefCount Protocol Flags Type St Inode Path 2 | ffff880037b0a3c0: 0000001F 00000000 00000000 0002 01 10518 /dev/log 3 | ffff88007d4a72c0: 00000002 00000000 00010000 0001 01 9714041 /var/run/tdaf-accounts/accounts-backend-1.sock 4 | ffff880037b0a080: 00000002 00000000 00010000 0001 01 9714043 /var/run/tdaf-accounts/accounts-backend-2.sock 5 | ffff88007d0a9b00: 00000002 00000000 00010000 0001 01 9714045 /var/run/tdaf-accounts/accounts-backend-3.sock 6 | ffff88007d93dac0: 00000002 00000000 00010000 0001 01 9715353 /var/run/tdaf-accounts/accounts-frontend-1.sock 7 | ffff88007d0a8100: 00000002 00000000 00010000 0001 01 9715355 /var/run/tdaf-accounts/accounts-frontend-2.sock 8 | ffff880037b0aa40: 00000002 00000000 00010000 0001 01 10628 /var/run/rpcbind.sock 9 | ffff88007d513b00: 00000002 00000000 00010000 0001 01 10899 /var/run/acpid.socket 10 | ffff880037b0b400: 00000002 00000000 00010000 0001 01 8178 @/com/ubuntu/upstart 11 | ffff880037b0b740: 00000002 00000000 00000000 0002 01 8597 @/org/kernel/udev/udevd 12 | ffff88007a825600: 00000002 00000000 00010000 0001 01 9748211 /var/run/tdaf-api-cyclops-agent/supervisor.sock.24782 13 | ffff88007d4b6480: 00000002 00000000 00010000 0001 01 48284 /var/run/nscd/socket 14 | ffff88007a824280: 00000003 00000000 00000000 0001 03 13290265 15 | ffff88007a825c80: 00000003 00000000 00000000 0001 03 13290264 16 | ffff88007a825940: 00000002 00000000 00000000 0002 01 13290261 17 | ffff88007c9df600: 00000002 00000000 00000000 0002 01 13284944 18 | ffff88007d93cdc0: 00000002 00000000 00000000 0001 03 9744641 19 | ffff88007bcac480: 00000002 00000000 00000000 0001 03 9744517 20 | ffff88007a8245c0: 00000003 00000000 00000000 0001 03 9716550 21 | ffff88007d512780: 00000003 00000000 00000000 0001 03 9716549 22 | ffff88007bcacb00: 00000003 00000000 00000000 0001 03 9715379 23 | ffff88007bcace40: 00000003 00000000 00000000 0001 03 9715378 24 | ffff88007bcad180: 00000003 00000000 00000000 0001 03 9715377 25 | ffff88007bcad4c0: 00000003 00000000 00000000 0001 03 9715376 26 | ffff88007bcad800: 00000003 00000000 00000000 0001 03 9715375 27 | ffff88007bcadb40: 00000003 00000000 00000000 0001 03 9715374 28 | ffff88007d0a8780: 00000003 00000000 00000000 0001 03 9714076 29 | ffff88007d0a8ac0: 00000003 00000000 00000000 0001 03 9714075 30 | ffff88007d0a8e00: 00000003 00000000 00000000 0001 03 9714067 31 | ffff88007d0a9140: 00000003 00000000 00000000 0001 03 9714066 32 | ffff88007d0a9480: 00000003 00000000 00000000 0001 03 9714065 33 | ffff88007d0a97c0: 00000003 00000000 00000000 0001 03 9714064 34 | ffff88007d93d780: 00000002 00000000 00000000 0002 01 9699349 35 | ffff88007c9de900: 00000003 00000000 00000000 0001 03 232602 36 | ffff88007a824900: 00000003 00000000 00000000 0001 03 232601 37 | ffff88007d93c0c0: 00000002 00000000 00000000 0002 01 79201 38 | ffff88007d4a6900: 00000002 00000000 00000000 0002 01 79200 39 | ffff88007d93c400: 00000002 00000000 00000000 0002 01 79199 40 | ffff88007d93c740: 00000002 00000000 00000000 0002 01 79198 41 | ffff88007d93ca80: 00000002 00000000 00000000 0002 01 79197 42 | ffff88007d4b6140: 00000002 00000000 00000000 0002 01 79196 43 | ffff88007d4a7600: 00000002 00000000 00000000 0002 01 79195 44 | ffff88007d4b7180: 00000002 00000000 00000000 0002 01 79194 45 | ffff88007d4b74c0: 00000002 00000000 00000000 0002 01 78153 46 | ffff88007d4b7800: 00000002 00000000 00000000 0002 01 78152 47 | ffff88007d4a7c80: 00000002 00000000 00000000 0002 01 78151 48 | ffff88007d4a7940: 00000002 00000000 00000000 0002 01 78150 49 | ffff88007d4a6280: 00000002 00000000 00000000 0002 01 78149 50 | ffff88007d4a6f80: 00000002 00000000 00000000 0002 01 78148 51 | ffff88007d93d440: 00000002 00000000 00000000 0002 01 78147 52 | ffff88007d93d100: 00000002 00000000 00000000 0002 01 78146 53 | ffff88007d4b7b40: 00000002 00000000 00000000 0002 01 48283 54 | ffff880037b0b0c0: 00000002 00000000 00000000 0002 01 39554 55 | ffff88007d4b6e40: 00000002 00000000 00000000 0002 01 15153 56 | ffff88007d4b67c0: 00000002 00000000 00000000 0002 01 15059 57 | ffff88007d4b6b00: 00000002 00000000 00000000 0002 01 14942 58 | ffff88007d512ac0: 00000002 00000000 00000000 0002 01 11966 59 | ffff88007d513480: 00000002 00000000 00000000 0002 01 11792 60 | ffff88007d513140: 00000002 00000000 00000000 0002 01 11748 61 | ffff88007d5137c0: 00000002 00000000 00000000 0002 01 10906 62 | ffff880037b0a700: 00000002 00000000 00000000 0002 01 10722 63 | ffff880037b0ad80: 00000003 00000000 00000000 0002 01 8613 64 | ffff880037b0ba80: 00000003 00000000 00000000 0002 01 8612 65 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/wireless: -------------------------------------------------------------------------------- 1 | Inter-| sta-| Quality | Discarded packets | Missed | WE 2 | face | tus | link level noise | nwid crypt frag retry misc | beacon | 22 3 | -------------------------------------------------------------------------------- /sysadmin/proc/1/net/xfrm_stat: -------------------------------------------------------------------------------- 1 | XfrmInError 0 2 | XfrmInBufferError 0 3 | XfrmInHdrError 0 4 | XfrmInNoStates 0 5 | XfrmInStateProtoError 0 6 | XfrmInStateModeError 0 7 | XfrmInStateSeqError 0 8 | XfrmInStateExpired 0 9 | XfrmInStateMismatch 0 10 | XfrmInStateInvalid 0 11 | XfrmInTmplMismatch 0 12 | XfrmInNoPols 0 13 | XfrmInPolBlock 0 14 | XfrmInPolError 0 15 | XfrmOutError 0 16 | XfrmOutBundleGenError 0 17 | XfrmOutBundleCheckError 0 18 | XfrmOutNoStates 0 19 | XfrmOutStateProtoError 0 20 | XfrmOutStateModeError 0 21 | XfrmOutStateSeqError 0 22 | XfrmOutStateExpired 0 23 | XfrmOutPolBlock 0 24 | XfrmOutPolDead 0 25 | XfrmOutPolError 0 26 | -------------------------------------------------------------------------------- /sysadmin/proc/1/oom_adj: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/oom_score: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/oom_score_adj: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/sched: -------------------------------------------------------------------------------- 1 | init (1, #threads: 1) 2 | --------------------------------------------------------- 3 | se.exec_start : 920545795.712078 4 | se.vruntime : 106620050.311918 5 | se.sum_exec_runtime : 17398.236397 6 | se.wait_start : 0.000000 7 | se.sleep_start : 920545795.712078 8 | se.block_start : 0.000000 9 | se.sleep_max : 902714.685848 10 | se.block_max : 101.740753 11 | se.exec_max : 1.731289 12 | se.slice_max : 41.065140 13 | se.wait_max : 36.008010 14 | se.wait_sum : 13627.336169 15 | se.wait_count : 20612 16 | se.iowait_sum : 219.367453 17 | se.iowait_count : 65 18 | sched_info.bkl_count : 157 19 | se.nr_migrations : 0 20 | se.nr_migrations_cold : 0 21 | se.nr_failed_migrations_affine : 0 22 | se.nr_failed_migrations_running : 0 23 | se.nr_failed_migrations_hot : 0 24 | se.nr_forced_migrations : 0 25 | se.nr_wakeups : 20081 26 | se.nr_wakeups_sync : 829 27 | se.nr_wakeups_migrate : 0 28 | se.nr_wakeups_local : 20081 29 | se.nr_wakeups_remote : 0 30 | se.nr_wakeups_affine : 0 31 | se.nr_wakeups_affine_attempts : 0 32 | se.nr_wakeups_passive : 0 33 | se.nr_wakeups_idle : 0 34 | avg_atom : 0.848156 35 | avg_per_cpu : 0.000001 36 | nr_switches : 20513 37 | nr_voluntary_switches : 20082 38 | nr_involuntary_switches : 431 39 | se.load.weight : 1024 40 | policy : 0 41 | prio : 120 42 | clock-delta : 160 43 | -------------------------------------------------------------------------------- /sysadmin/proc/1/schedstat: -------------------------------------------------------------------------------- 1 | 17398236397 12091261834 20513 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/sessionid: -------------------------------------------------------------------------------- 1 | 4294967295 -------------------------------------------------------------------------------- /sysadmin/proc/1/stat: -------------------------------------------------------------------------------- 1 | 1 (init) S 0 1 1 0 -1 4202752 2415 1872198592 19 6815 72 1671 5837170 1262655 20 0 1 0 4 19693568 234 18446744073709551615 1 1 0 0 0 0 0 4096 536962595 18446744073709551615 0 0 0 0 0 0 24 0 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/statm: -------------------------------------------------------------------------------- 1 | 4808 234 165 35 0 72 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/status: -------------------------------------------------------------------------------- 1 | Name: init 2 | State: S (sleeping) 3 | Tgid: 1 4 | Pid: 1 5 | PPid: 0 6 | TracerPid: 0 7 | Uid: 0 0 0 0 8 | Gid: 0 0 0 0 9 | Utrace: 0 10 | FDSize: 64 11 | Groups: 12 | VmPeak: 19260 kB 13 | VmSize: 19232 kB 14 | VmLck: 0 kB 15 | VmHWM: 1500 kB 16 | VmRSS: 936 kB 17 | VmData: 200 kB 18 | VmStk: 88 kB 19 | VmExe: 140 kB 20 | VmLib: 2348 kB 21 | VmPTE: 56 kB 22 | VmSwap: 0 kB 23 | Threads: 1 24 | SigQ: 1/7436 25 | SigPnd: 0000000000000000 26 | ShdPnd: 0000000000000000 27 | SigBlk: 0000000000000000 28 | SigIgn: 0000000000001000 29 | SigCgt: 00000001a0016623 30 | CapInh: 0000000000000000 31 | CapPrm: ffffffffffffffff 32 | CapEff: fffffffffffffeff 33 | CapBnd: ffffffffffffffff 34 | Cpus_allowed: 1 35 | Cpus_allowed_list: 0 36 | Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001 37 | Mems_allowed_list: 0 38 | voluntary_ctxt_switches: 20084 39 | nonvoluntary_ctxt_switches: 434 40 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/attr/current: -------------------------------------------------------------------------------- 1 | system_u:system_r:init_t:s0 -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/attr/exec: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/task/1/attr/exec -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/attr/fscreate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/task/1/attr/fscreate -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/attr/keycreate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/task/1/attr/keycreate -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/attr/prev: -------------------------------------------------------------------------------- 1 | system_u:system_r:kernel_t:s0 -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/attr/sockcreate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/task/1/attr/sockcreate -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/cgroup: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/proc/1/task/1/cgroup -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/cmdline: -------------------------------------------------------------------------------- 1 | /sbin/init -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/comm: -------------------------------------------------------------------------------- 1 | init 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/cpuset: -------------------------------------------------------------------------------- 1 | / 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/loginuid: -------------------------------------------------------------------------------- 1 | 4294967295 -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/mountinfo: -------------------------------------------------------------------------------- 1 | 16 21 0:3 / /proc rw,relatime - proc proc rw 2 | 17 21 0:0 / /sys rw,relatime - sysfs sysfs rw,seclabel 3 | 18 23 0:5 / /dev rw,relatime - devtmpfs devtmpfs rw,seclabel,size=951920k,nr_inodes=237980,mode=755 4 | 19 18 0:11 / /dev/pts rw,relatime - devpts devpts rw,seclabel,gid=5,mode=620,ptmxmode=000 5 | 20 18 0:16 / /dev/shm rw,relatime - tmpfs tmpfs rw,seclabel 6 | 21 1 252:1 / / rw,relatime - ext4 /dev/vda1 rw,seclabel,barrier=1,data=ordered 7 | 22 21 0:14 / /selinux rw,relatime - selinuxfs none rw 8 | 23 21 0:5 / /dev rw,relatime - devtmpfs devtmpfs rw,seclabel,size=951920k,nr_inodes=237980,mode=755 9 | 24 16 0:15 / /proc/bus/usb rw,relatime - usbfs /proc/bus/usb rw 10 | 25 16 0:17 / /proc/sys/fs/binfmt_misc rw,relatime - binfmt_misc none rw 11 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/mounts: -------------------------------------------------------------------------------- 1 | rootfs / rootfs rw 0 0 2 | proc /proc proc rw,relatime 0 0 3 | sysfs /sys sysfs rw,seclabel,relatime 0 0 4 | devtmpfs /dev devtmpfs rw,seclabel,relatime,size=951920k,nr_inodes=237980,mode=755 0 0 5 | devpts /dev/pts devpts rw,seclabel,relatime,gid=5,mode=620,ptmxmode=000 0 0 6 | tmpfs /dev/shm tmpfs rw,seclabel,relatime 0 0 7 | /dev/vda1 / ext4 rw,seclabel,relatime,barrier=1,data=ordered 0 0 8 | none /selinux selinuxfs rw,relatime 0 0 9 | devtmpfs /dev devtmpfs rw,seclabel,relatime,size=951920k,nr_inodes=237980,mode=755 0 0 10 | /proc/bus/usb /proc/bus/usb usbfs rw,relatime 0 0 11 | none /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0 12 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/oom_adj: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/oom_score: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/oom_score_adj: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/sched: -------------------------------------------------------------------------------- 1 | init (1, #threads: 1) 2 | --------------------------------------------------------- 3 | se.exec_start : 920559375.708789 4 | se.vruntime : 106625758.021799 5 | se.sum_exec_runtime : 17539.188446 6 | se.wait_start : 0.000000 7 | se.sleep_start : 920559375.708789 8 | se.block_start : 0.000000 9 | se.sleep_max : 902714.685848 10 | se.block_max : 101.740753 11 | se.exec_max : 1.731289 12 | se.slice_max : 41.065140 13 | se.wait_max : 50.302601 14 | se.wait_sum : 13847.370665 15 | se.wait_count : 20627 16 | se.iowait_sum : 219.367453 17 | se.iowait_count : 65 18 | sched_info.bkl_count : 157 19 | se.nr_migrations : 0 20 | se.nr_migrations_cold : 0 21 | se.nr_failed_migrations_affine : 0 22 | se.nr_failed_migrations_running : 0 23 | se.nr_failed_migrations_hot : 0 24 | se.nr_forced_migrations : 0 25 | se.nr_wakeups : 20088 26 | se.nr_wakeups_sync : 829 27 | se.nr_wakeups_migrate : 0 28 | se.nr_wakeups_local : 20088 29 | se.nr_wakeups_remote : 0 30 | se.nr_wakeups_affine : 0 31 | se.nr_wakeups_affine_attempts : 0 32 | se.nr_wakeups_passive : 0 33 | se.nr_wakeups_idle : 0 34 | avg_atom : 0.854403 35 | avg_per_cpu : 0.000001 36 | nr_switches : 20528 37 | nr_voluntary_switches : 20089 38 | nr_involuntary_switches : 439 39 | se.load.weight : 1024 40 | policy : 0 41 | prio : 120 42 | clock-delta : 251 43 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/schedstat: -------------------------------------------------------------------------------- 1 | 17539188446 12091339351 20528 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/sessionid: -------------------------------------------------------------------------------- 1 | 4294967295 -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/stat: -------------------------------------------------------------------------------- 1 | 1 (init) S 0 1 1 0 -1 4202752 2415 1872217475 19 6815 71 1685 5837179 1262747 20 0 1 0 4 19693568 234 18446744073709551615 1 1 0 0 0 0 0 4096 536962595 18446744073709551615 0 0 0 0 0 0 24 0 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/statm: -------------------------------------------------------------------------------- 1 | 4808 234 165 35 0 72 0 2 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/status: -------------------------------------------------------------------------------- 1 | Name: init 2 | State: S (sleeping) 3 | Tgid: 1 4 | Pid: 1 5 | PPid: 0 6 | TracerPid: 0 7 | Uid: 0 0 0 0 8 | Gid: 0 0 0 0 9 | Utrace: 0 10 | FDSize: 64 11 | Groups: 12 | VmPeak: 19260 kB 13 | VmSize: 19232 kB 14 | VmLck: 0 kB 15 | VmHWM: 1500 kB 16 | VmRSS: 936 kB 17 | VmData: 200 kB 18 | VmStk: 88 kB 19 | VmExe: 140 kB 20 | VmLib: 2348 kB 21 | VmPTE: 56 kB 22 | VmSwap: 0 kB 23 | Threads: 1 24 | SigQ: 1/7436 25 | SigPnd: 0000000000000000 26 | ShdPnd: 0000000000000000 27 | SigBlk: 0000000000000000 28 | SigIgn: 0000000000001000 29 | SigCgt: 00000001a0016623 30 | CapInh: 0000000000000000 31 | CapPrm: ffffffffffffffff 32 | CapEff: fffffffffffffeff 33 | CapBnd: ffffffffffffffff 34 | Cpus_allowed: 1 35 | Cpus_allowed_list: 0 36 | Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001 37 | Mems_allowed_list: 0 38 | voluntary_ctxt_switches: 20091 39 | nonvoluntary_ctxt_switches: 441 40 | -------------------------------------------------------------------------------- /sysadmin/proc/1/task/1/wchan: -------------------------------------------------------------------------------- 1 | poll_schedule_timeout -------------------------------------------------------------------------------- /sysadmin/proc/1/wchan: -------------------------------------------------------------------------------- 1 | poll_schedule_timeout -------------------------------------------------------------------------------- /sysadmin/proc/diskstats: -------------------------------------------------------------------------------- 1 | 1 0 ram0 0 0 0 0 0 0 0 0 0 0 0 2 | 1 1 ram1 0 0 0 0 0 0 0 0 0 0 0 3 | 1 2 ram2 0 0 0 0 0 0 0 0 0 0 0 4 | 1 3 ram3 0 0 0 0 0 0 0 0 0 0 0 5 | 1 4 ram4 0 0 0 0 0 0 0 0 0 0 0 6 | 1 5 ram5 0 0 0 0 0 0 0 0 0 0 0 7 | 1 6 ram6 0 0 0 0 0 0 0 0 0 0 0 8 | 1 7 ram7 0 0 0 0 0 0 0 0 0 0 0 9 | 1 8 ram8 0 0 0 0 0 0 0 0 0 0 0 10 | 1 9 ram9 0 0 0 0 0 0 0 0 0 0 0 11 | 1 10 ram10 0 0 0 0 0 0 0 0 0 0 0 12 | 1 11 ram11 0 0 0 0 0 0 0 0 0 0 0 13 | 1 12 ram12 0 0 0 0 0 0 0 0 0 0 0 14 | 1 13 ram13 0 0 0 0 0 0 0 0 0 0 0 15 | 1 14 ram14 0 0 0 0 0 0 0 0 0 0 0 16 | 1 15 ram15 0 0 0 0 0 0 0 0 0 0 0 17 | 7 0 loop0 0 0 0 0 0 0 0 0 0 0 0 18 | 7 1 loop1 0 0 0 0 0 0 0 0 0 0 0 19 | 7 2 loop2 0 0 0 0 0 0 0 0 0 0 0 20 | 7 3 loop3 0 0 0 0 0 0 0 0 0 0 0 21 | 7 4 loop4 0 0 0 0 0 0 0 0 0 0 0 22 | 7 5 loop5 0 0 0 0 0 0 0 0 0 0 0 23 | 7 6 loop6 0 0 0 0 0 0 0 0 0 0 0 24 | 7 7 loop7 0 0 0 0 0 0 0 0 0 0 0 25 | 252 0 vda 144894 35104 10326034 730289 6944579 14318682 170063432 65002141 0 21069259 65726171 26 | 252 1 vda1 144734 35082 10324578 730269 6937613 14318682 170063432 64982276 0 21060178 65706293 27 | -------------------------------------------------------------------------------- /sysadmin/solutions.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from __future__ import print_function, unicode_literals 3 | import psutil 4 | import time 5 | import sys 6 | from subprocess import check_output 7 | import chardet 8 | 9 | 10 | def multiplatform_vmstat(count): 11 | """Get data in a multiplatform way 12 | """ 13 | cpu_percent, io_stat, io_stat_0 = 0, 0, 0 14 | print("cpu%", "iops(r+w)") 15 | for x in range(-count, 1): 16 | cpu_percent = psutil.cpu_percent() 17 | io_counters = psutil.disk_io_counters() 18 | read_io = io_counters.read_count 19 | write_io = io_counters.write_count 20 | io_stat = read_io + write_io 21 | print(cpu_percent, io_stat - io_stat_0) 22 | io_stat_0 = io_stat 23 | if x: 24 | time.sleep(10) 25 | 26 | 27 | def grep(needle, fpath): 28 | """A simple grep implementation 29 | 30 | goal: open() is iterable and doesn't 31 | need splitlines() 32 | goal: comprehension can filter lists 33 | """ 34 | return [x for x in open(fpath) if needle in x] 35 | 36 | 37 | def igrep(expr, iterable): 38 | return [x for x in iterable if expr in x] 39 | 40 | 41 | def sh(cmd, shell=False, timeout=0): 42 | """"Returns an iterable output of a command string 43 | checking... 44 | """ 45 | from sys import version_info as python_version 46 | if python_version < (3, 3): # ..before using.. 47 | if timeout: 48 | raise ValueError("Timeout not supported until Python 3.3") 49 | output = check_output(cmd.split(), shell=shell) 50 | else: 51 | output = check_output(cmd.split(), shell=shell, timeout=timeout) 52 | 53 | # In mac os x we can have some accents in output (sáb) 54 | return [x.decode(chardet.detect(x).get('encoding')) for x in output.splitlines()] 55 | 56 | 57 | def pgrep(program): 58 | # linux or mac 59 | if sys.platform.startswith('win'): 60 | return igrep(program, sh("tasklist")) 61 | else: 62 | return igrep(program, sh("ps -fe")) 63 | 64 | 65 | diskstats_headers = ('major minor device' 66 | ' reads reads_merged reads_sectors reads_ms' 67 | ' writes writes_merged writes_sectors writes_ms' 68 | ' io_in_progress io_ms_spent io_ms_weight').split() 69 | 70 | 71 | def linux_diskstats(disk): 72 | """Get I/O information from /proc/diskstats 73 | @param disk def sda 74 | goal: usage of time.sleep 75 | goal: usage of dict.setdefault 76 | goal: use string concatenation to increase readability 77 | goal: use *magic with print+sep, splitting and slicing 78 | """ 79 | from time import sleep 80 | info = ('reads reads_merged reads_sectors reads_ms' 81 | ' writes writes_merged writes_sectors writes_ms' 82 | ' io_in_progress io_ms_weight').split() 83 | print(*info, sep=",") 84 | old, cur = dict(), dict() 85 | while True: 86 | disk_l = grep(disk, "proc/diskstats") 87 | for x in disk_l: 88 | info = x.split() 89 | # the first 3 fields are disk informations 90 | part = info[2] 91 | old.setdefault(part, [0] * 11) 92 | cur[part] = map(int, info[3:]) 93 | delta = [x - y for x, y in zip(cur[part], old[part])] 94 | print(*delta, sep=",") 95 | old[part] = cur[part] 96 | sleep(1) 97 | 98 | 99 | def parse_line(line): 100 | import re 101 | _, _, hour, host, _, _, dest = line.split()[:7] 102 | try: 103 | dest = re.split(r'[<>]', dest)[1] 104 | except (IndexError, TypeError): 105 | dest = None 106 | return (hour, host, dest) 107 | 108 | if __name__ == "__main__": 109 | print(pgrep("firefox")) 110 | linux_diskstats("vda1") 111 | -------------------------------------------------------------------------------- /sysadmin/test.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import logging.config 3 | import yaml 4 | 5 | with open('logger.yml') as logger_config: 6 | logging.config.dictConfig(yaml.safe_load(logger_config)) 7 | 8 | # The ```os``` module goes to syslog 9 | syslog = logging.getLogger('os') 10 | syslog.critical("To syslog") -------------------------------------------------------------------------------- /sysadmin/windows.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ealogar/curso-python/8b2bffa1d6aac32b029f18e8769c98aac3d5395f/sysadmin/windows.txt --------------------------------------------------------------------------------