Tuesday, April 4, 2017

APIsExceptions


Here’s the plan: when someone uses a feature you don’t understand, simply shoot them.
This is easier than learning something new, and before too long the only living coders
will be writing in an easily understood, tiny subset of Python 0.9.6 <wink>1.
— Tim Peters
 legendary core developer and author of The Zen of Python
“Python is an easy to learn, powerful programming language.” Those are the first words
of the official Python Tutorial. That is true, but there is a catch: because the language is
easy to learn and put to use, many practicing Python programmers leverage only a
fraction of its powerful features.


Exceções como APIsExceptions formam um aspecto importante da API como função. Os programadores de uma função precisam saber quais exceções esperar em várias condições para que eles possam garantir manipuladores de exceção adequados. Usaremos um achado da raiz quadrada como um exemplo usando uma função de raiz quadrada.
Coloque o seguinte código em um arquivo chamado roots.py. Há apenas um recurso de linguagem neste programa que não conhecemos antes, o lógico e o operador, que usamos neste caso para testar que duas condições são verdadeiras em cada iteração do loop. O Python também inclui um lógico ou operador, que pode ser usado para testar se um ou ambos os operandos são verdadeiros.

O Python fornece vários tipos de exceção padrão para sinalizar erros comuns. Se um parâmetro de função for fornecido com um valor ilegal, é costume criar um ValueError. Podemos fazer isso usando a palavra-chave raise com um objeto de exceção recém-criado, que podemos criar chamando o construtor ValueError. Há dois lugares que podemos lidar com a divisão por zero. A primeira abordagem seria envolver a raiz encontrando enquanto loop em uma tentativa, exceto ZeroDivisionError, em seguida, criar uma nova exceção ValueError dentro do manipulador de exceção. 


As you may have noticed, several of the operations mentioned work equally for texts, lists and tables. Texts, lists and tables together are called trains. […] The FOR command also works generically on trains1. Geurts, Meertens and Pemberton
 ABC Programmer’s Handbook

Understanding the variety of sequences available in Python saves us from reinventing the wheel, and their common interface inspires us to create APIs that properly support and leverage existing and future sequence types.

Overview of common mapping methods
The basic API for mappings is quite rich. Table 3-1 shows the methods implemented by dict and two of its most useful variations: defaultdict and OrderedDict, both
defined in the collections module.

ORM

Object-Relational Mapper — an API that provides access to database tables and records as Python classes and objects, providing method calls to perform database operations. SQLAlchemy is a popular stand-alone Python ORM; the Django and Web2py frameworks have their own bundled ORMs. 

overriding descriptor A descriptor that implements __set__ and therefore intercepts and overrides attempts at setting the managed attribute in the man‐ aged instance. Also called data descriptor or
enforced descriptor. Contrast with non-overriding descriptor.

parallel assignment Assigning to several variables from items in an iterable, using syntax like a, b = [c, d] — also known as destructuring assignment. This is a common application of tuple
unpacking.

No comments:

Post a Comment

Remote Hybrid and Office work