问:
Why do I get Android errors on Qt projects loading?
I do not use any Android features of Qt, my only purpose is to program some little softwares for Windows with minGW and QMake, no other project configurations is relevant for my needs. I'm simply curious if I missed a part, it does not affect the fonctionnality.

Project ERROR: You need to set the ANDROID_NDK_ROOT environment variable
  to point to your Android NDK

And a bunch of other errors like that. Its my 4th clean install of 4 different version of Qt bundle, (now I carefully unselect everything except minGW32 and it still showing up!) God's sake why?

Did I missed a part on How to use Qt? Do I need extra-steps to remove it manually on a project creation?

答:
If you want to code for Windows: just setting up your project with default setting (without Android/IOS setting).

If you want to code for Android: The problem was that the file wasn't the correct NDK file. Make sure you get the correct NDK from the website. Other than that it is pretty easy just unzip it and that directory is the root directory.


来源: stackexchange

问:
Material-UI Typography doesn't center in Tool Bar
I'm trying center the text in an AppBar. I've tried centering the text in Typography element, using align="center", textAlign="center", and style={{ align: "center" }}, justify ="center" It still doesnot work. How do I centre the text?</p>

```<code>              return (
               <MuiThemeProvider theme={theme}>
               <React.Fragment>
              <div alignItems="center" justify="center">
               <AppBar position="static">
                 <Toolbar>
                 <Typography style={{ align: "center" }}>
                  Best Practices Management System
                  </Typography>
                  </Toolbar>
                  </AppBar>
                   </div>
</code>```

答:
align='center' should be the solution but you have to use 100% width of parent element.

Update:

```<code>import React from 'react';
import Typography from '@material-ui/core/Typography';
import {makeStyles} from '@material-ui/core/styles';
import {MuiThemeProvider} from "@material-ui/core";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar"
import { createMuiTheme } from '@material-ui/core/styles';
import blue from '@material-ui/core/colors/blue';

const useStyles = makeStyles({
    root: {
        width: '100%',
        maxWidth: 500,
    },
});

const theme = createMuiTheme({
  palette: {
    primary: blue,
  },
});

export default class TypographyExamples extends React.Component {
    render() {
        const {values, handleChange} = this.props;
        return (
            <MuiThemeProvider theme={theme}>
                <React.Fragment>
                    <div className={useStyles.root}>
                        <AppBar position="static">
                            <Toolbar>
                                <Typography gutterBottom align="center" style={{width: "100%", alignItems: "center"}}> abc </Typography>
                            </Toolbar>
                        </AppBar>
                    </div>
                </React.Fragment>
            </MuiThemeProvider>
        );
    }
}
</code>```

Please check this Code Sandbox

ContentLicense: CC BY-SA 4.0

Body:
I'm trying center the text in an AppBar. I've tried centering the text in Typography element, using align="center", textAlign="center", and style={{ align: "center" }}, justify ="center" It still doesnot work. How do I centre the text?

              return (
               <MuiThemeProvider theme={theme}>
               <React.Fragment>

               <AppBar position="static">
                 <Toolbar>
                 <Typography style={{ align: "center" }}>
                  Best Practices Management System
                  </Typography>
                  </Toolbar>
                  </AppBar>

Body:
align='center' should be the solution but you have to use 100% width of parent element.

Update:

import React from 'react';
import Typography from '@material-ui/core/Typography';
import {makeStyles} from '@material-ui/core/styles';
import {MuiThemeProvider} from "@material-ui/core";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar"
import { createMuiTheme } from '@material-ui/core/styles';
import blue from '@material-ui/core/colors/blue';

const useStyles = makeStyles({
    root: {
        width: '100%',
        maxWidth: 500,
    },
});

const theme = createMuiTheme({
  palette: {
    primary: blue,
  },
});

export default class TypographyExamples extends React.Component {
    render() {
        const {values, handleChange} = this.props;
        return (
            <MuiThemeProvider theme={theme}>
                <React.Fragment>
                    <div className={useStyles.root}>
                        <AppBar position="static">
                            <Toolbar>
                                <Typography gutterBottom align="center" style={{width: "100%", alignItems: "center"}}> abc </Typography>
                            </Toolbar>
                        </AppBar>
                    </div>
                </React.Fragment>
            </MuiThemeProvider>
        );
    }
}

Please check this Code Sandbox


问:
Read data from SQL Server 2014 in Jupiter Notebook
I am trying to read data from sql server 2014 AdventureWorks2014 database. i wrote the below code in python jupyter note book

```<code>import pandas
import pyodbc
#Connection sting Diver, server, database, Authentication
connection = pyodbc.connect("Driver ={ODBC Driver 13 for SQL Server};"
                           "Server =JONWAY01\SQLSERVER2014;"
                           "Database=AdventureWorks2014;"
                           "Trusted_Connection=yes;")
#create df
df = pandas.read_sql('SELECT *FROM HumanResources.Employee',connection)
connection.close()
df.head()
</code>```

when i run the code it gives error

```<code>InterfaceError                            Traceback (most recent call last)
<ipython-input-15-8e663d2a3f56> in <module>
      2 import pyodbc
      3 #Connection sting Diver, server, database, Authentication
----> 4 connection = pyodbc.connect("Driver ={ODBC Driver 13 for SQL Server};"
      5                            "Server =JONWAY01\SQLSERVER2014;"
      6                            "Database=AdventureWorks2014;"

InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
</code>```

is there anything i am doing wrong? Please help
Thanks

答:
Believe it or not, you can't have a space after the keyword Driver.  Change it as follows, and so long as you have the ODBC driver installed, you're good-to-go.

```<code>connection = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
                           "Server=JONWAY01\SQLSERVER2014;"
                           "Database=AdventureWorks2014;"
                           "Trusted_Connection=yes;")
</code>```

Body:
I am trying to read data from sql server 2014 AdventureWorks2014 database. i wrote the below code in python jupyter note book

import pandas
import pyodbc
#Connection sting Diver, server, database, Authentication
connection = pyodbc.connect("Driver ={ODBC Driver 13 for SQL Server};"
                           "Server =JONWAY01\SQLSERVER2014;"
                           "Database=AdventureWorks2014;"
                           "Trusted_Connection=yes;")
#create df
df = pandas.read_sql('SELECT *FROM HumanResources.Employee',connection)
connection.close()
df.head()

when i run the code it gives error

InterfaceError                            Traceback (most recent call last)
<ipython-input-15-8e663d2a3f56> in <module>
      2 import pyodbc
      3 #Connection sting Diver, server, database, Authentication
----> 4 connection = pyodbc.connect("Driver ={ODBC Driver 13 for SQL Server};"
      5                            "Server =JONWAY01\SQLSERVER2014;"
      6                            "Database=AdventureWorks2014;"

InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

is there anything i am doing wrong? Please help
Thanks

Body:
Believe it or not, you can't have a space after the keyword Driver.  Change it as follows, and so long as you have the ODBC driver installed, you're good-to-go.

connection = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
                           "Server=JONWAY01\SQLSERVER2014;"
                           "Database=AdventureWorks2014;"
                           "Trusted_Connection=yes;")


问:
How to call oracle stored procedure in spring using entity manager?
```<code>create or replace PROCEDURE TEST
(
Role IN VARCHAR2,
     OUTPUT
 OUT SYS_REFCURSOR
)
AS
BEGIN
 OPEN OUTPUT FOR
Select PC.PRODUCT_NAME,P.PARTNER_NAME
............
.............
  WHERE PC.ROLE_NAME='POS_LEAD';
END TEST;
</code>```

Calling above procedure In Spring using below code

```<code>StoredProcedureQuery findByYearProcedure = entityManager.createStoredProcedureQuery("TEST");

StoredProcedureQuery storedProcedure = findByYearProcedure
            .registerStoredProcedureParameter("Role", String.class, ParameterMode.IN)
            .registerStoredProcedureParameter("SYS_REFCURSOR", Object.class,ParameterMode.REF_CURSOR);
            .setParameter("Role", "admin");

System.out.println(storedProcedure.getResultList());
</code>```

But I am getting an error.

```<code>ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'TEST'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
</code>```

can any one help?

答:
I tried to duplicate your issue but I cannot, though I'm using Postgres instead of Oracle.  Just to be sure, can you do this instead:

```<code>StoredProcedureQuery storedProcedure = findByYearProcedure
            .registerStoredProcedureParameter(1, String.class, ParameterMode.IN)
            .registerStoredProcedureParameter(2, Object.class,ParameterMode.REF_CURSOR);
            .setParameter("Role", "admin");
</code>```

Thanks

Body:
create or replace PROCEDURE TEST
(
Role IN VARCHAR2,
     OUTPUT
 OUT SYS_REFCURSOR
)
AS
BEGIN
 OPEN OUTPUT FOR
Select PC.PRODUCT_NAME,P.PARTNER_NAME
............
.............
  WHERE PC.ROLE_NAME='POS_LEAD';
END TEST;

Calling above procedure In Spring using below code

StoredProcedureQuery findByYearProcedure = entityManager.createStoredProcedureQuery("TEST");

StoredProcedureQuery storedProcedure = findByYearProcedure
            .registerStoredProcedureParameter("Role", String.class, ParameterMode.IN)
            .registerStoredProcedureParameter("SYS_REFCURSOR", Object.class,ParameterMode.REF_CURSOR);
            .setParameter("Role", "admin");

System.out.println(storedProcedure.getResultList());

But I am getting an error.

ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'TEST'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

can any one help?

Body:
I tried to duplicate your issue but I cannot, though I'm using Postgres instead of Oracle.  Just to be sure, can you do this instead:

StoredProcedureQuery storedProcedure = findByYearProcedure
            .registerStoredProcedureParameter(1, String.class, ParameterMode.IN)
            .registerStoredProcedureParameter(2, Object.class,ParameterMode.REF_CURSOR);
            .setParameter("Role", "admin");

Thanks


问:
How to pass my Firebase data from didSelectItemAt to ultimately cellForRowAt (w/o storyboard)
I am able to pull my own data from database and display it to my collectionViewCells... this is the label I have on my CustomCell (UICollectionViewCell) to refer to my Category model (i've only added one label here to simplify what i'm asking :))

```<code>func generateCell(_ category: Category) {

    nameLabel.text = category.name

}
</code>```

and this is what I have on my main UICollectionViewController

```<code>///empty array to collect data
var categoryArray: [Category] = []
</code>```

//

```<code>private func loadCategories() {
    downloadCategoriesFromFireBase { (allCategories) in
        //            print("we have \(allCategories.count)")
        self.categoryArray = allCategories
        self.collectionView.reloadData()
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    loadCategories()
}
</code>```

//CellForItemAt

```<code>override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCell

    cell.generateCell(categoryArray[indexPath.row])

    return cell
}
</code>```

this works like a charm and displays all my cells correctly with all data.

its on didSelectItemAt that lies the problem

```<code>override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let category = categoryArray[indexPath.item]

    let newVC = SomeTableViewController()

    newVC.category?.name = category.name

     navigationController?.pushViewController(newVC, animated: true)

}
</code>```

based on what you can see on didSelectItemAt and after I placed this var category: Category? onto my SomeTableViewController() the categories types still return nil when im trying to print the category name on my viewDidLoad()

How can I pass my category info to the tableViewController after I've selected a certain cell without Storyboard?

答:
You need to pass the entire category object instead of passing the property name to the non initialized newVC category object (which is why your newVC category object is equal to nil).

So, essentially, instead of this:

```<code>newVC.category?.name = category.name
</code>```

Do this:

```<code>newVC.category = category
</code>```

An optional chain (in your case category?.name) will only succeed, if the optionals inside that chain contain a value. In your case, category is still nil since it wasn't assigned a value at the time you were trying to assign a name to it.

To illustrate what you are doing with an example:

```<code>var category: Category! /* category is nil since it's not initialized */
category?.name = name  /* category is still nil, so assigning a value to its property fails */
</code>```

Body:
I am able to pull my own data from database and display it to my collectionViewCells... this is the label I have on my CustomCell (UICollectionViewCell) to refer to my Category model (i've only added one label here to simplify what i'm asking :))

func generateCell(_ category: Category) {

    nameLabel.text = category.name

}

and this is what I have on my main UICollectionViewController

///empty array to collect data
var categoryArray: [Category] = []

//

private func loadCategories() {
    downloadCategoriesFromFireBase { (allCategories) in
        //            print("we have \(allCategories.count)")
        self.categoryArray = allCategories
        self.collectionView.reloadData()
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    loadCategories()
}

//CellForItemAt

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCell

    cell.generateCell(categoryArray[indexPath.row])

    return cell
}

this works like a charm and displays all my cells correctly with all data.

its on didSelectItemAt that lies the problem

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let category = categoryArray[indexPath.item]

    let newVC = SomeTableViewController()

    newVC.category?.name = category.name

     navigationController?.pushViewController(newVC, animated: true)

}

based on what you can see on didSelectItemAt and after I placed this var category: Category? onto my SomeTableViewController() the categories types still return nil when im trying to print the category name on my viewDidLoad()

How can I pass my category info to the tableViewController after I've selected a certain cell without Storyboard?

Body:
You need to pass the entire category object instead of passing the property name to the non initialized newVC category object (which is why your newVC category object is equal to nil).

So, essentially, instead of this:

newVC.category?.name = category.name

Do this:

newVC.category = category

An optional chain (in your case category?.name) will only succeed, if the optionals inside that chain contain a value. In your case, category is still nil since it wasn't assigned a value at the time you were trying to assign a name to it.

To illustrate what you are doing with an example:

var category: Category! /* category is nil since it's not initialized */
category?.name = name  /* category is still nil, so assigning a value to its property fails */


问:
Stop toggle in treenode in winforms
i have a treeview in winforms . When i double click on treenode ,its childnode gets disappear .Again when i click on that very node its child nodes gets expand. Any body please help me out.</p>

答:
If you want to disable double click alltogether you will have handle directly the WM_LBUTTONDBLCLK (0x0203). To  do that create a MyTreeView control inheriting from System.Windows.Forms.TreeView and override the WndProc(ref Message m) method.

```<code> public class MyTreeView : TreeView
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x203) { m.Result = IntPtr.Zero; } //Makes the control ignore double licks`
        else base.WndProc(ref m);
    }
};
</code>```

This solution will disable completely double clicks on all the TreeView control. If you can live with that, this solution will do.

ContentLicense: CC BY-SA 3.0

Title: Stop toggle in treenode in winforms

Body:
If you want to disable double click alltogether you will have handle directly the WM_LBUTTONDBLCLK (0x0203). To  do that create a MyTreeView control inheriting from System.Windows.Forms.TreeView and override the WndProc(ref Message m) method.

 public class MyTreeView : TreeView
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x203) { m.Result = IntPtr.Zero; } //Makes the control ignore double licks`
        else base.WndProc(ref m);
    }
};

This solution will disable completely double clicks on all the TreeView control. If you can live with that, this solution will do.


问:
AttributeError: module 'numba' has no attribute 'targets'
I'm trying to utilize Numba engine when doing rolling.apply computation with pandas and it keeps throwing the following error:

```<code>AttributeError: module 'numba' has no attribute 'targets'
</code>```

I'm new to Numba and might have written the code incorrectly (see the code below). How can I improve this code?. I'm using Numba as my dataframe which is huge (more than 3 million rows) and would like to see if Numba can help to make my runtime more efficient.

```<code>s = re.groupby(["id","c_id"]).label.rolling("7d").apply(sum, raw=True, engine='numba')
</code>```

答:
It's probably because your Numba installation is too new for your Pandas installation. (https://github.com/numba/numba/issues/5772)

Numba <= 0.48 with Pandas <= 1.0.3

Numba >= 0.49 with Pandas >= 1.0.4

Body:
I'm trying to utilize Numba engine when doing rolling.apply computation with pandas and it keeps throwing the following error:

AttributeError: module 'numba' has no attribute 'targets'

I'm new to Numba and might have written the code incorrectly (see the code below). How can I improve this code?. I'm using Numba as my dataframe which is huge (more than 3 million rows) and would like to see if Numba can help to make my runtime more efficient.

s = re.groupby(["id","c_id"]).label.rolling("7d").apply(sum, raw=True, engine='numba')


问:
Getting a multi-dimensional array from a text file in powershell
So, this should be a simple one but I am scratching my head with it.
I have an array that I need passed in this format

```<code>$itemArray = @(('Valley1','111.111.111'), ('Valley2','111.111.112'), ('Valley3','111.111.113'));
</code>```

And I need to get it from a text file that is formatted like this

```<code>Valley1,111.111.111
Valley2,111.111.112
Valley3,111.111.113
</code>```

My code is currently throwing an error of "Cannot convert argument "items" with value "System.Object[]"
So I am trying to figure out the best way to get from point A, the text file, to point B, the $itemArray format I need with that data, attached is the code below, thanks for looking!

https://pastebin.com/nyMtcczF

答:
Using the text file.

```<code>Valley1,111.111.111
Valley2,111.111.112
Valley3,111.111.113
</code>```

This should work.

```<code>$Arrays = @()
Get-Content C:\TEST\Test.txt | ForEach-Object{
    $Arrays += , @($_ -split ",")
}

$Arrays[0]
</code>```

This would return...

```<code>Valley1
111.111.111
</code>```

Body:
So, this should be a simple one but I am scratching my head with it.
I have an array that I need passed in this format

$itemArray = @(('Valley1','111.111.111'), ('Valley2','111.111.112'), ('Valley3','111.111.113'));

And I need to get it from a text file that is formatted like this

Valley1,111.111.111
Valley2,111.111.112
Valley3,111.111.113

My code is currently throwing an error of "Cannot convert argument "items" with value "System.Object[]"
So I am trying to figure out the best way to get from point A, the text file, to point B, the $itemArray format I need with that data, attached is the code below, thanks for looking!

https://pastebin.com/nyMtcczF

Body:
Using the text file.

Valley1,111.111.111
Valley2,111.111.112
Valley3,111.111.113

This should work.

$Arrays = @()
Get-Content C:\TEST\Test.txt | ForEach-Object{
    $Arrays += , @($_ -split ",")
}

$Arrays[0]

This would return...

Valley1
111.111.111


问:
Can anyone give me an example of sdpTransform option in simplepeer
Hi I've been searching for an example of how to use sdpTransform feature in simple peer and haven't found anything yet. Also is there any way to check what codecs were used in mediaStream object. Is there anyway to create custom sdp offer in SimplePeer.</p>

答: the parameter must contain a function that returns the transformed sdp. Found this github issue as example. github</p>


问:
No module named 'tensorflow.python._impl
I want to run code for try neural network with python but
No module named 'tensorflow.python._impl

part of my code:

import tensorflow as tf
from tensorflow import keras
from tensorflow.python._impl import activations

I did all solutions that some sites said such as
 - Uninstall and install package like tensorflow,..
also:
 - Upgrade tensorflow
 - Upgrade keras

but it doesn't solve yet. I have this error on co lab too.
please help me if you have any solution

thanks

答:
```<code>from keras.layers.recurrent import ...
</code>```

Body: from keras.layers.recurrent import ...


问:
How to define documents order in envelope using REST API?
If I have 3 documents: doc1.doc, doc2.doc, doc3.doc, but within the DocuSign Sender View, and also more importantly in the DocuSign Signing Ceremony, I want to be able to define what order those three documents are displayed in, how do I achieve this. For example, I may want them to appear specifically in the following order:

doc2.doc,
doc3.doc
doc1.doc

I am using the REST API, and so I imagined there must be a way of me specifying when I create my envelope definition, which order the enclosed documents should appear in? I noticed that there is an 'Order' property on the document object - great, but assigning values such as "1", "2", "3" against the relevant docs seems to make no difference, so perhaps this 'Order' property against the document object is not for the purpose I've outlined above?

Also, just to confirm, I am not referring to the 'signing order', which I have working fine via the recipient routing property.

At present, the documents always appear within the sender view or signing ceremony in the order in which they appear within the envelope (the order in which I added them to the envelope, regardless of 'Order' or 'DocumentId' properties). Is that just how it works (so I need to put the documents in the envelope in the physical order in which I want them to appear)?

答: Yes, you answered your own questions. Documents are added and shown in the order they're added in. Like a list or array (in some languages). You can change the order by removing and re-adding documents in the correct order.</p>


问:
How can I increase my performance with Eigen? (sample code included)
Is there anything overly inefficient with my matrix math in the following function? I have optimizations enabled in visual studio, and am building in 64 bit release mode. I am feeding rather large matricies through here(750X350), but my performance seems slow none-the-less. </p>

