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: 528
Committed: Thu Mar 13 17:21:57 2014 UTC (12 years ago) by nino.borges
Content type: text/x-python
File size: 5519 byte(s)
Log Message:
small print tweak

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