问:
how to scrape data from shopee using beautiful soup
I'm currently a student where currently I studied beautifulsoup so my lecturer as me to scrape data from shopee however I cannot scrape the details of the products. Currently, I'm trying to scrape data from https://shopee.com.my/shop/13377506/search?page=0&sortBy=sales. I only want to scrape the name and price of the products. can someone tell me why I cannot scrape the data using beautifulsoup ?

Here is my code:

```<code>from requests import get
from bs4 import BeautifulSoup

url = "https://shopee.com.my/shop/13377506/search?page=0&sortBy=sales"
response= get (url)
soup=BeautifulSoup(response.text,'html.parser')
print (soup)
</code>```

答:
This question is a bit tricky (for python beginners) because it involves a combination of selenium (for headless browsing) and beautifulsoup (for html data extraction). Moreover, the problem becomes difficult because the Document Object Model (DOM) is encased within javascripting. We know javascript is there because we get an empty response from the website when accessed only using beautifulsoup, like, for item_n in soup.find_all('div', class_='_1NoI8_ _16BAGk'):
            print(item_n.get_text())

Therefore, to extract data from such a webpage which has a scripting language controlling its DOM, we have to use selenium for headless browsing (this tells the website that a browser is accessing it). We also have to use some sort of delay parameter, (which tells the website that it's accessed by a human). For this, the function WebdriverWait() from the selenium library will help.

I now present snippets of code that explain the process.

First, import the requisite libraries

```<code>from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from time import sleep
</code>```

Next, initialize the settings for the headless browser. I'm using chrome.

```<code># create object for chrome options
chrome_options = Options()
base_url = 'https://shopee.com.my/shop/13377506/search?page=0&sortBy=sales'

# set chrome driver options to disable any popup's from the website
# to find local path for chrome profile, open chrome browser
# and in the address bar type, "chrome://version"
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('start-maximized')
chrome_options.add_argument('user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default')
# To disable the message, "Chrome is being controlled by automated test software"
chrome_options.add_argument("disable-infobars")
# Pass the argument 1 to allow and 2 to block
chrome_options.add_experimental_option("prefs", {
    "profile.default_content_setting_values.notifications": 2
    })
# invoke the webdriver
browser = webdriver.Chrome(executable_path = r'C:/Users/username/Documents/playground_python/chromedriver.exe',
                          options = chrome_options)
browser.get(base_url)
delay = 5 #secods
</code>```

Next, I declare empty list variables to hold the data.

```<code># declare empty lists
item_cost, item_init_cost, item_loc = [],[],[]
item_name, items_sold, discount_percent = [], [], []
while True:
    try:
        WebDriverWait(browser, delay)
        print ("Page is ready")
        sleep(5)
        html = browser.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
        #print(html)
        soup = BeautifulSoup(html, "html.parser")

        # find_all() returns an array of elements.
        # We have to go through all of them and select that one you are need. And than call get_text()
        for item_n in soup.find_all('div', class_='_1NoI8_ _16BAGk'):
            print(item_n.get_text())
            item_name.append(item_n.text)

        # find the price of items
        for item_c in soup.find_all('span', class_='_341bF0'):
            print(item_c.get_text())
            item_cost.append(item_c.text)

        # find initial item cost
        for item_ic in soup.find_all('div', class_ = '_1w9jLI QbH7Ig U90Nhh'):
            print(item_ic.get_text())
            item_init_cost.append(item_ic.text)
        # find total number of items sold/month
        for items_s in soup.find_all('div',class_ = '_18SLBt'):
            print(items_s.get_text())
            items_sold.append(item_ic.text)

        # find item discount percent
        for dp in soup.find_all('span', class_ = 'percent'):
            print(dp.get_text())
            discount_percent.append(dp.text)
        # find item location
        for il in soup.find_all('div', class_ = '_3amru2'):
            print(il.get_text())
            item_loc.append(il.text)

        break # it will break from the loop once the specific element will be present.
    except TimeoutException:
        print ("Loading took too much time!-Try again")
</code>```

Thereafter, I use the zip function to combine the different list items.

```<code>rows = zip(item_name, item_init_cost,discount_percent,item_cost,items_sold,item_loc)
</code>```

Finally, I write this data to disc,

```<code>import csv
newFilePath = 'shopee_item_list.csv'
with open(newFilePath, "w") as f:
    writer = csv.writer(f)
    for row in rows:
        writer.writerow(row)
</code>```

As a good practice, its wise to close the headless browser once the task is complete. And so i code it as,

```<code># close the automated browser
browser.close()
</code>```

Result

```<code>Nestle MILO Activ-Go Chocolate Malt Powder (2kg)
NESCAFE GOLD Refill (170g)
Nestle MILO Activ-Go Chocolate Malt Powder (1kg)
MAGGI Hot Cup - Asam Asam Laksa (60g)
MAGGI 2-Minit Curry (79g x 5 Packs x 2)
MAGGI PAZZTA Cheese Macaroni 70g
.......
29.90
21.90
16.48
1.69
8.50
3.15
5.90
.......
RM40.70
RM26.76
RM21.40
RM1.80
RM9.62
........
9k sold/month
2.3k sold/month
1.8k sold/month
1.7k sold/month
.................
27%
18%
23%
6%
.............
Selangor
Selangor
Selangor
Selangor
</code>```

Note to the readers

The OP brought to my attention that the xpath was not working as given in my answer. I checked the website again after 2 days and noticed a strange phenomenon. The class_ attribute of the div class had indeed changed. I found a similar Q. But it did not help much. So for now, I'm concluding the div attributes in the shoppee website can change again. I leave this as an open problem to solve later.

Note to the OP

Ana, the above code will work for just one page i.e., it will work only for the webpage, https://shopee.com.my/shop/13377506/search?page=0&sortBy=sales. I invite you to further enhance your skills by solving how to scrape data for multiple webpages under the sales tag. Your hint is the 1/9 seen on the top right of the this page and/or the 1 2 3 4 5 links at the bottom of the page. Another hint for you is to look at the urljoin in the urlparse library. Hope this should get you started.

Helpful resources

XPATH tutorial

Body:
I'm currently a student where currently I studied beautifulsoup so my lecturer as me to scrape data from shopee however I cannot scrape the details of the products. Currently, I'm trying to scrape data from https://shopee.com.my/shop/13377506/search?page=0&sortBy=sales. I only want to scrape the name and price of the products. can someone tell me why I cannot scrape the data using beautifulsoup ?

Here is my code:

from requests import get
from bs4 import BeautifulSoup

url = "https://shopee.com.my/shop/13377506/search?page=0&sortBy=sales"
response= get (url)
soup=BeautifulSoup(response.text,'html.parser')
print (soup)

Body:
This question is a bit tricky (for python beginners) because it involves a combination of selenium (for headless browsing) and beautifulsoup (for html data extraction). Moreover, the problem becomes difficult because the Document Object Model (DOM) is encased within javascripting. We know javascript is there because we get an empty response from the website when accessed only using beautifulsoup, like, for item_n in soup.find_all('div', class_='_1NoI8_ _16BAGk'):
            print(item_n.get_text())

Therefore, to extract data from such a webpage which has a scripting language controlling its DOM, we have to use selenium for headless browsing (this tells the website that a browser is accessing it). We also have to use some sort of delay parameter, (which tells the website that it's accessed by a human). For this, the function WebdriverWait() from the selenium library will help.

I now present snippets of code that explain the process.

First, import the requisite libraries

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from time import sleep

Next, initialize the settings for the headless browser. I'm using chrome.

# create object for chrome options
chrome_options = Options()
base_url = 'https://shopee.com.my/shop/13377506/search?page=0&sortBy=sales'

# set chrome driver options to disable any popup's from the website
# to find local path for chrome profile, open chrome browser
# and in the address bar type, "chrome://version"
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('start-maximized')
chrome_options.add_argument('user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default')
# To disable the message, "Chrome is being controlled by automated test software"
chrome_options.add_argument("disable-infobars")
# Pass the argument 1 to allow and 2 to block
chrome_options.add_experimental_option("prefs", {
    "profile.default_content_setting_values.notifications": 2
    })
# invoke the webdriver
browser = webdriver.Chrome(executable_path = r'C:/Users/username/Documents/playground_python/chromedriver.exe',
                          options = chrome_options)
browser.get(base_url)
delay = 5 #secods

Next, I declare empty list variables to hold the data.

# declare empty lists
item_cost, item_init_cost, item_loc = [],[],[]
item_name, items_sold, discount_percent = [], [], []
while True:
    try:
        WebDriverWait(browser, delay)
        print ("Page is ready")
        sleep(5)
        html = browser.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
        #print(html)
        soup = BeautifulSoup(html, "html.parser")

        # find_all() returns an array of elements.
        # We have to go through all of them and select that one you are need. And than call get_text()
        for item_n in soup.find_all('div', class_='_1NoI8_ _16BAGk'):
            print(item_n.get_text())
            item_name.append(item_n.text)

        # find the price of items
        for item_c in soup.find_all('span', class_='_341bF0'):
            print(item_c.get_text())
            item_cost.append(item_c.text)

        # find initial item cost
        for item_ic in soup.find_all('div', class_ = '_1w9jLI QbH7Ig U90Nhh'):
            print(item_ic.get_text())
            item_init_cost.append(item_ic.text)
        # find total number of items sold/month
        for items_s in soup.find_all('div',class_ = '_18SLBt'):
            print(items_s.get_text())
            items_sold.append(item_ic.text)

        # find item discount percent
        for dp in soup.find_all('span', class_ = 'percent'):
            print(dp.get_text())
            discount_percent.append(dp.text)
        # find item location
        for il in soup.find_all('div', class_ = '_3amru2'):
            print(il.get_text())
            item_loc.append(il.text)

        break # it will break from the loop once the specific element will be present.
    except TimeoutException:
        print ("Loading took too much time!-Try again")

Thereafter, I use the zip function to combine the different list items.

rows = zip(item_name, item_init_cost,discount_percent,item_cost,items_sold,item_loc)

Finally, I write this data to disc,

import csv
newFilePath = 'shopee_item_list.csv'
with open(newFilePath, "w") as f:
    writer = csv.writer(f)
    for row in rows:
        writer.writerow(row)

As a good practice, its wise to close the headless browser once the task is complete. And so i code it as,

# close the automated browser
browser.close()

Result

Nestle MILO Activ-Go Chocolate Malt Powder (2kg)
NESCAFE GOLD Refill (170g)
Nestle MILO Activ-Go Chocolate Malt Powder (1kg)
MAGGI Hot Cup - Asam Asam Laksa (60g)
MAGGI 2-Minit Curry (79g x 5 Packs x 2)
MAGGI PAZZTA Cheese Macaroni 70g
.......
29.90
21.90
16.48
1.69
8.50
3.15
5.90
.......
RM40.70
RM26.76
RM21.40
RM1.80
RM9.62
........
9k sold/month
2.3k sold/month
1.8k sold/month
1.7k sold/month
.................
27%
18%
23%
6%
.............
Selangor
Selangor
Selangor
Selangor

Note to the readers

The OP brought to my attention that the xpath was not working as given in my answer. I checked the website again after 2 days and noticed a strange phenomenon. The class_ attribute of the div class had indeed changed. I found a similar Q. But it did not help much. So for now, I'm concluding the div attributes in the shoppee website can change again. I leave this as an open problem to solve later.

Note to the OP

Ana, the above code will work for just one page i.e., it will work only for the webpage, https://shopee.com.my/shop/13377506/search?page=0&sortBy=sales. I invite you to further enhance your skills by solving how to scrape data for multiple webpages under the sales tag. Your hint is the 1/9 seen on the top right of the this page and/or the 1 2 3 4 5 links at the bottom of the page. Another hint for you is to look at the urljoin in the urlparse library. Hope this should get you started.

Helpful resources

XPATH tutorial


来源: stackexchange

问:
How can I implement a RESTful API in Perl?
I'm trying to implement a RESTful API in Perl. My current idea is to simply parse the path_info with a regex then dispatch the request to the appropriate subroutine which will then spit out the JSON, XML or even XHTML for the requested resource.

For example to retrieve info about user 1234 the RESTful client should find it at:

http://example.com/model.pl/users/1234

Below is the skeleton code of my first attempt at implementing a RESTful API:

model.pl :

```<code>#!/usr/bin/perl -w
use strict;
use CGI;

my $q = CGI->new();

print $q->header('text/html');

my $restfuluri  = $q->path_info;

if      ($restfuluri =~ /^\/(questions)\/([1-9]+$)/) { questions($1, $2); }
elsif   ($restfuluri =~ /^\/(users)\/([1-9]+$)/)     { users($1, $2); }

sub questions
{
      my $object = shift;
      my $value  = shift;

      #This is a stub, spits out JSON or XML when implemented.
      print $q->p("GET question : $object -> $value");
}

sub users
{
      my $object = shift;
      my $value  = shift;

      #This is a stub, spits out JSON or XML when implemented.
      print $q->p("GET user: $object -> $value");
}
</code>```

Before I proceed any further, I would like to hear from experienced Perl hackers whether I got the basic idea right and if there are any serious shortcomings with this approach in terms of performance.

I can imagine, after a while, the if/else block would grow really large.

Looking forward to hear your views to make this code better.

答:
I know this has been a long time since this question has been asked, but I would like to provide some updated information.

There is a very useful module called Net::API::REST using Apache2 mod_perl behind.

You just need to set up Apache, and create your own module inheriting from Net::API::REST and create an init method in which you define your endpoints map, such as:

```<code>sub init
{
    my $self = shift( @_ );
    $self->{routes} =
    {
    # e.g. your API version 1
    1 =>
        {
        'favicon.ico' => $self->curry::noop,
        auth =>
            {
            google =>
                {
                _handler => $self->curry::oauth_google,
                callback => $self->curry::oauth_google(callback => 1),
                },
            linkedin =>
                {
                _handler => $self->curry::oauth_linkedin,
                callback => $self->curry::oauth_linkedin(callback => 1),
                },
            },
        },
        stripe => $self->curry::stripe,
    };
    $self->{api_version} = 1;
    $self->{supported_api_versions} = [qw( 1 )];
    $self->{default_methods} = [qw( GET POST )];
    $self->{supported_methods} = [qw( DELETE GET HEAD OPTIONS POST PUT )];
    $self->{supported_languages} = [qw( en-GB en fr-FR fr ja-JP )];
    $self->SUPER::init( @_ );
    return( $self );
}
</code>```

And when someone access one of your endpoint, your corresponding method gets called in the context of a powerful environment to use to reply to the request. Check the module metacpan page for more information.

ContentLicense: CC BY-SA 3.0

Body:
I'm trying to implement a RESTful API in Perl. My current idea is to simply parse the path_info with a regex then dispatch the request to the appropriate subroutine which will then spit out the JSON, XML or even XHTML for the requested resource.

For example to retrieve info about user 1234 the RESTful client should find it at:

http://example.com/model.pl/users/1234

Below is the skeleton code of my first attempt at implementing a RESTful API:

model.pl :

#!/usr/bin/perl -w
use strict;
use CGI;

my $q = CGI->new();

print $q->header('text/html');

my $restfuluri  = $q->path_info;

if      ($restfuluri =~ /^\/(questions)\/([1-9]+$)/) { questions($1, $2); }
elsif   ($restfuluri =~ /^\/(users)\/([1-9]+$)/)     { users($1, $2); }

sub questions
{
      my $object = shift;
      my $value  = shift;

      #This is a stub, spits out JSON or XML when implemented.
      print $q->p("GET question : $object -> $value");
}

sub users
{
      my $object = shift;
      my $value  = shift;

      #This is a stub, spits out JSON or XML when implemented.
      print $q->p("GET user: $object -> $value");
}

Before I proceed any further, I would like to hear from experienced Perl hackers whether I got the basic idea right and if there are any serious shortcomings with this approach in terms of performance.

I can imagine, after a while, the if/else block would grow really large.

Looking forward to hear your views to make this code better.

Body:
I know this has been a long time since this question has been asked, but I would like to provide some updated information.

There is a very useful module called Net::API::REST using Apache2 mod_perl behind.

You just need to set up Apache, and create your own module inheriting from Net::API::REST and create an init method in which you define your endpoints map, such as:

sub init
{
    my $self = shift( @_ );
    $self->{routes} =
    {
    # e.g. your API version 1
    1 =>
        {
        'favicon.ico' => $self->curry::noop,
        auth =>
            {
            google =>
                {
                _handler => $self->curry::oauth_google,
                callback => $self->curry::oauth_google(callback => 1),
                },
            linkedin =>
                {
                _handler => $self->curry::oauth_linkedin,
                callback => $self->curry::oauth_linkedin(callback => 1),
                },
            },
        },
        stripe => $self->curry::stripe,
    };
    $self->{api_version} = 1;
    $self->{supported_api_versions} = [qw( 1 )];
    $self->{default_methods} = [qw( GET POST )];
    $self->{supported_methods} = [qw( DELETE GET HEAD OPTIONS POST PUT )];
    $self->{supported_languages} = [qw( en-GB en fr-FR fr ja-JP )];
    $self->SUPER::init( @_ );
    return( $self );
}

And when someone access one of your endpoint, your corresponding method gets called in the context of a powerful environment to use to reply to the request. Check the module metacpan page for more information.

ContentLicense: CC BY-SA 4.0


问:
Chai - Assert that all elements in the array are equal to a given value with
I have this array of strings:

```<code>[ "apple", "apple", "apple", "apple", "apple", "apple", ]
</code>```

Is it possible to make an assertion with Chai that all elements in the array are equal to the certain value?

```<code>arrayFromApiResponse = [ "apple", "apple", "apple", "apple", "apple", "apple", ]
expectedFruit = "apple"

expect(arrayFromApiResponse).to ???
</code>```

I need to test that each value in the arrayFromApiResponse is "apple"

I found this https://github.com/chaijs/Chai-Things

Seems like with this library it can be accomplished like this:

```<code>expect(arrayFromApiResponse).should.all.be.a(expectedFruit)
</code>```

But is it possible to achieve this without additional library? Maybe I can make some changes to arrayFromApiResponse so it can be validated by Chai?

UPD:
I have updated the question title to prevent marking my question as duplicate with reference to this type of questions:
Check if all values of array are equal

答:
You could use the every() method.

const arrayFromApiResponse = [ "apple", "apple", "apple", "apple", "apple", "apple", ]
const expectedFruit = "apple"

const allAreExpectedFruit = arrayFromApiResponse.every(x => x === expectedFruit);

console.log(allAreExpectedFruit);```

Body:
I have this array of strings:

[ "apple", "apple", "apple", "apple", "apple", "apple", ]

Is it possible to make an assertion with Chai that all elements in the array are equal to the certain value?

arrayFromApiResponse = [ "apple", "apple", "apple", "apple", "apple", "apple", ]
expectedFruit = "apple"

expect(arrayFromApiResponse).to ???

I need to test that each value in the arrayFromApiResponse is "apple"

I found this https://github.com/chaijs/Chai-Things

Seems like with this library it can be accomplished like this:

expect(arrayFromApiResponse).should.all.be.a(expectedFruit)

But is it possible to achieve this without additional library? Maybe I can make some changes to arrayFromApiResponse so it can be validated by Chai?

UPD:
I have updated the question title to prevent marking my question as duplicate with reference to this type of questions:
Check if all values of array are equal


问:
There is no tracking information for the current branch
I've been using github from a relatively short period, and I've always used the client to perform commits and pulls. I decided to try it from the git bash yesterday, and I successfully created a new repo and committed files.

Today I did changes to the repository from another computer, I've committed the changes and now I'm back home and performed a git pull to update my local version and I get this:

```<code>There is no tracking information for the current branch.
    Please specify which branch you want to merge with.
    See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream develop origin/<branch>
</code>```

the only contributor to this repo is me and there are no branches (just a master). I'm on windows and I've performed the pull from git bash:

git status:

```<code>$ git status
# On branch master
nothing to commit, working directory clean
</code>```

git branch:

```<code>$ git branch
* master
</code>```

What am I doing wrong?

Body:
I've been using github from a relatively short period, and I've always used the client to perform commits and pulls. I decided to try it from the git bash yesterday, and I successfully created a new repo and committed files.

Today I did changes to the repository from another computer, I've committed the changes and now I'm back home and performed a git pull to update my local version and I get this:

There is no tracking information for the current branch.
    Please specify which branch you want to merge with.
    See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream develop origin/<branch>

the only contributor to this repo is me and there are no branches (just a master). I'm on windows and I've performed the pull from git bash:

git status:

$ git status
# On branch master
nothing to commit, working directory clean

git branch:

$ git branch
* master

What am I doing wrong?

Body: git branch --set-upstream-to=origin/main


问:
StreamException: An invalid XML character (Unicode: 0x1a)
I am using XStream to save the object of user in a file.

```<code>private void store() {
    XStream xStream = new XStream(new DomDriver("UTF-8"));
    xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

    xStream.alias("configuration", Configuration.class);
    xStream.alias("user", User.class);

    synchronized (ConfigurationDAOImpl.class) {
        try {
            xStream.toXML(configuration, new FileOutputStream(filename.getFile()));
        } catch (IOException e) {
            throw new RuntimeException("Failed to write to " + filename, e);
        }
    }
}
</code>```

When I am trying to read it by the following code I get an Exception: com.thoughtworks.xstream.io.StreamException:  : An invalid XML character (Unicode: 0x1a) was found in the element content of the document.

```<code>private void lazyLoad() {
    synchronized (ConfigurationDAOImpl.class) {
        // Has the configuration been loaded
        if (configuration == null) {
            if (filename.exists()) {
                try {
                    XStream xStream = new XStream(new DomDriver("UTF-8"));
                    xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

                    xStream.alias("configuration", Configuration.class);
                    xStream.alias("user", User.class);

                    configuration = (Configuration) xStream
                            .fromXML(filename.getInputStream());

                    LOGGER.debug("Loaded configuration from {}.", filename);
                } catch (Exception e) {
                    LOGGER.error("Failed to load configuration.", e);
                }
            } else {
                LOGGER.debug("{} does not exist.", filename);
                LOGGER.debug("Creating blank configuration.");

                configuration = new Configuration();
                configuration.setUsers(new ArrayList<User>());

                // and store it
                store();
            }
        }
    }
}
</code>```

Any idea?

答:
As already pointed out, XML 1.0 accepts only a set of characters according to this.

Here is a helpful java method to ensure that a string is XML 1.0 conformant, it replaces the invalid ones (all of them not just the 0x1a) with a given replacement.

```<code>public static String replaceInvalidXMLCharacters(String input, String replacement) {
        StringBuffer result = new StringBuffer();
        char currentChar;

        if (input == null || "".equals(input)) {
            return "";
        }
        for (int i = 0; i < input.length(); i++) {
            currentChar = input.charAt(i);
            if (currentChar == 0x9 || currentChar == 0xA || currentChar == 0xD || currentChar >= 0x20 && currentChar <= 0xD7FF || currentChar >= 0xE000
                    && currentChar <= 0xFFFD || currentChar >= 0x10000 && currentChar <= 0x10FFFF) {
                result.append(currentChar);
            } else {
                result.append(replacement);
            }
        }
        return result.toString();
    }
</code>```

Body:
I am using XStream to save the object of user in a file.

private void store() {
    XStream xStream = new XStream(new DomDriver("UTF-8"));
    xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

    xStream.alias("configuration", Configuration.class);
    xStream.alias("user", User.class);

    synchronized (ConfigurationDAOImpl.class) {
        try {
            xStream.toXML(configuration, new FileOutputStream(filename.getFile()));
        } catch (IOException e) {
            throw new RuntimeException("Failed to write to " + filename, e);
        }
    }
}

When I am trying to read it by the following code I get an Exception: com.thoughtworks.xstream.io.StreamException:  : An invalid XML character (Unicode: 0x1a) was found in the element content of the document.

private void lazyLoad() {
    synchronized (ConfigurationDAOImpl.class) {
        // Has the configuration been loaded
        if (configuration == null) {
            if (filename.exists()) {
                try {
                    XStream xStream = new XStream(new DomDriver("UTF-8"));
                    xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

                    xStream.alias("configuration", Configuration.class);
                    xStream.alias("user", User.class);

                    configuration = (Configuration) xStream
                            .fromXML(filename.getInputStream());

                    LOGGER.debug("Loaded configuration from {}.", filename);
                } catch (Exception e) {
                    LOGGER.error("Failed to load configuration.", e);
                }
            } else {
                LOGGER.debug("{} does not exist.", filename);
                LOGGER.debug("Creating blank configuration.");

                configuration = new Configuration();
                configuration.setUsers(new ArrayList<User>());

                // and store it
                store();
            }
        }
    }
}

Any idea?

Body:
As already pointed out, XML 1.0 accepts only a set of characters according to this.

Here is a helpful java method to ensure that a string is XML 1.0 conformant, it replaces the invalid ones (all of them not just the 0x1a) with a given replacement.

public static String replaceInvalidXMLCharacters(String input, String replacement) {
        StringBuffer result = new StringBuffer();
        char currentChar;

        if (input == null || "".equals(input)) {
            return "";
        }
        for (int i = 0; i < input.length(); i++) {
            currentChar = input.charAt(i);
            if (currentChar == 0x9 || currentChar == 0xA || currentChar == 0xD || currentChar >= 0x20 && currentChar <= 0xD7FF || currentChar >= 0xE000
                    && currentChar <= 0xFFFD || currentChar >= 0x10000 && currentChar <= 0x10FFFF) {
                result.append(currentChar);
            } else {
                result.append(replacement);
            }
        }
        return result.toString();
    }


问:
TypeError when reading CBOR file
I am working on a project using the CBOR file to contain data. I already install cbor with pip install cbor. But I cannot read it.

This is my code:

```<code>import cbor
cbor.loads(r"C:\Users\User\Desktop\project\score.cbor")
</code>```

Then its return this error:

```<code>Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\Anaconda3\envs\pyenv\lib\site-packages\cbor\cbor.py", line 263, in loads
    fp = StringIO(data)
TypeError: a bytes-like object is required, not 'str'
</code>```

How to solve this problem? And is there a way to convert CBOR to JSON file because I find it easier to work with the JSON file in Python.

答:
I still don't know how to solve this problem with cbor, I use cbor2 instead and it works.

```<code>import cbor2
path = "home/data/score.cbor"

with open(path, 'rb') as fp:
    obj = cbor2.load(fp)
    print(obj)
</code>```

Output

```<code>{'root': {'probe': 20, 'candidate': 31}, 'tree': [], 'support': []}
</code>```

Title: TypeError when reading CBOR file

Body:
I am working on a project using the CBOR file to contain data. I already install cbor with pip install cbor. But I cannot read it.

This is my code:

import cbor
cbor.loads(r"C:\Users\User\Desktop\project\score.cbor")

Then its return this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\Anaconda3\envs\pyenv\lib\site-packages\cbor\cbor.py", line 263, in loads
    fp = StringIO(data)
TypeError: a bytes-like object is required, not 'str'

How to solve this problem? And is there a way to convert CBOR to JSON file because I find it easier to work with the JSON file in Python.

Body:
I still don't know how to solve this problem with cbor, I use cbor2 instead and it works.

import cbor2
path = "home/data/score.cbor"

with open(path, 'rb') as fp:
    obj = cbor2.load(fp)
    print(obj)

Output

{'root': {'probe': 20, 'candidate': 31}, 'tree': [], 'support': []}


问:
Why does Bunifu controls shows error on this.Close()
Goal:

I have a simple goal, close the form.

Code:

This is Bunifu button:

```<code>private void cancelBtn_Click(object sender, EventArgs e)
{
    this.Close();
}
</code>```

This is normal button:

```<code>private void normal_Click(object sender, EventArgs e)
{
    this.Close();
}
</code>```

Detail:

This is what happens when clicking the normal button:

Form closes without errors.

Whereas, clicking the buniful button shows me this error:

Question:

Why does the Bunifu button throws me this error? I have not got a clue to what the error means. How can it be fixed?

答:
I think that happens because bunifu buttons have some methods after closing the form like change color on hover etc.

You may try this below codes

```<code>private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
bunifuFlatButton1.Enabled = false;
this.Close();
}
</code>```

Body:
Goal:

I have a simple goal, close the form.

Code:

This is Bunifu button:

private void cancelBtn_Click(object sender, EventArgs e)
{
    this.Close();
}

This is normal button:

private void normal_Click(object sender, EventArgs e)
{
    this.Close();
}

Detail:

This is what happens when clicking the normal button:

Form closes without errors.

Whereas, clicking the buniful button shows me this error:

Question:

Why does the Bunifu button throws me this error? I have not got a clue to what the error means. How can it be fixed?

Body:
I think that happens because bunifu buttons have some methods after closing the form like change color on hover etc.

You may try this below codes

private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
bunifuFlatButton1.Enabled = false;
this.Close();
}


问:
Initialize window handle for form and child controls without displaying
Is there a way to programmatically force a form and all of its child controls to have window handles without it being visible?  It looked like CreateControl would do it, but that only seems to work if the control is visible.  Requesting the handle of the form gives the form a handle, but doesn't give handles to all child controls, and I don't really want to iterate through all child controls manually forcing them to have handles.

Currently, I'm resorting to making the form visible for a split second and then hiding it, which seems to be a pretty hacky solution to me.  Is there a better way?

答:
I don't understand why you don't like iterating. It seems like a good solution to me. I'd take the opportunity to build a reusable recursive control iterator.

However, if you don't want to do that then you can try a simple variant on your current solution. Before you make the form visible set its position so that it does not appear on any monitor. Then when you hide it again restore the correct position.


问:
How can multiple files be opened in Visual Studio Code (VSC)?
How can multiple files be opened in Visual Studio Code (VSC)?

For some reason, my copy of Visual Studio Code can only open one file at a time. For example, if there are two files in the workspace folder, let’s say an HTML and CSS file, both cannot be opened simultaneously. If one file is open, clicking any other item in VSC's file explorer will replace the current file. VSC will not open the other file in a new editing tab.

I am not concerned with running multiple instances, Workspaces or folders of VSC. I just want to open two files in the same folder. Is there a configuration setting controlling this? Or is this the result of upgrading to the latest version of VSC? Is just a "new feature" of VSC?

It's seems ridiculously simple, but I need help. I am running Visual Studio Code v. 1.24.0 for Windows 10 64 bit.

Visual Studio Code screenshot:

答:
Go to 'settings' in Visual Studio Code and search for 'preview'. Then on the second position from the top you will find "Workbench › Editor: Enable Preview". Simply untick it and your problem will be solved.

In this GIF you can see it's working properly.


问:
scrapy export into single row
I'm trying to scrape store locations into a csv using scrapy. I'm capturing the right data, but the output looks like this (with "name" field as an example)

csv output

Code:

```<code>import scrapy

from xx.items import xxItem

class QuotesSpider(scrapy.Spider):
    name = 'xx_spider'
    allowed_domains = ['www.my.xx.com']
    start_urls = [
                'https://my.xx.com/storefinder/list/a',
                ]

    def parse(self, response):
        rows = response.css('div.col-md-4.col-sm-6')
        for row in rows:
            item = xxItem()
            item['name'] = rows.css('h3::text').extract()
            item['address'] = rows.css('p::text').extract()

        return item
</code>```

答:
A return statement is used to end the execution of the function call
and “returns” the result (value of the expression following the return
keyword) to the caller.

Reference link.

Hence, when you use return keyword, your code execution stops. Instead, you need to use yield keyword.

What does the “yield” keyword do?

Solution:

Replace statement return item with yield item and move it to the for loop scope:

Code with changes:

```<code>import scrapy

from xx.items import xxItem

class QuotesSpider(scrapy.Spider):
    name = 'xx_spider'
    allowed_domains = ['www.my.xx.com']
    start_urls = [
                'https://my.xx.com/storefinder/list/a',
                ]

    def parse(self, response):
        rows = response.css('div.col-md-4.col-sm-6')
        for row in rows:
            item = xxItem()
            item['name'] = row.css('h3::text').extract()
            item['address'] = row.css('p::text').extract()

            yield item
</code>```

To store data in csv file, run your spider using command:

```<code>scrapy crawl xx_spider -o output_file.csv
</code>```

Hope it helps :)

Title: scrapy export into single row

Body:
I'm trying to scrape store locations into a csv using scrapy. I'm capturing the right data, but the output looks like this (with "name" field as an example)

csv output

Code:

import scrapy

from xx.items import xxItem

class QuotesSpider(scrapy.Spider):
    name = 'xx_spider'
    allowed_domains = ['www.my.xx.com']
    start_urls = [
                'https://my.xx.com/storefinder/list/a',
                ]

    def parse(self, response):
        rows = response.css('div.col-md-4.col-sm-6')
        for row in rows:
            item = xxItem()
            item['name'] = rows.css('h3::text').extract()
            item['address'] = rows.css('p::text').extract()

        return item

Body:
A return statement is used to end the execution of the function call
and “returns” the result (value of the expression following the return
keyword) to the caller.

Reference link.

Hence, when you use return keyword, your code execution stops. Instead, you need to use yield keyword.

What does the “yield” keyword do?

Solution:

Replace statement return item with yield item and move it to the for loop scope:

Code with changes:

import scrapy

from xx.items import xxItem

class QuotesSpider(scrapy.Spider):
    name = 'xx_spider'
    allowed_domains = ['www.my.xx.com']
    start_urls = [
                'https://my.xx.com/storefinder/list/a',
                ]

    def parse(self, response):
        rows = response.css('div.col-md-4.col-sm-6')
        for row in rows:
            item = xxItem()
            item['name'] = row.css('h3::text').extract()
            item['address'] = row.css('p::text').extract()

            yield item

To store data in csv file, run your spider using command:

scrapy crawl xx_spider -o output_file.csv

Hope it helps :)


问:
Flex child of child does not expand to 100%
In CSS, if a child of a child is set to width: 100% and the wrapping div has display: flex set, the content does not expand to 100% it only uses the space of the content.

How would one make it expand to the size the grandchild sets itself but still use flex?

flex-grow is probably not the answer since this will always expand to take up the full space and not respect the size the grandchild sets itself.

See following example:

.wrapperFlex, .wrapperBlock{
  border: 1px solid silver;
}

.wrapperFlex {
  display: flex;
}

.levelOne {
}

.levelOneGrow {
  flex-grow: 1;
}

.levelTwo, .levelTwoFullWidth {
  color: white;
  background-color: blue;
}

.levelTwoFullWidth {
  width: 100%;
}

.levelOnePassthrough{
  display: contents;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><!-- Premise -->
<div class="wrapperFlex">
  <div>PRE</div>
  <div class="levelOne">
    <div class="levelTwoFullWidth">
      WRAPPER FLEX
    </div>
  </div>
  <div>AFTER</div>
</div>
<br/>

<!-- Not what is wanted, the grandchild here does not actually expand to 100%
it should be only as wide as the content here -->
<div class="wrapperFlex">
  <div>PRE</div>
  <div class="levelOneGrow">
    <div class="levelTwo">
      WRAPPER FLEX GROW
    </div>
  </div>
  <div>AFTER</div>
</div>

<br/>

<!-- What is wanted but not possible, display: contents is not commonly available -->
<div class="wrapperFlex">
  <div>PRE</div>
  <div class="levelOnePassthrough">
    <div class="levelTwoFullWidth ">
      WRAPPER FLEX PASSTHROUGH
    </div>
  </div>
  <div>AFTER</div>
</div></code>```

答:
You need to add the flex property as one to the child i.e levelOne like this flex:1;.
It will work properly as you check here.

.wrapperFlex, .wrapperBlock{
  border: 1px solid silver;
}

.wrapperFlex {
  display: flex;
}

.wrapperBlock {
  display: block;
}

.levelOne {
flex:1;
}

.levelTwo {
  color: white;
  width: 100%;
  background-color: blue;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="wrapperFlex">
  <div class="levelOne">
    <div class="levelTwo">
      WRAPPER FLEX
    </div>
  </div>
</div>

<br/>

<div class="wrapperBlock">
  <div class="levelOne">
    <div class="levelTwo">
      WRAPPER BLOCK
    </div>
  </div>
</div></code>```

Body:
In CSS, if a child of a child is set to width: 100% and the wrapping div has display: flex set, the content does not expand to 100% it only uses the space of the content.

How would one make it expand to the size the grandchild sets itself but still use flex?

flex-grow is probably not the answer since this will always expand to take up the full space and not respect the size the grandchild sets itself.

See following example:

.wrapperFlex, .wrapperBlock{
  border: 1px solid silver;
}

.wrapperFlex {
  display: flex;
}

.levelOne {
}

.levelOneGrow {
  flex-grow: 1;
}

.levelTwo, .levelTwoFullWidth {
  color: white;
  background-color: blue;
}

.levelTwoFullWidth {
  width: 100%;
}

.levelOnePassthrough{
  display: contents;
}

PRE

      WRAPPER FLEX

AFTER

PRE

      WRAPPER FLEX GROW

AFTER

PRE

      WRAPPER FLEX PASSTHROUGH

AFTER

Body:
You need to add the flex property as one to the child i.e levelOne like this flex:1;.
It will work properly as you check here.

.wrapperFlex, .wrapperBlock{
  border: 1px solid silver;
}

.wrapperFlex {
  display: flex;
}

.wrapperBlock {
  display: block;
}

.levelOne {
flex:1;
}

.levelTwo {
  color: white;
  width: 100%;
  background-color: blue;
}

<div class="wrapperFlex">
  <div class="levelOne">
    <div class="levelTwo">
      WRAPPER FLEX
    </div>
  </div>
</div>

<br/>

<div class="wrapperBlock">
  <div class="levelOne">
    <div class="levelTwo">
      WRAPPER BLOCK
    </div>
  </div>
</div>


问:
Why Is My Machine Learning Algorithm Getting Stuck?
So I am hitting a wall with my C# Machine Learning project.  I am attempting to train an algorithm to recognize numbers.  Since this is only an exercise I have a image set of 200 numbers (20 each for 0 to 9).  Obviously if I wanted a properly trained algorithm I would use a more robust training set, but this is just an exercise to see if I can get it working in the first place.  I can get it up to 60% accuracy, but not past that.  I have been doing some research into activation functions and I from what I understand, LeakyRelu is the function I should be using.  However, if I use the LeakyRelu function across the board then it doesn't learn anything, and I'm not sure how to use the LeakyRelu as an output activation function.  Using sigmoid or tanh as an output activation function makes more sense to me.  Here is a block of code that creates the array that feeds the backpropagation:

```<code>public static float ACTIVE_VALUE = 1;
public static float INACTIVE_VALUE = -1;

// This is specifically designed for a algorithm that will detect a number between 0 - 9
public static float[] valueToArray(int value)
{

    switch (value)
    {
        case 0:
            return new float[] { ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 1:
            return new float[] { INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 2:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 3:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 4:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 5:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 6:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 7:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 8:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE };
        case 9:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE };
        default:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };

    }
}
</code>```

I don't know how to use something like this to read a LeakyRelu output.  So I figured the best option would be to use LeakyRelu for the input and hidden layers and then use tanh or sigmoid for the output layer.  However that creates an issue, because sigmoid just returns NAN (due to a rounding error from what I understand) and tanh returns -1 or 1 but nothing in between.  If I use tanh across the board it works, and it learns, but it only reaches an accuracy of 60% then stops developing there. I assume this is due to the "vanishing gradient" issue.  However, If I use LeakyRelu for inpunt and hidden layers and then tanh for the output, it stays at 12-14% (which is just as good as randomly guessing a number).

I am using a neural network that I got from a github user here:
https://github.com/kipgparker/BackPropNetwork

He posted a research paper online about neural networks, and it was one of the top hits on google.  That's how I found it in the first place.  I posted my full project in a zip on GitHub here:
https://github.com/JoshuaC0352/Machine-Learning

I am not opposed to using a library I can get from nuget like SiaNet (https://scisharp.github.io/SiaNet/api/SiaNet.Layers.AvgPooling1D.html), however I have gotten so familiar with the one I am currently working with I am somewhat reluctant to switch over because I'd feel like I am almost starting from scratch, because I would have to learn how to interface with a whole new library.

EDIT: additional code.  This is my while loop that reads the image and trains the algorithm:

```<code>    public static void singleThread()
{

    int batchSize = 10000;
    int rangeLow = 0;
    int rangeHi = 9;

    int hits = 0;

    while (true)
    {

        // alternates between training and testing
        //Console.WriteLine("Training...  ");

        for (int i = 0; i < batchSize; i++)
        {

            // Give a training progress report every 100 iterations, this should increase performance
            if (i % 100 == 0)
            {
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.Write("Training: ");
                Console.Write("(" + (((float)i / (float)batchSize) * 100) + "%)");
                Console.Write("                    ");
            }

            // randomly select an image from the list
            int number = rng.Next(rangeLow, rangeHi);
            int index = rng.Next(1, 20);

            Bitmap loadedImage = (Bitmap)Image.FromFile("Train/" + number + "/" +
                                 index + ".png", true);

            int indexLocation = 0;
            // Convert the image into a grayScale value
            for (int x = 0; x < loadedImage.Width; x++)
            {
                for (int y = 0; y < loadedImage.Height; y++)
                {
                    Color pixel = loadedImage.GetPixel(x, y);
                    int grayValue = (int)((pixel.R * 0.3) + (pixel.G * 0.59) + (pixel.B * 0.11));
                    //Console.WriteLine(grayValue);
                    networkInputs[indexLocation] = grayValue;
                    indexLocation++;
                }
            }

            // The network will guess what the image is, and return the guess as a float array

            float[] guess = currentNetwork.BackPropagate(networkInputs, Interface.valueToArray(number));

            // This if statement checks if the guess was correct
            if (Interface.guessToValue(guess) == number)
            {
                hits++;
            }

        }

        currentNetwork.Performance = ((float) hits / (float) batchSize);
        hits = 0;

        Console.WriteLine("Score: " + (currentNetwork.Performance * 100) + "%");
    }
}
</code>```

答:
added the answer for future visitors

Try converting the grayscale values from 0-255 interval to 0-1
interval. Just divide each pixel with 255. the fact that LeakyRELU
performed better than sigmoid or tanh is because the values are too
large. large in a sense that they get mistreated by tanh and sigmoid
and get rounded by the computer to integers.

Look carefully about how the neural network weights are
initialised if you intend to use tanh or sigmoid.

Since this is a classification problem, I recommend you use a softmax
activation function in your output layer.

after preprocessing the data, @JMC0352 got 88% accuracy only.

the reason why you're getting 88% only is because neural network (alone) is not suitable for image recognition. convolutional neural networks are used for that matter. In order to understand the problem intuitively, you can picture raw neural networks as making sense of all the pixels together, where as conv. nets. make sense of relatively close pixels.

Body:
So I am hitting a wall with my C# Machine Learning project.  I am attempting to train an algorithm to recognize numbers.  Since this is only an exercise I have a image set of 200 numbers (20 each for 0 to 9).  Obviously if I wanted a properly trained algorithm I would use a more robust training set, but this is just an exercise to see if I can get it working in the first place.  I can get it up to 60% accuracy, but not past that.  I have been doing some research into activation functions and I from what I understand, LeakyRelu is the function I should be using.  However, if I use the LeakyRelu function across the board then it doesn't learn anything, and I'm not sure how to use the LeakyRelu as an output activation function.  Using sigmoid or tanh as an output activation function makes more sense to me.  Here is a block of code that creates the array that feeds the backpropagation:

public static float ACTIVE_VALUE = 1;
public static float INACTIVE_VALUE = -1;

// This is specifically designed for a algorithm that will detect a number between 0 - 9
public static float[] valueToArray(int value)
{

    switch (value)
    {
        case 0:
            return new float[] { ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 1:
            return new float[] { INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 2:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 3:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 4:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 5:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 6:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 7:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };
        case 8:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE, INACTIVE_VALUE };
        case 9:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, ACTIVE_VALUE };
        default:
            return new float[] { INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE,
                                INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE, INACTIVE_VALUE };

    }
}

I don't know how to use something like this to read a LeakyRelu output.  So I figured the best option would be to use LeakyRelu for the input and hidden layers and then use tanh or sigmoid for the output layer.  However that creates an issue, because sigmoid just returns NAN (due to a rounding error from what I understand) and tanh returns -1 or 1 but nothing in between.  If I use tanh across the board it works, and it learns, but it only reaches an accuracy of 60% then stops developing there. I assume this is due to the "vanishing gradient" issue.  However, If I use LeakyRelu for inpunt and hidden layers and then tanh for the output, it stays at 12-14% (which is just as good as randomly guessing a number).

I am using a neural network that I got from a github user here:
https://github.com/kipgparker/BackPropNetwork

He posted a research paper online about neural networks, and it was one of the top hits on google.  That's how I found it in the first place.  I posted my full project in a zip on GitHub here:
https://github.com/JoshuaC0352/Machine-Learning

I am not opposed to using a library I can get from nuget like SiaNet (https://scisharp.github.io/SiaNet/api/SiaNet.Layers.AvgPooling1D.html), however I have gotten so familiar with the one I am currently working with I am somewhat reluctant to switch over because I'd feel like I am almost starting from scratch, because I would have to learn how to interface with a whole new library.

EDIT: additional code.  This is my while loop that reads the image and trains the algorithm:

    public static void singleThread()
{

    int batchSize = 10000;
    int rangeLow = 0;
    int rangeHi = 9;

    int hits = 0;

    while (true)
    {

        // alternates between training and testing
        //Console.WriteLine("Training...  ");

        for (int i = 0; i < batchSize; i++)
        {

            // Give a training progress report every 100 iterations, this should increase performance
            if (i % 100 == 0)
            {
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.Write("Training: ");
                Console.Write("(" + (((float)i / (float)batchSize) * 100) + "%)");
                Console.Write("                    ");
            }

            // randomly select an image from the list
            int number = rng.Next(rangeLow, rangeHi);
            int index = rng.Next(1, 20);

            Bitmap loadedImage = (Bitmap)Image.FromFile("Train/" + number + "/" +
                                 index + ".png", true);

            int indexLocation = 0;
            // Convert the image into a grayScale value
            for (int x = 0; x < loadedImage.Width; x++)
            {
                for (int y = 0; y < loadedImage.Height; y++)
                {
                    Color pixel = loadedImage.GetPixel(x, y);
                    int grayValue = (int)((pixel.R * 0.3) + (pixel.G * 0.59) + (pixel.B * 0.11));
                    //Console.WriteLine(grayValue);
                    networkInputs[indexLocation] = grayValue;
                    indexLocation++;
                }
            }

            // The network will guess what the image is, and return the guess as a float array

            float[] guess = currentNetwork.BackPropagate(networkInputs, Interface.valueToArray(number));

            // This if statement checks if the guess was correct
            if (Interface.guessToValue(guess) == number)
            {
                hits++;
            }

        }

        currentNetwork.Performance = ((float) hits / (float) batchSize);
        hits = 0;

        Console.WriteLine("Score: " + (currentNetwork.Performance * 100) + "%");
    }
}


问:
Calculate the total value of the Widgets sold each month for a particular year
If I have a Django Model that defines a Widget like this:

```<code>class Widget(models.Model):
    type = models.CharField()
    sold = models.DateTimeField()
    price = models.DecimalField()
    .
    .
</code>```

How would I go about getting the total value of the Widgets sold per month for a particular year?

I'd like to end up with a list of 12 monthly totals, for example:

```<code>[1225, 197, 131, 125, ...
</code>```

This would indicate the total value of the Widgets sold in January was $1225, February $197 etc.

I expect this can be done with a Django Query, but not quite sure how.

UPDATE

I'd prefer not to use anything database specific to make changing databases easier. I don't mind if it's not all done in a single query. Doing a query, then some manipulation using Python after is fine.

UPDATE

I think I can slightly change some code from an answer to another question I raised to get this:

```<code>queryset = Widget.objects.filter(sold__year=year)
totals = [0] * 12
for widget in queryset:
    totals[widget.sold.month - 1] += widget.price
return totals
</code>```

Please let me know if I've missed something or if there's a better way of doing it.

答:
based on this answer you could do.

```<code>Widget.objects.extra(select={'year': "EXTRACT(year FROM sold)", 'month': "EXTRACT(month from sold)"}).values('year', 'month').annotate(Sum('price'))
</code>```

would give you something like

```<code>[{'price__sum': 1111, 'year': 2010L, 'month': 6L}...]
</code>```

Edit - without using EXTRACT

probably not very efficient but...

```<code>for date in Widget.objects.all().dates('sold','month'):
    values["%s_%s" % (date.year,date.month)] = Widget.objects.filter(sold__month=date.month,sold__year=date.year).aggregate(Sum('price'))
</code>```

Body:
If I have a Django Model that defines a Widget like this:

class Widget(models.Model):
    type = models.CharField()
    sold = models.DateTimeField()
    price = models.DecimalField()
    .
    .

How would I go about getting the total value of the Widgets sold per month for a particular year?

I'd like to end up with a list of 12 monthly totals, for example:

[1225, 197, 131, 125, ...

This would indicate the total value of the Widgets sold in January was $1225, February $197 etc.

I expect this can be done with a Django Query, but not quite sure how.

UPDATE

I'd prefer not to use anything database specific to make changing databases easier. I don't mind if it's not all done in a single query. Doing a query, then some manipulation using Python after is fine.

UPDATE

I think I can slightly change some code from an answer to another question I raised to get this:

queryset = Widget.objects.filter(sold__year=year)
totals = [0] * 12
for widget in queryset:
    totals[widget.sold.month - 1] += widget.price
return totals

Please let me know if I've missed something or if there's a better way of doing it.

Body:
based on this answer you could do.

Widget.objects.extra(select={'year': "EXTRACT(year FROM sold)", 'month': "EXTRACT(month from sold)"}).values('year', 'month').annotate(Sum('price'))

would give you something like

[{'price__sum': 1111, 'year': 2010L, 'month': 6L}...]

Edit - without using EXTRACT

probably not very efficient but...

for date in Widget.objects.all().dates('sold','month'):
    values["%s_%s" % (date.year,date.month)] = Widget.objects.filter(sold__month=date.month,sold__year=date.year).aggregate(Sum('price'))


问:
Redux request statuses. Reduce boilerplate
I am using Redux with react and redux-thunk as a middleware.

When I make http requests I have to dispatch three actions in my thunks.
I will use my auth example.

here are my actions:

```<code>export const loginSuccess = () => ({
  type: AUTH_LOGIN_SUCCESS,
})

export const loginFailure = (errorMessage) => ({
  type: AUTH_LOGIN_FAILURE,
  errorMessage,
})

export const loginRequest = () => ({
  type: AUTH_LOGIN_REQUEST,
})
</code>```

and here is the thunk which combines above three actions:

```<code>export const login = (credentials) => dispatch => {
  dispatch(loginRequest())
  const options = {
    method: 'post',
    url: `${ENDPOINT_LOGIN}?username=${credentials.username}&password=${credentials.password}`,
  }
  axiosInstance(options)
    .then(response => {
      dispatch(loginSuccess())
      dispatch(loadUser(response.data)) // I have separate action for user and separate reducer.
      window.localStorage.setItem(ACCESS_TOKEN_KEY, response.data.token)
    })
    .catch(error => {
      return dispatch(loginFailure(error))
    })
}
</code>```

And here is my reducer:

```<code>const initialState = {
  pending: false,
  error: false,
  errorMessage: null,
}

export const loginReducer = (state = initialState, action) => {
  switch (action.type) {
    case AUTH_LOGIN_SUCCESS:
      return {
        ...state,
        pending: false,
        error: false,
        errorMessage: null,
      }
    case AUTH_LOGIN_FAILURE:
      const { errorMessage } = action
      return {
        ...state,
        pending: false,
        error: true,
        errorMessage,
      }
    case AUTH_LOGIN_REQUEST:
      return {
        ...state,
        pending: true,
      }
    default:
      return state
  }
}
</code>```

I have to do almost exact same things when I am sending another request, for example in case of logout. I feel like I am repeating myself a lot and there must be a better way.

I need to know what is the best practice to handle this issue.

Any other corrections and recommendations will be appreciated.

答:
If you are looking for "ready to use" solution take a look at:

https://redux-toolkit.js.org/api/createAsyncThunk

https://redux-resource.js.org/ (but it is written with js (not TS), and no @types definition for this library)

If you are looking for a custom solution you can create a few factories:

factory for reducer

factory for three actions

factory for thunk

const actions = createActions('My request name');
const reducer = createReducer(actions);
...
const thunk = createThunk(config);
```

<p>or even you can combine them:</p>

<pre class="lang-js prettyprint-override"><code>const { actions, reducer, thunk } = createRequestState('Name...', config);
</code>```

... but this is just an idea.

Body:
I am using Redux with react and redux-thunk as a middleware.

When I make http requests I have to dispatch three actions in my thunks.
I will use my auth example.

here are my actions:

export const loginSuccess = () => ({
  type: AUTH_LOGIN_SUCCESS,
})

export const loginFailure = (errorMessage) => ({
  type: AUTH_LOGIN_FAILURE,
  errorMessage,
})

export const loginRequest = () => ({
  type: AUTH_LOGIN_REQUEST,
})

and here is the thunk which combines above three actions:

export const login = (credentials) => dispatch => {
  dispatch(loginRequest())
  const options = {
    method: 'post',
    url: `${ENDPOINT_LOGIN}?username=${credentials.username}&password=${credentials.password}`,
  }
  axiosInstance(options)
    .then(response => {
      dispatch(loginSuccess())
      dispatch(loadUser(response.data)) // I have separate action for user and separate reducer.
      window.localStorage.setItem(ACCESS_TOKEN_KEY, response.data.token)
    })
    .catch(error => {
      return dispatch(loginFailure(error))
    })
}

And here is my reducer:

const initialState = {
  pending: false,
  error: false,
  errorMessage: null,
}

export const loginReducer = (state = initialState, action) => {
  switch (action.type) {
    case AUTH_LOGIN_SUCCESS:
      return {
        ...state,
        pending: false,
        error: false,
        errorMessage: null,
      }
    case AUTH_LOGIN_FAILURE:
      const { errorMessage } = action
      return {
        ...state,
        pending: false,
        error: true,
        errorMessage,
      }
    case AUTH_LOGIN_REQUEST:
      return {
        ...state,
        pending: true,
      }
    default:
      return state
  }
}

I have to do almost exact same things when I am sending another request, for example in case of logout. I feel like I am repeating myself a lot and there must be a better way.

I need to know what is the best practice to handle this issue.

Any other corrections and recommendations will be appreciated.

Body:
If you are looking for "ready to use" solution take a look at:

https://redux-toolkit.js.org/api/createAsyncThunk

https://redux-resource.js.org/ (but it is written with js (not TS), and no @types definition for this library)

If you are looking for a custom solution you can create a few factories:

factory for reducer

factory for three actions

factory for thunk

const actions = createActions('My request name');
const reducer = createReducer(actions);
...
const thunk = createThunk(config);

or even you can combine them:

const { actions, reducer, thunk } = createRequestState('Name...', config);

... but this is just an idea.


问:
Get UIScrollView to scroll to the top
How do I make a UIScrollView scroll to the top?</p>

答:
In SWIFT 5
Just set content Offset to zero</p>

```<code>scrollView.setContentOffset(CGPoint.zero, animated: true)
</code>```

Title: Get UIScrollView to scroll to the top

Body:
In SWIFT 5
Just set content Offset to zero

scrollView.setContentOffset(CGPoint.zero, animated: true)


问:
Displaying forms one next another [JS]
I'm just doing a short form with 2 questions. I want the second one to appear onclick of the submit button of first question. I've tried several things but doesn't works :/
It seems to reload the page everytime i click on the submit button.
I begin so maybe it's a really stupid error.

Thanks !

```<code><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>

    <form id="question1">
        <h2>Salamèche est-il de type feu ?</h2>
        <div class="row">
            <div>
                <input type="radio" name="question1" id="oui1" value="oui" >
                <label for="oui1" class="oui">OUI</label>
            </div>
            <div>
                <input type="radio" name="question1" id="non1" value="non">
                <label for="non1" class="non ">NON</label>
            </div>
        </div>
        <input type="submit" value="Submit">
    </form>

    <form id="question2">
        <h2>Tiplouf est-il de type plante ?</h2>
        <div class="row">
            <div>
                <input type="radio" name="question1" id="oui1" value="oui" >
                <label for="oui2" class="oui">OUI</label>
            </div>
            <div>
                <input type="radio" name="question1" id="non1" value="non">
                <label for="non2" class="non ">NON</label>
            </div>
        </div>
        <input type="submit" value="Submit">
    </form>

    <form id="question3">
        <h2>Tiplouf est-il de type plante ?</h2>
        <div class="row">
            <div>
                <input type="radio" name="question1" id="oui1" value="oui" >
                <label for="oui3" class="oui">OUI</label>
            </div>
            <div>
                <input type="radio" name="question1" id="non1" value="non">
                <label for="non3" class="non ">NON</label>
            </div>
        </div>
        <input type="submit" value="Submit">
    </form>

    <script src="js/script.js"></script>

</body>
</html>
</code>```

The Javascript

```<code>function changeform(x){
    let nextForm = x + 1;
    for (i=0; i <= x; i++) {
        document.getElementById(i).addEventListener('submit', function() {
            $('question' + i).css('display','none');
            $('question' + nextForm).css('display','block');
        });
    }
}

function checked(x) {
    for (i=0; i <= x; i++) {
        var oui = 'oui'+i;
        var non = 'non'+i;
        console.log(oui);
        $(oui).addEventListener('click', function() {
            $(oui).className = 'cheked';
        });
        $(non).addEventListener('click', function() {
            $(non).className = 'cheked';
        });
    }
}

checked(3);
changeform(3);
</code>```

答:
submit event always reloads the browser window to stop that write

```<code>document.getElementById(i).addEventListener('submit', function(event) {
  event.preventDefault(); // this won't let submit action to reload window
  $('question' + i).css('display','none');
  $('question' + nextForm).css('display','block');
});
</code>```

Title: Displaying forms one next another [JS]

Body:
I'm just doing a short form with 2 questions. I want the second one to appear onclick of the submit button of first question. I've tried several things but doesn't works :/
It seems to reload the page everytime i click on the submit button.
I begin so maybe it's a really stupid error.

Thanks !

<!DOCTYPE html>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">

    <form id="question1">

Salamèche est-il de type feu ?

                <input type="radio" name="question1" id="oui1" value="oui" >
                OUI

                <input type="radio" name="question1" id="non1" value="non">
                NON

        <input type="submit" value="Submit">
    </form>

    <form id="question2">

Tiplouf est-il de type plante ?

                <input type="radio" name="question1" id="oui1" value="oui" >
                OUI

                <input type="radio" name="question1" id="non1" value="non">
                NON

        <input type="submit" value="Submit">
    </form>

    <form id="question3">

Tiplouf est-il de type plante ?

                <input type="radio" name="question1" id="oui1" value="oui" >
                OUI

                <input type="radio" name="question1" id="non1" value="non">
                NON

        <input type="submit" value="Submit">
    </form>

The Javascript

function changeform(x){
    let nextForm = x + 1;
    for (i=0; i <= x; i++) {
        document.getElementById(i).addEventListener('submit', function() {
            $('question' + i).css('display','none');
            $('question' + nextForm).css('display','block');
        });
    }
}

function checked(x) {
    for (i=0; i <= x; i++) {
        var oui = 'oui'+i;
        var non = 'non'+i;
        console.log(oui);
        $(oui).addEventListener('click', function() {
            $(oui).className = 'cheked';
        });
        $(non).addEventListener('click', function() {
            $(non).className = 'cheked';
        });
    }
}

checked(3);
changeform(3);

Body:
submit event always reloads the browser window to stop that write

document.getElementById(i).addEventListener('submit', function(event) {
  event.preventDefault(); // this won't let submit action to reload window
  $('question' + i).css('display','none');
  $('question' + nextForm).css('display','block');
});


问:
Relate Outlook message inline attachment to element in HTML body
When using Office.js to retrieve the body of an email that has inline attachments, we receive HTML like this:

```<code><div>
<div dir="ltr">
<div style="color:black;font-size:12pt;font-family:Calibri,Arial,Helvetica,sans-serif;background-color:white;">
This email has images in the body.</div>
<div style="color:black;font-size:12pt;font-family:Calibri,Arial,Helvetica,sans-serif;background-color:white;">
<img data-imagetype="AttachmentByCid" originalSrc="cid:ab24f213-f1a6-4804-ad70-25a4d3ac5f86" naturalHeight="0" naturalWidth="0" src="cid:ab24f213-f1a6-4804-ad70-25a4d3ac5f86" data-outlook-trace="F:1|T:1" tabindex="-1" size="9026" style="max-width:100%;">
<img data-imagetype="AttachmentByCid" originalSrc="cid:be90d9aa-49a6-48fb-b4b9-d50950aefae3" naturalHeight="0" naturalWidth="0" src="cid:be90d9aa-49a6-48fb-b4b9-d50950aefae3" data-outlook-trace="F:1|T:1" tabindex="-1" size="60886" style="max-width:100%;">
<br>

</div>
</div>
</div>
</code>```

Where the  tags are where the inline image attachments would be.

The attachments property on the mailbox item lists attachment metadata like this:

```<code>{
attachmentType: "file",
contentType: "image/jpeg",
id: "AAMkADFlM2Q0MzdkLTJhZWQtNGQzNS05YWI2LWMwYmNkZmMwODNmZgBGAAAAAAAbEbu84Nj1SIfOZGQpQsg/BwC74mYLmMGJSYnZmTQHpD7TAAAAAAEJAAC74mYLmMGJSYnZmTQHpD7TAAE1znR7AAABEgAQACz3QOlxzApEvhIoK7iHWoc=",
isInline: true,
name: "home_team.jpg",
size: 9341
}
</code>```

I can't any link between the first image tag and this attachment object. Is there some other way of identifying where each inline attachment is in the message?

答: The Office.AttachmentDetails interface doesn't provide any property for the content ID value. You need to use EWS to get a property value. See Call web services from an Outlook add-in to get started quickly. The Attachment.ContentId property gets or sets the content identifier of the attachment. ContentId can be used as a custom way to identify an attachment in order to reference it from within the body of the item that the attachment belongs to.

Body:
When using Office.js to retrieve the body of an email that has inline attachments, we receive HTML like this:

This email has images in the body.

Where the  tags are where the inline image attachments would be.

The attachments property on the mailbox item lists attachment metadata like this:

{
attachmentType: "file",
contentType: "image/jpeg",
id: "AAMkADFlM2Q0MzdkLTJhZWQtNGQzNS05YWI2LWMwYmNkZmMwODNmZgBGAAAAAAAbEbu84Nj1SIfOZGQpQsg/BwC74mYLmMGJSYnZmTQHpD7TAAAAAAEJAAC74mYLmMGJSYnZmTQHpD7TAAE1znR7AAABEgAQACz3QOlxzApEvhIoK7iHWoc=",
isInline: true,
name: "home_team.jpg",
size: 9341
}

I can't any link between the first image tag and this attachment object. Is there some other way of identifying where each inline attachment is in the message?


问:
How to fix Apache 2.4 Require not allow here when starting
Getting the below error  when starting apache 2.4.

```<code>httpd[11435]: AH00526: Syntax error on line 22 of /etc/httpd/conf.d/vhost1.conf:
httpd[11435]: Require not allowed here
</code>```

The following line causes this error in vhost1.conf

```<code><VirtualHost myserver:80>
    DocumentRoot "/var/www/html"
    Require all granted
</VirtualHost>
<VirtualHost myserver:443>
    DocumentRoot "/var/www/html"
    Require all granted
</VirtualHost>
</code>```

What is wrong with this? How can I fix the error?

答:
You need to use Require inside Directory. i.e </p>

```<code><Directory "/var/www/html">
    Require all granted
<Directory>
</code>```

Body:
Getting the below error  when starting apache 2.4.

httpd[11435]: AH00526: Syntax error on line 22 of /etc/httpd/conf.d/vhost1.conf:
httpd[11435]: Require not allowed here

The following line causes this error in vhost1.conf

<VirtualHost myserver:80>
    DocumentRoot "/var/www/html"
    Require all granted
</VirtualHost>
<VirtualHost myserver:443>
    DocumentRoot "/var/www/html"
    Require all granted
</VirtualHost>

What is wrong with this? How can I fix the error?

Body:
You need to use Require inside Directory. i.e

<Directory "/var/www/html">
    Require all granted
<Directory>


问:
Is there any optimization or different API for page aligned memory allocations on the heap?
I am about to write a class representing a double-ended queue, just like std::dequeue, but with the capability to store any trivially destructible type, and without indexing support. Iteration or pop operations will only work knowing the types stored before. Most of the time it will be used as a queue/stack like storage, referenced by other types, as references are guaranteed to stay valid, even if it is pushed/popped to either end in between. Allocation and freeing of memory should be done in huge blocks. The systems page size seems to be a perfect fit for a default block size.

I assume, that page aligned allocation of the page sized memory chunks would make sense in order to reduce cache misses on iteration over the elements of the container. Is this true?

I'm afraid using std::aligned_alloc() with page alignment and size will lead to the heap's metadata (which for example is later required by free(), to know the size of the allocated memory) being stored in front of allocated memory, which would lead to a huge waste of memory (nearly one page for each allocated page). Or is there any optimization or different API for page-aligned memory allocations of the heap? For example, I could imagine an API allowing the client to specify where to store the required metadata, or which returns a pointer to the memory and a pointer to the metadata on allocation.

On the other hand, using the systems native APIs (Windows / Posix / FreeRTOS) to allocate pages would require a separate memory pool, which would be a heap optimized for page-aligned memory allocations. But having two heaps not knowing about each other could lead to memory waste too, as both will have a pool of preallocated pages. Or do most standard library implementations free pages to the operating system as soon as they are not used by clients anymore?

答:
For cache hit-and-miss optimization, page-aligned blocks will not help you much.  At that level, you should look for cache memory line alignment.  On x86 CPUs that will be 64 bytes.

Page alignment may help you in optimizing the amount of RAM being allocated from the operating system. That is dependent on the size of blocks your allocator will claim from new or malloc at a time.  Losses in that area are usually fairly small, or nil.  If using OS calls, compute the amount of wasted memory, using this formula:

```<code>(PAGE_SIZE - (BlockSize % PAGE_SIZE)) * BlockCount
</code>```

For best cache optimization, align individual elements, including the list related pointers, on a cache line boundary, again, 64 bytes for x86.  This is where you will gain the most performance.  Beware! That is also where most of the waste in memory usually comes from.

The waste of memory in that area is

```<code>(LINE_SIZE - (ItemSize % LINE_SIZE)) * ItemCount
</code>```

Body:
For cache hit-and-miss optimization, page-aligned blocks will not help you much.  At that level, you should look for cache memory line alignment.  On x86 CPUs that will be 64 bytes.

Page alignment may help you in optimizing the amount of RAM being allocated from the operating system. That is dependent on the size of blocks your allocator will claim from new or malloc at a time.  Losses in that area are usually fairly small, or nil.  If using OS calls, compute the amount of wasted memory, using this formula:

(PAGE_SIZE - (BlockSize % PAGE_SIZE)) * BlockCount

For best cache optimization, align individual elements, including the list related pointers, on a cache line boundary, again, 64 bytes for x86.  This is where you will gain the most performance.  Beware! That is also where most of the waste in memory usually comes from.

The waste of memory in that area is

(LINE_SIZE - (ItemSize % LINE_SIZE)) * ItemCount


问:
Error "User limit of inotify watches reached". ExtReact build
I installed ExtReact, with examples. When I run

```<code>npm start
</code>```

I get an error:

```<code>ERROR in [@extjs/reactor-webpack-plugin]: Error:
[ERR] BUILD FAILED
[ERR] com.sencha.exceptions.BasicException: User limit of inotify watches
reached
[ERR]
[ERR] Total time: 13 seconds

[ERR] /home/user/project/build/ext-react/build.xml:101:
com.sencha.exceptions.BasicException: User limit of inotify watches reached
[ERR] A log is available in the file "/home/user/project/build/ext-
react/sencha-error-20171027.log"
</code>```

How to fix this error?

答:
On Linux mint,
Run this command and you are good to go</p>

```<code>echo fs.inotify.max_user_watches=524288 | sudo tee /etc/sysctl.d/40-max-user-watches.conf && sudo sysctl --system
</code>```

Body:
I installed ExtReact, with examples. When I run

npm start

I get an error:

ERROR in [@extjs/reactor-webpack-plugin]: Error:
[ERR] BUILD FAILED
[ERR] com.sencha.exceptions.BasicException: User limit of inotify watches
reached
[ERR]
[ERR] Total time: 13 seconds

[ERR] /home/user/project/build/ext-react/build.xml:101:
com.sencha.exceptions.BasicException: User limit of inotify watches reached
[ERR] A log is available in the file "/home/user/project/build/ext-
react/sencha-error-20171027.log"

How to fix this error?

Body:
On Linux mint,
Run this command and you are good to go

echo fs.inotify.max_user_watches=524288 | sudo tee /etc/sysctl.d/40-max-user-watches.conf && sudo sysctl --system


问:
Rails help model design problem
I am having that problem when I update my konkurrencer the do_foobar is called. The problem is that the konkurrancer have no ratings. And I get a off course a ZeroDivisionerror.

What is the best solution to solve this kind of problem?

My model:

```<code>before_update :do_foobar
def do_foobar
  self.rating = (rating_score/ratings)
end
</code>```

答:
What about:</p>

```<code>rating = rating_score/ratings if ratings != 0
</code>```

Title: Rails help model design problem

Body:
I am having that problem when I update my konkurrencer the do_foobar is called. The problem is that the konkurrancer have no ratings. And I get a off course a ZeroDivisionerror.

What is the best solution to solve this kind of problem?

My model:

before_update :do_foobar
def do_foobar
  self.rating = (rating_score/ratings)
end

Body:
What about:

rating = rating_score/ratings if ratings != 0


问:
Dynamically changing class in v-text-field in default slot
I am trying to dynamically change font-size in v-text-field default slot based on the length of the text. However, it seems that v-text-field ignores any specification I specify in the  section.

Here is the code

```<code>        <v-text-field
          v-model="attr.name"
          hide-details
          :readonly="true"
          class="core-select"
          label="Core Attribute"
        >
          <template
            v-slot:default
          >
            <div :class="attrNameStyle[0]">
              {{ attr.name }}
            </div>
          </template>
        </v-text-field>
</code>```

I have verified that attrNameStyle[0] is gets set correctly, however that style never gets applied to the default slot. I can change the way input slot looks via this CSS class .v-text-field__slot input { ... } however, I can't update that CSS dynamically.

Thanks for help!

Edit: Adding more context.

```<code>.core-select {
  width: 180px;
}

.short-core-select {
  font-size: 12px;
}
</code>```

attrNameStyle[0] is set to either '', or 'short-core-select'.

答:
Since v-text-field__slot is working, you could edit that CSS from a higher level.

```<code><v-text-field
      v-model="attr.name"
      hide-details
      hide-details
      class="core-select"
      :class="attrNameStyle[0]"
      label="Core Attribute"
    >
      <template>
        <div>
          {{ attr.name }}
        </div>
      </template>
    </v-text-field>
</code>```

```

Body:
I am trying to dynamically change font-size in v-text-field default slot based on the length of the text. However, it seems that v-text-field ignores any specification I specify in the  section.

Here is the code

        <v-text-field
          v-model="attr.name"
          hide-details
          :readonly="true"
          class="core-select"
          label="Core Attribute"
        >
          <template
            v-slot:default
          >

              {{ attr.name }}

          </template>
        </v-text-field>

I have verified that attrNameStyle[0] is gets set correctly, however that style never gets applied to the default slot. I can change the way input slot looks via this CSS class .v-text-field__slot input { ... } however, I can't update that CSS dynamically.

Thanks for help!

Edit: Adding more context.

.core-select {
  width: 180px;
}

.short-core-select {
  font-size: 12px;
}

attrNameStyle[0] is set to either '', or 'short-core-select'.

Body:
Since v-text-field__slot is working, you could edit that CSS from a higher level.

<v-text-field
      v-model="attr.name"
      hide-details
      hide-details
      class="core-select"
      :class="attrNameStyle[0]"
      label="Core Attribute"
    >
      <template>
        <div>
          {{ attr.name }}
        </div>
      </template>
    </v-text-field>

<style>
  .short-core-select .v-text-field__slot {
    font-size: 12px;
  }
</style>