Skip to content

Commit b72a3f0

Browse files
committed
maint: changes to the example files
1 parent cc27aaf commit b72a3f0

File tree

5 files changed

+43
-55
lines changed

5 files changed

+43
-55
lines changed

examples/05-file-IO/00-hdf5_double_float_comparison.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# Import the ``dpf-core`` module and its examples files, and then create a
3737
# temporary directory.
3838

39-
import os
39+
from pathlib import Path
4040

4141
from ansys.dpf import core as dpf
4242
from ansys.dpf.core import examples
@@ -78,8 +78,8 @@
7878
# Define a temporary folder for outputs
7979
tmpdir = dpf.core.make_tmp_dir_server(dpf.SERVER)
8080
files = [
81-
dpf.path_utilities.join(tmpdir, "dpf_float.h5"),
82-
dpf.path_utilities.join(tmpdir, "dpf_double.h5"),
81+
Path(dpf.path_utilities.join(tmpdir, "dpf_float.h5")),
82+
Path(dpf.path_utilities.join(tmpdir, "dpf_double.h5")),
8383
]
8484
###############################################################################
8585
# Export with simple precision.
@@ -98,8 +98,8 @@
9898
# Download the resulting .h5 files if necessary
9999

100100
if not dpf.SERVER.local_server:
101-
float_file_path = os.path.join(os.getcwd(), "dpf_float.h5")
102-
double_file_path = os.path.join(os.getcwd(), "dpf_double.h5")
101+
float_file_path = Path.cwd() / "dpf_float.h5"
102+
double_file_path = Path.cwd() / "dpf_double.h5"
103103
dpf.download_file(files[0], float_file_path)
104104
dpf.download_file(files[1], double_file_path)
105105
else:
@@ -109,8 +109,8 @@
109109

110110
###############################################################################
111111
# Compare simple precision versus double precision.
112-
float_precision = os.stat(float_file_path).st_size
113-
double_precision = os.stat(double_file_path).st_size
112+
float_precision = float_file_path.stat().st_size
113+
double_precision = double_file_path.stat().st_size
114114
print(
115115
f"size with float precision: {float_precision}\n"
116116
f"size with double precision: {double_precision}"

examples/05-file-IO/04-basic-load-file.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@
5858
# ~~~~~~~~~~~~~
5959
# Export the fields container in the CSV format:
6060

61-
import os
61+
from pathlib import Path
6262

6363
csv_file_name = "simple_bar_fc.csv"
6464
# Define an output path for the resulting .csv file
6565
if not dpf.SERVER.local_server:
6666
# Define it server-side if using a remote server
6767
tmp_dir_path = dpf.core.make_tmp_dir_server(dpf.SERVER)
68-
server_file_path = dpf.path_utilities.join(tmp_dir_path, csv_file_name)
68+
server_file_path = Path(dpf.path_utilities.join(tmp_dir_path, csv_file_name))
6969
else:
70-
server_file_path = os.path.join(os.getcwd(), csv_file_name)
70+
server_file_path = Path.cwd() / csv_file_name
7171

7272
# Perform the export to csv on the server side
7373
export_csv_operator = dpf.operators.serialization.field_to_csv()
@@ -81,7 +81,7 @@
8181
# Download the file ``simple_bar_fc.csv``:
8282

8383
if not dpf.SERVER.local_server:
84-
downloaded_client_file_path = os.path.join(os.getcwd(), "simple_bar_fc_downloaded.csv")
84+
downloaded_client_file_path = Path.cwd() / "simple_bar_fc_downloaded.csv"
8585
dpf.download_file(server_file_path, downloaded_client_file_path)
8686
else:
8787
downloaded_client_file_path = server_file_path
@@ -98,7 +98,7 @@
9898
mesh.plot(server_fc_out)
9999

100100
# Remove file to avoid polluting.
101-
os.remove(downloaded_client_file_path)
101+
downloaded_client_file_path.unlink()
102102

103103
###############################################################################
104104
# Make operations over the fields container

examples/08-python-operators/00-wrapping_numpy_capabilities.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@
5757
# Download and display the Python script.
5858

5959
from ansys.dpf.core.examples import download_easy_statistics
60+
from pathlib import Path
6061

61-
operator_file_path = download_easy_statistics()
62+
operator_file_path = Path(download_easy_statistics())
6263

63-
with open(operator_file_path, "r") as f:
64-
for line in f.readlines():
64+
with operator_file_path.open() as file:
65+
for line in file.readlines():
6566
print("\t\t\t" + line)
6667

6768
###############################################################################
@@ -76,15 +77,14 @@
7677
# - The third argument is the name of the function used to record operators.
7778
#
7879

79-
import os
8080
from ansys.dpf import core as dpf
8181
from ansys.dpf.core import examples
8282

8383
# Python plugins are not supported in process.
8484
dpf.start_local_server(config=dpf.AvailableServerConfigs.GrpcServer)
8585

86-
operator_server_file_path = dpf.upload_file_in_tmp_folder(operator_file_path)
87-
dpf.load_library(os.path.dirname(operator_server_file_path), "py_easy_statistics", "load_operators")
86+
operator_server_file_path = Path(dpf.upload_file_in_tmp_folder(operator_file_path))
87+
dpf.load_library(operator_server_file_path.parent, "py_easy_statistics", "load_operators")
8888

8989
###############################################################################
9090
# Instantiate the operator.

examples/08-python-operators/01-package_python_operators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@
7272
# for the plug-in package that is used to record operators.
7373
#
7474

75-
import os
76-
7775
from ansys.dpf import core as dpf
7876
from ansys.dpf.core import examples
7977

@@ -83,7 +81,7 @@
8381
tmp = dpf.make_tmp_dir_server()
8482
dpf.upload_files_in_folder(dpf.path_utilities.join(tmp, "average_filter_plugin"), plugin_folder)
8583
dpf.load_library(
86-
os.path.join(dpf.path_utilities.join(tmp, "average_filter_plugin")),
84+
dpf.path_utilities.join(tmp, "average_filter_plugin"),
8785
"py_average_filter",
8886
"load_operators",
8987
)

examples/08-python-operators/02-python_operators_with_dependencies.py

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@
5858
# created for you.
5959

6060
import os
61+
from pathlib import Path
6162

6263
from ansys.dpf.core import examples
6364

64-
plugin_path = examples.download_gltf_plugin()
65-
folder_root = os.path.join(os.getcwd().rsplit("pydpf-core", 1)[0], "pydpf-core")
65+
plugin_path = Path(examples.download_gltf_plugin())
66+
folder_root = Path(__file__).parent.parent.parent
6667

6768
# %%
6869
# To add third-party modules as dependencies to a plug-in package, you must
@@ -83,8 +84,9 @@
8384
# To simplify this step, you can add a requirements file in the plug-in package:
8485
#
8586
print("\033[1m gltf_plugin/requirements.txt: \n \033[0m")
86-
with open(os.path.join(plugin_path, "requirements.txt"), "r") as f:
87-
for line in f.readlines():
87+
requirements_path = plugin_path / "requirements.txt"
88+
with requirements_path.open("r") as file:
89+
for line in file.readlines():
8890
print("\t\t\t" + line)
8991

9092

@@ -117,26 +119,21 @@
117119
#
118120
# create_sites_for_python_operators.sh -pluginpath /path/to/plugin -zippath /path/to/plugin/assets/linx64.zip # noqa: E501
119121

120-
121-
if os.name == "nt" and not os.path.exists(
122-
os.path.join(plugin_path, "assets", "gltf_sites_winx64.zip")
123-
):
124-
cmd_file = os.path.join(
125-
folder_root,
126-
"doc",
127-
"source",
128-
"user_guide",
129-
"create_sites_for_python_operators.ps1",
122+
site_path = plugin_path / "assets" / "gltf_sites_winx64.zip"
123+
if os.name == "nt" and not site_path.exists():
124+
cmd_file = (
125+
folder_root / "doc" / "source" / "user_guide" / "create_sites_for_python_operators.ps1"
130126
)
131127
args = [
132128
"powershell",
133-
cmd_file,
129+
str(cmd_file),
134130
"-pluginpath",
135-
plugin_path,
131+
str(plugin_path),
136132
"-zippath",
137-
os.path.join(plugin_path, "assets", "gltf_sites_winx64.zip"),
133+
str(plugin_path / "assets" / "gltf_sites_winx64.zip"),
138134
]
139135
print(args)
136+
140137
import subprocess
141138

142139
process = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -150,20 +147,15 @@
150147
)
151148
else:
152149
print("Installing pygltf in a virtual environment succeeded")
153-
elif os.name == "posix" and not os.path.exists(
154-
os.path.join(plugin_path, "assets", "gltf_sites_linx64.zip")
155-
):
156-
cmd_file = os.path.join(
157-
folder_root,
158-
"doc",
159-
"source",
160-
"user_guide",
161-
"create_sites_for_python_operators.sh",
150+
151+
elif os.name == "posix" and not site_path.exists():
152+
cmd_file = (
153+
folder_root / "doc" / "source" / "user_guide" / "create_sites_for_python_operators.sh"
162154
)
163155
run_cmd = f"{cmd_file}"
164156
args = (
165157
f' -pluginpath "{plugin_path}" '
166-
f"-zippath \"{os.path.join(plugin_path, 'assets', 'gltf_sites_linx64.zip')}\""
158+
f'-zippath \"{plugin_path / "assets" / "gltf_sites_winx64.zip"}\"'
167159
)
168160
print(run_cmd + args)
169161
os.system(f"chmod u=rwx,o=x {cmd_file}")
@@ -189,9 +181,9 @@
189181
# Python plugins are not supported in process.
190182
dpf.start_local_server(config=dpf.AvailableServerConfigs.GrpcServer)
191183

192-
tmp = dpf.make_tmp_dir_server()
193-
dpf.upload_files_in_folder(dpf.path_utilities.join(tmp, "plugins", "gltf_plugin"), plugin_path)
194-
dpf.upload_file(plugin_path + ".xml", dpf.path_utilities.join(tmp, "plugins", "gltf_plugin.xml"))
184+
tmp = Path(dpf.make_tmp_dir_server())
185+
dpf.upload_files_in_folder(dpf.path_utilities.join(str(tmp), "plugins", "gltf_plugin"), plugin_path)
186+
dpf.upload_file(str(plugin_path) + ".xml", dpf.path_utilities.join(str(tmp), "plugins", "gltf_plugin.xml"))
195187

196188
dpf.load_library(
197189
dpf.path_utilities.join(tmp, "plugins", "gltf_plugin"),
@@ -235,8 +227,6 @@
235227
# Use the custom operator
236228
# -----------------------
237229

238-
import os
239-
240230
model = dpf.Model(dpf.upload_file_in_tmp_folder(examples.find_static_rst()))
241231

242232
mesh = model.metadata.meshed_region
@@ -245,14 +235,14 @@
245235
displacement = model.results.displacement()
246236
displacement.inputs.mesh_scoping(skin_mesh)
247237
displacement.inputs.mesh(skin_mesh)
248-
new_operator.inputs.path(os.path.join(tmp, "out"))
238+
new_operator.inputs.path(str(tmp / "out"))
249239
new_operator.inputs.mesh(skin_mesh)
250240
new_operator.inputs.field(displacement.outputs.fields_container()[0])
251241
new_operator.run()
252242

253243
print("operator ran successfully")
254244

255-
dpf.download_file(os.path.join(tmp, "out.glb"), os.path.join(os.getcwd(), "out.glb"))
245+
dpf.download_file(tmp / "out.glb", Path.cwd / "out.glb")
256246

257247
# %%
258248
# You can download :download:`output <images/thumb/out.glb>` from the ``gltf`` operator.

0 commit comments

Comments
 (0)