I am trying to make a parser in python which can achieve two types of functionality
s1 = foo()
s2 = {'k1':v1,'k2':v2}
s3 = [v1,v2,v3...]
I say two types… where first type is usually objects and second types are kind off like variables.
Now First one is easy..
def parse_string_to_command(string):
if "foo" in string:
#handle this.
elif # handle s2
elif #condition to handle s3
EDIT 1:
I think I didnt clearly stated what I am trying to achive.
All I am trying to do is follows:
Everything that passes from that function is a string.. some are methods.. while others are variables.
I am just trying to handle them accordingly.
Basically this is what user will be doing
> params = {"input":"foobar"}
> foo = Foo(params)
Now, to handle Foo method.. I do this:
if "Foo" in string:
tokens = string.split("=")
# I have tokens [foo,Foo(params)]
But params is a string now..whereas it is needed to be a dictionary.
Now I know i can just handle it here..inside Foo method.. but in general I want to handle these variable assignments inside parse_string_to_command function
Is it making any sense.
You might want to look into using pythons JSON parser for accomplishing your goal.
or if you need it completely custom… with out the intention to write a complete parser for you, I imagine using some regular expressions will help make your job easier.
To just identify the 3 types of input strings: