first allow me to explain the what the purpose of this program is: a "barcode" as I'm calling it consists of three parts:
- Serial Number: a string of 9-digits
- mod 10 digit: A digit (as a chr()) between 0-9 that is assigned randomly to the end of a barcode.
So a full barcode would look like this:
'ATX9784634072': (ATX=plan_id, '978463407'=serial_number, and '2' is the mod 10 digit)
Note: for each serial number there can only be one barcode. So in this case there are no other barcodes with the serial number 978463407. The plant ID simply indicates the plant that manufactured the item. Thus for each serial number there are 3*10=30 possible barcodes and only one is valid.
Any other barcode with serial number 978463407 will be invalid regardless of it's pland_id value or its mod_10 digit value.
Thus far I have an iterator class called BarcodeIterator that only iterates over the serial numbers in a given range (start and stop arguments).
Then I have a generator function bar_code_generator, that takes an instance of BarcodeIterator() as a parameter to generate the full bar codes.
## Let me begin by explaining the structure of the barcodes in question ## The barcode is made up of three parts. The Plant ID, ## The serial number, then a mod_10 digit. (For purposes of this ## code, I'm going to assume this is a random number between 1 and 10 ## Example: 'ATX9784634068', Plants=['ATX','HOU','DFW'] s_num0='978463406' s_num1='978463407' s_num2='978463408' class BarcodeIterator: def __init__(self,barcode,start,stop, plant_ID=['ATX','HOU','CHS']): self.plant=barcode[:3] self.serial_number=barcode[3:12] self.mod_10=barcode[-1] self.start = int(self.serial_number)-start-1 self.stop = int(self.serial_number)+stop self.plant_ID=plant_ID def __next__(self): self.start+=1 if self.start>self.stop: raise StopIteration else: start_string=str(self.start) result=start_string.zfill(9) return result def __iter__(self): return self def bar_code_generator(iterObj,plant_ID=['ATX','HOU','CHS']): for bc in iterObj: for p_id in plant_ID: def get_mod(): x0='0' while x0<='9': yield x0 x0=chr(ord(x0)+1) for digit in get_mod(): barcode_tuple=(p_id,bc,digit) barcode_string=''.join(barcode_tuple) yield barcode_string ##class Barcode: '''going to be my iterable class''' ## def __init__(self, barcode): ## self.barcode = barcode ## self.plant_id=barcode[:3] ## .... ## ## def __iter__(self): ## return BarcodeIterator( if __name__=='__main__': bc_iter=BarcodeIterator(s_num0,1,1) bc_gen=bar_code_generator(bc_iter) bc_list=[bc for bc in bc_gen] print(bc_list) and the result is this: ['ATX0004634050', 'ATX0004634051', 'ATX0004634052', 'ATX0004634053' , 'ATX0004634054', 'ATX0004634055', 'ATX0004634056', 'ATX0004634057' , 'ATX0004634058', 'ATX0004634059', 'HOU0004634050', 'HOU0004634051' , 'HOU0004634052', 'HOU0004634053', 'HOU0004634054', 'HOU0004634055' , 'HOU0004634056', 'HOU0004634057', 'HOU0004634058', 'HOU0004634059' , 'CHS0004634050', 'CHS0004634051', 'CHS0004634052', 'CHS0004634053', . . . it continues
Finally, my question (or plea for help) is this:
I want to:
clean this code up a lot: I would really like to have a BarcodeIterator() object and a Barcode() object (the iterable). So that each time I call an instance of Barcode() it returns the next valid Barcode... which brings me to my second question
As I have mentioned twice. There is only one valid barcode per serial number (hence, 30 possible barcodes). I haven't put an isValid() function in the scrip because I don't know where it would go.
I want to see each time the Barcode() object is called, it yields the next valid barcode without having to create a new object (if possible).
I hope this was clear. Any help will be greatly appreciated.
Please post here, but if you have vast experience in python and want to work with me on this you are more than welcome to pm me.