问:
React/JSX: Iterating through an object with key/value pairs
I have an object that I'm using to keep track of which checkboxes have been checked on a UI.

It looks something like this:

```<code>      checked: { key1: false, key2: false, key3: false, key4: false, key5: false, key6: false, key7: false, key8: false, key9: false, key10: false },

</code>```

I need to be able iterate through this object and set certain values to true/false, depending on the behavior the user wants from the UI.

I have two questions:

Is there a more elegant way to instantiate this?

How would I do this using the Object.entries method?

I'd like to do something like this, but it doesn't work:

```<code>
let obj = {k1: false, k2: false, k3: false}

Object.entries(obj).forEach(([key, value]) => {
    obj.value = true;
  console.log(`${key} ${value}`);

});
</code>```

Thank you!

答:
To answer your questions -

This is a pretty good way to store multiple checked booleans, as you can access them using object destructuring like const { key1, key2, ... } = checked which, IMHO makes the code more readable.

Try -

Object.entries(obj).forEach([key, value] => {
  // Set obj key/value
  obj[key] = true  // or use value here
})
```

Refer -

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

Body:
I have an object that I'm using to keep track of which checkboxes have been checked on a UI.

It looks something like this:

      checked: { key1: false, key2: false, key3: false, key4: false, key5: false, key6: false, key7: false, key8: false, key9: false, key10: false },

I need to be able iterate through this object and set certain values to true/false, depending on the behavior the user wants from the UI.

I have two questions:

Is there a more elegant way to instantiate this?

How would I do this using the Object.entries method?

I'd like to do something like this, but it doesn't work:

let obj = {k1: false, k2: false, k3: false}

Object.entries(obj).forEach(([key, value]) => {
    obj.value = true;
  console.log(`${key} ${value}`);

});

Thank you!

Body:
To answer your questions -

This is a pretty good way to store multiple checked booleans, as you can access them using object destructuring like const { key1, key2, ... } = checked which, IMHO makes the code more readable.

Try -

Object.entries(obj).forEach([key, value] => {
  // Set obj key/value
  obj[key] = true  // or use value here
})

Refer -

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries


来源: stackexchange

问:
How to check if a url is valid (actually loads a page with content) efficiently?
QUESTION:

How to check if a url is valid and actually loads a page ?

With my current code, only the status code is checked, which means that a url like http://fsd.com/ will be considered as valid although it does not load anything.

How can I check that the url actually points to a website that can be loaded ?

CODE:

```<code>$.ajax({
                    url: link,
                    dataType: 'jsonp',
                    statusCode: {
                        200: function() {
                            console.log( "status code 200 returned");
                            validURL = true;
                        },
                        404: function() {
                            console.log( "status code 404 returned");
                            validURL = false;
                        }
                    },
                    error:function(){
                        console.log("Error");
                    }
                });
</code>```

EDIT: By valid, I mean that the page is at last partially loaded (as in at least the html & css are loaded) instead of loading forever or somehow failing without the status code being 404.

EDIT2: http://fsd.com actually returns a 404 now as it should...

EDIT3: Another example: https://dsd.com loads an empty page (status code 200) and http://dsd.com actually loads a page with content (status code 200). On my Node.js backend, the npm package "url-exists" indicates that https://dsd.com is invalid, while my frontend with the code shown in my question indicates it is a valid url. This is what the package code looks like: https://github.com/boblauer/url-exists/blob/master/index.js but I wanted to know what would be the best way according to SO users.

EDIT4:

Sadly, the request provided by Addis is apparently blocked by CORS which blocks the execution of the rest of my code while my original request did not.

```<code>$.ajax({
                    type: "HEAD",
                    url: link,
                    dataType: 'jsonp',
                }).done(function(message,text,response){
                    const size = response.getResponseHeader('Content-Length');
                    const status = response.status;
                    console.log("SIZE: "+size);
                    console.log("STATUS: "+status);
                    if(size > 0 && status == "200") {
                        $("#submitErrorMessage").css("display","none");
                        $('#directoryForm').submit();
                    }
                    else {
                        $("#submitErrorMessage").css("display","block");
                        $("#submitLoading").css("display","none");
                    }
                });
</code>```

EDIT 5:

To be more precise, both requests trigger a warning message in the browser console indicating that the response has been blocked because of CORS but my original code is actually executed in its entirety while the the other request doesn't get to the console.log().

EDIT 6:

```<code>$.ajax({
                    async: true,
                    url: link,
                    dataType: 'jsonp',
                    success: function( data, status, jqxhr ){
                        console.log( "Response data received: ", data );
                        console.log("Response data length: ", data.length);
                        console.log("Response status code: ", status);
                        if (status == "200" && data.length > 0) {
                            $("#submitErrorMessage").css("display","none");
                            $('#directoryForm').submit();
                        }
                        else {
                            $("#submitErrorMessage").css("display","block");
                            $("#submitLoading").css("display","none");
                        }

                    },
                    error:function(jqXHR, textStatus, errorThrown){
                        console.log("Error: ", errorThrown);
                    }
                });
</code>```

Error:

```<code>Error:  Error: jQuery34108117853955031047_1582059896271 was not called
    at Function.error (jquery.js:2)
    at e.converters.script json (jquery.js:2)
    at jquery.js:2
    at l (jquery.js:2)
    at HTMLScriptElement.i (jquery.js:2)
    at HTMLScriptElement.dispatch (jquery.js:2)
    at HTMLScriptElement.v.handle (jquery.js:2)
</code>```

答:
What you are trying to accomplish is not very specific, I'm not going to give you a code example on how to do this but here are some pointers.

There are different ways you could get a response: the status code is not tied to the response you get, you could have a 200 response and have no data, or have a 500 error with some data, this could be an html page showing the error or a json object, or even a string specifying what went wrong.

when you say "actually loads a page", I guess you are referring to an html response, you can check for the Content-Type header on your response headers and look for text/html and also check for Content-Length header to check if there is content in your response, and even if you check for those things it's hard to tell if the html actually displays any content.

It really depends on what are you looking specifically, my suggestion is check the Content-Type header and Content-Length and it also depends on the implementation of the website as every one might have different ways of implementing the HTTP protocol.

ContentLicense: CC BY-SA 4.0

Body:
QUESTION:

How to check if a url is valid and actually loads a page ?

With my current code, only the status code is checked, which means that a url like http://fsd.com/ will be considered as valid although it does not load anything.

How can I check that the url actually points to a website that can be loaded ?

CODE:

$.ajax({
                    url: link,
                    dataType: 'jsonp',
                    statusCode: {
                        200: function() {
                            console.log( "status code 200 returned");
                            validURL = true;
                        },
                        404: function() {
                            console.log( "status code 404 returned");
                            validURL = false;
                        }
                    },
                    error:function(){
                        console.log("Error");
                    }
                });

EDIT: By valid, I mean that the page is at last partially loaded (as in at least the html & css are loaded) instead of loading forever or somehow failing without the status code being 404.

EDIT2: http://fsd.com actually returns a 404 now as it should...

EDIT3: Another example: https://dsd.com loads an empty page (status code 200) and http://dsd.com actually loads a page with content (status code 200). On my Node.js backend, the npm package "url-exists" indicates that https://dsd.com is invalid, while my frontend with the code shown in my question indicates it is a valid url. This is what the package code looks like: https://github.com/boblauer/url-exists/blob/master/index.js but I wanted to know what would be the best way according to SO users.

EDIT4:

Sadly, the request provided by Addis is apparently blocked by CORS which blocks the execution of the rest of my code while my original request did not.

$.ajax({
                    type: "HEAD",
                    url: link,
                    dataType: 'jsonp',
                }).done(function(message,text,response){
                    const size = response.getResponseHeader('Content-Length');
                    const status = response.status;
                    console.log("SIZE: "+size);
                    console.log("STATUS: "+status);
                    if(size > 0 && status == "200") {
                        $("#submitErrorMessage").css("display","none");
                        $('#directoryForm').submit();
                    }
                    else {
                        $("#submitErrorMessage").css("display","block");
                        $("#submitLoading").css("display","none");
                    }
                });

EDIT 5:

To be more precise, both requests trigger a warning message in the browser console indicating that the response has been blocked because of CORS but my original code is actually executed in its entirety while the the other request doesn't get to the console.log().

EDIT 6:

$.ajax({
                    async: true,
                    url: link,
                    dataType: 'jsonp',
                    success: function( data, status, jqxhr ){
                        console.log( "Response data received: ", data );
                        console.log("Response data length: ", data.length);
                        console.log("Response status code: ", status);
                        if (status == "200" && data.length > 0) {
                            $("#submitErrorMessage").css("display","none");
                            $('#directoryForm').submit();
                        }
                        else {
                            $("#submitErrorMessage").css("display","block");
                            $("#submitLoading").css("display","none");
                        }

                    },
                    error:function(jqXHR, textStatus, errorThrown){
                        console.log("Error: ", errorThrown);
                    }
                });

Error:

Error:  Error: jQuery34108117853955031047_1582059896271 was not called
    at Function.error (jquery.js:2)
    at e.converters.script json (jquery.js:2)
    at jquery.js:2
    at l (jquery.js:2)
    at HTMLScriptElement.i (jquery.js:2)
    at HTMLScriptElement.dispatch (jquery.js:2)
    at HTMLScriptElement.v.handle (jquery.js:2)


问:
JavaScript - Math.random() - parameter
What changes adding the parameter in Math.random()?

For example:

```<code>Math.random() == Math.random(1234)
</code>```

答:
No official parameters. Have a look here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/random

The confusion is understandable. Several sites have a seed in the function since it came from C / Java. More information about this ignored parameter here: Math.random() - Not random

If you want a better random number get one from here http://www.random.org/clients/http/ - you will need to wrap it in some server based client - see here for more information Cross domain ajax request from javascript file without help of server side code

UPDATE: Emailed the creator of random.org - he replied he is working on a jsonp implementation...

ContentLicense: CC BY-SA 3.0

Title: JavaScript - Math.random() - parameter

Body:
What changes adding the parameter in Math.random()?

For example:

Math.random() == Math.random(1234)


问:
Can threading.Event be used to protect variable access in Python?
I have a relatively simple scenario in some Python code where I have two threads, one of which sets a value and the other is waiting for it to be set. My instinct was to reach for threading.Condition to implement this but I got wondering whether I could simply use threading.Event instead.

So, I have something like this:

```<code>value = None
readyToRead = threading.Event()

def set():
    # executes in thread 1
    global value
    value = computeValue()
    readyToRead.set()

def get():
    # executes in thread 2
    readyToRead.wait()
    useValue(value)
