我正在使用以下代码收集实例:
class Hand():
instances = []
def __init__(self):
Hand.instances.append(self)
self.value = 5
def do_something(self, a):
self.value = self.value * a
class Foo():
def __init__(self):
pass
def insty(self):
self.hand1 = Hand()
self.hand2 = Hand()
foo = Foo()
foo.insty()
print Hand.instances
for hand in Hand.instances:
print "how do I print the instance name?"
Run Code Online (Sandbox Code Playgroud)
最后一行只是学习如何访问实例名称的方法,因此我可以按顺序在每个实例上调用'do_something'方法.
如何访问Hand的每个实例的实例名称?
我有以下代码:
client = MongoClient()
data_base = client.hkpr_restore
agents_collection = data_base.agents
agent_ids = agents_collection.find({},{"_id":1})
Run Code Online (Sandbox Code Playgroud)
这给了我一个结果:
{u'_id': ObjectId('553020a8bf2e4e7a438b46d9')}
{u'_id': ObjectId('553020a8bf2e4e7a438b46da')}
{u'_id': ObjectId('553020a8bf2e4e7a438b46db')}
Run Code Online (Sandbox Code Playgroud)
我如何获取 ObjectId,以便可以使用每个 ID 来搜索另一个集合?
如果我有一个功能:
def foo(self, a, b):
c = a + b
return c
Run Code Online (Sandbox Code Playgroud)
如何在不更改函数中的c的情况下调用foo?所以让我说我在另一个函数中调用foo:
def bar(self):
z = self.foo(2, 4)
return (z)
Run Code Online (Sandbox Code Playgroud)
然后我想在一个单独的函数中再次调用foo,但是我想要从'bar'调用时间c.
def baz(self):
self.foo(?, ?) # trying to just get c, without any changes.
Run Code Online (Sandbox Code Playgroud)
基本上,我试图在课堂上保留一个帐户,以便其他班级可以访问同一个帐户; 只是一个简单的平衡,添加和减去钱.
谢谢.
如果没有立即明显,我通过网络教程学习新手.
我试图通过不同长度的词典来循环,并将结果放在一个表格中.我想把"没什么"放到可能有空值的表中.
我正在尝试以下代码:
import os
os.system("clear")
dict1 = {'foo': {0:'a', 1:'b', 3:'c'}, 'bar': {0:'l', 1:'m', 2:'n'}, 'baz': {0:'x', 1:'y'} }
list1 = []
list2 = []
list3 = []
for thing in dict1:
list1.append(dict1[thing][0])
print list1
for thing in dict1:
list2.append(dict1[thing][1])
print list2
for thing in dict1:
if dict1[thing][2] == None:
list3.append('Nothing')
else:
list3.append(dict1[thing][2])
Run Code Online (Sandbox Code Playgroud)
我得到以下输出/错误:
['x', 'a', 'l']
['y', 'b', 'm']
Traceback (most recent call last):
File "county.py", line 19, in <module>
if dict1[thing][2] == None:
KeyError: 2
Run Code Online (Sandbox Code Playgroud)
如何在dict中引用空值?
谢谢!
collection.py
import sys
import os
import pymongo
from pymongo import MongoClient
class Collection():
"""returns a collection curser from mongodb"""
client = MongoClient()
def __init__(self, db, collection_name):
self.db = db
self.collection_name = collection_name
def getCollection(self):
data_base = getattr(self.client, self.db)
collObject = getattr(data_base, self.collection_name)
return collObject
Run Code Online (Sandbox Code Playgroud)
main.py
import sys
import os
import collection
def main():
pass
if __name__ == '__main__':
print"Begin Main"
agents = Collection('hkpr_restore','agents')
print "agents is" , agents
Run Code Online (Sandbox Code Playgroud)
这些文件位于同一目录中.main.py然而,当我跑步时,我收到一个错误:
Begin Main
Traceback (most recent call last):
File "main.py", …Run Code Online (Sandbox Code Playgroud) 我正在创建一个字符串,然后我在一个查询mongodb集合的方法中使用它.最终日期将来自用户输入.这是相关的代码和字符串:
import pymongo
from pymongo import MongoClient
from datetime import datetime
import time
import datetime
start_yr = 2015
start_mnth = 2
start_day = 1
end_yr = 2015
end_mnth = 2
end_day = 28
# this is the line called in the error
created_at_string = { "created_at": {"$gte" : datetime(start_yr, start_mnth, start_day),"$lt" : datetime(end_yr, end_mnth, end_day)}}
Run Code Online (Sandbox Code Playgroud)
这个想法将created_at_string用作更复杂的查询方法中的参数.
我越来越:
Traceback (most recent call last):
File "main.py", line 218, in <module>
program.runProgram()
File "main.py", line 61, in runProgram
report.RcreateReport()
File "/filepath/report.py", …Run Code Online (Sandbox Code Playgroud) html页面上的此代码以一行形式出现.为什么反斜杠没有工作?
<?php
$text1 = "I don't understand how this works! \nHow the hell do I get this thing to use \nmulti lines in PHP???!!!";
echo $text1;
?>
Run Code Online (Sandbox Code Playgroud) 我正在从一本书中学习C#,我正在扩展一个例子,以便更好地理解语法.
我正在尝试使用以下代码循环遍历一组对象并仅选择某些对象,以便我可以将它们加载到一个单独的数组中.我现在正在努力解决这个问题:
if (animalCollection[i].Equals(Chicken))
Run Code Online (Sandbox Code Playgroud)
这是Program.cs的完整代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ch11Ex02
{
class Program
{
static void Main(string[] args)
{
Animals animalCollection = new Animals();
animalCollection.Add(new Cow("Jack"));
animalCollection.Add(new Chicken("Vera"));
animalCollection.Add(new Chicken("Sally"));
Animal[] birds = new Animal[2];
for (int i = 0; i < animalCollection.Count; i++)
{
if (animalCollection[i].Equals(Chicken))
birds[i] = animalCollection[i];
}
foreach (Animal myAnimal in animalCollection)
{
myAnimal.Feed();
}
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标是只将对象类型Chicken加载到一个名为birds的新数组中.
这是Animal类的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ch11Ex02 …Run Code Online (Sandbox Code Playgroud) 我刚开始使用Python(在我的带下有大量的VBA)所以我正在玩一些简单的语法.
我写了这个简单的for循环,但输出似乎错了.我不能让变量'c'递增.
这是我的代码:
class Card:
def county(self):
for n in range(0,13):
c = 0
c = c + 1
print c
pick_card = Card()
print pick_card.county()
Run Code Online (Sandbox Code Playgroud)
输出只是'1'打印13次,然后是"无"
我究竟做错了什么?
我有一个名为`index.php'的登陆页面,其格式如下:
<form action="auto_mail.php" method="post">
<input id="signup" class="span8" type="text" placeholder="Your email" name="signup">
<input type="submit">
<?php
if (isset($_SESSION['got_it']))
{echo "<b>You're all signed up!</b>}
?></form>
Run Code Online (Sandbox Code Playgroud)
在auto_mail.php我有的文件中:
// code scrubbing user input...
$user_email = $_POST['signup'];
session_start();
$_SESSION['got_it'] = '1';
// code sending me an email when someone signs up.
echo <<<EOD
</b>
<meta http-equiv="refresh" content="0, url=index.php">
</div>
</body>
</html>
EOD;
?>
Run Code Online (Sandbox Code Playgroud)
我已经查看了其他一些SO问题(使用$ _SESSION来传输数据),但这不是我想要的.
我想要的只是让用户在输入有效的电子邮件时看到"你们都已注册"; 电子邮件确认电子邮件在后台发送.这段代码感觉笨拙和尴尬.它还会auto_mail.php短暂闪烁页面.
我试图设置<form action="index.php"...,但它不起作用,因为我设置了auto_mail.php你不能直接访问它.
如何使用代码auto_mail.php,这将检查一个有效的电子邮件地址,并发送电子邮件确认,没有既处理$_POST和$_SESSION,或至少使用它们更好?
我通过在线教程学习n00b.我在名为"response.php"的"服务器端"测试文件中有以下代码:
<?php
if (isset($_POST))
{
$answer = $_POST;
echo json_encode($answer);
} else {
echo json_encode(array('status' => false, 'msg' => 'no good'));
}
?>
Run Code Online (Sandbox Code Playgroud)
echo将数据返回到.ajaxjquery回调时,我是否总是必须使用?是否有其他方法将数据发送回客户端?
我有一个整洁的小桌子,使用tabulate:
from tabulate import tabulate
outputList = dictOfOutputs.items()
table = outputList
print tabulate(table)
Run Code Online (Sandbox Code Playgroud)
如何将其打印到文本文件?