def __set__(self, instance, value): if not isinstance(value, str): err_msg = f'{self.attr_name}: {value!r} must be string.' raise TypeError(err_msg) value = normalize_value(value) super().__set__(instance, value)
def __set__(self, instance, value): if not isinstance(value, (str, type(None))): err_msg = f'{self.attr_name}: {value!r} must be string or None.' raise TypeError(err_msg) if value is not None: value = normalize_value(value) super().__set__(instance, value)
def __set__(self, instance, value): if not isinstance(value, (list, type(None))): err_msg = f'{self.attr_name}: {value!r} must be list or None.' raise TypeError(err_msg) if value is not None: for i in range(len(value)): value[i] = normalize_value(value[i]) super().__set__(instance, value)
async def input_source(self, pre_fill: str = None): source_tags = self.db_session.get_source_tags( user_id=self.get_user_id()) source = await self.input( 'Source', pre_fill=(pre_fill or (source_tags[0] if source_tags else '')), history=source_tags) source = normalize_value(value=source, remove_trailing='.') source = source.capitalize() + '.' return source
def __init__(self, database, *, flashcard_type, setup_db=False): self.flashcard_type = normalize_value(flashcard_type, to_lower=True) self.db_conn = sqlite3.connect(database, detect_types=sqlite3.PARSE_DECLTYPES) self.db_conn.row_factory = sqlite3.Row self.db_cursor = self.db_conn.cursor() if setup_db: self.setup_users_table() self.setup_flashcards_table() self.setup_flashcard_example_table() self.setup_flashcard_review_history_table() self.setup_full_text_search()
def test_normalize_value(self): self.assertEqual(normalize_value(' Good morning , world !'), 'Good morning, world!') self.assertEqual(normalize_value(' Do not worry .'), 'Do not worry.') self.assertEqual( normalize_value(' Do not worry .', remove_trailing='.'), 'Do not worry') self.assertEqual( normalize_value(' Do not worry .', remove_trailing='.', to_lower=True), 'do not worry') self.assertEqual(normalize_value(' Hello ? ! '), 'Hello?!') self.assertEqual( normalize_value(' HELLO, WORLD ? ! ', to_lower=True), 'hello, world?!') self.assertEqual( normalize_value(' Hello ? ! ', remove_trailing='?!'), 'Hello') with self.assertRaises(TypeError): normalize_value(' HELLO, ! ', '!', to_lower=True),
def get_source_tags(self, user_id): query = self.db_cursor.execute( 'SELECT distinct(source) as tag ' 'FROM flashcards ' 'WHERE user_id = :user_id and flashcard_type = :flashcard_type ' 'ORDER BY created desc;', { 'user_id': user_id, 'flashcard_type': self.flashcard_type }) source_tags = [] for row in query: tag = (row['tag'] or '').strip() if tag: tag = normalize_value(value=tag, remove_trailing='.') source_tags.append(tag.capitalize() + '.') source_tags.sort(reverse=True) return source_tags