My knowledge of Python and of programming in general is self-taught from online sources - just enough to make me dangerous.
I'm working on my first major Python project, and it's going well in some ways, but when my initial file reached 1000 lines, I decided I needed to break it up into different files (er.. "modules"). So now it's in 5 modules divided up by subject - sort of. The smallest is 49 lines - the "main" file is still 960, which is still on the verge of being unwieldy. But my primary question is about classes and constants defined in them.
In the interest of a made-up example, this is how I would like to use Python:
But instead it turns into something nasty like:
Obviously I just don't get it and I'm doing it wrong. Help! Thanks.
I'm working on my first major Python project, and it's going well in some ways, but when my initial file reached 1000 lines, I decided I needed to break it up into different files (er.. "modules"). So now it's in 5 modules divided up by subject - sort of. The smallest is 49 lines - the "main" file is still 960, which is still on the verge of being unwieldy. But my primary question is about classes and constants defined in them.
In the interest of a made-up example, this is how I would like to use Python:
Code:
class HappyFastMenu(object):
class VomitError(Exception):
pass
OMNIVORE = object()
CATHOLIC = object()
# structure offsets
DRINK = 0
VEGETABLE = 1
MEAT = 2
def GetMeal(self, expensive):
r = ["Nothing" for i in xrange(3)]
r[VEGETABLE] = WhatsInSeason()
if expensive:
r[DRINK] = "Wine"
r[MEAT] = "Steak"
else:
r[DRINK] = "Water"
r[MEAT] = "Canned sardines"
return r
class MealListAquisition(object):
# flags
Finished = 1
GoWild = 2
FridayFish = 4
def __init__(self, themenu, opts, kind=OMNIVORE):
self.flags = 0
if "Money is no object" in opts:
self.flags |= GoWild
if id(kind) == id(CATHOLIC):
self.flags |= FridayFish
self.obj = themenu
self.state = []
def ProcessNext(self):
if len(self.state) < 7:
meal = self.obj.GetMeal(self.flags & GoWild )
if meal[VEGETABLE] == "Artichokes" and meal[DRINK] == "Pepsi":
raise VomitError("That's no good!", meal)
self.state.append(meal)
else:
self.flags |= Finished
def IsDone(self):
return self.flags & Finished
def Result(self):
pass
BobsMenu = HappyFastMenu()
meals_aqu = HappyFastMenu.MealListAquisition(BobsMenu, [])
while not meals_aqu.IsDone():
meals_aqu.ProcessNext()
BobsMeals = meals_aqu.Result()
But instead it turns into something nasty like:
Code:
class HappyFastMenu(object):
class VomitError(Exception):
pass
OMNIVORE = object()
CATHOLIC = object()
# structure offsets
DRINK = 0
VEGETABLE = 1
MEAT = 2
def GetMeal(self, expensive):
cls = HappyFastMenu
r = ["Nothing" for i in xrange(3)]
r[cls.VEGETABLE] = WhatsInSeason()
if expensive:
r[cls.DRINK] = "Wine"
r[cls.MEAT] = "Steak"
else:
r[cls.DRINK] = "Water"
r[cls.MEAT] = "Canned sardines"
return r
class MealListAquisition(object):
# flags
Finished = 1
GoWild = 2
FridayFish = 4
def __init__(self, themenu, opts, kind=???): # How to get OMNIVORE?
cls = HappyFastMenu.MealListAquisition
supercls = HappyFastMenu
self.flags = 0
if "Money is no object" in opts:
self.flags |= cls.GoWild
if id(kind) == id(supercls.CATHOLIC):
self.flags |= cls.FridayFish
self.obj = themenu
self.state = []
def ProcessNext(self):
cls = HappyFastMenu.MealListAquisition
supercls = HappyFastMenu
if len(self.state) < 7:
meal = self.obj.GetMeal(self.flags & cls.GoWild )
if meal[supercls.VEGETABLE] == "Artichokes" and meal[supercls.DRINK] == "Pepsi":
raise supercls.VomitError("That's no good!", meal)
self.state.append(meal)
else:
self.flags |= cls.Finished
def IsDone(self):
return self.flags & HappyFastMenu.MealListAquisition.Finished
def Result(self):
pass
BobsMenu = HappyFastMenu()
meals_aqu = HappyFastMenu.MealListAquisition(BobsMenu, [])
while not meals_aqu.IsDone():
meals_aqu.ProcessNext()
BobsMeals = meals_aqu.Result()
Obviously I just don't get it and I'm doing it wrong. Help! Thanks.