问:
Dynamically processing a concurrent collection in parallel by group but serially within each group
I've come across a problem that I can readily define but for the life of me can't seem to digest the MSDN for the best possible solution. It's been a while since I had to actually think about parallel processing outside of UI responsiveness.

So say, I have a Concurrent collection of Tasks that need to be processed. For example maybe it is loading data to various consumers by type (Consumer1, Consumer2, Consumer3...Consumer[N]) the underlying Task of sending the data is the same for each task but each consumer can only accept one source at a time

Basically, I want to process as much in parallel as possible with the caveat that I can only send 1 task to each consumer at a time. So if a current Job for a Consumer is already in progress then I should move to the next item in the collection and leave it for when the Job in progress for that consumer has completed. The Concurrent collection could also be added to at any time externally and if we had new types we'd need additional threads.

I guess what my question boils down to is how do I customize the "Take" from the collection so that I only grab the next Task with a property that designates it has a Consumer that doesn't already have a Job in progress.

Any ideas on what I'm missing here or if I'm even on the right path?

Example we have a Mediator Queue with Tasks associated with Banking transactions.

So we might add to our mediator queue (Let's say send SummaryData and Send TransactionData are using the same interface contract to send data)

SendTransactionData -> Bank1

SendTransactionData -> Bank2

SendSummaryData -> Arbiter

SendTransactionData -> Bank1

SendTransactionData -> Bank3

SendTransactionData -> Bank1

SendTransactionData -> Bank2

1,2,3,5 can be processed in parallel but due to their own system(s) each consumer can only accept one input at a time transaction 4 must wait for transaction 1 to be completed and Transaction 6 must wait for transaction 4 to process. Similarly Transaction 7 must wait for transaction 2.

Before any of the initial processes have completed someone may add another grouping.

SendSummaryData -> Arbiter

SendTransactionData -> Bank1

SendTransactionData -> Bank4

10 can be picked up immediately if a thread is available, but 8 and 9 must be queued behind their other related tasks.

Obviously there would be better ways to design a system to accomplish this but these are essentially the specs I'm looking to satisfy.

答:
Here is an approach that is not based on the TPL Dataflow library. It is based on the Parallel.ForEachAsync API instead (available from .NET 6 onwards). The custom ForEachExclusivePerKeyAsync method below supports all the options and the functionality of the Parallel.ForEachAsync overload that has an IAsyncEnumerable<T> as source. Its behavior in case of errors or cancellation is identical. The only difference is that concurrent operations for elements with the same key are prevented. The key of each element is obtained via a keySelector function. The processing of items with the same key is serialized.

```<code>/// <summary>
/// Executes a for-each operation on an async-enumerable sequence in which
/// iterations may run concurrently, enforcing a non-concurrent execution policy
/// for elements having the same key.
/// </summary>
public static Task ForEachExclusivePerKeyAsync<TSource, TKey>(
    this IAsyncEnumerable<TSource> source,
    ParallelOptions parallelOptions,
    Func<TSource, CancellationToken, ValueTask> body,
    Func<TSource, TKey> keySelector,
    IEqualityComparer<TKey> keyComparer = default)
{
    ArgumentNullException.ThrowIfNull(keySelector);
    // The other arguments are validated by the Parallel.ForEachAsync itself.
    Dictionary<TKey, Queue<TSource>> perKey = new(keyComparer);
    return Parallel.ForEachAsync(source, parallelOptions, async (item, ct) =>
    {
        TKey key = keySelector(item);
        lock (perKey)
        {
            // If there is no other task in-flight with the same key,
            // insert a null queue as an indicator of activity,
            // and start a processing loop for items with this key.
            // Otherwise enqueue this item and return.
            Queue<TSource> queue = CollectionsMarshal.GetValueRefOrAddDefault(
                perKey, key, out bool exists) ??= (exists ? new() : null);
            if (queue is not null)
            {
                queue.Enqueue(item); return;
            }
        }

        // Fire the task for this item, and for all other items with the
        // same key that might be queued while this task is in-flight.
        while (!ct.IsCancellationRequested)
        {
            await body(item, ct); // Continue on captured context
            lock (perKey)
            {
                Queue<TSource> queue = perKey[key];
                if (queue is null || queue.Count == 0)
                {
                    perKey.Remove(key); break;
                }
                item = queue.Dequeue();
            }
        }
    });
}
</code>```

Usage example. A Channel<T> is used as the source/controller of the IAsyncEnumerable<T> sequence:

```<code>var channel = Channel.CreateUnbounded<Transaction>();
//...
var options = new ParallelOptions() { MaxDegreeOfParallelism = 20 };
await ForEachExclusivePerKeyAsync(channel.Reader.ReadAllAsync(), options, async (x, _) =>
{
    await ProcessTransactionAsync(x);
}, keySelector: x => x.Bank);
//...
channel.Writer.TryWrite(new Transaction() { Bank = "Bank1" });
channel.Writer.TryWrite(new Transaction() { Bank = "Bank2" });
</code>```

The ForEachExclusivePerKeyAsync implementation above uses the CollectionsMarshal.GetValueRefOrAddDefault method for updating the perKey dictionary, improving the performance at the cost of readability. For a less performant but more readable version you can look at the 4th revision of this answer.

For a version of the same method that does not depend on the Parallel.ForEachAsync API, and so it can run on .NET versions earlier than 6, you can look at the 3rd revision of this answer.

Body:
Here is an approach that is not based on the TPL Dataflow library. It is based on the Parallel.ForEachAsync API instead (available from .NET 6 onwards). The custom ForEachExclusivePerKeyAsync method below supports all the options and the functionality of the Parallel.ForEachAsync overload that has an IAsyncEnumerable<T> as source. Its behavior in case of errors or cancellation is identical. The only difference is that concurrent operations for elements with the same key are prevented. The key of each element is obtained via a keySelector function. The processing of items with the same key is serialized.

/// <summary>
/// Executes a for-each operation on an async-enumerable sequence in which
/// iterations may run concurrently, enforcing a non-concurrent execution policy
/// for elements having the same key.
/// </summary>
public static Task ForEachExclusivePerKeyAsync<TSource, TKey>(
    this IAsyncEnumerable<TSource> source,
    ParallelOptions parallelOptions,
    Func<TSource, CancellationToken, ValueTask> body,
    Func<TSource, TKey> keySelector,
    IEqualityComparer<TKey> keyComparer = default)
{
    ArgumentNullException.ThrowIfNull(keySelector);
    // The other arguments are validated by the Parallel.ForEachAsync itself.
    Dictionary<TKey, Queue<TSource>> perKey = new(keyComparer);
    return Parallel.ForEachAsync(source, parallelOptions, async (item, ct) =>
    {
        TKey key = keySelector(item);
        lock (perKey)
        {
            // If there is no other task in-flight with the same key,
            // insert a null queue as an indicator of activity,
            // and start a processing loop for items with this key.
            // Otherwise enqueue this item and return.
            Queue<TSource> queue = CollectionsMarshal.GetValueRefOrAddDefault(
                perKey, key, out bool exists) ??= (exists ? new() : null);
            if (queue is not null)
            {
                queue.Enqueue(item); return;
            }
        }

        // Fire the task for this item, and for all other items with the
        // same key that might be queued while this task is in-flight.
        while (!ct.IsCancellationRequested)
        {
            await body(item, ct); // Continue on captured context
            lock (perKey)
            {
                Queue<TSource> queue = perKey[key];
                if (queue is null || queue.Count == 0)
                {
                    perKey.Remove(key); break;
                }
                item = queue.Dequeue();
            }
        }
    });
}

Usage example. A Channel<T> is used as the source/controller of the IAsyncEnumerable<T> sequence:

var channel = Channel.CreateUnbounded<Transaction>();
//...
var options = new ParallelOptions() { MaxDegreeOfParallelism = 20 };
await ForEachExclusivePerKeyAsync(channel.Reader.ReadAllAsync(), options, async (x, _) =>
{
    await ProcessTransactionAsync(x);
}, keySelector: x => x.Bank);
//...
channel.Writer.TryWrite(new Transaction() { Bank = "Bank1" });
channel.Writer.TryWrite(new Transaction() { Bank = "Bank2" });

The ForEachExclusivePerKeyAsync implementation above uses the CollectionsMarshal.GetValueRefOrAddDefault method for updating the perKey dictionary, improving the performance at the cost of readability. For a less performant but more readable version you can look at the 4th revision of this answer.

For a version of the same method that does not depend on the Parallel.ForEachAsync API, and so it can run on .NET versions earlier than 6, you can look at the 3rd revision of this answer.


来源: stackexchange

问:
C# comparing 2 lists of objects while ignoring the key field
I am trying to compare 2 lists of objects for equality, but I want to ignore the key field.
How can I do this?

Here is an example:

```<code>public class myProduct
{
    int ID {get; set;}
    int field1 {get; set;}
    int field2 {get; set;}
}

<List>myProduct greenProducts = new <List>myProduct();
<List>myProduct purpleProducts = new <List>myProduct();

greenProducts.Add(new myProduct { ID = 1, field1 = 5, field2 = 8});
greenProducts.Add(new myProduct { ID = 2, field1 = 9, field2 = 12});
greenProducts.Add(new myProduct { ID = 3, field1 = 7, field2 = 7});

purpleProducts.Add(new myProduct { ID = 8, field1 = 5, field2 = 8});
purpleProducts.Add(new myProduct { ID = 9, field1 = 9, field2 = 12});
purpleProducts.Add(new myProduct { ID = 10, field1 = 7, field2 = 7});
</code>```

If you ignore the key field (aka the ID field), these lists are exactly the same.

So I want my code to do something like this:

```<code>if (purpleProducts == greenProducts) //I WANT THE ID FIELD IGNORED FOR THE COMPARISON SO RESULT WITH ABOVE DATA WOULD BE TRUE
{
  //DO ACTION HERE
}
</code>```

答:
So, no duplicates to worry about, elements could be in any order

```<code>if(
  green.Select(x => new { x.field1, x.field2 })
    .ToHashSet()
    .SetEquals(purple.Select(x => new { x.field1, x.field2 }))
)
</code>```

We take your collections and project them to anonymous types of just the fields you want to compare. An anonymous type has built in equality logic based on the data contents of the properties, which saves you from having to override Equals and GetHashCode in your class (which would be another option). By turning one of the collections into a hashset you can then ask it if it equals the other collection. The other collection need not be a hashset.

A similar effect can be achieved with tuple, which also have built in equality checking

```<code>if(
  green.Select(x => (x.field1, x.field2))
    .ToHashSet()
    .SetEquals(purple.Select(x => (x.field1, x.field2)))
)
</code>```

If you override Equals and GetHashCode on your class so that they declare equal purely based on field1 and field2 the code gets simpler:

```<code>if(green.ToHashSet().SetEquals(purple))
</code>```

The overridden methods would look something like:

```<code>override bool Equals(object other) => other is MyProduct x && this.field1 == x.field1 && this.field2 == x.field2;
override int GetHashCode() => HashCode.Combine(field1, field2);
</code>```

If you make your collections HashSet (with the overrides done above too) instead of List then they automatically dedupe themselves and the code is simply

```<code>if(green.SetEquals(purple))
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I am trying to compare 2 lists of objects for equality, but I want to ignore the key field.
How can I do this?

Here is an example:

public class myProduct
{
    int ID {get; set;}
    int field1 {get; set;}
    int field2 {get; set;}
}

<List>myProduct greenProducts = new <List>myProduct();
<List>myProduct purpleProducts = new <List>myProduct();

greenProducts.Add(new myProduct { ID = 1, field1 = 5, field2 = 8});
greenProducts.Add(new myProduct { ID = 2, field1 = 9, field2 = 12});
greenProducts.Add(new myProduct { ID = 3, field1 = 7, field2 = 7});

purpleProducts.Add(new myProduct { ID = 8, field1 = 5, field2 = 8});
purpleProducts.Add(new myProduct { ID = 9, field1 = 9, field2 = 12});
purpleProducts.Add(new myProduct { ID = 10, field1 = 7, field2 = 7});

If you ignore the key field (aka the ID field), these lists are exactly the same.

So I want my code to do something like this:

if (purpleProducts == greenProducts) //I WANT THE ID FIELD IGNORED FOR THE COMPARISON SO RESULT WITH ABOVE DATA WOULD BE TRUE
{
  //DO ACTION HERE
}

Body:
So, no duplicates to worry about, elements could be in any order

if(
  green.Select(x => new { x.field1, x.field2 })
    .ToHashSet()
    .SetEquals(purple.Select(x => new { x.field1, x.field2 }))
)

We take your collections and project them to anonymous types of just the fields you want to compare. An anonymous type has built in equality logic based on the data contents of the properties, which saves you from having to override Equals and GetHashCode in your class (which would be another option). By turning one of the collections into a hashset you can then ask it if it equals the other collection. The other collection need not be a hashset.

A similar effect can be achieved with tuple, which also have built in equality checking

if(
  green.Select(x => (x.field1, x.field2))
    .ToHashSet()
    .SetEquals(purple.Select(x => (x.field1, x.field2)))
)

If you override Equals and GetHashCode on your class so that they declare equal purely based on field1 and field2 the code gets simpler:

if(green.ToHashSet().SetEquals(purple))

The overridden methods would look something like:

override bool Equals(object other) => other is MyProduct x && this.field1 == x.field1 && this.field2 == x.field2;
override int GetHashCode() => HashCode.Combine(field1, field2);

If you make your collections HashSet (with the overrides done above too) instead of List then they automatically dedupe themselves and the code is simply

if(green.SetEquals(purple))


问:
Bot not counting the amount of reactions discord.js
I'm trying to make a bot like the Mee6 xp system but for specific reactions, I'm stuck on the bot counting the amount of reactions and storing them to display later on. The problem I am facing is that the counter is at 0 no matter what I do.

Edit: Took out the second client.on and now i get the error:
TypeError: Cannot read properties of undefined (reading 'startsWith')"

```<code>client.on('messageReactionAdd', (reaction, message) => {

    console.log(message)
    console.log('upvote')

    if(reaction.emoji.id === '<:Upvote:577977768944467978> '){
        if(message.author.id == '495622453704261662'){
            arusKarma = (arusKarma+1);
        };
    };

        if(!message.content.startsWith(prefix) || message.author.bot) return;

        const args = message.content.slice(prefix.length).split(/ +/);
        const command = args.shift().toLowerCase();

        if(command === 'ping'){
            message.reply('pong');
        };

        if(command === 'karma'){
            if(message.author.id == '495622453704261662'){
                message.reply('You have '+arusKarma+' karma');
            }
        };
    });
});
</code>```

答:
the event 'messageReactionAdd' returns a MessageReaction & User. It does not return a message event.

Instead do

```<code>client.on('messageReactionAdd', (reaction, user) => {
    ...
    const message = reaction.message
    if(!message.content.startsWith(prefix) || message.author.bot) return;
    ...
}
</code>```

Body:
I'm trying to make a bot like the Mee6 xp system but for specific reactions, I'm stuck on the bot counting the amount of reactions and storing them to display later on. The problem I am facing is that the counter is at 0 no matter what I do.

Edit: Took out the second client.on and now i get the error:
TypeError: Cannot read properties of undefined (reading 'startsWith')"

client.on('messageReactionAdd', (reaction, message) => {

    console.log(message)
    console.log('upvote')

    if(reaction.emoji.id === '<:Upvote:577977768944467978> '){
        if(message.author.id == '495622453704261662'){
            arusKarma = (arusKarma+1);
        };
    };

        if(!message.content.startsWith(prefix) || message.author.bot) return;

        const args = message.content.slice(prefix.length).split(/ +/);
        const command = args.shift().toLowerCase();

        if(command === 'ping'){
            message.reply('pong');
        };

        if(command === 'karma'){
            if(message.author.id == '495622453704261662'){
                message.reply('You have '+arusKarma+' karma');
            }
        };
    });
});

Body:
the event 'messageReactionAdd' returns a MessageReaction & User. It does not return a message event.

Instead do

client.on('messageReactionAdd', (reaction, user) => {
    ...
    const message = reaction.message
    if(!message.content.startsWith(prefix) || message.author.bot) return;
    ...
}


问:
Conda does not create environment from yml file on MacOS
I'm trying to create an environment with conda after having installed Miniconda3, on macOS. I am using the conda env create -f SEM1-CB.yml command through terminal but it gives me the following error:

```<code>Collecting package metadata (repodata.json): done
Solving environment: failed

ResolvePackageNotFound:
  - xz==5.2.5=h62dcd97_1
  - mkl-service==2.4.0=py310h2bbff1b_0
  - numpy-base==1.21.2=py310h0829f74_0
  - libzlib==1.2.11=h8ffe710_1013
  - vc==14.2=hb210afc_5
  - lz4-c==1.9.3=h8ffe710_1
  - numpy==1.21.2=py310hfca59bb_0
  - jpeg==9d=h8ffe710_0
  - mkl_random==1.2.2=py310h4ed8f06_0
  - zlib==1.2.11=h8ffe710_1013
  - libffi==3.4.2=h8ffe710_5
  - mkl==2021.4.0=haa95532_640
  - setuptools==58.5.3=py310h5588dad_0
  - mkl_fft==1.3.1=py310ha0764ea_0
  - ca-certificates==2021.10.26=haa95532_4
  - libtiff==4.3.0=hd413186_2
  - openssl==3.0.0=h8ffe710_2
  - intel-openmp==2021.4.0=haa95532_3556
  - libdeflate==1.8=h8ffe710_0
  - zstd==1.5.0=h6255e5f_0
  - python==3.10.0=hcf16a7b_2_cpython
  - vs2015_runtime==14.29.30037=h902a5da_5
  - lcms2==2.12=h2a16943_0
  - lerc==3.0=h0e60522_0
  - openjpeg==2.4.0=hb211442_1
  - pillow==8.4.0=py310h22f3323_0
  - tk==8.6.11=h8ffe710_1
  - ucrt==10.0.20348.0=h57928b3_0
  - freetype==2.10.4=h546665d_1
  - jbig==2.1=h8d14728_2003
  - libpng==1.6.37=h1d00b33_2
  - sqlite==3.36.0=h8ffe710_2
  - bzip2==1.0.8=h8ffe710_4
</code>```

The SEM1-CB.yml file is as follows:

```<code>name: SEM1-CB
channels:
  - conda-forge
  - defaults
dependencies:
  - blas=1.0=mkl
  - bzip2=1.0.8=h8ffe710_4
  - ca-certificates=2021.10.26=haa95532_4
  - freetype=2.10.4=h546665d_1
  - intel-openmp=2021.4.0=haa95532_3556
  - jbig=2.1=h8d14728_2003
  - jpeg=9d=h8ffe710_0
  - lcms2=2.12=h2a16943_0
  - lerc=3.0=h0e60522_0
  - libdeflate=1.8=h8ffe710_0
  - libffi=3.4.2=h8ffe710_5
  - libpng=1.6.37=h1d00b33_2
  - libtiff=4.3.0=hd413186_2
  - libzlib=1.2.11=h8ffe710_1013
  - lz4-c=1.9.3=h8ffe710_1
  - mkl=2021.4.0=haa95532_640
  - mkl-service=2.4.0=py310h2bbff1b_0
  - mkl_fft=1.3.1=py310ha0764ea_0
  - mkl_random=1.2.2=py310h4ed8f06_0
  - numpy=1.21.2=py310hfca59bb_0
  - numpy-base=1.21.2=py310h0829f74_0
  - olefile=0.46=pyh9f0ad1d_1
  - openjpeg=2.4.0=hb211442_1
  - openssl=3.0.0=h8ffe710_2
  - pillow=8.4.0=py310h22f3323_0
  - pip=21.3.1=pyhd8ed1ab_0
  - pysimplegui=4.55.1=pyhd8ed1ab_0
  - python=3.10.0=hcf16a7b_2_cpython
  - python_abi=3.10=2_cp310
  - setuptools=58.5.3=py310h5588dad_0
  - six=1.16.0=pyhd3eb1b0_0
  - sqlite=3.36.0=h8ffe710_2
  - tk=8.6.11=h8ffe710_1
  - tzdata=2021e=he74cb21_0
  - ucrt=10.0.20348.0=h57928b3_0
  - vc=14.2=hb210afc_5
  - vs2015_runtime=14.29.30037=h902a5da_5
  - wheel=0.37.0=pyhd8ed1ab_1
  - xz=5.2.5=h62dcd97_1
  - zlib=1.2.11=h8ffe710_1013
  - zstd=1.5.0=h6255e5f_0
  - pip:
    - click==8.0.3
    - colorama==0.4.4
    - flask==2.0.2
    - itsdangerous==2.0.1
    - jinja2==3.0.3
    - markupsafe==2.0.1
    - pymata4==1.15
    - pyserial==3.5
    - werkzeug==2.0.2
prefix:
</code>```

I need to create and activate this environment in order to add it as interpreter on visual studio code. I have tried the procedure on microsoft windows and it works, but I need it to work even on macOS.

Thank you in advance for any help!

答:
In SEM1-CB.yml file you have Windows-specific conda package builds specified, which aren't available in OSX index, hence ends up showing you ResolvePackageNotFound error. Check the screenshot below I took from: https://anaconda.org/conda-forge/xz/files

To fix this, when you export your conda environment file in Windows run this command: conda env export --from-history --name env_name > file_name.yml or manually remove build numbers from the file.

Body:
I'm trying to create an environment with conda after having installed Miniconda3, on macOS. I am using the conda env create -f SEM1-CB.yml command through terminal but it gives me the following error:

Collecting package metadata (repodata.json): done
Solving environment: failed

ResolvePackageNotFound:
  - xz==5.2.5=h62dcd97_1
  - mkl-service==2.4.0=py310h2bbff1b_0
  - numpy-base==1.21.2=py310h0829f74_0
  - libzlib==1.2.11=h8ffe710_1013
  - vc==14.2=hb210afc_5
  - lz4-c==1.9.3=h8ffe710_1
  - numpy==1.21.2=py310hfca59bb_0
  - jpeg==9d=h8ffe710_0
  - mkl_random==1.2.2=py310h4ed8f06_0
  - zlib==1.2.11=h8ffe710_1013
  - libffi==3.4.2=h8ffe710_5
  - mkl==2021.4.0=haa95532_640
  - setuptools==58.5.3=py310h5588dad_0
  - mkl_fft==1.3.1=py310ha0764ea_0
  - ca-certificates==2021.10.26=haa95532_4
  - libtiff==4.3.0=hd413186_2
  - openssl==3.0.0=h8ffe710_2
  - intel-openmp==2021.4.0=haa95532_3556
  - libdeflate==1.8=h8ffe710_0
  - zstd==1.5.0=h6255e5f_0
  - python==3.10.0=hcf16a7b_2_cpython
  - vs2015_runtime==14.29.30037=h902a5da_5
  - lcms2==2.12=h2a16943_0
  - lerc==3.0=h0e60522_0
  - openjpeg==2.4.0=hb211442_1
  - pillow==8.4.0=py310h22f3323_0
  - tk==8.6.11=h8ffe710_1
  - ucrt==10.0.20348.0=h57928b3_0
  - freetype==2.10.4=h546665d_1
  - jbig==2.1=h8d14728_2003
  - libpng==1.6.37=h1d00b33_2
  - sqlite==3.36.0=h8ffe710_2
  - bzip2==1.0.8=h8ffe710_4

The SEM1-CB.yml file is as follows:

name: SEM1-CB
channels:
  - conda-forge
  - defaults
dependencies:
  - blas=1.0=mkl
  - bzip2=1.0.8=h8ffe710_4
  - ca-certificates=2021.10.26=haa95532_4
  - freetype=2.10.4=h546665d_1
  - intel-openmp=2021.4.0=haa95532_3556
  - jbig=2.1=h8d14728_2003
  - jpeg=9d=h8ffe710_0
  - lcms2=2.12=h2a16943_0
  - lerc=3.0=h0e60522_0
  - libdeflate=1.8=h8ffe710_0
  - libffi=3.4.2=h8ffe710_5
  - libpng=1.6.37=h1d00b33_2
  - libtiff=4.3.0=hd413186_2
  - libzlib=1.2.11=h8ffe710_1013
  - lz4-c=1.9.3=h8ffe710_1
  - mkl=2021.4.0=haa95532_640
  - mkl-service=2.4.0=py310h2bbff1b_0
  - mkl_fft=1.3.1=py310ha0764ea_0
  - mkl_random=1.2.2=py310h4ed8f06_0
  - numpy=1.21.2=py310hfca59bb_0
  - numpy-base=1.21.2=py310h0829f74_0
  - olefile=0.46=pyh9f0ad1d_1
  - openjpeg=2.4.0=hb211442_1
  - openssl=3.0.0=h8ffe710_2
  - pillow=8.4.0=py310h22f3323_0
  - pip=21.3.1=pyhd8ed1ab_0
  - pysimplegui=4.55.1=pyhd8ed1ab_0
  - python=3.10.0=hcf16a7b_2_cpython
  - python_abi=3.10=2_cp310
  - setuptools=58.5.3=py310h5588dad_0
  - six=1.16.0=pyhd3eb1b0_0
  - sqlite=3.36.0=h8ffe710_2
  - tk=8.6.11=h8ffe710_1
  - tzdata=2021e=he74cb21_0
  - ucrt=10.0.20348.0=h57928b3_0
  - vc=14.2=hb210afc_5
  - vs2015_runtime=14.29.30037=h902a5da_5
  - wheel=0.37.0=pyhd8ed1ab_1
  - xz=5.2.5=h62dcd97_1
  - zlib=1.2.11=h8ffe710_1013
  - zstd=1.5.0=h6255e5f_0
  - pip:
    - click==8.0.3
    - colorama==0.4.4
    - flask==2.0.2
    - itsdangerous==2.0.1
    - jinja2==3.0.3
    - markupsafe==2.0.1
    - pymata4==1.15
    - pyserial==3.5
    - werkzeug==2.0.2
prefix:

I need to create and activate this environment in order to add it as interpreter on visual studio code. I have tried the procedure on microsoft windows and it works, but I need it to work even on macOS.

Thank you in advance for any help!


问:
Type error: not all arguments converted during string formatting when using %
```<code>class car(object):
    def __init__(self, make, model,year,condition,kms):
        self.make = make
        self.model = model
        self.year = year
        self.condition = condition
        self.kms = kms

    def display(self, showAll):
        if showAll:
            print("This car is a %s %s from %s, it is %s and has $s kms." %(self.make, self.model, self.year, self.condition, self.kms))
        else:
            print("This car is a %s %s from %s." %(self.make, self.model, self.year))

whip = car('Ford', 'Fusion', 2012, 'New', 0)
whip.display(True)  #when this is false it works, when true I get the error.
</code>```

答:
You have $s in

```<code>print("This car is a %s %s from %s, it is %s and has $s kms." %(self.make, self.model, self.year, self.condition, self.kms))
</code>```

Change it to %s

```<code>print("This car is a %s %s from %s, it is %s and has %s kms." %(self.make, self.model, self.year, self.condition, self.kms))
</code>```

Body:
You have $s in

print("This car is a %s %s from %s, it is %s and has $s kms." %(self.make, self.model, self.year, self.condition, self.kms))

Change it to %s

print("This car is a %s %s from %s, it is %s and has %s kms." %(self.make, self.model, self.year, self.condition, self.kms))


问:
Gensim - index_to_key gives numbers instead of words as output
I'm trying to get 5 most frequent words from word2vec model created from wikipedia dump. I turned this model into KeyedVectors and the code looks like that:

```<code> from gensim.models import KeyedVectors

   def vocabulary():
        model = KeyedVectors.load("vectors.kv")
        result = model.index_to_key[:5]
        print(result)
</code>```

The result is:
[(507, 1), (858, 1), (785, 1), (251, 1), (9807, 1)]

On the other hand, when i'm trying the same with model made of tokenized text the result is:
['pdop', 'podany', 'wału', 'wytrzymałości', 'skręcanie']

Why am I getting numbers instead of words from the first model?

I used .get_texts() function, but I can't split the result of this function and pass it as sentences value to initiate the model, so I tried creating model with one short article, and then train it with data from .get_texts() article by article like this:

```<code>    wiki_txt = wiki.get_texts()
    model = Word2Vec.load('wiki_w2v.model')
    for i in wiki_txt:
       article = list(i)
       model.train(article,total_examples = 1, epochs = 1)
</code>```

Even if at this point article is a list[str], model still can't learn the data.

答:
In a KeyedVectors from a Gensim Word2Vec model, the index_to_key property is usually a plain Python list, where each entry is a string word.

That yours are not this suggests something atypical happened during training. Perhaps the corpus that was fed to the Word2Vec model was made up of lists-of-tuples, rather than lists-of-string-tokens?

You should go back to the code which initially created & saved that vectors.kv file, with special attention to the corpus that was used for training. (What was its first element? Didi it look like a usual text-of-words – a list of strings – or something else?)

Updates based on comments below:

It looks like you are providing an literal instance of WikiCorpus to Word2Vec as the sentences argument. That's not the sort of corpus – a sequence where each item is a list-of-words – that Word2Vec expects.

The iterator returned by wiki.get_texts() is closer – it's the right format, a sequence-of-lists-of-words –but it only passes over the dump once, wherease Word2Vec needs something it can re-iterate over for multiple passes.

I recommend one of these three options:

Writing your own wrapper iterable class, which will call wiki.get_texts() again each time a new iteration is requested; or...

Writing some code to iterate through wiki.get_texts() once, writing each text into a giant plain-text file, with one article per line. Then, run Word2Vec using that file as input; or...

If you're one a giant-memory machine that can load the whole corpus as a list, you could do corpus_list = list(wiki.get_texts(), then pass that corpus_list as the training data to Word2Vec.

Separately:

If your only goal is doing a Word2Vec training, you don't really need to do the long-running expensive initial load of the WikiCorpus (`wiki = WikiCorpus('plwiki-latest-pages-articles.xml.bz2') then re-save. That does a time-consuming vocabulary survey of the dump that isn't even consulted by later word2vec training, which does its own survey.

Instead, when you need to use the dump, you can just do:

wiki = WikiCorpus('plwiki-latest-pages-articles.xml.bz2', dictionary=dict())
```

That leaves the wiki object ready for operations like .get_texts(), without wastefully creating a redundant dictionary.

Body:
I'm trying to get 5 most frequent words from word2vec model created from wikipedia dump. I turned this model into KeyedVectors and the code looks like that:

 from gensim.models import KeyedVectors

   def vocabulary():
        model = KeyedVectors.load("vectors.kv")
        result = model.index_to_key[:5]
        print(result)

The result is:
[(507, 1), (858, 1), (785, 1), (251, 1), (9807, 1)]

On the other hand, when i'm trying the same with model made of tokenized text the result is:
['pdop', 'podany', 'wału', 'wytrzymałości', 'skręcanie']

Why am I getting numbers instead of words from the first model?

I used .get_texts() function, but I can't split the result of this function and pass it as sentences value to initiate the model, so I tried creating model with one short article, and then train it with data from .get_texts() article by article like this:

    wiki_txt = wiki.get_texts()
    model = Word2Vec.load('wiki_w2v.model')
    for i in wiki_txt:
       article = list(i)
       model.train(article,total_examples = 1, epochs = 1)

Even if at this point article is a list[str], model still can't learn the data.

Body:
In a KeyedVectors from a Gensim Word2Vec model, the index_to_key property is usually a plain Python list, where each entry is a string word.

That yours are not this suggests something atypical happened during training. Perhaps the corpus that was fed to the Word2Vec model was made up of lists-of-tuples, rather than lists-of-string-tokens?

You should go back to the code which initially created & saved that vectors.kv file, with special attention to the corpus that was used for training. (What was its first element? Didi it look like a usual text-of-words – a list of strings – or something else?)

Updates based on comments below:

It looks like you are providing an literal instance of WikiCorpus to Word2Vec as the sentences argument. That's not the sort of corpus – a sequence where each item is a list-of-words – that Word2Vec expects.

The iterator returned by wiki.get_texts() is closer – it's the right format, a sequence-of-lists-of-words –but it only passes over the dump once, wherease Word2Vec needs something it can re-iterate over for multiple passes.

I recommend one of these three options:

Writing your own wrapper iterable class, which will call wiki.get_texts() again each time a new iteration is requested; or...

Writing some code to iterate through wiki.get_texts() once, writing each text into a giant plain-text file, with one article per line. Then, run Word2Vec using that file as input; or...

If you're one a giant-memory machine that can load the whole corpus as a list, you could do corpus_list = list(wiki.get_texts(), then pass that corpus_list as the training data to Word2Vec.

Separately:

If your only goal is doing a Word2Vec training, you don't really need to do the long-running expensive initial load of the WikiCorpus (`wiki = WikiCorpus('plwiki-latest-pages-articles.xml.bz2') then re-save. That does a time-consuming vocabulary survey of the dump that isn't even consulted by later word2vec training, which does its own survey.

Instead, when you need to use the dump, you can just do:

wiki = WikiCorpus('plwiki-latest-pages-articles.xml.bz2', dictionary=dict())

That leaves the wiki object ready for operations like .get_texts(), without wastefully creating a redundant dictionary.


问:
bash optional command in variable
i have a code:

```<code>L12(){
echo -e "/tftpboot/log/archive/L12/*/*$sn*L12*.log /tftpboot/log/diag/*$sn*L12*.log"
command="| grep -v hdd"
}
</code>```
```<code>getlog(){
echo $(ls -ltr $(${1}) 2>/dev/null `${command}` | tail -1)
}
</code>```

however $command does not seem to be inserting | grep -v hdd correctly
i need $command to be either empty or | grep
is there a simple solution to my issue or should i go for different approach

edit:

there may be another problem in there

i am loading a few "modules"

EVAL.sh

```<code>ev(){
case "${1}" in
*FAIL*) paint $red "FAIL";;
*PASS*) paint $green "PASS";;
*)echo;;
esac
result=${1}
}
</code>```

rackinfo.sh (the "main script")

```<code>#! /bin/bash
#set -x
    n=0
    for src in $(ls modules/)
        do
            source modules/$src && ((n++))
## debugging
# source src/$src || ((n++)) || echo "there may be an issue in $src"
    done
## debugging
# x=($n - $(ls | grep src | wc -l))
# echo -e "$x plugin(s) failed to laod correctly"
# echo -e "loaded $n modules"
########################################################################
command=cat
tests=("L12" "AL" "BI" "L12-3")
while read sn
        do
                paint $blue "$sn\t"
                for test in ${tests[@]}
                        do
                                log="$(ev "$(getlog ${test})")"
                                if [[ -z ${log} ]]
                                        then
                                                paint $cyan "${test} "; paint $red "!LOG "
                                else
                                                paint $cyan "${test} ";echo -ne "$log "

                                fi
done
echo
done <$1
</code>```

the results i get are still containing "hdd" for L12()

答:
Set command to cat as a default.

Also, it's best to use an array for commands with arguments, in case any of the arguments is multiple words.

There's rarely a reason to write echo $(command). That's essentially the same as just writing command.

```<code>#default command does nothing
command=(cat)

L12(){
    echo -e "/tftpboot/log/archive/L12/*/*$sn*L12*.log /tftpboot/log/diag/*$sn*L12*.log"
    command=(grep -v hdd)
}

getlog(){
    ls -ltr $(${1}) 2>/dev/null | "${command[@]}" | tail -1)
}
</code>```

Title: bash optional command in variable

Body:
i have a code:

L12(){
echo -e "/tftpboot/log/archive/L12/*/*$sn*L12*.log /tftpboot/log/diag/*$sn*L12*.log"
command="| grep -v hdd"
}

getlog(){
echo $(ls -ltr $(${1}) 2>/dev/null `${command}` | tail -1)
}

however $command does not seem to be inserting | grep -v hdd correctly
i need $command to be either empty or | grep
is there a simple solution to my issue or should i go for different approach

edit:

there may be another problem in there

i am loading a few "modules"

EVAL.sh

ev(){
case "${1}" in
*FAIL*) paint $red "FAIL";;
*PASS*) paint $green "PASS";;
*)echo;;
esac
result=${1}
}

rackinfo.sh (the "main script")

#! /bin/bash
#set -x
    n=0
    for src in $(ls modules/)
        do
            source modules/$src && ((n++))
## debugging
# source src/$src || ((n++)) || echo "there may be an issue in $src"
    done
## debugging
# x=($n - $(ls | grep src | wc -l))
# echo -e "$x plugin(s) failed to laod correctly"
# echo -e "loaded $n modules"
########################################################################
command=cat
tests=("L12" "AL" "BI" "L12-3")
while read sn
        do
                paint $blue "$sn\t"
                for test in ${tests[@]}
                        do
                                log="$(ev "$(getlog ${test})")"
                                if [[ -z ${log} ]]
                                        then
                                                paint $cyan "${test} "; paint $red "!LOG "
                                else
                                                paint $cyan "${test} ";echo -ne "$log "

                                fi
done
echo
done <$1

the results i get are still containing "hdd" for L12()

Body:
Set command to cat as a default.

Also, it's best to use an array for commands with arguments, in case any of the arguments is multiple words.

There's rarely a reason to write echo $(command). That's essentially the same as just writing command.

#default command does nothing
command=(cat)

L12(){
    echo -e "/tftpboot/log/archive/L12/*/*$sn*L12*.log /tftpboot/log/diag/*$sn*L12*.log"
    command=(grep -v hdd)
}

getlog(){
    ls -ltr $(${1}) 2>/dev/null | "${command[@]}" | tail -1)
}


问:
NSSM and log monitoring via Filebeat
I'm working on adding log analytics for a simple application. The application is a Windows Service app hosted using NSSM and logs are going to ELK stack (Elastic Search + Kibana). Unfortunately, the app does not utilize a logging framework. It logs to console, and NSSM parses the stdout and stderr and creates log files. The problem is that the multiline logs are completely messed up. NSSM puts the multi-line with carriage returns into multiple log statements with time stamps, which causes the logs to be very noisy and fragmented on Kibana.

This is an example of the logs:

```<code>[2/1/2022 10:15:39 PM] Host initialized (4321ms)
[2/1/2022 10:15:40 PM] Host started (4342ms)
[2/1/2022 10:15:40 PM] Job host started
Listening on http://localhost:7071/
Hit CTRL-C to exit...
[2/1/2022 10:15:57 PM] Executing HTTP request: {
[2/1/2022 10:15:57 PM]   "requestId": "[OMITTED]",
[2/1/2022 10:15:57 PM]   "method": "GET",
[2/1/2022 10:15:57 PM]   "uri": "/"
[2/1/2022 10:15:57 PM] }
</code>```

As you can see in the above example, we have multi-line log statements that are turned into multiple log statements with timestamps for each line. Any suggestions on how to make the data less fragmented and format the data, so that it appears cleaner on Kibana?

