Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. class gen_doc_google:
  2.     '''
  3.        Obiekt generujący doc string na podstawie norm stylizacji google
  4.        
  5.        Args:
  6.            dictionary: (dict) słownik zapewniający informacje do stworzenia doc stringa
  7.        
  8.        Attributes:
  9.            dictionary: tu przechowujemy informacje o parametrze dictionary
  10.    '''
  11.     def __init__(self, dictionary):
  12.         self.dictionary = dictionary
  13.     def __str__(self):
  14.         """
  15.            Funkcja specjalna zamienia zbudowaną liste na stringa i konwertująca obiekt gen_doc_google na stringa
  16.        """
  17.         return "".join(self.build())
  18.     def __repr__(self):
  19.         """
  20.            Funkcja specjalna zamienia zbudowaną liste na stringa i konwertująca obiekt gen_doc_google na stringa
  21.        """
  22.         return "".join(self.build())
  23.     def build(self):
  24.         """
  25.            Metoda budująca liste informacji potrzebnych do stworzenia doc stringa.
  26.            
  27.            Returns:
  28.                list,
  29.        """
  30.         doc = ['"""\n']
  31.         for key in self.dictionary:
  32.             doc.append('\t{0}: \n'.format(key))
  33.             if key == "Args":
  34.                 for info in self.dictionary[key]:
  35.                     doc.append("\t\t\t {0}: ({1})\n".format(info, self.dictionary[key][info].__name__))
  36.             if key == "Attributes":
  37.                 for info in self.dictionary[key]:
  38.                     doc.append("\t\t\t {0}: tu przechowujemy informacje o parametrze {1}\n".format(info, info))
  39.             if key == "Raises":
  40.                 for info in self.dictionary[key]:
  41.                     doc.append("\t\t\t {0}, \n".format(info.__name__))
  42.             if key == "Returns":
  43.                 for info in self.dictionary[key]:
  44.                     doc.append("\t\t\t {0}, \n".format(info.__name__))
  45.             if key == "Summary":
  46.                 del doc[-1]
  47.                 doc.insert(1, "\t" + self.dictionary[key] + "\n\n")
  48.             if key == "Description":
  49.                 del doc[-1]
  50.                 doc.insert(2, "\t" + self.dictionary[key] + "\n\n")
  51.                
  52.         doc.append('\n"""')
  53.         return doc
  54. print(gen_doc_google({'Args': {'a':str, 'b':int},
  55.                       'Attributes': ['a', 'b'],
  56.                       'Summary': "Obiekt Gruszka opisuje nam wlasnosci gruszek.",
  57.                       'Returns': [bool],
  58.                       'Raises': [AttributeError, ValueError, TypeError],
  59.                       'Description': "Bardziej dokładny opis gruszki chyba"}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement