Writing Python modules in Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. It’s Efficient, expressive, elegant and definitely worth to check. While I was playing with it, I stumbled upon an interesting module that allows almost seamless interoperability betweeen Nim and Python; so I’m building a small proof of concept on this github project. first of all the Nim code: # file: demo.nim - file name should match the module name you're going to import from python import nimpy import unicode proc greet(name: string): string {.exportpy.} = return "Hello, " & name & "!" proc count(names: seq[string]): int {.exportpy.} = return names.len proc lowercase(names: seq[string]): seq[string] {.exportpy.} = for n in names: result.add tolower(n) ...

December 5, 2020 · Andrea Manzini

wrapping c plus plus classes in Python

This is a quick and dirty way to interface C++ code with Python, translating one or more C++ classes in Python objects. First, we need some c++ sample code: //myclass.h #ifndef MYCLASS_H #define MYCLASS_H #include <string> using namespace std; namespace pets { class Dog { public: Dog(string name, int age); virtual ~Dog(); string talk(); protected: string m_name; int m_age; }; } //myclass.cpp #include "myclass.h" #include <string> namespace pets { Dog::Dog(std::string name, int age): m_name(name),m_age(age) { } Dog::~Dog() { } std::string Dog::talk() { return "BARK! I am a DOG and my name is "+m_name; } } now, we can try a little test program just to exercise our class: ...

August 8, 2016 · Andrea Manzini

serata introduttiva al FabLab sulla programmazione Python

Pubblico qui le slide che ho usato durante la serata dedicata alla programmazione Python, svoltasi presso il FabLab Verona http://ilmanzo.github.io/files/slide_serata_python_fablab_2015.html

November 19, 2015 · Andrea Manzini

Linux: get simple I/O statistics per process

I had a trouble with a long process running and wish to know how much I/O this process is doing, so I wrote this quick and dirty python 2.x script: import time,sys,datetime def read_stat(pid): f=open("/proc/%s/io" % pid ,"r") for line in f: if line.startswith('rchar'): rchar=line.split(':')[1] continue if line.startswith('wchar'): wchar=line.split(':')[1] continue f.close() return int(rchar),int(wchar) pid=sys.argv[1] r0,w0 = read_stat(pid) while 1: time.sleep(1) r1,w1 = read_stat(pid) print "%s\t\tr=%s\t\tw=%s" % (datetime.datetime.now().time(),r1-r0,w1-w0) r0,w0=r1,w1 You must give the process PID number as input to the script. In the output you get the read/write throughput of the process in bytes per second, for instance: ...

August 22, 2014 · Andrea Manzini