Exemple #1
0
def test_drop_tables(db: TinyDB):
    db.drop_tables()

    db.insert({})
    db.drop_tables()

    assert len(db) == 0
Exemple #2
0
def apagaBanco(name):
    db = TinyDB(name)
    try:
        db.drop_tables()
        db.all()
    except Exception as e:
        print(f'erro ao apagar banco. {e}')
Exemple #3
0
def test_all(db: TinyDB):
    db.drop_tables()

    for i in range(10):
        db.insert({})

    assert len(db.all()) == 10
Exemple #4
0
class Handler:
    def __init__(self, db_name: str = 'db.json', shortcut: bool = False, reset: bool = False):
        self.DB_ABSOLUTE_PATH = os.path.join(BASE_DIR, db_name)
        self.SHORTCUT, self.RESET = shortcut, reset
        self.IS_EXISTS_BEFORE_LOAD = os.path.isfile(self.DB_ABSOLUTE_PATH)
        self.DB = TinyDB(self.DB_ABSOLUTE_PATH)
        self.IS_CREATED = False if self.RESET else all([self.IS_EXISTS_BEFORE_LOAD, self.DB.all()])

        if self.SHORTCUT:
            CMD.create_or_update_shortcut()

        if self.RESET:
            self.DB.drop_tables()

        if self.IS_CREATED:
            self.run()
        else:
            self.create_and_run()

    def run(self):
        first = self.DB.all()[0]
        CMD.pythonw_kill_and_run_with_flags(**first)

    def create_and_run(self):
        def callback(**kwargs):
            self.DB.insert(kwargs)
            CMD.pythonw_kill_and_run_with_flags(**kwargs)
        wrapper(callback=callback)
Exemple #5
0
class DatabaseManager():
    """
    A class representing an object used to interact with a local database, to keep track of visitors interacting with the robot
    """

    __db: TinyDB

    def __init__(self):
        # Initialize the database
        self.__db = TinyDB("./visitors_database.json")
        # Clear the DB at each initialization
        self.__db.drop_tables()

    def visitor_exists(self, visitor_id: str) -> bool:
        """Determines whether the provided visitor has been registered in the database

        Args:
            visitor_id (str): The identifier of the visitor

        Returns:
            bool: True if the visitor is present in the database, False otherwise
        """
        query = Query()
        result = self.__db.search(query.id == visitor_id)
        return len(result) > 0

    def has_visitor_left(self, visitor_id: str) -> bool:
        """Determines whether the provided visitor has left the exhibition

        Args:
            visitor_id (str): The identifier of the visitor

        Returns:
            bool: True if the visitor has left the exhibition, False otherwise
        """
        query = Query()
        result = self.__db.search((query.id == visitor_id))
        return len(result) > 0 and result[0]["exit"] != 0

    def write_visitor_entrance(self, visitor_id: str) -> None:
        """Registers the provided visitor into the database. The entry will contain:
        - their identifier
        - the entrance time (automatically retrieved)
        - an empty field for the exit time

        Args:
            visitor_id (str): The identifier of the visitor
        """
        visitor_data = {"id": visitor_id, "entrance": time.time(), "exit": 0}
        self.__db.insert(visitor_data)

    def write_visitor_exit(self, visitor_id: str) -> None:
        """Register the exit time for the given visitor

        Args:
            visitor_id (str): The identifier of the visitor
        """
        query = Query()
        self.__db.update({"exit": time.time()}, query.id == visitor_id)
Exemple #6
0
def db(request):
    with tempfile.TemporaryDirectory() as tmpdir:
        if request.param == 'json':
            db_ = TinyDB(os.path.join(tmpdir, 'test.db'), storage=JSONStorage)
        else:
            db_ = TinyDB(storage=MemoryStorage)

        db_.drop_tables()
        db_.insert_multiple({'int': 1, 'char': c} for c in 'abc')

        yield db_
    def test_data_matches(self):
        with open(join(DB_DIR, SCHEDULE), "r") as file:
            temp_path = join(DB_DIR, 'temp_test_database.json')
            temp = TinyDB(temp_path)
            temp.drop_tables()

            content = file.read()
            parse(content, db=temp)

            self.assertTrue(cmp(temp_path, join(DB_DIR, TEST_DB)),
                            'Generated Test DBs do not match')

            remove(temp_path)
Exemple #8
0
def test_insert_multiple_with_ids(db: TinyDB):
    db.drop_tables()

    # Insert multiple from list
    assert db.insert_multiple([{
        'int': 1,
        'char': 'a'
    }, {
        'int': 1,
        'char': 'b'
    }, {
        'int': 1,
        'char': 'c'
    }]) == [1, 2, 3]
Exemple #9
0
def test_insert(db: TinyDB):
    db.drop_tables()
    db.insert({'int': 1, 'char': 'a'})

    assert db.count(where('int') == 1) == 1

    db.drop_tables()

    db.insert({'int': 1, 'char': 'a'})
    db.insert({'int': 1, 'char': 'b'})
    db.insert({'int': 1, 'char': 'c'})

    assert db.count(where('int') == 1) == 3
    assert db.count(where('char') == 'a') == 1
Exemple #10
0
class Database:

    def __init__(self):
        self.db = TinyDB(DB_NAME)
        # self.clear_db()
        self.players_table = self.db.table('players')
        self.tournaments_table = self.db.table('tournaments')
        self.rounds_table = self.db.table('rounds')
        self.matchs_table = self.db.table('matchs')

    def clear_db(self):
        self.db.drop_tables()

    def insert_players_to_db(self, players):
        self.players_table.insert_multiple(players)

    def insert_rounds_to_db(self, rounds):
        self.rounds_table.insert_multiple(rounds)

    def insert_matchs_to_db(self, matchs):
        self.matchs_table.insert_multiple(matchs)

    def insert_tournaments_to_db(self, tournaments):
        self.tournaments_table.insert_multiple(tournaments)

    def get_all_players(self) -> list:
        players = self.db.table('players')
        return players.all()

    def get_all_tournaments(self) -> list:
        tournaments = self.db.table('tournaments')
        return tournaments.all()

    def get_all_rounds_of_tournament(self, tournament_id: str) -> list:
        tournament = self.tournaments_table.search(where('id') == tournament_id)
        rounds = []
        for round_id in tournament[0]['round_list']:
            round_dict = self.rounds_table.search(where('id') == round_id)
            rounds.append(round_dict[0])
        return rounds

    def get_all_matchs_of_round(self, round_id: int) -> list:
        round = self.rounds_table.search(where('id') == round_id)
        matchs = []
        for match_id in round[0]['matchs']:
            match_dict = self.matchs_table.search(where('id') == match_id)
            matchs.append(match_dict[0])
        return matchs
Exemple #11
0
 def save_report(self, path):
     db = TinyDB(path)
     db.drop_tables()
     header = db.table("header")
     header.insert({"film_title": self.film_title})
     header.insert({"director": self.director})
     header.insert({"cinematographer": self.cinematographer})
     header.insert({"production": self.production})
     header.insert({"report_number": self.report_number})
     header.insert({"qc_date": self.qc_date})
     header.insert({"shooting_days": self.shooting_days})
     header.insert({"csv_list": self.csv_list})
     header.insert({"card_list": self.all_card_list})
     clip_list = db.table("clip_list")
     for clip in self.clip_list:
         clip_list.insert(clip.clip_to_dict())
Exemple #12
0
def test_insert_valid_mapping_type(db: TinyDB):
    class CustomDocument(Mapping):
        def __init__(self, data):
            self.data = data

        def __getitem__(self, key):
            return self.data[key]

        def __iter__(self):
            return iter(self.data)

        def __len__(self):
            return len(self.data)

    db.drop_tables()
    db.insert(CustomDocument({'int': 1, 'char': 'a'}))
    assert db.count(where('int') == 1) == 1
Exemple #13
0
def test_insert_multiple_with_doc_ids(db: TinyDB):
    db.drop_tables()

    assert db.insert_multiple([
        Document({
            'int': 1,
            'char': 'a'
        }, 12),
        Document({
            'int': 1,
            'char': 'b'
        }, 77)
    ]) == [12, 77]
    assert db.get(doc_id=12) == {'int': 1, 'char': 'a'}
    assert db.get(doc_id=77) == {'int': 1, 'char': 'b'}

    with pytest.raises(ValueError):
        db.insert_multiple([Document({'int': 1, 'char': 'a'}, 12)])
Exemple #14
0
def merge_dbs(final: TinyDB, first: TinyDB, second: TinyDB, allowed):
    classes1 = {doc['CRN']: doc for doc in first.table('classes').all()}
    classes2 = {doc['CRN']: doc for doc in second.table('classes').all()}

    classes = []

    for CRN in classes1.keys():
        cl1 = classes1[CRN].copy()
        cl2 = classes2.get(CRN)

        if cl2:
            classes.append(merge_dicts(cl1, cl2.copy(), allowed=allowed))
        else:
            log_err(f'Class {CRN} was only found in one DB!')
            classes.append(cl1)

    final.drop_tables()
    final.table('classes').insert_multiple(classes)
Exemple #15
0
def test_cutom_mapping_type_with_json(tmpdir):
    from tinydb.database import Mapping

    class CustomDocument(Mapping):
        def __init__(self, data):
            self.data = data

        def __getitem__(self, key):
            return self.data[key]

        def __iter__(self):
            return iter(self.data)

        def __len__(self):
            return len(self.data)

    # Insert
    db = TinyDB(str(tmpdir.join('test.db')))
    db.drop_tables()
    db.insert(CustomDocument({'int': 1, 'char': 'a'}))
    assert db.count(where('int') == 1) == 1

    # Insert multiple
    db.insert_multiple([
        CustomDocument({
            'int': 2,
            'char': 'a'
        }),
        CustomDocument({
            'int': 3,
            'char': 'a'
        })
    ])
    assert db.count(where('int') == 1) == 1
    assert db.count(where('int') == 2) == 1
    assert db.count(where('int') == 3) == 1

    # Write back
    doc_id = db.get(where('int') == 3).doc_id
    db.update(CustomDocument({'int': 4, 'char': 'a'}), doc_ids=[doc_id])
    assert db.count(where('int') == 3) == 0
    assert db.count(where('int') == 4) == 1
Exemple #16
0
def test_insert_multiple(db: TinyDB):
    db.drop_tables()
    assert not db.contains(where('int') == 1)

    # Insert multiple from list
    db.insert_multiple([{
        'int': 1,
        'char': 'a'
    }, {
        'int': 1,
        'char': 'b'
    }, {
        'int': 1,
        'char': 'c'
    }])

    assert db.count(where('int') == 1) == 3
    assert db.count(where('char') == 'a') == 1

    # Insert multiple from generator function
    def generator():
        for j in range(10):
            yield {'int': j}

    db.drop_tables()

    db.insert_multiple(generator())

    for i in range(10):
        assert db.count(where('int') == i) == 1
    assert db.count(where('int').exists()) == 10

    # Insert multiple from inline generator
    db.drop_tables()

    db.insert_multiple({'int': i} for i in range(10))

    for i in range(10):
        assert db.count(where('int') == i) == 1
Exemple #17
0
def test_insert_with_duplicate_doc_id(db: TinyDB):
    db.drop_tables()
    assert db.insert({'int': 1, 'char': 'a'}) == 1

    with pytest.raises(AssertionError):
        db.insert(Document({'int': 1, 'char': 'a'}, 1))
Exemple #18
0
def test_insert_with_doc_id(db: TinyDB):
    db.drop_tables()
    assert db.insert({'int': 1, 'char': 'a'}) == 1
    assert db.insert(Document({'int': 1, 'char': 'a'}, 12)) == 12
    assert db.insert(Document({'int': 1, 'char': 'a'}, 77)) == 77
    assert db.insert({'int': 1, 'char': 'a'}) == 78
Exemple #19
0
def test_insert_ids(db: TinyDB):
    db.drop_tables()
    assert db.insert({'int': 1, 'char': 'a'}) == 1
    assert db.insert({'int': 1, 'char': 'a'}) == 2
Exemple #20
0
class Bot:
    def __init__(self, config: Config):
        self.config = config
        self.template_post = self._read_template(config.templates_path +
                                                 '/template_post')
        self.template_head = self._read_template(config.templates_path +
                                                 '/template_head')
        self.template_header = self._read_template(config.templates_path +
                                                   '/template_header')
        self.template_html = self._read_template(config.templates_path +
                                                 '/template_html')
        self.db = TinyDB(config.db_file_name)

    def clean_and_rebuild_blog(self):
        self.db.drop_tables()

        async def _rebuild():
            messages = await client.get_messages(self.config.channel_name,
                                                 None)
            for m in messages:
                if m.message is None or m.message == '':
                    continue
                self._save_to_db(m.id, m.date.strftime("%b %d, %H:%M"),
                                 m.message)
            channel_info = await self._get_channel_info(client)
            self._create_html_and_write_to_disk(channel_info)

        with TelegramClient('withuser', self.config.api_id,
                            self.config.api_hash) as client:
            client.loop.run_until_complete(_rebuild())

    def listen_for_channel_events(self):
        client = TelegramClient('telegabot', self.config.api_id,
                                self.config.api_hash)
        client.start(bot_token=self.config.bot_token)

        async def _process_channel_event(event):
            if event.message.message is None or event.message.message == '':
                return

            self._save_to_db(event.message.id,
                             event.message.date.strftime("%b %d, %H:%M"),
                             event.message.message)
            channel_info = await self._get_channel_info(client)
            self._create_html_and_write_to_disk(channel_info)

        client.add_event_handler(
            _process_channel_event,
            events.NewMessage(chats=self.config.channel_name))
        with client:
            client.run_until_disconnected()

    def _create_html_and_write_to_disk(self, channel_info):
        website_title = channel_info[
            'title'] if self.config.website_title is None else self.config.website_title
        website_description = channel_info[
            'about'] if self.config.website_description is None else self.config.website_description

        header = self.template_header \
            .replace('{{channel_name}}', self.config.channel_name) \
            .replace('{{channel_title}}', website_title) \
            .replace('{{channel_members}}', channel_info['members']) \
            .replace('{{channel_date}}', channel_info['date']) \
            .replace('{{channel_description}}', website_description.replace('\n', '</br>')) \
            .replace('{{channel_avatar}}', self.config.channel_photo_name)

        head = self.template_head \
            .replace('{{url}}', self.config.website_name) \
            .replace('{{channel_name}}', self.config.channel_name) \
            .replace('{{channel_title}}', website_title) \
            .replace('{{channel_description_clean}}', website_description.replace('\n', ' ')) \
            .replace('{{channel_avatar}}', self.config.channel_photo_name)

        post = ''
        for p in reversed(sorted(self.db.all(), key=lambda k: k['id'])):
            post = post + self.template_post \
                .replace('{{channel_name}}', self.config.channel_name) \
                .replace('{{channel_title}}', channel_info['title']) \
                .replace('{{post_date}}', p.get('date')) \
                .replace('{{post_text}}', p.get('text').replace('\n', '</br>')) + '\n'

        html = self.template_html \
            .replace('{{head}}', head) \
            .replace('{{header}}', header) \
            .replace('{{posts}}', post)

        with open('index.html', 'w') as f:
            f.write(html)
            f.flush()

    def _save_to_db(self, id, date, text):
        self.db.upsert({
            'id': id,
            'date': date,
            'text': text,
        },
                       where('id') == id)

    async def _get_channel_info(self, client):
        channel = await client.get_entity(self.config.channel_name)
        await client.download_profile_photo(
            self.config.channel_name, file=self.config.channel_photo_name)
        result = await client(
            functions.channels.GetFullChannelRequest(
                channel=self.config.channel_name))
        return {
            'about': result.full_chat.about,
            'members': str(result.full_chat.participants_count),
            'title': channel.title,
            'date': channel.date.strftime("%Y")
        }

    def _read_template(self, template_path: str) -> str:
        with open(template_path, 'r') as template_file:
            return template_file.read()
Exemple #21
0
def test_update_returns_ids(db: TinyDB):
    db.drop_tables()
    assert db.insert({'int': 1, 'char': 'a'}) == 1
    assert db.insert({'int': 1, 'char': 'a'}) == 2

    assert db.update({'char': 'b'}, where('int') == 1) == [1, 2]
