2
1

More than 1 year has passed since last update.

オブジェクト指向言語経験Python初心者の備忘録

Last updated at Posted at 2022-03-03

じっくり育てていこうと思います。

自作クラス


class User:
    id: str
    name: str

    @staticmethod
    def valid_user_id(v: str) -> str:
        if not v or not len(v):
            raise Exception("ユーザーIDを正しく指定してください!")

    @classmethod
    def valid_user_name(cls, v: str) -> str:
        if not v or not len(v):
            raise Exception("ユーザー名を正しく指定してください!")

    def __init__(self, id: str, name: str, **kawrgs):
        self.id = User.valid_user_id(id)
        self.name = User.valid_user_name(name)

        # id, name以外にパラメータが与えられていたらセットしておく
        for k in kawrgs:
            setattr(self, k, kawrgs.get(k))

    def __str__(self) -> str:
        return f"ユーザーID={self.id} ユーザー名={self.name}"

    def __repr__(self) -> str:
        return self.to_json()

    def __bool__(self) -> bool:
        return self.id is not None and self.name is not None

    def to_json(self) -> str:
        return json.dumps(self.__dict__, ensure_ascii=False, indent=4)

async def main():
     v = User(
        id="1",
        name="1番さん",
        xxx="xxx",
        amount=999,
    )
    print(v)
    print(v.to_json())

if __name__ == "__main__":
    asyncio.run(main()) # main内でasync関数を使用する場合はこう実行する(ついで)

# ユーザーID=1 ユーザー名=1番さん

#{
#    "id": "1",
#    "name": "1番さん",
#    "xxx": "xxx",
#    "amount": 999
#}

JSON

Dict->JSONを人間に見やすく出力

json_sample_dict: Dict[str, any] = {"id": "XX001", "name": "激辛マイケルズ兄さん"}
print(json_sample_dict)
print(json.dumps(json_sample_dict, ensure_ascii=False, indent=4))
# {'id': 'XX001', 'name': '激辛マイケルズ兄さん'}
# {
#    "id": "XX001",
#    "name": "激辛マイケルズ兄さん"
# }

Dict->JSON strに変換

    dic = {
        "hoge": "xxx",
    }
    print(json.dumps(dic).replace("x", "y"))
# {"hoge": "yyy"}

JSON strからdictに変換

    print(
        json.loads(
            """
            {
                "hoge": "ggg"
            }
            """
        )
    )
# {'hoge': 'ggg'}

辞書

値を取り出す

    dic = {
        "hoge": "xxx",
    }
    print(dic.get("a", "aは無いよ!"))
    print(dic["a"])
# aは無いよ!
# 例外が発生しました: KeyError

# バラシて取り出す
x, y = {"A":1, "B":2}.values()
print(y)
# 2

値をセットする

    dic = {
        "hoge": "xxx",
    }
    dic["a"] = "aだお"
    print(dic["a"])

    dic.setdefault("a", "xxx") # 既にそのキーで値がある時はセットされない
    dic.setdefault("b", "vvv")
    print(dic)

# aだお
# {'hoge': 'xxx', 'a': 'aだお', 'b': 'vvv'}

# forやifの内包表記で値を一発セットする
    dics = [
        {
            "hoge": "xxx",
        },
        {
            "hoge": "yyy",
        },
    ]
    
    [d.update(hoge=d.get("hoge") + "更新したぜ!") for d in dics]
    print(dics)
# [{'hoge': 'xxx更新したぜ!'}, {'hoge': 'yyy更新したぜ!'}]

ループ

# キーstrを順番に取り出す
for k in d.keys():
    ...

# 値を順番に取り出す
for v in d.values():
    ...

# キーと値を同時に順番に取り出す
for k, v in d.items():
    ...

ソート

dictlist: Dict[str, any] = [
    {"price":100, "name":"りんご"},
    {"price":50, "name":"みかん"},
    {"price":50, "name":"レモン"},
    {"price":120, "name":"なし"},
]
# keyにタプルを指定すると複数キーでソートできる
sorted_dictlist = sorted(dictlist, key=lambda x: (x["price"], x["name"]))

