++wythern++

X presents Y for a better Z

2018年8月14日 #

go test 用args带参数

测试中想通过命令行传递一些参数给test func,网上找了一些资料但过程不是很顺利,这里记录一下。
首先go test有一个-args的参数说可以达到这个目的,但实测下来发现有没有没区别。。。
google查到的大部分也是用到了flag类型。
flag.go的注释写的比较清楚
/*
    Package flag implements command-line flag parsing.

    Usage:

    Define flags using flag.String(), Bool(), Int(), etc.

    This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
        import "flag"
        var ip = flag.Int("flagname", 1234, "help message for flagname")
    If you like, you can bind the flag to a variable using the Var() functions.
        var flagvar int
        func init() {
            flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
        }
    Or you can create custom flags that satisfy the Value interface (with
    pointer receivers) and couple them to flag parsing by
        flag.Var(&flagVal, "name", "help message for flagname")
    For such flags, the default value is just the initial value of the variable.

    After all flags are defined, call
        flag.Parse()
    to parse the command line into the defined flags.

*/
因此需要做的事情就是:
1. 定义flag,这个需要在main()执行之前完成,我这里在test文件里面用全局变量完成,但a可以放在函数里面。
var (
    
// Define global args flags.
    pA = flag.Int("a"0,  "a.")
    a 
= 0
)
2. parse flag,这个要在test func执行之前,所以可以考虑加入一个init()在test文件里。
func init() {
    flag.Parse()
    a 
= *pA
}
后面使用这些变量就没有问题了,比如这样
func TestInit(t *testing.T) {
    flag.Parse()
    t.Log(
"a = ", a)
}
这里用到的主要是flag的功能,测试用发现有没有-args问题不大,所以这个用法可能不是很符合go test的要求,先用起来再说了。
REF
1. https://www.golangtc.com/t/584cbd16b09ecc2e1800000b
2. https://stackoverflow.com/.../process-command-line-arguments-in-go-test
3. https://hsulei.com/2017/08/23/gotest如何使用自定义参数/

posted @ 2018-08-14 17:04 wythern 阅读(82) | 评论 (0)编辑 收藏

[转]shell中if条件字符串、数字比对,[[ ]]和[ ]区别

原文在此

学习shell的时候总是被shell里的条件判断方式搞得头疼,经常不知道改 用[],[[]],(())还是test,let,而很少有书把它们的关系讲解的很清楚(应该是我悟性差或是看书太少),今天总结一下,基础的东西如它们 的使用方法不再赘述,重点说说它们的区别的使用时应该注意的地方。

先说[]和test,两者是一样的,在命令行里test expr和[ expr ]的效果相同。test的三个基本作用是判断文件、判断字符串、判断整数。支持使用与或非将表达式连接起来。要注意的有:

1.test中可用的比较运算符只有==和!=,两者都是用于字符串比较的,不可用于整数比较,整数比较只能使用-eq, -gt这种形式。无论是字符串比较还是整数比较都千万不要使用大于号小于号。当然,如果你实在想用也是可以的,对于字符串比较可以使用尖括号的转义形式, 如果比较"ab"和"bc":[ ab \< bc ],结果为真,也就是返回状态为0.

然后是[[ ]],这是内置在shell中的一个命令,它就比刚才说的test强大的多了。支持字符串的模式匹配(使用=~操作符时甚至支持shell的正则表达 式)。简直强大的令人发指!逻辑组合可以不使用test的-a,-o而使用&&,||这样更亲切的形式(针对c、Java程序员)。当 然,也不用想的太复杂,基本只要记住
1.字符串比较时可以把右边的作为一个模式(这是右边的字符串不加双引号的情况下。如果右边的字符串加了双引号,则认为是一个文本字符串。),而不仅仅是一个字符串,比如[[ hello == hell? ]],结果为真。

另外要注意的是,使用[]和[[]]的时候不要吝啬空格,每一项两边都要有空格,[[ 1 == 2 ]]的结果为“假”,但[[ 1==2 ]]的结果为“真”!后一种显然是错的


3.最后就是let和(()),两者也是一样的(或者说基本上是一样的,双括号比let稍弱一些)。主要进行算术运算(上面的两个都不行),也比较适合进 行整数比较,可以直接使用熟悉的<,>等比较运算符。可以直接使用变量名如var而不需要$var这样的形式。支持分号隔开的多个表达式

posted @ 2018-08-14 16:44 wythern 阅读(15) | 评论 (0)编辑 收藏

2018年8月1日 #

一张图了解公链和私链

图片来源:https://blog.slock.it/public-vs-private-chain-7b7ca45044f

posted @ 2018-08-01 14:05 wythern 阅读(15) | 评论 (0)编辑 收藏

2018年7月21日 #

