问:
plotting time series grouped by hour and day of week
I have hourly timeseries data for over a year, and wish to plot average hourly measurements for each day of the week. I have run the following code and my data is in the following format (i previously created columns for days of week).

```<code>df = df.groupby([df['date_UTC'].dt.hour, df['w_day']]).pm2_5_ugm3.mean().reset_index().dropna()

date_UTC    w_day   pm2_5_ugm3
0   0   Friday  11.868802
1   0   Monday  12.741524
2   0   Saturday    13.346273
3   0   Sunday  13.093804
4   0   Thursday    14.525096
... ... ... ...
163 23  Saturday    12.836788
164 23  Sunday  12.712683
165 23  Thursday    12.046935
166 23  Tuesday 12.043133
167 23  Wednesday   14.164027
</code>```

I ultimately want a plot that looks something like below, but I am not sure exactly how to plot it using seaborn/matplotlib.

UPDATE:

I used the suggestion of @Arnold Vialfont which basically gets me the results I am looking for, but facetplot doesnt seem to allow me to plot a legend when I introduce hue. I then used factorplot however it doesnt seem that this plot can produce a lineplot, which is what I need. Is there a way to use factorplot and have hue produce a legend?

```<code>col_order=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

sns.set_style("white")
ax = sns.FacetGrid(df, col="w_day", col_wrap=7, height=6, col_order=col_order, hue ='Zone')
ax.map(sns.lineplot, "date_UTC", "pm2_5_ugm3")
</code>```

Hue not producing legend

Here using factorplot i have a legend but cant produce a line plot as desired.

```<code>ax = sns.catplot(x="date_UTC", y="pm2_5_ugm3", col="w_day", hue = 'Zone',
                data=df, ci=None, aspect=.6)
</code>```

答:
You can use seaborn in the following way, where col_wrap allows to choose the number of graphics per line:

```<code>g = sns.FacetGrid(df, col="w_day", col_wrap=7, height=4)
h.map(sns.lineplot, "date_UTC", "pm2_5_ugm3")
</code>```

Note that as compared to what you say you ultimately want there will be a single curve.

Body:
I have hourly timeseries data for over a year, and wish to plot average hourly measurements for each day of the week. I have run the following code and my data is in the following format (i previously created columns for days of week).

df = df.groupby([df['date_UTC'].dt.hour, df['w_day']]).pm2_5_ugm3.mean().reset_index().dropna()

date_UTC    w_day   pm2_5_ugm3
0   0   Friday  11.868802
1   0   Monday  12.741524
2   0   Saturday    13.346273
3   0   Sunday  13.093804
4   0   Thursday    14.525096
... ... ... ...
163 23  Saturday    12.836788
164 23  Sunday  12.712683
165 23  Thursday    12.046935
166 23  Tuesday 12.043133
167 23  Wednesday   14.164027

I ultimately want a plot that looks something like below, but I am not sure exactly how to plot it using seaborn/matplotlib.

UPDATE:

I used the suggestion of @Arnold Vialfont which basically gets me the results I am looking for, but facetplot doesnt seem to allow me to plot a legend when I introduce hue. I then used factorplot however it doesnt seem that this plot can produce a lineplot, which is what I need. Is there a way to use factorplot and have hue produce a legend?

col_order=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

sns.set_style("white")
ax = sns.FacetGrid(df, col="w_day", col_wrap=7, height=6, col_order=col_order, hue ='Zone')
ax.map(sns.lineplot, "date_UTC", "pm2_5_ugm3")

Hue not producing legend

Here using factorplot i have a legend but cant produce a line plot as desired.

ax = sns.catplot(x="date_UTC", y="pm2_5_ugm3", col="w_day", hue = 'Zone',
                data=df, ci=None, aspect=.6)

Body:
You can use seaborn in the following way, where col_wrap allows to choose the number of graphics per line:

g = sns.FacetGrid(df, col="w_day", col_wrap=7, height=4)
h.map(sns.lineplot, "date_UTC", "pm2_5_ugm3")

Note that as compared to what you say you ultimately want there will be a single curve.


来源: stackexchange

问:
How can I export gallery images from SquareSpace?
SquareSpace does not offer any way to export uploaded content directly. The only export option available is for WordPress, but this only generates a small XML file. What is the best way to download the actual image files from a gallery, other than right-clicking each image and choosing "Save as..."?</p>

答: If you don't have too many images, you can do them one at a time from a gallery.  While viewing a gallery (Chrome) I can right-click and open the image in a new tab and then save that (getting rid of the parameters after *.jpeg )   </p>

ContentLicense: CC BY-SA 3.0

ContentLicense: CC BY-SA 4.0


问:
kafka streams + how to expire entries in a state store asynchronously
I have a kafka streams topology that reads from an input topic updates some state and determines if the state entry needs to remain in state store or can be deleted. If it can be deleted it will be removed else I've a punctuator that runs every 10s and expires items from the state store.

I recently found out that the punctuators run on the same stream thread and can potentially block processing of the stream.

What are some patterns I can use to execute the logic inside the punctuator in a separate thread pool to avoid blocking stream processing ?

Appreciate your help.

答:
Matthias J. Sax already said, that's not possible with state stores, so far, so as he works at Confluent, I believe thats the latest news.

However, what we did in our case was using a KStream-KTable join instead of a state store. I'm not sure, if that's possible for your case, but let me explain, what we did, maybe it's of some use for you, as well:

We have two Topics A and B, Topic A is consumed with a KStream. Topic B is consumed with a KTable. We transform the KTable data, so we can join it on the KStream for Topic A. We join it, perform our operations and "delete" the data from Topic B by writing a null value with the original key to Topic B, using map and through. So when we get another record in Topic A, there are no longer values in our KTable to join with (exactly what we wanted).

I hope it helps.


问:
Problems with clicking a button
I wanted the program to click the 'PLAY' Button on spotify to play a song, but sadly I get this error

selenium.common.exceptions.JavascriptException: Message: javascript error: arguments[0].click is not a function

The code I tried is

```<code>  WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH,"//*[text()='PLAY']")))
  time.sleep(1)
  driver.execute_script("arguments[0].click();", driver.find_element_by_xpath("//*[text()='PLAY']"))
</code>```

Can someone help me to find the button and click it, I tried also the

```<code>driver.find_element_by_xpath("//*[text()='PLAY']").click()
</code>```

But I get the error element is not interactable

答:
WebDriverWait wait = new WebDriverWait(driver, 10);
  wait.until(ExpectedConditions.visibilityOf(driver.find_element_by_xpath("//div[@class='TrackListHeader__button
  TrackListHeader__button--top']//button")));

Thread.sleep(1000);

driver.find_element_by_xpath("//div[@class='TrackListHeader__button
  TrackListHeader__button--top']//button").click();

also try this one

Title: Problems with clicking a button

Body:
I wanted the program to click the 'PLAY' Button on spotify to play a song, but sadly I get this error

selenium.common.exceptions.JavascriptException: Message: javascript error: arguments[0].click is not a function

The code I tried is

  WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH,"//*[text()='PLAY']")))
  time.sleep(1)
  driver.execute_script("arguments[0].click();", driver.find_element_by_xpath("//*[text()='PLAY']"))

Can someone help me to find the button and click it, I tried also the

driver.find_element_by_xpath("//*[text()='PLAY']").click()

But I get the error element is not interactable


问:
Docker daemon log taking up too much disk space
Preamble

For clarification, this concern is for the docker daemon logs, not for docker's container-level logs. The log-driver and log-opts docker daemon configurations seem to handle container-level logs just fine.

Description

The issue we're running into is that the docker daemon log (located at /var/log/docker.err.log) log file is growing too quickly, and not rotating on our docker swarm production servers. We are naturally worried about disk space over the long term. Concretely, we've been running for almost a month now, and on one node, that log file has already grown to 5.1GB. As our VM's are expected to run for months on end, this is obviously a concerning trend.

The linuxkit OS that we're using has very similar configuration to the example docker configuration, with some additional configuration in the /etc/docker/daemon.json file, most notably that we set debug to True. That was clearly a mistake, as our tests show that, had we not used that option, our log files would be substantially smaller.

However, even if we had left the log-level at it's default of info, it looks to me like the log file could still cause issues if the server is left running for too long. One of my coworkers did some rough calculations, and his guess is that the file could still grow to something like 10GB if the daemon is left running for 6 months or so.

What We've Tried

We've been trying to get a manual logrotate solution working to protect against this, but it seems like the docker daemon never reloads it's log file, which means that when logrotate does it's thing and creates a blank new docker.err.log, the docker daemon continues to write at whatever offset it left off at, and backfills the rest of the file with null bytes, taking up as much space as it was before. We've tried some solutions involving sending the HUP signal to the docker process without any success; it seems like the docker daemon doesn't handle that signal, or at least not in a way that reloads it's log file.

The Question(s)

Is there an accepted way to implement rotating of the docker daemon logs?

It seems unusual that we can't find any information about this, as it seems likely somebody somewhere has bumped into this issue before. Or do others running on swarm periodically restart servers at some point? Ideally we'd love to find a linuxkit-based way of rotating that log file specifically

Additionally, is there a way to truncate the existing docker.err.log on a running server without shutting down the server or docker daemon instance? We'd like to avoid at all costs having to deploy the updated OS image just to prevent the docker daemon log from using up all our disk space.

Steps to Reproduce

Create a linuxkit OS ISO with a docker daemon service with debug: True in it's configuration file

Using that ISO, run a docker swarm cluster for several days

Observe the growth of the docker.err.log file

Linuxkit Config

```<code>kernel:
  image: linuxkit/kernel:4.15.5
  cmdline: "console=tty0 quiet console=ttyAMA0"
init:
  - linuxkit/init:6061875ba11fd9c563fda6234b103ed9997ff782
  - linuxkit/runc:52ecfdef1ae051e7fd5ac5f1d0b7dd859adff015
  - linuxkit/containerd:13f62c61f0465fb07766d88b317cabb960261cbb
  - linuxkit/ca-certificates:0a188e40108b6ece8c2aefdfaaad94acc84368ce
 ...

services:
   - name: docker
    image: docker:17.12.0-ce-dind
    capabilities:
     - all
    net: host
    mounts:
     - type: cgroup
       options: ["rw","nosuid","noexec","nodev","relatime"]
    binds:
     - /tiles:/tiles
     - /etc/resolv.conf:/etc/resolv.conf
     - /tmp/hosts:/etc/hosts
     - /root/.ssh:/root/.ssh
     - /var/lib/docker:/var/lib/docker
     - /lib/modules:/lib/modules
     - /etc/docker/daemon.json:/etc/docker/daemon.json
     - /persistent:/persistent
     - /application:/application
    command: ["/usr/local/bin/docker-init", "/usr/local/bin/dockerd"]

files:
  - path: etc/docker/daemon.json
    contents: |
        {
          "debug": true,
          "data-root": "/persistent/docker",
          "insecure-registries" : ["foobar-docker-registry.chip:5000"],
          "log-driver": "json-file",
          "log-opts": {
            "max-size": "100m",
            "max-file": "4"
          }
        }
</code>```

答:
Just to close this off, this is no longer an issue in linuxkit. They have since added logging support using memlogd, and their own log writer, which handles automatic rotation. This is the link to their logging documentation.

The yaml is configured like so:

```<code>init:
  # A circular buffer that captures logs from onboot and service-level containers
  - linuxkit/memlogd:v0.7
  ...
services:
  - name: write-and-rotate-logs
    image: foobar/logwrite
    command: ["/usr/bin/logwrite",
              "-log-dir", "/persistent/log/bespin",
              # Keep at most 25 files (Note: file numbers are 0-based)
              "-max-log-files", "25",
              # Max log file size set to 200MB (200 * 1024 * 1024 = 209,715,200 bytes)
              "-max-log-size", "209715200"]
</code>```

This will capture all linuxkit service logs, including docker daemon logs, and write them to disk. It handles rotation as it writes.

Body:
Preamble

For clarification, this concern is for the docker daemon logs, not for docker's container-level logs. The log-driver and log-opts docker daemon configurations seem to handle container-level logs just fine.

Description

The issue we're running into is that the docker daemon log (located at /var/log/docker.err.log) log file is growing too quickly, and not rotating on our docker swarm production servers. We are naturally worried about disk space over the long term. Concretely, we've been running for almost a month now, and on one node, that log file has already grown to 5.1GB. As our VM's are expected to run for months on end, this is obviously a concerning trend.

The linuxkit OS that we're using has very similar configuration to the example docker configuration, with some additional configuration in the /etc/docker/daemon.json file, most notably that we set debug to True. That was clearly a mistake, as our tests show that, had we not used that option, our log files would be substantially smaller.

However, even if we had left the log-level at it's default of info, it looks to me like the log file could still cause issues if the server is left running for too long. One of my coworkers did some rough calculations, and his guess is that the file could still grow to something like 10GB if the daemon is left running for 6 months or so.

What We've Tried

We've been trying to get a manual logrotate solution working to protect against this, but it seems like the docker daemon never reloads it's log file, which means that when logrotate does it's thing and creates a blank new docker.err.log, the docker daemon continues to write at whatever offset it left off at, and backfills the rest of the file with null bytes, taking up as much space as it was before. We've tried some solutions involving sending the HUP signal to the docker process without any success; it seems like the docker daemon doesn't handle that signal, or at least not in a way that reloads it's log file.

The Question(s)

Is there an accepted way to implement rotating of the docker daemon logs?

It seems unusual that we can't find any information about this, as it seems likely somebody somewhere has bumped into this issue before. Or do others running on swarm periodically restart servers at some point? Ideally we'd love to find a linuxkit-based way of rotating that log file specifically

Additionally, is there a way to truncate the existing docker.err.log on a running server without shutting down the server or docker daemon instance? We'd like to avoid at all costs having to deploy the updated OS image just to prevent the docker daemon log from using up all our disk space.

Steps to Reproduce

Create a linuxkit OS ISO with a docker daemon service with debug: True in it's configuration file

Using that ISO, run a docker swarm cluster for several days

Observe the growth of the docker.err.log file

Linuxkit Config

kernel:
  image: linuxkit/kernel:4.15.5
  cmdline: "console=tty0 quiet console=ttyAMA0"
init:
  - linuxkit/init:6061875ba11fd9c563fda6234b103ed9997ff782
  - linuxkit/runc:52ecfdef1ae051e7fd5ac5f1d0b7dd859adff015
  - linuxkit/containerd:13f62c61f0465fb07766d88b317cabb960261cbb
  - linuxkit/ca-certificates:0a188e40108b6ece8c2aefdfaaad94acc84368ce
 ...

services:
   - name: docker
    image: docker:17.12.0-ce-dind
    capabilities:
     - all
    net: host
    mounts:
     - type: cgroup
       options: ["rw","nosuid","noexec","nodev","relatime"]
    binds:
     - /tiles:/tiles
     - /etc/resolv.conf:/etc/resolv.conf
     - /tmp/hosts:/etc/hosts
     - /root/.ssh:/root/.ssh
     - /var/lib/docker:/var/lib/docker
     - /lib/modules:/lib/modules
     - /etc/docker/daemon.json:/etc/docker/daemon.json
     - /persistent:/persistent
     - /application:/application
    command: ["/usr/local/bin/docker-init", "/usr/local/bin/dockerd"]

files:
  - path: etc/docker/daemon.json
    contents: |
        {
          "debug": true,
          "data-root": "/persistent/docker",
          "insecure-registries" : ["foobar-docker-registry.chip:5000"],
          "log-driver": "json-file",
          "log-opts": {
            "max-size": "100m",
            "max-file": "4"
          }
        }

Body:
Just to close this off, this is no longer an issue in linuxkit. They have since added logging support using memlogd, and their own log writer, which handles automatic rotation. This is the link to their logging documentation.

The yaml is configured like so:

init:
  # A circular buffer that captures logs from onboot and service-level containers
  - linuxkit/memlogd:v0.7
  ...
services:
  - name: write-and-rotate-logs
    image: foobar/logwrite
    command: ["/usr/bin/logwrite",
              "-log-dir", "/persistent/log/bespin",
              # Keep at most 25 files (Note: file numbers are 0-based)
              "-max-log-files", "25",
              # Max log file size set to 200MB (200 * 1024 * 1024 = 209,715,200 bytes)
              "-max-log-size", "209715200"]

This will capture all linuxkit service logs, including docker daemon logs, and write them to disk. It handles rotation as it writes.


问:
Wildcard date format not resolved in Word
In this loop (iii = 12 to 1 step -1), the .text wildcard doesn't find any dates at all:

```<code>With Selection.FInd
    .Forward = True
    .Wrap = WdFindStop
    .text= "([0-9]{1,2}] " & Mon_th(iii) & ([0-9]{4}])"
    .Execute
End With
</code>```

But in this loop, it does:

```<code>With ActiveDocument.Content
With.Find
  .Forward = True
  .Wrap = WdFindStop
  .text= "([0-9]{1,2}] " & Mon_th(iii) & ([0-9]{4}])"
End With
</code>```

I think I need to use the first version because I want to insert a comment every time a date is found, but can't figure out why it works with one but not the other.

All help appreciated, thanks.

Also, why does .text have an initial cap when I input it but changes to lower case immediately after?

答:
You can, of course, do this without that loop. For example:

```<code>.Text = "<[0-9]{1,2} [JFMASOND][abceghilmnoprstuvy]{2,8} [12][0-9]{3}>"
</code>```

or:

```<code>.Text = "<[0-9]{1,2} [JFMASOND][abceglnoprtuvy]{2} [12][0-9]{3}>"
</code>```

If you need different comments, that can be achieved via a Select Case statement in the loop you'll already need for adding those. Thus:

```<code>Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "<[0-9]{1,2} [JFMASOND][abceglnoprtuvy]{2} [12][0-9]{3}>"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    Select Case Split(.Text, " ")(1)
      Case "Jan": .Comments.Add .Duplicate, "Comment for Jan"
      Case "Feb": .Comments.Add .Duplicate, "Comment for Feb"
      Case "Mar": .Comments.Add .Duplicate, "Comment for Mar"
      Case "Apr": .Comments.Add .Duplicate, "Comment for Apr"
      Case "May": .Comments.Add .Duplicate, "Comment for May"
      Case "Jun": .Comments.Add .Duplicate, "Comment for Jun"
      Case "Jul": .Comments.Add .Duplicate, "Comment for Jul"
      Case "Aug": .Comments.Add .Duplicate, "Comment for Aug"
      Case "Sep": .Comments.Add .Duplicate, "Comment for Sep"
      Case "Oct": .Comments.Add .Duplicate, "Comment for Oct"
      Case "Nov": .Comments.Add .Duplicate, "Comment for Nov"
      Case "Dec": .Comments.Add .Duplicate, "Comment for Dec"
    End Select
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
</code>```

Body:
In this loop (iii = 12 to 1 step -1), the .text wildcard doesn't find any dates at all:

With Selection.FInd
    .Forward = True
    .Wrap = WdFindStop
    .text= "([0-9]{1,2}] " & Mon_th(iii) & ([0-9]{4}])"
    .Execute
End With

But in this loop, it does:

With ActiveDocument.Content
With.Find
  .Forward = True
  .Wrap = WdFindStop
  .text= "([0-9]{1,2}] " & Mon_th(iii) & ([0-9]{4}])"
End With

I think I need to use the first version because I want to insert a comment every time a date is found, but can't figure out why it works with one but not the other.

All help appreciated, thanks.

Also, why does .text have an initial cap when I input it but changes to lower case immediately after?

Body:
You can, of course, do this without that loop. For example:

.Text = "<[0-9]{1,2} [JFMASOND][abceghilmnoprstuvy]{2,8} [12][0-9]{3}>"

or:

.Text = "<[0-9]{1,2} [JFMASOND][abceglnoprtuvy]{2} [12][0-9]{3}>"

If you need different comments, that can be achieved via a Select Case statement in the loop you'll already need for adding those. Thus:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "<[0-9]{1,2} [JFMASOND][abceglnoprtuvy]{2} [12][0-9]{3}>"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    Select Case Split(.Text, " ")(1)
      Case "Jan": .Comments.Add .Duplicate, "Comment for Jan"
      Case "Feb": .Comments.Add .Duplicate, "Comment for Feb"
      Case "Mar": .Comments.Add .Duplicate, "Comment for Mar"
      Case "Apr": .Comments.Add .Duplicate, "Comment for Apr"
      Case "May": .Comments.Add .Duplicate, "Comment for May"
      Case "Jun": .Comments.Add .Duplicate, "Comment for Jun"
      Case "Jul": .Comments.Add .Duplicate, "Comment for Jul"
      Case "Aug": .Comments.Add .Duplicate, "Comment for Aug"
      Case "Sep": .Comments.Add .Duplicate, "Comment for Sep"
      Case "Oct": .Comments.Add .Duplicate, "Comment for Oct"
      Case "Nov": .Comments.Add .Duplicate, "Comment for Nov"
      Case "Dec": .Comments.Add .Duplicate, "Comment for Dec"
    End Select
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub


问:
How to remove property when window is scaled to lower/higher size
I have a table component here from antd

Basically I wanted to remove the scroll={{ x:1000 }} when I make the browser window bigger, say than 1200px. How can I achieve this in React?

Booking function()

```<code>function booking() {

    return (

        <div className="booking">
            <Navbar bg="" variant="dark" id="mainHeader">
                <Nav className="mr-auto">
                    <img
                        src={logo}
                        width="110"
                        height="50"
                        className="d-inline-block align-top ml-4 mt-3 mb-2"
                        alt="logo"
                    />
                </Nav>
                <Nav.Link a href="/" className="ml-sm-4 text-dark">Home</Nav.Link>
                <Nav.Link a href="/bookings" className=" text-dark">Bookings</Nav.Link>
                <Nav.Link a href="/signout" className="mr-sm-4 text-dark">Sign Out</Nav.Link>
            </Navbar>

            {/* My Booking Table */}
            <header className="App-header text-dark">
                    <Card border="light" className="bookingCard" id="bookingCard" >
                        <h1 className="mt-4 ml-4" id="cardTitle">Upcoming Bookings</h1>
                        <Card.Body>
                            <Table columns={columns} dataSource={data} {...!!this.state.scroll && { scroll: { x: 1000 }}} pagination={{ pageSize: 4 }} />
                        </Card.Body>
                    </Card>
            </header>
        </div>
    );
}
</code>```

