Skip to content
/ pylib Public

Python builtins/standard-Lib functions ported to Nim

License

Notifications You must be signed in to change notification settings

nimpylib/pylib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NimPylib

C Test JS Test Docs Issues PRs Commits

Originating from a fork of https://github.com/Yardanico/nimpylib, which is announced to be not maintained since 2021. This is no longer a simple fork with some simple improvement, but with great features.

Nimpylib is a collection of Python-like operators/functions and libraries (syntax sugar no longer just syntax sugar). It can help you to translate your Python program to Nim, and gain a better view into different behaviors between Python and Nim.

Usage

import pylib
from std/os import sleep  # python's `sleep` is in `time` module, however
from pylib/Lib/sys import nil  # like python's `import sys`
from pylib/Lib/platform import nil  # like python's `import sys`
import pylib/Lib/tempfile # more python-stdlib in pylib/Lib...

print 42  # print can be used with and without parenthesis too, like Python2.
pass str("This is a string.") # discard the string. Python doesn't allow this, however

# NOTE: from now on, the following is just valid Python3 code!
# only add the following to make it Python:
# import platform
# from timeit import timeit
# from time import sleep
# from tempfile import NamedTemporaryFile, TemporaryDirectory
print( f"{9.0} Hello {42} World {1 + 2}" ) # Python-like string interpolation

class O:
  @staticmethod
  def f():
    print("O.f")

O.f()

def show_range_list():
  python_like_range = range(0, -10, -2)
  print(list(python_like_range)) # [0, -2, -4, -6, -8]
show_range_list()

# Why using so many `def`s?
# as in `def`, you can write Nim more Python-like
# e.g. nondeclared assignment

# func definition
# typing is suppported and optional
def foo(a: int, b = 1, *args) -> int:
  def add(a, b): return a + b # nesting
  for i in args: print(i)
  return add(a, b)

# python 3.12's type statement
type Number = float | int  # which is originally supported by nim-lang itself, however ;) 

for i in range(10):
  # 0 1 2 3 4 5 6 7 8 9
  print(i, endl=" ")
print("done!")

# Python-like variable unpacking
def show_unpack():
  data = list(range(3, 15, 2))
  (first, second, *rest, last) = data
  assert (first + second + last) == (3 + 5 + 13)
  assert list(rest) == list([7, 9, 11])

show_unpack()

if (a := 6) > 5:
  assert a == 6

if (b := 42.0) > 5.0:
  assert b == 42.0

if (c := "hello") == "hello":
  assert c == "hello"

print("a".center(9)) # "    a    "

print("" or "b") # "b"
print("a" or "b") # "a"

print(not "") # True

print("Hello,", input("What is your name? "), endl="\n~\n")

def show_divmod_and_unpack(integer_bytes):
  (kilo, bite) = divmod(integer_bytes, 1_024)
  (mega, kilo) = divmod(kilo, 1_024)
  (giga, mega) = divmod(mega, 1_024)
  (tera, giga) = divmod(giga, 1_024)
  (peta, tera) = divmod(tera, 1_024)
  (exa, peta)  = divmod(peta, 1_024)
  (zetta, exa) = divmod(exa,  1_024)
  (yotta, zetta) = divmod(zetta, 1_024)
show_divmod_and_unpack(2_313_354_324)

let arg = "hello"
let anon = lambda: arg + " world"
assert anon() == "hello world"


print(sys.platform) # "linux"

print(platform.machine) # "amd64"

def allAny():
  truty = all([True, True, False])
  print(truty) # False

  truty = any([True, True, False])
  print(truty) # True
allAny()

timeit(100):  # Python-like timeit.timeit("code_to_benchmark", number=int)
  sleep(9)    # Repeats this code 100 times. Output is very informative.

# 2020-06-17T21:59:09+03:00 TimeIt: 100 Repetitions on 927 milliseconds, 704 microseconds, and 816 nanoseconds, CPU Time 0.0007382400000000003.

# Support for Python-like with statements
# All objects are closed at the end of the with statement
def t_open():
  with open("some_file.txt", 'w') as file:
    _ = file.write("hello world!")

  with open("some_file.txt", 'r') as file:
    while True:
      s = file.readline()
      if s == "": break
      print(s)

t_open()

