Skip to content

Instantly share code, notes, and snippets.

import os
import matplotlib.pyplot as plt
from imageio import imread
from wordcloud import WordCloud
# font type
base_path = os.getcwd()
font_path = base_path + '/fonts/SourceHanSerif/SourceHanSerifK-Light.otf'
# backgroud image
import re
from pyhanlp import *
words_count_dict = {}
skip_word_pat = re.compile("文章|https|http|html|JPTT|bbs|標題|ptt|女孩|八卦|版|WomenTalk|批踢踢")
def _segment(source):
for term in HanLP.segment(source):
pos = term.nature.__str__()
import asyncio
import requests
from bs4 import BeautifulSoup as bs
from requests.adapters import HTTPAdapter
from concurrent.futures import ThreadPoolExecutor
def fetch(session, url):
with session.get(url) as response:
src_html = response.text
def get_hotlist_article_urls():
hotlist_url = "https://moptt.azurewebsites.net/api/v2/hotpost?b=Gossiping&b=Boy-Girl&b=Beauty&b=marvel&b=WomenTalk&b=movie&page=%257B%2522skip%2522%253A0%257D"
headers = {"cookie": "over18=1;", "Authorization": "cMIS1Icr95gnR2U19hxO2K7r6mYQ96vp"}
resp = requests.get(hotlist_url, headers=headers)
res_json = resp.json()["posts"]
article_urls = [r["url"] for r in res_json]
from dataclasses import dataclass, asdict
import datetime
@dataclass
class AccountRecord:
consumption: int
event: str
date: datetime.datetime
today = datetime.datetime.now()
from cached_property import cached_property
class Monopoly(object):
def __init__(self):
self.boardwalk_price = 500
@cached_property
def boardwalk(self):
# Again, this is a silly example. Don't worry about it, this is
from cached_property import cached_property
class Monopoly(object):
def __init__(self):
self.boardwalk_price = 500
@cached_property
def boardwalk(self):
# using example from cached_property pypi document
class Pokemon(object):
def __init__(self, name, skill, level):
self.name = name
self.skill = skill
self.level = level
@property
def attack_power(self):
return self.level * 100
class Investment(object):
def __init__(self, us_dollar, fund, stock):
self.us_dollar = us_dollar
self.fund = fund
self.stock = stock
# convert unit: transfer us_dollar to RMB
@staticmethod
def convert_unit(us_dollar):
# -*- coding: UTF-8 -*-
class User(object):
bar = 1
def __init__(self, name, age, height):
self.name = name
self.age = age
self.height = height
# this function didnt use other class property