I wanted to change the Table there.

答:
Use resize event listener, something like that:

```<code>const threshold = 1200;

window.addEventListener('resize', () => {
    const addScroll = !this.state.addScroll;
    return window.innerWidth >= threshold ^ !addScroll && this.setState({ addScroll });
});
</code>```

And in render method:

```<code><Table columns={columns}
       dataSource={data}
       {...!!this.state.addScroll && { scroll: { x: 1000 }}
       pagination={{ pageSize: 4 }} />
</code>```

So your full code should look like this:

```<code>class Booking extends React.Component {
    constructor () {
        const widthThreshold = 1200;

        window.addEventListener('resize', () => {
            const addScroll = !this.state.addScroll;
            return window.innerWidth >= widthThreshold ^ !addScroll && this.setState({ addScroll });
        });
    }

    render = () =>
        <div className="booking">
            <Navbar bg="" variant="dark" id="mainHeader">
                <Nav className="mr-auto">
                    <img src={logo} width="110" height="50" className="d-inline-block align-top ml-4 mt-3 mb-2" alt="logo"/>
                </Nav>
                <Nav.Link a href="/" className="ml-sm-4 text-dark">Home</Nav.Link>
                <Nav.Link a href="/bookings" className=" text-dark">Bookings</Nav.Link>
                <Nav.Link a href="/signout" className="mr-sm-4 text-dark">Sign Out</Nav.Link>
            </Navbar>

            <header className="App-header text-dark">
                <Card border="light" className="bookingCard" id="bookingCard" >
                    <h1 className="mt-4 ml-4" id="cardTitle">Upcoming Bookings</h1>
                    <Card.Body>
                        <Table columns={columns} dataSource={data} {...!!this.state.addScroll && { scroll: { x: 1000 } }} pagination={{ pageSize: 4 }} />
                    </Card.Body>
                </Card>
            </header>
        </div>;
}
</code>```

Body:
I have a table component here from antd

Basically I wanted to remove the scroll={{ x:1000 }} when I make the browser window bigger, say than 1200px. How can I achieve this in React?

Booking function()

function booking() {

    return (

            <Navbar bg="" variant="dark" id="mainHeader">

                Home
                Bookings
                Sign Out
            </Navbar>

            {/* My Booking Table */}

                    <Card border="light" className="bookingCard" id="bookingCard" >

Upcoming Bookings

                        <Card.Body>

                        </Card.Body>
                    </Card>

    );
}

I wanted to change the Table there.

Body:
Use resize event listener, something like that:

const threshold = 1200;

window.addEventListener('resize', () => {
    const addScroll = !this.state.addScroll;
    return window.innerWidth >= threshold ^ !addScroll && this.setState({ addScroll });
});

And in render method:

<Table columns={columns}
       dataSource={data}
       {...!!this.state.addScroll && { scroll: { x: 1000 }}
       pagination={{ pageSize: 4 }} />

So your full code should look like this:

class Booking extends React.Component {
    constructor () {
        const widthThreshold = 1200;

        window.addEventListener('resize', () => {
            const addScroll = !this.state.addScroll;
            return window.innerWidth >= widthThreshold ^ !addScroll && this.setState({ addScroll });
        });
    }

    render = () =>
        <div className="booking">
            <Navbar bg="" variant="dark" id="mainHeader">
                <Nav className="mr-auto">
                    <img src={logo} width="110" height="50" className="d-inline-block align-top ml-4 mt-3 mb-2" alt="logo"/>
                </Nav>
                <Nav.Link a href="/" className="ml-sm-4 text-dark">Home</Nav.Link>
                <Nav.Link a href="/bookings" className=" text-dark">Bookings</Nav.Link>
                <Nav.Link a href="/signout" className="mr-sm-4 text-dark">Sign Out</Nav.Link>
            </Navbar>

            <header className="App-header text-dark">
                <Card border="light" className="bookingCard" id="bookingCard" >
                    <h1 className="mt-4 ml-4" id="cardTitle">Upcoming Bookings</h1>
                    <Card.Body>
                        <Table columns={columns} dataSource={data} {...!!this.state.addScroll && { scroll: { x: 1000 } }} pagination={{ pageSize: 4 }} />
                    </Card.Body>
                </Card>
            </header>
        </div>;
}


问:
Validate format of subdomain
How can I properly validate a subdomain format?

Here's what I've got:

```<code>  validates :subdomain, uniqueness: true, case_sensitive: false
  validates :subdomain, format: { with: /\A[A-Za-z0-9-]+\z/, message: "not a valid subdomain" }
  validates :subdomain, exclusion: { in: %w(support blog billing help api www host admin en ru pl ua us), message: "%{value} is reserved." }
  validates :subdomain, length: { maximum: 20 }
  before_validation :downcase_subdomain
  protected
    def downcase_subdomain
      self.subdomain.downcase! if attribute_present?("subdomain")
    end
</code>```

Question:

Is there a standard REGEX subdomain validation like there is for email? What is the best REGEX for subdomain to use?

validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true

答:
RFC 1035 defines subdomain syntax like so:

```<code><subdomain> ::= <label> | <subdomain> "." <label>

<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]

<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>

<let-dig-hyp> ::= <let-dig> | "-"

<let-dig> ::= <letter> | <digit>

<letter> ::= any one of the 52 alphabetic characters A through Z in
upper case and a through z in lower case

<digit> ::= any one of the ten digits 0 through 9
</code>```

And a merciful human readable description.

[Labels] must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen.  There are also some
restrictions on the length. Labels must be 63 characters or less.

We can do most of this with a regex, and the length restriction separately.

```<code>validates :subdomain, format: {
  with: %r{\A[a-z](?:[a-z0-9-]*[a-z0-9])?\z}i, message: "not a valid subdomain"
}, length: { in: 1..63 }
</code>```

Pulling that regex into pieces to explain it.

```<code>%r{
  \A
  [a-z]                       # must start with a letter
  (?:
    [a-z0-9-]*                # might contain alpha-numerics or a dash
    [a-z0-9]                  # must end with a letter or digit
  )?                          # that's all optional
 \z
}ix
</code>```

We might be tempted to use the simpler /\A[a-z][a-z0-9-]*[a-z0-9]?\z/i but this allows foo-.

See also Regexp for subdomain.

Title: Validate format of subdomain

Body:
How can I properly validate a subdomain format?

Here's what I've got:

  validates :subdomain, uniqueness: true, case_sensitive: false
  validates :subdomain, format: { with: /\A[A-Za-z0-9-]+\z/, message: "not a valid subdomain" }
  validates :subdomain, exclusion: { in: %w(support blog billing help api www host admin en ru pl ua us), message: "%{value} is reserved." }
  validates :subdomain, length: { maximum: 20 }
  before_validation :downcase_subdomain
  protected
    def downcase_subdomain
      self.subdomain.downcase! if attribute_present?("subdomain")
    end

Question:

Is there a standard REGEX subdomain validation like there is for email? What is the best REGEX for subdomain to use?

validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true

Body:
RFC 1035 defines subdomain syntax like so:

<subdomain> ::= <label> | <subdomain> "." <label>

<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]

<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>

<let-dig-hyp> ::= <let-dig> | "-"

<let-dig> ::= <letter> | <digit>

<letter> ::= any one of the 52 alphabetic characters A through Z in
upper case and a through z in lower case

<digit> ::= any one of the ten digits 0 through 9

And a merciful human readable description.

[Labels] must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen.  There are also some
restrictions on the length. Labels must be 63 characters or less.

We can do most of this with a regex, and the length restriction separately.

validates :subdomain, format: {
  with: %r{\A[a-z](?:[a-z0-9-]*[a-z0-9])?\z}i, message: "not a valid subdomain"
}, length: { in: 1..63 }

Pulling that regex into pieces to explain it.

%r{
  \A
  [a-z]                       # must start with a letter
  (?:
    [a-z0-9-]*                # might contain alpha-numerics or a dash
    [a-z0-9]                  # must end with a letter or digit
  )?                          # that's all optional
 \z
}ix

We might be tempted to use the simpler /\A[a-z][a-z0-9-]*[a-z0-9]?\z/i but this allows foo-.

See also Regexp for subdomain.


问:
Finding frequency of range of numbers in Mathematica
Given a list of numbers in Mathematica, how would I extract from that list the total number of numbers between numbers a and b that I specify?

答:
The most direct way is simply:

```<code>Count[data, x_ /; a <= x <= b]
</code>```

There are however much faster ways for most data, this one thanks to Carl Woll:

```<code>Tr@Unitize@Clip[data, {a, b}, {0, 0}]
</code>```

Carl Woll's method is particularly fast, but as yoda pointed out, it fails if your list contains zeros, and your range also straddles zero.  Here is another method from Kevin J. McCann that handles this case, and is still very fast:

```<code>Tr@UnitStep[(data - a)*(b - data)]
</code>```

As a pure function [data, a, b]:

```<code>Tr@UnitStep[(#-#2)*(#3-#)]&
</code>```

Body:
The most direct way is simply:

Count[data, x_ /; a <= x <= b]

There are however much faster ways for most data, this one thanks to Carl Woll:

Tr@Unitize@Clip[data, {a, b}, {0, 0}]

Carl Woll's method is particularly fast, but as yoda pointed out, it fails if your list contains zeros, and your range also straddles zero.  Here is another method from Kevin J. McCann that handles this case, and is still very fast:

Tr@UnitStep[(data - a)*(b - data)]

As a pure function [data, a, b]:

Tr@UnitStep[(#-#2)*(#3-#)]&


问:
Print a multi-line message with NLog.Extensions.Logging (1.6.1)
I'm developing a .NET Core 3.1 console application with NLog.Extensions.Logging (v1.6.1) and would like to print a message containing multiple lines.  I followed an example previously provided on StackOverflow using a wrapper layout renderer, copied below.  I see how new lines are being replaced; however, the beginning of the multi-line message (i.e. the first line) is not being prefixed (?)

Output from Example looks like this:

```<code>line 1> Hello world
line 2> 19:30:45.2313 Hello World , again
</code>```

Previously Provided Example Code:

```<code>using System;
using System.Text;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.LayoutRenderers.Wrappers;
using NLog.Layouts;

namespace My.Namespace
{
    [LayoutRenderer("replace-newlines-withlayout")]
    [ThreadAgnostic]
    public class ReplaceNewLinesFormatLayoutRendererWrapper : WrapperLayoutRendererBase
    {
        private string m_replacementString = " ";

        public ReplaceNewLinesFormatLayoutRendererWrapper()
        {
            // Changed from
            // Replacement = " ";
            Replacement = Layout.FromString(" ");
        }

        // Changed from
        // public string Replacement { get; set; }
        public Layout Replacement { get; set; }

        // Override Append in order to render the replacement.
        protected override void  Append(StringBuilder builder, NLog.LogEventInfo logEvent)
        {
            // Render...
            m_replacementString = Replacement.Render(logEvent);

            // The base functionality of append is fine.
            base.Append(builder, logEvent);
        }

        // Called from base.Append()
        protected override string Transform(string text)
        {
            // Changed from
            // return text.Replace(Environment.NewLine, Replacement);

            // Now just put in the rendered replacement string.
            return text.Replace(Environment.NewLine, m_replacementString);
        }
    }
}
</code>```

NLog.Config

```<code><?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogFile="c:\temp\console-example-internal.log"
      internalLogLevel="Info" >

  <!-- the targets to write to -->
  <targets>
    <!-- write logs to file -->
    <target xsi:type="File" name="target1" fileName="c:\temp\console-example.log"
        layout="${replace-newlines-withlayout:replacement=${newline}${time}:${message}}" />
    <target xsi:type="ColoredConsole" name="target2"
        layout="${replace-newlines-withlayout:replacement=${newline}${time}:${message}}" />
    />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="*" minlevel="Trace" writeTo="target1,target2" />
  </rules>
</nlog>
</code>```

Example:

```<code>class Program
{
    private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

    static void Main(string[] args)
    {
        try
        {
            var ReplaceNewLinesFormatLayoutRendererWrapper = new ReplaceNewLinesFormatLayoutRendererWrapper();
            ReplaceNewLinesFormatLayoutRendererWrapper.Register<NLogTest.ReplaceNewLinesFormatLayoutRendererWrapper>("replace-newlines-withlayout");

            Logger.Info($"Hello world {Environment.NewLine} Hello World , again");
            System.Console.ReadKey();
        }
        catch (Exception ex)
        {
            Logger.Error(ex, "yeah, here again");
        }
    }
}
</code>```

答:
The problem here is that Transform is called before Append, and your  Transform is depended of Append.

Luckily nowadays there is a better Transform to override, the one that will also include the LogEventInfo as parameter. (NLog 4.5+)

So you could drop the current Append and Transform overwrites, and replace with:

```<code>protected override string Transform(LogEventInfo logEvent, string text)
{
    // Render...
    var replacementString = Replacement.Render(logEvent);

    // Now just put in the rendered replacement string.
    return text.Replace(Environment.NewLine, replacementString);
}
</code>```

Full code (after some cleaning):

```<code>using System;
using NLog;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.LayoutRenderers.Wrappers;
using NLog.Layouts;

namespace My.Namespace
{
    [LayoutRenderer("replace-newlines-withlayout")]
    [ThreadAgnostic]
    public class ReplaceNewLinesFormatLayoutRendererWrapper : WrapperLayoutRendererBase
    {
        public Layout Replacement { get; set; } = " ";

        protected override string Transform(LogEventInfo logEvent, string text)
        {
            // Render...
            var replacementString = Replacement.Render(logEvent);

            // Now just put in the rendered replacement string.
            return text.Replace(Environment.NewLine, replacementString);
        }
    }
}
</code>```

Body:
I'm developing a .NET Core 3.1 console application with NLog.Extensions.Logging (v1.6.1) and would like to print a message containing multiple lines.  I followed an example previously provided on StackOverflow using a wrapper layout renderer, copied below.  I see how new lines are being replaced; however, the beginning of the multi-line message (i.e. the first line) is not being prefixed (?)

Output from Example looks like this:

line 1> Hello world
line 2> 19:30:45.2313 Hello World , again

Previously Provided Example Code:

using System;
using System.Text;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.LayoutRenderers.Wrappers;
using NLog.Layouts;

namespace My.Namespace
{
    [LayoutRenderer("replace-newlines-withlayout")]
    [ThreadAgnostic]
    public class ReplaceNewLinesFormatLayoutRendererWrapper : WrapperLayoutRendererBase
    {
        private string m_replacementString = " ";

        public ReplaceNewLinesFormatLayoutRendererWrapper()
        {
            // Changed from
            // Replacement = " ";
            Replacement = Layout.FromString(" ");
        }

        // Changed from
        // public string Replacement { get; set; }
        public Layout Replacement { get; set; }

        // Override Append in order to render the replacement.
        protected override void  Append(StringBuilder builder, NLog.LogEventInfo logEvent)
        {
            // Render...
            m_replacementString = Replacement.Render(logEvent);

            // The base functionality of append is fine.
            base.Append(builder, logEvent);
        }

        // Called from base.Append()
        protected override string Transform(string text)
        {
            // Changed from
            // return text.Replace(Environment.NewLine, Replacement);

            // Now just put in the rendered replacement string.
            return text.Replace(Environment.NewLine, m_replacementString);
        }
    }
}

NLog.Config

<?xml version="1.0" encoding="utf-8" ?>

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogFile="c:\temp\console-example-internal.log"
      internalLogLevel="Info" >

  <targets>

    <target xsi:type="File" name="target1" fileName="c:\temp\console-example.log"
        layout="${replace-newlines-withlayout:replacement=${newline}${time}:${message}}" />
    <target xsi:type="ColoredConsole" name="target2"
        layout="${replace-newlines-withlayout:replacement=${newline}${time}:${message}}" />
    />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="target1,target2" />
  </rules>
</nlog>

Example:

class Program
{
    private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

    static void Main(string[] args)
    {
        try
        {
            var ReplaceNewLinesFormatLayoutRendererWrapper = new ReplaceNewLinesFormatLayoutRendererWrapper();
            ReplaceNewLinesFormatLayoutRendererWrapper.Register<NLogTest.ReplaceNewLinesFormatLayoutRendererWrapper>("replace-newlines-withlayout");

            Logger.Info($"Hello world {Environment.NewLine} Hello World , again");
            System.Console.ReadKey();
        }
        catch (Exception ex)
        {
            Logger.Error(ex, "yeah, here again");
        }
    }
}

Body:
The problem here is that Transform is called before Append, and your  Transform is depended of Append.

Luckily nowadays there is a better Transform to override, the one that will also include the LogEventInfo as parameter. (NLog 4.5+)

So you could drop the current Append and Transform overwrites, and replace with:

protected override string Transform(LogEventInfo logEvent, string text)
{
    // Render...
    var replacementString = Replacement.Render(logEvent);

    // Now just put in the rendered replacement string.
    return text.Replace(Environment.NewLine, replacementString);
}

Full code (after some cleaning):

using System;
using NLog;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.LayoutRenderers.Wrappers;
using NLog.Layouts;

namespace My.Namespace
{
    [LayoutRenderer("replace-newlines-withlayout")]
    [ThreadAgnostic]
    public class ReplaceNewLinesFormatLayoutRendererWrapper : WrapperLayoutRendererBase
    {
        public Layout Replacement { get; set; } = " ";

        protected override string Transform(LogEventInfo logEvent, string text)
        {
            // Render...
            var replacementString = Replacement.Render(logEvent);

            // Now just put in the rendered replacement string.
            return text.Replace(Environment.NewLine, replacementString);
        }
    }
}


问:
signtool.exe Error: SignerSign() failed (-2147220492/0x800403f4) when signing Excel Macro
I am currently trying to sign VBA macros in Excel and Word and Powershell scripts via the cmd.

For this purpose I use the Microsoft signtool.exe as part of the Windows 10 SDK:
https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk/

Signing Powershell scripts using the following command works fine:

signtool.exe sign /f "testca.pfx" /fd SHA256 /p "123456" "test.ps1"

For signing macros in Office documents, "Microsoft Office Subject Interface Packages for Digitally Signing VBA Projects" is also required:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=56617

I downloaded the .exe and executed it.

Registering the .dll files using: "regsvr32" also worked. The paths to the .dll files are visible in the registry.

Microsoft Visual C++ 2015-2019 Redistributable (x86) is also installed.

So theoretically I should be able to sign Excel macros now.
However, my server receives an error message with an error code, which I can't find anything about in Google.

Here is the complete output:

The following certificates were considered:

  Issued to: TestCA

      Issued by: TestCA

      Expires: Wed Feb 03 08:53:09 2021

      SHA1 hash: E19CE363C88CDBCE677FA170008D0AB0D98A02DC

After EKU filter, one certs were left. After expiry filter, 1 certs were left.
  After Private Key filter, 1 certs were left. The following certificate was selected:

      Issued to: TestCA

      Issued by: TestCA

      Expires: Wed Feb 03 08:53:09 2021

      SHA1 hash: E19CE363C88CDBCE677FA170008D0AB0D98A02DC

The following additional certificates will be attached: Done Adding

  Additional Store SignTool Error: An unexpected internal error has occurred.

  Error information: "Error: SignerSign() failed. (-2147220492/0x800403f4)

I have now performed the installation as described above several times on different computers, all with admin rights and current updates:

Private win10 computer: .ps1 and .xlsm signing is possible

VM with win10 from work: .ps1 and .xlsm signing is possible

Private VM with win 7: only .ps1 signing works

Private VM with Win10: only .ps1 signing works

TerminalServer with Windows Server 2012: only .ps1 signing works

What you can probably say is that signtool.exe works as such, because I can sign PowerShell scripts on all machines.
But signing macros does not always work. Probably has something to do with the interface packages?

I hope somebody has had the same problem before and can help. Because I'm going crazy with this. Thanks a lot!

答:
Well, it took me a long time, but I'm up and running now. If anybody else has the same problem:

The "Microsoft Office Subject Interface Packages for Digitally Signing VBA Projects" .zip contained a readme.txt saying "Microsoft Visual C++ Redistributal (x86)" must be installed. The link behind it downloaded the "Microsoft Visual C++ 2015-2019 Redistributal (x86)", which is the latest version.

I have now tested all available versions of the last years and the only working one is the 2010 version. I don't know why the readme.txt refers to a different version, but the bug and the "solution" are definitely reproducible.

Anyway, use Visual C++ 2010 and everything is fine!


问:
transform list of lists to dataframes of list where sub lists have different number of rows
I am trying to transform a list of lists into data frames of list. The sub lists, however, have same number of columns but "different number of rows". When I run the command as.data.frame(parent_list), it generates: "Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : arguments imply differing number of rows" errors. How can I convert this into a list of data frames with each frame having a different number of rows? I have tried solutions using different threads but no luck. Any help would be appreciated. Here is a sample code:

```<code># making a list of 2 sub lists with different number of elements.
list <- list(A = c(1,2,3), B = c(1,2))

# trying to convert sub lists into data frames.
list_to_dataframe <- as.data.frame(list)
#> Error: "Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,
#> : arguments imply differing number of rows: 3, 2"
</code>```

Thanks!

答:
You can simply use the rbind function to get the data frame, but as suggested above your list contains two vectors of unequal length so you will get the desired result with a warning:

```<code>list <- list(A = c(1,2,3), B = c(1,2))

# sub lists into data frames.
do.call(rbind, list)
</code>```

Output:

```<code>  [,1] [,2] [,3]
A    1    2    3
B    1    2    1
</code>```

Warning message:
  In (function (..., deparse.level = 1)  :
    number of columns of result is not a multiple of vector length (arg 2)

Body:
I am trying to transform a list of lists into data frames of list. The sub lists, however, have same number of columns but "different number of rows". When I run the command as.data.frame(parent_list), it generates: "Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : arguments imply differing number of rows" errors. How can I convert this into a list of data frames with each frame having a different number of rows? I have tried solutions using different threads but no luck. Any help would be appreciated. Here is a sample code:

# making a list of 2 sub lists with different number of elements.
list <- list(A = c(1,2,3), B = c(1,2))

# trying to convert sub lists into data frames.
list_to_dataframe <- as.data.frame(list)
#> Error: "Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,
#> : arguments imply differing number of rows: 3, 2"

Thanks!

Body:
You can simply use the rbind function to get the data frame, but as suggested above your list contains two vectors of unequal length so you will get the desired result with a warning:

list <- list(A = c(1,2,3), B = c(1,2))

# sub lists into data frames.
do.call(rbind, list)

Output:

  [,1] [,2] [,3]
A    1    2    3
B    1    2    1

Warning message:
  In (function (..., deparse.level = 1)  :
    number of columns of result is not a multiple of vector length (arg 2)


问:
Python sklearn sparse pca: something wrong while computing variance of PCs
I am trying to compute principal components of n*p matrix X using the sklearn.decomposition.SparsePCA.

The n indicates the number of samples and the p indicates the number of features.

Then I follow the way in this paper(https://www.cc.gatech.edu/~isbell/reading/papers/sparsepc.pdf) to compute the adjusted variance of each principal components.

As it in the paper, I use QR decomposition to the modified PCs, and compute adjusted variance by (R[j][j])^2.

But the results( variance of modified PCs ) are not monotone decreasing. As following, variance of 3rd PC is larger than the one of 2nd PC:

variance of PCs:

```<code>[ 1.          0.59902395  0.64761414  0.64657846  0.50450935  0.59905398 0.45242596  0.42738204]
</code>```

variance_ratio of PCs:

```<code>[ 0.20506141  0.1228367   0.13280067  0.13258829  0.1034554   0.12284286
  0.09277511  0.08763956]
</code>```

Is it because the output of sklearn.decomposition.SparsePCA.transform whithout order of the PCs, or something wrong with my codes?

I would appreciate it if someone could solve my problem.

The following is my code:

```<code>    # read n*p matrix X( n = 1043, p = 20 )
    cluster_data = input_cluster_data('./data/km_results_1.csv')

    # train spca
    model = decomposition.SparsePCA( n_components=8 )
    model.fit( cluster_data )
    cluster_data_projection = model.transform( cluster_data )

    # QR decomposition of modified PCs
    cluster_data_projection_qr = np.linalg.qr( cluster_data_projection )
    q = cluster_data_projection_qr[0]
    r = cluster_data_projection_qr[1]

    # compute adjusted variance
    variance = []
    for i in range(8):
        variance.append( np.square(r[i][i]) )

    variance = np.array(variance)

    # compute variance_ratio
    sum = np.sum( variance )
    variance_ratio = variance / sum
</code>```

答: I believe this is due to the fact that PCs obtained by Elastic net regression (the method used in scikit learn) are not uncorrelated nor orthogonal, and therefore might not have the same properties as PCs from PCA</p>

Body:
I am trying to compute principal components of n*p matrix X using the sklearn.decomposition.SparsePCA.

The n indicates the number of samples and the p indicates the number of features.

Then I follow the way in this paper(https://www.cc.gatech.edu/~isbell/reading/papers/sparsepc.pdf) to compute the adjusted variance of each principal components.

As it in the paper, I use QR decomposition to the modified PCs, and compute adjusted variance by (R[j][j])^2.

But the results( variance of modified PCs ) are not monotone decreasing. As following, variance of 3rd PC is larger than the one of 2nd PC:

variance of PCs:

[ 1.          0.59902395  0.64761414  0.64657846  0.50450935  0.59905398 0.45242596  0.42738204]

variance_ratio of PCs:

[ 0.20506141  0.1228367   0.13280067  0.13258829  0.1034554   0.12284286
  0.09277511  0.08763956]

Is it because the output of sklearn.decomposition.SparsePCA.transform whithout order of the PCs, or something wrong with my codes?

I would appreciate it if someone could solve my problem.

The following is my code:

    # read n*p matrix X( n = 1043, p = 20 )
    cluster_data = input_cluster_data('./data/km_results_1.csv')

    # train spca
    model = decomposition.SparsePCA( n_components=8 )
    model.fit( cluster_data )
    cluster_data_projection = model.transform( cluster_data )

    # QR decomposition of modified PCs
    cluster_data_projection_qr = np.linalg.qr( cluster_data_projection )
    q = cluster_data_projection_qr[0]
    r = cluster_data_projection_qr[1]

    # compute adjusted variance
    variance = []
    for i in range(8):
        variance.append( np.square(r[i][i]) )

    variance = np.array(variance)

    # compute variance_ratio
    sum = np.sum( variance )
    variance_ratio = variance / sum


问:
Segmentation Fault when starting google-chrome-stable on Ubuntu 18.04
Just started experiencing crashes when trying to start google-chrome-stable with Xvfb via the CLI. Specifically the AWS EC2 AMI "Canonical, Ubuntu, 18.04 LTS, amd64 bionic image build on 2018-09-12"

Chrome has been running fine on this instance for months, and suddenly started crashing today. Not only that, but it also started crashing on a separate cloned instance that I hadn't touched. Very strange. Perhaps related to whatever update was recently pushed for this AMI?

On first attempt at invoking '$ google-chrome-stable' I get the following error:

```<code>[1917:1917:0213/165941.946941:ERROR:browser_main_loop.cc(1512)] Unable to open X display.
</code>```

On later attempts I get a segmentation fault.

```<code>[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `/opt/google/chrome/chrome    '.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f7c532462dd in ?? () from /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0
</code>```

Any ideas how to investigate what's going on with google-chrome-stable? Perhaps I could try to rollback the AMI to a previous version, or just start up a new instance. I strongly believe it was the AMI upgrade since Selenium/google-chrome-stable was running just fine this morning before the update. And the update broke two separate EC2 instances, one of which hadn't been touched today.

答: Found a workaround just by using the pip3 module "pyvirtualdisplay" which is a wrapper for X. I still don't know what caused Xvfb to break, but if you find this question with the same problem try using that module.</p>

Body:
Just started experiencing crashes when trying to start google-chrome-stable with Xvfb via the CLI. Specifically the AWS EC2 AMI "Canonical, Ubuntu, 18.04 LTS, amd64 bionic image build on 2018-09-12"

Chrome has been running fine on this instance for months, and suddenly started crashing today. Not only that, but it also started crashing on a separate cloned instance that I hadn't touched. Very strange. Perhaps related to whatever update was recently pushed for this AMI?

On first attempt at invoking '$ google-chrome-stable' I get the following error:

[1917:1917:0213/165941.946941:ERROR:browser_main_loop.cc(1512)] Unable to open X display.

On later attempts I get a segmentation fault.

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `/opt/google/chrome/chrome    '.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f7c532462dd in ?? () from /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0

Any ideas how to investigate what's going on with google-chrome-stable? Perhaps I could try to rollback the AMI to a previous version, or just start up a new instance. I strongly believe it was the AMI upgrade since Selenium/google-chrome-stable was running just fine this morning before the update. And the update broke two separate EC2 instances, one of which hadn't been touched today.


问:
Not able to calculate average with MongoDB
I wonder that I can't get calculate the average with Spring 5 and MongoDB.

In the database are 40 entries with this format

Result(id=5e4adeca9eab5f052e2ac239, date=2020-02-12,
  value=1.048097596376957)

I'm interested to get the date from t1 to t2 and the average of all matches.

The code which I wrote and ens in an NPE looks like this:

```<code>    MatchOperation filterDate = match(Criteria.where("date").gte(startWeek).lte(now));
    GroupOperation groupByValue = group("value").avg("value").as("averageValue");

    Aggregation aggregation = newAggregation(filterDate, groupByValue);
    AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, "unknown", Document.class);

    Document document = result.getUniqueMappedResult();
    document.getDouble("averageValue");
</code>```

When getDouble will be invoked a NPE happens.
The matchCriteria is in my opinion correct.
By the GroupOperation I have my doubts.

2020-02-17 19:43:22.661 DEBUG 10148 --- [nio-8080-exec-1]
  o.s.data.mongodb.core.MongoTemplate      : Executing aggregation: [{
  "$match" : { "date" : { "$gte" : { "$date" : 1581289200000}, "$lte" :
  { "$date" : 1581894000000}}}}, { "$group" : { "_id" : "$value",
  "averageValues" : { "$avg" : "$value"}}}] in collection unkown

```<code>import org.springframework.data.annotation.Id;

public class Result {

    @Id
    private String id;

    private LocalDate date;

    private Double value;

    public Result(LocalDate date, Double value) {
        this.date = date;
        this.value = value;
    }
}
</code>```

I wonder about the grouping. It this necessary?
And do I need a collection?

答:
To calculate avg, you need to $group similar records and use $avg operator.

In your case, you want calculate average of all matches, but I guess you don't have a field with the same value for all filtered matches. In this case, you may order MongoDB group by null to calculate average.

Change your Group stage to below:

```<code>GroupOperation groupByValue = group().avg("value").as("averageValue");
</code>```

which will be translated to:

```<code>{ "$group" : { "_id" : null, "averageValues" : { "$avg" : "$value"}}
</code>```

Body:
I wonder that I can't get calculate the average with Spring 5 and MongoDB.

In the database are 40 entries with this format

Result(id=5e4adeca9eab5f052e2ac239, date=2020-02-12,
  value=1.048097596376957)

I'm interested to get the date from t1 to t2 and the average of all matches.

The code which I wrote and ens in an NPE looks like this:

    MatchOperation filterDate = match(Criteria.where("date").gte(startWeek).lte(now));
    GroupOperation groupByValue = group("value").avg("value").as("averageValue");

    Aggregation aggregation = newAggregation(filterDate, groupByValue);
    AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, "unknown", Document.class);

    Document document = result.getUniqueMappedResult();
    document.getDouble("averageValue");

When getDouble will be invoked a NPE happens.
The matchCriteria is in my opinion correct.
By the GroupOperation I have my doubts.

2020-02-17 19:43:22.661 DEBUG 10148 --- [nio-8080-exec-1]
  o.s.data.mongodb.core.MongoTemplate      : Executing aggregation: [{
  "$match" : { "date" : { "$gte" : { "$date" : 1581289200000}, "$lte" :
  { "$date" : 1581894000000}}}}, { "$group" : { "_id" : "$value",
  "averageValues" : { "$avg" : "$value"}}}] in collection unkown

import org.springframework.data.annotation.Id;

public class Result {

    @Id
    private String id;

    private LocalDate date;

    private Double value;

    public Result(LocalDate date, Double value) {
        this.date = date;
        this.value = value;
    }
}

I wonder about the grouping. It this necessary?
And do I need a collection?

Body:
To calculate avg, you need to $group similar records and use $avg operator.

In your case, you want calculate average of all matches, but I guess you don't have a field with the same value for all filtered matches. In this case, you may order MongoDB group by null to calculate average.

Change your Group stage to below:

GroupOperation groupByValue = group().avg("value").as("averageValue");

which will be translated to:

{ "$group" : { "_id" : null, "averageValues" : { "$avg" : "$value"}}


问:
What's the difference between %s and %d in string formatting?
I don't understand what %s and %d do and how they work.

答:
%s is used to hold space for string
%d is used to hold space for number

```<code>name = "Moses";
age = 23
print("My name is %s am CEO at MoTech Computers " %name)
print("Current am %d years old" %age)
print("So Am %s and am %d years old" %(name,age))
</code>```

Program output

this video goes deep about that tip https://www.youtube.com/watch?v=4zN5YsuiqMA

Body:
%s is used to hold space for string
%d is used to hold space for number

name = "Moses";
age = 23
print("My name is %s am CEO at MoTech Computers " %name)
print("Current am %d years old" %age)
print("So Am %s and am %d years old" %(name,age))

Program output

this video goes deep about that tip https://www.youtube.com/watch?v=4zN5YsuiqMA


问:
How understand the granularity and block in ClickHouse?
I am not clear about these two words.
Whether does one block have a fixed number of rows?
Whether is one block the minimum unit to read from disk?
Whether are different blocks stored in different files?
Whether is the range of one block bigger than granule? That means, one block can have several granules skip indices.</p>

答:
https://clickhouse.tech/docs/en/development/architecture/#block

Block can contain any number of rows.
For example 1 row blocks:

```<code>set max_block_size=1;
SELECT * FROM numbers_mt(1000000000) LIMIT 3;

┌─number─┐
│      0 │
└────────┘
┌─number─┐
│      2 │
└────────┘
┌─number─┐
│      3 │
└────────┘

set max_block_size=100000000000;

create table X (A Int64) Engine=Memory;
insert into X values(1);
insert into X values(2);
insert into X values(3);
SELECT * FROM X;

┌─A─┐
│ 1 │
└───┘
┌─A─┐
│ 3 │
└───┘
┌─A─┐
│ 2 │
└───┘
</code>```

3 rows in block

```<code>drop table X;
create table X (A Int64) Engine=Memory;
insert into X values(1)(2)(3);
select * from X
┌─A─┐
│ 1 │
│ 2 │
│ 3 │
└───┘
</code>```

Body:
https://clickhouse.tech/docs/en/development/architecture/#block

Block can contain any number of rows.
For example 1 row blocks:

set max_block_size=1;
SELECT * FROM numbers_mt(1000000000) LIMIT 3;

┌─number─┐
│      0 │
└────────┘
┌─number─┐
│      2 │
└────────┘
┌─number─┐
│      3 │
└────────┘

set max_block_size=100000000000;

create table X (A Int64) Engine=Memory;
insert into X values(1);
insert into X values(2);
insert into X values(3);
SELECT * FROM X;

┌─A─┐
│ 1 │
└───┘
┌─A─┐
│ 3 │
└───┘
┌─A─┐
│ 2 │
└───┘

3 rows in block

drop table X;
create table X (A Int64) Engine=Memory;
insert into X values(1)(2)(3);
select * from X
┌─A─┐
│ 1 │
│ 2 │
│ 3 │
└───┘


问:
Coinpayment Laravel
I have installed Coinpayment with https://github.com/hexters/CoinPayment inside mine project.

The problem what i;m getting is Coinpayments can't send IPN request to mine server or there is somewhere timeout.
Now i have try all option and try to reinstall i'm not getting success to solve this problem.

Here is the picture what i'm getting in Coinpayments

ENV file

```<code>COINPAYMENT_PUBLIC_KEY=x
COINPAYMENT_PRIVATE_KEY=x
COINPAYMENT_CURRENCY=EUR
COINPAYMENT_IPN_ACTIVATE=true
COINPAYMENT_MARCHANT_ID=x
COINPAYMENT_IPN_SECRET=x
COINPAYMENT_IPN_DEBUG_EMAIL=x
</code>```

I have also add the path for Except in App\Http\Middleware\VerifyCsrfToken

```<code>protected $except = [
        '/coinpayment/ipn',
    ];
</code>```

Can anyone help me to solve this problem?

答:
I have solved this problem it seems mine friend has turn the Laravel project offline when i was busy thats why coinpayments IPN can't connect with the laravel project.
thanks @larabee for your support</p>

Title: Coinpayment Laravel

Body:
I have installed Coinpayment with https://github.com/hexters/CoinPayment inside mine project.

The problem what i;m getting is Coinpayments can't send IPN request to mine server or there is somewhere timeout.
Now i have try all option and try to reinstall i'm not getting success to solve this problem.

Here is the picture what i'm getting in Coinpayments

ENV file

COINPAYMENT_PUBLIC_KEY=x
COINPAYMENT_PRIVATE_KEY=x
COINPAYMENT_CURRENCY=EUR
COINPAYMENT_IPN_ACTIVATE=true
COINPAYMENT_MARCHANT_ID=x
COINPAYMENT_IPN_SECRET=x
COINPAYMENT_IPN_DEBUG_EMAIL=x

I have also add the path for Except in App\Http\Middleware\VerifyCsrfToken

protected $except = [
        '/coinpayment/ipn',
    ];

Can anyone help me to solve this problem?


问:
ISFORMULA function conditional formatting, exclude formulas that contain specific phrase
I have a spreadsheet where I would like to highlight cells when a user enters a formula in a cell.

The issue I have is there are other cells that contain formulas in the same range. The Formula in particular is this one =SUMIFS(B13:B99,AB13:AB99,TRUE) The cell ranges can change, but the rest will not.

I would like to set up conditional formatting that says, If the cell contains the SUMIF string, then do not apply the conditional formatting.

The user will just be entering simple formulas. Ex =10,000*1.34, or =5,733+2,000+1,000. They could be putting in addition, subtraction, multiplication, division and parentheses. I don't think they would need any more functionality. So we could also use those variables as well.

The reason I cannot just manually do this, is because the sheet is dynamic and imports data. The locations of the cells I do not want to highlight are always changing.

I am open to using VBA or just regular old Excel formulas.

My current formula is simply =ISFORMULA(B11:B99) and it returns a value of true or false. How can I write a condition to say, if there is a formula that does not contain SUMIF, return true, else false?

答:
You could use a simple custom function, such as:</p>

```<code>Function IS_SUMIFS(r As Range) As Boolean

If r.HasFormula Then
    If InStr(r.Formula, "SUMIFS") = 0 Then IS_SUMIFS = True   'formula doesn't contain SUMIFS
End If

End Function
</code>```

Body:
You could use a simple custom function, such as:

Function IS_SUMIFS(r As Range) As Boolean

If r.HasFormula Then
    If InStr(r.Formula, "SUMIFS") = 0 Then IS_SUMIFS = True   'formula doesn't contain SUMIFS
End If

End Function


问:
Video hosting - compress video?
I am planning to add videos to my website (1 minute long or less clips for short greetings). Since this is a startup, video size is very important.

What format is the best to use for storing the uploaded video (e.g.: mp4, avi, mpeg, flv)? Requirement: quality should be decent enough while keeping the file size as low as possible.

My platform is PHP. Are there built-in or open source frameworks for video compression/conversion if a user uploads a video in a different format? For photos we use imaggemagik; is there anything similar for video?

Ideally, I want to avoid using a Flash video player. Is there a better option?

答:
1) What format is the best to use for storing the uploaded video - mp4, avi, mpeg, flash, etc? (req is quality should be decent enough while keeping fiel size as low as possible).

mp4 containing h.264. Then WebM as a backup.

2) My platform is in PHP, are there built -in or open source frameworks for video compression/conversion if a user uploads a different format video? (Like for photos we use imaggemagik, similarly anything for video?)

ffmpeg seems popular

3) Ideally i want to avoid using flash for video if there is a better option?

You'll need Flash for backwards compatibility at least, but between h.264 and WebM you will have wide support in HTML 5 video.

Title: Video hosting - compress video?


问:
How to convert multiple array to single array
Sorry for asking this silly question here. I am very new to PHP language.
I am trying to know how can i convert the array.

I want to convert this array to single array like.

Convert this :-

```<code>Array ( [0] => user )
Array ( [0] => user1 )
Array ( [0] => user2 )
Array ( [0] => user3 )
Array ( [0] => user8 )
Array ( [0] => user7 )
Array ( [0] => user6 )
</code>```

Convert To :-

```<code>Array("user", "user1", "user2", "user3", "user4", "user5", "user6");
</code>```

答:
Another alternative, aside from array_merge, for the case of an arbitrary number of subarrays, you could use array_reduce and build your final array:

```<code>$inArray = [['user'],['user1'],['user2'],['user3'],['user8'],['user7'],['user6']];

$inArray = array_reduce($inArray, function($arr, $elem){
    $arr[] = $elem[0];
    return $arr;
});
</code>```

Of course, the straightforward solution would be to use array_merge:

```<code>$inArray = array_merge(...$inArray);
</code>```

Body:
Sorry for asking this silly question here. I am very new to PHP language.
I am trying to know how can i convert the array.

I want to convert this array to single array like.

Convert this :-

Array ( [0] => user )
Array ( [0] => user1 )
Array ( [0] => user2 )
Array ( [0] => user3 )
Array ( [0] => user8 )
Array ( [0] => user7 )
Array ( [0] => user6 )

Convert To :-

Array("user", "user1", "user2", "user3", "user4", "user5", "user6");

Body:
Another alternative, aside from array_merge, for the case of an arbitrary number of subarrays, you could use array_reduce and build your final array:

$inArray = [['user'],['user1'],['user2'],['user3'],['user8'],['user7'],['user6']];

$inArray = array_reduce($inArray, function($arr, $elem){
    $arr[] = $elem[0];
    return $arr;
});

Of course, the straightforward solution would be to use array_merge:

$inArray = array_merge(...$inArray);


问:
How to use pqxx::stateless_cursor class from libpqxx?
I'm learning libpqxx, the C++ API to PostgreSQL.  I'd like to use the pqxx::stateless_cursor class, but 1) I find the Doxygen output unhelpful in this case, and 2) the pqxx.org website has been down for some time now.

Anyone know how to use it?

I believe this is how I construct one:

```<code>pqxx::stateless_cursor <pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
    cursor( work, "SELECT * FROM mytable", ?, ? );
</code>```

The last two parms are called cname and hold, but are not documented.

And once the cursor is created, how would I go about using it in a for() loop to get each row, one at a time?

答:
Here's another cursor example, using a do-while() loop:</p>

```<code>     const std::conStr("user=" + opt::dbUser + " password=" + opt::dbPasswd + " host=" + opt::dbHost + " dbname=" + opt::dbName);

      pqxx::connection conn(connStr);
      pqxx::work txn(conn);
      std::string selectString = "SELECT id, name FROM table_name WHERE condition";

      pqxx::stateless_cursor<pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
      cursor(txn, selectString, "myCursor", false);

      //cursor variables
      size_t idx = 0;       //starting location
      size_t step = 10000;  //number of rows for each chunk
      pqxx::result result;
      do{
        //get next cursor chunk and update the index
        result = cursor.retrieve( idx, idx + step );
        idx += step;

        size_t records = result.size();
        cout << idx << ": records pulled = " << records << endl;

        for( pqxx::result::const_iterator row : result ){
          //iterate over cursor rows
        }
      }
      while( result.size() == step ); //if the result.size() != step, we're on our last loop
      cout << "Done!" << endl;
</code>```

Body:
I'm learning libpqxx, the C++ API to PostgreSQL.  I'd like to use the pqxx::stateless_cursor class, but 1) I find the Doxygen output unhelpful in this case, and 2) the pqxx.org website has been down for some time now.

