Only doctests on immediate module - haskell

If I have three modules:
-- A.hs
module A where
-- $
-- >>> True
-- True
-- B.hs
module B where
import A
-- $
-- >>> True
-- True
-- C.hs
module C where
import B
-- $
-- >>> True
-- True
Running doctest C.hs will run all the doctests in all three files.
$ doctest C.hs
Examples: 3 Tried: 3 Errors: 0 Failures: 0
Is there a way to run doctest on only the top-level module - that is, have it not recursively search for tests in included modules?

Related

Issue reading I2C with BPi with Raspbian Stretch OS

I am working with a BPi and I would like to read an CAN to I2C module connected to the SCL and SCA pins of the BPi.
When I try to detect the module, it seems to work fine, as i run i2cdetect -y 0 and the module with address 0x25 appears as follows:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- 25 -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Then, I try the i2cget -y 0 0x25 0 command, and something strange happens. I tried it once, and it returned 0x00, and after that, every time I run the same command, it pops up an error Error: Read failed.
Just as additional information, I am using a LoganLabs I2C-CAN module and a BPi with the Raspbian Stretch OS. I have the CAN module connected to a kvaser in which a CAN message is sent, so I think I should be able to read something with this commands.
I am trying to run this commands on the command window as a debbuging process, as I t also tried with the following code above, which simply returns "None".
from smbus2 import SMbus, i2c_msg
import time
address=25
def func1():
lect=i2c_msg.read(address,0)
while True:
lecture1=func1()
print(lecture1)
time.sleep(1)
'''
It returns: None
'''

RPI3B BME280 AttributeError: 'module' object has no attribute 'load_calibration_params'

I'm trying to get the following code working after battling with an smbus2 error i now find i have the following error and can't find how to fix it.
I know the sensor is working because if I run bme280.py from http://www.raspberrypi-spy.co.uk/ that works ok.
So my error is:
pi#wpi:~/weather $ python bme280_sensor.py
Traceback (most recent call last):
File "bme280_sensor.py", line 9, in <module>
bme280.load_calibration_params(bus, address)
AttributeError: 'module' object has no attribute 'load_calibration_params'
i2detect
pi#wpi:~/weather $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --
Code not working: from https://pypi.org/project/RPi.bme280/
import smbus2
import bme280
port = 1
address = 0x76
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, address)
# the sample method will take a single reading and return a
# compensated_reading object
data = bme280.sample(bus, address, calibration_params)
# the compensated_reading class has the following attributes
print(data.id)
print(data.timestamp)
print(data.temperature)
print(data.pressure)
print(data.humidity)
# there is a handy string representation too
print(data)
Found my issue posting it here in case any other newbies have the same issue!
I had a file called bme280.py in the same folder so it was loading that file instead of the correct bme280 library.
The solution was simply to rename that file.
Mine worked after I installed RPi.bme280
$ sudo pip3 install RPi.bme280
This is from the bme280 documentation:
https://github.com/rm-hull/bme280

HUnit does not allow to compile test cases if `Nothing == Nothing` condition is present in the test

I have encountered a weird HUnit behavior. It does not allow to compile test cases if Nothing == Nothing condition is present in the test. Here is my code that reproduces this behavior:
module TestTest where
import Control.Exception
import Control.Monad
import Test.HUnit
import Test.AssertError
testTests = test [
"test A01" ~: "x == x" ~: True ~=? Nothing == Nothing,
"test _" ~: "empty test" ~: True ~=? True
]
runTests :: IO Counts
runTests = do
runTestTT testTests
Attempt to load the file with this contents in ghci returns following error:
[2 of 2] Compiling TestTest ( Test/TestTest.hs, interpreted )
Test/TestTest.hs:9:49:
No instance for (Eq a0) arising from a use of ‘==’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Eq Counts -- Defined in ‘Test.HUnit.Base’
instance Eq Node -- Defined in ‘Test.HUnit.Base’
instance Eq State -- Defined in ‘Test.HUnit.Base’
...plus 53 others
In the second argument of ‘(~=?)’, namely ‘Nothing == Nothing’
In the second argument of ‘(~:)’, namely
‘True ~=? Nothing == Nothing’
In the second argument of ‘(~:)’, namely
‘"x == x" ~: True ~=? Nothing == Nothing’
Failed, modules loaded: Test.AssertError.
Note that condition Just 2 == Just 2 in the same test case works fine. If I type Nothing == Nothing in ghci, it returns True as expected.
Any ideas why HUnit might behave this way? Is this a bug or expected behavior?
The problem is that you specify two Nothings, and none of these thus hint what the type of a will be. Of course you can reason that for Nothing it does not matter. But Haskell does not reason that way: it is interested in "to what (==) function should I point?".
You can resolve the problem by making the type explicit. For instance:
testTests = test [
"test A01" ~: "x == x" ~: True ~=? (Nothing :: Maybe Int) == Nothing,
"test _" ~: "empty test" ~: True ~=? True
]

Hspec unable to load interface for Spec file

I am trying to run hspec but getting error as "Failed to load interface for Spec file". I tried similar example from github, got same error. Please suggest where I am going wrong...(PS: I am able to run this with stack).
Example from github (https://github.com/FranklinChen/twenty-four-days2015-of-hackage)
I have following files:
-- src.HintExample.hs
module HintExample where
import SortWrapper (Sort)
import qualified Language.Haskell.Interpreter as I
import Language.Haskell.Interpreter (OptionVal((:=)))
-- | Dynamically load a 'Sort' implementation from a file.
-- src is needed to pick up our SortWrapper.
-- sort-plugins is a sample user plugins directory
loadSort :: I.MonadInterpreter m =>
String -- ^ module name
-> String -- ^ function name
-> m Sort
loadSort moduleName functionName = do
I.set [I.searchPath := ["src", "sort-plugins"]]
I.loadModules [moduleName]
I.setImports [moduleName, "SortWrapper"]
I.interpret (moduleName ++ "." ++ functionName) (I.as :: Sort)
And test file as
-- test.HintExampleSpec.hs
module HintExampleSpec where
import SortWrapper (Sort(Sort))
import HintExample (loadSort)
import qualified Language.Haskell.Interpreter as I
import Test.Hspec (Spec, hspec, describe, it, shouldBe)
import Test.Hspec.QuickCheck (prop)
-- | Required for auto-discovery.
spec :: Spec
spec =
describe "hint" $ do
it "dynamically loads a correct polymorphic sort function" $ do
Right (Sort ourSort) <-
I.runInterpreter (loadSort "OurSorter" "ourSort")
ourSort "ebcad" `shouldBe` "abcde"
ourSort [1 :: Int, 5, 4, 3, 7] `shouldBe` [1, 3, 4, 5, 7]
it "dynamically loads a wrong (only head) sort function" $ do
Right (Sort onlyHead) <-
I.runInterpreter (loadSort "OurSorter" "onlyHead")
onlyHead "ebcad" `shouldBe` "e"
onlyHead [True, False] `shouldBe` [True]
main :: IO ()
main = hspec spec
Spec file (Spec.hs) in test directory as:
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
running as:
runhaskell test/Spec.hs

GHC API - How to dynamically load Haskell code from a compiled module using GHC 7.2?

I have an existing Haskell function that uses the GHC API to dynamically load compiled code from a module. It is based on the code from the blog post Dynamic Compilation and Loading of Modules in Haskell.
The code works fine in GHC 7.0, but had to be slightly modified to compile in GHC 7.2, because the GHC API changed.
The code now throws a runtime error in GHC 7.2:
mkTopLevEnv: not a home module (module name):(function name)
The code is
evalfuncLoadFFI String moduleName,
String externalFuncName,
String internalFuncName = do
result <- liftIO $ defaultRunGhc $ do
dynflags <- GHC.getSessionDynFlags
_ <- GHC.setSessionDynFlags dynflags
m <- GHC.findModule (GHC.mkModuleName moduleName) Nothing
--------------------------------------------------------
-- The following code works fine in GHC 7.0.4:
--
-- GHC.setContext [] [(m, Nothing)]
--
-- This new code attempts to set context to the module,
-- but throws an error in GHC 7.2:
--
(_,oi) <- GHC.getContext
GHC.setContext [m] oi
--------------------------------------------------------
fetched <- GHC.compileExpr (moduleName ++ "." ++ externalFuncName)
return (Unsafe.Coerce.unsafeCoerce fetched :: [LispVal] -> IOThrowsError LispVal)
defineVar env internalFuncName (IOFunc result)
For reference, the full code is available online in FFI.hs (github.com).
Does anyone have any idea how to fix or work around this problem?
Also, could this be caused by the new Safe Haskell changes in GHC 7.2, or is it just due to modifications to the GHC API?
The current module context is reserved for modules that are currently being compiled, i.e. when you specify modules in the context, they must explicitly not be external.
Instead, you should specify the wanted module as an import, in the second argument of setContext. This can be done like so:
GHC.setContext []
-- import qualified Module
[ (GHC.simpleImportDecl . GHC.mkModuleName $ moduleName)
{ GHC.ideclQualified = True
}
-- -- import qualified Data.Dynamic
-- , (GHC.simpleImportDecl . GHC.mkModuleName $ "Data.Dynamic")
-- { GHC.ideclQualified = True
-- }
]
fetched <- GHC.compileExpr $ moduleName ++ "." ++ externalFuncName
return . unsafeCoerce $ fetched
-- or:
-- fetched <- GHC.dynCompileExpr $ moduleName ++ "." ++ externalFuncName
-- return . fromDynamic (error "Illegal type cast") $ fetched
PS: it might be a good idea to use GHC.dynCompileExpr instead, so that you can avoid the unsafeCoerce. You must add a qualified import for Data.Dynamic in the context for it to work, but a Data.Dynamic.Dynamic value is generally nicer to work with, since you can handle type errors more gracefully. I've added the code for that as comments in the above code.
Update
And here is the syntax for GHC 7.4.1:
GHC.setContext
-- import qualified Module
[ GHC.IIDecl $
(GHC.simpleImportDecl . GHC.mkModuleName $ moduleName)
{GHC.ideclQualified = True}
]
Try
GHC.setContext [] [(m,Nothing)]
(from another StackOverflow question)

Resources