我是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)
它们之间有什么区别吗?
我试着写一个类似于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) 给出以下代码
#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,但我只是想知道例程何时运行。
applicative ×1
c ×1
dictionary ×1
haskell ×1
monads ×1
posix ×1
pthreads ×1
python ×1
sqlalchemy ×1