Edgewall Software

Ticket #8519: sqlite_like.py

File sqlite_like.py, 997 bytes (added by cboos, 2 years ago)

function to be used as s LIKE p operator in SQLite statements.

Line 
1def sqlite_like(p, s):
2    """Function to be used a `s LIKE p` operator in SQLite statements.
3    >>> sqlite_like('Starts with%', 'Starts with this')
4    True
5    >>> sqlite_like('Starts with%', ' Starts with this')
6    False
7    >>> sqlite_like('%ends with', '... ends with')
8    True
9    >>> sqlite_like('%ends with', ' ends with ...')
10    False
11    >>> sqlite_like('%contains%', 'This contains the pattern')
12    True
13    >>> sqlite_like('%contains%', 'This doesn't contain the pattern')
14    False
15    >>> sqlite_like('This is the pattern', 'This is the pattern')
16    True
17    >>> sqlite_like('This is the pattern', 'This is not the pattern')
18    False
19    >>> sqlite_like('', 'Anything')
20    True
21    """
22    if not p:
23        return True
24    l = p[-1] == '%'
25    if p[0] == '%':
26        if l:
27            return p[1:-1] in s
28        else:
29            return s.endswith(p[1:])
30    elif l:
31        return s.startswith(p[:-1])
32    else:
33        return s == p
34