So I am parsing strings that represent python types and other custom objects. An example would be '<Vector (1.0 , 0.2 , 0.8) >' or '<Color (0.3 , 0.9 , 0.4 )>' |
Administrator
|
IIUC I would parse just enough to separate the class name from the data, and then let each subclass parse the data. Something like: PythonObject fromString: '< Bone [ "side bone" , IK = True , ( 1.0 , 0.1 , 0.2) ]>'. where: PythonObject>>#fromString: aString | bracketContents pythonClassName data class | bracketContents := aString allButFirst allButLast trimBoth. pythonClassName := bracketContents copyUpTo: Character space. data := bracketContents copyAfter: Character space. class := self subclasses detect: [ :c | c pythonClassName = pythonClassName ]. ^ class data: data.
Cheers,
Sean |
oh Sean this so beautiful and elegant, exactly what I was looking for. Thank you very much. The rest of your code is what I was thinking doing with regex instead.class := self subclasses detect: [ :c | c pythonClassName = pythonClassName On Mon, Mar 9, 2015 at 11:02 PM, Sean P. DeNigris <[hidden email]> wrote: kilon.alios wrote |
Administrator
|
Yes I like/use this pattern a lot! I think I learned it from "A Mentoring Course on Smalltalk"
Cheers,
Sean |
I have seen a similar pattern in Kent Beck book Smalltalk Patterns , but I was not sure this was a good way to go in this case . So I thought that would not hurt to ask for a second opinion from a more experienced Pharo coder. On Tue, Mar 10, 2015 at 12:03 AM, Sean P. DeNigris <[hidden email]> wrote: kilon.alios wrote |
Free forum by Nabble | Edit this page |