Sigo haciendo feliz a pylint

This commit is contained in:
Daniel Cortes
2020-06-07 22:29:38 -04:00
parent 40a5d37bb8
commit a0a5857a8b
2 changed files with 53 additions and 39 deletions

View File

@@ -1,5 +1,16 @@
"""Modulo de funciones utilitarias"""
import json
import logging
from collections import Mapping
import functools
from multiprocessing.dummy import Pool
from django.http import JsonResponse
from pygments import highlight
from pygments.lexers import JsonLexer # pylint: disable=no-name-in-module
from pygments.formatters import TerminalTrueColorFormatter # pylint: disable=no-name-in-module
_log = logging.getLogger('utils')
_log.addHandler(logging.NullHandler())
@@ -7,20 +18,12 @@ _log.addHandler(logging.NullHandler())
def pretty_print_json(json_input):
"""Formats and prints json to stdout with colors using pygments"""
import json
from pygments import highlight
from pygments.lexers import JsonLexer
from pygments.formatters import TerminalTrueColorFormatter
formatted_json = json.dumps(json_input, indent=2)
print(highlight(formatted_json, JsonLexer(), TerminalTrueColorFormatter()))
def message_response(status, error_message):
"""Sends an error response with the status code of the error and an explanation"""
from django.http import JsonResponse
json_message = {
'status_code': status,
'message': error_message
@@ -29,48 +32,56 @@ def message_response(status, error_message):
return JsonResponse(json_message, status=status)
# noinspection PyPep8Naming
def require_JSON(function):
def require_json(function):
"""Decorator to make a view only accept a json body"""
import functools
import json
@functools.wraps(function)
def decorator(*args, **kwargs):
try:
received_json = json.loads(args[0].body)
return function(*args, **kwargs, received_json=received_json)
except json.JSONDecodeError as error:
_log.warning(f'Function {function.__name__} got a non json request body')
except json.JSONDecodeError:
_log.warning('Function %s got a non json request body', function.__name__)
return message_response(400, 'Se envío json no valido')
return decorator
def expected_keys(keys, dictionary):
"""Verifica que un diccionario contiene todas las keys de una lista"""
for key in keys:
if key not in dictionary:
return f'No se encuentra {key}'
return None
def replace_key(json, old, new):
json[new] = json[old]
del json[old]
def replace_key(dictionary, old, new):
"""Remplaza una key en un diccionario
En esencia crea una nueva key con los contenidos de la antigua y elimina la antigua
Esto lo hace inplace por lo que el diccionario es modificado
TODO que cree un nuevo diccionario, es mas bonito el codigo asi
"""
dictionary[new] = dictionary[old]
del dictionary[old]
def sanitize_keys(json):
for key in list(json.keys()):
if '-' in key and key in json:
def sanitize_keys(dictionary):
"""Remplaza las keys de un diccionario que contienen '-' ya que el diccionario pretende
ser json y acceder a json con esos caracteres en javascript es incomodo
El diccionario es modificado inplace pero de todas formas se retorna
"""
for key in list(dictionary.keys()):
if '-' in key and key in dictionary:
new_key = key.replace('-', '_')
replace_key(json, key, new_key)
return json
replace_key(dictionary, key, new_key)
return dictionary
def parallel_map(items, function):
from multiprocessing.dummy import Pool
"""Ejecuta una función sobre cada elemento de una lista de items en paralelo utilizando
una Pool del modulo multiprocessing.dummy """
with Pool() as pool:
return pool.map(function, items)