在运行脚本之前,如何检测是否已在Python脚本中初始化所有变量?理想情况下,我希望我的编译器(当前Wing IDE)在构建/编译过程中检测错误。这是为了防止程序在加载数据等约30分钟后由于一个简单的错误而崩溃。
我知道Python可以通过各种方式操作变量。但是,我的脚本往往相当简单,我不希望该方法是防弹的。
作为一个例子,我希望错误检测方法能够找到简单的错误,例如下面脚本中grocery_bill
的拼写错误。
prices = {'apple': 0.40, 'banana': 0.50}
my_purchase = {
'apple': 1,
'banana': 6}
grocery_bill = sum(prices[fruit] * my_purchase[fruit]
for fruit in my_purchase)
print 'I owe the grocer $%.2f' % grocery_blil
答案 0 :(得分:2)
我目前可以想到两种选择。
答案 1 :(得分:0)
但是类成员变量呢?没有 linter 可以找到它。
const fs = require('fs')
const path = require('path')
const utils = require('util')
const puppeteer = require('puppeteer')
const hb = require('handlebars')
const readFile = utils.promisify(fs.readFile)
async function getTemplateHtml() {
console.log("Loading template file in memory")
try {
const invoicePath = path.resolve("./invoice.html");
return await readFile(invoicePath, 'utf8');
} catch (err) {
return Promise.reject("Could not load html template");
}
}
async function generatePdf() {
let data = {};
getTemplateHtml()
.then(async (res) => {
// Now we have the html code of our template in res object
// you can check by logging it on console
// console.log(res)
console.log("Compiing the template with handlebars")
const template = hb.compile(res, { strict: true });
// we have compile our code with handlebars
const result = template(data);
// We can use this to add dyamic data to our handlebas template at run time from database or API as per need. you can read the official doc to learn more https://handlebarsjs.com/
const html = result;
// we are using headless mode
const browser = await puppeteer.launch();
const page = await browser.newPage()
// We set the page content as the generated html by handlebars
await page.setContent(html)
// we Use pdf function to generate the pdf in the same folder as this file.
await page.pdf({ path: 'invoice.pdf', format: 'A4' })
await browser.close();
console.log("PDF Generated")
})
.catch(err => {
console.error(err)
});
}
generatePdf();