From 0a5158b9b1fba1d2e960307e7b9bf509d70a945f 2008-07-04 16:48:31 From: Ville M. Vainio Date: 2008-07-04 16:48:31 Subject: [PATCH] sort() returns SList (obviously), %cpaste foo assigns the pasted block to foo as string list --- diff --git a/IPython/Magic.py b/IPython/Magic.py index 6c0c488..896601e 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -3195,7 +3195,7 @@ Defaulting color scheme to 'NoColor'""" exec b in self.user_ns self.user_ns['pasted_block'] = b else: - self.user_ns[par] = block + self.user_ns[par] = SList(block.splitlines()) print "Block assigned to '%s'" % par def magic_quickref(self,arg): diff --git a/IPython/genutils.py b/IPython/genutils.py index 90747b0..f380d89 100644 --- a/IPython/genutils.py +++ b/IPython/genutils.py @@ -1137,7 +1137,7 @@ class SList(list): res.append(" ".join(lineparts)) return res - def sort(self,field, nums = False): + def sort(self,field= None, nums = False): """ sort by specified fields (see fields()) Example:: @@ -1148,7 +1148,10 @@ class SList(list): """ #decorate, sort, undecorate - dsu = [[SList([line]).fields(field), line] for line in self] + if field is not None: + dsu = [[SList([line]).fields(field), line] for line in self] + else: + dsu = [[line, line] for line in self] if nums: for i in range(len(dsu)): numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) @@ -1161,11 +1164,15 @@ class SList(list): dsu.sort() - return [t[1] for t in dsu] + return SList([t[1] for t in dsu]) def print_slist(arg): """ Prettier (non-repr-like) and more informative printer for SList """ - print "SList (.p, .n, .l, .s, .grep(), .fields() available). Value:" + print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):" + if hasattr(arg, 'hideonce') and arg.hideonce: + arg.hideonce = False + return + nlprint(arg) print_slist = result_display.when_type(SList)(print_slist) diff --git a/docs/source/changes.txt b/docs/source/changes.txt index 529f315..7cb4bd7 100644 --- a/docs/source/changes.txt +++ b/docs/source/changes.txt @@ -32,6 +32,7 @@ New features * sh profile: ./foo runs foo as system command, no need to do !./foo anymore * String lists now support 'sort(field, nums = True)' method (to easily sort system command output). Try it with 'a = !ls -l ; a.sort(1, nums=1)' + * '%cpaste foo' now assigns the pasted block as string list, instead of string