Month: July 2017

  • linecache — 你所不知道的 Python 標準函式庫用法 05

    linecache — 你所不知道的 Python 標準函式庫用法 05

    linecache – Random access to text lines The linecache module allows one to get any line from a Python source file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file. 官方介紹文件:11.9. linecache — Random access to text lines linecache 這個模組旨在讓其他 Python library 能夠高效率的取得其他模組或套件的原始碼,在整個標準函式庫中被 import 了 21 次,主要是被 traceback 以及…

  • weakref — 你所不知道的 Python 標準函式庫用法 04

    weakref — 你所不知道的 Python 標準函式庫用法 04

    weakref – Weak References The weakref module allows the Python programmer to create weak references to objects. 官方介紹文件:8.8. weakref — Weak references weakref 函式庫是用來建立對物件的弱引用 (weak reference)。弱引用並不能保證被引用的物件能存活而不受 garbage collector 回收。弱引用通常會使用在 circular reference、caches 或是需要存放大型物件的地方 (上一篇文章談到的 abc 就有使用 WeakSet 來存放 Class attributes)。 01. Quickstart Tutorial 要理解前面一句話,你必須要先了解,在 CPython 中是對每個物件 (還記得 Everytething in Python is Object 嗎?) 採用 reference count 這樣的方式來達成記憶體管理。當物件 A 被建立的時候,其 reference count 會被設定為 1,如果有其他物件…

  • abc — 抽象類別 — 你所不知道的 Python 標準函式庫用法 03

    abc — 抽象類別 — 你所不知道的 Python 標準函式庫用法 03

    abc — Abstract Base Classes This module provides the infrastructure for defining abstract base classes (ABCs) in Python, as outlined in PEP 3119; see the PEP for why this was added to Python. (See also PEP 3141 and the numbers module regarding a type hierarchy for numbers based on ABCs.) 官方介紹文件:29.7. abc — Abstract Base Classes 01. Quickstart Tutorial 使用 Python abc 套件的原因,是為了要解決…

  • 說明文件如何運作,並且應用在你的專案 – PyCon 2017 How documentation works, and how to make it work for your project

    說明文件如何運作,並且應用在你的專案 – PyCon 2017 How documentation works, and how to make it work for your project

    How documentation works, and how to make it work for your project By Daniele Procida, video: youtube. article: What nobody tells you about documentation   TL; DR: 4-dimensional goals. TUTORIALS HOW-TO GUIDE DISCUSSIONS REFERENCE

  • random —  你所不知道的 Python 標準函式庫用法 02

    random — 你所不知道的 Python 標準函式庫用法 02

    random – 產生偽隨機亂數 This module implements pseudo-random number generators for various distributions. 官方介紹文件:9.6. random — Generate pseudo-random numbers 如果要產生亂數密碼或是 token,請使用 secrets 模組,絕對不要使用 random 模組來產生密碼。 亂數狀態相關 01. random.seed(a=None, version=2) random.seed() 初始化亂數種子。a 如果是 None 則使用目前的亂數種子。a 可以是 str, bytes, bytearray, 都會被轉型成 int。 02. random.getstate() random.getstate() 取得亂數器內部狀態。 03. random.setstate(state) random.setstate() 設定亂數器內部狀態,state需為 random.getstate() 獲得之 state。 使用範例: >>> import random >>> random.seed(‘foobar’) #…

  • sys — 你所不知道的 Python 標準函式庫用法 01

    sys — 你所不知道的 Python 標準函式庫用法 01

    sys — 系統相關的參數以及函式 This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. 官方介紹文件:29.1 sys – System-specific parameters and functions sys 是一個極其強大的 Python 標準函式庫,大家常常忽略他的存在,但是當要做到一些奇淫技巧的時候,就會需要使用 sys 來達成。 01. sys.argv – dealing with command line arguments sys.argv 用來表示從 command line 傳入的參數,sys.argv[0] 代表著程式的名稱。如果你對 C 熟悉的話,就會想起這個東西 int main(int…

  • 有關 Python 官方文件介紹以及 MOTW3 介紹之函式庫統計

    有關 Python 官方文件介紹以及 MOTW3 介紹之函式庫統計

    Python Documentations – The Python Standard Library 一共 247 個標準函式庫在 Python 官方文件被介紹。 [‘__future__’, ‘__main__’, ‘_dummy_thread’, ‘_thread’, ‘abc’, ‘aifc’, ‘and’, ‘argparse’, ‘array’, ‘ast’, ‘asynchat’, ‘asyncio’, ‘asyncore’, ‘atexit’, ‘audioop’, ‘base64’, ‘bdb’, ‘binascii’, ‘binhex’, ‘bisect’, ‘builtins’, ‘bytes’, ‘bz2’, ‘calendar’, ‘cgi’, ‘cgitb’, ‘chunk’, ‘cmath’, ‘cmd’, ‘code’, ‘codecs’, ‘codeop’, ‘collections’, ‘collections.abc’, ‘colorsys’, ‘compileall’, ‘concurrent’, ‘concurrent.futures’, ‘configparser’, ‘contextlib’, ‘copy’, ‘copyreg’, ‘crypt’,…

  • 使用 Python 套件 twstock 抓取台股股票資料 – 01 如何入門

    使用 Python 套件 twstock 抓取台股股票資料 – 01 如何入門

    說到 Python 上面的台股股票套件,大家第一時間一定是想到 toomore/grs,可以說是紅到不能再紅的一個套件。但是他壞掉了。所以誠心向大家推薦改用 mlouielu/twstock。 mlouielu/twstock 以簡潔為目標,延續 toomore/grs 的方便功能 (內建分析,四大買賣點),重新設計一套新的可用的台股抓取套件,同時支援基本市況報導網站的即時資料蒐集。讓大家可以專注在爬蟲設計上面。 使用的方法很簡單,透過 pip install 就可以裝載最新版的 twstock: $ sudo pip install twstock 使用範例: 抓取單檔 (台積電 2330) 股票歷史紀錄 >>> import twstock >>> stock = twstock.Stock(‘2330’) >>> stock.price[-5:] # 近五日之收盤價 [214.0, 214.5, 215.5, 214.0, 214.5] >>> stock.high[-5:] # 近五日之盤中高點 [214.0, 215.0, 215.5, 215.0, 214.5] 抓取單檔 (台積電 2330, 旺矽 6223) 股票即時資料…

  • 高雄輕軌的號誌系統 (多圖)

    高雄輕軌的號誌系統 (多圖)

  • Stacksort – 基於 stackoverflow 的排序法

    Stacksort – 基於 stackoverflow 的排序法

    總之這個排序法很簡單,給定一個含有字串或是數字的陣列,或是 json 格式也行。這個排序法就會透過 stackoverflow 的網站,搜尋被 tag 為 “javascript” 以及 “sort” 的問題。接著再透過爬蟲一個一個題目翻過去,接著在答案堆裏面尋找正確的答案。 什麼,你說答案錯了? 沒關係,請點擊 Try the next answer,繼續尋找正確的答案吧。 什麼,你說找了五分鐘還找不到答案? 沒關係,hacker news 下面已經有人找到解決方式了: The key last step is to have it automatically post questions to StackOverflow to fill in the last code snippets it can’t complete. 下一步就是幫你把無法解決的問題貼到 stackoverflow 上,這樣問題就會被解決了!