Skip to content

Instantly share code, notes, and snippets.

View amjith's full-sized avatar
🐐

Amjith Ramanujam amjith

🐐
View GitHub Profile
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((len(match.group()), match.start(), item))
return [x for _, _, x in sorted(suggestions)]
>>> print fuzzyfinder('user', collection)
['api_user.doc', 'user_group.doc']
>>> 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']
@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)
@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 / pong.elm
Created June 16, 2016 05:49
Using focus in elm 0.17
module Pong exposing (..)
import Element exposing (..)
import Collage exposing (..)
import Color exposing (..)
import Keyboard exposing (KeyCode)
import AnimationFrame
import Time exposing (Time)
import Html exposing (Html, text)
import Html.App as Html
@amjith
amjith / litecli.py
Last active November 24, 2017 06:13
Template for creating awesome REPLs with prompt-toolkit
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.contrib.completers import WordCompleter
from pygments.lexers.sql import SqlLexer
import sqlite3
history = FileHistory('history.txt')
completer = WordCompleter(['select', 'insert', 'delete', 'drop', 'from'])
connection = sqlite3.connect(':memory:')

Keybase proof

I hereby claim:

  • I am amjith on github.
  • I am amjith (https://keybase.io/amjith) on keybase.
  • I have a public key ASCybq0HEjJam9F4HCBqN8nVKGXLojzGGReK9y6p4vTLUgo

To claim this, I am signing this object:

ERROR: [23:00:40][tinytacoteam/zazu-clipboard:clip]
Script failed
{
"error": {},
"query": ""
}
ERROR: [23:00:40][tinytacoteam/zazu-clipboard:clip]
Script failed
{