Last active
October 3, 2020 08:10
-
-
Save igorzakhar/aaa092ae11d2085bac2f7de0e13b5753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import redis | |
def upload_questions_into_redis(text, redis_conn): | |
question, answer = None, None | |
counter = 1 | |
for contents in text: | |
if contents.strip().startswith('Вопрос'): | |
question = ' '.join(contents.strip().splitlines()[1:]) | |
redis_conn.hset(f'question_{counter}', 'question', question) | |
continue | |
if contents.strip().startswith('Ответ'): | |
answer = ' '.join(contents.strip().splitlines()[1:]) | |
redis_conn.hset(f'question_{counter}', 'answer', answer) | |
counter += 1 | |
def read_files(directory='quiz-questions'): | |
for file in os.listdir(os.path.abspath(directory)): | |
with open(f'{directory}/{file}', 'r', encoding='KOI8-R') as file: | |
text = file.read().split('\n\n') | |
yield text | |
def main(): | |
redis_conn = redis.Redis() | |
for text in read_files(): | |
upload_questions_into_redis(text, redis_conn) | |
if __name__ == '__main__': | |
main() | |
''' | |
conn.hset('user_vk_94719491', 'last_asked_question', 'question_8291') | |
conn.hset('user_vk_94719491', 'correct_answers', 0) | |
conn.hset('user_vk_94719491', 'wrong_answers', 0) | |
conn.hget('user_vk_94719491', 'last_asked_question') | |
conn.hget('user_vk_94719491', 'correct_answer') | |
conn.hincrby('user_vk_94719491', 'correct_answer') | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment