ftmq.util
clean_name(value)
Clean a value and return it only if it doesn't consist of special chars only.
Examples: >>> clean_name(" foo Bar") "foo Bar" >>> clean_name("- - . *") None
Args: value: Any input that will be cleaned
Returns:
The cleaned name or None
Source code in ftmq/util.py
clean_string(value)
Convert a value to a sanitized string without linebreaks, or None if empty.
Examples: >>> clean_string(" foo bar") "foo bar" >>> clean_string("foo Bar, baz") "foo Bar, baz" >>> clean_string(None) None >>> clean_string("") None >>> clean_string(" ") None >>> clean_string(100) "100"
Args: value: Any input that will be converted to string
Returns:
The cleaned value or None
Source code in ftmq/util.py
datetime_iso(v, default_now=False)
Ensure a UTC ISO datetime string from an arbitrary value.
A naive datetime is assumed to be UTC, an aware one is converted to UTC; a
string is passed through unchanged.
Examples:
>>> datetime_iso(datetime(2024, 1, 15, 10, 30))
"2024-01-15T10:30:00+00:00"
>>> datetime_iso("2024-01-15")
"2024-01-15"
>>> datetime_iso(None)
None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
datetime | str | None
|
A |
required |
default_now
|
bool
|
Return the current UTC timestamp for empty input instead
of |
False
|
Returns:
| Type | Description |
|---|---|
str | None
|
The ISO datetime string, or |
Source code in ftmq/util.py
ensure_entity(data, entity_type, default_dataset=None)
Ensure input data to be of the given Entity type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any] | Entity | EntityProxy
|
entity or data |
required |
entity_type
|
Type[E]
|
The entity class to use ( |
required |
default_dataset
|
str | Dataset | None
|
A default dataset if no dataset in data |
None
|
Returns:
| Type | Description |
|---|---|
E
|
The Entity instance |
Source code in ftmq/util.py
entity_fingerprints(entity)
Get the entity's name fingerprints, latinized if possible and with org / person tags removed depending on its schema.
get_country_code(value, splitter=',')
cached
Get the 2-letter iso country code for an arbitrary country name via rigour.territories.
Examples:
>>> get_country_code("Germany")
"de"
>>> get_country_code("Deutschland")
"de"
>>> get_country_code("Berlin, Deutschland")
"de"
>>> get_country_code("Foo")
None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Any input that will be cleaned |
required |
splitter
|
str | None
|
Split the value into tokens to look up if it doesn't match |
','
|
Returns:
| Type | Description |
|---|---|
str | None
|
The iso code or |
Source code in ftmq/util.py
get_country_name(code)
cached
Get the (english) country name for a 2-letter iso code via rigour.territories.
Examples:
>>> get_country_name("de")
"Germany"
>>> get_country_name("xx")
"xx"
>>> get_country_name("gb") == get_country_name("uk")
True # United Kingdom
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Two-letter iso code, case insensitive |
required |
Returns:
| Type | Description |
|---|---|
str
|
Either the country name for a valid code or the code as fallback. |
Source code in ftmq/util.py
get_dehydrated_entity(e)
Reduce an entity to the properties needed to compute its caption.
Source code in ftmq/util.py
get_featured_entity(e)
Reduce an entity to its caption and featured properties.
get_name_symbols(schema, *names, consolidate=False, phonetics=False)
Get the rigour name symbols for the given schema and names.
Source code in ftmq/util.py
get_scope_dataset(*names)
cached
Get a store's read scope over names: a single dataset is its own scope,
several are wrapped in a synthetic ftmq_scope collection.
Source code in ftmq/util.py
get_symbols(entity)
Get the rigour name symbols for the given entity.
Source code in ftmq/util.py
get_year_from_iso(value)
Extract the year from an iso date string or datetime object.
Examples:
>>> get_year_from_iso(None)
None
>>> get_year_from_iso("2023")
2023
>>> get_year_from_iso(2020)
2020
>>> get_year_from_iso(datetime.now())
2024
>>> get_year_from_iso("2000-01")
2000
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Any input that will be cleaned |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
The year or |
Source code in ftmq/util.py
inline_symbols(entity)
Write the entity's rigour name symbols to indexText, replacing old ones.
Source code in ftmq/util.py
iso_datetime(v)
Parse an ISO datetime string into an aware UTC datetime.
A naive value is assumed to be UTC, an offset is converted to UTC.
Unlike rigour.time.iso_datetime, microseconds are kept.
Examples:
>>> iso_datetime("2024-01-15T10:30:00")
datetime(2024, 1, 15, 10, 30, tzinfo=timezone.utc)
>>> iso_datetime("2024-01-15T10:30:00.123456")
datetime(2024, 1, 15, 10, 30, 0, 123456, tzinfo=timezone.utc)
>>> iso_datetime(None)
None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
str | datetime | None
|
An ISO datetime string, a |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
An aware datetime in UTC, or |
Source code in ftmq/util.py
join_slug(*parts, prefix=None, sep='-', strict=True, max_len=255)
Create a stable slug from parts.
Examples:
>>> join_slug("foo", "bar")
"foo-bar"
>>> join_slug("foo", None, "bar")
None
>>> join_slug("foo", None, "bar", strict=False)
"foo-bar"
>>> join_slug("foo", "bar", sep="_")
"foo_bar"
>>> join_slug("a very long thing", max_len=15)
"a-very-5c156cf9"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*parts
|
str | None
|
Ordered parts to compute the slug from |
()
|
prefix
|
str | None
|
Prefix for the slug |
None
|
sep
|
str
|
Parts separator |
'-'
|
strict
|
bool
|
Return |
True
|
max_len
|
int
|
Maximum length; a longer slug is cut and gets a hash suffix |
255
|
Returns:
| Type | Description |
|---|---|
str | None
|
The computed slug or |
Source code in ftmq/util.py
make_entity(data, entity_type=ValueEntity, default_dataset=None)
Create an Entity from a json dict. The input data is not mutated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
SDict
|
followthemoney data dict that represents entity data. |
required |
entity_type
|
Type[E] | None
|
The entity class to use ( |
ValueEntity
|
default_dataset
|
str | Dataset | None
|
A default dataset if no dataset in data |
None
|
Returns:
| Type | Description |
|---|---|
E
|
The Entity instance |
Source code in ftmq/util.py
make_fingerprint(value)
Create a stable, simplified string from input to generate ids from.
Examples:
>>> make_fingerprint("Mrs. Jane Doe")
"doe jane mrs"
>>> make_fingerprint("Mrs. Jane Mrs. Doe")
"doe jane mrs"
>>> make_fingerprint("#")
None
>>> make_fingerprint(" ")
None
>>> make_fingerprint("")
None
>>> make_fingerprint(None)
None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Any input that will be cleaned |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The simplified string (fingerprint) or |
Source code in ftmq/util.py
make_fingerprint_id(*values)
Compute a hash id based on the values' fingerprints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*values
|
Any
|
Parts to compute id from that will be fingerprinted |
()
|
Returns:
| Type | Description |
|---|---|
str | None
|
The computed hash id or |
Source code in ftmq/util.py
make_fingerprints(*names, schemata=None)
Get the name fingerprints, latinized if possible and with org / person tags removed depending on the given schemata.
Source code in ftmq/util.py
make_string_id(*values)
Compute a hash id based on values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*values
|
Any
|
Parts to compute id from that will be cleaned |
()
|
Returns:
| Type | Description |
|---|---|
str | None
|
The computed hash id or |
Source code in ftmq/util.py
select_annotations(e)
select_data(e, prefix)
Select data stored in indexText under the given prefix.
select_symbols(e)
Select the symbols stored in indexText.