正如标题所说,我希望能够使用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文件中的简单文本文件的行.提前致谢.
我使用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正确验证服务器证书(在这种情况下,证书链中似乎存在问题)
如何更改上面的代码以忽略证书错误(即根本不验证证书)?
设置
我需要遍历超过100个.txt文件的目录,打开每个文件并对每个文件执行一些功能,然后合并结果.这些文件很大,大约10GB.伪造的代码中的一些常见操作可能是:
foldr concatFile mempty $ openFile <$> [filePath1, ..., filePathn]
foldr countStuff 0 $ openFile <$> [filePath1, ..., filePathn]
Run Code Online (Sandbox Code Playgroud)
诀窍是确保所有文件同时存在于内存中,我以前的天真解决方案在我的mac上创建了各种交换文件.另外,如果其中一个filePath无效,我想跳过它继续该程序.
我的解决方案
目前我正在使用管道,并希望尽可能找到使用管道的解决方案.但如果它不是正确的工具我可以使用别的东西.
我使用haskell进行基于行的数据处理,即可以应用的任务sed,awk以及类似的工具.作为一个简单的例子,让我们000从标准输入前面加上每一行.
我有三种替代方法来完成任务:
ByteStringsByteString内部采用纯粹的严格处理.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) 我明白 "." (点)作为功能组合.我理解"|" (管道)作为"或",警卫介绍语法(从这里 ),但我在http-conduit上看到了一个使用".|"的答案.以我不理解的方式使用此运算符.
我发现的其他管道参考,例如:
...建议语法如"$$","$ =","= $ =","= $"用于组合数据流中的管道.
我该怎么称呼这个".|" 这个运算符,它是如何工作的?
可以预见的是,Google搜索".| haskell"或"'dot pipe'haskell"或"'dot pipe'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) 我使用以下方法编写了以下解析代码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但它 …
这是我正在关注的教程,链接
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) 首先,我很抱歉没有一个最小的例子(我可以尝试构建一个,但现在我有一个“之前和之后”的例子):
首先是具有内存泄漏的“之后”:
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) 我正在使用 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)