-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheanparser.py
52 lines (45 loc) · 1.64 KB
/
eanparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class EanParser:
def __init__(self, eanstring):
if len(eanstring) != 13 and len(eanstring) != 24:
raise ValueError('Length of EAN is not 13 or 23')
if not eanstring.isdigit():
raise ValueError('EAN is not a numeric string')
self.ean = eanstring
def isownproduct(self):
if self.ean[:2] == '84' and len(self.ean) == 13:
return True
return False
def isvariableweightproduct(self):
if self.ean[:2] == '23' and len(self.ean) == 13:
return True
return False
def isbulkproduct(self):
if self.ean[:2] == '23' and len(self.ean) == 24:
return True
return False
def getmcode(self):
if self.isownproduct():
return int(self.ean[7:12])
elif self.isvariableweightproduct():
return int(self.ean[2:7])
elif self.isbulkproduct():
return int(self.ean[3:8])
else:
raise ValueError('EAN does not belong to: own, variable weight, or bulk')
def getprice(self):
if self.isvariableweightproduct():
return int(self.ean[7:12])/100
elif self.isbulkproduct():
return int(self.ean[18:23])/100
else:
raise ValueError('EAN does not belong to variable weight or bulk prod')
def getpvp(self):
if self.isbulkproduct():
return int(self.ean[13:18])/100
else:
raise ValueError('EAN is not bulk prod')
def getweight(self):
if self.isbulkproduct():
return int(self.ean[8:13])/100
else:
raise ValueError('EAN is not bulk prod')