Skip to content

Instantly share code, notes, and snippets.

@traverseda
Last active December 9, 2016 23:29
Show Gist options
  • Save traverseda/13f82406a277e36a71ebac5310c20177 to your computer and use it in GitHub Desktop.
Save traverseda/13f82406a277e36a71ebac5310c20177 to your computer and use it in GitHub Desktop.
For https://github.com/jonathanslenders/ptpython/issues/155 . Edit datastructures in yaml using vim.
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
import yaml, tempfile, subprocess, sys
def edit(data):
fd, temp_path = tempfile.mkstemp(prefix='punctual.')
with open(temp_path, "r+") as f:
f.write(yaml.safe_dump(data, default_flow_style=False, allow_unicode=True))
f.truncate()
f.close()
while True:
with open(temp_path, "r+") as f:
subprocess.check_call('vim -c "set syntax=yaml" ' + temp_path, shell=True)
f.close
try:
data = yaml.load(open(temp_path,"r+").read())
break
except Exception as E:
print(E)
input("Press Enter to continue...")
return data
edit({})
@traverseda
Copy link
Author

traverseda commented Dec 9, 2016

An example of some bad data for you

foo:
 - biz
 - baz
 - buz
 bar:
  - fiz
  - faz
  - fuz

And some good data

Foo:
- biz
- baz
- buz
- Bar:
  - fiz
  - faz
  - fuz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment