小编aru*_*l84的帖子

如何测试Django自定义模型字段?

我正在考虑创建一些子类Django模型字段并将它们作为包发布在PyPI上.我喜欢为我的代码编写单元测试(例如TDD),但我对于如何为这个特定的库编写测试感到有些困惑.

我想到的第一个想法是创建一个Django项目,它使用我的子类并只使用Django测试工具,但这看起来并不优雅.必须有更好的方法!

有没有一种方法以某种方式引导Django这种类型的东西?我很感激有人指着我正确的方向.谢谢!

django unit-testing subclass django-models

6
推荐指数
1
解决办法
3650
查看次数

重构使用Reader monad的Haskell函数

我有一些看起来像这样的代码,忽略了与我的问题无关的所有代码:

import qualified Control.Monad.Reader as Reader

data FooEnv = FooEnv { bar :: Int -> Int }
type FooReader = Reader.Reader FooEnv

foo :: Int -> FooReader String
foo i = Reader.liftM show $ bar' i
  where
    bar' i' = do
      bar'' <- Reader.asks bar
      return $ bar'' i'
Run Code Online (Sandbox Code Playgroud)

有没有办法重构这个?具体来说,嵌套bar'函数最让我困扰.这可以浓缩成一行吗?

monads refactoring haskell do-notation reader-monad

6
推荐指数
1
解决办法
217
查看次数

Yesod/Persistent实体派生Show

Yesod书Persistent章节中,给出了这个实体的一个例子

{-# LANGUAGE QuasiQuotes, TypeFamilies, GeneralizedNewtypeDeriving, TemplateHaskell, OverloadedStrings, GADTs #-}
import Database.Persist
import Database.Persist.TH
import Database.Persist.Sqlite
import Control.Monad.IO.Class (liftIO)

mkPersist sqlSettings [persist|
Person
    name String
    age Int
    deriving Show
|]
Run Code Online (Sandbox Code Playgroud)

生成代码

{-# LANGUAGE TypeFamilies, GeneralizedNewtypeDeriving, OverloadedStrings, GADTs #-}
import Database.Persist
import Database.Persist.Store
import Database.Persist.Sqlite
import Database.Persist.GenericSql.Raw (SqlBackend)
import Database.Persist.EntityDef
import Control.Monad.IO.Class (liftIO)
import Control.Applicative

data Person = Person
    { personName :: String
    , personAge :: Int
    }
  deriving (Show, Read, Eq)

type PersonId = Key Person …
Run Code Online (Sandbox Code Playgroud)

entity persistent yesod

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

在Haskell中输入对类型类实例的约束?

所以我正在使用Data.Set.Monad,它似乎不像Data.Set那样Data.Foldable的实例.我决定尝试自己添加这个实例作为实验:

import Data.Foldable (Foldable, foldr)
import qualified Data.Set.Monad as S (Set, foldr)

instance Foldable S.Set where
  foldr = S.foldr
Run Code Online (Sandbox Code Playgroud)

我收到编译错误:

No instance for (Ord a) arising from a use of ‘S.foldr’
Possible fix:
  add (Ord a) to the context of
    the type signature for foldr :: (a -> b -> b) -> b -> Set a -> b
In the expression: S.foldr
In an equation for ‘foldr’: foldr = S.foldr
In the instance declaration …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass type-constraints

4
推荐指数
1
解决办法
275
查看次数