Salome HOME
Documentation and models update, pst4mod
[modules/adao.git] / src / daComposant / daNumerics / pst4mod / modelica_libraries / functions.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2016-2024 EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
19 ###############################################################################
20 #                            LIBRARIES IMPORT                                 #
21 ###############################################################################
22 from time import localtime
23 import os
24 from subprocess import Popen
25
26 ###############################################################################
27 #                            USEFUL FUNCTIONS                                 #
28 ###############################################################################
29
30 def get_cur_time():
31     """
32     Return a string in the format : year/month/day hour:min:sec
33     """
34     real_time = localtime()
35     str_real_time = \
36         '{0:04d}'.format(real_time.tm_year) + '/' + \
37         '{0:02d}'.format(real_time.tm_mon) + '/' + \
38         '{0:02d}'.format(real_time.tm_mday) + ' ' + \
39         '{0:02d}'.format(real_time.tm_hour) + ':' + \
40         '{0:02d}'.format(real_time.tm_min) + ':' + \
41         '{0:02d}'.format(real_time.tm_sec)
42
43     return str_real_time
44
45
46 def tol_equality(vars, tol=1.e-4):
47     """
48     Check whether two numbers (vars[0] and vars[1]) are approximately equal (with 'tol' tollerance)
49     """
50     try:
51         return abs(float(vars[1]) / float(vars[0]) - 1.) < tol
52     except ZeroDivisionError:
53         return abs(vars[1] - vars[0]) < tol
54
55
56 def Edit_File(filename):
57     if os.name == 'nt':  # Windows
58         editor = ['Notepad']
59     else:
60         editor = ['gedit', '-s']
61     file_display = Popen(editor + [f'{filename}'])
62     file_display.wait()
63
64
65 if __name__ == '__main__':
66     print('\n  AUTODIAGNOSTIC\n  ==============\n')