I'm trying to find a general way of generating objects which can be converted to strings and back again using the parse module. For example, for a class StringyObject
whose instances have just two attributes a
and b
:
import parse
class StringyObject(object):
fmt = "{a} {b}"
def __init__(self, a, b):
self.a = a
self.b = b
def __str__(self):
return self.fmt.format(a=self.a, b=self.b)
@classmethod
def parse(cls, string):
result = parse.parse(cls.fmt, string)
kwargs = result.named
return cls(**kwargs)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
else:
return NotImplemented
if __name__ == "__main__":
obj = StringyObject("foo", "bar")
reconstructed_obj = StringyObject.parse(str(obj))
assert reconstructed_obj == obj, "The reconstructed object should be equivalent to the original one."
The script consecutively calls the __str__
instance method and the parse
class method, and verifies that the resulting objects obj
and reconstructed_obj
are equivalent (defined here as being instances of the same class and having the same dictionaries; cf. Elegant ways to support equivalence ("equality") in Python classes).
So far, so good, but I'd like to extend this method to attributes which are lists of variable length. For example, if b
is a list, then I could do the following:
import parse
class StringyObject(object):
fmt = "{a} {b}"
separator = ", "
def __init__(self, a, b):
self.a = a
assert isinstance(b, list), "b should be a list."
self.b = b
def __str__(self):
b_string = self.separator.join(self.b)
return self.fmt.format(a=self.a, b=b_string)
@classmethod
def parse(cls, string):
result = parse.parse(cls.fmt, string)
kwargs = result.named
kwargs['b'] = kwargs['b'].split(cls.separator)
return cls(**kwargs)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
else:
return NotImplemented
if __name__ == "__main__":
obj = StringyObject("foo", ["bar1", "bar2"])
reconstructed_obj = StringyObject.parse(str(obj))
assert reconstructed_obj == obj, "The reconstructed object should be equivalent to the original object."
This still works for this example, but is less elegant because I start to have to use join()
and split()
, which is what I wanted to avoid by using parse.parse
. Furthermore, if I add another attribute c
which comes after b
in the string representation, the parsing goes haywire:
class StringyObject(object):
fmt = "{a} {b} {c}"
separator = ", "
def __init__(self, a, b, c):
self.a = a
assert isinstance(b, list), "b should be a list."
self.b = b
self.c = c
def __str__(self):
b_string = self.separator.join(self.b)
return self.fmt.format(a=self.a, b=b_string, c=self.c)
Then running the script
obj = StringyObject("foo", ["bar1", "bar2"], "hello")
result = parse.parse(StringyObject.fmt, str(obj))
produces the wrong Result
object:
<Result () {'a': 'foo', 'c': 'bar2 hello', 'b': 'bar1,'}>
What I would actually like to is implement a kind of 'sub-parser' for b
which keeps on running as long as it can find a separator
, and only then continues with parsing c
. Is there an elegant way to do this?
' '.join()
do not generate a list but a string from list