Using IndexMap with serde_json - rust

I would like to use serde_json to parse a .json file into a IndexMap. However it says the Deserialize trait has not been implemented for IndexMap. Is there a way to use serde_json with IndexMap?
let elements: IndexMap<String, Value> = serde_json::from_str(r#"
{
"Hydrogen (H) ": 1,
"Helium (He)": 4,
"Lithium (Li)": 7,
"Beryllium (Be)": 9,
"Boron (B) ": 11,
"Carbon (C) ": 12,
"Nitrogen (N) ": 14,
"Oxygen (O) ": 16,
"Flourine (F) ": 19,
"Neon (Ne)": 20
}
"#).expect("err: unable to parse json");

To deserialize index map you need either feature flag serde or serde-1.
main.rs
use indexmap::IndexMap;
use serde_json::Value;
fn main() {
let elements: IndexMap<String, Value> = serde_json::from_str(r#"
{
"Hydrogen (H) ": 1,
"Helium (He)": 4,
"Lithium (Li)": 7,
"Beryllium (Be)": 9,
"Boron (B) ": 11,
"Carbon (C) ": 12,
"Nitrogen (N) ": 14,
"Oxygen (O) ": 16,
"Flourine (F) ": 19,
"Neon (Ne)": 20
}
"#).expect("err: unable to parse json");
eprintln!("{:?}", elements);
}
Cargo.toml
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
indexmap = {version = "1.9.1", features = ["serde-1"]}

Related

Avro Rust unsupported union of null and fixed

I have an avro created by a Java code. I am trying to read this with Rust reader and getting this error
Result::unwrap()` on an `Err` value: DeserializeValue("unsupported union: Union(1, Fixed(4, [192, 168, 1, 2]))")'
It seems that union of "null" and "fixed" is not implemented yet.
The code in Rust:
let bytes = read(path).unwrap();
let schema_str = read_to_string("sw.schema.avsc").unwrap();
let schema = Schema::parse_str(&*schema_str).unwrap();
let reader = Reader::with_schema(&schema, &*bytes).unwrap();
println!(">>> {}", schema.canonical_form());
for record in reader {
let r = record.unwrap();
// it fails here on parsing data
let record: Flow = apache_avro::from_value::<Flow>(&r).unwrap();
println!("{:?}", record);
}
related schema definition for the field
"name": "ip",
"type": [
"null",
{
"type": "fixed",
"name": "IPv4",
"size": 4
},
{
"type": "fixed",
"name": "IPv6",
"size": 16
}
],
"default": null
Is it something I can parse somehow else or just report this as a Apache Avro Rust code bug?

Groovy to round all array decimal places down and return unique number count?

Thank you aspok for your help!
My goal is to get my list to be [3, 3, 4] and then get a count of unique values within it. Can anyone point me in the right direction for doing this?
My script consumes a JSON and puts all F4211_LNID values into a list. [3.1, 3.9, 4]. I need to now round all decimal places down.
I'm not sure if it's doable, but I am trying to use Math.floor(intListItems) to round my array values down. When I try this I receive the following error: Exception No signature of method: static java.lang.Math.floor() is applicable for argument types: (ArrayList) values: [[3.1, 3.9, 4]] Possible solutions: floor(double), log(double), find(), macro(groovy.lang.Closure), acos(double), cos(double)
I see my simplified list in the error, but I can't get it to round down and not sure what the error means.
(UPDATED) My Working Groovy
// Read Input Values
String aInputJson = aInputMap.InputJson ?: "{}"
// Initialize Output Values
def intListItems = []
def uniqueCount = 0
// Parse JSON
def json = new JsonSlurper().parseText( aInputJson )
// Determine Row Numbers
def rowset = json?.fs_DATABROWSE_F4211?.data?.gridData?.rowset
intListItems = rowset.collect{ Math.floor(it.F4211_LNID) }
intListItems.unique()
uniqueCount = intListItems.size()
JSON I am using.
{
"fs_DATABROWSE_F4211": {
"title": "Data Browser - F4211 [Sales Order Detail File]",
"data": {
"gridData": {
"id": 58,
"fullGridId": "58",
"rowset": [
{
"F4211_LNTY": "S",
"F4211_CPNT": 0,
"F4211_MCU": " 114000",
"F4211_DSC2": "NAS133N3EK166",
"F4211_NXTR": "580",
"F4211_LNID": 3.1,
"F4211_DOCO": 2845436
},
{
"F4211_LNTY": "S",
"F4211_CPNT": 0,
"F4211_MCU": " 114000",
"F4211_DSC2": "NAS133N3EK166",
"F4211_NXTR": "580",
"F4211_LNID": 3.9,
"F4211_DOCO": 2845436
},
{
"F4211_LNTY": "S",
"F4211_CPNT": 0,
"F4211_MCU": " 114000",
"F4211_DSC2": "NAS133N3EK166",
"F4211_NXTR": "580",
"F4211_LNID": 4,
"F4211_DOCO": 2845436
}
],
"summary": {
"records": 1,
"moreRecords": false
}
}
},
"errors": [],
"warnings": []
},
"currentApp": "DATABROWSE_F4211",
"timeStamp": "2000-06-01:09.42.02",
"sysErrors": []
}
You are getting the error Exception No signature of method: static java.lang.Math.floor() is applicable for argument types: (ArrayList) because there is no version of Math.floor() that accepts a List as a parameter.
Instead, you need to call Math.floor() on each individual item in the list. The easiest way to do this is in the collect { } call you are already doing.
def flooredList = rowset.collect { Math.floor(it.F4211_LNID) }
assert flooredList == [3.0, 3.0, 4.0]

Comparing the values of common keys in multiple Nested Dictionaries

Below is the Input_dict for data analysis:
input_dict =
{
"C:\\arm64\\lib_apple.so": { "func-abc": [5,6,7,8], "func-123":[1,1,1,1] },
"C:\\arm64\\lib_banana.so": { "func-123": [2,3,4], "func-rt": [0,0] },
"C:\\armeabi\\lib_banana.so": { "func-123": [1,0,0], "func-rt": [1,5] },
"C:\\armeabi\\lib2.so": { "func-0": [1]},
"C:\\x86\\lib_apple.so": { "func-so": [5,6,7,8], "func-123": [2,2,1,1] },
"C:\\x86\\lib_banana.so": { "func-rt": [2,0] },
"C:\\x86\\lib2.so": { "func-0": [1,2,3]}
}
The aim is to compare the 'values' of functions with same name of different architectures(arm64,armeabi,x86).
In other words, I want to compare the "lists" of functions(with same name) in different libraries(.so) files.
For example: Comparing func-123: [2,3,4] with func-123: [1,0,0] from arm64\lib_banana.so and armeabi\lib_banana.so respectively.
One of the desired output could be:
{ lib_apple.so: { func-123: [arm64,[1,1,1,1]],[x86,[2,2,1,1]]}}
You can restructure your function data to order by their name first, then supported architectures. Afterwards, print out those functions that appear in multiple architectures:
from collections import defaultdict
from pathlib import PureWindowsPath
lib2func = {
r'C:\arm64\lib_apple.so': { 'func-abc': [5,6,7,8], 'func-123': [1,1,1,1] },
r'C:\arm64\lib_banana.so': { 'func-123': [2,3,4], 'func-rt': [0,0] },
r'C:\armeabi\lib_banana.so': { 'func-123': [1,0,0], 'func-rt': [1,5] },
r'C:\armeabi\lib.so': {},
r'C:\armeabi\lib2.so': { 'func-0': [1]},
r'C:\x86\lib_apple.so': { 'func-so': [5,6,7,8], 'func-123': [2,2,1,1] },
r'C:\x86\lib_banana.so': { 'func-rt': [2,0] },
r'C:\x86\lib2.so': { 'func-0': [1,2,3] },
}
# restructure
func2arch = defaultdict(dict)
for lib_path, functions in lib2func.items():
path = PureWindowsPath(lib_path)
lib = path.name
arch = path.parent.name
for func_name, func_val in functions.items():
func2arch[(lib, func_name)][arch] = func_val
# find functions defined for multiple architectures
for (lib, func_name), arch_data in func2arch.items():
if len(arch_data) < 2:
continue # skip functions that only appear once
print(lib, func_name, arch_data)
gives
lib_apple.so func-123 {'arm64': [1, 1, 1, 1], 'x86': [2, 2, 1, 1]}
lib_banana.so func-123 {'arm64': [2, 3, 4], 'armeabi': [1, 0, 0]}
lib_banana.so func-rt {'arm64': [0, 0], 'armeabi': [1, 5], 'x86': [2, 0]}
lib2.so func-0 {'armeabi': [1], 'x86': [1, 2, 3]}
The above code assumes that library/function name pairs are unique.

Mongoose/Node: selecting element WITHOUT field/column name

So I have document like this
datatable: [{
data:[
["ABC", 123, 10, 1],
["ABC", 121, 10, 1],
["DDE", 13, 10, 1],
["OPP", 523, 10, 1]
]
}]
I want to select with a parameter "ABC" and would return arrays only with "ABC" like this:
datatable: [{
data:[
["ABC", 123, 10, 1],
["ABC", 121, 10, 1]
]
}]
Im starting with this code:
router.get("/", (req, res) => {
model.find({}).then(val=> {
res.send(val)
})
})
I cant find ways to find the value without the fieldname.
I tried using $elemMatch. Other ways needs a matching column name with the value.

Use AQL Variables e.g. for counting (LET sum = sum + 1)

According to https://www.arangodb.com/2014/07/13/arangodb-2-2-0-released it shall be possible to use statements like this:
LET sum = 0
FOR v IN values
SORT v.year
LET sum = sum + v.value
RETURN { year: v.year, value: v.value, sum: sum }
I currently use version 2.4 but am not able to use it, e.g. in such a statement:
LET sum = 0
FOR i in memoryColl
LET sum = sum + 1
// sum = sum + 1
RETURN { "i": i, "sum": sum }
I got the error
[1511] variable 'sum' is assigned multiple times (while parsing)
Can somebody tell me if such a statemtn should in principle work, and how exactly?
As explained in the upgrading docs for 2.3, it's no longer possible to update variables in queries:
Previous versions of ArangoDB allowed the modification of variables inside AQL
queries [...]
While this is admittedly a convenient feature, the new query optimizer design did not
allow to keep it.
Additionally, updating variables inside a query would prevent a lot
of optimizations to queries that we would like the optimizer to make. Additionally,
updating variables in queries that run on different nodes in a cluster would like cause
non-deterministic behavior because queries are not executed linearly.
To enumerate documents, you could do
LET range = 0..LENGTH(memoryColl)-1
FOR i IN range
RETURN {i: i+1, doc: memoryColl[i]}
but it looks like a really bad idea to me. Better return the documents and let the client enumerate them.
If you actually want to count the number of documents, you may use a sub-query:
LET result = (
FOR doc IN memoryColl
FILTER True // add some condition here for instance
RETURN doc
)
RETURN LENGTH(result)
In 2.4, it is also possible to count more efficiently:
http://jsteemann.github.io/blog/2014/12/12/aql-improvements-for-24/
On arango 3.7 in 2020 you could do something like described here
LET values = [
{ year: 2019, value: 35 },
{ year: 2017, value: 8 },
{ year: 2018, value: 17 },
{ year: 2020, value: 84 }
]
LET sortedValues = (FOR v IN values SORT v.year RETURN v)
FOR i IN 0..LENGTH(sortedValues)-1
LET v = sortedValues[i]
LET sum = sortedValues[i].value + SUM(SLICE(sortedValues, 0, i)[*].value)
RETURN {year:v.year,value:v.value,sum:sum}
This returned
[
{
"year": 2017,
"value": 8,
"sum": 8
},
{
"year": 2018,
"value": 17,
"sum": 25
},
{
"year": 2019,
"value": 35,
"sum": 60
},
{
"year": 2020,
"value": 84,
"sum": 144
}
]

Resources