Jeffrey McLarty <jeffrey.mcla...@gmail.com> wrote:

> Hello,
> 
> First - amazing work on SQLAlchemy, what an amazing body of work.
> 
> Second, my question.
> 
> I'm attempting to convert a pure-python object, into a custom type for column 
> definition. My question is, do I need to keep my original MyObj(object) and 
> use it inside the process_bind_param/process_result_value of 
> MyObjType(TypeDecorator), or should I be able to get this to work with just 
> one class?

if you want to serialize your object in some special way to fit it into a
column, a TypeDecorator is the most straightforward way to go. There’s no
magic method that SQLAlchemy calls on your object; only if you used
PickleType you could define a serializer.


> More:
> 
> I'm confused about the *arg and **kw passed to typeobj, in the to_instance() 
> function, from type_api.py...when those might conflict with my object 
> constructor's parameters.

those are arguments referring to the type object, not your actual object.
They are two separate things.

> 
> ...if anybody wants to have a look at my code, it is on github, organized 
> into a PR, https://github.com/Equitable/trump/pull/15 with links to the files 
> and line numbers.

right…that’s not right. You need two different objects. One would be
BitFlag, with all of the special logic you have here, and the other would be
BitFlagType. BitFlagType wouldn’t really need anything in its constructor,
just the process_bind_param / process_result_value methods.

However! This specific case has a special bonus feature. Your BitFlag
implementation could just as well be a subclass of int (e.g. class
BitFlag(int)) and you’d get the process_bind_param part for free in that
case. Though you’d still probably want process_result_value so that when you
load from the database, you get BitFlag objects back and not just plain
ints.

Either way the TD will be along these lines:

class BitFlagType(TypeDecorator):

    impl = Integer

    def process_bind_param(self, value, dialect):
        if value is not None:
            value = value.int_field

        return value

    def process_result_value(self, value, dialect):
        if value is not None:
            value = BitFlag(value)
        return value

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to