@anonymous/

UnpleasantBiodegradableSystems

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
rawstring = "not a good thing"

rawdict = {
  'error': ['string 1', 'string2'],
  'error2': ['string 3']
}

class ErrorFormat: 
    def __init__(self, key=None, val=None):
      self.entry = dict(field=key, message=val)

    
class ErrorResponse: 
    def __init__(self, someval, field_name=None):
      self.payload = dict(errors=list())
      if isinstance(someval, str):
        self.format_for_string(someval, field_name)
      elif isinstance(someval, dict):
        self.format_for_dict(someval)

    def format_for_string(self, someval, field_name):
      errlist = [ErrorFormat(field_name, someval).entry]
      self.payload['errors'] = errlist

    def format_for_dict(self, somedict):
      errlist = []
      for key in somedict:
        print('key in somedict')
        print(key)
        for msg in somedict[key]:
          # errlist.append({'field': key, 'message': msg})
          errlist.append(ErrorFormat(key, msg).entry)
      self.payload['errors'] = errlist


s = ErrorResponse(rawstring)
d = ErrorResponse(rawdict)