我有一个 Python 类,它在__init__(). 它还提供了一个用于打开和读取文件的工厂类方法:
from warnings import warn
class MyWarning(Warning):
"""Warning issued when an invalid name is found."""
pass
class MyClass:
def __init__(self, names):
# Simplified; actual code is longer
if is_invalid(names):
names = fix_names(names)
warn(f'{names!r} contains invalid element(s)',
MyWarning, stacklevel=2)
self._names = names
@classmethod
def from_file(cls, filename):
with open(filename) as file:
names = extract_names(file)
return cls(names)
Run Code Online (Sandbox Code Playgroud)
stacklevel=2使警告指的是调用MyClass()而不是warn()语句本身。这在用户代码直接实例化 MyClass 时有效。但是,当MyClass.from_file()发出警告时,MyWarning指的是return cls(names),而不是调用 的用户代码from_file()。
如何确保工厂方法也发出指向调用者的警告?我考虑过的一些选择:
我有一个 GitHub Pages 站点,是通过在 GitHub 上创建存储库并选择主题而生成的。虽然该网站在技术上是使用 Jekyll 构建的,但它没有 gemfile,而且_config.yml非常简洁:
theme: jekyll-theme-dinky
title: "<site name>"
Run Code Online (Sandbox Code Playgroud)
相比之下,使用正确的 Jekyll 站点创建的站点jekyll new .具有正确的gemfile和gemfile.lock文件,以及详细的_config.yml. 请原谅我对 Jekyll 和 Ruby 的无知,但我认为这些对于 Jekyll 在本地构建网站是必要的。
在我的网站的本地克隆上运行jekyll serve无法正确构建网站。然而 GitHub 自己的管道可以。
问题:有没有一种方法可以在本地构建和预览我的网站,而无需添加gemfile/gemfile.lock和更改_config.yml?即在我的计算机上完全执行 GitHub 管道的操作。
我注意到 Object Spread 语法对于它可以接受的值类型非常宽松:
console.log({ ...true });
console.log({ ...false });
console.log({ ...0 });
console.log({ ...42 });
console.log({ ...-1 });
console.log({ ...NaN });
console.log({ ...'batman' });
console.log({ .../\w+[0-9a-fA-F]?/ });
console.log({ ...['foo', 'bar', 42] });
console.log({ ...undefined });
console.log({ ...false });
console.log({ ...Symbol('hmm') });
console.log({ ...Promise.resolve('resolved') });
console.log({ ...Promise.reject('rejected') });Run Code Online (Sandbox Code Playgroud)
在对象字面量中传播时,是否存在无效的类型、类或值(即引发任何类型的错误)?当然,不包括未捕获的拒绝承诺。
Node.js newbie here, Windows 10. I npm install-ed some packages (without -g) while inside a directory that didn't have package.json. npm placed the packages in C:\Users\{MyName}\node_modules\.
Now I'm seeing some weird behavior:
package.json but no node_modules/ yet), npm list and npm list -g both show an empty listpackage.json)...
npm list -g still shows an empty listnpm list shows …constructor ×1
github-pages ×1
javascript ×1
jekyll ×1
node.js ×1
npm ×1
python ×1
python-3.x ×1
warnings ×1