def note_iter(total_notes, notes_per_user, course_ids):
    """
    Return an iterable of random notes data of length `total_notes`.

    Arguments:
        total_notes (int): total number of notes models to yield
        notes_per_user (int): number of notes to attribute to any one user
        course_ids (list): list of course_id strings to which notes will be
            randomly attributed

    Returns:
        generator: An iterable of note models.
    """
    DATA_DIRECTORY = os.path.join(os.path.dirname(__file__), "data/")
    with open(os.path.join(DATA_DIRECTORY, 'basic_words.txt')) as f:
        notes_text = [word for line in f for word in line.split()]

    def weighted_get_words(weighted_num_words):
        """
        Return random words of of a length of weighted probability.
        `weighted_num_words` should look like [(word_count, weight), (word_count, weight) ...]
        """
        return random.sample(
            notes_text,
            random.choice([
                word_count for word_count, weight in weighted_num_words
                for i in range(weight)
            ]))

    get_new_user_id = lambda: uuid.uuid4().hex
    user_id = get_new_user_id()

    for note_count in range(total_notes):
        if note_count % notes_per_user == 0:
            user_id = get_new_user_id()
        # Notice that quote and ranges are arbitrary
        yield Note(user_id=user_id,
                   course_id=random.choice(course_ids),
                   usage_id=uuid.uuid4().hex,
                   quote='foo bar baz',
                   text=' '.join(
                       weighted_get_words([(10, 5), (25, 3), (100, 2)])),
                   ranges=json.dumps([{
                       "start": "/div[1]/p[1]",
                       "end": "/div[1]/p[1]",
                       "startOffset": 0,
                       "endOffset": 6
                   }]),
                   tags=json.dumps(
                       weighted_get_words([(1, 40), (2, 30), (5, 15), (10, 10),
                                           (15, 5)])))
예제 #2
0
 def test_create_invalid_note(self):
     note = Note()
     for empty_type in (None, '', []):
         with self.assertRaises(ValidationError):
             note.create(empty_type)