问:
Paralle apply function on df in python
I have a function that go over two lists: items and dates.
The function return an updated list of items.
For now it runs with apply which is not that efficent on million of rows.
I want to make it more efficient by parallelizing it.

Items in item list are on chronological order, as well as the corresponding date list (item_list and date_list are the same size).

This is the df:

```<code>Date        item_list            date_list

12/05/20    [I1,I3,I4]    [10/05/20, 11/05/20, 12/05/20 ]
11/05/20    [I1,I3]       [11/05/20 , 14/05/20]
</code>```

This is the that I want df:

```<code>Date        item_list     date_list             items_list_per_date

12/05/20    [I1,I3,I4]    [10/05/20, 11/05/20, 12/05/20]   [I1,I3]
11/05/20    [I1,I3]       [11/05/20 , 14/05/20]               nan
</code>```

This is my code:

```<code>def get_item_list_per_date(date, items_list, date_list):

    if str(items_list)=="nan" or str(date_list)=="nan":
        return np.nan

    new_date_list = []
    for d in list(date_list):
        new_date_list.append(pd.to_datetime(d))

    if (date in new_date_list) and (len(new_date_list)>1):
        loc = new_date_list.index(date)
    else:
        return np.nan

    updated_items_list = items_list[:loc]

    if len(updated_items_list )==0:
        return np.nan

    return updated_items_list

df['items_list_per_date'] = df.progress_apply(lambda x: get_item_list_per_date(date=x['date'], items_list=x['items_list'], date_list=x['date_list']),axis=1)
</code>```

I would love to parallelized it of possible, can you help?

答:
Use:

```<code>import multiprocessing as mp

def fx(df):
    def __fx(s):
        date = s['Date']
        date_list = s['date_list']
        if date in date_list:
            loc = date_list.index(date)
            return s['item_list'][:loc]
        else:
            return np.nan

    return df.apply(__fx, axis=1)

def parallel_apply(df):
    dfs = filter(lambda d: not d.empty, np.array_split(df, mp.cpu_count()))
    pool = mp.Pool()
    per_date = pd.concat(pool.map(fx, dfs))
    pool.close()
    pool.join()
    return per_date

df['items_list_per_date'] = parallel_apply(df)
</code>```

Result:

```<code>#print(df)

Date        item_list     date_list             items_list_per_date

12/05/20    [I1,I3,I4]    [10/05/20, 11/05/20, 12/05/20]   [I1,I3]
11/05/20    [I1,I3]       [11/05/20 , 14/05/20]               nan
</code>```

Title: Paralle apply function on df in python

Body:
I have a function that go over two lists: items and dates.
The function return an updated list of items.
For now it runs with apply which is not that efficent on million of rows.
I want to make it more efficient by parallelizing it.

Items in item list are on chronological order, as well as the corresponding date list (item_list and date_list are the same size).

This is the df:

Date        item_list            date_list

12/05/20    [I1,I3,I4]    [10/05/20, 11/05/20, 12/05/20 ]
11/05/20    [I1,I3]       [11/05/20 , 14/05/20]

This is the that I want df:

Date        item_list     date_list             items_list_per_date

12/05/20    [I1,I3,I4]    [10/05/20, 11/05/20, 12/05/20]   [I1,I3]
11/05/20    [I1,I3]       [11/05/20 , 14/05/20]               nan

This is my code:

def get_item_list_per_date(date, items_list, date_list):

    if str(items_list)=="nan" or str(date_list)=="nan":
        return np.nan

    new_date_list = []
    for d in list(date_list):
        new_date_list.append(pd.to_datetime(d))

    if (date in new_date_list) and (len(new_date_list)>1):
        loc = new_date_list.index(date)
    else:
        return np.nan

    updated_items_list = items_list[:loc]

    if len(updated_items_list )==0:
        return np.nan

    return updated_items_list

df['items_list_per_date'] = df.progress_apply(lambda x: get_item_list_per_date(date=x['date'], items_list=x['items_list'], date_list=x['date_list']),axis=1)

I would love to parallelized it of possible, can you help?

Body:
Use:

import multiprocessing as mp

def fx(df):
    def __fx(s):
        date = s['Date']
        date_list = s['date_list']
        if date in date_list:
            loc = date_list.index(date)
            return s['item_list'][:loc]
        else:
            return np.nan

    return df.apply(__fx, axis=1)

def parallel_apply(df):
    dfs = filter(lambda d: not d.empty, np.array_split(df, mp.cpu_count()))
    pool = mp.Pool()
    per_date = pd.concat(pool.map(fx, dfs))
    pool.close()
    pool.join()
    return per_date

df['items_list_per_date'] = parallel_apply(df)

Result:

#print(df)

Date        item_list     date_list             items_list_per_date

12/05/20    [I1,I3,I4]    [10/05/20, 11/05/20, 12/05/20]   [I1,I3]
11/05/20    [I1,I3]       [11/05/20 , 14/05/20]               nan


来源: stackexchange

问:
How to change condition from a while loop
I am trying to create a reading list, using 2 functions - one to add books and another to display books. The add books function need to execute till such time user confirms that the update is completed. Following is the code :

```<code>book_list = []

def add_books(books):
    book_record = {}
    completed = "no"
    while completed == "no":
        title = input("Enter title : ")
        author = input("Enter author : ")
        year = input("Enter year of publication : ")
        book_record.update({"title": title, "author": author, "year_publ": year})
        books.append(book_record)
        print(books)
        completed = input("update completed ? yes/no")
    return books

def display_books(books):
    for book in books:
        title = book["title"]
        author = book["author"]
        year = book["year_publ"]
        print(f"{title}, {author}, {year}")

option = input("Enter 'a' to add and 'd' to display the books or 'q' to quit :")

while option != "q" :
    if option == "a":
        book_list = add_books(book_list)
    elif option == "d":
        display_books(book_list)
    else:
        print("Invalid Option")
        option = input("Enter 'a' to add and 'd' to display the books or 'q' to quit :")
</code>```

When I execute this code, the While loop ignores the completed condition and keeps asking for adding more books even though the user confirms updated completed.

What is wrong ? I understand that I am trying to update completed inside the loop, and that may be the reason. What are the options ?

Appreciate any help.

Thanks and Regards

Sachin

答:
Did you tried this?:</p>

```<code>def add_books(books):
book_record = {}
completed = "no"
while True:
    title = input("Enter title : ")
    author = input("Enter author : ")
    year = input("Enter year of publication : ")
    book_record.update({"title": title, "author": author, "year_publ": year})
    books.append(book_record)
    print(books)
    completed = input("update completed ? yes/no")
    if completed == 'yes':
        break
return books
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I am trying to create a reading list, using 2 functions - one to add books and another to display books. The add books function need to execute till such time user confirms that the update is completed. Following is the code :

book_list = []

def add_books(books):
    book_record = {}
    completed = "no"
    while completed == "no":
        title = input("Enter title : ")
        author = input("Enter author : ")
        year = input("Enter year of publication : ")
        book_record.update({"title": title, "author": author, "year_publ": year})
        books.append(book_record)
        print(books)
        completed = input("update completed ? yes/no")
    return books

def display_books(books):
    for book in books:
        title = book["title"]
        author = book["author"]
        year = book["year_publ"]
        print(f"{title}, {author}, {year}")

option = input("Enter 'a' to add and 'd' to display the books or 'q' to quit :")

while option != "q" :
    if option == "a":
        book_list = add_books(book_list)
    elif option == "d":
        display_books(book_list)
    else:
        print("Invalid Option")
        option = input("Enter 'a' to add and 'd' to display the books or 'q' to quit :")

When I execute this code, the While loop ignores the completed condition and keeps asking for adding more books even though the user confirms updated completed.

What is wrong ? I understand that I am trying to update completed inside the loop, and that may be the reason. What are the options ?

Appreciate any help.

Thanks and Regards

Sachin

Body:
Did you tried this?:

def add_books(books):
book_record = {}
completed = "no"
while True:
    title = input("Enter title : ")
    author = input("Enter author : ")
    year = input("Enter year of publication : ")
    book_record.update({"title": title, "author": author, "year_publ": year})
    books.append(book_record)
    print(books)
    completed = input("update completed ? yes/no")
    if completed == 'yes':
        break
return books


问:
incorrect date validation using moment
Im trying to use moment to validate the date input on my form, however for non existent dates such as 30th february instead of saying invalid date it will add the extra day onto the month meaning that it will output 1980-03-01 instead of taking the date as 1980-03-01 and invalidating the date

How can I get it to validate the date correctly?

```<code>const validateDate = (year, month, day) => {
    let validationMessage;
    const isNotEmpty = year && month && day;
    const date = isNotEmpty && `${year}-${month}-${day}`;
    const dateFormat = 'YYYY-MM-DD';
    const toDateFormat = moment(new Date(date)).format(dateFormat);
    const isDateValid = moment(toDateFormat, dateFormat, true).isValid();
    const isYearValid = (year > 1900 && year < new Date().getFullYear());

    if (!isDateValid) {
        validationMessage = `invalid date`;
    }

    return {
        isValid: isDateValid && isYearValid,
        validationMessage,
    };
};

export default {
    validate: validateDate
};
</code>```

答:
```<code>const date = moment('2020-02-30', 'YYYY-MM-DD', true);
const isValid = date.isValid(); //false

const date = moment('2020-02-28', 'YYYY-MM-DD', true);
const isValid = date.isValid(); //true
</code>```

Title: incorrect date validation using moment

Body:
Im trying to use moment to validate the date input on my form, however for non existent dates such as 30th february instead of saying invalid date it will add the extra day onto the month meaning that it will output 1980-03-01 instead of taking the date as 1980-03-01 and invalidating the date

How can I get it to validate the date correctly?

const validateDate = (year, month, day) => {
    let validationMessage;
    const isNotEmpty = year && month && day;
    const date = isNotEmpty && `${year}-${month}-${day}`;
    const dateFormat = 'YYYY-MM-DD';
    const toDateFormat = moment(new Date(date)).format(dateFormat);
    const isDateValid = moment(toDateFormat, dateFormat, true).isValid();
    const isYearValid = (year > 1900 && year < new Date().getFullYear());

    if (!isDateValid) {
        validationMessage = `invalid date`;
    }

    return {
        isValid: isDateValid && isYearValid,
        validationMessage,
    };
};

export default {
    validate: validateDate
};


问:
Infinite cycle due to QTextStream
So, I get infinite cycle while trying to read lines from file (line by line). I was trying to use do{}while(); cycle like that:

```<code>QTextStream stream(stdin);
QString line;
do {
    line = stream.readLine();
} while (!line.isNull());
</code>```

but I get empty string.

Sure, I checked file path (it is right). I was trying to use /Users/user/tts.txt path but without changes. I was trying to read other files (like m3u). And it's not working on macOS Catalina, Windows 10, Linux (Debian).

So, why did I get infinite cycle?

```<code>QStringList Manager::GetLinesFromFile(const QString &nameOfFile)
{
    QStringList lines = {};

    //path to file
    const QString path = QCoreApplication::applicationDirPath() + "/bin/" + "tts.txt";
    //"/Users/user/tts.txt"

    QFile buffer;
    buffer.QFile::setFileName(path);

    #ifndef Q_DEBUG
    qDebug() << path;
    #endif

    if(buffer.QFile::exists())
    {
        if(!buffer.QIODevice::open(QIODevice::ReadOnly))
        {
            #ifndef Q_DEBUG
            qCritical() << "error: can't open file";
            #endif
        }
        else
        {
            QTextStream stream(&buffer);

             // both conditions
            // (!stream.QTextStream::atEnd())
            while(!buffer.QFileDevice::atEnd())
                lines.QList::push_back(stream.QTextStream::readLine());

            buffer.QFile::close();
        }
    }
    else
    {
        #ifndef Q_DEBUG
        qCritical() << "error: file not exists";
        #endif
    }

    return lines;
}
</code>```

答:
So, I got it. I opened the file incorrectly.

I was using:

if(!file.QIODevice::open(QIODevice::ReadOnly))

but it should be like that:

if(!file.QFile::open(QFile::ReadOnly))

Title: Infinite cycle due to QTextStream

Body:
So, I get infinite cycle while trying to read lines from file (line by line). I was trying to use do{}while(); cycle like that:

QTextStream stream(stdin);
QString line;
do {
    line = stream.readLine();
} while (!line.isNull());

but I get empty string.

Sure, I checked file path (it is right). I was trying to use /Users/user/tts.txt path but without changes. I was trying to read other files (like m3u). And it's not working on macOS Catalina, Windows 10, Linux (Debian).

So, why did I get infinite cycle?

QStringList Manager::GetLinesFromFile(const QString &nameOfFile)
{
    QStringList lines = {};

    //path to file
    const QString path = QCoreApplication::applicationDirPath() + "/bin/" + "tts.txt";
    //"/Users/user/tts.txt"

    QFile buffer;
    buffer.QFile::setFileName(path);

    #ifndef Q_DEBUG
    qDebug() << path;
    #endif

    if(buffer.QFile::exists())
    {
        if(!buffer.QIODevice::open(QIODevice::ReadOnly))
        {
            #ifndef Q_DEBUG
            qCritical() << "error: can't open file";
            #endif
        }
        else
        {
            QTextStream stream(&buffer);

             // both conditions
            // (!stream.QTextStream::atEnd())
            while(!buffer.QFileDevice::atEnd())
                lines.QList::push_back(stream.QTextStream::readLine());

            buffer.QFile::close();
        }
    }
    else
    {
        #ifndef Q_DEBUG
        qCritical() << "error: file not exists";
        #endif
    }

    return lines;
}


问:
Karate fails to close earlier started Crome processes
In my project I use Karate 0.9.5, Oracle JDK8.
From time to time in our pipeline I see a problem. Karate fails to close earlier started Crome processes. Is there any solution to this problem?
I try to solved by explicitly call close() and quit() it didn't help. After running process was finished I found a couple of active chrome process on the server.
Here's a log:

```<code>12:51:45.227 preferred port 9222 not available, will use: 52436
12:51:47.524 request:
1 > GET http://localhost:52436/json
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Host: localhost:52436
1 > User-Agent: Apache-HttpClient/4.5.11 (Java/1.8.0_181)

12:51:47.584 response time in milliseconds: 58,31
1 < 200
1 < Content-Length: 361
1 < Content-Type: application/json; charset=UTF-8
[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:52436/devtools/page/4BD6A5C19E01B01D88995CD69367F81F",
   "id": "4BD6A5C19E01B01D88995CD69367F81F",
   "title": "",
   "type": "page",
   "url": "about:blank",
   "webSocketDebuggerUrl": "ws://localhost:52436/devtools/page/4BD6A5C19E01B01D88995CD69367F81F"
} ]

12:51:47.584 root frame id: 4BD6A5C19E01B01D88995CD69367F81F
12:51:47.632 >> {"method":"Target.activateTarget","params":{"targetId":"4BD6A5C19E01B01D88995CD69367F81F"},"id":1}
12:51:47.638 << {"id":1,"result":{}}
12:51:47.639 >> {"method":"Page.enable","id":2}
12:51:47.883 << {"id":2,"result":{}}
12:51:47.885 >> {"method":"Runtime.enable","id":3}
12:51:47.889 << {"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"origin":"://","name":"","auxData":{"isDefault":true,"type":"default","frameId":"4BD6A5C19E01B01D88995CD69367F81F"}}}}
12:51:47.890 << {"id":3,"result":{}}
12:51:47.890 >> {"method":"Target.setAutoAttach","params":{"autoAttach":true,"waitForDebuggerOnStart":false,"flatten":true},"id":4}
12:51:47.892 << {"id":4,"result":{}}
12:52:02.894 << timed out after milliseconds: 15000 - [id: 4, method: Target.setAutoAttach, params: {autoAttach=true, waitForDebuggerOnStart=false, flatten=true}]
12:52:02.917 driver config / start failed: failed to get reply for: [id: 4, method: Target.setAutoAttach, params: {autoAttach=true, waitForDebuggerOnStart=false, flatten=true}], options: {type=chrome, showDriverLog=true, httpConfig={readTimeout=60000}, headless=true, target=null}
</code>```

I think the easiest way to solve it is to extend root web driver interface com.intuit.karate.driver.Driver by adding additional method like getPID(). This method must return PID of launched process.

答:
Can you log a bug and also provide some information as to what happened before this, are you using the Docker image etc. It looks like the previous Scenario did not shut down clearly.

But before that do you mind upgrading to 0.9.6.RC3 (just released) and trying that ? There have been a couple of tweaks to the life-cycle especially trying to close Chrome without waiting for a response. Also it may be worth adding a couple of changes a) Target.setAutoAttach without waiting and b) add configure option to not start Chrome if the port is in use

Body:
In my project I use Karate 0.9.5, Oracle JDK8.
From time to time in our pipeline I see a problem. Karate fails to close earlier started Crome processes. Is there any solution to this problem?
I try to solved by explicitly call close() and quit() it didn't help. After running process was finished I found a couple of active chrome process on the server.
Here's a log:

12:51:45.227 preferred port 9222 not available, will use: 52436
12:51:47.524 request:
1 > GET http://localhost:52436/json
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Host: localhost:52436
1 > User-Agent: Apache-HttpClient/4.5.11 (Java/1.8.0_181)

12:51:47.584 response time in milliseconds: 58,31
1 < 200
1 < Content-Length: 361
1 < Content-Type: application/json; charset=UTF-8
[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:52436/devtools/page/4BD6A5C19E01B01D88995CD69367F81F",
   "id": "4BD6A5C19E01B01D88995CD69367F81F",
   "title": "",
   "type": "page",
   "url": "about:blank",
   "webSocketDebuggerUrl": "ws://localhost:52436/devtools/page/4BD6A5C19E01B01D88995CD69367F81F"
} ]

12:51:47.584 root frame id: 4BD6A5C19E01B01D88995CD69367F81F
12:51:47.632 >> {"method":"Target.activateTarget","params":{"targetId":"4BD6A5C19E01B01D88995CD69367F81F"},"id":1}
12:51:47.638 << {"id":1,"result":{}}
12:51:47.639 >> {"method":"Page.enable","id":2}
12:51:47.883 << {"id":2,"result":{}}
12:51:47.885 >> {"method":"Runtime.enable","id":3}
12:51:47.889 << {"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"origin":"://","name":"","auxData":{"isDefault":true,"type":"default","frameId":"4BD6A5C19E01B01D88995CD69367F81F"}}}}
12:51:47.890 << {"id":3,"result":{}}
12:51:47.890 >> {"method":"Target.setAutoAttach","params":{"autoAttach":true,"waitForDebuggerOnStart":false,"flatten":true},"id":4}
12:51:47.892 << {"id":4,"result":{}}
12:52:02.894 << timed out after milliseconds: 15000 - [id: 4, method: Target.setAutoAttach, params: {autoAttach=true, waitForDebuggerOnStart=false, flatten=true}]
12:52:02.917 driver config / start failed: failed to get reply for: [id: 4, method: Target.setAutoAttach, params: {autoAttach=true, waitForDebuggerOnStart=false, flatten=true}], options: {type=chrome, showDriverLog=true, httpConfig={readTimeout=60000}, headless=true, target=null}

I think the easiest way to solve it is to extend root web driver interface com.intuit.karate.driver.Driver by adding additional method like getPID(). This method must return PID of launched process.


问:
How to convert Pytorch model to ONNX model?
I have a model that contains two-part  encoder and decoder.
Each of them is a PyTorch model.

I inference them as below.

```<code>features = encoder(input)
output = decoder(features)
</code>```

and I want to convert them into one ONNX model instead of two ONNX models.
How can I do it?

答:
In general you should build a new class which inherits nn.Module that combines the multiple models. In this case you have a simple feed-forward network so we can use the nn.Sequential convenience class.

E.g.

```<code>model = torch.nn.Sequential([encoder, decoder])

# inference example
model.eval()
output = model(input)

# save model to onnx file ...
</code>```

Body:
I have a model that contains two-part  encoder and decoder.
Each of them is a PyTorch model.

I inference them as below.

features = encoder(input)
output = decoder(features)

and I want to convert them into one ONNX model instead of two ONNX models.
How can I do it?

Body:
In general you should build a new class which inherits nn.Module that combines the multiple models. In this case you have a simple feed-forward network so we can use the nn.Sequential convenience class.

E.g.

model = torch.nn.Sequential([encoder, decoder])

# inference example
model.eval()
output = model(input)

# save model to onnx file ...


问:
How do I clone an HTML element's style object using JavaScript or jQuery?
I'm trying to clone the style object of an element. This should allow me to reset the styles of said element after I change them.

For example:

```<code>el.style.left;      // 50px
curr_style.left;    // 50px;

/*
Modify the elements style.
The cloned style should still hold the original properties from when it was cloned.
*/
el.style.left = '20px';
curr_style.left // should still return 50px.
</code>```

I first tried to copy it by assigning a variable to the value of el.style. Unfortunately, this points to it by reference and any changes to the style are reflected in the cloned object.

My other attempt involved using jQuery's object extend method to create a copy as such:

```<code>var curr_style = $.extend( {}, el.style );
</code>```

This doesn't seem to work as curr_style.left etc. return undefined.

Any help would be appreciated!

I ended up doing this to retrieve each property: (based on advice from @Raynos)

```<code>$.fn.getStyle = function(){
    var style,
    el = this[0];

    // Fallbacks for old browsers.
    if (window.getComputedStyle) {
        style = window.getComputedStyle( el );
    } else if (el.currentStyle) {
        style = $.extend(true, {}, el.currentStyle);
    } else {
        style = $.extend(true, {}, el.style);
    }

    // Loop through styles and get each property. Add to object.
    var styles = {};
    for( var i=0; i<style.length; i++){
        styles[ style[i] ] = style[ style[i] ];
    }

    return styles;
};
</code>```

答:
For the purpose of simply resetting styles, I'd suggest just using the cssText (also see MDN) property of the style object. This works in all major browsers and is nice and simple.

jsFiddle:

http://jsfiddle.net/timdown/WpHme/

Example code:

```<code>// Store the original style
var originalCssText = el.style.cssText;

// Change a style property of the element
el.style.fontWeight = "bold";

// Now reset
el.style.cssText = originalCssText;
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I'm trying to clone the style object of an element. This should allow me to reset the styles of said element after I change them.

For example:

el.style.left;      // 50px
curr_style.left;    // 50px;

/*
Modify the elements style.
The cloned style should still hold the original properties from when it was cloned.
*/
el.style.left = '20px';
curr_style.left // should still return 50px.

I first tried to copy it by assigning a variable to the value of el.style. Unfortunately, this points to it by reference and any changes to the style are reflected in the cloned object.

My other attempt involved using jQuery's object extend method to create a copy as such:

var curr_style = $.extend( {}, el.style );

This doesn't seem to work as curr_style.left etc. return undefined.

Any help would be appreciated!

I ended up doing this to retrieve each property: (based on advice from @Raynos)

$.fn.getStyle = function(){
    var style,
    el = this[0];

    // Fallbacks for old browsers.
    if (window.getComputedStyle) {
        style = window.getComputedStyle( el );
    } else if (el.currentStyle) {
        style = $.extend(true, {}, el.currentStyle);
    } else {
        style = $.extend(true, {}, el.style);
    }

    // Loop through styles and get each property. Add to object.
    var styles = {};
    for( var i=0; i

Body:
For the purpose of simply resetting styles, I'd suggest just using the cssText (also see MDN) property of the style object. This works in all major browsers and is nice and simple.

jsFiddle:

http://jsfiddle.net/timdown/WpHme/

Example code:

// Store the original style
var originalCssText = el.style.cssText;

// Change a style property of the element
el.style.fontWeight = "bold";

// Now reset
el.style.cssText = originalCssText;


问:
Oracle sql combination
I have table with this result (just for example, actually it has more records):

```<code>31.3.2020   A  1
30.4.2020   A  2
31.3.2020   B  1
30.4.2020   C  3
</code>```

Is possible to add there empty missing values by sql?
In this case:

```<code>30.4.2020   B  0
31.3.2020   C  0
</code>```

Thanks

答:
Please use below query,

```<code>select col1, col2,
case when (col3 is null or col3 = '') then 0 else col3 as col3
from table;
</code>```

You can use below if you don't have any empty strings,

```<code>select col1, col2,
case when nvl(col3, 0) as col3
from table;
</code>```

Title: Oracle sql combination

Body:
I have table with this result (just for example, actually it has more records):

31.3.2020   A  1
30.4.2020   A  2
31.3.2020   B  1
30.4.2020   C  3

Is possible to add there empty missing values by sql?
In this case:

30.4.2020   B  0
31.3.2020   C  0

Thanks

Body:
Please use below query,

select col1, col2,
case when (col3 is null or col3 = '') then 0 else col3 as col3
from table;

You can use below if you don't have any empty strings,

select col1, col2,
case when nvl(col3, 0) as col3
from table;


问:
Get a list of AWS pipelines ready for stage approval
I have 40+ pipelines that I need to approve from dev to QA and then QA to stage.  I am working on a script to use AWS CLI commands to do do.  I have been able to do that for a single pipeline where I know that the specific pipeline is ready to be approved.

```<code>aws codepipeline put-approval-result --cli-input-json file://TestPipeline.json
</code>```

This is how I gathered the information for the approval for a single pipeline

```<code>aws codepipeline get-pipeline-state --name Pipeline-Ready-for-Approval
</code>```

What I am trying to find out is - is there a way to loop through all of the pipelines using the get-pipeline-state and identify the stage name and action name without manually going through output of each of the pipelines.

I can try to get the pipeline names from aws codepipeline list-pipelines to get the list to loop through.

Is it possible using bash script and awscli and jq together?

Thank you

答:
You can get most of the way there using the following

pipelines=$(aws codepipeline list-pipelines --query 'pipelines[].name' --output text)

for p in pipelines; do
    aws codepipeline get-pipeline-state \
        --name $p \
        --query 'stageStates[?latestExecution.status==`InProgress`].{stageName:stageName,actionName:actionStates[0].actionName,token:actionStates[0].latestExecution.token}'
done
```

This assumes the approval action exists as the first or only action within a stage.

You'll get the output required for the put-approval-result command

Body:
I have 40+ pipelines that I need to approve from dev to QA and then QA to stage.  I am working on a script to use AWS CLI commands to do do.  I have been able to do that for a single pipeline where I know that the specific pipeline is ready to be approved.

aws codepipeline put-approval-result --cli-input-json file://TestPipeline.json

This is how I gathered the information for the approval for a single pipeline

aws codepipeline get-pipeline-state --name Pipeline-Ready-for-Approval

What I am trying to find out is - is there a way to loop through all of the pipelines using the get-pipeline-state and identify the stage name and action name without manually going through output of each of the pipelines.

I can try to get the pipeline names from aws codepipeline list-pipelines to get the list to loop through.

Is it possible using bash script and awscli and jq together?

Thank you

Body:
You can get most of the way there using the following

pipelines=$(aws codepipeline list-pipelines --query 'pipelines[].name' --output text)

for p in pipelines; do
    aws codepipeline get-pipeline-state \
        --name $p \
        --query 'stageStates[?latestExecution.status==`InProgress`].{stageName:stageName,actionName:actionStates[0].actionName,token:actionStates[0].latestExecution.token}'
done

This assumes the approval action exists as the first or only action within a stage.

You'll get the output required for the put-approval-result command


问:
How to color one of a 100 boxes with a button in a modal using javascript
Just to make it easier Im explaining what I wish to do in a simplified example.

I have 100 boxes on my site using simple html/css. (styling not shown)

I have written a bit of JS to open a modal that opens when clicking on one of the 100 boxes carrying the same class: class="boxes":

The modal has an Id of "modal":

```<code>var modal = document.getElementById("modal");
var boxes = document.getElementsByClassName("box");
var boxesLength = boxes.length;
</code>```

When any box is clicked the modal appears:

```<code>    for (var i = 0; i < boxesLength; i++) {
    boxes[i].onclick = function () {
    modal.style.display = "block";
    };
    }
</code>```

Now I wish to color the box  that was clicked using a button in the modal that was opened. The code that doesn't work but is my closest guess looks like this:

```<code>//get the coloring button in the modal which has Id of "green".

var coloring = document.getElementById("green");

coloring.onclick = function () {
boxes[i].style.backgroundColor = "#90EE90";
//closing the modal after clicking which works
modal.style.display = "none";
};
</code>```

Writing "boxes[0]" will color the first box, but that is of course not what I wish. I wish to color the box that was clicked.

Thank you for any input. Good day!

on request ive added som simple html

```<code>    //The modal
    <div id="modal">
            <button id="green">Completed</button>
    </div>

    //just some boxes
    <div class="boxes"></div>
    <div class="boxes"></div>
    <div class="boxes"></div>
    <div class="boxes"></div>
    .... 96 more boxes
</code>```

答:
I believe your issue is here. You have: for (var i = 0; i < btnsLength; i++) {, but should be referring to boxesLength so for (var i = 0; i < boxesLength; i++.

This would be an ideal application for 'event delegation'. Read more here: https://javascript.info/event-delegation.

Essentially you put the click handler on the parent container for all of your boxes and then use target to point to the clicked box. That gives you 1 event handler instead of 100.

Body:
Just to make it easier Im explaining what I wish to do in a simplified example.

I have 100 boxes on my site using simple html/css. (styling not shown)

I have written a bit of JS to open a modal that opens when clicking on one of the 100 boxes carrying the same class: class="boxes":

The modal has an Id of "modal":

var modal = document.getElementById("modal");
var boxes = document.getElementsByClassName("box");
var boxesLength = boxes.length;

When any box is clicked the modal appears:

    for (var i = 0; i < boxesLength; i++) {
    boxes[i].onclick = function () {
    modal.style.display = "block";
    };
    }

Now I wish to color the box  that was clicked using a button in the modal that was opened. The code that doesn't work but is my closest guess looks like this:

//get the coloring button in the modal which has Id of "green".

var coloring = document.getElementById("green");

coloring.onclick = function () {
boxes[i].style.backgroundColor = "#90EE90";
//closing the modal after clicking which works
modal.style.display = "none";
};

Writing "boxes[0]" will color the first box, but that is of course not what I wish. I wish to color the box that was clicked.

Thank you for any input. Good day!

on request ive added som simple html

    //The modal

            <button id="green">Completed</button>

    //just some boxes

    .... 96 more boxes


问:
Rider does not open script while clicking error/log message in unity
When I double click script/message/error in unity rider does not open it.
 Instead it shows me this message:

Cannot find file 'D:\path_to_project--column'.

答:
Thank you, Ivan Shakov.

I am using different versions of Unity.
Rider updating to 2020.1.3 version helped me.


问:
Check the type of all elements in a list of list in and that their numerical value falls within a range
I want to loop over each element and check if its type is int and if it lays within range of 12 <= i <= 20
if so return True

I use any to return if all elements are correct and isinstance to catch the type of each element.

The function gives me no output and no bug report so I don't know where the error is.

```<code>listoflist = [[14,13,13,14]]

def list_check(my_list):
    if all(isinstance(i, int) and 12 <= i <= 20 for i in my_list):
        print("true")
        return True
</code>```

答:
The problem in your case because you have list in list:</p>

```<code>listoflist = [14,13,13,14]

def list_check(my_list):
    if all(isinstance(i, int) and 12 <= i <= 20 for i in my_list):
        print("true")
        return True

print(list_check(listoflist))
</code>```

Body:
I want to loop over each element and check if its type is int and if it lays within range of 12 <= i <= 20
if so return True

I use any to return if all elements are correct and isinstance to catch the type of each element.

The function gives me no output and no bug report so I don't know where the error is.

listoflist = [[14,13,13,14]]

def list_check(my_list):
    if all(isinstance(i, int) and 12 <= i <= 20 for i in my_list):
        print("true")
        return True

Body:
The problem in your case because you have list in list:

listoflist = [14,13,13,14]

def list_check(my_list):
    if all(isinstance(i, int) and 12 <= i <= 20 for i in my_list):
        print("true")
        return True

print(list_check(listoflist))


问:
grouping continuous data with specific pattern
I have a data frame with a column like this (I am not posting other columns)

```<code>Value
1
1
1
0
0
1
0
0
1
1
2
2
0
0
1
0
0
1
1
1
0
0
2
2
1
1
2
0
0
1
0
</code>```

I am trying to group it based on a specific condition. Grouping has to be done when I have 1 and 2. But conditions like these are one group :

```<code>1 1 0 0 1 1 0 0
</code>```

Basically I need to group occurrences of 1 but in between 0s are allowed

Expected output:

```<code>Value   Group
    1   1
    1   1
    1   1
    0   1
    0   1
    1   1
    0   1
    0   1
    1   1
    1   1
    2   2
    2   2
    0   2
    0   2
    1   3
    0   3
    0   3
    1   3
    1   3
    1   3
    0   3
    0   3
    2   4
    2   4
    1   5
    1   5
    2   6
    0   6
    0   6
    1   7
    0   7
    2   8
    0   8
    2   8
    1   9
</code>```

Body:
I have a data frame with a column like this (I am not posting other columns)

Value
1
1
1
0
0
1
0
0
1
1
2
2
0
0
1
0
0
1
1
1
0
0
2
2
1
1
2
0
0
1
0

I am trying to group it based on a specific condition. Grouping has to be done when I have 1 and 2. But conditions like these are one group :

1 1 0 0 1 1 0 0

Basically I need to group occurrences of 1 but in between 0s are allowed

Expected output:

Value   Group
    1   1
    1   1
    1   1
    0   1
    0   1
    1   1
    0   1
    0   1
    1   1
    1   1
    2   2
    2   2
    0   2
    0   2
    1   3
    0   3
    0   3
    1   3
    1   3
    1   3
    0   3
    0   3
    2   4
    2   4
    1   5
    1   5
    2   6
    0   6
    0   6
    1   7
    0   7
    2   8
    0   8
    2   8
    1   9

Body:
This loop in Base-R does the trick

group <- 0
lastgroupvalue <- NA
data$Group <- NA

for(i in 1:nrow(data)){
    if(!data$Value[i] %in% c(lastgroupvalue, 0)){
        group <- group + 1
        lastgroupvalue <- data$Value[i]
    }
    data$Group[i] <- group
}

> data
   Value Group
1      1     1
2      1     1
3      1     1
4      0     1
5      0     1
6      1     1
7      0     1
8      0     1
9      1     1
10     1     1
11     2     2
12     2     2
13     0     2
14     0     2
15     1     3
16     0     3
17     0     3
18     1     3
19     1     3
20     1     3
21     0     3
22     0     3
23     2     4
24     2     4
25     1     5
26     1     5
27     2     6
28     0     6
29     0     6
30     1     7
31     0     7

Data:

data <- structure(list(Value = c(1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L,
1L, 2L, 2L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 2L, 2L, 1L,
1L, 2L, 0L, 0L, 1L, 0L)), class = "data.frame", row.names = c(NA,
-31L))


问:
Vuex: How to grab latest user data after user profile updated?
I am using Vuex and having trouble getting a user's data to be "reactive" after his profile has been updated. Here's my scenario:

My App.vue checks a user's properties during the created() hook
  like so:

```<code>  async created() {
    await this.$store.dispatch('getSSOUser') // gets user from auth server
    await this.$store.dispatch('fetchUserProfiles') // queries known user table to see if user has a profile
    // set for global user state throughout app
    await this.setUser()
// then loads the UI
    this.isBusy = false
</code>```

```<code>  methods: {
    setUser() {
      const user = this.getUserProfileBySSOID(this.ssoUser.data.user_id)
      this.$store.commit('SET_USER', user)
    }
</code>```

So now I have the user's profile (user object) to use throughout the app. Works good....but...when a user edits his profile in the app (for example, updates his phone number, etc) and clicks submit, I can't seem to get the state to refresh/see that there has been a change unless the user manually refreshes the page.

What is the recommended way to handle this issue? Do I need to run a dispatch to the user state on every route change? The user's profile is located at path: '/userEdit/:uid'

This is my app structure:

```<code><div id="app">
    <Banner />
    <section class="container-fluid">
      <loading-spinner v-if="isBusy"></loading-spinner>
      <div v-else>
        <AuthName class="text-right" />
        <MainNav />
        <main id="routerView">
          <transition name="component-fade" mode="out-in">
            <RouterView :key="$route.fullPath" />
          </transition>
        </main>
      </div>
    </section>
  </div>
</code>```

User profile update function:

```<code>  ApiService.putUserProfile(this.user)
    .then(() => {
      this.loading = false
      this.$router.push('/admin/users')
      }
    })
    .catch(err => {
      if (err.response) {
        this.errors = err.response.data
      } else {
        if (err.request) {
          this.errors = err.request
        } else {
          this.errors = err.message
        }
      }
      this.loading = false
      console.error('Error from update', err)
    })
</code>```

答:
after you update the detail of the user you can fire an event which may will fetch the data from the server behind the scene.

```<code>methods:{
loadData(){
         await this.$store.dispatch('getSSOUser') ;
         await this.$store.dispatch('fetchUserProfiles');
         await this.setUser()
         this.isBusy = false
          },
updateInfo() {
                this.form.put('api/profile').then((response) => {
                let userData = response.data.userdata;
                Fire.$emit('ProfileEvent');
            }
           },
  },
created(){
        this.loadData();
        Fire.$on('ProfileEvent', () => {
        this.loadData();
        });
       }
</code>```

may be firing an event after the there is any changes saved in the profile page and executing the function that is called when the component is created after the fired event may resolve your problem. Hope you will get the idea from above code example.

Body:
I am using Vuex and having trouble getting a user's data to be "reactive" after his profile has been updated. Here's my scenario:

My App.vue checks a user's properties during the created() hook
  like so:

  async created() {
    await this.$store.dispatch('getSSOUser') // gets user from auth server
    await this.$store.dispatch('fetchUserProfiles') // queries known user table to see if user has a profile
    // set for global user state throughout app
    await this.setUser()
// then loads the UI
    this.isBusy = false

  methods: {
    setUser() {
      const user = this.getUserProfileBySSOID(this.ssoUser.data.user_id)
      this.$store.commit('SET_USER', user)
    }

So now I have the user's profile (user object) to use throughout the app. Works good....but...when a user edits his profile in the app (for example, updates his phone number, etc) and clicks submit, I can't seem to get the state to refresh/see that there has been a change unless the user manually refreshes the page.

What is the recommended way to handle this issue? Do I need to run a dispatch to the user state on every route change? The user's profile is located at path: '/userEdit/:uid'

This is my app structure:

    <Banner />

      <loading-spinner v-if="isBusy"></loading-spinner>

        <AuthName class="text-right" />
        <MainNav />

          <transition name="component-fade" mode="out-in">
            <RouterView :key="$route.fullPath" />
          </transition>

User profile update function:

  ApiService.putUserProfile(this.user)
    .then(() => {
      this.loading = false
      this.$router.push('/admin/users')
      }
    })
    .catch(err => {
      if (err.response) {
        this.errors = err.response.data
      } else {
        if (err.request) {
          this.errors = err.request
        } else {
          this.errors = err.message
        }
      }
      this.loading = false
      console.error('Error from update', err)
    })

Body:
after you update the detail of the user you can fire an event which may will fetch the data from the server behind the scene.

methods:{
loadData(){
         await this.$store.dispatch('getSSOUser') ;
         await this.$store.dispatch('fetchUserProfiles');
         await this.setUser()
         this.isBusy = false
          },
updateInfo() {
                this.form.put('api/profile').then((response) => {
                let userData = response.data.userdata;
                Fire.$emit('ProfileEvent');
            }
           },
  },
created(){
        this.loadData();
        Fire.$on('ProfileEvent', () => {
        this.loadData();
        });
       }

may be firing an event after the there is any changes saved in the profile page and executing the function that is called when the component is created after the fired event may resolve your problem. Hope you will get the idea from above code example.


问:
WooCommerce - Remove item from cart if specific coupon code or product is not present or removed
I have added a function to add a free product when a certain coupon code is entered.

I now need to check if the original product is still in the cart, and if not, remove coupon and free product from the cart.

add_action('woocommerce_applied_coupon', 'apply_product_on_coupon');
function apply_product_on_coupon( ) {
    global $woocommerce;
    $coupon_idcus = 'ccam0620';
    $free_product_id = 11259;

    if(in_array($coupon_idcus, $woocommerce->cart->get_applied_coupons())){
        $woocommerce->cart->add_to_cart($free_product_id, 2);
    }
}
```

答:
You will need another action to check if the coupon was removed.

```<code>function action_woocommerce_removed_coupon( $coupon_code ) {
   if($coupon_code == 'ccam0620') {

     $product_id = 11259;
     $product_cart_id = WC()->cart->generate_cart_id( $product_id );
     $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );
     if ( $cart_item_key ) WC()->cart->remove_cart_item( $cart_item_key );
   }
};

// add the action
add_action( 'woocommerce_removed_coupon', 'action_woocommerce_removed_coupon', 10, 1 );
</code>```

This should do it - I haven't tested it.

Source 1: http://hookr.io/actions/woocommerce_removed_coupon/

Source 2: https://businessbloomer.com/woocommerce-remove-product-from-cart-programmatically/

Body:
You will need another action to check if the coupon was removed.

function action_woocommerce_removed_coupon( $coupon_code ) {
   if($coupon_code == 'ccam0620') {

     $product_id = 11259;
     $product_cart_id = WC()->cart->generate_cart_id( $product_id );
     $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );
     if ( $cart_item_key ) WC()->cart->remove_cart_item( $cart_item_key );
   }
};

// add the action
add_action( 'woocommerce_removed_coupon', 'action_woocommerce_removed_coupon', 10, 1 );

This should do it - I haven't tested it.

Source 1: http://hookr.io/actions/woocommerce_removed_coupon/

Source 2: https://businessbloomer.com/woocommerce-remove-product-from-cart-programmatically/


问:
File not found when located in "Test Resources Root"
I am new to IntelliJ IDEA and I am trying to use specific files for my unit tests, but it seems that these cannot be found. I have built a special project/module to "reduce" the error in a totally clean setting to make sure that I don't miss something, however, it still does not work.

My file structure (as shown in the screenshot):
.../test/jave/FooTest.java
.../test/resources/test.txt

Folder ".../test/resources" has been marked as "Test Resources Root". However, when the test runs, file could not be found.

I suppose that marking the folder as "Test Resources Root" should add all its files in the path. Isn't this right?

PS. I have seen this post but even adding the folder as a dependency did not work.

答:
To get a file from the resources folder, You can use any approach given here:https://www.baeldung.com/junit-src-test-resources-directory-path

```<code>File("test.txt").exists();
</code>```

Will return true when your file located at ...\project_name\test.txt.
Check by

```<code>File("test.txt").getAbsolutePath();
</code>```

Body:
To get a file from the resources folder, You can use any approach given here:https://www.baeldung.com/junit-src-test-resources-directory-path

File("test.txt").exists();

Will return true when your file located at ...\project_name\test.txt.
Check by

File("test.txt").getAbsolutePath();


问:
What is the best way to parse a tuple from a string in Python?
I tried this:

```<code>def string_to_value(self, old_value, distribution_type, new_value_str):
    parameter_names = distribution_type.parameters  # a list of string
    try:
        parameter_values = ast.literal_eval(new_value_str)  # a tuple or basic type hopefully
    except SyntaxError:
        raise ValueError('Syntax error during parse')
    retval = copy.copy(old_value)
    try:
        if len(parameter_names) == 1:
            setattr(retval, parameter_names[0], parameter_values)
        else:
            if len(parameter_names) != len(parameter_values):
                raise BoostPythonArgumentError
            for parm_name, parm_value in zip(parameter_names,
                                             parameter_values):
                setattr(retval, parm_name, parm_value)
    except BoostPythonArgumentError:
        raise ValueError('Lots of helpful text here')
    return retval
</code>```

This works for a lot of cases.  Boost.Python automatically checks the type of parm_value at set time.  Unfortunately, it doesn't work on strings containing 'inf'.  ast.literal_eval raises ValueError('malformed string') when I would like it to return a float.  I don't understand how Python can parse 'inf', but literal_eval can't.

答: Check this documentation and this PEP about evaluating inf. I guess they will help

Body:
I tried this:

def string_to_value(self, old_value, distribution_type, new_value_str):
    parameter_names = distribution_type.parameters  # a list of string
    try:
        parameter_values = ast.literal_eval(new_value_str)  # a tuple or basic type hopefully
    except SyntaxError:
        raise ValueError('Syntax error during parse')
    retval = copy.copy(old_value)
    try:
        if len(parameter_names) == 1:
            setattr(retval, parameter_names[0], parameter_values)
        else:
            if len(parameter_names) != len(parameter_values):
                raise BoostPythonArgumentError
            for parm_name, parm_value in zip(parameter_names,
                                             parameter_values):
                setattr(retval, parm_name, parm_value)
    except BoostPythonArgumentError:
        raise ValueError('Lots of helpful text here')
    return retval

This works for a lot of cases.  Boost.Python automatically checks the type of parm_value at set time.  Unfortunately, it doesn't work on strings containing 'inf'.  ast.literal_eval raises ValueError('malformed string') when I would like it to return a float.  I don't understand how Python can parse 'inf', but literal_eval can't.


答:
It can be done using numpy following way

```<code>import numpy as np
arr = np.array([[3,3,5],[3,3,7],[3,3,3],[5,1,1]])  # toy example
sorted_arr = arr[np.lexsort(arr.T[::-1])]
print(sorted_arr)
</code>```

Output:

```<code>[[3 3 3]
 [3 3 5]
 [3 3 7]
 [5 1 1]]
</code>```

Note that we need to do following actions to use arr as keys for numpy.lexsort:

transpose (to get k,N-shaped array, where k is number of different columns to be sorted - in my example 3)

flip (without that [::-1] rightmost column would be most important and leftmost least important)

Title: Is it possible to sort Numpy 2D array like dictionary characters are matching

Body:
I would like to sort them as given below:

First it will check '0th' col & if all values are different, then it will sort by interchanging the rows up & down.

If '0th' column contains same values, then it will place them consecutive rows & sorting depends on the value of 1st col. If further matches, process continues f

As the row index 2 & 3 have the same values in 0, 1, 2 the column. So by checking at 3rd col it decides the position of row for these two rows. Please help. Thanks in advance.
NB: As we compare words in dictionary to search, I also want to compare like that.

Input Array:

[23.339,    0.015,  0.008,  0.0071,     0.079,  0.001]
[20.759,    0.024,  0.020,  0.0161,     0.1282, 0.030]
[17.352,    0.063,  0.024,  0.0187,     0.1484, 0.039]
[17.352,    0.063,  0.024,  0.008,  0.128,  0.035]
[23.339,    0.015,  0.001,  0.204,  1.819,  0.762]
[17.352,    1.645,  1.045,  0.779,  1.106,  0.821]
[14.556,    0.061,  0.058,  0.0071,     0.1016, 0.005]

Output Array:

[14.556,    0.061,  0.058,  0.0071,     0.1016, 0.005]
[**17.352,  0.063,  0.024,**    0.008,  0.128,  0.035]
[**17.352,  0.063,  0.024,**    0.0187,     0.1484, 0.039]
[17.352,    1.645,  1.045,  0.779,  1.106,  0.821]
[20.759,    0.024,  0.020,  0.0161, 0.1282, 0.030]
[**23.339,  0.015,**    0.001,  0.204,  1.819,  0.762]
[**23.339,  0.015,**    0.008,  0.0071, 0.079,  0.001]

Body:
It can be done using numpy following way

import numpy as np
arr = np.array([[3,3,5],[3,3,7],[3,3,3],[5,1,1]])  # toy example
sorted_arr = arr[np.lexsort(arr.T[::-1])]
print(sorted_arr)

Output:

[[3 3 3]
 [3 3 5]
 [3 3 7]
 [5 1 1]]

Note that we need to do following actions to use arr as keys for numpy.lexsort:

transpose (to get k,N-shaped array, where k is number of different columns to be sorted - in my example 3)

flip (without that [::-1] rightmost column would be most important and leftmost least important)


问:
How to customize BooleanInput to add or remove items from a list?
I am using the awesome react-admin library and looking for a way to customize BooleanInput field.

The following use case: I have an array of rights given to a user. E.g.

```<code>user: {
  rights: ["REGULAR", "VIP", "ADMIN"],
  otherFields...
}
</code>```

and the UX requirement is to have an booleanInput to indicate if a user has a certain right. E.g. if the rights array contains VIP, then the BooleanInput should be set to true. If a user set it to false, VIP should be removed from the array and vice versa.

Any idea how to do this?

答:
What you are looking is transforming input to suite your needs
https://marmelab.com/react-admin/Inputs.html#transforming-input-value-tofrom-record</p>

```<code><BooleanInput source="rights" format={v => v.includes("VIP")} parse={v => {if(v){ //return array including vip} else {// return array excluding vip}}} label="Admin" />
</code>```

Body:
I am using the awesome react-admin library and looking for a way to customize BooleanInput field.

The following use case: I have an array of rights given to a user. E.g.

user: {
  rights: ["REGULAR", "VIP", "ADMIN"],
  otherFields...
}

and the UX requirement is to have an booleanInput to indicate if a user has a certain right. E.g. if the rights array contains VIP, then the BooleanInput should be set to true. If a user set it to false, VIP should be removed from the array and vice versa.

Any idea how to do this?

Body:
What you are looking is transforming input to suite your needs
https://marmelab.com/react-admin/Inputs.html#transforming-input-value-tofrom-record

<BooleanInput source="rights" format={v => v.includes("VIP")} parse={v => {if(v){ //return array including vip} else {// return array excluding vip}}} label="Admin" />


问:
How to convert the file with multiple delimiter to dataframe
I have a text file as below

```<code>1234_4567_DigitalDoc_XRay-01.pdf
2345_5678_DigitalDoc_CTC-03.png
1234_5684_DigitalDoc_XRay-05.pdf
1234_3345_DigitalDoc_XRay-02.pdf
</code>```

I am expecting the output as

```<code>| catg|sub_catg|      doc_name        |revision_label|extension|
|1234|     4567|DigitalDoc_XRay-01.pdf|   01         |pdf      |
</code>```

I have created a custom schema

```<code> val customSchema = StructType(
      StructField("catg", StringType, true)
        :: StructField("sub_catg", StringType, true)
        :: StructField("doc_name", StringType, true)
        :: StructField("revision_label", StringType, true)
        :: StructField("extension", StringType, true)
        :: Nil
    )
</code>```

I am trying to create a dataframe as

```<code>val df = sparkSession.read
  .format("csv")
  .schema(customSchema)
  .option("delimiter", "_")
  .load("src/main/resources/data/sample.txt")

df.show()
</code>```

I am wondering how to break that each line by custom record

I could probably write a java code something of this kind, can someone please help me with the spark. I am new to spark.

```<code>String word[] = line.split("_");

            String filenName[] = word[3].split("-");
            String revision = filenName[1];
            word[0]+","+word[1]+","+ word[2]+"_"+word[3]+","+revision.replace(".", " ");
</code>```

答:
you can use spark functions to fetch the required details -

1. Load the data

 val data =
      """
        |1234_4567_DigitalDoc_XRay-01.pdf
        |2345_5678_DigitalDoc_CTC-03.png
        |1234_5684_DigitalDoc_XRay-05.pdf
        |1234_3345_DigitalDoc_XRay-02.pdf
      """.stripMargin

    val customSchema = StructType(
      StructField("catg", StringType, true)
        :: StructField("sub_catg", StringType, true)
        :: StructField("doc_name", StringType, true)
        :: StructField("revision_label", StringType, true)
        :: StructField("extension", StringType, true)
        :: Nil
    )
    val df = spark.read.schema(customSchema)
      .option("sep", "_")
      .csv(data.split(System.lineSeparator()).toSeq.toDS())
    df.show(false)
    df.printSchema()
```

<p>Output-</p>

```+----+--------+----------+--------------+---------+
|catg|sub_catg|doc_name  |revision_label|extension|
+----+--------+----------+--------------+---------+
|1234|4567    |DigitalDoc|XRay-01.pdf   |null     |
|2345|5678    |DigitalDoc|CTC-03.png    |null     |
|1234|5684    |DigitalDoc|XRay-05.pdf   |null     |
|1234|3345    |DigitalDoc|XRay-02.pdf   |null     |
+----+--------+----------+--------------+---------+

root
 |-- catg: string (nullable = true)
 |-- sub_catg: string (nullable = true)
 |-- doc_name: string (nullable = true)
 |-- revision_label: string (nullable = true)
 |-- extension: string (nullable = true)
```

<h2>2. Extract the required info</h2>

<pre class="lang-scala prettyprint-override"><code> df.withColumn("doc_name", concat_ws("_", col("doc_name"), col("revision_label")))
      .withColumn("extension", substring_index(col("revision_label"), ".", -1))
      .withColumn("revision_label", regexp_extract(col("revision_label"),"""\d+""", 0))
      .show(false)
</code>```

Output-

```<code>+----+--------+----------------------+--------------+---------+
|catg|sub_catg|doc_name              |revision_label|extension|
+----+--------+----------------------+--------------+---------+
|1234|4567    |DigitalDoc_XRay-01.pdf|01            |pdf      |
|2345|5678    |DigitalDoc_CTC-03.png |03            |png      |
|1234|5684    |DigitalDoc_XRay-05.pdf|05            |pdf      |
|1234|3345    |DigitalDoc_XRay-02.pdf|02            |pdf      |
+----+--------+----------------------+--------------+---------+
</code>```

Body:
I have a text file as below

1234_4567_DigitalDoc_XRay-01.pdf
2345_5678_DigitalDoc_CTC-03.png
1234_5684_DigitalDoc_XRay-05.pdf
1234_3345_DigitalDoc_XRay-02.pdf

I am expecting the output as

| catg|sub_catg|      doc_name        |revision_label|extension|
|1234|     4567|DigitalDoc_XRay-01.pdf|   01         |pdf      |

I have created a custom schema

 val customSchema = StructType(
      StructField("catg", StringType, true)
        :: StructField("sub_catg", StringType, true)
        :: StructField("doc_name", StringType, true)
        :: StructField("revision_label", StringType, true)
        :: StructField("extension", StringType, true)
        :: Nil
    )

I am trying to create a dataframe as

val df = sparkSession.read
  .format("csv")
  .schema(customSchema)
  .option("delimiter", "_")
  .load("src/main/resources/data/sample.txt")

df.show()

I am wondering how to break that each line by custom record

I could probably write a java code something of this kind, can someone please help me with the spark. I am new to spark.

String word[] = line.split("_");

            String filenName[] = word[3].split("-");
            String revision = filenName[1];
            word[0]+","+word[1]+","+ word[2]+"_"+word[3]+","+revision.replace(".", " ");

Body:
you can use spark functions to fetch the required details -

1. Load the data

 val data =
      """
        |1234_4567_DigitalDoc_XRay-01.pdf
        |2345_5678_DigitalDoc_CTC-03.png
        |1234_5684_DigitalDoc_XRay-05.pdf
        |1234_3345_DigitalDoc_XRay-02.pdf
      """.stripMargin

    val customSchema = StructType(
      StructField("catg", StringType, true)
        :: StructField("sub_catg", StringType, true)
        :: StructField("doc_name", StringType, true)
        :: StructField("revision_label", StringType, true)
        :: StructField("extension", StringType, true)
        :: Nil
    )
    val df = spark.read.schema(customSchema)
      .option("sep", "_")
      .csv(data.split(System.lineSeparator()).toSeq.toDS())
    df.show(false)
    df.printSchema()

Output-

+----+--------+----------+--------------+---------+
|catg|sub_catg|doc_name  |revision_label|extension|
+----+--------+----------+--------------+---------+
|1234|4567    |DigitalDoc|XRay-01.pdf   |null     |
|2345|5678    |DigitalDoc|CTC-03.png    |null     |
|1234|5684    |DigitalDoc|XRay-05.pdf   |null     |
|1234|3345    |DigitalDoc|XRay-02.pdf   |null     |
+----+--------+----------+--------------+---------+

root
 |-- catg: string (nullable = true)
 |-- sub_catg: string (nullable = true)
 |-- doc_name: string (nullable = true)
 |-- revision_label: string (nullable = true)
 |-- extension: string (nullable = true)

2. Extract the required info

 df.withColumn("doc_name", concat_ws("_", col("doc_name"), col("revision_label")))
      .withColumn("extension", substring_index(col("revision_label"), ".", -1))
      .withColumn("revision_label", regexp_extract(col("revision_label"),"""\d+""", 0))
      .show(false)

Output-

+----+--------+----------------------+--------------+---------+
|catg|sub_catg|doc_name              |revision_label|extension|
+----+--------+----------------------+--------------+---------+
|1234|4567    |DigitalDoc_XRay-01.pdf|01            |pdf      |
|2345|5678    |DigitalDoc_CTC-03.png |03            |png      |
|1234|5684    |DigitalDoc_XRay-05.pdf|05            |pdf      |
|1234|3345    |DigitalDoc_XRay-02.pdf|02            |pdf      |
+----+--------+----------------------+--------------+---------+


问:
Cleaning Origin and Destination data with duplicates but different factor level
I have some GIS data with origins and destinations (OD) and an information about the time of the day of the OD. I intending to make a map of this, and to color the ODs by the time of day information.

One thing is that some ODs are in the data set with both day and night and maybe with a different order. I would like to mark those differntly, e.g. "Day/Night"

Is there an easy way to do this? MY MWE is just one OD but I would need to identify it among several others. I can manage to find the duplicates regardless of the order, but I dont know how to find out wether or not there are both time cases there and how to replace them with "Day/Night"

```<code>library(data.table)
Origin<-c("London", "Paris", "Lisbon", "Madrid", "Berlin", "London")
Destination<-c("Paris", "London", "Berlin","Lisbon", "Lisbon", "Paris")
Time=factor(c("Day", "Night", "Day", "Day/Night","Day", "Day/Night"))
dt<-data.table(Origin=Origin, Destination=Destination, Time=Time)
#duplicates regardless of order
dat.sort = t(apply(dt[,.(Origin,Destination)], 1, sort))
dt[duplicated(dat.sort) | duplicated(dat.sort, fromLast=TRUE),]

</code>```

答:
If I understand correctly, the OP wants to

identify city pairs regardless of the order of origin and destination, e.g. London-Paris belongs to the same city pair as Paris-London

collapse separate rows if a city pair is operated Day and Night or Day/Night

or update the original dataset

This is what I would do:

```<code>library(data.table)
dt <- data.table(Origin, Destination, Time)
# add city pair as unique grouping variable
dt[, Pair := paste(pmin(Origin, Destination), pmax(Origin, Destination), sep = "-")][]
# identify city pairs which are operated day and night
pairs_DN <- dt[, all(c("Day", "Night") %in% Time) | "Day/Night" %in% Time, by = Pair][(V1), .(Pair)]
# update original dataset by an update join
dt[pairs_DN, on = "Pair", Time := "Day/Night"][]
</code>```

```<code>   Origin Destination      Time          Pair
1: London       Paris Day/Night  London-Paris
2:  Paris      London Day/Night  London-Paris
3: Lisbon      Berlin       Day Berlin-Lisbon
4: Madrid      Lisbon Day/Night Lisbon-Madrid
5: Berlin      Lisbon       Day Berlin-Lisbon
6: London       Paris Day/Night  London-Paris
</code>```

The key point is to identify the city pairs which fullfil the second requirement:

```<code>dt[, all(c("Day", "Night") %in% Time) | "Day/Night" %in% Time, by = Pair]
</code>```

```<code>            Pair    V1
1:  London-Paris  TRUE
2: Berlin-Lisbon FALSE
3: Lisbon-Madrid  TRUE
</code>```

So, there is no need to deal with factor levels. BTW, factor levels are an attribute of the whole column and do not change when subsetting or grouping. What does change is which of the levels are used in a subset or group.

pairs_DN contains the unique key of those city pairs

```<code>            Pair
1:  London-Paris
2: Lisbon-Madrid
</code>```

Body:
I have some GIS data with origins and destinations (OD) and an information about the time of the day of the OD. I intending to make a map of this, and to color the ODs by the time of day information.

One thing is that some ODs are in the data set with both day and night and maybe with a different order. I would like to mark those differntly, e.g. "Day/Night"

Is there an easy way to do this? MY MWE is just one OD but I would need to identify it among several others. I can manage to find the duplicates regardless of the order, but I dont know how to find out wether or not there are both time cases there and how to replace them with "Day/Night"

library(data.table)
Origin<-c("London", "Paris", "Lisbon", "Madrid", "Berlin", "London")
Destination<-c("Paris", "London", "Berlin","Lisbon", "Lisbon", "Paris")
Time=factor(c("Day", "Night", "Day", "Day/Night","Day", "Day/Night"))
dt<-data.table(Origin=Origin, Destination=Destination, Time=Time)
#duplicates regardless of order
dat.sort = t(apply(dt[,.(Origin,Destination)], 1, sort))
dt[duplicated(dat.sort) | duplicated(dat.sort, fromLast=TRUE),]

Body:
If I understand correctly, the OP wants to

identify city pairs regardless of the order of origin and destination, e.g. London-Paris belongs to the same city pair as Paris-London

collapse separate rows if a city pair is operated Day and Night or Day/Night

or update the original dataset

This is what I would do:

library(data.table)
dt <- data.table(Origin, Destination, Time)
# add city pair as unique grouping variable
dt[, Pair := paste(pmin(Origin, Destination), pmax(Origin, Destination), sep = "-")][]
# identify city pairs which are operated day and night
pairs_DN <- dt[, all(c("Day", "Night") %in% Time) | "Day/Night" %in% Time, by = Pair][(V1), .(Pair)]
# update original dataset by an update join
dt[pairs_DN, on = "Pair", Time := "Day/Night"][]

   Origin Destination      Time          Pair
1: London       Paris Day/Night  London-Paris
2:  Paris      London Day/Night  London-Paris
3: Lisbon      Berlin       Day Berlin-Lisbon
4: Madrid      Lisbon Day/Night Lisbon-Madrid
5: Berlin      Lisbon       Day Berlin-Lisbon
6: London       Paris Day/Night  London-Paris

The key point is to identify the city pairs which fullfil the second requirement:

dt[, all(c("Day", "Night") %in% Time) | "Day/Night" %in% Time, by = Pair]

            Pair    V1
1:  London-Paris  TRUE
2: Berlin-Lisbon FALSE
3: Lisbon-Madrid  TRUE

So, there is no need to deal with factor levels. BTW, factor levels are an attribute of the whole column and do not change when subsetting or grouping. What does change is which of the levels are used in a subset or group.

pairs_DN contains the unique key of those city pairs

            Pair
1:  London-Paris
2: Lisbon-Madrid


问:
Is there a function like millis() of Arduino in python?
Is there a function like millis() of Arduino (not delay() function) in python?
I want to stop a specific part of program for a while ,but not the whole program.</p>

答:
Did you try using </p>

```<code>import time
seconds_to_sleep=1
time.sleep(seconds_to_sleep)
</code>```

Body:
Did you try using

import time
seconds_to_sleep=1
time.sleep(seconds_to_sleep)


问:
Updating column value based on condition with substring
I need to change all values of a column that contain autor substring to author.

Example: My current row is role-autor and this needs to become role-author

Is it possible in liquibase?

答:
Do you just want replace()?

```<code>update t
    set col = replace(col, 'autor', 'author')
    where col like '%autor%';
</code>```

Body:
Do you just want replace()?

update t
    set col = replace(col, 'autor', 'author')
    where col like '%autor%';


答:
Or a tidyverse solution that avoids loops:

```<code>x <- tibble(Value=c(1,1,1,0,0,1,0,0,1,1,2,2,0,0,1,0,0,1,1,1,
                    0,0,2,2,1,1,2,0,0,1,0,2,0,2,1)) %>%
       mutate(ModValue=ifelse(Value == 0, NA, Value)) %>%
       fill(ModValue, .direction="down")

runLengths <- rle(x$ModValue)
groupIndex <- unlist(lapply(1:length(runLengths$lengths),
                            function(x) rep(x, runLengths$lengths[x]))
                    )

x <- x %>% add_column(Group=groupIndex) %>% select(-ModValue)
</code>```

Your input data has a different length to your expected output.  Took me a while to work that out... :)

** Edit **

And an inelegant solution to account for changing days (or other super-groupings...

```<code>x <- tibble(
       RowNumber=1:35,
       Date=lubridate::ymd(c(rep("2020-05-31", 20), rep("2020-06-01", 15))),
       Value=c(1,1,1,0,0,1,0,0,1,1,2,2,0,0,1,0,0,1,1,1,0,0,2,2,1,1,2,0,0,1,0,2,0,2,1))

# Check we have a change of date mid-sequence
x %>% filter(row_number() > 15 & row_number() < 25)

x <- x %>%
       mutate(ModValue=ifelse(Value == 0, NA, Value)) %>%
       fill(ModValue, .direction="down")

# Inelegantly compute the groups
make_groups <- function(x) {
  runs <- rle(x)
  return(tibble(GroupWithinDay=unlist(
                                 lapply(1:length(runs$lengths),
                                        function(x) rep(x, runs$lengths[x])))))
}

y <- x %>% group_by(Date) %>% do(make_groups(.$ModValue))
x <- x %>% add_column(GroupWithinDay=y$GroupWithinDay) %>% select(-ModValue)

# Check the change of date is handled correctly
x %>% filter(row_number() > 15 & row_number() < 25)
</code>```

Giving

```<code># A tibble: 9 x 4
  RowNumber Date       Value GroupWithinDay
      <int> <date>     <dbl>          <int>
1        16 2020-05-31     0              3
2        17 2020-05-31     0              3
3        18 2020-05-31     1              3
4        19 2020-05-31     1              3
5        20 2020-05-31     1              3
6        21 2020-06-01     0              1
7        22 2020-06-01     0              1
8        23 2020-06-01     2              2
9        24 2020-06-01     2              2
</code>```

Title: grouping continuous data with specific pattern

Body:
I have a data frame with a column like this (I am not posting other columns)

Value
1
1
1
0
0
1
0
0
1
1
2
2
0
0
1
0
0
1
1
1
0
0
2
2
1
1
2
0
0
1
0

I am trying to group it based on a specific condition. Grouping has to be done when I have 1 and 2. But conditions like these are one group :

1 1 0 0 1 1 0 0

Basically I need to group occurrences of 1 but in between 0s are allowed

Expected output:

Value   Group
    1   1
    1   1
    1   1
    0   1
    0   1
    1   1
    0   1
    0   1
    1   1
    1   1
    2   2
    2   2
    0   2
    0   2
    1   3
    0   3
    0   3
    1   3
    1   3
    1   3
    0   3
    0   3
    2   4
    2   4
    1   5
    1   5
    2   6
    0   6
    0   6
    1   7
    0   7
    2   8
    0   8
    2   8
    1   9

Body:
Or a tidyverse solution that avoids loops:

x <- tibble(Value=c(1,1,1,0,0,1,0,0,1,1,2,2,0,0,1,0,0,1,1,1,
                    0,0,2,2,1,1,2,0,0,1,0,2,0,2,1)) %>%
       mutate(ModValue=ifelse(Value == 0, NA, Value)) %>%
       fill(ModValue, .direction="down")

runLengths <- rle(x$ModValue)
groupIndex <- unlist(lapply(1:length(runLengths$lengths),
                            function(x) rep(x, runLengths$lengths[x]))
                    )

x <- x %>% add_column(Group=groupIndex) %>% select(-ModValue)

Your input data has a different length to your expected output.  Took me a while to work that out... :)

** Edit **

And an inelegant solution to account for changing days (or other super-groupings...

x <- tibble(
       RowNumber=1:35,
       Date=lubridate::ymd(c(rep("2020-05-31", 20), rep("2020-06-01", 15))),
       Value=c(1,1,1,0,0,1,0,0,1,1,2,2,0,0,1,0,0,1,1,1,0,0,2,2,1,1,2,0,0,1,0,2,0,2,1))

# Check we have a change of date mid-sequence
x %>% filter(row_number() > 15 & row_number() < 25)

x <- x %>%
       mutate(ModValue=ifelse(Value == 0, NA, Value)) %>%
       fill(ModValue, .direction="down")

# Inelegantly compute the groups
make_groups <- function(x) {
  runs <- rle(x)
  return(tibble(GroupWithinDay=unlist(
                                 lapply(1:length(runs$lengths),
                                        function(x) rep(x, runs$lengths[x])))))
}

y <- x %>% group_by(Date) %>% do(make_groups(.$ModValue))
x <- x %>% add_column(GroupWithinDay=y$GroupWithinDay) %>% select(-ModValue)

# Check the change of date is handled correctly
x %>% filter(row_number() > 15 & row_number() < 25)

Giving

# A tibble: 9 x 4
  RowNumber Date       Value GroupWithinDay
      <int> <date>     <dbl>          <int>
1        16 2020-05-31     0              3
2        17 2020-05-31     0              3
3        18 2020-05-31     1              3
4        19 2020-05-31     1              3
5        20 2020-05-31     1              3
6        21 2020-06-01     0              1
7        22 2020-06-01     0              1
8        23 2020-06-01     2              2
9        24 2020-06-01     2              2


问:
Will it be called augmented reality if I render a reality-based 3D model instead of render a camera view?
I've developed an application, and now I wonder if my work can be regarded as augmented reality:

Instead of overlap something on a camera preview, I build and render 3D buildings on a mobile phone. These models are the same (or similar to) some real buildings. I use GPS and gyros to decide the player's position and heading, so that I can render an animation with specified orientation so that the image will "look like" an image captured by the phone camera.

Then I can perform some games related to the real world. For example, I can make 3D models for restored Athenian ruins. When you go Athens and point your phone to the ruins, you'll see its original state on the screen. An early demo video: http://youtu.be/YIX3oZi1O_o

Can I define my application as a kind of augmented reality game, according to general meanings of augmented reality? If not, is there any proper term that can describe my work?

答: In the academic papers applications like this are known as "situated simulations", see the work of Gunnar Liestøl: http://inventioproject.no/sitsim/</p>


问:
Transform array of arrays to a single array
I would like to expand a list of arrays into a single array, so for example:

```<code>a = [array([1,2,3]), array([4,5,6]), array([7,8,9,])]
</code>```

To become:

```<code>a = [array([1,2,3,4,5,6,7,8,9])]
</code>```

How do I do this?

答:
One option is to convert list to np.array and then flatten inside the list:</p>

```<code>>>> import numpy as np
>>> arr = [np.array([1,2,3]), np.array([4,5,6]), np.array([7,8,9,])]
>>> [np.array(arr).flatten()]
[array([1, 2, 3, 4, 5, 6, 7, 8, 9])]
</code>```

Body:
I would like to expand a list of arrays into a single array, so for example:

a = [array([1,2,3]), array([4,5,6]), array([7,8,9,])]

To become:

a = [array([1,2,3,4,5,6,7,8,9])]

How do I do this?

Body:
One option is to convert list to np.array and then flatten inside the list:

>>> import numpy as np
>>> arr = [np.array([1,2,3]), np.array([4,5,6]), np.array([7,8,9,])]
>>> [np.array(arr).flatten()]
[array([1, 2, 3, 4, 5, 6, 7, 8, 9])]


问:
Does the existence of a derived class modify a base class, even if the derived class is never used?
I have a base class, Sample and a derived class SignalSample, and the code for both of these classes normally resides within the same file Sample.py.

I import these classes into my main script in the usual way: from Sample import Sample, SignalSample

While chasing down a bug within one of these classes, I noticed some unusual behavior: deleting the code for the SignalSample derived class changed the behavior of the Signal base class.

So my question is, can the existence of a derived class alter a base class, even if the derived class is never instantiated?

To be concrete,  I tried the following combinations.

The code for Sample and SignalSample  are both in Sample.py. from Sample import Sample, SignalSample is used in my main script to load these classes. Sample  objects are instantiated, there are no SignalSample objects instantiated, the code is just there and unused. In this scenario, I get an error which I will call "type 1".

Delete the code for SignalSample inside Sample.py, and remove the  ... import SignalSample statement. In this case, I get a different error which I'll call  "type 2".

Note that I don't think the errors are coming from the classes themselves (although they may be), it was more that  I found it interesting that the behavior of the code seemed to change because there was an inherited class, even though that class was not used.

This is a stripped-down example of my setup, note that this is not an MWE of the source of my bug, as at the moment I don't know where it is coming from, and so I can't even narrow it down. It's not the solution to the bug I'm looking for, just more information on this seemingly strange behavior of class inheritance.

```<code># file Sample.py
class Sample:
   def __init__(self):
      self._tfile = None
      self._filepath = None

   def calculate_filepath(self):
      return "my/file/path"

   __calculate_filepath = calculate_filepath # private copy

   def get_histogram(self, histogram_name):
      if not self._filepath:
         self._filepath = self.calculate_filepath()
      if not self._tfile:
         from ROOT import TFile # this is a special filetype
         self._tfile = TFile.Open(self._filepath, "READ")

      histo = self._tfile.Get(histogram_name)
      histo.SetDirectory(0)
      self._tfile.Close()
      return histo

class SignalSample(Sample):
   def __init__(self):

      # Inherit
      Sample.__init__(self)

      self._filepath = self.calculate_filepath()

   def calculate_filepath(self):
      # Overloaded version of the function in Sample
      return "my/very/special/filepath"
</code>```

Note that I chose to call the calculate_filepath method inside get_histogram because I wanted to avoid possible namespace clashes with the derived class. This is also why I try to make the method "private" with the namespace mangling. This is also why I open the special TFile file inside the get_histogram method, although it is nice that I can then also Close this file inside the same function. Perhaps this isn't the correct usage and maybe this related to the source of my problem?

答:
get_histogram looks potentially broken if it is called more than once. You assign the opened file to the instance (on self._tfile), but then close it before returning... that means that next time the method is called not self._tfile will probably (*) evaluate to False, meaning that you then try to call Get on a closed file. If you are using a sane library this would probably throw a nice error telling you this, but I see you're using ROOT so who knows what might happen :)

Probably simplest would be not to store the file on Sample, and just open the file whenever get_histogram is called?

(*) Implicit booliness is sometimes worth avoiding. In particular when what you actually want to check is whether something is None, prefer writing if x is None:  ( https://legacy.python.org/dev/peps/pep-0008/#programming-recommendations )

Incidentally, in this example, __calculate_filepath = calculate_filepath # private copy isn't doing anything, since you never actually use it.

Body:
I have a base class, Sample and a derived class SignalSample, and the code for both of these classes normally resides within the same file Sample.py.

I import these classes into my main script in the usual way: from Sample import Sample, SignalSample

While chasing down a bug within one of these classes, I noticed some unusual behavior: deleting the code for the SignalSample derived class changed the behavior of the Signal base class.

So my question is, can the existence of a derived class alter a base class, even if the derived class is never instantiated?

To be concrete,  I tried the following combinations.

The code for Sample and SignalSample  are both in Sample.py. from Sample import Sample, SignalSample is used in my main script to load these classes. Sample  objects are instantiated, there are no SignalSample objects instantiated, the code is just there and unused. In this scenario, I get an error which I will call "type 1".

Delete the code for SignalSample inside Sample.py, and remove the  ... import SignalSample statement. In this case, I get a different error which I'll call  "type 2".

Note that I don't think the errors are coming from the classes themselves (although they may be), it was more that  I found it interesting that the behavior of the code seemed to change because there was an inherited class, even though that class was not used.

This is a stripped-down example of my setup, note that this is not an MWE of the source of my bug, as at the moment I don't know where it is coming from, and so I can't even narrow it down. It's not the solution to the bug I'm looking for, just more information on this seemingly strange behavior of class inheritance.

# file Sample.py
class Sample:
   def __init__(self):
      self._tfile = None
      self._filepath = None

   def calculate_filepath(self):
      return "my/file/path"

   __calculate_filepath = calculate_filepath # private copy

   def get_histogram(self, histogram_name):
      if not self._filepath:
         self._filepath = self.calculate_filepath()
      if not self._tfile:
         from ROOT import TFile # this is a special filetype
         self._tfile = TFile.Open(self._filepath, "READ")

      histo = self._tfile.Get(histogram_name)
      histo.SetDirectory(0)
      self._tfile.Close()
      return histo

class SignalSample(Sample):
   def __init__(self):

      # Inherit
      Sample.__init__(self)

      self._filepath = self.calculate_filepath()

   def calculate_filepath(self):
      # Overloaded version of the function in Sample
      return "my/very/special/filepath"

Note that I chose to call the calculate_filepath method inside get_histogram because I wanted to avoid possible namespace clashes with the derived class. This is also why I try to make the method "private" with the namespace mangling. This is also why I open the special TFile file inside the get_histogram method, although it is nice that I can then also Close this file inside the same function. Perhaps this isn't the correct usage and maybe this related to the source of my problem?


问:
How to get the text value from an element based on its sibling attribute value?
Example xml:

```<code><root>
    <ns1:agent ns2:about="a3">
        <ns2:name xml:lang="en">Helen</ns2:name>
        <ns2:age>26</ns2:age>
    </ns1:agent>
    <ns1:agent ns2:about="a1">
        <ns2:name xml:lang="en">John</ns2:name>
        <ns2:age>26</ns2:age>
    </ns1:agent>
    <ns1:agent ns2:about="a2">
        <ns2:name xml:lang="en">Mike</ns2:name>
        <ns2:age>30</ns2:age>
    </ns1:agent>
    ...
    <!-- More ns1:agent nodes -->
    ...
    <ns3:person ns2:id="a1"/>
    <ns3:person ns2:id="a2"/>
    <ns3:person ns2:id="a3"/>
    ...
    <!-- More ns3:person nodes -->
    ...
</root>
</code>```

So how to get the text value of the agent/name element based on its about attribute starting from person element, using XPath?

Edit:
XPath expression will be used in a loop, for each ns3:person node, so I don't know the specific id attribute value beforehand.

答:
If I understand your question correctly, you are looking for an xpath expression like this:

```<code>    //ns1:agent[@ns2:about=//ns3:person/@ns2:id]/ns2:name
</code>```

To test it, I would change your ns3:persons nodes in your sample xml from

```<code><ns3:person ns2:id="a1"/>
<ns3:person ns2:id="a2"/>
<ns3:person ns2:id="a3"/>
</code>```

to

```<code><ns3:person ns2:id="a7"/>
<ns3:person ns2:id="a2"/>
<ns3:person ns2:id="a9"/>
</code>```

so that only one random ns1:agent corresponds to one random ns3:person. If you do that, that xpath expression would select Mike as the target name.

Body:
Example xml:

<root>
    <ns1:agent ns2:about="a3">
        <ns2:name xml:lang="en">Helen</ns2:name>
        <ns2:age>26</ns2:age>
    </ns1:agent>
    <ns1:agent ns2:about="a1">
        <ns2:name xml:lang="en">John</ns2:name>
        <ns2:age>26</ns2:age>
    </ns1:agent>
    <ns1:agent ns2:about="a2">
        <ns2:name xml:lang="en">Mike</ns2:name>
        <ns2:age>30</ns2:age>
    </ns1:agent>
    ...

    ...
    <ns3:person ns2:id="a1"/>
    <ns3:person ns2:id="a2"/>
    <ns3:person ns2:id="a3"/>
    ...

    ...
</root>

So how to get the text value of the agent/name element based on its about attribute starting from person element, using XPath?

Edit:
XPath expression will be used in a loop, for each ns3:person node, so I don't know the specific id attribute value beforehand.

Body:
If I understand your question correctly, you are looking for an xpath expression like this:

    //ns1:agent[@ns2:about=//ns3:person/@ns2:id]/ns2:name

To test it, I would change your ns3:persons nodes in your sample xml from

<ns3:person ns2:id="a1"/>
<ns3:person ns2:id="a2"/>
<ns3:person ns2:id="a3"/>

to

<ns3:person ns2:id="a7"/>
<ns3:person ns2:id="a2"/>
<ns3:person ns2:id="a9"/>

so that only one random ns1:agent corresponds to one random ns3:person. If you do that, that xpath expression would select Mike as the target name.


问:
Using snake case without explicit Configuration dependency in Circe
As specified in the documentation it is possible to convert snake case to a camel case that is idiomatic in Scala. I tried it and it worked fine. Here is it:

```<code>implicit lazy val configuration: Configuration = Configuration.default.withSnakeCaseMemberNames
@ConfiguredJsonCodec final case class ModelClass(someField1: String, someField2: Int, someField3: String)
</code>```

I want to keep my model clean without adding dependencies on external frameworks so it comprises only business-specific case classes.

Is it possible to avoid addding the annotation @ConfiguredJsonCodec and bringing implicit lazy val configuration: Configuration into scope? Maybe it could be configured on the Decoder level?

答:
It's perfectly possible. It's a trade off:

if you have implicits in your companion objects, you don't have to import them

if you don't want to have coupling with libraries in your models, you have to out all implicits in trait/object and then mixin/import them every single time you need them

If you are developing application with a fixed stack, chosen libraries for each task, and so on - having all implicits in companion is just cleaner and easier to maintain.

```<code>package com.example

package object domain {

  private[domain] implicit lazy val configuration: Configuration = ...
}
</code>```

```<code>package com.example.domain

import io.circe.generic.extra._

@ConfiguredJsonCodec
final case class ModelClass(...)
</code>```

Many utilities are optimized for this e.g. enumeratum-circe uses a mixin to add add codec for enumeration into companion object.

If you don't want to have them there, because e.g. you have your models in one module and it should be dependency-free, then you would have to put these implicits somewhere else. And that requires writing code manually, no way around it:

```<code>package com.example.domain

final case class ModelClass(...)
</code>```

```<code>package com.example.domain.circe

import io.circe._
import io.circe.generic.extra.semiauto._

// if I want a mixin:
//   class SomeClass extends Codecs { ... }
trait Codecs {

  protected implicit lazy val configuration: Configuration = ...

  implicit val modelClassDecoder: Decoder[ModelClass] = deriveConfiguredDecoder[ModelClass]
  implicit val modelClassEncoder: Encoder[ModelClass] = deriveConfiguredEncoder[ModelClass]
}

// if I want an import:
//   import com.example.domain.circe.Codecs._
object Circe extends Circe
</code>```

If you pick this way, you are giving up on e.g. enumeraturm-circe's ability to provide codecs, and you will have to write them manually.

You have to pick one of these depending on your use case, but you cannot have the benefits of both at once: either you give up on boilerplate-reduction or on dependency-reduction.

Body:
As specified in the documentation it is possible to convert snake case to a camel case that is idiomatic in Scala. I tried it and it worked fine. Here is it:

implicit lazy val configuration: Configuration = Configuration.default.withSnakeCaseMemberNames
@ConfiguredJsonCodec final case class ModelClass(someField1: String, someField2: Int, someField3: String)

I want to keep my model clean without adding dependencies on external frameworks so it comprises only business-specific case classes.

Is it possible to avoid addding the annotation @ConfiguredJsonCodec and bringing implicit lazy val configuration: Configuration into scope? Maybe it could be configured on the Decoder level?

Body:
It's perfectly possible. It's a trade off:

if you have implicits in your companion objects, you don't have to import them

if you don't want to have coupling with libraries in your models, you have to out all implicits in trait/object and then mixin/import them every single time you need them

If you are developing application with a fixed stack, chosen libraries for each task, and so on - having all implicits in companion is just cleaner and easier to maintain.

package com.example

package object domain {

  private[domain] implicit lazy val configuration: Configuration = ...
}

package com.example.domain

import io.circe.generic.extra._

@ConfiguredJsonCodec
final case class ModelClass(...)

Many utilities are optimized for this e.g. enumeratum-circe uses a mixin to add add codec for enumeration into companion object.

If you don't want to have them there, because e.g. you have your models in one module and it should be dependency-free, then you would have to put these implicits somewhere else. And that requires writing code manually, no way around it:

package com.example.domain

final case class ModelClass(...)

package com.example.domain.circe

import io.circe._
import io.circe.generic.extra.semiauto._

// if I want a mixin:
//   class SomeClass extends Codecs { ... }
trait Codecs {

  protected implicit lazy val configuration: Configuration = ...

  implicit val modelClassDecoder: Decoder[ModelClass] = deriveConfiguredDecoder[ModelClass]
  implicit val modelClassEncoder: Encoder[ModelClass] = deriveConfiguredEncoder[ModelClass]
}

// if I want an import:
//   import com.example.domain.circe.Codecs._
object Circe extends Circe

If you pick this way, you are giving up on e.g. enumeraturm-circe's ability to provide codecs, and you will have to write them manually.

You have to pick one of these depending on your use case, but you cannot have the benefits of both at once: either you give up on boilerplate-reduction or on dependency-reduction.


问:
Alexa AMAZON.DATE slot default to past dates
I am using the AMAZON.DATE slot and I would like it to default to past dates.

For example, if a user says Monday I would like it to automatically select last Monday rather than next Monday.

In the documentation it confirms that it defaults to 'on or after the current date':

"Utterances that map to a specific date (such as "today", "now", or
  "november twenty-fifth") convert to a complete date: 2015-11-25. Note
  that this defaults to dates on or after the current date (see below
  for more examples)."

For my application there is no way a future date would make sense so it wouldn't be too difficult to manually program this in but I just wondered if there was any other way of doing it?

答: No, you cannot change the behavior of built in slots, so if Alexa resolves user's answer as a date - before passing it to your further processing just subtract 7 from resolved value. It will give you the date in the past.</p>


问:
Python read next()
next() in python does not work. What is an alternative to reading next line in Python? Here is a sample:

```<code>filne = "D:/testtube/testdkanimfilternode.txt"
f = open(filne, 'r+')

while 1:
    lines = f.readlines()
    if not lines:
        break
    for line in lines:
        print line
        if (line[:5] == "anim "):
            print 'next() '
            ne = f.next()
            print ' ne ',ne,'\n'
            break

f.close()
</code>```

Running this on a file does not show 'ne '.

答:
When you do : f.readlines() you already read all the file so f.tell() will show you that you are in the end of the file, and doing f.next() will result in a StopIteration error.

Alternative of what you want to do is:

```<code>filne = "D:/testtube/testdkanimfilternode.txt"

with open(filne, 'r+') as f:
    for line in f:
        if line.startswith("anim "):
            print f.next()
            # Or use next(f, '') to return <empty string> instead of raising a
            # StopIteration if the last line is also a match.
            break
</code>```

Title: Python read next()

Body:
next() in python does not work. What is an alternative to reading next line in Python? Here is a sample:

filne = "D:/testtube/testdkanimfilternode.txt"
f = open(filne, 'r+')

while 1:
    lines = f.readlines()
    if not lines:
        break
    for line in lines:
        print line
        if (line[:5] == "anim "):
            print 'next() '
            ne = f.next()
            print ' ne ',ne,'\n'
            break

f.close()

Running this on a file does not show 'ne '.

Body:
When you do : f.readlines() you already read all the file so f.tell() will show you that you are in the end of the file, and doing f.next() will result in a StopIteration error.

Alternative of what you want to do is:

filne = "D:/testtube/testdkanimfilternode.txt"

with open(filne, 'r+') as f:
    for line in f:
        if line.startswith("anim "):
            print f.next()
            # Or use next(f, '') to return <empty string> instead of raising a
            # StopIteration if the last line is also a match.
            break


问:
Check the type of all elements in a list of list in and that their numerical value falls within a range
I want to loop over each element and check if its type is int and if it lays within range of 12 <= i <= 20
if so return True

I use any to return if all elements are correct and isinstance to catch the type of each element.

The function gives me no output and no bug report so I don't know where the error is.

```<code>listoflist = [[14,13,13,14]]

def list_check(my_list):
    if all(isinstance(i, int) and 12 <= i <= 20 for i in my_list):
        print("true")
        return True
</code>```

答:
Try this

def list_check(my_list_of_lists):
    for my_list in my_list_of_lists:
        if all([isinstance(i, int) and 12 <= i <= 20 for i in my_list]):
            print("true")
            return True
```

Its not clear that you want it always to use list of lists as you post in your example. Or if you want the output to be true if all the lists are true. This will output true as many times as there are lists that fulfill the condition.

Body:
I want to loop over each element and check if its type is int and if it lays within range of 12 <= i <= 20
if so return True

I use any to return if all elements are correct and isinstance to catch the type of each element.

The function gives me no output and no bug report so I don't know where the error is.

listoflist = [[14,13,13,14]]

def list_check(my_list):
    if all(isinstance(i, int) and 12 <= i <= 20 for i in my_list):
        print("true")
        return True

Body:
Try this

def list_check(my_list_of_lists):
    for my_list in my_list_of_lists:
        if all([isinstance(i, int) and 12 <= i <= 20 for i in my_list]):
            print("true")
            return True

Its not clear that you want it always to use list of lists as you post in your example. Or if you want the output to be true if all the lists are true. This will output true as many times as there are lists that fulfill the condition.