Exemple #1
0
import time
import traceback
import types

import arrow

from contextlib import contextmanager

from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from sqlalchemy.ext.declarative import declarative_base as real_declarative_base
from sqlalchemy.orm import sessionmaker


### SQLALCHEMY INIT START ###
declarative_base = lambda cls: real_declarative_base(cls=cls)

@declarative_base
class BetterBase(object):
    '''
    Add some default properties and methods to the SQLAlchemy declarative base.
    '''
    @property
    def columns(self):
        '''
        Return all of the columns.
        '''
        return (c.name for c in self.__table__.columns)
        #return (c.key for c in class_mapper(self.__class__).columns)

    @property
Exemple #2
0
import json
from itertools import ifilter
from ballot_box.modules import helpers
from sqlalchemy import Column, DateTime, Float, ForeignKey, Table, Text, text
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base as real_declarative_base
from datetime import datetime

declarative_base = lambda cls: real_declarative_base(cls=cls)


@declarative_base
class Base(object):
    """
    Add some default properties and methods to the SQLAlchemy declarative base.
    """
    @property
    def columns(self):
        return [c.name for c in self.__table__.columns]

    @property
    def column_items(self):
        return dict([(c, getattr(self, c)) for c in self.columns])

    def __repr__(self):
        return '{}({})'.format(self.__class__.__name__, self.column_items)

    def to_json(self):
        return json.dumps(self.column_items)