问:
What is the use of Docker Desktop?
As a technology, we understand the docker creates virtualization and there is a docker hub, where Docker images are stored.
To explain in simple words, what purpose does docker desktop serve?
Or why do I need to install Docker Desktop App to use Docker

If we need to use docker on linux server, do we need to install Docker Desktop there as well or there is some other way?

答:
Docker Deskop packages a managed VM for running containers with various developer focused features. Some of the highlights include:

A managed immutable VM for running containers that makes recovering from issues typically as easy as a restart.

Automatic upgrades to apply security fixes.

Mapping of networking connections from the VM back to the desktop, allowing developers to point to localhost instead of tracking down the current IP of their VM.

Syncing filesystems between the host and containers so developers can write their code on their desktop and do their testing in a container as if it was on the same host.

GUI for managing containers and debugging issues. There's still the CLI if you prefer.

Kubernetes deployed with a toggle switch. Alternatives to this include KinD , k3s, and MiniKube.

Windows containers are available on Docker for Windows.

Bundled and preconfigured tooling with the newest features. For example, scanning, docker compose (v2), Dev environments, multi-platform support, were all released early on Docker Desktop.

For me, if you're a Windows or Mac user that doesn't want to be a Linux administrator, and wants your development experience to appear as if it's all running on your laptop instead of ssh'ing files to the server, Docker Desktop fills a nice role.

Further reading:

https://docs.docker.com/desktop/

https://www.docker.com/blog/the-magic-behind-the-scenes-of-docker-desktop/

Title: What is the use of Docker Desktop?


来源: stackexchange

问:
use of webcodecs API in Angular project?
I am trying to use features from the new webcodecs API in Chrome within an angular project. Typescript doesn't seem to be aware of the types, so I installed dom-webcodecs.

https://www.npmjs.com/package/@types/dom-webcodecs

It still seems to be missing VideoDecoder when I build. Here is a stackblitz with a simple example. Am I missing a step to use this in my project? Is there another way to use this API in my project?

https://stackblitz.com/edit/angular-ivy-zzysyg?file=src%2Fapp%2Fapp.component.ts

答: While I'm using Nx+Angular, I imagine our solution would be similar. I didn't need "typeRoots": ["node_modules/@types"] in my tsconfig, rather had to add "types": ["dom-webcodecs"] to tsconfig.worker.json.

ContentLicense: CC BY-SA 4.0


问:
asyncpg: How to construct SET strings with parameters
What is the correct way to pass in parameters in a SET query?

this will return asyncpg.exceptions.PostgresSyntaxError: syntax error at or near "$1"

import asyncio

import asyncpg

async def main():
    conn = await asyncpg.connect(user="xx", password="yy", host="127.0.0.1", database="my_db")

    # works
    await conn.execute("select $1", "1")

    identity = "arn:aws:sts::123456:assumed-role/thing_1"

    # fails
    await conn.execute("set session iam.identity = $1", identity)

asyncio.run(main())

```

答:
asyncpg has a function for this:

await conn.execute("select set_config('iam.identity', $1, false)", identity)
```


问:
React-Bootstrap modal only take last item of map
I'm a beginner in coding. I'm creating my graphic designer portfolio. I've put all the portfolio content (thumbnail, client name, description...) into an array called "portfolio" in a JSON file. Each item the array is a different project.
I display a galery of thumbnails, and when I click on a thumbnail, a modal opens with the project details.

I map on my "portfolio" array to display the galery, that works. But when I open the modal, it always display the last item of my array.

import React from 'react';
import Modal from 'react-bootstrap/Modal';

class Portfolio extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      error: null,
      isLoaded: false,
      items: [],
      projectName: "",
      show: false
    };

    this.handleShow = () => {
      this.setState({ show: true });
    };

    this.handleClose = () => {
      this.setState({ show: false });
    };
  }

  componentDidMount() {
    fetch("https://api.myjson.com/bins/1cbaj5")
      .then(res => res.json())
      .then(
        (result) => {
          this.setState({
            isLoaded: true,
            items: result.portfolio
          });
        },
        (error) => {
          this.setState({
            isLoaded: true,
            error
          });
        }
      )
  }

  render() {
    const {error, isLoaded, items} = this.state;
    if (error) {
      return
Erreur : {error.message}
;
    } else if (!isLoaded) {
      return
Chargement…
;
    } else {
      return (

Portfolio

            {items.map(item => (

                  <button onClick={() => this.handleShow()}>

{item.client}

                  </button>

                  <Modal show={this.state.show} onHide={this.handleClose}>
                    <Modal.Header closeButton>

{item.client}

                    </Modal.Header>
                    <Modal.Body>

{item.description}

                    </Modal.Body>
                  </Modal>

            ))}

      );
    }
  }
}

export default Portfolio;
```

I would like the modal to display the corresponding project details.
Thank you for your help.

答:
The previous answer is great. In functional components it is possible to create a function for handling both states and it works perfectly.

```<code>    export const TeamCollection = () => {
  const [modalShow, setModalShow] = useState(false);
  const [name, setName] = useState("");
  const handleName = (name) => {
    setName(name);
    setModalShow(true);
  };
  return (
    <Container>
      <Row>
        {team_data.map((item) => (
          <Col key={item.id}>
            <div style={{ width: "175px" }}>
              <ImageContainer>
                <LinkContainer onClick={() => handleName(item.name)}>
                  <BackgroundImage
                    style={{ backgroundImage: `url(${item.imageUrl})` }}
                  />
                </LinkContainer>
              </ImageContainer>
              <h4>{item.name}</h4>
              <p>{item.position}</p>
            </div>
          </Col>
        ))}
      </Row>
      <RouteModal
        data={name}
        show={modalShow}
        onHide={() => setModalShow(false)}
      />
    </Container>
  );
};

export default TeamCollection;
</code>```

Body:
I'm a beginner in coding. I'm creating my graphic designer portfolio. I've put all the portfolio content (thumbnail, client name, description...) into an array called "portfolio" in a JSON file. Each item the array is a different project.
I display a galery of thumbnails, and when I click on a thumbnail, a modal opens with the project details.

I map on my "portfolio" array to display the galery, that works. But when I open the modal, it always display the last item of my array.

import React from 'react';
import Modal from 'react-bootstrap/Modal';

class Portfolio extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      error: null,
      isLoaded: false,
      items: [],
      projectName: "",
      show: false
    };

    this.handleShow = () => {
      this.setState({ show: true });
    };

    this.handleClose = () => {
      this.setState({ show: false });
    };
  }

  componentDidMount() {
    fetch("https://api.myjson.com/bins/1cbaj5")
      .then(res => res.json())
      .then(
        (result) => {
          this.setState({
            isLoaded: true,
            items: result.portfolio
          });
        },
        (error) => {
          this.setState({
            isLoaded: true,
            error
          });
        }
      )
  }

  render() {
    const {error, isLoaded, items} = this.state;
    if (error) {
      return
Erreur : {error.message}
;
    } else if (!isLoaded) {
      return
Chargement…
;
    } else {
      return (

Portfolio

            {items.map(item => (

                  <button onClick={() => this.handleShow()}>

{item.client}

                  </button>

                  <Modal show={this.state.show} onHide={this.handleClose}>
                    <Modal.Header closeButton>

{item.client}

                    </Modal.Header>
                    <Modal.Body>

{item.description}

                    </Modal.Body>
                  </Modal>

            ))}

      );
    }
  }
}

export default Portfolio;

I would like the modal to display the corresponding project details.
Thank you for your help.

Body:
The previous answer is great. In functional components it is possible to create a function for handling both states and it works perfectly.

    export const TeamCollection = () => {
  const [modalShow, setModalShow] = useState(false);
  const [name, setName] = useState("");
  const handleName = (name) => {
    setName(name);
    setModalShow(true);
  };
  return (
    <Container>
      <Row>
        {team_data.map((item) => (
          <Col key={item.id}>
            <div style={{ width: "175px" }}>
              <ImageContainer>
                <LinkContainer onClick={() => handleName(item.name)}>
                  <BackgroundImage
                    style={{ backgroundImage: `url(${item.imageUrl})` }}
                  />
                </LinkContainer>
              </ImageContainer>
              <h4>{item.name}</h4>
              <p>{item.position}</p>
            </div>
          </Col>
        ))}
      </Row>
      <RouteModal
        data={name}
        show={modalShow}
        onHide={() => setModalShow(false)}
      />
    </Container>
  );
};

export default TeamCollection;


问:
problem in creating a different thread for socket in kivy framework
I'm using kivy framework and socket.in the call back back function this code conn, addr=s.accept() makes problems for kivy and stops its work.that's mean when i'm clicking start server button kivy gui is not responding.I tried to create a thread and i  used : Thread(target=s.listen()).start()
but is this thread work different from kivy?
what should i do to kivy be responsive with socket?

code:

import kivy
from threading import Thread
import socket
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.clock import Clock

s=socket.socket()
s.bind(('localhost',1234))
Thread(target=s.listen()).start()
print('listen')

class me (App):
    def __init__(self):
        super().__init__()

        self.b=Button(text='start server',on_press=self.callback)
        self.g=GridLayout(cols=4)
        self.l=Label(text='listening...')
        self.t=TextInput(hint_text='Type here your message: ' )
        self.g.add_widget(self.b)
        self.g.add_widget(self.t)
        self.g.add_widget(self.l)
    def build(self):
            return self.g

    def callback(self,click):
        conn, addr=s.accept()

m=me()

m.run()
```

答:
Try this:</p>
```<code>from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.app import App

import socket
from threading import Thread

class Me(App):
    def __init__(self):
        super().__init__()

        self.b = Button(text='start server', on_press=self.callback)
        self.g = GridLayout(cols=4)
        self.l = Label(text='listening...')
        self.t = TextInput(hint_text='Type here your message: ')
        self.g.add_widget(self.b)
        self.g.add_widget(self.t)
        self.g.add_widget(self.l)

    def build(self):
        return self.g

    def stream(self):
        while True:
            conn, addr = self.sock.accept()
            print(addr)
            conn.send(self.t.text.encode())
            print(f"{self.t.text} sended!")
            conn.close()

    def callback(self, click):
        self.sock = socket.socket()
        self.sock.bind(('localhost', 1234))
        self.sock.listen(1000)
        Thread(target=self.stream).start()
        print("server started")

m = Me()
m.run()
</code>```

Body:
Try this:

from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.app import App

import socket
from threading import Thread

class Me(App):
    def __init__(self):
        super().__init__()

        self.b = Button(text='start server', on_press=self.callback)
        self.g = GridLayout(cols=4)
        self.l = Label(text='listening...')
        self.t = TextInput(hint_text='Type here your message: ')
        self.g.add_widget(self.b)
        self.g.add_widget(self.t)
        self.g.add_widget(self.l)

    def build(self):
        return self.g

    def stream(self):
        while True:
            conn, addr = self.sock.accept()
            print(addr)
            conn.send(self.t.text.encode())
            print(f"{self.t.text} sended!")
            conn.close()

    def callback(self, click):
        self.sock = socket.socket()
        self.sock.bind(('localhost', 1234))
        self.sock.listen(1000)
        Thread(target=self.stream).start()
        print("server started")

m = Me()
m.run()


问:
Modern Command-Line Programming a la Bloomberg Terminal
I want build a financial analysis tool that will feature screen after screen of diverse, dynamic options. The screens will change based on the input from the previous screens. Any single screen can have hundreds of options, most of which will be listed in lists. I think creating my application using a regular gui style will be difficult and slow.

I'd like to build my software in a command-line like environment. I think the closest thing to what I'm going for is a Bloomberg terminal.

Are there any tools, toolkits, environments, etc that I can use to build my app on a totally blank slate that I can constantly change? I need to be able to draw charts and would love as much flexibility in graphics as possible. The folks at Bloomberg somehow do this. Can anyone give me some tips?

I have considered using a browser-based approach but I would much prefer something more native.

If it matters I'd most like to code in Java and/or jRuby.

Thanks!

Please note that regardless of the merits of the approach (per your view), I have a valid technical question.

答:
SDL comes to mind.  Although developed for games, it gives you complete control over graphics output.  There's bindings for Java as well as Ruby.

Wikipedia has a good overview

ContentLicense: CC BY-SA 3.0


问:
Closest subsequent index for a specified value
Consider a vector:

int = c(1, 1, 0, 5, 2, 0, 0, 2)
```
<p>I'd like to get the closest subsequent index (not the difference) for a specified value. The first parameter of the function should be the vector, while the second should be the value one wants to see the closest subsequent elements.</p>
<p>For instance,</p>
<pre class="lang-r prettyprint-override"><code>f(int, 0)
# [1] 2 1 0 2 1 0 0 NA
</code>```

Here, the first element of the vector (1) is two positions away from the first subsequent 0, (3 - 1 = 2), so it should return 2. Then the second element is 1 position away from a 0 (2 - 1 = 1). When there is no subsequent values that match the specified value, return NA (here it's the case for the last element, because no subsequent value is 0).

Other examples:

f(int, 1)
# [1] 0 0 NA NA NA NA NA NA

f(int, 2)
# [1] 4 3 2 1 0 2 1 0

f(int, 3)
# [1] NA NA NA NA NA NA NA NA
```
<p>This should also work for character vectors:</p>
<pre class="lang-r prettyprint-override"><code>char = c("A", "B", "C", "A", "A")

f(char, "A")
# [1] 0 2 1 0 0
</code>```

答:
A single-pass for loop is simple and efficient:

```<code>f1 <- function(v, val) {
  out <- integer(length(v))
  if (v[length(v)] != val) out[length(v)] <- NA_integer_

  for (i in (length(v) - 1L):1) {
    if (v[i] == val) {
      out[i] <- 0L
    } else {
      out[i] <- out[i + 1L] + 1L
    }
  }

  return(out)
}

int <- c(1, 1, 0, 5, 2, 0, 0, 2)
chr <- c("A", "B", "C", "A", "A")
f1(int, 0)
#> [1]  2  1  0  2  1  0  0 NA
f1(chr, "A")
#> [1] 0 2 1 0 0
</code>```

Benchmarking against other solutions:

```<code>f2 <- function(v, x){
  sapply(seq_along(v), function(i){
    which(v[ i:length(v) ] == x)[ 1 ] - 1
  })
}

f3 <- function(lookup,val ) {
  ind <- which(lookup == val)[1] -1
  if (length(lookup) > 1) {
    c(ind, f3(lookup[-1], val))
  } else {
    ind
  }
}

f4 <- function(x, value) {
  idx = which(x == value)
  nearest = rep(NA, length(x))
  nearest[1:max(idx)] = rep(idx, diff(c(0, idx)))
  abs(seq_along(x) - nearest)
}

f5 <- function(vec, value) {
  replace(
    Reduce(
      function(x, y)
        x  + (y * x) ,
      vec != value,
      right = TRUE,
      accumulate = TRUE
    ),
    max(tail(which(vec == value), 1), 0) < seq_along(vec),
    NA
  )
}

microbenchmark::microbenchmark(f1 = {f1(int, 0); f1(chr, "A")},
                               f2 = {f2(int, 0); f2(chr, "A")},
                               f3 = {f3(int, 0); f3(chr, "A")},
                               f4 = {f4(int, 0); f4(chr, "A")},
                               f5 = {f5(int, 0); f5(chr, "A")},
                               check = "equal")
#> Unit: microseconds
#>  expr  min    lq   mean median    uq   max neval
#>    f1  6.0  7.50  8.990   8.40  9.60  18.3   100
#>    f2 54.2 61.45 71.752  65.55 79.40 131.8   100
#>    f3 25.5 28.60 33.393  30.75 35.90 105.2   100
#>    f4 22.3 26.30 30.599  28.00 32.65  82.4   100
#>    f5 59.7 64.55 73.474  69.10 75.70 157.0   100
</code>```

Body:
Consider a vector:

int = c(1, 1, 0, 5, 2, 0, 0, 2)

I'd like to get the closest subsequent index (not the difference) for a specified value. The first parameter of the function should be the vector, while the second should be the value one wants to see the closest subsequent elements.

For instance,

f(int, 0)
# [1] 2 1 0 2 1 0 0 NA

Here, the first element of the vector (1) is two positions away from the first subsequent 0, (3 - 1 = 2), so it should return 2. Then the second element is 1 position away from a 0 (2 - 1 = 1). When there is no subsequent values that match the specified value, return NA (here it's the case for the last element, because no subsequent value is 0).

Other examples:

f(int, 1)
# [1] 0 0 NA NA NA NA NA NA

f(int, 2)
# [1] 4 3 2 1 0 2 1 0

f(int, 3)
# [1] NA NA NA NA NA NA NA NA

This should also work for character vectors:

char = c("A", "B", "C", "A", "A")

f(char, "A")
# [1] 0 2 1 0 0

Body:
A single-pass for loop is simple and efficient:

f1 <- function(v, val) {
  out <- integer(length(v))
  if (v[length(v)] != val) out[length(v)] <- NA_integer_

  for (i in (length(v) - 1L):1) {
    if (v[i] == val) {
      out[i] <- 0L
    } else {
      out[i] <- out[i + 1L] + 1L
    }
  }

  return(out)
}

int <- c(1, 1, 0, 5, 2, 0, 0, 2)
chr <- c("A", "B", "C", "A", "A")
f1(int, 0)
#> [1]  2  1  0  2  1  0  0 NA
f1(chr, "A")
#> [1] 0 2 1 0 0

Benchmarking against other solutions:

f2 <- function(v, x){
  sapply(seq_along(v), function(i){
    which(v[ i:length(v) ] == x)[ 1 ] - 1
  })
}

f3 <- function(lookup,val ) {
  ind <- which(lookup == val)[1] -1
  if (length(lookup) > 1) {
    c(ind, f3(lookup[-1], val))
  } else {
    ind
  }
}

f4 <- function(x, value) {
  idx = which(x == value)
  nearest = rep(NA, length(x))
  nearest[1:max(idx)] = rep(idx, diff(c(0, idx)))
  abs(seq_along(x) - nearest)
}

f5 <- function(vec, value) {
  replace(
    Reduce(
      function(x, y)
        x  + (y * x) ,
      vec != value,
      right = TRUE,
      accumulate = TRUE
    ),
    max(tail(which(vec == value), 1), 0) < seq_along(vec),
    NA
  )
}

microbenchmark::microbenchmark(f1 = {f1(int, 0); f1(chr, "A")},
                               f2 = {f2(int, 0); f2(chr, "A")},
                               f3 = {f3(int, 0); f3(chr, "A")},
                               f4 = {f4(int, 0); f4(chr, "A")},
                               f5 = {f5(int, 0); f5(chr, "A")},
                               check = "equal")
#> Unit: microseconds
#>  expr  min    lq   mean median    uq   max neval
#>    f1  6.0  7.50  8.990   8.40  9.60  18.3   100
#>    f2 54.2 61.45 71.752  65.55 79.40 131.8   100
#>    f3 25.5 28.60 33.393  30.75 35.90 105.2   100
#>    f4 22.3 26.30 30.599  28.00 32.65  82.4   100
#>    f5 59.7 64.55 73.474  69.10 75.70 157.0   100


问:
pandas - How to unstack dict column values to 2 new columns with the same name to all?
I currently have the dataframe bellow, with a dict inside the column value.

```<code>     variable    value
0       b44     {55: 20}
1       a11     {56: 19}
5       a34     {33: 19}
</code>```

How to transform the above df to a df that looks like this:

```<code>      variable    id   value
0       b44       55     20
1       a11       56     19
5       a34       33     19
</code>```

答:
Use list comprehension for list of tuples, DataFrame.pop is for extract column value for new ordering of columns names:

```<code>df[['id','value']] = [list(x.items())[0] for x in df.pop('value')]
print (df)
  variable  id  value
0      b44  55     20
1      a11  56     19
5      a34  33     19
</code>```

Or:

```<code>df[['id','value']] = [(*x.keys(), *x.values()) for x in df.pop('value')]
print (df)
  variable  id  value
0      b44  55     20
1      a11  56     19
5      a34  33     19
</code>```

Body:
I currently have the dataframe bellow, with a dict inside the column value.

     variable    value
0       b44     {55: 20}
1       a11     {56: 19}
5       a34     {33: 19}

How to transform the above df to a df that looks like this:

      variable    id   value
0       b44       55     20
1       a11       56     19
5       a34       33     19

Body:
Use list comprehension for list of tuples, DataFrame.pop is for extract column value for new ordering of columns names:

df[['id','value']] = [list(x.items())[0] for x in df.pop('value')]
print (df)
  variable  id  value
0      b44  55     20
1      a11  56     19
5      a34  33     19

Or:

df[['id','value']] = [(*x.keys(), *x.values()) for x in df.pop('value')]
print (df)
  variable  id  value
0      b44  55     20
1      a11  56     19
5      a34  33     19


问:
WSO2 api manager 200 - Delegating authentication
Does anyone knows how to perform authentication in backend system, below Api Manager ?

Trying to implement own custom user store, but after place the builded jar in folder "repository/components/dropins", nothing vas loaded at product starting and in the carbon console interface, custom userstore doesn't appear...

Version of apim: WSO2 APIM 2.0.0

答:
WSO2 APIM 2.0.0 is an osgi framework. When you build an osgi bundle and place it on "repository/components/dropins" it will be visible from the osgi console.
Find the reason why your bundle is not starting using these steps:

Start the osgi console do "$ sh wso2server.sh -DosgiConsole"

Search for your bundle type 'osgi> ss bundle-name' and note the id

Try to start your bundle with 'osgi> start id' and the reason with it doesn't start will be displayed.

If the bundle is not displayed in the list, it may be that your jar is not a valid osgi or is created on a different osgi version. You could also try to install the bundle manually on the osgi console using 'osgi> install file:/path/to/your/bundle.jar'

More documentation can be found here https://wso2.com/library/articles/getting-started-wso2-carbon/

I hope that helps with your debugging.


问:
PandasNotImplementedError for converted pandas dataframe to Koalas dataframe
I am having a small issue which I am facing in my code logic.

I am converting a line of code which uses pandas dataframe to use Koalas dataframe and I get the following error during the code execution.

```<code># Error Message
PandasNotImplementedError: The method `pd.Index.__iter__()` is not implemented. If you want to collect your data as an NumPy array, use 'to_numpy()' instead.

</code>```
```<code>#Sample dataframe

# initialize data of lists.
data = {'team_code':['A1', 'S1'],
        'TeamName':['JohnTeam', 'SusanTeam']}

# Create DataFrame
input_df= pd.DataFrame(data)

</code>```

The original line of code using the pandas dataframe is shown below:

```<code># input_df is a pandas df here

to_remove_df = input_df.drop_duplicates(['team_code', 'TeamName'])

dropped_df = input_df[~input_df.index.isin(to_remove_df.index)].copy().reset_index(drop=True)

</code>```

I converted the code above using a dataframe as shown below.

The original line of code using the pandas dataframe is shown below. The only difference is that input_df is now a koalas dataframe:

```<code># input_df is now a koalas df here

to_remove_df = input_df.drop_duplicates(['team_code', 'TeamName'])

dropped_df = input_df[~input_df.index.isin(to_remove_df.index)].copy().reset_index(drop=True)

</code>```

EDIT

Stacktrace on a databricks cluster with Spark 3.2.0 and koalas==1.8.2

```<code>---------------------------------------------------------------------------
PandasNotImplementedError                 Traceback (most recent call last)
<command-2399235872097642> in <module>
      2 to_remove_df = input_df.drop_duplicates(['team_code', 'TeamName'])
      3 print(to_remove_df)
----> 4 dropped_df = input_df[~input_df.index.isin(to_remove_df.index)].copy().reset_index(drop=True)
      5 dropped_df

/databricks/python/lib/python3.8/site-packages/databricks/koalas/usage_logging/__init__.py in wrapper(*args, **kwargs)
    193             start = time.perf_counter()
    194             try:
--> 195                 res = func(*args, **kwargs)
    196                 logger.log_success(
    197                     class_name, function_name, time.perf_counter() - start, signature

/databricks/python/lib/python3.8/site-packages/databricks/koalas/usage_logging/__init__.py in wrapper(*args, **kwargs)
    188         if hasattr(_local, "logging") and _local.logging:
    189             # no need to log since this should be internal call.
--> 190             return func(*args, **kwargs)
    191         _local.logging = True
    192         try:

/databricks/python/lib/python3.8/site-packages/databricks/koalas/base.py in isin(self, values)
   1234             )
   1235
-> 1236         values = values.tolist() if isinstance(values, np.ndarray) else list(values)
   1237         return self._with_new_scol(self.spark.column.isin(values))
   1238

/databricks/python/lib/python3.8/site-packages/databricks/koalas/indexes/base.py in __iter__(self)
   2479
   2480     def __iter__(self):
-> 2481         return MissingPandasLikeIndex.__iter__(self)
   2482
   2483     def __xor__(self, other):

/databricks/python/lib/python3.8/site-packages/databricks/koalas/usage_logging/__init__.py in wrapper(*args, **kwargs)
    248     def wrapper(*args, **kwargs):
    249         try:
--> 250             return func(*args, **kwargs)
    251         finally:
    252             logger.log_missing(class_name, function_name, is_deprecated, signature)

/databricks/python/lib/python3.8/site-packages/databricks/koalas/missing/__init__.py in unsupported_function(*args, **kwargs)
     20 def unsupported_function(class_name, method_name, deprecated=False, reason=""):
     21     def unsupported_function(*args, **kwargs):
---> 22         raise PandasNotImplementedError(
     23             class_name=class_name, method_name=method_name, reason=reason
     24         )

PandasNotImplementedError: The method `pd.Index.__iter__()` is not implemented. If you want to collect your data as an NumPy array, use 'to_numpy()' instead.
</code>```

I am not able to figure out what I missed in the code above when using the input_df as a koalas dataframe. May someone please help me resolve it?

Thank you to all!

答:
Looks like your filtering method is using __iter__() behind the scenes, which is currently not supported in Koalas.

I suggest an alternative approach in which you define a custom function and pass your dataframe to it. This way, you should obtain the same results as with pandas code. A detailed explanation of the function is written line by line.

```<code>def my_func(df):

  # be sure to create a column with unique identifiers
  df = df.reset_index(drop=True).reset_index()

  # create dataframe to be removed
  # the additional dummy column is needed to correctly filter out rows later on
  to_remove_df = df.drop_duplicates(['team_code', 'TeamName'])[['index']]
  to_remove_df = to_remove_df.assign(check = lambda x: 'remove_me')

  # merge the two dataframes and remove rows
  merged_df = df.merge(to_remove_df, on='index', how='outer')
  result = merged_df.loc[merged_df['check'].isna()]

  # drop unnecessary columns
  result = result.drop(columns=['index', 'check'])

  return result
</code>```

Example 1

```<code># your data
data = {'team_code':['A1', 'S1'],
        'TeamName':['JohnTeam', 'SusanTeam']}
input_df = ks.DataFrame(data)

df = my_func(input_df)
print(df)
# Empty DataFrame
# Columns: [team_code, TeamName]
# Index: []
</code>```

Example 2

```<code># other sample data
data = {'team_code':['A1', 'S1', 'A1', 'S1'],
        'TeamName':['JohnTeam', 'SusanTeam', 'RickTeam', 'SusanTeam']}
input_df = ks.DataFrame(data)

df = my_func(input_df)
print(df)
#   team_code   TeamName
# 3        S1  SusanTeam
</code>```

Body:
I am having a small issue which I am facing in my code logic.

I am converting a line of code which uses pandas dataframe to use Koalas dataframe and I get the following error during the code execution.

# Error Message
PandasNotImplementedError: The method `pd.Index.__iter__()` is not implemented. If you want to collect your data as an NumPy array, use 'to_numpy()' instead.

#Sample dataframe

# initialize data of lists.
data = {'team_code':['A1', 'S1'],
        'TeamName':['JohnTeam', 'SusanTeam']}

# Create DataFrame
input_df= pd.DataFrame(data)

The original line of code using the pandas dataframe is shown below:

# input_df is a pandas df here

to_remove_df = input_df.drop_duplicates(['team_code', 'TeamName'])

dropped_df = input_df[~input_df.index.isin(to_remove_df.index)].copy().reset_index(drop=True)

I converted the code above using a dataframe as shown below.

The original line of code using the pandas dataframe is shown below. The only difference is that input_df is now a koalas dataframe:

# input_df is now a koalas df here

to_remove_df = input_df.drop_duplicates(['team_code', 'TeamName'])

dropped_df = input_df[~input_df.index.isin(to_remove_df.index)].copy().reset_index(drop=True)

EDIT

Stacktrace on a databricks cluster with Spark 3.2.0 and koalas==1.8.2

---------------------------------------------------------------------------
PandasNotImplementedError                 Traceback (most recent call last)
<command-2399235872097642> in <module>
      2 to_remove_df = input_df.drop_duplicates(['team_code', 'TeamName'])
      3 print(to_remove_df)
----> 4 dropped_df = input_df[~input_df.index.isin(to_remove_df.index)].copy().reset_index(drop=True)
      5 dropped_df

/databricks/python/lib/python3.8/site-packages/databricks/koalas/usage_logging/__init__.py in wrapper(*args, **kwargs)
    193             start = time.perf_counter()
    194             try:
--> 195                 res = func(*args, **kwargs)
    196                 logger.log_success(
    197                     class_name, function_name, time.perf_counter() - start, signature

/databricks/python/lib/python3.8/site-packages/databricks/koalas/usage_logging/__init__.py in wrapper(*args, **kwargs)
    188         if hasattr(_local, "logging") and _local.logging:
    189             # no need to log since this should be internal call.
--> 190             return func(*args, **kwargs)
    191         _local.logging = True
    192         try:

/databricks/python/lib/python3.8/site-packages/databricks/koalas/base.py in isin(self, values)
   1234             )
   1235
-> 1236         values = values.tolist() if isinstance(values, np.ndarray) else list(values)
   1237         return self._with_new_scol(self.spark.column.isin(values))
   1238

/databricks/python/lib/python3.8/site-packages/databricks/koalas/indexes/base.py in __iter__(self)
   2479
   2480     def __iter__(self):
-> 2481         return MissingPandasLikeIndex.__iter__(self)
   2482
   2483     def __xor__(self, other):

/databricks/python/lib/python3.8/site-packages/databricks/koalas/usage_logging/__init__.py in wrapper(*args, **kwargs)
    248     def wrapper(*args, **kwargs):
    249         try:
--> 250             return func(*args, **kwargs)
    251         finally:
    252             logger.log_missing(class_name, function_name, is_deprecated, signature)

/databricks/python/lib/python3.8/site-packages/databricks/koalas/missing/__init__.py in unsupported_function(*args, **kwargs)
     20 def unsupported_function(class_name, method_name, deprecated=False, reason=""):
     21     def unsupported_function(*args, **kwargs):
---> 22         raise PandasNotImplementedError(
     23             class_name=class_name, method_name=method_name, reason=reason
     24         )

PandasNotImplementedError: The method `pd.Index.__iter__()` is not implemented. If you want to collect your data as an NumPy array, use 'to_numpy()' instead.

I am not able to figure out what I missed in the code above when using the input_df as a koalas dataframe. May someone please help me resolve it?

Thank you to all!

Body:
Looks like your filtering method is using __iter__() behind the scenes, which is currently not supported in Koalas.

I suggest an alternative approach in which you define a custom function and pass your dataframe to it. This way, you should obtain the same results as with pandas code. A detailed explanation of the function is written line by line.

def my_func(df):

  # be sure to create a column with unique identifiers
  df = df.reset_index(drop=True).reset_index()

  # create dataframe to be removed
  # the additional dummy column is needed to correctly filter out rows later on
  to_remove_df = df.drop_duplicates(['team_code', 'TeamName'])[['index']]
  to_remove_df = to_remove_df.assign(check = lambda x: 'remove_me')

  # merge the two dataframes and remove rows
  merged_df = df.merge(to_remove_df, on='index', how='outer')
  result = merged_df.loc[merged_df['check'].isna()]

  # drop unnecessary columns
  result = result.drop(columns=['index', 'check'])

  return result

Example 1

# your data
data = {'team_code':['A1', 'S1'],
        'TeamName':['JohnTeam', 'SusanTeam']}
input_df = ks.DataFrame(data)

df = my_func(input_df)
print(df)
# Empty DataFrame
# Columns: [team_code, TeamName]
# Index: []

Example 2

# other sample data
data = {'team_code':['A1', 'S1', 'A1', 'S1'],
        'TeamName':['JohnTeam', 'SusanTeam', 'RickTeam', 'SusanTeam']}
input_df = ks.DataFrame(data)

df = my_func(input_df)
print(df)
#   team_code   TeamName
# 3        S1  SusanTeam


问:
Dictionary values to Dataframe?
I have a dictionary, where the key is a unique integer and the value is a dataframe row.

I want to take all the values (rows) from my dictionary and turn it into an actual dataframe. The structure of one row looks like this.

```<code>{1: {'imuid': '1192',
     'usid': 63,
     'usme': 'de'},
 2: {etc etc}
}
</code>```

I can't quite work out which pandas function to use. I've tried from_records but that produces the error TypeError: object of type 'builtin_function_or_method' has no len().

I don't care about the iteration ID, just need the values in the dictionary to be a row. So the dataframe will have multiple rows where the columns are imuid, usid and usme.

Using pandas and python

答:
pd.DataFrame.from_dict ?

```<code>df = pd.DataFrame.from_dict(<your_dict>, orient='index')
</code>```

Example:

```<code>d = {1: {'imuid': '1192',
     'usid': 63,
     'usme': 'de'}}

# convert dict to dataframe
df = pd.DataFrame.from_dict(d, orient='index')
</code>```

Output:

```<code>>       imuid   usid usme
>     1 1192    63  de
</code>```

Title: Dictionary values to Dataframe?

Body:
I have a dictionary, where the key is a unique integer and the value is a dataframe row.

I want to take all the values (rows) from my dictionary and turn it into an actual dataframe. The structure of one row looks like this.

{1: {'imuid': '1192',
     'usid': 63,
     'usme': 'de'},
 2: {etc etc}
}

I can't quite work out which pandas function to use. I've tried from_records but that produces the error TypeError: object of type 'builtin_function_or_method' has no len().

I don't care about the iteration ID, just need the values in the dictionary to be a row. So the dataframe will have multiple rows where the columns are imuid, usid and usme.

Using pandas and python

Body:
pd.DataFrame.from_dict ?

df = pd.DataFrame.from_dict(<your_dict>, orient='index')

Example:

d = {1: {'imuid': '1192',
     'usid': 63,
     'usme': 'de'}}

# convert dict to dataframe
df = pd.DataFrame.from_dict(d, orient='index')

Output:

>       imuid   usid usme
>     1 1192    63  de


问:
SQLite Exception: no such table Error

Possible Duplicate:

  Android Sqlite - “No Such Table” Error

We are trying to develop an application on Android. We are using SQLite database and on phone we are getting

SQLiteException:no such table.

It is working fine on simulator.

Can anyone provide any input on this?

答:
I had faced a different flavour of the same problem.

I was getting no such table error when I try to insert.

Before inserting, the code was calling

```<code>mDb = mDbHelper.getWritableDatabase();
</code>```

getWritableDatabase() , when called first time will call onCreate()

I had my SQL query to create the table within this oncreate method

```<code>public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
            Log.v("INFO1","creating db");
            //Toast.makeText(mCtx, "created", Toast.LENGTH_SHORT).show();
        }
</code>```

So for me what had happened was, the db was successfully created when the application was first run but no table due to some other errors.
Later whenever the application is run, onCreate() is never called as db is already there and thus no table created, so all further SQL commands failed.

So I moved creating table out of onCreate() , and now its working

Title: SQLite Exception: no such table Error

Body:
I had faced a different flavour of the same problem.

I was getting no such table error when I try to insert.

Before inserting, the code was calling

mDb = mDbHelper.getWritableDatabase();

getWritableDatabase() , when called first time will call onCreate()

I had my SQL query to create the table within this oncreate method

public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
            Log.v("INFO1","creating db");
            //Toast.makeText(mCtx, "created", Toast.LENGTH_SHORT).show();
        }

So for me what had happened was, the db was successfully created when the application was first run but no table due to some other errors.
Later whenever the application is run, onCreate() is never called as db is already there and thus no table created, so all further SQL commands failed.

So I moved creating table out of onCreate() , and now its working


问:
OpenBSD fails to execute a.out
I wrote this program that should just exit with exitcode 44:

```<code>// prog.S
#include <sys/syscall.h>
        .text
        .globl _start
_start:
        subl $8, %esp
        pushl $44
        pushl $0
        movl $SYS_exit, %eax
        int $0x80
</code>```

I compiled with

```<code>$ cc prog.S -nostdlib -o a.out
</code>```

and run

```<code>$./a.out
</code>```

Doing so on FreeBSD 13.0-RELEASE i386 (clang 11.0.1) worked fine. In fact, the executable runs and the exit code of the program is 44 as it should be.

However, doing the same on OpenBSD 7.0 GENERIC.MP#5 i386 (clang version 11.1.0) and on NetBSD 9.2 i386 (gcc 7.5.0), the kernel refused to execute the code and it was passed to the shell, which of course failed:

```<code>openbsd$ ./a.out
./a.out[1]: syntax error: `(' unexpected
</code>```

The strange thing is also that file says it's an ELF binary and therefore should be normally executed by the kernel

```<code>openbsd$ file a.out

a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
</code>```

Even objdump prints what is expected to print

```<code>openbsd$ objdump -d a.out

a.out:     file format elf32-i386

Disassembly of section .text:

00001184 <_start>:
    1184:   83 ec 08                sub    $0x8,%esp
    1187:   6a 2c                   push   $0x2c
    1189:   b8 01 00 00 00          mov    $0x1,%eax
    118e:   6a 00                   push   $0x0
    1190:   cd 80                   int    $0x80

</code>```

Any idea about what I'm doing wrong?

PS: Changing _start with main and compiling without -nostdlib works fine both on OpenBSD and NetBSD

答:
I found out that OpenBSD checks for .section ".note.openbsd.ident", "a". If it's not present it doesn't execute the file. If I substituted _start with main and linked without -nostdlib, .note.openbsd.ident can be found in libc. Similarly for NetBSD.

To learn more visit https://www.exploit-db.com/papers/13219

Title: OpenBSD fails to execute a.out

Body:
I wrote this program that should just exit with exitcode 44:

// prog.S
#include <sys/syscall.h>
        .text
        .globl _start
_start:
        subl $8, %esp
        pushl $44
        pushl $0
        movl $SYS_exit, %eax
        int $0x80

I compiled with

$ cc prog.S -nostdlib -o a.out

and run

$./a.out

Doing so on FreeBSD 13.0-RELEASE i386 (clang 11.0.1) worked fine. In fact, the executable runs and the exit code of the program is 44 as it should be.

However, doing the same on OpenBSD 7.0 GENERIC.MP#5 i386 (clang version 11.1.0) and on NetBSD 9.2 i386 (gcc 7.5.0), the kernel refused to execute the code and it was passed to the shell, which of course failed:

openbsd$ ./a.out
./a.out[1]: syntax error: `(' unexpected

The strange thing is also that file says it's an ELF binary and therefore should be normally executed by the kernel

openbsd$ file a.out

a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped

Even objdump prints what is expected to print

openbsd$ objdump -d a.out

a.out:     file format elf32-i386

Disassembly of section .text:

00001184 <_start>:
    1184:   83 ec 08                sub    $0x8,%esp
    1187:   6a 2c                   push   $0x2c
    1189:   b8 01 00 00 00          mov    $0x1,%eax
    118e:   6a 00                   push   $0x0
    1190:   cd 80                   int    $0x80

Any idea about what I'm doing wrong?

PS: Changing _start with main and compiling without -nostdlib works fine both on OpenBSD and NetBSD


问:
How to view the rendered (raw) query from a MySQLi prepared statement?
I'm building a logging class for my PHP/MySQLi app.  I can obviously log the raw prepared statement (with question marks), but without manually parsing it, is there anyway to see the actual raw query executed in MySQL?

Thanks!

答:
There are many ways to see the 'raw' queries, port sniffing using tcpdump, mysql-proxy, or watch 'mysqladmin -ppasswd "processlist"' though for a short time you can do this:

```<code>Start:
echo "set global general_log_file=mysqllogfile;set global general_log=1;" | mysql -u root -ppasswd

From now on all queries will appear in /var/lib/mysql/mysqllogfile

Stop:
echo "set global general_log=0;" | mysql -u root -ppasswd
</code>```

Don't forget to disable when finished!

You can log the queries to the table mysql.general_log:

```<code>echo "set global log_output=TABLE;" | mysql -u root -ppasswd
# restart logging
echo "set global general_log=0;" | mysql -u root -ppasswd
echo "set global general_log=1;" | mysql -u root -ppasswd
</code>```

Note that viewing the general_log table also causes them to be updated!

Body:
There are many ways to see the 'raw' queries, port sniffing using tcpdump, mysql-proxy, or watch 'mysqladmin -ppasswd "processlist"' though for a short time you can do this:

Start:
echo "set global general_log_file=mysqllogfile;set global general_log=1;" | mysql -u root -ppasswd

From now on all queries will appear in /var/lib/mysql/mysqllogfile

Stop:
echo "set global general_log=0;" | mysql -u root -ppasswd

Don't forget to disable when finished!

You can log the queries to the table mysql.general_log:

echo "set global log_output=TABLE;" | mysql -u root -ppasswd
# restart logging
echo "set global general_log=0;" | mysql -u root -ppasswd
echo "set global general_log=1;" | mysql -u root -ppasswd

Note that viewing the general_log table also causes them to be updated!


问:
Google MLKIt TextRecognition on iOS return always empty result
I am using react-native, react-native-vision-camera (with frameProcessor) and mlkit to perform in device text recognition.

My code works on Android, but I got always empty results on iOS.

Here is my Objective C implementation:

```<code>static inline id scanOCR(Frame* frame, NSArray* args) {
  MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];
  MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:frame.buffer];
  image.orientation = frame.orientation;

  NSError *error;
  MLKText *result = [textRecognizer resultsInImage:image error:&error];
  if (error != nil || result == nil) {
    NSLog(@"%@", error); // <- This is NEVER called
  } else {
    NSLog(@"text: %@", result.text); // <- this is always empty
    NSLog(@"blocks lenght: %lu", (unsigned long)result.blocks.count); // <- this is always 0
  }
  return @{@"text": result.text};
}
</code>```

The project corretly compile, with no errors, but results.text is always empty, even if the camera is framing texts.

I think that MLKit is corretly installed. This is my Podfile

```<code>require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '11.0'

target 'com.digitalbore.papertag' do
  config = use_native_modules!

  use_react_native!(
    :path => config[:reactNativePath],
    # to enable hermes on iOS, change `false` to `true` and then install pods
    :hermes_enabled => false
  )

  pod 'GoogleMLKit/TextRecognition','2.2.0'

  target 'com.digitalbore.papertagTests' do
    inherit! :complete
    # Pods for testing
    pod 'react-native-splash-screen', :path => '../node_modules/react-native-splash-screen'

    pod 'react-native-receive-sharing-intent', :path => '../node_modules/react-native-receive-sharing-intent'

  end

  # Enables Flipper.
  #
  # Note that if you have use_frameworks! enabled, Flipper will not work and
  # you should disable the next line.
  use_flipper!()

  post_install do |installer|
    react_native_post_install(installer)
    __apply_Xcode_12_5_M1_post_install_workaround(installer)
  end
end
</code>```

答: Solved by updating iPhone iOS version to last version.</p>

Body:
I am using react-native, react-native-vision-camera (with frameProcessor) and mlkit to perform in device text recognition.

My code works on Android, but I got always empty results on iOS.

Here is my Objective C implementation:

static inline id scanOCR(Frame* frame, NSArray* args) {
  MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];
  MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:frame.buffer];
  image.orientation = frame.orientation;

  NSError *error;
  MLKText *result = [textRecognizer resultsInImage:image error:&error];
  if (error != nil || result == nil) {
    NSLog(@"%@", error); // <- This is NEVER called
  } else {
    NSLog(@"text: %@", result.text); // <- this is always empty
    NSLog(@"blocks lenght: %lu", (unsigned long)result.blocks.count); // <- this is always 0
  }
  return @{@"text": result.text};
}

The project corretly compile, with no errors, but results.text is always empty, even if the camera is framing texts.

I think that MLKit is corretly installed. This is my Podfile

require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '11.0'

target 'com.digitalbore.papertag' do
  config = use_native_modules!

  use_react_native!(
    :path => config[:reactNativePath],
    # to enable hermes on iOS, change `false` to `true` and then install pods
    :hermes_enabled => false
  )

  pod 'GoogleMLKit/TextRecognition','2.2.0'

  target 'com.digitalbore.papertagTests' do
    inherit! :complete
    # Pods for testing
    pod 'react-native-splash-screen', :path => '../node_modules/react-native-splash-screen'

    pod 'react-native-receive-sharing-intent', :path => '../node_modules/react-native-receive-sharing-intent'

  end

  # Enables Flipper.
  #
  # Note that if you have use_frameworks! enabled, Flipper will not work and
  # you should disable the next line.
  use_flipper!()

  post_install do |installer|
    react_native_post_install(installer)
    __apply_Xcode_12_5_M1_post_install_workaround(installer)
  end
end


问:
Connecting blocks in Simulink
The "A" port of a three level bridge needs to be connected to Gain block as shown here:

However, a red line pop-ups and the connections are not possible.

Can somebody please let me know how do we make this connection ?

答:
You have two issues

The bridge block is from the Simscape Electrical components library. Simscape ("physical system") blocks and Simulink (mathematical / numerical) blocks are not directly compatible, see this documentation: Connecting Simscape Diagrams to Simulink....

This is indicated by the different symbols on the connector lines and the connection points of the blocks

You are trying to connect an input port of the Bridge to the input port of the Gain. Work out which way you actually want this signal to go and connect one end of your signal to an output of the Gain or the Bridge. You may have confused things here by flipping the bridge upside-down from its default orientation, so the inputs are now on the right.

An example for a three-level PWM converter (which contains conversions from Simulink to PS and use of the three-way bridge block) is shipped with MATLAB, you can run

```<code>uiopen('power_3levelVSC',1);
</code>```

To view the model. The corresponding documentation is here: https://uk.mathworks.com/help/physmod/sps/ug/ac-dc-three-level-pwm-converter.html

Title: Connecting blocks in Simulink

Body:
You have two issues

The bridge block is from the Simscape Electrical components library. Simscape ("physical system") blocks and Simulink (mathematical / numerical) blocks are not directly compatible, see this documentation: Connecting Simscape Diagrams to Simulink....

This is indicated by the different symbols on the connector lines and the connection points of the blocks

You are trying to connect an input port of the Bridge to the input port of the Gain. Work out which way you actually want this signal to go and connect one end of your signal to an output of the Gain or the Bridge. You may have confused things here by flipping the bridge upside-down from its default orientation, so the inputs are now on the right.

An example for a three-level PWM converter (which contains conversions from Simulink to PS and use of the three-way bridge block) is shipped with MATLAB, you can run

uiopen('power_3levelVSC',1);

To view the model. The corresponding documentation is here: https://uk.mathworks.com/help/physmod/sps/ug/ac-dc-three-level-pwm-converter.html


问:
ViewModel datas don't update
I have an UpdateFragment which display cards group by tags using a RecyclerView and ViewModel. In UpdateFragment I can also modify the name of the tags but I don't know why the change isn't detected by UpdateFragment until I recreate the fragment so UI don't update. The strange thing is that the MainActivity instead detect the update of the list and the code is equal.

UpdateFragment.class

```<code>public class UpdateFragment extends BaseCardFragment {

// TODO show total cards number

private static final String TAG = "UpdateFragment";

private RecyclerView recyclerView;

public UpdateFragment(List<CardWithTags> cardList, List<Tag> tagList) {
    super(cardList, tagList);
}

@Override
public void updateUI(List<CardWithTags> cardListWithTags, List<Tag> tagList) {

    RecyclerViewAdapterUpdate adapter = (RecyclerViewAdapterUpdate) recyclerView.getAdapter();
    Log.d(TAG, "Adapter:" + adapter);
    Log.d(TAG, "Cards:" + cardListWithTags);
    Log.d(TAG, "Tags:" + tagList);
    if (adapter != null && cardListWithTags != null && tagList != null) {

        adapter.setCardList(cardListWithTags);
        adapter.setTagList(tagList);
        adapter.notifyDataSetChanged();
        Log.d(TAG, ">>Update, Total cards: " + adapter.getItemCount());
        Log.d(TAG, ">>Update, Total tags: " + tagList.size());

    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_update, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    setViewModel();

    setRecyclerView();
}

private void setViewModel() {

    Log.d(TAG, ">>SetViewModel()");

    viewModel = new ViewModelProvider(this).get(ViewModelMainActivity.class);
    viewModel.getAllCards().observe(getActivity(), cardListLoaded -> {
        cardList = cardListLoaded;
        Log.d(TAG, ">>CardList updated:" + cardList);
        updateUI(cardList, tagList);
    });

    viewModel.getAllTags().observe(getActivity(), tagListLoaded -> {
        tagList = tagListLoaded;
        Log.d(TAG, ">>TagList updated:" + tagList);
        updateUI(cardList, tagList);
    });

}

private void setRecyclerView() {

    Log.d(TAG, ">>SetRecyclerView()");
    recyclerView = getView().findViewById(R.id.recycler_view_list);

    RecyclerViewAdapterUpdate recyclerViewAdapter = new RecyclerViewAdapterUpdate(cardList, tagList,
                                                            getContext(), getLayoutInflater(),
                                                            viewModel);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(),
                                                        RecyclerView.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);

    recyclerView.setAdapter(recyclerViewAdapter);

}
}
</code>```

ViewModelMainActivity.class

```<code>public class ViewModelMainActivity extends AndroidViewModel {

    private static final String TAG = "ViewModelMainActivity";
    private static boolean cardListIsUpdatedWithDb = false;
    private static boolean tagListIsUpdatedWithDb = false;

    private LiveData<List<CardWithTags>> cardList = new MutableLiveData<>();
    private LiveData<List<Tag>> tagList = new MutableLiveData<>();
    private final CardDAO cardDAO;
    private final ExecutorService executor;

    public ViewModelMainActivity(@NonNull Application application) {
        super(application);

        this.cardDAO = DatabaseTaboom.getDatabase(application).cardDao();
        this.executor = Executors.newSingleThreadExecutor();
    }

    public LiveData<List<CardWithTags>> getAllCards() {

        Log.d(TAG, ">>GetAllCards()");

        if (!cardListIsUpdatedWithDb) {
            cardList = cardDAO.getAllCards();
            cardListIsUpdatedWithDb = true;
        }

        return cardList;

    }

    public LiveData<List<Tag>> getAllTags() {

        Log.d(TAG, ">>GetAllTags()");

        if (!tagListIsUpdatedWithDb) {
            tagList = cardDAO.getAllTags();
            tagListIsUpdatedWithDb = true;
        }

        return tagList;

    }

    public void insertCard(CardWithTags card) {

        Log.d(TAG, ">>InsertCard(): " + card);

        executor.execute(() -> {

            long idCard = cardDAO.insertCard(card.getCard());

            if (idCard >= 1) {
                cardListIsUpdatedWithDb = false;
                Log.d(TAG, ">>Inserted Card: " + card.getCard().getTitle());
            }

            for (Tag t: card.getTagList()) {
                long idTag = cardDAO.insertTag(t);

                CardTagCrossRef cardTagCrossRef = new CardTagCrossRef();
                cardTagCrossRef.idCard = idCard;

                if (idTag < 1) {
                    // If Tag already exist, retrieve that to get id to try insert cwt
                    idTag = cardDAO.getTag(t.getTag()).getIdTag();
                } else {
                    // New Tag
                    tagListIsUpdatedWithDb = false;
                    Log.d(TAG, ">>Inserted Tag: " + t.getTag());
                }

                cardTagCrossRef.idTag = idTag;
                long idCWT = cardDAO.insertCardWithTags(cardTagCrossRef);
                if (idCWT >= 1) {
                    Log.d(TAG, ">>Inserted CWT: [" + idCard + "," + idTag + "]");
                    // Tag linked to a card
                    cardListIsUpdatedWithDb = false;
                }
            }

        });

    }

    public void shuffle(List<CardWithTags> list) {
        if (list != null) {
            Collections.shuffle(list);
            cardList = new MutableLiveData<>(list);
            Log.d(TAG, ">>List shuffled: " + list);
        } else {
            Log.d(TAG, ">>List is null");
        }
    }

    public void updateTag(Tag tag) {

        executor.execute(() -> {
            cardDAO.updateTag(tag);
            tagListIsUpdatedWithDb = false;
            cardListIsUpdatedWithDb = false;
        });
    }

}
</code>```

RecyclerViewAdapterUpdate.class

```<code>public class RecyclerViewAdapterUpdate extends RecyclerView.Adapter<RecyclerViewAdapterUpdate.ViewHolder> {

    private static final String TAG = "RecyclerViewAdapterUpdate";

    private List<CardWithTags> cardList;
    private List<Tag> tagList;
    private Context context;
    private LayoutInflater layoutInflater;
    private ViewModelMainActivity viewModelFragment;

    public RecyclerViewAdapterUpdate(List<CardWithTags> cardList, List<Tag> tagList, Context context,
                                     LayoutInflater layoutInflater, ViewModelMainActivity viewModelFragment) {
        this.cardList = cardList;
        this.tagList = tagList;
        this.context = context;
        this.layoutInflater = layoutInflater;
        this.viewModelFragment = viewModelFragment;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_update, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if (position == 0) {
            holder.numberOfItems.setText(String.valueOf(cardList.size()));
            holder.tagName.setText(R.string.all_cards_tag);
            holder.clearTag.setVisibility(View.GONE);
        } else {

            Tag tag = tagList.get(position - 1);
            List<CardWithTags> listOfSingleTag = new ArrayList<>();

            for (CardWithTags cwt: cardList) {
                for (Tag t: cwt.getTagList()) {
                    if (t.getTag().equals(tag.getTag()))
                        listOfSingleTag.add(cwt);
                }
            }

            holder.numberOfItems.setText(String.valueOf(listOfSingleTag.size()));
            holder.tagName.setText(tag.getTag());

            View viewDialogTag = layoutInflater.inflate(R.layout.dialog_modify_tag, null);
            EditText tagNameEditText = viewDialogTag.findViewById(R.id.tag_name_dialog);
            tagNameEditText.setText(tag.getTag());

            // In this way the dialog is created only one time
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setView(viewDialogTag)
                    .setTitle(R.string.title_modify_tag)
                    .setPositiveButton(R.string.modify, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            String newTag = tagNameEditText.getText().toString();
                            if (!newTag.isEmpty()&& !newTag.equalsIgnoreCase(tag.getTag())) {
                                tag.setTag(newTag);
                                viewModelFragment.updateTag(tag);
                                Log.d(TAG, ">>Update TAG: " + tag.getTag() +  "->" + newTag);
                            }
                        }
                    })
                    .create();

            holder.tagName.setOnLongClickListener( view -> {
                dialog.show();
                return true;
            });

            holder.clearTag.setOnClickListener( v -> Log.d(TAG, ">>CLICKED"));
        }

    }

    @Override
    public int getItemCount() {
        if (tagList == null)
            return 1;
        return tagList.size() + 1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private static final String TAG = "ViewHolder";

        private final TextView numberOfItems;
        private final TextView tagName;
        private final Button clearTag;
        private final CheckBox checkBox;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            numberOfItems = itemView.findViewById(R.id.number_of_items);
            tagName = itemView.findViewById(R.id.tag_name);
            clearTag = itemView.findViewById(R.id.clear_tag);
            checkBox = itemView.findViewById(R.id.check_box);

        }

    }

    public void setCardList(List<CardWithTags> cardList) {
        this.cardList = cardList;
    }

    public void setTagList(List<Tag> tagList) {
        this.tagList = tagList;
    }
}
</code>```

I don't understand why this piece of code isn't called:

```<code> viewModel.getAllCards().observe(getActivity(), cardListLoaded -> {
        cardList = cardListLoaded;
        Log.d(TAG, ">>CardList updated:" + cardList);
        updateUI(cardList, tagList);
    });

    viewModel.getAllTags().observe(getActivity(), tagListLoaded -> {
        tagList = tagListLoaded;
        Log.d(TAG, ">>TagList updated:" + tagList);
        updateUI(cardList, tagList);
    });
</code>```

MainActivity.class method that instead is called even if is equal to that one in UpdateFragment (the activity is the same):

```<code>private void firstLoadCardList() {

        ConstraintLayout fragmentContainer = findViewById(R.id.fragment_container);
        ConstraintLayout progressBarContainer = findViewById(R.id.loading_container);
        fragmentContainer.setVisibility(View.GONE);
        progressBarContainer.setVisibility(View.VISIBLE);

        Log.d(TAG, ">>FirstLoadCardList()");

        viewModel = new ViewModelProvider(this).get(ViewModelMainActivity.class);

        checkIfDatabaseAlreadyExist();

        viewModel.getAllCards().observe(this, cardListLoaded -> {

            cardList = cardListLoaded;

            Log.d(TAG, ">>Total cards found: " + cardList.size());

            if (tagList != null /*&& appJustOpened*/) {
                appJustOpened = false;
                progressBarContainer.setVisibility(View.GONE);
                fragmentContainer.setVisibility(View.VISIBLE);
                bottomNav.setSelectedItemId(R.id.play_nav);
            }
        });

        viewModel.getAllTags().observe(this, tagListLoaded -> {

            tagList = tagListLoaded;
            Log.d(TAG, ">>Total tags found: " + tagList.size());

            // If tagList not loaded before creation of BottomNav update Fragment list
            // and cardList already loaded
            if (cardList!= null /*&& appJustOpened*/) {
                appJustOpened = false;
                progressBarContainer.setVisibility(View.GONE);
                fragmentContainer.setVisibility(View.VISIBLE);
                bottomNav.setSelectedItemId(R.id.play_nav);
            }
        });

    }
</code>```

Please stop talking about notifyDataSetChanged() in the comments, I know how it works, my problem is with ViewModel/observer. The part about observer shouldn't be called every time the lists of the ViewModel change? Thank you.

答:
I think you just need to add the below function in your recycler adapter.

```<code>public void setCardList(List<CardWithTags> cardList){

this.cardList=new ArrayList<CardWithTags>()

this.cardList.addAll(cardList)

notifyDataSetChanged()

}

public void setTagList(List<Tag> tagList){

this.tagList=new ArrayList<Tag>();

this.tagList.addAll(tagList);

notifyDataSetChanged();

}
</code>```

Title: ViewModel datas don't update

Body:
I have an UpdateFragment which display cards group by tags using a RecyclerView and ViewModel. In UpdateFragment I can also modify the name of the tags but I don't know why the change isn't detected by UpdateFragment until I recreate the fragment so UI don't update. The strange thing is that the MainActivity instead detect the update of the list and the code is equal.

UpdateFragment.class

public class UpdateFragment extends BaseCardFragment {

// TODO show total cards number

private static final String TAG = "UpdateFragment";

private RecyclerView recyclerView;

public UpdateFragment(List<CardWithTags> cardList, List<Tag> tagList) {
    super(cardList, tagList);
}

@Override
public void updateUI(List<CardWithTags> cardListWithTags, List<Tag> tagList) {

    RecyclerViewAdapterUpdate adapter = (RecyclerViewAdapterUpdate) recyclerView.getAdapter();
    Log.d(TAG, "Adapter:" + adapter);
    Log.d(TAG, "Cards:" + cardListWithTags);
    Log.d(TAG, "Tags:" + tagList);
    if (adapter != null && cardListWithTags != null && tagList != null) {

        adapter.setCardList(cardListWithTags);
        adapter.setTagList(tagList);
        adapter.notifyDataSetChanged();
        Log.d(TAG, ">>Update, Total cards: " + adapter.getItemCount());
        Log.d(TAG, ">>Update, Total tags: " + tagList.size());

    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_update, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    setViewModel();

    setRecyclerView();
}

private void setViewModel() {

    Log.d(TAG, ">>SetViewModel()");

    viewModel = new ViewModelProvider(this).get(ViewModelMainActivity.class);
    viewModel.getAllCards().observe(getActivity(), cardListLoaded -> {
        cardList = cardListLoaded;
        Log.d(TAG, ">>CardList updated:" + cardList);
        updateUI(cardList, tagList);
    });

    viewModel.getAllTags().observe(getActivity(), tagListLoaded -> {
        tagList = tagListLoaded;
        Log.d(TAG, ">>TagList updated:" + tagList);
        updateUI(cardList, tagList);
    });

}

private void setRecyclerView() {

    Log.d(TAG, ">>SetRecyclerView()");
    recyclerView = getView().findViewById(R.id.recycler_view_list);

    RecyclerViewAdapterUpdate recyclerViewAdapter = new RecyclerViewAdapterUpdate(cardList, tagList,
                                                            getContext(), getLayoutInflater(),
                                                            viewModel);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(),
                                                        RecyclerView.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);

    recyclerView.setAdapter(recyclerViewAdapter);

}
}

ViewModelMainActivity.class

public class ViewModelMainActivity extends AndroidViewModel {

    private static final String TAG = "ViewModelMainActivity";
    private static boolean cardListIsUpdatedWithDb = false;
    private static boolean tagListIsUpdatedWithDb = false;

    private LiveData<List<CardWithTags>> cardList = new MutableLiveData<>();
    private LiveData<List<Tag>> tagList = new MutableLiveData<>();
    private final CardDAO cardDAO;
    private final ExecutorService executor;

    public ViewModelMainActivity(@NonNull Application application) {
        super(application);

        this.cardDAO = DatabaseTaboom.getDatabase(application).cardDao();
        this.executor = Executors.newSingleThreadExecutor();
    }

    public LiveData<List<CardWithTags>> getAllCards() {

        Log.d(TAG, ">>GetAllCards()");

        if (!cardListIsUpdatedWithDb) {
            cardList = cardDAO.getAllCards();
            cardListIsUpdatedWithDb = true;
        }

        return cardList;

    }

    public LiveData<List<Tag>> getAllTags() {

        Log.d(TAG, ">>GetAllTags()");

        if (!tagListIsUpdatedWithDb) {
            tagList = cardDAO.getAllTags();
            tagListIsUpdatedWithDb = true;
        }

        return tagList;

    }

    public void insertCard(CardWithTags card) {

        Log.d(TAG, ">>InsertCard(): " + card);

        executor.execute(() -> {

            long idCard = cardDAO.insertCard(card.getCard());

            if (idCard >= 1) {
                cardListIsUpdatedWithDb = false;
                Log.d(TAG, ">>Inserted Card: " + card.getCard().getTitle());
            }

            for (Tag t: card.getTagList()) {
                long idTag = cardDAO.insertTag(t);

                CardTagCrossRef cardTagCrossRef = new CardTagCrossRef();
                cardTagCrossRef.idCard = idCard;

                if (idTag < 1) {
                    // If Tag already exist, retrieve that to get id to try insert cwt
                    idTag = cardDAO.getTag(t.getTag()).getIdTag();
                } else {
                    // New Tag
                    tagListIsUpdatedWithDb = false;
                    Log.d(TAG, ">>Inserted Tag: " + t.getTag());
                }

                cardTagCrossRef.idTag = idTag;
                long idCWT = cardDAO.insertCardWithTags(cardTagCrossRef);
                if (idCWT >= 1) {
                    Log.d(TAG, ">>Inserted CWT: [" + idCard + "," + idTag + "]");
                    // Tag linked to a card
                    cardListIsUpdatedWithDb = false;
                }
            }

        });

    }

    public void shuffle(List<CardWithTags> list) {
        if (list != null) {
            Collections.shuffle(list);
            cardList = new MutableLiveData<>(list);
            Log.d(TAG, ">>List shuffled: " + list);
        } else {
            Log.d(TAG, ">>List is null");
        }
    }

    public void updateTag(Tag tag) {

        executor.execute(() -> {
            cardDAO.updateTag(tag);
            tagListIsUpdatedWithDb = false;
            cardListIsUpdatedWithDb = false;
        });
    }

}

RecyclerViewAdapterUpdate.class

public class RecyclerViewAdapterUpdate extends RecyclerView.Adapter<RecyclerViewAdapterUpdate.ViewHolder> {

    private static final String TAG = "RecyclerViewAdapterUpdate";

    private List<CardWithTags> cardList;
    private List<Tag> tagList;
    private Context context;
    private LayoutInflater layoutInflater;
    private ViewModelMainActivity viewModelFragment;

    public RecyclerViewAdapterUpdate(List<CardWithTags> cardList, List<Tag> tagList, Context context,
                                     LayoutInflater layoutInflater, ViewModelMainActivity viewModelFragment) {
        this.cardList = cardList;
        this.tagList = tagList;
        this.context = context;
        this.layoutInflater = layoutInflater;
        this.viewModelFragment = viewModelFragment;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_update, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if (position == 0) {
            holder.numberOfItems.setText(String.valueOf(cardList.size()));
            holder.tagName.setText(R.string.all_cards_tag);
            holder.clearTag.setVisibility(View.GONE);
        } else {

            Tag tag = tagList.get(position - 1);
            List<CardWithTags> listOfSingleTag = new ArrayList<>();

            for (CardWithTags cwt: cardList) {
                for (Tag t: cwt.getTagList()) {
                    if (t.getTag().equals(tag.getTag()))
                        listOfSingleTag.add(cwt);
                }
            }

            holder.numberOfItems.setText(String.valueOf(listOfSingleTag.size()));
            holder.tagName.setText(tag.getTag());

            View viewDialogTag = layoutInflater.inflate(R.layout.dialog_modify_tag, null);
            EditText tagNameEditText = viewDialogTag.findViewById(R.id.tag_name_dialog);
            tagNameEditText.setText(tag.getTag());

            // In this way the dialog is created only one time
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setView(viewDialogTag)
                    .setTitle(R.string.title_modify_tag)
                    .setPositiveButton(R.string.modify, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            String newTag = tagNameEditText.getText().toString();
                            if (!newTag.isEmpty()&& !newTag.equalsIgnoreCase(tag.getTag())) {
                                tag.setTag(newTag);
                                viewModelFragment.updateTag(tag);
                                Log.d(TAG, ">>Update TAG: " + tag.getTag() +  "->" + newTag);
                            }
                        }
                    })
                    .create();

            holder.tagName.setOnLongClickListener( view -> {
                dialog.show();
                return true;
            });

            holder.clearTag.setOnClickListener( v -> Log.d(TAG, ">>CLICKED"));
        }

    }

    @Override
    public int getItemCount() {
        if (tagList == null)
            return 1;
        return tagList.size() + 1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private static final String TAG = "ViewHolder";

        private final TextView numberOfItems;
        private final TextView tagName;
        private final Button clearTag;
        private final CheckBox checkBox;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            numberOfItems = itemView.findViewById(R.id.number_of_items);
            tagName = itemView.findViewById(R.id.tag_name);
            clearTag = itemView.findViewById(R.id.clear_tag);
            checkBox = itemView.findViewById(R.id.check_box);

        }

    }

    public void setCardList(List<CardWithTags> cardList) {
        this.cardList = cardList;
    }

    public void setTagList(List<Tag> tagList) {
        this.tagList = tagList;
    }
}

I don't understand why this piece of code isn't called:

 viewModel.getAllCards().observe(getActivity(), cardListLoaded -> {
        cardList = cardListLoaded;
        Log.d(TAG, ">>CardList updated:" + cardList);
        updateUI(cardList, tagList);
    });

    viewModel.getAllTags().observe(getActivity(), tagListLoaded -> {
        tagList = tagListLoaded;
        Log.d(TAG, ">>TagList updated:" + tagList);
        updateUI(cardList, tagList);
    });

MainActivity.class method that instead is called even if is equal to that one in UpdateFragment (the activity is the same):

private void firstLoadCardList() {

        ConstraintLayout fragmentContainer = findViewById(R.id.fragment_container);
        ConstraintLayout progressBarContainer = findViewById(R.id.loading_container);
        fragmentContainer.setVisibility(View.GONE);
        progressBarContainer.setVisibility(View.VISIBLE);

        Log.d(TAG, ">>FirstLoadCardList()");

        viewModel = new ViewModelProvider(this).get(ViewModelMainActivity.class);

        checkIfDatabaseAlreadyExist();

        viewModel.getAllCards().observe(this, cardListLoaded -> {

            cardList = cardListLoaded;

            Log.d(TAG, ">>Total cards found: " + cardList.size());

            if (tagList != null /*&& appJustOpened*/) {
                appJustOpened = false;
                progressBarContainer.setVisibility(View.GONE);
                fragmentContainer.setVisibility(View.VISIBLE);
                bottomNav.setSelectedItemId(R.id.play_nav);
            }
        });

        viewModel.getAllTags().observe(this, tagListLoaded -> {

            tagList = tagListLoaded;
            Log.d(TAG, ">>Total tags found: " + tagList.size());

            // If tagList not loaded before creation of BottomNav update Fragment list
            // and cardList already loaded
            if (cardList!= null /*&& appJustOpened*/) {
                appJustOpened = false;
                progressBarContainer.setVisibility(View.GONE);
                fragmentContainer.setVisibility(View.VISIBLE);
                bottomNav.setSelectedItemId(R.id.play_nav);
            }
        });

    }

Please stop talking about notifyDataSetChanged() in the comments, I know how it works, my problem is with ViewModel/observer. The part about observer shouldn't be called every time the lists of the ViewModel change? Thank you.

Body:
I think you just need to add the below function in your recycler adapter.

public void setCardList(List<CardWithTags> cardList){

this.cardList=new ArrayList<CardWithTags>()

this.cardList.addAll(cardList)

notifyDataSetChanged()

}

public void setTagList(List<Tag> tagList){

this.tagList=new ArrayList<Tag>();

this.tagList.addAll(tagList);

notifyDataSetChanged();

}


问:
Adobe Air for Android: Where is a xml file after instalation .apk file
An Air app which loading data from an external XML file (cfg.xml). I've published the Android/Air file, included the cfg.xml file in the Android package. Then I've installed on my  SD Card(). App's reading xml file - working perfectly. But I need raw access(on my device) to cfg.xml I cannot find this file, checked everywhere(Rooted Phone). Where is it? Is there other solution? I need a file which is easy to read/write.    </p>

答: Use DDMS -> File Explorer and look for you app name in folders like data, ...</p>


问:
How to make relationship between csv columns in neo4j?
I have three csv files:

sample_users.csv

```<code>user_id, first_name, location
</code>```

sample_orders.csv