Obviously the below solutions come to my mind:

Use a standard logging framework like Log4Net that allows us to specify log severity and have multiline logs per one log statement and configure our logger to

Use Regex in the filebeat config to fix the multi-line issue. The problem is that I don't have a particular pattern to match on.

Use logstash for data processing?

Any help would be appreciated. Thanks!

答:
Filebeat can deal with multiline by itself (https://www.elastic.co/guide/en/beats/filebeat/current/multiline-examples.html)

However, if logs look like this - I really doubt it can do better than NSSM did no clear pattern.

Logstash won't help either - it's also pattern-based.

And I must notice that's really quite unusual pain in the bottom area to have a Windows Service spitting output to "console" only.

So if rebuilding your Win Service with a logging framework IS an option - I strongly suggest to go with it.

Or at least make it start message with something uniformal - like timestamp.

Title: NSSM and log monitoring via Filebeat

Body:
I'm working on adding log analytics for a simple application. The application is a Windows Service app hosted using NSSM and logs are going to ELK stack (Elastic Search + Kibana). Unfortunately, the app does not utilize a logging framework. It logs to console, and NSSM parses the stdout and stderr and creates log files. The problem is that the multiline logs are completely messed up. NSSM puts the multi-line with carriage returns into multiple log statements with time stamps, which causes the logs to be very noisy and fragmented on Kibana.

This is an example of the logs:

[2/1/2022 10:15:39 PM] Host initialized (4321ms)
[2/1/2022 10:15:40 PM] Host started (4342ms)
[2/1/2022 10:15:40 PM] Job host started
Listening on http://localhost:7071/
Hit CTRL-C to exit...
[2/1/2022 10:15:57 PM] Executing HTTP request: {
[2/1/2022 10:15:57 PM]   "requestId": "[OMITTED]",
[2/1/2022 10:15:57 PM]   "method": "GET",
[2/1/2022 10:15:57 PM]   "uri": "/"
[2/1/2022 10:15:57 PM] }

As you can see in the above example, we have multi-line log statements that are turned into multiple log statements with timestamps for each line. Any suggestions on how to make the data less fragmented and format the data, so that it appears cleaner on Kibana?

Obviously the below solutions come to my mind:

Use a standard logging framework like Log4Net that allows us to specify log severity and have multiline logs per one log statement and configure our logger to

Use Regex in the filebeat config to fix the multi-line issue. The problem is that I don't have a particular pattern to match on.

Use logstash for data processing?

Any help would be appreciated. Thanks!


问:
JSON.stringify is distorting code adding \ inside strings?
I am trying to save local storage info but what happens is I am getting \ added to all the items that are strings I mean as some answers stated it is caused by double stringfy ? issue is my blob action that saves the local storage refuse to handle the it unless is it is stringifed, long story short could this double stringfy needed for the function cause issues later, I have no problem when parsing back but the look of the saved elements looks really weird  ?

//const ls = JSON.stringify(localStorage);
// "id":"\"159e17e9-19b7-453d-8e10-a411c7424586\"
// "groups":"{\"bee7bdc4-d888-46e6-93d7-ed0c\" : :[{\"id\":\"0e6e6426-4d79-4eea-9180-06111dd2a0e3\"}]

const config = {a: 'fdfgdg', b: 2}
console.log(JSON.stringify(config))```

答:
There's no "extra backslash", it's just your means of output show that extra backslash, because it is the way it renders string values (you probably use browser console for that, didn't you?).

Be sure, the actual stringified value does not contain any characters that are not supposed to be there.


问:
How to check if a font is available in version of iOS?
I'm currently working on an app that uses the "ChalkboardSE-Regular" font and my deployment target is 4.0+.  This font was not available in 4.1 but it is supported in 4.3.  What would be the best way to go about checking if the font exists and if it does not, use another supported font on the <4.1 versions of iOS. [UIFont familyName] returns a list of these fonts "Chalkboard SE"

Thanks in advance!

T

答:
I figured that using swiftui preview, you can make a app that shows what each font looks like using the preview simulator.

struct TitleViewModifier_Previews:
    PreviewProvider {

    static var previews: some View {

        List{
            ForEach(UIFont.familyNames.sorted(),id: \.self){ family in
                ForEach(UIFont.fontNames(forFamilyName: family), id: \.self){ eachFont in
                    Text(eachFont)
                        .font(.custom(eachFont, size: 22))
                }
            }
        }
    }
}
```

Here is what my preview looks like
Press the play button to make the list interactive and scrollable

ContentLicense: CC BY-SA 3.0

Body:
I figured that using swiftui preview, you can make a app that shows what each font looks like using the preview simulator.

struct TitleViewModifier_Previews:
    PreviewProvider {

    static var previews: some View {

        List{
            ForEach(UIFont.familyNames.sorted(),id: \.self){ family in
                ForEach(UIFont.fontNames(forFamilyName: family), id: \.self){ eachFont in
                    Text(eachFont)
                        .font(.custom(eachFont, size: 22))
                }
            }
        }
    }
}

Here is what my preview looks like
Press the play button to make the list interactive and scrollable


问:
Can I iterate through other resources in main.tf in readResource() when developing a TF provider?
Context: I'm developing a TF provider (here's the official guide from HashiCorp).

I run into the following situation:

```<code># main.tf
resource "foo" "example" {
   id = "foo-123"
   name = "foo-name"
   lastname = "foo-lastname"
}

resource "bar" "example" {
   id = "bar-123"
   parent_id = foo.example.id
   parent_name = foo.example.name
   parent_lastname = foo.example.lastname
}
</code>```

where I have to declare parent_name and parent_lastname (effectively duplicate them) explicitly to be able to read these values that are necessary for read / create request for Bar resource.

Is it possible to use a fancy trick with d *schema.ResourceData in

```<code>func resourceBarRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
</code>```

to avoid duplicated in my TF config, i.e. have just:

```<code>resource "foo" "example" {
   id = "foo-123"
   name = "foo-name"
   lastname = "foo-lastname"
}

resource "bar" "example" {
   id = "bar-123"
   parent_id = foo.example.id
}
</code>```

infer foo.example.name and foo.example.lastname just based on foo.example.id in resourceBarRead() somehow so I won't have to duplicate those fields in both resources?

Obviously, this is minimal example and let's assume I need both foo.example.name and foo.example.lastname to send a read / create request for Bar resource? In other words, can I iterate through other resource in TF state / main.tf file based on target ID to find its other attributes? It seems to be a useful feauture howerever it's not mentioned in HashiCorp's guide so I guess it's undesirable and I have to duplicate those fields.

答:
In Terraform's provider/resource model, each resource block is independent of all others unless the user explicitly connects them using references like you showed.

However, in most providers it's sufficient to pass only the id (or similar unique identifier) attribute downstream to create a relationship like this, because the other information about that object is already known to the remote system.

Without knowledge about the particular remote system you are interacting with, I would expect that you'd be able to use the value given as parent_id either directly in an API call (and thus have the remote system connect it with the existing object), or to make an additional read request to the remote API to look up the object using that ID and obtain the name and lastname values that were saved earlier.

If those values only exist in the provider's context and not in the remote API then I don't think there will be any alternative but to have the user pass them in again, since that is the only way that local values (as opposed to values persisted in the remote API) can travel between resources.

Body:
Context: I'm developing a TF provider (here's the official guide from HashiCorp).

I run into the following situation:

# main.tf
resource "foo" "example" {
   id = "foo-123"
   name = "foo-name"
   lastname = "foo-lastname"
}

resource "bar" "example" {
   id = "bar-123"
   parent_id = foo.example.id
   parent_name = foo.example.name
   parent_lastname = foo.example.lastname
}

where I have to declare parent_name and parent_lastname (effectively duplicate them) explicitly to be able to read these values that are necessary for read / create request for Bar resource.

Is it possible to use a fancy trick with d *schema.ResourceData in

func resourceBarRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

to avoid duplicated in my TF config, i.e. have just:

resource "foo" "example" {
   id = "foo-123"
   name = "foo-name"
   lastname = "foo-lastname"
}

resource "bar" "example" {
   id = "bar-123"
   parent_id = foo.example.id
}

infer foo.example.name and foo.example.lastname just based on foo.example.id in resourceBarRead() somehow so I won't have to duplicate those fields in both resources?

Obviously, this is minimal example and let's assume I need both foo.example.name and foo.example.lastname to send a read / create request for Bar resource? In other words, can I iterate through other resource in TF state / main.tf file based on target ID to find its other attributes? It seems to be a useful feauture howerever it's not mentioned in HashiCorp's guide so I guess it's undesirable and I have to duplicate those fields.


问:
In OpenTBS: How to use "new paragraph" carriage returns instead of "new line" in MS Word Templates
I have a PHP variable holding plain text that i need to insert into a MS Word Template using OpenTBS.

My Word placeholder is:

```<code>[onshow.introduction]
</code>```

The problem is I use justification formatting, so I need actual "new paragraph" ie "carriage returns" (Enter) in Word. Instead all my newlines are translated into "new line" (shift-Enter), messing up my formatting.

I tried inserting
, \r, \n, \n\r, \r\n, PHP_EOL - all of them just create newline characters instead of new paragraph characters in MS Word.

Is there any way to get "new paragraph" characters in Word from OpenTBS?

答:
In Ms Word, a new paragraph is a complicated XML entity. Something like:

```<code><w:p><w:r><w:t>My paragraph</w:t></w:r></w:p>
</code>```

and it can have extra attributes and extra sub-entities that can appear at several places.

So it would be very touchy and hazardous to replace your line-breaks with </w:t></w:r><w:r><w:t>.

The best and robust solution is to explode your text in several parts, and merge it on a block bounded an Ms Word paragraph.

Example :

PHP:

```<code>// uniformize line breaks
$introduction = str_replace("\n", "\r", $introduction);
$introduction = str_replace("\r\r", "\r", $introduction);

// explode in several parts
$introduction = explode("\r", $introduction);

// merge on a block
$TBS->MergeBlock('intro', $introduction);
</code>```

DOCX:

```<code>[intro.val;block=tbs:p]
</code>```

Body:
I have a PHP variable holding plain text that i need to insert into a MS Word Template using OpenTBS.

My Word placeholder is:

[onshow.introduction]

The problem is I use justification formatting, so I need actual "new paragraph" ie "carriage returns" (Enter) in Word. Instead all my newlines are translated into "new line" (shift-Enter), messing up my formatting.

I tried inserting
, \r, \n, \n\r, \r\n, PHP_EOL - all of them just create newline characters instead of new paragraph characters in MS Word.

Is there any way to get "new paragraph" characters in Word from OpenTBS?

Body:
In Ms Word, a new paragraph is a complicated XML entity. Something like:

<w:p><w:r><w:t>My paragraph</w:t></w:r></w:p>

and it can have extra attributes and extra sub-entities that can appear at several places.

So it would be very touchy and hazardous to replace your line-breaks with </w:t></w:r><w:r><w:t>.

The best and robust solution is to explode your text in several parts, and merge it on a block bounded an Ms Word paragraph.

Example :

PHP:

// uniformize line breaks
$introduction = str_replace("\n", "\r", $introduction);
$introduction = str_replace("\r\r", "\r", $introduction);

// explode in several parts
$introduction = explode("\r", $introduction);

// merge on a block
$TBS->MergeBlock('intro', $introduction);

DOCX:

[intro.val;block=tbs:p]


问:
Iterate through Python for loop more quickly
I have a Pandas data frame (called "ud_flex" below) that looks like the one below:

The data frame has over 27 million observations in it that I'm trying to iterate through to do a calculation for each row. Below is the calculation that I'm using:

```<code>def set_fpts(pos, rank, curr_fpts):
    if pos == "RB" and rank >= 3.0:
        return 0
    elif pos == "WR" and rank >= 4.0:
        return 0
    elif (pos == "TE" or pos == "QB") and rank >= 2.0:
        return 0
    else:
        return curr_fpts
</code>```

Here is the for loop that I've created:

```<code>players = ud_flex.shape[0]

for i in range(0,players):
    new_fpts = set_fpts(ud_flex.iloc[i]['position_name'], ud_flex.iloc[i]['wk_rank_orig'], ud_flex.iloc[i]['fpts'])
    ud_flex.at[i, 'fpts_orig'] = new_fpts
</code>```

Does anyone have any suggestions for how to speed up this loop? It's currently taking nearly an hour! Thanks!

答:
In general, iterating through pandas data frames is slow, so it's not surprising that your for loop based approach is taking a while.

I suspect that the following alternative should work more quickly for a data frame of your size.

```<code>mask = (((ud_flex['position_name']=="RB") & (ud_flex['wk_rank_orig']>=3))
       |((ud_flex['position_name']=="WR") & (ud_flex['wk_rang_orig']>=4))
       |((ud_flex['position_name'].isin["TE","QB"]) & (ud_flex['wk_rang_orig']>=2)))
ud_flex['fpts_orig'][mask] = 0
ud_flex['fpts_orig'][~mask] = ud_flex['fpts']
</code>```

Body:
I have a Pandas data frame (called "ud_flex" below) that looks like the one below:

The data frame has over 27 million observations in it that I'm trying to iterate through to do a calculation for each row. Below is the calculation that I'm using:

def set_fpts(pos, rank, curr_fpts):
    if pos == "RB" and rank >= 3.0:
        return 0
    elif pos == "WR" and rank >= 4.0:
        return 0
    elif (pos == "TE" or pos == "QB") and rank >= 2.0:
        return 0
    else:
        return curr_fpts

Here is the for loop that I've created:

players = ud_flex.shape[0]

for i in range(0,players):
    new_fpts = set_fpts(ud_flex.iloc[i]['position_name'], ud_flex.iloc[i]['wk_rank_orig'], ud_flex.iloc[i]['fpts'])
    ud_flex.at[i, 'fpts_orig'] = new_fpts

Does anyone have any suggestions for how to speed up this loop? It's currently taking nearly an hour! Thanks!

Body:
In general, iterating through pandas data frames is slow, so it's not surprising that your for loop based approach is taking a while.

I suspect that the following alternative should work more quickly for a data frame of your size.

mask = (((ud_flex['position_name']=="RB") & (ud_flex['wk_rank_orig']>=3))
       |((ud_flex['position_name']=="WR") & (ud_flex['wk_rang_orig']>=4))
       |((ud_flex['position_name'].isin["TE","QB"]) & (ud_flex['wk_rang_orig']>=2)))
ud_flex['fpts_orig'][mask] = 0
ud_flex['fpts_orig'][~mask] = ud_flex['fpts']


问:
Why is String.Format static?
Compare

```<code>String.Format("Hello {0}", "World");
</code>```

with

```<code>"Hello {0}".Format("World");
</code>```

Why did the .Net designers choose a static method over an instance method? What do you think?

答: I think it is because Format doesn't take a string per se, but a "format string". Most strings are equal to things like "Bob Smith" or "1010 Main St" or what have you and not to "Hello {0}", generally you only put those format strings in when you are trying to use a template to create another string, like a factory method, and therefore it lends it self to a static method.</p>

OwnerDisplayName: Jakub Šturc

ContentLicense: CC BY-SA 2.5

Title: Why is String.Format static?

Body:
Compare

String.Format("Hello {0}", "World");

with

"Hello {0}".Format("World");

Why did the .Net designers choose a static method over an instance method? What do you think?

LastEditorDisplayName: Orion Edwards

OwnerDisplayName: Kearns


问:
Could managed code (specifically .NET) ever become 'unmanaged'?
Recently I was talking with a friend of mine who had started a C++ class a couple months ago (his first exposure to programming). We got onto the topic of C# and .NET generally, and he made the point to me that he felt it was 'doomed' for all of the commonly-cited issues (low speed, breakable bytecode, etc). I agreed with him on all those issues, but I held back in saying it was doomed, only because I felt that, in time, languages like C# could instead become native code (if Microsoft so chose to change the implementation of .NET from a bytecode, JIT runtime environemnent to one which compiles directly to native code like your C++ program does).

My question is, am I out to lunch here? I mean, it may take a lot of work (and may break too many things), but there isn't some type of magical barrier which prevents C# code from being compiled natively (if one wanted to do it), right? There was a time where C++ was considered a very high-level language (which it still is, but not as much as in the past) yet now it's the bedrock (along with C) for Microsoft's native APIs. The idea that .NET could one day be on the same level as C++ in that respect seems only to be a matter of time and effort to me, not some fundamental flaw in the design of the language.

EDIT: I should add that if native compilation of .NET is possible, why does Microsoft choose not to go that route? Why have they chosen the JIT bytecode path?

答: Are you suggesting that the fact that C# is managed code is a design flaw?? </p>

OwnerDisplayName: GRB

LastEditorDisplayName: Joel Coehoorn

OwnerDisplayName: BFree


问:
How do you inject build-specific configuration into an APK?
I have an Android app which I distribute to several different market providers; the app is functionally the same for each, but requires certain distinct configuration parameters.  Ideally I would like to produce all the individual APKs in one build step.  Is there an "official" way to achieve this?

I am considering an approach in which I have a template file in the project structure, and at build time use ant to interpolate values into an output file such as res/xml/config.xml.

Thanks in advance!

答:
I use a library project that has all the code and two (or more) projects referencing that library.

The library project has a resource file called config.xml in its res folder and this file is overridden in the other projects.
that way I can have different application settings and each build has its own manifest.xml so you can really configure things differently for each build type.

This also allows you to have specific code/permissions/... that is only required by a specific build and makes the build process very easy.


问:
Does navigator.mediaDevices.getUserMedia work on hybrid apps?
basic question here that I have been unable to get an answer from.

Does  navigator.mediaDevices.getUserMedia work in a hybrid app?

Let me expand on this a bit.

I have a hybrid app currently that I am trying to transition to use mainly web apis, while for the most part everything is working as expected I am unable to get the camera to be accessable in an 'app' form.

When I run the code

```<code>navigator.mediaDevices.getUserMedia
</code>```

I get back one of two errors (depending on the hybrid framework I use)

Dom message stating permission is denied

Dom message stating video stream is not ready

Now, item one would indicate that the user has not 'allowed' permissions to the actual camera but the catch is, I never get prompted to grant permissions in-app.

For number two, that would seem to indicate that the stream is already in use (have seen some stating this too can be related to permissions so I'm leaning toward that assumption)

So I guess back to the question at hand and that is, is this even possible in a hybrid app? I have seen many of similar questions with similar Dom messages but non seem to have an answer in the sense of is it even possible? I have seen some mention setting the 'feature-policy' headers but in the context of a hybrid app, running a localhost how or where is this set?

Anyhow, just wanted to ask and see if anyone else has been successful in doing this or do we need to use the native plugins for this?

Thanks again!

答:
Yes, it’s possible to use it, I have a sample app that uses it
https://github.com/jcesarmobile/can-i-use-on-webview/tree/master/getusermedia

You have to provide certain permissions on android and add certain usage descriptions on iOS.

The example app only provides camera permission/usage description because it doesn’t capture audio.

Doesn’t work on all iOS versions, I think the first version to support it was 14.5.

Body:
basic question here that I have been unable to get an answer from.

Does  navigator.mediaDevices.getUserMedia work in a hybrid app?

Let me expand on this a bit.

I have a hybrid app currently that I am trying to transition to use mainly web apis, while for the most part everything is working as expected I am unable to get the camera to be accessable in an 'app' form.

When I run the code

navigator.mediaDevices.getUserMedia

I get back one of two errors (depending on the hybrid framework I use)

Dom message stating permission is denied

Dom message stating video stream is not ready

Now, item one would indicate that the user has not 'allowed' permissions to the actual camera but the catch is, I never get prompted to grant permissions in-app.

For number two, that would seem to indicate that the stream is already in use (have seen some stating this too can be related to permissions so I'm leaning toward that assumption)

So I guess back to the question at hand and that is, is this even possible in a hybrid app? I have seen many of similar questions with similar Dom messages but non seem to have an answer in the sense of is it even possible? I have seen some mention setting the 'feature-policy' headers but in the context of a hybrid app, running a localhost how or where is this set?

Anyhow, just wanted to ask and see if anyone else has been successful in doing this or do we need to use the native plugins for this?

Thanks again!


问:
AssertionError inside of ensure_local_distutils when building a PyInstaller exe using setuptools/distutils
I am trying to convert some Python code into an .exe with PyInstaller. My code uses distutils, which has already caused me some head scratching in the past as it seems to duplicate setuptools functionality, and interacts with it strangely. It e.g. requires an unused import of setuptools to work properly which seems very unpythonic to me.

I need to use both packages inside my project. I import both packages in the order import setuptools; import distutils because the other order throws a warning which is already worrying.

My script runs fine but after I turn it into an exe I get a traceback inside of the suspicious _distutils_hack submodule of setuptools. And yes it just prints a file name with no context.

Traceback (most recent call last):
  <18 lines omitted>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "_distutils_hack\override.py", line 71, in <module>
  File "_distutils_hack\__init__.py", line 71, in do_override
  File "_distutils_hack\__init__.py", line 59, in ensure_local_distutils
AssertionError: C:\Users\<omitted>\AppData\Local\Temp\_MEI294562\distutils\core.pyc
```

I am using

Python 3.7.7 on win32 (but I reproduced it with 3.8, 3.9, and 3.10)

pyinstaller==4.8 (Jan 2022) for Windows

distutils==3.7.7 (built-in)

setuptools==60.5.0 (Jan 2022)

Apparently, setuptools is listed on PyPI and thus upgradable, but distutils is not listed on PyPI and thus not upgradable (the version is bundled with Python).

答:
A workaround I found was downgrading to

pip install setuptools==59.8.0
```

I reported this issue to the project and then confirmed the bug was introduced in setuptools==60.0.0. I'll update this question when I find out more, or it gets fixed.

Body:
I am trying to convert some Python code into an .exe with PyInstaller. My code uses distutils, which has already caused me some head scratching in the past as it seems to duplicate setuptools functionality, and interacts with it strangely. It e.g. requires an unused import of setuptools to work properly which seems very unpythonic to me.

I need to use both packages inside my project. I import both packages in the order import setuptools; import distutils because the other order throws a warning which is already worrying.

My script runs fine but after I turn it into an exe I get a traceback inside of the suspicious _distutils_hack submodule of setuptools. And yes it just prints a file name with no context.

Traceback (most recent call last):
  <18 lines omitted>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "_distutils_hack\override.py", line 71, in <module>
  File "_distutils_hack\__init__.py", line 71, in do_override
  File "_distutils_hack\__init__.py", line 59, in ensure_local_distutils
AssertionError: C:\Users\<omitted>\AppData\Local\Temp\_MEI294562\distutils\core.pyc

I am using

Python 3.7.7 on win32 (but I reproduced it with 3.8, 3.9, and 3.10)

pyinstaller==4.8 (Jan 2022) for Windows

distutils==3.7.7 (built-in)

setuptools==60.5.0 (Jan 2022)

Apparently, setuptools is listed on PyPI and thus upgradable, but distutils is not listed on PyPI and thus not upgradable (the version is bundled with Python).

Body:
A workaround I found was downgrading to

pip install setuptools==59.8.0

I reported this issue to the project and then confirmed the bug was introduced in setuptools==60.0.0. I'll update this question when I find out more, or it gets fixed.


问:
Match if element of array is a partial match to a string?
I am aware of

```<code>if ( grep(/^$pattern$/, @array) ) {...}
</code>```

which will return true if the entire string is found in an element of the array.
However im trying to figure out how to return true if one of the elements in the array is a partial match to the end of the string.

For example:

```<code>my @array = (".com", ".net", ".org");
my $domain = "www.example.com";   #<--Returns True
   $domain = "www.example.gov";   #<--Returns False
   $domain = "www.computer.gov";  #<--Returns False, .com not at end
</code>```

Is there a more elegant way to do this without creating a foreach() and using a m// match against each element?

答:
Can use any from List::Util

```<code>if ( any { $re = quotemeta; $string =~ /$re$/ } @ary )  { ... }
</code>```

The $ matches the end of the string so the above matches any $string for which $re pattern is at its end (regardless of what comes before in that string).
The quotemeta escapes all "ASCII non-word characters", so (also) things that have a special meaning inside a regex. In this case it turns that . (a pattern matching any character) into \., a literal dot.

The quotemeta has a \Q ... \E form that can be used inside a regex, for

```<code>if ( any { $string =~ /\Q$_\E$/ } @ary )  { ... }
</code>```

But careful with it to not escape other parts of a potentially more complex pattern.

Body:
I am aware of

if ( grep(/^$pattern$/, @array) ) {...}

which will return true if the entire string is found in an element of the array.
However im trying to figure out how to return true if one of the elements in the array is a partial match to the end of the string.

For example:

my @array = (".com", ".net", ".org");
my $domain = "www.example.com";   #<--Returns True
   $domain = "www.example.gov";   #<--Returns False
   $domain = "www.computer.gov";  #<--Returns False, .com not at end

Is there a more elegant way to do this without creating a foreach() and using a m// match against each element?

Body:
Can use any from List::Util

if ( any { $re = quotemeta; $string =~ /$re$/ } @ary )  { ... }

The $ matches the end of the string so the above matches any $string for which $re pattern is at its end (regardless of what comes before in that string).
The quotemeta escapes all "ASCII non-word characters", so (also) things that have a special meaning inside a regex. In this case it turns that . (a pattern matching any character) into \., a literal dot.

The quotemeta has a \Q ... \E form that can be used inside a regex, for

if ( any { $string =~ /\Q$_\E$/ } @ary )  { ... }

But careful with it to not escape other parts of a potentially more complex pattern.


问:
SQL Error [91016] [22000]: Remote file 'stage_name/java_udf.jar' was not found
Created a jar file and use it as function. I created it with same user role for both function and snowflake stage. Uploaded the jar file to the stage using snowsql.

When I run the the following command in snowflake ui (browser), it works.

```<code>ls @~/stage_name
</code>```

However, when I use the service account with similar role that I have using DBeaver. It does not work. It comes up empty.

Same thing with the function, it works in the Snowflake UI, but not in DBeaver. Please note that both users have the same role. Also, added grant "all privileges" and "usage" (which be part of all) to the roles I want them to use. But again, it does not work. It shows error below

**> SQL Error [91016] [22000]: Remote file 'stage_name/java_udf.jar' was

not found. If you are running a copy command, please make sure files
are not deleted when they are being loaded or files are not being
loaded into two different tables concurrently with auto purge option.**

However, when I run the function in Snowflake UI using my user account, it works fine. Please note my user account has the same role as the service account. But it doesn't work on the service account. Any ideas?

Followed steps here in the documentation:
https://docs.snowflake.com/en/developer-guide/udf/java/udf-java-creating.html#label-udf-java-in-line-examples

答:
~@ is the USER LOCAL stoage only area.

thus, unless the automation is the "same" user, it will not be able to access it.

This should be provable by getting the same "run" command that works from the WebUI for your user, and logging in as the automation user, and seeing you get the error there.

Reading that link document, full you can see that you should use a table storage, or a named storage, which you can grant access to the role you both have.

working proof:

on user simeon:

```<code>create or replace stage my_stage;

create or replace function echo_varchar(x varchar)
returns varchar
language java
called on null input
handler='TestFunc.echo_varchar'
target_path='@my_stage/testfunc.jar'
as
'class TestFunc {
  public static String echo_varchar(String x) {
    return x;
  }
}';

create role my_role;

grant usage on function echo_varchar(varchar) to my_role;
grant all on stage my_stage to my_role;
grant usage on database test to my_role;
grant usage on schema not_test to my_role;
grant usage on warehouse compute_wh to my_role;
</code>```

then I test it:

```<code>use role my_role;
select current_user(), current_role();
/*CURRENT_USER()    CURRENT_ROLE()
SIMEON  MY_ROLE*/

select test.not_test.echo_varchar('Hello');
/*TEST.NOT_TEST.ECHO_VARCHAR('HELLO')
Hello*/
</code>```

I created a new user test_two set them to role my_role

on user test_two:

```<code>use role my_role;
select current_user(), current_role();
/*CURRENT_USER()    CURRENT_ROLE()
TEST_TWO    MY_ROLE*/

select test.not_test.echo_varchar('Hello');
/*TEST.NOT_TEST.ECHO_VARCHAR('HELLO')
Hello*/
</code>```

Ok so a function put on a accessible stage works, lets put another on my user simeon local stage ~@

on user Simeon:

```<code>returns varchar
language java
called on null input
handler='TestFuncB.echo_varcharb'
target_path='@~/testfuncb.jar'
as
'class TestFuncB {
  public static String echo_varcharb(String x) {
    return x;
  }
}';

grant usage on function echo_varcharb(varchar) to my_role;

select test.not_test.echo_varcharb('Hello');
/*TEST.NOT_TEST.ECHO_VARCHARB('HELLO')
Hello*/
</code>```

back on user test_two:

```<code>select test.not_test.echo_varcharb('Hello');
/*Remote file 'testfuncb.jar' was not found. If you are running a copy command, please make sure files are not deleted when they are being loaded or files are not being loaded into two different tables concurrently with auto purge option.*/

</code>```

Body:
Created a jar file and use it as function. I created it with same user role for both function and snowflake stage. Uploaded the jar file to the stage using snowsql.

When I run the the following command in snowflake ui (browser), it works.

ls @~/stage_name

However, when I use the service account with similar role that I have using DBeaver. It does not work. It comes up empty.

Same thing with the function, it works in the Snowflake UI, but not in DBeaver. Please note that both users have the same role. Also, added grant "all privileges" and "usage" (which be part of all) to the roles I want them to use. But again, it does not work. It shows error below

**> SQL Error [91016] [22000]: Remote file 'stage_name/java_udf.jar' was

not found. If you are running a copy command, please make sure files
are not deleted when they are being loaded or files are not being
loaded into two different tables concurrently with auto purge option.**

However, when I run the function in Snowflake UI using my user account, it works fine. Please note my user account has the same role as the service account. But it doesn't work on the service account. Any ideas?

Followed steps here in the documentation:
https://docs.snowflake.com/en/developer-guide/udf/java/udf-java-creating.html#label-udf-java-in-line-examples

Body:
~@ is the USER LOCAL stoage only area.

thus, unless the automation is the "same" user, it will not be able to access it.

This should be provable by getting the same "run" command that works from the WebUI for your user, and logging in as the automation user, and seeing you get the error there.

Reading that link document, full you can see that you should use a table storage, or a named storage, which you can grant access to the role you both have.

working proof:

on user simeon:

create or replace stage my_stage;

create or replace function echo_varchar(x varchar)
returns varchar
language java
called on null input
handler='TestFunc.echo_varchar'
target_path='@my_stage/testfunc.jar'
as
'class TestFunc {
  public static String echo_varchar(String x) {
    return x;
  }
}';

create role my_role;

grant usage on function echo_varchar(varchar) to my_role;
grant all on stage my_stage to my_role;
grant usage on database test to my_role;
grant usage on schema not_test to my_role;
grant usage on warehouse compute_wh to my_role;

then I test it:

use role my_role;
select current_user(), current_role();
/*CURRENT_USER()    CURRENT_ROLE()
SIMEON  MY_ROLE*/

select test.not_test.echo_varchar('Hello');
/*TEST.NOT_TEST.ECHO_VARCHAR('HELLO')
Hello*/

I created a new user test_two set them to role my_role

on user test_two:

use role my_role;
select current_user(), current_role();
/*CURRENT_USER()    CURRENT_ROLE()
TEST_TWO    MY_ROLE*/

select test.not_test.echo_varchar('Hello');
/*TEST.NOT_TEST.ECHO_VARCHAR('HELLO')
Hello*/

Ok so a function put on a accessible stage works, lets put another on my user simeon local stage ~@

on user Simeon:

returns varchar
language java
called on null input
handler='TestFuncB.echo_varcharb'
target_path='@~/testfuncb.jar'
as
'class TestFuncB {
  public static String echo_varcharb(String x) {
    return x;
  }
}';

grant usage on function echo_varcharb(varchar) to my_role;

select test.not_test.echo_varcharb('Hello');
/*TEST.NOT_TEST.ECHO_VARCHARB('HELLO')
Hello*/

back on user test_two:

select test.not_test.echo_varcharb('Hello');
/*Remote file 'testfuncb.jar' was not found. If you are running a copy command, please make sure files are not deleted when they are being loaded or files are not being loaded into two different tables concurrently with auto purge option.*/


问:
Redirect to SHOW action in another controller
After creating a Person associated with a specific Account, how do I redirect back to the Account page?

The account_id is passed to the CREATE Person action via a URL parameter as follows:

```<code>http://localhost:3000/people/new?account_id=1
</code>```

Here is the code:

```<code><h2>Account:
    <%= Account.find_by_id(params[:account_id]).organizations.
        primary.first.name %>
</h2>

<%= form_for @person do |f| %>

    <%= f.hidden_field :account_id, :value => params[:account_id] %><br />
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %><br />
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %><br />
    <%= f.label :email1 %><br />
    <%= f.text_field :email1 %><br />
    <%= f.label :home_phone %><br />
    <%= f.text_field :home_phone %><br />
    <%= f.submit "Add person" %>

<% end %>

class PeopleController < ApplicationController

    def new
        @person = Person.new
    end

    def create
        @person = Person.new(params[:person])
        if @person.save
            flash[:success] = "Person added successfully"
            redirect_to account_path(params[:account_id])
        else
            render 'new'
        end
    end
end
</code>```

When I submit the above form I get the following error message:

```<code>Routing Error

No route matches {:action=>"destroy", :controller=>"accounts"}
</code>```

Why is the redirect_to routing to the DESTROY action?  I want to redirect via the SHOW action.  Any help will be greatly appreciated.

答:
I would not be pass this through using a hidden_field. Instead, use nested resources:

```<code>resources :account do
  resources :people
end
</code>```

Then have an account object for the form:

```<code><%= form_for [@account, @person] do |f| %>
  ...
<% end %>
</code>```

This @account object should be set up in the action that renders the form with a line like this:

```<code>@acccount = Account.find(params[:account_id])
</code>```

Then when the form is submitted you'll have params[:account_id] in that action without the ugly hidden_field hack to get it there.

Yippee!

Body:
After creating a Person associated with a specific Account, how do I redirect back to the Account page?

The account_id is passed to the CREATE Person action via a URL parameter as follows:

http://localhost:3000/people/new?account_id=1

Here is the code:

Account:
    <%= Account.find_by_id(params[:account_id]).organizations.
        primary.first.name %>

<%= form_for @person do |f| %>

    <%= f.hidden_field :account_id, :value => params[:account_id] %>

    <%= f.label :first_name %>

    <%= f.text_field :first_name %>

    <%= f.label :last_name %>

    <%= f.text_field :last_name %>

    <%= f.label :email1 %>

    <%= f.text_field :email1 %>

    <%= f.label :home_phone %>

    <%= f.text_field :home_phone %>

    <%= f.submit "Add person" %>

<% end %>

class PeopleController < ApplicationController

    def new
        @person = Person.new
    end

    def create
        @person = Person.new(params[:person])
        if @person.save
            flash[:success] = "Person added successfully"
            redirect_to account_path(params[:account_id])
        else
            render 'new'
        end
    end
end

When I submit the above form I get the following error message:

Routing Error

No route matches {:action=>"destroy", :controller=>"accounts"}

Why is the redirect_to routing to the DESTROY action?  I want to redirect via the SHOW action.  Any help will be greatly appreciated.

Body:
I would not be pass this through using a hidden_field. Instead, use nested resources:

resources :account do
  resources :people
end

Then have an account object for the form:

<%= form_for [@account, @person] do |f| %>
  ...
<% end %>

This @account object should be set up in the action that renders the form with a line like this:

@acccount = Account.find(params[:account_id])

Then when the form is submitted you'll have params[:account_id] in that action without the ugly hidden_field hack to get it there.

Yippee!


问:
My doc2vec library cannot load DocvecsArray.is there a solution.python code
it shows  Can't get attribute 'DocvecsArray' on <module 'gensim.models.doc2vec' from 'C:\Users\aysha\anaconda3\lib\site-packages\gensim\models\doc2vec.py'> in anaconda prompt while compiling my code.What should i do to solve this?</p>

答:
DocvecsArray is a long-obsolete class name from older versions of Gensim.

Are you trying to load an old model into a Python environment with a current Gensim? If so, do you know from which version of Gensim the model was saved (even approximately, or by date)?

It may be possible to bring an old model forward, but it may require one or more interim steps, where the model is loaded into an older version of Gensim, that can still read, convert, & then re-save (in a newer format) the old model.


问:
Recursively remove empty elements and subarrays from a multi-dimensional array
I can't seem to find a simple, straight-forward solution to the age-old problem of removing empty elements from arrays in PHP.

My input array may look like this:

```<code>Array ( [0] => Array ( [Name] => [EmailAddress] => ) )
</code>```

