-2

I'm trying to change this python 2 class to make it works in python 3 But unfortunatly i'm stuck with comprehension list

Here is the original class:

class GTIN(object):

def __init__(self, barcode=''):
    self.barcode = barcode

def __checkDigit(self, digits):
        total = sum(digits) + sum(map(lambda d: d*2, digits[-1::-2]))
        return (10 - (total % 10)) % 10

def validateCheckDigit(self, barcode=''):
    barcode = (barcode if barcode else self.barcode)
    if len(barcode) in (8,12,13,14) and barcode.isdigit():
        digits = map(int, barcode)
        checkDigit = self.__checkDigit( digits[0:-1] )
        return checkDigit == digits[-1]
    return False

def addCheckDigit(self, barcode=''):
    barcode = (barcode if barcode else self.barcode)
    if len(barcode) in (7,11,12,13) and barcode.isdigit():
        digits = map(int, barcode)
        return barcode + str(self.__checkDigit(digits))
    return ''

Here is where am I with my noob skill

class GTIN(object):

def __init__(self, barcode=''):
    self.barcode = barcode

def __checkDigit(self, digits):
        #total = sum(digits) + sum(map(lambda d: d*2, digits[-1::-2]))
        total = sum(digits) + sum([d*2 for d in digits[-1::-2]])
        return (10 - (total % 10)) % 10

def validateCheckDigit(self, barcode=''):
    barcode = (barcode if barcode else self.barcode)
    if len(barcode) in (8,12,13,14) and barcode.isdigit():
        #digits = map(int, barcode)
        digits = (int(s) for s in barcode)
        checkDigit = self.__checkDigit( digits[0:-1] )
        return checkDigit == digits[-1]
    return False

def addCheckDigit(self, barcode=''):
    barcode = (barcode if barcode else self.barcode)
    if len(barcode) in (7,11,12,13) and barcode.isdigit():
        #digits = map(int, barcode)
        digits = (int(s) for s in barcode)
        return barcode + str(self.__checkDigit(digits))
    return ''

I changed all map fonction by comprehension list but now i'm stuck with this error

TypeError: 'generator' object is not subscriptable
Andronaute
  • 379
  • 3
  • 12
  • 1
    Why did you change all applications of `map` with list comprehensions? Where does the error appear? Do you know [what it means](http://stackoverflow.com/questions/6288016/generator-object-is-not-subscriptable-error)? – Michael Foukarakis Feb 22 '16 at 11:56
  • It's my mistake sry i dit not noticed the () on my comprehension list ! Fixed now – Andronaute Feb 22 '16 at 12:13

1 Answers1

1

The problem is here:

digits = (int(s) for s in barcode)
return barcode + str(self.__checkDigit(digits))

and later, in __checkDigit, you do

total = sum(digits) + sum([d*2 for d in digits[-1::-2]])

For digits[-1::-2] to work, digits has to be a list, or some other kind of "subscriptable". In Python 3, map returns a generator, so you changed it. But you changed it to just another kind of generator -- a generator expression.

You can either use a proper list comprehension (note the square brackets):

digits = [int(s) for s in barcode]

or stick with map, but turn the generator into a list:

digits = list(map(int, barcode))
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • Addendum: In the general case, another alternative might be using [`itertools.islice`](https://docs.python.org/3/library/itertools.html#itertools.islice) instead of `[-1::-2]`, but `islice` does not support negative values for start, stop and step. – tobias_k Feb 22 '16 at 12:05
  • actually , It's my mistake i noticed that later i did not used well comprehension list, arghhh !!! Thanks for your support – Andronaute Feb 22 '16 at 12:15