ftmq.util
clean_name(value)
cached
Clean a value and only return it if it is a "name" in the sense of, doesn't contain exclusively of special chars
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)
cached
Convert a value to None
or a sanitized string without linebreaks
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
get_country_code(value, splitter=',')
cached
Get the 2-letter iso country code for an arbitrary country name
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
|
Character to use to get text tokens to find country name for |
','
|
Returns:
Type | Description |
---|---|
str | None
|
The iso code or |
Source code in ftmq/util.py
get_country_name(alpha2)
cached
Get the (english) country name for the given 2-letter iso code via pycountry
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha2
|
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_proxy(proxy)
Reduce proxy payload to only include caption property
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proxy
|
CE
|
|
required |
Returns:
Type | Description |
---|---|
CE
|
A |
Source code in ftmq/util.py
get_featured_proxy(proxy)
Reduce proxy payload to only include featured properties
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proxy
|
CE
|
|
required |
Returns:
Type | Description |
---|---|
CE
|
A |
Source code in ftmq/util.py
get_statements(proxy, *datasets)
Get statements from a nomenklatura.entity.CompositeEntity
with multiple
datasets if needed
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proxy
|
CE
|
|
required |
*datasets
|
str
|
Any (additional) datasets to create statements for |
()
|
Yields:
Type | Description |
---|---|
SGenerator
|
A generator of |
Source code in ftmq/util.py
get_year_from_iso(value)
Extract the year from a 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
join_slug(*parts, prefix=None, sep='-', strict=True, max_len=255)
Create a stable slug from parts with optional validation
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
|
Multiple (ordered) parts to compute the slug from |
()
|
prefix
|
str | None
|
Add a prefix to the slug |
None
|
sep
|
str
|
Parts separator |
'-'
|
strict
|
bool
|
Ensure all parts are not |
True
|
max_len
|
int
|
Maximum length of the slug. If it exceeds, the returned value will get a computed hash suffix |
255
|
Returns:
Type | Description |
---|---|
str | None
|
The computed slug or |
Source code in ftmq/util.py
make_fingerprint(value)
cached
Create a stable but simplified string or None
from input that can be used
to generate ids (to mimic fingerprints.generate
which is unstable for IDs
as its algorithm could change)
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)
cached
Compute a hash id based on 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_proxy(data, dataset=None)
Create a nomenklatura.entity.CompositeEntity
from a json dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict[str, Any]
|
followthemoney data dict that represents entity data. |
required |
dataset
|
str | Dataset | None
|
A default dataset |
None
|
Returns:
Type | Description |
---|---|
CE
|
The composite entity proxy |
Source code in ftmq/util.py
make_string_id(*values)
cached
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
prop_is_numeric(schema, prop)
cached
Indicate if the given property is numeric type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Schema
|
followthemoney schema |
required |
prop
|
str
|
Property |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in ftmq/util.py
to_numeric(value)
Convert a string value into a primitive numeric dtype (int
or float
)
taking US and DE formatting into account via regex
Examples:
>>> to_numeric("1")
1
>>> to_numeric("1.0")
1
>>> to_numeric("1.1")
1.1
>>> to_numeric("1,101,000")
1101000
>>> to_numeric("1.000,1")
1000.1
>>> to_numeric("foo")
None
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
The input |
required |
Returns:
Type | Description |
---|---|
float | int | None
|
The converted number or |