Anyone know how to use it?

I believe this is how I construct one:

pqxx::stateless_cursor <pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
    cursor( work, "SELECT * FROM mytable", ?, ? );

The last two parms are called cname and hold, but are not documented.

And once the cursor is created, how would I go about using it in a for() loop to get each row, one at a time?

Body:
Here's another cursor example, using a do-while() loop:

     const std::conStr("user=" + opt::dbUser + " password=" + opt::dbPasswd + " host=" + opt::dbHost + " dbname=" + opt::dbName);

      pqxx::connection conn(connStr);
      pqxx::work txn(conn);
      std::string selectString = "SELECT id, name FROM table_name WHERE condition";

      pqxx::stateless_cursor<pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
      cursor(txn, selectString, "myCursor", false);

      //cursor variables
      size_t idx = 0;       //starting location
      size_t step = 10000;  //number of rows for each chunk
      pqxx::result result;
      do{
        //get next cursor chunk and update the index
        result = cursor.retrieve( idx, idx + step );
        idx += step;

        size_t records = result.size();
        cout << idx << ": records pulled = " << records << endl;

        for( pqxx::result::const_iterator row : result ){
          //iterate over cursor rows
        }
      }
      while( result.size() == step ); //if the result.size() != step, we're on our last loop
      cout << "Done!" << endl;