</code>```

I suppose I am uneasy because access to value is not actually mutex protected and I think in some languages at least it might not be safe simply to rely on the ordering implied by the statements in the code.

Is this a valid use of Event in Python?

答:
yes this is the valid use-case of event..
value is thread protected.
if you increase the number of thread you have to wait in all thread .if that is the case you can use semaphore to.</p>

Body:
I have a relatively simple scenario in some Python code where I have two threads, one of which sets a value and the other is waiting for it to be set. My instinct was to reach for threading.Condition to implement this but I got wondering whether I could simply use threading.Event instead.

So, I have something like this:

value = None
readyToRead = threading.Event()

def set():
    # executes in thread 1
    global value
    value = computeValue()
    readyToRead.set()

def get():
    # executes in thread 2
    readyToRead.wait()
    useValue(value)

I suppose I am uneasy because access to value is not actually mutex protected and I think in some languages at least it might not be safe simply to rely on the ordering implied by the statements in the code.

Is this a valid use of Event in Python?


问:
Cleaning dataframe columns for classification
I'm trying to remove all the irrelevant information from one of my columns in my dataframe but have no idea how to do it. I import the data from a CSV file straight to my dataframe.

```<code>0       [{"id": 28, "name": "Action"}, {"id": 12, "nam...

1       [{"id": 12, "name": "Adventure"}, {"id": 14, "...
2       [{"id": 28, "name": "Action"}, {"id": 12, "nam...

4798    [{"id": 28, "name": "Action"}, {"id": 80, "nam...
4799    [{"id": 35, "name": "Comedy"}, {"id": 10749, "...
4800    [{"id": 35, "name": "Comedy"}, {"id": 18, "nam...
</code>```

I would like to just have the genre e.g action, comedy, adventure. If anyone could help me or send me in the correct direction to do this it would be greatly appreciated.

答:
I am assuming you have loaded the data into a dataframe df and it has a column called name which represents the genre. You may then do the following to filter the required genres -

```<code>req_genres = ['action', 'comedy', 'adventure']
filtered_df = df[df['name'].isin(req_genres)].reset_index(drop=True)
</code>```

The above will give you only rows where the genre is 'action', 'comedy' or 'adventure'.

Body:
I'm trying to remove all the irrelevant information from one of my columns in my dataframe but have no idea how to do it. I import the data from a CSV file straight to my dataframe.

0       [{"id": 28, "name": "Action"}, {"id": 12, "nam...

1       [{"id": 12, "name": "Adventure"}, {"id": 14, "...
2       [{"id": 28, "name": "Action"}, {"id": 12, "nam...

4798    [{"id": 28, "name": "Action"}, {"id": 80, "nam...
4799    [{"id": 35, "name": "Comedy"}, {"id": 10749, "...
4800    [{"id": 35, "name": "Comedy"}, {"id": 18, "nam...

I would like to just have the genre e.g action, comedy, adventure. If anyone could help me or send me in the correct direction to do this it would be greatly appreciated.

Body:
I am assuming you have loaded the data into a dataframe df and it has a column called name which represents the genre. You may then do the following to filter the required genres -

req_genres = ['action', 'comedy', 'adventure']
filtered_df = df[df['name'].isin(req_genres)].reset_index(drop=True)

The above will give you only rows where the genre is 'action', 'comedy' or 'adventure'.


问:
Why does Cython expect 0 dimensions?
I have boiled my problem down to a small reproducible test case:

In file 1 (custom_cython.pyx) file I have the following:

```<code>import numpy as np
cimport numpy as np
cimport cython

ctypedef np.uint8_t DTYPE_B_t
ctypedef np.uint16_t CELL_ID_t
ctypedef np.int64_t DTYPE_INT64_t

cdef struct LOOKUPMEM_t:
    DTYPE_B_t filled_flag
    CELL_ID_t key_i
    CELL_ID_t key_j
    CELL_ID_t key_k
    DTYPE_INT64_t offset
    DTYPE_INT64_t num_elements

cdef LOOKUPMEM_t[:] lookup_memory

my_dtype = [("filled_flag", np.uint8, 1),
            ("ijk", np.uint16, 3),
            ("offset_and_num", np.int64, 2)]

input_numpy_dtype = np.dtype(my_dtype, align=True)

lookup_memory = np.zeros(5000, dtype=input_numpy_dtype)
</code>```

In file 2 (custom_cython_test.py) I have the following:

```<code>from custom_cython import lookup_memory

print(lookup_memory)
</code>```

When I run python custom_cython_test.py I end up with ValueError: Expected 0 dimension(s), got 1 on the line lookup_memory = np.zeros(5000, dtype=input_numpy_dtype)

In my struct definition I have tried using cdef packed struct LOOKUPMEM_t and align=False in the dtype creation, and that yields the same error.

I'm on Python 3.7.3 with Cython version 0.29.12 and Numpy 1.16.4.

I have successfully assigned cython memoryviews to 1-D numpy arrays before, so I am baffled as to why my apparent 1d cdef LOOKUPMEM_t[:] lookup_memory is expecting 0 dimensions.  Can anyone tell me what's going on?

答:
The problem seems to be this part of your struct:

```<code>    CELL_ID_t key_i
    CELL_ID_t key_j
    CELL_ID_t key_k
</code>```

combined with this part of your dtype:

```<code>("ijk", np.uint16, 3)
</code>```

and similarly for your combined offset_and_num field.

The issue is that when the memoryview interface sees a tuple-like field like ("ijk", np.uint16, 3) it wants to unpack it into a 1-D array of 3 elements, but the next key in the struct is just CELL_ID_t key_i, a 0-D scalar.

If I change your struct to more closely match the Numpy dtype it works:

```<code>cdef struct LOOKUPMEM_t:
    DTYPE_B_t filled_flag
    CELL_ID_t ijk[3]
    DTYPE_INT64_t offset_num_elements[2]
</code>```

So you have a few choices for how to proceed.  If you really want to keep your struct the same way, you can do so, and format the dtype differently.  Since it's easy to view Numpy arrays with different dtypes you could also use a different dtype for the memoryview initialization, if you want to keep the existing dtype format for other use cases.

Title: Why does Cython expect 0 dimensions?

Body:
I have boiled my problem down to a small reproducible test case:

In file 1 (custom_cython.pyx) file I have the following:

import numpy as np
cimport numpy as np
cimport cython

ctypedef np.uint8_t DTYPE_B_t
ctypedef np.uint16_t CELL_ID_t
ctypedef np.int64_t DTYPE_INT64_t

cdef struct LOOKUPMEM_t:
    DTYPE_B_t filled_flag
    CELL_ID_t key_i
    CELL_ID_t key_j
    CELL_ID_t key_k
    DTYPE_INT64_t offset
    DTYPE_INT64_t num_elements

cdef LOOKUPMEM_t[:] lookup_memory

my_dtype = [("filled_flag", np.uint8, 1),
            ("ijk", np.uint16, 3),
            ("offset_and_num", np.int64, 2)]

input_numpy_dtype = np.dtype(my_dtype, align=True)

lookup_memory = np.zeros(5000, dtype=input_numpy_dtype)

In file 2 (custom_cython_test.py) I have the following:

from custom_cython import lookup_memory

print(lookup_memory)

When I run python custom_cython_test.py I end up with ValueError: Expected 0 dimension(s), got 1 on the line lookup_memory = np.zeros(5000, dtype=input_numpy_dtype)

In my struct definition I have tried using cdef packed struct LOOKUPMEM_t and align=False in the dtype creation, and that yields the same error.

I'm on Python 3.7.3 with Cython version 0.29.12 and Numpy 1.16.4.

I have successfully assigned cython memoryviews to 1-D numpy arrays before, so I am baffled as to why my apparent 1d cdef LOOKUPMEM_t[:] lookup_memory is expecting 0 dimensions.  Can anyone tell me what's going on?

Body:
The problem seems to be this part of your struct:

    CELL_ID_t key_i
    CELL_ID_t key_j
    CELL_ID_t key_k

combined with this part of your dtype:

("ijk", np.uint16, 3)

and similarly for your combined offset_and_num field.

The issue is that when the memoryview interface sees a tuple-like field like ("ijk", np.uint16, 3) it wants to unpack it into a 1-D array of 3 elements, but the next key in the struct is just CELL_ID_t key_i, a 0-D scalar.

If I change your struct to more closely match the Numpy dtype it works:

cdef struct LOOKUPMEM_t:
    DTYPE_B_t filled_flag
    CELL_ID_t ijk[3]
    DTYPE_INT64_t offset_num_elements[2]

So you have a few choices for how to proceed.  If you really want to keep your struct the same way, you can do so, and format the dtype differently.  Since it's easy to view Numpy arrays with different dtypes you could also use a different dtype for the memoryview initialization, if you want to keep the existing dtype format for other use cases.


问:
While making Plot in pandas python I am getting Error: EOL
df

```<code>import pandas as pd
df = pd.DataFrame({
    'SKU': ['BIKES','MATS', 'BLANKETS', 'CREAMS'],'Col1': [1,3,6,10],'Col2': [4,5, 6,16],'Month':[1,2,3,4],'Year':['2015','2010','2011','2012']})
</code>```

Code tried which doesnt work

```<code>    import matplotlib.pyplot as plt

#Visualisations

fig, ax = plt.subplots()
for col in ['Col1', 'Col2']:
    ax.plot(df[col], label=col)
ax.legend(loc="best")
ax.tick_params(axis="x", rotation=30)
</code>```

I need tickers for months and values for years. But before I can proceed with tickers etc., I am getting error:

```<code>File "<ipython-input-14-136f7818ccc7>", line 6
    for col in ['Col1', 'Col2']:
                                ^
SyntaxError: EOL while scanning string literal
</code>```

答:
```<code>for col in ['Col1', 'Col2']:
    ax.plot(df['Date'], df[col], label=col)
</code>```

Body:
df

import pandas as pd
df = pd.DataFrame({
    'SKU': ['BIKES','MATS', 'BLANKETS', 'CREAMS'],'Col1': [1,3,6,10],'Col2': [4,5, 6,16],'Month':[1,2,3,4],'Year':['2015','2010','2011','2012']})

Code tried which doesnt work

    import matplotlib.pyplot as plt

#Visualisations

fig, ax = plt.subplots()
for col in ['Col1', 'Col2']:
    ax.plot(df[col], label=col)
ax.legend(loc="best")
ax.tick_params(axis="x", rotation=30)

I need tickers for months and values for years. But before I can proceed with tickers etc., I am getting error:

File "<ipython-input-14-136f7818ccc7>", line 6
    for col in ['Col1', 'Col2']:
                                ^
SyntaxError: EOL while scanning string literal


问:
How can I validate US Social Security Number?
Anyone out there know how to improve this function? I'm not worried about shortening the code, I'm sure this could be done with better regex, I am more concerned about correct logic. I have had a terrible time finding documentation for SSN #'s. Most of the rules I use below have come from other programmers who work in the credit industry (no sources cited).

Are there any additional rules that
you are aware of?

Do you know if any of this is wrong?

Can you site your sources?

```<code>public static bool isSSN(string ssn)
{
    Regex rxBadSSN = new Regex(@"(\d)\1\1\1\1\1\1\1\1");

    //Must be 9 bytes
    if(ssn.Trim().Length != 9)
        return false;

    //Must be numeric
    if(!isNumeric(ssn))
        return false;

    //Must be less than 772999999
    if( (Int32)Double.Parse(ssn.Substring(0,3)) > 772 )
    {
        //Check for Green Card Temp SSN holders
        // Could be 900700000
        //          900800000
        if(ssn.Substring(0,1) != "9")
            return false;

        if(ssn.Substring(3,1) != "7" && ssn.Substring(3,1) != "8")
            return false;
    }

    //Obviously Fake!
    if(ssn == "123456789")
        return false;

    //Try again!
    if(ssn == "123121234")
        return false;

    //No single group can have all zeros
    if(ssn.Substring(0,3) == "000")
        return false;
    if(ssn.Substring(3,2) == "00")
        return false;
    if(ssn.Substring(5,4) == "0000")
        return false;

    //Check to make sure the SSN number is not repeating
    if (rxBadSSN.IsMatch(ssn))
        return false;

    return true;
}
</code>```

Body:
Anyone out there know how to improve this function? I'm not worried about shortening the code, I'm sure this could be done with better regex, I am more concerned about correct logic. I have had a terrible time finding documentation for SSN #'s. Most of the rules I use below have come from other programmers who work in the credit industry (no sources cited).

Are there any additional rules that
you are aware of?

Do you know if any of this is wrong?

Can you site your sources?

public static bool isSSN(string ssn)
{
    Regex rxBadSSN = new Regex(@"(\d)\1\1\1\1\1\1\1\1");

    //Must be 9 bytes
    if(ssn.Trim().Length != 9)
        return false;

    //Must be numeric
    if(!isNumeric(ssn))
        return false;

    //Must be less than 772999999
    if( (Int32)Double.Parse(ssn.Substring(0,3)) > 772 )
    {
        //Check for Green Card Temp SSN holders
        // Could be 900700000
        //          900800000
        if(ssn.Substring(0,1) != "9")
            return false;

        if(ssn.Substring(3,1) != "7" && ssn.Substring(3,1) != "8")
            return false;
    }

    //Obviously Fake!
    if(ssn == "123456789")
        return false;

    //Try again!
    if(ssn == "123121234")
        return false;

    //No single group can have all zeros
    if(ssn.Substring(0,3) == "000")
        return false;
    if(ssn.Substring(3,2) == "00")
        return false;
    if(ssn.Substring(5,4) == "0000")
        return false;

    //Check to make sure the SSN number is not repeating
    if (rxBadSSN.IsMatch(ssn))
        return false;

    return true;
}

Body:
In Hive, the SSN validation or ITIN validation is like:

select case when '078051120' rlike '^(?!1{9}|2{9}|3{9}|4{9}|5{9}|6{9}|7{9}|8{9}|9{9}|219099999|078051120|123456789)(?!666|000|9[0-9]{2})[0-9]{3}(?!00)[0-9]{2}(?!0{4})[0-9]{4}$' then 'SSN'
     when '078051120' rlike '^9[0-9]{2}(7[0-9]|80|81|82|83|84|85|86|87|88)[0-9]{4}$' then 'ITIN'
     else 'INVLD'
end as ssn_flg;

change the dummy '078051120' to column name of your table.


问:
Select positive (or negative) values in GNU Octave
I have a single column matrix of un-ordered signed values; e.g.:

```<code>values = [ -3; 0; 2; 3; -1; -2; 1 ];
</code>```

How can I separate this into a positive and a negative matrix? i.e.,

```<code>values_neg = [ -3; -1; -2 ];
values_pos = [ 2; 3; 1 ];
</code>```

I have tried looking for a way to specify an index; but, my GNU Octave-fu is quite limited at this point. I can find how to index entire rows or columns. Is there a way to specify constraints?

答:
Use logical indexing:</p>

```<code>values_pos = values(values > 0);
</code>```

Body:
I have a single column matrix of un-ordered signed values; e.g.:

values = [ -3; 0; 2; 3; -1; -2; 1 ];

How can I separate this into a positive and a negative matrix? i.e.,

values_neg = [ -3; -1; -2 ];
values_pos = [ 2; 3; 1 ];

I have tried looking for a way to specify an index; but, my GNU Octave-fu is quite limited at this point. I can find how to index entire rows or columns. Is there a way to specify constraints?

Body:
Use logical indexing:

values_pos = values(values > 0);


问:
Persistent volume and claims in kubernetes workspace
I have been working in k8's workspace in for like 6 months now and always wonder why do we need a Persistent Volume(PV) and Persistent Volume claim(PVC)? Can anyone please make this concept clear to me?</p>

答:
These information are from Kubernetes In Action book.

Decoupling pods from the underlying storage technology

Ideally,  a  developer  deploying  their  apps  on  Kubernetes  should  never  have  to know what kind of storage technology is used underneath, the same way they don’t have to know what type of physical servers are being used to run their pods.

When a developer needs a certain amount of persistent storage for their application, they can request it from Kubernetes, the same way they can request CPU, memory, and other resources when creating a pod. The system administrator can configure the cluster so it can give the apps what they request.

Introducing PersistentVolumes and PersistentVolumeClaims

To enable apps to request storage in a Kubernetes cluster without having to deal with infrastructure  specifics,  two  new  resources  were  introduced.  They  are  PersistentVolumes and PersistentVolumeClaims.

Instead  of  the  developer  adding  a  technology-specific  volume  to  their  pod,  it’s  the cluster  administrator  who  sets  up  the  underlying  storage  and  then  registers  it  in Kubernetes  by  creating  a  PersistentVolume  resource  through  the  Kubernetes  API server. When creating the PersistentVolume, the admin specifies its size and the access modes it supports.

```<code>This image is from Kubernetes In Action book, too.
</code>```

When a cluster user needs to use persistent storage in one of their pods, they first create a PersistentVolumeClaim manifest, specifying the minimum size and the access mode they require. The user then submits the PersistentVolumeClaim manifest to the Kubernetes  API  server,  and  Kubernetes  finds  the  appropriate  PersistentVolume  and binds the volume to the claim.

The PersistentVolumeClaim can then be used as one of the volumes inside a pod.Other users can not use the same PersistentVolume until it has been released by deleting the bound PersistentVolumeClaim.

Body:
These information are from Kubernetes In Action book.

Decoupling pods from the underlying storage technology

Ideally,  a  developer  deploying  their  apps  on  Kubernetes  should  never  have  to know what kind of storage technology is used underneath, the same way they don’t have to know what type of physical servers are being used to run their pods.

When a developer needs a certain amount of persistent storage for their application, they can request it from Kubernetes, the same way they can request CPU, memory, and other resources when creating a pod. The system administrator can configure the cluster so it can give the apps what they request.

Introducing PersistentVolumes and PersistentVolumeClaims

To enable apps to request storage in a Kubernetes cluster without having to deal with infrastructure  specifics,  two  new  resources  were  introduced.  They  are  PersistentVolumes and PersistentVolumeClaims.

Instead  of  the  developer  adding  a  technology-specific  volume  to  their  pod,  it’s  the cluster  administrator  who  sets  up  the  underlying  storage  and  then  registers  it  in Kubernetes  by  creating  a  PersistentVolume  resource  through  the  Kubernetes  API server. When creating the PersistentVolume, the admin specifies its size and the access modes it supports.

This image is from Kubernetes In Action book, too.

When a cluster user needs to use persistent storage in one of their pods, they first create a PersistentVolumeClaim manifest, specifying the minimum size and the access mode they require. The user then submits the PersistentVolumeClaim manifest to the Kubernetes  API  server,  and  Kubernetes  finds  the  appropriate  PersistentVolume  and binds the volume to the claim.

The PersistentVolumeClaim can then be used as one of the volumes inside a pod.Other users can not use the same PersistentVolume until it has been released by deleting the bound PersistentVolumeClaim.


问:
Variable being overwritten in ForEach loop - Powershell
I am trying to run the below code to import a list of directories from a .csv file (if chosen as a parameter) and then do a foreach loop to determined what files have been modified in the last XX days in the directories listed in the .csv file.

However it appears that the $modfiles variable is overwritten in each iteration as opposed to being appended.

The .csv being imported has 3 directories in it but my output only shows the relevant files from the last directory imported.  Is their away to append $modfiles?

CSV Contents:

```<code>$importDirs = Import-Csv $importCsv
foreach ($importDir in $importDirs){
            $modfiles = Get-ChildItem -Path $importDir.Directory -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $modDate }
}
</code>```

Output:

Here is the entire script:

```<code>[CmdletBinding(DefaultParameterSetName='ManualDirectory')]
param (
    [Parameter(Mandatory = $true, ParameterSetName = 'ManualDirectory')]
    [System.String]
    $dir,

    [Parameter(Mandatory = $true, ParameterSetName = 'ImportDirectory')]
    [System.String]
    $importCsv,

    [Parameter(Mandatory=$true)]
    [int]$days,

    [switch]$exportCsv,

    [switch]$console

)

Process {
    #Clear-Host
    $totSize = 0
    $totFiles = 0
    $modDate = (Get-date).AddDays(-$days).Date
    If ($dir){
        $modfiles = Get-ChildItem -Path $dir -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $modDate }
    }
    If ($importCsv){
        $importDirs = Import-Csv $importCsv
        foreach ($importDir in $importDirs){
            $modfiles = Get-ChildItem -Path $importDir.Directory -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $modDate }
       }
    }
    If ($exportCsv){
        $modfiles | Select-Object -Property FullName, Length,LastWriteTime | Export-Csv -Path .\modFiles.csv -NoTypeInformation
    }
    foreach ($file in $modfiles){
        $totFiles = $totFiles + 1
        $totSize = $totSize + $file.Length
        If ($console -eq $true){
            Write-Host $file.FullName
        }
    }

    If ($totSize -lt 1MB){
        $outSize = $totSize/1KB
        $unit = "KB"
    }
    elseif (($totSize -ge 1MB) -and ($totSize -lt 1GB)){
        $outSize = $totSize/1MB
        $unit = "MB"
    }
    elseif ($totSize -ge 1GB){
        $outSize = $totSize/1GB
        $unit = "GB"
    }

    $outRound = [math]::Round($outSize,2)
    Write-Host $totFiles "Files"
    Write-Host $outRound $unit
}
</code>```

答:
```<code>$importDirs = Import-Csv $importCsv
foreach ($importDir in $importDirs){
            $modfiles = Get-ChildItem -Path $importDir.Directory -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $modDate }
}
</code>```

Each time you loop through $importDirs, you assign a value to $modfiles. Each iteration, you overwrite the values from previous iteration.

If you need to keep a list of all the values, append the data by using

```<code>$modfiles = $modfiles, (Get-Childitem ...)

# or
$modfiles += Get-ChildItem ...
</code>```

Body:
I am trying to run the below code to import a list of directories from a .csv file (if chosen as a parameter) and then do a foreach loop to determined what files have been modified in the last XX days in the directories listed in the .csv file.

However it appears that the $modfiles variable is overwritten in each iteration as opposed to being appended.

The .csv being imported has 3 directories in it but my output only shows the relevant files from the last directory imported.  Is their away to append $modfiles?

CSV Contents:

$importDirs = Import-Csv $importCsv
foreach ($importDir in $importDirs){
            $modfiles = Get-ChildItem -Path $importDir.Directory -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $modDate }
}

Output:

Here is the entire script:

[CmdletBinding(DefaultParameterSetName='ManualDirectory')]
param (
    [Parameter(Mandatory = $true, ParameterSetName = 'ManualDirectory')]
    [System.String]
    $dir,

    [Parameter(Mandatory = $true, ParameterSetName = 'ImportDirectory')]
    [System.String]
    $importCsv,

    [Parameter(Mandatory=$true)]
    [int]$days,

    [switch]$exportCsv,

    [switch]$console

)

Process {
    #Clear-Host
    $totSize = 0
    $totFiles = 0
    $modDate = (Get-date).AddDays(-$days).Date
    If ($dir){
        $modfiles = Get-ChildItem -Path $dir -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $modDate }
    }
    If ($importCsv){
        $importDirs = Import-Csv $importCsv
        foreach ($importDir in $importDirs){
            $modfiles = Get-ChildItem -Path $importDir.Directory -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $modDate }
       }
    }
    If ($exportCsv){
        $modfiles | Select-Object -Property FullName, Length,LastWriteTime | Export-Csv -Path .\modFiles.csv -NoTypeInformation
    }
    foreach ($file in $modfiles){
        $totFiles = $totFiles + 1
        $totSize = $totSize + $file.Length
        If ($console -eq $true){
            Write-Host $file.FullName
        }
    }

    If ($totSize -lt 1MB){
        $outSize = $totSize/1KB
        $unit = "KB"
    }
    elseif (($totSize -ge 1MB) -and ($totSize -lt 1GB)){
        $outSize = $totSize/1MB
        $unit = "MB"
    }
    elseif ($totSize -ge 1GB){
        $outSize = $totSize/1GB
        $unit = "GB"
    }

    $outRound = [math]::Round($outSize,2)
    Write-Host $totFiles "Files"
    Write-Host $outRound $unit
}

Body:
$importDirs = Import-Csv $importCsv
foreach ($importDir in $importDirs){
            $modfiles = Get-ChildItem -Path $importDir.Directory -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $modDate }
}

Each time you loop through $importDirs, you assign a value to $modfiles. Each iteration, you overwrite the values from previous iteration.

If you need to keep a list of all the values, append the data by using

$modfiles = $modfiles, (Get-Childitem ...)

# or
$modfiles += Get-ChildItem ...


问:
Can not find element in XPath with Selenium driver
Every time I load up python script the Xpath for the Facebook login button and popup button has to changed manually every time before I login. How do I change this so that I dont have to enter the XPath manually?

```<code>from selenium import webdriver
from time import sleep

class TinderBot():
    def __init__(self):
        self.driver = webdriver.Chrome()

    def login(self):
        self.driver.get('https://tinder.com')

        sleep(3)

        #fbxpath = //button[@type = 'button' and @aria-label = 'Log in with Facebook']

        fb_btn = self.driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
        fb_btn.click()

        base_window = self.driver.window_handles[0]
        self.driver.switch_to_window(self.driver.window_handles[1])
        fbemail = self.driver.find_element_by_xpath('//*[@id="email"]')
        fbemail.send_keys('ara_98425@yahoo.com')

        fbpass = self.driver.find_element_by_xpath('//*[@id="pass"]')
        fbpass.send_keys('BlackToast5')

        login_btn = self.driver.find_element_by_xpath('//*[@id="u_0_0"]')
        login_btn.click()

        self.driver.switch_to_window(base_window)

        popup1 = self.driver.find_element_by_xpath('//*[@id="modal-manager"]/div/div/div/div/div[3]/button[1]')
        popup1.click()

        popup2 = self.driver.find_element_by_xpath('//*[@id="modal-manager"]/div/div/div/div/div[3]/button[1]')
        popup2.click()

bot = TinderBot()
bot.login()
</code>```

This is the error I get:

```<code>File "tind_bt.py", line 16, in login
    fb_btn = self.driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button"}
  (Session info: chrome=80.0.3987.106)
</code>```

答:
Maybe you can implement the WebDriverWait and see if that takes care of the problem.

This might get you started - Selenium WebDriver wait till element is displayed

Here's the Python documentation for explicit waits - https://selenium-python.readthedocs.io/waits.html

Body:
Every time I load up python script the Xpath for the Facebook login button and popup button has to changed manually every time before I login. How do I change this so that I dont have to enter the XPath manually?

from selenium import webdriver
from time import sleep

class TinderBot():
    def __init__(self):
        self.driver = webdriver.Chrome()

    def login(self):
        self.driver.get('https://tinder.com')

        sleep(3)

        #fbxpath = //button[@type = 'button' and @aria-label = 'Log in with Facebook']

        fb_btn = self.driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
        fb_btn.click()

        base_window = self.driver.window_handles[0]
        self.driver.switch_to_window(self.driver.window_handles[1])
        fbemail = self.driver.find_element_by_xpath('//*[@id="email"]')
        fbemail.send_keys('ara_98425@yahoo.com')

        fbpass = self.driver.find_element_by_xpath('//*[@id="pass"]')
        fbpass.send_keys('BlackToast5')

        login_btn = self.driver.find_element_by_xpath('//*[@id="u_0_0"]')
        login_btn.click()

        self.driver.switch_to_window(base_window)

        popup1 = self.driver.find_element_by_xpath('//*[@id="modal-manager"]/div/div/div/div/div[3]/button[1]')
        popup1.click()

        popup2 = self.driver.find_element_by_xpath('//*[@id="modal-manager"]/div/div/div/div/div[3]/button[1]')
        popup2.click()

bot = TinderBot()
bot.login()

This is the error I get:

File "tind_bt.py", line 16, in login
    fb_btn = self.driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button"}
  (Session info: chrome=80.0.3987.106)


问:
Regular expression - repeat groups
I have text:

```<code><b>Title1:</b><br/><b>Title2:</b> Value1<br/><b>Title3:</b> Value2<br/><b>Title4:</b> Value3<br/>Value4<b>Title5:</b> Value5<br/>
</code>```

What regex to get:

```<code>[0] => <b>Title1:</b><br/>
[1] => <b>Title2:</b> Value1<br/>
[2] => <b>Title3:</b> Value2<br/>
[3] => <b>Title4:</b> Value3<br/>Value4
[4] => <b>Title5:</b> Value5<br/>
</code>```

My variant not working:
(.*?)(.*?)

答:
You can use preg_split() with a lookahead:

```<code><?php
$split = preg_split( '/(?=<b>Title\d+:)/', '<b>Title1:</b><br/><b>Title2:</b> Value1<br/><b>Title3:</b> Value2<br/><b>Title4:</b> Value3<br/>Value4<b>Title5:</b> Value5<br/>' );
array_shift( $split );
var_dump( $split );
</code>```

Output:

```<code>array(5) {
  [0]=>
  string(19) "<b>Title1:</b><br/>"
  [1]=>
  string(26) "<b>Title2:</b> Value1<br/>"
  [2]=>
  string(26) "<b>Title3:</b> Value2<br/>"
  [3]=>
  string(32) "<b>Title4:</b> Value3<br/>Value4"
  [4]=>
  string(26) "<b>Title5:</b> Value5<br/>"
}
</code>```

Your regex was close, you need:

```<code><b>(.*?)<\/b>(.*?)(?=<b>|$)
</code>```

https://regex101.com/r/dk67IK/1

Title: Regular expression - repeat groups

Body:
I have text:

Title1:
Title2: Value1
Title3: Value2
Title4: Value3
Value4Title5: Value5

What regex to get:

[0] => Title1:

[1] => Title2: Value1

[2] => Title3: Value2

[3] => Title4: Value3
Value4
[4] => Title5: Value5

My variant not working:
(.*?)(.*?)

Body:
You can use preg_split() with a lookahead:

<?php
$split = preg_split( '/(?=<b>Title\d+:)/', '<b>Title1:</b><br/><b>Title2:</b> Value1<br/><b>Title3:</b> Value2<br/><b>Title4:</b> Value3<br/>Value4<b>Title5:</b> Value5<br/>' );
array_shift( $split );
var_dump( $split );

Output:

array(5) {
  [0]=>
  string(19) "<b>Title1:</b><br/>"
  [1]=>
  string(26) "<b>Title2:</b> Value1<br/>"
  [2]=>
  string(26) "<b>Title3:</b> Value2<br/>"
  [3]=>
  string(32) "<b>Title4:</b> Value3<br/>Value4"
  [4]=>
  string(26) "<b>Title5:</b> Value5<br/>"
}

Your regex was close, you need:

<b>(.*?)<\/b>(.*?)(?=<b>|$)

https://regex101.com/r/dk67IK/1


问:
NgRx Effects loaded twice
I have a simple feature module, which provides services and imports its own stats fragment as a feature:

```<code>@NgModule({
  imports: [
    CommonModule,
    StoreModule.forFeature('alarms', alarmsReducer, { initialState: alarmsInitialState }),
    EffectsModule.forFeature([AlarmsEffects])
  ],
  declarations: [],
  providers: [
    AlarmsFacade,
    AlarmsService
  ]
})
export class AlarmsModule {
}
</code>```

And I'm importing this module in two other modules, a page that needs to have access to the services, and AppModule as I need to import it here in order to have the state initialized properly.

When I'm checking Redux DevTools, I can clearly see that @ngrx/store/update-reducers is called twice for the feature alarms. This results in the state being frozen, stoping all the effects (including the ones not related to alarms feature).

The timeline below can show the issue (a third @ngrx/store/update-reducers action is fired after the effects init while there's only two features at the moment, it contains reducers for feature 'alarms'):

How can I avoid the effects to be loaded twice? I tried to remove the module from AppModule but it's breaking the alarms state as there's no default one provided for my selectors.

答:
You'll also get effects firing twice if you subscribe to the effect observable itself, like by composing effects out of other effects (rather than the actions they dispatch).

You can solve it by subscribing to the actions the effects dispatch, or you can also use shareReplay() or other similar "hot" observable solution.

Title: NgRx Effects loaded twice

Body:
I have a simple feature module, which provides services and imports its own stats fragment as a feature:

@NgModule({
  imports: [
    CommonModule,
    StoreModule.forFeature('alarms', alarmsReducer, { initialState: alarmsInitialState }),
    EffectsModule.forFeature([AlarmsEffects])
  ],
  declarations: [],
  providers: [
    AlarmsFacade,
    AlarmsService
  ]
})
export class AlarmsModule {
}

And I'm importing this module in two other modules, a page that needs to have access to the services, and AppModule as I need to import it here in order to have the state initialized properly.

When I'm checking Redux DevTools, I can clearly see that @ngrx/store/update-reducers is called twice for the feature alarms. This results in the state being frozen, stoping all the effects (including the ones not related to alarms feature).

The timeline below can show the issue (a third @ngrx/store/update-reducers action is fired after the effects init while there's only two features at the moment, it contains reducers for feature 'alarms'):

How can I avoid the effects to be loaded twice? I tried to remove the module from AppModule but it's breaking the alarms state as there's no default one provided for my selectors.


问:
Perplexing pandas index change after left merge
I have a data frame and I am interested in a particular row. When I run

questionnaire_events[questionnaire_events['event_id'].eq(6506308)]
```

<p>I get the row, and its index is 7,816. I then merge questionnaire_events with another data frame</p>

<pre class="lang-python prettyprint-override"><code>merged = questionnaire_events.merge(
    ordinals,
    how='left',
    left_on='event_id',
    right_on='id')
</code>```

(It is worth noting that the ordinals data frame has no NaNs and no duplicated ids, but questionnaire_events does have some rows with NaN values for event_id.)

merged[merged['event_id'].eq(6506308)]
```

The resulting row has index 7,581. Why? What has happened in the merge, a left outer merge, to mean that my row has moved from 7,816 to 7,581? If there were multiple rows with the same id in the ordinals data frame then I can see how the merged data frame would have more rows than the left data frame in the merge, but that is not the case, so why has the row moved?

(N.B. Sorry I cannot give a crisp code sample. When I try to produce test data the row index change does not happen, it is only happening on my real data.)

答:
pd.DataFrame.merge does not preserve the original datafame indexes.

```<code>df1 = pd.DataFrame({'key':[*'ABCDE'], 'val':[1,2,3,4,5]}, index=[100,200,300,400,500])

print('df1 dataframe:')
print(df1)
print('\n')
df2 = pd.DataFrame({'key':[*'AZCWE'], 'val':[10,20,30,40,50]}, index=[*'abcde'])
print('df2 dataframe:')
print(df2)
print('\n')

df_m = df1.merge(df2, on='key', how='left')
print('df_m dataframe:')
print(df_m)
</code>```

Now, if your df1 is the default range index, then it is possible that you could get different index in your merged dataframe. If you subset or filter your df1, then your indexing will not match.

Work Around:

```<code>df1 = df1.reset_index()
df_m2 = df1.merge(df2, on='key', how='left')
df_m2 = df_m2.set_index('index')
print('df_m2 work around dataframe:')
print(df_m2)
</code>```

Output:

```<code>df_m2 work around dataframe:
      key  val_x  val_y
index
100     A      1   10.0
200     B      2    NaN
300     C      3   30.0
400     D      4    NaN
500     E      5   50.0
</code>```

Body:
I have a data frame and I am interested in a particular row. When I run

questionnaire_events[questionnaire_events['event_id'].eq(6506308)]

I get the row, and its index is 7,816. I then merge questionnaire_events with another data frame

merged = questionnaire_events.merge(
    ordinals,
    how='left',
    left_on='event_id',
    right_on='id')

(It is worth noting that the ordinals data frame has no NaNs and no duplicated ids, but questionnaire_events does have some rows with NaN values for event_id.)

merged[merged['event_id'].eq(6506308)]

The resulting row has index 7,581. Why? What has happened in the merge, a left outer merge, to mean that my row has moved from 7,816 to 7,581? If there were multiple rows with the same id in the ordinals data frame then I can see how the merged data frame would have more rows than the left data frame in the merge, but that is not the case, so why has the row moved?

(N.B. Sorry I cannot give a crisp code sample. When I try to produce test data the row index change does not happen, it is only happening on my real data.)

Body:
pd.DataFrame.merge does not preserve the original datafame indexes.

df1 = pd.DataFrame({'key':[*'ABCDE'], 'val':[1,2,3,4,5]}, index=[100,200,300,400,500])

print('df1 dataframe:')
print(df1)
print('\n')
df2 = pd.DataFrame({'key':[*'AZCWE'], 'val':[10,20,30,40,50]}, index=[*'abcde'])
print('df2 dataframe:')
print(df2)
print('\n')

df_m = df1.merge(df2, on='key', how='left')
print('df_m dataframe:')
print(df_m)

Now, if your df1 is the default range index, then it is possible that you could get different index in your merged dataframe. If you subset or filter your df1, then your indexing will not match.

Work Around:

df1 = df1.reset_index()
df_m2 = df1.merge(df2, on='key', how='left')
df_m2 = df_m2.set_index('index')
print('df_m2 work around dataframe:')
print(df_m2)

Output:

df_m2 work around dataframe:
      key  val_x  val_y
index
100     A      1   10.0
200     B      2    NaN
300     C      3   30.0
400     D      4    NaN
500     E      5   50.0


问:
This AWS CLI bash script works, but how to limit it to delete only log files on top level?
I don't write many bash scripts, especially with AWS, so could use some help.

A results of turning-on AWS S3 Logging with Versioning at the same time and it ended-up generating over 70,000 log files (joy), then trying to deleting only sets a delete marker file.

The bash shell script below works great to delete all versions of S3 objects/files and delete markers in the given bucket.

Problem is, I need to keep the 25 GB of real/legit files, but need to delete the 70,000 versioned log files with delete markers that got mixed-in.

This doesn't do anything anymore, because they're versioned and already have delete markers:

aws s3 rm s3://MYBUCKETNAME/ --exclude "" --include "2020-"

Don't think any kind of LifeCycle expiration stuff will work, because valid files are mixed-in with valid files, before and afterwards, only thing I can think of that could work is to delete files that begin with "2020-" (BUT, only on the top level, NOT recursively.)

So, I'm looking to modify script below to only delete files that begin with "2020-" (BUT, only on the top level), which is all the logs files that accidentally got created on the S3 container.

2020-01-25-07-33-44-DAC13CC9B600B11A

2020-01-25-07-33-46-59F44EEC6F93E8C7

2020-01-25-07-33-47-786A5091CABF6912

...etc...

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

    #USAGE: sh delete_all_object_versions.sh MYBUCKETNAME

    bucket=$1

    set -e

    echo "Removing all versions from $bucket"

    versions=`aws s3api list-object-versions --bucket $bucket |jq '.Versions'`
    markers=`aws s3api list-object-versions --bucket $bucket |jq '.DeleteMarkers'`

    echo "removing files"
    for version in $(echo "${versions}" | jq -r '.[] | @base64'); do
        version=$(echo ${version} | base64 --decode)

        key=`echo $version | jq -r .Key`
        versionId=`echo $version | jq -r .VersionId `
        cmd="aws s3api delete-object --bucket $bucket --key $key --version-id $versionId"
        echo $cmd
        $cmd
    done

    echo "removing delete markers"
    for marker in $(echo "${markers}" | jq -r '.[] | @base64'); do
        marker=$(echo ${marker} | base64 --decode)

        key=`echo $marker | jq -r .Key`
        versionId=`echo $marker | jq -r .VersionId `
        cmd="aws s3api delete-object --bucket $bucket --key $key --version-id $versionId"
        echo $cmd
        $cmd
    done
</code>```

答:
I found a solution to deleted the 85,000 log files that were deleted (with delete markers), after reading John's helpful comment, using a prefix to ensure that the objects start with '2020-'.

This removed only the log files starting with '2020-' on the root level of the S3 container and did NOT go into subfolders like 'mp3/2020-', so it left the valid files that started with year dates.

```<code>versions=`aws s3api list-object-versions --bucket $bucket --prefix '2020-' |jq '.Versions'`
markers=`aws s3api list-object-versions --bucket $bucket --prefix '2020-' |jq '.DeleteMarkers'`
</code>```

Below, is the final bash script that I ran on my Mac with AWS CLI S3API...

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

#USAGE: sh delete_all_object_versions.sh BUCKETNAME
#TEST:  aws s3api list-objects --bucket BUCKETNAME --prefix '2020-'

bucket=$1

set -e

echo "Removing all versions from $bucket"

versions=`aws s3api list-object-versions --bucket $bucket --prefix '2020-' |jq '.Versions'`
markers=`aws s3api list-object-versions --bucket $bucket --prefix '2020-' |jq '.DeleteMarkers'`

echo "removing files"
for version in $(echo "${versions}" | jq -r '.[] | @base64'); do
    version=$(echo ${version} | base64 --decode)

    key=`echo $version | jq -r .Key`
    versionId=`echo $version | jq -r .VersionId `
    cmd="aws s3api delete-object --bucket $bucket --key $key --version-id $versionId"
    echo $cmd
    $cmd
done

echo "removing delete markers"
for marker in $(echo "${markers}" | jq -r '.[] | @base64'); do
    marker=$(echo ${marker} | base64 --decode)

    key=`echo $marker | jq -r .Key`
    versionId=`echo $marker | jq -r .VersionId `
    cmd="aws s3api delete-object --bucket $bucket --key $key --version-id $versionId"
    echo $cmd
    $cmd
done
</code>```

Body:
I don't write many bash scripts, especially with AWS, so could use some help.

A results of turning-on AWS S3 Logging with Versioning at the same time and it ended-up generating over 70,000 log files (joy), then trying to deleting only sets a delete marker file.

The bash shell script below works great to delete all versions of S3 objects/files and delete markers in the given bucket.

Problem is, I need to keep the 25 GB of real/legit files, but need to delete the 70,000 versioned log files with delete markers that got mixed-in.

This doesn't do anything anymore, because they're versioned and already have delete markers:

aws s3 rm s3://MYBUCKETNAME/ --exclude "" --include "2020-"

Don't think any kind of LifeCycle expiration stuff will work, because valid files are mixed-in with valid files, before and afterwards, only thing I can think of that could work is to delete files that begin with "2020-" (BUT, only on the top level, NOT recursively.)

So, I'm looking to modify script below to only delete files that begin with "2020-" (BUT, only on the top level), which is all the logs files that accidentally got created on the S3 container.

2020-01-25-07-33-44-DAC13CC9B600B11A

2020-01-25-07-33-46-59F44EEC6F93E8C7

2020-01-25-07-33-47-786A5091CABF6912

...etc...

    #!/bin/bash

    #USAGE: sh delete_all_object_versions.sh MYBUCKETNAME

    bucket=$1

    set -e

    echo "Removing all versions from $bucket"

    versions=`aws s3api list-object-versions --bucket $bucket |jq '.Versions'`
    markers=`aws s3api list-object-versions --bucket $bucket |jq '.DeleteMarkers'`

    echo "removing files"
    for version in $(echo "${versions}" | jq -r '.[] | @base64'); do
        version=$(echo ${version} | base64 --decode)

        key=`echo $version | jq -r .Key`
        versionId=`echo $version | jq -r .VersionId `
        cmd="aws s3api delete-object --bucket $bucket --key $key --version-id $versionId"
        echo $cmd
        $cmd
    done

    echo "removing delete markers"
    for marker in $(echo "${markers}" | jq -r '.[] | @base64'); do
        marker=$(echo ${marker} | base64 --decode)

        key=`echo $marker | jq -r .Key`
        versionId=`echo $marker | jq -r .VersionId `
        cmd="aws s3api delete-object --bucket $bucket --key $key --version-id $versionId"
        echo $cmd
        $cmd
    done

Body:
I found a solution to deleted the 85,000 log files that were deleted (with delete markers), after reading John's helpful comment, using a prefix to ensure that the objects start with '2020-'.

This removed only the log files starting with '2020-' on the root level of the S3 container and did NOT go into subfolders like 'mp3/2020-', so it left the valid files that started with year dates.

versions=`aws s3api list-object-versions --bucket $bucket --prefix '2020-' |jq '.Versions'`
markers=`aws s3api list-object-versions --bucket $bucket --prefix '2020-' |jq '.DeleteMarkers'`

Below, is the final bash script that I ran on my Mac with AWS CLI S3API...

#!/bin/bash

#USAGE: sh delete_all_object_versions.sh BUCKETNAME
#TEST:  aws s3api list-objects --bucket BUCKETNAME --prefix '2020-'

bucket=$1

set -e

echo "Removing all versions from $bucket"

versions=`aws s3api list-object-versions --bucket $bucket --prefix '2020-' |jq '.Versions'`
markers=`aws s3api list-object-versions --bucket $bucket --prefix '2020-' |jq '.DeleteMarkers'`

echo "removing files"
for version in $(echo "${versions}" | jq -r '.[] | @base64'); do
    version=$(echo ${version} | base64 --decode)

    key=`echo $version | jq -r .Key`
    versionId=`echo $version | jq -r .VersionId `
    cmd="aws s3api delete-object --bucket $bucket --key $key --version-id $versionId"
    echo $cmd
    $cmd
done

echo "removing delete markers"
for marker in $(echo "${markers}" | jq -r '.[] | @base64'); do
    marker=$(echo ${marker} | base64 --decode)

    key=`echo $marker | jq -r .Key`
    versionId=`echo $marker | jq -r .VersionId `
    cmd="aws s3api delete-object --bucket $bucket --key $key --version-id $versionId"
    echo $cmd
    $cmd
done


问:
How to read the post request parameters using JavaScript
I am trying to read the post request parameters from my HTML. I can read the get request parameters using the following code in JavaScript.

```<code>$wnd.location.search
</code>```

But it does not work for post request. Can anyone tell me how to read the post request parameter values in my HTML using JavaScript?

答:
I have a simple code to make it:

In your index.php :

```<code><input id="first_post_data" type="hidden" value="<?= $_POST['first_param']; ?>"/>
</code>```

In your main.js :

```<code>let my_first_post_param = $("#first_post_data").val();
</code>```

So when you will include main.js in index.php () you could get the value of your hidden input which contains your post data.

Body:
I am trying to read the post request parameters from my HTML. I can read the get request parameters using the following code in JavaScript.

$wnd.location.search

But it does not work for post request. Can anyone tell me how to read the post request parameter values in my HTML using JavaScript?

Body:
I have a simple code to make it:

In your index.php :

<input id="first_post_data" type="hidden" value="<?= $_POST['first_param']; ?>"/>

In your main.js :

let my_first_post_param = $("#first_post_data").val();

So when you will include main.js in index.php (<script type="text/javascript" src="./main.js"></script>) you could get the value of your hidden input which contains your post data.


问:
FosUser Override Roles attribute
I would like to override the roles attribute of FOSUser in Symfony (3.4), because I want a user to have only one role.

By default, this attribute is an array, I want to override it to a string.
So I guess I have to override the default __construct() function? Because there is this line below in it :

```<code>$this->roles = array();
</code>```

From what I found on Google, I maybe must to use an annotation like this one below on my user entity, can you confirm?

```<code>/*
 *  @AttributeOverrides({
 *      @AttributeOverride(name="roles",
 *          column=@Column(
 *              name     = "roles",
 *              type     = "string",
 *              length   = 30
 *  ))})
 */
</code>```

Do I have the right idea? How could I override the constructor or get this working?

答: Now, it's impossible, you can override only name, but you can't change type</p>

Title: FosUser Override Roles attribute

Body:
I would like to override the roles attribute of FOSUser in Symfony (3.4), because I want a user to have only one role.

By default, this attribute is an array, I want to override it to a string.
So I guess I have to override the default __construct() function? Because there is this line below in it :

$this->roles = array();

From what I found on Google, I maybe must to use an annotation like this one below on my user entity, can you confirm?

/*
 *  @AttributeOverrides({
 *      @AttributeOverride(name="roles",
 *          column=@Column(
 *              name     = "roles",
 *              type     = "string",
 *              length   = 30
 *  ))})
 */

Do I have the right idea? How could I override the constructor or get this working?


问:
When do we have Push down code in SAP Hana
I need an answer or better an explanation between SQL (Open/Native), CDS and AMDP.

I understand that in order to follow the rules of SAP and push the code down in DB HANA we must use CDS or/and AMDP. I have thought that if we still use only SQL Queries is the old way with which we use the Code in AS. I have read some articles or I have seen some videos in the internet and they have confused me.

Can someone explain me which is the best way for following the PUSH DOWN the CODE in HANA?

```<code> - Use of SQL queries Open or Native what it is?
 - Use of CDS or AMDP are PUSH DOWN CODE technique for Hana.
</code>```

Thanks

答:
“Code push down” means that you execute the expensive main part of your computations in the database, not in the application code (= ABAP). How exactly you do that is related only indirectly.

For example, instead of SELECTing from two database tables and then mixing up rows in the ABAP code, pushing down means you run a JOIN in the database. This reduces the number of round trips with the database, exploits the columnar characteristics of SAP HANA, and benefits from other code-near-the-data effects, such as reduced amount of data that needs to be transported to the upper layer.

Whether you implement that as an OpenSQL query, a classical database view, a CDS view, or inside an AMDP, is not the main question. We found that resorting to simple OpenSQL queries on CDS views is the optimal choice for standard cases. When things get more complicated, my answer to this other question may give some more guidance when to use what.

Disclaimer: although I am working for SAP, there are many other opinions on the topic and some may look at this differently, so kindly do not understand this as a reliable official guidance that fits everything.

Note that code push down is not a silver bullet that fixes everything. In scenarios with many parallel consumers, pushing down large operations may clog up the database for the other users, such that you need to resort to different patterns, or find ways to limit resource consumption.

Body:
I need an answer or better an explanation between SQL (Open/Native), CDS and AMDP.

I understand that in order to follow the rules of SAP and push the code down in DB HANA we must use CDS or/and AMDP. I have thought that if we still use only SQL Queries is the old way with which we use the Code in AS. I have read some articles or I have seen some videos in the internet and they have confused me.

Can someone explain me which is the best way for following the PUSH DOWN the CODE in HANA?

 - Use of SQL queries Open or Native what it is?
 - Use of CDS or AMDP are PUSH DOWN CODE technique for Hana.

Thanks


问:
AutoFixture can't create immutable object
I'm coding some tests using AutoFixture in C# 8.0 but I'm had been problems, I guess, about class inheritance.

For example, I have these classes:

```<code>public class Parent
{
  public Guid Id {get; protected set;}
  public int Age {get; protected set;}

  public void SetAge(int age)
  {
      this.Age = age;
  }
}
</code>```

and the child class

```<code>public class Child: Parent
{
  public string name {get; private set;}

  private Child() //I Need this because Migrations
  {
  }
}
</code>```

After it, I use AutoFixture to test my class:

```<code> var fixture = new Fixture();
 fixture.Behaviors.Remove(new ThrowingRecursionBehavior());
 fixture.Behaviors
                    .OfType<ThrowingRecursionBehavior>()
                    .ToList()
                    .ForEach(b => fixture.Behaviors.Remove(b));
fixture.Register<Parent>(() => null);

fixture.Behaviors.Add(new OmitOnRecursionBehavior(recursionDepth: 3));

fixture.Customize<Child>(c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));

fixture.Customize<Child>(ce => ce.Do(x => x.SetAge(10));

var childObject = fixture.Create<Child>();

</code>```

and BOOM! I get an error at the moment when the fixture tries to create the object

```<code>"AutoFixture was unable to create an instance from the project, most likely because it has no public constructor, is an abstract or non-public type.
</code>```

If I change private for the public constructor in Child class, when I set the Age, the object is created Empty. If I don't use SetAge method, the object is created without problems.

Someone had this problem? Do I need to set some property in AutoFixture when I Use inheritance classes?

Thank you

答:
Alright, babe!

I found this:

Force AutoFixture to use the greediest constructor

And Gotcha! I solve my problem!

Body:
I'm coding some tests using AutoFixture in C# 8.0 but I'm had been problems, I guess, about class inheritance.

For example, I have these classes:

public class Parent
{
  public Guid Id {get; protected set;}
  public int Age {get; protected set;}

  public void SetAge(int age)
  {
      this.Age = age;
  }
}

and the child class

public class Child: Parent
{
  public string name {get; private set;}

  private Child() //I Need this because Migrations
  {
  }
}

After it, I use AutoFixture to test my class:

 var fixture = new Fixture();
 fixture.Behaviors.Remove(new ThrowingRecursionBehavior());
 fixture.Behaviors
                    .OfType<ThrowingRecursionBehavior>()
                    .ToList()
                    .ForEach(b => fixture.Behaviors.Remove(b));
fixture.Register<Parent>(() => null);

fixture.Behaviors.Add(new OmitOnRecursionBehavior(recursionDepth: 3));

fixture.Customize<Child>(c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));

fixture.Customize<Child>(ce => ce.Do(x => x.SetAge(10));

var childObject = fixture.Create<Child>();

and BOOM! I get an error at the moment when the fixture tries to create the object

"AutoFixture was unable to create an instance from the project, most likely because it has no public constructor, is an abstract or non-public type.

If I change private for the public constructor in Child class, when I set the Age, the object is created Empty. If I don't use SetAge method, the object is created without problems.

Someone had this problem? Do I need to set some property in AutoFixture when I Use inheritance classes?

Thank you


问:
AWS transcribe: How to give a path to output folder
I am using aws transcribe to get the text of the video using node js. I can specify the particular destination bucket in params but not the particular folder. Can anyone help me with this ? This is my code

```<code>var params = {
        LanguageCode: "en-US",
        Media: { /* required */
          MediaFileUri: "s3://bucket-name/public/events/545/videoplayback1.mp4"
        },
        TranscriptionJobName: 'STRING_VALUE', /* required */
        MediaFormat: "mp4", //mp3 | mp4 | wav | flac,
        OutputBucketName: 'test-rekognition',
        // }
      };
      transcribeservice.startTranscriptionJob(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
      });
</code>```

I have specified the destination bucket name in OutputBucketName field. But how to specify a particular folder ?

答:
I would recommend creating a designated S3 Bucket for Transcribe output and adding a trigger with a lambda function to respond to that trigger on 'Object create (All)'. Essentially, as soon as there is a new object added to your S3 bucket, a lambda function is invoked to move/process that output by placing it in a specific 'folder' of your choice.

This doesn't solve the API issue but I hope it serves as a good workaround - you could look at this article ( https://linuxacademy.com/hands-on-lab/0e291fc6-52a4-4ed3-ad65-8cf2fd84e0df/  ) as a guide.

have a good one.

Body:
I am using aws transcribe to get the text of the video using node js. I can specify the particular destination bucket in params but not the particular folder. Can anyone help me with this ? This is my code

var params = {
        LanguageCode: "en-US",
        Media: { /* required */
          MediaFileUri: "s3://bucket-name/public/events/545/videoplayback1.mp4"
        },
        TranscriptionJobName: 'STRING_VALUE', /* required */
        MediaFormat: "mp4", //mp3 | mp4 | wav | flac,
        OutputBucketName: 'test-rekognition',
        // }
      };
      transcribeservice.startTranscriptionJob(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
      });

I have specified the destination bucket name in OutputBucketName field. But how to specify a particular folder ?


问:
Zwift : Add resistance with FTMS control point
I try to build a smart home trainer.
at this moment, it is connected with Zwift with the Fitness Machine Service.
I can send to zwift Power and Cadence and i can play.
Now i try to add the control point (one of the characteristics included in FTMS)
But i cannot finish the transaction described in the specifications.
I think it's not very easy.
The xml file which describe the control point is empty!
There is no complete sequence diagram of flow chart.
at this moment, i can receive a write event from zwift into the control point.
First zwift send 0x7 and then 0x0
After that...again write 7 into the control point and then 0
I try to answer (indicate) 0x80, 0x801, 0x0180 for the 2 bytes needed (cf specification)
I think i don't really understand the specification
Have you somme informations to help me?
any flow chart, sequence diagram for the resistance level update?
do you confirme i juste need to indicate 2 bytes to answer to a write from zwift into the control point?

@Yonkee

答:
I agree that it's a bit weird that the Control Point XML is empty, since the FTMS specification PDF doesn't give a complete list of e.g. what to expect as parameters to the various commands.

However, I've managed to create an implementation that supports some of the use cases you might be interested in. It currently supports setting target resistance and power, but not the simulation parameters you will need for the regular simulation mode in Zwift. You can find this implementation on github. It's not complete, and I just handle a few commands there, but you can grasp the concept from the code.

I wrote this using the information I could find online, basically the Fitness Machine Specification and the different GATT Characteristics Specifications.

When an app like Zwift write to the CP it's an OP Code optionally followed by parameters, you should reply with the OP Code 0x80 (Response Code) followed by the OP Code that the reply is for, optionally followed by parameters.

In the case of OP Code 0x00 (Request Control) you should therefore reply with: 0x80, 0x00, 0x01. The last 0x01 is the result code for "Success".

In the case of OP Code 0x07 (Start/Resume), you should reply with: 0x80, 0x07, 0x01. Assuming you consider the request successful, the other possible responses are detailed in Table 4.24 of the FTMS Specification PDF.

On command you should look into is OP Code 0x11. When running Zwift in "non-workout mode", so just the normal mode, Zwift will use OP Code 0x11 followed by a set of simulation parameters: Wind Speed, Grade, Rolling resistance coeff and Wind resistance coeff. See the "4.16.2.18 Set Indoor Bike Simulation Parameters Procedure" of the PDF for details on the format of those parameters.

I hope this enables you to proceed. Happy hacking!


问:
Hibernate @OneToOne does not map id
I'm trying to write simple world generator using kotlin, springboot and hibernate, and I have many relations in Entities but one of them not working. Program generate the Countries and cities, but in DB I have a null ID for 'capital'

Entities:

```<code>@Entity
data class City(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id:Long? = null,
        val name: String,
        @OneToMany
        @JoinColumn(name="CityId")
        val flats: List<Flat>)

@Entity
data class Country(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id:Long? = null,
        val name: String,
        @OneToOne(fetch = FetchType.EAGER)
        val capital: City,
        @OneToMany
        @JoinColumn(name="CountryId")
        val cities: List<City>)
</code>```

Country generator:

```<code>@Component
class CountryGenerator @Autowired constructor(val util: Util) {

    fun getRandomCountry(): Country = util.getObj(
            Country(null,
                    gen.country().name(),
                    CityGenerator(util).getRandomCapital(),
                    getCities())
    ) as Country

    private fun getCities(): List<City> =
            IntStream.range(0, rnd.nextInt(MAX_CITIES_NUMBER!!) + MIN_CITIES_NUMBER!!)
                    .mapToObj { CityGenerator(util).getRandomCity() }
                    .toList()
}
</code>```

City generator:

```<code>@Component
class CityGenerator @Autowired constructor(val util: Util) {

    fun getRandomCity() = util.getObj(
            City(null,
                    getCityName(),
                    getListOfFlats())
    ) as City

    fun getRandomCapital() = util.getObj(
            City(null,
                    getCapital(),
                    getListOfFlats())
    ) as City

    private fun getListOfFlats(): List<Flat> =
            IntStream.range(0, rnd.nextInt(MAX_FLATS_NUMBER!!) + MIN_FLATS_NUMBER!!)
                    .mapToObj { FlatGenerator(util).getFlat() }
                    .toList()

    private fun getCapital() = gen.country().capital()

    private fun getCityName(): String = gen.address().city()
}
</code>```

Any ideas what is wrong with it?

EDIT:

Saving to DB:

```<code>@Component
class Util(private val personRepository: PersonRepository,
           private val flatRepository: FlatRepository,
           private val cityRepository: CityRepository,
           private val countryRepository: CountryRepository,
           private val worldRepository: WorldRepository) {

    fun getObj(obj: Any): Any {
        return when (obj) {
            is Person -> this.personRepository.save(obj)
            is Flat -> flatRepository.save(obj)
            is City -> cityRepository.save(obj)
            is Country -> countryRepository.save(obj)
            is World -> worldRepository.save(obj)
            else -> throw IllegalArgumentException("Wrong object");
        }
    }
</code>```

EDIT2:

Method Util.getObj() returns correct objects:

答:
Confirm that the method Util.getObj is really saving the new city and returning a city with an id.

Also, if this relationship is required, you should use the property optional = false in the OneToOne annotation:

```<code>@OneToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name="capital_id", nullable=false)
val capital: City
</code>```

Title: Hibernate @OneToOne does not map id

Body:
I'm trying to write simple world generator using kotlin, springboot and hibernate, and I have many relations in Entities but one of them not working. Program generate the Countries and cities, but in DB I have a null ID for 'capital'

Entities:

@Entity
data class City(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id:Long? = null,
        val name: String,
        @OneToMany
        @JoinColumn(name="CityId")
        val flats: List<Flat>)

@Entity
data class Country(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id:Long? = null,
        val name: String,
        @OneToOne(fetch = FetchType.EAGER)
        val capital: City,
        @OneToMany
        @JoinColumn(name="CountryId")
        val cities: List<City>)

Country generator:

@Component
class CountryGenerator @Autowired constructor(val util: Util) {

    fun getRandomCountry(): Country = util.getObj(
            Country(null,
                    gen.country().name(),
                    CityGenerator(util).getRandomCapital(),
                    getCities())
    ) as Country

    private fun getCities(): List<City> =
            IntStream.range(0, rnd.nextInt(MAX_CITIES_NUMBER!!) + MIN_CITIES_NUMBER!!)
                    .mapToObj { CityGenerator(util).getRandomCity() }
                    .toList()
}

City generator:

@Component
class CityGenerator @Autowired constructor(val util: Util) {

    fun getRandomCity() = util.getObj(
            City(null,
                    getCityName(),
                    getListOfFlats())
    ) as City

    fun getRandomCapital() = util.getObj(
            City(null,
                    getCapital(),
                    getListOfFlats())
    ) as City

    private fun getListOfFlats(): List<Flat> =
            IntStream.range(0, rnd.nextInt(MAX_FLATS_NUMBER!!) + MIN_FLATS_NUMBER!!)
                    .mapToObj { FlatGenerator(util).getFlat() }
                    .toList()

    private fun getCapital() = gen.country().capital()

    private fun getCityName(): String = gen.address().city()
}

Any ideas what is wrong with it?

EDIT:

Saving to DB:

@Component
class Util(private val personRepository: PersonRepository,
           private val flatRepository: FlatRepository,
           private val cityRepository: CityRepository,
           private val countryRepository: CountryRepository,
           private val worldRepository: WorldRepository) {

    fun getObj(obj: Any): Any {
        return when (obj) {
            is Person -> this.personRepository.save(obj)
            is Flat -> flatRepository.save(obj)
            is City -> cityRepository.save(obj)
            is Country -> countryRepository.save(obj)
            is World -> worldRepository.save(obj)
            else -> throw IllegalArgumentException("Wrong object");
        }
    }

EDIT2:

Method Util.getObj() returns correct objects:

Body:
Confirm that the method Util.getObj is really saving the new city and returning a city with an id.

Also, if this relationship is required, you should use the property optional = false in the OneToOne annotation:

@OneToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name="capital_id", nullable=false)
val capital: City


问:
How to check if a url is valid (actually loads a page with content) efficiently?
QUESTION:

How to check if a url is valid and actually loads a page ?

With my current code, only the status code is checked, which means that a url like http://fsd.com/ will be considered as valid although it does not load anything.

How can I check that the url actually points to a website that can be loaded ?

CODE:

```<code>$.ajax({
                    url: link,
                    dataType: 'jsonp',
                    statusCode: {
                        200: function() {
                            console.log( "status code 200 returned");
                            validURL = true;
                        },
                        404: function() {
                            console.log( "status code 404 returned");
                            validURL = false;
                        }
                    },
                    error:function(){
                        console.log("Error");
                    }
                });
</code>```

EDIT: By valid, I mean that the page is at last partially loaded (as in at least the html & css are loaded) instead of loading forever or somehow failing without the status code being 404.

EDIT2: http://fsd.com actually returns a 404 now as it should...

EDIT3: Another example: https://dsd.com loads an empty page (status code 200) and http://dsd.com actually loads a page with content (status code 200). On my Node.js backend, the npm package "url-exists" indicates that https://dsd.com is invalid, while my frontend with the code shown in my question indicates it is a valid url. This is what the package code looks like: https://github.com/boblauer/url-exists/blob/master/index.js but I wanted to know what would be the best way according to SO users.

EDIT4:

Sadly, the request provided by Addis is apparently blocked by CORS which blocks the execution of the rest of my code while my original request did not.

```<code>$.ajax({
                    type: "HEAD",
                    url: link,
                    dataType: 'jsonp',
                }).done(function(message,text,response){
                    const size = response.getResponseHeader('Content-Length');
                    const status = response.status;
                    console.log("SIZE: "+size);
                    console.log("STATUS: "+status);
                    if(size > 0 && status == "200") {
                        $("#submitErrorMessage").css("display","none");
                        $('#directoryForm').submit();
                    }
                    else {
                        $("#submitErrorMessage").css("display","block");
                        $("#submitLoading").css("display","none");
                    }
                });
</code>```

EDIT 5:

To be more precise, both requests trigger a warning message in the browser console indicating that the response has been blocked because of CORS but my original code is actually executed in its entirety while the the other request doesn't get to the console.log().

EDIT 6:

```<code>$.ajax({
                    async: true,
                    url: link,
                    dataType: 'jsonp',
                    success: function( data, status, jqxhr ){
                        console.log( "Response data received: ", data );
                        console.log("Response data length: ", data.length);
                        console.log("Response status code: ", status);
                        if (status == "200" && data.length > 0) {
                            $("#submitErrorMessage").css("display","none");
                            $('#directoryForm').submit();
                        }
                        else {
                            $("#submitErrorMessage").css("display","block");
                            $("#submitLoading").css("display","none");
                        }

                    },
                    error:function(jqXHR, textStatus, errorThrown){
                        console.log("Error: ", errorThrown);
                    }
                });
</code>```

Error:

```<code>Error:  Error: jQuery34108117853955031047_1582059896271 was not called
    at Function.error (jquery.js:2)
    at e.converters.script json (jquery.js:2)
    at jquery.js:2
    at l (jquery.js:2)
    at HTMLScriptElement.i (jquery.js:2)
    at HTMLScriptElement.dispatch (jquery.js:2)
    at HTMLScriptElement.v.handle (jquery.js:2)
</code>```

答:
The HEAD request is used to get meta-information contained in the HTTP headers. The good thing is that the response doesn't contain the body. It's pretty speedy and there shouldn't be any heavy processing going on in the server to handle it. This makes it handy for quick status checking.

The HEAD method is identical to GET except that the server MUST NOT
  return a message-body in the response. The metainformation
  contained in the HTTP headers in response to a HEAD request SHOULD be
  identical to the information sent in response to a GET request. This
  method can be used for obtaining metainformation about the entity
  implied by the request without transferring the entity-body itself.
  This method is often used for testing hypertext links for validity,
  accessibility, and recent modification.
   www.w3.org

```<code>$.ajax({
    type: "HEAD",
    async: true,
    url: link,
    dataType: 'json',
}).done(function(message,text,response){
    const size = response.getResponseHeader('Content-Length');

    //optionally you may check for the status code to know if the request has been successfully completed
    const status = response.status;
});
</code>```

Content-Length is one of the meta-data available in the head request which gives the size of the body in bytes, so by checking the size only without loading the whole page you could check if some content is available in the response body.
 -

EDIT:
The above code is for dataType of json. For dataType of jsonp, callback functions for success and error properties will take of the response like the following:

```<code>$.ajax({
    url: link,
    dataType: 'jsonp',
    crossDomain: true,
    data: data,
    success: function( data, status, jqxhr ){
        console.log( "Response data received: ", data );
        console.log("Response data length: ", data.length);
        console.log("Response status code: ", status);
    },
    error:function(jqXHR, textStatus, errorThrown){
        console.log("Error: ", errorThrown);
    }
}
</code>```

Body:
QUESTION:

How to check if a url is valid and actually loads a page ?

With my current code, only the status code is checked, which means that a url like http://fsd.com/ will be considered as valid although it does not load anything.

How can I check that the url actually points to a website that can be loaded ?

CODE:

$.ajax({
                    url: link,
                    dataType: 'jsonp',
                    statusCode: {
                        200: function() {
                            console.log( "status code 200 returned");
                            validURL = true;
                        },
                        404: function() {
                            console.log( "status code 404 returned");
                            validURL = false;
                        }
                    },
                    error:function(){
                        console.log("Error");
                    }
                });

EDIT: By valid, I mean that the page is at last partially loaded (as in at least the html & css are loaded) instead of loading forever or somehow failing without the status code being 404.

EDIT2: http://fsd.com actually returns a 404 now as it should...

EDIT3: Another example: https://dsd.com loads an empty page (status code 200) and http://dsd.com actually loads a page with content (status code 200). On my Node.js backend, the npm package "url-exists" indicates that https://dsd.com is invalid, while my frontend with the code shown in my question indicates it is a valid url. This is what the package code looks like: https://github.com/boblauer/url-exists/blob/master/index.js but I wanted to know what would be the best way according to SO users.

EDIT4:

Sadly, the request provided by Addis is apparently blocked by CORS which blocks the execution of the rest of my code while my original request did not.

$.ajax({
                    type: "HEAD",
                    url: link,
                    dataType: 'jsonp',
                }).done(function(message,text,response){
                    const size = response.getResponseHeader('Content-Length');
                    const status = response.status;
                    console.log("SIZE: "+size);
                    console.log("STATUS: "+status);
                    if(size > 0 && status == "200") {
                        $("#submitErrorMessage").css("display","none");
                        $('#directoryForm').submit();
                    }
                    else {
                        $("#submitErrorMessage").css("display","block");
                        $("#submitLoading").css("display","none");
                    }
                });

EDIT 5:

To be more precise, both requests trigger a warning message in the browser console indicating that the response has been blocked because of CORS but my original code is actually executed in its entirety while the the other request doesn't get to the console.log().

EDIT 6:

$.ajax({
                    async: true,
                    url: link,
                    dataType: 'jsonp',
                    success: function( data, status, jqxhr ){
                        console.log( "Response data received: ", data );
                        console.log("Response data length: ", data.length);
                        console.log("Response status code: ", status);
                        if (status == "200" && data.length > 0) {
                            $("#submitErrorMessage").css("display","none");
                            $('#directoryForm').submit();
                        }
                        else {
                            $("#submitErrorMessage").css("display","block");
                            $("#submitLoading").css("display","none");
                        }

                    },
                    error:function(jqXHR, textStatus, errorThrown){
                        console.log("Error: ", errorThrown);
                    }
                });

Error:

Error:  Error: jQuery34108117853955031047_1582059896271 was not called
    at Function.error (jquery.js:2)
    at e.converters.script json (jquery.js:2)
    at jquery.js:2
    at l (jquery.js:2)
    at HTMLScriptElement.i (jquery.js:2)
    at HTMLScriptElement.dispatch (jquery.js:2)
    at HTMLScriptElement.v.handle (jquery.js:2)

Body:
The HEAD request is used to get meta-information contained in the HTTP headers. The good thing is that the response doesn't contain the body. It's pretty speedy and there shouldn't be any heavy processing going on in the server to handle it. This makes it handy for quick status checking.

The HEAD method is identical to GET except that the server MUST NOT
  return a message-body in the response. The metainformation
  contained in the HTTP headers in response to a HEAD request SHOULD be
  identical to the information sent in response to a GET request. This
  method can be used for obtaining metainformation about the entity
  implied by the request without transferring the entity-body itself.
  This method is often used for testing hypertext links for validity,
  accessibility, and recent modification.
   www.w3.org

$.ajax({
    type: "HEAD",
    async: true,
    url: link,
    dataType: 'json',
}).done(function(message,text,response){
    const size = response.getResponseHeader('Content-Length');

    //optionally you may check for the status code to know if the request has been successfully completed
    const status = response.status;
});

Content-Length is one of the meta-data available in the head request which gives the size of the body in bytes, so by checking the size only without loading the whole page you could check if some content is available in the response body.
 -

EDIT:
The above code is for dataType of json. For dataType of jsonp, callback functions for success and error properties will take of the response like the following:

$.ajax({
    url: link,
    dataType: 'jsonp',
    crossDomain: true,
    data: data,
    success: function( data, status, jqxhr ){
        console.log( "Response data received: ", data );
        console.log("Response data length: ", data.length);
        console.log("Response status code: ", status);
    },
    error:function(jqXHR, textStatus, errorThrown){
        console.log("Error: ", errorThrown);
    }
}


问:
Running "jsonlint test.json" from cmd (Command Prompt), returns ENOENT: no such file or directory, errno: -4058
I'm running this command "jsonlint test.json" from cmd (Command Prompt).
 It returns ENOENT: no such file or directory, errno: -4058.  If I enter the text directly into https://jsonlint.com, it says valid.  I don't think its not valid, just says it can't find it.

I'm running the cmd prompt as administrator, going to the C:\Temp directory, then typing in from C:\Temp>jsonlint test.json

The test.json file exists in the C:\Temp folder.  Any thoughts on why it says it can't find it?

I'm afraid I'm totally new to jsonlint, so I don't know if I'm not typing something correctly, or what.  Any thoughts or ideas are greatly appreciated.

答: Ok. nevermind.  I'm embarrassed now.  I solved it once I figured out the machine I'm working on wasn't displaying the file extensions, so my "test.json" was really called, "test.json.txt"  Stupid Windows!</p>


问:
Textjoin: Using a unique ID, how do I concat multiple values?
I'm having trouble getting this to work. Basically, I have the following set of data:

In the TextJoin Formula column, I want it to do a look up against the adjacent ID, scan the income code column and concat the income codes pertaining to that unique ID, separated by a ','.

Many thanks

答:
use:

```<code>=TEXTJOIN(",",TRUE,IF($A$2:$A$6=$A2,$B$2:$B$6,""))
</code>```

Depending on one's version this may require Ctrl-Shift-Enter instead of Enter when exiting edit mode.

If one has the Dynamic Array Formula FILTER:

```<code>=TEXTJOIN(",",,FILTER(B:B,A:A=A2))
</code>```

Body:
use:

=TEXTJOIN(",",TRUE,IF($A$2:$A$6=$A2,$B$2:$B$6,""))

Depending on one's version this may require Ctrl-Shift-Enter instead of Enter when exiting edit mode.

If one has the Dynamic Array Formula FILTER:

=TEXTJOIN(",",,FILTER(B:B,A:A=A2))