问:
Convert Stack Exchange Markdown to Github Markdown
Has anyone documented the differences between Stack Exchange Markup and Github Markup?

I'm in the midst of a project to convert Stack Exchange Markdown to Github Markdown. It might be a little more complicated because Jekyll on Github Pages uses a Markdown derivative called "Kramdown".

I've already written some of the conversion in my Python program. For example old SE posts with #Header must be converted to # Header.

Another example are "> Block quote" lines have two spaces appended to the end of the line.

Now it's starting to get tricky (for me at least) where in an image in SE is specified as:

```<code>[![Ubuntu 5 DE.png][1]][1]

**Note:** Blah, blah, blah

  [1]: https://i.stack.imgur.com/MoxHd.jpg

</code>```

It has to be converted to Github image markdown format:

```<code>![Ubuntu 5 DE.png](https://i.stack.imgur.com/MoxHd.jpg)

**Note:** Blah, blah, blah

</code>```

Another example of "footer hyper links" (for lack of a better noun) in Stack Exchange Markdown is:

```<code>- [Jack Master Volume?][1]

The simplest solution then is to install [JackMix][2]:

find listed [here][3].

[this script][4] is where you are heading:

  [1]: https://discourse.ardour.org/t/jack-master-volume/84650
  [2]: http://www.arnoldarts.de/jackmix/.
  [3]: http://jackaudio.org/applications/
  [4]: https://unix.stackexchange.com/questions/374085/lower-or-increase-pulseaudio-volume-on-all-outputs
</code>```

Needs to be converted to Github Markdown format of:

```<code>- [Jack Master Volume?](https://discourse.ardour.org/t/jack-master-volume/84650)

The simplest solution then is to install [JackMix](http://www.arnoldarts.de/jackmix/.):

find listed [here](http://jackaudio.org/applications/).

[this script](https://unix.stackexchange.com/questions/374085/lower-or-increase-pulseaudio-volume-on-all-outputs) is where you are heading:
</code>```

Finally tonight I discovered that in Stack Exchange you can have:

```<code><!-- language: bash -->

    #!/bin/bash
    cat "$Filename.zip" | base64 > "$Filename64"
</code>```

That needs reformatting to Github Markdown like this:

```<code>``` bash
#!/bin/bash
cat "$Filename.zip" | base64 > "$Filename64"
```
</code>```

It gets even more complicated when SE Markdown has:

```<code><!-- language-all: lang-bash -->
</code>```

Or it has this:

```<code>```Some lines of code
some more lines of
code ```
</code>```

An existing Github Repo to convert would be awesome! If not then if someone has documented the differences between Stack Exchange Markup and Github Markup that would be great too.

If this question goes unanswered for a month then I guess I'll be answering it eventually after the trial-error-fix process is finished.

答:
Converting thousands of Stack Exchange Q&A in markdown format isn't as easy
as simply copying them over to GitHub Pages. The python program
stack-to-blog.py was used to convert Stack Exchange posts to
GitHub Pages Posts.
The full stack-to-blog.py program can be accessed on the
Pippim Website repo .

The program automatically:

Creates Jekyll front matter on posts and front matter totals for site.

Selects Stack Exchange Posts based on meeting minimum criteria such as up-votes or accepted answer status.

If self-answered question, the answer is included and not the question.

If self-answered question, the accepted answer alone doesn't qualify. Votes from other are the qualifier.

Initial testing allows selecting small set of random record numbers to convert.

Converts Stack Exchange Markdown formats to GitHub Pages Kramdown Markdown format.

Creates hyperlinks to original Answer in Stack Exchange and Kramdown in GitHub Pages.

Creates search word to URL indices excluding 50% of words like "a", "the", etc. to save space.

Selectively inserts Table of Contents based on minimum criteria settings.

Selectively inserts Section Navigation Buttons for: Top (Top of Page), ToS (Top of Section), ToC (Table of Contents) and Skip (Skip section).

Selectively inserts "Copy Code Block to System Clipboard" button based on lines of code.

Creates HTML with "Top Ten Answers" with the most votes.

Creates powerful nested expandable/collapsible detail/summary HTML for many thousands of tags by post.

Remaps hyperlinks in Stack Exchange Posts to {{ site.title }} website posts if they were converted.

Fixes old broken #header Stack Exchange Markdown.

Converts < block quote Stack Exchange Markdown into what works in Jekyll Kramdown.

Convert Stack Exchange  tags to fenced code block language.

When no fenced code block language is provided, uses shebang language first (if available).

Converts older four-space indented code blocks to fenced code blocks.

Converts Stack Exchange Hyperlinks where the website post title is implied and not explicit.

Prints list of self-answered questions that were not accepted after the mandatory two day wait period.

Prints list of Rouge Syntax Highlighting languages not supported in fenced code blocks.

Prints summary totals when finished.

Full documentation is provided here.

This is what program looks like when running:

Body:
Has anyone documented the differences between Stack Exchange Markup and Github Markup?

I'm in the midst of a project to convert Stack Exchange Markdown to Github Markdown. It might be a little more complicated because Jekyll on Github Pages uses a Markdown derivative called "Kramdown".

I've already written some of the conversion in my Python program. For example old SE posts with #Header must be converted to # Header.

Another example are "> Block quote" lines have two spaces appended to the end of the line.

Now it's starting to get tricky (for me at least) where in an image in SE is specified as:

[![Ubuntu 5 DE.png][1]][1]

**Note:** Blah, blah, blah

  [1]: https://i.stack.imgur.com/MoxHd.jpg

It has to be converted to Github image markdown format:

![Ubuntu 5 DE.png](https://i.stack.imgur.com/MoxHd.jpg)

**Note:** Blah, blah, blah

Another example of "footer hyper links" (for lack of a better noun) in Stack Exchange Markdown is:

- [Jack Master Volume?][1]

The simplest solution then is to install [JackMix][2]:

find listed [here][3].

[this script][4] is where you are heading:

  [1]: https://discourse.ardour.org/t/jack-master-volume/84650
  [2]: http://www.arnoldarts.de/jackmix/.
  [3]: http://jackaudio.org/applications/
  [4]: https://unix.stackexchange.com/questions/374085/lower-or-increase-pulseaudio-volume-on-all-outputs

Needs to be converted to Github Markdown format of:

- [Jack Master Volume?](https://discourse.ardour.org/t/jack-master-volume/84650)

The simplest solution then is to install [JackMix](http://www.arnoldarts.de/jackmix/.):

find listed [here](http://jackaudio.org/applications/).

[this script](https://unix.stackexchange.com/questions/374085/lower-or-increase-pulseaudio-volume-on-all-outputs) is where you are heading:

Finally tonight I discovered that in Stack Exchange you can have:

    #!/bin/bash
    cat "$Filename.zip" | base64 > "$Filename64"

That needs reformatting to Github Markdown like this:

``` bash
#!/bin/bash
cat "$Filename.zip" | base64 > "$Filename64"
```

It gets even more complicated when SE Markdown has:

Or it has this:

Some lines of code
some more lines of
code

An existing Github Repo to convert would be awesome! If not then if someone has documented the differences between Stack Exchange Markup and Github Markup that would be great too.

If this question goes unanswered for a month then I guess I'll be answering it eventually after the trial-error-fix process is finished.

Body:
Converting thousands of Stack Exchange Q&A in markdown format isn't as easy
as simply copying them over to GitHub Pages. The python program
stack-to-blog.py was used to convert Stack Exchange posts to
GitHub Pages Posts.
The full stack-to-blog.py program can be accessed on the
Pippim Website repo .

The program automatically:

Creates Jekyll front matter on posts and front matter totals for site.

Selects Stack Exchange Posts based on meeting minimum criteria such as up-votes or accepted answer status.

If self-answered question, the answer is included and not the question.

If self-answered question, the accepted answer alone doesn't qualify. Votes from other are the qualifier.

Initial testing allows selecting small set of random record numbers to convert.

Converts Stack Exchange Markdown formats to GitHub Pages Kramdown Markdown format.

Creates hyperlinks to original Answer in Stack Exchange and Kramdown in GitHub Pages.

Creates search word to URL indices excluding 50% of words like "a", "the", etc. to save space.

Selectively inserts Table of Contents based on minimum criteria settings.

Selectively inserts Section Navigation Buttons for: Top (Top of Page), ToS (Top of Section), ToC (Table of Contents) and Skip (Skip section).

Selectively inserts "Copy Code Block to System Clipboard" button based on lines of code.

Creates HTML with "Top Ten Answers" with the most votes.

Creates powerful nested expandable/collapsible detail/summary HTML for many thousands of tags by post.

Remaps hyperlinks in Stack Exchange Posts to {{ site.title }} website posts if they were converted.

Fixes old broken #header Stack Exchange Markdown.

Converts < block quote Stack Exchange Markdown into what works in Jekyll Kramdown.

Convert Stack Exchange <!-- language --> tags to fenced code block language.

When no fenced code block language is provided, uses shebang language first (if available).

Converts older four-space indented code blocks to fenced code blocks.

Converts Stack Exchange Hyperlinks where the website post title is implied and not explicit.

Prints list of self-answered questions that were not accepted after the mandatory two day wait period.

Prints list of Rouge Syntax Highlighting languages not supported in fenced code blocks.

Prints summary totals when finished.

Full documentation is provided here.

This is what program looks like when running:


来源: stackexchange

问:
How to remove everything excluding what's between given characters?
I am automating a task, where I need it to open a .csv or .txt file, that will have a bunch of information I don't need. I want to remove everything excluding what's in a specific range.

For example: adhjnwadk'symbol:xxx'abcahjda.

So I would want to keep everything in 'symbol:xxx' and remove everything outside of it. Please note that xxx is a variable and is different in every line.

Is this possible?

答:
There are many ways to skin this particular cat:

with open("data.txt", "r") as infile:
    symbols = []
    for line in infile.readlines():
        _, _, lineend = line.partition("'symbol:")
        if lineend:
            symbol = lineend.split("'")[0]
            symbols.append(f"'symbol:{symbol}'")
```

This finds everything after symbol:, discards everything after the ', and stores the 'symbol:xxx' in a list.

ContentLicense: CC BY-SA 4.0

Body:
There are many ways to skin this particular cat:

with open("data.txt", "r") as infile:
    symbols = []
    for line in infile.readlines():
        _, _, lineend = line.partition("'symbol:")
        if lineend:
            symbol = lineend.split("'")[0]
            symbols.append(f"'symbol:{symbol}'")

This finds everything after symbol:, discards everything after the ', and stores the 'symbol:xxx' in a list.


问:
how to return promise from an async code block
I want to send an http request and return the result using typescript, before send the http request, I want to get the token from google chrome local storage and put the token into http header. This is my typescript code looks like:

```<code>api_post:<T>(url: string, data: any): Promise<T> => {
    chrome.storage.local.get("token", function (result:any) {
        return fetch(url, {
            method: 'POST',
            headers: {
                'Content-type': 'application/json',
                'x-access-token': result,
            },
            body: JSON.stringify(data),
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error(response.statusText);
                }
                return response.json() as Promise<T>;
            });
    });
},
</code>```

the problem is the promise return from the inner of an async code block, this function shows error:

```<code>interface Promise<T>
Represents the completion of an asynchronous operation

A function whose declared type is neither 'void' nor 'any' must return a value.ts(2355)
</code>```

what should I do to return the promise inside the chrome local storage get and make the code works?

答:
Just throwing in another snippet, since OP seem to dislike async/await from Ross Gatih' answer and I don't like the new Promise antipattern from Jordan Wrights answer

```<code>const api_post = <T>(url: string, data: any): Promise<T> => {
  return chrome.storage.local.get("token")
    .then((result: any) => fetch(url, {
      method: "POST",
      headers: {
        "Content-type": "application/json",
        "x-access-token": result,
      },
      body: JSON.stringify(data),
    }))
    .then((response) => {
      if (!response.ok) {
        throw new Error(response.statusText);
      }
      return response.json();
    });
};
</code>```

Body:
I want to send an http request and return the result using typescript, before send the http request, I want to get the token from google chrome local storage and put the token into http header. This is my typescript code looks like:

api_post:<T>(url: string, data: any): Promise<T> => {
    chrome.storage.local.get("token", function (result:any) {
        return fetch(url, {
            method: 'POST',
            headers: {
                'Content-type': 'application/json',
                'x-access-token': result,
            },
            body: JSON.stringify(data),
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error(response.statusText);
                }
                return response.json() as Promise<T>;
            });
    });
},

the problem is the promise return from the inner of an async code block, this function shows error:

interface Promise<T>
Represents the completion of an asynchronous operation

A function whose declared type is neither 'void' nor 'any' must return a value.ts(2355)

what should I do to return the promise inside the chrome local storage get and make the code works?

Body:
Just throwing in another snippet, since OP seem to dislike async/await from Ross Gatih' answer and I don't like the new Promise antipattern from Jordan Wrights answer

const api_post = <T>(url: string, data: any): Promise<T> => {
  return chrome.storage.local.get("token")
    .then((result: any) => fetch(url, {
      method: "POST",
      headers: {
        "Content-type": "application/json",
        "x-access-token": result,
      },
      body: JSON.stringify(data),
    }))
    .then((response) => {
      if (!response.ok) {
        throw new Error(response.statusText);
      }
      return response.json();
    });
};


问:
At runtime, I was promoted that could not create a database because of a syntax error
I refer to the professor's video to write my own Lab4 homework.
But when I run the program, it prompts me that a syntax error is occur for mysql.

I checked my code with the professor's Sample code，But can't find the error.

In fact, there is not much difference between the business class that I created and the pet class which create by professor.

Now I know the reason,
I think it is a very typical mistake,
If you are not familiar with mysql, you may have the same problem as me.

So, IN the answer column, I'll share with you one thing need to notice when accessing the database using JPA

答:
Mysql has keywords. （select，from，add，between...）

If a property in our business class has the same name as the keywords, Then there will be problems.

For my case, I created a bug ticket class.
it has a field named rank (Used to distinguish the severity of a bug)，but as you know Rank is also one of the mysql keywords.

That's why I'm reminded of syntax error，The problem was solved when I changed the property name from rank to bugRank.


问:
Updating application icon Badge in iPhone 3.0, when application is not running
I have an application, which lists number of IM sent by any of my friend using my application. And application icon show number of unread IMs to me. If my application is running in foreground, I can update badge by calling web service every 1 minute which will return number of unread IMs. Suppose, if the person is using iPhone3.0 and application is not running, how can i update the badge? Push notification without alert, but with just number of unread IMs as badge number will work? </p>

<p>Please advice, Thank you. </p>

答:
At last, we found the problem, the problem is passing badge parameter as string in JSON payload, Now we are sending it as number, so even when the application is in background, badge getting set properly.
Thank you for your all help. </p>

ContentLicense: CC BY-SA 3.0

Body:
I have an application, which lists number of IM sent by any of my friend using my application. And application icon show number of unread IMs to me. If my application is running in foreground, I can update badge by calling web service every 1 minute which will return number of unread IMs. Suppose, if the person is using iPhone3.0 and application is not running, how can i update the badge? Push notification without alert, but with just number of unread IMs as badge number will work?

Please advice, Thank you.


问:
How can I avoid a collectAsList() when path is read from another data source?
I have the parquet path present as a table column, need to then pass this column list as an input to readFrom parquet.

```<code>List<Row> rows = spark.read.<datasource>.select("path").collectAsList();

List<String> paths =  <convert the rows to string>

spark.read.parquet(paths).
</code>```

The collectAsList is an expensive operation with data being brought to the driver.

Is there a better approach?

答:
Another approach is to use a CollectionAccumulator.</p>
```<code>df.javaRDD().forEachPartition{
//executor code
//add values to the accumulator
}

//Note that this is executed in the driver
List<String> paths = accumulator.value
</code>```

Body:
I have the parquet path present as a table column, need to then pass this column list as an input to readFrom parquet.

List<Row> rows = spark.read.<datasource>.select("path").collectAsList();

List<String> paths =  <convert the rows to string>

spark.read.parquet(paths).

The collectAsList is an expensive operation with data being brought to the driver.

Is there a better approach?

Body:
Another approach is to use a CollectionAccumulator.

df.javaRDD().forEachPartition{
//executor code
//add values to the accumulator
}

//Note that this is executed in the driver
List<String> paths = accumulator.value


问:
convert markdown text to html table using perl
I'm trying to convert a Markdown text to HTML table with Perl modules. I tried to use Text::Markdown, but it didn't work.

I also tried to use Markdown::Table and follow the documentation: https://metacpan.org/pod/Markdown::Table,
but same thing no result.

Here is my code:

```<code>use Markdown::Table;

my $markdown = q~
| Id | Name | Role |
+---+---+---+
| 1 | John Smith | Testrole |
| 2 | Jane Smith | Admin |
~;

my @tables = Markdown::Table->parse($markdown);

use Data::Dumper;
Core::Global::Logger::debug(Dumper( $tables[0]->get_table ) );
</code>```

Output:

can anyone help about that please ?

答:
I'd recommend to use Perl's Pandoc module, which is a wrapper around Pandoc, a tool to convert documents from one format to another. For instance,

```<code>use Pandoc;

my $markdown = q(
Some `standard` **Markdown** _syntax_. Also, a table:

country | capital
--------|---------
France  | Paris
UK      | London
Germany | Berlin);

print pandoc->convert('markdown' => 'html', $markdown);
</code>```

Which outputs:

Some standard Markdown syntax. Also, a table:

country
capital

France
Paris

UK
London

Germany
Berlin

```
</div>
</div>
</p>
<p>Note that for this to work, you'll have to install Perl's Pandoc module (with <code>cpanm Pandoc</code> for instance), as well as the Pandoc program (with <code>sudo apt install pandoc</code> on Debian/Ubuntu; see Installing pandoc for other systems).</p>
<hr />
<p>Regarding the other modules you tried to use:</p>
<ul>
<li><p>Markdown::Table is a module that aims at helping you parse and manipulate Markdown tables. It does not generate HTML (nor anything besides Markdown for that matter).</p>
</li>
<li><p>Text::Markdown generates HTML from Markdown, but does not support Markdown tables. The syntax of Markdown supported by this module is specified in https://daringfireball.net/projects/markdown/syntax, and does not include tables.<br />
Still, this module allows you to write HTML inside your Markdown, which <em>could</em> be a workaround, depending on your exact requirements. For instance:</p>
```use Text::Markdown 'markdown';

my $markdown = q(
This _text_ uses **Markdown** `syntax`. Then a table:

country	capital

France	Paris

UK	London

Germany	Berlin

);

print markdown( $markdown );
```

Text::Markdown will convert the Markdown elements to HTML, while preserving the table.

Body:
I'm trying to convert a Markdown text to HTML table with Perl modules. I tried to use Text::Markdown, but it didn't work.

I also tried to use Markdown::Table and follow the documentation: https://metacpan.org/pod/Markdown::Table,
but same thing no result.

Here is my code:

use Markdown::Table;

my $markdown = q~
| Id | Name | Role |
+---+---+---+
| 1 | John Smith | Testrole |
| 2 | Jane Smith | Admin |
~;

my @tables = Markdown::Table->parse($markdown);

use Data::Dumper;
Core::Global::Logger::debug(Dumper( $tables[0]->get_table ) );

Output:

can anyone help about that please ?

Body:
I'd recommend to use Perl's Pandoc module, which is a wrapper around Pandoc, a tool to convert documents from one format to another. For instance,

use Pandoc;

my $markdown = q(
Some `standard` **Markdown** _syntax_. Also, a table:

country | capital
--------|---------
France  | Paris
UK      | London
Germany | Berlin);

print pandoc->convert('markdown' => 'html', $markdown);

Which outputs:

<p>Some <code>standard</code> <strong>Markdown</strong> <em>syntax</em>. Also, a table:</p>
<table>
<thead>
<tr class="header">
<th>country</th>
<th>capital</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>France</td>
<td>Paris</td>
</tr>
<tr class="even">
<td>UK</td>
<td>London</td>
</tr>
<tr class="odd">
<td>Germany</td>
<td>Berlin</td>
</tr>
</tbody>
</table>

Note that for this to work, you'll have to install Perl's Pandoc module (with cpanm Pandoc for instance), as well as the Pandoc program (with sudo apt install pandoc on Debian/Ubuntu; see Installing pandoc for other systems).

Regarding the other modules you tried to use:

Markdown::Table is a module that aims at helping you parse and manipulate Markdown tables. It does not generate HTML (nor anything besides Markdown for that matter).

Text::Markdown generates HTML from Markdown, but does not support Markdown tables. The syntax of Markdown supported by this module is specified in https://daringfireball.net/projects/markdown/syntax, and does not include tables.

Still, this module allows you to write HTML inside your Markdown, which could be a workaround, depending on your exact requirements. For instance:

use Text::Markdown 'markdown';

my $markdown = q(
This _text_ uses **Markdown** `syntax`. Then a table:

<table>
<tr><td>country</td><th>capital</th></tr>
<tr><td>France</td><td>Paris</td></tr>
<tr><td>UK</td><td>London</td></tr>
<tr><td>Germany</td><td>Berlin</td></tr>
</table>);

print markdown( $markdown );

Text::Markdown will convert the Markdown elements to HTML, while preserving the table.


问:
Finding the local maxima in a 1D array
Is there an easy way to find the local maxima in a 1D array?

Let's say I have an array:

```<code>[ 0,
  1,
  10, <- max
  8,  <- (ignore)
  3,
  0,
  0,
  4,
  6,  <- (ignore)
  10, <- max
  6,  <- (ignore)
  1,
  0,
  0,
  1,
  4,  <- max
  1,
  0 ]
</code>```

I want it to find the 10s and the 4, but ignore the 8 & 6, since those are next to 10s. Mathematically, you could just find where derivative is equal to zero if it were a function. I'm not too sure how to do this in Javascript.

答:
const values = [3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3];

const findMinimas = arr => arr.filter((el, index) => {
  return el < arr[index - 1] && el < arr[index + 1]
});

console.log(findMinimas(values)); // => [2, 1, 1]

const findMinimumIndices = arr => arr.map((el, index) => {
  return el < arr[index - 1] && el < arr[index + 1] ? index : null
}).filter(i => i != null);

console.log(findMinimumIndices(values)); // => [1, 5, 9]```

Title: Finding the local maxima in a 1D array

Body:
Is there an easy way to find the local maxima in a 1D array?

Let's say I have an array:

[ 0,
  1,
  10, <- max
  8,  <- (ignore)
  3,
  0,
  0,
  4,
  6,  <- (ignore)
  10, <- max
  6,  <- (ignore)
  1,
  0,
  0,
  1,
  4,  <- max
  1,
  0 ]

I want it to find the 10s and the 4, but ignore the 8 & 6, since those are next to 10s. Mathematically, you could just find where derivative is equal to zero if it were a function. I'm not too sure how to do this in Javascript.


问:
Calculate first day of week as a special result string
I've got a special request for a new project. We've got a date like "2022-02-01" and we have to calculate it to get the following string as result "WB - Jan-31". It has to be always the date of the first day of the week. First day of week is Monday. A colleague give the hint to solve it with the to_char () function, but how do I manage to get a result like this?

date
week

2022-02-01
WS - Jan 31

2022-02-02
WS - Jan 31

2022-02-07
WS - Feb 07

2022-02-08
WS - Feb 07

What I have right now:

This gives a number of day of the week back as integer for calculation.

```<code>SELECT create_date, to_char(create_date, 'ID')::integer as dow
FROM customer;
</code>```

I also figured how to produce a string.

```<code>SELECT create_date, to_char(create_date, '"WB - " Mon DD')
FROM customer;
</code>```

What I don't know how to mange to get as result the first day of week for Monday to Friday like "WS - Jan 31"?

I also figured how to calculate first day of week as date but how put it in this result?

```<code>SELECT create_date - to_char(create_date, 'ID')::integer + 1
FROM customer;
</code>```

I appreciate every help, I'm still into learning SQL from scratch and it's my first week of doing this at my new position.

答:
To get the start of the week from a DATE value, use date_trunct() the result can then be formatted using to_char()

```<code>to_char(date_trunc('week', create_date), 'Mon DD')
</code>```

You can concatenate any prefix you want to the result of that, e.g.

```<code>concat('WS - ', to_char(date_trunc('week', create_date), 'Mon DD'))
</code>```

Body:
I've got a special request for a new project. We've got a date like "2022-02-01" and we have to calculate it to get the following string as result "WB - Jan-31". It has to be always the date of the first day of the week. First day of week is Monday. A colleague give the hint to solve it with the to_char () function, but how do I manage to get a result like this?

date
week

2022-02-01
WS - Jan 31

2022-02-02
WS - Jan 31

2022-02-07
WS - Feb 07

2022-02-08
WS - Feb 07

What I have right now:

This gives a number of day of the week back as integer for calculation.

SELECT create_date, to_char(create_date, 'ID')::integer as dow
FROM customer;

I also figured how to produce a string.

SELECT create_date, to_char(create_date, '"WB - " Mon DD')
FROM customer;

What I don't know how to mange to get as result the first day of week for Monday to Friday like "WS - Jan 31"?

I also figured how to calculate first day of week as date but how put it in this result?

SELECT create_date - to_char(create_date, 'ID')::integer + 1
FROM customer;

I appreciate every help, I'm still into learning SQL from scratch and it's my first week of doing this at my new position.

Body:
To get the start of the week from a DATE value, use date_trunct() the result can then be formatted using to_char()

to_char(date_trunc('week', create_date), 'Mon DD')

You can concatenate any prefix you want to the result of that, e.g.

concat('WS - ', to_char(date_trunc('week', create_date), 'Mon DD'))


问:
Why does Yup validation not work when using both number().positive() and number().moreThan()?
Based on my issue's title, it may seem similar to the one posted here; however, the context is different. In my situation, I have two number fields (i.e., Min and a Max). The Min value has to be a non-zero, positive number and less than the Max value (if there is a Max value). The Max value has to be a non-zero, positive number and greater than the Min value (if there is a Min value). I have error messages that are supposed to display depending on whether those conditions aren't met when a user tries to submit a form; however, it appears that the order matters in determining when an error message is displayed. For example, if I input -5 as the Min value and -2 as the Max value, only the error message for the Min value is displayed, even though the Max value violates the "non-zero" rule.

I'd really appreciate it if someone could suggest any workarounds, if there are any. I've included my code below:

```<code>maxValue: Yup.number().when(['minValue'], {
    is: (minValue) => Boolean(minValue),
    otherwise: Yup.number().positive().nullable(),
    then: Yup.number().positive().moreThan(Yup.ref('minValue)).nullable(),
}),
minValue: Yup.number().when(['maxValue'], {
    is: (maxValue) => Boolean(maxValue),
    otherwise: Yup.number().positive().nullable(),
    then: Yup.number().positive().lessThan(Yup.ref('maxValue)).nullable(),
}),
</code>```

答: I was able to resolve my issue by swapping out the .positive() in my then chain for maxValue and replacing it with .test('greaterThanZero', 'Value must be greater than zero', value => value ? value > 0 : ' ').

Body:
Based on my issue's title, it may seem similar to the one posted here; however, the context is different. In my situation, I have two number fields (i.e., Min and a Max). The Min value has to be a non-zero, positive number and less than the Max value (if there is a Max value). The Max value has to be a non-zero, positive number and greater than the Min value (if there is a Min value). I have error messages that are supposed to display depending on whether those conditions aren't met when a user tries to submit a form; however, it appears that the order matters in determining when an error message is displayed. For example, if I input -5 as the Min value and -2 as the Max value, only the error message for the Min value is displayed, even though the Max value violates the "non-zero" rule.

I'd really appreciate it if someone could suggest any workarounds, if there are any. I've included my code below:

maxValue: Yup.number().when(['minValue'], {
    is: (minValue) => Boolean(minValue),
    otherwise: Yup.number().positive().nullable(),
    then: Yup.number().positive().moreThan(Yup.ref('minValue)).nullable(),
}),
minValue: Yup.number().when(['maxValue'], {
    is: (maxValue) => Boolean(maxValue),
    otherwise: Yup.number().positive().nullable(),
    then: Yup.number().positive().lessThan(Yup.ref('maxValue)).nullable(),
}),


问:
What is the correct syntax for an MDX Filter Expression when using MTD and QTD functions in SSRS/SSAS Query Designer?
Having used the Query Designer in BIDS to type in straight MDX queries I am now attempting to create similar queries but using the graphical drag-and-drop GUI interface instead.

However I am having trouble specifying a syntactically correct MDX filter expression for returning all the MTD and QTD dates.

Here is what I have tried so far:

```<code>MTD([dimTime].[Hierarchy].[Day Key].currentmember)
MTD([dimTime].[Hierarchy].currentmember)
MTD()
</code>```

Each one in turn parses correctly but returns no rows.

The filter is set up as follows:

```<code>Dimension = Time
Hierarchy = Hierarchy
Operator = MDX
MDX = (see the three examples I tried above)
</code>```

答:
As I understood you want to obtain list of dates like a String?

"...expression for returning all the MTD and QTD dates."

So, you can use function SetToStr:

```<code>SetToStr(MTD([dimTime].[Hierarchy].[Day Key].currentmember))
</code>```

Body:
Having used the Query Designer in BIDS to type in straight MDX queries I am now attempting to create similar queries but using the graphical drag-and-drop GUI interface instead.

However I am having trouble specifying a syntactically correct MDX filter expression for returning all the MTD and QTD dates.

Here is what I have tried so far:

MTD([dimTime].[Hierarchy].[Day Key].currentmember)
MTD([dimTime].[Hierarchy].currentmember)
MTD()

Each one in turn parses correctly but returns no rows.

The filter is set up as follows:

Dimension = Time
Hierarchy = Hierarchy
Operator = MDX
MDX = (see the three examples I tried above)

Body:
As I understood you want to obtain list of dates like a String?

"...expression for returning all the MTD and QTD dates."

So, you can use function SetToStr:

SetToStr(MTD([dimTime].[Hierarchy].[Day Key].currentmember))


问:
java java.io.FileNotFoundException when file exist
I need to read JSON file and cast it to Java ArrayList, i have code:

```<code>public void loadConfig() {
List<ConfigModel> configModels = JsonUtils.fromFileToList(ConfigReaderService.class.getClassLoader().getResource(ConfigFileName).getPath(), new TypeReference<List<ConfigModel>>() {
        });
}
</code>```
```<code>public static <T> List<T> fromFileToList(String fileName, TypeReference<List<T>> typeReference) {
        try {
            File f = new File(fileName);

            return jsonMapper.readValue(f, typeReference);
        } catch (IOException e) {
            throw new IllegalStateException("can't convert String to Object", e);
        }
    }
</code>```

It's perfectly working when i'm starting application with idea in run/debug, but it throws exception when i'm building spring-boot jar application and execution it via cmd/bash.

```<code>java -jar service.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.3)

2022-02-11 16:37:59.582 sid: [main] INFO - Starting ServiceApplication v0.0.1-SNAPSHOT using Java 1.8.0_291 on ! with PID 8780 ()
2022-02-11 16:37:59.582 sid: [main] INFO  g.n.w.ServiceApplication - No active profile set, falling back to default profiles: default
2022-02-11 16:38:01.737 sid: [main] ERROR g.n.ws.service.ConfigService - Exception in ConfigService
java.lang.IllegalStateException: can't convert String to Object
        at utils.JsonUtils.fromFileToList(JsonUtils.java:86)
Caused by: java.io.FileNotFoundException: file:\D:\IdeaProjects\service\target\service.jar!\BOOT-INF\classes!\config.json (The filename, directory name, or volume label syntax is incorrect)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:1029)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3514)
        at utils.JsonUtils.fromFileToList(JsonUtils.java:84)
        ... 49 common frames omitted

</code>```

where my mistake?

答: getResourceAsStream() fixed this problem

Body:
I need to read JSON file and cast it to Java ArrayList, i have code:

public void loadConfig() {
List<ConfigModel> configModels = JsonUtils.fromFileToList(ConfigReaderService.class.getClassLoader().getResource(ConfigFileName).getPath(), new TypeReference<List<ConfigModel>>() {
        });
}

public static <T> List<T> fromFileToList(String fileName, TypeReference<List<T>> typeReference) {
        try {
            File f = new File(fileName);

            return jsonMapper.readValue(f, typeReference);
        } catch (IOException e) {
            throw new IllegalStateException("can't convert String to Object", e);
        }
    }

It's perfectly working when i'm starting application with idea in run/debug, but it throws exception when i'm building spring-boot jar application and execution it via cmd/bash.

java -jar service.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.3)

2022-02-11 16:37:59.582 sid: [main] INFO - Starting ServiceApplication v0.0.1-SNAPSHOT using Java 1.8.0_291 on ! with PID 8780 ()
2022-02-11 16:37:59.582 sid: [main] INFO  g.n.w.ServiceApplication - No active profile set, falling back to default profiles: default
2022-02-11 16:38:01.737 sid: [main] ERROR g.n.ws.service.ConfigService - Exception in ConfigService
java.lang.IllegalStateException: can't convert String to Object
        at utils.JsonUtils.fromFileToList(JsonUtils.java:86)
Caused by: java.io.FileNotFoundException: file:\D:\IdeaProjects\service\target\service.jar!\BOOT-INF\classes!\config.json (The filename, directory name, or volume label syntax is incorrect)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:1029)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3514)
        at utils.JsonUtils.fromFileToList(JsonUtils.java:84)
        ... 49 common frames omitted

where my mistake?


问:
Choose Sentences with x High Score and Save to the Same Row (Dataframe)
I have Dataframe ranked_sentences like this

```<code>       0              1
0   0.093305    Hello this is a
1   0.088413    Hello this is b
2   0.091036    Hello this is c
3   0.095541    Hello this is d
4   0.092201    Hello this is e
5   0.088338    Hello this is f
6   0.089100    Hello this is g
7   0.089980    Hello this is h
8   0.088446    Hello this is i
9   0.092737    Hello this is j
10  0.090902    Hello this is k
</code>```

i want to choose sentences with sn (sn=4) high score and save in list. moreover, a sentence order according to index. I want the output like this:

```<code>                                    x
0   Hello this is a. Hello this is d. Hello this is e. Hello this is j.
</code>```

anybody can help me to get my goal. I try with this code, but i confused for get sentences with high score. thank you

```<code>d = []
sn = 4
for i in range(sn):
    summary = ranked_sentences[1][i]
    d.append(summary)
    print(summary)
</code>```

答:
This below code will

sort the dataframe on column 0 in descending order.

Fetch top 3 rows.

Convert the 3 values of column 1 to a list

Assign the list to list d.

```<code>d = ranked_sentences.sort_values(0, ascending = False).head(3)[1].to_list()
</code>```

Body:
I have Dataframe ranked_sentences like this

       0              1
0   0.093305    Hello this is a
1   0.088413    Hello this is b
2   0.091036    Hello this is c
3   0.095541    Hello this is d
4   0.092201    Hello this is e
5   0.088338    Hello this is f
6   0.089100    Hello this is g
7   0.089980    Hello this is h
8   0.088446    Hello this is i
9   0.092737    Hello this is j
10  0.090902    Hello this is k

i want to choose sentences with sn (sn=4) high score and save in list. moreover, a sentence order according to index. I want the output like this:

                                    x
0   Hello this is a. Hello this is d. Hello this is e. Hello this is j.

anybody can help me to get my goal. I try with this code, but i confused for get sentences with high score. thank you

d = []
sn = 4
for i in range(sn):
    summary = ranked_sentences[1][i]
    d.append(summary)
    print(summary)

Body:
This below code will

sort the dataframe on column 0 in descending order.

Fetch top 3 rows.

Convert the 3 values of column 1 to a list

Assign the list to list d.

d = ranked_sentences.sort_values(0, ascending = False).head(3)[1].to_list()


问:
Placement New Operator
```<code>#include<iostream>
using namespace std;

int main() {
    int buf[2];
    int *p=new (buf) int(2);
    int *q=new (buf+1) int(6);
    for(int i=0;i<2;i++)
        cout<<buf[i]<<" ";
    return 0;
}
</code>```

I was trying placement new operator with the following example. For the above code I get the output as:

```<code>trial.cpp:7:20: warning: placement new constructing an object of type 'int' and size '4' in a region of type 'int [2]' and size '0' [-Wplacement-new=]
     int *q=new (buf+1) int(6);
2 6
</code>```

Why am I getting a warning for *q ? From my understanding p and q are 2 pointers pointing to 2 different blocks in buf array. Please correct me if I'm wrong.

答:
This is a GCC bug affecting versions 8 to 10 and fixed in 11, see bug report.

The code is fine, the warning a false-positive.

Title: Placement New Operator

Body:
#include<iostream>
using namespace std;

int main() {
    int buf[2];
    int *p=new (buf) int(2);
    int *q=new (buf+1) int(6);
    for(int i=0;i<2;i++)
        cout<<buf[i]<<" ";
    return 0;
}

I was trying placement new operator with the following example. For the above code I get the output as:

trial.cpp:7:20: warning: placement new constructing an object of type 'int' and size '4' in a region of type 'int [2]' and size '0' [-Wplacement-new=]
     int *q=new (buf+1) int(6);
2 6

Why am I getting a warning for *q ? From my understanding p and q are 2 pointers pointing to 2 different blocks in buf array. Please correct me if I'm wrong.


问:
Modifying String in Java
I have a String  , String a = newNumber + "*" + nn + "+" + difference;
the newNumber = 106 , nn = 3 and difference = 3.
so the output should be as follow ;

Output :
106*3+3

I would like to modify the String so that the output becomes (35*3+1)*3+3 and then with this new String I would like to modify it again so that it becomes ((11*3+2)*3+1)*3+3
Basically I just need to replace the newNumber which was 106 and kept changing to 11, as you can see I'm trying to modify only the newNumber and replacing it with another while keeping the entire String untouched , I'm just replacing and adding to it , how can this be achieved ?
The output should be like this,
Output :

```<code>106*3+3
(35*3+1)*3+3
((11*3+2)*3+1)*3+3
</code>```

I'm solving an equation with steps , the formulas don't matter I'm just trying to figure out how can I modify the String by replacing the newNumber with a another number and adding new brackets to the equation.
I hope I wrote my problem in a way you would understand , I'd really appreciate the help.

答:
I could not get to the same output which you have but here the code which try to solve this problem I think it might give you little help though which you could solve the problem.

Breaking the number until its prime number and adding the prime numbers to the result. Since we are replacing and appending with strings its better to use StringBuilder.

```<code>import java.io.PrintStream;
import java.util.Arrays;

public class StringSimplification {
    public static PrintStream out = System.out;
    public static final boolean prime[];
    public static final int SIZE = 1000000;

    static {
        prime = new boolean[SIZE];
        Arrays.fill(prime, true);
        prime[0] = prime[1] = false;
        //Sieve of Eratosthenes algorithm to find weather number is prime
        for (int i = 2; i < SIZE; i++)
            if (prime[i])
                for (int j = i * 2; j < SIZE; j += i)
                    prime[j] = false;
    }
    //simplifies your String expression
    public static String simplify(final String expression) {
        StringBuilder result = new StringBuilder("");
        String exp = "";
        for (char ch : expression.toCharArray()) {
            if (Character.isDigit(ch))
                exp += ch;
            else {
                if (isNumber(exp)) {
                    String simplified = getExpression(Integer.parseInt(exp));
                    result.append(simplified+ch);
                    exp = "";//clearing exp
                };
            }
        }
        result.append(exp);
        return result.toString();
    }
    //returns weather number is prime or not
    static boolean isPrime(final int val) {
        return prime[val];
    }

    static String getExpression(final int val) {
        if (val == 0 || val == 1 || prime[val])
            return "(" + val + ")";
        int prev = 1;
        int div = 1;
        for (int i = 1; i < val; i++) {
            if (val % i == 0) {
                prev = i;
                div = val / i;
            }
        }
        return getExpression(prev) + "*" + getExpression(div);
    }
    //Check's weather the expression is number
    public static boolean isNumber(final String s) {
        for (var c : s.toCharArray())
            if (!Character.isDigit(c))
                return false;

        return s.length() > 0;
    }

    public static void main(final String... $) {
        out.println(simplify("106*3+3"));
        out.println(simplify("1024*3+3"));
    }
}
</code>```

Output:

```<code>(53)*(2)*(3)+3
(2)*(2)*(2)*(2)*(2)*(2)*(2)*(2)*(2)*(2)*(3)+3
</code>```

Title: Modifying String in Java

Body:
I have a String  , String a = newNumber + "*" + nn + "+" + difference;
the newNumber = 106 , nn = 3 and difference = 3.
so the output should be as follow ;

Output :
106*3+3

I would like to modify the String so that the output becomes (35*3+1)*3+3 and then with this new String I would like to modify it again so that it becomes ((11*3+2)*3+1)*3+3
Basically I just need to replace the newNumber which was 106 and kept changing to 11, as you can see I'm trying to modify only the newNumber and replacing it with another while keeping the entire String untouched , I'm just replacing and adding to it , how can this be achieved ?
The output should be like this,
Output :

106*3+3
(35*3+1)*3+3
((11*3+2)*3+1)*3+3

I'm solving an equation with steps , the formulas don't matter I'm just trying to figure out how can I modify the String by replacing the newNumber with a another number and adding new brackets to the equation.
I hope I wrote my problem in a way you would understand , I'd really appreciate the help.

Body:
I could not get to the same output which you have but here the code which try to solve this problem I think it might give you little help though which you could solve the problem.

Breaking the number until its prime number and adding the prime numbers to the result. Since we are replacing and appending with strings its better to use StringBuilder.

import java.io.PrintStream;
import java.util.Arrays;

public class StringSimplification {
    public static PrintStream out = System.out;
    public static final boolean prime[];
    public static final int SIZE = 1000000;

    static {
        prime = new boolean[SIZE];
        Arrays.fill(prime, true);
        prime[0] = prime[1] = false;
        //Sieve of Eratosthenes algorithm to find weather number is prime
        for (int i = 2; i < SIZE; i++)
            if (prime[i])
                for (int j = i * 2; j < SIZE; j += i)
                    prime[j] = false;
    }
    //simplifies your String expression
    public static String simplify(final String expression) {
        StringBuilder result = new StringBuilder("");
        String exp = "";
        for (char ch : expression.toCharArray()) {
            if (Character.isDigit(ch))
                exp += ch;
            else {
                if (isNumber(exp)) {
                    String simplified = getExpression(Integer.parseInt(exp));
                    result.append(simplified+ch);
                    exp = "";//clearing exp
                };
            }
        }
        result.append(exp);
        return result.toString();
    }
    //returns weather number is prime or not
    static boolean isPrime(final int val) {
        return prime[val];
    }

    static String getExpression(final int val) {
        if (val == 0 || val == 1 || prime[val])
            return "(" + val + ")";
        int prev = 1;
        int div = 1;
        for (int i = 1; i < val; i++) {
            if (val % i == 0) {
                prev = i;
                div = val / i;
            }
        }
        return getExpression(prev) + "*" + getExpression(div);
    }
    //Check's weather the expression is number
    public static boolean isNumber(final String s) {
        for (var c : s.toCharArray())
            if (!Character.isDigit(c))
                return false;

        return s.length() > 0;
    }

    public static void main(final String... $) {
        out.println(simplify("106*3+3"));
        out.println(simplify("1024*3+3"));
    }
}

Output:

(53)*(2)*(3)+3
(2)*(2)*(2)*(2)*(2)*(2)*(2)*(2)*(2)*(2)*(3)+3


问:
Failed to find provider info for cn.teddymobile.free.anteater.den.provider
i have a question about this log, what does it mean ? i don't understand, but my bar chart has appeared. My data has not appeared. log :

```<code>E/ActivityThread: Failed to find provider info for cn.teddymobile.free.anteater.den.provider
</code>```

Android Manifest :

```<code><provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.cn.teddymobile.free.anteater.den.provider"
    android:exported="false"
    android:grantUriPermissions="true"
    tools:replace="android:authorities">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"
        tools:replace="android:resource" />
</code>```

xml - > path :

```<code><?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>
</code>```

答: just clear the app data of your app you are working on and you should be good to go..</p>

Body:
i have a question about this log, what does it mean ? i don't understand, but my bar chart has appeared. My data has not appeared. log :

E/ActivityThread: Failed to find provider info for cn.teddymobile.free.anteater.den.provider

Android Manifest :

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.cn.teddymobile.free.anteater.den.provider"
    android:exported="false"
    android:grantUriPermissions="true"
    tools:replace="android:authorities">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"
        tools:replace="android:resource" />

xml - > path :

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>


问:
xpath for click to a button - python/selenium
I'm trying to access a homepage, make login, click the login button and click a button (in the second page) using python/selenium.

The login button I wrote using Xpath and it's working well. The browser opens, writes user and login and click the login button.

Unfortunately, in the next page I'm not able to click on the button that I need to. It didn't work using Xpath and I can't understand why. The html is completely different from the first button, the button name is 'Reservar' and it is inside a class named <app-menu__menu>, written as:

```<code><a href="/Services" id="advanced" class="element  ">

                    <span>Reservar</span>
                </a>
</code>```

The xpath I got and tried:

```<code>xpath = "//*[@id="advanced"]"
</code>```

Then I tried a second verion (it was gotten as the second line code xpath):

```<code>xpath = "//*[@id="advanced"]/span"
</code>```

When I first tried to used them, I got an error. Then I change the "" to ' ' and the error was gone. But the program can't locate the button.

I'm using google-chrome, ubuntu, python3 and selenium package:

```<code>driver.find_element_by_xpath(xpath).click()
</code>```

Thanks for any help.

答:
You probably missing a delay / wait.

After clicking the submit / login button on the login page it takes some time to make the internal page loaded.

The recommended way to do that is to use Expected Conditions explicit waits, something like this:

wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='advanced']"))).click()
```
<p>To use the <code>wait</code> object here you will need to import the following inports:</p>
<pre class="lang-py prettyprint-override"><code>from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
</code>```

And to initialize the wait object with

wait = WebDriverWait(driver, 20)
```

You will have to validate your locator is unique. As well as to validate the element you are trying to access is not inside iframe and not in a new window / tab etc.

Body:
I'm trying to access a homepage, make login, click the login button and click a button (in the second page) using python/selenium.

The login button I wrote using Xpath and it's working well. The browser opens, writes user and login and click the login button.

Unfortunately, in the next page I'm not able to click on the button that I need to. It didn't work using Xpath and I can't understand why. The html is completely different from the first button, the button name is 'Reservar' and it is inside a class named <app-menu__menu>, written as:

                    Reservar

The xpath I got and tried:

xpath = "//*[@id="advanced"]"

Then I tried a second verion (it was gotten as the second line code xpath):

xpath = "//*[@id="advanced"]/span"

When I first tried to used them, I got an error. Then I change the "" to ' ' and the error was gone. But the program can't locate the button.

I'm using google-chrome, ubuntu, python3 and selenium package:

driver.find_element_by_xpath(xpath).click()

Thanks for any help.

Body:
You probably missing a delay / wait.

After clicking the submit / login button on the login page it takes some time to make the internal page loaded.

The recommended way to do that is to use Expected Conditions explicit waits, something like this:

wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='advanced']"))).click()

To use the wait object here you will need to import the following inports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

And to initialize the wait object with

wait = WebDriverWait(driver, 20)

You will have to validate your locator is unique. As well as to validate the element you are trying to access is not inside iframe and not in a new window / tab etc.


问:
String matching from one data frame column to another data frame column
i have two data frame , A and B . In A Data frame two column value and filed .
and in B data frame have also value and filed column. i want to match the 'value' column of B to A of 'Filed' column,
replace the Filed of A to value of B.
A=

```<code> Value                     Filed
valid username              username
valid username           input_txtuserid
Password                 input_txtpassword
Password                 txtPassword
Login                     input_submit_log_in
LOG IN                    SIGNIN
</code>```

B=

```<code> Value                     Filed
input_txtuserid               "JOHN"
input_txtpassword           "78945"
input_submit_log_in        "Sucessfully"
Password                 txtPassword
City                       "London"
PLACE                      "4-A avenue Street"
</code>```

i want my dataframe C to look like this

C=

```<code>Value                     Filed
valid username            "JOHN"
Password                   "78945"
Login                       "Sucessfully"
</code>```

i written below code , but i getting KeyError: 'City',

_map = dict(zip(A.Filed.values, A.Value.values))

def get_correct_value(row, _map=_map):

```<code> new_value = _map[row.Value]

 filed = row.Filed

return new_value, filed
</code>```

C = B.apply(get_correct_value, axis=1, result_type='expand')

C.columns = ['Value','Filed']

i want to ignore the keyword which is not available in A dataframe.                         '

答:
I'm going to assume that in the DataFrames are strings as we typically don't use Dataframes to carry variables. With this I created a sample with your dataframe values.

```<code>data_a = {"Value": ["valid username", "valid username", "Password", "Password", "Login", "LOG IN"],
         "Filed": ["username", "input_txtuserid", "input_txtpassword", "txtPassword", "input_submit_log_in", "SIGNIN"]}

data_b = {"Value": ["input_txtuserid", "input_txtpassword", "input_submit_log_in", "Password", "City", "PLACE"],
          "Filed": ["JOHN", "78945", "Sucessfully", "txtPassword", "London", "4-A avenue Street"]}

A = pd.DataFrame(data_a)
B = pd.DataFrame(data_b)
</code>```

A looks like:

B looks like:

Below the code to create C:

```<code># Merging A and B, using a left join on the columns Filed for A and Value for B. Creatingg an indicator where exists
C = pd.merge(A, B, left_on=['Filed'], right_on=['Value'], how='left', indicator='Exist')

# If exists put true, otherwise false
C['Exist'] = np.where(C.Exist == 'both', True, False)
# Dropping all False so those that dont exist in both dataframes
C.drop(C[C['Exist'] == False].index, inplace=True)

# Making sure C has the right column and column names.
C = C[['Value_y', 'Filed_y']]
C.rename(columns = {"Value_y": "Value",
                    "Filed_y": "Filed"}, inplace = True)
</code>```

Output of C

Hope that helps! Please Mark this as answer if it does :)

Body:
i have two data frame , A and B . In A Data frame two column value and filed .
and in B data frame have also value and filed column. i want to match the 'value' column of B to A of 'Filed' column,
replace the Filed of A to value of B.
A=

 Value                     Filed
valid username              username
valid username           input_txtuserid
Password                 input_txtpassword
Password                 txtPassword
Login                     input_submit_log_in
LOG IN                    SIGNIN

B=

 Value                     Filed
input_txtuserid               "JOHN"
input_txtpassword           "78945"
input_submit_log_in        "Sucessfully"
Password                 txtPassword
City                       "London"
PLACE                      "4-A avenue Street"

i want my dataframe C to look like this

C=

Value                     Filed
valid username            "JOHN"
Password                   "78945"
Login                       "Sucessfully"

i written below code , but i getting KeyError: 'City',

_map = dict(zip(A.Filed.values, A.Value.values))

def get_correct_value(row, _map=_map):

 new_value = _map[row.Value]

 filed = row.Filed

return new_value, filed

C = B.apply(get_correct_value, axis=1, result_type='expand')

C.columns = ['Value','Filed']

i want to ignore the keyword which is not available in A dataframe.                         '

Body:
I'm going to assume that in the DataFrames are strings as we typically don't use Dataframes to carry variables. With this I created a sample with your dataframe values.

data_a = {"Value": ["valid username", "valid username", "Password", "Password", "Login", "LOG IN"],
         "Filed": ["username", "input_txtuserid", "input_txtpassword", "txtPassword", "input_submit_log_in", "SIGNIN"]}

data_b = {"Value": ["input_txtuserid", "input_txtpassword", "input_submit_log_in", "Password", "City", "PLACE"],
          "Filed": ["JOHN", "78945", "Sucessfully", "txtPassword", "London", "4-A avenue Street"]}

A = pd.DataFrame(data_a)
B = pd.DataFrame(data_b)

A looks like:

B looks like:

Below the code to create C:

# Merging A and B, using a left join on the columns Filed for A and Value for B. Creatingg an indicator where exists
C = pd.merge(A, B, left_on=['Filed'], right_on=['Value'], how='left', indicator='Exist')

# If exists put true, otherwise false
C['Exist'] = np.where(C.Exist == 'both', True, False)
# Dropping all False so those that dont exist in both dataframes
C.drop(C[C['Exist'] == False].index, inplace=True)

# Making sure C has the right column and column names.
C = C[['Value_y', 'Filed_y']]
C.rename(columns = {"Value_y": "Value",
                    "Filed_y": "Filed"}, inplace = True)

Output of C

Hope that helps! Please Mark this as answer if it does :)


问:
Is it advisable to write a test case for every class in your program?
Im am just getting introduced to unit testing and test driven development. Thus far, I have only used Junit as testing framework. A question which emerged and for which I have not yet found a definite answer is: how much test cases do I need to write? Do I have to write a test case for every single class in my program? Or is this a stupid question because unit testing implies testing at the lowest (i.e. class) level?

I think writing a test case for every class is probably the more safe way to go (after all the more you test the lower the number of unforeseen bugs). But I was wondering if there are any widely agreed upon strategies regarding the amount of test cases to write?

答:
There are no strict rules in this area, only guidelines. You usually have one test case per class, but there are different strategies:

TestCase Per Class

TestCase Per Feature

TestCase Per Method

TestCase Per User Story

For example you can use 'Per Class' approach for classes that are relatively small and follow SRP. But if you have legacy code with a giant *Manager class it is fine to use 'Per Method' and have dedicated test case for only one of its methods. I think that choosing naming strategy for tests is at least as important as test code organization.

Using code coverage tool can help you find spots of untested code. It is less useful as metric. Having high code coverage does not necessarily mean that you have good tests. At the end of the day what matters is that you have meaningful and readable tests.


问:
Is there a way to have two docs in Docusaurus 2?
As I know, Docusaurus supports customized pages, but is there a way to have two docs in one Docusaurus project?

The original Navbar items have:

Docs

Blog

...

I want to have something like this:

Docs 1

Docs 2

Blog

...

I know I can make many subfolders just in one doc, but for some reason, I want a two Docs structure, which gives me a cleaner way to access docs.

If Docusaurus cannot offer this feature currently, I want to ask is there other documentation frameworks offer this feature?

答:
This solution worked for me. Using the 'autogenerated' sidebar in Docusaurus v2.0.0-beta.15

sidebars.js

```<code>/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */

const sidebars = {

  // tutorialSidebar: [{type: 'autogenerated', dirName: '.'}],
  newone: [{type: 'autogenerated', dirName: 'newone'}],  // foldername
  newtwo: [{type: 'autogenerated', dirName: 'newtwo'}],  // foldername

};

module.exports = sidebars;
</code>```

docusaurus.config.js

```<code>      navbar: {
        title: 'My Site',
        logo: {
          alt: 'My Site Logo',
          src: 'img/logo.svg',
        },
        items: [
          // {
          //   type: 'doc',
          //   docId: 'intro',
          //   position: 'left',
          //   label: 'Tutorials',
          // },

          {
            type: 'docSidebar',  // docSidebar
            position: 'left',
            sidebarId: 'newone', // foldername
            label: 'NEWONE',     // navbar title
          },
          {
            type: 'docSidebar',  // docSidebar
            position: 'left',
            sidebarId: 'newtwo', // foldername
            label: 'NEWTWO',     // navbar title
          },

          {to: '/blog', label: 'Blog', position: 'left'},
          {
            href: 'https://github.com/facebook/docusaurus',
            label: 'GitHub',
            position: 'right',
          },
        ],
      },
</code>```

Your docs folder:

```<code>docs/
    newone/
        intro.md
    newtwo/
        intro.md
</code>```

Body:
This solution worked for me. Using the 'autogenerated' sidebar in Docusaurus v2.0.0-beta.15

sidebars.js

/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */

const sidebars = {

  // tutorialSidebar: [{type: 'autogenerated', dirName: '.'}],
  newone: [{type: 'autogenerated', dirName: 'newone'}],  // foldername
  newtwo: [{type: 'autogenerated', dirName: 'newtwo'}],  // foldername

};

module.exports = sidebars;

docusaurus.config.js

      navbar: {
        title: 'My Site',
        logo: {
          alt: 'My Site Logo',
          src: 'img/logo.svg',
        },
        items: [
          // {
          //   type: 'doc',
          //   docId: 'intro',
          //   position: 'left',
          //   label: 'Tutorials',
          // },

          {
            type: 'docSidebar',  // docSidebar
            position: 'left',
            sidebarId: 'newone', // foldername
            label: 'NEWONE',     // navbar title
          },
          {
            type: 'docSidebar',  // docSidebar
            position: 'left',
            sidebarId: 'newtwo', // foldername
            label: 'NEWTWO',     // navbar title
          },

          {to: '/blog', label: 'Blog', position: 'left'},
          {
            href: 'https://github.com/facebook/docusaurus',
            label: 'GitHub',
            position: 'right',
          },
        ],
      },

Your docs folder:

docs/
    newone/
        intro.md
    newtwo/
        intro.md


问:
Flutter with direct dart firebase initialization still gets error on real device: Could not locate configuration file: 'GoogleService-Info.plist'
Flutter 2.8.1 using flutterfire - according to docs, should not need use the GoogleService-Info.plist file. This version states that it support direct dart initialization. Works fine on simulator, but when running on real iOS device - from Android Studio or XCode, I get the error about the plist file. flutterfire configure creates a valid firebase_options.dart file and main looks like this:

```<code>void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  runApp(const MyApp());
}
</code>```

Output of flutter doctor:

```<code>Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.8.1, on macOS 12.0.1 21A559 darwin-x64, locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.63.2)
[✓] Connected device (3 available)
</code>```

答:
Looks like this is a known issue:
https://github.com/FirebaseExtended/flutterfire/issues/7983

Need to keep an eye out for a solution....

Body:
Flutter 2.8.1 using flutterfire - according to docs, should not need use the GoogleService-Info.plist file. This version states that it support direct dart initialization. Works fine on simulator, but when running on real iOS device - from Android Studio or XCode, I get the error about the plist file. flutterfire configure creates a valid firebase_options.dart file and main looks like this:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  runApp(const MyApp());
}

Output of flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.8.1, on macOS 12.0.1 21A559 darwin-x64, locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.63.2)
[✓] Connected device (3 available)


问:
Is there any actively supported lcov port for windows
I measure coverage for my code using gcov library and I would like to generate coverage report in user-friendly format. I've found lcov utility for that, but it's not compatibile with Windows environment (mainly because of the way the paths are parsed). Does anyone know about actively supported lcov port for Windows?

It seems that making it work on Windows would be quite easy (it think it might even be solved by few creative batch/perl scripts that mimic the behavior of unix shell commands), and the tool is quite useful, so I suppose someone might have already done it.

答: There is an article here by Fred Grott that shows how to install lcov on windows using the Chocolatey package manager</p>

ContentLicense: CC BY-SA 2.5


问:
How do I connect to an AWS ElastiCache for Redis Cluster using Jedis?
Previously, we were using Redis with cluster mode disabled via AWS ElastiCache.

Our Java code using Jedis was pointing to the primary single node endpoint, which was used for reads and writes.

We've now enabled cluster mode.

We have now changed the code to point to the configuration endpoint of the new Redis cluster however it is now throwing errors whenever receiving requests, see below:

Redis Unavailable. Continue using the Queue requestMessage instead.
org.springframework.data.redis.ClusterRedirectException: Redirect: slot 2356 to [ipaddress]:6379.; nested exception is redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 2356 [ipaddress]:6379

Our configuration code is as below:

```<code>    @Bean(name = "redisTemplate")
    public RedisTemplate<String, String> getRedisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(jedisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    JedisConnectionFactory jedisConnectionFactory(Configuration config) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(config.get(HOST));
        jedisConnectionFactory.setPort(config.getInt(PORT));
        jedisConnectionFactory.setUsePool(true);
        jedisConnectionFactory.setPoolConfig(createJedisPoolConfig(config));
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    JedisPoolConfig createJedisPoolConfig(Config config) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(config.getInt(MAX, 8));
        poolConfig.setMaxIdle(config.getInt(MAXIDLE, 8));
        poolConfig.setMinIdle(config.getInt(MINIDLE, 1));
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        return poolConfig;
    }
</code>```

Shouldn't just changing Jedis to point to the configuration endpoint be enough?

Do I need to change anything in my code?

答:
Shouldn't just changing Jedis to point to the configuration endpoint be enough?

No, as the Redis client - Jedis in this instance - needs to be aware that it is connecting to a cluster and it currently isn't.

You are getting MOVED as the Redis cluster is telling the client that the data being requested has now been 'resharded' to another node. Jedis, however, is not aware that a cluster is being used, and thus that there are any other nodes and therefore, can't even connect to them to retrieve data.

Do I need to change anything in my code?

You need to use JedisCluster instead of JedisPool for the discovery of the Redis nodes to happen & make the other relative changes within the codebase.

Note that you only need to reference the configuration endpoint:

Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();

jedisClusterNodes.add(new HostAndPort(CONFIGURATION_ENDPOINT, 6379));

try (JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes)) {
// ...
}
```

Body:
Previously, we were using Redis with cluster mode disabled via AWS ElastiCache.

Our Java code using Jedis was pointing to the primary single node endpoint, which was used for reads and writes.

We've now enabled cluster mode.

We have now changed the code to point to the configuration endpoint of the new Redis cluster however it is now throwing errors whenever receiving requests, see below:

Redis Unavailable. Continue using the Queue requestMessage instead.
org.springframework.data.redis.ClusterRedirectException: Redirect: slot 2356 to [ipaddress]:6379.; nested exception is redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 2356 [ipaddress]:6379

Our configuration code is as below:

    @Bean(name = "redisTemplate")
    public RedisTemplate<String, String> getRedisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(jedisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    JedisConnectionFactory jedisConnectionFactory(Configuration config) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(config.get(HOST));
        jedisConnectionFactory.setPort(config.getInt(PORT));
        jedisConnectionFactory.setUsePool(true);
        jedisConnectionFactory.setPoolConfig(createJedisPoolConfig(config));
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    JedisPoolConfig createJedisPoolConfig(Config config) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(config.getInt(MAX, 8));
        poolConfig.setMaxIdle(config.getInt(MAXIDLE, 8));
        poolConfig.setMinIdle(config.getInt(MINIDLE, 1));
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        return poolConfig;
    }

Shouldn't just changing Jedis to point to the configuration endpoint be enough?

Do I need to change anything in my code?


问:
Override web Firebase cloud messaging background notification with custom behavior
I have a Firebase web project with a service worker that handles background notifications. By default, Firebase displays any background notifications sent to the user. I want to prevent this from happening, so I can show a different message based off the received message. I tried doing this:

```<code>import { FirebaseOptions, initializeApp } from 'firebase/app';
import { getMessaging, onBackgroundMessage } from 'firebase/messaging/sw';

const firebaseOptions: FirebaseOptions = {
    // Firebase config
};

initializeApp(firebaseOptions);

const messaging = getMessaging();

onBackgroundMessage(messaging, () => {
    self.registration.showNotification('Overriden message');
});
</code>```

Right now, the device shows both the message received from the server, and the overridden message. I would like it to only show the overridden message so I can customize the behavior. How can I do this?

答:
Apparently "notification" fields in your HTTP send request take precedence over any values specified in the service worker (source)

Try encapsulating the notification field in your request with a data field so that

```<code>"notification": {
  "title": "hello",
  "body": "world"
}
</code>```

Becomes:

```<code>"data": {
  "notification": {
    "title": "hello",
    "body": "world"
  }
}
</code>```

If that doesn't work for you, there are some other workarounds you can try here.

Body:
I have a Firebase web project with a service worker that handles background notifications. By default, Firebase displays any background notifications sent to the user. I want to prevent this from happening, so I can show a different message based off the received message. I tried doing this:

import { FirebaseOptions, initializeApp } from 'firebase/app';
import { getMessaging, onBackgroundMessage } from 'firebase/messaging/sw';

const firebaseOptions: FirebaseOptions = {
    // Firebase config
};

initializeApp(firebaseOptions);

const messaging = getMessaging();

onBackgroundMessage(messaging, () => {
    self.registration.showNotification('Overriden message');
});

Right now, the device shows both the message received from the server, and the overridden message. I would like it to only show the overridden message so I can customize the behavior. How can I do this?

Body:
Apparently "notification" fields in your HTTP send request take precedence over any values specified in the service worker (source)

Try encapsulating the notification field in your request with a data field so that

"notification": {
  "title": "hello",
  "body": "world"
}

Becomes:

"data": {
  "notification": {
    "title": "hello",
    "body": "world"
  }
}

If that doesn't work for you, there are some other workarounds you can try here.


问:
Neural Networks Using Python and NumPy
I am newbie to NN and I am trying to implement NN with Python/Numpy from the code I found at:
"Create a Simple Neural Network in Python from Scratch"
enter link description here

My input array is:

```<code>array([[5.71, 5.77, 5.94],
   [5.77, 5.94, 5.51],
   [5.94, 5.51, 5.88],
   [5.51, 5.88, 5.73]])
</code>```

Output array is:

```<code>array([[5.51],
   [5.88],
   [5.73],
   [6.41]])
</code>```

after running the code, I see following results which are not correct:

```<code>synaptic_weights after training
[[1.90625275]
[2.54867698]
[1.07698312]]
outputs after training
[[1.]
[1.]
[1.]
[1.]]
</code>```

Here is the core of the code:

```<code>for iteration in range(1000):
    input_layer = tr_input
    outputs = sigmoid(np.dot(input_layer, synapic_weights))

    error = tr_output - outputs

    adjustmnets = error * sigmoid_derivative(outputs)

    synapic_weights +=np.dot(input_layer.T, adjustmnets )

print('synaptic_weights after training')
print(synapic_weights)

print('outputs after training')
print(outputs)
</code>```

What should I change in this code so it works for my data? Or shall I take different method? Any help is highly appreciated.

答:
These are the steps involved in my neural network implementation.

Randomly initialize weights (θ Theta)

Implement forward propagation

Compute cost function

Implement back propagation to compute partial derivative

Use gradient descent

```<code>def forward_prop(X, theta_list):

    m = X.shape[0]
    a_list = []
    z_list = []

    a_list.append(np.insert(X, 0, values=np.ones(m), axis=1))

    idx = 0
    for idx, thera in enumerate(theta_list):
        z_list.append(a_list[idx] * (theta_list[idx].T))
        if idx != (len(theta_list)-1):
            a_list.append(np.insert(sigmoid(z_list[idx]), 0, values=np.ones(m), axis=1))
        else:
            a_list.append(sigmoid(z_list[idx]))

    return a_list, z_list
</code>```
```<code>def back_prop(params, input_size, hidden_layers, num_labels, X, y, regularization, regularize):

    m = X.shape[0]
    X = np.matrix(X)
    y = np.matrix(y)

    theta_list = []
    startCount = 0
    idx = 0
    for idx, val in enumerate(hidden_layers):
        if idx == 0:
            startCount = val * (input_size + 1)
            theta_list.append(np.matrix(np.reshape(params[:startCount], (val, (input_size + 1)))))
        if idx != 0:
            tempCount = startCount
            startCount += (val * (hidden_layers[idx-1] + 1))
            theta_list.append(np.matrix(np.reshape(params[tempCount:startCount], (val, (hidden_layers[idx-1] + 1)))))
        if idx == (len(hidden_layers)-1):
            theta_list.append(np.matrix(np.reshape(params[startCount:], (num_labels, (val + 1)))))

    a_list, z_list= forward_prop(X, theta_list)
    J = cost(X, y, a_list[len(a_list)-1], theta_list, regularization, regularize)

    d_list = []
    d_list.append(a_list[len(a_list)-1] - y)

    idx = 0
    while idx < (len(theta_list)-1):
        d_temp = np.multiply(d_list[idx] * theta_list[len(a_list) - 2 - idx], sigmoid_gradient(a_list[len(a_list) - 2 - idx]))
        d_list.append(d_temp[:,1:])
        idx += 1

    delta_list = []
    for theta in theta_list:
        delta_list.append(np.zeros(theta.shape))

    for idx, delta in enumerate(delta_list):
        delta_list[idx] = delta_list[idx] + ((d_list[len(d_list) - 1 -idx].T) * a_list[idx])
        delta_list[idx] = delta_list[idx] / m

    if regularize:
        for idx, delta in enumerate(delta_list):
            delta_list[idx][:, 1:] = delta_list[idx][:, 1:] + (theta_list[idx][:, 1:] * regularization)

    grad_list = np.ravel(delta_list[0])
    idx = 1
    while idx < (len(delta_list)):
        grad_list = np.concatenate((grad_list, np.ravel(delta_list[idx])), axis=None)
        idx += 1

    return J, grad_list
</code>```
```<code>def cost(X, y, h, theta_list, regularization, regularize):

    m = X.shape[0]
    X = np.matrix(X)
    y = np.matrix(y)

    J = (np.multiply(-y, np.log(h)) - np.multiply((1 - y), np.log(1 - h))).sum() / m

    if regularize:
        regularization_value = 0.0
        for theta in theta_list:
            regularization_value += np.sum(np.power(theta[:, 1:], 2))
        J += (float(regularization) / (2 * m)) * regularization_value

    return J
</code>```

Implementation

Title: Neural Networks Using Python and NumPy

Body:
I am newbie to NN and I am trying to implement NN with Python/Numpy from the code I found at:
"Create a Simple Neural Network in Python from Scratch"
enter link description here

My input array is:

array([[5.71, 5.77, 5.94],
   [5.77, 5.94, 5.51],
   [5.94, 5.51, 5.88],
   [5.51, 5.88, 5.73]])

Output array is:

array([[5.51],
   [5.88],
   [5.73],
   [6.41]])

after running the code, I see following results which are not correct:

synaptic_weights after training
[[1.90625275]
[2.54867698]
[1.07698312]]
outputs after training
[[1.]
[1.]
[1.]
[1.]]

Here is the core of the code:

for iteration in range(1000):
    input_layer = tr_input
    outputs = sigmoid(np.dot(input_layer, synapic_weights))

    error = tr_output - outputs

    adjustmnets = error * sigmoid_derivative(outputs)

    synapic_weights +=np.dot(input_layer.T, adjustmnets )

print('synaptic_weights after training')
print(synapic_weights)

print('outputs after training')
print(outputs)

What should I change in this code so it works for my data? Or shall I take different method? Any help is highly appreciated.

Body:
These are the steps involved in my neural network implementation.

Randomly initialize weights (θ Theta)

Implement forward propagation

Compute cost function

Implement back propagation to compute partial derivative

Use gradient descent

def forward_prop(X, theta_list):

    m = X.shape[0]
    a_list = []
    z_list = []

    a_list.append(np.insert(X, 0, values=np.ones(m), axis=1))

    idx = 0
    for idx, thera in enumerate(theta_list):
        z_list.append(a_list[idx] * (theta_list[idx].T))
        if idx != (len(theta_list)-1):
            a_list.append(np.insert(sigmoid(z_list[idx]), 0, values=np.ones(m), axis=1))
        else:
            a_list.append(sigmoid(z_list[idx]))

    return a_list, z_list

def back_prop(params, input_size, hidden_layers, num_labels, X, y, regularization, regularize):

    m = X.shape[0]
    X = np.matrix(X)
    y = np.matrix(y)

    theta_list = []
    startCount = 0
    idx = 0
    for idx, val in enumerate(hidden_layers):
        if idx == 0:
            startCount = val * (input_size + 1)
            theta_list.append(np.matrix(np.reshape(params[:startCount], (val, (input_size + 1)))))
        if idx != 0:
            tempCount = startCount
            startCount += (val * (hidden_layers[idx-1] + 1))
            theta_list.append(np.matrix(np.reshape(params[tempCount:startCount], (val, (hidden_layers[idx-1] + 1)))))
        if idx == (len(hidden_layers)-1):
            theta_list.append(np.matrix(np.reshape(params[startCount:], (num_labels, (val + 1)))))

    a_list, z_list= forward_prop(X, theta_list)
    J = cost(X, y, a_list[len(a_list)-1], theta_list, regularization, regularize)

    d_list = []
    d_list.append(a_list[len(a_list)-1] - y)

    idx = 0
    while idx < (len(theta_list)-1):
        d_temp = np.multiply(d_list[idx] * theta_list[len(a_list) - 2 - idx], sigmoid_gradient(a_list[len(a_list) - 2 - idx]))
        d_list.append(d_temp[:,1:])
        idx += 1

    delta_list = []
    for theta in theta_list:
        delta_list.append(np.zeros(theta.shape))

    for idx, delta in enumerate(delta_list):
        delta_list[idx] = delta_list[idx] + ((d_list[len(d_list) - 1 -idx].T) * a_list[idx])
        delta_list[idx] = delta_list[idx] / m

    if regularize:
        for idx, delta in enumerate(delta_list):
            delta_list[idx][:, 1:] = delta_list[idx][:, 1:] + (theta_list[idx][:, 1:] * regularization)

    grad_list = np.ravel(delta_list[0])
    idx = 1
    while idx < (len(delta_list)):
        grad_list = np.concatenate((grad_list, np.ravel(delta_list[idx])), axis=None)
        idx += 1

    return J, grad_list

def cost(X, y, h, theta_list, regularization, regularize):

    m = X.shape[0]
    X = np.matrix(X)
    y = np.matrix(y)

    J = (np.multiply(-y, np.log(h)) - np.multiply((1 - y), np.log(1 - h))).sum() / m

    if regularize:
        regularization_value = 0.0
        for theta in theta_list:
            regularization_value += np.sum(np.power(theta[:, 1:], 2))
        J += (float(regularization) / (2 * m)) * regularization_value

    return J

Implementation


问:
Is there any specific way to convert [3] [1, 2] to 3 1 2 in python 3
I am performing the zig zag level order traversal of the binary tree.

```<code>def zigZagTraversal(self, root):
    if root is None:
        return []
    s1=[root]
    s2=[]
    level=[]
    res=[]
    while s1 or s2:
        while s1:
            root=s1.pop()
            level.append(root.data)
            #first move from left ot right
            if root.left!=None:
                s2.append(root.left)
            if root.right!=None:
                s2.append(root.right)

        res.append(level)
        level=[]
        #now we will run the while loop for the other stacks
        while s2:
            root=s2.pop()
            level.append(root.data)
            #here move from right to left
            if root.right!=None:
                s1.append(root.right)
            if root.left!=None:
                s1.append(root.left)
        if level!=[]:
            res.append(level)
            level=[]
    return res
</code>```

My output  [3] [1, 2]
**expected output ** 3 1 2

答:
You could write a loop to flatten the list:

```<code>res = [[3],[1,2]]
for i,_ in enumerate(res):
    while isinstance(res[i],list):
        res[i:i+1] = res[i]

print(*res)
3 1 2
</code>```

alternatively you could extend res instead of appending a sub-list in your own code:

```<code>res.extend(level) # instead of res.append(level)
</code>```

Body:
I am performing the zig zag level order traversal of the binary tree.

def zigZagTraversal(self, root):
    if root is None:
        return []
    s1=[root]
    s2=[]
    level=[]
    res=[]
    while s1 or s2:
        while s1:
            root=s1.pop()
            level.append(root.data)
            #first move from left ot right
            if root.left!=None:
                s2.append(root.left)
            if root.right!=None:
                s2.append(root.right)

        res.append(level)
        level=[]
        #now we will run the while loop for the other stacks
        while s2:
            root=s2.pop()
            level.append(root.data)
            #here move from right to left
            if root.right!=None:
                s1.append(root.right)
            if root.left!=None:
                s1.append(root.left)
        if level!=[]:
            res.append(level)
            level=[]
    return res

My output  [3] [1, 2]
**expected output ** 3 1 2

Body:
You could write a loop to flatten the list:

res = [[3],[1,2]]
for i,_ in enumerate(res):
    while isinstance(res[i],list):
        res[i:i+1] = res[i]

print(*res)
3 1 2

alternatively you could extend res instead of appending a sub-list in your own code:

res.extend(level) # instead of res.append(level)


问:
How to allocate struct with ANYSIZE_ARRAY?
I'm working on HID Device API in Rust. I'm stuck on trying to get device path using Windows API.

I'm using the winapi crate function SetupDiGetDeviceInterfaceDetailA that takes pointer to struct SP_DEVICE_INTERFACE_DETAIL_DATA_A, which should be populated with device path, however the DevicePath field is an array of fixed size 1 (ANYSIZE_ARRAY).

STRUCT! {
    #[cfg_attr(target_arch = "x86", repr(packed))]
    struct SP_DEVICE_INTERFACE_DETAIL_DATA_A {
        cbSize: DWORD,
        DevicePath: [CHAR; ANYSIZE_ARRAY],
    }
}
```

GetLastError returns ERROR_INSUFFICIENT_BUFFER which totally makes sense.

What is proper way of allocating memory for a struct in such case ?

答:
Not sure about ‘proper’, but then Rust does not exactly have good support for dynamically-sized structures; this may change when work on DynSized (which in its current state can be described as ‘not fully defined’) is finalized.  For now, expect lots of unsafe.

Going by Microsoft’s documentation for SetupDiGetDeviceInterfaceDetailA:

Using this function to get details about an interface is typically a two-step process:

Get the required buffer size. Call SetupDiGetDeviceInterfaceDetail with a NULL DeviceInterfaceDetailData pointer, a DeviceInterfaceDetailDataSize of zero, and a valid RequiredSize variable. In response to such a call, this function returns the required buffer size at RequiredSize and fails with GetLastError returning ERROR_INSUFFICIENT_BUFFER.

Allocate an appropriately sized buffer and call the function again to get the interface details.

As such, first you need to query the size you need to allocate for the structure.

use std::{ptr, mem};

let required_size = {
    let mut required_size = mem::MaybeUninit::uninit();

    let ok = SetupDiGetDeviceInterfaceDetailA(
        device_info_set,
        device_iface_data,
        ptr::null_mut(),
        0,
        required_size.as_mut_ptr(),
        ptr::null_mut()
    );

    assert!(!ok && GetLastError() == ERROR_INSUFFICIENT_BUFFER);

    required_size.assume_init()
};
```
<p>Then, allocate the buffer to contain the structure.  Below, I use <code>Vec<u8></code>.  Because <code>Vec</code> is not guaranteed to allocate memory to any given alignment, I take account of that by adding some padding and offsetting the pointer.  (In practice, I would expect the offset to be zero most of the time, but I cannot rely on that.)</p>
<pre class="lang-rust prettyprint-override"><code>let mut buf: Vec<u8> = Vec::with_capacity(
    TryInto::<usize>::try_into(required_size).unwrap() +
    mem::align_of::<SP_DEVICE_INTERFACE_DETAIL_DATA_A>() - 1);

let align_offset =
    buf.as_mut_ptr().align_offset(
        mem::align_of::<SP_DEVICE_INTERFACE_DETAIL_DATA_A>());

assert!(
    align_offset < (buf.capacity() -
        TryInto::<usize>::try_into(required_size).unwrap())
);

let device_iface_detail = unsafe {
    &mut *(buf.as_mut_ptr().offset(align_offset.try_into().unwrap())
        as *mut mem::MaybeUninit<SP_DEVICE_INTERFACE_DETAIL_DATA_A>)
};
</code>```

After allocating, fill the buffer with initial data:

device_iface_detail.write(SP_DEVICE_INTERFACE_DETAIL_DATA_A {
    cbSize: mem::size_of::<SP_DEVICE_INTERFACE_DETAIL_DATA_A>().try_into().unwrap(),
    DevicePath: [0],
});
```
<p>Now call <code>SetupDiGetDeviceInterfaceDetailA</code> again to actually obtain the data you require:</p>
<pre class="lang-rust prettyprint-override"><code>let ok = unsafe {
    SetupDiGetDeviceInterfaceDetailA(
        device_info_set,
        device_iface_data,
        device_iface_detail.as_mut_ptr(),
        required_size,
        ptr::null_mut(),
        ptr::null_mut() // XXX: or actually put a pointer to a structure here
    )
}
</code>```

Finally, extract the device path from the buffer:

if ok {
    let path = unsafe {
        std::ffi::CStr::from_ptr(
            ptr::addr_of!(device_iface_detail.assume_init_ref().DevicePath)
            as *const std::os::raw::c_char
        )
    };

    /* ... use path and devinfo_data.assume_init_mut() ... */
}
```

The path will be usable as long as buf stays in scope.  If you want to keep it for longer, you will need to copy it into a CString or some other owning container.

Body:
I'm working on HID Device API in Rust. I'm stuck on trying to get device path using Windows API.

I'm using the winapi crate function SetupDiGetDeviceInterfaceDetailA that takes pointer to struct SP_DEVICE_INTERFACE_DETAIL_DATA_A, which should be populated with device path, however the DevicePath field is an array of fixed size 1 (ANYSIZE_ARRAY).

STRUCT! {
    #[cfg_attr(target_arch = "x86", repr(packed))]
    struct SP_DEVICE_INTERFACE_DETAIL_DATA_A {
        cbSize: DWORD,
        DevicePath: [CHAR; ANYSIZE_ARRAY],
    }
}

GetLastError returns ERROR_INSUFFICIENT_BUFFER which totally makes sense.

What is proper way of allocating memory for a struct in such case ?

Body:
Not sure about ‘proper’, but then Rust does not exactly have good support for dynamically-sized structures; this may change when work on DynSized (which in its current state can be described as ‘not fully defined’) is finalized.  For now, expect lots of unsafe.

Going by Microsoft’s documentation for SetupDiGetDeviceInterfaceDetailA:

Using this function to get details about an interface is typically a two-step process:

Get the required buffer size. Call SetupDiGetDeviceInterfaceDetail with a NULL DeviceInterfaceDetailData pointer, a DeviceInterfaceDetailDataSize of zero, and a valid RequiredSize variable. In response to such a call, this function returns the required buffer size at RequiredSize and fails with GetLastError returning ERROR_INSUFFICIENT_BUFFER.

Allocate an appropriately sized buffer and call the function again to get the interface details.

As such, first you need to query the size you need to allocate for the structure.

use std::{ptr, mem};

let required_size = {
    let mut required_size = mem::MaybeUninit::uninit();

    let ok = SetupDiGetDeviceInterfaceDetailA(
        device_info_set,
        device_iface_data,
        ptr::null_mut(),
        0,
        required_size.as_mut_ptr(),
        ptr::null_mut()
    );

    assert!(!ok && GetLastError() == ERROR_INSUFFICIENT_BUFFER);

    required_size.assume_init()
};

Then, allocate the buffer to contain the structure.  Below, I use Vec<u8>.  Because Vec is not guaranteed to allocate memory to any given alignment, I take account of that by adding some padding and offsetting the pointer.  (In practice, I would expect the offset to be zero most of the time, but I cannot rely on that.)

let mut buf: Vec<u8> = Vec::with_capacity(
    TryInto::<usize>::try_into(required_size).unwrap() +
    mem::align_of::<SP_DEVICE_INTERFACE_DETAIL_DATA_A>() - 1);

let align_offset =
    buf.as_mut_ptr().align_offset(
        mem::align_of::<SP_DEVICE_INTERFACE_DETAIL_DATA_A>());

assert!(
    align_offset < (buf.capacity() -
        TryInto::<usize>::try_into(required_size).unwrap())
);

let device_iface_detail = unsafe {
    &mut *(buf.as_mut_ptr().offset(align_offset.try_into().unwrap())
        as *mut mem::MaybeUninit<SP_DEVICE_INTERFACE_DETAIL_DATA_A>)
};

After allocating, fill the buffer with initial data:

device_iface_detail.write(SP_DEVICE_INTERFACE_DETAIL_DATA_A {
    cbSize: mem::size_of::<SP_DEVICE_INTERFACE_DETAIL_DATA_A>().try_into().unwrap(),
    DevicePath: [0],
});

Now call SetupDiGetDeviceInterfaceDetailA again to actually obtain the data you require:

let ok = unsafe {
    SetupDiGetDeviceInterfaceDetailA(
        device_info_set,
        device_iface_data,
        device_iface_detail.as_mut_ptr(),
        required_size,
        ptr::null_mut(),
        ptr::null_mut() // XXX: or actually put a pointer to a structure here
    )
}

Finally, extract the device path from the buffer:

if ok {
    let path = unsafe {
        std::ffi::CStr::from_ptr(
            ptr::addr_of!(device_iface_detail.assume_init_ref().DevicePath)
            as *const std::os::raw::c_char
        )
    };

    /* ... use path and devinfo_data.assume_init_mut() ... */
}

The path will be usable as long as buf stays in scope.  If you want to keep it for longer, you will need to copy it into a CString or some other owning container.