Updating string formatting #504

Merged
merged 4 commits into from Jul 3, 2011
@@ -24,7 +24,6 @@
from pprint import pformat
from IPython.core import ultratb
-from IPython.external.Itpl import itpl
from IPython.utils.sysinfo import sys_info
#-----------------------------------------------------------------------------
@@ -33,26 +32,26 @@
# Template for the user message.
_default_message_template = """\
-Oops, $self.app_name crashed. We do our best to make it stable, but...
+Oops, {app_name} crashed. We do our best to make it stable, but...
A crash report was automatically generated with the following information:
- A verbatim copy of the crash traceback.
- A copy of your input history during this session.
- Data on your current $self.app_name configuration.
It was left in the file named:
-\t'$self.crash_report_fname'
+\t'{crash_report_fname}'
If you can email this file to the developers, the information in it will help
them in understanding and correcting the problem.
-You can mail it to: $self.contact_name at $self.contact_email
-with the subject '$self.app_name Crash Report'.
+You can mail it to: $self.contact_name at {contact_email}
+with the subject '{app_name} Crash Report'.
If you want to do it now, the following command will work (under Unix):
-mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
+mail -s '{app_name} Crash Report' {contact_email} < {crash_report_fname}
To ensure accurate tracking of this issue, please file a report about it at:
-$self.bug_tracker
+{bug_tracker}
"""
@@ -66,6 +65,7 @@ def __call__(self, etype, evalue, etb)
"""
message_template = _default_message_template
+ section_sep = '\n\n'+'*'*75+'\n\n'
def __init__(self, app, contact_name=None, contact_email=None,
bug_tracker=None, show_crash_traceback=True, call_pdb=False):
@@ -96,16 +96,17 @@ def __init__(self, app, contact_name=None, contact_email=None,
further customization of the crash handler's behavior. Please see the
source for further details.
"""
+ self.crash_report_fname = "Crash_report_%s.txt" % app.name
self.app = app
- self.app_name = self.app.name
- self.contact_name = contact_name
- self.contact_email = contact_email
- self.bug_tracker = bug_tracker
- self.crash_report_fname = "Crash_report_%s.txt" % self.app_name
- self.show_crash_traceback = show_crash_traceback
- self.section_sep = '\n\n'+'*'*75+'\n\n'
self.call_pdb = call_pdb
#self.call_pdb = True # dbg
+ self.show_crash_traceback = show_crash_traceback
+ self.info = dict(app_name = app.name,
+ contact_name = contact_name,
+ contact_email = contact_email,
+ bug_tracker = bug_tracker,
+ crash_report_fname = self.crash_report_fname)
+
def __call__(self, etype, evalue, etb):
"""Handle an exception, call for compatible with sys.excepthook"""
@@ -148,8 +149,8 @@ def __call__(self, etype, evalue, etb):
return
# Inform user on stderr of what happened
- msg = itpl('\n'+'*'*70+'\n'+self.message_template)
- print >> sys.stderr, msg
+ print >> sys.stderr, '\n'+'*'*70+'\n'
+ print >> sys.stderr, self.message_template.format(**self.info)
# Construct report on disk
report.write(self.make_report(traceback))
View
@@ -51,7 +51,6 @@
from IPython.core import page
from IPython.core.prefilter import ESC_MAGIC
from IPython.lib.pylabtools import mpl_runner
-from IPython.external.Itpl import itpl, printpl
from IPython.testing.skipdoctest import skip_doctest
from IPython.utils.io import file_read, nlprint
from IPython.utils.path import get_py_filename
@@ -915,8 +914,7 @@ def type_name(v):
datalabel = 'Data/Info'
colsep = 3
# variable format strings
- vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
- vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
+ vformat = "{0:<{varwidth}}{1:<{typewidth}}"
aformat = "%s: %s elems, type `%s`, %s bytes"
# find the size of the columns to format the output nicely
varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
@@ -928,7 +926,7 @@ def type_name(v):
kb = 1024
Mb = 1048576 # kb**2
for vname,var,vtype in zip(varnames,varlist,typelist):
- print itpl(vformat),
+ print vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth),
if vtype in seq_types:
print "n="+str(len(var))
elif vtype in [array_type,ndarray_type]:
@@ -962,7 +960,7 @@ def type_name(v):
if len(vstr) < 50:
print vstr
else:
- printpl(vfmt_short)
+ print vstr[:25] + "<...>" + vstr[-25:]
def magic_reset(self, parameter_s=''):
"""Resets the namespace by removing all names defined by the user.
View
@@ -28,7 +28,6 @@
# IPython's own
from IPython.core import page
-from IPython.external.Itpl import itpl
from IPython.utils import PyColorize
from IPython.utils import io
from IPython.utils.text import indent
@@ -298,22 +297,24 @@ def pdoc(self,obj,oname='',formatter = None):
-formatter: a function to run the docstring through for specially
formatted docstrings."""
- head = self.__head # so that itpl can find it even if private
+ head = self.__head # For convenience
ds = getdoc(obj)
if formatter:
ds = formatter(ds)
if inspect.isclass(obj):
init_ds = getdoc(obj.__init__)
- output = itpl('$head("Class Docstring:")\n'
- '$indent(ds)\n'
- '$head("Constructor Docstring"):\n'
- '$indent(init_ds)')
+ output = "\n".join([head("Class Docstring:"),
+ indent(ds),
+ head("Constructor Docstring:"),
+ indent(init_ds)])
elif (type(obj) is types.InstanceType or isinstance(obj,object)) \
and hasattr(obj,'__call__'):
call_ds = getdoc(obj.__call__)
if call_ds:
- output = itpl('$head("Class Docstring:")\n$indent(ds)\n'
- '$head("Calling Docstring:")\n$indent(call_ds)')
+ output = "\n".join([head("Class Docstring:"),
+ indent(ds),
+ head("Calling Docstring:"),
+ indent(call_ds)])
else:
output = ds
else:
@@ -62,34 +62,9 @@
# Crash handler for this application
#-----------------------------------------------------------------------------
-_message_template = """\
-Oops, $self.app_name crashed. We do our best to make it stable, but...
-
-A crash report was automatically generated with the following information:
- - A verbatim copy of the crash traceback.
- - A copy of your input history during this session.
- - Data on your current $self.app_name configuration.
-
-It was left in the file named:
-\t'$self.crash_report_fname'
-If you can email this file to the developers, the information in it will help
-them in understanding and correcting the problem.
-
-You can mail it to: $self.contact_name at $self.contact_email
-with the subject '$self.app_name Crash Report'.
-
-If you want to do it now, the following command will work (under Unix):
-mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
-
-To ensure accurate tracking of this issue, please file a report about it at:
-$self.bug_tracker
-"""
-
class IPAppCrashHandler(CrashHandler):
"""sys.excepthook for IPython itself, leaves a detailed report on disk."""
- message_template = _message_template
-
def __init__(self, app):
contact_name = release.authors['Fernando'][0]
contact_email = release.authors['Fernando'][1]
View
@@ -17,8 +17,6 @@
import sys
import tempfile
-from IPython.external.Itpl import itpl, printpl
-
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
@@ -268,12 +266,13 @@ def __call__(self,lst,pos='',**kw):
for idx in range(start,stop):
elem = lst[idx]
+ newpos = pos + str(idx)
if type(elem)==type([]):
self.depth += 1
- self.__call__(elem,itpl('$pos$idx,'),**kw)
+ self.__call__(elem, newpos+",", **kw)
self.depth -= 1
else:
- printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
+ print(kw['indent']*self.depth + newpos + kw["sep"] + repr(elem))
nlprint = NLprinter()