I had written a Python application that had a number of classes. Let's say one of them was something like this (oversimplified - but I have tried to represent the type of functionality):
class PythonXyz(object):
def __init__(self):
self.x = []
self.y = []
self.z = []
def append(self, x, y):
self.x.append(x)
self.y.append(y)
def get_x(self):
return self.x
def get_y(self):
return self.y
def get_z(self):
return self.x + self.y
def size(self):
return len(self.x)
def dump(self):
#generate some output
def do_some_complex_stuff(self, q):
#lots of calculations and manipulation of the self.x and self.y lists.
def save(self, filename):
#some code that saves data to disk
def load(self, filename):
#some code that loads data from disk
When the app worked, but lacked in useability, I decided to djangofy the app, so that I can use the browser as a poor man's GUI and use the database goodness as well. So I made a model.py like so:
class DjangoXyzModel(models.model):
x = models.FloatField()
y = models.FloatField()
And I modified the load() and save() methods in the PythonXYZ class to use the database rather than a file, and made some views to work via the browser.
Now I ended up with 3 Django apps, [edit]with different db schemas[/edit], each with its own models.py file with several model classes and in addition to that my original code in a separate folder. I feel this is all getting very messy and it ocurred to me that it would be much cleaner design to completely integrate all the PythonXyz methods into the DjangoXyzModel class, for example:
class DjangoXyzModel(models.model):
x_db = models.FloatField()
y_db = models.FloatField()
def init_lists(self):
self.x = []
self.y = []
self.z = []
def append(self, x, y):
self.x.append(x)
self.y.append(y)
def get_x(self):
return self.x
def get_y(self):
return self.y
def get_z(self):
return self.x + self.y
def size(self):
return len(self.x)
def dump(self):
#generate some output
def do_some_complex_stuff(self, q):
#lots of calculations and manipulation of the self.x and self.y lists.
def save_to_db(self):
#some code that saves x,y lists to the database
def load_from_db(self, filename):
#some code that loads x,y lists from the database
My question is: would this approach be considered 'pollution' of the Django models class, or is this ok and just a matter of personal taste, or is this maybe exactly how the models class is supposed to be used? If this is not recommended, or frowned upon, what would be a better way to deal with the duplication of classes?
Note, the obvious(?) solution to get rid of the lists alltogether and work straight from the database is not acceptable, because I need the lists in memory for quicker access. E.g. the lists are loaded from the database into memory once and accessed hundreds or thousands of times. Each list may be > 1000 items, so bulk read/write is also be much faster than individual access when a value is needed (it is often not sequential). In addition, often data is modified several times before finally being committed to the database.