How to load multiple values for same key in property files? - properties-file

See this property file:
S=1
M=2
[...]
IA=i
S=g
First, the value 1 will be assigned to S and then g will be assigned to S in the last line.
I want to keep multiple values for the same key S, how can I do this?

I think you need to specify whether this is a Java properties file or some other file, as I've rarely seen it possible to define two separate "keys" with different values without some kind of a section break (ie .ini files).
The only other way I can think of for reading this type of file would be to pull it in as nested dict using the alphabet as the index (a-z, aa-az, etc), storing the key value pairs you've seen, so for example you'd have "a" = {S='1'}, "b" = {M='2'} [..] "z" = {S='g'} and then you could do a query for "letterKey" in dict "alpha" where key.value("innerKey") = 'S', which would give you both 'a' and 'z' due to the 'S' = '1' and 'g'. This may not be any easier than simply rewriting some of the existing code though.
Since a dictionary can't have multiple keys with the same "index" ie 'S' can't appear twice, if you could do as a commenter suggested and store two values in an array in 'S' and reference them via position S.value([0]) and S.value([1]), you'd have a much better program overall.

Related

Building a (process) variable in Appian using the value of another one?

As far as I understand, it is not possible in Appian to dynamically construct (process) variable names, just like you would do e.g. with bash using backticks like MY_OBJECT=pv!MY_CONS_`extract(valueOfPulldown)`. Is that correct? Is there a workaround?
I have set of Appian constants, let's call them MY_CONS_FOO, MY_CONS_BAR, MY_CONS_LALA, all of which are e.g. refering to an Appian data store entity. I would like to write an Appian expression rule which populates another variable MY_OBJECT of the same type (here: data store entity), depending e.g. of the options of a pull-down menu having the possible options stored in an array MY_CONS_OPTIONS looking as follows
FOO
BAR
LALA
I could of course build a lengthy case-structure which I have to maintain in addition to MY_CONS_OPTIONS, so I am searching for a more dynanmic approach using the extract() function depending on valueOfPulldown as the chosen value of the pulldown-menu.
Edit: Here the expression-rule (in pseudo-code) I want to avoid:
if (valueOfPulldown = 'FOO') then MY_OBJECT=pv!MY_CONS_FOO
if (valueOfPulldown = 'BAR') then MY_OBJECT=pv!MY_CONS_BAR
if (valueOfPulldown = 'LALA') then MY_OBJECT=pv!MY_CONS_LALA
The goal is to be able to change the data store entity via pulldown-menu.
This can help you find what is behind your constant.
fn!typeName(fn!typeOf(cons!YOUR_CONSTANT)).
Having in mind additional details I would do as follows:
Create separate expression that will combine details into list of Dictionary like below:
Expression results (er):
{
{dd_label: "label1", dd_value: 1, cons: "cons!YOUR_CONSTANT1" }
,{dd_label: "label2", dd_value: 2, cons: "cons!YOUR_CONSTANT2" }
}
on UI for your dropdown control use er.dd_label as choiceLabels and er.dd_value as choiceValues
when user selects value on Dropdown save dropdown value to some local variable and then use it to find your const by doing:
property( index(er, wherecontains(local!dropdownselectedvalue, tointeger(er.dd_value))), "cons")
returned value of step 3 is your constant
This might not be perfect as you still have to maintain your dictionary but you can avoid long if...else statements.
As a alternative have a look on Decisions Tables in Appian https://docs.appian.com/suite/help/21.1/Appian_Decisions.html

Livecode: How do I program a button to create unique variables?

I apologize if this has been asked before (I couldn't find anything).
I'm an extreme noob in Livecode, and I want to know if there is a way of programming a button to create many new, unique variables and assign a value to them. I apologize if this is a dumb question.
Usually you use an array for that. An array is basically a list of things, where each thing is associated with an "index". An index can be any word, so you can use an array like a dictionary, where you'd e.g. have French words as the index, and English words as the value, like:
put "cow" into myDictionary["vache"]
But you can also just use numbers as the keys and make them a numbered list:
put "cow" into allMyAnimals[1]
put "duck" into allMyAnimals[2]
In end effect, you create one variable and put several things in it. For example if you had a loop that calculated something (in this example a number +100) and you wanted to have variables containing all those numbers, but named with 100 less, you'd do something like:
repeat with x = 1 to 250
put x +100 into twoHundredFiftyNumbersFrom101[x]
end repeat
And to read the first one:
answer "the first number is" && twoHundredFiftyNumbersFrom101[1]
Or all of them:
repeat with x = 1 to 250
answer twoHundredFiftyNumbersFrom101[x]
end repeat
Or whatever. You could also use 'do' to build the lines of code as a string, but then you have to make sure your variable names are generated in a fashion that makes them valid identifiers (e.g. have no spaces in them, no special characters). An array key can be any valid string, and the compiler can optimize them, and you can treat them as a whole and pass them between handlers.
Or you can do this "in the clear" with a "do" construction:
on mouseUp
repeat with y = 1 to 10
get random(100)
do "put it into onTheFlyVariable" & y
end repeat
end mouseUp
Step through this handler and watch the variables assemble themselves.

BACnet deserialization: How do I know if a new list elements starts

I'm implementing a generic BACnet decoder and came across the following question, of which I can't seem to find the answer within the BACnet standard. The chapter "20.2.1.3.2 Constructed Data" does not answer my question, or I might not fully understand it.
Let's assume I have a List (SEQUENCE OF) with elements of type Record (SEQUENCE).
Said record has 4 fields, identified by context tag, where field 0 and 1 are optional.
I further assume that the order, in which those fields are serialized, can be arbitrary (because they're identified by their context tags).
The data could look like that (number indicates field / column):
[{ "3", "0", 2" }, {"1", "2", "3"}]
Over the wire, the only "structure information" I assume I get are the open / close tags for the list.
That means:
Open Tag List
ctxTagColumn3, valueColumn3,
ctxTagColumn0, valueColumn0,
ctxTagColumn2, valueColumn2,
ctxTagColumn1, valueColumn1,
ctxTagColumn2, valueColumn2,
ctxTagColumn3, valueColumn3
Close Tag List
How do I know, after I've read the last column data ("2") of my first list item, that I must begin decoding the second item, starting with a value for column "1"?
Which of my assumptions is wrong?
Thank you and kind regards
Pascal
The order of elements of a SEQUENCE is always known and shall not be arbitrarily by definition. Further, not all conceivable combinations are possible to encode. Regarding BACnet, all type definitions shall be decodable universally.
Assuming I understand you correctly; the "order" cannot be "arbitrary"; i.e.:
SEQUENCE = *ordered* collection of variables of **different** types
SEQUENCE OF = *ordered* collection of variables of **same** type
The tag-number for the item (SD) context-tag will be different (/possibly an incremented value/maybe +1) from the containing (PD) context-tag; so you could check for that, or better still if the tag-number value is <= 5 (/'length' value) then it's a SD context-tag for one of your items, rather than a (/the closing) PD context tag (/'type' value) delimiting the end of your items.

Sort a list of custom objects multiple times by different object properties in vb.net

I want to sort a list of custom objects by multiple object properties.
For example, I have:
MyObject.A
MyObject.B
MyObject.C
I want to sort the list first by the values of property "A", then by B and then by C. All those properties are strings(that may or may not be equal to each other and may or may not consist of/contain number characters).
After digging through web I found something that worked for the case where I only needed to sort the list by one property (by "A" in this example):
MyList.Sort(Function(x, y) x.A.CompareTo(y.A))
That worked fine.
So after that, I figured I just need to do more sorts in correct order and I tried doing something like this:
MyList.Sort(Function(x, y) x.C.CompareTo(y.C))
MyList.Sort(Function(x, y) x.B.CompareTo(y.B))
MyList.Sort(Function(x, y) x.A.CompareTo(y.A))
Which kinda sometimes works and sometimes doesn't. If there are few list entries (<10), it works fine and, for example, if "A" values are equal, the list is sorted by "B" values and if those are equal, then by "C".
But, when I add more entries, it breaks down and only the last sort is correct.
Seems that each next sort doesn't retain the original order of entries it doesn't need to sort.
How would I sort something like this?
MyList = (MyList.OrderBy(Function(i) i.A).
ThenBy(Function(i) i.B).
ThenBy(Function(i) i.C)).ToList()
As to why your existing method did not work: that's the difference between a stable and an unstable sort. According to MSDN, the Sort() method unstable.

File operation is slower is there a faster look up method in Python?

I am storing the values of the form given below into a file:
143 800 'Ask'
213 457 'Comment'
424 800 'Hi'
The first column contains unique elements here.
However, the look up on the values of the first column is quite inefficient when I am storing it in file format, is there a more efficient way in Python for a faster look-up.
I am aware of dictionaries in python for accomplishing this, but I am looking for some other method. Since the data I have consits of trillions of records..therefore I can not keep them in dictionary in the RAM. Therefore, I am searching for some other method.
Also with each program exection the rows are going to be inserted in the case of databases, how to overcome that, an example of what I am getting confused about in databases is given below:
143 800 'Ask'
213 457 'Comment'
424 800 'Hi'
143 800 'Ask'
213 457 'Comment'
424 800 'Hi'
Here's a full code example using sqlite3, showing how to initialise the database, put data into it, and get a single row of data out.
import sqlite3
conn = sqlite3.connect(':memory:')
conn.execute("""CREATE TABLE Widget (id INTEGER PRIMARY KEY,
serial_number INTEGER,
description TEXT);""")
my_data = [ [143, 800, 'Ask'],
[213, 457, 'Comment'],
[424, 800, 'Hi'] ]
for row in my_data:
conn.execute("INSERT INTO Widget (id, serial_number, description) VALUES (?,?,?);" , row )
conn.commit() # save changes
res = conn.execute("SELECT * FROM Widget WHERE id=143")
row = res.next()
print row #prints (143, 800, u'Ask')
Note the use of the special filename :memory: to open a temporary database.
What you're asking for is probably called a "Database table" and an "Index". The classic approach is to have a supplementary file (index) which maps the keys of the data tuples in the table to absolute positions of the tuples in the file.
I don't understand, you want to be able to search faster in the file itself, or with the file content in python? In the latter, use a dictionary with the unique elements as key.
values = {143:[800,'ask'], 213:[457,'Comment'], 424:[800:'Hi']}
If you need to look things up in a persistent store, use a database. One example is sqlite, which is built-in.
Also with each program exection the rows are going to be inserted
If you want to keep the storage in a file, the way you do it, then the simple solution to prevent duplicate entries from appearing the next execution would be to simply truncate the file first. You can do this, by opening it with the w flag:
f = open('filename', 'w')
# ...
f.close()
However it sounds as if you just want to store some data while the program is executed, i.e. you want to keep data around without making it persistent. If that’s the case, then I am wondering why you actually store the contents in a file.
The more obvious way, which is also pythonic (although it’s not special to Python), would be to keep it in a dictionary during the program execution. A dictionary is mutable, so you can change its content all the time: You can add new entries, or even update entries if you later get more information on them.
I knew about this from of storing in dictionary but at times I don't have values for values[143][1] ie the string 'None' is stored in its place
That’s not a problem at all. You can easily store an entry with 143 as the key and None as its value, or even an array of None values:
>>> values[143] = [ None, None ]
That way, the dictionary will still remember that you entered the key, so a check if the key is in the dictionary will return true:
>>> 143 in values
True
Is there any other way other than dictionaries in python for accomplising the same, I was aware of dictionaries...I am just searching for some other way.
No, there usually is only one way to do something right in Python, as also told by the Zen of Python: “There should be one-- and preferably only one --obvious way to do it.”
As such, no, there is probably not an appropriate way to use dictionaries without dictionaries. But then again, why are you searching for some other way? It does not sound to me, as if you have a good reason to do so, and if you have, you have to make sure that you explain why certain ways are undesirable for you to use.