```<code>order_id, user_id, product,order_ts
</code>```

sample_products.csv

```<code>product_id, product
</code>```

I was successful in making relationship between sample_users with sample_orders but now I want to get the product_id of each product ordered in the sample_orders table. but I am failing in that. There is no error but the relationship is not happening. What is going wrong?

```<code>//load user nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_users.csv' AS row
MERGE(u:User {user_id:row.user_id, name:row.first_name, location:row.location})
RETURN count(u);

//load order nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_orders.csv' AS row WITH row WHERE row.order_id IS NOT NULL
MERGE(o:Orders {order_id:row.order_id, order_ts:row.order_ts, user_id_2:row.user_id, product:row.product})
RETURN count(o);

//load product nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_products_v2.csv' AS row WITH row where row.p_name IS NOT NULL
MERGE(p:Products {product_id:row.p_id, product_name:row.p_name})
RETURN count(p);

//Create relationships
LOAD CSV WITH HEADERS FROM 'file:///sample_users.csv' AS row
MATCH(u:User{user_id:row.user_id})
MATCH(o:Orders{user_id_2:row.user_id})
MERGE(u)-[:HAS_ORDERED]->(o)
RETURN *;

LOAD CSV WITH HEADERS FROM 'file:///sample_products_v2.csv' AS row
MATCH(o:Orders{product:row.product})
MATCH(p:Products{product_name:row.p_name})
MERGE(p)-[:IS_ITEM]->(o)
RETURN *;
</code>```

答:
There is a typographical error on your script. Your column names for Products are product_id, product.

This should be the loading script:

```<code>//load product nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_products.csv' AS row WITH row where row.product IS NOT NULL
MERGE(p:Products {product_id:row.product_id, product_name:row.product})
RETURN count(p);
</code>```

Thus you should use

```<code>LOAD CSV WITH HEADERS FROM 'file:///sample_products.csv' AS row
MATCH(o:Orders{product:row.product_id})
MATCH(p:Products{product_name:row.product})
MERGE(p)-[:IS_ITEM]->(o)
RETURN *;
</code>```

Also check if Orders.product is found in Products.product_id by running this query:

```<code>MATCH (o:Orders)
MATCH (p:Products) WHERE p.product_id = o.product
RETURN n LIMIT 5
</code>```

If you the query does not return any result, then your Orders and Products are not matching

Body:
I have three csv files:

sample_users.csv

user_id, first_name, location

sample_orders.csv

order_id, user_id, product,order_ts

sample_products.csv

product_id, product

I was successful in making relationship between sample_users with sample_orders but now I want to get the product_id of each product ordered in the sample_orders table. but I am failing in that. There is no error but the relationship is not happening. What is going wrong?

//load user nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_users.csv' AS row
MERGE(u:User {user_id:row.user_id, name:row.first_name, location:row.location})
RETURN count(u);

//load order nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_orders.csv' AS row WITH row WHERE row.order_id IS NOT NULL
MERGE(o:Orders {order_id:row.order_id, order_ts:row.order_ts, user_id_2:row.user_id, product:row.product})
RETURN count(o);

//load product nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_products_v2.csv' AS row WITH row where row.p_name IS NOT NULL
MERGE(p:Products {product_id:row.p_id, product_name:row.p_name})
RETURN count(p);

//Create relationships
LOAD CSV WITH HEADERS FROM 'file:///sample_users.csv' AS row
MATCH(u:User{user_id:row.user_id})
MATCH(o:Orders{user_id_2:row.user_id})
MERGE(u)-[:HAS_ORDERED]->(o)
RETURN *;

LOAD CSV WITH HEADERS FROM 'file:///sample_products_v2.csv' AS row
MATCH(o:Orders{product:row.product})
MATCH(p:Products{product_name:row.p_name})
MERGE(p)-[:IS_ITEM]->(o)
RETURN *;

Body:
There is a typographical error on your script. Your column names for Products are product_id, product.

This should be the loading script:

//load product nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_products.csv' AS row WITH row where row.product IS NOT NULL
MERGE(p:Products {product_id:row.product_id, product_name:row.product})
RETURN count(p);

Thus you should use

LOAD CSV WITH HEADERS FROM 'file:///sample_products.csv' AS row
MATCH(o:Orders{product:row.product_id})
MATCH(p:Products{product_name:row.product})
MERGE(p)-[:IS_ITEM]->(o)
RETURN *;

Also check if Orders.product is found in Products.product_id by running this query:

MATCH (o:Orders)
MATCH (p:Products) WHERE p.product_id = o.product
RETURN n LIMIT 5

If you the query does not return any result, then your Orders and Products are not matching


问:
EF SqlQuery throws not supported MultipleActiveResultSets exception if previous operation timeouts out
I have the following logic:

```<code>public async Task UpdateData(DbContext context)
{
    try
    {
        await LongUpdate(context);
    }
    catch (Exception e)
    {
        try
        {
            await context.Database.ExecuteSqlCommandAsync($@"update d set d.UpdatedAt = GETDATE() from SomeTable d where id > 11");
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}
// this operations takes about 1 minute
private static async Task<int> LongUpdate(DbContext context)
{
    context.Database.CommandTimeout = 5; // change this to 15 to see MultipleActiveResultSets exception
    return await context.Database.SqlQuery<int>($@"update otherTable set UpdatedAt = GETDATE();SELECT @@ROWCOUNT").FirstOrDefaultAsync();
}
</code>```

As presented above there are two update operations both awaited.
LongUpdate takes more than minute.

When timeout is set to 5s:
LongUpdate throws timeout exception and the second update is executed successfully.

When I increase timeout to 15s or more:
LongUpdate throws timeout exception but second update immediately throws: System.InvalidOperationException: The connection does not support MultipleActiveResultSets..

Shouldn’t await prevent this exception?
Why this depends on timeout value?

答:
The command timeout is distinct from the connection timeout. A value set with this API for the command timeout will override any value set in the connection string. Database.CommandTimeout Property is use for Gets or sets the timeout value, in seconds, for all context operations.

private static async Task<int> LongUpdate(DbContext context)
{
    context.Database.CommandTimeout = 5; // change this to 15 to see MultipleActiveResultSets exception
    return await context.Database.SqlQuery<int>($@"update otherTable set UpdatedAt = GETDATE();SELECT @@ROWCOUNT").FirstOrDefaultAsync();
}
```
<p>here you set <code>CommandTimeout</code>, If your query not execute in 5 second then TimeoutException fired and after that you are trying to execute another query in catch block, but you use same <code>context</code> here, which is already timeout and its throws: <code>System.InvalidOperationException:</code>.
So to fix this you have to initialize your context again.</p>
<pre class="lang-cs prettyprint-override"><code>public async Task UpdateData(DbContext context)
{
    try
    {
        await LongUpdate(context);
    }
    catch (Exception e)
    {
        try
        {
            context = new MyContext()// initialize your DbContext here.
            await context.Database.ExecuteSqlCommandAsync($@"update d set d.UpdatedAt = GETDATE() from SomeTable d where id > 11");
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}
</code>```

Body:
I have the following logic:

public async Task UpdateData(DbContext context)
{
    try
    {
        await LongUpdate(context);
    }
    catch (Exception e)
    {
        try
        {
            await context.Database.ExecuteSqlCommandAsync($@"update d set d.UpdatedAt = GETDATE() from SomeTable d where id > 11");
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}
// this operations takes about 1 minute
private static async Task<int> LongUpdate(DbContext context)
{
    context.Database.CommandTimeout = 5; // change this to 15 to see MultipleActiveResultSets exception
    return await context.Database.SqlQuery<int>($@"update otherTable set UpdatedAt = GETDATE();SELECT @@ROWCOUNT").FirstOrDefaultAsync();
}

As presented above there are two update operations both awaited.
LongUpdate takes more than minute.

When timeout is set to 5s:
LongUpdate throws timeout exception and the second update is executed successfully.

When I increase timeout to 15s or more:
LongUpdate throws timeout exception but second update immediately throws: System.InvalidOperationException: The connection does not support MultipleActiveResultSets..

Shouldn’t await prevent this exception?
Why this depends on timeout value?

Body:
The command timeout is distinct from the connection timeout. A value set with this API for the command timeout will override any value set in the connection string. Database.CommandTimeout Property is use for Gets or sets the timeout value, in seconds, for all context operations.

private static async Task<int> LongUpdate(DbContext context)
{
    context.Database.CommandTimeout = 5; // change this to 15 to see MultipleActiveResultSets exception
    return await context.Database.SqlQuery<int>($@"update otherTable set UpdatedAt = GETDATE();SELECT @@ROWCOUNT").FirstOrDefaultAsync();
}

here you set CommandTimeout, If your query not execute in 5 second then TimeoutException fired and after that you are trying to execute another query in catch block, but you use same context here, which is already timeout and its throws: System.InvalidOperationException:.
So to fix this you have to initialize your context again.

public async Task UpdateData(DbContext context)
{
    try
    {
        await LongUpdate(context);
    }
    catch (Exception e)
    {
        try
        {
            context = new MyContext()// initialize your DbContext here.
            await context.Database.ExecuteSqlCommandAsync($@"update d set d.UpdatedAt = GETDATE() from SomeTable d where id > 11");
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}


问:
How can i count number of records in last 30 days for each user per row in pyspark?
I have a pyspark dataset with a sample fragment below.How can I fill the recordcounts_in_last_30_days with a count of number of records in last 30 days for each user per row like this:

date
userid
comment
recordcounts_in_last_30_days

2022-01-15 09:00
1
examplecomment1
0

2022-01-16 09:00
2
examplecomment2
0

2022-01-25 09:00
1
examplecomment3
1

2022-01-28 09:00
2
examplecomment3
1

2022-02-26 09:00
2
examplecomment3
1

2022-03-25 09:00
1
examplecomment4
0

Please write in the comments if you don't understand the problem

Body:
Use count over a window bounded with rangeBetween like this:

from pyspark.sql import functions as F, Window

w = (Window.partitionBy("userid").orderBy(F.col("date").cast("long"))
     .rangeBetween(-30 * 86400, -1) # 86400 = number of second in a day
     )

result = (df.withColumn("date", F.to_timestamp("date", "yyyy-MM-dd HH:mm"))
          .withColumn("recordcounts_in_last_30_days", F.count("*").over(w))
          )

result.show()
#+-------------------+------+---------------+----------------------------+
#|               date|userid|        comment|recordcounts_in_last_30_days|
#+-------------------+------+---------------+----------------------------+
#|2022-01-15 09:00:00|     1|examplecomment1|                           0|
#|2022-01-25 09:00:00|     1|examplecomment3|                           1|
#|2022-03-25 09:00:00|     1|examplecomment4|                           0|
#|2022-01-16 09:00:00|     2|examplecomment2|                           0|
#|2022-01-28 09:00:00|     2|examplecomment3|                           1|
#|2022-02-26 09:00:00|     2|examplecomment3|                           1|
#+-------------------+------+---------------+----------------------------+