Full go tool list (keep constructing)

REF:  https://dominik.honnef.co/posts/2014/12/an_incomplete_list_of_go_tools/

go get github.com/golang/lint/golint
go get github.com/kisielk/errcheck
go get golang.org/x/tools/cmd/benchcmp
go get github.com/cespare/prettybench
go get github.com/ajstarks/svgo/benchviz
go get golang.org/x/tools/cmd/stringer
go get github.com/josharian/impl
go get golang.org/x/tools/cmd/goimports
go get sourcegraph.com/sqs/goreturns
go get code.google.com/p/rog-go/exp/cmd/godef
go get github.com/nsf/gocode
go get golang.org/x/tools/cmd/oracle
go get golang.org/x/tools/cmd/gorename
go get github.com/kisielk/godepgraph

posted @ 2018-07-21 14:09 wythern 阅读(36) | 评论 (0)编辑 收藏

2018年7月14日 #

Some materials about RPC transaction call.

Introduction

Solidiay doc about ABI and contract access.
https://solidity.readthedocs.io/en/develop/abi-spec.html
https://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html?highlight=selfdestruct
http://www.ethdocs.org/en/latest/contracts-and-transactions/accessing-contracts-and-transactions.html
http://ethdocs.org/en/latest/contracts-and-transactions/contracts.html#testing-contracts-and-transactions
https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=suicide [global variables like msg.sender]
ABI intro in Ethereum Wiki.
https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#argument-encoding
https://github.com/ethereum/go-ethereum/wiki/Contract-Tutorial
JAON rpc api of Eth.
https://github.com/ethereum/wiki/wiki/JSON-RPC#json-rpc-api
https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-methods
Deploy a contract using RPC.
https://github.com/rsksmart/rskj/wiki/Deploying-contracts-using-RPC-calls
A tool to generate the grpc server code for a contract
https://github.com/getamis/grpc-contract

How to test.

http://ethereum-tests.readthedocs.io/en/latest/test_types/transaction_tests.html
https://github.com/ethereum/cpp-ethereum/blob/develop/doc/generating_tests.rst
go-ethereum里面有一批vm和contract相关的tests,结合ABI和contract的定义,可以很容易的写一批自己定制的测试,从web3js或者直接在go project里面调用都可以。

posted @ 2018-07-14 17:00 wythern 阅读(32) | 评论 (0)编辑 收藏

2017年6月19日 #

What is shade jar, and what is its purpose.

https://stackoverflow.com/questions/13620281/what-is-the-maven-shade-plugin-used-for-and-why-would-you-want-to-relocate-java


Uber JAR, in short, is a JAR containing everything.

Normally in Maven, we rely on dependency management. An artifact contains only the classes/resources of itself. Maven will be responsible to find out all artifacts (JARs etc) that the project depending on when the project is built.

An uber-jar is something that take all dependencies, and extract the content of the dependencies and put them with the classes/resources of the project itself, in one big JAR. By having such uber-jar, it is easy for execution, because you will need only one big JAR instead of tons of small JARs to run your app. It also ease distribution in some case.

Just a side-note. Avoid using uber-jar as Maven dependency, as it is ruining the dependency resolution feature of Maven. Normally we create uber-jar only for the final artifact for actual deployment or for manual distribution, but not for putting to Maven repository.


Update: I have just discovered I haven't answered one part of the question : "What's the point of renaming the packages of the dependencies?". Here is some brief updates and hopefully will help people having similar question.

Creating uber-jar for ease of deployment is one use case of shade plugin. There are also other common use cases which involve package renaming.

For example, I am developing Foo library, which depends on a specific version (e.g. 1.0) of Bar library. Assuming I cannot make use of other version of Bar lib (because API change, or other technical issues, etc). If I simply declare Bar:1.0 as Foo's dependency in Maven, it is possible to fall into a problem: A Qux project is depending on Foo, and also Bar:2.0 (and it cannot use Bar:1.0 because Qux needs to use new feature in Bar:2.0). Here is the dilemma: should Qux use Bar:1.0 (which Qux's code will not work) or Bar:2.0 (which Foo's code will not work)?

In order to solve this problem, developer of Foo can choose to use shade plugin to rename its usage of Bar, so that all classes in Bar:1.0 jar are embedded in Foo jar, and the package of the embedded Bar classes is changed from com.bar to com.foo.bar. By doing so, Qux can safely depends on Bar:2.0 because now Foo is no longer depending on Bar, and it is using is own copy of "altered" Bar located in another package.

posted @ 2017-06-19 11:23 wythern 阅读(27) | 评论 (0)编辑 收藏

2017年5月18日 #

[Collection] Spark partition related things.

Partition:
Understanding:
1. https://www.dezyre.com/article/how-data-partitioning-in-spark-helps-achieve-more-parallelism/297
2. http://dev.sortable.com/spark-repartition/ -- example of partition & repartition to avoid data-imbalance.
3. https://acadgild.com/blog/partitioning-in-spark/ -- real case on existing partitioner & self-created partitioner.

Programming guidence.
Avoid using GroupByKey https://databricks.gitbooks.io/databricks-spark-knowledge-base/content/best_practices/prefer_reducebykey_over_groupbykey.html

Reference 1 says: Applying transformations that return RDDs with specific partitioners. Some operation on RDDs that hold to and propagate a partitioner are-
  • Join
  • LeftOuterJoin
  • RightOuterJoin
  • groupByKey
  • reduceByKey
  • foldByKey
  • sort
  • partitionBy
  • foldByKey
groupByKey is one of them, My understanding is such operations may cause extra shuffle, but repartition also helps relieve data imbalance if well considered, so use head please! :)

