-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_like.py
75 lines (64 loc) · 2.45 KB
/
file_like.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class FileLikeObject:
def __init__(self):
self.content = ""
self.position = 0
def write(self, data):
self.content += data
def read(self, size=-1):
if size == -1:
size = len(self.content) - self.position
data = self.content[self.position:self.position+size]
self.position += size
return data
def seek(self, position, whence=0):
if whence == 0:
self.position = position
elif whence == 1:
self.position += position
elif whence == 2:
self.position = len(self.content) + position
def tell(self):
return self.position
def close(self):
pass # Add any cleanup code here, if necessary
class FileLikeDict:
def __init__(self, infile=None):
# Initialize with an empty dict if no infile is provided
self.files = infile if infile else {}
self.current_file = None
self.mode = None
self.closed = True
def open(self, file_key, mode):
if file_key not in self.files and 'x' not in mode:
raise FileNotFoundError(f"File '{file_key}' not found")
if 'x' in mode and file_key in self.files:
raise FileExistsError(f"File '{file_key}' already exists")
self.current_file = file_key
self.mode = mode
self.closed = False
if 'w' in mode:
# Clear the data for the current file if it's a write operation
self.files[self.current_file] = ''
def read(self):
if self.closed:
raise IOError("File not open")
if 'r' not in self.mode:
raise IOError("File not opened in read mode")
return self.files[self.current_file]
def write(self, data):
if self.closed:
raise IOError("File not open")
if 'w' not in self.mode and 'a' not in self.mode:
raise IOError("File not opened in write or append mode")
if 'b' in self.mode and not isinstance(data, bytes):
raise ValueError("Binary mode requires bytes-like object")
if 't' in self.mode and not isinstance(data, str):
raise ValueError("Text mode requires str object")
if self.mode == 'w':
self.files[self.current_file] = data
elif self.mode == 'a':
self.files[self.current_file] += data # Append the data
def close(self):
self.current_file = None
self.mode = None
self.closed = True