Today I Learned

Series of notes that's somewhere between a few sentences and a few paragraphs. It's a place for me to write down things I've learned and want to remember. It's also a place to humiliate myself by showing experience doesn't equate knowledge and that there are always holes to fill.

Golang binaries in Python

Dawid Laszuk published on

To use golang binaries in Python, one can do that by first converting golang programs into shared libraries and then calling them from Python. This can be done by using the cgo package in golang. The following is an example of how to do this:

package main

import "C"

//export Add
func Add(a, b int) int {
    return a + b
}

func main() {}

The above code can be compiled into a shared library by running the following command:

go build -buildmode=c-shared -o libadd.so add.go

The shared library can then be called from Python by using the ctypes package. The following is an example of how to do this:

from ctypes import cdll

lib = cdll.LoadLibrary('./libadd.so')
lib.Add.argtypes = [ctypes.c_int, ctypes.c_int]
lib.Add.restype = ctypes.c_int

print(lib.Add(1, 2))

Boto3 for GovCloud

Dawid Laszuk published on

When working with AWS in GovCloud, it turned out that the boto3 library is not configured to work with GovCloud by default. This is because the GovCloud region is not included in the default list of regions. To fix this, you can add the GovCloud region to the list of regions in the boto3 library. This can be done by adding the following code to your Python script:

import boto3
from botocore.config import Config

config = Config(
    region_name = 'us-gov-west-1',
    signature_version = 'v4',
    retries = {
        'max_attempts': 10,
        'mode': 'standard'
    }
)

client = boto3.client('s3', config=config)