请考虑以下代码实现一个简单的MixIn:
class Story(object):
def __init__(self, name, content):
self.name = name
self.content = content
class StoryHTMLMixin(object):
def render(self):
return ("<html><title>%s</title>"
"<body>%s</body></html>"
% (self.name, self.content))
def MixIn(TargetClass, MixInClass):
if MixInClass not in TargetClass.__bases__:
TargetClass.__bases__ += (MixInClass,)
if __name__ == "__main__":
my_story = Story("My Life", "<p>Is good.</p>")
# plug-in the MixIn here
MixIn(Story, StoryHTMLMixin)
# now I can render the story as HTML
print my_story.render()
Run Code Online (Sandbox Code Playgroud)
运行main会导致以下错误:
TypeError: Cannot create a consistent method resolution
order (MRO) for bases object, StoryHTMLMixin …Run Code Online (Sandbox Code Playgroud) Pythonistas:
假设您要使用Pyparsing解析以下字符串:
'ABC_123_SPEED_X 123'
Run Code Online (Sandbox Code Playgroud)
分别ABC_123是一个标识符; SPEED_X是一个参数,123是一个值.我想到了以下使用Pyparsing的BNF:
Identifier = Word( alphanums + '_' )
Parameter = Keyword('SPEED_X') or Keyword('SPEED_Y') or Keyword('SPEED_Z')
Value = # assume I already have an expression valid for any value
Entry = Identifier + Literal('_') + Parameter + Value
tokens = Entry.parseString('ABC_123_SPEED_X 123')
#Error: pyparsing.ParseException: Expected "_" (at char 16), (line:1, col:17)
Run Code Online (Sandbox Code Playgroud)
如果我从中间删除下划线(并相应地调整Entry定义),它会正确解析.
如何使这个解析器变得有点懒惰,并等到它与关键字匹配(而不是将整个字符串作为标识符啜饮并等待_,而不存在.
谢谢.
[注意:这完全改写了我的问题; 我没有意识到真正的问题是什么]
我有两个表'date'列.一个持有(名称,日期),另一个持有(日期,p1,p2).给定一个名称,我想使用表1中的日期来查询表2中的p1和p2; 如果表1中的日期在表2中的日期的两秒内,则匹配应该发生.
你如何使用SQLAlchemy实现这一目标?
我试过(不成功)使用between运算符并使用如下子句:
td = datetime.timedelta(seconds=2)
q = session.query(table1, table2).filter(table1.name=='my_name').\
filter(between(table1.date, table2.date - td, table2.date + td))
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
编辑: 我已设法使用以下方法解决问题:
from sqlalchemy.sql import between
import datetime
# [all other relevant imports]
td = datetime.timedelta(seconds=2)
t1_entry = session.query(table_1).filter(table_1.name == 'the_name').first()
if t1_entry is not None:
tmin = t1_entry.date - td
tmax = t1_entry.date + td
t2_entry = session.query(table_2).filter(between(table_2.date, tmin, tmax)).first()
return (t1_entry, t2_entry)
return None
Run Code Online (Sandbox Code Playgroud)
所以可以进行比较,但我不确定这种方法是否有效.
我有一个简单的工作流程[Step 0]->[1]->[2]->...->[Step N]。主程序知道它当前所处的步骤(状态)。
我想将其实时传输到网站(在局域网中),以便当我的同事打开时,例如,http://thecomputer:8000他们可以看到我们工作流程当前状态的实时渲染以及任何相关细节。
我已经考虑过将脚本的状态写入一个StringIO对象(流式传输到它)并用于Javascript刷新浏览器,但老实说我不知道如何实际执行此操作。
有什么建议吗?
假设您有一个不同类型的用户可以签名的应用程序:公司,律师和客户.公司有很多律师; 律师有很多客户.当然,对于公司用户而言,观点与律师用户的观点不同; 这两者与客户端用户不同.
您将如何为三个不同的用户建模?我可以想到以下方法:
三种不同的模式有ForeignKey以User,每个都有自己的领域,如:
class Firm(models.Model):
user = models.ForeignKey(User)
class Lawyer(models.Model):
user = models.ForeignKey(User)
specialty = models.CharField(max_length=100)
class Client(models.Model)
user = modelsForeignKey(User)
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用两个来创建咨询作为单独的模型ForeignKeys:to Lawyerto to Client; 您还可以通过创建Resource带有ForeignKeyto 的模型来向咨询(例如文档或类似的东西)添加资源Consultation.
这种方法很难区分用户:例如,您如何知道a是否user是a Firm- 您需要多次查询数据库或将其分配Profile给通用User对象.
你也可以只添加Profile到User和包括Role,然后你道基础上的意见和认证user.get_profile().role.
你会如何处理这个问题?
我有一个数据源,最好用字典建模(它是key = value对的集合).对于特定的可视化目的,我需要提供类似列表的数据访问接口(除了常规字典界面),这意味着您应该能够执行以下操作:
data["mykey"] # returns the associated value
data[12][0] # returns the 13th key in the dictionary
data[12][1] # returns the 13th value in the dictionary
Run Code Online (Sandbox Code Playgroud)
我找不到合适的Facade实现 - 如果我将索引存储为字典键:
data[12] = ("mykey", "myval")
Run Code Online (Sandbox Code Playgroud)
我可以很容易地解决最后两种情况,但我放弃了做第一种情况的能力.如果我存储数据
data["mykey"] = "myval"
Run Code Online (Sandbox Code Playgroud)
在返回元素之前,我必须枚举临时列表中的所有键和值.
请注意,所有这些实现都假设我使用的是OrderedDict.
你会如何提供这两种接口?
如果你很好奇,那就是创建一个PyQt QAbstractTableModel,其中底层数据容器是一个字典.
谢谢.
请考虑以下代码
#include<algorithm>
#include<iostream>
#include<array>
void show(double x[2][2]) {
std::cout<<x[0][0]<<", "<<x[0][1]<<std::endl
<<x[1][0]<<", "<<x[1][1]<<std::endl;
}
int main() {
std::array<double, 4> y = {1, 2, 3, 4};
double x[2][2];
// it is safe to copy because x[2][2] consists of
// four contiguous blocks of memory in row-major order
std::copy(y.begin(), y.end(), &x[0][0]);
show(x); // this, obviously, works as expected
// but how can I cast y, or y.data(),
// or y.begin() to use the function foo?
// show(y);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用遗留库,其中有许多函数参数x[a][b].但是,我的代码依赖于线性数据表示(也就是说,我只使用C++"线性"容器,例如std::array<T, N> …
我为传统的Fortran库准备了一个C++接口.
遗留库中的一些子例程遵循一个丑陋但可用的状态代码约定来报告错误,我使用这样的状态代码从我的C++代码中抛出一个可读的异常:它工作得很好.
另一方面,有时遗留库调用STOP(终止程序).即使条件可以恢复,它也经常这样做.
我想从C++中捕获这个STOP,到目前为止我还没有成功.
以下代码很简单,但完全代表了手头的问题:
Fortran遗留库fmodule.f90:
module fmodule
use iso_c_binding
contains
subroutine fsub(x) bind(c, name="fsub")
real(c_double) x
if(x>=5) then
stop 'x >=5 : this kills the program'
else
print*, x
end if
end subroutine fsub
end module fmodule
Run Code Online (Sandbox Code Playgroud)
C++接口main.cpp:
#include<iostream>
// prototype for the external Fortran subroutine
extern "C" {
void fsub(double& x);
}
int main() {
double x;
while(std::cin >> x) {
fsub(x);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译行(GCC 4.8.1/OS …
我是一名“emacs/[c]make/ninja/clang/bash/linux/macOs”类型的 C++ 开发人员,不习惯 Visual Studio 等 IDE。我并不是说这两种方法更好或更差,只是我不知道如何使用 IDE。
我现在需要为一个强烈依赖 Visual Studio 的基于 Windows 的客户端开发一个 C++ 项目。
我的理解是,Visual Studio 2017 内置了对 CMake、Ninja 和 Google Test 的支持。然而,我无法想出一个工作流程,让我像往常一样简单地编码,将我的代码提交到 GitHub,并让我的客户简单地“刷新存储库并重建解决方案”。
我的问题是:克隆 C++/CMake GitHub 存储库并在 Visual Studio 中构建它的绝对最简单的方法是什么?
作为参考,我创建了以下存储库:
https://github.com/arrieta/visual-studio-cmake-test
这就是我使用正常方法构建它的方式:
$ git clone https://github.com/arrieta/visual-studio-cmake-test.git
$ cd visual-studio-cmake-test
$ mkdir build
$ cd build
$ cmake -G Ninja ../
$ ninja
$ ./app
Welcome to app v0.0.1
Hello, world!
Run Code Online (Sandbox Code Playgroud)
对于我的一生,我无法在 Visual Studio 中想出如此简单的方法(并不是说这个工具不好,只是我的无知)。我创建了一个“解决方案”,然后创建了一个“项目”,然后创建了一个“仓库”,我有太多的选择,以至于我不知所措。
任何帮助表示赞赏。
vre的解决方案用户vre提供了以下方法,该方法完全按照预期工作。这里,C:\>表示我的 Windows开发人员命令提示符。
C:\> …Run Code Online (Sandbox Code Playgroud) 我正在阅读boost文档中提供的智能指针编程技术.
在" 使用抽象类实现隐藏 "一节中,它们提供了一个很好的习惯用法来完全隐藏纯虚拟接口背后的实现.例如:
// Foo.hpp
#include <memory>
class Foo {
public:
virtual void Execute() const = 0;
protected:
~Foo() = default;
};
std::shared_ptr<const Foo> MakeFoo();
Run Code Online (Sandbox Code Playgroud)
和
// Foo.cpp
#include "Foo.hpp"
#include <iostream>
class FooImp final
: public Foo {
public:
FooImp() = default;
FooImp(const FooImp&) = delete;
FooImp& operator=(const FooImp&) = delete;
void Execute() const override {
std::cout << "Foo::Execute()" << std::endl;
}
};
std::shared_ptr<const Foo> MakeFoo() {
return std::make_shared<const FooImp>();
}
Run Code Online (Sandbox Code Playgroud)
关于受保护的非虚拟析构函数class Foo,该文档指出:
请注意上面示例中的受保护和非虚拟析构函数.客户端代码不能,也不需要删除指针 …