以下代码存储在名为sample.py的文件中。
import re
from typing import Optional, Tuple
def func(path: str) -> Optional[Tuple[str, str]]:
regex = re.compile(r"/'([^/']+?)'/'([^/']+?)'")
try:
return regex.match(path).groups()
except AttributeError:
return None
Run Code Online (Sandbox Code Playgroud)
Mypy Python linter 在分析代码时抛出以下错误:
sample.py:8: error: Incompatible return value type (got "Union[Sequence[str], Any]", expected "Optional[Tuple[str, str]]")
sample.py:8: error: Item "None" of "Optional[Match[str]]" has no attribute "groups"
Run Code Online (Sandbox Code Playgroud)
虽然regex.match(path).groups()可能返回一个None没有groups属性的类型,但会处理产生的异常,并在返回类型中指定处理方式。但是,Mypy 似乎不明白正在处理异常。据我了解Optional[Tuple[str, str]]是正确的返回类型,而 Mypy 坚持认为不太具体的类型Union[Sequence[str], Any]是正确的。在 Python 类型中使用异常处理的正确方法是什么?(请注意,我并不是要求在不使用异常处理的情况下编写代码的替代方法。我只是想提供一个最小且完整的示例,其中 Python 类型检查器的行为与我期望的异常处理不同。)
我开始学习 Haskell,我发现ghc可以使用带有标志的 LLVM 进行编译-fllvm。每当我使用该标志时,我都会收到错误消息:
<no location info>: error:
Warning: Couldn't figure out LLVM version!
Make sure you have installed LLVM 3.7
ghc: could not execute: opt
Run Code Online (Sandbox Code Playgroud)
不过,我opt的文件夹里有/usr/local/Cellar/llvm/3.9.0/。我使用的是 Mac OS X,并且已经安装了完整的 LLVM,brew install llvm但错误仍然存在。这是一个真正的版本问题吗?我必须卸载 LLVM 并重新安装其 3.7 版本?或者在ghc查找时遇到问题opt,并且我可以修改某种搜索路径来解决问题?感谢您的帮助,祝您有美好的一天。
我正在处理一个自定义文件路径类,它应该始终在写入相应的系统文件并关闭其文件对象后执行一个函数。该函数将文件路径的内容上传到远程位置。我希望从用户的角度来看,上传功能完全在幕后发生,即用户可以像使用任何其他os.PathLike
类一样使用该类并自动获取上传功能。下面的伪代码供参考。
import os
class CustomPath(os.PathLike):
def __init__(self, remote_path: str):
self._local_path = "/some/local/path"
self._remote_path = remote_path
def __fspath__(self) -> str:
return self._local_path
def upload(self):
# Upload local path to remote path.
Run Code Online (Sandbox Code Playgroud)
当用户直接调用任何方法时,我当然可以自动调用上传函数。
但是,我不清楚如果有人使用内置文件写入文件,如何自动调用上传功能open,如下所示。
custom_path = CustomPath("some remote location")
with open(custom_path, "w") as handle:
handle.write("Here is some text.")
Run Code Online (Sandbox Code Playgroud)
或者
custom_path = CustomPath("some remote location")
handle = open(custom_path, "w")
handle.write("Here is some text.")
handle.close()
Run Code Online (Sandbox Code Playgroud)
我希望与open函数调用兼容,以便上传行为适用于所有第三方文件编写器。这种行为在 Python 中是可能的吗?
我正在尝试用C++运行我的第一个opengl程序,它打开一个窗口,设置一个背景颜色,并从Mac OS X上的终端给出一个标题.
代码编译和链接很好.当我运行程序时,窗口和标题打开很好,但背景颜色总是黑色.
我的理解是该功能glClearColor设置背景颜色.但是,无论我传递给函数的参数是什么,窗口的背景颜色总是黑色.
如果有人能向我解释我正在犯的错误,我将非常感激.谢谢,以下是代码:
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const GLint WIDTH = 800, HEIGHT = 600;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Learn OpenGL", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if(nullptr == window)
{
std::cout << "Failed to create GLFW window" << '\n';
glfwTerminate();
return -1;
}
glewExperimental = GL_TRUE;
GLenum err=glewInit();
if(err != glewInit())
{
std::cout …Run Code Online (Sandbox Code Playgroud)