Coder Social home page Coder Social logo

extending-python-with-c's Introduction

Extending Python with C

black

Description

This repository contains a examples revealing how to extend Python with C code. It's inspired by a Medium article from Matthias Bitzer and the Python Documentation.

Usage

If you want to run the modules in Python, you can install them using the provided setup.py. I recommend creating a virtual environment before installing them.

Unix

$ python -m venv venv
$ source venv/bin/activate

Windows

$ python -m venv venv
$ venv\bin\activate

Installation

The following command will install the modules in the site-packages directory of your current environment.

$ python setup.py install

You can also only build the modules using:

$ python setup.py build

This creates a new directory called build. Change your working directory to the one containing the .so libraries (.dll under Windows). Creating a Python session in this directory gives you access to the modules even though they are not installed.

Docker

If you want to test the modules in a Docker container to not mess up your own environment, you can run the following commands:

$ docker image build -t cmodules .
$ docker container run --rm --name cmodules -it cmodules

After running the container a Python REPL is started were you can import the necessary modules. An example is given below.

Python 3.7.3 (default, Mar 27 2019, 23:40:30) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ccodemath import factorial
>>> factorial(6)
720
>>> factorial("6")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)
>>> quit()

Available Modules

Module Name Description
ccodemath A collection of mathematical functions

extending-python-with-c's People

Contributors

dahlitzflorian avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.