Skip to content

Commit 66d20df

Browse files
Feature/add documentation (#2)
* * WIP * * check sourcery * 'Refactored by Sourcery' (#1) Co-authored-by: Sourcery AI <> * * sourcery is cool! * * Add documentation * * Increment version * * Fix linters * Add autohooks --------- Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
1 parent aff38b6 commit 66d20df

11 files changed

Lines changed: 357 additions & 23 deletions

File tree

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ linters: ruff pylint mypy
3636
# Local dev
3737
ipython:
3838
ipython --InteractiveShellApp.exec_lines "['%autoreload 2', 'from restcountries_cli.factories import *', 'from restcountries_cli.cli import *']" --InteractiveShellApp.extensions autoreload
39+
autohooks:
40+
poetry run autohooks activate --mode poetry
3941

4042
# Testing
4143
test:

README.rst

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
============
1+
========================
22
Restcountries_cli Readme
3-
============
3+
========================
44

55

66
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
77
:target: https://github.com/psf/black
8+
:alt: Black
9+
10+
.. image:: https://img.shields.io/badge/types-Mypy-202235.svg?logo=python
11+
:target: https://github.com/python/mypy
12+
:alt: Mypy
13+
14+
.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json
15+
:target: https://github.com/astral-sh/ruff
16+
:alt: Ruff
17+
18+
.. image:: https://img.shields.io/badge/Sourcery-enabled-edb641
19+
:target: https://sourcery.ai
20+
:alt: Sourcery
821

922
.. image:: https://github.com/martingaldeca/restcountries_cli/actions/workflows/linters.yml/badge.svg?event=push
1023
:target: https://github.com/martingaldeca/restcountries_cli/actions/workflows/linters.yml
@@ -14,7 +27,7 @@ Restcountries_cli Readme
1427
:target: https://github.com/martingaldeca/restcountries_cli/actions
1528
:alt: Tests
1629

17-
.. image:: https://coveralls.io/github/martingaldeca/restcountries_cli/badge.svg?branch=main
30+
.. image:: https://coveralls.io/repos/github/martingaldeca/restcountries_cli/badge.svg?branch=main
1831
:target: https://coveralls.io/github/martingaldeca/restcountries_cli?branch=main
1932
:alt: Coverage
2033

@@ -26,4 +39,18 @@ Restcountries_cli Readme
2639
:target: https://restcountries-cli.readthedocs.io/en/latest/?badge=latest
2740
:alt: Documentation Status
2841

29-
TODO
42+
.. image:: https://img.shields.io/pypi/pyversions/restcountries-cli?logo=python
43+
:target: https://img.shields.io/pypi/pyversions/restcountries-cli?logo=python
44+
:alt: Python version
45+
46+
.. image:: https://img.shields.io/badge/license-MIT-202235.svg?logo=python
47+
:target: https://spdx.org/licenses/
48+
:alt: License
49+
50+
Restcountries cli is a python client to encapsulate the calls to restcountries API.
51+
52+
The main difference with other restcountries clients is that the calls are cached.
53+
54+
Also all the countries are returned as pydantic models, so the data navigation is easier.
55+
56+
Yo can check the code in `GitHub <https://github.com/martingaldeca/restcountries_cli>`_ or follow the `official documentation <https://restcountries-cli.readthedocs.io/en/latest/>`_.

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pylint: skip-file
12
import sys
23
from os.path import abspath, dirname
34

@@ -19,6 +20,7 @@
1920
"sphinx.ext.napoleon",
2021
"sphinx.ext.viewcode",
2122
"sphinx.ext.extlinks",
23+
"sphinx_copybutton",
2224
"sphinx.ext.autosectionlabel",
2325
]
2426
autosectionlabel_prefix_document = True

docs/index.rst

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,81 @@
1-
Restcountries Cli
2-
===========
1+
===============================
2+
Restcountries Cli Documentation
3+
===============================
4+
5+
6+
7+
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
8+
:target: https://github.com/psf/black
9+
:alt: Black
10+
11+
.. image:: https://img.shields.io/badge/types-Mypy-202235.svg?logo=python
12+
:target: https://github.com/python/mypy
13+
:alt: Mypy
14+
15+
.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json
16+
:target: https://github.com/astral-sh/ruff
17+
:alt: Ruff
18+
19+
.. image:: https://img.shields.io/badge/Sourcery-enabled-edb641
20+
:target: https://sourcery.ai
21+
:alt: Sourcery
22+
23+
.. image:: https://github.com/martingaldeca/restcountries_cli/actions/workflows/linters.yml/badge.svg?event=push
24+
:target: https://github.com/martingaldeca/restcountries_cli/actions/workflows/linters.yml
25+
:alt: Linters
26+
27+
.. image:: https://github.com/martingaldeca/restcountries_cli/actions/workflows/tests.yml/badge.svg?event=push
28+
:target: https://github.com/martingaldeca/restcountries_cli/actions
29+
:alt: Tests
30+
31+
.. image:: https://coveralls.io/repos/github/martingaldeca/restcountries_cli/badge.svg?branch=main
32+
:target: https://coveralls.io/github/martingaldeca/restcountries_cli?branch=main
33+
:alt: Coverage
334

4-
Description
35+
.. image:: https://img.shields.io/pypi/v/restcountries-cli.svg
36+
:target: https://pypi.org/project/restcountries-cli/
37+
:alt: PyPI
538

6-
Installation
7-
------------
39+
.. image:: https://readthedocs.org/projects/restcountries-cli/badge/?version=latest
40+
:target: https://restcountries-cli.readthedocs.io/en/latest/?badge=latest
41+
:alt: Documentation Status
842

9-
.. code-block:: bash
43+
.. image:: https://img.shields.io/pypi/pyversions/restcountries-cli?logo=python
44+
:target: https://img.shields.io/pypi/pyversions/restcountries-cli?logo=python
45+
:alt: Python version
1046

11-
pip install restcountries_cli
47+
.. image:: https://img.shields.io/badge/license-MIT-202235.svg?logo=python
48+
:target: https://spdx.org/licenses/
49+
:alt: License
1250

51+
Restcountries cli is a python client to encapsulate the calls to restcountries API.
52+
53+
The main difference with other restcountries clients is that the calls are cached.
54+
55+
Also all the countries are returned as pydantic models, so the data navigation is easier.
56+
57+
Yo can check the code in `GitHub <https://github.com/martingaldeca/restcountries_cli>`_ or follow the `official documentation <https://restcountries-cli.readthedocs.io/en/latest/>`_.
58+
59+
First Steps
60+
===========
61+
62+
So, you want a new assistant that is really "real". You want to speak fluently
63+
with him/her, and you want them to understand you better. This is your package.
1364

1465
.. toctree::
15-
:titlesonly:
16-
:caption: Documentation
17-
:hidden:
66+
:caption: First Steps
67+
:hidden:
68+
:maxdepth: 4
69+
70+
intro/overview
71+
intro/installation
72+
intro/tutorial
73+
74+
:doc:`intro/overview`
75+
Introduction to Restcountries cli
76+
77+
:doc:`intro/installation`
78+
Installation guide
1879

19-
TODO
80+
:doc:`intro/tutorial`
81+
Start with restcountries cli!

docs/intro/installation.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.. _intro-installation:
2+
3+
==================
4+
Installation Guide
5+
==================
6+
7+
.. _intro-installation-python_versions:
8+
9+
Supported Python Versions
10+
=========================
11+
The Restcountries CLI requires Python ``3.9+``. The idea behind it is to be
12+
continuously updated, so it will support the latest versions of Python.
13+
14+
.. _intro-installation-requirements:
15+
16+
Installing Restcountries-cli
17+
============================
18+
You can install restcountries-cli and its dependencies from ``PyPI`` using
19+
pip:
20+
21+
.. code-block:: shell
22+
23+
pip install restcountries-cli
24+
25+
That's all! You can also add it to your project using poetry or the package
26+
manager you prefer.

docs/intro/overview.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.. _intro-overview:
2+
3+
=========================
4+
Countries for All
5+
=========================
6+
7+
Rest countries are meant to be used as part of larger projects to get the data
8+
of the countries.
9+
10+
If you ever had a problem related to having in your database the data of the
11+
countries, you know that the first thing is getting all the data of the countries.
12+
13+
Nowadays you can do this directly using some other libraries like
14+
`django-countries <https://pypi.org/project/django-countries/)>`_,
15+
`pycountry <https://pypi.org/project/pycountry/>`_,
16+
`python-restcountries <https://pypi.org/project/python-restcountries/>`_, or even
17+
downloading a CSV or JSON from somewhere and parsing it yourself. But sometimes
18+
there is not enough data, or even worse, it is not maintained.
19+
20+
The idea behind this package is to have a solution that allows you to get this
21+
needed data in some projects without too many problems, and you can make sure
22+
that the data is maintained as it is based on the API of
23+
`restcountries <https://restcountries.com/>`_.

docs/intro/tutorial.rst

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
.. _intro-tutorial:
2+
3+
.. role:: red
4+
5+
.. raw:: html
6+
7+
<style> .red {color:red} </style>
8+
9+
==========================
10+
Restcountries cli Tutorial
11+
==========================
12+
13+
Once you have install the package you can instantiate a client and get the data for the
14+
countries.
15+
16+
.. code-block:: python
17+
18+
from restcountries_cli import RestCountriesCli
19+
20+
client = RestCountriesCli()
21+
countries = client.all()
22+
23+
If you want for example information about a single country you can use the country_name method.
24+
25+
.. code-block:: python
26+
27+
from restcountries_cli import RestCountriesCli
28+
29+
client = RestCountriesCli()
30+
country = client.all() # This will call the API
31+
country = client.all() # This will not call the API
32+
33+
:red:`TODO` More methods will be add in the future to query the API with other values.
34+
35+
Cached API calls
36+
----------------
37+
38+
All the calls to the restcountries API by default are cached in a sqlite database. However this cache can be configured.
39+
40+
To start you can avoid this API cache and call each time you run the methods of the cli by setting the parameter ``cached_session`` to False when instantiating the client:
41+
42+
.. code-block:: python
43+
44+
from restcountries_cli import RestCountriesCli
45+
46+
client = RestCountriesCli(cached_session=False)
47+
country = client.all() # This will call the API
48+
country = client.all() # This will also call the API
49+
50+
Also you can specify the `sqlite` file name if you want by changing the ``cache_name`` parameter. By default it will create a file which name will be a random uuid.
51+
52+
But this means that I have to clean the file each time I run RestCountriesCli? NO! Once you destroy the client object it will automatically clean the cache by removing that file. So you can use this parameter, but it was designed to be used for testing purposes.
53+
54+
.. code-block:: python
55+
56+
from restcountries_cli import RestCountriesCli
57+
58+
client = RestCountriesCli(cache_name="test")
59+
country = client.country_name("spain") # This will add an entry in the test.sqlite file in the project root folder
60+
61+
You can also force to restart the cache by calling the method ``refresh_cached_session``.
62+
63+
.. code-block:: python
64+
65+
from restcountries_cli import RestCountriesCli
66+
67+
client = RestCountriesCli()
68+
country = client.all() # This will call the API
69+
client.refresh_cached_session()
70+
country = client.all() # This will call the API again
71+
72+
Data saved in the cli
73+
---------------------
74+
75+
Ok so imagine that you get the data of all the countries by calling the ``all`` method of the client. Now you want to know the info of a concrete country. You can search it in the list returned by the ``all`` method oooooor, you can just get the country by it's name for example.
76+
77+
This second call will not call the API, even if your session is not cached. This is because internally all the countries given by the API are saved in a list inside the cli called ``countries``. So, if the country you are looking for is inside this list, the call is not needed.
78+
79+
.. code-block:: python
80+
81+
from restcountries_cli import RestCountriesCli
82+
83+
client = RestCountriesCli()
84+
country = client.all() # This will call the API and save the countries parsed in client.countries
85+
country = client.country_name("spain") # This will not call the API again as this country is inside client.countries from the previous call
86+
87+
This will even work for single countries
88+
89+
.. code-block:: python
90+
91+
from restcountries_cli import RestCountriesCli
92+
93+
client = RestCountriesCli()
94+
country = client.country_name("spain") # This will call the API and save the country in the client countries list
95+
country = client.country_name("spain") # This will not call the API again as this country is inside client.countries from the previous call
96+
97+
But you can force the query, I mean, this is not usual (as it was designed for testing purposes), but you can:
98+
99+
.. code-block:: python
100+
101+
from restcountries_cli import RestCountriesCli
102+
103+
client = RestCountriesCli(cached_session=False)
104+
country = client.country_name("spain") # This will call the API
105+
country = client.country_name("spain", force_query=True) # This will also call the API, as the cli has not cache and you forced to call again the endpoint

0 commit comments

Comments
 (0)