Boo Language Calculation on Web Drop Down List - if-statement

The software we use LanDesk Service Desk uses Boo Language calculations to allow for dynamic windows.
I have a drop down list on a form that has one of two options. "Acute" and "Ambulatory". Based on which is chosen, one of two possible fields will no longer be hidden and will be set to mandatory. I have managed to get this to work, but I'm afraid if the number of options grows on future forms that the code will get a little wordy. Do any of you have any suggestions for alternatives. Thank you,
import System
static def GetAttributeValue(Request):
isAcuteHidden = true
isAcuteMandatory = false
isAmbulatoryHidden = true
isAmbulatoryMandatory = false
if Request._PharmacyType != null and Request._PharmacyType._Name == "Acute":
isAcuteHidden = false
isAcuteMandatory = true
elif Request._PharmacyType != null and Request._PharmacyType._Name == "Ambulatory":
isAmbulatoryHidden = false
isAmbulatoryMandatory = true
return String.Format(":SetHidden(_AcutePharmacy, {0});:SetMandatory(_AcutePharmacy, {1});:SetHidden(_AmbulatoryPharmacy, {2});:SetMandatory(_AmbulatoryPharmacy, {3});", isAcuteHidden, isAcuteMandatory, isAmbulatoryHidden, isAmbulatoryMandatory)

A few pointers:
It appears that for both pairs, the is?????Hidden and corresponding is?????Mandatory will always be opposite values. If so, you don't need two separate boolean variables to track them.
If you're going to end up with a lot of blocks that look basically like this one, the proper way to handle it in Boo is with a macro, like so:
macro HiddenOrRequiredValues:
ControlStrings = List[of String]()
ArgVarNames = List[of ReferenceExpression]()
counter = 0
for arg as ReferenceExpression in HiddenOrRequiredValues.Arguments:
argVariable = ReferenceExpression("is$(arg)Hidden")
yield [|$argVariable = true|]
yield [|
if Request._PharmacyType != null and Request._PharmacyType._Name == $(arg.ToString()):
$argVariable = false
|]
ControlStrings.Add(":SetHidden(_$(arg)Pharmacy, {$(counter)});:$(arg)Mandatory(_AcutePharmacy, {$(counter + 1)})")
ArgVarNames.Add(argVariable)
counter += 2
resultString = join(ControlStrings, '')
resultCmd as MethodInvocationExpression = [|String.Format($resultString)|]
for varName in ArgVarNames:
resultCmd.Arguments.Add([|$varName|])
resultCmd.Arguments.Add([|not $varName|])
yield [|return $resultCmd|]
Then the entirety of your method becomes:
static def GetAttributeValue(Request):
HiddenOrRequiredValues Acute, Ambulatory
It's more work to set it up, but once you have the macro in place, adding in more values becomes much, much easier. Feel free to modify the macro to suit your needs. This should be enough to get you started, though.

Related

Checking values across multiple location and returning a match only if the sources are unique

