0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

しゅっとしたseederを作ろう

Last updated at Posted at 2023-10-17

まずソースを見て

そう、継承つきのデータモデルのクラスがあるね

from abc import ABC, abstractmethod

class BasePairDataModel(ABC):
    created_at = "YYYY-MM-DD 00:00:00"
    updated_at = "YYYY-MM-DD 00:00:00"
    @abstractmethod
    def return_value(self):
        pass

class AsIsPairDataModel(BasePairDataModel):
    """
    データ構造クラスにラッピングしてそのまま(as is)返す
    """
    def __init__(self, data_a: int, data_b: int):
        self.data_a = data_a
        self.data_b = data_b

    def return_value(self):
        return self.data_a + self.data_b

class TwicePairDataModel(BasePairDataModel):
    """
    データ構造クラスにラッピングして2倍して返す
    """
    def __init__(self, data_c: int, data_d: int):
        self.data_c = data_c
        self.data_d = data_d

    def return_value(self):
        return (self.data_c * 2) + (self.data_d * 2)

つぎに実行パートのソースをみよう

import json
from typing import Callable

def seeding(json_data: str, callback_func: Callable[[int], BasePairDataModel]):
    datas = json.loads(json_data)
    return_value = []
    for data in datas:
        return_value.append(callback_func(data))

    print([x.return_value() for x in return_value])

if __name__ == "__main__":
    data_list_for_as_is = json.dumps([
        {"data_a": 1, "data_b": 2},
        {"data_a": 10, "data_b": 20},
        {"data_a": 5, "data_b": 10},
        {"data_a": 100, "data_b": 200},
        {"data_a": 7, "data_b": 14},
        {"data_a": 50, "data_b": 60},
        {"data_a": 11, "data_b": 12},
        {"data_a": 25, "data_b": 30},
        {"data_a": 18, "data_b": 36},
        {"data_a": 8, "data_b": 16}
    ])
    data_list_for_twice = json.dumps([
        {"data_c": 1, "data_d": 2},
        {"data_c": 10, "data_d": 20},
        {"data_c": 5, "data_d": 10},
        {"data_c": 100, "data_d": 200},
        {"data_c": 7, "data_d": 14},
        {"data_c": 50, "data_d": 60},
        {"data_c": 11, "data_d": 12},
        {"data_c": 25, "data_d": 30},
        {"data_c": 18, "data_d": 36},
        {"data_c": 8, "data_d": 16}
    ])
    seeding(data_list_for_as_is, lambda data: AsIsPairDataModel(data_a=data["data_a"], data_b=data["data_b"]))
    seeding(data_list_for_twice, lambda data: TwicePairDataModel(data_c=data["data_c"], data_d=data["data_d"]))

Console

[3, 30, 15, 300, 21, 110, 23, 55, 54, 24]
[6, 60, 30, 600, 42, 220, 46, 110, 108, 48]

なにに注目してほしいのか

image.png

2種類のデータモデルクラス(データのキーも全く異なる)を使い分けながら、seeding 関数自体は1つで済んでいるというところ!まるでバスクリンを溶かした風呂を手でかきまぜるがごとし(よくわからない例えシリーズ)

つまり、「機構」がひとつなのにまったく違うクラスを Callback として外から投げ込んで処理を定義できる

seederって普段はDjango使ってるからフレームワークに頼ってるんだけど、FastAPIってシーダーがないらしくてね。いろんなデータ源をデータモデルクラスにconvertできる。こういう処理はまさに Seeder といえる。いやー勉強になったわ

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?