问:
reactjs: setState being called twice in a function called once? why?
EDIT: since the code snip does not reproduce the bug - here is a link to the github repo: (code is far FAR from complete)

https://github.com/altruios/clicker-game

I have run it on two computers now - both with the same behavior the code snip doesn't show.

//interestingly enough, this works just fine, where the same code I run locally has the doubling.
//when I comment out ALL other code except for this code I STILL get the error locally
//at this point the only difference is import export of components... here they are in one file.
//below is original code from file (
/*
FILE::::Clicker.js

import React from 'react';

function Clicker(props)
 {
 return(

  {props.name}
  <button
   name={props.name}
   onClick={props.HandleClick}
   data-target={props.subjectsOfIncrease}>
    {props.name} {props.value}

  </button>

  )
}

export default Clicker;

FILE:: Resouce.js

import React from 'react';
function Resource(props)
 {
 return(

  {props.name} and  {props.amount || 0}

  )
}

export default Resource;

*/
//besides the import/export and seprate files - code is the same. it works in here, does not work locally on my machine.

const gameData = {
  clickerData: [{
    name: "grey",
    subjectsOfIncrease: ["grey"],
    isUnlocked: true,
    value: 1
  }],
  resourceData: [{
    name: "grey",
    resouceMax: 100,
    isUnlocked: true,
    changePerTick: 0,
    counterTillStopped: 100,
    amount: 0
  }]
}
class App extends React.Component {
    constructor() {
      super();
      this.state = {
        resources: gameData.resourceData,
        clickers: gameData.clickerData
      };
      this.gainResource = this.gainResource.bind(this);
    }
    gainResource(event) {
      console.count("gain button");
      const name = event.target.name;
      this.setState((prevState) => {
        const newResources = prevState.resources.map(resource => {
          if (resource.name === name) {
            resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
          }
          return resource;
        });
        console.log(prevState.resources.find(item => item.name === name).amount, "old");
        console.log(newResources.find(item => item.name === name).amount, "new");
        return {
          resources: newResources
        }
      });
    }
    render() {
      const resources = this.state.resources.map(resourceData => {
          return (
            <Resource
              name = {resourceData.name}
              resouceMax = {resourceData.resourceMax}
              isUnlocked = {resourceData.isUnlocked}
              changePerTick = {resourceData.changePerTick}
              counterTillStopped = {resourceData.countTillStopped}
              amount = {resourceData.amount}
              key = {resourceData.name}
            />
          )
      })

      const clickers = this.state.clickers.map(clickerData => {
          return (
            <Clicker
              name = {clickerData.name}
              HandleClick = {this.gainResource}
              value = {clickerData.amount}
              key = {clickerData.name}
            />
          )
    })

    return (

            {resources}
           {clickers}

   )
 }
}
function Resource(props) {
      return
  {props.name} and {props.amount || 0}

}

function Clicker(props) {
      return (

 {props.name}
          <button name = {props.name}  onClick = {props.HandleClick}>
            {props.name} {props.value}
          </button>

     )
}
const root = document.getElementById('root');
ReactDOM.render(  <App / >,root );```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div></code>```

so I'm building a clicker game to learn react, and something I don't understand why this code is behaving the way it does:

in the main app I have this function:

```<code>
  gainResource(event)
    {
    console.count("gain button");
    const name = event.target.name;
    this.setState( (prevState)=>
      {
      const newResources =  prevState.resources.map(resource=>
        {
        if(resource.name === name)
          {
          resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
        }
        return resource;
      });
      console.log(prevState.resources.find(item=>item.name===name).amount, "old");
      console.log(newResources.find(item=>item.name===name).amount, "new");
      return {resources: newResources}
    });
  }
</code>```

that console.count runs a single time... but I get 2 'old and new' pairs. as if setState is running twice in this function which only runs once?

the console.output is:

```<code>App.js:64 gain button: 1
App.js:76 1 "old"
App.js:77 1 "new"
App.js:76 2 "old"
App.js:77 2 "new"
</code>```

so it looks like the function is running once. but the set state is being run twice?

the symptoms are that it counts up by 2. but also the initial state of amount is 0, not 1, as seen in the gamedata.json

```<code>    resourceData:
        [
            {
            name:"grey",
            resouceMax:100,
            isUnlocked:true,
            changePerTick:0,
            counterTillStopped:100,
            amount:0
            },{etc},{},{}],
        clickerData:
        [
            {
            name:"grey",
            subjectsOfIncrease:["grey"],
            isUnlocked:true,
            value:1
           },{etc},{},{}]

</code>```

i don't think the rest of the code I'm about to most is relevant to this behavior, but I don't know react yet, so I don't know what I'm missing: but this is how I'm generating the clicker button:

```<code>const clickers = this.state.clickers.map(clickerData=>
      {
      return(
        <Clicker
          name={clickerData.name}
          HandleClick = {this.gainResource}
          value = {clickerData.amount}
          key={clickerData.name}

        />

        )
    })

</code>```

and in the clicker.js functional component I'm just returning this:

```<code>        <div>
        {props.name}
        <button name={props.name} onClick={props.HandleClick}>
                {props.name} {props.value}
        </button>
        </div>

</code>```

the function is bound to this in the constructor... I don't understand why this is running setState twice inside a function that's called once.

I've also tried:

```<code>
        <div>
        {props.name}
        <button name={props.name} onClick={()=>props.HandleClick}> //anon function results in no output
                {props.name} {props.value}
        </button>
        </div>
</code>```

答:
okay... so after some hair pulling... I found out a way that works... but I DON'T think this is 'best practice' but it now works for me when I write this:

```<code>  gainResource(event)
    {
    const name = event.target.name;

    const lastResources =  this.state.resources.slice();
      const newResources = lastResources.map(resource=>
        {
        if(resource.name === name)
          {
          resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
        }
        return resource;
      });

    this.setState({resources: newResources});
  }
</code>```

vs

```<code>
  gainResource(event)
    {
    console.count("gain button");
    const name = event.target.name;
    this.setState( (prevState)=>
      {
      const newResources =  prevState.resources.map(resource=>
        {
        if(resource.name === name)
          {
          resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
        }
        return resource;
      });
      console.log(prevState.resources.find(item=>item.name===name).amount, "old");
      console.log(newResources.find(item=>item.name===name).amount, "new");
      return {resources: newResources}
    });
  }
</code>```

that setState without the function of prevState is called once... whereas with the prevState it's called twice... why?

so I still don't understand why setState using a function with prevState is causing two function calls within a function that's called only once... I have read that I should be using prev state as this.state.resources.slice(); just takes an 'untimed snapshot' and could be unreliable. is this true... or is this methodology acceptable?

this is AN answer to anyone else struggling with this. hopefully a better answer can be posted after this enlightenment to what might be happening.

Body:
EDIT: since the code snip does not reproduce the bug - here is a link to the github repo: (code is far FAR from complete)

https://github.com/altruios/clicker-game

I have run it on two computers now - both with the same behavior the code snip doesn't show.

//interestingly enough, this works just fine, where the same code I run locally has the doubling.
//when I comment out ALL other code except for this code I STILL get the error locally
//at this point the only difference is import export of components... here they are in one file.
//below is original code from file (
/*
FILE::::Clicker.js

import React from 'react';

function Clicker(props)
 {
 return(

  {props.name}
  <button
   name={props.name}
   onClick={props.HandleClick}
   data-target={props.subjectsOfIncrease}>
    {props.name} {props.value}

  </button>

  )
}

export default Clicker;

FILE:: Resouce.js

import React from 'react';
function Resource(props)
 {
 return(

  {props.name} and  {props.amount || 0}

  )
}

export default Resource;

*/
//besides the import/export and seprate files - code is the same. it works in here, does not work locally on my machine.

const gameData = {
  clickerData: [{
    name: "grey",
    subjectsOfIncrease: ["grey"],
    isUnlocked: true,
    value: 1
  }],
  resourceData: [{
    name: "grey",
    resouceMax: 100,
    isUnlocked: true,
    changePerTick: 0,
    counterTillStopped: 100,
    amount: 0
  }]
}
class App extends React.Component {
    constructor() {
      super();
      this.state = {
        resources: gameData.resourceData,
        clickers: gameData.clickerData
      };
      this.gainResource = this.gainResource.bind(this);
    }
    gainResource(event) {
      console.count("gain button");
      const name = event.target.name;
      this.setState((prevState) => {
        const newResources = prevState.resources.map(resource => {
          if (resource.name === name) {
            resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
          }
          return resource;
        });
        console.log(prevState.resources.find(item => item.name === name).amount, "old");
        console.log(newResources.find(item => item.name === name).amount, "new");
        return {
          resources: newResources
        }
      });
    }
    render() {
      const resources = this.state.resources.map(resourceData => {
          return (
            <Resource
              name = {resourceData.name}
              resouceMax = {resourceData.resourceMax}
              isUnlocked = {resourceData.isUnlocked}
              changePerTick = {resourceData.changePerTick}
              counterTillStopped = {resourceData.countTillStopped}
              amount = {resourceData.amount}
              key = {resourceData.name}
            />
          )
      })

      const clickers = this.state.clickers.map(clickerData => {
          return (
            <Clicker
              name = {clickerData.name}
              HandleClick = {this.gainResource}
              value = {clickerData.amount}
              key = {clickerData.name}
            />
          )
    })

    return (

            {resources}
           {clickers}

   )
 }
}
function Resource(props) {
      return
  {props.name} and {props.amount || 0}

}

function Clicker(props) {
      return (

 {props.name}
          <button name = {props.name}  onClick = {props.HandleClick}>
            {props.name} {props.value}
          </button>

     )
}
const root = document.getElementById('root');
ReactDOM.render(  <App / >,root );

so I'm building a clicker game to learn react, and something I don't understand why this code is behaving the way it does:

in the main app I have this function:

  gainResource(event)
    {
    console.count("gain button");
    const name = event.target.name;
    this.setState( (prevState)=>
      {
      const newResources =  prevState.resources.map(resource=>
        {
        if(resource.name === name)
          {
          resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
        }
        return resource;
      });
      console.log(prevState.resources.find(item=>item.name===name).amount, "old");
      console.log(newResources.find(item=>item.name===name).amount, "new");
      return {resources: newResources}
    });
  }

that console.count runs a single time... but I get 2 'old and new' pairs. as if setState is running twice in this function which only runs once?

the console.output is:

App.js:64 gain button: 1
App.js:76 1 "old"
App.js:77 1 "new"
App.js:76 2 "old"
App.js:77 2 "new"

so it looks like the function is running once. but the set state is being run twice?

the symptoms are that it counts up by 2. but also the initial state of amount is 0, not 1, as seen in the gamedata.json

    resourceData:
        [
            {
            name:"grey",
            resouceMax:100,
            isUnlocked:true,
            changePerTick:0,
            counterTillStopped:100,
            amount:0
            },{etc},{},{}],
        clickerData:
        [
            {
            name:"grey",
            subjectsOfIncrease:["grey"],
            isUnlocked:true,
            value:1
           },{etc},{},{}]

i don't think the rest of the code I'm about to most is relevant to this behavior, but I don't know react yet, so I don't know what I'm missing: but this is how I'm generating the clicker button:

const clickers = this.state.clickers.map(clickerData=>
      {
      return(
        <Clicker
          name={clickerData.name}
          HandleClick = {this.gainResource}
          value = {clickerData.amount}
          key={clickerData.name}

        />

        )
    })

and in the clicker.js functional component I'm just returning this:

        {props.name}
        <button name={props.name} onClick={props.HandleClick}>
                {props.name} {props.value}
        </button>

the function is bound to this in the constructor... I don't understand why this is running setState twice inside a function that's called once.

I've also tried:

        {props.name}
        <button name={props.name} onClick={()=>props.HandleClick}> //anon function results in no output
                {props.name} {props.value}
        </button>

Body:
okay... so after some hair pulling... I found out a way that works... but I DON'T think this is 'best practice' but it now works for me when I write this:

  gainResource(event)
    {
    const name = event.target.name;

    const lastResources =  this.state.resources.slice();
      const newResources = lastResources.map(resource=>
        {
        if(resource.name === name)
          {
          resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
        }
        return resource;
      });

    this.setState({resources: newResources});
  }

vs

  gainResource(event)
    {
    console.count("gain button");
    const name = event.target.name;
    this.setState( (prevState)=>
      {
      const newResources =  prevState.resources.map(resource=>
        {
        if(resource.name === name)
          {
          resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
        }
        return resource;
      });
      console.log(prevState.resources.find(item=>item.name===name).amount, "old");
      console.log(newResources.find(item=>item.name===name).amount, "new");
      return {resources: newResources}
    });
  }

that setState without the function of prevState is called once... whereas with the prevState it's called twice... why?

so I still don't understand why setState using a function with prevState is causing two function calls within a function that's called only once... I have read that I should be using prev state as this.state.resources.slice(); just takes an 'untimed snapshot' and could be unreliable. is this true... or is this methodology acceptable?

this is AN answer to anyone else struggling with this. hopefully a better answer can be posted after this enlightenment to what might be happening.


来源: stackexchange

问:
H2 ddl create table
I would like to know if it is possible to get the H2 ddl create table from the

```<code>http://localhost:5678/pecador/console/
</code>```

I have tried show columns from T_USER ;

but it only shows the columns

答:
If you want to get SQL for a table T_USER, you can use one of two following commands in H2:

SCRIPT NODATA NOPASSWORDS NOSETTINGS TABLE T_NAME;
SELECT SQL FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'T_NAME';
```

The first one also returns some unrelated elements. Actually you can dump the whole database with it, check its documentation for details:
https://h2database.com/html/commands.html#script

The second one returns only the SQL of the specified table (unless you have tables with the same name in multiple schemas, but you can add AND SCHEMA_NAME = 'PUBLIC' to exclude them). Note that you need to specify 'T_NAME' in its real case, if you're using ;DATABASE_TO_LOWER=TRUE setting it is 't_name'. Beware: presence of INFORMATION_SCHEMA.TABLES.SQL column in future versions of H2 is not guaranteed.

ContentLicense: CC BY-SA 4.0

Title: H2 ddl create table

Body:
I would like to know if it is possible to get the H2 ddl create table from the

http://localhost:5678/pecador/console/

I have tried show columns from T_USER ;

but it only shows the columns

Body:
If you want to get SQL for a table T_USER, you can use one of two following commands in H2:

SCRIPT NODATA NOPASSWORDS NOSETTINGS TABLE T_NAME;
SELECT SQL FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'T_NAME';

The first one also returns some unrelated elements. Actually you can dump the whole database with it, check its documentation for details:
https://h2database.com/html/commands.html#script

The second one returns only the SQL of the specified table (unless you have tables with the same name in multiple schemas, but you can add AND SCHEMA_NAME = 'PUBLIC' to exclude them). Note that you need to specify 'T_NAME' in its real case, if you're using ;DATABASE_TO_LOWER=TRUE setting it is 't_name'. Beware: presence of INFORMATION_SCHEMA.TABLES.SQL column in future versions of H2 is not guaranteed.


问:
Understanding list `pop index out of range`
I'm trying to arrange a list generated in ascending order.

Using remove in my while loop works. But if I use pop I keep getting an IndexError: Pop index out of range.

I also tried increasing the length of values list when the last iteration will be carried out; to while len(values) > 1 and I'm still getting the same error.

Can anyone please explain why I keep getting this error when using pop but not when using remove?

```<code>
#import randrange
from random import randrange

#generate a list of values
values = []
for i in range(1, 10):
   values.append(randrange(1, 10))
print(values)

#initiate list for keeping minimum values
new_values = []

#define how least value would be determined
while len(values) > 0 :
   new_values.append(min(values))
   values.pop(min(values))

print(new_values)

</code>```

答:
pop takes an index into the list, not a value present in the list. See list.pop([i]).

Also see the docs on sorted.

Body:
I'm trying to arrange a list generated in ascending order.

Using remove in my while loop works. But if I use pop I keep getting an IndexError: Pop index out of range.

I also tried increasing the length of values list when the last iteration will be carried out; to while len(values) > 1 and I'm still getting the same error.

Can anyone please explain why I keep getting this error when using pop but not when using remove?

#import randrange
from random import randrange

#generate a list of values
values = []
for i in range(1, 10):
   values.append(randrange(1, 10))
print(values)

#initiate list for keeping minimum values
new_values = []

#define how least value would be determined
while len(values) > 0 :
   new_values.append(min(values))
   values.pop(min(values))

print(new_values)


问:
how to return value before deleting it?
I would like to jump over two nodes, in order to have access to penultimate node as well as last node:

```<code>#include <stdio.h>
#include <stdlib.h>

struct node {
    int value;
    struct node *next;
};
typedef struct node node_t;

//prototypes
node_t *create(int);
node_t *push(node_t *, node_t **);
node_t *shift(node_t *);

int main(void){
    node_t *h = 0;

    for(int i =0; i<23 ; i++)
        push(create(i+1),&h);

    shift(h);

}

node_t *create(int value){
    node_t *new = malloc(sizeof(node_t));
    new->value=value;
    //new->next=0;
    return new;
}

node_t *push(node_t *new, node_t **head){
    new->next = *head;
    *head=new;
    return new;
}

node_t *shift(node_t *head){
    node_t *tmp = head; //do not change head pointer
    for(;tmp;tmp=tmp->next->next); //jump over 2 nodes, to have access to last node and penultimate
    node_t *last = tmp->next; //one before null node (the very last), the one I want return;
    tmp->next = 0; //now delete it, so last node will become penultimate one
    return last;
}
</code>```

But that would gives - command terminated. I know i am returning null, but how can I return before delete the node? Because return is the last command to be executed, it has to be the last. But then I am returning 0. How to get from that?

答:
Correct  way to find penultimate node, without crashing, and handling cases where there are less than 2 nodes in the list:</p>

```<code>for(;tmp && tmp->next && tmp->next->next;tmp=tmp->next);

if (tmp && tmp->next && tmp->next->next {
   // tmp is the penultimate node
} else {
   // List does not have at least 2 nodes
}
</code>```

Title: how to return value before deleting it?

Body:
I would like to jump over two nodes, in order to have access to penultimate node as well as last node:

#include <stdio.h>
#include <stdlib.h>

struct node {
    int value;
    struct node *next;
};
typedef struct node node_t;

//prototypes
node_t *create(int);
node_t *push(node_t *, node_t **);
node_t *shift(node_t *);

int main(void){
    node_t *h = 0;

    for(int i =0; i<23 ; i++)
        push(create(i+1),&h);

    shift(h);

}

node_t *create(int value){
    node_t *new = malloc(sizeof(node_t));
    new->value=value;
    //new->next=0;
    return new;
}

node_t *push(node_t *new, node_t **head){
    new->next = *head;
    *head=new;
    return new;
}

node_t *shift(node_t *head){
    node_t *tmp = head; //do not change head pointer
    for(;tmp;tmp=tmp->next->next); //jump over 2 nodes, to have access to last node and penultimate
    node_t *last = tmp->next; //one before null node (the very last), the one I want return;
    tmp->next = 0; //now delete it, so last node will become penultimate one
    return last;
}

But that would gives - command terminated. I know i am returning null, but how can I return before delete the node? Because return is the last command to be executed, it has to be the last. But then I am returning 0. How to get from that?

Body:
Correct  way to find penultimate node, without crashing, and handling cases where there are less than 2 nodes in the list:

for(;tmp && tmp->next && tmp->next->next;tmp=tmp->next);

if (tmp && tmp->next && tmp->next->next {
   // tmp is the penultimate node
} else {
   // List does not have at least 2 nodes
}


问:
PHP json_encode size limit?
I'm using a PHP proxy to get the contents of a file. I want to search through that file using the powerfull jQuery options, without having to write all kinds of queries in PHP. Here is my PHP code:

```<code>$page = file_get_contents( filter_var( $_POST[url], FILTER_SANITIZE_URL ) );
die( json_encode( $page ) );
</code>```

If the page loaded gets too big PHP will read the entire document, but json_encoding it will only give the first part of the file, not the entire file. I can't find anything about a size limit on json passed data, but apparently there is one.

the question: is there a workaround to prevent only part of the file being transfered?

I need to grab files from other domains, so reading the contents of a file in jQuery is not really an option.

答:
I resolved similar case.
So, I write here.

My website also responded broken jsondata. It is about 8MB.

The cause was the storage has not free.
The result of command of df was displayed "used 100%".

I deleted some files. Then, resolved.

ContentLicense: CC BY-SA 3.0

Title: PHP json_encode size limit?

Body:
I'm using a PHP proxy to get the contents of a file. I want to search through that file using the powerfull jQuery options, without having to write all kinds of queries in PHP. Here is my PHP code:

$page = file_get_contents( filter_var( $_POST[url], FILTER_SANITIZE_URL ) );
die( json_encode( $page ) );

If the page loaded gets too big PHP will read the entire document, but json_encoding it will only give the first part of the file, not the entire file. I can't find anything about a size limit on json passed data, but apparently there is one.

the question: is there a workaround to prevent only part of the file being transfered?

I need to grab files from other domains, so reading the contents of a file in jQuery is not really an option.


问:
Override function with both list and object as argument in Typescript
Can someone explain me why the first override's signature is not compatible with the implementation signature?

I want to have a function that takes both standard list arguments as well as "named" arguments in the form of an object.

I saw it done e.g. in react-query's useQuery hook. I had a look at their type definitions and they seem to be doing pretty much the same as I do.

Here's my attempt:

interface Args {
  readonly a: string;
  readonly b?: boolean;
}

function foo(args: Args): number;
function foo(...args: [Args['a'], Args['b']?]): number;
function foo(a: string, b?: boolean): number { /* IMPLEMENTATION */ }
```

<p>I get</p>

<blockquote>
  <p><code>This overload signature is not compatible with its implementation signature. ts(2394)</code></p>
</blockquote>

<p>error.</p>

<p>I get that object as an argument is something different than a list of arguments, so I don't really get how it actually works in <code>react-query</code>.</p>

<p>From inspection of their code however, they seem to define types (in <code>*.d.ts</code> file) of their <code>useQuery</code> export in similar way (see <code>types/index.d.ts</code>) and the exported implementation seems to be just spreading the arguments:</p>

<p><code>src/useQuery.js</code>:</p>

```...
export function useQuery(...args) {
...
```

As spread is not possible in TS if the argument is not of an Array type, I'm not sure how to simulate this behavior.

答:
The final function in an overload list in TypeScript is the implementation function and isn't recognized as a possible signature for the function call. The implementation should be able to account for the shape of all the listed overload signatures but TypeScript is only looking at those overload signatures (and not the final implementation call) when type checking.

The following is likely what you're aiming for:

function foo(args: Args): number;
function foo(args: string, b?: boolean): number
function foo(args: Args | string, b?: boolean): number { /* IMPLEMENTATION */ }
```

Sources:

https://www.typescriptlang.org/docs/handbook/functions.html
-- Last paragraph on that page explains this behaviour

TypeScript function overloading --  More detailed explanation

Body:
Can someone explain me why the first override's signature is not compatible with the implementation signature?

I want to have a function that takes both standard list arguments as well as "named" arguments in the form of an object.

I saw it done e.g. in react-query's useQuery hook. I had a look at their type definitions and they seem to be doing pretty much the same as I do.

Here's my attempt:

interface Args {
  readonly a: string;
  readonly b?: boolean;
}

function foo(args: Args): number;
function foo(...args: [Args['a'], Args['b']?]): number;
function foo(a: string, b?: boolean): number { /* IMPLEMENTATION */ }

I get

This overload signature is not compatible with its implementation signature. ts(2394)

error.

I get that object as an argument is something different than a list of arguments, so I don't really get how it actually works in react-query.

From inspection of their code however, they seem to define types (in *.d.ts file) of their useQuery export in similar way (see types/index.d.ts) and the exported implementation seems to be just spreading the arguments:

src/useQuery.js:

...
export function useQuery(...args) {
...

As spread is not possible in TS if the argument is not of an Array type, I'm not sure how to simulate this behavior.

Body:
The final function in an overload list in TypeScript is the implementation function and isn't recognized as a possible signature for the function call. The implementation should be able to account for the shape of all the listed overload signatures but TypeScript is only looking at those overload signatures (and not the final implementation call) when type checking.

The following is likely what you're aiming for:

function foo(args: Args): number;
function foo(args: string, b?: boolean): number
function foo(args: Args | string, b?: boolean): number { /* IMPLEMENTATION */ }

Sources:

https://www.typescriptlang.org/docs/handbook/functions.html
-- Last paragraph on that page explains this behaviour

TypeScript function overloading --  More detailed explanation


问:
Is it possible to automate MySQL using python?
I am a beginner to python and programming in general. But I've been wondering if it is possible to write a python script that would automate tasks on MySQL, like creating new database, user, table etc. For example I would ask the user for name input and after that I would want my script to automatically create a name_db database. I forgot to add that I need the commands not only to execute but to show on my cmd one after another like I wrote them manually. Is it possible to do? </p>

答:
Yes, of course, you can.
You have to learn about MySQL connection with python
link: https://www.datacamp.com/community/tutorials/mysql-python

And if you going to learn web I suggest Django, all database connections are already there.


问:
How to check rows value and update an already existent column with them
I want to update a column (Names) as follows:

if the last character of a word in Names ends with a and is not pizza, then assign c;

if the last character of a word in Names ends with o or i or it is pizza, then assign m.

The column to check is

```<code>Names

choriocarcinoma
medulloblastoma
phenylketonuria
counterguerilla
lymphogranuloma
archipelago
braggadocio
chiaroscuro
decrescendo
violoncello
accelerando
afficionado
tachyarrhythmia
teratocarcinoma
caudillismo
cinquecento
countermemo
oligodendroglia
pleuropneumonia
pizza
</code>```

To extract the last character I did:

```<code>df['Names'] = [x.strip()[-1] for x in df['Names']]
</code>```

to check and assign the new values, I did:

```<code>if df['Names'] == 'a':
   df['Names'] = 'c'
elif df['Names'] in ('o','i','pizza'):
   df['Names'] = 'm'
else:
   df['Names'] = 'Other'
</code>```

but it does not work. Also

```<code>def applyFunc(s):
    if s == 'a' and s != 'pizza':
        return 'c'
    elif s in ('o','i', 'pizza'):
        return 'm'
    return 'other'
</code>```

does not work.

Could you tell me where the error is?

答:
IIUC, np.select and Series.str.endswith

```<code>import numpy as np
c11 = df['Names'].str.endswith('a')
c12 = df['Names'].ne('pizza')
c21 = df['Names'].str.endswith('i')
c22 = df['Names'].str.endswith('o')
c23 = ~c12

c1 = c11 & c12
c2 = c21 | c22 | c23

df['new'] = np.select((c1, c2), ('c', 'm'), default='other')
print(df)
</code>```

Output

```<code>              Names new
0   choriocarcinoma   c
1   medulloblastoma   c
2   phenylketonuria   c
3   counterguerilla   c
4   lymphogranuloma   c
5       archipelago   m
6       braggadocio   m
7       chiaroscuro   m
8       decrescendo   m
9       violoncello   m
10      accelerando   m
11      afficionado   m
12  tachyarrhythmia   c
13  teratocarcinoma   c
14      caudillismo   m
15      cinquecento   m
16      countermemo   m
17  oligodendroglia   c
18  pleuropneumonia   c
19            pizza   m
</code>```

Body:
I want to update a column (Names) as follows:

if the last character of a word in Names ends with a and is not pizza, then assign c;

if the last character of a word in Names ends with o or i or it is pizza, then assign m.

The column to check is

Names

choriocarcinoma
medulloblastoma
phenylketonuria
counterguerilla
lymphogranuloma
archipelago
braggadocio
chiaroscuro
decrescendo
violoncello
accelerando
afficionado
tachyarrhythmia
teratocarcinoma
caudillismo
cinquecento
countermemo
oligodendroglia
pleuropneumonia
pizza

To extract the last character I did:

df['Names'] = [x.strip()[-1] for x in df['Names']]

to check and assign the new values, I did:

if df['Names'] == 'a':
   df['Names'] = 'c'
elif df['Names'] in ('o','i','pizza'):
   df['Names'] = 'm'
else:
   df['Names'] = 'Other'

but it does not work. Also

def applyFunc(s):
    if s == 'a' and s != 'pizza':
        return 'c'
    elif s in ('o','i', 'pizza'):
        return 'm'
    return 'other'

does not work.

Could you tell me where the error is?

Body:
IIUC, np.select and Series.str.endswith

import numpy as np
c11 = df['Names'].str.endswith('a')
c12 = df['Names'].ne('pizza')
c21 = df['Names'].str.endswith('i')
c22 = df['Names'].str.endswith('o')
c23 = ~c12

c1 = c11 & c12
c2 = c21 | c22 | c23

df['new'] = np.select((c1, c2), ('c', 'm'), default='other')
print(df)

Output

              Names new
0   choriocarcinoma   c
1   medulloblastoma   c
2   phenylketonuria   c
3   counterguerilla   c
4   lymphogranuloma   c
5       archipelago   m
6       braggadocio   m
7       chiaroscuro   m
8       decrescendo   m
9       violoncello   m
10      accelerando   m
11      afficionado   m
12  tachyarrhythmia   c
13  teratocarcinoma   c
14      caudillismo   m
15      cinquecento   m
16      countermemo   m
17  oligodendroglia   c
18  pleuropneumonia   c
19            pizza   m


问:
Spring Boot Embedded Apache Derby
I know a Similar question has been asked and answered before. But i've tried the solutions from the previous answers and they are not working out. I've tried out as much as i could and hope someone can shed some lights over the problem i'm facing.

Configuring embedded Derby in Spring Boot app

Spring-boot error using Apache Derby as embedded database

Spring Boot non-embedded Derby database?

And a lot more from the web. But seriously, i don't see a solution yet for the probelm i'm facing.

Here is my code,

application.properties

```<code>spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.url=jdbc:derby:memory:mydb;create=true
spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver
</code>```

ApacheDerbyExample.java

```<code>package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.CommandLineRunner;

@ComponentScan("com.app")
@SpringBootApplication
public class ApacheDerbyExample implements CommandLineRunner {

    public static void main(String[] args) {
        System.out.println("STARTING THE APPLICATION");
        SpringApplication.run(ApacheDerbyExample.class, args);
        System.out.println("APPLICATION FINISHED");
    }

    @Override
    public void run(String... args) {
        System.out.println("EXECUTING : command line runner");
        //org.apache.derby.jdbc.ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
        //ds.setDatabaseName("mydb");
    }
}
</code>```

And from my pom.xml,

        org.apache.derby
        derby
        10.15.2.0
        runtime

        org.apache.derby
        derbyclient
        10.15.2.0
        runtime

https://github.com/manikandaraj/practice-code/tree/master/java/spring-boot-apache-derby-embedded

I'm building the project using,

```<code>mvn clean package
</code>```

And when i run,

```<code>java -jar -Dspring.config.location=config/spring-conf/application.properties target/derby-101-0.0.1-SNAPSHOT.jar
</code>```

But i'm getting the following error,

```<code>Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-31 02:15:43.402 ERROR 8733 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Dbcp2.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.commons.dbcp2.BasicDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: org.apache.derby.jdbc.EmbeddedDriver
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
</code>```

I'm just trying to use the Apache Derby Embedded Database in my Application and with the dependencies defined in Maven, I don't know why i'm still getting the error.

I want to be able to use the Embedded Pooled Data Source(Derby) and run some query on CSV content I get from REST APIs.

答:
Mani,
Please add below annotation in your Application class

```<code>@EnableAutoConfiguration(exclude = { //
    DataSourceAutoConfiguration.class, //
    DataSourceTransactionManagerAutoConfiguration.class, //
    HibernateJpaAutoConfiguration.class }
</code>```

)

Also remove the below version from your pom.xml..

```<code><version>2.7.0</version>
</code>```

Title: Spring Boot Embedded Apache Derby

Body:
I know a Similar question has been asked and answered before. But i've tried the solutions from the previous answers and they are not working out. I've tried out as much as i could and hope someone can shed some lights over the problem i'm facing.

Configuring embedded Derby in Spring Boot app

Spring-boot error using Apache Derby as embedded database

Spring Boot non-embedded Derby database?

And a lot more from the web. But seriously, i don't see a solution yet for the probelm i'm facing.

Here is my code,

application.properties

spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.url=jdbc:derby:memory:mydb;create=true
spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver

ApacheDerbyExample.java

package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.CommandLineRunner;

@ComponentScan("com.app")
@SpringBootApplication
public class ApacheDerbyExample implements CommandLineRunner {

    public static void main(String[] args) {
        System.out.println("STARTING THE APPLICATION");
        SpringApplication.run(ApacheDerbyExample.class, args);
        System.out.println("APPLICATION FINISHED");
    }

    @Override
    public void run(String... args) {
        System.out.println("EXECUTING : command line runner");
        //org.apache.derby.jdbc.ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
        //ds.setDatabaseName("mydb");
    }
}

And from my pom.xml,

        org.apache.derby
        derby
        10.15.2.0
        runtime

        org.apache.derby
        derbyclient
        10.15.2.0
        runtime

https://github.com/manikandaraj/practice-code/tree/master/java/spring-boot-apache-derby-embedded

I'm building the project using,

mvn clean package

And when i run,

java -jar -Dspring.config.location=config/spring-conf/application.properties target/derby-101-0.0.1-SNAPSHOT.jar

But i'm getting the following error,

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-31 02:15:43.402 ERROR 8733 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Dbcp2.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.commons.dbcp2.BasicDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: org.apache.derby.jdbc.EmbeddedDriver
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]

I'm just trying to use the Apache Derby Embedded Database in my Application and with the dependencies defined in Maven, I don't know why i'm still getting the error.

I want to be able to use the Embedded Pooled Data Source(Derby) and run some query on CSV content I get from REST APIs.

Body:
Mani,
Please add below annotation in your Application class

@EnableAutoConfiguration(exclude = { //
    DataSourceAutoConfiguration.class, //
    DataSourceTransactionManagerAutoConfiguration.class, //
    HibernateJpaAutoConfiguration.class }

)

Also remove the below version from your pom.xml..

<version>2.7.0</version>


问:
MDX Help and tutorial
Hello guys again here with my MDX headaches (why does my boss think programmer = solve anything computer related).

The measure of the Filter
Productos is compose this way [Casa].[Linea].[Producto] and I am trying to filter by Linea 72 and 74, outside this anyone know a good tutorial online or book for MDX?

Anyways here my MDX so far

SELECT 'Dic  1 2010' AS Fecha, A.*

from (SELECT
"[Measures].[Piezas Dia]",
"[Measures].[P Redondeadas]"
FROM openrowset('MSOLAP'
,'DATASOURCE=XXX
;Initial Catalog=XXX
; User Id=XXX;Password=XXX;',
'WITH  SET [Lineas] AS ''
FILTER(DESCENDANTS([PRODUCTOS],[LINEA],SELF),

ANCESTOR([PRODUCTOS].CURRENTMEMBER,SELF) <> [74 VARIOS] AND

ANCESTOR([PRODUCTOS].CURRENTMEMBER,SELF) <> [72 VARIOS] )''
Member [Measures].[Piezas Dia]   AS ''sum([Lineas],[Measures].[|P| Venta Dia Año Actual])''

Member [Measures].[P Redondeadas] AS ''sum([Lineas],[Measures].[Venta Piezas Redondeadas])''
SELECT {[Measures].[Piezas Dia], [Measures].[P Redondeadas]} on columns
FROM [Venta Gerencial]
WHERE ([Fecha Diario].[Fecha].&[2010].&[12].&[1])')) as A

Thanks for all help provided.

答: You can find an 'gentle' introduction to MDX here</p>

Title: MDX Help and tutorial


问:
Understanding a MySQL table's maximum column width
I'm trying to understand how MySQL 8.0 row width works, and some parts are quite confusing.  The easiest way to explain my challenge is to show it in code.  Please don't focus on the fact that using so many columns could be a bad db design; I know that, this is just to help illustrate my confusion.

If I create a table with 186 columns using the below statement, the maximum varchar length I can use is 116.  Using utf8, that has an apparent length of 64,728 bytes.  (186 columns x 116 char/col x 3 byte/char).  And it works for me.

```<code>create table RowTest(Row1 varchar(116),Row2 varchar(116),Row3 varchar(116),Row4 varchar(116),Row5 varchar(116),Row6 varchar(116),Row7 varchar(116),Row8 varchar(116),Row9 varchar(116),Row10 varchar(116),Row11 varchar(116),Row12 varchar(116),Row13 varchar(116),Row14 varchar(116),Row15 varchar(116),Row16 varchar(116),Row17 varchar(116),Row18 varchar(116),Row19 varchar(116),Row20 varchar(116),Row21 varchar(116),Row22 varchar(116),Row23 varchar(116),Row24 varchar(116),Row25 varchar(116),Row26 varchar(116),Row27 varchar(116),Row28 varchar(116),Row29 varchar(116),Row30 varchar(116),Row31 varchar(116),Row32 varchar(116),Row33 varchar(116),Row34 varchar(116),Row35 varchar(116),Row36 varchar(116),Row37 varchar(116),Row38 varchar(116),Row39 varchar(116),Row40 varchar(116),Row41 varchar(116),Row42 varchar(116),Row43 varchar(116),
Row44 varchar(116),Row45 varchar(116),Row46 varchar(116),Row47 varchar(116),Row48 varchar(116),Row49 varchar(116),Row50 varchar(116),Row51 varchar(116),Row52 varchar(116),Row53 varchar(116),Row54 varchar(116),Row55 varchar(116),Row56 varchar(116),Row57 varchar(116),Row58 varchar(116),Row59 varchar(116),Row60 varchar(116),Row61 varchar(116),Row62 varchar(116),Row63 varchar(116),Row64 varchar(116),Row65 varchar(116),Row66 varchar(116),Row67 varchar(116),Row68 varchar(116),Row69 varchar(116),Row70 varchar(116),Row71 varchar(116),Row72 varchar(116),Row73 varchar(116),Row74 varchar(116),Row75 varchar(116),Row76 varchar(116),Row77 varchar(116),Row78 varchar(116),Row79 varchar(116),Row80 varchar(116),Row81 varchar(116),Row82 varchar(116),Row83 varchar(116),Row84 varchar(116),Row85 varchar(116),Row86 varchar(116),Row87 varchar(116),
Row88 varchar(116),Row89 varchar(116),Row90 varchar(116),Row91 varchar(116),Row92 varchar(116),Row93 varchar(116),Row94 varchar(116),Row95 varchar(116),Row96 varchar(116),Row97 varchar(116),Row98 varchar(116),Row99 varchar(116),Row100 varchar(116),Row101 varchar(116),Row102 varchar(116),Row103 varchar(116),Row104 varchar(116),Row105 varchar(116),Row106 varchar(116),Row107 varchar(116),Row108 varchar(116),Row109 varchar(116),Row110 varchar(116),Row111 varchar(116),Row112 varchar(116),Row113 varchar(116),Row114 varchar(116),Row115 varchar(116),Row116 varchar(116),Row117 varchar(116),Row118 varchar(116),Row119 varchar(116),Row120 varchar(116),Row121 varchar(116),Row122 varchar(116),Row123 varchar(116),Row124 varchar(116),Row125 varchar(116),Row126 varchar(116),Row127 varchar(116),Row128 varchar(116),Row129 varchar(116),Row130 varchar(116),Row131 varchar(116),
Row132 varchar(116),Row133 varchar(116),Row134 varchar(116),Row135 varchar(116),Row136 varchar(116),Row137 varchar(116),Row138 varchar(116),Row139 varchar(116),Row140 varchar(116),Row141 varchar(116),Row142 varchar(116),Row143 varchar(116),Row144 varchar(116),Row145 varchar(116),Row146 varchar(116),Row147 varchar(116),Row148 varchar(116),Row149 varchar(116),Row150 varchar(116),Row151 varchar(116),Row152 varchar(116),Row153 varchar(116),Row154 varchar(116),Row155 varchar(116),Row156 varchar(116),Row157 varchar(116),Row158 varchar(116),Row159 varchar(116),Row160 varchar(116),Row161 varchar(116),Row162 varchar(116),Row163 varchar(116),Row164 varchar(116),Row165 varchar(116),Row166 varchar(116),Row167 varchar(116),Row168 varchar(116),Row169 varchar(116),Row170 varchar(116),Row171 varchar(116),Row172 varchar(116),Row173 varchar(116),Row174 varchar(116),
Row175 varchar(116),Row176 varchar(116),Row177 varchar(116),Row178 varchar(116),Row179 varchar(116),Row180 varchar(116),Row181 varchar(116),Row182 varchar(116),Row183 varchar(116),Row184 varchar(116),Row185 varchar(116),Row186 varchar(116))
engine = innodb DEFAULT CHARSET=utf8 row_format = compressed;
</code>```

If I increase each row above to varchar(117), I get this error:

```<code>Error Code: 1118. Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535.
</code>```

That makes sense, because that puts us above the 65,535 boundary.  (Assuming there is some amount of hidden consumption.)

But if I create a table with ONE additional column (187 columns), the maximum varchar length I can use now drops from 116 to 13 (!).  And if I try to increase it to varchar(14), I get a completely different error.

```<code>Error Code: 1118. Row size too large (> 8126). Changing some columns to TEXT or BLOB may help. In current row format, BLOB prefix of 0 bytes is stored inline.
</code>```

It seems like going from 186 columns to 187 columns has somehow kicked me into a different boundary that is much lower.

I do know that MySQL has multiple boundaries, but I'm not sure why adding a single column would reduce the table's maximum width from 65k to 8126.  After all, with one less column, the table is much wider.

Any ideas how to understand what's going on behind the scenes?  Or is there anything published that explains exactly how to calculate table width against each of the boundaries in this scenario?

Thanks in advance.

答:
The answer lies on the mysql manual page you linked in your comment: https://dev.mysql.com/doc/refman/8.0/en/innodb-row-format.html

Your innodb table uses compressed row format, which has the same storage characteristics as the dynamic row format.

One of the characteristics of the dynamic row format is:

Whether columns are stored off-page depends on the page size and the total size of the row. When a row is too long, the longest columns are chosen for off-page storage until the clustered index record fits on the B-tree page. TEXT and BLOB columns that are less than or equal to 40 bytes are stored in line.

The varchar(116) fields are probably considered too long and therefore are stored in the overflow pages. Therefore, only a 20 byte pointer to the overflow page is stored within the row itself and 186×20 bytes is the size that counts toward the innodb limit of 8126 bytes per row. However, their full size counts towards the mysql limit of 65535 bytes.

But when you change the field types to varchar(13) or varchar(14), that's probably considered as short (40 byte limit applies to text and blob types only), therefore are stored within the row, not in the overflow pages. Therefore, their entire length counts toward the innodb limit of 8126 bytes. The row contains additional data as well.

Body:
I'm trying to understand how MySQL 8.0 row width works, and some parts are quite confusing.  The easiest way to explain my challenge is to show it in code.  Please don't focus on the fact that using so many columns could be a bad db design; I know that, this is just to help illustrate my confusion.

If I create a table with 186 columns using the below statement, the maximum varchar length I can use is 116.  Using utf8, that has an apparent length of 64,728 bytes.  (186 columns x 116 char/col x 3 byte/char).  And it works for me.

create table RowTest(Row1 varchar(116),Row2 varchar(116),Row3 varchar(116),Row4 varchar(116),Row5 varchar(116),Row6 varchar(116),Row7 varchar(116),Row8 varchar(116),Row9 varchar(116),Row10 varchar(116),Row11 varchar(116),Row12 varchar(116),Row13 varchar(116),Row14 varchar(116),Row15 varchar(116),Row16 varchar(116),Row17 varchar(116),Row18 varchar(116),Row19 varchar(116),Row20 varchar(116),Row21 varchar(116),Row22 varchar(116),Row23 varchar(116),Row24 varchar(116),Row25 varchar(116),Row26 varchar(116),Row27 varchar(116),Row28 varchar(116),Row29 varchar(116),Row30 varchar(116),Row31 varchar(116),Row32 varchar(116),Row33 varchar(116),Row34 varchar(116),Row35 varchar(116),Row36 varchar(116),Row37 varchar(116),Row38 varchar(116),Row39 varchar(116),Row40 varchar(116),Row41 varchar(116),Row42 varchar(116),Row43 varchar(116),
Row44 varchar(116),Row45 varchar(116),Row46 varchar(116),Row47 varchar(116),Row48 varchar(116),Row49 varchar(116),Row50 varchar(116),Row51 varchar(116),Row52 varchar(116),Row53 varchar(116),Row54 varchar(116),Row55 varchar(116),Row56 varchar(116),Row57 varchar(116),Row58 varchar(116),Row59 varchar(116),Row60 varchar(116),Row61 varchar(116),Row62 varchar(116),Row63 varchar(116),Row64 varchar(116),Row65 varchar(116),Row66 varchar(116),Row67 varchar(116),Row68 varchar(116),Row69 varchar(116),Row70 varchar(116),Row71 varchar(116),Row72 varchar(116),Row73 varchar(116),Row74 varchar(116),Row75 varchar(116),Row76 varchar(116),Row77 varchar(116),Row78 varchar(116),Row79 varchar(116),Row80 varchar(116),Row81 varchar(116),Row82 varchar(116),Row83 varchar(116),Row84 varchar(116),Row85 varchar(116),Row86 varchar(116),Row87 varchar(116),
Row88 varchar(116),Row89 varchar(116),Row90 varchar(116),Row91 varchar(116),Row92 varchar(116),Row93 varchar(116),Row94 varchar(116),Row95 varchar(116),Row96 varchar(116),Row97 varchar(116),Row98 varchar(116),Row99 varchar(116),Row100 varchar(116),Row101 varchar(116),Row102 varchar(116),Row103 varchar(116),Row104 varchar(116),Row105 varchar(116),Row106 varchar(116),Row107 varchar(116),Row108 varchar(116),Row109 varchar(116),Row110 varchar(116),Row111 varchar(116),Row112 varchar(116),Row113 varchar(116),Row114 varchar(116),Row115 varchar(116),Row116 varchar(116),Row117 varchar(116),Row118 varchar(116),Row119 varchar(116),Row120 varchar(116),Row121 varchar(116),Row122 varchar(116),Row123 varchar(116),Row124 varchar(116),Row125 varchar(116),Row126 varchar(116),Row127 varchar(116),Row128 varchar(116),Row129 varchar(116),Row130 varchar(116),Row131 varchar(116),
Row132 varchar(116),Row133 varchar(116),Row134 varchar(116),Row135 varchar(116),Row136 varchar(116),Row137 varchar(116),Row138 varchar(116),Row139 varchar(116),Row140 varchar(116),Row141 varchar(116),Row142 varchar(116),Row143 varchar(116),Row144 varchar(116),Row145 varchar(116),Row146 varchar(116),Row147 varchar(116),Row148 varchar(116),Row149 varchar(116),Row150 varchar(116),Row151 varchar(116),Row152 varchar(116),Row153 varchar(116),Row154 varchar(116),Row155 varchar(116),Row156 varchar(116),Row157 varchar(116),Row158 varchar(116),Row159 varchar(116),Row160 varchar(116),Row161 varchar(116),Row162 varchar(116),Row163 varchar(116),Row164 varchar(116),Row165 varchar(116),Row166 varchar(116),Row167 varchar(116),Row168 varchar(116),Row169 varchar(116),Row170 varchar(116),Row171 varchar(116),Row172 varchar(116),Row173 varchar(116),Row174 varchar(116),
Row175 varchar(116),Row176 varchar(116),Row177 varchar(116),Row178 varchar(116),Row179 varchar(116),Row180 varchar(116),Row181 varchar(116),Row182 varchar(116),Row183 varchar(116),Row184 varchar(116),Row185 varchar(116),Row186 varchar(116))
engine = innodb DEFAULT CHARSET=utf8 row_format = compressed;

If I increase each row above to varchar(117), I get this error:

Error Code: 1118. Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535.

That makes sense, because that puts us above the 65,535 boundary.  (Assuming there is some amount of hidden consumption.)

But if I create a table with ONE additional column (187 columns), the maximum varchar length I can use now drops from 116 to 13 (!).  And if I try to increase it to varchar(14), I get a completely different error.

Error Code: 1118. Row size too large (> 8126). Changing some columns to TEXT or BLOB may help. In current row format, BLOB prefix of 0 bytes is stored inline.

It seems like going from 186 columns to 187 columns has somehow kicked me into a different boundary that is much lower.

I do know that MySQL has multiple boundaries, but I'm not sure why adding a single column would reduce the table's maximum width from 65k to 8126.  After all, with one less column, the table is much wider.

Any ideas how to understand what's going on behind the scenes?  Or is there anything published that explains exactly how to calculate table width against each of the boundaries in this scenario?

Thanks in advance.


问:
Streaming tweets from private twitter account
I am trying to create a twitter stream for accounts that I currently follow. Most of them are private. I was wondering if there was a way to create a twitter stream that catches tweets from private accounts?

Currently using Tweepy, but open to other APIs.

```<code>class listener(StreamListener):
    print("Running stream")
    def on_data(self, data):

        print("data")
        return(True)

    def on_error(self, status):

        print(status)

auth = OAuthHandler(ckey, csecret)

# Access to user's access key and access secret
auth.set_access_token(atoken, asecret)

# Calling api
api = API(auth)

twitterStream = Stream(auth, listener())
twitterStream.filter(follow=['x'])
</code>```

答: No, there is not. The only way you could do this is via the Account Activity (webhook) API and if those private accounts consented to sign in and deliver events to your application. You cannot simply stream events from protected accounts.</p>

Body:
I am trying to create a twitter stream for accounts that I currently follow. Most of them are private. I was wondering if there was a way to create a twitter stream that catches tweets from private accounts?

Currently using Tweepy, but open to other APIs.

class listener(StreamListener):
    print("Running stream")
    def on_data(self, data):

        print("data")
        return(True)

    def on_error(self, status):

        print(status)

auth = OAuthHandler(ckey, csecret)

# Access to user's access key and access secret
auth.set_access_token(atoken, asecret)

# Calling api
api = API(auth)

twitterStream = Stream(auth, listener())
twitterStream.filter(follow=['x'])


问:
Anyone knows what is in Skimage TIfffile save, unknown error "type b".?
I am getting a strange error saving a tiff file (stack grayscale), any idea?:

File
  "C:\Users\ptyimg_np.MT00200169\Anaconda3\lib\site-packages\tifffile\tifffile.py",
  line 1241, in save
      sampleformat = {'u': 1, 'i': 2, 'f': 3, 'c': 6}[datadtype.kind] KeyError: 'b'

my code is

```<code> #!/usr/bin/env python
# -*- coding: utf-8 -*-
from skimage.morphology import watershed
from skimage.feature import peak_local_max
from scipy import ndimage
from skimage import img_as_float
from skimage import exposure,io
from skimage import external
from skimage.color import rgb2gray
from skimage.filters import threshold_local  , threshold_niblack
import numpy as np
import  tifffile
from joblib import Parallel, delayed
import sys

# Load an example image
input_namefile = sys.argv[1]
output_namefile = 'seg_'+ input_namefile

#Settings
block_size = 25 #Size block of the local thresholding

img = io.imread(input_namefile, plugin='tifffile')
thresh =  threshold_niblack(img, window_size=block_size , k=0.8) #
res = img > thresh
res = np.asanyarray(res)
print("saving segmentation")
tifffile.imsave(output_namefile, res , photometric='minisblack' )
</code>```

答:
It looks like the error is caused by a bug in writing boolean images in your installed version of tifffile. However, the bug has been fixed in more recent versions (I have 2020.2.16 in my current environment). On my machine, this works fine:

import numpy as np
import tifffile

tifffile.imsave('test.tiff', np.random.random((10, 10)) > 0.5)
```

and the line causing a crash in your version is never executed in the case of a boolean image.

So, long story short, use python -m pip install -U tifffile to upgrade your version of tifffile, and your program should work!

Body:
I am getting a strange error saving a tiff file (stack grayscale), any idea?:

File
  "C:\Users\ptyimg_np.MT00200169\Anaconda3\lib\site-packages\tifffile\tifffile.py",
  line 1241, in save
      sampleformat = {'u': 1, 'i': 2, 'f': 3, 'c': 6}[datadtype.kind] KeyError: 'b'

my code is

 #!/usr/bin/env python
# -*- coding: utf-8 -*-
from skimage.morphology import watershed
from skimage.feature import peak_local_max
from scipy import ndimage
from skimage import img_as_float
from skimage import exposure,io
from skimage import external
from skimage.color import rgb2gray
from skimage.filters import threshold_local  , threshold_niblack
import numpy as np
import  tifffile
from joblib import Parallel, delayed
import sys

# Load an example image
input_namefile = sys.argv[1]
output_namefile = 'seg_'+ input_namefile

#Settings
block_size = 25 #Size block of the local thresholding

img = io.imread(input_namefile, plugin='tifffile')
thresh =  threshold_niblack(img, window_size=block_size , k=0.8) #
res = img > thresh
res = np.asanyarray(res)
print("saving segmentation")
tifffile.imsave(output_namefile, res , photometric='minisblack' )

Body:
It looks like the error is caused by a bug in writing boolean images in your installed version of tifffile. However, the bug has been fixed in more recent versions (I have 2020.2.16 in my current environment). On my machine, this works fine:

import numpy as np
import tifffile

tifffile.imsave('test.tiff', np.random.random((10, 10)) > 0.5)

and the line causing a crash in your version is never executed in the case of a boolean image.

So, long story short, use python -m pip install -U tifffile to upgrade your version of tifffile, and your program should work!


问:
Enable Sound button in Notification Channel
In MI Note 5 Pro which has latest MI UI 10.0 with Oreo, so when I try to send push notification by default sound is disable, so i am not able to enable sound programmatically when I am creating a channel for that.

In other Oreo devices notification sound is coming but in MI custom Oreo OS Sound is by default disable

Let me show my code for notification :

```<code>    var intent = Intent(mContext, HomeActivity::class.java)
    intent.putExtra(Constants.EXTRA_FROM_NOTIFICATION, true)
    var pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

    var uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

    var mBuilder = NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
            .setContentTitle(mContext.getString(R.string.app_name))
            .setContentText(mFirstContactName + " : " + mListChatWindow[0].message)
            .setPriority(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) NotificationManager.IMPORTANCE_HIGH else Notification.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setSound(uri)
            .setSmallIcon(R.drawable.ic_app_icon)
            .setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
            .setVibrate(longArrayOf(0, 100, 1000, 300))
            .setAutoCancel(true)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        var channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH)
        channel.description = "NOTIFICATION_DESCRIPTION"
        channel.lightColor = Color.LTGRAY
        channel.enableVibration(true)
        channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC

        val attributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()
        channel.setSound(uri, attributes)
        var notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }

    var notificationManager = NotificationManagerCompat.from(mContext)
    notificationManager.notify(NOTIFICATION_CHAT_ID, mBuilder.build())
</code>```

I am also set channel.setSound(uri, attributes) in cannel but sound not coming

Here is the screenshot of Notification channel see there sound icon is disabled, how to enable?

Plz help

答:
I'm facing same issue and still didn't get satisfying answer, but till that time we can workaround it like so:

```<code>final Uri NOTIFICATION_SOUND = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
RingtoneManager.getRingtone(context, NOTIFICATION_SOUND).play();
</code>```

You need to call this after:

```<code>notificationManager.notify(notificationId, notification);
</code>```

This way you will always get sound playing even if "allow sound" was turned off for your App, and the sound played will be from the system not media (expected behavior from the notifications).

And to avoid having two sounds playing at once (for the devices that don't have this issue), you can turn the sounds off like so:

1- For the Builder:

```<code>notificationBuilder.setContentTitle(title)
    .set.....
    .set.....
    .setSound(null);
</code>```

2- For the channel:

```<code>channel.setSound(null, null);
</code>```

Body:
In MI Note 5 Pro which has latest MI UI 10.0 with Oreo, so when I try to send push notification by default sound is disable, so i am not able to enable sound programmatically when I am creating a channel for that.

In other Oreo devices notification sound is coming but in MI custom Oreo OS Sound is by default disable

Let me show my code for notification :

    var intent = Intent(mContext, HomeActivity::class.java)
    intent.putExtra(Constants.EXTRA_FROM_NOTIFICATION, true)
    var pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

    var uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

    var mBuilder = NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
            .setContentTitle(mContext.getString(R.string.app_name))
            .setContentText(mFirstContactName + " : " + mListChatWindow[0].message)
            .setPriority(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) NotificationManager.IMPORTANCE_HIGH else Notification.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setSound(uri)
            .setSmallIcon(R.drawable.ic_app_icon)
            .setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
            .setVibrate(longArrayOf(0, 100, 1000, 300))
            .setAutoCancel(true)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        var channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH)
        channel.description = "NOTIFICATION_DESCRIPTION"
        channel.lightColor = Color.LTGRAY
        channel.enableVibration(true)
        channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC

        val attributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()
        channel.setSound(uri, attributes)
        var notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }

    var notificationManager = NotificationManagerCompat.from(mContext)
    notificationManager.notify(NOTIFICATION_CHAT_ID, mBuilder.build())

I am also set channel.setSound(uri, attributes) in cannel but sound not coming

Here is the screenshot of Notification channel see there sound icon is disabled, how to enable?

Plz help

Body:
I'm facing same issue and still didn't get satisfying answer, but till that time we can workaround it like so:

final Uri NOTIFICATION_SOUND = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
RingtoneManager.getRingtone(context, NOTIFICATION_SOUND).play();

You need to call this after:

notificationManager.notify(notificationId, notification);

This way you will always get sound playing even if "allow sound" was turned off for your App, and the sound played will be from the system not media (expected behavior from the notifications).

And to avoid having two sounds playing at once (for the devices that don't have this issue), you can turn the sounds off like so:

1- For the Builder:

notificationBuilder.setContentTitle(title)
    .set.....
    .set.....
    .setSound(null);

2- For the channel:

channel.setSound(null, null);


答:
Boolean select by combining str.endswith() , ampersand and |for alternative selections. Use nested np.where. Caution; np.select always the better option

```<code>import numpy as np
a= (df.Names.str.endswith('a'))&(df.Names!='pizza')
b= (df.Names.str.endswith('o'))|(df.Names.str.endswith('i'))|(df.Names=='pizza')

df['N']=np.where(a, 'c', np.where(b,'m',df.Names))
</code>```

Title: How to check rows value and update an already existent column with them

Body:
Boolean select by combining str.endswith() , ampersand and |for alternative selections. Use nested np.where. Caution; np.select always the better option

import numpy as np
a= (df.Names.str.endswith('a'))&(df.Names!='pizza')
b= (df.Names.str.endswith('o'))|(df.Names.str.endswith('i'))|(df.Names=='pizza')

df['N']=np.where(a, 'c', np.where(b,'m',df.Names))


问:
Adding a new label where clicked SWIFT
I would like to write a simple swift code for a MacOS app which add a new label wherever the user perform a mouse click in a window.

This code compiles but make the app crash:

```<code>@IBOutlet var here2 = [NSTextField]()
var count: Int = 0
func getCoordinates(){
        NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown]) {
        if self.location.x < 700 && self.location.y<750 {
            self.here2.append(NSTextField.init())
            self.here2[self.count].frame.origin = CGPoint(x: self.location.x, y: self.location.y)
            self.here2[self.count].stringValue = String(self.count)
            print("count is: " + String(self.here2.count))
            self.count+=1
        }
        return $0
    }

</code>```

答:
Here is a programmatic approach which may be run in Terminal.  Note that window.acceptsMouseMovedEvents is set to true.</p>

```<code>//May be run in Terminal with:
//swiftc label.swift -framework Cocoa -o label && ./label

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {
var window : NSWindow!
var here2 = [NSTextField]()
var count: Int = 0
let _wndW : CGFloat = 700
let _wndH : CGFloat = 750
let _labelW : CGFloat = 24
let _labelH : CGFloat = 24

@objc func getCoordinates(_ sender:AnyObject ) {
 NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown]) {
var pt: NSPoint? { self.window.mouseLocationOutsideOfEventStream }
if let location = pt {
 let msPt:NSPoint = self.window.contentView!.convert(location, from: nil)
 print("x = \(msPt.x) : y = \(msPt.y)")
 if msPt.x < (self._wndW - self._labelW) && msPt.y < (self._wndH - self._labelH ) {
 self.here2.append(NSTextField.init())
 self.here2[self.count].frame = NSMakeRect(msPt.x, msPt.y, self._labelW, self._labelH)
 print(self.here2[self.count].frame)
 self.window.contentView!.addSubview (self.here2[self.count])
 self.here2[self.count].backgroundColor = NSColor.white
 self.here2[self.count].isSelectable = false
 self.here2[self.count].stringValue = String(self.count)
 print("count is: " + String(self.here2.count))
 self.count+=1
}
}
  return $0
 }
}

func buildMenu() {
 let mainMenu = NSMenu()
 NSApp.mainMenu = mainMenu
 // **** App menu **** //
 let appMenuItem = NSMenuItem()
 mainMenu.addItem(appMenuItem)
 let appMenu = NSMenu()
 appMenuItem.submenu = appMenu
 appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
}

func buildWnd() {
 window = NSWindow(contentRect:NSMakeRect(0,0,_wndW,_wndH),styleMask:[.titled, .closable, .miniaturizable, .resizable], backing:.buffered, defer:false)
 window.center()
 window.title = "Swift Test Window"
 window.makeKeyAndOrderFront(window)
 window.acceptsMouseMovedEvents = true

// **** Button **** //
let myBtn = NSButton (frame:NSMakeRect( _wndW - 180, 15, 135, 24 ))
 myBtn.bezelStyle = .rounded
 myBtn.autoresizingMask = [.maxXMargin,.minYMargin]
 myBtn.title = "Start Label Maker"
 myBtn.action = #selector(self.getCoordinates(_:))
 window.contentView!.addSubview (myBtn)

// **** Quit btn **** //
let quitBtn = NSButton (frame:NSMakeRect( _wndW - 50, 10, 40, 40 ))
 quitBtn.bezelStyle = .circular
 quitBtn.autoresizingMask = [.minXMargin,.maxYMargin]
 quitBtn.title = "Q"
 quitBtn.action = #selector(NSApplication.terminate)
 window.contentView!.addSubview(quitBtn)
}

func applicationDidFinishLaunching(_ notification: Notification) {
 buildMenu()
 buildWnd()
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
 return true
}

}
let appDelegate = AppDelegate()

// **** main.swift **** //
let app = NSApplication.shared
app.delegate = appDelegate
app.setActivationPolicy(.regular)
app.activate(ignoringOtherApps:true)
app.run()

</code>```

Title: Adding a new label where clicked SWIFT

Body:
I would like to write a simple swift code for a MacOS app which add a new label wherever the user perform a mouse click in a window.

This code compiles but make the app crash:

@IBOutlet var here2 = [NSTextField]()
var count: Int = 0
func getCoordinates(){
        NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown]) {
        if self.location.x < 700 && self.location.y<750 {
            self.here2.append(NSTextField.init())
            self.here2[self.count].frame.origin = CGPoint(x: self.location.x, y: self.location.y)
            self.here2[self.count].stringValue = String(self.count)
            print("count is: " + String(self.here2.count))
            self.count+=1
        }
        return $0
    }

Body:
Here is a programmatic approach which may be run in Terminal.  Note that window.acceptsMouseMovedEvents is set to true.

//May be run in Terminal with:
//swiftc label.swift -framework Cocoa -o label && ./label

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {
var window : NSWindow!
var here2 = [NSTextField]()
var count: Int = 0
let _wndW : CGFloat = 700
let _wndH : CGFloat = 750
let _labelW : CGFloat = 24
let _labelH : CGFloat = 24

@objc func getCoordinates(_ sender:AnyObject ) {
 NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown]) {
var pt: NSPoint? { self.window.mouseLocationOutsideOfEventStream }
if let location = pt {
 let msPt:NSPoint = self.window.contentView!.convert(location, from: nil)
 print("x = \(msPt.x) : y = \(msPt.y)")
 if msPt.x < (self._wndW - self._labelW) && msPt.y < (self._wndH - self._labelH ) {
 self.here2.append(NSTextField.init())
 self.here2[self.count].frame = NSMakeRect(msPt.x, msPt.y, self._labelW, self._labelH)
 print(self.here2[self.count].frame)
 self.window.contentView!.addSubview (self.here2[self.count])
 self.here2[self.count].backgroundColor = NSColor.white
 self.here2[self.count].isSelectable = false
 self.here2[self.count].stringValue = String(self.count)
 print("count is: " + String(self.here2.count))
 self.count+=1
}
}
  return $0
 }
}

func buildMenu() {
 let mainMenu = NSMenu()
 NSApp.mainMenu = mainMenu
 // **** App menu **** //
 let appMenuItem = NSMenuItem()
 mainMenu.addItem(appMenuItem)
 let appMenu = NSMenu()
 appMenuItem.submenu = appMenu
 appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
}

func buildWnd() {
 window = NSWindow(contentRect:NSMakeRect(0,0,_wndW,_wndH),styleMask:[.titled, .closable, .miniaturizable, .resizable], backing:.buffered, defer:false)
 window.center()
 window.title = "Swift Test Window"
 window.makeKeyAndOrderFront(window)
 window.acceptsMouseMovedEvents = true

// **** Button **** //
let myBtn = NSButton (frame:NSMakeRect( _wndW - 180, 15, 135, 24 ))
 myBtn.bezelStyle = .rounded
 myBtn.autoresizingMask = [.maxXMargin,.minYMargin]
 myBtn.title = "Start Label Maker"
 myBtn.action = #selector(self.getCoordinates(_:))
 window.contentView!.addSubview (myBtn)

// **** Quit btn **** //
let quitBtn = NSButton (frame:NSMakeRect( _wndW - 50, 10, 40, 40 ))
 quitBtn.bezelStyle = .circular
 quitBtn.autoresizingMask = [.minXMargin,.maxYMargin]
 quitBtn.title = "Q"
 quitBtn.action = #selector(NSApplication.terminate)
 window.contentView!.addSubview(quitBtn)
}

func applicationDidFinishLaunching(_ notification: Notification) {
 buildMenu()
 buildWnd()
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
 return true
}

}
let appDelegate = AppDelegate()

// **** main.swift **** //
let app = NSApplication.shared
app.delegate = appDelegate
app.setActivationPolicy(.regular)
app.activate(ignoringOtherApps:true)
app.run()


问:
How to sort a pandas dataframe on two (or more) different columns, in a particular order

Note

df2 is the only thing that can be used here - using df or df1 would be to use data that isn't possible. The data is received as df2, it wants to be manipulated to the form of df1. Neither df1 or df can be used as part of of the solution (as df1 is the solution).

Setup test data

this is just setup for this post.

# sample data
reps = {1: "dog", 2: "ant", 3: "cat", 6: "orange", 7: "apple", 8: "grape"}
df = pd.DataFrame(
    {"one": [1, 1, 1, 2, 2, 2, 3, 3, 3], "two": [6, 7, 8, 6, 7, 8, 6, 7, 8]}
)
df = df.replace(reps).copy()
df1 = df.copy()
df2 = df.sample(frac=1, random_state=1).replace(reps).reset_index(drop=True)
```

<h1>Problem</h1>

<p>Sort <code>df1</code> so that it is in the same order as <code>df1</code>.</p>

<p><code>df2</code>:</p>

```   one     two
0  cat   grape
1  dog   grape
2  cat  orange
3  cat   apple
4  dog   apple
5  dog  orange
6  ant   apple
7  ant  orange
8  ant   grape
```

<p><code>df1</code></p>

```   one     two
0  dog  orange
1  dog   apple
2  dog   grape
3  ant  orange
4  ant   apple
5  ant   grape
6  cat  orange
7  cat   apple
8  cat   grape
```

<h2>Conditions</h2>

<p>You can not use <code>df1</code> as part of a solution, or <code>df</code>, the data is <code>df2</code>, and
it needs to be sorted into the ordering of <code>df1</code>.</p>

<h1>Attempt</h1>

<p>I have tried with <code>pd.Categorical</code>, but couldn't make something work.</p>

<pre class="lang-py prettyprint-override"><code>order_one = ["dog", "ant", "cat"]
order_two = ["orange", "apple", "grape"]

df2 = (
    df2.groupby(["two"])
    .apply(lambda a: a.iloc[pd.Categorical(a["one"], order_one).argsort()])
    .reset_index(drop=True)
)

df2 = (
    df2.groupby(["one"])
    .apply(lambda a: a.iloc[pd.Categorical(a["two"], order_two).argsort()])
    .reset_index(drop=True)
)
</code>```

