Skip to main content

Python library for the Hypothes.is API

Project description

This package provides python bindings to the Hypothesis API.

Low-level access

The api submodule is a low-level implementation of the Hypothesis API.

The contents of this module reflect the REST nature of the API:

  • API calls are made by functions.

  • Input and output data are uninterpreted data (e.g. JSON strings, not objects resulting from interpreting the JSON).

  • Argument checking is minimal and most exceptions will come when the API returns an error (APIError is raised if the server does not return 200).

  • The auth argument is always given first (except to root(), which does not take authorization). auth can currently be None or a string containing a developer token.

Examples:

>>> h_annot.api.read(None, '53LMZGVCEemN4zOvm3oFEQ')
u'{"updated": "2019-04-22T21:09:23.352503+00:00", "group": "__world__", ...
>>> h_annot.api.read(None, 'bogusannotationid')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "h_annot/api.py", line 39, in read
    raise APIError(r)
h_annot.exceptions.APIError: API call returned 404 (Not Found): not_found

High-level access

The Annotation class is an object abstraction of an annotation. Use the load() class method to get an annotation from its ID:

>>> annot = h_annot.Annotation.load('53LMZGVCEemN4zOvm3oFEQ')
>>> annot.text
u"I'm imagining!!!"

Exceptions are more pythonic:

>>> h_annot.Annotation.load('somebogusannotid')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "h_annot/annotation.py", line 97, in load
    raise KeyError('annotation ID %s not found' % annot_id)
KeyError: 'annotation ID somebogusannotid not found'

The Annotation constructor should not be called directly.

Some attributes can be updated; to do so, set the authentication token using the h_annot.auth() context manager:

>>> with h_annot.auth(authentication_token):
...     annot.text = 'new text'

Annotations don’t have an inherent concept of authentication, so the previous way of declaring authentication to annotations:

>>> annot = h_annot.Annotation.load('someannotationid', 'somedevelkey')
>>> annot.text = 'new text'

is deprecated.

Tags are accessed and changed through the tags attribute. This attribute acts like a case-insensitive set (like Hypothesis itself treats tags).

>>> print annot.tags
TagSet(objectives, interwebs)
>>> for tag in annot.tags:
...     print tag
objectives
interwebs
>>> with h_annot.auth(authentication_token):
...     annot.tags = ['all', 'new', 'tags']
...     annot.tags.add('and one more')
...     annot.tags.remove('new')

Searching via Annotation.search() is deprecated. This search just wrapped the results of api.search() in Annotation constructors, so api.search() should now be used for searches that used Annotation.search(). For a high-level search interface, use h_annot.search(). This takes keyword arguments uri, user, tags, and text. Note that tags are joined by AND and separate words in text are joined by OR, which is the behavior of the Hypothesis search API. h_annot.search() respects the authentication set by the h_annot.auth() context manager.

h_annot.search() returns a SearchResults instance. SearchResults instances respond properly to len(), and iteration will result in annotations:

>>> results = h_annot.search()
800765
>>> len(results)
>>> for annotation in results:
...     print(annotation)
...     break
<Hypothesis annotation WcxuNG0CEemcl4_d0fJoaw>

The SearchResults object will load more results as needed from Hypothesis, but with a one second delay before each subsequent query to avoid rapid repeated hits to Hypothesis if something easily done and seemingly innocuous like list(h_annot.search()) is done.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-hypothesis-0.4.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

python_hypothesis-0.4.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

python_hypothesis-0.4.1-py2-none-any.whl (9.0 kB view details)

Uploaded Python 2

File details

Details for the file python-hypothesis-0.4.1.tar.gz.

File metadata

  • Download URL: python-hypothesis-0.4.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for python-hypothesis-0.4.1.tar.gz
Algorithm Hash digest
SHA256 75b67a6360057a9731b54340af7263a843127d03b925f38a371280cc1a0ed8db
MD5 dc0033cbd3ce16ef519e326794f94d02
BLAKE2b-256 9f1de52e925606fae91fb315da8a8bb12ab8b283fc67b379a2eded5f6cc552e4

See more details on using hashes here.

File details

Details for the file python_hypothesis-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: python_hypothesis-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for python_hypothesis-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 54748a827e6e86fc2bd65c66ac201731024656151ee7d535f008a120993761bd
MD5 b0cac03887d76f802697f27422d81a5e
BLAKE2b-256 ed7d2592bab18c8ad46a545cde5eb34c53fe1186514cc89ec31e25a56d8ceb4f

See more details on using hashes here.

File details

Details for the file python_hypothesis-0.4.1-py2-none-any.whl.

File metadata

  • Download URL: python_hypothesis-0.4.1-py2-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for python_hypothesis-0.4.1-py2-none-any.whl
Algorithm Hash digest
SHA256 bb3619e14c2b47b62fd0cae4b02b9e7cb7fae0975c2aed0993fc895d0f036272
MD5 45ff01bb0064732201e155bd9e5a1cde
BLAKE2b-256 1aea3fe0fe53bf27d938a8d24d8883643ffee6f1331a7850afbf18268dfa56c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page