flexmeasures.utils.flexmeasures_inflection

FlexMeasures way of handling inflection

Functions

flexmeasures.utils.flexmeasures_inflection.atoi(text)

Utility method for the natural_keys method.

flexmeasures.utils.flexmeasures_inflection.capitalize(x: str, lower_case_remainder: bool = False) → str

Capitalize string with control over whether to lower case the remainder.

flexmeasures.utils.flexmeasures_inflection.human_sorted(alist: list, attr: Any | None = None, reverse: bool = False)

Human sort a list (for example, a list of strings or dictionaries).

Parameters:
  • alist – List to be sorted.

  • attr – Optionally, pass a dictionary key or attribute name to sort by

  • reverse – If True, sorts descending.

Example: >>> alist = [“PV 10”, “CP1”, “PV 2”, “PV 1”, “CP 2”] >>> sorted(alist) [‘CP 2’, ‘CP1’, ‘PV 1’, ‘PV 10’, ‘PV 2’] >>> human_sorted(alist) [‘CP1’, ‘CP 2’, ‘PV 1’, ‘PV 2’, ‘PV 10’]

flexmeasures.utils.flexmeasures_inflection.humanize(word)
flexmeasures.utils.flexmeasures_inflection.indefinite_article(word: str) → str

Return “a” or “an”, whichever fits in front of word, e.g. “power” -> “a”, “energy price” -> “an”.

The choice follows pronunciation rather than spelling, so a word starting with a vowel can still take “a” (“a unit”), a word starting with a consonant can take “an” (“an hour”), and an abbreviation takes whichever fits the letter it is read out as (“an MW”).

>>> indefinite_article("power")
'a'
>>> indefinite_article("energy price")
'an'
>>> indefinite_article("unit")
'a'
>>> indefinite_article("hour")
'an'
>>> indefinite_article("MW")
'an'
flexmeasures.utils.flexmeasures_inflection.join_words_into_a_list(words: Sequence[str], conj: str = 'and', final_sep: str = '') → str

Join words into a human-readable list, e.g. [“a”, “b”, “c”] -> “a, b and c”.

Pass final_sep=”,” for an Oxford comma before the conjunction (e.g. “a, b, and c”), and conj=”or” for a disjunction (e.g. “a, b or c”).

flexmeasures.utils.flexmeasures_inflection.natural_keys(text: str)

Support for human sorting.

alist.sort(key=natural_keys) sorts in human order.

https://stackoverflow.com/a/5967539/13775459

flexmeasures.utils.flexmeasures_inflection.parameterize(word)

Parameterize the word, so it can be used as a Python or JavaScript variable name. For example: >>> parameterize(“Acme® EV-Charger™”) ‘acme_ev_chargertm’

flexmeasures.utils.flexmeasures_inflection.pluralize(word, count: str | int | None = None, include_count: bool = False)
flexmeasures.utils.flexmeasures_inflection.titleize(word)

Acronym exceptions are not yet supported by the inflection package, even though Ruby on Rails, of which the package is a port, does.

In most cases it’s probably better to use our capitalize function instead of titleize, because it has less unintended side effects. For example:

>>> word = "two PV panels"
>>> titleize(word)
'Two Pv Panels'
>>> capitalize(word)
'Two PV panels'