| Conditions | 3 |
| Total Lines | 53 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import functools |
||
| 74 | def paginate(max_per_page=10): |
||
| 75 | def decorator(f): |
||
|
1 ignored issue
–
show
|
|||
| 76 | @functools.wraps(f) |
||
|
1 ignored issue
–
show
|
|||
| 77 | def wrapped(*args, **kwargs): |
||
|
1 ignored issue
–
show
|
|||
| 78 | page = request.args.get('page', 1, type=int) |
||
|
1 ignored issue
–
show
|
|||
| 79 | per_page = min( |
||
|
1 ignored issue
–
show
|
|||
| 80 | request.args.get('per_page', max_per_page, type=int), max_per_page) |
||
| 81 | query = f(*args, **kwargs) |
||
|
1 ignored issue
–
show
|
|||
| 82 | p = query.paginate(page, per_page) |
||
|
1 ignored issue
–
show
|
|||
| 83 | pages = { |
||
|
1 ignored issue
–
show
|
|||
| 84 | 'page': page, |
||
| 85 | 'per_page': per_page, |
||
| 86 | 'total': p.total, |
||
| 87 | 'pages': p.pages |
||
| 88 | } |
||
| 89 | if p.has_prev: |
||
|
1 ignored issue
–
show
|
|||
| 90 | pages['prev'] = url_for( |
||
|
1 ignored issue
–
show
|
|||
| 91 | request.endpoint, |
||
| 92 | page=p.prev_num, |
||
| 93 | per_page=per_page, |
||
| 94 | _external=True, |
||
| 95 | **kwargs) |
||
| 96 | else: |
||
|
1 ignored issue
–
show
|
|||
| 97 | pages['prev'] = None |
||
|
1 ignored issue
–
show
|
|||
| 98 | if p.has_next: |
||
|
1 ignored issue
–
show
|
|||
| 99 | pages['next'] = url_for( |
||
|
1 ignored issue
–
show
|
|||
| 100 | request.endpoint, |
||
| 101 | page=p.next_num, |
||
| 102 | per_page=per_page, |
||
| 103 | _external=True, |
||
| 104 | **kwargs) |
||
| 105 | else: |
||
|
1 ignored issue
–
show
|
|||
| 106 | pages['next'] = None |
||
|
1 ignored issue
–
show
|
|||
| 107 | pages['first'] = url_for( |
||
|
1 ignored issue
–
show
|
|||
| 108 | request.endpoint, |
||
| 109 | page=1, |
||
| 110 | per_page=per_page, |
||
| 111 | _external=True, |
||
| 112 | **kwargs) |
||
| 113 | pages['last'] = url_for( |
||
|
1 ignored issue
–
show
|
|||
| 114 | request.endpoint, |
||
| 115 | page=p.pages, |
||
| 116 | per_page=per_page, |
||
| 117 | _external=True, |
||
| 118 | **kwargs) |
||
| 119 | return jsonify({ |
||
|
1 ignored issue
–
show
|
|||
| 120 | 'urls': [item.get_url() for item in p.items], |
||
| 121 | 'meta': pages |
||
| 122 | }) |
||
| 123 | |||
| 124 | return wrapped |
||
|
1 ignored issue
–
show
|
|||
| 125 | |||
| 126 | return decorator |
||
|
1 ignored issue
–
show
|
|||
| 127 | |||
| 170 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.