Dealing with multiple cases of a string while keeping code short and OOP friendly
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 )>' etc But I dont want to use too many ifTrue , ifFalse and end up with a long method. So I was thinking creating a OrderedCollection that would be 2D , first dimension is the regex that fits the type the second is the pharo object that corresponds to this type . Then I will have a common method for each of those pharo object to deal with the parsing. But I was wondering how to use a class that I dont know its name until runtime. For example lets say i get a string aPythonString := '< Bone [ "side bone" , IK = True , ( 1.0 , 0.1 , 0.2) ]>' I would want in this case to send the message the EphBoneType parse : aPythonString. The problem is that I dont know the contents of the string until I receive it. So I want the name of the class to be composed according the string received . Is this possible ? Is this a smart way ? How would you do it ?
kilon.alios wrote
Is this possible ? Is this a smart way ? How would you do it ?
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 -- View this message in context: http://forum.world.st/Dealing-with-multiple-cases-of-a-string-while-keeping-... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
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. This was the part I was not aware of class := self subclasses detect: [ :c | c pythonClassName = pythonClassName On Mon, Mar 9, 2015 at 11:02 PM, Sean P. DeNigris <sean@clipperadams.com> wrote:
kilon.alios wrote
Is this possible ? Is this a smart way ? How would you do it ?
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 -- View this message in context: http://forum.world.st/Dealing-with-multiple-cases-of-a-string-while-keeping-... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
kilon.alios wrote
oh Sean this so beautiful and elegant class := self subclasses detect: [ :c | c pythonClassName = pythonClassName
Yes I like/use this pattern a lot! I think I learned it from "A Mentoring Course on Smalltalk" ----- Cheers, Sean -- View this message in context: http://forum.world.st/Dealing-with-multiple-cases-of-a-string-while-keeping-... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
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 <sean@clipperadams.com> wrote:
kilon.alios wrote
oh Sean this so beautiful and elegant class := self subclasses detect: [ :c | c pythonClassName = pythonClassName
Yes I like/use this pattern a lot! I think I learned it from "A Mentoring Course on Smalltalk"
----- Cheers, Sean -- View this message in context: http://forum.world.st/Dealing-with-multiple-cases-of-a-string-while-keeping-... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
participants (2)
-
kilon alios -
Sean P. DeNigris