The Hacker's Guide To Python

1y ago
30 Views
2 Downloads
1.94 MB
271 Pages
Last View : 2d ago
Last Download : 4m ago
Upload by : Giovanna Wyche
Transcription

ContentsStarting your project.P⁴thon versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Project la⁴out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Version numbering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Coding st⁴le & automated checks . . . . . . . . . . . . . . . . . . . . . .Modules and libraries.The import s⁴stem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Standard libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .External libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Interview with Doug Hellmann . . . . . . . . . . . . . . . . . . . . . . . .Managing API changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Interview with Christophe de Vienne . . . . . . . . . . . . . . . . . . . .Documentation.Getting started with Sphinx and reST . . . . . . . . . . . . . . . . . . . .

iiCONTENTS.Sphinx modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Extending Sphinx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Distribution.A bit of histor⁴ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Packaging with pbr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Wheel format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Package installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Sharing ⁴our work with the world . . . . . . . . . . . . . . . . . . . . . .Interview with Nick Coghlan . . . . . . . . . . . . . . . . . . . . . . . . . .Entr⁴ points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Visualising entr⁴ points . . . . . . . . . . . . . . . . . . . . . . . . .Using console scripts . . . . . . . . . . . . . . . . . . . . . . . . . .Using plugins and drivers . . . . . . . . . . . . . . . . . . . . . . .Virtual environmentsUnit testing.The basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Mocking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Test streaming and parallelism . . . . . . . . . . . . . . . . . . . . . . . .Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using virtualenv with tox . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CONTENTS.Testing polic⁴ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Interview with Robert Collins . . . . . . . . . . . . . . . . . . . . . . . . .Methods and decorators.Creating decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .How methods work in P⁴thon . . . . . . . . . . . . . . . . . . . . . . . . .Static methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Class method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Abstract methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Mixing static, class, and abstract methods . . . . . . . . . . . . . . . . .The truth about super . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Functional programming.Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .List comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Functional functions functioning . . . . . . . . . . . . . . . . . . . . . . .The AST.H⁴ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Interview with Paul Tagliamonte . . . . . . . . . . . . . . . . . . . . . . .Performances and optimizations.Data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Ordered list and bisect . . . . . . . . . . . . . . . . . . . . . . . . . . . . .iii

CONTENTS.Namedtuple and slots . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Memoi⁵ation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .P⁴P⁴ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Achieving ⁵ero cop⁴ with the buffer protocol . . . . . . . . . . . . . . .Interview with Victor Stinner . . . . . . . . . . . . . . . . . . . . . . . . .Scaling and architecture.A note on multi-threading . . . . . . . . . . . . . . . . . . . . . . . . . . .Multiprocessing vs multithreading . . . . . . . . . . . . . . . . . . . . . .As⁴nchronous and event-driven architecture . . . . . . . . . . . . . . .Service-oriented architecture . . . . . . . . . . . . . . . . . . . . . . . . .RDBMS and ORM.Streaming data with Flask and PostgreSQL . . . . . . . . . . . . . . . .Interview with Dimitri Fontaine . . . . . . . . . . . . . . . . . . . . . . . .Python support strategies.Language and standard librar⁴ . . . . . . . . . . . . . . . . . . . . . . . .External libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using six . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Write less, code more.Single dispatcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Context managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .iv

List of Figures.Standard package director⁴ . . . . . . . . . . . . . . . . . . . . . . . . . .Coverage of ceilometer.publisher . . . . . . . . . . . . . . . . . . . . .KCacheGrind example . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using slice on memoryview objects . . . . . . . . . . . . . . . . . . . . .P⁴thon base classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .P⁴thon base classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

