Created
May 20, 2012 21:08
-
-
Save gregsexton/2759543 to your computer and use it in GitHub Desktop.
Persistent directory tracking
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 sys | |
import os.path | |
import pickle | |
from SizeLimitedStack import * | |
PERSISTENT_FILE = os.path.expanduser('~/.persdirs') | |
def add_dir_to_stack(new_dir): | |
idx = sls.find(new_dir) | |
if idx > -1: sls.move_to_top(idx) | |
else: sls.push(new_dir) | |
f = open(PERSISTENT_FILE, 'w+') | |
try: pickle.dump(sls, f) | |
finally: f.close() | |
def output_choose_dir(): | |
acc = [] | |
for idx,d in enumerate(sls): | |
acc.append("%d: %s" % (idx, d)) | |
return "\n".join(acc) | |
def print_choice(choice): | |
idx = int(choice) | |
if idx >= 0 and idx < 10: print sls.get(idx) | |
else: print "" | |
if os.path.exists(PERSISTENT_FILE): | |
f = open(PERSISTENT_FILE, 'r') | |
try: sls = pickle.load(f) | |
finally: f.close() | |
else: | |
sls = SizeLimitedStack(10) | |
if len(sys.argv) > 1: | |
if(sys.argv[1] == "--choose" and len(sys.argv) == 3): | |
print_choice(sys.argv[2]) | |
else: | |
add_dir_to_stack(" ".join(sys.argv[1:])) | |
else: | |
print output_choose_dir() |
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
#This was a quick hack; most things are O(n). | |
class SizeLimitedStack: | |
def __init__(self, size): | |
self.stack = [] | |
self.count = 0 | |
self.size = size | |
self.pointer = 0 | |
def __iter__(self): | |
if self.count <= 0: return | |
for i in xrange(min(self.size, self.count)): | |
yield self.stack[(self.pointer - i - 1)] | |
def __str__(self): | |
if self.count <= 0: return "[]" | |
return "[" + ", ".join([str(x) for x in self]) + "]" | |
def push(self, elem): | |
if self.count < self.size: | |
self.stack.append(elem) | |
else: | |
self.stack[self.pointer] = elem | |
self.pointer = (self.pointer + 1) % self.size | |
self.count += 1 | |
#allow for fluid api | |
return self | |
def contains(self, x): | |
for elem in self.stack: | |
if x == elem: | |
return True | |
return False | |
def find(self, x): | |
#returns the true index | |
for i,e in enumerate(self.stack): | |
if e == x: | |
return i | |
return -1 | |
def get(self, idx): | |
#takes the pseudo-index | |
for i,v in enumerate(self): | |
if i == idx: return v | |
def move_to_top(self, idx): | |
#takes the true index, will de-dup anything equal to the element | |
#at idx as a side-effect | |
if idx > self.count or idx < 0: | |
return | |
acc = [self.stack[idx]] | |
for x in self: | |
if x == self.stack[idx]: continue | |
acc.append(x) | |
acc.reverse() | |
self.pointer = len(acc) % self.size | |
self.count = len(acc) | |
self.stack = acc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment