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

Make NTEnum useful #27

Closed
wants to merge 3 commits into from
Closed

Make NTEnum useful #27

wants to merge 3 commits into from

Conversation

mdavidsaver
Copy link
Member

A rather involved change to the NT wrapper interface in order to make NTEnum useful (#25).

Mainly a replacement/extension of the client side wrapper functions with a stateful wrap/unwrap object. This allows p4p.nt.enum.NTEnum to cache a choices list, allowing for index lookup in wrap() and unwrap() where it might not otherwise be available.

This is really a workaround for the race conditions inherent in the way PVA protocol and the NTEnum are defined.

The p4p.nt.enum.ntenum wrapper acts like an integer, with an additional member choice containing the string (if known).

There are still some TODOs here. I wonder if the wrapper object should be compatible with str?

@tynanford Would this work for you? Improvements?

Server side usage would be:

from p4p.nt import NTEnum
from p4p.server.thread import SharedPV
pv = SharedPV(nt=NTEnum(), initial={'choices':['zero','one'], 'index':1})
assert pv.current()==1, pv.current() # compare ntenum with int
print pv.current() # prints "one\n"

pv.post(0) # as integer
assert pv.current()==0 # compare ntenum with int

pv.post(1) # as string
assert pv.current()==1

# change choices list
pv.post({'choices':['A','B','C'], 'index':2})
assert pv.current()==2

Client side usage against QSRV w/ an mbboRecord

record(mbbo, "select") {
    field(ZRVL, "0")
    field(ONVL, "1")
    field(ZRST, "Zero")
    field(ONST, "One")
}

would be

from p4p.client.thread import Context
ctxt=Context('pva')
print repr(ctxt.get('select'))
# "ntenum(0, Zero)\n"
ctxt.put('select', 1)
print repr(ctxt.get('select'))
# "ntenum(1, One)\n"
ctxt.put('select', 'Zero')
print repr(ctxt.get('select'))
# "ntenum(0, Zero)\n"

Documentation of this new NT interface is tbd.

@tynanford
Copy link
Contributor

Yes this is great! Everything is working.

@mdavidsaver
Copy link
Member Author

Rebased and merged as d60cbd7

@mdavidsaver mdavidsaver closed this Feb 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants