Exemplo n.º 1
0
 def read_masterdata(self, criteria):
     criteria_sql = list()
     for key, value in criteria.items():
         criteria_sql.append(SqlHelper.key_value_to_selection_sql(key, value))
     selection_sql = ' AND '.join(criteria_sql)
     q = '''
         SELECT `code`, `name`
         FROM masterdata
         WHERE ({selection_sql})
     '''.format(selection_sql=selection_sql)
     return self.fetch_as_dict(q)
    def words_with_pattern(self, input_word, exists=False):
        '''Return all words with the given pattern.

        Inputs
        ------
        pattern: word with periods as jokers.
        exists: flag indicating whether to check if a word merely has to exist.

        Returns
        -------
        If exists == False: List of words that match the input pattern. Words are capitalized.
        If exists == True: Boolean to indicate if a word with the given input pattern exists.

        Example
        -------
        >>> words_with_pattern('u.tge...en')
        >>> ['UITGEBETEN', 'UITGEBOGEN', 'UITGEGETEN', 'UITGEGETEN', 'UITGEGOTEN',
             'UITGEKOMEN', 'UITGEKOZEN', 'UITGELADEN', 'UITGELOPEN', 'UITGEMALEN',
             'UITGENEPEN', 'UITGEREDEN', 'UITGEREZEN', 'UITGEVAREN', 'UITGEWEKEN',
             'UITGEWEZEN', 'UITGEWOGEN', 'UITGEZETEN', 'UITGEZOGEN', 'UITGEZOPEN']
        '''
        selection = dict()
        for i, input_char in enumerate(input_word):
            if input_char != '.':
                field = 'letter{:02d}'.format(i + 1)
                selection[field] = input_char.upper()
        field = 'letter{:02d}'.format(i + 2)
        selection[field] = ''
        selection_sql = SqlHelper.criteria_dict_to_selection_sql(selection)

        sql = 'SELECT word FROM by_letter WHERE {}'.format(selection_sql)

        if exists:
            sql += ' LIMIT 1'
            r = self.db.fetch_as_array(sql)
            if not r:
                return False
            else:
                return True
        else:
            r = self.db.fetch_as_array(sql)
            return r