Testing Program Who Having PrintLn Output - scala

I having problem with program which is prints to standard outputs. The method I test is print to standard output so it having Unit return type. I then writing Scalatest to assert output but I don't know how. I get error like this
This is output of Scalatest
Customer 1 : 20.0
Customer 2 : 20.0
Customer 3 : 20.0
Customer 4 : 20.0
Customer 5 : 20.0
<(), the Unit value> did not equal "Customer 1 : 20.0
Customer 2 : 20.0
Customer 3 : 20.0
Customer 4 : 20.0
Customer 5 : 20.0"
My assert looking like
assert(output() == "Customer 1 : 20.0\nCustomer 2 : 20.0\nCustomer 3 : 20.0\nCustomer 4 : 20.0\nCustomer 5 : 20.0")
How can I testing this?

Console.withOut enables temporary redirection of output to a stream that we can assert on, for example,
class OutputSpec extends FlatSpec with Matchers {
val someStr =
"""
|Customer 1 : 20.0
|Customer 2 : 20.0
|Customer 3 : 20.0
|Customer 4 : 20.0
|Customer 5 : 20.0
""".stripMargin
def output(): Unit = println(someStr)
"Output" should "print customer information" in {
val stream = new java.io.ByteArrayOutputStream()
Console.withOut(stream) { output() }
assert(stream.toString contains someStr)
}
}

Related

Lower time frame in pine script indicator

I want to fix calculation time period of my indicator as 1 minute .
That means
if I am checking on 5 minute chart . I want to see the current 5 minute indicator value as some of 5 one minute indicator values included in the current 5 minute candle.
If I am checking on 15 minute chart. I want to see the current 15 minute indicator value as some of 15 one minute indicator values included in the current 15 minute candle.
Same for all.
And I coded it as bellow . But as chart time frame changes calculation also changes.
This is how I Coded it
indicator(title = "OBV MULTI" , overlay = false)
var a = 0.0
var b = 0.0
var c = 0.0
var d = 0.0
a := ta.change(time("D")) ? 0.0 : a
b := ta.change(time("W")) ? 0.0 : b
c := ta.change(time("M")) ? 0.0 : c
d := ta.change(time("12M")) ? 0.0 :d
src1 = request.security_lower_tf(syminfo.tickerid" ,"1",close)
src2 = request.security_lower_tf(syminfo.tickerid","1",open)
src3 = request.security_lower_tf(syminfo.tickerid, "1",volume)
a := (close>open) ? a+volume : (close<open) ? a-volume : ((close==open)and(a[1]>a[2])) ? a +volume : ((close==open))and(a[1]<a[2])) ? a-volume : 0
b := (close>close) ? b+volume : (close<open) ? b-volume : ((close==open)and(b[1]>b[2])) ? b +volume : ((close==open))and(b[1]<b[2])) ? b-volume : 0
c := (close>open) ? c+volume : (close<open) ? c-volume : ((close==open)and(c[1]>c[2])) ? c +volume : ((close==open))and(c[1]<c[2])) ? c-volume : 0
d := (close>open) ? d+volume : (close<open) ? d-volume : ((close==open)and(d[1]>d[2])) ? d +volume : ((close==open))and(d[1]<d[2])) ? d-volume : 0
Plot(a , title = "D Line" , color = color.blue)
Plot(b , title = "W Line" , color = color.green)
Plot(c , title = "M Line" , color = color.yellow)
Plot(d , title = "Y Line" , color = color.orange)
Plot(0 , title = "Zero Line", color = color.red)

parsing text with Parse::RecDescent

I am trying to parse some text with Parse::RecDescent
from :
x=2 and y=2 and f=3 and (x has 3,4 or r=5 or r=6 ) and z=2
to something like :
x equal 2 and y equal 2 and f equal 3 and (( x contains 3 or x contains 4 or r equal 5 or requal 6 )) and z equal 2
other example :
input :
x= 3 and y has 5 and (z has 6 or z=3 ) and f=2
output :
x equals 3 and (( y contains 5)) and ((z has 6 or z equals 3)) and f equals 2
my question if i find a list of those operators :
has ,or
i should put "((" before the code and "))" after the code as mentioned on the example aboves
is it possible to do something like this with Parse::RecDescent ?
The grammar would look something like the following:
parse : expr EOF
expr : logic_or
# vvv Lowest precedence vvv
logic_or : <leftop: logic_and LOGIC_OR logic_and>
logic_and : <leftop: comparison LOGIC_AND comparison>
comparison : term comparison_[ $item[1] ]
comparison_ : '=' term
| HAS ident_list
|
# ^^^ Highest precedence ^^^
ident_list : <leftop: IDENT ',' IDENT>
term : '(' expr ')'
| IDENT
# Tokens
IDENT : /\w+/
LOGIC_OR : /or\b/
LOGIC_AND : /and\b/
HAS : /has\b/
EOF : /\Z/
Now you just need to add the code block to emit the desired output.
In comparison_, the LHS term is available as $arg[0].
I had to make some assumptions, so there could be errors.

Swift 4 Firebase Realtime Database nested dictionary

ZERO experience, so bear with me...
Im trying to retrieve data from a Firebase Realtime Database and im using this code..
func fetchData(){
refHandle = ref?.child("caddata").observe(.childAdded, with: { (snapshot) in
if let values = snapshot.value as? [AnyHashable: Any] {
for value in values.values {
print (value)
"values" shows the following:
▿ 1 element
▿ 0 : 2 elements
▿ key : AnyHashable("parsedContent")
- value : "parsedContent"
▿ value : 1 element
▿ 0 : 7 elements
▿ 0 : 2 elements
- key : location
- value : 1234 ANY ADDRESS ST ANYTOWN
▿ 1 : 2 elements
- key : agencyId
- value : 3-08
▿ 2 : 2 elements
- key : alarmLevel
- value : 0
▿ 3 : 2 elements
- key : agencyEventSubtypeCode
- value : 59-C-3O
▿ 4 : 2 elements
- key : originatingAction
- value : CadEventNew
▿ 5 : 2 elements
- key : agencyEventId
- value : CC17187712
▿ 6 : 2 elements
- key : dateTime
- value : 2017-12-22T22:37:27Z
Which I believe you'd refer to as a nested dictionary. "value" prints:
▿ 1 element
▿ 0 : 7 elements
▿ 0 : 2 elements
- key : location
- value : 1234 ANY ADDRESS ST ANYTOWN
▿ 1 : 2 elements
- key : agencyId
- value : 3-08
▿ 2 : 2 elements
- key : alarmLevel
- value : 0
▿ 3 : 2 elements
- key : agencyEventSubtypeCode
- value : 59-C-3O
▿ 4 : 2 elements
- key : originatingAction
- value : CadEventNew
▿ 5 : 2 elements
- key : agencyEventId
- value : CC17187712
▿ 6 : 2 elements
- key : dateTime
- value : 2017-12-22T22:37:27Z
I have no idea how to get into the next level or levels to retrieve the value for each key shown. The end goal is to show some or all of the data in a tableview. Like I said, I have no experience. Any help (in the simplest terms) would be greatly appreciated.
Looks like your value is another dictionary. So
if let dict = value as Dictionary {
for key in dict.allKeys {
print("the value for key \(key) is:")
print(dict[key])
}
}

[torch]how to read weights in nn model

I constructed the nn model using itorch notebook.
model = nn.Sequential()
model:add(nn.Reshape(ninputs))
model:add(nn.Linear(ninputs,noutputs))
Input data to the model
output = model:forward(input)
Then, I print the model and got this.
print(model)
nn.Sequential {
[input -> (1) -> (2) -> output]
(1): nn.Reshape(3072)
(2): nn.Linear(3072 -> 10)
}
{
gradInput : DoubleTensor - empty
modules :
{
1 :
nn.Reshape(3072)
{
_input : DoubleTensor - empty
nelement : 3072
train : true
output : DoubleTensor - size: 3072
gradInput : DoubleTensor - empty
size : LongStorage - size: 1
_gradOutput : DoubleTensor - empty
batchsize : LongStorage - size: 2
}
2 :
nn.Linear(3072 -> 10)
{
gradBias : DoubleTensor - size: 10
weight : DoubleTensor - size: 10x3072
train : true
bias : DoubleTensor - size: 10
gradInput : DoubleTensor - empty
gradWeight : DoubleTensor - size: 10x3072
output : DoubleTensor - size: 10
}
}
train : true
output : DoubleTensor - size: 10
}
how to read the weight in nn.linear ?
Thanks in advance.
Oh, it is similar to php
model.modules[2].weight
I find that model.modules[1].weight is similar to model:get(1).weight, but both can't get the parameters from the table layer like residual block. In this way the residual block as a layer.
however, we can use params, gradParams = model:parameters() to get the parameters for each layer even in the table layer.
It is worth noting that, in the second way, each layer of the network parameters are divided into two layers and arranged in layers

Leafletjs legand value = 0

I would like to know (if it's possible) how to add a 0 value ine Leafletjs block's legend.
For an example, this is my class :
function getColorPOPULATION(d) {
return d > 500 ? '#004590' :
d > 250 ? '#00ABFF' :
d > 1 ? '#A0E0FF' :
'#FFF4D0' ;
... and my grades (div legend) :
grades = [0, 1, 250, 500],
-> I get this result :
0 - 1
1 - 250
250 - 500
500+
I would like to isolate the 0 value (my first class will be 0). Does Leafletjs able to do this ?
Thank you,