问:
2d array as index of a 3d array
I had a 2D array (C) with 8000x64 elements, an 1D array (s) with 8000x1 elements and another 1D array (d) with 1x64 elements.
Every row of index i, where s[i] is True, shall be added by vector d.
This works quite well:

```<code>C[s == True] += d
</code>```

Now I have added one dimension to C, s, and d and the logic above shall be applied to every element of the additional dimension.

The following code does what I want, but it's very slow.

```<code>for i in range(I):
        C_this = C[:,:,i]
        s_this = s[:,i]
        d_this = d[:,i]

        C_this[s_this == True] += d_this
        C[:,:,i] = C_this
</code>```

Is there a numpy way to do this without a for loop?

答:
Due to how numpy indexing works, s selects the relevant rows of C in the first example. To do the same thing in the 3D case, you would have to reshape C into something that is (8000*3, 64) and s into (8000*3, 1). The only problem now is getting d to account for the different number of rows in each third dimension, which can be done with np.repeat.

The first part is

```<code>C2 = np.swapaxes(C, -1, 1).reshape(-1, 64)
</code>```

This is extremely inefficient because it copies your entire array. A better arrangement would be if C had shape (3, 8000, 64) to begin with. Then you would only need to ravel the first two axes to get the proper shape and memory layout, without copying data.

