问:
Pandas / Python - Very slow performance using stack() groupby() and apply()
I am trying to create a new column in a dataframe based on pairs of information and its previous values. Although the code that I run is correct, and gives the results I need, it is very slow when I run it on a large dataframe. So I susspect I am not using all of the Python power for this task. Is there a more efficient and faster way of doing this in Python?.

To put you in context, let me explain to you a little about what I am looking for:

I have a dataframe, which describes competitions results, where for each 'date' you can see the 'type' who competed and its score called 'xx'.

What my code does is to obtain the difference of score 'xx' between 'type' for each 'date' and then get the sum of difference of the results of the previous competitions that all the types competing with each other had in the past ('win_comp_past_difs').

Below you can see the data and the model with its output.

## I. DATA AND MODEL ##
```

<p><em>I.1. Data</em></p>

<pre class="lang-py prettyprint-override"><code>import pandas as pd
import numpy as np

idx = [np.array(['Jan-18', 'Jan-18', 'Feb-18', 'Mar-18', 'Mar-18', 'Mar-18','Mar-18', 'Mar-18', 'May-18', 'Jun-18', 'Jun-18', 'Jun-18','Jul-18', 'Aug-18', 'Aug-18', 'Sep-18', 'Sep-18', 'Oct-18','Oct-18', 'Oct-18', 'Nov-18', 'Dec-18', 'Dec-18',]),np.array(['A', 'B', 'B', 'A', 'B', 'C', 'D', 'E', 'B', 'A', 'B', 'C','A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'A', 'B', 'C'])]
data = [{'xx': 1}, {'xx': 5}, {'xx': 3}, {'xx': 2}, {'xx': 7}, {'xx': 3},{'xx': 1}, {'xx': 6}, {'xx': 3}, {'xx': 5}, {'xx': 2}, {'xx': 3},{'xx': 1}, {'xx': 9}, {'xx': 3}, {'xx': 2}, {'xx': 7}, {'xx': 3}, {'xx': 6}, {'xx': 8}, {'xx': 2}, {'xx': 7}, {'xx': 9}]
df = pd.DataFrame(data, index=idx, columns=['xx'])
df.index.names=['date','type']
df=df.reset_index()
df['date'] = pd.to_datetime(df['date'],format = '%b-%y')
df=df.set_index(['date','type'])
df['xx'] = df.xx.astype('float')
</code>```

Which looks like this:

```<code>                  xx
date       type
2018-01-01 A     1.0
           B     5.0
2018-02-01 B     3.0
2018-03-01 A     2.0
           B     7.0
           C     3.0
           D     1.0
           E     6.0
2018-05-01 B     3.0
2018-06-01 A     5.0
           B     2.0
           C     3.0
2018-07-01 A     1.0
2018-08-01 B     9.0
           C     3.0
2018-09-01 A     2.0
           B     7.0
2018-10-01 C     3.0
           A     6.0
           B     8.0
2018-11-01 A     2.0
2018-12-01 B     7.0
           C     9.0
</code>```

I.2. Model (very slow in a large dataframe)

# get differences of pairs, useful for win counts and win_difs
def get_diff(x):
    teams = x.index.get_level_values(1)
    tmp = pd.DataFrame(x[:,None]-x[None,:],columns = teams.values,index=teams.values).stack()
    return tmp[tmp.index.get_level_values(0)!=tmp.index.get_level_values(1)]
new_df = df.groupby('date').xx.apply(get_diff).to_frame()

# group by players
groups = new_df.groupby(level=[1,2])

# sum function
def cumsum_shift(x):
    return x.cumsum().shift()

# assign new values
df['win_comp_past_difs'] = groups.xx.apply(cumsum_shift).sum(level=[0,1])
```

<p>Below you can see how the output of the model looks like:</p>

<pre class="lang-py prettyprint-override"><code>                  xx  win_comp_past_difs
date       type
2018-01-01 A     1.0                 0.0
           B     5.0                 0.0
2018-02-01 B     3.0                 NaN
2018-03-01 A     2.0                -4.0
           B     7.0                 4.0
           C     3.0                 0.0
           D     1.0                 0.0
           E     6.0                 0.0
2018-05-01 B     3.0                 NaN
2018-06-01 A     5.0               -10.0
           B     2.0                13.0
           C     3.0                -3.0
2018-07-01 A     1.0                 NaN
2018-08-01 B     9.0                 3.0
           C     3.0                -3.0
2018-09-01 A     2.0                -6.0
           B     7.0                 6.0
2018-10-01 C     3.0               -10.0
           A     6.0               -10.0
           B     8.0                20.0
2018-11-01 A     2.0                 NaN
2018-12-01 B     7.0                14.0
           C     9.0               -14.0

</code>```

Just in case it is difficult for you to understand what does the User-Defined function (def) do, let me explain it to you below.

For this porpouse I will work with one group of the groupby of the dataframe.

Below you will see an explanation of how the User-Defines function  work.

## II. EXPLANATION OF THE USER-DEFINED FUNCTION ##
```

<p>So, for you to see how the User-defined function work let me select an specific group of the groupby.</p>

<p><em>II.1 Choosing an specific group</em></p>

<pre class="lang-py prettyprint-override"><code>gb = df.groupby('date')
gb2 = gb.get_group((list(gb.groups)[2]))
</code>```

Which looks like this:

```<code>                    xx
  date       type
  2018-03-01 A     2.0
             B     7.0
             C     3.0
             D     1.0
             E     6.0
</code>```

II.2 Creating a list of competitors (teams)'

teams = gb2.index.get_level_values(1)
```

<p><em>II.3 Creating a dataframe of the difference of 'xx' between 'type'</em></p>

<pre class="lang-py prettyprint-override"><code>df_comp= pd.DataFrame(gb2.xx[:,None]-gb2.xx[None,:],columns = teams.values,index=teams.values)
</code>```

Which looks like this:

```<code>    A    B    C    D    E
  A  0.0 -5.0 -1.0  1.0 -4.0
  B  5.0  0.0  4.0  6.0  1.0
  C  1.0 -4.0  0.0  2.0 -3.0
  D -1.0 -6.0 -2.0  0.0 -5.0
  E  4.0 -1.0  3.0  5.0  0.0
</code>```

From this point I use the stack() function as an intermediate step to go back to the original dataframe. The rest you can follow it in the I. DATA AND MODEL.

If you could elaborate on the code to make it more efficient and execute faster, I would really appreciate it.

答:
I formulate the problem as I understand it and would like to suggest a slightly different approach that uses the built-ins.  Two variations where the second one uses half the memory and runs in about half the time:

```<code>timeit -r10 event_score6(games, scores)
21.3 µs ± 165 ns per loop (mean ± std. dev. of 10 runs, 10000 loops each)

timeit -r10 event_score(events, games, scores)
42.8 µs ± 210 ns per loop (mean ± std. dev. of 10 runs, 10000 loops each)
</code>```

```<code>#
# Assume game data comes from a csv-file that contains reasonably clean data.
#
# We have a list of games each with a list of participating teams and the
# scores for each team in the game.
#
# For each of the pairs in the current game first calculate the sum of the
# differences in score from the previous competitions (win_comp_past_difs);
# include only the pairs in the current game.  Second update each pair in the
# current game with the difference in scores.
#
# Using a defaultdict keep track of the scores for each pair in each game and
# update this score as each game is played.
#
import csv
from collections import defaultdict
from itertools import groupby
from itertools import permutations
from itertools import combinations
from math import nan as NaN

def read_data(data_file):
    """Read and group games and scores by event date

    Sort the participants in each game. Returns header, events, games,
    scores.
    """
    header = ""
    events = []
    games = []
    scores = []
    with open(data_file, newline='') as fd:
        sample = fd.read(1024)
        dialect = csv.Sniffer().sniff(sample)
        fd.seek(0)
        reader = csv.reader(fd, dialect)
        if csv.Sniffer().has_header(sample):
            header = next(reader)
        for event_date, row in groupby(reader, key=lambda r: r[0]):
            _, gg, ss = tuple(zip(*row))
            events.append(event_date.strip())
            gms = (tuple(g.strip() for g in gg))
            scr = (tuple(float(s) for s in ss))
            g, s = zip(*sorted(zip(gms, scr)))
            games.append(g)
            scores.append(s)
    return header, events, games, scores

def event_score(events, games, scores, wd=defaultdict(float)):
    """Score each event and calculare win_comp_past_difs iteratively

    Return the acuumulated state from all events and the
    win_comp_past_difs grouped by event.
    """
    wins = []
    for evnt, game, xx in zip(events, games, scores):
        evnt_wins = []
        if len(game) == 1:
            win_comp_past_difs = NaN
            evnt_wins.append(win_comp_past_difs)
            wins.append(evnt_wins)
            continue

        # Pairs and difference generator for current game.
        pairs = list(permutations(game, 2))
        dgen = (value[0] - value[1] for value in permutations(xx, 2))

        # Sum of differences from previous games including only pair of teams
        # in the current game.
        for team, result in zip(game, xx):
            win_comp_past_difs = sum(wd[key]
                                     for key in pairs if key[0] == team)
            evnt_wins.append(win_comp_past_difs)
        wins.append(evnt_wins)

        # Update pair differeces for current game.
        for pair, diff in zip(pairs, dgen):
            wd[pair] += diff
    return wd, wins

def event_score6(games, scores, wd=defaultdict(float)):
    """Score each game and calculare win_comp_past_difs iteratively

    Assume sorted order in each game. Return the acuumulated state from
    all events and the win_comp_past_difs grouped by event.
    """
    wins = []
    for game, xx in zip(games, scores):
        if len(game) == 1:
            wins.append((NaN,))
            continue

        # Pairs for current game.
        pairs = tuple(combinations(game, 2))

        # Sum of differences from previous games including
        # only pair of teams in the current game.
        win_comp_past_difs = defaultdict(float)
        for pair in pairs:
            tmp = wd[pair]
            win_comp_past_difs[pair[0]] += tmp
            win_comp_past_difs[pair[1]] -= tmp
        wins.append(tuple(win_comp_past_difs.values()))

        # Update pair differeces for current game.
        for pair, value in zip(pairs, combinations(xx, 2)):
            wd[pair] += value[0] - value[1]
    return wd, wins

h, events, games, scores = read_data('data2.csv')

wd, wins = event_score(events, games, scores)
wd6, wins6 = event_score6(games, scores)

print(h)
print("Elements ", len(wd))
for evnt, gm, sc, wns in zip(events, games, scores, wins):
    for team, result, win_comp_past_difs in zip(gm, sc, wns):
        print(f"{evnt} {team}: {result}\t{win_comp_past_difs: 5.1f}")

print(h)
print("Elements ", len(wd6))
for evnt, gm, sc, wns in zip(events, games, scores, wins6):
    for team, result, win_comp_past_difs in zip(gm, sc, wns):
        print(f"{evnt} {team}: {result}\t{win_comp_past_difs: 5.1f}")

</code>```

A run of the code gives:

```<code>['Event', 'Team', 'Score']
Elements  20
Jan-18 A: 1.0     0.0
Jan-18 B: 5.0     0.0
Feb-18 B: 3.0     nan
Mar-18 A: 2.0    -4.0
Mar-18 B: 7.0     4.0
Mar-18 C: 3.0     0.0
Mar-18 D: 1.0     0.0
Mar-18 E: 6.0     0.0
May-18 B: 3.0     nan
Jun-18 A: 5.0   -10.0
Jun-18 B: 2.0    13.0
Jun-18 C: 3.0    -3.0
Jul-18 A: 1.0     nan
Aug-18 B: 9.0     3.0
Aug-18 C: 3.0    -3.0
Sep-18 A: 2.0    -6.0
Sep-18 B: 7.0     6.0
Oct-18 A: 6.0   -10.0
Oct-18 B: 8.0    20.0
Oct-18 C: 3.0   -10.0
Nov-18 A: 2.0     nan
Dec-18 B: 7.0    14.0
Dec-18 C: 9.0   -14.0
['Event', 'Team', 'Score']
Elements  10
Jan-18 A: 1.0     0.0
Jan-18 B: 5.0     0.0
Feb-18 B: 3.0     nan
Mar-18 A: 2.0    -4.0
Mar-18 B: 7.0     4.0
Mar-18 C: 3.0     0.0
Mar-18 D: 1.0     0.0
Mar-18 E: 6.0     0.0
May-18 B: 3.0     nan
Jun-18 A: 5.0   -10.0
Jun-18 B: 2.0    13.0
Jun-18 C: 3.0    -3.0
Jul-18 A: 1.0     nan
Aug-18 B: 9.0     3.0
Aug-18 C: 3.0    -3.0
Sep-18 A: 2.0    -6.0
Sep-18 B: 7.0     6.0
Oct-18 A: 6.0   -10.0
Oct-18 B: 8.0    20.0
Oct-18 C: 3.0   -10.0
Nov-18 A: 2.0     nan
Dec-18 B: 7.0    14.0
Dec-18 C: 9.0   -14.0
</code>```

Using the file data2.csv

```<code>Event, Team, Score
Jan-18, A, 1
Jan-18, B, 5
Feb-18, B, 3
Mar-18, A, 2
Mar-18, B, 7
Mar-18, C, 3
Mar-18, D, 1
Mar-18, E, 6
May-18, B, 3
Jun-18, A, 5
Jun-18, B, 2
Jun-18, C, 3
Jul-18, A, 1
Aug-18, B, 9
Aug-18, C, 3
Sep-18, A, 2
Sep-18, B, 7
Oct-18, C, 3
Oct-18, A, 6
Oct-18, B, 8
Nov-18, A, 2
Dec-18, B, 7
Dec-18, C, 9
</code>```

Body:
I am trying to create a new column in a dataframe based on pairs of information and its previous values. Although the code that I run is correct, and gives the results I need, it is very slow when I run it on a large dataframe. So I susspect I am not using all of the Python power for this task. Is there a more efficient and faster way of doing this in Python?.

To put you in context, let me explain to you a little about what I am looking for:

I have a dataframe, which describes competitions results, where for each 'date' you can see the 'type' who competed and its score called 'xx'.

What my code does is to obtain the difference of score 'xx' between 'type' for each 'date' and then get the sum of difference of the results of the previous competitions that all the types competing with each other had in the past ('win_comp_past_difs').

Below you can see the data and the model with its output.

## I. DATA AND MODEL ##

I.1. Data

import pandas as pd
import numpy as np

