本文整理汇总了Python中Formatter.data方法的典型用法代码示例。如果您正苦于以下问题:Python Formatter.data方法的具体用法?Python Formatter.data怎么用?Python Formatter.data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Formatter
的用法示例。
在下文中一共展示了Formatter.data方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getHealthNews
# 需要导入模块: import Formatter [as 别名]
# 或者: from Formatter import data [as 别名]
def getHealthNews(lan='en',format='json'):
returnData = MutableString()
returnData = ''
if (lan == 'es'):
dom = minidom.parse(urllib.urlopen(AppConfig.medlinePlusHealthNewsSpanishURL))
else:
dom = minidom.parse(urllib.urlopen(AppConfig.medlinePlusHealthNewsEnglishURL))
rssTitle = MutableString()
rssDescription = MutableString()
rssURL = MutableString()
for node in dom.getElementsByTagName('item'):
for item_node in node.childNodes:
rssTitle = ''
rssDescription = ''
rssURL = ''
#item title
if (item_node.nodeName == "title"):
for text_node in item_node.childNodes:
if (text_node.nodeType == node.TEXT_NODE):
rssTitle += text_node.nodeValue
#description
if (item_node.nodeName == "description"):
for text_node in item_node.childNodes:
rssDescription += text_node.nodeValue
#link to URL
if (item_node.nodeName == "link"):
for text_node in item_node.childNodes:
rssURL += text_node.nodeValue
if (format == 'json'):
startTag = '{'
endTag = '},'
#cleanup
#rssTitle = re.sub("\"", "'", rssTitle)
rssTitle = re.sub("\n", "", rssTitle)
rssTitle = re.sub("\"", "\\\"", rssTitle)
rssDescription = re.sub("\"", "\\\"", rssDescription)
rssDescription = re.sub("\n", "", rssDescription)
rssDescription = re.sub("\t", " ", rssDescription)
rssDescription = re.sub("\r", "", rssDescription)
if (len(rssDescription) > 0):
rssDescription = Formatter.data(format, 'description', escape(rssDescription))[:-1]
else:
startTag = '<record>'
endTag = '</record>'
if (len(rssDescription) > 0):
rssDescription = Formatter.data(format, 'description', escape(rssDescription))
if (len(rssTitle) > 0):
returnData += startTag + Formatter.data(format, 'title', rssTitle)
if (len(rssURL) > 0):
returnData += Formatter.data(format, 'url', rssURL)
if (len(rssDescription) > 0 ):
returnData += rssDescription + endTag
return returnData
示例2: getDiagnosisICD9Data
# 需要导入模块: import Formatter [as 别名]
# 或者: from Formatter import data [as 别名]
def getDiagnosisICD9Data(self,xml,format):
if (xml):
documentElms = xml.getElementsByTagName('entry')
if (documentElms):
#grab two highest ranked content
content1 = ''
content2 = ''
contentNode1 = documentElms[0]
content1 = APIUtils.parseHealthDiagnosisICD9Content(format,contentNode1)
if (len(documentElms) > 0):
contentNode2 = documentElms[1]
content2 = APIUtils.parseHealthDiagnosisICD9Content(format,contentNode2)
returnData = "%s%s" % (content1, content2)
if (format == 'json'):
return returnData[:-1]
else:
return returnData
else:
logging.error('unable to retrieve diagnosis content')
return Formatter.data(format, 'error', 'No results')
else:
logging.error('unable to retrieve diagnosis content')
return Formatter.data(format, 'error', 'Unable to retrieve content from provider')
示例3: get
# 需要导入模块: import Formatter [as 别名]
# 或者: from Formatter import data [as 别名]
def get(self,zip=None,format='json'):
#set content-type
self.response.headers['Content-Type'] = Formatter.contentType(format)
#validate zip
if (not zip or zip == ''):
self.response.out.write(Formatter.error(format, 'invalid zipcode'))
return
#get lat/lon from zipcode
urlStr = 'http://maps.google.com/maps/geo?q=%s&output=json&sensor=true&key=%s' % (zip,AppConfig.googleMapsAPIKey)
jsonData = UtilLib.reverseGeo(urlStr)
#lets see if have jsonData from reverse geocoding call
if (jsonData):
lon = jsonData['Placemark'][0]['Point']['coordinates'][0]
lat = jsonData['Placemark'][0]['Point']['coordinates'][1]
logging.debug("GPS Coordinates: %s,%s" % (lat,lon))
gb = geobox2.Geobox(lat, lon)
#scope 100 miles
box = gb.search_geobox(100)
query = HospitalInfo.all().filter("geoboxes IN", [box])
#get 100 records
results = query.fetch(100)
db_recs = {}
for result in results:
distance = UtilLib.getEarthDistance(lat, lon, result.location.lat, result.location.lon)
if (distance and distance > 0):
db_recs[distance] = result
returnData = MutableString()
returnData = ''
#output this badboy
if (db_recs and len(db_recs) > 0):
for key in sorted(db_recs.iterkeys()):
p = db_recs[key]
if (format == 'json'):
startTag = '{'
endTag = '},'
distance = Formatter.data(format, 'distance', '%s %s' % (str(math.ceil(key)), "mi"))[:-1]#'%.2g %s' % (key, "mi"))[:-1]
else:
startTag = '<record>'
endTag = '</record>'
distance = Formatter.data(format, 'distance', '%s %s' % (str(math.ceil(key)), "mi"))
#build the string
returnData = "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" % (returnData,startTag,
Formatter.data(format, 'hospital_id', p.hospital_id),
Formatter.data(format, 'name', p.name.replace('&', '&')),
Formatter.data(format, 'address', p.address.replace('&', '&')),
Formatter.data(format, 'city', p.city),
Formatter.data(format, 'state', p.state),
Formatter.data(format, 'zip_code', p.zip_code),
Formatter.data(format, 'county', p.county.replace('&', '&')),
Formatter.data(format, 'phone', p.phone),
Formatter.data(format, 'hospital_type', p.hospital_type.replace('&', '&')),
Formatter.data(format, 'hospital_owner', p.hospital_owner.replace('&', '&')),
Formatter.data(format, 'emergency_service', p.emergency_service),
Formatter.data(format, 'geo_location', p.location),
distance,
endTag
)
#output to the browser
self.response.out.write(Formatter.dataWrapper(format, returnData))
else:
self.response.out.write(Formatter.error(format, 'Unable to perform reverse geoencoding'))
return
示例4: getHospitalData
# 需要导入模块: import Formatter [as 别名]
# 或者: from Formatter import data [as 别名]
def getHospitalData(self,lat,lon,format):
#get hospital data
gb = geobox2.Geobox(lat, lon)
box = gb.search_geobox(100)
query = HospitalInfo.all().filter("geoboxes IN", [box])
results = query.fetch(100)
db_recs = {}
for result in results:
distance = UtilLib.getEarthDistance(lat, lon, result.location.lat, result.location.lon)
if (distance and distance > 0):
db_recs[distance] = result
returnData = MutableString()
returnData = ''
#output this badboy
if (db_recs and len(db_recs) > 0):
for key in sorted(db_recs.iterkeys()):
p = db_recs[key]
if (format == 'json'):
startTag = '{'
endTag = '},'
distance = Formatter.data(format, 'distance', '%s %s' % (str(math.ceil(key)), "mi"))[:-1]#'%.2g %s' % (key, "mi"))[:-1]
else:
startTag = '<record>'
endTag = '</record>'
distance = Formatter.data(format, 'distance', '%s %s' % (str(math.ceil(key)), "mi"))
#build the string
returnData = "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" % (returnData,startTag,
Formatter.data(format, 'hospital_id', p.hospital_id),
Formatter.data(format, 'name', p.name.replace('&', '&')),
Formatter.data(format, 'address', p.address.replace('&', '&')),
Formatter.data(format, 'city', p.city),
Formatter.data(format, 'state', p.state),
Formatter.data(format, 'zip_code', p.zip_code),
Formatter.data(format, 'county', p.county.replace('&', '&')),
Formatter.data(format, 'phone', p.phone),
Formatter.data(format, 'hospital_type', p.hospital_type.replace('&', '&')),
Formatter.data(format, 'hospital_owner', p.hospital_owner.replace('&', '&')),
Formatter.data(format, 'emergency_service', p.emergency_service),
Formatter.data(format, 'geo_location', p.location),
distance,
endTag
)
if (format == 'json'):
return returnData[:-1]
else:
return returnData
示例5: getTopicData
# 需要导入模块: import Formatter [as 别名]
# 或者: from Formatter import data [as 别名]
def getTopicData(self,xml,format,lan='en'):
if (xml):
documentElms = xml.getElementsByTagName('document')
if (documentElms):
#grab two highest ranked content
content1 = ''
content2 = ''
content3 = ''
contentNode1 = documentElms[0]
content1 = APIUtils.parseHealthTopicContent(format,contentNode1)
if (len(documentElms) > 1):
contentNode2 = documentElms[1]
content2 = APIUtils.parseHealthTopicContent(format,contentNode2)
if (len(documentElms) > 2):
contentNode3 = documentElms[2]
content3 = APIUtils.parseHealthTopicContent(format,contentNode3)
returnData = "%s%s%s" % (content1, content2, content3)
"""
Giving up for now....major pain in the butt to translate entire XML or JSON document
#fun stuff: lets see if we need to translate
#because we have three content records it's better to
#translate the entire document (json or xml) instead of individual content summaries
translatedData = ''
if (lan != 'en'):
#prep data
#if JSON, replace record definition with non-translatable text
if (format == 'json'):
returnData = re.sub("\"", "X59X", returnData)
else:
#get rid of the tags. Google Translate doesn't seem to like them
returnData = returnData#CharReplacementMap.translate_tags_from_xml_record(returnData)
translatedData = self.translateData(lan,returnData.encode("utf-8"))
self.out(translatedData)
if (translatedData):
#cheesy search&replace - need a quick solution to fix JSON
if (format == 'json'):
translatedData = re.sub("X59X","\"", translatedData)
translatedData = re.sub("\n", "", translatedData)
translatedData = re.sub("\r", "", translatedData)
translatedData = re.sub("\t", "", translatedData)
else:
translatedData = translatedData#CharReplacementMap.translate_tags_to_xml_record(translatedData)
return translatedData
"""
if (format == 'json'):
return returnData[:-1]
else:
return returnData
else:
logging.error('unable to retrieve health topic content')
return Formatter.data(format, 'error', 'No results')
else:
logging.error('unable to retrieve health topic content')
return Formatter.data(format, 'error', 'Unable to retrieve content from provider')
示例6:
# 需要导入模块: import Formatter [as 别名]
# 或者: from Formatter import data [as 别名]
#get hospital data
if (userLat and userLon):
hospitalData = self.getHospitalData(userLat, userLon,format)
if (hospitalData):
returnData += Formatter.dataComplex(format, 'hospital-records', hospitalData)
if (rpcClinicalTrials):
clinicalTrialsData = ''
try:
resultClinicalTrials = rpcClinicalTrials.get_result()
if (resultClinicalTrials and resultClinicalTrials.status_code == 200):
if (resultClinicalTrials.content):
clinicalTrialsData = self.getClinicalTrialsData(minidom.parseString(resultClinicalTrials.content), format)
else:
logging.error('unable to retrieve clinical trials content')
clinicalTrialsData = Formatter.data(format, 'error', 'Unable to retrieve content from provider')
except Exception,ex:
logging.error('Errors getting clinical trials content: %s' % ex)
clinicalTrialsData = Formatter.data(format, 'error', 'Error(s) retrieving content from provider: %s' % ex)
#append to the overall dataset
returnData += Formatter.dataComplex(format, 'clinicaltrials-records', clinicalTrialsData)
#fetch ICD-9 diagnosis data
if (rpcDiagnosis):
diagnosisData = ''
try:
resultDiagnosis = rpcDiagnosis.get_result()
if (resultDiagnosis and resultDiagnosis.status_code == 200):
if (resultDiagnosis.content):
diagnosisData = self.getDiagnosisICD9Data(minidom.parseString(resultDiagnosis.content), format)
示例7: parseHealthTopicContent
# 需要导入模块: import Formatter [as 别名]
# 或者: from Formatter import data [as 别名]
def parseHealthTopicContent(format,node):
if (node):
contentTitle = MutableString()
contentAltTitle = MutableString()
contentSummary = MutableString()
contentTitle = ''
contentAltTitle = ''
contentSummary = ''
for content_node in node.childNodes:
#get title
if (content_node and content_node.nodeName == 'content' and content_node.attributes['name'].value == "title"):
for text_node in content_node.childNodes:
contentTitle += CharReplacementMap.remove_html_tags(text_node.nodeValue)
#get alt title
"""if (content_node and content_node.nodeName == 'content' and content_node.attributes['name'].value == "altTitle"):
for text_node in content_node.childNodes:
contentAltTitle += remove_html_tags(text_node.nodeValue)
"""
#get content
if (content_node and content_node.nodeName == 'content' and content_node.attributes['name'].value == "FullSummary"):
for text_node in content_node.childNodes:
contentSummary += text_node.nodeValue
#replace HTML tags with friendly tags (that will be replaced back to HTML later)
#contentSummary = CharReplacementMap.translate_tags_from_html(contentSummary)
#now strip HTML garbage
#contentSummary = CharReplacementMap.remove_html_tags(contentSummary)
returnData = MutableString()
returnData = ''
if (format == 'json'):
startTag = '{'
endTag = '},'
#cleanup
contentTitle = re.sub("\n", "", contentTitle)
contentTitle = re.sub("\"", "\\\"", contentTitle)
contentAltTitle = re.sub("\n", "", contentAltTitle)
contentAltTitle = re.sub("\r", "", contentAltTitle)
contentAltTitle = re.sub("\"", "\\\"", contentAltTitle)
contentSummary = re.sub("\"", "\\\"", contentSummary)
contentSummary = re.sub("\n", "", contentSummary)
contentSummary = re.sub("\t", " ", contentSummary)
contentSummary = re.sub("\r", "", contentSummary)
if (len(contentSummary) > 0):
contentSummary = Formatter.data(format, 'summary', escape(contentSummary))[:-1]
else:
startTag = '<record>'
endTag = '</record>'
if (len(contentSummary) > 0):
contentSummary = Formatter.data(format, 'summary', escape(contentSummary))
if (len(contentTitle) > 0):
returnData += startTag + Formatter.data(format, 'title', escape(contentTitle))
if (len(contentAltTitle) > 0):
returnData += Formatter.data(format, 'alt_title', escape(contentAltTitle))
if (len(contentSummary) > 0 ):
returnData += contentSummary + endTag
return returnData
示例8: parseClinicalTrialsContent
# 需要导入模块: import Formatter [as 别名]
# 或者: from Formatter import data [as 别名]
def parseClinicalTrialsContent(format,node):
if (node):
contentTitle = MutableString()
contentLink = MutableString()
contentSummary = MutableString()
contentStatus = MutableString()
contentTitle = ''
contentLink = ''
contentSummary = ''
contentStatus = ''
for content_node in node.childNodes:
#get title
if (content_node and content_node.nodeName == 'title'):
for text_node in content_node.childNodes:
contentTitle += text_node.nodeValue
#get link
if (content_node and content_node.nodeName == 'url'):
for text_node in content_node.childNodes:
contentLink += text_node.nodeValue
#get status
if (content_node and content_node.nodeName == 'status'):
for text_node in content_node.childNodes:
contentStatus += text_node.nodeValue
#get content
if (content_node and content_node.nodeName == 'condition_summary'):
for text_node in content_node.childNodes:
contentSummary += text_node.nodeValue
returnData = MutableString()
returnData = ''
if (format == 'json'):
startTag = '{'
endTag = '},'
#cleanup
contentStatus = re.sub("\n", "", contentStatus)
contentStatus = re.sub("\"", "\\\"", contentStatus)
contentTitle = re.sub("\n", "", contentTitle)
contentTitle = re.sub("\"", "\\\"", contentTitle)
contentTitle = re.sub("\t", " ", contentTitle)
contentLink = re.sub("\n", "", contentLink)
contentLink = re.sub("\"", "\\\"", contentLink)
contentSummary = re.sub("\"", "\\\"", contentSummary)
contentSummary = re.sub("\n", "", contentSummary)
contentSummary = re.sub("\t", " ", contentSummary)
if (len(contentSummary) > 0):
contentSummary = Formatter.data(format, 'summary', escape(contentSummary))[:-1]
else:
startTag = '<record>'
endTag = '</record>'
if (len(contentSummary) > 0):
contentSummary = Formatter.data(format, 'summary', escape(contentSummary))
if (len(contentTitle) > 0):
returnData += startTag + Formatter.data(format, 'title', escape(contentTitle))
if (len(contentLink) > 0):
returnData += Formatter.data(format, 'link', escape(contentLink))
if (len(contentStatus) > 0):
returnData += Formatter.data(format, 'status', escape(contentStatus))
if (len(contentSummary) > 0 ):
returnData += contentSummary + endTag
return returnData
示例9: parsePageContent
# 需要导入模块: import Formatter [as 别名]
# 或者: from Formatter import data [as 别名]
#.........这里部分代码省略.........
if (key in comments_stuff):
tupComments = comments_stuff[key]
else:
tupComments = None
if (tupURL):
url = ''
title = ''
score = ''
user = ''
comments = ''
timeAgo = ''
itemId = ''
itemInfo = ''
#assign vars
url = tupURL[0]
title = tupURL[1]
if (tupComments):
score = tupComments[0]
user = tupComments[1]
comments = tupComments[2]
timeAgo = tupComments[3]
itemId = tupComments[4]
itemInfo = tupComments[5]
else:
#need this for formatting
itemInfo = 'n/a '
#last record (either news2 or x?fnid)
if (title.lower() == 'more' or '/x?fnid' in url):
title = 'NextId'
if ('/x?fnid' in url):
url = '%s/format/%s/page/%s' % (apiURL, format, url.replace('/x?fnid=', ''))
else:
url = '/news2'
itemInfo = 'hn next id %s ' % tupURL[0]
if (format == 'json'):
startTag = '{'
endTag = '},'
#cleanup
if (title):
title = re.sub("\n", "", title)
title = re.sub("\"", "\\\"", title)
if (itemInfo):
itemInfo = re.sub("\"", "\\\"", itemInfo)
itemInfo = re.sub("\n", "", itemInfo)
itemInfo = re.sub("\t", " ", itemInfo)
itemInfo = re.sub("\r", "", itemInfo)
if (len(itemInfo) > 0):
itemInfo = Formatter.data(format, 'description', escape(itemInfo))[:-1]
else:
startTag = '<record>'
endTag = '</record>'
if (len(title) > 0):
title = escape(removeNonAscii(title))
if (len(url) > 0):
url = escape(url)
if (len(user) > 0):
user = escape(user)
if (len(itemInfo) > 0):
itemInfo = Formatter.data(format, 'description', escape(itemInfo))
if (len(title) > 0):
returnData += startTag + Formatter.data(format, 'title', title)
if (len(url) > 0):
returnData += Formatter.data(format, 'url', url)
if (len(score) > 0):
returnData += Formatter.data(format, 'score', score)
if (len(user) > 0):
returnData += Formatter.data(format, 'user', user)
if (len(comments) > 0):
returnData += Formatter.data(format, 'comments', comments)
if (len(timeAgo) > 0):
returnData += Formatter.data(format, 'time', timeAgo)
if (len(itemId) > 0):
#cleanup
if ('item?id=' in itemId):
itemId = itemId.replace('item?id=', '')
returnData += Formatter.data(format, 'item_id', itemId)
if (len(itemInfo) > 0 ):
returnData += itemInfo + endTag
else:
returnData = None
return returnData
示例10: parseCommentsContent
# 需要导入模块: import Formatter [as 别名]
# 或者: from Formatter import data [as 别名]
def parseCommentsContent(hnAPIUrl, hnAPIUrlBackup, apiURL, page='',format='json'):
returnData = MutableString()
returnData = ''
logging.debug('HN URL: %s' % hnAPIUrl)
result = getRemoteData(hnAPIUrl, hnAPIUrlBackup)
if (result):
htmlData = result.content
soup = BeautifulSoup(htmlData)
urlLinksContent = soup('table')
counter = 0
comment_container = {}
for node in urlLinksContent:
commentTd = node.first('td', {'class' : 'default'})
if (commentTd):
authorSpan = commentTd.first('span', {'class' : 'comhead'})
#multi-paragraph comments are a bit tricky, parser wont' retrieve them using "span class:comment" selector
commentSpan = getParagraphCommentSiblings(commentTd.first('span', {'class' : 'comment'}))
replyLink = commentTd.first('a', {'href' : re.compile('^reply.*')})['href']
if (replyLink and "reply?id=" in replyLink):
replyLink = replyLink.replace('reply?id=', '')
if (authorSpan and commentSpan):
#author span: <span class="comhead"><a href="user?id=dendory">dendory</a> 1 day ago | <a href="item?id=3015166">link</a></span>
commentId = authorSpan.first('a', {'href' : re.compile('^item.*')})
user = authorSpan.first('a', {'href' : re.compile('^user.*')})
#get time posted...lame but works. for some reason authorSpan.string returns NULL
timePosted = str(authorSpan).replace('<span class="comhead">', '').replace('</span>', '')
#now replace commentId and user blocks
timePosted = timePosted.replace(str(user), '').replace('| ', '').replace(str(commentId), '')
if (commentId['href'] and "item?id=" in commentId['href']):
commentId = commentId['href'].replace('item?id=', '')
#cleanup
commentString = removeHtmlTags(str(commentSpan))
if ('__BR__reply' in commentString):
commentString = commentString.replace('__BR__reply', '')
comment_container[counter] = [commentId, user.string, timePosted.strip(), commentString, replyLink]
counter = counter + 1
#build up string
commentKeyContainer = {}
for key in comment_container.keys():
listCommentData = comment_container[key]
if (listCommentData and not commentKeyContainer.has_key(listCommentData[0])):
commentId = listCommentData[0]
if (commentId):
commentKeyContainer[commentId] = 1
userName = listCommentData[1]
whenPosted = listCommentData[2]
commentsString = listCommentData[3]
replyId = listCommentData[4]
if (format == 'json'):
startTag = '{'
endTag = '},'
#cleanup
if (commentsString):
commentsString = re.sub("\"", "\\\"", commentsString)
commentsString = re.sub("\n", "", commentsString)
commentsString = re.sub("\t", " ", commentsString)
commentsString = re.sub("\r", "", commentsString)
if (len(commentsString) > 0):
commentsString = Formatter.data(format, 'comment', escape(removeNonAscii(commentsString)))
else:
commentsString = "n/a "
else:
startTag = '<record>'
endTag = '</record>'
if (len(userName) > 0):
userName = escape(removeNonAscii(userName))
if (len(whenPosted) > 0):
whenPosted = escape(whenPosted)
if (len(commentsString) > 0):
commentsString = Formatter.data(format, 'comment', escape(removeNonAscii(commentsString)))
if (commentId and userName and whenPosted and replyId and commentsString):
if (len(commentId) > 0):
returnData += startTag + Formatter.data(format, 'id', commentId)
if (len(userName) > 0):
returnData += Formatter.data(format, 'username', userName)
if (len(whenPosted) > 0):
returnData += Formatter.data(format, 'time', whenPosted)
if (len(replyId) > 0):
returnData += Formatter.data(format, 'reply_id', escape(replyId))
if (len(commentsString) > 0 ):
returnData += commentsString + endTag
else:
returnData = None
return returnData