python - SQLAlchemy Columns result processing -


i'm working ibm db2 database using ibm_db2 driver , sqlalchemy. model is:

class user(model):     id          = column('uid', integer, primary_key=true)     user        = column('user', string(20))     password    = column('password', string(10))     name        = column('name', string(30)) 

string fields database (e.g. name) comes in form of:

>>> "john                                " 

, value filled right blanks full length of field schema.

i need change behavior sqlalchemy type string (or derivative thereof) produced follow (e.g. value.strip()) before output results query.all():

>>> "john" 

how can this?

@property decorator not applicable. need change behavior of standard sqlalchemy string class.

i not want change behaviour of standard string make new type (you can rename string per module basis or whatever) cleanest way:

from sqlalchemy import types  class strippedstring(types.typedecorator):     '''     returns char values spaces stripped     '''      impl = types.string      def process_bind_param(self, value, dialect):         "no-op"         return value      def process_result_value(self, value, dialect):         "strip trailing spaces on resulting values"         return value.rstrip()      def copy(self):         "make copy of type"         return strippedstring(self.impl.length) 

now can use strippedstring instead of string


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -