def we(self, value, expected_error_message, error_type=None, **kwargs): if error_type is None: error_type = WriteError try: write(value, **kwargs) except error_type, error: self.assertEqual(unicode(error), expected_error_message)
def GET(self): q = web.input()["q"] elasticQuery = { "filtered": { "query": # highlight seems to not work with fuzzy { "fuzzy": { "_all": { "value": q, "prefix_length": 2 } } }, "filter": { "or": [ { "prefix": { "value": q, "boost": 2.0 } }, ] } } } response = elastic.request(method="get", path="mpd/song/_search", payload=jsonlib.write(elasticQuery)) result = jsonlib.read(response.body_string()) web.header("Content-Type", "application/json") return jsonlib.write(result)
def test_fail_nonascii_bytestring(self): # Test that that ASCII is the default codec try: write(['Fail\xa2']) self.fail("No exception raised") except UnicodeDecodeError, exc: self.assertEqual(exc.encoding, 'ascii')
def getToken(self, userId, itemId, qty, isMobile): itemObj = inventory.getItem(itemId) if qty is None: itemObj['qty'] = 0 else: itemObj['qty'] = qty dirRoot = "http://%s%s" % (os.environ['HTTP_HOST'], os.environ['PATH_INFO']) p = re.compile( '/[a-z,A-Z]+.py$') dirRoot = p.sub( '/', dirRoot) postDetails = { 'USER': common.UID, 'PWD': common.PASSWORD, 'SIGNATURE': common.SIG, 'METHOD': 'SetExpressCheckout', 'VERSION': common.VER, 'PAYMENTREQUEST_0_CURRENCYCODE': 'USD', 'PAYMENTREQUEST_0_AMT': float(itemObj.get('qty', 0)) * float(itemObj.get('amt', 0)), 'PAYMENTREQUEST_0_TAXAMT': '0', 'PAYMENTREQUEST_0_DESC': 'JS Wars', 'PAYMENTREQUEST_0_PAYMENTACTION': 'Sale', 'L_PAYMENTREQUEST_0_ITEMCATEGORY0': itemObj.get('category', None), 'L_PAYMENTREQUEST_0_NAME0': itemObj.get('name', None), 'L_PAYMENTREQUEST_0_NUMBER0': itemObj.get('number', 0), 'L_PAYMENTREQUEST_0_QTY0': itemObj.get('qty', 0), 'L_PAYMENTREQUEST_0_TAXAMT0': '0', 'L_PAYMENTREQUEST_0_AMT0': itemObj.get('amt', 0), 'L_PAYMENTREQUEST_0_DESC0': itemObj.get('desc', None), 'PAYMENTREQUEST_0_SHIPPINGAMT': '0', 'PAYMENTREQUEST_0_SHIPDISCAMT': '0', 'PAYMENTREQUEST_0_TAXAMT': '0', 'PAYMENTREQUEST_0_INSURANCEAMT': '0', 'PAYMENTREQUEST_0_PAYMENTACTION': 'sale', 'L_PAYMENTTYPE0': 'sale', 'PAYMENTREQUEST_0_CUSTOM': '%s,%s' % (userId, itemObj.get('number', 0)), 'RETURNURL': '%ssuccess.py?data=%s|%s|%s' % (dirRoot, float(itemObj.get('qty', 0)) * float(itemObj.get('amt', 0)), userId, itemId), 'CANCELURL': '%scancel.py' % dirRoot } response = common.curl(common.URLBASE, postDetails) #forward the user to login and accept transaction redirect = common.URLREDIRECTINCONTEXT if isMobile == 'true': redirect = common.URLREDIRECT redirect = "%s?token=%s" % (redirect, dict(cgi.parse_qsl(response))['TOKEN']) returnObj = { 'success': 'true', 'redirecturl': redirect } print json.write(returnObj)
def verifyPurchase(self, userId, itemId, transactions): returnObj = None #json correctness hack - remove surrounding quotes on each dict in the array transactions = urllib2.unquote(transactions) p = re.compile('"{') transactions = p.sub('{', transactions) p = re.compile('}"') transactions = p.sub('}', transactions) if transactions is not None: arrTransactions = json.read(transactions) transactionId = None transactions = json.read(transactions) if transactions is not None: for item in transactions: if item['itemId'] == itemId: transactionId = item['transactionId'] if transactionId is not None: postDetails = { 'USER': common.UID, 'PWD': common.PASSWORD, 'SIGNATURE': common.SIG, 'METHOD': 'GetTransactionDetails', 'VERSION': common.VER, 'TRANSACTIONID': transactionId } response = dict( cgi.parse_qsl(common.curl(common.URLBASE, postDetails))) custom = string.split(response["CUSTOM"], ',') if (identity.getUserId() == custom[0]) and (itemId == custom[1]): returnObj = { 'success': 'true', 'error': '', 'transactionId': response["TRANSACTIONID"], 'orderTime': response["ORDERTIME"], 'paymentStatus': response["PAYMENTSTATUS"], 'itemId': itemId, 'userId': userId } else: returnObj = { 'success': 'false', 'error': 'Item not found in transaction history' } print json.write(returnObj)
def is_a_hostpost(search, point): """Determines if something is going on around this point""" query = { "query": { "filtered" : { "query" : { "match_all" : {}, }, "filter" : { "geo_distance" : { "distance" : "0.05km", "pin.location" : { "lat" : point[0], "lon" : point[1] } } } } } } results = search.post(query, 'pin', search=True) logging.debug('[query]: %s', jsonlib.write(query)) if results: results = results.get('hits') logging.debug('[results] : %s', results) total = results.get('total') if total > 10: logging.info('[hotspot] %s', results) return True return False
def dump_json(json_obj, is_utf8=True, ind=None): '''Build json string from json object using jsonlib''' # if is_utf8 is true, convert utf8 string in json_obj to unicode if is_utf8: _decode_utf8(json_obj) return jsonlib.write(json_obj, ascii_only=False, indent=ind)
def sendOutIfFull(): if MeasureIt.queue.qsize() < 1 or not UDP_IP: return with MeasureIt.lock: if MeasureIt.to_be_sent is None: MeasureIt.to_be_sent = "[ " try: while True: item = MeasureIt.queue.get_nowait() item_json = jsonlib.write(item) if len(MeasureIt.to_be_sent) + len( item_json) + 2 > MAX_FRAME and len( MeasureIt.to_be_sent) > 2: # print "Sending..." MeasureIt.to_be_sent = "%s%s" % ( MeasureIt.to_be_sent[:-1], ",\"%s\",%.2f ]" % (API_KEY, time.time())) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(zlib.compress(MeasureIt.to_be_sent), (UDP_IP, UDP_PORT)) # print "sent", MeasureIt.to_be_sent #print len(zlib.compress(MeasureIt.to_be_sent)) MeasureIt.to_be_sent = "[ %s," % (item_json, ) else: MeasureIt.to_be_sent = "%s%s," % (MeasureIt.to_be_sent, item_json) # print MeasureIt.to_be_sent MeasureIt.queue.task_done() except: import sys print sys.exc_info() pass
def ping(self, issue, command, created, creator): """ note: we might send multiple pings if a command was of more than one class """ for row in self.graph.queryd( "SELECT DISTINCT ?cls WHERE { ?cmd a ?cls }", initBindings={'cmd' : command}): payload = jsonlib.write({ 'issue' : issue, 'commandClass' : row['cls'], 'command' : command, 'created' : created, 'creator' : creator, }) for receiver in [ "http://bang:9030/dispatch/newCommand", # ^ this one should be dispatching to the rest, but i # don't have a proper PSHB setup yet "http://bang:9055/newCommand", "http://bang:9072/newCommand", ]: try: restkit.request( method="POST", url=receiver, body=payload, headers={"Content-Type" : "application/json"}) except Exception, e: log.error(e)
def sendOutIfFull(): if MeasureIt.queue.qsize() < 1 or not UDP_IP: return with MeasureIt.lock: if MeasureIt.to_be_sent is None: MeasureIt.to_be_sent = "[ " try: while True: item = MeasureIt.queue.get_nowait() item_json = jsonlib.write(item) if len(MeasureIt.to_be_sent) + len(item_json) + 2 > MAX_FRAME and len(MeasureIt.to_be_sent) > 2: # print "Sending..." MeasureIt.to_be_sent = "%s%s" %(MeasureIt.to_be_sent[:-1], ",\"%s\",%.2f ]" % (API_KEY, time.time())) sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) sock.sendto( zlib.compress(MeasureIt.to_be_sent), (UDP_IP, UDP_PORT) ) # print "sent", MeasureIt.to_be_sent #print len(zlib.compress(MeasureIt.to_be_sent)) MeasureIt.to_be_sent = "[ %s," % (item_json,) else: MeasureIt.to_be_sent = "%s%s," %(MeasureIt.to_be_sent, item_json) # print MeasureIt.to_be_sent MeasureIt.queue.task_done() except: import sys print sys.exc_info() pass
def send_packet(self, packet): try: json = jsonlib.write(packet) for cli in self.clients_ok: self.server.send_data('tcp', json, cli) except Exception, msg: print msg
def request(self, task): print task strs = task.content.split(" ") if len(strs) != 5: return "" tickets = Parse(strs[0], strs[1], strs[2], strs[3], strs[4]) return jsonlib.write(tickets)
def _generateOutput(requestedContentTypes, pcs): ctype = None page = "" for requestedType in requestedContentTypes: if requestedType == "text/plain": ctype = "text/plain" textarray = [] for row in pcs: textarray.append(" ".join([str(x) for x in row])) textarray = "\n".join(textarray) page = textarray break elif requestedType == "text/html": ctype = "text/html" textarray = [] for row in pcs: textarray.append("<tr>" + "".join(["<td>" + str(x) + "</td>\n" for x in row]) + "</tr>\n") page = "<html><body><table>" + "\n".join(textarray) + "</table></body></html>" break elif requestedType == "application/json": import jsonlib ctype = "application/json" page = jsonlib.write(pcs.tolist()) break return ctype, page
def commitPayment(self, userId, payerId, token, amt, itemId): returnObj = {} if identity.verifyUser(userId): postDetails = { 'USER': common.UID, 'PWD': common.PASSWORD, 'SIGNATURE': common.SIG, 'METHOD': 'DoExpressCheckoutPayment', 'VERSION': common.VER, 'AMT': amt, 'TOKEN': token, 'PAYERID': payerId, 'PAYMENTACTION': 'Sale' }; response = common.curl(common.URLBASE, postDetails) #HACK: On sandbox the first request will fail - we need to wait for 2 seconds and then try again if response == False: time.sleep(2) response = dict(cgi.parse_qsl(common.curl(common.URLBASE, postDetails))) else: response = dict(cgi.parse_qsl(response)) returnObj['transactionId'] = response["PAYMENTINFO_0_TRANSACTIONID"] returnObj['orderTime'] = response["PAYMENTINFO_0_ORDERTIME"] returnObj['paymentStatus'] = response["PAYMENTINFO_0_PAYMENTSTATUS"] returnObj['itemId'] = itemId returnObj['userId'] = userId identity.recordPayment(returnObj) return json.write(returnObj)
def ping(self, issue, command, created, creator): """ note: we might send multiple pings if a command was of more than one class """ for row in self.graph.queryd( "SELECT DISTINCT ?cls WHERE { ?cmd a ?cls }", initBindings={'cmd': command}): payload = jsonlib.write({ 'issue': issue, 'commandClass': row['cls'], 'command': command, 'created': created, 'creator': creator, }) for receiver in [ "http://bang:9030/dispatch/newCommand", # ^ this one should be dispatching to the rest, but i # don't have a proper PSHB setup yet "http://bang:9055/newCommand", "http://bang:9072/newCommand", ]: try: restkit.request( method="POST", url=receiver, body=payload, headers={"Content-Type": "application/json"}) except Exception, e: log.error(e)
def _dm_process(self, session, message): #global dms, config global last_sys_da if session not in dms.keys(): dms[session] = get_class(config.get(self.MY_ID, 'dialogueManagerClassPath')); message_type= message['type'] dm = dms[session] ret = {'session': session} if message_type == 'new_dialogue': dm.new_dialogue() sys_da = dm.da_out() ret['message'] = {'type':'sys_turn', 'tts': str(sys_da), 'grammar': sys_da.grammar.get_fullname()} last_sys_da = sys_da elif message_type== 'asr_att_results': #update belief space asr_hyps = WatsonASRResult.GetASRHypotheses(message['asr'], last_sys_da.grammar) dm.da_in(asr_hyps) sys_da = dm.da_out() last_sys_da = sys_da ret['message'] = {'type':'sys_turn', 'tts': str(sys_da), 'grammar': str(sys_da.grammar), 'asr_hps': str(asr_hyps)} elif message_type=='end_reward': pass #ret['message'] = {'type':'hello', 'tts':'Hello, how may I help you?', 'grammar':''} #ret['message'] = {'type':'sys_turn', 'tts': str(sys_da), 'grammar': 'decision'} #ret['message'] = {'type':'sys_turn', 'tts': str(sys_da), 'grammar': 'db-1k.all'} #ret['message'] = {'type':'sys_turn', 'tts': 'Hi!', 'grammar': 'decision'} #ret['message'] = {'type':'sys_turn', 'tts': str(asr_hyps), 'grammar': 'decision'} ret = jsonlib.write(ret) return ret
def finishSong(self): if not self.currentSong: return if 'file' in self.currentSong: index.post("mpd/song/", payload=jsonlib.write(self.currentSong)) self.currentSong = {}
def complete_workloads(self): import urllib #t = [{"id": task.id, "content": task.content, "source": task.source, "workload_key": task.workload_key, "error": Error}] with self.__sem: result = self.__client.get("/complete_workload?q=" + urllib.quote(jsonlib.write(self.__tasks_status))) self.__tasks_status = [] return True
def test_encode_utf16(self): value = write([u'\U0001D11E \u24CA'], ascii_only=False, encoding='utf-16-le') self.assertEqual(type(value), str) self.assertEqual( value, '\x5b\x00\x22\x00\x34\xd8\x1e\xdd' '\x20\x00\xca\x24\x22\x00\x5d\x00')
def test_fixed_truncation_error(self): data = [{"a": 3}] * 270 fp = io.StringIO() jsonlib.dump(data, fp, encoding=None) self.assertEqual(len(read(fp.getvalue())), len(data)) self.assertEqual(len(read(write(data))), len(data))
def verifyPurchase(self, userId, itemId, transactions): returnObj = None #json correctness hack - remove surrounding quotes on each dict in the array transactions = urllib2.unquote(transactions) p = re.compile('"{') transactions = p.sub( '{', transactions) p = re.compile('}"') transactions = p.sub( '}', transactions) if transactions is not None: arrTransactions = json.read(transactions) transactionId = None transactions = json.read(transactions) if transactions is not None: for item in transactions: if item['itemId'] == itemId: transactionId = item['transactionId'] if transactionId is not None: postDetails = { 'USER': common.UID, 'PWD': common.PASSWORD, 'SIGNATURE': common.SIG, 'METHOD': 'GetTransactionDetails', 'VERSION': common.VER, 'TRANSACTIONID': transactionId } response = dict(cgi.parse_qsl(common.curl(common.URLBASE, postDetails))) custom = string.split(response["CUSTOM"], ',') if (identity.getUserId() == custom[0]) and (itemId == custom[1]): returnObj = { 'success': 'true', 'error': '', 'transactionId': response["TRANSACTIONID"], 'orderTime': response["ORDERTIME"], 'paymentStatus': response["PAYMENTSTATUS"], 'itemId': itemId, 'userId': userId } else: returnObj = {'success': 'false', 'error': 'Item not found in transaction history'} print json.write(returnObj)
def generateJson(self): pubMedYears = {} allPublications = {} statements = self.getRDFStatements() allPubmedIds, pubMedYears = self.getIdentifiersForPubMedID(statements, pubMedYears) pubMedYears = self.queryPubmedYear(allPubmedIds, allPublications, pubMedYears) # C'est tout. return jsonlib.write(pubMedYears)
def generateJson(self): context = aq_inner(self.context) erneWS = context.queryDataSource specimenCount = {} if erneWS: for siteID, erneID in SITES.items(): specimenCount = self.getSpecimens(erneID, erneWS) # C'est tout. return jsonlib.write(specimenCount)
def GET(self): q = web.input()["q"] elasticQuery = { "filtered" : { "query" : # highlight seems to not work with fuzzy {"fuzzy" : {"_all" : {"value" : q, "prefix_length" : 2}}}, "filter" : { "or" : [ {"prefix" : {"value" : q, "boost" : 2.0}}, ] } } } response = elastic.request(method="get", path="mpd/song/_search", payload=jsonlib.write(elasticQuery)) result = jsonlib.read(response.body_string()) web.header("Content-Type", "application/json") return jsonlib.write(result)
def _query(self, query_type, payload, object_type, search, index): """Sends a post or get query to ElasticSearcg. Object type should be made a list in the future. """ url = '%s/%s/%s' % (self.elasticsearch, index, object_type) if search: url = '%s/_search' % url res = self._parse_response(query_type(url, jsonlib.write(payload))) logging.debug(res) if res is not None: return res
def generateJson(self): organDatasetCount = {} statements = self.getRDFStatements() for uri, predicates in statements.items(): organ = predicates[_organPredicateURI][0].rsplit('/', 1)[-1] if organ in organDatasetCount: organDatasetCount[organ] += 1 else: organDatasetCount[organ] = 1 # C'est tout. return jsonlib.write(organDatasetCount)
def generateJson(self): #graph = rdflib.Graph() context = aq_inner(self.context) rdfDataSource, bmoDataSource = context.biomarkerURL, context.organURL if not rdfDataSource or not bmoDataSource: raise RDFIngestException(_(u'This generator folder lacks one or both of its RDF source URLs.')) normalizerFunction = queryUtility(IIDNormalizer).normalize graph = ConjunctiveGraph() graph.parse(URLInputSource(rdfDataSource)) statements = self._parseRDF(graph) biomarkerTypeFreq = {} biomarkerOrganFreq = {} allBiomarkers = {} for uri, predicates in statements.items(): try: typeURI = predicates[_typeURI][0] if typeURI != _biomarkerPredicateURI: continue isPanel = bool(int(predicates[_isPanelPredicateURI][0])) title = unicode(predicates[_bmTitlePredicateURI][0]) hgnc = predicates[_hgncPredicateURI][0] if _hgncPredicateURI in predicates else None if hgnc is not None: hgnc = hgnc.strip() objID = hgnc if hgnc else normalizerFunction(title) if not isPanel: #Add frequencies for biomarker associated with biomarker type (Gene, Protein, etc...) for bmtype in predicates[_typePredicateURI]: if objID in biomarkerTypeFreq: biomarkerTypeFreq[objID] += [bmtype] else: biomarkerTypeFreq[objID] = [bmtype] allBiomarkers[URIRef(uri)] = objID except KeyError: pass # Add organ-specific information graph = ConjunctiveGraph() graph.parse(URLInputSource(bmoDataSource)) organStatements = self._parseRDF(graph) self.addOrganSpecificInformation(allBiomarkers, organStatements, biomarkerOrganFreq) jsondata = self.generateOrganTypeStats(biomarkerTypeFreq,biomarkerOrganFreq) # C'est tout. return jsonlib.write(jsondata)
def __call__(self): data = {} body = self.request.get('BODY') if body: body_unicode = self.request.get('BODY').decode('utf-8') data = json.loads(body_unicode) if data: contentid = idnormalizer.normalize(data.get('maschine').get('title')) mycontext = self.context[data.get('keyword')] checkdaten = {} checkdaten[data.get('id')] = data.get('optionen') notizdaten = {} notizdaten[data.get('id')] = data.get('notiz', u'') if not mycontext.has_key(contentid): obj = api.content.create( type='Ergebnisdaten', id = contentid, title = data.get('maschine').get('title'), maschnr = data.get('maschine').get('maschnr'), hersteller = data.get('maschine').get('hersteller'), fragebogen = data.get('fragebogen'), history = [data.get('id')], notizen = notizdaten, fortschritt = float(data.get('fortschritt', 0.0)), daten = checkdaten, container=mycontext) else: obj = mycontext[contentid] checkdaten = obj.daten checkdaten[data.get('id')] = data.get('optionen') obj.daten = checkdaten history = obj.history if not history: history = [] history.append(data.get('id')) obj.history = history notizdaten = obj.notizen if not notizdaten: notizdaten = {} notizdaten[data.get('id')] = data.get('notiz', u'') obj.notizen = notizdaten obj.fortschritt = data.get('fortschritt', 0.0) retdict = data payload = jsonlib.write(retdict) return payload
def _distance_process(self, message): doc1 = message['content'] doc1 = preprocess(doc1) doc2 = message['content2'] doc2 = preprocess(doc2) docvec1 = doc2vec.infer_vector(doc1) docvec2 = doc2vec.infer_vector(doc2) euclidean = Similarity.euclidean_distance(docvec1, docvec2) manhattan = Similarity.manhattan_distance(docvec1, docvec2) cosine = Similarity.cosine_similarity(docvec1, docvec2) ret = { 'cosine': cosine, 'euclidean': euclidean, 'manhattan': manhattan } return jsonlib.write(ret).decode('utf8')
def __call__(self): data = self.request.get('data') data = ast.literal_eval(data) result = True solution = [] for i in self.context.antworten: if i.get('bewertung') == 'richtig': solution.append(True) if not self.context.antworten.index(i) in data: result = False else: solution.append(False) if self.context.antworten.index(i) in data: result = False self.request.response.setHeader("Content-type", "application/json") self.request.response.setHeader("Access-Control-Allow-Origin", "*") retdict = {'result': result, 'solution': solution} payload = jsonlib.write(retdict) return payload
def __call__(self): data = {} body = self.request.get('BODY') if body: body_unicode = self.request.get('BODY').decode('utf-8') data = json.loads(body_unicode) datainput = "%s%s" %(data.get('email'), data.get('pin')) m = hashlib.md5() m.update(datainput) keyword = m.hexdigest() data['created'] = False if not self.context.has_key(keyword): data['created'] = True obj = api.content.create( type='Benutzerordner', id = keyword, title=keyword, container=self.context) data['keyword'] = keyword payload = jsonlib.write(data) return payload
def fetchlocationstoaddcontent(self): portal_state = getMultiAdapter((self.context, self.request), name=u"plone_portal_state") portal = portal_state.portal() results = getLocationListForAddContent(portal) output = {} items = [] for eachobj in results: temp = {} temp['title'] = force_unicode(eachobj['object'].Title,'utf8') temp['UID'] = eachobj['object'].UID temp['occ'] = '' if eachobj['canAdd'] == False or 'Discussion' in eachobj['disallowedtypes']: temp['occ'] = 'disabledspaceselection' temp['depth'] = eachobj['depth'] items.append(temp) output['items'] = items output = jsonlib.write(output) return output
def __call__(self): self.db = DBConnect(host=self.context.host, db=self.context.database, user=self.context.username, password=self.context.password) mixtures = [] select = "SELECT substance_mixture_id, title FROM substance_mixture;" gemische = self.db.execute(select) for gemisch in gemische: mixture_entry = {} selectoldid = "SELECT link FROM oldlinks WHERE mixture_id = %s" % gemisch[ 0] oldid = self.db.execute(selectoldid) if oldid: mixture_entry['@id'] = oldid[0][0] else: mixture_entry['@id'] = "bgetem.substance_mixture." + str( gemisch[0] ) # über die Punkt-Notation könnten mehrere potenzielle Quellen angezapft werden mixture_entry['title'] = gemisch[1] mixtures.append(mixture_entry) return jsonlib.write(mixtures)
def commitPayment(self, userId, payerId, token, amt, itemId): returnObj = {} if identity.verifyUser(userId): postDetails = { 'USER': common.UID, 'PWD': common.PASSWORD, 'SIGNATURE': common.SIG, 'METHOD': 'DoExpressCheckoutPayment', 'VERSION': common.VER, 'AMT': amt, 'TOKEN': token, 'PAYERID': payerId, 'PAYMENTACTION': 'Sale' } response = common.curl(common.URLBASE, postDetails) #HACK: On sandbox the first request will fail - we need to wait for 2 seconds and then try again if response == False: time.sleep(2) response = dict( cgi.parse_qsl(common.curl(common.URLBASE, postDetails))) else: response = dict(cgi.parse_qsl(response)) returnObj['transactionId'] = response[ "PAYMENTINFO_0_TRANSACTIONID"] returnObj['orderTime'] = response["PAYMENTINFO_0_ORDERTIME"] returnObj['paymentStatus'] = response[ "PAYMENTINFO_0_PAYMENTSTATUS"] returnObj['itemId'] = itemId returnObj['userId'] = userId identity.recordPayment(returnObj) return json.write(returnObj)
def _dm_process(self, message): content = message['content'] tokens = preprocess(content) docvec = doc2vec.infer_vector(tokens) docvec_scaled = docvec.reshape(1, -1) docvec_scaled = scaler.transform(docvec_scaled) #cats = classifier.predict(docvec_scaled)[0] cats = classifier.predict_proba(docvec_scaled)[0] cats_rank = np.argsort(cats)[::-1][:5] ret_cats = [(id2cat[i], cats[i]) for i in cats_rank] related = doc2vec.docvecs.most_similar(positive=[docvec], topn=10) rels = [] for rel in related: rels.append((doc_infos[rel[0]][4], rel[1])) ret = { 'category': ret_cats, 'related': rels, 'docvec': docvec.tolist() } return jsonlib.write(ret).decode('utf8')
def __call__(self): data = {} if not ploneapi.user.is_anonymous(): authuser = ploneapi.user.get_current() data['user'] = {'name':authuser.getProperty('fullname'), 'pin':authuser.getProperty('email'), 'role':'teacher'} else: session = ISession(self.request) chatid = 'chat_%s' % self.context.UID() userdata = session.get(chatid) userdata['role'] = 'trainee' data['user'] = userdata database = True if not self.get_databases(): database = self.create_database() data['database'] = database data['teacher_folder_uid'] = self.context.aq_parent.UID() data['classroom_uid'] = self.context.UID() myauth = self.get_credentials() data['dbuser'] = myauth[0] data['dbpassword'] = myauth[1] classlist = self.context.get_classlist() data['classlist'] = classlist payload = jsonlib.write(data) return payload
def getjsondata(context,reply_dict,portal_url,item_url,extra_data={}): site_encoding = context.plone_utils.getSiteEncoding() mi = getToolByName(context, 'portal_membership') util = getToolByName(context,'translation_service') output = {} items = [] for eachobj in reply_dict: temp = {} temp['depth'] = 0 if eachobj.has_key('prev_id'): temp['prev_id'] = eachobj['prev_id'] else: temp['prev_id'] = '' reply = eachobj['object'] if reply <> None: temp['id'] = reply.id temp['replyurl'] = reply.absolute_url() temp['replytoid'] = '-1' if reply.inReplyTo() and reply.inReplyTo().portal_type == 'Discussion Item': temp['replytoid'] = reply.inReplyTo().getId() temp['depth'] = eachobj['depth'] temp['mdate'] = util.ulocalized_time(reply.ModificationDate(), 1, context, domain='plonelocales') creator = reply.Creator() temp['userid'] = creator temp['userinfourl'] = portal_url + '/userinfo?userid=' + creator temp['useravatarurl'] = mi.getPersonalPortrait(creator).absolute_url() temp['replycooked'] = reply.cooked_text.decode(site_encoding) temp['permalink'] = item_url + '#' + reply.id items.append(temp) output['items'] = items for key,val in extra_data.items(): output[key] = val return jsonlib.write(output)
def init(self): returnObj = {'success': true, 'userId': getUserId(), 'state': 'init'} return json.write(returnObj)
def test_encode_unicode_none(self): value = write([u'\U0001D11E \u24CA'], ascii_only=False, encoding=None) self.assertEqual(type(value), unicode) self.assertEqual(value, u'["\U0001D11E \u24CA"]')
def __str__(self): return jsonlib.write(self.__dict__)
def test_encode_utf8_default(self): value = write([u'\U0001D11E \u24CA'], ascii_only=False) self.assertEqual(type(value), str) self.assertEqual(value, '["\xf0\x9d\x84\x9e \xe2\x93\x8a"]')
def test_make_suggestion_as_non_bureau(self, action = 'None', already_logged_in_as=None, do_suggest = True, do_delete = True): if already_logged_in_as: u = already_logged_in_as else: # Create a throwaway user u, p, e, n = [herder.model.user.random_alphanum() for k in range(4)] herder.tests.functional.test_account.do_register(self.app, user_name=u, password=p, email=e + '@example.com', human_name=n) # Pretend to be that user self.login_as(u, p) # No matter what, we have to be logged in. assert 'appears to be ' in self.app.get(url_for(controller='account', action='profile')) # First, change it so old -> new i18n_key = 'country.us' old = 'United States' new = 'Untied States' # Just check that things are the way we thought self.test_strings_contain(desired_key=i18n_key, desired_value=old) if do_suggest: # Now try to do the edit url_indeed = url_for(controller='language', action='edit_string', domain='cc_org', id='en_US') response = self.app.post(url_indeed, params={'data': jsonlib.write( {'id': i18n_key, 'new_value': new, 'old_value': old})}) # And, uh, FIXME - right now we have no way of checking if it stuck # At least, the real string shouldn't have changed - repeat this check self.test_strings_contain(desired_key=i18n_key, desired_value=old) # And check that the suggestion exists in the lame UI url_lame = url_for(controller='language', action='lame_suggestions_ui', domain='cc_org', id='en_US') response = self.app.get(url_lame) assert new in response url_json = url_for(controller='language', action='suggestions_for_message', domain='cc_org', id='en_US', message_id=i18n_key) response = self.app.get(url_json) assert new in response assert old not in response assert u in response.body if do_delete: # Now delete it, but maybe take some other action first self.login_as(bureau_username, bureau_password) response = self.app.get(url_lame) assert u in response # make sure my assert new in response # suggestion is there now # Here goes the deletion response = self.app.get(url_lame) delete_form = response.forms[0] response = delete_form.submit() response = response.follow() assert u not in response assert new not in response
import restkit, jsonlib index = restkit.Resource("http://plus:9200/") try: index.delete('/mpd/') except restkit.errors.RequestFailed: pass index.put("mpd/") index.put("mpd/_mapping", payload=jsonlib.write({ "song" : { # this was meant to make highlighting work, but i can't use HL # with fuzzy matching, it seems "_all" : {"type" : "string", "store" : "yes", "term_vector" : "with_positions_offsets"}, "properties" : { "title" : {"type" : "string", "store" : "yes", "term_vector" : "with_positions_offsets"}, } } })) class TagCacheParser(object): def __init__(self, index, tagCachePath="/var/lib/mpd/tag_cache"): self.currentSong = {} for line in open(tagCachePath): line = line.strip() if ': ' in line: key, value = line.split(': ', 1) if key == 'begin': self.directory = value
index.delete('/mpd/') except restkit.errors.RequestFailed: pass index.put("mpd/") index.put( "mpd/_mapping", payload=jsonlib.write({ "song": { # this was meant to make highlighting work, but i can't use HL # with fuzzy matching, it seems "_all": { "type": "string", "store": "yes", "term_vector": "with_positions_offsets" }, "properties": { "title": { "type": "string", "store": "yes", "term_vector": "with_positions_offsets" }, } } })) class TagCacheParser(object): def __init__(self, index, tagCachePath="/var/lib/mpd/tag_cache"): self.currentSong = {} for line in open(tagCachePath): line = line.strip()
def do_POST_inner(self): ## global db, dm, prevSysAction, dmModuleName, uuid # strip of leading '/' path = self.path[1:] print '!!!!!!!!Get path=', path # read in body content inputText = self.rfile.readline() self.log_message("Got message: %s" % (inputText)) # vivify into json try: inputJSON = jsonlib.read(inputText, use_float=True) except: self.log_error("Could not parse JSON: %s\nError: %s" % (inputText, sys.exc_info())) return '{"status": "error","error": "input-JSON-decoding","error_msg": "Could not parse input JSON"}\n' if (path == 'dm'): #clien yeu cau cai gi? dua vao path cua url print '!!!Request DM' pdb.set_trace() ## # dm request if ( inputJSON['session'] == 'new' ): #phien moi thi tao dm moi, gui ve askaction va luu he thong cu ## sysAction = dm.Init() ## prevSysAction = sysAction ## asrProbs = None pass elif ( prevSysAction == None ): #yeu cau lai ma he thong chua tung khoi toa, loi vi dm chua cod self.log_error("DM not ready; resetting connection") return '{"status": "error","error": "dm", "error_msg": "DM not ready"}\n' else: #dua nhan dang dong noi vao de DM tinh toan pass ## asrResult = ASRResult.FromWatson(inputJSON['message'],prevSysAction.grammar)#grammar is firt/last... or all ## asrProbs = asrResult.GetProbs() ## sysAction = dm.TakeTurn(asrResult) ## prevSysAction = sysAction ## messageJSON = { ## 'sysAction': sysAction.GetJSON(), ## 'asrProbs': asrProbs, ## 'dialogResult': dm.GetDisplayJSON(), ## } messageJSON = { 'sysAction': None, 'asrProbs': None, 'dialogResult': None, } elif (path == 'sampleName'): # sample a name nameString = 'Accept' messageJSON = { 'nameText': nameString, } elif (path == 'info'): # config info messageJSON = { 'dmModuleName': 'THANH', 'uuid': uuid, } else: self.log_error("Dont recognize path %s" % (path)) return '{"status": "error","error": "path", "error_msg": "Dont recognize path %s"}\n' % ( path) # add session & status outputJSON = {} outputJSON['status'] = 'ok' outputJSON['message'] = messageJSON # textify result try: output_text = jsonlib.write(outputJSON) except: self.log_error("Could not parse JSON into text: %s\nError: %s" % (outputJSON, sys.exc_info())) return '{"status": "error","error": "output-JSON-encoding","msg": "Could not parse JSON: %s\nError: %s."}\n' % ( outputJSON, sys.exc_info()[0]) return output_text
if 'data' in params: data = params['data'] data = string.split(data, '|') returnObj = transact.commitPayment(data[1], params['PayerID'], params['token'], data[0], data[2]) print '''\ Content-type: text/html; charset=UTF-8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Thank you</title> <script> function closeFlow() { ''' print 'parent.pptransact.releaseDG(%s);' % json.write(returnObj) print '''\ } </script> </head> <body onload="closeFlow()"> <div style="background-color:#FFF;height:700px;width:300px; border-radius:8px;padding:20px;"> Thank you for the purchase!<br /> <button id="close" onclick="closeFlow();">close</button> </div> </body> </html> '''
results.append(r.get_nowait()) r.task_done() print "dumping results to shit.db" conn = sqlite3.connect("shit.db") c = conn.cursor() try: c.execute("create table whois(name varchar(255), response text);") c.execute("create index whois_dns on whois(name);") except: print "Oh good, it appears to already be created..." pass for o in results: (name, whois) = o w = jsonlib.write(dict([ (x, getattr(whois,x)) for x in whois.attrs() ])) c.execute("insert into whois values(?,?)", (name, w)) conn.commit() conn.close() print "coalescing faults..." results = [] while faults.empty() == False: results.append(faults.get_nowait()) faults.task_done() output = open("faultlist.txt","w+") print "emitting faultlist.txt" for o in results: output.write( o + "\n" ) output.close()
def json_escape(field, arg): return jsonlib.write(field)
def w(self, value, expected, **kwargs): serialized = write(value, encoding=None, **kwargs) self.assertEqual(serialized, expected) self.assertEqual(type(serialized), type(expected))
def generateJson(self): '''Generate an empty graph.''' return jsonlib.write({})
def __call__(self): self.db = DBConnect(host=self.context.host, db=self.context.database, user=self.context.username, password=self.context.password) gemischid = self.request.get('gemischid') #gemischid = "https://emissionsarme-produkte.bgetem.de/datenbank-chemie-dp/wasch-und-reinigungsmittel-fuer-den-etikettendruck/biolon-xi-fluessig" if gemischid.startswith('https://'): select = "SELECT mixture_id FROM oldlinks WHERE link = '%s'" % gemischid mixture_id = self.db.execute(select) else: mixture_id = gemischid.split('.')[-1] mixture_id = mixture_id[0][0] data1select = "SELECT * FROM substance_mixture WHERE substance_mixture_id = %s" % mixture_id data1 = self.db.execute(data1select) data2select = "SELECT * FROM manufacturer WHERE manufacturer_id = %s" % data1[0][25] data2 = self.db.execute(data2select) data3select = "SELECT * FROM recipes WHERE mixture_id = %s" % mixture_id data3 = self.db.execute(data3select) gefahrstoffdata = {} hersteller = {} hersteller['title'] = data2[0][1] hersteller['@id'] = "bgetem.manufacturer."+str(data2[0][0]) hersteller['description'] = data2[0][2] hersteller['homepage'] = data2[0][4] inhaltsstoffe = list() for inhalt in data3: inhaltsstoff = {} select = "SELECT * FROM substance WHERE substance_id = %s" % inhalt[1] reinstoff = self.db.execute(select) inhaltsstoff['cas'] = reinstoff[0][4] inhaltsstoff['gefahrstoff'] = reinstoff[0][1] inhaltsstoff['anteil_min'] = inhalt[3] inhaltsstoff['anteil_max'] = inhalt[4] inhaltsstoff['anteil'] = f">= {inhalt[3]}% - <= {inhalt[4]}%" inhaltsstoffe.append(inhaltsstoff) productclassselect = "SELECT class_name FROM productclasses WHERE class_id = %s" % data1[0][27] try: productclass = self.db.execute(productclassselect) productclass = productclass[0][0] except: productclass = None produktkategorien = { "label": "Reinigungsmittel im Etikettendruck", "offset": "Offsetdruck allgemein", "heatset": "Heatsetwaschmittel", "uv": "Reinigungsmittel im UV-Druck", "special": "Sonderreiniger", "dental": "Dentaltechnik", "textil": "Textil und Mode" } produktkategorie = list() produktkategorie.append(produktkategorien[data1[0][5]]) gefahrstoffdata['hersteller'] = hersteller gefahrstoffdata['hskategorie'] = data1[0][16] gefahrstoffdata['bemerkungen'] = data1[0][23] gefahrstoffdata['chemikalienliste'] = inhaltsstoffe gefahrstoffdata['UID'] = data1[0][3] gefahrstoffdata['title'] = data1[0][1] gefahrstoffdata['review_state'] = data1[0][26] gefahrstoffdata['emissionsgeprueft'] = data1[0][17] gefahrstoffdata['produktkategorie'] = produktkategorie gefahrstoffdata['description'] = data1[0][2] gefahrstoffdata['wertebereich'] = data1[0][20] gefahrstoffdata['flammpunkt'] = data1[0][19] gefahrstoffdata['@id'] = gemischid gefahrstoffdata['produktklasse'] = productclass return jsonlib.write(gefahrstoffdata)
def test_edit_string_as_bureau(self, skip_login_step = False, lang_id = None, new_value=None, should_fail = False, error_string=''): if lang_id is None: lang_id = 'en_US' if not skip_login_step: # Pretend to be bureau self.login_as(bureau_username, bureau_password) # No matter what, we must be logged in for this to have a # hope of working assert 'appears to be ' in self.app.get(url_for(controller='account', action='profile')) # First, change it so old -> new i18n_key = 'country.us' old = 'United States' if new_value is None: new = u'¿Untied States?' else: new = unicode(new_value) self.test_strings_contain(desired_key=i18n_key, desired_value=old) url_indeed = url_for(controller='language', action='edit_string', domain='cc_org', id='en_US') response = self.app.post(url_indeed, params={'data': jsonlib.write({'id': i18n_key, 'new_value': new, 'old_value': old})}) # If it should have failed, check that it did and how if should_fail: parsed = jsonlib.read(response.body) assert parsed['result'] == 'error' if error_string: assert error_string in parsed['message'] # Check that the write took with a deep test import herder.model.language lang = herder.model.language.Language.by_domain_id(domain_id='cc_org', lang='en_US') if should_fail: try: assert lang[i18n_key].string == old except AssertionError: # We should undo the damage, I suppose, then re-raise url_indeed = url_for(controller='language', action='edit_string', domain='cc_org', id='en_US') response = self.app.post(url_indeed, params={'data': jsonlib.write({'id': i18n_key, 'new_value': old, 'old_value': new})}) raise # FIXME: Would be nice to scrape out the given error message return # This function can end now; no need to change it back else: assert lang[i18n_key].string == new # Check that the write took with a UI-level test # FIXME: Don't disable this check. # self.test_strings_contain(desired_key=i18n_key, desired_value=new) # Good, the new value stuck. # Then, change it back (just because I feel bad) response = self.app.post(url_indeed, params={'data': jsonlib.write({'id': i18n_key, 'new_value': old, 'old_value': new})}) # Check deep lang = herder.model.language.Language.by_domain_id(domain_id='cc_org', lang='en_US') assert lang[i18n_key].string == old # FIXME: # Re-enable this check: # self.test_strings_contain(desired_key=i18n_key, desired_value=old) # Good, the old value is back. # Stop pretending to be bureau. logout = url_for(controller='account', action='logout') response = self.app.get(logout)
print ''' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Thank you</title> <script src="https://www.paypalobjects.com/js/external/dg.js"></script> <script src="../../client/jquery-1.6.2.min.js" type="text/javascript"></script> <script src="../../client/pptransact.js"></script> <script> function closeFlow(){ results = %s; jsonResults = jQuery.parseJSON(results.replace(/\\"/g,'\"')); pptransact.setUserId(jsonResults['userId']); pptransact.releaseDG(results); } </script> </head> <body onload="closeFlow()"> <div style="background-color:#FFF;height:700px;width:300px; border-radius:8px;padding:20px;"> Thank you for the purchase!<br /> <button id="close" onclick="closeFlow();">close</button> </div> </body> </html> ''' % json.write(result)