<jupyter_start><jupyter_text>Import a survey file from S3<jupyter_code>!pip install --upgrade pip boto3
import io
import logging
import os

import boto3
import citric
from IPython.display import HTML
citric.__version__<jupyter_output><empty_output><jupyter_text>Set up logging<jupyter_code>formatter = logging.Formatter("{asctime} {levelname} {message}", style="{")

handler = logging.StreamHandler()
handler.setFormatter(formatter)

logger = logging.getLogger("citric")
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
LS_URL = os.environ["LIMESURVEY_URL"]
LS_USERNAME = os.environ["LIMESURVEY_USERNAME"]
LS_PASSWORD = os.environ["LIMESURVEY_PASSWORD"]<jupyter_output><empty_output><jupyter_text>Move survey file to S3- Setup S3 client- Create a new bucket- Upload file from local path<jupyter_code>!ls ../../examples
s3 = boto3.client("s3", endpoint_url="http://storage:9000")

s3.create_bucket(Bucket="testing")
s3.upload_file("../../examples/survey.lss", "testing", "survey.lss")<jupyter_output><empty_output><jupyter_text>Upload survey from S3- Start `citric` client- Download file from S3- Import survey from S3 file- Display survey questions<jupyter_code>with citric.Client(LS_URL, LS_USERNAME, LS_PASSWORD) as client:
    file_object = io.BytesIO()
    s3.download_fileobj("testing", "survey.lss", file_object)

    file_object.seek(0)
    survey_id = client.import_survey(file_object)
    questions = client.list_questions(survey_id)

    for question in questions:
        display(HTML(question["question"] + question["help"] + "<hr>"))<jupyter_output>2022-02-05 23:38:07,086 INFO Invoked RPC method get_session_key with ID 321389
2022-02-05 23:38:18,549 INFO Invoked RPC method import_survey with ID 306084
2022-02-05 23:38:19,266 INFO Invoked RPC method list_questions with ID 478053<jupyter_text>Cleanup- Delete bucket- Delete survey<jupyter_code>s3.delete_object(Bucket="testing", Key="survey.lss")
s3.delete_bucket(Bucket="testing")

with citric.Client(LS_URL, LS_USERNAME, LS_PASSWORD) as client:
    client.delete_survey(survey_id)<jupyter_output>2022-02-05 23:38:24,724 INFO Invoked RPC method get_session_key with ID 24642
2022-02-05 23:38:25,890 INFO Invoked RPC method delete_survey with ID 239814
2022-02-05 23:38:26,447 INFO Invoked RPC method release_session_key with ID 154651
{'fraction_non_alphanumeric': 0.11341991341991342, 'fraction_numerical': 0.0683982683982684, 'mean_word_length': 6.677740863787376, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '429194', 'license': 'MIT', 'n_tokens_mistral': 915, 'n_tokens_neox': 813, 'n_words': 179, 'path': 'docs/notebooks/import_s3.ipynb', 'repo_name': 'edgarrmondragon/limette', 'size': '2310'}