问:
Convert nested JSON to CSV
I am converting a nested JSON object with more than 10 levels to CSV file in C# .NET.

I have been using JavaScriptSerializer().Deserialize<ObjectA>(json) or XmlNode xml = (XmlDocument)JsonConvert.DeserializeXmlNode(json) to break down the object. With the objects I can further write into CSV file. However now the JSON object further expand. Most data that is not really in use so I would prefer a raw data dump.

Is that easier way I can just dump the data into csv format without declaring the structure?

Sample JSON

```<code>{
"F1":1,
"F2":2,
"F3":[
    {
        "E1":3,
        "E2":4
    },
    {
        "E1":5,
        "E2":6
    },
    {
        "E1":7,
        "E2":8,
        "E3":[
            {
                "D1":9,
                "D2":10
            }
        ]
    },
]
}
</code>```

And my expected CSV output is

```<code>F1,F2,E1,E2,D1,D2
1,2
1,2,3,4
1,2,5,6
1,2,7,8,9,10
</code>```

答:
pass the json parsed jobject to static extension, then it will return the array of jobjects with flatten, then convert to csv. some of the source code is taken from the other stack overflow and other resources, I don't have reference.</p>
```<code> public static IEnumerable<string> JsonToCsvRowsWithHierarchyHeaders(string jsonData)
    {
        if (jsonData.Trim().StartsWith("[") && jsonData.Trim().EndsWith("]"))
        {
            var startString = "{\"appendRoot\":";
            var endString = "}";
            jsonData = $"{startString}{jsonData}{endString}";
        }

        var jObject = JsonConvert.DeserializeObject<JObject>(jsonData);
        var flattenJObjects = JsonParserExtensions.FlattenJsonGetJObjects(jObject).ToList();

        var csvRows = new List<string>();
        if (flattenJObjects.Any())
        {
            var firstRow = flattenJObjects.First().Children<JProperty>().Select(x => x.Name).ToList();
            var header = string.Join(delimeter, firstRow).Replace("appendRoot_", "").ToLower();
            csvRows.Add(header);
            foreach (var flattenJObject in flattenJObjects)
            {
                var tokens = flattenJObject.Children<JProperty>();

                if (firstRow.Count() != tokens.Count())
                {
                    var missMatchPropertyValues = firstRow.Select(cell => tokens.FirstOrDefault(x => x.Name == cell))
                        .Select(value => value == null
                            ? string.Empty
                            : CheckAndUpdateRowCellValueTextQualifier(JsonConvert.DeserializeObject<string>(value.Value.ToString(Newtonsoft.Json.Formatting.None))))
                        .ToList();
                    var rowString = string.Join(delimeter, missMatchPropertyValues);
                    csvRows.Add(rowString);
                }
                else
                {
                    var rowValue = tokens.Select(token =>
                        CheckAndUpdateRowCellValueTextQualifier(
                            JsonConvert.DeserializeObject<string>(
                                token.Value.ToString(Newtonsoft.Json.Formatting.None))));
                    var rowString = string.Join(delimeter, rowValue);
                    csvRows.Add(rowString);
                }
            }
        }
        return csvRows;
    }

    private static string CheckAndUpdateRowCellValueTextQualifier(string value)
    {
        const string q = @"""";
        if (!string.IsNullOrEmpty(value) && value.Contains('\"'))
        {
            return value;
        }

        if (!string.IsNullOrEmpty(value) && (value.Contains(',') ||
                                         value.Contains('"') ||
                                         value.Contains('\n') || value.Contains('\r')))
        {
            return $"{q}{value}{q}";
        }

        return value;
    }
}

public static class JsonParserExtensions
{
    public static IEnumerable<JObject> FlattenJsonGetJObjects(JObject jObject, string parentName = null)
    {
        if (!(parentName is null))
            jObject = RenamePropertiesByHierarchyName(jObject, parentName);

        var fields = jObject.Properties().Where(p => p.Value.GetType().Name == "JValue").ToList();
        var objects = jObject.Properties().Where(p => p.Value.GetType().Name == "JObject").ToList();
        var arrays = jObject.Properties().Where(p => p.Value.GetType().Name == "JArray").ToList();
        var objectsArray = arrays.Where(array => array.Value.All(elements => elements.GetType().Name == "JObject")).ToList();
        var valuesArray = arrays.Where(array => array.Value.All(elements => elements.GetType().Name == "JValue")).ToList();

        var nestedObjects = ProcessNestedObjects(objects);

        var joinedInnerObjects = nestedObjects.Any()
            ? nestedObjects.Select(innerObject => JoinJObject(new JObject(fields), innerObject))
            : new List<JObject> { new JObject(fields) };

        var arraysObjectList = GetJObjectsFromArrayOfJProperties(objectsArray);
        var arraysValueList = GetJObjectsFromArrayOfValues(valuesArray);
        var joinedAll = joinedInnerObjects.SelectMany(inner => JoinMultipleJObjects(arraysObjectList, arraysValueList, inner));
        return joinedAll;
    }

    public static List<JObject> ProcessNestedObjects(List<JProperty> jObjects)
    {
        var processNestedObjects = new List<JObject>();
        var renamedJObjects = jObjects?.Select(obj => RenamePropertiesByHierarchyName(obj.Value.ToObject<JObject>(), obj.Name)).ToList();

        if (!(renamedJObjects?.Count > 0)) return processNestedObjects;
        var renamed = renamedJObjects.Aggregate((acc, next) => JoinJObject(acc, next));

        var nestedObjects = renamed.Properties().Where(p => p.Value.GetType().Name == "JObject").ToList();
        var nestedArrays = renamed.Properties().Where(p => p.Value.GetType().Name == "JArray").ToList();
        var nestedObjectsArray = nestedArrays.Where(array => array.Value.All(elements => elements.GetType().Name == "JObject")).ToList();
        var nestedValuesArray = nestedArrays.Where(array => array.Value.All(elements => elements.GetType().Name == "JValue")).ToList();

        nestedArrays.ForEach(p => renamed.Remove(p.Name));
        nestedObjects.ForEach(p => renamed.Remove(p.Name));

        var nestedObjectList = new List<JObject>();
        var nestedMultipleObjectList = new List<JObject>();
        foreach (var listJObjects in nestedObjects.Select(innerObject => FlattenJsonGetJObjects(innerObject.Value.ToObject<JObject>(), innerObject.Name)).ToList())
        {
            if (listJObjects.Count() > 1)
                nestedMultipleObjectList.AddRange(listJObjects);
            else
                nestedObjectList.Add(listJObjects.First());
        }

        var jObjectsFromArrayOfJProperties = GetJObjectsFromArrayOfJProperties(nestedObjectsArray);
        var jObjectsFromArrayOfValues = GetJObjectsFromArrayOfValues(nestedValuesArray);

        var aggregate = nestedObjectList.Aggregate(renamed, (acc, next) => JoinJObject(acc, next));
        var groupedNestedObjects = (nestedMultipleObjectList.Any()) ? nestedMultipleObjectList.Select(nested => JoinJObject(aggregate, nested))
            : new List<JObject> { aggregate };
        var groupedNestedObjectsList = groupedNestedObjects.Select(groupedNested => JoinMultipleJObjects(jObjectsFromArrayOfJProperties, jObjectsFromArrayOfValues, groupedNested));
        processNestedObjects.AddRange(groupedNestedObjectsList.SelectMany(e => e));
        return processNestedObjects;
    }

    public static List<JObject> JoinMultipleJObjects(List<JObject> nestedArraysObjectList, List<JObject> nestedArraysValueList, JObject groupedNestedObjects)
    {
        var result = new List<JObject>();
        var joined = new List<JObject>();
        if (!nestedArraysObjectList.Any())
            joined.Add(groupedNestedObjects);
        else
            nestedArraysObjectList.ForEach(e => joined.Add(JoinJObject(groupedNestedObjects, e)));

        result.AddRange(nestedArraysValueList.Any()
            ? nestedArraysValueList
                .SelectMany(value => joined, (value, joinedItem) => JoinJObject(joinedItem, value)).ToList()
            : joined);
        return result;
    }

    public static List<JObject> GetJObjectsFromArrayOfJProperties(List<JProperty> nestedJProperties)
    {
        var fromArrayOfJProperties = new List<JObject>();
        foreach (var jProperty in nestedJProperties)
        {
            var nestedArraysObjectList = new List<JObject>();
            var name = jProperty.Name;
            var jPropertyValue = jProperty.Value;
            var renamedObjects = jPropertyValue?.Select(obj => RenamePropertiesByHierarchyName(obj.ToObject<JObject>(), name)).ToList();
            foreach (var jObjects in renamedObjects.Select(innerObject => FlattenJsonGetJObjects(innerObject.ToObject<JObject>())))
            {
                nestedArraysObjectList.AddRange(jObjects);
            }

            if (fromArrayOfJProperties.Any() && nestedArraysObjectList.Any())
                fromArrayOfJProperties = nestedArraysObjectList
                    .SelectMany(nested => fromArrayOfJProperties, (current, joined) => JoinJObject(joined, current)).ToList();

            if (!fromArrayOfJProperties.Any())
                fromArrayOfJProperties.AddRange(nestedArraysObjectList);
        }
        return fromArrayOfJProperties;
    }

    public static List<JObject> GetJObjectsFromArrayOfValues(List<JProperty> nestedValuesArray)
    {
        return (from innerArray in nestedValuesArray let name = innerArray.Name let values = innerArray.Value from innerValue in values select new JObject(new JProperty(name, innerValue.ToObject<JValue>()))).ToList();
    }

    public static JObject RenamePropertiesByHierarchyName(JObject jObject, string hierarchyName)
    {
        var properties = jObject.Properties().ToList().Select(p => new JProperty($"{hierarchyName}_{p.Name}", p.Value));
        return new JObject(properties);
    }

    public static JObject JoinJObject(JObject parentJObject, JObject innerObject)
    {
        var joinJObject = new JObject
            {
                parentJObject.Properties(),
                innerObject.Properties()
            };
        return joinJObject;
    }
}
</code>```

Title: Convert nested JSON to CSV

Body:
I am converting a nested JSON object with more than 10 levels to CSV file in C# .NET.

I have been using JavaScriptSerializer().Deserialize<ObjectA>(json) or XmlNode xml = (XmlDocument)JsonConvert.DeserializeXmlNode(json) to break down the object. With the objects I can further write into CSV file. However now the JSON object further expand. Most data that is not really in use so I would prefer a raw data dump.

Is that easier way I can just dump the data into csv format without declaring the structure?

Sample JSON

{
"F1":1,
"F2":2,
"F3":[
    {
        "E1":3,
        "E2":4
    },
    {
        "E1":5,
        "E2":6
    },
    {
        "E1":7,
        "E2":8,
        "E3":[
            {
                "D1":9,
                "D2":10
            }
        ]
    },
]
}

And my expected CSV output is

F1,F2,E1,E2,D1,D2
1,2
1,2,3,4
1,2,5,6
1,2,7,8,9,10

Body:
pass the json parsed jobject to static extension, then it will return the array of jobjects with flatten, then convert to csv. some of the source code is taken from the other stack overflow and other resources, I don't have reference.

 public static IEnumerable<string> JsonToCsvRowsWithHierarchyHeaders(string jsonData)
    {
        if (jsonData.Trim().StartsWith("[") && jsonData.Trim().EndsWith("]"))
        {
            var startString = "{\"appendRoot\":";
            var endString = "}";
            jsonData = $"{startString}{jsonData}{endString}";
        }

        var jObject = JsonConvert.DeserializeObject<JObject>(jsonData);
        var flattenJObjects = JsonParserExtensions.FlattenJsonGetJObjects(jObject).ToList();

        var csvRows = new List<string>();
        if (flattenJObjects.Any())
        {
            var firstRow = flattenJObjects.First().Children<JProperty>().Select(x => x.Name).ToList();
            var header = string.Join(delimeter, firstRow).Replace("appendRoot_", "").ToLower();
            csvRows.Add(header);
            foreach (var flattenJObject in flattenJObjects)
            {
                var tokens = flattenJObject.Children<JProperty>();

                if (firstRow.Count() != tokens.Count())
                {
                    var missMatchPropertyValues = firstRow.Select(cell => tokens.FirstOrDefault(x => x.Name == cell))
                        .Select(value => value == null
                            ? string.Empty
                            : CheckAndUpdateRowCellValueTextQualifier(JsonConvert.DeserializeObject<string>(value.Value.ToString(Newtonsoft.Json.Formatting.None))))
                        .ToList();
                    var rowString = string.Join(delimeter, missMatchPropertyValues);
                    csvRows.Add(rowString);
                }
                else
                {
                    var rowValue = tokens.Select(token =>
                        CheckAndUpdateRowCellValueTextQualifier(
                            JsonConvert.DeserializeObject<string>(
                                token.Value.ToString(Newtonsoft.Json.Formatting.None))));
                    var rowString = string.Join(delimeter, rowValue);
                    csvRows.Add(rowString);
                }
            }
        }
        return csvRows;
    }

    private static string CheckAndUpdateRowCellValueTextQualifier(string value)
    {
        const string q = @"""";
        if (!string.IsNullOrEmpty(value) && value.Contains('\"'))
        {
            return value;
        }

        if (!string.IsNullOrEmpty(value) && (value.Contains(',') ||
                                         value.Contains('"') ||
                                         value.Contains('\n') || value.Contains('\r')))
        {
            return $"{q}{value}{q}";
        }

        return value;
    }
}

public static class JsonParserExtensions
{
    public static IEnumerable<JObject> FlattenJsonGetJObjects(JObject jObject, string parentName = null)
    {
        if (!(parentName is null))
            jObject = RenamePropertiesByHierarchyName(jObject, parentName);

        var fields = jObject.Properties().Where(p => p.Value.GetType().Name == "JValue").ToList();
        var objects = jObject.Properties().Where(p => p.Value.GetType().Name == "JObject").ToList();
        var arrays = jObject.Properties().Where(p => p.Value.GetType().Name == "JArray").ToList();
        var objectsArray = arrays.Where(array => array.Value.All(elements => elements.GetType().Name == "JObject")).ToList();
        var valuesArray = arrays.Where(array => array.Value.All(elements => elements.GetType().Name == "JValue")).ToList();

        var nestedObjects = ProcessNestedObjects(objects);

        var joinedInnerObjects = nestedObjects.Any()
            ? nestedObjects.Select(innerObject => JoinJObject(new JObject(fields), innerObject))
            : new List<JObject> { new JObject(fields) };

        var arraysObjectList = GetJObjectsFromArrayOfJProperties(objectsArray);
        var arraysValueList = GetJObjectsFromArrayOfValues(valuesArray);
        var joinedAll = joinedInnerObjects.SelectMany(inner => JoinMultipleJObjects(arraysObjectList, arraysValueList, inner));
        return joinedAll;
    }

    public static List<JObject> ProcessNestedObjects(List<JProperty> jObjects)
    {
        var processNestedObjects = new List<JObject>();
        var renamedJObjects = jObjects?.Select(obj => RenamePropertiesByHierarchyName(obj.Value.ToObject<JObject>(), obj.Name)).ToList();

        if (!(renamedJObjects?.Count > 0)) return processNestedObjects;
        var renamed = renamedJObjects.Aggregate((acc, next) => JoinJObject(acc, next));

        var nestedObjects = renamed.Properties().Where(p => p.Value.GetType().Name == "JObject").ToList();
        var nestedArrays = renamed.Properties().Where(p => p.Value.GetType().Name == "JArray").ToList();
        var nestedObjectsArray = nestedArrays.Where(array => array.Value.All(elements => elements.GetType().Name == "JObject")).ToList();
        var nestedValuesArray = nestedArrays.Where(array => array.Value.All(elements => elements.GetType().Name == "JValue")).ToList();

        nestedArrays.ForEach(p => renamed.Remove(p.Name));
        nestedObjects.ForEach(p => renamed.Remove(p.Name));

        var nestedObjectList = new List<JObject>();
        var nestedMultipleObjectList = new List<JObject>();
        foreach (var listJObjects in nestedObjects.Select(innerObject => FlattenJsonGetJObjects(innerObject.Value.ToObject<JObject>(), innerObject.Name)).ToList())
        {
            if (listJObjects.Count() > 1)
                nestedMultipleObjectList.AddRange(listJObjects);
            else
                nestedObjectList.Add(listJObjects.First());
        }

        var jObjectsFromArrayOfJProperties = GetJObjectsFromArrayOfJProperties(nestedObjectsArray);
        var jObjectsFromArrayOfValues = GetJObjectsFromArrayOfValues(nestedValuesArray);

        var aggregate = nestedObjectList.Aggregate(renamed, (acc, next) => JoinJObject(acc, next));
        var groupedNestedObjects = (nestedMultipleObjectList.Any()) ? nestedMultipleObjectList.Select(nested => JoinJObject(aggregate, nested))
            : new List<JObject> { aggregate };
        var groupedNestedObjectsList = groupedNestedObjects.Select(groupedNested => JoinMultipleJObjects(jObjectsFromArrayOfJProperties, jObjectsFromArrayOfValues, groupedNested));
        processNestedObjects.AddRange(groupedNestedObjectsList.SelectMany(e => e));
        return processNestedObjects;
    }

    public static List<JObject> JoinMultipleJObjects(List<JObject> nestedArraysObjectList, List<JObject> nestedArraysValueList, JObject groupedNestedObjects)
    {
        var result = new List<JObject>();
        var joined = new List<JObject>();
        if (!nestedArraysObjectList.Any())
            joined.Add(groupedNestedObjects);
        else
            nestedArraysObjectList.ForEach(e => joined.Add(JoinJObject(groupedNestedObjects, e)));

        result.AddRange(nestedArraysValueList.Any()
            ? nestedArraysValueList
                .SelectMany(value => joined, (value, joinedItem) => JoinJObject(joinedItem, value)).ToList()
            : joined);
        return result;
    }

    public static List<JObject> GetJObjectsFromArrayOfJProperties(List<JProperty> nestedJProperties)
    {
        var fromArrayOfJProperties = new List<JObject>();
        foreach (var jProperty in nestedJProperties)
        {
            var nestedArraysObjectList = new List<JObject>();
            var name = jProperty.Name;
            var jPropertyValue = jProperty.Value;
            var renamedObjects = jPropertyValue?.Select(obj => RenamePropertiesByHierarchyName(obj.ToObject<JObject>(), name)).ToList();
            foreach (var jObjects in renamedObjects.Select(innerObject => FlattenJsonGetJObjects(innerObject.ToObject<JObject>())))
            {
                nestedArraysObjectList.AddRange(jObjects);
            }

            if (fromArrayOfJProperties.Any() && nestedArraysObjectList.Any())
                fromArrayOfJProperties = nestedArraysObjectList
                    .SelectMany(nested => fromArrayOfJProperties, (current, joined) => JoinJObject(joined, current)).ToList();

            if (!fromArrayOfJProperties.Any())
                fromArrayOfJProperties.AddRange(nestedArraysObjectList);
        }
        return fromArrayOfJProperties;
    }

    public static List<JObject> GetJObjectsFromArrayOfValues(List<JProperty> nestedValuesArray)
    {
        return (from innerArray in nestedValuesArray let name = innerArray.Name let values = innerArray.Value from innerValue in values select new JObject(new JProperty(name, innerValue.ToObject<JValue>()))).ToList();
    }

    public static JObject RenamePropertiesByHierarchyName(JObject jObject, string hierarchyName)
    {
        var properties = jObject.Properties().ToList().Select(p => new JProperty($"{hierarchyName}_{p.Name}", p.Value));
        return new JObject(properties);
    }

    public static JObject JoinJObject(JObject parentJObject, JObject innerObject)
    {
        var joinJObject = new JObject
            {
                parentJObject.Properties(),
                innerObject.Properties()
            };
        return joinJObject;
    }
}


来源: stackexchange

问:
How I can collect users’ tweets without typing their accounts
I would like to ask if there is a way to collect several tweets about different users, whether by Python or R, without specifying their account names?</p>

答:
I sugest you to look the twitter API. Any kind of request can be done.
Twitter API

For instance :
import tweepy as tw

```<code>my_api_key = "XXXXXXXXXXXXXXXXX"
my_api_secret = "XXXXXXXXXXXXXXXXXXXXXXX"

auth = tw.OAuthHandler(my_api_key, my_api_secret)
api = tw.API(auth, wait_on_rate_limit=True)
    # get tweets from the API
    tweets = tw.Cursor(api.search,
                  q="#covid19",
                  lang="en",
                  since="2020-09-16").items(50)
    # store the API responses in a list
    tweets_copy = []
    for tweet in tweets:
        tweets_copy.append(tweet)

    print("Total Tweets fetched:", len(tweets_copy))
</code>```

More detailed example could be found here : Data science

PS: You need to be more explicit next time. It's hard to help someone without any details.

ContentLicense: CC BY-SA 4.0

Body:
I sugest you to look the twitter API. Any kind of request can be done.
Twitter API

For instance :
import tweepy as tw

my_api_key = "XXXXXXXXXXXXXXXXX"
my_api_secret = "XXXXXXXXXXXXXXXXXXXXXXX"

auth = tw.OAuthHandler(my_api_key, my_api_secret)
api = tw.API(auth, wait_on_rate_limit=True)
    # get tweets from the API
    tweets = tw.Cursor(api.search,
                  q="#covid19",
                  lang="en",
                  since="2020-09-16").items(50)
    # store the API responses in a list
    tweets_copy = []
    for tweet in tweets:
        tweets_copy.append(tweet)

    print("Total Tweets fetched:", len(tweets_copy))

More detailed example could be found here : Data science

PS: You need to be more explicit next time. It's hard to help someone without any details.


问:
Django how to sort and paginate nested ORM and non-ORM object
I need to make a query to get some properties and with its category FK get data for monthly cost in another table. With the result, need to calc value by day, installments, total price and due date.

all works fine but there is a need to order data by some fields of the Property model, order by total price and paginate the result.

i could make both, order and paginate work, but not together.

Also, i'd like to ask if those calcs with installments is right to be placed on serializer.py, or should be moved to the viewset.py, or somewhere else?

Here is my code so far:

ViewSet.py with pagination:

class MyViewSet(viewsets.ModelViewSet):
    queryset = MyModel.Property.objects.filter(status)\
        .prefetch_related('owner__user')\
        .prefetch_related('host__user')\
        .prefetch_related('category__user')

    serializer_class = MySerializer
    pagination_class = StandardResultsSetPagination
    filterset_class = MyFilterSet
    filter_backends = [filters.OrderingFilter, dj_filters.DjangoFilterBackend]

    def get_queryset(self):
        today = str(datetime.now())
        start_date = self.request.query_params.get('start_date')
        end_date = self.request.query_params.get('end_date')

        '''
        validate start_date and end_date
        '''

        return self.queryset

```
<p>ViewSet.py with sort:</p>
<pre class="lang-py prettyprint-override"><code>class MyViewSet(viewsets.ModelViewSet):
    queryset = MyModel.Property.objects.filter(status)\
        .prefetch_related('owner__user')\
        .prefetch_related('host__user')\
        .prefetch_related('category__user')

    serializer_class = MySerializer
    # pagination_class = StandardResultsSetPagination
    filterset_class = MyFilterSet
    filter_backends = [filters.OrderingFilter, dj_filters.DjangoFilterBackend]

    def get_queryset(self):
        today = str(datetime.now())
        start_date = self.request.query_params.get('start_date')
        end_date = self.request.query_params.get('end_date')

        '''
        validate start_date and end_date
        '''

        return self.queryset

    def list(self, request, *args, **kwargs):
        response = super().list(request, args, kwargs)
        ordering = request.query_params.get('ordering')

        if ordering:
            response.data = sorted(
                response.data,
                key=operator.itemgetter(ordering.replace('-', ''),)
            )

            if "-" in ordering:
                response.data = sorted(
                    response.data,
                    key=lambda k: (k[ordering.replace('-', '')], ),
                    reverse=True
                )
            else:
                response.data = sorted(
                    response.data,
                    key=lambda k: (k[ordering], )
                )
        return Response(response.data)
</code>```

Serializer.py

class MySerializer(serializers.ModelSerializer):
    monthly_price = serializers.SerializerMethodField()
    total_price = serializers.SerializerMethodField()

    class Meta:
        model = MyModel.Property

    def get_monthly_price(self, instance):
        start_date = self.context['request'].query_params['start_date']
        end_date = self.context['request'].query_params['end_date']

        months=helper.calc_months(start_date, end_date)
        '''
        calc months between start_date and end_date
        '''

        prices = MyModel.Category_month_price.objects.filter(
            category=instance.category_location.id,
            month__in=months)

        installments = helper.calc_installments(dates, start_date, prices)
        '''
        installments is a list of dicts with installments due dates and values like:
        [{
            "instalment": 0,
            "value": 1000,
            "due_date": 2022-01-01,
        }, ...]
        '''

        total_price = sum([x['value'] for x in installments])
        self.total_price = total_price

        return installments

    def get_total_price(self, instance):
        return self.total_price
```
<p>Also tried whit Paginator at <code>list Method</code>, but didn't find a way to buil next and previous pages link</p>
<pre class="lang-py prettyprint-override"><code>from django.core.paginator import Paginator
from django.core.paginator import EmptyPage
from django.core.paginator import PageNotAnInteger

def list(self, ...) :
    """
    ...
    """
    paginator = Paginator(response.data, 25)
    page = self.request.GET.get('page')
    try:
        result = paginator.page(page)
    except PageNotAnInteger:
        result = paginator.page(1)
    except EmptyPage:
        result = paginator.page(paginator.num_pages)

    return Response({
        "count": "",
        "next": "",
        "previous": "",
        "results": []
    )

</code>```

答:
I Found a simple solution:

class MyViewSet(viewsets.ModelViewSet):
    queryset = MyModel.Property.objects.filter(status)\
        .prefetch_related('owner__user')\
        .prefetch_related('host__user')\
        .prefetch_related('category__user')

    serializer_class = MySerializer
    filterset_class = MyFilterSet
    filter_backends = [filters.OrderingFilter, dj_filters.DjangoFilterBackend]

    def get_queryset(self):
        today = str(datetime.now())
        start_date = self.request.query_params.get('start_date')
        end_date = self.request.query_params.get('end_date')

        '''
        validate start_date and end_date
        '''

        return self.queryset

    def list(self, request, *args, **kwargs):
        response = super().list(request, args, kwargs)
        response.request = self.request
        ordering = request.query_params.get('ordering')

        # ORDERING WORKING
        if ordering:
            response.data = sorted(
                response.data,
                key=operator.itemgetter(ordering.replace('-', ''),)
            )

            if "-" in ordering:
                response.data = sorted(
                    response.data,
                    key=lambda k: (k[ordering.replace('-', '')], ),
                    reverse=True
                )
            else:
                response.data = sorted(
                    response.data,
                    key=lambda k: (k[ordering], )
                )

        # HERE IS ALL I NEED TO DO TO GET PAGINATION WORK
        paginator = StandardResultsSetPagination()
        result_page = paginator.paginate_queryset(response.data, request)

        return paginator.get_paginated_response(result_page)
```

Body:
I need to make a query to get some properties and with its category FK get data for monthly cost in another table. With the result, need to calc value by day, installments, total price and due date.

all works fine but there is a need to order data by some fields of the Property model, order by total price and paginate the result.

i could make both, order and paginate work, but not together.

Also, i'd like to ask if those calcs with installments is right to be placed on serializer.py, or should be moved to the viewset.py, or somewhere else?

Here is my code so far:

ViewSet.py with pagination:

class MyViewSet(viewsets.ModelViewSet):
    queryset = MyModel.Property.objects.filter(status)\
        .prefetch_related('owner__user')\
        .prefetch_related('host__user')\
        .prefetch_related('category__user')

    serializer_class = MySerializer
    pagination_class = StandardResultsSetPagination
    filterset_class = MyFilterSet
    filter_backends = [filters.OrderingFilter, dj_filters.DjangoFilterBackend]

    def get_queryset(self):
        today = str(datetime.now())
        start_date = self.request.query_params.get('start_date')
        end_date = self.request.query_params.get('end_date')

        '''
        validate start_date and end_date
        '''

        return self.queryset

ViewSet.py with sort:

class MyViewSet(viewsets.ModelViewSet):
    queryset = MyModel.Property.objects.filter(status)\
        .prefetch_related('owner__user')\
        .prefetch_related('host__user')\
        .prefetch_related('category__user')

    serializer_class = MySerializer
    # pagination_class = StandardResultsSetPagination
    filterset_class = MyFilterSet
    filter_backends = [filters.OrderingFilter, dj_filters.DjangoFilterBackend]

    def get_queryset(self):
        today = str(datetime.now())
        start_date = self.request.query_params.get('start_date')
        end_date = self.request.query_params.get('end_date')

        '''
        validate start_date and end_date
        '''

        return self.queryset

    def list(self, request, *args, **kwargs):
        response = super().list(request, args, kwargs)
        ordering = request.query_params.get('ordering')

        if ordering:
            response.data = sorted(
                response.data,
                key=operator.itemgetter(ordering.replace('-', ''),)
            )

            if "-" in ordering:
                response.data = sorted(
                    response.data,
                    key=lambda k: (k[ordering.replace('-', '')], ),
                    reverse=True
                )
            else:
                response.data = sorted(
                    response.data,
                    key=lambda k: (k[ordering], )
                )
        return Response(response.data)

Serializer.py

class MySerializer(serializers.ModelSerializer):
    monthly_price = serializers.SerializerMethodField()
    total_price = serializers.SerializerMethodField()

    class Meta:
        model = MyModel.Property

    def get_monthly_price(self, instance):
        start_date = self.context['request'].query_params['start_date']
        end_date = self.context['request'].query_params['end_date']

        months=helper.calc_months(start_date, end_date)
        '''
        calc months between start_date and end_date
        '''

        prices = MyModel.Category_month_price.objects.filter(
            category=instance.category_location.id,
            month__in=months)

        installments = helper.calc_installments(dates, start_date, prices)
        '''
        installments is a list of dicts with installments due dates and values like:
        [{
            "instalment": 0,
            "value": 1000,
            "due_date": 2022-01-01,
        }, ...]
        '''

        total_price = sum([x['value'] for x in installments])
        self.total_price = total_price

        return installments

    def get_total_price(self, instance):
        return self.total_price

Also tried whit Paginator at list Method, but didn't find a way to buil next and previous pages link

from django.core.paginator import Paginator
from django.core.paginator import EmptyPage
from django.core.paginator import PageNotAnInteger

def list(self, ...) :
    """
    ...
    """
    paginator = Paginator(response.data, 25)
    page = self.request.GET.get('page')
    try:
        result = paginator.page(page)
    except PageNotAnInteger:
        result = paginator.page(1)
    except EmptyPage:
        result = paginator.page(paginator.num_pages)

    return Response({
        "count": "",
        "next": "",
        "previous": "",
        "results": []
    )


问:
How do you test functions and closures for equality?
The book says that "functions and closures are reference types". So, how do you find out if the references are equal? == and === don't work.</p>

```<code>func a() { }
let å = a
let b = å === å // Could not find an overload for === that accepts the supplied arguments
</code>```

答:
You can use a callAsFunction method so for example

```<code>struct MyType: Equatable {
    func callAsFunction() {
        print("Image a function")
    }

    static func == (lhs: MyType, rhs: MyType) -> Bool { true }
}

let a = MyType()
let b = MyType()
a()
b()
let e = a == b
</code>```

In this case they are always going to be true, you can have initialiser t give them different internal states, or other methods to change there states, and the callAsFunction can be changed to take arguments

Not sure why === would not work on real functions because you are just testing address, but == invokes the == method of the Equatable protocol, and function don't implement this protocol

Body:
The book says that "functions and closures are reference types". So, how do you find out if the references are equal? == and === don't work.

func a() { }
let å = a
let b = å === å // Could not find an overload for === that accepts the supplied arguments

Body:
You can use a callAsFunction method so for example

struct MyType: Equatable {
    func callAsFunction() {
        print("Image a function")
    }

    static func == (lhs: MyType, rhs: MyType) -> Bool { true }
}

let a = MyType()
let b = MyType()
a()
b()
let e = a == b

In this case they are always going to be true, you can have initialiser t give them different internal states, or other methods to change there states, and the callAsFunction can be changed to take arguments

Not sure why === would not work on real functions because you are just testing address, but == invokes the == method of the Equatable protocol, and function don't implement this protocol


问:
Fast delimited JSON writing with arrow?
Is it possible to use the R arrow package to write JSON-LD? I know there are very fast methods to write CSV (arrow::write_csv_arrow) and parquet, and I have been using arrow::read_json_arrow for super fast reading of very large delimited JSON.

But to write I am still using writeLines which seems fairly slow. I wonder if there are plans to support this in arrow itself?

Thanks

Updated with reproducible example showing my current code. In reality the data is much larger and the JSON generation takes over 1 hour:

library(data.table)
library(arrow)
library(rbenchmark)

dt <- data.table(id = 1:1000)
for (i in seq(3)) dt[[paste0("numeric",i)]] <- runif(nrow(dt))
for (i in seq(4)) dt[[paste0("symbolic",i)]] <- as.character(paste0("X",round(1000*runif(nrow(dt)))))

listToJson <- function(l) {
  jsonlite::toJSON(l, auto_unbox = T, na = "null", digits = NA)
}

b <- benchmark( "row-wise json" = {
  writeLines(sapply(seq(nrow(dt)), function(r) { listToJson(as.list(dt[r,])) }), "test.json")
},
"write.csv" = {
  write.csv(dt, "test.csv")
},
"arrow::write_csv_arrow" = {
  arrow::write_csv_arrow(dt, "test.csv")
},
replications = 10
)
```

Results show how much slower my JSON writing is compared to CSV, even with write.csv:

test
replications
elapsed
relative
user.self
sys.self
user.child
sys.child

arrow::write_csv_arrow
10
0.029
1.000
0.027
0.004
0
0

write.csv
10
0.054
1.862
0.051
0.004
0
0

row-wise json
10
5.056
174.345
5.014
0.040
0
0

答:
It is not currently possible to do this with the R package (or the underlying C++ implementation).  There is a ticket open on the project which mentions this, but it has not been worked on. I have added a comment on there highlighting that it would be useful to have.

In the meantime, perhaps if you tell us a bit more about your code and data (how many rows/columns etc, the code you're using to write it), we could see if there's anything we can do to help make it it more efficient.

Title: Fast delimited JSON writing with arrow?

Body:
Is it possible to use the R arrow package to write JSON-LD? I know there are very fast methods to write CSV (arrow::write_csv_arrow) and parquet, and I have been using arrow::read_json_arrow for super fast reading of very large delimited JSON.

But to write I am still using writeLines which seems fairly slow. I wonder if there are plans to support this in arrow itself?

Thanks

Updated with reproducible example showing my current code. In reality the data is much larger and the JSON generation takes over 1 hour:

library(data.table)
library(arrow)
library(rbenchmark)

dt <- data.table(id = 1:1000)
for (i in seq(3)) dt[[paste0("numeric",i)]] <- runif(nrow(dt))
for (i in seq(4)) dt[[paste0("symbolic",i)]] <- as.character(paste0("X",round(1000*runif(nrow(dt)))))

listToJson <- function(l) {
  jsonlite::toJSON(l, auto_unbox = T, na = "null", digits = NA)
}

b <- benchmark( "row-wise json" = {
  writeLines(sapply(seq(nrow(dt)), function(r) { listToJson(as.list(dt[r,])) }), "test.json")
},
"write.csv" = {
  write.csv(dt, "test.csv")
},
"arrow::write_csv_arrow" = {
  arrow::write_csv_arrow(dt, "test.csv")
},
replications = 10
)

Results show how much slower my JSON writing is compared to CSV, even with write.csv:

test
replications
elapsed
relative
user.self
sys.self
user.child
sys.child

arrow::write_csv_arrow
10
0.029
1.000
0.027
0.004
0
0

write.csv
10
0.054
1.862
0.051
0.004
0
0

row-wise json
10
5.056
174.345
5.014
0.040
0
0


问:
finish git rebase operation after conflict
For my job I need to do a git rebase of my feature branch onto the development branch.

These are my commands thus far to prepare the rebase:

```<code>git checkout feature-branch
git rebase development
# resolve conflicts in Visual Studio
git rebase --continue
</code>```

After this, I'm getting the following in the cmd:

Now it's just as if I'm typing a novel in Notepad. Hitting enter, ctrl + C, ... doesn't do anything.

I've already read other questions like this one but this doesn't offer me a clue of how to complete the rebase / exit the shell.

So how do I finish this rebase operation? What buttons do I need to press?

答:
I don't have enough rep to post a comment, but as a side note it's possible to configure Git to use a different editor, for example
git config --global core.editor "code --wait" would change the default to be VS Code

more information on this answer
https://stackoverflow.com/a/36644561/18039381

Body:
For my job I need to do a git rebase of my feature branch onto the development branch.

These are my commands thus far to prepare the rebase:

git checkout feature-branch
git rebase development
# resolve conflicts in Visual Studio
git rebase --continue

After this, I'm getting the following in the cmd:

Now it's just as if I'm typing a novel in Notepad. Hitting enter, ctrl + C, ... doesn't do anything.

I've already read other questions like this one but this doesn't offer me a clue of how to complete the rebase / exit the shell.

So how do I finish this rebase operation? What buttons do I need to press?


问:
MSSQL Stored Procedure: Compare Parameter with Column if ISNUMERIC
I have following table "Managers" (simplified):

```<code>ID, int
Name, nvarchar(100)
</code>```

In a stored procedure that has one argument ("Search", type nvarchar), I want to select every row where

The ID-Column is exactly @Search OR

The Name-Column contains @Search.

At the moment, my select in the stored procedure looks something like this:

```<code>SELECT ID, Name FROM Managers WHERE
(ISNUMERIC(@Search) = 1 AND [ID] = CAST(@Search AS INT)) Or
Contains([Name], @Search)
</code>```

If I call the stored procedure with @Search = 1321 (example), the select works.

But if I have a @Search - parameter that is not numeric (example "HES"), I get the following error:
Conversion failed when converting the nvarchar value 'HES' to data type int.

How can I fix this?

Thanks in advance

Raphi

答:
```<code>SELECT ID, Name FROM Managers
WHERE [ID] = CASE
               WHEN ISNUMERIC(@Search) = 1 AND @Search NOT LIKE '%.%' THEN CAST(@Search AS INT)
               ELSE -1
             END
OR Contains([Name], @Search)
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I have following table "Managers" (simplified):

ID, int
Name, nvarchar(100)

In a stored procedure that has one argument ("Search", type nvarchar), I want to select every row where

The ID-Column is exactly @Search OR

The Name-Column contains @Search.

At the moment, my select in the stored procedure looks something like this:

SELECT ID, Name FROM Managers WHERE
(ISNUMERIC(@Search) = 1 AND [ID] = CAST(@Search AS INT)) Or
Contains([Name], @Search)

If I call the stored procedure with @Search = 1321 (example), the select works.

But if I have a @Search - parameter that is not numeric (example "HES"), I get the following error:
Conversion failed when converting the nvarchar value 'HES' to data type int.

How can I fix this?

Thanks in advance

Raphi


问:
Is there way to automate Snack Bar text using Protractor?
I'm trying by using bellow sample code but is not working properly, can you know anyone kindly suggest way or what are the changers to do.

getText: async function() {
  return await new Promise(async function(resolve, reject) {
    return await protractor.element(protractor.by.css('simple-snack-bar>span,.mat-simple-snackbar > span')).isPresent().then(async(isPresent) => {
      if (isPresent) {
        return await protractor.element(protractor.by.css('simple-snack-bar>span,.mat-simple-snackbar > span')).getText().then(async(snackBarText) => {
          resolve(snackBarText);
        }).catch(() => {
          return false;
        })
      } else {
        reject(isPresent);
        return false;
      }
    }).catch(() => {
      return false;
    })
  }).catch((error) => {
    if (error.name === 'StaleElementReferenceError') {
      return false;
    }
  })
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code>getText: async function () { return await new Promise(async function (resolve, reject) { return await protractor.element(protractor.by.css('simple-snack-bar>span,.mat-simple-snackbar > span')).isPresent().then(async (isPresent) => { if (isPresent) { return
await protractor.element(protractor.by.css('simple-snack-bar>span,.mat-simple-snackbar > span')).getText().then(async (snackBarText) => { resolve(snackBarText); }).catch(() => { return false; }) } else { reject(isPresent); return false; } }).catch(()
=> { return false; }) }).catch((error) => { if (error.name === 'StaleElementReferenceError') { return false; } }) }</code>```

答:
I think your function can simplify to following:</p>
```<code>getText: async function() {
    snackBarElement = element(by.css('simple-snack-bar>span,.mat-simple-snackbar > span'))

    try {
        isPresent = await snackBarElement.isPresent()
        if (isPresent) {
            return await snackBarElement.getText()
        }
    }
    catch(e) {}

    return 'Not find Snack Bar'
}
</code>```

Body:
I'm trying by using bellow sample code but is not working properly, can you know anyone kindly suggest way or what are the changers to do.

getText: async function() {
  return await new Promise(async function(resolve, reject) {
    return await protractor.element(protractor.by.css('simple-snack-bar>span,.mat-simple-snackbar > span')).isPresent().then(async(isPresent) => {
      if (isPresent) {
        return await protractor.element(protractor.by.css('simple-snack-bar>span,.mat-simple-snackbar > span')).getText().then(async(snackBarText) => {
          resolve(snackBarText);
        }).catch(() => {
          return false;
        })
      } else {
        reject(isPresent);
        return false;
      }
    }).catch(() => {
      return false;
    })
  }).catch((error) => {
    if (error.name === 'StaleElementReferenceError') {
      return false;
    }
  })
}

getText: async function () { return await new Promise(async function (resolve, reject) { return await protractor.element(protractor.by.css('simple-snack-bar>span,.mat-simple-snackbar > span')).isPresent().then(async (isPresent) => { if (isPresent) { return
await protractor.element(protractor.by.css('simple-snack-bar>span,.mat-simple-snackbar > span')).getText().then(async (snackBarText) => { resolve(snackBarText); }).catch(() => { return false; }) } else { reject(isPresent); return false; } }).catch(()
=> { return false; }) }).catch((error) => { if (error.name === 'StaleElementReferenceError') { return false; } }) }

Body:
I think your function can simplify to following:

getText: async function() {
    snackBarElement = element(by.css('simple-snack-bar>span,.mat-simple-snackbar > span'))

    try {
        isPresent = await snackBarElement.isPresent()
        if (isPresent) {
            return await snackBarElement.getText()
        }
    }
    catch(e) {}

    return 'Not find Snack Bar'
}


问:
If condition is meet but is ignored in PHP code
I have the following code to validate if a username is entered:

```<code> if (!isset($_POST['username']) ) {

    header("location: index.php");
    $error_message = "Please enter your Username and Password!";
    exit();
 }
</code>```

If I don't enter any value in the username field, the condition turns out false and would execute the code inside the if statement and similar situation when I enter a username!

Any idea why this is happening?

答:
If you have a form with an <input name="username"> then this input will always  have some sort of value. If you don't enter anything, the value is still the empty string. And of course that empty string is posted to the server.

On the other hand PHP isset checks

"if a variable is declared and is different than null"

And of course this is true even for the empty string (ie username exists and it's different from null). If you want to catch all situations where your username is either not existing at all or an empty string you can for instance do as follows

```<code>if (!isset($_POST['username']) || trim($_POST['username']) === '' )
</code>```

or you can use

```<code>if (strlen($_POST['username']) == 0)
</code>```

Body:
I have the following code to validate if a username is entered:

 if (!isset($_POST['username']) ) {

    header("location: index.php");
    $error_message = "Please enter your Username and Password!";
    exit();
 }

If I don't enter any value in the username field, the condition turns out false and would execute the code inside the if statement and similar situation when I enter a username!

Any idea why this is happening?

Body:
If you have a form with an <input name="username"> then this input will always  have some sort of value. If you don't enter anything, the value is still the empty string. And of course that empty string is posted to the server.

On the other hand PHP isset checks

"if a variable is declared and is different than null"

And of course this is true even for the empty string (ie username exists and it's different from null). If you want to catch all situations where your username is either not existing at all or an empty string you can for instance do as follows

if (!isset($_POST['username']) || trim($_POST['username']) === '' )

or you can use

if (strlen($_POST['username']) == 0)


问:
Check if function body references symbols that are not arguments (formals)
I was implementing a long function which takes many arguments and realised that the function body referenced a variable (symbol) which was not an argument (i.e. not a formal contained in the call signature) but the function was failing silently since the variable was present in the global environment. This is of course a common problem for beginners, but also more experienced users on occasion.

Is there a way, either programmatically or via a tool available in a popular UI (e.g. RStudio) to quickly check the modularity of functions?

Of course one can implement unit tests and in general be careful when writing scripts but tests are overkill for functions used only for parts of brief projects and a tool as described would be useful when writing longer / more complicated scripts.

I have extracted out the core idea in the following snippet where the question is effectively: How can the presence of z in the function body of add_three_numbers be quickly, easily and ideally automatically identified?

z <- 2

add_three_numbers <- function(x, y) {
  x + y + z
}

add_three_numbers(1, 2)
```

For example, I imagined a function that parses the function body and differences a set of the symbols found therein with the list of symbols making up the formal arguments of that function if doing this programmatically, or else a function in RStudio that somehow achieves the same result.

答:
R CMD check will do this for you if your code is in a package. If you're not using a package, you should be. The test uses codetools::findGlobals.  For example,

add_three_numbers <- function(x, y) {
  x + y + z
}

codetools::findGlobals(add_three_numbers, merge = FALSE)
#> $functions
#> [1] "{" "+"
#>
#> $variables
#> [1] "z"
```

Created on 2022-02-09 by the reprex package (v2.0.1.9000)

Body:
I was implementing a long function which takes many arguments and realised that the function body referenced a variable (symbol) which was not an argument (i.e. not a formal contained in the call signature) but the function was failing silently since the variable was present in the global environment. This is of course a common problem for beginners, but also more experienced users on occasion.

Is there a way, either programmatically or via a tool available in a popular UI (e.g. RStudio) to quickly check the modularity of functions?

Of course one can implement unit tests and in general be careful when writing scripts but tests are overkill for functions used only for parts of brief projects and a tool as described would be useful when writing longer / more complicated scripts.

I have extracted out the core idea in the following snippet where the question is effectively: How can the presence of z in the function body of add_three_numbers be quickly, easily and ideally automatically identified?

z <- 2

add_three_numbers <- function(x, y) {
  x + y + z
}

add_three_numbers(1, 2)

For example, I imagined a function that parses the function body and differences a set of the symbols found therein with the list of symbols making up the formal arguments of that function if doing this programmatically, or else a function in RStudio that somehow achieves the same result.

Body:
R CMD check will do this for you if your code is in a package. If you're not using a package, you should be. The test uses codetools::findGlobals.  For example,

add_three_numbers <- function(x, y) {
  x + y + z
}

codetools::findGlobals(add_three_numbers, merge = FALSE)
#> $functions
#> [1] "{" "+"
#>
#> $variables
#> [1] "z"

Created on 2022-02-09 by the reprex package (v2.0.1.9000)


问:
Can you annotate return type when value is instance of cls?
Given a class with a helper method for initialization:

```<code>class TrivialClass:
    def __init__(self, str_arg: str):
        self.string_attribute = str_arg

    @classmethod
    def from_int(cls, int_arg: int) -> ?:
        str_arg = str(int_arg)
        return cls(str_arg)
</code>```

Is it possible to annotate the return type of the from_int method?

I'v tried both cls and TrivialClass but PyCharm flags them as unresolved references which sounds reasonable at that point in time.

答:
In Python 3.11 there is a nicer way to do this using the new Self type:

from typing import Self

class TrivialClass:
    def __init__(self, str_arg: str):
        self.string_attribute = str_arg

    @classmethod
    def from_int(cls, int_arg: int) -> Self:
        str_arg = str(int_arg)
        return cls(str_arg)
```
<p>This also works correctly with sub classes as well.</p>
<pre class="lang-py prettyprint-override"><code>class TrivialSubClass(TrivialClasss):
    ...

TrivialSubclass.from_int(42)
</code>```

The IDE shows return type TrivialSubClass and not TrivialClass.

This is described in PEP 673.

Body:
Given a class with a helper method for initialization:

class TrivialClass:
    def __init__(self, str_arg: str):
        self.string_attribute = str_arg

    @classmethod
    def from_int(cls, int_arg: int) -> ?:
        str_arg = str(int_arg)
        return cls(str_arg)

Is it possible to annotate the return type of the from_int method?

I'v tried both cls and TrivialClass but PyCharm flags them as unresolved references which sounds reasonable at that point in time.

Body:
In Python 3.11 there is a nicer way to do this using the new Self type:

from typing import Self

class TrivialClass:
    def __init__(self, str_arg: str):
        self.string_attribute = str_arg

    @classmethod
    def from_int(cls, int_arg: int) -> Self:
        str_arg = str(int_arg)
        return cls(str_arg)

This also works correctly with sub classes as well.

class TrivialSubClass(TrivialClasss):
    ...

TrivialSubclass.from_int(42)

The IDE shows return type TrivialSubClass and not TrivialClass.

This is described in PEP 673.


问:
macOS Mojave - httpd process is running, but sudo apachectl stop results in httpd (no pid file) not running
Title pretty much covers it. I have an apache web server taking over localhost. When I visit http://127.0.0.1 I see a white page with "It works!" on it. Default apache stuff.

I take a look at running httpd processes, which shows multiple running

```<code>> ps -ef | grep httpd
    0 47867     1   0  8:24AM ??         0:00.18 /usr/sbin/httpd -D FOREGROUND
   70 47871 47867   0  8:24AM ??         0:00.00 /usr/sbin/httpd -D FOREGROUND
</code>```

This is interferring with another service I want to use for localhost. So I try to kill the local apache only to get the "not running" response.

```<code>> sudo apachectl stop
httpd (no pid file) not running
</code>```

So how do I stop the running apache process?

答:
Try following command in you mac terminal:

command1: sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

command2: sudo apachectl -k restart

Then try sudo apachectl start

Body:
Title pretty much covers it. I have an apache web server taking over localhost. When I visit http://127.0.0.1 I see a white page with "It works!" on it. Default apache stuff.

I take a look at running httpd processes, which shows multiple running

> ps -ef | grep httpd
    0 47867     1   0  8:24AM ??         0:00.18 /usr/sbin/httpd -D FOREGROUND
   70 47871 47867   0  8:24AM ??         0:00.00 /usr/sbin/httpd -D FOREGROUND

This is interferring with another service I want to use for localhost. So I try to kill the local apache only to get the "not running" response.

> sudo apachectl stop
httpd (no pid file) not running

So how do I stop the running apache process?


问:
How to troubleshoot exit code 139 in dotnet core 3.1 container on a Mac M1?
I recently got an M1 Mac powerbook, thinking that I could code in C# on dotnet core 3.1. My previous development environment was Windows 10 where we used linux containers.

I can build and start my containers without issue. But when I use them the main api host dies with error 139. The docker community says this is a segmentation fault.

Based on the log, the code is making a rest call to Google for Recaptcha.

```<code>_logger.LogInformation("About to call rest api on {RecaptchaVerificationEndpointV2}");
var response = await _restClient.ExecuteAsync(request);
_logger.LogInformation("Called rest api");
</code>```

The log has "About to call..." but not "Called...". This is the first call outside the container's network.

My question is - how can I troubleshoot this?

I guess that either there is some incompatibility with the platform (e.g. that dotnet 3.1 has a limitation on arm64) OR a firewall is blocking the call.

The only information I have is the code 139. Which is shown both in docker and in rider:

As background, I have dotnet 6.0.1 installed. Docker is current -
4.4.2 (73305).

The container that dies is based on this image:

```<code>FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
</code>```

I got the same result with mcr.microsoft.com/dotnet/core/aspnet:3.1.416-bullseye-arm64v8.

Everything I've read from MS says this should be supported. Based on https://learn.microsoft.com/en-us/dotnet/core/install/macos I tried installing libgdiplus.

When i try building for platform=linux/amd64, i get a different error:

```<code>#23 13.60 qemu: uncaught target signal 11 (Segmentation fault) - core dumped
------
failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c dotnet restore ./********.csproj]: exit code: 139
</code>```

(project name removed).

答: I didn't find out what caused the container to crash with exit code 139, but upgrading to dotnet 6 resolved the issue. Now I base the image on mcr.microsoft.com/dotnet/aspnet:6.0.

Body:
I recently got an M1 Mac powerbook, thinking that I could code in C# on dotnet core 3.1. My previous development environment was Windows 10 where we used linux containers.

I can build and start my containers without issue. But when I use them the main api host dies with error 139. The docker community says this is a segmentation fault.

Based on the log, the code is making a rest call to Google for Recaptcha.

_logger.LogInformation("About to call rest api on {RecaptchaVerificationEndpointV2}");
var response = await _restClient.ExecuteAsync(request);
_logger.LogInformation("Called rest api");

The log has "About to call..." but not "Called...". This is the first call outside the container's network.

My question is - how can I troubleshoot this?

I guess that either there is some incompatibility with the platform (e.g. that dotnet 3.1 has a limitation on arm64) OR a firewall is blocking the call.

The only information I have is the code 139. Which is shown both in docker and in rider:

As background, I have dotnet 6.0.1 installed. Docker is current -
4.4.2 (73305).

The container that dies is based on this image:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base

I got the same result with mcr.microsoft.com/dotnet/core/aspnet:3.1.416-bullseye-arm64v8.

Everything I've read from MS says this should be supported. Based on https://learn.microsoft.com/en-us/dotnet/core/install/macos I tried installing libgdiplus.

When i try building for platform=linux/amd64, i get a different error:

#23 13.60 qemu: uncaught target signal 11 (Segmentation fault) - core dumped
------
failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c dotnet restore ./********.csproj]: exit code: 139

(project name removed).


问:
ORA-01791: not a SELECTed expression
I need to fetch details from DB. Any thing wrong in my code?

```<code>SELECT DISTINCT FNAME, LNAME, MEMBERORG, DAYCOUNT, TIMESTAMP, COUNT(FNAME) AS total,(SELECT COUNT(*) FROM REPORT_VIEW_PAGE) AS tot
FROM REPORT_VIEW_PAGE
WHERE ID = '68' AND TYPE = 'node'
GROUP BY FNAME, LNAME, MEMBERORG, DAYCOUNT, TIMESTAMP
ORDER BY TITLE ASC
</code>```

This giving me an error:

```<code>ORA-01791: not a SELECTed expression
01791. 00000 -  "not a SELECTed expression"
*Cause:
*Action:
Error at Line: 6 Column: 10
</code>```

答:
Add the title column to your SELECT statement. When you're using DISTINCT, you must have all the columns from the ORDER BY in your SELECT statement as well.

```<code>-- correct
SELECT DISTINCT a, b, c FROM tbl.x ORDER BY a,b;

-- incorrect
SELECT DISTINCT c FROM tbl.x ORDER BY a,b;
</code>```

The a and b columns must be selected.

Title: ORA-01791: not a SELECTed expression

Body:
I need to fetch details from DB. Any thing wrong in my code?

SELECT DISTINCT FNAME, LNAME, MEMBERORG, DAYCOUNT, TIMESTAMP, COUNT(FNAME) AS total,(SELECT COUNT(*) FROM REPORT_VIEW_PAGE) AS tot
FROM REPORT_VIEW_PAGE
WHERE ID = '68' AND TYPE = 'node'
GROUP BY FNAME, LNAME, MEMBERORG, DAYCOUNT, TIMESTAMP
ORDER BY TITLE ASC

This giving me an error:

ORA-01791: not a SELECTed expression
01791. 00000 -  "not a SELECTed expression"
*Cause:
*Action:
Error at Line: 6 Column: 10

Body:
Add the title column to your SELECT statement. When you're using DISTINCT, you must have all the columns from the ORDER BY in your SELECT statement as well.

-- correct
SELECT DISTINCT a, b, c FROM tbl.x ORDER BY a,b;

-- incorrect
SELECT DISTINCT c FROM tbl.x ORDER BY a,b;

The a and b columns must be selected.


问:
Can you make an objects that deletes itself in Django?
So I made model VerificationToken

```<code>class VerificationToken(models.Model):

    user = models.ForeignKey(User, on_delete=models.CASCADE)
    code = models.CharField(max_length=4)
    date_created = models.DateTimeField(auto_now_add=True)

    @property
    def expiration(self):

        lifetime = timedelta(minutes=10)

        expire = self.date_created + lifetime

        return expire
</code>```

and this model have an expiration property. Is there a way to make this model delete itself after it expires?

答:
I believe that Django does't provide that kind of functionality. You can use Celery to create a celery task that will delete tokens that expires after certain amount of time. Also you can create task that will periodically find expired tokens and will delete them.

Celery documentation

Body:
So I made model VerificationToken

class VerificationToken(models.Model):

    user = models.ForeignKey(User, on_delete=models.CASCADE)
    code = models.CharField(max_length=4)
    date_created = models.DateTimeField(auto_now_add=True)

    @property
    def expiration(self):

        lifetime = timedelta(minutes=10)

        expire = self.date_created + lifetime

        return expire

and this model have an expiration property. Is there a way to make this model delete itself after it expires?


问:
Interop with MS Excel
I am trying to write a set of data to excel file using VB.NET. While using the function Autofit() following error is returned at run time:

System.Runtime.InteropServices.COMException: '0x800A03EC'

The exact line of code which I have written is given below.

```<code>shXL.Range("A", "I").Rows.AutoFit()
</code>```

where shXL is the worksheet definition.

All other functions on shXL are executing properly.

答:
I too don't understand why you're specifying columns for autofitting the rows but try this ...</p>
```<code>shXL.Range("A:I").Rows.AutoFit()
</code>```

Title: Interop with MS Excel

Body:
I am trying to write a set of data to excel file using VB.NET. While using the function Autofit() following error is returned at run time:

System.Runtime.InteropServices.COMException: '0x800A03EC'

The exact line of code which I have written is given below.

shXL.Range("A", "I").Rows.AutoFit()

where shXL is the worksheet definition.

All other functions on shXL are executing properly.

Body:
I too don't understand why you're specifying columns for autofitting the rows but try this ...

shXL.Range("A:I").Rows.AutoFit()


答: Unfortunately it's not possible. What you can do is to create a cronjob using celery-beat or what you like and set a cronjob task to clean up expired DB records (once per minute/hour/day)</p>

Title: Can you make an objects that deletes itself in Django?


问:
Configure a bean with a parameter sent with API Call
I have a configuration class to configure a sqlite database connection. Here I do configure it manually.

```<code>@Configuration
@EnableJpaRepositories(basePackages = "fr.company.dashboard.io.repository")
public class DBConfiguration {
    private final ApplicationProperties applicationProperties;

    @Autowired
    public DBConfiguration(ApplicationProperties applicationProperties) {
        this.applicationProperties = applicationProperties;
    }

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(applicationProperties.getProperty("hibernate.connection.driverClassName"));
        dataSource.setUrl(applicationProperties.getProperty("url"));
        dataSource.setUsername(applicationProperties.getProperty("user"));
        dataSource.setPassword(applicationProperties.getProperty("password"));
        return dataSource;
    }
}
</code>```

I would like to set the database by using an API get method which includes the database name in the URL

答:
You need firstly, to change the scope of the bean to be "request", then pass the parameters you want to the been like this:

```<code>    @Scope("request")
    @bean
      public DataSource dataSource(String dbName){
           ......
           ......
        return dataSource;
      }
</code>```

And on your controller, you should do something like that,

```<code>@Controller
public class YourController{

   @Autowired
   private BeanFactory beanFactory;

   @RequestMapping("/")
   public String exFunc(){
      String dbName = "....."
      DataSource dataSource =
         beanFactory.getBean(DataSource.class, dbName);
   }
}
</code>```

Body:
I have a configuration class to configure a sqlite database connection. Here I do configure it manually.

@Configuration
@EnableJpaRepositories(basePackages = "fr.company.dashboard.io.repository")
public class DBConfiguration {
    private final ApplicationProperties applicationProperties;

    @Autowired
    public DBConfiguration(ApplicationProperties applicationProperties) {
        this.applicationProperties = applicationProperties;
    }

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(applicationProperties.getProperty("hibernate.connection.driverClassName"));
        dataSource.setUrl(applicationProperties.getProperty("url"));
        dataSource.setUsername(applicationProperties.getProperty("user"));
        dataSource.setPassword(applicationProperties.getProperty("password"));
        return dataSource;
    }
}

I would like to set the database by using an API get method which includes the database name in the URL

Body:
You need firstly, to change the scope of the bean to be "request", then pass the parameters you want to the been like this:

    @Scope("request")
    @bean
      public DataSource dataSource(String dbName){
           ......
           ......
        return dataSource;
      }

And on your controller, you should do something like that,

@Controller
public class YourController{

   @Autowired
   private BeanFactory beanFactory;

   @RequestMapping("/")
   public String exFunc(){
      String dbName = "....."
      DataSource dataSource =
         beanFactory.getBean(DataSource.class, dbName);
   }
}


问:
How to structure seqeulize migration files for many-to-many association
What do you have to put in your migration files (and model files) to create a M:N association in sequelize? I suspect I specifically mean, what references need to be set, and what do the required keys in those objects mean, but I'm not 100% sure that's what I mean.

I'm trying to create a users<->rolls association, similar to what was asked here.

Initially, I added references to my user and roll migration files. Then I realised that the userrolls join table migration file probably needed references the other direction as well. I suspect I'm nearing every possible combination of settings, but somehow still haven't gotten this to work :)

The associations from my model files:

role.js

```<code>static associate(models) {
  models.Role.belongsToMany(models.User, {
    through: models.UserRole
  })
}
</code>```

user.js

```<code>static associate(models) {
  models.User.belongsToMany(models.Role, {
    through: models.UserRole
  })
}
</code>```

userrole.js

```<code>static associate(models) {
  UserRole.belongsTo(models.User, { foreignKey: 'id' });
  UserRole.belongsTo(models.Role, { foreignKey: 'id' });
  // UserRole.belongsTo(models.User, { foreignKey: 'userId' }); // <- *
  // UserRole.belongsTo(models.Role, { foreignKey: 'roleId' });
}
</code>```

* Here, and below, commented out lines are various things I've tried, though they probably don't account for all attempts.

My migration files:

create-user

```<code>'use strict';
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
        /*
        references: {
          model: 'UserRoles',
          key: 'id',
          as: 'userId'
        }
        */
        references: {
          model: 'UserRoles',
          key: 'userId'
        }
      },
      username: {
        type: Sequelize.STRING
      },
      ...
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('Users');
  }
};
</code>```

The create-role.js file is congruent to the above.

create-user-role.js

```<code>'use strict';
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('UserRoles', {
      /*
      userId: {
        // allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        references: {
          model: 'Users',
          key: 'id'
        }
      },
      */
      userId: {
        // allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        references: {
          model: 'Users',
          key: 'id',
          as: 'userId'
        }
      },
      /*
      roleId: {
        // allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        references: {
          model: 'Roles',
          key: 'id'
        }
      },
      */
      roleId: {
        // allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        references: {
          model: 'Roles',
          key: 'id',
          as: 'roleId'
        }
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('UserRoles');
  }
};
</code>```

I had/have this working, using the standard, documented method where you use sequelize.define and rely on sync to create the tables, but it seems I can't find the right incantation to get it to work with the seqeulize-cli migration method that autoloads all the models. To my (rather untrained) eye, the tables that are generated look right:

```<code>sqlite> .schema
CREATE TABLE `Users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT REFERENCES `UserRoles` (`userId`), `username` VARCHAR(255), `email` VARCHAR(255), `password` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
CREATE TABLE `Roles` (`id` INTEGER PRIMARY KEY AUTOINCREMENT REFERENCES `UserRoles` (`roleId`), `name` VARCHAR(255), `createdAt` DATETIME, `updatedAt` DATETIME NOT NULL);
CREATE TABLE `UserRoles` (`userId` INTEGER NOT NULL REFERENCES `Users` (`id`), `roleId` INTEGER NOT NULL REFERENCES `Roles` (`id`), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`userId`, `roleId`));
</code>```

At least that last entry looks analogous to the PostgreSQL example they give in the docs, but they don't seem to provide examples of the main tables.

But, no matter what I do, if I go to insert a user, it complains about a foreign key mismatch:

```<code>Executing (default): INSERT INTO `Users` (`id`,`username`,`email`,`password`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4,$5);
SQLITE_ERROR: foreign key mismatch - "Users" referencing "UserRoles"
</code>```

I'd take a simple "change this in your code" answer, but, as I mentioned in the beginning, I suspect this is a fundamental misunderstand of what needs to be set and why, so ... some explanation of what I'm supposed to be doing would be greatly appreciated.

答:
If you need M:N relationship between User and Role using UserRole then references option should be used only in UserRole because only it has foreign keys to both User and Role.

Also you should indicate the same foreign keys in belongsToMany as you indicated in belongsTo:

role.js

models.Role.belongsToMany(models.User, {
    through: models.UserRole,
    foreignKey: 'roleId',
    otherKey: 'userId'
  })
```
<p><strong>user.js</strong></p>
<pre class="lang-js prettyprint-override"><code>models.User.belongsToMany(models.Role, {
    through: models.UserRole,
    foreignKey: 'userId',
    otherKey: 'roleId'
  })
</code>```

userrole.js

static associate(models) {
  UserRole.belongsTo(models.User, { foreignKey: 'userId' });
  UserRole.belongsTo(models.Role, { foreignKey: 'roleId' });
}
```
<p><strong>createuser.js</strong></p>
<pre class="lang-js prettyprint-override"><code>'use strict';
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      username: {
        type: Sequelize.STRING
      },
      ...
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('Users');
  }
};
</code>```

A primary key should be a separate single column as it is in User model.

create-user-role.js

'use strict';
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('UserRoles', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      userId: {
        type: Sequelize.INTEGER,
        references: {
          model: 'Users',
          key: 'id'
        }
      },
      roleId: {
        type: Sequelize.INTEGER,
        references: {
          model: 'Roles',
          key: 'id'
        }
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('UserRoles');
  }
};
```

Body:
What do you have to put in your migration files (and model files) to create a M:N association in sequelize? I suspect I specifically mean, what references need to be set, and what do the required keys in those objects mean, but I'm not 100% sure that's what I mean.

I'm trying to create a users<->rolls association, similar to what was asked here.

Initially, I added references to my user and roll migration files. Then I realised that the userrolls join table migration file probably needed references the other direction as well. I suspect I'm nearing every possible combination of settings, but somehow still haven't gotten this to work :)

The associations from my model files:

role.js

static associate(models) {
  models.Role.belongsToMany(models.User, {
    through: models.UserRole
  })
}

user.js

static associate(models) {
  models.User.belongsToMany(models.Role, {
    through: models.UserRole
  })
}

userrole.js

static associate(models) {
  UserRole.belongsTo(models.User, { foreignKey: 'id' });
  UserRole.belongsTo(models.Role, { foreignKey: 'id' });
  // UserRole.belongsTo(models.User, { foreignKey: 'userId' }); // <- *
  // UserRole.belongsTo(models.Role, { foreignKey: 'roleId' });
}

* Here, and below, commented out lines are various things I've tried, though they probably don't account for all attempts.

My migration files:

create-user

'use strict';
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
        /*
        references: {
          model: 'UserRoles',
          key: 'id',
          as: 'userId'
        }
        */
        references: {
          model: 'UserRoles',
          key: 'userId'
        }
      },
      username: {
        type: Sequelize.STRING
      },
      ...
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('Users');
  }
};

The create-role.js file is congruent to the above.

create-user-role.js

'use strict';
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('UserRoles', {
      /*
      userId: {
        // allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        references: {
          model: 'Users',
          key: 'id'
        }
      },
      */
      userId: {
        // allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        references: {
          model: 'Users',
          key: 'id',
          as: 'userId'
        }
      },
      /*
      roleId: {
        // allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        references: {
          model: 'Roles',
          key: 'id'
        }
      },
      */
      roleId: {
        // allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        references: {
          model: 'Roles',
          key: 'id',
          as: 'roleId'
        }
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('UserRoles');
  }
};

I had/have this working, using the standard, documented method where you use sequelize.define and rely on sync to create the tables, but it seems I can't find the right incantation to get it to work with the seqeulize-cli migration method that autoloads all the models. To my (rather untrained) eye, the tables that are generated look right:

sqlite> .schema
CREATE TABLE `Users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT REFERENCES `UserRoles` (`userId`), `username` VARCHAR(255), `email` VARCHAR(255), `password` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
CREATE TABLE `Roles` (`id` INTEGER PRIMARY KEY AUTOINCREMENT REFERENCES `UserRoles` (`roleId`), `name` VARCHAR(255), `createdAt` DATETIME, `updatedAt` DATETIME NOT NULL);
CREATE TABLE `UserRoles` (`userId` INTEGER NOT NULL REFERENCES `Users` (`id`), `roleId` INTEGER NOT NULL REFERENCES `Roles` (`id`), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`userId`, `roleId`));

At least that last entry looks analogous to the PostgreSQL example they give in the docs, but they don't seem to provide examples of the main tables.

But, no matter what I do, if I go to insert a user, it complains about a foreign key mismatch:

Executing (default): INSERT INTO `Users` (`id`,`username`,`email`,`password`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4,$5);
SQLITE_ERROR: foreign key mismatch - "Users" referencing "UserRoles"

I'd take a simple "change this in your code" answer, but, as I mentioned in the beginning, I suspect this is a fundamental misunderstand of what needs to be set and why, so ... some explanation of what I'm supposed to be doing would be greatly appreciated.

Body:
If you need M:N relationship between User and Role using UserRole then references option should be used only in UserRole because only it has foreign keys to both User and Role.

Also you should indicate the same foreign keys in belongsToMany as you indicated in belongsTo:

role.js

models.Role.belongsToMany(models.User, {
    through: models.UserRole,
    foreignKey: 'roleId',
    otherKey: 'userId'
  })

user.js

models.User.belongsToMany(models.Role, {
    through: models.UserRole,
    foreignKey: 'userId',
    otherKey: 'roleId'
  })

userrole.js

static associate(models) {
  UserRole.belongsTo(models.User, { foreignKey: 'userId' });
  UserRole.belongsTo(models.Role, { foreignKey: 'roleId' });
}

createuser.js

'use strict';
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      username: {
        type: Sequelize.STRING
      },
      ...
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('Users');
  }
};

A primary key should be a separate single column as it is in User model.

create-user-role.js

'use strict';
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('UserRoles', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      userId: {
        type: Sequelize.INTEGER,
        references: {
          model: 'Users',
          key: 'id'
        }
      },
      roleId: {
        type: Sequelize.INTEGER,
        references: {
          model: 'Roles',
          key: 'id'
        }
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('UserRoles');
  }
};


问:
how to rearrange bootstrap columns in order on mobile view
how to rearrange bootstrap columns? i have a 2 column layout on desktop view

this is what i have in desktop view

im trying to achieve this layout on mobile view, is this possible?

答:
i have found the answer

Template

```<code><div class="col-lg-4 col-xs-12">1</div>
<div class="col-lg-8 col-xs-12 top-features">2</div>
<div class="col-lg-4 col-xs-12 item-brand">3</div>
<div class="col-lg-8 col-xs-12"><4/div>
<div class="col-lg-4 col-xs-12">5</div>
<div class="col-lg-8 col-xs-12">6</div>
</code>```

Styles

```<code>@media screen and (min-width: 769px) {

  .top-features {
    order: 2;
  }

  .item-brand {
    order: 1;
  }
}
</code>```

Body:
i have found the answer

Template

<div class="col-lg-4 col-xs-12">1</div>
<div class="col-lg-8 col-xs-12 top-features">2</div>
<div class="col-lg-4 col-xs-12 item-brand">3</div>
<div class="col-lg-8 col-xs-12"><4/div>
<div class="col-lg-4 col-xs-12">5</div>
<div class="col-lg-8 col-xs-12">6</div>

Styles

@media screen and (min-width: 769px) {

  .top-features {
    order: 2;
  }

  .item-brand {
    order: 1;
  }
}


问:
can I forward same animationController again after AnimationStatus.completed
I want to make same animationController forward when the animation status is completed, and setState to update value which is inside animation tween, so the second animationController forward will achieve different animation, is that possible? thank you!</p>
```<code>GestureDetector(

        onTap: () {
          if (firstController.status == AnimationStatus.dismissed) {
            firstController.forward();
          } else if (firstController.status == AnimationStatus.completed) {
            setState(() {

              double firstOpacity = 1.0;  //new value
              double secondOpacity = 0.7;//new value
              double thirdOpacity = 0.4;//new value
            });
            firstController.forward();  // now it does not work
          }
        },
        child: Center(
          child: Container(
            child: AnimatedBuilder(
              animation: firstController,
              builder: _buildAnimation,
            ),
          ),
        ),
      ),
</code>```

答:
You can do that and more! This api article talks about staggered animations where you can set an interval percentage of the entire animation to your specific animation. The Interval part is where you set those values.

width = Tween<double>(
  begin: 50.0,
  end: 150.0,
).animate(
  CurvedAnimation(
    parent: controller,
    curve: Interval(
      0.125, 0.250,
      curve: Curves.ease,
    ),
  ),
);
```
<p>About the <code>AnimationController</code>, you have some methods there to help, look this api article about them and search for <code>repeat</code>, <code>reset</code> and <code>reverse</code> methods and look the parameters that you have. You can do a lot with Flutter.</p>
<p><em><strong>Edit:</strong></em></p>
<p>If you want to make a 1/3 animation that pauses before continuing, you can do something like:</p>
<pre class="lang-dart prettyprint-override"><code>    final animationController = AnimationController(vsync: this);
    final animation = Tween<double>(begin: 0, end: 0).animate(
      CurvedAnimation(
        parent: animationController,
        curve: const Interval(
          0,
          1 / 3,
          curve: Curves.easeIn,
        ),
      ),
    );
    animation.addStatusListener((status) {
      animationController.stop(); // For example, but read the documentation about the stop method
    });
</code>```

Body:
I want to make same animationController forward when the animation status is completed, and setState to update value which is inside animation tween, so the second animationController forward will achieve different animation, is that possible? thank you!

GestureDetector(

        onTap: () {
          if (firstController.status == AnimationStatus.dismissed) {
            firstController.forward();
          } else if (firstController.status == AnimationStatus.completed) {
            setState(() {

              double firstOpacity = 1.0;  //new value
              double secondOpacity = 0.7;//new value
              double thirdOpacity = 0.4;//new value
            });
            firstController.forward();  // now it does not work
          }
        },
        child: Center(
          child: Container(
            child: AnimatedBuilder(
              animation: firstController,
              builder: _buildAnimation,
            ),
          ),
        ),
      ),

Body:
You can do that and more! This api article talks about staggered animations where you can set an interval percentage of the entire animation to your specific animation. The Interval part is where you set those values.

width = Tween<double>(
  begin: 50.0,
  end: 150.0,
).animate(
  CurvedAnimation(
    parent: controller,
    curve: Interval(
      0.125, 0.250,
      curve: Curves.ease,
    ),
  ),
);

About the AnimationController, you have some methods there to help, look this api article about them and search for repeat, reset and reverse methods and look the parameters that you have. You can do a lot with Flutter.

Edit:

If you want to make a 1/3 animation that pauses before continuing, you can do something like:

    final animationController = AnimationController(vsync: this);
    final animation = Tween<double>(begin: 0, end: 0).animate(
      CurvedAnimation(
        parent: animationController,
        curve: const Interval(
          0,
          1 / 3,
          curve: Curves.easeIn,
        ),
      ),
    );
    animation.addStatusListener((status) {
      animationController.stop(); // For example, but read the documentation about the stop method
    });


问:
Integration of a vector function in GSL
I am trying to integrate a vector valued (49 components) function f[] which as an example may look like:

```<code>f[0] = 1
f[1] = cos(x)
f[2] = cos(2x)
f[3] = cos(3x)
</code>```

... and so on.

I was wondering if there was a way to integrate such a vector function in GSL using a single command. I can currently do this only by having n=49 different cquad integration handles/procedures which seems inefficient, as I wish to use the same integration "mesh  " for all the function components.

Thank you for your attention.

答:
As far as I know, at the moment such things via GSL cannot be done.
But your task can be solved with the help of openmp (with #pragma omp parallel for), but there may be a very large overhead when using gsl dll by several threads at the same time.
It is possible (this is not accurate) that for that you will have to rebuild the GSL library itself with the openmp-compiler-flags. But GSL is quite thread-safe on its own.

Title: Integration of a vector function in GSL

Body:
I am trying to integrate a vector valued (49 components) function f[] which as an example may look like:

f[0] = 1
f[1] = cos(x)
f[2] = cos(2x)
f[3] = cos(3x)

... and so on.

I was wondering if there was a way to integrate such a vector function in GSL using a single command. I can currently do this only by having n=49 different cquad integration handles/procedures which seems inefficient, as I wish to use the same integration "mesh  " for all the function components.

Thank you for your attention.


问:
Randomize selection from snowflake SQL
I am trying to select 1,000 random rows from a database of 97 million rows. I am using the following code:

```<code>SELECT *
FROM "DB"."SCHEMA"."TABLE"
ORDER BY
   RAND()                                      LIMIT 1000
</code>```

I tried this code and got an error stating "SQL compilation error: Unknown function RAND." Is there a better way to do this in Snowflake? I am worried that this code only works in MySQL.

答:
In Snowflake the function is RANDOM(), not RAND().

So your original query should be:

```<code>SELECT *
FROM "DB"."SCHEMA"."TABLE"
ORDER BY
   RANDOM()
LIMIT 1000
</code>```

But as Lukasz mentioned, SAMPLE() function is the native way to do it in Snowflake.

Title: Randomize selection from snowflake SQL

Body:
I am trying to select 1,000 random rows from a database of 97 million rows. I am using the following code:

SELECT *
FROM "DB"."SCHEMA"."TABLE"
ORDER BY
   RAND()                                      LIMIT 1000

I tried this code and got an error stating "SQL compilation error: Unknown function RAND." Is there a better way to do this in Snowflake? I am worried that this code only works in MySQL.

Body:
In Snowflake the function is RANDOM(), not RAND().

So your original query should be:

SELECT *
FROM "DB"."SCHEMA"."TABLE"
ORDER BY
   RANDOM()
LIMIT 1000

But as Lukasz mentioned, SAMPLE() function is the native way to do it in Snowflake.


问:
default values on a related model
From my model I have a many2one relationship to res.partner

Such relationship "feeds" a dropdown field (menu)

Such dropdown field contains a list of partners and the last item is "Start writing", to create a new partner (see this picture; "Inizia a scrivere is the Italian for "Start writing")

I'd like the new partner being created to have some fields automatically assigned with some default values

I've been suggested to use an action with a context but in this case I'm not starting the creation of a new partner from a menu or button and I'm not sure how to call my action from the last item of the dropdown menu

答:
you can apply a context attribute on a related field in a view

Like this

```<code><field name="some_related_field_id"
             context="{'default_field_to_be_set_on_the_related_model': 'some_value'}"/>
</code>```

Please notice that the name of the field to be set is not

```<code>default_field_to_be_set_on_the_related_model
</code>```

it is

```<code>field_to_be_set_on_the_related_model
</code>```

The initial default_ is a convention to set default values

That's required, if you don't prefix the name of your field with default_ the thing won't work

Title: default values on a related model

Body:
you can apply a context attribute on a related field in a view

Like this

<field name="some_related_field_id"
             context="{'default_field_to_be_set_on_the_related_model': 'some_value'}"/>

Please notice that the name of the field to be set is not

default_field_to_be_set_on_the_related_model

it is

field_to_be_set_on_the_related_model

The initial default_ is a convention to set default values

That's required, if you don't prefix the name of your field with default_ the thing won't work


问:
Access PHP Logfile
is there  any "sexy" solution to access the php error_log from remote? Like implemented in Zend Server CE?

Or how to you handle it, if there is no SSH Access for the server users?

To clarify: it is our own server running on winXP :-)

答: It is - Just put error log in your www directory (public viewable) and you can then browse to it/download it (others can download/browse it,too!)</p>

Title: Access PHP Logfile


问:
How to get all analytics from SendGrid for each email?
I would like to get all analytical data from SendGrid for each email using "unique args".

Currently to be able to do this, i would need to:

call SendGrid API to get all emails with "unique arguments"

from response get email ID

call SendGrid again based on email ID to get all "events" (clicks, opens, subscribes, unsubscribes, bounces, blocks,.. etc.)

With this approach i would make twice as much requests as needed to.

Does anyone know a way to skip step 2 and 3, and get all "events" data from just one call?

答:
You can use Event Webhooks from SendGrid. Events are generated when the email is processed by SendGrid and email service providers. There are 2 types of events - delivery and engagement events.

Delivery events indicate the status of email delivery to the recipient. Engagement events indicate how the recipient is interacting with the email.

```<code> {
    "email": "example@test.com",
    "timestamp": 1513299569,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "processed",
    "category": "cat facts",
    "sg_event_id": "sg_event_id",
    "sg_message_id": "sg_message_id"
  },
  {
    "email": "example@test.com",
    "timestamp": 1513299569,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "deferred",
    "ip": "168.1.1.1",
    "category": "cat facts",
    "sg_event_id": "sg_event_id",
    "sg_message_id": "sg_message_id",
    "response": "400 try again later",
    "attempt": "5"
  },
  {
    "email": "example@test.com",
    "timestamp": 1513299569,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "delivered",
    "ip": "168.1.1.1",
    "category": "cat facts",
    "sg_event_id": "sg_event_id",
    "sg_message_id": "sg_message_id",
    "response": "250 OK"
  },
  {
    "email": "example@test.com",
    "timestamp": 1513299569,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "open",
    "category": "cat facts",
    "sg_event_id": "sg_event_id",
    "sg_message_id": "sg_message_id",
    "useragent": "Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
    "ip": "255.255.255.255"
  },
</code>```

Delivery events include processed, dropped, delivered, deferred, and bounce.

Engagement events include open, click, spam report, unsubscribe, group unsubscribe, and group resubscribe.

I recommend you to check the link for more details.

Body:
You can use Event Webhooks from SendGrid. Events are generated when the email is processed by SendGrid and email service providers. There are 2 types of events - delivery and engagement events.

Delivery events indicate the status of email delivery to the recipient. Engagement events indicate how the recipient is interacting with the email.

 {
    "email": "example@test.com",
    "timestamp": 1513299569,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "processed",
    "category": "cat facts",
    "sg_event_id": "sg_event_id",
    "sg_message_id": "sg_message_id"
  },
  {
    "email": "example@test.com",
    "timestamp": 1513299569,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "deferred",
    "ip": "168.1.1.1",
    "category": "cat facts",
    "sg_event_id": "sg_event_id",
    "sg_message_id": "sg_message_id",
    "response": "400 try again later",
    "attempt": "5"
  },
  {
    "email": "example@test.com",
    "timestamp": 1513299569,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "delivered",
    "ip": "168.1.1.1",
    "category": "cat facts",
    "sg_event_id": "sg_event_id",
    "sg_message_id": "sg_message_id",
    "response": "250 OK"
  },
  {
    "email": "example@test.com",
    "timestamp": 1513299569,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "open",
    "category": "cat facts",
    "sg_event_id": "sg_event_id",
    "sg_message_id": "sg_message_id",
    "useragent": "Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
    "ip": "255.255.255.255"
  },

Delivery events include processed, dropped, delivered, deferred, and bounce.

Engagement events include open, click, spam report, unsubscribe, group unsubscribe, and group resubscribe.

I recommend you to check the link for more details.