问:
How to access a running nodemon script after closing and reopening the terminal?
I'm running a node.js script using nodemon, but I'm wondering if it's possible to reaccess the nodemon process using the terminal after closing it.

Normally I would type "npm run start" that points to a "nodemon index.js" in the package.json, and some yellow and green logs would appear to tell the state of the process(starting, restarting). But if I close the terminal, how can I access this process again, without having to type another "npm run start"?

Just the case nodemon is not suitable for that, I would like to know which tools could help me achieve that, such as pm2, etc.

答:
Open a screen or tmux session, and then run your app.
This permits the terminal to be disconnected from the program and reconnected later by reviving the same session.</p>


来源: stackexchange

问:
Delete Rows Based on Visible Range In Another Worksheet
I have 2 worksheets, sheet1 and sheet2.  Since there are common rows in both sheets, I want to delete rows in sheet1 that can be found in sheet2.  I only want rows in sheet 1 to be deleted for a columns that have the criteria "Y".  I I filtered the column using criteria Y in sheet2 and selected the range as visible range.  So the deletion of rows in sheet1 should be based on the visible range in sheet2.  However, the deletion of rows in sheet1 is deleting every occurrence as per sheet2, not as per the visible range. I have the below code but it's not working.  Could someone please let me know what I'm doing wrong?  Thank you.</p>

```<code>lastrow = Sheets("Sheet1").Cells.Find("*", searchorder:=xlByRows, SearchDirection:=xlPrevious).Row

With Sheets("Sheet2")
    .AutoFilterMode = False
    .Range("B1").AutoFilter Field:=2, Criteria1:="Y"
End With

With Sheets("Sheet1")
    For i = lastrow To 1 Step -1
            If IsNumeric(Application.Match(.Range("A" & i).Value, Sheets("Sheet2").Range("A1:B" & lastrow).SpecialCells(xlCellTypeVisible), 0)) Then
            .Rows(i).Delete
            End If
    Next i
End With
</code>```

答:
If I understand the requirements correctly, this code will work.

```<code>With Sheets("Sheet2")

    Dim lastRow As Long
    lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row

    Dim data As Range
    Set data = .Range("B1:B" & lastRow)

    .AutoFilterMode = False
    data.AutoFilter 1, "Y"

    Dim deleteRows As Range
    Set deleteRows = data.SpecialCells(xlCellTypeVisible)

End With

Worksheets("Sheet1").Range(deleteRows.Address).EntireRow.Delete
</code>```

To remove the values in sheet1 that are in the sheet2 filtered data do this

```<code>Dim d as Range
For each d in deleteRows

    Dim found as Range
    Set found = Worksheets("Sheet1").Columns(1).Find(d.Value,lookat:=xlWhole)

    If not found is Nothing Then

        Dim removeFrom1 as Range
        If removeFrom1 is Nothing Then
            set removeFrom1 = found
        else
            set removeFrom1 = Union(removeFrom1,found)
        end If

    End If

Next

removeFrom1.entirerow.delete
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have 2 worksheets, sheet1 and sheet2.  Since there are common rows in both sheets, I want to delete rows in sheet1 that can be found in sheet2.  I only want rows in sheet 1 to be deleted for a columns that have the criteria "Y".  I I filtered the column using criteria Y in sheet2 and selected the range as visible range.  So the deletion of rows in sheet1 should be based on the visible range in sheet2.  However, the deletion of rows in sheet1 is deleting every occurrence as per sheet2, not as per the visible range. I have the below code but it's not working.  Could someone please let me know what I'm doing wrong?  Thank you.

lastrow = Sheets("Sheet1").Cells.Find("*", searchorder:=xlByRows, SearchDirection:=xlPrevious).Row

With Sheets("Sheet2")
    .AutoFilterMode = False
    .Range("B1").AutoFilter Field:=2, Criteria1:="Y"
End With

With Sheets("Sheet1")
    For i = lastrow To 1 Step -1
            If IsNumeric(Application.Match(.Range("A" & i).Value, Sheets("Sheet2").Range("A1:B" & lastrow).SpecialCells(xlCellTypeVisible), 0)) Then
            .Rows(i).Delete
            End If
    Next i
End With

Body:
If I understand the requirements correctly, this code will work.

With Sheets("Sheet2")

    Dim lastRow As Long
    lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row

    Dim data As Range
    Set data = .Range("B1:B" & lastRow)

    .AutoFilterMode = False
    data.AutoFilter 1, "Y"

    Dim deleteRows As Range
    Set deleteRows = data.SpecialCells(xlCellTypeVisible)

End With

Worksheets("Sheet1").Range(deleteRows.Address).EntireRow.Delete

To remove the values in sheet1 that are in the sheet2 filtered data do this

Dim d as Range
For each d in deleteRows

    Dim found as Range
    Set found = Worksheets("Sheet1").Columns(1).Find(d.Value,lookat:=xlWhole)

    If not found is Nothing Then

        Dim removeFrom1 as Range
        If removeFrom1 is Nothing Then
            set removeFrom1 = found
        else
            set removeFrom1 = Union(removeFrom1,found)
        end If

    End If

Next

removeFrom1.entirerow.delete


问:
Jest/Enzyme testing: expect.toContain(img)
I want to write a test using Jest/Enzyme that tests whether the correct image is returned depending on a condition. I currently have it passing with expect(wrapper.html()).toContain('');
 but I was wondering if there was any way I could just test have it return that it contains an image with className=CardImage?

For example, it would be something along the lines of expect(wrapper.html().img.className).toContain('CardImage'). I've been searching online but can't seem to find a solution.

答:
Yes you can use the method hasClass.

I suggest you to add an id, it will be easier to locate the image.

Here is an example:

```<code>expect(wrapper.find('my-image-id').hasClass('CardImage')).to.equal(true);
</code>```

Body:
Yes you can use the method hasClass.

I suggest you to add an id, it will be easier to locate the image.

Here is an example:

expect(wrapper.find('my-image-id').hasClass('CardImage')).to.equal(true);


问:
How do I change global variable in nested scope using python?
My example below. I have tried adding global to the variable that I want to change but doesn't help. The error I'm getting is in the comment.

```<code>def getNumOfSwapsToSort(array):
    sorted = array.copy()
    sorted.sort()

    swaps = 0

    def swap():
        currentSwaps = 0
        for i, el in enumerate(array):
            if ((len(array) - 1) == i) and currentSwaps != 0:
                swap()

            elif el != sorted[i]:
                idx = sorted.index(el)
                temp = array[idx]
                array[idx] = el
                array[i] = temp
                # UnboundLocalError: local variable 'swaps' referenced before assignment
                # if I use global swaps, then NameError: name 'swaps' is not defined
                swaps += 1
                currentSwaps += 1

    swap()

    return swaps
</code>```

答:
The swaps variable in your code is not global, it's nonlocal. It was defined in the enclosing function getNumOfSwapsToSort(), not in the global namespace.

Your other function, swap(), has access to this nonlocal variable, but the variable is protected from being edited from the inner function. If you try to edit or overwrite it, it's going to be treated as a new, local variable - hence the UnboundLocalError you're getting.

Try using the nonlocal statement:

```<code>nonlocal swaps
swaps += 1
</code>```

Here's an article with a few simple examples which explain the concept of closure in Python.

Body:
My example below. I have tried adding global to the variable that I want to change but doesn't help. The error I'm getting is in the comment.

def getNumOfSwapsToSort(array):
    sorted = array.copy()
    sorted.sort()

    swaps = 0

    def swap():
        currentSwaps = 0
        for i, el in enumerate(array):
            if ((len(array) - 1) == i) and currentSwaps != 0:
                swap()

            elif el != sorted[i]:
                idx = sorted.index(el)
                temp = array[idx]
                array[idx] = el
                array[i] = temp
                # UnboundLocalError: local variable 'swaps' referenced before assignment
                # if I use global swaps, then NameError: name 'swaps' is not defined
                swaps += 1
                currentSwaps += 1

    swap()

    return swaps

Body:
The swaps variable in your code is not global, it's nonlocal. It was defined in the enclosing function getNumOfSwapsToSort(), not in the global namespace.

Your other function, swap(), has access to this nonlocal variable, but the variable is protected from being edited from the inner function. If you try to edit or overwrite it, it's going to be treated as a new, local variable - hence the UnboundLocalError you're getting.

Try using the nonlocal statement:

nonlocal swaps
swaps += 1

Here's an article with a few simple examples which explain the concept of closure in Python.


问:
Recursion with '|' or '||' operator not returning false in base case
So I'm looking at recursion method I'm using to solve code challenge.   I believe this might be just a misunderstanding on my part.  Here is the recursive function.

```<code>    public static boolean canSum(int[] arr, int max)
    {
        System.out.println(Arrays.toString(arr));
        if(max == 0) return true;
        if(arr.length == 0) return false;
        else return canSum(Arrays.copyOfRange(arr, 1, arr.length), max) | canSum(Arrays.copyOfRange(arr, 1, arr.length), max-arr[0]);
    }
</code>```

Now it works in that it will eventually give me true or false if the integers in my Array in any combination will give me the max value I'm looking for, or it will return false.  What I don't understand is if I System.out.println(Arrays.toString(arr) I can see my array being reduced to 0 length and if I debug it appears to hit the line if(arr.length == 0) return false;.  So why does the function not break there and return false.  This recursive function keeps going essentially.  Here is the console arrays I'm seeing for this use case

```<code>System.out.println(canSum(new int[]{3,5,-1,8}, 12));
</code>```

Output:

```<code>[-1, 3, 5, 8]
[3, 5, 8]
[5, 8]
[8]
[]
[]
[8]
[]
[]
[5, 8]
[8]
[]
[]
[8]
[]
[]
[3, 5, 8]
[5, 8]
[8]
[]
[]
[8]
[]
[]
[5, 8]
[8]
[]
[]
[8]
[]
[]
true
</code>```

I understand how the Arrays.copyOfRange keeps removing an index of the array but I guess I'm not understanding the operator | (I think || works as well) and why I need to max=arr[0].  Based on debug, it looks like canSum(Arrays.copyOfRange(arr, 1, arr.length), max-arr[0]) runs when my arr.length = 0.  So is the OR really like ternary here for recursive function call?  I'm not sure how 'return recurseFunct(n-1, m) | recurseFunct (n-1, m-n[0])' works in this scenario exactly.  It seems really handy and I would like to understand it better.

****** Adding this video as it supports the accepted answer *******

You can visually see the stack that is being used in this recurse on the left hand side of the debugger.  The size of the stack varies for a bit until it finally empties completely.  Once empty i will return true or false in the recursive function at that point.  This was good learning experience for me.  Thank you!

答:
If I debug it appears to hit the line if(arr.length == 0) return
  false;. So why does the function not break there and return false.
  This recursive function keeps going essentially.

The sequence of the function call is like below. (index in a bracket).

```<code>                 call(0)
                /       \
        call(1)     ||     call(4)
        /     \             /    \
    call(2) || call(3)  call(5) || call(6)
    /\          /\      /\          /\
   /  \        /  \    /  \        /  \
