Giter VIP home page Giter VIP logo

yamlmagic's Introduction

yamlmagic

an IPython magic for capturing data in YAML into a running IPython kernel.

Build Status pypi

Install

From the command line (or with ! in a notebook cell):

pip install yamlmagic

Enable

Ad-hoc

In the notebook, you can use the %load_ext or %reload_ext line magic.

%reload_ext yamlmagic

Configuration

In your profile's ipython_kernel_config.py, you can add the following line to automatically load yamlmagic into all your running kernels:

c.InteractiveShellApp.extensions = ['yaml_magic']

Use

The %%yaml cell magic will either act as simple parser:

%%yaml
a_toplevel_key: 1
<IPython.core.display.Javascript object>
{'a_toplevel_key': 1}

which can be accessed by the special last result variable _:

_
{'a_toplevel_key': 1}

Or will update a named variable with the parsed document:

%%yaml x
- a: 1
  b: 2
<IPython.core.display.Javascript object>
x
[{'a': 1, 'b': 2}]

By default, yaml.SafeLoader will be used, which won't allow the powerful but dangerous (and unportable) `!python/ tags <http://pyyaml.org/wiki/PyYAMLDocumentation#YAMLtagsandPythontypes>`__. If you'd like to use them, provide the -l (or --loader) argument with a BaseLoader subclass available via a local variable...

from yaml import Loader
class FooLoader(Loader):
    # some special things you have built
    pass
%%yaml --loader FooLoader
!!python/float 0
<IPython.core.display.Javascript object>
0.0

...or dotted-notation path to a loader:

%%yaml --loader yaml.Loader
!!python/float 0
<IPython.core.display.Javascript object>
0.0

Contribute

Issues and pull requests welcome!

License

yamlmagic is released as free software under the BSD 3-Clause license.

Thank

yamlmagic's People

Contributors

bollwyvl avatar

Stargazers

Mart van Rijthoven avatar Gábor Vecsei avatar Mikael Lund avatar  avatar Robert Clewley avatar Daniel Mahler avatar Philip Douglass avatar Tony Fast avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

josejg

yamlmagic's Issues

Flag to assign values in both kernel and client

Yaml cell magic is only going to be used in the notebook. It is an efficient serialization for both Python and Javascript values. Would it be possible to make a flag that assigns the Yaml values in both the kernel and client. The variables are useful both in compute and presentation.

Setting Nested Global Values

It would be rad to set nested values in existing variables.

%%yaml foo.bar.0
%%yaml foo.bar.11.baz
%%yaml things.metadata

code in the readme leads to TypeError: Item in ``from list'' not a string

I get this error when running the last command

%%yaml --loader yaml.Loader
!!python/float 0

from the readme. this is the complete error output. some idea what could be wrong?
otherwise I'm gonna try it out more...
are you not using ruamel.yaml?

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-ba569f150714> in <module>()
----> 1 get_ipython().run_cell_magic(u'yaml', u'--loader yaml.Loader', u'!!python/float 0')

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2101             magic_arg_s = self.var_expand(line, stack_depth)
   2102             with self.builtin_trap:
-> 2103                 result = fn(magic_arg_s, cell)
   2104             return result
   2105 

/Users/git/venv/lib/python2.7/site-packages/yamlmagic.pyc in yaml(self, line, cell)

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

/Users/git/venv/lib/python2.7/site-packages/yamlmagic.pyc in yaml(self, line, cell)
     71         loader = get_ipython().user_global_ns.get(args.loader, None)
     72         if loader is None:
---> 73             loader = import_item(args.loader)
     74 
     75         try:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/utils/importstring.pyc in import_item(name)
     29         # called with 'foo.bar....'
     30         package, obj = parts
---> 31         module = __import__(package, fromlist=[obj])
     32         try:
     33             pak = getattr(module, obj)

TypeError: Item in ``from list'' not a string

Decorator for new Yaml Loaders

Currently, adding a constructor requires

  1. Initializing a loader class

    class Loader(yaml.Loader):
         pass
    
  2. Create a function to map the yaml parser

    def resolve_constructor( loader, node ):
       data = loader.construct_mapping(node, deep=True)
       return f(data)
    
  3. Register a constructor

    Loader.add_constructor("!resolve", resolve_constructor)
    

I think with decorators this process could be compressed to something like with this package.

    import yamlmagic
    loader = yamlmagic.Loader

    @loader.add_constructor('resolve')
    def resolve_constructor(data):
         return f(data)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.