```<code>void NeuralNetwork::backward(Eigen::MatrixXf back, float learningRate)
{
    std::vector<Eigen::MatrixXf> dedw;
    std::vector<Eigen::MatrixXf> delta;

    int nLayers = layers.size();

    int dIt = 0;
    for (int i = nLayers -1;i > 0; i=i-1)
    {
        Eigen::MatrixXf deltai;
        logger->LogVerbose("--------","Back", i,"---------");
        if (i == (nLayers - 1))
        {
            deltai.noalias() = back.cwiseProduct(dSigmoid(X[i]));
            delta.push_back(deltai);
            dIt++;

        }
        else
        {
            logger->LogVerbose("W", i);
            deltai.noalias() = (W[i].transpose() * delta[dIt - 1]).cwiseProduct(dSigmoid(X[i]));
            delta.push_back(deltai);
            dIt++;
        }
        Eigen::MatrixXf dedwi;
        dedwi.noalias() = delta[dIt - 1] * X[i - 1].transpose();
        dedw.push_back(dedwi);
        logger->LogVerbose("dedw", dIt - 1, dedw[dIt - 1]);

        Eigen::MatrixXf WiNew;
        WiNew.noalias() = W[i - 1] - learningRate * dedw[dIt - 1];
        W[i-1] = WiNew;
        logger->LogVerbose("W", i - 1);
        logger->LogVerbose(W[i - 1]);

        Eigen::MatrixXf BiNew;
        BiNew.noalias() = B[i - 1] - learningRate * delta[dIt - 1];
        B[i-1] = BiNew;
        logger->LogVerbose("B", i - 1);
        logger->LogVerbose(B[i - 1]);

    }
}
</code>```

答:
Avoid memory-intensive operations in performance-sensitive code.

Never pass "big arguments" by value.

For instance

```<code>void NeuralNetwork::backward(Eigen::MatrixXf back, float learningRate)
</code>```

should probably be

```<code>void NeuralNetwork::backward(const Eigen::MatrixXf& back, float learningRate)
</code>```

This avoids copy-constructing the parameter again and again in the function.

Avoid copying inside the function. You push to delta and dedw vectors matrices that are temporary. You probably want to move them like this: delta.push_back(std::move(deltai));

Reuse allocated memory. You can probably reuse delta and dedw workspace vectors. Even if to minimize the unnecessary allocations-deallocations. If you don't need the elements from the previous call -- simply clear them.

These would be the first things to do. And finally -- profile!

I would use perf record to analyze the bottlenecks in the program. The best way to squeeze out more performance is by fixing the biggest bottlenecks. At this point you don't know where you biggest bottleneck is. So profile, profile, profile.

Body:
Is there anything overly inefficient with my matrix math in the following function? I have optimizations enabled in visual studio, and am building in 64 bit release mode. I am feeding rather large matricies through here(750X350), but my performance seems slow none-the-less.

void NeuralNetwork::backward(Eigen::MatrixXf back, float learningRate)
{
    std::vector<Eigen::MatrixXf> dedw;
    std::vector<Eigen::MatrixXf> delta;

    int nLayers = layers.size();

    int dIt = 0;
    for (int i = nLayers -1;i > 0; i=i-1)
    {
        Eigen::MatrixXf deltai;
        logger->LogVerbose("--------","Back", i,"---------");
        if (i == (nLayers - 1))
        {
            deltai.noalias() = back.cwiseProduct(dSigmoid(X[i]));
            delta.push_back(deltai);
            dIt++;

        }
        else
        {
            logger->LogVerbose("W", i);
            deltai.noalias() = (W[i].transpose() * delta[dIt - 1]).cwiseProduct(dSigmoid(X[i]));
            delta.push_back(deltai);
            dIt++;
        }
        Eigen::MatrixXf dedwi;
        dedwi.noalias() = delta[dIt - 1] * X[i - 1].transpose();
        dedw.push_back(dedwi);
        logger->LogVerbose("dedw", dIt - 1, dedw[dIt - 1]);

        Eigen::MatrixXf WiNew;
        WiNew.noalias() = W[i - 1] - learningRate * dedw[dIt - 1];
        W[i-1] = WiNew;
        logger->LogVerbose("W", i - 1);
        logger->LogVerbose(W[i - 1]);

        Eigen::MatrixXf BiNew;
        BiNew.noalias() = B[i - 1] - learningRate * delta[dIt - 1];
        B[i-1] = BiNew;
        logger->LogVerbose("B", i - 1);
        logger->LogVerbose(B[i - 1]);

    }
}

Body:
Avoid memory-intensive operations in performance-sensitive code.

Never pass "big arguments" by value.

For instance

void NeuralNetwork::backward(Eigen::MatrixXf back, float learningRate)

should probably be

void NeuralNetwork::backward(const Eigen::MatrixXf& back, float learningRate)

This avoids copy-constructing the parameter again and again in the function.

Avoid copying inside the function. You push to delta and dedw vectors matrices that are temporary. You probably want to move them like this: delta.push_back(std::move(deltai));

Reuse allocated memory. You can probably reuse delta and dedw workspace vectors. Even if to minimize the unnecessary allocations-deallocations. If you don't need the elements from the previous call -- simply clear them.

These would be the first things to do. And finally -- profile!

I would use perf record to analyze the bottlenecks in the program. The best way to squeeze out more performance is by fixing the biggest bottlenecks. At this point you don't know where you biggest bottleneck is. So profile, profile, profile.


问:
How to split array into chunks of given size? (JavaScript)
I'm trying to work out how I can get such senario to work:

```<code>MEAL DEAL: $4.99 - Total 3 items
Must includ 1 of each "DRINK, SANCKS, SANDWICH"
</code>```

I have this array:

```<code>[
  { "CAT": "DRINKS", "QTY": 1, "PRICE": 1.2 },
  { "CAT": "DRINKS", "QTY": 1, "PRICE": 1.5 },
  { "CAT": "SANCKS", "QTY": 1, "PRICE": 2.5 },
  { "CAT": "SANCKS", "QTY": 1, "PRICE": 3.5 },
  { "CAT": "SANCKS", "QTY": 1, "PRICE": 1.5 },
  { "CAT": "SANDWICH", "QTY": 1, "PRICE": 5.3 },
  { "CAT": "SANDWICH", "QTY": 1, "PRICE": 3.0 }
]
</code>```

Based on array above, I need to split it into chunks of 3 elements to create a meal deal.

Looking at array above (7 elements) we can only have 2x Meal deals with 1 item "SNACK" that can't be part of the meal deal just yet because its missing other 2 necessary elements.

I have tried the forEach loop but it only works for initial meal deal and than rest it dont work out.

```<code>var PER_CATEGORY_SET ={}
var ITM = "";
MY_ARRAY.forEach(function (my_item){
    var xx = MY_ARRAY.length;

    while (xx > 0) {
        if(ITM !=  my_item.CAT ){
           ITM = my_item.CAT;
           PER_CATEGORY_SET.push({'CAT':my_item.CAT,'QTY':my_item.QTY,'PRICE':my_item.PRICE});
           MY_ARRAY.splice(0, 1);
        }
        xx--;
    }
});
console.log(PER_CATEGORY_SET);
</code>```

Any help would be very much appreciated, I have tried searching for examples to help me work out how I can do it myself (I'm not looking for a copy & paste code)

答:
You could group the objects, based on cat and get a cartesian product of it.

var data = [{ CAT: "DRINKS", QTY: 1, PRICE: 1.2 }, { CAT: "DRINKS", QTY: 1, PRICE: 1.5 }, { CAT: "SANCKS", QTY: 1, PRICE: 2.5 }, { CAT: "SANCKS", QTY: 1, PRICE: 3.5 }, { CAT: "SANCKS", QTY: 1, PRICE: 1.5 }, { CAT: "SANDWICH", QTY: 1, PRICE: 5.3 }, { CAT: "SANDWICH", QTY: 1, PRICE: 3 }],
    result = Object
        .values(data.reduce((r, o) => ((r[o.CAT] = r[o.CAT] || []).push(o), r), {}))
        .reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));

console.log(result);```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.as-console-wrapper { max-height: 100% !important; top: 0; }</code>```

Body:
I'm trying to work out how I can get such senario to work:

MEAL DEAL: $4.99 - Total 3 items
Must includ 1 of each "DRINK, SANCKS, SANDWICH"

I have this array:

[
  { "CAT": "DRINKS", "QTY": 1, "PRICE": 1.2 },
  { "CAT": "DRINKS", "QTY": 1, "PRICE": 1.5 },
  { "CAT": "SANCKS", "QTY": 1, "PRICE": 2.5 },
  { "CAT": "SANCKS", "QTY": 1, "PRICE": 3.5 },
  { "CAT": "SANCKS", "QTY": 1, "PRICE": 1.5 },
  { "CAT": "SANDWICH", "QTY": 1, "PRICE": 5.3 },
  { "CAT": "SANDWICH", "QTY": 1, "PRICE": 3.0 }
]

Based on array above, I need to split it into chunks of 3 elements to create a meal deal.

Looking at array above (7 elements) we can only have 2x Meal deals with 1 item "SNACK" that can't be part of the meal deal just yet because its missing other 2 necessary elements.

I have tried the forEach loop but it only works for initial meal deal and than rest it dont work out.

var PER_CATEGORY_SET ={}
var ITM = "";
MY_ARRAY.forEach(function (my_item){
    var xx = MY_ARRAY.length;

    while (xx > 0) {
        if(ITM !=  my_item.CAT ){
           ITM = my_item.CAT;
           PER_CATEGORY_SET.push({'CAT':my_item.CAT,'QTY':my_item.QTY,'PRICE':my_item.PRICE});
           MY_ARRAY.splice(0, 1);
        }
        xx--;
    }
});
console.log(PER_CATEGORY_SET);

Any help would be very much appreciated, I have tried searching for examples to help me work out how I can do it myself (I'm not looking for a copy & paste code)

Body:
You could group the objects, based on cat and get a cartesian product of it.

var data = [{ CAT: "DRINKS", QTY: 1, PRICE: 1.2 }, { CAT: "DRINKS", QTY: 1, PRICE: 1.5 }, { CAT: "SANCKS", QTY: 1, PRICE: 2.5 }, { CAT: "SANCKS", QTY: 1, PRICE: 3.5 }, { CAT: "SANCKS", QTY: 1, PRICE: 1.5 }, { CAT: "SANDWICH", QTY: 1, PRICE: 5.3 }, { CAT: "SANDWICH", QTY: 1, PRICE: 3 }],
    result = Object
        .values(data.reduce((r, o) => ((r[o.CAT] = r[o.CAT] || []).push(o), r), {}))
        .reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }


问:
Android Random string generated from int
Basically Im trying to generate a random string when the page loads up. The problem is I dont know how to make the created random integer as a variable for my switch case. Can anyone modify my code and help me?

CODE

```<code>import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ask_2 extends Activity {

TextView ActivityNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ask2);

    ChooseActivity();
}

private void ChooseActivity() {
    Random myRandom = new Random();
    TextView textGenerateNumber = (TextView) findViewById(R.id.generatenumber);
    textGenerateNumber.setText(String.valueOf(myRandom.nextInt(2)));
    TextView textGenerateDesc = (TextView) findViewById(R.id.generatedesc);
    switch (myRandom) {
    case 0:
        textGenerateDesc.setText("Hello0");
        break;
    case 1:
        textGenerateDesc.setText("Hello1");
        break;
    case 2:
        textGenerateDesc.setText("Hello2");
        break;
    }

}
}
</code>```

XML

