mirror of
				https://github.com/fjosw/pyerrors.git
				synced 2025-11-04 01:25:46 +01:00 
			
		
		
		
	Merge pull request #26 from fjosw/feature/import_json_string
Feature/import json string
This commit is contained in:
		
				commit
				
					
						ca4548b105
					
				
			
		
					 2 changed files with 53 additions and 28 deletions
				
			
		| 
						 | 
					@ -203,20 +203,18 @@ def dump_to_json(ol, fname, description='', indent=1, gz=True):
 | 
				
			||||||
    fp.close()
 | 
					    fp.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def load_json(fname, verbose=True, gz=True, full_output=False):
 | 
					def import_json_string(json_string, verbose=True, full_output=False):
 | 
				
			||||||
    """Import a list of Obs or structures containing Obs from a .json.gz file.
 | 
					    """Reconstruct a list of Obs or structures containing Obs from a json string.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    The following structures are supported: Obs, list, numpy.ndarray
 | 
					    The following structures are supported: Obs, list, numpy.ndarray
 | 
				
			||||||
    If the list contains only one element, it is unpacked from the list.
 | 
					    If the list contains only one element, it is unpacked from the list.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Parameters
 | 
					    Parameters
 | 
				
			||||||
    ----------
 | 
					    ----------
 | 
				
			||||||
    fname : str
 | 
					    json_string : str
 | 
				
			||||||
        Filename of the input file.
 | 
					        json string containing the data.
 | 
				
			||||||
    verbose : bool
 | 
					    verbose : bool
 | 
				
			||||||
        Print additional information that was written to the file.
 | 
					        Print additional information that was written to the file.
 | 
				
			||||||
    gz : bool
 | 
					 | 
				
			||||||
        If True, assumes that data is gzipped. If False, assumes JSON file.
 | 
					 | 
				
			||||||
    full_output : bool
 | 
					    full_output : bool
 | 
				
			||||||
        If True, a dict containing auxiliary information and the data is returned.
 | 
					        If True, a dict containing auxiliary information and the data is returned.
 | 
				
			||||||
        If False, only the data is returned.
 | 
					        If False, only the data is returned.
 | 
				
			||||||
| 
						 | 
					@ -281,35 +279,22 @@ def load_json(fname, verbose=True, gz=True, full_output=False):
 | 
				
			||||||
            ret[-1].tag = taglist[i]
 | 
					            ret[-1].tag = taglist[i]
 | 
				
			||||||
        return np.reshape(ret, layout)
 | 
					        return np.reshape(ret, layout)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if not fname.endswith('.json') and not fname.endswith('.gz'):
 | 
					    prog = json_string.get('program', '')
 | 
				
			||||||
        fname += '.json'
 | 
					    version = json_string.get('version', '')
 | 
				
			||||||
    if gz:
 | 
					    who = json_string.get('who', '')
 | 
				
			||||||
        if not fname.endswith('.gz'):
 | 
					    date = json_string.get('date', '')
 | 
				
			||||||
            fname += '.gz'
 | 
					    host = json_string.get('host', '')
 | 
				
			||||||
        with gzip.open(fname, 'r') as fin:
 | 
					 | 
				
			||||||
            d = json.loads(fin.read().decode('utf-8'))
 | 
					 | 
				
			||||||
    else:
 | 
					 | 
				
			||||||
        if fname.endswith('.gz'):
 | 
					 | 
				
			||||||
            warnings.warn("Trying to read from %s without unzipping!" % fname, UserWarning)
 | 
					 | 
				
			||||||
        with open(fname, 'r', encoding='utf-8') as fin:
 | 
					 | 
				
			||||||
            d = json.loads(fin.read())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    prog = d.get('program', '')
 | 
					 | 
				
			||||||
    version = d.get('version', '')
 | 
					 | 
				
			||||||
    who = d.get('who', '')
 | 
					 | 
				
			||||||
    date = d.get('date', '')
 | 
					 | 
				
			||||||
    host = d.get('host', '')
 | 
					 | 
				
			||||||
    if prog and verbose:
 | 
					    if prog and verbose:
 | 
				
			||||||
        print('Data has been written using %s.' % (prog))
 | 
					        print('Data has been written using %s.' % (prog))
 | 
				
			||||||
    if version and verbose:
 | 
					    if version and verbose:
 | 
				
			||||||
        print('Format version %s' % (version))
 | 
					        print('Format version %s' % (version))
 | 
				
			||||||
    if np.any([who, date, host] and verbose):
 | 
					    if np.any([who, date, host] and verbose):
 | 
				
			||||||
        print('Written by %s on %s on host %s' % (who, date, host))
 | 
					        print('Written by %s on %s on host %s' % (who, date, host))
 | 
				
			||||||
    description = d.get('description', '')
 | 
					    description = json_string.get('description', '')
 | 
				
			||||||
    if description and verbose:
 | 
					    if description and verbose:
 | 
				
			||||||
        print()
 | 
					        print()
 | 
				
			||||||
        print('Description: ', description)
 | 
					        print('Description: ', description)
 | 
				
			||||||
    obsdata = d['obsdata']
 | 
					    obsdata = json_string['obsdata']
 | 
				
			||||||
    ol = []
 | 
					    ol = []
 | 
				
			||||||
    for io in obsdata:
 | 
					    for io in obsdata:
 | 
				
			||||||
        if io['type'] == 'Obs':
 | 
					        if io['type'] == 'Obs':
 | 
				
			||||||
| 
						 | 
					@ -335,3 +320,37 @@ def load_json(fname, verbose=True, gz=True, full_output=False):
 | 
				
			||||||
            ol = ol[0]
 | 
					            ol = ol[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return ol
 | 
					        return ol
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def load_json(fname, verbose=True, gz=True, full_output=False):
 | 
				
			||||||
 | 
					    """Import a list of Obs or structures containing Obs from a .json.gz file.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    The following structures are supported: Obs, list, numpy.ndarray
 | 
				
			||||||
 | 
					    If the list contains only one element, it is unpacked from the list.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Parameters
 | 
				
			||||||
 | 
					    ----------
 | 
				
			||||||
 | 
					    fname : str
 | 
				
			||||||
 | 
					        Filename of the input file.
 | 
				
			||||||
 | 
					    verbose : bool
 | 
				
			||||||
 | 
					        Print additional information that was written to the file.
 | 
				
			||||||
 | 
					    gz : bool
 | 
				
			||||||
 | 
					        If True, assumes that data is gzipped. If False, assumes JSON file.
 | 
				
			||||||
 | 
					    full_output : bool
 | 
				
			||||||
 | 
					        If True, a dict containing auxiliary information and the data is returned.
 | 
				
			||||||
 | 
					        If False, only the data is returned.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    if not fname.endswith('.json') and not fname.endswith('.gz'):
 | 
				
			||||||
 | 
					        fname += '.json'
 | 
				
			||||||
 | 
					    if gz:
 | 
				
			||||||
 | 
					        if not fname.endswith('.gz'):
 | 
				
			||||||
 | 
					            fname += '.gz'
 | 
				
			||||||
 | 
					        with gzip.open(fname, 'r') as fin:
 | 
				
			||||||
 | 
					            d = json.loads(fin.read().decode('utf-8'))
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        if fname.endswith('.gz'):
 | 
				
			||||||
 | 
					            warnings.warn("Trying to read from %s without unzipping!" % fname, UserWarning)
 | 
				
			||||||
 | 
					        with open(fname, 'r', encoding='utf-8') as fin:
 | 
				
			||||||
 | 
					            d = json.loads(fin.read())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return import_json_string(d, verbose, full_output)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -41,6 +41,9 @@ def test_jsonio():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    os.remove(fname + '.json.gz')
 | 
					    os.remove(fname + '.json.gz')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for o, r in zip(ol, rl):
 | 
				
			||||||
 | 
					        assert np.all(o == r)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for i in range(len(rl)):
 | 
					    for i in range(len(rl)):
 | 
				
			||||||
        if isinstance(ol[i], pe.Obs):
 | 
					        if isinstance(ol[i], pe.Obs):
 | 
				
			||||||
            o = ol[i] - rl[i]
 | 
					            o = ol[i] - rl[i]
 | 
				
			||||||
| 
						 | 
					@ -57,6 +60,9 @@ def test_jsonio():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    rl = jsonio.load_json(fname, gz=False, full_output=True)
 | 
					    rl = jsonio.load_json(fname, gz=False, full_output=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    assert(description == rl['description'])
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    os.remove(fname + '.json')
 | 
					    os.remove(fname + '.json')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for o, r in zip(ol, rl['obsdata']):
 | 
				
			||||||
 | 
					        assert np.all(o == r)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert(description == rl['description'])
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue