问:
Safely hooking C functions into assembly
I'm trying to put some C hooks into some code that someone else wrote in asm. I don't know much about x86 asm and I want to make sure I'm doing this safely.

So far I've got this much:

```<code>EXTERN _C_func

CALL _C_func
</code>```

And that seems to work, but I'm sure that's dangerous as-is. At the very least it could be destroying EAX, and possibly more.

So I think all I need to know is: Which registers does C destroy and how do I ensure the registers are being saved properly? Is there anything else I can do to make sure I can safely insert hooks into arbitrary locations?

I'm using Visual Studio for the C functions, if that helps.
Thank you.

UPDATE:

I looked up the "paranoid" method several people suggested and it looks like this:

```<code>pushfd
pushad
call _C_func
popad
popfd
</code>```

AD = (A)ll general registers

FD = (F)lags

(I'm not sure what the 'd' stands for, but it means 32-bit registers instead of 16-bit.)

But the most efficient method is illustrated in Kragen's answer below. (Assuming you don't need the preserve the flags. If you do, add the FD instructions.)

Also, be careful about allocating lots of stack variables in your C function as it could overrun the asm subroutine's stack space.

I think that about does it, thanks for you help everyone!

答:
If the calling convention is cdecl and the signature of C_funct is simply:

```<code>void C_func(void);
</code>```

Then that is perfectly "safe", however the registers EAX, ECX and EDX are "available for use inside the function", and so may be overwritten.

To protect against this you can save the registers that you care about and restore them afterwards:

```<code>push eax
push ecx
push edx

call _C_func

pop edx
pop ecx
pop eax
</code>```

By convention the registers EBX, ESI, EDI, and EBP shouldn't be modified by the callee.

I believe that the flags may be modified by the callee - again if you care about preserving the value of flags then you should save them.

See the Wikipedia page on x86 calling conventions.

Body:
I'm trying to put some C hooks into some code that someone else wrote in asm. I don't know much about x86 asm and I want to make sure I'm doing this safely.

So far I've got this much:

EXTERN _C_func

CALL _C_func

And that seems to work, but I'm sure that's dangerous as-is. At the very least it could be destroying EAX, and possibly more.

So I think all I need to know is: Which registers does C destroy and how do I ensure the registers are being saved properly? Is there anything else I can do to make sure I can safely insert hooks into arbitrary locations?

I'm using Visual Studio for the C functions, if that helps.
Thank you.

UPDATE:

I looked up the "paranoid" method several people suggested and it looks like this:

pushfd
pushad
call _C_func
popad
popfd

AD = (A)ll general registers

FD = (F)lags

(I'm not sure what the 'd' stands for, but it means 32-bit registers instead of 16-bit.)

But the most efficient method is illustrated in Kragen's answer below. (Assuming you don't need the preserve the flags. If you do, add the FD instructions.)

Also, be careful about allocating lots of stack variables in your C function as it could overrun the asm subroutine's stack space.

I think that about does it, thanks for you help everyone!

Body:
If the calling convention is cdecl and the signature of C_funct is simply:

void C_func(void);

Then that is perfectly "safe", however the registers EAX, ECX and EDX are "available for use inside the function", and so may be overwritten.

To protect against this you can save the registers that you care about and restore them afterwards:

push eax
push ecx
push edx

call _C_func

pop edx
pop ecx
pop eax

By convention the registers EBX, ESI, EDI, and EBP shouldn't be modified by the callee.

I believe that the flags may be modified by the callee - again if you care about preserving the value of flags then you should save them.

See the Wikipedia page on x86 calling conventions.


来源: stackexchange

问:
Min and max terms and Karnaugh maps
Could anyone explain what is the importance of max and min terms in boolean algebra and uses of Karnaugh maps. I feel like confused in those titles.</p>

答:
Minterms use SOP (sum of products) - and are represented by Sigma

Maxterms use POS (product of sums) - and are represented by Pi

Minterms are classified when the output of the truth table row is a 1, while maxterms capture the 0's.

The representation is also different, minterms look like: (AB)+(CD) while maxterms look like (A+B)(C+D).

K-Map:

SOP uses the K-map normally and captures the 1s. When the 1 (header) is constant, the variable will be A for example, but if the 0 is constant, it would be A' (A Bar).

POS does the exact opposite (captures all the 0s). Keep in mind that when a 0 is constant, it will be A for example, but when a 1 is constant (it's inverted) and becomes an A'

ContentLicense: CC BY-SA 4.0

Title: Min and max terms and Karnaugh maps


问:
How can I execute multiple NN training?
I have two NVidia GPUs in the machine, but I am not using them.

I have three NN training running on my machine. When I am trying to run the fourth one, the script is giving me the following error:

```<code>my_user@my_machine:~/my_project/training_my_project$ python3 my_project.py
Traceback (most recent call last):
  File "my_project.py", line 211, in <module>
    load_data(
  File "my_project.py", line 132, in load_data
    tx = tf.convert_to_tensor(data_x, dtype=tf.float32)
  File "/home/my_user/.local/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/my_user/.local/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 106, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Failed to allocate scratch buffer for device 0
my_user@my_machine:~/my_project/training_my_project$
</code>```

How can I resolve this issue?

The following is my RAM usage:

```<code>my_user@my_machine:~/my_project/training_my_project$ free -m
              total        used        free      shared  buff/cache   available
Mem:          15947        6651        3650          20        5645        8952
Swap:          2047         338        1709
my_user@my_machine:~/my_project/training_my_project$
</code>```

The following is my CPU usage:

```<code>my_user@my_machine:~$ top -i
top - 12:46:12 up 79 days, 21:14,  2 users,  load average: 4,05, 3,82, 3,80
Tasks: 585 total,   2 running, 583 sleeping,   0 stopped,   0 zombie
%Cpu(s): 11,7 us,  1,6 sy,  0,0 ni, 86,6 id,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st
MiB Mem :  15947,7 total,   3638,3 free,   6662,7 used,   5646,7 buff/cache
MiB Swap:   2048,0 total,   1709,4 free,    338,6 used.   8941,6 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
2081821 my_user  20   0   48,9g   2,5g 471076 S 156,1  15,8   1832:54 python3
2082196 my_user  20   0   48,8g   2,6g 467708 S 148,5  16,8   1798:51 python3
2076942 my_user  20   0   47,8g   1,6g 466916 R 147,5  10,3   2797:51 python3
   1594 gdm       20   0 3989336  65816  31120 S   0,7   0,4  38:03.14 gnome-shell
     93 root      rt   0       0      0      0 S   0,3   0,0   0:38.42 migration/13
   1185 root     -51   0       0      0      0 S   0,3   0,0   3925:59 irq/54-nvidia
2075861 root      20   0       0      0      0 I   0,3   0,0   1:30.17 kworker/22:0-events
2076418 root      20   0       0      0      0 I   0,3   0,0   1:38.65 kworker/1:0-events
2085325 root      20   0       0      0      0 I   0,3   0,0   1:17.15 kworker/3:1-events
2093002 root      20   0       0      0      0 I   0,3   0,0   1:00.05 kworker/23:0-events
2100000 root      20   0       0      0      0 I   0,3   0,0   0:45.78 kworker/2:2-events
2104688 root      20   0       0      0      0 I   0,3   0,0   0:33.08 kworker/9:0-events
2106767 root      20   0       0      0      0 I   0,3   0,0   0:25.16 kworker/20:0-events
2115469 root      20   0       0      0      0 I   0,3   0,0   0:01.98 kworker/11:2-events
2115470 root      20   0       0      0      0 I   0,3   0,0   0:01.96 kworker/12:2-events
2115477 root      20   0       0      0      0 I   0,3   0,0   0:01.95 kworker/30:1-events
2116059 my_user  20   0   23560   4508   3420 R   0,3   0,0   0:00.80 top
</code>```

The following is my TF configuration:

```<code>import os

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
# os.environ["CUDA_VISIBLE_DEVICES"] = "99" # Use both gpus for training.

import sys, random
import time
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint
import numpy as np
from lxml import etree, objectify

# <editor-fold desc="GPU">
# resolve GPU related issues.
try:
    physical_devices = tf.config.list_physical_devices('GPU')
    for gpu_instance in physical_devices:
        tf.config.experimental.set_memory_growth(gpu_instance, True)
except Exception as e:
    pass
# END of try
# </editor-fold>
</code>```

Please, take the commented lines as commented-out lines.

Relevant source code:

```<code>def load_data(fname: str, class_index: int, feature_start_index: int, **selection):
    i = 0
    file = open(fname)
    if "top_n_lines" in selection:
        lines = [next(file) for _ in range(int(selection["top_n_lines"]))]
    elif "random_n_lines" in selection:
        tmp_lines = file.readlines()
        lines = random.sample(tmp_lines, int(selection["random_n_lines"]))
    else:
        lines = file.readlines()

    data_x, data_y = [], []
    for l in lines:
        row = l.strip().split()
        x = [float(ix) for ix in row[feature_start_index:]]
        y = encode(row[class_index])
        data_x.append(x)
        data_y.append(y)
    # END for l in lines

    num_rows = len(data_x)
    given_fraction = selection.get("validation_part", 1.0)
    if given_fraction > 0.9999:
        valid_x, valid_y = data_x, data_y
    else:
        n = int(num_rows * given_fraction)
        data_x, data_y = data_x[n:], data_y[n:]
        valid_x, valid_y = data_x[:n], data_y[:n]
    # END of if-else block

    tx = tf.convert_to_tensor(data_x, np.float32)
    ty = tf.convert_to_tensor(data_y, np.float32)

    vx = tf.convert_to_tensor(valid_x, np.float32)
    vy = tf.convert_to_tensor(valid_y, np.float32)

    return tx, ty, vx, vy
# END of the function
</code>```

答:
Using multiple GPUs

If developing on a system with a single GPU, you can simulate multiple GPUs with virtual devices. This enables easy testing of multi-GPU setups without requiring additional resources.

```<code>gpus = tf.config.list_physical_devices('GPU')
if gpus:
  # Create 2 virtual GPUs with 1GB memory each
  try:
    tf.config.set_logical_device_configuration(
        gpus[0],
        [tf.config.LogicalDeviceConfiguration(memory_limit=1024),
         tf.config.LogicalDeviceConfiguration(memory_limit=1024)])
    logical_gpus = tf.config.list_logical_devices('GPU')
    print(len(gpus), "Physical GPU,", len(logical_gpus), "Logical GPUs")
  except RuntimeError as e:
    # Virtual devices must be set before GPUs have been initialized
    print(e)
</code>```

NOTE: Virtual devices cannot be modified after being initialized

Once there are multiple logical GPUs available to the runtime, you can utilize the multiple GPUs with tf.distribute.Strategy or with manual placement.

With tf.distribute.Strategy best practice for using multiple GPUs, here is a simple example:

```<code>tf.debugging.set_log_device_placement(True)
gpus = tf.config.list_logical_devices('GPU')
strategy = tf.distribute.MirroredStrategy(gpus)
with strategy.scope():
  inputs = tf.keras.layers.Input(shape=(1,))
  predictions = tf.keras.layers.Dense(1)(inputs)
  model = tf.keras.models.Model(inputs=inputs, outputs=predictions)
  model.compile(loss='mse',
                optimizer=tf.keras.optimizers.SGD(learning_rate=0.2))
</code>```

This program will run a copy of your model on each GPU, splitting the input data between them, also known as "data parallelism".

For more information about distribution strategies or manual placement, check out the guides on the links.

Title: How can I execute multiple NN training?

Body:
I have two NVidia GPUs in the machine, but I am not using them.

I have three NN training running on my machine. When I am trying to run the fourth one, the script is giving me the following error:

my_user@my_machine:~/my_project/training_my_project$ python3 my_project.py
Traceback (most recent call last):
  File "my_project.py", line 211, in <module>
    load_data(
  File "my_project.py", line 132, in load_data
    tx = tf.convert_to_tensor(data_x, dtype=tf.float32)
  File "/home/my_user/.local/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/my_user/.local/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 106, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Failed to allocate scratch buffer for device 0
my_user@my_machine:~/my_project/training_my_project$

How can I resolve this issue?

The following is my RAM usage:

my_user@my_machine:~/my_project/training_my_project$ free -m
              total        used        free      shared  buff/cache   available
Mem:          15947        6651        3650          20        5645        8952
Swap:          2047         338        1709
my_user@my_machine:~/my_project/training_my_project$

The following is my CPU usage:

my_user@my_machine:~$ top -i
top - 12:46:12 up 79 days, 21:14,  2 users,  load average: 4,05, 3,82, 3,80
Tasks: 585 total,   2 running, 583 sleeping,   0 stopped,   0 zombie
%Cpu(s): 11,7 us,  1,6 sy,  0,0 ni, 86,6 id,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st
MiB Mem :  15947,7 total,   3638,3 free,   6662,7 used,   5646,7 buff/cache
MiB Swap:   2048,0 total,   1709,4 free,    338,6 used.   8941,6 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
2081821 my_user  20   0   48,9g   2,5g 471076 S 156,1  15,8   1832:54 python3
2082196 my_user  20   0   48,8g   2,6g 467708 S 148,5  16,8   1798:51 python3
2076942 my_user  20   0   47,8g   1,6g 466916 R 147,5  10,3   2797:51 python3
   1594 gdm       20   0 3989336  65816  31120 S   0,7   0,4  38:03.14 gnome-shell
     93 root      rt   0       0      0      0 S   0,3   0,0   0:38.42 migration/13
   1185 root     -51   0       0      0      0 S   0,3   0,0   3925:59 irq/54-nvidia
2075861 root      20   0       0      0      0 I   0,3   0,0   1:30.17 kworker/22:0-events
2076418 root      20   0       0      0      0 I   0,3   0,0   1:38.65 kworker/1:0-events
2085325 root      20   0       0      0      0 I   0,3   0,0   1:17.15 kworker/3:1-events
2093002 root      20   0       0      0      0 I   0,3   0,0   1:00.05 kworker/23:0-events
2100000 root      20   0       0      0      0 I   0,3   0,0   0:45.78 kworker/2:2-events
2104688 root      20   0       0      0      0 I   0,3   0,0   0:33.08 kworker/9:0-events
2106767 root      20   0       0      0      0 I   0,3   0,0   0:25.16 kworker/20:0-events
2115469 root      20   0       0      0      0 I   0,3   0,0   0:01.98 kworker/11:2-events
2115470 root      20   0       0      0      0 I   0,3   0,0   0:01.96 kworker/12:2-events
2115477 root      20   0       0      0      0 I   0,3   0,0   0:01.95 kworker/30:1-events
2116059 my_user  20   0   23560   4508   3420 R   0,3   0,0   0:00.80 top

The following is my TF configuration:

import os

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
# os.environ["CUDA_VISIBLE_DEVICES"] = "99" # Use both gpus for training.

import sys, random
import time
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint
import numpy as np
from lxml import etree, objectify

# <editor-fold desc="GPU">
# resolve GPU related issues.
try:
    physical_devices = tf.config.list_physical_devices('GPU')
    for gpu_instance in physical_devices:
        tf.config.experimental.set_memory_growth(gpu_instance, True)
except Exception as e:
    pass
# END of try
# </editor-fold>

Please, take the commented lines as commented-out lines.

Relevant source code:

def load_data(fname: str, class_index: int, feature_start_index: int, **selection):
    i = 0
    file = open(fname)
    if "top_n_lines" in selection:
        lines = [next(file) for _ in range(int(selection["top_n_lines"]))]
    elif "random_n_lines" in selection:
        tmp_lines = file.readlines()
        lines = random.sample(tmp_lines, int(selection["random_n_lines"]))
    else:
        lines = file.readlines()

    data_x, data_y = [], []
    for l in lines:
        row = l.strip().split()
        x = [float(ix) for ix in row[feature_start_index:]]
        y = encode(row[class_index])
        data_x.append(x)
        data_y.append(y)
    # END for l in lines

    num_rows = len(data_x)
    given_fraction = selection.get("validation_part", 1.0)
    if given_fraction > 0.9999:
        valid_x, valid_y = data_x, data_y
    else:
        n = int(num_rows * given_fraction)
        data_x, data_y = data_x[n:], data_y[n:]
        valid_x, valid_y = data_x[:n], data_y[:n]
    # END of if-else block

    tx = tf.convert_to_tensor(data_x, np.float32)
    ty = tf.convert_to_tensor(data_y, np.float32)

    vx = tf.convert_to_tensor(valid_x, np.float32)
    vy = tf.convert_to_tensor(valid_y, np.float32)

    return tx, ty, vx, vy
# END of the function

Body:
Using multiple GPUs

If developing on a system with a single GPU, you can simulate multiple GPUs with virtual devices. This enables easy testing of multi-GPU setups without requiring additional resources.

gpus = tf.config.list_physical_devices('GPU')
if gpus:
  # Create 2 virtual GPUs with 1GB memory each
  try:
    tf.config.set_logical_device_configuration(
        gpus[0],
        [tf.config.LogicalDeviceConfiguration(memory_limit=1024),
         tf.config.LogicalDeviceConfiguration(memory_limit=1024)])
    logical_gpus = tf.config.list_logical_devices('GPU')
    print(len(gpus), "Physical GPU,", len(logical_gpus), "Logical GPUs")
  except RuntimeError as e:
    # Virtual devices must be set before GPUs have been initialized
    print(e)

NOTE: Virtual devices cannot be modified after being initialized

Once there are multiple logical GPUs available to the runtime, you can utilize the multiple GPUs with tf.distribute.Strategy or with manual placement.

With tf.distribute.Strategy best practice for using multiple GPUs, here is a simple example:

tf.debugging.set_log_device_placement(True)
gpus = tf.config.list_logical_devices('GPU')
strategy = tf.distribute.MirroredStrategy(gpus)
with strategy.scope():
  inputs = tf.keras.layers.Input(shape=(1,))
  predictions = tf.keras.layers.Dense(1)(inputs)
  model = tf.keras.models.Model(inputs=inputs, outputs=predictions)
  model.compile(loss='mse',
                optimizer=tf.keras.optimizers.SGD(learning_rate=0.2))

This program will run a copy of your model on each GPU, splitting the input data between them, also known as "data parallelism".

For more information about distribution strategies or manual placement, check out the guides on the links.


问:
How to get an Amazon ECR container URI for a specific model image in Sagemaker?
I want to know if it's possible to get an Amazon ECR container URI for a specific image programmatically (using AWS CLI or Python). For example, if I need the URL for the latest linear-learner (built-in model) image for the eu-central-1 region.

Expected result:

```<code>664544806723.dkr.ecr.eu-central-1.amazonaws.com/linear-learner:latest
</code>```

EDIT: I have found the solution with get_image_uri. It looks like this function will be depreceated and I don't know how to use ImageURIProvider instead.

答:
from sagemaker import image_uris
container = sagemaker.image_uris.retrieve("linear-learner", boto3.Session().region_name)</p>

Body:
I want to know if it's possible to get an Amazon ECR container URI for a specific image programmatically (using AWS CLI or Python). For example, if I need the URL for the latest linear-learner (built-in model) image for the eu-central-1 region.

Expected result:

664544806723.dkr.ecr.eu-central-1.amazonaws.com/linear-learner:latest

EDIT: I have found the solution with get_image_uri. It looks like this function will be depreceated and I don't know how to use ImageURIProvider instead.


问:
In Talend Studio in tFileOutputDelimited what does 'OS line separator as row separator' do?
In Talend Studio (at least in versions 7.3.1 and 8.0.1), the component tFileOutputDelimited has a checkbox that says

Use OS line separator as row separator when CSV Row Separator is set to CR, LF or CRLF.

And right below there's a dropdown menu with only 3 options:

LF ("\n")

CR ("\r")

CRLF ("\r\n")

What's the difference between checking or unchecking the checkbox? What does it do?

What's the difference between an OS line separator and a CSV row separator?

答:
When you check the option "Use OS line separator", and in the CSV Row Separator you set one of the 3 delimiters (LF, CR, CRLF), when the job is executed it automatically selects the appropriate row separator for the OS it's running on (LF for Linux, CRLF for Windows). It's a nice feature if your job is deployed to a different OS and you want your file to have the right separator for that platform, without having to modify your job.

If you set the option "Use OS line separator" but you set a custom line separator, Talend will use the separator you specify, so the option doesn't really matter.

If you uncheck the option "Use OS line separator", you can specify the separator that will always be used regardless of the underlying OS.


问:
how can i create a grid with java using toStrings
Hi Im new to java and I would like to create a grid using java

I want my output to look like this

```<code>1 2
3 4
</code>```

How can I do it with just the toStrings method? I am thinking of using a constructor where if I put  (int 2) then it will give me this grid, and if it is  (int 3) then it will look like:

```<code>1 2 3
4 5 6
</code>```

答:
You need to use nested for loops. Your method need to take 3 parameters for the row count and column count and also a start number (if say you want to start from 10 instead of 1)

```<code>private static String generateGrid(int rows, int cols, int start){
        StringBuilder gridBuilder = new StringBuilder();
        int nextNumber = start;
        for(int row = 0 ; row < rows; row++){
           for(int col = 0 ; col < cols; col++){
                gridBuilder.append(nextNumber);
                gridBuilder.append(" ");
                nextNumber++;
           }
           gridBuilder.append("\n");
        }
        return gridBuilder.toString();
    }
</code>```

The method returns a String. You are free to print the result or use it for other purposes

Body:
Hi Im new to java and I would like to create a grid using java

I want my output to look like this

1 2
3 4

How can I do it with just the toStrings method? I am thinking of using a constructor where if I put  (int 2) then it will give me this grid, and if it is  (int 3) then it will look like:

1 2 3
4 5 6

Body:
You need to use nested for loops. Your method need to take 3 parameters for the row count and column count and also a start number (if say you want to start from 10 instead of 1)

private static String generateGrid(int rows, int cols, int start){
        StringBuilder gridBuilder = new StringBuilder();
        int nextNumber = start;
        for(int row = 0 ; row < rows; row++){
           for(int col = 0 ; col < cols; col++){
                gridBuilder.append(nextNumber);
                gridBuilder.append(" ");
                nextNumber++;
           }
           gridBuilder.append("\n");
        }
        return gridBuilder.toString();
    }

The method returns a String. You are free to print the result or use it for other purposes


问:
Generic virtual method and concrete Implementation?
I'm currently working on a piece of code, where Generics are driving me crazy. Maybe somebody can point out the issue.

All our DatabaseEntities have common Interfaces, one for example is IDatabaseEntityWithId.

let's say there is EntityA : IDatabaseEntityWithId. We have different, non-generic Forms / Controls. We also have Controls that should be shared, and do their Job independent of the concrete Type.

Now, coming from Java this is quite easy there, cause every control could just use for example List<?> and pass / Process the list without actually caring what is inside.

So, our Search-Control should also be able to "deal" with List<T> and IQueryable<T> without caring what T actually is.

The base implementation for an EntityList has the following methods, which our Search should utilize to generate the results, do pagination, etc.

```<code>public class EntityListControl{
  public virtual IQueryable<T> BaseQuery<T>(DBContext dbContext) where T : IDBEntityWithId => throw new NotImplementedException();
  public virtual List<string> AutocompleteSuggestionsQuickSearch() => throw new NotImplementedException();
  public virtual List<IDBEntityWithId> DoQuickSearch(string reference) => throw new NotImplementedException();
  public virtual IQueryable<T> DoTaggedSearchExtensions<T>(IQueryable<T> query, DbContext dbContext) where T : IDBEntityWithId  => throw new NotImplementedException();
}
</code>```

The SearchControl has a reference to the currently loaded EntityListControl and basically should just act as orchestrator for the concrete methods, i.e. shortened it looks like:

```<code>class SearchControl{
   public void DoSearch(){
      String[] searchTags = this.SearchField.Text.Split(' ');
      using (DbContext db = DbContext.Factory()){
          var query = this.EntityListControl.BaseQuery<IDBEntityWithId>(db);

          //do other stuff.

          query = this.EntityListControl.DoTaggedSearchExtension<IDBEntityWithID>(query, db);

         //do other stuff

          List<IDBEntityWithId> result = query.Take(X).ToList();

         //do other stuff

          this.EntityListView.FactoryRows(result);
      }
</code>```

You'll get the idea.

Now, for one of the EntityListControl-implementations, these methods might look like:

```<code>public class EntityList: UserControl{
    ...
}
...

public class EntityListA : EntityList{

   public override IQueryable<EntityA> BaseQuery<EntityA>(DbContext db){
       return db.EntityA.Include("EntityB").Include("EntityC");
   }

   public IQueryable<EntityA> DoTaggedSearchExtension<EntityA>(IQueryable<EntityA> query, DbContext db){
      //Problem is now here...
      return query.Where(x => x.PropertyOfEntityA == true);
   }
}
</code>```

The Problem is now in the concrete implementation of DoTaggedSearchExtension. Visual Studio here clearly lines out, that T is EntityA and the query is IQueryable<EntityA> - but refuses to accept any properties that don't belong to IDBEntityWithId. Why is that?

The error Message even reads "EntityA does not contain a definition for 'property'...." - which is wrong, it is there... IntelliSense just proposes properties of the common interface.

Obviously in the generic EntityListControl, I can not be more concrete on the type of T, IBaseEntityWithId is one of the common interfaces I can ensure for all entities.

Ofc, I could leave away the whole generic-part and JUST work with interfaces, casting them to the actual type in the implementations, but that's not the point, isn't it?

Any suggestion on how to fix this particular example?

答:
The definition of DoTaggedSearchExtension identifies that T is a IDBEntityWithId

C# is strongly typed so it only allows methods defined on IDBEntityWithId

Body:
I'm currently working on a piece of code, where Generics are driving me crazy. Maybe somebody can point out the issue.

All our DatabaseEntities have common Interfaces, one for example is IDatabaseEntityWithId.

let's say there is EntityA : IDatabaseEntityWithId. We have different, non-generic Forms / Controls. We also have Controls that should be shared, and do their Job independent of the concrete Type.

Now, coming from Java this is quite easy there, cause every control could just use for example List<?> and pass / Process the list without actually caring what is inside.

So, our Search-Control should also be able to "deal" with List<T> and IQueryable<T> without caring what T actually is.

The base implementation for an EntityList has the following methods, which our Search should utilize to generate the results, do pagination, etc.

public class EntityListControl{
  public virtual IQueryable<T> BaseQuery<T>(DBContext dbContext) where T : IDBEntityWithId => throw new NotImplementedException();
  public virtual List<string> AutocompleteSuggestionsQuickSearch() => throw new NotImplementedException();
  public virtual List<IDBEntityWithId> DoQuickSearch(string reference) => throw new NotImplementedException();
  public virtual IQueryable<T> DoTaggedSearchExtensions<T>(IQueryable<T> query, DbContext dbContext) where T : IDBEntityWithId  => throw new NotImplementedException();
}

The SearchControl has a reference to the currently loaded EntityListControl and basically should just act as orchestrator for the concrete methods, i.e. shortened it looks like:

class SearchControl{
   public void DoSearch(){
      String[] searchTags = this.SearchField.Text.Split(' ');
      using (DbContext db = DbContext.Factory()){
          var query = this.EntityListControl.BaseQuery<IDBEntityWithId>(db);

          //do other stuff.

          query = this.EntityListControl.DoTaggedSearchExtension<IDBEntityWithID>(query, db);

         //do other stuff

          List<IDBEntityWithId> result = query.Take(X).ToList();

         //do other stuff

          this.EntityListView.FactoryRows(result);
      }

You'll get the idea.

Now, for one of the EntityListControl-implementations, these methods might look like:

public class EntityList: UserControl{
    ...
}
...

public class EntityListA : EntityList{

   public override IQueryable<EntityA> BaseQuery<EntityA>(DbContext db){
       return db.EntityA.Include("EntityB").Include("EntityC");
   }

   public IQueryable<EntityA> DoTaggedSearchExtension<EntityA>(IQueryable<EntityA> query, DbContext db){
      //Problem is now here...
      return query.Where(x => x.PropertyOfEntityA == true);
   }
}

The Problem is now in the concrete implementation of DoTaggedSearchExtension. Visual Studio here clearly lines out, that T is EntityA and the query is IQueryable<EntityA> - but refuses to accept any properties that don't belong to IDBEntityWithId. Why is that?

The error Message even reads "EntityA does not contain a definition for 'property'...." - which is wrong, it is there... IntelliSense just proposes properties of the common interface.

Obviously in the generic EntityListControl, I can not be more concrete on the type of T, IBaseEntityWithId is one of the common interfaces I can ensure for all entities.

Ofc, I could leave away the whole generic-part and JUST work with interfaces, casting them to the actual type in the implementations, but that's not the point, isn't it?

Any suggestion on how to fix this particular example?


问:
How to Grant permission to user on Certificate private key using powershell?
Certificate is already installed on machine. Now I want to give read permission on PrivateKey of Certificate to application user.</p>

答:
It came to my attention a few weeks ago that something changed (I suspect a Windows update) and broke the ability for some certificates to use the CspKeyContainerInfo.UniqueKeyContainerName property referenced in Michael Armitage's script. Some sleuthing uncovered that Windows decided to start using CNG instead of Crypto Service Provider to protect the key. The following script fixed my issue and should correctly support CNG vs CSP use case scenarios:

```<code>$serviceUser = "DOMAIN\Service User"
$certificate = Get-ChildItem Cert:\LocalMachine\My | Where-Object Thumbprint -eq "certificatethumbprint"

$privateKey = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($certificate)
$containerName = ""
if ($privateKey.GetType().Name -ieq "RSACng")
{
    $containerName = $privateKey.Key.UniqueName
}
else
{
    $containerName = $privateKey.CspKeyContainerInfo.UniqueKeyContainerName
}

$keyFullPath = $env:ProgramData + "\Microsoft\Crypto\RSA\MachineKeys\" + $containerName;
if (-Not (Test-Path -Path $keyFullPath -PathType Leaf))
{
    throw "Unable to get the private key container to set permissions."
}

# Get the current ACL of the private key
$acl = (Get-Item $keyFullPath).GetAccessControl()

# Add the new ACE to the ACL of the private key
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($serviceUser, "Read", "Allow")
$acl.AddAccessRule($accessRule);

# Write back the new ACL
Set-Acl -Path $keyFullPath -AclObject $acl;
</code>```

You would, of course, want to adapt/enhance this to meet your specific needs.

ContentLicense: CC BY-SA 3.0

Body:
It came to my attention a few weeks ago that something changed (I suspect a Windows update) and broke the ability for some certificates to use the CspKeyContainerInfo.UniqueKeyContainerName property referenced in Michael Armitage's script. Some sleuthing uncovered that Windows decided to start using CNG instead of Crypto Service Provider to protect the key. The following script fixed my issue and should correctly support CNG vs CSP use case scenarios:

$serviceUser = "DOMAIN\Service User"
$certificate = Get-ChildItem Cert:\LocalMachine\My | Where-Object Thumbprint -eq "certificatethumbprint"

$privateKey = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($certificate)
$containerName = ""
if ($privateKey.GetType().Name -ieq "RSACng")
{
    $containerName = $privateKey.Key.UniqueName
}
else
{
    $containerName = $privateKey.CspKeyContainerInfo.UniqueKeyContainerName
}

$keyFullPath = $env:ProgramData + "\Microsoft\Crypto\RSA\MachineKeys\" + $containerName;
if (-Not (Test-Path -Path $keyFullPath -PathType Leaf))
{
    throw "Unable to get the private key container to set permissions."
}

# Get the current ACL of the private key
$acl = (Get-Item $keyFullPath).GetAccessControl()

# Add the new ACE to the ACL of the private key
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($serviceUser, "Read", "Allow")
$acl.AddAccessRule($accessRule);

# Write back the new ACL
Set-Acl -Path $keyFullPath -AclObject $acl;

You would, of course, want to adapt/enhance this to meet your specific needs.


问:
In Hexagonal architecture, can a service rely on another service, or is that tight coupling?
I am creating a banking application. Currently, there is one primary service, the transaction service. This service allows getting transactions by id, and creating transactions. In order to create a transaction, I first want to check if the transaction is valid, by checking the balance of the account it is trying to deduct from, and seeing if they have enough balance. Right now I am doing

TransactionController calls TransactionService. TransactionService creates Transaction, then checks if this is a valid transaction. At this point, I have created an AccountsService, that queries the AccountsRepository, returns an Account. I then do the comparison based on Account.balance > Transaction.amount.

I am conscious here that the TransactionService create method is relying on the AccountService get method. Additionally, AccountService is never directly called from a controller.

Is this an ok way to architect, or is there a more elegant way to do this?

答:
You can reference your AccountRepository directly in your TransactionService.

Sorry I don't speak Java but here is a C# example :

public class Transaction {
  // implementation redacted

  public Transaction(decimal amount, Account from, Account to) {
    if(amount > from?.Balance) throw ... ;
    // redacted
  }
}
```
<pre class="lang-cs prettyprint-override"><code>public class TransactionService {
  private readonly AccountRepository accounts; // by injection
  private readonly TransactionRepository transactions; // by injection

  public void AddTransaction(decimal amount, int source, int destination) {
    var from = accounts.Find(source); // throws if not found
    var to = accounts.Find(destination); // throws if not found
    var transaction = new Transaction(amount, from, to);
    transactions.Insert(transaction);
    transactions.Persist();
  }
}
</code>```

However, this solution is less ORM friendly because of the Transaction constructor. Another way around would be to use Account as your root aggregate, and place the business rule validation and entities relationship handling code there :

public class Account {
  // implementation redacted

  public void AddTransaction(decimal amount, Account to) {
    if(amount > this.Balance) throw ... ;
    // more redacted validations
    this.Debitus.Add(new Transaction { Amount = amount, From = this, To = to });
  }
}
```
<pre class="lang-cs prettyprint-override"><code>public class TransactionService {
  private readonly AccountRepository accounts; // by injection

  public void AddTransaction(decimal amount, int source, int destination) {
    var from = accounts.Find(source); // throws if not found
    var to = accounts.Find(destination); // throws if not found
    from.AddTransaction(amount, to);
    accounts.Persist();
  }
}
</code>```

Body:
You can reference your AccountRepository directly in your TransactionService.

Sorry I don't speak Java but here is a C# example :

public class Transaction {
  // implementation redacted

  public Transaction(decimal amount, Account from, Account to) {
    if(amount > from?.Balance) throw ... ;
    // redacted
  }
}

public class TransactionService {
  private readonly AccountRepository accounts; // by injection
  private readonly TransactionRepository transactions; // by injection

  public void AddTransaction(decimal amount, int source, int destination) {
    var from = accounts.Find(source); // throws if not found
    var to = accounts.Find(destination); // throws if not found
    var transaction = new Transaction(amount, from, to);
    transactions.Insert(transaction);
    transactions.Persist();
  }
}

However, this solution is less ORM friendly because of the Transaction constructor. Another way around would be to use Account as your root aggregate, and place the business rule validation and entities relationship handling code there :

public class Account {
  // implementation redacted

  public void AddTransaction(decimal amount, Account to) {
    if(amount > this.Balance) throw ... ;
    // more redacted validations
    this.Debitus.Add(new Transaction { Amount = amount, From = this, To = to });
  }
}

public class TransactionService {
  private readonly AccountRepository accounts; // by injection

  public void AddTransaction(decimal amount, int source, int destination) {
    var from = accounts.Find(source); // throws if not found
    var to = accounts.Find(destination); // throws if not found
    from.AddTransaction(amount, to);
    accounts.Persist();
  }
}


问:
FlatList stop scroll when button press during scroll
i would like to know if there is any way to stop scroll event on iOs and Android

Steps to reproduce

scroll FlatList / ScrollView

click any button

already tried "onScroll" but when i set scroll ref.scrollTo it will scroll when stroll stops

Example GIF:

答:
This is just an idea which I think might work, you may have to test it yourself.

Assuming you have the following state, controlling the modal.

```<code>const [open, setOpen] = useState(false);
</code>```

You can probably add a onScroll event to Flatlist (onScroll is inherited from ScrollView), then

```<code>   const onScroll = (e) => {
      if (open) e.preventDefault();
   }
</code>```

If there is a delay before e.preventDefault is called, since setting state is async, you may want to use useRef() instead of useState() for checking if (open) since scroll happens quickly.

Another idea is to probably use a listener for the flat list, to listen to scroll, using same logic above.

Body:
This is just an idea which I think might work, you may have to test it yourself.

Assuming you have the following state, controlling the modal.

const [open, setOpen] = useState(false);

You can probably add a onScroll event to Flatlist (onScroll is inherited from ScrollView), then

   const onScroll = (e) => {
      if (open) e.preventDefault();
   }

If there is a delay before e.preventDefault is called, since setting state is async, you may want to use useRef() instead of useState() for checking if (open) since scroll happens quickly.

Another idea is to probably use a listener for the flat list, to listen to scroll, using same logic above.


问:
A typical bracket balanced problem in ocaml
I'm trying to create a function which returns a boolean value to see if the string is balanced. But the result is just not what I expect. Can someone point out what I did wrong?

For example:

```<code>false = is_balanced")("
false = is_balanced "(a)b)"
true = is_balanced "fo"
true = is_balanced "a(b(c)d)e"
</code>```
```<code>let rec list_car ch = match ch with
  | "" -> []
  | ch -> (String.get ch 0 ) :: (list_car (String.sub ch 1 ( (String.length ch)-1) ) )  ;;

let is_balanced klm =
  let brk = list_car klm in
  let rec left = function
    | x::xs, 0 ->
      if x = ')' then false
      else left (xs, 0)
    | x::xs, level ->
      if x = '(' then left (xs, succ level)
      else if x = ')' then left (xs, pred level)
      else left (xs, level)
    | [], level -> if level = 0 then true else false
  in
  left (brk, 0) ;;
</code>```

But I got the following results:

```<code>is_balanced "(((";;
- : bool = true
 is_balanced "()";;
- : bool = false
</code>```

答:
Exploding a string into a list of char by using String.sub is inefficient and unnecessary. Your code can be rewritten as

```<code>let is_balanced s =
  let rec count pos level =
    if (* termination condition *) ... then level = 0
    else match s.[pos] with
     | '(' -> ...
     | ')' -> ...
     | _  -> ...
   in
   count 0 0
</code>```

With this variant, your mistake should disappear by itself.

Indeed one possible root issue with your previous code is that you are matching on both the list of chars and the level and end up mixing the logic of the termination condition with the logic of the level counting.

In particular, should the level at position n influence the effect of characters on the level at the position n+1?

Body:
I'm trying to create a function which returns a boolean value to see if the string is balanced. But the result is just not what I expect. Can someone point out what I did wrong?

For example:

false = is_balanced")("
false = is_balanced "(a)b)"
true = is_balanced "fo"
true = is_balanced "a(b(c)d)e"

let rec list_car ch = match ch with
  | "" -> []
  | ch -> (String.get ch 0 ) :: (list_car (String.sub ch 1 ( (String.length ch)-1) ) )  ;;

let is_balanced klm =
  let brk = list_car klm in
  let rec left = function
    | x::xs, 0 ->
      if x = ')' then false
      else left (xs, 0)
    | x::xs, level ->
      if x = '(' then left (xs, succ level)
      else if x = ')' then left (xs, pred level)
      else left (xs, level)
    | [], level -> if level = 0 then true else false
  in
  left (brk, 0) ;;

But I got the following results:

is_balanced "(((";;
- : bool = true
 is_balanced "()";;
- : bool = false

Body:
Exploding a string into a list of char by using String.sub is inefficient and unnecessary. Your code can be rewritten as

let is_balanced s =
  let rec count pos level =
    if (* termination condition *) ... then level = 0
    else match s.[pos] with
     | '(' -> ...
     | ')' -> ...
     | _  -> ...
   in
   count 0 0

With this variant, your mistake should disappear by itself.

Indeed one possible root issue with your previous code is that you are matching on both the list of chars and the level and end up mixing the logic of the termination condition with the logic of the level counting.

In particular, should the level at position n influence the effect of characters on the level at the position n+1?


问:
Is the C# static constructor thread safe?
In other words, is this Singleton implementation thread safe:</p>

```<code>public class Singleton
{
    private static Singleton instance;

    private Singleton() { }

    static Singleton()
    {
        instance = new Singleton();
    }

    public static Singleton Instance
    {
        get { return instance; }
    }
}
</code>```

答:
Static constructors are guaranteed to fire only once per App Domain so your approach should be OK. However, it is functionally no different from the more concise, inline version:

```<code>private static readonly Singleton instance = new Singleton();
</code>```

Thread safety is more of an issue when you are lazily initializing things.

ContentLicense: CC BY-SA 2.5

Body:
In other words, is this Singleton implementation thread safe:

public class Singleton
{
    private static Singleton instance;

    private Singleton() { }

    static Singleton()
    {
        instance = new Singleton();
    }

    public static Singleton Instance
    {
        get { return instance; }
    }
}

Body:
Static constructors are guaranteed to fire only once per App Domain so your approach should be OK. However, it is functionally no different from the more concise, inline version:

private static readonly Singleton instance = new Singleton();

Thread safety is more of an issue when you are lazily initializing things.


问:
Managing large volumes of data - stored procedures or datasets or other...?
I have an application that imports large volumes of data daily, several 100 thousands records.

Data comes from different sources. The data is read using C#, then bulk inserted into the database.

This data is then processed:

different tables are linked

new tables are generated

data is corrected using complicated algorithmns (totals of certain tables have to total zero)

Most of this processing is done in stored procedures.

Although some of the complex processing would be simpler in C#, the extraction of the data into a dataset and its reinjection would slow things down considerably.

You may ask why I do not process the data before inserting it into the database, but I do not think it practical to manipulate 100,000s of records in memory, and the SQLs set based commands help when creating lots of records.

This will probably spark up the age old question of using stored procedures and their pros and cons.
(eg. How do you unit test stored procedures?)

What I would like in response, is your experience with large volumes of data and how you tackled the problem.

答:
I would use SSIS or DTS (assuming you are talking about MSSQL). They are made for that purpose and work with SPs if you need them.

Another option is to preprocess the data using Perl. Even though it sounds like a wierd suggestion, Perl is actually extremely fast in these scenarios. I've used it in the past to process billions of records in reasonable time (i.e. days instead of weeks).

Regarding "How do you Unit Test store procedures", you unit test them with MBUnit like anything else. Only bit of advice: the setup and rollback of the data can be tricky, you can either use a DTS transaction or explicit SQL statements.

OwnerDisplayName: ThatBloke

OwnerDisplayName: Sklivvz


问:
When should I use semicolons in SQL Server?
While checking some code on the web and scripts generated by SQL Server Management Studio I have noticed that some statements are ended with a semicolon.

So when should I use it?

答:
By default, SQL statements are terminated with semicolons. You  use a semicolon to terminate statements unless you've (rarely) set a new statement terminator.

If you're sending just one statement, technically you can dispense with the statement terminator; in a script, as you're sending more than one statement, you need it.

In practice, always include the terminator even if you're just sending one statement to the database.

Edit: in response to those saying statement terminators are not required by [particular RDBMS], while that may be true, they're required by the ANSI SQL Standard. In all programming, if we can adhere to a Standard without loss of functionality, we should, because then neither our code or our habits are tied to one proprietary vendor.

With some C compilers, it's possible to have main return void, even though the Standard requires main to return int. But doing so makes our code, and ourselves, less portable.

The biggest difficulty in programming effectively isn't learning new things, it's unlearning bad habits. To the extent that we can avoid acquiring bad habits in the first place, it's a win for us, for our code, and for anyone reading or using our code.

OwnerDisplayName: apinto

LastEditorDisplayName: Rich B

OwnerDisplayName: tpdi

LastEditorDisplayName: tpdi


问:
SendGrid emailing API , which types of email attachment supported by SendGrid
I'm using next code for send email attachments:

```<code>Attachments attachments3 = new Attachments();
Base64 x = new Base64();
String imageDataString = x.encodeAsString(fileData);
attachments3.setContent(imageDataString);
attachments3.setType("image/png");//"application/pdf"
attachments3.setFilename("x.png");
attachments3.setDisposition("attachment");
attachments3.setContentId("Banner");
mail.addAttachments(attachments3);
</code>```

Here there is setter setType.

Do SendGrid have white list for type of attachment?

For example i can't send .exe or .rar files in another services. I want know prohibitions for SendGrid.

答:
According to their documentation the only limit is 30MB or less.

https://docs.sendgrid.com/api-reference/mail-send/limitations

From having used SendGrid in the past, I would be surprised if they limit what file types you can send.

Body:
I'm using next code for send email attachments:

Attachments attachments3 = new Attachments();
Base64 x = new Base64();
String imageDataString = x.encodeAsString(fileData);
attachments3.setContent(imageDataString);
attachments3.setType("image/png");//"application/pdf"
attachments3.setFilename("x.png");
attachments3.setDisposition("attachment");
attachments3.setContentId("Banner");
mail.addAttachments(attachments3);

Here there is setter setType.

Do SendGrid have white list for type of attachment?

For example i can't send .exe or .rar files in another services. I want know prohibitions for SendGrid.


问:
Something missing or additional in my Do While loop, how do i fix it
```<code>            String message = "Hello " + name + ", Do you know what day it is today?";
            answer = JOptionPane.showConfirmDialog(frame, message);
            JOptionPane.showMessageDialog(null, "Come on think a little harder", "Are you new here?",
                    JOptionPane.INFORMATION_MESSAGE);

        } while (answer == JOptionPane.NO_OPTION);

        if (answer == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null, "Great!", "Yay", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}
</code>```

When i press No, it will say "come on think a little harder" and loop back to 'do you know what day it is' but when i press yes, it still says 'come on think a little harder" and then only says "great'

What i want it to say when i press yes is only 'great'

I think i may have misplaced something that should not/should be in the while loop im not really sure..

答:
You just have to add an if condition , otherwise Come on think a little harder will be exectued for both yes and no options.

```<code>do {
  String message = "Hello " + name + ", Do you know what day it is today?";
  answer = JOptionPane.showConfirmDialog(frame, message);

  if (answer == JOptionPane.NO_OPTION) { // Add if condition
    JOptionPane.showMessageDialog(null, "Come on think a little harder", "Are you new here?",
      JOptionPane.INFORMATION_MESSAGE);
  }

} while (answer == JOptionPane.NO_OPTION);

if (answer == JOptionPane.YES_OPTION) {
  JOptionPane.showMessageDialog(null, "Great!", "Yay", JOptionPane.INFORMATION_MESSAGE);
}
</code>```

Body:
            String message = "Hello " + name + ", Do you know what day it is today?";
            answer = JOptionPane.showConfirmDialog(frame, message);
            JOptionPane.showMessageDialog(null, "Come on think a little harder", "Are you new here?",
                    JOptionPane.INFORMATION_MESSAGE);

        } while (answer == JOptionPane.NO_OPTION);

        if (answer == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null, "Great!", "Yay", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

When i press No, it will say "come on think a little harder" and loop back to 'do you know what day it is' but when i press yes, it still says 'come on think a little harder" and then only says "great'

What i want it to say when i press yes is only 'great'

I think i may have misplaced something that should not/should be in the while loop im not really sure..

Body:
You just have to add an if condition , otherwise Come on think a little harder will be exectued for both yes and no options.

do {
  String message = "Hello " + name + ", Do you know what day it is today?";
  answer = JOptionPane.showConfirmDialog(frame, message);

  if (answer == JOptionPane.NO_OPTION) { // Add if condition
    JOptionPane.showMessageDialog(null, "Come on think a little harder", "Are you new here?",
      JOptionPane.INFORMATION_MESSAGE);
  }

} while (answer == JOptionPane.NO_OPTION);

if (answer == JOptionPane.YES_OPTION) {
  JOptionPane.showMessageDialog(null, "Great!", "Yay", JOptionPane.INFORMATION_MESSAGE);
}


问:
Dependabot does not find latest commit
I'm exploring how Dependabot works and it isn't working as I expect.

I've created 2 private Golang repos (one, two) with one depending on two:

one's go.mod:

```<code>module github.com/dazwilkin/one

go 1.17

require github.com/dazwilkin/two v0.0.0-20210927170438-e7aa41e4107b
</code>```

NOTE e7aa41e4107b is a prior commit intentionally in order to check VS Code's and Dependabot's update checking.

And dependabot.yml:

```<code>version: 2
updates:
  - package-ecosystem: "gomod"
    directory: "/"
    schedule:
      interval: "daily"
</code>```

two's 2 most recent commits are:

curl \
--silent \
--header "Authorization: Bearer ${TOKEN}" \
https://api.github.com/repos/${OWNER}/${REPO}/commits \
| jq -r '.[]|{"sha":.sha,"date":.commit.committer.date}'
```
<p>And:</p>
```{
  "sha": "b2f2074829aa61218d7e38f27bb6051ccd97ab7a",
  "date": "2021-09-27T18:03:33Z"
}
{
  "sha": "e7aa41e4107b8c28f99cadfe55b831380730e808",
  "date": "2021-09-27T17:04:38Z"
}
```
<blockquote>
<p><strong>NOTE</strong> <code>b2f2074829aa</code> is the commit I'm expecting to be told about and <code>e7aa41e4107b</code> is the prior commit on <code>two</code> that <code>one</code> continues to reference.</p>
</blockquote>
<p>VS Code quickly determined that an update is available and:</p>
<pre class="lang-sh prettyprint-override"><code>go list -m -u all

github.com/dazwilkin/one
github.com/dazwilkin/two v0.0.0-20210927170438-e7aa41e4107b [v0.0.0-20210927180333-b2f2074829aa]
</code>```

NOTE Correctly identifying the latest commit (b2f2074829aa) to replace the prior commit (e7aa41e4107b)

But, after 22 hours and repeated forced updates, dependabot continues to report that e7aa41e4107b is current:

```<code>updater | INFO <job_214390230> Starting job processing
updater | INFO <job_214390230> Starting update job for DazWilkin/one
updater | INFO <job_214390230> Checking if github.com/dazwilkin/two 0.0.0-20210927170438-e7aa41e4107b needs updating
updater | INFO <job_214390230> Latest version is 0.0.0-20210927170438-e7aa41e4107b
updater | INFO <job_214390230> No update needed for github.com/dazwilkin/two 0.0.0-20210927170438-e7aa41e4107b
updater | INFO <job_214390230> Finished job processing
</code>```

NOTE Dependabot appears to have no issue accessing github.com/dazwilkin/two but it doesn't find the most recent commit.

Is this just an eventual consistency issue and I need wait longer?

Update I've waited another 24 hours and it continues to find the earlier commit as the latest version

Or am I misunderstanding or misconfiguring Dependabot?

One perhaps relevant issue that my GitHub account is mixed-case DazWilkin but, for simplicity, I'm publishing and referencing the Golang Modules using all-lowercase (github.com/dazwilkin). However Dependabot appears to have no issues finding the prior commit.

答: I believe this is because dependabot doesn't support pseudoversions - https://github.com/dependabot/dependabot-core/issues/3017</p>

Title: Dependabot does not find latest commit

Body:
I'm exploring how Dependabot works and it isn't working as I expect.

I've created 2 private Golang repos (one, two) with one depending on two:

one's go.mod:

module github.com/dazwilkin/one

go 1.17

require github.com/dazwilkin/two v0.0.0-20210927170438-e7aa41e4107b

NOTE e7aa41e4107b is a prior commit intentionally in order to check VS Code's and Dependabot's update checking.

And dependabot.yml:

version: 2
updates:
  - package-ecosystem: "gomod"
    directory: "/"
    schedule:
      interval: "daily"

two's 2 most recent commits are:

curl \
--silent \
--header "Authorization: Bearer ${TOKEN}" \
https://api.github.com/repos/${OWNER}/${REPO}/commits \
| jq -r '.[]|{"sha":.sha,"date":.commit.committer.date}'

And:

{
  "sha": "b2f2074829aa61218d7e38f27bb6051ccd97ab7a",
  "date": "2021-09-27T18:03:33Z"
}
{
  "sha": "e7aa41e4107b8c28f99cadfe55b831380730e808",
  "date": "2021-09-27T17:04:38Z"
}

NOTE b2f2074829aa is the commit I'm expecting to be told about and e7aa41e4107b is the prior commit on two that one continues to reference.

VS Code quickly determined that an update is available and:

go list -m -u all

github.com/dazwilkin/one
github.com/dazwilkin/two v0.0.0-20210927170438-e7aa41e4107b [v0.0.0-20210927180333-b2f2074829aa]

NOTE Correctly identifying the latest commit (b2f2074829aa) to replace the prior commit (e7aa41e4107b)

But, after 22 hours and repeated forced updates, dependabot continues to report that e7aa41e4107b is current:

updater | INFO <job_214390230> Starting job processing
updater | INFO <job_214390230> Starting update job for DazWilkin/one
updater | INFO <job_214390230> Checking if github.com/dazwilkin/two 0.0.0-20210927170438-e7aa41e4107b needs updating
updater | INFO <job_214390230> Latest version is 0.0.0-20210927170438-e7aa41e4107b
updater | INFO <job_214390230> No update needed for github.com/dazwilkin/two 0.0.0-20210927170438-e7aa41e4107b
updater | INFO <job_214390230> Finished job processing

NOTE Dependabot appears to have no issue accessing github.com/dazwilkin/two but it doesn't find the most recent commit.

Is this just an eventual consistency issue and I need wait longer?

Update I've waited another 24 hours and it continues to find the earlier commit as the latest version

Or am I misunderstanding or misconfiguring Dependabot?

One perhaps relevant issue that my GitHub account is mixed-case DazWilkin but, for simplicity, I'm publishing and referencing the Golang Modules using all-lowercase (github.com/dazwilkin). However Dependabot appears to have no issues finding the prior commit.


问:
how to download large files without memory issues in java
When I am trying to download a large file which is of 260MB from server, I get this error: java.lang.OutOfMemoryError: Java heap space. I am sure my heap size is less than 252MB. Is there any way I can download large files without increasing heap size?

How I can download large files without getting this issue? My code is given below:

```<code>String path= "C:/temp.zip";
response.addHeader("Content-Disposition", "attachment; filename=\"test.zip\"");
byte[] buf = new byte[1024];
try {

             File file = new File(path);
             long length = file.length();
             BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
             ServletOutputStream out = response.getOutputStream();

             while ((in != null) && ((length = in.read(buf)) != -1)) {
             out.write(buf, 0, (int) length);
             }
             in.close();
             out.close();
</code>```

答:
There are 2 places where I can see you could potentially be building up memory usage:

In the buffer reading your input file.

In the buffer writing to your output stream (HTTPOutputStream?)

For #1 I would suggest reading directly from the file via FileInputStream without the BufferedInputStream. Try this first and see if it resolves your issue. ie:

```<code>FileInputStream in = new FileInputStream(file);
</code>```

instead of:

```<code>BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
</code>```

If #1 does not resolve the issue, you could try periodically flushing the output stream after so much data is written (decrease chunk size if necessary):

ie:

```<code>try
{
    FileInputStream fileInputStream  = new FileInputStream(file);
    byte[] buf=new byte[8192];
    int bytesread = 0, bytesBuffered = 0;
    while( (bytesread = fileInputStream.read( buf )) > -1 ) {
        out.write( buf, 0, bytesread );
        bytesBuffered += bytesread;
        if (bytesBuffered > 1024 * 1024) { //flush after 1MB
            bytesBuffered = 0;
            out.flush();
        }
    }
}
finally {
    if (out != null) {
        out.flush();
    }
}
</code>```

Body:
When I am trying to download a large file which is of 260MB from server, I get this error: java.lang.OutOfMemoryError: Java heap space. I am sure my heap size is less than 252MB. Is there any way I can download large files without increasing heap size?

How I can download large files without getting this issue? My code is given below:

String path= "C:/temp.zip";
response.addHeader("Content-Disposition", "attachment; filename=\"test.zip\"");
byte[] buf = new byte[1024];
try {

             File file = new File(path);
             long length = file.length();
             BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
             ServletOutputStream out = response.getOutputStream();

             while ((in != null) && ((length = in.read(buf)) != -1)) {
             out.write(buf, 0, (int) length);
             }
             in.close();
             out.close();

Body:
There are 2 places where I can see you could potentially be building up memory usage:

In the buffer reading your input file.

In the buffer writing to your output stream (HTTPOutputStream?)

For #1 I would suggest reading directly from the file via FileInputStream without the BufferedInputStream. Try this first and see if it resolves your issue. ie:

FileInputStream in = new FileInputStream(file);

instead of:

BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

If #1 does not resolve the issue, you could try periodically flushing the output stream after so much data is written (decrease chunk size if necessary):

ie:

try
{
    FileInputStream fileInputStream  = new FileInputStream(file);
    byte[] buf=new byte[8192];
    int bytesread = 0, bytesBuffered = 0;
    while( (bytesread = fileInputStream.read( buf )) > -1 ) {
        out.write( buf, 0, bytesread );
        bytesBuffered += bytesread;
        if (bytesBuffered > 1024 * 1024) { //flush after 1MB
            bytesBuffered = 0;
            out.flush();
        }
    }
}
finally {
    if (out != null) {
        out.flush();
    }
}


问:
Change process.env variable for single test using JEST
i have the following code in a file that i'm trying to test:

foo.js

```<code>let localEnv = (process.env.LOCAL_ENVIRONMENT).toLowerCase() === 'prod' ? 'prod' : 'stage';
</code>```

currently, i set this value using setupFiles which points to env.js and it contains:

```<code>process.env.LOCAL_ENVIRONMENT = 'prod';
</code>```

my question is, how do i change process.env.LOCAL_ENVIRONMENT to test (or anything else) in foo.test.js? Just need to do this in a single test, so this line will be covered in my test coverage.

I tried doing something like this but it didn't work...

foo.test.js

```<code>test('Nonprod', async () => {
  process.env.LOCAL_ENVIRONMENT = 'test';
  ...
});
</code>```

答:
The most easier way to test it for a specific test case is to set the test into a describe scope and to apply / remove the env value in beforeAll / afterAll or beforeEach / afterEach hooks depending on your needs.</p>
```<code>describe('Test example', () => {
  describe('prod (default)', () => {
    test('do the thing', () => {
      doTheThing(); // process.env.LOCAL_ENVIRONMENT is by default 'prod' because of your setupFiles
    });
  });
  describe('test env', () => {
    const oldEnv = process.env.LOCAL_ENVIRONMENT; // or { ...process.env } to copy the whole env

    beforeAll(() => {
      process.env.LOCAL_ENVIRONMENT = 'test';
    });
    afterAll(() => {
      process.env.LOCAL_ENVIRONMENT = oldEnv; // do not forget to do this
    });

    test('do the thing', () => {
      doTheThing(); // process.env.LOCAL_ENVIRONMENT is 'test' here
    });
  });
});
</code>```

Body:
i have the following code in a file that i'm trying to test:

foo.js

let localEnv = (process.env.LOCAL_ENVIRONMENT).toLowerCase() === 'prod' ? 'prod' : 'stage';

currently, i set this value using setupFiles which points to env.js and it contains:

process.env.LOCAL_ENVIRONMENT = 'prod';

my question is, how do i change process.env.LOCAL_ENVIRONMENT to test (or anything else) in foo.test.js? Just need to do this in a single test, so this line will be covered in my test coverage.

I tried doing something like this but it didn't work...

foo.test.js

test('Nonprod', async () => {
  process.env.LOCAL_ENVIRONMENT = 'test';
  ...
});

Body:
The most easier way to test it for a specific test case is to set the test into a describe scope and to apply / remove the env value in beforeAll / afterAll or beforeEach / afterEach hooks depending on your needs.

describe('Test example', () => {
  describe('prod (default)', () => {
    test('do the thing', () => {
      doTheThing(); // process.env.LOCAL_ENVIRONMENT is by default 'prod' because of your setupFiles
    });
  });
  describe('test env', () => {
    const oldEnv = process.env.LOCAL_ENVIRONMENT; // or { ...process.env } to copy the whole env

    beforeAll(() => {
      process.env.LOCAL_ENVIRONMENT = 'test';
    });
    afterAll(() => {
      process.env.LOCAL_ENVIRONMENT = oldEnv; // do not forget to do this
    });

    test('do the thing', () => {
      doTheThing(); // process.env.LOCAL_ENVIRONMENT is 'test' here
    });
  });
});


问:
pdo bind param for multiple when and then statements
I need to update multiple rows in a single table at once. So I used following query:

```<code>UPDATE color
    SET color_id= CASE color_id
        WHEN 45 THEN 56
        WHEN 64 THEN 78
    END
WHERE color_id IN (45,64)
</code>```

I have to use prepared statement in pdo structure, so altered it as:

```<code>$query = "UPDATE color SET color_id= CASE color_id WHEN :old THEN :new END WHERE color_id = :old";
$stmt = $conn->prepare($query);
$params[0] = array(array("var"=>":old","val"=>"45"),array("var"=>":new","val"=>"56"));
$params[1] = array(array("var"=>":old","val"=>"64"),array("var"=>":new","val"=>"78"));
 foreach ($params as $value) {
    foreach ($value as $row) {
        $stmt->bindParam($value['var'], $value['val'], PDO::PARAM_STR);
    }
    $stmt->execute();
 }

 $result = $stmt->fetchAll();
</code>```

I know this method will be bit slower than previous method. My main goal is to prevent sql injection. Is there any other way of implement 1st query with prepared statement to make it as fast as 1st query?

答:
If the data is only numbers, then testing each number with is_numeric() would suffice to prevent SQL Injection.

If you are working with strings, then it is better to simulate the escaping that PDO will do.  I think that addslashes() will suffice.

(There are, unfortunately, queries where the APIs are not flexible enough.)

Body:
I need to update multiple rows in a single table at once. So I used following query:

UPDATE color
    SET color_id= CASE color_id
        WHEN 45 THEN 56
        WHEN 64 THEN 78
    END
WHERE color_id IN (45,64)

I have to use prepared statement in pdo structure, so altered it as:

$query = "UPDATE color SET color_id= CASE color_id WHEN :old THEN :new END WHERE color_id = :old";
$stmt = $conn->prepare($query);
$params[0] = array(array("var"=>":old","val"=>"45"),array("var"=>":new","val"=>"56"));
$params[1] = array(array("var"=>":old","val"=>"64"),array("var"=>":new","val"=>"78"));
 foreach ($params as $value) {
    foreach ($value as $row) {
        $stmt->bindParam($value['var'], $value['val'], PDO::PARAM_STR);
    }
    $stmt->execute();
 }

 $result = $stmt->fetchAll();

I know this method will be bit slower than previous method. My main goal is to prevent sql injection. Is there any other way of implement 1st query with prepared statement to make it as fast as 1st query?


问:
How do you convert an integer into a date (format YYYY) in R
I have a big data set, filled with dates that are in integer form, and different types of integer form (YYYYMMDD, YYYYMM and YYYY)

I just want to get all of them to YYYY.

I first tried splitting up the data frame into three data frames with the different respective date (integer) forms. And then I tried to change the data form from integer to date.

Below is me splitting up the data and then all the various things i have tried and subsequently commented out.

```<code>Opera_A <- Opera_split$Date_Format_A
Opera_B <- Opera_split$Date_Format_B
Opera_C <- Opera_split$Date_Format_C

lubridate::dmy(Opera_C$Composer_Born)
#as.Date(Opera_C$Composer_Born, "%m/%d/%y")
#Composer_Born_C <- data.frame(Composer_Born)
#Opera_C <- Opera_C %>% mutate(df,dateTime=as.Date(Composer_Born, format = "%Y-%m-%d"))
#Opera_C <- transform(Opera_C,Composer_Born=as.Date(as.character(Integer),"%Y%m%d"))
# Opera_C$Composer_Born <- as.Date(Opera_C$Composer_Born, '%Y-%m-%d')
</code>```

I keep getting Error in Opera_C$Composer_Born : $ operator is invalid for atomic vectors --- do i have to turn these vectors into dfs or can i just convert them directly?

Any help much appreciated --- I'm an R beginner!

Thank you

答:
If you want them to be recognized as dates, you could do this:</p>
```<code>date <- as.numeric(19351225)

as.Date(paste(substr(as.character(date), 1, 4),"-01-01",sep=""),format="%Y-%m-%d")
</code>```

Body:
I have a big data set, filled with dates that are in integer form, and different types of integer form (YYYYMMDD, YYYYMM and YYYY)

I just want to get all of them to YYYY.

I first tried splitting up the data frame into three data frames with the different respective date (integer) forms. And then I tried to change the data form from integer to date.

Below is me splitting up the data and then all the various things i have tried and subsequently commented out.

Opera_A <- Opera_split$Date_Format_A
Opera_B <- Opera_split$Date_Format_B
Opera_C <- Opera_split$Date_Format_C

lubridate::dmy(Opera_C$Composer_Born)
#as.Date(Opera_C$Composer_Born, "%m/%d/%y")
#Composer_Born_C <- data.frame(Composer_Born)
#Opera_C <- Opera_C %>% mutate(df,dateTime=as.Date(Composer_Born, format = "%Y-%m-%d"))
#Opera_C <- transform(Opera_C,Composer_Born=as.Date(as.character(Integer),"%Y%m%d"))
# Opera_C$Composer_Born <- as.Date(Opera_C$Composer_Born, '%Y-%m-%d')

I keep getting Error in Opera_C$Composer_Born : $ operator is invalid for atomic vectors --- do i have to turn these vectors into dfs or can i just convert them directly?

Any help much appreciated --- I'm an R beginner!

Thank you

Body:
If you want them to be recognized as dates, you could do this:

date <- as.numeric(19351225)

as.Date(paste(substr(as.character(date), 1, 4),"-01-01",sep=""),format="%Y-%m-%d")


问:
Can you use an if else statement with Curl command in bash script
I have command I am running with works for one enviornment in a bash script e.g.

so for example

ZONE_ID=prod

```<code>#!/bin/bash

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'
</code>```

What i would like to do is change the data item if possible for a different environment depending on the zone_id

ZONE_ID=prod
ZONE_ID=UAT

for example UAT would be

```<code>curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
--data '{"files":["http://www.example.com/css/styles.css",{"url":"http://www.example.com/cat_picture.jpg","headers":{"Origin":"https://www.cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}'
</code>```

So what I kind of want to do is the following, but this doesnt work

```<code>#!/bin/bash

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
if [$ZONE_ID -q prod]; then
--data '{"purge_everything":true}'
else
--data '{"files":["http://www.example.com/css/styles.css",{"url":"http://www.example.com/cat_picture.jpg","headers":{"Origin":"https://www.cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}'

</code>```

答:
I would recommend set variable before use it in curl command

example as follow

DATA=""
if [ "$ZONE_ID" = "prod" ]; then
DATA='{"purge_everything":true}'
else
DATA='{"files":["http://www.example.com/css/styles.css",{"url":"http://www.examp
le.com/cat_picture.jpg","headers":{"Origin":"https://www.cloudflare.com","CF-IPC
ountry":"US","CF-Device-Type":"desktop"}}]}'
fi

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache"
 \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
--data $DATA
```

Body:
I have command I am running with works for one enviornment in a bash script e.g.

so for example

ZONE_ID=prod

#!/bin/bash

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'

What i would like to do is change the data item if possible for a different environment depending on the zone_id

ZONE_ID=prod
ZONE_ID=UAT

for example UAT would be

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
--data '{"files":["http://www.example.com/css/styles.css",{"url":"http://www.example.com/cat_picture.jpg","headers":{"Origin":"https://www.cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}'

So what I kind of want to do is the following, but this doesnt work

#!/bin/bash

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
if [$ZONE_ID -q prod]; then
--data '{"purge_everything":true}'
else
--data '{"files":["http://www.example.com/css/styles.css",{"url":"http://www.example.com/cat_picture.jpg","headers":{"Origin":"https://www.cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}'


问:
What do scatter_kws and line_kws do in seaborn lmplot
I am trying to understand the below code snippet.

```<code>sns.lmplot('num_items', 'total_value', data=log_carts,
           scatter_kws={'s': 1, 'alpha': 0.1},
           line_kws={'lw': 2, 'color': '#4682b4'})
</code>```

I understand that lmplot gives a regression line for the variables 'num_items' and 'total_value' from the dataframe 'log_carts' after plotting a scatter plot. But I couldn't understand what does scatter_kws and line_kws do to the plot. I searched the documentation of lmplot but it just says these two arguments are "additional keyword arguments to pass to plt.scatter and plt.plot" which doesn't help for me. I am looking for explanation for every element used in those two arguments.

答:
I was also looking at how to improve the line styling of seaborn chart.

There are more options on to this.

For instance, to set some additional styling on line you could do (for a dashed line):

```<code>sns.lmplot('Flour', 'Sugar', data=coba, hue='Type', palette='Set1', fit_reg=True, scatter_kws={"s": 780},\
    line_kws={"lw":25, 'linestyle':'--'});
</code>```

You could find all possible settings here: matplotlib docs for line styling

Body:
I am trying to understand the below code snippet.

sns.lmplot('num_items', 'total_value', data=log_carts,
           scatter_kws={'s': 1, 'alpha': 0.1},
           line_kws={'lw': 2, 'color': '#4682b4'})

I understand that lmplot gives a regression line for the variables 'num_items' and 'total_value' from the dataframe 'log_carts' after plotting a scatter plot. But I couldn't understand what does scatter_kws and line_kws do to the plot. I searched the documentation of lmplot but it just says these two arguments are "additional keyword arguments to pass to plt.scatter and plt.plot" which doesn't help for me. I am looking for explanation for every element used in those two arguments.

Body:
I was also looking at how to improve the line styling of seaborn chart.

There are more options on to this.

For instance, to set some additional styling on line you could do (for a dashed line):

sns.lmplot('Flour', 'Sugar', data=coba, hue='Type', palette='Set1', fit_reg=True, scatter_kws={"s": 780},\
    line_kws={"lw":25, 'linestyle':'--'});

You could find all possible settings here: matplotlib docs for line styling


答:
Is this what you are going for?

```<code>import numpy as np
from sklearn.linear_model import LinearRegression as LR
import matplotlib.pyplot as plt

model1 = LR().fit(X,y)

preds = model1.predict(X)

plt.scatter(preds, y)

xpoints = ypoints = plt.xlim()
plt.plot(xpoints, ypoints, linestyle='--', color='k')
plt.show()
</code>```

Title: Scikitlearn Linear Regression with 2 features

Body:
I am new to scikit learn and trying to fit a simple linear regression model. I have a matrix X which contains 2 columns, c1 and c1^2, and I have the corresponding y values. I tried to fit a simple OLS model using scikit learn but am getting strange plot in the end. Any ideas on what I am doing wrong?

X = np.array([[ -0.016746535778021,   0.280446460564527],
  [-0.014577470749242,   0.212502653445002],
   [0.034515758657299,   1.191337595688933],
  [-0.047010075743201,   2.209947221381472],
   [0.036975119046363,   1.367159428492700],
  [-0.040686110015367,   1.655359548182586],
  [-0.004472010975766,   0.019998882167376],
  [0.026533634894789 ,  0.704033780729957],
  [-0.042797683100180,   1.831641678743394],
   [0.025374099383528,   0.643844919525139],
  [-0.031109553977308,   0.967804348667025],
   [0.027311768635213,   0.745932705983427],
  [-0.003263862013657,   0.010652795244191],
  [-0.001818276487116,   0.003306129383598],
  [-0.040719662402516,   1.658090906174888],
  [-0.050013243645495,   2.501324539943689],
  [-0.017411771548016,   0.303169788440313],
   [0.003588193696644,   0.012875134004637],
   [0.007085480261971,   0.050204030542776,],
   [0.046282369018539,   2.142057681968212],
   [0.014612289091657,   0.213518992498145]])*1e3

y = np.array([4.1702,
    4.0673,
   31.8731,
   10.6237,
   31.8360,
    4.9594,
    4.4516,
   22.2763,
   -0.0000,
   20.5038,
    3.8583,
   19.3651,
    4.8838,
   11.0972,
    7.4617,
    1.4769,
    2.7192,
   10.9269,
    8.3487,
   52.7819,
   13.3573])

from sklearn.linear_model import LinearRegression as LR

model1 = LR().fit(X,y)

import matplotlib.pyplot as plt

plt.plot(X[:,0],model1.predict(X))
plt.scatter(X[:,0],y,color = 'red')
plt.show()

Body:
Is this what you are going for?

import numpy as np
from sklearn.linear_model import LinearRegression as LR
import matplotlib.pyplot as plt

model1 = LR().fit(X,y)

preds = model1.predict(X)

plt.scatter(preds, y)

xpoints = ypoints = plt.xlim()
plt.plot(xpoints, ypoints, linestyle='--', color='k')
plt.show()


问:
Direct Table Access vs Stored Procedures in n-tier arcitecture

Possible Duplicate:

  What are the pros and cons to keeping SQL in Stored Procs versus Code

What is the drawbacks and benifits of using direct table access vs stored procedures to access data components

I am currently designing an application in an n-tier enviroment.

Also in regard with:
Security
Performance
application deployment issues

Which of the two methods is recommended

答:
I would agree that this is a duplicate of this question, but one thing I would add to the discussion is that DBAs often insist on stored proc access because it gives them control over access to the data.  Views are often also used for this reason.  When you grant direct table access you have to open the table up from a security perspective.

Depending on whether your apps log in as applications or if they log the users in directly (I've seen it done both ways) and depending on whether your app is mostly write (i.e. transactional system) or mostly read (i.e. reporting system) you (or your DBA - or auditor) may not want to leave access wide open.


问:
Cipher mismatch error while trying to access an app deployed in GKE as HTTPS Ingress
I am trying to deploy a springboot application running on 8080 port. My target is to have https protocol for custom subdomain with google managed-certificates.
here are my yamls.

deployment.yaml

```<code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  namespace: my-namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-deployment
      namespace: my-namespace
  template:
    metadata:
      labels:
        app: my-deployment
        namespace: my-namespace
    spec:
      containers:
        - name: app
          image: gcr.io/PROJECT_ID/IMAGE:TAG
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          resources:
            requests:
              memory: "256Mi"
              ephemeral-storage: "256Mi"
              cpu: "250m"
            limits:
              memory: "512Mi"
              ephemeral-storage: "512Mi"
              cpu: "250m"
</code>```

2.service.yaml

```<code>apiVersion: v1
kind: Service
metadata:
  name: my-service
  namespace: my-namespace
  annotations:
    cloud.google.com/backend-config: '{"default": "my-http-health-check"}'
spec:
  selector:
    app: my-deployment
    namespace: my-namespace
  type: NodePort
  ports:
    - port: 80
      name: http
      targetPort: http
      protocol: TCP
</code>```

ingress.yaml

```<code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  namespace: my-name-space
  annotations:
    kubernetes.io/ingress.global-static-ip-name: my-ip
    networking.gke.io/managed-certificates: my-cert
    kubernetes.io/ingress.class: "gce"
  labels:
    app: my-ingress
spec:
  rules:
    - host: my-domain.com
      http:
        paths:
          - pathType: ImplementationSpecific
            backend:
              service:
                name: my-service
                port:
                  name: http
</code>```

I followed various documentation, most of them could help to make http work but, couldn't make https work and ends with error ERR_SSL_VERSION_OR_CIPHER_MISMATCH. Looks like there is issue with "Global forwarding rule". Ports shows 443-443. What is the correct way to terminate the HTTPS traffic at loadbalancer and route it to backend app with http?

答:
From the information provided, I can see that the "ManagedCertificate" object is missing, you need to create a yaml file with the following structure:

```<code>apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata:
  name: my-cert
spec:
  domains:
    - <your-domain-name1>
    - <your-domain-name2>
</code>```

And then apply it with the command: kubectl apply -f file-name.yaml

Provisioning of the Google-managed certificate can take up to 60 minutes; you can check the status of the certificate using the following command: kubectl describe managedcertificate my-cert, wait for the status to be as "Active".

A few prerequisites you need to be aware, though:

You must own the domain name. The domain name must be no longer than
63 characters. You can use Google Domains or another registrar.

The cluster must have the HttpLoadBalancing add-on enabled.

Your "kubernetes.io/ingress.class" must be "gce".

You must apply Ingress and ManagedCertificate resources in the same
project and namespace.

Create a reserved (static) external IP address. Reserving a static IP
address guarantees that it remains yours, even if you delete the
Ingress. If you do not reserve an IP address, it might change,
requiring you to reconfigure your domain's DNS records.

Finally, you can take a look at the complete Google's guide on Creating an Ingress with a Google-managed certificate.

Body:
I am trying to deploy a springboot application running on 8080 port. My target is to have https protocol for custom subdomain with google managed-certificates.
here are my yamls.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  namespace: my-namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-deployment
      namespace: my-namespace
  template:
    metadata:
      labels:
        app: my-deployment
        namespace: my-namespace
    spec:
      containers:
        - name: app
          image: gcr.io/PROJECT_ID/IMAGE:TAG
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          resources:
            requests:
              memory: "256Mi"
              ephemeral-storage: "256Mi"
              cpu: "250m"
            limits:
              memory: "512Mi"
              ephemeral-storage: "512Mi"
              cpu: "250m"

2.service.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-service
  namespace: my-namespace
  annotations:
    cloud.google.com/backend-config: '{"default": "my-http-health-check"}'
spec:
  selector:
    app: my-deployment
    namespace: my-namespace
  type: NodePort
  ports:
    - port: 80
      name: http
      targetPort: http
      protocol: TCP

ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  namespace: my-name-space
  annotations:
    kubernetes.io/ingress.global-static-ip-name: my-ip
    networking.gke.io/managed-certificates: my-cert
    kubernetes.io/ingress.class: "gce"
  labels:
    app: my-ingress
spec:
  rules:
    - host: my-domain.com
      http:
        paths:
          - pathType: ImplementationSpecific
            backend:
              service:
                name: my-service
                port:
                  name: http

I followed various documentation, most of them could help to make http work but, couldn't make https work and ends with error ERR_SSL_VERSION_OR_CIPHER_MISMATCH. Looks like there is issue with "Global forwarding rule". Ports shows 443-443. What is the correct way to terminate the HTTPS traffic at loadbalancer and route it to backend app with http?

Body:
From the information provided, I can see that the "ManagedCertificate" object is missing, you need to create a yaml file with the following structure:

apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata:
  name: my-cert
spec:
  domains:
    - <your-domain-name1>
    - <your-domain-name2>

And then apply it with the command: kubectl apply -f file-name.yaml

Provisioning of the Google-managed certificate can take up to 60 minutes; you can check the status of the certificate using the following command: kubectl describe managedcertificate my-cert, wait for the status to be as "Active".

A few prerequisites you need to be aware, though:

You must own the domain name. The domain name must be no longer than
63 characters. You can use Google Domains or another registrar.

The cluster must have the HttpLoadBalancing add-on enabled.

Your "kubernetes.io/ingress.class" must be "gce".

You must apply Ingress and ManagedCertificate resources in the same
project and namespace.

Create a reserved (static) external IP address. Reserving a static IP
address guarantees that it remains yours, even if you delete the
Ingress. If you do not reserve an IP address, it might change,
requiring you to reconfigure your domain's DNS records.

Finally, you can take a look at the complete Google's guide on Creating an Ingress with a Google-managed certificate.


问:
What is the difference between def and defonce in Clojure?
What is the difference between def and defonce in Clojure?

When to use def over defonce or vice versa?

答:
As for when to use defonce, if you're using system with hot reloading (CLJS with mount and re-frame for example), defonce is useful to keep the state between reloads.

Similar situation when you re-evaluate source file yourself (e.g. in REPL) but want to keep the value of the var bound to the symbol.


Title: White-out output when trying to get the ploty treemap and plotly sunburst

Body:
I would like to create a treemap and a sunburst of a data structure, in order to arrange the hirearchy of the data.

The data structure is stored in an excel file under two columns 'Parent' and 'Child'. This data is extracted into a pandas DataFrame named my_data. Here I have converted the data to list:

my_data_parent = ['', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'D', 'E', 'E', 'E', 'F', 'F', 'F', 'F', 'G', 'G', 'G', 'H', 'AV', 'CF', 'CF', 'EL', 'EL', 'EV', 'EV', 'MT', 'DI', 'DI', 'DI', 'DI', 'I', 'I', 'I', 'J', 'EJ', 'EJ', 'EJ', 'EJ', 'K', 'I2', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'N', 'N', 'P', 'P', 'P4', 'R', 'Q', 'Q', 'Q', 'S', 'S', 'S', 'S', 'S', 'S2', 'S2', 'S2', 'S2', 'S2', 'S2', 'S3', 'S3', 'S3', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S6', 'S6', 'S6', 'S6', 'S6', 'S6', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S8', 'S8', 'S9', 'S9', 'S9', 'S9', 'S10', 'S10', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S12', 'S12', 'S13', 'S13', 'S13', 'S13', 'S14', 'S14', 'S14', 'S14', 'S14', 'S14', 'S15', 'T', 'T1', 'T3', 'T3', 'T3', 'T3', 'T5', 'T5', 'T5', 'T6', 'T6', 'T14', 'U', 'U1', 'V', 'V', 'V5', 'V6', 'V6', 'V6', 'V7', 'V7', 'V7', 'V9', 'V9', 'V11', 'V11', 'V11', 'V11', 'W', 'W', 'X', 'X', 'X']

my_data_child = ['A', 'B', 'C', 'D', 'E', 'CS', 'SC', 'CC', 'BU', 'F', 'EA', 'G', 'H', 'Nu', 'AV', 'CF', 'EL', 'EV', 'MT', 'DTT', 'DI', 'I', 'J', 'EI', 'EJ', 'K', 'F1', 'F2', 'F3', 'F4', 'I1', 'I2', 'G1', 'L', 'M', 'N', 'N1', 'P', 'P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10', 'R', 'Q', 'Q1', 'Q2', 'Q3', 'Q4', 'S', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13', 'S14', 'S15', 'T', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10', 'T11', 'T12', 'T13', 'T14', 'T15', 'T16', 'T17', 'T18', 'T19', 'T20', 'T21', 'T22', 'T23', 'T24', 'T25', 'T26', 'T27', 'T28', 'T29', 'T30', 'T31', 'T32', 'T33', 'T34', 'T35', 'T36', 'T37', 'T38', 'T39', 'T40', 'T41', 'T42', 'T43', 'T44', 'T45', 'T46', 'T47', 'T48', 'T49', 'T50', 'T51', 'T52', 'T53', 'T54', 'T55', 'T56', 'T57', 'T58', 'T59', 'T60', 'T61', 'T62', 'T63', 'T64', 'U', 'U1', 'U2', 'U3', 'U4', 'U5', 'U6', 'U7', 'U8', 'U9', 'U10', 'U11', 'U12', 'U13', 'U14', 'U15', 'U16', 'U17', 'U18', 'U19', 'U20', 'U21', 'U22', 'U23', 'U24', 'U25', 'U26', 'U27', 'U28', 'U29', 'U30', 'U31', 'V', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V90', 'V9', 'V10', 'V11', 'V12', 'V13', 'V14', 'V15', 'V16', 'V17', 'V18', 'V19', 'V20', 'W', 'X', 'X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9']

Surprisingly, the following piece of code is working for every other excel file (Parent and Child datas) except the one I need. There is no error in the execution, however the output of the code is a white-out space. I wrote the treemap to html, even there there is white-out space and also in the jpg image that I downloaded.

my_labels = my_data_child
my_parents = my_data_parent

my_parents[0]=''
my_labels[0]=my_parents[1]

fig = go.Figure(go.Treemap(
    labels=np.array(my_labels),
    parents=np.array(my_parents),
    hoverinfo= 'text',
    maxdepth=2,
    #marker=dict(
     #   colorscale='RdBu',
     #   cmid=0.5),
))

fig.update_layout(margin = dict(t=0, l=0, r=0, b=0),
                 uniformtext=dict(minsize=12,
                                 # mode='show',
                                 ),
                  width=1200, height=800
                 )
fig.show()
fig.write_html("Data_treemap.html")

The code to generate sunburst is similar. This is not the exact data I am working with. But it has the exact same structure. For the trail data I am able to get the treemap and sunburst but not for the actual data. I have invested some time in trying all combinations to make it work. Expecting to get some help here.

Body:
Since the sample data you included has the same structure as your actual data, but the sample data can be used to produce a treemap while your actual data doesn't work, I can't think of any explanation except that your actual data must have a typo.

For example, your code with the following lists:

my_data_parent = ['', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'D', 'E', 'E', 'E', 'F', 'F', 'F', 'F', 'G', 'G', 'G', 'H', 'AV', 'CF', 'CF', 'EL', 'EL', 'EV', 'EV', 'MT', 'DI', 'DI', 'DI', 'DI', 'I', 'I', 'I', 'J', 'EJ', 'EJ', 'EJ', 'EJ', 'K', 'I2', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'N', 'N', 'P', 'P', 'P4', 'R', 'Q', 'Q', 'Q', 'S', 'S', 'S', 'S', 'S', 'S2', 'S2', 'S2', 'S2', 'S2', 'S2', 'S3', 'S3', 'S3', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S5', 'S6', 'S6', 'S6', 'S6', 'S6', 'S6', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S7', 'S8', 'S8', 'S9', 'S9', 'S9', 'S9', 'S10', 'S10', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S12', 'S12', 'S13', 'S13', 'S13', 'S13', 'S14', 'S14', 'S14', 'S14', 'S14', 'S14', 'S15', 'T', 'T1', 'T3', 'T3', 'T3', 'T3', 'T5', 'T5', 'T5', 'T6', 'T6', 'T14', 'U', 'U1', 'V', 'V', 'V5', 'V6', 'V6', 'V6', 'V7', 'V7', 'V7', 'V9', 'V9', 'V11', 'V11', 'V11', 'V11', 'W', 'W', 'X', 'X', 'X']

my_data_child = ['A', 'B', 'C', 'D', 'E', 'CS', 'SC', 'CC', 'BU', 'F', 'EA', 'G', 'H', 'Nu', 'AV', 'CF', 'EL', 'EV', 'MT', 'DTT', 'DI', 'I', 'J', 'EI', 'EJ', 'K', 'F1', 'F2', 'F3', 'F4', 'I1', 'I2', 'G1', 'L', 'M', 'N', 'N1', 'P', 'P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10', 'R', 'Q', 'Q1', 'Q2', 'Q3', 'Q4', 'S', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13', 'S14', 'S15', 'T', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10', 'T11', 'T12', 'T13', 'T14', 'T15', 'T16', 'T17', 'T18', 'T19', 'T20', 'T21', 'T22', 'T23', 'T24', 'T25', 'T26', 'T27', 'T28', 'T29', 'T30', 'T31', 'T32', 'T33', 'T34', 'T35', 'T36', 'T37', 'T38', 'T39', 'T40', 'T41', 'T42', 'T43', 'T44', 'T45', 'T46', 'T47', 'T48', 'T49', 'T50', 'T51', 'T52', 'T53', 'T54', 'T55', 'T56', 'T57', 'T58', 'T59', 'T60', 'T61', 'T62', 'T63', 'T64', 'U', 'U1', 'U2', 'U3', 'U4', 'U5', 'U6', 'U7', 'U8', 'U9', 'U10', 'U11', 'U12', 'U13', 'U14', 'U15', 'U16', 'U17', 'U18', 'U19', 'U20', 'U21', 'U22', 'U23', 'U24', 'U25', 'U26', 'U27', 'U28', 'U29', 'U30', 'U31', 'V', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V90', 'V9', 'V10', 'V11', 'V12', 'V13', 'V14', 'V15', 'V16', 'V17', 'V18', 'V19', 'V20', 'W', 'X', 'X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9']

... produces this plot:

But if I create a typo by inserting a space in one of the parent labels

my_data_parent = ['', 'A', 'A ', 'A',...]

Then I do indeed get a completely blank white plot because the parent-child hierarchy no longer makes sense to plotly.

At this point, it's more of a debugging issue with your data than something plotly-specific, but I would encourage you to look through your actual data and make sure there are no typos like this. You said you manually mapped your actual data to your sample data - perhaps it's possible that you mapped a correct label and a label with a typo to the the same value.

One way forward could be to use python to perform a mapping from your actual labels to some new labels and see what happens.


问:
How can i get State from my store in Appjs?
I would like to see the state in my App.js
i am importing like this

```<code>import configureStore from './src/configureStore'
let store = configureStore()
</code>```

so i am injecting in my App.js

```<code><Provider store={store} theme={theme}>
  {console.log('11111',store)}
  <NavigationContainer>
</code>```

in console only i can see this:
{dispatch: ƒ, subscribe: ƒ, getState: ƒ, replaceReducer: ƒ, @@observable: ƒ}

how can i see the state?

btw: configureStore is:

```<code>import {createStore, applyMiddleware} from 'redux';
import Reducers from './reducers'
import thunk from 'redux-thunk'

export default function configureStore () {
    let store = createStore(Reducers, applyMiddleware(thunk))
    return store
}
</code>```

答:
for this u need Selector hook from "react-redux"

...
import {useSelector} from "react-redux";
...

...
const {yourStateKey} = useSelector((state) => state);
...
```
<p>this part can be different based how manny store you have</p>
<pre class="lang-js prettyprint-override"><code>(state) => state
</code>```

to set state value in store use "useDispatch" hook from "react-redux"

...
import {useDispatch} from "react-redux";
...
const dispatch = useDispatch();
...
dispatch(reduxActions.yourAction(yourValue))
```

Body:
I would like to see the state in my App.js
i am importing like this

import configureStore from './src/configureStore'
let store = configureStore()

so i am injecting in my App.js

<Provider store={store} theme={theme}>
  {console.log('11111',store)}
  <NavigationContainer>

in console only i can see this:
{dispatch: ƒ, subscribe: ƒ, getState: ƒ, replaceReducer: ƒ, @@observable: ƒ}

how can i see the state?

btw: configureStore is:

import {createStore, applyMiddleware} from 'redux';
import Reducers from './reducers'
import thunk from 'redux-thunk'

export default function configureStore () {
    let store = createStore(Reducers, applyMiddleware(thunk))
    return store
}

Body:
for this u need Selector hook from "react-redux"

...
import {useSelector} from "react-redux";
...

...
const {yourStateKey} = useSelector((state) => state);
...

this part can be different based how manny store you have

(state) => state

to set state value in store use "useDispatch" hook from "react-redux"

...
import {useDispatch} from "react-redux";
...
const dispatch = useDispatch();
...
dispatch(reduxActions.yourAction(yourValue))