随着.net 4 中的Tuple类的增加,我一直试图决定在我的设计中使用它们是否是一个糟糕的选择.我看到它的方式,一个元组可以是编写结果类的快捷方式(我相信还有其他用途).
所以这:
public class ResultType
{
public string StringValue { get; set; }
public int IntValue { get; set; }
}
public ResultType GetAClassedValue()
{
//..Do Some Stuff
ResultType result = new ResultType { StringValue = "A String", IntValue = 2 };
return result;
}
Run Code Online (Sandbox Code Playgroud)
相当于:
public Tuple<string, int> GetATupledValue()
{
//...Do Some stuff
Tuple<string, int> result = new Tuple<string, int>("A String", 2);
return result;
}
Run Code Online (Sandbox Code Playgroud)
因此,抛开我错过了元组点的可能性,是一个Tuple设计选择不好的例子吗?对我来说,它似乎不那么混乱,但不像自我记录和干净.这意味着对于类型ResultType
,后面很清楚类的每个部分意味着什么,但你有额外的代码来维护.随着Tuple<string, int>
您将需要查找并弄清每一个 …
我刚刚开始在django项目中实现信号监听器.虽然我明白它们是什么以及如何使用它们.我很难搞清楚应该放在哪里.来自django网站的文档有这样的说法:
您可以将信号处理和注册码放在任何您喜欢的地方.但是,您需要确保早期导入模块,以便在需要发送任何信号之前注册信号处理.这使您的应用程序的models.py成为放置信号处理程序注册的好地方.
虽然这是一个很好的建议,但在我的models.py中使用非模型类或方法只会让我误以为然.
那么,存储和注册信号处理程序的最佳实践/规则是什么?
是否有可能ModelForm
在django中包含多个模型?我正在尝试创建个人资料编辑表单.所以我需要包含User模型和 UserProfile模型中的一些字段.目前我正在使用这样的2个表单
class UserEditForm(ModelForm):
class Meta:
model = User
fields = ("first_name", "last_name")
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ("middle_name", "home_phone", "work_phone", "cell_phone")
Run Code Online (Sandbox Code Playgroud)
有没有办法将这些整合到一个表单中,或者我只需要创建一个表单并处理数据库加载并保存自己?
我正在尝试为我们的django-celery项目提出测试方法.我已经阅读了文档中的注释,但它并没有让我对实际操作有什么了解.我并不担心测试实际守护进程中的任务,只是我的代码的功能.主要是我想知道:
task.delay()
在测试期间绕过(我尝试过设置,CELERY_ALWAYS_EAGER = True
但没有区别)?manage.py test
或者我们必须使用自定义跑步者吗?总的来说,使用芹菜进行测试的任何提示或技巧都会非常有用.
一位朋友在Clojure中给了我这段代码片段
(defn sum [coll acc] (if (empty? coll) acc (recur (rest coll) (+ (first coll) acc))))
(time (sum (range 1 9999999) 0))
Run Code Online (Sandbox Code Playgroud)
并问我如何对付类似的Scala实现.
我写的Scala代码看起来像这样:
def from(n: Int): Stream[Int] = Stream.cons(n, from(n+1))
val ints = from(1).take(9999998)
def add(a: Stream[Int], b: Long): Long = {
if (a.isEmpty) b else add(a.tail, b + a.head)
}
val t1 = System.currentTimeMillis()
println(add(ints, 0))
val t2 = System.currentTimeMillis()
println((t2 - t1).asInstanceOf[Float] + " msecs")
Run Code Online (Sandbox Code Playgroud)
底线是:Clojure中的代码在我的机器上运行大约1.8秒并且使用少于5MB的堆,Scala中的代码运行大约12秒并且512MB的堆是不够的(如果我设置了它,它完成计算堆到1GB).
所以我想知道为什么在这种特殊情况下Clojure会更快更轻薄?你有一个Scala实现在速度和内存使用方面有类似的行为吗?
请不要发表宗教言论,我的兴趣在于找出主要是什么使得clojure在这种情况下如此快速,并且如果在scala中更快地实现算法.谢谢.
performance scala tail-recursion clojure tail-call-optimization
不得不说我大约两周前就开始学习Clojure了,现在我整整三天都遇到了问题.
我有一张这样的地图:
{
:agent1 {:name "Doe" :firstname "John" :state "a" :time "VZ" :team "X"}
:agent2 {:name "Don" :firstname "Silver" :state "a" :time "VZ" :team "X"}
:agent3 {:name "Kim" :firstname "Test" :state "B" :time "ZZ" :team "G"}
}
Run Code Online (Sandbox Code Playgroud)
并需要:team "X"
改为:team "H"
.我尝试了许多类似的东西assoc
,update-in
但没有任何作用.
我怎么能做我的东西?非常感谢!
我使用的是python 2.7和pymssql 1.9.908.
在.net中查询数据库我会做这样的事情:
using (SqlCommand com = new SqlCommand("select * from Customer where CustomerId = @CustomerId", connection))
{
com.Parameters.AddWithValue("@CustomerID", CustomerID);
//Do something with the command
}
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚python的等价物是什么,尤其是pymssql.我意识到我可以只进行字符串格式化,但是这似乎没有像参数一样正确地进行转义(我可能错了).
我怎么在python中这样做?
我有一个插件式模块包.它看起来像这样:
/Plugins /Plugins/__init__.py /Plugins/Plugin1.py /Plugins/Plugin2.py etc...
每个.py文件都包含一个派生自的类PluginBaseClass
.所以我需要列出Plugins
包中的每个模块,然后搜索任何实现的类PluginBaseClass
.理想情况下,我希望能够做到这样的事情:
for klass in iter_plugins(project.Plugins):
action = klass()
action.run()
Run Code Online (Sandbox Code Playgroud)
我已经看到了其他一些答案,但我的情况有所不同.我有一个实际的导入基础包(即:) import project.Plugins
,我需要在发现模块后找到类.
我测试期货的方式是使用value1.我迁移到了play2.2.我发现,我习惯的测试方法已经消失了.@ scala.deprecated("使用scala.concurrent.Promise代替.","2.2")
任何帮助将不胜感激.
奥利弗
python 内置方法是否可以在某个包中引用?
让我解释.在python的早期(ier)时代,我制作了一个类似于此的django模型:
class MyModel(models.Model):
first_name = models.CharField(max_length=100, null=True, blank=True)
last_name = models.CharField(max_length=100, null=True, blank=True)
property = models.ForeignKey("Property")
Run Code Online (Sandbox Code Playgroud)
我已经需要为它添加一个属性.这让我有这个模型:
class MyModel(models.Model):
first_name = models.CharField(max_length=100, null=True, blank=True)
last_name = models.CharField(max_length=100, null=True, blank=True)
property = models.ForeignKey("Property")
@property
def name(self):
return "{} {}".format(first_name, last_name)
Run Code Online (Sandbox Code Playgroud)
所以现在在运行时我得到错误:TypeError: 'ForeignKey' object is not callable
.发生这种情况是因为ForeignKey属性已替换内置标识符属性.我希望能够做的是,而不是@property
使用@sys.property
(或类似的东西).
注意:我已经知道将name属性移动到属性字段声明之上的解决方法.我不是那么关心这个特殊情况,因为我是引用python内置函数的替代位置的主要问题.
python ×5
django ×3
clojure ×2
scala ×2
unit-testing ×2
.net ×1
built-in ×1
c# ×1
celery ×1
django-forms ×1
future ×1
key ×1
map ×1
performance ×1
pymssql ×1
reflection ×1
specs2 ×1
tuples ×1