Skip to content

Commit 5e0282a

Browse files
author
Max Wang
committed
some more cleanup
1 parent e8fac2e commit 5e0282a

2 files changed

Lines changed: 49 additions & 39 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ for page in pages: # page is list[dict]
272272
### Custom table (metadata) example
273273

274274
```python
275-
# Support multi-lingual enums
275+
# Support enums with labels in different languages
276276
class Status(IntEnum):
277277
Active = 1
278278
Inactive = 2

examples/quickstart.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ class Status(IntEnum):
8282
"Archived": "Archivé",
8383
}
8484
}
85-
table_info = None
86-
created_this_run = False
8785

8886
print("Ensure custom table exists (Metadata):")
87+
table_info = None
88+
created_this_run = False
8989

9090
# Check for existing table using list_tables
9191
log_call("client.list_tables()")
@@ -169,6 +169,20 @@ def print_line_summaries(label: str, summaries: list[dict]) -> None:
169169
f"count={s.get('count')} amount={s.get('amount')} when={s.get('when')}"
170170
)
171171

172+
def _resolve_status_value(kind: str, raw_value, use_french: bool):
173+
"""kind values:
174+
- 'label': English label
175+
- 'fr_label': French label if allowed, else fallback to English equivalent
176+
- 'int': the enum integer value
177+
"""
178+
if kind == "label":
179+
return raw_value
180+
if kind == "fr_label":
181+
if use_french:
182+
return raw_value
183+
return "Active" if raw_value == "Actif" else "Inactive"
184+
return raw_value
185+
172186
def _has_installed_language(base_url: str, credential, lcid: int) -> bool:
173187
try:
174188
token = credential.get_token(f"{base_url}/.default").token
@@ -188,6 +202,7 @@ def _has_installed_language(base_url: str, credential, lcid: int) -> bool:
188202
except Exception:
189203
return False
190204

205+
# if French language (1036) is installed, we use labels in both English and French
191206
use_french_labels = _has_installed_language(base_url, credential, 1036)
192207
if use_french_labels:
193208
print({"labels_language": "fr", "note": "French labels in use."})
@@ -205,50 +220,45 @@ def _has_installed_language(base_url: str, credential, lcid: int) -> bool:
205220
amount_key: 123.45,
206221
when_key: "2025-01-01",
207222
f"{attr_prefix}_active": True,
208-
status_key: ("Actif" if use_french_labels else Status.Active.value), # label or int
223+
status_key: ("Actif" if use_french_labels else Status.Active.value),
209224
}
210225
# Generate multiple payloads
211226
# Distribution update: roughly one-third English labels, one-third French labels, one-third raw integer values.
212227
# We cycle per record: index % 3 == 1 -> English label, == 2 -> French label (if available, else English), == 0 -> integer value.
213228
multi_payloads: list[dict] = []
214-
total_multi = 15
215229
base_date = date(2025, 1, 2)
216-
# Fixed 6-step cycle pattern: Active, Inactive, Actif, Inactif, 1, 2 (repeat)
217-
# If French not present, Actif/Inactif positions fallback to Active/Inactive.
218-
pattern_cycle = [
219-
("label", "Active"),
220-
("label", "Inactive"),
221-
("fr_label", "Actif"),
222-
("fr_label", "Inactif"),
223-
("int", Status.Active.value),
224-
("int", Status.Inactive.value),
225-
]
226-
cycle_len = len(pattern_cycle)
227-
for i in range(1, total_multi + 1):
228-
kind, val = pattern_cycle[(i - 1) % cycle_len]
229-
if kind == "label":
230-
status_field_value = val # English label
231-
elif kind == "fr_label":
232-
if use_french_labels:
233-
status_field_value = val
234-
else:
235-
# Fallback to equivalent English (Actif->Active, Inactif->Inactive)
236-
status_field_value = "Active" if val == "Actif" else "Inactive"
237-
else: # int
238-
status_field_value = val
239-
multi_payloads.append(
240-
{
241-
f"{attr_prefix}_name": f"Sample {i:02d}",
242-
code_key: f"X{200 + i:03d}",
243-
count_key: 5 * i,
244-
amount_key: round(10.0 * i, 2),
245-
when_key: (base_date + timedelta(days=i - 1)).isoformat(),
246-
f"{attr_prefix}_active": True,
247-
status_key: status_field_value,
248-
}
249-
)
230+
# Fixed 6-step cycle pattern encapsulated in helper: Active, Inactive, Actif, Inactif, 1, 2 (repeat)
231+
def _status_value_for_index(idx: int, use_french: bool):
232+
pattern = [
233+
("label", "Active"),
234+
("label", "Inactive"),
235+
("fr_label", "Actif"),
236+
("fr_label", "Inactif"),
237+
("int", Status.Active.value),
238+
("int", Status.Inactive.value),
239+
]
240+
kind, raw = pattern[(idx - 1) % len(pattern)]
241+
if kind == "label":
242+
return raw
243+
if kind == "fr_label":
244+
if use_french:
245+
return raw
246+
return "Active" if raw == "Actif" else "Inactive"
247+
return raw
248+
249+
for i in range(1, 16):
250+
multi_payloads.append({
251+
f"{attr_prefix}_name": f"Sample {i:02d}",
252+
code_key: f"X{200 + i:03d}",
253+
count_key: 5 * i,
254+
amount_key: round(10.0 * i, 2),
255+
when_key: (base_date + timedelta(days=i - 1)).isoformat(),
256+
f"{attr_prefix}_active": True,
257+
status_key: _status_value_for_index(i, use_french_labels),
258+
})
250259

251260
record_ids: list[str] = []
261+
252262
try:
253263
# Single create returns list[str] (length 1)
254264
log_call(f"client.create('{entity_set}', single_payload)")

0 commit comments

Comments
 (0)