标签: conduit

使用Haskell的zip-conduit从zip存档中的文件中读取行

正如标题所说,我希望能够使用zip-conduit从zip存档中的文件中读取行(我正在处理的zip文件非常大,所以我需要能够做到这在恒定的记忆中).我理解了管道的基本概念,但从来没有在愤怒中使用它们,并且我对从何处开始感到困惑.我已经阅读了管道教程,但是我无法将其与我的问题相匹配.

zip-conduit文档说,可以通过以下内容从zip存档中获取:

import qualified Data.Conduit.Binary as CB
import Codec.Archive.Zip

withArchive archivePath $ do
    name:_ <- entryNames
    sourceEntry name $ CB.sinkFile name
Run Code Online (Sandbox Code Playgroud)

我认为我需要做的就是写一些代替的东西CB.sinkFile.Data.Conduit.Text有一个lines功能 - 这可以用某种方式来获取文件的行吗?

我真的很感激一个简单的例子,比如putStrLn用来写出一个存档在zip文件中的简单文本文件的行.提前致谢.

zip haskell conduit

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

在Network.HTTP.Conduit中禁用SSL/TLS证书验证

我使用http-conduit库版本2.0+从http://URL 获取内容:

import Network.HTTP.Conduit
myurl = ... -- Your URL goes here
main = do content <- simpleHttp myurl
          print $ content
Run Code Online (Sandbox Code Playgroud)

运行此程序时,我收到此错误:

*** Exception: TlsException (HandshakeFailed (Error_Protocol
      ("certificate rejected: certificate is not allowed to sign another certificate",
        True,CertificateUnknown)))
Run Code Online (Sandbox Code Playgroud)

从错误消息中可以看出,问题是无法Network.HTTP.Conduit正确验证服务器证书(在这种情况下,证书链中似乎存在问题)

如何更改上面的代码以忽略证书错误(即根本不验证证书)?

ssl haskell conduit http-conduit

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

你将如何遍历目录并对所有文件执行某些功能并以高效的内存方式组合输出?

设置

我需要遍历超过100个.txt文件的目录,打开每个文件并对每个文件执行一些功能,然后合并结果.这些文件很大,大约10GB.伪造的代码中的一些常见操作可能是:

foldr concatFile mempty $ openFile <$> [filePath1, ..., filePathn]
foldr countStuff 0      $ openFile <$> [filePath1, ..., filePathn]
Run Code Online (Sandbox Code Playgroud)

诀窍是确保所有文件同时存在于内存中,我以前的天真解决方案在我的mac上创建了各种交换文件.另外,如果其中一个filePath无效,我想跳过它继续该程序.

我的解决方案

目前我正在使用管道,并希望尽可能找到使用管道的解决方案.但如果它不是正确的工具我可以使用别的东西.

haskell conduit haskell-pipes

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

提高基于线路的管道性能的方法

我使用haskell进行基于行的数据处理,即可以应用的任务sed,awk以及类似的工具.作为一个简单的例子,让我们000从标准输入前面加上每一行.

我有三种替代方法来完成任务:

  1. 惰性IO与懒惰ByteStrings
  2. 基于行的管道.
  3. 基于块的管道,ByteString内部采用纯粹的严格处理.

example.hs:

{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}

import ClassyPrelude.Conduit
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as BL8
import qualified Data.Conduit.Binary as CB

main = do
  [arg] <- getArgs
  case arg of

    "lazy" -> BL8.getContents >>= BL8.putStr . BL8.unlines . map ("000" ++) . BL8.lines

    "lines" -> runConduitRes $ stdinC .| CB.lines .|
      mapC ("000" ++) .| …
Run Code Online (Sandbox Code Playgroud)

io haskell conduit

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

什么是点管".|" Haskell的运营商?

我明白 "." (点)作为功能组合.我理解"|" (管道)作为"或",警卫介绍语法(从这里 ),但我在http-conduit上看到了一个使用".|"的答案.以我不理解的方式使用此运算符.

我发现的其他管道参考,例如:

...建议语法如"$$","$ =","= $ =","= $"用于组合数据流中的管道.

我该怎么称呼这个".|" 这个运算符,它是如何工作的?

可以预见的是,Google搜索".| haskell"或"'dot pipe'haskell"或"'dot pipe'haskell运营商管道"并不是很成功.

haskell conduit http-conduit

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

使用 Haskell 进行实时音频流传输

我希望能够从 GHCI 实时播放简单的 [1] 音频,启动和停止独立的振荡器和样本(“声音”)。

在 Linux 上使用 Bash,很容易[2] 将数据传输到命令中,通过扬声器传输音频。

在 Haskell 中,我想象了一些类似的东西:

x <- realtimeAudioSink
createVoices x [VoiceName "sinewave",VoiceName "drums"]
play x (VoiceName "sinewave") $ sinewave (Hz 440)
play x (VoiceName "sinewave") $ sinewave (Hz 220)
  -- replace the earlier sinewave with a lower-frequency one
play x (VoiceName "drums")
  $ asSoonAs (theTime >= floor theTime)
  $ every (Seconds 10) $ soundfile "snare.wav"
  -- starting on the next second, play the snare drum sample every 10 seconds
destroyVoice …
Run Code Online (Sandbox Code Playgroud)

linux audio haskell stream conduit

6
推荐指数
0
解决办法
1150
查看次数

将普通的attoparsec解析器代码转换为基于管道/管道

我使用以下方法编写了以下解析代码attoparsec:

data Test = Test {
  a :: Int,
  b :: Int
  } deriving (Show)

testParser :: Parser Test
testParser = do
  a <- decimal
  tab
  b <- decimal
  return $ Test a b

tParser :: Parser [Test]
tParser =  many' $ testParser <* endOfLine
Run Code Online (Sandbox Code Playgroud)

这适用于小型文件,我这样执行:

main :: IO ()
main = do
  text <- TL.readFile "./testFile"
  let (Right a) = parseOnly (manyTill anyChar endOfLine *> tParser) text
  print a  
Run Code Online (Sandbox Code Playgroud)

但是当文件大小超过70MB时,它会占用大量内存.作为解决方案,我想我会用attoparsec-conduit.在完成他们的API之后,我不确定如何让它们一起工作.我的解析器有类型,Parser Test但它 …

parsing haskell attoparsec conduit haskell-pipes

5
推荐指数
2
解决办法
541
查看次数

_generate_jwt_token 中的格式字符串无效

这是我正在关注的教程,链接

https://thinkster.io/tutorials/django-json-api/authentication

正如标题所说,我在这一行收到此错误“格式字符串无效”:

'exp': int(dt.strftime('%s'))

_generate_jwt_token。

我查看了 strftime 的文档,没有这样的格式 '%s' 有一个大写的 S ('%S'),我将格式更改为大写的 S,但是我在尝试的过程中遇到了错误解码授权令牌,我收到以下错误

{"user": {"detail": "身份验证无效。无法解码令牌。"}}

如果我保留小写字母 s,我会收到“无效格式字符串”错误。

(authentication/backends.py)
def _authenticate_credentials(self, request, token):
    """
    Try to authenticate the given credentials. If authentication is
    successful, return the user and token. If not, throw an error.
    """
    try:
        payload = jwt.decode(token, settings.SECRET_KEY)
    except:
        msg = 'Invalid authentication. Could not decode token.'
        raise exceptions.AuthenticationFailed(msg)


(authentication/models.py)
def _generate_jwt_token(self):
        """
        Generates a JSON Web Token that stores this user's ID and has an expiry …
Run Code Online (Sandbox Code Playgroud)

python django conduit

5
推荐指数
2
解决办法
1171
查看次数

为什么在添加 `bracketOnError` 时这段代码内存泄漏?

首先,我很抱歉没有一个最小的例子(我可以尝试构建一个,但现在我有一个“之前和之后”的例子):

首先是具有内存泄漏的“之后”:

protoReceiver :: RIO FdsEnv ()
protoReceiver = do
  logItS Info ["Entering FarmPCMessage protoReceiver"]
  tMap <- liftIO $ newThreadMap
  fdsEnv <- ask
  let lgr = fdsLogger fdsEnv
  loopBody <- pure $ bracketOnError
    (runResourceT $ protoServe fdsEnv tMap readFarmPCMessage)
    (\(_,w) -> do
      logLogItS Debug lgr ["Entering cleanup for protoReceiver"]
      )
    (\(server,_) -> do
      logLogItS Debug lgr ["Entering FarmPCMessage protoReceiver bracket"]
      server
        .| mapMC (liftIO . traverse_ (persistFarmEntry fdsEnv))
        .| mapMC ((logLogIt Info lgr) . pure)
        .| sinkUnits & runConduitRes
      ) …
Run Code Online (Sandbox Code Playgroud)

haskell lazy-evaluation conduit

5
推荐指数
0
解决办法
136
查看次数

如何使用管道获取惰性 ByteString 并将其写入文件(在常量内存中)

我正在使用 amazonka 流式传输 S3 文件的下载,并使用该sinkBody功能继续流式传输。目前,我下载的文件如下:

getFile bucketName fileName = do
    resp <- send (getObject (BucketName bucketName) fileName)
    sinkBody (resp ^. gorsBody) sinkLazy
Run Code Online (Sandbox Code Playgroud)

在哪里sinkBody :: MonadIO m => RsBody -> ConduitM ByteString Void (ResourceT IO) a -> m a。为了在恒定内存中运行,我认为这sinkLazy是从管道流中获取值的一个不错的选择。

之后,我想将数据的惰性字节串(S3 文件)保存到本地文件中,为此我使用以下代码:

-- fetch stream of data from S3
bytestream <- liftIO $ AWS.runResourceT $ runAwsT awsEnv $ getFile serviceBucket key

-- create a file
liftIO $ writeFile filePath  ""

-- write content of stream into …
Run Code Online (Sandbox Code Playgroud)

haskell lazy-evaluation conduit

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