Re: code128.py

Robin Becker <[email protected]>
Newsgroups gmane.comp.python.reportlab.user
Message-ID <[email protected]>
On 11/01/2016 16:19, Klaas Feenstra wrote:
> Hi Robin,
>
> Maybe we should first break the list in separate list starting with START_X
> or TO_X and STOP.
> Then check each list, if last 4 position are digits, we do first a backward
> iteration until the first break. So we divide the list in 2 parts, right
> list and left list. After the backward iteration,until first break, we
> continue from the left side (and we remove the right part from the list.)
> If the list is an even number of digits, all string is converted to C in
> the first iteration run.

H Klaas,

I believe I have you ends at stop case covered. See the algorithm below.

1) I believe we only need to check the special case when we break at a non 
start. If we break at a start we have to do the start and we won't see a TO_C if 
we do this algorithm

2) we apply the backward algorithm to i, i-1, ..... startpos+1. Startpos cannot 
be part of the digits. We compute rsavings and then compare with savings.

3) In the comparison the savings ta has to be 1 because l[i] is not STOP and we 
didn't stop at a start that means we test

rsavings > savings+int(savings>=0 and (startpos and nl[-1] in starts))-1

and if this is true we fix up the values of rl, nl, startpos and i so it looks 
like we did the forward algorithm starting at startpos + 1. That means we will 
eliminate the dangling digit. When I run this algorithm I find it applies twice 
on the expanded data set.

Can you test this and see if it works for you?

###########################################################################
from string import digits

starts = ['START_B','TO_A','TO_B']
def _trailingDigitsToC(self, l):
	# Optimization: trailing digits -> set C double-digits

	i = 0
	nl = []
	while i < len(l):
		startpos = i
		rl = []
		savings = -1 # the TO_C costs one character
		while i < len(l):
			if l[i] in starts:
				j = i
				break
			elif l[i] == '\xf1':
				rl.append(l[i])
				i += 1
				continue
			elif l[i] in digits \
				and l[i+1] in digits:
				rl.append(l[i] + l[i+1])
				i += 2
				savings += 1
				continue
			else:
				if l[i] in digits and l[i+1]=='STOP':
					rrl = []
					rsavings = -1	#we need a TO_C
					k = i
					while k>startpos:
						if l[k]=='\xf1':
							rrl.append(l[i])
							k -= 1
						elif l[k] in digits and l[k-1] in digits:
							rrl.append(l[k-1]+l[k])
							rsavings += 1
							k -= 2
						else:
							break
					rrl.reverse()
					if rsavings>savings+int(savings>=0 and (startpos and nl[-1] in starts))-1:
						nl += l[startpos]
						startpos += 1
						rl = rrl
						del rrl
						i += 1
				break
		ta = not (l[i]=='STOP' or j==i)
		xs = savings>=0 and (startpos and nl[-1] in starts)
		if savings+int(xs) > int(ta):
			if xs:
				toc = nl[-1][:-1]+'C'
				del nl[-1]
			else:
				toc = 'TO_C'
			nl += [toc]+rl
			if ta:
				nl.append('TO'+l[j][-2:])
			nl.append(l[i])
		else:
			nl += l[startpos:i+1]
		i += 1
	return nl

for x,l in enumerate((
		'START_B b b b b 1 2 3 4 5 6 b b b STOP',
		'START_B b b b b 1 2 3 4 5 b b b STOP',
		'START_B b b b b 1 2 3 4 b b b STOP',
		'START_B b b b b 1 2 3 b b b STOP',
		'START_B b b b b 1 2 b b b STOP',
		'START_B b b b b 1 b b b STOP',
		'START_B b b b b 1 2 3 4 5 6 TO_A a a STOP',
		'START_B b b b b 1 2 3 4 TO_A a a STOP',
		'START_B b b b b 1 2 3 TO_A a a STOP',
		'START_B b b b b 1 2 TO_A a a STOP',
		'START_B b b b b 1 TO_A a a STOP',
		'START_B b b b b 1 2 3 4 5 6 STOP',
		'START_B b b b b 1 2 3 4 5 STOP',
		'START_B b b b b 1 2 3 4 STOP',
		'START_B b b b b 1 2 3 STOP',
		'START_B b b b b 1 2 STOP',
		'START_B b b b b 1 STOP',
		'START_B \xf1 1 2 3 4 b STOP',
		'START_B b TO_A a TO_B \xf1 1 2 3 4 b STOP',
		'START_B b TO_A a TO_B 1 2 STOP',
		'START_B b TO_A a TO_B 1 2 3 b STOP',
		'START_B b TO_A a TO_B 1 2 3 4 b STOP',
		'START_B b b b b 1 2 3 4 5 6 7 STOP',
		'START_B b b b b 1 2 3 4 5 6 7 TO_A a a STOP',
		)):
	l = l.split()
	lo = _trailingDigitsToC(None, l)
	print '%2d: %s(%d) --> %s(%d)' % (x,' '.join(l),len(l),' '.join(lo),len(lo))
###########################################################################
-- 
Robin Becker
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.