python外部命令传参(笔记)

方法一:命令行的参数是不可或缺的,之前在paddle的代码里看到python的命令行参数的自定义,通过click的import,Click 支持对 command 方法添加自定义的参数,由 option() 和 argument() 装饰器实现。先记下这个笔记,方便未来使用。

方法二:通过sys.argv[],从外部传参,可以从外部取得的多个参数,所以获得的是一个列表(list),也就是说sys.argv其实可以看作是一个列表,所以才能用[]提取其中的元素。其第一个元素是程序本身,随后才依次是外部给予的参数。


## Loading data ......
try:
    edges_file = sys.argv[1]  # enter the path of the edges
    community_file = sys.argv[2]  # enter  the path of the community
    topk=sys.argv[3]               # enter the top depand what you need
except :
    print("Please enter the args when you run it,which has to contain the path of edges and community and the topk,then try again~")


A_mat = load_adj_matrix(edges_file)              #######
gdclus, cluster_dict = load_labels(community_file)          #######
ground_truth_labels = list(gdclus.values())

## Algorithm: Harmonic Modularity (HAM)

猜你喜欢

转载自blog.csdn.net/hensonwells/article/details/81485225