結合

d1.update(d2) # d1が直接上書きされる

joined_dict = dict(**d1, **d2) # 別の入れ物としてreturnされる

リスト

重複を弾く

elems = ["", "", "", ""]
elems = list(set(elems)) 
# ["", "あ", "え"]

リストを条件で選別する

elems = ["", "", ""]
selected_list = [elem for elem in elems if elem != ""]
# ["あ", "え"]

一部を抽出する

list_choise = mylist[1: 3]
list_choise = mylist[1:] #2番目以降を全て抽出する

# バラして取り出す
x, y = [1, 2]
print(y)
# 2

ソート

mylist.sort() # mylistが上書きされる

mylist.sort(reverse=True) #降順

new_list = sorted(mylist) # 別の入れ物としてreturnされる

# 逆順に並べ替える
print(*reversed([1, 2, 3]))
# 3 2 1

結合

a.extend(b) # aが上書きされる

# 結合しつつリターン
    a = [1]
    b = [2]
    print(list([*a, *b]))
# [1, 2]

タプル

ソート

sorted_str_list = sorted(str_tuple)

listをtupleに変換

tup = tuple([1, 2, 3, 4])

tupleをlistに変換

tup = list((1, 2, 3, 4))

文字

strを分割する

s = "a,b,c"
s.split(",") 
# ["a", "b", "c"]

連結

joined_str = ",".join(["a", "b", "c"])
# "a,b,c"

ソート

sorted_char_list = sorted("bedac")
# ['a', 'b', 'c', 'd', 'e']

正規表現チェック

Eメールアドレス

if not re.match("[^@]+@[^@]+\.[^@]+", email_address):
    return False

画像

縦・横の短い方に合わせてできるだけ中央を正方形に切り抜く

from PIL.Image import Image

def get_square_cropped_pillow_image(im: Image) -> Image:
    square_size = min(im.size)
    width, height = im.size
    box = None
    if width > height:
        top = 0
        bottom = square_size
        left = (width - square_size) / 2
        right = left + square_size
        box = (left, top, right, bottom)
    else:
        left = 0
        right = square_size
        top = (height - square_size) / 2
        bottom = top + square_size
        box = (left, top, right, bottom)
    return im.crop(box)

時間

現在日時を取得する

from datetime import datetime

print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# 2022-02-09 16:54:46

print(datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
# 2022-02-09 07:54:46

タイムゾーン変換する

from datetime import datetime
from dateutil import tz

UTC = tz.gettz("UTC")
print(datetime.now(UTC).strftime("%Y-%m-%d %H:%M:%S"))
# 2022-02-09 07:54:46


JST = timezone(timedelta(hours=+9), "JST")

def to_jst(dt: datetime) -> datetime:
    if dt is None:
        return None
    return dt.astimezone(JST)

時間の加減操作

print(datetime.now() + timedelta(minutes=int(10)))

Enum

    class Fruit(str, Enum):
        APPLE = "APPLE"
        BANANA = "BANANA"

    if "POTATO" not in [v.value for v in Fruit]:
        print("フルーツじゃないよ!")
# フルーツじゃないよ!

    if Fruit("APPLE") in Fruit:
        print("フルーツだよ!")
# フルーツだよ!

    if "APPLE" in [v.value for v in Fruit]:
        print("フルーツだよ!")
# フルーツだよ!

クラス

無名関数(lambda)

add_a_and_b = lambda a, b=1: a + b

インスタンスの型判定

print(isinstance(["aaa"], List))
# True

print(isinstance("a", type("b")))
# True

None(null)判定

v = None
print("Noneだよ!" if v is None else v)
# Noneだよ!

v = None
print("Noneだよ!" if not v else v)
# Noneだよ!

print(isinstance(None, type(None)))
print(not None)
print(None is None)
# True
# True
# True

# Pythonのこういうとこ好き
print(v or "Noneだよ!")
# Noneだよ!

main

非同期関数を実行する

import asyncio

async def main():
    print("非同期処理だよ!")


if __name__ == "__main__":
    asyncio.run(main())

# 非同期処理だよ!
2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1