ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/BatesRangeFunctions.py
Revision: 744
Committed: Tue Apr 13 21:40:41 2021 UTC (4 years, 11 months ago) by nino.borges
Content type: text/x-python
File size: 5778 byte(s)
Log Message:
Updated all tools libs to be python 3 compatible. 

File Contents

# Content
1 """
2 BatesRangeFunctions
3
4 Created by
5 Emanuel Borges
6 12.31.2019
7
8 A set of functions for dealing wiht bates ranges, like enumerating, page counts, etc. Originally this was a program
9 called FixBatesRange_func in the summation tools package but I use the enumerate and page counts for lots of other reasons,
10 so I copied it out to tools and deleted the parts that I didnt need.
11
12 Note that separate_alpha was the third version of this function, fixing lots of bugs, but it still calls the original SeperateAlpha
13 as a fall back for some reason.
14
15 """
16
17
18 import re
19
20 class EnumError(Exception):
21 def __init__(self,value):
22 self.value = value
23 def __str__(self):
24 return repr(self.value)
25
26
27 def _SeperateAlphaOrig(bates):
28 """ This original function will chop the individual bates into alpha and numeric. still used as a fall back for separate_alpha"""
29 import string
30 alpha = []
31 for i in bates:
32 if i in string.ascii_letters:
33 alpha.append(i)
34 if i in string.punctuation:
35 alpha.append(i)
36 alpha = ",".join(alpha).replace(" ","")
37 if alpha == "":
38 num = bates
39 else:
40 num = bates.replace(alpha,"")
41 while num[0] == '0':
42 alpha = alpha + num[0]
43 num = num[1:]
44 return (alpha, int(num))
45
46 def seperate_alpha(bates):
47 """This third version of this function will chop the individual bates into alpha and numeric"""
48
49 parts = [_f for _f in re.split(r'(\d+|\s+)',bates) if _f]
50 moreParts = _SeperateAlphaOrig(parts[-1])
51 num = moreParts[-1]
52 parts = parts[0:-1]
53 moreParts = moreParts[0:-1]
54 alpha = ""
55 for i in parts:
56 alpha = alpha + i
57 for x in moreParts:
58 alpha = alpha +x
59 return (alpha, int(num))
60
61
62 def generate_index(str):
63 """
64 Splits a string into alpha and numeric elements, which
65 is used as an index for sorting"
66 """
67 #
68 # the index is built progressively
69 # using the _append function
70 #
71 index = []
72 def _append(fragment, alist=index):
73 if fragment.isdigit(): fragment = int(fragment)
74 alist.append(fragment)
75
76 # initialize loop
77 prev_isdigit = str[0].isdigit()
78 current_fragment = ''
79 # group a string into digit and non-digit parts
80 for char in str:
81 curr_isdigit = char.isdigit()
82 if curr_isdigit == prev_isdigit:
83 current_fragment += char
84 else:
85 _append(current_fragment)
86 current_fragment = char
87 prev_isdigit = curr_isdigit
88 _append(current_fragment)
89 return tuple(index)
90
91
92 def GetBatesPageCount(begNo, endNo):
93 begNoIndex = generate_index(begNo)
94 endNoIndex = generate_index(endNo)
95 matrix = list(zip(begNoIndex, endNoIndex))
96 for i in matrix:
97 try:
98 numb = int(i[1]) - int(i[0])
99 except:
100 pass
101 ## Now check to see if there was a hanging chad on the endDoc and account for it.
102 if len(endNoIndex) > len(begNoIndex):
103 ## first lets see if it's an int first. this will work if its begno foo-0001 endno foo0001_002
104 ## but not if the endno was foo0001_002a
105 if str(endNoIndex[-1]).isdigit():
106 numb=+endNoIndex[-1]
107 numb = numb - 1
108 else:
109 ## otherwise it's probable just a real handing chad like foo-0001a
110 numb =+1
111 return numb +1
112
113 def GetBatesRanges(batesList):
114 """Takes a list of bates numbers (this single list will include all bates from beg and end values) and
115 returns a list of tuple ranges"""
116 batesMatrix = {}
117 batesRangeList = []
118 for bates in batesList:
119 alpha, numb = seperate_alpha(bates)
120 ## now remove the trailing zeros
121 alpha = alpha.rstrip("0")
122 if alpha in list(batesMatrix.keys()):
123 batesMatrix[alpha].append(bates)
124 else:
125 batesMatrix[alpha] = [bates,]
126 for batesGroup in list(batesMatrix.keys()):
127 tempList = batesMatrix[batesGroup]
128 tempList.sort()
129 batesRangeList.append((tempList[0],tempList[-1]))
130 return batesRangeList
131
132 def EnumerateBates(begNo, endNo):
133 """Will return all the possible bates values in between begNo and endNo. Split Alpha needs to be fixed
134 for bates with spaces. This will error out if the begdoc or enddoc has a weird value like foo-001.a"""
135 pageCount = GetBatesPageCount(begNo, endNo)
136 begBates = seperate_alpha(begNo)
137 endBates = seperate_alpha(endNo)
138 prefix = begBates[0]
139 padding = ""
140 #reverse the string
141 for x in prefix[::-1]:
142 if x.isdigit():
143 padding = padding + x
144 prefix = prefix[:-1]
145 else:
146 break
147 padding = len(str(begBates[-1]) + padding)
148
149 batesList = []
150 for i in range(begBates[-1], endBates[-1]+1):
151 batesList.append(prefix + str(i).zfill(padding))
152 if len(batesList) == pageCount:
153 return batesList
154 else:
155 #print begBates,endBates
156 ## My own exception that I can catch.
157 raise EnumError("Could not enumerate fully.")
158
159 def NextBatesShouldBe(bates):
160 """Method will tell you what the next bates should be based on the bates that you give it."""
161 batesPrefix, batesNumber = seperate_alpha(bates)
162 batesNumber = batesNumber + 1
163 newBates = batesPrefix + str(batesNumber)
164 return newBates
165
166 def PreviousBatesShouldBe(bates):
167 """Method will tell you what the previous bates should be based on the bates that you give it."""
168 batesPrefix, batesNumber = seperate_alpha(bates)
169 batesNumber = batesNumber - 1
170 newBates = batesPrefix + str(batesNumber)
171 return newBates