| Conditions | 12 |
| Total Lines | 70 |
| 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:
Complex classes like encode_multipart_formdata_stream() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from __future__ import unicode_literals |
||
| 16 | def encode_multipart_formdata_stream(fields, boundary=None): |
||
| 17 | """ |
||
| 18 | Encode a dictionary of ``fields`` using the multipart/form-data MIME format. |
||
| 19 | |||
| 20 | :param fields: |
||
| 21 | Dictionary of fields or list of (key, value) or (key, value, MIME type) |
||
| 22 | field tuples. The key is treated as the field name, and the value as |
||
| 23 | the body of the form-data bytes. If the value is a tuple of two |
||
| 24 | elements, then the first element is treated as the filename of the |
||
| 25 | form-data section and a suitable MIME type is guessed based on the |
||
| 26 | filename. If the value is a tuple of three elements, then the third |
||
| 27 | element is treated as an explicit MIME type of the form-data section. |
||
| 28 | |||
| 29 | Field names and filenames must be unicode. |
||
| 30 | |||
| 31 | :param boundary: |
||
| 32 | If not specified, then a random boundary will be generated using |
||
| 33 | :func:`mimetools.choose_boundary`. |
||
| 34 | """ |
||
| 35 | body = [] |
||
| 36 | |||
| 37 | def body_write(item): |
||
| 38 | if isinstance(item, six.binary_type): |
||
| 39 | item = BytesIO(item) |
||
| 40 | elif isinstance(item, six.text_type): |
||
| 41 | item = StringIO(item) |
||
| 42 | body.append(item) |
||
| 43 | |||
| 44 | body_write_encode = lambda item: body.append(BytesIO(item.encode('utf-8'))) |
||
| 45 | |||
| 46 | if boundary is None: |
||
| 47 | boundary = choose_boundary() |
||
| 48 | |||
| 49 | for fieldname, value in iter_fields(fields): |
||
| 50 | body_write_encode('--%s\r\n' % (boundary)) |
||
| 51 | |||
| 52 | if isinstance(value, tuple): |
||
| 53 | if len(value) == 3: |
||
| 54 | filename, data, content_type = value |
||
| 55 | else: |
||
| 56 | filename, data = value |
||
| 57 | from mimetypes import guess_type |
||
| 58 | content_type, _ = guess_type(filename) |
||
| 59 | if content_type is None: |
||
| 60 | content_type = 'application/octet-stream' |
||
| 61 | body_write_encode('Content-Disposition: form-data; name="%s"; ' |
||
| 62 | 'filename="%s"\r\n' % (fieldname, filename)) |
||
| 63 | body_write_encode('Content-Type: %s\r\n\r\n' % |
||
| 64 | (content_type,)) |
||
| 65 | else: |
||
| 66 | data = value |
||
| 67 | body_write_encode('Content-Disposition: form-data; name="%s"\r\n' |
||
| 68 | % (fieldname)) |
||
| 69 | body_write(b'\r\n') |
||
| 70 | |||
| 71 | if isinstance(data, six.integer_types): |
||
| 72 | data = six.text_type(data) # Backwards compatibility |
||
| 73 | |||
| 74 | if isinstance(data, six.text_type): |
||
| 75 | body_write_encode(data) |
||
| 76 | else: |
||
| 77 | body_write(data) |
||
| 78 | |||
| 79 | body_write(b'\r\n') |
||
| 80 | |||
| 81 | body_write_encode('--%s--\r\n' % (boundary)) |
||
| 82 | |||
| 83 | content_type = 'multipart/form-data; boundary=%s' % (boundary) |
||
| 84 | |||
| 85 | return body, content_type |
||
| 86 | |||
| 166 |