-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(document): add fluent interface and make docbot generate it (#3980)
- Loading branch information
Showing
11 changed files
with
185 additions
and
6 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Fluent Interface | ||
|
||
Jina provides a simple fluent interface for `Document` that allows one to process (often preprocess) a Document object by chaining methods. For example to read an image file as `numpy.ndarray`, resize it, normalize it and then store it to another file; one can simply do: | ||
|
||
```python | ||
from jina import Document | ||
|
||
d = ( | ||
Document(uri='apple.png') | ||
.load_uri_to_image_blob() | ||
.set_image_blob_shape((64, 64)) | ||
.set_image_blob_normalization() | ||
.dump_image_blob_to_file('apple1.png') | ||
) | ||
``` | ||
|
||
```{figure} apple.png | ||
:scale: 20% | ||
Original `apple.png` | ||
``` | ||
|
||
```{figure} apple1.png | ||
:scale: 50% | ||
Processed `apple1.png` | ||
``` | ||
|
||
````{important} | ||
Note that, chaining methods always modify the original Document in-place. That means the above example is equivalent to: | ||
```python | ||
from jina import Document | ||
d = Document(uri='apple.png') | ||
(d.load_uri_to_image_blob() | ||
.set_image_blob_shape((64, 64)) | ||
.set_image_blob_normalization() | ||
.dump_image_blob_to_file('apple1.png')) | ||
``` | ||
```` | ||
|
||
|
||
## Methods | ||
|
||
All the following methods can be chained. | ||
|
||
|
||
<!-- fluent-interface-start --> | ||
### Convert | ||
Provide helper functions for {class}`Document` to support conversion between {attr}`.blob`, {attr}`.text` | ||
and {attr}`.buffer`. | ||
- {meth}`~jina.types.document.mixins.convert.ConvertMixin.convert_blob_to_buffer` | ||
- {meth}`~jina.types.document.mixins.convert.ConvertMixin.convert_buffer_to_blob` | ||
- {meth}`~jina.types.document.mixins.convert.ConvertMixin.convert_uri_to_datauri` | ||
|
||
|
||
### TextData | ||
Provide helper functions for {class}`Document` to support text data. | ||
- {meth}`~jina.types.document.mixins.text.TextDataMixin.convert_blob_to_text` | ||
- {meth}`~jina.types.document.mixins.text.TextDataMixin.convert_text_to_blob` | ||
- {meth}`~jina.types.document.mixins.text.TextDataMixin.dump_text_to_datauri` | ||
- {meth}`~jina.types.document.mixins.text.TextDataMixin.load_uri_to_text` | ||
|
||
|
||
### ImageData | ||
Provide helper functions for {class}`Document` to support image data. | ||
- {meth}`~jina.types.document.mixins.image.ImageDataMixin.convert_buffer_to_image_blob` | ||
- {meth}`~jina.types.document.mixins.image.ImageDataMixin.convert_image_blob_to_sliding_windows` | ||
- {meth}`~jina.types.document.mixins.image.ImageDataMixin.convert_image_blob_to_uri` | ||
- {meth}`~jina.types.document.mixins.image.ImageDataMixin.dump_image_blob_to_file` | ||
- {meth}`~jina.types.document.mixins.image.ImageDataMixin.load_uri_to_image_blob` | ||
- {meth}`~jina.types.document.mixins.image.ImageDataMixin.set_image_blob_channel_axis` | ||
- {meth}`~jina.types.document.mixins.image.ImageDataMixin.set_image_blob_normalization` | ||
- {meth}`~jina.types.document.mixins.image.ImageDataMixin.set_image_blob_shape` | ||
|
||
|
||
### AudioData | ||
Provide helper functions for {class}`Document` to support audio data. | ||
- {meth}`~jina.types.document.mixins.audio.AudioDataMixin.dump_audio_blob_to_file` | ||
- {meth}`~jina.types.document.mixins.audio.AudioDataMixin.load_uri_to_audio_blob` | ||
|
||
|
||
### BufferData | ||
Provide helper functions for {class}`Document` to handle binary data. | ||
- {meth}`~jina.types.document.mixins.buffer.BufferDataMixin.dump_buffer_to_datauri` | ||
- {meth}`~jina.types.document.mixins.buffer.BufferDataMixin.load_uri_to_buffer` | ||
|
||
|
||
### DumpFile | ||
Provide helper functions for {class}`Document` to dump content to a file. | ||
- {meth}`~jina.types.document.mixins.dump.DumpFileMixin.dump_buffer_to_file` | ||
- {meth}`~jina.types.document.mixins.dump.DumpFileMixin.dump_uri_to_file` | ||
|
||
|
||
### ContentProperty | ||
Provide helper functions for {class}`Document` to allow universal content property access. | ||
- {meth}`~jina.types.document.mixins.content.ContentPropertyMixin.dump_content_to_datauri` | ||
|
||
|
||
### VideoData | ||
Provide helper functions for {class}`Document` to support video data. | ||
- {meth}`~jina.types.document.mixins.video.VideoDataMixin.dump_video_blob_to_file` | ||
- {meth}`~jina.types.document.mixins.video.VideoDataMixin.load_uri_to_video_blob` | ||
|
||
|
||
### MeshData | ||
Provide helper functions for {class}`Document` to support 3D mesh data and point cloud. | ||
- {meth}`~jina.types.document.mixins.mesh.MeshDataMixin.load_uri_to_point_cloud_blob` | ||
|
||
|
||
<!-- fluent-interface-end --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import inspect | ||
import re | ||
import sys | ||
from collections import defaultdict | ||
|
||
from jina import Document | ||
|
||
all_meth = defaultdict(list) | ||
for f in inspect.getmembers(Document): | ||
if ( | ||
callable(f[1]) | ||
and not f[1].__name__.startswith('_') | ||
and not f[0].startswith('_') | ||
): | ||
if ( | ||
'return' in inspect.getfullargspec(f[1]).annotations | ||
and str(inspect.getfullargspec(f[1]).annotations['return']) == '~T' | ||
): | ||
module_name = f[1].__qualname__.split('.')[0].replace('Mixin', '') | ||
desc = inspect.getdoc( | ||
vars(sys.modules[f[1].__module__])[f[1].__qualname__.split('.')[0]] | ||
) | ||
|
||
all_meth[ | ||
( | ||
module_name, | ||
desc.strip() | ||
.replace(':class:', '{class}') | ||
.replace(':attr:', '{attr}'), | ||
) | ||
].append(f'{{meth}}`~{f[1].__module__}.{f[1].__qualname__}`') | ||
|
||
all_s = [] | ||
for k, v in all_meth.items(): | ||
all_s.append(f'### {k[0].strip()}') | ||
all_s.append(f'{k[1].strip()}') | ||
for vv in v: | ||
all_s.append(f'- {vv}') | ||
|
||
all_s.append('\n') | ||
|
||
|
||
doc_md = '../docs/fundamentals/document/fluent-interface.md' | ||
text = '\n'.join(all_s) | ||
|
||
with open(doc_md) as fp: | ||
_old = fp.read() | ||
_new = re.sub( | ||
r'(<!-- fluent-interface-start -->\s*?\n).*(\n\s*?<!-- fluent-interface-end -->)', | ||
rf'\g<1>{text}\g<2>', | ||
_old, | ||
flags=re.DOTALL, | ||
) | ||
|
||
with open(doc_md, 'w') as fp: | ||
fp.write(_new) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters