我写了一个函数:
func Pic(dx, dy int) [][]uint8 {
type matrix [][]uint8
for i := 0; i < dx; i++ { // fills up the matrix with z's in their right places.
for j := 0; j < dy; j++ {
matrix[i][j] = Z(i,j)
}
}
return matrix
}
Run Code Online (Sandbox Code Playgroud)
这应该用每个 x 和 y 值的 z 值填充矩阵并返回它。由于我希望根据 Pic 函数的参数为矩阵设置不同的维度,因此我在第 2 行创建了一个切片。然后在我的 for 循环中填充矩阵。
运行此代码时出现错误:type matrix is not an expression对于该matrix[i][j] = Z(i,j)行。我究竟做错了什么?matrix[i][j] 应该计算为表达式吗?为什么要在那里放东西(它现在是空的/不存在的!)?
web.php:
Route::post('caption/{id}/delete', 'DetailController@deleteCaption');
Run Code Online (Sandbox Code Playgroud)
DetailController.php:
public function deleteCaption(Request $request, $id) {
$caption = Caption::findOrFail($id);
$caption->delete(); //doesn't delete permanently
return response(204);
}
Run Code Online (Sandbox Code Playgroud)
admin.blade.php:
<p value='{{$caption->id}}'>{{$caption->content}}</p>
<form action="caption/{{$caption->id}}/delete" method="post">
<button type="submit">Delete caption</button>
</form>
<form action="caption/{{$caption->id}}/approve" method="post">
<button type="submit">Accept caption</button>
</form>
Run Code Online (Sandbox Code Playgroud)
我想这样做,以便在删除图像后,用户被重定向回位于 localhost:8000/admin 的管理页面。
我怎样才能做到这一点?文档对我来说无法理解。
我正在尝试遵循有关 pipenv 和 virtualenv 的指南:http ://docs.python-guide.org/en/latest/dev/virtualenvs/ 。问题是,我在尝试时遇到了问题$ pipenv install requests(在我的情况下我认为应该是$python3 -m pipenv install requests因为只是pipenv返回未找到的命令。)
为什么权限被拒绝?
我是一个终端菜鸟,所以请耐心等待。
$ pip3 install --user pipenv
$ python3 -m pipenv
Usage: __main__.py [OPTIONS] COMMAND [ARGS]...
$ python3 -m pipenv install requests
Creating a Pipfile for this project...
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/__main__.py", line 4, in <module>
cli()
File "/Users/sahandzarrinkoub/Library/Python/3.6/lib/python/site-packages/pipenv/vendor/click/core.py", line …Run Code Online (Sandbox Code Playgroud) <!DOCTYPE html>
<html>
<head>
<style>
.dropdown-content a{
display: block;
background-color: blue;
}
</style>
</head>
<body>
<div class="dropdown-content">
<a>1</a>
<a>2</a>
</div>
<script>
window.onclick = function(event){
if(!event.target.matches('.dropdown-content')){
alert("foo");
}
};
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我试图alert(foo);仅在我们没有点击div正文中标签内的任何内容时才执行。不幸的是,无论我点击哪里,它都会执行。为什么?
我已经定义了一个自定义用户模型:
class User(AbstractUser):
REQUIRED_FIELDS = []
USERNAME_FIELD = 'email'
email = models.EmailField(
_('email address'),
max_length=150,
unique=True,
help_text=_('Required. 150 characters or fewer. Must be a valid email address.'),
error_messages={
'unique':_("A user with that email address already exists."),
},
)
Run Code Online (Sandbox Code Playgroud)
关键是要使用电子邮件地址而不是用户名.
我把它放在设置中:
# custom user model
AUTH_USER_MODEL = 'workoutcal.User'
Run Code Online (Sandbox Code Playgroud)
用户模型有效,但有一个问题.我无法创建超级用户:
(workout) n155-p250:workout sahandzarrinkoub$ ./manage.py createsuperuser
Email address: sahandz@kth.se
Password:
Password (again):
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/__init__.py", …Run Code Online (Sandbox Code Playgroud) 我想写入myFile.txt每次打开程序时调用的文件。如果之前运行的程序中存在具有该名称的文件,我希望它被覆盖。我该如何实现?我读过关于:
RandomAccessFile f = new RandomAccessFile("myFile.txt", "rw");
Run Code Online (Sandbox Code Playgroud)
但这会覆盖现有文件吗?文档没有告诉我是否有。请注意,完整删除旧文件很重要,如果我在当前运行中只覆盖其长度的一半,我不希望它的结尾仍然存在。
编辑: 通过测试,我发现“rw”不会删除现有文件,它只会覆盖文件顶部并保留其余文件。
我找到了满足我需求的替代解决方案:
RandomAccessFile file = new RandomAccessFile("myFile.txt", "rw");
file.setLength(0);
file.write(contentBytes);
Run Code Online (Sandbox Code Playgroud)
但如果可以以更短的方式完成它会很好。
我一直在尝试运行我在Github上发现的现有React项目.我有一些问题.首先,我需要将React更新到v 16.2.0才能React.Fragment正常工作.现在,完成更新后,我遇到了第二个问题:
./node_modules/react-dom/lib/ReactCompositeComponent.js
Module not found: Can't resolve 'react/lib/React' in '/Users/sahandzarrinkoub/Documents/Programming/DH2642/Sentify/repo/Sentify/node_modules/react-dom/lib'
Run Code Online (Sandbox Code Playgroud)
从控制台:
index.js:2178 ./node_modules/react-dom/lib/instantiateReactComponent.js
Module not found: Can't resolve 'react/lib/getNextDebugID' in '/Users/sahandzarrinkoub/Documents/Programming/DH2642/Sentify/repo/Sentify/node_modules/react-dom/lib'
__stack_frame_overlay_proxy_console__ @ index.js:2178
handleErrors @ webpackHotDevClient.js:178
./node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage @ webpackHotDevClient.js:211
./node_modules/sockjs-client/lib/event/eventtarget.js.EventTarget.dispatchEvent @ eventtarget.js:51
(anonymous) @ main.js:274
./node_modules/sockjs-client/lib/main.js.SockJS._transportMessage @ main.js:272
./node_modules/sockjs-client/lib/event/emitter.js.EventEmitter.emit @ emitter.js:50
WebSocketTransport.ws.onmessage @ websocket.js:35
Run Code Online (Sandbox Code Playgroud)
附加信息:
这是项目主文件夹.这是package.json文件:
{
"name": "Sentify",
"version": "0.1.0",
"private": true,
"dependencies": {
"ajv": "^5.5.2",
"cors": "^2.8.4",
"dom": "0.0.3",
"jquery": "^3.3.1",
"jsdom": "^11.7.0",
"mapbox-gl": "^0.44.1",
"material-ui": "^0.20.0",
"react": "^16.2.0",
"react-d3-basic": "^1.6.11",
"react-d3-core": …Run Code Online (Sandbox Code Playgroud) 以下代码是 JUnit 测试函数,执行失败。
List<KGramPostingsEntry> a = new ArrayList<KGramPostingsEntry>();
List<KGramPostingsEntry> b = new ArrayList<KGramPostingsEntry>();
KGramPostingsEntry entry = new KGramPostingsEntry(1);
a.add(entry);
entry = new KGramPostingsEntry(1);
b.add(entry);
assertTrue(a.containsAll(b));
Run Code Online (Sandbox Code Playgroud)
它使用以下KGramPostingsEntry类:
package ir;
public class KGramPostingsEntry {
int tokenID;
public KGramPostingsEntry(int tokenID) {
this.tokenID = tokenID;
}
public KGramPostingsEntry(KGramPostingsEntry other) {
this.tokenID = other.tokenID;
}
public String toString() {
return tokenID + "";
}
public boolean equals(KGramPostingsEntry other) {
if(other.tokenID == this.tokenID) {
return true;
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
equals()正如您所看到的,类中有一个函数可以比较tokenID …
我已经看到在Java 8中,可以像这样定义一个比较器:
Comparator c = (Computer c1, Computer c2) -> c1.getAge().compareTo(c2.getAge());
Run Code Online (Sandbox Code Playgroud)
这相当于:
Comparator d = new Comparator<Computer> () {
@Override
public int compare(Computer c1, Computer c2){
return c1.getAge().compareTo(c2.getAge());
}
};
Run Code Online (Sandbox Code Playgroud)
我想了解这是如何工作的.在第二个例子中,它非常简单:Comparator使用一个方法创建一个对象,该方法compare使用属性中的compareTo方法执行比较.当我们这样做时,我们只需调用此方法:ageComputer
Computer comp1 = new Computer(10);
Computer comp2 = new Computer(11);
d.compare(comp1, comp2); // -1
Run Code Online (Sandbox Code Playgroud)
但是在第一个例子中,当使用lambda时会发生什么?在我看来,我们正在设置Comparator等于执行比较的方法.但这不可能,因为Comparator对象是一个有方法的对象compare.我已经了解到lambdas可以与功能接口一起使用(只有一种方法的接口).但Comparator它不是一个功能界面(除了compare!之外还有许多其他方法).那么Java解释器如何知道它是compare我们正在实现的方法呢?
这是可停止线程的实现以及使用它的尝试:
import threading
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self, target, kwargs):
super(StoppableThread, self).__init__(target, kwargs)
self._stop_event = threading.Event()
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.it_set()
def func(s):
print(s)
t = StoppableThread(target = func, kwargs={"s":"Hi"})
t.start()
Run Code Online (Sandbox Code Playgroud)
此代码会生成错误:
Traceback (most recent call last):
File "test.py", line 19, in <module>
t = StoppableThread(target = func)
File "test.py", line 7, in __init__
super(StoppableThread, self).__init__(target)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 780, in __init__ …Run Code Online (Sandbox Code Playgroud)