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 sqlite3 | |
from pygments.lexers.sql import SqlLexer | |
from prompt_toolkit import prompt | |
from prompt_toolkit.history import FileHistory | |
import click | |
from prompt_toolkit.contrib.completers import WordCompleter | |
sql_completer = WordCompleter([ | |
'select', 'insert', 'delete', 'drop', 'from', 'where' | |
]) |
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
from pygments.token import Token | |
from pygments.style import Style | |
from pygments.util import ClassNotFound | |
from prompt_toolkit.styles import default_style_extensions | |
import pygments.styles | |
def style_factory(name, completion_selected_color, completion_unselected_color): | |
try: | |
style = pygments.styles.get_style_by_name(name) |
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
>>> collection = open('/usr/share/dict/words').readlines() | |
>>> fuzzyfinder('pgcli', collection) | |
<generator object <genexpr> at 0x102f20eb0> | |
>>> list(fuzzyfinder('pgcli', collection)) | |
['plagioclinal\n', 'plagioclasite\n', 'plagioclastic\n', 'pragmaticality\n', | |
'pyrgocephalic\n', 'phagocytolysis\n', 'phagocytolytic\n', 'phenylglycolic\n', | |
'plagiocephalic\n', 'plagiocephalism\n', 'pathologicoclinical\n', 'periangiocholitis\n', | |
'pathologicohistological\n', 'pathologicopsychological\n'] | |
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
>>> print fuzzyfinder('user', collection) | |
['api_user.doc', 'user_group.doc'] |
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 re # regex module from standard library. | |
>>> def fuzzyfinder(user_input, collection): | |
suggestions = [] | |
pattern = '.*'.join(user_input) # Converts 'djm' to 'd.*j.*m' | |
regex = re.compile(pattern) # Compiles a regex. | |
for item in collection: | |
match = regex.search(item) # Checks if the current item matches the regex. | |
if match: | |
suggestions.append((len(match.group()), match.start(), item)) | |
return [x for _, _, x in sorted(suggestions)] |
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
regex = '(m.*i.*g)' | |
'main_generator.py' -> 'main_g' | |
'migrations.py' -> 'mig' | |
'django_migrations.py' -> 'mig' | |
'django_admin_log.py' -> 'min_log' |
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 re # regex module from standard library. | |
>>> def fuzzyfinder(user_input, collection): | |
suggestions = [] | |
pattern = '.*'.join(user_input) # Converts 'djm' to 'd.*j.*m' | |
regex = re.compile(pattern) # Compiles a regex. | |
for item in collection: | |
match = regex.search(item) # Checks if the current item matches the regex. | |
if match: | |
suggestions.append((match.start(), item)) | |
return [x for _, x in sorted(suggestions)] |
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
'main_generator.py' - 0 | |
'migrations.py' - 0 | |
'django_migrations.py' - 7 | |
'django_admin_log.py' - 9 |
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 re # regex module from standard library. | |
>>> def fuzzyfinder(user_input, collection): | |
suggestions = [] | |
pattern = '.*'.join(user_input) # Converts 'djm' to 'd.*j.*m' | |
regex = re.compile(pattern) # Compiles a regex. | |
for item in collection: | |
match = regex.search(item) # Checks if the current item matches the regex. | |
if match: | |
suggestions.append(item) | |
return suggestions |
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
>>> collection = ['django_migrations.py', | |
'django_admin_log.py', | |
'main_generator.py', | |
'migrations.py', | |
'api_user.doc', | |
'user_group.doc', | |
'accounts.txt', | |
] |