Added new method save_json to deserialize the content before write in the file
Added extra documentation
@@ -8,16 +8,27 @@ class File(object):
self.file_name = file_name
def write(self, text):
- """
- write the content of text in the file set in self.file_name
+ """Write File
+
+ Writes the content of text in the file set in self.file_name
+
+ Arguments:
+ text {str} -- text to write
+
+ Returns:
+ bool -- true if was writed succesuflly
"""
with open(self.file_name, 'w') as file:
file.write(text)
return True
def read(self):
- """
- return the content of the file set in self.file_name
+ """Read File
+
+ returns the content of the file set in self.file_name
+
+ Returns:
+ str -- string from file
"""
encoding = 'utf-8'
text = ''
@@ -30,9 +41,12 @@ class File(object):
return text
def read_json(self):
- """
+ """read JSON
serialize the content of the file and transform it in
a json format type
+
+ Returns:
+ JSON -- serialized json data
"""
from json import loads
@@ -40,3 +54,16 @@ class File(object):
json = loads(file)
return json
+
+ def save_json(self, text):
+ """Save JSON
+
+ Stores JSON data in a file, before do it deserialize
+
+ Arguments:
+ text {str} -- json data to stored
+ """
+ from json import dumps
+
+ text = dumps(text, sort_keys=True, indent=4)
+ self.write(text)
