fmt
pinky_core.fmt
Pure formatting utility functions.
No external dependencies — importable without a Snowflake connection. Safe to use in SP/UDF handlers, Streamlit apps, and local scripts.
AddressComponents
dataclass
Parsed components of a postal address.
Raw fields preserve the original casing of the input.
afnor contains the same fields normalised per NF Z10-011 (FR only).
Attributes:
| Name | Type | Description |
|---|---|---|
number |
str | None
|
Street number ( |
repetition_index |
str | None
|
Repetition suffix, normalised — |
street_type |
str | None
|
Street type in original casing ( |
street_name |
str | None
|
Street name in original casing ( |
zip_code |
str | None
|
Postal code — provided or extracted from the address string. |
city |
str | None
|
City name in original casing. |
afnor |
AfnorAddress | None
|
AFNOR-normalised fields (FR only) — see |
raw |
str | None
|
Original input; always set. |
Source code in src/pinky_core/fmt.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | |
AfnorAddress
dataclass
AFNOR NF Z10-011 normalised address components (FR only).
All string fields are unidecode + uppercase. Analogous to e164_format
on phone numbers — use these fields when the target system (ADP, La Poste…)
requires the normalised form.
Attributes:
| Name | Type | Description |
|---|---|---|
number |
str | None
|
Street number ( |
repetition_index |
str | None
|
Abbreviated suffix — |
street_type |
str | None
|
Uppercase street type — |
street_name |
str | None
|
Uppercase street name — |
zip_code |
str | None
|
Postal code (unchanged). |
city |
str | None
|
Uppercase city — |
Source code in src/pinky_core/fmt.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
deduplicate_headers(headers)
Deduplicate a list of column headers by appending a counter suffix.
Example
["A", "B", "A", "A"] → ["A", "B", "A_1", "A_2"]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
headers
|
list[str]
|
List of header strings (already normalised). |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List with duplicates resolved by appending _1, _2, etc. |
Source code in src/pinky_core/fmt.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
format_boolean(value, style='check')
Format a boolean as a readable icon (tri-state: True / False / None).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
bool | None
|
Boolean value. None = indeterminate. |
required |
style
|
str
|
Rendering style, one of: - "check" → ✅ / ❌ / ☑️ - "yesno" → Yes / No / — - "onoff" → ON / OFF / — - "dot" → 🟢 / 🔴 / ⚪ |
'check'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted string for the given style. |
Source code in src/pinky_core/fmt.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
format_date(d, fmt='%d/%m/%Y')
Format a date as a readable string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
date | datetime | None
|
Date to format. Returns "" if None. |
required |
fmt
|
str
|
strftime format string. Defaults to dd/mm/yyyy. |
'%d/%m/%Y'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted date string, or "" if d is None. |
Source code in src/pinky_core/fmt.py
22 23 24 25 26 27 28 29 30 31 32 33 34 | |
format_datetime(d, fmt='%d/%m/%Y %H:%M')
Format a datetime as a readable string with time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
datetime | None
|
Datetime to format. Returns "" if None. |
required |
fmt
|
str
|
strftime format string. Defaults to dd/mm/yyyy HH:MM. |
'%d/%m/%Y %H:%M'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted datetime string, or "" if d is None. |
Source code in src/pinky_core/fmt.py
37 38 39 40 41 42 43 44 45 46 47 48 49 | |
format_duration(seconds)
Format a duration in seconds as a human-readable string.
Examples:
45 → "45s" 125 → "2min 05s" 3661 → "1h 01min" 90000 → "1d 1h"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float | int | None
|
Duration in seconds. Returns "" if None. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted duration string. |
Source code in src/pinky_core/fmt.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
format_fraction(numerator, denominator)
Format a ratio as a readable fraction with percentage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numerator
|
float | None
|
Numerator. Returns "" if None. |
required |
denominator
|
float | None
|
Denominator. Returns "" if None or zero. |
required |
Returns:
| Type | Description |
|---|---|
str
|
String "X / Y (Z.Z%)", or "" if inputs are invalid. |
Source code in src/pinky_core/fmt.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
format_number(num, num_type='count')
Format a number using compact notation (K, M).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num
|
float | int | None
|
Number to format. Returns "" if None. |
required |
num_type
|
str
|
One of: - "count" → compact integer (K, M) - "amount" → compact amount with € suffix - "percent" → value already in % (25.0 → "25.0 %") - "percent_decimal" → decimal value x 100 (0.25 → "25.0 %") |
'count'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with appropriate suffix. |
Source code in src/pinky_core/fmt.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
format_period(period)
Format a period string as MM/YYYY.
Accepts
- "032026" (MMYYYY) → "03/2026"
- "2026-03" (YYYY-MM) → "03/2026"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
period
|
str
|
Period string to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted period MM/YYYY, or the original string if format is unrecognised. |
Source code in src/pinky_core/fmt.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
format_stars(value, max_stars=5)
Format a rating as unicode stars.
Examples:
3.5 → "★★★☆☆" 5 → "★★★★★"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float | int | None
|
Rating value. Returns "" if None. |
required |
max_stars
|
int
|
Maximum number of stars. Defaults to 5. |
5
|
Returns:
| Type | Description |
|---|---|
str
|
String of filled and empty stars. |
Source code in src/pinky_core/fmt.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
format_trend(current, previous)
Format a trend with a directional arrow.
Examples:
(120, 100) → "↑ +20.0%" (80, 100) → "↓ -20.0%" (100, 100) → "→ 0.0%"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current
|
float | None
|
Current value. Returns "" if None. |
required |
previous
|
float | None
|
Previous value. Returns "" if None or zero. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Trend string with arrow and percentage change. |
Source code in src/pinky_core/fmt.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
iso3_to_iso2(code)
Convert an ISO 3166-1 alpha-3 country code to alpha-2.
Returns the input unchanged if it is already alpha-2 or unknown.
Examples:
"FRA" → "FR" "USA" → "US" "FR" → "FR"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
ISO 3166-1 alpha-2 or alpha-3 country code (case-insensitive). |
required |
Returns:
| Type | Description |
|---|---|
str
|
ISO 3166-1 alpha-2 country code. |
Source code in src/pinky_core/fmt.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
normalize_city_fr(city)
Normalize a French city name to its ADP/AFNOR abbreviated form.
Applies :func:normalize_text then substitutes SAINT → ST and
SAINTE → STE (whole words only, not inside longer words).
Examples:
"Saint-Étienne" → "ST ETIENNE" "Sainte-Marie" → "STE MARIE" "Villesaint" → "VILLESAINT" (not a whole word, unchanged)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
city
|
str | None
|
Raw city name. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Normalized city string, or |
Source code in src/pinky_core/fmt.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
normalize_text(text)
Normalize a string for postal/administrative use.
Applies unidecode transliteration, uppercases, then replaces any run of
non-alphanumeric characters with a single space. Returns None for
blank input.
Equivalent to the udf_standardize pattern used in ADP/Workday flows.
Examples:
"Résidence de l'Étoile" → "RESIDENCE DE L ETOILE" "Saint-Étienne" → "SAINT ETIENNE"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str | None
|
Raw string to normalize. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Normalized string, or |
Source code in src/pinky_core/fmt.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
parse_address(address, country_code='FR', zip_code=None, city=None)
Parse a postal address into structured components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
str
|
Raw address string. Can be the street line only
( |
required |
country_code
|
str
|
ISO 3166-1 alpha-2 ( |
'FR'
|
zip_code
|
str | None
|
Postal code from the source data. Overrides extraction
from |
None
|
city
|
str | None
|
City name from the source data. Overrides extraction
from |
None
|
Returns:
| Type | Description |
|---|---|
AddressComponents
|
|
AddressComponents
|
the original input. |
AddressComponents
|
street line for FR addresses. |
Note
US parsing requires the optional usaddress package::
pip install pinky-core[address]
Source code in src/pinky_core/fmt.py
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 | |
period_to_date(period)
Convert a period string to a date object (first day of the month).
Accepts
- "032026" (MMYYYY) → date(2026, 3, 1)
- "2026-03" (YYYY-MM) → date(2026, 3, 1)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
period
|
str
|
Period string to convert. |
required |
Returns:
| Type | Description |
|---|---|
date
|
Date of the first day of the month. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is not recognised. |
Source code in src/pinky_core/fmt.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
to_camel_case(name)
Convert UPPER_SNAKE_CASE or snake_case to camelCase.
Useful for building Snowflake OBJECT keys that follow JSON/camelCase conventions (e.g. UDF return dicts consumed by downstream SQL).
Examples:
"is_valid_email" → "isValidEmail" "COUNTRY_CODE" → "countryCode" "phone" → "phone"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Snake-case or upper-snake-case string to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
camelCase string. |
Source code in src/pinky_core/fmt.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
to_upper_snake_case(name)
Convert a string to UPPER_SNAKE_CASE for Snowflake identifiers.
Uses unidecode to transliterate accented and non-ASCII characters,
then collapses any run of non-alphanumeric characters to a single _.
Examples:
"NAF 2025\nsous-classes" → "NAF_2025_SOUS_CLASSES" "Intitulés" → "INTITULES" "first name" → "FIRST_NAME" "Ñoño" → "NONO"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
String to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
UPPER_SNAKE_CASE string safe for use as a Snowflake identifier. |
Source code in src/pinky_core/fmt.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |