Esempio n. 1
0
def restore(fname, top_level=True, _larch=None):
    """restore data from a json Larch save file

    Arguments
    ---------
    top_level  bool  whether to restore to _main [True]


    Returns
    -------
    None   with `top_level=True` or group with `top_level=False`

    Notes
    -----
    1.  With top_level=False, a new group containing the
        recovered data will be returned.
    """

    grouplist = _larch.symtable._sys.saverestore_groups

    datalines = open(fname, 'r').readlines()
    line1 = datalines.pop(0)
    if not line1.startswith("#Larch Save File:"):
        raise ValueError("%s is not a valid Larch save file" % fname)
    version_string = line1.split(':')[1].strip()
    version_info = [s for s in version_string.split('.')]

    ivar = 0
    header = {'version': version_info}
    varnames = []
    gname = fix_varname('restore_%s' % fname)
    out = Group(name=gname)
    for line in datalines:
        line = line[:-1]
        if line.startswith('#save.'):
            key, value = line[6:].split(':', 1)
            value = value.strip()
            if key == 'nitems': value = int(value)
            header[key] = value
        elif line.startswith('#=>'):
            name = fix_varname(line[4:].strip())
            ivar += 1
            if name in (None, 'None', '__unknown__') or name in varnames:
                name = 'var_%5.5i' % (ivar)
            varnames.append(name)
        else:
            val = decode4js(json.loads(line), grouplist)
            setattr(out, varnames[-1], val)
    setattr(out, '_restore_metadata_', header)

    if top_level:
        _main = _larch.symtable
        for objname in dir(out):
            setattr(_main, objname, getattr(out, objname))
        return
    return out
Esempio n. 2
0
def restore(fname, top_level=True, _larch=None):
    """restore data from a json Larch save file

    Arguments
    ---------
    top_level  bool  whether to restore to _main [True]


    Returns
    -------
    None   with `top_level=True` or group with `top_level=False`

    Notes
    -----
    1.  With top_level=False, a new group containing the
        recovered data will be returned.
    """

    grouplist = _larch.symtable._sys.saverestore_groups

    datalines = open(fname, 'r').readlines()
    line1 = datalines.pop(0)
    if not line1.startswith("#Larch Save File:"):
        raise ValueError("%s is not a valid Larch save file" % fname)
    version_string = line1.split(':')[1].strip()
    version_info = [s for s in version_string.split('.')]

    ivar = 0
    header = {'version': version_info}
    varnames = []
    gname = fix_varname('restore_%s' % fname)
    out = Group(name=gname)
    for line in datalines:
        line = line[:-1]
        if line.startswith('#save.'):
            key, value = line[6:].split(':', 1)
            value = value.strip()
            if key == 'nitems': value = int(value)
            header[key] = value
        elif line.startswith('#=>'):
            name = fix_varname(line[4:].strip())
            ivar += 1
            if name in (None, 'None', '__unknown__') or name in varnames:
                name = 'var_%5.5i' % (ivar)
            varnames.append(name)
        else:
            val = decode4js(json.loads(line), grouplist)
            setattr(out, varnames[-1], val)
    setattr(out, '_restore_metadata_', header)

    if top_level:
        _main = _larch.symtable
        for objname in dir(out):
            setattr(_main, objname, getattr(out, objname))
        return
    return out
Esempio n. 3
0
import xmlrpclib
import time
import json
from larch.utils.jsonutils import decode4js
s = xmlrpclib.ServerProxy('http://127.0.0.1:4966')

print 'Avaialable Methods from XML-RPC server: ', s.system.listMethods()
s.larch('m = 222.3')

s.larch('g = group(x=linspace(0, 10, 11))')
s.larch('g.z = cos(g.x)')

# show and print will be done in server process of course!!!
s.larch('show(g)')

s.larch('print g.z[3:10]')

print '== Messages:'
print s.get_messages()
print '=='


gx  = decode4js(s.get_data('g.z'))
print 'm = ', s.get_data('m')
print 'x = ', s.get_data('x')

print 'gx = ',  gx, type(gx), gx.dtype

# could tell server to exit!
# s.exit()
Esempio n. 4
0
def deval(obj):
    out = decode4js(encode(obj))
    print("Decoded to: ", out)
    return out
Esempio n. 5
0
def deval(obj):
    out = decode4js(encode(obj))
    print("Decoded to: ", out)
    return out