Python. урок 11. работа с исключениями

Exceptions in Python

Python has many built-in exceptions that are raised when your program encounters an error (something in the program goes wrong).

When these exceptions occur, the Python interpreter stops the current process and passes it to the calling process until it is handled. If not handled, the program will crash.

For example, let us consider a program where we have a function that calls function , which in turn calls function . If an exception occurs in function but is not handled in , the exception passes to and then to .

If never handled, an error message is displayed and our program comes to a sudden unexpected halt.

Аутентификация

Аутенфикация помогает сервисам понять кто вы. Как правило, вы предоставляете свои учетные данные на сервер, передавая данные через заголовок авторизации или пользовательский заголовок, определенный службой.
Все функции запроса, которые вы видели до этого момента, предоставляют параметр , который позволяет передавать вам свои учетные данные.

Одним из примеров API, которые требует аутентификации, является GitHub’s Authenticated User API. Это конечная точка предоставляет информацию о профиле аутентифицированного пользователя. Чтобы сделать запрос к Authenticated User API, вы можете передать свое имя пользователя и пароль в кортеже :

Запрос будет успешно выполнен, если учетные данные, которые вы передали в кортеже верны. Если вы попробуете сделать запрос не указав учетные данные, то получите в ответ от сервера код состояния .

Когда вы передаете имя пользователя и пароль в кортеже параметру , применяет учетные данные с использованием базовой схемы аутентификации доступа HTTP.
Таким образом, вы можете сделать тот же запрос, передав учетные данные, используя :

Вам не обязательно использовать , вы можете аутентифицироваться и другим способом. из коробки предоставляют и другие методы аутентификации, такие как и .
Вы даже можете использовать свой собственный механизм аутентификации. Для этого, вы должны создать подкласс , затем внедрить :

Здесь ваш пользовательский механизм получает токен, а затем включает этот токен в заголовок вашего запроса.

Плохие механизмы проверки подлинности могут привести к уязвимостям в безопасности. Поэтому, если вашему сервису, по какой-либо причине не требуется настраиваемый механизм проверки подлинности, то лучше использовать готовые схемы проверки подлинности, такие как или .

Пока вы думаете о безопасности, давайте рассмотрим работу с SSL-сертификатами в библиотеке .

Raising Exceptions

We can use raise keyword to throw an exception from our code. Some of the possible scenarios are:

  • Function input parameters validation fails
  • Catching an exception and then throwing a custom exception
class ValidationError(Exception):
    pass


def divide(x, y):
    try:
        if type(x) is not int:
            raise TypeError("Unsupported type")
        if type(y) is not int:
            raise TypeError("Unsupported type")
    except TypeError as e:
        print(e)
        raise ValidationError("Invalid type of arguments")

    if y is 0:
        raise ValidationError("We can't divide by 0.")


try:
    divide(10, 0)
except ValidationError as ve:
    print(ve)

try:
    divide(10, "5")
except ValidationError as ve:
    print(ve)

Output:

We can't divide by 0.
Unsupported type
Invalid type of arguments

Задания для самоподготовки

1. Напишите
программу ввода натуральных чисел через запятую и преобразования этой строки в
список целых чисел. (Используйте здесь функцию map для
преобразования элементов последовательности строк в последовательность чисел).
Реализовать обработку возможных исключений при таком преобразовании.

2. Написать
функцию вычисления среднего арифметического элементов переданного ей списка.
Реализовать обработку возможных исключений при ее работе.

3. Написать
функцию-генератор (с использованием оператора yield) для удаления
произвольного элемента из множества (с помощью метода pop()). Функция
должна возвращать значение удаленного элемента. Реализовать обработку возможных
исключений при ее работе.

Видео по теме

Python 3 #1: установка и запуск интерпретатора языка

Python 3 #2: переменные, оператор присваивания, типы данных

Python 3 #3: функции input и print ввода/вывода

Python 3 #4: арифметические операторы: сложение, вычитание, умножение, деление, степень

Python 3 #5: условный оператор if, составные условия с and, or, not

Python 3 #6: операторы циклов while и for, операторы break и continue

Python 3 #7: строки — сравнения, срезы строк, базовые функции str, len, ord, in

Python 3 #8: методы строк — upper, split, join, find, strip, isalpha, isdigit и другие

Python 3 #9: списки list и функции len, min, max, sum, sorted

Python 3 #10: списки — срезы и методы: append, insert, pop, sort, index, count, reverse, clear

Python 3 #11: списки — инструмент list comprehensions, сортировка методом выбора

Python 3 #12: словарь, методы словарей: len, clear, get, setdefault, pop

Python 3 #13: кортежи (tuple) и операции с ними: len, del, count, index

Python 3 #14: функции (def) — объявление и вызов

Python 3 #15: делаем «Сапер», проектирование программ «сверху-вниз»

Python 3 #16: рекурсивные и лямбда-функции, функции с произвольным числом аргументов

Python 3 #17: алгоритм Евклида, принцип тестирования программ

Python 3 #18: области видимости переменных — global, nonlocal

Python 3 #19: множества (set) и операции над ними: вычитание, пересечение, объединение, сравнение

Python 3 #20: итераторы, выражения-генераторы, функции-генераторы, оператор yield

Python 3 #21: функции map, filter, zip

Python 3 #22: сортировка sort() и sorted(), сортировка по ключам

Python 3 #23: обработка исключений: try, except, finally, else

Python 3 #24: файлы — чтение и запись: open, read, write, seek, readline, dump, load, pickle

Python 3 #25: форматирование строк: метод format и F-строки

Python 3 #26: создание и импорт модулей — import, from, as, dir, reload

Python 3 #27: пакеты (package) — создание, импорт, установка (менеджер pip)

Python 3 #28: декораторы функций и замыкания

Python 3 #29: установка и порядок работы в PyCharm

Python 3 #30: функция enumerate, примеры использования

Перехват исключений в Python

В Python исключения обрабатываются при помощи инструкции .

Критическая операция, которая может вызвать исключение, помещается внутрь блока . А код, при помощи которого это исключение будет обработано, — внутрь блока .

Таким образом, мы можем выбрать набор операций, который мы хотим совершить при перехвате исключения. Вот простой пример.

# Для получения типа исключения импортируем модуль sys
import sys

randomList = 

for entry in randomList:
    try:
        print("The entry is", entry)
        r = 1/int(entry)
        break
    except:
        print("Oops!", sys.exc_info(), "occurred.")
        print("Next entry.")
        print()
print("The reciprocal of", entry, "is", r)

Результат:

В данном примере мы, при помощи цикла , производим итерацию списка . Как мы ранее заметили, часть кода, в которой может произойти ошибка, помещена внутри блока .

Если исключений не возникает, блок пропускается, а программа продолжает выполнятся обычным образом (в данном примере так происходит с последним элементом списка). Но если исключение появляется, оно сразу обрабатывается в блоке (в данном примере так происходит с первым и вторым элементом списка).

В нашем примере мы вывели на экран имя исключения при помощи функции , которую импортировали из модуля . Можно заметить, что элемент вызывает , а  вызывает .

Так как все исключения в Python наследуются из базового класса , мы можем переписать наш код следующим образом:

# Для получения типа исключения импортируем модуль sys
import sys

randomList = 

for entry in randomList:
    try:
        print("The entry is", entry)
        r = 1/int(entry)
        break
    except Exception as e:
        print("Oops!", e.__class__, "occurred.")
        print("Next entry.")
        print()
print("The reciprocal of", entry, "is", r)

Результат выполнения этого кода будет точно таким же.

Raise Exception with Arguments

What is Raise?

We can forcefully raise an exception using the raise keyword.

We can also optionally pass values to the exception and specify why it has occurred.

Raise Syntax

Here is the syntax for calling the “raise” method.

raise ]]

Where,

  • Under the “Exception” – specify its name.
  • The “args” is optional and represents the value of the exception argument.
  • The final argument, “traceback,” is also optional and if present, is the traceback object used for the exception.

Let’s take an example to clarify this.

Raise Example

>>> raise MemoryError
Traceback (most recent call last):
...
MemoryError
 
>>> raise MemoryError("This is an argument")
Traceback (most recent call last):
...
MemoryError: This is an argument
 
 
>>> try:
      a = int(input("Enter a positive integer value: "))
     if a <= 0:
            raise ValueError("This is not a positive number!!")
    except ValueError as ve:
      print(ve)
 
 
Following Output is displayed if we enter a negative number:
  
Enter a positive integer: –5
 
This is not a positive number!!

Warnings¶

The following exceptions are used as warning categories; see the
documentation for more details.

exception

Base class for warning categories.

exception

Base class for warnings generated by user code.

exception

Base class for warnings about deprecated features when those warnings are
intended for other Python developers.

Ignored by the default warning filters, except in the module
(PEP 565). Enabling the shows
this warning.

exception

Base class for warnings about features which are obsolete and
expected to be deprecated in the future, but are not deprecated
at the moment.

This class is rarely used as emitting a warning about a possible
upcoming deprecation is unusual, and
is preferred for already active deprecations.

Ignored by the default warning filters. Enabling the shows this warning.

exception

Base class for warnings about dubious syntax.

exception

Base class for warnings about dubious runtime behavior.

exception

Base class for warnings about deprecated features when those warnings are
intended for end users of applications that are written in Python.

exception

Base class for warnings about probable mistakes in module imports.

Ignored by the default warning filters. Enabling the shows this warning.

exception

Base class for warnings related to Unicode.

exception

Base class for warnings related to and .

7.1.4. Подходы к обработке ошибок¶

При написании кода необходимо предусматривать, что в определенном месте программы может возникнуть ошибка и дополнять код на случай ее возникновения.

Существует два ключевых подхода программирования реакции на возможные ошибки:

  1. «Семь раз отмерь, один раз отрежь» — LBYL (англ. Look Before You Leap);

  2. «Легче попросить прощения, чем разрешения» — EAFP (англ. «It’s Easier To Ask Forgiveness Than Permission»).

В Листинге 7.1.2 приведен пример сравнения двух подходов.

Листинг 7.1.2 — Псевдокод функций, использующих разные подходы к обработке ошибок: «Семь раз отмерь, один раз отрежь» и «Легче попросить прощения, чем разрешения»

Обе функции возвращают решение линейного уравнения и НИЧЕГО, если 'a' = 


ФУНКЦИЯ найти_корень_1(a, b):
    ЕСЛИ a не равно 
        ВЕРНУТЬ -b  a
    ИНАЧЕ
        ВЕРНУТЬ НИЧЕГО


ФУНКЦИЯ найти_корень_2(a, b):
    ОПАСНЫЙ БЛОК КОДА      # Внутри данного блока пишется код, который
        ВЕРНУТЬ -b  a     # потенциально может привести к ошибкам
    ЕСЛИ ПРОИЗОШЛА ОШИБКА  # В случае деления на 0 попадаем сюда
        ВЕРНУТЬ НИЧЕГО

Подход «Семь раз отмерь, один раз отрежь» имеет определенные минусы:

  • проверки могут уменьшить читаемость и ясность основного кода;

  • код проверки может дублировать значительную часть работы, осуществляемой основным кодом;

  • разработчик может легко допустить ошибку, забыв какую-либо из проверок;

  • ситуация может изменится между моментом проверки и моментом выполнения операции.

Метод read_very_eager¶

Или использовать еще один метод для чтения read_very_eager.
При использовании метода read_very_eager, можно отправить несколько
команд, а затем считать весь доступный вывод:

In : telnet.write(b'sh arp\n')

In : telnet.write(b'sh clock\n')

In : telnet.write(b'sh ip int br\n')

In : all_result = telnet.read_very_eager().decode('utf-8')

In : print(all_result)
sh arp
Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  10.30.0.1               -   aabb.cc00.6530  ARPA   Ethernet0/3.300
Internet  10.100.0.1              -   aabb.cc00.6530  ARPA   Ethernet0/3.100
Internet  10.200.0.1              -   aabb.cc00.6530  ARPA   Ethernet0/3.200
Internet  19.1.1.1                -   aabb.cc00.6520  ARPA   Ethernet0/2
Internet  192.168.100.1           -   aabb.cc00.6500  ARPA   Ethernet0/0
Internet  192.168.100.2         124   aabb.cc00.6600  ARPA   Ethernet0/0
Internet  192.168.100.3         143   aabb.cc00.6700  ARPA   Ethernet0/0
Internet  192.168.100.100       160   aabb.cc80.c900  ARPA   Ethernet0/0
Internet  192.168.200.1           -   0203.e800.6510  ARPA   Ethernet0/1
Internet  192.168.200.100        13   0800.27ac.16db  ARPA   Ethernet0/1
Internet  192.168.230.1           -   aabb.cc00.6530  ARPA   Ethernet0/3
R1>sh clock
*19:18:57.980 UTC Fri Nov 3 2017
R1>sh ip int br
Interface                  IP-Address      OK? Method Status                Protocol
Ethernet0/0                192.168.100.1   YES NVRAM  up                    up
Ethernet0/1                192.168.200.1   YES NVRAM  up                    up
Ethernet0/2                19.1.1.1        YES NVRAM  up                    up
Ethernet0/3                192.168.230.1   YES NVRAM  up                    up
Ethernet0/3.100            10.100.0.1      YES NVRAM  up                    up
Ethernet0/3.200            10.200.0.1      YES NVRAM  up                    up
Ethernet0/3.300            10.30.0.1       YES NVRAM  up                    up
R1>

Предупреждение

Перед методом read_very_eager всегда надо ставить time.sleep(n).

С read_until будет немного другой подход. Можно выполнить те же три
команды, но затем получать вывод по одной за счет чтения до строки с
приглашением:

The Standard Exception Hierarchy

Behold the standard exception hierarchy. It is defined in the new
standard library module exceptions.py. Exceptions that were new since
Python 1.5 are marked with (*).

The root class for all exceptions is the new exception Exception.
From this, two additional classes are derived, StandardError, which is
the root class for all standard exceptions, and SystemExit. It is
recommended that user-defined exceptions in new code be derived from
Exception, although for backward compatibility reasons, this is not
required. Eventually this rule will be tightened.

SystemExit is derived from Exception because while it is an
exception, it is not an error.

Most standard exceptions are direct descendants of StandardError.
Some related exceptions are grouped together using an intermediate
class derived from StandardError; this makes it possible to catch
several different exceptions in one except clause, without using the
tuple notation.

We looked into introducing more groups of related exceptions,
but couldn’t decide on the best grouping. In a language as dynamic as
Python, it’s hard to say whether TypeError is a «program error», a
«runtime error» or an «environmental error», so we decided to leave it
undecided. It could be argued that NameError and AttributeError
should be derived from LookupError, but this is questionable and
depends entirely on the application.

Exception Class Definitions

The Python class definitions for the standard exceptions are
imported from the standard module «exceptions». You can’t change this
file thinking that the changes will automatically show up in the
standard exceptions; the builtin module expects the current hierarchy
as defined in exceptions.py.

Details on the standard exception classes are available in the
Python library reference manual’s entry for the

exceptions module.

try-except

Lets take do a real world example of the try-except block.

The program asks for numeric user input. Instead the user types characters in the input box. The program normally would crash. But with a try-except block it can be handled properly.

The try except statement prevents the program from crashing and properly deals with it.

123456
try:    x = input("Enter number: ")    x = x + 1    print(x)except:    print("Invalid input")

Entering invalid input, makes the program continue normally:

The try except statement can be extended with the finally keyword, this will be executed if no exception is thrown:

12
finally:    print("Valid input.")

The program continues execution if no exception has been thrown.

There are different kinds of exceptions: ZeroDivisionError, NameError, TypeError and so on. Sometimes modules define their own exceptions.

The try-except block works for function calls too:

123456789
def fail():    1 / try:    fail()except:    print('Exception occured')print('Program continues')

This outputs:

If you are a beginner, then I highly recommend this book.

Summing Up

After seeing the difference between syntax errors and exceptions, you learned about various ways to raise, catch, and handle exceptions in Python. In this article, you saw the following options:

  • allows you to throw an exception at any time.
  • enables you to verify if a certain condition is met and throw an exception if it isn’t.
  • In the clause, all statements are executed until an exception is encountered.
  • is used to catch and handle the exception(s) that are encountered in the try clause.
  • lets you code sections that should run only when no exceptions are encountered in the try clause.
  • enables you to execute sections of code that should always run, with or without any previously encountered exceptions.

Free PDF Download: Python 3 Cheat Sheet

