def write_output(occur, outfile, write_header=False): if write_header: header = ''' <thead> <tr> <th> Word Set </th> <th> Count </th> </tr> </thead> ''' else: header = '' counts = sorted(((words, len(urls)) for words, urls in occur.iteritems()), key=itemgetter(1), reverse=True) rows = [] for words, count in counts[0:N_MOST_COMMON]: urlfile = 'urls/%s.html' % '-'.join(words) rows.append((' '.join(words), make_link(urlfile, '%d' % count))) url_rows = [(make_link(url), '%d' % count) for url, count in Counter(occur[words]).most_common()] with open(os.path.join(OUTDIR, urlfile), 'w') as fp: fp.write(format_table_page(url_rows)) with codecs.open(outfile, 'w', 'utf-8') as fp: fp.write(format_table_page(rows, header=header)) print 'Top %d word sets written to %s' % (N_MOST_COMMON, outfile)
def get_json_events(url): f = urllib.urlopen(url) raw_json = f.read() f.close() events_json = json.loads(raw_json) events = events_json.get('events', []) events = [event for event in events if upcoming(event)] items = [] if len(events) > 0: events.sort(key = lambda e: parse_date(e.get('date'))) for event in events: title = event.get('title', 'Untitled event') start_date = parse_date(event.get('date')) event_date_str = make_date_range(start_date, start_date) # only handle single day events for now event_tags = event.get('tags', []) event_url = event.get('url', None) entry = make_link(title, event_url) + "<br>" entry += event_date_str if len(event_tags) > 0: entry += "<br>" entry += " ".join([make_tag(tag) for tag in event_tags]) items.append(entry) return make_ul(items) else: return "<p>No upcoming events</p>"
def get_so_answers(user_id): f = feedparser.parse("http://stackoverflow.com/feeds/user/%s" % user_id) answers = [entry for entry in f.entries if is_answer(entry)] answers.sort(key = lambda e : e.published_parsed, reverse = True) if len(answers) > 0: items = [] for answer in answers[:5]: items.append(make_link(get_question_title(answer), get_question_link(answer))) return make_ul(items) else: return "<p>No recent answers</p>"
def get_github_activity(user): f = feedparser.parse("http://github.com/%s.atom" % user) entries = f.entries entries.sort(key = lambda e : e.published_parsed, reverse = True) if len(entries) > 0: items = [] for entry in entries[:5]: title = re.sub("^%s " % user, "", entry.title).capitalize() items.append(make_link(title, entry.link)) return make_ul(items) else: return "<p>No recent activity</p>"
def get_lanyrd(username): events = get_events(username) events.sort(key = lambda e: get_start_datetime(e)) if len(events) > 0: items = [] for event in events: event_title = event.get('SUMMARY') event_date = make_event_date(event) event_url = event.get('URL') items.append(make_link(event_title, event_url) + "<br/>" + event_date) return make_ul(items) else: return "<p>No upcoming events</p>"
def generate_status_text(status: Status) -> str: base: str = hashtag.sub( r'<a href="https://twitter.com/hashtag/\1?src=hashtag_click" target="_blank">#\1</a>', status.full_text) if status.urls: for url in status.urls: if url.expanded_url.rstrip("/").split("/")[-1] == ( getattr(status, "quoted_id_str", None) or getattr(status, "quoted_status_id_str", None) or ""): base = base.replace(url.url, "", 1) else: base = base.replace(url.url, make_link(url.expanded_url), 1) base = base.strip() if status.user_mentions: for user in status.user_mentions: if base.lstrip().startswith("@"): base = base.replace("@" + user.screen_name, "", 1) else: base = base.replace("@" + user.screen_name, twitter_username(user.screen_name)) if status.media: if not base.rstrip().endswith("<br />"): base += "<br />" for media in status.media: if media.type == "photo": base += f' <img src="{media.media_url_https}">' base = base.replace(media.url, "", 1) elif media.type in ["video", "animated_gif"]: type_str = "<video autoplay loop" if media.type == "video": type_str += " controls" type_str += ">" for variant in media.video_info["variants"]: type_str += f'<source src="{variant["url"]}" type="{variant["content_type"]}">' base += type_str + f'<a href="{media.expanded_url}">{url_left_part.sub("", media.display_url)}</a>' + \ "</video>" base = base.replace(media.url, "", 1) return base
def __init__(self, x): self.val = x self.next = None class Solution: def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ pre = dummy = ListNode(0) while head: repeat = head.val if head.next and head.next.val == repeat: while head and head.val == repeat: head = head.next else: pre.next = head head = head.next pre = pre.next pre.next = None return dummy.next if __name__ == '__main__': s = Solution() nums = [1, 2, 2] from utils import make_link, travel_list print(travel_list(s.deleteDuplicates(make_link(nums))))
def test_make_link(self): self.assertEqual(utils.make_link('test1', 'test2', title=''), '<a href="test1">test2</a>') self.assertEqual(utils.make_link('test1', 'test2', title='test3'), '<a href="test1" title="test3">test2</a>')
def get_goodreads(id, shelf): f = feedparser.parse("http://www.goodreads.com/review/list_rss/%s?shelf=%s" % (id, shelf)) items = [] for entry in f.entries: items.append(make_link(entry.title, "http://www.goodreads.com/book/show/%s" % (entry.book_id)) + " by " + entry.author_name) return make_ul(items)
def make_instapaper_favourite_html(entry): return make_link(entry.title, entry.link) + \ " <span style=\"color: #808080;\">(" + get_domain(entry.link) + ")</span>"
def last_fm_track_link(track): artist = track.getElementsByTagName('artist')[0].firstChild.nodeValue name = track.getElementsByTagName('name')[0].firstChild.nodeValue url = track.getElementsByTagName('url')[0].firstChild.nodeValue return make_link(name, url) + " - " + artist
def extract_infolists(filename, lang='sk', mode='regular', verbose=True): """Extract all infolists with all of their courses from a study program XML file. Params: filename: path to the XML file lang: language Returns: list of infolists with cou dics """ xmldoc = ET.parse(filename) root = xmldoc.getroot() organizacnaJednotka = root.find('organizacnaJednotka').text vysokaSkola = root.find('vysokaSkola').text fakulta = root.find('fakulta').text ilisty = root.find('informacneListy') if verbose: print " Nasiel som %d informacnych listov." % len(ilisty.findall('informacnyList')) # elementy, ktore sa budu parsovat z XML-ka # kluc => XPath (kluc sa pouziva neskor v template) elements = {'kod': 'kod', 'nazov': 'nazov', 'kredit': 'kredit', 'sposobVyucby': 'sposobVyucby', 'rozsahTyzdenny': 'rozsahTyzdenny', 'rozsahSemestranly': 'rozsahSemestranly', 'rokRocnikStudPlan': 'rokRocnikStudPlan', 'kodSemesterStudPlan': 'kodSemesterStudPlan', 'sposobUkoncenia': 'sposobUkoncenia', 'studijnyProgram': 'studijneProgramy/studijnyProgram/popis', 'podmienujucePredmety': 'podmienujucePredmety', 'vylucujucePredmety': 'vylucujucePredmety', 'doplujuceUdaje': 'doplujuceUdaje', 'zabezpecuju': 'zabezpecuju', 'strucnaOsnova': '_SO_/texty', 'webstranka': '_URL_/texty/p', 'ciel': '_C_/texty', 'zaverecneHodnotenie': '_Z_/texty/p', 'literatura': '_L_/texty', 'priebezneHodnotenie': '_P_/texty/p', 'obsahovaPrerekvizita': '_O_/texty', 'sylabus': '_S_/texty', 'datumSchvalenia': 'datumSchvalenia', 'vahaHodnotenia': '_VH_/texty/p', 'garanti': 'garanti/garant/plneMeno', 'jazyk': '_PJ_/texty/p', 'obsahovaNapln': '_ON_/texty', 'podmienkyAbsolvovania': '_PA_/texty', 'vysledkyVzdelavania': '_VV_/texty', 'poznamky': '_PZ_/texty'} data = [] # spracovanie informacnych listov jednotlivych predmetov for il in ilisty.findall('informacnyList'): # preskocime statne skusky, tie sa spracuvaju inym skriptom if mode=='regular' and (il.find('_ON_') is not None): continue if mode=='statnice' and (il.find('_ON_') is None): continue d = {'lang' : lang, 'organizacnaJednotka': organizacnaJednotka, 'vysokaSkola': vysokaSkola, 'fakulta': fakulta } for key, path in elements.iteritems(): if il.find(path) is not None: if key != 'vahaHodnotenia' and path.startswith('_'): d[key] = utils.get_text(il.find(path)) elif key in ['studijnyProgram', 'jazyk']: d[key] = [el.text for el in il.findall(path)] if key == 'jazyk': d[key] = list(set(d[key])) else: d[key] = il.find(path).text else: d[key] = '' # uprava kodov predmetov d['kod'] = utils.parse_code(d['kod']) # uprava URL if d['webstranka']: d['webstranka'] = utils.make_link(d['webstranka'],d['webstranka']) data.append(d) return data
:type head: ListNode :type m: int :type n: int :rtype: ListNode """ dummy = ListNode(0) dummy.next = head cur_node = dummy for _ in range(m - 1): cur_node = cur_node.next pre_m = cur_node cur_node = cur_node.next prev_node = rev_node = cur_node for _ in range(n - m + 1): next_node = cur_node.next cur_node.next = rev_node rev_node = cur_node cur_node = next_node prev_node.next = cur_node pre_m.next = rev_node return dummy.next if __name__ == '__main__': s = Solution() nums = [1, 2, 3, 4, 5] from utils import make_link, travel_list print(travel_list(s.reverseBetween(make_link(nums), 2, 4)))
n = nodes.popleft() # 以m为基准进行排序,此时m和n均为有序的链表 rv_head = m if m.val < n.val else n pre = None while m or n: if n.val <= m.val: tmp = n n = n.next tmp.next = m if pre: pre.next = tmp pre = tmp else: pre = m m = m.next if not m: pre.next = n nodes.append(rv_head) return nodes[0] if __name__ == '__main__': from utils import make_link, travel_list l = [1, 2] s = Solution() head = make_link(l) head = s.sortList(head) travel_list(head)
""" :type head: ListNode :rtype: ListNode """ if not head: return head dummy = ListNode(-1) dummy.next = head first, second = head, head.next prev = dummy while first and second: third = second.next prev.next = second second.next = first first.next = third prev = first if third: first, second = third, third.next else: first, second = None, None return dummy.next if __name__ == '__main__': s = Solution() from utils import make_link, travel_list l = [1, 2, 3, 4] new = make_link(l) rel = s.swapPairs(new) print(travel_list(rel))
def make_bookmark_html(post): url = post.getAttribute("href") title = post.getAttribute("description") notes = post.getAttribute("extended") return make_link(title, url, notes) + " <span style=\"color: #808080;\">(" + get_domain(url) + ")</span>"
def addTwoNumbers2(self, l1, l2): dummy_head, carry = ListNode(0), 0 tail = dummy_head while l1 or l2: r1, r2 = 0, 0 if l1: r1 = l1.val l1 = l1.next if l2: r2 = l2.val l2 = l2.next sum = r1 + r2 + carry carry = sum // 10 tail.next = ListNode(sum % 10) tail = tail.next if carry == 1: tail.next = ListNode(1) return dummy_head.next if __name__ == '__main__': from utils import make_link, travel_list s = Solution() l1, l2 = [9, 8], [1] l1 = make_link(l1) l2 = make_link(l2) head = s.addTwoNumbers2(l1, l2) travel_list(head)