Further cleanup of sequence generation.
Changed rollover warning to a InfoMsg() call.
1 parent 68f94a1 commit bbdd183ee00f81fc0085af07f9d455206d3b450d
@tundra tundra authored on 12 Mar 2010
Showing 1 changed file
View
29
tren.py
 
PROGNAME = "tren.py"
BASENAME = PROGNAME.split(".py")[0]
PROGENV = BASENAME.upper()
RCSID = "$Id: tren.py,v 1.188 2010/03/12 03:04:12 tundra Exp $"
RCSID = "$Id: tren.py,v 1.189 2010/03/12 22:19:29 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
 
iRENFORCED = "Target '%s' Exists. Creating Backup."
iRENSKIPPED = "Target '%s' Exists. Renaming Of '%s' Skipped."
iRENAMING = "Renaming '%s' " + ARROW + " '%s'."
iSEQTOOLONG = "Sequence number %s, longer than format string %s, Rolling over!"
 
 
#####
# Usage Prompts
#####
 
 
def ComputeSeqString(fmt, incr, alphabet):
 
"""
fmt = A literal "format field" string
incr = A integer to be "added" to the field
alphabet = The alphabet of characters to use, in ascending order
 
Add 'incr' to 'fmt' in base(len(alphabet)). Characters in
'fmt' that are not in 'alphabet' are ignored in this addition.
 
The final result is limited to be no longer than 'fmt'. Any
result longer than fmt has MSD dropped, thereby effectively
rolling over the count. If 'fmt' is null on entry, the final
result length is unlimited.
"""
fmtlen = len(fmt)
alphalen = len(alphabet)
newval = ""
carry = None
# format string will be in the alphabet.
 
# First convert the increment into a string in the base of the
# alphabet.
# alphabet
 
idigits = []
while incr > alphalen-1:
 
idigits.append(alphabet[incr])
idigits.reverse()
incr = "".join(idigits)
 
# Now to right-to-left digit addition with the format
# Now do right-to-left digit addition with the format
# field.
 
# Do position-wise "addition" via symbol substitution moving from
# right-to-left. Take into account that the format pattern string
sum += alphabet.index(fmt[i])
 
sum += alphabet.index(incr[i])
 
# Do arithmetic modulo alphabet length.
# Do arithmetic modulo alphabet length
carry, digit = sum/alphalen, sum % alphalen
 
if not carry:
if newval != alphabet[0]:
while newval and newval[0] == alphabet[0]:
newval = newval[1:]
 
 
# Constrain the results to the length of the original format
# string, rolling over and warning the user as necessary. The one
# exception to this is when a null format string is passed. This
# is understood to mean that sequences of any length are
 
if fmtlen:
if len(newval) > fmtlen:
print "Result longer than pattern, rolling over"
InfoMsg(iSEQTOOLONG % (newval,fmt))
newval = newval[-fmtlen:]
 
return fmt[:-len(newval)] + newval