Pyramid framework settings in eclipse (aptana studio)

aptana studio 3 pyramid framework

How can i set up Pyramid Framework 1.3 in Eclipse or in Aptana Studio 3?

Sure, IDE is very important in software development(debugging, code assist, organize imports etc.). You can code python in editors, it is practical but diffucult for beginners.

Python and Django have eclipse plugin, there is no eclipse plugin for Pyramid Framework?

How i can run a Pyramid Project from Eclipse or Aptana Studio 3?

Steps :

1 – in Console : Create new pyramid 1.3.xx project :
On Linux:

   $ bin/pcreate -s alchemy MyProject

On Windows :

   > Scripts\pcreate -s alchemy MyProject

2 – Install your newly created project for development

   $ cd MyProject
   $ ../bin/python setup.py develop

On Windows :

   > cd MyProject
   > ..\Scripts\python.exe setup.py develop

3 – In Aptana Studio 3 : Create new PyDev project and give project content path as your Pyramid Project path

pydev

4 – For run configuration add a  pyramid_run variable in menu : Window > Preferences > in list PyDev – Interpreter – Python in tab String Substitution Variables – click Add variable and give key and value as in the screenshot below. On MS Windows select Scripts\pserve-script.py instead pserve.

pydev

5 – Add a run configuration in menu : Run -> Run Configuration on the left side on Python Run node Right Click : New Click and give run configuration settings and run.

pydev

pydev

6 – Your pyramid application runs on browser.

pydev

SQLAlchemy Tutorial 3: Base Entity Class in SQLAlchemy

SQLAlchemy Tutorial 3: Base Entity Class in SQLAlchemy

How can i use a base entity class in SQLAlchemy?
For example i have 4 columns in all my database tables. I don’t like write again and again these shared fields in all my entities.

created_by_user = Column(VARCHAR(50))
created_at = Column(DATETIME)
updated_by_user = Column(VARCHAR(50))
updated_at = Column(DATETIME)

For before insert before update base extension class

# sqlalchemy declarative base
Base = declarative_base() 

class BaseExtension(MapperExtension):
    """Base entension class for all entity """

    def before_insert(self, mapper, connection, instance):
        """ set the created_at  """
        instance.created_at = datetime.now()

    def before_update(self, mapper, connection, instance):
        """ set the updated_at  """
        instance.updated_at = datetime.now()

You can use as base entity object a mixin class in SQLAlchemy with base extension and table args.

class BaseEntity(object):
    __table_args__ = {
            'mysql_engine': 'InnoDB',
            'mysql_charset': 'utf8'
    }
    # for before insert before update base extension class
    __mapper_args__ = { 'extension': BaseExtension() }  

    # shared fields for all entities
    created_by_user = Column(VARCHAR(50))
    created_at = Column(DATETIME)
    updated_by_user = Column(VARCHAR(50))
    updated_at = Column(DATETIME)

Here Country entity inherits from declarative base class and base entity mixin class.

class Country(Base, BaseEntity):
    """ Country entity class """
    __tablename__ = 'country'

    id = Column('country_id', BIGINT(unsigned=True), primary_key=True)
    code = Column(VARCHAR(10), nullable=False, unique=True)
    name = Column(VARCHAR(100), nullable=False, unique=True)

    def __init__(self, code, name):
        self.code = code
        self.name = name

SQLAlchemy Tutorial 2 :Before Insert Before Update in SQLAlchemy

SQLAlchemy Tutorial 2 :Before Insert Before Update in SQLAlchemy

Please check my previous post for declarative mysql tables in SQLAlchemy:

how can i define and create my MySQL tables with SQLAlchemy ORM?

I have 4 fields in my all database tables for change logs. But i need automatic timestamp values for created_at and updated_at columns. For this purposes you can use mapper extension in SQLAlchemy.

Here base extension class

class BaseExtension(MapperExtension):
    """Base entension class for all entity """

    def before_insert(self, mapper, connection, instance):
        """ set the created_at  """
        instance.created_at = datetime.now()

    def before_update(self, mapper, connection, instance):
        """ set the updated_at  """
        instance.updated_at = datetime.now()

and here country entity

class Country(Base):
    """ Country entity class """
    __tablename__ = 'country'
    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8'
    }
    # mapper extension declarative for before insert and before update
    __mapper_args__ = { 'extension': BaseExtension() }

    # this field is in model "id", in db "country_id"
    # not null and auto_increment settings with "primary_key"
    id = Column('country_id', BIGINT(unsigned=True), primary_key=True)
    code = Column(VARCHAR(10), nullable=False, unique=True)
    name = Column(VARCHAR(100), nullable=False, unique=True)
    created_by_user = Column(VARCHAR(50))
    created_at = Column(DATETIME)
    updated_by_user = Column(VARCHAR(50))
    updated_at = Column(DATETIME)

    def __init__(self, code, name):
        self.code = code
        self.name = name