Skip to content

Commit 6ee92f0

Browse files
committed
Add no_empty parameter to generate_resource
1 parent 0f402f5 commit 6ee92f0

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

requirements.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.2
198200
# via libhxl
199201
xlrd==2.0.2
200202
# via hdx-python-utilities

src/hdx/data/dataset.py

Lines changed: 12 additions & 7 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)
@@ -2505,7 +2507,7 @@ def process_row(row: Sequence | Mapping) -> Sequence | Mapping | None:
25052507
encoding=encoding,
25062508
row_function=process_row,
25072509
)
2508-
if not rows:
2510+
if not rows and no_empty:
25092511
logger.error(f"No data rows in {filename}!")
25102512
return False, retdict
25112513
if yearcol is not None or date_function is not None:
@@ -2731,6 +2733,7 @@ def download_generate_resource(
27312733
datecol: int | str | None = None,
27322734
yearcol: int | str | None = None,
27332735
date_function: Callable[[dict], dict | None] | None = None,
2736+
no_empty: bool = True,
27342737
**kwargs: Any,
27352738
) -> tuple[bool, dict]:
27362739
"""Download url, write rows to csv and create resource, adding to it
@@ -2769,9 +2772,10 @@ def download_generate_resource(
27692772
columns: Columns to write. Defaults to all.
27702773
format: Format to write. Defaults to csv.
27712774
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,
2775+
datecol: Date column for setting time period. Defaults to None (don't set).
2776+
yearcol: Year column for setting dataset year range. Defaults to None (don't set).
2777+
date_function: Date function to call for each row. Defaults to None.
2778+
no_empty: Don't generate resource if there are no data rows. Defaults to True.
27752779
**kwargs: Any additional args to pass to downloader.get_tabular_rows
27762780
27772781
Returns:
@@ -2797,6 +2801,7 @@ def download_generate_resource(
27972801
datecol=datecol,
27982802
yearcol=yearcol,
27992803
date_function=date_function,
2804+
no_empty=no_empty,
28002805
)
28012806

28022807
def download_and_generate_resource(

0 commit comments

Comments
 (0)