问:
How to combine a pagination with Observables and the AsyncPipe in Angular 9?
There's a list of products coming from an API. The products are paginated and the user can switch to another page. The simplified template looks like this:

```<code><ul>
  <li *ngFor="let product of products$ | async">{{ product.name }}</li>
</ul>

<button type="button" (click)="gotoPage(1)">1</button>
<button type="button" (click)="gotoPage(2)">2</button>
</code>```

The component does look like this:

```<code>export class ProductsComponent implements OnInit {
  products$: Observable<Product[]>;

  constructor(
    private service: ProductService
  ) { }

  ngOnInit() {
    this.products$ = this.service.getAll({page: 1});
  }

  gotoPage(page: number): void {
    this.products$ = this.service.getAll({page: page});
  }
}
</code>```

My questions are:
Is this the correct way to update the Obersavble? Or does this produce memory leaks?

As a note: The URL will not change and the component should not reloaded on pagination change.

答:
As we all know with page comes also number of items per page.

I prefer to change criteria to behavior subject and combine two observables with mergeMap

class ProductService {
  constructor() {
    this.defaultCriteria = {
      page: 0,
      pageSize: 5
    }
  }

  getAll(criteria) {
    criteria = {
      ...this.defaultCriteria,
      ...criteria
    }

    return rxjs.of(Array(criteria.pageSize).fill(0).map((pr, index) => {
      const num = index + criteria.pageSize * criteria.page + 1;
      return {
        id: num,
        name: `Product ${num}`
      }
    }))
  }

}

class ProductsComponent {
  constructor(service) {
    this.service = service;
    this.page$ = new rxjs.BehaviorSubject(0);
    this.products$ = null;
  }

  ngOnInit() {
    this.products$ = this.page$.asObservable()
      .pipe(rxjs.operators.mergeMap(page => this.service.getAll({
        page
      })))
  }

  gotoPage(page) {
    this.page$.next(page);
  }
}

const service = new ProductService();
const component = new ProductsComponent(service);
component.ngOnInit();
component.products$.subscribe(products => console.log(products));
component.gotoPage(1);
component.gotoPage(2);```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.5/rxjs.umd.js"></script></code>```

Body:
There's a list of products coming from an API. The products are paginated and the user can switch to another page. The simplified template looks like this:

{{ product.name }}

<button type="button" (click)="gotoPage(1)">1</button>
<button type="button" (click)="gotoPage(2)">2</button>

The component does look like this:

export class ProductsComponent implements OnInit {
  products$: Observable<Product[]>;

  constructor(
    private service: ProductService
  ) { }

  ngOnInit() {
    this.products$ = this.service.getAll({page: 1});
  }

  gotoPage(page: number): void {
    this.products$ = this.service.getAll({page: page});
  }
}

My questions are:
Is this the correct way to update the Obersavble? Or does this produce memory leaks?

As a note: The URL will not change and the component should not reloaded on pagination change.

Body:
As we all know with page comes also number of items per page.

I prefer to change criteria to behavior subject and combine two observables with mergeMap

class ProductService {
  constructor() {
    this.defaultCriteria = {
      page: 0,
      pageSize: 5
    }
  }

  getAll(criteria) {
    criteria = {
      ...this.defaultCriteria,
      ...criteria
    }

    return rxjs.of(Array(criteria.pageSize).fill(0).map((pr, index) => {
      const num = index + criteria.pageSize * criteria.page + 1;
      return {
        id: num,
        name: `Product ${num}`
      }
    }))
  }

}

class ProductsComponent {
  constructor(service) {
    this.service = service;
    this.page$ = new rxjs.BehaviorSubject(0);
    this.products$ = null;
  }

  ngOnInit() {
    this.products$ = this.page$.asObservable()
      .pipe(rxjs.operators.mergeMap(page => this.service.getAll({
        page
      })))
  }

  gotoPage(page) {
    this.page$.next(page);
  }
}

const service = new ProductService();
const component = new ProductsComponent(service);
component.ngOnInit();
component.products$.subscribe(products => console.log(products));
component.gotoPage(1);
component.gotoPage(2);

<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.5/rxjs.umd.js"></script>


来源: stackexchange

问:
How to use TimeDsitributed layer for neural network ensmeble (specifically CNN+LSTM)?
So this is the first of 2 ways that I tried:

```<code>inputs=Input(shape=(frames, 103*4, 1))

z=TimeDistributed(Conv2D(32, (5,25), padding='same', activation='relu'), input_shape=(frames, 103*4, 1))(inputs)
z=TimeDistributed(BatchNormalization())(z)
z=TimeDistributed(ReLU())(z)
z=TimeDistributed(Dropout(0.2))(z)
z=TimeDistributed(MaxPooling2D(pool_size=(1,2)))(z)

z=TimeDistributed(Conv2D(32, (3,5), padding='same', activation='relu'))(z)
z=TimeDistributed(BatchNormalization())(z)
z=TimeDistributed(ReLU())(z)
z=TimeDistributed(Dropout(0.2))(z)
z=TimeDistributed(MaxPooling2D(pool_size=(1,2)))(z)

z=TimeDistributed(Flatten())(z)

z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)
z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)
z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)

z=Flatten()(z)

z=Dense(1000, activation='relu')(z)
z=Dropout(0.5)(z)

z=Dense(500, activation='relu')(z)
z=Dropout(0.5)(z)

z=Dense(200, activation='relu')(z)
z=Dropout(0.5)(z)

outputs=Dense(88, activation='sigmoid')(z)
</code>```

I'm getting this error (although I don't have strides size of 2 anywhere in my code):

```<code>ValueError: strides should be of length 1, 1 or 3 but was 2
</code>```

I've already looked at this question and it seems that I've implemented the advice given in my code. But what's the problem? I can't get a grasp of it.

Finally I managed to combine CNN and LSTM via FunctionalAPI of Keras:

```<code>inputs=Input(shape=(frames, 103*4, 1))

z=Conv2D(32, (5,25), padding='same', activation='relu')(inputs)
z=BatchNormalization()(z)
z=ReLU()(z)
z=Dropout(0.2)(z)
z=MaxPooling2D(pool_size=(1,2))(z)

z=Conv2D(32, (3,5), padding='same', activation='relu')(z)
z=BatchNormalization()(z)
z=ReLU()(z)
z=Dropout(0.2)(z)
z=MaxPooling2D(pool_size=(1,2))(z)

z=TimeDistributed(Flatten())(z)

z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)
z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)
z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)

z=Flatten()(z)

z=Dense(1000, activation='relu')(z)
z=Dropout(0.5)(z)

z=Dense(500, activation='relu')(z)
z=Dropout(0.5)(z)

z=Dense(200, activation='relu')(z)
z=Dropout(0.5)(z)

outputs=Dense(88, activation='sigmoid')(z)

model=Model(inputs=inputs, outputs=outputs)
</code>```

It works, but I'm still at a loss if that's the right way. I can see via model.summary() that my TimeDstributed wrapper has output shape (None, 7, 3296) and all LSTM layers have output shapes (None, 7, 1000). What should I change to give it center frame instead of 7 frames?

Speaking of my dataset, I give 7 frames of amplitudes of frequencies values to predict values in the center frame.

答:
I am assuming that you want to apply the TimeDistributed layer over the frames dimension which makes our input to be of 2-dimension which is sent to the convolution layer.
If that is the case then you should use Conv1D not Conv2D. Change the kernel size accordingly.
Also change Maxpooling2D to Maxpooling1D accordingly.

For eg.

```<code>z=TimeDistributed(Conv1D(32, 2, padding='same', activation='relu'))(inputs)
</code>```

ContentLicense: CC BY-SA 4.0

Body:
So this is the first of 2 ways that I tried:

inputs=Input(shape=(frames, 103*4, 1))

z=TimeDistributed(Conv2D(32, (5,25), padding='same', activation='relu'), input_shape=(frames, 103*4, 1))(inputs)
z=TimeDistributed(BatchNormalization())(z)
z=TimeDistributed(ReLU())(z)
z=TimeDistributed(Dropout(0.2))(z)
z=TimeDistributed(MaxPooling2D(pool_size=(1,2)))(z)

z=TimeDistributed(Conv2D(32, (3,5), padding='same', activation='relu'))(z)
z=TimeDistributed(BatchNormalization())(z)
z=TimeDistributed(ReLU())(z)
z=TimeDistributed(Dropout(0.2))(z)
z=TimeDistributed(MaxPooling2D(pool_size=(1,2)))(z)

z=TimeDistributed(Flatten())(z)

z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)
z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)
z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)

z=Flatten()(z)

z=Dense(1000, activation='relu')(z)
z=Dropout(0.5)(z)

z=Dense(500, activation='relu')(z)
z=Dropout(0.5)(z)

z=Dense(200, activation='relu')(z)
z=Dropout(0.5)(z)

outputs=Dense(88, activation='sigmoid')(z)

I'm getting this error (although I don't have strides size of 2 anywhere in my code):

ValueError: strides should be of length 1, 1 or 3 but was 2

I've already looked at this question and it seems that I've implemented the advice given in my code. But what's the problem? I can't get a grasp of it.

Finally I managed to combine CNN and LSTM via FunctionalAPI of Keras:

inputs=Input(shape=(frames, 103*4, 1))

z=Conv2D(32, (5,25), padding='same', activation='relu')(inputs)
z=BatchNormalization()(z)
z=ReLU()(z)
z=Dropout(0.2)(z)
z=MaxPooling2D(pool_size=(1,2))(z)

z=Conv2D(32, (3,5), padding='same', activation='relu')(z)
z=BatchNormalization()(z)
z=ReLU()(z)
z=Dropout(0.2)(z)
z=MaxPooling2D(pool_size=(1,2))(z)

z=TimeDistributed(Flatten())(z)

z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)
z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)
z=LSTM(1000, dropout=0.1, recurrent_dropout=0.2, return_sequences=True)(z)

z=Flatten()(z)

z=Dense(1000, activation='relu')(z)
z=Dropout(0.5)(z)

z=Dense(500, activation='relu')(z)
z=Dropout(0.5)(z)

z=Dense(200, activation='relu')(z)
z=Dropout(0.5)(z)

outputs=Dense(88, activation='sigmoid')(z)

model=Model(inputs=inputs, outputs=outputs)

It works, but I'm still at a loss if that's the right way. I can see via model.summary() that my TimeDstributed wrapper has output shape (None, 7, 3296) and all LSTM layers have output shapes (None, 7, 1000). What should I change to give it center frame instead of 7 frames?

Speaking of my dataset, I give 7 frames of amplitudes of frequencies values to predict values in the center frame.

Body:
I am assuming that you want to apply the TimeDistributed layer over the frames dimension which makes our input to be of 2-dimension which is sent to the convolution layer.
If that is the case then you should use Conv1D not Conv2D. Change the kernel size accordingly.
Also change Maxpooling2D to Maxpooling1D accordingly.

For eg.

z=TimeDistributed(Conv1D(32, 2, padding='same', activation='relu'))(inputs)


问:
DOM throwing error because of Multiple HTML files
I am trying to create a simple recipe-app with javascript.
I have 3 HTML files: index.html, create.html, edit.html
and 2 JS files.

I have a couple of DOM elements. One on index.html, Two on edit, Two on create.
Now when I open index.html I get an error:

```<code>recipe.js:14 Uncaught TypeError: Cannot set property 'value' of null
</code>```

The problem here is that the code that the error is it's about edit.html not index.html.
Same problem on edit.html

```<code>Uncaught TypeError: Cannot read property 'appendChild' of null
at recipe.js:55
</code>```

recipe.js code 55 is about index.html, not edit.html.

Is there a way to target an HTML file when using DOM.

```<code>document.getElementById("edit-body").value = getRecipeBody();
</code>```

I want the code above to be only for edit.html, not for index.html or create.html.

edit-body is a form that is only on edit.html, not on index or create.html

```<code>document.getElementById('recipes').appendChild(recipeEl)
</code>```

I want this code to be for index.html not for other HTML files because there is no #recipes ID on those files. The #recipes is only on index.html

I am using localStorage.

答:
window.location.pathname would let you execute code for a specific path.

```<code>if(window.location.pathname === '/edit.html') {
  document.getElementById("edit-body").value = getRecipeBody();
}
</code>```

Note that if you load index.html by default (as in, http://localhost:80/ as opposed to http://localhost:80/index.html), then the pathname will simply be /, so make sure you handle both of those, i.e.:

```<code>if(window.location.pathname === '/index.html' || window.location.pathname === '/') {
  document.getElementById('recipes').appendChild(recipeEl)
}
</code>```

A better approach would be to code it defensively. Check that the element you're getting exists before running a function against it. For example:

```<code>var editEl = document.getElementById("edit-body");
if (editEl) {
  editEl.value = getRecipeBody();
}
</code>```

Instead of either of these, you could also just load index.js on index.html, edit.js on edit.html, etc., though you'd want to split out any shared functions into a separate (common) JS file.

Body:
I am trying to create a simple recipe-app with javascript.
I have 3 HTML files: index.html, create.html, edit.html
and 2 JS files.

I have a couple of DOM elements. One on index.html, Two on edit, Two on create.
Now when I open index.html I get an error:

recipe.js:14 Uncaught TypeError: Cannot set property 'value' of null

The problem here is that the code that the error is it's about edit.html not index.html.
Same problem on edit.html

Uncaught TypeError: Cannot read property 'appendChild' of null
at recipe.js:55

recipe.js code 55 is about index.html, not edit.html.

Is there a way to target an HTML file when using DOM.

document.getElementById("edit-body").value = getRecipeBody();

I want the code above to be only for edit.html, not for index.html or create.html.

edit-body is a form that is only on edit.html, not on index or create.html

document.getElementById('recipes').appendChild(recipeEl)

I want this code to be for index.html not for other HTML files because there is no #recipes ID on those files. The #recipes is only on index.html

I am using localStorage.

Body:
window.location.pathname would let you execute code for a specific path.

if(window.location.pathname === '/edit.html') {
  document.getElementById("edit-body").value = getRecipeBody();
}

Note that if you load index.html by default (as in, http://localhost:80/ as opposed to http://localhost:80/index.html), then the pathname will simply be /, so make sure you handle both of those, i.e.:

if(window.location.pathname === '/index.html' || window.location.pathname === '/') {
  document.getElementById('recipes').appendChild(recipeEl)
}

A better approach would be to code it defensively. Check that the element you're getting exists before running a function against it. For example:

var editEl = document.getElementById("edit-body");
if (editEl) {
  editEl.value = getRecipeBody();
}

Instead of either of these, you could also just load index.js on index.html, edit.js on edit.html, etc., though you'd want to split out any shared functions into a separate (common) JS file.


问:
php implode breaks on spaces

I have an array of strings, and these strings have spaces in them. For example:

```<code>$arr = array('bob', 'john smith', 'grease monkey', 'etc');
</code>```

Why is it when I try to $str = implode('|', $arr);, it stops at the first space it finds??
I'm left with a string like:

```<code>$str = "bob|john";
</code>```

If I try:

```<code>$arr = array('bob', 'john', 'grease monkey', 'etc');
</code>```

and implode, I get:

```<code>$str = "bob|john|grease";
</code>```

Edit: I'm actually trying to set it to the value of a hidden field:

```<code><input id="hidLblFields" name="hidLblFields" type="text" value=<?php echo implode('|', $myFields);?> />
</code>```

答: This is simply not possible. You have some other functions doing some other operations</p>

ContentLicense: CC BY-SA 3.0

Title: php implode breaks on spaces

Body:
I have an array of strings, and these strings have spaces in them. For example:

$arr = array('bob', 'john smith', 'grease monkey', 'etc');

Why is it when I try to $str = implode('|', $arr);, it stops at the first space it finds??
I'm left with a string like:

$str = "bob|john";

If I try:

$arr = array('bob', 'john', 'grease monkey', 'etc');

and implode, I get:

$str = "bob|john|grease";

Edit: I'm actually trying to set it to the value of a hidden field:

<input id="hidLblFields" name="hidLblFields" type="text" value=<?php echo implode('|', $myFields);?> />


问:
What is `"dev" true` in package-lock.json for?
What does "dev" true means in package-lock.json file?

In my case it is automatically updated when I perform npm operations.

How can we remove it?

答:
So answering your first question, "dev": true in package-lock.json means this dependency won't be installed by npm install/npm ci when running in production mode.

Having dependencies used only for local development environment marked with "dev": true and then using --production in your CI  might save you some build time.

From documentation https://docs.npmjs.com/cli/install#description:

By default, npm install will install all modules listed as
  dependencies in package.json.

With the --production flag (or when the NODE_ENV environment
  variable is set to production), npm will not install modules listed
  in devDependencies.


问:
Why subprocess "sleep 10" does not terminate?
I need to test if a process terminates and all I have is its pid number. To do so I test if the pseudo file "/proc/<pid>" exist.

Writing a test for this function, I noticed that the process doesn’t terminate as expected.

For the test, I run "sleep 10" as sub-process which should run for 10 seconds. After starting this process, I poll for the disappearance of the pseudo file "/proc/<pid>". That pseudo file never disappear and the termination of the sub-process is not detected.

Testing the code of golang playground reproduces the problem: https://play.golang.org/p/fb4CbXkIjh3.

I checked that the process is created, and that the pid is correct. While checking the process is seen that it turns into <defunct>. It isn’t thus removed.

The questions are the following:

why doesn’t the sub-process terminate ?

how can I change the code to make it terminate ?

```<code>package main

import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "strconv"
    "time"
)

func main() {
    fmt.Println("Hello, playground")

    cmd := exec.Command("sleep", "10")
    if err := cmd.Start(); err != nil {
        log.Fatal("unexpected error:", err)
    }
    pidStr := strconv.Itoa(cmd.Process.Pid)
    log.Println("sleep pid:", pidStr)

    for {
        if _, err := os.Stat("/proc/" + pidStr); os.IsNotExist(err) {
            log.Println("detect termination of /proc/" + pidStr)
            return
        }
        log.Println("pgm /proc/" + pidStr + " is running")
        time.Sleep(3 * time.Second)
    }
}
</code>```

答:
At the OS level, in any POSIX-compatible OS (Unix, Linux, Darwin, etc), a process that has completed, but not yet been collected by its superior, is in "defunct" or "zombie" state.  It still exists, but cannot be killed: it is already dead.  It exists precisely so that its superior—the process that can call the OS level wait system call—can call the OS level wait system call and see that the process is now dead.

Once its superior has waited for it, the process is truly removed: there is no longer a zombie process hogging that process ID.  If you have a /proc file system, this is when the process vanishes from /proc.

In Go, calling cmd.Wait() invokes the OS-level wait call, so that is the way to do this.  If you would like to collect the result of this cmd.Wait(), a good way to do that is to send it through a channel.

(If you want to spawn a very long running process and not wait for it, you can disown it, so that you are no longer its superior.  The details on doing this correctly are full of OS-specific doodads, such as discarding control ttys, setting sessions, using procctl or prctl, and so on.)

Body:
I need to test if a process terminates and all I have is its pid number. To do so I test if the pseudo file "/proc/<pid>" exist.

Writing a test for this function, I noticed that the process doesn’t terminate as expected.

For the test, I run "sleep 10" as sub-process which should run for 10 seconds. After starting this process, I poll for the disappearance of the pseudo file "/proc/<pid>". That pseudo file never disappear and the termination of the sub-process is not detected.

Testing the code of golang playground reproduces the problem: https://play.golang.org/p/fb4CbXkIjh3.

I checked that the process is created, and that the pid is correct. While checking the process is seen that it turns into <defunct>. It isn’t thus removed.

The questions are the following:

why doesn’t the sub-process terminate ?

how can I change the code to make it terminate ?

package main

import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "strconv"
    "time"
)

func main() {
    fmt.Println("Hello, playground")

    cmd := exec.Command("sleep", "10")
    if err := cmd.Start(); err != nil {
        log.Fatal("unexpected error:", err)
    }
    pidStr := strconv.Itoa(cmd.Process.Pid)
    log.Println("sleep pid:", pidStr)

    for {
        if _, err := os.Stat("/proc/" + pidStr); os.IsNotExist(err) {
            log.Println("detect termination of /proc/" + pidStr)
            return
        }
        log.Println("pgm /proc/" + pidStr + " is running")
        time.Sleep(3 * time.Second)
    }
}


问:
How to use np.random.uniform() for real world examples

What I tried

```<code>from numpy import random
import matplotlib.pyplot as plt

import seaborn as sns

sns.distplot(random.uniform(0,30, 5), hist=True)

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

Result which I got
Here its showing 8% and 12%, but my required answer as according to manual prediction is 16%. I think I am confused about the size parameter in `random.binomial()

答:
From what I understand you want to simulate the result.

Try the following

```<code>import numpy as np

# sample 100k uniform random values (it can be any large number) from 0 to 30
waiting_time = np.random.uniform(0, 30, size = 100_000)

# caclulate the proportion that are between 10 and 15
np.mean((waiting_time >=10) & (waiting_time <= 15))
# will be around 0.166 i.e. around 16.6%.

</code>```

Plot

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

sns.distplot(waiting_time)
plt.show();

# OR
import matplotlib.pyplot as plt
plt.hist(waiting_time);
</code>```

Body:
What I tried

from numpy import random
import matplotlib.pyplot as plt

import seaborn as sns

sns.distplot(random.uniform(0,30, 5), hist=True)

plt.show()

Result which I got
Here its showing 8% and 12%, but my required answer as according to manual prediction is 16%. I think I am confused about the size parameter in `random.binomial()

Body:
From what I understand you want to simulate the result.

Try the following

import numpy as np

# sample 100k uniform random values (it can be any large number) from 0 to 30
waiting_time = np.random.uniform(0, 30, size = 100_000)

# caclulate the proportion that are between 10 and 15
np.mean((waiting_time >=10) & (waiting_time <= 15))
# will be around 0.166 i.e. around 16.6%.

Plot

import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot(waiting_time)
plt.show();

# OR
import matplotlib.pyplot as plt
plt.hist(waiting_time);


问:
How to async parse/write json in webflux? ObjectMapper methods are blocking
I saw that jackson supports non-blocking since 2.9, but how to use it with webflux? Is there a demo?</p>

答: Just in case if you are looking for blocking ObjectMapper, try with subscribeOn method in a thread pool such as bounded elastic. And wrap it with Mono.fromCallable


问:
How to get size and position of a sceneWindow under mac Catalyst
I have been able to obtain the size of a sceneWindow when I resize it using

```<code>func windowScene(_ windowScene: UIWindowScene, didUpdate previousCoordinateSpace: UICoordinateSpace, interfaceOrientation previousInterfaceOrientation: UIInterfaceOrientation, traitCollection previousTraitCollection: UITraitCollection) {
        print("movement trapped \(windowScene.coordinateSpace.bounds)"
}
</code>```

within the sceneDelegate.   But the x,y coordinates are always 0,0 regardless of where I drag the window to.   Looking to be able to dictate where the new sceneWindow is located on the mac's screen relative to the "default" sceneWindow.

答:
Well

I didn't find a way to get this done in UIWindow but remember, Catalyst does support AppKit and you can call it anytime use objc runtime.

So here comes up the idea:

Get NSWindows from NSApplication

Get UIWindow from view.window

Compare some magic and lookup our target NSWindow

Get the frame of that NSWindow

```<code>var targetNSWindow: AnyObject? = nil
let nsWindows = (NSClassFromString("NSApplication")?.value(forKeyPath: "sharedApplication.windows") as? [AnyObject])!
for nsWindow in nsWindows {
    let uiWindows = nsWindow.value(forKeyPath: "uiWindows") as? [UIWindow] ?? []
    if uiWindows.contains(view.window!) {
        targetNSWindow = nsWindow
    }
}
if let found = targetNSWindow {
    print(found.value(forKeyPath: "_frame")!)
}
</code>```

And here is a sample output.

```<code>NSRect: {{818, 296}, {964, 614}}
</code>```

A little bit more, you can have your window information from sceneDelegate and compare the magic with it in a similar way. But be careful, sometimes you don't have any window when the app just loads. Do the jobs in DispatchQueue.main.async block if that happens.

Body:
I have been able to obtain the size of a sceneWindow when I resize it using

func windowScene(_ windowScene: UIWindowScene, didUpdate previousCoordinateSpace: UICoordinateSpace, interfaceOrientation previousInterfaceOrientation: UIInterfaceOrientation, traitCollection previousTraitCollection: UITraitCollection) {
        print("movement trapped \(windowScene.coordinateSpace.bounds)"
}

within the sceneDelegate.   But the x,y coordinates are always 0,0 regardless of where I drag the window to.   Looking to be able to dictate where the new sceneWindow is located on the mac's screen relative to the "default" sceneWindow.

Body:
Well

I didn't find a way to get this done in UIWindow but remember, Catalyst does support AppKit and you can call it anytime use objc runtime.

So here comes up the idea:

Get NSWindows from NSApplication

Get UIWindow from view.window

Compare some magic and lookup our target NSWindow

Get the frame of that NSWindow

var targetNSWindow: AnyObject? = nil
let nsWindows = (NSClassFromString("NSApplication")?.value(forKeyPath: "sharedApplication.windows") as? [AnyObject])!
for nsWindow in nsWindows {
    let uiWindows = nsWindow.value(forKeyPath: "uiWindows") as? [UIWindow] ?? []
    if uiWindows.contains(view.window!) {
        targetNSWindow = nsWindow
    }
}
if let found = targetNSWindow {
    print(found.value(forKeyPath: "_frame")!)
}

And here is a sample output.

NSRect: {{818, 296}, {964, 614}}

A little bit more, you can have your window information from sceneDelegate and compare the magic with it in a similar way. But be careful, sometimes you don't have any window when the app just loads. Do the jobs in DispatchQueue.main.async block if that happens.


问:
Why does sqlalchemy seem to be spontaneously committing certain queries while ignoring others?
I have a quirky GCP cloud function that does, at sort of a high level, unintelligent website scraping, by taking advantage of the website accepting a numerically ascending 'id' parameter.

main.py

def upsert(db, i, x, y):
    with db.connect() as cursor:
        cursor.execute(f"REPLACE INTO TABLE_NAME (i, x, y) VALUES (i, \"{x}\", \"{y}\"")

def main(data, context):
    db = sqlalchemy.connect(...)

    for i in range(0, 100):
        soup = BeautifulSoup(f"websiteimscraping.com/id={i}")
        if soup is not valid:
            continue

        x = soup.find_all(...)
        y = soup.find_all(...)

        upsert(db, i, x, y)
```

<p>In this example, I know for a fact that most if not all of the id's 0 thru 99 are valid + unique and I should have a very similar amount of rows in the database when the cloud function is complete.</p>

<p>To verify that I have 100 rows in MySQL, I enter debug mode and evaluate the expression:</p>

<pre class="lang-py prettyprint-override"><code>list(cursor.execute("SELECT * FROM TABLE_NAME"))
</code>```

and I get back like 20% of the rows i'd expect, without any semblance of order. with ids Like (7, 8, 9, 10, 13, 44, 45, 46, 47, 48, 49, 50, 51, 86)

I do some print statements to make sure none of my edge cases are triggered (which would result in skipping an invalid id) and indeed, none are in the code, which I verified by stepping through in debug mode and seeing all the id's sent to the upsert step.

Something which I think is important to clarify is that the rows saved to the database each time are not randomly determined -> it's the same rows each time that are not inserted/updated.

A core assumption I'm making is that if there were to be a data issue for the rows that are not being inserted which caused some issue with the REPLACE statement, sqlalchemy would raise an exception for me. sqlalchemy raised no exceptions throughout the entire function.

Is there anything not data related with regards to sqlalchemy that could be causing rows not to be inserted?

I have tried looking for patterns in the data for the rows that have been inserted to try to gauge some sort of data (in)consistency or patterns (which would seem like the likelier suspect) but haven't found any.

I wish it were simpler for me to give the code away for reproducing but I'm pretty sure no one wants to set up their own mysql instance to try this out - if you are, I am happy to give the actual code and you can try it yourself.

答:
I have executed your code and I managed to reproduce your scenario, encountering the same problem that only some rows seem to be inserted.

The problem is that you are using a REPLACE statement. As stated in MySQL's documentation a REPLACE statement works exactly like an INSERT operation but if the PRIMARY KEY already exists said row is replaced instead of failing the query.

As per your shared code it seems like you are using the field code as PRIMARY KEY but you are checking the field id as an indicator of an inserted web page. What happens is that multiple rows have the same code, thus every time one is inserted the previous one is removed.

I have solved your issue simply making the field id the PRIMARY KEY instead of code. Once you change that (remember to drop the original table), you can run your code again and you will see no missing ids. You can verify it using:

```<code>SELECT id FROM CLASSES_MASTER;
</code>```

By the way, now that you have all the data inserted you can check the amount of repeated codes with this:

```<code>SELECT code, COUNT(*) FROM CLASSES_MASTER GROUP BY code;
</code>```

Body:
I have a quirky GCP cloud function that does, at sort of a high level, unintelligent website scraping, by taking advantage of the website accepting a numerically ascending 'id' parameter.

main.py

def upsert(db, i, x, y):
    with db.connect() as cursor:
        cursor.execute(f"REPLACE INTO TABLE_NAME (i, x, y) VALUES (i, \"{x}\", \"{y}\"")

def main(data, context):
    db = sqlalchemy.connect(...)

    for i in range(0, 100):
        soup = BeautifulSoup(f"websiteimscraping.com/id={i}")
        if soup is not valid:
            continue

        x = soup.find_all(...)
        y = soup.find_all(...)

        upsert(db, i, x, y)

In this example, I know for a fact that most if not all of the id's 0 thru 99 are valid + unique and I should have a very similar amount of rows in the database when the cloud function is complete.

To verify that I have 100 rows in MySQL, I enter debug mode and evaluate the expression:

list(cursor.execute("SELECT * FROM TABLE_NAME"))

and I get back like 20% of the rows i'd expect, without any semblance of order. with ids Like (7, 8, 9, 10, 13, 44, 45, 46, 47, 48, 49, 50, 51, 86)

I do some print statements to make sure none of my edge cases are triggered (which would result in skipping an invalid id) and indeed, none are in the code, which I verified by stepping through in debug mode and seeing all the id's sent to the upsert step.

Something which I think is important to clarify is that the rows saved to the database each time are not randomly determined -> it's the same rows each time that are not inserted/updated.

A core assumption I'm making is that if there were to be a data issue for the rows that are not being inserted which caused some issue with the REPLACE statement, sqlalchemy would raise an exception for me. sqlalchemy raised no exceptions throughout the entire function.

Is there anything not data related with regards to sqlalchemy that could be causing rows not to be inserted?

I have tried looking for patterns in the data for the rows that have been inserted to try to gauge some sort of data (in)consistency or patterns (which would seem like the likelier suspect) but haven't found any.

I wish it were simpler for me to give the code away for reproducing but I'm pretty sure no one wants to set up their own mysql instance to try this out - if you are, I am happy to give the actual code and you can try it yourself.

Body:
I have executed your code and I managed to reproduce your scenario, encountering the same problem that only some rows seem to be inserted.

The problem is that you are using a REPLACE statement. As stated in MySQL's documentation a REPLACE statement works exactly like an INSERT operation but if the PRIMARY KEY already exists said row is replaced instead of failing the query.

As per your shared code it seems like you are using the field code as PRIMARY KEY but you are checking the field id as an indicator of an inserted web page. What happens is that multiple rows have the same code, thus every time one is inserted the previous one is removed.

I have solved your issue simply making the field id the PRIMARY KEY instead of code. Once you change that (remember to drop the original table), you can run your code again and you will see no missing ids. You can verify it using:

SELECT id FROM CLASSES_MASTER;

By the way, now that you have all the data inserted you can check the amount of repeated codes with this:

SELECT code, COUNT(*) FROM CLASSES_MASTER GROUP BY code;


问:
asyncio.Queue as producer-consumer flow in a webserver like Quart
Is it possible to use asyncio.Queue with a webserver like Quart to communicate between the producer and consumer?

Here is what I am trying to do....

```<code>from quart import Quart, request
import asyncio

queue = asyncio.Queue()
producers = []
consumers = []

async def producer(mesg):
    print(f'produced {mesg}')
    await queue.put(mesg)
    await asyncio.sleep(1) # do some work

async def consumer():
    while True:
        token = await queue.get()
        await asyncio.sleep(1) # do some work
        queue.task_done()
        print(f'consumed {token}')

@app.route('/route', methods=['POST'])
async def index():
    mesg = await request.get_data()
    try:
        p = asyncio.create_task(producer(mesg))
        producers.append(p)
        c = asyncio.create_task(consumer())
        consumers.append(c)
        return f"published message {mesg}", 200
    except Exception as e:
        logger.exception("Failed tp publish message %s!", mesg)
        return f"Failed to publish message: {mesg}", 400

if __name__ == '__main__':
    PORT = int(os.getenv('PORT')) if os.getenv('PORT') else 8050
    app.run(host='0.0.0.0', port=PORT, debug=True)
</code>```

This works fine.
But I am not sure if this is a good practice because I am confused how (where in my code) to do the below steps.

```<code># Making sure all the producers have completed
await asyncio.gather(*producers)

#wait for the remaining tasks to be processed
await queue.join()

# cancel the consumers, which are now idle
for c in consumers:
    c.cancel()
</code>```

EDIT-1:

I have tried using @app.after_serving, with some logger.debug statements.

```<code>@app.after_serving
async def shutdown():
    logger.debug("Shutting down...")
    logger.debug("waiting for producers to finish...")
    await asyncio.gather(*producers)
    logger.debug("waiting for tasks to complete...")
    await queue.join()
    logger.debug("cancelling consumers...")
    for c in consumers:
        c.cancel()
</code>```

But the debug statements are not printed when hypercorn is gracefully shutting down. So, I am not sure whether the function(shutdown) decorated with @app.after_serving is actually called during a shutdown.

Here is the message from hypercorn during shutdown

```<code>appserver_1  | 2020-05-29 15:55:14,200 - base_events.py:1490 -        create_server - INFO - <Server sockets=(<asyncio.TransportSocket fd=14, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 8080)>,)> is serving
appserver_1  | Running on 0.0.0.0:8080 over http (CTRL + C to quit)
Gracefully stopping... (press Ctrl+C again to force)
</code>```

I using a kill -SIGTERM <PID> to signal a graceful shutdown to the process.

答:
I would place the cleanup code in a shutdown after_serving function,

```<code>@app.after_serving
async def shutdown():
    # Making sure all the producers have completed
    await asyncio.gather(*producers)

    #wait for the remaining tasks to be processed
    await queue.join()

    # cancel the consumers, which are now idle
    for c in consumers:
        c.cancel()
</code>```

As for the globals, I tend to store them on the app directly so that they can be accessed via the current_app proxy. Please note though that this (and your solution) only works for a single process (worker), if you want to use multiple workers (or equivalently hosts) you will need a third party store for this information e.g. using redis.

Body:
Is it possible to use asyncio.Queue with a webserver like Quart to communicate between the producer and consumer?

Here is what I am trying to do....

from quart import Quart, request
import asyncio

queue = asyncio.Queue()
producers = []
consumers = []

async def producer(mesg):
    print(f'produced {mesg}')
    await queue.put(mesg)
    await asyncio.sleep(1) # do some work

async def consumer():
    while True:
        token = await queue.get()
        await asyncio.sleep(1) # do some work
        queue.task_done()
        print(f'consumed {token}')

@app.route('/route', methods=['POST'])
async def index():
    mesg = await request.get_data()
    try:
        p = asyncio.create_task(producer(mesg))
        producers.append(p)
        c = asyncio.create_task(consumer())
        consumers.append(c)
        return f"published message {mesg}", 200
    except Exception as e:
        logger.exception("Failed tp publish message %s!", mesg)
        return f"Failed to publish message: {mesg}", 400

if __name__ == '__main__':
    PORT = int(os.getenv('PORT')) if os.getenv('PORT') else 8050
    app.run(host='0.0.0.0', port=PORT, debug=True)

This works fine.
But I am not sure if this is a good practice because I am confused how (where in my code) to do the below steps.

# Making sure all the producers have completed
await asyncio.gather(*producers)

#wait for the remaining tasks to be processed
await queue.join()

# cancel the consumers, which are now idle
for c in consumers:
    c.cancel()

EDIT-1:

I have tried using @app.after_serving, with some logger.debug statements.

@app.after_serving
async def shutdown():
    logger.debug("Shutting down...")
    logger.debug("waiting for producers to finish...")
    await asyncio.gather(*producers)
    logger.debug("waiting for tasks to complete...")
    await queue.join()
    logger.debug("cancelling consumers...")
    for c in consumers:
        c.cancel()

But the debug statements are not printed when hypercorn is gracefully shutting down. So, I am not sure whether the function(shutdown) decorated with @app.after_serving is actually called during a shutdown.

Here is the message from hypercorn during shutdown

appserver_1  | 2020-05-29 15:55:14,200 - base_events.py:1490 -        create_server - INFO - <Server sockets=(<asyncio.TransportSocket fd=14, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 8080)>,)> is serving
appserver_1  | Running on 0.0.0.0:8080 over http (CTRL + C to quit)
Gracefully stopping... (press Ctrl+C again to force)

I using a kill -SIGTERM <PID> to signal a graceful shutdown to the process.

Body:
I would place the cleanup code in a shutdown after_serving function,

@app.after_serving
async def shutdown():
    # Making sure all the producers have completed
    await asyncio.gather(*producers)

    #wait for the remaining tasks to be processed
    await queue.join()

    # cancel the consumers, which are now idle
    for c in consumers:
        c.cancel()

As for the globals, I tend to store them on the app directly so that they can be accessed via the current_app proxy. Please note though that this (and your solution) only works for a single process (worker), if you want to use multiple workers (or equivalently hosts) you will need a third party store for this information e.g. using redis.


问:
How to cache image files in spring java application?
In my public facing web app, images ( along with meta data) are shown like in photo album app which is being developed using Spring framework. What I want is to cache in RAM all images ( thousands of them) from the file system so that when image tag like is encountered in HTML:

the image is served from cache memory and not from disk by tomcat webserver for high performance.

How to achieve this in spring framework web application?

If mongodb is used to store web content like image meta data, how to achieve above in this scenario in the same spring web app?

So there are two answers one involving no mongodb and one involving mongodb.

答:
@Christaan's answer can also be extended to cache externally referenced images locally to your solution, for use in say captive environments. For example:</p>

```<code>@ResponseBody
@RequestMapping(value = "/sample-image", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
@Cacheable("images")
public byte[] image() throws IOException {

            URL url = new URL("http://iminsys.com/assets/images/pages/about/edrich.jpg");
            InputStream in = new BufferedInputStream(url.openStream());
            return FileCopyUtils.copyToByteArray(in); // using Spring's FileCopyUtils
}
</code>```

Body:
@Christaan's answer can also be extended to cache externally referenced images locally to your solution, for use in say captive environments. For example:

@ResponseBody
@RequestMapping(value = "/sample-image", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
@Cacheable("images")
public byte[] image() throws IOException {

            URL url = new URL("http://iminsys.com/assets/images/pages/about/edrich.jpg");
            InputStream in = new BufferedInputStream(url.openStream());
            return FileCopyUtils.copyToByteArray(in); // using Spring's FileCopyUtils
}


问:
Error C2719 in header files - without using stl:vector
I have some problems with compiling my code. There are couple of functions which can't be compiled because of error C2719 - formal parameter with __declspec(align('16')) won't be aligned.

Functions which VisualStudio can't compile look like that

```<code>Eigen::Matrix2d AlgorithmBase::ReverseTransform(Eigen::Vector2d point, Eigen::Vector2d *translation, Eigen::Matrix2d *scaling, double phi, Eigen::Matrix2d *share)
{
    Eigen::Matrix2d reversedScaling;
    reversedScaling(0,0) = 1/(*scaling)(0,0);
    reversedScaling(0,1) = reversedScaling(1,0) = 0;
    reversedScaling(1,1) = 1/(*scaling)(1,1);
    Eigen::MatrixXd  newTranslation = -1**translation;

    return  MatrixHelper::CreateRotationMatrix(-phi)* *scaling*point + newTranslation;
}

void TemplateClusterBase::SetScalingMatrix( Eigen::Matrix2d matrix )
{
    if(matrix.rows() == 1 || matrix.cols()==1)
    {
        this->scalingMatrix = MatrixHelper::CreateScalingMatrix(matrix(0,0));
    }
    else
    {
        this->scalingMatrix = matrix;
    }
}
</code>```

It's quite strange because of the fact that previously I used MatrixXd instead of Vector2d and Matrix2d and everything was fine. What is more this is common problem when using stl:vector - however as You can see this functions doesn't take as a parameter stl:vector.

What can I do to fix this?

答:
Compiler error C2719 has nothing to do with STL, it is telling you that you are not allowed to use the 'align' __declspec modifier on formal parameter declarations.

To fix your problem, you need to declare your functions without using __declspec(align (...)).  Of course you aren't explicitly using __declspec so really you need to figure out how/why it is being used on your behalf.

A good place to start might be the definition of Eigen::Matrix2d.

Body:
I have some problems with compiling my code. There are couple of functions which can't be compiled because of error C2719 - formal parameter with __declspec(align('16')) won't be aligned.

Functions which VisualStudio can't compile look like that

Eigen::Matrix2d AlgorithmBase::ReverseTransform(Eigen::Vector2d point, Eigen::Vector2d *translation, Eigen::Matrix2d *scaling, double phi, Eigen::Matrix2d *share)
{
    Eigen::Matrix2d reversedScaling;
    reversedScaling(0,0) = 1/(*scaling)(0,0);
    reversedScaling(0,1) = reversedScaling(1,0) = 0;
    reversedScaling(1,1) = 1/(*scaling)(1,1);
    Eigen::MatrixXd  newTranslation = -1**translation;

    return  MatrixHelper::CreateRotationMatrix(-phi)* *scaling*point + newTranslation;
}

void TemplateClusterBase::SetScalingMatrix( Eigen::Matrix2d matrix )
{
    if(matrix.rows() == 1 || matrix.cols()==1)
    {
        this->scalingMatrix = MatrixHelper::CreateScalingMatrix(matrix(0,0));
    }
    else
    {
        this->scalingMatrix = matrix;
    }
}

It's quite strange because of the fact that previously I used MatrixXd instead of Vector2d and Matrix2d and everything was fine. What is more this is common problem when using stl:vector - however as You can see this functions doesn't take as a parameter stl:vector.

What can I do to fix this?


问:
Tensorflow Federated Image Classification Example #Epochs has major effect. Is the model overfitting?
I've been trying to characterize the learning process (accuracy and loss) on the Federated Learning for Image Classification notebook tutorial with TF Federated.

I'm seeing major improvements in speed of convergence by modifying the epoch hyperparameter.
Changing epochs from 5, 10, 20 etc. But I'm also seeing major increase in training accuracy.
I suspect overfitting is occurring, though then I evaluate on the test set accuracy is still high.

Wondering what is going on. ?

My understanding is that the epoch param controls the # of forward/back prop on each client per round of training. Is this correct ?
So ie 10 rounds of training on 10 clients with 10 epochs would be 10 Epochs X 10 Clients X 10 rounds.
Realise a lager range of clients is needed etc but I was expecting to see poorer accuracy on the test set.

What can I do to see whats going on. Could I use the evaluation check with something like learning curves to to see if overfitting is occurring ?

test_metrics = evaluation(state.model, federated_test_data)
Only appears to give a single data point, how can I get the individual test accuracy for each test example validated?

答:
Increasing the number of client epochs can indeed increase per-round convergence rate; but you're absolutely right that there is a risk of overfitting.

In the Federated Averaging algorithm, the number of client epochs determines the amount of "sequential progress" (or learning) each client makes before updating the global model. More epochs will result in more local progress each round, this can manifest as a much faster per-round convergence rate. Plotting this against the number of examples seen on all clients may instead show a more similar convergence rate however.

In the federated optimization setting, there is a new risk of overfitting that may be correlated to how non-IID each client dataset is. If each client dataset has the same distribution as the global data distribution, the same practices used for non-federated optimization can be used. The less similar each client dataset is to the "global" dataset, the more likely there will be "drift" (clients converge to different optimal points) when using a high number of client epochs during later rounds. Training accuracy can still appear high in this setting, as each client is fitting to its own local data well during local training. However test accuracy is less likely to improve, as the global model average likely will average out to be very small (the different client-local optimal points cancelling each other out). Praneeth et. al has some discussion about this.


问:
How to fix 'can't open file 'pip': [Errno 2] No such file or directory' when installing gekko
I'm trying to install the gekko module in my python terminal using

```<code>python install -m pip gekko
</code>```

but it is throwing an error not recognizing pip:

```<code>can't open file 'pip': [Errno 2] No such file or directory.
</code>```

I'm using the terminal in Pycharm with Python 3.7

答:
I was facing the same problem, but

$python -m pip install gekko

worked for me.

Body:
I'm trying to install the gekko module in my python terminal using

python install -m pip gekko

but it is throwing an error not recognizing pip:

can't open file 'pip': [Errno 2] No such file or directory.

I'm using the terminal in Pycharm with Python 3.7


问:
Jackson JsonNode with empty element key
I am using jackson-dataformat-xml (2.9) to parse an XML into JsonNode and then parse it to JSON
(the XML is very dynamic so that is why I am using JsonNode instead of binding to a POJO. e.g 'elementName' and 'id' names may vary).

It happens that during the JSON parsing phase, one of the element keys is empty string ("").

XML:

```<code><elementName>
      <id type="pid">abcdef123</id>
</elementName>
</code>```

Parsing logic:

```<code>public Parser() {
        ObjectMapper jsonMapper = new ObjectMapper();
        XmlMapper xmlMapper = new XmlMapper(new XmlFactory(new WstxInputFactory()));
}

public InputStream parseXmlResponse(InputStream xmlStream) {
        InputStream stream = null;

        try {
            JsonNode node = xmlMapper.readTree(xmlStream);
            stream = new ByteArrayInputStream(jsonMapper.writer().writeValueAsBytes(node));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stream;
    }
</code>```

Json:

Result:

```<code>{
   "elementName": {
     "id": {
        "type": "pid",
        "": "abcdef123"
     }
   },
}
</code>```

Expected:

```<code>{
   "elementName": {
     "id": {
        "type": "pid",
        "value": "abcdef123"
     }
   },
}
</code>```

My idea is to find whenever I have the empty key "" and replace it with "value". Either at XML de-serialization or during JSON serialization.
I have tried to use default serializer, filter, but haven't got it working in a nice and concise way.

Suggestions are much appreciated.

Thank you for the help.

Possible Solution:

Based on @shoek suggestion I decided to write a custom serializer to avoid creating an intermediate object (ObjectNode) during the process.

edit: refactor based on the same solution proposed by @shoek.

```<code>public class CustomNode {
    private JsonNode jsonNode;

    public CustomNode(JsonNode jsonNode) {
        this.jsonNode = jsonNode;
    }

    public JsonNode getJsonNode() {
        return jsonNode;
    }
}

public class CustomObjectsResponseSerializer extends StdSerializer<CustomNode> {

    protected CustomObjectsResponseSerializer() {
        super(CustomNode.class);
    }

    @Override
    public void serialize(CustomNode node, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        convertObjectNode(node.getJsonNode(), jgen, provider);
    }

    private void convertObjectNode(JsonNode node, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        jgen.writeStartObject();
        for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) {
            String childName = it.next();
            JsonNode childNode = node.get(childName);
            // XML parser returns an empty string as value name. Replacing it with "value"
            if (Objects.equals("", childName)) {
                childName = "value";
            }

            if (childNode instanceof ArrayNode) {
                jgen.writeFieldName(childName);
                convertArrayNode(childNode, jgen, provider);
            } else if (childNode instanceof ObjectNode) {
                jgen.writeFieldName(childName);
                convertObjectNode(childNode, jgen, provider);
            } else {
                provider.defaultSerializeField(childName, childNode, jgen);
            }
        }
        jgen.writeEndObject();

    }

    private void convertArrayNode(JsonNode node, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        jgen.writeStartArray();
        for (Iterator<JsonNode> it = node.elements(); it.hasNext(); ) {
            JsonNode childNode = it.next();

            if (childNode instanceof ArrayNode) {
                convertArrayNode(childNode, jgen, provider);
            } else if (childNode instanceof ObjectNode) {
                convertObjectNode(childNode, jgen, provider);
            } else {
                provider.defaultSerializeValue(childNode, jgen);
            }
        }
        jgen.writeEndArray();
    }
}
</code>```

答:
Serializer version.</p>

```<code>package com.example;

import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.module.SimpleSerializers;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

public class Stackoverflow62009220_B {
    public static void main(String[] args) throws JsonProcessingException {
        // see https://www.baeldung.com/jackson-call-default-serializer-from-custom-serializer

        convert("{\"elementName\":{\"id\":{\"type\":\"pid\",\"\":\"abcdef123\"}}}");

        // j =  {"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,5],"obj":{"a":"A","b":22}}
        // (simple json object)
        String j = "{\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,5],\"obj\":{\"a\":\"A\",\"b\":22}}";
        convert(j);

        // g = {"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,{"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,5],"obj":{"a":"A","b":22}}],"obj":{"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,5],"obj":{"a":"A","b":22}}}
        // (includes an array containing object j, and an object j containing array)
        String g = " {\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,{\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,5],\"obj\":{\"a\":\"A\",\"b\":22}}],\"obj\":{\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,5],\"obj\":{\"a\":\"A\",\"b\":22}}}";
        convert(g);
    }

    private static void convert(String str) throws JsonProcessingException {
        JsonNode input = (new ObjectMapper()).readTree(str);
        System.out.println("in:");
        System.out.println(input);

        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        SimpleSerializers serializers = new SimpleSerializers();
        serializers.addSerializer(ObjectNode.class, new MyObjectNodeSerializer());
        module.setSerializers(serializers);
        mapper.registerModule(module);

        String output = mapper.writer().writeValueAsString(input);
        System.out.println("out:");
        System.out.println(output);
        System.out.println("----------");
    }
}

class MyObjectNodeSerializer extends StdSerializer<ObjectNode> {

    public MyObjectNodeSerializer() {
        super(ObjectNode.class);
    }

    public static MyObjectNodeSerializer create() {
        return new MyObjectNodeSerializer();
    }

    @Override
    public void serialize(ObjectNode value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        for (Iterator<String> it = value.fieldNames(); it.hasNext();) {
            String childName = it.next();
            JsonNode childNode = value.get(childName);

            if (Objects.equals("", childName)) {
                childName = "value";
            }

            if (childNode instanceof ArrayNode) {
                gen.writeFieldName(childName);
                MyArrayNodeSerializer.create().serialize((ArrayNode) childNode, gen, provider);
            } else if (childNode instanceof ObjectNode) {
                gen.writeFieldName(childName);
                this.serialize((ObjectNode) childNode, gen, provider);
            } else {
                provider.defaultSerializeField(childName, childNode, gen);
            }
        }
        gen.writeEndObject();
    }
}

class MyArrayNodeSerializer extends StdSerializer<ArrayNode> {

    public MyArrayNodeSerializer() {
        super(ArrayNode.class);
    }

    public static MyArrayNodeSerializer create() {
        return new MyArrayNodeSerializer();
    }

    @Override
    public void serialize(ArrayNode value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartArray();
        for (Iterator<JsonNode> it = value.elements(); it.hasNext();) {
            JsonNode childNode = it.next();
            if (childNode instanceof ArrayNode) {
                this.serialize((ArrayNode) childNode, gen, provider);
            } else if (childNode instanceof ObjectNode) {
                MyObjectNodeSerializer.create().serialize((ObjectNode) childNode, gen, provider);
            } else {
                provider.defaultSerializeValue(childNode, gen);
            }
        }
        gen.writeEndArray();
    }
}
</code>```

Title: Jackson JsonNode with empty element key

Body:
I am using jackson-dataformat-xml (2.9) to parse an XML into JsonNode and then parse it to JSON
(the XML is very dynamic so that is why I am using JsonNode instead of binding to a POJO. e.g 'elementName' and 'id' names may vary).

It happens that during the JSON parsing phase, one of the element keys is empty string ("").

XML:

<elementName>
      <id type="pid">abcdef123</id>
</elementName>

Parsing logic:

public Parser() {
        ObjectMapper jsonMapper = new ObjectMapper();
        XmlMapper xmlMapper = new XmlMapper(new XmlFactory(new WstxInputFactory()));
}

public InputStream parseXmlResponse(InputStream xmlStream) {
        InputStream stream = null;

        try {
            JsonNode node = xmlMapper.readTree(xmlStream);
            stream = new ByteArrayInputStream(jsonMapper.writer().writeValueAsBytes(node));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stream;
    }

Json:

Result:

{
   "elementName": {
     "id": {
        "type": "pid",
        "": "abcdef123"
     }
   },
}

Expected:

{
   "elementName": {
     "id": {
        "type": "pid",
        "value": "abcdef123"
     }
   },
}

My idea is to find whenever I have the empty key "" and replace it with "value". Either at XML de-serialization or during JSON serialization.
I have tried to use default serializer, filter, but haven't got it working in a nice and concise way.

Suggestions are much appreciated.

Thank you for the help.

Possible Solution:

Based on @shoek suggestion I decided to write a custom serializer to avoid creating an intermediate object (ObjectNode) during the process.

edit: refactor based on the same solution proposed by @shoek.

public class CustomNode {
    private JsonNode jsonNode;

    public CustomNode(JsonNode jsonNode) {
        this.jsonNode = jsonNode;
    }

    public JsonNode getJsonNode() {
        return jsonNode;
    }
}

public class CustomObjectsResponseSerializer extends StdSerializer<CustomNode> {

    protected CustomObjectsResponseSerializer() {
        super(CustomNode.class);
    }

    @Override
    public void serialize(CustomNode node, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        convertObjectNode(node.getJsonNode(), jgen, provider);
    }

    private void convertObjectNode(JsonNode node, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        jgen.writeStartObject();
        for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) {
            String childName = it.next();
            JsonNode childNode = node.get(childName);
            // XML parser returns an empty string as value name. Replacing it with "value"
            if (Objects.equals("", childName)) {
                childName = "value";
            }

            if (childNode instanceof ArrayNode) {
                jgen.writeFieldName(childName);
                convertArrayNode(childNode, jgen, provider);
            } else if (childNode instanceof ObjectNode) {
                jgen.writeFieldName(childName);
                convertObjectNode(childNode, jgen, provider);
            } else {
                provider.defaultSerializeField(childName, childNode, jgen);
            }
        }
        jgen.writeEndObject();

    }

    private void convertArrayNode(JsonNode node, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        jgen.writeStartArray();
        for (Iterator<JsonNode> it = node.elements(); it.hasNext(); ) {
            JsonNode childNode = it.next();

            if (childNode instanceof ArrayNode) {
                convertArrayNode(childNode, jgen, provider);
            } else if (childNode instanceof ObjectNode) {
                convertObjectNode(childNode, jgen, provider);
            } else {
                provider.defaultSerializeValue(childNode, jgen);
            }
        }
        jgen.writeEndArray();
    }
}

Body:
Serializer version.

package com.example;

import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.module.SimpleSerializers;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

public class Stackoverflow62009220_B {
    public static void main(String[] args) throws JsonProcessingException {
        // see https://www.baeldung.com/jackson-call-default-serializer-from-custom-serializer

        convert("{\"elementName\":{\"id\":{\"type\":\"pid\",\"\":\"abcdef123\"}}}");

        // j =  {"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,5],"obj":{"a":"A","b":22}}
        // (simple json object)
        String j = "{\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,5],\"obj\":{\"a\":\"A\",\"b\":22}}";
        convert(j);

        // g = {"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,{"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,5],"obj":{"a":"A","b":22}}],"obj":{"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,5],"obj":{"a":"A","b":22}}}
        // (includes an array containing object j, and an object j containing array)
        String g = " {\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,{\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,5],\"obj\":{\"a\":\"A\",\"b\":22}}],\"obj\":{\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,5],\"obj\":{\"a\":\"A\",\"b\":22}}}";
        convert(g);
    }

    private static void convert(String str) throws JsonProcessingException {
        JsonNode input = (new ObjectMapper()).readTree(str);
        System.out.println("in:");
        System.out.println(input);

        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        SimpleSerializers serializers = new SimpleSerializers();
        serializers.addSerializer(ObjectNode.class, new MyObjectNodeSerializer());
        module.setSerializers(serializers);
        mapper.registerModule(module);

        String output = mapper.writer().writeValueAsString(input);
        System.out.println("out:");
        System.out.println(output);
        System.out.println("----------");
    }
}

class MyObjectNodeSerializer extends StdSerializer<ObjectNode> {

    public MyObjectNodeSerializer() {
        super(ObjectNode.class);
    }

    public static MyObjectNodeSerializer create() {
        return new MyObjectNodeSerializer();
    }

    @Override
    public void serialize(ObjectNode value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        for (Iterator<String> it = value.fieldNames(); it.hasNext();) {
            String childName = it.next();
            JsonNode childNode = value.get(childName);

            if (Objects.equals("", childName)) {
                childName = "value";
            }

            if (childNode instanceof ArrayNode) {
                gen.writeFieldName(childName);
                MyArrayNodeSerializer.create().serialize((ArrayNode) childNode, gen, provider);
            } else if (childNode instanceof ObjectNode) {
                gen.writeFieldName(childName);
                this.serialize((ObjectNode) childNode, gen, provider);
            } else {
                provider.defaultSerializeField(childName, childNode, gen);
            }
        }
        gen.writeEndObject();
    }
}

class MyArrayNodeSerializer extends StdSerializer<ArrayNode> {

    public MyArrayNodeSerializer() {
        super(ArrayNode.class);
    }

    public static MyArrayNodeSerializer create() {
        return new MyArrayNodeSerializer();
    }

    @Override
    public void serialize(ArrayNode value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartArray();
        for (Iterator<JsonNode> it = value.elements(); it.hasNext();) {
            JsonNode childNode = it.next();
            if (childNode instanceof ArrayNode) {
                this.serialize((ArrayNode) childNode, gen, provider);
            } else if (childNode instanceof ObjectNode) {
                MyObjectNodeSerializer.create().serialize((ObjectNode) childNode, gen, provider);
            } else {
                provider.defaultSerializeValue(childNode, gen);
            }
        }
        gen.writeEndArray();
    }
}


问:
Using scipy's newton function to solve an equation
I've been using scipy's 'newton' function for a few months, and I've come across a rather confusing situation. One of my plots, involving the following code:

```<code>#Declaring frequency and wave vector arrays
w_array = np.linspace(0.1,3,1000)*10**15
k_array = np.array([])

#Declaring variables
p = 2.85*10**15
e1=1
c=300000000

def twolayer(k):
    e2 = 3.43-(p**2)/(w**2 - (1j*gamma*w))
    result = ((w/c)*(((e2*e1)/(e2+e1))**0.5))-k
    return result

#Loop that uses secant method for each value of omega to find solutions for k
for w in w_array:
    gamma = 2.23 * 10**14
    k_array = np.append(k_array,newton(twolayer,0))

print(twolayer(0))

#Plotting
plt.figure(dpi=100)
plt.scatter(k_array.real,w_array)
plt.xlabel('Wave vector k')
plt.ylabel('Frequency ω')
</code>```

Which outputs the following graph:

But i'm a little confused what the 'newton' function is actually doing here. As I haven't supplied a derivative, it undergoes the Secant method, but what process allows it to solve a function that has multiple y-values for a given x-value?

答:
You are just plotting it the wrong way! In your computation, k is a function of w.

Try:

```<code>#Plotting
plt.figure(dpi=100)
plt.scatter(w_array, k_array.real)
plt.xlabel('Frequency ω')
plt.ylabel('Wave vector k')
</code>```

gives

Body:
I've been using scipy's 'newton' function for a few months, and I've come across a rather confusing situation. One of my plots, involving the following code:

#Declaring frequency and wave vector arrays
w_array = np.linspace(0.1,3,1000)*10**15
k_array = np.array([])

#Declaring variables
p = 2.85*10**15
e1=1
c=300000000

def twolayer(k):
    e2 = 3.43-(p**2)/(w**2 - (1j*gamma*w))
    result = ((w/c)*(((e2*e1)/(e2+e1))**0.5))-k
    return result

#Loop that uses secant method for each value of omega to find solutions for k
for w in w_array:
    gamma = 2.23 * 10**14
    k_array = np.append(k_array,newton(twolayer,0))

print(twolayer(0))

#Plotting
plt.figure(dpi=100)
plt.scatter(k_array.real,w_array)
plt.xlabel('Wave vector k')
plt.ylabel('Frequency ω')

Which outputs the following graph:

But i'm a little confused what the 'newton' function is actually doing here. As I haven't supplied a derivative, it undergoes the Secant method, but what process allows it to solve a function that has multiple y-values for a given x-value?

Body:
You are just plotting it the wrong way! In your computation, k is a function of w.

Try:

#Plotting
plt.figure(dpi=100)
plt.scatter(w_array, k_array.real)
plt.xlabel('Frequency ω')
plt.ylabel('Wave vector k')

gives


问:
Django not create database table
Hey,
I've a database already created. Now I've updated UserProfile with:

```<code>class UserProfile(models.Model):
    user = models.ForeignKey(User, unique = True, related_name = 'user')
    follows = models.ManyToManyField("self", related_name = 'follows') <-- NEW LINE
</code>```

so python manage.py sqlall myapp returns me:

```<code>[...]
CREATE TABLE "myapp_userprofile_follows" (
    "id" integer NOT NULL PRIMARY KEY,
    "from_userprofile_id" integer NOT NULL,
    "to_userprofile_id" integer NOT NULL,
    UNIQUE ("from_userprofile_id", "to_userprofile_id")
)
[...]
</code>```

When I run python manage.py syncdb:

```<code>Creating tables ...
Installing custom SQL ...
Installing indexes ...
No fixtures found.
</code>```

But the table is not created when I try to insert data into. Why? (I'm testing locally, with sqlite3)

答: manage.py syncdb will not modify existing tables to add or remove fields. You will need to either manually modify your database, or use a tool like South  to create automated database migrations (which is what I highly recommend)

Title: Django not create database table

Body:
Hey,
I've a database already created. Now I've updated UserProfile with:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique = True, related_name = 'user')
    follows = models.ManyToManyField("self", related_name = 'follows') <-- NEW LINE

so python manage.py sqlall myapp returns me:

[...]
CREATE TABLE "myapp_userprofile_follows" (
    "id" integer NOT NULL PRIMARY KEY,
    "from_userprofile_id" integer NOT NULL,
    "to_userprofile_id" integer NOT NULL,
    UNIQUE ("from_userprofile_id", "to_userprofile_id")
)
[...]

When I run python manage.py syncdb:

Creating tables ...
Installing custom SQL ...
Installing indexes ...
No fixtures found.

But the table is not created when I try to insert data into. Why? (I'm testing locally, with sqlite3)


问:
Scala "belongsTo" function
I am often in the situation of doing this type of filtering:

```<code>allPeople
  .filter(person => englishPeopleIds contains person.id)

</code>```

and it would make my life easier and my code more readable if there was some sort of "belongsTo" function to do the following:

```<code>allPeople
  .filter(_.id belongsTo englishPeopleIds)
</code>```

belongsTo function would have this kind of behaviour (but would be a method of the element):

```<code>def belongsTo[T](element: T, list: List[T]): Boolean = list contains element
</code>```

Do you know if such function is already implemented in Scala?

答:
No, but you could use an implicit class to do the same:

```<code>implicit class BelongsTo[T](private val t: T) extends AnyVal {
  def belongsTo(s: Seq[T]): Boolean = s.contains(t)
  def belongsTo(s: Set[T]): Boolean = s(t)
}
</code>```

In Dotty, you can do this:

```<code>extension [T](t: T):
  def belongsTo(s: Seq[T]): Boolean = s.contains(t)
  def belongsTo(s: Set[T]): Boolean = s(t)
</code>```

Honestly, though, it doesn't seem worth it.

Title: Scala "belongsTo" function

Body:
I am often in the situation of doing this type of filtering:

allPeople
  .filter(person => englishPeopleIds contains person.id)

and it would make my life easier and my code more readable if there was some sort of "belongsTo" function to do the following:

allPeople
  .filter(_.id belongsTo englishPeopleIds)

belongsTo function would have this kind of behaviour (but would be a method of the element):

def belongsTo[T](element: T, list: List[T]): Boolean = list contains element

Do you know if such function is already implemented in Scala?

Body:
No, but you could use an implicit class to do the same:

implicit class BelongsTo[T](private val t: T) extends AnyVal {
  def belongsTo(s: Seq[T]): Boolean = s.contains(t)
  def belongsTo(s: Set[T]): Boolean = s(t)
}

In Dotty, you can do this:

extension [T](t: T):
  def belongsTo(s: Seq[T]): Boolean = s.contains(t)
  def belongsTo(s: Set[T]): Boolean = s(t)

Honestly, though, it doesn't seem worth it.


问:
How to change value in a nested YAML record using yq
I am trying to change the bind_host: localhost to bind_host: 0.0.0.0 where the bind_host is the 3rd layer nested value of YAML file
Current value of File

```<code>server:
  application_connectors:
  - type: http
    port: 8989
    bind_host: localhost
  request_log:
      appenders: []
  admin_connectors:
  - type: http
    port: 8990
    bind_host: localhost
</code>```

Expected Output

```<code>server:
  application_connectors:
  - type: http
    port: 8989
    bind_host: 0.0.0.0
  request_log:
      appenders: []
  admin_connectors:
  - type: http
    port: 8990
    bind_host: 0.0.0.0
</code>```

I am trying

```<code>awk '
/:$/{
  flag=""
}
/server/{
  flag=1
}
flag && NF && (/bind_host:/){
  match($0,/^[[:space:]]+/);
  val=substr($0,RSTART,RLENGTH);
  $NF="0.0.0.0";
  print val $0;
  next
}
1
'   config.yml
</code>```

EDIT: Adding image based on answer of @inian

Second Image

答:
Running

<input.yaml yq -y '((.server.application_connectors[].bind_host| select(.) ) |= gsub("localhost";"0.0.0.0") )' | \
yq -y '((.server.admin_connectors[].bind_host| select(.) ) |= gsub("localhost";"0.0.0.0") )'
```

<p>you have</p>

<pre class="lang-yaml prettyprint-override"><code>server:
  application_connectors:
    - type: http
      port: 8989
      bind_host: 0.0.0.0
  request_log:
    appenders: []
  admin_connectors:
    - type: http
      port: 8990
      bind_host: 0.0.0.0
</code>```

Body:
I am trying to change the bind_host: localhost to bind_host: 0.0.0.0 where the bind_host is the 3rd layer nested value of YAML file
Current value of File

server:
  application_connectors:
  - type: http
    port: 8989
    bind_host: localhost
  request_log:
      appenders: []
  admin_connectors:
  - type: http
    port: 8990
    bind_host: localhost

Expected Output

server:
  application_connectors:
  - type: http
    port: 8989
    bind_host: 0.0.0.0
  request_log:
      appenders: []
  admin_connectors:
  - type: http
    port: 8990
    bind_host: 0.0.0.0

I am trying

awk '
/:$/{
  flag=""
}
/server/{
  flag=1
}
flag && NF && (/bind_host:/){
  match($0,/^[[:space:]]+/);
  val=substr($0,RSTART,RLENGTH);
  $NF="0.0.0.0";
  print val $0;
  next
}
1
'   config.yml

EDIT: Adding image based on answer of @inian

Second Image

Body:
Running

<input.yaml yq -y '((.server.application_connectors[].bind_host| select(.) ) |= gsub("localhost";"0.0.0.0") )' | \
yq -y '((.server.admin_connectors[].bind_host| select(.) ) |= gsub("localhost";"0.0.0.0") )'

you have

server:
  application_connectors:
    - type: http
      port: 8989
      bind_host: 0.0.0.0
  request_log:
    appenders: []
  admin_connectors:
    - type: http
      port: 8990
      bind_host: 0.0.0.0


问:
java language detection LangDetectException
Working on language detection in java, i try to use langdetect library but i got this error when running Exception in thread "main" com.cybozu.labs.langdetect.LangDetectException: need to load profiles. Could someone help me to add profile? i don't know it can look like.

regards,

答: you can download the profile from: Profiles can be downloaded from : https://language-detection.googlecode.com/files/langdetect-09-13-2011.zip</p>


问:
Power BI using $expand on columns
Power BI newbie here.

I wanted to build a Power BI visualization as shown in the sample report based on my Azure DevOps data (https://learn.microsoft.com/en-us/azure/devops/report/powerbi/sample-test-plans-execution-trend?view=azure-devops&tabs=powerbi).

Making the X-axis with DateSK seems not working as PowerBI treating it as a whole number and the way of X-axis in my visualization is taking it linearly (the plotted X-axis like this: 20200100 20200150 20200200 20200250 and so on). I wanted to have a view of day-by-day as shown in the Microsoft docs.

PowerBI also do not allow me to convert the DateSK data type from whole number into date format.

I have tried using $expand in my Power BI query but it seems the date column couldn't let me expand or I did it wrongly.

Please let me know if you have any workaround or ways to query using the $expand. I want to have a view of expanded Date format instead of DateSK.

Below is the reference from Microsoft docs as shown in the link above:

```<code>let
    Source = OData.Feed ("https://analytics.dev.azure.com/{organization}/{project}/_odata/v3.0-preview/TestPointHistorySnapshot?"
        &"$apply=filter((TestSuite/TestPlanTitle eq '{testPlanTitle}') and (DateSK ge {startDate} and DateSK le {endDate}))"
        &"/groupby("
            &"(DateSK),"
            &"aggregate("
                &"$count as TotalCount,"
                &"cast(ResultOutcome  eq 'Passed', Edm.Int32) with sum as Passed,"
                &"cast(ResultOutcome  eq 'Failed', Edm.Int32) with sum as Failed,"
                &"cast(ResultOutcome eq 'Blocked', Edm.Int32) with sum as Blocked,"
                &"cast(ResultOutcome eq 'NotApplicable', Edm.Int32) with sum as NotApplicable,"
                &"cast(ResultOutcome eq 'None', Edm.Int32) with sum as NotExecuted,"
                &"cast(ResultOutcome ne 'None', Edm.Int32) with sum as Executed
            )
        )", null, [Implementation="2.0"])
in
    Source

</code>```

答:
I achieved this by converting the column twice, first to text, then to date.

Below I am showing the query changes from the advanced editor, but I made the column conversions in the visual query editor.  I convert the counts to whole numbers, and the dateSK to text in the first step.

In the second step I Convert the Date column to a date, then rename the column to Date from DateSK.

```<code>    #"Changed Type" = Table.TransformColumnTypes(Source,{{"TotalCount", Int64.Type}, {"Passed", Int64.Type}, {"Failed", Int64.Type}, {"Blocked", Int64.Type}, {"NotApplicable", Int64.Type}, {"NotExecuted", Int64.Type}, {"Executed", Int64.Type}, {"DateSK", type text}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"DateSK", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"DateSK", "Date"}}),
</code>```

Title: Power BI using $expand on columns

Body:
Power BI newbie here.

I wanted to build a Power BI visualization as shown in the sample report based on my Azure DevOps data (https://learn.microsoft.com/en-us/azure/devops/report/powerbi/sample-test-plans-execution-trend?view=azure-devops&tabs=powerbi).

Making the X-axis with DateSK seems not working as PowerBI treating it as a whole number and the way of X-axis in my visualization is taking it linearly (the plotted X-axis like this: 20200100 20200150 20200200 20200250 and so on). I wanted to have a view of day-by-day as shown in the Microsoft docs.

PowerBI also do not allow me to convert the DateSK data type from whole number into date format.

I have tried using $expand in my Power BI query but it seems the date column couldn't let me expand or I did it wrongly.

Please let me know if you have any workaround or ways to query using the $expand. I want to have a view of expanded Date format instead of DateSK.

Below is the reference from Microsoft docs as shown in the link above:

let
    Source = OData.Feed ("https://analytics.dev.azure.com/{organization}/{project}/_odata/v3.0-preview/TestPointHistorySnapshot?"
        &"$apply=filter((TestSuite/TestPlanTitle eq '{testPlanTitle}') and (DateSK ge {startDate} and DateSK le {endDate}))"
        &"/groupby("
            &"(DateSK),"
            &"aggregate("
                &"$count as TotalCount,"
                &"cast(ResultOutcome  eq 'Passed', Edm.Int32) with sum as Passed,"
                &"cast(ResultOutcome  eq 'Failed', Edm.Int32) with sum as Failed,"
                &"cast(ResultOutcome eq 'Blocked', Edm.Int32) with sum as Blocked,"
                &"cast(ResultOutcome eq 'NotApplicable', Edm.Int32) with sum as NotApplicable,"
                &"cast(ResultOutcome eq 'None', Edm.Int32) with sum as NotExecuted,"
                &"cast(ResultOutcome ne 'None', Edm.Int32) with sum as Executed
            )
        )", null, [Implementation="2.0"])
in
    Source

Body:
I achieved this by converting the column twice, first to text, then to date.

Below I am showing the query changes from the advanced editor, but I made the column conversions in the visual query editor.  I convert the counts to whole numbers, and the dateSK to text in the first step.

In the second step I Convert the Date column to a date, then rename the column to Date from DateSK.

    #"Changed Type" = Table.TransformColumnTypes(Source,{{"TotalCount", Int64.Type}, {"Passed", Int64.Type}, {"Failed", Int64.Type}, {"Blocked", Int64.Type}, {"NotApplicable", Int64.Type}, {"NotExecuted", Int64.Type}, {"Executed", Int64.Type}, {"DateSK", type text}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"DateSK", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"DateSK", "Date"}}),


问:
What is the most modern way to log to syslog using a java.util.logging handler?
I am specifically looking for the most up to date, modern SysLogHandler for java.util.logging.

I have found a few that date back to 2001 - 2003, mostly un-supported now.

I know that syslog is a pretty static service, I am wondering before I write something myself, if there are any newer handler implementations that support the Java 1.5 and newer features.

I am not interested in any of the other logging frameworks or wrapper / proxy libraries.

I am not looking for SLF4J or any other alternative logging frameworks, as suggested in this question.

答: In an old job, I used the Syslog4j library and wired up a custom Log4j-appender for it, so you can probably do something similar by implementing a custom java.util.logging.Handler that passes messages along.


问:
Filter an array taking into account that the filtered options are optional
I have a code that should filter the array depending on some condition. One time it could be 1 condition or 2, depending on how many user are inserted.

const arr = [
  {
    name: 'John',
    age: 44,
    old: 'No'
  },
  {
    name: 'John',
    age: 4,
    old: 'No'
  },
  {
    name: 'Bill',
    age: 47,
    old: 'No'
  },
]
const filteredData = [4, "John", "No"];

var find = arr.filter(function(result) {
  return filteredData.includes(result.name) && filteredData.includes(result.age) && filteredData.includes(result.old);
})

console.log(find)```
</div>
</div>
</p>

<p>In this case the code works, but if I add just one value in <code>const filteredData = [4, "John", "No" ];</code>, like: <code>const filteredData = ["John"];</code>, I should get:</p>

```[
  {
    name: 'John',
    age: 44,
    old: 'No'
  },
  {
    name: 'John',
    age: 4,
    old: 'No'
  },
]
```

...but now I don't get anything, because in filteredData all 3 conditions should be mandatory. How to change the code to make the number of condition optional?

答:
What about this?

It's clearer to express what you want to filter using an object as a filter. If you only pass the name: 'John':

const arr = [
  {
    name:'John',
    age:44,
    old:'No'
  },
   {
    name:'John',
    age:4,
    old:'No'
  },
  {
    name:'Bill',
    age:47,
    old:'No'
  },
]
const filteredData = { names: ["John"] };

var find = arr.filter(function(result) {
return (!filteredData.names || filteredData.names.includes(result.name))
  && (!filteredData.ages || filteredData.ages.includes(result.age))
  && (!filteredData.olds || filteredData.olds.includes(result.old));
})

console.log(find)```
</div>
</div>
</p>

<p>If you also pass the age:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>const arr = [
  {
    name:'John',
    age:44,
    old:'No'
  },
   {
    name:'John',
    age:4,
    old:'No'
  },
  {
    name:'Bill',
    age:47,
    old:'No'
  },
]
const filteredData = { names: ["John"], ages: [4] };

var find = arr.filter(function(result) {
return (!filteredData.names || filteredData.names.includes(result.name))
  && (!filteredData.ages || filteredData.ages.includes(result.age))
  && (!filteredData.olds || filteredData.olds.includes(result.old));
})

console.log(find)</code>```

Also, this way you can filter more than one name (or age) at the same time. If you only care for a value at a time for each field, you could just use a plain object without arrays and adapt the code accordingly:

{ name: "John", age: 4 }

In any case, it is important that you can differentiate the different fields (name, age, old), otherwise it could lead to unexpected behavior when searching (given you can't tell which values are exclusive and which inclusive), as you faced it already.

Body:
I have a code that should filter the array depending on some condition. One time it could be 1 condition or 2, depending on how many user are inserted.

const arr = [
  {
    name: 'John',
    age: 44,
    old: 'No'
  },
  {
    name: 'John',
    age: 4,
    old: 'No'
  },
  {
    name: 'Bill',
    age: 47,
    old: 'No'
  },
]
const filteredData = [4, "John", "No"];

var find = arr.filter(function(result) {
  return filteredData.includes(result.name) && filteredData.includes(result.age) && filteredData.includes(result.old);
})

console.log(find)

In this case the code works, but if I add just one value in const filteredData = [4, "John", "No" ];, like: const filteredData = ["John"];, I should get:

[
  {
    name: 'John',
    age: 44,
    old: 'No'
  },
  {
    name: 'John',
    age: 4,
    old: 'No'
  },
]

...but now I don't get anything, because in filteredData all 3 conditions should be mandatory. How to change the code to make the number of condition optional?

Body:
What about this?

It's clearer to express what you want to filter using an object as a filter. If you only pass the name: 'John':

const arr = [
  {
    name:'John',
    age:44,
    old:'No'
  },
   {
    name:'John',
    age:4,
    old:'No'
  },
  {
    name:'Bill',
    age:47,
    old:'No'
  },
]
const filteredData = { names: ["John"] };

var find = arr.filter(function(result) {
return (!filteredData.names || filteredData.names.includes(result.name))
  && (!filteredData.ages || filteredData.ages.includes(result.age))
  && (!filteredData.olds || filteredData.olds.includes(result.old));
})

console.log(find)

If you also pass the age:

const arr = [
  {
    name:'John',
    age:44,
    old:'No'
  },
   {
    name:'John',
    age:4,
    old:'No'
  },
  {
    name:'Bill',
    age:47,
    old:'No'
  },
]
const filteredData = { names: ["John"], ages: [4] };

var find = arr.filter(function(result) {
return (!filteredData.names || filteredData.names.includes(result.name))
  && (!filteredData.ages || filteredData.ages.includes(result.age))
  && (!filteredData.olds || filteredData.olds.includes(result.old));
})

console.log(find)

Also, this way you can filter more than one name (or age) at the same time. If you only care for a value at a time for each field, you could just use a plain object without arrays and adapt the code accordingly:

{ name: "John", age: 4 }

In any case, it is important that you can differentiate the different fields (name, age, old), otherwise it could lead to unexpected behavior when searching (given you can't tell which values are exclusive and which inclusive), as you faced it already.


问:
Howto get total duration of a Powerpoint file
I need to have the functionality in my delphi application to get the total duration time of a given Powerpoint-File (pptx format).

Anybody who can get me going?

Thanks a lot already in advance!

答:
one simpler solution (hack) without using ole and without installing powerpoint:

change the pptx extension to .zip (powerpoint files are a bunch of files compressed under the ppts extension) and read all the info you want from the file  ppt/PRESENTATION.xml

done.


问:
Libgdx center Scaled Textbutton in HorizontalGroup
Putting TextBttons into a HorizontalGroup, then scaling them results in a distorted view, even with centered alignment.

add_button = new TextButton("+",textButtonStyle);
add_button.setTransform(true);
add_button.setScale(0.7f);
add_button.setDebug(true);
remove_button = new TextButton("-",textButtonStyle);
remove_button.setTransform(true);
remove_button.setScale(0.7f);
remove_button.setDebug(true);

HorizontalGroup brush_op_group = new HorizontalGroup();
brush_op_group.addActor(remove_button);
brush_op_group.addActor(add_button);
brush_op_group.setDebug(true);

```

which results in the below image:

It seems like the buttons positions are calculated based on their unscaled sizes.
How can this be corrected?

答:
For this purpose I ended up using tables:

add_button = new TextButton("+",textButtonStyle);
add_button.setTransform(true);
add_button.setOrigin(Align.left | Align.top);
add_button.setScale(0.8f);
remove_button = new TextButton("-",textButtonStyle);
remove_button.setTransform(true);
remove_button.setOrigin(Align.right | Align.top);
remove_button.setScale(0.8f);

HorizontalGroup file_op_group = new HorizontalGroup();
Table brush_op_group = new Table().center();
brush_op_group.add(remove_button).right();
brush_op_group.add(add_button).left();
brush_op_group.setDebug(true);
```

Works like a charm!

Body:
Putting TextBttons into a HorizontalGroup, then scaling them results in a distorted view, even with centered alignment.

add_button = new TextButton("+",textButtonStyle);
add_button.setTransform(true);
add_button.setScale(0.7f);
add_button.setDebug(true);
remove_button = new TextButton("-",textButtonStyle);
remove_button.setTransform(true);
remove_button.setScale(0.7f);
remove_button.setDebug(true);

HorizontalGroup brush_op_group = new HorizontalGroup();
brush_op_group.addActor(remove_button);
brush_op_group.addActor(add_button);
brush_op_group.setDebug(true);

which results in the below image:

It seems like the buttons positions are calculated based on their unscaled sizes.
How can this be corrected?

Body:
For this purpose I ended up using tables:

add_button = new TextButton("+",textButtonStyle);
add_button.setTransform(true);
add_button.setOrigin(Align.left | Align.top);
add_button.setScale(0.8f);
remove_button = new TextButton("-",textButtonStyle);
remove_button.setTransform(true);
remove_button.setOrigin(Align.right | Align.top);
remove_button.setScale(0.8f);

HorizontalGroup file_op_group = new HorizontalGroup();
Table brush_op_group = new Table().center();
brush_op_group.add(remove_button).right();
brush_op_group.add(add_button).left();
brush_op_group.setDebug(true);

Works like a charm!


问:
Find closest value in a vector with binary search
As a silly toy example, suppose

```<code>x=4.5
w=c(1,2,4,6,7)
</code>```

I wonder if there is a simple R function that finds the index of the closest match to x in w.  So if foo is that function, foo(w,x) would return 3.  The function match is the right idea, but seems to apply only for exact matches.

Solutions here (e.g. which.min(abs(w - x)), which(abs(w-x)==min(abs(w-x))), etc.) are all O(n) instead of log(n) (I'm assuming that w is already sorted).

答:
Based on @neal-fultz answer, here is a simple function that uses findInterval():

get_closest_index <- function(x, vec){
  # vec must be sorted
  iv <- findInterval(x, vec)
  dist_left <- x - vec[ifelse(iv == 0, NA, iv)]
  dist_right <- vec[iv + 1] - x
  ifelse(! is.na(dist_left) & (is.na(dist_right) | dist_left < dist_right), iv, iv + 1)
}
values <- c(-15, -0.01, 3.1, 6, 10, 100)
grid <- c(-2, -0.1, 0.1, 3, 7)
get_closest_index(values, grid)
#> [1] 1 2 4 5 5 5
```

Created on 2020-05-29 by the reprex package (v0.3.0)

Body:
As a silly toy example, suppose

x=4.5
w=c(1,2,4,6,7)

I wonder if there is a simple R function that finds the index of the closest match to x in w.  So if foo is that function, foo(w,x) would return 3.  The function match is the right idea, but seems to apply only for exact matches.

Solutions here (e.g. which.min(abs(w - x)), which(abs(w-x)==min(abs(w-x))), etc.) are all O(n) instead of log(n) (I'm assuming that w is already sorted).

Body:
Based on @neal-fultz answer, here is a simple function that uses findInterval():

get_closest_index <- function(x, vec){
  # vec must be sorted
  iv <- findInterval(x, vec)
  dist_left <- x - vec[ifelse(iv == 0, NA, iv)]
  dist_right <- vec[iv + 1] - x
  ifelse(! is.na(dist_left) & (is.na(dist_right) | dist_left < dist_right), iv, iv + 1)
}
values <- c(-15, -0.01, 3.1, 6, 10, 100)
grid <- c(-2, -0.1, 0.1, 3, 7)
get_closest_index(values, grid)
#> [1] 1 2 4 5 5 5

Created on 2020-05-29 by the reprex package (v0.3.0)


问:
Set focus on <input> element
I am working a front-end application with Angular 5, and I need to have a search box hidden, but on click of a button, the search box should be displayed and focused.

I have tried a few ways found on StackOverflow with directive or so, but can't succeed.

Here is the sample code:

```<code>@Component({
   selector: 'my-app',
   template: `
    <div>
    <h2>Hello</h2>
    </div>
    <button (click) ="showSearch()">Show Search</button>
    <p></p>
    <form>
      <div >
        <input *ngIf="show" #search type="text"  />
      </div>
    </form>
    `,
  })
  export class App implements AfterViewInit {
  @ViewChild('search') searchElement: ElementRef;

  show: false;
  name:string;
  constructor() {
  }

  showSearch(){
    this.show = !this.show;
    this.searchElement.nativeElement.focus();
    alert("focus");
  }

  ngAfterViewInit() {
    this.firstNameElement.nativeElement.focus();
  }
</code>```

The search box is not set to focus.

How can I do that?

答:
There is also a  DOM attribute called cdkFocusInitial which works for me on inputs.
You can read more about it here: https://material.angular.io/cdk/a11y/overview

Title: Set focus on <input> element

Body:
I am working a front-end application with Angular 5, and I need to have a search box hidden, but on click of a button, the search box should be displayed and focused.

I have tried a few ways found on StackOverflow with directive or so, but can't succeed.

Here is the sample code:

@Component({
   selector: 'my-app',
   template: `

Hello

    <button (click) ="showSearch()">Show Search</button>

    <form>

        <input *ngIf="show" #search type="text"  />

    </form>
    `,
  })
  export class App implements AfterViewInit {
  @ViewChild('search') searchElement: ElementRef;

  show: false;
  name:string;
  constructor() {
  }

  showSearch(){
    this.show = !this.show;
    this.searchElement.nativeElement.focus();
    alert("focus");
  }

  ngAfterViewInit() {
    this.firstNameElement.nativeElement.focus();
  }

The search box is not set to focus.

How can I do that?


问:
Issue with counting upper and lower letters in C
I've written a program which should count upper and lower letters and other signs but it counts anything but when I click Enter and then ^C (EOF). I don't know how to jump over it, hope somebody can help me somehow <3</p>

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

int main()
{
  char ch;
  int uppers = 0, lowers = 0, others = 0;

  while((ch = getchar()) != EOF)
  {
    if(islower(ch))
      lowers++;

    else if(isupper(ch))
      uppers++;

    else
      others++;
  }

  printf("\n\nUpper letters - %d  Lower letters - %d   Others- %d", uppers, lowers, others);

  return 0;
}
</code>```

答:
Ctrl+C sends a SIGINT which normally just terminates your application.

What you need is Ctrl+D, which triggers EOF.

EDIT: Note that on Windows' default shell you may need Enter, Ctrl+Z, Enter (or F6) instead (though Ctrl+Z does something else entirely in Linux shells, sending a SIGSTOP). See this question.

You could also compare against 0xD instead of EOF to catch Enter, or maybe use 0x1B which will catch Esc. This way you avoid the weirdness of how to trigger the end-of-input on different platforms (unless you want to process an input stream).

Also take a look at this comment above as well as this answer which contain important additional info that I was missing!

Body:
I've written a program which should count upper and lower letters and other signs but it counts anything but when I click Enter and then ^C (EOF). I don't know how to jump over it, hope somebody can help me somehow <3

#include <stdio.h>
#include <ctype.h>

int main()
{
  char ch;
  int uppers = 0, lowers = 0, others = 0;

  while((ch = getchar()) != EOF)
  {
    if(islower(ch))
      lowers++;

    else if(isupper(ch))
      uppers++;

    else
      others++;
  }

  printf("\n\nUpper letters - %d  Lower letters - %d   Others- %d", uppers, lowers, others);

  return 0;
}