```<code><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Activity " />
<TextView android:id="@+id/generatenumber"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Number" />
<TextView android:id="@+id/generatedesc" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Description" />

</LinearLayout>
</code>```

答:
In your code, you have myRandom.nextInt(2)). This will generate 0 or 1, but in your switch you check for values of 0, 1 or 2. I assume you want one of these values.

```<code>Random myRandom = new Random();
int randomNumber = myRandom.nextInt(3);
TextView textGenerateNumber = (TextView) findViewById(R.id.generatenumber);
textGenerateNumber.setText(String.valueOf(randomNumber));
</code>```

Body:
Basically Im trying to generate a random string when the page loads up. The problem is I dont know how to make the created random integer as a variable for my switch case. Can anyone modify my code and help me?

CODE

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ask_2 extends Activity {

TextView ActivityNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ask2);

    ChooseActivity();
}

private void ChooseActivity() {
    Random myRandom = new Random();
    TextView textGenerateNumber = (TextView) findViewById(R.id.generatenumber);
    textGenerateNumber.setText(String.valueOf(myRandom.nextInt(2)));
    TextView textGenerateDesc = (TextView) findViewById(R.id.generatedesc);
    switch (myRandom) {
    case 0:
        textGenerateDesc.setText("Hello0");
        break;
    case 1:
        textGenerateDesc.setText("Hello1");
        break;
    case 2:
        textGenerateDesc.setText("Hello2");
        break;
    }

}
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Activity " />
<TextView android:id="@+id/generatenumber"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Number" />
<TextView android:id="@+id/generatedesc" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Description" />

</LinearLayout>

Body:
In your code, you have myRandom.nextInt(2)). This will generate 0 or 1, but in your switch you check for values of 0, 1 or 2. I assume you want one of these values.

Random myRandom = new Random();
int randomNumber = myRandom.nextInt(3);
TextView textGenerateNumber = (TextView) findViewById(R.id.generatenumber);
textGenerateNumber.setText(String.valueOf(randomNumber));


问:
My developer app problem with opening database on Android 9
Can you help my?

I have created and published the app on Google play store that contains a database, I have tested it on different android phones. My app works perfect on android version (Android 10,8,5 etc...) except on Android 9, do you know how can I fix that problem? I find that problem on Huawei Smart Z, Honor P20, Samsung Y6,Xiaomi Mi. Play store console says that my app can run on that models. I am beginner in app development.

This happening on mine Huawei Smart: when I install the app it opens and crashes when I want to enter in database, and when I want to enter in app again it crashes and on logcat:

```<code>E/Path 1: data/data/com.gema/databases/
E/SQLiteLog: (14) cannot open file at line 36906 of [c255889bd9]
E/SQLiteLog: (14) os_unix.c:36906: (2) open(//data/data/com.gematranslate/databases/gema_recnik.db) -
E/SQLiteDatabase: Failed to open database 'data/data/com.gematranslate/databases/gema_recnik.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (Sqlite code 14 SQLITE_CANTOPEN): Could not open database, (OS error - 2:No such file or directory)
</code>```

In android manifest I put:

```<code><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</code>```

This is my code for check db:

```<code>public boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);
        myContext.getDatabasePath(DB_NAME).getPath();
        checkDB.disableWriteAheadLogging();
    } catch (SQLException e)
    {
        //
    }
    if (checkDB!= null){
        checkDB.close();
    }
    return checkDB != null? true : false;
</code>```

This is my code for opening db:

```<code>public void openDataBase() {
    String myPath = DB_PATH + DB_NAME;
    myContext.getDatabasePath(DB_NAME).getPath();
    db = SQLiteDatabase.openDatabase(myPath,null, SQLiteDatabase.OPEN_READWRITE);
    db.disableWriteAheadLogging();
</code>```

If anyone can help me, thanks a lot.

答:
This is a solution that is supported on all versions of Android

```<code>public class DataBase extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "database.db";
private static String DATABASE_PATH = "";
private static String LAST_DATABASE_PATH = "";
private SQLiteDatabase mDataBase;
private final Context ctx;

public DataBase2(@Nullable Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    DATABASE_PATH = context.getDatabasePath(DATABASE_NAME).getPath();
    this.ctx = context;

    if (!checkIfDBExists()) {
        createDataBase();
    }

    File dbFile = new File(Environment.getDataDirectory().getPath()+"/data/"+ BuildConfig.APPLICATION_ID +"/databases");
    if (dbFile.isDirectory()) {
        String[] child = dbFile.list();
        assert child != null;
        for (int i = 0; i < child.length; i++) {
            if (!(child[i].equals("database.db") || child[i].equals(DATABASE_NAME))) {
                new File(dbFile, child[i]).delete();
            }
        }
    }
}

public void createDataBase() {
    this.getReadableDatabase();
    this.close();
    try {
        copyDataBase();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    try {
        File DbFile = ctx.getDatabasePath(DATABASE_NAME);
        if (DbFile.exists()) {
            copyDataBase();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private boolean checkIfDBExists() {
    File dbFile = new File(DATABASE_PATH);
    return dbFile.exists();
}

public void openDataBase() {
    close();
    mDataBase = SQLiteDatabase.openDatabase(DATABASE_PATH, null, CREATE_IF_NECESSARY);
}

private void copyDataBase() throws IOException {
    InputStream mInput = ctx.getAssets().open("database.db");
    String outfileName = DATABASE_PATH;
    OutputStream mOutput = new FileOutputStream(outfileName);
    byte[] buffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(buffer)) > 0) {
        mOutput.write(buffer, 0, mLength);
    }
    mOutput.flush();
    mInput.close();
    mOutput.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public synchronized void close() {
    if (mDataBase != null)
        mDataBase.close();

    SQLiteDatabase.releaseMemory();
    super.close();
}
</code>```

}

Body:
Can you help my?

I have created and published the app on Google play store that contains a database, I have tested it on different android phones. My app works perfect on android version (Android 10,8,5 etc...) except on Android 9, do you know how can I fix that problem? I find that problem on Huawei Smart Z, Honor P20, Samsung Y6,Xiaomi Mi. Play store console says that my app can run on that models. I am beginner in app development.

This happening on mine Huawei Smart: when I install the app it opens and crashes when I want to enter in database, and when I want to enter in app again it crashes and on logcat:

E/Path 1: data/data/com.gema/databases/
E/SQLiteLog: (14) cannot open file at line 36906 of [c255889bd9]
E/SQLiteLog: (14) os_unix.c:36906: (2) open(//data/data/com.gematranslate/databases/gema_recnik.db) -
E/SQLiteDatabase: Failed to open database 'data/data/com.gematranslate/databases/gema_recnik.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (Sqlite code 14 SQLITE_CANTOPEN): Could not open database, (OS error - 2:No such file or directory)

In android manifest I put:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

This is my code for check db:

public boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);
        myContext.getDatabasePath(DB_NAME).getPath();
        checkDB.disableWriteAheadLogging();
    } catch (SQLException e)
    {
        //
    }
    if (checkDB!= null){
        checkDB.close();
    }
    return checkDB != null? true : false;

This is my code for opening db:

public void openDataBase() {
    String myPath = DB_PATH + DB_NAME;
    myContext.getDatabasePath(DB_NAME).getPath();
    db = SQLiteDatabase.openDatabase(myPath,null, SQLiteDatabase.OPEN_READWRITE);
    db.disableWriteAheadLogging();

If anyone can help me, thanks a lot.

Body:
This is a solution that is supported on all versions of Android

public class DataBase extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "database.db";
private static String DATABASE_PATH = "";
private static String LAST_DATABASE_PATH = "";
private SQLiteDatabase mDataBase;
private final Context ctx;

public DataBase2(@Nullable Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    DATABASE_PATH = context.getDatabasePath(DATABASE_NAME).getPath();
    this.ctx = context;

    if (!checkIfDBExists()) {
        createDataBase();
    }

    File dbFile = new File(Environment.getDataDirectory().getPath()+"/data/"+ BuildConfig.APPLICATION_ID +"/databases");
    if (dbFile.isDirectory()) {
        String[] child = dbFile.list();
        assert child != null;
        for (int i = 0; i < child.length; i++) {
            if (!(child[i].equals("database.db") || child[i].equals(DATABASE_NAME))) {
                new File(dbFile, child[i]).delete();
            }
        }
    }
}

public void createDataBase() {
    this.getReadableDatabase();
    this.close();
    try {
        copyDataBase();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    try {
        File DbFile = ctx.getDatabasePath(DATABASE_NAME);
        if (DbFile.exists()) {
            copyDataBase();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private boolean checkIfDBExists() {
    File dbFile = new File(DATABASE_PATH);
    return dbFile.exists();
}

public void openDataBase() {
    close();
    mDataBase = SQLiteDatabase.openDatabase(DATABASE_PATH, null, CREATE_IF_NECESSARY);
}

private void copyDataBase() throws IOException {
    InputStream mInput = ctx.getAssets().open("database.db");
    String outfileName = DATABASE_PATH;
    OutputStream mOutput = new FileOutputStream(outfileName);
    byte[] buffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(buffer)) > 0) {
        mOutput.write(buffer, 0, mLength);
    }
    mOutput.flush();
    mInput.close();
    mOutput.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public synchronized void close() {
    if (mDataBase != null)
        mDataBase.close();

    SQLiteDatabase.releaseMemory();
    super.close();
}

}


问:
How does.format() in this code work [pandas]
I am very much a beginner in pandas posting here for some help.
I was doing an assignment on Coursera, and after I did it I tried searching stack overflow to find more compact and faster codes. Stuff I saw on here I understood but when I did a search on Google I found some code on pastebin that looked too good to be true.

Here it is

```<code>def convert_housing_data_to_quarters():
    data = pd.read_csv('City_Zhvi_AllHomes.csv')
    towns = data[['State','RegionName']]
    for yr in range(2000,2017):
        for q in range(1,12,3):
            if yr == 2016 and q == 4:
                break
            m1 = '{}-{:02d}'.format(yr,q)
            m2 = '{}-{:02d}'.format(yr,q+1)
            m3 = '{}-{:02d}'.format(yr,q+2)
            quarter = '{}q{}'.format(yr,q)
            towns[quarter] = data[[m1,m2,m3]].mean(axis=1)
    return towns
</code>```

The assignment was to read a .csv with data for each month and to make a dataframe with quarters from 2001q1 to the end each quarter has a value of the mean of the 3 months that make it up.
My solution was very normal and looking at code others wrote for this assignment it looked nearly similar.

The code linked up works for everything except that it named the columns as 20xxq1,q4,q7 and q10 instead of q1,2,3,4.
I don't understand how the code in the 2nd for loop works.

What does this syntax mean '{}-{:02d}'? I don't get this at all but it seems extremely powerful. I would love to know how to use it in the future. I saw the pandas documentation, it says you can use a dict as parameters for .format() but I really can't understand how its working here.

Thanks for any help.

答:
range(1,12,3) gives the following result: 1, 4, 7, 10. It just iterates through the values from 1 to 12 with a step 3. If I understood it correctly, this code just outputs the quarters according to its first month. So, for example, the first quarter starts with the first month, then goes forth month and so on.

'{}-{:02d}'.format(value1, value2) just puts whatever values you put in format() inside those curly brackets. There is now a better way for formatting strings: f'{value1}--{value2}'.

{:02d} just prints numbers with a width of two (here is a similar question)

Hope it helps

Body:
I am very much a beginner in pandas posting here for some help.
I was doing an assignment on Coursera, and after I did it I tried searching stack overflow to find more compact and faster codes. Stuff I saw on here I understood but when I did a search on Google I found some code on pastebin that looked too good to be true.

Here it is

def convert_housing_data_to_quarters():
    data = pd.read_csv('City_Zhvi_AllHomes.csv')
    towns = data[['State','RegionName']]
    for yr in range(2000,2017):
        for q in range(1,12,3):
            if yr == 2016 and q == 4:
                break
            m1 = '{}-{:02d}'.format(yr,q)
            m2 = '{}-{:02d}'.format(yr,q+1)
            m3 = '{}-{:02d}'.format(yr,q+2)
            quarter = '{}q{}'.format(yr,q)
            towns[quarter] = data[[m1,m2,m3]].mean(axis=1)
    return towns

The assignment was to read a .csv with data for each month and to make a dataframe with quarters from 2001q1 to the end each quarter has a value of the mean of the 3 months that make it up.
My solution was very normal and looking at code others wrote for this assignment it looked nearly similar.

The code linked up works for everything except that it named the columns as 20xxq1,q4,q7 and q10 instead of q1,2,3,4.
I don't understand how the code in the 2nd for loop works.

What does this syntax mean '{}-{:02d}'? I don't get this at all but it seems extremely powerful. I would love to know how to use it in the future. I saw the pandas documentation, it says you can use a dict as parameters for .format() but I really can't understand how its working here.

Thanks for any help.


问:
dask - Applying a function over a large dataframe which is more than RAM
It is believed that Dask framework is capable of handling datasets which are more than RAM in size. Nevertheless, I wasn't able to successfully apply it to my problem, which sounds like this:

I have a huge .csv file (1.8Gb), which contains texts of users' comments, and a RAM of 8Gb. The goal is to preprocess the given data (to tokenize sentences, at first). In order to achieve this, I run the following code:

```<code>if __name__ == '__main__':

client = Client(n_workers=3, memory_limit='1.5GB', processes=True)
df = dd.read_csv('texts_no_n', dtype={'user_id': int, 'post_id': int, 'text': str})

print('Tokenizing sents')
def tokenize(df):
    df['text'] = df.text.apply(lambda post: nltk.sent_tokenize(post, language='russian'))
    print('tokenized')
    return df

df = df.map_partitions(tokenize, meta=df)
df.compute()
</code>```

Dask splits my dataframe on 20 partitions.

I expect Dask workers to do iteratively for each partition:

Tokenize the text (to run tokenize(df_part)) and to return a new,
preprocessed part of the given dataframe

Release the memory, spent on reading the partition from the file. As it always do after executing the 'compute' method

After it has iterated through all the partitions, I expect Dask to concatenate all the preprocessed partitions and return a full preprocessed dataframe.

This behaviour seems logical and most memory-saving to me, even though the practice shows that Dask doesn't release the memory, before it has processed the whole dataframe.

After computing 12 partitions of 20, I run out of my RAM and Dask seems to be trying to dump the workers' data to the disk. Have a look at the output:

```<code>Tokenizing sents
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
distributed.worker - WARNING - Memory use is high but worker has no data to store to disk. Perhaps some other process is leaking memory?  Process memory: 1.05 GB -- Worker memory limit: 1.50 GB
distributed.worker - WARNING - Memory use is high but worker has no data to store to disk.  Perhaps some other process is leaking memory?  Process memory: 1.05 GB -- Worker memory limit: 1.50 GB
distributed.worker - WARNING - Memory use is high but worker has no data to store to disk.  Perhaps some other process is leaking memory?  Process memory: 979.51 MB -- Worker memory limit: 1.50 GB
distributed.nanny - WARNING - Worker exceeded 95% memory budget. Restarting
distributed.nanny - WARNING - Restarting worker
</code>```

The scheduler restarts all the workers due to memory leak. A huge amount of RAM releases and the process of tokenizing starts all over again (it happens at the time of the steep RAM decline in the picture)

I suppose, when workers restart, they start their job from the beginning, otherwise my data preprocessing would eventually finish. Therefore, restarting the workers doesn't fit my needs.

After running the same process several times, the scheduler kills the workers and the code terminates.

My questions are:

1) Is there a possibility to multiprocessingly preprocess the Big Data with Dask or any other tool?

I could have managed this dataset of 1.8Gb with pandas' dataframe, using only one process, but I ask for educational purposes: What if my dataset exceeded my RAM? Let it be, for instance, 10Gb.

2) Why Dask's workers can't dump the data they computed for each partition to the disk in order to release the RAM?

Output shows that workers have no data to store, but it is not the truth, because my RAM is full of data. If a partition size is approximately 60 Mb (as it is in my case), can't Dask just dump some of the partitions ?

One more point to think about is the following:

Consider the case of 3 workers. If each worker processes approximately the same amount of data, it seems logical that for my case of 1.8Gb, the maximum of memory amount used by one process should be equal to around

1) 1.8Gb / 3 * 2 = 1.2Gb, and the desired is: 2) 1.8Gb / 3 = 600Mb

In the first case, I multiplied the result by 2, assuming that data spent by df = df.map_partitions(tokenize, meta=df) equals the amount of given data plus the amount of processed data (which is approximately the same, in my case). The second formula of data consuming is the one of my desired technique, outlined above (the way that I expect Dask to work).

The problem is that I don't possess such a huge RAM to accommodate the data, spent by the first formula.

答:
Finally, I am able to answer my own question.

As practice (and the documentation) showed, the best way to treat dask - is to use it with .parquet format. At first, I splitted my huge file into many .parquet files with df.to_parquet(dir_name), then loaded them back with dd.read_parquet(dir_name) and applied my function.

The following code worked for me:

```<code>def preprocess_df(df): # To pass to 'map_partition'

    mystem = Mystem()  # Most important to set it here! Don't pass objects as an argument
    df['text'] = df['text'].apply(lambda x: pr.preprocess_post(x, mystem))

    mystem.close()
    return df

if __name__ == '__main__':
    client = Client(n_workers=4)

    # Splitting the big file
    df = dd.read_csv('texts.csv', dtype={'user_id': int, 'post_id': int, 'text': str}) # Read a big data file
    df = df.repartition(npartitions=df.npartitions*8) # 8 migh be too high, try with lower values at first (e.g., 2 or don't repartition at all)
    df.to_parquet(dir_name) # convert .csv file to .parquet parts

    # Loading the splitted file parts
    df = dd.read_parquet(dir_name)

    # Applying the function
    df = df.map_partitions(preprocess_df, meta={'user_id': int, 'post_id': int, 'text': object}) # Be sure not to '.compute' here

    df.to_parquet('preprocesed.parquet')
    client.close()
</code>```

RAM consumption didn't rise above 50%.

I guess, it was not the .parquet format, that helped to reduce RAM consumption, but splitting the file into parts.

Update: Be careful when passing objects (mystem) to the function (preprocess_df), on which 'map_partition' is applied, because it might result in an unexpected behaviour (cause all the workers will try to shape this object, which is not what we want in most of cases). If you need to pass additional 'multiprocessingly-problematic' objects, define them inside the function itself (like in the 3rd line: mystem = Mystem()).

Body:
It is believed that Dask framework is capable of handling datasets which are more than RAM in size. Nevertheless, I wasn't able to successfully apply it to my problem, which sounds like this:

I have a huge .csv file (1.8Gb), which contains texts of users' comments, and a RAM of 8Gb. The goal is to preprocess the given data (to tokenize sentences, at first). In order to achieve this, I run the following code:

if __name__ == '__main__':

client = Client(n_workers=3, memory_limit='1.5GB', processes=True)
df = dd.read_csv('texts_no_n', dtype={'user_id': int, 'post_id': int, 'text': str})

print('Tokenizing sents')
def tokenize(df):
    df['text'] = df.text.apply(lambda post: nltk.sent_tokenize(post, language='russian'))
    print('tokenized')
    return df

df = df.map_partitions(tokenize, meta=df)
df.compute()

Dask splits my dataframe on 20 partitions.

I expect Dask workers to do iteratively for each partition:

Tokenize the text (to run tokenize(df_part)) and to return a new,
preprocessed part of the given dataframe

Release the memory, spent on reading the partition from the file. As it always do after executing the 'compute' method

After it has iterated through all the partitions, I expect Dask to concatenate all the preprocessed partitions and return a full preprocessed dataframe.

This behaviour seems logical and most memory-saving to me, even though the practice shows that Dask doesn't release the memory, before it has processed the whole dataframe.

After computing 12 partitions of 20, I run out of my RAM and Dask seems to be trying to dump the workers' data to the disk. Have a look at the output:

Tokenizing sents
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
tokenized
distributed.worker - WARNING - Memory use is high but worker has no data to store to disk. Perhaps some other process is leaking memory?  Process memory: 1.05 GB -- Worker memory limit: 1.50 GB
distributed.worker - WARNING - Memory use is high but worker has no data to store to disk.  Perhaps some other process is leaking memory?  Process memory: 1.05 GB -- Worker memory limit: 1.50 GB
distributed.worker - WARNING - Memory use is high but worker has no data to store to disk.  Perhaps some other process is leaking memory?  Process memory: 979.51 MB -- Worker memory limit: 1.50 GB
distributed.nanny - WARNING - Worker exceeded 95% memory budget. Restarting
distributed.nanny - WARNING - Restarting worker

The scheduler restarts all the workers due to memory leak. A huge amount of RAM releases and the process of tokenizing starts all over again (it happens at the time of the steep RAM decline in the picture)

I suppose, when workers restart, they start their job from the beginning, otherwise my data preprocessing would eventually finish. Therefore, restarting the workers doesn't fit my needs.

After running the same process several times, the scheduler kills the workers and the code terminates.

My questions are:

1) Is there a possibility to multiprocessingly preprocess the Big Data with Dask or any other tool?

I could have managed this dataset of 1.8Gb with pandas' dataframe, using only one process, but I ask for educational purposes: What if my dataset exceeded my RAM? Let it be, for instance, 10Gb.

2) Why Dask's workers can't dump the data they computed for each partition to the disk in order to release the RAM?

Output shows that workers have no data to store, but it is not the truth, because my RAM is full of data. If a partition size is approximately 60 Mb (as it is in my case), can't Dask just dump some of the partitions ?

One more point to think about is the following:

Consider the case of 3 workers. If each worker processes approximately the same amount of data, it seems logical that for my case of 1.8Gb, the maximum of memory amount used by one process should be equal to around

1) 1.8Gb / 3 * 2 = 1.2Gb, and the desired is: 2) 1.8Gb / 3 = 600Mb

In the first case, I multiplied the result by 2, assuming that data spent by df = df.map_partitions(tokenize, meta=df) equals the amount of given data plus the amount of processed data (which is approximately the same, in my case). The second formula of data consuming is the one of my desired technique, outlined above (the way that I expect Dask to work).

The problem is that I don't possess such a huge RAM to accommodate the data, spent by the first formula.

Body:
Finally, I am able to answer my own question.

As practice (and the documentation) showed, the best way to treat dask - is to use it with .parquet format. At first, I splitted my huge file into many .parquet files with df.to_parquet(dir_name), then loaded them back with dd.read_parquet(dir_name) and applied my function.

The following code worked for me:

def preprocess_df(df): # To pass to 'map_partition'

    mystem = Mystem()  # Most important to set it here! Don't pass objects as an argument
    df['text'] = df['text'].apply(lambda x: pr.preprocess_post(x, mystem))

    mystem.close()
    return df

if __name__ == '__main__':
    client = Client(n_workers=4)

    # Splitting the big file
    df = dd.read_csv('texts.csv', dtype={'user_id': int, 'post_id': int, 'text': str}) # Read a big data file
    df = df.repartition(npartitions=df.npartitions*8) # 8 migh be too high, try with lower values at first (e.g., 2 or don't repartition at all)
    df.to_parquet(dir_name) # convert .csv file to .parquet parts

    # Loading the splitted file parts
    df = dd.read_parquet(dir_name)

    # Applying the function
    df = df.map_partitions(preprocess_df, meta={'user_id': int, 'post_id': int, 'text': object}) # Be sure not to '.compute' here

    df.to_parquet('preprocesed.parquet')
    client.close()

RAM consumption didn't rise above 50%.

I guess, it was not the .parquet format, that helped to reduce RAM consumption, but splitting the file into parts.

Update: Be careful when passing objects (mystem) to the function (preprocess_df), on which 'map_partition' is applied, because it might result in an unexpected behaviour (cause all the workers will try to shape this object, which is not what we want in most of cases). If you need to pass additional 'multiprocessingly-problematic' objects, define them inside the function itself (like in the 3rd line: mystem = Mystem()).


问:
Custom splash screen
I have made the splash screen from the manual:

I have created SplashActivity that extends from AppCompatActivity etc. Its works as expected.

