assertAlmostEqual giving TypeError: 'str' object cannot be interpreted as an integer, despite isinstance check - pytest

I have some test code:
def assertSamplesEqual(self, a, b, msg=None):
if isinstance(a.value, float) and isinstance(b.value, float):
self.assertAlmostEqual(
a.value, b.value, msg or
'Samples %s and %s have different values' % (a, b))
else:
self.assertEqual(
a.value, b.value, msg or
'Samples %s and %s have different values' % (a, b))
I just swapped to assertAlmostEqual to better handle floats (I could get float or string values here, hence the isinstance conditional) and started getting the following error:
third_party/py/perfkitbenchmarker/test_util.py:52 in assertSamplesEqualUpToTimestamp
self.assertAlmostEqual(
<embedded stdlib>/unittest/case.py:882 in assertAlmostEqual
if round(diff, places) == 0:
TypeError: 'str' object cannot be interpreted as an integer
How can I be getting this string error from assertAlmostEqual, when I've just checked via isinstance that the values are floats?
I tried print(type(a.value)) & got the expected <class 'float'> response.

It was not a.value which was of type string, but rather the following parameter msg. Look at the function definition for assertAlmostEqual:
assertAlmostEqual(first, second, places=7, msg=None, delta=None) Note that the third parameter is places & the fourth parameter is msg. Since msg was my third parameter, it was being treated as places & resulted in the TypeError at the line if round(diff, places) == 0:.
My fix was pretty simple - add the parameter name msg=. So my new line is:
self.assertAlmostEqual(
a.value, b.value, msg=msg or
'Samples %s and %s have different values' % (a, b))
Tricky red herring - I doubted isinstance when I should have checked assertAlmostEqual!

Related

case types boolean and double precision cannot be matched in Postgres

Query : if b is credit (i.e 0 or greater than 0), then the values should be a. Otherwise (a+b), and negative values should be zero.
My query condition is
Case when(b>=0) Then (a) Else (SUM(a+b)>0 End as Difference
But I'm getting this error
Error : CASE Types boolean and double precision cannot be matched
It would be a great pleasure if someone assist for the query solution. I'm using Postgres as database.
There is a syntax error in your expression; the number of opening and closing parentheses doesn't match.
But a is a number, while a + b > 0 is a boolean value (“true” or “false”), so PostgreSQL complains that it cannot determine what the type of your case expression should be.
According to your description, I would use
CASE WHEN b >= 0
THEN a
ELSE greatest(a + b, 0)
END

type char to type num mapping

q)type variable
returns the type num of the arguement variable.
Is there a mapping that can produce the type char from a type num or do I have to create that dictionary myself?
Ideally something like
q)typeChar 1
i
You can use .Q.t.
q).Q.t abs type `test
"s"
q).Q.t abs type 5i
"i"
Edit: Or even better just use .Q.ty
This seems to return upper case for atoms and lower case for lists.
q).Q.ty `test
"S"
q).Q.ty `test`test2
"s"
One option is to use 'key' function :
Reference: http://code.kx.com/q/ref/metadata/#key
Wiki says: Given a simple list, returns the name of the type as a symbol:
So you can make function like:
q) tyeInChar:{key x,()}
q) typeInChar 1i // output `int
q) typeInChar "s" //output `char

Fortran convert string to number

I want to have a subroutine that converts a contents of a numeric
string to a numeric type (int, real, double precision, real(real128)).
However I am getting an error when trying to use Class(*). The error
is shown below:
gfortran -o build/lib/larsa.o -c -ffree-form -g -J./build/lib lib/larsa.f
lib/larsa.f:1933.35:
Read (s, frmt, iostat=ios) num
1
Error: Data transfer element at (1) cannot be polymorphic unless
it is processed by a defined input/output procedure
lib/larsa.f:1935.32:
Read (s, *, iostat=ios) num
1
Error: Data transfer element at (1) cannot be polymorphic unless
it is processed by a defined input/output procedure
This is the subroutine I have written.
Subroutine converts_str_to_num &
( &
s, num, &
fmt, wrn &
)
Character (len=*), Intent (in) :: s
Character (len=*), Intent (in), Optional :: fmt
Class (*) :: num
Character (len=*), Intent (inout), Optional :: wrn
Integer :: ios
Character (len=65) :: frmt
!!$ Reads contents of s and puts value in i.
If (Present (fmt)) Then
frmt = "(" // Trim (fmt) // ")"
Read (s, frmt, iostat=ios) num
Else
Read (s, *, iostat=ios) num
End If
End Subroutine converts_str_to_num
To tidy up the comments, I'll provide an answer.
The error message is clear: you cannot have a polymorphic variable in an input/output list unless the list is processed by defined input/output. This is 9.6.3.5 in Fortran 2008. class(*) num is (unlimited) polymorphic.
Now, for polymorphic derived types you could define such a defined input/output procedure, but that counts as a lot of work and gfortran certainly doesn't (yet) support that notion. Also, you can't do this for intrinsic types. These factors mean you have to deal with non-polymorphic variables in the input list you have.
Of course, it's possible to use generics to avoid polymorphism, but the alternative (as it is for about everything polymorphic) is to use a select type construct. For simplicity, ignore the list-directed and explicit format cases:
select type (assoc => num)
type is (int)
Read (s, *, iostat=ios) assoc
type is (real)
...
type is (...)
class default
error stop "Oh noes!"
end select
I've used an associate name in the select type to address one part of your confusion. If you've just done
select type(num)
type is (int)
Read (s, *, iostat=ios) num
end select
to think that "now using num is fine: why?" that's because the num inside the construct is not the same as the num outside. Crucially, it isn't polymorphic but is the exact type matching the type is.

SOME([]) interpreted as 'z -> 'z option rather then 'z list option

Hi im trying to write a simple function using the standard library
it should take the following argument
try = fn: 'a -> 'b list option
a_list = 'a list
and the defintion is the following:
fun all_answers try a_list =
let fun acc(SOME(a), SOME(b)) = SOME(b#a)
| acc(_,_) = NONE
in
List.foldl (fn(x,y) => acc(try(x), y)) SOME([]) a_list
end
and i get the following error:
hw3provided.sml:70.3-70.60 Error: operator and operand don't agree [tycon mismatch]
operator domain: 'Z list option
operand: 'Y -> 'Y option
in expression:
(List.foldl (fn (<pat>,<pat>) => acc <exp>)) SOME
C:\Program Files (x86)\SMLNJ\\bin\.run\run.x86-win32.exe: Fatal error -- Uncaught exception Error with 0
raised at ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
ive tried decomposing the function and the error seems to correspond to the use of SOME([]), if i use NONE instead it typechecks perfectly
I am at a total loss here
just incase its significant im using the sublime repl to run the script
figured out the solution but not really the problem
surround the SOME([]) with brackets (SOME([]))
just in case anybody else comes across the same issue

Custom Sort Range

Just need to know how can I get the following code not to get me a type mismatch error. The last line which is commented out works but when i replace the Range("B2:B2000") with f it gives me a type mismatch error. Reason why I am not just using the last line instead since it works is because what if column B becomes Column C if i insert a new column in Column B. Is there something else that I need to add to the f to make it work?
f = Application.WorksheetFunction.Match("PCR No.", Range("A1:AZ1"), 0)
ActiveWorkbook.Worksheets("3. PMO Internal View").Sort.SortFields.Add Key:=Cells(1, f)
ActiveWorkbook.Worksheets("3. PMO Internal View").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("3. PMO Internal View").Sort.SortFields.Add Key:= _
f, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
'Range("B2:B2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal