Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M trevoux patch 1 #31

Merged
merged 20 commits into from
Aug 13, 2021
Merged

M trevoux patch 1 #31

merged 20 commits into from
Aug 13, 2021

Conversation

MTrevoux
Copy link
Contributor

@MTrevoux MTrevoux commented Aug 5, 2021

Hello,

Adding context manager for dealing with connections avoiding blockage of licenses.

Testing below:
image

MTrevoux added 14 commits July 30, 2020 21:10
Hi gents,

I was following this project. I did some personal coding that I believe is good to share with you.
Idea is to add DoSlowCmd for long calculation to avoid timeout of a script.

I followed the orignal VBA code. I did a try and it worked fine. We could go for furhter testing. Can we discuss about this patch?
Tell me your thoughts.

Marc
label:enhancement adding DoSlowCmd in openserver.py
adding context manager
@knudsvik
Copy link
Contributor

knudsvik commented Aug 6, 2021

Not sure I understand the purpose of this PR. The initial class has methods to connect and disconnect to free up the license. If an exception is thrown, the disconnect method is run and license free'd up?

@MTrevoux
Copy link
Contributor Author

MTrevoux commented Aug 6, 2021

Hi Thorjan,

There are several advantages of using a such a context:

The exceptions you are mentioning are the ones happening when using the DoGet, DoSet or DoCmd methods.
However if you make an error outside from those methods, you keep the connection open.
See the example below:
image

In this example one leaves the connection open and can lock it if the instance is killed. The context manager on the other hand is automatically closing the connection in such cases. It happens to be very useful when using notebooks and testing codes.
It also saves you if case you forget to disconnect after few line of codes...

Finally, in terms of scripting you need a single line of code to create the instance, connect and deal with disconnection whereas in the actual case you need 3:

With OSconnection() as c:
    ...Do stuff...

#Versus

c = OpenServer()
c.connect()
...Do Stuff...
c.disconnect()

I hope it makes sense

@knudsvik
Copy link
Contributor

Hi, the use of with statements is quite new to us, but we see how this could improve certain use cases. Would it be possible to include the __enter__ and __exit__ method directly in the OpenServer class instead? So instantiating it by
with OpenServer as c: c.DoSet() ...

@MTrevoux
Copy link
Contributor Author

Hi,
Sure I will look at how to include it in the main class. I need to check few things there.

For the use cases, I see two of them:

  • Notebook development with trial and error testing
  • When you see a block like below which is an equivalent:
c = OpenServer()
c.connect()
try:
   ... Do things ...
except:
   c.disconnect()

I will keep you posted in the coming days.

@MTrevoux
Copy link
Contributor Author

Ok, all done and giving the same results:

You can see in the screen shot below that the with statement is working when declaring the OpenServer() instance.

image

@knudsvik
Copy link
Contributor

Looks good! Please update version.py and changelog.md and I can merge it afterwards.

@knudsvik
Copy link
Contributor

Thank you very much for your contribution :)

@knudsvik knudsvik merged commit 5eca84b into equinor:master Aug 13, 2021
@MTrevoux
Copy link
Contributor Author

With pleasure!

@knudsvik
Copy link
Contributor

The new version is now available on PyPi as well so you can run pip install openserver --upgrade to patch your installation!

@MTrevoux MTrevoux deleted the MTrevoux-patch-1 branch August 13, 2021 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants