问:
Poisson GLM for non-integer counts - R
I was hoping to get some advice on a GLM with Poisson family.

I have a data set with the number of bites taken by each individual over a period of time. As the individuals observed were feeding for different periods of time, when I calculated the bite rate for each individual as bites/minute, I have ended up with non-integer numbers. Now, based on what I have read so far, I should still be able to do a GLM with a Poisson family. However, I am encountering errors and I think this may be because R is not liking the fact I'm using non-integers. Does anyone have any advice?

```<code>Example <- structure(list(Species = c("Fish1", "Fish2", "Fish3", "Fish4",
"Fish5", "Fish6", "Fish7", "Fish1", "Fish2", "Fish3", "Fish4",
"Fish5", "Fish6", "Fish7", "Fish1", "Fish2", "Fish3", "Fish4",
"Fish5", "Fish6", "Fish7"), Site = c(1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3), Bite_Rate = c(3.5, 7.5,
0, 0, 2.45, 5.5, 6.5, 6.5, 7.5, 8.03, 32.1, 15.6, 18.2, 19.1,
20.5, 20.5, 3.5, 5.7, 6.7, 23.2, 0)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -21L), spec = structure(list(
   cols = list(Species = structure(list(), class = c("collector_character",
   "collector")), Site = structure(list(), class = c("collector_double",
   "collector")), Bite_Rate = structure(list(), class = c("collector_double",
   "collector"))), default = structure(list(), class = c("collector_guess",
   "collector")), skip = 1), class = "col_spec"))

str(Example) # check structure
Example$Species<-as.factor(Example$Species) # set species as a factor
str(Example) # check structure
glm<-glm(Species~Bite_Rate, data=Example, family = poisson) # create the GLM

</code>```

The error message when I run the GLM is:

```<code>Error in if (any(y < 0)) stop("negative values not allowed for the 'Poisson' family") :
  missing value where TRUE/FALSE needed
In addition: Warning message:
In Ops.factor(y, 0) : ‘<’ not meaningful for factors
</code>```

I don't actually have any negative values which is what throws me off a little bit.
Any advice would be much appreciated!

EDIT:
Based on the comments, I have updated my example data so that it has counts of bites and the time of observation in seconds

```<code>
Example <- structure(list(Species = structure(c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("Fish1",
"Fish2", "Fish3", "Fish4", "Fish5", "Fish6", "Fish7"), class = "factor"),
    Site = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
    3, 3, 3, 3, 3), Bites = c(0, 10, 18, 17, 6, 0, 1, 0, 19,
    12, 7, 3, 5, 1, 5, 0, 10, 18, 17, 7, 25), Observed_Seconds = c(50,
    33, 47, 20, 17, 10, 14, 21, 48, 10, 50, 33, 47, 20, 17, 10,
    14, 21, 48, 10, 90)), row.names = c(NA, -21L), spec = structure(list(
    cols = list(Species = structure(list(), class = c("collector_character",
    "collector")), Site = structure(list(), class = c("collector_double",
    "collector")), Bites = structure(list(), class = c("collector_double",
    "collector")), Observed_Seconds = structure(list(), class = c("collector_double",
    "collector"))), default = structure(list(), class = c("collector_guess",
    "collector")), skip = 1), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))

</code>```

Thank you!

答:
You need to get the seconds (denominator) with which you normalized, and the actual bites (counts).

Next include the minutes as an offset, and note, your response variable is on the left hand side of ~ :

```<code>fit = glm(Bites ~ Species,offset=log(Observed_Seconds),
family=poisson,data=Example)
</code>```

You can look at the summary:

```<code>summary(fit)

   Coefficients:
             Estimate Std. Error z value Pr(>|z|)
SpeciesFish1  -2.8679     0.4472  -6.413 1.42e-10 ***
SpeciesFish2  -1.1436     0.1857  -6.158 7.35e-10 ***
SpeciesFish3  -0.5738     0.1581  -3.629 0.000284 ***
SpeciesFish4  -0.7732     0.1543  -5.011 5.42e-07 ***
SpeciesFish5  -1.3269     0.1961  -6.766 1.33e-11 ***
SpeciesFish6  -1.7198     0.2887  -5.958 2.56e-09 ***
SpeciesFish7  -1.5244     0.1925  -7.921 2.35e-15 ***
</code>```

Seems very significant, but ne good thing to check is whether the data is overdispersed, and also include other factors (e.g Site):

```<code>fit_quasi = glm(Bites ~ Species + factor(Site),offset=log(Observed_Seconds),
          family=quasipoisson,data=Example)
summary(fit_quasi)

Coefficients:
              Estimate Std. Error t value Pr(>|t|)
(Intercept)    -2.9754     1.0713  -2.777   0.0167 *
SpeciesFish2    1.8487     1.1434   1.617   0.1319
SpeciesFish3    2.2731     1.1152   2.038   0.0642 .
SpeciesFish4    2.1246     1.1205   1.896   0.0823 .
SpeciesFish5    1.3533     1.1604   1.166   0.2662
SpeciesFish6    1.2754     1.2658   1.008   0.3336
SpeciesFish7    0.8922     1.1719   0.761   0.4612
factor(Site)2  -0.2325     0.5132  -0.453   0.6587
factor(Site)3   0.6118     0.4677   1.308   0.2154
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for quasipoisson family taken to be 5.521045)
</code>```

If it follows a poisson, the dispersion will be around 1, but in this case, it is overdispersed.

Title: Poisson GLM for non-integer counts - R

Body:
I was hoping to get some advice on a GLM with Poisson family.

I have a data set with the number of bites taken by each individual over a period of time. As the individuals observed were feeding for different periods of time, when I calculated the bite rate for each individual as bites/minute, I have ended up with non-integer numbers. Now, based on what I have read so far, I should still be able to do a GLM with a Poisson family. However, I am encountering errors and I think this may be because R is not liking the fact I'm using non-integers. Does anyone have any advice?

Example <- structure(list(Species = c("Fish1", "Fish2", "Fish3", "Fish4",
"Fish5", "Fish6", "Fish7", "Fish1", "Fish2", "Fish3", "Fish4",
"Fish5", "Fish6", "Fish7", "Fish1", "Fish2", "Fish3", "Fish4",
"Fish5", "Fish6", "Fish7"), Site = c(1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3), Bite_Rate = c(3.5, 7.5,
0, 0, 2.45, 5.5, 6.5, 6.5, 7.5, 8.03, 32.1, 15.6, 18.2, 19.1,
20.5, 20.5, 3.5, 5.7, 6.7, 23.2, 0)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -21L), spec = structure(list(
   cols = list(Species = structure(list(), class = c("collector_character",
   "collector")), Site = structure(list(), class = c("collector_double",
   "collector")), Bite_Rate = structure(list(), class = c("collector_double",
   "collector"))), default = structure(list(), class = c("collector_guess",
   "collector")), skip = 1), class = "col_spec"))

str(Example) # check structure
Example$Species<-as.factor(Example$Species) # set species as a factor
str(Example) # check structure
glm<-glm(Species~Bite_Rate, data=Example, family = poisson) # create the GLM

The error message when I run the GLM is:

Error in if (any(y < 0)) stop("negative values not allowed for the 'Poisson' family") :
  missing value where TRUE/FALSE needed
In addition: Warning message:
In Ops.factor(y, 0) : ‘<’ not meaningful for factors

I don't actually have any negative values which is what throws me off a little bit.
Any advice would be much appreciated!

EDIT:
Based on the comments, I have updated my example data so that it has counts of bites and the time of observation in seconds

Example <- structure(list(Species = structure(c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("Fish1",
"Fish2", "Fish3", "Fish4", "Fish5", "Fish6", "Fish7"), class = "factor"),
    Site = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
    3, 3, 3, 3, 3), Bites = c(0, 10, 18, 17, 6, 0, 1, 0, 19,
    12, 7, 3, 5, 1, 5, 0, 10, 18, 17, 7, 25), Observed_Seconds = c(50,
    33, 47, 20, 17, 10, 14, 21, 48, 10, 50, 33, 47, 20, 17, 10,
    14, 21, 48, 10, 90)), row.names = c(NA, -21L), spec = structure(list(
    cols = list(Species = structure(list(), class = c("collector_character",
    "collector")), Site = structure(list(), class = c("collector_double",
    "collector")), Bites = structure(list(), class = c("collector_double",
    "collector")), Observed_Seconds = structure(list(), class = c("collector_double",
    "collector"))), default = structure(list(), class = c("collector_guess",
    "collector")), skip = 1), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))

Thank you!

Body:
You need to get the seconds (denominator) with which you normalized, and the actual bites (counts).

Next include the minutes as an offset, and note, your response variable is on the left hand side of ~ :

fit = glm(Bites ~ Species,offset=log(Observed_Seconds),
family=poisson,data=Example)

You can look at the summary:

summary(fit)

   Coefficients:
             Estimate Std. Error z value Pr(>|z|)
SpeciesFish1  -2.8679     0.4472  -6.413 1.42e-10 ***
SpeciesFish2  -1.1436     0.1857  -6.158 7.35e-10 ***
SpeciesFish3  -0.5738     0.1581  -3.629 0.000284 ***
SpeciesFish4  -0.7732     0.1543  -5.011 5.42e-07 ***
SpeciesFish5  -1.3269     0.1961  -6.766 1.33e-11 ***
SpeciesFish6  -1.7198     0.2887  -5.958 2.56e-09 ***
SpeciesFish7  -1.5244     0.1925  -7.921 2.35e-15 ***

Seems very significant, but ne good thing to check is whether the data is overdispersed, and also include other factors (e.g Site):

fit_quasi = glm(Bites ~ Species + factor(Site),offset=log(Observed_Seconds),
          family=quasipoisson,data=Example)
summary(fit_quasi)

Coefficients:
              Estimate Std. Error t value Pr(>|t|)
(Intercept)    -2.9754     1.0713  -2.777   0.0167 *
SpeciesFish2    1.8487     1.1434   1.617   0.1319
SpeciesFish3    2.2731     1.1152   2.038   0.0642 .
SpeciesFish4    2.1246     1.1205   1.896   0.0823 .
SpeciesFish5    1.3533     1.1604   1.166   0.2662
SpeciesFish6    1.2754     1.2658   1.008   0.3336
SpeciesFish7    0.8922     1.1719   0.761   0.4612
factor(Site)2  -0.2325     0.5132  -0.453   0.6587
factor(Site)3   0.6118     0.4677   1.308   0.2154
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for quasipoisson family taken to be 5.521045)

If it follows a poisson, the dispersion will be around 1, but in this case, it is overdispersed.


来源: stackexchange

问:
Limit number of items / length of json for logging
I am working on an API that returns JSON. I am logging my responses, and sometimes the JSON is just absurdly long and basically clogs my log files. Is there a neat way to reduce the length of a JSON, purely for visually logging the data? (not in effect in production)

The basic approach is to reduce arrays over a length of 5 to [first 2, "...", last 2], and dictionaries with more than 4 items to {first 4, "..." : "..."}

The code below is ugly. I am aware that it should be a recursive solution that reduces the items in the same way for a JSON of arbitrary depth - it currently only does so for depth 2.

```<code>def log_reducer(response_log):
original_response_log = response_log
try:
    if type(response_log) == dict:
        if len(response_log) >= 4:  # {123456}
            response_log = dict(list(response_log.items())[:4])
            response_log.update({"...": "..."})  # {1234...}
        for key, value in response_log.items():
            if type(value) == list:
                if len(value) >= 5:  # {key:[123456]}
                    new_item = value[:2] + ['...'] + value[-2:]  # {[12...56]}
                    response_log.update({key: new_item})
            if type(value) == dict:
                if len(value) >= 4:  # {key:{123456}}
                    reduced_dict = dict(list(value.items())[:4])
                    reduced_dict.update({"...": "..."})
                    response_log.update({key: reduced_dict})  # {{1234...}}

    elif type(response_log) == list:
        if len(response_log) >= 5:  # [123456]
            response_log = response_log[:2] + ['...'] + response_log[-2:]  # [12...56]
        for inner_item in response_log:
            if type(inner_item) == list:
                if len(inner_item) >= 5:  # [[123456]]
                    reduced_list = inner_item[:2] + ['...'] + inner_item[-2:]  # [[12...56]]
                    response_log.remove(inner_item)
                    response_log.append(reduced_list)
            if type(inner_item) == dict:
                if len(inner_item) >= 4:  # [{123456}]
                    reduced_dict = dict(list(inner_item.items())[:4])
                    reduced_dict.update({"...": "..."})  # [{1234...}]
                    response_log.remove(inner_item)
                    response_log.append(reduced_dict)
except Exception as e:
    return original_response_log
return response_log
</code>```

The returned response_log is then logged with logger.info(str(response_log))

As you can see, the fact that there can be either arrays or dictionaries at every level makes this task a little more complex, and I am struggling to find a library or code snipped of any kind which would simplify this. If anyone wants to give it a shot, I would appreciate it a lot.

you can use a test JSON like this to see it in effect:

```<code>test_json = {"works": [1, 2, 3, 4, 5, 6],
             "not_affected": [{"1": "1", "2": "2", "3": "3", "4": "4", "5": "5"}],
             "1": "1", "2": "2", "3": "3",
             "removed": "removed"
             }
print("original", test_json)
reduced_log = log_reducer(test_json)
print("reduced", reduced_log)

print("original", test_json)
reduced_log = log_reducer([test_json])  # <- increases nesting depth
print("reduced", reduced_log)
</code>```

答:
This answer uses @calceamenta's idea, but implements the actual cutting-down logic:

```<code>def recursive_reduce(obj):
    if isinstance(obj, (float, str, int, bool, type(None))):
        return obj

    if isinstance(obj, dict):
        keys = list(sorted(obj))
        obj['...'] = '...'

        if len(keys) > 5:
            new_keys = keys[:2] + ["..."] + keys[-2:]
        else:
            new_keys = keys

        new_dict = {x:obj[x] for x in new_keys}
        for k, v in new_dict.items():
            new_dict[k] = recursive_reduce(v)

        return new_dict

    if isinstance(obj, list):
        if len(obj) > 5:
            new_list = obj[:2] + ["..."] + obj[-2:]
        else:
            new_list = obj

        for i, v in enumerate(new_list):
            new_list[i] = recursive_reduce(v)

        return new_list

    return str(obj)

test_json = {"works": [1, 2, 3, 4, 5, 6],
             "not_affected": [{"1": "1", "2": "2", "3": "3", "4": "4", "5": "5"}],
             "1": "1", "2": "2", "3": "3",
             "removed": "removed"
             }

print("original", test_json)
reduced_log = recursive_reduce(test_json)
print("reduced", reduced_log)
</code>```

Output:

```<code>original {'works': [1, 2, 3, 4, 5, 6], 'not_affected': [{'1': '1', '2': '2', '3': '3', '4': '4', '5': '5'}], '1': '1', '2': '2', '3': '3', 'removed': 'removed'}
reduced {'1': '1', '2': '2', '...': '...', 'removed': 'removed', 'works': [1, 2, '...', 5, 6]}
</code>```

Hope this helps :)

ContentLicense: CC BY-SA 4.0

Body:
I am working on an API that returns JSON. I am logging my responses, and sometimes the JSON is just absurdly long and basically clogs my log files. Is there a neat way to reduce the length of a JSON, purely for visually logging the data? (not in effect in production)

The basic approach is to reduce arrays over a length of 5 to [first 2, "...", last 2], and dictionaries with more than 4 items to {first 4, "..." : "..."}

The code below is ugly. I am aware that it should be a recursive solution that reduces the items in the same way for a JSON of arbitrary depth - it currently only does so for depth 2.

def log_reducer(response_log):
original_response_log = response_log
try:
    if type(response_log) == dict:
        if len(response_log) >= 4:  # {123456}
            response_log = dict(list(response_log.items())[:4])
            response_log.update({"...": "..."})  # {1234...}
        for key, value in response_log.items():
            if type(value) == list:
                if len(value) >= 5:  # {key:[123456]}
                    new_item = value[:2] + ['...'] + value[-2:]  # {[12...56]}
                    response_log.update({key: new_item})
            if type(value) == dict:
                if len(value) >= 4:  # {key:{123456}}
                    reduced_dict = dict(list(value.items())[:4])
                    reduced_dict.update({"...": "..."})
                    response_log.update({key: reduced_dict})  # {{1234...}}

    elif type(response_log) == list:
        if len(response_log) >= 5:  # [123456]
            response_log = response_log[:2] + ['...'] + response_log[-2:]  # [12...56]
        for inner_item in response_log:
            if type(inner_item) == list:
                if len(inner_item) >= 5:  # [[123456]]
                    reduced_list = inner_item[:2] + ['...'] + inner_item[-2:]  # [[12...56]]
                    response_log.remove(inner_item)
                    response_log.append(reduced_list)
            if type(inner_item) == dict:
                if len(inner_item) >= 4:  # [{123456}]
                    reduced_dict = dict(list(inner_item.items())[:4])
                    reduced_dict.update({"...": "..."})  # [{1234...}]
                    response_log.remove(inner_item)
                    response_log.append(reduced_dict)
except Exception as e:
    return original_response_log
return response_log

The returned response_log is then logged with logger.info(str(response_log))

As you can see, the fact that there can be either arrays or dictionaries at every level makes this task a little more complex, and I am struggling to find a library or code snipped of any kind which would simplify this. If anyone wants to give it a shot, I would appreciate it a lot.

you can use a test JSON like this to see it in effect:

test_json = {"works": [1, 2, 3, 4, 5, 6],
             "not_affected": [{"1": "1", "2": "2", "3": "3", "4": "4", "5": "5"}],
             "1": "1", "2": "2", "3": "3",
             "removed": "removed"
             }
print("original", test_json)
reduced_log = log_reducer(test_json)
print("reduced", reduced_log)

print("original", test_json)
reduced_log = log_reducer([test_json])  # <- increases nesting depth
print("reduced", reduced_log)

Body:
This answer uses @calceamenta's idea, but implements the actual cutting-down logic:

def recursive_reduce(obj):
    if isinstance(obj, (float, str, int, bool, type(None))):
        return obj

    if isinstance(obj, dict):
        keys = list(sorted(obj))
        obj['...'] = '...'

        if len(keys) > 5:
            new_keys = keys[:2] + ["..."] + keys[-2:]
        else:
            new_keys = keys

        new_dict = {x:obj[x] for x in new_keys}
        for k, v in new_dict.items():
            new_dict[k] = recursive_reduce(v)

        return new_dict

    if isinstance(obj, list):
        if len(obj) > 5:
            new_list = obj[:2] + ["..."] + obj[-2:]
        else:
            new_list = obj

        for i, v in enumerate(new_list):
            new_list[i] = recursive_reduce(v)

        return new_list

    return str(obj)

test_json = {"works": [1, 2, 3, 4, 5, 6],
             "not_affected": [{"1": "1", "2": "2", "3": "3", "4": "4", "5": "5"}],
             "1": "1", "2": "2", "3": "3",
             "removed": "removed"
             }

print("original", test_json)
reduced_log = recursive_reduce(test_json)
print("reduced", reduced_log)

Output:

original {'works': [1, 2, 3, 4, 5, 6], 'not_affected': [{'1': '1', '2': '2', '3': '3', '4': '4', '5': '5'}], '1': '1', '2': '2', '3': '3', 'removed': 'removed'}
reduced {'1': '1', '2': '2', '...': '...', 'removed': 'removed', 'works': [1, 2, '...', 5, 6]}

Hope this helps :)


问:
linq2sql C#: How to query from a table with changing schema name
I have a webservice which tries to connect to a database of a desktop accounting application.

It have tables with same name but with different schema names such as:

```<code>[DatabaseName].[202001].[CustomerCredit]
[DatabaseName].[202002].[CustomerCredit]
.
.
.
[DatabaseName].[202014].[CustomerCredit]
[DatabaseName].[202015].[CustomerCredit]
[DatabaseName].[202016].[CustomerCredit]
...
..
[DatabaseName].[2020xx].[CustomerCredit]
</code>```

Schema name is in format [Year+IncrementalNumber] such as [202014], [202015],[202016] and etc.

Whenever I want to query customer credit information in database, I should fetch information from schema with biggest number such as [DatabaseName].[202016].[CustomerCredit] if 202016 is latest schema in my db.

Note:
Creation of new schema in accounting application database have no rules and is completely decided by user of accounting application and every instance of application installed on different place may have different number of schemas.

So when I'm developing my webservice I have no idea to connect to which schema prior to development. In run-time I can find correct schema to query from its tables but I don't know how to manage to fetch table information with correct schema name in query.
I ususally creat a linq-to-sql dbml class and use its definitions to read information from db but I don't know how to manage schema change in this way?

DBML designer manage Scehma names like this:

```<code>[global::System.Data.Linq.Mapping.TableAttribute(Name="[202001].CustomerCredit")]
</code>```

However since my app can retrieve schema name in run time, I don't know how to fix table declaration in my special case.
It is so easy to handle in ADO.NET but I don't know its equivalent in Linq2SQL:

```<code>select count(*)  from [" + Variables.FinancialYearSchemaName + "].CustomerCredit  where SFC_Status = 100;
</code>```

答:
That schema is essentially a manual partitioning of the CustomerCredit table. The best solution would one that makes partitioning transparent to all users. The code shouldn't know how the data is partitioned.

Database Solutions

The benefit of database solutions is that they are transparent or almost transparent to users and require minimal maintenance

Table Partitioning

The clean solution would be to use table partitioning, making the different partitions transparent to all users. Table partitioning used to be an Enterprise-only feature but it became available in all editions since SQL Server 2016 SP1, even Express. This means it's free in all versions still in mainstream support.

The table is partitioned based on a function (eg a date based function) and stored in different files. Whenever possible, the query optimizer can check the partition boundaries and the query conditions and use only the file that contains the relevant data. Eg in a date-partitioned table, queries that contain a date filter can search only the relevant partitions.

Partitioned views

Another option, available since 2000 at least, is to use partitionend views, essentially a UNION ALL view that combines all table partitions, eg :

```<code>SELECT <select_list1>
FROM [202001].[CustomerCredit]
UNION ALL
SELECT <select_list2>
FROM [202002].[CustomerCredit]
UNION ALL
...
SELECT <select_listn>
FROM Tn;
</code>```

EF can map entities to views instead of tables. If the criteria for updatable views are met, the partitioned view itself will be updatable and any modifications will be made to the correct table.

The query optimizer can take advantage of CHECK constraints on the tables to search only one table at a time, similar to how partitioned tables work.

Code solutions

This requires raw SQL queries, and a way to identify the correct table/schema each time a change is made. It requires modifications to the application each time the table partitioning changes, whether those are code modifications, or changes in a configuration file.

In all cases, one query can only read from one table at a time

Keep ADO.NET

One possibility is to keep using ADO.NET, replacing the table/schema name in a query template. The code will have to map to objects if needed, the same way it already did.

EF Raw SQL

Another, is to use EF's raw SQL features, eg EF Core's FromSqlRaw to query from a specific table , the same way ADO.NET would. The benefit is that EF will map the query results to objects. In EF Core, the raw query can be combined with LINQ operators :

```<code>var query=$"select * from [DatabaseName].[{schemaName}].[CustomerCredit]"
var credits = context.CustomerCredits
    .FromSqlRaw(query)
    .Where(...)
    .ToList();
</code>```

Dapper

Another option is to use Dapper or another micro-ORM with an ad-hoc query, similar to ADO.NET, and map the results to objects:

```<code>var query=$"select * from [DatabaseName].[{schemaName}].[CustomerCredit] where customerID=@ID";
var credits=connection.Query<CustomerCredit>(query,new {ID=someID});
</code>```

Body:
I have a webservice which tries to connect to a database of a desktop accounting application.

It have tables with same name but with different schema names such as:

[DatabaseName].[202001].[CustomerCredit]
[DatabaseName].[202002].[CustomerCredit]
.
.
.
[DatabaseName].[202014].[CustomerCredit]
[DatabaseName].[202015].[CustomerCredit]
[DatabaseName].[202016].[CustomerCredit]
...
..
[DatabaseName].[2020xx].[CustomerCredit]

Schema name is in format [Year+IncrementalNumber] such as [202014], [202015],[202016] and etc.

Whenever I want to query customer credit information in database, I should fetch information from schema with biggest number such as [DatabaseName].[202016].[CustomerCredit] if 202016 is latest schema in my db.

Note:
Creation of new schema in accounting application database have no rules and is completely decided by user of accounting application and every instance of application installed on different place may have different number of schemas.

So when I'm developing my webservice I have no idea to connect to which schema prior to development. In run-time I can find correct schema to query from its tables but I don't know how to manage to fetch table information with correct schema name in query.
I ususally creat a linq-to-sql dbml class and use its definitions to read information from db but I don't know how to manage schema change in this way?

DBML designer manage Scehma names like this:

[global::System.Data.Linq.Mapping.TableAttribute(Name="[202001].CustomerCredit")]

However since my app can retrieve schema name in run time, I don't know how to fix table declaration in my special case.
It is so easy to handle in ADO.NET but I don't know its equivalent in Linq2SQL:

select count(*)  from [" + Variables.FinancialYearSchemaName + "].CustomerCredit  where SFC_Status = 100;

Body:
That schema is essentially a manual partitioning of the CustomerCredit table. The best solution would one that makes partitioning transparent to all users. The code shouldn't know how the data is partitioned.

Database Solutions

The benefit of database solutions is that they are transparent or almost transparent to users and require minimal maintenance

Table Partitioning

The clean solution would be to use table partitioning, making the different partitions transparent to all users. Table partitioning used to be an Enterprise-only feature but it became available in all editions since SQL Server 2016 SP1, even Express. This means it's free in all versions still in mainstream support.

The table is partitioned based on a function (eg a date based function) and stored in different files. Whenever possible, the query optimizer can check the partition boundaries and the query conditions and use only the file that contains the relevant data. Eg in a date-partitioned table, queries that contain a date filter can search only the relevant partitions.

Partitioned views

Another option, available since 2000 at least, is to use partitionend views, essentially a UNION ALL view that combines all table partitions, eg :

SELECT <select_list1>
FROM [202001].[CustomerCredit]
UNION ALL
SELECT <select_list2>
FROM [202002].[CustomerCredit]
UNION ALL
...
SELECT <select_listn>
FROM Tn;

EF can map entities to views instead of tables. If the criteria for updatable views are met, the partitioned view itself will be updatable and any modifications will be made to the correct table.

The query optimizer can take advantage of CHECK constraints on the tables to search only one table at a time, similar to how partitioned tables work.

Code solutions

This requires raw SQL queries, and a way to identify the correct table/schema each time a change is made. It requires modifications to the application each time the table partitioning changes, whether those are code modifications, or changes in a configuration file.

In all cases, one query can only read from one table at a time

Keep ADO.NET

One possibility is to keep using ADO.NET, replacing the table/schema name in a query template. The code will have to map to objects if needed, the same way it already did.

EF Raw SQL

Another, is to use EF's raw SQL features, eg EF Core's FromSqlRaw to query from a specific table , the same way ADO.NET would. The benefit is that EF will map the query results to objects. In EF Core, the raw query can be combined with LINQ operators :

var query=$"select * from [DatabaseName].[{schemaName}].[CustomerCredit]"
var credits = context.CustomerCredits
    .FromSqlRaw(query)
    .Where(...)
    .ToList();

Dapper

Another option is to use Dapper or another micro-ORM with an ad-hoc query, similar to ADO.NET, and map the results to objects:

var query=$"select * from [DatabaseName].[{schemaName}].[CustomerCredit] where customerID=@ID";
var credits=connection.Query<CustomerCredit>(query,new {ID=someID});


问:
R: Sum specific columns grouped by a particular column
I have a data frame that looks like this:

```<code>Col1 Col2 Col3 Col4
10    A    5    4
10    A    6    3
30    B    2    7
45    C    5    1
45    C    2    1
</code>```

I want to sum columns 3 and 4 only grouped by Col2, such that my resulting data frame looks like

```<code>Col1 Col2 Col3 Col4
10    A    11    7
30    B    2     7
45    C    7     2
</code>```

Col1 is an identifier for Col2 so I want that to remain. Thanks

答:
You can use aggregate. As Col1 is an identifier for Col2 it could be used with Col2 for grouping.

```<code>aggregate(.~Col1+Col2, df, sum)
#  Col1 Col2 Col3 Col4
#1   10    A   11    7
#2   30    B    2    7
#3   45    C    7    2
</code>```

or you can use rowsum, match and cbind:

```<code>x <- rowsum(df[c("Col3","Col4")], df$Col2)
cbind(df[match(rownames(x), df$Col2), c("Col1","Col2")], x)
#  Col1 Col2 Col3 Col4
#1   10    A   11    7
#3   30    B    2    7
#4   45    C    7    2
</code>```

Body:
I have a data frame that looks like this:

Col1 Col2 Col3 Col4
10    A    5    4
10    A    6    3
30    B    2    7
45    C    5    1
45    C    2    1

I want to sum columns 3 and 4 only grouped by Col2, such that my resulting data frame looks like

Col1 Col2 Col3 Col4
10    A    11    7
30    B    2     7
45    C    7     2

Col1 is an identifier for Col2 so I want that to remain. Thanks

Body:
You can use aggregate. As Col1 is an identifier for Col2 it could be used with Col2 for grouping.

aggregate(.~Col1+Col2, df, sum)
#  Col1 Col2 Col3 Col4
#1   10    A   11    7
#2   30    B    2    7
#3   45    C    7    2

or you can use rowsum, match and cbind:

x <- rowsum(df[c("Col3","Col4")], df$Col2)
cbind(df[match(rownames(x), df$Col2), c("Col1","Col2")], x)
#  Col1 Col2 Col3 Col4
#1   10    A   11    7
#3   30    B    2    7
#4   45    C    7    2


问:
How can I find the number of arguments of a Python function?
How can I find the number of arguments of a Python function? I need to know how many normal arguments it has and how many named arguments.

Example:

```<code>def someMethod(self, arg1, kwarg1=None):
    pass
</code>```

This method has 2 arguments and 1 named argument.

答:
In:

```<code>import inspect

class X:
    def xyz(self, a, b, c):
        return

print(len(inspect.getfullargspec(X.xyz).args))
</code>```

Out:

4

Note: If xyz wasn't inside class X and had no "self" and just "a, b, c", then it would have printed 3.

For python below 3.5, you may want to replace inspect.getfullargspec by inspect.getargspec in the code above.

ContentLicense: CC BY-SA 2.5

Body:
How can I find the number of arguments of a Python function? I need to know how many normal arguments it has and how many named arguments.

Example:

def someMethod(self, arg1, kwarg1=None):
    pass

This method has 2 arguments and 1 named argument.

Body:
In:

import inspect

class X:
    def xyz(self, a, b, c):
        return

print(len(inspect.getfullargspec(X.xyz).args))

Out:

4

Note: If xyz wasn't inside class X and had no "self" and just "a, b, c", then it would have printed 3.

For python below 3.5, you may want to replace inspect.getfullargspec by inspect.getargspec in the code above.


问:
Build failing when working with strict mode in tsconfig for spring-flo, jointjs, code-mirror
When tsconfig.json has strict mode on, when doing ng serve spring-flow dependencies throws error for code-mirror and Model

What can be done here.

答: Converting it to skipLibCheck and skipDefaultLibCheck to true works as required.


问:
How to export a json file from a specific Influxdb database?
I have an influxdb database named metrics, I want to export it as a JSON file with its entire data.

```<code>directory = '/var/lib/influxdb/data'
</code>```

I can save the list of tag keys:

```<code>influx -execute='show tag keys' -database=metrics -format=json -pretty=true > /home/ivms/metrics.json
</code>```

but how can I save all tags with all values?

答:
The query to return all data from all measurements in the database, including both fields and tags, would be:

SELECT * FROM /.*/
```

<p>The full command line:</p>

<pre class="lang-sh prettyprint-override"><code>influx -database=metrics -format=json -pretty=true  \
    -execute='SELECT * FROM /.*/'  \
    > ~/metrics.json
</code>```

NOTE:

If your database is complicated (millions of series) you might have to wait a long time for influx to actually respond, in my case it was almost 30min but it worked.

Also Make sure you give the system a large enough swap, to avoid getting OOM killed.

Body:
I have an influxdb database named metrics, I want to export it as a JSON file with its entire data.

directory = '/var/lib/influxdb/data'

I can save the list of tag keys:

influx -execute='show tag keys' -database=metrics -format=json -pretty=true > /home/ivms/metrics.json

but how can I save all tags with all values?

Body:
The query to return all data from all measurements in the database, including both fields and tags, would be:

SELECT * FROM /.*/

The full command line:

influx -database=metrics -format=json -pretty=true  \
    -execute='SELECT * FROM /.*/'  \
    > ~/metrics.json

NOTE:

If your database is complicated (millions of series) you might have to wait a long time for influx to actually respond, in my case it was almost 30min but it worked.

Also Make sure you give the system a large enough swap, to avoid getting OOM killed.


问:
Limit on Group by in Mysql
In my web application, I would like to show a list of my users. Users can have different statuses (FB-like). I want to display the last 3 statuses below each user's name.

```<code>Dinosaur
   I'm a dino!
   I eat meat!
   I like cows!
Fish
   Blub!
   I don't like dinosaurs!
   Going for a swim!
</code>```

I have the following SQL query:

```<code>SELECT s.status, u.voornaam, u.achternaam FROM status AS s INNER JOIN sportjefit_user AS u ON s.user = u.id where u.begeleider='53' group by id desc limit 3
</code>```

However, this returns only the top 3 of the results, in this case it would only show the Dinosaur's statuses. I want to show the top 3 statuses for every users though. Can I do this with a group by and put a limit on this or do I need multiple queries here? And, if I do need multiple queries, how would I go about implementing this? The number of users will keep increasing as my application grows.

Can someone help me out?

Thanks.

答:
I'd use multiple queries like this:

```<code>select all users
for each user
    select last 3 statuses
</code>```

using

```<code>SELECT * FROM sportjefit_user
</code>```

and

```<code>SELECT status FROM status WHERE userid = ? ORDER BY id DESC LIMIT 3
</code>```

How many users do you anticipate? If you're trying to show all users on a page, I'd paginate them. Hope this helps.

ContentLicense: CC BY-SA 3.0

Title: Limit on Group by in Mysql

Body:
In my web application, I would like to show a list of my users. Users can have different statuses (FB-like). I want to display the last 3 statuses below each user's name.

Dinosaur
   I'm a dino!
   I eat meat!
   I like cows!
Fish
   Blub!
   I don't like dinosaurs!
   Going for a swim!

I have the following SQL query:

SELECT s.status, u.voornaam, u.achternaam FROM status AS s INNER JOIN sportjefit_user AS u ON s.user = u.id where u.begeleider='53' group by id desc limit 3

However, this returns only the top 3 of the results, in this case it would only show the Dinosaur's statuses. I want to show the top 3 statuses for every users though. Can I do this with a group by and put a limit on this or do I need multiple queries here? And, if I do need multiple queries, how would I go about implementing this? The number of users will keep increasing as my application grows.

Can someone help me out?

Thanks.

Body:
I'd use multiple queries like this:

select all users
for each user
    select last 3 statuses

using

SELECT * FROM sportjefit_user

and

SELECT status FROM status WHERE userid = ? ORDER BY id DESC LIMIT 3

How many users do you anticipate? If you're trying to show all users on a page, I'd paginate them. Hope this helps.


问:
Changing default Styles in a Word document in a separate sub using Excel
In a word document controlled by Excel I want to make a subtle change to the used default styles. This works using the following code, when a word document is already open:

```<code>Private Sub basic_syle()
    Dim objWord As Word.Application
    Dim objSelection As Word.selection

    Set objWord = GetObject(, "Word.application")
    Set objSelection = objWord.selection

    adjust_obj_style
End Sub

Sub adjust_obj_style()
    ActiveDocument.Styles("Heading 1").ParagraphFormat.PageBreakBefore = False
End Sub
</code>```

However if I want to let Excel create the word document and adjust the style the style is not changed.

```<code>Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSelection As Word.selection

Private Sub basic_syle()
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    Set objSelection = objWord.selection

    objWord.Visible = True

    adjust_obj_style
End Sub
</code>```

While I think it has to do with the activeDocument class I did not yet find a correct solution. Activating the document object was no solution.

Styles documentation
PageBreak documentation

答:
You need to redefine the sub:

```<code>Sub adjust_obj_style(doc as Object)
</code>```

and then pass that document to the sub:

```<code>adjust_obj_style objDoc
</code>```

and then use that variable instead of ActiveDocument like this:

```<code>doc.Styles("Heading 1").ParagraphFormat.PageBreakBefore = False
</code>```

Body:
In a word document controlled by Excel I want to make a subtle change to the used default styles. This works using the following code, when a word document is already open:

Private Sub basic_syle()
    Dim objWord As Word.Application
    Dim objSelection As Word.selection

    Set objWord = GetObject(, "Word.application")
    Set objSelection = objWord.selection

    adjust_obj_style
End Sub

Sub adjust_obj_style()
    ActiveDocument.Styles("Heading 1").ParagraphFormat.PageBreakBefore = False
End Sub

However if I want to let Excel create the word document and adjust the style the style is not changed.

Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSelection As Word.selection

Private Sub basic_syle()
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    Set objSelection = objWord.selection

    objWord.Visible = True

    adjust_obj_style
End Sub

While I think it has to do with the activeDocument class I did not yet find a correct solution. Activating the document object was no solution.

Styles documentation
PageBreak documentation

Body:
You need to redefine the sub:

Sub adjust_obj_style(doc as Object)

and then pass that document to the sub:

adjust_obj_style objDoc

and then use that variable instead of ActiveDocument like this:

doc.Styles("Heading 1").ParagraphFormat.PageBreakBefore = False


问:
Remove commit from history
I entered a curseword in my code and I pushed the code on the master branch. I pushed a few more times after that so people do not pull the bad stuff, but I can still find the curseword in the commits history.

I don't want to add the file to .gitignore because we need that file.

Is there a way to delete the commit from history?

答:
This works for me:

git log to find the commit you want to remove and copy its hash

git rebase -i <commit_hash>~ which opens your text editor

in text editor, switch from pick to drop for your particular commit

Title: Remove commit from history


问:
How should I save QTextEdit content with format to database?toPlainText method not get format
```<code>def add_question(self):
    sub_id = self.qSubComboBox.itemData(self.qComboBox.currentIndex())
    question = self.textEdit.toPlainText()
    option1 = self.textEdit_2.toPlainText()
    option2 = self.textEdit_3.toPlainText()
    option3 = self.textEdit_4.toPlainText()
    option4 = self.textEdit_5.toPlainText()
    option5 = self.textEdit_6.toPlainText()
    data = (sub_id,question,option1,option2,option3,option4,option5,)

    if question=="" or option1=="" or option2=="":
        QMessageBox.about(self, "Uyarı", "Soru metni ve en az iki seçenek boş bırakılamaz.")
    else:
        helper.insert("INSERT INTO question(SubID,Question,Option1,Option2,Option3,Option4,Option5) VALUES(?,?,?,?,?,?,?)", data)
        QMessageBox.about(self, "TestPro", "Soru başarıyla kaydedildi.")
</code>```

<p>'''I want to save QTextEdit contents to database with text formatting.'''</p>

答:
Example : test text is bold >> tohmtl() . When I saved to database,

My database record is:
<----html>....
.....test
....<---html>

is there a another simply way:)?

Body:
def add_question(self):
    sub_id = self.qSubComboBox.itemData(self.qComboBox.currentIndex())
    question = self.textEdit.toPlainText()
    option1 = self.textEdit_2.toPlainText()
    option2 = self.textEdit_3.toPlainText()
    option3 = self.textEdit_4.toPlainText()
    option4 = self.textEdit_5.toPlainText()
    option5 = self.textEdit_6.toPlainText()
    data = (sub_id,question,option1,option2,option3,option4,option5,)

    if question=="" or option1=="" or option2=="":
        QMessageBox.about(self, "Uyarı", "Soru metni ve en az iki seçenek boş bırakılamaz.")
    else:
        helper.insert("INSERT INTO question(SubID,Question,Option1,Option2,Option3,Option4,Option5) VALUES(?,?,?,?,?,?,?)", data)
        QMessageBox.about(self, "TestPro", "Soru başarıyla kaydedildi.")

'''I want to save QTextEdit contents to database with text formatting.'''


答:
If you just want to group by Col2 and retain Col1, maybe you can use aggregate + merge from base R like below

```<code>dfout <- merge(unique(df[1:2]),aggregate(.~Col2,df[-1],sum))
</code>```

such that

```<code>> dfout
  Col2 Col1 Col3 Col4
1    A   10   11    7
2    B   30    2    7
3    C   45    7    2
</code>```

Title: R: Sum specific columns grouped by a particular column

Body:
If you just want to group by Col2 and retain Col1, maybe you can use aggregate + merge from base R like below

dfout <- merge(unique(df[1:2]),aggregate(.~Col2,df[-1],sum))

such that

> dfout
  Col2 Col1 Col3 Col4
1    A   10   11    7
2    B   30    2    7
3    C   45    7    2


问:
direction: rtl with javascript
I am using the following JS to mimick ellipsis with Firfox. All works well but i need to change the direction of the text so the ellipsis is at the beginning rather than at the end of the text.

Does anyone know how to add this functionality to the code below

Thanks in advance

L

```<code>(function($) {

$.fn.ellipsis = function(enableUpdating){
    var s = document.documentElement.style;
    if (!('textOverflow' in s || 'OTextOverflow' in s)) {
        return this.each(function(){
            var el = $(this);
            if(el.css("overflow") == "hidden"){

                var originalText = el.html();
                var w = el.width();

                var t = $(this.cloneNode(true)).hide().css({

                    'position': 'absolute',
                    'width': 'auto',
                    'overflow': 'visible',
                    'max-width': 'inherit'
                });
                el.after(t);

                var text = originalText;
                while(text.length > 0 && t.width() > el.width()){
                    text = text.substr(0, text.length - 1);
                    t.html(text + "...");
                }
                el.html(t.html());

                t.remove();

                if(enableUpdating == true){
                    var oldW = el.width();
                    setInterval(function(){
                        if(el.width() != oldW){
                            oldW = el.width();
                            el.html(originalText);
                            el.ellipsis();
                        }
                    }, 200);
                }
            }
        });
    } else return this;
};
</code>```

})(jQuery);

答:
It ought to work by adding

```<code>'direction': 'rtl'
</code>```

to the CSS map you already use.

Title: direction: rtl with javascript

Body:
I am using the following JS to mimick ellipsis with Firfox. All works well but i need to change the direction of the text so the ellipsis is at the beginning rather than at the end of the text.

Does anyone know how to add this functionality to the code below

Thanks in advance

L

(function($) {

$.fn.ellipsis = function(enableUpdating){
    var s = document.documentElement.style;
    if (!('textOverflow' in s || 'OTextOverflow' in s)) {
        return this.each(function(){
            var el = $(this);
            if(el.css("overflow") == "hidden"){

                var originalText = el.html();
                var w = el.width();

                var t = $(this.cloneNode(true)).hide().css({

                    'position': 'absolute',
                    'width': 'auto',
                    'overflow': 'visible',
                    'max-width': 'inherit'
                });
                el.after(t);

                var text = originalText;
                while(text.length > 0 && t.width() > el.width()){
                    text = text.substr(0, text.length - 1);
                    t.html(text + "...");
                }
                el.html(t.html());

                t.remove();

                if(enableUpdating == true){
                    var oldW = el.width();
                    setInterval(function(){
                        if(el.width() != oldW){
                            oldW = el.width();
                            el.html(originalText);
                            el.ellipsis();
                        }
                    }, 200);
                }
            }
        });
    } else return this;
};

})(jQuery);

Body:
It ought to work by adding

'direction': 'rtl'

to the CSS map you already use.


问:
Object is possibly 'null' in email validation Angular FormControl
I get this message when I ng s --aot  Object is possibly 'null'.

I've been trying things all morning to fix it, any ideas? The error occurs on the  <mdb-success *ngIf="email.valid && (email.dirty || email.touched)" class="mt-2">Email valid</mdb-success> email.valid, dirty etc objects.

```<code> <form class="form-inline md-form form-sm" [formGroup]="validatingForm" (submit)="update(emailInput.value)">
          <input mdbInput mdbValidate type="email" class="form-control form-control-sm mr-3 w-75" placeholder="Enter user's email" aria-label="Search" formControlName="email" #emailInput required />
          <button mdbBtn type="button" color="primary" mdbWavesEffect (click)="update(emailInput.value)">
            <mdb-icon fas icon="search" aria-hidden="true"></mdb-icon>
          </button>
          <mdb-error *ngIf="email.invalid && (email.dirty || email.touched)" class="mt-2">Email invalid</mdb-error>
          <mdb-success *ngIf="email.valid && (email.dirty || email.touched)" class="mt-2">Email valid</mdb-success>
        </form>
</code>```

ts code:

```<code> export class UsersComponent implements OnInit {
      validatingForm: FormGroup;
      token: any;
      userDetails: {};
      hasUsers = false;
      emailValue: string;
      constructor(private crudService: CrudService) {}

      ngOnInit() {
        this.validatingForm = new FormGroup({
          email: new FormControl("", Validators.email)
        });
        this.loginAsAdmin();
      }

      get email() {
        return this.validatingForm.get("email");
      }
</code>```

答: *ngIf="email && email.valid && (email.dirty || email.touched)" @ritaj</p>

Body:
I get this message when I ng s --aot  Object is possibly 'null'.

I've been trying things all morning to fix it, any ideas? The error occurs on the  <mdb-success *ngIf="email.valid && (email.dirty || email.touched)" class="mt-2">Email valid</mdb-success> email.valid, dirty etc objects.

 <form class="form-inline md-form form-sm" [formGroup]="validatingForm" (submit)="update(emailInput.value)">
          <input mdbInput mdbValidate type="email" class="form-control form-control-sm mr-3 w-75" placeholder="Enter user's email" aria-label="Search" formControlName="email" #emailInput required />
          <button mdbBtn type="button" color="primary" mdbWavesEffect (click)="update(emailInput.value)">
            <mdb-icon fas icon="search" aria-hidden="true"></mdb-icon>
          </button>
          <mdb-error *ngIf="email.invalid && (email.dirty || email.touched)" class="mt-2">Email invalid</mdb-error>
          <mdb-success *ngIf="email.valid && (email.dirty || email.touched)" class="mt-2">Email valid</mdb-success>
        </form>

