实现具有多个属性的类时(如下面的玩具示例),处理散列的最佳方法是什么?
我猜测,__eq__而__hash__应该是一致的,但如何实现适当的哈希函数,能够处理所有属性?
class AClass:
def __init__(self):
self.a = None
self.b = None
def __eq__(self, other):
return other and self.a == other.a and self.b == other.b
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash((self.a, self.b))
Run Code Online (Sandbox Code Playgroud)
我读到这个问题,元组是可以清洗的,所以我想知道上面的例子是否合情合理.是吗?
我正在尝试设置我的(iframe)Facebook应用程序以使用OAuth进行身份验证.我使用了Facebook 的python-sdk,但我对结果并不满意.
问题是,当我将从未访问过我的应用程序的用户重定向到登录页面时,我的iframe会显示一个丑陋的中间页面,例如以下页面:

如果用户点击"转到Facebook.com"链接,则会将其重定向到标准的"请求权限"页面.

有没有办法避免第一页并引导用户直接进入第二页?
对于尚未向我的应用程序授予任何权限的用户,首次访问时会出现此问题.
登录代码基于Python SDK中的OAuth示例:
class LoginHandler(BaseHandler):
def get(self):
verification_code = self.request.get("code")
args = dict(client_id=FACEBOOK_APP_ID, redirect_uri=self.request.path_url)
if self.request.get("code"):
args["client_secret"] = FACEBOOK_APP_SECRET
args["code"] = self.request.get("code")
raw_response = urllib.urlopen(
"https://graph.facebook.com/oauth/access_token?" +
urllib.urlencode(args)).read()
logging.debug("access_token raw response " + raw_response)
response = cgi.parse_qs(raw_response)
access_token = response["access_token"][-1]
# Download the user profile and cache a local instance of the
# basic profile info
graph = facebook.GraphAPI(access_token)
profile = graph.get_object("me")
user = User.get_by_key_name(profile["id"])
if not user:
user = User(key_name=str(profile["id"]),
id=str(profile["id"]),
name=profile["name"], …Run Code Online (Sandbox Code Playgroud) 我有一个包含复杂类型的WSDL,如下所示:
<xsd:complexType name="string_array">
<xsd:complexContent>
<xsd:restriction base="SOAP-ENC:Array">
<xsd:attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="xsd:string[]"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
Run Code Online (Sandbox Code Playgroud)
我决定对soap客户端使用zeep,并希望将该类型用作WSDL中引用的其他方法之一的参数.我似乎无法弄清楚如何使用这种类型.当我通过看文件关于如何使用WSDL中引用的某些数据结构,它说,使用client.get_type()方法,所以我做了以下内容:
wsdl = "https://wsdl.location.com/?wsdl"
client = Client(wsdl=wsdl)
string_array = client.get_type('tns:string_array')
string_array('some value')
client.service.method(string_array)
Run Code Online (Sandbox Code Playgroud)
这给出了一个错误TypeError: argument of type 'string_array' is not iterable.我也试过很多变化,并尝试使用这样的字典:
client.service.method(param_name=['some value'])
Run Code Online (Sandbox Code Playgroud)
这给出了错误
ValueError: Error while create XML for complexType '{https://wsdl.location.com/?wsdl}string_array': Expected instance of type <class 'zeep.objects.string_array'>, received <class 'str'> instead.`
Run Code Online (Sandbox Code Playgroud)
如果有人知道如何使用带有zeep的WSDL中的上述类型,我将不胜感激.谢谢.
我们有一个在JBoss 5.1上运行的Java应用程序,在某些情况下,我们需要防止在JDBCException某些基础方法抛出事件时关闭事务.
我们有一个类似于下面的EJB方法
@PersistenceContext(unitName = "bar")
public EntityManager em;
public Object foo() {
try {
insert(stuff);
return stuff;
} (catch PersistenceException p) {
Object t = load(id);
if (t != null) {
find(t);
return t;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果insert由于PersistenceException(JDBCException由于约束违规导致包装)而失败,我们希望load在同一事务中继续执行.
我们现在无法执行此操作,因为事务已由容器关闭.这是我们在日志中看到的内容:
org.hibernate.exception.GenericJDBCException: Cannot open connection
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
...
Caused by: javax.resource.ResourceException: Transaction is not active: tx=TransactionImple < ac, BasicAction: 7f000101:85fe:4f04679d:182 status: ActionStatus.ABORT_ONLY >
Run Code Online (Sandbox Code Playgroud)
EJB类标有以下注释
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER) …Run Code Online (Sandbox Code Playgroud) 是否有任何聪明的方法可以避免在以下情况下使用IN子句进行代价高昂的查询?
我正在使用Google App Engine构建Facebook应用程序,并且在某些时候我(显然)需要查询数据存储区以获取属于给定用户的任何facebook朋友的所有实体.
假设我有几个实体建模:
class Thing(db.Model):
owner = db.ReferenceProperty(reference_class=User, required=True)
owner_id = db.StringProperty(required=True)
...
Run Code Online (Sandbox Code Playgroud)
和
class User(db.Model):
id = db.StringProperty(required=True)
...
Run Code Online (Sandbox Code Playgroud)
在某些时候,我查询Facebook以获取给定用户的朋友列表,我需要执行以下查询
# get all Thing instances that belong to friends
query = Thing.all()
query.filter('owner_id IN', friend_ids)
Run Code Online (Sandbox Code Playgroud)
如果我这样做,AppEngine将为每个id执行子查询friend_ids,可能超过任何查询可以生成的子查询的最大数量(30).
有没有更好的方法来做到这一点(即最小化查询数量)?我知道使用数据存储区没有任何关系和连接,但是,特别是,如果它有助于使事情变得更容易,我会考虑向User或Thing类添加新字段.
我需要针对SOCKS代理设置代理身份验证.我发现这篇文章提供的说明似乎与常见的HTTP代理一起使用.
httpclient.getHostConfiguration().setProxy("proxyserver.example.com", 8080);
HttpState state = new HttpState();
state.setProxyCredentials(new AuthScope("proxyserver.example.com", 8080),
new UsernamePasswordCredentials("username", "password"));
httpclient.setState(state);
Run Code Online (Sandbox Code Playgroud)
这也适用于SOCKS代理,还是我必须做一些不同的事情?
对于我正在编写的Rails 3应用程序,我正在考虑从本地文件系统上的XML,YAML或JSON文件中读取一些配置数据.
关键是:我应该把这些文件放在哪里?Rails应用程序中是否存在存储此类内容的默认位置?
作为旁注,我的应用程序部署在Heroku上.
我在EHCache 文档中找到了对此概念的引用,但我找不到对其含义的任何正确解释。它尝试过谷歌搜索,但没有成功。
有没有办法使用JPA2标准API执行以下查询?
select a from b where a in (1, 2, 3, 4)
Run Code Online (Sandbox Code Playgroud)
有一种方法可以使用普通的Hibernate来做到这一点,但我们在JPA2中找不到类似的东西.
我知道Java的实现尽其所能隐藏开发人员的信息,但我正在构建一个不依赖于第三方库的内存限制算法,因此信息会派上用场.
特别是,我将许多大型int[]数组分配为实例变量.我将很快调查更紧凑的表示,但是现在我有兴趣知道对于普通数组使用了多少空间.
这只是一个偏好和熟悉的问题,还是语言产生了实际的差异?
我正在使用Django模板在Google AppEngine上开发一个项目,所以我必须使用标签{{ aitem.Author }}来打印我的HTML模板中的内容.
Author但是,可以是字符串或列表对象,我无法事先告诉它.当作者是一个列表并且我尝试在我的模板上打印它时,我得到了丑陋的结果
作者:[u'JK罗琳',u'Mary GrandPr\xe9']
有没有办法处理这种情况(基本上根据其类型打印一个字段)?我是否必须依赖自定义标签或任何其他方式?
我有接受Object类型参数的方法.但是检查方法内部是否是例如List类型.有可能在mockito中存根吗?例如
public void checkValue(Object arg) {
if (arg instanceof List) {
....
Run Code Online (Sandbox Code Playgroud)
所以在mockito中:
Object myObject=mock(Object.class);
Run Code Online (Sandbox Code Playgroud)
在我需要写下之后:
when (myObject instanceof List).thenReturn List
Run Code Online (Sandbox Code Playgroud)
怎么做?谢谢.
java ×4
python ×4
jpa ×2
c# ×1
c++ ×1
caching ×1
collections ×1
criteria ×1
criteria-api ×1
django ×1
ehcache ×1
ejb-3.0 ×1
facebook ×1
gql ×1
gquery ×1
hash ×1
heroku ×1
http ×1
jboss ×1
jpa-2.0 ×1
kinect ×1
memory ×1
methods ×1
mockito ×1
oauth ×1
proxy ×1
ruby ×1
soap ×1
transactions ×1
windows ×1
wsdl ×1
zeep ×1