codebarre.py

Created by nicolas-patrois

Created on August 29, 2019

1.11 KB

codebarre(“5011386029399”) affiche le code à barres EAN13 correspondant.

Attention, un code à barres erroné est refusé.


from kandinsky import set_pixel,draw_string

def codebarre(ch="5011386029399"):
  s=0
  for i,e in enumerate(ch):
    s+=int(e)*(1+2*(i%2))
    s%=10
  if s!=0:
    raise ValueError("Code errone")

  codes={"L":["0001101","0011001","0010011","0111101","0100011",
              "0110001","0101111","0111011","0110111","0001011"]}
  codes["R"]=["".join({"0":"1","1":"0"}[c] for c in l) for l in codes["L"]]
  codes["G"]=["".join(l[-i-1] for i in range(7)) for l in codes["R"]]
  choix=["LLLLLL","LLGLGG","LLGGLG","LLGGGL","LGLLGG",
         "LGGLLG","LGGGLL","LGLGLG","LGLGGL","LGGLGL"]

  barres="101"
  gauche=choix[int(ch[0])]
  for i in range(1,7):
    barres+=codes[gauche[i-1]][int(ch[i])]
  barres+="01010"
  for i in range(7,13):
    barres+=codes["R"][int(ch[i])]
  barres+="101"

  i=53
  for j,e in enumerate(ch):
    draw_string(e,i,162)
    if j in {0,6}:
      i+=21
    else:
      i+=14
  for i,e in enumerate(barres):
    if e=="1":
      if i in {0,2,46,48,92,94}:
        l=111
      else:
        l=100
      for j in range(l):
        set_pixel(65+2*i,j+60,(0,0,0))
        set_pixel(66+2*i,j+60,(0,0,0))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.