Skip to content

Instantly share code, notes, and snippets.

@holyjak
Created May 4, 2013 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyjak/5517911 to your computer and use it in GitHub Desktop.
Save holyjak/5517911 to your computer and use it in GitHub Desktop.
Method Promiscuity Or The Case For Encapsulation: Do not expose more information and structure to the clients than necessary - improved version See https://gist.github.com/jakubholynet/5517865
...
# The client code
@my.route('/cleanjson/<collection>/')
def getcleanjson(collection):
return Cleanjson().get_mongojson(str(collection), "?s={'_id': 1}", format=True)
# The API code
...
class Cleanjson:
# No self.cleanjson attribute anymore
def get_mongojson(self, collection, query, format=False):
request_url = self._make_requesturl(collection, query)
alldata = requests.get(request_url).json()
rawjson = alldata['rows']
if format:
return __parse_json(rawjson)
else:
return rawjson
def __parse_json(self, datalist):
cleaned = [ { "date": datalist[i]['timestamp'], ... }
for i in range(len(datalist)) ]
return json.dumps(cleaned)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment