All Projects → sstadick → ponim

sstadick / ponim

Licence: MIT license
Nim + Python + Poetry = :)

Programming Languages

python
139335 projects - #7 most used programming language
nim
578 projects

Labels

Projects that are alternatives of or similar to ponim

Dephell
📦 🔥 Python project management. Manage packages: convert between formats, lock, install, resolve, isolate, test, build graph, show outdated, audit. Manage venvs, build package, bump version.
Stars: ✭ 1,730 (+5306.25%)
Mutual labels:  poetry
fish-poetry
🐟🐍 a fish plugin that automatically activates the poetry subshell
Stars: ✭ 25 (-21.87%)
Mutual labels:  poetry
poesy
Poetry generation via natural language markov models
Stars: ✭ 56 (+75%)
Mutual labels:  poetry
jira-sprint-analytics
No description or website provided.
Stars: ✭ 13 (-59.37%)
Mutual labels:  poetry
desktop
گنجور رومیزی
Stars: ✭ 50 (+56.25%)
Mutual labels:  poetry
poet-v
Vim Meets Poetry and Pipenv Virtual Environments
Stars: ✭ 57 (+78.13%)
Mutual labels:  poetry
Tensorflow poems
中文古诗自动作诗机器人,屌炸天,基于tensorflow1.10 api,正在积极维护升级中,快star,保持更新!
Stars: ✭ 3,429 (+10615.63%)
Mutual labels:  poetry
blackout
Procedurally generated blackout poetry
Stars: ✭ 56 (+75%)
Mutual labels:  poetry
weapp-poem
诗词墨客 - 最全中华古诗词小程序
Stars: ✭ 409 (+1178.13%)
Mutual labels:  poetry
Saaghar
“Saaghar” (ساغر) is a Persian poetry software written by C++ under Qt framework, it uses "ganjoor" database as its database. It has tab feature in both its “Viewer” and its “Search” page that cause it be suitable for research goals.
Stars: ✭ 42 (+31.25%)
Mutual labels:  poetry
dotfiles
🔯 A collection of my rc files (tmux, neovim, zsh, fish, poetry, git, ...etc) and utilities that make everyday coding fun!
Stars: ✭ 23 (-28.12%)
Mutual labels:  poetry
symmetric
A powerful tool to enable super fast module-to-API transformations. Learn in minutes, implement in seconds. Batteries included.
Stars: ✭ 65 (+103.13%)
Mutual labels:  poetry
poemexe
Code for the poem.exe bot on Twitter and Mastodon.
Stars: ✭ 17 (-46.87%)
Mutual labels:  poetry
poesy
Poetic processing, for Python.
Stars: ✭ 28 (-12.5%)
Mutual labels:  poetry
Divan.hs
Ottoman Divan poetry vezin checker in Haskell!
Stars: ✭ 37 (+15.63%)
Mutual labels:  poetry
Chinese Poetry
The most comprehensive database of Chinese poetry 🧶最全中华古诗词数据库, 唐宋两朝近一万四千古诗人, 接近5.5万首唐诗加26万宋诗. 两宋时期1564位词人,21050首词。
Stars: ✭ 34,881 (+108903.13%)
Mutual labels:  poetry
cookiecutter-modern-pypackage
Cookiecutter template for a modern Python package.
Stars: ✭ 97 (+203.13%)
Mutual labels:  poetry
inboard
🚢 Docker images and utilities to power your Python APIs and help you ship faster. With support for Uvicorn, Gunicorn, Starlette, and FastAPI.
Stars: ✭ 106 (+231.25%)
Mutual labels:  poetry
poetry-setup
Generate setup.py (setuptools) from pyproject.toml (poetry)
Stars: ✭ 44 (+37.5%)
Mutual labels:  poetry
Text-Generate-RNN
中国古诗生成(文本生成)
Stars: ✭ 106 (+231.25%)
Mutual labels:  poetry

Nim + Python + Poetry = :)

Sometimes you need a little more speed. Or you need to be able to justify writing some Nim. In either case there is an easy way to bundle your Nim code in with your Python code.

In this article we will do a quick overview of how to add Nim via Poetry. Before diving in here are the tools being used:

  • Nim: A fast, pythonic, compiled language
  • Nimpy: A Nim library for exporting functions to python
  • Nimporter: A Python library for importing .nim files
  • Poetry: A build tool for Python heavily influenced by Cargo

Install Things

You will need to install Nim, Poetry, and Nimpy. Nim and poetry include very good install instructions via the links above. For Nimpy, I recommend installing it via Nimble, which is bundled with Nim. Just run nimble install nimpy.

Not Doing

Both nimpy and nimporter have excellent docs. This guide is just walking through how to hook nimporter in with poetry, not the details of nimpy, nimporter, or poetry.

End structure

.
├── LICENSE
├── README.md
├── poetry.lock
├── ponim
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── adder.nim.hash
│   │   ├── adder.so
│   │   └── subtractor.cpython-37.pyc
│   ├── adder.nim
│   └── subtractor.py
└── pyproject.toml

This is a Hack

But it kind of works... and Python packaging is basically all one giant gross hack, so maybe this isn't so bad?

Begin Hack

Create a poetry project:

mkdir ponim
cd ponim
poetry init -n

This will create a pyproject.toml file in your current directory. Next up, lets make this a Python library!

mkdir ponim
cd ponim
touch __init__.py
touch subtractor.py
cd ../

We are just going to make a subtractor function in python:

# In subtractor.py
def subtractor(a: int, b: int) -> int:
    return a - b

Now let's make that available high up the import chain:

# In __init__.py
from .subtractor import subtractor

Cool, now let's test that all that works so far and install our package into a venv managed by Poetry:

# back in your main project dir
poetry shell
poetry install

And now drop into the Python REPL and test it:

python
>>> import ponim
>>> ponim.subtractor(44, 2)
42

It works! So let's add some Nim.

poetry add nimporter
poetry install
touch ponim/adder.nim

And then fill out the nim file:

# In adder.nim
import nimpy

proc add(a: int, b: int): int {.exportpy.} =
    return a + b

And now for the magic bit, making this work seamlessly with subtractor.py. We'll update the __init__.py to import the nim file and make it available in the package.

import nimporter # This must come first
from .subtractor import subtractor
from .adder import adder

And then rebuild the package and test it out

poetry install
python
>>> import ponim
>>> ponim.subtractor(44, 2)
42
>>> ponim.adder(40, 2)
42

And that's it! You now have nim code bundled with your Python code! To create a wheel all you need to do is run poetry build and your .nim files will be included. The host system will still need to have nimpy + Nim installed, but that shouldn't be too hard to work around / improve in the future.

There is the overhead of the nim code compiling on the first run. If you actually use this to bundle application code, you could work around that issue by running 'python -c "import ponim"' after installing the wheel. This will cache the compiled nim code in your __pycache__.

So why go through all this trouble? Nim is way nicer to work with than Cython, and offers the same or better performance. There are a few wrinkles yet to be smoothed out, but this setup could easily be used with docker to deploy a python + nim application. With all the exiting things going on in Nim, like optional move semantics for performance, and the concurrency work, having this easy of an interop is amazing.

Happy Hacking!

Blog version of this article: http://ducktape.blot.im/nim-python-poetry-nbsp

Notes

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].