How should I create an SQLAlchemy model based on another class? - python

I have a regular class Something that basically just holds some fields. I need to make an SQLAlchemy model SomethingModel (in the ORM) that represents a Something-like object, with a couple extra fields (primary key, for example). What's the best way to do this?
So far, I've thought of having SomethingModel inherit from Something, but then I'm using multiple inheritance which I've heard is bad (SomethingModel would be inheriting from Something and SQLAlchemy's Base). I also thought that I could simply call Something.__init__ from within SomethingModel.__init__ - would that be better?
I am aware that I'll still need SQLAlchemy's column fields for the fields of Something that I want to save in SomethingModel. This also seems to make things a bit messier.
What's the "best" way to accomplish this?

Use classical mapping for your class

Related

Python NDB: what's the best way to have a Set instead of a List in property?

in NDB you have repeated properties, they behave just like a native python list but i want them to behave just like native sets.
i need to have a set of keys that is without duplicates.
in python you can remove duplicates like the_list = list(set(the_list)),
but how would you implement this so it is automatic and i don't to think about this ?
Three ways come to mind:
enforce the list (repeated property) is unique with a "setter" method that only inserts unique values;
likewise, enforce the list is unique with a _pre_put_hook() method;
use the key on each entity as your list, ndb will make sure they are unique.
Another option would be to subclass ndb.Property. Quite a few examples here:
https://cloud.google.com/appengine/docs/python/ndb/subclassprop
I believe the correct strategy would to build a custom SetProperty which subclasses the ListProperty, to enforce your requirements.
Have a read of up on Subclassing properties. https://cloud.google.com/appengine/docs/python/ndb/subclassprop
This I believe is the correct way for implementing this type of property, rather than _pre_put hooks. That is generally too late to perform appropriate validation and feedback.
You could write custom setters, however you setter can't be the name of the property, so this will look odd.
The other alternative would be to use a validator which is allowed to coerce the value. See https://cloud.google.com/appengine/docs/python/ndb/properties#options

How to create a dict like class in python 2.7?

Looks like there are multiple ways to do that but couldn't find the latest best method.
Subclass UserDict
Subclass DictMixin
Subclass dict
Subclass MutableMapping
What is the correct way to do? I want to abstract actual data which is in a database.
Since your dict-like class isn't in fact a dictionary, I'd go with MutableMapping. Subclassing dict implies dict-like characteristics, including performance characteristics, which won't be true if you're actually hitting a database.
If you are doing your own thing (e.g. inventing your own wheel) you might as well write the class from scratch (i.e. subclass from object), just providing the correct special members (e.g. __getitem__) and other such functions as described in the object data model, so that it quacks like a dict. Internally, you might even own a number of dicts (has-a) to help your implementation.
This way, you don't have to shoehorn your design to "fit" some existing implementation, and you aren't paying for some things you aren't necessarily using .This recommendation in part is because your DB-backed class will already be considerably more complex than a standard dict if you make it account for performance/caching/consistency/optimal querying etc.

Python: Data Object or class

I enjoy all the python libraries for scraping websites and I am experimenting with BeautifulSoup and IMDB just for fun.
As I come from Java, I have some Java-practices incorporated into my programming styles. I am trying to get the info of a certain movie, I can either create a Movie class or just use a dictionary with keys for the attributes.
My question is, should I just use dictionaries when a class will only contain data and perhaps almost no behaviour? In other languages creating a type will help you enforce certain restrictions and because of type checks the IDE will help you program, this is not always the case in python, so what should I do?
Should I resort to creating a class only when there's both, behaviour and data? Or create a movie class even though it'll probably be just a data container?
This all depends on your model, in this particular case either one is fine but I'm wondering about what's a good practice.
It's fine to use a class just to store attributes. You may also wish to use a namedtuple instead
The main differences between dict and class are the way you access the attributes [] vs . and inheritence.
instance.__dict__ is just a dict after all
You can even just use a single class for all of those types of objects if you wish
class Bunch:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
movie = Bunch(title='foo', director='bar', ...)
In your case you could use a class that inherits from dict (e.g class MyClass(dict)) so that you can define custom behavior to your dict-like class or use UserDict.
It depends on what you really mean for "perhaps almost no behaviour", if dict already provides what you need stay with it. Otherwise consider to subclass dict adding your specific behaviour. Since Python 2.2 it is possible. Using UserDict is an older approach to the problem.
You could also use a plain dictionary and implement the behaviour externally via some function. I use this approach for prototyping, and eventually refactor the code later to make it Object Oriented (generally more scalable).
You can see what a dictionary offers typing this at the interpreter:
>>> help({})
or referring to the docs.
I would stick to KISS (Keep it simple stupid). If you only want to store values you are better off with a dictionary, because you can dynamically add values at runtime. WRONG:(But you can not add new filds to a class at runtime.)
So classes are useful if they provide state and behaviour.
EDIT: You can add fields to classes in python.

Should I add methods to my classes that inherit db.Model, or should I inherit those classes into a new class?

When working with classes that inherit db.Model, is it better practice to add methods, or should I instead create a separate class?
E.g., if I want to store information on posts, should I have Post extend db.Model, or should I have PostData extend db.Model and Post extend (or even reference?) PostData?
The difference, I think, is that classes that inherit db.Model won't create instances without all the required attributes. The behaviour I'd like to see is not saving to the datastore without required attributes. Which is cleaner? Which is preferred?
Adding methods to db.Model subclasses is perfectly fine practice. There's only any point in having your actual model subclass something that is itself a db.Model subclass if you have common functionality you want shared by several model classes - just like in standard inheritance.
I'm not sure how your proposed approach would help with "not saving to the datastore without required attributes", unless you're planning on creating your own data models that you translate to and from datastore models - which is just going to be a huge waste of time (both yours and the processor's). The way the datastore library works, it's not possible to create a model with values that don't validate, and I'm not sure why you'd want to.
Tip: use the business logic separated from the data store model
I think you should use your models as they are, in the purest form.
You can make other handlers to use those as explicit types.
It is some cleaner not to inherit them. Just think to the data connection. You can use your models either in disconnected or in connected state.
In ruby I would use mixins or the concerned_with pattern on the models
In python I advice to use Django, so your views may contain the most part of the business logic. http://www.djangobook.com/en/1.0/chapter05/
Try to use mixins!
A mixin class is a way of using the inheritance capabilities of classes to compose a class out of smaller pieces of behavior.
https://docs.djangoproject.com/en/dev/ref/class-based-views/
Another way is to use composition (usually the better alternative to inheritance). e.g.
class MyModel(db.Model): pass
# Avoiding inheritance.
class MyWrapper(object):
def __init__(self, my_model):
# The leading _ indicates that methods in this class should
# access self._my_model.
self._my_model = my_model
Many would consider this to be unnecessary indirection, and I don't blame those people. Still, this might be good if you want to constrain MyModel instances in a way that the validator parameter in db.Property does not support. E.g.
class MyModel(db.Model):
# f(x, y, z) = 0
x = db.FloatProperty()
y = db.FloatProperty()
z = db.FloatProperty()
Enforcing the constraint without the help of MyWrapper is going to be harder.
Another reason to go with a wrapper is that you want to implement your own caching scheme, although ndb solves this in a more general way. When MyModel can only be manipulated through MyWrapper, then you can control what mutations are possible. Then, you can take care to invalidate cache entries as necessary.

Can django lazy-load fields in a model?

One of my django models has a large TextField which I often don't need to use. Is there a way to tell django to "lazy-load" this field? i.e. not to bother pulling it from the database unless I explicitly ask for it. I'm wasting a lot of memory and bandwidth pulling this TextField into python every time I refer to these objects.
The alternative would be to create a new table for the contents of this field, but I'd rather avoid that complexity if I can.
The functionality happens when you make the query, using the defer() statement, instead of in the model definition. Check it out here in the docs:
http://docs.djangoproject.com/en/dev/ref/models/querysets/#defer
Now, actually, your alternative solution of refactoring and pulling the data into another table is a really good solution. Some people would say that the need to lazy load fields means there is a design flaw, and the data should have been modeled differently.
Either way works, though!
There are two options for lazy-loading in Django: https://docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.QuerySet.only
defer(*fields)
Avoid loading those fields that require expensive processing to convert them to Python objects.
Entry.objects.defer("text")
only(*fields)
Only load the fields that you actually need
Person.objects.only("name")
Personally, I think only is better than defer since the code is not only easier to understand, but also more maintainable in the long run.
For something like this you can just override the default manager. Usually, it's not advised but for a defer() it makes sense:
class CustomManager(models.Manager):
def get_queryset(self):
return super(CustomManager, self).get_queryset().defer('YOUR_TEXTFIELD_FIELDNAME')
class DjangoModel(models.Model):
objects = CustomerManager()

Categories