使用特征时,我很难理解Scala中的线性化顺序:
class A {
def foo() = "A"
}
trait B extends A {
override def foo() = "B" + super.foo()
}
trait C extends B {
override def foo() = "C" + super.foo()
}
trait D extends A {
override def foo() = "D" + super.foo()
}
object LinearizationPlayground {
def main(args: Array[String]) {
var d = new A with D with C with B;
println(d.foo) // CBDA????
}
}
Run Code Online (Sandbox Code Playgroud)
它打印CBDA但我无法弄清楚为什么.如何确定特征的顺序?
谢谢
下面是我的pom.xml文件.在第一行,我收到错误
解析生命周期处理指令时出错.
我需要帮助找到导致错误的原因.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>First</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用boost::asio::spawn协同程序重写项目.项目的某些部分无法更改.例如,存储协议库也是用boost::asio但没有协同程序编写的.
问题是如何转换yield_context为普通回调(boost::function对象或经典仿函数).
这就是我们在存储库API中的含义:
void async_request_data(uint64_t item_id, boost::function< void(Request_result *) > callback);
Run Code Online (Sandbox Code Playgroud)
正如我们从示例中所知,asio yield上下文可以像这样使用:
my_socket.async_read_some(boost::asio::buffer(data), yield);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,boost::asio::yield_context对象充当async_read_some的回调.我想传递一个yield对象作为第二个参数async_request_data,所以我可以以同步的方式使用它.
如何才能做到这一点?我认为可能通过一些代理对象,可能使用基于asio_handler_invoke的方法.但我很难看到如何做到这一点.
在Udacity完成AI课程时,我在转学习部分遇到了这个错误.这是似乎导致麻烦的代码:
import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
data_dir = 'filename'
# TODO: Define transforms for the training data and testing data
train_transforms= transforms.Compose([transforms.Resize((224,224)), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), transforms.ToTensor()])
test_transforms= transforms.Compose([transforms.Resize((224,224)), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), transforms.ToTensor()])
# Pass transforms in here, then run the next cell to see how the transforms look
train_data = datasets.ImageFolder(data_dir + '/train', transform=train_transforms)
test_data = datasets.ImageFolder(data_dir + '/test', …Run Code Online (Sandbox Code Playgroud) 我有一个QGraphicsScene我添加了一些实例QGraphicsItem.
我需要在各个视图中显示整个场景的特定部分.
为此,我想创建多个实例,QGraphicsView每个实例显示我的特定部分QGraphicsScene(不是类似的部分).
怎么做到呢?
QGraphicsScene mcpGraphicsScene = new QGraphicsScene(this);
QGraphicsRectItem * mcpGraphicsRect = mcpGraphicsScene->addRect(5,5,200,200);
QGraphicsLineItem * mcpGraphicsLine = mcpGraphicsScene->addLine(500,500,300,300);
QGraphicsView * mcpGraphicsView1 = new QGraphicsView(this);
mcpGraphicsView1->setScene(mcpGraphicsScene);
mcpGraphicsView1->setGeometry(260,20,311,500);
QGraphicsView * mcpGraphicsView2 = new QGraphicsView(this);
mcpGraphicsView2->setScene(mcpGraphicsScene);
mcpGraphicsView2->setGeometry(260,520,311,1061);
Run Code Online (Sandbox Code Playgroud) 我有一个看似像的numpy数组
a = ['blue' 'red' 'green']
Run Code Online (Sandbox Code Playgroud)
我希望它成为
b = ['blue', 'red', 'green']
Run Code Online (Sandbox Code Playgroud)
我试过了
b = a.split(' ')
Run Code Online (Sandbox Code Playgroud)
但它返回一个错误: 'numpy.ndarray' object has no attribute 'split'
我有一个庞大的数据集,我需要将图分成网格并计算每个网格方块内的点数。我正在遵循此处概述的方法:
下面是我的代码的精简版本:
import numpy as np
import matplotlib.pyplot as plt
x = [ 1.83259571, 1.76278254, 1.38753676, 1.6406095, 1.34390352, 1.23045712, 1.85877565, 1.26536371, 0.97738438]
y = [ 0.04363323, 0.05235988, 0.09599311, 0.10471976, 0.1134464, 0.13962634, 0.17453293, 0.20943951, 0.23561945]
gridx = np.linspace(min(x),max(x),11)
gridy = np.linspace(min(y),max(y),11)
grid, _, _ = np.histogram2d(x, y, bins=[gridx, gridy])
plt.figure()
plt.plot(x, y, 'ro')
plt.grid(True)
plt.figure()
plt.pcolormesh(gridx, gridy, grid)
plt.plot(x, y, 'ro')
plt.colorbar()
plt.show()
Run Code Online (Sandbox Code Playgroud)
出现问题的地方是网格将绘图的元素识别为点出现的位置,但其中一些元素中没有点;同样,当出现一些实际数据点时,网格不会将它们识别为实际上不存在。
可能是什么原因导致这个问题?另外,很抱歉没有附上剧情,我是新用户,我的声誉还不够高。
更新 这是生成 100 个随机点并尝试将它们绘制在二维直方图中的代码:
import numpy as np
import matplotlib.pyplot as plt
x = …Run Code Online (Sandbox Code Playgroud) 我在 Windows 7 上运行 rStudio v3.1.2。这台笔记本电脑是 64 位机器。
我正在参加 Coursera 提供的 JHU R 编程课程,但我在问题的第 1 部分中遇到了一个错误。我有一些错误处理函数,我没有考虑到这个例子,所以我真的只是想展示我绝对需要的东西。我包含这些消息的唯一原因是证明必须满足所有条件才能继续。
pollutantmean <- function(directory, pollutant, id=1:332) {
setwd("C:\\Users\\WR-eSUB\\specdata")
if(!isValidDirectory(directory)) {
stop("Invalid input given. Please specify valid directory to operate on.")
}
if(!isValidPollutant(pollutant)) {
stop("Invalid input given. Please specify valid pollutant (nitrate/sulfate).")
}
if(!isValidIdRange(id)) {
stop("Invalid input given. Please specify valid id range (1:332).")
}
sortedData = numeric()
for (i in id) {
thisFileName = paste(formatC(i, width = 3, flag = "0"), ".csv", sep="")
thisFileRead = …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试使用 Pandas、JSON 和 Python 3.4 从文本文件中打印数据。
当我在朋友的机器上使用 Python 2.7 运行代码时,它运行良好,但不适用于 Python 3.4。
这是我的代码:
import json
import pandas as pd
tweets_data_path = 'tweets.txt'
tweets_data = []
tweets_file = open(tweets_data_path, "r")
for line in tweets_file:
try:
tweet = json.loads(line)
tweets_data.append(tweet)
except:
continue
print (len(tweets_data))
tweets = pd.DataFrame()
tweets['text'] = map(lambda tweet: tweet['text'], tweets_data)
tweets['lang'] = map(lambda tweet: tweet['lang'], tweets_data)
tweets['country'] = map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data)
for i in range(len(tweets_data)):
print(tweets['text'][i])
Run Code Online (Sandbox Code Playgroud)
它不打印推文数据,而是打印数据的内存位置。例如
<map object at 0x04988050>
<map …Run Code Online (Sandbox Code Playgroud) 我编写了下面的代码,用于获取最大值和最小值作为我的MOOC分配的一部分.该程序不断地从用户接收输入,直到用户键入"完成".
输入'done'后,程序会给出最大值和最小值的结果.问题是最大值的结果总是正确的,但最小值的结果总是"无".
largest = None
smallest = None
while ( True ) :
inp = raw_input('Enter a number: ')
if inp == 'done' :
break
try:
inp = float(inp)
except:
print 'Invalid input'
continue
if inp is None or inp > largest:
largest = inp
if inp is None or inp < smallest:
smallest = inp
print largest, smallest
Run Code Online (Sandbox Code Playgroud)