代码重构第一步

This commit is contained in:
2022-02-06 18:59:45 +08:00
parent c4dd7b1ce8
commit aa210ac678
7 changed files with 132 additions and 18 deletions

View File

@ -78,7 +78,7 @@ class jsonIO:
class uniteIO:
def __init__(self,fileName:str,fileType,data: Any = None) -> None:
def __init__(self,fileName:str,fileType = None,data: Any = None) -> None:
'''简单的文件数据IO操作功能'''
self.filename = fileName
if not fileType is None:
@ -91,21 +91,12 @@ class uniteIO:
with open (self.file, 'rb') as f:
self._type = pickle
if self._type == json:
with open (self.filename, 'r', encoding='utf-8') as f:
self._rfile = f
with open (self.filename, 'w', encoding='utf-8') as f:
self._wfile = f
elif self._type == pickle:
with open (self.file, 'rb') as f:
self._rfile = f
with open (self.file, 'wb') as f:
self._wfile = f
if not data is None:
self._data = data
else:
self._data = self._type.load(self._rfile)
self._data = self.load()
def __call__(self, *args: Any, **kwds: Any) -> Any:
@ -113,11 +104,19 @@ class uniteIO:
def write(self):
'''将数据写入文件'''
if self._type == json:
self._wfile = open(self.filename, 'w', encoding='utf-8')
elif self._type == pickle:
self._wfile = open(self.file, 'wb')
self._type.dump(self._data, self._wfile)
def load(self) -> Any:
'''从文件读取数据'''
if self._type == json:
self._rfile = open(self.filename, 'r', encoding='utf-8')
elif self._type == pickle:
self._rfile = open(self.file, 'rb')
self._data = self._type.load(self._rfile)
return self.data
@ -131,4 +130,4 @@ if __name__ == '__main__':
from sys import argv
if argv[1]:
input(pickle(argv[1]).data)
input(uniteIO(argv[1]).data)