one(x)
one(T::type)
Return a multiplicative identity for x
: a value such that one(x)*x == x*one(x) == x
. Alternatively one(T)
can take a type T
, in which case one
returns a multiplicative identity for any x
of type T
.
If possible, one(x)
returns a value of the same type as x
, and one(T)
returns a value of type T
. However, this may not be the case for types representing dimensionful quantities (e.g. time in days), since the multiplicative identity must be dimensionless. In that case, one(x)
should return an identity value of the same precision (and shape, for matrices) as x
.
If you want a quantity that is of the same type as x
, or of type T
, even if x
is dimensionful, use oneunit
instead.
See also the identity
function, and I
in LinearAlgebra
for the identity matrix.
Examples
julia> one(3.7)
1.0
julia> one(Int)
1
julia> import Dates; one(Dates.Day(1))
1
all(itr) -> Bool
Test whether all elements of a boolean collection are true
, returning false
as soon as the first false
value in itr
is encountered (short-circuiting). To short-circuit on true
, use any
.
If the input contains missing
values, return missing
if all non-missing values are true
(or equivalently, if the input contains no false
value), following three-valued logic.
See also: all!
, any
, count
, &
, , &&
, allunique
.
Examples
julia> a = [true,false,false,true]
4-element Vector{Bool}:
1
0
0
1
julia> all(a)
false
julia> all((println(i); v) for (i, v) in enumerate(a))
1
2
false
julia> all([missing, false])
false
julia> all([true, missing])
missing
all(p, itr) -> Bool
Determine whether predicate p
returns true
for all elements of itr
, returning false
as soon as the first item in itr
for which p
returns false
is encountered (short-circuiting). To short-circuit on true
, use any
.
If the input contains missing
values, return missing
if all non-missing values are true
(or equivalently, if the input contains no false
value), following three-valued logic.
Examples
julia> all(i->(4<=i<=6), [4,5,6])
true
julia> all(i -> (println(i); i < 3), 1:10)
1
2
3
false
julia> all(i -> i > 0, [1, missing])
missing
julia> all(i -> i > 0, [-1, missing])
false
julia> all(i -> i > 0, [1, 2])
true
all(A; dims)
Test whether all values along the given dimensions of an array are true
.
Examples
julia> A = [true false; true true]
2×2 Matrix{Bool}:
1 0
1 1
julia> all(A, dims=1)
1×2 Matrix{Bool}:
1 0
julia> all(A, dims=2)
2×1 Matrix{Bool}:
0
1
all(p, A; dims)
Determine whether predicate p
returns true
for all elements along the given dimensions of an array.
Examples
julia> A = [1 -1; 2 2]
2×2 Matrix{Int64}:
1 -1
2 2
julia> all(i -> i > 0, A, dims=1)
1×2 Matrix{Bool}:
1 0
julia> all(i -> i > 0, A, dims=2)
2×1 Matrix{Bool}:
0
1
updatewith!(m::T, w::T)::T where {T<:AbstractModel}
Update the model m
with the values from w
and return the updated model.
Examples
julia> using UserApp.User
julia> SearchLight.updatewith!(user, Dict("name" => "John Doe", "email" => "foo@bar.com"))
julia> save!(user)
updatewith!(m::T, w::T)::T where {T<:AbstractModel}
Update the model m
with the values from w
and return the updated model.
Examples
julia> using UserApp.User
julia> SearchLight.updatewith!(user, Dict("name" => "John Doe", "email" => "foo@bar.com"))
julia> save!(user)
update_or_create(m::T; ignore = Symbol[], skip_update = false, filters...)::T where {T<:AbstractModel}
Examples
julia>
to_models(m::Type{T}, df::DataFrames.DataFrame)::Vector{T} where {T<:AbstractModel}
Return an array of type Model
Examples
julia> DataFrame(Stat, SQLWhereExpression("date >= ? AND date <= ?", startdate, enddate), order=["stats.date"])
8160×9 DataFrame
Row │ stats_id stats_package_uuid stats_package_name stats_status stats_region stats_date stats_request_count stats_year stats_month
│ Int64 String String Int64 String String Int64 Int64 String
──────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 1 00000000-1111-2222-3333-44444444… REPLTreeViews 200 cn-northeast 2021-11-25 1 2021 2021-11
2 │ 17 00701ae9-d1dc-5365-b64a-a3a3ebf5… BioAlignments 200 au 2021-11-25 1 2021 2021-11
3 │ 217 00701ae9-d1dc-5365-b64a-a3a3ebf5… BioAlignments 200 us-west 2021-11-25 1 2021 2021-11
4 │ 314 009559a3-9522-5dbb-924b-0b6ed2b2… XGBoost 200 cn-northeast 2021-11-25 1 2021 2021-11
5 │ 406 009559a3-9522-5dbb-924b-0b6ed2b2… XGBoost 200 eu-central 2021-11-25 5 2021 2021-11
6 │ 461 009559a3-9522-5dbb-924b-0b6ed2b2… XGBoost 200 sa 2021-11-25 1 2021 2021-11
⋮
8160 │ 623498 fff527a3-8410-504e-9ca3-60d5e79b… SimpleANOVA 200 eu-central 2021-11-25 1 2021 2021-11
julia> SearchLight.to_models(Stat, DataFrame(Stat, SQLWhereExpression("date >= ? AND date <= ?", startdate, enddate), order=["stats.date"]))
8160-element Vector{Stat}:
Stat
| KEY | VALUE |
|----------------------|--------------------------------------|
| date::Date | 2021-11-25 |
| id::DbId | 1 |
| month::String | 2021-11 |
| package_name::String | REPLTreeViews |
| package_uuid::String | 00000000-1111-2222-3333-444444444444 |
| region::String | cn-northeast |
| request_count::Int64 | 1 |
| status::Int64 | 200 |
| year::Int64 | 2021 |
⋮
Stat
| KEY | VALUE |
|----------------------|--------------------------------------|
| date::Date | 2021-11-25 |
| id::DbId | 623498 |
| month::String | 2021-11 |
| package_name::String | SimpleANOVA |
| package_uuid::String | fff527a3-8410-504e-9ca3-60d5e79bb1e4 |
| region::String | eu-central |
| request_count::Int64 | 1 |
| status::Int64 | 200 |
| year::Int64 | 2021 |
columns_from_joins(joins::Vector{SQLJoin})::Vector{SQLColumn}
Extracts columns from joins param and adds to be used for the SELECT part
escape_column_name(c::SQLColumn) :: SQLColumn
escape_column_name(s::String)
Sanitizes input to be use as column names in SQL queries.
add_quotes(str::String) :: String
Adds quotes around str
and escapes any previously existing quotes.
NamedTuple
NamedTuple
s are, as their name suggests, named Tuple
s. That is, they're a tuple-like collection of values, where each entry has a unique name, represented as a Symbol
. Like Tuple
s, NamedTuple
s are immutable; neither the names nor the values can be modified in place after construction.
A named tuple can be created as a tuple literal with keys, e.g. (a=1, b=2)
, or as a tuple literal with semicolon after the opening parenthesis, e.g. (; a=1, b=2)
(this form also accepts programmatically generated names as described below), or using a NamedTuple
type as constructor, e.g. NamedTuple{(:a, :b)}((1,2))
.
Accessing the value associated with a name in a named tuple can be done using field access syntax, e.g. x.a
, or using getindex
, e.g. x[:a]
or x[(:a, :b)]
. A tuple of the names can be obtained using keys
, and a tuple of the values can be obtained using values
.
tip Note
Iteration over NamedTuple
s produces the values without the names. (See example below.) To iterate over the name-value pairs, use the pairs
function.
The @NamedTuple
macro can be used for conveniently declaring NamedTuple
types.
Examples
julia> x = (a=1, b=2)
(a = 1, b = 2)
julia> x.a
1
julia> x[:a]
1
julia> x[(:a,)]
(a = 1,)
julia> keys(x)
(:a, :b)
julia> values(x)
(1, 2)
julia> collect(x)
2-element Vector{Int64}:
1
2
julia> collect(pairs(x))
2-element Vector{Pair{Symbol, Int64}}:
:a => 1
:b => 2
In a similar fashion as to how one can define keyword arguments programmatically, a named tuple can be created by giving pairs name::Symbol => value
after a semicolon inside a tuple literal. This and the name=value
syntax can be mixed:
julia> (; :a => 1, :b => 2, c=3)
(a = 1, b = 2, c = 3)
The name-value pairs can also be provided by splatting a named tuple or any iterator that yields two-value collections holding each a symbol as first value:
julia> keys = (:a, :b, :c); values = (1, 2, 3);
julia> NamedTuple{keys}(values)
(a = 1, b = 2, c = 3)
julia> (; (keys .=> values)...)
(a = 1, b = 2, c = 3)
julia> nt1 = (a=1, b=2);
julia> nt2 = (c=3, d=4);
julia> (; nt1..., nt2..., b=20) # the final b overwrites the value from nt1
(a = 1, b = 20, c = 3, d = 4)
julia> (; zip(keys, values)...) # zip yields tuples such as (:a, 1)
(a = 1, b = 2, c = 3)
As in keyword arguments, identifiers and dot expressions imply names:
julia> x = 0
0
julia> t = (; x)
(x = 0,)
julia> (; t.x)
(x = 0,)
tip Julia 1.5
Implicit names from identifiers and dot expressions are available as of Julia 1.5.
tip Julia 1.7
Use of getindex
methods with multiple Symbol
s is available as of Julia 1.7.