10.6.1. Base class for all API Objects

class pytan3.api_models.ApiModel[source]

Bases: object

Base class for all models in the API.

API_NAME = None

str – Name of object used in API calls.

API_NAME_SRC = None

str – Name of object in source file.

API_SIMPLE = None

dict – Map of simple attributes to their types.

API_COMPLEX = None

dict – Map of complex attributes to their types.

API_CONSTANTS = None

dict – Map of constants to their values.

API_STR_ADD = None

list or str – Added to API_STR in str.

T_ATTR_REPR = '{attr}={value!r}'

str – Template for simple attrs in str and all attr in repr.

T_ATTR_STR = '{attr}={value}'

str – Template for complex attrs in str.

T_ATTR_JOIN = ', '

str – String used to join attr vals in str and repr.

T_CLS_STR = '{obj.__class__.__name__}({attrs})'

str – Template for class name and attrs in str.

T_CLS_REPR = '{obj.__class__.__name__}({attrs})'

str – Template for class name and attrs in repr.

T_ATTR_DESC = ' - {api_type} attribute {attr!r} of type {attr_type!r}'

str – Template for attr line in api_attrs_desc().

T_DESC = 'Defined attributes:'

str – Template for first line in api_attrs_desc().

API_STR = ['id', 'name', 'display_name', 'value', 'type', 'public_flag', 'hidden_flag', 'question', 'query_text', 'question_text', 'expiration', 'saved_question']

list of str – Attrs to display in str or to put first in repr.

__copy__()[source]

Support shallow copy for self.

Returns:ApiItem
__deepcopy__(memo)[source]

Support deep copy for self.

Returns:ApiItem
__eq__(value)[source]

Support for self == value.

Parameters:value (dict or list or ApiModel or object) – Value for comparison.

Notes

  • If value is a dict, it will be turned into an ApiModel.
  • If this object is an ApiList and value is a list, it will be turned into an ApiList.
  • If value is turned into or already is an ApiModel, it will be serialized into a dict/list.
  • Finally, actual comparison of value is done against this object serialized into a dict/list.
Returns:bool
__ne__(value)[source]

Support for self != value.

Parameters:value (dict or list or ApiModel or object) – Value for comparison.
Returns:bool
classmethod api_attrs()[source]

Get simple and complex attributes combined.

Returns:dict
classmethod api_attrs_repr()[source]

Get attributes for repr formatting.

Notes

Will return attrs without dupes in order of priority to show on format string from API_STR, API_STR_ADD, and api_attrs().

Returns:list of str
classmethod api_attrs_str()[source]

Get attributes for str formatting.

Notes

Will return attrs without dupes in order of priority to show on format string from API_STR and API_STR_ADD.

If no attrs from API_STR and API_STR_ADD exist in api_attrs(), return all attrs from api_attrs().

Returns:list of str
classmethod api_attrs_desc()[source]

Get description of attrs from API_SIMPLE and API_COMPLEX.

Notes

Used by exceptions to add defined attributes to error messages.

Returns:list of str
classmethod api_coerce_int(value)[source]

Try to coerce value into int if possible.

Parameters:value (object) – Object to coerce into int.
Returns:int or object if unchanged.
classmethod api_coerce_float(value)[source]

Try to coerce value into float if possible.

Parameters:value (object) – Object to coerce into float.
Returns:float or object if unchanged.
classmethod api_coerce_list(value)[source]

Coerce value into list if it is not already.

Parameters:value (object) – Object to coerce into list. If value is None, will return empty list.
Returns:list or object if unchanged.
classmethod api_coerce_simple(value, be_type)[source]

Try to coerce a value into a simple type.

Parameters:
  • value (object) – Value that should be coerced.
  • be_type (object) – Type that value should be coerced to.
Returns:

float or int or str or object if unchanged.

classmethod api_coerce_complex(value, be_type)[source]

Try to coerce a value into a complex type.

Parameters:
  • value (object) – Value that should be coerced.
  • be_type (object) – Type that value should be coerced to.
Returns:

ApiItem or ApiList or object if unchanged.

api_coerce_value(attr, value)[source]

Perform type checking of value.

Will coerce value into expected type if needed and possible.

Parameters:
  • attr (str) – Attribute that is being set with value on obj.
  • value (object) – Value of attr being checked.
Raises:

Notes

No checking or coercion is done if:
  • value is None
  • attr starts with “_”
  • attr is all upper case
Returns:object