使用PyWebIO,我創建了一個最多需要六個日期的用戶表單,每一對組成一個從 start datesdt到 end date的日期范圍edt。前兩個日期是必需的,其余四個是可選的。可以通過函式驗證此表單;check_form在我的例子中。
我想使用這個功能來允許用戶只輸入連續的、按時間順序排列的對。
示例表格:
from pywebio import *
form = input.input_group("form", [
input.input("Period 1 - Start",
name="sdt_1",
type=input.DATE,
required=True),
input.input("Period 1 - End",
name="edt_1",
type=input.DATE,
required=True),
input.input("Period 2 - Start (optional)",
name="sdt_2",
type=input.DATE),
input.input("Period 2 - End (optional)",
name="edt_2",
type=input.DATE),
input.input("Period 3 - Start (optional)",
name="sdt_3",
type=input.DATE),
input.input("Period 3 - End (optional)",
name="edt_3",
type=input.DATE),
], validate = check_form)
PyWebIO 將輸入排序為字典,使用名稱作為鍵,輸入作為值。
輸出print(form):
form = {
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '2021-01-01',
'edt_2': '2021-08-08',
'sdt_3': '2023-01-01',
'edt_3': '2023-08-08'
}
我想出了一個解決方案,可以檢查時間順序和相同數量的條目。但是,此解決方案仍然允許用戶進行無意義的輸入,例如僅輸入日期sdt_1, edt_1, sdt_3, sdt_3或sdt_1, edt_1, sdt_2, edt_3
def check_form(data):
data_lst = []
for key in data.keys():
if data[key] != "":
data_lst.append(data[key])
if sorted(data_lst) != data_lst:
return ("sdt_1", "ERROR: Entries not in chronological order.")
if len(data_lst) % 2:
return ("sdt_1", "ERROR: Uneven amount of entries.")
任何幫助深表感謝!
uj5u.com熱心網友回復:
由于第一對是必需的,其余兩對是可選的,因此您應該強制執行該邏輯。
def check_form(data):
required_pairs = {1} # Specify all the required pairs in this set
max_pairs = 3 # Max numbber of pairs
data_lst = []
for i in range(1, max_pairs 1):
s_key = f"sdt_{i}"
e_key = f"edt_{i}"
# Get the data from the dict. If the key doesn't exist, treat it as an empty string
s_val = data.get(s_key, "")
e_val = data.get(e_key, "")
if i in required_pairs or s_val or e_val:
if i in required_pairs:
# This is a required pair and input is missing
if not s_val: return (s_key, "ERROR: Missing required input")
elif not e_val: return (e_key, "ERROR: Missing required input")
elif e_val and not s_val:
# e_key is specified but not s_key
return (s_key, "ERROR: Uneven pair of inputs")
elif s_val and not e_val:
# s_key is specified but not e_key
return (e_key, "ERROR: Uneven pair of inputs")
# We didn't return an error, so this data must be valid
data_lst.append(data[s_key])
data_lst.append(data[e_key])
if sorted(data_lst) != data_lst:
return ("sdt_1", "ERROR: Entries not in chronological order")
請注意,您可以s簡單地檢查字串是否為空if s(空字串是假的)
讓我們測驗一下:
##### VALID INPUTS:
# Case 1: Specify all inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '2021-01-01',
'edt_2': '2021-08-08',
'sdt_3': '2023-01-01',
'edt_3': '2023-08-08'
}) == None
# Case 2: Specify only required inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '',
'edt_2': '',
'sdt_3': '',
'edt_3': ''
}) == None
# Case 3.1: Specify required inputs and one optional input
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '2021-01-01',
'edt_2': '2021-01-08',
'sdt_3': '',
'edt_3': ''
}) == None
# Case 3.2: Specify required inputs and one optional input
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '',
'edt_2': '',
'sdt_3': '2021-01-01',
'edt_3': '2021-01-08',
}) == None
#### INVALID INPUTS
# Case 4.1: Skip required inputs
assert check_form({
'sdt_1': '',
'edt_1': '2020-08-08',
'sdt_2': '2021-01-01',
'edt_2': '2021-08-08',
'sdt_3': '2022-01-01',
'edt_3': '2022-01-08',
}) == ('sdt_1', "ERROR: Missing required input")
# Case 4.2: Skip required inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '',
'sdt_2': '2021-01-01',
'edt_2': '2021-08-08',
'sdt_3': '2022-01-01',
'edt_3': '2022-01-08',
}) == ('edt_1', "ERROR: Missing required input")
# Case 4.3: Skip required inputs
assert check_form({
'sdt_1': '',
'edt_1': '',
'sdt_2': '2021-01-01',
'edt_2': '2021-08-08',
'sdt_3': '2022-01-01',
'edt_3': '2022-01-08',
}) == ('sdt_1', "ERROR: Missing required input")
# Case 5.1: Incomplete pair of optional inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '',
'edt_2': '2021-08-08',
'sdt_3': '2022-01-01',
'edt_3': '2022-01-08',
}) == ('sdt_2', "ERROR: Uneven pair of inputs")
# Case 5.2: Incomplete pair of optional inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '2021-01-01',
'edt_2': '',
'sdt_3': '2022-01-01',
'edt_3': '2022-01-08',
}) == ('edt_2', "ERROR: Uneven pair of inputs")
# Case 5.3: Incomplete pair of optional inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '2021-01-01',
'edt_2': '2021-08-08',
'sdt_3': '',
'edt_3': '2022-01-08',
}) == ('sdt_3', "ERROR: Uneven pair of inputs")
# Case 5.4: Incomplete pair of optional inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '2021-01-01',
'edt_2': '2021-08-08',
'sdt_3': '2022-01-01',
'edt_3': '',
}) == ('edt_3', "ERROR: Uneven pair of inputs")
# Case 5.5: Incomplete pair of optional inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '2021-01-01',
'edt_2': '',
'sdt_3': '2022-01-01',
'edt_3': '',
}) == ('edt_2', "ERROR: Uneven pair of inputs")
# Case 5.6: Incomplete pair of optional inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '',
'edt_2': '2021-08-08',
'sdt_3': '',
'edt_3': '2022-08-08',
}) == ('sdt_2', "ERROR: Uneven pair of inputs")
# Case 6.1: Non-chronological inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2019-08-08',
'sdt_2': '2021-01-01',
'edt_2': '2021-08-08',
'sdt_3': '2022-01-01',
'edt_3': '2022-08-08',
}) == ('sdt_1', "ERROR: Entries not in chronological order")
# Case 6.2: Non-chronological inputs
assert check_form({
'sdt_1': '2020-01-01',
'edt_1': '2020-08-08',
'sdt_2': '2021-01-01',
'edt_2': '2022-08-08',
'sdt_3': '2022-01-01',
'edt_3': '2022-08-08',
}) == ('sdt_1', "ERROR: Entries not in chronological order")
uj5u.com熱心網友回復:
我想出了一個解決方案。將 dict 放入串列中,并從第二個索引檢查前一個索引是否為空。像這樣:
def check_form(data):
data_lst = []
data_lst_2 = list(form.values())
for key in data.keys():
if data[key] != "":
data_lst.append(data[key])
if sorted(data_lst) != data_lst:
return ("sdt_1", "ERROR: Entries not in chronological order.")
if len(data_lst) % 2:
return ("sdt_1", "ERROR: Uneven amount of entries.")
for item in data_lst_2[1:]:
if data_lst_2[data_lst_2.index(item) - 1] == "":
return ("sdt_1", "ERROR: Entries not consecutive.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/477036.html
標籤:Python python-3.x
上一篇:如何將Matlab中的ppval和@()轉換為Python等效項?
下一篇:如何從我的特定資料框中創建字典?
