言語リファレンス
Python 言語リファレンス — Python 3.10.6 ドキュメント
標準ライブラリ
https://docs.python.org/ja/3/library/index.html#library-index
コードチェッカー、フォーマッターのインストール
conda install -c conda-forge flake8 black isort mypy
pip install flake8 black isort mypy
Work spaceを作る場合はworkspaceのsetting.jsonを以下の通り
{ "python.linting.flake8Enabled": true, "python.formatting.provider": "black", "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true }, "python.linting.mypyEnabled": true, "python.linting.flake8Args": [ "--max-line-length=88", "--ignore=E203,W503,W504" ], }
コマンドの出力を変数に設定したい場合は、 (バッククォート) を使用する
$ VAR=`date "+%Y-%m-%d"` $ echo $VAR 2022-09-02
フォルダの圧縮・解凍
$ tar -czvf directory.tar.gz directory
$ tar -xzf archive_name.tar.gz
SimpleNamespace
>>> from types import SimpleNamespace >>> d = {'key1': 'value1', 'key2': 'value2'} >>> n = SimpleNamespace(**d) >>> print(n) namespace(key1='value1', key2='value2') >>> n.key2 'value2'
os.rename
)ファイルから作成日の情報を取り出して、'yymmdd'の形式でファイル名の最初につける。
os.rename
# ファイルから作成日の情報を取り出して、'yymmdd'の形式でファイル名の最初につける import os from datetime import datetime from glob import glob def get_dateStr(path: str, system_type='unix'): if system_type=='unix': c_timestamp = os.stat(path).st_birthtime else: c_timestamp = os.path.getctime(path) # for Windows PC dt = datetime.fromtimestamp(c_timestamp) date_str = datetime.strftime(dt, '%y%m%d') return date_str def rename_cdate(path: str): data_str = get_dateStr(path) rm_file_name = f"{data_str}_{os.path.basename(path)}" rm_path = os.path.join(os.path.dirname(path), rm_file_name) os.rename(path, rm_path) if __name__ == '__main__': path_list = glob('*.md') for path in path_list: rename_cdate(path)
sys.path
に登録__file__
でスクリプトファイル自身のpathを参照できるので、これを利用する。
import os import sys # スクリプト自身のpathから一階層上のディレクトリをsys.pathに加える。 sys.path.append(os.path.join(os.path.dirname(__file__), '..')
以下の正規表現で99%以上のDOIは識別されるとのこと。 さらにカバー率を上げるにはrefを参考に追加で4つのパターンを加える。それでもマッチできないものは僅かに残るとのこと。
/^10.\d{4,9}/[-._;()/:A-Z0-9]+$/i
conda install -c conda-forge xmltodict
上記でインストール
import xmltodict import json with open('data.xml', 'r') as f: xml_data = f.read() dictionary = xmltodict.parse(xml_data) json_object = json.dumps(dictionary) print(json_object)
以下の設定をしておく。上書き防止対策。
set -o noclobber
API keyなしでは3回/秒までのリクエスト。(API keyありで10回/秒がデフォルト) この条件を超えないようにすること。これ以上のアクセスが必要な場合はNCBI所定の登録が必要
A General Introduction to the E-utilities - Entrez Programming Utilities Help - NCBI Bookshelf
PMIDからabstractを取得したり、PubMed centralのfull textデータ取得の場合の例などは下記に記載されている。
https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=17284678,9997&retmode=text&rettype=abstract
https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=11748933,11700088&retmode=xml
https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pmc&id=212403
使用できるdb
とデータ型の一覧