-
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.
- Loading branch information
Showing
856 changed files
with
7,656 additions
and
40,206 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Temporary Cookbook on Clean Code | ||
|
||
Jina is designed as a lean and efficient framework. Solutions built on top of Jina also mean to be so. Here are some | ||
tips to help you write clean & beautiful code. | ||
|
||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
|
||
|
||
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
||
1. `from jina import Document, DocumentArray, Executor, Flow, requests` is all you need. Copy-paste it as the first line of your code. | ||
|
||
1. No need to implement `__init__` if your `Executor` does not contain initial states. | ||
|
||
✅ Do: | ||
```python | ||
from jina import Executor | ||
|
||
class MyExecutor(Executor): | ||
def foo(self, **kwargs): | ||
... | ||
``` | ||
😔 Don't: | ||
```python | ||
from jina import Executor | ||
|
||
class MyExecutor(Executor): | ||
def __init__(**kwargs): | ||
super().__init__(**kwargs) | ||
|
||
def foo(self, **kwargs): | ||
... | ||
``` | ||
|
||
1. Use `@requests` without specifying `on=` if your function mean to work on all requests. You can use it for catching all requests that are not for this Executor. | ||
|
||
✅ Do: | ||
```python | ||
from jina import Executor, requests | ||
|
||
class MyExecutor(Executor): | ||
|
||
@requests | ||
def _skip_all(self, **kwargs): | ||
pass | ||
``` | ||
😔 Don't: | ||
```python | ||
from jina import Executor | ||
|
||
class MyExecutor(Executor): | ||
@requests(on='/index') | ||
def _skip_index(self, **kwargs): | ||
pass | ||
|
||
@requests(on='/search') | ||
def _skip_search(self, **kwargs): | ||
pass | ||
``` | ||
|
||
1. Fold unnecessary arguments into `**kwargs`, only get what you need. | ||
|
||
✅ Do: | ||
```python | ||
from jina import Executor, requests | ||
|
||
class MyExecutor(Executor): | ||
|
||
@requests | ||
def foo_need_pars_only(self, parameters, **kwargs): | ||
print(parameters) | ||
``` | ||
😔 Don't: | ||
```python | ||
from jina import Executor, requests | ||
|
||
class MyExecutor(Executor): | ||
|
||
@requests | ||
def foo_need_pars_only(self, docs, parameters, docs_matrix, groundtruths_matrix, **kwargs): | ||
print(parameters) | ||
``` |
Oops, something went wrong.