Handling Multiple Exceptions with Except

We can define multiple exceptions with the same except clause. It means that if the Python interpreter finds a matching exception, then it’ll execute the code written under the except clause.

In short, when we define except clause in this way, we expect the same piece of code to throw different exceptions. Also, we want to take the same action in each case.

Please refer to the below example.

Example

try:
   You do your operations here;
   ......................
except(Exception1]]):
   If there is any exception from the given exception list,
   then execute this block.
   ......................
else:
   If there is no exception then execute this block

С использованием else

В некоторых ситуациях вам может потребоваться запустить определенный блок кода, если внутри try выполняется без ошибок. В этих случаях вы можете использовать необязательное ключевое слово else с оператором try.

Примечание: Исключения в предложении else не обрабатываются предыдущими предложениями except.

Давайте посмотрим на пример:

# program to print the reciprocal of even numbers

try:
    num = int(input("Enter a number: "))
    assert num % 2 == 0
except:
    print("Not an even number!")
else:
    reciprocal = 1/num
    print(reciprocal)

Выход

Если передать нечетное число:

Enter a number: 1
Not an even number!

Если мы передаем четное число, вычисляется и отображается обратная величина.

Enter a number: 4
0.25

Однако, если мы передадим 0, мы получим ZeroDivisionError, поскольку блок кода внутри else не обрабатывается предшествующим исключением.

Enter a number: 0
Traceback (most recent call last):
  File "<string>", line 7, in <module>
    reciprocal = 1/num
ZeroDivisionError: division by zero

«Голое» исключение

Есть еще один способ поймать ошибку:

Python

try:
1 / 0
except:
print(«You cannot divide by zero!»)

1
2
3
4

try

1

except

print(«You cannot divide by zero!»)

Но мы его не рекомендуем. На жаргоне Пайтона, это известно как голое исключение, что означает, что будут найдены вообще все исключения. Причина, по которой так делать не рекомендуется, заключается в том, что вы не узнаете, что именно за исключение вы выловите. Когда у вас возникло что-то в духе ZeroDivisionError, вы хотите выявить фрагмент, в котором происходит деление на ноль. В коде, написанном выше, вы не можете указать, что именно вам нужно выявить. Давайте взглянем еще на несколько примеров:

Python

my_dict = {«a»:1, «b»:2, «c»:3}

try:
value = my_dict
except KeyError:
print(«That key does not exist!»)

1
2
3
4
5
6

my_dict={«a»1,»b»2,»c»3}

try

value=my_dict»d»

exceptKeyError

print(«That key does not exist!»)

Python

my_list =

try:
my_list
except IndexError:
print(«That index is not in the list!»)

1
2
3
4
5
6

my_list=1,2,3,4,5

try

my_list6

exceptIndexError

print(«That index is not in the list!»)

В первом примере, мы создали словарь из трех элементов. После этого, мы попытались открыть доступ ключу, которого в словаре нет. Так как ключ не в словаре, возникает KeyError, которую мы выявили. Второй пример показывает список, длина которого состоит из пяти объектов. Мы попытались взять седьмой объект из индекса.

Помните, что списки в Пайтоне начинаются с нуля, так что когда вы говорите 6, вы запрашиваете 7. В любом случае, в нашем списке только пять объектов, по этой причине возникает IndexError, которую мы выявили. Вы также можете выявить несколько ошибок за раз при помощи одного оператора. Для этого существует несколько различных способов. Давайте посмотрим:

Python

my_dict = {«a»:1, «b»:2, «c»:3}

try:
value = my_dict
except IndexError:
print(«This index does not exist!»)
except KeyError:
print(«This key is not in the dictionary!»)
except:
print(«Some other error occurred!»)

1
2
3
4
5
6
7
8
9
10

my_dict={«a»1,»b»2,»c»3}

try

value=my_dict»d»

exceptIndexError

print(«This index does not exist!»)

exceptKeyError

print(«This key is not in the dictionary!»)

except

print(«Some other error occurred!»)

Это самый стандартный способ выявить несколько исключений. Сначала мы попробовали открыть доступ к несуществующему ключу, которого нет в нашем словаре. При помощи try/except мы проверили код на наличие ошибки KeyError, которая находится во втором операторе except

Обратите внимание на то, что в конце кода у нас появилась «голое» исключение. Обычно, это не рекомендуется, но вы, возможно, будете сталкиваться с этим время от времени, так что лучше быть проинформированным об этом

Кстати, также обратите внимание на то, что вам не нужно использовать целый блок кода для обработки нескольких исключений. Обычно, целый блок используется для выявления одного единственного исключения. Изучим второй способ выявления нескольких исключений:

Python

try:
value = my_dict
except (IndexError, KeyError):
print(«An IndexError or KeyError occurred!»)

1
2
3
4

try

value=my_dict»d»

except(IndexError,KeyError)

print(«An IndexError or KeyError occurred!»)

Обратите внимание на то, что в данном примере мы помещаем ошибки, которые мы хотим выявить, внутри круглых скобок. Проблема данного метода в том, что трудно сказать какая именно ошибка произошла, так что предыдущий пример, мы рекомендуем больше чем этот

Зачастую, когда происходит ошибка, вам нужно уведомить пользователя, при помощи сообщения.

В зависимости от сложности данной ошибки, вам может понадобиться выйти из программы. Иногда вам может понадобиться выполнить очистку, перед выходом из программы. Например, если вы открыли соединение с базой данных, вам нужно будет закрыть его, перед выходом из программы, или вы можете закончить с открытым соединением. Другой пример – закрытие дескриптора файла, к которому вы обращаетесь. Теперь нам нужно научиться убирать за собой. Это очень просто, если использовать оператор finally.

5.1. Base classes¶

The following exceptions are used mostly as base classes for other exceptions.

exception

The base class for all built-in exceptions. It is not meant to be directly
inherited by user-defined classes (for that, use ). If
is called on an instance of this class, the representation of
the argument(s) to the instance are returned, or the empty string when
there were no arguments.

The tuple of arguments given to the exception constructor. Some built-in
exceptions (like ) expect a certain number of arguments and
assign a special meaning to the elements of this tuple, while others are
usually called only with a single string giving an error message.

(tb)

This method sets tb as the new traceback for the exception and returns
the exception object. It is usually used in exception handling code like
this:

try
    ...