def show_tempfile():
  with NamedTemporaryFile() as file:
    _ = file.write(b"test!")  # in binary mode

  with TemporaryDirectory() as name:
    print(name)

show_tempfile()

class Example(object):  # Mimic simple Python "classes".
  """Example class with Python-ish Nim syntax!."""
  start: int
  stop: int
  step: int
  def init(self, start, stop, step=1):
    self.start = start
    self.stop = stop
    self.step = step

  def stopit(self, argument):
    """Example function with Python-ish Nim syntax."""
    self.stop = argument
    return self.stop

# Oop, the following is no longer Python....
let e = newExample(5, 3)
print(e.stopit(5))

Nimpylib heavily relies on Nim generics, converters, operator overloading, and even on concepts.

Check the Examples folder for more examples. Have more Macros or Templates for Python-like syntax, send Pull Request.

Installation

To install nimpylib, you can simply run

nimble install https://github.com/litlighilit/nimpylib

or

git clone https://github.com/litlighilit/nimpylib
cd nimpylib
nimble install

Uninstall with nimble uninstall pylib.

Requisites

Supported features

  • F-Strings f"foo {variable} bar {1 + 2} baz"
  • str bytes bytearray list dict set with their methods
  • Python-like variable unpacking
  • Math with Float and Int mixed like Python.
  • import antigravity
  • lambda:
  • class Python-like OOP with methods and DocStrings (without multi-inheritance)
  • @classmethod and @staticmethod
  • with open(fn, [, ...]): Read, write, append, and read(), seek(), tell(), etc.
  • super(...).method(...)
  • global/nonlocal (with some limits)
  • with tempfile.TemporaryDirectory(): Read, write, append, and file.read().
  • with tempfile.NamedTemporaryFile() as file: Read, write, append, and file.read().
  • timeit() with Nanoseconds precision
  • True / False
  • pass also can take and discard any arguments
  • del foo[x]
  • := Walrus Operator
  • abs()
  • all()
  • any()
  • ascii()
  • bin()
  • chr()
  • complex()
  • divmod()
  • enumerate()
  • filter()
  • float()
  • format()
  • getattr()
  • hasattr()
  • hash()
  • hex()
  • id()
  • input()
  • int()
  • isinstance()
  • issubclass()
  • iter()
  • list()
  • map()
  • max()
  • min()
  • next()
  • oct()
  • ord()
  • open() (though without close_fd, opener, errors)
  • pow(base, exp, mod=None)
  • print("foo") / print "foo" Python2 like
  • range()
  • reversed(iterable)
  • round()
  • hasattr()
  • set(), also named pyset() to distingish with system.set
  • slice()
  • sorted(iterable)
  • str()
  • sum()
  • != and Python1 <>
  • long() Python2 like
  • unicode() Python2 like
  • u"string here" / u'a' Python2 like
  • b"string here" / b'a' Python2 like
  • zip(iterable1, iterable2)
  • More...

Other Python-like modules

Tests

$ nimble test
[OK] getattr/set/has
[OK] bytes
[OK] bytes meth
[OK] complex
[OK] decorator
[OK] custom decorator
[OK] dict
[OK] rewrite in `def`
[OK] Floor division
[OK] io & with
[OK] bltin iters
[OK] iters as iterable
[OK] Python-like types
[OK] divmod
[OK] pass
[OK] lambda
[OK] walrus operator
2024-06-14T21:31:43+08:00 TimeIt: 9 Repetitions on 1 millisecond, 14 microseconds, and 700 nanoseconds, CPU Time 0.0.
[OK] timeit
[OK] hex()
[OK] chr()
[OK] oct()
[OK] ord()
[OK] bin()
[OK] Range-like Nim procedure
[OK] tonim macro
[OK] unpack macro
[OK] With statement
[OK] list shallow
[OK] list.sort
[OK] list methods
[OK] str.format
[OK] random
[OK] Lib/string
[OK] Lib/math
[OK] os
[OK] os.path
[OK] tempfile
[OK] iter/next
[OK] int(x[, base])
[OK] float(str)
[OK] Modulo operations
[OK] str operations
[OK] str index
[OK] str methods
[OK] str.maketrans&translate
[OK] set
[OK] bytearray

About

Python builtins/standard-Lib functions ported to Nim

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •