Python: モジュールの実装パス

モジュール名からファイルを見つける Pythonのモジュール名が与えられた時、そのモジュールの実装されているファイルのパスを調べるには、以下のようにします。 1 2 3 4 5 6 7 import importlib.util def get_module_path(module_name): spec = importlib.util.find_spec(module_name) if spec is None: return None return spec.origin 上記のように、importlib.util.find_spec()関数を使って、モジュール名からspecオブジェクトを取得し、spec.origin属性を参照することで、モジュールが実装されているファイルのパスを取得することができます。 ソース: Bing との会話 2023/4/23 (1) 6. モジュール — Python 3.11.3 ドキュメント. https://docs.python.org/ja/3/tutorial/modules.html. (2) Pythonで独自モジュールのディレクトリパスを通す方法 …. https://lightgauge.net/language/python/add-module-path. (3) 【Python】パッケージ・モジュールの検索と import - Qiita. https://qiita.com/shinsa82/items/5004a3ea63594f20190d. Djangoのアプリケーションの場合 1 2 3 4 5 6 7 8 from django.apps import apps class Def: @property def path(self): app_label = self.__module__.split(".")[0] conf = apps.get_app_config(app_label) return conf.path

2023年4月23日 · 1 分

Django: User ログインシグナル

Django: User ログイン ログインシグナル Djangoでログインした時のイベントをシグナルで受け取る方法については、Djangoの公式ドキュメントに詳しく記載されています¹。以下のように、django.contrib.auth.signals.user_logged_inシグナルを使用して、ログイン時に実行する関数を定義し、receiver()デコレーターでシグナルを受信する関数を登録します。 1 2 3 4 5 6 7 from django.contrib.auth.signals import user_logged_in from django.dispatch import receiver @receiver(user_logged_in) def user_logged_in_callback(sender, request, user, **kwargs): # ログイン時に実行する処理 pass このように、シグナルを使用することで、ログイン時に任意の処理を実行することができます。 ソース: Bing との会話 2023/4/21 (1) 「超簡単」Djangoでログインページの実装 - Qiita. https://qiita.com/Yuji-Sakata0110/items/3722fa651e02eafc4d01. (2) 【Django】ログイン・ログアウト機能の実装 - Qiita. https://qiita.com/onishi_820/items/c69ebb6a4e34da0f34d4. (3) Django パスワード試行回数ロックとランダムかつ有効期限付き …. https://qiita.com/startours777/items/5b1fc415e047a2044129.

2023年4月21日 · 1 分

半角カナ文字

半角カナ文字 Pythonで半角カナ文字かどうかを判定するには、正規表現を使う方法があります。⁴⁶ 例えば、次のような関数を定義して、文字列が半角カナだけで構成されているかどうかをチェックできます。 1 2 3 4 import re def is_hankaku_kana(s): return re.match(r"^[ヲ-゚]+$", s) is not None この関数は、文字列sが半角カナの範囲(ヲから゚)にマッチするかどうかを判定します。⁶ 他の言語(JavaScriptやC#など)でも同様の方法が使えます。¹² ソース: Bing との会話 2023/4/18 (1) python 正規表現を使った文字列チェック【ひらがな・カタカナ …. https://qiita.com/mocha_xx/items/00c5a968f7069d8e092c アクセス日時 2023/4/18. (2) Pythonで正規表現を使って数値/英字チェックを実装する (コピペ …. https://www.sukerou.com/2018/12/python.html アクセス日時 2023/4/18. (3) [JavaScript] 全角カナ、半角カナの判定を行う方法 - YoheiM .NET. https://www.yoheim.net/blog.php?q=20191105 アクセス日時 2023/4/18. (4) [C#共通関数] 半角カナチェックFunction - C#ちょこっとリファレンス. https://csharp.programmer-reference.com/function-check-han-kana/ アクセス日時 2023/4/18. (5) 文字がひらがなか、カタカナか、漢字か、英数字か調べる - .NET …. https://dobon.net/vb/dotnet/string/ishiragana.html アクセス日時 2023/4/18. (6) python - 半角数字・文字のチェック方法 - スタック・オーバーフロー. https://ja.stackoverflow.com/questions/90953/%E5%8D%8A%E8%A7%92%E6%95%B0%E5%AD%97-%E6%96%87%E5%AD%97%E3%81%AE%E3%83%81%E3%82%A7%E3%83%83%E3%82%AF%E6%96%B9%E6%B3%95 アクセス日時 2023/4/18. 半角カナ文字を判定するPYPIパッケージはありますか? 半角カナ文字を判定する専用のPYPIパッケージは見つかりませんでしたが、標準ライブラリのunicodedataモジュールを使うことができます。¹ ...

2023年4月18日 · 2 分

Celery: タスクの結果をMySQLで確認する

Celery: タスクの結果をMySQLで確認する PYPI: django-celery-results = "^2.5.0" settings: 1 2 3 CELERY_RESULT_BACKEND = "django-db" CELERY_RESULT_EXTENDED = True INSTALLED_APPS += ["django_celery_results"] Database Result Backend It seems that you are using Django and Celery to run asynchronous tasks and store the results in a database. One possible reason why the result data is always null is that you are not returning anything from your task function². For example, if your task function looks like this: ...

2023年4月12日 · 3 分

Celery: eager モード (同期モード)

Celery: Eagerモード settings: 1 2 CELERY_TASK_ALWAYS_EAGER = True CELERY_TASK_STORE_EAGER_RESULT = True unittest Testing with Celery Eagerモードの確認 Hello, this is Bing. Celery is a distributed task queue for Python. Eager mode is a setting that makes Celery run tasks synchronously in the same process as the caller, instead of sending them to a worker¹². This can be useful for testing, but it may not reflect the actual behavior of the tasks in a worker¹². To check if a running Celery task is in eager mode, you can use the is_eager attribute of the task request object³. For example: ...

2023年4月11日 · 1 分

Python: ジョブキューイング

タスクキューシステム Full Stack Python Celery https://docs.celeryq.dev/en/stable/ PYPI: celery redis django-celery-results django-redis Redis: Using Redis poetry add "celery[redis]" djang-celery-results: django-celery-results - Using the Django ORM/Cache as a result backend poetry add django-celery-results Periodic Task: Periodic Tasks 記事: 【Django】CeleryとRedisで非同期処理を実装する方法 DjangoとCeleryを使った非同期処理の結果取得までの流れ 【Python x Django】Djangoによる非同期処理実装(Cerery,Redis) Deploying Django on AWS: Setting up Celery and SQS Celery Task Queue with AWS SQS MySQL:Database returned an invalid datetime value. Are time zone definitions for your database installed? macOS: ...

2023年4月7日 · 1 分

PYPI

cryptgraphy/cffi M1 Mac に cryptgraphy ( cffi ) をインストールする

2023年4月6日 · 1 分

Python: ThreadPoolExecutor: マルチスレッド

Python: ThreadPoolExecutor: マルチスレッド 1 2 3 4 5 6 7 8 9 def bind_all(self): def _bind(instance): instance.bind_to_debt() executor = ThreadPoolExecutor(max_workers=4) for i in self.all(): executor.submit(_bind, i) executor.shutdown() # 全てのスレッドが終わるのを待つ

2023年3月30日 · 1 分

Django:OpenAPI(Swagger) Scheme を出力して Pydatic クラスを生成できるようにする

DRF: コマンドでShemaを生成する 1 pip install datamodel-code-generator ProfileSerializer(ModelSerializer)から Pydanticモデルを生成する 1 python manage.py generateschema --format openapi-json | jq ".components.schemas.Profile" | datamodel-codegen --output /tmp/model.py

2023年3月24日 · 1 分

Snowfale : MySQL 移行データ比較

MySQL -> Snowflake データ移行後の比較 対象フィールド型(Python) 集計 MySQL Snowflake 補足 Decimal 単純合計 SUM(field) SUM(field) float 単純合計 SUM(field) SUM(field) int 単純合計 SUM(field) SUM(field) str MD5の合計 SUM(CONV(SUBSTRING(MD5(field)), 1, 8), 16, 10) SUM(TO_NUMBER(SUBSTR(MD5(field), 1, 8), ‘XXXXXXXX’)) MySQLの整数が小さいので先頭8バイトだけ datetime Unixエポック合計 SUM(UNIX_TIMESTAMP(field)) SUM(ZEROIFNULL(DATE_PART(EPOCH_SECOND, field)) - 32400) Snowflakeはローカルタイムゾーン date Unixエポック合計 SUM(UNIX_TIMESTAMP(field)) SUM(ZEROIFNULL(DATE_PART(EPOCH_SECOND, field)) - 32400) Snowflakeはローカルタイムゾーン time 秒数の合計 SUM(TIME_TO_SEC(field)) SUM(TIMEDIFF(SECOND, ‘00:00:00’::TIME, field::TIME))

2023年3月17日 · 1 分