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

python: provide more high-level APIs #2584

Open
markus2330 opened this Issue Apr 4, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@markus2330
Copy link
Contributor

markus2330 commented Apr 4, 2019

I have some common helpers which are often needed and should be directly added to our binding (so that they are always available, well tested and well documented)

def make_set (ks, where):
	s = set ()
	for k in ks:
		if k.isDirectBelow (where):
			s.add (k)
	return s;

def unpack_names (ks):
	s = set ()
	for k in ks:
		s.add (k.name)
	return s

def unpack_basenames (ks):
	s = set ()
	for k in ks:
		s.add (k.basename)
	return s

def is_array_entry (k, arrayname):
	if k.basename[0] != '#':
		return False
	x = copy.copy(k)
	x.delBaseName()
	if x.basename == arrayname:
		return True

def append (ks, name, p):
	n = kdb.Key(name)
	n.string = str(p)
	ks.append (n)
	print n

# this is only a workaround, ideally this would be directly in kdb.KeySet:
class GetKeySet(kdb.KeySet):
	def get(self, n):
		k = ks.lookup(n)
		if not k:
			raise KeyError(n+" not found")
		return k.string
	def set(self, n, s):
		k = ks.lookup(n)
		if not k: # TODO: call append here
			raise KeyError(n+" not found")
		k.string = str(s)
@kodebach

This comment has been minimized.

Copy link
Contributor

kodebach commented Apr 4, 2019

# this is only a workaround, ideally this would be directly in kdb.KeySet:
class GetKeySet(kdb.KeySet):
	def get(self, n):
		k = ks.lookup(n)
		if not k:
			raise KeyError(n+" not found")
		return k.string
	def set(self, n, s):
		k = ks.lookup(n)
		if not k: # TODO: call append here
			raise KeyError(n+" not found")
		k.string = str(s)

The binding already has __getitem__ so ks["/some/key"] already works. We should just add a __setitem__ instead of get and set.

Also is_array_entry would ideally use libease's elektraArrayValidateName (or elektraArrayValidateBaseNameString) to ensure consistency.

And if someone has the python skills for it, a constructor for creating a Key from two strings (name and value) and a dictionary of strings (for metadata) would be much nicer than the C like vararg constructor as well.

@markus2330

This comment has been minimized.

Copy link
Contributor Author

markus2330 commented Apr 4, 2019

Thank you for the feedback! Yes, then __setitem__ would be the more pythonic solution. Nevertheless, I would appreciate to also have get/set as my code already uses this. It is also more consistent with the C++ binding. (Where "get" is a template and also supports complex types.)

is_array_entry would ideally use libease's elektraArrayValidateName

You are completely right! If we pull in the libease dependency, however, we should fully add all these functions to the python binding. This would be a separate task, though. It would be interesting if elektraKsFilter is faster than a python loop.

And if someone has the python skills for it, a constructor for creating a Key from two strings (name and value) and a dictionary of strings (for metadata) would be much nicer than the C like vararg constructor as well.

Yes, in Java we already have this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.