profile
viewpoint

Ask questionsProposal: "more pythonic" bindings

While implementing some python callbacks using sysrepo bindings, I've made a few tweaks for easier use. I could make a pull request, but SWIG is not my strong point, so I just post them here for discussion:

  • We could extract data from sr.Val class as a corresponding Python type directly
yang_types = {
    sr.SR_BOOL_T:      sr.Data.get_bool,
    sr.SR_DECIMAL64_T: sr.Data.get_decimal64,
    sr.SR_ENUM_T:      sr.Data.get_enum,
    sr.SR_INT8_T:      sr.Data.get_int8,
    sr.SR_INT16_T:     sr.Data.get_int16,
    sr.SR_INT32_T:     sr.Data.get_int32,
    sr.SR_INT64_T:     sr.Data.get_int64,
    sr.SR_STRING_T:    sr.Data.get_string,
    sr.SR_UINT8_T:     sr.Data.get_uint8,
    sr.SR_UINT16_T:    sr.Data.get_uint16,
    sr.SR_UINT32_T:    sr.Data.get_uint32,
    sr.SR_UINT64_T:    sr.Data.get_uint64
}

class Val(_object):
    def pdata(self)
        return yang_types[self.type()](self.data())

such a method would remove a lot of boilerplate code

  • Make a "Pythonic" iterator for changes
class Session(_object):
    def get_changes(self, change_path):
        it = self.get_changes_iter(change_path)
        while True:
            change = self.get_change_next(it)
            if change is None:
                break
            yield change

then in callbacks we can write a simple for-loop

for change in sess.get_changes(change_path):
     ...
  • Add indexing support to sr.Vals and other such classes. It is a straightforward mapping, i.e. __len__() -> val_cnt() and __getitem__(idx) -> val(idx). However, to use it in for-loops, __getitem__ has to return IndexError, but val returns SystemError
sysrepo/sysrepo

Answer questions rjarry

I forgot to mention. One of the perks in using CFFI instead of manually written python C extensions is pypy support directly out of the box.

useful!

Related questions

why "There is a not enabled node in ietf-system module, it can not be committed to the running" occured when I try to call "sr_set_item" in running datastore? hot 1
sysrepocfg error using config false in a YANG file hot 1
New sysrepo: leafref can't be used as a list key hot 1
[New sysrepo] sysrepo shows 'Schema node "status" not found (../status) with context node "/o-ran-ald:ald-communication/output/error-message".' - sysrepo hot 1
possibility send notification in another thread during subtree_change callback hot 1
Can't connect to netopeer2-server via a unix socket when sysrepo is run as an unprivilieged process hot 1
Filling up startup datastore hot 1
[New-Sysrepo] sr_main_lock not released when install new netopeer2-server hot 1
Can't connect to netopeer2-server via a unix socket when sysrepo is run as an unprivilieged process hot 1
Sysrepo Api to lock DB hot 1
Python global_loop() replacement hot 1
source:https://uonfu.com/
Github User Rank List