Skip to content

Commit fe1edc9

Browse files
authored
Merge pull request #19 from rihp/main
readme improvements
2 parents 6241372 + c952dcf commit fe1edc9

3 files changed

Lines changed: 201 additions & 35 deletions

File tree

README.md

Lines changed: 201 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,168 @@
1-
# Polywrap python client
1+
![Public Release Announcement](https://user-images.githubusercontent.com/5522128/177473887-2689cf25-7937-4620-8ca5-17620729a65d.png)
22

3-
[Polywrap](https://polywrap.io/#/) is a developer tool that enables easy integration of Web3 protocols into any application. It makes it possible for applications on any platform, written in any language, to read and write data to Web3 protocols.
3+
# Polywrap Python Client
44

5-
## Setup for building and testing
6-
- Requirement: Python ^3.10, Poetry ^1.1.14
7-
- If you are using a linux system or WSL, which comes with Python3.8, then you will need to upgrade Python3.8 to Python3.10 and also fix the pip and distutil as upgrading to Python3.10 will break them. You may follow [this guide](https://cloudbytes.dev/snippets/upgrade-python-to-latest-version-on-ubuntu-linux) to upgrade.
8-
- To install poetry follow [this guide](https://python-poetry.org/docs/#installation). If you are on macos then you can install poetry simply with the following homebrew command
5+
[Polywrap](https://polywrap.io) is a developer tool that enables easy integration of Web3 protocols into any application. It makes it possible for applications on any platform, written in any language, to read and write data to Web3 protocols.
6+
7+
8+
# Working Features
9+
10+
This MVP Python client enables the execution of **[WebAssembly](https://en.wikipedia.org/wiki/WebAssembly) Polywrappers** *(or just "wrappers")* on a python environment, regardless of what language this wrapper was built in.
11+
12+
The client is built following the functionality of the [JavaScript Polywrap Client](https://github.com/polywrap/toolchain), which is currently more robust and battle tested, as it has additional capabilities than this MVP. In the future, the Polywrap DAO will continue improving this Python capabilities to reach feature parity with the JS stack, while building in parallel clients for other languages like Go and Rust.
13+
14+
[Here](https://github.com/polywrap/client-test-harness) you can see which features have been implemented on each language, and make the decision of which one to use for your project.
15+
16+
17+
# Getting Started:
18+
19+
Have questions or want to get involved? Join our community [Discord](https://discord.polywrap.io) or [open an issue](https://github.com/polywrap/toolchain/issues) on Github.
20+
21+
For detailed information about Polywrap and the WRAP standard, visit our [developer documentation](https://docs.polywrap.io/).
22+
23+
## Pre-requisites
24+
25+
### Clone the repo.
926
```
10-
brew install poetry
27+
git clone https://github.com/polywrap/python-client
1128
```
12-
- Clone the repo.
29+
30+
### `python ˆ3.10`
31+
- Make sure you're running the correct version of python by running:
1332
```
14-
git clone https://github.com/polywrap/python-client
33+
python3 --version
1534
```
16-
- We will be using [Poetry](https://python-poetry.org) for building and testing our packages.
35+
> If you are using a Linux system or WSL, which comes with Python3.8, then you will need to upgrade from Python3.8 to Python3.10 and also fix the `pip` and `distutil` as upgrading to Python3.10 will break them. You may follow [this guide](https://cloudbytes.dev/snippets/upgrade-python-to-latest-version-on-ubuntu-linux) to upgrade.
36+
37+
### `poetry ^1.1.14`
38+
- To install poetry follow [this guide](https://python-poetry.org/docs/#installation).
39+
- If you are on MacOS then you can install poetry simply with the following homebrew command
40+
```
41+
brew install poetry
42+
```
43+
> To make sure you're it's installed properly, run `poetry`. Learn more [here](https://python-poetry.org/docs/)
44+
45+
46+
47+
# Building and Testing
1748

18-
- Each of the package folders consists the pyproject.toml file and the poetry.lock file. In pyproject.toml file, one can find out all the project dependencies and configs related to the package. These files will be utilized by Poetry to install correct dependencies, build, lint and test the package.
49+
## Poetry
1950

20-
- For example, we can install deps, build and test the polywrap-msgpack package using Poetry.
51+
- We will be using [Poetry](https://python-poetry.org) for building and testing our packages.
52+
Each of the package folders consists of the `pyproject.toml` file and the `poetry.lock` file. In `pyproject.toml` file, one can find out all the project dependencies and configs related to the package. These files will be utilized by Poetry to install correct dependencies, build, lint and test the package.
53+
54+
- For example, we can **install** deps, **build** and **test** the `polywrap-msgpack` package using Poetry.
2155

2256
- Install dependencies using Poetry.
2357
```
2458
poetry install
2559
```
26-
> Make sure your cwd is `polywrap-msgpack` package.
27-
- As we can see in the pyproject.toml file, we installed [PyTest](https://docs.pytest.org) package. We will be using the same as our testing framework.
28-
- Now we are ready to build and test the core package using Poetry and PyTest.
29-
- To build the package run the following command
60+
> Make sure your cwd is the appropriate module, for example `polywrap-msgpack`, `polywrap-wasm` or `polywrap-client`.
61+
62+
## Pytest
63+
64+
In order to assure the integrity of the modules Polywrap Python Client uses [pytest 7.1.3](https://docs.pytest.org/en/7.1.x/contents.html) as a testing framework.
65+
66+
- As we can see in the `pyproject.toml` files, we installed the [PyTest](https://docs.pytest.org) package. We will be using it as our testing framework.
67+
- Now we are ready to **build** and **test** the core package using Poetry and PyTest.
68+
69+
To build the package run the following command
3070
```
3171
poetry build
3272
```
33-
- You need to activate the venv with poetry using `poetry shell` command before running any other command
34-
- We are using tox to run lint and tests easily. You can list all the testenv defined in the
35-
tox config with following command
73+
74+
You need to activate the venv with poetry using the `shell` command before running any other command
75+
```
76+
poetry shell
77+
```
78+
79+
Finally, to test your module to execute the test suite:
80+
```
81+
poetry run pytest
82+
```
83+
84+
85+
This last command will run a series of scripts that verify that the specific module of the client is performing as expected in your local machine. The output on your console should look something like this:
86+
87+
```
88+
$ poetry run pytest
89+
>>
90+
================================= test session starts =================================
91+
platform darwin -- Python 3.10.0, pytest-7.1.3, pluggy-1.0.0
92+
rootdir: /Users/polywrap/pycode/polywrap/toolchain/packages/py, configfile: pytest.ini
93+
collected 26 items
94+
95+
tests/test_msgpack.py ........................ [100%]
96+
```
97+
98+
### Debugging with Pytest:
99+
100+
You should expect to see the tests passing with a 100% accuracy. To better understand these outputs, read [this quick guide](https://docs.pytest.org/en/7.1.x/how-to/output.html). If any of the functionality fails (marked with an 'F'), or if there are any Warnings raised, you can debug them by running a verbose version of the test suite:
101+
- `poetry run pytests -v` or `poetry run pytests -vv` for even more detail
102+
- Reach out to the devs on the [Discord](https://discord.polywrap.io) explaining your situation, and what configuration you're using on your machine.
103+
104+
105+
## TOX
106+
We are using [`tox`](https://tox.wiki/en) to run lint and tests even more easily. Below are some basic commands to get you running.
107+
108+
### List all the testenv defined in the tox config
36109
```
37110
tox -a
38111
```
39-
- To run tests using tox simply run `tox`
40-
- You can run linters with the `tox -e lint` and check type with `tox -e typecheck`. By running `tox -e secure`, you can find security vulnerability if any.
41-
- While developing, you can run `tox -e dev` and apply lint fixes and style formatting.
42-
- As we see the mentioned tests passing, we are ready to update and test the package.
112+
### Run tests
113+
```
114+
tox
115+
```
116+
### Linting
117+
```
118+
tox -e lint
119+
```
120+
### Check types
121+
```
122+
tox -e typecheck
123+
```
43124

44-
## For VSCode users
45-
If you use VSCode, open this setup using the workspace file python-monorepo.code-workspace:
125+
### Find security vulnerabilities, if any
126+
```
127+
tox -e secure
128+
```
46129

130+
### Dev environment
131+
Use this command to only apply lint fixes and style formatting.
47132
```
48-
File -> Open Workspace from File...
133+
tox -e dev
49134
```
50135

51-
Each folder is now a project to VSCode. For the Python virtual environments to be picked up automatically, you need to create .vscode/settings.json file in each folder, pointing to the in-project virtual environment created by the poetry.
136+
- After running these commands we should see all the tests passing and commands executing successfully, which means that we are ready to update and test the package.
137+
- To create your own tox scripts, modify the `tox.ini` file in the respective module.
138+
139+
## VSCode users: Improved dev experience
140+
If you use VSCode, we have prepared a pre-configured workspace that improves your dev experience. So when you open VScode, set up the workspace file `python-monorepo.code-workspace` by going to:
52141

53-
You can easily find the correct virtual env by running following command in the package for which you want to find it
54142
```
55-
poetry shell
143+
File -> Open Workspace from File...
56144
```
145+
![File -> Open Workspace from File](misc/VScode_OpenWorkspaceFromFile.png)
146+
147+
Each folder is now a project to VSCode. This action does not change the underlying code, but facilitates the development process. So our file directory should look like this now:
148+
149+
![all modules have their respective folder, along with a root folder](misc/VScode_workspace.png)
57150

58-
Once you get the virtual env, you need to create the following `settings.json` file under the .vscode folder of the given package.
59-
Ex: in case of polywrap-client package, it would be under:
151+
> Note: You might have to do this step again next time you close and open VS code!
152+
153+
### Picking up the virtual environments automatically
154+
We will need to create a `.vscode/settings.json` file in each module's folder, pointing to the in-project virtual environment created by the poetry.
155+
156+
- You can easily find the path to the virtual env by running following command in the package for which you want to find it for:
60157
```
61-
polywrap-client -> .vscode -> settings.json
158+
poetry shell
62159
```
63160

64-
Here's the `settings.json` file we are using for configuring the vscode:
161+
- Once you get the path virtual env, you need to create the following `settings.json` file under the `.vscode/` folder of the given package. For example, in case of `polywrap-client` package, it would be under
162+
`./polywrap-client/.vscode/settings.json`
163+
164+
165+
Here's the structure `settings.json` file we are using for configuring the vscode. Make sure you update your virtual env path you got from poetry as the `python.defaultInterpreterPath` argument:
65166
```json
66167
{
67168
"python.formatting.provider": "black",
@@ -70,6 +171,71 @@ Here's the `settings.json` file we are using for configuring the vscode:
70171
"python.defaultInterpreterPath": "/Users/polywrap/Library/Caches/pypoetry/virtualenvs/polywrap-client-abcdef-py3.10"
71172
}
72173
```
73-
You need to put your virtual env path you got from the poetry under: `python.defaultInterpreterPath`
74174

75-
Once you configure this you should be good to go.
175+
Keep in mind that these venv paths will vary for each module you run `poetry shell` on. Once you configure these `setting.json` files correctly on each module you should be good to go!
176+
177+
178+
# What WASM wrappers can you execute today?
179+
180+
Check these resources to browse a variety available wrappers, for DeFi, decentralised storage, and other development utilites:
181+
182+
- [Wrappers.io](https://wrappers.io/)
183+
- [Polywrap Integrations Repository](https://github.com/polywrap/integrations)
184+
185+
# Example call
186+
187+
Calling a function of a wrapper from the python client is as simple as creating a file in the `TODO (?polywrap-client)` directory, importing the `PolywrapClient`, calling the `Uri` where the WASM wrapper is hosted, and specifying any required `arguments`.
188+
189+
Here is an example which takes in a message as a string and returns it.
190+
191+
```python
192+
# hello_world.py
193+
from polywrap_client import PolywrapClient
194+
from polywrap_core import Uri, InvokerOptions
195+
196+
async def echo_message(message: str):
197+
198+
# Instantiate the client
199+
client = PolywrapClient()
200+
201+
# Load the WebAssembly wrapper through a URI that points to local file system
202+
uri = Uri('wrap://ens/rinkeby/helloworld.dev.polywrap.eth')
203+
204+
args = {"arg": message }
205+
206+
# Configure the client
207+
options = InvokerOptions(
208+
uri=uri, method="simpleMethod", args=args, encode_result=False
209+
)
210+
211+
# Invoke the wrapper
212+
result = await client.invoke(options)
213+
214+
return result.result
215+
216+
if __name__ == "__main__":
217+
return echo_message('hello polywrap!')
218+
```
219+
220+
221+
## Creating your own tests for the client
222+
223+
By creating tests you can quickly experiment with the Polywrap Client and its growing set of wrappers. Since Pytest is already set up on the repo, go to the `polywrap-client\tests\` directory, and take a look at how some of the functions are built. You can use similar patterns to mod your own apps and build new prototypes with more complex functionality.
224+
225+
Here's a good guide to learn about [building tests with Pytest](https://realpython.com/pytest-python-testing/)
226+
227+
# Contributing
228+
229+
The Polywrap project is completely open-source and we welcome contributors of all levels. Learn more about how you can contribute [here](https://github.com/polywrap/toolchain#contributing).
230+
231+
# Contact Us:
232+
233+
[Join our discord](https://discord.polywrap.io) and ask your questions right away!
234+
235+
236+
# Resources
237+
238+
- [Polywrap Documentation](https://docs.polywrap.io)
239+
- [Polywrap Integrations Repository](https://github.com/polywrap/integrations)
240+
- [Running operations concurrently with python's asyncio](https://realpython.com/async-io-python/#the-10000-foot-view-of-async-io)
241+
- [Intro Video](TODO)
270 KB
Loading

misc/VScode_workspace.png

15 KB
Loading

0 commit comments

Comments
 (0)