Code Monkey home page Code Monkey logo

go-python's Introduction

Go high-level bindings for the CPython-3 C-API

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

Important

Currently supports python-3.11 only.

The package provides a higher level for interacting with Python-3 C-API without directly using the PyObject.

Main goals:

  • Provide generic a Object that interact with Golang types.
  • Automatically marshal and unmarshal Golang types to Python types.

Prerequisites

  • Go >= 1.22
  • Python = 3.11

Install

go get go.nhat.io/python/v3

Examples

package main

import (
    "fmt"

    python3 "go.nhat.io/python/v3"
)

func main() {
    sys := python3.MustImportModule("sys")
    version := sys.GetAttr("version_info")

    pyMajor := version.GetAttr("major")
    defer pyMajor.DecRef()

    pyMinor := version.GetAttr("minor")
    defer pyMinor.DecRef()

    pyReleaseLevel := version.GetAttr("releaselevel")
    defer pyReleaseLevel.DecRef()

    major := python3.AsInt(pyMajor)
    minor := python3.AsInt(pyMinor)
    releaseLevel := python3.AsString(pyReleaseLevel)

    fmt.Println("major:", major)
    fmt.Println("minor:", minor)
    fmt.Println("release level:", releaseLevel)

    // Output:
    // major: 3
    // minor: 11
    // release level: final
}
package main

import (
    "fmt"

    python3 "go.nhat.io/python/v3"
)

func main() { //nolint: govet
    math := python3.MustImportModule("math")

    pyResult := math.CallMethodArgs("sqrt", 4)
    defer pyResult.DecRef()

    result := python3.AsFloat64(pyResult)

    fmt.Printf("sqrt(4) = %.2f\n", result)

    // Output:
    // sqrt(4) = 2.00
}

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

go-python's People

Contributors

dependabot[bot] avatar nhatthm avatar

Stargazers

 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.