Hey,
I am trying to rewrite my lib written in PHP into python. It handles all sphinx requests. In the init function, I am trying to set default search and match modes, but I have run into a little problem. I get the modes from a config file.
In PHP, you need to use a constant as an input:
$this->sphinx->SetMatchMode(constant($this->conf['match_mode']));
This will convert the string from config file into a constant and everything works. The tricky part starts in python, when I try to do this:
self.sphinx.SetMatchMode(self.config['match_mode'])
I get:
AssertionError in
assert(mode in [SPH_MATCH_ALL, SPH_MATCH_ANY, SPH_MATCH_PHRASE, SPH_MATCH_BOOLEAN, SPH_MATCH_EXTENDED, SPH_MATCH_FULLSCAN, SPH_MATCH_EXTENDED2])
In this case, the input should be an integer, but the input is a string and I cant convert it because I get an exception - the string is SPH_MATCH_ALL
.
invalid literal for int() with base 10: 'SPH_MATCH_ALL'
When I try this:
print type(self.config['match_mode']) # -- string
print type(SPH_MATCH_ALL) # -- integer
print SPH_MATCH_ALL # -- 1
print SPH_MATCH_ANY # -- 0
So my question would be, how can I convert the string into an integer or whatever it thinks it is, so I wont get an assertion error. Of course, that I could just do some if/else statements, but I dont want that. Is there any elegant way to do this?