Giter Club home page Giter Club logo

athanor's Introduction

Athanor

Athanor is a set of utilities for SQLAlchemy.

Athanor provides simple conveinence functions for common patterns found in many applications. Tools should be selected as the situation calls for. SQLAlchemy is marvelous package; Athanor is not an attempt to eliminate SQLAlchemy.

See Also - http://en.wikipedia.org/wiki/Athanor

Examples

See examples.py

BaseModel

BaseModel is base class for SQLAlchemy mapped objects that provides a number of utilities to inspect, create, find, fetch and update objects/columns. It also provides “magic finders” that will supply dynamic getters and finders such as get_by_foo and find_by_bar.

Many of these utilities are familiar to Django’s ORM.

Class Methods and Attributes

.__tablename__

Description

Automatically computes __tablename__ unless overriden as the plural of the words_with_underscores version of the class name. Override if the magic falls on it’s face.

Example

FooBar -> foo_bars

.objects

Description

The .objects attribute is query of all objects in the collection with no filters applied. See SQLAlchemy’s query_property. The finders below use .objects so subclasses can override it if they need to apply any default options and criteria.

def get(cls, id):

Description

Gets an object by the collection’s primary key. Returns None if no object is found

Returns

Returns an object if one exists or None if no object is found.

Example

Foo.get(1)

def get_by(cls, column, value):

Description

Gets an object by a column and value.

Returns

Returns an object if one exists or None if no object is found.

Example

Foo.get_by(Foo.id, 1)

def find(cls, criteria, order_by=None):

Description

Find the objects of .objects matching criteria by order_by

Returns

Returns an query of .objects with the criteria and order_by applied.

Example

Foo.find(and_(Foo.id > 10, Foo.id < 100))

def find_one(cls, criteria, order_by=None):

Description

Find the first object of .objects matching criteria by order_by

Returns

Returns and object or None is no such match exists.

Example

Foo.find_one(and_(Foo.id > 10, Foo.id < 100))

def create(cls, **data):

Description

Creates and object from data kwargs, adds it to the session and commits. Override in subclass if you want to enforce behavior on creation (e.g. If you want to always pass in a created_by user)

Returns

A the created object after it has been committed to the database.

Example

Foo.create(user=user, name=’marc’)

def get_or_create(cls, id, data, update=True):

Description

Attempts to fetch an object by given primary key. If no object is found then an object will be created from the given data dictionary. If update is True, existing objects will be updated from the data dictionary.

Returns

Returns a tuple of the object and a boolean indicating whether or not it was created.

Example

obj, created = Foo.get_or_create(1, {‘name’: ‘marc’, ‘user’: user})

def get_by_or_create(cls, column, value, data, update=True):

Description

Attempts to fetch an object by given column and value. If no object is found then an object will be created from the given data dictionary. If update is True, existing objects will be updated from the data dictionary.

Returns

Returns a tuple of the object and a boolean indicating whether or not it was created.

Example

obj, created = Foo.get_by_or_create(Foo.name, ‘marc’, {‘name’: ‘marc’, ‘user’: user})

def find_or_create(cls, criteria, data, update=True):

Description

Attempts to fetch an object by given criteria. If no object is found then an object will be created from the given data dictionary. If update is True, existing objects will be updated from the data dictionary.

Returns

Returns a tuple of the object and a boolean indicating whether or not it was created.

Example

obj, created = Foo.get_by_or_create(and_(Foo.name==’marc’, Foo.active=True), {‘name’: ‘marc’, ‘user’: user})

Instance Methods

def __repr__(self):

Description

Convenience method to build a simple representation of a mapped instance from the class name and includes the objects primary key(s).

Returns

A string

Example

print foo

.columns:

Description

A list of an objects column names

.relationships:

Description

A list of an objects relationship names

.primary_key:

Description

A list of an object’s primary key names

.attributes:

Description

A list of an object’s columns and relationships

def to_dict(self, include=None, exclude=None):

Description

Build a dict representation of an object. Pass a list of field names to include to only includes fields explicitly listed. Pass a list of fields names to exclude to omit fields. No relationships are traversed, if you want to include relationships, subclass and manually include those fields.

Returns

A dictionary of an object’s fields.

Example

foo.to_dict(exclude=[‘id’])

def __json__(self):

Description

A method to handle JSON serialization. A common convention is to call __json__ if such a method exists on an object when converting to JSON. By default calls to_dict().

Returns

A dict suitable for serialization.

def update(self, data):

Description

Update an object from a dictionary.

Returns

The object updated

Example

foo.update({‘name’: ‘marc’})

def save(self):

Description

Commits the session. This method does not restrict the commit to only the object in question. This is a simple convenience to make code look a bit more literate at the cost of possible unintentional side effects

Returns

None

Example

foo.save()

def delete(self):

Description

Deletes an object

Returns

None

Example

foo.delete()

Types

UTCDateTime

Description

UTCDateTime will take a time-zone aware datetime and store it as UTC in the database automatically.

DeclEnum, EnumSymbol

See http://techspot.zzzeek.org/2011/01/14/the-enum-recipe/

Included here for easy install

Mixins

StampedMixin

Description

StampedMixin adds created_on and modified_on columns to a table. These columns will updated as needed.

TrackedMixin

Description

TrackedMixin adds created_by and modified_by columns to a table that relate to a User object. Use the .touch(user) method to update modified_by.

Todo

  • tests
  • ReST docs
  • A simple makefile

Author

Marc DellaVolpe ([email protected])

athanor's People

Contributors

mdellavo avatar

Watchers

 avatar James Cloos avatar Pierre Prinetti avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.