问:
how do I flatten json data (from a list) into separate columns in python?
I have this pandas dataframe

```<code>    asin        salesMetricsByAsin
0   B001U81442  [{'reportingDate': '2021-01-20', 'salesMetricsByDate': {'shippedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 5.25, 'currencyCode': 'USD'}, 'shippedUnits': 1, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'orderedUnits': 1, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': 84, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 3, 'conversionRate': 0.3333}}, {'reportingDate': '2021-01-21', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}]    AMAZON_RETAIL
1   B003SVYRNE  [{'reportingDate': '2021-06-28', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 1, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}, {'reportingDate': '2021-06-29', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}]    AMAZON_RE

</code>```

How do I flatten the list in the salesMetricsByAsin column into separate columns?  I have tried several methods including from_dict() and json_normalize() but I am running into issues since the json data is in a list.

答:
Your question is similar in structure to this one. My answer is adjusted from one of the answers there.

You need to break the problem down into the following steps:

convert the rows in 'salesMetricsByAsin' containing a list of jsons to seperate rows

flatten the json into seperate columns

For step 1, you can use df.explode('column_name'). For step 2, you can indeed use json_normalize()

Code:

```<code>import pandas as pd

d = {
    'asin' : ['B001U81442', 'B003SVYRNE'],
    'salesMetricsByAsin' : [
        [{'reportingDate': '2021-01-20', 'salesMetricsByDate': {'shippedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 5.25, 'currencyCode': 'USD'}, 'shippedUnits': 1, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'orderedUnits': 1, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': 84, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 3, 'conversionRate': 0.3333}},
         {'reportingDate': '2021-01-21', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}],
        [{'reportingDate': '2021-06-28', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 1, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}},
         {'reportingDate': '2021-06-29', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}],
    ],
    '3rd_col' : ['AMAZON_RETAIL', 'AMAZON_RE']
}
df = pd.DataFrame(d)
df_2 = df.explode('salesMetricsByAsin').reset_index()
df_2.join(pd.json_normalize(df_2.pop('salesMetricsByAsin')))
</code>```

Output:

```<code>index   asin    3rd_col reportingDate   salesMetricsByDate.shippedRevenue.amount    salesMetricsByDate.shippedRevenue.currencyCode  salesMetricsByDate.shippedCOGS.amount   salesMetricsByDate.shippedCOGS.currencyCode salesMetricsByDate.shippedUnits salesMetricsByDate.customerReturns  ... salesMetricsByDate.orderedRevenue.amount    salesMetricsByDate.orderedRevenue.currencyCode  salesMetricsByDate.orderedUnits salesMetricsByDate.preOrderedUnits  salesMetricsByDate.shippedSubcategorySalesRank  salesMetricsByDate.orderedSubcategorySalesRank  salesMetricsByDate.replenishableOutOfStock  salesMetricsByDate.lostBuyBox   salesMetricsByDate.glanceViews  salesMetricsByDate.conversionRate
0   0   B001U81442  AMAZON_RETAIL   2021-01-20  9.99    USD 5.25    USD 1   0   ... 9.99    USD 1   0   84.0    83.0    0.0 0.0 3   0.3333
1   0   B001U81442  AMAZON_RETAIL   2021-01-21  0.00    USD 0.00    USD 0   0   ... 0.00    USD 0   0   NaN 83.0    0.0 0.0 0   0.0000
2   1   B003SVYRNE  AMAZON_RE   2021-06-28  0.00    USD 0.00    USD 0   1   ... 0.00    USD 0   0   NaN NaN 0.0 0.0 0   0.0000
3   1   B003SVYRNE  AMAZON_RE   2021-06-29  0.00    USD 0.00    USD 0   0   ... 0.00    USD 0   0   NaN NaN 0.0 0.0 0   0.0000
</code>```

Body:
I have this pandas dataframe

    asin        salesMetricsByAsin
0   B001U81442  [{'reportingDate': '2021-01-20', 'salesMetricsByDate': {'shippedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 5.25, 'currencyCode': 'USD'}, 'shippedUnits': 1, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'orderedUnits': 1, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': 84, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 3, 'conversionRate': 0.3333}}, {'reportingDate': '2021-01-21', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}]    AMAZON_RETAIL
1   B003SVYRNE  [{'reportingDate': '2021-06-28', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 1, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}, {'reportingDate': '2021-06-29', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}]    AMAZON_RE

How do I flatten the list in the salesMetricsByAsin column into separate columns?  I have tried several methods including from_dict() and json_normalize() but I am running into issues since the json data is in a list.

Body:
Your question is similar in structure to this one. My answer is adjusted from one of the answers there.

You need to break the problem down into the following steps:

convert the rows in 'salesMetricsByAsin' containing a list of jsons to seperate rows

flatten the json into seperate columns

For step 1, you can use df.explode('column_name'). For step 2, you can indeed use json_normalize()

Code:

import pandas as pd

d = {
    'asin' : ['B001U81442', 'B003SVYRNE'],
    'salesMetricsByAsin' : [
        [{'reportingDate': '2021-01-20', 'salesMetricsByDate': {'shippedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 5.25, 'currencyCode': 'USD'}, 'shippedUnits': 1, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'orderedUnits': 1, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': 84, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 3, 'conversionRate': 0.3333}},
         {'reportingDate': '2021-01-21', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}],
        [{'reportingDate': '2021-06-28', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 1, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}},
         {'reportingDate': '2021-06-29', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}],
    ],
    '3rd_col' : ['AMAZON_RETAIL', 'AMAZON_RE']
}
df = pd.DataFrame(d)
df_2 = df.explode('salesMetricsByAsin').reset_index()
df_2.join(pd.json_normalize(df_2.pop('salesMetricsByAsin')))

Output:

index   asin    3rd_col reportingDate   salesMetricsByDate.shippedRevenue.amount    salesMetricsByDate.shippedRevenue.currencyCode  salesMetricsByDate.shippedCOGS.amount   salesMetricsByDate.shippedCOGS.currencyCode salesMetricsByDate.shippedUnits salesMetricsByDate.customerReturns  ... salesMetricsByDate.orderedRevenue.amount    salesMetricsByDate.orderedRevenue.currencyCode  salesMetricsByDate.orderedUnits salesMetricsByDate.preOrderedUnits  salesMetricsByDate.shippedSubcategorySalesRank  salesMetricsByDate.orderedSubcategorySalesRank  salesMetricsByDate.replenishableOutOfStock  salesMetricsByDate.lostBuyBox   salesMetricsByDate.glanceViews  salesMetricsByDate.conversionRate
0   0   B001U81442  AMAZON_RETAIL   2021-01-20  9.99    USD 5.25    USD 1   0   ... 9.99    USD 1   0   84.0    83.0    0.0 0.0 3   0.3333
1   0   B001U81442  AMAZON_RETAIL   2021-01-21  0.00    USD 0.00    USD 0   0   ... 0.00    USD 0   0   NaN 83.0    0.0 0.0 0   0.0000
2   1   B003SVYRNE  AMAZON_RE   2021-06-28  0.00    USD 0.00    USD 0   1   ... 0.00    USD 0   0   NaN NaN 0.0 0.0 0   0.0000
3   1   B003SVYRNE  AMAZON_RE   2021-06-29  0.00    USD 0.00    USD 0   0   ... 0.00    USD 0   0   NaN NaN 0.0 0.0 0   0.0000


来源: stackexchange

问:
Using float type to represent money amount without arithmetic operations
I understand float/double are bad choices for storing money amounts. When you do some arithmetic operations, you may lose precision.

Is it safe to use float just to represent the amount when arithmetic operations are not performed on that number? For example, mapping amount from API to float type.

答: I know a commercial company that uses some kind of double variables in their database to represent money count, and I even know cases where their values differ by 245,20 -> 245,1999999999999999999999 because of that so that should be fine for a learning purpose at least. Just use math.round(value, precision(2)) or whatever math functions / methods for rounding a value java has.</p>

ContentLicense: CC BY-SA 4.0


问:
Nlog JsonLayout does not include LogEvent Properties
I try to implement this example https://github.com/nlog/nlog/wiki/JsonLayout#nested-json-with-structured-logging but output in my case has no data in eventProperties field.
NLog config:

```<code>    <?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="TRACE" internalLogFile="c:\temp\nlog-internal.log">

  <variable name="LogLevel" value="DEBUG"/>
  <variable name="brief" value="${longdate} | ${level} | ${logger} | ${message}"/>
  <variable name="verbose" value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} | ${message}"/>
  <variable name="logLifetime"  value="3"/>

  <targets>
    <target name="Global" xsi:type="File" fileName="Logs/GlobalLog.txt"  archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/IPSlog.{#}.zip" archiveDateFormat="yyyy-MM-dd"
        maxArchiveDays="${logLifetime}"
       archiveEvery="Day" >
    <layout xsi:type="JsonLayout">
      <attribute name="time" layout="${longdate}" />
      <attribute name="level" layout="${level:upperCase=true}"/>
      <attribute name="message" layout="${message}" />
      <attribute name="eventProperties" encode="false" >
        <layout xsi:type='JsonLayout' includeEventProperties="true"  maxRecursionLimit="2"/>
      </attribute>
    </layout>
    </target>
    <target name="Color" xsi:type="Console">
      <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="message" layout="${message}" />
        <attribute name="eventProperties" encode="false" >
          <layout xsi:type='JsonLayout' includeEventProperties="true"  maxRecursionLimit="2"/>
        </attribute>
      </layout>
    </target>
  </targets>

  <rules>
    <logger name="Global" minlevel="${LogLevel}"  writeTo="Global,Color" />

  </rules>
</nlog>
</code>```

c#:

```<code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    [Serializable]
    public class TestObject
    {
        public string A { get; set; }
        public int B { get; set; }

        public override string ToString() { return A; }
    }

    class Program
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetLogger("Global");
        static void Main(string[] args)
        {
            var testObj = new TestObject
            {
                A = "AlphaObject",
                B = 2
            };

            var testObjB = new TestObject
            {
                A = "BetaObject",
                B = 3
            };

            Logger.Info("First: {alpha}, Second: {beta}", testObj, testObjB);

            Console.ReadLine();
        }
    }
}
</code>```

My json output: { "time": "2022-02-09 14:21:27.7096", "level": "INFO", "message": "First: AlphaObject, Second: BetaObject", "eventProperties": {  } }

Json output from example:

```<code>{
  "time": "2018-04-02 02:00:00.2349",
  "level": "Info",
  "message": "First: AlphaObject, Second: BetaObject",
  "eventProperties": {
    "alpha": {
      "A": "AlphaObject",
      "B": 2
    },
    "beta": {
      "A": "BetaObject",
      "B": 3
    }
  }
}
</code>```

I use last nuget package of nlog and this app uses .NET framework 4.5.2. Also nlog schema highlights one of layout parameters

and nlog-internal has next warn:

```<code>Warn Error has been raised. Exception: NLog.NLogConfigurationException: Error when setting value 'true' for property 'includeEventProperties' on element 'NLog.Config.NLogXmlElement' ---> System.NotSupportedException: Parameter includeEventProperties not supported on JsonLayout
   в NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
   в NLog.Config.LoggingConfigurationParser.ConfigureObjectFromAttributes(Object targetObject, ILoggingConfigurationElement element, Boolean ignoreType)
   --- Конец трассировки внутреннего стека исключений ---
</code>```

答:
IncludeEventProperties is for NLog 5.0, when using older version of NLog then one should use IncludeAllProperties (Older than NLog v4.7.14)

      <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="message" layout="${message}" />
        <attribute name="eventProperties" encode="false" >
          <layout xsi:type='JsonLayout' includeAllProperties="true"  maxRecursionLimit="2"/>
        </attribute>
      </layout>
```

Reason for changing the name is because NLog 5.0 also introduces the new option IncludeScopeProperties (And then IncludeAllProperties becomes confusing).

Body:
I try to implement this example https://github.com/nlog/nlog/wiki/JsonLayout#nested-json-with-structured-logging but output in my case has no data in eventProperties field.
NLog config:

    <?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="TRACE" internalLogFile="c:\temp\nlog-internal.log">

  <variable name="LogLevel" value="DEBUG"/>
  <variable name="brief" value="${longdate} | ${level} | ${logger} | ${message}"/>
  <variable name="verbose" value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} | ${message}"/>
  <variable name="logLifetime"  value="3"/>

  <targets>
    <target name="Global" xsi:type="File" fileName="Logs/GlobalLog.txt"  archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/IPSlog.{#}.zip" archiveDateFormat="yyyy-MM-dd"
        maxArchiveDays="${logLifetime}"
       archiveEvery="Day" >
    <layout xsi:type="JsonLayout">
      <attribute name="time" layout="${longdate}" />
      <attribute name="level" layout="${level:upperCase=true}"/>
      <attribute name="message" layout="${message}" />
      <attribute name="eventProperties" encode="false" >
        <layout xsi:type='JsonLayout' includeEventProperties="true"  maxRecursionLimit="2"/>
      </attribute>
    </layout>
    </target>
    <target name="Color" xsi:type="Console">
      <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="message" layout="${message}" />
        <attribute name="eventProperties" encode="false" >
          <layout xsi:type='JsonLayout' includeEventProperties="true"  maxRecursionLimit="2"/>
        </attribute>
      </layout>
    </target>
  </targets>

  <rules>
    <logger name="Global" minlevel="${LogLevel}"  writeTo="Global,Color" />

  </rules>
</nlog>

c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    [Serializable]
    public class TestObject
    {
        public string A { get; set; }
        public int B { get; set; }

        public override string ToString() { return A; }
    }

    class Program
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetLogger("Global");
        static void Main(string[] args)
        {
            var testObj = new TestObject
            {
                A = "AlphaObject",
                B = 2
            };

            var testObjB = new TestObject
            {
                A = "BetaObject",
                B = 3
            };

            Logger.Info("First: {alpha}, Second: {beta}", testObj, testObjB);

            Console.ReadLine();
        }
    }
}

My json output: { "time": "2022-02-09 14:21:27.7096", "level": "INFO", "message": "First: AlphaObject, Second: BetaObject", "eventProperties": {  } }

Json output from example:

{
  "time": "2018-04-02 02:00:00.2349",
  "level": "Info",
  "message": "First: AlphaObject, Second: BetaObject",
  "eventProperties": {
    "alpha": {
      "A": "AlphaObject",
      "B": 2
    },
    "beta": {
      "A": "BetaObject",
      "B": 3
    }
  }
}

I use last nuget package of nlog and this app uses .NET framework 4.5.2. Also nlog schema highlights one of layout parameters

and nlog-internal has next warn:

Warn Error has been raised. Exception: NLog.NLogConfigurationException: Error when setting value 'true' for property 'includeEventProperties' on element 'NLog.Config.NLogXmlElement' ---> System.NotSupportedException: Parameter includeEventProperties not supported on JsonLayout
   в NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
   в NLog.Config.LoggingConfigurationParser.ConfigureObjectFromAttributes(Object targetObject, ILoggingConfigurationElement element, Boolean ignoreType)
   --- Конец трассировки внутреннего стека исключений ---

Body:
IncludeEventProperties is for NLog 5.0, when using older version of NLog then one should use IncludeAllProperties (Older than NLog v4.7.14)

      <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="message" layout="${message}" />
        <attribute name="eventProperties" encode="false" >
          <layout xsi:type='JsonLayout' includeAllProperties="true"  maxRecursionLimit="2"/>
        </attribute>
      </layout>

Reason for changing the name is because NLog 5.0 also introduces the new option IncludeScopeProperties (And then IncludeAllProperties becomes confusing).


问:
Manually moving a state file to a different backend
I recreated a state file for an existing infrastructure using a third party tool i.e. terraformer. Now I want o move the .tfstate to another azurerm back-end and manage from there.

If I just copy the file i.e. mystate.tfstate from local to inside storage account container with the same file name/key as in the backend configurations will it work or do I need to do something else to achieve it?

I don't want to risk the state file or infrastructure by trying to do something that isn't sure to work as I expect.

答:
Terraform has some automatic migration behavior built in to terraform init.

Based on your description, it sounds like so far you've been using local state storage, and so the latest state snapshot is in a .tfstate file on your local system and you probably don't have a backend block in your configuration yet, since local storage is the default.

Before beginning this process, I suggest first making a copy of your state file in a safe place so that you can experiment more confidently. This process should not risk your existing state file, but it can't hurt to be careful if you've invested significant work in constructing that state file already.

Next, add a backend "azurerm" block to tell Terraform it should use that backend. Refer to the documentation to see which settings you'll need to set and what other preparation steps you may need to make first, such as establishing a new storage container.

If you've been using local state then you will presumably have a terraform.tfstate file in your current working directory, which Terraform will check for in the next step. If you've renamed that file at any point so far, you'll need to rename it back to terraform.tfstate to match the expectations of Terraform's local state storage implementation.

If you now run terraform init, Terraform should notice the following two things:

You have a backend block but the current working directory doesn't currently have an initialized backend connection.

You have an existing terraform.tfstate file in your working directory.

With those two things being true, Terraform will propose to migrate your state from the local backend to the azurerm backend. You can follow the steps it proposes and answer the prompts that appear, after which you should find the same state snapshot stored in your configured Azure storage container.

Once you've confirmed that the object is present in Azure storage, you can delete the terraform.tfstate file, since Terraform will no longer refer to it.


问:
Google Apps Script for form sets stuck at first value entered
I am writing a form to update an inventory whenever an item is ordered. In this project, there is a google sheet which is linked to a form. The form contains a script that gets the current inventory from the sheet and subtracts one from this upon order.

The form has three questions: name, size, and comment.

The issue I am getting is that the system works once, and then somehow stores the first response for each successive form submission.

For a minimal working example: I've distilled the issue to the following code, which is triggered by a form submission:

```<code>function updateStock()
{
  var customer_name = "";
  const ss_id = ### I enter spreadsheet ID here ###
  const form_id = ### I enter form ID here ###

  var sheet = SpreadsheetApp.openById(ss_id);
  var form = FormApp.openById(form_id);

  var form_resp = form.getResponses()[0];
  var customer_name = form_resp.getItemResponses()[0].getResponse();
  Logger.log(customer_name);
// Rest of code follows from here
}
</code>```

Upon entry for the first form I write:

Name: Peter

Size: M

Comment: none

Code returns:

1:43:40 AM Info    Peter

(If I include the rest of the code, it correctly subtracts the inventory).

Then on the next (or tenth) submission, I might submit:

Name: Joe

Size: L

Comment: none

again, the code returns:

```<code>1:44:55 AM  Info    Peter
</code>```

If I start from scratch or clear responses, I can get it to work once, but if not, the code will forever return "Peter" for the name, regardless of what I enter.

The form responses however are correct! It is just the call to

```<code>var form_resp = form.getResponses()[0];
var customer_name = form_resp.getItemResponses()[0].getResponse();
</code>```

... that seems to have a "cached" value.

This seems like a weird bug, but am wondering if anyone has a clue as to what this might be (or, honestly, if there is a better way to achieve the same result!)

Thanks for taking the time.

答:
Ugh. Very bad error on my part.

As vector pointed out:

The code: form.getResponses() returns all responses whereas I thought it was returning the current response (read the docs!)

Thus form.getResponses()[0] was naturally returning the first answer, whereas I should have written:

```<code>  var N = form.getResponses().length-1;
  var form_resp = form.getResponses()[N];
</code>```

Whoops.

Body:
I am writing a form to update an inventory whenever an item is ordered. In this project, there is a google sheet which is linked to a form. The form contains a script that gets the current inventory from the sheet and subtracts one from this upon order.

The form has three questions: name, size, and comment.

The issue I am getting is that the system works once, and then somehow stores the first response for each successive form submission.

For a minimal working example: I've distilled the issue to the following code, which is triggered by a form submission:

function updateStock()
{
  var customer_name = "";
  const ss_id = ### I enter spreadsheet ID here ###
  const form_id = ### I enter form ID here ###

  var sheet = SpreadsheetApp.openById(ss_id);
  var form = FormApp.openById(form_id);

  var form_resp = form.getResponses()[0];
  var customer_name = form_resp.getItemResponses()[0].getResponse();
  Logger.log(customer_name);
// Rest of code follows from here
}

Upon entry for the first form I write:

Name: Peter

Size: M

Comment: none

Code returns:

1:43:40 AM Info    Peter

(If I include the rest of the code, it correctly subtracts the inventory).

Then on the next (or tenth) submission, I might submit:

Name: Joe

Size: L

Comment: none

again, the code returns:

1:44:55 AM  Info    Peter

If I start from scratch or clear responses, I can get it to work once, but if not, the code will forever return "Peter" for the name, regardless of what I enter.

The form responses however are correct! It is just the call to

var form_resp = form.getResponses()[0];
var customer_name = form_resp.getItemResponses()[0].getResponse();

... that seems to have a "cached" value.

This seems like a weird bug, but am wondering if anyone has a clue as to what this might be (or, honestly, if there is a better way to achieve the same result!)

Thanks for taking the time.

Body:
Ugh. Very bad error on my part.

As vector pointed out:

The code: form.getResponses() returns all responses whereas I thought it was returning the current response (read the docs!)

Thus form.getResponses()[0] was naturally returning the first answer, whereas I should have written:

  var N = form.getResponses().length-1;
  var form_resp = form.getResponses()[N];

Whoops.


问:
Change sender account is when sending coins?
I have deployed an (openzeppelin's) ERC20 smart contract using truffle on my Ganache development network.

When contract is deployed, the first address (on the development ganache network) receives the amount of tokens specified by me below (in this case 20):

```<code>// 2_deploy_contracts.js
var GLDToken = artifacts.require("GLDToken");

module.exports =  function(deployer) {
    deployer.deploy(GLDToken, 20);
}
</code>```

As per Openzeppelin docs I have made a function to make transfers which works perfectly, see below the full code:

```<code>// app.js
var GLDToken = artifacts.require("GLDToken");

var contractAddress = '0x3cAd6d5f5acFcCBAaC516Df696c65C2AAA2D1F82';

var testWallet1 = '0x35a356879a4573dE4CeD9D36ff76bBbA98afA2dA';
var testWallet2 = '0x8AB027e8439169Aa68514299c8bDa29C8A98483a';
var testWallet3 = '0xA384974dd53A21d10E3671FE23aB45E0CA0CDDf8';

module.exports = function() {

    async function sendTransfer(recipient, amount) {
        let ins = await GLDToken.at(contractAddress);
        let res = await ins.transfer(recipient, amount);

        console.log(res.toString());
    }

    sendTransfer(testWallet2, 5);
}
</code>```

However I'm unable to specify a sender address and it defaults to the first wallet on my Ganache network, this means I'm able to make transfers from testWallet1 to testWallet2 and testWallet1 to testWallet3 but not from testWallet2 to testWallet3.

The transfer function of the smart contract is the one below and as you can see uses _msgSender():

```<code>function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
    _transfer(_msgSender(), recipient, amount);
    return true;
}
</code>```

How can I change this?

答:
As you are not using from in the config, the contract would take the default account to do things.

You may set the default account as you want

```<code>web3.eth.defaultAccount = testWallet2;
</code>```

Body:
I have deployed an (openzeppelin's) ERC20 smart contract using truffle on my Ganache development network.

When contract is deployed, the first address (on the development ganache network) receives the amount of tokens specified by me below (in this case 20):

// 2_deploy_contracts.js
var GLDToken = artifacts.require("GLDToken");

module.exports =  function(deployer) {
    deployer.deploy(GLDToken, 20);
}

As per Openzeppelin docs I have made a function to make transfers which works perfectly, see below the full code:

// app.js
var GLDToken = artifacts.require("GLDToken");

var contractAddress = '0x3cAd6d5f5acFcCBAaC516Df696c65C2AAA2D1F82';

var testWallet1 = '0x35a356879a4573dE4CeD9D36ff76bBbA98afA2dA';
var testWallet2 = '0x8AB027e8439169Aa68514299c8bDa29C8A98483a';
var testWallet3 = '0xA384974dd53A21d10E3671FE23aB45E0CA0CDDf8';

module.exports = function() {

    async function sendTransfer(recipient, amount) {
        let ins = await GLDToken.at(contractAddress);
        let res = await ins.transfer(recipient, amount);

        console.log(res.toString());
    }

    sendTransfer(testWallet2, 5);
}

However I'm unable to specify a sender address and it defaults to the first wallet on my Ganache network, this means I'm able to make transfers from testWallet1 to testWallet2 and testWallet1 to testWallet3 but not from testWallet2 to testWallet3.

The transfer function of the smart contract is the one below and as you can see uses _msgSender():

function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
    _transfer(_msgSender(), recipient, amount);
    return true;
}

How can I change this?

Body:
As you are not using from in the config, the contract would take the default account to do things.

You may set the default account as you want

web3.eth.defaultAccount = testWallet2;


问:
JavaScript Hide Dom Modal Window
On the page linkvertise(dot) com I noticed that if you click on the Premium button in the upper right corner, completely new HTML markup appears in the source code. And if you click again, the new markup disappears.

My question is, what is the most up-to-date way to make a modal window appear? An already created window with display none or a window that is being created on button click?

答:
I don't think that beetween the two options there is one more up-to-date.
But I would say that using an already ceated window is more efficient :

Using an already created window is simpler while coding

It uses less CPU

It uses les files, so less memory.

Title: JavaScript Hide Dom Modal Window


问:
Firebase call inside useEffect is not returning data properly
I have a component Photo.js responsible for making a call to to my firestore and rendering the returned data. The returned data is set to a state variable venues.

This data is then mapped over and rendered to the browser, however I'm getting the following error in the browser:

Cannot read properties of null (reading 'map')

And when I console log the state variable venues, it's being returned as null.

If I comment out the code responsible for mapping out the returned data (below), my webpage renders without problem - and if I uncomment the same code and save, the firebase call works and the data is rendered:

```<code>            {venues.map((item) => {
                return(<img src = {item.photoUrl}/>)
            })}
</code>```

Here's the Photos component controlling the firebase call:

```<code>import { useState,useEffect } from 'react'
import {getVenues} from '../../services/firebase.js'

const Photo = () => {
    const [ venues,setVenues ] = useState(null)

    useEffect(() => {
        console.log('it got here')
        async function getAllVenues(){
            const response = await getVenues()
            await setVenues(response)
        }
        getAllVenues()
    },[])

    console.log(venues)
    return(
        <div className = 'venueCard-container'>
            {venues.map((item) => {
                return(<img src = {item.photoUrl}/>)
            })}
        </div>
    )
}

export default Photo
</code>```

...and the the firebase functions in services/firebase.jss

```<code>import {firebase} from '../firebaseConfig'

export async function getVenues() {
    const response = await firebase
    .firestore()
    .collection('venues')
    .get()
    return response.docs
        .map((venue) => ({...venue.data()}))
}
</code>```

I'm thinking this is some sort of async problem - the component is rendering before the firebase call has returned the data. Suggestions?

答:
```<code>const [ venues,setVenues ] = useState(null)
</code>```

You've set the initial value of the state to be null, so that's what it will be on the first render. Some time later the data will finish loading and you'll render again, but until that time, your component needs to work with the initial state. You could check for null and render nothing:

```<code>return(
    <div className = 'venueCard-container'>
        {venues && venues.map((item) => {
            return(<img src = {item.photoUrl}/>)
        })}
    </div>
)
</code>```

...or you could render a placeholder:

```<code>if (!venues) {
    return <div>Loading...</div>
} else {
    return (
        <div className = 'venueCard-container'>
            {venues.map((item) => {
                return(<img src = {item.photoUrl}/>)
            })}
        </div>
    )
  );
}
</code>```

...or you could make the initial state be an empty array, which means it will always have a .map method even before loading has finished:

```<code>const [ venues,setVenues ] = useState([])
</code>```

Body:
I have a component Photo.js responsible for making a call to to my firestore and rendering the returned data. The returned data is set to a state variable venues.

This data is then mapped over and rendered to the browser, however I'm getting the following error in the browser:

Cannot read properties of null (reading 'map')

And when I console log the state variable venues, it's being returned as null.

If I comment out the code responsible for mapping out the returned data (below), my webpage renders without problem - and if I uncomment the same code and save, the firebase call works and the data is rendered:

            {venues.map((item) => {
                return()
            })}

Here's the Photos component controlling the firebase call:

import { useState,useEffect } from 'react'
import {getVenues} from '../../services/firebase.js'

const Photo = () => {
    const [ venues,setVenues ] = useState(null)

    useEffect(() => {
        console.log('it got here')
        async function getAllVenues(){
            const response = await getVenues()
            await setVenues(response)
        }
        getAllVenues()
    },[])

    console.log(venues)
    return(

            {venues.map((item) => {
                return()
            })}

    )
}

export default Photo

...and the the firebase functions in services/firebase.jss

import {firebase} from '../firebaseConfig'

export async function getVenues() {
    const response = await firebase
    .firestore()
    .collection('venues')
    .get()
    return response.docs
        .map((venue) => ({...venue.data()}))
}

I'm thinking this is some sort of async problem - the component is rendering before the firebase call has returned the data. Suggestions?

Body:
const [ venues,setVenues ] = useState(null)

You've set the initial value of the state to be null, so that's what it will be on the first render. Some time later the data will finish loading and you'll render again, but until that time, your component needs to work with the initial state. You could check for null and render nothing:

return(
    <div className = 'venueCard-container'>
        {venues && venues.map((item) => {
            return(<img src = {item.photoUrl}/>)
        })}
    </div>
)

...or you could render a placeholder:

if (!venues) {
    return <div>Loading...</div>
} else {
    return (
        <div className = 'venueCard-container'>
            {venues.map((item) => {
                return(<img src = {item.photoUrl}/>)
            })}
        </div>
    )
  );
}

...or you could make the initial state be an empty array, which means it will always have a .map method even before loading has finished:

const [ venues,setVenues ] = useState([])


问:
Where does docker get the node version from?
In the Dockerfile I have specified the node:14.17.6-alpine3.13 version. In the deploy log the following version appears node:16.13.2-alpine. Does anyone know what is the reason for this? Or where to change this?

docker-compose deploy log:

[+] Building 20.1s (11/17)
 => [docker_nestjs-backend internal] load build definition from Dockerfile                                                    0.6s
 => => transferring dockerfile: 32B                                                                                0.0s
 => [docker_angular-frontend internal] load build definition from Dockerfile                                                  0.7s
 => => transferring dockerfile: 32B                                                                                0.0s
 => [docker_nestjs-backend internal] load .dockerignore                                                                       0.9s
 => => transferring context: 2B                                                                                0.0s
 => [docker_angular-frontend internal] load .dockerignore                                                                     1.1s
 => => transferring context: 2B                                                                                0.0s

 // HERE
 => [docker_angular-frontend internal] load metadata for docker.io/library/node:16.13.2-alpine                               11.8s
 // ^ at this line docker load metadata for 16.13.2-alpine but in dockerfile is node:14.17.6-alpine3.13 specified

 => CANCELED [docker_nestjs-backend internal] load build context                                                              6.9s
 => => transferring context: 113.92MB                                                                                6.3s
 => [docker_angular-frontend development 1/6] FROM docker.io/library/node:16.13.2-alpine@sha256:2f50f4a428f8b5280817c9d4d896  0.0s
 => CACHED [docker_angular-frontend development 2/6] WORKDIR     /usr/src/app                                                 0.0s
 => [docker_angular-frontend internal] load build context                                                                     0.4s
 => => transferring context: 19.58kB                                                                                0.0s
 => [docker_angular-frontend development 3/6] COPY        package*.json ./                                                    0.6s
 => ERROR [docker_angular-frontend development 4/6] RUN         npm ci                                                        5.6s
```
<p><code>Dockerfile:</code></p>
```# -----------------------------------------------
# Development
# -----------------------------------------------
FROM        node:14.17.6-alpine3.13 AS development
WORKDIR     /usr/src/app
RUN         npm install -g @angular/cli@12.2.6
COPY        package*.json ./
RUN         npm ci
COPY        . .
RUN         npm run build

# -----------------------------------------------
# Production
# -----------------------------------------------
FROM        httpd:2.4
ENV         TZ=Europe/Zurich
RUN         ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY        ./docker/copy-source/httpd.conf /usr/local/apache2/conf/httpd.conf
COPY        --from=development /usr/src/app/dist /usr/local/apache2/htdocs/

ENTRYPOINT  apachectl -DFOREGROUND
```
<p><code>docker-compose.yml:</code></p>
```version: '3.8'
services:
  # -----------------------------------------------
  # Angular Frontend
  # -----------------------------------------------
  angular-frontend:
    build:
      dockerfile: Dockerfile
      context: ../angular-frontend
      target: development
    container_name: angular-frontend
    command: npm run start
    links:
      - nestjs-backend
    ports:
      - "4200:4200"
    restart: on-failure
    volumes:
      - ./angular-frontend:/usr/src/app/angular-frontend
```

答: This line docker_nestjs-backend development , shows that docker_nestjs-backend throws the error. You can change that to required configuration.</p>

Body:
In the Dockerfile I have specified the node:14.17.6-alpine3.13 version. In the deploy log the following version appears node:16.13.2-alpine. Does anyone know what is the reason for this? Or where to change this?

docker-compose deploy log:

[+] Building 20.1s (11/17)
 => [docker_nestjs-backend internal] load build definition from Dockerfile                                                    0.6s
 => => transferring dockerfile: 32B                                                                                0.0s
 => [docker_angular-frontend internal] load build definition from Dockerfile                                                  0.7s
 => => transferring dockerfile: 32B                                                                                0.0s
 => [docker_nestjs-backend internal] load .dockerignore                                                                       0.9s
 => => transferring context: 2B                                                                                0.0s
 => [docker_angular-frontend internal] load .dockerignore                                                                     1.1s
 => => transferring context: 2B                                                                                0.0s

 // HERE
 => [docker_angular-frontend internal] load metadata for docker.io/library/node:16.13.2-alpine                               11.8s
 // ^ at this line docker load metadata for 16.13.2-alpine but in dockerfile is node:14.17.6-alpine3.13 specified

 => CANCELED [docker_nestjs-backend internal] load build context                                                              6.9s
 => => transferring context: 113.92MB                                                                                6.3s
 => [docker_angular-frontend development 1/6] FROM docker.io/library/node:16.13.2-alpine@sha256:2f50f4a428f8b5280817c9d4d896  0.0s
 => CACHED [docker_angular-frontend development 2/6] WORKDIR     /usr/src/app                                                 0.0s
 => [docker_angular-frontend internal] load build context                                                                     0.4s
 => => transferring context: 19.58kB                                                                                0.0s
 => [docker_angular-frontend development 3/6] COPY        package*.json ./                                                    0.6s
 => ERROR [docker_angular-frontend development 4/6] RUN         npm ci                                                        5.6s

Dockerfile:

# -----------------------------------------------
# Development
# -----------------------------------------------
FROM        node:14.17.6-alpine3.13 AS development
WORKDIR     /usr/src/app
RUN         npm install -g @angular/cli@12.2.6
COPY        package*.json ./
RUN         npm ci
COPY        . .
RUN         npm run build

# -----------------------------------------------
# Production
# -----------------------------------------------
FROM        httpd:2.4
ENV         TZ=Europe/Zurich
RUN         ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY        ./docker/copy-source/httpd.conf /usr/local/apache2/conf/httpd.conf
COPY        --from=development /usr/src/app/dist /usr/local/apache2/htdocs/

ENTRYPOINT  apachectl -DFOREGROUND

docker-compose.yml:

version: '3.8'
services:
  # -----------------------------------------------
  # Angular Frontend
  # -----------------------------------------------
  angular-frontend:
    build:
      dockerfile: Dockerfile
      context: ../angular-frontend
      target: development
    container_name: angular-frontend
    command: npm run start
    links:
      - nestjs-backend
    ports:
      - "4200:4200"
    restart: on-failure
    volumes:
      - ./angular-frontend:/usr/src/app/angular-frontend


问:
Encoding issues storing emojis in MySQL: How can I define a character collation in NodeJS using Prisma ORM?
Dear nodejs experts and database gurus,

We face issues with storing emojis and other special characters in our MySQL database. We get an error by Prisma, which is the "ORM" we use:

Conversion from collation utf8_general_ci into utf8mb4_unicode_520_ci impossible for parameter

```<code>prisma:query INSERT INTO `app`.`show` [...]
PrismaClientUnknownRequestError: Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Server(ServerError {
   code: 3988,
   message: "Conversion from collation utf8_general_ci into utf8mb4_unicode_520_ci impossible for parameter",
   state: "HY000" })) })
at cb (/app/node_modules/@prisma/client/runtime/index.js:38695:17) { clientVersion: '3.8.1' }
prisma:query ROLLBACK
</code>```

We tried the following solutions already:

Using different collations on the database (db level, table level, field level): utf8mb4_unicode_520_ci, utf8mb4_unicode_ci, but the goal is to use a set which is most modern like utf8mb4_0900_ai_ci

Changing it to utf8 instead of utf8mb4 resulted in an error like Incorrect string value: '\\xF0\\x9F\\x92\\x95' and is not our goal as we want to be able to store all emojis

Dumping, changing all encoding statements in the sql dump and re-importing with parameters. We used a bunch of parameters in hope it will encode it differently like --default-character-set=utf8mb4 or --hex-blob

Defining a charset in our backend as we believe that the database is not the issue, but more the input we send to it like the old PHP way we knew ini_set("default_charset", "UTF-8"), but as we understood Nodejs does not care about the encoding/charset, but maybe there is a way

Defining a charset at the MySQL client, but we did not find a way to do so as we use prisma.io and maybe do not have access to the mysql client underhood. Prisma by itself seems to support utf8mb4 as this is in the docs https://www.prisma.io/docs/concepts/components/prisma-client/case-sensitivity#database-collation-and-case-sensitivity and we found a recent discussion about defining it on a more granular level here https://github.com/prisma/prisma/discussions/4743

Maybe two more valuable insights:

The data comes from a React Native app, but before the database migration, we had no issues, so we hope it is not a problem on the client-side, but for sure, we can specify something there as well, eg. setting HEADER information on the API call, which we do with axios

Before we used the db-as-a-service tool Airtable, which was quite a hassle to "break free" from them. We exported everything in CSV format and imported it in a local MySQL db powered with XAMPP (which had different options for encoding) and then imported the dump to our new MySQL cluster

Our goal is to

Set the right encoding character set in our new MySQL cluster

Send the data in the right format to the DB (setting the same set in NodeJS and React)

Hope you can help us! Thanks!

答:
\\xF0\\x9F\\x92\\x95 () needs the CHARACTER SET utf8mb4, not utf8.  The COLLATION is irrelevant, other than that it must be consistent (that is, utf8mb4_...).  (Note the 4 byte hex, and the 4 in utf8mb4.)

There are several places to specify the charset.  It sounds like you missed a place.

I don't have any notes on Prisma or React, but for node.js:

```<code>var connection = mysql.createConnection({ ... , charset : 'utf8mb4'});
</code>```

See Emoji are not inserting in database node js mysql

That's the connection; the table also needs it.  Please provide SHOW CREATE TABLE so we can check that the column has the charset specified or, if not, then the table has the appropriate default.

Yes, the COLLATION utf8mb4_0900_ai_ci is probably best for MySQL 8.0.  (It is not available in older versions of MySQL nor in any version of MariaDB.)

Note:  Specifying just a COLLATION causes the CHARACTER SET to be set to the beginning part of the collation.

PHP's ini_set("default_charset", "UTF-8") should be fine, but is probably not relevant to the problem).

Please provide SHOW VARIABLES LIKE 'char%';  The _client, _connection and _results should all be utf8mb4.

Character set refers to 'encoding', as in what the bits represent.  Collation deals with comparisons and sorting -- "A" < "a" or not, plus many other variants on that.

Body:
Dear nodejs experts and database gurus,

We face issues with storing emojis and other special characters in our MySQL database. We get an error by Prisma, which is the "ORM" we use:

Conversion from collation utf8_general_ci into utf8mb4_unicode_520_ci impossible for parameter

prisma:query INSERT INTO `app`.`show` [...]
PrismaClientUnknownRequestError: Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Server(ServerError {
   code: 3988,
   message: "Conversion from collation utf8_general_ci into utf8mb4_unicode_520_ci impossible for parameter",
   state: "HY000" })) })
at cb (/app/node_modules/@prisma/client/runtime/index.js:38695:17) { clientVersion: '3.8.1' }
prisma:query ROLLBACK

We tried the following solutions already:

Using different collations on the database (db level, table level, field level): utf8mb4_unicode_520_ci, utf8mb4_unicode_ci, but the goal is to use a set which is most modern like utf8mb4_0900_ai_ci

Changing it to utf8 instead of utf8mb4 resulted in an error like Incorrect string value: '\\xF0\\x9F\\x92\\x95' and is not our goal as we want to be able to store all emojis

Dumping, changing all encoding statements in the sql dump and re-importing with parameters. We used a bunch of parameters in hope it will encode it differently like --default-character-set=utf8mb4 or --hex-blob

Defining a charset in our backend as we believe that the database is not the issue, but more the input we send to it like the old PHP way we knew ini_set("default_charset", "UTF-8"), but as we understood Nodejs does not care about the encoding/charset, but maybe there is a way

Defining a charset at the MySQL client, but we did not find a way to do so as we use prisma.io and maybe do not have access to the mysql client underhood. Prisma by itself seems to support utf8mb4 as this is in the docs https://www.prisma.io/docs/concepts/components/prisma-client/case-sensitivity#database-collation-and-case-sensitivity and we found a recent discussion about defining it on a more granular level here https://github.com/prisma/prisma/discussions/4743

Maybe two more valuable insights:

The data comes from a React Native app, but before the database migration, we had no issues, so we hope it is not a problem on the client-side, but for sure, we can specify something there as well, eg. setting HEADER information on the API call, which we do with axios

Before we used the db-as-a-service tool Airtable, which was quite a hassle to "break free" from them. We exported everything in CSV format and imported it in a local MySQL db powered with XAMPP (which had different options for encoding) and then imported the dump to our new MySQL cluster

Our goal is to

Set the right encoding character set in our new MySQL cluster

Send the data in the right format to the DB (setting the same set in NodeJS and React)

Hope you can help us! Thanks!

Body:
\\xF0\\x9F\\x92\\x95 () needs the CHARACTER SET utf8mb4, not utf8.  The COLLATION is irrelevant, other than that it must be consistent (that is, utf8mb4_...).  (Note the 4 byte hex, and the 4 in utf8mb4.)

There are several places to specify the charset.  It sounds like you missed a place.

I don't have any notes on Prisma or React, but for node.js:

var connection = mysql.createConnection({ ... , charset : 'utf8mb4'});

See Emoji are not inserting in database node js mysql

That's the connection; the table also needs it.  Please provide SHOW CREATE TABLE so we can check that the column has the charset specified or, if not, then the table has the appropriate default.

Yes, the COLLATION utf8mb4_0900_ai_ci is probably best for MySQL 8.0.  (It is not available in older versions of MySQL nor in any version of MariaDB.)

Note:  Specifying just a COLLATION causes the CHARACTER SET to be set to the beginning part of the collation.

PHP's ini_set("default_charset", "UTF-8") should be fine, but is probably not relevant to the problem).

Please provide SHOW VARIABLES LIKE 'char%';  The _client, _connection and _results should all be utf8mb4.

Character set refers to 'encoding', as in what the bits represent.  Collation deals with comparisons and sorting -- "A" < "a" or not, plus many other variants on that.


问:
Django crispy forms tabholder can't change css
In my form i would like to change the style of the tabs, if i add a css_class to TabHolder it won't render it, it just keeps the default class. Here is the init of the modelform.

```<code>def __init__(self, *args, **kwargs):
    self.user = kwargs.pop('user', None)
    super().__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_tag = True
    self.helper.form_method = 'POST'
    self.helper.layout = Layout(
        TabHolder(
            Tab(_('Company'),
                'name',
                css_class='nav-item active'),
            Tab('Address',
                'country',
                css_class='nav-item'),
            Tab('Info',
                'email', 'phone', 'website',
                css_class='nav-item'),
            css_class='nav nav-tabs nav-tabs-highlight nav-justified mb-0'
        ),
        ButtonHolder(
            Submit('submit', "Submit form", css_class='btn btn-primary')
        ),
    )
</code>```

As you can see in TabHolder there is css_class='nav nav-tabs nav-tabs-highlight nav-justified mb-0' but it keeps showing just css_class='nav nav-tabs'

答:
I had the same problem and I thought it would be better to solve it in another way.
I preferred a less invasive approach.
I used a javascript inside my template, which adjusts the element class.
Just add the code above in your template.</p>
```<code>    <script>
        function load() {
            document.getElementById('tab-content').getElementsByClassName('nav nav-tabs')[0].className = "nav nav-tabs bg-nav-pills nav-justified mb-3";
        }
        window.onload = load;
    </script>
</code>```

Body:
In my form i would like to change the style of the tabs, if i add a css_class to TabHolder it won't render it, it just keeps the default class. Here is the init of the modelform.

def __init__(self, *args, **kwargs):
    self.user = kwargs.pop('user', None)
    super().__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_tag = True
    self.helper.form_method = 'POST'
    self.helper.layout = Layout(
        TabHolder(
            Tab(_('Company'),
                'name',
                css_class='nav-item active'),
            Tab('Address',
                'country',
                css_class='nav-item'),
            Tab('Info',
                'email', 'phone', 'website',
                css_class='nav-item'),
            css_class='nav nav-tabs nav-tabs-highlight nav-justified mb-0'
        ),
        ButtonHolder(
            Submit('submit', "Submit form", css_class='btn btn-primary')
        ),
    )

As you can see in TabHolder there is css_class='nav nav-tabs nav-tabs-highlight nav-justified mb-0' but it keeps showing just css_class='nav nav-tabs'

Body:
I had the same problem and I thought it would be better to solve it in another way.
I preferred a less invasive approach.
I used a javascript inside my template, which adjusts the element class.
Just add the code above in your template.

    <script>
        function load() {
            document.getElementById('tab-content').getElementsByClassName('nav nav-tabs')[0].className = "nav nav-tabs bg-nav-pills nav-justified mb-3";
        }
        window.onload = load;
    </script>


问:
for loops with lots of if conditions while creating a new column
I am trying to convert 1 column (kinda 2) of categories (strings) into a set of numbers 1 for star, 2 qso, 3 for galaxies that are not agn (second column defines that) and then 4 for galaxies that are AGN.all  saved on a new column of the dataframe.

```<code>for n ,i, l in zip(data_clean['class'], data_clean['subClass'], data_clean['nClass'] ):
if n == 'STAR':
    l = 1
elif n == 'QSO':
    l=2
elif n == 'GALAXY' and i != 'AGN':
    l=3
elif  n == 'GALAXY' and i == 'AGN':
   l=4
</code>```

where class is the major category, subclass where I get the AGN classification and the nclass is the new column where I put the new integer classification.  But I  get all zeros. What am I doing wrong?

答:
Does this do what you want?

def type_to_value(n, i):
    if n == 'STAR':
        return 1
    elif n == 'QSO':
        return 2
    elif n == 'GALAXY' and i != 'AGN':
        return 3
    elif n == 'GALAXY' and i == 'AGN':
        return 4

data_clean['nClass'] = [type_to_value(n, i) for n, i in zip(data_clean['class'], data_clean['subClass'])]
```

Body:
I am trying to convert 1 column (kinda 2) of categories (strings) into a set of numbers 1 for star, 2 qso, 3 for galaxies that are not agn (second column defines that) and then 4 for galaxies that are AGN.all  saved on a new column of the dataframe.

for n ,i, l in zip(data_clean['class'], data_clean['subClass'], data_clean['nClass'] ):
if n == 'STAR':
    l = 1
elif n == 'QSO':
    l=2
elif n == 'GALAXY' and i != 'AGN':
    l=3
elif  n == 'GALAXY' and i == 'AGN':
   l=4

where class is the major category, subclass where I get the AGN classification and the nclass is the new column where I put the new integer classification.  But I  get all zeros. What am I doing wrong?


问:
How does the C++ compiler decide which of these functions is called?
Consider the following setup:

I am given an interface

```<code>template<class T>
  void FooClass<T>::foo(boost::function<double (int)> f)
{...}
</code>```

I want to implement f using a Functor:

```<code>class MyFun {
    public: double operator()(int a) {do something...;}
}
</code>```

However there is another function defined in the interface

```<code>template<class T>
  template <class FunPtr>
    void FooClass<T>::foo(const FunPtr& f)
{...}
</code>```

When a FooClass object is called,

```<code>MyFun f;
FooClass<double> fooclass;
fooclass.foo(f);
</code>```

it uses the second definition, while I want it to call the first one - can this be changed somehow?

答:
You can explicitly construct a boost::function object so the first overload will be preferred:

```<code>fooclass.foo(boost::function<double (int)>(f));
</code>```

Regarding your question title, are you wondering how overload resolution works? When the overload is being chosen, an untemplated type is a better match than a templated one, but a templated type that matches directly (FunPtr = MyFun) is a better match than an an implictit conversion (MyFun -> boost::function<double(int)>), so the second overload matches better in your original case.

(Thanks to @David for pointing out a glaring error in my original formulation!)

ContentLicense: CC BY-SA 3.0

Body:
Consider the following setup:

I am given an interface

template<class T>
  void FooClass<T>::foo(boost::function<double (int)> f)
{...}

I want to implement f using a Functor:

class MyFun {
    public: double operator()(int a) {do something...;}
}

However there is another function defined in the interface

template<class T>
  template <class FunPtr>
    void FooClass<T>::foo(const FunPtr& f)
{...}

When a FooClass object is called,

MyFun f;
FooClass<double> fooclass;
fooclass.foo(f);

it uses the second definition, while I want it to call the first one - can this be changed somehow?

Body:
You can explicitly construct a boost::function object so the first overload will be preferred:

fooclass.foo(boost::function<double (int)>(f));

Regarding your question title, are you wondering how overload resolution works? When the overload is being chosen, an untemplated type is a better match than a templated one, but a templated type that matches directly (FunPtr = MyFun) is a better match than an an implictit conversion (MyFun -> boost::function<double(int)>), so the second overload matches better in your original case.

(Thanks to @David for pointing out a glaring error in my original formulation!)


问:
How to get the file name from a full path using JavaScript?
Is there a way that I can get the last value (based on the '\' symbol) from a full path?

Example:

C:\Documents and Settings\img\recycled log.jpg

With this case, I just want to get recycled log.jpg from the full path in JavaScript.

答:
A simple function like PHP pathInfo:

function pathInfo(s) {
    s=s.match(/(.*?[\\/:])?(([^\\/:]*?)(\.[^\\/.]+?)?)(?:[?#].*)?$/);
    return {path:s[1],file:s[2],name:s[3],ext:s[4]};
}

console.log( pathInfo('c:\\folder\\file.txt') );

console.log( pathInfo('/folder/another/file.min.js?query=1') );```
<pre class="snippet-code-html lang-html prettyprint-override"><code>Type and try it:
<input oninput="document.getElementById('test').textContent=pathInfo(this.value).file" value="c:\folder\folder.name\file.ext" style="width:300px"></code>```

OwnerDisplayName: Jin Yong

LastEditorDisplayName: nickf

Body:
A simple function like PHP pathInfo:

function pathInfo(s) {
    s=s.match(/(.*?[\\/:])?(([^\\/:]*?)(\.[^\\/.]+?)?)(?:[?#].*)?$/);
    return {path:s[1],file:s[2],name:s[3],ext:s[4]};
}

console.log( pathInfo('c:\\folder\\file.txt') );

console.log( pathInfo('/folder/another/file.min.js?query=1') );

Type and try it:
<input oninput="document.getElementById('test').textContent=pathInfo(this.value).file" value="c:\folder\folder.name\file.ext" style="width:300px">


问:
How can I unhide .git file by default?
When I use .git init .git file is always hidden by default, I have to always  unselect the hidden option in properties. What is the way I can change this default setting?

Also when I del .git only the config,description and HEAD file are deleted,not the entire .git folder. How can I change this default setting so I don't have to always unhide after git init ?

I'm using windows 8.1

答:
core.hideDotFiles

(Windows-only) If true, mark newly-created directories and files whose name starts with a dot as hidden. If dotGitOnly, only the .git/ directory is hidden, but no other files starting with a dot. The default mode is dotGitOnly.

```<code>> git config --global core.hideDotFiles false
</code>```

Title: How can I unhide .git file by default?

Body:
core.hideDotFiles

(Windows-only) If true, mark newly-created directories and files whose name starts with a dot as hidden. If dotGitOnly, only the .git/ directory is hidden, but no other files starting with a dot. The default mode is dotGitOnly.

> git config --global core.hideDotFiles false


问:
TortoiseGit Commit window make none selected as default
In the commit window of TortoiseGit , all files are selected by default. I would like to have "none" or no file selected by default.

Is there any setting to do this?

答:
After upgrading to TG 2.13, I had the opposite issue (no items selected by default, undesired behavior for me), even though Select items automatically was checked.
Uncheck it, click "Apply". Check it back, click "Apply". It fixed it for me.

Full path to this option:
TG Settings → General → Dialogs2


问:
How to convert a MySQL DB to XML?
How can you convert a MySQL database to XML?
I want everythimg... data and there relation in XML schema file

how to convert from sqlyog community 8.13(free version)

答:
in fact it's actually

```<code>mysqldump --xml dbname -t tablename
</code>```

with eventually -u username -pPASSWORD (or -p for an interactive passwd input)
according to your privileges..

ContentLicense: CC BY-SA 2.5

Title: How to convert a MySQL DB to XML?

Body:
in fact it's actually

mysqldump --xml dbname -t tablename

with eventually -u username -pPASSWORD (or -p for an interactive passwd input)
according to your privileges..


问:
How to read huge .gz file in S3, as I'm getting memory error while reading
I'm trying to read .gz files in S3 and there are total 120 files and I want to merge all the files into one single file.

P.s. the size of each file is between (419MB to 900MB).

```<code>output = []
for i in files:
    if i.endswith('.gz'):
        obj = s3.Object(bucket_name=bucket ,key=i)
        with gzip.GzipFile(fileobj=obj.get()['Body']) as gzipfile:
            content = gzipfile.read()
            output.append(content)
</code>```

I'm getting MemoryError at this line  content = gzipfile.read()

答: Instead of gzipfile.read(), use a loop to do gzipfile.read(buffersize), where buffersize is some reasonable number of uncompressed bytes to load into memory and process. When you get no bytes back, then you exit your loop.

Body:
I'm trying to read .gz files in S3 and there are total 120 files and I want to merge all the files into one single file.

P.s. the size of each file is between (419MB to 900MB).

output = []
for i in files:
    if i.endswith('.gz'):
        obj = s3.Object(bucket_name=bucket ,key=i)
        with gzip.GzipFile(fileobj=obj.get()['Body']) as gzipfile:
            content = gzipfile.read()
            output.append(content)

I'm getting MemoryError at this line  content = gzipfile.read()


问:
Use script to change the font of a button label (TextMesh Pro)?
I want to use script to dynamically change the font of a specific button label in Unity. However, my efforts haven't worked.

I have created a simple game. The game's interface has multiple buttons on the canvas, with unique text labels on each button. The buttons are canvas elements (UI button) with a sub element (text - TextMeshPro) which provides the text label for each of the buttons.

In the code example below, all of the code works except the last full line beginning, "buttonLabelText.font =". I can, for example, use the rest of the code to easily change what the button says (change the button text). However, I cannot change the font (or do other font manipulations like change the size of the font).

Additional info: The below code does not throw any errors. I am using TextMeshPro. The desired font is in the following folder: Assets > TextMesh Pro > Resources > Fonts & Materials. The font is saved in SDF format.

Additional context: I want to change the font via script because there are multiple buttons on the game UI, all of which will change fonts at certain times. So, the script would allow me to more easily specify which buttons should change fonts at certain times.

Any potential solutions?

```<code>    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;

    public class FontManipulation : MonoBehaviour {
    public TMP_FontAsset ChillerSDF; // ChillerSDF is the font I want for the button label text.
    private TextMeshProUGUI buttonLabelText; // buttonLabelText is the actual text within the button label

    void Start() {

        TextMeshProUGUI buttonLabelText = GameObject.Find("ButtonLabel").GetComponent<TextMeshProUGUI>(); // This code finds the correct button label among multiple button labels in the scene, and grabs the TextMesh Pro GUI. This code works (I can use it to change what the text says, for example).
        buttonLabelText.font = ChillerSDF; // This code should change the font to the desired font, but does not work.
    }
    }
</code>```

答:
I had the same problem as you and I solved it by getting TMP_Text instead of TextMeshProUGUI</p>
```<code>public TMP_FontAsset ChillerSDF;
private TMP_Text buttonLabelText; //TMP_Text instead of TextMeshProUGUI

void Start() {
    TMP_Text buttonLabelText = GameObject.Find("ButtonLabel").GetComponent<TMP_Text>();
    buttonLabelText.font = ChillerSDF; // This code should work now
}
</code>```

Body:
I want to use script to dynamically change the font of a specific button label in Unity. However, my efforts haven't worked.

I have created a simple game. The game's interface has multiple buttons on the canvas, with unique text labels on each button. The buttons are canvas elements (UI button) with a sub element (text - TextMeshPro) which provides the text label for each of the buttons.

In the code example below, all of the code works except the last full line beginning, "buttonLabelText.font =". I can, for example, use the rest of the code to easily change what the button says (change the button text). However, I cannot change the font (or do other font manipulations like change the size of the font).

Additional info: The below code does not throw any errors. I am using TextMeshPro. The desired font is in the following folder: Assets > TextMesh Pro > Resources > Fonts & Materials. The font is saved in SDF format.

Additional context: I want to change the font via script because there are multiple buttons on the game UI, all of which will change fonts at certain times. So, the script would allow me to more easily specify which buttons should change fonts at certain times.

Any potential solutions?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;

    public class FontManipulation : MonoBehaviour {
    public TMP_FontAsset ChillerSDF; // ChillerSDF is the font I want for the button label text.
    private TextMeshProUGUI buttonLabelText; // buttonLabelText is the actual text within the button label

    void Start() {

        TextMeshProUGUI buttonLabelText = GameObject.Find("ButtonLabel").GetComponent<TextMeshProUGUI>(); // This code finds the correct button label among multiple button labels in the scene, and grabs the TextMesh Pro GUI. This code works (I can use it to change what the text says, for example).
        buttonLabelText.font = ChillerSDF; // This code should change the font to the desired font, but does not work.
    }
    }

Body:
I had the same problem as you and I solved it by getting TMP_Text instead of TextMeshProUGUI

public TMP_FontAsset ChillerSDF;
private TMP_Text buttonLabelText; //TMP_Text instead of TextMeshProUGUI

void Start() {
    TMP_Text buttonLabelText = GameObject.Find("ButtonLabel").GetComponent<TMP_Text>();
    buttonLabelText.font = ChillerSDF; // This code should work now
}


问:
Using float type to represent money amount without arithmetic operations
I understand float/double are bad choices for storing money amounts. When you do some arithmetic operations, you may lose precision.

Is it safe to use float just to represent the amount when arithmetic operations are not performed on that number? For example, mapping amount from API to float type.

答:
Not necessarily. Most numbers that "look nice" in base-10 representation, such as 0.1, can not be represented exactly as a binary floating-point number. For example, even when using double, the base-10 number 0.1 will be represented as a number that is approximately 0.1000000000000000055511151231257827021181583404541015625.

Only numbers where the decimal part can be written as a finite sum of inverse powers of two (1/2, 1/4, 1/8, 1/16, and so on) can be represented exactly as a finite binary decimal number (for example: 0.9375, which is the sum of those four numbers). And among those numbers, only the ones that have few enough binary digits can be represented exactly as a floating-point number (at most 24 binary digits for float and 53 binary digits for double, as per the IEEE 754 standard). This is similar to the situation with base-10 numbers; it just happens for more numbers in binary. For example, 1/3 cannot be represented with a finite number of decimals in either base-10 or binary.

However, if you're just storing the number and printing it at some point, you'll probably be fine, because most functions for printing floating-point numbers will round the number off nicely for you.


答:
There is no indentations on your for loop?

Try:

```<code>for n ,i, l in zip(data_clean['class'], data_clean['subClass'], data_clean['nClass'] ):
    if n == 'STAR':
       l = 1
    elif n == 'QSO':
       l=2
    elif n == 'GALAXY' and i != 'AGN':
       l=3
    elif  n == 'GALAXY' and i == 'AGN':
       l=4
</code>```

I can't use tab here, so just try to do equal in your code. Don't copy and paste.

Title: for loops with lots of if conditions while creating a new column

Body:
There is no indentations on your for loop?

Try:

for n ,i, l in zip(data_clean['class'], data_clean['subClass'], data_clean['nClass'] ):
    if n == 'STAR':
       l = 1
    elif n == 'QSO':
       l=2
    elif n == 'GALAXY' and i != 'AGN':
       l=3
    elif  n == 'GALAXY' and i == 'AGN':
       l=4

I can't use tab here, so just try to do equal in your code. Don't copy and paste.


问:
Python: Typehints for argparse.Namespace objects
Is there a way to have Python static analyzers (e.g. in PyCharm, other IDEs) pick up on Typehints on argparse.Namespace objects?  Example:

```<code>parser = argparse.ArgumentParser()
parser.add_argument('--somearg')
parsed = parser.parse_args(['--somearg','someval'])  # type: argparse.Namespace
the_arg = parsed.somearg  # <- Pycharm complains that parsed object has no attribute 'somearg'
</code>```

If I remove the type declaration in the inline comment, PyCharm doesn't complain, but it also doesn't pick up on invalid attributes.  For example:

```<code>parser = argparse.ArgumentParser()
parser.add_argument('--somearg')
parsed = parser.parse_args(['--somearg','someval'])  # no typehint
the_arg = parsed.somaerg   # <- typo in attribute, but no complaint in PyCharm.  Raises AttributeError when executed.
</code>```

Any ideas?

Update

Inspired by Austin's answer below, the simplest solution I could find is one using namedtuples:

```<code>from collections import namedtuple
ArgNamespace = namedtuple('ArgNamespace', ['some_arg', 'another_arg'])

parser = argparse.ArgumentParser()
parser.add_argument('--some-arg')
parser.add_argument('--another-arg')
parsed = parser.parse_args(['--some-arg', 'val1', '--another-arg', 'val2'])  # type: ArgNamespace

x = parsed.some_arg  # good...
y = parsed.another_arg  # still good...
z = parsed.aint_no_arg  # Flagged by PyCharm!
</code>```

While this is satisfactory, I still don't like having to repeat the argument names.  If the argument list grows considerably, it will be tedious updating both locations.  What would be ideal is somehow extracting the arguments from the parser object like the following:

```<code>parser = argparse.ArgumentParser()
parser.add_argument('--some-arg')
parser.add_argument('--another-arg')
MagicNamespace = parser.magically_extract_namespace()
parsed = parser.parse_args(['--some-arg', 'val1', '--another-arg', 'val2'])  # type: MagicNamespace
</code>```

I haven't been able to find anything in the argparse module that could make this possible, and I'm still unsure if any static analysis tool could be clever enough to get those values and not bring the IDE to a grinding halt.

Still searching...

Update 2

Per hpaulj's comment, the closest thing I could find to the method described above that would "magically" extract the attributes of the parsed object is something that would extract the dest attribute from each of the parser's _actions.:

```<code>parser = argparse.ArgumentParser()
parser.add_argument('--some-arg')
parser.add_argument('--another-arg')
MagicNamespace = namedtuple('MagicNamespace', [act.dest for act in parser._actions])
parsed = parser.parse_args(['--some-arg', 'val1', '--another-arg', 'val2'])  # type: MagicNamespace
</code>```

But this still does not cause attribute errors to get flagged in static analysis.  This is true also true if I pass namespace=MagicNamespace in the parser.parse_args call.

答:
Another way to do it which could be ideal if you have few arguments is as follows.

First make a function that sets up the parser and returns the namespace. For example:

def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument("-a")
    parser.add_argument("-b", type=int)
    return parser.parse_args()
```
<p>Then you define a main function which takes the args you declared above individually; like so.</p>
<pre class="lang-py prettyprint-override"><code>def main(a: str, b: int):
    print("hello world", a, b)
</code>```

And when you call your main, you do it like this:

if __name__ == "__main__":
    main(**vars(parse_args())
```

From your main onwards, you'll have your variables a and b properly recognised by your static type checker, although you won't have an object any more containing all your arguments, which may be a good or bad thing depending on your use case.

Body:
Is there a way to have Python static analyzers (e.g. in PyCharm, other IDEs) pick up on Typehints on argparse.Namespace objects?  Example:

parser = argparse.ArgumentParser()
parser.add_argument('--somearg')
parsed = parser.parse_args(['--somearg','someval'])  # type: argparse.Namespace
the_arg = parsed.somearg  # <- Pycharm complains that parsed object has no attribute 'somearg'

If I remove the type declaration in the inline comment, PyCharm doesn't complain, but it also doesn't pick up on invalid attributes.  For example:

parser = argparse.ArgumentParser()
parser.add_argument('--somearg')
parsed = parser.parse_args(['--somearg','someval'])  # no typehint
the_arg = parsed.somaerg   # <- typo in attribute, but no complaint in PyCharm.  Raises AttributeError when executed.

Any ideas?

Update

Inspired by Austin's answer below, the simplest solution I could find is one using namedtuples:

from collections import namedtuple
ArgNamespace = namedtuple('ArgNamespace', ['some_arg', 'another_arg'])

parser = argparse.ArgumentParser()
parser.add_argument('--some-arg')
parser.add_argument('--another-arg')
parsed = parser.parse_args(['--some-arg', 'val1', '--another-arg', 'val2'])  # type: ArgNamespace

x = parsed.some_arg  # good...
y = parsed.another_arg  # still good...
z = parsed.aint_no_arg  # Flagged by PyCharm!

While this is satisfactory, I still don't like having to repeat the argument names.  If the argument list grows considerably, it will be tedious updating both locations.  What would be ideal is somehow extracting the arguments from the parser object like the following:

parser = argparse.ArgumentParser()
parser.add_argument('--some-arg')
parser.add_argument('--another-arg')
MagicNamespace = parser.magically_extract_namespace()
parsed = parser.parse_args(['--some-arg', 'val1', '--another-arg', 'val2'])  # type: MagicNamespace

I haven't been able to find anything in the argparse module that could make this possible, and I'm still unsure if any static analysis tool could be clever enough to get those values and not bring the IDE to a grinding halt.

Still searching...

Update 2

Per hpaulj's comment, the closest thing I could find to the method described above that would "magically" extract the attributes of the parsed object is something that would extract the dest attribute from each of the parser's _actions.:

parser = argparse.ArgumentParser()
parser.add_argument('--some-arg')
parser.add_argument('--another-arg')
MagicNamespace = namedtuple('MagicNamespace', [act.dest for act in parser._actions])
parsed = parser.parse_args(['--some-arg', 'val1', '--another-arg', 'val2'])  # type: MagicNamespace

But this still does not cause attribute errors to get flagged in static analysis.  This is true also true if I pass namespace=MagicNamespace in the parser.parse_args call.

Body:
Another way to do it which could be ideal if you have few arguments is as follows.

First make a function that sets up the parser and returns the namespace. For example:

def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument("-a")
    parser.add_argument("-b", type=int)
    return parser.parse_args()

Then you define a main function which takes the args you declared above individually; like so.

def main(a: str, b: int):
    print("hello world", a, b)

And when you call your main, you do it like this:

if __name__ == "__main__":
    main(**vars(parse_args())

From your main onwards, you'll have your variables a and b properly recognised by your static type checker, although you won't have an object any more containing all your arguments, which may be a good or bad thing depending on your use case.


问:
Django won't refresh staticfiles
I have a JavaScript file referenced on a django template:

```<code> <script src="{% static 'js/login.js' %} "></script>
</code>```

I made some changes to that js file. Then, I refresh the page and I can't see the changes happen.

If I remove the JavaScript from the file and put it in the HTML, it works correctly, but if I try to do it using the external js file then it doesn't. I have tried closing the server and running runserver several times, I've also tried changing from Firefox to Chrome. It makes no sense at all. Please help me understand, I can get away with including all the js inside the HTML but it bothers me that I can't do it the normal way anymore.

More details about this error (the #$&%# weirdest thing I've found):

The JavaScript is something like this:

```<code>old_sentence = "Dig that hole, forget the sun"  // this is what I want to change
$("#jqselector").text(old_sentence)

new_sentence = "Run, rabbit, run"  // this is the new sentence, it just won't show up.
</code>```

So, I changed the js and restart the server, still the html shows the old sentence. Then I deleted the reference to the login.js file and put all the js inside script tags in the HTML, of course, the new sentence appears. Then I include the login.js file, comment out the js inside the html but I delete all the content inside the login.js file, making it an empty file... but the old sentence still shows up.

Therefore the old login.js file must be cashed somewhere I don't know. Then I open Chrome and try it all again, same problem.

What can it be? is there an option to force django to refresh staticfiles? I thought restarting the server was enough. Should I reboot my computer?

答: first run   python manage.py collectstatic --link -l then python manage.py collectstatic do not use python manage.py collectstatic --noinput --clear it will bring down your server.

Title: Django won't refresh staticfiles

Body:
I have a JavaScript file referenced on a django template:

I made some changes to that js file. Then, I refresh the page and I can't see the changes happen.

If I remove the JavaScript from the file and put it in the HTML, it works correctly, but if I try to do it using the external js file then it doesn't. I have tried closing the server and running runserver several times, I've also tried changing from Firefox to Chrome. It makes no sense at all. Please help me understand, I can get away with including all the js inside the HTML but it bothers me that I can't do it the normal way anymore.

More details about this error (the #$&%# weirdest thing I've found):

The JavaScript is something like this:

old_sentence = "Dig that hole, forget the sun"  // this is what I want to change
$("#jqselector").text(old_sentence)

new_sentence = "Run, rabbit, run"  // this is the new sentence, it just won't show up.

So, I changed the js and restart the server, still the html shows the old sentence. Then I deleted the reference to the login.js file and put all the js inside script tags in the HTML, of course, the new sentence appears. Then I include the login.js file, comment out the js inside the html but I delete all the content inside the login.js file, making it an empty file... but the old sentence still shows up.

Therefore the old login.js file must be cashed somewhere I don't know. Then I open Chrome and try it all again, same problem.

What can it be? is there an option to force django to refresh staticfiles? I thought restarting the server was enough. Should I reboot my computer?


问:
size of C structure
```<code>struct st1{
    int a:1; int b:3; int c:6; int d:3;
}s1;

struct st2{
    char a:3;
}s2;

int main(){
    printf("%d : %d",sizeof(s1),sizeof(s2));
    getchar();
}
</code>```

I am getting the output as 2 : 1

will you please tell me, how this program works and whats the use of : operator (a:1) here.

Thank you

答:
The : defines a bit-field.

In your example, objects of type struct st1 use 13 bits in some arrangement chosen by the compiler.

The particular arrangement chosen when you compiled the code originated an object that occupies 2 bytes. The 13 bits are not necessarily the first (or last) in those bytes.

The other struct type (struct st2) occupies (3 bits out of) 1 byte.

Title: size of C structure

Body:
struct st1{
    int a:1; int b:3; int c:6; int d:3;
}s1;

struct st2{
    char a:3;
}s2;

int main(){
    printf("%d : %d",sizeof(s1),sizeof(s2));
    getchar();
}

I am getting the output as 2 : 1

will you please tell me, how this program works and whats the use of : operator (a:1) here.

Thank you


问:
mute/unmute icon js fontawesome
My function should change the icon to mute or to unmute but it sometimes said that the class is undefined. I don't know why.

var i = document.getElementById("sound")
var unmute = false

function change() {
  console.log(i.className)
  if (unmute == false) {
    i.className = "fas fa-volume volumen"
    console.log("unmute")
    unmute = true;
  } else {
    i.className = "fas fa-volume-mute volumen"
    console.log("mute")
    unmute = false
  }

}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<strike>
    <i id="sound" onclick="change()" class="fas fa-volume-mute volumen"></i>
<strike></code>```

答:
this have no sense but it works me i declare the var i (now called sound) as local inside the function and now it works</p>
```<code>var unmute = false
function change(){
let sound = document.getElementById("sound")
console.log(sound.className);
if(unmute == false){
sound.className ="fal fa-volume volumen"
console.log("unmute")
unmute = true;
     }
else{
      sound.className ="fal fa-volume-mute volumen"
      console.log("mute")
      unmute = false
    }

    }
</code>```

Title: mute/unmute icon js fontawesome

Body:
My function should change the icon to mute or to unmute but it sometimes said that the class is undefined. I don't know why.

var i = document.getElementById("sound")
var unmute = false

function change() {
  console.log(i.className)
  if (unmute == false) {
    i.className = "fas fa-volume volumen"
    console.log("unmute")
    unmute = true;
  } else {
    i.className = "fas fa-volume-mute volumen"
    console.log("mute")
    unmute = false
  }

}

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />

Body:
this have no sense but it works me i declare the var i (now called sound) as local inside the function and now it works

var unmute = false
function change(){
let sound = document.getElementById("sound")
console.log(sound.className);
if(unmute == false){
sound.className ="fal fa-volume volumen"
console.log("unmute")
unmute = true;
     }
else{
      sound.className ="fal fa-volume-mute volumen"
      console.log("mute")
      unmute = false
    }

    }


问:
Connecting to Aurora Postgres (Babelfish, 1433)
I'm attempting to connect to a new Aurora PostgreSQL instance with Babelfish enabled.

NOTE: I am able to connect to the instance using the pg library through the normal port 5432 (the Postgres TDAS endpoint).

However, for this test, I am attempting to connect through the Babelfish TDS endpoint (1433) using the standard mssql package.

If I specify a database name (it is correct), I receive the error 'database "postgres" does not exist':

```<code>var config = {
    server: 'xxx.us-east-1.rds.amazonaws.com',
    database: 'postgres',
    user: 'xxx',
    password: 'xxx'
};
</code>```

and the connection closes since the connection fails.

if I omit the database property in the config, like:

```<code>var config = {
    server: 'xxx.us-east-1.rds.amazonaws.com',
    user: 'xxx',
    password: 'xxx'
};
</code>```

It will connect. Also, I can use that connection to query basic things like SELECT CURRENT_TIMESTAMP and it works!

However, I can't access any tables.

If I run:

```<code>SELECT COUNT(1) FROM PERSON
</code>```

I receive an error 'relation "person" does not exist'.

If I dot-notate it:

```<code>SELECT COUNT(1) FROM postgres.dbo."PERSON"
</code>```

I receive an error "Cross DB query is not supported".

So, I can't connect to the specific database directly and if I connect without specifying a database, I can't cross-query to the table.

Any one done this yet?

Or, if not, any ideas on helping me figure out what to try next? I'm out of ideas.

答:
The bigger answer here is that cross-DB object references are not currently supported in Babelfish, outside your current SQL Server database.
This is currently being worked on. Stay tuned.</p>

Body:
I'm attempting to connect to a new Aurora PostgreSQL instance with Babelfish enabled.

NOTE: I am able to connect to the instance using the pg library through the normal port 5432 (the Postgres TDAS endpoint).

However, for this test, I am attempting to connect through the Babelfish TDS endpoint (1433) using the standard mssql package.

If I specify a database name (it is correct), I receive the error 'database "postgres" does not exist':

var config = {
    server: 'xxx.us-east-1.rds.amazonaws.com',
    database: 'postgres',
    user: 'xxx',
    password: 'xxx'
};

and the connection closes since the connection fails.

if I omit the database property in the config, like:

var config = {
    server: 'xxx.us-east-1.rds.amazonaws.com',
    user: 'xxx',
    password: 'xxx'
};

It will connect. Also, I can use that connection to query basic things like SELECT CURRENT_TIMESTAMP and it works!

However, I can't access any tables.

If I run:

SELECT COUNT(1) FROM PERSON

I receive an error 'relation "person" does not exist'.

If I dot-notate it:

SELECT COUNT(1) FROM postgres.dbo."PERSON"

I receive an error "Cross DB query is not supported".

So, I can't connect to the specific database directly and if I connect without specifying a database, I can't cross-query to the table.

Any one done this yet?

Or, if not, any ideas on helping me figure out what to try next? I'm out of ideas.