sources¶
Package members¶
-
class
campos.sources.FieldSource(obj, exclude=(), under=False, dunder=False, prettify=True, apply=None)[source]¶ Bases:
objectBase 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 settingprettify=True(default) or providing a custom transformation function usingapplykeyword.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
excludeand values ofunder(beginning with exactly one _) anddunder(beginning with two or more _). - A transformation function(if provided through
applykeyword) 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 implementcreate_fields()for this.Parameters: - obj (any) – object to extract fields from
- exclude (iterable of
stror compiledre) – 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=Truethis 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
-
campos.sources.from_bool(name, text, value, **kwargs)[source]¶ Creates a
BoolFieldfrom 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
BoolFieldwith the given name and textReturn type: - name (
-
campos.sources.from_date(name, text, value, **kwargs)[source]¶ Creates a
DateFieldfrom 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
DateFieldwith the given name and textReturn type: See also
- name (
-
campos.sources.from_datetime(name, text, value, **kwargs)[source]¶ Creates a
DatetimeFieldfrom 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
DatetimeFieldwith the given name and textReturn type: See also
- name (
-
campos.sources.from_float(name, text, value, **kwargs)[source]¶ Creates a
FloatFieldfrom 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
FloatFieldwith the given name and textReturn type: - name (
-
campos.sources.from_int(name, text, value, **kwargs)[source]¶ Creates a
IntFieldfrom 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
IntFieldwith the given name and textReturn type: - name (
-
campos.sources.from_str(name, text, value, istext=False, **kwargs)[source]¶ Creates a
StringFieldor aTextFieldfrom 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 aTextFieldsetistext=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 aTextField - kwargs – keyword arguments to pass to field constructor
Returns: a new
StringFieldorTextFieldwith the given name and textReturn type: - name (
-
campos.sources.from_time(name, text, value, **kwargs)[source]¶ Creates a
TimeFieldfrom 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
TimeFieldwith the given name and textReturn type: See also
- name (
-
campos.sources.get_fields_source(arg, **source_kw)[source]¶ Tries to find the best
FieldSourcefor the given argument.An
ObjectSourceis 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
FieldSourceconstructor.
Returns: a new field source object
Return type: 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.FieldSourceGeneric field source for all kinds of objects, this is the fallback class when more fitted sources can’t be found.
See also
sqlalchemy module¶
-
class
campos.sources.sqlalchemy.SQLAlchemySource(obj, exclude=(), under=False, dunder=False, prettify=True, apply=None)[source]¶ Bases:
campos.sources.FieldSourceField 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