Skip to content

Instantly share code, notes, and snippets.

View amjith's full-sized avatar
🐐

Amjith Ramanujam amjith

🐐
View GitHub Profile
@amjith
amjith / cli.py
Created January 29, 2016 05:55
prompt-toolkit starter template
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'
])
@amjith
amjith / pgstyle.py
Created August 2, 2015 18:10
PGStyle with adjustable colors
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)
>>> 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']
>>> print fuzzyfinder('user', collection)
['api_user.doc', 'user_group.doc']
>>> 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)]
regex = '(m.*i.*g)'
'main_generator.py' -> 'main_g'
'migrations.py' -> 'mig'
'django_migrations.py' -> 'mig'
'django_admin_log.py' -> 'min_log'
>>> 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)]
'main_generator.py' - 0
'migrations.py' - 0
'django_migrations.py' - 7
'django_admin_log.py' - 9
>>> 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
@amjith
amjith / file_list.py
Last active August 29, 2015 14:23
List of files.
>>> collection = ['django_migrations.py',
'django_admin_log.py',
'main_generator.py',
'migrations.py',
'api_user.doc',
'user_group.doc',
'accounts.txt',
]