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: 8
Committed: Sat May 5 04:21:19 2012 UTC (13 years, 10 months ago) by ninoborges
Content type: text/x-python
File size: 5054 byte(s)
Log Message:
Initial Import

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     def generate_index(str):
76     """
77     Splits a string into alpha and numeric elements, which
78     is used as an index for sorting"
79     """
80     #
81     # the index is built progressively
82     # using the _append function
83     #
84     index = []
85     def _append(fragment, alist=index):
86     if fragment.isdigit(): fragment = int(fragment)
87     alist.append(fragment)
88    
89     # initialize loop
90     prev_isdigit = str[0].isdigit()
91     current_fragment = ''
92     # group a string into digit and non-digit parts
93     for char in str:
94     curr_isdigit = char.isdigit()
95     if curr_isdigit == prev_isdigit:
96     current_fragment += char
97     else:
98     _append(current_fragment)
99     current_fragment = char
100     prev_isdigit = curr_isdigit
101     _append(current_fragment)
102     return tuple(index)
103    
104    
105     def GetBatesPageCount(begNo, endNo):
106     begNoIndex = generate_index(begNo)
107     endNoIndex = generate_index(endNo)
108     matrix = zip(begNoIndex, endNoIndex)
109     for i in matrix:
110     try:
111     numb = int(i[1]) - int(i[0])
112     except:
113     pass
114     ## Now check to see if there was a hanging chad on the endDoc and account for it.
115     if len(endNoIndex) > len(begNoIndex):
116     ## first lets see if it's an int first. this will work if its begno foo-0001 endno foo0001_002
117     ## but not if the endno was foo0001_002a
118     if str(endNoIndex[-1]).isdigit():
119     numb=+endNoIndex[-1]
120     numb = numb - 1
121     else:
122     ## otherwise it's probable just a real handing chad like foo-0001a
123     numb =+1
124     return numb +1
125    
126     def EnumerateBates(begNo, endNo):
127     """Will return all the possible bates values in between begNo and endNo. Split Alpha needs to be fixed
128     for bates with spaces. This will error out if the begdoc or enddoc has a weird value like foo-001.a"""
129     pageCount = GetBatesPageCount(begNo, endNo)
130     begBates = SeperateAlpha(begNo)
131     endBates = SeperateAlpha(endNo)
132     prefix = begBates[0]
133     padding = ""
134     #reverse the string
135     for x in prefix[::-1]:
136     if x.isdigit():
137     padding = padding + x
138     prefix = prefix[:-1]
139     else:
140     break
141     padding = len(str(begBates[-1]) + padding)
142    
143     batesList = []
144     for i in range(begBates[-1], endBates[-1]+1):
145     batesList.append(prefix + str(i).zfill(padding))
146     if len(batesList) == pageCount:
147     return batesList
148     else:
149     raise "Could not enumerate fully."
150