List of Examples.A pep run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Running pep with --ignore . . . . . . . . . . . . . . . . . . . . . . . . . .Hy module importer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A documented API change . . . . . . . . . . . . . . . . . . . . . . . . . . .A documented API change with warning . . . . . . . . . . . . . . . . . .Running python.Code from sphinxcontrib.pecanwsme.rest.setup . . . . . . . . . . .setup.py using distutils.setup.py using setuptools.Using setup.py.Result of epi group list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Result of epi group show console scripts . . . . . . . . . . . . . . . . . .Result of epi ep show console scripts coverage . . . . . . . . . . . . . .A console script generated b⁴ setuptools . . . . . . . . . . . . . . . . . .Running p⁴timed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Automatic virtual environment creation . . . . . . . . . . . . . . . . . .Boostraping a venv environment . . . . . . . . . . . . . . . . . . . . . . .A reall⁴ simple test in test true.py . . . . . . . . . . . . . . . . . . . . .Failing a test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Skipping tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .-W errorsdist. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

viiLIST OF EXAMPLES.Using setUp with unittest . . . . . . . . . . . . . . . . . . . . . . . . . . .Using fixtures.EnvironmentVariable . . . . . . . . . . . . . . . . . . .Basic mock usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Checking method calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using mock.patch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using mock.patch to test a set of behaviour . . . . . . . . . . . . . . . .testscenarios basic usage.Using testscenarios to test drivers . . . . . . . . . . . . . . . . . . . . .Using subunit2pyunit . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A .testr.conf file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Running testr.Using nosetests.Using coverage with testrepository . . . . . . . . . . . . . . . . . . . . . .A .travis.yml example file . . . . . . . . . . . . . . . . . . . . . . . . . .A registering decorator . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Source code of functools.update wrapper in P⁴thon .Using functools.wraps . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Retrieving function arguments using inspect . . . . . . . . . . . . . . .A P⁴thon method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A P⁴thon method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Calling unbound get si⁵e in P⁴thon. . . . . . . . . . . . . . . . . . .Calling unbound get si⁵e in P⁴thon. . . . . . . . . . . . . . . . . . .Calling bound get size . . . . . . . . . . . . . . . . . . . . . . . . . . . .@staticmethod usage.Implementing an abstract method . . . . . . . . . . . . . . . . . . . . . .Implementing an abstract method using abc . . . . . . . . . . . . . . .Mixing @classmethod and @abstractmethod . . . . . . . . . . . . . . . .Using super() with abstract methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .run --parallel. . . . . . . . . . . . . . . . . . . . . . .--with-coverage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

viiiLIST OF EXAMPLES.yield returning a value. . . . . . . . . . . . . . . . . . . . . . . . . . . .filter usage in P⁴thon. . . . . . . . . . . . . . . . . . . . . . . . . . . .Using first . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the operator module with itertools.groupby . . . . . . . . . .Parsing P⁴thon code to AST . . . . . . . . . . . . . . . . . . . . . . . . . .Hello world using P⁴thon AST . . . . . . . . . . . . . . . . . . . . . . . . .Changing all binar⁴ operation to addition . . . . . . . . . . . . . . . . .Using the cProfile module . . . . . . . . . . . . . . . . . . . . . . . . . .Using KCacheGrind to visuali⁵e P⁴thon profiling data . . . . . . . . . .A function defined in a function, disassembled . . . . . . . . . . . . . .Disassembling a closure . . . . . . . . . . . . . . . . . . . . . . . . . . . .Usage of bisect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Usage of bisect.insort . . . . . . . . . . . . . . . . . . . . . . . . . . . .A SortedList implementation . . . . . . . . . . . . . . . . . . . . . . . . .A class declaration using slots . . . . . . . . . . . . . . . . . . . . .Memor⁴ usage of objects using slots . . . . . . . . . . . . . . . . .Declaring a class using namedtuple . . . . . . . . . . . . . . . . . . . . .Memor⁴ usage of a class built from collections.namedtuple . . . . .A basic memoi⁵ation technique . . . . . . . . . . . . . . . . . . . . . . . .Using functools.lru cache . . . . . . . . . . . . . . . . . . . . . . . . . .Result of time python worker.py . . . . . . . . . . . . . . . . . . . . . . . .Worker using multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . .Result of time python worker.py . . . . . . . . . . . . . . . . . . . . . . . .Basic example of using select . . . . . . . . . . . . . . . . . . . . . . . .Example with pyev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating the message table . . . . . . . . . . . . . . . . . . . . . . . . . .The notify on insert function . . . . . . . . . . . . . . . . . . . . . . . . .The trigger for notify on insert . . . . . . . . . . . . . . . . . . . . . . . .

LIST OF EXAMPLES.Receiving notifications in P⁴thon . . . . . . . . . . . . . . . . . . . . . . .Flask streamer application . . . . . . . . . . . . . . . . . . . . . . . . . . .Simple implementation of a context object . . . . . . . . . . . . . . . .Simplest usage of contextlib.contextmanager . . . . . . . . . . . . .Using a context manager on a pipeline object . . . . . . . . . . . . . . .Opening two files at the same time . . . . . . . . . . . . . . . . . . . . .Opening two files at the same time with one with statement . . . . .ix

About this bookVersion . released in March.If ⁴ou’re reading this, odds are good ⁴ou’ve been working with P⁴thon for sometime alread⁴. Ma⁴be ⁴ou learned it using some tutorials, delved into some existingprograms, or started from scratch, but whatever the case, ⁴ou’ve hacked ⁴our wa⁴into learning it. That’s exactl⁴ how I got familiar with P⁴thon up until I joined theOpenStack team over two ⁴ears ago.Before then, I was building m⁴ own P⁴thon libraries and applications on a "garageproject" scale, but things change once ⁴ou start working with hundreds of developers on sotware and libraries that thousands of users rel⁴ on. The OpenStackplatform represents over half a million lines of P⁴thon code, all of which needs tobe concise, efficient, and scalable to needs of whatever cloud computing application its users require. And when ⁴ou have a project this si⁵e, things like testing anddocumentation absolutel⁴ require automation, or else the⁴ won’t get done at all.I thought I knew a lot about P⁴thon when I first joined OpenStack, but I’ve learned alot more these past two ⁴ears working on projects the scale of which I could barel⁴even imagine when I got started. I’ve also had the opportunit⁴ to meet some of thebest P⁴thon hackers in the industr⁴ and learn from them – ever⁴thing from generalarchitecture and design principles to various helpful tips and tricks. Through thisbook, I hope to share the most important things I’ve learned so that ⁴ou can buildbetter P⁴thon programs – and build them more efficientl⁴, too!

Starting your project1.1Python versionsOne of the first questions ⁴ou’re likel⁴ to ask is "which versions of P⁴thon shouldm⁴ sotware support?". It’s well worth asking, since each new version of P⁴thonintroduces new features and deprecates old ones. Furthermore, there’s a huge gapbetween P⁴thon .x and P⁴thon .x: there are enough changes between the twobranches of the language that it can be hard to keep code compatible with both,as we’ll see in more detail later, and it can be hard to tell which version is moreappropriate when ⁴ou’re starting a new project. Here are some short answers: Versions . and older are prett⁴ much obsolete b⁴ now, so ⁴ou don’t have toworr⁴ about supporting them at all. If ⁴ou’re intent on supporting these older versions an⁴wa⁴, be warned that ⁴ou’ll have an even harder time ensuring that ⁴ourprogram supports P⁴thon .x as well. Though ⁴ou might still run into P⁴thon .on some older s⁴stems; if that’s the case for ⁴ou, sorr⁴! Version . is still viable; ⁴ou’ll find it in some older versions of operating s⁴stemssuch as Red Hat Enterprise Linux. It’s not hard to support P⁴thon . as well asnewer versions, but if ⁴ou don’t think ⁴our program will need to run on . , don’tstress ⁴ourself tr⁴ing to accommodate it. Version . is and will remain the last version of P⁴thon .x. It’s a good idea to

. . PROJECT LAYOUTmake it ⁴our main target, or one of ⁴our main targets, since a lot of sotware, libraries, and developers still make use of it. P⁴thon . should continue to be supported until around, so odds are it’s not going awa⁴ an⁴time soon. Version . , . , and . were released in quick succession and as such haven’tseen much adoption. If ⁴our code alread⁴ supports . , there’s not much point insupporting these versions as well. Version . and . are the most recent distributed editions of P⁴thon and theones ⁴ou should focus on supporting. P⁴thon . and . represent the future ofthe language, so unless ⁴ou’re focusing on compatibilit⁴ with older versions, ⁴oushould make sure ⁴our code runs on these versions as well.In summar⁴: support . onl⁴ if ⁴ou have to (or are looking for a challenge), definitel⁴ support . , and if ⁴ou want to guarantee that ⁴our sotware will continueto run for the foreseeable future, support . and above as well. You can safel⁴ ignore other versions, though that’s not to sa⁴ it’s impossible to support them all: theCherr⁴P⁴ project supports all versions of P⁴thon from . onward.Techniques for writing programs that support both P⁴thon . and . will be discussed in Chapter. You might spot some of these techniques in the sample codeas ⁴ou read: all of the code that ⁴ou’ll see in this book has been written to supportboth major versions.1.2Project layoutYour project structure should be fairl⁴ simple. Use packages and hierarch⁴ wisel⁴:a deep hierarch⁴ can be a nightmare to navigate, while a flat hierarch⁴ tends tobecome bloated.One common mistake is leaving unit tests outside the package director⁴. Thesetests should definitel⁴ be included in a sub-package of ⁴our sotware so that:

. . PROJECT LAYOUT the⁴ don’t get automaticall⁴ installed as a tests top-level module b⁴ setuptools(or some other packaging librar⁴). the⁴ can be installed and eventuall⁴ used b⁴ other packages to build their ownunit tests.The following diagram illustrates what a standard file hierarch⁴ should look like:Figure . : Standard package director⁴setup.py is the standard name for P⁴thon installation script.When run, it installs⁴our package using the P⁴thon distribution utilities (distutils). You can also pro-

. . PROJECT LAYOUTvide important information to users in README.rst (or README.txt, or whatever filename suits ⁴our fanc⁴).requirements.txtshould list ⁴our P⁴thon package’s de-pendencies – i.e., all of the packages that a tool such as pip should install to make⁴our package work. You can also include test-requirements.txt, which lists onl⁴the dependencies required to run the test suite. Finall⁴, the docs director⁴ shouldcontain the package’s documentation in reStructuredText format, that will be consumed b⁴ Sphinx (see Section . ).Packages oten have to provide extra data, such as images, shell scripts, and soforth. Unfortunatel⁴, there’s no universall⁴ accepted standard for where these filesshould be stored. Just put them wherever makes the most sense for ⁴our project.The following top-level directories also frequentl⁴ appear:Most of the time, the following extra top level directories are used: etc is for sample configuration files. tools is for shell scripts or related tools. bin is for binar⁴ scripts ⁴ou’ve written that will be installed b⁴ setup.py. data is for other kinds of data, such as media files.A design issue I oten encountered is to create files or modules based on the t⁴peof code the⁴ will store. Having a functions.py or exceptions.py file is a terribleapproach. It doesn’t help an⁴thing at all with code organi⁵ation and forces a readerto jump between files for no good reason. Organi⁵e ⁴our code based on features,not t⁴pe.Also, don’t create a director⁴ and just an init .py file in it, e.g. don’t createhooks/ init .pywhere hooks.py would have been enough. If ⁴ou create a di-rector⁴, it should contains several other P⁴thon files that belongs to the categor⁴/module the director⁴ represents.

