小编Pau*_*aul的帖子

如何从python中的tests模块导入src

我有一个应用程序,我想测试使用,unittest但我有一些问题.我的目录结构如下:

root_dir
??? src
?   ??? cmds
?   ?   ??? baz.py
?   ?   ??? __init__.py
?   ?   ??? bar.py
?   ??? foo.py
??? tests
    ??? cmds.py
    ??? __init__.py
Run Code Online (Sandbox Code Playgroud)

我想测试bazbar模块cmds,我正在尝试

root_dir> python2.7 -m unittest tests.cmds

但是tests.cmds我无法导入cmds我的src目录中的包.

我怎样才能做到这一点?

基本上我想root_dirsrctests目录分别测试应用程序.

我试图追加srcsys.path,但是当我导入cmds.baztests/cmds.py我还是得到一个AttributeError: 'module' object has no attribute 'cmds'从单元测试.

编辑:我的导入和sys.path声明是:

import sys
sys.path.append('../src') …
Run Code Online (Sandbox Code Playgroud)

python unit-testing

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

如何在Haskell中正确使用foldr?

我正在尝试编写一个行为如下的函数:

correctCards :: [Card] -> [Card] -> Int
Run Code Online (Sandbox Code Playgroud)

它需要两个类型卡列表并检查有多少卡是相同的.这是我的代码:

correctCards answer guess = foldr step acc guess
        where 
            acc = 0
            step acc guess
                | elem (head guess) answer  = acc + 1
                | otherwise                 = acc
Run Code Online (Sandbox Code Playgroud)

但类型不匹配.谁能告诉我哪里出错了?谢谢.

haskell fold

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

在非安全(匿名)路由上显示经过身份验证的用户

我使用PHP和Silex构建一个Web应用程序,我通过SecurityServiceProvider以下方式实现了基本身份验证:

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'private' => array(
            'remember_me' => array(
                'key' => $config['secret_key'],
                'lifetime' => $config['remember_me_duration'],
            ),
            'pattern' => '^/admin',
            'form' => array('login_path' => '/login', 'check_path' => '/admin/login_check'),
            'logout' => array('logout_path' => '/admin/logout'),
            'users' => $app->share(function () use ($app) {
                // ...
            }),
        ),
        'public' => array(
            'pattern' => '^/$',
            'anonymous' => true,
        ),
        'login' => array(
            'pattern' => '^/login$',
            'anonymous' => true,
        ),
    ),
));
Run Code Online (Sandbox Code Playgroud)

正如你所看到的/admin路径被固定,我可以使用$app['security']->getToken()->getUser();来获取被认证的实际用户,并显示类似登入身分$的用户名,但如果我这样做对/login或 …

php authentication symfony twig silex

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

自定义数据类型的Monadic Haskell运算符(带有状态的+)

我正在按照48小时编写自己的方案教程,并给出下面的代码我稍微绕道而行,以便能够运行(+ 4 4.0)(我添加了对Floats的支持):

import Control.Monad.Except
import Text.ParserCombinators.Parsec hiding (spaces)

data LispError = NumArgs Integer [LispVal]
    | TypeMismatch String LispVal
    | Parser ParseError
    | BadSpecialForm String LispVal
    | NotFunction String String
    | UnboundVar String String
    | Default String

type ThrowsError = Either LispError

data LispVal = Atom String
    | List [LispVal]
    | DottedList [LispVal] LispVal
    | Number Integer
    | Float Float
    | String String
    | Bool Bool

instance Show LispVal where
    show = showVal

instance Show LispError …
Run Code Online (Sandbox Code Playgroud)

error-handling monads haskell

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