如何测试某个属性是否存在于某些XML中(How to test if an attribute exists in some XML)
我有一些XML,我通过lxml在python中解析。
我遇到了一些元素具有属性而另一些元素没有属性的情况。
如果它们存在,我需要提取它们,但如果它们不存在,则跳过它们 - 我目前正在着陆并出现错误(因为我的方法错误...)
我已经部署了testfornull,但在所有情况下都不起作用:
码:
if root[0][a][b].attrib == '<>': ByteSeqReference = "NULL" else: ByteSeqReference = (attributes["Reference"])
XML A:
<ByteSequence Reference="BOFoffset">
XML B:
<ByteSequence Endianness = "little-endian" Reference="BOFoffset">
XML C:
<ByteSequence Endianness = "little-endian">
XML D:
<ByteSequence>
我目前的方法只能处理A,B或D.它不能处理C.
I have some XML that I am parsing in python via lxml.
I am encountering situations where some elements have attributes and some don't.
I need to extract them if they exist, but skip them if they don't - I'm currently landing with errors (as my approach is wrong...)
I have deployed a testfornull, but that doesn't work in all cases:
Code:
if root[0][a][b].attrib == '<>': ByteSeqReference = "NULL" else: ByteSeqReference = (attributes["Reference"])
XML A:
<ByteSequence Reference="BOFoffset">
XML B:
<ByteSequence Endianness = "little-endian" Reference="BOFoffset">
XML C:
<ByteSequence Endianness = "little-endian">
XML D:
<ByteSequence>
My current method can only deal with A, B or D. It can not cope with C.
原文:https://stackoverflow.com/questions/10115396
最满意答案
你会如何执行Arduino的
reactimate
?我会通过执行具有可观察副作用的IO操作来间接执行它们。 然后,在
withArduino
内部,我会观察这种副作用并运行相应的Arduino命令。这是一些示例代码。 首先,让我们看看进口产品。
{-# LANGUAGE GeneralizedNewtypeDeriving, ScopedTypeVariables #-} import Control.Monad.IO.Class import Data.IORef import Data.Word import Reactive.Banana import Reactive.Banana.Frameworks import Text.Printf
由于我没有arduino,所以我不得不模拟来自hArduino的一些方法。
newtype Arduino a = Arduino (IO a) deriving (Functor, Applicative, Monad, MonadIO) newtype Pin = Pin Word8 pin :: Word8 -> Pin pin = Pin digitalWrite :: Pin -> Bool -> Arduino () digitalWrite (Pin n) v = Arduino $ do printf "Pretend pin %d on the arduino just got turned %s.\n" n (if v then "on" else "off") digitalRead :: Pin -> Arduino Bool digitalRead (Pin n) = Arduino $ do printf "We need to pretend we read a value from pin %d.\n" n putStrLn "Should we return True or False?" readLn withArduino :: Arduino () -> IO () withArduino (Arduino body) = do putStrLn "Pretend we're initializing the arduino." body
在其余的代码中,我会假装Arduino和Pin类型是不透明的。
我们需要一个事件网络来将表示从arduino接收到的信号的输入事件转换为描述我们想要发送给arduino的输出事件。 为了让事情变得非常简单,让我们从一个引脚接收数据,并在另一个引脚上输出完全相同的数据。
eventNetwork :: forall t. Event t Bool -> Event t Bool eventNetwork = id
接下来,让我们将我们的活动网络连接到外部世界。 当输出事件发生时,我只需将该值写入IORef中,以后可以观察。
main :: IO () main = do (inputPinAddHandler, fireInputPin) <- newAddHandler outputRef <- newIORef False let networkDescription :: forall t. Frameworks t => Moment t () networkDescription = do -- input inputPinE <- fromAddHandler inputPinAddHandler -- output let outputPinE = eventNetwork inputPinE reactimate $ writeIORef outputRef <$> outputPinE network <- compile networkDescription actuate network withArduino $ do let inputPin = pin 1 let outputPin = pin 2 -- initialize pins here... -- main loop loop inputPin outputPin fireInputPin outputRef
注意在主循环之外,只有一次调用
reactimate
和compile
。 这些功能设置你的事件网络,你不想在每个循环中调用它们。最后,我们运行主循环。
loop :: Pin -> Pin -> (Bool -> IO ()) -> IORef Bool -> Arduino () loop inputPin outputPin fireInputPin outputRef = do -- read the input from the arduino inputValue <- digitalRead inputPin -- send the input to the event network liftIO $ fireInputPin inputValue -- read the output from the event network outputValue <- liftIO $ readIORef outputRef -- send the output to the arduino digitalWrite outputPin outputValue loop inputPin outputPin fireInputPin outputRef
注意我们如何使用
liftIO
与Arduino计算内部的事件网络进行交互。 我们调用fireInputPin
来触发一个输入事件,事件网络导致一个输出事件被触发作为响应,并且我们给出的writeIORef
用于reactimate
导致输出事件的值被写入IORef。 如果事件网络更复杂并且输入事件未触发任何输出事件,则IORef的内容将保持不变。 无论如何,我们可以观察这些内容,并使用它来确定运行哪个Arduino计算。 在这种情况下,我们只需将输出值发送到预定的引脚。How would you execute Arduino actions in
reactimate
?I would cause them to be executed indirectly, by executing an IO action which has an observable side-effect. Then, inside
withArduino
, I would observe this side-effect and run the corresponding Arduino command.Here's some example code. First, let's get the imports out of the way.
{-# LANGUAGE GeneralizedNewtypeDeriving, ScopedTypeVariables #-} import Control.Monad.IO.Class import Data.IORef import Data.Word import Reactive.Banana import Reactive.Banana.Frameworks import Text.Printf
Since I do not have an arduino, I'll have to mock up a few methods from hArduino.
newtype Arduino a = Arduino (IO a) deriving (Functor, Applicative, Monad, MonadIO) newtype Pin = Pin Word8 pin :: Word8 -> Pin pin = Pin digitalWrite :: Pin -> Bool -> Arduino () digitalWrite (Pin n) v = Arduino $ do printf "Pretend pin %d on the arduino just got turned %s.\n" n (if v then "on" else "off") digitalRead :: Pin -> Arduino Bool digitalRead (Pin n) = Arduino $ do printf "We need to pretend we read a value from pin %d.\n" n putStrLn "Should we return True or False?" readLn withArduino :: Arduino () -> IO () withArduino (Arduino body) = do putStrLn "Pretend we're initializing the arduino." body
In the rest of the code, I'll pretend that the Arduino and Pin types are opaque.
We'll need an event network to transform input events representing signals received from the arduino into output events describing what we want to send to the arduino. To keep things extremely simple, let's receive data from one pin and output the exact same data on another pin.
eventNetwork :: forall t. Event t Bool -> Event t Bool eventNetwork = id
Next, let's connect our event network to the external world. When output events occur, I simply write the value into an IORef, which I'll be able to observe later.
main :: IO () main = do (inputPinAddHandler, fireInputPin) <- newAddHandler outputRef <- newIORef False let networkDescription :: forall t. Frameworks t => Moment t () networkDescription = do -- input inputPinE <- fromAddHandler inputPinAddHandler -- output let outputPinE = eventNetwork inputPinE reactimate $ writeIORef outputRef <$> outputPinE network <- compile networkDescription actuate network withArduino $ do let inputPin = pin 1 let outputPin = pin 2 -- initialize pins here... -- main loop loop inputPin outputPin fireInputPin outputRef
Note how
reactimate
andcompile
are only called once, outside the main loop. Those functions setup your event network, you do not want to call them on every loop.Finally, we run the main loop.
loop :: Pin -> Pin -> (Bool -> IO ()) -> IORef Bool -> Arduino () loop inputPin outputPin fireInputPin outputRef = do -- read the input from the arduino inputValue <- digitalRead inputPin -- send the input to the event network liftIO $ fireInputPin inputValue -- read the output from the event network outputValue <- liftIO $ readIORef outputRef -- send the output to the arduino digitalWrite outputPin outputValue loop inputPin outputPin fireInputPin outputRef
Note how we use
liftIO
to interact with the event network from inside an Arduino computation. We callfireInputPin
to trigger an input event, the event network causes an output event to be triggered in response, and thewriteIORef
we gave toreactimate
causes the output event's value to be written to the IORef. If the event network was more complicated and the input event did not trigger any output event, the contents of the IORef would remain unchanged. Regardless, we can observe that contents, and use it to determine which Arduino computation to run. In this case, we simply send the output value to a predetermined pin.
相关问答
更多-
你会如何执行Arduino的reactimate ? 我会通过执行具有可观察副作用的IO操作来间接执行它们。 然后,在withArduino内部,我会观察这种副作用并运行相应的Arduino命令。 这是一些示例代码。 首先,让我们看看进口产品。 {-# LANGUAGE GeneralizedNewtypeDeriving, ScopedTypeVariables #-} import Control.Monad.IO.Class import Data.IORef import Data.Word im ...
-
使用MonadIO测试类型类:“没有实例也没有默认方法”错误(Testing a typeclass with MonadIO: “No instance nor default method” error)[2019-11-14]
一种解决方案是支持WorkingDBM实际IO 。 例如: newtype WorkingDBM a = WorkingDBM (IO a) -- N.B. IO not Identity deriving (Functor, Applicative, Monad) instance MonadIO WorkingDBM where liftIO = WorkingDBM MonadIO的派生实例也可以正常工作; 但是空实例不会,因为它相当于 instance MonadIO Worki ... -
在片段内执行操作(Performing Action inside the Fragment)[2023-05-03]
首先,你的代码中的Nothings错误,你的代码所知道的是正确的,你只是忘了一点。 基于log-cat错误,你的textView id有问题,所以将你的id更改为与main.xml不同的id,然后将其写入代码然后尝试运行它。 一件事,尝试删除你的try catch异常,这是没用的。 这是您的代码的结论。 public class Main extends Fragment{ Button btn; TextView tx; @Override public View onC ... -
如何创建一个允许IO但不是MonadIO的monad?(How to create a monad which allows IO but is NOT a MonadIO?)[2023-10-26]
如果您只想隐藏MonadIO的AppM 我会继续努力,并投入一项研究 {-# LANGUAGE GeneralizedNewtypeDeriving #-} 并将data声明更改为 newtype App a = App {runApp :: ReaderT Env (LoggingT IO) a} deriving (Functor, Applicative, Monad, MonadReader Env, , MonadLogg ... -
为什么显然任何monad堆栈通常都会派生出MonadIO?(Why can apparently any monad stack generally derive MonadIO?)[2023-07-13]
使用GeneralizedNewtypeDeriving ,所有实例都具有相同的约束 - newtype的基类型必须是同一个类的实例: newtypes的广义派生实例 所有实例都应用并删除newtype构造函数。 派生的实例,即Monad具有约束Monad (ExceptT em) ,它已经存在。 但是, MonadIO (ExceptT em)没有实例,因此必须对生成的MonadIO声明进行约束。 如果我尝试使用 MonadIO (Foo em) ,则会生成错误: something :: Foo e m ... -
看起来图像缓存在客户端上。 您可以通过附加随机查询字符串参数来破坏缓存: 通过使用[OutputCache]属性进行装饰来禁用GenerateThumbnail控制器操作上的缓存,就像使用GetThumbnail操作一样。 It looks like the image is cached on the client. Yo ...
-
使用behaviorText函数获得的behaviorText只会对用户对编辑框所做的更改做出反应。 它不包括程序更改,例如使用接收sink功能执行的更改。 区分用户事件和编程事件对于编写具有双向数据流的响应式UI元素至关重要。 有关演示,请参阅CurrencyConverter示例 。 如果你想跟踪程序化的变化,我建议保持“在FRP世界中”,即使用行为b_out = id <$> b_in而不是尝试从小部件中读取文本。 (顺便说一句, id <$> x = x 。) The behavior obtain ...
-
听起来你已经掌握了潜在的语义问题,但是为了清楚起见,让我重申一下。 at i是一个Lens进入一个返回Maybe的容器,因为容器中可能缺少该项(可能索引超出了列表的末尾)。 用像_Just这样的Prism这样的Lens会将整个事物变成Traversal : players . at i . _Just :: Traversal' Game Player 现在, zoom可以与Traversal一起使用,但它需要一个Monoid来获取有状态动作的返回值。 来自文档 : 当应用于Traversal' mult ...
-
您正在寻找的是MonadBase类,它是MonadBaseControl的超类: class (Applicative b, Applicative m, Monad b, Monad m) => MonadBase (b :: * -> *) (m :: * -> *) | m -> b where liftBase :: b α -> m α 所以你可以liftBase $ someIOAction r 。 What you are looking for is the MonadBa ...
-
如何在具有异步操作WinJS的for循环之后执行操作(How to execute an action after a for loop with asynchronous actions WinJS)[2023-01-09]
如果您希望在承诺完成后发生某些事情,那么您需要附加到承诺。 如果你希望在几个承诺全部完成之后发生某些事情,你可以将承诺加入到单个组合承诺中,然后附加到组合承诺的then 。 您的代码也有一个错误,它捕获循环变量。 这意味着deleteCapturesIndexArray.push(i)将始终推送arrayCaptures.length 。 这是两个问题的解决方案。 // Triggers SOAP requests depending of how many webServices are required ...