Commits

Anonymous committed 5cc7141

Cleaned up python api and updated tests to v2

Comments (0)

Files changed (3)

         self.version = version
 
     def _call_api(self, api_function, use_http_get=False, response_type='json', **params):
-        
+
         assert response_type in ('json', 'xml'), "%s is not a valid response type" % response_type
 
         params = dict((x, y) for x, y in params.iteritems() if y is not None)
-        
+
         data = urllib.urlencode(params)
-        
+
         url_args = dict(
             response_type=response_type,
             function=api_function,
             key=self.api_key,
             version=self.version,
         )
-        
+
         if api_function == 'powerpoint':
             url = self.ppt_url % url_args
             response = urllib2.urlopen(url, data)
             result = response.read()
         else:
             url = self.url_template % url_args
-        
+
             if use_http_get:
                 url = '%s?%s' % (url, data)
                 response = urllib2.urlopen(url)
             if response_type == 'json':
                 result = json.load(response)
             else:
-                result = etree.ElementTree.fromstring(response)
-            
+                result = etree.fromstring(response.read())
+
         return result
 
-    def _call_natural_language(self, api_function, cloud=None, text=None, url=None):
+    def _call_natural_language(self, api_function, cloud=None, text=None,
+                               url=None, response_type=None):
         """
         Helper function for the NLP calls.
         """
-        return self._call_api(api_function, use_http_get=(not text), cloud=cloud, text=text, url=url)
+        return self._call_api(api_function, cloud=cloud, text=text, url=url,
+                              response_type=response_type)
 
     def sentiment(self, text=None, url=None, response_type='json'):
         """
-        Retreive the sentiment for a single URl or block of text.
+        Retrieve the sentiment for a single URl or block of text.
         """
-        return self._call_api('score', text=text, url=url)
+        return self._call_api('score', text=text, url=url,
+                              response_type=response_type)
 
     def bulk_sentiment(self, items=None, response_type='json'):
         """
         Bulk score multiple pieces of text (not urls!).
         """
         items_to_score = {}
-        
+
         for idx, item in enumerate(items):
             items_to_score['text%d' % idx] = item
-        
-        return self._call_api('bulk-score', **items_to_score)
-    
+
+        return self._call_api('bulk-score', response_type=response_type,
+                              **items_to_score)
+
     def clean_html(self, url=None, response_type='json'):
         """
         Clean up a web page. It doesn't work well on home pages - it's designed for content pages.
         """
-        return self._call_api('clean-html', use_http_get=True, url=url)
+        return self._call_api('clean-html', use_http_get=True, url=url,
+                              response_type=response_type)
 
     def adjectives(self, cloud=None, text=None, url=None, response_type='json'):
-        return self._call_natural_language('adj', cloud=cloud, text=text, url=url)
+        return self._call_natural_language('adj', cloud=cloud, text=text,
+                                           url=url, response_type=response_type)
 
     def verbs(self, cloud=None, text=None, url=None, response_type='json'):
-        return self._call_natural_language('verb', cloud=cloud, text=text, url=url)
+        return self._call_natural_language('verb', cloud=cloud, text=text,
+                                           url=url, response_type=response_type)
 
-    def ngrams(self, url=None, text=None, max=None, min=None, freq=None, stopwords=None, response_type='json'):
-        return self._call_api('ngrams', use_http_get=bool(url), text=text, url=url, max=max, min=min, freq=freq, stopwords=stopwords,)
-    
+    def ngrams(self, url=None, text=None, max=None, min=None, freq=None,
+               stopwords=None, response_type='json'):
+        return self._call_api('ngrams', use_http_get=bool(url), text=text,
+                              url=url, max=max, min=min, freq=freq,
+                              stopwords=stopwords, response_type=response_type)
+
     def date_extraction(self, text, response_type='json'):
         """
         Convert english date indicators like "today", "tomorrow", "next week"
         into date strings like 2011-01-12.
         """
-        return self._call_api('extract-dates', text=text)
-    
+        return self._call_api('extract-dates', text=text,
+                              response_type=response_type)
+
     def powerpoint(self, report_title, author, images, titles):
         """
         Given a list of images and titles, generate a simple powerpoint presentation.
     to yours if you want to actually run this.
     """
     client = Repustate(api_key='demokey', version='v2')
-    
+
     # Score a single piece of text.
     score = client.sentiment(text='I hate food.', response_type='txt')
-    
+
     # Score multiple pieces of text.
     scores = client.bulk_sentiment(['I love candy', 'I hate fish', 'I want to go watch a movie'], response_type='xml')
-    
+
     # To make this example work, you'll have to have an image called 'test.jpg'
     # in this directory. A new power point presentation called "Test.ppt" will
     # be saved in the current directory.

python/repustate.gif

Added
New image

python/test_repustate.py

+from lxml import etree
 import unittest2 as unittest
 
 from client import Repustate
 
 class TestRepustate(unittest.TestCase):
 
+    text = 'iPad'
+
+    url = 'http://tcrn.ch/aav9Ty'
+
     key = '0c9cf39718d8ce33e5310047617f8020984d8f18'
 
     def get_accessor(self):
-        return Repustate(self.key, 'v1')
+        return Repustate(self.key, 'v2')
 
-    def check_call(self, func_name, **kwargs):
+    def check_status(self, result, response_type):
+        if response_type == 'json':
+            expected_type = dict
+            status = result.get('status')
+        else:
+            expected_type = etree._Element
+            status = result.findtext('status')
+        self.assertIsInstance(result, expected_type, 'Invalid result')
+        self.assertEqual(status, 'OK')
+
+    def check_call(self, func_name, check_status=True, **kwargs):
         func = getattr(self.get_accessor(), func_name)
         result = func(**kwargs)
-        self.assertIsInstance(result, dict, 'Invalid result')
-        self.assertEqual(result.get('status'), 'OK', result)
+        self.assertIsNotNone(result, 'Invalid result')
+        if check_status:
+            self.check_status(result, kwargs.get('response_type', 'json'))
+
+    def test_xml_response_type(self):
+        self.check_call('sentiment', text=self.text, response_type='xml')
 
     def test_sentiment_for_text(self):
-        self.check_call('sentiment', text='iPad')
+        self.check_call('sentiment', text=self.text)
 
     def test_sentiment_for_url(self):
-        self.check_call('sentiment', url='http://www.twitter.com')
+        self.check_call('sentiment', url=self.url)
 
     def test_bulk_sentiment(self):
         self.check_call('bulk_sentiment', items=['I love candy'])
 
     def test_adjectives_for_text(self):
-        self.check_call('adjectives', text='iPad')
+        self.check_call('adjectives', text=self.text)
 
     def test_adjectives_for_url(self):
-        self.check_call('adjectives', url='http://www.twitter.com')
+        self.check_call('adjectives', url=self.url)
 
     def test_verbs_for_text(self):
-        self.check_call('verbs', text='iPad')
+        self.check_call('verbs', text=self.text)
 
     def test_verbs_for_url(self):
-        self.check_call('verbs', url='http://www.twitter.com')
+        self.check_call('verbs', url=self.url)
 
     def test_clean_html(self):
-        self.check_call('clean_html', url='http://tcrn.ch/aav9Ty')
+        self.check_call('clean_html', url=self.url)
 
     def test_ngrams_for_text(self):
-        self.check_call('ngrams', text='iPad')
+        self.check_call('ngrams', text=self.text)
 
     def test_ngrams_for_url(self):
-        self.check_call('ngrams', url='http://tcrn.ch/aav9Ty')
+        self.check_call('ngrams', url=self.url)
+
+    def test_date_extraction(self):
+        self.check_call('date_extraction', text=self.text)
+
+    def test_powerpoint(self):
+        kwargs = dict(
+            report_title='Test Report',
+            author='Martin Ostrovsky',
+            images=['repustate.gif'],
+            titles=['A test title'],
+            )
+        self.check_call('powerpoint', check_status=False, **kwargs)