Skip to content

Commit a1ee2e5

Browse files
committed
Add move resource method to Dataset with test
1 parent 2e185a2 commit a1ee2e5

7 files changed

Lines changed: 224 additions & 26 deletions

File tree

.config/pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repos:
88
- id: end-of-file-fixer
99
- id: check-ast
1010
- repo: https://github.com/astral-sh/ruff-pre-commit
11-
rev: v0.8.6
11+
rev: v0.9.6
1212
hooks:
1313
# Run the linter.
1414
- id: ruff
@@ -17,7 +17,7 @@ repos:
1717
- id: ruff-format
1818
args: [--config, .config/ruff.toml]
1919
- repo: https://github.com/astral-sh/uv-pre-commit
20-
rev: 0.5.15
20+
rev: 0.5.29
2121
hooks:
2222
# Run the pip compile
2323
- id: pip-compile

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ dependencies = [
3737
"ckanapi>=4.8",
3838
"defopt>=6.4.0",
3939
"email_validator",
40-
"hdx-python-country>=3.8.6",
41-
"hdx-python-utilities>=3.8.2",
40+
"hdx-python-country>=3.8.8",
41+
"hdx-python-utilities>=3.8.3",
4242
"libhxl>=5.2.2",
4343
"makefun",
4444
"quantulum3",
@@ -54,7 +54,7 @@ content-type = "text/markdown"
5454
Homepage = "https://github.com/OCHA-DAP/hdx-python-api"
5555

5656
[project.optional-dependencies]
57-
test = ["pytest", "pytest-cov", "gspread"]
57+
test = ["pytest", "pytest-check", "pytest-cov", "gspread"]
5858
dev = ["pre-commit"]
5959

6060

@@ -98,7 +98,7 @@ run = """
9898
"""
9999

100100
[tool.hatch.envs.hatch-static-analysis]
101-
dependencies = ["ruff==0.8.6"]
101+
dependencies = ["ruff==0.9.6"]
102102

103103
[tool.hatch.envs.hatch-static-analysis.scripts]
104104
format-check = ["ruff format --config .config/ruff.toml --check --diff {args:.}",]

requirements.txt

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
# uv pip compile pyproject.toml --resolver=backtracking --all-extras -o requirements.txt
33
annotated-types==0.7.0
44
# via pydantic
5-
attrs==24.3.0
5+
attrs==25.1.0
66
# via
77
# frictionless
88
# jsonlines
99
# jsonschema
1010
# referencing
11-
cachetools==5.5.0
11+
cachetools==5.5.1
1212
# via google-auth
13-
certifi==2024.12.14
13+
certifi==2025.1.31
1414
# via requests
1515
cfgv==3.4.0
1616
# via pre-commit
@@ -22,7 +22,7 @@ ckanapi==4.8
2222
# via hdx-python-api (pyproject.toml)
2323
click==8.1.8
2424
# via typer
25-
coverage==7.6.10
25+
coverage==7.6.12
2626
# via pytest-cov
2727
defopt==6.4.0
2828
# via hdx-python-api (pyproject.toml)
@@ -40,27 +40,27 @@ email-validator==2.2.0
4040
# via hdx-python-api (pyproject.toml)
4141
et-xmlfile==2.0.0
4242
# via openpyxl
43-
filelock==3.16.1
43+
filelock==3.17.0
4444
# via virtualenv
4545
frictionless==5.18.0
4646
# via hdx-python-utilities
47-
google-auth==2.37.0
47+
google-auth==2.38.0
4848
# via
4949
# google-auth-oauthlib
5050
# gspread
5151
google-auth-oauthlib==1.2.1
5252
# via gspread
5353
gspread==6.1.4
5454
# via hdx-python-api (pyproject.toml)
55-
hdx-python-country==3.8.6
55+
hdx-python-country==3.8.8
5656
# via hdx-python-api (pyproject.toml)
57-
hdx-python-utilities==3.8.2
57+
hdx-python-utilities==3.8.3
5858
# via
5959
# hdx-python-api (pyproject.toml)
6060
# hdx-python-country
6161
humanize==4.11.0
6262
# via frictionless
63-
identify==2.6.5
63+
identify==2.6.7
6464
# via pre-commit
6565
idna==3.10
6666
# via
@@ -102,7 +102,7 @@ markupsafe==3.0.2
102102
# via jinja2
103103
mdurl==0.1.2
104104
# via markdown-it-py
105-
more-itertools==10.5.0
105+
more-itertools==10.6.0
106106
# via inflect
107107
nodeenv==1.9.1
108108
# via pre-commit
@@ -126,15 +126,15 @@ ply==3.11
126126
# libhxl
127127
pockets==0.9.1
128128
# via sphinxcontrib-napoleon
129-
pre-commit==4.0.1
129+
pre-commit==4.1.0
130130
# via hdx-python-api (pyproject.toml)
131131
pyasn1==0.6.1
132132
# via
133133
# pyasn1-modules
134134
# rsa
135135
pyasn1-modules==0.4.1
136136
# via google-auth
137-
pydantic==2.10.5
137+
pydantic==2.10.6
138138
# via frictionless
139139
pydantic-core==2.27.2
140140
# via pydantic
@@ -145,7 +145,10 @@ pyphonetics==0.5.3
145145
pytest==8.3.4
146146
# via
147147
# hdx-python-api (pyproject.toml)
148+
# pytest-check
148149
# pytest-cov
150+
pytest-check==2.5.0
151+
# via hdx-python-api (pyproject.toml)
149152
pytest-cov==6.0.0
150153
# via hdx-python-api (pyproject.toml)
151154
python-dateutil==2.9.0.post0
@@ -168,7 +171,7 @@ quantulum3==0.9.2
168171
# via hdx-python-api (pyproject.toml)
169172
ratelimit==2.2.1
170173
# via hdx-python-utilities
171-
referencing==0.35.1
174+
referencing==0.36.2
172175
# via
173176
# jsonschema
174177
# jsonschema-specifications
@@ -216,7 +219,7 @@ sphinxcontrib-napoleon==0.7
216219
# via defopt
217220
stringcase==1.2.0
218221
# via frictionless
219-
structlog==24.4.0
222+
structlog==25.1.0
220223
# via libhxl
221224
tableschema-to-template==0.0.13
222225
# via hdx-python-utilities
@@ -235,6 +238,7 @@ typing-extensions==4.12.2
235238
# frictionless
236239
# pydantic
237240
# pydantic-core
241+
# referencing
238242
# typeguard
239243
# typer
240244
unidecode==1.3.8
@@ -247,7 +251,7 @@ urllib3==2.3.0
247251
# requests
248252
validators==0.34.0
249253
# via frictionless
250-
virtualenv==20.28.1
254+
virtualenv==20.29.2
251255
# via pre-commit
252256
wheel==0.45.1
253257
# via libhxl
@@ -257,7 +261,7 @@ xlrd3==1.1.0
257261
# via libhxl
258262
xlsx2csv==0.8.4
259263
# via hdx-python-utilities
260-
xlsxwriter==3.2.0
264+
xlsxwriter==3.2.2
261265
# via tableschema-to-template
262266
xlwt==1.3.0
263267
# via hdx-python-utilities

src/hdx/data/dataset.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,39 @@ def reorder_resources(
423423
if hxl_update:
424424
self.hxl_update()
425425

426+
def move_resource(
427+
self,
428+
resource_name: str,
429+
insert_before: str,
430+
) -> "Resource":
431+
"""Move resource in dataset to be before the resource whose name starts
432+
with the value of insert_before.
433+
434+
Args:
435+
resource_name (str): Name of resource to move
436+
insert_before (str): Resource to insert before
437+
438+
Returns:
439+
Resource: The resource that was moved
440+
"""
441+
from_index = None
442+
to_index = None
443+
for i, resource in enumerate(self.resources):
444+
res_name = resource["name"]
445+
if res_name == resource_name:
446+
from_index = i
447+
elif res_name.startswith(insert_before):
448+
to_index = i
449+
if to_index is None:
450+
# insert at the start if a manual resource for year cannot be found
451+
to_index = 0
452+
resource = self.resources.pop(from_index)
453+
if from_index < to_index:
454+
# to index was calculated while element was in front
455+
to_index -= 1
456+
self.resources.insert(to_index, resource)
457+
return resource
458+
426459
def update_from_yaml(
427460
self, path: str = join("config", "hdx_dataset_static.yaml")
428461
) -> None:

0 commit comments

Comments
 (0)