guanpengchn / guanpengchn.github.io

:memo: code on DEV branch, blogs on ISSUES
https://guanpengchn.github.io
18 stars 8 forks source link

golang调用python #15

Open guanpengchn opened 6 years ago

guanpengchn commented 6 years ago

golang调用python方案列表

go-python

执行

go get github.com/sbinet/go-python
brew install pkg-config
# pkg-config --cflags python-2.7
Package python-2.7 was not found in the pkg-config search path.
Perhaps you should add the directory containing `python-2.7.pc'
to the PKG_CONFIG_PATH environment variable
No package 'python-2.7' found
pkg-config: exit status 1

解决方案

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/pkgconfig

qiniu/py

google的grumpy

http服务

命令行调用

package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type ConfigStruct struct {
    Host              string   `json:"host"`
    Port              int      `json:"port"`
    AnalyticsFile     string   `json:"analytics_file"`
    StaticFileVersion int      `json:"static_file_version"`
    StaticDir         string   `json:"static_dir"`
    TemplatesDir      string   `json:"templates_dir"`
    SerTcpSocketHost  string   `json:"serTcpSocketHost"`
    SerTcpSocketPort  int      `json:"serTcpSocketPort"`
    Fruits            []string `json:"fruits"`
}

type Other struct {
    SerTcpSocketHost string   `json:"serTcpSocketHost"`
    SerTcpSocketPort int      `json:"serTcpSocketPort"`
    Fruits           []string `json:"fruits"`
}

func main() { 
    jsonStr := `{"host": "http://localhost:9090","port": 9090,"analytics_file": "","static_file_version": 1,"static_dir": "E:/Project/goTest/src/","templates_dir": "E:/Project/goTest/src/templates/","serTcpSocketHost": ":12340","serTcpSocketPort": 12340,"fruits": ["apple", "peach"]}`

    //json str 转map
    var dat map[string]interface{}
    if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil {
        fmt.Println("==============json str 转map=======================")
        fmt.Println(dat)
        fmt.Println(dat["host"])
    }

    //json str 转struct
    var config ConfigStruct
    if err := json.Unmarshal([]byte(jsonStr), &config); err == nil {
        fmt.Println("================json str 转struct==")
        fmt.Println(config)
        fmt.Println(config.Host)
    }

    //json str 转struct(部份字段)
    var part Other
    if err := json.Unmarshal([]byte(jsonStr), &part); err == nil {
        fmt.Println("================json str 转struct==")
        fmt.Println(part)
        fmt.Println(part.SerTcpSocketPort)
    }

    //struct 到json str
    if b, err := json.Marshal(config); err == nil {
        fmt.Println("================struct 到json str==")
        fmt.Println(string(b))
    }

    //map 到json str
    fmt.Println("================map 到json str=====================")
    enc := json.NewEncoder(os.Stdout)
    enc.Encode(dat)

    //array 到 json str
    arr := []string{"hello", "apple", "python", "golang", "base", "peach", "pear"}
    lang, err := json.Marshal(arr)
    if err == nil {
        fmt.Println("================array 到 json str==")
        fmt.Println(string(lang))
    }

    //json 到 []string
    var wo []string
    if err := json.Unmarshal(lang, &wo); err == nil {
        fmt.Println("================json 到 []string==")
        fmt.Println(wo)
    }
}
python modelchain_lr.py --p_batch_size=128 --p_epochs=20 --p_max_rounds=200 --p_verbosity=1 --p_inc_cutoff=-0.01 --p_p_cutoff=0.25 --p_rb_tolerance=16

# setup channel
peer channel create -o orderer.chainplaza.com:7050 -c chainplaza -f /etc/chainplaza/conf/material/chainplaza.tx 

# join channel
CORE_PEER_MSPCONFIGPATH=/etc/chainplaza/conf/crypto/peerOrganizations/org1.chainplaza.com/users/Admin@org1.chainplaza.com/msp
CORE_PEER_ADDRESS=peer2.org1.chainplaza.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
peer channel join -b chainplaza.block

# install and instantiate modelchain
peer chaincode install -p github.com/chainplaza/chaincode/test -n modelchain -v 0
peer chaincode instantiate -n modelchain -v 0 -c '{"Args":[]}' -C chainplaza

# invoke modelchain
peer chaincode invoke -n modelchain -c '{"Args":["modelchain"]}' -C chainplaza

Install anaconda

cd /tmp
curl -O https://repo.anaconda.com/archive/Anaconda2-5.2.0-Linux-x86_64.sh
chmod +x Anaconda2-5.2.0-Linux-x86_64.sh
./Anaconda2-5.2.0-Linux-x86_64.sh
source /root/.bashrc
cd -
pip install requirements.txt
panchengtao commented 6 years ago

我也总结了下,可以看看 https://github.com/wddpct/articles/issues/11

cobolbaby commented 5 years ago

针对提到的"grumpy对于机器学习相关包的支持不足",有无具体的案例呢?@guanpengchn

guanpengchn commented 5 years ago

@cobolbaby 我也记不清了,只记得当时使用了keras,然后有报错