Skip to content

Instantly share code, notes, and snippets.

@hamaguchi-amago
hamaguchi-amago / dict_to_str.py
Created Jul 14, 2021
【Python】dictからstrへの変換・strからdictへの変換 【Python】dictからstrへの変換・strからdictへの変換
View dict_to_str.py
import json
dict_data = {"key1": "data1",
"key2": "data2",
"key3": "data3"}
print(dict_data)
print(type(dict_data))
str_data = json.dumps(dict_data)
print(str_data)
@hamaguchi-amago
hamaguchi-amago / tortoise_orm_is_null.py
Created Jul 13, 2021
【Tortoise ORM】Null・NotNullの抽出方法
View tortoise_orm_is_null.py
from tortoise import fields, models
class TestModel(models.Model):
id = fields.IntField(pk=True, null=False)
name = fields.CharField(max_length=5, null=True)
# Null
null_data = await TestModel.filter(name__isnull=True)
@hamaguchi-amago
hamaguchi-amago / tortoise_orm_order_by.py
Created Jul 12, 2021
【Tortoise ORM】OrderBy昇順・降順
View tortoise_orm_order_by.py
from tortoise import fields, models
class TestModel(models.Model):
id = fields.IntField(pk=True, null=False)
name = fields.CharField(max_length=5, null=False)
# 昇順
asc_data = await TestModel.all().order_by("name")
@hamaguchi-amago
hamaguchi-amago / fastapi_checkbox_value_get.py
Last active Jul 12, 2021
【FastAPI】チェックボックスの値取得方法
View fastapi_checkbox_value_get.py
from fastapi import (
APIRouter,
Form,
)
router = APIRouter()
@router.post("/")
async def function(
checkbox: bool = Form(False),
@hamaguchi-amago
hamaguchi-amago / fastapi_form_blank_error.py
Created Jul 9, 2021
FastAPIで空欄のINPUTをPOSTすると発生するエラーの解消方法
View fastapi_form_blank_error.py
from typing import Optional
from fastapi import (
APIRouter,
Form,
)
router = APIRouter()
@hamaguchi-amago
hamaguchi-amago / dynamic_parameter.py
Created Jul 8, 2021
動的な引数の渡し方のサンプル
View dynamic_parameter.py
def function(
param1='default1',
param2='default2',
param3='default3',
):
print(f"param1:{param1}")
print(f"param2:{param2}")
print(f"param3:{param3}")
@hamaguchi-amago
hamaguchi-amago / dict_none_delete.py
Created Jul 7, 2021
dictからNoneのKeyを削除するサンプル
View dict_none_delete.py
dict1 = {'key1': 'value1',
'key2': None,
'key3': 'value3',
'key4': None,
'key5': 'value5'}
dict1 = {k: v for k, v in dict1.items() if v is not None}
print(dict1)
@hamaguchi-amago
hamaguchi-amago / .htaccess
Last active Jan 18, 2020
簡単なFlaskのサンプル
View .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /flask_sample/index.cgi/$1 [QSA,L]
@hamaguchi-amago
hamaguchi-amago / sample_svc.py
Created Jan 4, 2020
機械学習(クラス分け)処理
View sample_svc.py
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# ファイルの読み込み・準備
test_data = pd.read_csv("test_data.csv", encoding="utf-8")
# データとラベルの準備
evaluation = test_data.loc[:, "Evaluation"]
@hamaguchi-amago
hamaguchi-amago / sample_write_test_data_1.py
Created Jan 4, 2020
機械学習(クラス分け)用のデータ作成処理
View sample_write_test_data_1.py
import csv
import random
with open('test_data.csv', 'w') as f:
writer = csv.writer(f, lineterminator='\n')
writer.writerow(['Japanese', 'Mathematics', 'English', 'Evaluation'])
for i in range(500):
test = []
for j in range(3):
test.append(random.randint(0, 10) * 10)