I have this query:
UPDATE test SET cucc = 'akarmi' WHERE id IN (1, 2)
Laravel equivalent:
DB::table('test')
->whereIn('id', [1, 2])
->update(['cucc' => 'akarmi']);
But i'd like to append new values to column cucc:
UPDATE test SET cucc = CONCAT(cucc, 'akarmi') WHERE id IN (1, 2)
Laravel equivalent:
DB::table('test')
->whereIn('id', [1, 2])
->update(['cucc' => DB::raw("CONCAT(cucc, 'plus')")]);
But if the 'plus' string comes from a user input variable, it need to escape. I tried this:
DB::table('test')
->whereIn('id', [1, 2])
->update(['cucc' => DB::raw('CONCAT(cucc, ?)', 'plus')]);
But DB::raw not supports replace ? to escaped value.
How can I solve this?
DB::raw() doesnt have a parameter for bindings but the query builder does
DB::table('test')
->whereRaw('id IN (?,?)')
->setBindings(['plus', 1, 2])
->update(['cucc' => DB::raw('CONCAT(cucc, ?)')]);
You can also use the bindings parameter of the update() method
(should work but didnt test it)
DB::table('test')
->whereIn('id', [1, 2])
->update(['cucc' => DB::raw('CONCAT(cucc, ?)')], ['plus']);
Related
I have a very simple sample script:
using Pkg
Pkg.add("DecisionTree")
Pkg.add("DataFrames")
using DataFrames
using DecisionTree
dat = DataFrame(A=[1, 2, 3, 4, 5], B=[2, 5, 1, 2, 6])
model = build_tree(dat[!, "A"], dat[!, "B"])
Which returns an error:
ERROR: LoadError: MethodError: no method matching build_tree(::Vector{Int64}, ::Vector{Int64})
Closest candidates are:
build_tree(::AbstractVector{T}, ::AbstractMatrix{S}) where {S, T} at C:\Users\**\.julia\packages\DecisionTree\iWCbW\src\classification\main.jl:74
build_tree(::AbstractVector{T}, ::AbstractMatrix{S}, ::Any) where {S, T} at C:\Users\**\.julia\packages\DecisionTree\iWCbW\src\classification\main.jl:74
build_tree(::AbstractVector{T}, ::AbstractMatrix{S}, ::Any, ::Any) where {S, T} at C:\Users\**\.julia\packages\DecisionTree\iWCbW\src\classification\main.jl:74
What is going on? How do I deal with that?
Your data types do not match. Try this:
C = reshape(dat[!, "B"], (1, 5))
model = DecisionTree.build_tree(dat[!, "A"], C')
So, I was trying to normalize (i.e. max = 1, min = value/max) a specific column within a numpy array.
I hoped this piece of code would do the trick:
bar = np.arange(12).reshape(6,2)
bar
array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]])
bar[:,1] = bar[:,1] / bar[:,1].max()
bar
array([[ 0, 0],
[ 2, 0],
[ 4, 0],
[ 6, 0],
[ 8, 0],
[10, 1]])
works as expected if the type of each value is 'float'.
foo = np.array([[1.1,2.2],
[3.3,4.4],
[5.5,6.6]])
foo[:,1] = foo[:,1] / foo[:,1].max()
foo
array([[1.1 , 0.33333333],
[3.3 , 0.66666667],
[5.5 , 1. ]])
I guess what I'm asking is where is this default 'int' I'm missing here?
(I'm taking this as a 'learning opportunity')
If you simply execute:
out = bar[:,1] / bar[:,1].max()
print(out)
>>> [0.09090909 0.27272727 0.45454545 0.63636364 0.81818182 1. ]
It's working just fine, since out is a newly created float array made to store these float values. But np.arange(12) gives you an int array by default. bar[:,1] = bar[:,1] / bar[:,1].max() tries to store the float values inside the integer array, and all the values become integers and you get [0 0 0 0 0 1].
To set the array as a float by default:
bar = np.arange(12, dtype='float').reshape(6,2)
Alternatively, you can also use:
bar = np.arange(12).reshape(6,2).astype('float')
It isn't uncommon for us to need to change the data type of the array throughout the program, as you may not always need the dtype you define originally. So .astype() is actually pretty handy in all kinds of scenarios.
From np.arange documentation :
dtype : dtype
The type of the output array. If dtype is not given, infer the data type from the other input arguments.
Since you passed int values it will infer that the values in the array are int and so they won't change to float, you can do like this if you want:
bar = np.arange(12.0).reshape(6,2)
I came across the following Kotlin code:
single(name = walletOkHttpTag) {
createOkHttpClient {
addHeaders(
*mutableListOf<Pair<String, String>>().apply {
add(HeaderKey.ACCEPT to APPLICATION_JSON_HEADER)
if (isDebug || isBeta) {
add(HeaderKey.AUTHORIZATION to BASIC_AUTH_WALLET_STAGE_HEADER)
}
}.toTypedArray()
)
}
}
What does the asterisk * mean that is in front of mutableListOf?
This is the spread operator and it is required to pass an existing array to a vararg function.
When we call a vararg-function, we can pass arguments one-by-one, e.g. asList(1, 2, 3), or, if we already have an array and want to pass its contents to the function, we use the spread operator (prefix the array with *):
Simplified example from the documentation:
val a = arrayOf(1, 2, 3)
val list = listOf(-1, 0, *a, 4)
println(list)
Output:
[-1, 0, 1, 2, 3, 4]
Without the spread operator, the array itself would be added as a single element, resulting in a List<Serializable> with 4 elements:
[-1, 0, [Ljava.lang.Integer;#31befd9f, 4]
What's the best way to do the following in Ramda:
_.range(0, 3, 0);
// => [0, 0, 0]
Thank you.
If you need to repeat the same number n times, then Ori Drori already provided a good answer with repeat.
However if you need to support step, you would have to build a function yourself. (Ramda has a range function but it does not support step.)
So where Lodash would return:
_.range(1, 10, 2);
//=> [1, 3, 5, 7, 9]
You can achieve a similar functionality with Ramda unfold function:
const rangeStep = curry((start, end, step) =>
unfold(n => n < end ? [n, n + step] : false, start));
rangeStep(1, 10, 2);
//=> [1, 3, 5, 7, 9]
You can use R.repeat to create an array of multiple instances of a single item:
const result = R.repeat(0, 3)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
I'm quite new to pytest and I would like to know how to mark a test as "expected to fail" when called with certain parameters. I parametrize test like this:
#pytest.mark.parametrize("param1", [False, True])
#pytest.mark.parametrize("param2", [1, 2, 3])
def test_foo(self, param1, param2):
...
What I'm trying to achieve is that when the test is called with (param1 == True and param2 == 2), the test should fail; whilst any other parameter combinations should pass.
But I haven't found any way to do this. Do you have any ideas?
See xfail with parametrize:
#pytest.mark.parametrize("param2, param2", [
(1, True),
(2, True),
pytest.param(1, False, marks=pytest.mark.xfail(reason='some bug')),
])
def test_foo(self, param1, param2):
...