Lets say I have a list of Vendors: Asda, Tesco, Spar.
And I have a list of Sources (or suppliers in this analogy): Kellogg, Cadbury, Nestle, Johnsons, Pampers, Simple, etc. (there is a defined list of around 20).
Elsewhere in the flow of data. I am returning a result, which is Yes/No for each vendor, for multiple different things.
For example: Asda: ukOnly = "Yes"; Spar: ukOnly = "No" etc.
In this specific section, I am collating results.
Mostly it doesn't matter if the sources from the vendors overlap. So I can just say:
function concatResults(x) -- concats the result of "x" for each vendor
local pathAsda = *this is where I call the path location specific to Asda*
local pathTesco = *this is where I call the path location specific to Tesco*
local pathSpar = *this is where I call the path location specific to Spar*
if (pathAsda == "Yes" or pathTesco == "Yes" or pathSpar == "Yes") then
return "Yes"
else
return "No"
end
end
ukOnlyAgr = concatResults("ukOnly")
Great!
Now, say I want to do something more comple.
I want to know how many unique suppliers are providing chocolate and cereal. The example below is being used further up the process to produce a fact suppliesSweet, only if there is at least two sources (suppliers) involved and they must be at least supplying chocolate. This will be done for each vendor separately (please assume I have already defined my variables based on the input data:
if (suppliesChoc > 0 and suppliesCereal > 0 and numSources > 1) or (suppliesChoc > 1) then
then suppliesSweet = "Yes"
else suppliesSweet = "No"
end
Not a problem yet.
The issue comes when I try to aggregate these results across vendors (as I did before with ukOnly).
I already have the following function being used:
table.contains = function(t, value) -- Finds if "value" exists inside the table "t"
for index = 1, #t do
if t[index] == value then
return index
end
end
end
And was thinking of creating this:
table.overlap = function(t,g) -- Finds if tables "g" and "t" have any overlapping values
for i=1,#t do
if table.contains(g,t[i]) then
return true
else
return false
end
end
end
But I'm just not sure where to go from there.
You can assume I have already got a list of unique sources for each vendor and I don't mind if we're over restrictive. I.e. if any sources overlap between the two vendors, that would invalidate the entire result.
You can also assume I have each "fact": suppliesChoc, suppliesCereal, numSources and suppliesSweet for each vendor returned separately.
I believe your looking for the intersection of two sets.
https://en.wikipedia.org/wiki/Intersection_(set_theory)
One set being your vendor's suppliers and the other being the suppliers who supply sweets.
local vendors = {
Asda = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true, Pampers = true, Simple = true},
Tesco = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true},
Spar ={Nestle = true, Johnsons = true, Pampers = true, Simple = true}
}
function intersection(s1, s2)
local output = {}
for key in pairs(s1) do
output[#output + 1] = s2[key]
end
return output
end
local sweetSuppliers = {Kellogg = true, Cadbury = true, Nestle = true}
for name, suppliers in pairs(vendors) do
local result = intersection(sweetSuppliers, suppliers)
print(name .. " has " .. #result .. " sweets suppliers")
end
Here are some examples of a libraries for handling sets:
odkr's properset.lua
Windower's sets.lua
Both can give you an idea of how you can use sets to accomplish things like intersection, and much more

'int' object is not iterable - while not loop with booleans

I have the following problem:
I want to use a while not loop under a true-false condition. I define the condition and tell the loop what to do while the condition is True and False respectively. However, in the line 'converge = False, j = 1' (please see code below), I get the message 'int' object is not iterable. Can you help please? I don't know what the problem here is as I am fairly new to python.
Condition that the model run should stop at steady state (when the slope does not change anymore)
Final_pools_2 = [pools_2[-1,:]]
converge = False, j = 1
while not converge:
parset_2[-6:] = Final_pools_2
j = j+1
fluxes_2,pools_2 = f2py.dalec(fluxes_2,pools_2,drivers,-10.,deltat,removal,fires,parset_2,1)
Final_pools_2.append(pools_2[-1,:])
test = 1-np.abs(Final_pools_2[-1]/parset_2[-6:])
if test > 0.001:
converge = True
Final_pools_2 = np.array(Final_pools_2)
Thanks for your help :)
You cant inialise two variables of different types on one line.
Change it to:
converge = False
j = 1

Sequence of dictionaries in python

I am trying to create a sequence of similar dictionaries to further store them in a tuple. I tried two approaches, using and not using a for loop
Without for loop
dic0 = {'modo': lambda x: x[0]}
dic1 = {'modo': lambda x: x[1]}
lst = []
lst.append(dic0)
lst.append(dic1)
tup = tuple(lst)
dic0 = tup[0]
dic1 = tup[1]
f0 = dic0['modo']
f1 = dic1['modo']
x = np.array([0,1])
print (f0(x) , f1(x)) # 0 , 1
With a for loop
lst = []
for j in range(0,2):
dic = {}
dic = {'modo': lambda x: x[j]}
lst.insert(j,dic)
tup = tuple(lst)
dic0 = tup[0]
dic1 = tup[1]
f0 = dic0['modo']
f1 = dic1['modo']
x = np.array([0,1])
print (f0(x) , f1(x)) # 1 , 1
I really don't understand why I am getting different results. It seems that the last dictionary I insert overwrite the previous ones, but I don't know why (the append method does not work neither).
Any help would be really welcomed
This is happening due to how scoping works in this case. Try putting j = 0 above the final print statement and you'll see what happens.
Also, you might try
from operator import itemgetter
lst = [{'modo': itemgetter(j)} for j in range(2)]
You have accidentally created what is know as a closure. The lambda functions in your second (loop-based) example include a reference to a variable j. That variable is actually the loop variable used to iterate your loop. So the lambda call actually produces code with a reference to "some variable named 'j' that I didn't define, but it's around here somewhere."
This is called "closing over" or "enclosing" the variable j, because even when the loop is finished, there will be this lambda function you wrote that references the variable j. And so it will never get garbage-collected until you release the references to the lambda function(s).
You get the same value (1, 1) printed because j stops iterating over the range(0,2) with j=1, and nothing changes that. So when your lambda functions ask for x[j], they're asking for the present value of j, then getting the present value of x[j]. In both functions, the present value of j is 1.
You could work around this by creating a make_lambda function that takes an index number as a parameter. Or you could do what #DavisYoshida suggested, and use someone else's code to create the appropriate closure for you.

Google Sheets; arrayformular(if x=1 write 1 BUT if x=2 write 2)

I've got this working:
=IF(AND(B3 = "Hverdag");"Hverdag";IF(AND(B3 = "Weekend");"Weekend";""))
What I want is:
=ARRAYFORMULA(IF(AND(B3:B = "Hverdag");"Hverdag";IF(AND(B3:B = "Weekend");"Weekend";"")))
Did I miss something or is there a better way doing this math?
I'm not sure why the AND functions are needed since there is only one condition being tested.
Does this amended version work as you want:
=ArrayFormula(IF(B3:B = "Hverdag";"Hverdag";IF(B3:B = "Weekend";"Weekend";"")))

Erlang record item list

For example i have erlang record:
-record(state, {clients
}).
Can i make from clients field list?
That I could keep in client filed as in normal list? And how can i add some values in this list?
Thank you.
Maybe you mean something like:
-module(reclist).
-export([empty_state/0, some_state/0,
add_client/1, del_client/1,
get_clients/1]).
-record(state,
{
clients = [] ::[pos_integer()],
dbname ::char()
}).
empty_state() ->
#state{}.
some_state() ->
#state{
clients = [1,2,3],
dbname = "QA"}.
del_client(Client) ->
S = some_state(),
C = S#state.clients,
S#state{clients = lists:delete(Client, C)}.
add_client(Client) ->
S = some_state(),
C = S#state.clients,
S#state{clients = [Client|C]}.
get_clients(#state{clients = C, dbname = _D}) ->
C.
Test:
1> reclist:empty_state().
{state,[],undefined}
2> reclist:some_state().
{state,[1,2,3],"QA"}
3> reclist:add_client(4).
{state,[4,1,2,3],"QA"}
4> reclist:del_client(2).
{state,[1,3],"QA"}
::[pos_integer()] means that the type of the field is a list of positive integer values, starting from 1; it's the hint for the analysis tool dialyzer, when it performs type checking.
Erlang also allows you use pattern matching on records:
5> reclist:get_clients(reclist:some_state()).
[1,2,3]
Further reading:
Records
Types and Function Specifications
dialyzer(1)
#JUST MY correct OPINION's answer made me remember that I love how Haskell goes about getting the values of the fields in the data type.
Here's a definition of a data type, stolen from Learn You a Haskell for Great Good!, which leverages record syntax:
data Car = Car {company :: String
,model :: String
,year :: Int
} deriving (Show)
It creates functions company, model and year, that lookup fields in the data type. We first make a new car:
ghci> Car "Toyota" "Supra" 2005
Car {company = "Toyota", model = "Supra", year = 2005}
Or, using record syntax (the order of fields doesn't matter):
ghci> Car {model = "Supra", year = 2005, company = "Toyota"}
Car {company = "Toyota", model = "Supra", year = 2005}
ghci> let supra = Car {model = "Supra", year = 2005, company = "Toyota"}
ghci> year supra
2005
We can even use pattern matching:
ghci> let (Car {company = c, model = m, year = y}) = supra
ghci> "This " ++ c ++ " " ++ m ++ " was made in " ++ show y
"This Toyota Supra was made in 2005"
I remember there were attempts to implement something similar to Haskell's record syntax in Erlang, but not sure if they were successful.
Some posts, concerning these attempts:
In Response to "What Sucks About Erlang"
Geeking out with Lisp Flavoured Erlang. However I would ignore parameterized modules here.
It seems that LFE uses macros, which are similar to what provides Scheme (Racket, for instance), when you want to create a new value of some structure:
> (define-struct car (company model year))
> (define supra (make-car "Toyota" "Supra" 2005))
> (car-model supra)
"Supra"
I hope we'll have something close to Haskell record syntax in the future, that would be really practically useful and handy.
Yasir's answer is the correct one, but I'm going to show you WHY it works the way it works so you can understand records a bit better.
Records in Erlang are a hack (and a pretty ugly one). Using the record definition from Yasir's answer...
-record(state,
{
clients = [] ::[pos_integer()],
dbname ::char()
}).
...when you instantiate this with #state{} (as Yasir did in empty_state/0 function), what you really get back is this:
{state, [], undefined}
That is to say your "record" is just a tuple tagged with the name of the record (state in this case) followed by the record's contents. Inside BEAM itself there is no record. It's just another tuple with Erlang data types contained within it. This is the key to understanding how things work (and the limitations of records to boot).
Now when Yasir did this...
add_client(Client) ->
S = some_state(),
C = S#state.clients,
S#state{clients = [Client|C]}.
...the S#state.clients bit translates into code internally that looks like element(2,S). You're using, in other words, standard tuple manipulation functions. S#state.clients is just a symbolic way of saying the same thing, but in a way that lets you know what element 2 actually is. It's syntactic saccharine that's an improvement over keeping track of individual fields in your tuples in an error-prone way.
Now for that last S#state{clients = [Client|C]} bit, I'm not absolutely positive as to what code is generated behind the scenes, but it is likely just straightforward stuff that does the equivalent of {state, [Client|C], element(3,S)}. It:
tags a new tuple with the name of the record (provided as #state),
copies the elements from S (dictated by the S# portion),
except for the clients piece overridden by {clients = [Client|C]}.
All of this magic is done via a preprocessing hack behind the scenes.
Understanding how records work behind the scenes is beneficial both for understanding code written using records as well as for understanding how to use them yourself (not to mention understanding why things that seem to "make sense" don't work with records -- because they don't actually exist down in the abstract machine...yet).
If you are only adding or removing single items from the clients list in the state you could cut down on typing with a macro.
-record(state, {clients = [] }).
-define(AddClientToState(Client,State),
State#state{clients = lists:append([Client], State#state.clients) } ).
-define(RemoveClientFromState(Client,State),
State#state{clients = lists:delete(Client, State#state.clients) } ).
Here is a test escript that demonstrates:
#!/usr/bin/env escript
-record(state, {clients = [] }).
-define(AddClientToState(Client,State),
State#state{clients = lists:append([Client], State#state.clients)} ).
-define(RemoveClientFromState(Client,State),
State#state{clients = lists:delete(Client, State#state.clients)} ).
main(_) ->
%Start with a state with a empty list of clients.
State0 = #state{},
io:format("Empty State: ~p~n",[State0]),
%Add foo to the list
State1 = ?AddClientToState(foo,State0),
io:format("State after adding foo: ~p~n",[State1]),
%Add bar to the list.
State2 = ?AddClientToState(bar,State1),
io:format("State after adding bar: ~p~n",[State2]),
%Add baz to the list.
State3 = ?AddClientToState(baz,State2),
io:format("State after adding baz: ~p~n",[State3]),
%Remove bar from the list.
State4 = ?RemoveClientFromState(bar,State3),
io:format("State after removing bar: ~p~n",[State4]).
Result:
Empty State: {state,[]}
State after adding foo: {state,[foo]}
State after adding bar: {state,[bar,foo]}
State after adding baz: {state,[baz,bar,foo]}
State after removing bar: {state,[baz,foo]}