Skip to content

Commit eac027e

Browse files
authored
Time period, format and retry fix (#58)
Rename reference_period to time_period Rename file_type to format Update docs Create deprecated functions for backwards compatibility for now Update hdx python utilities Parameter renamed to allowed_methods (fixes retrying) Attempt to add 429 logic to retry after a certain number of seconds given by Retry-After Disable 429 specific rate limiting until Retry-After is available
1 parent 1bf8830 commit eac027e

File tree

12 files changed

+358
-311
lines changed

12 files changed

+358
-311
lines changed

documentation/main.md

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ upload your datasets to HDX.
2121
- [Configuring Logging](#configuring-logging)
2222
- [Operations on HDX Objects](#operations-on-hdx-objects)
2323
- [Dataset Specific Operations](#dataset-specific-operations)
24-
- [Reference Period](#reference-period)
24+
- [Time Period](#time-period)
2525
- [Expected Update Frequency](#expected-update-frequency)
2626
- [Location](#location)
2727
- [Tags](#tags)
@@ -52,6 +52,10 @@ The library has detailed API documentation which can be found in the menu at the
5252

5353

5454
## Breaking Changes
55+
From 6.1.5, any method or parameter with "reference_period" in it is renamed
56+
to "time_period" and any method or parameter with "file_type" in it is renamed
57+
to "format"
58+
5559
From 6.0.0, generate_resource_view is renamed to generate_quickcharts
5660

5761
From 5.9.9, get_location_iso3s returns uppercase codes instead of lowercase
@@ -221,7 +225,7 @@ virtualenv if not installed:
221225
from HDX and view the date of the dataset:
222226

223227
dataset = Dataset.read_from_hdx("novel-coronavirus-2019-ncov-cases")
224-
print(dataset.get_reference_period())
228+
print(dataset.get_time_period())
225229

226230
11. You can search for datasets on HDX and get their resources:
227231

@@ -239,14 +243,14 @@ virtualenv if not installed:
239243
server. With a dataset to which you have permissions, change the dataset date:
240244

241245
dataset = Dataset.read_from_hdx("ID OR NAME OF DATASET")
242-
print(dataset.get_reference_period()) # record this
243-
dataset.set_reference_period("2015-07-26")
244-
print(dataset.get_reference_period())
246+
print(dataset.get_time_period()) # record this
247+
dataset.set_time_period("2015-07-26")
248+
print(dataset.get_time_period())
245249
dataset.update_in_hdx()
246250

247251
14. You can view it on HDX before changing it back (if you have an API key):
248252

249-
dataset.set_reference_period("PREVIOUS DATE")
253+
dataset.set_time_period("PREVIOUS DATE")
250254
dataset.update_in_hdx()
251255

252256
15. If you are storing your data on HDX, you can upload a new file to a
@@ -532,47 +536,54 @@ object to update not existing.
532536

533537
A dataset can have resources and can be in a showcase.
534538

535-
If you wish to add resources, you can supply a list and call
536-
the **add_update_resources** function, for example:
537-
538-
resources = [{
539-
"name": xlsx_resourcename,
540-
"format": "xlsx",
541-
"url": xlsx_url
542-
}, {
543-
"name": csv_resourcename,
544-
"format": "zipped csv",
545-
"url": csv_url
546-
}]
547-
for resource in resources:
548-
resource["description"] = resource["url"].rsplit("/", 1)[-1]
549-
dataset.add_update_resources(resources)
550-
551-
Calling **add_update_resources** creates a list of HDX Resource objects in
552-
dataset and operations can be performed on those objects.
553-
554-
To see the list of resources, you use the **get_resources** function eg.
539+
If you wish to add a resource, you can create a resource dictionary and set the
540+
format then call the **add_update_resource** function, for example:
555541

556-
resources = dataset.get_resources()
542+
resource = Resource({
543+
"name": "myfile.xlsx",
544+
"description": "description",
545+
})
546+
resource.set_format("xlsx")
547+
resource.set_file_to_upload(PATH_TO_FILE)
548+
dataset.add_update_resource(resource)
557549

558-
If you wish to add one resource, you can supply an id string, dictionary or Resource
559-
object and call the **add_update_resource** function, for example:
550+
It is also possible to supply a resource id string or dictionary to the
551+
**add_update_resource** function. A url can be given instead of uploading a
552+
file to the HDX filestore (although using the filestore is preferred):
560553

554+
resource = Resource({
555+
"name": "myfile.xlsx",
556+
"description": "description",
557+
"url": "https://www.blah.com/myfile.xlsx"
558+
})
559+
resource.set_format("xlsx")
561560
dataset.add_update_resource(resource)
562561

563-
You can delete a Resource object from the dataset using the **delete_resource** function, for example:
562+
You can delete a Resource object from the dataset using the **delete_resource**
563+
function, for example:
564564

565565
dataset.delete_resource(resource)
566566

567+
**add_update_resources** creates a list of HDX Resource objects
568+
in a dataset:
569+
570+
dataset.add_update_resources(resources)
571+
572+
To see the list of resources, you use the **get_resources** function eg.
573+
574+
resources = dataset.get_resources()
575+
567576
You can get all the resources from a list of datasets as follows:
568577

569578
resources = Dataset.get_all_resources(datasets)
570579

571-
To see the list of showcases a dataset is in, you use the **get_showcases** function eg.
580+
To see the list of showcases a dataset is in, you use the **get_showcases**
581+
function eg.
572582

573583
showcases = dataset.get_showcases()
574584

575-
If you wish to add the dataset to a showcase, you must first create the showcase in HDX if it does not already exist:
585+
If you wish to add the dataset to a showcase, you must first create the
586+
showcase in HDX if it does not already exist:
576587

577588
showcase = Showcase({"name": "new-showcase-1",
578589
"title": "MyShowcase1",
@@ -582,47 +593,47 @@ If you wish to add the dataset to a showcase, you must first create the showcase
582593
"url": "http://visualisation/url/"})
583594
showcase.create_in_hdx()
584595

585-
Then you can supply an id, dictionary or Showcase object and call the **add_showcase**
586-
function, for example:
596+
Then you can supply an id, dictionary or Showcase object and call the
597+
**add_showcase** function, for example:
587598

588599
dataset.add_showcase(showcase)
589600

590-
You can remove the dataset from a showcase using the **remove_showcase** function, for
591-
example:
601+
You can remove the dataset from a showcase using the **remove_showcase**
602+
function, for example:
592603

593604
dataset.remove_showcase(showcase)
594605

595-
### Reference Period
606+
### Time Period
596607

597-
Reference Period is a mandatory field in HDX. It is the time period for which
598-
data are collected or calculated and to which, as a result, they refer. The
599-
reference period may be of any length: a year, a month, or even a day. It
600-
should not to be confused with when data was last added/changed in the dataset.
601-
It can be a single date or a range.
608+
Time Period is a mandatory field in HDX. It is the earliest start date and
609+
latest end date across all the resources included in the dataset. The time
610+
period may be of any length: a year, a month, or even a day. It should not to
611+
be confused with when data was last added/changed in the dataset. It can be a
612+
single date or a range.
602613

603-
To get the reference period, you can do as shown below. It returns a dictionary containing
604-
keys "startdate" (start date as datetime), "enddate" (end date as datetime),
605-
"startdate_str" (start date as string), "enddate_str" (end date as string) and ongoing
606-
(whether the end date is a rolls forward every day). You can supply a
607-
[date format](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior).
614+
To get the time period, you can do as shown below. It returns a dictionary
615+
containing keys "startdate" (start date as datetime), "enddate" (end date as
616+
datetime), "startdate_str" (start date as string), "enddate_str" (end date as
617+
string) and ongoing (whether the end date is a rolls forward every day). You
618+
can supply a [date format](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior).
608619
If you do not, the output format will be an
609620
[ISO 8601 date](https://en.wikipedia.org/wiki/ISO_8601) eg. 2007-01-25.
610621

611-
reference_period = dataset.get_reference_period("OPTIONAL FORMAT")
622+
time_period = dataset.get_time_period("OPTIONAL FORMAT")
612623

613-
To set the reference period, you must pass either datetime.datetime objects or strings to
624+
To set the time period, you must pass either datetime.datetime objects or strings to
614625
the function below. It accepts a start date and an optional end date which if not
615626
supplied is assumed to be the same as the start date. Instead of the end date, the flag
616627
"ongoing" which by default is False can be set to True which indicates that the end date
617628
rolls forward every day.
618629

619-
dataset.set_reference_period("START DATE", "END DATE")
630+
dataset.set_time_period("START DATE", "END DATE")
620631

621-
The method below allows you to set the reference period using a year range. The start and
632+
The method below allows you to set the time period using a year range. The start and
622633
end year can be supplied as integers or strings. If no end year is supplied then the
623634
range will be from the beginning of the start year to the end of that year.
624635

625-
dataset.set_reference_period_year_range(START YEAR, END YEAR)
636+
dataset.set_time_period_year_range(START YEAR, END YEAR)
626637

627638
### Expected Update Frequency
628639

@@ -794,7 +805,7 @@ A resource can be generated from a given list or tuple: HEADERS and an ITERATOR
794805
which can return rows in list, tuple or dictionary form. A mapping from headers
795806
to HXL hashtags, HXLTAGS, must be provided along with the FOLDER and FILENAME
796807
where the file will be generated for upload to the filestore. The dataset
797-
reference period can optionally be set by supplying DATECOL for looking up
808+
time period can optionally be set by supplying DATECOL for looking up
798809
dates or YEARCOL for looking up years. DATECOl and YEARCOL can be a column name
799810
or the index of a column. Note that any timezone information is ignored and UTC
800811
is assumed.
@@ -804,7 +815,7 @@ in a row. It should accept a row and should return None to ignore the row or a
804815
dictionary which can either be empty if there are no dates in the row or can be
805816
populated with keys startdate and/or enddate which are of type timezone-aware
806817
datetime. The lowest start date and highest end date are used to set the
807-
reference period and are returned in the results dictionary in keys startdate
818+
time period and are returned in the results dictionary in keys startdate
808819
and enddate.
809820

810821
dataset.generate_resource_from_iterator(HEADERS, ITERATOR, HXLTAGS,

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ dependencies = [
3737
"ckanapi>=4.7",
3838
"defopt>=6.4.0",
3939
"email_validator",
40-
"hdx-python-country>=3.5.9",
41-
"hdx-python-utilities>=3.6.2",
40+
"hdx-python-country>=3.6.3",
41+
"hdx-python-utilities>=3.6.3",
4242
"libhxl>=5.1",
4343
"makefun",
4444
"ndg-httpsclient",

requirements.txt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ attrs==23.1.0
1111
# frictionless
1212
# jsonlines
1313
# jsonschema
14-
certifi==2023.7.22
14+
certifi==2023.11.17
1515
# via requests
1616
cffi==1.16.0
1717
# via cryptography
@@ -29,11 +29,11 @@ colorama==0.4.6
2929
# via typer
3030
coverage[toml]==7.3.2
3131
# via pytest-cov
32-
cryptography==41.0.5
32+
cryptography==41.0.7
3333
# via pyopenssl
3434
defopt==6.4.0
3535
# via hdx-python-api (pyproject.toml)
36-
distlib==0.3.7
36+
distlib==0.3.8
3737
# via virtualenv
3838
dnspython==2.4.2
3939
# via email-validator
@@ -51,17 +51,17 @@ filelock==3.13.1
5151
# via virtualenv
5252
frictionless==5.16.0
5353
# via hdx-python-utilities
54-
hdx-python-country==3.5.9
54+
hdx-python-country==3.6.3
5555
# via hdx-python-api (pyproject.toml)
56-
hdx-python-utilities==3.6.2
56+
hdx-python-utilities==3.6.3
5757
# via
5858
# hdx-python-api (pyproject.toml)
5959
# hdx-python-country
60-
humanize==4.8.0
60+
humanize==4.9.0
6161
# via frictionless
62-
identify==2.5.31
62+
identify==2.5.33
6363
# via pre-commit
64-
idna==3.4
64+
idna==3.6
6565
# via
6666
# email-validator
6767
# requests
@@ -89,11 +89,11 @@ libhxl==5.1
8989
# hdx-python-country
9090
loguru==0.7.2
9191
# via hdx-python-utilities
92-
makefun==1.15.1
92+
makefun==1.15.2
9393
# via hdx-python-api (pyproject.toml)
9494
markdown-it-py==3.0.0
9595
# via rich
96-
marko==2.0.1
96+
marko==2.0.2
9797
# via frictionless
9898
markupsafe==2.1.3
9999
# via jinja2
@@ -111,7 +111,7 @@ packaging==23.2
111111
# via pytest
112112
petl==1.7.14
113113
# via frictionless
114-
platformdirs==3.11.0
114+
platformdirs==4.1.0
115115
# via virtualenv
116116
pluggy==1.3.0
117117
# via pytest
@@ -121,21 +121,21 @@ ply==3.11
121121
# libhxl
122122
pockets==0.9.1
123123
# via sphinxcontrib-napoleon
124-
pre-commit==3.5.0
124+
pre-commit==3.6.0
125125
# via hdx-python-api (pyproject.toml)
126-
pyasn1==0.5.0
126+
pyasn1==0.5.1
127127
# via
128128
# hdx-python-api (pyproject.toml)
129129
# ndg-httpsclient
130130
pycparser==2.21
131131
# via cffi
132-
pydantic==2.4.2
132+
pydantic==2.5.2
133133
# via
134134
# frictionless
135135
# inflect
136-
pydantic-core==2.10.1
136+
pydantic-core==2.14.5
137137
# via pydantic
138-
pygments==2.16.1
138+
pygments==2.17.2
139139
# via rich
140140
pyopenssl==23.3.0
141141
# via
@@ -182,7 +182,7 @@ requests-file==1.5.1
182182
# via hdx-python-utilities
183183
rfc3986==2.0.0
184184
# via frictionless
185-
rich==13.6.0
185+
rich==13.7.0
186186
# via typer
187187
ruamel-yaml==0.18.5
188188
# via hdx-python-utilities
@@ -214,7 +214,7 @@ text-unidecode==1.3
214214
# via python-slugify
215215
typer[all]==0.9.0
216216
# via frictionless
217-
typing-extensions==4.8.0
217+
typing-extensions==4.9.0
218218
# via
219219
# frictionless
220220
# inflect
@@ -225,15 +225,15 @@ unidecode==1.3.7
225225
# via
226226
# libhxl
227227
# pyphonetics
228-
urllib3==2.0.7
228+
urllib3==2.1.0
229229
# via
230230
# libhxl
231231
# requests
232232
validators==0.22.0
233233
# via frictionless
234-
virtualenv==20.24.6
234+
virtualenv==20.25.0
235235
# via pre-commit
236-
wheel==0.41.3
236+
wheel==0.42.0
237237
# via libhxl
238238
xlrd==2.0.1
239239
# via hdx-python-utilities

src/hdx/api/configuration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def create_session_user_agent(
426426
427427
"""
428428
if not session:
429-
whitelist = (
429+
allowed_methods = (
430430
"HEAD",
431431
"TRACE",
432432
"GET",
@@ -441,7 +441,7 @@ def create_session_user_agent(
441441
user_agent_lookup,
442442
use_env,
443443
prefix=Configuration.prefix,
444-
method_whitelist=whitelist,
444+
allowed_methods=allowed_methods,
445445
**kwargs,
446446
)
447447
ua = session.headers["User-Agent"]

0 commit comments

Comments
 (0)