Ejemplo n.º 1
0
 def _test_create_advanced_deck(self):
     """Create a new empty deck, with advanced options."""
     deck = Deck(
         'Test advanced Deck',
         self.tinycards.user_id,
         # Only test knowledge with back side of cards.
         blacklisted_side_indices=[0],
         # Only test knowledge with questions which do not require any
         # typing.
         blacklisted_question_types=NO_TYPING,
         # Stricter evaluation of answers.
         grading_modes=NO_TYPOS,
         # Text-to-speech for both front (English) and back (Japanese)
         # sides.
         tts_languages=['en', 'ja'],
     )
     deck = self.tinycards.create_deck(deck)
     self._assert_advanced_options_are_set(deck)
     # Add a few tests cards and update the deck, in order to test PATCH
     # with an application/json content-type:
     deck.add_card(('one', 'いち'))
     deck.add_card(('two', 'に'))
     deck = self.tinycards.update_deck(deck)
     self._assert_advanced_options_are_set(deck)
     # Set a cover on the deck and update it, in order to test PATCH with a
     # multipart-form content-type:
     deck.cover = path_to('test_logo_blue.jpg')
     deck = self.tinycards.update_deck(deck)
     self._assert_advanced_options_are_set(deck)
     self._delete_deck(deck.id)  # Clean up after ourselves.
def add(pair):
    deck = client.find_deck_by_title(os.environ['TC_CURRENT_DECK'])
    if deck is None:
        deck = Deck(os.environ['TC_CURRENT_DECK'])
        deck = client.create_deck(deck)

    if not deck_has_duplicates(deck, pair[0]):
        deck.add_card(pair)
        client.update_deck(deck)
Ejemplo n.º 3
0
    def test_cards_to_csv(self):
        test_deck = Deck('Test Deck')
        test_deck.add_card(('front word', 'back word'))
        file_buffer = StringIO()

        test_deck.save_cards_to_csv(file_buffer)

        # Excel-generated CSV files use Windows-style line terminator.
        line_terminator = '\r\n'
        expected_output = 'front,back' + line_terminator \
                          + 'front word,back word' + line_terminator
        self.assertEqual(expected_output, file_buffer.getvalue())
 def _test_create_deck_with_cover_from_url(self):
     """Create a new empty deck, with a cover using an image available online."""
     url = 'https://d9np3dj86nsu2.cloudfront.net/thumb/5bd5092200f7fe41e1d926158b5e8243/350_403'
     deck = Deck('Test Deck with cover', cover=url)
     deck = self.tinycards.create_deck(deck)
     self.assertTrue(isinstance(deck, Deck))
     self._assert_cover_was_updated_with_url(url, deck.image_url)
     self._assert_cover_was_updated_with_url(url, deck.cover_image_url)
     # Add a few tests cards (to pass server-side validation) & update the deck's cover:
     deck.add_card(('front test 1', 'back test 1'))
     deck.add_card(('front test 2', 'back test 2'))
     url = 'https://d9np3dj86nsu2.cloudfront.net/thumb/8aaa075410df4c562bdd6c42659f02e2/350_403'
     deck.cover = url
     deck = self.tinycards.update_deck(deck)
     self.assertTrue(isinstance(deck, Deck))
     self._assert_cover_was_updated_with_url(url, deck.image_url)
     self._assert_cover_was_updated_with_url(url, deck.cover_image_url)
     self._delete_deck(deck.id)  # Clean up after ourselves.
 def _test_create_deck_with_cover_from_file(self):
     """Create a new empty deck, with a cover using a local file."""
     blue_cover_filepath = path_to('test_logo_blue.jpg')
     deck = Deck('Test Deck with cover', cover=blue_cover_filepath)
     deck = self.tinycards.create_deck(deck)
     self.assertTrue(isinstance(deck, Deck))
     self._assert_cover_was_updated_with_file(blue_cover_filepath, deck.image_url)
     self._assert_cover_was_updated_with_file(blue_cover_filepath, deck.cover_image_url)
     # Add a few tests cards (to pass server-side validation) & update the deck's cover:
     deck.add_card(('front test 1', 'back test 1'))
     deck.add_card(('front test 2', 'back test 2'))
     red_cover_filepath = path_to('test_logo_red.png')
     deck.cover = red_cover_filepath
     deck = self.tinycards.update_deck(deck)
     self.assertTrue(isinstance(deck, Deck))
     self._assert_cover_was_updated_with_file(red_cover_filepath, deck.image_url)
     self._assert_cover_was_updated_with_file(red_cover_filepath, deck.cover_image_url)
     self._delete_deck(deck.id)  # Clean up after ourselves.