posted @ 2017-05-18 14:29 wythern 阅读(29) | 评论 (0)编辑 收藏

2017年4月17日 #

ubuntu switch window in current workspace.

gsettings set org.gnome.shell.app-switcher current-workspace-only true

[ref]https://unix.stackexchange.com/questions/171964/restrict-alttab-to-the-current-workspace-in-gnome-3

posted @ 2017-04-17 10:21 wythern 阅读(47) | 评论 (0)编辑 收藏

2017年2月22日 #

Smb 连不上的一个可能

在公司整smb,用windows访问linux的share,但是配了很久都访问不了,一个选项一个选项的试,都不行。
偶尔发现一次能进到share folder,但是看起来windows一直在重新load该folder。
没有什么别的办法了,查日志:/var/log/samba/log.$ConnectionName.发现某个lib挂了。

[2017/02/22 17:16:29.389531,  0] ../source3/param/loadparm.c:4412(widelinks_warning)
  Share 'ubuntu-share' has wide links and unix extensions enabled. These parameters are incompatible. Wide links will be disabled for this share.
[2017/02/22 17:16:29.392626,  0] ../source3/lib/popt_common.c:68(popt_s3_talloc_log_fn)
  Bad talloc magic value - unknown value
[2017/02/22 17:16:29.392669,  0] ../source3/lib/util.c:789(smb_panic_s3)
  PANIC (pid 9408): Bad talloc magic value - unknown value
[2017/02/22 17:16:29.393497,  0] ../source3/lib/util.c:900(log_stack_trace)
  BACKTRACE: 28 stack frames:
   #0 /usr/lib/x86_64-linux-gnu/samba/libsmbregistry.so.0(log_stack_trace+0x1a) [0x7f21ca87514a]
   #1 /usr/lib/x86_64-linux-gnu/samba/libsmbregistry.so.0(smb_panic_s3+0x20) [0x7f21ca875220]
   #2 /usr/lib/x86_64-linux-gnu/libsamba-util.so.0(smb_panic+0x2f) [0x7f21cb5ec8df]
   #3 /usr/lib/x86_64-linux-gnu/libtalloc.so.2(_talloc_free+0x4de) [0x7f21c83742fe]
   #4 /usr/lib/x86_64-linux-gnu/libsamba-util.so.0(+0x1246c) [0x7f21cb5e346c]

看log好像是这个talloc lib和本地的smb lib 不match。
重装之 sudo apt-get install libtalloc2
搞定!
BTW: 本机的Samba的版本是Samba 4.3.9-Ubuntu。之前的talloc忘记看版本。。。无所谓了。

这里有人遇到一样的问题:https://answers.launchpad.net/ubuntu/+question/291448

posted @ 2017-02-22 09:46 wythern 阅读(109) | 评论 (0)编辑 收藏

2016年2月14日 #

【笔记】逆序数的求法

做个笔记!
【参考】http://blog.csdn.net/dlengong/article/details/7594919

3种思路:
1. 冒泡法统计交换次数。 O(N*N)
2. MergeSort同时统计。 O(NlogN)
3. 用binary index tree!。 O(NlogN),其实是基于IndexSort,然后用BinIdxTree求和。
   BIT适用的场景是:
   对于某个序列a0, a1, a2, ..., aN.
   BITsum(0, m) [0 <= m <= N] == sum(a0, a1, ..., am).
   和普通的sum不同点在于,当ai发生变化的时候,BIT支持在logN时间内重新算出sum值。
   所以这条求逆序的方式就是indexSort找到当前max value对应的idx, 然后a(idx) = 1,然后BITsum(0, idx)看看前面有多少1,就是当前value的逆序数K, sum(K)就得到了整个序列的逆序数。

posted @ 2016-02-14 21:27 wythern 阅读(86) | 评论 (0)编辑 收藏

仅列出标题  下一页