diff --git a/Manifest.toml b/Manifest.toml index 7cb69c2..a74bf74 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -1,6 +1,6 @@ # This file is machine-generated - editing it directly is not advised -julia_version = "1.10.6" +julia_version = "1.11.2" manifest_format = "2.0" project_hash = "75c6a269a13b222c106479d2177b05facfa23f74" @@ -12,13 +12,15 @@ version = "1.1.3" [[deps.ArgTools]] uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" -version = "1.1.1" +version = "1.1.2" [[deps.Artifacts]] uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" +version = "1.11.0" [[deps.Base64]] uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" +version = "1.11.0" [[deps.CSV]] deps = ["CodecZlib", "Dates", "FilePathsBase", "InlineStrings", "Mmap", "Parsers", "PooledArrays", "PrecompileTools", "SentinelArrays", "Tables", "Unicode", "WeakRefStrings", "WorkerUtilities"] @@ -77,16 +79,18 @@ version = "1.0.0" [[deps.Dates]] deps = ["Printf"] uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" +version = "1.11.0" [[deps.Distributed]] deps = ["Random", "Serialization", "Sockets"] uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" +version = "1.11.0" [[deps.Distributions]] deps = ["AliasTables", "FillArrays", "LinearAlgebra", "PDMats", "Printf", "QuadGK", "Random", "SpecialFunctions", "Statistics", "StatsAPI", "StatsBase", "StatsFuns"] -git-tree-sha1 = "d7477ecdafb813ddee2ae727afa94e9dcb5f3fb0" +git-tree-sha1 = "3101c32aab536e7a27b1763c0797dba151b899ad" uuid = "31c24e10-a181-5473-b8eb-7969acd0382f" -version = "0.25.112" +version = "0.25.113" [deps.Distributions.extensions] DistributionsChainRulesCoreExt = "ChainRulesCore" @@ -125,6 +129,7 @@ version = "0.9.22" [[deps.FileWatching]] uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee" +version = "1.11.0" [[deps.FillArrays]] deps = ["LinearAlgebra"] @@ -141,12 +146,13 @@ weakdeps = ["PDMats", "SparseArrays", "Statistics"] [[deps.Future]] deps = ["Random"] uuid = "9fa8497b-333b-5362-9e8d-4d0656e87820" +version = "1.11.0" [[deps.HypergeometricFunctions]] deps = ["LinearAlgebra", "OpenLibm_jll", "SpecialFunctions"] -git-tree-sha1 = "7c4195be1649ae622304031ed46a2f4df989f1eb" +git-tree-sha1 = "b1c2585431c382e3fe5805874bda6aea90a95de9" uuid = "34004b35-14d8-5ef3-9330-4cdb6864b03a" -version = "0.3.24" +version = "0.3.25" [[deps.InlineStrings]] git-tree-sha1 = "45521d31238e87ee9f9732561bfee12d4eebd52d" @@ -164,6 +170,7 @@ version = "1.4.2" [[deps.InteractiveUtils]] deps = ["Markdown"] uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +version = "1.11.0" [[deps.InvertedIndices]] git-tree-sha1 = "0dc7b50b8d436461be01300fd8cd45aa0274b038" @@ -211,16 +218,17 @@ version = "0.6.4" [[deps.LibCURL_jll]] deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" -version = "8.4.0+0" +version = "8.6.0+0" [[deps.LibGit2]] deps = ["Base64", "LibGit2_jll", "NetworkOptions", "Printf", "SHA"] uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" +version = "1.11.0" [[deps.LibGit2_jll]] deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll"] uuid = "e37daf67-58a4-590a-8e99-b0245dd2ffc5" -version = "1.6.4+0" +version = "1.7.2+0" [[deps.LibSSH2_jll]] deps = ["Artifacts", "Libdl", "MbedTLS_jll"] @@ -229,10 +237,12 @@ version = "1.11.0+1" [[deps.Libdl]] uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" +version = "1.11.0" [[deps.LinearAlgebra]] deps = ["Libdl", "OpenBLAS_jll", "libblastrampoline_jll"] uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +version = "1.11.0" [[deps.LogExpFunctions]] deps = ["DocStringExtensions", "IrrationalConstants", "LinearAlgebra"] @@ -252,6 +262,7 @@ version = "0.3.28" [[deps.Logging]] uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" +version = "1.11.0" [[deps.MQTTClient]] deps = ["Distributed", "Random", "Sockets"] @@ -266,11 +277,12 @@ weakdeps = ["PrecompileTools"] [[deps.Markdown]] deps = ["Base64"] uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" +version = "1.11.0" [[deps.MbedTLS_jll]] deps = ["Artifacts", "Libdl"] uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" -version = "2.28.2+1" +version = "2.28.6+0" [[deps.Missings]] deps = ["DataAPI"] @@ -280,10 +292,11 @@ version = "1.2.0" [[deps.Mmap]] uuid = "a63ad114-7e13-5084-954f-fe012c677804" +version = "1.11.0" [[deps.MozillaCACerts_jll]] uuid = "14a3606d-f60d-562e-9121-12d972cd8159" -version = "2023.1.10" +version = "2023.12.12" [[deps.NetworkOptions]] uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" @@ -292,7 +305,7 @@ version = "1.2.0" [[deps.OpenBLAS_jll]] deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"] uuid = "4536629a-c528-5b80-bd46-f80d51c5b363" -version = "0.3.23+4" +version = "0.3.27+1" [[deps.OpenLibm_jll]] deps = ["Artifacts", "Libdl"] @@ -306,9 +319,9 @@ uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e" version = "0.5.5+0" [[deps.OrderedCollections]] -git-tree-sha1 = "dfdf5519f235516220579f949664f1bf44e741c5" +git-tree-sha1 = "12f1439c4f986bb868acda6ea33ebc78e19b95ad" uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" -version = "1.6.3" +version = "1.7.0" [[deps.PDMats]] deps = ["LinearAlgebra", "SparseArrays", "SuiteSparse"] @@ -323,9 +336,15 @@ uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" version = "2.8.1" [[deps.Pkg]] -deps = ["Artifacts", "Dates", "Downloads", "FileWatching", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] +deps = ["Artifacts", "Dates", "Downloads", "FileWatching", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "Random", "SHA", "TOML", "Tar", "UUIDs", "p7zip_jll"] uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" -version = "1.10.0" +version = "1.11.0" + + [deps.Pkg.extensions] + REPLExt = "REPL" + + [deps.Pkg.weakdeps] + REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" [[deps.PooledArrays]] deps = ["DataAPI", "Future"] @@ -359,6 +378,7 @@ version = "2.4.0" [[deps.Printf]] deps = ["Unicode"] uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" +version = "1.11.0" [[deps.PtrArrays]] git-tree-sha1 = "77a42d78b6a92df47ab37e177b2deac405e1c88f" @@ -377,13 +397,10 @@ version = "2.11.1" [deps.QuadGK.weakdeps] Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" -[[deps.REPL]] -deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] -uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" - [[deps.Random]] deps = ["SHA"] uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +version = "1.11.0" [[deps.Reexport]] git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b" @@ -414,9 +431,11 @@ version = "1.4.7" [[deps.Serialization]] uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" +version = "1.11.0" [[deps.Sockets]] uuid = "6462fe0b-24de-5631-8697-dd941f90decc" +version = "1.11.0" [[deps.SortingAlgorithms]] deps = ["DataStructures"] @@ -427,7 +446,7 @@ version = "1.2.1" [[deps.SparseArrays]] deps = ["Libdl", "LinearAlgebra", "Random", "Serialization", "SuiteSparse_jll"] uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" -version = "1.10.0" +version = "1.11.0" [[deps.SpecialFunctions]] deps = ["IrrationalConstants", "LogExpFunctions", "OpenLibm_jll", "OpenSpecFun_jll"] @@ -442,9 +461,14 @@ version = "2.4.0" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" [[deps.Statistics]] -deps = ["LinearAlgebra", "SparseArrays"] +deps = ["LinearAlgebra"] +git-tree-sha1 = "ae3bb1eb3bba077cd276bc5cfc337cc65c3075c0" uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" -version = "1.10.0" +version = "1.11.1" +weakdeps = ["SparseArrays"] + + [deps.Statistics.extensions] + SparseArraysExt = ["SparseArrays"] [[deps.StatsAPI]] deps = ["LinearAlgebra"] @@ -491,7 +515,7 @@ uuid = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9" [[deps.SuiteSparse_jll]] deps = ["Artifacts", "Libdl", "libblastrampoline_jll"] uuid = "bea87d4a-7f5b-5778-9afe-8cc45184846c" -version = "7.2.1+1" +version = "7.7.0+0" [[deps.TOML]] deps = ["Dates"] @@ -523,9 +547,11 @@ version = "0.11.3" [[deps.UUIDs]] deps = ["Random", "SHA"] uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" +version = "1.11.0" [[deps.Unicode]] uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" +version = "1.11.0" [[deps.WeakRefStrings]] deps = ["DataAPI", "InlineStrings", "Parsers"] @@ -551,7 +577,7 @@ version = "5.11.0+0" [[deps.nghttp2_jll]] deps = ["Artifacts", "Libdl"] uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" -version = "1.52.0+1" +version = "1.59.0+0" [[deps.p7zip_jll]] deps = ["Artifacts", "Libdl"] diff --git a/src/util.jl b/src/util.jl index b7a12e8..88fe060 100644 --- a/src/util.jl +++ b/src/util.jl @@ -2,7 +2,8 @@ module util export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, replaceDictKeys, findMatchingDictKey, textToDict, randstring, randstrings, timeout, - dataframeToCSV, dfToVectorDict, disintegrate_vectorDict + dataframeToCSV, dfToVectorDict, disintegrate_vectorDict, getDataFrameValue, dfRowtoString, + dfToString, dataframe_to_json_list, dict_to_string using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames @@ -522,8 +523,188 @@ end +""" Get a value from a DataFrame row by a given key + +# Arguments + - `row::DataFrameRow` + The DataFrame row to retrieve the value from. + - `key::Symbol` + The column name (as a symbol) whose value is to be retrieved. + +# Return + - `Any` + The value of the specified column in the given row. + +# Example + ```jldoctest + julia> using DataFrames + + julia> df = DataFrame(name=["Alice", "Bob"], age=[25, 30]) + 2×2 DataFrame + Row │ name age + │ String Int64 + ┌─────┼─────────┼─────── + │ 1 │ Alice 25 + │ 2 │ Bob 30 + + julia> getDataFrameValue(df[1, :], :name) + "Alice" + ``` + +# Signature +""" +getDataFrameValue(row::DataFrameRow, key::Symbol) = row.:($key) +""" Convert a DataFrame row to a key:value string + +# Arguments + - `row::DataFrameRow` + The DataFrame row to convert. + +# Return + - `String` + A string containing the formatted representation of the row, with each column prefixed by its name and separated by commas. + +# Example + ```jldoctest + julia> using DataFrames + + julia> df = DataFrame(name=["Alice", "Bob"], age=[25, 30]) + 2×2 DataFrame + Row │ name age + │ String Int64 + ┌─────┼─────────┼─────── + │ 1 │ Alice 25 + │ 2 │ Bob 30 + + julia> dfRowtoString(df[1, :]) + "name: Alice, age: 25" + ``` + +# Signature +""" +function dfRowtoString(row::DataFrameRow)::String + str = "" + for key in keys(row) + value = getDataFrameValue(row, key) + str *= "$key: $value, " + end + result = str[1:end-2] # remove ", " at the end of row + return result +end + + +""" Convert a DataFrame to a string representation + +# Arguments + - `df::DataFrame` + The DataFrame to convert, where each row will be converted to a string. + +# Return + - `String` + A string containing the formatted representation of the DataFrame, with each row prefixed by its index and separated by newlines. + +# Example + ```jldoctest + julia> using DataFrames + + julia> df = DataFrame(name=["Alice", "Bob"], age=[25, 30]) + 2×2 DataFrame + Row │ name age + │ String Int64 + ┌─────┼─────────┼─────── + │ 1 │ Alice 25 + │ 2 │ Bob 30 + + julia> dfToString(df) + "1) name: Alice, age: 25\n2) name: Bob, age: 30" + ``` + +# Signature +""" +function dfToString(df::DataFrame) + dfstr = "" + for (i, row) in enumerate(eachrow(df)) + rowstr = dfRowtoString(row) + dfstr *= "$i) $rowstr\n" + end + return dfstr +end + + +""" Convert a DataFrame to a list of JSON strings + +# Arguments + - `df::DataFrame` + The DataFrame to convert, where each row will be converted to a JSON string. + +# Return + - `Vector{String}` + A vector containing the JSON representation of each row in the DataFrame. + +# Example + ```jldoctest + julia> using DataFrames + + julia> df = DataFrame(name=["Alice", "Bob"], age=[25, 30]) + 2×2 DataFrame + Row │ name age + │ String Int64 + ┌─────┼─────────┼─────── + │ 1 │ Alice 25 + │ 2 │ Bob 30 + + julia> dataframe_to_json_list(df) + 2-element Vector{String}: + "{\"name\":\"Alice\",\"age\":25}" + "{\"name\":\"Bob\",\"age\":30}" + ``` + +# Signature +""" +function dataframe_to_json_list(df::DataFrame)::Vector{String} + json_list = [] + for row in eachrow(df) + json_row = Dict(zip(names(df), row)) + push!(json_list, JSON.json(json_row)) + end + return json_list +end + + +""" Convert a dictionary to a string representation. + +# Arguments + - `od::OrderedDict` + The OrderedDict to convert, where each key-value pair will be represented as "index) key: value". + +# Return + - `String` + A string containing the representation of each key-value pair in the OrderedDict. + +# Example + ```jldoctest + julia> using DataStructures + + julia> od = OrderedDict("name" => "Alice", "age" => 25) + OrderedDict{String,Any} with 2 entries: + "name" => "Alice" + "age" => 25 + + julia> dict_to_string(od) + "1) name: Alice, 2) age: 25" + ``` + +# Signature +""" +function dict_to_string(od::T) where {T<:AbstractDict} + items = [] + for (i, (key, value)) in enumerate(od) + push!(items, "$i) $key: $value") + end + return join(items, ", ") +end