小编Cos*_*una的帖子

SQLAlchemy中的Model.query和session.query(Model)有什么区别?

我是SQLAlchemy的初学者,发现查询可以用2种方法完成:

方法1:

DBSession = scoped_session(sessionmaker())
class _Base(object):
    query = DBSession.query_property()

Base = declarative_base(cls=_Base)

class SomeModel(Base):
    key   = Column(Unicode, primary_key=True)
    value = Column(Unicode)

# When querying
result = SomeModel.query.filter(...)
Run Code Online (Sandbox Code Playgroud)

方法2

DBSession = scoped_session(sessionmaker())
Base = declarative_base()

class SomeModel(Base):
    key   = Column(Unicode, primary_key=True)
    value = Column(Unicode)

# When querying
session = DBSession()
result = session.query(SomeModel).filter(...)
Run Code Online (Sandbox Code Playgroud)

它们之间有什么区别吗?

python sqlalchemy

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

我可以用Applicative而不是Monad重写这个类似unionWith的函数吗?

我试着写一个类似于Data.Map.unionWith的函数,但可能会失败.原来的人使用了Maybe,这确实是Monad,所以monadic对我来说效果很好.但是我想知道它是否可以用Applicative重写,因为我用纯粹的fmap来满足unionWith的类型要求.或者使用Data.Map中的其他函数而不是unionWith?

{-# LANGUAGE RankNTypes #-}
import Control.Monad
import Data.Map
unionWithM :: (Monad m, Traversable t)
          => (forall a. (a -> a -> a)
              -> t a
              -> t a
              -> t a
             )
          -> (v -> v -> m v)
          -> t v
          -> t v
          -> m (t v)
unionWithM u f a b = sequenceA (u f' (pure <$> a) (pure <$> b))
  where f' x y = join $ f <$> x <*> y

unionWithOriginal …
Run Code Online (Sandbox Code Playgroud)

monads dictionary haskell applicative

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

传递给 pthread_create 的例程何时开始?

给出以下代码

#include <pthread.h>

void *pt_routine(void *arg)
{
    pthread_t *tid;
    tid = (pthread_t *) arg;
    /* do something with tid , say printf?*/
    /*
    printf("The thread ID is %lu\n", *tid);
    */
    return NULL;
}

int main(int argc, char **argv)
{
    int rc;
    pthread_t tid;
    rc = pthread_create(&tid, NULL, pt_routine, &tid);
    if (rc)
    {
        return 1;
    }
    printf("The new thread is %lu\n", tid);
    pthread_join(tid, NULL);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

例行公事能永远正确吗tid

当然,我可以使用 pthread 来获取自身 ID,但我只是想知道例程何时运行。

c posix pthreads

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

标签 统计

applicative ×1

c ×1

dictionary ×1

haskell ×1

monads ×1

posix ×1

pthreads ×1

python ×1

sqlalchemy ×1