khard.helpers.typing

Helper code for type annotations and runtime type conversion.

Module Contents

Classes

ObjectType

Create a collection of name/value pairs.

Functions

convert_to_vcard(→ StrList)

converts user input into vCard compatible data structures

list_to_string(→ str)

converts list to string recursively so that nested lists are supported

string_to_list(→ List[str])

string_to_date(→ datetime.datetime)

Convert a date string into a date object.

Attributes

Date

StrList

PostAddress

class khard.helpers.typing.ObjectType(*args, **kwds)

Bases: enum.Enum

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access:

>>> Color.RED
<Color.RED: 1>
  • value lookup:

>>> Color(1)
<Color.RED: 1>
  • name lookup:

>>> Color['RED']
<Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.

str = 1
list = 2
both = 3
khard.helpers.typing.Date
khard.helpers.typing.StrList
khard.helpers.typing.PostAddress
khard.helpers.typing.convert_to_vcard(name: str, value: StrList, constraint: ObjectType) StrList

converts user input into vCard compatible data structures

Parameters:
  • name – object name, only required for error messages

  • value – user input

  • constraint – set the accepted return type for vCard attribute

Returns:

cleaned user input, ready for vCard or a ValueError

khard.helpers.typing.list_to_string(input: str | List, delimiter: str) str

converts list to string recursively so that nested lists are supported

Parameters:
  • input – a list of strings and lists of strings (and so on recursive)

  • delimiter – the delimiter to use when joining the items

Returns:

the recursively joined list

khard.helpers.typing.string_to_list(input: str | List[str], delimiter: str) List[str]
khard.helpers.typing.string_to_date(string: str) datetime.datetime

Convert a date string into a date object.

Parameters:

string – the date string to parse

Returns:

the parsed datetime object