Skip to content

Commit 489fa8c

Browse files
committed
Lint the unit test and loklak.py
settings() has been changed to a stand alone function.
1 parent 7486bbf commit 489fa8c

File tree

5 files changed

+62
-51
lines changed

5 files changed

+62
-51
lines changed

Examples/loklak-tweets-search.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from loklak import Loklak
2-
import json
32

43
myTweets = Loklak()
54

6-
movieName = raw_input("What movie would you like to search recent tweets for? ") #asks for what user would like to search for
5+
movieName = raw_input("What movie would you like to \
6+
search recent tweets for? ") #asks for what user would like to search for
77

88
index = 1
99
for tweet in myTweets.search(movieName)["statuses"]: #searches movie and finds "statuses" where "text" is under
10-
print '%d%s' %(index, '.') #prints the tweet number
11-
print tweet["text"] + "\n" #prints the text of the tweet and makes a new line
12-
index+=1 #increments index
13-
10+
print("%d%s", index, '.') #prints the tweet number
11+
print("%s\n", tweet["text"]) #prints the text of the tweet and makes a new line
12+
index += 1 #increments index

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Python Loklak API
2-
[![PyPI version](https://badge.fury.io/py/python-loklak-api.svg)](https://badge.fury.io/py/python-loklak-api)
2+
[![PyPI version](https://badge.fury.io/py/python-loklak-api.svg)](https://badge.fury.io/py/python-loklak-api)
33
[![Build Status](https://travis-ci.org/loklak/python-loklak-api.svg?branch=master)](https://travis-ci.org/loklak/python-loklak-api)
44
[![Code Health](https://landscape.io/github/loklak/python-loklak-api/master/landscape.svg?style=flat)](https://landscape.io/github/loklak/python-loklak-api/master)
55
--------------------------------------------
@@ -73,7 +73,7 @@ Using the object created above, `l.status()` returns a json of the status as fol
7373

7474
##### Settings of the loklak server (strictly only for localhost clients)
7575

76-
Using the object created above `l.settings()` returns a json of the settings being used by the loklak server
76+
Using the class method `settings()` to returns a json of the settings being used by the loklak server
7777

7878
##### Hello test - Check if the server is responding properly and is online
7979

loklak.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44
from __future__ import (absolute_import, division,
55
print_function, unicode_literals)
66

7-
import copy
8-
import math
9-
import sys
10-
import time
11-
from collections import namedtuple
12-
import requests
137
import json
8+
import requests
149

1510

1611
class Loklak(object):
@@ -30,7 +25,7 @@ class Loklak(object):
3025
limit = None
3126
action = None
3227
data = {}
33-
28+
3429
def status(self):
3530
"""Returns the status of the server"""
3631
status_application = 'api/status.json'
@@ -66,17 +61,18 @@ def geocode(self, places=None):
6661
return_to_user = {}
6762
return json.dumps(return_to_user)
6863

69-
def map(self, latitude, longitude, width=500, height=500, zoom=8, text=""):
70-
"""Returns a map of size 500x500"""
71-
map_application = 'vis/map.png'
72-
params = {'text': text, 'mlat': latitude, 'mlon': longitude,\
73-
'width': width, 'height': height, 'zoom': zoom}
74-
return_to_user = requests.get(self.baseUrl + map_application, \
75-
params=params, stream=True)
76-
if return_to_user.status_code == 200:
77-
return return_to_user.raw.read()
78-
else:
79-
return ''
64+
def get_map(self, latitude, longitude, width=500, height=500,
65+
zoom=8, text=""):
66+
"""Returns a map of size 500x500"""
67+
map_application = 'vis/map.png'
68+
params = {'text': text, 'mlat': latitude, 'mlon': longitude,
69+
'width': width, 'height': height, 'zoom': zoom}
70+
return_to_user = requests.get(self.baseUrl + map_application,
71+
params=params, stream=True)
72+
if return_to_user.status_code == 200:
73+
return return_to_user.raw.read()
74+
else:
75+
return ''
8076

8177
def peers(self):
8278
"""Gives the peers of a user"""
@@ -113,13 +109,14 @@ def user(self, name=None, followers=None, following=None):
113109
return json.dumps(return_to_user)
114110
else:
115111
return_to_user = {}
116-
return_to_user['error'] = 'No user name given to query. Please check and try again'
112+
return_to_user['error'] = ('No user name given to query. Please '
113+
'check and try again')
117114
return json.dumps(return_to_user)
118115

119116
def settings(self):
120117
"""Gives the settings of the application"""
121-
settings_application = 'settings.json'
122-
url_to_give = 'http://localhost:9000/api/'+settings_application
118+
settings_application = 'api/settings.json'
119+
url_to_give = self.baseUrl + settings_application
123120
return_to_user = requests.get(url_to_give)
124121
if return_to_user.status_code == 200:
125122
return return_to_user.json()
@@ -151,21 +148,24 @@ def search(self, query=None, since=None, until=None, from_user=None):
151148
return return_to_user.json()
152149
else:
153150
return_to_user = {}
154-
return_to_user['error'] = 'Something went wrong, Looks like the server is down.'
151+
return_to_user['error'] = ('Something went wrong, Looks like'
152+
' the server is down.')
155153
return json.dumps(return_to_user)
156154
else:
157155
return_to_user = {}
158-
return_to_user['error'] = 'No Query string has been given to run a query for'
156+
return_to_user['error'] = ('No Query string has been'
157+
' given to run a query for')
159158
return json.dumps(return_to_user)
160159

161-
def aggregations(self, query=None, since=None, until=None, fields=None, limit=None):
160+
def aggregations(self, query=None, since=None, until=None,
161+
fields=None, limit=None):
162162
"""Gives the aggregations of the application"""
163163
aggregations_application = 'api/search.json'
164164
url_to_give = self.baseUrl+aggregations_application
165165
self.query = query
166166
self.since = since
167167
self.until = until
168-
self.fields = fields # Array seperated values to be passed
168+
self.fields = fields
169169
self.limit = limit
170170
if query:
171171
params = {}
@@ -177,11 +177,11 @@ def aggregations(self, query=None, since=None, until=None, fields=None, limit=No
177177
if fields:
178178
field_string = ''
179179
for i in self.fields:
180-
field_string += i +','
180+
field_string += i + ','
181181
params['fields'] = field_string
182182
if limit:
183183
params['limit'] = self.limit
184-
if limit == None:
184+
if limit is None:
185185
limit = 6
186186
params['limit'] = self.limit
187187
params['count'] = 0
@@ -191,7 +191,8 @@ def aggregations(self, query=None, since=None, until=None, fields=None, limit=No
191191
return return_to_user
192192
else:
193193
return_to_user = {}
194-
return_to_user['error'] = 'Something went wrong, Looks like the server is down.'
194+
return_to_user['error'] = ('Something went wrong, '
195+
'Looks like the server is down.')
195196
return json.dumps(return_to_user)
196197
else:
197198
return_to_user = {}
@@ -208,32 +209,38 @@ def account(self, name=None, action=None, data=None):
208209
self.action = action
209210
# Simple GET Query
210211
headers = {
211-
'User-Agent': 'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0',
212+
'User-Agent': ('Mozilla/5.0 (Android 4.4; Mobile; rv:41.0)'
213+
' Gecko/41.0 Firefox/41.0'),
212214
213215
}
214216
if name:
215217
params = {}
216218
params['screen_name'] = self.name
217-
return_to_user = requests.get(url_to_give, params=params, headers=headers)
219+
return_to_user = requests.get(url_to_give, params=params,
220+
headers=headers)
218221
if return_to_user.status_code == 200:
219222
return return_to_user.json()
220223
else:
221224
return_to_user = {}
222-
return_to_user['error'] = 'Something went wrong, Looks query is wrong.'
225+
return_to_user['error'] = ('Something went wrong, '
226+
'Looks query is wrong.')
223227
return json.dumps(return_to_user)
224228
# if action = update and data is provided, then make request
225229
elif self.action == 'update' and data:
226230
params = {}
227231
params['action'] = self.action
228232
params['data'] = self.data
229-
return_to_user = requests.post(url_to_give, params=params, headers=headers)
233+
return_to_user = requests.post(url_to_give,
234+
params=params, headers=headers)
230235
if return_to_user.status_code == 200:
231236
return return_to_user.json()
232237
else:
233238
return_to_user = {}
234-
return_to_user['error'] = 'Something went wrong, Looks query is wrong.'
239+
return_to_user['error'] = ('Something went wrong,'
240+
' Looks query is wrong.')
235241
return json.dumps(return_to_user)
236242
else:
237243
return_to_user = {}
238-
return_to_user['error'] = 'No Query string has been given to run an query for account'
244+
return_to_user['error'] = ('No Query string has been given'
245+
' to run an query for account')
239246
return json.dumps(return_to_user)

sample.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import unittest
2-
import shutil
31
import tempfile
42
from loklak import Loklak
53
from pprint import pprint
@@ -40,8 +38,8 @@
4038
geocode = l.geocode(['New York', 'Singapore'])
4139
pprint(geocode)
4240

43-
# Loklak Map API
44-
png_data = l.map(17.582729, 79.118320, 512, 512, 12, 'Hello')
41+
# Loklak Geographic Map API
42+
png_data = l.get_map(17.582729, 79.118320, 512, 512, 12, 'Hello')
4543
path = tempfile.mkstemp(suffix=".png")[1]
4644
open(path, 'wb').write(png_data)
4745

@@ -110,8 +108,7 @@
110108
pprint(account1)
111109

112110
account2 = l.account('name','update','type')
113-
pprint(account3)
111+
pprint(account2)
114112

115113
# Try call function without name
116114
account3 = l.account(action='update', data='type')
117-

test.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33

44

55
class TestLoklak(unittest.TestCase):
6+
""""test class"""
67
def setUp(self):
8+
"""test proper setup"""
79
self.loklak = Loklak()
810

911
def test_hello(self):
12+
"""test hello instance"""
1013
result = self.loklak.hello()
1114
self.assertEqual(result, {u'status': u'ok'})
1215

1316
def test_geocode(self):
17+
"""test geological features"""
1418
result = self.loklak.geocode()
1519
self.assertEqual(result, '{}')
1620

@@ -26,25 +30,28 @@ def test_geocode(self):
2630
result['locations']['Moscow']['country']
2731
)
2832
self.assertTrue(
29-
type(result['locations']['Moscow']['place']) == list
33+
isinstance(result['locations']['Moscow']['place'], list)
3034
)
3135

3236
def test_peers(self):
37+
"""test finding peers"""
3338
result = self.loklak.peers()
3439
self.assertTrue('peers' in result)
35-
self.assertTrue(type(result['peers']) == list)
40+
self.assertTrue(isinstance(result['peers'], list))
3641
self.assertTrue(len(result['peers']) >= 1)
3742
self.assertEqual(len(result['peers']), result['count'])
3843

3944
def test_search(self):
45+
"""test search result"""
4046
result = self.loklak.search('doctor who')
4147
self.assertTrue('error' in self.loklak.search())
4248
self.assertTrue('statuses' in result)
43-
self.assertTrue(type(result['statuses']) == list)
49+
self.assertTrue(isinstance(result['statuses'], list))
4450
self.assertTrue(len(result['statuses']) >= 1)
4551
self.assertEqual(len(result['statuses']), int(result['search_metadata']['count']))
4652

4753
def test_status(self):
54+
"""test status"""
4855
result = self.loklak.status()
4956

5057
self.assertTrue('index_sizes' in result)
@@ -71,6 +78,7 @@ def test_status(self):
7178
)
7279

7380
def test_user(self):
81+
"""test user"""
7482
result = self.loklak.user('dhruvRamani98')
7583
self.assertTrue('error' in self.loklak.user())
7684
self.assertTrue('user' in result)

0 commit comments

Comments
 (0)