问:
Django Rest Framework custom serializer data
I got a serializer response that looks like this:

```<code>"results": [
            {
                "property_name": "Villa",
                "username": "Ivan",
                "email": "ivan@mail.com",
                "start_date": "2022-01-05",
                "end_date": "2022-01-19"
            },
            {
                "property_name": "Villa",
                "username": "Ivan",
                "email": "ivan@mail.com",
                "start_date": "2022-02-16",
                "end_date": "2022-03-03"
            },
            {
                "property_name": "Villa",
                "username": "Ivan",
                "email": "ivan@mail.com",
                "start_date": "2022-02-11",
                "end_date": "2022-02-25"
            }
        ]
</code>```

I wonder is it possible for the serializer response to look like this:

```<code>[
  {
    name: 'Ivan',
    email: 'ivan@mail.com',
    stays: [
      {
        "start_date": "2022-01-05",
        "end_date": "2022-01-19",
        "property_name: Villa"
      },
      {
        "start_date": "2022-02-16",
        "end_date": "2022-03-03",
        "property_name: Villa"
      },
      {
        "start_date": "2022-02-11",
        "end_date": "2022-02-25",
        "property_name: Villa"
      },
    ]
  }
]
</code>```

This is my model:

```<code>class PropertyReservation(BaseModelFields):
    start_date = models.DateField()
    end_date = models.DateField()
    property = models.ForeignKey('property.Property', on_delete=models.CASCADE)
    user = models.ForeignKey('user.UserModel', on_delete=models.CASCADE)
</code>```

my serializer:

```<code>class PropertyReservationGuestSerializer(serializers.ModelSerializer):
    property_name = serializers.ReadOnlyField(source='property.name')
    username = serializers.ReadOnlyField(source='user.username')
    email = serializers.ReadOnlyField(source='user.email')

    class Meta:
        model = PropertyReservation
        fields = ('property_name', 'username',
                  'email', 'start_date', 'end_date')
</code>```

I tried a few things and cant manage the serializer response to look like one in the example above. This was the best answer I found on the internet Django RestFramework group by and still was not able to do it, on the first solution I got an error when I call the super() function: "super(type, obj): obj must be an instance or subtype of type". On the second solution when I try to achieve what I want with using a SerializerMethodField, I got an error "object has no attribute 'filter'".

Do you have any suggestions on how can I do this? Thanks for any help.

答:
There is a one way to get your stays with SerializerMethodField</p>
```<code>class StaySerializer(serializers.ModelSerializer):
    class Meta:
        model = PropertyReservation
        fields = ('start_date', 'end_date', 'property_name')

class PropertyReservationGuestSerializer(serializers.ModelSerializer):
    name = serializers.ReadOnlyField(source='user.get_full_name')
    email = serializers.ReadOnlyField(source='user.email')
    stays = serializers.SerializerMethodField()

    def get_stays(self, obj):
        stays = PropertyReservation.objects.filter(user__email=obj.email)
        return StaySerializer(stays, many=True).data

    class Meta:
        model = PropertyReservation
        fields = ('name', 'email', 'stays')
</code>```

Body:
I got a serializer response that looks like this:

"results": [
            {
                "property_name": "Villa",
                "username": "Ivan",
                "email": "ivan@mail.com",
                "start_date": "2022-01-05",
                "end_date": "2022-01-19"
            },
            {
                "property_name": "Villa",
                "username": "Ivan",
                "email": "ivan@mail.com",
                "start_date": "2022-02-16",
                "end_date": "2022-03-03"
            },
            {
                "property_name": "Villa",
                "username": "Ivan",
                "email": "ivan@mail.com",
                "start_date": "2022-02-11",
                "end_date": "2022-02-25"
            }
        ]

I wonder is it possible for the serializer response to look like this:

[
  {
    name: 'Ivan',
    email: 'ivan@mail.com',
    stays: [
      {
        "start_date": "2022-01-05",
        "end_date": "2022-01-19",
        "property_name: Villa"
      },
      {
        "start_date": "2022-02-16",
        "end_date": "2022-03-03",
        "property_name: Villa"
      },
      {
        "start_date": "2022-02-11",
        "end_date": "2022-02-25",
        "property_name: Villa"
      },
    ]
  }
]

This is my model:

class PropertyReservation(BaseModelFields):
    start_date = models.DateField()
    end_date = models.DateField()
    property = models.ForeignKey('property.Property', on_delete=models.CASCADE)
    user = models.ForeignKey('user.UserModel', on_delete=models.CASCADE)

my serializer:

class PropertyReservationGuestSerializer(serializers.ModelSerializer):
    property_name = serializers.ReadOnlyField(source='property.name')
    username = serializers.ReadOnlyField(source='user.username')
    email = serializers.ReadOnlyField(source='user.email')

    class Meta:
        model = PropertyReservation
        fields = ('property_name', 'username',
                  'email', 'start_date', 'end_date')

I tried a few things and cant manage the serializer response to look like one in the example above. This was the best answer I found on the internet Django RestFramework group by and still was not able to do it, on the first solution I got an error when I call the super() function: "super(type, obj): obj must be an instance or subtype of type". On the second solution when I try to achieve what I want with using a SerializerMethodField, I got an error "object has no attribute 'filter'".

Do you have any suggestions on how can I do this? Thanks for any help.

Body:
There is a one way to get your stays with SerializerMethodField

class StaySerializer(serializers.ModelSerializer):
    class Meta:
        model = PropertyReservation
        fields = ('start_date', 'end_date', 'property_name')

class PropertyReservationGuestSerializer(serializers.ModelSerializer):
    name = serializers.ReadOnlyField(source='user.get_full_name')
    email = serializers.ReadOnlyField(source='user.email')
    stays = serializers.SerializerMethodField()

    def get_stays(self, obj):
        stays = PropertyReservation.objects.filter(user__email=obj.email)
        return StaySerializer(stays, many=True).data

    class Meta:
        model = PropertyReservation
        fields = ('name', 'email', 'stays')


来源: stackexchange

问:
How can I remove startDestination attribute from navigation_main?
navigation_main file

```<code><?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@id/navigation_main_countries_fragment">

    <fragment
        android:id="@+id/navigation_main_countries_fragment"
        android:name="com.test.app.fragments.CountriesFragment"
        tools:layout="@layout/fragment_countries" />

    <fragment
        android:id="@+id/navigation_main_categories_fragment"
        android:name="com.test.app.fragments.CategoriesFragment"
        tools:layout="@layout/fragment_categories" />

</navigation>
</code>```

Is it possible to remove app:startDestination="@id/navigation_main_countries_fragment" from navigation_main file and do it programmatically? If the int x was 0 then run  countries_fragment otherwise categories_fragment.

I'm using Java language.

Please test your answer before posting it because many people posted their answers but unfortunately the answers do not work.

答: The navigation documentation explains it pretty well on Conditional Navigation using navigation components.</p>

ContentLicense: CC BY-SA 4.0

Body:
navigation_main file

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@id/navigation_main_countries_fragment">

    <fragment
        android:id="@+id/navigation_main_countries_fragment"
        android:name="com.test.app.fragments.CountriesFragment"
        tools:layout="@layout/fragment_countries" />

    <fragment
        android:id="@+id/navigation_main_categories_fragment"
        android:name="com.test.app.fragments.CategoriesFragment"
        tools:layout="@layout/fragment_categories" />

</navigation>

Is it possible to remove app:startDestination="@id/navigation_main_countries_fragment" from navigation_main file and do it programmatically? If the int x was 0 then run  countries_fragment otherwise categories_fragment.

I'm using Java language.

Please test your answer before posting it because many people posted their answers but unfortunately the answers do not work.


问:
two x-data in one div
I'm trying to learn AlpineJS with HTML. It's very easy to hide/show one div. But when I try to make 2 items in the same div (dis)appear it doesn't work for the second one. I'm assuming it's an easy fix but I can't really find the answer somewhere. Here's my code:

```<code><!DOCTYPE html>
<html lang="en">
<head>
    <script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
    <script src="https://cdn.tailwindcss.com"></script>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Testpage</title>
</head>
<body>
    <div
        x-data="{ isOpen: true }"
        x-data="{ isOpen2: true }">
        <button @click="isOpen = !isOpen" class="bg-slate-300 border-black border-4 mt-5">BUTTON 1</button>
        <br>
        <button @click="isOpen2 = !isOpen2" class="bg-slate-300 border-black border-4 mt-5 mb-5">BUTTON 2</button>
        <h1 x-show="isOpen">TEXT 1</h1>
        <h1 x-show="isOpen2">TEXT 2</h1>
    </div>
</body>
</html>
</code>```

Does anyone know how I can best make this work?

答:
Have you tried doing it like this:</p>
```<code><!DOCTYPE html>
<html lang="en">
<head>
    <script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
    <script src="https://cdn.tailwindcss.com"></script>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Testpage</title>
</head>
<body>
    <div
        x-data="{ isOpen: true, isOpen2: true }">
        <button @click="isOpen = !isOpen" class="bg-slate-300 border-black border-4 mt-5">BUTTON 1</button>
        <br>
        <button @click="isOpen2 = !isOpen2" class="bg-slate-300 border-black border-4 mt-5 mb-5">BUTTON 2</button>
        <h1 x-show="isOpen">TEXT 1</h1>
        <h1 x-show="isOpen2">TEXT 2</h1>
    </div>
</body>
</html>
</code>```

Title: two x-data in one div

Body:
I'm trying to learn AlpineJS with HTML. It's very easy to hide/show one div. But when I try to make 2 items in the same div (dis)appear it doesn't work for the second one. I'm assuming it's an easy fix but I can't really find the answer somewhere. Here's my code:

<!DOCTYPE html>

    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Testpage</title>

        <button @click="isOpen = !isOpen" class="bg-slate-300 border-black border-4 mt-5">BUTTON 1</button>

        <button @click="isOpen2 = !isOpen2" class="bg-slate-300 border-black border-4 mt-5 mb-5">BUTTON 2</button>

TEXT 1

TEXT 2

Does anyone know how I can best make this work?

Body:
Have you tried doing it like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
    <script src="https://cdn.tailwindcss.com"></script>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Testpage</title>
</head>
<body>
    <div
        x-data="{ isOpen: true, isOpen2: true }">
        <button @click="isOpen = !isOpen" class="bg-slate-300 border-black border-4 mt-5">BUTTON 1</button>
        <br>
        <button @click="isOpen2 = !isOpen2" class="bg-slate-300 border-black border-4 mt-5 mb-5">BUTTON 2</button>
        <h1 x-show="isOpen">TEXT 1</h1>
        <h1 x-show="isOpen2">TEXT 2</h1>
    </div>
</body>
</html>


问:
Find Neighbors in 2d array and Assign them to Cluster
I go a table with pixel X and Y coordinates and I need to find neighbor pixels in 8 directions and assign them to a cluster.

I already build myself a recursive function which turned out to be too slow for large tables. For simplicity I use quite often panda dataframes, which might be one reason.

However, I feel like I reinvented the wheel but I am working on this for 3 days now and it seems I am too newbie to figure out any numpy or scipy function to cope this problem. Has someone an idea to speed this up?
Thanks

My MWE is:

```<code>def findNeighborPixel(InIniX, InIniY, InIniIndex, InClusterID, InSearchlist, InpdTable):

    for (x,y) in InSearchlist:
        x = InIniX + x
        y = InIniY + y

        ## Check if X & Y & no ClusterID is True and retun to FindRow
        FindRow = InpdTable.loc[(InpdTable['X-Pos'] == x) & (InpdTable['Y-Pos'] == y) & (InpdTable['ClusterID'].isnull())]

        if len(FindRow.index) > 0:
            NewIndex = FindRow.first_valid_index()
            InpdTable.loc[InIniIndex, 'ClusterID'] = InClusterID
            InpdTable = findNeighborPixel(x, y, NewIndex, InClusterID, InSearchlist, InpdTable)

            ## Make Sure, the last found Position is marked with the current Cluster ID
            InpdTable.loc[NewIndex, 'ClusterID'] = InClusterID

    return InpdTable

data = pd.DataFrame({'X-Pos': [0,1,1,1,2,2,3,3,3,5,5,5,5,5,6,6],
                     'Y-Pos': [0,0,858,998,0,741,0,533,711,0,455,631,0,857,0,11],
                     'ClusterID': numpy.nan
                    })

searchlist = [
    (-1,+1), (+0,+1), (+1,+1),
    (-1,+0),          (+1,+0),
    (-1,-1), (+0,-1), (+1,-1),
    ]

ClusterID = 0

for IniIndex in range(0, len(data)):
    ## If the current row was not already assigned to a cluster
    if pd.isna(data.loc[IniIndex, 'ClusterID']) == True:
        ClusterID += 1

        IniX = data.loc[IniIndex, 'X-Pos']
        IniY = data.loc[IniIndex, 'Y-Pos']
        newData = findNeighborPixel(IniX, IniY, IniIndex, ClusterID, searchlist, data)
</code>```

答:
You may want to explore 'KMeans' from sklearn

Without claiming that this solves you problem, for example you can do the following:

```<code>from sklearn.cluster import KMeans
data = pd.DataFrame({'X-Pos': [0,1,1,1,2,2,3,3,3,5,5,5,5,5,6,6],
                     'Y-Pos': [0,0,858,998,0,741,0,533,711,0,455,631,0,857,0,11],
                     'ClusterID': np.nan
                    })

coords = data[['X-Pos','Y-Pos']].values
kmeans = KMeans(n_clusters=4, random_state=0).fit(coords)
data['ClusterID'] = kmeans.labels_
data.sort_values('ClusterID')
</code>```

output

```<code>      X-Pos    Y-Pos    ClusterID
--  -------  -------  -----------
 0        0        0            0
 1        1        0            0
 4        2        0            0
 6        3        0            0
 9        5        0            0
12        5        0            0
14        6        0            0
15        6       11            0
 5        2      741            1
 8        3      711            1
11        5      631            1
 7        3      533            2
10        5      455            2
 2        1      858            3
 3        1      998            3
13        5      857            3
</code>```

Body:
I go a table with pixel X and Y coordinates and I need to find neighbor pixels in 8 directions and assign them to a cluster.

I already build myself a recursive function which turned out to be too slow for large tables. For simplicity I use quite often panda dataframes, which might be one reason.

However, I feel like I reinvented the wheel but I am working on this for 3 days now and it seems I am too newbie to figure out any numpy or scipy function to cope this problem. Has someone an idea to speed this up?
Thanks

My MWE is:

def findNeighborPixel(InIniX, InIniY, InIniIndex, InClusterID, InSearchlist, InpdTable):

    for (x,y) in InSearchlist:
        x = InIniX + x
        y = InIniY + y

        ## Check if X & Y & no ClusterID is True and retun to FindRow
        FindRow = InpdTable.loc[(InpdTable['X-Pos'] == x) & (InpdTable['Y-Pos'] == y) & (InpdTable['ClusterID'].isnull())]

        if len(FindRow.index) > 0:
            NewIndex = FindRow.first_valid_index()
            InpdTable.loc[InIniIndex, 'ClusterID'] = InClusterID
            InpdTable = findNeighborPixel(x, y, NewIndex, InClusterID, InSearchlist, InpdTable)

            ## Make Sure, the last found Position is marked with the current Cluster ID
            InpdTable.loc[NewIndex, 'ClusterID'] = InClusterID

    return InpdTable

data = pd.DataFrame({'X-Pos': [0,1,1,1,2,2,3,3,3,5,5,5,5,5,6,6],
                     'Y-Pos': [0,0,858,998,0,741,0,533,711,0,455,631,0,857,0,11],
                     'ClusterID': numpy.nan
                    })

searchlist = [
    (-1,+1), (+0,+1), (+1,+1),
    (-1,+0),          (+1,+0),
    (-1,-1), (+0,-1), (+1,-1),
    ]

ClusterID = 0

for IniIndex in range(0, len(data)):
    ## If the current row was not already assigned to a cluster
    if pd.isna(data.loc[IniIndex, 'ClusterID']) == True:
        ClusterID += 1

        IniX = data.loc[IniIndex, 'X-Pos']
        IniY = data.loc[IniIndex, 'Y-Pos']
        newData = findNeighborPixel(IniX, IniY, IniIndex, ClusterID, searchlist, data)

Body:
You may want to explore 'KMeans' from sklearn

Without claiming that this solves you problem, for example you can do the following:

from sklearn.cluster import KMeans
data = pd.DataFrame({'X-Pos': [0,1,1,1,2,2,3,3,3,5,5,5,5,5,6,6],
                     'Y-Pos': [0,0,858,998,0,741,0,533,711,0,455,631,0,857,0,11],
                     'ClusterID': np.nan
                    })

coords = data[['X-Pos','Y-Pos']].values
kmeans = KMeans(n_clusters=4, random_state=0).fit(coords)
data['ClusterID'] = kmeans.labels_
data.sort_values('ClusterID')

output

      X-Pos    Y-Pos    ClusterID
--  -------  -------  -----------
 0        0        0            0
 1        1        0            0
 4        2        0            0
 6        3        0            0
 9        5        0            0
12        5        0            0
14        6        0            0
15        6       11            0
 5        2      741            1
 8        3      711            1
11        5      631            1
 7        3      533            2
10        5      455            2
 2        1      858            3
 3        1      998            3
13        5      857            3


问:
Javascript access to other page
Can I have access from javascript on one page to elements on other page the script has created before? Something like this:

```<code>var win=window.open("http://www.ftwars.com/battle",'myWindow');
win.document.getElementById('center').onClick();
</code>```

答: Simply yes - you can , via variable reference, you cannot reach reference if you not created it in your javascript</p>

ContentLicense: CC BY-SA 3.0

Title: Javascript access to other page

Body:
Can I have access from javascript on one page to elements on other page the script has created before? Something like this:

var win=window.open("http://www.ftwars.com/battle",'myWindow');
win.document.getElementById('center').onClick();


问:
Select pandas rows where column is a substring
I'd like to filter my dataframe such that only rows that have a column containing a substring of another string are selected. I know that the opposite can be done like this:

```<code>selection = df[df.str.contains(substring)]
</code>```

But how would I do it such that the substring is in the dataframe and it is compared to another string. What I've tried are

```<code>import pandas
a = pandas.DataFrame({"b":["foo","bar"]})
selection = a[a.b.str in "foot"] # should match first row
selection = a[a.b.str.isin("foot")] # should match first row
selection = a[a.b.str.isin("foobar")] # should match both rows
</code>```

but these won't work

答:
You can do this.</p>
```<code>import pandas as pd
a = pd.DataFrame({"b":["foo","bar"]})
selection = a[a.b.apply(lambda x: x in "foot")] # should match first row
selection = a[a.b.apply(lambda x: x in "foot")] # should match first row
selection = a[a.b.apply(lambda x: x in "foobar")] # should match both rows
</code>```

Body:
I'd like to filter my dataframe such that only rows that have a column containing a substring of another string are selected. I know that the opposite can be done like this:

selection = df[df.str.contains(substring)]

But how would I do it such that the substring is in the dataframe and it is compared to another string. What I've tried are

import pandas
a = pandas.DataFrame({"b":["foo","bar"]})
selection = a[a.b.str in "foot"] # should match first row
selection = a[a.b.str.isin("foot")] # should match first row
selection = a[a.b.str.isin("foobar")] # should match both rows

but these won't work

Body:
You can do this.

import pandas as pd
a = pd.DataFrame({"b":["foo","bar"]})
selection = a[a.b.apply(lambda x: x in "foot")] # should match first row
selection = a[a.b.apply(lambda x: x in "foot")] # should match first row
selection = a[a.b.apply(lambda x: x in "foobar")] # should match both rows


问:
Bigquery - Version Control Scheduled Queries
Right now I have scheduled queries via BQ interface. They work but do not scale or migrate very well (across dev and prod gcp projects). So I am trying to do scheduled queries in a way that is reproducible, scalable and migrate-able.

My queries are complicated and hence I am struggling with ', " and ''' to make it run via bq commands and schedule via github actions.

This is the query which is most complicated:

```<code>declare bq_last_id int64;
declare external_sql string;
set bq_last_id = (select max(id) from bq_dataset.bq_table);
set external_sql = '"select * from mysql_table where id > ('|| bq_last_id ||')"';
execute immediate 'select * from external_query("my-gcp-project.my-region.my-connection-name",'|| external_sql || ');'
</code>```

In total there are 20 something queries that have to scheduled. This one is the only one which is incremental or other are drop and recreate table again so they are not as complicated as this one.

WHAT I HAVE TRIED TILL NOW:

creating an on-demand query in BQ interface and then using bq mk command to run it with timestamp as a variable as shown in this answer. The problem with it is I still have to manually create the on-demand queries and I will have to do separately in dev and prod projects.

I am unable to find a way to create on-demand query using bq cmd.

I am unable to bq query to run the queries (that is not create bq scheduled queries at all). and then later schedule them via gihub action.

Any help with correct syntax or better suggestions to do this will be super helpful to me.

Thanks.

答:
The way I use to solve problems with scape characters when inserting queries in bq commands is using jq on shell and my queries on a file, as following:

create queries.sql file with your query script:

```<code>cat queries.sql
</code>```
```<code>declare bq_last_id int64;
declare external_sql string;
set bq_last_id = (select max(id) from bq_dataset.bq_table);
set external_sql = '"select * from mysql_table where id > ('|| bq_last_id ||')"';
execute immediate 'select * from external_query("my-gcp-project.my-region.my-connection-name",'|| external_sql || ');'
</code>```

Create the following script:
schedule_query.sh

#!/bin/bash
set -f #avoind * used as wildcard
json=$(jq -nc --arg query "$(<queries.sql)" '{ "query": $query }')

#adapt the command and param to work in your environment (destination, tables, etc...)
bq mk \
--transfer_config \
--target_dataset=mydataset \
--display_name='My Scheduled Query' \
--params="$json" \
--data_source=scheduled_query \
--service_account_name=abcdef-test-sa@abcdef-test.iam.gserviceaccount.com
```

Body:
Right now I have scheduled queries via BQ interface. They work but do not scale or migrate very well (across dev and prod gcp projects). So I am trying to do scheduled queries in a way that is reproducible, scalable and migrate-able.

My queries are complicated and hence I am struggling with ', " and ''' to make it run via bq commands and schedule via github actions.

This is the query which is most complicated:

declare bq_last_id int64;
declare external_sql string;
set bq_last_id = (select max(id) from bq_dataset.bq_table);
set external_sql = '"select * from mysql_table where id > ('|| bq_last_id ||')"';
execute immediate 'select * from external_query("my-gcp-project.my-region.my-connection-name",'|| external_sql || ');'

In total there are 20 something queries that have to scheduled. This one is the only one which is incremental or other are drop and recreate table again so they are not as complicated as this one.

WHAT I HAVE TRIED TILL NOW:

creating an on-demand query in BQ interface and then using bq mk command to run it with timestamp as a variable as shown in this answer. The problem with it is I still have to manually create the on-demand queries and I will have to do separately in dev and prod projects.

I am unable to find a way to create on-demand query using bq cmd.

I am unable to bq query to run the queries (that is not create bq scheduled queries at all). and then later schedule them via gihub action.

Any help with correct syntax or better suggestions to do this will be super helpful to me.

Thanks.

Body:
The way I use to solve problems with scape characters when inserting queries in bq commands is using jq on shell and my queries on a file, as following:

create queries.sql file with your query script:

cat queries.sql

declare bq_last_id int64;
declare external_sql string;
set bq_last_id = (select max(id) from bq_dataset.bq_table);
set external_sql = '"select * from mysql_table where id > ('|| bq_last_id ||')"';
execute immediate 'select * from external_query("my-gcp-project.my-region.my-connection-name",'|| external_sql || ');'

Create the following script:
schedule_query.sh

#!/bin/bash
set -f #avoind * used as wildcard
json=$(jq -nc --arg query "$(<queries.sql)" '{ "query": $query }')

#adapt the command and param to work in your environment (destination, tables, etc...)
bq mk \
--transfer_config \
--target_dataset=mydataset \
--display_name='My Scheduled Query' \
--params="$json" \
--data_source=scheduled_query \
--service_account_name=abcdef-test-sa@abcdef-test.iam.gserviceaccount.com


问:
what is the best way to check if a string is date value?
Reflection does not seem to be working for time. What is the best approach here?</p>
```<code>package main

import (
    "fmt"
    "reflect"
    "time"
)

func main() {
    stringDate := "07/26/2020"

    // parse string date to golang time
    t, _ := time.Parse("01/02/2006", stringDate)

    ts := ""

    ts = t.String()

    v := reflect.ValueOf(ts)

    fmt.Println(ts) // prints "2020-07-26 00:00:00 +0000 UTC"

    fmt.Println(v.Type()) //  prints "string". How do I get this to time.Time ?

}
</code>```

答:
When you use time.Parse you are checking already if the stringDate is a valid date and you are parsing it to time.Time. What you really need is to check for err when parsing the date.

Example:

```<code>stringDate := "not a date"

// parse string date to golang time
t, err := time.Parse("01/02/2006", stringDate)
if err != nil {
    fmt.Println(err)
    return
}

fmt.Println("Time: ", t, "Type of t: ", reflect.ValueOf(t).Type())
</code>```

Which would print out

```<code>parsing time "not a date" as "01/02/2006": cannot parse "not a date" as "01"
</code>```

But providing a valid date will result in printing out:

```<code>Time:  2012-03-07 00:00:00 +0000 UTC Type of t:  time.Time
</code>```

The working example you can find here

Body:
Reflection does not seem to be working for time. What is the best approach here?

package main

import (
    "fmt"
    "reflect"
    "time"
)

func main() {
    stringDate := "07/26/2020"

    // parse string date to golang time
    t, _ := time.Parse("01/02/2006", stringDate)

    ts := ""

    ts = t.String()

    v := reflect.ValueOf(ts)

    fmt.Println(ts) // prints "2020-07-26 00:00:00 +0000 UTC"

    fmt.Println(v.Type()) //  prints "string". How do I get this to time.Time ?

}

Body:
When you use time.Parse you are checking already if the stringDate is a valid date and you are parsing it to time.Time. What you really need is to check for err when parsing the date.

Example:

stringDate := "not a date"

// parse string date to golang time
t, err := time.Parse("01/02/2006", stringDate)
if err != nil {
    fmt.Println(err)
    return
}

fmt.Println("Time: ", t, "Type of t: ", reflect.ValueOf(t).Type())

Which would print out

parsing time "not a date" as "01/02/2006": cannot parse "not a date" as "01"

But providing a valid date will result in printing out:

Time:  2012-03-07 00:00:00 +0000 UTC Type of t:  time.Time

The working example you can find here


问:
Is it possible to implement JsCast for a struct defined in Rust?
The documentation for JsCast states that JsCast is implemented for any type defined inside a #[wasm_bindgen] extern block, i.e., any JS struct that has been imported into Rust. I would like to know if it is possible to implement JsCast for structs defined in Rust and exported to JS. As an example, consider the following code snippet:

#[wasm_bindgen]
struct MyStruct;

let ms = MyStruct;
let msjsv: JsValue = ms.into(); // (1)
let ms = JsCast::unchecked_ref::<MyStruct>(&msjsv); // (2)
```

There are two things that I am confused about here. To start with, I would like to know what has happened after (1), e.g., I guess MyStruct still resides in the WebAssembly module's memory (in principle, I know it is a ZST), but I guess it's lifetime is now managed by JS...?

Secondly, I would like to know why (2) doesn't work, i.e., why MyStruct doesn't implement JsCast. It seems logical that I should be able to get a reference to MyStruct again. Should I manually implement JsCast here, or is there a reason why this hasn't been automatically provided?

答:
I feel @Masklinn answered the second part of the question in the comments above, that is, this is a requested feature that is being discussed on Github but is not implemented yet.

For the first part of the question, regarding the use of into to convert a Rust struct into JsValue, I think I have found the answer from studying the output of wasm-bindgen.

What seems to happen is that the structure on the Rust side is moved onto the heap and a raw pointer to it is returned to wasm-bindgen's glue code, this raw pointer is stored inside a JS version of the struct. Once all references to the JS struct are gone, the garbage collector will free the memory, at which point, a hook is executed to also free the memory on the Rust side. In a sense, Rust has forgotten about this memory and relies on Javascript's garbage collector to free and trigger the deallocation at some point in the future.

Body:
The documentation for JsCast states that JsCast is implemented for any type defined inside a #[wasm_bindgen] extern block, i.e., any JS struct that has been imported into Rust. I would like to know if it is possible to implement JsCast for structs defined in Rust and exported to JS. As an example, consider the following code snippet:

#[wasm_bindgen]
struct MyStruct;

let ms = MyStruct;
let msjsv: JsValue = ms.into(); // (1)
let ms = JsCast::unchecked_ref::<MyStruct>(&msjsv); // (2)

There are two things that I am confused about here. To start with, I would like to know what has happened after (1), e.g., I guess MyStruct still resides in the WebAssembly module's memory (in principle, I know it is a ZST), but I guess it's lifetime is now managed by JS...?

Secondly, I would like to know why (2) doesn't work, i.e., why MyStruct doesn't implement JsCast. It seems logical that I should be able to get a reference to MyStruct again. Should I manually implement JsCast here, or is there a reason why this hasn't been automatically provided?


问:
Keep items centered with interfering elements
I have a navbar which basically has one child div that serves as a list, and then these three options (potentially more in the future)

I want to ask how I can add a logo (img) at the left of this navbar, while maintaining the #navbar-options in the center, and all its items, cause if I try to add the image now, it will push the navbar options to off to the right depending on the image

```<code><div id="navbar">
    <div id="navbar-options">
        <div class="navbar-option"><a class="navbar-option-link" href="/">Home</a></div>
        <div class="navbar-option"><a class="navbar-option-link" href="/newsfeed">Newsfeed</a></div>
        <div class="navbar-option"><a class="navbar-option-link" href="/societies">Societies</a></div>
    </div>
</div>
</code>```

This is all the relevant CSS

```<code>#navbar {
    width: calc(100vw - 0);
    max-width: 100%;
    min-height: 2.5vw;
    background-color: var(--tertiary-clr);
    color: white;
    display: flex;
}

#navbar-options {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-evenly;
    align-items: center;
    margin: 0 auto;
}

.navbar-option {
    font-size: 1.5rem;
    padding: 1rem;
}
</code>```

This is the navbar currently, and an arrow indicating where I would like the image, without affecting the current position of the navbar options

答:
#navbar {
    width: calc(100vw - 0);
    max-width: 100%;
    min-height: 2.5vw;
    background-color: red;
    color: white;
    display: flex;
    position: relative;
}

#navbar-options {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-evenly;
    align-items: center;
    margin: 0 auto;
}

.navbar-option {
    font-size: 1.5rem;
    padding: 1rem;
}

img.logo {
    position: absolute;
    height: 100%;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="navbar">
    <img class="logo" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Stack_Overflow_icon.svg/768px-Stack_Overflow_icon.svg.png" />
    <div id="navbar-options">
        <div class="navbar-option">Home</div>
        <div class="navbar-option">Newsfeed</div>
        <div class="navbar-option">Societies</div>
    </div>
</div></code>```

By saying "without affecting the current position of the navbar options", meaning you want the "links" to be placed centered while ignoring the logo right?
You can achieve it using the snippet above, do note that the important part on the snippet to make it ignore the logo is by:

Add position relative to the #navbar

Add position absolute to the .logo

So the logo will be positioned based on the navbar, you can improve the spacing/padding by changing the logo to a div first with padding, and put the image inside that div

Of course I believe there's many ways to achieve what you want, you can fiddle with the code and other properties like "calc", "translate" and other stuff.

Body:
I have a navbar which basically has one child div that serves as a list, and then these three options (potentially more in the future)

I want to ask how I can add a logo (img) at the left of this navbar, while maintaining the #navbar-options in the center, and all its items, cause if I try to add the image now, it will push the navbar options to off to the right depending on the image

Home

Newsfeed

Societies

This is all the relevant CSS

#navbar {
    width: calc(100vw - 0);
    max-width: 100%;
    min-height: 2.5vw;
    background-color: var(--tertiary-clr);
    color: white;
    display: flex;
}

#navbar-options {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-evenly;
    align-items: center;
    margin: 0 auto;
}

.navbar-option {
    font-size: 1.5rem;
    padding: 1rem;
}

This is the navbar currently, and an arrow indicating where I would like the image, without affecting the current position of the navbar options

Body:
#navbar {
    width: calc(100vw - 0);
    max-width: 100%;
    min-height: 2.5vw;
    background-color: red;
    color: white;
    display: flex;
    position: relative;
}

#navbar-options {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-evenly;
    align-items: center;
    margin: 0 auto;
}

.navbar-option {
    font-size: 1.5rem;
    padding: 1rem;
}

img.logo {
    position: absolute;
    height: 100%;
}

<div id="navbar">
    <img class="logo" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Stack_Overflow_icon.svg/768px-Stack_Overflow_icon.svg.png" />
    <div id="navbar-options">
        <div class="navbar-option"><a class="navbar-option-link" href="/">Home</a></div>
        <div class="navbar-option"><a class="navbar-option-link" href="/newsfeed">Newsfeed</a></div>
        <div class="navbar-option"><a class="navbar-option-link" href="/societies">Societies</a></div>
    </div>
</div>

By saying "without affecting the current position of the navbar options", meaning you want the "links" to be placed centered while ignoring the logo right?
You can achieve it using the snippet above, do note that the important part on the snippet to make it ignore the logo is by:

Add position relative to the #navbar

Add position absolute to the .logo

So the logo will be positioned based on the navbar, you can improve the spacing/padding by changing the logo to a div first with padding, and put the image inside that div

Of course I believe there's many ways to achieve what you want, you can fiddle with the code and other properties like "calc", "translate" and other stuff.


问:
How filter specific rows in a df
I want to select each row which have a list values in a geometry column.

Some rows have a empty list and I want to remove them and just get the rows with a list of geometry values...

I can do that with numeric values with dplyr::filter but I don't know do that with a list...

Someone can help me please ?

答:
Without your data it will be hard to exactly answer your question, and depending on if your list element is NA, NULL, or length 0 (i.e., character(0), integer(0)). A couple of options you could try:

```<code># List with both NA and NULL values
lst <- list(c(1:5), c(LETTERS[1:18]), c(NA), c(NULL), integer(), character() ,c(1:17))

# Remove if list element is NULL
plyr::compact(lst)

# Remove if list element is NA
Filter(Negate(anyNA), lst)

# If element is zero length as character(0), integer(0)
lst[lapply(lst, length) > 0]
</code>```

Title: How filter specific rows in a df

Body:
Without your data it will be hard to exactly answer your question, and depending on if your list element is NA, NULL, or length 0 (i.e., character(0), integer(0)). A couple of options you could try:

# List with both NA and NULL values
lst <- list(c(1:5), c(LETTERS[1:18]), c(NA), c(NULL), integer(), character() ,c(1:17))

# Remove if list element is NULL
plyr::compact(lst)

# Remove if list element is NA
Filter(Negate(anyNA), lst)

# If element is zero length as character(0), integer(0)
lst[lapply(lst, length) > 0]


问:
Direct Table Access vs Stored Procedures in n-tier arcitecture

Possible Duplicate:

  What are the pros and cons to keeping SQL in Stored Procs versus Code

What is the drawbacks and benifits of using direct table access vs stored procedures to access data components

I am currently designing an application in an n-tier enviroment.

Also in regard with:
Security
Performance
application deployment issues

Which of the two methods is recommended

答:
Generally SPs are better. But that depends on your requirement. If you are going to stick with a specific DBMS them there are many advantages of SPs.

Look here for more details.


问:
How to prevent duplicate installation of Miniconda when used in a Wix bundle?
I am working on a .NET application (and its installer) that calls python scripts. In order to do that, not just a the application but also Miniconda needs to be installed. However, when there is an update of the application, Miniconda should not be installed again when it is already available.

I tried to achieve that but I do not succeed. Here is my Bundle.wxs (I am using Wix 3.11).

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:bal="http://schemas.microsoft.com/wix/BalExtension"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">

    <?include ..\InstallPlugin\Forwards.wxi?>

    <Bundle
        Name="$(var.ProductTitle)"
        Version="$(var.ProductVersion)"
        Manufacturer="$(var.ProductCompany)"
        Copyright="$(var.ProductCopyright)"
        DisableModify="yes"
        DisableRemove="yes"
        IconSourceFile="../Images/Icon.ico"
        UpgradeCode="f8b6f347-2832-41c9-9d89-112144f62ed8"
        >

        <BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.HyperlinkSidebarLicense">
            <Payload SourceFile="..\Deployment\Bootstrapper\bin\Release\net462\Bootstrapper.dll"/>
            <Payload Name="BootstrapperCore.config" SourceFile="..\Deployment\Bootstrapper\Bootstrapper.BootstrapperCore.config"/>
            <Payload Name="en-US\License.rtf" SourceFile="../Licenses/License.en-US.rtf"/>
            <Payload SourceFile="../Images/Logo.png"/>
            <bal:WixStandardBootstrapperApplication
                LicenseUrl="https://SOMETHING"
                ShowVersion="yes" />
        </BootstrapperApplicationRef>

        <Chain>
            <PackageGroupRef Id="PluginSystem" />
            <MsiPackage SourceFile="..\InstallPlugin\bin\Release\en-US\InstallPlugin.msi"
                        Compressed="yes"
                        DisplayInternalUI="no"
                        Vital="yes"
                        Visible="yes"
            >

                <MsiProperty Name="USERLANGUAGE" Value="en-US"/>
            </MsiPackage>
            <ExePackage Id="MiniConda"
                        SourceFile="C:\\AnaCondaDependencies\\Miniconda3-py39_4.10.3-Windows-x86_64.exe"
                        DetectCondition="CondaFileFound"
                >
                <ExitCode Value ="3010" Behavior="success" />
            </ExePackage>
        </Chain>
    </Bundle>

    <Fragment>
    <util:RegistrySearchRef Id="CondaFound" />
    <util:FileSearchRef Id="CondaFileFound" />
      <util:RegistrySearch
       Id="CondaFound"
       Root="HKLM" Key="SOFTWARE\Python\ContinuumAnalytics\SupportUrl" Value="https://github.com/continuumio/anaconda-issues"
       Result="exists" Variable="CondaFound" />

     <util:FileSearch
       Id="CondaFileFound"
       Path="[CommonAppDataFolder]Miniconda3\python39.dll"
       Variable="CondaFileFound"
       Result="exists" />
    </Fragment>
</Wix>

```

As becomes clear from the .wxs file shown above, I defined two conditions: CondaFileFound and CondaFound to be used as a DetectCondition.  Both are not working for me. The installer just starts the Miniconda installer even when Miniconda is already installed and my condition checks for this. This is really frustrating. What am I doing wrong? And how can I fix this?

答:
as Bob already mentioned you are 'ref'ing your RegistrySeach and FileSearch function calls in the very same Fragment as they are defined. This does not execute them from your Bundle.

You have to move your RegistrySearchRef and FileSearchRef up into the Bundle definition.

See the test bundle below.

```<code><Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <Bundle Name="Bootstrapper1" Version="1.0.0.0" Manufacturer="cccc" UpgradeCode="b8a7411a-68dc-40a9-9557-d7f64c2e7940">
    <BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.RtfLicense" />
    <util:FileSearchRef Id="searchCmd"/>
    <Chain>
      <ExePackage SourceFile="c:\temp\cmd.exe"  DetectCondition="detected"/>
    </Chain>
  </Bundle>
  <Fragment>
    <util:FileSearch
           Id="searchCmd"
           Path="[WindowsVolume]temp\cmd.exe"
           Variable="detected"
            />
  </Fragment>
</Wix>
</code>```

Body:
I am working on a .NET application (and its installer) that calls python scripts. In order to do that, not just a the application but also Miniconda needs to be installed. However, when there is an update of the application, Miniconda should not be installed again when it is already available.

I tried to achieve that but I do not succeed. Here is my Bundle.wxs (I am using Wix 3.11).

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:bal="http://schemas.microsoft.com/wix/BalExtension"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">

    <?include ..\InstallPlugin\Forwards.wxi?>

    <Bundle
        Name="$(var.ProductTitle)"
        Version="$(var.ProductVersion)"
        Manufacturer="$(var.ProductCompany)"
        Copyright="$(var.ProductCopyright)"
        DisableModify="yes"
        DisableRemove="yes"
        IconSourceFile="../Images/Icon.ico"
        UpgradeCode="f8b6f347-2832-41c9-9d89-112144f62ed8"
        >

        <BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.HyperlinkSidebarLicense">
            <Payload SourceFile="..\Deployment\Bootstrapper\bin\Release\net462\Bootstrapper.dll"/>
            <Payload Name="BootstrapperCore.config" SourceFile="..\Deployment\Bootstrapper\Bootstrapper.BootstrapperCore.config"/>
            <Payload Name="en-US\License.rtf" SourceFile="../Licenses/License.en-US.rtf"/>
            <Payload SourceFile="../Images/Logo.png"/>
            <bal:WixStandardBootstrapperApplication
                LicenseUrl="https://SOMETHING"
                ShowVersion="yes" />
        </BootstrapperApplicationRef>

        <Chain>
            <PackageGroupRef Id="PluginSystem" />
            <MsiPackage SourceFile="..\InstallPlugin\bin\Release\en-US\InstallPlugin.msi"
                        Compressed="yes"
                        DisplayInternalUI="no"
                        Vital="yes"
                        Visible="yes"
            >

                <MsiProperty Name="USERLANGUAGE" Value="en-US"/>
            </MsiPackage>
            <ExePackage Id="MiniConda"
                        SourceFile="C:\\AnaCondaDependencies\\Miniconda3-py39_4.10.3-Windows-x86_64.exe"
                        DetectCondition="CondaFileFound"
                >
                <ExitCode Value ="3010" Behavior="success" />
            </ExePackage>
        </Chain>
    </Bundle>

    <Fragment>
    <util:RegistrySearchRef Id="CondaFound" />
    <util:FileSearchRef Id="CondaFileFound" />
      <util:RegistrySearch
       Id="CondaFound"
       Root="HKLM" Key="SOFTWARE\Python\ContinuumAnalytics\SupportUrl" Value="https://github.com/continuumio/anaconda-issues"
       Result="exists" Variable="CondaFound" />

     <util:FileSearch
       Id="CondaFileFound"
       Path="[CommonAppDataFolder]Miniconda3\python39.dll"
       Variable="CondaFileFound"
       Result="exists" />
    </Fragment>
</Wix>

As becomes clear from the .wxs file shown above, I defined two conditions: CondaFileFound and CondaFound to be used as a DetectCondition.  Both are not working for me. The installer just starts the Miniconda installer even when Miniconda is already installed and my condition checks for this. This is really frustrating. What am I doing wrong? And how can I fix this?

Body:
as Bob already mentioned you are 'ref'ing your RegistrySeach and FileSearch function calls in the very same Fragment as they are defined. This does not execute them from your Bundle.

You have to move your RegistrySearchRef and FileSearchRef up into the Bundle definition.

See the test bundle below.

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <Bundle Name="Bootstrapper1" Version="1.0.0.0" Manufacturer="cccc" UpgradeCode="b8a7411a-68dc-40a9-9557-d7f64c2e7940">
    <BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.RtfLicense" />
    <util:FileSearchRef Id="searchCmd"/>
    <Chain>
      <ExePackage SourceFile="c:\temp\cmd.exe"  DetectCondition="detected"/>
    </Chain>
  </Bundle>
  <Fragment>
    <util:FileSearch
           Id="searchCmd"
           Path="[WindowsVolume]temp\cmd.exe"
           Variable="detected"
            />
  </Fragment>
</Wix>


问:
How To Decrypt a .p7m File via Python Openssl
I have a python program, that iterates over emails and download the Attachment files.
Now I want to procsess the content of said emails.

I have a key like this "c34pj34365g0394hg" and a smime_post.key file with content like this:

```<code>-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIJrTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIeHcIC+7nTU4CAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBC/DGRNkgo3TizIHH3TWnHqBIIJ
UPu99JJuhB6hcHYS5UxgACXAdO6xOmXV7C1JUduGmTfNin0YwkJN7SMG6nOhGuxN
6BWczg5ENf1b3btIB6LwxyCFY+O/lvgzXMWrOr6/0XEQt3p7WTqOxbZ94nEcki1u
vZT9QdtveNAoZrZ48RyY1uoo2l3GLbSumSKaHx88lGLecMtnbUhFbwfE6Z6l6msK
VIi6kq+g1qiFD62xenyyS20UHkTgPy8CsJdllDDfU+aPxkOzTmfAdEHeIUzYwNPu
</code>```

and on top of that of course the smime.p7m file with the encryptet content.
I'm not able to finde something usefull to this topic.

I had a similar problem with another email import (from italy) where I was able to encrypt the file with this code:

```<code>def remove_signature(fileString):
    p7 = crypto.load_pkcs7_data(crypto.FILETYPE_ASN1, fileString)
    bio_out = crypto._new_mem_buf()
    res = _lib.PKCS7_verify(p7._pkcs7, _ffi.NULL, _ffi.NULL, _ffi.NULL, bio_out,
                            _lib.PKCS7_NOVERIFY | _lib.PKCS7_NOSIGS)

    if res == 1:
        return (crypto._bio_to_string(bio_out).decode('UTF-8'))
    else:
        errno = _lib.ERR_get_error()
        errstrlib = _ffi.string(_lib.ERR_lib_error_string(errno))
        errstrfunc = _ffi.string(_lib.ERR_func_error_string(errno))
        errstrreason = _ffi.string(_lib.ERR_reason_error_string(errno))
        print(f"------------------------------------\berrstrreason: {errstrreason}---------------------------\b")
        print(f"------------------------------------\berrno: {errno}---------------------------\b")
        print(f"------------------------------------\berrstrlib: {errstrlib}---------------------------\b")
        print(f"------------------------------------\berrstrfunc: {errstrfunc}---------------------------\b")
        return ""
</code>```

My guess is, that I have to decrypt the .key file and use that as a cert?
If someone has some experience with that, I would appreciate any help.

答:
Tock me some time, but I got my problem solved.
We changed the encryption to pgp and here is how I generate my keys / encrypt / decrypt files / blobs (i.e. a mailattachment)

```<code>import pgpy
from pgpy.constants import PubKeyAlgorithm, KeyFlags, HashAlgorithm, SymmetricKeyAlgorithm, CompressionAlgorithm

 class PgpDecryptor:
    """
    A class that can encrypt / decrypt files with pgp encryption.
    """
    # in die config packen?
    filename_key = "key.pem"

    def __init__(self):
        self.key = None
        self.message = None
        self.encrypted_message = None
        self.filename_org = None
    self.filename_encrypted_PGPMessage = None
    self.passphrase = None

def generate_pgp_key(self, filename_key, passphrase=None):
    """
    Generates a new pgp key pair with a passphrase. T
    :param filename_key: The path for the key storage
    :param passphrase: The passphrase to lock / unlock the private key
    """
    self.key = pgpy.PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 4096)
    uid = pgpy.PGPUID.new('Martin Kraemer', email='martin.kraemer@sixt.com')

    # All diese Verschluesselungstechniken / usages usw. sind moeglich (keine Preferenz)
    # Man kann ebenfalls nen expiration date mitgeben (in etwa so: key_expiration=timedelta(days=365))
    self.key.add_uid(uid, usage={KeyFlags.Sign, KeyFlags.EncryptCommunications, KeyFlags.EncryptStorage},
                     hashes=[HashAlgorithm.SHA256, HashAlgorithm.SHA384, HashAlgorithm.SHA512,
                             HashAlgorithm.SHA224],
                     ciphers=[SymmetricKeyAlgorithm.AES256, SymmetricKeyAlgorithm.AES192,
                              SymmetricKeyAlgorithm.AES128],
                     compression=[CompressionAlgorithm.ZLIB, CompressionAlgorithm.BZ2, CompressionAlgorithm.ZIP,
                                  CompressionAlgorithm.Uncompressed])

    # protect a key with a phrase like "Schnarr"
    # this phrase is needed to use the private key
    if passphrase:
        self.key.protect(passphrase, SymmetricKeyAlgorithm.AES256, HashAlgorithm.SHA256)
    save_to_file(filename_key, str(self.key), mode="w")
    save_to_file(f"pub_{filename_key}", str(self.key.pubkey), mode="w")

def encrypt_file(self, file_to_encrypt=None, filename_encrypted_file=None, key_path=None):
    """
    Encrypts a file with a pgp key.
    :param file_to_encrypt: The path to the file we want to encrypt
    :param filename_encrypted_file: The path to the file we've encrypted
    :poram key_path: Sets the path to an specific key, otherwise the key in the same folder (key.pem) is used
    """
    if file_to_encrypt:
        self.filename_org = file_to_encrypt
    if filename_encrypted_file:
        self.filename_encrypted_PGPMessage = filename_encrypted_file

    self.__get_message()
    self.key = self.__get_pgp_key(key_path)
    self.encrypted_message = self.key.pubkey.encrypt(self.message)
    save_to_file(self.filename_encrypted_PGPMessage, str(self.encrypted_message), mode="w")

def decrypt_blob(self, blob_to_decrypt, filename_decrypted_file, key_path=None, passphrase=None):
    """
    Decrypts an blob that is pgp encrypted, using the private we've already created and saves the result
    to the given file
    :param blob_to_decrypt: If set, encrypt the message directly and not from file
    :param filename_decrypted_file: The filename of the decrypted file
    :poram key_path: Sets the path to an specific key, otherwise the key in the same folder (key.pem) is used
    :poram passphrase: The passphrase used to unlock the private key
    """
    self.key = self.__get_pgp_key(key_path)
    self.message = pgpy.PGPMessage.from_blob(blob_to_decrypt)

    # unlock the key with the passphrase
    with self.key.unlock(passphrase) as k:
        save_to_file(filename_decrypted_file, self.key.decrypt(self.message).message, mode='wb')

def decrypt_file(self, file_to_decrypt, filename_decrypted_file, key_path=None, passphrase=None):
    """
    Decrypts an blob that is pgp encrypted, using the private we've already created and saves the result
    to the given file
    :param file_to_decrypt: The file we want to decrypt (path to the file)
    :param filename_decrypted_file: The filename of the decrypted file
    :poram key_path: Sets the path to an specific key, otherwise the key in the same folder (key.pem) is used
    :poram passphrase: The passphrase used to unlock the private key
    """
    self.key = self.__get_pgp_key(key_path)
    self.message = pgpy.PGPMessage.from_file(file_to_decrypt)

    # unlock the key with the passphrase
    with self.key.unlock(passphrase) as k:
        save_to_file(filename_decrypted_file, self.key.decrypt(self.message).message, mode='wb')

def __get_pgp_key(self, key_path=None):
    if key_path:
        key, _ = pgpy.PGPKey.from_file(key_path)
    else:
        key, _ = pgpy.PGPKey.from_file(self.filename_key)
    return key

def __get_message(self):
    with open(self.filename_org, "rb") as f:
        self.message = pgpy.PGPMessage.new(f.read())

def save_to_file(file_name, content, path=".", mode='wb'):
    with open(f"{path}/{file_name}", mode=mode) as f:
        if mode.startswith('w'):
            f.write(content)
        else:
            f.append(content)
</code>```

and here is how to use it:

```<code># encrypt example:
pgp_decryptor = PgpDecryptor()
pgp_decryptor.generate_pgp_key(filename_key="test.pem")
# pgp_decryptor.generate_pgp_key(filename_key="test.pem", passphrase="schnarr")
pgp_decryptor.encrypt_file(file_to_encrypt="43630766.PDF", filename_encrypted_file="43630766_encrypted.pdf", key_path="test.pem")
pgp_decryptor.decrypt_file("43630766_encrypted.pdf", "43630766_decrypted.pdf", key_path="test.pem")
# pgp_decryptor.decrypt_file("43630766_encrypted.pdf", "43630766_decrypted.pdf", key_path="test.pem", passphrase="schnarr")
</code>```

Body:
I have a python program, that iterates over emails and download the Attachment files.
Now I want to procsess the content of said emails.

I have a key like this "c34pj34365g0394hg" and a smime_post.key file with content like this:

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIJrTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIeHcIC+7nTU4CAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBC/DGRNkgo3TizIHH3TWnHqBIIJ
UPu99JJuhB6hcHYS5UxgACXAdO6xOmXV7C1JUduGmTfNin0YwkJN7SMG6nOhGuxN
6BWczg5ENf1b3btIB6LwxyCFY+O/lvgzXMWrOr6/0XEQt3p7WTqOxbZ94nEcki1u
vZT9QdtveNAoZrZ48RyY1uoo2l3GLbSumSKaHx88lGLecMtnbUhFbwfE6Z6l6msK
VIi6kq+g1qiFD62xenyyS20UHkTgPy8CsJdllDDfU+aPxkOzTmfAdEHeIUzYwNPu

and on top of that of course the smime.p7m file with the encryptet content.
I'm not able to finde something usefull to this topic.

I had a similar problem with another email import (from italy) where I was able to encrypt the file with this code:

def remove_signature(fileString):
    p7 = crypto.load_pkcs7_data(crypto.FILETYPE_ASN1, fileString)
    bio_out = crypto._new_mem_buf()
    res = _lib.PKCS7_verify(p7._pkcs7, _ffi.NULL, _ffi.NULL, _ffi.NULL, bio_out,
                            _lib.PKCS7_NOVERIFY | _lib.PKCS7_NOSIGS)

    if res == 1:
        return (crypto._bio_to_string(bio_out).decode('UTF-8'))
    else:
        errno = _lib.ERR_get_error()
        errstrlib = _ffi.string(_lib.ERR_lib_error_string(errno))
        errstrfunc = _ffi.string(_lib.ERR_func_error_string(errno))
        errstrreason = _ffi.string(_lib.ERR_reason_error_string(errno))
        print(f"------------------------------------\berrstrreason: {errstrreason}---------------------------\b")
        print(f"------------------------------------\berrno: {errno}---------------------------\b")
        print(f"------------------------------------\berrstrlib: {errstrlib}---------------------------\b")
        print(f"------------------------------------\berrstrfunc: {errstrfunc}---------------------------\b")
        return ""

My guess is, that I have to decrypt the .key file and use that as a cert?
If someone has some experience with that, I would appreciate any help.

Body:
Tock me some time, but I got my problem solved.
We changed the encryption to pgp and here is how I generate my keys / encrypt / decrypt files / blobs (i.e. a mailattachment)

import pgpy
from pgpy.constants import PubKeyAlgorithm, KeyFlags, HashAlgorithm, SymmetricKeyAlgorithm, CompressionAlgorithm

 class PgpDecryptor:
    """
    A class that can encrypt / decrypt files with pgp encryption.
    """
    # in die config packen?
    filename_key = "key.pem"

    def __init__(self):
        self.key = None
        self.message = None
        self.encrypted_message = None
        self.filename_org = None
    self.filename_encrypted_PGPMessage = None
    self.passphrase = None

def generate_pgp_key(self, filename_key, passphrase=None):
    """
    Generates a new pgp key pair with a passphrase. T
    :param filename_key: The path for the key storage
    :param passphrase: The passphrase to lock / unlock the private key
    """
    self.key = pgpy.PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 4096)
    uid = pgpy.PGPUID.new('Martin Kraemer', email='martin.kraemer@sixt.com')

    # All diese Verschluesselungstechniken / usages usw. sind moeglich (keine Preferenz)
    # Man kann ebenfalls nen expiration date mitgeben (in etwa so: key_expiration=timedelta(days=365))
    self.key.add_uid(uid, usage={KeyFlags.Sign, KeyFlags.EncryptCommunications, KeyFlags.EncryptStorage},
                     hashes=[HashAlgorithm.SHA256, HashAlgorithm.SHA384, HashAlgorithm.SHA512,
                             HashAlgorithm.SHA224],
                     ciphers=[SymmetricKeyAlgorithm.AES256, SymmetricKeyAlgorithm.AES192,
                              SymmetricKeyAlgorithm.AES128],
                     compression=[CompressionAlgorithm.ZLIB, CompressionAlgorithm.BZ2, CompressionAlgorithm.ZIP,
                                  CompressionAlgorithm.Uncompressed])

    # protect a key with a phrase like "Schnarr"
    # this phrase is needed to use the private key
    if passphrase:
        self.key.protect(passphrase, SymmetricKeyAlgorithm.AES256, HashAlgorithm.SHA256)
    save_to_file(filename_key, str(self.key), mode="w")
    save_to_file(f"pub_{filename_key}", str(self.key.pubkey), mode="w")

def encrypt_file(self, file_to_encrypt=None, filename_encrypted_file=None, key_path=None):
    """
    Encrypts a file with a pgp key.
    :param file_to_encrypt: The path to the file we want to encrypt
    :param filename_encrypted_file: The path to the file we've encrypted
    :poram key_path: Sets the path to an specific key, otherwise the key in the same folder (key.pem) is used
    """
    if file_to_encrypt:
        self.filename_org = file_to_encrypt
    if filename_encrypted_file:
        self.filename_encrypted_PGPMessage = filename_encrypted_file

    self.__get_message()
    self.key = self.__get_pgp_key(key_path)
    self.encrypted_message = self.key.pubkey.encrypt(self.message)
    save_to_file(self.filename_encrypted_PGPMessage, str(self.encrypted_message), mode="w")

def decrypt_blob(self, blob_to_decrypt, filename_decrypted_file, key_path=None, passphrase=None):
    """
    Decrypts an blob that is pgp encrypted, using the private we've already created and saves the result
    to the given file
    :param blob_to_decrypt: If set, encrypt the message directly and not from file
    :param filename_decrypted_file: The filename of the decrypted file
    :poram key_path: Sets the path to an specific key, otherwise the key in the same folder (key.pem) is used
    :poram passphrase: The passphrase used to unlock the private key
    """
    self.key = self.__get_pgp_key(key_path)
    self.message = pgpy.PGPMessage.from_blob(blob_to_decrypt)

    # unlock the key with the passphrase
    with self.key.unlock(passphrase) as k:
        save_to_file(filename_decrypted_file, self.key.decrypt(self.message).message, mode='wb')

def decrypt_file(self, file_to_decrypt, filename_decrypted_file, key_path=None, passphrase=None):
    """
    Decrypts an blob that is pgp encrypted, using the private we've already created and saves the result
    to the given file
    :param file_to_decrypt: The file we want to decrypt (path to the file)
    :param filename_decrypted_file: The filename of the decrypted file
    :poram key_path: Sets the path to an specific key, otherwise the key in the same folder (key.pem) is used
    :poram passphrase: The passphrase used to unlock the private key
    """
    self.key = self.__get_pgp_key(key_path)
    self.message = pgpy.PGPMessage.from_file(file_to_decrypt)

    # unlock the key with the passphrase
    with self.key.unlock(passphrase) as k:
        save_to_file(filename_decrypted_file, self.key.decrypt(self.message).message, mode='wb')

def __get_pgp_key(self, key_path=None):
    if key_path:
        key, _ = pgpy.PGPKey.from_file(key_path)
    else:
        key, _ = pgpy.PGPKey.from_file(self.filename_key)
    return key

def __get_message(self):
    with open(self.filename_org, "rb") as f:
        self.message = pgpy.PGPMessage.new(f.read())

def save_to_file(file_name, content, path=".", mode='wb'):
    with open(f"{path}/{file_name}", mode=mode) as f:
        if mode.startswith('w'):
            f.write(content)
        else:
            f.append(content)

and here is how to use it:

# encrypt example:
pgp_decryptor = PgpDecryptor()
pgp_decryptor.generate_pgp_key(filename_key="test.pem")
# pgp_decryptor.generate_pgp_key(filename_key="test.pem", passphrase="schnarr")
pgp_decryptor.encrypt_file(file_to_encrypt="43630766.PDF", filename_encrypted_file="43630766_encrypted.pdf", key_path="test.pem")
pgp_decryptor.decrypt_file("43630766_encrypted.pdf", "43630766_decrypted.pdf", key_path="test.pem")
# pgp_decryptor.decrypt_file("43630766_encrypted.pdf", "43630766_decrypted.pdf", key_path="test.pem", passphrase="schnarr")


问:
MongoDB using $cond with Update ($inc)
Is there any way to use $cond along with ($set, $inc, ...) operators in update? (MongoDB 4.2)
I want to update a field in my document by $inc it with "myDataInt" if a condition comes true, otherwise keeps it as it is:

```<code>db.mycoll.update(
    {"_id" : "5e9e5da03da783817d231dc4"},
    {"$inc" : {
       "my_data_sum" : {
           "$cond" : [
               {
                  "$ne" : ["snapshot_time", new_snapshot_time)]
               },myDataInt, 0]
           ]
       }
    },
    {upsert=True, multi=False}
)
</code>```

However, this gives an error in pymongo:

```<code>raise WriteError(error.get("errmsg"), error.get("code"), error)
pymongo.errors.WriteError: The dollar ($) prefixed field '$cond' in 'my_data_sum.$cond' is not valid for storage.
</code>```

Any idea to avoid using find() before update in this case?

Update:

If I use the approach that Joe has mentioned, an exception will be raised in PyMongo (v3.10.1) due to using 'list' as a parameter in update_many() instead of 'dict':

```<code>from pymongo import MongoClient

db = MongoClient()['mydb']

db.mycoll.update_many(
    {"_id" : "5e9e5da03da783817d231dc4"},
    [{"$set" : {
       "my_data_sum" : {
           "$sum": [
               "$my_data_sum",
               {"$cond" : [
                   {"$ne" : ["snapshot_time", new_snapshot_time]},
                   myDataInt,
                   0
               ]}
           ]
       }
    }}],

    upsert:true
)
</code>```

That ends up with this error:

```<code>  File "/usr/local/lib64/python3.6/site-packages/pymongo/collection.py", line 1076, in update_many session=session),
  File "/usr/local/lib64/python3.6/site-packages/pymongo/collection.py", line 856, in _update_retryable _update, session)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/mongo_client.py", line 1491, in _retryable_write return self._retry_with_session(retryable, func, s, None)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/mongo_client.py", line 1384, in _retry_with_session return func(session, sock_info, retryable)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/collection.py", line 852, in _update retryable_write=retryable_write)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/collection.py", line 823, in _update _check_write_command_response(result)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/helpers.py", line 221, in _check_write_command_response _raise_last_write_error(write_errors)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/helpers.py", line 203, in _raise_last_write_error raise WriteError(error.get("errmsg"), error.get("code"), error)
pymongo.errors.WriteError: Modifiers operate on fields but we found type array instead. For example: {$mod: {<field>: ...}} not {$set: [ { $set: { my_data_sum: { $sum: [ "$my_data_sum", { $cond: [ { $ne: [ "$snapshot_time", 1586910283 ] }, 1073741824, 0 ] } ] } } } ]}
</code>```

答:
https://mongoplayground.net/p/1AklFKuhFi6

```<code>[
  {
    "id": 1,
    "like": 3
  },
  {
    "id": 2,
    "like": 1
  }
]
</code>```

let value = 1,

if you want to increment then use

value = -1 * value

```<code>db.collection.aggregate([
  {
    "$match": {
      "id": 1
    }
  },
  {
    "$set": {
      "count": {
        $cond: {
          if: {
            $gt: [
              "$like",
              0
            ]
          },
          then: {
            "$subtract": [
              "$like",
               value
            ]
          },
          else: 0
        }
      }
    }
  }
])
</code>```

Title: MongoDB using $cond with Update ($inc)

Body:
Is there any way to use $cond along with ($set, $inc, ...) operators in update? (MongoDB 4.2)
I want to update a field in my document by $inc it with "myDataInt" if a condition comes true, otherwise keeps it as it is:

db.mycoll.update(
    {"_id" : "5e9e5da03da783817d231dc4"},
    {"$inc" : {
       "my_data_sum" : {
           "$cond" : [
               {
                  "$ne" : ["snapshot_time", new_snapshot_time)]
               },myDataInt, 0]
           ]
       }
    },
    {upsert=True, multi=False}
)

However, this gives an error in pymongo:

raise WriteError(error.get("errmsg"), error.get("code"), error)
pymongo.errors.WriteError: The dollar ($) prefixed field '$cond' in 'my_data_sum.$cond' is not valid for storage.

Any idea to avoid using find() before update in this case?

Update:

If I use the approach that Joe has mentioned, an exception will be raised in PyMongo (v3.10.1) due to using 'list' as a parameter in update_many() instead of 'dict':

from pymongo import MongoClient

db = MongoClient()['mydb']

db.mycoll.update_many(
    {"_id" : "5e9e5da03da783817d231dc4"},
    [{"$set" : {
       "my_data_sum" : {
           "$sum": [
               "$my_data_sum",
               {"$cond" : [
                   {"$ne" : ["snapshot_time", new_snapshot_time]},
                   myDataInt,
                   0
               ]}
           ]
       }
    }}],

    upsert:true
)

That ends up with this error:

  File "/usr/local/lib64/python3.6/site-packages/pymongo/collection.py", line 1076, in update_many session=session),
  File "/usr/local/lib64/python3.6/site-packages/pymongo/collection.py", line 856, in _update_retryable _update, session)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/mongo_client.py", line 1491, in _retryable_write return self._retry_with_session(retryable, func, s, None)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/mongo_client.py", line 1384, in _retry_with_session return func(session, sock_info, retryable)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/collection.py", line 852, in _update retryable_write=retryable_write)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/collection.py", line 823, in _update _check_write_command_response(result)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/helpers.py", line 221, in _check_write_command_response _raise_last_write_error(write_errors)
  File "/usr/local/lib64/python3.6/site-packages/pymongo/helpers.py", line 203, in _raise_last_write_error raise WriteError(error.get("errmsg"), error.get("code"), error)
pymongo.errors.WriteError: Modifiers operate on fields but we found type array instead. For example: {$mod: {<field>: ...}} not {$set: [ { $set: { my_data_sum: { $sum: [ "$my_data_sum", { $cond: [ { $ne: [ "$snapshot_time", 1586910283 ] }, 1073741824, 0 ] } ] } } } ]}

Body:
https://mongoplayground.net/p/1AklFKuhFi6

[
  {
    "id": 1,
    "like": 3
  },
  {
    "id": 2,
    "like": 1
  }
]

let value = 1,

if you want to increment then use

value = -1 * value

db.collection.aggregate([
  {
    "$match": {
      "id": 1
    }
  },
  {
    "$set": {
      "count": {
        $cond: {
          if: {
            $gt: [
              "$like",
              0
            ]
          },
          then: {
            "$subtract": [
              "$like",
               value
            ]
          },
          else: 0
        }
      }
    }
  }
])


问:
update field in struct column pyspark - withField does not replace
I have a table with a column with dates, which I want to use to update the value of field in a struct that I define for a new column.

Simple table:

date
----------
1960-12-01
```
<p>Struct:</p>
```value_type = T.StructType(
    [
       T.StructField("extra",T.MapType(T.StringType(), T.StringType(), True), True),
       T.StructField("date", T.StringType(), True),
       T.StructField("from_date", T.StringType(), True),
       T.StructField("to_date", T.StringType(), True),
       T.StructField("value", T.StringType(), True),
    ]
)
```
<p>Which I use to create a new empty column which I then want to update with this code:</p>
```dateOfBirth_df = (
  df
 .withColumn("dob_new", (F.lit(None).cast(value_type)))
 .withColumn("dob_new", F.col("dob_new").withField("value", F.lit("date"))))
```

Which works, but it the dob_new.date field does not get updated.
Anyone knows what I am doing wrong?

答:
When you do:

```<code>F.lit(None).cast(value_type)
</code>```

It is creating a column with value NULL. So withField on null value will always give null.

You can try this instead:

```<code>from pyspark.sql import functions as F

dateOfBirth_df = df.withColumn(
    "dob_new",
    F.struct(*[F.lit(None).cast(f.dataType).alias(f.name) for f in value_type.fields])
).withColumn(
    "dob_new",
    F.col("dob_new").withField("value", F.col("date"))
)

dateOfBirth_df.show(truncate=False)
#+----------+------------------------------------+
#|date      |dob_new                             |
#+----------+------------------------------------+
#|1960-12-01|{null, null, null, null, 1960-12-01}|
#+----------+------------------------------------+
</code>```

Body:
I have a table with a column with dates, which I want to use to update the value of field in a struct that I define for a new column.

Simple table:

date
----------
1960-12-01

Struct:

value_type = T.StructType(
    [
       T.StructField("extra",T.MapType(T.StringType(), T.StringType(), True), True),
       T.StructField("date", T.StringType(), True),
       T.StructField("from_date", T.StringType(), True),
       T.StructField("to_date", T.StringType(), True),
       T.StructField("value", T.StringType(), True),
    ]
)

Which I use to create a new empty column which I then want to update with this code:

dateOfBirth_df = (
  df
 .withColumn("dob_new", (F.lit(None).cast(value_type)))
 .withColumn("dob_new", F.col("dob_new").withField("value", F.lit("date"))))

Which works, but it the dob_new.date field does not get updated.
Anyone knows what I am doing wrong?

Body:
When you do:

F.lit(None).cast(value_type)

It is creating a column with value NULL. So withField on null value will always give null.

You can try this instead:

from pyspark.sql import functions as F

dateOfBirth_df = df.withColumn(
    "dob_new",
    F.struct(*[F.lit(None).cast(f.dataType).alias(f.name) for f in value_type.fields])
).withColumn(
    "dob_new",
    F.col("dob_new").withField("value", F.col("date"))
)

dateOfBirth_df.show(truncate=False)
#+----------+------------------------------------+
#|date      |dob_new                             |
#+----------+------------------------------------+
#|1960-12-01|{null, null, null, null, 1960-12-01}|
#+----------+------------------------------------+


问:
.net blazor wasm testing bunit with localization
I'm building a blazor webassembly site and I want to use bunit for testing purpose.

I run into trouble with components that use localization, I am getting the following error:

System.InvalidOperationException
Cannot provide a value for property 'L' on type 'Path.To.Component'. There is no registered service of type 'Microsoft.Extensions.Localization.IStringLocalizer`1[Path.To.Component]'.
at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass7_0.g__Initialize|1(IServiceProvider serviceProvider, IComponent component)

I already tried to install localization on the test project, but this did not work.

答:
I found the answer on the github discussion:

https://github.com/bUnit-dev/bUnit/discussions/239


问:
How to remove special characters from the `TextField` in SwiftUI
I need to apply some validations in my form like remove special character, accept only number, alphabets, alphanumeric, and only specific length of a string.

I have many text fields, in many places in my app. So that I'm creating extensions to Binding, and trying to apply conditions when editing.

These filters/conditions using for @State, @Published, and @Binding variables.

Here is my code:

struct ContentView: View {
    @State var name = ""
    var body: some View {
        InputField(text: $name)
    }
}

struct InputField: View {
    @Binding var text: String
    var body: some View {
        VStack {
            TextField("Name here...", text: $text.limit(6).alphaNumeric)
                .frame(maxWidth: .infinity, minHeight: 48,  alignment: .leading)
        }.padding()
    }
}

extension Binding where Value == String {

    var alphaNumeric: Binding<String> {
        Binding<String>(get: { self.wrappedValue },
                        set: {
            self.wrappedValue = $0.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)})
    }

    func limit(_ length: Int) -> Binding<String> {
        Binding<String>(get: { self.wrappedValue },
                        set: {
            print($0.prefix(length))
            self.wrappedValue = String($0.prefix(length))
        })
    }
}
```

Here in the above code $text.limit(6).alphaNumeric, I'm trying to limit the length to 6 characters and only allow the alphaNumeric string.

答:
You can use .onChange(of: ). Should also work for @Binding etc.

```<code>@State private var inputText = ""
let maxCount = 20

var body: some View {
    TextField("Input", text: $inputText)
        .padding()
        .background(.gray.opacity(0.1))
        .padding()

        .onChange(of: inputText) { _ in
            // check character set
            var checked = inputText.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
            // check length
            checked = String(checked.prefix(maxCount)) // regards to Leo Dabus
            inputText = checked
        }
}
</code>```

Body:
I need to apply some validations in my form like remove special character, accept only number, alphabets, alphanumeric, and only specific length of a string.

I have many text fields, in many places in my app. So that I'm creating extensions to Binding, and trying to apply conditions when editing.

These filters/conditions using for @State, @Published, and @Binding variables.

Here is my code:

struct ContentView: View {
    @State var name = ""
    var body: some View {
        InputField(text: $name)
    }
}

struct InputField: View {
    @Binding var text: String
    var body: some View {
        VStack {
            TextField("Name here...", text: $text.limit(6).alphaNumeric)
                .frame(maxWidth: .infinity, minHeight: 48,  alignment: .leading)
        }.padding()
    }
}

extension Binding where Value == String {

    var alphaNumeric: Binding<String> {
        Binding<String>(get: { self.wrappedValue },
                        set: {
            self.wrappedValue = $0.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)})
    }

    func limit(_ length: Int) -> Binding<String> {
        Binding<String>(get: { self.wrappedValue },
                        set: {
            print($0.prefix(length))
            self.wrappedValue = String($0.prefix(length))
        })
    }
}

Here in the above code $text.limit(6).alphaNumeric, I'm trying to limit the length to 6 characters and only allow the alphaNumeric string.

Body:
You can use .onChange(of: ). Should also work for @Binding etc.

@State private var inputText = ""
let maxCount = 20

var body: some View {
    TextField("Input", text: $inputText)
        .padding()
        .background(.gray.opacity(0.1))
        .padding()

        .onChange(of: inputText) { _ in
            // check character set
            var checked = inputText.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
            // check length
            checked = String(checked.prefix(maxCount)) // regards to Leo Dabus
            inputText = checked
        }
}


问:
How do I access the n-grams produced by FeaturizeText in Microsoft.ML?
I managed to get a first text analyser running in Microsoft.ML. I would like to get to the list of ngrams determined by the model, but I can only get the numerical vectors "counting" occurrences without knowing what they refer to.

Here is the core of my working code so far:

```<code>var mlContext = new MLContext();
var articles = SampleData.Articles.Select(a => new TextData{ Text=a }).ToArray();
var dataview = mlContext.Data.LoadFromEnumerable(articles);
var options = new TextFeaturizingEstimator.Options() {
  OutputTokensColumnName = "OutputTokens",
  CaseMode = TextNormalizingEstimator.CaseMode.Lower,
  KeepDiacritics = false,
  KeepPunctuations = false,
  KeepNumbers = false,
  Norm = TextFeaturizingEstimator.NormFunction.L2,
  StopWordsRemoverOptions = new StopWordsRemovingEstimator.Options() {
    Language = TextFeaturizingEstimator.Language.Dutch,
  },
  WordFeatureExtractor = new WordBagEstimator.Options() {
    NgramLength = 4,
    SkipLength = 1,
    UseAllLengths = true,
    MaximumNgramsCount = new int[] { 20, 10, 10, 10 },
    Weighting = NgramExtractingEstimator.WeightingCriteria.TfIdf,
  },
  CharFeatureExtractor = null,
};
var textPipeline = mlContext.Transforms.Text
  .FeaturizeText("Features", options, "Text");
var textTransformer = textPipeline.Fit(dataview);
var predictionEngine = mlContext.Model.CreatePredictionEngine<TextData, TransformedTextData>(textTransformer);
foreach (var article in articles)
{
  var prediction = predictionEngine.Predict(article);
  Console.WriteLine($"Article: {article.Text.Substring(0, 30)}...");
  Console.WriteLine($"Number of Features: {prediction.Features.Length}");
  Console.WriteLine($"Features: {string.Join(",", prediction.Features.Take(50).Select(f => f.ToString("0.00")))}\n");
}
</code>```

答:
Well, I figured it out, and wanted to share it here if anyone might bump into this same issue. First, you create your model as usual. Take notice of the name of the column where you put the output of the Ngrams step (in our case "ProduceNgrams").

Then the combination of "Schema.GetSlotNames" and "slotNames.GetValues" does the trick of fetching the desired ngrams:

var textPipeline =
    mlContext.Transforms.Text.NormalizeText("Tokens", "Text", TextNormalizingEstimator.CaseMode.Lower, false, false, false)
    .Append(mlContext.Transforms.Text.TokenizeIntoWords("Tokens"))
    .Append(mlContext.Transforms.Text.RemoveDefaultStopWords("Tokens", language: StopWordsRemovingEstimator.Language.Dutch))
    .Append(mlContext.Transforms.Conversion.MapValueToKey("Tokens"))
    .Append(mlContext.Transforms.Text.ProduceNgrams("NgramFeatures", "Tokens"))
    .Append(mlContext.Transforms.Text.LatentDirichletAllocation("LDAFeatures", "NgramFeatures",
      numberOfTopics: 10
    ))
    .Append(mlContext.Transforms.NormalizeLpNorm("Features", "LDAFeatures"));

var textTransformer = textPipeline.Fit(dataview);
var transformedDataView = textTransformer.Transform(dataview);

VBuffer<ReadOnlyMemory<char>> slotNames = default;
transformedDataView.Schema["NgramFeatures"].GetSlotNames(ref slotNames);
var ngrams = slotNames.GetValues().ToArray().Select(x => x.Span.ToString()); //.Replace('|',' '));
Console.WriteLine($"Ngrams: {string.Join(", ", ngrams)}\n");

var predictionEngine = mlContext.Model.CreatePredictionEngine<TextData, TransformedTextData>(textTransformer);
var articlesWithFeatures = new List<(TextData, TransformedTextData)>();
foreach (var article in articles)
{
  var articleWithFeatures = predictionEngine.Predict(article);
  Console.WriteLine($"Article: {article.Text.Substring(0, 30)}...");
  Console.WriteLine($"Number of Features: {articleWithFeatures.Features.Length}");
  Console.WriteLine($"Features: {string.Join(",", articleWithFeatures.Features.Take(50).Select(f => f.ToString("0.00")))}\n");

  articlesWithFeatures.Add((article, articleWithFeatures));
}
```

Body:
I managed to get a first text analyser running in Microsoft.ML. I would like to get to the list of ngrams determined by the model, but I can only get the numerical vectors "counting" occurrences without knowing what they refer to.

Here is the core of my working code so far:

var mlContext = new MLContext();
var articles = SampleData.Articles.Select(a => new TextData{ Text=a }).ToArray();
var dataview = mlContext.Data.LoadFromEnumerable(articles);
var options = new TextFeaturizingEstimator.Options() {
  OutputTokensColumnName = "OutputTokens",
  CaseMode = TextNormalizingEstimator.CaseMode.Lower,
  KeepDiacritics = false,
  KeepPunctuations = false,
  KeepNumbers = false,
  Norm = TextFeaturizingEstimator.NormFunction.L2,
  StopWordsRemoverOptions = new StopWordsRemovingEstimator.Options() {
    Language = TextFeaturizingEstimator.Language.Dutch,
  },
  WordFeatureExtractor = new WordBagEstimator.Options() {
    NgramLength = 4,
    SkipLength = 1,
    UseAllLengths = true,
    MaximumNgramsCount = new int[] { 20, 10, 10, 10 },
    Weighting = NgramExtractingEstimator.WeightingCriteria.TfIdf,
  },
  CharFeatureExtractor = null,
};
var textPipeline = mlContext.Transforms.Text
  .FeaturizeText("Features", options, "Text");
var textTransformer = textPipeline.Fit(dataview);
var predictionEngine = mlContext.Model.CreatePredictionEngine<TextData, TransformedTextData>(textTransformer);
foreach (var article in articles)
{
  var prediction = predictionEngine.Predict(article);
  Console.WriteLine($"Article: {article.Text.Substring(0, 30)}...");
  Console.WriteLine($"Number of Features: {prediction.Features.Length}");
  Console.WriteLine($"Features: {string.Join(",", prediction.Features.Take(50).Select(f => f.ToString("0.00")))}\n");
}


问:
How can I use localStorage to save this timer on page Refresh in angular 12?
How can I use localStorage to save this timer on page Refresh in angular 12 ?

timer:any;

```<code>ngOnInit(): void { this.startTimer() }

startTimer() {

    this.timer = this.questions.length * 1 * 60;

    let t = window.setInterval(() => {
      if (this.timer <= 0) {
        this.evaluateQuiz();
        clearInterval(t);
      } else {
        this.timer--;
      }
    }, 1000);   }

//using this in html
    getFormattedTime() {
    let mm = Math.floor(this.timer / 60);
    let ss = this.timer - mm * 60;

    return `${mm} min : ${ss} sec`;   }
</code>```

答: You cannot save whole interval. But what you can do is to save the remaining timer number (this.timer). You could set the localStorage.set('timer', this.timer) in the ngOnDestroy (which is called when the component gets destroyed). In the ngOnInit you would check if there is something in the cache and if so you would set it to this value. If you also need to reduce the value of the this.timer variable with the time between the last time you opened the page you need also to include the time when the component gets destroyed into the local storage.</p>

Body:
How can I use localStorage to save this timer on page Refresh in angular 12 ?

timer:any;

ngOnInit(): void { this.startTimer() }

startTimer() {

    this.timer = this.questions.length * 1 * 60;

    let t = window.setInterval(() => {
      if (this.timer <= 0) {
        this.evaluateQuiz();
        clearInterval(t);
      } else {
        this.timer--;
      }
    }, 1000);   }

//using this in html
    getFormattedTime() {
    let mm = Math.floor(this.timer / 60);
    let ss = this.timer - mm * 60;

    return `${mm} min : ${ss} sec`;   }


问:
Fluent Bit 1.8+ and MULTILINE_PARSER
My goal is to collect logs from Java (Spring Boot) applications running on Bare Kubernetes.
These logs are then translated into ES and visualized in Kibana.

For these purposes I deployed Fleunt Bit 1.8.9 via Kubernetes 1.22. Since I use Containerd instead for Docker, then my Fluent Bit configuration is as follow (Please note that I have only specified one log-file):

```<code>  fluent-bit.conf: |
    [SERVICE]
        Flush         1
        Log_Level     info
        Daemon        off
        Parsers_File  parsers.conf
        HTTP_Server   On
        HTTP_Listen   0.0.0.0
        HTTP_Port     2020

    @INCLUDE input-kubernetes.conf
    @INCLUDE filter-kubernetes.conf
    @INCLUDE output-elasticsearch.conf

  input-kubernetes.conf: |
    [INPUT]
        Name              tail
        Tag               kube.*
        Path              /var/log/containers/*.log
        Read_from_head    true
        Parser            cri

  filter-kubernetes.conf: |
    [FILTER]
        Name                kubernetes
        Match               kube.*
        Kube_URL            https://kubernetes.default.svc:443
        Kube_CA_File        /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        Kube_Token_File     /var/run/secrets/kubernetes.io/serviceaccount/token
        Kube_Tag_Prefix     kube.var.log.containers.
        Merge_Log           On
        Merge_Log_Key       log_processed
        K8S-Logging.Parser  On
        K8S-Logging.Exclude Off

  output-elasticsearch.conf: |
    [OUTPUT]
        Name     es
        Match    kube.*
        Host     ${FLUENT_ELASTICSEARCH_HOST}
        Port     ${FLUENT_ELASTICSEARCH_PORT}
        Index    kube-code_index
        Type     kube-code_type

  parsers.conf: |
    [PARSER]
        Name cri
        Format regex
        Regex ^(?<time>[^ ]+) (?<stream>stdout|stderr) (?<logtag>[^ ]*) (?<message>.*)$
        Time_Key    time
        Time_Format %Y-%m-%dT%H:%M:%S.%L%z
</code>```

With this configuration in Kibana, the Java stack trace messages are displayed unstructured:

But I need the Java stack trace to be structured like in the screenshot below:

I tried a configuration like this:

```<code>  input-kubernetes.conf: |
    [INPUT]
        Name              tail
        Tag               kube.*
        Path              /var/log/containers/*.log
        Read_from_head    true
        Multiline.parser  cri, multiline-regex-cri
</code>```

And:

```<code>parsers.conf: |
    [PARSER]
        # http://rubular.com/r/tjUt3Awgg4
        Name cri
        Format regex
        Regex ^(?<time>[^ ]+) (?<stream>stdout|stderr) (?<logtag>[^ ]*) (?<message>.*)$
        Time_Key    time
        Time_Format %Y-%m-%dT%H:%M:%S.%L%z

    [MULTILINE_PARSER]
        name          multiline-regex-cri
        type          regex
        flush_timeout 1000
        #
        # Regex rules for multiline parsing
        # ---------------------------------
        #
        # configuration hints:
        #
        #  - first state always has the name: start_state
        #  - every field in the rule must be inside double quotes
        #
        # rules |   state name  | regex pattern                  | next state
        # ------|---------------|--------------------------------------------
        rule      "start_state"   "/(\D+ \d+ \d+\:\d+\:\d+)(.*)/"  "cont"
        rule      "cont"          "/^\s+at.*/"                     "cont"
</code>```

But all the same, the logs are not structured:

Please help me structure the logs.

答:
I had this problem too. If you use multiple parsers on your input, fluentbit tries to apply each of them on the same original input and does not apply them one after the other.

When you have multiple multiline parsers, and want them to be applied one after the other, you should use filters, in your case it would be something like that:

```<code>[INPUT]
        Name              tail
        Tag               kube.*
        Path              /var/log/containers/*.log
        Read_from_head    true
        Multiline.parser  cri

[FILTER]
        Name                  multiline
        Match                 kube.*
        multiline.key_content log
        multiline.parser      java

</code>```

I can see in your screenshot, that you are trying to parse java stacttrace, for that you can use build-in java parser, so you do not need multiline-regex-cri.

Title: Fluent Bit 1.8+ and MULTILINE_PARSER

Body:
My goal is to collect logs from Java (Spring Boot) applications running on Bare Kubernetes.
These logs are then translated into ES and visualized in Kibana.

For these purposes I deployed Fleunt Bit 1.8.9 via Kubernetes 1.22. Since I use Containerd instead for Docker, then my Fluent Bit configuration is as follow (Please note that I have only specified one log-file):

  fluent-bit.conf: |
    [SERVICE]
        Flush         1
        Log_Level     info
        Daemon        off
        Parsers_File  parsers.conf
        HTTP_Server   On
        HTTP_Listen   0.0.0.0
        HTTP_Port     2020

    @INCLUDE input-kubernetes.conf
    @INCLUDE filter-kubernetes.conf
    @INCLUDE output-elasticsearch.conf

  input-kubernetes.conf: |
    [INPUT]
        Name              tail
        Tag               kube.*
        Path              /var/log/containers/*.log
        Read_from_head    true
        Parser            cri

  filter-kubernetes.conf: |
    [FILTER]
        Name                kubernetes
        Match               kube.*
        Kube_URL            https://kubernetes.default.svc:443
        Kube_CA_File        /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        Kube_Token_File     /var/run/secrets/kubernetes.io/serviceaccount/token
        Kube_Tag_Prefix     kube.var.log.containers.
        Merge_Log           On
        Merge_Log_Key       log_processed
        K8S-Logging.Parser  On
        K8S-Logging.Exclude Off

  output-elasticsearch.conf: |
    [OUTPUT]
        Name     es
        Match    kube.*
        Host     ${FLUENT_ELASTICSEARCH_HOST}
        Port     ${FLUENT_ELASTICSEARCH_PORT}
        Index    kube-code_index
        Type     kube-code_type

  parsers.conf: |
    [PARSER]
        Name cri
        Format regex
        Regex ^(?<time>[^ ]+) (?<stream>stdout|stderr) (?<logtag>[^ ]*) (?<message>.*)$
        Time_Key    time
        Time_Format %Y-%m-%dT%H:%M:%S.%L%z

With this configuration in Kibana, the Java stack trace messages are displayed unstructured:

But I need the Java stack trace to be structured like in the screenshot below:

I tried a configuration like this:

  input-kubernetes.conf: |
    [INPUT]
        Name              tail
        Tag               kube.*
        Path              /var/log/containers/*.log
        Read_from_head    true
        Multiline.parser  cri, multiline-regex-cri

And:

parsers.conf: |
    [PARSER]
        # http://rubular.com/r/tjUt3Awgg4
        Name cri
        Format regex
        Regex ^(?<time>[^ ]+) (?<stream>stdout|stderr) (?<logtag>[^ ]*) (?<message>.*)$
        Time_Key    time
        Time_Format %Y-%m-%dT%H:%M:%S.%L%z

    [MULTILINE_PARSER]
        name          multiline-regex-cri
        type          regex
        flush_timeout 1000
        #
        # Regex rules for multiline parsing
        # ---------------------------------
        #
        # configuration hints:
        #
        #  - first state always has the name: start_state
        #  - every field in the rule must be inside double quotes
        #
        # rules |   state name  | regex pattern                  | next state
        # ------|---------------|--------------------------------------------
        rule      "start_state"   "/(\D+ \d+ \d+\:\d+\:\d+)(.*)/"  "cont"
        rule      "cont"          "/^\s+at.*/"                     "cont"

But all the same, the logs are not structured:

Please help me structure the logs.

Body:
I had this problem too. If you use multiple parsers on your input, fluentbit tries to apply each of them on the same original input and does not apply them one after the other.

When you have multiple multiline parsers, and want them to be applied one after the other, you should use filters, in your case it would be something like that:

[INPUT]
        Name              tail
        Tag               kube.*
        Path              /var/log/containers/*.log
        Read_from_head    true
        Multiline.parser  cri

[FILTER]
        Name                  multiline
        Match                 kube.*
        multiline.key_content log
        multiline.parser      java

I can see in your screenshot, that you are trying to parse java stacttrace, for that you can use build-in java parser, so you do not need multiline-regex-cri.


问:
Need to compare contents of word template & actual PDF
I have 2 files (word & pdf) and need to compare them. word doc will be template which defines how  pdf should be generated. Below are the samples.

Word doc:

```<code>    <firstname>,<LastName>
    <ID>,<organization>
    <salary>,<place>

    Dear <firstname>,
    you are working in the department of <organization> and we are really honored to have you here. Expecting many more successful years of service from you.
    Thanks,
</code>```

Actual PDF:

```<code>    John, Kennedy
    234,google
    USD1245,CA

    Dear John,
    you are working in the department of google and we are really honored to have you here. Expecting many more successful years of service from you.
    Thanks,
</code>```

Can someone help with the comparison logic to validate both the static and dynamic content are getting generated as expected??
we are using TestComplete with JavaScript for the automation.

答:
If you know what is in the template, you could start with some regex splitting first few lines where are all variables. Then read the template, replace every variable with values got from pdf beggining and if the replaced text matches the text in pdf, then you have pdf built from template.

Regex splitting example: read word file, split every line, get the first 3 lines, split them with ",", and set the vars to coresponding indexes.
As I said, this works only if you know the content of the template

Body:
I have 2 files (word & pdf) and need to compare them. word doc will be template which defines how  pdf should be generated. Below are the samples.

Word doc:

    <firstname>,<LastName>
    <ID>,<organization>
    <salary>,<place>

    Dear <firstname>,
    you are working in the department of <organization> and we are really honored to have you here. Expecting many more successful years of service from you.
    Thanks,

Actual PDF:

    John, Kennedy
    234,google
    USD1245,CA

    Dear John,
    you are working in the department of google and we are really honored to have you here. Expecting many more successful years of service from you.
    Thanks,

Can someone help with the comparison logic to validate both the static and dynamic content are getting generated as expected??
we are using TestComplete with JavaScript for the automation.


问:
Javascript access to other page
Can I have access from javascript on one page to elements on other page the script has created before? Something like this:

```<code>var win=window.open("http://www.ftwars.com/battle",'myWindow');
win.document.getElementById('center').onClick();
</code>```

答: You are correct. From the context you have given above, win is like any other object.</p>

Title: Javascript access to other page

Body:
Can I have access from javascript on one page to elements on other page the script has created before? Something like this:

var win=window.open("http://www.ftwars.com/battle",'myWindow');
win.document.getElementById('center').onClick();