-
Notifications
You must be signed in to change notification settings - Fork 3
Using Cahoots In Your Application
Ryan Vennell edited this page Mar 24, 2015
·
2 revisions
- You must bootstrap Cahoots in your application or it will not work correctly and errors will occur.
- The Cahoots bootstrap process is something you typically will only want to execute once during the execution of your application because it's intensive.
- If you plan on creating only one instance of the Cahoots parser and then reusing that instance as needed in your application, you may wish to bootstrap upon instantiation.
- If you plan on instantiating the Cahoots parser many times throughout your application, you will want to bootstrap cahoots as part of your application's launch process and then not bootstrap upon creation of Cahoots instances. This requires you to provide the bootstrap process with an instance of your config or the BaseConfig.
Bootstrap Upon Instantiation:
from cahoots.parser import CahootsParser
cahoots = CahootsParser(bootstrap=True)
Bootstrap As Part Of Application Launch:
from cahoots.parser import CahootsParser
from cahoots.config import BaseConfig
CahootsParser.bootstrap(BaseConfig())
...
cahoots = CahootsParser()
...
cahoots = CahootsParser()
Note: If you bootstrap at the start of your application and want to use a custom config, you will still have to provide your config to the constructor of each cahoots instance you create:
If the BaseConfig does not contain the settings you want, you will need to create a config class that extends the base config and implements the options you want.
Using the BaseConfig:
from cahoots.parser import CahootsParser
cahoots = CahootsParser(bootstrap=True)
Using An Override/Replacement Config:
from cahoots.parser import CahootsParser
from myapp.config import MyCustomConfigClass
cahoots = CahootsParser(config=MyCustomConfigClass, bootstrap=True)
The parse method accepts a string and returns a dict containing result data from the parsing process.
>>> from cahoots.parser import CahootsParser
>>> cahoots = CahootsParser(bootstrap=True)
>>> results = cahoots.parse('http://www.google.com/')
>>> results
{
'date': '2015-03-22T23:47:36.340187',
'query': 'http://www.google.com/',
'top': <cahoots.result.ParseResult object>,
'results': {
'count': 1,
'matches': [
<cahoots.result.ParseResult object>
],
'types': [
"URI"
]
},
'execution_seconds': 0.006306886672973633
}
Represented as JSON:
{
"date": "2015-03-22T23:47:36.340187",
"query": "http://www.google.com/",
"top": {
"subtype": "URL",
"confidence": 100,
"type": "URI",
"value": null,
"data": {}
},
"results": {
"count": 1,
"matches": [
{
"subtype": "URL",
"confidence": 100,
"type": "URI",
"value": null,
"data": {}
}
],
"types": [
"URI"
]
},
"execution_seconds": 0.006306886672973633
}