Skip to content

Instantly share code, notes, and snippets.

View helinwang's full-sized avatar

Helin Wang helinwang

  • Bay Area
View GitHub Profile
@helinwang
helinwang / string.md
Last active March 7, 2024 01:23
How to call go from c with string (char *) as the parameter without making a copy

Here is the gist for showing how to send string from c to go without a copy, for sending an float32 array from c to go without copy, please see here

Below is the documentation for converting between a c string and a go string:

// From https://golang.org/cmd/cgo/
// A few special functions convert between Go and C types by making copies of the data. 
// Go string to C string
// The C string is allocated in the C heap using malloc.
// It is the caller's responsibility to arrange for it to be
// freed, such as by calling C.free (be sure to include stdlib.h
@helinwang
helinwang / unraid_mount.md
Last active February 17, 2024 16:07
Unraid: mount a unraid share using the unraid mount tag in Ubuntu

Mounting the shared path You can mount the shared folder using

mount -t 9p -o trans=virtio [mount tag] [mount point] -oversion=9p2000.L

mount tag: As specified in Qemu commandline. mount point: Path to mount point. trans: Transport method (here virtio for using 9P over virtio) version: Protocol version. By default it is 9p2000.u . Other options that can be used include:

@helinwang
helinwang / byte_slice_to_float32_slice.go
Last active February 6, 2023 02:29
Go byte slice to float32 slice
package main
import (
"fmt"
"unsafe"
)
func byteSliceToFloat32Slice(src []byte) []float32 {
if len(src) == 0 {
return nil
@helinwang
helinwang / gostring.md
Last active January 3, 2023 07:50
Call go (or c) code from python with string as argument and string as return value

go code (foo.go) compiled into a shared library and a c header (foo.h) is generated.

For calling go from c, please see here

Arguments

The code below shows two ways of passing string parameter to go function:

  1. Using GoString structure as argument, without making a copy in go code: no conversion to go string needed.
  2. Using c_char_p as argument, making a copy in go code when converting to go string.

When using the first method without the copy, I don't know how python will do the memory management with the pointer passed into go. So the second method is preferred.

@helinwang
helinwang / tf_feature_columns.py
Last active October 31, 2021 01:27
A survey of TensorFlow feature columns
from tensorflow.keras import layers
import tensorflow as tf
import numpy as np
video_id = tf.feature_column.categorical_column_with_identity(
key="video_id", num_buckets=1000000, default_value=0
)
features = {
"video_id": tf.sparse.from_dense([[2, 85, 0, 0, 0], [33, 78, 2, 73, 1]]),
@helinwang
helinwang / gist:f9ac4d3b8b927bdcd2a2b87a5ee7bb5e
Created December 3, 2020 18:38
Convert TF saved model to TensorBoard summary - works with unknown custom TF op
# Copyright 2019 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
@helinwang
helinwang / init.el
Last active November 3, 2020 04:31
My Rust emacs configuration
;; Make sure to install rust-analyzer binary, otherwise lsp-mode
;; default to using rls instead of rust-analyzer.
(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
(not (gnutls-available-p))))
(proto (if no-ssl "http" "https")))
(when no-ssl
@helinwang
helinwang / tf_decode_proto.py
Last active July 3, 2020 23:28
TensorFlow decode arbitrary Protobuf using tf.io.decode_proto and TFRecordDataset.
import tensorflow as tf
from google.protobuf.descriptor_pb2 import FileDescriptorSet
from google.protobuf.descriptor_pb2 import FileDescriptorProto
import baz_pb2
def decode(x):
proto = FileDescriptorProto()
@helinwang
helinwang / mac_malware_auto_start.txt
Last active April 18, 2019 06:11
Places that mac malware could hide (auto start)
/Library/StartUpItems
/Library/LaunchAgents
/Library/LaunchDaemons
/System/Library/LaunchDaemons
/System/Library/LaunchAgents
/Users/$USER/Library/LaunchAgents
/Library/PrivilegedHelperTools
and "system preferences" -> login items
"keychain access": unkown certificates (except the ones in "System Roots")
@helinwang
helinwang / click.js
Created January 4, 2019 00:36
Batch delete Weibo posts
setInterval(() => {
document.querySelector('a[action-type="feed_list_delete"]').click()
document.querySelector('a[action-type="ok"]').click()
}, 500)