ts code:

 export class UsersComponent implements OnInit {
      validatingForm: FormGroup;
      token: any;
      userDetails: {};
      hasUsers = false;
      emailValue: string;
      constructor(private crudService: CrudService) {}

      ngOnInit() {
        this.validatingForm = new FormGroup({
          email: new FormControl("", Validators.email)
        });
        this.loginAsAdmin();
      }

      get email() {
        return this.validatingForm.get("email");
      }


问:
Bitbucket Pipeline - PHP extension zip is missing
I'm trying to set up pipelines on bitbucket and I'm receiving an error when composer runs

```<code>Your requirements could not be resolved to an installable set of packages.
  Problem 1
    - Installation request for craftcms/cms 3.1.24 -> satisfiable by craftcms/cms[3.1.24].
    - craftcms/cms 3.1.24 requires ext-zip * -> the requested PHP extension zip is missing from your system.
</code>```

I'm not sure exactly what I need to add to my bitbucket-pipelines.yml file.

This is my current file:

```<code>image: php:7.2
pipelines:
  branches:
    master:
      - step:
          script:
            - apt-get update && apt-get install -y unzip git rsync zip
            - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
            - composer install
            - php deploy.php
</code>```

This guy seems to have a similar issue, but I'm not sure how he solved. It's vague and he also seems to have more output regarding mcrypt: Docker: bitbucket pipelines ext-zip *

If you're curious, I got my configuration from this suggested commit at fortrabbit.com: https://gist.github.com/ukautz/4f3219c3eb5d97fbd018027dca4b8808

The php file it's running is on the link above, but it doesn't reach that yet, so it's irrelevant to my problem

I'm not entirely sure what I have to install or if I'm just not seeing the actual problem logged (for instance if there's more granular logs somewhere and I'm not seeing the actual issue)

答:
I got it working by installing libzip-dev and executing the docker-php-ext-install zip and docker-php-ext-enable zip commands.

Here is a part of my bitbucket-pipelines.yml:

```<code>  step:
    caches:
      - composer
    script:
      - apt-get update && apt-get install -y unzip libzip-dev
      - docker-php-ext-install zip
      - docker-php-ext-enable zip
      - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
      - composer install
</code>```

Your bitbucket-pipelines.yml would look like this:

```<code>image: php:7.2
pipelines:
  branches:
    master:
      - step:
          script:
            - apt-get update && apt-get install -y unzip git rsync zip libzip-dev
            - docker-php-ext-install zip
            - docker-php-ext-enable zip
            - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
            - composer install
            - php deploy.php
</code>```

Body:
I'm trying to set up pipelines on bitbucket and I'm receiving an error when composer runs

Your requirements could not be resolved to an installable set of packages.
  Problem 1
    - Installation request for craftcms/cms 3.1.24 -> satisfiable by craftcms/cms[3.1.24].
    - craftcms/cms 3.1.24 requires ext-zip * -> the requested PHP extension zip is missing from your system.

I'm not sure exactly what I need to add to my bitbucket-pipelines.yml file.

This is my current file:

image: php:7.2
pipelines:
  branches:
    master:
      - step:
          script:
            - apt-get update && apt-get install -y unzip git rsync zip
            - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
            - composer install
            - php deploy.php

This guy seems to have a similar issue, but I'm not sure how he solved. It's vague and he also seems to have more output regarding mcrypt: Docker: bitbucket pipelines ext-zip *

If you're curious, I got my configuration from this suggested commit at fortrabbit.com: https://gist.github.com/ukautz/4f3219c3eb5d97fbd018027dca4b8808

The php file it's running is on the link above, but it doesn't reach that yet, so it's irrelevant to my problem

I'm not entirely sure what I have to install or if I'm just not seeing the actual problem logged (for instance if there's more granular logs somewhere and I'm not seeing the actual issue)

Body:
I got it working by installing libzip-dev and executing the docker-php-ext-install zip and docker-php-ext-enable zip commands.

Here is a part of my bitbucket-pipelines.yml:

  step:
    caches:
      - composer
    script:
      - apt-get update && apt-get install -y unzip libzip-dev
      - docker-php-ext-install zip
      - docker-php-ext-enable zip
      - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
      - composer install

Your bitbucket-pipelines.yml would look like this:

image: php:7.2
pipelines:
  branches:
    master:
      - step:
          script:
            - apt-get update && apt-get install -y unzip git rsync zip libzip-dev
            - docker-php-ext-install zip
            - docker-php-ext-enable zip
            - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
            - composer install
            - php deploy.php


问:
Get 2 meta values from meta key column
I need to use some library to export product names, SKU and prices to one CSV file. This library connects using PDO and needs an SQL query.

I want to select 'name', 'SKU' and 'price' from 2 WordPress tables, namely wp_posts and wp_postmeta.

I don't know how to get data from 'meta_value' column twice for 'meta_key'='_price' and 'meta_key'='_sku', ex.

My current query:

```<code>"SELECT a.post_title, m1.meta_value, m2.meta_value FROM wp_posts a, wp_postmeta m1, wp_postmeta m2
        WHERE a.post_type='product' AND m1.post_id = a.ID
        AND m1.meta_key='_sku'
        AND m2.meta_key='_price'"
</code>```

答:
It sounds like you could do with a join so you're relating the meta information to the right posts.

```<code>SELECT
  post.post_title,
  meta.meta_value
FROM wp_posts AS post
LEFT JOIN wp_postmeta AS meta
  ON post.post_id = meta.post_id
WHERE post.post_type = 'product'
  AND meta.meta_key IN ('_sku', '_price')
</code>```

Example results:

```<code>post_title    | meta_value
--------------|-----------
Cheddar       | CHE001
Cheddar       | 2.45
Red Leicester | CHE002
...
</code>```

This assumes that the id column in wp_posts is post_id.

It's important to note that this will return up to two rows for each post, depending on whether it has a meta row for _sku and _price). If you need the data all on the same row (as you might for your export) you might need something like this instead:

```<code>SELECT
  post.post_title,
  metaSku.meta_value AS sku,
  metaPrice.meta_value AS price
FROM wp_posts AS post
LEFT JOIN (
  SELECT
    *
  FROM wp_postmeta
  WHERE meta_key = '_sku'
) AS metaSku
  ON post.post_id = metaSku.post_id
LEFT JOIN (
  SELECT
    *
  FROM wp_postmeta
  WHERE meta_key = '_price'
) AS metaPrice
  ON post.post_id = metaPrice.post_id
WHERE post.post_type = 'product'
</code>```

Example results:

```<code>post_title    | sku    | price
--------------|--------|------
Cheddar       | CHE001 | 2.45
Red Leicester | CHE002 |
...
</code>```

I hope this helps.

Title: Get 2 meta values from meta key column

Body:
I need to use some library to export product names, SKU and prices to one CSV file. This library connects using PDO and needs an SQL query.

I want to select 'name', 'SKU' and 'price' from 2 WordPress tables, namely wp_posts and wp_postmeta.

I don't know how to get data from 'meta_value' column twice for 'meta_key'='_price' and 'meta_key'='_sku', ex.

My current query:

"SELECT a.post_title, m1.meta_value, m2.meta_value FROM wp_posts a, wp_postmeta m1, wp_postmeta m2
        WHERE a.post_type='product' AND m1.post_id = a.ID
        AND m1.meta_key='_sku'
        AND m2.meta_key='_price'"

Body:
It sounds like you could do with a join so you're relating the meta information to the right posts.

SELECT
  post.post_title,
  meta.meta_value
FROM wp_posts AS post
LEFT JOIN wp_postmeta AS meta
  ON post.post_id = meta.post_id
WHERE post.post_type = 'product'
  AND meta.meta_key IN ('_sku', '_price')

Example results:

post_title    | meta_value
--------------|-----------
Cheddar       | CHE001
Cheddar       | 2.45
Red Leicester | CHE002
...

This assumes that the id column in wp_posts is post_id.

It's important to note that this will return up to two rows for each post, depending on whether it has a meta row for _sku and _price). If you need the data all on the same row (as you might for your export) you might need something like this instead:

SELECT
  post.post_title,
  metaSku.meta_value AS sku,
  metaPrice.meta_value AS price
FROM wp_posts AS post
LEFT JOIN (
  SELECT
    *
  FROM wp_postmeta
  WHERE meta_key = '_sku'
) AS metaSku
  ON post.post_id = metaSku.post_id
LEFT JOIN (
  SELECT
    *
  FROM wp_postmeta
  WHERE meta_key = '_price'
) AS metaPrice
  ON post.post_id = metaPrice.post_id
WHERE post.post_type = 'product'

Example results:

post_title    | sku    | price
--------------|--------|------
Cheddar       | CHE001 | 2.45
Red Leicester | CHE002 |
...

I hope this helps.


问:
Vuetify: Hide a skeleton loader after a element loads
I've been trying to create a simple skeleton loader in Vuetify that is shown while the document's resources (like images) are being loaded and then hidden (replaced with a <v-card>) when everything is fully loaded.

Currently, I have this <v-skeleton-loader>:

```<code><v-skeleton-loader v-if="loading == true" :loading="loading" transition="fade-transition" type="card">
</v-skeleton-loader>
</code>```

My v-card element uses v-show like this:

```<code><v-card max-width="344" v-show="loaded">
</code>```

For reactive properties, I have:

```<code>data: () => ({
    loading: true,
    loaded: false
})
</code>```

I am trying to update the values of those properties using the updated lifecycle hook of Vue.js:

```<code>updated: function() {
    if (document.readyState == 'complete') {
        this.loading = false;
        this.loaded = true;
    }

}
</code>```

Currently, Vue Devtools tells me that the properties are not being updated with their new values, defined in the updated hook. How can I change that so the values are properly updated when the readyState of the DOM has changed to complete?

答:
You must observe document.readyState:</p>

```<code>mounted: function() {
  var that = this;

  if (document.readyState === 'complete')
    this.$set(that, 'loaded', true);
  else
    document.addEventListener('readystatechange', function() {
      if (document.readyState === 'complete')
        that.$set(that, 'loaded', true);
    })
}
</code>```

Body:
I've been trying to create a simple skeleton loader in Vuetify that is shown while the document's resources (like images) are being loaded and then hidden (replaced with a <v-card>) when everything is fully loaded.

Currently, I have this <v-skeleton-loader>:

<v-skeleton-loader v-if="loading == true" :loading="loading" transition="fade-transition" type="card">
</v-skeleton-loader>

My v-card element uses v-show like this:

<v-card max-width="344" v-show="loaded">

For reactive properties, I have:

data: () => ({
    loading: true,
    loaded: false
})

I am trying to update the values of those properties using the updated lifecycle hook of Vue.js:

updated: function() {
    if (document.readyState == 'complete') {
        this.loading = false;
        this.loaded = true;
    }

}

Currently, Vue Devtools tells me that the properties are not being updated with their new values, defined in the updated hook. How can I change that so the values are properly updated when the readyState of the DOM has changed to complete?

Body:
You must observe document.readyState:

mounted: function() {
  var that = this;

  if (document.readyState === 'complete')
    this.$set(that, 'loaded', true);
  else
    document.addEventListener('readystatechange', function() {
      if (document.readyState === 'complete')
        that.$set(that, 'loaded', true);
    })
}


问:
How to use python async def without awaiting on every parent function call
I was wondering if it possible to create async function without setting main as async.

I have a function call like this:

```<code>async def C:
    t = asyncio.create_subprocess_shell(command)
    ...
    await t

def B:
    asyncio.set_event_loop(asyncio.new_event_loop())
    C()
    <How to await for C?>

def A:
    B()
</code>```

I'm unable to await for C (either as a task or a future). The function exits immediately after starting C(). I've tried loop.create_task and loop.run_until_complete(task) but nothing seems to work.

I don't want to set all parent function calls as async up to main(). Is there a way to do this?

Edit:
My original problem is to run multiple shell commands in parallel (which start another application) from a python function and then wait for their results.

答:
The canonical way to use asyncio in modern Python (3.7 and later) is something like:

async def main():
    ... your code here ...

async.run(main())
```

<p>To do multiple things in parallel, you can use <code>asyncio.gather</code>. For example:</p>

<pre class="lang-py prettyprint-override"><code>async def main():
    proc1 = await asyncio.create_subprocess_shell(command1)
    proc2 = await asyncio.create_subprocess_shell(command2)

    # wait in parallel for both processes to finish and obtain their statuses
    status1, status2 = await asyncio.gather(proc1.wait(), proc2.wait())
</code>```

Body:
I was wondering if it possible to create async function without setting main as async.

I have a function call like this:

async def C:
    t = asyncio.create_subprocess_shell(command)
    ...
    await t

def B:
    asyncio.set_event_loop(asyncio.new_event_loop())
    C()
    <How to await for C?>

def A:
    B()

I'm unable to await for C (either as a task or a future). The function exits immediately after starting C(). I've tried loop.create_task and loop.run_until_complete(task) but nothing seems to work.

I don't want to set all parent function calls as async up to main(). Is there a way to do this?

Edit:
My original problem is to run multiple shell commands in parallel (which start another application) from a python function and then wait for their results.

Body:
The canonical way to use asyncio in modern Python (3.7 and later) is something like:

async def main():
    ... your code here ...

async.run(main())

To do multiple things in parallel, you can use asyncio.gather. For example:

async def main():
    proc1 = await asyncio.create_subprocess_shell(command1)
    proc2 = await asyncio.create_subprocess_shell(command2)

    # wait in parallel for both processes to finish and obtain their statuses
    status1, status2 = await asyncio.gather(proc1.wait(), proc2.wait())


问:
Velero installation in kubernetes server using azure provider
While installation of Velero on kubernetes using helm charts as below

```<code>helm install --namespace velero \
--set configuration.provider="Microsoft Azure" \
--set-file credentials.secretContents.cloud=<FULL PATH TO FILE> \
--set configuration.backupStorageLocation.name=azure \
--set configuration.backupStorageLocation.bucket=<BUCKET NAME> \
--set configuration.volumeSnapshotLocation.name=<PROVIDER NAME> \
--set configuration.volumeSnapshotLocation.config.region=<REGION> \
--set image.repository=velero/velero \
--set image.tag=v1.2.0 \
--set image.pullPolicy=IfNotPresent \
--set initContainers[0].name=velero-plugin-for-microsoft-azure:v1.0.0 \
--set initContainers[0].image=velero/velero-plugin-for-microsoft-azure:v1.0.0 \
--set initContainers[0].volumeMounts[0].mountPath=/target \
--set initContainers[0].volumeMounts[0].name=plugins \
stable/velero
</code>```

I have configured the below environment variables in credential-velero file and path has been provided in above command.

credentials-velero file -

```<code>AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID}
AZURE_TENANT_ID=${AZURE_TENANT_ID}
AZURE_CLIENT_ID=${AZURE_CLIENT_ID}
AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}
AZURE_RESOURCE_GROUP=${AZURE_RESOURCE_GROUP}
AZURE_CLOUD_NAME=AzurePublicCloud
</code>```

I am getting below error -

```<code>an error occurred: some backup storage locations are invalid: error getting backup store for location "default": rpc error: code = Unknown desc = unable to get all required environment variables: the following keys do not have values: AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_SUBSCRIPTION_ID
</code>```

Could you please help with resolution of above error?

答:
Your velero credential files should contain values for those, not placeholders.

```<code>cat << EOF  > ./credentials-velero
AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID}
AZURE_TENANT_ID=${AZURE_TENANT_ID}
AZURE_CLIENT_ID=${AZURE_CLIENT_ID}
AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}
AZURE_RESOURCE_GROUP=${AZURE_RESOURCE_GROUP}
AZURE_CLOUD_NAME=AzurePublicCloud
EOF
</code>```

https://github.com/vmware-tanzu/velero-plugin-for-microsoft-azure#setup

Body:
While installation of Velero on kubernetes using helm charts as below

helm install --namespace velero \
--set configuration.provider="Microsoft Azure" \
--set-file credentials.secretContents.cloud=<FULL PATH TO FILE> \
--set configuration.backupStorageLocation.name=azure \
--set configuration.backupStorageLocation.bucket=<BUCKET NAME> \
--set configuration.volumeSnapshotLocation.name=<PROVIDER NAME> \
--set configuration.volumeSnapshotLocation.config.region=<REGION> \
--set image.repository=velero/velero \
--set image.tag=v1.2.0 \
--set image.pullPolicy=IfNotPresent \
--set initContainers[0].name=velero-plugin-for-microsoft-azure:v1.0.0 \
--set initContainers[0].image=velero/velero-plugin-for-microsoft-azure:v1.0.0 \
--set initContainers[0].volumeMounts[0].mountPath=/target \
--set initContainers[0].volumeMounts[0].name=plugins \
stable/velero

I have configured the below environment variables in credential-velero file and path has been provided in above command.

credentials-velero file -

AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID}
AZURE_TENANT_ID=${AZURE_TENANT_ID}
AZURE_CLIENT_ID=${AZURE_CLIENT_ID}
AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}
AZURE_RESOURCE_GROUP=${AZURE_RESOURCE_GROUP}
AZURE_CLOUD_NAME=AzurePublicCloud

I am getting below error -

an error occurred: some backup storage locations are invalid: error getting backup store for location "default": rpc error: code = Unknown desc = unable to get all required environment variables: the following keys do not have values: AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_SUBSCRIPTION_ID

Could you please help with resolution of above error?

Body:
Your velero credential files should contain values for those, not placeholders.

cat << EOF  > ./credentials-velero
AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID}
AZURE_TENANT_ID=${AZURE_TENANT_ID}
AZURE_CLIENT_ID=${AZURE_CLIENT_ID}
AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}
AZURE_RESOURCE_GROUP=${AZURE_RESOURCE_GROUP}
AZURE_CLOUD_NAME=AzurePublicCloud
EOF

https://github.com/vmware-tanzu/velero-plugin-for-microsoft-azure#setup


问:
Powershell logging setup that supports files, console output and event logs
Is there a way to do logging in a Powershell script so that

a single call can be made to log a message

this message is then routed to none/any/multiple (depending on config) of the following

standard console output

a log file

windows event logs

Sort of like python's logging framework or log4j in Java, where the logging targets are configured separately from actual the logging of messages.

答:
You can use logger module or script.

```<code><#
.Synopsis
   Write-Log writes a message to a specified log file with the current time stamp.
.DESCRIPTION
   The Write-Log function is designed to add logging capability to other scripts.
   In addition to writing output and/or verbose you can write to a log file for
   later debugging.
   Changelog:
    * Code simplification and clarification
    * Added documentation.
    * Renamed LogPath parameter to Path to keep it standard.
    * Revised the Force switch to work as it should.

   To Do:
    * Add error handling if trying to create a log file in a inaccessible location.
    * Add ability to write $Message to $Verbose or $Error pipelines to eliminate
      duplicates.
.PARAMETER Message
   Message is the content that you wish to add to the log file.
.PARAMETER Path
   The path to the log file to which you would like to write. By default the function will
   create the path and file if it does not exist.
.PARAMETER Level
   Specify the criticality of the log information being written to the log (i.e. Error, Warning, Informational)
.PARAMETER NoClobber
   Use NoClobber if you do not wish to overwrite an existing file.
.EXAMPLE
   Write-Log -Message 'Log message'
   Writes the message to c:\Logs\PowerShellLog.log.
.EXAMPLE
   Write-Log -Message 'Restarting Server.' -Path c:\Logs\Scriptoutput.log
   Writes the content to the specified log file and creates the path and file specified.
.EXAMPLE
   Write-Log -Message 'Folder does not exist.' -Path c:\Logs\Script.log -Level Error
   Writes the message to the specified log file as an error message, and writes the message to the error pipeline.
#>
function Write-Log
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [ValidateNotNullOrEmpty()]
        [Alias("LogContent")]
        [string]$Message,

        [Parameter(Mandatory=$false)]
        [Alias('LogPath')]
        [string]$Path='C:\Logs\PowerShellLog.log',

        [Parameter(Mandatory=$false)]
        [ValidateSet("Error","Warn","Info")]
        [string]$Level="Info",

        [Parameter(Mandatory=$false)]
        [switch]$NoClobber
    )

    Begin
    {
        # Set VerbosePreference to Continue so that verbose messages are displayed.
        $VerbosePreference = 'Continue'
    }
    Process
    {

        # If the file already exists and NoClobber was specified, do not write to the log.
        if ((Test-Path $Path) -AND $NoClobber) {
            Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name."
            Return
            }

        # If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
        elseif (!(Test-Path $Path)) {
            Write-Verbose "Creating $Path."
            $NewLogFile = New-Item $Path -Force -ItemType File
            }

        else {
            # Nothing to see here yet.
            }

        # Format Date for our Log File
        $FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

        # Write message to error, warning, or verbose pipeline and specify $LevelText
        switch ($Level) {
            'Error' {
                Write-Error $Message
                $LevelText = 'ERROR:'
                }
            'Warn' {
                Write-Warning $Message
                $LevelText = 'WARNING:'
                }
            'Info' {
                Write-Verbose $Message
                $LevelText = 'INFO:'
                }
            }

        # Write log entry to $Path
        "$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append
    }
    End
    {
    }
}
</code>```

Then just call it out wherever you want in script :

```<code>Write-Log -Message "################ Script Started #############################" -Level Info
</code>```

and if you want to redirect the error then call it in catch block:

```<code>try{ }
catch
{
    {Write-Log -Message $_.Exception.Message -Level Error}
}
</code>```

Note: Make sure in that case you compile the function in the beginning, else PS will not be able to use it inside the blocks because the scope will vary.

This will give you console and file output and you can script to direct to eventlog even. Hope it helps.

Body:
You can use logger module or script.

<#
.Synopsis
   Write-Log writes a message to a specified log file with the current time stamp.
.DESCRIPTION
   The Write-Log function is designed to add logging capability to other scripts.
   In addition to writing output and/or verbose you can write to a log file for
   later debugging.
   Changelog:
    * Code simplification and clarification
    * Added documentation.
    * Renamed LogPath parameter to Path to keep it standard.
    * Revised the Force switch to work as it should.

   To Do:
    * Add error handling if trying to create a log file in a inaccessible location.
    * Add ability to write $Message to $Verbose or $Error pipelines to eliminate
      duplicates.
.PARAMETER Message
   Message is the content that you wish to add to the log file.
.PARAMETER Path
   The path to the log file to which you would like to write. By default the function will
   create the path and file if it does not exist.
.PARAMETER Level
   Specify the criticality of the log information being written to the log (i.e. Error, Warning, Informational)
.PARAMETER NoClobber
   Use NoClobber if you do not wish to overwrite an existing file.
.EXAMPLE
   Write-Log -Message 'Log message'
   Writes the message to c:\Logs\PowerShellLog.log.
.EXAMPLE
   Write-Log -Message 'Restarting Server.' -Path c:\Logs\Scriptoutput.log
   Writes the content to the specified log file and creates the path and file specified.
.EXAMPLE
   Write-Log -Message 'Folder does not exist.' -Path c:\Logs\Script.log -Level Error
   Writes the message to the specified log file as an error message, and writes the message to the error pipeline.
#>
function Write-Log
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [ValidateNotNullOrEmpty()]
        [Alias("LogContent")]
        [string]$Message,

        [Parameter(Mandatory=$false)]
        [Alias('LogPath')]
        [string]$Path='C:\Logs\PowerShellLog.log',

        [Parameter(Mandatory=$false)]
        [ValidateSet("Error","Warn","Info")]
        [string]$Level="Info",

        [Parameter(Mandatory=$false)]
        [switch]$NoClobber
    )

    Begin
    {
        # Set VerbosePreference to Continue so that verbose messages are displayed.
        $VerbosePreference = 'Continue'
    }
    Process
    {

        # If the file already exists and NoClobber was specified, do not write to the log.
        if ((Test-Path $Path) -AND $NoClobber) {
            Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name."
            Return
            }

        # If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
        elseif (!(Test-Path $Path)) {
            Write-Verbose "Creating $Path."
            $NewLogFile = New-Item $Path -Force -ItemType File
            }

        else {
            # Nothing to see here yet.
            }

        # Format Date for our Log File
        $FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

        # Write message to error, warning, or verbose pipeline and specify $LevelText
        switch ($Level) {
            'Error' {
                Write-Error $Message
                $LevelText = 'ERROR:'
                }
            'Warn' {
                Write-Warning $Message
                $LevelText = 'WARNING:'
                }
            'Info' {
                Write-Verbose $Message
                $LevelText = 'INFO:'
                }
            }

        # Write log entry to $Path
        "$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append
    }
    End
    {
    }
}

Then just call it out wherever you want in script :

Write-Log -Message "################ Script Started #############################" -Level Info

and if you want to redirect the error then call it in catch block:

try{ }
catch
{
    {Write-Log -Message $_.Exception.Message -Level Error}
}

Note: Make sure in that case you compile the function in the beginning, else PS will not be able to use it inside the blocks because the scope will vary.

This will give you console and file output and you can script to direct to eventlog even. Hope it helps.


问:
ASP.NET Core Scaffolding does not work in VS 2017
I have a beginner ASP.NET Core project in Visual Studio 2017, and I am at the scaffolding step of the HelloWorld. The Scaffolding does not work, I tested on a first computer, then at a second one...

when I try to generate a controller with views

, it gives the following error on the first machine:

Microsoft Visual Studio

Error

There was an error running the selected code generator:

'Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.Web.CodeGeneration.Utils, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
     at Microsoft.VisualStudio.Web.CodeGeneration.Design.Program.Main(String[] args)'

OK

On the second machine:

Microsoft Visual Studio

Error

There was an error running the selected code generator:

'Error: assembly specified in the dependencies manifest was not found -- package: 'microsoft.applicationinsights.aspnetcore', version: '2.1.0', path: 'lib/netstandard1.6/Microsoft.ApplicationInsights.AspNetCore.dll''

OK

答:
For the benefit of searchers:

I got this in VS2019. The cause was that my code was broken.

Didn't realise why to start, but when you realise that code generation is a nuget package; if the project doesn't build, then Visual Studio can't get to that package.

Once I got the code compiling, this error went away.


问:
Good alternative to Squirrel.Windows
Recently I implemented Squirrel.Windows in one of my programs. It worked fine for basic functionality.

Problem is that main methods are all internal and we are left out to use Extension methods or do our own implementation. Plus, Squirrel project has too many open issues, so I think it wont be a nice project to work with.

Is there any library with similar functionalities, but at the same time gives me more control and let me use the already-implemented code?

Needs to be able to work with WPF.

Cheers

答: Maybe late but AutoUpdater.NET is a good solution for this, it works well with both Winform and WPF </p>

Title: Good alternative to Squirrel.Windows


问:
How do I count the number of words (text) in an HTML source
I have some html documents for which I need to return the number of words in the document. This count should only include actual text (so no html tags e.g. html, br, etc).

Any ideas how to do this? Naturally, I would prefer to re-use some code.

Thanks,

Assaf

答:
I would add an extra step to Jigar's answer:

Parse out the document text using JSoup or Jericho or Dom4j

Tokenise the resulting text. This depends on your definition of a "word". It is unlikely to be as simple as splitting on white-space. And you'll need to deal with punctuation etc. So take a look at the various Tokeniser's available e.g from the Lucene or Stanford NLP projects. Here are some simple examples you will encounter:

"Today I'm going to New York!" - Is "I'm" one word or two? What about "New York"?

"We applied two meta-filters in the analysis" - Is "meta-filter" one word or two?

And what about badly formatted text, e.g missing of a space at the end of a sentence:

```<code>"So we went there.And on arrival..."
</code>```

Tokenising is tricky...

Iterate through your tokens and count them up, e.g using a HashMap.

Body:
I would add an extra step to Jigar's answer:

Parse out the document text using JSoup or Jericho or Dom4j

Tokenise the resulting text. This depends on your definition of a "word". It is unlikely to be as simple as splitting on white-space. And you'll need to deal with punctuation etc. So take a look at the various Tokeniser's available e.g from the Lucene or Stanford NLP projects. Here are some simple examples you will encounter:

"Today I'm going to New York!" - Is "I'm" one word or two? What about "New York"?

"We applied two meta-filters in the analysis" - Is "meta-filter" one word or two?

And what about badly formatted text, e.g missing of a space at the end of a sentence:

"So we went there.And on arrival..."

Tokenising is tricky...

Iterate through your tokens and count them up, e.g using a HashMap.


问:
Run-length decompression using C++
I have a text file with a string which I encoded.

Let's say it is: aaahhhhiii kkkjjhh ikl wwwwwweeeett

Here the code for encoding, which works perfectly fine:

```<code>void Encode(std::string &inputstring, std::string &outputstring)
{
    for (int i = 0; i < inputstring.length(); i++) {
        int count = 1;
        while (inputstring[i] == inputstring[i+1]) {
            count++;
            i++;
        }
        if(count <= 1) {
            outputstring += inputstring[i];
        } else {
            outputstring += std::to_string(count);
            outputstring += inputstring[i];
        }
    }
}
</code>```

Output is as expected: 3a4h3i 3k2j2h ikl 6w4e2t

Now, I'd like to decompress the output - back to original.

And I am struggling with this since a couple days now.

My idea so far:

```<code>void Decompress(std::string &compressed, std::string &original)
{
    char currentChar = 0;
    auto n = compressed.length();
    for(int i = 0; i < n; i++) {

        currentChar = compressed[i++];

        if(compressed[i] <= 1) {
            original += compressed[i];
        } else if (isalpha(currentChar)) {
            //
        } else {
            //
            int number = isnumber(currentChar).....
            original += number;
        }
    }
}
</code>```

I know my Decompress function seems a bit messy, but I am pretty lost with this one.
Sorry for that.

Maybe there is someone out there at stackoverflow who would like to help a lost and beginner soul.

Thanks for any help, I appreciate it.

答:
Assuming input strings cannot contain digits (this cannot be covered by your encoding as e. g. both the strings "3a" and "aaa" would result in the encoded string "3a" – how would you ever want to decompose again?) then you can decompress as follows:

```<code>unsigned int num = 0;
for(auto c : compressed)
{
    if(std::isdigit(static_cast<unsigned char>(c)))
    {
        num = num * 10 + c - '0';
    }
    else
    {
        num += num == 0; // assume you haven't read a digit yet!
        while(num--)
        {
            original += c;
        }
    }
}
</code>```

Untested code, though...

Characters in a string actually are only numerical values, though. You can consider char (or signed char, unsigned char) as ordinary 8-bit integers as well. And you can store a numerical value in such a byte, too. Usually, you do run length encoding exactly that way: Count up to 255 equal characters, store the count in a single byte and the character in another byte. One single "a" would then be encoded as 0x01 0x61 (the latter being the ASCII value of a), "aa" would get 0x02 0x61, and so on. If you have to store more than 255 equal characters you store two pairs: 0xff 0x61, 0x07 0x61 for a string containing 262 times the character a... Decoding then gets trivial: you read characters pairwise, first byte you interpret as number, second one as character – rest being trivial. And you nicely cover digits that way as well.

Title: Run-length decompression using C++

Body:
I have a text file with a string which I encoded.

Let's say it is: aaahhhhiii kkkjjhh ikl wwwwwweeeett

Here the code for encoding, which works perfectly fine:

void Encode(std::string &inputstring, std::string &outputstring)
{
    for (int i = 0; i < inputstring.length(); i++) {
        int count = 1;
        while (inputstring[i] == inputstring[i+1]) {
            count++;
            i++;
        }
        if(count <= 1) {
            outputstring += inputstring[i];
        } else {
            outputstring += std::to_string(count);
            outputstring += inputstring[i];
        }
    }
}

Output is as expected: 3a4h3i 3k2j2h ikl 6w4e2t

Now, I'd like to decompress the output - back to original.

And I am struggling with this since a couple days now.

My idea so far:

void Decompress(std::string &compressed, std::string &original)
{
    char currentChar = 0;
    auto n = compressed.length();
    for(int i = 0; i < n; i++) {

        currentChar = compressed[i++];

        if(compressed[i] <= 1) {
            original += compressed[i];
        } else if (isalpha(currentChar)) {
            //
        } else {
            //
            int number = isnumber(currentChar).....
            original += number;
        }
    }
}

I know my Decompress function seems a bit messy, but I am pretty lost with this one.
Sorry for that.

Maybe there is someone out there at stackoverflow who would like to help a lost and beginner soul.

Thanks for any help, I appreciate it.

Body:
Assuming input strings cannot contain digits (this cannot be covered by your encoding as e. g. both the strings "3a" and "aaa" would result in the encoded string "3a" – how would you ever want to decompose again?) then you can decompress as follows:

unsigned int num = 0;
for(auto c : compressed)
{
    if(std::isdigit(static_cast<unsigned char>(c)))
    {
        num = num * 10 + c - '0';
    }
    else
    {
        num += num == 0; // assume you haven't read a digit yet!
        while(num--)
        {
            original += c;
        }
    }
}

Untested code, though...

Characters in a string actually are only numerical values, though. You can consider char (or signed char, unsigned char) as ordinary 8-bit integers as well. And you can store a numerical value in such a byte, too. Usually, you do run length encoding exactly that way: Count up to 255 equal characters, store the count in a single byte and the character in another byte. One single "a" would then be encoded as 0x01 0x61 (the latter being the ASCII value of a), "aa" would get 0x02 0x61, and so on. If you have to store more than 255 equal characters you store two pairs: 0xff 0x61, 0x07 0x61 for a string containing 262 times the character a... Decoding then gets trivial: you read characters pairwise, first byte you interpret as number, second one as character – rest being trivial. And you nicely cover digits that way as well.


问:
What does a lock statement do under the hood?
I see that for using objects which are not thread safe we wrap the code with a lock like this:

```<code>private static readonly Object obj = new Object();

lock (obj)
{
    // thread unsafe code
}
</code>```

So, what happens when multiple threads access the same code (let's assume that it is running in a ASP.NET web application). Are they queued? If so how long will they wait?

What is the performance impact because of using locks?

答: The part within the lock statement can only be executed by one thread, so all other threads will wait indefinitely for it the thread holding the lock to finish. This can result in a so-called deadlock.</p>

Body:
I see that for using objects which are not thread safe we wrap the code with a lock like this:

private static readonly Object obj = new Object();

lock (obj)
{
    // thread unsafe code
}

So, what happens when multiple threads access the same code (let's assume that it is running in a ASP.NET web application). Are they queued? If so how long will they wait?

What is the performance impact because of using locks?


问:
Find password by username
I already have this code. Now I want to change the code, that when someone types in his username, he has to fill in the right password which belongs to the username.

```<code>import csv
        csvbestand='inlog.csv'
        csv = open('inlog.csv', 'r').read().split('\n')[1].split(';')
        username= input("Fill in your username: ")
        if username == "admin" or username in csv:
            print("Username found")
            break
        else:
            print("Username not found")

    while True:
        import csv
        csvbestand='inlog.csv'
        csv = open('inlog.csv', 'r').read().split('\n')[2].split(';')
        password = input("Fill in your password: ")
        if password == "admin" or password in csv:
            print("Congratiulations")
            break
        else:
            print("Password not right")
</code>```

So when the username is ''John'' then I want only the password which belongs to "John" as the right password.

答:
```<code>with open('Usernames.txt', 'r') as f:
   content = f.readlines()
   index = [x for x in range(len(content)) if password in content[x].lower()]
       index = (str(index)[1:-1])
       if index == '':
            print("user not found")
       else:
            index = (int(index))
            with open('passwords.txt', 'r') as d:
               d = d.readlines()
               f = (d[index]).strip()
               if password == f:
                    print("found password")
</code>```

If I wanted to use separate files(txt) to contain usernames and passwords I would do this. It grabs the line no. the username is on, then in parallel the password

Title: Find password by username

Body:
I already have this code. Now I want to change the code, that when someone types in his username, he has to fill in the right password which belongs to the username.

import csv
        csvbestand='inlog.csv'
        csv = open('inlog.csv', 'r').read().split('\n')[1].split(';')
        username= input("Fill in your username: ")
        if username == "admin" or username in csv:
            print("Username found")
            break
        else:
            print("Username not found")

    while True:
        import csv
        csvbestand='inlog.csv'
        csv = open('inlog.csv', 'r').read().split('\n')[2].split(';')
        password = input("Fill in your password: ")
        if password == "admin" or password in csv:
            print("Congratiulations")
            break
        else:
            print("Password not right")

So when the username is ''John'' then I want only the password which belongs to "John" as the right password.

Body:
with open('Usernames.txt', 'r') as f:
   content = f.readlines()
   index = [x for x in range(len(content)) if password in content[x].lower()]
       index = (str(index)[1:-1])
       if index == '':
            print("user not found")
       else:
            index = (int(index))
            with open('passwords.txt', 'r') as d:
               d = d.readlines()
               f = (d[index]).strip()
               if password == f:
                    print("found password")

If I wanted to use separate files(txt) to contain usernames and passwords I would do this. It grabs the line no. the username is on, then in parallel the password