Created
May 4, 2013 16:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
# The client code | |
@my.route('/cleanjson/<collection>/') | |
def getcleanjson(collection): | |
return Cleanjson().get_mongojson(str(collection), "?s={'_id': 1}", format=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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