-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_storage.py
102 lines (88 loc) · 3.4 KB
/
list_storage.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
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
"""
A small module I made to save lists to files for future use.
There are two classes and multiple methods.
"""
# The pickle module can only be used in python.
import pickle
# So you can use the more universal json format.
import json
class StoreList():
# This class requires one argument, a list, on creation.
def __init__(self, list):
self.list = list
# Now you can send it to any file in any method as many times as you want.
# Just use a file path as file name.
# The file_name must be entered as a string.
# file_name example: '/home/user/Desktop/example_list.txt'
#***Single line, aka iterator loop.
def simplesend(self, file_name):
with open('{}'.format(file_name), 'a+') as filehandle:
for listitem in self.list:
filehandle.write('%s\n' % listitem)
#***Multi line
def multisend(self, file_name):
with open('{}'.format(file_name), 'a+') as filehandle:
filehandle.writelines("%s\n" % place for place in self.list)
#***Python only pickle module.
# Typically use a .pickle extension.
def pklsend(self, file_name):
with open('{}'.format(file_name), 'wb') as filehandle:
# Store the data as binary data stream.
pickle.dump(self.list, filehandle)
#***JSON format, very universal.
def jsend(self, file_name):
with open('{}'.format(file_name), 'w') as filehandle:
json.dump(self.list, filehandle)
# Seperated store and retrieve
# because it would use the same list and cause problems.
# Now you can retrieve anyway as a one liner right to a list or print.
class GetList():
#***Single line
def simpleretrieve(file_name):
list = []
# Open file and read the content in a list.
with open('{}'.format(file_name), 'r') as filehandle:
for line in filehandle:
# Remove linebreak which is the last character of the string.
currentPlace = line[:-1]
# Add item to the list.
list.append(currentPlace)
return(list)
#***Multi line
def multiretrieve(file_name):
# Define empty list.
list = []
# Open file and read the content in a list.
with open('{}'.format(file_name), 'r') as filehandle:
filecontents = filehandle.readlines()
for line in filecontents:
# Remove linebreak which is the last character of the string.
current_place = line[:-1]
# Add item to the list.
list.append(current_place)
return(list)
#***Pythonic aka one liner.
def pythonicmultiretrive(file_name):
list = []
# Open file and read the content in a list.
with open('{}'.format(file_name), 'r') as filehandle:
list = [
current_place.rstrip()
for current_place in filehandle.readlines()
]
return(list)
#***Python only pickle module
# Typically use .pickle extension.
def pklretrieve(file_name):
list = []
with open('{}'.format(file_name), 'rb') as filehandle:
# Read the data as binary data stream.
list = pickle.load(filehandle)
return(list)
#***JSON format, very universal.
def jretrieve(file_name):
list = []
# Open output file for reading.
with open('{}'.format(file_name), 'r') as filehandle:
list = json.load(filehandle)
return(list)