{"id":29073394,"url":"https://github.com/nimpylib/nimpylib","last_synced_at":"2025-07-11T19:01:58.552Z","repository":{"id":159893031,"uuid":"576904092","full_name":"nimpylib/nimpylib","owner":"nimpylib","description":"\"Write Python in Nim!\"   Python builtins/standard-Lib functions ported to Nim","archived":false,"fork":false,"pushed_at":"2025-07-05T19:04:29.000Z","size":13960,"stargazers_count":41,"open_issues_count":22,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-05T20:22:38.193Z","etag":null,"topics":["library","metaprogramming","nim-lang","nimble","python","python3"],"latest_commit_sha":null,"homepage":"https://nimpylib.org/","language":"Nim","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"Yardanico/nimpylib","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nimpylib.png","metadata":{"files":{"readme":"README.md","changelog":"changelogs/changelog_0_6_0.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"litlighilit","open_collective":"nimpylib"}},"created_at":"2022-12-11T11:21:07.000Z","updated_at":"2025-07-05T19:00:39.000Z","dependencies_parsed_at":"2024-05-08T01:45:31.557Z","dependency_job_id":"0559afdc-3dc7-4daa-86ac-d8f6f46b20e5","html_url":"https://github.com/nimpylib/nimpylib","commit_stats":null,"previous_names":["nimpylib/nimpylib"],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/nimpylib/nimpylib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nimpylib%2Fnimpylib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nimpylib%2Fnimpylib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nimpylib%2Fnimpylib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nimpylib%2Fnimpylib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nimpylib","download_url":"https://codeload.github.com/nimpylib/nimpylib/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nimpylib%2Fnimpylib/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264878580,"owners_count":23677450,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["library","metaprogramming","nim-lang","nimble","python","python3"],"created_at":"2025-06-27T15:01:08.935Z","updated_at":"2025-07-11T19:01:58.496Z","avatar_url":"https://github.com/nimpylib.png","language":"Nim","funding_links":["https://github.com/sponsors/litlighilit","https://opencollective.com/nimpylib"],"categories":["Nim"],"sub_categories":[],"readme":"# NimPylib\n\n[![C Test](https://github.com/nimpylib/pylib/workflows/testC/badge.svg)](https://github.com/nimpylib/pylib/actions/workflows/testC.yml)\n[![JS Test](https://github.com/nimpylib/pylib/workflows/testJs/badge.svg)](https://github.com/nimpylib/pylib/actions/workflows/testJs.yml)\n[![Docs](https://github.com/nimpylib/pylib/workflows/docs/badge.svg)](https://github.com/nimpylib/pylib/actions/workflows/docs.yml)\n![](https://img.shields.io/github/languages/code-size/nimpylib/pylib?style=flat)\n[![Commits](https://img.shields.io/github/last-commit/nimpylib/pylib?style=flat)](https://github.com/nimpylib/pylib/commits/)\n\u003c!--![](https://img.shields.io/github/stars/nimpylib/pylib?style=flat \"Star NimPylib on GitHub!\")\n[![Issues](https://img.shields.io/github/issues-raw/nimpylib/pylib?style=flat)](https://github.com/nimpylib/pylib/issues)\n[![PRs](https://img.shields.io/github/issues-pr-raw/nimpylib/pylib?style=flat)](https://github.com/nimpylib/pylib/pulls)--\u003e\n\n\u003e Write Python in Nim\n\nNimpylib is a collection of Python-like operators/functions and libraries as well as syntax sugars.\n\n\n---\n\n[Read Docs](https://nimpylib.github.io/pylib/)\n|\n[Lib Docs][]\n|\n[Wiki about History](https://github.com/nimpylib/pylib/wiki/History)\n|\n[Design Wiki](https://github.com/nimpylib/pylib/wiki/Design)\n\n[Lib Docs]: https://nimpylib.github.io/pylib/Lib\n\n## Why NimPyLib?\nIt helps you to:\n- use much Python-like out-of-box API in Nim:\n  - with no need of any Python dependency (neither dynamic library nor binary).\n  - even handy for ones who don't use much Python but want more functions in Nim\n- translate your Python program to Nim:\n  - gaining a right-away speed boot of even 700x\n  - no worry about binary distribution or packaging, just \"compile once, distribute everywhere\"\n  - rid of many annoying runtime-errors (which are turned to compile-time error)\n- gain a better view into different behaviors between Python and Nim:\n  - `dynamically-typed` vs `statically-typed`\n  - unconvertible syntax from Python to Nim, e.g.\n    [`end` keyword](./doc/mustRewriteExtern/endKeyword.md),\n    [`not in` syntax](./doc/mustRewriteExtern/not-in.md)\n\n### Backends\n\nThanks to Nim supporting multiply backends, pylib currently officially supports\nto compile to C and JavaScript [^JS]. C++ and ObjC backends are currently not tested.\n\n[^JS]: Some of features (listed\n [here](./tests/skipJs.txt))\n and Libs (listed\n [here](./src/pylib/Lib/test/skipJs.txt))\nis not available for JS backend yet.\n\n## Demo\n\n```nim\nimport pylib\nfrom pylib/Lib/timeit import timeit\nfrom pylib/Lib/time import sleep\nfrom pylib/Lib/sys import nil  # like python's `import sys`\nfrom pylib/Lib/platform import nil  # like python's `import platform`\nimport pylib/Lib/tempfile\n# like python's `import tempfile; from tempfile import *`\n# more python-stdlib in pylib/Lib/...\n\nprint 42  # print can be used with and without parenthesis too, like Python2.\n\n# NOTE: from now on, the following is just valid Python3 code!\n# only add the following to make it Python:\n# import platform\n# from timeit import timeit\n# from time import sleep\n# from tempfile import NamedTemporaryFile, TemporaryDirectory\nprint( f\"{9.0} Hello {42} World {1 + 2}\" ) # Python-like string interpolation\n\nclass O:\n  @staticmethod\n  def f():\n    print(\"O.f\")\n\nO.f()\n\ndef show_range_list():\n  python_like_range = range(0, -10, -2)\n  print(list(python_like_range)[1:-1]) # [-2, -4, -6]\nshow_range_list()\n\n# Why using so many `def`s?\n# as in `def`, you can write Nim more Python-like\n# e.g. nondeclared assignment\n\n# func definition\n# typing is suppported and optional\ndef foo(a: int, b = 1, *args) -\u003e int:\n  def add(a, b): return a + b # nesting\n  for i in args: print(i)\n  return add(a, b)\n\ndef show_literals():\n  ls = [1, 2]  # if outside `def`, `ls` will be an Nim's `array`,\n  #   which is fixed size and \"pass by value\"\n  ls_shallow = ls\n  ls.append(3)\n  assert len(ls_shallow) == 3\n\n  s = {\"Rachel\", \"Zack\"}  # if outside `def`, `s` will be an Nim's `set`,\n  #    which only supports small ordinal type as elements\n  s.add(\"Zack\")\n  assert len(s) == 2\n\n  d = {  # if outside `def`, `d` will be an Nim's `array[I, (K, V)]`,\n    #   which even lacks `__getitem__` method\n    'S': \"kaneki ken\"\n  }\n\n  assert d['S'].title() == \"Kaneki Ken\"  # if outside `def`,\n  #   all double-quotation marked literals will be Nim's `string`,\n  #     which is more like `bytearray`\n  #   and single-quotation marked literals will be Nim's `char`,\n  #     which repesents a single byte (ASCII character)\n\nshow_literals()\n\n\n# python 3.12's type statement\ntype Number = float | int  # which is originally supported by nim-lang itself, however ;) \n\nfor i in range(10):\n  # 0 1 2 3 4 5 6 7 8 9\n  print(i, endl=\" \")\nprint(\"done!\")\n\n# Python-like variable unpacking\ndef show_unpack():\n  data = list(range(3, 15, 2))\n  (first, second, *rest, last) = data\n  assert (first + second + last) == (3 + 5 + 13)\n\nshow_unpack()\n\nif (a := 6) \u003e 5:\n  assert a == 6\n\nprint(\"a\".center(9)) # \"    a    \"\n\nprint(\"\" or \"b\") # \"b\"\nprint(\"a\" or \"b\") # \"a\"\n\nprint(not \"\") # True\n\nprint(\"Hello,\", input(\"What is your name? \"), endl=\"\\n~\\n\")\n\ndef show_divmod_and_unpack(integer_bytes):\n  (kilo, bite) = divmod(integer_bytes, 1_024)\n  (mega, kilo) = divmod(kilo, 1_024)\n  (giga, mega) = divmod(mega, 1_024)\nshow_divmod_and_unpack(2_313_354_324)\n\ndef lambda_closure(arg):\n  anno = lambda: \"hello \" + arg\n  return anno()\nassert lambda_closure(\"world\") == \"hello world\"\n\nprint(sys.platform) # \"linux\"\n\nprint(platform.machine) # \"x86_64\"\n\ndef allAny():\n  truty = all([True, True, False])\n  print(truty) # False\n\n  truty = any([True, True, False])\n  print(truty) # True\nallAny()\n\ndef a_little_sleep():\n  \"sleep around 0.001 milsecs.\"\n  # note Nim's os.sleep's unit is milsec,\n  # while Python's time.sleep's is second.\n  sleep(0.001)\n\nassert timeit(a_little_sleep, number=1000) \u003e 1.0\n\n# Support for Python-like with statements\n# All objects are closed at the end of the with statement\ndef test_open():\n  with open(\"some_file.txt\", 'w') as file:\n    _ = file.write(\"hello world!\")\n\n  with open(\"some_file.txt\", 'r') as file:\n    while True:\n      s = file.readline()\n      if s == \"\": break\n      print(s)\n\ntest_open()\n\ndef show_tempfile():\n  with NamedTemporaryFile() as file:\n    _ = file.write(b\"test!\")  # in binary mode\n\n  with TemporaryDirectory() as name:\n    print(name)\n\nshow_tempfile()\n\nclass Example(object):  # Mimic simple Python \"classes\".\n  \"\"\"Example class with Python-ish Nim syntax!.\"\"\"\n  start: int\n  stop: int\n  step: int\n  def init(self, start, stop, step=1):\n    self.start = start\n    self.stop = stop\n    self.step = step\n\n  def stopit(self, argument):\n    \"\"\"Example function with Python-ish Nim syntax.\"\"\"\n    self.stop = argument\n    return self.stop\n\ndef exa():\n  e = Example(5, 3)\n  print(e.stopit(5))\n\nexa()\n```\n\nNimpylib heavily relies on Nim generics, converters, operator overloading, and even on concepts.\n\nCheck the [Examples folder](./examples/) for more examples.\nHave more Macros or Templates for Python-like syntax, send [Pull Request](https://github.com/nimpylib/pylib/pulls).\n\n## Installation\n\n```shell\nnimble install pylib\n```\n\n\u003e If the installing is stuck with: \n`Downloading https://github.com/Yardanico/nimpylib using git`\nPlease note your nimble package.json is outdated, and that old URL is 404 now [^oldUrl].\nRun `nimble refresh` to fetch a newer `package.json`\n\nOf course, a workaround is to install with full URL:\n\n```shell\nnimble install https://github.com/nimpylib/pylib\n```\n\n[^oldUrl]: see [wiki-history](https://github.com/nimpylib/pylib/wiki/History#the-newer-package-url) for details\n\nUninstall with `nimble uninstall pylib`.\n\n\n## Requisites\n\n- [Nim](https://nim-lang.org)\n\n\n## Supported features\n\n- [x] F-Strings `f\"foo {variable} bar {1 + 2} baz\"`\n- [x] `str` `bytes` `bytearray` `list` `dict` `set` `frozenset()` with their methods\n- [x] Python-like variable unpacking\n- [x] Math with Float and Int mixed like Python.\n- [x] `lambda:`\n- [x] `class` Python-like OOP with methods and DocStrings (without multi-inheritance)\n- [x] `@classmethod` and `@staticmethod`\n- [x] `with open(fn, [, ...]):` Read, write, append, and `read()`, `seek()`, `tell()`, etc.\n- [x] `super(...).method(...)`\n- [x] `global/nonlocal` (with some limits)\n- [x] `True` / `False`\n- [x] `pass`\n- [x] `del foo[x]`\n- [x] `:=` Walrus Operator\n- [x] `abs()`\n- [x] `all()`\n- [x] `any()`\n- [x] `ascii()`\n- [x] `bin()`\n- [x] `chr()`\n- [x] `complex()`\n- [x] `divmod()`\n- [x] `enumerate()`\n- [x] `filter()`\n- [x] `float()`\n- [x] `format()`\n- [x] `getattr()`\n- [x] `hasattr()`\n- [x] `hash()`\n- [x] `hex()`\n- [x] `id()`\n- [x] `input()`\n- [x] `int()`\n- [x] `isinstance()`\n- [x] `issubclass()`\n- [x] `iter()`\n- [x] `list()`\n- [x] `map()`\n- [x] `max()`\n- [x] `min()`\n- [x] `next()`\n- [x] `oct()`\n- [x] `ord()`\n- [x] `open()` (though without close_fd, opener, errors)\n- [x] `pow(base, exp, mod=None)`\n- [x] `print(\"foo\")` / `print \"foo\"` Python2 like\n- [x] `range()`\n- [x] `reversed(iterable)`\n- [x] `round()`\n- [x] `hasattr()`\n- [x] `set()`, also named `pyset()` to distingish with `system.set`\n- [x] `slice()`\n- [x] `sorted(iterable)`\n- [x] `str()`\n- [x] `sum()`\n- [x] `!=` and Python1 `\u003c\u003e`\n- [x] `long()` Python2 like (deprecated)\n- [x] `u\"string here\"` / `u'a'` Python2 like\n- [x] `b\"string here\"` / `b'a'`\n- [x] `zip(*iterables, strict=False)`\n- [x] (WIP) standard libraries `math`, `random`, `datetime`, `os`, `tempfile`, `timeit`, ... (see [Lib Docs][] for all supported)\n- [ ] `aiter` `anext` and `await` (yet `async def` is supported)\n- More...\n\n### Features cannot be implemented\nHowever, due to Nim's AST astrict[^nimInvalidAST], a few syntaxes of Python cannot be implemented.\n\nSee [here](./doc/mustRewriteExtern/) for details and workaround.\n\n[^nimInvalidAst]: Mostly because they cannot form valid AST in Nim.\n\n## Other Python-like modules\n\n- [serach in nimble](https://nimble.directory/search?query=python)\n- [metacraft-labs/py2nim](https://github.com/metacraft-labs/py2nim): py2nim transpiler in Nim.\n- [py2many/py2many](https://github.com/py2many/py2many): py2nim transpiler in Python.\n- [juancarlospaco/cpython](https://github.com/juancarlospaco/cpython): invoke Python Standard Library API in Nim.\n- [yglukhov/nimpy](https://github.com/yglukhov/nimpy): Python bridge in Nim.\n\n## Tests\n\nThis is one snippest from version 0.9.5:\n\n\u003e as too much tests output is it,\nand it's not suitable to list too much content in README,\nthe following demo\nis not likely to be updated from then on.\n\n```console\n$ nimble test\n[Suite] datetime\n  [OK] utcoffset\n  [OK] attrs\n  [OK] isoformat\n\n[Suite] fromisoformat\n  [OK] if reversible\n\n[Suite] timedelta\n  [OK] init\n  [OK] float init\n  [OK] normalize\n  [OK] stringify\n\n[Suite] date\n  [OK] fromisocalendar\n  [OK] fromisocalendar_value_errors\n  [OK] ordinal_conversion\n  [OK] replace\n  [OK] strftime\n  [OK] ctime\n\n[Suite] tzinfo\n  [OK] fromtimestamp\n\n[Suite] gamma\n  [OK] gamma(-integer)\n\n[Suite] ldexp\n\n[Suite] sumprod\n  [OK] array\n  [OK] CPython:test_math.testSumProd\n\n[Suite] constants\n  [OK] nan\n  [OK] inf\n\n[Suite] classify\n  [OK] isinf\n  [OK] isfinite\n\n[Suite] nextafter_ulp\n  [OK] nextafter\n  [OK] ulp\n\n[Suite] ldexp\n  [OK] static\n  [OK] small\n  [OK] non-normal first arg\n  [OK] large second arg\n\n[Suite] ErrnoAttributeTests\n  [OK] using_errorcode\n[OK] touch, unlink, is_file\n[OK] Lib/tempfile\n[OK] Lib/time\n[OK] Lib/timeit\n\n[Suite] Lib/array\n  [OK] py3.13: 'w' Py_UCS4\n  [OK] bytes\n  [OK] cmp\n  [OK] byteswap\n\n[Suite] os.path\n  [OK] if export right\n  [OK] getxtime\n\n[Suite] Lib/os with no JS support\n  [OK] mkdir rmdir\n  [OK] open fdopen close\n  [OK] get,set_inheritable\n[OK] getattr/set/has\n[OK] bytearray\n\n[Suite] bytes\n  [OK] getitem\n  [OK] meth\n  [OK] repr\n\n[Suite] complex.__init__(str)\n  [OK] from str\n  [OK] negative_nans_from_string\n\n[Suite] complex\n  [OK] init\n  [OK] literals\n  [OK] str\n  [OK] op\n\n[Suite] complex.__repr__\n  [OK] (N+nanj)\n  [OK] real == 0.0\n\n[Suite] complex.__pow__\n  [OK] CPython:test_complex.ComplexTest.test_pow\n  [OK] with small integer exponents\n[OK] decorator\n[OK] custom decorator\n[OK] dict\n\n[Suite] float.fromhex\n  [OK] literals\n  [OK] nans\n  [OK] some values\n  [OK] overflow\n  [OK] zeros and underflow\n  [OK] round-half-even\n\n[Suite] float.fromhex and hex\n  [OK] roundtrip\n\n[Suite] float\n  [OK] hex\n  [OK] test_nan_signs\n  [OK] is_integer\n  [OK] as_integer_ratio\n\n[Suite] rewrite as py stmt\n  [OK] rewrite in `def`\n  [OK] rewrite raise\n[OK] Floor division\n[OK] int.{from,to}_bytes\n[OK] io \u0026 with\n[OK] bltin iters\n[OK] iters as iterable\n[OK] iter/next\n[OK] random\n[OK] Lib/string\n[OK] list shallow\n[OK] list.sort\n[OK] list methods\n[OK] Python-like types\n[OK] divmod\n[OK] pass\n[OK] lambda\n[OK] walrus operator\n[OK] hex()\n[OK] chr()\n[OK] oct()\n[OK] ord()\n[OK] bin()\n[OK] Modulo operations\n[OK] int(x[, base])\n[OK] float(str)\n[OK] Range-like Nim procedure\n[OK] str.format\n[OK] str operations\n[OK] str index\n[OK] str methods\n[OK] str.maketrans\u0026translate\n[OK] tonim macro\n[OK] unpack macro\n[OK] With statement\n[OK] generics in func signature\n[OK] generics in class's methods\n[OK] set\nPASS: tests/testaments/builtins/print.nim c                        ( 1.93 sec)\nSKIP: tests/testaments/builtins/print.nim js\nPASS: tests/testaments/builtins/print_ct.nim c                     ( 1.93 sec)\nPASS: tests/testaments/builtins/zip_no_seq.nim c                   ( 1.96 sec)\nPASS: tests/testaments/builtins/zip_more_args.nim c                ( 0.65 sec)\nPASS: tests/testaments/builtins/filter_toseq_deadloop.nim c        ( 0.66 sec)\nUsed D:\\software\\scoop\\shims\\nim.exe to run the tests. Use --nim to override.\nPASS: tests/testaments/pysugar/colonToSlice.nim c                  ( 2.75 sec)\nSKIP: tests/testaments/pysugar/colonToSlice.nim js\nPASS: tests/testaments/pysugar/strlitCat.nim c                     ( 1.92 sec)\nSKIP: tests/testaments/pysugar/strlitCat.nim js\nPASS: tests/testaments/pysugar/tripleStrTranslate.nim c            ( 0.84 sec)\nPASS: tests/testaments/pysugar/autoSetListDict.nim c               ( 3.40 sec)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnimpylib%2Fnimpylib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnimpylib%2Fnimpylib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnimpylib%2Fnimpylib/lists"}