Edit

The solution should be based purely on df2, df1 is just part of the test data and to demonstrate how df2 should be sorted. A solution that uses df1 is not viable as this is the result of sorting df2, I can't use that as part of a solution

答:
Let us try pd.Categorical

```<code>df2.one=pd.Categorical(df2.one,categories=df1.one.unique())
df2.two=pd.Categorical(df2.two,categories=df1.two.unique())
df2=df2.sort_values(['one','two'])
df2
   one     two
5  dog  orange
4  dog   apple
1  dog   grape
7  ant  orange
6  ant   apple
8  ant   grape
2  cat  orange
3  cat   apple
0  cat   grape
</code>```

Make it into function

```<code>def yourfunc(x,y):
...     for c in x.columns :
...         x[c]=pd.Categorical(x[c],categories=y[c].unique())
...     return x.sort_values(x.columns.tolist())
...
yourfunc(df1,df2)
   one     two
8  cat   grape
6  cat  orange
7  cat   apple
2  dog   grape
0  dog  orange
1  dog   apple
5  ant   grape
3  ant  orange
4  ant   apple
</code>```

Update

```<code>order_fruit = ["orange", "apple", "grape"]
order_animals = ["dog", "ant", "cat"]
def yourfunc(x,y):
...      for c, self in zip(x.columns,y) :
...          x[c]=pd.Categorical(x[c],categories=self)
...      return x.sort_values(x.columns.tolist())
...
yourfunc(df2,[order_animals,order_fruit])
   one     two
5  dog  orange
4  dog   apple
1  dog   grape
7  ant  orange
6  ant   apple
8  ant   grape
2  cat  orange
3  cat   apple
0  cat   grape
</code>```

Body:
Note

df2 is the only thing that can be used here - using df or df1 would be to use data that isn't possible. The data is received as df2, it wants to be manipulated to the form of df1. Neither df1 or df can be used as part of of the solution (as df1 is the solution).

Setup test data

this is just setup for this post.

# sample data
reps = {1: "dog", 2: "ant", 3: "cat", 6: "orange", 7: "apple", 8: "grape"}
df = pd.DataFrame(
    {"one": [1, 1, 1, 2, 2, 2, 3, 3, 3], "two": [6, 7, 8, 6, 7, 8, 6, 7, 8]}
)
df = df.replace(reps).copy()
df1 = df.copy()
df2 = df.sample(frac=1, random_state=1).replace(reps).reset_index(drop=True)

Problem

Sort df1 so that it is in the same order as df1.

df2:

   one     two
0  cat   grape
1  dog   grape
2  cat  orange
3  cat   apple
4  dog   apple
5  dog  orange
6  ant   apple
7  ant  orange
8  ant   grape

df1

   one     two
0  dog  orange
1  dog   apple
2  dog   grape
3  ant  orange
4  ant   apple
5  ant   grape
6  cat  orange
7  cat   apple
8  cat   grape

Conditions

You can not use df1 as part of a solution, or df, the data is df2, and
it needs to be sorted into the ordering of df1.

Attempt

I have tried with pd.Categorical, but couldn't make something work.

order_one = ["dog", "ant", "cat"]
order_two = ["orange", "apple", "grape"]

df2 = (
    df2.groupby(["two"])
    .apply(lambda a: a.iloc[pd.Categorical(a["one"], order_one).argsort()])
    .reset_index(drop=True)
)

df2 = (
    df2.groupby(["one"])
    .apply(lambda a: a.iloc[pd.Categorical(a["two"], order_two).argsort()])
    .reset_index(drop=True)
)

Edit

The solution should be based purely on df2, df1 is just part of the test data and to demonstrate how df2 should be sorted. A solution that uses df1 is not viable as this is the result of sorting df2, I can't use that as part of a solution

Body:
Let us try pd.Categorical

df2.one=pd.Categorical(df2.one,categories=df1.one.unique())
df2.two=pd.Categorical(df2.two,categories=df1.two.unique())
df2=df2.sort_values(['one','two'])
df2
   one     two
5  dog  orange
4  dog   apple
1  dog   grape
7  ant  orange
6  ant   apple
8  ant   grape
2  cat  orange
3  cat   apple
0  cat   grape

Make it into function

def yourfunc(x,y):
...     for c in x.columns :
...         x[c]=pd.Categorical(x[c],categories=y[c].unique())
...     return x.sort_values(x.columns.tolist())
...
yourfunc(df1,df2)
   one     two
8  cat   grape
6  cat  orange
7  cat   apple
2  dog   grape
0  dog  orange
1  dog   apple
5  ant   grape
3  ant  orange
4  ant   apple

Update

order_fruit = ["orange", "apple", "grape"]
order_animals = ["dog", "ant", "cat"]
def yourfunc(x,y):
...      for c, self in zip(x.columns,y) :
...          x[c]=pd.Categorical(x[c],categories=self)
...      return x.sort_values(x.columns.tolist())
...
yourfunc(df2,[order_animals,order_fruit])
   one     two
5  dog  orange
4  dog   apple
1  dog   grape
7  ant  orange
6  ant   apple
8  ant   grape
2  cat  orange
3  cat   apple
0  cat   grape


答:
You need to pass the index to pop not the value you want to remove
(list.pop docs)

If you really must use pop you can use numpy.argmin to get index of minimum value like this:

```<code>import numpy as np
new_values = []

#define how least value would be determined
while len(values) > 0 :
    new_values.append(min(values))
    values.pop(np.argmin(values))
</code>```

But if you are just sorting a list why not use sorted built-in function?

```<code>new_values = sorted(values)
</code>```

Title: Understanding list `pop index out of range`

Body:
I'm trying to arrange a list generated in ascending order.

Using remove in my while loop works. But if I use pop I keep getting an IndexError: Pop index out of range.

I also tried increasing the length of values list when the last iteration will be carried out; to while len(values) > 1 and I'm still getting the same error.

Can anyone please explain why I keep getting this error when using pop but not when using remove?

#import randrange
from random import randrange

#generate a list of values
values = []
for i in range(1, 10):
   values.append(randrange(1, 10))
print(values)

#initiate list for keeping minimum values
new_values = []

#define how least value would be determined
while len(values) > 0 :
   new_values.append(min(values))
   values.pop(min(values))

print(new_values)

Body:
You need to pass the index to pop not the value you want to remove
(list.pop docs)

If you really must use pop you can use numpy.argmin to get index of minimum value like this:

import numpy as np
new_values = []

#define how least value would be determined
while len(values) > 0 :
    new_values.append(min(values))
    values.pop(np.argmin(values))

But if you are just sorting a list why not use sorted built-in function?

new_values = sorted(values)