But now I want set some animation to the splash activity and I am confused, because all that I put on the SplashActivity  is unvisible (and now I cann't be to sure that the itself SplashActivity  is visible).

Now I have deleted all excess stuff from the SplashActivity and start it as is:

```<code>// `MainLauncher` argument for my MainActivity is false now
[Activity(Theme = "@style/SplashTheme", NoHistory = true, MainLauncher = true)]
public class SplashActivity : Activity                      // AppCompatActivity
{

    protected override void OnCreate(Bundle savedInstanceState) // , PersistableBundle persistentState
    {
        base.OnCreate(savedInstanceState); // persistentState
        this.SetContentView(Resource.Layout.LPreloader);
    }

    protected override void OnResume()
    {
        base.OnResume();
    }

}
</code>```

LPreloader.xml:

```<code><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >

  <Button
      android:id="@+id/my_button"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Some element on Activity" />
</LinearLayout>
</code>```

Theme in styles for the activity is:

```<code>  <style name="SplashTheme" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowActionBar">true</item>
  </style>
</code>```

I want to see for example the my_button on activity, but I have nothing. More than that: I set breakpoint on the line base.OnCreate(savedInstanceState); and its never stopped on it. I see just logo from @drawable/splash_screen and nothing.

If I create new Xamarin.Android project with same code - it works fine (I see the button). I cann't understand it. What is hinder to working in Xamarin.Forms project?

答:
Remove Window background from splash theme and try it will work, as you except</p>

```<code><style name="SplashTheme" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowActionBar">true</item>
  </style>
</code>```

Title: Custom splash screen

Body:
I have made the splash screen from the manual:

I have created SplashActivity that extends from AppCompatActivity etc. Its works as expected.

But now I want set some animation to the splash activity and I am confused, because all that I put on the SplashActivity  is unvisible (and now I cann't be to sure that the itself SplashActivity  is visible).

Now I have deleted all excess stuff from the SplashActivity and start it as is:

// `MainLauncher` argument for my MainActivity is false now
[Activity(Theme = "@style/SplashTheme", NoHistory = true, MainLauncher = true)]
public class SplashActivity : Activity                      // AppCompatActivity
{

    protected override void OnCreate(Bundle savedInstanceState) // , PersistableBundle persistentState
    {
        base.OnCreate(savedInstanceState); // persistentState
        this.SetContentView(Resource.Layout.LPreloader);
    }

    protected override void OnResume()
    {
        base.OnResume();
    }

}

LPreloader.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >

  <Button
      android:id="@+id/my_button"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Some element on Activity" />
</LinearLayout>

Theme in styles for the activity is:

I want to see for example the my_button on activity, but I have nothing. More than that: I set breakpoint on the line base.OnCreate(savedInstanceState); and its never stopped on it. I see just logo from @drawable/splash_screen and nothing.

If I create new Xamarin.Android project with same code - it works fine (I see the button). I cann't understand it. What is hinder to working in Xamarin.Forms project?

Body:
Remove Window background from splash theme and try it will work, as you except

<style name="SplashTheme" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowActionBar">true</item>
  </style>


问:
Python rotating the elements inside subarrays
I am trying to create two subarrays in given order, in this case i have two integers a and b a represents the value of the subarrays range, and b represents how many times it needs to be rotated.

I created the subarrays like this;

```<code>def reorder(a,b):
   return [[i for i in range(0, a//2)]] + [[f for f in range(a//2, a)]]
</code>```

Imagine a is 10 and b is 1 the output is:

```<code>[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
</code>```

But how can i reverse each subarrays b times?

The output that i want;

```<code>[[4, 0, 1, 2, 3], [9, 5, 6, 7, 8]]
</code>```

答:
I also came up with these solutions.

With numpy's roll function;

```<code>import numpy as np
def reorder(a, b):
   return [list(np.roll(v,b)) for v in [list(range(a//2)),list(range(a-a//2,a))]]

In:  reorder(10,9)
Out: [[1, 2, 3, 4, 0], [6, 7, 8, 9, 5]]
</code>```

Also much simpler solution with numpy's roll function;

```<code>import numpy as np
def reorder(a, b):
    return np.roll(np.arange(a).reshape(2, -1), b, 1).tolist()

In:  reorder(10,9)
Out: [[1, 2, 3, 4, 0], [6, 7, 8, 9, 5]]
</code>```

If you don't wanna use packages and you are okay with a little bit of eye bleeding;

```<code>def reorder(a, b):
   return [list(range(a//2))[-b%(a//2):] + list(range(a//2))[:-b%(a//2)], list(range(a//2, a))[-b%(a//2):] + list(range(a//2, a))[:-b%(a//2)]]

In: reorder(10,9)
Out: [[1, 2, 3, 4, 0], [6, 7, 8, 9, 5]]
</code>```

Body:
I am trying to create two subarrays in given order, in this case i have two integers a and b a represents the value of the subarrays range, and b represents how many times it needs to be rotated.

I created the subarrays like this;

def reorder(a,b):
   return [[i for i in range(0, a//2)]] + [[f for f in range(a//2, a)]]

Imagine a is 10 and b is 1 the output is:

[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]

But how can i reverse each subarrays b times?

The output that i want;

[[4, 0, 1, 2, 3], [9, 5, 6, 7, 8]]

Body:
I also came up with these solutions.

With numpy's roll function;

import numpy as np
def reorder(a, b):
   return [list(np.roll(v,b)) for v in [list(range(a//2)),list(range(a-a//2,a))]]

In:  reorder(10,9)
Out: [[1, 2, 3, 4, 0], [6, 7, 8, 9, 5]]

Also much simpler solution with numpy's roll function;

import numpy as np
def reorder(a, b):
    return np.roll(np.arange(a).reshape(2, -1), b, 1).tolist()

In:  reorder(10,9)
Out: [[1, 2, 3, 4, 0], [6, 7, 8, 9, 5]]

If you don't wanna use packages and you are okay with a little bit of eye bleeding;

def reorder(a, b):
   return [list(range(a//2))[-b%(a//2):] + list(range(a//2))[:-b%(a//2)], list(range(a//2, a))[-b%(a//2):] + list(range(a//2, a))[:-b%(a//2)]]

In: reorder(10,9)
Out: [[1, 2, 3, 4, 0], [6, 7, 8, 9, 5]]


问:
Change value in for-loop whilst looping
I'm stuck on a dice exercise. For each dice that rolls 6, they need to be excluded from the count and rolled again. Problem is that I don't know how to change the value of a for range loop whilst looping through it, and I'm not sure how to attack it otherwise. Here's where I'm at:

```<code>numRolls = 10
initSum = 0

for i in range(0,numRolls):
    diceScore = random.randint(1,6)
    if(diceScore == 6):
        print("You rolled a 6! You receive two extra rolls.")
        numRolls = numRolls + 2
        # Not making its way to the top of the for-loop
    else:
        totalSum = initSum + diceScore
print("Done! You've rolled a total of ",totalSum)
totalSum = 0

</code>```

I'm guessing numRolls in the for-loop is out of scope. The alternative solutions I've come up with would mean absolutely endless amount of separate rolls. Randoming between 1 and 5 is not a viable option.. :)

Can someone point me in the right direction here?

答:
Use a while loop and initialize the counter outside the loop. Then increment both, the counter and numRolls, as and when you want inside the loop

```<code>numRolls = 10
rollsSoFar = 0
initSum = 0

while rollsSoFar <= nulRolls:
    diceScore = random.randint(1,6)
    if(diceScore == 6):
        print("You rolled a 6! You receive two extra rolls.")
        numRolls = numRolls + 2
    else:
        totalSum = initSum + diceScore
    rollsSoFar += 1
print("Done! You've rolled a total of ",totalSum)
totalSum = 0
</code>```

Edit: added explanation for why this works

When you create a for loop like for i in range(0,numRolls) you are calling the function range() and passing it parameters 0 and numRolls. range() then gives you back a generator (let's call this generator rollsGenerator) that you iterate over. This generator has been created to give you values 0 to numRolls-1 inclusive.

So your for-loop effectively evaluates to something like for i in rollsGenerator before the first iteration begins.

Then, inside the loop, when you increment the value of numRolls, it doesn't change the number of iterations because rollsGenerator has already been created with the original value of numRolls. The for-loop does not call range(0,numRolls) on every iteration, and so the generator stays the same no matter how you change numRolls.

By using a while-loop, you are basically changing your code to check the latest value of numRolls on each iteration and you skip the whole generator business.

Title: Change value in for-loop whilst looping

Body:
I'm stuck on a dice exercise. For each dice that rolls 6, they need to be excluded from the count and rolled again. Problem is that I don't know how to change the value of a for range loop whilst looping through it, and I'm not sure how to attack it otherwise. Here's where I'm at:

numRolls = 10
initSum = 0

for i in range(0,numRolls):
    diceScore = random.randint(1,6)
    if(diceScore == 6):
        print("You rolled a 6! You receive two extra rolls.")
        numRolls = numRolls + 2
        # Not making its way to the top of the for-loop
    else:
        totalSum = initSum + diceScore
print("Done! You've rolled a total of ",totalSum)
totalSum = 0

I'm guessing numRolls in the for-loop is out of scope. The alternative solutions I've come up with would mean absolutely endless amount of separate rolls. Randoming between 1 and 5 is not a viable option.. :)

Can someone point me in the right direction here?

Body:
Use a while loop and initialize the counter outside the loop. Then increment both, the counter and numRolls, as and when you want inside the loop

numRolls = 10
rollsSoFar = 0
initSum = 0

while rollsSoFar <= nulRolls:
    diceScore = random.randint(1,6)
    if(diceScore == 6):
        print("You rolled a 6! You receive two extra rolls.")
        numRolls = numRolls + 2
    else:
        totalSum = initSum + diceScore
    rollsSoFar += 1
print("Done! You've rolled a total of ",totalSum)
totalSum = 0

Edit: added explanation for why this works

When you create a for loop like for i in range(0,numRolls) you are calling the function range() and passing it parameters 0 and numRolls. range() then gives you back a generator (let's call this generator rollsGenerator) that you iterate over. This generator has been created to give you values 0 to numRolls-1 inclusive.

So your for-loop effectively evaluates to something like for i in rollsGenerator before the first iteration begins.

Then, inside the loop, when you increment the value of numRolls, it doesn't change the number of iterations because rollsGenerator has already been created with the original value of numRolls. The for-loop does not call range(0,numRolls) on every iteration, and so the generator stays the same no matter how you change numRolls.

By using a while-loop, you are basically changing your code to check the latest value of numRolls on each iteration and you skip the whole generator business.


问:
Call parent method inside super().__init__ when method is overriden by child
I have overriden a method in a child class.
This method is used in the parent class constructor.
When calling super().__init__ in the child class the child's method is executed instead of the parent's one inside the parent's constructor.

Example:

class A:
    def __init__(self):
        self.method()

    def method(self):
        print('parent method!')

class B(A):
    def __init__(self):
        super().__init__()

    def method(self):
        print('child method!')

b = B()

```

Output: child method!

While I want to get parent method!

Edit:

I need the parent's class constructor to use the not-overriden method, but after that each call to method() from the parent class should call overriden one.

答:
If you have that kind of situation, I would suggest something like:</p>

```<code>class B(A):
    def __init__(self):
        super().__init()
        self.n_method = 1
    def method(self):
        if self.n_method == 1:
            super().method()
        else:
            do method B stuffs
        self.n_method += 1
</code>```

Body:
I have overriden a method in a child class.
This method is used in the parent class constructor.
When calling super().__init__ in the child class the child's method is executed instead of the parent's one inside the parent's constructor.

Example:

class A:
    def __init__(self):
        self.method()

    def method(self):
        print('parent method!')

class B(A):
    def __init__(self):
        super().__init__()

    def method(self):
        print('child method!')

b = B()

Output: child method!

While I want to get parent method!

Edit:

I need the parent's class constructor to use the not-overriden method, but after that each call to method() from the parent class should call overriden one.

Body:
If you have that kind of situation, I would suggest something like:

class B(A):
    def __init__(self):
        super().__init()
        self.n_method = 1
    def method(self):
        if self.n_method == 1:
            super().method()
        else:
            do method B stuffs
        self.n_method += 1


问:
Passing self reference as parameter in abstract base class static function for use in child classes
In typescript, one can create a base class that has a static function that child classes can use and that return whatever the type of the calling child class. This is shown here TypeScript: self-referencing return type for static methods in inheriting classes.

```<code>type Constructor<T> = { new (): T }

class BaseModel {
  static getAll<T>(this: Constructor<T>): T[] {
    return [] // dummy impl
  }

  /**
   * Example of static method with an argument:
   */
  static getById<T>(this: Constructor<T>, id: number): T {
    return // dummy impl
  }
}

class SubModel extends BaseModel {}
const savedSubs: SubModel = SubModel.getById(1234)
</code>```

How can one create a function that takes an input parameter of the type of the subclass though?

Psuedocode-wise it would look something like this:

```<code>static getByChild<T>(this: Constructor<T>, child: this): T {
  return // dummy impl
}
</code>```

But this doesn't work. How can I agnostically make the method parameter be a subclass of this base class?

答:
If T is intended to be the subclass instance type, as evidenced by this being Constructor<T>, then you just need to use T as the type of the child parameter:

```<code>static getByChild<T>(this: Constructor<T>, child: T): T {
  return child; // ?
}
</code>```

And you can verify this works:

```<code>const oneSub = SubModel.getById(123); // SubModel
const anotherSub = SubModel.getByChild(oneSub); // SubModel
</code>```

Hope that helps; good luck!

Playground link to code

Body:
In typescript, one can create a base class that has a static function that child classes can use and that return whatever the type of the calling child class. This is shown here TypeScript: self-referencing return type for static methods in inheriting classes.

type Constructor<T> = { new (): T }

class BaseModel {
  static getAll<T>(this: Constructor<T>): T[] {
    return [] // dummy impl
  }

  /**
   * Example of static method with an argument:
   */
  static getById<T>(this: Constructor<T>, id: number): T {
    return // dummy impl
  }
}

class SubModel extends BaseModel {}
const savedSubs: SubModel = SubModel.getById(1234)

How can one create a function that takes an input parameter of the type of the subclass though?

Psuedocode-wise it would look something like this:

static getByChild<T>(this: Constructor<T>, child: this): T {
  return // dummy impl
}

But this doesn't work. How can I agnostically make the method parameter be a subclass of this base class?

Body:
If T is intended to be the subclass instance type, as evidenced by this being Constructor<T>, then you just need to use T as the type of the child parameter:

static getByChild<T>(this: Constructor<T>, child: T): T {
  return child; // ?
}

And you can verify this works:

const oneSub = SubModel.getById(123); // SubModel
const anotherSub = SubModel.getByChild(oneSub); // SubModel

Hope that helps; good luck!

<a href="https://www.typescriptlang.org/play/#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAqAfFALxQDeUCEA7gBQCUAXFAVAL4BQXmANgEMkSKACEhEALJwAJhD6UeUKACMJABVRwwZKACI4AawEg9SqCgHAAlpigBzCMACCfPoSL1gAC2tJW8MhoWDj4xCxsANoAuorKyqhO6KgIUDFQAPQZUDLoALZ5IFDWeWB8XMocUOYZAFS1Fcq1UACiAB4CpXzQcABmFsBWtlB5Tt6yULTWPlACqQKo9vkQCMDMjVC1GY2WNnaOwKIgAJIyHl6+-rCIKBjYuB4ANMUyrAj5KhCoEewUG1CJYDJVLvNwAQgA3I1uDtBnsHE4jjBfHwzsQLn4AjdgvcwkRnpgUa82D84vFAcCoITrKiIZlsgB+aFcGH8IQiADK6BU0jkCggbWAKxkInESCksnkZKQ3M02l0egEbgB1ns3mAZm4mGxUEQEC5Kl0Bt58gAdAcjqd6ABGABMAGZGHSslBjZLytqgrMEHAfF8DUbuSa+ObESBkTSZPQ9QanfTXUH3UA" rel="nofollow noreferrer">Playground link to code


问:
TS-Node with Mocha doesn't use TS_NODE_PROJECT
I'm having trouble with using the env variable TS_NODE_PROJECT when ts-node is used for testing using Mocha.

The project structure looks like this:

```<code>src/
  main_test.ts
  tsconfig.json
package.json
</code>```

In my test, I want to use an async function, which requires "lib": ["es2018"] as a compilation option.

```<code>// src/main_test.ts
describe('', () => {
    it('test', () => {
        (async function() {})()
    });
});

// src/tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true,
    "lib": ["es2018"]
  },
  "exclude": [
    "../node_modules"
  ]
}
</code>```

To run the test, I use this command, but it results in an error:

```<code>TS_NODE_PROJECT='src' && mocha --require ts-node/register src/*_test.ts
# TSError: ⨯ Unable to compile TypeScript:
# error TS2468: Cannot find global value 'Promise'.
# src/main_test.ts(3,10): error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor.  Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.
</code>```

This means that src/tsconfig.json is not used. According to Overriding `tsconfig.json` for ts-node in mocha
 and the ts-node documentation, the command should pass the correct tsconfig.json path to ts-node.

Moving src/tsconfig.json to project directory and running the same command causes the test to succeed. How can I pass the tsconfig.json path to ts-node so that the test compiles correctly?

答:
I find very useful to move mocha setup in different files so package.json remains clean, you can use a mocharc file like this:

module.exports = {
  ignore: [
    './test/helpers/**/*',
    './test/mocha.env.js'
  ],
  require: [
    'test/mocha.env', // init env here
    'ts-node/register'
  ],
  extension: [
    'ts'
  ]
}
```

<p>and then create the file <code>test/mocha.env.js</code> (or call it as you wish) with this content:</p>

<pre class="lang-js prettyprint-override"><code>process.env.NODE_ENV = 'test'
process.env.TS_NODE_PROJECT = 'src/tsconfig.json'
</code>```

Body:
I'm having trouble with using the env variable TS_NODE_PROJECT when ts-node is used for testing using Mocha.

The project structure looks like this:

src/
  main_test.ts
  tsconfig.json
package.json

In my test, I want to use an async function, which requires "lib": ["es2018"] as a compilation option.

// src/main_test.ts
describe('', () => {
    it('test', () => {
        (async function() {})()
    });
});

// src/tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true,
    "lib": ["es2018"]
  },
  "exclude": [
    "../node_modules"
  ]
}

To run the test, I use this command, but it results in an error:

TS_NODE_PROJECT='src' && mocha --require ts-node/register src/*_test.ts
# TSError: ⨯ Unable to compile TypeScript:
# error TS2468: Cannot find global value 'Promise'.
# src/main_test.ts(3,10): error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor.  Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.

This means that src/tsconfig.json is not used. According to Overriding `tsconfig.json` for ts-node in mocha
 and the ts-node documentation, the command should pass the correct tsconfig.json path to ts-node.

Moving src/tsconfig.json to project directory and running the same command causes the test to succeed. How can I pass the tsconfig.json path to ts-node so that the test compiles correctly?

Body:
I find very useful to move mocha setup in different files so package.json remains clean, you can use a mocharc file like this:

module.exports = {
  ignore: [
    './test/helpers/**/*',
    './test/mocha.env.js'
  ],
  require: [
    'test/mocha.env', // init env here
    'ts-node/register'
  ],
  extension: [
    'ts'
  ]
}

and then create the file test/mocha.env.js (or call it as you wish) with this content:

process.env.NODE_ENV = 'test'
process.env.TS_NODE_PROJECT = 'src/tsconfig.json'


问:
Using OraclePreparedStatement with DBCP Connection
I'm trying to make a connection pool with the dbcp framework for my oracle server.
I used this tutorial for the connection.
The problem is to create a OraclePreparedStatement with this connection:

```<code>Connection oracleCon;
OraclePreparedStatement o_stmt;
String sql = "INSERT INTO T002_metadata (T002_datacitexml,T002_version, T002_active)       VALUES (?,?,?) RETURNING T002_id INTO ?";

oracleCon = ConnectionManager.ds.getConnection();

o_stmt = ((OraclePreparedStatement) oracleCon.prepareStatement(sql));
</code>```

After executing this, an exception is thrown.

```<code>org.apache.commons.dbcp.DelegatingPreparedStatement cannot be cast to oracle.jdbc.OraclePreparedStatement
</code>```

Is there any possibility to cast the statement?

答:
I use an utility method

```<code>public static OraclePreparedStatement getOraclePreparedStatement(PreparedStatement stmt) {
        if (stmt instanceof OraclePreparedStatement) {
            return (OraclePreparedStatement) stmt;
        }
        if (stmt instanceof DelegatingPreparedStatement) {
            DelegatingPreparedStatement stmt1 = (DelegatingPreparedStatement) stmt;

            return (OraclePreparedStatement) stmt1.getInnermostDelegate();
        }
        System.out.println(stmt);
        throw new RuntimeException("unknown statement");
    }
</code>```

Essentially it allows using the same code standalone (e.g. in tests) or in tomcat. I use it instead of casting, so the code does not get longer.

```<code>try (OraclePreparedStatement stmt = getOraclePreparedStatement(con.prepareStatement(SQL_INSERT_TREATMENT))) {
    ...
    }
</code>```

Body:
I'm trying to make a connection pool with the dbcp framework for my oracle server.
I used this tutorial for the connection.
The problem is to create a OraclePreparedStatement with this connection:

Connection oracleCon;
OraclePreparedStatement o_stmt;
String sql = "INSERT INTO T002_metadata (T002_datacitexml,T002_version, T002_active)       VALUES (?,?,?) RETURNING T002_id INTO ?";

oracleCon = ConnectionManager.ds.getConnection();

o_stmt = ((OraclePreparedStatement) oracleCon.prepareStatement(sql));

After executing this, an exception is thrown.

org.apache.commons.dbcp.DelegatingPreparedStatement cannot be cast to oracle.jdbc.OraclePreparedStatement

Is there any possibility to cast the statement?

Body:
I use an utility method

public static OraclePreparedStatement getOraclePreparedStatement(PreparedStatement stmt) {
        if (stmt instanceof OraclePreparedStatement) {
            return (OraclePreparedStatement) stmt;
        }
        if (stmt instanceof DelegatingPreparedStatement) {
            DelegatingPreparedStatement stmt1 = (DelegatingPreparedStatement) stmt;

            return (OraclePreparedStatement) stmt1.getInnermostDelegate();
        }
        System.out.println(stmt);
        throw new RuntimeException("unknown statement");
    }

Essentially it allows using the same code standalone (e.g. in tests) or in tomcat. I use it instead of casting, so the code does not get longer.

try (OraclePreparedStatement stmt = getOraclePreparedStatement(con.prepareStatement(SQL_INSERT_TREATMENT))) {
    ...
    }


问:
VMWare: About snapshots: do they usually occupy how much% of the disk space source VM? And are they used to downgrade software?
I would like to update the samba on a 3TB NAS. My boss suggested making a clone, however, there is no storage that will fit him whole. If a snapshot of the VM costs a smaller size, and serves to, in case of failure, restore the samba as it was, making it a better idea.</p>

答: There's no real guide on how much space snapshots occupy. That will greatly depend on the activity on the VM where the snapshot has been taken. If it's an active VM (database or something of the like), there could be a considerable amount of data written. If it's not a very used VM, there could be limited to no data written to the backend datastore. </p>


问:
Search for column values in another column and assign a value from the next column from the row found to another column
I am working with Pandas and am trying to populate a column with the values in another column, where another column contains the same value as in a specific column.

Example:

```<code>>>> df = pd.DataFrame({'StringToCheck': ['10T','125T', '',''],
                   'FromNumber' :['','','',''],
                   'ToNumber' : ['','','',''],
                   'CheckStringHere': ['AAA_ER', 'FGGR_DBC', '10T', '125T'],
                   'AssociatedValue1': ['','','56','16'],
                   'AssociatedValue2': ['','','58','24']})
>>> df
  StringToCheck FromNumber ToNumber CheckStringHere AssociatedValue1 AssociatedValue2
0           10T                              AAA_ER
1          125T                            FGGR_DBC
2                                               10T               56               58
3                                              125T               16               24
</code>```

I want to search for each value in StringToCheck inside the CheckStringHere column, and assign the value from the AssociatedValue1 on the row found to the FromNumber column but on the row where the value in StringToCheck is.

I have tried

```<code>>>> df['FromNumber'] = df[df['CheckStringHere'].isin(df['StringToCheck'])]['AssociatedValue1']
>>> df
  StringToCheck FromNumber ToNumber CheckStringHere AssociatedValue1 AssociatedValue2
0           10T        NaN                   AAA_ER
1          125T        NaN                 FGGR_DBC
2                       56                      10T               56               58
3                       16                     125T               16               24
</code>```

As you see, the values have been added in FromNumber column, but on the row where the values from StringToCheck were found in CheckStringHere, I need to have

```<code>  StringToCheck FromNumber ToNumber CheckStringHere AssociatedValue1 AssociatedValue2
0           10T        56                    AAA_ER
1          125T        16                  FGGR_DBC
2                                               10T               56               58
3                                              125T               16               24
</code>```

Next, if it is possible to get the values from AssociatedValue2 to ToNumber column simultaneously, it would be a real treat.

P.S.

I am out of ideas what is wrong with this question, I researched and tried a lot of solutions, explained my approach and what did not work. Downvoting questions like this is definitely wrong.

答:
You can try creating a dictionary from columns ['CheckStringHere','AssociatedValue1'] and replace values from StringToCheck column:

```<code>d = dict(df[['CheckStringHere','AssociatedValue1']].to_numpy())
df['FromNumber'] = df['StringToCheck'].replace(d)
#or df['FromNumber'] = df['StringToCheck'].map(d).fillna(df['FromNumber'])
</code>```

```<code>print(df)
  StringToCheck FromNumber ToNumber CheckStringHere AssociatedValue1  \
0           10T         56                   AAA_ER
1          125T         16                 FGGR_DBC
2                                               10T               56
3                                              125T               16

  AssociatedValue2
0
1
2               58
3               24
</code>```

Body:
I am working with Pandas and am trying to populate a column with the values in another column, where another column contains the same value as in a specific column.

Example:

>>> df = pd.DataFrame({'StringToCheck': ['10T','125T', '',''],
                   'FromNumber' :['','','',''],
                   'ToNumber' : ['','','',''],
                   'CheckStringHere': ['AAA_ER', 'FGGR_DBC', '10T', '125T'],
                   'AssociatedValue1': ['','','56','16'],
                   'AssociatedValue2': ['','','58','24']})
>>> df
  StringToCheck FromNumber ToNumber CheckStringHere AssociatedValue1 AssociatedValue2
0           10T                              AAA_ER
1          125T                            FGGR_DBC
2                                               10T               56               58
3                                              125T               16               24

I want to search for each value in StringToCheck inside the CheckStringHere column, and assign the value from the AssociatedValue1 on the row found to the FromNumber column but on the row where the value in StringToCheck is.

I have tried

>>> df['FromNumber'] = df[df['CheckStringHere'].isin(df['StringToCheck'])]['AssociatedValue1']
>>> df
  StringToCheck FromNumber ToNumber CheckStringHere AssociatedValue1 AssociatedValue2
0           10T        NaN                   AAA_ER
1          125T        NaN                 FGGR_DBC
2                       56                      10T               56               58
3                       16                     125T               16               24

As you see, the values have been added in FromNumber column, but on the row where the values from StringToCheck were found in CheckStringHere, I need to have

  StringToCheck FromNumber ToNumber CheckStringHere AssociatedValue1 AssociatedValue2
0           10T        56                    AAA_ER
1          125T        16                  FGGR_DBC
2                                               10T               56               58
3                                              125T               16               24

Next, if it is possible to get the values from AssociatedValue2 to ToNumber column simultaneously, it would be a real treat.

P.S.

I am out of ideas what is wrong with this question, I researched and tried a lot of solutions, explained my approach and what did not work. Downvoting questions like this is definitely wrong.

Body:
You can try creating a dictionary from columns ['CheckStringHere','AssociatedValue1'] and replace values from StringToCheck column:

d = dict(df[['CheckStringHere','AssociatedValue1']].to_numpy())
df['FromNumber'] = df['StringToCheck'].replace(d)
#or df['FromNumber'] = df['StringToCheck'].map(d).fillna(df['FromNumber'])

print(df)
  StringToCheck FromNumber ToNumber CheckStringHere AssociatedValue1  \
0           10T         56                   AAA_ER
1          125T         16                 FGGR_DBC
2                                               10T               56
3                                              125T               16

  AssociatedValue2
0
1
2               58
3               24


问:
Can I scrape StackOverflow job postings for research purpose?
I am thinking of scraping StackoOverflow job postings for my research.

In specific, I am going to construct the network, where nodes are 'industry and technology' tagged to the job postings.

This research is not for the personal practice, but for the publication of journal papers.

Here, I want to know whether
1) it is possible to scrape StackoOverflow job postings for the research purpose.
2) if possible, is there a way to scrape job postings in the past
3) other database that provides job postings.

答:
It doesn't seem so, since /jobs/ is restricted from scraping.

See: https://stackoverflow.com/robots.txt

So to make a long story short, you are not allowed to, but technically you could.
Be smart and follow the gentlemen agreement.