ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/Summation/Summation_functions/FixBatesRange_func.py
Revision: 563
Committed: Tue Nov 25 16:03:19 2014 UTC (11 years, 4 months ago) by nino.borges
Content type: text/x-python
File size: 5726 byte(s)
Log Message:
Added some code so that I can catch my own exception.

File Contents

# User Rev Content
1 ninoborges 8 ## FixBatesRange_func
2     ## This is not a standalone program. Instead it is one of many functions that can be called to repair summation
3     ## database entries. This one for example fixes a bates range that shows up as a single page instead of multi.
4     ## For ex. "IN_02001","IN_02001"\n "IN_02004","IN02004"\n when the first should have been "IN_02001","IN02003"\n
5     ## 12.03.03
6     ## EBorges
7    
8 nino.borges 528 import re
9    
10 nino.borges 563 class EnumError(Exception):
11     def __init__(self,value):
12     self.value = value
13     def __str__(self):
14     return repr(self.value)
15    
16    
17 ninoborges 8 def FixBatesRange(batesList):
18     """ This should be just the begin and end bates as shown in the ex. above THIS WONT GET THE LAST LINE!!! SO
19     MAKE SURE YOU HAVE A CORRECT LINE AT THE BOTTOM. eg. the next line of the import"""
20     import string
21     output = open(r'C:\test_dir\good_bates.txt','w')
22     batesListPos = 0
23     try:
24     for line in batesList:
25     batesListPos = batesListPos + 1
26     beginBates = line.split(',')[0]
27     badEndBates = line.split(',')[1]
28     goodEndBates = batesList[batesListPos].split(',')[0].replace('"','')
29     alpha,num = SeperateAlpha(goodEndBates)
30     num = num - 1
31     goodEndBates = alpha + str(num)
32     output.write(beginBates + ',' + '"'+ goodEndBates + '"'+ '\n')
33     except:
34     pass
35     output.close()
36    
37    
38    
39     def SeperateAlpha(bates):
40     """ This function will chop the individual bates into alpha and numeric"""
41     import string
42     alpha = []
43     for i in bates:
44     if i in string.letters:
45     alpha.append(i)
46     if i in string.punctuation:
47     alpha.append(i)
48     alpha = string.join(alpha).replace(" ","")
49     if alpha == "":
50     num = bates
51     else:
52     num = bates.replace(alpha,"")
53     while num[0] == '0':
54     alpha = alpha + num[0]
55     num = num[1:]
56     return (alpha, int(num))
57    
58     def SeperateAlpha2(bates):
59     """Same as SeperateAlpha but it has support for spaces in the bates"""
60     import string
61     alpha = []
62     for i in bates:
63     if i in string.letters:
64     alpha.append(i)
65     if i in string.punctuation:
66     alpha.append(i)
67     if i == " ":
68     alpha.append(i)
69     tempAlpha = ""
70     for x in alpha:
71     tempAlpha = tempAlpha+ x
72     alpha = tempAlpha
73     #alpha = string.join(alpha).replace(" ","")
74     if alpha == "":
75     num = bates
76     else:
77     num = bates.replace(alpha,"")
78     while num[0] == '0':
79     alpha = alpha + num[0]
80     num = num[1:]
81     return (alpha, int(num))
82    
83    
84 nino.borges 508 def SeperateAlpha3(bates):
85     """One seperator to rule them all :-)"""
86 nino.borges 528
87 nino.borges 513 parts = filter(None,re.split(r'(\d+|\s+)',bates))
88 nino.borges 508 moreParts = SeperateAlpha(parts[-1])
89     num = moreParts[-1]
90     parts = parts[0:-1]
91     moreParts = moreParts[0:-1]
92     alpha = ""
93     for i in parts:
94     alpha = alpha + i
95     for x in moreParts:
96     alpha = alpha +x
97     return (alpha, int(num))
98    
99    
100 ninoborges 8 def generate_index(str):
101     """
102     Splits a string into alpha and numeric elements, which
103     is used as an index for sorting"
104     """
105     #
106     # the index is built progressively
107     # using the _append function
108     #
109     index = []
110     def _append(fragment, alist=index):
111     if fragment.isdigit(): fragment = int(fragment)
112     alist.append(fragment)
113    
114     # initialize loop
115     prev_isdigit = str[0].isdigit()
116     current_fragment = ''
117     # group a string into digit and non-digit parts
118     for char in str:
119     curr_isdigit = char.isdigit()
120     if curr_isdigit == prev_isdigit:
121     current_fragment += char
122     else:
123     _append(current_fragment)
124     current_fragment = char
125     prev_isdigit = curr_isdigit
126     _append(current_fragment)
127     return tuple(index)
128    
129    
130     def GetBatesPageCount(begNo, endNo):
131     begNoIndex = generate_index(begNo)
132     endNoIndex = generate_index(endNo)
133     matrix = zip(begNoIndex, endNoIndex)
134     for i in matrix:
135     try:
136     numb = int(i[1]) - int(i[0])
137     except:
138     pass
139     ## Now check to see if there was a hanging chad on the endDoc and account for it.
140     if len(endNoIndex) > len(begNoIndex):
141     ## first lets see if it's an int first. this will work if its begno foo-0001 endno foo0001_002
142     ## but not if the endno was foo0001_002a
143     if str(endNoIndex[-1]).isdigit():
144     numb=+endNoIndex[-1]
145     numb = numb - 1
146     else:
147     ## otherwise it's probable just a real handing chad like foo-0001a
148     numb =+1
149     return numb +1
150    
151     def EnumerateBates(begNo, endNo):
152     """Will return all the possible bates values in between begNo and endNo. Split Alpha needs to be fixed
153     for bates with spaces. This will error out if the begdoc or enddoc has a weird value like foo-001.a"""
154     pageCount = GetBatesPageCount(begNo, endNo)
155 nino.borges 527 begBates = SeperateAlpha3(begNo)
156     endBates = SeperateAlpha3(endNo)
157 ninoborges 8 prefix = begBates[0]
158     padding = ""
159     #reverse the string
160     for x in prefix[::-1]:
161     if x.isdigit():
162     padding = padding + x
163     prefix = prefix[:-1]
164     else:
165     break
166     padding = len(str(begBates[-1]) + padding)
167    
168     batesList = []
169     for i in range(begBates[-1], endBates[-1]+1):
170     batesList.append(prefix + str(i).zfill(padding))
171     if len(batesList) == pageCount:
172     return batesList
173     else:
174 nino.borges 528 #print begBates,endBates
175 nino.borges 563 ## My own exception that I can catch.
176     raise EnumError("Could not enumerate fully.")
177 nino.borges 528
178 ninoborges 8