Ejemplo n.º 6
0
def csv_to_deck(csv_path):
    """Creates a Tinycards deck from a CSV file.

    The CSV file is expected to have two columns named 'front' and 'back'.
    """
    # Create new deck.
    tinycards = Tinycards(user_identifier, user_password)
    deck = Deck('French Words')
    deck = tinycards.create_deck(deck)

    # Extract data from CSV file.
    word_pairs = []
    with open(csv_path, 'r') as csv_file:
        csv_reader = csv.DictReader(csv_file)
        for row in csv_reader:
            current_word_pair = (row['front'], row['back'])
            word_pairs.append(current_word_pair)

    # Populate deck with cards from CSV data.
    for pair in word_pairs:
        deck.add_card(pair)

    # Save changes to Tinycards.
    tinycards.update_deck(deck)
from tinycards import Tinycards
from tinycards.model import Deck
from PyDictionary import PyDictionary
import yaml, json, sys
import config

dictionary = PyDictionary()
tinycards = Tinycards(config.TINY_CARDS_CREDENTIALS['email'],
                      config.TINY_CARDS_CREDENTIALS['password'])
deck = Deck('12 Rules Of Life')
deck = tinycards.create_deck(deck)
fh = open('words.txt')
for word in fh:
    meaning = str(dictionary.meaning(word))
    translation_table = dict.fromkeys(map(ord, '{}[]\''), None)
    meaning = meaning.translate(translation_table)
    print(meaning)
    deck.add_card((word, meaning))
fh.close()

tinycards.update_deck(deck)
Ejemplo n.º 8
0
def csv_to_deck(csv_path, deck_base_name):
    """
    Creates a Tinycards deck from a CSV file.
    The CSV file is expected to have two columns
    """
    # Extract data from CSV file.
    word_pairs = []
    pairs_amount = 0
    with open(csv_path, 'r') as csv_file:
        csv_reader = csv.reader(csv_file)
        for row in csv_reader:
            word_pairs.append((row[1], row[0]))
        pairs_amount = len(word_pairs)

    if not DEBUG:
        client_tinycards = Tinycards(user_identifier, user_password)

    if pairs_amount == 0:
        print('No word pairs were found in CSV')
        sys.exit()

    decks = math.ceil(pairs_amount / DECK_CARDS_PER_UNIT)
    for index, deck in enumerate(range(decks)):
        # Get deck by name. If doesn't exist - create new
        subdeck_name = f'{deck_base_name} - {index}'
        if not DEBUG:
            deck = client_tinycards.find_deck_by_title(subdeck_name)
            if not deck:
                deck = Deck(subdeck_name)
                deck = client_tinycards.create_deck(deck)
            elif DECK_REMOVE_EXISTING:
                deck.cards = []

        # Customize deck
        deck.cover = DECK_COVER_PATH
        deck.description = (
            f'{DECK_DESC}. '
            f'Last updated on {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}')
        deck.tts_languages = DECK_TTS_LANGUAGES

        # Populate deck with cards from CSV data
        for pair in range(DECK_CARDS_PER_UNIT):
            try:
                deck.add_card(word_pairs.pop())
            except IndexError:
                pass

        # Save changes to Tinycards
        if not DEBUG:
            client_tinycards.update_deck(deck)
            print(f'Successfully created "{subdeck_name}"')

    if not DEBUG:
        print(
            f'Successfully created {decks} decks from {pairs_amount} word pairs'
        )
    else:
        print('Dry run. No Tinycard decks were created')
        print(
            f'{decks} decks from {pairs_amount} word pairs are ready to be uploaded'
        )