From 93de9373edc3653f79ed94501f3bef1941e541c5 2009-07-02 19:14:41 From: Brian Granger Date: 2009-07-02 19:14:41 Subject: [PATCH] Moving things from tools to new utils directory. --- diff --git a/IPython/testing/attic/ipdoctest.py b/IPython/testing/attic/ipdoctest.py index 0f457a8..44b89a1 100755 --- a/IPython/testing/attic/ipdoctest.py +++ b/IPython/testing/attic/ipdoctest.py @@ -62,7 +62,7 @@ import unittest from doctest import * -from IPython.tools import utils +from IPython.utils import genutils from IPython.core import ipapi ########################################################################### @@ -481,8 +481,8 @@ class IPDocTestLoader(unittest.TestLoader): if dt_files is None: dt_files = [] if dt_modules is None: dt_modules = [] - self.dt_files = utils.list_strings(dt_files) - self.dt_modules = utils.list_strings(dt_modules) + self.dt_files = genutils.list_strings(dt_files) + self.dt_modules = genutils.list_strings(dt_modules) if test_finder is None: test_finder = doctest.DocTestFinder(parser=IPDocTestParser()) self.test_finder = test_finder diff --git a/IPython/testing/tools.py b/IPython/testing/tools.py index c43754c..f6ce3ae 100644 --- a/IPython/testing/tools.py +++ b/IPython/testing/tools.py @@ -31,7 +31,7 @@ import sys import nose.tools as nt -from IPython.tools import utils +from IPython.utils import genutils from IPython.testing import decorators as dec #----------------------------------------------------------------------------- @@ -84,6 +84,6 @@ def full_path(startPath,files): ['/a.txt'] """ - files = utils.list_strings(files) + files = genutils.list_strings(files) base = os.path.split(startPath)[0] return [ os.path.join(base,f) for f in files ] diff --git a/IPython/testing/tutils.py b/IPython/testing/tutils.py index 43e7ba9..dbcad02 100644 --- a/IPython/testing/tutils.py +++ b/IPython/testing/tutils.py @@ -23,7 +23,7 @@ import os import sys # From this project -from IPython.tools import utils +from IPython.utils import genutils # path to our own installation, so we can find source files under this. TEST_PATH = os.path.dirname(os.path.abspath(__file__)) @@ -82,6 +82,6 @@ def fullPath(startPath,files): ['/a.txt'] """ - files = utils.list_strings(files) + files = genutils.list_strings(files) base = os.path.split(startPath)[0] return [ os.path.join(base,f) for f in files ] diff --git a/IPython/tools/__init__.py b/IPython/tools/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/IPython/tools/__init__.py +++ /dev/null diff --git a/IPython/tools/tests/__init__.py b/IPython/tools/tests/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/IPython/tools/tests/__init__.py +++ /dev/null diff --git a/IPython/tools/tests/test_tools_utils.txt b/IPython/tools/tests/test_tools_utils.txt deleted file mode 100644 index 60235bc..0000000 --- a/IPython/tools/tests/test_tools_utils.txt +++ /dev/null @@ -1,17 +0,0 @@ -========================================= - Doctests for the ``tools.utils`` module -========================================= - -The way doctest loads these, the entire document is applied as a single test -rather than multiple individual ones, unfortunately:: - - >>> from IPython.tools import utils - - # Some other tests for utils - - >>> utils.marquee('Testing marquee') - '****************************** Testing marquee ******************************' - - >>> utils.marquee('Another test',30,'.') - '........ Another test ........' - diff --git a/IPython/tools/utils.py b/IPython/tools/utils.py deleted file mode 100644 index f34cf5a..0000000 --- a/IPython/tools/utils.py +++ /dev/null @@ -1,128 +0,0 @@ -# encoding: utf-8 -"""Generic utilities for use by IPython's various subsystems. -""" - -__docformat__ = "restructuredtext en" - -#------------------------------------------------------------------------------- -# Copyright (C) 2006 Fernando Perez -# Brian E Granger -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#------------------------------------------------------------------------------- - -#--------------------------------------------------------------------------- -# Stdlib imports -#--------------------------------------------------------------------------- - -import os -import sys - -#--------------------------------------------------------------------------- -# Other IPython utilities -#--------------------------------------------------------------------------- - - -#--------------------------------------------------------------------------- -# Normal code begins -#--------------------------------------------------------------------------- - -def extractVars(*names,**kw): - """Extract a set of variables by name from another frame. - - :Parameters: - - `*names`: strings - One or more variable names which will be extracted from the caller's - frame. - - :Keywords: - - `depth`: integer (0) - How many frames in the stack to walk when looking for your variables. - - - Examples: - - In [2]: def func(x): - ...: y = 1 - ...: print extractVars('x','y') - ...: - - In [3]: func('hello') - {'y': 1, 'x': 'hello'} - """ - - depth = kw.get('depth',0) - - callerNS = sys._getframe(depth+1).f_locals - return dict((k,callerNS[k]) for k in names) - - -def extractVarsAbove(*names): - """Extract a set of variables by name from another frame. - - Similar to extractVars(), but with a specified depth of 1, so that names - are exctracted exactly from above the caller. - - This is simply a convenience function so that the very common case (for us) - of skipping exactly 1 frame doesn't have to construct a special dict for - keyword passing.""" - - callerNS = sys._getframe(2).f_locals - return dict((k,callerNS[k]) for k in names) - -def shexp(s): - """Expand $VARS and ~names in a string, like a shell - - :Examples: - - In [2]: os.environ['FOO']='test' - - In [3]: shexp('variable FOO is $FOO') - Out[3]: 'variable FOO is test' - """ - return os.path.expandvars(os.path.expanduser(s)) - - -def list_strings(arg): - """Always return a list of strings, given a string or list of strings - as input. - - :Examples: - - In [7]: list_strings('A single string') - Out[7]: ['A single string'] - - In [8]: list_strings(['A single string in a list']) - Out[8]: ['A single string in a list'] - - In [9]: list_strings(['A','list','of','strings']) - Out[9]: ['A', 'list', 'of', 'strings'] - """ - - if isinstance(arg,basestring): return [arg] - else: return arg - -def marquee(txt='',width=78,mark='*'): - """Return the input string centered in a 'marquee'. - - :Examples: - - In [16]: marquee('A test',40) - Out[16]: '**************** A test ****************' - - In [17]: marquee('A test',40,'-') - Out[17]: '---------------- A test ----------------' - - In [18]: marquee('A test',40,' ') - Out[18]: ' A test ' - - """ - if not txt: - return (mark*width)[:width] - nmark = (width-len(txt)-2)/len(mark)/2 - if nmark < 0: nmark =0 - marks = mark*nmark - return '%s %s %s' % (marks,txt,marks) - - diff --git a/IPython/utils/genutils.py b/IPython/utils/genutils.py index 7786c5c..85f5e3f 100644 --- a/IPython/utils/genutils.py +++ b/IPython/utils/genutils.py @@ -1424,14 +1424,6 @@ def qw_lol(indata): else: return qw(indata) -#----------------------------------------------------------------------------- -def list_strings(arg): - """Always return a list of strings, given a string or list of strings - as input.""" - - if type(arg) in StringTypes: return [arg] - else: return arg - #---------------------------------------------------------------------------- def grep(pat,list,case=1): """Simple minded grep-like function. @@ -2169,4 +2161,101 @@ def num_cpus(): ncpus = 1 return ncpus +def extract_vars(*names,**kw): + """Extract a set of variables by name from another frame. + + :Parameters: + - `*names`: strings + One or more variable names which will be extracted from the caller's + frame. + + :Keywords: + - `depth`: integer (0) + How many frames in the stack to walk when looking for your variables. + + + Examples: + + In [2]: def func(x): + ...: y = 1 + ...: print extractVars('x','y') + ...: + + In [3]: func('hello') + {'y': 1, 'x': 'hello'} + """ + + depth = kw.get('depth',0) + + callerNS = sys._getframe(depth+1).f_locals + return dict((k,callerNS[k]) for k in names) + + +def extract_vars_above(*names): + """Extract a set of variables by name from another frame. + + Similar to extractVars(), but with a specified depth of 1, so that names + are exctracted exactly from above the caller. + + This is simply a convenience function so that the very common case (for us) + of skipping exactly 1 frame doesn't have to construct a special dict for + keyword passing.""" + + callerNS = sys._getframe(2).f_locals + return dict((k,callerNS[k]) for k in names) + +def shexp(s): + """Expand $VARS and ~names in a string, like a shell + + :Examples: + + In [2]: os.environ['FOO']='test' + + In [3]: shexp('variable FOO is $FOO') + Out[3]: 'variable FOO is test' + """ + return os.path.expandvars(os.path.expanduser(s)) + + +def list_strings(arg): + """Always return a list of strings, given a string or list of strings + as input. + + :Examples: + + In [7]: list_strings('A single string') + Out[7]: ['A single string'] + + In [8]: list_strings(['A single string in a list']) + Out[8]: ['A single string in a list'] + + In [9]: list_strings(['A','list','of','strings']) + Out[9]: ['A', 'list', 'of', 'strings'] + """ + + if isinstance(arg,basestring): return [arg] + else: return arg + +def marquee(txt='',width=78,mark='*'): + """Return the input string centered in a 'marquee'. + + :Examples: + + In [16]: marquee('A test',40) + Out[16]: '**************** A test ****************' + + In [17]: marquee('A test',40,'-') + Out[17]: '---------------- A test ----------------' + + In [18]: marquee('A test',40,' ') + Out[18]: ' A test ' + + """ + if not txt: + return (mark*width)[:width] + nmark = (width-len(txt)-2)/len(mark)/2 + if nmark < 0: nmark =0 + marks = mark*nmark + return '%s %s %s' % (marks,txt,marks) + #*************************** end of file ********************** diff --git a/IPython/tools/growl.py b/IPython/utils/growl.py similarity index 100% rename from IPython/tools/growl.py rename to IPython/utils/growl.py