Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Str and Bytes traitlets #462

Merged
merged 7 commits into from Jun 22, 2011
@@ -28,7 +28,7 @@
)
from IPython.utils.traitlets import (
- Int, Float, Str
+ Int, Float, Unicode
)
from IPython.config.loader import Config
@@ -42,7 +42,7 @@
class MyConfigurable(Configurable):
a = Int(1, config=True, help="The integer a.")
b = Float(1.0, config=True, help="The integer b.")
- c = Str('no config')
+ c = Unicode('no config')
mc_help=u"""MyConfigurable options
@@ -56,11 +56,11 @@ class MyConfigurable(Configurable):
class Foo(Configurable):
a = Int(0, config=True, help="The integer a.")
- b = Str('nope', config=True)
+ b = Unicode('nope', config=True)
class Bar(Foo):
- b = Str('gotit', config=False, help="The string b.")
+ b = Unicode('gotit', config=False, help="The string b.")
c = Float(config=True, help="The string c.")
View
@@ -28,7 +28,7 @@
# Our own imports
from IPython.config.configurable import Configurable
from IPython.lib import pretty
-from IPython.utils.traitlets import Bool, Dict, Int, Str, CStr
+from IPython.utils.traitlets import Bool, Dict, Int, Unicode, CUnicode, ObjectName
#-----------------------------------------------------------------------------
@@ -191,11 +191,11 @@ class BaseFormatter(Configurable):
returned and this format type is not used.
"""
- format_type = Str('text/plain')
+ format_type = Unicode('text/plain')
enabled = Bool(True, config=True)
- print_method = Str('__repr__')
+ print_method = ObjectName('__repr__')
# The singleton printers.
# Maps the IDs of the builtin singleton objects to the format functions.
@@ -337,14 +337,14 @@ def dtype_pprinter(obj, p, cycle):
"""
# The format type of data returned.
- format_type = Str('text/plain')
+ format_type = Unicode('text/plain')
# This subclass ignores this attribute as it always need to return
# something.
enabled = Bool(True, config=False)
# Look for a _repr_pretty_ methods to use for pretty printing.
- print_method = Str('_repr_pretty_')
+ print_method = ObjectName('_repr_pretty_')
# Whether to pretty-print or not.
pprint = Bool(True, config=True)
@@ -356,12 +356,12 @@ def dtype_pprinter(obj, p, cycle):
max_width = Int(79, config=True)
# The newline character.
- newline = Str('\n', config=True)
+ newline = Unicode('\n', config=True)
# format-string for pprinting floats
- float_format = Str('%r')
+ float_format = Unicode('%r')
# setter for float precision, either int or direct format-string
- float_precision = CStr('', config=True)
+ float_precision = CUnicode('', config=True)
def _float_precision_changed(self, name, old, new):
"""float_precision changed, set float_format accordingly.
@@ -454,9 +454,9 @@ class HTMLFormatter(BaseFormatter):
could be injected into an existing DOM. It should *not* include the
```<html>`` or ```<body>`` tags.
"""
- format_type = Str('text/html')
+ format_type = Unicode('text/html')
- print_method = Str('_repr_html_')
+ print_method = ObjectName('_repr_html_')
class SVGFormatter(BaseFormatter):
@@ -471,9 +471,9 @@ class SVGFormatter(BaseFormatter):
```<svg>``` tags, that could be injected into an existing DOM. It should
*not* include the ```<html>`` or ```<body>`` tags.
"""
- format_type = Str('image/svg+xml')
+ format_type = Unicode('image/svg+xml')
- print_method = Str('_repr_svg_')
+ print_method = ObjectName('_repr_svg_')
class PNGFormatter(BaseFormatter):
@@ -487,9 +487,9 @@ class PNGFormatter(BaseFormatter):
The return value of this formatter should be raw PNG data, *not*
base64 encoded.
"""
- format_type = Str('image/png')
+ format_type = Unicode('image/png')
- print_method = Str('_repr_png_')
+ print_method = ObjectName('_repr_png_')
class LatexFormatter(BaseFormatter):
@@ -503,9 +503,9 @@ class LatexFormatter(BaseFormatter):
The return value of this formatter should be a valid LaTeX equation,
enclosed in either ```$``` or ```$$```.
"""
- format_type = Str('text/latex')
+ format_type = Unicode('text/latex')
- print_method = Str('_repr_latex_')
+ print_method = ObjectName('_repr_latex_')
class JSONFormatter(BaseFormatter):
@@ -518,9 +518,9 @@ class JSONFormatter(BaseFormatter):
The return value of this formatter should be a valid JSON string.
"""
- format_type = Str('application/json')
+ format_type = Unicode('application/json')
- print_method = Str('_repr_json_')
+ print_method = ObjectName('_repr_json_')
class JavascriptFormatter(BaseFormatter):
@@ -534,9 +534,9 @@ class JavascriptFormatter(BaseFormatter):
The return value of this formatter should be valid Javascript code and
should *not* be enclosed in ```<script>``` tags.
"""
- format_type = Str('application/javascript')
+ format_type = Unicode('application/javascript')
- print_method = Str('_repr_javascript_')
+ print_method = ObjectName('_repr_javascript_')
FormatterABC.register(BaseFormatter)
FormatterABC.register(PlainTextFormatter)
@@ -70,7 +70,7 @@
from IPython.utils.strdispatch import StrDispatch
from IPython.utils.syspathcontext import prepended_to_syspath
from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
-from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
+from IPython.utils.traitlets import (Int, CBool, CaselessStrEnum, Enum,
List, Unicode, Instance, Type)
from IPython.utils.warn import warn, error, fatal
import IPython.core.hooks
@@ -122,16 +122,16 @@ def get_default_colors():
return 'Linux'
-class SeparateStr(Str):
- """A Str subclass to validate separate_in, separate_out, etc.
+class SeparateUnicode(Unicode):
+ """A Unicode subclass to validate separate_in, separate_out, etc.
- This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
+ This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
"""
def validate(self, obj, value):
if value == '0': value = ''
value = value.replace('\\n','\n')
- return super(SeparateStr, self).validate(obj, value)
+ return super(SeparateUnicode, self).validate(obj, value)
class ReadlineNoRecord(object):
@@ -294,9 +294,9 @@ def _exiter_default(self):
"""
)
- prompt_in1 = Str('In [\\#]: ', config=True)
- prompt_in2 = Str(' .\\D.: ', config=True)
- prompt_out = Str('Out[\\#]: ', config=True)
+ prompt_in1 = Unicode('In [\\#]: ', config=True)
+ prompt_in2 = Unicode(' .\\D.: ', config=True)
+ prompt_out = Unicode('Out[\\#]: ', config=True)
prompts_pad_left = CBool(True, config=True)
quiet = CBool(False, config=True)
@@ -307,7 +307,7 @@ def _exiter_default(self):
readline_use = CBool(True, config=True)
readline_merge_completions = CBool(True, config=True)
readline_omit__names = Enum((0,1,2), default_value=2, config=True)
- readline_remove_delims = Str('-/~', config=True)
+ readline_remove_delims = Unicode('-/~', config=True)
# don't use \M- bindings by default, because they
# conflict with 8-bit encodings. See gh-58,gh-88
readline_parse_and_bind = List([
@@ -327,9 +327,9 @@ def _exiter_default(self):
# TODO: this part of prompt management should be moved to the frontends.
# Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
- separate_in = SeparateStr('\n', config=True)
- separate_out = SeparateStr('', config=True)
- separate_out2 = SeparateStr('', config=True)
+ separate_in = SeparateUnicode('\n', config=True)
+ separate_out = SeparateUnicode('', config=True)
+ separate_out2 = SeparateUnicode('', config=True)
wildcards_case_sensitive = CBool(True, config=True)
xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
default_value='Context', config=True)
@@ -1670,7 +1670,8 @@ def init_readline(self):
# Remove some chars from the delimiters list. If we encounter
# unicode chars, discard them.
delims = readline.get_completer_delims().encode("ascii", "ignore")
- delims = delims.translate(None, self.readline_remove_delims)
+ for d in self.readline_remove_delims:
+ delims = delims.replace(d, "")
delims = delims.replace(ESC_MAGIC, '')
readline.set_completer_delims(delims)
# otherwise we end up with a monster history after a while:
View
@@ -3472,25 +3472,25 @@ def magic_precision(self, s=''):
In [1]: from math import pi
In [2]: %precision 3
- Out[2]: '%.3f'
+ Out[2]: u'%.3f'
In [3]: pi
Out[3]: 3.142
In [4]: %precision %i
- Out[4]: '%i'
+ Out[4]: u'%i'
In [5]: pi
Out[5]: 3
In [6]: %precision %e
- Out[6]: '%e'
+ Out[6]: u'%e'
In [7]: pi**10
Out[7]: 9.364805e+04
In [8]: %precision
- Out[8]: '%r'
+ Out[8]: u'%r'
In [9]: pi**10
Out[9]: 93648.047476082982
View
@@ -36,7 +36,7 @@
from IPython.core.splitinput import split_user_input
from IPython.core import page
-from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool, Instance
+from IPython.utils.traitlets import List, Int, Any, Unicode, CBool, Bool, Instance
from IPython.utils.text import make_quoted_expr
from IPython.utils.autoattr import auto_attr
@@ -756,7 +756,7 @@ def check(self, line_info):
class PrefilterHandler(Configurable):
- handler_name = Str('normal')
+ handler_name = Unicode('normal')
esc_strings = List([])
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
@@ -797,7 +797,7 @@ def __str__(self):
class AliasHandler(PrefilterHandler):
- handler_name = Str('alias')
+ handler_name = Unicode('alias')
def handle(self, line_info):
"""Handle alias input lines. """
@@ -812,7 +812,7 @@ def handle(self, line_info):
class ShellEscapeHandler(PrefilterHandler):
- handler_name = Str('shell')
+ handler_name = Unicode('shell')
esc_strings = List([ESC_SHELL, ESC_SH_CAP])
def handle(self, line_info):
@@ -839,7 +839,7 @@ def handle(self, line_info):
class MacroHandler(PrefilterHandler):
- handler_name = Str("macro")
+ handler_name = Unicode("macro")
def handle(self, line_info):
obj = self.shell.user_ns.get(line_info.ifun)
@@ -850,7 +850,7 @@ def handle(self, line_info):
class MagicHandler(PrefilterHandler):
- handler_name = Str('magic')
+ handler_name = Unicode('magic')
esc_strings = List([ESC_MAGIC])
def handle(self, line_info):
@@ -864,7 +864,7 @@ def handle(self, line_info):
class AutoHandler(PrefilterHandler):
- handler_name = Str('auto')
+ handler_name = Unicode('auto')
esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
def handle(self, line_info):
@@ -924,7 +924,7 @@ def handle(self, line_info):
class HelpHandler(PrefilterHandler):
- handler_name = Str('help')
+ handler_name = Unicode('help')
esc_strings = List([ESC_HELP])
def handle(self, line_info):
@@ -962,7 +962,7 @@ def handle(self, line_info):
class EmacsHandler(PrefilterHandler):
- handler_name = Str('emacs')
+ handler_name = Unicode('emacs')
esc_strings = List([])
def handle(self, line_info):
@@ -448,15 +448,15 @@ def doctest_precision():
In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain']
In [2]: %precision 5
- Out[2]: '%.5f'
+ Out[2]: u'%.5f'
In [3]: f.float_format
- Out[3]: '%.5f'
+ Out[3]: u'%.5f'
In [4]: %precision %e
- Out[4]: '%e'
+ Out[4]: u'%e'
In [5]: f(3.1415927)
- Out[5]: '3.141593e+00'
+ Out[5]: u'3.141593e+00'
"""
Oops, something went wrong.