idx = [np.array(['Jan-18', 'Jan-18', 'Feb-18', 'Mar-18', 'Mar-18', 'Mar-18','Mar-18', 'Mar-18', 'May-18', 'Jun-18', 'Jun-18', 'Jun-18','Jul-18', 'Aug-18', 'Aug-18', 'Sep-18', 'Sep-18', 'Oct-18','Oct-18', 'Oct-18', 'Nov-18', 'Dec-18', 'Dec-18',]),np.array(['A', 'B', 'B', 'A', 'B', 'C', 'D', 'E', 'B', 'A', 'B', 'C','A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'A', 'B', 'C'])]
data = [{'xx': 1}, {'xx': 5}, {'xx': 3}, {'xx': 2}, {'xx': 7}, {'xx': 3},{'xx': 1}, {'xx': 6}, {'xx': 3}, {'xx': 5}, {'xx': 2}, {'xx': 3},{'xx': 1}, {'xx': 9}, {'xx': 3}, {'xx': 2}, {'xx': 7}, {'xx': 3}, {'xx': 6}, {'xx': 8}, {'xx': 2}, {'xx': 7}, {'xx': 9}]
df = pd.DataFrame(data, index=idx, columns=['xx'])
df.index.names=['date','type']
df=df.reset_index()
df['date'] = pd.to_datetime(df['date'],format = '%b-%y')
df=df.set_index(['date','type'])
df['xx'] = df.xx.astype('float')

Which looks like this:

                  xx
date       type
2018-01-01 A     1.0
           B     5.0
2018-02-01 B     3.0
2018-03-01 A     2.0
           B     7.0
           C     3.0
           D     1.0
           E     6.0
2018-05-01 B     3.0
2018-06-01 A     5.0
           B     2.0
           C     3.0
2018-07-01 A     1.0
2018-08-01 B     9.0
           C     3.0
2018-09-01 A     2.0
           B     7.0
2018-10-01 C     3.0
           A     6.0
           B     8.0
2018-11-01 A     2.0
2018-12-01 B     7.0
           C     9.0

I.2. Model (very slow in a large dataframe)

# get differences of pairs, useful for win counts and win_difs
def get_diff(x):
    teams = x.index.get_level_values(1)
    tmp = pd.DataFrame(x[:,None]-x[None,:],columns = teams.values,index=teams.values).stack()
    return tmp[tmp.index.get_level_values(0)!=tmp.index.get_level_values(1)]
new_df = df.groupby('date').xx.apply(get_diff).to_frame()

# group by players
groups = new_df.groupby(level=[1,2])

# sum function
def cumsum_shift(x):
    return x.cumsum().shift()

# assign new values
df['win_comp_past_difs'] = groups.xx.apply(cumsum_shift).sum(level=[0,1])

Below you can see how the output of the model looks like:

                  xx  win_comp_past_difs
date       type
2018-01-01 A     1.0                 0.0
           B     5.0                 0.0
2018-02-01 B     3.0                 NaN
2018-03-01 A     2.0                -4.0
           B     7.0                 4.0
           C     3.0                 0.0
           D     1.0                 0.0
           E     6.0                 0.0
2018-05-01 B     3.0                 NaN
2018-06-01 A     5.0               -10.0
           B     2.0                13.0
           C     3.0                -3.0
2018-07-01 A     1.0                 NaN
2018-08-01 B     9.0                 3.0
           C     3.0                -3.0
2018-09-01 A     2.0                -6.0
           B     7.0                 6.0
2018-10-01 C     3.0               -10.0
           A     6.0               -10.0
           B     8.0                20.0
2018-11-01 A     2.0                 NaN
2018-12-01 B     7.0                14.0
           C     9.0               -14.0

Just in case it is difficult for you to understand what does the User-Defined function (def) do, let me explain it to you below.

For this porpouse I will work with one group of the groupby of the dataframe.

Below you will see an explanation of how the User-Defines function  work.

## II. EXPLANATION OF THE USER-DEFINED FUNCTION ##

So, for you to see how the User-defined function work let me select an specific group of the groupby.

II.1 Choosing an specific group

gb = df.groupby('date')
gb2 = gb.get_group((list(gb.groups)[2]))

Which looks like this:

                    xx
  date       type
  2018-03-01 A     2.0
             B     7.0
             C     3.0
             D     1.0
             E     6.0

II.2 Creating a list of competitors (teams)'

teams = gb2.index.get_level_values(1)

II.3 Creating a dataframe of the difference of 'xx' between 'type'

df_comp= pd.DataFrame(gb2.xx[:,None]-gb2.xx[None,:],columns = teams.values,index=teams.values)

Which looks like this:

    A    B    C    D    E
  A  0.0 -5.0 -1.0  1.0 -4.0
  B  5.0  0.0  4.0  6.0  1.0
  C  1.0 -4.0  0.0  2.0 -3.0
  D -1.0 -6.0 -2.0  0.0 -5.0
  E  4.0 -1.0  3.0  5.0  0.0

From this point I use the stack() function as an intermediate step to go back to the original dataframe. The rest you can follow it in the I. DATA AND MODEL.

If you could elaborate on the code to make it more efficient and execute faster, I would really appreciate it.

Body:
I formulate the problem as I understand it and would like to suggest a slightly different approach that uses the built-ins.  Two variations where the second one uses half the memory and runs in about half the time:

timeit -r10 event_score6(games, scores)
21.3 µs ± 165 ns per loop (mean ± std. dev. of 10 runs, 10000 loops each)

timeit -r10 event_score(events, games, scores)
42.8 µs ± 210 ns per loop (mean ± std. dev. of 10 runs, 10000 loops each)

#
# Assume game data comes from a csv-file that contains reasonably clean data.
#
# We have a list of games each with a list of participating teams and the
# scores for each team in the game.
#
# For each of the pairs in the current game first calculate the sum of the
# differences in score from the previous competitions (win_comp_past_difs);
# include only the pairs in the current game.  Second update each pair in the
# current game with the difference in scores.
#
# Using a defaultdict keep track of the scores for each pair in each game and
# update this score as each game is played.
#
import csv
from collections import defaultdict
from itertools import groupby
from itertools import permutations
from itertools import combinations
from math import nan as NaN

def read_data(data_file):
    """Read and group games and scores by event date

    Sort the participants in each game. Returns header, events, games,
    scores.
    """
    header = ""
    events = []
    games = []
    scores = []
    with open(data_file, newline='') as fd:
        sample = fd.read(1024)
        dialect = csv.Sniffer().sniff(sample)
        fd.seek(0)
        reader = csv.reader(fd, dialect)
        if csv.Sniffer().has_header(sample):
            header = next(reader)
        for event_date, row in groupby(reader, key=lambda r: r[0]):
            _, gg, ss = tuple(zip(*row))
            events.append(event_date.strip())
            gms = (tuple(g.strip() for g in gg))
            scr = (tuple(float(s) for s in ss))
            g, s = zip(*sorted(zip(gms, scr)))
            games.append(g)
            scores.append(s)
    return header, events, games, scores

def event_score(events, games, scores, wd=defaultdict(float)):
    """Score each event and calculare win_comp_past_difs iteratively

    Return the acuumulated state from all events and the
    win_comp_past_difs grouped by event.
    """
    wins = []
    for evnt, game, xx in zip(events, games, scores):
        evnt_wins = []
        if len(game) == 1:
            win_comp_past_difs = NaN
            evnt_wins.append(win_comp_past_difs)
            wins.append(evnt_wins)
            continue

        # Pairs and difference generator for current game.
        pairs = list(permutations(game, 2))
        dgen = (value[0] - value[1] for value in permutations(xx, 2))

        # Sum of differences from previous games including only pair of teams
        # in the current game.
        for team, result in zip(game, xx):
            win_comp_past_difs = sum(wd[key]
                                     for key in pairs if key[0] == team)
            evnt_wins.append(win_comp_past_difs)
        wins.append(evnt_wins)

        # Update pair differeces for current game.
        for pair, diff in zip(pairs, dgen):
            wd[pair] += diff
    return wd, wins

def event_score6(games, scores, wd=defaultdict(float)):
    """Score each game and calculare win_comp_past_difs iteratively

    Assume sorted order in each game. Return the acuumulated state from
    all events and the win_comp_past_difs grouped by event.
    """
    wins = []
    for game, xx in zip(games, scores):
        if len(game) == 1:
            wins.append((NaN,))
            continue

        # Pairs for current game.
        pairs = tuple(combinations(game, 2))

        # Sum of differences from previous games including
        # only pair of teams in the current game.
        win_comp_past_difs = defaultdict(float)
        for pair in pairs:
            tmp = wd[pair]
            win_comp_past_difs[pair[0]] += tmp
            win_comp_past_difs[pair[1]] -= tmp
        wins.append(tuple(win_comp_past_difs.values()))

        # Update pair differeces for current game.
        for pair, value in zip(pairs, combinations(xx, 2)):
            wd[pair] += value[0] - value[1]
    return wd, wins

h, events, games, scores = read_data('data2.csv')

wd, wins = event_score(events, games, scores)
wd6, wins6 = event_score6(games, scores)

print(h)
print("Elements ", len(wd))
for evnt, gm, sc, wns in zip(events, games, scores, wins):
    for team, result, win_comp_past_difs in zip(gm, sc, wns):
        print(f"{evnt} {team}: {result}\t{win_comp_past_difs: 5.1f}")

print(h)
print("Elements ", len(wd6))
for evnt, gm, sc, wns in zip(events, games, scores, wins6):
    for team, result, win_comp_past_difs in zip(gm, sc, wns):
        print(f"{evnt} {team}: {result}\t{win_comp_past_difs: 5.1f}")

A run of the code gives:

['Event', 'Team', 'Score']
Elements  20
Jan-18 A: 1.0     0.0
Jan-18 B: 5.0     0.0
Feb-18 B: 3.0     nan
Mar-18 A: 2.0    -4.0
Mar-18 B: 7.0     4.0
Mar-18 C: 3.0     0.0
Mar-18 D: 1.0     0.0
Mar-18 E: 6.0     0.0
May-18 B: 3.0     nan
Jun-18 A: 5.0   -10.0
Jun-18 B: 2.0    13.0
Jun-18 C: 3.0    -3.0
Jul-18 A: 1.0     nan
Aug-18 B: 9.0     3.0
Aug-18 C: 3.0    -3.0
Sep-18 A: 2.0    -6.0
Sep-18 B: 7.0     6.0
Oct-18 A: 6.0   -10.0
Oct-18 B: 8.0    20.0
Oct-18 C: 3.0   -10.0
Nov-18 A: 2.0     nan
Dec-18 B: 7.0    14.0
Dec-18 C: 9.0   -14.0
['Event', 'Team', 'Score']
Elements  10
Jan-18 A: 1.0     0.0
Jan-18 B: 5.0     0.0
Feb-18 B: 3.0     nan
Mar-18 A: 2.0    -4.0
Mar-18 B: 7.0     4.0
Mar-18 C: 3.0     0.0
Mar-18 D: 1.0     0.0
Mar-18 E: 6.0     0.0
May-18 B: 3.0     nan
Jun-18 A: 5.0   -10.0
Jun-18 B: 2.0    13.0
Jun-18 C: 3.0    -3.0
Jul-18 A: 1.0     nan
Aug-18 B: 9.0     3.0
Aug-18 C: 3.0    -3.0
Sep-18 A: 2.0    -6.0
Sep-18 B: 7.0     6.0
Oct-18 A: 6.0   -10.0
Oct-18 B: 8.0    20.0
Oct-18 C: 3.0   -10.0
Nov-18 A: 2.0     nan
Dec-18 B: 7.0    14.0
Dec-18 C: 9.0   -14.0

Using the file data2.csv

Event, Team, Score
Jan-18, A, 1
Jan-18, B, 5
Feb-18, B, 3
Mar-18, A, 2
Mar-18, B, 7
Mar-18, C, 3
Mar-18, D, 1
Mar-18, E, 6
May-18, B, 3
Jun-18, A, 5
Jun-18, B, 2
Jun-18, C, 3
Jul-18, A, 1
Aug-18, B, 9
Aug-18, C, 3
Sep-18, A, 2
Sep-18, B, 7
Oct-18, C, 3
Oct-18, A, 6
Oct-18, B, 8
Nov-18, A, 2
Dec-18, B, 7
Dec-18, C, 9


来源: stackexchange

问:
Randomly sampling groups, followed by sampling within these sampled groups
My dataset contains several groups and each group can have a different number of unique observations. I carry out some calculations by group (simplified in the code below), resulting in a summary value for each group. Next, for the purpose of a bootstrap, I want to:

Randomly sample the groups with replacement (number of sampled groups = equal to number of different groups in the original dataset)

Within these sampled groups, randomly sample observations with replacement (number of sampled observations per group = equal to number of unique observations in that group in the original dataset)

A simplified version of my data set up (data1):

```<code>data1:

id    group  y
1001  1      10
1002  1      15
1003  1      3
3002  2      24
3003  2      15
3005  2      37
3006  2      32
3007  2      11
4001  3      12
4002  3      15
5006  4      7
5007  4      9
5009  4      22
5010  4      19
</code>```

E.g. based on the dataset example above: there are 4 groups in the original dataset, so I want to sample 4 groups with replacement (e.g. groups sampled = groups 4,3,3,1), and then sample observations/rows from those 4 groups (4 ids from group 4 (e.g. 5007, 5007, 5006, 5009); 2 ids from group 3 (twice, as group 3 was sampled twice), and 3 ids from group 1, all with replacement), and return the sampled rows together in a dataframe (4+2+2+3 = 11 rows).

For the above, I some have code working for these steps separately, but I cannot seem to combine them:

```<code># Calculate group value
y.group <- tapply(data1$y,data1$group,mean)

# Step 1. Sample groups, with replacement:
sampled.group <- sample(1:length(unique(data1$group)),replace=T)

# Step 2. Sample within groups, with replacement
data2 <- data.frame(data1 %>%
   group_by(group) %>%   # for each group
   sample_frac(1, replace = TRUE) %>%
   ungroup)

</code>```

Obviously, the code above in full does not do what I want, as in step 2 the sampled groups from step 1 are ignored since it just uses the original group var (I am aware of this). I have tried to solve this using step 1 and trying to generate a new dataframe containing only the sampled groups' observations (with duplicates if a group was sampled more than once, which is likely to happen), and then apply step 2 to that new dataframe, but I cannot get this to work.

I think I am just on the wrong path or overthinking things. Hopefully you can give me some advice on how to proceed.

Edit: While awaiting any potential solutions, I continued on the question myself and ended up with:

```<code>total.result <- c()
for (j in 1:length(unique(data1$group))){
     sampled.group <- sample(1:length(unique(data1$group)),size=1,replace=T)
     group.result <- sample_n(data1[data1$group==sampled.group,],
            size=length(unique(data1$id[data1$group==sampled.group])),replace=T)
     total.result <- rbind(total.result,group.result)
     }
</code>```

(So basically using a loop to sample the groups one at a time, creating datasets for each, and then sampling individual rows from those, and finally combining the results with rbind)

However, I think Allan Cameron's solution (see below) is more straigthforward, so I have accepted that one as the answer to my question.

答:
I think this is what you're looking for. Let's start with your data in a reproducible format:

data1 <- structure(list(id = structure(1:14, .Label = c("1001", "1002",
"1003", "3002", "3003", "3005", "3006", "3007", "4001", "4002",
"5006", "5007", "5009", "5010"), class = "factor"), group = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("1",
"2", "3", "4"), class = "factor"), y = structure(c(1L, 4L, 8L,
7L, 4L, 10L, 9L, 2L, 3L, 4L, 11L, 12L, 6L, 5L), .Label = c("10",
"11", "12", "15", "19", "22", "24", "3", "32", "37", "7", "9"
), class = "factor")), class = "data.frame", row.names = c(NA,
-14L))
```

<p>And just to make sure:</p>

```data1
#>      id group  y
#> 1  1001     1 10
#> 2  1002     1 15
#> 3  1003     1  3
#> 4  3002     2 24
#> 5  3003     2 15
#> 6  3005     2 37
#> 7  3006     2 32
#> 8  3007     2 11
#> 9  4001     3 12
#> 10 4002     3 15
#> 11 5006     4  7
#> 12 5007     4  9
#> 13 5009     4 22
#> 14 5010     4 19
```

<p>We start by splitting the data frame by <code>group</code> into smaller data frames, using the <code>split</code> function. This gives us a list with four data frames, each one containing all the members of its respective group. (The <code>set.seed</code> is there purely to make this example reproducible).</p>

```set.seed(69)
split_dfs <- split(data1, data1$group)
```

<p>Now we can sample this list, giving us a new list of four data frames drawn with replacement from <code>split_dfs</code>. Each one will again contain all the members of its respective group, though of course some whole groups might be sampled more than once, and other whole groups not sampled at all.</p>

```sampled_group_dfs <- split_dfs[sample(length(split_dfs), replace = TRUE)]
```

<p>Now we can sample <em>within</em> each group by sampling with replacement from the rows of each data frame in our new list. We do this for all our data frames in our list by using <code>lapply</code></p>

```all_sampled <- lapply(sampled_group_dfs, function(x) x[sample(nrow(x), replace = TRUE), ])
```

<p>All that remains is to stick all the resultant dataframes in this list back together to get our result:</p>

```result <- do.call(rbind, all_sampled)
```

<p>As you can see from the final result, it just so happens that each of the four groups was sampled once (this is just by chance - alter set.seed to get different results). However, within the groups there have clearly been some duplicates drawn. In fact, since R mandates unique row names in a data frame, these are easy to pick out by the <code>.1</code> that has been appended to the duplicate row names. If you don't like this, you can reset the row names with <code>rownames(result) <- seq(nrow(result))</code></p>

```result
#>          id group  y
#> 4.14   5010     4 19
#> 4.14.1 5010     4 19
#> 4.11   5006     4  7
#> 4.13   5009     4 22
#> 1.3    1003     1  3
#> 1.3.1  1003     1  3
#> 1.2    1002     1 15
#> 3.9    4001     3 12
#> 3.9.1  4001     3 12
#> 2.5    3003     2 15
#> 2.5.1  3003     2 15
#> 2.6    3005     2 37
#> 2.7    3006     2 32
#> 2.5.2  3003     2 15
```

Created on 2020-02-15 by the reprex package (v0.3.0)

ContentLicense: CC BY-SA 4.0

Body:
My dataset contains several groups and each group can have a different number of unique observations. I carry out some calculations by group (simplified in the code below), resulting in a summary value for each group. Next, for the purpose of a bootstrap, I want to:

Randomly sample the groups with replacement (number of sampled groups = equal to number of different groups in the original dataset)

Within these sampled groups, randomly sample observations with replacement (number of sampled observations per group = equal to number of unique observations in that group in the original dataset)

A simplified version of my data set up (data1):

data1:

id    group  y
1001  1      10
1002  1      15
1003  1      3
3002  2      24
3003  2      15
3005  2      37
3006  2      32
3007  2      11
4001  3      12
4002  3      15
5006  4      7
5007  4      9
5009  4      22
5010  4      19

E.g. based on the dataset example above: there are 4 groups in the original dataset, so I want to sample 4 groups with replacement (e.g. groups sampled = groups 4,3,3,1), and then sample observations/rows from those 4 groups (4 ids from group 4 (e.g. 5007, 5007, 5006, 5009); 2 ids from group 3 (twice, as group 3 was sampled twice), and 3 ids from group 1, all with replacement), and return the sampled rows together in a dataframe (4+2+2+3 = 11 rows).

For the above, I some have code working for these steps separately, but I cannot seem to combine them:

# Calculate group value
y.group <- tapply(data1$y,data1$group,mean)

# Step 1. Sample groups, with replacement:
sampled.group <- sample(1:length(unique(data1$group)),replace=T)

# Step 2. Sample within groups, with replacement
data2 <- data.frame(data1 %>%
   group_by(group) %>%   # for each group
   sample_frac(1, replace = TRUE) %>%
   ungroup)

Obviously, the code above in full does not do what I want, as in step 2 the sampled groups from step 1 are ignored since it just uses the original group var (I am aware of this). I have tried to solve this using step 1 and trying to generate a new dataframe containing only the sampled groups' observations (with duplicates if a group was sampled more than once, which is likely to happen), and then apply step 2 to that new dataframe, but I cannot get this to work.

I think I am just on the wrong path or overthinking things. Hopefully you can give me some advice on how to proceed.

Edit: While awaiting any potential solutions, I continued on the question myself and ended up with:

total.result <- c()
for (j in 1:length(unique(data1$group))){
     sampled.group <- sample(1:length(unique(data1$group)),size=1,replace=T)
     group.result <- sample_n(data1[data1$group==sampled.group,],
            size=length(unique(data1$id[data1$group==sampled.group])),replace=T)
     total.result <- rbind(total.result,group.result)
     }

(So basically using a loop to sample the groups one at a time, creating datasets for each, and then sampling individual rows from those, and finally combining the results with rbind)

However, I think Allan Cameron's solution (see below) is more straigthforward, so I have accepted that one as the answer to my question.

Body:
I think this is what you're looking for. Let's start with your data in a reproducible format:

data1 <- structure(list(id = structure(1:14, .Label = c("1001", "1002",
"1003", "3002", "3003", "3005", "3006", "3007", "4001", "4002",
"5006", "5007", "5009", "5010"), class = "factor"), group = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("1",
"2", "3", "4"), class = "factor"), y = structure(c(1L, 4L, 8L,
7L, 4L, 10L, 9L, 2L, 3L, 4L, 11L, 12L, 6L, 5L), .Label = c("10",
"11", "12", "15", "19", "22", "24", "3", "32", "37", "7", "9"
), class = "factor")), class = "data.frame", row.names = c(NA,
-14L))

And just to make sure:

data1
#>      id group  y
#> 1  1001     1 10
#> 2  1002     1 15
#> 3  1003     1  3
#> 4  3002     2 24
#> 5  3003     2 15
#> 6  3005     2 37
#> 7  3006     2 32
#> 8  3007     2 11
#> 9  4001     3 12
#> 10 4002     3 15
#> 11 5006     4  7
#> 12 5007     4  9
#> 13 5009     4 22
#> 14 5010     4 19

We start by splitting the data frame by group into smaller data frames, using the split function. This gives us a list with four data frames, each one containing all the members of its respective group. (The set.seed is there purely to make this example reproducible).

set.seed(69)
split_dfs <- split(data1, data1$group)

Now we can sample this list, giving us a new list of four data frames drawn with replacement from split_dfs. Each one will again contain all the members of its respective group, though of course some whole groups might be sampled more than once, and other whole groups not sampled at all.

sampled_group_dfs <- split_dfs[sample(length(split_dfs), replace = TRUE)]

Now we can sample within each group by sampling with replacement from the rows of each data frame in our new list. We do this for all our data frames in our list by using lapply

all_sampled <- lapply(sampled_group_dfs, function(x) x[sample(nrow(x), replace = TRUE), ])

All that remains is to stick all the resultant dataframes in this list back together to get our result:

result <- do.call(rbind, all_sampled)

As you can see from the final result, it just so happens that each of the four groups was sampled once (this is just by chance - alter set.seed to get different results). However, within the groups there have clearly been some duplicates drawn. In fact, since R mandates unique row names in a data frame, these are easy to pick out by the .1 that has been appended to the duplicate row names. If you don't like this, you can reset the row names with rownames(result) <- seq(nrow(result))

result
#>          id group  y
#> 4.14   5010     4 19
#> 4.14.1 5010     4 19
#> 4.11   5006     4  7
#> 4.13   5009     4 22
#> 1.3    1003     1  3
#> 1.3.1  1003     1  3
#> 1.2    1002     1 15
#> 3.9    4001     3 12
#> 3.9.1  4001     3 12
#> 2.5    3003     2 15
#> 2.5.1  3003     2 15
#> 2.6    3005     2 37
#> 2.7    3006     2 32
#> 2.5.2  3003     2 15

Created on 2020-02-15 by the reprex package (v0.3.0)


问:
Public __construct , retrieve user information class
I am looking to create a class and retrieve the account information in private and then make a public Getters
do you have any advice to improve this code?

```<code>    private $db;
    private $get;

    public function __construct($db = null)
    {
        $this->db = new Database();
    }

    private function get($information = 0, $userid = null)
    {
        if ($userid === null)
        {
            //if ($this->isOnline()) {
                $token = $_COOKIE['session'];

                $req = $this->db->query('SELECT user_id FROM cms_sessions WHERE token = :token',
                array(
                "token" => $token
                ));

                $userid = $req[0]['user_id'];
            //}
        }

        $req = $this->db->query('SELECT '. $information .' FROM users INNER JOIN users_info ON users.id = users_info.user_id WHERE users.id = :userid',
        array(
        "userid" => $userid
        ));

        return (count($req) > 0) ? htmlspecialchars($req[0][$information]) : "Erreur";
    }

    public function getId()
    {
        $req = $this->db->query('SELECT user_id FROM cms_sessions WHERE token = :token',
        array(
        "token" => @$_COOKIE['session']
        ));

        return (count($req) > 0) ? intval($req[0]['user_id']) : "0";
    }

    public function getIP()
    {
        return $this->get('last_ip');
    }

    public function getGender()
    {
        return $this->get('gender');
    }
</code>```

}

but this gives me '0' once I try to retrieve user information
Thank you for your response! cordially

答:
Welcome to stackoverflow!

"Improvement" is a really subjective topic. If I understand your code correctly then I would personally decouple the logic a little bit. In your concrete case I would use a Repository which accesses the database and return a Model and which will have your public getters.

```<code>class User
{

    private $attributes = [];

    public function __construct(array $attributes)
    {
        $this->attributes = $attributes;
    }

    public function getId()
    {
        return $this->attributes['id'];
    }

    public function getIP()
    {
        return $this->attributes['last_ip'];
    }

    public function getGender()
    {
        return $this->attributes['gender'];
    }

}
</code>```

```<code>class UserRepository
{
    private $db;

    public function __construct(Database $db)
    {
        $this->db = $db;
    }

    public function getById($id)
    {
        $result = $this->db->query('SELECT * FROM ... WHERE userid = :userid', ['userid' => $id]);
        return new User($result);
    }

    public function getByToken($token)
    {
        $result = $this->db->query('SELECT * FROM ... WHERE token = :token', ['token' => $token]);
        return new User($result);
    }
}
</code>```

And finally you could use it like this:

```<code>$repository = new UserRepository(new Database());

if (!empty($_COOKIE['session'])) {
    $user = $repository->getByToken($_COOKIE['session']);

} else if ($userid > 0) {
    $user = $repository->getById($userid);

} else {
  // Pseudo exit here. You should handle this accordingly.
  exit('User does not exist / not logged in');
}

echo $user->getId();
echo $user->getIP();
echo $user->getGender();
</code>```

Body:
I am looking to create a class and retrieve the account information in private and then make a public Getters
do you have any advice to improve this code?

    private $db;
    private $get;

    public function __construct($db = null)
    {
        $this->db = new Database();
    }

    private function get($information = 0, $userid = null)
    {
        if ($userid === null)
        {
            //if ($this->isOnline()) {
                $token = $_COOKIE['session'];

                $req = $this->db->query('SELECT user_id FROM cms_sessions WHERE token = :token',
                array(
                "token" => $token
                ));

                $userid = $req[0]['user_id'];
            //}
        }

        $req = $this->db->query('SELECT '. $information .' FROM users INNER JOIN users_info ON users.id = users_info.user_id WHERE users.id = :userid',
        array(
        "userid" => $userid
        ));

        return (count($req) > 0) ? htmlspecialchars($req[0][$information]) : "Erreur";
    }

    public function getId()
    {
        $req = $this->db->query('SELECT user_id FROM cms_sessions WHERE token = :token',
        array(
        "token" => @$_COOKIE['session']
        ));

        return (count($req) > 0) ? intval($req[0]['user_id']) : "0";
    }

    public function getIP()
    {
        return $this->get('last_ip');
    }

    public function getGender()
    {
        return $this->get('gender');
    }

}

but this gives me '0' once I try to retrieve user information
Thank you for your response! cordially

Body:
Welcome to stackoverflow!

"Improvement" is a really subjective topic. If I understand your code correctly then I would personally decouple the logic a little bit. In your concrete case I would use a Repository which accesses the database and return a Model and which will have your public getters.

class User
{

    private $attributes = [];

    public function __construct(array $attributes)
    {
        $this->attributes = $attributes;
    }

    public function getId()
    {
        return $this->attributes['id'];
    }

    public function getIP()
    {
        return $this->attributes['last_ip'];
    }

    public function getGender()
    {
        return $this->attributes['gender'];
    }

}

class UserRepository
{
    private $db;

    public function __construct(Database $db)
    {
        $this->db = $db;
    }

    public function getById($id)
    {
        $result = $this->db->query('SELECT * FROM ... WHERE userid = :userid', ['userid' => $id]);
        return new User($result);
    }

    public function getByToken($token)
    {
        $result = $this->db->query('SELECT * FROM ... WHERE token = :token', ['token' => $token]);
        return new User($result);
    }
}

And finally you could use it like this:

$repository = new UserRepository(new Database());

if (!empty($_COOKIE['session'])) {
    $user = $repository->getByToken($_COOKIE['session']);

} else if ($userid > 0) {
    $user = $repository->getById($userid);

} else {
  // Pseudo exit here. You should handle this accordingly.
  exit('User does not exist / not logged in');
}

echo $user->getId();
echo $user->getIP();
echo $user->getGender();


问:
Largest Subset whose sum is less than equal to a given sum
A list is defined as follows: [1, 2, 3]

and the sub-lists of this are:

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

Given K for example 3 the task is to find the largest length of sublist with sum of elements is less than equal to k.

I am aware of itertools in python but it will result in segmentation fault for larger lists. Is there any other efficient algorithm to achieve this? Any help would be appreciated.

My code is as allows:

```<code>from itertools import combinations
def  maxLength(a, k):
#print a,k
l= []

i = len(a)
while(i>=0):
    lst= list(combinations(sorted(a),i))
    for j in lst:
        #rint list(j)
        lst = list(j)
        #print sum(lst)
        sum1=0
        sum1 = sum(lst)
        if sum1<=k:
            return len(lst)
    i=i-1
</code>```

答:
Assume everything is positive. (Handling negatives is a simple extension of this and is left to the reader as an exercise). There exists an O(n) algorithm for the described problem. Using the O(n) median select, we partition the array based on the median. We find the sum of the left side. If that is greater than k, then we cannot take all elements, we must thus recur on the left half to try to take a smaller set. Otherwise, we subtract the sum of the left half from k, then we recur on the right half to see how many more elements we can take.

Partitioning the array based on median select and recurring on only 1 of the halves yields a runtime of n+n/2 +n/4 +n/8.. which geometrically sums up to O(n).

Body:
A list is defined as follows: [1, 2, 3]

and the sub-lists of this are:

[1], [2], [3],
[1,2]
[1,3]
[2,3]
[1,2,3]

Given K for example 3 the task is to find the largest length of sublist with sum of elements is less than equal to k.

I am aware of itertools in python but it will result in segmentation fault for larger lists. Is there any other efficient algorithm to achieve this? Any help would be appreciated.

My code is as allows:

from itertools import combinations
def  maxLength(a, k):
#print a,k
l= []

i = len(a)
while(i>=0):
    lst= list(combinations(sorted(a),i))
    for j in lst:
        #rint list(j)
        lst = list(j)
        #print sum(lst)
        sum1=0
        sum1 = sum(lst)
        if sum1<=k:
            return len(lst)
    i=i-1


问:
Entity Framework database-first approach Pascal case
I am using EF Core 3.0 with a database-first approach to Scaffold-DbContext from a SQL Server database:

```<code>dotnet ef dbcontext
       scaffold "Server=***;Database=***;User Id=***;Password=****;" Microsoft.EntityFrameworkCore.SqlServer
       -o Models -c "MyContext" --project=Data
</code>```

When the models are generated, it uses a specified naming for the variable for example:

if the column in the database called for example

ISBuy becomes Isbuy

CategoryID => CategoryId

and this really misses up my system ...

Is there is a way to prevent that converting and generate variable name with the same as database or is there is a super magical way to prevent just these and use camel case as EF Core does?

And I am not talking about the object that returned from the API because I found a lot of solutions for that case but never this one.

Thanks

答:
In PowerShell, you can use the UseDatabaseNames option (with a single dash) as part of a Scaffold-DbContext call to keep the casing used in the database schema.

```<code>Scaffold-DbContext "Server=.\SERVERNAME;Database=DBNAME;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir ENTITIESDIR -Context "NameContext" -UseDatabaseNames
</code>```

Body:
I am using EF Core 3.0 with a database-first approach to Scaffold-DbContext from a SQL Server database:

dotnet ef dbcontext
       scaffold "Server=***;Database=***;User Id=***;Password=****;" Microsoft.EntityFrameworkCore.SqlServer
       -o Models -c "MyContext" --project=Data

When the models are generated, it uses a specified naming for the variable for example:

if the column in the database called for example

ISBuy becomes Isbuy

CategoryID => CategoryId

and this really misses up my system ...

Is there is a way to prevent that converting and generate variable name with the same as database or is there is a super magical way to prevent just these and use camel case as EF Core does?

And I am not talking about the object that returned from the API because I found a lot of solutions for that case but never this one.

Thanks

Body:
In PowerShell, you can use the UseDatabaseNames option (with a single dash) as part of a Scaffold-DbContext call to keep the casing used in the database schema.

Scaffold-DbContext "Server=.\SERVERNAME;Database=DBNAME;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir ENTITIESDIR -Context "NameContext" -UseDatabaseNames


问:
Dask - How to connect to running cluster scheduler and access 'total_occupancy'?
I use the following to create a local cluster from a Jupyter notebook :

```<code>from dask.distributed import Client, LocalCluster

cluster = LocalCluster(n_workers=24)
c = Client(cluster)
</code>```

Is it possible to connect from another notebook when the kernel is occupied (compute operation) ?

My goal is to access to 'total_occupancy' for example.

答:
As suggested by @moshevi you can connect to the scheduler by providing the address.

```<code>client = Client("address-of-scheduler")
</code>```

Then you can use the client.run_on_scheduler command to execute operations on the remote scheduler

```<code>client.run_on_scheduler(lambda dask_scheduler: dask_scheduler.total_occupancy)
</code>```

https://docs.dask.org/en/latest/futures.html#distributed.Client.run_on_scheduler

Body:
I use the following to create a local cluster from a Jupyter notebook :

from dask.distributed import Client, LocalCluster

cluster = LocalCluster(n_workers=24)
c = Client(cluster)

Is it possible to connect from another notebook when the kernel is occupied (compute operation) ?

My goal is to access to 'total_occupancy' for example.

Body:
As suggested by @moshevi you can connect to the scheduler by providing the address.

client = Client("address-of-scheduler")

Then you can use the client.run_on_scheduler command to execute operations on the remote scheduler

client.run_on_scheduler(lambda dask_scheduler: dask_scheduler.total_occupancy)

https://docs.dask.org/en/latest/futures.html#distributed.Client.run_on_scheduler


问:
Function is not returning 'China UnionPay', not sure why
My function takes in a card number that is a string and returns the name of the card. Every card seems to be working but the China Pay one. I am getting undefined, not sure why. There are about 800 China Pay prefixes. I iterated through those 800 numbers and pushed them in to an empty array that I then searched the index of. If it matches one of the prefixes in the array and one of the two lengths it should return the name but its not.  </p>

<p>Any help would be greatly appreciated. I am sure there is a more efficient way to do this but this is where my brain went. It should work, not sure why it's not. </p>

```<code>  // loop through the 800 numbers and push to an empty array
  var chinaPayPrefix = [];
  for (let i = 622126; i <= 622925; i++){
    let chinaPayStringOne = i.toString();
    chinaPayPrefix.push(chinaPayStringOne);
  }
  for (let j = 6282; j <= 6288; j++){
    let chinaPayStringTwo = j.toString();
    chinaPayPrefix.push(chinaPayStringTwo);
  }
  // card lengths
  const discoverCardLength = [16,19];
  const maestroCardLength = [12,13,14,15,16,17,18,19];
  const visaCardLength = [13,16,19];
  const masterCardLength = 16;
  const dinersCardLength = 14;
  const americanExpressLength = 15;
  const chinaPayLength = [16,19];
  // card prefixes
  const discoverPrefix = ['6011','644','645','646','647','648','649','65'];
  const maestroPrefix = ['5018','5020','5038','6304'];
  const visaPrefix = ['4'];
  const mastercardPrefix = ['51','52','53','54','55'];
  const dinersPrefix = ['38', '39'];
  const americanExpressPrefix = ['34', '37'];
  // substrings of cardNumber
  let singleSplit = cardNumber.substring(0,1);
  let doubleSplit = cardNumber.substring(0,2);
  let tripleSplit = cardNumber.substring(0,3);
  let quadSplit = cardNumber.substring(0,4);
  let sextupleSplit = cardNumber.substring(0,7);
  // search if the applicable substring is present and length is correct
  if (dinersPrefix.indexOf(doubleSplit) > -1 && cardNumber.length === dinersCardLength) {
    return 'Diner\'s Club';
  } else if (americanExpressPrefix.indexOf(doubleSplit) > -1 && cardNumber.length === americanExpressLength) {
    return 'American Express';
  } else if (visaPrefix.indexOf(singleSplit) > -1 && visaCardLength.indexOf(cardNumber.length) > - 1){
    return 'Visa';
  } else if (mastercardPrefix.indexOf(doubleSplit) > -1 && cardNumber.length === masterCardLength){
    return 'MasterCard';
  } else if (discoverPrefix.indexOf(doubleSplit) > -1 || discoverPrefix.indexOf(tripleSplit) > -1 || discoverPrefix.indexOf(quadSplit) > -1 && discoverCardLength.indexOf(cardNumber.length) > - 1){
    return 'Discover';
  } else if (maestroPrefix.indexOf(quadSplit) > - 1 && maestroCardLength.indexOf(cardNumber.length) > - 1){
    return 'Maestro';
  } else if (chinaPayPrefix.indexOf(sextupleSplit) > - 1 && chinaPayLength.indexOf(cardNumber.length) > - 1){
    return 'China UnionPay';
  }else if (chinaPayPrefix.indexOf(quadSplit) > - 1 && chinaPayLength.indexOf(cardNumber.length) > - 1){
    return 'China UnionPay';
  }else if (chinaPayPrefix.indexOf(tripleSplit) > - 1 && chinaPayLength.indexOf(cardNumber.length) > - 1){
    return 'China UnionPay';
  }
};
</code>```

答:
This line for 6 characters:

```<code>let sextupleSplit = cardNumber.substring(0,7);
</code>```

should be

```<code>let sextupleSplit = cardNumber.substring(0,6)
</code>```

Body:
My function takes in a card number that is a string and returns the name of the card. Every card seems to be working but the China Pay one. I am getting undefined, not sure why. There are about 800 China Pay prefixes. I iterated through those 800 numbers and pushed them in to an empty array that I then searched the index of. If it matches one of the prefixes in the array and one of the two lengths it should return the name but its not.

Any help would be greatly appreciated. I am sure there is a more efficient way to do this but this is where my brain went. It should work, not sure why it's not.

  // loop through the 800 numbers and push to an empty array
  var chinaPayPrefix = [];
  for (let i = 622126; i <= 622925; i++){
    let chinaPayStringOne = i.toString();
    chinaPayPrefix.push(chinaPayStringOne);
  }
  for (let j = 6282; j <= 6288; j++){
    let chinaPayStringTwo = j.toString();
    chinaPayPrefix.push(chinaPayStringTwo);
  }
  // card lengths
  const discoverCardLength = [16,19];
  const maestroCardLength = [12,13,14,15,16,17,18,19];
  const visaCardLength = [13,16,19];
  const masterCardLength = 16;
  const dinersCardLength = 14;
  const americanExpressLength = 15;
  const chinaPayLength = [16,19];
  // card prefixes
  const discoverPrefix = ['6011','644','645','646','647','648','649','65'];
  const maestroPrefix = ['5018','5020','5038','6304'];
  const visaPrefix = ['4'];
  const mastercardPrefix = ['51','52','53','54','55'];
  const dinersPrefix = ['38', '39'];
  const americanExpressPrefix = ['34', '37'];
  // substrings of cardNumber
  let singleSplit = cardNumber.substring(0,1);
  let doubleSplit = cardNumber.substring(0,2);
  let tripleSplit = cardNumber.substring(0,3);
  let quadSplit = cardNumber.substring(0,4);
  let sextupleSplit = cardNumber.substring(0,7);
  // search if the applicable substring is present and length is correct
  if (dinersPrefix.indexOf(doubleSplit) > -1 && cardNumber.length === dinersCardLength) {
    return 'Diner\'s Club';
  } else if (americanExpressPrefix.indexOf(doubleSplit) > -1 && cardNumber.length === americanExpressLength) {
    return 'American Express';
  } else if (visaPrefix.indexOf(singleSplit) > -1 && visaCardLength.indexOf(cardNumber.length) > - 1){
    return 'Visa';
  } else if (mastercardPrefix.indexOf(doubleSplit) > -1 && cardNumber.length === masterCardLength){
    return 'MasterCard';
  } else if (discoverPrefix.indexOf(doubleSplit) > -1 || discoverPrefix.indexOf(tripleSplit) > -1 || discoverPrefix.indexOf(quadSplit) > -1 && discoverCardLength.indexOf(cardNumber.length) > - 1){
    return 'Discover';
  } else if (maestroPrefix.indexOf(quadSplit) > - 1 && maestroCardLength.indexOf(cardNumber.length) > - 1){
    return 'Maestro';
  } else if (chinaPayPrefix.indexOf(sextupleSplit) > - 1 && chinaPayLength.indexOf(cardNumber.length) > - 1){
    return 'China UnionPay';
  }else if (chinaPayPrefix.indexOf(quadSplit) > - 1 && chinaPayLength.indexOf(cardNumber.length) > - 1){
    return 'China UnionPay';
  }else if (chinaPayPrefix.indexOf(tripleSplit) > - 1 && chinaPayLength.indexOf(cardNumber.length) > - 1){
    return 'China UnionPay';
  }
};

Body:
This line for 6 characters:

let sextupleSplit = cardNumber.substring(0,7);

should be

let sextupleSplit = cardNumber.substring(0,6)


问:
How to save data from BLE heart sensor to file using node.js
I will try my best to explain my problem. So I have heart rate sensor which is sending me a data via BLE connection. I checked it and it is working very well but I want it to save on raspberry pi to some kind of file. However, saving all data to file will destroy my poor raspberry so I want to only save last three logs (or even one doesnt matter) because in the next step I will be sending them to my server.

TL;DR

-Save data from BLE to a file on Raspberry Pi Zero using node.js

-Only three last logs

-Sending and saving data have to be endless

答:
Here is a solution which only saves the last log (you mentioned it doesnt matter) in a file. im assuming you want to save the data in a .txt file. If this isnt the case, just switch the file extension.

```<code>const fs = require('fs');

let data = 'YOUR DATASTRING HERE';

let writeStream = fs.createWriteStream('./output.txt', {flags: 'w'});
writeStream.write(data);
</code>```

The option {flags: 'w'} is set by default and is just doing the following:

Open file for writing. The file is created (if it does not exist) or truncated (if it exists) (https://nodejs.org/api/fs.html#fs_file_system_flags)

So every time you execute this script, the file will be replaced.

In case you want to store your data in a .json file, you need to change the code a little bit, because .write only accepts strings or buffer-objects.

```<code>const fs = require('fs');

let data = {test: 'ThisIsATest'};

let writeStream = fs.createWriteStream('./output.json', {flags: 'w'});
writeStream.write(JSON.stringify(data));
</code>```

Body:
Here is a solution which only saves the last log (you mentioned it doesnt matter) in a file. im assuming you want to save the data in a .txt file. If this isnt the case, just switch the file extension.

const fs = require('fs');

let data = 'YOUR DATASTRING HERE';

let writeStream = fs.createWriteStream('./output.txt', {flags: 'w'});
writeStream.write(data);

The option {flags: 'w'} is set by default and is just doing the following:

Open file for writing. The file is created (if it does not exist) or truncated (if it exists) (https://nodejs.org/api/fs.html#fs_file_system_flags)

So every time you execute this script, the file will be replaced.

In case you want to store your data in a .json file, you need to change the code a little bit, because .write only accepts strings or buffer-objects.

const fs = require('fs');

let data = {test: 'ThisIsATest'};

let writeStream = fs.createWriteStream('./output.json', {flags: 'w'});
writeStream.write(JSON.stringify(data));


问:
cvxopt.glpk.ilp Documentation
I've seen that CVXOPT supports GLPK and one can do:

```<code>from cvxopt.glpk import ilp
</code>```

However, I cannot find the documentation for the glpk module in cvxopt's documentation. I am trying to solve an integer program and I want to understand the ilp interface.

答:
cvxopt.glpk solves ilp by using GLPK.

Consider the following LP:

```<code>Min -3x1 -x2
 x1 + x2 <= 10
    - x2 <= -4.5
</code>```

This becomes (assuming first x1,x2 fractional, and then integers).

```<code>>>> c = matrix(np.array([-3,-1],dtype=float))
>>> h = matrix(np.array([10,-4.5],dtype=float))
>>> G = matrix(np.array([[1,1],[0,-1]],dtype=float))
>>> (status,x) = ilp(c=c,G=G,h=h)
>>> print(x)
[ 5.50e+00]
[ 4.50e+00]
>>> (status,x) = ilp(c=c,G=G,h=h,I=set(range(2)))
>>> print(x)
[ 5.00e+00]
[ 5.00e+00]
</code>```

For additional information you can refer to the documentation

```<code>>>> help(ilp)

    PURPOSE
    Solves the mixed integer linear programming problem

        minimize    c'*x
        subject to  G*x <= h
                    A*x = b
                    x[k] is integer for k in I
                    x[k] is binary for k in B

    ARGUMENTS
    c            nx1 dense 'd' matrix with n>=1

    G            mxn dense or sparse 'd' matrix with m>=1

    h            mx1 dense 'd' matrix

    A            pxn dense or sparse 'd' matrix with p>=0

    b            px1 dense 'd' matrix

    I            set of indices of integer variables

    B            set of indices of binary variables
</code>```

Title: cvxopt.glpk.ilp Documentation

Body:
I've seen that CVXOPT supports GLPK and one can do:

from cvxopt.glpk import ilp

However, I cannot find the documentation for the glpk module in cvxopt's documentation. I am trying to solve an integer program and I want to understand the ilp interface.

Body:
cvxopt.glpk solves ilp by using GLPK.

Consider the following LP:

Min -3x1 -x2
 x1 + x2 <= 10
    - x2 <= -4.5

This becomes (assuming first x1,x2 fractional, and then integers).

>>> c = matrix(np.array([-3,-1],dtype=float))
>>> h = matrix(np.array([10,-4.5],dtype=float))
>>> G = matrix(np.array([[1,1],[0,-1]],dtype=float))
>>> (status,x) = ilp(c=c,G=G,h=h)
>>> print(x)
[ 5.50e+00]
[ 4.50e+00]
>>> (status,x) = ilp(c=c,G=G,h=h,I=set(range(2)))
>>> print(x)
[ 5.00e+00]
[ 5.00e+00]

For additional information you can refer to the documentation

>>> help(ilp)

    PURPOSE
    Solves the mixed integer linear programming problem

        minimize    c'*x
        subject to  G*x <= h
                    A*x = b
                    x[k] is integer for k in I
                    x[k] is binary for k in B

    ARGUMENTS
    c            nx1 dense 'd' matrix with n>=1

    G            mxn dense or sparse 'd' matrix with m>=1

    h            mx1 dense 'd' matrix

    A            pxn dense or sparse 'd' matrix with p>=0

    b            px1 dense 'd' matrix

    I            set of indices of integer variables

    B            set of indices of binary variables


问:
Extracting class within class in beautifulsoup
I am trying to extract customer service star rating from the three reviews in the html:

Currently, my code looks something like this:

```<code>oururl= urllib.request.urlopen(x).read()
soup=BeautifulSoup(oururl,'lxml')
for rating in soup.find_all("div",{"class":"rating-customer-service"}):
    list_rating.append(rating.find_all("div",{"class":"star-rating"}))
</code>```

But I do not get the star ratings in the list_rating. Can anyone suggest alternate approaches?

答:
Try this.</p>

```<code>oururl= urllib.request.urlopen(x).read()
soup=BeautifulSoup(oururl,'lxml')
for rating in soup.find_all("div",{"class":"star-rating"}):
    list_rating.append(rating.span.text)
</code>```

Body:
I am trying to extract customer service star rating from the three reviews in the html:

Currently, my code looks something like this:

oururl= urllib.request.urlopen(x).read()
soup=BeautifulSoup(oururl,'lxml')
for rating in soup.find_all("div",{"class":"rating-customer-service"}):
    list_rating.append(rating.find_all("div",{"class":"star-rating"}))

But I do not get the star ratings in the list_rating. Can anyone suggest alternate approaches?

Body:
Try this.

oururl= urllib.request.urlopen(x).read()
soup=BeautifulSoup(oururl,'lxml')
for rating in soup.find_all("div",{"class":"star-rating"}):
    list_rating.append(rating.span.text)


问:
complex With query on SQL Server 2008
Could you please explain what do these lines of code do?

Table 1: INT,VARCHAR,FLOAT

```<code>ID Name  value
---------------------------
1   a1  32116580
2   a2  50785384
3   a3  54327508
4   a4  61030844

;with coords(i,row,col,total,N) as (
select 1,1,1,N.N*(N.N+1)/2, N.N
from (select count(*) N from table1) N
union all
select i+1,
       case when col+1>N then row+1 else row end,
       case when col+1>N then row+1 else col+1 end,
       total, N
from coords
where i<total
)
</code>```

I know with provides a way to write auxiliary statements for use in a larger query, so it is like if I declare some variables I would use, but after that I am a little confused...And Why the use of case for getting row and col; Also why in the case there are two:  case when col+1>N then row+1 else , How does SQL know when to do one case or the other?...

```<code>i  row col total N
--------------------
1   1   1   10   4
2   1   2   10   4
3   1   3   10   4
4   1   4   10   4
5   2   2   10   4
6   2   3   10   4
7   2   4   10   4
8   3   3   10   4
9   3   4   10   4
10  4   4   10   4
</code>```

答:
The columns of table1 can be ignored since only the count is being used - thus you can rewrite your CTE as:

```<code>;with coords(i,row,col,total,N) as (
    select 1, 1, 1, 10, 4
    union all
    select i+1,
           case when col+1>N then row+1 else row end,
           case when col+1>N then row+1 else col+1 end,
           total, N
    from coords
    where i<total
    )
SELECT * FROM coords
</code>```

...with a SELECT * FROM coords there to visualise the results.

The select 1, 1, 1, 10, 4 'seeds' the recursion - this is the row from which later levels of the recursion will derive.  The rest of the rows are built up from the second query acting upon (initially) the seed row and then the resultant row from the second query fed back in to itself etc.

ContentLicense: CC BY-SA 3.0

Title: complex With query on SQL Server 2008

Body:
Could you please explain what do these lines of code do?

Table 1: INT,VARCHAR,FLOAT

ID Name  value
---------------------------
1   a1  32116580
2   a2  50785384
3   a3  54327508
4   a4  61030844

;with coords(i,row,col,total,N) as (
select 1,1,1,N.N*(N.N+1)/2, N.N
from (select count(*) N from table1) N
union all
select i+1,
       case when col+1>N then row+1 else row end,
       case when col+1>N then row+1 else col+1 end,
       total, N
from coords
where i<total
)

I know with provides a way to write auxiliary statements for use in a larger query, so it is like if I declare some variables I would use, but after that I am a little confused...And Why the use of case for getting row and col; Also why in the case there are two:  case when col+1>N then row+1 else , How does SQL know when to do one case or the other?...

i  row col total N
--------------------
1   1   1   10   4
2   1   2   10   4
3   1   3   10   4
4   1   4   10   4
5   2   2   10   4
6   2   3   10   4
7   2   4   10   4
8   3   3   10   4
9   3   4   10   4
10  4   4   10   4

Body:
The columns of table1 can be ignored since only the count is being used - thus you can rewrite your CTE as:

;with coords(i,row,col,total,N) as (
    select 1, 1, 1, 10, 4
    union all
    select i+1,
           case when col+1>N then row+1 else row end,
           case when col+1>N then row+1 else col+1 end,
           total, N
    from coords
    where i<total
    )
SELECT * FROM coords

...with a SELECT * FROM coords there to visualise the results.

The select 1, 1, 1, 10, 4 'seeds' the recursion - this is the row from which later levels of the recursion will derive.  The rest of the rows are built up from the second query acting upon (initially) the seed row and then the resultant row from the second query fed back in to itself etc.


问:
Finding data ending in ___ in SQL?
Trying to get get this information from a database: number of Zip codes where the city name ends in ‘boro’ or 'town' and has an average house value at or below $250,000.

This is what I put:

```<code>select count(*)
from zip_codes
where city like ’%boro’ and city like ‘%town’ and average_house_value <= 250000;
</code>```

and keep getting this message: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%boro’ OR city like ‘%town’) and average_house_value <= 250000' at line 1

help?

答:
I would suggest regular expressions for this:</p>

```<code>where city regexp '(boro|town)$' and
      average_house_value <= 250000;
</code>```

Title: Finding data ending in ___ in SQL?

Body:
Trying to get get this information from a database: number of Zip codes where the city name ends in ‘boro’ or 'town' and has an average house value at or below $250,000.

This is what I put:

select count(*)
from zip_codes
where city like ’%boro’ and city like ‘%town’ and average_house_value <= 250000;

and keep getting this message: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%boro’ OR city like ‘%town’) and average_house_value <= 250000' at line 1

help?

Body:
I would suggest regular expressions for this:

where city regexp '(boro|town)$' and
      average_house_value <= 250000;


问:
How to trim an std::string?
I'm currently using the following code to right-trim all the std::strings in my programs:

```<code>std::string s;
s.erase(s.find_last_not_of(" \n\r\t")+1);
</code>```

It works fine, but I wonder if there are some end-cases where it might fail?

Of course, answers with elegant alternatives and also left-trim solution are welcome.

答:
The accepted answer and even Boost's version did not work for me, so I wrote the following version:

```<code>std::string trim(const std::string& input) {
    std::stringstream string_stream;
    for (const auto character : input) {
        if (!isspace(character)) {
            string_stream << character;
        }
    }

    return string_stream.str();
}
</code>```

This will remove any whitespace character from anywhere in the string and return a new copy of the string.

OwnerDisplayName: Milan Babuskov

Title: How to trim an std::string?

Body:
I'm currently using the following code to right-trim all the std::strings in my programs:

std::string s;
s.erase(s.find_last_not_of(" \n\r\t")+1);

It works fine, but I wonder if there are some end-cases where it might fail?

Of course, answers with elegant alternatives and also left-trim solution are welcome.

LastEditorDisplayName: Brian R. Bondy

Body:
The accepted answer and even Boost's version did not work for me, so I wrote the following version:

std::string trim(const std::string& input) {
    std::stringstream string_stream;
    for (const auto character : input) {
        if (!isspace(character)) {
            string_stream << character;
        }
    }

    return string_stream.str();
}

This will remove any whitespace character from anywhere in the string and return a new copy of the string.


问:
TypeERROR after yarn upgrade (ReactJS project)
I ran the command yarn upgrade and then I got this error next time I did yarn start, and I have no idea why? Hoping someone can tell me whats going on here? No idea what to do to solve it. Tried googling it, without much luck as to what is wrong. </p>

```<code>TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
at validateString (internal/validators.js:113:11)
at Object.join (path.js:375:7)
at noopServiceWorkerMiddleware (C:\Users\perni\Projects\prime\node_modules\react-dev-utils\noopServiceWorkerMiddleware.js:14:26)
at Layer.handle [as handle_request] (C:\Users\perni\Projects\prime\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:317:13)
at C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:275:10)
at launchEditorMiddleware (C:\Users\perni\Projects\prime\node_modules\react-dev-utils\errorOverlayMiddleware.js:20:7)
at Layer.handle [as handle_request] (C:\Users\perni\Projects\prime\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:317:13)
at C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:275:10)
at handleWebpackInternalMiddleware (C:\Users\perni\Projects\prime\node_modules\react-dev-utils\evalSourceMapMiddleware.js:42:7)
at Layer.handle [as handle_request] (C:\Users\perni\Projects\prime\node_modules\express\lib\router\layer.js:95:5)
</code>```

答: Try deleting the node-modules and reinstall  with you new version</p>

Body:
I ran the command yarn upgrade and then I got this error next time I did yarn start, and I have no idea why? Hoping someone can tell me whats going on here? No idea what to do to solve it. Tried googling it, without much luck as to what is wrong.

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
at validateString (internal/validators.js:113:11)
at Object.join (path.js:375:7)
at noopServiceWorkerMiddleware (C:\Users\perni\Projects\prime\node_modules\react-dev-utils\noopServiceWorkerMiddleware.js:14:26)
at Layer.handle [as handle_request] (C:\Users\perni\Projects\prime\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:317:13)
at C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:275:10)
at launchEditorMiddleware (C:\Users\perni\Projects\prime\node_modules\react-dev-utils\errorOverlayMiddleware.js:20:7)
at Layer.handle [as handle_request] (C:\Users\perni\Projects\prime\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:317:13)
at C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\perni\Projects\prime\node_modules\express\lib\router\index.js:275:10)
at handleWebpackInternalMiddleware (C:\Users\perni\Projects\prime\node_modules\react-dev-utils\evalSourceMapMiddleware.js:42:7)
at Layer.handle [as handle_request] (C:\Users\perni\Projects\prime\node_modules\express\lib\router\layer.js:95:5)


问:
Values of a Dask Dataframe
Is there a way to get values of a dask dataframe without using compute function as pandas?

Pandas: df.values

Dask using compute function: ddf.compute().values

Dask using compute function is slow and I have to transform the dask dataframe to json format and to do that I should get the values.

答:
I have to transform the dask dataframe to json format and

You may be interested in the .to_json method of Dask dataframes

and to do that I should get the values.

This may not be true

Title: Values of a Dask Dataframe


问:
JSX element type 'HTMLImageElement' is not a constructor function for JSX elements
```<code>I am trying an example with React Native and Typescript and for the following code:

<Image
        style={{
          width: 51,
          height: 51,
          resizeMode: 'contain',
        }}
        source={{ uri: portretPhoto }}
        style={styles.uploadedImage}
        resizeMode="contain"
      />
</code>```

I get error:
JSX element type 'HTMLImageElement' is not a constructor function for JSX elements.
  Type 'HTMLImageElement' is missing the following properties from type 'ElementClass': render, context, setState, forceUpdate, and 3 more.

How do I fix it?

答: There is an Image defined globally. You need to import the one from react-native

Body:
I am trying an example with React Native and Typescript and for the following code:

<Image
        style={{
          width: 51,
          height: 51,
          resizeMode: 'contain',
        }}
        source={{ uri: portretPhoto }}
        style={styles.uploadedImage}
        resizeMode="contain"
      />

I get error:
JSX element type 'HTMLImageElement' is not a constructor function for JSX elements.
  Type 'HTMLImageElement' is missing the following properties from type 'ElementClass': render, context, setState, forceUpdate, and 3 more.

How do I fix it?


问:
C++ primer 5 edition: class Message and Folder
I am trying to write the solution for an exercise in C++ primer 5 ed. The program is writing two classes Message and Folder. each Folder has a set of pointers to a Message object and each Message has a set of pointers to Folders in which it exists so I wrote this code:

For brevity I didn't show the whole code so I'll try to focus on the part may causing problem:

```<code>class Folder;
class Message
{
public:
    Message(const std::string & = "");
    Message(const Message&);
    Message(Message&&);
    Message& operator=(const Message&);
    Message& operator=(Message&&);
    ~Message();

    const std::string& getMsg()const;
    void setMsg(const std::string&);
    void  save(Folder&);
    void remove(Folder&);

//private:
    std::string msg_;
    std::set<Folder*> dirs_;

    void addToDirs(const Message&);
    void delFromDirs();
    void moveMsgs(Message&);

    friend void swap(Message&, Message&);
    friend class Folder;
};

class Folder
{
public:
    void addMsg(Message&);
    void delMsg(Message&);

//private:
    std::set<Message*> msgs_;
};

Message::Message(const std::string& msg) :
    msg_(msg)
{}

Message::Message(const Message& rhs) :
    msg_(rhs.msg_),
    dirs_(rhs.dirs_)
{
    addToDirs(rhs);
}

Message& Message::operator=(const Message& rhs)
{
    delFromDirs();
    msg_ = rhs.msg_;
    dirs_ = rhs.dirs_;
    addToDirs(rhs);

    return *this;
}

void Message::addToDirs(const Message& msg)
{
    for (auto& fdr : msg.dirs_)
        fdr->addMsg(*this);
}

Message::~Message()
{
    delFromDirs();
}

void Message::save(Folder& fdr)
{
    fdr.addMsg(*this);
}

void Message::remove(Folder& fdr)
{
    fdr.delMsg(*this);
}

void Message::delFromDirs()
{
    for (auto fdr : dirs_)
        fdr->delMsg(*this);
}

void Folder::addMsg( Message& msg)
{
    msgs_.insert(&msg);
    msg.dirs_.insert(this);
}

void Folder::delMsg(Message& msg)
{
    msgs_.erase(&msg);
    msg.dirs_.erase(this);
}

const std::string& Message::getMsg()const
{
    return msg_;
}

void Message::setMsg(const std::string& msg)
{
    msg_ = msg;
}

void Message::moveMsgs(Message& msg)
{
    msg_ = std::move(msg.msg_);
    dirs_ = std::move(msg.dirs_);

    for (auto& fdr : dirs_)
    {
        fdr->delMsg(msg);
        fdr->addMsg(*this);
    }
    msg.dirs_.clear();
}

Message::Message(Message&& rhs) :
    msg_(std::move(rhs.msg_)),
    dirs_(std::move(rhs.dirs_))
{
    std::cout << "Message's move-ctor\n";
    moveMsgs(rhs);
}
Message& Message::operator=(Message&& rhs)
{
    std::cout << "=(Message&&)\n";
    if (this != &rhs)
    {
        delFromDirs();
        moveMsgs(rhs);
    }
    return *this;
}

int main()
{

    Folder f1, f2, f3;

    Message m1("Message m1"),
        m2("Message m2"), m3("Message m3"), m4("Message m4"),
        m5("Message m5");

    f1.addMsg(m1);
    f1.addMsg(m2);
    f1.addMsg(m5);
    f2.addMsg(m1);
    f2.addMsg(m4);
    f3.addMsg(m3);

    cout << "f1.msgs: \n";
    for (auto& msg : f1.msgs_)
        cout << msg->msg_ << endl;
    cout << endl;

    std::cout << "\nDone!\n";
}
</code>```

The problem is: The code works fine on VC++19 and compiled on wandbox.org using GCC-Head 10 and Clang 10 and works fine but I have an another compiler on my phone called "coding C++" it says:

"process completed (signal 11) press enter".

I want to know whether delFromDirs() is correctly handling deletion? Because the destructor of Message should remove each folder from its set of folders and each folder from them should remove that message from its set of messages.

答:
You are iterating over dirs_ in delFromDirs and you are calling fdr->delMsg(*this) which does

```<code>msgs_.erase(&msg);
msg.dirs_.erase(this);
</code>```

The line msg.dirs_.erase(this) erases fdr from the dirs_ that you are iterating over. Since this is the element that you are currently iterating over, it will invalidate the current iterator of the range-for loop and then when the loop tries to increment the iterator, it will cause undefined behavior.

Don't remove elements from a container that you are currently iterating over, at least not the current element of a range-for loop. If you need to do that, you can take the return value from erase (which will be a valid iterator to the next element) and reset your loop iterator to continue from there. This then doesn't work with a range-for, though.

Body:
I am trying to write the solution for an exercise in C++ primer 5 ed. The program is writing two classes Message and Folder. each Folder has a set of pointers to a Message object and each Message has a set of pointers to Folders in which it exists so I wrote this code:

For brevity I didn't show the whole code so I'll try to focus on the part may causing problem:

class Folder;
class Message
{
public:
    Message(const std::string & = "");
    Message(const Message&);
    Message(Message&&);
    Message& operator=(const Message&);
    Message& operator=(Message&&);
    ~Message();

    const std::string& getMsg()const;
    void setMsg(const std::string&);
    void  save(Folder&);
    void remove(Folder&);

//private:
    std::string msg_;
    std::set<Folder*> dirs_;

    void addToDirs(const Message&);
    void delFromDirs();
    void moveMsgs(Message&);

    friend void swap(Message&, Message&);
    friend class Folder;
};

class Folder
{
public:
    void addMsg(Message&);
    void delMsg(Message&);

//private:
    std::set<Message*> msgs_;
};

Message::Message(const std::string& msg) :
    msg_(msg)
{}

Message::Message(const Message& rhs) :
    msg_(rhs.msg_),
    dirs_(rhs.dirs_)
{
    addToDirs(rhs);
}

Message& Message::operator=(const Message& rhs)
{
    delFromDirs();
    msg_ = rhs.msg_;
    dirs_ = rhs.dirs_;
    addToDirs(rhs);

    return *this;
}

void Message::addToDirs(const Message& msg)
{
    for (auto& fdr : msg.dirs_)
        fdr->addMsg(*this);
}

Message::~Message()
{
    delFromDirs();
}

void Message::save(Folder& fdr)
{
    fdr.addMsg(*this);
}

void Message::remove(Folder& fdr)
{
    fdr.delMsg(*this);
}

void Message::delFromDirs()
{
    for (auto fdr : dirs_)
        fdr->delMsg(*this);
}

void Folder::addMsg( Message& msg)
{
    msgs_.insert(&msg);
    msg.dirs_.insert(this);
}

void Folder::delMsg(Message& msg)
{
    msgs_.erase(&msg);
    msg.dirs_.erase(this);
}

const std::string& Message::getMsg()const
{
    return msg_;
}

void Message::setMsg(const std::string& msg)
{
    msg_ = msg;
}

void Message::moveMsgs(Message& msg)
{
    msg_ = std::move(msg.msg_);
    dirs_ = std::move(msg.dirs_);

    for (auto& fdr : dirs_)
    {
        fdr->delMsg(msg);
        fdr->addMsg(*this);
    }
    msg.dirs_.clear();
}

Message::Message(Message&& rhs) :
    msg_(std::move(rhs.msg_)),
    dirs_(std::move(rhs.dirs_))
{
    std::cout << "Message's move-ctor\n";
    moveMsgs(rhs);
}
Message& Message::operator=(Message&& rhs)
{
    std::cout << "=(Message&&)\n";
    if (this != &rhs)
    {
        delFromDirs();
        moveMsgs(rhs);
    }
    return *this;
}

int main()
{

    Folder f1, f2, f3;

    Message m1("Message m1"),
        m2("Message m2"), m3("Message m3"), m4("Message m4"),
        m5("Message m5");

    f1.addMsg(m1);
    f1.addMsg(m2);
    f1.addMsg(m5);
    f2.addMsg(m1);
    f2.addMsg(m4);
    f3.addMsg(m3);

    cout << "f1.msgs: \n";
    for (auto& msg : f1.msgs_)
        cout << msg->msg_ << endl;
    cout << endl;

    std::cout << "\nDone!\n";
}

The problem is: The code works fine on VC++19 and compiled on wandbox.org using GCC-Head 10 and Clang 10 and works fine but I have an another compiler on my phone called "coding C++" it says:

"process completed (signal 11) press enter".

I want to know whether delFromDirs() is correctly handling deletion? Because the destructor of Message should remove each folder from its set of folders and each folder from them should remove that message from its set of messages.

Body:
You are iterating over dirs_ in delFromDirs and you are calling fdr->delMsg(*this) which does

msgs_.erase(&msg);
msg.dirs_.erase(this);

The line msg.dirs_.erase(this) erases fdr from the dirs_ that you are iterating over. Since this is the element that you are currently iterating over, it will invalidate the current iterator of the range-for loop and then when the loop tries to increment the iterator, it will cause undefined behavior.

Don't remove elements from a container that you are currently iterating over, at least not the current element of a range-for loop. If you need to do that, you can take the return value from erase (which will be a valid iterator to the next element) and reset your loop iterator to continue from there. This then doesn't work with a range-for, though.


问:
Git merge file content outside of a repo (--no-index)
The git diff command (with the --no-index flag) works incredibly well to diff two files, irrespective of whether or not these two files are contained in a Git repo or not.

Example

```<code>$ git diff --no-index -- filea fileb
// shows diff between filea and fileb
</code>```

Question

Is it also possible to use Git to selectively "merge" (i.e. git add) content from one file to another if they are not part of a Git repo? Basically like a git add -p but not from the working directory to the index, but from fileb to filea.

I feel like all the basic building blocks (plumbing commands) should be available. Possibly something involving format-patch and/or apply?

```<code>$ <command> filea fileb
// wanted: interactively choose patches/hunks from fileb and apply them to filea
</code>```

答:
The git add -p command is—or at least was1—a big Perl script.  To view it, find the "core" directory:

```<code>git --exec-path
</code>```

which will print something like /usr/lib/git-core or /usr/local/libexec/git-core.  This is where various Git internal binaries and scripts live.

In whatever directory gets printed out, look for git-add--interactive.  This is, or was, the git add -p code.

This Perl program runs the Git diff and apply commands, and should be relatively easy to modify to make it do what you would like done.  Note that this is used for all of git add -p, git reset -p, and git checkout -p, which is why it's nearly 2000 lines long.

The Git project folks are in the middle of rewriting all of this to make it impossible for you to modify. :-) Er, that is, to make it written in C to go fast—you'll still have the source, and can still modify it all you like, but as a C program, it will be much harder to work on.

Body:
The git diff command (with the --no-index flag) works incredibly well to diff two files, irrespective of whether or not these two files are contained in a Git repo or not.

Example

$ git diff --no-index -- filea fileb
// shows diff between filea and fileb

Question

Is it also possible to use Git to selectively "merge" (i.e. git add) content from one file to another if they are not part of a Git repo? Basically like a git add -p but not from the working directory to the index, but from fileb to filea.

I feel like all the basic building blocks (plumbing commands) should be available. Possibly something involving format-patch and/or apply?

$ <command> filea fileb
// wanted: interactively choose patches/hunks from fileb and apply them to filea

Body:
The git add -p command is—or at least was1—a big Perl script.  To view it, find the "core" directory:

git --exec-path

which will print something like /usr/lib/git-core or /usr/local/libexec/git-core.  This is where various Git internal binaries and scripts live.

In whatever directory gets printed out, look for git-add--interactive.  This is, or was, the git add -p code.

This Perl program runs the Git diff and apply commands, and should be relatively easy to modify to make it do what you would like done.  Note that this is used for all of git add -p, git reset -p, and git checkout -p, which is why it's nearly 2000 lines long.

The Git project folks are in the middle of rewriting all of this to make it impossible for you to modify. :-) Er, that is, to make it written in C to go fast—you'll still have the source, and can still modify it all you like, but as a C program, it will be much harder to work on.


问:
How do I replace the deprecated "constructor" method for PHP7 in Wordpress?
I'm working on moving a site from a PHP5 server to a server with PHP7. My client is using a theme from the company Themelovin which appears to have gone dark. They have a few helper plugins like a Portfolio plugin, Twitter plugin and Teams plugin that come from a private repo with the purchase of a theme. I tried to get the latest version to see if it's been fixed but it hasn't.

Their Portfolio and Teams plugins both used an old constructor that is now deprecated with PHP7.

The original code looked like so:

```<code> class thmlv_widget_team extends WP_Widget{
   function thmlv_widget_team(){
     $widget_ops = array('classname' => 'team-items', 'description' => __('Display team members.', 'themelovin'));
     $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'thmlv_widget_team');
     $this->WP_Widget('thmlv_widget_team', __('Themelovin Team', 'themelovin'), $widget_ops, $control_ops);
   }
</code>```

From what i've read, the issue here is the class "thmlv_widget_team" is the same name as the function. I am very poor at PHP but my understanding is that is the conflict. I read that you can fix this using __construct() and creating something that uses that plus a backup however that still isn't working. This is what I have now:

```<code>class thmlv_widget_team extends WP_Widget{
    function __construct(){
      $widget_ops = array('classname' => 'team-items', 'description' => __('Display team members.', 'themelovin'));
      $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'thmlv_widget_team');
      $this->WP_Widget('thmlv_widget_team', __('Themelovin Team', 'themelovin'), $widget_ops, $control_ops);
    }

    function thmlv_widget_team(){
      self::__construct();
    }
</code>```

Ultimately I created the function __construct() with the code and then had a fallback for the original function to reference the construct I created.

Can you assist in what i'm missing here? The error i'm getting is as follows:

Notice: The called constructor method for WP_Widget in thmlv_widget_team is deprecated since version 4.3.0! Use __construct() instead. in /wordpress/wp-includes/functions.php on line 4716

I don't know why it's referencing the functions.php file but i'm updated to the latest version of Wordpress (5.3.2).

This is the Wordpress Theme in use.

https://themeforest.net/item/tag-creative-agencyportfolio-fullscreen-theme/9294146

答: The error message is pretty clear: remove function thmlv_widget_team(). This is a constructor that was neccessary in PHP 4, but has been deprecated with the introduction of PHP 7

Body:
I'm working on moving a site from a PHP5 server to a server with PHP7. My client is using a theme from the company Themelovin which appears to have gone dark. They have a few helper plugins like a Portfolio plugin, Twitter plugin and Teams plugin that come from a private repo with the purchase of a theme. I tried to get the latest version to see if it's been fixed but it hasn't.

Their Portfolio and Teams plugins both used an old constructor that is now deprecated with PHP7.

The original code looked like so:

 class thmlv_widget_team extends WP_Widget{
   function thmlv_widget_team(){
     $widget_ops = array('classname' => 'team-items', 'description' => __('Display team members.', 'themelovin'));
     $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'thmlv_widget_team');
     $this->WP_Widget('thmlv_widget_team', __('Themelovin Team', 'themelovin'), $widget_ops, $control_ops);
   }

From what i've read, the issue here is the class "thmlv_widget_team" is the same name as the function. I am very poor at PHP but my understanding is that is the conflict. I read that you can fix this using __construct() and creating something that uses that plus a backup however that still isn't working. This is what I have now:

class thmlv_widget_team extends WP_Widget{
    function __construct(){
      $widget_ops = array('classname' => 'team-items', 'description' => __('Display team members.', 'themelovin'));
      $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'thmlv_widget_team');
      $this->WP_Widget('thmlv_widget_team', __('Themelovin Team', 'themelovin'), $widget_ops, $control_ops);
    }

    function thmlv_widget_team(){
      self::__construct();
    }

Ultimately I created the function __construct() with the code and then had a fallback for the original function to reference the construct I created.

Can you assist in what i'm missing here? The error i'm getting is as follows:

Notice: The called constructor method for WP_Widget in thmlv_widget_team is deprecated since version 4.3.0! Use __construct() instead. in /wordpress/wp-includes/functions.php on line 4716

I don't know why it's referencing the functions.php file but i'm updated to the latest version of Wordpress (5.3.2).

This is the Wordpress Theme in use.

https://themeforest.net/item/tag-creative-agencyportfolio-fullscreen-theme/9294146


问:
Can I display lists (<ol>, <ul>) as inline items?
I know they are block elements, but I'm sure there is a way, maybe with CSS? I tried to use the span tag but it doesn't work. What did I do wrong? I would like to put the second element next to the first one on the website. Not under it, but next.</p>

```<code>  <span>
        <section>
        <h3>Favourite quotes</h3>
         <ul>
          <li>
              “Pizza is good" ―Me
          </li>
         </ul>
        </section>
  </span>

  <span>
        <section>
        <h3>Favourite series</h3>
         <ul>
          <li> X </li>
          <li> Y </li>
          <li> Z </li>
         </ul>
        </section>
  </span>
</code>```

答:
Try something like this</p>

```<code>.displayList .ol .ul {
    visibility: visible;
}
</code>```

Body:
I know they are block elements, but I'm sure there is a way, maybe with CSS? I tried to use the span tag but it doesn't work. What did I do wrong? I would like to put the second element next to the first one on the website. Not under it, but next.

Favourite quotes

              “Pizza is good" ―Me

Favourite series

 X

 Y

 Z

Body:
Try something like this

.displayList .ol .ul {
    visibility: visible;
}


问:
C++ cannot open source file
In C++ with Visual studio 2017,

I copied some header files into my project folder, then added them under the "solution explorer" in c++. Now when I write

```<code>#include "name.h"
</code>```

it prints an error under include, and says "cannot open source file".

Why, and what can I potentially do to fix it?

I only just downloaded VS and am learning c++ for the first time.

答:
There is more information here on how to deal with this problem: Where does Visual Studio look for C++ header files?

For me, I followed xflowXen's answer and then at "Include Directories" typed in the specific pathname where my header file was located followed by a semicolon, something like:
C:\Users\name\source\repos\p2-A\p2-A;
then applied the changes and the issue went away.

Title: C++ cannot open source file

Body:
In C++ with Visual studio 2017,

I copied some header files into my project folder, then added them under the "solution explorer" in c++. Now when I write

#include "name.h"

it prints an error under include, and says "cannot open source file".

Why, and what can I potentially do to fix it?

I only just downloaded VS and am learning c++ for the first time.


问:
Dask - import dask_ml.cluster - ModuleNotFoundError: No module named 'sklearn.cluster._k_means'
I would like to use Dask to create a simple clustering model. I have tried the following import statements individually (in separate cells) in Jupyter Notebooks:

```<code>import dask_ml.cluster

from dask_ml.cluster import KMeans

from dask_ml.cluster import SpectralClustering
</code>```

Each of them separately resulting in the following error:

```<code>ModuleNotFoundError: No module named 'sklearn.cluster._k_means'
</code>```

This seems really strange. Has anyone experienced this?

答: I encourage you to update Dask-ML to the latest release</p>

Body:
I would like to use Dask to create a simple clustering model. I have tried the following import statements individually (in separate cells) in Jupyter Notebooks:

import dask_ml.cluster

from dask_ml.cluster import KMeans

from dask_ml.cluster import SpectralClustering

Each of them separately resulting in the following error:

ModuleNotFoundError: No module named 'sklearn.cluster._k_means'

This seems really strange. Has anyone experienced this?