except SomeException
    tb = sys.exc_info()[2
    raise OtherException(...).with_traceback(tb)
exception

All built-in, non-system-exiting exceptions are derived from this class. All
user-defined exceptions should also be derived from this class.

exception

The base class for those built-in exceptions that are raised for various
arithmetic errors: , ,
.

exception

Raised when a related operation cannot be
performed.

Python hierarchy of exceptions

The exceptions are organized in a hierarchy, being
the parent of all exceptions.

interrupt.py

#!/usr/bin/env python

# interrupt.py

try:
    while True:
       pass

except KeyboardInterrupt:

    print("Program interrupted")

The script starts and endless cycle. If we press Ctrl+C,
we interrupt the cycle. Here, we caught the
exception.

Exception
  BaseException
    KeyboardInterrupt

This is the hierarchy of the exception.

interrupt2.py

#!/usr/bin/env python

# interrupt2.py

try:
    while True:
        pass

except BaseException:

    print("Program interrupted")

This example works too. The also catches the
keyboard interruption; among other exceptions. This is not a good practice, however.
We should catch specific exceptions in our clauses.

What Could Break?

The new design does its very best not to break old code, but there
are some cases where it wasn’t worth compromising the new semantics in
order to avoid breaking code. In other words, some old code may
break. That’s why the -X switch is there; however this shouldn’t be
an excuse for not fixing your code.

There are two kinds of breakage: sometimes, code will print
slightly funny error messages when it catches a class exception but
expects a string exception. And sometimes, but much less often, code
will actually crash or otherwise do the wrong thing in its error
handling.

Non-fatal Breakage

An examples of the first kind of breakage is code that attempts to
print the exception name, e.g.

modulename.classname

Fatal Breakage

More serious is breaking error handling code. This usually happens
because the error handling code expects the exception or the value
associated with the exception to have a particular type (usually
string or tuple). With the new scheme, the type is a class and the
value is a class instance. For example, the following code will
break:

Another example involves code that assumes too much about the type
of the value associated with the exception. For example:

Again, the remedy is to just go ahead and try the tuple unpack, and
if it fails, use the fallback strategy:

Note that the second try-except statement does not specify the
exception to catch — this is because with string exceptions, the
exception raised is «TypeError: unpack non-tuple», while with class
exceptions it is «ValueError: unpack sequence of wrong size». This
is because a string is a sequence; we must assume that error messages
are always more than two characters long!

(An alternative approach would be to use try-except to test for the
presence of the errno attribute; in the future, this would make sense,
but at the present time it would require even more code in order to be
compatible with string exceptions.)

Some Built-In Warning Classes

The Warning class is the base class for all the warnings. It has the following sub-classes.

  • BytesWarning – bytes, and buffer related warnings, mostly related to string conversion and comparison.
  • DeprecationWarning – warning about deprecated features
  • FutureWarning – base class for warning about constructs that will change semantically in the future.
  • ImportWarning – warning about mistakes in module imports
  • PendingDeprecationWarning – warning about features that will be deprecated in future.
  • ResourceWarning – resource usage warnings
  • RuntimeWarning – warnings about dubious runtime behavior.
  • SyntaxWarning – warning about dubious syntax
  • UnicodeWarning – Unicode conversion-related warnings
  • UserWarning – warnings generated by the user code

Built-in Exceptions

The table below shows built-in exceptions that are usually raised in Python:

Exception Description
ArithmeticError Raised when an error occurs in numeric calculations
AssertionError Raised when an assert statement fails
AttributeError Raised when attribute reference or assignment fails
Exception Base class for all exceptions
EOFError Raised when
the input() method hits an «end of file» condition (EOF)
FloatingPointError Raised when a floating point calculation fails
GeneratorExit Raised when a generator is closed (with the close() method)
ImportError Raised when an imported module does not exist
IndentationError Raised when indendation is not correct
IndexError Raised when an index of a sequence does not exist
KeyError Raised when a key does not exist in a dictionary
KeyboardInterrupt Raised when the user presses Ctrl+c,
Ctrl+z or Delete
LookupError Raised when errors raised cant be found
MemoryError Raised when a program runs out of memory
NameError Raised when a variable does not exist
NotImplementedError Raised when an abstract method requires an inherited class to override the
method
OSError Raised when a system related operation causes an error
OverflowError Raised when the result of a numeric calculation is too large
ReferenceError Raised when a weak reference object does not exist
RuntimeError Raised when an error occurs that do not belong to any specific expections
StopIteration Raised when the next() method of an iterator has no further values
SyntaxError Raised when a syntax error occurs
TabError Raised when indentation consists of tabs or spaces
SystemError Raised when a system error occurs
SystemExit Raised when the sys.exit() function is called
TypeError Raised when two different types are combined
UnboundLocalError Raised when a local variable is referenced before assignment
UnicodeError Raised when a unicode problem occurs
UnicodeEncodeError Raised when a unicode encoding problem occurs
UnicodeDecodeError Raised when a unicode decoding problem occurs
UnicodeTranslateError Raised when a unicode translation problem occurs
ValueError Raised when there is a wrong value in a specified data type
ZeroDivisionError Raised when the second operator in a division is zero

❮ Previous
Next ❯

Объект Response

Response — это объект для проверки результатов запроса.

Давайте сделаем тот же запрос, но на этот раз сохраним его в переменную, чтобы мы могли более подробно изучить его атрибуты и поведение:

В этом примере вы захватили значение, возвращаемое значение , которое является экземпляром Response, и сохранили его в переменной response. Название переменной может быть любым.

Код ответа HTTP

Первый кусок данных, который можно получить из ответа — код состояния (он же код ответа HTTP). Код ответа информирует вас о состоянии запроса.

Например, статус означает, что ваш запрос был успешно выполнен, а статус означает, что ресурс не найден. Есть множество других ответов сервера, которые могут дать вам информацию о том, что произошло с вашим запросом.

Используя вы можете увидеть статус, который вернул вам в ответ сервер:

вернул 200 — это значит, что запрос успешно выполнен и сервер отдал вам запрашиваемые данные.

Иногда эту информацию можно использовать в коде для принятия решений:

Если сервер возвращает 200, то программа выведет , если код ответа 400, то программа выведет .

Requests делает еще один шаг к тому, чтобы сделать это проще. Если вы используете экземпляр Response в условном выражении, то он получит значение , если код ответа между 200 и 400, и False во всех остальных случаях.

Поэтому вы можете сделать проще последний пример, переписав :

Помните, что этот метод не проверяет, что код состояния равен 200.
Причиной этого является то, что ответы с кодом в диапазоне от 200 до 400, такие как и , тоже считаются истинными, так как они дают некоторый обрабатываемый ответ.

Например, статус 204 говорит о том, что запрос был успешным, но в теле ответа нет содержимого.

Поэтому убедитесь, что вы используете этот сокращенный вид записи, только если хотите узнать был ли запрос успешен в целом. А затем обработать код состояния соответствующим образом.

Если вы не хотите проверять код ответа сервера в операторе , то вместо этого вы можете вызвать исключение, если запрос был неудачным. Это можно сделать вызвав :

Если вы используете , то HTTPError сработает только для определенных кодов состояния. Если состояние укажет на успешный запрос, то исключение не будет вызвано и программа продолжит свою работу.

Теперь вы знаете многое о том, что делать с кодом ответа от сервера. Но когда вы делаете GET-запрос, вы редко заботитесь только об ответе сервера — обычно вы хотите увидеть больше.

Далее вы узнаете как просмотреть фактические данные, которые сервер отправил в теле ответа.

Content

Ответ на Get-запрос, в теле сообщения часто содержит некую ценную информацию, известную как «полезная нагрузка» («Payload»). Используя атрибуты и методы Response, вы можете просматривать payload в разных форматах.

Чтобы увидеть содержимое ответа в байтах, используйте :

Пока дает вам доступ к необработанным байтам полезной нагрузки ответа, вы можете захотеть преобразовать их в строку с использованием кодировки символов UTF-8. Response это сделает за вас, когда вы укажите :

Поскольку для декодирования байтов в строки требуется схема кодирования, Requests будет пытаться угадать кодировку на основе заголовков ответа. Вы можете указать кодировку явно, установив перед указанием :

Если вы посмотрите на ответ, то вы увидите, что на самом деле это последовательный JSON контент. Чтобы получить словарь, вы можете взять строку, которую получили из и десериализовать ее с помощью . Однако, более простой способ сделать это — использовать .

Тип возвращаемого значения — это словарь, поэтому вы можете получить доступ к значениям в объекте по ключу.

Вы можете делать многое с кодом состояний и телом сообщений. Но если вам нужна дополнительная информация, такая как метаданные о самом ответе, то вам нужно взглянуть на заголовки ответа.

Заголовки

Заголовки ответа могут дать вам полезную информацию, такую как тип ответа и ограничение по времени, в течение которого необходимо кэшировать ответ.
Чтобы посмотреть заголовки, укажите :

возвращает похожий на словарь объект, позволяющий получить доступ к значениям объекта по ключу. Например, чтобы получить тип содержимого ответа, вы можете получить доступ к Content-Type:

Используя ключ или — вы получите одно и то же значение.

Теперь вы узнали основное о Response. Вы увидели его наиболее используемые атрибуты и методы в действии. Давайте сделаем шаг назад и посмотрим как изменяются ответы при настройке Get-запросов.

Other Changes

Some changes to the language were made as part of the same project.

New Builtin Functions

Two new intrinsic functions for class testing were introduced
(since the functionality had to be implemented in the C API, there was
no reason not to make it accessible to Python programmers).

returns true iff class D is derived
from class C,
directly or indirectly. issubclass(C, C) always returns true. Both
arguments must be class objects.

returns true iff x is an
instance of C or of a
(direct or indirect) subclass of C. The first argument may hyave any
type; if x is not an instance of any class, isinstance(x, C) always
returns false. The second argument must be a class object.

Sequence Unpacking

Previous Python versions require an exact type match between the
left hand and right hand side of «unpacking» assignments, e.g.

As part of the same project, the right hand side of either statement can be
any sequence with exactly three items. This makes it possible to
extract e.g. the errno and strerror values from an IOError exception
in a backwards compatible way:

The same approach works for the SyntaxError exception, with the
proviso that the info part is not always present:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Adblock
detector