我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用traitlets.Unicode()。
def test_cli_priority(): with TemporaryDirectory() as td: class TestApp(BaseIPythonApplication): test = Unicode().tag(config=True) # Create the config file, so it tries to load it. with open(os.path.join(td, 'ipython_config.py'), "w") as f: f.write("c.TestApp.test = 'config file'") app = TestApp() app.initialize(['--profile-dir', td]) nt.assert_equal(app.test, 'config file') app = TestApp() app.initialize(['--profile-dir', td, '--TestApp.test=cli']) nt.assert_equal(app.test, 'cli')
def make_layout(layout=None, **kwargs): from ipywidgets import Layout import traitlets if layout is None: layout = Layout() for key, val in kwargs.items(): # note that this is the type of the class descriptor, not the instance attribute if isinstance(getattr(Layout, key), traitlets.Unicode): val = in_pixels(val) setattr(layout, key, val) return layout
def test_hastraits_defaults(): class Foo(jst.JSONHasTraits): _additional_traits = [T.Integer()] name = T.Unicode() f = Foo(name="Bob", age=40) f.set_trait('year', 2000) assert set(f.trait_names()) == {'name', 'age', 'year'} with pytest.raises(T.TraitError): f.set_trait('foo', 'abc') with pytest.raises(T.TraitError): f.set_trait('age', 'blah')
def test_no_defaults(): class Foo(jst.JSONHasTraits): _additional_traits = False name = T.Unicode() with pytest.raises(T.TraitError): Foo(name="Sarah", year=2000)
def test_AnyOfObject(): class Foo(jst.JSONHasTraits): intval = T.Integer() flag = T.Bool() class Bar(jst.JSONHasTraits): strval = T.Unicode() flag = T.Bool() class FooBar(jst.AnyOfObject): _classes = [Foo, Bar] FooBar(strval='hello', flag=True) FooBar(intval=5, flag=True) with pytest.raises(T.TraitError): FooBar(strval=666, flag=False) with pytest.raises(T.TraitError): FooBar(strval='hello', flag='bad arg') with pytest.raises(T.TraitError): FooBar(intval='bad arg', flag=False) with pytest.raises(T.TraitError): FooBar(intval=42, flag='bad arg') # Test from_dict FooBar.from_dict({'strval': 'hello', 'flag': True}) FooBar.from_dict({'intval': 42, 'flag': False})
def run(): try: try: # IPython 4.x from traitlets import Unicode from qtconsole.rich_jupyter_widget import RichIPythonWidget from qtconsole.qtconsoleapp import IPythonQtConsoleApp # TODO: check if we can/should set IPythonQtConsoleApp.version except: # IPython <4.x from IPython.utils.traitlets import Unicode from IPython.qt.console.rich_ipython_widget import RichIPythonWidget from IPython.qt.console.qtconsoleapp import IPythonQtConsoleApp IPythonQtConsoleApp.version.default_value = release.version class SpockConsole(RichIPythonWidget): banner = Unicode(config=True) def _banner_default(self): config = get_config() return config.FrontendWidget.banner IPythonQtConsoleApp.widget_factory = SpockConsole except ImportError: pass try: check_requirements() except exception.SpockMissingRequirement, requirement: print str(requirement) sys.exit(-1) except exception.SpockMissingRecommended, recommended: print str(recommended) prepare_input_handler() prepare_cmdline() launch_new_instance()
def patch_qt_console(): """Overwrite the original IPython Qt widget with our own so we can put a customized banner. Return the customized application class. """ # Traitlets imports try: # IPython 4.x from traitlets import Unicode except ImportError: # IPython < 4.x from IPython.utils.traitlets import Unicode # Qt concole imports try: from qtconsole import rich_ipython_widget from qtconsole import qtconsoleapp except ImportError: from IPython.qt.console import rich_ipython_widget from IPython.qt.console import qtconsoleapp # Patch widget factory try: WidgetClass = rich_ipython_widget.RichJupyterWidget except AttributeError: WidgetClass = rich_ipython_widget.RichIPythonWidget # Custom widget class class ITangoConsole(WidgetClass): banner = Unicode(config=True) def _banner_default(self): config = get_config() banner = config.ITangoConsole.banner return banner if isinstance(banner, str) else '' # Patch widget factory try: AppClass = qtconsoleapp.JupyterQtConsoleApp except AttributeError: AppClass = qtconsoleapp.IPythonQtConsoleApp AppClass.widget_factory = ITangoConsole # Return patched application class return AppClass