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: 513
Committed: Fri Jan 17 21:43:00 2014 UTC (12 years, 2 months ago) by nino.borges
Content type: text/x-python
File size: 5473 byte(s)
Log Message:
save

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