(And so on, if there's more data, although there may not be...)

If it looks like the above, I want it to be completely empty after I've processed it.

So print_r($array); would output:

```<code>Array ( )
</code>```

If I run $arrayX = array_filter($arrayX); I still get the same print_r output.  Everywhere I've looked suggests this is the simplest way of removing empty array elements in PHP5, however.

I also tried $arrayX = array_filter($arrayX,'empty_array'); but I got the following error:

Warning: array_filter() [function.array-filter]: The second argument, 'empty_array', should be a valid callback

What am I doing wrong?

答:
When this question was asked, the latest version of PHP was 5.3.10. As of today, it is now 8.1.1, and a lot has changed since! Some of the earlier answers will provide unexpected results, due to changes in the core functionality. Therefore, I feel an up-to-date answer is required. The below will iterate through an array, remove any elements that are either an empty string, empty array, or null (so false and 0 will remain), and if this results in any more empty arrays, it will remove them too.

```<code>function removeEmptyArrayElements( $value ) {
    if( is_array($value) ) {
        $value = array_map('removeEmptyArrayElements', $value);
        $value = array_filter( $value, function($v) {
            // Change the below to determine which values get removed
            return !( $v === "" || $v === null || (is_array($v) && empty($v)) );
        } );
    }
    return $value;
}
</code>```

To use it, you simply call removeEmptyArrayElements( $array );

Body:
I can't seem to find a simple, straight-forward solution to the age-old problem of removing empty elements from arrays in PHP.

My input array may look like this:

Array ( [0] => Array ( [Name] => [EmailAddress] => ) )

(And so on, if there's more data, although there may not be...)

If it looks like the above, I want it to be completely empty after I've processed it.

So print_r($array); would output:

Array ( )

If I run $arrayX = array_filter($arrayX); I still get the same print_r output.  Everywhere I've looked suggests this is the simplest way of removing empty array elements in PHP5, however.

I also tried $arrayX = array_filter($arrayX,'empty_array'); but I got the following error:

Warning: array_filter() [function.array-filter]: The second argument, 'empty_array', should be a valid callback

What am I doing wrong?

Body:
When this question was asked, the latest version of PHP was 5.3.10. As of today, it is now 8.1.1, and a lot has changed since! Some of the earlier answers will provide unexpected results, due to changes in the core functionality. Therefore, I feel an up-to-date answer is required. The below will iterate through an array, remove any elements that are either an empty string, empty array, or null (so false and 0 will remain), and if this results in any more empty arrays, it will remove them too.

function removeEmptyArrayElements( $value ) {
    if( is_array($value) ) {
        $value = array_map('removeEmptyArrayElements', $value);
        $value = array_filter( $value, function($v) {
            // Change the below to determine which values get removed
            return !( $v === "" || $v === null || (is_array($v) && empty($v)) );
        } );
    }
    return $value;
}

To use it, you simply call removeEmptyArrayElements( $array );


问:
How can I wrap a closure in a closure?
This is my particular use case, but I'm sure there are others:

I pass a callback closure to a function that may run in a different thread. The code in the closure may throw an exception. I don't want that exception to kill my program. I'm happy to catch it and report it. (Yes, I know that doing this can result in resource leaks; I'm ignoring this issue for now.)

There are hundreds of these closures in my code, so I don't want to edit every single one of them to add try…catch clauses to them. I'd prefer to have a short wrapper around the closures instead.

My sample code looks like this:

```<code>#include <iostream>
#include <thread>

using namespace std;

int
main()
{
    string str("Hello, world");

    // Program will die because of exception thrown in handler.
    context->invoke(
        [str](bool success) {
            cout << str << ": " << success << endl;
            throw exception();
        }
    );

    myThread.join();

    // I want my program to not die
    context->invoke(
        []{exceptionCatcher(
            [str](bool success) {
                cout << str << ": " << success << endl;
                throw exception();
            }
        );}
    );

    myThread2.join();

    return 0;
}
</code>```

and then my exceptionCatcher template looks something like this:

```<code>template<typename Func>
void exceptionCatcher(Func func)
{
    try {
        func();
    }
    catch (const exception& e) {
        cout << "Caught exception\n";
    }
}
</code>```

except that the compiler doesn't like this.

```<code>c++ -std=c++17 -o tester tester.cpp
tester.cpp:40:14: error: variable 'str' cannot be implicitly captured in a lambda with no
      capture-default specified
            [str]{
             ^
tester.cpp:23:12: note: 'str' declared here
    string str("Hello, world");
           ^
tester.cpp:39:9: note: lambda expression begins here
        []{exceptionCatcher(
</code>```

What's the magic incantation to write exceptionCatcher?

Complication: There are many such "invoke" functions. The captured values and the arguments to the closure are varied. Is it possible to write a wrapper that handles all these cases?

答:
You didn't capture str in the outer lambda, so it is not available in the inner lambda.

If invoke() allows you to pass the callable object and its parameters separately (like std::thread's constructor allows), then you can do something like this:

template<typename Func>
void exceptionCatcher(Func func)
{
    try {
        func(success);
    }
    catch (const exception& e) {
        cout << "Caught exception\n";
    }
}

auto func = [str](bool success) {
    cout << str << ": " << success << endl;
    throw exception();
};

context->invoke(
    exceptionCatcher<decltype(func)>,
    func
);
```
<p>Alternatively:</p>
```void exceptionCatcher(std::function<void(bool)> func)
{
    try {
        func(success);
    }
    catch (const exception& e) {
        cout << "Caught exception\n";
    }
}

context->invoke(
    exceptionCatcher,
    [str](bool success){
        cout << str << ": " << success << endl;
        throw exception();
    }
);
```
<p>Otherwise, if that is not an option, you will just have to add an additional capture to your outer lambda:</p>
```context->invoke(
    [str]{ // <-- add this
        exceptionCatcher(
            [str](bool success) {
                cout << str << ": " << success << endl;
                throw exception();
            }
        );
    }
);
```

Title: How can I wrap a closure in a closure?

Body:
This is my particular use case, but I'm sure there are others:

I pass a callback closure to a function that may run in a different thread. The code in the closure may throw an exception. I don't want that exception to kill my program. I'm happy to catch it and report it. (Yes, I know that doing this can result in resource leaks; I'm ignoring this issue for now.)

There are hundreds of these closures in my code, so I don't want to edit every single one of them to add try…catch clauses to them. I'd prefer to have a short wrapper around the closures instead.

My sample code looks like this:

#include <iostream>
#include <thread>

using namespace std;

int
main()
{
    string str("Hello, world");

    // Program will die because of exception thrown in handler.
    context->invoke(
        [str](bool success) {
            cout << str << ": " << success << endl;
            throw exception();
        }
    );

    myThread.join();

    // I want my program to not die
    context->invoke(
        []{exceptionCatcher(
            [str](bool success) {
                cout << str << ": " << success << endl;
                throw exception();
            }
        );}
    );

    myThread2.join();

    return 0;
}

and then my exceptionCatcher template looks something like this:

template<typename Func>
void exceptionCatcher(Func func)
{
    try {
        func();
    }
    catch (const exception& e) {
        cout << "Caught exception\n";
    }
}

except that the compiler doesn't like this.

c++ -std=c++17 -o tester tester.cpp
tester.cpp:40:14: error: variable 'str' cannot be implicitly captured in a lambda with no
      capture-default specified
            [str]{
             ^
tester.cpp:23:12: note: 'str' declared here
    string str("Hello, world");
           ^
tester.cpp:39:9: note: lambda expression begins here
        []{exceptionCatcher(

What's the magic incantation to write exceptionCatcher?

Complication: There are many such "invoke" functions. The captured values and the arguments to the closure are varied. Is it possible to write a wrapper that handles all these cases?

Body:
You didn't capture str in the outer lambda, so it is not available in the inner lambda.

If invoke() allows you to pass the callable object and its parameters separately (like std::thread's constructor allows), then you can do something like this:

template<typename Func>
void exceptionCatcher(Func func)
{
    try {
        func(success);
    }
    catch (const exception& e) {
        cout << "Caught exception\n";
    }
}

auto func = [str](bool success) {
    cout << str << ": " << success << endl;
    throw exception();
};

context->invoke(
    exceptionCatcher<decltype(func)>,
    func
);

Alternatively:

void exceptionCatcher(std::function<void(bool)> func)
{
    try {
        func(success);
    }
    catch (const exception& e) {
        cout << "Caught exception\n";
    }
}

context->invoke(
    exceptionCatcher,
    [str](bool success){
        cout << str << ": " << success << endl;
        throw exception();
    }
);

Otherwise, if that is not an option, you will just have to add an additional capture to your outer lambda:

context->invoke(
    [str]{ // <-- add this
        exceptionCatcher(
            [str](bool success) {
                cout << str << ": " << success << endl;
                throw exception();
            }
        );
    }
);


问:
Is it possible to send me a Discord private message when an user confirm a form sending in my website?
I would like to be able to get Discord private message when an user wants to send me a message through my personal website. There is a form inside my website, how can I do this on the onClick button "Send" ?</p>

答:
Look into WebSockets. SocketIO has the most support and documentation however for this simple task you can get away with implementing this using the ws package in nodejs.
Make sure the Discord Bot is the Server & the frontend is the Client.

https://www.npmjs.com/package/ws

https://socket.io/docs/v4/server-api/


问:
Custom train-test split using two stratified classes
I am trying to use NLP techniques to predict the mental status of patients at the time doctor's notes were taken. I have two classes, suffix (multilabel) as well as mental_status (binary, Lucid or Delirium). I am trying to build train and test sets from a reduced list that meet the following specifications:

Shared unique suffix list

For each set for each suffix, at least one row for Lucid and Delirium

Any imbalances I will correct with sample weights (I am only trying to predict mental_status, but I need the data to also be balanced on suffix). I just need at least one row for each combination within the full Cartesian product of suffixes and mental_statuses.

For example, in the dataset below, only the DWM Communication suffixes would make it into the train-test split.

```<code>suffix              mental_status
DWN Communication   Lucid
DWN Communication   Delirium
DWN Communication   Lucid
DWN Communication   Delirium
DWN Communication   Lucid
DWN Psychiatry      Lucid
DWN Psychiatry      Delirium
DWN Psychiatry      Delirium
DWN Cardio stuff    Lucid
DWN Cardio stuff    Delirium
DWN Blood .....     Lucid
DWN Blood .....     Lucid
DWN Blood .....     Lucid
</code>```

The output I would want for the example above for train would be

```<code>DWN Communication   Lucid
DWN Communication   Delirium
DWN Communication   Lucid
</code>```

and test

```<code>DWN Communication   Lucid
DWN Communication   Delirium
</code>```

I've tried

```<code>df.groupby(['suffix','mental_status']).filter(lambda x: len(x) > 1)
</code>```

but it doesn't ensure there is one of each Delirium and Lucid for each suffix.

As another example,

```<code>    suf ms
11  blood   delirium
15  blood   delirium
0   blood   lucid
1   blood   lucid
5   blood   lucid
6   blood   lucid
10  blood   lucid
16  blood   lucid
3   psych   delirium
19  psych   delirium
4   psych   lucid
8   psych   lucid
9   psych   lucid
13  psych   lucid
14  psych   lucid
18  psych   lucid
7   stool   delirium
2   stool   lucid
12  stool   lucid
17  stool   lucid
</code>```

would be split into

```<code>    suf ms
11  blood   delirium
0   blood   lucid
1   blood   lucid
5   blood   lucid
6   blood   lucid
3   psych   delirium
4   psych   lucid
8   psych   lucid
9   psych   lucid
13  psych   lucid
</code>```

and

```<code>    suf ms
15  blood   delirium
10  blood   lucid
16  blood   lucid
19  psych   delirium
14  psych   lucid
18  psych   lucid
</code>```

Edit:

I think I arrived at a solution:

```<code>from sklearn.model_selection import train_test_split
def custom_split(df, test_size):
    f = lambda ms: (
        df[df.mental_status==ms]
        .groupby('suffix')
        .filter(lambda x:len(x)>=1/min(test_size,1-test_size))
        .suffix
    )
    delirium = f('Delirium')
    lucid = f('Lucid')
    valid_suffixes = set(delirium).intersection(lucid)
    df = df[df.suffix.isin(valid_suffixes)]
    df_train, df_test = train_test_split(df,test_size,random_state=9,
                                         stratify=df.mental_status+df.suffix)
    return df_train, df_test
</code>```

答: Does it work if you just do df.drop_duplicates(inplace=True)? It looks like that’s all there is to it because you just want the unique values based on the combination of both columns.

Body:
I am trying to use NLP techniques to predict the mental status of patients at the time doctor's notes were taken. I have two classes, suffix (multilabel) as well as mental_status (binary, Lucid or Delirium). I am trying to build train and test sets from a reduced list that meet the following specifications:

Shared unique suffix list

For each set for each suffix, at least one row for Lucid and Delirium

Any imbalances I will correct with sample weights (I am only trying to predict mental_status, but I need the data to also be balanced on suffix). I just need at least one row for each combination within the full Cartesian product of suffixes and mental_statuses.

For example, in the dataset below, only the DWM Communication suffixes would make it into the train-test split.

suffix              mental_status
DWN Communication   Lucid
DWN Communication   Delirium
DWN Communication   Lucid
DWN Communication   Delirium
DWN Communication   Lucid
DWN Psychiatry      Lucid
DWN Psychiatry      Delirium
DWN Psychiatry      Delirium
DWN Cardio stuff    Lucid
DWN Cardio stuff    Delirium
DWN Blood .....     Lucid
DWN Blood .....     Lucid
DWN Blood .....     Lucid

The output I would want for the example above for train would be

DWN Communication   Lucid
DWN Communication   Delirium
DWN Communication   Lucid

and test

DWN Communication   Lucid
DWN Communication   Delirium

I've tried

df.groupby(['suffix','mental_status']).filter(lambda x: len(x) > 1)

but it doesn't ensure there is one of each Delirium and Lucid for each suffix.

As another example,

    suf ms
11  blood   delirium
15  blood   delirium
0   blood   lucid
1   blood   lucid
5   blood   lucid
6   blood   lucid
10  blood   lucid
16  blood   lucid
3   psych   delirium
19  psych   delirium
4   psych   lucid
8   psych   lucid
9   psych   lucid
13  psych   lucid
14  psych   lucid
18  psych   lucid
7   stool   delirium
2   stool   lucid
12  stool   lucid
17  stool   lucid

would be split into

    suf ms
11  blood   delirium
0   blood   lucid
1   blood   lucid
5   blood   lucid
6   blood   lucid
3   psych   delirium
4   psych   lucid
8   psych   lucid
9   psych   lucid
13  psych   lucid

and

    suf ms
15  blood   delirium
10  blood   lucid
16  blood   lucid
19  psych   delirium
14  psych   lucid
18  psych   lucid

Edit:

I think I arrived at a solution:

from sklearn.model_selection import train_test_split
def custom_split(df, test_size):
    f = lambda ms: (
        df[df.mental_status==ms]
        .groupby('suffix')
        .filter(lambda x:len(x)>=1/min(test_size,1-test_size))
        .suffix
    )
    delirium = f('Delirium')
    lucid = f('Lucid')
    valid_suffixes = set(delirium).intersection(lucid)
    df = df[df.suffix.isin(valid_suffixes)]
    df_train, df_test = train_test_split(df,test_size,random_state=9,
                                         stratify=df.mental_status+df.suffix)
    return df_train, df_test