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

#!/usr/bin/env python3 

# System modules 

import json 

import datetime 

 

# External modules 

 

# Internal modules 

 

 

# Variables 

EMPTY_JSON = {} 

 

# read json from a filename 

 

 

def read_json_from_file(filename): 

""" 

read json from a file given then filename 

args: 

filename (str): The path to the file to read 

returns: 

dict, empty dict if error occured during read 

""" 

try: # open and read, return result 

with open(filename, "r") as f: 

return json.load(f) 

except BaseException: # didn't work, return empty dict 

return {} 

 

# write json to file 

 

 

def write_json_to_file(data, filename): 

""" 

write data to json file 

args: 

filename (str): the json file to write the data to 

data (dict): the data to write to the file 

returns: 

True on success, False otherwise 

""" 

try: 

jsonstr = json.dumps(data, sort_keys=True, indent=4) # try to convert 

# write to file 

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

f.write(jsonstr) 

return True 

except BaseException: 

return False 

 

# get UNIX-timestamp from datetime object 

 

 

def unix_timestamp(dt): 

""" 

Calculate the UNIX-timestamp - i.e. the seconds since 01.01.1970 - from a 

given datetime object 

 

Args: 

dt (:any:`datetime.datetime`): a datetime object 

Returns: 

float : the UNIX-timestamp in seconds 

""" 

return (dt - datetime.datetime(1970, 1, 1)).total_seconds() 

 

# do nothing 

 

 

def nothing(): # pragma: no cover 

pass