sol.models.utils – Utilities — SoL 4 developers documentation

# `sol.models.utils` – Utilities

Simple helper functions.

`sol.models.utils.` `asunicode`(s)

  Force a string to be a unicode instance.

  Parameters: | s – any value
  Return type: | str

  If s is not already an unicode string, it is assumed to be a `utf-8` bytes string, and thus decoded to unicode and returned. Otherwise s is returned as is:

      >>> assert asunicode(None) is None
      >>> assert not isinstance(asunicode(b'ascii'), bytes)

`sol.models.utils.` `normalize`(s, title=None)

  Normalize the case of a string, removing spurious spaces.

  Parameters: | s – a string title – if True always titleize the string, if False never do that, if None (default) only when the input string is all lower case or all upper case
  Return type: | unicode

      >>> assert normalize(None) is None
      >>> print(normalize('lele gaifax'))
      Lele Gaifax
      >>> print(normalize('LELE'))
      Lele
      >>> print(normalize('LeLe', title=False))
      LeLe

`sol.models.utils.` `njoin`(elts, stringify=<function asunicode>, localized=True)

  Given a sequence of items, concatenate them in a nice way.

  Parameters: | elts – a sequence of elements stringify – the stringification function applied to all elements, by default coerced to unicode localized – a boolean flag to disable the translation of the final ‘and’
  Return type: | unicode

  If elts is empty returns an empty unicode string; if it contains a single element, returns the stringified element; otherwise returns a unicode string composed by all but the last elements stringified and joined by a comma, followed by the localized version of and followed by the last element stringified:

      >>> print(njoin([1,2,3]))
      1, 2 and 3
      >>> print(njoin([1,2]))
      1 and 2
      >>> print(njoin([1]))
      1
      >>> assert njoin([]) == ''
      >>> print(njoin([1,2], stringify=lambda x: str(x*10)))
      10 and 20

  Note that falsey elements are skipped:

      >>> print(njoin(['first', None, False, '', 'last']))
      first and last

  but `0` (zero) isn’t considered a falsey value:

      >>> print(njoin([1,0,2]))
      1, 0 and 2

`sol.models.utils.` `entity_from_primary_key`(pkname)

  Given the name of a primary key, return the mapped entity.

  Parameters: | pkname – the name of a primary key
  Return type: | a mapped class

`sol.models.utils.` `table_from_primary_key`(pkname)

  Given the name of a primary key, return the related table.

  Parameters: | pkname – the name of a primary key
  Return type: | a SQLAlchemy table