This paste expires on 2023-04-13 10:13:47.891929. Repaste, or download this paste. . Pasted through v1-api.

#!/usr/bin/python
# vim: noet nowrap number ts=4
import barcode
#import bytes_as_braille as bab
barcode.Code128.default_writer_options = {
"module_width": 0.2,
"module_height": 10.0,
"quiet_zone": 6.5,
"font_size": 10,
"text_distance": 3.0,
"background": None,
"foreground": "black",
"write_text": True,
"text": "",
}
def mkBar( byte_content, ASCII = True ):
if not ASCII:
string_content = str(int.from_bytes(byte_content,'big'))
else:
try:
try:
int(byte_content)
print("raising UnicodeDecodeError")
raise UnicodeDecodeError('0',b'0', False, False, '0')
print("raising UnicodeDecodeError failed (this is NOT displayed, as expected)")
except ValueError:
string_content =  byte_content.decode('ascii')
except UnicodeDecodeError:
#except:
print("converting to int")
string_content = str(int.from_bytes(byte_content,'big'))
print(f"Encoded: {string_content}")
b = barcode.Code128( string_content )
#print(b.default_writer_options['background'])
b.write(open('/tmp/barcode.svg','wb'), text = byte_content.decode('utf-8'))
if __name__ == '__main__':
#while True:
# #mkBar( bab.input() )
# mkBar( input('> ').encode('utf-8') )
# all good
mkBar(b'gdgdggdgd')
mkBar('ddddddé'.encode('utf-8'))
# why is "converting to int" not being displayed?!?!" that except clause is not being used!
mkBar(b'12345689')
Filename: mk_barcodes.py. Size: 1kb. View raw, , hex, or download this file.