|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Assert:\n", |
| 8 | + "## Python provides the assert statement to check if a given logical expression is true or false. Program execution proceeds only if the expression is true and raises the AssertionError when it is false. The following code shows the usage of the assert statement." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 5, |
| 14 | + "metadata": {}, |
| 15 | + "outputs": [ |
| 16 | + { |
| 17 | + "ename": "AssertionError", |
| 18 | + "evalue": "", |
| 19 | + "output_type": "error", |
| 20 | + "traceback": [ |
| 21 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 22 | + "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", |
| 23 | + "\u001b[1;32m<ipython-input-5-5726816e126f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
| 24 | + "\u001b[1;31mAssertionError\u001b[0m: " |
| 25 | + ] |
| 26 | + } |
| 27 | + ], |
| 28 | + "source": [ |
| 29 | + "num = -6\n", |
| 30 | + "assert num > 0" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": 9, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [ |
| 38 | + { |
| 39 | + "ename": "AssertionError", |
| 40 | + "evalue": "", |
| 41 | + "output_type": "error", |
| 42 | + "traceback": [ |
| 43 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 44 | + "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", |
| 45 | + "\u001b[1;32m<ipython-input-9-3c372f62eb3c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0meggs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'goodbye'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mbacon\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'GOODbye'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[0meggs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mbacon\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'The strings are not same'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 46 | + "\u001b[1;31mAssertionError\u001b[0m: " |
| 47 | + ] |
| 48 | + } |
| 49 | + ], |
| 50 | + "source": [ |
| 51 | + "eggs = 'goodbye'\n", |
| 52 | + "bacon = 'GOODbye'\n", |
| 53 | + "assert eggs.lower() != bacon.lower()\n", |
| 54 | + "print('The strings are not same')\n" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 11, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [ |
| 62 | + { |
| 63 | + "ename": "AssertionError", |
| 64 | + "evalue": "", |
| 65 | + "output_type": "error", |
| 66 | + "traceback": [ |
| 67 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 68 | + "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", |
| 69 | + "\u001b[1;32m<ipython-input-11-a871fdc9ebee>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
| 70 | + "\u001b[1;31mAssertionError\u001b[0m: " |
| 71 | + ] |
| 72 | + } |
| 73 | + ], |
| 74 | + "source": [ |
| 75 | + "assert False" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 22, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [ |
| 83 | + { |
| 84 | + "name": "stderr", |
| 85 | + "output_type": "stream", |
| 86 | + "text": [ |
| 87 | + " 2021-03-16 20:19:10,472 - DEBUG - Akash Borgalli's Logger\n" |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "source": [ |
| 92 | + "## 1st line to call logging.debug()\n", |
| 93 | + "import logging as logger\n", |
| 94 | + "## 2nd line to call logging.debug()\n", |
| 95 | + "logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')\n", |
| 96 | + "## calling the debug logger\n", |
| 97 | + "logger.debug(\"Akash Borgalli's Logger\")" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": null, |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [], |
| 105 | + "source": [ |
| 106 | + "logging.basicConfig(filename='programLOG.txt', level=logging.DEBUG,\n", |
| 107 | + "format=' %(asctime)s - %(levelname)s - %(message)s')\n", |
| 108 | + "logger.debug(\"Message stored in programLog file\")" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "markdown", |
| 113 | + "metadata": {}, |
| 114 | + "source": [ |
| 115 | + "## The five levels of logging are INFO, DEBUG, WARNING, ERROR, CRITICAL" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": 23, |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [ |
| 124 | + "## To disable logging\n", |
| 125 | + "logger.disable(logging.DEBUG)" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "markdown", |
| 130 | + "metadata": {}, |
| 131 | + "source": [ |
| 132 | + "## Logging messages are better because it gives timestamp also it has different types of logging levels apart from that you can store these logs into .txt file or db based on your requirement and it looks pretty with colors." |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "## This buttons are present in pycharm\n", |
| 140 | + "- The Over button will quickly execute the function call without stepping into it. \n", |
| 141 | + "- The Step In button will move the debugger into a function call. \n", |
| 142 | + "- The Out button will quickly execute the rest of the code until it steps out of the function it currently is in." |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "markdown", |
| 147 | + "metadata": {}, |
| 148 | + "source": [ |
| 149 | + "## Yes, It will Stop" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "markdown", |
| 154 | + "metadata": {}, |
| 155 | + "source": [ |
| 156 | + "## Breakpoint is something that will take a pause at a particular line of code when executing meaning debugging" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "markdown", |
| 161 | + "metadata": {}, |
| 162 | + "source": [ |
| 163 | + "## In Mu, right-click the line -> select Set Breakpoint from the context menu" |
| 164 | + ] |
| 165 | + } |
| 166 | + ], |
| 167 | + "metadata": { |
| 168 | + "kernelspec": { |
| 169 | + "display_name": "Python 3", |
| 170 | + "language": "python", |
| 171 | + "name": "python3" |
| 172 | + }, |
| 173 | + "language_info": { |
| 174 | + "codemirror_mode": { |
| 175 | + "name": "ipython", |
| 176 | + "version": 3 |
| 177 | + }, |
| 178 | + "file_extension": ".py", |
| 179 | + "mimetype": "text/x-python", |
| 180 | + "name": "python", |
| 181 | + "nbconvert_exporter": "python", |
| 182 | + "pygments_lexer": "ipython3", |
| 183 | + "version": "3.8.5" |
| 184 | + } |
| 185 | + }, |
| 186 | + "nbformat": 4, |
| 187 | + "nbformat_minor": 4 |
| 188 | +} |
0 commit comments