. . VERSION NUMBERING1.3Version numberingAs ⁴ou might alread⁴ know, there’s an ongoing effort to standardi⁵e package metadata in the P⁴thon ecos⁴stem. One such piece of metadata is version number.PEPintroduces a version format that ever⁴ P⁴thon package, and ideall⁴ ever⁴application, should follow. This wa⁴, other programs and packages will be able toeasil⁴ and reliabl⁴ identif⁴ which versions of ⁴our package the⁴ require.PEPdefines the following regular expression format for version numbering:N[.N] [{a b c rc}N][.postN][.devN]This allows for standard numbering like . or . . . But note: . is equivalent to . . ; . . is equivalent to . . . , and so forth. Versions matching N[.N] are considered final releases. Date-based versions such asdesigned to detect PEP. .are considered invalid. Automated tools-format version numbers will (or should) raise an errorif the⁴ detect a version number greater than or equal to.Final components can also use the following format: N[.N] aN (e.g. . a ) denotes an alpha release, a version that might be unstableand missing features. N[.N] bN (e.g. . . b ) denotes a beta release, a version that might be featurecomplete but still bugg⁴. N[.N] cN or N[.N] rcN (e.g. . rc ) denotes a (release) candidate, a version thatmight be released as the final product unless significant bugs emerge. While the rcand c suffixes have the same meaning, if both are used, rc releases are consideredto be newer than c releases.

. . VERSION NUMBERINGThese suffixes can also be used: .postN (e.g. . .post ) indicates a post release. These are t⁴picall⁴ used to address minor errors in the publication process (e.g. mistakes in release notes). Youshouldn’t use .postN when releasing a bugfix version; instead, ⁴ou should increment the minor version number. .devN (e.g. . . .dev ) indicates a developmental release. This suffix is discouraged because it is harder for humans to parse. It indicates a prerelease of theversion that it qualifies: e.g. . . .dev indicates the third developmental versionof the . . release, prior to an⁴ alpha, beta, candidate or final release.This scheme should be sufficient for most common use cases.NoteYou might have heard of Semantic Versioning, which provides its own guidelines for version numbering. This specification partially overlaps with PEP 440, but unfortunately,they’re not entirely compatible. For example, Semantic Versioning’s recommendation forprerelease versioning uses a scheme such as 1.0.0-alpha 001 that is not compliant withPEP 440.If ⁴ou need to handle more advanced version numbers, ⁴ou should note that PEPdefines source label, a field that ⁴ou can use to carr⁴ an⁴ version string, andthen build a version number consistent with PEP requirements.Man⁴ DVCS ¹ platforms, such as Git and Mercurial, are able to generate version numbers using an identif⁴ing hash ². Unfortunatel⁴, this s⁴stem isn’t compatible withthe scheme defined b⁴ PEP: for one thing, identif⁴ing hashes aren’t orderable.However, it’s possible to use a source label field to hold such a version number anduse it to build a PEP-compliant version number.¹Distributed Version Control S⁴stem²For Git, refer to git-describe( ).

. . CODING STYLE & AUTOMATED CHECKSTippbr ᵃ, which will be discussed in Section 4.2, is able to automatically build version numbersbased on the Git revision of a project.ᵃPython Build Reasonableness1.4Coding style & automated checksYes, coding st⁴le is a touch⁴ subject, but we still need to talk about it.P⁴thon has an ama⁵ing qualit⁴ ³ that few other languages have: it uses indentationto define blocks. At first glance, it seems to offer a solution to the age-old question of "where should I put m⁴ curl⁴ braces?"; unfortunatel⁴, it introduces a newquestion in the process: "how should I indent?"And so the P⁴thon communit⁴, in their vast wisdom, came up with the PEP ⁛ standard for writing P⁴thon code. The list of guidelines boils down to: Use spaces per indentation level. Limit all lines to a maximum ofcharacters. Separate top-level function and class definitions with two blank lines. Encode files using ASCII or UTF- . One module import per import statement and per line, at the top of the file, atercomments and docstrings, grouped first b⁴ standard, then third-part⁴, and finall⁴local librar⁴ imports. No extraneous whitespaces between parentheses, brackets, or braces, or beforecommas.³Your mileage ma⁴ var⁴.⁛PEP Style Guide for Python Code, th Jul⁴, Guido van Rossum, Barr⁴ Warsaw, Nick Coghlan

. . CODING STYLE & AUTOMATED CHECKS Name classes in CamelCase; suffix exceptions with Error (if applicable); name functions in lowercase with words separated by underscores; and use a leading underscore for private attributes or methods.These guidelines reall⁴ aren’t hard to follow, and furthermore, the⁴ make a lot ofsense. Most P⁴thon programmers have no trouble sticking to them as the⁴ writecode.However, errare humanum est, and it’s still a pain to look through ⁴our code to makesure it fits the PEP guidelines. That’s what the pep tool is there for: it can automaticall⁴ check an⁴ P⁴thon file ⁴ou send its wa⁴.Example . A pep run pep8 hello.pyhello.py:4:1: E302 expected 2 blank lines, found 1 echo ?1pep indicates which lines and columns do not conform to PEP and reports eachissue with a code. Violations of MUST statements in the specification are reportedas errors (starting with E), while minor problems are reported as warnings (startingwith W). The three-digit code following the letter indicates the exact kind of erroror warning; ⁴ou can tell the general categor⁴ at a glance b⁴ looking at the hundredsdigit. For example, errors starting with E indicate issues with whitespace; errorsstarting with E indicate issues with blank lines; and warnings starting with W indicate deprecated features being used.The communit⁴ still debates whether validating against PEP code that is not partof the standard librar⁴ is a good practice. I advise ⁴ou to consider it and run a PEPvalidation tool against ⁴our source code on a regular basis. An eas⁴ wa⁴ to do thisis to integrate it into ⁴our test suite. While it ma⁴ seem a bit extreme, it’s a goodwa⁴ to ensure that ⁴ou continue to respect the PEP guidelines in the long term.

. . CODING STYLE & AUTOMATED CHECKSWe’ll discuss in Section . how ⁴ou can integrate pep with tox to automate thesechecks.The OpenStack project has enforced PEP conformance through automatic checkssince the beginning. While it sometimes frustrates newcomers, it ensures that thecodebase – which has grown to over .million lines of code – alwa⁴s looks thesame in ever⁴ part of the project. This is ver⁴ important for a project of an⁴ si⁵ewhere there are multiple developers with differing opinions on whitespace ordering.It’s also possible to ignore certain kinds of errors and warnings b⁴ using the --ignoreoption:Example . Running pep with --ignore pep8 --ignore E3 hello.py echo ?0This allows ⁴ou to effectivel⁴ ignore parts of the PEP standard that ⁴ou don’t wantto follow. If ⁴ou’re running pep on a existing code base, it also allows ⁴ou to ignorecertain kinds of problems so ⁴ou can focus on fixing issues one categor⁴ at a time.NoteIf you write C code for Python (e.g. modules), the PEP 7 standard describes the codingstyle that you should follow.Other tools also exist that check for actual coding errors rather than st⁴le errors.Some notable examples include: p⁴flakes, which supports plugins p⁴lint, which also checks PEP conformance, performs more checks b⁴ default,and supports plugins

. . CODING STYLE & AUTOMATED CHECKSThese tools all make use of static anal⁴sis – that is, the⁴ parse the code and anal⁴⁵eit rather than running it outright.If ⁴ou choose to use pyflakes, note that it doesn’t check PEP conformance on itsown – ⁴ou’ll still need to run pep as well. To simplif⁴ things, a project called flakecombines pyflakes and pep into a single command. It also adds some new featuressuch as skipping checks on lines containing #noqa and extensibilit⁴ via entr⁴ points.In its quest for beautiful and unified code, the OpenStack project chose flake for allof its code checks. However, as time passed, the hackers took advantage of flake 'sextensibilit⁴ to test for even more potential issues with submitted code. The endresult of all this is a flake extension called hacking. It checks for errors such asodd usage of except, P⁴thon / portabilit⁴ issues, import st⁴le, dangerous stringformatting, and possible locali⁵ation issues.If ⁴ou’re starting a new project, I strongl⁴ recommend ⁴ou use one of these tools andrel⁴ on it for automatic checking of ⁴our code qualit⁴ and st⁴le. If ⁴ou alread⁴ havea codebase, a good approach is to run them with most of the warnings disabled andfix issues one categor⁴ at a time.While none of these tools ma⁴ be a perfect fit for ⁴our project or ⁴our preferences,using flake and hacking together is a good wa⁴ to improve the qualit⁴ of ⁴our codeand make it more durable. If nothing else, it’s a good start toward that goal.TipMany text editors, including the famous GNU Emacs and vim, have plugins available (suchas Flymake) that can run tools such as pep8 or flake8 directly in your code buffer, interactively highlighting any part of your code that isn’t PEP 8-compliant. This is a handy wayto fix most style errors as you write your code.

Modules and libraries2.1The import systemIn order to use modules and libraries, ⁴ou have to import them.The Zen of Python import thisThe Zen of Python, by Tim PetersBeautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.

. . THE IMPORT SYSTEMNow is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!The import s⁴stem is quite complex, but ⁴ou probabl⁴ alread⁴ know the basics.Here, I’ll show ⁴ou some of the internals of this subs⁴stem.The sys module contains a lot of information about P⁴thon’s import s⁴stem. Firstof all, the list of modules currentl⁴ imported is available through the sys.modul

The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity.

Related Documents:

May 02, 2018 · D. Program Evaluation ͟The organization has provided a description of the framework for how each program will be evaluated. The framework should include all the elements below: ͟The evaluation methods are cost-effective for the organization ͟Quantitative and qualitative data is being collected (at Basics tier, data collection must have begun)

Silat is a combative art of self-defense and survival rooted from Matay archipelago. It was traced at thé early of Langkasuka Kingdom (2nd century CE) till thé reign of Melaka (Malaysia) Sultanate era (13th century). Silat has now evolved to become part of social culture and tradition with thé appearance of a fine physical and spiritual .

On an exceptional basis, Member States may request UNESCO to provide thé candidates with access to thé platform so they can complète thé form by themselves. Thèse requests must be addressed to esd rize unesco. or by 15 A ril 2021 UNESCO will provide thé nomineewith accessto thé platform via their émail address.

̶The leading indicator of employee engagement is based on the quality of the relationship between employee and supervisor Empower your managers! ̶Help them understand the impact on the organization ̶Share important changes, plan options, tasks, and deadlines ̶Provide key messages and talking points ̶Prepare them to answer employee questions

Dr. Sunita Bharatwal** Dr. Pawan Garga*** Abstract Customer satisfaction is derived from thè functionalities and values, a product or Service can provide. The current study aims to segregate thè dimensions of ordine Service quality and gather insights on its impact on web shopping. The trends of purchases have

Chính Văn.- Còn đức Thế tôn thì tuệ giác cực kỳ trong sạch 8: hiện hành bất nhị 9, đạt đến vô tướng 10, đứng vào chỗ đứng của các đức Thế tôn 11, thể hiện tính bình đẳng của các Ngài, đến chỗ không còn chướng ngại 12, giáo pháp không thể khuynh đảo, tâm thức không bị cản trở, cái được

Le genou de Lucy. Odile Jacob. 1999. Coppens Y. Pré-textes. L’homme préhistorique en morceaux. Eds Odile Jacob. 2011. Costentin J., Delaveau P. Café, thé, chocolat, les bons effets sur le cerveau et pour le corps. Editions Odile Jacob. 2010. Crawford M., Marsh D. The driving force : food in human evolution and the future.

Le genou de Lucy. Odile Jacob. 1999. Coppens Y. Pré-textes. L’homme préhistorique en morceaux. Eds Odile Jacob. 2011. Costentin J., Delaveau P. Café, thé, chocolat, les bons effets sur le cerveau et pour le corps. Editions Odile Jacob. 2010. 3 Crawford M., Marsh D. The driving force : food in human evolution and the future.