V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
intohole
V2EX  ›  程序员

几行 Python 代码,清理 fork 的工程

  •  
  •   intohole · 2017-12-21 15:11:38 +08:00 · 2961 次点击
    这是一个创建于 2315 天前的主题,其中的信息可能已经有所发展或是发生改变。

    因为今天想整理自己的 github,想把过去 fork 的代码删除掉,只保留自己的,自己手动删除了几个,但发现自己 fork 的工程太多,手动太麻烦,所以,找到 github api 写了如下几行代码

    sudo pip PyGithub

    #coding=utf-8
    
    from github import Github
    
    g = Github("xxxxx", "passwd")
    my_forks = []
    for repo in g.get_user().get_repos():
        if repo.fork:
            my_forks.append(repo)
    
    for repo in my_forks:
        repo.delete()
    
    10 条回复    2017-12-22 11:15:55 +08:00
    yichinzhu
        1
    yichinzhu  
       2017-12-21 16:30:46 +08:00 via Android
    所以为什么要 fork 而不是 star
    yuhanle
        2
    yuhanle  
       2017-12-21 16:35:26 +08:00
    手动点赞比心
    intohole
        3
    intohole  
    OP
       2017-12-21 17:03:34 +08:00
    @yuhanle 这个代码没什么营养 但是感觉省事
    intohole
        4
    intohole  
    OP
       2017-12-21 17:04:07 +08:00
    @yichinzhu 因为关注了 400+项目,一般看到很好的项目 想慢慢研究才会 fork
    tofishes
        5
    tofishes  
       2017-12-21 17:39:36 +08:00
    可以让 fork 转 star 么?
    intohole
        6
    intohole  
    OP
       2017-12-21 18:36:20 +08:00
    @tofishes 应该可以
    lihongjie0209
        7
    lihongjie0209  
       2017-12-21 22:39:09 +08:00
    ```
    #coding=utf-8

    from github import Github

    g = Github("xxxxx", "passwd")

    for repo in g.get_user().get_repos():
    if repo.fork:
    my_forks.append(repo)
    # 直接删就好了, 不需要中间数组
    repo.delete()

    ```
    intohole
        8
    intohole  
    OP
       2017-12-22 10:36:01 +08:00
    @lihongjie0209 .... 居然纠结这个
    corningsun
        9
    corningsun  
       2017-12-22 11:14:19 +08:00
    @lihongjie0209 @intohole

    刚想评论,发现已经有回复了。

    必须纠结,看着就难受呀!
    lihongjie0209
        10
    lihongjie0209  
       2017-12-22 11:15:55 +08:00
    @intohole #8 看着难受, 要是用中间数组还不如用`yield`, 或者用列表解析式, 或者中 filter, map, 写 Java 考虑设计模式, 写 Python 考虑 PEP8
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1145 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:51 · PVG 06:51 · LAX 15:51 · JFK 18:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.