-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_files.py
More file actions
138 lines (112 loc) · 4.89 KB
/
06_files.py
File metadata and controls
138 lines (112 loc) · 4.89 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
Simulate a terminal prompting for commands from users. Each time the user entered a new command,
save it into a file in the local hard disk so that next time when the user runs the program,
all commands he/she typed in the past can be refered again by issuing the following command:
rf - read the whole commands
rf n - read the most current 'n' commands
Ask the username and the type of the file at the beginning of each terminal session to
determine which file type to use during that session. File type should only be either
'json' (JSON file - human readable) or 'bin' (binary file).
"""
import json
import pickle
import re
import math
commands = []
json_file_name = '06_files.json'
binary_fine_name = '06_files.bin'
def ask_for_username():
return input('Username: ')
def ask_for_file_type():
""" Ask the user to provide the type of file to which data will be saved.
Should be either 'json' (for text file in JSON format) or 'bin' (for binary file)"""
while True:
file_type = input('Type of the file to store data (json/bin): ')
if file_type != 'json' and file_type != 'bin':
continue
else:
return file_type
def get_user_input(username, session_id):
""" Prompt and get a command from the user.
Arguments:
:username: The username of the user.
:session_id: The session number incremented each time asking for inputing a command.
Returns:
The string command the user entered.
"""
print('\nrf [n]: read file content [with last n lines], q: quit')
return input(f'[{session_id:^3}@{username}] $ ')
def save_to_file(file_name):
""" Convert commands list to JSON data and save that data into a JSON file
(text that can be read by human) that will be stored in the local hard disk.
Arguments:
:file_name: The name of the file to which data will be saved.
"""
with open(file_name, mode='w') as f:
f.write(json.dumps(commands))
def save_to_binary_file(file_name):
""" Save data into a binary file that will be stored in the local hard disk.
Arguments:
:file_name: The name of the binary file to which data will be saved.
"""
with open(file_name, mode='wb') as f:
f.write(pickle.dumps(commands))
def read_from_file(file_name, lines='All'):
""" Read data from a text file (human readable) and print out the most currently
entered commands. The JSON data is deserialized to its original list of commands.
Arguments:
:file_name: The name of the text file to be read.
:lines: The number of the most currently entered commands that will be printed out.
"""
with open(file_name, mode='r') as f:
global commands
commands = json.loads(f.readlines()[0])
num_of_lines = len(commands) if lines == 'All' else int(lines)
printed_commands = commands[-num_of_lines:]
for command in printed_commands:
print(command)
def read_from_binary_file(file_name, lines='All'):
""" Read data from a binary file and print out the most currently entered commands.
The binary data is deserialized to its original list of commands.
Arguments:
:file_name: The name of the binary file to be read.
:lines: The number of the most currently entered commands that will be printed out.
"""
with open(file_name, mode='rb') as f:
global commands
commands = pickle.loads(f.read())
num_of_lines = len(commands) if lines == 'All' else int(lines)
printed_commands = commands[-num_of_lines:]
for command in printed_commands:
print(command)
username = ask_for_username()
file_type = ask_for_file_type()
session_id = 1
readfile_command_pattern = re.compile(r'rf [1-9]{1}[0-9]*')
while True:
command = get_user_input(username, session_id)
commands.append(command)
if command == 'q':
break
elif command == 'rf':
print("Read whole file")
if file_type == 'json':
read_from_file(json_file_name)
elif file_type == 'bin':
read_from_binary_file(binary_fine_name)
elif readfile_command_pattern.match(command):
match_info = readfile_command_pattern.match(command)
num_of_lines = f'{match_info.group(0)}'.split(' ')[1]
print(
f'Read {num_of_lines} {"lines" if int(num_of_lines) > 1 else "line"}')
if file_type == 'json':
read_from_file(json_file_name, lines=num_of_lines)
elif file_type == 'bin':
read_from_binary_file(binary_fine_name, lines=num_of_lines)
else:
print(f'Saving {command} to file')
if file_type == 'json':
save_to_file(json_file_name)
elif file_type == 'bin':
save_to_binary_file(binary_fine_name)
session_id += 1