一段时间以来,我一直在努力处理这段代码,并且正在寻求帮助。我有一个日期数组,我试图按数组顺序发出http请求,并按顺序写返回信息。
这是我的代码:
import os
import tkinter as tk
from tkinter import messagebox
import DatabaseFile as db
def selection():
print("At %s of %d" % (select.curselection(), len(contacts)))
return int(select.curselection()[0])
def add_contact():
try:
db.add_contact(name_var.get(), phone_var.get())
except Exception as ex:
print('Ex:', ex)
messagebox.showwarning("Field cannot be blank", "Please enter a value in both the fields.")
else:
set_list()
def update_contact():
contacts[selection()] = [name_var.get(), phone_var.get()]
set_list()
def delete_contact():
del contacts[selection()]
setList()
def load_contact():
_, name, phone = contacts[selection()]
name_var.set(name)
phone_var.set(phone)
def save_contact():
fobj = open("contacts.py", "w")
fobj.write("contactlist = [")
for items in contacts:
fobj.write(str(items))
fobj.write(",")
fobj.write("\n")
fobj.write("]")
def exit_contact():
result = messagebox.askokcancel(title="Contacts", message="Do you want to exit, OK or Cancel")
if result == 1:
exit(1)
def build_frame():
global name_var
global phone_var
global select
root = tk.Tk()
root.title('My Contact List')
# ---
frame_form = tk.Frame(root)
frame_form.pack()
tk.Label(frame_form, text="Name:").grid(row=0, column=0, sticky='w')
name_var = tk.StringVar()
name = tk.Entry(frame_form, textvariable=name_var)
name.grid(row=0, column=1, sticky='w')
tk.Label(frame_form, text="Phone:").grid(row=1, column=0, sticky='w')
phone_var = tk.StringVar()
phone = tk.Entry(frame_form, textvariable=phone_var)
phone.grid(row=1, column=1, sticky='w')
# ---
frame_buttons = tk.Frame(root)
frame_buttons.pack()
btn1 = tk.Button(frame_buttons, text=" Add ", command=add_contact)
btn2 = tk.Button(frame_buttons, text="Update", command=update_contact)
btn3 = tk.Button(frame_buttons, text="Delete", command=delete_contact)
btn4 = tk.Button(frame_buttons, text=" Load ", command=load_contact)
btn5 = tk.Button(frame_buttons, text=" Save ", command=save_contact)
btn6 = tk.Button(frame_buttons, text=" Exit ", command=exit_contact)
btn1.pack(side='left')
btn2.pack(side='left')
btn3.pack(side='left')
btn4.pack(side='left')
btn5.pack(side='left')
btn6.pack(side='right')
# ---
frame_list = tk.Frame(root)
frame_list.pack()
scroll = tk.Scrollbar(frame_list, orient='vertical')
select = tk.Listbox(frame_list, yscrollcommand=scroll.set, height=7)
scroll.config(command=select.yview)
scroll.pack(side='right', fill='y')
select.pack(side='left', fill='both')
return root
def set_list() :
global contacts
contacts = db.read_contacts()
select.delete(0, 'end')
#for item in contacts:
# select.insert('end', item)
select.insert('end', *contacts) # need star * to insert all items at once
# --- main ---
contacts = [] # default value at start
# ---
db.open() # open once before program
db.create_table()
root = build_frame()
set_list()
root.mainloop()
db.close() # close after closing program
答案 0 :(得分:0)
我设法通过使用使用承诺而不是回调的'request'版本解决了这个问题。查看代码:
async function loppThroughArray() {
for (const date of dateArray) {
const options = {
url: process.env.URL + date
};
await asyncRequest(options, date);
console.log('fetching: ', date);
}
}
async function asyncRequest(options, date) {
await rp(options)
.then(function(html){
const $ = cheerio.load(html);
const content = $('.entry-content');
const formattedContent = content.text()
.split('\t').join('')
.split('\n\n\n').join('')
.split('\n\n').join('');
const dataToBeWritten = '### ' + date + '\n' + formattedContent + '\n\n';
fs.appendFileSync(`./WODs/${currentDate}.md`, dataToBeWritten, 'utf-8', {'flags':'a+'});
fs.appendFileSync(`./WODs/${currentDate}.txt`, dataToBeWritten, 'utf-8', {'flags':'a+'});
});
}
loppThroughArray();