sources

Package members

class campos.sources.FieldSource(obj, exclude=(), under=False, dunder=False, prettify=True, apply=None)[source]

Bases: object

Base class for field extractors.

Inspects an object looking for valid attributes to create fields, callables are always ignored and extra filters can be provided using keyword arguments(exclude, under, dunder). Field’s text can be modified by setting prettify=True (default) or providing a custom transformation function using apply keyword.

The following steps are performed when extracting valid object attributes to create fields:

  • Non-callable attributes and their values are extracted from the provided object.
  • Attribute names are filtered using expressions in exclude and values of under (beginning with exactly one _) and dunder (beginning with two or more _).
  • A transformation function(if provided through apply keyword) is used to obtain a nice text for the future field using attribute’s name.
  • Some simple and common transformations such as _ removal and text capitalization are done upon attributes names in order to obtain a nice text for the future field. Note this step is performed even if a transformation function is provided, to disable this behavior set pretiffy=False.
  • Finally only attributes with supported python types can pass, see SUPPORTED_TYPES. Subclasses must implement this check accordingly since some objects may contain wrapped python types.

Fields are created after this filtering process, this is done by calling an utility function designed for each supported python type. See from_bool(), from_int(), from_str(), etc. Subclasses must implement create_fields() for this.

Parameters:
  • obj (any) – object to extract fields from
  • exclude (iterable of str or compiled re) – regular expressions to exclude, attribute names matching any of these will be ignored.
  • under – whether to allow or not attribute names beginning with exactly one _. Defaults to False.
  • dunder – whether to allow or not attribute names beginning with two or more _. Defaults to False.
  • prettify – perform some simple and common transformations such as _ removal and text capitalization upon attributes names in order to obtain a nice text for the future field. Defaults to True.
  • apply (callable) – transformation function to apply to each attribute name to obtain a nice text for the future field. Note that if prettify=True this is done after that step.
SUPPORTED_TYPES = (<class 'int'>, <class 'float'>, <class 'str'>, <class 'bool'>, <class 'datetime.date'>, <class 'datetime.time'>, <class 'datetime.datetime'>)

Supported python types.

create_fields(attributes)[source]

Creates new fields from attributes.

Subclasses must implement this and check if type of each attribute is a supported python type, see SUPPORTED_TYPES.

In order to create new fields the from_* methods in this module can can be useful.

Parameters:attributes (dict) – a dict like d[attr_name] = (attr_text, attr_value) where attr_name will be the new field’s name and attr_text its nice text.
Returns:a dict like d[field_name] = field
Return type:dict
get_members()[source]

Returns all members of the source object, along with their value.

Returns:a list of tuples (member_name, member_value)
Return type:list
campos.sources.from_bool(name, text, value, **kwargs)[source]

Creates a BoolField from a boolean value.

Parameters:
  • name (str) – name for the field
  • text (str) – text for the field
  • value (bool) – a boolean value
  • kwargs – keyword arguments to pass to field constructor
Returns:

a new BoolField with the given name and text

Return type:

BoolField

campos.sources.from_date(name, text, value, **kwargs)[source]

Creates a DateField from a date object. A bit of logic is applied using provided arguments to determine some field’s settings.

Parameters:
  • name (str) – name for the field
  • text (str) – text for the field
  • value (datetime.date) – date object to help adjust field’s settings
  • kwargs – keyword arguments to pass to field constructor
Returns:

a new DateField with the given name and text

Return type:

DateField

campos.sources.from_datetime(name, text, value, **kwargs)[source]

Creates a DatetimeField from a datetime object. A bit of logic is applied using provided arguments to determine some field’s settings.

Parameters:
  • name (str) – name for the field
  • text (str) – text for the field
  • value (datetime.datetime) – datetime object to help adjust field’s settings
  • kwargs – keyword arguments to pass to field constructor
Returns:

a new DatetimeField with the given name and text

Return type:

DatetimeField

See also

from_date() and from_time()

campos.sources.from_float(name, text, value, **kwargs)[source]

Creates a FloatField from a float value. A bit of logic is applied using provided arguments to determine some field’s settings.

Parameters:
  • name (str) – name for the field
  • text (str) – text for the field
  • value (float) – float value to help adjust field’s settings
  • kwargs – keyword arguments to pass to field constructor
Returns:

a new FloatField with the given name and text

Return type:

FloatField

campos.sources.from_int(name, text, value, **kwargs)[source]

Creates a IntField from an integer value. A bit of logic is applied using provided arguments to determine some field’s settings.

Parameters:
  • name (str) – name for the field
  • text (str) – text for the field
  • value (int) – integer value to help adjust field’s settings
  • kwargs – keyword arguments to pass to field constructor
Returns:

a new IntField with the given name and text

Return type:

IntField

campos.sources.from_str(name, text, value, istext=False, **kwargs)[source]

Creates a StringField or a TextField from a string value. A bit of logic is applied using provided arguments to determine some field’s settings and if output will be a string field or a text field. If you want the output to be a TextField set istext=True.

Parameters:
  • name (str) – name for the field
  • text (str) – text for the field
  • value (str) – string value to help adjust field’s settings
  • istext (bool) – if True forces output to be a TextField
  • kwargs – keyword arguments to pass to field constructor
Returns:

a new StringField or TextField with the given name and text

Return type:

StringField or TextField

campos.sources.from_time(name, text, value, **kwargs)[source]

Creates a TimeField from a time object. A bit of logic is applied using provided arguments to determine some field’s settings.

Parameters:
  • name (str) – name for the field
  • text (str) – text for the field
  • value (datetime.time) – time object to help adjust field’s settings
  • kwargs – keyword arguments to pass to field constructor
Returns:

a new TimeField with the given name and text

Return type:

TimeField

campos.sources.get_fields_source(arg, **source_kw)[source]

Tries to find the best FieldSource for the given argument.

An ObjectSource is always returned when a more adequate field source isn’t found.

Parameters:
  • arg – object to find the right field source for.
  • arg – any
  • source_kw – keyword arguments to pass to the FieldSource constructor.
Returns:

a new field source object

Return type:

FieldSource

Raises ValueError:
 

if argument is None

object module

class campos.sources.object.ObjectSource(obj, exclude=(), under=False, dunder=False, prettify=True, apply=None)[source]

Bases: campos.sources.FieldSource

Generic field source for all kinds of objects, this is the fallback class when more fitted sources can’t be found.

See also

SQLAlchemySource

sqlalchemy module

class campos.sources.sqlalchemy.SQLAlchemySource(obj, exclude=(), under=False, dunder=False, prettify=True, apply=None)[source]

Bases: campos.sources.FieldSource

Field source for SQLAlchemy objects.

This field source supports classes and objects created using SQLAlchemy’s Declarative Mapping system as well as SQLAlchemy’s Table objects. This means that fields generated by the following code snippets are the same:

import os
import sys

os.environ['QT_API'] = 'pyqt4'

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

import campos

engine = create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base()

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    last = Column(String)
    password = Column(String)

app = QApplication(sys.argv)

# using an object
user = User(id=1, name='Rajesh', last='Koothrappali', password='ok')
source = campos.get_fields_source(user)
fields1 = source.fields

# using a declarative class
source = campos.get_fields_source(User)
fields2 = source.fields

# using a Table object
source = campos.get_fields_source(User.__table__)
fields3 = source.fields