</code>```

The functions call will be stored in a stack like below

```<code>call(2)
call(1)
call(0)
</code>```

if call(2) returns false, it will pop call(2) from the stack and come back to call(1) and push call(3) to stack.

```<code>call(3)
call(1)
call(0)
</code>```

if call(3) returns false, it will pop call(3) from stack, come back to call(1), call(1) will return (call(2) || call(3)),pop call(1) from stack, come back to call(0) and push call(4).

so even if any function call returns false, there are a remaining bunch of calls to be performed and so

recursive function keeps going essentially

Body:
So I'm looking at recursion method I'm using to solve code challenge.   I believe this might be just a misunderstanding on my part.  Here is the recursive function.

    public static boolean canSum(int[] arr, int max)
    {
        System.out.println(Arrays.toString(arr));
        if(max == 0) return true;
        if(arr.length == 0) return false;
        else return canSum(Arrays.copyOfRange(arr, 1, arr.length), max) | canSum(Arrays.copyOfRange(arr, 1, arr.length), max-arr[0]);
    }

Now it works in that it will eventually give me true or false if the integers in my Array in any combination will give me the max value I'm looking for, or it will return false.  What I don't understand is if I System.out.println(Arrays.toString(arr) I can see my array being reduced to 0 length and if I debug it appears to hit the line if(arr.length == 0) return false;.  So why does the function not break there and return false.  This recursive function keeps going essentially.  Here is the console arrays I'm seeing for this use case

System.out.println(canSum(new int[]{3,5,-1,8}, 12));

Output:

[-1, 3, 5, 8]
[3, 5, 8]
[5, 8]
[8]
[]
[]
[8]
[]
[]
[5, 8]
[8]
[]
[]
[8]
[]
[]
[3, 5, 8]
[5, 8]
[8]
[]
[]
[8]
[]
[]
[5, 8]
[8]
[]
[]
[8]
[]
[]
true

I understand how the Arrays.copyOfRange keeps removing an index of the array but I guess I'm not understanding the operator | (I think || works as well) and why I need to max=arr[0].  Based on debug, it looks like canSum(Arrays.copyOfRange(arr, 1, arr.length), max-arr[0]) runs when my arr.length = 0.  So is the OR really like ternary here for recursive function call?  I'm not sure how 'return recurseFunct(n-1, m) | recurseFunct (n-1, m-n[0])' works in this scenario exactly.  It seems really handy and I would like to understand it better.

****** Adding this video as it supports the accepted answer *******

You can visually see the stack that is being used in this recurse on the left hand side of the debugger.  The size of the stack varies for a bit until it finally empties completely.  Once empty i will return true or false in the recursive function at that point.  This was good learning experience for me.  Thank you!

Body:
If I debug it appears to hit the line if(arr.length == 0) return
  false;. So why does the function not break there and return false.
  This recursive function keeps going essentially.

The sequence of the function call is like below. (index in a bracket).

                 call(0)
                /       \
        call(1)     ||     call(4)
        /     \             /    \
    call(2) || call(3)  call(5) || call(6)
    /\          /\      /\          /\
   /  \        /  \    /  \        /  \

The functions call will be stored in a stack like below

call(2)
call(1)
call(0)

if call(2) returns false, it will pop call(2) from the stack and come back to call(1) and push call(3) to stack.

call(3)
call(1)
call(0)

if call(3) returns false, it will pop call(3) from stack, come back to call(1), call(1) will return (call(2) || call(3)),pop call(1) from stack, come back to call(0) and push call(4).

so even if any function call returns false, there are a remaining bunch of calls to be performed and so

recursive function keeps going essentially


问:
How to run node.js app forever when console is closed?
I connect to my remote server via ssh. Then I start my node.js app with Forever. Everything works fine until I close my console window. How to run node.js app FOREVER on my remote server even when I close my connection via ssh? I just want to start an app and shut down my copmputer. My app should be working in the background on my remote server.</p>

答:
Wow, I just found a very simple solution:

First, start your process (node app)

```<code>forever dist/index.js
</code>```

run: ^Z cmd + z.

Then: bg. Yeah.. bg (background).
And pum.. you are out.

Finish with exitif you are with sshor just close the terminal.

ContentLicense: CC BY-SA 3.0

Body:
Wow, I just found a very simple solution:

First, start your process (node app)

forever dist/index.js

run: ^Z cmd + z.

Then: bg. Yeah.. bg (background).
And pum.. you are out.

Finish with exitif you are with sshor just close the terminal.


问:
Can't pars subjectAlternativeNames in java
I try to parse extra data of certificate in java. I interested in subjectAlternativeNames section. My code is:

```<code>CertificateFactory certFactory = CertificateFactory.getInstance("X.509")
certFactory.generateCertificate(ByteArrayInputStream(x509EntryChain.leafCertificate))

</code>```

X509CertInfo contains java.io.IOException: invalid URI name:DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu.
and subjectAlternativeNames is empty.

How can I parse it?

I am interested only in extra_data, not in leaf_input. I can find the certificate here: http://transparencyreport.google.com/https/certificates/

The DNS names section is empty, seems Google can't parse it either.

Full code to parce cert:

```<code>
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import sun.security.x509.CertificateExtensions;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;

import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

public class CertParser {

    public static void main(String[] args) throws CertificateException, IOException {
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        ByteArrayInputStream in = new ByteArrayInputStream(Base64.decodeBase64(
                "AAAAAAFXsGq8HwAAAAW+MIIFujCCBKKgAwIBAgIDAZZpMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNVBAYTAkxVMRYwFAYDVQQKEw1MdXhUcnVzdCBTLkEuMR4wHAYDVQQDExVMdXhUcnVzdCBRdWFsaWZpZWQgQ0EwHhcNMTAwNDE1MDY0MzM0WhcNMTMwNDE1MDY0MzM0WjB8MQswCQYDVQQGEwJMVTERMA8GA1UEBxMITXVuc2JhY2gxFDASBgNVBAoTC0NFVFJFTCBTLkEuMRAwDgYDVQQLEwdJT1AtU1NTMRQwEgYDVQQDFAsqLmNldHJlbC5sdTEcMBoGCSqGSIb3DQEJARYNc3NzQGNldHJlbC5sdTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALhf+I7RshQlHMMWq/WPDLNxx+ODMd4Tn9ej14igvMEE+RYEagdMOZoeO8Bqz9qV2atzFHqz0D+Ad+cxuznBGKl4rhS9gUejgoAMox7car0+LSsv1NT4J0gAlnmH3BJlDMd9CihT0D/sRwMNfa8GYAvCuGDtWIvYb497RFy+2kmzk3cwCk3BgOO3MsT7iqhcn65Pd1Lq1vLjCCuQBoWLlcKk4uptPsyFKrHEh1/0ksY5evqBPxioVppoN+oay20RK36JzrzAl+vfpzq03WRlM2IgM0ItnesLqid9GqTUsOTq59i5aVX1EKlfgM5v7YCpYMLrJA+JBO3beR/4FSczfccCAwEAAaOCAnowggJ2MAwGA1UdEwEB/wQCMAAwYAYIKwYBBQUHAQEEVDBSMCMGCCsGAQUFBzABhhdodHRwOi8vb2NzcC5sdXh0cnVzdC5sdTArBggrBgEFBQcwAoYfaHR0cDovL2NhLmx1eHRydXN0Lmx1L0xUUUNBLmNydDBBBgNVHREEOjA4hjZETlM6Ki5jZXRyZWwubHUsIEROUzptYWlsLmNldHJlbC5sdSwgRE5TOnd3dy5jZXRyZWwubHUwggEABgNVHSAEgfgwgfUwgegGCCuBKwEBAgYBMIHbMIGtBggrBgEFBQcCAjCBoBqBnUx1eFRydXN0IFNlcnZlciBDZXJ0aWZpY2F0ZS4gTm90IHN1cHBvcnRlZCBieSBTU0NELCBLZXkgR2VuZXJhdGlvbiBieSBTdWJzY3JpYmVyLiBHVEMsIENQIGFuZCBDUFMgb24gaHR0cDovL3JlcG9zaXRvcnkubHV4dHJ1c3QubHUuIFNpZ25lZCBieSBhIFF1YWxpZmllZCBDQS4wKQYIKwYBBQUHAgEWHWh0dHA6Ly9yZXBvc2l0b3J5Lmx1eHRydXN0Lmx1MAgGBgQAj3oBAzARBglghkgBhvhCAQEEBAMCBeAwDgYDVR0PAQH/BAQDAgSwMCcGA1UdJQQgMB4GCCsGAQUFBwMBBggrBgEFBQcDAgYIKwYBBQUHAwQwHwYDVR0jBBgwFoAUjZCjB90aE3eZTJKrTUPeP80pZAUwMQYDVR0fBCowKDAmoCSgIoYgaHR0cDovL2NybC5sdXh0cnVzdC5sdS9MVFFDQS5jcmwwHQYDVR0OBBYEFLhOdzNJg4CSchxSsHbwKWMVm8xnMA0GCSqGSIb3DQEBBQUAA4IBAQBrQ9bYRA5O8a+4vIfVx5izH5x9yieKuQjpF4Etc4YUxfk3h5yZieWJuZGHygUjV5TCYDdhtVf6FFWkJ4FT5l6zDQeOLrEWqL12qcT4hGFN61mwjZO7kca8IHlqPPeqtYVg/Ssbpun+bjPOsGdvrvMulqNNTz5UeQuovc/VFaoHpYCQhezrQ6E6uQ684f6LFVIbsah6pT58wEnrf6xE1aRdSk27e3bF8wns3zOVsWE2wKck5pMS5DkGwjWli27Aqt6QQCyCKC7xqqxwL8GfnmXZNdn2iYYfSyr0I7rdqxa7FsuNFkEb8/PdZlyMxQP867YnucRCyLzzjCnRy1bOLnUeAAA="
        ));
        readNumber(in, 1); // version
        readNumber(in, 1); // type
        readNumber(in, 8); // timestamp
        readNumber(in, 2); // entry type
        int length = (int) readNumber(in, 3);
        byte[]  x509 = readFixedLength(in, length);
        X509CertImpl cert = (X509CertImpl) certFactory.generateCertificate(new ByteArrayInputStream(x509));
        ((CertificateExtensions)((X509CertInfo)cert.get("x509.info")).get("extensions")).getUnparseableExtensions(); // <-- want too be empty, but it's not!!!
    }

    static byte[] readFixedLength(InputStream inputStream, int dataLength) throws IOException {
        byte[] toReturn = new byte[dataLength];
        int bytesRead = inputStream.read(toReturn);
        if (bytesRead < dataLength) {
            throw new RuntimeException();
        }
        return toReturn;
    }

    static long readNumber(InputStream inputStream, int numBytes) throws IOException {
        long toReturn = 0;
        for (int i = 0; i < numBytes; i++) {
            int valRead = inputStream.read();
            if (valRead < 0) {
                throw new RuntimeException();
            }
            toReturn = (toReturn << 8) | valRead;
        }
        return toReturn;
    }
}

</code>```

答:
After you shared your code to access the certificate, I could safe and inspect it.

As was already clear from the question, the problematic extension is the subject alternative name. Its value is specified (RFC 5280) as an instance of GeneralNames which is a SEQUENCE of GeneralName which is a CHOICE:

SubjectAltName ::= GeneralNames

GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName

GeneralName ::= CHOICE {
     otherName                 [0]  AnotherName,
     rfc822Name                [1]  IA5String,
     dNSName                   [2]  IA5String,
     x400Address               [3]  ORAddress,
     directoryName             [4]  Name,
     ediPartyName              [5]  EDIPartyName,
     uniformResourceIdentifier [6]  IA5String,
     iPAddress                 [7]  OCTET STRING,
     registeredID              [8]  OBJECT IDENTIFIER }
```
</blockquote>

<p>This is a dump of the extension in question of your certificate:</p>

<pre class="lang-none prettyprint-override"><code>    <30 41>
 676   65: . . . . SEQUENCE {
    <06 03>
 678    3: . . . . . OBJECT IDENTIFIER subjectAltName (2 5 29 17)
         : . . . . . . (X.509 extension)
    <04 3A>
 683   58: . . . . . OCTET STRING, encapsulates {
    <30 38>
 685   56: . . . . . . SEQUENCE {
    <86 36>
 687   54: . . . . . . . [6]
         : . . . . . . . . 'DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cet'
         : . . . . . . . . 'rel.lu'
         : . . . . . . . }
         : . . . . . . }
         : . . . . . }
</code>```

The value is tagged with a 6 ([6]). Thus, the uniformResourceIdentifier choice is used. RFC 5280 in the context of subject alternative names requires for the value of this choice:

The name
     MUST NOT be a relative URI, and it MUST follow the URI syntax and
     encoding rules specified in [RFC3986].  The name MUST include both a
     scheme (e.g., "http" or "ftp") and a scheme-specific-part.

Thus, the sun.security.x509.X509CertImpl attempts to parse the value as an URI. This obviously must fail as the value simply is no URI:

java.io.IOException: invalid URI name:DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu
    at sun.security.x509.URIName.<init>(URIName.java:109)
    at sun.security.x509.URIName.<init>(URIName.java:96)
    at sun.security.x509.GeneralName.<init>(GeneralName.java:122)
    at sun.security.x509.GeneralName.<init>(GeneralName.java:76)
    at sun.security.x509.GeneralNames.<init>(GeneralNames.java:68)
    at sun.security.x509.SubjectAlternativeNameExtension.<init>(SubjectAlternativeNameExtension.java:141)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at sun.security.x509.CertificateExtensions.parseExtension(CertificateExtensions.java:113)
    at sun.security.x509.CertificateExtensions.init(CertificateExtensions.java:88)
    at sun.security.x509.CertificateExtensions.<init>(CertificateExtensions.java:78)
    at sun.security.x509.X509CertInfo.parse(X509CertInfo.java:702)
    at sun.security.x509.X509CertInfo.<init>(X509CertInfo.java:167)
    at sun.security.x509.X509CertImpl.parse(X509CertImpl.java:1804)
    at sun.security.x509.X509CertImpl.<init>(X509CertImpl.java:195)
    at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:102)
    at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:339)
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 16: DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.checkChars(URI.java:3021)
    at java.net.URI$Parser.parse(URI.java:3058)
    at java.net.URI.<init>(URI.java:588)
    at sun.security.x509.URIName.<init>(URIName.java:107)
    ... 46 more
```

<p>If you want to access the value nonetheless, simply retrieve and parse it from the <code>UnparseableExtensions</code> map you have like this:</p>

<pre class="lang-java prettyprint-override"><code>Map<String, Extension> unparseables = ((CertificateExtensions)((X509CertInfo)cert.get("x509.info")).get("extensions")).getUnparseableExtensions();

Extension extension = unparseables.get("2.5.29.17");
byte[] value = extension.getValue();
DerValue derValue = new DerValue(value);
while (derValue.data.available() > 0) {
    DerValue encName = derValue.data.getDerValue();
    if ((encName.tag & 0x1f) == 6) {
        encName.resetTag(DerValue.tag_IA5String);
        System.out.printf("IA5String value from URI GeneralName value: %s\n", encName.getIA5String());
    }
}
</code>```

for sun.security.x509.Extension and sun.security.util.DerValue.

The output:

IA5String value from URI GeneralName value: DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu
```

Body:
I try to parse extra data of certificate in java. I interested in subjectAlternativeNames section. My code is:

CertificateFactory certFactory = CertificateFactory.getInstance("X.509")
certFactory.generateCertificate(ByteArrayInputStream(x509EntryChain.leafCertificate))

X509CertInfo contains java.io.IOException: invalid URI name:DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu.
and subjectAlternativeNames is empty.

How can I parse it?

I am interested only in extra_data, not in leaf_input. I can find the certificate here: http://transparencyreport.google.com/https/certificates/

The DNS names section is empty, seems Google can't parse it either.

Full code to parce cert:

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import sun.security.x509.CertificateExtensions;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;

import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

public class CertParser {

    public static void main(String[] args) throws CertificateException, IOException {
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        ByteArrayInputStream in = new ByteArrayInputStream(Base64.decodeBase64(
                "AAAAAAFXsGq8HwAAAAW+MIIFujCCBKKgAwIBAgIDAZZpMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNVBAYTAkxVMRYwFAYDVQQKEw1MdXhUcnVzdCBTLkEuMR4wHAYDVQQDExVMdXhUcnVzdCBRdWFsaWZpZWQgQ0EwHhcNMTAwNDE1MDY0MzM0WhcNMTMwNDE1MDY0MzM0WjB8MQswCQYDVQQGEwJMVTERMA8GA1UEBxMITXVuc2JhY2gxFDASBgNVBAoTC0NFVFJFTCBTLkEuMRAwDgYDVQQLEwdJT1AtU1NTMRQwEgYDVQQDFAsqLmNldHJlbC5sdTEcMBoGCSqGSIb3DQEJARYNc3NzQGNldHJlbC5sdTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALhf+I7RshQlHMMWq/WPDLNxx+ODMd4Tn9ej14igvMEE+RYEagdMOZoeO8Bqz9qV2atzFHqz0D+Ad+cxuznBGKl4rhS9gUejgoAMox7car0+LSsv1NT4J0gAlnmH3BJlDMd9CihT0D/sRwMNfa8GYAvCuGDtWIvYb497RFy+2kmzk3cwCk3BgOO3MsT7iqhcn65Pd1Lq1vLjCCuQBoWLlcKk4uptPsyFKrHEh1/0ksY5evqBPxioVppoN+oay20RK36JzrzAl+vfpzq03WRlM2IgM0ItnesLqid9GqTUsOTq59i5aVX1EKlfgM5v7YCpYMLrJA+JBO3beR/4FSczfccCAwEAAaOCAnowggJ2MAwGA1UdEwEB/wQCMAAwYAYIKwYBBQUHAQEEVDBSMCMGCCsGAQUFBzABhhdodHRwOi8vb2NzcC5sdXh0cnVzdC5sdTArBggrBgEFBQcwAoYfaHR0cDovL2NhLmx1eHRydXN0Lmx1L0xUUUNBLmNydDBBBgNVHREEOjA4hjZETlM6Ki5jZXRyZWwubHUsIEROUzptYWlsLmNldHJlbC5sdSwgRE5TOnd3dy5jZXRyZWwubHUwggEABgNVHSAEgfgwgfUwgegGCCuBKwEBAgYBMIHbMIGtBggrBgEFBQcCAjCBoBqBnUx1eFRydXN0IFNlcnZlciBDZXJ0aWZpY2F0ZS4gTm90IHN1cHBvcnRlZCBieSBTU0NELCBLZXkgR2VuZXJhdGlvbiBieSBTdWJzY3JpYmVyLiBHVEMsIENQIGFuZCBDUFMgb24gaHR0cDovL3JlcG9zaXRvcnkubHV4dHJ1c3QubHUuIFNpZ25lZCBieSBhIFF1YWxpZmllZCBDQS4wKQYIKwYBBQUHAgEWHWh0dHA6Ly9yZXBvc2l0b3J5Lmx1eHRydXN0Lmx1MAgGBgQAj3oBAzARBglghkgBhvhCAQEEBAMCBeAwDgYDVR0PAQH/BAQDAgSwMCcGA1UdJQQgMB4GCCsGAQUFBwMBBggrBgEFBQcDAgYIKwYBBQUHAwQwHwYDVR0jBBgwFoAUjZCjB90aE3eZTJKrTUPeP80pZAUwMQYDVR0fBCowKDAmoCSgIoYgaHR0cDovL2NybC5sdXh0cnVzdC5sdS9MVFFDQS5jcmwwHQYDVR0OBBYEFLhOdzNJg4CSchxSsHbwKWMVm8xnMA0GCSqGSIb3DQEBBQUAA4IBAQBrQ9bYRA5O8a+4vIfVx5izH5x9yieKuQjpF4Etc4YUxfk3h5yZieWJuZGHygUjV5TCYDdhtVf6FFWkJ4FT5l6zDQeOLrEWqL12qcT4hGFN61mwjZO7kca8IHlqPPeqtYVg/Ssbpun+bjPOsGdvrvMulqNNTz5UeQuovc/VFaoHpYCQhezrQ6E6uQ684f6LFVIbsah6pT58wEnrf6xE1aRdSk27e3bF8wns3zOVsWE2wKck5pMS5DkGwjWli27Aqt6QQCyCKC7xqqxwL8GfnmXZNdn2iYYfSyr0I7rdqxa7FsuNFkEb8/PdZlyMxQP867YnucRCyLzzjCnRy1bOLnUeAAA="
        ));
        readNumber(in, 1); // version
        readNumber(in, 1); // type
        readNumber(in, 8); // timestamp
        readNumber(in, 2); // entry type
        int length = (int) readNumber(in, 3);
        byte[]  x509 = readFixedLength(in, length);
        X509CertImpl cert = (X509CertImpl) certFactory.generateCertificate(new ByteArrayInputStream(x509));
        ((CertificateExtensions)((X509CertInfo)cert.get("x509.info")).get("extensions")).getUnparseableExtensions(); // <-- want too be empty, but it's not!!!
    }

    static byte[] readFixedLength(InputStream inputStream, int dataLength) throws IOException {
        byte[] toReturn = new byte[dataLength];
        int bytesRead = inputStream.read(toReturn);
        if (bytesRead < dataLength) {
            throw new RuntimeException();
        }
        return toReturn;
    }

    static long readNumber(InputStream inputStream, int numBytes) throws IOException {
        long toReturn = 0;
        for (int i = 0; i < numBytes; i++) {
            int valRead = inputStream.read();
            if (valRead < 0) {
                throw new RuntimeException();
            }
            toReturn = (toReturn << 8) | valRead;
        }
        return toReturn;
    }
}

Body:
After you shared your code to access the certificate, I could safe and inspect it.

As was already clear from the question, the problematic extension is the subject alternative name. Its value is specified (RFC 5280) as an instance of GeneralNames which is a SEQUENCE of GeneralName which is a CHOICE:

SubjectAltName ::= GeneralNames

GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName

GeneralName ::= CHOICE {
     otherName                 [0]  AnotherName,
     rfc822Name                [1]  IA5String,
     dNSName                   [2]  IA5String,
     x400Address               [3]  ORAddress,
     directoryName             [4]  Name,
     ediPartyName              [5]  EDIPartyName,
     uniformResourceIdentifier [6]  IA5String,
     iPAddress                 [7]  OCTET STRING,
     registeredID              [8]  OBJECT IDENTIFIER }

This is a dump of the extension in question of your certificate:

    <30 41>
 676   65: . . . . SEQUENCE {
    <06 03>
 678    3: . . . . . OBJECT IDENTIFIER subjectAltName (2 5 29 17)
         : . . . . . . (X.509 extension)
    <04 3A>
 683   58: . . . . . OCTET STRING, encapsulates {
    <30 38>
 685   56: . . . . . . SEQUENCE {
    <86 36>
 687   54: . . . . . . . [6]
         : . . . . . . . . 'DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cet'
         : . . . . . . . . 'rel.lu'
         : . . . . . . . }
         : . . . . . . }
         : . . . . . }

The value is tagged with a 6 ([6]). Thus, the uniformResourceIdentifier choice is used. RFC 5280 in the context of subject alternative names requires for the value of this choice:

The name
     MUST NOT be a relative URI, and it MUST follow the URI syntax and
     encoding rules specified in [RFC3986].  The name MUST include both a
     scheme (e.g., "http" or "ftp") and a scheme-specific-part.

Thus, the sun.security.x509.X509CertImpl attempts to parse the value as an URI. This obviously must fail as the value simply is no URI:

java.io.IOException: invalid URI name:DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu
    at sun.security.x509.URIName.<init>(URIName.java:109)
    at sun.security.x509.URIName.<init>(URIName.java:96)
    at sun.security.x509.GeneralName.<init>(GeneralName.java:122)
    at sun.security.x509.GeneralName.<init>(GeneralName.java:76)
    at sun.security.x509.GeneralNames.<init>(GeneralNames.java:68)
    at sun.security.x509.SubjectAlternativeNameExtension.<init>(SubjectAlternativeNameExtension.java:141)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at sun.security.x509.CertificateExtensions.parseExtension(CertificateExtensions.java:113)
    at sun.security.x509.CertificateExtensions.init(CertificateExtensions.java:88)
    at sun.security.x509.CertificateExtensions.<init>(CertificateExtensions.java:78)
    at sun.security.x509.X509CertInfo.parse(X509CertInfo.java:702)
    at sun.security.x509.X509CertInfo.<init>(X509CertInfo.java:167)
    at sun.security.x509.X509CertImpl.parse(X509CertImpl.java:1804)
    at sun.security.x509.X509CertImpl.<init>(X509CertImpl.java:195)
    at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:102)
    at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:339)
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 16: DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.checkChars(URI.java:3021)
    at java.net.URI$Parser.parse(URI.java:3058)
    at java.net.URI.<init>(URI.java:588)
    at sun.security.x509.URIName.<init>(URIName.java:107)
    ... 46 more

If you want to access the value nonetheless, simply retrieve and parse it from the UnparseableExtensions map you have like this:

Map<String, Extension> unparseables = ((CertificateExtensions)((X509CertInfo)cert.get("x509.info")).get("extensions")).getUnparseableExtensions();

Extension extension = unparseables.get("2.5.29.17");
byte[] value = extension.getValue();
DerValue derValue = new DerValue(value);
while (derValue.data.available() > 0) {
    DerValue encName = derValue.data.getDerValue();
    if ((encName.tag & 0x1f) == 6) {
        encName.resetTag(DerValue.tag_IA5String);
        System.out.printf("IA5String value from URI GeneralName value: %s\n", encName.getIA5String());
    }
}

for sun.security.x509.Extension and sun.security.util.DerValue.

The output:

IA5String value from URI GeneralName value: DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu


问:
Why does not my code in C++ that declares an (local) int array without doing anything initialize by zeros?
I have two C++ codes in which the one has an global int array while the other code has a local array whose length is determined by user input (so at runtime).
Both arrays are not explicitly initialized.

#include <iostream>
using namespace std;
int M = 1000;
int a[M];
int main() {
  for(int i = 0; i < M; i++)
    cout << a[i];
  cout << endl;
}
```

<pre class="lang-cpp prettyprint-override"><code>#include <iostream>
using namespace std;
int main() {
  int M;
  cin >> M;
  int a[M];
  for(int i = 0; i < M; i++)
    cout << a[i];
  cout << endl;
}

</code>```

I observed that the global array is filled with zeros, while the local array (whose length is determined at runtime) is not filled with zeros and instead filled with a random numbers (but same at a time).
I used the g++ compiler.

What is this behavior?
Does C++ standard define this behavior?

答:
What is this behavior?

The behaviour is that objects with static storage duration are zero-initialised before any other initialisation (if any).

The behaviour for all other storage duration is that there is no additional zero initialisation.

Does C++ standard define this behavior?

Yes, the zero initialisation of static objects is defined in the standard. The behaviour of reading indeterminate values is specified to be undefined.

Both programs are ill-formed because the size of an (non dynamic) array must be compile time constant, which M is not.

Body:
I have two C++ codes in which the one has an global int array while the other code has a local array whose length is determined by user input (so at runtime).
Both arrays are not explicitly initialized.

#include <iostream>
using namespace std;
int M = 1000;
int a[M];
int main() {
  for(int i = 0; i < M; i++)
    cout << a[i];
  cout << endl;
}

#include <iostream>
using namespace std;
int main() {
  int M;
  cin >> M;
  int a[M];
  for(int i = 0; i < M; i++)
    cout << a[i];
  cout << endl;
}

I observed that the global array is filled with zeros, while the local array (whose length is determined at runtime) is not filled with zeros and instead filled with a random numbers (but same at a time).
I used the g++ compiler.

What is this behavior?
Does C++ standard define this behavior?


问:
XML parsing with Python and regex does not return all results
I am still struggling with regexp:

```<code>import re

text = '''
          <SW-VARIABLE>
            <SHORT-NAME>abc</SHORT-NAME>
            <CATEGORY>VALUE</CATEGORY>
            <SW-ARRAYSIZE>
              <VF>4</VF>
            </SW-ARRAYSIZE>
            <SW-DATA-DEF-PROPS>
              cde
            </SW-DATA-DEF-PROPS>
          </SW-VARIABLE>

          <SW-VARIABLE>
            <SHORT-NAME>def</SHORT-NAME>
            <CATEGORY>VALUE</CATEGORY>
            <SW-ARRAYSIZE>
              <VF>8</VF>
            </SW-ARRAYSIZE>
            <SW-DATA-DEF-PROPS>
                <HELLO>dsfadsf </HELLO>
                <NO>itis</NO>
            </SW-DATA-DEF-PROPS>
          </SW-VARIABLE>
'''

pattern = r'<SW-VARIABLE>\s*<SHORT-NAME>([^<]*)</SHORT-NAME>.*<SW-ARRAYSIZE>\s*<VF>([^<]*)</VF>\s*</SW-ARRAYSIZE>.*?<(?:/(?!<SW-VARIABLE>)[^/]*?)SW-VARIABLE>'

print(re.findall(pattern, text, re.S))
</code>```

This returns:

```<code>[('abc', '8')]
</code>```

I would expect it to return:

```<code>[('abc', '4'), ('def', '8')]
</code>```

Why is it so greedy and matches everything until the last closing tag?

This is the regex101 link: https://regex101.com/r/ANO7RA/1

Maybe negative lookahead will solve this. I was not able to fully grasp the concept, though... :-(

答:
This is the pattern you need.</p>

```<code>pattern = r'<SW-VARIABLE>\s*<SHORT-NAME>([^<].*?)</SHORT-NAME>.*?<SW-ARRAYSIZE>\s*<VF>([^<]*?)</VF>\s*</SW-ARRAYSIZE>.*?<(?:/(?!<SW-VARIABLE>)[^/]*?)SW-VARIABLE>'

</code>```

Body:
I am still struggling with regexp:

import re

text = '''
          <SW-VARIABLE>
            <SHORT-NAME>abc</SHORT-NAME>
            <CATEGORY>VALUE</CATEGORY>
            <SW-ARRAYSIZE>
              <VF>4</VF>
            </SW-ARRAYSIZE>
            <SW-DATA-DEF-PROPS>
              cde
            </SW-DATA-DEF-PROPS>
          </SW-VARIABLE>

          <SW-VARIABLE>
            <SHORT-NAME>def</SHORT-NAME>
            <CATEGORY>VALUE</CATEGORY>
            <SW-ARRAYSIZE>
              <VF>8</VF>
            </SW-ARRAYSIZE>
            <SW-DATA-DEF-PROPS>
                <HELLO>dsfadsf </HELLO>
                <NO>itis</NO>
            </SW-DATA-DEF-PROPS>
          </SW-VARIABLE>
'''

pattern = r'<SW-VARIABLE>\s*<SHORT-NAME>([^<]*)</SHORT-NAME>.*<SW-ARRAYSIZE>\s*<VF>([^<]*)</VF>\s*</SW-ARRAYSIZE>.*?<(?:/(?!<SW-VARIABLE>)[^/]*?)SW-VARIABLE>'

print(re.findall(pattern, text, re.S))

This returns:

[('abc', '8')]

I would expect it to return:

[('abc', '4'), ('def', '8')]

Why is it so greedy and matches everything until the last closing tag?

This is the regex101 link: https://regex101.com/r/ANO7RA/1

Maybe negative lookahead will solve this. I was not able to fully grasp the concept, though... :-(

Body:
This is the pattern you need.

pattern = r'<SW-VARIABLE>\s*<SHORT-NAME>([^<].*?)</SHORT-NAME>.*?<SW-ARRAYSIZE>\s*<VF>([^<]*?)</VF>\s*</SW-ARRAYSIZE>.*?<(?:/(?!<SW-VARIABLE>)[^/]*?)SW-VARIABLE>'


问:
Conditional flag based on Groupby Python
I want to use conditional statements to create flag based on id and precedence order -

Dataframe -

```<code>    df=pd.DataFrame({'id':[1,1,1,1,2,3,3,3],
     'var':['Apple','Banana','Orange','Mango', 'Mango', 'Banana','Orange','Mango'],
      'flag':[1,1,1,1,1,1,1,1]})
</code>```

Precedence order -
Apple > Banana > Orange

Conditions -

1 - if Apple, Banana & Mango are present in id then flag for Banana & Ornage becomes 0, there will be no impact on Mango, it will be 1.

2 - If Banana & Orange are present then flag for Orange will become 0

Output -

```<code>     df=pd.DataFrame({'id':[1,1,1,1,2,3,3,3],
     'var':['Apple','Banana','Orange','Mango', 'Mango', 'Banana','Orange','Mango'],
      'flag':[1,0,0,1,1,1,0,1]})
</code>```

Method -

I have tried grouping the data and thought of performing the intersection by defining conditional lists.

I believe I have complicated it and doing the wrong way. Please guide.

```<code>group_test = df.groupby(['id'],as_index=False).var.agg(lambda x: x.unique().tolist())
list_1 = ['Apple','Banana','Orange']
list_2 = ['Banana', 'Orange']
</code>```

答:
We can loop through each chunk of the GroupBy object and set the flag column to the value required where rows meet the certain condition using loc.

First let's set all of our conditions ahead of time.

```<code>match1 = {'Apple', 'Banana', 'Mango'}
match2 = {'Banana', 'Orange'}
p_cond = (df['var'].str.contains('Banana|Orange', regex=True))
s_cond = (df['var'] == 'Orange')
</code>```

Then we can loop over our grouped object setting the flag column if it matches our criteria.

```<code>for k, v in df.groupby('id')['var']:
    vals = set(v)
    id_cond = (df['id'] == k)

    if vals.issuperset(match1):
        df.loc[id_cond & p_cond, 'flag'] = 0
    elif vals.issuperset(match2):
        df.loc[id_cond & s_cond, 'flag'] = 0
    else:
        continue
</code>```

This yields us the following results.

```<code>   flag  id     var
0     1   1   Apple
1     0   1  Banana
2     0   1  Orange
3     1   1   Mango
4     1   2   Mango
5     1   3  Banana
6     0   3  Orange
7     1   3   Mango
</code>```

Body:
I want to use conditional statements to create flag based on id and precedence order -

Dataframe -

    df=pd.DataFrame({'id':[1,1,1,1,2,3,3,3],
     'var':['Apple','Banana','Orange','Mango', 'Mango', 'Banana','Orange','Mango'],
      'flag':[1,1,1,1,1,1,1,1]})

Precedence order -
Apple > Banana > Orange

Conditions -

1 - if Apple, Banana & Mango are present in id then flag for Banana & Ornage becomes 0, there will be no impact on Mango, it will be 1.

2 - If Banana & Orange are present then flag for Orange will become 0

Output -

     df=pd.DataFrame({'id':[1,1,1,1,2,3,3,3],
     'var':['Apple','Banana','Orange','Mango', 'Mango', 'Banana','Orange','Mango'],
      'flag':[1,0,0,1,1,1,0,1]})

Method -

I have tried grouping the data and thought of performing the intersection by defining conditional lists.

I believe I have complicated it and doing the wrong way. Please guide.

group_test = df.groupby(['id'],as_index=False).var.agg(lambda x: x.unique().tolist())
list_1 = ['Apple','Banana','Orange']
list_2 = ['Banana', 'Orange']

Body:
We can loop through each chunk of the GroupBy object and set the flag column to the value required where rows meet the certain condition using loc.

First let's set all of our conditions ahead of time.

match1 = {'Apple', 'Banana', 'Mango'}
match2 = {'Banana', 'Orange'}
p_cond = (df['var'].str.contains('Banana|Orange', regex=True))
s_cond = (df['var'] == 'Orange')

Then we can loop over our grouped object setting the flag column if it matches our criteria.

for k, v in df.groupby('id')['var']:
    vals = set(v)
    id_cond = (df['id'] == k)

    if vals.issuperset(match1):
        df.loc[id_cond & p_cond, 'flag'] = 0
    elif vals.issuperset(match2):
        df.loc[id_cond & s_cond, 'flag'] = 0
    else:
        continue

This yields us the following results.

   flag  id     var
0     1   1   Apple
1     0   1  Banana
2     0   1  Orange
3     1   1   Mango
4     1   2   Mango
5     1   3  Banana
6     0   3  Orange
7     1   3   Mango


问:
Passing Data between BottomNavigation fragments via NavigationComponent
Is it possible to pass Parcelable object/arguments between BottomNavigation controlled Fragments via NavigationContoller?

Here is the flow of my app, user logs in to app and a fragment is opened which contains BottomNavigation. I was able to get the argument to first fragment(first fragment opens using         NavHostFragment.findNavController(this).navigate(action) ) i was able to add arguments to this action and pass values, but BottomNavigation doesn't have any navigate instructions navigation is done via menuID. How can i pass logged in user argument between all these bottom navigated fragments via bundle.

I tried to pass arguments using onDestinationChanged()

```<code>@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
        NavArgument argument = new NavArgument.Builder().setDefaultValue(selectedUser).build();
        destination.addArgument("user", argument);
}
</code>```

but still app crashes with     java.lang.IllegalArgumentException: Required argument "user" is missing and does not have an android:defaultValue

答:
We can either user SharedViewModel as asad has mentioned or we can use NavGraph to set argument</p>
```<code>navController.getGraph().findNode(R.id.todoFragment)
            .addArgument("user", new NavArgument.Builder()
                    .setDefaultValue(user)
                    .build());
</code>```

Body:
Is it possible to pass Parcelable object/arguments between BottomNavigation controlled Fragments via NavigationContoller?

Here is the flow of my app, user logs in to app and a fragment is opened which contains BottomNavigation. I was able to get the argument to first fragment(first fragment opens using         NavHostFragment.findNavController(this).navigate(action) ) i was able to add arguments to this action and pass values, but BottomNavigation doesn't have any navigate instructions navigation is done via menuID. How can i pass logged in user argument between all these bottom navigated fragments via bundle.

I tried to pass arguments using onDestinationChanged()

@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
        NavArgument argument = new NavArgument.Builder().setDefaultValue(selectedUser).build();
        destination.addArgument("user", argument);
}

but still app crashes with     java.lang.IllegalArgumentException: Required argument "user" is missing and does not have an android:defaultValue

Body:
We can either user SharedViewModel as asad has mentioned or we can use NavGraph to set argument

navController.getGraph().findNode(R.id.todoFragment)
            .addArgument("user", new NavArgument.Builder()
                    .setDefaultValue(user)
                    .build());


问:
LINQ: Get unique rows by selected ID's based on latest time
I have managed to connect to my database using the current syntax below and running simple LINQ query's.
I have a database containing around 20 unique SHIP_ID's that gets updated information regarding positions for all ships every 2 minutes. That means SHIP_ID and TIMESTAMP combined together are unique, and that my database has a lot of data in it so i want the load to be on the SQL server to return the correct data. And not the client to go through a response of 20k lines to find the value i want.

I want to retrieve the last position stored within the database for a group of select ships, lets says the ships with SHIP_ID's 1, 2 and 3. As of now the SHIP_ID's are stored in a string[].

```<code>            using (var scope = _scopeFactory.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetRequiredService<AisRecordContext>();
                var query = dbContext.SimpleAisRecords
                    //LINQ statements goes here to get intended result
                    .Where ?
                    .GroupBy ?
                    .Select?
                    .OrderderByDecending ?

            }
</code>```

How could I go forward making the correct LINQ statements to get the data i want? I also want the result to have the whole row it selects as data, for example LAT, LON, SPEED and HEADING that are some of my other columns.

I also wonder what is the best format to return the result. The result will be the row's that returns, the row where TIMESTAMP is newest for the SHIP_ID's i want to call. I will need all the columns that my database has (about 15 columns) since i will display the data in a map.

Would I use ToList? ToArray? or somehow just have my query be the result without modifying it after it gets added.

Thanks for your time (and sorry for a very specific question, could not find any articles getting multiply rows by selected ID's for their latest DATE)

答:
I assume your SHIP_ID is of type int? If so:

```<code>var ships = new string[]{ "1","2","3"};
using (var scope = _scopeFactory.CreateScope())
{
    var dbContext = scope.ServiceProvider.GetRequiredService<AisRecordContext>();
    var result = dbContext.SimpleAisRecords
        .Where(r => ships.Any(i => int.Parse(i) == r.SHIP_ID)) //Looks for any records matching any item in ships
        .OrderByDescending(r => r.TIMESTAMP); // Orders by time stamp descending...
}
</code>```

After you could select the one that is the latest one (which will be the first one since we ordered by time stamp) like so:
var latestShip = result.FirstOrDefault();

As far as what the type of collection to return, it depends on what you need to do after... having the original record will give you flexibility as you can updated, delete if required.

Body:
I have managed to connect to my database using the current syntax below and running simple LINQ query's.
I have a database containing around 20 unique SHIP_ID's that gets updated information regarding positions for all ships every 2 minutes. That means SHIP_ID and TIMESTAMP combined together are unique, and that my database has a lot of data in it so i want the load to be on the SQL server to return the correct data. And not the client to go through a response of 20k lines to find the value i want.

I want to retrieve the last position stored within the database for a group of select ships, lets says the ships with SHIP_ID's 1, 2 and 3. As of now the SHIP_ID's are stored in a string[].

            using (var scope = _scopeFactory.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetRequiredService<AisRecordContext>();
                var query = dbContext.SimpleAisRecords
                    //LINQ statements goes here to get intended result
                    .Where ?
                    .GroupBy ?
                    .Select?
                    .OrderderByDecending ?

            }

How could I go forward making the correct LINQ statements to get the data i want? I also want the result to have the whole row it selects as data, for example LAT, LON, SPEED and HEADING that are some of my other columns.

I also wonder what is the best format to return the result. The result will be the row's that returns, the row where TIMESTAMP is newest for the SHIP_ID's i want to call. I will need all the columns that my database has (about 15 columns) since i will display the data in a map.

Would I use ToList? ToArray? or somehow just have my query be the result without modifying it after it gets added.

Thanks for your time (and sorry for a very specific question, could not find any articles getting multiply rows by selected ID's for their latest DATE)

Body:
I assume your SHIP_ID is of type int? If so:

var ships = new string[]{ "1","2","3"};
using (var scope = _scopeFactory.CreateScope())
{
    var dbContext = scope.ServiceProvider.GetRequiredService<AisRecordContext>();
    var result = dbContext.SimpleAisRecords
        .Where(r => ships.Any(i => int.Parse(i) == r.SHIP_ID)) //Looks for any records matching any item in ships
        .OrderByDescending(r => r.TIMESTAMP); // Orders by time stamp descending...
}

After you could select the one that is the latest one (which will be the first one since we ordered by time stamp) like so:
var latestShip = result.FirstOrDefault();

As far as what the type of collection to return, it depends on what you need to do after... having the original record will give you flexibility as you can updated, delete if required.


问:
New column based on several YES/ NO columns in RStudio
I am fairly new to RStudio and have been able to learn and solve so many problems thanks to this and similar sites, thank you all very much!
However I have not been able to solve the following and have tried several types of searches and codes.

I have a dataset in RStudio with 1400 entries(patients) and 68 columns, no missing data.

For some of the data on medication I want to create some new columns, like "antithrombotic medication YES/NO" and "heart medication YES/NO".
These should be based on 5-10 columns that either give a YES or a NO. If any column gives a "YES", the new column should be "YES" otherwise "NO".
I have tried several versions of "mutate", "if else" etc where I also tried to use "|" to write "or" but no "luck" so far.

I have several examples, but none that I really believed in too much
.. trial and error! This one is based of a solution to a similar problem that I was able to solve

hipfx_master60 <- hipfx_master60 %>%
  mutate(cardiac_HBP_antithromb_med = case_when({Diuretika="YES"|Betablokkere="YES"|Cakanalblokkere="YES"|ACEhaemmere="YES"|ATIIinhibitorer="YES"|Antithrombotica="YES"|Thrombocythammere="YES"|Vitamin_K_antagonister="YES"|Digoxin="YES"|Organiske_nitrater="YES"} ~ "YES", TRUE ~ "NO"))
```

(image removed)

Any help greatly appreciated!

答:
Here is a base R approach. First reproducible data. Glancing at your data, NO seems to be much more common than YES:

```<code>set.seed(42)
dta <- data.frame(matrix(sample(c("YES", rep("NO", 9)), 100, replace=TRUE), 20))
</code>```

Now compute column X6:

```<code>dta$X6 <- apply(dta[, c("X1", "X3", "X5")], 1, function(x) any(x=="YES"))
dta$X6 <- ifelse(dta$X6=="TRUE", "YES", "NO")
head(dta)
#    X1 X2 X3  X4  X5  X6
# 1 YES NO NO  NO YES YES
# 2  NO NO NO  NO  NO  NO
# 3 YES NO NO  NO  NO YES
# 4  NO NO NO  NO  NO  NO
# 5  NO NO NO YES  NO  NO
# 6  NO NO NO YES  NO  NO
</code>```

Body:
I am fairly new to RStudio and have been able to learn and solve so many problems thanks to this and similar sites, thank you all very much!
However I have not been able to solve the following and have tried several types of searches and codes.

I have a dataset in RStudio with 1400 entries(patients) and 68 columns, no missing data.

For some of the data on medication I want to create some new columns, like "antithrombotic medication YES/NO" and "heart medication YES/NO".
These should be based on 5-10 columns that either give a YES or a NO. If any column gives a "YES", the new column should be "YES" otherwise "NO".
I have tried several versions of "mutate", "if else" etc where I also tried to use "|" to write "or" but no "luck" so far.

I have several examples, but none that I really believed in too much
.. trial and error! This one is based of a solution to a similar problem that I was able to solve

hipfx_master60 <- hipfx_master60 %>%
  mutate(cardiac_HBP_antithromb_med = case_when({Diuretika="YES"|Betablokkere="YES"|Cakanalblokkere="YES"|ACEhaemmere="YES"|ATIIinhibitorer="YES"|Antithrombotica="YES"|Thrombocythammere="YES"|Vitamin_K_antagonister="YES"|Digoxin="YES"|Organiske_nitrater="YES"} ~ "YES", TRUE ~ "NO"))

(image removed)

Any help greatly appreciated!

Body:
Here is a base R approach. First reproducible data. Glancing at your data, NO seems to be much more common than YES:

set.seed(42)
dta <- data.frame(matrix(sample(c("YES", rep("NO", 9)), 100, replace=TRUE), 20))

Now compute column X6:

dta$X6 <- apply(dta[, c("X1", "X3", "X5")], 1, function(x) any(x=="YES"))
dta$X6 <- ifelse(dta$X6=="TRUE", "YES", "NO")
head(dta)
#    X1 X2 X3  X4  X5  X6
# 1 YES NO NO  NO YES YES
# 2  NO NO NO  NO  NO  NO
# 3 YES NO NO  NO  NO YES
# 4  NO NO NO  NO  NO  NO
# 5  NO NO NO YES  NO  NO
# 6  NO NO NO YES  NO  NO


问:
sum values in sub array
this is how I store values to array

```<code>$array[$row['name']][] = $row['from'] - $row['to'];
</code>```

and here is result

```<code>Array
(
   [name1] => Array
    (
        [0] => 1
        [1] => 12
        [2] => 10
    )

)

Array
(
   [name2] => Array
    (
        [0] => 0.25
        [1] => 0.55
        [2] => 0.35
        [3] => 5
    )

)
</code>```

i need result like

```<code>echo $sum_of_first_array. "hours" .$name;
</code>```

or:

23 hours name1

6.15 hours name2

...

答:
I could be wrong but this looks like PHP to me. In which case I would probably just use objects as follows:</p>

```<code><?

class className {
    public $name;
    public $hours;
}

$name1 = new className();
$name1->name = "name1";
$name1->hours = array(1, 12, 10);

$name2 = new className();
$name2->name = "name2";
$name2->hours = array(0.25, 0.55, 0.35, 5);

$names = array($name1, $name2);

foreach ($names as $name){
    $sum_name = 0.00;
    foreach ($name->hours as $value){
        $sum_name = $sum_name + $value;
    }
    echo($sum_name . " hours " . $name->name);
    echo("<br>");
}
?>
</code>```

Title: sum values in sub array

Body:
this is how I store values to array

$array[$row['name']][] = $row['from'] - $row['to'];

and here is result

Array
(
   [name1] => Array
    (
        [0] => 1
        [1] => 12
        [2] => 10
    )

)

Array
(
   [name2] => Array
    (
        [0] => 0.25
        [1] => 0.55
        [2] => 0.35
        [3] => 5
    )

)

i need result like

echo $sum_of_first_array. "hours" .$name;

or:

23 hours name1

6.15 hours name2

...

Body:
I could be wrong but this looks like PHP to me. In which case I would probably just use objects as follows:

<?

class className {
    public $name;
    public $hours;
}

$name1 = new className();
$name1->name = "name1";
$name1->hours = array(1, 12, 10);

$name2 = new className();
$name2->name = "name2";
$name2->hours = array(0.25, 0.55, 0.35, 5);

$names = array($name1, $name2);

foreach ($names as $name){
    $sum_name = 0.00;
    foreach ($name->hours as $value){
        $sum_name = $sum_name + $value;
    }
    echo($sum_name . " hours " . $name->name);
    echo("<br>");
}
?>


问:
How to use FluentD as a buffer between Telegraf and InfluxDB
Is there any way to ship metrics gathered form Telegraf to FluentD, then into InfluxDB?

I know it's possible to write data from FluentD into InfluxDB; but how does one ship data from Telegraf into FluentD, basically using use FluentD as a buffer (as opposed to using Kafka or Redis)?

答:
While it might be possible to do with FluentD using some of the available, although outdated output plugins, such as InfluxDB-Metrics, I couldn't get the plugin to work properly and it hasn't been updated in over six years, so it will probably not work with newer releases of FluentD.

Fluent Bit however, has an Influxdb output built right into it, so I was able to get it to work with that. The caveat is that it has no Telegraf plugin. So the solution I found was to setup a tcp input plugin in Fluent Bit, and set Telegraf to write JSON formatted data to it in it's output section.

The caveat of doing this, is that the JSON data is nested and not formatted properly for InfluxDB. The workaround is to use nest filters in Fluent Bit to 'lift' the nested data format, and re-format properly for InfluxDB.

Below is an example for disk-space, which is not a metric that is natively supported with Fluent Bit metrics but is natively supported with Telegraf:

```<code>@SET me=${HOST_HOSTNAME}

[INPUT]  ## tcp recipe  ## Collect data from telegraf
    Name          tcp
    Listen        0.0.0.0
    Port          5170
    Tag           telegraf.${me}
    Chunk_Size    32
    Buffer_Size   64
    Format        json
[FILTER]  ## rename the three tags sent from Telegraf to prevent duplicates
    Name          modify
    Match         telegraf.*
    Condition Key_Value_Equals name disk
    Rename        fields   fieldsDisk
    Rename        name     nameDisk
    Rename        tags     tagsDisk
[FILTER]  ## un-nest nested JSON formatted info under 'field' tag
    Name          nest
    Match         telegraf.*
    Operation     lift
    Nested_under  fieldsDisk
    Add_prefix    disk.
[FILTER]  ## un-nest nested JSON formatted info under 'disk' tag
    Name          nest
    Match         telegraf.*
    Operation     lift
    Nested_under  tagsDisk
    Add_prefix    disk.
[OUTPUT]  ## output properly formatted JSON info
    Name          influxdb
    Match         telegraf.*
    Host          influxdb.server.com
    Port          8086
    #HTTP_User     whatever
    #HTTP_Passwd   whatever
    Database      telegraf.${me}
    Sequence_Tag  point_in_time
    Auto_Tags     On
</code>```

NOTE: This is just a simple awkward config for my own proof of concept

Body:
While it might be possible to do with FluentD using some of the available, although outdated output plugins, such as InfluxDB-Metrics, I couldn't get the plugin to work properly and it hasn't been updated in over six years, so it will probably not work with newer releases of FluentD.

Fluent Bit however, has an Influxdb output built right into it, so I was able to get it to work with that. The caveat is that it has no Telegraf plugin. So the solution I found was to setup a tcp input plugin in Fluent Bit, and set Telegraf to write JSON formatted data to it in it's output section.

The caveat of doing this, is that the JSON data is nested and not formatted properly for InfluxDB. The workaround is to use nest filters in Fluent Bit to 'lift' the nested data format, and re-format properly for InfluxDB.

Below is an example for disk-space, which is not a metric that is natively supported with Fluent Bit metrics but is natively supported with Telegraf:

@SET me=${HOST_HOSTNAME}

[INPUT]  ## tcp recipe  ## Collect data from telegraf
    Name          tcp
    Listen        0.0.0.0
    Port          5170
    Tag           telegraf.${me}
    Chunk_Size    32
    Buffer_Size   64
    Format        json
[FILTER]  ## rename the three tags sent from Telegraf to prevent duplicates
    Name          modify
    Match         telegraf.*
    Condition Key_Value_Equals name disk
    Rename        fields   fieldsDisk
    Rename        name     nameDisk
    Rename        tags     tagsDisk
[FILTER]  ## un-nest nested JSON formatted info under 'field' tag
    Name          nest
    Match         telegraf.*
    Operation     lift
    Nested_under  fieldsDisk
    Add_prefix    disk.
[FILTER]  ## un-nest nested JSON formatted info under 'disk' tag
    Name          nest
    Match         telegraf.*
    Operation     lift
    Nested_under  tagsDisk
    Add_prefix    disk.
[OUTPUT]  ## output properly formatted JSON info
    Name          influxdb
    Match         telegraf.*
    Host          influxdb.server.com
    Port          8086
    #HTTP_User     whatever
    #HTTP_Passwd   whatever
    Database      telegraf.${me}
    Sequence_Tag  point_in_time
    Auto_Tags     On

NOTE: This is just a simple awkward config for my own proof of concept


问:
Diagnose app crashing on startup in specific machines?
Recently i upgraded from Delphi 2007 to 10.3.3 . The same app when compiled with 10.3.3 will crash at startup, but only in some machines ; in my environment i could not simulate the problem but some of my customers are having this issue. If i compile the same app in D2007, the error won't happen so i assume is something related to the new Delphi version.

The error message is this below and after this, the app will close. The message is too generic, what can i do to at least try to identify what is causing the crash ?

Here is what i tried : in the main form of my application, i went to the FormCreate event and put a showmessage('stop'); on the first line, but the crash happens before this.

I know this is a hard question, but i'm seeking for suggestions to at least look in some direction, so please help !

Thanks in advance.

UPDATE : Here is Eureka Log trace :

答:
After install EurekaLog, i could find the problem was related to Datasnap. Here is what i did that fixed the issue, hopefully will help somebody.

The library midas.dll was already present in the same folder of the app. I then copied midas.dll to c:\windows\syswow64 and run the command below in prompt with Admin rights :

```<code>c:\windows\syswow64\regsvr32 midas.dll
</code>```

After that the app runs fine.

Can somebody explains why this was needed ?

Body:
After install EurekaLog, i could find the problem was related to Datasnap. Here is what i did that fixed the issue, hopefully will help somebody.

The library midas.dll was already present in the same folder of the app. I then copied midas.dll to c:\windows\syswow64 and run the command below in prompt with Admin rights :

c:\windows\syswow64\regsvr32 midas.dll

After that the app runs fine.

Can somebody explains why this was needed ?


问:
ERR_OUT_OF_RANGE problem with node and SQL Server bulk insert
When I try to use bulk insert, I get a rangeError:

[ERR_OUT_OF_RANGE]: The value of "value" is out of range. It must be >= -2147483648 and <= 2147483647. Received 3912066366.

I know it is a node problem but any work around would be greatly appreciated

Here is the data set I am using https://github.com/gerrymandr/vtd-adjacency-graphs/

Insert code:

```<code>async function insertBulkNode(json) {
  const newPool = await sql.connect(dbConfig)
  let count = json.length;

  const table = new sql.Table('[nodes_shapefile]');
  table.create = true;
  table.columns.add('stCode', sql.Bit, { nullable: true });
  table.columns.add('boundary_node', sql.Int, { nullable: true });
  table.columns.add('ALAND10', sql.Int, { nullable: true });
  table.columns.add('AWATER10', sql.Int, { nullable: true });
  table.columns.add('COUNTYFP10', sql.VarChar(100), { nullable: true });
  table.columns.add('CD', sql.VarChar(10), { nullable: true });
  table.columns.add('POP10', sql.Int, { nullable: true });
  table.columns.add('CD113', sql.VarChar(10), { nullable: true });
  table.columns.add('id', sql.VarChar(100), { nullable: true });

  for (i = 0; i < count; i++) {
      let count2 = json[i]['nodes'].length

      for(let j = 0; j < count2; j++) {
          table.rows.add(json[i]['graph'][0][1],  json[i]['nodes'][j]['boundary_node'],  json[i]['nodes'][j]['ALAND10'], json[i]['nodes'][j]['AWATER10'], json[i]['nodes'][j]['NAME10'], json[i]['nodes'][j]['COUNTYFP10'], json[i]['nodes'][j]['CD'], json[i]['nodes'][j]['POP10'], json[i]['nodes'][j]['CD113'], json[i]['nodes'][j]['id']);
      }
    }

    console.log('Pool Party');

    const request = await newPool.request();
    request.bulk(table, (err, result) => {
        if (err) {
            console.log("ERROR post bulk: " + err);
            newPool.close()
        }
        else {
            console.log("SUCCESS! " + result);
            newPool.close()
        }
    })
}
</code>```

Rest of the code

```<code>const sql = require("mssql");
const fs = require('fs')

const dbConfig = {
  server: server,
  database: database,
  user: user,
  password: password,
  port: 1433,
  options: {
    encrypt: true
  }
};

const path = 'C:\/Users\/RobbieEdwardes\/Documents\/GitHub\/vtd-adjacency-graphs\/vtd-adjacency-graphs';
const fileNames = ['queen.json', 'rook.json'];

const fullSet = [];
const adjacencySet = [];

(async function(){
let dirList =  await fs.readdirSync(path);
let filePathList = await dirFileList(dirList);
let fileList = await getFilesList(filePathList, dirList);
let readFiles = await readEachFile(fileNames, filePathList)
let final = await insertBulkNode(readFiles)
})()

function dirFileList(data){
  const returnList = [];
  let dirCount = data.length;
  for(let i = 0; i < dirCount; i++){
    returnList.push(`${path}\/${data[i]}`);
  }
  return returnList
};

function getFilesList(list, stCode){
  const returnSet = []
  let listCount = list.length;
  for(let i = 0; i < listCount; i++){
    let dataSets = {
    [i] : fs.readdirSync(`${list[i]}`)
    }
    returnSet.push(dataSets)
  }
  return returnSet
}

function readEachFile(list, path){
  let pathCount = path.length;
  for(let i = 0; i < pathCount; i++){
    for(let j = 0; j < fileNames.length; j++){
      let fileRead = fs.readFileSync(`${path[i]}\/${list[j]}`)
      let jsonParse = JSON.parse(fileRead);
      fullSet.push(jsonParse);
    }
  }
  return fullSet;
}
</code>```

答: unfortunately to fix it i`ve changed the installed version of Node to 6.11.5</p>

Body:
When I try to use bulk insert, I get a rangeError:

[ERR_OUT_OF_RANGE]: The value of "value" is out of range. It must be >= -2147483648 and <= 2147483647. Received 3912066366.

I know it is a node problem but any work around would be greatly appreciated

Here is the data set I am using https://github.com/gerrymandr/vtd-adjacency-graphs/

Insert code:

async function insertBulkNode(json) {
  const newPool = await sql.connect(dbConfig)
  let count = json.length;

  const table = new sql.Table('[nodes_shapefile]');
  table.create = true;
  table.columns.add('stCode', sql.Bit, { nullable: true });
  table.columns.add('boundary_node', sql.Int, { nullable: true });
  table.columns.add('ALAND10', sql.Int, { nullable: true });
  table.columns.add('AWATER10', sql.Int, { nullable: true });
  table.columns.add('COUNTYFP10', sql.VarChar(100), { nullable: true });
  table.columns.add('CD', sql.VarChar(10), { nullable: true });
  table.columns.add('POP10', sql.Int, { nullable: true });
  table.columns.add('CD113', sql.VarChar(10), { nullable: true });
  table.columns.add('id', sql.VarChar(100), { nullable: true });

  for (i = 0; i < count; i++) {
      let count2 = json[i]['nodes'].length

      for(let j = 0; j < count2; j++) {
          table.rows.add(json[i]['graph'][0][1],  json[i]['nodes'][j]['boundary_node'],  json[i]['nodes'][j]['ALAND10'], json[i]['nodes'][j]['AWATER10'], json[i]['nodes'][j]['NAME10'], json[i]['nodes'][j]['COUNTYFP10'], json[i]['nodes'][j]['CD'], json[i]['nodes'][j]['POP10'], json[i]['nodes'][j]['CD113'], json[i]['nodes'][j]['id']);
      }
    }

    console.log('Pool Party');

    const request = await newPool.request();
    request.bulk(table, (err, result) => {
        if (err) {
            console.log("ERROR post bulk: " + err);
            newPool.close()
        }
        else {
            console.log("SUCCESS! " + result);
            newPool.close()
        }
    })
}

Rest of the code

const sql = require("mssql");
const fs = require('fs')

const dbConfig = {
  server: server,
  database: database,
  user: user,
  password: password,
  port: 1433,
  options: {
    encrypt: true
  }
};

const path = 'C:\/Users\/RobbieEdwardes\/Documents\/GitHub\/vtd-adjacency-graphs\/vtd-adjacency-graphs';
const fileNames = ['queen.json', 'rook.json'];

const fullSet = [];
const adjacencySet = [];

(async function(){
let dirList =  await fs.readdirSync(path);
let filePathList = await dirFileList(dirList);
let fileList = await getFilesList(filePathList, dirList);
let readFiles = await readEachFile(fileNames, filePathList)
let final = await insertBulkNode(readFiles)
})()

function dirFileList(data){
  const returnList = [];
  let dirCount = data.length;
  for(let i = 0; i < dirCount; i++){
    returnList.push(`${path}\/${data[i]}`);
  }
  return returnList
};

function getFilesList(list, stCode){
  const returnSet = []
  let listCount = list.length;
  for(let i = 0; i < listCount; i++){
    let dataSets = {
    [i] : fs.readdirSync(`${list[i]}`)
    }
    returnSet.push(dataSets)
  }
  return returnSet
}

function readEachFile(list, path){
  let pathCount = path.length;
  for(let i = 0; i < pathCount; i++){
    for(let j = 0; j < fileNames.length; j++){
      let fileRead = fs.readFileSync(`${path[i]}\/${list[j]}`)
      let jsonParse = JSON.parse(fileRead);
      fullSet.push(jsonParse);
    }
  }
  return fullSet;
}


问:
Connecting two docker containers
I have two existing docker container web and db. I want to link these two container, so that they will communicate with each other. If i go with --link command means it will link web to a new image and not to the db.</p>

答:
Just to benefit people that stumble upon this question. The --link feature is now considered legacy and is a prime candidate to be deprecated by docker.
The easiest way is to use

```<code>depends_on:
</code>```

In order to do this, its recommended to first create a network like so:

```<code>docker network create <network_name>
</code>```

Then use docker-compose to spawn services that bind with each other. Look at the example below where I've bound my spring-boot app to rabbit-mq. You can clone my repo from here.

```<code>version: "3.1"
services:
  rabbitmq-container:
    image: rabbitmq:3.5.3-management
    hostname: rabbitmq-container
    ports:
      - 5673:5673
      - 5672:5672
      - 15672:15672
    networks:
      - resolute

  resolute-container:
    build: .
    ports:
      - 8080:8080
    environment:
      - spring_rabbitmq_host=rabbitmq-container
      - spring_rabbitmq_port=5672
      - spring_rabbitmq_username=guest
      - spring_rabbitmq_password=guest
      - resolute_rabbitmq_publishQueueName=resolute-run-request
      - resolute_rabbitmq_exchange=resolute
    depends_on:
      - rabbitmq-container
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - resolute

networks:
  resolute:
    external:
      name: resolute
</code>```

See how I've created a network called resolute and bound the apps to the same network. I've also given my rabbitmq-container a hostname. This is because docker now prepends the container name and that makes it difficult to bind services by name.

Title: Connecting two docker containers

Body:
Just to benefit people that stumble upon this question. The --link feature is now considered legacy and is a prime candidate to be deprecated by docker.
The easiest way is to use

depends_on:

In order to do this, its recommended to first create a network like so:

docker network create <network_name>

Then use docker-compose to spawn services that bind with each other. Look at the example below where I've bound my spring-boot app to rabbit-mq. You can clone my repo from here.

version: "3.1"
services:
  rabbitmq-container:
    image: rabbitmq:3.5.3-management
    hostname: rabbitmq-container
    ports:
      - 5673:5673
      - 5672:5672
      - 15672:15672
    networks:
      - resolute

  resolute-container:
    build: .
    ports:
      - 8080:8080
    environment:
      - spring_rabbitmq_host=rabbitmq-container
      - spring_rabbitmq_port=5672
      - spring_rabbitmq_username=guest
      - spring_rabbitmq_password=guest
      - resolute_rabbitmq_publishQueueName=resolute-run-request
      - resolute_rabbitmq_exchange=resolute
    depends_on:
      - rabbitmq-container
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - resolute

networks:
  resolute:
    external:
      name: resolute

See how I've created a network called resolute and bound the apps to the same network. I've also given my rabbitmq-container a hostname. This is because docker now prepends the container name and that makes it difficult to bind services by name.


答:
What makes this complicated is that you want to retrieve rows with the most recent timestamps for multiple ships. I think the only way to do this will be to make a request for each SHIP_ID, and populate a list with each result.

var lastLocations = new List<SimpleAisRecords>();
var shipIds = new string[] { "1", "2", "3" };

using (var scope = _scopeFactory.CreateScope())
{
    var dbContext = scope.ServiceProvider.GetRequiredService<AisRecordContext>();
    foreach (var shipId in shipIds)
    {
        // Gets records for a SHIP_ID, orders by newest to oldest, and then grabs the first (newest) record
        var query = dbContext.SimpleAisRecords
                             .Where(ship => ship.SHIP_ID == shipId)
                             .OrderderByDecending(ship => ship.TIMESTAMP)
                             .FirstOrDefault();

        if (query != null)
        {
            lastLocations.Add(query);
        }
    }
}
```

By default it will return all columns from the associated table, so you'll have the LAT, LON, SPEED, HEADING, and all the other columns you need.

Title: LINQ: Get unique rows by selected ID's based on latest time

Body:
What makes this complicated is that you want to retrieve rows with the most recent timestamps for multiple ships. I think the only way to do this will be to make a request for each SHIP_ID, and populate a list with each result.

var lastLocations = new List<SimpleAisRecords>();
var shipIds = new string[] { "1", "2", "3" };

using (var scope = _scopeFactory.CreateScope())
{
    var dbContext = scope.ServiceProvider.GetRequiredService<AisRecordContext>();
    foreach (var shipId in shipIds)
    {
        // Gets records for a SHIP_ID, orders by newest to oldest, and then grabs the first (newest) record
        var query = dbContext.SimpleAisRecords
                             .Where(ship => ship.SHIP_ID == shipId)
                             .OrderderByDecending(ship => ship.TIMESTAMP)
                             .FirstOrDefault();

        if (query != null)
        {
            lastLocations.Add(query);
        }
    }
}

By default it will return all columns from the associated table, so you'll have the LAT, LON, SPEED, HEADING, and all the other columns you need.


问:
Out-FINcodedCommand.ps1 is not recognized - executing a script in the current directory
I have seen this script https://github.com/danielbohannon/Out-FINcodedCommand/blob/master/README.md

And when I try the the example given I get the error

Out-FINcodedCommand.ps1 : The term 'Out-FINcodedCommand.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that
the path is correct and try again.
At line:1 char:1
Out-FINcodedCommand.ps1 -command "iex (iwr https://github.com/danielb ...
CategoryInfo : ObjectNotFound: (Out-FINcodedCommand.ps1:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
```

Can you help?

答:
To complement SureThing's effective solution with background information:

By design, as a security measure, PowerShell - unlike cmd.exe - does NOT invoke executables (which includes scripts) located in the current directory by file name only.

Only executables in the so-called [command-lookup] path can be invoked by file name only, namely only executables located in one of the directories listed in environment variable Path ($env:PATH). An example is findstr.exe, whose  directory, C:\WINDOWS\system32, is listed in $env:PATH.

To invoke executables that are not in the command-lookup path, you must use a file path:

The file path may be absolute (full) or relative (and may be based on variables).

Therefore, to invoke a script located in the current directory, prefix the file name with .\ (. refers to the current directory), which in the case at hand means

.\Out-FINcodedCommand.ps1. Alternatively, use ./ (PowerShell treats \ and / in paths the same).

Separately, if your executable path happens to be quoted and/or contain variable references, you must use &, the call operator for invocation, e.g.,
& ".\some program.exe"

The rationale for requiring an explicit path in order to execute a file located in the current directory is that it signals the intent unambiguously and thereby prevents accidental invocation of a (possibly maliciously placed) executable of the specified name that also happens to exist in the current directory even though invocation via $env:PATH is intended.

Body:
I have seen this script https://github.com/danielbohannon/Out-FINcodedCommand/blob/master/README.md

And when I try the the example given I get the error

Out-FINcodedCommand.ps1 : The term 'Out-FINcodedCommand.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that
the path is correct and try again.
At line:1 char:1
Out-FINcodedCommand.ps1 -command "iex (iwr https://github.com/danielb ...
CategoryInfo : ObjectNotFound: (Out-FINcodedCommand.ps1:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException

Can you help?


问:
Game's background leaving a trail or smudge trace when in motion
So I have been trying to implement a scrolling background in my game recently, however the game background when moved to the left as I move forward leaves a huge trail such as so:

The code is here down below:

```<code>#Game background
background = pg.transform.scale(pg.image.load('splash.jpg'), (WIDTH,HEIGHT))#background size being adjusted to fit a particular screen size
background_xpos = 0 #the x position of the background is at 0 of (0,0)
background_xpos2 = background.get_width() #obtains the width of the background at a certain coordinate

def UpdateGame():
    window.blit(background,(background_xpos,0))
    window.blit(background,(background_xpos,0))
    pg.display.update()

run = True  #while the game is running
while run:
    clock.tick(FPS) #determines the amount of frames per second
    background_xpos = background_xpos - 2
    background_xpos2 = background_xpos - 2

    if background_xpos < background.get_width() * -1:
        background_xpos = background.get_width()

    if background_xpos2 < background.get_width() * -1:
       background_xpos2 = background.get_width()
</code>```

答:
You have to clear the screen before.</p>

```<code>#Game background
background = pg.transform.scale(pg.image.load('splash.jpg'), (WIDTH,HEIGHT))#background size being adjusted to fit a particular screen size
background_xpos = 0 #the x position of the background is at 0 of (0,0)
background_xpos2 = background.get_width() #obtains the width of the background at a certain coordinate

def UpdateGame():
    window.blit(background,(background_xpos,0))
    window.blit(background,(background_xpos,0))
    pg.display.update()

run = True  #while the game is running
while run:
    screen.fill((0,0,0))
    clock.tick(FPS) #determines the amount of frames per second
    background_xpos = background_xpos - 2
    background_xpos2 = background_xpos - 2

    if background_xpos < background.get_width() * -1:
        background_xpos = background.get_width()

    if background_xpos2 < background.get_width() * -1:
       background_xpos2 = background.get_width()
</code>```

Body:
So I have been trying to implement a scrolling background in my game recently, however the game background when moved to the left as I move forward leaves a huge trail such as so:

The code is here down below:

#Game background
background = pg.transform.scale(pg.image.load('splash.jpg'), (WIDTH,HEIGHT))#background size being adjusted to fit a particular screen size
background_xpos = 0 #the x position of the background is at 0 of (0,0)
background_xpos2 = background.get_width() #obtains the width of the background at a certain coordinate

def UpdateGame():
    window.blit(background,(background_xpos,0))
    window.blit(background,(background_xpos,0))
    pg.display.update()

run = True  #while the game is running
while run:
    clock.tick(FPS) #determines the amount of frames per second
    background_xpos = background_xpos - 2
    background_xpos2 = background_xpos - 2

    if background_xpos < background.get_width() * -1:
        background_xpos = background.get_width()

    if background_xpos2 < background.get_width() * -1:
       background_xpos2 = background.get_width()

Body:
You have to clear the screen before.

#Game background
background = pg.transform.scale(pg.image.load('splash.jpg'), (WIDTH,HEIGHT))#background size being adjusted to fit a particular screen size
background_xpos = 0 #the x position of the background is at 0 of (0,0)
background_xpos2 = background.get_width() #obtains the width of the background at a certain coordinate

def UpdateGame():
    window.blit(background,(background_xpos,0))
    window.blit(background,(background_xpos,0))
    pg.display.update()

run = True  #while the game is running
while run:
    screen.fill((0,0,0))
    clock.tick(FPS) #determines the amount of frames per second
    background_xpos = background_xpos - 2
    background_xpos2 = background_xpos - 2

    if background_xpos < background.get_width() * -1:
        background_xpos = background.get_width()

    if background_xpos2 < background.get_width() * -1:
       background_xpos2 = background.get_width()


问:
Which does stale-while-revalidate cache strategy mean?
I am trying to implement different cache strategies using ServiceWorker. For the following strategies the way to implement is completely clear:

Cache first

Cache only

Network first

Network only

For example, while trying to implement the cache-first strategy, in the fetch hook of the service-worker I will first ask the CacheStorage (or any other) for the requested URL and then if exists respondWith it and if not respondWith the result of network request.

But for the stale-while-revalidate strategy according to this definition of the workbox, I have the following questions:

First about the mechanism itself. Does stale-while-revalidate mean that use cache until the network responses and then use the network data or just use the network response to renew your cache data for the next time?

Now if the network is cached for the next time, then what scenarios contain a real use-case of that?

And if the network response should be replaced immediately in the app, so how could it be done in a service worker? Because the hook will be resolved with the cached data and then network data could not be resolved (with respondWith).

答:
Yes, it means exactly that. The idea is simple: respond immediately from the cache, then refresh the cache in the background for the next time.

All scenarios where it is not important to always get the very latest version of the page/app =) I'm using stale-while-revalidate strategy on two different web applications, one for public transportation services and one for displaying restaurant menu information. Many sites/apps are just fine with this but of course not all.

One very important thing to note here on the #2:
You could eg. use stale-while-revalidate only for static assets. This way your html, js, css, images etc. would be cached and quickly served to the user, but the data fetched dynamically from an API could still be fresh. For some apps this works, for some others not so well. Depends completely on the app. Of course you have to remember not to change the semantics of your API if the user is running a previous version of the app etc.

Not possible in any automatic way. What you could do, however, is implement a msg channel between the Service Worker and the "regular JS code on the page" using window.postMessage API. You could listen for certain messages on the page and then, from the Service Worker, send a msg when an important change has happened and the cache has been updated. Then you could either show the user a prompt telling that the page really needs to be reloaded right now or even force reload it from JS. You would need to put this logic of determining when an important update has happened into the Service Worker of course.


问:
Django Field Error cannot resolve keyword 'is_staff
```<code>Cannot resolve keyword 'is_staff' into field. Choices are: dob, experience, id,user, user_id
</code>```

I get the above error when adding trainer as a Foreign Key to the Subscription model and then accessing any record for Subscription model from admin panel

```<code>class Subscription(models.Model):
    client = models.OneToOneField(ClientProfile, on_delete=models.CASCADE)
    trainer = models.ForeignKey(TrainerProfile, null=True, blank=True,
        on_delete=models.SET_NULL, limit_choices_to={'is_staff': True})
    plan = models.ForeignKey(Plan, on_delete=models.CASCADE)
    transaction = models.OneToOneField(PaymentHistory, on_delete=models.CASCADE)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()

class TrainerProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    dob = models.DateField(null=True)
    experience = models.PositiveIntegerField(default=0)
</code>```

答:
You are referencing the nested relationship in wrong way

```<code>class Subscription(models.Model):
    # other fields
    trainer = models.ForeignKey(TrainerProfile, null=True, blank=True,
                                on_delete=models.SET_NULL,
                                <b>limit_choices_to={'user__is_staff': True}</b>)</code>```

That is, it should be user__is_staff instead of is_staff

Body:
Cannot resolve keyword 'is_staff' into field. Choices are: dob, experience, id,user, user_id

I get the above error when adding trainer as a Foreign Key to the Subscription model and then accessing any record for Subscription model from admin panel

class Subscription(models.Model):
    client = models.OneToOneField(ClientProfile, on_delete=models.CASCADE)
    trainer = models.ForeignKey(TrainerProfile, null=True, blank=True,
        on_delete=models.SET_NULL, limit_choices_to={'is_staff': True})
    plan = models.ForeignKey(Plan, on_delete=models.CASCADE)
    transaction = models.OneToOneField(PaymentHistory, on_delete=models.CASCADE)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()

class TrainerProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    dob = models.DateField(null=True)
    experience = models.PositiveIntegerField(default=0)

Body:
You are referencing the nested relationship in wrong way

class Subscription(models.Model):
    # other fields
    trainer = models.ForeignKey(TrainerProfile, null=True, blank=True,
                                on_delete=models.SET_NULL,
                                limit_choices_to={'user__is_staff': True})

That is, it should be user__is_staff instead of is_staff


问:
How to access files in a directory given a content URI?
My question is very similar to this question.
I got content Uri of a directory using  ACTION_OPEN_DOCUMENT_TREE.
and got something like this

```<code>content://com.android.externalstorage.documents/tree/primary%3ASHAREit%2Fpictures
</code>```

as result upon selecting a directory.
Now, my problem is how can i access all the files inside the directory (and preferably subdirectories too).

答:
Here a snippet related to @CommonsWare answer.

Launch the file picker with Intent.ACTION_OPEN_DOCUMENT_TREE

```<code>startActivityForResult(
    Intent.createChooser(Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), "Choose directory"),
    IMPORT_FILE_REQUEST
)
</code>```

Receive the Uri of the selected directory from file picker

```<code>override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    when (requestCode) {
        IMPORT_FILE_REQUEST -> {
            if (resultCode != Activity.RESULT_OK) return
            val uri = data?.data ?: return
            // get children uri from the tree uri
            val childrenUri =
                DocumentsContract.buildChildDocumentsUriUsingTree(
                    uri,
                    DocumentsContract.getTreeDocumentId(uri)
                )
            // get document file from children uri
            val tree = DocumentFile.fromTreeUri(this, childrenUri)
            // get the list of the documents
            tree?.listFiles()?.forEach { doc ->
                // get the input stream of a single document
                val iss = contentResolver.openInputStream(doc.uri)
                // prepare the output stream
                val oss = FileOutputStream(File(filesDir, doc.name))
                // copy the file
                CopyFile { result ->
                    println("file copied? $result")
                }.execute(iss, oss)
            }
        }
    }
}
</code>```

Copy file with AsyncTask (feel free to use threads, coroutines..)

```<code>class CopyFile(val callback: (Boolean) -> Unit) :
    AsyncTask<Closeable, Int, Boolean>() {
    override fun doInBackground(vararg closeables: Closeable): Boolean {
        if (closeables.size != 2) throw IllegalArgumentException("two arguments required: input stream and output stream")
        try {
            (closeables[0] as InputStream).use { iss ->
                (closeables[1] as OutputStream).use { oss ->
                    iss.copyTo(oss)
                    return true
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    override fun onPostExecute(result: Boolean) {
        callback.invoke(result)
    }
}
</code>```

Body:
My question is very similar to this question.
I got content Uri of a directory using  ACTION_OPEN_DOCUMENT_TREE.
and got something like this

content://com.android.externalstorage.documents/tree/primary%3ASHAREit%2Fpictures

as result upon selecting a directory.
Now, my problem is how can i access all the files inside the directory (and preferably subdirectories too).

Body:
Here a snippet related to @CommonsWare answer.

Launch the file picker with Intent.ACTION_OPEN_DOCUMENT_TREE

startActivityForResult(
    Intent.createChooser(Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), "Choose directory"),
    IMPORT_FILE_REQUEST
)

Receive the Uri of the selected directory from file picker

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    when (requestCode) {
        IMPORT_FILE_REQUEST -> {
            if (resultCode != Activity.RESULT_OK) return
            val uri = data?.data ?: return
            // get children uri from the tree uri
            val childrenUri =
                DocumentsContract.buildChildDocumentsUriUsingTree(
                    uri,
                    DocumentsContract.getTreeDocumentId(uri)
                )
            // get document file from children uri
            val tree = DocumentFile.fromTreeUri(this, childrenUri)
            // get the list of the documents
            tree?.listFiles()?.forEach { doc ->
                // get the input stream of a single document
                val iss = contentResolver.openInputStream(doc.uri)
                // prepare the output stream
                val oss = FileOutputStream(File(filesDir, doc.name))
                // copy the file
                CopyFile { result ->
                    println("file copied? $result")
                }.execute(iss, oss)
            }
        }
    }
}

Copy file with AsyncTask (feel free to use threads, coroutines..)

class CopyFile(val callback: (Boolean) -> Unit) :
    AsyncTask<Closeable, Int, Boolean>() {
    override fun doInBackground(vararg closeables: Closeable): Boolean {
        if (closeables.size != 2) throw IllegalArgumentException("two arguments required: input stream and output stream")
        try {
            (closeables[0] as InputStream).use { iss ->
                (closeables[1] as OutputStream).use { oss ->
                    iss.copyTo(oss)
                    return true
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    override fun onPostExecute(result: Boolean) {
        callback.invoke(result)
    }
}