```<code>repeats = np.count_nonzero(s, axis=0)
C.reshape(-1, 64)[s.ravel()] += np.repeat(d, repeats, axis=0)
</code>```

Since the reshape operation returns a view in this case, the indexing should work properly to increment in-place. I don't think this approach is necessarily very good though, since it copies each row of d as many times as s is non-zero in the corresponding element of the new dimension.

Title: 2d array as index of a 3d array

Body:
I had a 2D array (C) with 8000x64 elements, an 1D array (s) with 8000x1 elements and another 1D array (d) with 1x64 elements.
Every row of index i, where s[i] is True, shall be added by vector d.
This works quite well:

C[s == True] += d

Now I have added one dimension to C, s, and d and the logic above shall be applied to every element of the additional dimension.

The following code does what I want, but it's very slow.

for i in range(I):
        C_this = C[:,:,i]
        s_this = s[:,i]
        d_this = d[:,i]

        C_this[s_this == True] += d_this
        C[:,:,i] = C_this

Is there a numpy way to do this without a for loop?

Body:
Due to how numpy indexing works, s selects the relevant rows of C in the first example. To do the same thing in the 3D case, you would have to reshape C into something that is (8000*3, 64) and s into (8000*3, 1). The only problem now is getting d to account for the different number of rows in each third dimension, which can be done with np.repeat.

The first part is

C2 = np.swapaxes(C, -1, 1).reshape(-1, 64)

This is extremely inefficient because it copies your entire array. A better arrangement would be if C had shape (3, 8000, 64) to begin with. Then you would only need to ravel the first two axes to get the proper shape and memory layout, without copying data.

repeats = np.count_nonzero(s, axis=0)
C.reshape(-1, 64)[s.ravel()] += np.repeat(d, repeats, axis=0)

Since the reshape operation returns a view in this case, the indexing should work properly to increment in-place. I don't think this approach is necessarily very good though, since it copies each row of d as many times as s is non-zero in the corresponding element of the new dimension.


问:
How to take data from aspx page to stored procedure?
I have to create an RDLC report in asp.net. For which I am writing a stored procedure using different scalar functions.
I have a GUI from which I have to select a criteria and collect data according to that criteria. The GUI is here:

I have implemented all cases like "All Staff", "All services", etc but the problem comes when I have to select specific records from list box and bring data according to that. For example I select specfic Staff records from list and my report should display the records only having that selected records.
How can I handle this? I mean when I select some records from listbox, how can I take these records to the where clause of my stored procedure and how to use it there?

I am using SQL SERVER 2008.
My Stored Procedure is:

```<code>USE [PC_Rewrite]
</code>```

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spGetClients]
(
    @orderBy varchar(50)
)

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

```<code>-- Insert statements for procedure here
IF(@orderBy = 'Consumer Name')
BEGIN
    SELECT c.Id, c.LastName, c.FirstName, c.MiddleInit,
    c.DateOfBirth, dbo.GetAge(c.DateOfBirth, GETDATE()) AS Age, c.Sex, cs.Status, ca.Address, co.Phone,
    dbo.GetEthnicity(c.Id) AS Ethnicity, dbo.GetDevelopmentalDisabilities(c.Id) AS Disabilities,
    dbo.GetClientStaffContacts(c.Id) AS Staff, dbo.GetClientContacts(c.Id) AS Contact,
    dbo.GetClientInsuranceProviders(c.Id) AS InsuranceProvider FROM Client c
    LEFT OUTER JOIN ClientStatus cs ON cs.Id = c.StatusId
    LEFT OUTER JOIN(
        SELECT ca.ParentEntityId, ca.Address
        FROM ContactAddress ca
        INNER JOIN EntityName en ON en.Id = ca.EntityNameId AND en.Name = 'Client'
        INNER JOIN GeneralLookup gl ON ca.glAddressTypeId = gl.Id AND gl.LookupItem = 'Primary'
    ) ca ON c.Id = ca.ParentEntityId
    LEFT OUTER JOIN(
        SELECT co.ParentEntityId, co.ContactData Phone
        FROM ContactOther co
        INNER JOIN EntityName en ON en.Id = co.EntityNameId AND en.Name = 'Client'
        INNER JOIN GeneralLookup gl ON co.glContactTypeId = gl.Id AND gl.LookupItem = 'Home'
    ) co ON c.Id = co.ParentEntityId
    ORDER BY c.LastName, c.FirstName, c.MiddleInit
END

ELSE IF(@orderBy = 'Consumer Address')
BEGIN
    SELECT c.Id, c.LastName, c.FirstName, c.MiddleInit,
    c.DateOfBirth, dbo.GetAge(c.DateOfBirth, GETDATE()) AS Age, c.Sex, cs.Status, ca.Address, co.Phone,
    dbo.GetEthnicity(c.Id) AS Ethnicity, dbo.GetDevelopmentalDisabilities(c.Id) AS Disabilities,
    dbo.GetClientStaffContacts(c.Id) AS Staff, dbo.GetClientContacts(c.Id) AS Contact,
    dbo.GetClientInsuranceProviders(c.Id) AS InsuranceProvider FROM Client c
    LEFT OUTER JOIN ClientStatus cs ON cs.Id = c.StatusId
    LEFT OUTER JOIN(
        SELECT ca.ParentEntityId, ca.Address
        FROM ContactAddress ca
        INNER JOIN EntityName en ON en.Id = ca.EntityNameId AND en.Name = 'Client'
        INNER JOIN GeneralLookup gl ON ca.glAddressTypeId = gl.Id AND gl.LookupItem = 'Primary'
    ) ca on c.Id = ca.ParentEntityId
    LEFT OUTER JOIN(
        SELECT co.ParentEntityId, co.ContactData Phone
        FROM ContactOther co
        INNER JOIN EntityName en ON en.Id = co.EntityNameId AND en.Name = 'Client'
        INNER JOIN GeneralLookup gl ON co.glContactTypeId = gl.Id AND gl.LookupItem = 'Home'
    ) co ON c.Id = co.ParentEntityId
    ORDER BY ca.Address
END
</code>```

END

Any help would be appreciated.

答:
i think you want to pass your search criteria to your store procedure right?
use something like this..

//in code behind

CommandType.StoredProcedure;

