问:
Kafka batch consumer without duplicate records
I have the following requirements, we are reading from a relational database with CDC insert/update on a specific table and import these as events into a Kafka topic.

e.g. jdbc-source-topic

```<code>|---------------------|------------------|------------------|
|      Timestamp      |        ID        |      Column      |
|---------------------|------------------|------------------|
|        10:00        |         1        |         A        |
|---------------------|------------------|------------------|
|        10:01        |         2        |         B        |
|---------------------|------------------|------------------|
|        10:01        |         1        |         C        |
|---------------------|------------------|------------------|
</code>```

At the end of the pipeline we would like to consume these events once per day and to avoid duplicates for the same ID.

e.g. target-topic

```<code>|---------------------|------------------|------------------|
|      Timestamp      |        ID        |      Column      |
|---------------------|------------------|------------------|
|        10:01        |         2        |         B        |
|---------------------|------------------|------------------|
|        10:01        |         1        |         C        |
|---------------------|------------------|------------------|
</code>```

The best solution in my mind is to have a consumer with a group_id (so that offset is store in kafka for the next day) that run once. But this means that every time the consumer runs it has to drop duplicates from the fetched records.

Considering that this table may be used also for KSQL Joins in future, I'm wondering if it exists a better approach using KSQL queries, in order for the consumer to fetch from a cleaned topic with one record for each key.

答:
If the only consumer of this data is ksqlDB then you may not need to de-dup as ksqlDB will correctly handle multiple updates to the same key if you import the topic as a TABLE in ksql, i.e. rather than doing:

```<code>CREATE STREAM FOO (... columns ...) WITH (...);
</code>```

Do:

```<code>CREATE TABLE FOO (... columns ...) WITH (...);
</code>```

At present, when ksqlDB processes such a change log it will output all / some of the duplicates, depending on how you've configured cache.max.bytes.buffering.

You may be able to avoid emitting the duplicates by using a 24 hour window and the up comming Suppress support.  Until then, if you wanted to remove the duplicates as you've suggested. You may also be able to get something working by writing your own Kafka Streams app to materialize the table into a state store and use the suppress api to remove the duplicates.

However, it's worth pointing out that, semantically, the duplicates don't cause any problems. The result of materializing the changelog into a table is
correct with and without the duplicates.  So, as I said at the start, removing duplicates may not even be necessary.

Body:
I have the following requirements, we are reading from a relational database with CDC insert/update on a specific table and import these as events into a Kafka topic.

e.g. jdbc-source-topic

|---------------------|------------------|------------------|
|      Timestamp      |        ID        |      Column      |
|---------------------|------------------|------------------|
|        10:00        |         1        |         A        |
|---------------------|------------------|------------------|
|        10:01        |         2        |         B        |
|---------------------|------------------|------------------|
|        10:01        |         1        |         C        |
|---------------------|------------------|------------------|

At the end of the pipeline we would like to consume these events once per day and to avoid duplicates for the same ID.

e.g. target-topic

|---------------------|------------------|------------------|
|      Timestamp      |        ID        |      Column      |
|---------------------|------------------|------------------|
|        10:01        |         2        |         B        |
|---------------------|------------------|------------------|
|        10:01        |         1        |         C        |
|---------------------|------------------|------------------|

The best solution in my mind is to have a consumer with a group_id (so that offset is store in kafka for the next day) that run once. But this means that every time the consumer runs it has to drop duplicates from the fetched records.

Considering that this table may be used also for KSQL Joins in future, I'm wondering if it exists a better approach using KSQL queries, in order for the consumer to fetch from a cleaned topic with one record for each key.

Body:
If the only consumer of this data is ksqlDB then you may not need to de-dup as ksqlDB will correctly handle multiple updates to the same key if you import the topic as a TABLE in ksql, i.e. rather than doing:

CREATE STREAM FOO (... columns ...) WITH (...);

Do:

CREATE TABLE FOO (... columns ...) WITH (...);

At present, when ksqlDB processes such a change log it will output all / some of the duplicates, depending on how you've configured cache.max.bytes.buffering.

You may be able to avoid emitting the duplicates by using a 24 hour window and the up comming Suppress support.  Until then, if you wanted to remove the duplicates as you've suggested. You may also be able to get something working by writing your own Kafka Streams app to materialize the table into a state store and use the suppress api to remove the duplicates.

However, it's worth pointing out that, semantically, the duplicates don't cause any problems. The result of materializing the changelog into a table is
correct with and without the duplicates.  So, as I said at the start, removing duplicates may not even be necessary.


来源: stackexchange

问:
mysql root password forgotten
I did not use PHP MySQL for quite a while and now I need to use it again. But the problem is I forget the password for the MySQL console. and getting error #1045 when trying to login in to PHPMyAdmin.

In the MySQL site I saw an article how to reset root password( http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html#resetting-permissions-windows)

Steps are

```<code>create a mysql-init.txt file containing UPDATE mysql.user SET Password=PASSWORD('newpass') WHERE User='root';
FLUSH PRIVILEGES;
</code>```

I saved it as C:\me\mysql-init

and in command prompt I wrote--

```<code>C:\wamp\bin\mysql\mysql5.5.8\bin\mysqld --init-file=C:\me\mysql-init.txt
</code>```

I tried with double backslashes also..but it is not working. MySQL console is asking for a password and it's not taking the new-one. What am I doing wrong? I have several tables there.what to do?

Thanks in advance.

答:
Mac OS Mojave

$ brew services stop mysql

$ pkill mysqld

// warning: deletes all tables
$ rm -rf /usr/local/var/mysql/

$ brew postinstall mysql

$ brew services restart mysql

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'p4ssword';
Query OK, 0 rows affected (0.04 sec)

mysql> exit
Bye
```

<p>Then you are back to normal for dev.</p>

<pre class="lang-sh prettyprint-override"><code>$ sudo mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
</code>```

I don't like to see the word root in my .env files, so I usually do this after, if making a site such as www.hockeysticks.net:

#
CREATE DATABASE hockeysticks;

CREATE USER 'hockeysticks'@'localhost' IDENTIFIED BY 'hockeysticks';
GRANT ALL PRIVILEGES ON hockeysticks.* TO 'hockeysticks'@'localhost';
```

<p>Then your localdev <code>.env</code> file is simple:</p>

<pre class="lang-sh prettyprint-override"><code>DB_DATABASE=hockeysticks
DB_USERNAME=hockeysticks
DB_PASSWORD=hockeysticks
</code>```

Note: If you need to retain your databases, use the skip-grant-tables method. That has 3 hard parts:

Make sure MySQL is stopped, so you can restart it with skip-grant-tables

Make sure your password update SQL syntax is correct for your MySQL version

Make sure you append the end of the query with FLUSH PRIVELEGES;

ContentLicense: CC BY-SA 4.0

Title: mysql root password forgotten

Body:
I did not use PHP MySQL for quite a while and now I need to use it again. But the problem is I forget the password for the MySQL console. and getting error #1045 when trying to login in to PHPMyAdmin.

In the MySQL site I saw an article how to reset root password( http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html#resetting-permissions-windows)

Steps are

create a mysql-init.txt file containing UPDATE mysql.user SET Password=PASSWORD('newpass') WHERE User='root';
FLUSH PRIVILEGES;

I saved it as C:\me\mysql-init

and in command prompt I wrote--

C:\wamp\bin\mysql\mysql5.5.8\bin\mysqld --init-file=C:\me\mysql-init.txt

I tried with double backslashes also..but it is not working. MySQL console is asking for a password and it's not taking the new-one. What am I doing wrong? I have several tables there.what to do?

Thanks in advance.

Body:
Mac OS Mojave

$ brew services stop mysql

$ pkill mysqld

// warning: deletes all tables
$ rm -rf /usr/local/var/mysql/

$ brew postinstall mysql

$ brew services restart mysql

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'p4ssword';
Query OK, 0 rows affected (0.04 sec)

mysql> exit
Bye

Then you are back to normal for dev.

$ sudo mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.

I don't like to see the word root in my .env files, so I usually do this after, if making a site such as www.hockeysticks.net:

#
CREATE DATABASE hockeysticks;

CREATE USER 'hockeysticks'@'localhost' IDENTIFIED BY 'hockeysticks';
GRANT ALL PRIVILEGES ON hockeysticks.* TO 'hockeysticks'@'localhost';

Then your localdev .env file is simple:

DB_DATABASE=hockeysticks
DB_USERNAME=hockeysticks
DB_PASSWORD=hockeysticks

Note: If you need to retain your databases, use the skip-grant-tables method. That has 3 hard parts:

Make sure MySQL is stopped, so you can restart it with skip-grant-tables

Make sure your password update SQL syntax is correct for your MySQL version

Make sure you append the end of the query with FLUSH PRIVELEGES;


问:
Get key name based on subkey value
I'm trying to setup some monitoring.  As part of that I need to parse some gnarly json output to retrieve a node ID, which changes each time the node is rebooted or the service restarts.  I always know the node name but not the "id".  The JSON looks something like this:

```<code>{
 "cluster_name":"cluster1",
 "nodes":
  {
   "generatednodeid1":{"name":"node01"},
   "generatednodeid2":{"name":"node2"}
  }
}
</code>```

Doing .nodes | keys gives me ["generatednodeid1","generatednodeid2"] as I'd expect.

I've tried .nodes[] | select(.name=="node2") but that only outputs {"name":"node2"}

What I really need to happen is if .name=="node2" then it gives me generatednodeid2

I've been beating my head against a wall.  I can't for the life of me figure out what I'm missing.  This seems so simple (probably is and I've looked at it too long).  Any ideas?

答:
Any ideas?

In this situation, the "to_entries" family of filters is helpful, e.g.:

```<code>.nodes
| to_entries[]
| select(.value.name == "node2")
| .key
</code>```

Title: Get key name based on subkey value

Body:
I'm trying to setup some monitoring.  As part of that I need to parse some gnarly json output to retrieve a node ID, which changes each time the node is rebooted or the service restarts.  I always know the node name but not the "id".  The JSON looks something like this:

{
 "cluster_name":"cluster1",
 "nodes":
  {
   "generatednodeid1":{"name":"node01"},
   "generatednodeid2":{"name":"node2"}
  }
}

Doing .nodes | keys gives me ["generatednodeid1","generatednodeid2"] as I'd expect.

I've tried .nodes[] | select(.name=="node2") but that only outputs {"name":"node2"}

What I really need to happen is if .name=="node2" then it gives me generatednodeid2

I've been beating my head against a wall.  I can't for the life of me figure out what I'm missing.  This seems so simple (probably is and I've looked at it too long).  Any ideas?

Body:
Any ideas?

In this situation, the "to_entries" family of filters is helpful, e.g.:

.nodes
| to_entries[]
| select(.value.name == "node2")
| .key


答:
You can do stack with set_index

```<code>d=df.set_index('Merc\Prod').stack().to_dict()
</code>```

Title: How to convert a dict of dicts to a dict of tuples in python

Body:
I read an excel distance matrix and generated a dataframe:

df = pd.read_excel(xlsx, "Dem_Prod")

   Merc\Prod    1   2   3   4
0   1          80   85  300 6
1   2         270   160 400 7
2   3         250   130 350 4
3   4         160   60  200 3
4   5         180   40  150 5

Hen I created a dict

d = df.set_index('Merc\Prod').T.to_dict()

{1: {1: 80, 2: 85, 3: 300, 4: 6}, 2: {1: 270, 2: 160, 3: 400, 4: 7}, 3: {1: 250, 2: 130, 3: 350, 4: 4}, 4: {1: 160, 2: 60, 3: 200, 4: 3}, 5: {1: 180, 2: 40, 3: 150, 4: 5}}

But I want a dict of tuples

{(1,1):80, (1,2):85, (1,3):300, (1,4):6, (2,1):270, (2,2):160, (2,3):400, (2,4):7, (3,1):250, (3,2):130, (3,3):350, (3,4):4, (4,1):160, (4,2):60, (4,3):200, (4,4):3, (5,1):180, (5,2):40, (5,3):150, (5,4):5 }

Body:
You can do stack with set_index

d=df.set_index('Merc\Prod').stack().to_dict()


问:
Cross Entropy Calculation in PyTorch tutorial
I'm reading the Pytorch tutorial of a multi-class classification problem. And I find the behavior of Loss calculation in Pytorch confuses me a lot. Can you help me with this?

The model used for classification goes like this:

```<code>class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
</code>```

The training process goes as follows:

```<code>optimizer.zero_grad()
outputs = net(inputs)
loss = nn.CrossEntropyLoss(outputs, labels)
loss.backward()
optimizer.step()
</code>```

My question is: What's the exact behavior of Loss calculation in Pytorch here? During each iteration, the input of nn.CrossEntropyLoss() has two parts:

The output of the model, which is a 10 by 1 tensor, with different values in it. This is a tensor without normalized into probability.

The label as a scalar, like 1 or 2 or 3.

As far as I know, the calculation of cross-entropy usually used between two tensors like:

Target as [0,0,0,1], where 1 is the right class

Output tensor as [0.1,0.2,0.3,0.4], where the sum as 1.

So based on this assumption, nn.CrossEntropyLoss() here needs to achieve:

Firstly normalize the output tensor into possibility one.

Encode the label into one-hot ones, like 2 in 5 class as [0,1,0,0,0]. The length must be the same as output tensor.

Then calculate the loss.

May I ask is this what nn.CrossEntropyLoss() does? Or do we need to one-hot encoding the true label before we input into the model?

Thank you a lot for your time in advance!

答:
nn.CrossEntropyLoss first applies log-softmax (log(Softmax(x)) to get log probabilities and then calculates the negative-log likelihood as mentioned in the documentation:

This criterion combines nn.LogSoftmax() and nn.NLLLoss() in one single class.

When using one-hot encoded targets, the cross-entropy can be calculated as follows:

where y is the one-hot encoded target vector and ŷ is the vector of probabilities for each class. To get the probabilities you would apply softmax to the output of the model. The logarithm of the probabilities is used, and PyTorch just combines the logarithm and the softmax into one operation nn.LogSoftmax(), for numerical stability.

Since all of the values except one in the one-hot vector are zero, only a single term of the sum will be non-zero. Therefore given the actual class, it can be simplified to:

As long as you know the class index, the loss can be calculated directly, making it more efficient than using a one-hot encoded target, hence nn.CrossEntropyLoss expects the class indices.

The full calculation is given in the documentation of nn.CrossEntropyLoss:

The loss can be described as:

Body:
I'm reading the Pytorch tutorial of a multi-class classification problem. And I find the behavior of Loss calculation in Pytorch confuses me a lot. Can you help me with this?

The model used for classification goes like this:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

The training process goes as follows:

optimizer.zero_grad()
outputs = net(inputs)
loss = nn.CrossEntropyLoss(outputs, labels)
loss.backward()
optimizer.step()

My question is: What's the exact behavior of Loss calculation in Pytorch here? During each iteration, the input of nn.CrossEntropyLoss() has two parts:

The output of the model, which is a 10 by 1 tensor, with different values in it. This is a tensor without normalized into probability.

The label as a scalar, like 1 or 2 or 3.

As far as I know, the calculation of cross-entropy usually used between two tensors like:

Target as [0,0,0,1], where 1 is the right class

Output tensor as [0.1,0.2,0.3,0.4], where the sum as 1.

So based on this assumption, nn.CrossEntropyLoss() here needs to achieve:

Firstly normalize the output tensor into possibility one.

Encode the label into one-hot ones, like 2 in 5 class as [0,1,0,0,0]. The length must be the same as output tensor.

Then calculate the loss.

May I ask is this what nn.CrossEntropyLoss() does? Or do we need to one-hot encoding the true label before we input into the model?

Thank you a lot for your time in advance!


问:
Cannot return array and use in another method
I am trying to call my array in my printmessage method. I am getting an error as if I cannot access it outside my method getSum(). Any help? Thanks</p>

```<code>    public int getSum(int input) {
        int empty[] = new int[input] ;

        for (int i =0;i < input;i++) {
            int num = scan.nextInt();
            empty[i] = num;
            sum = num +sum;
        }
        return sum && empty[];
    }

    public void Printmessage(){
        for(int j = 0; j< empty.length; j++) {
            System.out.print(empty[j]+ " ");
        }
    }
}
</code>```

答:
You cant return two values from a function ! Change it for this :</p>

```<code>public int getSum(int input) {
    int empty[] = new int[input] ;

    for (int i =0;i < input;i++) {
        int num = scan.nextInt();
        empty[i] = num;
        sum += num;
    }
    return sum; // ONLY RETURN ONE VALUE !
}

public void Printmessage(){
    for(int j = 0; j< empty.length; j++) {
        System.out.print(empty[j]+ " ");
    }
}
}
</code>```

Body:
I am trying to call my array in my printmessage method. I am getting an error as if I cannot access it outside my method getSum(). Any help? Thanks

    public int getSum(int input) {
        int empty[] = new int[input] ;

        for (int i =0;i < input;i++) {
            int num = scan.nextInt();
            empty[i] = num;
            sum = num +sum;
        }
        return sum && empty[];
    }

    public void Printmessage(){
        for(int j = 0; j< empty.length; j++) {
            System.out.print(empty[j]+ " ");
        }
    }
}

Body:
You cant return two values from a function ! Change it for this :

public int getSum(int input) {
    int empty[] = new int[input] ;

    for (int i =0;i < input;i++) {
        int num = scan.nextInt();
        empty[i] = num;
        sum += num;
    }
    return sum; // ONLY RETURN ONE VALUE !
}

public void Printmessage(){
    for(int j = 0; j< empty.length; j++) {
        System.out.print(empty[j]+ " ");
    }
}
}


问:
JNI: From C code to Java and JNI

Background

I am developing an application for android in eclipse and now I have a problem and I need your help. So I must Call function written in C from JAVA application. But on my way of writing code I have some Questions which you can see below. I am waiting for your answers and ideas ...

C Code:

```<code>typdef struct blobData_s {
    unsigned long length;
    unsigned char data[1];
} blobData_t;

unsigned int CheckEnrollmentExist ( unsigned long hdevice, blobData_t* pInputInfo ) {
    // Function code goes here
    ..........................
    return some_value;
}
</code>```

JAVA Code:

In JAVA code instead of unsigned long I use int so I can write.

```<code>class jblobData_c {
    public int langth;
    *Question 1.*
}

public class ApplicationMainClass extends Activity {
    // Some code goes here
    ......................

    public native int JCheckEnrollmentExist( int jhdevive, *Question 2.* );

}
</code>```

Question 1.

What I can use instead of unsigned char in JAVA Code ?

What I must write in JAVA code instead of unsigned char data[1]; ?

Question 2.

How I can use class jblobData_c instead of blobData_t* pInputInfo in the JAVA Code ?

What I must write in JAVA instead of blobData_t* pInputInfo ?

JNI Code:

```<code>JNIEXPORT jint JNICALL Java_com_Test_JCheckEnrollmentExist(JNIEnv* env, jobject obj, jint jhdevice, *Question 2.* ) {

    // Call the base function from C code.
    return CheckEnrollmentExist( jhdevice, *Question 3.*);
}
</code>```

Question 3.

What I must write in CheckEnrollmentExist function that is C Code Function instead of blobData_t* pInputInfo in order this function works right and given parameter be the same

Reference

How to pass C structs back and forth to Java code in JNI ?

Passing large C structure through JNI efficiently

Return a Structure Object from C to Java Through JNI

Pass data between Java and C

Passing a pointer from JNI to Java using a long

Passing pointers between C and Java through JNI

答:
For question #1:

You can use a jchar.  Primitive chars in java are not signed, it's about the only primitive that isn't.  Note that jchar is a UTF-16 char, so you will have to "map" the jchar to a regular char, as you would have to with any character conversion issue.  For simple conversions, this can typically be done by casting

```<code>char c_char = (char)java_char;
</code>```

because the core ASCII shares the same numeric values between ASCII and UTF-16.  However, this is prone to error should anyone actually attempt to pass a "special" character through the interface.  A much better way would be to (in the java side, as it is easier) convert the characters to bytes using the appropriate character set for your platform (to ensure platform compatibility in the C layers).  Then you only need to pass a byte[] to the JNI call, and the bytes will correctly correspond to the characters that C likely will expect.

For question #2:

If your CheckEnrollmentExists(...) method is the JNI binding entry point, you cannot change data types safely.  That means that all entry inputs must be JNI data type values.  While you might be able to select the C data type equivalents (and you might be able to get your compiler to do it anyway) such techniques should be frowned upon.  This implicitly means that JNI entry points cannot accept struct data structure not defined in the JNI headers.  In other words, you can't pass your own struct to the method.

If the method needs access to a C struct across calls, use another means.  I've seen people store the pointer to the allocated data structure in a member integer or long (doing correct casting).  You can then rewrite the native code side to retrieve the pointer from the "this" object being passed into the call, and the do a dereference to obtain the required data.

For Question #3:

This is actually the same as question #2.  In the "binding wrapper" you put, you would retrieve the pointer's stored value in the java object's int or long field, cast it to the appropriate struct pointer and then pass it to the internal method.  As the passing of the pointer is a C to C call, no extra magic is required.

ContentLicense: CC BY-SA 3.0

Title: JNI: From C code to Java and JNI

Body:
Background

I am developing an application for android in eclipse and now I have a problem and I need your help. So I must Call function written in C from JAVA application. But on my way of writing code I have some Questions which you can see below. I am waiting for your answers and ideas ...

C Code:

typdef struct blobData_s {
    unsigned long length;
    unsigned char data[1];
} blobData_t;

unsigned int CheckEnrollmentExist ( unsigned long hdevice, blobData_t* pInputInfo ) {
    // Function code goes here
    ..........................
    return some_value;
}

JAVA Code:

In JAVA code instead of unsigned long I use int so I can write.

class jblobData_c {
    public int langth;
    *Question 1.*
}

public class ApplicationMainClass extends Activity {
    // Some code goes here
    ......................

    public native int JCheckEnrollmentExist( int jhdevive, *Question 2.* );

}

Question 1.

What I can use instead of unsigned char in JAVA Code ?

What I must write in JAVA code instead of unsigned char data[1]; ?

Question 2.

How I can use class jblobData_c instead of blobData_t* pInputInfo in the JAVA Code ?

What I must write in JAVA instead of blobData_t* pInputInfo ?

JNI Code:

JNIEXPORT jint JNICALL Java_com_Test_JCheckEnrollmentExist(JNIEnv* env, jobject obj, jint jhdevice, *Question 2.* ) {

    // Call the base function from C code.
    return CheckEnrollmentExist( jhdevice, *Question 3.*);
}

Question 3.

What I must write in CheckEnrollmentExist function that is C Code Function instead of blobData_t* pInputInfo in order this function works right and given parameter be the same

Reference

How to pass C structs back and forth to Java code in JNI ?

Passing large C structure through JNI efficiently

Return a Structure Object from C to Java Through JNI

Pass data between Java and C

Passing a pointer from JNI to Java using a long

Passing pointers between C and Java through JNI

Body:
For question #1:

You can use a jchar.  Primitive chars in java are not signed, it's about the only primitive that isn't.  Note that jchar is a UTF-16 char, so you will have to "map" the jchar to a regular char, as you would have to with any character conversion issue.  For simple conversions, this can typically be done by casting

char c_char = (char)java_char;

because the core ASCII shares the same numeric values between ASCII and UTF-16.  However, this is prone to error should anyone actually attempt to pass a "special" character through the interface.  A much better way would be to (in the java side, as it is easier) convert the characters to bytes using the appropriate character set for your platform (to ensure platform compatibility in the C layers).  Then you only need to pass a byte[] to the JNI call, and the bytes will correctly correspond to the characters that C likely will expect.

For question #2:

If your CheckEnrollmentExists(...) method is the JNI binding entry point, you cannot change data types safely.  That means that all entry inputs must be JNI data type values.  While you might be able to select the C data type equivalents (and you might be able to get your compiler to do it anyway) such techniques should be frowned upon.  This implicitly means that JNI entry points cannot accept struct data structure not defined in the JNI headers.  In other words, you can't pass your own struct to the method.

If the method needs access to a C struct across calls, use another means.  I've seen people store the pointer to the allocated data structure in a member integer or long (doing correct casting).  You can then rewrite the native code side to retrieve the pointer from the "this" object being passed into the call, and the do a dereference to obtain the required data.

For Question #3:

This is actually the same as question #2.  In the "binding wrapper" you put, you would retrieve the pointer's stored value in the java object's int or long field, cast it to the appropriate struct pointer and then pass it to the internal method.  As the passing of the pointer is a C to C call, no extra magic is required.


问:
Google Cloud Talent API - Java: Library Missing or example outdated?
After importing 'com.google.cloud:google-cloud-talent:0.36.1' (from here: https://cloud.google.com/talent-solution/job-search/docs/libraries#client-libraries-install-java) into my build.gradle file and running, the class 'TenantOrProjectName' from the documentation cannot resolve to a type in Eclipse.

When looking through the api documentation, I didn't see a class with that name in it either. I'm wondering if either the example is outdated or if I'm missing a library that isn't in the Java documentation.

Steps to duplicate(in Java)
1.)Copy 'com.google.cloud:google-cloud-talent:0.36.1' into build.gradle file, then run:
gradle cleanEclipse
gradle cleanEclipsewtp
gradle eclipse
gradle eclipsewtp

on command line.

2.) Copy and paste searchJobs() method from here:

(https://github.com/GoogleCloudPlatform/java-docs-samples/blob/talent-v4beta1-samples/jobs/v4/src/main/java/com/google/cloud/examples/talent/v4beta1/JobSearchHistogramSearch.java)

into Java program and 'TenantOrProjectName' can't be imported as it isn't recognized by Spring.

I'm just wondering if I'm missing a library because that's kind of what it feels like at the moment.

答: It seems that the documentation is out to date. I recommend you to take a look at this GitHub issue for further references. In here you can find the breaking changes for the v0.36.0 and it is recomended to use TenantName for resources in place of TenantOrProjectName.</p>


问:
How to create a dictionary from a given list?
I am trying to create a dictionary from my given list, knowing that the key will be the first letter of that word, and those have the same first letter would be added up to 1 key correspondingly. Can you guys help me out, please?

words= ['apple', 'bible','bird' ,'candy', 'day', 'elephant','friend']
```

答:
Just another option:

from collections import defaultdict

words = ['apple', 'bible','bird' ,'candy', 'day', 'elephant','friend']

word_dict = defaultdict(lambda: [])

list(map(lambda word: word_dict[word[0]].append(word), words))

print(dict(word_dict))
```

<p>Results in:</p>

```{'a': ['apple'], 'b': ['bible', 'bird'], 'c': ['candy'], 'd': ['day'], 'e': ['elephant'], 'f': ['friend']}
```

Body:
Just another option:

from collections import defaultdict

words = ['apple', 'bible','bird' ,'candy', 'day', 'elephant','friend']

word_dict = defaultdict(lambda: [])

list(map(lambda word: word_dict[word[0]].append(word), words))

print(dict(word_dict))

Results in:

{'a': ['apple'], 'b': ['bible', 'bird'], 'c': ['candy'], 'd': ['day'], 'e': ['elephant'], 'f': ['friend']}


问:
In Eclipse IDE, how can I limit Backward/Forward History to the current file?
Currently it goes between files.</p>

答: No.                You can not.</p>

Body: Currently it goes between files.

Body: No.                You can not.


问:
How can I perform COALESCE in power query?
I have a table that has values for quarters and I need to add a new column that gives me the last not null value as last quarter. For example</p>

```<code>ID | Project  | Q1   | Q2   | Q3   | Q4   | Current Quarter Value
1  | bal bal  | 23   | 32   | 34   | null | 34
2  | cuz cuz  | 43   | 56   | null | null | 56
</code>```

答:
I think is a little bit late, but I'm also looking for a better solution than:
List.First( List.RemoveNulls( { list } ) )

or for a default value:

List.First( List.RemoveNulls( List.Combine { { list }, { defaultValue } } ) ) )

Body:
I have a table that has values for quarters and I need to add a new column that gives me the last not null value as last quarter. For example

ID | Project  | Q1   | Q2   | Q3   | Q4   | Current Quarter Value
1  | bal bal  | 23   | 32   | 34   | null | 34
2  | cuz cuz  | 43   | 56   | null | null | 56


问:
SystemVerilog: always_comb construct does not infer purely combinational logic
I have this FSM that controls different events in a game I'm building (in Quartus Prime Lite Edition 17.0).
When I try to compile it, I get this error:

```<code>Error (10166): SystemVerilog RTL Coding error at GameFSM.sv(40): always_comb construct does not infer purely combinational logic.
</code>```

