Repository: dag-hammarskjold-library/dlx

Issue:
Table serialization fails when tag group has 10+ instances
Regex needs to be updated here 

https://github.com/dag-hammarskjold-library/dlx/blob/0c44bf62c67b76c67ca2011a82c1f8ce8a9b035a/dlx/marc/__init__.py#L147

Hints from issue comments:

Solution patch:
diff --git a/README.md b/README.md
index c8a7ddd9..457894c7 100644
--- a/README.md
+++ b/README.md
@@ -32,15 +32,15 @@ query = Query.from_string("245__a:'Title' AND 245__b:'subtitle'")
 
 # construct a more specific query using a `Condition`
 query = Query(
-	Condition('245', {'a': 'Title', 'b': 'subtitle'})
+    Condition('245', {'a': 'Title', 'b': 'subtitle'})
 )
 
 # get the records that match the query as `Bib` objects
 for bib in BibSet.from_query(query):
-	# edit the records
-	bib.set('245', 'a', 'Edited title')
-	# save the records back to the database
-	bib.commit(user='myusername')
+    # edit the records
+    bib.set('245', 'a', 'Edited title')
+    # save the records back to the database
+    bib.commit(user='myusername')
 
 ### File
 
@@ -48,20 +48,20 @@ from dlx.file import File, Identifier
 
 # import a file from the local disk
 File.import_from_path(
-	'c:\\files\file.pdf', 
-	identifiers=[Identifier('isbn', '9781234567890')], 
-	filename='fn.txt', languages=['EN'], 
-	mimetype='text/plain', 
-	source='demo'
+    'c:\\files\file.pdf', 
+    identifiers=[Identifier('isbn', '9781234567890')], 
+    filename='fn.txt', languages=['EN'], 
+    mimetype='text/plain', 
+    source='demo'
 )
 
 # import a file from a url
 File.import_from_path(
-	'www.someurl.com/file.pdf', 
-	identifiers=[Identifier('isbn', '9781234567890')], 
-	filename='fn.txt', languages=['EN'], 
-	mimetype='text/plain', 
-	source='demo'
+    'www.someurl.com/file.pdf', 
+    identifiers=[Identifier('isbn', '9781234567890')], 
+    filename='fn.txt', languages=['EN'], 
+    mimetype='text/plain', 
+    source='demo'
 )
 
 # retrieve the file record as a `File` object
diff --git a/dlx/marc/__init__.py b/dlx/marc/__init__.py
index 899275df..56fa5243 100644
--- a/dlx/marc/__init__.py
+++ b/dlx/marc/__init__.py
@@ -144,7 +144,7 @@ def sort_table_header(cls, header: list) -> list:
             key=lambda x: ( 
                 (re.match(r'\d+\.(\w+)', x)).group(1), # sort by tag
                 int(re.match(r'(\d+)\.', x).group(1)), # sort by prefix group
-                (re.match(r'\d\.\d{3}\$?(\w)?', x)).group(1) # sort by subfield code
+                (re.match(r'\d+\.\d{3}\$?(\w)?', x)).group(1) # sort by subfield code
             )
         )
 


Test patch:
diff --git a/tests/test_marcset.py b/tests/test_marcset.py
index 1d8e4c82..25857e00 100644
--- a/tests/test_marcset.py
+++ b/tests/test_marcset.py
@@ -218,6 +218,7 @@ def test_to_str(db):
 
 def test_to_csv(db):
     from dlx.marc import BibSet
+    from dlx.marc import Bib
 
     bibset = BibSet.from_query({})
     bibset.records = list(bibset.records)
@@ -231,6 +232,14 @@ def test_to_csv(db):
 
     assert bibs.to_csv(write_id=False) == '1.245$a,1.245$b\n"A title, with a comma",subtitle\n"A ""title, or name"" with double quotes in the middle",subtitle'
 
+    # bug issue 507: fields with more 10+ instances
+    from dlx.marc import Bib
+    bib = Bib()
+    [bib.set('999', 'a', str(i), address=['+']) for i in range(0, 11)]
+    bibs = BibSet()
+    bibs.records.append(bib)
+    assert bibs.to_csv(write_id=False) == '1.999$a,2.999$a,3.999$a,4.999$a,5.999$a,6.999$a,7.999$a,8.999$a,9.999$a,10.999$a,11.999$a\n0,1,2,3,4,5,6,7,8,9,10'
+
 def test_from_aggregation(db, bibs):
     from dlx.marc import BibSet, Query