update
This commit is contained in:
130
src/util.jl
130
src/util.jl
@@ -1,9 +1,10 @@
|
||||
module util
|
||||
|
||||
export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, replaceDictKeys,
|
||||
findMatchingDictKey, textToDict, randstring, randstrings, timeout
|
||||
findMatchingDictKey, textToDict, randstring, randstrings, timeout,
|
||||
dataframeToCSV, dfToVectorDict, disintegrate_vectorDict
|
||||
|
||||
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient
|
||||
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames
|
||||
|
||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||
|
||||
@@ -412,6 +413,131 @@ function timeout(f::Function, timeoutwindow::Integer; fargs=nothing, timeoutmsg=
|
||||
timeoutmsg
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
""" Convert a dataframe into CSV.
|
||||
|
||||
# Arguments
|
||||
- `df::DataFrame`
|
||||
A connection object to Postgres database
|
||||
|
||||
# Return
|
||||
- `result::String`
|
||||
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> using DataFrames, GeneralUtils
|
||||
julia> df = DataFrame(A=1:3, B=5:7, fixed=1)
|
||||
julia> result = GeneralUtils.dataframeToCSV(df)
|
||||
```
|
||||
|
||||
# Signature
|
||||
"""
|
||||
function dataframeToCSV(df::DataFrame)
|
||||
# Create an IOBuffer to capture the output
|
||||
io = IOBuffer()
|
||||
CSV.write(io, df)
|
||||
dfStr = String(take!(io))
|
||||
return dfStr
|
||||
end
|
||||
|
||||
""" Convert a DataFrame into a list of Dict rows.
|
||||
|
||||
# Arguments
|
||||
- `df::DataFrame`
|
||||
The input DataFrame to be converted.
|
||||
|
||||
# Return
|
||||
- `rows::Vector{Dict{String, Any}}`
|
||||
A vector of dictionaries, where each dictionary represents a row in a dataframe.
|
||||
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> using DataFrames, JSON3, GeneralUtils
|
||||
julia> df = DataFrame(A = [1, 2, 3], B = ["apple", "banana", "cherry"])
|
||||
julia> vectorDict = GeneralUtils.dfToVectorDict(df)
|
||||
[Dict{String, Any}("B" => "apple", "A" => 1),
|
||||
Dict{String, Any}("B" => "banana", "A" => 2)
|
||||
Dict{String, Any}("B" => "cherry", "A" => 3)]
|
||||
```
|
||||
|
||||
# Signature
|
||||
"""
|
||||
function dfToVectorDict(df::DataFrame)
|
||||
vec = []
|
||||
for row in eachrow(df)
|
||||
d = Dict{String, Any}()
|
||||
for col in names(df)
|
||||
d[col] = row[col]
|
||||
end
|
||||
push!(vec, d)
|
||||
end
|
||||
return vec
|
||||
end
|
||||
|
||||
|
||||
|
||||
""" Turn a large vector of dictionaries into smaller one
|
||||
|
||||
# Arguments
|
||||
- `data`
|
||||
data to be partioning
|
||||
- `partsize`
|
||||
how many dicts per part
|
||||
|
||||
# Return
|
||||
- `parts`
|
||||
a dictionay of parts
|
||||
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> using GeneralUtils, Dates, JSON3, UUIDs
|
||||
julia> vecDict = [Dict("a" => i) for i in 1:10]
|
||||
julia> d = GeneralUtils.disintegrate_vectorDict(vecDict, 3)
|
||||
julia> println(d[:data])
|
||||
Dict{Int64, Vector{Dict}} with 4 entries:
|
||||
1 => [Dict("a"=>1), Dict("a"=>2), Dict("a"=>3)]
|
||||
2 => [Dict("a"=>4), Dict("a"=>5), Dict("a"=>6)]
|
||||
3 => [Dict("a"=>7), Dict("a"=>8), Dict("a"=>9)]
|
||||
4 => [Dict("a"=>10)]
|
||||
```
|
||||
|
||||
# Signature
|
||||
"""
|
||||
function disintegrate_vectorDict(data::Vector{Dict{T1, T2}}, partsize::Integer
|
||||
) where {T1<:Any, T2<:Any}
|
||||
parts = Dict{Int, Vector{Dict}}()
|
||||
for (i, dict) in enumerate(data)
|
||||
partkey = (i - 1) ÷ partsize + 1
|
||||
if !haskey(parts, partkey)
|
||||
parts[partkey] = Vector{Dict}()
|
||||
end
|
||||
push!(parts[partkey], dict)
|
||||
end
|
||||
return (datatype="vector{Dict}", totalparts=length(parts), partsize=partsize, data=parts)
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user