Skip to content

Commit 238c651

Browse files
authored
HDXDSYS-2660 Add facility to allow writing resources with headers and no data (#105)
* Add no_empty parameter to generate_resource
1 parent 0f402f5 commit 238c651

4 files changed

Lines changed: 35 additions & 10 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dependencies = [
3838
"defopt>=7.0.0",
3939
"email_validator",
4040
"hdx-python-country>=4.0.1",
41-
"hdx-python-utilities>=4.0.2",
41+
"hdx-python-utilities>=4.0.3",
4242
"libhxl>=5.2.2",
4343
"makefun",
4444
"quantulum3",

requirements.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ frictionless==5.18.1
3636
# via hdx-python-utilities
3737
hdx-python-country==4.0.1
3838
# via hdx-python-api (pyproject.toml)
39-
hdx-python-utilities==4.0.2
39+
hdx-python-utilities==4.0.3
4040
# via
4141
# hdx-python-api (pyproject.toml)
4242
# hdx-python-country
@@ -86,6 +86,8 @@ num2words==0.5.14
8686
# via quantulum3
8787
openpyxl==3.1.5
8888
# via hdx-python-utilities
89+
packaging==26.0
90+
# via wheel
8991
petl==1.7.17
9092
# via frictionless
9193
ply==3.11
@@ -144,7 +146,7 @@ rpds-py==0.30.0
144146
# referencing
145147
ruamel-yaml==0.19.1
146148
# via hdx-python-utilities
147-
setuptools==80.9.0
149+
setuptools==80.10.1
148150
# via ckanapi
149151
shellingham==1.5.4
150152
# via typer
@@ -194,7 +196,7 @@ urllib3==2.6.3
194196
# requests
195197
validators==0.35.0
196198
# via frictionless
197-
wheel==0.45.1
199+
wheel==0.46.3
198200
# via libhxl
199201
xlrd==2.0.2
200202
# via hdx-python-utilities

src/hdx/data/dataset.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,6 +2410,7 @@ def generate_resource(
24102410
datecol: int | str | None = None,
24112411
yearcol: int | str | None = None,
24122412
date_function: Callable[[dict], dict | None] | None = None,
2413+
no_empty: bool = True,
24132414
) -> tuple[bool, dict]:
24142415
"""Write rows to file and create resource, adding it to the dataset. The headers
24152416
argument is either a row number (rows start counting at 1), or the actual
@@ -2440,9 +2441,10 @@ def generate_resource(
24402441
columns: Columns to write. Defaults to all.
24412442
format: Format to write. Defaults to csv.
24422443
encoding: Encoding to use. Defaults to None (infer encoding).
2443-
datecol: Optional[Union[int, str]] = None,
2444-
yearcol: Optional[Union[int, str]] = None,
2445-
date_function: Optional[Callable[[Dict], Optional[Dict]]] = None,
2444+
datecol: Date column for setting time period. Defaults to None (don't set).
2445+
yearcol: Year column for setting dataset year range. Defaults to None (don't set).
2446+
date_function: Date function to call for each row. Defaults to None.
2447+
no_empty: Don't generate resource if there are no data rows. Defaults to True.
24462448
24472449
Returns:
24482450
(True if resource added, dictionary of results)
@@ -2504,6 +2506,7 @@ def process_row(row: Sequence | Mapping) -> Sequence | Mapping | None:
25042506
format=format,
25052507
encoding=encoding,
25062508
row_function=process_row,
2509+
no_empty=no_empty,
25072510
)
25082511
if not rows:
25092512
logger.error(f"No data rows in {filename}!")
@@ -2731,6 +2734,7 @@ def download_generate_resource(
27312734
datecol: int | str | None = None,
27322735
yearcol: int | str | None = None,
27332736
date_function: Callable[[dict], dict | None] | None = None,
2737+
no_empty: bool = True,
27342738
**kwargs: Any,
27352739
) -> tuple[bool, dict]:
27362740
"""Download url, write rows to csv and create resource, adding to it
@@ -2769,9 +2773,10 @@ def download_generate_resource(
27692773
columns: Columns to write. Defaults to all.
27702774
format: Format to write. Defaults to csv.
27712775
encoding: Encoding to use. Defaults to None (infer encoding).
2772-
datecol: Optional[Union[int, str]] = None,
2773-
yearcol: Optional[Union[int, str]] = None,
2774-
date_function: Optional[Callable[[Dict], Optional[Dict]]] = None,
2776+
datecol: Date column for setting time period. Defaults to None (don't set).
2777+
yearcol: Year column for setting dataset year range. Defaults to None (don't set).
2778+
date_function: Date function to call for each row. Defaults to None.
2779+
no_empty: Don't generate resource if there are no data rows. Defaults to True.
27752780
**kwargs: Any additional args to pass to downloader.get_tabular_rows
27762781
27772782
Returns:
@@ -2797,6 +2802,7 @@ def download_generate_resource(
27972802
datecol=datecol,
27982803
yearcol=yearcol,
27992804
date_function=date_function,
2805+
no_empty=no_empty,
28002806
)
28012807

28022808
def download_and_generate_resource(

tests/hdx/data/test_dataset_resource_generation.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,23 @@ def process_year(row):
500500
)
501501
assert success is False
502502
url = "https://raw.githubusercontent.com/OCHA-DAP/hdx-python-api/main/tests/fixtures/gen_resource/test_data_no_data.csv"
503+
success, results = dataset.download_generate_resource(
504+
downloader,
505+
url,
506+
folder,
507+
filename,
508+
resourcedata,
509+
)
510+
assert success is False
511+
success, results = dataset.download_generate_resource(
512+
downloader,
513+
url,
514+
folder,
515+
filename,
516+
resourcedata,
517+
no_empty=False,
518+
)
519+
assert success is True
503520
success, results = dataset.download_generate_resource(
504521
downloader,
505522
url,

0 commit comments

Comments
 (0)