-
Notifications
You must be signed in to change notification settings - Fork 0
/
nutrition.py
73 lines (48 loc) · 1.22 KB
/
nutrition.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import urlparse
import urllib
from bs4 import BeautifulSoup
class Food:
def __init__(self, name, fat, fatp, carb, carbp, protein, calorie):
self.name = str(name)
self.fat = convertToFloat(fat)
self.fatp = convertToFloat2(fatp)
self.carb = convertToFloat(carb)
self.carbp = convertToFloat2(carbp)
self.protein = convertToFloat(protein)
self.calorie = convertToFloat2(calorie)
self.id = -1
def __repr__(self):
try:
return self.name + " | Fat:" + str(self.fat) + "g | Carbs:" + str(self.carb) + "g | Protein:" + str(self.protein) +"g | Calories:" + str(self.calorie)
except:
print("UNICODE ERROR")
return "error"
def get_name(self):
return self.name
def get_fat(self):
return self.fat
def get_carbs(self):
return self.carb
def get_protein(self):
return self.protein
def get_calorie(self):
return self.calorie
def get_id(self):
return self.id
"""
==============================================
Extra functions used to parse strings into floats
=============================================
"""
def convertToFloat(obj):
try:
obj= float(obj[:len(obj)-1])
except:
return None
return obj
def convertToFloat2(obj):
try:
obj = float(obj)
except:
return None
return obj