formal package

Submodules

formal.database module

Changes notice

This file has been changed by the Hackerfleet Community and this notice has been added in accordance to the Apache License 2.0

Description

Interface to pymongo and sqlalchemy

exception NotConnected[source]

Bases: RuntimeError

Raised when a query is made without being connected to the database

connect(database, username=None, password=None, host='localhost', port=27017)[source]

Connect to a database.

connect_sql(database, database_type='postgresql', username=None, password=None, host='localhost', port=5432)[source]

Connect an optional SQL database

get_collection(collection, database=None)[source]

Get the collection of a database

get_database(database=None)[source]

Get a database by name, or the default database.

formal.exceptions module

Changes notice

This file has been changed by the Hackerfleet Community and this notice has been added in accordance to the Apache License 2.0

exception InvalidReloadException[source]

Bases: Exception

Thrown when we attempt to call reload() on a model that is not in the database.

exception InvalidSchemaException[source]

Bases: Exception

Thrown when we have discovered an invalid schema.

exception ValidationError[source]

Bases: Exception

Thrown when a field does not match the schema.

formal.model_base module

Changes notice

This file has been changed by the Hackerfleet Community and this notice has been added in accordance to the Apache License 2.0

class ModelBase(original_fields=None, from_find=False, *args, **kwargs)[source]

Bases: object

This class serves as a base class for the main model types in formal: Model, and TwistedModel.

__init__(original_fields=None, from_find=False, *args, **kwargs)[source]

Creates an instance of the object.

cast(fields, schema=None)[source]

Cast the fields from Mongo into our format - necessary to convert floats into ints since Javascript doesn’t support ints.

classmethod collection_name()[source]

Get the collection associated with this class.

classmethod database_name()[source]

Get the database associated with this class. Meant to be overridden in subclasses.

get(field, default=None)[source]

Get a field if it exists, otherwise return the default.

to_dict()[source]

Convert the object to a dict.

update(new_fields, update_id=False)[source]

Updates an objects fields

validate()[source]

Validate schema against a dict obj.

extend_with_default(validator_class)[source]

Extend a validator by adding default functionality

formal.model_mongodb module

Changes notice

This file has been changed by the Hackerfleet Community and this notice has been added in accordance to the Apache License 2.0

class Model(original_fields=None, from_find=False, *args, **kwargs)[source]

Bases: formal.model_base.ModelBase

The Mongodb object model class

classmethod bulk_create(objects, *args, **kwargs)[source]

Create a number of objects (yay performance).

classmethod collection()[source]

Get the pymongo collection object for this model. Useful for features not supported by formal like aggregate queries and map-reduce.

classmethod count(object_filter=None)[source]

Counts the number of items: - not the same as pymongo’s count, this is the equivalent to:

collection.find(*args, **kwargs).count()
delete()[source]

Removes an object from the database.

classmethod find(*args, **kwargs)[source]

Grabs a set of elements from the DB. Note: This returns a generator, so you can’t to do an efficient count. To get a count, use the count() function which accepts the same arguments as find() with the exception of non-query fields like sort, limit, skip.

classmethod find_by_id(obj_id, **kwargs)[source]

Finds a single object from this collection.

classmethod find_latest(*args, **kwargs)[source]

Finds the latest one by _id and returns it.

classmethod find_one(*args, **kwargs)[source]

Finds a single object from this collection.

classmethod find_or_create(query, *args, **kwargs)[source]

Retrieve an element from the database. If it doesn’t exist, create it. Calling this method is equivalent to calling find_one and then creating an object. Note that this method is not atomic.

reload()[source]

Reload this object’s data from the DB.

save(*args, **kwargs)[source]

Saves an object to the database.

serializablefields()[source]

Return serializable fields of the object

formal.model_sqlalchemy module

SQL Support for Formal

class Model(original_fields=None, from_find=False, *args, **kwargs)[source]

Bases: object

The SQL object model class

__init__(original_fields=None, from_find=False, *args, **kwargs)[source]

Creates an instance of the object.

classmethod bulk_create(objects, *args, **kwargs)[source]

Create a number of objects (yay performance).

cast(fields, schema=None)[source]

Cast the fields from Mongo into our format - necessary to convert floats into ints since Javascript doesn’t support ints.

classmethod clear()[source]

Clear a collection

classmethod collection()[source]

Get the pymongo collection object for this model. Useful for features not supported by formal like aggregate queries and map-reduce.

classmethod collection_name()[source]

Get the collection associated with this class.

classmethod count(*args, **kwargs)[source]

Counts the number of items:

classmethod database_name()[source]

Get the database associated with this class. Meant to be overridden in subclasses.

delete()[source]

Removes an object from the database.

classmethod find(*args, **kwargs)[source]

Grabs a set of elements from the DB. Note: This returns a generator, so you can’t do an efficient count. To get a count, use the count() function which accepts the same arguments as find() with the exception of non-query fields like sort, limit, skip.

classmethod find_by_id(obj_id, **kwargs)[source]

Finds a single object from this collection.

classmethod find_latest(*args, **kwargs)[source]

Finds the latest one by _id and returns it.

classmethod find_one(*args, **kwargs)[source]

Finds a single object from this collection.

classmethod find_or_create(query, *args, **kwargs)[source]

Retrieve an element from the database. If it doesn’t exist, create it. Calling this method is equivalent to calling find_one and then creating an object. Note that this method is not atomic.

get(field, default=None)[source]

Get a field if it exists, otherwise return the default.

classmethod make_migration(new_schema)[source]

Make migrations for a schema

classmethod migrate(patchset)[source]

Migrate an object model with a patchset

reload()[source]

Reload this object’s data from the DB.

save(*args, **kwargs)[source]

Saves an object to the database.

serializablefields()[source]

Return serializable fields of the object

to_dict()[source]

Convert the object to a dict.

update(new_fields, update_id=False)[source]

Update an object’s fields

validate()[source]

Validate schema against a dict obj.

formal.scm_version module

Module contents

Changes notice

This file has been changed by the Hackerfleet Community and this notice has been added in accordance to the Apache License 2.0

model_factory(schema, base_class=<class 'formal.model_mongodb.Model'>)[source]

Construct a model based on schema that inherits from base_class.