问:
Pandas error when using resample and groupby to apply function
I am new to python. I used to code in R and work with period_apply function. So I tried the following approaches in python below.

First, I do not understand what the errors are trying to tell me.
Second, I do not understand why I only get errors with groupby if I include the first row of the data. Yet with resample, i get error no matter whether I include the first row or not.
Third, how do I resolve this problem, please do not tell me skip the first row, because I work with a much much bigger dataset

Data

```<code>                           Best_Bid  Best_Ask
Timestamp
2019-05-02 11:59:59.602   29636.0   29638.0
2019-05-02 12:59:00.033       NaN       NaN
2019-05-02 12:59:00.033       NaN       NaN
2019-05-02 12:59:00.033       NaN       NaN
2019-05-02 12:59:00.033       NaN       NaN
2019-05-02 12:59:00.033       NaN       NaN
2019-05-02 12:59:00.033       NaN       NaN

{'Best_Bid': {Timestamp('2019-05-02 11:59:59.602000'): 29636.0,
  Timestamp('2019-05-02 12:59:00.033000'): nan},
 'Best_Bid_Q': {Timestamp('2019-05-02 11:59:59.602000'): 4.0,
  Timestamp('2019-05-02 12:59:00.033000'): nan},
 'Best_Ask': {Timestamp('2019-05-02 11:59:59.602000'): 29638.0,
  Timestamp('2019-05-02 12:59:00.033000'): nan}}
</code>```

And I am trying to apply the below function(I know I could have just done .agg({'Best_Bid':['last']}) but this is a simplified version of my original code).

Function

```<code>def func(x):
    best_bid = (x['Best_Bid'])[-1]
    best_ask = (x['Best_Ask'])[-1]
    return pd.Series([best_bid,best_ask], index=['bbbid', 'aaask'])
</code>```

groupby and grouper
If I skip the first row and run. Things work fine.

```<code>df.iloc[1:,:].groupby(pd.Grouper(freq='180S',closed='right',label='right',base=-0.0001)).apply(func)

                               bbbid  aaask
Timestamp
2019-05-02 12:59:59.999899904    NaN    NaN
</code>```

However, if i include the first row, I got the following error.

```<code>df.groupby(pd.Grouper(freq='180S',closed='right',label='right',base=-0.0001)).apply(func)

Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4405, in get_value
    return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
  File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 471, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 997, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1004, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: -1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-191-85550b07b869>", line 1, in <module>
    df.iloc[371448:371455,0:3].groupby(pd.Grouper(freq='180S',closed='right',label='right',base=-0.0001)).apply(func)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\groupby.py", line 735, in apply
    result = self._python_apply_general(f)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\groupby.py", line 751, in _python_apply_general
    keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\ops.py", line 206, in apply
    res = f(group)
  File "<ipython-input-104-c57c7e2b6885>", line 2, in func
    best_bid = (x['Best_Bid'])[-1]
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 871, in __getitem__
    result = self.index.get_value(self, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 651, in get_value
    value = Index.get_value(self, series, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4411, in get_value
    return libindex.get_value_at(s, key)
  File "pandas\_libs\index.pyx", line 44, in pandas._libs.index.get_value_at
  File "pandas\_libs\index.pyx", line 45, in pandas._libs.index.get_value_at
  File "pandas\_libs\util.pxd", line 98, in pandas._libs.util.get_value_at
  File "pandas\_libs\util.pxd", line 89, in pandas._libs.util.validate_indexer
IndexError: index out of bounds
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4405, in get_value
    return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
  File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 471, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 997, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1004, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: -1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-191-85550b07b869>", line 1, in <module>
    df.iloc[371448:371455,0:3].groupby(pd.Grouper(freq='180S',closed='right',label='right',base=-0.0001)).apply(func)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\groupby.py", line 735, in apply
    result = self._python_apply_general(f)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\groupby.py", line 751, in _python_apply_general
    keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\ops.py", line 206, in apply
    res = f(group)
  File "<ipython-input-104-c57c7e2b6885>", line 2, in func
    best_bid = (x['Best_Bid'])[-1]
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 871, in __getitem__
    result = self.index.get_value(self, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 651, in get_value
    value = Index.get_value(self, series, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4411, in get_value
    return libindex.get_value_at(s, key)
  File "pandas\_libs\index.pyx", line 44, in pandas._libs.index.get_value_at
  File "pandas\_libs\index.pyx", line 45, in pandas._libs.index.get_value_at
  File "pandas\_libs\util.pxd", line 98, in pandas._libs.util.get_value_at
  File "pandas\_libs\util.pxd", line 89, in pandas._libs.util.validate_indexer
IndexError: index out of bounds
</code>```

Resample

I got the following error regardless of including the first row or not.

```<code>df.resample(rule='180S',closed='right',label='right',base=-0.0001).agg(func)

Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4411, in get_value
    return libindex.get_value_at(s, key)
  File "pandas\_libs\index.pyx", line 44, in pandas._libs.index.get_value_at
  File "pandas\_libs\index.pyx", line 45, in pandas._libs.index.get_value_at
  File "pandas\_libs\util.pxd", line 98, in pandas._libs.util.get_value_at
  File "pandas\_libs\util.pxd", line 83, in pandas._libs.util.validate_indexer
TypeError: 'str' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 651, in get_value
    value = Index.get_value(self, series, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4419, in get_value
    raise e1
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4405, in get_value
    return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
  File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 473, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas\_libs\index.pyx", line 479, in pandas._libs.index.DatetimeEngine._date_check_type
KeyError: 'Best_Bid'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "pandas\_libs\tslibs\conversion.pyx", line 520, in pandas._libs.tslibs.conversion.convert_str_to_tsobject
  File "pandas\_libs\tslibs\parsing.pyx", line 228, in pandas._libs.tslibs.parsing.parse_datetime_string
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\dateutil\parser\_parser.py", line 1374, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\dateutil\parser\_parser.py", line 649, in parse
    raise ParserError("Unknown string format: %s", timestr)
dateutil.parser._parser.ParserError: Unknown string format: Best_Bid
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 660, in get_value
    return self.get_value_maybe_box(series, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 675, in get_value_maybe_box
    key = Timestamp(key)
  File "pandas\_libs\tslibs\timestamps.pyx", line 418, in pandas._libs.tslibs.timestamps.Timestamp.__new__
  File "pandas\_libs\tslibs\conversion.pyx", line 292, in pandas._libs.tslibs.conversion.convert_to_tsobject
  File "pandas\_libs\tslibs\conversion.pyx", line 523, in pandas._libs.tslibs.conversion.convert_str_to_tsobject
ValueError: could not convert string to Timestamp
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-190-d2caa0c5152a>", line 1, in <module>
    df.iloc[371448:371455,0:3].resample(rule='180S',closed='right',label='right',base=-0.0001).agg(func)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\resample.py", line 285, in aggregate
    result = self._groupby_and_aggregate(how, grouper, *args, **kwargs)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\resample.py", line 359, in _groupby_and_aggregate
    result = grouped._aggregate_item_by_item(how, *args, **kwargs)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\generic.py", line 1172, in _aggregate_item_by_item
    result[item] = colg.aggregate(func, *args, **kwargs)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\generic.py", line 269, in aggregate
    result = self._aggregate_named(func, *args, **kwargs)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\generic.py", line 452, in _aggregate_named
    output = func(group, *args, **kwargs)
  File "<ipython-input-104-c57c7e2b6885>", line 2, in func
    best_bid = (x['Best_Bid'])[-1]
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 871, in __getitem__
    result = self.index.get_value(self, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 662, in get_value
    raise KeyError(key)
KeyError: 'Best_Bid'
</code>```

答:
From what I can tell you are getting an error with the (x['Best_Bid'])[-1] because it is returning a KeyError: -1

Your apply function is iterating though each element (x) from the column (Best_Bid and Bid_Ask) and trying to grab the last index from the element (x) which doesn't make sense.

I don't have your dataset in front of me to work with but I would try this code to see if it works.

```<code>gdf = df.groupby(pd.Grouper(freq='180S',closed='right',label='right',base=-0.0001)).copy()

print(gdf['Best_Bid'][gdf.index[-1]],gdf.index[-1])
print(gdf['Best_Ask'][gdf.index[-1]],gdf.index[-1])
</code>```

Now this code can definitely be simplified but it should work for all rows and it will be much faster than the .apply method if it is a large dataset.

Body:
I am new to python. I used to code in R and work with period_apply function. So I tried the following approaches in python below.

First, I do not understand what the errors are trying to tell me.
Second, I do not understand why I only get errors with groupby if I include the first row of the data. Yet with resample, i get error no matter whether I include the first row or not.
Third, how do I resolve this problem, please do not tell me skip the first row, because I work with a much much bigger dataset

Data

                           Best_Bid  Best_Ask
Timestamp
2019-05-02 11:59:59.602   29636.0   29638.0
2019-05-02 12:59:00.033       NaN       NaN
2019-05-02 12:59:00.033       NaN       NaN
2019-05-02 12:59:00.033       NaN       NaN
2019-05-02 12:59:00.033       NaN       NaN
2019-05-02 12:59:00.033       NaN       NaN
2019-05-02 12:59:00.033       NaN       NaN

{'Best_Bid': {Timestamp('2019-05-02 11:59:59.602000'): 29636.0,
  Timestamp('2019-05-02 12:59:00.033000'): nan},
 'Best_Bid_Q': {Timestamp('2019-05-02 11:59:59.602000'): 4.0,
  Timestamp('2019-05-02 12:59:00.033000'): nan},
 'Best_Ask': {Timestamp('2019-05-02 11:59:59.602000'): 29638.0,
  Timestamp('2019-05-02 12:59:00.033000'): nan}}

And I am trying to apply the below function(I know I could have just done .agg({'Best_Bid':['last']}) but this is a simplified version of my original code).

Function

def func(x):
    best_bid = (x['Best_Bid'])[-1]
    best_ask = (x['Best_Ask'])[-1]
    return pd.Series([best_bid,best_ask], index=['bbbid', 'aaask'])

groupby and grouper
If I skip the first row and run. Things work fine.

df.iloc[1:,:].groupby(pd.Grouper(freq='180S',closed='right',label='right',base=-0.0001)).apply(func)

                               bbbid  aaask
Timestamp
2019-05-02 12:59:59.999899904    NaN    NaN

However, if i include the first row, I got the following error.

df.groupby(pd.Grouper(freq='180S',closed='right',label='right',base=-0.0001)).apply(func)

Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4405, in get_value
    return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
  File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 471, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 997, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1004, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: -1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-191-85550b07b869>", line 1, in <module>
    df.iloc[371448:371455,0:3].groupby(pd.Grouper(freq='180S',closed='right',label='right',base=-0.0001)).apply(func)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\groupby.py", line 735, in apply
    result = self._python_apply_general(f)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\groupby.py", line 751, in _python_apply_general
    keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\ops.py", line 206, in apply
    res = f(group)
  File "<ipython-input-104-c57c7e2b6885>", line 2, in func
    best_bid = (x['Best_Bid'])[-1]
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 871, in __getitem__
    result = self.index.get_value(self, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 651, in get_value
    value = Index.get_value(self, series, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4411, in get_value
    return libindex.get_value_at(s, key)
  File "pandas\_libs\index.pyx", line 44, in pandas._libs.index.get_value_at
  File "pandas\_libs\index.pyx", line 45, in pandas._libs.index.get_value_at
  File "pandas\_libs\util.pxd", line 98, in pandas._libs.util.get_value_at
  File "pandas\_libs\util.pxd", line 89, in pandas._libs.util.validate_indexer
IndexError: index out of bounds
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4405, in get_value
    return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
  File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 471, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 997, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1004, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: -1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-191-85550b07b869>", line 1, in <module>
    df.iloc[371448:371455,0:3].groupby(pd.Grouper(freq='180S',closed='right',label='right',base=-0.0001)).apply(func)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\groupby.py", line 735, in apply
    result = self._python_apply_general(f)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\groupby.py", line 751, in _python_apply_general
    keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\ops.py", line 206, in apply
    res = f(group)
  File "<ipython-input-104-c57c7e2b6885>", line 2, in func
    best_bid = (x['Best_Bid'])[-1]
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 871, in __getitem__
    result = self.index.get_value(self, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 651, in get_value
    value = Index.get_value(self, series, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4411, in get_value
    return libindex.get_value_at(s, key)
  File "pandas\_libs\index.pyx", line 44, in pandas._libs.index.get_value_at
  File "pandas\_libs\index.pyx", line 45, in pandas._libs.index.get_value_at
  File "pandas\_libs\util.pxd", line 98, in pandas._libs.util.get_value_at
  File "pandas\_libs\util.pxd", line 89, in pandas._libs.util.validate_indexer
IndexError: index out of bounds

Resample

I got the following error regardless of including the first row or not.

df.resample(rule='180S',closed='right',label='right',base=-0.0001).agg(func)

Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4411, in get_value
    return libindex.get_value_at(s, key)
  File "pandas\_libs\index.pyx", line 44, in pandas._libs.index.get_value_at
  File "pandas\_libs\index.pyx", line 45, in pandas._libs.index.get_value_at
  File "pandas\_libs\util.pxd", line 98, in pandas._libs.util.get_value_at
  File "pandas\_libs\util.pxd", line 83, in pandas._libs.util.validate_indexer
TypeError: 'str' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 651, in get_value
    value = Index.get_value(self, series, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4419, in get_value
    raise e1
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 4405, in get_value
    return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
  File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 473, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas\_libs\index.pyx", line 479, in pandas._libs.index.DatetimeEngine._date_check_type
KeyError: 'Best_Bid'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "pandas\_libs\tslibs\conversion.pyx", line 520, in pandas._libs.tslibs.conversion.convert_str_to_tsobject
  File "pandas\_libs\tslibs\parsing.pyx", line 228, in pandas._libs.tslibs.parsing.parse_datetime_string
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\dateutil\parser\_parser.py", line 1374, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\dateutil\parser\_parser.py", line 649, in parse
    raise ParserError("Unknown string format: %s", timestr)
dateutil.parser._parser.ParserError: Unknown string format: Best_Bid
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 660, in get_value
    return self.get_value_maybe_box(series, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 675, in get_value_maybe_box
    key = Timestamp(key)
  File "pandas\_libs\tslibs\timestamps.pyx", line 418, in pandas._libs.tslibs.timestamps.Timestamp.__new__
  File "pandas\_libs\tslibs\conversion.pyx", line 292, in pandas._libs.tslibs.conversion.convert_to_tsobject
  File "pandas\_libs\tslibs\conversion.pyx", line 523, in pandas._libs.tslibs.conversion.convert_str_to_tsobject
ValueError: could not convert string to Timestamp
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-190-d2caa0c5152a>", line 1, in <module>
    df.iloc[371448:371455,0:3].resample(rule='180S',closed='right',label='right',base=-0.0001).agg(func)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\resample.py", line 285, in aggregate
    result = self._groupby_and_aggregate(how, grouper, *args, **kwargs)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\resample.py", line 359, in _groupby_and_aggregate
    result = grouped._aggregate_item_by_item(how, *args, **kwargs)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\generic.py", line 1172, in _aggregate_item_by_item
    result[item] = colg.aggregate(func, *args, **kwargs)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\generic.py", line 269, in aggregate
    result = self._aggregate_named(func, *args, **kwargs)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\generic.py", line 452, in _aggregate_named
    output = func(group, *args, **kwargs)
  File "<ipython-input-104-c57c7e2b6885>", line 2, in func
    best_bid = (x['Best_Bid'])[-1]
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 871, in __getitem__
    result = self.index.get_value(self, key)
  File "C:\Users\testUser\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\datetimes.py", line 662, in get_value
    raise KeyError(key)
KeyError: 'Best_Bid'

Body:
From what I can tell you are getting an error with the (x['Best_Bid'])[-1] because it is returning a KeyError: -1

Your apply function is iterating though each element (x) from the column (Best_Bid and Bid_Ask) and trying to grab the last index from the element (x) which doesn't make sense.

I don't have your dataset in front of me to work with but I would try this code to see if it works.

gdf = df.groupby(pd.Grouper(freq='180S',closed='right',label='right',base=-0.0001)).copy()

print(gdf['Best_Bid'][gdf.index[-1]],gdf.index[-1])
print(gdf['Best_Ask'][gdf.index[-1]],gdf.index[-1])

Now this code can definitely be simplified but it should work for all rows and it will be much faster than the .apply method if it is a large dataset.


来源: stackexchange

问:
Submit HTML forms that contain dynamic IDs
I have 2 HTML forms that contain dynamic ID attributes. What I want is to store data with AJAX call from each HTML form separately. Currently AJAX call works only for one HTML form when I use static ID name "surveyImage".

I don't know how I can with jQuery to call method submit() individually for each form. Is there any way to resolve this issue?

Form with id="surveyImage13"

```<code> <form method="POST" action="http://localhost/1/467/survey" accept-charset="UTF-8" id="surveyImage13" role="form" class="form-material m-t-40" novalidate="novalidate">
    <div class="row">
        <div class="col-lg-12">
            <input name="questionnaire_pivot_id" id="questionnaire_pivot_id13" class="questionnaire_pivot_id" type="hidden" value="13">
            <input name="questionnaire_id" id="questionnaire_id" class="questionnaire_id" type="hidden" value="1">
            <input name="survey_image_id" id="survey_image_id" class="survey_image_id" type="hidden" value="467">

 ...
            <div class="row" style="margin-bottom: 5%;">
                <div class="col-xl-2 col-lg-3 col-md-3">
                     <button id="add" class="btn  btn-default btn-md-6" type="submit" style="margin-top: 11%;">Save</button>
                </div>
            </div>
     </form>
</code>```

Form with ID="surveyImage18"

```<code><form method="POST" action="http://localhost/2/467/survey" accept-charset="UTF-8" id="surveyImage18" role="form" class="form-material m-t-40" novalidate="novalidate">
      <div class="row">
        <div class="col-lg-12">
            <input name="questionnaire_pivot_id" id="questionnaire_pivot_id18" class="questionnaire_pivot_id" type="hidden" value="18">
            <input name="questionnaire_id" id="questionnaire_id" class="questionnaire_id" type="hidden" value="2">
           <input name="survey_image_id" id="survey_image_id" class="survey_image_id" type="hidden" value="467">

...

            </div>
        </div>

    <div class="row" style="margin-bottom: 5%;">
        <div class="col-xl-2 col-lg-3 col-md-3">
             <button id="add" class="btn  btn-default btn-md-6" type="submit" style="margin-top: 11%;">Save</button>
        </div>
    </div>
</form>
</code>```

AJAX call

```<code><script type="text/javascript">
 $("#surveyImage13").validate({
                    rules: {
                         'responses[]': {
                            required:true
                        }
                    },
                     // change name of error class that is assigned to input fields
    errorClass: 'error_validate',

    errorPlacement: function (label, element) {

        // default
        if (element.is(':radio')) {
            label.insertAfter(element.parent('.form-check-inline'));
        }
        else {
            label.insertAfter(element);
        }
    }

     });
</script>

<script type="text/javascript">

     $("#surveyImage13").submit(function(e) {
        e.preventDefault();

        var route=$('#surveyImage13').attr('action');
        var pivot_id = $("#questionnaire_pivot_id").val();

        // Get values of checked checkboxes
        var responses = $('.form-check-inline input').filter(':checked').map(function() {
          return this.value;
        }).get();

     var isFormValid = $("#surveyImage13").valid();

    if(isFormValid){

        $.ajax({
          type: "POST",
          url: route,
          data: {'responses': responses, 'pivot_id': pivot_id},
          success: function(response){

            $("#surveyImageForm").css("display", "none");
            $("#surveyImageAjax").css("display", "block");

            $('#SurveyTableAjaxColumn1').append(response[1]);
            $('#SurveyTableAjaxColumn2').append(response[0]);
          },
          error: function(){
            console.log('Error');
          }
        })
        }
     });

</script>
</code>```

答:
Thanks for all answers but I found solution. Im working in LARAVEL therefore I used foreach loop based on which i was able to assign dynamic ID on HTML forms.

```<code>@foreach($questionnaire_by_images as $t)
   <form id="surveyImage{{$t->id}}">...</form>
   <form id="surveyImage{{$t->id}}">...</form>
@endforeach
</code>```

script

```<code> @foreach($questionnaire_by_images as $t)
    <script type="text/javascript">
        $( document ).ready(function() {

             $("#surveyImage{{$t->id}}").validate({
                                rules: {
                                     'responses[]': {
                                        required:true
                                    }
                                },
                                 // change name of error class that is assigned to input fields
                errorClass: 'error_validate',

                errorPlacement: function (label, element) {

                    // default
                    if (element.is(':radio')) {
                        label.insertAfter(element.parent('.form-check-inline'));
                    }
                    else {
                        label.insertAfter(element);
                    }
                }

                 });

                 $("#surveyImage{{$t->id}}").submit(function(e) {
                    e.preventDefault();

                    var route=$('#surveyImage{{$t->id}}').attr('action');

                    var survey_image_pivot = $("#survey_image_pivot{{$t->id}}").val();

                    // Get values of checked checkboxes
                    var responses = $('.form-check-inline .radio{{$t->id}}').filter(':checked').map(function() {
                      return this.value;
                    }).get();

                 var isFormValid = $("#surveyImage{{$t->id}}").valid();

                if(isFormValid){

                    $.ajax({
                      type: "POST",
                      url: route,
                      data: {'responses': responses, 'survey_image_pivot': survey_image_pivot},
                      success: function(response){

                        $("#surveyImageForm{{$t->id}}").css("display", "none");
                        $("#surveyImageAjax{{$t->id}}").css("display", "block");

                        $('#SurveyTableAjaxColumn1{{$t->id}}').append(response[1]);
                        $('#SurveyTableAjaxColumn2{{$t->id}}').append(response[0]);
                      },
                      error: function(){
                        console.log('Error');
                      }
                    })
                    }
                 });
        });
        </script>
    </script>

  @endforeach
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have 2 HTML forms that contain dynamic ID attributes. What I want is to store data with AJAX call from each HTML form separately. Currently AJAX call works only for one HTML form when I use static ID name "surveyImage".

I don't know how I can with jQuery to call method submit() individually for each form. Is there any way to resolve this issue?

Form with id="surveyImage13"

 <form method="POST" action="http://localhost/1/467/survey" accept-charset="UTF-8" id="surveyImage13" role="form" class="form-material m-t-40" novalidate="novalidate">

            <input name="questionnaire_pivot_id" id="questionnaire_pivot_id13" class="questionnaire_pivot_id" type="hidden" value="13">
            <input name="questionnaire_id" id="questionnaire_id" class="questionnaire_id" type="hidden" value="1">
            <input name="survey_image_id" id="survey_image_id" class="survey_image_id" type="hidden" value="467">

 ...

                     <button id="add" class="btn  btn-default btn-md-6" type="submit" style="margin-top: 11%;">Save</button>

     </form>

Form with ID="surveyImage18"

<form method="POST" action="http://localhost/2/467/survey" accept-charset="UTF-8" id="surveyImage18" role="form" class="form-material m-t-40" novalidate="novalidate">

            <input name="questionnaire_pivot_id" id="questionnaire_pivot_id18" class="questionnaire_pivot_id" type="hidden" value="18">
            <input name="questionnaire_id" id="questionnaire_id" class="questionnaire_id" type="hidden" value="2">
           <input name="survey_image_id" id="survey_image_id" class="survey_image_id" type="hidden" value="467">

...

             <button id="add" class="btn  btn-default btn-md-6" type="submit" style="margin-top: 11%;">Save</button>

</form>

AJAX call

Body:
Thanks for all answers but I found solution. Im working in LARAVEL therefore I used foreach loop based on which i was able to assign dynamic ID on HTML forms.

@foreach($questionnaire_by_images as $t)
   <form id="surveyImage{{$t->id}}">...</form>
   <form id="surveyImage{{$t->id}}">...</form>
@endforeach

script

 @foreach($questionnaire_by_images as $t)
    <script type="text/javascript">
        $( document ).ready(function() {

             $("#surveyImage{{$t->id}}").validate({
                                rules: {
                                     'responses[]': {
                                        required:true
                                    }
                                },
                                 // change name of error class that is assigned to input fields
                errorClass: 'error_validate',

                errorPlacement: function (label, element) {

                    // default
                    if (element.is(':radio')) {
                        label.insertAfter(element.parent('.form-check-inline'));
                    }
                    else {
                        label.insertAfter(element);
                    }
                }

                 });

                 $("#surveyImage{{$t->id}}").submit(function(e) {
                    e.preventDefault();

                    var route=$('#surveyImage{{$t->id}}').attr('action');

                    var survey_image_pivot = $("#survey_image_pivot{{$t->id}}").val();

                    // Get values of checked checkboxes
                    var responses = $('.form-check-inline .radio{{$t->id}}').filter(':checked').map(function() {
                      return this.value;
                    }).get();

                 var isFormValid = $("#surveyImage{{$t->id}}").valid();

                if(isFormValid){

                    $.ajax({
                      type: "POST",
                      url: route,
                      data: {'responses': responses, 'survey_image_pivot': survey_image_pivot},
                      success: function(response){

                        $("#surveyImageForm{{$t->id}}").css("display", "none");
                        $("#surveyImageAjax{{$t->id}}").css("display", "block");

                        $('#SurveyTableAjaxColumn1{{$t->id}}').append(response[1]);
                        $('#SurveyTableAjaxColumn2{{$t->id}}').append(response[0]);
                      },
                      error: function(){
                        console.log('Error');
                      }
                    })
                    }
                 });
        });
        </script>
    </script>

  @endforeach


问:
Ansible OpenShift shell command fails, but works outside of Ansible
I have an Ansible playbook that includes an OpenShift CLI login:

```<code>- name: "OpenShift CLI login"
  shell: oc login -u kubeadmin -p $(cat /root/auth/kubeadmin-password) https://{{ groups['boot'][0] }}:6443
</code>```

It fails with (hostname has been replaced below):

```<code>fatal: [<host>]: FAILED! => {"changed": true, "cmd": "oc login -u kubeadmin -p $(cat /root/auth/kubeadmin-password) https://<host>:6443", "delta": "0:00:00.228068", "end": "2020-02-19 09:47:32.651617", "msg": "non-zero return code", "rc": 1, "start": "2020-02-19 09:47:32.423549", "stderr": "error: The server is using a certificate that does not match its hostname: x509: certificate is valid for kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, openshift, openshift.default, openshift.default.svc, openshift.default.svc.cluster.local, 172.30.0.1, not <host>", "stderr_lines": ["error: The server is using a certificate that does not match its hostname: x509: certificate is valid for kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, openshift, openshift.default, openshift.default.svc, openshift.default.svc.cluster.local, 172.30.0.1, not <host>"], "stdout": "", "stdout_lines": []}
</code>```

But if I run the command directly, it works:

```<code>oc login -u kubeadmin -p $(cat /root/auth/kubeadmin-password) https://<host>:6443
Login successful.
You have access to 55 projects, the list has been suppressed. You can list all projects with 'oc projects'
Using project "openshift-image-registry".
</code>```

Any thoughts on why it fails under Ansible?  Ansible version is 2.4.2.0.

答: This appears to be a problem with the certificate installed in OCP.  I can bypass the problem with the flag --insecure-skip-tls-verify=true on the oc login command.  Talking to our OCP admins...

Body:
I have an Ansible playbook that includes an OpenShift CLI login:

- name: "OpenShift CLI login"
  shell: oc login -u kubeadmin -p $(cat /root/auth/kubeadmin-password) https://{{ groups['boot'][0] }}:6443

It fails with (hostname has been replaced below):

fatal: [<host>]: FAILED! => {"changed": true, "cmd": "oc login -u kubeadmin -p $(cat /root/auth/kubeadmin-password) https://<host>:6443", "delta": "0:00:00.228068", "end": "2020-02-19 09:47:32.651617", "msg": "non-zero return code", "rc": 1, "start": "2020-02-19 09:47:32.423549", "stderr": "error: The server is using a certificate that does not match its hostname: x509: certificate is valid for kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, openshift, openshift.default, openshift.default.svc, openshift.default.svc.cluster.local, 172.30.0.1, not <host>", "stderr_lines": ["error: The server is using a certificate that does not match its hostname: x509: certificate is valid for kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, openshift, openshift.default, openshift.default.svc, openshift.default.svc.cluster.local, 172.30.0.1, not <host>"], "stdout": "", "stdout_lines": []}

But if I run the command directly, it works:

oc login -u kubeadmin -p $(cat /root/auth/kubeadmin-password) https://<host>:6443
Login successful.
You have access to 55 projects, the list has been suppressed. You can list all projects with 'oc projects'
Using project "openshift-image-registry".

Any thoughts on why it fails under Ansible?  Ansible version is 2.4.2.0.


问:
Batch extract multiple pass protected .7z
I'd like to write a PowerShell script to extract multiple password-protected .7z files, that have their passwords saved inside a text file that its filename contains the name of the .7z

File structure example:

EncryptedFile1_02192020.7z

EncryptedFile1_password.txt

EncryptedFile2_02192020.7z

EncryptedFile2_password.txt

The script below works only if there are 1 .7z and 1 .txt file in the folder.

```<code>$pass = get-content *.txt
7z.exe x *.7z -p"$pass" -o*
</code>```

Problem: I do not know how to use the combination of foreach and -Like statements to batch extract multiple encrypted files in the working directory. Thanks in advance for your help.

答:
Here's one method. It gathers the files inside the rootfolder, groups them on the first part of the filename before the underscore _ and finds the files ending in .txt and .7z inside these groups.

If we have both, the password is read from the text file and the 7z file is extracted.

```<code>Get-ChildItem -Path 'TheFolderWhereTheFilesAre' -File |
    Group-Object @{Expression = {($_.Name -split '_')[0]}} |
    Where-Object { $_.Count -gt 1 } |
    ForEach-Object {
        $pwFile = $_.Group | Where-Object { $_.Extension -eq '.txt' }
        $7zFile = $_.Group | Where-Object { $_.Extension -eq '.7z' }
        if (($pwFile) -and ($7zFile)) {
            Write-Host "Extracting file '$($7zFile.FullName)'"
            # the single quotes are needed when the password contains spaces or special characters
            $pass = "'{0}'" -f (Get-Content -Path $pwFile.FullName -Raw)
            7z.exe x $($7zFile.FullName) -p"$pass" -o*
    }
}
</code>```

Note, If the files all start with EncryptedFile_, then you can add -Filter 'EncryptedFile_*.*' to the Get-ChildItem cmdlet to speed things up

Body:
I'd like to write a PowerShell script to extract multiple password-protected .7z files, that have their passwords saved inside a text file that its filename contains the name of the .7z

File structure example:

EncryptedFile1_02192020.7z

EncryptedFile1_password.txt

EncryptedFile2_02192020.7z

EncryptedFile2_password.txt

The script below works only if there are 1 .7z and 1 .txt file in the folder.

$pass = get-content *.txt
7z.exe x *.7z -p"$pass" -o*

Problem: I do not know how to use the combination of foreach and -Like statements to batch extract multiple encrypted files in the working directory. Thanks in advance for your help.

Body:
Here's one method. It gathers the files inside the rootfolder, groups them on the first part of the filename before the underscore _ and finds the files ending in .txt and .7z inside these groups.

If we have both, the password is read from the text file and the 7z file is extracted.

Get-ChildItem -Path 'TheFolderWhereTheFilesAre' -File |
    Group-Object @{Expression = {($_.Name -split '_')[0]}} |
    Where-Object { $_.Count -gt 1 } |
    ForEach-Object {
        $pwFile = $_.Group | Where-Object { $_.Extension -eq '.txt' }
        $7zFile = $_.Group | Where-Object { $_.Extension -eq '.7z' }
        if (($pwFile) -and ($7zFile)) {
            Write-Host "Extracting file '$($7zFile.FullName)'"
            # the single quotes are needed when the password contains spaces or special characters
            $pass = "'{0}'" -f (Get-Content -Path $pwFile.FullName -Raw)
            7z.exe x $($7zFile.FullName) -p"$pass" -o*
    }
}

Note, If the files all start with EncryptedFile_, then you can add -Filter 'EncryptedFile_*.*' to the Get-ChildItem cmdlet to speed things up


问:
how to combine 3 conditions by "OR" in jquery?
Is there anyway to combine 3 conditions by "OR" in jquery? Eg, $(t_obj).keyup(function(e) || $('#sendResp').click(function(e) || $('.emo').click(function(e) {  ? I have tested to combine them but I get error. How to combine them?

答:
Maybe this?</p>

```<code>function doThing(){...}

$(t_obj).keyup(doThing());
$('#sendResp, .emo').click(doThing());
</code>```

ContentLicense: CC BY-SA 3.0

Body:
Maybe this?

function doThing(){...}

$(t_obj).keyup(doThing());
$('#sendResp, .emo').click(doThing());


问:
read a csv file and and add its all data into vector in c++
For example to add the following CSV data:

I am trying to add CSV file into a 2D array string vector and get the sum of each column. The following program didn't work properly,

```<code>vector<string> read_csv(string filename){

    vector<string> result;
    fstream fin;
    fin.open(filename, ios::in);

    if(!fin.is_open())
        throw std::runtime_error("Could not open file");

    std::string line, colname;
    int val;

    // Read the column names
    if(fin.good())
    {
        std::getline(fin, line);
        std::stringstream ss(line);
        while(std::getline(ss, colname, ',')){
            result.push_back(colname);
            cout << colname << endl;
        }
    }

    while(std::getline(fin, line))
    {
        std::stringstream ss(line);
        int colIdx = 0;
        while(ss >> val){

            if(ss.peek() == ',') ss.ignore();
            colIdx++;
        }
    }
    fin.close();
    return result;
}
</code>```

when I tried to go through the vector, I didn't get a proper result. It showed only the column names.

```<code>for (int i = 0; i < vectorCsv.size(); ++i)
{
        cout << vectorCsv[i] << endl;
}
</code>```

I couldn't find whether the error is in read_csv() function or in the forloop.
Thank you for looking at this problem.

答:
In your while loop, you never pushed any values to your vector.

It looks like you have everything you need to read the csv into a vector right here. Only problem is you stopped at column names.

```<code>// Read the column names
    if(fin.good())
    {
        std::getline(fin, line);
        std::stringstream ss(line);
        while(std::getline(ss, colname, ',')){
            result.push_back(colname);
            cout << colname << endl;
        }
    }
</code>```

Try changing the code I copied above to:

```<code>// Read the column names
    while(std::getline(fin, line))
    {
        std::getline(fin, line);
        std::stringstream ss(line);
        while(std::getline(ss, colname, ',')){
            result.push_back(colname);
            cout << colname << endl;
        }
    }
</code>```

Body:
For example to add the following CSV data:

I am trying to add CSV file into a 2D array string vector and get the sum of each column. The following program didn't work properly,

vector<string> read_csv(string filename){

    vector<string> result;
    fstream fin;
    fin.open(filename, ios::in);

    if(!fin.is_open())
        throw std::runtime_error("Could not open file");

    std::string line, colname;
    int val;

    // Read the column names
    if(fin.good())
    {
        std::getline(fin, line);
        std::stringstream ss(line);
        while(std::getline(ss, colname, ',')){
            result.push_back(colname);
            cout << colname << endl;
        }
    }

    while(std::getline(fin, line))
    {
        std::stringstream ss(line);
        int colIdx = 0;
        while(ss >> val){

            if(ss.peek() == ',') ss.ignore();
            colIdx++;
        }
    }
    fin.close();
    return result;
}

when I tried to go through the vector, I didn't get a proper result. It showed only the column names.

for (int i = 0; i < vectorCsv.size(); ++i)
{
        cout << vectorCsv[i] << endl;
}

I couldn't find whether the error is in read_csv() function or in the forloop.
Thank you for looking at this problem.

Body:
In your while loop, you never pushed any values to your vector.

It looks like you have everything you need to read the csv into a vector right here. Only problem is you stopped at column names.

// Read the column names
    if(fin.good())
    {
        std::getline(fin, line);
        std::stringstream ss(line);
        while(std::getline(ss, colname, ',')){
            result.push_back(colname);
            cout << colname << endl;
        }
    }

Try changing the code I copied above to:

// Read the column names
    while(std::getline(fin, line))
    {
        std::getline(fin, line);
        std::stringstream ss(line);
        while(std::getline(ss, colname, ',')){
            result.push_back(colname);
            cout << colname << endl;
        }
    }


问:
Manually clearing an Android ViewModel?
Edit: This question is a bit out of date now that Google has given us the ability to scope ViewModel to navigation graphs. The better approach (rather than trying to clear activity-scoped models) would be to create specific navigation graphs for the right amount of screens, and scope to those.

With reference to the android.arch.lifecycle.ViewModel class.

ViewModel is scoped to the lifecycle of the UI component it relates to, so in a Fragment-based app, that will be the fragment lifecycle. This is a good thing.

In some cases one wants to share a ViewModel instance between multiple fragments. Specifically I am interested in the case where many screens relate to the same underlying data.

(The docs suggest similar approach when multiple related fragments are displayed on the same screen but this can be worked around by using a single host fragment as per answer below.)

This is discussed in the official ViewModel documentation:

ViewModels can also be used as a communication layer between different
  Fragments of an Activity. Each Fragment can acquire the ViewModel
  using the same key via their Activity. This allows communication
  between Fragments in a de-coupled fashion such that they never need to
  talk to the other Fragment directly.

In other words, to share information between fragments that represent different screens, the ViewModel should be scoped to the Activity lifecycle (and according to Android docs this can also be used in other shared instances).

Now in the new Jetpack Navigation pattern, it is recommended to use a "One Activity / Many Fragments" architecture. This means that the activity lives for the whole time the app is being used.

i.e. any shared ViewModel instances that are scoped to Activity lifecycle will never be cleared - the memory remains in constant use.

With a view to preserving memory and using as little as required at any point in time, it would be nice to be able to clear shared ViewModel instances when no longer required.

How can one manually clear a ViewModel from it's ViewModelStore or holder fragment?

答:
In my case, most of the things I observe are related to the Views, so I don't need to clear it in case the View gets destroyed (but not the Fragment).

In the case I need things like a LiveData that takes me to another Fragment (or that does the thing only once), I create a "consuming observer".

It can be done by extending MutableLiveData<T>:

```<code>fun <T> MutableLiveData<T>.observeConsuming(viewLifecycleOwner: LifecycleOwner, function: (T) -> Unit) {
    observe(viewLifecycleOwner, Observer<T> {
        function(it ?: return@Observer)
        value = null
    })
}
</code>```

and as soon as it's observed, it will clear from the LiveData.

Now you can call it like:

```<code>viewModel.navigation.observeConsuming(viewLifecycleOwner) {
    startActivity(Intent(this, LoginActivity::class.java))
}
</code>```

Title: Manually clearing an Android ViewModel?

Body:
In my case, most of the things I observe are related to the Views, so I don't need to clear it in case the View gets destroyed (but not the Fragment).

In the case I need things like a LiveData that takes me to another Fragment (or that does the thing only once), I create a "consuming observer".

It can be done by extending MutableLiveData<T>:

fun <T> MutableLiveData<T>.observeConsuming(viewLifecycleOwner: LifecycleOwner, function: (T) -> Unit) {
    observe(viewLifecycleOwner, Observer<T> {
        function(it ?: return@Observer)
        value = null
    })
}

and as soon as it's observed, it will clear from the LiveData.

Now you can call it like:

viewModel.navigation.observeConsuming(viewLifecycleOwner) {
    startActivity(Intent(this, LoginActivity::class.java))
}


问:
What is smart contract creation TX?
I am beginner and begun my Solidity programming with Truffle and work with Ganache. Whenever i open Contracts item in Ganache there is a CREATION TX there. What means that exactly. Thank you for giving me a clear explanation about that.

enter image description here

答:
When you deploy contract to Ethereum, you are changing the Ethereum state. Changing state will create a tx in Ethereum.

Take a look at this contract creation tx here.

0x495...6ab is the tx that changed Ethereum state that 'registered' the contract in Ethereum

0xbaa...3f4 is the contract address

So, based on your image, 0x6aC... is like 0x495...6ab (creation tx) and 0x926... is like 0xbaa...3f4 (contract address).

Title: What is smart contract creation TX?


问:
Storyboard.TargetProperty path contains nonanimatable property 'Text
Edit:

I'am trying to change Text of TextBox with event trigger, But I am getting a run time error like below.

System.InvalidOperationException: ''(0)' Storyboard.TargetProperty path contains nonanimatable property 'Text'.'

How to change Text with event trigger in WPF

```<code><Style x:Key="mystyle"
           TargetType="{x:Type TextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">

                    <StackPanel>
                        <ScrollViewer x:Name="PART_ContentHost"
                                      Focusable="false"
                                      HorizontalScrollBarVisibility="Auto"
                                      VerticalScrollBarVisibility="Auto" />

                        <ToggleButton HorizontalAlignment="Right"
                                VerticalAlignment="Top"
                                Content="search"
                                Name="searchButton">
                            <ToggleButton.Triggers>
                                <EventTrigger RoutedEvent="Button.Click">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <StringAnimationUsingKeyFrames Storyboard.Target="{Binding RelativeSource={RelativeSource AncestorType=TextBox,AncestorLevel=1}}"
                                                                           Storyboard.TargetProperty="(TextBox.Text)">
                                                <DiscreteStringKeyFrame KeyTime="00:00:00"
                                                                        Value="texttwo" />
                                            </StringAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </ToggleButton.Triggers>
                        </ToggleButton>
                    </StackPanel>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</code>```

答:
Edit: since you have totally rewritten the original question, my original answer does not make sense anymore, so I will rewrite it as well.

The problem is that in the EventTrigger you can only use a StoryBoard, and you cannot set the Text property of the TextBox from it. Therefore I think even if it can be achieved purely from WPF with a Template, the solution would be so complicated and ugly that it does not worth it, so I would suggest to create a user control.
If we make a step backward, what is your goal with this customized TextBox?

Edit2:
Finally I could do it with a custom animation class, but I still think that implementing this functionality with a user control is the good solution.
This is the custom animation class (for sake of simplicity I have put it in "local" namespace):

```<code>    public class TextBoxStringAnimation : StringAnimationBase {
        public static readonly DependencyProperty TextBoxToChangeProperty = DependencyProperty.Register(nameof(TextBoxToChange), typeof(TextBox), typeof(TextBoxStringAnimation));
        public TextBox TextBoxToChange {
            get => (TextBox)this.GetValue(TextBoxToChangeProperty);
            set => this.SetValue(TextBoxToChangeProperty, value);
        }
        public static readonly DependencyProperty NewTextProperty = DependencyProperty.Register(nameof(NewText), typeof(string), typeof(TextBoxStringAnimation));
        public string NewText {
            get => (string)this.GetValue(NewTextProperty);
            set => this.SetValue(NewTextProperty, value);
        }
        protected override Freezable CreateInstanceCore() => new TextBoxStringAnimation();
        protected override string GetCurrentValueCore(string defaultOriginValue, string defaultDestinationValue, AnimationClock animationClock) {
            TextBoxToChange.Text = NewText;
            return defaultDestinationValue;
        }
        protected override bool FreezeCore(bool isChecking) {
            return true;
        }
    }
</code>```

This inherits from StringAnimationBase, and you can set the TextBox and the NewText values directly. I have implemented both as dependency properties, so you can bind them.

When getting the current value (GetCurrentValueCore) it simply sets the Text property of the target TextBox. I had to override FreezeCore, because by default it has returned false, and that causes an InvelidOperationException when the animation is triggered.

So this is an animation that does not animate at all. :) I consider this an ugly solution, but without using a user control I could not come up with a better solution.

Below is the XAML markup. You must set the TargetProperty of the animation, although at the end it is not used - that is why I have specified Tag. If you use the Tag property of the textbox, check if the animation resets it or not.

```<code>        <Style x:Key="mystyle" TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <StackPanel>
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" />

                            <ToggleButton HorizontalAlignment="Right" VerticalAlignment="Top" Content="search" Name="searchButton">
                                <ToggleButton.Triggers>
                                    <EventTrigger RoutedEvent="Button.Click">
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <local:TextBoxStringAnimation TextBoxToChange="{Binding RelativeSource={RelativeSource AncestorType=TextBox,AncestorLevel=1}}"
                                                                              NewText="texttwo" Storyboard.TargetProperty="(TextBox.Tag)"/>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </ToggleButton.Triggers>
                            </ToggleButton>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
</code>```

Body:
Edit: since you have totally rewritten the original question, my original answer does not make sense anymore, so I will rewrite it as well.

The problem is that in the EventTrigger you can only use a StoryBoard, and you cannot set the Text property of the TextBox from it. Therefore I think even if it can be achieved purely from WPF with a Template, the solution would be so complicated and ugly that it does not worth it, so I would suggest to create a user control.
If we make a step backward, what is your goal with this customized TextBox?

Edit2:
Finally I could do it with a custom animation class, but I still think that implementing this functionality with a user control is the good solution.
This is the custom animation class (for sake of simplicity I have put it in "local" namespace):

    public class TextBoxStringAnimation : StringAnimationBase {
        public static readonly DependencyProperty TextBoxToChangeProperty = DependencyProperty.Register(nameof(TextBoxToChange), typeof(TextBox), typeof(TextBoxStringAnimation));
        public TextBox TextBoxToChange {
            get => (TextBox)this.GetValue(TextBoxToChangeProperty);
            set => this.SetValue(TextBoxToChangeProperty, value);
        }
        public static readonly DependencyProperty NewTextProperty = DependencyProperty.Register(nameof(NewText), typeof(string), typeof(TextBoxStringAnimation));
        public string NewText {
            get => (string)this.GetValue(NewTextProperty);
            set => this.SetValue(NewTextProperty, value);
        }
        protected override Freezable CreateInstanceCore() => new TextBoxStringAnimation();
        protected override string GetCurrentValueCore(string defaultOriginValue, string defaultDestinationValue, AnimationClock animationClock) {
            TextBoxToChange.Text = NewText;
            return defaultDestinationValue;
        }
        protected override bool FreezeCore(bool isChecking) {
            return true;
        }
    }

This inherits from StringAnimationBase, and you can set the TextBox and the NewText values directly. I have implemented both as dependency properties, so you can bind them.

When getting the current value (GetCurrentValueCore) it simply sets the Text property of the target TextBox. I had to override FreezeCore, because by default it has returned false, and that causes an InvelidOperationException when the animation is triggered.

So this is an animation that does not animate at all. :) I consider this an ugly solution, but without using a user control I could not come up with a better solution.

Below is the XAML markup. You must set the TargetProperty of the animation, although at the end it is not used - that is why I have specified Tag. If you use the Tag property of the textbox, check if the animation resets it or not.

        <Style x:Key="mystyle" TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <StackPanel>
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" />

                            <ToggleButton HorizontalAlignment="Right" VerticalAlignment="Top" Content="search" Name="searchButton">
                                <ToggleButton.Triggers>
                                    <EventTrigger RoutedEvent="Button.Click">
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <local:TextBoxStringAnimation TextBoxToChange="{Binding RelativeSource={RelativeSource AncestorType=TextBox,AncestorLevel=1}}"
                                                                              NewText="texttwo" Storyboard.TargetProperty="(TextBox.Tag)"/>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </ToggleButton.Triggers>
                            </ToggleButton>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


问:
Cannot install npm package (fsevents) on Mac OS Catalina
When I run npm install express --save this command, I'm getting an error. Can anyone please help me with this issue?

Error:

```<code>> fsevents@1.2.11 install /Users/delowar/Desktop/ytdl/node_modules/chokidar/node_modules/fsevents
> node-gyp rebuild

No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLILeo' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'.

gyp: No Xcode or CLT version detected!
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:351:16)
gyp ERR! stack     at ChildProcess.emit (events.js:321:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Darwin 19.2.0
gyp ERR! command "/usr/local/Cellar/node/13.7.0/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/delowar/Desktop/ytdl/node_modules/chokidar/node_modules/fsevents
gyp ERR! node -v v13.7.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok

> fsevents@1.2.11 install /Users/delowar/Desktop/ytdl/node_modules/jest-haste-map/node_modules/fsevents
> node-gyp rebuild

No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLILeo' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'.

gyp: No Xcode or CLT version detected!
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:351:16)
gyp ERR! stack     at ChildProcess.emit (events.js:321:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Darwin 19.2.0
gyp ERR! command "/usr/local/Cellar/node/13.7.0/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/delowar/Desktop/ytdl/node_modules/jest-haste-map/node_modules/fsevents
gyp ERR! node -v v13.7.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok
npm WARN tsutils@3.17.1 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 (node_modules/chokidar/node_modules/fsevents):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 (node_modules/jest-haste-map/node_modules/fsevents):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1
</code>```

Screenshot:

答:
I don't like to use the rm -rf command so an alternative option when you have brew installed with the "trash" formulae you can do on your terminal is :

```<code>trash Library/Developer/CommandLineTools
</code>```

And then

```<code>xcode-select --install
</code>```

If you don't have trash installed then you can do it on your terminal :
https://formulae.brew.sh/formula/trash

```<code>brew install trash
</code>```

Body:
When I run npm install express --save this command, I'm getting an error. Can anyone please help me with this issue?

Error:

> fsevents@1.2.11 install /Users/delowar/Desktop/ytdl/node_modules/chokidar/node_modules/fsevents
> node-gyp rebuild

No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLILeo' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'.

gyp: No Xcode or CLT version detected!
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:351:16)
gyp ERR! stack     at ChildProcess.emit (events.js:321:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Darwin 19.2.0
gyp ERR! command "/usr/local/Cellar/node/13.7.0/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/delowar/Desktop/ytdl/node_modules/chokidar/node_modules/fsevents
gyp ERR! node -v v13.7.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok

> fsevents@1.2.11 install /Users/delowar/Desktop/ytdl/node_modules/jest-haste-map/node_modules/fsevents
> node-gyp rebuild

No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLILeo' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'.

gyp: No Xcode or CLT version detected!
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:351:16)
gyp ERR! stack     at ChildProcess.emit (events.js:321:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Darwin 19.2.0
gyp ERR! command "/usr/local/Cellar/node/13.7.0/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/delowar/Desktop/ytdl/node_modules/jest-haste-map/node_modules/fsevents
gyp ERR! node -v v13.7.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok
npm WARN tsutils@3.17.1 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 (node_modules/chokidar/node_modules/fsevents):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 (node_modules/jest-haste-map/node_modules/fsevents):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1

Screenshot:

Body:
I don't like to use the rm -rf command so an alternative option when you have brew installed with the "trash" formulae you can do on your terminal is :

trash Library/Developer/CommandLineTools

And then

xcode-select --install

If you don't have trash installed then you can do it on your terminal :
https://formulae.brew.sh/formula/trash

brew install trash


问:
How to map over Applicative form?
I want to map over Applicative form.

The type of map-like function would be like below:

```<code>mapX :: (Applicative f) => (f a -> f b) -> f [a] -> f [b]
</code>```

used as:

```<code>result :: (Applicative f) => f [b]
result = mapX f xs
  where f  :: f a -> f b
        f = ...
        xs :: f[a]
        xs = ...
</code>```

As the background of this post, I try to write fluid simulation program using Applicative style referring to Paul Haduk's "The Haskell School of Expression", and I want to express the simulation with Applicative style as below:

```<code>x, v, a :: Sim VArray
x = x0 +: integral (v * dt)
v = v0 +: integral (a * dt)
a = (...calculate acceleration with x v...)

instance Applicative Sim where
  ...
</code>```

where Sim type means the process of simulation computation and VArray means Array of Vector (x,y,z). X, v a are the arrays of position, velocity and acceleration, respectively.

Mapping over Applicative form comes when definining a.

I've found one answer to my question.

After all, my question is "How to lift high-order functions (like map
:: (a -> b) -> [a] -> [b]) to the Applicative world?" and the answer
I've found is "To build them using lifted first-order functions."

For example, the "mapX" is defined with lifted first-order functions
(headA, tailA, consA, nullA, condA) as below:

```<code>mapX :: (f a -> f b) -> f [a] -> f [b]
mapX f xs0 = condA (nullA xs0) (pure []) (consA (f x) (mapA f xs))
 where
   x = headA xs0
   xs = tailA xs0

headA = liftA head

tailA = liftA tail

consA = liftA2 (:)

nullA = liftA null

condA b t e = liftA3 aux b t e
  where aux b t e = if b then t else e
</code>```

答:
If I'm thinking about this right, you can't do this just with an applicative functor; you'll need a monad.  If you have an Applicative—call it f—you have the following three functions available to you:

```<code>fmap  :: (a -> b) -> f a -> f b
pure  :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
</code>```

So, given some f :: f a -> f b, what can you do with it?  Well, if you have some xs :: [a], then you can map it across: map (f . pure) xs :: [f b].  And if you instead have fxs :: f [a], then you could instead do fmap (map (f . pure)) fxs :: f [f b].1  However, you're stuck at this point.  You want some function of type [f b] -> f [b], and possibly a function of type f (f b) -> f b; however, you can't define these on applicative functors (edit: actually, you can define the former; see the edit).  Why?  Well, if you look at fmap, pure, and <*>, you'll see that you have no way to get rid of (or rearrange) the f type constructor, so once you have [f a], you're stuck in that form.

Luckily, this is what monads are for: computations which can "change shape", so to speak.  If you have a monad m, then in addition to the above, you get two extra methods (and return as a synonym for pure):

```<code>(>>=) :: m a -> (a -> m b) -> m b
join  :: m (m a) -> m a
</code>```

While join is only defined in Control.Monad, it's just as fundamental as >>=, and can sometimes be clearer to think about.  Now we have the ability to define your [m b] -> m [b] function, or your m (m b) -> m b.  The latter one is just join; and the former is sequence, from the Prelude.  So, with monad m, you can define your mapX as

```<code>mapX :: Monad m => (m a -> m b) -> m [a] -> m [b]
mapX f mxs = mxs >>= sequence . map (f . return)
</code>```

However, this would be an odd way to define it.  There are a couple of other useful functions on monads in the prelude: mapM :: Monad m => (a -> m b) -> [a] -> m [b], which is equivalent to mapM f = sequence . map f; and (=<<) :: (a -> m b) -> m a -> m b, which is equivalent to flip (>>=).  Using those, I'd probably define mapX as

```<code>mapX :: Monad m => (m a -> m b) -> m [a] -> m [b]
mapX f mxs = mapM (f . return) =<< mxs
</code>```

Edit: Actually, my mistake: as John L kindly pointed out in a comment, Data.Traversable (which is a base package) supplies the function sequenceA :: (Applicative f, Traversable t) => t (f a) => f (t a); and since [] is an instance of Traversable, you can sequence an applicative functor.  Nevertheless, your type signature still requires join or =<<, so you're still stuck.  I would probably suggest rethinking your design; I think sclv probably has the right idea.

1: Or map (f . pure) <$> fxs, using the <$> synonym for fmap from Control.Applicative.

Title: How to map over Applicative form?

Body:
I want to map over Applicative form.

The type of map-like function would be like below:

mapX :: (Applicative f) => (f a -> f b) -> f [a] -> f [b]

used as:

result :: (Applicative f) => f [b]
result = mapX f xs
  where f  :: f a -> f b
        f = ...
        xs :: f[a]
        xs = ...

As the background of this post, I try to write fluid simulation program using Applicative style referring to Paul Haduk's "The Haskell School of Expression", and I want to express the simulation with Applicative style as below:

x, v, a :: Sim VArray
x = x0 +: integral (v * dt)
v = v0 +: integral (a * dt)
a = (...calculate acceleration with x v...)

instance Applicative Sim where
  ...

where Sim type means the process of simulation computation and VArray means Array of Vector (x,y,z). X, v a are the arrays of position, velocity and acceleration, respectively.

Mapping over Applicative form comes when definining a.

I've found one answer to my question.

After all, my question is "How to lift high-order functions (like map
:: (a -> b) -> [a] -> [b]) to the Applicative world?" and the answer
I've found is "To build them using lifted first-order functions."

For example, the "mapX" is defined with lifted first-order functions
(headA, tailA, consA, nullA, condA) as below:

mapX :: (f a -> f b) -> f [a] -> f [b]
mapX f xs0 = condA (nullA xs0) (pure []) (consA (f x) (mapA f xs))
 where
   x = headA xs0
   xs = tailA xs0

headA = liftA head

tailA = liftA tail

consA = liftA2 (:)

nullA = liftA null

condA b t e = liftA3 aux b t e
  where aux b t e = if b then t else e

Body:
If I'm thinking about this right, you can't do this just with an applicative functor; you'll need a monad.  If you have an Applicative—call it f—you have the following three functions available to you:

fmap  :: (a -> b) -> f a -> f b
pure  :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b

So, given some f :: f a -> f b, what can you do with it?  Well, if you have some xs :: [a], then you can map it across: map (f . pure) xs :: [f b].  And if you instead have fxs :: f [a], then you could instead do fmap (map (f . pure)) fxs :: f [f b].1  However, you're stuck at this point.  You want some function of type [f b] -> f [b], and possibly a function of type f (f b) -> f b; however, you can't define these on applicative functors (edit: actually, you can define the former; see the edit).  Why?  Well, if you look at fmap, pure, and <*>, you'll see that you have no way to get rid of (or rearrange) the f type constructor, so once you have [f a], you're stuck in that form.

Luckily, this is what monads are for: computations which can "change shape", so to speak.  If you have a monad m, then in addition to the above, you get two extra methods (and return as a synonym for pure):

(>>=) :: m a -> (a -> m b) -> m b
join  :: m (m a) -> m a

While join is only defined in Control.Monad, it's just as fundamental as >>=, and can sometimes be clearer to think about.  Now we have the ability to define your [m b] -> m [b] function, or your m (m b) -> m b.  The latter one is just join; and the former is sequence, from the Prelude.  So, with monad m, you can define your mapX as

mapX :: Monad m => (m a -> m b) -> m [a] -> m [b]
mapX f mxs = mxs >>= sequence . map (f . return)

However, this would be an odd way to define it.  There are a couple of other useful functions on monads in the prelude: mapM :: Monad m => (a -> m b) -> [a] -> m [b], which is equivalent to mapM f = sequence . map f; and (=<<) :: (a -> m b) -> m a -> m b, which is equivalent to flip (>>=).  Using those, I'd probably define mapX as

mapX :: Monad m => (m a -> m b) -> m [a] -> m [b]
mapX f mxs = mapM (f . return) =<< mxs

Edit: Actually, my mistake: as John L kindly pointed out in a comment, Data.Traversable (which is a base package) supplies the function sequenceA :: (Applicative f, Traversable t) => t (f a) => f (t a); and since [] is an instance of Traversable, you can sequence an applicative functor.  Nevertheless, your type signature still requires join or =<<, so you're still stuck.  I would probably suggest rethinking your design; I think sclv probably has the right idea.

1: Or map (f . pure) <$> fxs, using the <$> synonym for fmap from Control.Applicative.


问:
How can I include xml configuration in logback.groovy
I'm writing a Spring Boot app and need the flexibility of controlling my logback configuration using Groovy.  In Spring Boot all I have to do is create src/main/resources/logback.groovy and it is automatically used for configuration.

What I would like to do though is start with Spring Boot's default logback configuration, and just override or modify settings as needed.

If I were using logback.xml instead of logback.groovy I could do something like the following.

```<code><?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>
</configuration>
</code>```

Is there something similar to the include line above that I can use in logback.groovy?  I can look at the contents of base.xml and it's other included files to see how to replicate this manually, but it would add a bit of boilerplate code I'd like to avoid.

Thanks for any help you can provide.

答:
Contrary to the documentation stating that:

Everything you can do using XML in configuration files, you can do in
  Groovy with a much shorter syntax.

include is not possible with Groovy out-of-the-box. However, thanks to a bug ticket that was opened in 2014, there are a couple of workarounds. I am including them here (slightly edited), but all credit goes to "Yih Tsern" from the original JIRA bug:

logback.groovy

```<code>include(new File('logback-fragment.groovy'))

root(DEBUG, ["CONSOLE"])

def include(File fragmentFile) {
  GroovyShell shell = new GroovyShell(
    getClass().classLoader,
    binding,
    new org.codehaus.groovy.control.CompilerConfiguration(scriptBaseClass: groovy.util.DelegatingScript.name))

  Script fragment = shell.parse(fragmentFile.text)
  fragment.setDelegate(this)

  fragment.run()
}
</code>```

logback-fragment.groovy:

```<code>// NOTE: No auto-import
import ch.qos.logback.core.*
import ch.qos.logback.classic.encoder.*

appender("CONSOLE", ConsoleAppender) {
  encoder(PatternLayoutEncoder) {
    pattern = "%d [%thread] %level %mdc %logger{35} - %msg%n"
  }
}

</code>```

Given the workaround and a pull-request to add the feature, I'm not sure why the functionality hasn't been added to Logback core yet.

Body:
I'm writing a Spring Boot app and need the flexibility of controlling my logback configuration using Groovy.  In Spring Boot all I have to do is create src/main/resources/logback.groovy and it is automatically used for configuration.

What I would like to do though is start with Spring Boot's default logback configuration, and just override or modify settings as needed.

If I were using logback.xml instead of logback.groovy I could do something like the following.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>
</configuration>

Is there something similar to the include line above that I can use in logback.groovy?  I can look at the contents of base.xml and it's other included files to see how to replicate this manually, but it would add a bit of boilerplate code I'd like to avoid.

Thanks for any help you can provide.

Body:
Contrary to the documentation stating that:

Everything you can do using XML in configuration files, you can do in
  Groovy with a much shorter syntax.

include is not possible with Groovy out-of-the-box. However, thanks to a bug ticket that was opened in 2014, there are a couple of workarounds. I am including them here (slightly edited), but all credit goes to "Yih Tsern" from the original JIRA bug:

logback.groovy

include(new File('logback-fragment.groovy'))

root(DEBUG, ["CONSOLE"])

def include(File fragmentFile) {
  GroovyShell shell = new GroovyShell(
    getClass().classLoader,
    binding,
    new org.codehaus.groovy.control.CompilerConfiguration(scriptBaseClass: groovy.util.DelegatingScript.name))

  Script fragment = shell.parse(fragmentFile.text)
  fragment.setDelegate(this)

  fragment.run()
}

logback-fragment.groovy:

// NOTE: No auto-import
import ch.qos.logback.core.*
import ch.qos.logback.classic.encoder.*

appender("CONSOLE", ConsoleAppender) {
  encoder(PatternLayoutEncoder) {
    pattern = "%d [%thread] %level %mdc %logger{35} - %msg%n"
  }
}

Given the workaround and a pull-request to add the feature, I'm not sure why the functionality hasn't been added to Logback core yet.


问:
Get parent page node for component in magnolia
I configured a listener in the magnolia observation module that uses info.magnolia.module.observation.commands.RestrictToNodeTypeEventListener for mgnl:component node type. When a NODE_ADDED, NODE_MOVED, NODE_REMOVED, PROPERTY_ADDED, PROPERTY_CHANGED and PROPERTY_REMOVED event happens on the mgnl:component it triggers a custom command class.
In the custom command class the node is of type mgnl:component and the path is for example:  node at path /aalongpubname/de/test-page-for-observer/content/01 but I need to get the parent mgnl:page node for it (ie. /aalongpubname/de/test-page-for-observer). The component could also be nested ie. not at the first level. What is the easiest way to do this?

答: I believe one of the way to do this is: NodeUtil.getNearestAncestorOfType(node,"mgnl:page").getName()


问:
Typescript treats boolean like true | false when strictNullCheck is off
I am having the following typescript code:

```<code>type PossiblePromise<T> = T extends Promise<infer U>
  ? T | U
  : T | Promise<T>;
type Z = PossiblePromise<boolean>;

const a = "a"
const f: () => Z = () => Promise.resolve(!!a);
</code>```

With strictNullChecks set to false.

The resulting type Z is boolean | Promise<false> | Promise<true>, but I would expect it to be boolean | Promise<boolean>. So last line produces an error:

```<code>Type 'Promise<boolean>' is not assignable to type 'boolean | Promise<false> | Promise<true>'.
  Type 'Promise<boolean>' is not assignable to type 'Promise<false>'.
    Type 'boolean' is not assignable to type 'false'.(2322)
input.ts(7, 10): The expected type comes from the return type of this signature.
</code>```

Any ideas why typescript behaves like this treating boolean as true | false which should not be the case with strictNullChecks off.

How is it possible to achieve PossiblePromise<T> type that works with boolean correctly?

https://www.typescriptlang.org/play/?strictNullChecks=false&ssl=6&ssc=1&pln=7&pc=47#code/C4TwDgpgBACg9gZwQSwEYBsIwE5wLbIIQA8AKgHxQC8UpUEAHsBAHYAmCsuBRxyLAMwjYoAVXIBYAFBQoAflpQAPmOmyAXIpU58hEhQDc00JCgAtarEQoMWbnuKo4cTAEMW5I1OkBjOCwRgKFdLACJXUN9-QKgBTQAKAEpqSgsaJJSuXSIAOmwIBBcANwh4gEIy10SDIA

答:
You can fix it by lifting the Promise type. The issue is that boolean is a union type, and conditional types are automatically distributed over union types. Consider following code:

```<code>type PossiblePromise<T, Y = Promise<T>> = T extends Promise<infer U>
  ? T | U
  : T | Y;
type Z = PossiblePromise<boolean>;

const a = "a"
const f: () => Z = () => Promise.resolve(!!a);
</code>```

As you can see Y is lifted outside condition type, and is used only when needed.

BTW - using Boolean is not a fix of your type definition, it is only different type which works well with your implementation (Boolean is not a union type) - consider more about Boolean. It also means that if you would use any other union type it would have the same wrong effect, check below the difference

```<code>type T = "a" | "b";
// previous result type would be:
type PreviousResult = "a" | "b" | Promise<"a"> | Promise<"b">
// after the change it will be:
type Result = "a" | "b" | Promise<"a" | "b">
</code>```

Body:
I am having the following typescript code:

type PossiblePromise<T> = T extends Promise<infer U>
  ? T | U
  : T | Promise<T>;
type Z = PossiblePromise<boolean>;

const a = "a"
const f: () => Z = () => Promise.resolve(!!a);

With strictNullChecks set to false.

The resulting type Z is boolean | Promise<false> | Promise<true>, but I would expect it to be boolean | Promise<boolean>. So last line produces an error:

Type 'Promise<boolean>' is not assignable to type 'boolean | Promise<false> | Promise<true>'.
  Type 'Promise<boolean>' is not assignable to type 'Promise<false>'.
    Type 'boolean' is not assignable to type 'false'.(2322)
input.ts(7, 10): The expected type comes from the return type of this signature.

Any ideas why typescript behaves like this treating boolean as true | false which should not be the case with strictNullChecks off.

How is it possible to achieve PossiblePromise<T> type that works with boolean correctly?

https://www.typescriptlang.org/play/?strictNullChecks=false&ssl=6&ssc=1&pln=7&pc=47#code/C4TwDgpgBACg9gZwQSwEYBsIwE5wLbIIQA8AKgHxQC8UpUEAHsBAHYAmCsuBRxyLAMwjYoAVXIBYAFBQoAflpQAPmOmyAXIpU58hEhQDc00JCgAtarEQoMWbnuKo4cTAEMW5I1OkBjOCwRgKFdLACJXUN9-QKgBTQAKAEpqSgsaJJSuXSIAOmwIBBcANwh4gEIy10SDIA

Body:
You can fix it by lifting the Promise type. The issue is that boolean is a union type, and conditional types are automatically distributed over union types. Consider following code:

type PossiblePromise<T, Y = Promise<T>> = T extends Promise<infer U>
  ? T | U
  : T | Y;
type Z = PossiblePromise<boolean>;

const a = "a"
const f: () => Z = () => Promise.resolve(!!a);

As you can see Y is lifted outside condition type, and is used only when needed.

BTW - using Boolean is not a fix of your type definition, it is only different type which works well with your implementation (Boolean is not a union type) - consider more about Boolean. It also means that if you would use any other union type it would have the same wrong effect, check below the difference

type T = "a" | "b";
// previous result type would be:
type PreviousResult = "a" | "b" | Promise<"a"> | Promise<"b">
// after the change it will be:
type Result = "a" | "b" | Promise<"a" | "b">


问:
Python logging: Why is __init__ called twice?
I am trying to use python logging with a config file and an own handler. This works to some degree. What really puzzle me is __init__ being called twice and __del__ being called once. When I remove the whole config file stuff and create the handler directly within the code __init__ is called once and __del__ is never called.

My questions:

Why is __init__ called twice?

Why is __del__ called less often than __init__?

The code:

```<code>#!/bin/env python

import logging
import logging.handlers
import logging.config

class Test1TimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
    def __init__(self,filename):
        print "init called"
        logging.handlers.TimedRotatingFileHandler.__init__(self,filename, when='S', interval=86400, backupCount=8, encoding=None)

    def __del__(self):
        print "del called"
        if hasattr(logging.handlers.TimedRotatingFileHandler,"__del__"):
            logging.handlers.TimedRotatingFileHandler.__del__(self)

logging.config.fileConfig('/root/test1.conf')
logger = logging.getLogger("test1")
</code>```

The config file:

```<code>[formatters]
keys: simple

[handlers]
keys: file

[loggers]
keys: root

[formatter_simple]
format: "%(message)s"

[handler_file]
class: test1.Test1TimedRotatingFileHandler
args: ("/root/test1.log",)
level=INFO

[logger_root]
level: INFO
handlers: file
qualname: test1
</code>```

Output looks like this:

```<code>init called
init called
del called
</code>```

Using the debugger to get the stack trace as suggested by Sentinal reveals this:

First call:

```<code>> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) where
  /root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(156)_install_handlers()
-> klass = _resolve(klass)
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(94)_resolve()
-> found = __import__(used)
  /root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(159)_install_handlers()
-> h = klass(*args)
> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) c
init called
</code>```

Second call:

```<code>> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) w
  /root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(159)_install_handlers()
-> h = klass(*args)
> /root/test1.py(12)__init__()
-> print "init called"
</code>```

答:
Why is init called twice?

If you follow the code of the logging module, you'll see that when you're loading the logging configuration file, it instantiates all the handlers (First instantiation).

In your code, you declare your handler like test1.Test1TimedRotatingFileHandler, so when it try to import your handler, it parses the code in the test1 module... so it recreates the handler !!

Corrected code will guard using __name__ == '__main__':

```<code>#!/bin/env python

import logging
import logging.handlers
import logging.config

class Test1TimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
    def __init__(self,filename):
        print "init called"
        logging.handlers.TimedRotatingFileHandler.__init__(self,filename, when='S', interval=86400, backupCount=8, encoding=None)

    def __del__(self):
        print "del called"
        if hasattr(logging.handlers.TimedRotatingFileHandler,"__del__"):
            logging.handlers.TimedRotatingFileHandler.__del__(self)

if __name__ == "__main__":
    logging.config.fileConfig('./test1.conf')
    logger = logging.getLogger("test1")
</code>```

2 . Why is del called less often than init?

In general, the __del__ operator is called when-python-wants, more exactly, it is called when the garbage collector decides to garbage-collect the object; this is not necessarily just after you release it.

Body:
I am trying to use python logging with a config file and an own handler. This works to some degree. What really puzzle me is __init__ being called twice and __del__ being called once. When I remove the whole config file stuff and create the handler directly within the code __init__ is called once and __del__ is never called.

My questions:

Why is __init__ called twice?

Why is __del__ called less often than __init__?

The code:

#!/bin/env python

import logging
import logging.handlers
import logging.config

class Test1TimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
    def __init__(self,filename):
        print "init called"
        logging.handlers.TimedRotatingFileHandler.__init__(self,filename, when='S', interval=86400, backupCount=8, encoding=None)

    def __del__(self):
        print "del called"
        if hasattr(logging.handlers.TimedRotatingFileHandler,"__del__"):
            logging.handlers.TimedRotatingFileHandler.__del__(self)

logging.config.fileConfig('/root/test1.conf')
logger = logging.getLogger("test1")

The config file:

[formatters]
keys: simple

[handlers]
keys: file

[loggers]
keys: root

[formatter_simple]
format: "%(message)s"

[handler_file]
class: test1.Test1TimedRotatingFileHandler
args: ("/root/test1.log",)
level=INFO

[logger_root]
level: INFO
handlers: file
qualname: test1

Output looks like this:

init called
init called
del called

Using the debugger to get the stack trace as suggested by Sentinal reveals this:

First call:

> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) where
  /root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(156)_install_handlers()
-> klass = _resolve(klass)
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(94)_resolve()
-> found = __import__(used)
  /root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(159)_install_handlers()
-> h = klass(*args)
> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) c
init called

Second call:

> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) w
  /root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
  /usr/local/python/2.6.4/lib/python2.6/logging/config.py(159)_install_handlers()
-> h = klass(*args)
> /root/test1.py(12)__init__()
-> print "init called"

Body:
Why is init called twice?

If you follow the code of the logging module, you'll see that when you're loading the logging configuration file, it instantiates all the handlers (First instantiation).

In your code, you declare your handler like test1.Test1TimedRotatingFileHandler, so when it try to import your handler, it parses the code in the test1 module... so it recreates the handler !!

Corrected code will guard using __name__ == '__main__':

#!/bin/env python

import logging
import logging.handlers
import logging.config

class Test1TimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
    def __init__(self,filename):
        print "init called"
        logging.handlers.TimedRotatingFileHandler.__init__(self,filename, when='S', interval=86400, backupCount=8, encoding=None)

    def __del__(self):
        print "del called"
        if hasattr(logging.handlers.TimedRotatingFileHandler,"__del__"):
            logging.handlers.TimedRotatingFileHandler.__del__(self)

if __name__ == "__main__":
    logging.config.fileConfig('./test1.conf')
    logger = logging.getLogger("test1")

2 . Why is del called less often than init?

In general, the __del__ operator is called when-python-wants, more exactly, it is called when the garbage collector decides to garbage-collect the object; this is not necessarily just after you release it.