From afcaaafbe59c0c24c41ec481a0b0b5cd913fa10f Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Sun, 25 May 2014 21:55:25 -0400 Subject: [PATCH] Improved filesystem efficiency Previously, `sheets.print()` would query the filesystem every time it was invoked. This was inelegant, because it is called multiple times every time `cheat` is executed. Thus, unnecessary calls were being made out to the filesystem. Now the result of that function is being buffered into a module variable when it is executed the first time, and served from there thereafter. I broke the "functional" paradigm to a degree by doing this, but it wasn't worth the complexity of implementing proper memoization (decorators, etc) for such a trivial case. Bumped the version number accordingly. --- bin/cheat | 2 +- cheat/sheets.py | 15 +++++++++++++-- setup.py | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/bin/cheat b/bin/cheat index cc172d0..60bad56 100755 --- a/bin/cheat +++ b/bin/cheat @@ -38,7 +38,7 @@ from docopt import docopt if __name__ == '__main__': # parse the command-line options - options = docopt(__doc__, version='cheat 2.0.3') + options = docopt(__doc__, version='cheat 2.0.4') # list directories if options['--directories']: diff --git a/cheat/sheets.py b/cheat/sheets.py index c4da657..10dbfaf 100644 --- a/cheat/sheets.py +++ b/cheat/sheets.py @@ -2,6 +2,12 @@ from cheat import cheatsheets from cheat.utils import * import os +# @kludge: it breaks the functional paradigm to a degree, but declaring this +# var here (versus within get()) gives us a "poor man's" memoization on the +# call to get(). This, in turn, spares us from having to call out to the +# filesystem more than once. +cheats = {} + def default_path(): """ Returns the default cheatsheet path """ @@ -35,10 +41,15 @@ def default_path(): return default_sheets_dir -# @todo: memoize result def get(): """ Assembles a dictionary of cheatsheets as name => file-path """ - cheats = {} + + # if we've already reached out to the filesystem, just return the result + # from memory + if cheats: + return cheats + + # otherwise, scan the filesystem for cheat_dir in reversed(paths()): cheats.update( dict([ diff --git a/setup.py b/setup.py index 711969e..ef8eb69 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ if os.name == 'nt': setup( name = 'cheat', - version = '2.0.3', + version = '2.0.4', author = 'Chris Lane', author_email = 'chris@chris-allen-lane.com', license = 'GPL3',