我想知道是否有办法做到以下几点:
class Test_Vector_test(unittest.TestCase):
def test_add(self):
vector1 = Vector(2,2,2)
scalar = 1
self.assertRaises(NotImplementedError, vector1+scalar)
Run Code Online (Sandbox Code Playgroud)
此测试失败,出现以下错误代码:
Traceback (most recent call last):
File "/Users/sahandz/Documents/School/Programmeringsteknik och Matlab/dd1315pylab3-master/test/test_Vector.py", line 14, in test_add
self.assertRaises(NotImplementedError, vector1+scalar)
File "/Users/sahandz/Documents/School/Programmeringsteknik och Matlab/dd1315pylab3-master/lab/Vector.py", line 17, in __add__
raise NotImplementedError
NotImplementedError
----------------------------------------------------------------------
Ran 3 tests in 0.007s
FAILED (errors=1)
Run Code Online (Sandbox Code Playgroud)
即使你可以看到它实际上引起了错误.如何在不使用方法的情况下达到所需的功能(检查是否vector1+scalar引发NotImplementedError)?vector1.__add__
在Python 2中:
>>> class A:
... pass
...
>>> A.__new__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: class A has no attribute '__new__'
>>> class A(object):
... pass
...
>>> A.__new__
<built-in method __new__ of type object at 0x1062fe2a0>
Run Code Online (Sandbox Code Playgroud)
结论:object包含__new__和子类继承该方法.
在Python 3中:
>>> class A:
... pass
...
>>> A.__new__
<built-in method __new__ of type object at 0x100229940>
Run Code Online (Sandbox Code Playgroud)
__new__是我们类中定义的方法,没有任何继承.这是如何运作的?__new__"来自哪里"?
我正在遵循这个关于Python的virtualenv的指南,并遇到了一个小问题:
Sahands-MBP:empty sahandzarrinkoub$ source /usr/local/bin/virtualenvwrapper.sh
/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
Run Code Online (Sandbox Code Playgroud)
打印输出非常有用.它说我需要检查virtualenvwrapper是否已安装VIRTUALENVWRAPPER_PYTHON=/usr/bin/python并且PATH设置正确.唯一的问题是,我不知道这些东西是什么意思.所以我的问题是:
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python.PATH在这种情况下,什么是正确的设置?这是我的模特:
class Workout(models.Model):
datetime = models.DateTimeField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
lifts = fields.LiftsField(null=True)
cardios = fields.CardiosField(null=True)
def __str__(self):
return str(self.datetime)+" "+self.user.email
__repr__ = __str__
Run Code Online (Sandbox Code Playgroud)
而我正试图从django shell做到这一点:
(workout) Sahands-MBP:workout sahandzarrinkoub$ shell
Python 3.6.2 (default, Jul 17 2017, 16:44:45)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from workoutcal.models import Workout
>>> Workout.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/query.py", line 226, in __repr__ …Run Code Online (Sandbox Code Playgroud) 我已经在Django上开发了自己的网站一段时间,今天我开始学习如何部署它。我将此添加到我的settings.py中:
SECURE_SSL_REDIRECT = True,
Run Code Online (Sandbox Code Playgroud)
这导致开发服务器停止正常工作,并显示以下错误消息:
[13/Jan/2018 16:56:49] code 400, message Bad request syntax ('\x16\x03\x01\x00À\x01\x00\x00¼\x03\x03ßà\x84¼+Jnßþn-ñ\x88ý©vAþK\x83¤²êT\x86\x0b.\x8em\x0b:â\x00\x00\x1cÚÚÀ+À/À,À0̨̩À\x13À\x14\x00\x9c\x00\x9d\x00/\x005\x00')
[13/Jan/2018 16:56:49] code 400, message Bad HTTP/0.9 request type ('\x16\x03\x01\x00À\x01\x00\x00¼\x03\x03\x87')
[13/Jan/2018 16:56:49] You're accessing the development server over HTTPS, but it only supports HTTP.
[13/Jan/2018 16:56:49] You're accessing the development server over HTTPS, but it only supports HTTP.
[13/Jan/2018 16:56:49] code 400, message Bad request version ('JJÀ+À/À,À0̨̩À\x13À\x14\x00\x9c\x00\x9d\x00/\x005\x00')
[13/Jan/2018 16:56:49] You're accessing the development server over HTTPS, but it only supports HTTP.
Run Code Online (Sandbox Code Playgroud)
为什么我的服务器停止正常工作?
请注意,当我将设置更改回时SECURE_SSL_REDIRECT = False …
我正在尝试创建一个类,我可以在二进制搜索的帮助下快速将对象插入到类列表中.
这是我的班级和内部班级:
public class PostingsList implements Iterator<PostingsEntry>{
/** The postings list */
private ArrayList<PostingsEntry> list = new ArrayList<PostingsEntry>();
class PostingsEntryComparator implements Comparator{
@Override
public int compare(PostingsEntry pA, PostingsEntry pB){
if(pA.docID < pB.docID){
return -1;
}
else if(pA.docID == pB.docID){
return 0;
}
else{
return 1;
}
}
}
public void add(PostingsEntry newPostingsEntry){
//put in the right place
PostingsEntryComparator pc = new PostingsEntryComparator();
int res = Collections.binarySearch(list, newPostingsEntry, pc);
if(res < 0){
list.add(-res-1, newPostingsEntry);
}
else{
System.out.println("already exists");
}
}
} …Run Code Online (Sandbox Code Playgroud) 在此提交消息中,它说2个文件已被更改。
$ git commit -m "fixed .gitignore"
[master c30afbe] fixed .gitignore
Committer: Sahand Zarrinkoub <sahandzarrinkoub@n133-p41.eduroam.kth.se>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend …Run Code Online (Sandbox Code Playgroud) private void addCompoundsFrom(Verse verse) {
Optional<List<Compound>> compounds = Optional.of(verse.getCompounds());
if (compounds.isPresent()) {
for (Compound compound : compounds.get()) {
addCompoundsFrom(compound);
}
}
}
Run Code Online (Sandbox Code Playgroud)
IntelliJ检查器告诉我,if语句始终为真。它怎么知道呢?这是Compounds类:
public class Compounds extends PositionalIds {
@XmlElement(name = "verse")
private List<Verse> verses;
public List<Verse> getVerses() {
return verses;
}
}
@XmlTransient
public abstract class PositionalIds {
@XmlAttribute(name = "start")
private String startId;
@XmlAttribute(name = "end")
private String endId;
public String getStartId() {
return startId;
}
public String getEndId() {
return endId;
}
}
Run Code Online (Sandbox Code Playgroud)
和Verse类:
public class Verse …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序来检查给定的密码是否至少包含1个大写字母和1个小写字母.我可以很容易地检查这样:
#Password needs to have at least 1 upper case letter
has_upper_letter = False
for letter in password:
if letter.isupper():
has_upper_letter = True
break
if not has_upper_letter:
raise forms.ValidationError(_("The password needs to have at least 1 upper case letter"))
#Password needs to have at least 1 lower case letter
has_lower_letter = False
for letter in password:
if letter.islower():
has_lower_letter = True
break
if not has_lower_letter:
raise forms.ValidationError(_("The password needs to have at least 1 lower case letter"))
Run Code Online (Sandbox Code Playgroud)
但是这段代码是重复的.我想只在一次检查某种字母的字母时编写逻辑.我的想法是编写一个以isupper()或 …
我了解我使用的是react.js版本15.6.2。我知道这是因为在我的代码中这样做:
console.log(React.version);
Run Code Online (Sandbox Code Playgroud)
导致此控制台输出:
15.6.2
Run Code Online (Sandbox Code Playgroud)
我想升级到React@^v16.2.0。我尝试这样做:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo n latest
Run Code Online (Sandbox Code Playgroud)
但是什么都没有改变。我在控制台中得到了相同版本的输出。如何升级节点?
编辑:
在这种情况下,我位于具有以下层次结构的项目文件夹中:
node_modules似乎包含react安装,因为它有一个react文件夹,其中package.json包含一个包含版本号的文件15.6.2。
我已经尝试过npm update --save react和npm update -g react。没有工作。发生相同的事情,并且可以在中找到相同的版本号node_modules/react/package.json。npm install在加入之前,我什至尝试再次跑步npm start。还有其他建议吗?