问:
How to improve for-loop on DataFrame with row-wise calculation depending on previous row?
I have a working code with a for-loop that I would like to optimise in terms of speed (the project has been rejected because it's slow).

This is my situation: I have an Excel file from which I import a table with thousands of rows and a few columns into a Pandas DataFrame. The first column is a monotonic increasing series of timestamps, with 15 minutes frequency.

I have to use this table to calculate few extra columns, append them to the original table and save the resulting DataFrame into a new Excel file.

What is slowing the code is the core calculation of the extra columns. Here is a snippet of the working code:

```<code>import pandas as pd
from datetime import timedelta as td

cons_prod = pd.read_csv("sample.csv", index_col=0, parse_dates=True)

soc_dct = {}  # State of charge (kW)
charge_dct = {}  # Charge (kW)
discharge_dct = {}  # Discharge (kW)
acc_dct = {}  # Auto-consumption NEW (kW)
lst_dct = {}  # Lost injection due to battery efficiency (kW)
inj_dct = {}  # Injection NEW (kW)
gridcons_dct = {}  # Grid Consumption NEW (kW)
agg_dct = {}  # Additional Auto-consumption through battery (kW)

battery_parameters = {
    "power": 50,
    "energy": 130,
    "efficiency": 0.9,
    "minsoc": 0.1,
    "soct0": 65.0,
}

bp_energy = battery_parameters["energy"]
bp_power = battery_parameters["power"]
soct0 = 0.5 * bp_energy

for t in cons_prod.index:
    L = cons_prod.loc[t, "Injection (kW)"]
    m = cons_prod.loc[t, "Grid Consumption (kW)"]
    k = cons_prod.loc[t, "Auto-consumption (kW)"]
    f = cons_prod.loc[t, "Consumption (kW)"]
    if t == cons_prod.index[0]:
        # State of charge (kW)
        soc_dct.setdefault(t, soct0)

        # Charge (kW)
        charge_dct.setdefault(
            t,
            min(
                L,
                (bp_energy - soc_dct[t]) * 4,
                bp_power,
            )
            * battery_parameters["efficiency"]
            if L >= 0
            else 0,
        )

        # Discharge (kW)
        discharge_dct.setdefault(
            t,
            -min(
                m,
                4 * soc_dct[t] - 4 * (battery_parameters["minsoc"] * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0,
        )

        # Auto-consumption NEW (kW)
        acc_dct.setdefault(t, k - discharge_dct[t])

        # Lost injection due to battery efficiency (kW)
        lst_dct.setdefault(
            t,
            (charge_dct[t] / battery_parameters["efficiency"]) - charge_dct[t],
        )

        # Injection NEW (kW)
        inj_dct.setdefault(t, L - charge_dct[t] - lst_dct[t])

        # Grid Consumption NEW (kW)
        gridcons_dct.setdefault(t, f - acc_dct[t])

        # Additional Auto-consumption through battery (kW)
        agg_dct.setdefault(t, acc_dct[t] - k)
    else:
        # State of charge (kW)
        soc_dct.setdefault(
            t,
            soc_dct[t - td(minutes=15)]
            + (charge_dct[t - td(minutes=15)] + discharge_dct[t - td(minutes=15)]) / 4,
        )

        # Charge (kW)
        charge_dct.setdefault(
            t,
            min(
                L,
                (bp_energy - soc_dct[t]) * 4,
                bp_power,
            )
            * battery_parameters["efficiency"]
            if L >= 0
            else 0,
        )

        # Discharge (kW)
        discharge_dct.setdefault(
            t,
            -min(
                m,
                4 * soc_dct[t] - 4 * (battery_parameters["minsoc"] * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0,
        )

        # Auto-consumption NEW (kW)
        acc_dct.setdefault(t, k - discharge_dct[t])

        # Lost injection due to battery efficiency (kW)
        lst_dct.setdefault(
            t, charge_dct[t] / battery_parameters["efficiency"] - charge_dct[t]
        )

        # Injection NEW (kW)
        inj_dct.setdefault(t, L - charge_dct[t] - lst_dct[t])

        # Grid Consumption NEW (kW)
        gridcons_dct.setdefault(t, f - acc_dct[t])

        # Additional Auto-consumption through battery (kW)
        agg_dct.setdefault(t, acc_dct[t] - k)
# Creating a DataFrame with all the values
output_df = pd.DataFrame(
    data=[
        soc_dct,
        charge_dct,
        discharge_dct,
        acc_dct,
        lst_dct,
        inj_dct,
        gridcons_dct,
        agg_dct,
    ]
).T
output_df.columns = [
    "State of charge (kW)",
    "Charge (kW)",
    "Discharge (kW)",
    "Auto-consumption NEW (kW)",
    "Lost injection due to battery efficiency (kW)",
    "Injection NEW (kW)",
    "Grid Consumption NEW (kW)",
    "Additional Auto-consumption through battery (kW)",
]
charge_dct = {}  # Charge (kW)
discharge_dct = {}  # Discharge (kW)
acc_dct = {}  # Auto-consumption NEW (kW)
lst_dct = {}  # Lost injection due to battery efficiency (kW)
inj_dct = {}  # Injection NEW (kW)
gridcons_dct = {}  # Grid Consumption NEW (kW)
agg_dct = {}  # Additional Auto-consumption through battery (kW)

for t in cons_prod.index:
    L = cons_prod.loc[t, "Injection (kW)"]
    m = cons_prod.loc[t, "Grid Consumption (kW)"]
    k = cons_prod.loc[t, "Auto-consumption (kW)"]
    f = cons_prod.loc[t, "Consumption (kW)"]
    if t == cons_prod.index[0]:
        # State of charge (kW)
        soc_dct.setdefault(t, soct0)

        # Charge (kW)
        charge_dct.setdefault(
            t,
            min(
                L,
                (bp_energy - soc_dct[t]) * 4,
                bp_power,
            )
            * battery_parameters["efficiency"]
            if L >= 0
            else 0,
        )

        # Discharge (kW)
        discharge_dct.setdefault(
            t,
            -min(
                m,
                4 * soc_dct[t] - 4 * (battery_parameters["minsoc"] * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0,
        )

        # Auto-consumption NEW (kW)
        acc_dct.setdefault(t, k - discharge_dct[t])

        # Lost injection due to battery efficiency (kW)
        lst_dct.setdefault(
            t,
            (charge_dct[t] / battery_parameters["efficiency"]) - charge_dct[t],
        )

        # Injection NEW (kW)
        inj_dct.setdefault(t, L - charge_dct[t] - lst_dct[t])

        # Grid Consumption NEW (kW)
        gridcons_dct.setdefault(t, f - acc_dct[t])

        # Additional Auto-consumption through battery (kW)
        agg_dct.setdefault(t, acc_dct[t] - k)
    else:
        # State of charge (kW)
        soc_dct.setdefault(
            t,
            soc_dct[t - td(minutes=15)]
            + (charge_dct[t - td(minutes=15)] + discharge_dct[t - td(minutes=15)]) / 4,
        )

        # Charge (kW)
        charge_dct.setdefault(
            t,
            min(
                L,
                (bp_energy - soc_dct[t]) * 4,
                bp_power,
            )
            * battery_parameters["efficiency"]
            if L >= 0
            else 0,
        )

        # Discharge (kW)
        discharge_dct.setdefault(
            t,
            -min(
                m,
                4 * soc_dct[t] - 4 * (battery_parameters["minsoc"] * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0,
        )

        # Auto-consumption NEW (kW)
        acc_dct.setdefault(t, k - discharge_dct[t])

        # Lost injection due to battery efficiency (kW)
        lst_dct.setdefault(
            t, charge_dct[t] / battery_parameters["efficiency"] - charge_dct[t]
        )

        # Injection NEW (kW)
        inj_dct.setdefault(t, L - charge_dct[t] - lst_dct[t])

        # Grid Consumption NEW (kW)
        gridcons_dct.setdefault(t, f - acc_dct[t])

        # Additional Auto-consumption through battery (kW)
        agg_dct.setdefault(t, acc_dct[t] - k)
# Creating a DataFrame with all the values
output_df = pd.DataFrame(
    data=[
        soc_dct,
        charge_dct,
        discharge_dct,
        acc_dct,
        lst_dct,
        inj_dct,
        gridcons_dct,
        agg_dct,
    ]
).T
output_df.columns = [
    "State of charge (kW)",
    "Charge (kW)",
    "Discharge (kW)",
    "Auto-consumption NEW (kW)",
    "Lost injection due to battery efficiency (kW)",
    "Injection NEW (kW)",
    "Grid Consumption NEW (kW)",
    "Additional Auto-consumption through battery (kW)",
]

</code>```

cons_prod is the table imported into a DataFrame.

As you can see, we have two situations: when t == cons_prod.index[0] (i.e., the first item of the timestamps), the calculations use the values at the same t. However, from the second timestamp on, some of the calculations refer to the previous value (in here referred to the 15 minutes before using the index t - td(minutes=15)).

These are the reasons why I am struggling to move away from the for-loop.

A few explanations to possible questions

Q: Why are you using dictionaries? A: Because I figured that they fill faster than other data types, and I can use them to create a DataFrame later on.

Q: Are the timestamps coherent, e.g. do they have missing values? A: No missing values, as there is a previous function I wrote to make sure that the timestamps are filled completely.

Q: The calculations do not currently refer to a previous row in a DataFrame, but in a dictionary! Why the misleading title? A: This is the best solution I could come up with so far, but I am wondering whether my not-so-complete knowledge of Pandas is hiding a simpler, faster solution.

Hope the framework is clear.

Thank you in advance!

EDIT: as per request, added a 100-lines sample of cons_prod and modified the previous code to meet the requirements of a MRE.

EDIT 2: I have tried moving from dictionaries to a Pandas lookup, trying to optimise as much as possible. This is the code I came up with:

```<code>from time import time as tt

cp = cons_prod.copy(deep=True)

# Initialise the columns filling them with zeroes
cp["State of charge (kW)"] = 0
cp["Charge (kW)"] = 0
cp["Discharge (kW)"] = 0

# Storing the position of the columns in variables
cp_soc = cp.columns.get_loc("State of charge (kW)")
cp_charge = cp.columns.get_loc("Charge (kW)")
cp_discharge = cp.columns.get_loc("Discharge (kW)")
cp_inj = cp.columns.get_loc("Injection (kW)")
cp_gridcons = cp.columns.get_loc("Grid Consumption (kW)")

# Storing the values of the battery dictionary lookups in variables
bp_energy = dct_bp["energy"]
bp_power = dct_bp["power"]
bp_efficiency = dct_bp["efficiency"]
bp_soct0 = dct_bp["soct0"]
bp_minsoc = dct_bp["minsoc"]

start1 = tt()  # Measuring time
for row in cp.itertuples(name=None):  # Using itertuples to gain some speed
    L = cp.loc[row[0], "Injection (kW)"]
    m = cp.loc[row[0], "Grid Consumption (kW)"]
    k = cp.loc[row[0], "Auto-consumption (kW)"]
    f = cp.loc[row[0], "Consumption (kW)"]
    if row[0] == cp.index[0]:
        cp.iloc[0, cp_soc] = bp_soct0
        cp.iloc[0, cp_charge] = float(
            min(L, (bp_energy - bp_soct0) * 4, bp_power) * bp_efficiency
            if L >= 0
            else 0,
        )
        cp.iloc[0, cp_discharge] = float(
            -min(
                m,
                4 * bp_soct0 - 4 * (bp_minsoc * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0
        )
    else:
        t = pd.Index(cp.index).get_loc(row[0])
        cp.iloc[t, cp_soc] = float(
            cp.iloc[t - 1, cp_soc]
            + (cp.iloc[t - 1, cp_charge] + cp.iloc[t - 1, cp_discharge]) / 4
        )
        cp.iloc[t, cp_charge] = float(
            min(L, (bp_energy - cp.iloc[t, cp_soc]) * 4, bp_power) * bp_efficiency
            if L >= 0
            else 0,
        )
        cp.iloc[t, cp_discharge] = float(
            -min(
                m,
                4 * cp.iloc[t, cp_soc] - 4 * (dct_bp["minsoc"] * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0
        )
end1 = tt() - start1
print(f"Pandas lookup took {end1:.2f} seconds")

</code>```

With this code, I got an average of 42 seconds per complete task, while I used to have <20 seconds with dictionaries.

答:
Since I don't have a data set to test with and I'm not sure just how fast you need this to run I figured I'd start an answer with some optimisations and suggestions and add to it until it covers everything off.

You are using textual labels to identify columns inside a loop that runs over all of your data:

```<code>L = cp.loc[row[0], "Injection (kW)"]
m = cp.loc[row[0], "Grid Consumption (kW)"]
k = cp.loc[row[0], "Auto-consumption (kW)"]
f = cp.loc[row[0], "Consumption (kW)"]
</code>```

These labels are likely cached somewhere in a dictionary but you should be able to completely avoid this lookup using iloc instead of loc. Just identify the correct columns prior to starting the loop then use the integer indexes.

You have a dictionary of constants which you pull from inside your loop:

```<code>battery_parameters = {
    "power": 50,
    "energy": 130,
    "efficiency": 0.9,
    "minsoc": 0.1,
    "soct0": 65.0,
}
</code>```

Every time you use one of these values in your loop you do an extra dictionary lookup for no reason. why not bp_power=50 bp_energy=130?

You have an if statement in your loop to check for the first row that gets evaluated every loop. Just pull the first row's code out of the loop and do it before the loop and you can skip this evaluation for all of the other rows.

I don't really see why you're using dictionaries and setdefault for the additional rows you're calculating. Unless I'm missing something you should be using a list as all of your keys are just integers anyway and setdefault checks for previous values that won't exist in the dictionary which wastes time. If you do something like:

```<code>soc_dct = [0] * num_rows
</code>```

You'll have a preallocated list of 0s. The preallocation will save time but not much the big win is much faster lookups and insertions and no need to append to the lists as all the indexes will already exist. So instead of soc_dct.setdefault(index,newValue) you just use soc_dct[index] = newValue

I have a feeling this will get you to a level of performance you're happy with. It seems like you're just looping over data and doing simple math. Simple math is very slow in python but probably not a problem here. If you still want more speed you could use Cython to compile this module into a C module just using the cythonize command and if that isn't enough you'd likely have to go in and start doing type annotations.

Body:
I have a working code with a for-loop that I would like to optimise in terms of speed (the project has been rejected because it's slow).

This is my situation: I have an Excel file from which I import a table with thousands of rows and a few columns into a Pandas DataFrame. The first column is a monotonic increasing series of timestamps, with 15 minutes frequency.

I have to use this table to calculate few extra columns, append them to the original table and save the resulting DataFrame into a new Excel file.

What is slowing the code is the core calculation of the extra columns. Here is a snippet of the working code:

import pandas as pd
from datetime import timedelta as td

cons_prod = pd.read_csv("sample.csv", index_col=0, parse_dates=True)

soc_dct = {}  # State of charge (kW)
charge_dct = {}  # Charge (kW)
discharge_dct = {}  # Discharge (kW)
acc_dct = {}  # Auto-consumption NEW (kW)
lst_dct = {}  # Lost injection due to battery efficiency (kW)
inj_dct = {}  # Injection NEW (kW)
gridcons_dct = {}  # Grid Consumption NEW (kW)
agg_dct = {}  # Additional Auto-consumption through battery (kW)

battery_parameters = {
    "power": 50,
    "energy": 130,
    "efficiency": 0.9,
    "minsoc": 0.1,
    "soct0": 65.0,
}

bp_energy = battery_parameters["energy"]
bp_power = battery_parameters["power"]
soct0 = 0.5 * bp_energy

for t in cons_prod.index:
    L = cons_prod.loc[t, "Injection (kW)"]
    m = cons_prod.loc[t, "Grid Consumption (kW)"]
    k = cons_prod.loc[t, "Auto-consumption (kW)"]
    f = cons_prod.loc[t, "Consumption (kW)"]
    if t == cons_prod.index[0]:
        # State of charge (kW)
        soc_dct.setdefault(t, soct0)

        # Charge (kW)
        charge_dct.setdefault(
            t,
            min(
                L,
                (bp_energy - soc_dct[t]) * 4,
                bp_power,
            )
            * battery_parameters["efficiency"]
            if L >= 0
            else 0,
        )

        # Discharge (kW)
        discharge_dct.setdefault(
            t,
            -min(
                m,
                4 * soc_dct[t] - 4 * (battery_parameters["minsoc"] * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0,
        )

        # Auto-consumption NEW (kW)
        acc_dct.setdefault(t, k - discharge_dct[t])

        # Lost injection due to battery efficiency (kW)
        lst_dct.setdefault(
            t,
            (charge_dct[t] / battery_parameters["efficiency"]) - charge_dct[t],
        )

        # Injection NEW (kW)
        inj_dct.setdefault(t, L - charge_dct[t] - lst_dct[t])

        # Grid Consumption NEW (kW)
        gridcons_dct.setdefault(t, f - acc_dct[t])

        # Additional Auto-consumption through battery (kW)
        agg_dct.setdefault(t, acc_dct[t] - k)
    else:
        # State of charge (kW)
        soc_dct.setdefault(
            t,
            soc_dct[t - td(minutes=15)]
            + (charge_dct[t - td(minutes=15)] + discharge_dct[t - td(minutes=15)]) / 4,
        )

        # Charge (kW)
        charge_dct.setdefault(
            t,
            min(
                L,
                (bp_energy - soc_dct[t]) * 4,
                bp_power,
            )
            * battery_parameters["efficiency"]
            if L >= 0
            else 0,
        )

        # Discharge (kW)
        discharge_dct.setdefault(
            t,
            -min(
                m,
                4 * soc_dct[t] - 4 * (battery_parameters["minsoc"] * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0,
        )

        # Auto-consumption NEW (kW)
        acc_dct.setdefault(t, k - discharge_dct[t])

        # Lost injection due to battery efficiency (kW)
        lst_dct.setdefault(
            t, charge_dct[t] / battery_parameters["efficiency"] - charge_dct[t]
        )

        # Injection NEW (kW)
        inj_dct.setdefault(t, L - charge_dct[t] - lst_dct[t])

        # Grid Consumption NEW (kW)
        gridcons_dct.setdefault(t, f - acc_dct[t])

        # Additional Auto-consumption through battery (kW)
        agg_dct.setdefault(t, acc_dct[t] - k)
# Creating a DataFrame with all the values
output_df = pd.DataFrame(
    data=[
        soc_dct,
        charge_dct,
        discharge_dct,
        acc_dct,
        lst_dct,
        inj_dct,
        gridcons_dct,
        agg_dct,
    ]
).T
output_df.columns = [
    "State of charge (kW)",
    "Charge (kW)",
    "Discharge (kW)",
    "Auto-consumption NEW (kW)",
    "Lost injection due to battery efficiency (kW)",
    "Injection NEW (kW)",
    "Grid Consumption NEW (kW)",
    "Additional Auto-consumption through battery (kW)",
]
charge_dct = {}  # Charge (kW)
discharge_dct = {}  # Discharge (kW)
acc_dct = {}  # Auto-consumption NEW (kW)
lst_dct = {}  # Lost injection due to battery efficiency (kW)
inj_dct = {}  # Injection NEW (kW)
gridcons_dct = {}  # Grid Consumption NEW (kW)
agg_dct = {}  # Additional Auto-consumption through battery (kW)

for t in cons_prod.index:
    L = cons_prod.loc[t, "Injection (kW)"]
    m = cons_prod.loc[t, "Grid Consumption (kW)"]
    k = cons_prod.loc[t, "Auto-consumption (kW)"]
    f = cons_prod.loc[t, "Consumption (kW)"]
    if t == cons_prod.index[0]:
        # State of charge (kW)
        soc_dct.setdefault(t, soct0)

        # Charge (kW)
        charge_dct.setdefault(
            t,
            min(
                L,
                (bp_energy - soc_dct[t]) * 4,
                bp_power,
            )
            * battery_parameters["efficiency"]
            if L >= 0
            else 0,
        )

        # Discharge (kW)
        discharge_dct.setdefault(
            t,
            -min(
                m,
                4 * soc_dct[t] - 4 * (battery_parameters["minsoc"] * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0,
        )

        # Auto-consumption NEW (kW)
        acc_dct.setdefault(t, k - discharge_dct[t])

        # Lost injection due to battery efficiency (kW)
        lst_dct.setdefault(
            t,
            (charge_dct[t] / battery_parameters["efficiency"]) - charge_dct[t],
        )

        # Injection NEW (kW)
        inj_dct.setdefault(t, L - charge_dct[t] - lst_dct[t])

        # Grid Consumption NEW (kW)
        gridcons_dct.setdefault(t, f - acc_dct[t])

        # Additional Auto-consumption through battery (kW)
        agg_dct.setdefault(t, acc_dct[t] - k)
    else:
        # State of charge (kW)
        soc_dct.setdefault(
            t,
            soc_dct[t - td(minutes=15)]
            + (charge_dct[t - td(minutes=15)] + discharge_dct[t - td(minutes=15)]) / 4,
        )

        # Charge (kW)
        charge_dct.setdefault(
            t,
            min(
                L,
                (bp_energy - soc_dct[t]) * 4,
                bp_power,
            )
            * battery_parameters["efficiency"]
            if L >= 0
            else 0,
        )

        # Discharge (kW)
        discharge_dct.setdefault(
            t,
            -min(
                m,
                4 * soc_dct[t] - 4 * (battery_parameters["minsoc"] * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0,
        )

        # Auto-consumption NEW (kW)
        acc_dct.setdefault(t, k - discharge_dct[t])

        # Lost injection due to battery efficiency (kW)
        lst_dct.setdefault(
            t, charge_dct[t] / battery_parameters["efficiency"] - charge_dct[t]
        )

        # Injection NEW (kW)
        inj_dct.setdefault(t, L - charge_dct[t] - lst_dct[t])

        # Grid Consumption NEW (kW)
        gridcons_dct.setdefault(t, f - acc_dct[t])

        # Additional Auto-consumption through battery (kW)
        agg_dct.setdefault(t, acc_dct[t] - k)
# Creating a DataFrame with all the values
output_df = pd.DataFrame(
    data=[
        soc_dct,
        charge_dct,
        discharge_dct,
        acc_dct,
        lst_dct,
        inj_dct,
        gridcons_dct,
        agg_dct,
    ]
).T
output_df.columns = [
    "State of charge (kW)",
    "Charge (kW)",
    "Discharge (kW)",
    "Auto-consumption NEW (kW)",
    "Lost injection due to battery efficiency (kW)",
    "Injection NEW (kW)",
    "Grid Consumption NEW (kW)",
    "Additional Auto-consumption through battery (kW)",
]

cons_prod is the table imported into a DataFrame.

As you can see, we have two situations: when t == cons_prod.index[0] (i.e., the first item of the timestamps), the calculations use the values at the same t. However, from the second timestamp on, some of the calculations refer to the previous value (in here referred to the 15 minutes before using the index t - td(minutes=15)).

These are the reasons why I am struggling to move away from the for-loop.

A few explanations to possible questions

Q: Why are you using dictionaries? A: Because I figured that they fill faster than other data types, and I can use them to create a DataFrame later on.

Q: Are the timestamps coherent, e.g. do they have missing values? A: No missing values, as there is a previous function I wrote to make sure that the timestamps are filled completely.

Q: The calculations do not currently refer to a previous row in a DataFrame, but in a dictionary! Why the misleading title? A: This is the best solution I could come up with so far, but I am wondering whether my not-so-complete knowledge of Pandas is hiding a simpler, faster solution.

Hope the framework is clear.

Thank you in advance!

EDIT: as per request, added a 100-lines sample of cons_prod and modified the previous code to meet the requirements of a MRE.

EDIT 2: I have tried moving from dictionaries to a Pandas lookup, trying to optimise as much as possible. This is the code I came up with:

from time import time as tt

cp = cons_prod.copy(deep=True)

# Initialise the columns filling them with zeroes
cp["State of charge (kW)"] = 0
cp["Charge (kW)"] = 0
cp["Discharge (kW)"] = 0

# Storing the position of the columns in variables
cp_soc = cp.columns.get_loc("State of charge (kW)")
cp_charge = cp.columns.get_loc("Charge (kW)")
cp_discharge = cp.columns.get_loc("Discharge (kW)")
cp_inj = cp.columns.get_loc("Injection (kW)")
cp_gridcons = cp.columns.get_loc("Grid Consumption (kW)")

# Storing the values of the battery dictionary lookups in variables
bp_energy = dct_bp["energy"]
bp_power = dct_bp["power"]
bp_efficiency = dct_bp["efficiency"]
bp_soct0 = dct_bp["soct0"]
bp_minsoc = dct_bp["minsoc"]

start1 = tt()  # Measuring time
for row in cp.itertuples(name=None):  # Using itertuples to gain some speed
    L = cp.loc[row[0], "Injection (kW)"]
    m = cp.loc[row[0], "Grid Consumption (kW)"]
    k = cp.loc[row[0], "Auto-consumption (kW)"]
    f = cp.loc[row[0], "Consumption (kW)"]
    if row[0] == cp.index[0]:
        cp.iloc[0, cp_soc] = bp_soct0
        cp.iloc[0, cp_charge] = float(
            min(L, (bp_energy - bp_soct0) * 4, bp_power) * bp_efficiency
            if L >= 0
            else 0,
        )
        cp.iloc[0, cp_discharge] = float(
            -min(
                m,
                4 * bp_soct0 - 4 * (bp_minsoc * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0
        )
    else:
        t = pd.Index(cp.index).get_loc(row[0])
        cp.iloc[t, cp_soc] = float(
            cp.iloc[t - 1, cp_soc]
            + (cp.iloc[t - 1, cp_charge] + cp.iloc[t - 1, cp_discharge]) / 4
        )
        cp.iloc[t, cp_charge] = float(
            min(L, (bp_energy - cp.iloc[t, cp_soc]) * 4, bp_power) * bp_efficiency
            if L >= 0
            else 0,
        )
        cp.iloc[t, cp_discharge] = float(
            -min(
                m,
                4 * cp.iloc[t, cp_soc] - 4 * (dct_bp["minsoc"] * bp_energy),
                bp_power,
            )
            if m >= 0
            else 0
        )
end1 = tt() - start1
print(f"Pandas lookup took {end1:.2f} seconds")

With this code, I got an average of 42 seconds per complete task, while I used to have <20 seconds with dictionaries.

Body:
Since I don't have a data set to test with and I'm not sure just how fast you need this to run I figured I'd start an answer with some optimisations and suggestions and add to it until it covers everything off.

You are using textual labels to identify columns inside a loop that runs over all of your data:

L = cp.loc[row[0], "Injection (kW)"]
m = cp.loc[row[0], "Grid Consumption (kW)"]
k = cp.loc[row[0], "Auto-consumption (kW)"]
f = cp.loc[row[0], "Consumption (kW)"]

These labels are likely cached somewhere in a dictionary but you should be able to completely avoid this lookup using iloc instead of loc. Just identify the correct columns prior to starting the loop then use the integer indexes.

You have a dictionary of constants which you pull from inside your loop:

battery_parameters = {
    "power": 50,
    "energy": 130,
    "efficiency": 0.9,
    "minsoc": 0.1,
    "soct0": 65.0,
}

Every time you use one of these values in your loop you do an extra dictionary lookup for no reason. why not bp_power=50 bp_energy=130?

You have an if statement in your loop to check for the first row that gets evaluated every loop. Just pull the first row's code out of the loop and do it before the loop and you can skip this evaluation for all of the other rows.

I don't really see why you're using dictionaries and setdefault for the additional rows you're calculating. Unless I'm missing something you should be using a list as all of your keys are just integers anyway and setdefault checks for previous values that won't exist in the dictionary which wastes time. If you do something like:

soc_dct = [0] * num_rows

You'll have a preallocated list of 0s. The preallocation will save time but not much the big win is much faster lookups and insertions and no need to append to the lists as all the indexes will already exist. So instead of soc_dct.setdefault(index,newValue) you just use soc_dct[index] = newValue

I have a feeling this will get you to a level of performance you're happy with. It seems like you're just looping over data and doing simple math. Simple math is very slow in python but probably not a problem here. If you still want more speed you could use Cython to compile this module into a C module just using the cythonize command and if that isn't enough you'd likely have to go in and start doing type annotations.


来源: stackexchange

问:
Android ScrollView, scroll before visible
how can I set scroll of scrollview to x pixels, before it's even shown?

In ScrollView I have some Views and i know that it will fill screen. Can I scroll to let say second View, so that first View is not visible when activity is started?

Now I have sth like this, but I think it's not perfect, sometimes, I see first View, and then it's scrolled to the second one

```<code>@Override
public void onGlobalLayout() {
    if (mHorizontalScrollView.getChildCount() > 0 && mHorizontalScrollView.getChildAt(0).getWidth() > mScreenWidth) {
            hsv.scrollTo(100, 0);
    }
}
</code>```

EDIT!!

Second attempt was to use http://developer.android.com/reference/android/view/ViewTreeObserver.OnPreDrawListener.html instead of http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html
In OnPreDrawListener we can read that At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs. so basically at this point we should adjust scrolling. So I created:

```<code>@Override
public boolean onPreDraw() {
    if (hsv.getChildCount() > 0 && hsv.getChildAt(0).getWidth() > mScreenWidth) {
        hsv.scrollTo(100, 0);
        return false;
    }
    return true;
}
</code>```

but now it's never working for me.

答: try View.post(Runnable) in onCreate-method.</p>

ContentLicense: CC BY-SA 3.0

Body:
how can I set scroll of scrollview to x pixels, before it's even shown?

In ScrollView I have some Views and i know that it will fill screen. Can I scroll to let say second View, so that first View is not visible when activity is started?

Now I have sth like this, but I think it's not perfect, sometimes, I see first View, and then it's scrolled to the second one

@Override
public void onGlobalLayout() {
    if (mHorizontalScrollView.getChildCount() > 0 && mHorizontalScrollView.getChildAt(0).getWidth() > mScreenWidth) {
            hsv.scrollTo(100, 0);
    }
}

EDIT!!

Second attempt was to use http://developer.android.com/reference/android/view/ViewTreeObserver.OnPreDrawListener.html instead of http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html
In OnPreDrawListener we can read that At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs. so basically at this point we should adjust scrolling. So I created:

@Override
public boolean onPreDraw() {
    if (hsv.getChildCount() > 0 && hsv.getChildAt(0).getWidth() > mScreenWidth) {
        hsv.scrollTo(100, 0);
        return false;
    }
    return true;
}

but now it's never working for me.


问:
How can I wrap text wrapping on a jquery datatable?
I'm trying to display some long text with help of a datatable, but somehow it is not beeing wraped. This means that instead of getting multiple rows of my text, I just get a long line. When I go to my DB and edit my Description field (Field type is text), I can see the text in multiple rows.

I already tried fixing the width of the table to 100%, but then the text line just keeps going outside the table.  I also tried adding word-wrap and flex-wrap, and defining a text-wrap class on my CSS, but it also didn't work.

Any idea how can I solve this?

```<code>  <div style="font-size:18px;">
    <table class="table-striped table-hover" id="product_table" cellspacing="0" width="100%">
      <thead>
        <tr>
          <th>Name</th>
          <th>Group</th>
          <th>Price</th>
          <th>Action</th>
          <th class="none">Description DE</th>
          <th class="none">Description EN</th>
          <th class="none">Description ES</th>
        </tr>
      </thead>
      <tbody>
        @foreach($ProductList as $product)
        <tr>
          <td>{{$product['ProductName']}}</td>
          <td>{{$product['ProductGroupName']}}</td>
          <td>{{$product['ProductListPrice']}}</td>
          <td ><button class="btn btn-primary waves-effect w-md waves-light w-sm-5" data-toggle="modal" data-target="#Edit" data-id="{{$product['ProductID']}}" data-name="{{$product['ProductName']}}" data-group="{{$product['ProductGroupName']}}"
              data-price="{{$product['ProductListPrice']}}" data-descde="{{$product['ProductDescriptionDE']}}" data-descen="{{$product['ProductDescriptionEN']}}" data-desces="{{$product['ProductDescriptionES']}}">Edit</button>
          <td>{{$product['ProductDescriptionDE']}}</td>
          <td>{{$product['ProductDescriptionEN']}}</td>
          <td>{{$product['ProductDescriptionES']}}</td>
          </td>
        </tr>
        @endforeach
      </tbody>
    </table>
  </div>

  <script type="text/javascript">
    $(document).ready(function() {
      $('#product_table').DataTable({
        columnDefs: [{
          "className": "dt-center",
          "targets": "_all"
        }],
        pageLength: 50,
        responsive: true
      });
    });
  </script>
</code>```

答:
Here is my solution, it works on all kinds of table:</p>
```<code>table.dataTable tbody td {
  word-break: break-word; word-break: break-all; white-space: normal;
}
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I'm trying to display some long text with help of a datatable, but somehow it is not beeing wraped. This means that instead of getting multiple rows of my text, I just get a long line. When I go to my DB and edit my Description field (Field type is text), I can see the text in multiple rows.

I already tried fixing the width of the table to 100%, but then the text line just keeps going outside the table.  I also tried adding word-wrap and flex-wrap, and defining a text-wrap class on my CSS, but it also didn't work.

Any idea how can I solve this?

          Name
          Group
          Price
          Action
          Description DE
          Description EN
          Description ES

        @foreach($ProductList as $product)

          {{$product['ProductName']}}
          {{$product['ProductGroupName']}}
          {{$product['ProductListPrice']}}
          <button class="btn btn-primary waves-effect w-md waves-light w-sm-5" data-toggle="modal" data-target="#Edit" data-id="{{$product['ProductID']}}" data-name="{{$product['ProductName']}}" data-group="{{$product['ProductGroupName']}}"
              data-price="{{$product['ProductListPrice']}}" data-descde="{{$product['ProductDescriptionDE']}}" data-descen="{{$product['ProductDescriptionEN']}}" data-desces="{{$product['ProductDescriptionES']}}">Edit</button>
          {{$product['ProductDescriptionDE']}}
          {{$product['ProductDescriptionEN']}}
          {{$product['ProductDescriptionES']}}

        @endforeach

Body:
Here is my solution, it works on all kinds of table:

table.dataTable tbody td {
  word-break: break-word; word-break: break-all; white-space: normal;
}


问:
Python: How do I convert date to UK format when automating Excel
I am automating my Excel (2016) workbook using Python (3.7.1). A cut down snippet of my code is below. The code prompts a user to input a date in UK format (dd/mm/yyyy). The problem is that when it updates the spreadsheet in Cell B8, it does so in US format (mm/dd/yyyy).

e.g. I enter 12/01/2022 (12 January 2022) and I get 01/12/2022 (1 December 2022).
The format of cell B8 is 'Date'.
If I try: type(uk_date_format) I get <class 'str'>
I am using a company machine and my regional setting are set to UK.
I am running my code via a cmd window on a Windows 10 machine.

Is there some way I can force it to a UK format? Any assistance would be great.

```<code>import os.path
import win32com.client

uk_date_format = input("Enter date in UK format e.g. dd/mm/yyyy:")
#I enter 12/01/2022 (12 January 2022)

file_path_and_name = "C:\\temp\\exampleFile.xlsb"
if os.path.exists(file_path_and_name):
    xlApp = win32com.client.DispatchEx('Excel.Application')
    xlsPath = os.path.expanduser(file_path_and_name)
    wb = xlApp.Workbooks.Open(Filename=xlsPath)
    ws = wb.Worksheets("Sheet1")
    ws.Range("B8").value = uk_date_format
    wb.Save()
    wb.Close(False)
    wb = None
    xlApp.Quit()
    xlApp = None
    print("All done")
</code>```

This is what I get in Excel:

答:
I guess since you are inserting a raw string input rather a datetime object into your excel it shows up in the default (m/d/Y) format. You shall try this

```<code>import datetime.datetime
uk_date_format = input("Enter date in UK format e.g. dd/mm/yyyy:")
day, month,year = map(int, uk_date_format.split('/'))
ukdate = datetime.datetime(year,month,day)
ukdate=ukdate.strftime("%d-%m-%Y")
print(ukdate)
</code>```

and replace

```<code>ws.Range("B8").value = uk_date_format
</code>```

with

```<code>ws.Range("B8").value = ukdate
</code>```

Hopefully that works

Body:
I am automating my Excel (2016) workbook using Python (3.7.1). A cut down snippet of my code is below. The code prompts a user to input a date in UK format (dd/mm/yyyy). The problem is that when it updates the spreadsheet in Cell B8, it does so in US format (mm/dd/yyyy).

e.g. I enter 12/01/2022 (12 January 2022) and I get 01/12/2022 (1 December 2022).
The format of cell B8 is 'Date'.
If I try: type(uk_date_format) I get <class 'str'>
I am using a company machine and my regional setting are set to UK.
I am running my code via a cmd window on a Windows 10 machine.

Is there some way I can force it to a UK format? Any assistance would be great.

import os.path
import win32com.client

uk_date_format = input("Enter date in UK format e.g. dd/mm/yyyy:")
#I enter 12/01/2022 (12 January 2022)

file_path_and_name = "C:\\temp\\exampleFile.xlsb"
if os.path.exists(file_path_and_name):
    xlApp = win32com.client.DispatchEx('Excel.Application')
    xlsPath = os.path.expanduser(file_path_and_name)
    wb = xlApp.Workbooks.Open(Filename=xlsPath)
    ws = wb.Worksheets("Sheet1")
    ws.Range("B8").value = uk_date_format
    wb.Save()
    wb.Close(False)
    wb = None
    xlApp.Quit()
    xlApp = None
    print("All done")

This is what I get in Excel:

Body:
I guess since you are inserting a raw string input rather a datetime object into your excel it shows up in the default (m/d/Y) format. You shall try this

import datetime.datetime
uk_date_format = input("Enter date in UK format e.g. dd/mm/yyyy:")
day, month,year = map(int, uk_date_format.split('/'))
ukdate = datetime.datetime(year,month,day)
ukdate=ukdate.strftime("%d-%m-%Y")
print(ukdate)

and replace

ws.Range("B8").value = uk_date_format

with

ws.Range("B8").value = ukdate

Hopefully that works


问:
Integer division in sql server
In Microsoft SQL Server 2005, why do the following commands produce integer results?</p>

```<code>SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
</code>```

答:
The CAST statement is a bit verbose. You can use the following instead:

```<code>DECLARE @TO_FLOAT FLOAT = 1.0;
SELECT (1 * @TO_FLOAT) / 2;
</code>```

Or use a different multiplier type like DECIMAL if you prefer.

ContentLicense: CC BY-SA 2.5

Title: Integer division in sql server

Body:
In Microsoft SQL Server 2005, why do the following commands produce integer results?

SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6

Body:
The CAST statement is a bit verbose. You can use the following instead:

DECLARE @TO_FLOAT FLOAT = 1.0;
SELECT (1 * @TO_FLOAT) / 2;

Or use a different multiplier type like DECIMAL if you prefer.


问:
TypeError: Invalid comparison between dtype=datetime64[ns, UTC] and datetime64
Im trying to specifiy the timeframe in which I want to evaluate the csv data.

I thought converting the date I entered to datetime64 using numpy.datetime64 would solve the issue, but it seems datetime64[ns, UTC] is another format.

This is what I tried, and im getting the above error: Invalid comparison between dtype=datetime64[ns, UTC] and datetime64.

How can I convert the dtype to datetime64[ns UTC] or the other way around?

This is the code:

df = pd.read_csv('/Users/youruser/Desktop/0b13c0f751243fda1229b7601db2c51c.csv', names=headers)

df['dt_iso'] = df['dt_iso'].map(lambda x: datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S %z %Z'))

start_date = numpy.datetime64('2011-05-01T01:00:00.000000+0100')
end_date = numpy.datetime64('2012-05-01T01:00:00.000000+0100')
mask = (df['dt_iso'] > start_date) & (df['dt_iso'] <= end_date)

x = mask['dt_iso']
y = df['temp']
```
<p>Thanks in advance!</p>
<p>Bonus question:</p>
<pre class="lang-py prettyprint-override"><code>df['dt_iso'] = df['dt_iso'].map(lambda x: datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S %z %Z'))
</code>```

What exactly does this line of code do? As far as I understand, it takes the Date information from the csv, makes a dtype object from it, then maps it to the dt_iso values. So is the unit of a datetime object datetime64[ns UTC]?

答:
Consider pandas' datetime64[ns UTC] to be an extension of numpy's datetime (datetime64[...]), that additionally allows to handle time zones. If one class has a time zone and the other does not, direct comparison is not possible. Even if you use pandas datetime consistently, either both datetime Series have to have a tz defined (be "tz-aware") or both have no tz defined ("tz-naive") - yes, UTC counts as a time zone in this context.

If you work with pandas and your input date/time for example represents UTC, you can .tz_localize to UTC, so that both Series in comparison are tz-aware.

In your example, you could use aware pd.Timestamps;

```<code>df['dt_iso'] = pd.to_datetime(df['dt_iso']) # simplify, does not require Py datetime

# use pd.Timestamps for comparisons
start_date = pd.Timestamp('2011-05-01T01:00:00.000000+0100')
end_date = pd.Timestamp('2012-05-01T01:00:00.000000+0100')

mask = (df['dt_iso'] > start_date) & (df['dt_iso'] <= end_date)
</code>```

Body:
Im trying to specifiy the timeframe in which I want to evaluate the csv data.

I thought converting the date I entered to datetime64 using numpy.datetime64 would solve the issue, but it seems datetime64[ns, UTC] is another format.

This is what I tried, and im getting the above error: Invalid comparison between dtype=datetime64[ns, UTC] and datetime64.

How can I convert the dtype to datetime64[ns UTC] or the other way around?

This is the code:

df = pd.read_csv('/Users/youruser/Desktop/0b13c0f751243fda1229b7601db2c51c.csv', names=headers)

df['dt_iso'] = df['dt_iso'].map(lambda x: datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S %z %Z'))

start_date = numpy.datetime64('2011-05-01T01:00:00.000000+0100')
end_date = numpy.datetime64('2012-05-01T01:00:00.000000+0100')
mask = (df['dt_iso'] > start_date) & (df['dt_iso'] <= end_date)

x = mask['dt_iso']
y = df['temp']

Thanks in advance!

Bonus question:

df['dt_iso'] = df['dt_iso'].map(lambda x: datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S %z %Z'))

What exactly does this line of code do? As far as I understand, it takes the Date information from the csv, makes a dtype object from it, then maps it to the dt_iso values. So is the unit of a datetime object datetime64[ns UTC]?

Body:
Consider pandas' datetime64[ns UTC] to be an extension of numpy's datetime (datetime64[...]), that additionally allows to handle time zones. If one class has a time zone and the other does not, direct comparison is not possible. Even if you use pandas datetime consistently, either both datetime Series have to have a tz defined (be "tz-aware") or both have no tz defined ("tz-naive") - yes, UTC counts as a time zone in this context.

If you work with pandas and your input date/time for example represents UTC, you can .tz_localize to UTC, so that both Series in comparison are tz-aware.

In your example, you could use aware pd.Timestamps;

df['dt_iso'] = pd.to_datetime(df['dt_iso']) # simplify, does not require Py datetime

# use pd.Timestamps for comparisons
start_date = pd.Timestamp('2011-05-01T01:00:00.000000+0100')
end_date = pd.Timestamp('2012-05-01T01:00:00.000000+0100')

mask = (df['dt_iso'] > start_date) & (df['dt_iso'] <= end_date)


问:
returning just a key from a hash field
I have an ActivityPart model that has a field called activity_json. this field is a hash and has a key named "image". How do I select all "image" from the activity_json field? I did something like this but it didn't work:

```<code>x = ActivityPart.all.pluck(activity_json: ['image'])
</code>```

my idea is to return all 'image' from the activiy_json field

答:
Try something like

ActivityPart.select("activity_json->'image' as image").map(&:image)

Title: returning just a key from a hash field

Body:
I have an ActivityPart model that has a field called activity_json. this field is a hash and has a key named "image". How do I select all "image" from the activity_json field? I did something like this but it didn't work:

x = ActivityPart.all.pluck(activity_json: ['image'])

my idea is to return all 'image' from the activiy_json field


问:
Core Data With NSData Attribute
I am working on a Core Data  based iOS application , in which i stored an array strings in coredata as NSData(binary data).
While  fetching  the data , how can i write  a NSPredicate which will only retrieve the result with  NSData value contain given string .

if name attribute is a string then i can write a predicate like below.

```<code>NSString *inputName = @"bbb";
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name = %@",inputName];
</code>```

my question is

what will be the predicate if name is NSData and this NSData from

```<code>NSArray *anArray = [NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",nil];
NSData *arrayData = [NSArchiver archivedDataWithRootObject:anArray];
</code>```

arrayData is stored as name attribute in Core Data .

I hope this information is enough to understand  my problem.
Thanks in advance.

答:
If I understand your question and subsequent comments correctly, and you are using an array stored as data to represent a hierarchical relationship then you are clearly not using Core Data as designed.

Instead, I recommend you create a separate entity to represent the employees and then set up a relationship between them and the supervisor?

Although it's not iOS specific, this tutorial by Apple demonstrates that very concept.

Title: Core Data With NSData Attribute

Body:
I am working on a Core Data  based iOS application , in which i stored an array strings in coredata as NSData(binary data).
While  fetching  the data , how can i write  a NSPredicate which will only retrieve the result with  NSData value contain given string .

if name attribute is a string then i can write a predicate like below.

NSString *inputName = @"bbb";
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name = %@",inputName];

my question is

what will be the predicate if name is NSData and this NSData from

NSArray *anArray = [NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",nil];
NSData *arrayData = [NSArchiver archivedDataWithRootObject:anArray];

arrayData is stored as name attribute in Core Data .

I hope this information is enough to understand  my problem.
Thanks in advance.


问:
How to pass command-line arguments when calling source() on an R file within another R file
In one R file, I plan to source another R file that supports reading two command-line arguments. This sounds like a trivial task but I couldn't find a solution online. Any help is appreciated.</p>

答:
I've tested some of the alternatives here and I end up with this:

File calling the source:

```<code>source_with_args <- function(file, ...){
  system(paste("Rscript", file, ...))
}
source_with_args(
  script_file,
  paste0("--data=", data_processed_file),
  paste0("--output=", output_file)
)
</code>```

File to be sourced:

```<code>  library(optparse)
  option_list = list(
    make_option(
      c("--data"),
      type="character",
      default=NULL,
      help="input file",
      metavar="filename"
    ),
    make_option(
      c("--output"),
      type="character",
      default=NULL,
      help="output file [default= %default]",
      metavar="filename"
    )
  )
  opt_parser = OptionParser(option_list=option_list)
  data_processed_file <- opt$data
  oputput_file <- opt$output
  if(is.null(data_processed_file) || is.null(oputput_file)){
    stop("Required information is missing")
  }
</code>```

Body:
I've tested some of the alternatives here and I end up with this:

File calling the source:

source_with_args <- function(file, ...){
  system(paste("Rscript", file, ...))
}
source_with_args(
  script_file,
  paste0("--data=", data_processed_file),
  paste0("--output=", output_file)
)

File to be sourced:

  library(optparse)
  option_list = list(
    make_option(
      c("--data"),
      type="character",
      default=NULL,
      help="input file",
      metavar="filename"
    ),
    make_option(
      c("--output"),
      type="character",
      default=NULL,
      help="output file [default= %default]",
      metavar="filename"
    )
  )
  opt_parser = OptionParser(option_list=option_list)
  data_processed_file <- opt$data
  oputput_file <- opt$output
  if(is.null(data_processed_file) || is.null(oputput_file)){
    stop("Required information is missing")
  }


问:
How to know deep inside component hierarchy we have a special component, efficiently?
I'm creating a form builder using React.

The point is, everything is dynamic.

A form can have fields, or field groups. Each field group can have many fields. Each field can have it's own controls, hints, valdiations, etc.

This means that a form is a component hierarchy. A tree of components.

Now I want to know if in this tree, any Upload field exist at all.

Because if there is no Upload field, then I submit the form using application/json content type. Otherwise I would submit the form using application/form-data content type.

I know I can traverse the entire JSON of component props. But that seems very inefficient.

Is there a way to traverse React's component tree in an efficient way?

答:
You can do so by using contextApi or with its hook for React Version > 16.8 the useContext.

Read more in React official docs

My suggestion is to use formik npm package to build your form correctly and easily, formik using context for managing the state behind the scene.
 I recommend it for regular use of forms, not heavy form with many fields (~25-30 max) like in enterprises.

You can read about formik more here:
https://formik.org/docs/api/formik

There is another alternative for formik which is also popular in react using forms:

react form hook: https://react-hook-form.com/

 I didn't use it yet but as I know it has some pros/cons over formik.


问:
align different image ridimension and delete margin
I should like know how I can fix different image alignment and remove margin.  I want to align items in the center but there is a part of iamge that I think is margin that I can't remove. The part I want remove is the orange zone

.content {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 20px;
  width: 100%;
}

.content__row {
  display: flex;
  width: 100%;
  margin: 5px 0;
}

.content__img {
  display: flex;
  width: 80%;
  height: 90%;
  padding: 0%;
}

.content__a {
  text-decoration: none;
  align-items: center;
}

.content__p {
  align-items: center;
  text-align: center;
  width: 80%;
  font-size: 35px;
  font-weight: 900;
  font-family: 'Ubuntu', sans-serif;
  color: rgb(39, 39, 255);
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="content">
  <div class="content__row">

      <img class="content__img" src="https://via.placeholder.com/300x600" alt="">
      <p class="content__p">COMPILARE
        <br>
        <span class="content__span">730?</span>
      </p>

      <img class="content__img" src="https://via.placeholder.com/300x600" alt="">
      <p class="content__p">CALCOLARE
        <br>
        <span class="content__span">L'ISEE?</span>
      </p>

      <img class="content__img" src="https://via.placeholder.com/300x600" alt="">
      <p class="content__p">CALCOLARE
        <br>
        <span class="content__span">L'ISEE?</span>
      </p>

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

答:
Well, basically you've given your 's a width of 30%, and there's no justify-content property on that flexbox (.content__row), so there's 10% left over on the right. Add justify-content: space-between; (or around) into your .column__row class.

Some comments:

I'm not sure why you're adding flex onto your images. If you turn your 's into column flexboxes then you'll get rid of the 'space' character between your image and
 tags (a common problem).

Look into adding the object-fit: cover; property onto your images, then you can resize them without worrying about the images distorting.

Body:
I should like know how I can fix different image alignment and remove margin.  I want to align items in the center but there is a part of iamge that I think is margin that I can't remove. The part I want remove is the orange zone

.content {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 20px;
  width: 100%;
}

.content__row {
  display: flex;
  width: 100%;
  margin: 5px 0;
}

.content__img {
  display: flex;
  width: 80%;
  height: 90%;
  padding: 0%;
}

.content__a {
  text-decoration: none;
  align-items: center;
}

.content__p {
  align-items: center;
  text-align: center;
  width: 80%;
  font-size: 35px;
  font-weight: 900;
  font-family: 'Ubuntu', sans-serif;
  color: rgb(39, 39, 255);
}

COMPILARE

        730?

CALCOLARE

        L'ISEE?

CALCOLARE

        L'ISEE?

Body:
Well, basically you've given your <a>'s a width of 30%, and there's no justify-content property on that flexbox (.content__row), so there's 10% left over on the right. Add justify-content: space-between; (or around) into your .column__row class.

Some comments:

I'm not sure why you're adding flex onto your images. If you turn your <a>'s into column flexboxes then you'll get rid of the 'space' character between your image and <p> tags (a common problem).

Look into adding the object-fit: cover; property onto your images, then you can resize them without worrying about the images distorting.


问:
how to show n lines above and below my grep search?
I checked out the manifest file of k8s, but i want to also see the above n lines of my search.

```<code>   cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep -i authorization -A4
        - --authorization-mode=Node,RBAC
        - --client-ca-file=/etc/kubernetes/pki/ca.crt
        - --enable-admission-plugins=NodeRestriction
        - --enable-bootstrap-token-auth=true
        - --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
</code>```

how tod this?

答: Append -B<n> to your command. Example: grep -i authorization -A4 -B4

Body:
I checked out the manifest file of k8s, but i want to also see the above n lines of my search.

   cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep -i authorization -A4
        - --authorization-mode=Node,RBAC
        - --client-ca-file=/etc/kubernetes/pki/ca.crt
        - --enable-admission-plugins=NodeRestriction
        - --enable-bootstrap-token-auth=true
        - --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt

how tod this?


问:
Python error only size-1 arrays can be converted to Python scalars
I'm writing a tKinter GUI and I've come accross a problem. Given the the main code is way to large, I've made a dummy example to replicate my problem.

```<code>class test():
    def __init__(self,time,a):

        self.time = time
        self.a = a

    def something(self):

        self.b = float(self.time) * self.a

        return {'bVar':self.b}

time = np.linspace(0,100,10)
testObj = test(time,2.2)
print(testObj.something().get('bVar'))
</code>```

The error is:

```<code>----> 9         self.b = float(self.time) * self.a
     10
     11         return {'bVar':self.b}

TypeError: only size-1 arrays can be converted to Python scalars
</code>```

If I remove the float conversion of time, then it works. But in my main GUI I need to keep float(self.time).
self.a in my GUI is a tKinter entry that has to be also of type float(entry[1].get())

Any thoughts on how I can remedy this?

答:
The error "only length-1 arrays can be converted to Python scalars" is raised when the function expects a single value but you pass an array instead.

Problem is that you are passing an array self.time to the float that expects a single value instead of an array.

You got time using np.linspace(0, 100, 10) which returns an array of evenly spaced numbers over a specified interval.

https://numpy.org/doc/stable/reference/generated/numpy.linspace.html

Updating my answer, assuming you want to multiply each value in the list with a single value.

```<code>class Test():
    def __init__(self,time,a):
        self.time = time
        self.a = a

    def something(self):
        self.b = np.array(self.time, dtype=float) * self.a
        return {'bVar':self.b}

time = np.linspace(0,100,10)
testObj = Test(time,2.2)
print(testObj.something().get('bVar'))
</code>```

You can set dtype=float in numpy array to set the data type of the array to float. And then you can use * to multiply a single value with each value in the array.

Body:
I'm writing a tKinter GUI and I've come accross a problem. Given the the main code is way to large, I've made a dummy example to replicate my problem.

class test():
    def __init__(self,time,a):

        self.time = time
        self.a = a

    def something(self):

        self.b = float(self.time) * self.a

        return {'bVar':self.b}

time = np.linspace(0,100,10)
testObj = test(time,2.2)
print(testObj.something().get('bVar'))

The error is:

----> 9         self.b = float(self.time) * self.a
     10
     11         return {'bVar':self.b}

TypeError: only size-1 arrays can be converted to Python scalars

If I remove the float conversion of time, then it works. But in my main GUI I need to keep float(self.time).
self.a in my GUI is a tKinter entry that has to be also of type float(entry[1].get())

Any thoughts on how I can remedy this?

Body:
The error "only length-1 arrays can be converted to Python scalars" is raised when the function expects a single value but you pass an array instead.

Problem is that you are passing an array self.time to the float that expects a single value instead of an array.

You got time using np.linspace(0, 100, 10) which returns an array of evenly spaced numbers over a specified interval.

https://numpy.org/doc/stable/reference/generated/numpy.linspace.html

Updating my answer, assuming you want to multiply each value in the list with a single value.

class Test():
    def __init__(self,time,a):
        self.time = time
        self.a = a

    def something(self):
        self.b = np.array(self.time, dtype=float) * self.a
        return {'bVar':self.b}

time = np.linspace(0,100,10)
testObj = Test(time,2.2)
print(testObj.something().get('bVar'))

You can set dtype=float in numpy array to set the data type of the array to float. And then you can use * to multiply a single value with each value in the array.


问:
Swift Dateformatter() converts wrong date
I want to convert a string into a date and use a small function. If I run the code the results is different from the input:

```<code> let startDateString = "20-02-2022-15-00"
        print(startDateString)
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "DD-MM-YYYY-HH:mm"
        dateFormatter.timeZone = TimeZone.current
        let startDate = dateFormatter.date(from: startDateString)
        print(startDate!)
</code>```

the startDateString is "20-02-2022-15-00"
the result of startDate is: 2021-12-27 14:00:00 +0000

The results has the wrong format YYYY-MM-DD HH:MM vs DD-MM-YYYY-HH:mm
and the date is about two month in the past.

What am I doing wrong?

答:
You need yyyy not YYYY

```<code>dateFormatter.dateFormat = "dd-MM-yyyy-HH:mm"
</code>```

Body:
I want to convert a string into a date and use a small function. If I run the code the results is different from the input:

 let startDateString = "20-02-2022-15-00"
        print(startDateString)
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "DD-MM-YYYY-HH:mm"
        dateFormatter.timeZone = TimeZone.current
        let startDate = dateFormatter.date(from: startDateString)
        print(startDate!)

the startDateString is "20-02-2022-15-00"
the result of startDate is: 2021-12-27 14:00:00 +0000

The results has the wrong format YYYY-MM-DD HH:MM vs DD-MM-YYYY-HH:mm
and the date is about two month in the past.

What am I doing wrong?

Body:
You need yyyy not YYYY

dateFormatter.dateFormat = "dd-MM-yyyy-HH:mm"


问:
How to align icon with element instead of element text
I have a svg icon and a span element side by side. When the text overflows inside the span element, the svg also comes down along with it. I don't want it to happen.

Here is a  of what I'm doing.

As you can see, the arrow icon is proper in Section 1 but in Section 2, it comes down with the text.

Here is the HTML code -

.rightContent {
  border: 2px solid green;
  height: 100vh;
  overflow-y: scroll;
}

.sectionHeading {
  background: #f7f9fa;
  text-align: left;
  font-family: sf pro display, -apple-system, BlinkMacSystemFont, Roboto, segoe ui, Helvetica, Arial, sans-serif, apple color emoji, segoe ui emoji, segoe ui symbol;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -.02rem;
  font-size: 1rem;
  padding-top: .75rem;
  padding-bottom: 1.05rem;
}

.sectionHeading p {
  padding: 0;
  margin: 0;
}

.sectionHeading svg {
  float: right;
}

.sectionHeading small {
  font-size: 0.75rem;
  font-weight: 500;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="rightContent col col-lg-3 col-md-3 sol-sm-3" style="border: 2px solid green;">
  <!-- Section 1: Introduction -->
  <div class="row">
    <div class="card">
      <div class="sectionHeading">
        <span>Section 1: Introduction</span>
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>
        <p></p>
        <small>14 / 14 | 1hr 5min</small>
      </div>
    </div>
  </div>

  <!-- Section 2: Experience -->
  <div class="row">
    <div class="card">
      <div class="sectionHeading">
        <span>Section 2: Experiences In The Corporate World</span>
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>
        <p></p>
        <small>19 / 19 | 2hr 15min</small>
      </div>
    </div>
  </div></code>```

Most of the questions already present deal with aligning icons with text, but as I mentioned earlier, I want to align icons with span element and keep it fixed there, instead of aligning it with text.

Please feel free to share your inputs. I'll be grateful to learn from others since I'm still learning CSS. Thank you.

答:
Please set max-width or width of span tag. As width svg tag is 16px, you can set that max-width of  is calc(100% - 16px).

Try it

```

Body:
I have a svg icon and a span element side by side. When the text overflows inside the span element, the svg also comes down along with it. I don't want it to happen.

Here is a  of what I'm doing.

As you can see, the arrow icon is proper in Section 1 but in Section 2, it comes down with the text.

Here is the HTML code -

.rightContent {
  border: 2px solid green;
  height: 100vh;
  overflow-y: scroll;
}

.sectionHeading {
  background: #f7f9fa;
  text-align: left;
  font-family: sf pro display, -apple-system, BlinkMacSystemFont, Roboto, segoe ui, Helvetica, Arial, sans-serif, apple color emoji, segoe ui emoji, segoe ui symbol;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -.02rem;
  font-size: 1rem;
  padding-top: .75rem;
  padding-bottom: 1.05rem;
}

.sectionHeading p {
  padding: 0;
  margin: 0;
}

.sectionHeading svg {
  float: right;
}

.sectionHeading small {
  font-size: 0.75rem;
  font-weight: 500;
}

        Section 1: Introduction
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>

        14 / 14 | 1hr 5min

        Section 2: Experiences In The Corporate World
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>

        19 / 19 | 2hr 15min

Most of the questions already present deal with aligning icons with text, but as I mentioned earlier, I want to align icons with span element and keep it fixed there, instead of aligning it with text.

Please feel free to share your inputs. I'll be grateful to learn from others since I'm still learning CSS. Thank you.

Body:
Please set max-width or width of span tag. As width svg tag is 16px, you can set that max-width of  is calc(100% - 16px).

Try it

<style>
.sectionHeading span{
    display: inline-block;
    max-width: calc(100% - 16px);
}
</style>


问:
The code I wrote on Flutter was working yesterday, but today it just gives a black screen
The code I wrote on Flutter was working yesterday, but today it just gives a black screen. I don't understand. I am learning and it's very annoying to do this without any errors. 199 / 5,000

These parts were entered because more details were requested about the post. It's still waiting for me to explain this issue further. I'm getting a black screen, I don't understand how to explain.

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';

    bool _valuehidra = true;
    bool _valuelight = true;
    bool _valuewebasto = true;
    bool _valueusb = true;
    bool _valuealter = true;

    void main() {
      runApp(const MaterialApp(
      ));
    }

    class MyApp extends StatefulWidget {
      const MyApp({Key? key}) : super(key: key);

      @override
      _State createState() => _State();
    }

    class _State extends State<MyApp> {
      bool isSwitched = false;

      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
        body: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
            Container(
              margin: const EdgeInsets.fromLTRB(10, 30, 0, 0),
              child: Image.asset('assets/images/logoisim.png', height: 125.0, fit: BoxFit.cover,),
            ),
            ],
          ),
          Container(

            decoration: BoxDecoration(
              color: Colors.indigoAccent,
                borderRadius: BorderRadius.circular(30.0),
            ),
            width: 350,
            height: 70,
            alignment: Alignment.bottomCenter,
            margin: const EdgeInsets.only(top: 20),
            child: Row(
              children: <Widget>[
                SizedBox(width: 200,
                  child: Container(
                    margin: const EdgeInsets.only(left: 30),
                    alignment: Alignment.centerLeft,
                    child: const Text('AYDINLATMA', style: TextStyle(color: Colors.white, fontSize: 25,fontFamily: 'YesevaOne' )),
                  ),
                ),
                Transform.scale( scale: 1.3,
                  child: Container(
                    margin: const EdgeInsets.only(left:50),
                    child: CupertinoSwitch(
                      value: _valuelight,
                      onChanged: (value) {
                        setState(() {
                          _valuelight = value;
                        });
                      },
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            decoration: BoxDecoration(
              color: Colors.indigo,
              borderRadius: BorderRadius.circular(30.0),
            ),
            width: 350,
            height: 70,
            alignment: Alignment.bottomCenter,
            margin: const EdgeInsets.only(top: 10),
            child: Row(
              children: <Widget>[
                SizedBox(width: 200,
                  child: Container(
                    margin: const EdgeInsets.only(left: 30),
                    alignment: Alignment.centerLeft,
                    child: const Text('WEBASTO', style: TextStyle(color: Colors.white, fontSize: 25,fontFamily: 'YesevaOne' )),
                  ),
                ),
                Transform.scale( scale: 1.3,
                  child: Container(
                    margin: const EdgeInsets.only(left:50),
                    child: CupertinoSwitch(
                      value: _valuewebasto,
                      onChanged: (value) {
                        setState(() {
                          _valuewebasto = value;
                        });
                      },
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            decoration: BoxDecoration(
              color: Colors.indigoAccent,
              borderRadius: BorderRadius.circular(30.0),
            ),
            width: 350,
            height: 70,
            alignment: Alignment.bottomCenter,
            margin: const EdgeInsets.only(top: 10),
            child: Row(
              children: <Widget>[
                SizedBox(width: 200,
                  child: Container(
                    margin: const EdgeInsets.only(left: 30),
                    alignment: Alignment.centerLeft,
                    child: const Text('HİDRAFOR', style: TextStyle(color: Colors.white, fontSize: 25,fontFamily: 'YesevaOne' )),
                  ),
                ),
                Transform.scale( scale: 1.3,
                  child: Container(
                    margin: const EdgeInsets.only(left:50),
                    child: CupertinoSwitch(
                      value: _valuehidra,
                      onChanged: (value) {
                        setState(() {
                          _valuehidra = value;
                        });
                      },
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            decoration: BoxDecoration(
              color: Colors.indigo,
              borderRadius: BorderRadius.circular(30.0),
            ),
            width: 350,
            height: 70,
            alignment: Alignment.bottomCenter,
            margin: const EdgeInsets.only(top: 10),
            child: Row(
              children: <Widget>[
                SizedBox(width: 200,
                  child: Container(
                    margin: const EdgeInsets.only(left: 30),
                    alignment: Alignment.centerLeft,
                    child: const Text('ALTERNATÖR', style: TextStyle(color: Colors.white, fontSize: 25,fontFamily: 'YesevaOne' )),
                  ),
                ),
                Transform.scale( scale: 1.3,
                  child: Container(
                    margin: const EdgeInsets.only(left:50),
                    child: CupertinoSwitch(
                      value: _valuealter,
                      onChanged: (value) {
                        setState(() {
                          _valuealter = value;
                        });
                      },
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            decoration: BoxDecoration(
              color: Colors.indigoAccent,
              borderRadius: BorderRadius.circular(30.0),
            ),
            width: 350,
            height: 70,
            alignment: Alignment.bottomCenter,
            margin: const EdgeInsets.only(top: 10),
            child: Row(
              children: <Widget>[
                SizedBox(width: 200,
                  child: Container(
                    margin: const EdgeInsets.only(left: 30),
                    alignment: Alignment.centerLeft,
                    child: const Text('USB (12V)', style: TextStyle(color: Colors.white, fontSize: 25,fontFamily: 'YesevaOne' )),
                  ),
                ),
                Transform.scale( scale: 1.3,
                  child: Container(
                    margin: const EdgeInsets.only(left:50),
                    child: CupertinoSwitch(
                      value: _valueusb,
                      onChanged: (value) {
                        setState(() {
                          _valueusb = value;
                        });
                      },
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            decoration: BoxDecoration(
              color: Colors.indigo,
              borderRadius: BorderRadius.circular(30.0),
            ),
            width: 350,
            height: 90,
            alignment: Alignment.bottomCenter,
            margin: const EdgeInsets.only(top: 10),
            child: Row(
              children: <Widget>[
                Container(
                  margin: const EdgeInsets.only(left: 30),
                  child: Image.asset('assets/images/caravan.png', height: 40.0, fit: BoxFit.cover,),
                ),
                Container(
                  margin: const EdgeInsets.only(left: 10),
                  alignment: Alignment.center,
                  child: const Text('KARAVANIM', style: TextStyle(color: Colors.white, fontSize: 31,fontFamily: 'YesevaOne' )),
                ),
                Container(
                  margin: const EdgeInsets.only(left: 10),
                  child: Image.asset('assets/images/caravan2.png', height: 40.0, fit: BoxFit.cover,),
                ),
              ],
            ),
          ),

        ],
        ),
      ),
    );
  }
}

```

答:
You were not running MyApp widget but MaterialApp

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

Body:
You were not running MyApp widget but MaterialApp

void main() {
 runApp(const MyApp(
  ));
}


问:
Can I change a component I downloaded in react-native project from node_modules?
I want to use a kinda outdated module react-native-numbers-please (last update 2 years ago), but it doesn't work. I think it's because it uses a Picker from react-native. I've changed that in the module's NumbersPlease.tsx which is the main file from which the component is exported.

So I've updated it to use Picker from @react-native-picker/picker as react suggested in a warning message, but I don't think its changed anything since I still get the exact same warning (Picker has been extracted from react-native core and will be removed in a future release...).

I've tried restarting the npm server, but It hasn't changed anything.

Is there something else I need to do for react-native to recognize that I've changed the module, or does it just simply not work?

Thanks in advance!

答:
```<code>npm uninstall packName
npm install packageName
</code>```

then start your project

Make sure server is off by the time you are running this command.

if it does not work  reset-cache
try starting it with
npm start -- --reset-cache

expo r -c  ///if u are using expo

Body:
npm uninstall packName
npm install packageName

then start your project

Make sure server is off by the time you are running this command.

if it does not work  reset-cache
try starting it with
npm start -- --reset-cache

expo r -c  ///if u are using expo


问:
OpenCV & C++: returned cv::Mat causes a Segmentation Fault
I'm writing a class that grabs frames from basler USB cameras using pylonAPI and converts them to openCV format. Here's the code of the grabbing method:

```<code>std::pair<cv::Mat, cv::Mat> GrabPair()
    {
        int key;
        Pylon::CImageFormatConverter fmtConv;
        Pylon::CPylonImage pylonImg1, pylonImg2;
        fmtConv.OutputPixelFormat = Pylon::PixelType_BGR8packed;
        Pylon::CPylonImage pylonImgLeft, pylonImgRight;

        cv::Mat outImageL, outImageR;
        Pylon::CGrabResultPtr pResultLeft, pResultRight;

        if(cams.IsGrabbing()){
            cams[leftCamIndex].RetrieveResult(1000, pResultLeft, Pylon::TimeoutHandling_ThrowException);
            cams[rightCamIndex].RetrieveResult(1000, pResultRight, Pylon::TimeoutHandling_ThrowException);

            if(pResultLeft->GrabSucceeded() && pResultRight->GrabSucceeded()){
                fmtConv.Convert(pylonImgLeft, pResultLeft);

                outImageL = cv::Mat(pResultLeft->GetHeight(),
                    pResultLeft->GetWidth(),
                    CV_8UC3,
                    (uint8_t*)pylonImgLeft.GetBuffer()
                );

                fmtConv.Convert(pylonImgRight, pResultRight);

                outImageR = cv::Mat(pResultRight->GetHeight(),
                    pResultRight->GetWidth(),
                    CV_8UC3,
                    (uint8_t*)pylonImgRight.GetBuffer()
                );

                /*cv::namedWindow("OpenCV out L");
                cv::namedWindow("OpenCV out R");

                cv::imshow("OpenCV out L", outImageL);
                cv::imshow("OpenCV out R", outImageR);
                key = cv::waitKey(20);*/
            }
        }

        return std::make_pair(outImageL, outImageR);
    }
</code>```

And this is the code fragment from main() where I try to acquire and show the grabbed frames

```<code>int key;
    BslrCap bc(40146142, 40146151); //the grabber object
    bc.PrintDevicesInfo();
    bc.Open();
    cv::Mat raw_left, raw_right;
    while(1)
    {
        std::pair<cv::Mat, cv::Mat> images_pair = bc.GrabPair();
        cv::Mat raw_left = images_pair.first;
        cv::Mat raw_right = images_pair.second;

        if(!raw_right.empty() && !raw_left.empty()){
            cv::namedWindow("OpenCV out L");
            cv::namedWindow("OpenCV out R");

            cv::imshow("OpenCV out L", raw_left);
            cv::imshow("OpenCV out R", raw_right);

            key = cv::waitKey(20);
        }
    }
</code>```

But when I call cv::imshow I immediately receive SIGSEGV and my program crashes.

If I uncomment these lines in GrabPair() code:

```<code>                    pResultRight->GetWidth(),
                    CV_8UC3,
                    (uint8_t*)pylonImgRight.GetBuffer()
                );

                /*cv::namedWindow("OpenCV out L");
                cv::namedWindow("OpenCV out R");

                cv::imshow("OpenCV out L", outImageL);
                cv::imshow("OpenCV out R", outImageR);
                key = cv::waitKey(20);*/
</code>```

I can see two openCV windows on my screen with two captured frames. This means that both frames are captured without errors, and the problem is when I try to return std::pair from the method GrabPair() back to main()

Any ideas what I'm doing wrong?

Great thanks in advance!

答:
the problem here is, that the Mat constructor using an external pointer is NOT refcounted. once you leave the GrabPair function, both Mat's are invalid (dangling pointer)

you need to clone() them (deep copy of the data !) e.g. like:

```<code>    return std::make_pair(outImageL.clone(), outImageR.clone());
</code>```

Body:
I'm writing a class that grabs frames from basler USB cameras using pylonAPI and converts them to openCV format. Here's the code of the grabbing method:

std::pair<cv::Mat, cv::Mat> GrabPair()
    {
        int key;
        Pylon::CImageFormatConverter fmtConv;
        Pylon::CPylonImage pylonImg1, pylonImg2;
        fmtConv.OutputPixelFormat = Pylon::PixelType_BGR8packed;
        Pylon::CPylonImage pylonImgLeft, pylonImgRight;

        cv::Mat outImageL, outImageR;
        Pylon::CGrabResultPtr pResultLeft, pResultRight;

        if(cams.IsGrabbing()){
            cams[leftCamIndex].RetrieveResult(1000, pResultLeft, Pylon::TimeoutHandling_ThrowException);
            cams[rightCamIndex].RetrieveResult(1000, pResultRight, Pylon::TimeoutHandling_ThrowException);

            if(pResultLeft->GrabSucceeded() && pResultRight->GrabSucceeded()){
                fmtConv.Convert(pylonImgLeft, pResultLeft);

                outImageL = cv::Mat(pResultLeft->GetHeight(),
                    pResultLeft->GetWidth(),
                    CV_8UC3,
                    (uint8_t*)pylonImgLeft.GetBuffer()
                );

                fmtConv.Convert(pylonImgRight, pResultRight);

                outImageR = cv::Mat(pResultRight->GetHeight(),
                    pResultRight->GetWidth(),
                    CV_8UC3,
                    (uint8_t*)pylonImgRight.GetBuffer()
                );

                /*cv::namedWindow("OpenCV out L");
                cv::namedWindow("OpenCV out R");

                cv::imshow("OpenCV out L", outImageL);
                cv::imshow("OpenCV out R", outImageR);
                key = cv::waitKey(20);*/
            }
        }

        return std::make_pair(outImageL, outImageR);
    }

And this is the code fragment from main() where I try to acquire and show the grabbed frames

int key;
    BslrCap bc(40146142, 40146151); //the grabber object
    bc.PrintDevicesInfo();
    bc.Open();
    cv::Mat raw_left, raw_right;
    while(1)
    {
        std::pair<cv::Mat, cv::Mat> images_pair = bc.GrabPair();
        cv::Mat raw_left = images_pair.first;
        cv::Mat raw_right = images_pair.second;

        if(!raw_right.empty() && !raw_left.empty()){
            cv::namedWindow("OpenCV out L");
            cv::namedWindow("OpenCV out R");

            cv::imshow("OpenCV out L", raw_left);
            cv::imshow("OpenCV out R", raw_right);

            key = cv::waitKey(20);
        }
    }

But when I call cv::imshow I immediately receive SIGSEGV and my program crashes.

If I uncomment these lines in GrabPair() code:

                    pResultRight->GetWidth(),
                    CV_8UC3,
                    (uint8_t*)pylonImgRight.GetBuffer()
                );

                /*cv::namedWindow("OpenCV out L");
                cv::namedWindow("OpenCV out R");

                cv::imshow("OpenCV out L", outImageL);
                cv::imshow("OpenCV out R", outImageR);
                key = cv::waitKey(20);*/

I can see two openCV windows on my screen with two captured frames. This means that both frames are captured without errors, and the problem is when I try to return std::pair from the method GrabPair() back to main()

Any ideas what I'm doing wrong?

Great thanks in advance!

Body:
the problem here is, that the Mat constructor using an external pointer is NOT refcounted. once you leave the GrabPair function, both Mat's are invalid (dangling pointer)

you need to clone() them (deep copy of the data !) e.g. like:

    return std::make_pair(outImageL.clone(), outImageR.clone());


问:
A-frame limit cameras y distance
I have some code that restricts the camera movement in A-frame so when the camera moves 10 spaces away from the starting point, they are teleported back to the position 0, 1.6, 0. Currently, this works is the players x or y axis moves 10 spaces away from their starting point. How can I modify this so the player is only teleported back if only their y position moves 10 spaces from their starting point? Code:

```<code><!DOCTYPE html>
<html lang="en">

<head>
  <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
  <meta charset="UTF-8" />
</head>

<body>
  <script>
    AFRAME.registerComponent('limit-my-distance', {
      init: function() {
        this.zero = new THREE.Vector3(0, 0, 0);
      },
      tick: function() {
        if (this.el.object3D.position.distanceTo(this.zero) > 10) {
          this.el.object3D.position.set(0, 1.6, 0);
        }
      }
    });
  </script>
  <a-scene>
    <a-sphere position="0 2 -10"color="red"></a-sphere>
    <a-plane color="green" position="0 0 -5" rotation="-90 0 0" width="20" height="20"></a-plane>
    <a-camera limit-my-distance></a-camera>
    <a-sky color="#fff"></a-sky>
  </a-scene>
</body>

</html>
</code>```

答:
you can even do something like this to not trap into walls at position 4 around you!
Here as well with camera movement settings WASD

```

Title: A-frame limit cameras y distance

Body:
I have some code that restricts the camera movement in A-frame so when the camera moves 10 spaces away from the starting point, they are teleported back to the position 0, 1.6, 0. Currently, this works is the players x or y axis moves 10 spaces away from their starting point. How can I modify this so the player is only teleported back if only their y position moves 10 spaces from their starting point? Code:

<!DOCTYPE html>

  <meta charset="UTF-8" />

Body:
you can even do something like this to not trap into walls at position 4 around you!
Here as well with camera movement settings WASD

<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script>
    AFRAME.registerComponent('limit-my-distance', {
      init: function() {
          // nothing here
      },
      tick: function() {
        // limit Z
        if (this.el.object3D.position.z > 3.8) {
          this.el.object3D.position.z = 3.8;
        }
        if (this.el.object3D.position.z < -3.8) {
          this.el.object3D.position.z = -3.8;
        }

        // limit X
        if (this.el.object3D.position.x > 3.8) {
          this.el.object3D.position.x = 3.8;
        }
        if (this.el.object3D.position.x < -3.8) {
          this.el.object3D.position.x = -3.8;
        }

      }
    });
</script>
<a-scene>
<a-sphere position="0 2 -10"color="red"></a-sphere>
<a-plane color="green" position="0 0 -5" rotation="-90 0 0" width="20" height="20"></a-plane>
<a-camera limit-my-distance look-controls wasd-controls="acceleration:10" position="0 1.6 0"></a-camera>
<a-sky color="#fff"></a-sky>
</a-scene>


问:
How to select every (1918/1000)th record from SQL?
I have this SQL to select 1000 from 2000 records.

```<code>(SELECT
        @rownum := @rownum + 1 AS rownum,
        data.*,
        IF(@rownum % 2 = 1, '1', '2') AS grp,
</code>```

But now I am required to select 1000 from 1918 using the same notation, order.

That means I need to do instead of 2 a value of 1.918

How to do that via the SQL?

答:
I've come up with this:</p>

```<code>IF(MOD(@rownum,2)=1 OR MOD(@rownum,1.918) BETWEEN 0.960 AND 1.040, '1', '2') AS grp,
</code>```

Body:
I have this SQL to select 1000 from 2000 records.

(SELECT
        @rownum := @rownum + 1 AS rownum,
        data.*,
        IF(@rownum % 2 = 1, '1', '2') AS grp,

But now I am required to select 1000 from 1918 using the same notation, order.

That means I need to do instead of 2 a value of 1.918

How to do that via the SQL?

Body:
I've come up with this:

IF(MOD(@rownum,2)=1 OR MOD(@rownum,1.918) BETWEEN 0.960 AND 1.040, '1', '2') AS grp,