libraryDeprecationTest.py
You can view and download this file on Github: libraryDeprecationTest.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: A deprecated function or argument of the Python library warns like the deprecations of the C++
5# side (#2807): a DeprecationWarning at the line of the script that uses it, once per session
6# and name - every time with exu.special.deprecations.warnOnce = False -, and each use counted
7# in exu.sys['deprecationUse']['library']. graphics.BrickXYZ is a deprecated function,
8# bodyNumbers of the Create functions a deprecated argument (#2863), and bodyOrNodeList is not
9# accepted; graphics.Brick, which uses BrickXYZ inside, is not deprecated and records nothing.
10#
11# Author: Johannes Gerstmayr
12# Date: 2026-10-03
13#
14# Copyright:This file is part of Exudyn. Exudyn is free software. You can redistribute it and/or modify it under the terms of the Exudyn license. See 'LICENSE.txt' for more details.
15#
16#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
18import exudyn as exu
19from exudyn.utilities import *
20import exudyn.graphics as graphics
21import numpy as np
22import warnings
23import os
24
25testIsActive = exu.sys.get('testIsActive', False)
26
27def Uses(name):
28 return exu.sys.get('deprecationUse', {}).get('library', {}).get(name, 0)
29
30def FromThisScript(caught):
31 """the warnings point at this script, not into the package"""
32 return all(os.sep + 'exudyn' + os.sep not in entry.filename for entry in caught)
33
34errors = 0
35deprecations = exu.special.deprecations
36warnOnceStored = deprecations.warnOnce
37
38#a deprecated function
39used = Uses('graphics.BrickXYZ')
40graphics.Brick(centerPoint=[0,0,0], size=[1,1,1]) #uses BrickXYZ inside: no use of a deprecated name
41if Uses('graphics.BrickXYZ') != used:
42 errors += 1
43nWarnings = []
44for warnOnce in [True, False]:
45 deprecations.warnOnce = warnOnce
46 deprecations.Reset()
47 with warnings.catch_warnings(record=True) as caught:
48 warnings.simplefilter('always')
49 for i in range(3):
50 graphics.BrickXYZ(0, 0, 0, 1, 1, 1)
51 caught = [entry for entry in caught if issubclass(entry.category, DeprecationWarning)]
52 nWarnings.append(len(caught))
53 if not FromThisScript(caught) or any('graphics.BrickXYZ is deprecated' not in str(entry.message) for entry in caught):
54 errors += 1
55if nWarnings != [1, 3] or Uses('graphics.BrickXYZ') != used + 6:
56 errors += 1
57
58#a deprecated argument, reported for the Create function that was called
59SC = exu.SystemContainer()
60mbs = SC.AddSystem()
61oGround = mbs.CreateGround()
62b0 = mbs.CreateMassPoint(referencePosition=[1,0,0], mass=1)
63used = Uses('MainSystem.CreateSpringDamper.bodyNumbers')
64deprecations.warnOnce = False
65with warnings.catch_warnings(record=True) as caught:
66 warnings.simplefilter('always')
67 mbs.CreateSpringDamper(bodyNumbers=[oGround, b0], stiffness=100)
68caught = [entry for entry in caught if issubclass(entry.category, DeprecationWarning)]
69deprecations.warnOnce = warnOnceStored
70exu.Print('bodyNumbers:', [str(entry.message) for entry in caught])
71if len(caught) != 1 or not FromThisScript(caught) or Uses('MainSystem.CreateSpringDamper.bodyNumbers') != used + 1:
72 errors += 1
73
74#an argument that is not accepted any more says what to use
75try:
76 mbs.CreateSpringDamper(bodyOrNodeList=[oGround, b0], stiffness=100)
77 errors += 1
78except TypeError as error:
79 if 'use itemNumbers' not in str(error):
80 errors += 1
81
82exu.Print('libraryDeprecationTest: warnings', nWarnings, ', errors', errors)
83u = sum(nWarnings) + errors
84exu.Print('solution of libraryDeprecationTest=', u)
85
86exu.sys['testResult'] = u