Exemple #22
0
class StarredDB(object):
    def __init__(self, my_stars_home, mode):
        self._db = TinyDB(os.path.join(my_stars_home, 'mystars.db'),
                          storage=CachingMiddleware(JSONStorage))

        if mode == 't':
            self._db.drop_tables()

        self._idx = self._db.table('index')

        if not self._idx.contains(Query().name == 'language'):
            self._idx.insert({'name': 'language', 'docs': {}})
        if not self._idx.contains(Query().name == 'keyword'):
            self._idx.insert({'name': 'keyword', 'docs': {}})

    def __enter__(self):
        return self

    def __exit__(self, exception_type, exception_value, traceback):
        self._db.close()

    def _get_index_docs(self, name):
        return self._idx.get(Query().name == name).get('docs', {})

    def update(self, repo_list):

        if repo_list:
            self._db.table('latest_repo').truncate()
            self._db.table('latest_repo').insert(repo_list[0])

        language_docs = self._get_index_docs('language')
        keyword_docs = self._get_index_docs('keyword')

        for repo in repo_list:

            # save repo data
            doc_id = self._db.insert(repo)

            # update index
            name = repo.get('name')
            language = repo.get('language')
            description = repo.get('description')

            if language:
                for lang in language.split():
                    update_inverted_index(language_docs, lang.lower(), doc_id)

            keywords = split_repo_name(name)
            if description:
                keywords += split_repo_desc(description)
            for keyword in split_keywords(keywords):
                update_inverted_index(keyword_docs, keyword.lower(), doc_id)

        self._idx.update(operations.set('docs', language_docs),
                         Query().name == 'language')
        self._idx.update(operations.set('docs', keyword_docs),
                         Query().name == 'keyword')

    def get_latest_repo_full_name(self):
        latest_repo = self._db.table('latest_repo').all()
        if len(latest_repo) > 0:
            return latest_repo[0].get('full_name')

    def all_repos(self):
        return self._db.table('_default').all()

    def search(self, languages, keywords):

        # self._build_index()
        language_docs = self._get_index_docs('language')
        keyword_docs = self._get_index_docs('keyword')

        if not language_docs and not language_docs:
            raise EmptyIndexWarning('empty index')

        language_results = []
        if languages:
            for search in languages:
                language_results += language_docs.get(search.lower(), [])

        keywords_results = []
        if keywords:
            for keyword in keywords:
                for term in split_repo_name(keyword):
                    results = keyword_docs.get(term.lower(), [])
                    keywords_results.append(results)

        if languages and keywords:
            # python > 2.6
            search_results = list(
                set(language_results).intersection(*keywords_results))
        else:
            if len(keywords_results) > 1:
                # python > 2.6
                final_keywords_results = list(
                    set(keywords_results[0]).intersection(
                        *keywords_results[1:]))
            else:
                final_keywords_results = []
                for results in keywords_results:
                    for r in results:
                        final_keywords_results.append(r)

            search_results = language_results + final_keywords_results

        # remove duplicates then sort by id
        search_results = sorted(list(set(search_results)), key=int)

        return [self._db.get(doc_id=doc_id) for doc_id in search_results]
Exemple #23
0
from tinydb import TinyDB

db = TinyDB('data/db.json')
db.drop_tables()
t_product = db.table('Products')
t_product.insert({'name': 'chleb', 'price': 5, 'shelf': 0, 'number': 3})  # 0
t_product.insert({'name': 'bułka', 'price': 0.8, 'shelf': 0, 'number': 5})  # 1
t_product.insert({'name': 'mąka', 'price': 1.8, 'shelf': 1, 'number': 3})  # 2
t_product.insert({'name': 'jajko', 'price': 0.4, 'shelf': 2, 'number': 5})  # 3
t_product.insert({'name': 'olej', 'price': 3, 'shelf': 5, 'number': 2})  # 4
t_product.insert({'name': 'pomidor', 'price': 2, 'shelf': 6, 'number': 4})  # 5
t_product.insert({
    'name': 'ogórek',
    'price': 1.5,
    'shelf': 6,
    'number': 3
})  # 6
t_product.insert({
    'name': 'szczypiorek',
    'price': 0.3,
    'shelf': 4,
    'number': 3
})  # 7
t_product.insert({
    'name': 'sałata',
    'price': 0.5,
    'shelf': 6,
    'number': 3
})  # 8
t_product.insert({'name': 'ser', 'price': 4, 'shelf': 3, 'number': 3})  # 9
t_product.insert({
Exemple #24
0
def db():
    db_ = TinyDB(storage=MemoryStorage)
    db_.drop_tables()
    db_.insert_multiple({'int': 1, 'char': c} for c in 'abc')
    return db_