Skip to content

Commit c87c689

Browse files
committed
Format code and add makefile
1 parent 5621435 commit c87c689

37 files changed

+1758
-1057
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[flake8]
2-
ignore = D203, E126
2+
ignore = D203, E126, W503, E203
33
exclude =
44
.git,
55
__pycache__,

Makefile

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.PHONY: clean clean-test clean-pyc clean-build format test help docs
2+
.DEFAULT_GOAL := help
3+
4+
define BROWSER_PYSCRIPT
5+
import os, webbrowser, sys
6+
7+
from urllib.request import pathname2url
8+
9+
webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])))
10+
endef
11+
export BROWSER_PYSCRIPT
12+
13+
define PRINT_HELP_PYSCRIPT
14+
import re, sys
15+
16+
for line in sys.stdin:
17+
match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line)
18+
if match:
19+
target, help = match.groups()
20+
print("%-20s %s" % (target, help))
21+
endef
22+
export PRINT_HELP_PYSCRIPT
23+
24+
BROWSER := poetry run python -c "$$BROWSER_PYSCRIPT"
25+
26+
help:
27+
@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)
28+
29+
cov: ## check code coverage
30+
poetry run coverage run --source pycardano -m pytest
31+
poetry run coverage report -m
32+
33+
cov-html: cov ## check code coverage and generate an html report
34+
poetry run coverage html -d cov_html
35+
$(BROWSER) cov_html/index.html
36+
37+
38+
clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts
39+
40+
clean-build: ## remove build artifacts
41+
rm -fr build/
42+
rm -fr dist/
43+
rm -fr .eggs/
44+
find . -name '*.egg-info' -exec rm -fr {} +
45+
find . -name '*.egg' -exec rm -f {} +
46+
47+
clean-pyc: ## remove Python file artifacts
48+
find . -name '*.pyc' -exec rm -f {} +
49+
find . -name '*.pyo' -exec rm -f {} +
50+
find . -name '*~' -exec rm -f {} +
51+
find . -name '__pycache__' -exec rm -fr {} +
52+
53+
clean-test: ## remove test and coverage artifacts
54+
rm -f .coverage
55+
rm -fr cov_html/
56+
rm -fr .pytest_cache
57+
58+
test: ## runs tests
59+
poetry run pytest
60+
61+
qa: ## runs static analysis with flake8
62+
poetry run flake8 pycardano
63+
64+
format: ## runs code style and formatter
65+
poetry run isort .
66+
poetry run black .
67+
68+
docs: ## build the documentation
69+
poetry run sphinx-build docs/source docs/build/html
70+
$(BROWSER) docs/build/html/index.html
71+
72+
release: clean qa test format ## build dist version and release to pypi
73+
poetry build
74+
poetry publish

README.md

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -178,48 +178,42 @@ Change directory into the repo, install all dependencies using poetry, and you a
178178

179179
`cd pycardano && poetry install`
180180

181-
#### Test
182-
183-
PyCardano uses [pytest](https://docs.pytest.org/en/6.2.x/) for unit testing.
184-
185181
When testing or running any program, it is recommended to enter
186182
a [poetry shell](https://python-poetry.org/docs/cli/#shell) in which all python dependencies are automatically
187183
configured: `poetry shell`.
188184

189-
Run all tests:
190-
`pytest`
191185

192-
Run all tests including doctests:
193-
`pytest --doctest-modules --ignore=examples`
186+
#### Test
187+
188+
PyCardano uses [pytest](https://docs.pytest.org/en/6.2.x/) for unit testing.
189+
190+
Run all tests:
191+
`make test`
194192

195193
Run all tests in a specific test file:
196-
`pytest test/pycardano/test_transaction.py`
194+
`poetry run pytest test/pycardano/test_transaction.py`
197195

198196
Run a specific test function:
199-
`pytest -k "test_transaction_body"`
197+
`poetry run pytest -k "test_transaction_body"`
200198

201199
Run a specific test function in a test file:
202-
`pytest test/pycardano/test_transaction.py -k "test_transaction_body"`
200+
`poetry run pytest test/pycardano/test_transaction.py -k "test_transaction_body"`
203201

204202
#### Test coverage
205203

206-
Test coverage could be checked by running:
207-
`pytest --cov=pycardano --cov-config=.coveragerc`
208-
209-
A coverage report visualized in html could be generated by running:
210-
`pytest --cov=pycardano --cov-config=.coveragerc --cov-report html:cov_html`
211-
212-
Run all possible tests (including doc tests) and generate html report and terminal report:
213-
`pytest --doctest-modules --ignore=examples --cov=pycardano --cov-config=.coveragerc --cov-report html:cov_html --cov-report term`
204+
We use [Coverage](https://coverage.readthedocs.io/en/latest/) to calculate the test coverage.
214205

215-
The generated report will be in folder `./cov_html`.
206+
Test coverage could be generated by: `make cov`
207+
An html report could be generated and opened in browser by: `make cov-html`
216208

217209
### Style guidelines
218210

219211
The package uses
220212
[Google style](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) docstring.
221213

222-
The code style could be checked by [flake8](https://flake8.pycqa.org/en/latest/): `flake8 pycardano`
214+
Code could be formatted with command: `make format`
215+
216+
The code style could be checked by [flake8](https://flake8.pycqa.org/en/latest/): `make qa`
223217

224218
### Docs generation
225219

@@ -228,13 +222,9 @@ We use [sphinx](https://www.sphinx-doc.org/en/master/) with
228222
[Read the Docs theme](https://sphinx-rtd-theme.readthedocs.io/en/stable/) to generate the
229223
html pages.
230224

231-
Build htmls:
232-
233-
`cd docs && make html`
234-
235-
Go to the main page:
225+
Build docs and open the docs in browser:
236226

237-
`open build/html/index.html`
227+
`make docs`
238228

239229

240230

docs/source/conf.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,43 @@
1212
#
1313
import os
1414
import sys
15-
sys.path.insert(0, os.path.abspath('../'))
15+
16+
sys.path.insert(0, os.path.abspath("../"))
1617
# For readthedocs whose working dir is at docs/source
17-
sys.path.insert(0, os.path.abspath('../../'))
18+
sys.path.insert(0, os.path.abspath("../../"))
1819

1920

2021
# -- Project information -----------------------------------------------------
2122

22-
project = 'PyCardano'
23-
copyright = '2021, JerryC'
24-
author = 'JerryC'
23+
project = "PyCardano"
24+
copyright = "2021, JerryC"
25+
author = "JerryC"
2526

2627

2728
# -- General configuration ---------------------------------------------------
2829

2930
# Add any Sphinx extension module names here, as strings. They can be
3031
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3132
# ones.
32-
extensions = [
33-
'sphinx.ext.autodoc',
34-
'sphinx.ext.napoleon',
35-
'sphinx_rtd_theme'
36-
]
33+
extensions = ["sphinx.ext.autodoc", "sphinx.ext.napoleon", "sphinx_rtd_theme"]
3734

38-
master_doc = 'index'
35+
master_doc = "index"
3936

4037
napoleon_google_docstring = True
4138
napoleon_include_init_with_doc = False
4239

4340
autodoc_member_order = "bysource"
4441

4542
# Add any paths that contain templates here, relative to this directory.
46-
templates_path = ['_templates']
43+
templates_path = ["_templates"]
4744

4845
# List of patterns, relative to source directory, that match files and
4946
# directories to ignore when looking for source files.
5047
# This pattern also affects html_static_path and html_extra_path.
5148
exclude_patterns = []
5249

53-
pygments_style = 'sphinx'
54-
highlight_language = 'python'
50+
pygments_style = "sphinx"
51+
highlight_language = "python"
5552
todo_include_todos = False
5653

5754

@@ -60,9 +57,9 @@
6057
# The theme to use for HTML and HTML Help pages. See the documentation for
6158
# a list of builtin themes.
6259
#
63-
html_theme = 'sphinx_rtd_theme'
60+
html_theme = "sphinx_rtd_theme"
6461

6562
# Add any paths that contain custom static files (such as style sheets) here,
6663
# relative to this directory. They are copied after the builtin static files,
6764
# so a file named "default.css" will overwrite the builtin "default.css".
68-
html_static_path = ['_static']
65+
html_static_path = ["_static"]

examples/delegator_loyalty_rewards.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
https://cardanoscan.io/transaction/c1b58dd4f2f4ee8656cc7962eefa8552877c4aa23d0699c02b885363d592a961
44
"""
55

6-
from blockfrost import BlockFrostApi, ApiUrls
6+
from blockfrost import ApiUrls, BlockFrostApi
77

88
from pycardano import *
99

@@ -36,7 +36,9 @@
3636

3737
# Derive an address from payment verification key
3838
input_address = Address(pvk.hash(), network=network)
39-
print(f"ADA will be distributed from this address: {input_address}, make sure there are enough ADA in it.")
39+
print(
40+
f"ADA will be distributed from this address: {input_address}, make sure there are enough ADA in it."
41+
)
4042

4143
# Create a BlockFrost chain context
4244
context = BlockFrostChainContext(BLOCK_FROST_PROJECT_ID, network)
@@ -73,16 +75,19 @@
7375

7476
# ======= Business logic ends ========
7577

76-
auxiliary_data = AuxiliaryData(AlonzoMetadata(metadata=Metadata({
77-
674: {
78-
"Title": f"Loyalty rewards for stake pool [{POOL_TICKER}] delegators",
79-
"Rules": {
80-
"100K+": "50 ADA",
81-
"10K+": "10 ADA"
82-
},
83-
"Notes": "Created with https://github.com/cffls/pycardano."
84-
}
85-
})))
78+
auxiliary_data = AuxiliaryData(
79+
AlonzoMetadata(
80+
metadata=Metadata(
81+
{
82+
674: {
83+
"Title": f"Loyalty rewards for stake pool [{POOL_TICKER}] delegators",
84+
"Rules": {"100K+": "50 ADA", "10K+": "10 ADA"},
85+
"Notes": "Created with https://github.com/cffls/pycardano.",
86+
}
87+
}
88+
)
89+
)
90+
)
8691

8792
builder.auxiliary_data = auxiliary_data
8893

@@ -95,8 +100,11 @@
95100
vk_witnesses = [VerificationKeyWitness(pvk, signature)]
96101

97102
# Create final signed transaction
98-
signed_tx = Transaction(tx_body, TransactionWitnessSet(vkey_witnesses=vk_witnesses),
99-
auxiliary_data=auxiliary_data)
103+
signed_tx = Transaction(
104+
tx_body,
105+
TransactionWitnessSet(vkey_witnesses=vk_witnesses),
106+
auxiliary_data=auxiliary_data,
107+
)
100108

101109
# Submit signed transaction to the network
102110
print(signed_tx)

examples/native_token.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
BLOCK_FROST_PROJECT_ID = "your_project_id"
1515
NETWORK = Network.TESTNET
1616

17-
chain_context = BlockFrostChainContext(project_id=BLOCK_FROST_PROJECT_ID, network=NETWORK)
17+
chain_context = BlockFrostChainContext(
18+
project_id=BLOCK_FROST_PROJECT_ID, network=NETWORK
19+
)
1820

1921
"""Preparation"""
2022
# Define the root directory where images and keys will be stored.
@@ -93,15 +95,14 @@ def load_or_create_key_pair(base_dir, base_name):
9395
"""Define NFT (Alternative)"""
9496
# The nft definition above is somewhat verbose.
9597
# We can also directly create native assets from python primitives.
96-
my_nft_alternative = MultiAsset.from_primitive({
97-
policy_id.payload: # Use policy ID created from above. We can't use policy_id here because policy_id's type
98-
{ # is ScriptHash, which is not a primitive type. Instead, we use policy_id.payload (bytes)
99-
b"MY_NFT_1": # Name of our NFT1
100-
1, # Quantity of this NFT
101-
b"MY_NFT_2": # Name of our NFT2
102-
1 # Quantity of this NFT
98+
my_nft_alternative = MultiAsset.from_primitive(
99+
{
100+
policy_id.payload: { # Use policy ID created from above. We can't use policy_id here because policy_id's type # is ScriptHash, which is not a primitive type. Instead, we use policy_id.payload (bytes)
101+
b"MY_NFT_1": 1, # Name of our NFT1 # Quantity of this NFT
102+
b"MY_NFT_2": 1, # Name of our NFT2 # Quantity of this NFT
103103
}
104-
})
104+
}
105+
)
105106

106107
# my_nft and my_nft_alternative are equivalent
107108
assert my_nft == my_nft_alternative
@@ -116,14 +117,14 @@ def load_or_create_key_pair(base_dir, base_name):
116117
"description": "This is my first NFT thanks to PyCardano",
117118
"name": "PyCardano NFT example token 1",
118119
"id": 1,
119-
"image": "ipfs://QmRhTTbUrPYEw3mJGGhQqQST9k86v1DPBiTTWJGKDJsVFw"
120+
"image": "ipfs://QmRhTTbUrPYEw3mJGGhQqQST9k86v1DPBiTTWJGKDJsVFw",
120121
},
121122
"MY_NFT_2": {
122123
"description": "This is my second NFT thanks to PyCardano",
123124
"name": "PyCardano NFT example token 2",
124125
"id": 2,
125-
"image": "ipfs://QmRhTTbUrPYEw3mJGGhQqQST9k86v1DPBiTTWJGKDJsVFw"
126-
}
126+
"image": "ipfs://QmRhTTbUrPYEw3mJGGhQqQST9k86v1DPBiTTWJGKDJsVFw",
127+
},
127128
}
128129
}
129130
}
@@ -170,19 +171,22 @@ def load_or_create_key_pair(base_dir, base_name):
170171
# Add verification keys and their signatures to the witness set
171172
vk_witnesses = [
172173
VerificationKeyWitness(payment_vkey, payment_signature),
173-
VerificationKeyWitness(policy_vkey, policy_signature)
174+
VerificationKeyWitness(policy_vkey, policy_signature),
174175
]
175176

176177
# We also need to add the policy script to witness set when we are minting new tokens
177178
native_script_witnesses = [policy]
178179

179180
# Create final signed transaction
180-
signed_tx = Transaction(tx_body,
181-
TransactionWitnessSet(vkey_witnesses=vk_witnesses,
182-
native_scripts=native_scripts), # We also need to add the policy script
183-
# to witness set when we are minting
184-
# new tokens.
185-
auxiliary_data=auxiliary_data)
181+
signed_tx = Transaction(
182+
tx_body,
183+
TransactionWitnessSet(
184+
vkey_witnesses=vk_witnesses, native_scripts=native_scripts
185+
), # We also need to add the policy script
186+
# to witness set when we are minting
187+
# new tokens.
188+
auxiliary_data=auxiliary_data,
189+
)
186190

187191
print("############### Transaction created ###############")
188192
print(signed_tx)

0 commit comments

Comments
 (0)