I have all the outputs assigned as default values, so no latches should be inferred I believe (I don't totally understand how it works..).

```<code>    //Game FSM, controls the game events

module GameFSM (
    input logic clk, resetN, NPCCollisions, Dead_NPCs, start_game, pause_game, continue_game, delay_indicator,

    output logic Message_main_menu,
    output logic Message_pause,
    output logic Message_win,
    output logic Message_lose,
    output logic Display_Main_Menu,
    output logic MainMenu_Music,
    output logic BackGround_Music,
    output logic Paused,
    output logic Win_Music,
    output logic Lose_Music
   );

enum logic [2:0] {main_Menu, three_hearts, two_hearts, one_heart, delay_state, pause, win, lose} prState, nxtState;

logic [1:0] return_state;

always @(posedge clk or negedge resetN)
  begin

    if ( !resetN ) begin // Asynchronic reset
        prState <= main_Menu;
    end

    else    begin   // Synchronic logic FSM
        prState <= nxtState;
    end

end // always

always_comb // Update next state and outputs
    begin
        nxtState = prState; // default values
        Message_main_menu = 1'b0;
        Message_pause = 1'b0;
        Message_win = 1'b0;
        Message_lose = 1'b0;
        MainMenu_Music = 1'b0;
        BackGround_Music = 1'b0;
        Paused = 1'b0;
        Win_Music = 1'b0;
        Lose_Music = 1'b0;
        Display_Main_Menu = 1'b0;

        case (prState)

            main_Menu: begin
                Message_main_menu = 1'b1;
                MainMenu_Music = 1'b1;
                Display_Main_Menu = 1'b1;
                if(start_game)
                    nxtState = three_hearts;
            end

            three_hearts: begin
                BackGround_Music = 1'b1;
                return_state = 2'h3;
                if(NPCCollisions) //Corony hits an npc while he has 3 hearts left
                    nxtState = delay_state;
                else if(Dead_NPCs) //all npc's are dead
                    nxtState = win;
                else if(pause_game)
                    nxtState = pause;
            end

            two_hearts: begin
                BackGround_Music = 1'b1;
                return_state = 2'h2;
                if(NPCCollisions) //Corony hits an npc while he has 2 hearts left
                    nxtState = delay_state;
                else if(Dead_NPCs) //all npc's are dead
                    nxtState = win;
                else if(pause_game)
                    nxtState = pause;
            end

            one_heart: begin
                BackGround_Music = 1'b1;
                return_state = 2'h1;
                if(NPCCollisions) //Corony lost all his hearts
                    nxtState = lose;
                else if(Dead_NPCs) //all npc's are dead
                    nxtState = win;
                else if(pause_game)
                    nxtState = pause;
            end

            delay_state: begin
                if(delay_indicator) begin
                    if(return_state == 2'h3)
                        nxtState = two_hearts;
                    else if(return_state == 2'h2)
                        nxtState = one_heart;
                end
            end

            pause: begin
                Paused = 1'b1;
                Message_pause = 1'b1;
                if(continue_game) begin
                    if(return_state == 2'h3)
                        nxtState = three_hearts;
                    else if(return_state == 2'h2)
                        nxtState = two_hearts;
                    else
                        nxtState = one_heart;
                end
            end

            win: begin
                Win_Music = 1'b1;
                Message_win = 1'b1;
            end

            lose: begin
                Lose_Music = 1'b1;
                Message_lose = 1'b1;
                end

            endcase
    end // always comb

endmodule
</code>```

return_state is used so I can know which state delivered me to the pause state, so when I want to leave the pause state, I'd know where I should return to.

答:
The problem is that you read and assign to the counter signal in side the always_comb block:

```<code>                counter = counter - 1;
</code>```

This can create a combinational feedback loop.

Similarly for:

```<code>                counter = DELAY_TIME;
</code>```

Perhaps you should use sequential logic for counter: always @(posedge clk ...)

Another problem signal might be return_state.  I'm not sure why my simulator doesn't complain about this one.  I think return_state should be assigned in its own sequential logic block as well.

Body:
I have this FSM that controls different events in a game I'm building (in Quartus Prime Lite Edition 17.0).
When I try to compile it, I get this error:

Error (10166): SystemVerilog RTL Coding error at GameFSM.sv(40): always_comb construct does not infer purely combinational logic.

I have all the outputs assigned as default values, so no latches should be inferred I believe (I don't totally understand how it works..).

    //Game FSM, controls the game events

module GameFSM (
    input logic clk, resetN, NPCCollisions, Dead_NPCs, start_game, pause_game, continue_game, delay_indicator,

    output logic Message_main_menu,
    output logic Message_pause,
    output logic Message_win,
    output logic Message_lose,
    output logic Display_Main_Menu,
    output logic MainMenu_Music,
    output logic BackGround_Music,
    output logic Paused,
    output logic Win_Music,
    output logic Lose_Music
   );

enum logic [2:0] {main_Menu, three_hearts, two_hearts, one_heart, delay_state, pause, win, lose} prState, nxtState;

logic [1:0] return_state;

always @(posedge clk or negedge resetN)
  begin

    if ( !resetN ) begin // Asynchronic reset
        prState <= main_Menu;
    end

    else    begin   // Synchronic logic FSM
        prState <= nxtState;
    end

end // always

always_comb // Update next state and outputs
    begin
        nxtState = prState; // default values
        Message_main_menu = 1'b0;
        Message_pause = 1'b0;
        Message_win = 1'b0;
        Message_lose = 1'b0;
        MainMenu_Music = 1'b0;
        BackGround_Music = 1'b0;
        Paused = 1'b0;
        Win_Music = 1'b0;
        Lose_Music = 1'b0;
        Display_Main_Menu = 1'b0;

        case (prState)

            main_Menu: begin
                Message_main_menu = 1'b1;
                MainMenu_Music = 1'b1;
                Display_Main_Menu = 1'b1;
                if(start_game)
                    nxtState = three_hearts;
            end

            three_hearts: begin
                BackGround_Music = 1'b1;
                return_state = 2'h3;
                if(NPCCollisions) //Corony hits an npc while he has 3 hearts left
                    nxtState = delay_state;
                else if(Dead_NPCs) //all npc's are dead
                    nxtState = win;
                else if(pause_game)
                    nxtState = pause;
            end

            two_hearts: begin
                BackGround_Music = 1'b1;
                return_state = 2'h2;
                if(NPCCollisions) //Corony hits an npc while he has 2 hearts left
                    nxtState = delay_state;
                else if(Dead_NPCs) //all npc's are dead
                    nxtState = win;
                else if(pause_game)
                    nxtState = pause;
            end

            one_heart: begin
                BackGround_Music = 1'b1;
                return_state = 2'h1;
                if(NPCCollisions) //Corony lost all his hearts
                    nxtState = lose;
                else if(Dead_NPCs) //all npc's are dead
                    nxtState = win;
                else if(pause_game)
                    nxtState = pause;
            end

            delay_state: begin
                if(delay_indicator) begin
                    if(return_state == 2'h3)
                        nxtState = two_hearts;
                    else if(return_state == 2'h2)
                        nxtState = one_heart;
                end
            end

            pause: begin
                Paused = 1'b1;
                Message_pause = 1'b1;
                if(continue_game) begin
                    if(return_state == 2'h3)
                        nxtState = three_hearts;
                    else if(return_state == 2'h2)
                        nxtState = two_hearts;
                    else
                        nxtState = one_heart;
                end
            end

            win: begin
                Win_Music = 1'b1;
                Message_win = 1'b1;
            end

            lose: begin
                Lose_Music = 1'b1;
                Message_lose = 1'b1;
                end

            endcase
    end // always comb

endmodule

return_state is used so I can know which state delivered me to the pause state, so when I want to leave the pause state, I'd know where I should return to.

Body:
The problem is that you read and assign to the counter signal in side the always_comb block:

                counter = counter - 1;

This can create a combinational feedback loop.

Similarly for:

                counter = DELAY_TIME;

Perhaps you should use sequential logic for counter: always @(posedge clk ...)

Another problem signal might be return_state.  I'm not sure why my simulator doesn't complain about this one.  I think return_state should be assigned in its own sequential logic block as well.


问:
Convert 'String' to 'SQlCommand' in vb.net
I am a novice vb programmer. I am sure it's simple but I can't get it right.

Value of type 'String' cannot be converted to 'SQLCommand'

It is the error.

```<code>Public Sub InsertPackageDetails(ByVal PackageName As String, ByVal PackageDescription As String, ByVal DateOfInstallation As DateTime, ByVal ImportPackageStatus As String)
    Dim SQL As String = "Insert into PackageInstallation(PackageName,PackageDescription,DateOfInstallation,ImportPackageStatus) values('" + PackageName + "','" + PackageDescription + "','" + DateOfInstallation + "'," + ImportPackageStatus + ") "
    DBHelper2.ExecuteNonQuery(SQL)
End Sub
</code>```

How can I convert SQL which is the string to SQLCommand?
I appreciate the help.

答:
You can see the various ways to create a command here. https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.-ctor?view=dotnet-plat-ext-3.1

You should use Using...End Using blocks so that your connection and command will be closed and disposed but you will need to have these objects in the local method where they are used.

I had to guess at the type and size of your fields in the database. Check in the database and correct the code accordingly.

```<code>Public Sub InsertPackageDetails(ByVal PackageName As String, ByVal PackageDescription As String, ByVal DateOfInstallation As DateTime, ByVal ImportPackageStatus As String)
    Dim SQL As String = "Insert into PackageInstallation(PackageName,PackageDescription,DateOfInstallation,ImportPackageStatus)
                          Values(@PackageName, @PackageDescription, @DateOfInstallation, @ImportPackageStatus;"
    Using cn As New SqlConnection("Your connection string"),
            cmd As New SqlCommand(SQL, cn)
        With cmd.Parameters
            .Add("@PackageName", SqlDbType.VarChar, 100).Value = PackageName
            .Add("@PackageDescription", SqlDbType.VarChar, 100).Value = PackageDescription
            .Add("@DateOfInstallation", SqlDbType.DateTime).Value = DateOfInstallation
            .Add("@ImportPackageStatus", SqlDbType.VarChar, 100).Value = ImportPackageStatus
        End With
        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub
</code>```

Body:
I am a novice vb programmer. I am sure it's simple but I can't get it right.

Value of type 'String' cannot be converted to 'SQLCommand'

It is the error.

Public Sub InsertPackageDetails(ByVal PackageName As String, ByVal PackageDescription As String, ByVal DateOfInstallation As DateTime, ByVal ImportPackageStatus As String)
    Dim SQL As String = "Insert into PackageInstallation(PackageName,PackageDescription,DateOfInstallation,ImportPackageStatus) values('" + PackageName + "','" + PackageDescription + "','" + DateOfInstallation + "'," + ImportPackageStatus + ") "
    DBHelper2.ExecuteNonQuery(SQL)
End Sub

How can I convert SQL which is the string to SQLCommand?
I appreciate the help.

Body:
You can see the various ways to create a command here. https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.-ctor?view=dotnet-plat-ext-3.1

You should use Using...End Using blocks so that your connection and command will be closed and disposed but you will need to have these objects in the local method where they are used.

I had to guess at the type and size of your fields in the database. Check in the database and correct the code accordingly.

Public Sub InsertPackageDetails(ByVal PackageName As String, ByVal PackageDescription As String, ByVal DateOfInstallation As DateTime, ByVal ImportPackageStatus As String)
    Dim SQL As String = "Insert into PackageInstallation(PackageName,PackageDescription,DateOfInstallation,ImportPackageStatus)
                          Values(@PackageName, @PackageDescription, @DateOfInstallation, @ImportPackageStatus;"
    Using cn As New SqlConnection("Your connection string"),
            cmd As New SqlCommand(SQL, cn)
        With cmd.Parameters
            .Add("@PackageName", SqlDbType.VarChar, 100).Value = PackageName
            .Add("@PackageDescription", SqlDbType.VarChar, 100).Value = PackageDescription
            .Add("@DateOfInstallation", SqlDbType.DateTime).Value = DateOfInstallation
            .Add("@ImportPackageStatus", SqlDbType.VarChar, 100).Value = ImportPackageStatus
        End With
        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub


问:
How to access functions from different "Action" in QTP?
I have some code in Action1 item. I have added Action2 where I want to store some common function but if I call it from Action1, it does not work.
I just want to create some "global actions" that will be reusable.
Thanks</p>

答:
For modular reuse with QTP, your options are:

Use function libraries (as @Vikas noted)

Use reusable Actions

If you are going to use actions, then each action should itself be modular. Whatever piece of code you want to be reused should be a whole Action unto itself, not part of another action.


问:
Check if a date range is within a date range
I have the following class:

```<code>public class Membership
{
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; } // If null then it lasts forever
}
</code>```

I need to make sure when adding to the following list that the new item doesn't overlap the dates from existing item:

```<code>var membership = new List<Membership>
{
    new Membership { StartDate = DateTime.UtcNow.AddDays(-10), EndDate = DateTime.UtcNow.AddDays(-5) },
    new Membership { StartDate = DateTime.UtcNow.AddDays(-5), EndDate = null }
};
</code>```

For example doing:

```<code>var newItem = new Membership { StartDate = DateTime.UtcNow.AddDays(-15), EndDate = DateTime.UtcNow.AddDays(-10) }; // Allowed

var newItem2 = new Membership { StartDate = DateTime.UtcNow.AddDays(-15), EndDate = null }; // Not Allowed

if (AllowededToAdd(newItem))
    membership.Add(newItem);

if (AllowededToAdd(newItem2))
    membership.Add(newItem2);
</code>```

I thought this would be simple but so far my attempts have all been wrong and i'm starting to confuse myself and was hoping someone had done something similar they could share.  Thanks

答:
I came up with the following method to check if dates overlap, it may not be the most effecient way but i hope this helps..</p>

```<code>public static bool DateRangesOverlap(DateTime startDateA, DateTime endDateA, DateTime startDateB, DateTime endDateB)
{
    var allDatesA = new List<DateTime>();
    var allDatesB = new List<DateTime>();

    for (DateTime date = startDateA; date <= endDateA; date = date.AddDays(1))
    {
        allDatesA.Add(date);
    }

    for (DateTime date = startDateB; date <= endDateB; date = date.AddDays(1))
    {
        allDatesB.Add(date);
    }

    var isInRange = false;
    foreach (var date in allDatesA)
    {
        var existsInAllDatesB = allDatesB.Any(x => x == date);
        if (existsInAllDatesB)
        {
            isInRange = true;
            break;
        }
    }

    return isInRange;
}
</code>```

Body:
I have the following class:

public class Membership
{
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; } // If null then it lasts forever
}

I need to make sure when adding to the following list that the new item doesn't overlap the dates from existing item:

var membership = new List<Membership>
{
    new Membership { StartDate = DateTime.UtcNow.AddDays(-10), EndDate = DateTime.UtcNow.AddDays(-5) },
    new Membership { StartDate = DateTime.UtcNow.AddDays(-5), EndDate = null }
};

For example doing:

var newItem = new Membership { StartDate = DateTime.UtcNow.AddDays(-15), EndDate = DateTime.UtcNow.AddDays(-10) }; // Allowed

var newItem2 = new Membership { StartDate = DateTime.UtcNow.AddDays(-15), EndDate = null }; // Not Allowed

if (AllowededToAdd(newItem))
    membership.Add(newItem);

if (AllowededToAdd(newItem2))
    membership.Add(newItem2);

I thought this would be simple but so far my attempts have all been wrong and i'm starting to confuse myself and was hoping someone had done something similar they could share.  Thanks

Body:
I came up with the following method to check if dates overlap, it may not be the most effecient way but i hope this helps..

public static bool DateRangesOverlap(DateTime startDateA, DateTime endDateA, DateTime startDateB, DateTime endDateB)
{
    var allDatesA = new List<DateTime>();
    var allDatesB = new List<DateTime>();

    for (DateTime date = startDateA; date <= endDateA; date = date.AddDays(1))
    {
        allDatesA.Add(date);
    }

    for (DateTime date = startDateB; date <= endDateB; date = date.AddDays(1))
    {
        allDatesB.Add(date);
    }

    var isInRange = false;
    foreach (var date in allDatesA)
    {
        var existsInAllDatesB = allDatesB.Any(x => x == date);
        if (existsInAllDatesB)
        {
            isInRange = true;
            break;
        }
    }

    return isInRange;
}


问:
Codeigniter 3 routing with and without a parameter
How do you set up routes in Codeigniter 3 in the following case:

If I want to all products, I use this URL:

```<code>https://www.domain.local/product
</code>```

If I want to show a specific product, I use this URL:

```<code>https://www.domain.local/product/123
</code>```

So far, the only way I can get this to work is by using two separate routes:

```<code>$route['product']        = "Catalog/product";
$route['product/(:any)'] = "Catalog/product/$1";
</code>```

How can I combine these routes into one? I can find no documentation that refers to this.

答:
your approach is the correct approach:

Routes will run in the order they are defined. Higher routes will
  always take precedence over lower ones.

and

Route rules are not filters! Setting a rule of e.g. ‘foo/bar/(:num)’
  will not prevent controller Foo and method bar to be called with a
  non-numeric value if that is a valid route.

In your controller function you'll check if the parameter exists.

```<code>function product($id=0){
   if ($id){
      // your code for product where id corresponds to $1 (from link)
   }else{
     // your code if no product id in link
   }
}
</code>```

see Codeigniter URI Routing - Wildcards

Body:
How do you set up routes in Codeigniter 3 in the following case:

If I want to all products, I use this URL:

https://www.domain.local/product

If I want to show a specific product, I use this URL:

https://www.domain.local/product/123

So far, the only way I can get this to work is by using two separate routes:

$route['product']        = "Catalog/product";
$route['product/(:any)'] = "Catalog/product/$1";

How can I combine these routes into one? I can find no documentation that refers to this.

Body:
your approach is the correct approach:

Routes will run in the order they are defined. Higher routes will
  always take precedence over lower ones.

and

Route rules are not filters! Setting a rule of e.g. ‘foo/bar/(:num)’
  will not prevent controller Foo and method bar to be called with a
  non-numeric value if that is a valid route.

In your controller function you'll check if the parameter exists.

function product($id=0){
   if ($id){
      // your code for product where id corresponds to $1 (from link)
   }else{
     // your code if no product id in link
   }
}

see Codeigniter URI Routing - Wildcards


问:
Purrr with [[<-
Can someone explain to me why this gives an error using purrr:

```<code>map(list(list(a=1, b=2, c=3), list(a=1, b=2, c=3)), `[[<-`, "b", 1)
</code>```

but the same call with lapply seems to work.

I was under the impression that [[<- was just a function call, so was expecting both purrr and lapply to do the same thing here.

答:
The issue lies in purrr::as_mapper(), which map() calls under the hood. Consider the difference:

```<code>x <- list( a=1, b=2, c=3 )

`[[<-`( x, "b", 1 )                    # This is what lapply() calls
                                       # x is unchanged, returns modified list

purrr::as_mapper(`[[<-`)( x, "b", 1 )  # This is what map() calls
                                       # x is modified in-place, returns value 1
</code>```

The second function call behaves as if you typed the following assignment expression:

```<code>(x[["b"]] <- 1)
# [1] 1
</code>```

which must be causing issues when the function is passed down to map() internals. Interestingly, wrapping the function with a ~ lambda works, but returns the "wrong" result:

```<code>y <- list(list(a=1, b=2, c=3), list(a=1, b=2, c=3))

purrr::map( y, ~purrr::as_mapper(`[[<-`)(.x, "b", 1) )
# [[1]]
# [1] 1

# [[2]]
# [1] 1

purrr::map( y, purrr::as_mapper(`[[<-`), "b", 1 )
# Error in list(a = 1, b = 2, c = 3)[["b"]] <- 1 :
#   target of assignment expands to non-language object
</code>```

The proper purrr equivalents to your lapply() example would instead look something like this:

```<code>r1 <- lapply(y, `[[<-`, "b", 1)
r2 <- purrr::map(y, purrr::modify_at, "b", ~1)
r3 <- purrr::map(y, ~`[[<-`(.x, "b", 1))

identical( r1, r2 )               # TRUE
identical( r1, r3 )               # TRUE
</code>```

In the above, modify_at() returns the modified list, just like [[<-(...) does, and the tilde ~ is needed because modify_at() expects a function. The alternative is to wrap `[[<-` into a ~ lambda.

Title: Purrr with [[<-

Body:
Can someone explain to me why this gives an error using purrr:

map(list(list(a=1, b=2, c=3), list(a=1, b=2, c=3)), `[[<-`, "b", 1)

but the same call with lapply seems to work.

I was under the impression that [[<- was just a function call, so was expecting both purrr and lapply to do the same thing here.

Body:
The issue lies in purrr::as_mapper(), which map() calls under the hood. Consider the difference:

x <- list( a=1, b=2, c=3 )

`[[<-`( x, "b", 1 )                    # This is what lapply() calls
                                       # x is unchanged, returns modified list

purrr::as_mapper(`[[<-`)( x, "b", 1 )  # This is what map() calls
                                       # x is modified in-place, returns value 1

The second function call behaves as if you typed the following assignment expression:

(x[["b"]] <- 1)
# [1] 1

which must be causing issues when the function is passed down to map() internals. Interestingly, wrapping the function with a ~ lambda works, but returns the "wrong" result:

y <- list(list(a=1, b=2, c=3), list(a=1, b=2, c=3))

purrr::map( y, ~purrr::as_mapper(`[[<-`)(.x, "b", 1) )
# [[1]]
# [1] 1

# [[2]]
# [1] 1

purrr::map( y, purrr::as_mapper(`[[<-`), "b", 1 )
# Error in list(a = 1, b = 2, c = 3)[["b"]] <- 1 :
#   target of assignment expands to non-language object

The proper purrr equivalents to your lapply() example would instead look something like this:

r1 <- lapply(y, `[[<-`, "b", 1)
r2 <- purrr::map(y, purrr::modify_at, "b", ~1)
r3 <- purrr::map(y, ~`[[<-`(.x, "b", 1))

identical( r1, r2 )               # TRUE
identical( r1, r3 )               # TRUE

In the above, modify_at() returns the modified list, just like [[<-(...) does, and the tilde ~ is needed because modify_at() expects a function. The alternative is to wrap `[[<-` into a ~ lambda.


问:
Is there a way to tell if a Jupyter notebook is frozen without killing the kernel?
I am working with a very large set of data and certain functions take a long time to run on my machine.  Is there a way to see if the function is still running or if the kernel is frozen without killing the cell?

The example below is being used to compare company names in two different dictionaries of distinct names as keys with a list of words in the name as values.   I have 650000 entries in one dictionary and 100000 in the other.

Example:

```<code>    def insert(df, entries):
       try:
          df.loc[max(df.index) + 1] = entries
    except ValueError:
          df.loc[0] = entries

    def check_for_matching_phrases(dict1, dict2):
        df = pd.DataFrame(columns=['filings_name', 'no_match_name', 'percent_match'])
        for key1, value1 in dict1.items():
            for key2, value2 in dict2.items():
                matches = 0
                for val_1 in value1:
                    if val_1 in value2:
                       matches += 1
                denominator = len(min(value1, value2))
                perc_match = matches / denominator
                insert(df, [key1, key2, perc_match])
        return df
</code>```

答:
tqdm is godsend for things like this. If I'm working with a lot of data/processing (whether in a jupyter notebook or in a normal python script) I'll use it to estimate how long my process will take and monitor if it gets stuck at anypoint. You can just pop on the tqdm function onto your iterable and viola:

```<code>from tqdm import tqdm

...

for key1, value1 in tqdm(dict1.items()):
    for key2, value2 in dict2.items():
</code>```

It also works nicely for nested for loops as well:

```<code>for i in tqdm(range(10), position=0):
    for i in tqdm(range(10000000), position=1, leave=False):
        pass
</code>```

Body:
I am working with a very large set of data and certain functions take a long time to run on my machine.  Is there a way to see if the function is still running or if the kernel is frozen without killing the cell?

The example below is being used to compare company names in two different dictionaries of distinct names as keys with a list of words in the name as values.   I have 650000 entries in one dictionary and 100000 in the other.

Example:

    def insert(df, entries):
       try:
          df.loc[max(df.index) + 1] = entries
    except ValueError:
          df.loc[0] = entries

    def check_for_matching_phrases(dict1, dict2):
        df = pd.DataFrame(columns=['filings_name', 'no_match_name', 'percent_match'])
        for key1, value1 in dict1.items():
            for key2, value2 in dict2.items():
                matches = 0
                for val_1 in value1:
                    if val_1 in value2:
                       matches += 1
                denominator = len(min(value1, value2))
                perc_match = matches / denominator
                insert(df, [key1, key2, perc_match])
        return df

Body:
tqdm is godsend for things like this. If I'm working with a lot of data/processing (whether in a jupyter notebook or in a normal python script) I'll use it to estimate how long my process will take and monitor if it gets stuck at anypoint. You can just pop on the tqdm function onto your iterable and viola:

from tqdm import tqdm

...

for key1, value1 in tqdm(dict1.items()):
    for key2, value2 in dict2.items():

It also works nicely for nested for loops as well:

for i in tqdm(range(10), position=0):
    for i in tqdm(range(10000000), position=1, leave=False):
        pass


问:
Utilisation of 'this' in React Js
i have a classic functionnal component returning what will be rendered in JSX

```<code>function Search(){

    const loadFilms = function (){
         console.log(this)); // logs undefined
    };

    return(

            <View style= {styleAreas.view}>
                <TextInput style= {styleAreas.textinput} placeholder='Type your movie ' />
                <Button title='Search' onPress= {()=> { console.log(this);loadFilms();} } />  // logs undefined
                <FlatList
                data={films}
                keyExtractor = {item => item.id.toString()}
                renderItem={({item}) => <FilmItem filmDesc = {item} />}
                />
            </View>

        )
}
</code>```

so i'm learning JS / React, and what i don't understand why this is undefined but when we run it since Class Search extends React.Component {}. it returns the  right instance.
 especially that i tought that in JS ( i'm maybe over simplifying)  a class is  a function with pre-configured prototypes etc...

thank you for your explanations :)

答:
I believe you are mistaken because your function is not extending the Component class.

A react component class would begin with the declaration as so:

```<code>class App extends Component {
    render () {
        return (
            <Text>Hello World!</Text>
        )
    }
}
</code>```

Class components are functions, but your function is not extending the component class.

Title: Utilisation of 'this' in React Js

Body:
i have a classic functionnal component returning what will be rendered in JSX

function Search(){

    const loadFilms = function (){
         console.log(this)); // logs undefined
    };

    return(

            <View style= {styleAreas.view}>
                <TextInput style= {styleAreas.textinput} placeholder='Type your movie ' />
                <Button title='Search' onPress= {()=> { console.log(this);loadFilms();} } />  // logs undefined
                <FlatList
                data={films}
                keyExtractor = {item => item.id.toString()}
                renderItem={({item}) => <FilmItem filmDesc = {item} />}
                />
            </View>

        )
}

so i'm learning JS / React, and what i don't understand why this is undefined but when we run it since Class Search extends React.Component {}. it returns the  right instance.
 especially that i tought that in JS ( i'm maybe over simplifying)  a class is  a function with pre-configured prototypes etc...

thank you for your explanations :)

Body:
I believe you are mistaken because your function is not extending the Component class.

A react component class would begin with the declaration as so:

class App extends Component {
    render () {
        return (
            <Text>Hello World!</Text>
        )
    }
}

Class components are functions, but your function is not extending the component class.


问:
PHP/MySQL Many-to-many relationship - the next step here
I'm new to advanced MySQL having only come across many-to-many relationships yesterday. I'm working on a project where users can join multiple projects and projects can accommodate multiple users.

My tables are:

Users - ID, name, email, password etc
Projects - ID, name, URL etc

Following advice from this site, I have set up a linking table with two foreign keys matched to the IDs of the above tables:

Users_Projects - Users_ID, Projects_ID

I understand the next step is something to do with joining, but how do I add a user to a table, or see who the members of a project are/what projects a particular user is a member of?

答:
To assign user to project you need to type user id and project id into the User_Projects table, to see which users are in selected project you can type:

```<code>SELECT Users.name, Users.email etc.. from Users_Projects JOIN Users on Users.ID =
</code>```

Users_Projects.Users_ID JOIN Projects ON Projects.ID = Users_Projects.Projects_ID WHERE (Put your where statement here)

Read more on wiki: JOIN wikipedia

Body:
To assign user to project you need to type user id and project id into the User_Projects table, to see which users are in selected project you can type:

SELECT Users.name, Users.email etc.. from Users_Projects JOIN Users on Users.ID =

Users_Projects.Users_ID JOIN Projects ON Projects.ID = Users_Projects.Projects_ID WHERE (Put your where statement here)

Read more on wiki: JOIN wikipedia


问:
How to set a row color based on it's field value.from client side in asp.net
I am using grid view to display a table, and using onRowDataBound to execute a function which sets color based on the field value

function definition is like this

```<code>    {
        e.Row.Cells[3].Visible = false;
        if (e.Row.Cells[3].Text == "0")
            {
                e.Row.ForeColor = System.Drawing.Color.Red;

            }
    }
</code>```

I want to know how this could be done from client side, instead of server side.

答:
Found this basic exemple for client side

Try ajust it to work with your data in the views and add the you can put color via css i guess.

```<code><div>
    <table>
        <tr>
            <td style="width: 100px">StuId
            </td>
            <td style="width: 150px">StuName
            </td>
            <td style="width: 100px">Course
            </td>
        </tr>
    </table>
    <asp:GridView AutoGenerateColumns="false" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound" ID="GridView1" runat="server">
        <Columns>
            <asp:BoundField DataField="StuId" HeaderText="StuId" ItemStyle-Width="100px" />
            <asp:BoundField DataField="StuName" HeaderText="StuName" ItemStyle-Width="150px" />
            <asp:BoundField DataField="Course" HeaderText="Course" ItemStyle-Width="100px" />
        </Columns>
    </asp:GridView>
</div>
</code>```

Acticle : https://www.codeproject.com/Articles/249155/Rows-and-Columns-Merging-in-ASP-NET-GridView-Contr

Body:
I am using grid view to display a table, and using onRowDataBound to execute a function which sets color based on the field value

function definition is like this

    {
        e.Row.Cells[3].Visible = false;
        if (e.Row.Cells[3].Text == "0")
            {
                e.Row.ForeColor = System.Drawing.Color.Red;

            }
    }

I want to know how this could be done from client side, instead of server side.

Body:
Found this basic exemple for client side

Try ajust it to work with your data in the views and add the you can put color via css i guess.

<div>
    <table>
        <tr>
            <td style="width: 100px">StuId
            </td>
            <td style="width: 150px">StuName
            </td>
            <td style="width: 100px">Course
            </td>
        </tr>
    </table>
    <asp:GridView AutoGenerateColumns="false" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound" ID="GridView1" runat="server">
        <Columns>
            <asp:BoundField DataField="StuId" HeaderText="StuId" ItemStyle-Width="100px" />
            <asp:BoundField DataField="StuName" HeaderText="StuName" ItemStyle-Width="150px" />
            <asp:BoundField DataField="Course" HeaderText="Course" ItemStyle-Width="100px" />
        </Columns>
    </asp:GridView>
</div>

Acticle : https://www.codeproject.com/Articles/249155/Rows-and-Columns-Merging-in-ASP-NET-GridView-Contr


问:
How to get Active/Inactive Topics in JSON format within a configurable timeline
Can somebody explain or provide some useful link to get active/inactive topics in Kafka using Java?

答:
If I am right, you are looking for AdminClient provided by Apache Kafka. Here is a Link of its official documentation. This API provides various methods - displaying a list of topics, create/delete topics and other features like delete consumer groups, create partitions, etc.

I am writing a sample code that I have used in my project for topic creation using the AdminClient API.

```<code>public static NewTopic createTopics(String topicName) throws IOException {
    Properties properties = new Properties();
    properties.load(new FileReader(new File("C:\\Users\\Dell\\Downloads\\kafka_2.12-2.3.0\\kafkaproperty\\kafka.properties")));
    AdminClient adminClient = AdminClient.create(properties);
    NewTopic newTopic = new NewTopic(topicName, 1, (short) 1); //new NewTopic(topicName, numPartitions, replicationFactor)

    List<NewTopic> newTopics = new ArrayList<NewTopic>();
    newTopics.add(newTopic);
    adminClient.createTopics(newTopics); // Creation of new topic
    adminClient.listTopics(); // List of topics which you can convert into JSON

    adminClient.close();
    return newTopic;
}
</code>```

I hope you find this helpful. If you have any questions please feel free to ask.

Body:
If I am right, you are looking for AdminClient provided by Apache Kafka. Here is a Link of its official documentation. This API provides various methods - displaying a list of topics, create/delete topics and other features like delete consumer groups, create partitions, etc.

I am writing a sample code that I have used in my project for topic creation using the AdminClient API.

public static NewTopic createTopics(String topicName) throws IOException {
    Properties properties = new Properties();
    properties.load(new FileReader(new File("C:\\Users\\Dell\\Downloads\\kafka_2.12-2.3.0\\kafkaproperty\\kafka.properties")));
    AdminClient adminClient = AdminClient.create(properties);
    NewTopic newTopic = new NewTopic(topicName, 1, (short) 1); //new NewTopic(topicName, numPartitions, replicationFactor)

    List<NewTopic> newTopics = new ArrayList<NewTopic>();
    newTopics.add(newTopic);
    adminClient.createTopics(newTopics); // Creation of new topic
    adminClient.listTopics(); // List of topics which you can convert into JSON

    adminClient.close();
    return newTopic;
}

I hope you find this helpful. If you have any questions please feel free to ask.


问:
R Indicate If Group Has Value
```<code>data=data.frame(GROUP=c(1,1,1,2,2,2,3,3,3,4,4,4),
VAR=c('A','B','C','A','B','C','A','B','C','A','B','C'),
SCORE=c(3,6,NA,NA,NA,NA,1,NA,5,5,2,NA),
NEWVAR=c(1,1,1,NA,NA,NA,2,2,2,1,1,1))

score1 = c(2,3,7)
score2 = c(0,5,6)
</code>```

I have 'data' with all columns but 'NEWVAR' and I wish to create it like this:

If score1 %in% SCORE for a particular 'GROUP' then a value '1' is given to 'NEWVAR'

If score2 %in% SCORE & score1 is not in SCORE for a particular 'GROUP' then a value '2' is given to 'NEWVAR'

答:
Using dplyr:

input <- data %>% select(-NEWVAR)
input %>%
  group_by(GROUP) %>%
  mutate(NEWVAR=case_when(any(SCORE %in% score1) ~ 1,
                          any(SCORE %in% score2 & !(SCORE %in% score1)) ~ 2))
```

Title: R Indicate If Group Has Value

Body:
data=data.frame(GROUP=c(1,1,1,2,2,2,3,3,3,4,4,4),
VAR=c('A','B','C','A','B','C','A','B','C','A','B','C'),
SCORE=c(3,6,NA,NA,NA,NA,1,NA,5,5,2,NA),
NEWVAR=c(1,1,1,NA,NA,NA,2,2,2,1,1,1))

score1 = c(2,3,7)
score2 = c(0,5,6)

I have 'data' with all columns but 'NEWVAR' and I wish to create it like this:

If score1 %in% SCORE for a particular 'GROUP' then a value '1' is given to 'NEWVAR'

If score2 %in% SCORE & score1 is not in SCORE for a particular 'GROUP' then a value '2' is given to 'NEWVAR'