DHI / mikepluspy

MIKE+Py is the official python interface for MIKE+
Apache License 2.0
6 stars 1 forks source link

Consider to support context manager (`with`) #17

Open ecomodeller opened 5 months ago

ecomodeller commented 5 months ago

https://github.com/DHI/mikepluspy/blob/024977cdd21ffcede755e93bfa1a5ac19d4aa5bf/tests/test_datatable.py#L16

Instead of:

data_access.open_database()
muids = data_access.get_muid_where("msm_Link", "MUID='link_test'")
data_access.close_database()

it will be

with data_access.open_database()
    muids = data_access.get_muid_where("msm_Link", "MUID='link_test'")
    ...
# db connection is closed at the end of the with block

Closing databases is typically done with automatically with a context manager. https://realpython.com/python-with-statement/#the-with-statement-approach

Equivalent to using in C# https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using