-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathepisode_6.py
231 lines (167 loc) · 6.25 KB
/
episode_6.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from pymarc import MARCReader
from copy import deepcopy
my_marc_file = "NLNZ_example_marc.marc"
a = ["Hello"]
# assigning b to be a
b = a
#printing b
print ("b =", b)
print (id(a))
print (id(b))
print ()
#changing a *only*!
a[0] = "World!"
#printing b
print ("b =", b)
print (id(a))
print (id(b))
print ()
print ("_______ making a deep copy _______")
print ()
with open(my_marc_file, 'rb') as data:
reader = MARCReader(data)
for record in reader:
my_record = deepcopy(record)
print (id(record))
print (id(my_record))
print (my_record)
break
print ()
print ("_______ making a deep copy _______")
print ()
with open(my_marc_file, 'rb') as data:
reader = MARCReader(data)
for record in reader:
my_record = deepcopy(record)
# we only need to update the 'a' subfield.
# note the catalogers punctuation... we must include the commas.
my_record['100']['a'] = "Manning, Arthuretta,"
#comparing the two
print (record['100'])
print (my_record['100'])
break
print ()
print ("_______ Remove Field(s)_______")
print ()
with open(my_marc_file, 'rb') as data:
reader = MARCReader(data)
for record in reader:
my_record = deepcopy(record)
# We use the get_fields() method to generate a list of 300 get_fields
# As there is only, we can just remove it.
for my_field in my_record.get_fields('300'):
my_record.remove_field(my_field)
#comparing the two
print (record['300'])
print (my_record['300'])
break
print ()
print ("_______ Remove Field(s) with basic checks_______")
print ()
with open(my_marc_file, 'rb') as data:
reader = MARCReader(data)
for record in reader:
my_record = deepcopy(record)
# We use the get_fields() method to generate a list of 300 get_fields
my_fields = my_record.get_fields('300')
# We test if this list of fields contains only one member
if len(my_fields) == 1:
print ("Only one 300 field found in record ID {}. Removing it.".format(record['001'].value()))
for my_field in my_fields:
my_record.remove_field(my_field)
else:
print ("More than one 300 field found in record ID {}. Doing nothing.".format(record['001'].value()))
# comparing the two
print ("Number of 300 fields in record:", len(record.get_fields('300')))
print ("Number of 300 fields in my_record:", len(my_record.get_fields('300')))
print()
# testing the failing case
# We use the get_fields() method to generate a list of 300 get_fields
my_fields = my_record.get_fields('035')
# We test if this list of fields contains only one member
if len(my_fields) == 1:
print ("Only one 035 field found in record ID {}. Removing it.".format(record['001'].value()))
for my_field in my_fields:
my_record.remove_field(my_field)
else:
print ("More than one 035 field found in record ID {}. Doing nothing.".format(record['001'].value()))
# comparing the two
print ("Number of 035 fields in record:", len(record.get_fields('035')))
print ("Number of 035 fields in my_record:", len(my_record.get_fields('035')))
break
print ()
print ("_______ Remove Field(s) with logic_______")
print ()
with open(my_marc_file, 'rb') as data:
reader = MARCReader(data)
for record in reader:
my_record = deepcopy(record)
my_fields = my_record.get_fields('035')
for my_field in my_fields:
if "ilsdb" in my_field.value():
my_record.remove_field(my_field)
print (len(record.get_fields('035')))
print (len(my_record.get_fields('035')))
break
print ()
print ("_______ Remove Subfield(s)_______")
print ()
with open(my_marc_file, 'rb') as data:
reader = MARCReader(data)
for record in reader:
my_record = deepcopy(record)
my_fields = my_record.get_fields('100')
for my_field in my_fields:
my_field.delete_subfield('d')
print (record['100'])
print (my_record['100'])
break
print ()
print ("_______ Add field _______")
print ()
from pymarc import Field
with open(my_marc_file, 'rb') as data:
reader = MARCReader(data)
for record in reader:
my_record = deepcopy(record)
### making the new 245 field
my_new_245_field = Field(
tag = '245',
indicators = ['0','1'],
subfields = [
'a', 'The pragmatic programmer : ',
'b', 'from journeyman to master /',
'c', 'Andrew Hunt, David Thomas.',
]
)
### adding the new field
my_record.add_field(my_new_245_field)
# my_record.add_ordered_field(my_new_245_field)
### showing the diffence
for original_245 in record.get_fields('245'):
print (original_245)
print ("______")
for my_record_245 in my_record.get_fields('245'):
print (my_record_245)
print (my_record)
break
print ()
print ("_______ making a new record _______")
print ()
from pymarc import Record
my_new_record = Record()
print (my_new_record)
print ()
print ("_______ making a new record - populated _______")
print ()
from pymarc import Record
my_new_record = Record()
my_new_fields = []
my_new_fields.append(Field('003', data='Nz'))
my_new_fields.append(Field(tag='100', indicators=['1',' '], subfields=['a','Gattuso, Jay,', 'd', 'd1978-']))
my_new_fields.append(Field(tag='245', indicators=['1','0'], subfields=['a','Goats. Are they the best animals? :', 'b', 'What about Cats!? /' ]))
my_new_fields.append(Field(tag='650', indicators=[' ','0'], subfields=['a','Goats', 'b', 'Competitive Pet Keeping']))
my_new_fields.append(Field(tag='650', indicators=[' ','0'], subfields=['a','Cats', 'b', 'Competitive Pet Keeping']))
for my_new_field in my_new_fields:
my_new_record.add_ordered_field(my_new_field)
print (my_new_record)