ustrade 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ustrade-0.4.0/LICENSE.txt +21 -0
- ustrade-0.4.0/PKG-INFO +189 -0
- ustrade-0.4.0/README.md +175 -0
- ustrade-0.4.0/pyproject.toml +30 -0
- ustrade-0.4.0/setup.cfg +4 -0
- ustrade-0.4.0/tests/test_api.py +28 -0
- ustrade-0.4.0/tests/test_client.py +40 -0
- ustrade-0.4.0/tests/test_hscode.py +33 -0
- ustrade-0.4.0/ustrade/__init__.py +175 -0
- ustrade-0.4.0/ustrade/client.py +490 -0
- ustrade-0.4.0/ustrade/codes.py +73 -0
- ustrade-0.4.0/ustrade/countries.py +35 -0
- ustrade-0.4.0/ustrade/data/country_codes.csv +241 -0
- ustrade-0.4.0/ustrade/data/harmonized-system.csv +6941 -0
- ustrade-0.4.0/ustrade/errors.py +47 -0
- ustrade-0.4.0/ustrade.egg-info/PKG-INFO +189 -0
- ustrade-0.4.0/ustrade.egg-info/SOURCES.txt +25 -0
- ustrade-0.4.0/ustrade.egg-info/dependency_links.txt +1 -0
- ustrade-0.4.0/ustrade.egg-info/requires.txt +2 -0
- ustrade-0.4.0/ustrade.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 fantinsib
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
ustrade-0.4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ustrade
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Python client for the U.S. Census Bureau International Trade API
|
|
5
|
+
Author: Fantin Sibony
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/fantinsib/ustrade
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Requires-Dist: requests
|
|
12
|
+
Requires-Dist: pandas
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# ustrade
|
|
16
|
+
|
|
17
|
+
> A lightweight and intuitive Python client for the **U.S. Census Bureau International Trade API**.
|
|
18
|
+
> Allows to fetch imports and exports between the US and commercial partners - based on Harmonized System codes (HS)
|
|
19
|
+
|
|
20
|
+
<p align="left">
|
|
21
|
+
<img src="https://img.shields.io/badge/python-3.10%2B-blue" />
|
|
22
|
+
<img src="https://img.shields.io/badge/status-active-success" />
|
|
23
|
+
<img src="https://img.shields.io/badge/license-MIT-green" />
|
|
24
|
+
</p>
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
|
|
30
|
+
- **Simple API**: `ust.get_imports()`, `ust.get_exports()`
|
|
31
|
+
- Automatic normalization of country inputs:
|
|
32
|
+
- `"France"`
|
|
33
|
+
- `"FR"` (ISO2)
|
|
34
|
+
- `"4279"` (Census code)
|
|
35
|
+
- HS codes lookup + product descriptions
|
|
36
|
+
- Standardized DataFrame output with clean column names
|
|
37
|
+
- Uses a cached internal client for efficiency
|
|
38
|
+
- Zero configuration required
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
### PyPI Install
|
|
45
|
+
|
|
46
|
+
Install with pip :
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install ustrade
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Github
|
|
53
|
+
Clone this repository and install via pip in editable mode:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
git clone https://github.com/fantinsib/ustrade.git
|
|
57
|
+
cd ustrade
|
|
58
|
+
pip install -e .
|
|
59
|
+
```
|
|
60
|
+
## Quick Example
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
|
|
64
|
+
import ustrade as ust
|
|
65
|
+
|
|
66
|
+
# Example: Mexican imports of fruits and nuts between January 2010 and January 2025
|
|
67
|
+
df = ust.get_imports_on_period("Mexico", "08", "2010-01", "2025-01")
|
|
68
|
+
print(df)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Full API Reference
|
|
72
|
+
|
|
73
|
+
## *Fetching Data*
|
|
74
|
+
|
|
75
|
+
### • `get_imports(country, product, date)`
|
|
76
|
+
Fetch monthly import data for a given country and HS code.
|
|
77
|
+
|
|
78
|
+
**Parameters:**
|
|
79
|
+
- `country` — country name (`France`), ISO2 (`FR`), Census code (`4279`) or `Country` instance, or a list of the previous
|
|
80
|
+
- `product` — HS code as string (e.g. `2701`) or list of string
|
|
81
|
+
- `date` — `YYYY-MM` format (e.g. `2020-01`)
|
|
82
|
+
|
|
83
|
+
**Example:**
|
|
84
|
+
```python
|
|
85
|
+
ust.get_imports("FR", "10", "2025-01")
|
|
86
|
+
ust.get_imports(["France", "GB"], ["12", "13"], "2018-03")
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
### • `get_exports(country, product, date)`
|
|
92
|
+
Fetch monthly export data for a given country and HS code.
|
|
93
|
+
|
|
94
|
+
**Example:**
|
|
95
|
+
```python
|
|
96
|
+
ust.get_exports("GB", "73", "2019-01")
|
|
97
|
+
ust.get_exports(["France", "GB"], ["08", "09"], "2018-03")
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
### • `get_imports_on_period(country, product, start, end)`
|
|
103
|
+
Fetch a DataFrame containing monthly imports for a given country and HS code
|
|
104
|
+
|
|
105
|
+
**Parameters:**
|
|
106
|
+
- `country` — country name (`France`), ISO2 (`FR`), Census code (`4279`) or `Country` instance, or list of the previous
|
|
107
|
+
- `product` — HS code as string (e.g. `2701`) or list of string
|
|
108
|
+
- `start` and `end` — `YYYY-MM` format (e.g. `2020-01`)
|
|
109
|
+
|
|
110
|
+
**Example:**
|
|
111
|
+
```python
|
|
112
|
+
ust.get_imports_on_period("Mexico", "27", "2010-01", "2025-01")
|
|
113
|
+
ust.get_imports_on_period(["France", "DE", "GB"], ["09", "08", "07"], "2016-01", "2018-01")
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
### • `get_exports_on_period(country, product, start, end)`
|
|
119
|
+
Fetch a DataFrame containing monthly exports for a given country and HS code
|
|
120
|
+
|
|
121
|
+
**Example:**
|
|
122
|
+
```python
|
|
123
|
+
ust.get_exports_on_period("France", "10", "2020-01", "2023-01")
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## *Exploring Codes*
|
|
129
|
+
|
|
130
|
+
HS Codes follow a tree hierarchy.
|
|
131
|
+
|
|
132
|
+
### • `get_desc_from_code(hs)`
|
|
133
|
+
Return the description associated with an HS code.
|
|
134
|
+
|
|
135
|
+
**Example:**
|
|
136
|
+
```python
|
|
137
|
+
ust.get_desc_from_code("2701")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### • `get_children_codes(code, return_names)`
|
|
141
|
+
Return a dictionnary of the codes and descriptions attached to the parent code if return_names is True, and a list of the code if return_names if False.
|
|
142
|
+
|
|
143
|
+
**Example:**
|
|
144
|
+
```python
|
|
145
|
+
ust.get_children_codes("10")
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
### • `get_product(hs)`
|
|
150
|
+
Return the HSCode instance associated with the hs code specified.
|
|
151
|
+
|
|
152
|
+
**Example:**
|
|
153
|
+
```python
|
|
154
|
+
ust.get_product("10")
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## *Exploring countries*
|
|
158
|
+
|
|
159
|
+
### • `get_country_by_name(name)`
|
|
160
|
+
Look up a country by its full name (case-insensitive).
|
|
161
|
+
|
|
162
|
+
**Example:**
|
|
163
|
+
```python
|
|
164
|
+
ust.get_country_by_name("France")
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### • `get_country_by_code(code)`
|
|
168
|
+
Look up a country using its U.S. Census numeric code.
|
|
169
|
+
|
|
170
|
+
**Example:**
|
|
171
|
+
```python
|
|
172
|
+
ust.get_country_by_code("4279")
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### • `get_country_by_iso2(iso)`
|
|
176
|
+
Look up a country by ISO2 code.
|
|
177
|
+
|
|
178
|
+
**Example:**
|
|
179
|
+
```python
|
|
180
|
+
ust.get_country_by_iso2("FR")
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## 🧩 Notes
|
|
186
|
+
|
|
187
|
+
- All data retrieval functions return a **pandas DataFrame** unless otherwise noted.
|
|
188
|
+
- Column names are automatically standardized (see schema section).
|
|
189
|
+
- This library is still in <1.0.0 version and can change. Contributions are always welcome !
|
ustrade-0.4.0/README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# ustrade
|
|
2
|
+
|
|
3
|
+
> A lightweight and intuitive Python client for the **U.S. Census Bureau International Trade API**.
|
|
4
|
+
> Allows to fetch imports and exports between the US and commercial partners - based on Harmonized System codes (HS)
|
|
5
|
+
|
|
6
|
+
<p align="left">
|
|
7
|
+
<img src="https://img.shields.io/badge/python-3.10%2B-blue" />
|
|
8
|
+
<img src="https://img.shields.io/badge/status-active-success" />
|
|
9
|
+
<img src="https://img.shields.io/badge/license-MIT-green" />
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- **Simple API**: `ust.get_imports()`, `ust.get_exports()`
|
|
17
|
+
- Automatic normalization of country inputs:
|
|
18
|
+
- `"France"`
|
|
19
|
+
- `"FR"` (ISO2)
|
|
20
|
+
- `"4279"` (Census code)
|
|
21
|
+
- HS codes lookup + product descriptions
|
|
22
|
+
- Standardized DataFrame output with clean column names
|
|
23
|
+
- Uses a cached internal client for efficiency
|
|
24
|
+
- Zero configuration required
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
### PyPI Install
|
|
31
|
+
|
|
32
|
+
Install with pip :
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install ustrade
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Github
|
|
39
|
+
Clone this repository and install via pip in editable mode:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
git clone https://github.com/fantinsib/ustrade.git
|
|
43
|
+
cd ustrade
|
|
44
|
+
pip install -e .
|
|
45
|
+
```
|
|
46
|
+
## Quick Example
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
|
|
50
|
+
import ustrade as ust
|
|
51
|
+
|
|
52
|
+
# Example: Mexican imports of fruits and nuts between January 2010 and January 2025
|
|
53
|
+
df = ust.get_imports_on_period("Mexico", "08", "2010-01", "2025-01")
|
|
54
|
+
print(df)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Full API Reference
|
|
58
|
+
|
|
59
|
+
## *Fetching Data*
|
|
60
|
+
|
|
61
|
+
### • `get_imports(country, product, date)`
|
|
62
|
+
Fetch monthly import data for a given country and HS code.
|
|
63
|
+
|
|
64
|
+
**Parameters:**
|
|
65
|
+
- `country` — country name (`France`), ISO2 (`FR`), Census code (`4279`) or `Country` instance, or a list of the previous
|
|
66
|
+
- `product` — HS code as string (e.g. `2701`) or list of string
|
|
67
|
+
- `date` — `YYYY-MM` format (e.g. `2020-01`)
|
|
68
|
+
|
|
69
|
+
**Example:**
|
|
70
|
+
```python
|
|
71
|
+
ust.get_imports("FR", "10", "2025-01")
|
|
72
|
+
ust.get_imports(["France", "GB"], ["12", "13"], "2018-03")
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
### • `get_exports(country, product, date)`
|
|
78
|
+
Fetch monthly export data for a given country and HS code.
|
|
79
|
+
|
|
80
|
+
**Example:**
|
|
81
|
+
```python
|
|
82
|
+
ust.get_exports("GB", "73", "2019-01")
|
|
83
|
+
ust.get_exports(["France", "GB"], ["08", "09"], "2018-03")
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
### • `get_imports_on_period(country, product, start, end)`
|
|
89
|
+
Fetch a DataFrame containing monthly imports for a given country and HS code
|
|
90
|
+
|
|
91
|
+
**Parameters:**
|
|
92
|
+
- `country` — country name (`France`), ISO2 (`FR`), Census code (`4279`) or `Country` instance, or list of the previous
|
|
93
|
+
- `product` — HS code as string (e.g. `2701`) or list of string
|
|
94
|
+
- `start` and `end` — `YYYY-MM` format (e.g. `2020-01`)
|
|
95
|
+
|
|
96
|
+
**Example:**
|
|
97
|
+
```python
|
|
98
|
+
ust.get_imports_on_period("Mexico", "27", "2010-01", "2025-01")
|
|
99
|
+
ust.get_imports_on_period(["France", "DE", "GB"], ["09", "08", "07"], "2016-01", "2018-01")
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
### • `get_exports_on_period(country, product, start, end)`
|
|
105
|
+
Fetch a DataFrame containing monthly exports for a given country and HS code
|
|
106
|
+
|
|
107
|
+
**Example:**
|
|
108
|
+
```python
|
|
109
|
+
ust.get_exports_on_period("France", "10", "2020-01", "2023-01")
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## *Exploring Codes*
|
|
115
|
+
|
|
116
|
+
HS Codes follow a tree hierarchy.
|
|
117
|
+
|
|
118
|
+
### • `get_desc_from_code(hs)`
|
|
119
|
+
Return the description associated with an HS code.
|
|
120
|
+
|
|
121
|
+
**Example:**
|
|
122
|
+
```python
|
|
123
|
+
ust.get_desc_from_code("2701")
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### • `get_children_codes(code, return_names)`
|
|
127
|
+
Return a dictionnary of the codes and descriptions attached to the parent code if return_names is True, and a list of the code if return_names if False.
|
|
128
|
+
|
|
129
|
+
**Example:**
|
|
130
|
+
```python
|
|
131
|
+
ust.get_children_codes("10")
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
### • `get_product(hs)`
|
|
136
|
+
Return the HSCode instance associated with the hs code specified.
|
|
137
|
+
|
|
138
|
+
**Example:**
|
|
139
|
+
```python
|
|
140
|
+
ust.get_product("10")
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## *Exploring countries*
|
|
144
|
+
|
|
145
|
+
### • `get_country_by_name(name)`
|
|
146
|
+
Look up a country by its full name (case-insensitive).
|
|
147
|
+
|
|
148
|
+
**Example:**
|
|
149
|
+
```python
|
|
150
|
+
ust.get_country_by_name("France")
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### • `get_country_by_code(code)`
|
|
154
|
+
Look up a country using its U.S. Census numeric code.
|
|
155
|
+
|
|
156
|
+
**Example:**
|
|
157
|
+
```python
|
|
158
|
+
ust.get_country_by_code("4279")
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### • `get_country_by_iso2(iso)`
|
|
162
|
+
Look up a country by ISO2 code.
|
|
163
|
+
|
|
164
|
+
**Example:**
|
|
165
|
+
```python
|
|
166
|
+
ust.get_country_by_iso2("FR")
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## 🧩 Notes
|
|
172
|
+
|
|
173
|
+
- All data retrieval functions return a **pandas DataFrame** unless otherwise noted.
|
|
174
|
+
- Column names are automatically standardized (see schema section).
|
|
175
|
+
- This library is still in <1.0.0 version and can change. Contributions are always welcome !
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ustrade"
|
|
7
|
+
version = "0.4.0"
|
|
8
|
+
description = "Python client for the U.S. Census Bureau International Trade API"
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "Fantin Sibony" }
|
|
11
|
+
]
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
license = "MIT"
|
|
14
|
+
license-files = ["LICENSE.txt"]
|
|
15
|
+
requires-python = ">=3.10"
|
|
16
|
+
dependencies = [
|
|
17
|
+
"requests",
|
|
18
|
+
"pandas",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/fantinsib/ustrade"
|
|
23
|
+
|
|
24
|
+
[tool.setuptools]
|
|
25
|
+
package-dir = {"" = "."}
|
|
26
|
+
packages = ["ustrade"]
|
|
27
|
+
include-package-data = true
|
|
28
|
+
|
|
29
|
+
[tool.setuptools.package-data]
|
|
30
|
+
"ustrade" = ["data/*.csv"]
|
ustrade-0.4.0/setup.cfg
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
####### Test for API calls ######
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import ustrade as ut
|
|
5
|
+
from ustrade.client import CensusClient
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_build_client():
|
|
11
|
+
c= CensusClient()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_basic_request():
|
|
15
|
+
df = ut.get_exports("Mexico", "27", "2010-01")
|
|
16
|
+
|
|
17
|
+
assert len(df) == 1
|
|
18
|
+
assert df.loc[0, "country_name"] == "MEXICO"
|
|
19
|
+
assert df.loc[0, "product_code"] == "27"
|
|
20
|
+
assert df.loc[0, "export_value"] == 773377170.0
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_get_children_codes():
|
|
24
|
+
children = ut.get_children_codes("1001")
|
|
25
|
+
|
|
26
|
+
expected_keys = {"100111", "100119", "100191", "100199"}
|
|
27
|
+
assert set(children.keys()) == expected_keys
|
|
28
|
+
assert "durum wheat" in children["100111"]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
######## Tests for clients core methods ########
|
|
2
|
+
|
|
3
|
+
from urllib.parse import urlparse, parse_qs
|
|
4
|
+
from ustrade import CensusClient
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
import inspect
|
|
8
|
+
from ustrade.client import CensusClient
|
|
9
|
+
|
|
10
|
+
def test_debug():
|
|
11
|
+
print("module:", CensusClient.__module__)
|
|
12
|
+
print("file:", inspect.getfile(CensusClient))
|
|
13
|
+
print("_build_params in dir?", "_build_params" in dir(CensusClient))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_build_param():
|
|
18
|
+
|
|
19
|
+
c = CensusClient()
|
|
20
|
+
print(type(c))
|
|
21
|
+
print(hasattr(c, "_build_params"))
|
|
22
|
+
print(c.__class__)
|
|
23
|
+
|
|
24
|
+
url = c._build_params(["Mexico", "Canada"], ["08", "09"], "imports", start="2013-01", end= "2014-01")
|
|
25
|
+
|
|
26
|
+
parsed = urlparse(url)
|
|
27
|
+
qs = parse_qs(parsed.query)
|
|
28
|
+
|
|
29
|
+
# URL de base
|
|
30
|
+
assert parsed.scheme == "https"
|
|
31
|
+
assert parsed.netloc == "api.census.gov"
|
|
32
|
+
assert "/intltrade/imports/hs" in parsed.path
|
|
33
|
+
|
|
34
|
+
# Paramètres critiques
|
|
35
|
+
assert "get" in qs
|
|
36
|
+
assert "CTY_CODE" in qs
|
|
37
|
+
assert "I_COMMODITY" in qs
|
|
38
|
+
assert "time" in qs
|
|
39
|
+
|
|
40
|
+
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
####### TESTS FOR codes.py ######
|
|
2
|
+
|
|
3
|
+
import ustrade as ut
|
|
4
|
+
from ustrade.codes import HSCode, build_tree_from_codes
|
|
5
|
+
from ustrade import CensusClient
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_build_tree_simple():
|
|
9
|
+
codes = [
|
|
10
|
+
HSCode(section="I", hscode="10", description="Cereals", parent="", level=2, children=[]),
|
|
11
|
+
HSCode(section="I", hscode="1001", description="Wheat", parent="10", level=4, children=[]),
|
|
12
|
+
HSCode(section="I", hscode="100190", description="Other wheat", parent="1001", level=6, children=[]),
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
tree = build_tree_from_codes(codes)
|
|
16
|
+
|
|
17
|
+
assert set(tree.keys()) == {"10", "1001", "100190"}
|
|
18
|
+
assert "1001" in tree["10"].children
|
|
19
|
+
assert "100190" in tree["1001"].children
|
|
20
|
+
assert tree["100190"].children == []
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_build_tree_roots():
|
|
24
|
+
codes = [
|
|
25
|
+
HSCode(section="I", hscode="10", description="Cereals", parent="", level=2, children=[]),
|
|
26
|
+
HSCode(section="I", hscode="1001", description="Wheat", parent="10", level=4, children=[]),
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
tree = build_tree_from_codes(codes)
|
|
30
|
+
roots = [code for code, node in tree.items() if all(code not in n.children for n in tree.values())]
|
|
31
|
+
assert "10" in roots
|
|
32
|
+
|
|
33
|
+
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
from .countries import Country
|
|
3
|
+
from .client import CensusClient
|
|
4
|
+
from .codes import HSCode
|
|
5
|
+
from .errors import *
|
|
6
|
+
|
|
7
|
+
from importlib import metadata
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
__version__ = metadata.version("ustrade")
|
|
11
|
+
except metadata.PackageNotFoundError:
|
|
12
|
+
__version__ = "0.0.0"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
_default_client: CensusClient | None = None
|
|
16
|
+
|
|
17
|
+
def _get_default_client() -> CensusClient:
|
|
18
|
+
global _default_client
|
|
19
|
+
if _default_client is None:
|
|
20
|
+
_default_client = CensusClient()
|
|
21
|
+
return _default_client
|
|
22
|
+
|
|
23
|
+
def get_imports(country : str| Country | list[str | Country], product : str|list[str], date : str)-> pd.DataFrame:
|
|
24
|
+
"""
|
|
25
|
+
Returns the import value from the US to the specified country of the product for the month
|
|
26
|
+
Args:
|
|
27
|
+
country (str | Country | list[str | Country]) : can be the ISO2 code, the full name, the Census Bureau code for this country, or a Country object
|
|
28
|
+
product (str | list[str]) : HS code
|
|
29
|
+
date (str): the month, in format 'YYYY-MM'
|
|
30
|
+
|
|
31
|
+
Examples:
|
|
32
|
+
>>> ut.get_imports(["France", "GB"], ["12", "13"], "2018-03")
|
|
33
|
+
>>> ut.get_imports("GB", "12", "2018-03")
|
|
34
|
+
"""
|
|
35
|
+
return _get_default_client().get_imports(country = country, product= product, date = date)
|
|
36
|
+
|
|
37
|
+
def get_exports(country : str| Country | list[str | Country], product : str|list[str], date : str)-> pd.DataFrame:
|
|
38
|
+
"""
|
|
39
|
+
Returns the export value from the US to the specified country of the product for the month
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
country (str | Country | list[str | Country]) : can be the ISO2 code, the full name, the Census Bureau code for this country, or a Country object
|
|
43
|
+
product (str | list[str]) : HS code
|
|
44
|
+
date (str): the date, in format 'YYYY-MM'
|
|
45
|
+
Examples:
|
|
46
|
+
>>> ut.get_exports(["France", "GB"], ["08", "09"], "2018-03")
|
|
47
|
+
>>> ut.get_exports("GB", "08", "2018-03")
|
|
48
|
+
"""
|
|
49
|
+
return _get_default_client().get_exports(country = country, product= product, date = date)
|
|
50
|
+
|
|
51
|
+
def get_imports_on_period(country : str| Country | list[str | Country], product : str|list[str], start: str, end: str)->pd.DataFrame:
|
|
52
|
+
"""
|
|
53
|
+
Return the imports on the specified period
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
country (str | Country | list[str | Country]):
|
|
57
|
+
ISO2 code, full name, Census Bureau code, or a Country object.
|
|
58
|
+
product (str | list[str]):
|
|
59
|
+
HS code.
|
|
60
|
+
start (str):
|
|
61
|
+
Starting date in format "YYYY-MM".
|
|
62
|
+
end (str):
|
|
63
|
+
Ending date in format "YYYY-MM".
|
|
64
|
+
|
|
65
|
+
Examples:
|
|
66
|
+
>>> ut.get_imports_on_period(["France", "DE", "GB"], ["09", "08", "07"], "2016-01", "2018-01")
|
|
67
|
+
>>> from ustrade import CensusClient
|
|
68
|
+
>>> c = CensusClient(timeout=120)
|
|
69
|
+
>>> c.get_imports_on_period(["France", "DE", "GB"], ["08", "07"], "2016-01", "2018-01")
|
|
70
|
+
|
|
71
|
+
Notes:
|
|
72
|
+
- Queries can take time to load.
|
|
73
|
+
- Consider increasing `timeout`.
|
|
74
|
+
- Data is only available from 2010-01.
|
|
75
|
+
"""
|
|
76
|
+
return _get_default_client().get_imports_on_period(country, product, start, end)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def get_exports_on_period(country : str| Country | list[str | Country], product : str|list[str], start: str, end: str)->pd.DataFrame:
|
|
80
|
+
"""
|
|
81
|
+
Return the exports on the specified period.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
country (str | Country | list[str | Country]):
|
|
85
|
+
ISO2 code, full name, Census Bureau code, or a Country object.
|
|
86
|
+
product (str | list[str]):
|
|
87
|
+
HS code(s).
|
|
88
|
+
start (str):
|
|
89
|
+
Start date in format "YYYY-MM".
|
|
90
|
+
end (str):
|
|
91
|
+
End date in format "YYYY-MM".
|
|
92
|
+
|
|
93
|
+
Examples:
|
|
94
|
+
>>> ut.get_exports_on_period(["France", "DE", "GB"], ["09", "08", "07"], "2016-01", "2018-01")
|
|
95
|
+
>>> from ustrade import CensusClient
|
|
96
|
+
>>> c = CensusClient(timeout=120)
|
|
97
|
+
>>> c.get_exports_on_period(["France", "DE", "GB"], ["08", "07"], "2016-01", "2018-01")
|
|
98
|
+
|
|
99
|
+
Notes:
|
|
100
|
+
- Queries can take time to load.
|
|
101
|
+
- Consider increasing `timeout`.
|
|
102
|
+
- Data is only available from 2010-01.
|
|
103
|
+
"""
|
|
104
|
+
return _get_default_client().get_exports_on_period(country, product, start, end)
|
|
105
|
+
|
|
106
|
+
def get_country_by_name(country: str)-> Country:
|
|
107
|
+
"""
|
|
108
|
+
Search a country with its name
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
country (str) : the full name of the country (ex: 'France')
|
|
112
|
+
"""
|
|
113
|
+
return _get_default_client().get_country_by_name(country)
|
|
114
|
+
|
|
115
|
+
def get_country_by_code(cty_code: str):
|
|
116
|
+
"""
|
|
117
|
+
Search a country with its code
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
cty_code (str) : the Census Bureau code of the country (ex: '4120')
|
|
121
|
+
"""
|
|
122
|
+
return _get_default_client().get_country_by_code(cty_code)
|
|
123
|
+
|
|
124
|
+
def get_country_by_iso2(iso2: str):
|
|
125
|
+
"""
|
|
126
|
+
Search a country with its ISO 2 ID
|
|
127
|
+
|
|
128
|
+
Args:
|
|
129
|
+
iso2 (str) : the ISO2 code of the country (ex: 'IT')
|
|
130
|
+
"""
|
|
131
|
+
return _get_default_client().get_country_by_iso2(iso2)
|
|
132
|
+
|
|
133
|
+
def get_desc_from_code(hs: str):
|
|
134
|
+
"""
|
|
135
|
+
Returns the description associated with the HS code specified
|
|
136
|
+
|
|
137
|
+
Args:
|
|
138
|
+
hs (str): the HS code (ex: '73')
|
|
139
|
+
"""
|
|
140
|
+
return _get_default_client().get_desc_from_code(hs)
|
|
141
|
+
|
|
142
|
+
def get_children_codes(code: str | HSCode, return_names = True)-> dict | list[str]:
|
|
143
|
+
"""
|
|
144
|
+
Returns a dict of the codes and their desc directly attached to code in the hierarchy
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
code (str | HSCode): either the code as a string or the HSCode object
|
|
148
|
+
return_names (bool): returns a dict with the code and the description if true, a list of the codes if false
|
|
149
|
+
"""
|
|
150
|
+
return _get_default_client().get_children_codes(code, return_names)
|
|
151
|
+
|
|
152
|
+
def get_product(hs: str) -> HSCode:
|
|
153
|
+
"""
|
|
154
|
+
Returns all the informations on a specified HS code through a HSCode object
|
|
155
|
+
|
|
156
|
+
Args:
|
|
157
|
+
hs (str): the HS code (ex: '1806')
|
|
158
|
+
"""
|
|
159
|
+
return _get_default_client().get_product(hs)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
__all__ = [
|
|
163
|
+
"CensusClient",
|
|
164
|
+
"Country",
|
|
165
|
+
"get_imports",
|
|
166
|
+
"get_exports",
|
|
167
|
+
"get_imports_on_period",
|
|
168
|
+
"get_exports_on_period",
|
|
169
|
+
"get_country_by_name",
|
|
170
|
+
"get_country_by_code",
|
|
171
|
+
"get_country_by_iso2",
|
|
172
|
+
"get_desc_from_code",
|
|
173
|
+
"get_children_codes",
|
|
174
|
+
"get_product"
|
|
175
|
+
]
|