Conversation
|
👍 💃 |
|
I'd avoid using string concatenation. I think this reads a bit better (and does not violate pep8): class foo():
def __init__(self, name):
self.name = name
def __repr__(self):
s = '<{}.{}: {} at {}>'.format(
self.__module__,
self.__class__.__name__,
self.name,
id(self))
return sMaybe I sound silly, but a two liner test could also be a nice addition say somebody removes the name attribute, and does not update repr :D . Eventually qcodes should have 100% code coverage. 🍷 |
|
@giulioungaretti Using |
|
@peendebak or @eendebakpt (I am confused :P ). If you need help to write the test just ping. |
|
@giulioungaretti I added explicit conversion to str for the from qcodes import Parameter
def repr_test():
param = Parameter(name='myparam')
s=param.__repr__()eendebakpt or peendebak is indeed confusing. I wish there was a credential manager for git that couples to the directories instead of the user login... |
|
@peendebak not sure I understand what you say about git (but it would be nice to just have one of you contributing :P ). Back to the code .format casts automagically the types, so the str() in 4beeffa are not required. The reason why I am stressing the test is that, if you log the class and there is something wrong with the repr, you will get an exception which is not really pleasant :D Some pseudo code, assuming we have either a unittest.TestCase class or you add a method to a test class (f.ex in case of instrument.parameter, here ) Writing on my phone, so not really sure this is 100% correct 🌜 . def repr_test(self):
for i in [None, 0, "foo", "", "fåil"]:
with self.subTest(i=i):
param = Parameter(name=i)
s = param.__repr__()
#tweak this so it looks how you want the repr to look like
self.assertEqual(s , "foo.bar: {} {}".format(i, id(param) ) |
|
@giulioungaretti I do not see why the I added the test anyway, since if will not do much harm. |
|
@peendebak yes it's a bit overkill for now, and yes assert equal is not required as anything that implements a str method will pass, my bad, owe you a beer! But if one changes parameter class the test will now fail. example: class Parameter():
def __init__(self, name):
self.naame = name
def __repr__(self):
s = '<{}.{}: {} at {}>'.format(
self.__module__,
self.__class__.__name__,
self.name,
id(self))
return sor example: class Parameter():
def __init__(self, name):
self.name = name
def __repr__(self):
s = '<{}.{}: {} at {}>'.format(
self.__module__,
self.__class__.__name__,
self._name,
id(self))
return s |
|
@peendebak thanks, this is a good addition. @giulioungaretti thanks for pushing for robustness and tests. I suspect that we could make this DRYer as a mixin class ( I changed some nonstandard indentation. I think we can 💃 ! |
This gives a more informative output of a
Parameter(and derived classes):instead of