//pass each parameter for your criteria like below
Command.Parameters.Add("@ID, SqlDbType.string).Value = staff;

Command.Parameters.Add("@ID, SqlDbType.string).Value = service;

//now in the store procedure
declare sql as varchar(200)
set sql = "select * from tablename"

//check parameters are blank or has a value
if(@paraname1 != "")
    set sql = sql + "where...parameter1";
end if

if(@paraname2 != "")
    set sql = sql + "where...parameter2";
end if

this is what you were looking for?

Body:
I have to create an RDLC report in asp.net. For which I am writing a stored procedure using different scalar functions.
I have a GUI from which I have to select a criteria and collect data according to that criteria. The GUI is here:

I have implemented all cases like "All Staff", "All services", etc but the problem comes when I have to select specific records from list box and bring data according to that. For example I select specfic Staff records from list and my report should display the records only having that selected records.
How can I handle this? I mean when I select some records from listbox, how can I take these records to the where clause of my stored procedure and how to use it there?

I am using SQL SERVER 2008.
My Stored Procedure is:

USE [PC_Rewrite]

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spGetClients]
(
    @orderBy varchar(50)
)

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

-- Insert statements for procedure here
IF(@orderBy = 'Consumer Name')
BEGIN
    SELECT c.Id, c.LastName, c.FirstName, c.MiddleInit,
    c.DateOfBirth, dbo.GetAge(c.DateOfBirth, GETDATE()) AS Age, c.Sex, cs.Status, ca.Address, co.Phone,
    dbo.GetEthnicity(c.Id) AS Ethnicity, dbo.GetDevelopmentalDisabilities(c.Id) AS Disabilities,
    dbo.GetClientStaffContacts(c.Id) AS Staff, dbo.GetClientContacts(c.Id) AS Contact,
    dbo.GetClientInsuranceProviders(c.Id) AS InsuranceProvider FROM Client c
    LEFT OUTER JOIN ClientStatus cs ON cs.Id = c.StatusId
    LEFT OUTER JOIN(
        SELECT ca.ParentEntityId, ca.Address
        FROM ContactAddress ca
        INNER JOIN EntityName en ON en.Id = ca.EntityNameId AND en.Name = 'Client'
        INNER JOIN GeneralLookup gl ON ca.glAddressTypeId = gl.Id AND gl.LookupItem = 'Primary'
    ) ca ON c.Id = ca.ParentEntityId
    LEFT OUTER JOIN(
        SELECT co.ParentEntityId, co.ContactData Phone
        FROM ContactOther co
        INNER JOIN EntityName en ON en.Id = co.EntityNameId AND en.Name = 'Client'
        INNER JOIN GeneralLookup gl ON co.glContactTypeId = gl.Id AND gl.LookupItem = 'Home'
    ) co ON c.Id = co.ParentEntityId
    ORDER BY c.LastName, c.FirstName, c.MiddleInit
END

ELSE IF(@orderBy = 'Consumer Address')
BEGIN
    SELECT c.Id, c.LastName, c.FirstName, c.MiddleInit,
    c.DateOfBirth, dbo.GetAge(c.DateOfBirth, GETDATE()) AS Age, c.Sex, cs.Status, ca.Address, co.Phone,
    dbo.GetEthnicity(c.Id) AS Ethnicity, dbo.GetDevelopmentalDisabilities(c.Id) AS Disabilities,
    dbo.GetClientStaffContacts(c.Id) AS Staff, dbo.GetClientContacts(c.Id) AS Contact,
    dbo.GetClientInsuranceProviders(c.Id) AS InsuranceProvider FROM Client c
    LEFT OUTER JOIN ClientStatus cs ON cs.Id = c.StatusId
    LEFT OUTER JOIN(
        SELECT ca.ParentEntityId, ca.Address
        FROM ContactAddress ca
        INNER JOIN EntityName en ON en.Id = ca.EntityNameId AND en.Name = 'Client'
        INNER JOIN GeneralLookup gl ON ca.glAddressTypeId = gl.Id AND gl.LookupItem = 'Primary'
    ) ca on c.Id = ca.ParentEntityId
    LEFT OUTER JOIN(
        SELECT co.ParentEntityId, co.ContactData Phone
        FROM ContactOther co
        INNER JOIN EntityName en ON en.Id = co.EntityNameId AND en.Name = 'Client'
        INNER JOIN GeneralLookup gl ON co.glContactTypeId = gl.Id AND gl.LookupItem = 'Home'
    ) co ON c.Id = co.ParentEntityId
    ORDER BY ca.Address
END

END

Any help would be appreciated.


问:
What's the difference between Web Workers and Worker Threads?
I haven't been able to find any resources that explain the difference between Web Workers (https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers), which have been around for years and enable PWA's, and Worker Threads (https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads), which have recently been released in Node.js.

To my understanding both are simply ways of allowing JavaScript to run code in multiple threads. So why are Worker Threads being released as a "new" thing?

答:
Web Workers are a technology that exists in browsers.

Worker Threads are a technology that exists in node.js.

They have similar goals, but due to environment differences, they have different implementations.

To my understanding both are simply ways of allowing JavaScript to run code in multiple threads.

Yes, that is correct.  With significant limitations such as no access to the same variables as the main thread and in the browser, no access to the DOM.  Communication between threads and the main thread is generally done via messaging.

So why are Worker Threads being released as a "new" thing?

Node.js has not had the ability to run Javascript code in threads until the "new" Worker Threads.  node.js never had Web Workers.  Web Workers have existed in the browser for awhile.  Before Worker Threads, developers had to use multiple processes to involve additional CPUs or to keep CPU-intensive code from blocking the event loop.  Now, node.js developers can do this with Worker Threads.


问:
Spring Boot OpenFeign: java.lang.IllegalStateException: Method has too many Body parameters
The examples below demonstrate the issues experienced when using OpenFeign. The problem becomes evident when your response object has too many fields, which throws an error: Method has too many parameters. Example 1 works perfectly, but Example 2 fails.

Example 1 uses HTTP.POST with the same response object as used in Example 2, which uses HTTP.GET.

Why does OpenFeign limit the fields in an HTTP.GET method, and throw an exception? I can't use an HTTP.POST to get/fetch/read a resource. BAD REST API Design standards.

Using the same response object, for both HTTP.POST (which works), HTTP.GET fails

```<code>public interface ClientFeignV2 {

//Example 1
@Headers("Content-Type: application/json") @RequestLine("POST api/v2/clients") ClientResponse findAllClientsByUid1(@RequestBody ClientRequest request);

//Example 2
@Headers("Content-Type: application/json")
@RequestLine("GET api/v2/clients/{uid}")
ClientResponse findAllClientsByUid(@PathVariable(value = "uid") String uid,
                                         @RequestParam(value = "limit", required = false) Integer limit,
                                         @RequestParam(value = "offset", required = false) Integer offset);
}
</code>```

StackTrace:

```<code>Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.services.requestresponse.ClientResponse com.microservice.gateway.feign.v2.ClientFeignV2.findAllClientsByUid(java.lang.String,java.lang.Integer,java.lang.Integer)
at feign.Util.checkState(Util.java:128) ~[feign-core-9.4.0.jar:na]
at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:114) ~[feign-core-9.4.0.jar:na]
at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:64) ~[feign-core-9.4.0.jar:na]
at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146) ~[feign-core-9.4.0.jar:na]
at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53) ~[feign-core-9.4.0.jar:na]
at feign.Feign$Builder.target(Feign.java:209) ~[feign-core-9.4.0.jar:na]
at feign.Feign$Builder.target(Feign.java:205) ~[feign-core-9.4.0.jar:na]
at com.microservice.gateway.service.v2.impl.ClientServiceV2Impl.<init>(ClientServiceV2Impl.java:27) ~[classes/:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_222]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_222]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_222]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_222]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:203) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
... 40 common frames omitted
</code>```

I went through the OpenFeign documentation, and it supports the above implementation. If I can't find a solution to this issue, I would have to resort to a workaround and use HTTP.POST and @RequestBody which is not an ideal solution according to Rest-API design standards.

答:
According to the tags, you're using Spring Boot, obviously with Spring Cloud OpenFeign. And the thing is that you're mixing two different Feign contracts.

Annotations like @RequestLine and @Headers are from the core feign library.
You can use it as a declarative HTTP client, and not only in Spring applications (it won't need to use Spring annotations in that case).

So, the correct "example 2" with plain Feign could be like:

```<code>@Headers("Content-Type: application/json")
@RequestLine("GET api/v2/clients/{uid}?limit={limit}&offset={offset}")
ClientResponse findAllClientsByUid(@Param("uid") String uid,
                                   @Param("limit") Integer limit,
                                   @Param("offset") Integer offset);
</code>```

On the other hand, things like @RequestParam and @PathVariable are from Spring Web.
They can be used if you have Spring Cloud OpenFeign library (the core feign is one of its components btw).
This library brings support for SpringMvcContract, which, in its case, allows you to use usual Spring Web annotations for defining request mapping instead of Feign-specific ones.

In case of SpringMvcContract, "example 2" could be like:

```<code>@GetMapping(value = "api/v2/clients/{uid}", consumes = MediaType.APPLICATION_JSON_VALUE)
ClientResponse findAllClientsByUid(@PathVariable(value = "uid") String uid,
                @RequestParam(value = "limit", required = false) Integer limit,
                @RequestParam(value = "offset", required = false) Integer offset);
</code>```

It's worth to mention that in Spring Cloud OpenFeign the second approach is used by default. To change it back to plain Feign contract, define the custom Contract bean (source):

```<code>@Bean
public Contract feignContract() {
    return new feign.Contract.Default();
}
</code>```

Body:
The examples below demonstrate the issues experienced when using OpenFeign. The problem becomes evident when your response object has too many fields, which throws an error: Method has too many parameters. Example 1 works perfectly, but Example 2 fails.

Example 1 uses HTTP.POST with the same response object as used in Example 2, which uses HTTP.GET.

Why does OpenFeign limit the fields in an HTTP.GET method, and throw an exception? I can't use an HTTP.POST to get/fetch/read a resource. BAD REST API Design standards.

Using the same response object, for both HTTP.POST (which works), HTTP.GET fails

public interface ClientFeignV2 {

//Example 1
@Headers("Content-Type: application/json") @RequestLine("POST api/v2/clients") ClientResponse findAllClientsByUid1(@RequestBody ClientRequest request);

//Example 2
@Headers("Content-Type: application/json")
@RequestLine("GET api/v2/clients/{uid}")
ClientResponse findAllClientsByUid(@PathVariable(value = "uid") String uid,
                                         @RequestParam(value = "limit", required = false) Integer limit,
                                         @RequestParam(value = "offset", required = false) Integer offset);
}

StackTrace:

Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.services.requestresponse.ClientResponse com.microservice.gateway.feign.v2.ClientFeignV2.findAllClientsByUid(java.lang.String,java.lang.Integer,java.lang.Integer)
at feign.Util.checkState(Util.java:128) ~[feign-core-9.4.0.jar:na]
at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:114) ~[feign-core-9.4.0.jar:na]
at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:64) ~[feign-core-9.4.0.jar:na]
at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146) ~[feign-core-9.4.0.jar:na]
at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53) ~[feign-core-9.4.0.jar:na]
at feign.Feign$Builder.target(Feign.java:209) ~[feign-core-9.4.0.jar:na]
at feign.Feign$Builder.target(Feign.java:205) ~[feign-core-9.4.0.jar:na]
at com.microservice.gateway.service.v2.impl.ClientServiceV2Impl.<init>(ClientServiceV2Impl.java:27) ~[classes/:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_222]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_222]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_222]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_222]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:203) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
... 40 common frames omitted

I went through the OpenFeign documentation, and it supports the above implementation. If I can't find a solution to this issue, I would have to resort to a workaround and use HTTP.POST and @RequestBody which is not an ideal solution according to Rest-API design standards.

Body:
According to the tags, you're using Spring Boot, obviously with Spring Cloud OpenFeign. And the thing is that you're mixing two different Feign contracts.

Annotations like @RequestLine and @Headers are from the core feign library.
You can use it as a declarative HTTP client, and not only in Spring applications (it won't need to use Spring annotations in that case).

So, the correct "example 2" with plain Feign could be like:

@Headers("Content-Type: application/json")
@RequestLine("GET api/v2/clients/{uid}?limit={limit}&offset={offset}")
ClientResponse findAllClientsByUid(@Param("uid") String uid,
                                   @Param("limit") Integer limit,
                                   @Param("offset") Integer offset);

On the other hand, things like @RequestParam and @PathVariable are from Spring Web.
They can be used if you have Spring Cloud OpenFeign library (the core feign is one of its components btw).
This library brings support for SpringMvcContract, which, in its case, allows you to use usual Spring Web annotations for defining request mapping instead of Feign-specific ones.

In case of SpringMvcContract, "example 2" could be like:

@GetMapping(value = "api/v2/clients/{uid}", consumes = MediaType.APPLICATION_JSON_VALUE)
ClientResponse findAllClientsByUid(@PathVariable(value = "uid") String uid,
                @RequestParam(value = "limit", required = false) Integer limit,
                @RequestParam(value = "offset", required = false) Integer offset);

It's worth to mention that in Spring Cloud OpenFeign the second approach is used by default. To change it back to plain Feign contract, define the custom Contract bean (source):

@Bean
public Contract feignContract() {
    return new feign.Contract.Default();
}


问:
What's the jquery syntax for click function of submit button
my form has a submit button that's been defined this way:

```<code><input type="submit" name="submit-reg" value="Register" class="submit button">
</code>```

What is the syntax to call the button's jquery click() function?
I've tried:

```<code>$('#submit-reg').click(function()...
</code>```

But it's not working.

答:
use

```<code>$(".submit").click(function(){
     //click event
 });
</code>```

You don't need to change your html if you use above function.

Body:
my form has a submit button that's been defined this way:

<input type="submit" name="submit-reg" value="Register" class="submit button">

What is the syntax to call the button's jquery click() function?
I've tried:

$('#submit-reg').click(function()...

But it's not working.

Body:
use

$(".submit").click(function(){
     //click event
 });

You don't need to change your html if you use above function.