Python小玩具:用Github来背单词的实现方法

前一阵刷 GSoC 然后在 Github 上攒了一些小绿块... 这段时间忙于刷英语考试就比较没有在开源社区工作了,感觉让绿块断了有点不爽。。某天突发奇想:要是用 Github 来背单词会怎么样?

每天从服务器上自动 push 一定数量的单词到 Github 里面,然后就拿 Github 来背单词(顺便打卡) [逃 求叉友帮忙改善小玩具,吐槽也是好的哈哈哈

Repo: https://github.com/huangyz0918/words-flashcard


Python小玩具:用Github来背单词的实现方法

5 回复

为了刷而刷?没意义

我觉得 Github 的每次 PUSH 都应该有意义


我理解你想用GitHub的机制来辅助背单词,这个想法挺有意思的。核心思路是利用Git的提交历史来记录和复习单词,比如把单词库做成一个仓库,每次学习相当于一次提交。

下面是一个简单的实现示例,它会创建一个单词本文件,并模拟学习(提交)的过程:

import subprocess
import os
from datetime import datetime

class GitHubWordReciter:
    def __init__(self, repo_path):
        self.repo_path = repo_path
        self.word_file = os.path.join(repo_path, "words.txt")
        os.makedirs(repo_path, exist_ok=True)
        
        # 初始化Git仓库(如果不存在)
        if not os.path.exists(os.path.join(repo_path, ".git")):
            subprocess.run(["git", "init"], cwd=repo_path, capture_output=True)
    
    def add_word(self, word, meaning):
        """添加新单词到单词本"""
        with open(self.word_file, "a", encoding="utf-8") as f:
            timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            f.write(f"{timestamp} | {word} - {meaning}\n")
        
        # Git操作
        subprocess.run(["git", "add", "."], cwd=self.repo_path, capture_output=True)
        commit_msg = f"Learn: {word}"
        subprocess.run(["git", "commit", "-m", commit_msg], cwd=self.repo_path, capture_output=True)
        
        print(f"✓ 已学习: {word}")
    
    def review(self):
        """查看学习历史"""
        result = subprocess.run(
            ["git", "log", "--oneline", "--", "words.txt"],
            cwd=self.repo_path,
            capture_output=True,
            text=True
        )
        
        if result.stdout:
            print("学习记录:")
            print(result.stdout)
        else:
            print("还没有学习记录")

# 使用示例
if __name__ == "__main__":
    # 创建单词本仓库
    reciter = GitHubWordReciter("./my_vocabulary")
    
    # 添加单词
    reciter.add_word("serendipity", "意外发现珍奇事物的本领")
    reciter.add_word("ephemeral", "短暂的")
    
    # 查看学习历史
    reciter.review()

这个脚本做了几件事:

  1. 在指定目录初始化Git仓库
  2. 每次调用add_word()会:
    • 将单词和释义追加到words.txt
    • 执行git addgit commit,提交信息包含单词
  3. review()方法用git log查看学习历史

你可以扩展这个基础版本:

  • 添加分类(不同文件对应不同词库)
  • 实现复习提醒(用Git历史时间判断)
  • 添加随机测试功能
  • 推送到真正的GitHub仓库实现多设备同步

要真正用起来,你还需要设置GitHub远程仓库并定期push。不过注意别把API密钥之类的敏感信息传上去。

总结:用Git版本控制来管理学习进度是个巧妙的思路。

更大程度上像是在刷提交记录
如果是为了背单词,倒不如写脚本发邮箱,还可以收到邮箱通知

用 github 把每天吃了啥记录一下

浪费 github 资源

回到顶部