def gettags(conf, dlcs, *tags, **opts): """Print info about tag. """ if opts["ignore_case"]: tags = [t.lower() for t in tags] for tag in cached_tags(conf, dlcs, opts["keep_cache"])["tags"]: if tag["tag"] in findtags or (opts["ignore_case"] and tag["tag"].lower() in findtags): print jsonwrite(tag)
def process(q, msg_obj): now = datetime.now() log_file = now.strftime(LOG_FILE) log_path = os.path.join(LOG_DIR, log_file) event_dict = msg_obj.as_dict() json_event = jsonwrite(event_dict) with open(log_path, 'w') as f: f.write(json_event)
def getGeniusLyrics(self): from json import dump as jsonwrite import lyricsgenius from os import getenv genius = lyricsgenius.Genius(getenv("GENIUS")) for song in self.songs: print("-=" * 32 + "-\n" + song + ":") if song["Lyrics"] == []: try: result = [ line for line in genius.search_song( song.split(".")[0].split(" - ")[1], song.split(".")[0].split(" - ")[0]).lyrics.split( "\n") if not "[" in line ] self.songs[song]["lyrics"] = result except: print("None") else: print("Already has lyrics!") with open(self.location + "songs.json", "w") as jsonfile: jsonwrite(self.songs, jsonfile, indent=4)
def call(self, function, args, data_converter=None, headers=None): """The actual remote call txtr reaktor. Internal only. function: string, '<interface>.<function>' of txtr reaktor args: list of arguments for '<interface>.<function>' Return: ReaktorObject of list of ReaktorObject's """ # some args might not be JSON-serializable, e.g. sets params = [list(arg) if isinstance(arg, set) else arg for arg in args] # mandatory RPC ID request_id = id_generator() # json-encode request data post = jsonwrite({u"method": function, u"params": params, u"id": request_id}) response = None try: response = self.http_service.call(post, headers or {}) finally: resp_status = response.status if response else 'ERR' resp_time = response.time if response else -1 resp_data = response.data if response else None summary = dict( request=u'POST %s %s %s' % (function, params, self.http_service.protocol), status=resp_status, length=len(post), duration=resp_time, request_id=request_id, headers=headers ) if self.history is not None: self.history.append(summary) logger_request.info(summary['request'], extra=summary) if resp_data: logger.debug(resp_data) # raise ReaktorHttpError for http response status <> 200 if not response.status == 200: raise ReaktorHttpError( response.status, u"server returned status %i: %s" % (response.status, response.data)) # json-decode response data data = jsonread(response.data) # raise ReaktorApiError for reaktor errors err = data.get("error") if err: code = err.get("reaktorErrorCode", err.get("code", "error code unknown")) msg = err.get("msg", unicode(code)) call_id = err.get("callId") if code == ReaktorApiError.AUTHENTICATION_INVALID: raise ReaktorAuthError(msg, call_id) elif code == ReaktorApiError.DISCOVERY_SERVICE_ACCESS_ERROR: raise ReaktorAccessError(msg, call_id) elif code == ReaktorApiError.ILLEGAL_ARGUMENT_ERROR: raise ReaktorArgumentError(msg, call_id) elif code == ReaktorApiError.UNKNOWN_ENTITY_ERROR: raise ReaktorEntityError(msg, call_id) elif code == ReaktorApiError.ILLEGAL_CALL: raise ReaktorIllegalCallError(msg, call_id) else: raise ReaktorApiError(msg, code, call_id) # check response RPC ID _after_ checking for ReaktorAPIError # somebody didn't read http://www.jsonrpc.org/specification response_id = data.get("id", "") if response_id != request_id: raise ReaktorJSONRPCError( response.status, u"invalid RPC ID response %s != request %s" % ( response_id, request_id)) # return result as ReaktorObject('s) - if Reaktor doesn't violate the # JSONRPC spec by not sending a result. data = data.get("result", {}) if data_converter is None: data_converter = ReaktorObject.to_reaktorobject return data_converter(data)
def output_prettyjson(data): return prettify_json(jsonwrite(data))
def output_json(data): return jsonwrite(data)
def saveAuthorCounts(authorCounts): with open('authorCounts.json','w+') as jsonfile: jsonwrite({k:dict(sorted(v.items(),key=lambda item:item[1],reverse=True)) for k,v in authorCounts.items()},jsonfile,indent=4)
for m in v[:messagesToTest]: tests += 1 m = formatM(m,testN) result = sorted([(key,sum([(0 if not word in value.keys() else value[word]) for word in m])) for key,value in authorCounts.items()],key=lambda item:item[1],reverse=True) if result[0][0] == k: correctFirst += 1 elif result[1][0] == k: correctSecond += 1 # elif result[2][0] == k: # correctThird += 1 result = (correctFirst+correctSecond/2)/tests results[testN][lengthWeight][probabilityWeight] = [correctFirst,correctSecond] if result > bestResult[0]: print(' '*12+'!!!!! NEW BEST !!!!!') bestResult = [result,[[testN,lengthWeight,probabilityWeight]]] elif result == bestResult[0]: print(' '*12+'!!! ANOTHER BEST !!!') bestResult[1] += [[testN,lengthWeight,probabilityWeight]] # print(f"{' '*12}Accuracy (first try)): {int(correctFirst/tests*1000)/10}%") # print(f"{' '*12}Accuracy (atleast second try)): {int((correctFirst+correctSecond)/tests*1000)/10}%") # print(f"{' '*12}Accuracy (atleast third try)): {int((correctFirst+correctSecond+correctThird)/tests*1000)/10}%") print('Best results:') print(' Score:',bestResult[0]) for i,result in enumerate(bestResult[1]): print(f' Result {i+1}:') print(' '*8+'ns:',result[0]) print(' '*8+'length weight:',result[1]) print(' '*8+'probability weight:',result[2]) with open('accuracyResults.json','w+') as jsonfile: jsonwrite(results,jsonfile)