Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

# coding: utf-8 

# Copyright (c) Pymatgen Development Team. 

# Distributed under the terms of the MIT License. 

 

from __future__ import division, unicode_literals 

 

""" 

Most features of this module has been moved to monty. Please refer to 

monty.json's documentation. 

""" 

 

__author__ = "Shyue Ping Ong" 

__copyright__ = "Copyright 2012, The Materials Project" 

__version__ = "0.1" 

__maintainer__ = "Shyue Ping Ong" 

__email__ = "shyuep@gmail.com" 

__date__ = "Apr 30, 2012" 

 

import json 

import functools 

 

from monty.serialization import loadfn, dumpfn 

from monty.dev import deprecated 

 

 

def pmg_serialize(method): 

""" 

Decorator for methods that add MSON serializations keys  

to the dictionary. See documentation of MSON for more details 

""" 

@functools.wraps(method) 

def wrapper(*args, **kwargs): 

self = args[0] 

d = method(*args, **kwargs) 

# Add @module and @class 

d["@module"] = self.__class__.__module__ 

d["@class"] = self.__class__.__name__ 

return d 

 

return wrapper 

 

 

def json_pretty_dump(obj, filename): 

""" 

Serialize obj as a JSON formatted stream to the given filename ( 

pretty printing version) 

""" 

with open(filename, "w") as fh: 

json.dump(obj, fh, indent=4, sort_keys=4) 

 

 

@deprecated(loadfn, "Will be removed in pmg 4.0.") 

def pmg_load(filename, **kwargs): 

""" 

Loads a json file and deserialize it with MontyDecoder. 

 

Args: 

filename (str): Filename of file to open. Can be gzipped or bzipped. 

\*\*kwargs: Any of the keyword arguments supported by the json.load 

method. 

 

Returns: 

Deserialized pymatgen object. Note that these objects can be lists, 

dicts or otherwise nested pymatgen objects that support the as_dict() 

and from_dict MSONable protocol. 

""" 

return loadfn(filename, **kwargs) 

 

 

@deprecated(dumpfn, "Will be removed in pmg 4.0.") 

def pmg_dump(obj, filename, **kwargs): 

""" 

Dump an object to a json file using MontyEncoder. Note that these 

objects can be lists, dicts or otherwise nested pymatgen objects that 

support the as_dict() and from_dict MSONable protocol. 

 

Args: 

obj (object): Object to dump. 

filename (str): Filename of file to open. Can be gzipped or bzipped. 

\*\*kwargs: Any of the keyword arguments supported by the json.dump 

method. 

""" 

return dumpfn(obj, filename, **kwargs)