Py学习  »  Python

在类中使用Python Click

sean • 5 年前 • 1296 次点击  

click 结束 argparse ,但我在使用 点击 . 我的代码如下(减去几个额外的函数):

load_deck() 需要阅读 file 争论。我试着做的方式是通过 这个 文件 main()

最后,运行此代码说明 Flashcards().main() 打电话, 缺少位置参数self。我想这是我使用的方式的问题 点击

import os
import random
import sys

import click
import keyboard
import ruamel.yaml


class Flashcards:
    """
    pass
    """

    def __init__(self):
        self.deck = ruamel.yaml.YAML()
        self.card = ['key', 'value']

    def load_deck(self, file):
        """
        pass
        """
        with open(file, mode='r') as deck:
            self.deck = self.deck.load(deck)

    @click.command()
    @click.option('-f', '--file', default='aws.yaml', help='specifies yaml file to use')
    @click.option('-r', '--reverse', default=False, help='displays values prompting user to guess keys')
    def main(self, file, reverse):
        """
        pass
        """
        self.load_deck(file)
        if reverse is True:
            self.deck = [
                [card[1], card[0]
                 ] for card in self.deck]

        os.system('clear')
        print('Press [SPACEBAR] to advance. Exit at anytime with [CTRL] + [C]\n'
              'There are {} cards in your deck.\n'.format(len(self.deck)))
        try:
            while True:
                self.read_card()
                self.flip_card()

        except KeyboardInterrupt:
            # removes '^C' from terminal output
            os.system('clear')
            sys.exit(0)


if __name__ == "__main__":
    Flashcards().main()

程序读取以下格式的yaml文件(以前是一个西班牙flashcard应用程序):

bajar: to descend
borrar: to erase
contestar: to answer
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53776
 
1296 次点击  
文章 [ 1 ]  |  最新文章 5 年前