小编aka*_*ola的帖子

如何在Django中使用Python函数扩展SQLite?

可以在Python中为SQLite定义新的SQL函数.我怎么能在Django中这样做,以便功能在任何地方都可用?

示例用例是使用GREATEST()和LEAST() PostgreSQL函数的查询,这些函数在SQLite中不可用.我的测试套件运行此查询,我希望能够在运行测试时使用SQLite作为数据库后端.

python sqlite django

5
推荐指数
1
解决办法
861
查看次数

如何pickle继承自A的类B(具有许多变量)的对象,该对象定义了 __setstate__ 和 __getstate__

我的问题是:

class A(object):
    def __init__(self):
        #init
    def __setstate__(self,state):
        #A __setstate__ code here            
    def __getstate__(self):
        #A __getstate__ code here
        return state

class B(A):
    def __init__(self):
        #creates many object variables here
Run Code Online (Sandbox Code Playgroud)

A 来自外部库。

硬解

我想避免这种情况

当 pickle B 时,pickle 当然使用 A 类的__setstate__方法__getstate__,所以为了让 pickle 工作,我应该这样做:

class B(A):
    def __init__(self):
        #creates many object variables here

    def __setstate__(self,state)
        A.__setstate__(self,state)
        #B __setstate__ code here
        #getting various variables from state for example
        self._a0 = state['a0']
        self._a1 = state['a1']
        #...
        self._a100 = state['a100']
        self._a101 …
Run Code Online (Sandbox Code Playgroud)

python inheritance pickle

5
推荐指数
1
解决办法
4485
查看次数

Magit无法识别git存储库

我目前正在尝试在Win7 64位上将Magit与Emacs 23.1结合使用,但是Magit无法识别我的git存储库。请记住,我是一名完整的Emacs新手。

我运行magit-status命令,它要求一个包含存储库的目录,我显然输入了该目录,然后每次我尝试时都说:“ e:/ path / to / directory中没有Git存储库。当肯定有一个存储库时,创建一个?(y或n)。

还有其他人遇到过吗?我读到它可能是找不到实际的git.exe,并且我尝试弄乱我的Path变量,但是我没有做任何让Magit识别我的存储库的事情。有任何想法吗?

windows git emacs

3
推荐指数
1
解决办法
2178
查看次数

将动态属性添加到python对象

site = object()
mydict = {'name': 'My Site', 'location': 'Zhengjiang'}
for key, value in mydict.iteritems():
    setattr(site, key, value)
print site.a  # it doesn't work
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用.有什么建议吗?

python properties add dynamic

3
推荐指数
2
解决办法
6232
查看次数

Haskell异常和单元测试

基于SO问题13350164 如何在Haskell中测试错误?,我正在尝试编写一个单元测试,断言给定无效输入,递归函数引发异常.我采用的方法适用于非递归函数(或者当第一次调用引发异常时),但是一旦异常发生在调用链中,断言就会失败.

我已经阅读了问题6537766 Haskell错误处理方法的优秀答案,但不幸的是,对于我的学习曲线的这一点,建议有点过于通用.我的猜测是这里的问题与懒惰评估和非纯测试代码有关,但我很欣赏专家的解释.

在这种情况下(例如Maybe或者Either),我应该采用不同的方法来处理错误,还是在使用这种风格时是否有合理的方法使测试用例正常工作?

这是我提出的代码.前两个测试用例成功,但第三个测试用例失败了"Received no exception, but was expecting exception: Negative item".

import Control.Exception (ErrorCall(ErrorCall), evaluate)
import Test.HUnit.Base  ((~?=), Test(TestCase, TestList))
import Test.HUnit.Text (runTestTT)
import Test.HUnit.Tools (assertRaises)

sumPositiveInts :: [Int] -> Int
sumPositiveInts [] = error "Empty list"
sumPositiveInts (x:[]) = x
sumPositiveInts (x:xs) | x >= 0 = x + sumPositiveInts xs
                       | otherwise = error "Negative item"

instance Eq ErrorCall where
    x == y = (show x) == …
Run Code Online (Sandbox Code Playgroud)

haskell unit-testing exception

3
推荐指数
1
解决办法
523
查看次数

如何用 Python 绘制 polyfit `n log n`?

下面是我的代码的摘录,它根据给定的顺序绘制并创建趋势线 numpy.polyfit库。我能够绘制线性、二次和许多其他多项式趋势。但是我无法为可能适合的数据创建趋势线登录 或者 日志 n 趋势。

任何点击如何去做?

import numpy as np
from matplotlib import pyplot, pylab

def plotChart(title, xlabel, ylabel, x, y, fit):
    plot1 = pyplot.plot(x, y, "o", label="runtime")
    plot2 = pyplot.plot(x, fit(x), "--", label="trendline")
    pylab.title(title)
    pylab.ylabel(ylabel)
    pylab.xlabel(xlabel)
    pyplot.legend()
    pyplot.tight_layout()
    pyplot.show()

def analyzeTimes(sampleList, timingList, order, title, xlabel, ylabel):
    x = np.array(sampleList)
    y = np.array(timingList)
    coefficients = np.polyfit(x, y, order)
    fit = np.poly1d(coefficients)

    plotChart(
        f"{title}\n {fit}", 
        xlabel, 
        ylabel,
        x,
        y,
        fit
    )
Run Code Online (Sandbox Code Playgroud)

python numpy matplotlib

3
推荐指数
1
解决办法
3298
查看次数

Android WebView - Javascript不会触发回调函数

我是Android的新手,并且正在尝试设置Android Webview以加载javascript函数并将结果返回给回调函数.

请参阅下面的代码

public class AWebViewActivity extends Activity {

    /**
     * Defines an Interface to call Syntax Highlighting Javascript and return the result
     * to a string by calling the call back function.
     */
     public class JavaScriptInterface {
         Context mContext;
         JavaScriptInterface(Context c) {
             mContext = c;
         }

         //add other interface methods to be called from JavaScript
         // This annotation is required in Jelly Bean and later:
         @JavascriptInterface
         public void receiveValueFromJs(String str) {
              //do something useful with str
              Toast.makeText(mContext, "Received Value …
Run Code Online (Sandbox Code Playgroud)

javascript android android-webview

2
推荐指数
1
解决办法
8222
查看次数