From 80a3f0da4dc56593a79d81c79edc510e8e130b86 Mon Sep 17 00:00:00 2001 From: William Ting Date: Sat, 28 Dec 2013 07:13:47 -0600 Subject: [PATCH] remove unused imports, minor formatting cleanup --- Makefile | 3 +++ bin/autojump | 17 ++++++++--------- bin/data.py | 9 ++++++--- bin/utils.py | 21 +++++++++------------ tools/autojump_ipython.py | 20 ++++++++++++-------- 5 files changed, 38 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 5c2e671..9a06039 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,9 @@ docs: pandoc -s -w man docs/manpage_header.md docs/header.md docs/body.md -o docs/autojump.1 pandoc -s -w markdown docs/header.md docs/install.md docs/development.md docs/body.md -o README.md +lint: + flake8 ./ + release: docs # Check for tag existence # git describe release-$(VERSION) 2>&1 >/dev/null || exit 1 diff --git a/bin/autojump b/bin/autojump index d366fb7..0a509f5 100755 --- a/bin/autojump +++ b/bin/autojump @@ -22,13 +22,11 @@ from __future__ import print_function from difflib import SequenceMatcher -from functools import partial from itertools import chain from math import sqrt from operator import attrgetter from operator import itemgetter import os -import platform import re import sys @@ -61,7 +59,6 @@ from utils import last from utils import print_entry from utils import print_tab_menu from utils import sanitize -from utils import second from utils import surround_quotes from utils import take @@ -142,7 +139,7 @@ def add_path(data, path, increment=10): return data, Entry(path, 0) if path in data: - data[path] = sqrt((data[path]**2) + (increment**2)) + data[path] = sqrt((data[path] ** 2) + (increment ** 2)) else: data[path] = increment @@ -152,7 +149,7 @@ def add_path(data, path, increment=10): def decrease_path(data, path, increment=15): """Decrease weight of existing path.""" path = decode(path).rstrip(os.sep) - data[path] = max(0, data[path]-increment) + data[path] = max(0, data[path] - increment) return data, Entry(path, data[path]) @@ -212,7 +209,10 @@ def match_anywhere(needles, haystack, ignore_case=False): """ regex_needle = '.*' + '.*'.join(needles) + '.*' regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE - found = lambda haystack: re.search(regex_needle, haystack.path, flags=regex_flags) + found = lambda haystack: re.search( + regex_needle, + haystack.path, + flags=regex_flags) return ifilter(found, haystack) @@ -289,8 +289,7 @@ def match_fuzzy(needles, haystack, ignore_case=False): match_percent = lambda entry: SequenceMatcher( a=needle, b=end_dir(entry.path)).ratio() - meets_threshold = lambda entry: match_percent(entry) \ - >= FUZZY_MATCH_THRESHOLD + meets_threshold = lambda entry: match_percent(entry) >= FUZZY_MATCH_THRESHOLD return ifilter(meets_threshold, haystack) @@ -344,7 +343,7 @@ def main(args): old_data = load(config) new_data = dictify(purge_missing_paths(entriefy(old_data))) save(config, new_data) - print("Purged %d entries." % (len(old_data)-len(new_data))) + print("Purged %d entries." % (len(old_data) - len(new_data))) elif args.stat: print_stats(load(config), config['data_path']) else: diff --git a/bin/data.py b/bin/data.py index fc9c716..c0e519a 100644 --- a/bin/data.py +++ b/bin/data.py @@ -4,7 +4,6 @@ from __future__ import print_function from codecs import open from collections import namedtuple -from operator import itemgetter import os import shutil import sys @@ -68,8 +67,12 @@ def load(config): tupleize = lambda x: (x[1], float(x[0])) try: - with open(config['data_path'], 'r', encoding='utf-8', errors='replace') as f: - return dict(imap(tupleize, ifilter(correct_length, imap(parse, f)))) + with open( + config['data_path'], + 'r', encoding='utf-8', + errors='replace') as f: + return dict( + imap(tupleize, ifilter(correct_length, imap(parse, f)))) except (IOError, EOFError): return load_backup(config) diff --git a/bin/utils.py b/bin/utils.py index 0377663..6343872 100644 --- a/bin/utils.py +++ b/bin/utils.py @@ -3,7 +3,6 @@ from __future__ import print_function -from collections import Iterable import errno from itertools import islice import os @@ -14,11 +13,9 @@ import sys import unicodedata if sys.version_info[0] == 3: - ifilter = filter imap = map os.getcwdu = os.getcwd else: - from itertools import ifilter from itertools import imap @@ -68,7 +65,7 @@ def get_needle(tab_entry, separator): [needle]__ """ - return re.match(r'(.*)'+separator, tab_entry).group(1) + return re.match(r'(.*)' + separator, tab_entry).group(1) def get_needle_and_index(tab_entry, separator): @@ -78,9 +75,9 @@ def get_needle_and_index(tab_entry, separator): [needle]__[index]__[possible_match] """ matches = re.search( - r'(.*)' + \ - separator + \ - r'([0-9]{1})' + \ + r'(.*)' + + separator + + r'([0-9]{1})' + separator, tab_entry) return matches.group(1), int(matches.group(2)) @@ -126,15 +123,15 @@ def is_tab_entry(needle, separator): [needle]__[index]__[possible_match] """ pattern = re.compile( - '.*' + \ - separator + \ - '[0-9]{1}' + \ + '.*' + + separator + + '[0-9]{1}' + separator) return re.search(pattern, needle) def is_tab_partial_match(needle, separator): - return re.match(r'(.*)'+separator, needle) + return re.match(r'(.*)' + separator, needle) def is_windows(): @@ -186,7 +183,7 @@ def print_tab_menu(needle, tab_entries, separator): '%s%s%d%s%s' % ( needle, separator, - i+1, + i + 1, separator, entry.path)))) diff --git a/tools/autojump_ipython.py b/tools/autojump_ipython.py index 6f530f8..dd71cb8 100644 --- a/tools/autojump_ipython.py +++ b/tools/autojump_ipython.py @@ -4,27 +4,31 @@ IPython autojump magic Written by keith hughitt , based on an earlier version by Mario Pastorelli . -To install, `create a new IPython user profile `_ -if you have not already done so by running: +To install, create a new IPython user profile by running: ipython profile create -And copy this file into the "startup" folder of your new profile (e.g. +And copy this file into the "startup" folder of your new profile (e.g. "$HOME/.config/ipython/profile_default/startup/"). @TODO: extend %cd to call "autojump -a" """ -import os -import subprocess as sub -from IPython.core.magic import (register_line_magic, register_cell_magic, - register_line_cell_magic) +from subprocess import Popen +from subprocess import PIPE + +from IPython.core.magic import register_line_magic ip = get_ipython() + @register_line_magic def j(path): cmd = ['autojump'] + path.split() - newpath = sub.Popen(cmd, stdout=sub.PIPE, shell=False).communicate()[0].strip() + newpath = Popen( + cmd, + stdout=PIPE, + shell=False).communicate()[0].strip() + if newpath: ip.magic('cd %s' % newpath.decode('utf-8'))