File: index.rst

package info (click to toggle)
python-sigmavirus24-urltemplate 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 284 kB
  • sloc: python: 1,190; sh: 7; makefile: 4
file content (82 lines) | stat: -rw-r--r-- 2,471 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
uritemplate
===========

Release v\ |version|.

Examples
--------

This first example shows how simple the API can be when using for a one-off
item in a script or elsewhere.

.. code-block:: python

    from requests import get
    from uritemplate import expand

    uri = 'https://api.github.com{/user}'

    user = get(expand(uri, user='sigmavirus24')).json()

This second example shows how using the class will save you time for template
parsing and object creation. Making the template once means the URI is parsed
once which decreases the number of :class:`URITemplate
<uritemplate.URITemplate>` objects created and usage of the ``re`` module.
This means that as soon as the file is parsed, the ``User.github_url`` and
``Repository.github_url`` variables are made once and only once. They're then
usable in every instance of those classes.

.. code-block:: python

    from uritemplate import URITemplate

    class User(object):
        github_url = URITemplate('https://api.github.com{/user}')
        def __init__(self, name):
            self.uri = self.github_url.expand({'user': name})
            self.name = name

    class Repository(object):
        github_url = URITemplate('https://api.github.com{/user}{/repo}')
        def __init__(self, name):
            self.uri = self.github_url.expand(
                dict(zip(['user', 'repo'], name.split('/')))
            )
            self.name = name

API
---

.. module:: uritemplate

.. autofunction:: uritemplate.api.expand

.. autofunction:: uritemplate.api.partial

.. autofunction:: uritemplate.api.variables

.. autoclass:: uritemplate.template.URITemplate
    :members:

Implementation Details
----------------------

Classes, their methods, and functions in this section are not part of the API
and as such are not meant to be used by users of ``uritemplate.py``. These are
documented here purely for reference as they are inadvertently exposed via the
public API.

For example::

    t = URITemplate('https://api.github.com/users{/user}')
    t.variables
    # => [URIVariable(/user)]

Users can interact with :class:`~uritemplate.variable.URIVariable` objects as
they see fit, but their API may change and are not guaranteed to be consistent
across versions. Code relying on methods defined on
:class:`~uritemplate.variable.URIVariable` and other classes, methods, and
functions in this section may break in future releases.

.. autoclass:: uritemplate.variable.URIVariable
    :members: expand