本文整理汇总了Python中ipython_genutils.py3compat.string_types方法的典型用法代码示例。如果您正苦于以下问题:Python py3compat.string_types方法的具体用法?Python py3compat.string_types怎么用?Python py3compat.string_types使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ipython_genutils.py3compat
的用法示例。
在下文中一共展示了py3compat.string_types方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: register
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def register(name=''):
"For backwards compatibility, we support @register(name) syntax."
def reg(widget):
"""A decorator registering a widget class in the widget registry."""
w = widget.class_traits()
Widget.widget_types.register(w['_model_module'].default_value,
w['_model_module_version'].default_value,
w['_model_name'].default_value,
w['_view_module'].default_value,
w['_view_module_version'].default_value,
w['_view_name'].default_value,
widget)
return widget
if isinstance(name, string_types):
import warnings
warnings.warn("Widget registration using a string name has been deprecated. Widget registration now uses a plain `@register` decorator.", DeprecationWarning)
return reg
else:
return reg(name)
示例2: array_to_binary_or_json
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def array_to_binary_or_json(ar, obj=None):
if ar is None:
return None
element = ar
dimension = 0
try:
while True:
element = element[0]
dimension += 1
except:
pass
try:
element = element.item() # for instance get back the value from array(1)
except:
pass
if isinstance(element, string_types):
return array_to_json(ar)
if dimension == 0: # scalars are passed as is (json)
return element
return [array_to_binary(ar)]
示例3: _import_mapping
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def _import_mapping(mapping, original=None):
"""Import any string-keys in a type mapping."""
#log = get_logger()
#log.debug("Importing canning map")
for key, value in list(mapping.items()):
if isinstance(key, string_types):
try:
cls = import_item(key)
except Exception:
if original and key not in original:
# only message on user-added classes
# log.error("canning class not importable: %r", key, exc_info=True)
print("ERROR: canning class not importable: %r", key, exc_info=True)
mapping.pop(key)
else:
mapping[cls] = mapping.pop(key)
示例4: can
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def can(obj):
"""Prepare an object for pickling."""
import_needed = False
for cls, canner in iteritems(can_map):
if isinstance(cls, string_types):
import_needed = True
break
elif istype(obj, cls):
return canner(obj)
if import_needed:
# perform can_map imports, then try again
# this will usually only happen once
_import_mapping(can_map, _original_can_map)
return can(obj)
return obj
示例5: uncan
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def uncan(obj, g=None):
"""Invert canning."""
import_needed = False
for cls, uncanner in iteritems(uncan_map):
if isinstance(cls, string_types):
import_needed = True
break
elif isinstance(obj, cls):
return uncanner(obj, g)
if import_needed:
# perform uncan_map imports, then try again
# this will usually only happen once
_import_mapping(uncan_map, _original_uncan_map)
return uncan(obj, g)
return obj
示例6: html2text
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def html2text(element):
"""extract inner text from html
Analog of jQuery's $(element).text()
"""
if isinstance(element, py3compat.string_types):
try:
element = ElementTree.fromstring(element)
except Exception:
# failed to parse, just return it unmodified
return element
text = element.text or ""
for child in element:
text += html2text(child)
text += (element.tail or "")
return text
示例7: get_color
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def get_color(self, color, intensity=0):
""" Returns a QColor for a given color code or rgb list, or None if one
cannot be constructed.
"""
if isinstance(color, int):
# Adjust for intensity, if possible.
if color < 8 and intensity > 0:
color += 8
constructor = self.color_map.get(color, None)
elif isinstance(color, (tuple, list)):
constructor = color
else:
return None
if isinstance(constructor, string_types):
# If this is an X11 color name, we just hope there is a close SVG
# color name. We could use QColor's static method
# 'setAllowX11ColorNames()', but this is global and only available
# on X11. It seems cleaner to aim for uniformity of behavior.
return QtGui.QColor(constructor)
elif isinstance(constructor, (tuple, list)):
return QtGui.QColor(*constructor)
return None
示例8: set_style
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def set_style(self, style):
""" Sets the style to the specified Pygments style.
"""
if isinstance(style, string_types):
style = get_style_by_name(style)
self._style = style
self._clear_caches()
示例9: widget_from_single_value
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def widget_from_single_value(o):
"""Make widgets from single values, which can be used as parameter defaults."""
if isinstance(o, string_types):
return Text(value=unicode_type(o))
elif isinstance(o, bool):
return Checkbox(value=o)
elif isinstance(o, Integral):
min, max, value = _get_min_max_value(None, None, o)
return IntSlider(value=o, min=min, max=max)
elif isinstance(o, Real):
min, max, value = _get_min_max_value(None, None, o)
return FloatSlider(value=o, min=min, max=max)
else:
return None
示例10: _json_to_widget
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def _json_to_widget(x, obj):
if isinstance(x, dict):
return {k: _json_to_widget(v, obj) for k, v in x.items()}
elif isinstance(x, (list, tuple)):
return [_json_to_widget(v, obj) for v in x]
elif isinstance(x, string_types) and x.startswith('IPY_MODEL_') and x[10:] in Widget.widgets:
return Widget.widgets[x[10:]]
else:
return x
示例11: array_sequence_to_binary_or_json
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def array_sequence_to_binary_or_json(ar, obj=None):
if ar is None:
return None
element = ar
dimension = 0
try:
while True:
element = element[0]
dimension += 1
except:
pass
try:
element = element.item() # for instance get back the value from array(1)
except:
pass
if isinstance(element, string_types):
return array_to_json(ar)
if dimension == 0: # scalars are passed as is (json), empty lists as well
if isinstance(element, np.ndarray): # must be an empty list
return []
else:
return element
if isinstance(ar, (list, tuple, np.ndarray)): # ok, at least 1d
if isinstance(ar[0], (list, tuple, np.ndarray)): # ok, 2d
return [array_to_binary(ar[k]) for k in range(len(ar))]
else:
return [array_to_binary(ar)]
else:
raise ValueError("Expected a sequence, got %r", ar)
示例12: color_to_binary_or_json
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def color_to_binary_or_json(ar, obj=None):
if ar is None:
return None
element = ar
dimension = 0
try:
while True:
element = element[0]
dimension += 1
except:
pass
try:
element = element.item() # for instance get back the str from array('foo')
except:
pass
if isinstance(element, string_types):
return array_to_json(ar)
if dimension == 0: # scalars are passed as is (json)
return ar
if ar.ndim > 1 and ar.shape[-1] == 3:
# we add an alpha channel
ones = np.ones(ar.shape[:-1])
ar = np.stack([ar[..., 0], ar[..., 1], ar[..., 2], ones], axis=-1)
elif ar.ndim > 1 and ar.shape[-1] != 4:
raise ValueError('array should be of shape (...,3) or (...,4), not %r' % (ar.shape,))
if dimension == 3:
return [array_to_binary(ar[k]) for k in range(len(ar))]
else:
return [array_to_binary(ar)]
示例13: register_target
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def register_target(self, target_name, f):
"""Register a callable f for a given target name
f will be called with two arguments when a comm_open message is
received with `target`:
- the Comm instance
- the `comm_open` message itself.
f can be a Python callable or an import string for one.
"""
if isinstance(f, string_types):
f = import_item(f)
self.targets[target_name] = f
示例14: __init__
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def __init__(self, name):
if not isinstance(name, string_types):
raise TypeError("illegal name: %r" % name)
self.name = name
self.buffers = []
示例15: register_preprocessor
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import string_types [as 别名]
def register_preprocessor(self, preprocessor, enabled=False):
"""
Register a preprocessor.
Preprocessors are classes that act upon the notebook before it is
passed into the Jinja templating engine. preprocessors are also
capable of passing additional information to the Jinja
templating engine.
Parameters
----------
preprocessor : :class:`~nbconvert.preprocessors.Preprocessor`
A dotted module name, a type, or an instance
enabled : bool
Mark the preprocessor as enabled
"""
if preprocessor is None:
raise TypeError('preprocessor must not be None')
isclass = isinstance(preprocessor, type)
constructed = not isclass
# Handle preprocessor's registration based on it's type
if constructed and isinstance(preprocessor, py3compat.string_types):
# Preprocessor is a string, import the namespace and recursively call
# this register_preprocessor method
preprocessor_cls = import_item(preprocessor)
return self.register_preprocessor(preprocessor_cls, enabled)
if constructed and hasattr(preprocessor, '__call__'):
# Preprocessor is a function, no need to construct it.
# Register and return the preprocessor.
if enabled:
preprocessor.enabled = True
self._preprocessors.append(preprocessor)
return preprocessor
elif isclass and issubclass(preprocessor, HasTraits):
# Preprocessor is configurable. Make sure to pass in new default for
# the enabled flag if one was specified.
self.register_preprocessor(preprocessor(parent=self), enabled)
elif isclass:
# Preprocessor is not configurable, construct it
self.register_preprocessor(preprocessor(), enabled)
else:
# Preprocessor is an instance of something without a __call__
# attribute.
raise TypeError('preprocessor must be callable or an importable constructor, got %r' % preprocessor)