logo
首页技术栈工具库讨论
pathlib
pathlib
**Attention:** this backport module isn't maintained anymore. If you want to report issues or contribute patches, please consider the `pathlib2 <https://pypi.python.org/pypi/pathlib2/>`_ project instead. Description ----------- pathlib offers a set of classes to handle filesystem paths. It offers the following advantages over using string objects: * No more cumbersome use of os and os.path functions. Everything can be done easily through operators, attribute accesses, and method calls. * Embodies the semantics of different path types. For example, comparing Windows paths ignores casing. * Well-defined semantics, eliminating any warts or ambiguities (forward vs. backward slashes, etc.). Requirements ------------ Python 3.2 or later is recommended, but pathlib is also usable with Python 2.7 and 2.6. Install ------- In Python 3.4, pathlib is now part of the standard library. For Python 3.3 and earlier, ``easy_install pathlib`` or ``pip install pathlib`` should do the trick. Examples -------- Importing the module classes:: >>> from pathlib import * Listing Python source files in a directory:: >>> list(p.glob('*.py')) [PosixPath('test_pathlib.py'), PosixPath('setup.py'), PosixPath('pathlib.py')] Navigating inside a directory tree:: >>> p = Path('/etc') >>> q = p / 'init.d' / 'reboot' >>> q PosixPath('/etc/init.d/reboot') >>> q.resolve() PosixPath('/etc/rc.d/init.d/halt') Querying path properties:: >>> q.exists() True >>> q.is_dir() False Opening a file:: >>> with q.open() as f: f.readline() ... '#!/bin/bash\n' Documentation ------------- The full documentation can be read at `Read the Docs <https://pathlib.readthedocs.org/>`_. Contributing ------------ Main development now takes place in the Python standard library: see the `Python developer's guide <http://docs.python.org/devguide/>`_, and report issues on the `Python bug tracker <http://bugs.python.org/>`_. However, if you find an issue specific to prior versions of Python (such as 2.7 or 3.2), you can post an issue on the `BitBucket project page <https://bitbucket.org/pitrou/pathlib/>`_. History ------- Version 1.0.1 ^^^^^^^^^^^^^ - Pull request #4: Python 2.6 compatibility by eevee. Version 1.0 ^^^^^^^^^^^ This version brings ``pathlib`` up to date with the official Python 3.4 release, and also fixes a couple of 2.7-specific issues. - Python issue #20765: Add missing documentation for PurePath.with_name() and PurePath.with_suffix(). - Fix test_mkdir_parents when the working directory has additional bits set (such as the setgid or sticky bits). - Python issue #20111: pathlib.Path.with_suffix() now sanity checks the given suffix. - Python issue #19918: Fix PurePath.relative_to() under Windows. - Python issue #19921: When Path.mkdir() is called with parents=True, any missing parent is created with the default permissions, ignoring the mode argument (mimicking the POSIX "mkdir -p" command). - Python issue #19887: Improve the Path.resolve() algorithm to support certain symlink chains. - Make pathlib usable under Python 2.7 with unicode pathnames (only pure ASCII, though). - Issue #21: fix TypeError under Python 2.7 when using new division. - Add tox support for easier testing. Version 0.97 ^^^^^^^^^^^^ This version brings ``pathlib`` up to date with the final API specified in :pep:`428`. The changes are too long to list here, it is recommended to read the `documentation <https://pathlib.readthedocs.org/>`_. .. warning:: The API in this version is partially incompatible with pathlib 0.8 and earlier. Be sure to check your code for possible breakage! Version 0.8 ^^^^^^^^^^^ - Add PurePath.name and PurePath.anchor. - Add Path.owner and Path.group. - Add Path.replace(). - Add Path.as_uri(). - Issue #10: when creating a file with Path.open(), don't set the executable bit. - Issue #11: fix comparisons with non-Path objects. Version 0.7 ^^^^^^^^^^^ - Add '**' (recursive) patterns to Path.glob(). - Fix openat() support after the API refactoring in Python 3.3 beta1. - Add a *target_is_directory* argument to Path.symlink_to() Version 0.6 ^^^^^^^^^^^ - Add Path.is_file() and Path.is_symlink() - Add Path.glob() and Path.rglob() - Add PurePath.match() Version 0.5 ^^^^^^^^^^^ - Add Path.mkdir(). - Add Python 2.7 compatibility by Michele Lacchia. - Make parent() raise ValueError when the level is greater than the path length.
priority
priority
Priority: A HTTP/2 Priority Implementation ========================================== Priority is a pure-Python implementation of the priority logic for HTTP/2, set out in `RFC 7540 Section 5.3 (Stream Priority)`_. This logic allows for clients to express a preference for how the server allocates its (limited) resources to the many outstanding HTTP requests that may be running over a single HTTP/2 connection. Specifically, this Python implementation uses a variant of the implementation used in the excellent `H2O`_ project. This original implementation is also the inspiration for `nghttp2's`_ priority implementation, and generally produces a very clean and even priority stream. The only notable changes from H2O's implementation are small modifications to allow the priority implementation to work cleanly as a separate implementation, rather than being embedded in a HTTP/2 stack directly. While priority information in HTTP/2 is only a suggestion, rather than an enforceable constraint, where possible servers should respect the priority requests of their clients. Using Priority -------------- Priority has a simple API. Streams are inserted into the tree: when they are inserted, they may optionally have a weight, depend on another stream, or become an exclusive dependent of another stream. .. code-block:: python >>> p = priority.PriorityTree() >>> p.insert_stream(stream_id=1) >>> p.insert_stream(stream_id=3) >>> p.insert_stream(stream_id=5, depends_on=1) >>> p.insert_stream(stream_id=7, weight=32) >>> p.insert_stream(stream_id=9, depends_on=7, weight=8) >>> p.insert_stream(stream_id=11, depends_on=7, exclusive=True) Once streams are inserted, the stream priorities can be requested. This allows the server to make decisions about how to allocate resources. Iterating The Tree ~~~~~~~~~~~~~~~~~~ The tree in this algorithm acts as a gate. Its goal is to allow one stream "through" at a time, in such a manner that all the active streams are served as evenly as possible in proportion to their weights. This is handled in Priority by iterating over the tree. The tree itself is an iterator, and each time it is advanced it will yield a stream ID. This is the ID of the stream that should next send data. This looks like this: .. code-block:: python >>> for stream_id in p: ... send_data(stream_id) If each stream only sends when it is 'ungated' by this mechanism, the server will automatically be emitting stream data in conformance to RFC 7540. Updating The Tree ~~~~~~~~~~~~~~~~~ If for any reason a stream is unable to proceed (for example, it is blocked on HTTP/2 flow control, or it is waiting for more data from another service), that stream is *blocked*. The ``PriorityTree`` should be informed that the stream is blocked so that other dependent streams get a chance to proceed. This can be done by calling the ``block`` method of the tree with the stream ID that is currently unable to proceed. This will automatically update the tree, and it will adjust on the fly to correctly allow any streams that were dependent on the blocked one to progress. For example: .. code-block:: python >>> for stream_id in p: ... send_data(stream_id) ... if blocked(stream_id): ... p.block(stream_id) When a stream goes from being blocked to being unblocked, call the ``unblock`` method to place it back into the sequence. Both the ``block`` and ``unblock`` methods are idempotent and safe to call repeatedly. Additionally, the priority of a stream may change. When it does, the ``reprioritize`` method can be used to update the tree in the wake of that change. ``reprioritize`` has the same signature as ``insert_stream``, but applies only to streams already in the tree. Removing Streams ~~~~~~~~~~~~~~~~ A stream can be entirely removed from the tree by calling ``remove_stream``. Note that this is not idempotent. Further, calling ``remove_stream`` and then re-adding it *may* cause a substantial change in the shape of the priority tree, and *will* cause the iteration order to change. License ------- Priority is made available under the MIT License. For more details, see the LICENSE file in the repository. Authors ------- Priority is maintained by Cory Benfield, with contributions from others. For more details about the contributors, please see CONTRIBUTORS.rst in the repository. .. _RFC 7540 Section 5.3 (Stream Priority): https://tools.ietf.org/html/rfc7540#section-5.3 .. _nghttp2's: https://nghttp2.org/blog/2015/11/11/stream-scheduling-utilizing-http2-priority/ .. _H2O: https://h2o.examp1e.net/ Changelog ========= 1.3.0 (2017-01-27) ------------------ **API Changes** - Throw ``PriorityLoop`` when inserting or reprioritising a stream that depends on itself. - Throw ``BadWeightError`` when creating or reprioritising a stream with a weight that is not an integer between 1 and 256, inclusive. - Throw ``PseudoStreamError`` when trying to reprioritise, remove, block or unblock stream 0. - Add a new ``PriorityError`` parent class for the exceptions that can be thrown by priority. 1.2.2 (2016-11-11) ------------------ **Bugfixes** - Allow ``insert_stream`` to be called with ``exclusive=True`` but no explicit ``depends_on`` value. 1.2.1 (2016-10-26) ------------------ **Bugfixes** - Allow insertion of streams that have parents in the idle or closed states. This would previously raise a KeyError. 1.2.0 (2016-08-04) ------------------ **Security Fixes** - CVE-2016-6580: All versions of this library prior to 1.2.0 are vulnerable to a denial of service attack whereby a remote peer can cause a user to insert an unbounded number of streams into the priority tree, eventually consuming all available memory. This version adds a ``TooManyStreamsError`` exception that is raised when too many streams are inserted into the priority tree. It also adds a keyword argument to the priority tree, ``maximum_streams``, which limits how many streams may be inserted. By default, this number is set to 1000. Implementations should strongly consider whether they can set this value lower. 1.1.1 (2016-05-28) ------------------ **Bugfixes** - 2.5x performance improvement by swapping from ``queue.PriorityQueue`` to ``heapq``. 1.1.0 (2016-01-08) ------------------ **API Changes** - Throw ``DuplicateStreamError`` when inserting a stream that is already in the tree. - Throw ``MissingStreamError`` when reprioritising a stream that is not in the tree. 1.0.0 (2015-12-07) ------------------ - Initial release.