Skip to content

Implement Port.alias() property. - #45

Merged
mgeier merged 9 commits into
spatialaudio:masterfrom
zynthian:master
Jan 29, 2018
Merged

mgeier merged 9 commits into
spatialaudio:masterfrom
zynthian:master

Conversation

@jofemodo

Copy link
Copy Markdown
Contributor

Uses "int jack_port_get_aliases(const jack_port_t * port, char *const aliases[2])" from the jack C library.

+ uses "int jack_port_get_aliases(const jack_port_t * port, char *const aliases[2])" from the jack C library.
@mgeier

mgeier commented Jan 19, 2018

Copy link
Copy Markdown
Member

Thanks for this PR!

What about adding set_alias() and unset_alias()?
This way we could easily test if/how the alias property works.

There seem to be up to two aliases per port, what about returning a list of str instead of a single str?

Comment thread src/jack.py Outdated
out-hw-2-0-0-MK-249C-USB-MIDI-keyboard-MIDI-

"""
aliases=[_ffi.new("char[0xff]"), _ffi.new("char[0xff]")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably use port_name_size() instead of 0xff?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think port_name_size() return the size of the "name", not the alias. I've not found any way of getting the alias size, so i decided to use 255, that should be enough ... i hope ;-)

@jofemodo jofemodo Jan 19, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, i just read the "set_alias" documentation and it says:

Set alias as an alias for port. May be called at any time. If the alias is longer than jack_port_name_size(), it will be truncated.

So i think you are right. I will change the code ;-)

@jofemodo

Copy link
Copy Markdown
Contributor Author

I've implemented this feature because i need it. I've no need for the set_alias and unset_alias functions, although it's pretty simple to implement. I will do.

Also, i will return a list of aliases ;-)

+ Change the name of property alias to aliases. Now it returns a list of 2 strings.
+ Use jack_port_name_size() for correctly set the size of strings in aliases property getter.
@jofemodo

Copy link
Copy Markdown
Contributor Author

Done! ;-)

@mgeier

mgeier commented Jan 20, 2018

Copy link
Copy Markdown
Member

Thanks a lot for the changes!
Currently, I don't have time to review, I'll come back to you after the weekend.

@jofemodo

jofemodo commented Jan 22, 2018 •

Copy link
Copy Markdown
Contributor Author

I just added a little test to examples for easier reviewing ;-)

@mgeier

mgeier commented Jan 22, 2018

Copy link
Copy Markdown
Member

The API of jack_port_get_aliases() seems quite strange to me, because if it returns 1, it doesn't tell us which of the two aliases is set ... or am I missing something?

Returning a list which may have empty strings in it (or even None values) seems a bit strange and un-Pythonic to me.
What about simply returning a "list of aliases" with 0, 1 or 2 elements, depending on the number of actual aliases?

@mgeier

mgeier commented Jan 22, 2018

Copy link
Copy Markdown
Member

Another thought: We could even return a set instead of a list?

@jofemodo

jofemodo commented Jan 22, 2018 •

Copy link
Copy Markdown
Contributor Author

OK to the first part. Returning a set is not a good idea because the aliases sorting is lost.

@mgeier

mgeier commented Jan 23, 2018

Copy link
Copy Markdown
Member

Thanks for the changes!

Does a "sorted list of aliases" even make sense?
Would you ever use the sorted-ness of the aliases in your code?
And if we return 1-element lists, we already give up on the sorted-ness.

How would you use the aliases property in your code?

Anyway, I'm not totally convinced about using a set. If you are convinced about using a list, I'm fine with it.

Comment thread src/jack.py Outdated
aliasesptr=_ffi.new("char *[]", aliases)
result=[]
if (_lib.jack_port_get_aliases(self._ptr, aliasesptr) > 0):
alias1=_ffi.string(aliases[0]).decode()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could reduce the duplication by using something like

for i in 0, 1:
    alias = ... aliases[i] ...
    if alias:
        result.append(alias)

@jofemodo

jofemodo commented Jan 23, 2018 •

Copy link
Copy Markdown
Contributor Author

OK! I've improved the code, reducing the redundance.
Regarding the list/set question, i prefer returning a list. In my code is not important the order, as only one alias is set, but i can imagine a case where you would like to get the "first" or "last" alias ;-)
Anyway, if you think it's better, i will change to a "set" ...

@mgeier

mgeier commented Jan 25, 2018

Copy link
Copy Markdown
Member

OK, thanks for the changes, let's keep the list.

Now there are just a few formal issues left, regarding PEP-8 and PEP-257.

$ python3 -m pycodestyle src/jack.py 
...
src/jack.py:1451:14: E225 missing whitespace around operator
src/jack.py:1452:16: E225 missing whitespace around operator
src/jack.py:1453:19: E225 missing whitespace around operator
src/jack.py:1454:15: E225 missing whitespace around operator
src/jack.py:1457:22: E225 missing whitespace around operator
src/jack.py:1465:67: W291 trailing whitespace
...
$ python3 -m pydocstyle src/jack.py
...
src/jack.py:1463 in public method `set_alias`:
        D205: 1 blank line required between summary line and description (found 0)
src/jack.py:1463 in public method `set_alias`:
        D209: Multi-line docstring closing quotes should be on a separate line
src/jack.py:1470 in public method `unset_alias`:
        D205: 1 blank line required between summary line and description (found 0)
src/jack.py:1470 in public method `unset_alias`:
        D209: Multi-line docstring closing quotes should be on a separate line
src/jack.py:1470 in public method `unset_alias`:
        D400: First line should end with a period (not ',')
...

Please rename the example file, because files starting and ending with "test" should be reserved for automated tests (e.g. py.test looks for those files).

Comment thread src/jack.py Outdated

@property
def aliases(self):
"""A list of 2 strings containing the aliases for the JACK port."""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please updated the docstring to reflect the changed code.

Comment thread src/jack.py Outdated
aliases=[_ffi.new(ctype), _ffi.new(ctype)]
aliasesptr=_ffi.new("char *[]", aliases)
result=[]
if (_lib.jack_port_get_aliases(self._ptr, aliasesptr) > 0):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The outer parentheses are not necessary.

The "> 0" isn't strictly necessary either, but you can keep it if you think it makes the situation clearer.

Comment thread src/jack.py Outdated
@property
def aliases(self):
"""A list of 2 strings containing the aliases for the JACK port."""
ctype="char[%d]" % _lib.jack_port_name_size()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use .format(), because that's used in the rest of the code, too.

Comment thread examples/test_alias.py Outdated
ports = client.get_ports()

print('Testing set_alias() ...')
i=0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jofemodo

Copy link
Copy Markdown
Contributor Author

OK @mgeier! I hope everything is OK now ;-)

@mgeier

mgeier commented Jan 29, 2018

Copy link
Copy Markdown
Member

Thanks for the changes!

Did you consider these comments of mine?

You are using tabs in the example file, which is a no-go. You should check for PEP-8 issues (e.g. with python3 -m pycodestyle examples/aliases.py). Ideally, you should use a plugin for your text editor to show those issues.

Finally, please also use .format() in the example.

@jofemodo

Copy link
Copy Markdown
Contributor Author

OK! Let's see now ... cross my fingers ;-D

@mgeier
mgeier merged commit 3b78622 into spatialaudio:master Jan 29, 2018
@mgeier

mgeier commented Jan 29, 2018

Copy link
Copy Markdown
Member

Thanks a lot @jofemodo!

@jofemodo

Copy link
Copy Markdown
Contributor Author

It's been a pleasure, @mgeier ;-)

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