Tag:

AttributeError:'模块'对象没有属性'strptime'

这是我的Transaction类: class Transaction(object): def __init__(self, company, num, price, date, is_buy): self.company = company self.num = num self.price = price self.date = datetime.strptime(date, "%Y-%m-%d") self.is_buy = is_buy 而当我试图运行datefunction: tr = Transaction('AAPL', 600, '2013-10-25') print tr.date 我收到以下错误: self.date = datetime.strptime(self.d, "%Y-%m-%d") AttributeError: 'module' object has no attribute 'strptime' 我该如何解决这个问题?

Javascript – 如何获得多个类的元素

说我有这个: <div class="class1 class2"></div> 我如何select这个div元素? document.getElementsByClassName('class1')[0].getElementsByClassName('class2')[0] 这是行不通的。 我知道,在jQuery中,它是$('.class1.class2') ,但我想用香草JavaScript来select它。

获得当前class的名字?

我如何得到我目前所在class级的名字? 例: def get_input(class_name): [do things] return class_name_result class foo(): input = get_input([class name goes here]) 由于程序的性质,我与(vistrails)接口,我不能使用__init__()来初始化input 。

我应该如何处理RESTful API中的对象层次结构?

我目前正在为现有的PHP应用程序deviseAPI,并为此调查REST作为一种明智的架构方法。 我相信我对关键概念有合理的把握,但是我正在努力find解决了对象层次结构和REST的人。 这是问题… 在[应用程序]业务对象层次结构中,我们有: Users L which have one-to-many Channel objects L which have one-to-many Member objects 在应用程序本身中,我们使用延迟加载方法根据需要使用这些对象的数组填充User对象。 我相信OO术语这是对象聚合,但我已经看到了各种命名不一致,不关心开始一场关于精确的命名约定的战争。 现在,考虑我有一些松散耦合的对象,根据应用程序的需要我可能/不可以填充。 从REST的angular度来看,我试图确定这个方法应该是什么。 这是我目前的想法(暂时只考虑GET): 选项1 – 完全填充对象: GET api.example.com/user/{user_id} 读取用户对象(资源)并返回用户对象,其中包含预先加载和编码的所有可能的通道和成员对象(JSON或XML)。 优点:减less对象数量,不需要遍历对象层次结构 缺点:对象必须完全填充(昂贵) 选项2 – 填充主要对象并包含指向其他对象资源的链接: GET api.example.com/user/{user_id} 阅读用户对象(资源)并返回填充的用户对象用户数据和两个列表。 每个列表引用适当的(子)资源,即 api.example.com/channel/{channel_id} api.example.com/member/{member_id} 我认为这与超媒体的影响(或确切地说)很接近 – 客户可以根据需要获得其他资源(只要我明智地标记)。 PROS:客户端可以select加载下属或者更好地分离对象作为REST资源 缺点:需要进一步旅行获得次要资源 选项3 – 启用recursion检索 GET api.example.com/user/{user_id} 阅读用户对象,并包含指向子对象列表的链接 api.example.com/user/{user_id}/channels api.example.com/user/{user_id}/members /通道调用将返回表单中的通道资源列表(如上所示): api.example.com/channel/{channel_id} PROS:主要的资源暴露了从哪里去获取子元素,而不是它们是什么(更RESTful?),没有要求获得下属,下级列表生成器(/通道和/成员)提供接口(方法)响应更多的服务。 […]

如何使用__getattribute__方法?

我想覆盖对一个类中的一个variables的访问,但通常返回所有其他的variables。 我如何用__getattribute__完成这个工作? 我试过以下(这也应该说明我在做什么),但我得到一个recursion错误: class D(object): def __init__(self): self.test=20 self.test2=21 def __getattribute__(self,name): if name=='test': return 0. else: return self.__dict__[name] >>> print D().test 0.0 >>> print D().test2 … RuntimeError: maximum recursion depth exceeded in cmp

.Contains()在自定义类对象的列表上

我正在尝试在自定义对象列表上使用.Contains()函数 这是名单: List<CartProduct> CartProducts = new List<CartProduct>(); CartProduct : public class CartProduct { public Int32 ID; public String Name; public Int32 Number; public Decimal CurrentPrice; /// <summary> /// /// </summary> /// <param name="ID">The ID of the product</param> /// <param name="Name">The name of the product</param> /// <param name="Number">The total number of that product</param> /// <param name="CurrentPrice">The […]

为什么Swift初始化器不能在它们的超类中调用便捷初始化器?

考虑这两个类: class A { var x: Int init(x: Int) { self.x = x } convenience init() { self.init(x: 0) } } class B: A { init() { super.init() // Error: Must call a designated initializer of the superclass 'A' } } 我不明白为什么这是不允许的。 最终,每个类的指定初始化器都被调用了它们需要的任何值,那么为什么我需要在B的init再次指定一个默认值来重复自己呢?

PHP中的静态类初始化器

我有一个辅助类与一些静态function。 类中的所有函数都需要一个“繁重的”初始化函数来运行一次(就像它是一个构造函数一样)。 有没有一个很好的做法,实现这一目标? 我唯一想到的就是调用一个init函数,如果它已经运行一次(使用静态的$initializedvariables),就会中断它的stream程。 问题是我需要在每个类的函数上调用它。

jquery,按类查找下一个元素

我怎样才能find下一个类的元素。 我尝试使用$(obj).next('.class'); 但是这只返回$(obj)父类中的类。 我需要通过类名在代码中的任何位置使用下一个元素。 因为我的代码看起来像 <table> <tr><td><div class="class">First</div></td></tr> <tr><td><div class="class">Second</div></td></tr> </table> 这可能吗?

在Backbone中访问父类

我需要从inheritance的MyModel类中调用父类的initialize方法,而不是像我今天所做的那样完全覆盖它。 我怎么能这样做? 以下是我的代码现在看起来: BaseModel = Backbone.Model.extend({ initialize: function(attributes, options) { // Do parent stuff stuff } }); MyModel = BaseModel.extend({ initialize: function() { // Invoke BaseModel.initialize(); // Continue doing specific stuff for this child-class. }, });