假设我想创建一个对话框,我的主程序的子代:
from PyQt4 import QtGui, QtCore
class WizardJournal(QtGui.QDialog):
def __init__(self, parent):
super(WizardJournal, self).__init__(parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.wizard = QtGui.QWidget()
self.ok_button = QtGui.QPushButton("OK", self)
self.vbox_global = QtGui.QVBoxLayout(self)
self.vbox_global.addWidget(self.ok_button)
self.paret.wizard.setLayout(self.vbox_global)
self.parent.wizard.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
parent = QtGui.QWidget()
obj = WizardJournal(parent)
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
该对话框将由我的主程序打开和关闭.有关内存消耗的更好方法:
self.ok_button = QtGui.QPushButton("OK", self)self.ok_button = QtGui.QPushButton("OK")基本上,我想知道在创建窗口小部件时是否应该提到父窗口小部件.当我关闭此对话框时,如果在创建父窗口小部件时没有提及父窗口小部件,是否会从内存中释放确定按钮?
我通过pew使用virtualenv(我认为这是一个很棒的工具),但我发现了一些奇怪的东西.
我安装了scipy系统端:
7,7 MiB [##########] /sparse
5,1 MiB [###### ] /special
5,1 MiB [###### ] /stats
5,0 MiB [###### ] /linalg
3,5 MiB [#### ] /spatial
3,0 MiB [### ] /optimize
2,5 MiB [### ] /signal
2,3 MiB [### ] /interpolate
2,3 MiB [## ] /misc
2,2 MiB [## ] /io
1,5 MiB [## ] /integrate
1,3 MiB [# ] /ndimage
1,0 MiB [# ] /fftpack
744,0 KiB [ ] /cluster
512,0 KiB [ ] /odr
464,0 KiB [ ] …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的PyQt程序移植到Mac OS.我是在Linux上开发的.
PyQt(4)安装在2台计算机上,使用python 3.4.一切似乎都有效.该程序在Linux计算机上运行良好.我只是将它转移到mac计算机,并试图运行它.它运行,除了一件事:
我有一个菜单项(在菜单栏中),用于程序的设置:
# Action to show a settings window
self.settingsAction = QtGui.QAction('Settings', self)
self.settingsAction.triggered.connect(lambda: Settings(self))
...Some code...
# Menu entry for the settings
self.menubar.addAction(self.settingsAction)
Run Code Online (Sandbox Code Playgroud)
在Linux上,"设置"条目完美地显示在菜单栏中,并打开设置窗口.但是,在Mac OS上,不显示"设置"条目.根本不显示.所有其他条目(文件,编辑,视图等)都正确显示,但不是"设置".并且没有例外.
我被困在这里,我从未使用过Mac OS,所以我甚至不知道从哪里开始调试.
你有什么建议吗?
编辑:
我也试过了
#显示设置窗口的操作self.settingsAction = QtGui.QAction('Preferences',self)self.settingsAction.triggered.connect(lambda:Settings(self))
...Some code...
# Menu entry for the settings
self.menubar.addAction(self.settingsAction)
Run Code Online (Sandbox Code Playgroud) 我有一组积分。基本上,我有 P = f(t)。
比方说,我有 50 次测量。50 个 P 值,时间函数。这些价值观遵循既定的规律。
我要做的就是找到规律中参数的值,仅此而已。基本上,我必须用最佳曲线拟合点。这是法律:
P = V.t - ((V - W)(1 - exp(-k.t)) / k)
Run Code Online (Sandbox Code Playgroud)
我需要做的是找到 V、W 和 k 的数值。我有 t 和 P。你知道怎么做吗?
编辑:
这是我想要获得的截图:

在图片上:
这就是我在 reptilicus 的帮助下获得的:
http://i.imgur.com/f59Eo29.png
import numpy as np
from scipy.optimize import curve_fit
from matplotlib.pyplot import *
import xlrd
def myFunc(t, V, W, k):
y = V * t - ((V - W) * (1 - np.exp(-k * t)) …Run Code Online (Sandbox Code Playgroud) 关于正则表达式的另一个问题。
我正在尝试匹配除“*”之外的所有特殊字符。
因此,如果我将我的正则表达式与:
John%%%* dadidou
Run Code Online (Sandbox Code Playgroud)
我应该得到:
John* dadidou
Run Code Online (Sandbox Code Playgroud)
此处:如何与正则表达式匹配 PHP 中除“-”以外的所有特殊字符?
要使用的已接受答案建议(如果我想排除“-”):
[^\w-]
Run Code Online (Sandbox Code Playgroud)
但这不是说:“不是特殊字符,不是 -”,这有点多余吗?
我有三个代表长度的数字。每个数字都是对象在 x、y 和 z 轴上的长度。我想要表示的对象(可能)是一个变形的球体。
如何通过指定这 3 个长度来 3D 绘制该对象?
我想从这里开始:
例如,对于这样的事情(我只是放大了图片):
我尝试使用以下代码(来自Python 中的 Ellipsoid 创建)并尝试一下 x、y 和 z 的定义:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
phi = np.linspace(0,2*np.pi, 256).reshape(256, 1) # the angle of the projection in the xy-plane
theta = np.linspace(0, np.pi, 256).reshape(-1, 256) # the angle from the polar axis, ie the polar angle
radius = 4
# Transformation formulae for a spherical coordinate system.
x = radius*np.sin(theta)*np.cos(phi)
y …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我在此处找到的“带处理的受控圆包装”算法移植到 Python:
现在我的目标只是让它工作,然后我根据自己的需要调整它。这个问题不是关于做圆形包装的最佳方法。
到目前为止,这是我所拥有的:
#!/usr/bin/python
# coding: utf-8
import numpy as np
import matplotlib.pyplot as plt
from random import uniform
class Ball:
def __init__(self, x, y, radius):
self.r = radius
self.acceleration = np.array([0, 0])
self.velocity = np.array([uniform(0, 1),
uniform(0, 1)])
self.position = np.array([x, y])
@property
def x(self):
return self.position[0]
@property
def y(self):
return self.position[1]
def applyForce(self, force):
self.acceleration = np.add(self.acceleration, force)
def update(self):
self.velocity = np.add(self.velocity, self.acceleration)
self.position = np.add(self.position, self.velocity)
self.acceleration *= 0
class Pack:
def __init__(self, radius, …Run Code Online (Sandbox Code Playgroud) 让我们考虑以下两种语法变体:
class Foo:
x: int
def __init__(self, an_int: int):
self.x = an_int
Run Code Online (Sandbox Code Playgroud)
和
class Foo:
def __init__(self, an_int: int):
self.x = an_int
Run Code Online (Sandbox Code Playgroud)
显然,以下代码在两种情况下都会引发 mypy 错误(这是预期的):
obj = Foo(3)
obj.x.title() # this is a str operation
Run Code Online (Sandbox Code Playgroud)
但我真的想强制执行契约:我想明确说明 x 是每个Foo对象的实例变量。那么应该首选哪种语法,为什么?
我正在比较 Python 3 中斐波那契例程的两个版本:
import functools
@functools.lru_cache()
def fibonacci_rec(target: int) -> int:
if target < 2:
return target
res = fibonacci_rec(target - 1) + fibonacci_rec(target - 2)
return res
def fibonacci_it(target: int) -> int:
if target < 2:
return target
n_1 = 2
n_2 = 1
for n in range(3, target):
new = n_2 + n_1
n_2 = n_1
n_1 = new
return n_1
Run Code Online (Sandbox Code Playgroud)
第一个版本是递归的,带有记忆功能(感谢lru_cache)。第二个是简单的迭代。
然后我对这两个版本进行了基准测试,结果让我有点惊讶:
In [5]: %timeit fibonacci_rec(1000)
82.7 ns ± 2.94 ns per loop (mean …Run Code Online (Sandbox Code Playgroud) 我正在学习 Rust,并试图解决代码挑战的出现(2015 年第 9 天)。
我创建了一种情况,最终得到一个具有该类型的变量Vec<&&str>(注意双“&”,这不是拼写错误)。我现在想知道这种类型是否与Vec<&str>. 我不知道对某事物的引用是否有意义。我知道我可以通过使用Stringforfrom和to变量来避免这种情况。我在问我是否Vec<&&str> == Vec<&str>以及是否应该尝试避免Vec<&&str>。
这是触发这个问题的代码:
use itertools::Itertools
use std::collections::{HashSet};
fn main() {
let contents = fs::read_to_string("input.txt").unwrap();
let mut vertices: HashSet<&str> = HashSet::new();
for line in contents.lines() {
let data: Vec<&str> = line.split(" ").collect();
let from = data[0];
let to = data[2];
vertices.insert(from);
vertices.insert(to);
}
// `Vec<&&str>` originates from here
let permutations_iter = vertices.iter().permutations(vertices.len());
for perm in permutations_iter {
let length_trip = …Run Code Online (Sandbox Code Playgroud) 来自这里的Python。
我想知道为什么 BTreeMap 是可散列的。我并不惊讶 Hashmap 不是,但我不明白为什么 BTreeMap 是。
例如,我可以这样做:
let mut seen_comb: HashSet<BTreeMap<u8, u8>> = HashSet::new();
seen_comb.insert(BTreeMap::new());
Run Code Online (Sandbox Code Playgroud)
但我不能这样做:
let mut seen: HashSet<HashMap<u8, u8>> = HashSet::new();
seen.insert(HashMap::new());
Run Code Online (Sandbox Code Playgroud)
因为我得到:
error[E0599]: the method `insert` exists for struct `HashSet<HashMap<u8, u8>>`, but its trait bounds were not satisfied
--> src/main.rs:14:10
|
14 | seen.insert(HashMap::new());
| ^^^^^^ method cannot be called on `HashSet<HashMap<u8, u8>>` due to unsatisfied trait bounds
|
::: /home/djipey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:209:1
|
209 | pub struct HashMap<K, V, S = RandomState> {
| ----------------------------------------- doesn't …Run Code Online (Sandbox Code Playgroud) python ×8
numpy ×2
pyqt ×2
rust ×2
circle-pack ×1
hashmap ×1
linux ×1
macos ×1
math ×1
matplotlib ×1
memory ×1
mypy ×1
porting ×1
reference ×1
regex ×1
scipy ×1
type-hinting ×1
virtualenv ×1