问:
Python Thread Pool Faster than Go Routines when Scanning AWS S3?
I have recently been digging into understanding Golang concurrency, in particular the use of channels and worker pools.  I wanted to compare performance between Go and Python (as many have done) because I have mostly read that Go outperforms Python with regard to concurrency.  So I wrote two programs to scan an AWS account's S3 buckets and report back the total size.  I performed this on an account that had more the 75 buckets totaling more than a few TB of data.

I was surprised to find that my Python implementation was nearly 2x faster than my Go implementation.  This confuses me based on all the benchmarks and literature I have read.  This leads me to believe that I did not implement my Go code correctly.  While watching both programs run I noticed that the Go implementation only used up to 15% of my CPU while Python used >85%.  Am I missing an important step with Go or am I missing something in my implementation?  Thanks in advance!

Python Code:

'''
Get the size of all objects in all buckets in S3
'''
import os
import sys
import boto3
import concurrent.futures

def get_s3_bucket_sizes(aws_access_key_id, aws_secret_access_key, aws_session_token=None):

    s3client = boto3.client('s3')

    # Create the dictionary which will be indexed by the bucket's
    # name and has an S3Bucket object as its contents
    buckets = {}

    total_size = 0.0

    #
    # Start gathering data...
    #

    # Get all of the buckets in the account
    _buckets = s3client.list_buckets()

    cnt = 1
    with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
        future_bucket_to_scan = {executor.submit(get_bucket_objects, s3client, bucket): bucket for bucket in _buckets["Buckets"]}

        for future in concurrent.futures.as_completed(future_bucket_to_scan):
            bucket_object = future_bucket_to_scan[future]

            try:
                ret = future.result()
            except Exception as exc:
                print('ERROR: %s' % (str(exc)))
            else:
                total_size += ret

    print(total_size)

def get_bucket_objects(s3client, bucket):

    name = bucket["Name"]

    # Get all of the objects in the bucket
    lsbuckets = s3client.list_objects(Bucket=name)

    size = 0
    while True:
        if "Contents" not in lsbuckets.keys():
            break

        for content in lsbuckets["Contents"]:
            size += content["Size"]

        break

    return size

#
# Main
#
if __name__=='__main__':
    get_s3_bucket_sizes(os.environ.get("AWS_ACCESS_KEY_ID"), os.environ.get("AWS_SECRET_ACCESS_KEY"))

```

<p><strong>Go Code:</strong></p>

<pre class="lang-golang prettyprint-override"><code>package main

import (
    "fmt"
    "sync"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/awserr"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

type S3_Bucket_Response struct {
    bucket string
    count  int64
    size   int64
    err    error
}

type S3_Bucket_Request struct {
    bucket string
    region string
}

func get_bucket_objects_async(wg *sync.WaitGroup, requests chan S3_Bucket_Request, responses chan S3_Bucket_Response) {

    var size  int64
    var count int64

    for request := range requests {
        bucket := request.bucket
        region := request.region

        // Create a new response
        response := new(S3_Bucket_Response)
        response.bucket = bucket

        sess, err := session.NewSession(&aws.Config{
            Region: aws.String(region),
        })

        s3conn := s3.New(sess)

        resp, err := s3conn.ListObjectsV2(&s3.ListObjectsV2Input{
            Bucket: aws.String(bucket),
        })

        if err != nil {
            if awsErr, ok := err.(awserr.Error); ok {

                switch awsErr.Code() {
                case "NoSuchBucket":
                    response.err = fmt.Errorf("Bucket: (%s) is NoSuchBucket.  Must be in process of deleting.", bucket)
                case "AccessDenied":
                    response.err = fmt.Errorf("Bucket: (%s) is AccessDenied.  You should really be running this with full Admin Privaleges", bucket)
                }
            } else {
                response.err = fmt.Errorf("Listing Objects Unhandled Error: %s ", err)
            }

            responses <- *response
            continue
        }

        contents := resp.Contents
        size      = 0
        count     = 0

        for i:=0; i<len(contents); i++ {
            size  += *contents[i].Size
            count += 1
        }

        response.size  = size
        response.count = count

        responses <- *response
    }

    wg.Done()
}

func main() {

    var err  error
    var size int64
    var resp *s3.ListBucketsOutput
    var wg sync.WaitGroup

    sess, _ := session.NewSession()
    s3conn  := s3.New(sess)

    // Get account bucket listing
    if resp, err = s3conn.ListBuckets(&s3.ListBucketsInput{});err != nil {
        fmt.Println("Error listing buckets: %s", err)
        return
    }

    buckets := resp.Buckets
    size = 0

    // Create the buffered channels
    requests  := make(chan S3_Bucket_Request , len(buckets))
    responses := make(chan S3_Bucket_Response, len(buckets))

    for i := range buckets {

        bucket := *buckets[i].Name

        resp2, err := s3conn.GetBucketLocation(&s3.GetBucketLocationInput{
            Bucket: aws.String(bucket),
        })

        if err != nil {
            fmt.Printf("Could not get bucket location for bucket (%s): %s", bucket, err)
            continue
        }

        wg.Add(1)
        go get_bucket_objects_async(&wg, requests, responses)

        region := "us-east-1"
        if resp2.LocationConstraint != nil {
            region = *resp2.LocationConstraint
        }

        request := new(S3_Bucket_Request)
        request.bucket = bucket
        request.region = region

        requests <- *request
    }

    // Close requests channel and wait for responses
    close(requests)
    wg.Wait()
    close(responses)

    cnt := 1
    // Process the results as they come in
    for response := range responses {

        fmt.Printf("Bucket: (%s) complete!  Buckets remaining: %d\n", response.bucket, len(buckets)-cnt)

        // Did the bucket request have errors?
        if response.err != nil {
            fmt.Println(response.err)
            continue
        }

        cnt  += 1
        size += response.size
    }

    fmt.Println(size)
    return
}

</code>```

答:
Sorry haven't had an opportunity to fully review this but my answer would be: the solutions don't appear to be equivalent in terms of concurrency. 3 things pop out:

Thread safety of boto s3 client. Is this thread safe? Can you confirm it? This reddit article suggests that it is not threadsafe.

Python is using a worker pool size of 50 but go is unbounded. (a semphore could be used to add an upper bound of 50 to the current code)

I"m not super familiar with boto but it looks like go is performing an additional IO call on the main thread for each bucket (GetBucketLocation) when compared to python.

My next questions would be:

Is each solution correct and can you prove it? (do both sum to the same amount of bytes and do those match the s3 console?)

Are you sure that the concurrent structure is the same,
i.e. summing on main thread, same pool sizes, same amount of IO work
per worker.

Are the defaults of the clients equivalent?, i.e. does python have a default connection pool size? Go doesn't so it will create connections per request (i just ran into this last week)

Body:
I have recently been digging into understanding Golang concurrency, in particular the use of channels and worker pools.  I wanted to compare performance between Go and Python (as many have done) because I have mostly read that Go outperforms Python with regard to concurrency.  So I wrote two programs to scan an AWS account's S3 buckets and report back the total size.  I performed this on an account that had more the 75 buckets totaling more than a few TB of data.

I was surprised to find that my Python implementation was nearly 2x faster than my Go implementation.  This confuses me based on all the benchmarks and literature I have read.  This leads me to believe that I did not implement my Go code correctly.  While watching both programs run I noticed that the Go implementation only used up to 15% of my CPU while Python used >85%.  Am I missing an important step with Go or am I missing something in my implementation?  Thanks in advance!

Python Code:

'''
Get the size of all objects in all buckets in S3
'''
import os
import sys
import boto3
import concurrent.futures

def get_s3_bucket_sizes(aws_access_key_id, aws_secret_access_key, aws_session_token=None):

    s3client = boto3.client('s3')

    # Create the dictionary which will be indexed by the bucket's
    # name and has an S3Bucket object as its contents
    buckets = {}

    total_size = 0.0

    #
    # Start gathering data...
    #

    # Get all of the buckets in the account
    _buckets = s3client.list_buckets()

    cnt = 1
    with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
        future_bucket_to_scan = {executor.submit(get_bucket_objects, s3client, bucket): bucket for bucket in _buckets["Buckets"]}

        for future in concurrent.futures.as_completed(future_bucket_to_scan):
            bucket_object = future_bucket_to_scan[future]

            try:
                ret = future.result()
            except Exception as exc:
                print('ERROR: %s' % (str(exc)))
            else:
                total_size += ret

    print(total_size)

def get_bucket_objects(s3client, bucket):

    name = bucket["Name"]

    # Get all of the objects in the bucket
    lsbuckets = s3client.list_objects(Bucket=name)

    size = 0
    while True:
        if "Contents" not in lsbuckets.keys():
            break

        for content in lsbuckets["Contents"]:
            size += content["Size"]

        break

    return size

#
# Main
#
if __name__=='__main__':
    get_s3_bucket_sizes(os.environ.get("AWS_ACCESS_KEY_ID"), os.environ.get("AWS_SECRET_ACCESS_KEY"))

Go Code:

package main

import (
    "fmt"
    "sync"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/awserr"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

type S3_Bucket_Response struct {
    bucket string
    count  int64
    size   int64
    err    error
}

type S3_Bucket_Request struct {
    bucket string
    region string
}

func get_bucket_objects_async(wg *sync.WaitGroup, requests chan S3_Bucket_Request, responses chan S3_Bucket_Response) {

    var size  int64
    var count int64

    for request := range requests {
        bucket := request.bucket
        region := request.region

        // Create a new response
        response := new(S3_Bucket_Response)
        response.bucket = bucket

        sess, err := session.NewSession(&aws.Config{
            Region: aws.String(region),
        })

        s3conn := s3.New(sess)

        resp, err := s3conn.ListObjectsV2(&s3.ListObjectsV2Input{
            Bucket: aws.String(bucket),
        })

        if err != nil {
            if awsErr, ok := err.(awserr.Error); ok {

                switch awsErr.Code() {
                case "NoSuchBucket":
                    response.err = fmt.Errorf("Bucket: (%s) is NoSuchBucket.  Must be in process of deleting.", bucket)
                case "AccessDenied":
                    response.err = fmt.Errorf("Bucket: (%s) is AccessDenied.  You should really be running this with full Admin Privaleges", bucket)
                }
            } else {
                response.err = fmt.Errorf("Listing Objects Unhandled Error: %s ", err)
            }

            responses <- *response
            continue
        }

        contents := resp.Contents
        size      = 0
        count     = 0

        for i:=0; i<len(contents); i++ {
            size  += *contents[i].Size
            count += 1
        }

        response.size  = size
        response.count = count

        responses <- *response
    }

    wg.Done()
}

func main() {

    var err  error
    var size int64
    var resp *s3.ListBucketsOutput
    var wg sync.WaitGroup

    sess, _ := session.NewSession()
    s3conn  := s3.New(sess)

    // Get account bucket listing
    if resp, err = s3conn.ListBuckets(&s3.ListBucketsInput{});err != nil {
        fmt.Println("Error listing buckets: %s", err)
        return
    }

    buckets := resp.Buckets
    size = 0

    // Create the buffered channels
    requests  := make(chan S3_Bucket_Request , len(buckets))
    responses := make(chan S3_Bucket_Response, len(buckets))

    for i := range buckets {

        bucket := *buckets[i].Name

        resp2, err := s3conn.GetBucketLocation(&s3.GetBucketLocationInput{
            Bucket: aws.String(bucket),
        })

        if err != nil {
            fmt.Printf("Could not get bucket location for bucket (%s): %s", bucket, err)
            continue
        }

        wg.Add(1)
        go get_bucket_objects_async(&wg, requests, responses)

        region := "us-east-1"
        if resp2.LocationConstraint != nil {
            region = *resp2.LocationConstraint
        }

        request := new(S3_Bucket_Request)
        request.bucket = bucket
        request.region = region

        requests <- *request
    }

    // Close requests channel and wait for responses
    close(requests)
    wg.Wait()
    close(responses)

    cnt := 1
    // Process the results as they come in
    for response := range responses {

        fmt.Printf("Bucket: (%s) complete!  Buckets remaining: %d\n", response.bucket, len(buckets)-cnt)

        // Did the bucket request have errors?
        if response.err != nil {
            fmt.Println(response.err)
            continue
        }

        cnt  += 1
        size += response.size
    }

    fmt.Println(size)
    return
}


来源: stackexchange

问:
How do I escape values in SpEL?
I am writing some SpEL statements in XML, and I can't get the parser to determine when I need to escape a character.

I've attempted the following:

```<code><... property="someProperty" value="#{ someBean.aMethodOnTheBean('st\'ring') }" />
</code>```

However adding in the \' does not seem to escape that single quote and I keep receiving a parser exception.

Is there any way to escape these values?

答:
From the documentation:

"To put a single quote itself in a
  string use two single quote
  characters."

expression = 'something = ''' + someMethod.getValue + ''''

ContentLicense: CC BY-SA 2.5

Title: How do I escape values in SpEL?

Body:
I am writing some SpEL statements in XML, and I can't get the parser to determine when I need to escape a character.

I've attempted the following:

<... property="someProperty" value="#{ someBean.aMethodOnTheBean('st\'ring') }" />

However adding in the \' does not seem to escape that single quote and I keep receiving a parser exception.

Is there any way to escape these values?

ContentLicense: CC BY-SA 3.0


问:
How to draw transparent polygons?
I'm using PIL (Python Imaging Library).  I'd like to draw transparent polygons.  It seems that specifying a fill color that includes alpha level does not work.  Are their workarounds?

If it can't be done using PIL I'm willing to use something else.

If there is more than one solution, then performance should be factored in.  The drawing needs to be as fast as possible.

答:
To do that you can use Shapely and OpenCV like this:</p>

```<code>import cv2
import numpy as np
from shapely.geometry import Polygon

alpha = 0.5 # that's your transparency factor
path = 'path_to_image.jpg'
image = cv2.imread(path)
(H, W) = image.shape[:2]

xmin = 0
ymin = 0
xmax = int(W / 2)
ymax = int(H / 2)

polygon = Polygon([(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)])
int_coords = lambda x: np.array(x).round().astype(np.int32)
exterior = [int_coords(polygon.exterior.coords)]

overlay = image.copy()
cv2.fillPoly(overlay, exterior, color=(255, 255, 0))
cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)
cv2.imshow("Polygon", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
</code>```

OwnerDisplayName: carrier

ContentLicense: CC BY-SA 4.0

Title: How to draw transparent polygons?

LastEditorDisplayName: carrier

Body:
To do that you can use Shapely and OpenCV like this:

import cv2
import numpy as np
from shapely.geometry import Polygon

alpha = 0.5 # that's your transparency factor
path = 'path_to_image.jpg'
image = cv2.imread(path)
(H, W) = image.shape[:2]

xmin = 0
ymin = 0
xmax = int(W / 2)
ymax = int(H / 2)

polygon = Polygon([(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)])
int_coords = lambda x: np.array(x).round().astype(np.int32)
exterior = [int_coords(polygon.exterior.coords)]

overlay = image.copy()
cv2.fillPoly(overlay, exterior, color=(255, 255, 0))
cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)
cv2.imshow("Polygon", image)
cv2.waitKey(0)
cv2.destroyAllWindows()


Title: Convert list of dict to lists using python

Body:
Have two lists with values :

[{'_id': ObjectId('5ab123e123c075f123ec11ab'),
  'stp_id': ObjectId('5ab000c12345ac9d12345678'),
  'date': Timestamp('2019-11-01 05:38:44.089000')}]

[{'_id': ObjectId('5ab123e123c075f123ec11ad'),
  'stp_id': ObjectId('5ab000c12345ac9d12345679'),
  'date': Timestamp('2019-10-01 05:38:44.089000')},
{'_id': ObjectId('5ab123e123c075f123ec11ac'),
  'stp_id': ObjectId('5ab000c12345ac9d12345679'),
  'date': Timestamp('2019-10-01 05:38:44.089000')}]

how to convert these list of dicts to dicts like this :

{{'_id': ObjectId('5ab123e123c075f123ec11ab'),
  'stp_id': ObjectId('5ab000c12345ac9d12345678'),
  'date': Timestamp('2019-11-01 05:38:44.089000')}}

{{'_id': ObjectId('5ab123e123c075f123ec11ad'),
  'stp_id': ObjectId('5ab000c12345ac9d12345679'),
  'date': Timestamp('2019-10-01 05:38:44.089000')},
{'_id': ObjectId('5ab123e123c075f123ec11ac'),
  'stp_id': ObjectId('5ab000c12345ac9d12345679'),
  'date': Timestamp('2019-10-01 05:38:44.089000')}}

Body:
{{'_id': ObjectId('5ab123e123c075f123ec11ad'),
  'stp_id': ObjectId('5ab000c12345ac9d12345679'),
  'date': Timestamp('2019-10-01 05:38:44.089000')},
{'_id': ObjectId('5ab123e123c075f123ec11ac'),
  'stp_id': ObjectId('5ab000c12345ac9d12345679'),
  'date': Timestamp('2019-10-01 05:38:44.089000')}}

is not a dict because a dict needs to have a key and a value

But you can instead individual dictionaries from the list of dictionaaries

Try this:

li = [{'_id': ObjectId('5ab123e123c075f123ec11ad'),
       'stp_id': ObjectId('5ab000c12345ac9d12345679'),
       'date': Timestamp('2019-10-01 05:38:44.089000')},
      {'_id': ObjectId('5ab123e123c075f123ec11ac'),
       'stp_id': ObjectId('5ab000c12345ac9d12345679'),
       'date': Timestamp('2019-10-01 05:38:44.089000')}]

for x in li:
    print(dict(x))


问:
How can I numerically solve an equation with Sympy?
I am interested in one solution, not all. As an example, take

```<code># Circle with radius 3, infinitely many solutions
x^2 + y^2 = 9
</code>```

There are two nice solutions because the values are integers (x=3, y=0 and x=0, y=3), but I would be fine with any solution.

What I tried

```<code>from sympy import nsolve, Eq, symbols

x, y = symbols("x y")
eq = Eq(x**2 + y**2, 9)
print(nsolve(eq, [x, y], [0, 0]))
</code>```

but I get

```<code>ValueError: expected a one-dimensional and numerical function
</code>```

I also tried

```<code>print(nsolve([eq], [x, y], [0, 0]))
</code>```

but got

```<code>NotImplementedError: need at least as many equations as variables
</code>```

Is sympy not able to do what I want to do?

答:
nsolve (as the error message says) cannot solve a single equation in two variables. You must add a second equation involving one or both of the symbols

```<code>>>> list(nsolve((eq,x),(x,y),(0,1)))  # 2nd equation is x = 0
[0, 3.00000000000000]
>>> list(nsolve((eq,x-1),(x,y),(0,1)))  #...x = 1
[1.00000000000000, 2.82842712474619]
>>> list(nsolve((eq,x-y/2),(x,y),(0,1)))  # x = y/2
[1.34164078649987, 2.68328157299975]
</code>```

Body:
I am interested in one solution, not all. As an example, take

# Circle with radius 3, infinitely many solutions
x^2 + y^2 = 9

There are two nice solutions because the values are integers (x=3, y=0 and x=0, y=3), but I would be fine with any solution.

What I tried

from sympy import nsolve, Eq, symbols

x, y = symbols("x y")
eq = Eq(x**2 + y**2, 9)
print(nsolve(eq, [x, y], [0, 0]))

but I get

ValueError: expected a one-dimensional and numerical function

I also tried

print(nsolve([eq], [x, y], [0, 0]))

but got

NotImplementedError: need at least as many equations as variables

Is sympy not able to do what I want to do?

Body:
nsolve (as the error message says) cannot solve a single equation in two variables. You must add a second equation involving one or both of the symbols

>>> list(nsolve((eq,x),(x,y),(0,1)))  # 2nd equation is x = 0
[0, 3.00000000000000]
>>> list(nsolve((eq,x-1),(x,y),(0,1)))  #...x = 1
[1.00000000000000, 2.82842712474619]
>>> list(nsolve((eq,x-y/2),(x,y),(0,1)))  # x = y/2
[1.34164078649987, 2.68328157299975]


问:
Seaborn stripplot with violin plot bars in front of points
I would like to draw a violin plot behind a jitter stripplot. The resulting plot has the mean/std bar behind the jitter points which makes it hard to see. I'm wondeing if there's a way to bring the bar in front of the points.

```<code>import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.violinplot(x="day", y="total_bill", data=tips, color="0.8")
sns.stripplot(x="day", y="total_bill", data=tips, jitter=True)
plt.show()
</code>```

答:
I just came across the same problem and could fix it simply by adjusting the zorderparameter in sns.stripplot:

```<code>import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.violinplot(x="day", y="total_bill", data=tips, color="0.8")
sns.stripplot(x="day", y="total_bill", data=tips, jitter=True, zorder=1)
plt.show()
</code>```

The result then similar to the answer of @ImportanceOfBeingErnest:

Body:
I would like to draw a violin plot behind a jitter stripplot. The resulting plot has the mean/std bar behind the jitter points which makes it hard to see. I'm wondeing if there's a way to bring the bar in front of the points.

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.violinplot(x="day", y="total_bill", data=tips, color="0.8")
sns.stripplot(x="day", y="total_bill", data=tips, jitter=True)
plt.show()

Body:
I just came across the same problem and could fix it simply by adjusting the zorderparameter in sns.stripplot:

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.violinplot(x="day", y="total_bill", data=tips, color="0.8")
sns.stripplot(x="day", y="total_bill", data=tips, jitter=True, zorder=1)
plt.show()

The result then similar to the answer of @ImportanceOfBeingErnest:


问:
Parallel.Invoke Not working as expected in C#
I am calling a SSIS Package from my C# code. The package processes one by one file and deletes it from a particular folder.

As the package processes one file by one, I want show the progress in a progressbar.

I have written this function to fetch the progress:

```<code>private void fileIteration(string folderPath)
{
    int initialFileCount, fileCount, processCount;
    initialFileCount = Directory.GetFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly).Length;
    this.Invoke(new MethodInvoker(delegate (){ progressBar1.Value = initialFileCount;}));

    if (initialFileCount > 0)
    {
        fileCount = Directory.GetFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly).Length;

        while (fileCount != 0)
        {
            // Thread.Sleep(2000);
            fileCount = Directory.GetFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly).Length;
            processCount = initialFileCount - fileCount;
            this.Invoke(new MethodInvoker(delegate () { progressBar1.Increment(processCount); }));
            initialFileCount = initialFileCount + processCount;
        }
    }
}
</code>```

Since both tasks should execute in parallel, I am invoking like this

```<code>  Parallel.Invoke(

            () => fileIteration(folderPath),// for prgressbar
            () => results = package.Execute()); // for calling package
</code>```

However both are not working together for me. I tried to keep 100 files and kept folder open so as to crosscheck the fast process is making me to unable to see the progressbar. It is not the case. Any suggestions?

答:
The Parallel.Invoke is a blocking method. If you are calling it from the UI thread, a deadlock will occur, because the fileIteration method calls back the UI thread in order to update the progress bar. So everything will get stuck.

This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination.

My suggestion is to ditch both the Parallel.Invoke and the Invoke(new MethodInvoker shenanigans, and embrace the async/await:

```<code>private async void Button1_Click(object sender, EventArgs e)
{
    int filesCountInit = await GetFilesCountAsync();
    if (filesCountInit == 0) return;
    progressBar1.Value = 0;
    var ssisTask = Task.Run(() => _package.Execute());
    while (!ssisTask.IsCompleted)
    {
        await Task.WhenAny(ssisTask, Task.Delay(1000));
        int filesCount = await GetFilesCountAsync();
        int percentDone = Math.Max(0, 100 - (filesCount * 100 / filesCountInit));
        progressBar1.Value = percentDone;
    }
    if (ssisTask.Status == TaskStatus.RanToCompletion)
        progressBar1.Value = 100;
    var results = await ssisTask;
    // Display the results
}

private Task<int> GetFilesCountAsync()
{
    return Task.Run(() => Directory.GetFiles(
        _folderPath, "*.*", SearchOption.TopDirectoryOnly).Length);
}
</code>```

Body:
I am calling a SSIS Package from my C# code. The package processes one by one file and deletes it from a particular folder.

As the package processes one file by one, I want show the progress in a progressbar.

I have written this function to fetch the progress:

private void fileIteration(string folderPath)
{
    int initialFileCount, fileCount, processCount;
    initialFileCount = Directory.GetFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly).Length;
    this.Invoke(new MethodInvoker(delegate (){ progressBar1.Value = initialFileCount;}));

    if (initialFileCount > 0)
    {
        fileCount = Directory.GetFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly).Length;

        while (fileCount != 0)
        {
            // Thread.Sleep(2000);
            fileCount = Directory.GetFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly).Length;
            processCount = initialFileCount - fileCount;
            this.Invoke(new MethodInvoker(delegate () { progressBar1.Increment(processCount); }));
            initialFileCount = initialFileCount + processCount;
        }
    }
}

Since both tasks should execute in parallel, I am invoking like this

  Parallel.Invoke(

            () => fileIteration(folderPath),// for prgressbar
            () => results = package.Execute()); // for calling package

However both are not working together for me. I tried to keep 100 files and kept folder open so as to crosscheck the fast process is making me to unable to see the progressbar. It is not the case. Any suggestions?

Body:
The Parallel.Invoke is a blocking method. If you are calling it from the UI thread, a deadlock will occur, because the fileIteration method calls back the UI thread in order to update the progress bar. So everything will get stuck.

This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination.

My suggestion is to ditch both the Parallel.Invoke and the Invoke(new MethodInvoker shenanigans, and embrace the async/await:

private async void Button1_Click(object sender, EventArgs e)
{
    int filesCountInit = await GetFilesCountAsync();
    if (filesCountInit == 0) return;
    progressBar1.Value = 0;
    var ssisTask = Task.Run(() => _package.Execute());
    while (!ssisTask.IsCompleted)
    {
        await Task.WhenAny(ssisTask, Task.Delay(1000));
        int filesCount = await GetFilesCountAsync();
        int percentDone = Math.Max(0, 100 - (filesCount * 100 / filesCountInit));
        progressBar1.Value = percentDone;
    }
    if (ssisTask.Status == TaskStatus.RanToCompletion)
        progressBar1.Value = 100;
    var results = await ssisTask;
    // Display the results
}

private Task<int> GetFilesCountAsync()
{
    return Task.Run(() => Directory.GetFiles(
        _folderPath, "*.*", SearchOption.TopDirectoryOnly).Length);
}


问:
How to remove the first character of a variable
HI All,

In my js file i am getting an array of records (negetive values ) like this

```<code>deletedDetailIds = $.map(eformDetailIds, function(e) { return e < 0 ? e : null; });
</code>```

For example the deletedDetailIds = -1234,-1235,-1256 like this.

Now i have to create an array and add these values with out negetive symbols. like

newArray = 1234,1235,1256.

Please help me in this

答:
if u are willing for replace -ve symbol then,</p>

```<code>deletedDetailIds.replace(/-/g, "");
</code>```

Body:
HI All,

In my js file i am getting an array of records (negetive values ) like this

deletedDetailIds = $.map(eformDetailIds, function(e) { return e < 0 ? e : null; });

For example the deletedDetailIds = -1234,-1235,-1256 like this.

Now i have to create an array and add these values with out negetive symbols. like

newArray = 1234,1235,1256.

Please help me in this

Body:
if u are willing for replace -ve symbol then,

deletedDetailIds.replace(/-/g, "");


问:
duplicate params get array by node.js express or vue router
I have a big problem.

On all other project I've done I used apache or Nginx and PHP.
If I have a Url like /test?param=1&param=2 the value of $_REUEST['param'] is 2.

But in Node.js (Express) I get an array [1,2]

I could use new URLSearchParams(this.$route.fullPath) and than the method .get() or .getAll() to get only one value. But we have a big project and many loops and usages of this.$route.query in several files.

Is it possible to filter this.$route.query on one place?
Or is express able to change the return of this params like apache/nginx?

答:
This is indeed a problem as express's underlying parser works this way. As a workaround you could write a little helper to extract your desired value in case the query param is an array. Something like:

```<code>function getQueryParam(req, queryParam) {
    if (!req.query) {
        return undefined;
    }
    if (Array.isArray(req.query[queryParam])) {
        const paramArray = req.query[queryParam];
        return paramArray[paramArray.length - 1]; // always return the last provided value
    }
    return req.query[queryParam];
}
</code>```

You can use it like this:

```<code>app.get('/', (req, res) => {
    const paramValue = getQueryParam(req, "param");
    // do something with paramValue
});
</code>```

Body:
This is indeed a problem as express's underlying parser works this way. As a workaround you could write a little helper to extract your desired value in case the query param is an array. Something like:

function getQueryParam(req, queryParam) {
    if (!req.query) {
        return undefined;
    }
    if (Array.isArray(req.query[queryParam])) {
        const paramArray = req.query[queryParam];
        return paramArray[paramArray.length - 1]; // always return the last provided value
    }
    return req.query[queryParam];
}

You can use it like this:

app.get('/', (req, res) => {
    const paramValue = getQueryParam(req, "param");
    // do something with paramValue
});


问:
Cannot login to Vtiger api with Node.js
I'm trying to get a session token to access Vtiger's REST api.

I first get the challenge token using my username:

```<code>var USERNAME = "Josh";
var ACCESS_KEY = "DZsAkVoTeoajNCRD";
var DEFAULT_PATH = "/crm/webservice.php?";
var DEFAULT_HEADERS = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Accept-Charset": "utf-8"
};

var getChallenge = function( callback ) {

    var path = getPath("operation=getchallenge&username="+USERNAME);
    var reqOptions = {
              host: HOST,
              path: path,
              headers: DEFAULT_HEADERS,
              method: "get",
              rejectUnauthorized: false
    };

    request( reqOptions, function( err, result ) {
        if( err ) callback( err );
        else {
            if( !result.success ) {
                callback( new Error("Unable to generate session token") );
                return;
            }

            callback( undefined, result.result.token );
        }
    });
};
</code>```

and that returns successfully with:

```<code>{ success: true,
  result:
   { token: '5361062e11cd6',
     serverTime: 1398867502,
     expireTime: 1398867802 } }
</code>```

Using that token I try to login:

```<code>var login = function( callback ) {
    getChallenge( function( err, token ) {
        if(err) callback( err );
        else {
            var hashedKey = mcrypto.MD5(token+ACCESS_KEY).toString(mcrypto.enc.Hex);
            //var hashedKey = mcrypto.createHash("md5").update(token+ACCESS_KEY).digest("hex");
            var path = getPath("operation=login&username="+USERNAME+"&accessKey="+hashedKey);
            var reqOptions = {
                      host: HOST,
                      path: path,
                      headers: DEFAULT_HEADERS,
                      method: "post",
                      rejectUnauthorized: false
            };

            request( reqOptions, function( err, result ) {
                if(err) callback( err );
                else {
                    callback(undefined, result);
                }
            });
        }
    });
};
</code>```

but it keeps failing:

```<code>{ success: false,
  error:
   { code: 'INVALID_AUTH_TOKEN',
     message: 'Specified token is invalid or expired' } }
</code>```

I know I'm using the right vtiger server url and proper username because the first request is successful and I know my accessKey is good so what could be the problem?

答:
It's been a while but i had the same issue as OP , and after looking on the web, i found and other solution to this issue, that was related to the HEADERS of the post request. Using axios, my code look like this :

```<code>const FormData = require('form-data');
const axios = require('axios');

const formData = new FormData();
formData.append('foo', 'bar');
const res = await axios.post('<YOUR_POST_URL>', formData, {
  // You need to use `getHeaders()` in Node.js because Axios doesn't
  // automatically set the multipart form boundary in Node.
  headers: formData.getHeaders()
});
</code>```

After that, my node application works perfectly and i can get my sessionName from Vtiger !

Title: Cannot login to Vtiger api with Node.js

Body:
I'm trying to get a session token to access Vtiger's REST api.

I first get the challenge token using my username:

var USERNAME = "Josh";
var ACCESS_KEY = "DZsAkVoTeoajNCRD";
var DEFAULT_PATH = "/crm/webservice.php?";
var DEFAULT_HEADERS = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Accept-Charset": "utf-8"
};

var getChallenge = function( callback ) {

    var path = getPath("operation=getchallenge&username="+USERNAME);
    var reqOptions = {
              host: HOST,
              path: path,
              headers: DEFAULT_HEADERS,
              method: "get",
              rejectUnauthorized: false
    };

    request( reqOptions, function( err, result ) {
        if( err ) callback( err );
        else {
            if( !result.success ) {
                callback( new Error("Unable to generate session token") );
                return;
            }

            callback( undefined, result.result.token );
        }
    });
};

and that returns successfully with:

{ success: true,
  result:
   { token: '5361062e11cd6',
     serverTime: 1398867502,
     expireTime: 1398867802 } }

Using that token I try to login:

var login = function( callback ) {
    getChallenge( function( err, token ) {
        if(err) callback( err );
        else {
            var hashedKey = mcrypto.MD5(token+ACCESS_KEY).toString(mcrypto.enc.Hex);
            //var hashedKey = mcrypto.createHash("md5").update(token+ACCESS_KEY).digest("hex");
            var path = getPath("operation=login&username="+USERNAME+"&accessKey="+hashedKey);
            var reqOptions = {
                      host: HOST,
                      path: path,
                      headers: DEFAULT_HEADERS,
                      method: "post",
                      rejectUnauthorized: false
            };

            request( reqOptions, function( err, result ) {
                if(err) callback( err );
                else {
                    callback(undefined, result);
                }
            });
        }
    });
};

but it keeps failing:

{ success: false,
  error:
   { code: 'INVALID_AUTH_TOKEN',
     message: 'Specified token is invalid or expired' } }

I know I'm using the right vtiger server url and proper username because the first request is successful and I know my accessKey is good so what could be the problem?

Body:
It's been a while but i had the same issue as OP , and after looking on the web, i found and other solution to this issue, that was related to the HEADERS of the post request. Using axios, my code look like this :

const FormData = require('form-data');
const axios = require('axios');

const formData = new FormData();
formData.append('foo', 'bar');
const res = await axios.post('<YOUR_POST_URL>', formData, {
  // You need to use `getHeaders()` in Node.js because Axios doesn't
  // automatically set the multipart form boundary in Node.
  headers: formData.getHeaders()
});

After that, my node application works perfectly and i can get my sessionName from Vtiger !


问:
What is the scancode of "Pause/Break" key?
I download "SharpKeys.exe" from here. It works well. But I want to change "Pause/Break" to another, this app cannot recognize this key correctly. Does any one know the scancode? Thx  </p>

答:
I remapped Pause / Break key flawlessly using only SharpKeys! This is how I did it:

I also downloaded SharpKeys (which, by the way, is awesome), and at first was also unable to change the Pause/Break key to another key (my case wanna change to Media:Play/Pause).

After a little research, found this page blog: https://www.neox.net/w/2008/02/13/keyboard-remap-pause-break-key-as-del-key/

There, the guy was able to remap the key just using the first 2 bytes of the sequence. E1 and 1D. (he also messed around with Del key, which is unnecessary)

Since SharpKeys is already a great tool, I just download its code and added to the key map table this key code E1 1D and was able to remap using it without any further modifications.

Long story short:

I downloaded the source code of SharpKeys, and added to its code, file Dialog_Main.cs, method BuildParseTables(), the line m_hashKeys.Add("E1_1D", "-by Vitox: Pause/Break");

After that, I was able to remap the Pause/Break key flawlessly.

Resources:

Source code of SharpKeys, after changes

Interface of SharpKeys, after mod

SharpKeys: https://github.com/randyrants/sharpkeys

by the way, Randy Rants, this is a great tool. Thank You!

note: the keyboard where this mod worked was a Logitech G413, and OS was Windows 7


问:
Kubernetes cluster Mysql Nodes Storage
We have started setting up a Kubernetes cluster. On Production, we have 4 Mysql Nodes(2 Active Master, 2 Active slaves). Complete servers are on-premise, There is NO cloud providers usage.

Now how do I configure storage? I mean should I use PV / PVC? How will it work. Should I use local PV? Can someone explain to me this?

答:
You need to use PersistentVolumes and PersistentVolumeClaims in order to achieve that.

A PersistentVolume (PV) is a piece of storage in the cluster that has
  been provisioned by an administrator or dynamically provisioned using
  Storage Classes.

A PersistentVolumeClaim (PVC) is a request for storage by a user.
  Claims can request specific size and access modes (e.g., they can be
  mounted once read/write or many times read-only).

Containers are ephemeral. When the container is restarted all the changes made prior to it are lost. Databases, however expect the data is persistent, therefore you need persistent volumes. You have to create a storage claim and the pod must be configured to mount the claimed storage.

Here you will find a simple guide showing how to deploy MySQL with a PersistentVolume. However, I strongly recommend getting familiar with the official docs that I have linked in order to fully understand the concept and adjust the access mode, class, size, etc according to your needs.

Please let me know if that helped.

Title: Kubernetes cluster Mysql Nodes Storage


问:
Generic Data fetch API based on entity name and its primary key
I'm developing a generic API to fetch data based on the Entity name and its primary key.
URL for get mapping: api/fetch/{id}/data/{entity}
There are many entities present like student, course, instructor, class...
Based on the entity name, the API should return data for that entity by given id in URL.
What should be the best approach using spring boot and JPA?
Trying below, but cannot work when entities are large in number and keep on increasing. Need a generic approach.

```<code>    @RestController
    public class Datacontroller{
    @Autowired
    CourseRepo courserepo;

    @Autowired
    Studentrepo studentrepo;

    @GetMapping("api/fetch/{id}/data/{entity}")
        public <T> T getData(@PathVariable("id") String id, @PathVariable("entity") String entity) {
            T l = null;
            //depending on entity
            if("course".equals(entity)) {
                Optional<Course> c = courserepo.findById(id);
                l=(T) c.get();
            }

            if("student".equals(entity)) {
                Optional<Student> a = studentrepo.findById(id);
                l = (T) a.get();
            }

            return l;
    }
</code>```

答:
We can fetch all entities and get entity class from entityName. Once we have class, we can use find method from EntityManager to get the particular record by primary id.</p>

```<code>public static Class<?> getEntityClass(EntityManager entityManager, String entityName) {
        for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) {
            if (entityName.equals(entity.getName())) {
                return entity.getJavaType();
            }
        }
        return null;
    }
</code>```

Body:
I'm developing a generic API to fetch data based on the Entity name and its primary key.
URL for get mapping: api/fetch/{id}/data/{entity}
There are many entities present like student, course, instructor, class...
Based on the entity name, the API should return data for that entity by given id in URL.
What should be the best approach using spring boot and JPA?
Trying below, but cannot work when entities are large in number and keep on increasing. Need a generic approach.

    @RestController
    public class Datacontroller{
    @Autowired
    CourseRepo courserepo;

    @Autowired
    Studentrepo studentrepo;

    @GetMapping("api/fetch/{id}/data/{entity}")
        public <T> T getData(@PathVariable("id") String id, @PathVariable("entity") String entity) {
            T l = null;
            //depending on entity
            if("course".equals(entity)) {
                Optional<Course> c = courserepo.findById(id);
                l=(T) c.get();
            }

            if("student".equals(entity)) {
                Optional<Student> a = studentrepo.findById(id);
                l = (T) a.get();
            }

            return l;
    }

Body:
We can fetch all entities and get entity class from entityName. Once we have class, we can use find method from EntityManager to get the particular record by primary id.

public static Class<?> getEntityClass(EntityManager entityManager, String entityName) {
        for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) {
            if (entityName.equals(entity.getName())) {
                return entity.getJavaType();
            }
        }
        return null;
    }


问:
is it possible to get rid of the relative paths on imports?
in any project with node.js, react, i set up the env variable:

```<code>NODE_PATH=src/
</code>```

and then i can do imports like

```<code>import COLORS from 'Constants/Colors'
</code>```

instead of

```<code>import COLORS from '../../../Constants/Colors'
</code>```

I just started with react-native, with expo and am trying to find a solution for this.

I tried the same approach of adding the env variable and it doesn't work

答: You can use babel-plugin-module-resolver library to simplify your imports paths. It allows to write alias imports instead of complex relative paths.</p>

Body:
in any project with node.js, react, i set up the env variable:

NODE_PATH=src/

and then i can do imports like

import COLORS from 'Constants/Colors'

instead of

import COLORS from '../../../Constants/Colors'

I just started with react-native, with expo and am trying to find a solution for this.

I tried the same approach of adding the env variable and it doesn't work


问:
unindent does not match any outer indentation level error when compiling the Python code
Error:

```<code>unindent does not match any outer indentation level
</code>```

Code:

```<code>def confirmation(self):
    if WebDriverWait(self.driver, 1000).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'failed'))):
        print("Checkout Failed!")

     if WebDriverWait(self.driver, 1000).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'succesful'))):
        print("Checkout Succesful!")
</code>```

How can I address the error?

答:
The indent of the 2nd 'if' does not match the first, it is in one extra space.  In python, your indentation must be consistent

Basically the places you could indent that 2nd 'if' would be even with the first 'if' or even with the first 'print' (in which case the 2nd 'if' would be within the first 'if').  The current location of the 2nd 'if' doesn't match any of these previous levels so python throws an error.

Body:
Error:

unindent does not match any outer indentation level

Code:

def confirmation(self):
    if WebDriverWait(self.driver, 1000).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'failed'))):
        print("Checkout Failed!")

     if WebDriverWait(self.driver, 1000).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'succesful'))):
        print("Checkout Succesful!")

How can I address the error?


问:
Export sub-folder name as path and create a csv for each parent sub-folder in a different location
I am trying to export the sub-folder names (as path) of each parent folder individually and create a csv for each one (1,2,3) in a different location.

Tree example looks like this:

```<code>C:\USERS\XXXXX\DESKTOP\TEST
├───1
│   ├───11
│   ├───111
│   └───1111
├───2
│   ├───22
│   ├───222
│   └───2222
└───3
    ├───33
    ├───333
    └───3333
</code>```

Script I am using is:

```<code>$parentDir = "C:\Users\XXXXX\Desktop\Test";
Get-ChildItem -Recurse -Force -Depth 2 -Attribute Directory -Path $parentDir | Select-Object -Property FullName | Export-Csv Test.csv
</code>```

The problem is that it will show the result for the Test folder, I want to export the content of each sub-folder and create a csv with their name in a different location, the process has to be repeated for 42 folders and changing the $parentDir for each one is time consuming..

答:
If I understand correctly, it might be better to do this using two Get-ChildItem commands:

```<code>$parentDir = "C:\USERS\XXXXX\DESKTOP\TEST"
$outputDir = "C:\USERS\XXXXX\DESKTOP"
Get-ChildItem -Path $parentDir -Force -Directory | ForEach-Object {
    $outCsv = Join-Path -Path $outputDir -ChildPath "$($_.Name).csv"
    $_ | Get-ChildItem -Directory | Select-Object FullName | Export-Csv -Path $outCsv -NoTypeInformation
}
</code>```

Using your example folders, this wil write 3 csv files on your desktop, named 1.csv, 2.csv and 3.csv with content:

```
"FullName"
"D:\Test\1\11"
"D:\Test\1\111"
"D:\Test\1\1111"
```

```
"FullName"
"D:\Test\2\22"
"D:\Test\2\222"
"D:\Test\2\2222"
```

```
"FullName"
"D:\Test\3\33"
"D:\Test\3\333"
"D:\Test\3\3333"
```

For old PowerShell versions, the -Directory switch is not available and you should use

```<code>Get-ChildItem -Path $parentDir -Force | Where-Object { $_.PsIsContainer } | ForEach-Object { ... }
</code>```

Body:
I am trying to export the sub-folder names (as path) of each parent folder individually and create a csv for each one (1,2,3) in a different location.

Tree example looks like this:

C:\USERS\XXXXX\DESKTOP\TEST
├───1
│   ├───11
│   ├───111
│   └───1111
├───2
│   ├───22
│   ├───222
│   └───2222
└───3
    ├───33
    ├───333
    └───3333

Script I am using is:

$parentDir = "C:\Users\XXXXX\Desktop\Test";
Get-ChildItem -Recurse -Force -Depth 2 -Attribute Directory -Path $parentDir | Select-Object -Property FullName | Export-Csv Test.csv

The problem is that it will show the result for the Test folder, I want to export the content of each sub-folder and create a csv with their name in a different location, the process has to be repeated for 42 folders and changing the $parentDir for each one is time consuming..

Body:
If I understand correctly, it might be better to do this using two Get-ChildItem commands:

$parentDir = "C:\USERS\XXXXX\DESKTOP\TEST"
$outputDir = "C:\USERS\XXXXX\DESKTOP"
Get-ChildItem -Path $parentDir -Force -Directory | ForEach-Object {
    $outCsv = Join-Path -Path $outputDir -ChildPath "$($_.Name).csv"
    $_ | Get-ChildItem -Directory | Select-Object FullName | Export-Csv -Path $outCsv -NoTypeInformation
}

Using your example folders, this wil write 3 csv files on your desktop, named 1.csv, 2.csv and 3.csv with content:

"FullName"
"D:\Test\1\11"
"D:\Test\1\111"
"D:\Test\1\1111"

"FullName"
"D:\Test\2\22"
"D:\Test\2\222"
"D:\Test\2\2222"

"FullName"
"D:\Test\3\33"
"D:\Test\3\333"
"D:\Test\3\3333"

For old PowerShell versions, the -Directory switch is not available and you should use

Get-ChildItem -Path $parentDir -Force | Where-Object { $_.PsIsContainer } | ForEach-Object { ... }


问:
can I mutate a selector value?
I have an app using NGRX.

I have a question, I know the store is a readonly place where you can't mutate object directly.
But when I use a selector

Like if I have the following ngrx selector

```<code>export const getSelectedMission = (state: State): Readonly<IMission> => state.selectedMission;
export const selectedMission: MemoizedSelector<object, Readonly<string>> = createSelector(
  selectMissionState,
  getSelectedMissionId
);
</code>```

If in my app I do this

```<code>this.store$.pipe(select(MissionsStoreSelectors.selectedMission)).subscribe((mission) => mission.name = "123")
</code>```

Is this a valid operation ? or it should be avoided ?

If I want to select portion in the store, and be able to edit those value (to dispatch action again in the store), what would be the best way ? Should I make all my selector return a copy of the object/array ?

EDIT :

It's fine to do so, but only if you do a copy of the object.

What if I do like this in the component :

```<code>this.selectedMission$ = this.store$.pipe(select(MissionsStoreSelectors.selectedMission))
</code>```

Then in the template I pass this value to a child

```<code><app-child [mission]="selectedMission$ | async"></app-child>
</code>```

And in the child I so at some point

```<code>mission.name = "123"
</code>```

This would edit the direct reference of the value in the store right? but the child component shouldn't care about making copy of the object before mutating it.

答:
It's fine to give back a different result. But you should clone the object like this:

```<code>this.store$.pipe(select(MissionsStoreSelectors.selectedRouteId)).subscribe((mission) => ({...mission, name: "123"}))
</code>```

EDIT after EDIT

The whole idea of a store is that if you change a value of an object from the store that it should be via the store. So if your component, for whatever reason, wants do mutations directly on a object from the store, then the component should clone the object.

Title: can I mutate a selector value?

Body:
I have an app using NGRX.

I have a question, I know the store is a readonly place where you can't mutate object directly.
But when I use a selector

Like if I have the following ngrx selector

export const getSelectedMission = (state: State): Readonly<IMission> => state.selectedMission;
export const selectedMission: MemoizedSelector<object, Readonly<string>> = createSelector(
  selectMissionState,
  getSelectedMissionId
);

If in my app I do this

this.store$.pipe(select(MissionsStoreSelectors.selectedMission)).subscribe((mission) => mission.name = "123")

Is this a valid operation ? or it should be avoided ?

If I want to select portion in the store, and be able to edit those value (to dispatch action again in the store), what would be the best way ? Should I make all my selector return a copy of the object/array ?

EDIT :

It's fine to do so, but only if you do a copy of the object.

What if I do like this in the component :

this.selectedMission$ = this.store$.pipe(select(MissionsStoreSelectors.selectedMission))

Then in the template I pass this value to a child

<app-child [mission]="selectedMission$ | async"></app-child>

And in the child I so at some point

mission.name = "123"

This would edit the direct reference of the value in the store right? but the child component shouldn't care about making copy of the object before mutating it.

Body:
It's fine to give back a different result. But you should clone the object like this:

this.store$.pipe(select(MissionsStoreSelectors.selectedRouteId)).subscribe((mission) => ({...mission, name: "123"}))

EDIT after EDIT

The whole idea of a store is that if you change a value of an object from the store that it should be via the store. So if your component, for whatever reason, wants do mutations directly on a object from the store, then the component should clone the object.


问:
Translation of Plus and Minus button previous and next to EditText Respectively Code that is in Kotlin to Java
Hello i am having an Edit text that has a minus and plus button next to and after the Edit text. The edit text will have an input option for inputting a number then the minus button will decrease the value of the entered number in the edit text upon being pressed and the plus button will increase the value of the entered number in the edit text upon being pressed. The issue am having is that am following a tutorial and the code in the Main activity that has the on Click listeners for the button is in Kotlin and am well familiar with java compared to Kotlin, i would appreciate a nice translation of the code in Main Activity from Kotlin to android java

XML code

```<code>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/decrease"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-" />

        <TextView
            android:id="@+id/integer_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:layout_marginTop="16dp"
            android:text="0"
            android:textStyle="bold"
            android:textSize="70sp" />

        <Button
            android:id="@+id/increase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+" />
    </LinearLayout>

</LinearLayout>

</code>```

The following code below is the one which is in Kotlin and i would appreciate if it is in android Java form

```<code>class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        increase.setOnClickListener { increaseInteger() }
        decrease.setOnClickListener { decreaseInteger() }
    }

    fun increaseInteger() {
        display(integer_number.text.toString().toInt() + 1)
    }

    fun decreaseInteger() {
        display(integer_number.text.toString().toInt() - 1)
    }

    private fun display(number: Int) {
        integer_number.setText("$number")
    }
}

</code>```

答:
So here is your code in Java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.increase).setOnClickListener(this)
        findViewById(R.id.decrease).setOnClickListener(this)
    }

    public void increaseInteger() {
        display(Integer.parseInt(integer_number.getText().toString()) + 1);
    }

    public void decreaseInteger() {
        display(Integer.parseInt(integer_number.getText().toString()) - 1);
    }

    private fun display(int number) {
        integer_number.setText(String.valueOf(number));
    }

    @Override
    protected void onClick(View v) {
        switch (v.getId()) {
            case R.id.increase:
            increaseInteger()
            break;

            case R.id.decrease:
            decreaseInteger()
            break;
        }
    }
}
```

I did it in notepad. So if you have any problem, let me know.

Body:
Hello i am having an Edit text that has a minus and plus button next to and after the Edit text. The edit text will have an input option for inputting a number then the minus button will decrease the value of the entered number in the edit text upon being pressed and the plus button will increase the value of the entered number in the edit text upon being pressed. The issue am having is that am following a tutorial and the code in the Main activity that has the on Click listeners for the button is in Kotlin and am well familiar with java compared to Kotlin, i would appreciate a nice translation of the code in Main Activity from Kotlin to android java

XML code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/decrease"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-" />

        <TextView
            android:id="@+id/integer_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:layout_marginTop="16dp"
            android:text="0"
            android:textStyle="bold"
            android:textSize="70sp" />

        <Button
            android:id="@+id/increase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+" />
    </LinearLayout>

</LinearLayout>

The following code below is the one which is in Kotlin and i would appreciate if it is in android Java form

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        increase.setOnClickListener { increaseInteger() }
        decrease.setOnClickListener { decreaseInteger() }
    }

    fun increaseInteger() {
        display(integer_number.text.toString().toInt() + 1)
    }

    fun decreaseInteger() {
        display(integer_number.text.toString().toInt() - 1)
    }

    private fun display(number: Int) {
        integer_number.setText("$number")
    }
}

Body:
So here is your code in Java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.increase).setOnClickListener(this)
        findViewById(R.id.decrease).setOnClickListener(this)
    }

    public void increaseInteger() {
        display(Integer.parseInt(integer_number.getText().toString()) + 1);
    }

    public void decreaseInteger() {
        display(Integer.parseInt(integer_number.getText().toString()) - 1);
    }

    private fun display(int number) {
        integer_number.setText(String.valueOf(number));
    }

    @Override
    protected void onClick(View v) {
        switch (v.getId()) {
            case R.id.increase:
            increaseInteger()
            break;

            case R.id.decrease:
            decreaseInteger()
            break;
        }
    }
}

I did it in notepad. So if you have any problem, let me know.


问:
render pdf.js page like real html elements not canvas or svg?
I am trying to make simple mobile UI that could be reading pdf, but I am planning to add many features by using my own made pdf read, not just by using the viewer made by pdf.js team, so I was asking if there is any way of rendering pdf as just an HTML with elements, as they did on their viewer, I am not comfortable with canvas, any help guys, Thanks in advance</p>

答:
I don't think there's such option.

The only way of displaying pdf files in a browser (not every and not mobile browsers) using native support is by passing url to iframe/embed tag

But note that this still doesn't result in HTML output you are looking for.

Body:
I don't think there's such option.

The only way of displaying pdf files in a browser (not every and not mobile browsers) using native support is by passing url to iframe/embed tag <iframe src="url_to_your.pdf">

But note that this still doesn't result in HTML output you are looking for.


问:
How do you pivot some columns into rows using sql or t-sql
Here is the table

```<code>Income|Expenses|Other|Department|Date

 2k   |      4k|  .5k|Marketing |2014-05-28
 52k  |      7k|  .1k|Sales     |2014-04-01
</code>```

Result

```<code>Description|Value|Department|Date

 Income    |   2k|Marketing |2014-05-28
 Expenses  |   4k|Marketing |2014-05-28
 Other     |  .5k|Marketing |2014-05-28
 Income    |  52k|Sales     |2014-04-01
 Expenses  |   7k|Sales     |2014-04-01
 Other     |  .1k|Sales     |2014-04-01
</code>```

Actually, I can use SELECT + UNION to get the result but the requirement is to use Pivot.

How to get this result using PIVOT function?

答:
You can unpivot your values via a Cross Apply</p>

```<code>Select B.*
      ,A.Department
      ,A.Date
 From  YourTable A
 Cross Apply ( values ('Income'  ,Income  )
                     ,('Expences',Expenses)
                     ,('Other'   ,Other   )
             ) B(Description,Value)
</code>```

Body:
Here is the table

Income|Expenses|Other|Department|Date

 2k   |      4k|  .5k|Marketing |2014-05-28
 52k  |      7k|  .1k|Sales     |2014-04-01

Result

Description|Value|Department|Date

 Income    |   2k|Marketing |2014-05-28
 Expenses  |   4k|Marketing |2014-05-28
 Other     |  .5k|Marketing |2014-05-28
 Income    |  52k|Sales     |2014-04-01
 Expenses  |   7k|Sales     |2014-04-01
 Other     |  .1k|Sales     |2014-04-01

Actually, I can use SELECT + UNION to get the result but the requirement is to use Pivot.

How to get this result using PIVOT function?

Body:
You can unpivot your values via a Cross Apply

Select B.*
      ,A.Department
      ,A.Date
 From  YourTable A
 Cross Apply ( values ('Income'  ,Income  )
                     ,('Expences',Expenses)
                     ,('Other'   ,Other   )
             ) B(Description,Value)


问:
View Controllers deinitialized still memory leak
I have deinit method in all my VCs to check if they are being deinitialized or not

```<code> deinit {
        print("\(self.description) successfully deinitialized")
    }
</code>```

When I logout, I see all my view controllers are deinitialized and my LoginViewController becomes the root view controller. But I can still see the RAM usage as 90MB. Every time I log in and logout, it keeps increasing.
Is it possible if my view controller has deinitialized, still it can have a memory leak?

What I know is, if the view controller is deinitialized, there is no memory leak. If there is a memory leak, the view controller will not deinitialize.

Am I wrong? Please help.

UPDATE: As per Luca's suggestion, and with the help of this answer, I found out the memory leaks. But I am not able to understand any of this. Is there any way to make it more readable?

答:
Even though the ViewController is de-initialised it doesn't necessarily mean it will be deallocated. There is a possibility there is a strong reference cycle. Or maybe in your code, to avoid retain cycle you are using unowned reference somewhere, for which the reference count is not becoming zero. The object will not be de-allocated until both strong and unowned reference counts become zero. In that case try using weak instead of unowned and see if it works. Weak references point to something called side table instead of directly pointing to the object, and hence don't keep the object from getting deallocated once de-initialised

Check out this article to understand how unowned reference can sometimes keep the object from getting deallocated, and ARC in general.

Body:
I have deinit method in all my VCs to check if they are being deinitialized or not

 deinit {
        print("\(self.description) successfully deinitialized")
    }

When I logout, I see all my view controllers are deinitialized and my LoginViewController becomes the root view controller. But I can still see the RAM usage as 90MB. Every time I log in and logout, it keeps increasing.
Is it possible if my view controller has deinitialized, still it can have a memory leak?

What I know is, if the view controller is deinitialized, there is no memory leak. If there is a memory leak, the view controller will not deinitialize.

Am I wrong? Please help.

UPDATE: As per Luca's suggestion, and with the help of this answer, I found out the memory leaks. But I am not able to understand any of this. Is there any way to make it more readable?


问:
How to excute code after form validation within Google Tag Manager constrictions?
I need to call a tracking code when a form is submit and after it is validated using Google Tag Manager. I do not have direct access to the site code. I can only execute script from Google Tag Manager.

I've searched the web for an answer, but I could not find something that worked.

I've tried:

```<code>jQuery('#formId').on('submit',function(){
    console.log('test');
})
</code>```

But this prints "test" before the form validates the fields, so if I leave a field blank, it still prints "test".

I've tried different approaches that I found:

```<code>jQuery('#formId').on('submit',function(){
    if (jQuery('#formId').valid()){
     console.log('test');
    }
})
</code>```

or

```<code>jQuery('#formId').on('submit',function(){
    if (jQuery(this).valid()){
     console.log('test');
    }
})
</code>```

or

```<code>jQuery('#formId').on('submit',function(event){
    if (jQuery(event).valid()){
     console.log('test');
    }
})
</code>```

But they all return VM914:2 Uncaught TypeError: jQuery(...).valid is not a function

How can I run a script from Google Tag Manager that runs on formsubmit after the form validation?

答:
you seem to be trying to use the jquery validate plugin, which is not native jquery. you can add it via a script tag:

```<code><script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
</code>```

but to use the valid function, you actually need to set validation rules ahead of time:

```<code>var form = jQuery('#formId');

form.validate()
form.on('submit', function(event) {
  if (form.valid()) {
    consoel.log('good now')
  }
})
</code>```

this will cause validation against any web native validation directives on the inputs in the form, if this is NOT using web native validation directive, (like it's using angular or some external lib), you'll need to manually define validation rules in your validate function or do something completely different... here are the docs: https://jqueryvalidation.org/

fair warning, the validate and valid functions actually do DOM manipulation and can override event listeners applied elsewhere, so tread carefully. You're doing this with your hands tied behind your back not just having access to the site code.

here's a stackblitz: https://stackblitz.com/edit/jquery-m7qkpy?file=index.html

if you can't add an external plugin, well then that changes a lot.

Body:
I need to call a tracking code when a form is submit and after it is validated using Google Tag Manager. I do not have direct access to the site code. I can only execute script from Google Tag Manager.

I've searched the web for an answer, but I could not find something that worked.

I've tried:

jQuery('#formId').on('submit',function(){
    console.log('test');
})

But this prints "test" before the form validates the fields, so if I leave a field blank, it still prints "test".

I've tried different approaches that I found:

jQuery('#formId').on('submit',function(){
    if (jQuery('#formId').valid()){
     console.log('test');
    }
})

or

jQuery('#formId').on('submit',function(){
    if (jQuery(this).valid()){
     console.log('test');
    }
})

or

jQuery('#formId').on('submit',function(event){
    if (jQuery(event).valid()){
     console.log('test');
    }
})

But they all return VM914:2 Uncaught TypeError: jQuery(...).valid is not a function

How can I run a script from Google Tag Manager that runs on formsubmit after the form validation?

Body:
you seem to be trying to use the jquery validate plugin, which is not native jquery. you can add it via a script tag:

<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>

but to use the valid function, you actually need to set validation rules ahead of time:

var form = jQuery('#formId');

form.validate()
form.on('submit', function(event) {
  if (form.valid()) {
    consoel.log('good now')
  }
})

this will cause validation against any web native validation directives on the inputs in the form, if this is NOT using web native validation directive, (like it's using angular or some external lib), you'll need to manually define validation rules in your validate function or do something completely different... here are the docs: https://jqueryvalidation.org/

fair warning, the validate and valid functions actually do DOM manipulation and can override event listeners applied elsewhere, so tread carefully. You're doing this with your hands tied behind your back not just having access to the site code.

here's a stackblitz: https://stackblitz.com/edit/jquery-m7qkpy?file=index.html

if you can't add an external plugin, well then that changes a lot.


答:
You can use apply :

```<code>select tt.Description, tt.Value, t.Department, t.Date
from table t cross apply
     ( values ('Income', Income), ('Expenses', Expenses), ('Other', Other)
     ) tt(Description, Value);
</code>```

Title: How do you pivot some columns into rows using sql or t-sql

Body:
You can use apply :

select tt.Description, tt.Value, t.Department, t.Date
from table t cross apply
     ( values ('Income', Income), ('Expenses', Expenses), ('Other', Other)
     ) tt(Description, Value);


答:
Use abs:

So
    Math.abs(number)

Where number will be the negative number

Title: How to remove the first character of a variable

Body:
HI All,

In my js file i am getting an array of records (negetive values ) like this

deletedDetailIds = $.map(eformDetailIds, function(e) { return e < 0 ? e : null; });

For example the deletedDetailIds = -1234,-1235,-1256 like this.

Now i have to create an array and add these values with out negetive symbols. like

newArray = 1234,1235,1256.

Please help me in this


问:
Show all tabs in Android Studio
Is there a way of showing all source code tabs in Android Studio one one horizontal line (in the tab bar) and scroll through them?

When there are too many source code tabs open in Android Studio, some of them are moved to the drop down list at the top right corner of the code edit window. The tabs that are moved to this drop down list are not shown in the tab bar any more.

The tabs in the tab bar can be scrolled with the mouse wheel, but only the ones that have not been moved to the drop down list.

Is there a way to remove the drop down list and keep all tabs in the tab bar?

答:
In android Studio 4.0.1 you can find tabs configuration in:

Window->Editor Tabs->Configure Editor Tabs...

After, uncheck "Show tabs in one row" checkbox if it is selected.

Title: Show all tabs in Android Studio


问:
Google Cloud Pub/Sup API: Almost 100% Errors on StreamingPull but still works?
I have just started using Google Cloud Pub/Sub as part of an event-based architecture. I was somewhat surprised when I just checked the API dashboard and saw that I have almost 100% errors on the StreamingPull method. However, it seems like everything is working as intended.

I am using a listener within a node.js app (which is running on a server) and another one in a locally run python script (currently in development and doesn't run right now).

My node.js instantiation looks like this:

```<code>const { PubSub } = require("@google-cloud/pubsub");

client = new PubSub({
    projectId: conf.GOOGLE_PROJECT_ID,
    credentials: {
        client_email: conf.GOOGLE_CLIENT_EMAIL,
        private_key: conf.GOOGLE_CLIENT_PRIVATE_KEY
    },
});

const subscription = client.subscription(subscriptionName);

subscription.on("message", (message) => {
    console.log(`Received message ${message.id}:`);
    console.log(JSON.parse(message.data));
    const parsedMessage = JSON.parse(message.data).message;

    if (parsedMessage) {
      ... do things ...
    }
    message.ack()
});

const errorHandler = function (error) {
    console.error(`ERROR: ${error}`);
    throw error;
};

subscription.on("error", errorHandler);
</code>```

Can anybody shed some light on this?

答:
This is expected and explained in the documentation:

"StreamingPull streams are always terminated with a non-OK status. Note that, unlike in regular RPCs, the status here is simply an indication that the stream has been broken, not that requests are failing. Therefore, while the StreamingPull API may have a seemingly surprising 100% error rate, this is by design."

Body:
I have just started using Google Cloud Pub/Sub as part of an event-based architecture. I was somewhat surprised when I just checked the API dashboard and saw that I have almost 100% errors on the StreamingPull method. However, it seems like everything is working as intended.

I am using a listener within a node.js app (which is running on a server) and another one in a locally run python script (currently in development and doesn't run right now).

My node.js instantiation looks like this:

const { PubSub } = require("@google-cloud/pubsub");

client = new PubSub({
    projectId: conf.GOOGLE_PROJECT_ID,
    credentials: {
        client_email: conf.GOOGLE_CLIENT_EMAIL,
        private_key: conf.GOOGLE_CLIENT_PRIVATE_KEY
    },
});

const subscription = client.subscription(subscriptionName);

subscription.on("message", (message) => {
    console.log(`Received message ${message.id}:`);
    console.log(JSON.parse(message.data));
    const parsedMessage = JSON.parse(message.data).message;

    if (parsedMessage) {
      ... do things ...
    }
    message.ack()
});

const errorHandler = function (error) {
    console.error(`ERROR: ${error}`);
    throw error;
};

subscription.on("error", errorHandler);

Can anybody shed some light on this?


问:
SQL "IS NOT NULL" in SAP HANA SQLScript
Actually I tried to run a is null SQL Statement on a SAP HANA Database using the SAP HANA Studio. This does not work because SQLScript has no is not null or is null function. My statement looks like:

```<code>Select *
From MSEG
Where KDAUF is null
</code>```

Unfortunately it does not work. Does anybody know an alternative approach which is practicable using SAP HANA SQLScript? On the internet I found a hint to either use NULLIF or COALESCE. But I neither know how to use this function nor to adapt it to a working WHERE condition.

答:
Try to use it this way</p>

```<code>Select * from MSEG Where KDAUF **NE** ''
</code>```

Title: SQL "IS NOT NULL" in SAP HANA SQLScript

Body:
Actually I tried to run a is null SQL Statement on a SAP HANA Database using the SAP HANA Studio. This does not work because SQLScript has no is not null or is null function. My statement looks like:

Select *
From MSEG
Where KDAUF is null

Unfortunately it does not work. Does anybody know an alternative approach which is practicable using SAP HANA SQLScript? On the internet I found a hint to either use NULLIF or COALESCE. But I neither know how to use this function nor to adapt it to a working WHERE condition.

Body:
Try to use it this way

Select * from MSEG Where KDAUF **NE** ''


问:
Overwrite a value in the .Xlsx if a pariculat string is found using Pandas Python
I have an excel sheet and I can get the necessary block of cell using the below piece of code.

```<code>   import pandas as pd

    ENVIRONMENT="SIMM"
    excel_file_name=r'C:\Users\spadravsasthu\Desktop\SVN\HSRI-DEV\TEST\checklist.xlsx'
    workbook_sheet = pd.read_excel(excel_file_name,sheet_name='Sheet1',keep_default_na=False)
    df=pd.DataFrame(workbook_sheet,columns=['Environment', 'Available chunk start value','Available chunk end value'])
df['Available chunk start value'] = np.where((df.Environment == 'SIMM'),
                                             df['Available chunk start value'].str.replace('776021800','123456789'),
                                             df['Available chunk start value'])
</code>```

Below is the output of df

```<code>    Environment Available chunk start value Available chunk end value
   7         SIMM                         NaN                 799900000
</code>```

The original values was

```<code> Environment Available chunk start value Available chunk end value
       7         SIMM        776021800                  799900000
</code>```

From the above, I want to change 776021800 to 123456789 in the dataframe, but it making it as NaN. Any idea on how can that be done using Pandas!

答:
You can use np.where to conditionally replace right in the original dataframe, then save that to Excel.

```<code>df = pd.DataFrame({
    'Environment': ['SIMM', 'notSIMM', 'SIMM'],
    'start_value': ['793951420', '793951420', 'something else']
})

print(df)

  Environment     start_value
0        SIMM       793951420
1     notSIMM       793951420
2        SIMM  something else

ENVIRONMENT = 'SIMM'
df['start_value'] = np.where(df.Environment.str.match(ENVIRONMENT),
                             df['start_value'].str.replace('793951420','793956789'),
                             df['start_value'])
print(df)

  Environment     start_value
0        SIMM       793956789  <-- only this changed
1     notSIMM       793951420
2        SIMM  something else
</code>```

Edit: For your specific case (note, this saves output to new sheet Sheet2:

```<code>import pandas as pd

ENVIRONMENT="SIMM"
excel_file_name=r'C:\Users\spadravsasthu\Desktop\SVN\HSRI-DEV\TEST\checklist.xlsx'
df = pd.read_excel(excel_file_name,sheet_name='Sheet1',keep_default_na=False)
df = df[['Environment', 'Available chunk start value','Available chunk end value']]
df['Available chunk start value'] = np.where(df.Environment.str.match(ENVIRONMENT),
                                             df['Available chunk start value'].str.replace('793951420','793956789'),
                                             df['Available chunk start value'])

with pd.ExcelWriter(excel_file_name, mode='a') as writer:
    df.to_excel(writer, sheet_name='Sheet2')
</code>```

Body:
I have an excel sheet and I can get the necessary block of cell using the below piece of code.

   import pandas as pd

    ENVIRONMENT="SIMM"
    excel_file_name=r'C:\Users\spadravsasthu\Desktop\SVN\HSRI-DEV\TEST\checklist.xlsx'
    workbook_sheet = pd.read_excel(excel_file_name,sheet_name='Sheet1',keep_default_na=False)
    df=pd.DataFrame(workbook_sheet,columns=['Environment', 'Available chunk start value','Available chunk end value'])
df['Available chunk start value'] = np.where((df.Environment == 'SIMM'),
                                             df['Available chunk start value'].str.replace('776021800','123456789'),
                                             df['Available chunk start value'])

Below is the output of df

    Environment Available chunk start value Available chunk end value
   7         SIMM                         NaN                 799900000

The original values was

 Environment Available chunk start value Available chunk end value
       7         SIMM        776021800                  799900000

From the above, I want to change 776021800 to 123456789 in the dataframe, but it making it as NaN. Any idea on how can that be done using Pandas!

Body:
You can use np.where to conditionally replace right in the original dataframe, then save that to Excel.

df = pd.DataFrame({
    'Environment': ['SIMM', 'notSIMM', 'SIMM'],
    'start_value': ['793951420', '793951420', 'something else']
})

print(df)

  Environment     start_value
0        SIMM       793951420
1     notSIMM       793951420
2        SIMM  something else

ENVIRONMENT = 'SIMM'
df['start_value'] = np.where(df.Environment.str.match(ENVIRONMENT),
                             df['start_value'].str.replace('793951420','793956789'),
                             df['start_value'])
print(df)

  Environment     start_value
0        SIMM       793956789  <-- only this changed
1     notSIMM       793951420
2        SIMM  something else

Edit: For your specific case (note, this saves output to new sheet Sheet2:

import pandas as pd

ENVIRONMENT="SIMM"
excel_file_name=r'C:\Users\spadravsasthu\Desktop\SVN\HSRI-DEV\TEST\checklist.xlsx'
df = pd.read_excel(excel_file_name,sheet_name='Sheet1',keep_default_na=False)
df = df[['Environment', 'Available chunk start value','Available chunk end value']]
df['Available chunk start value'] = np.where(df.Environment.str.match(ENVIRONMENT),
                                             df['Available chunk start value'].str.replace('793951420','793956789'),
                                             df['Available chunk start value'])

with pd.ExcelWriter(excel_file_name, mode='a') as writer:
    df.to_excel(writer, sheet_name='Sheet2')


问:
Recognize Missing Space
How can I recognize when a user has missed a space when entering a search term? For example, if the user enters "usbcable", I want to search for "usb cable". I'm doing a  REGEX search in MySQL to match full words.

I have a table with every term used in a search, so I know that "usb" and "cable" are valid terms. Is there a way to construct a WHERE clause that will give me all the rows where the term matches part of the string?

Something like this:

```<code>SELECT st.term
FROM SearchTerms st
WHERE 'usbcable' LIKE '%' + st.term + '%'
</code>```

Or any other ideas?

答:
Posted in the comments, but I thought it important to bring up as an answer:

Does that query not work? – Simon Buchan

Followed by:

Well, I should've tested it before I
  posted. That query does not work, but
  with a CONCAT it does, like so: WHERE
  'usbcable' LIKE Concat('%', st.term,
  '%'). I think this is the simplest,
  and most relevant (specific to my
  site), way to go. – arnie0674

Certainly far easier than text segmentation for this application...

-Adam

Title: Recognize Missing Space

Body:
How can I recognize when a user has missed a space when entering a search term? For example, if the user enters "usbcable", I want to search for "usb cable". I'm doing a  REGEX search in MySQL to match full words.

I have a table with every term used in a search, so I know that "usb" and "cable" are valid terms. Is there a way to construct a WHERE clause that will give me all the rows where the term matches part of the string?

Something like this:

SELECT st.term
FROM SearchTerms st
WHERE 'usbcable' LIKE '%' + st.term + '%'

Or any other ideas?

OwnerDisplayName: Adam Davis


问:
SyntaxError: Unexpected token { when running ts-node in Angular Package Format project
I'm trying to run a typescript file with ts-node.  Getting a weird error:

```<code> import { tes } from "./tes";
       ^
</code>```

SyntaxError: Unexpected token {
     at Module._compile (internal/modules/cjs/loader.js:720:23)

If I copy the project ts files into an empty directory and try to run the file without any configuration it works fine, so it's something in the configuration.

I also tried generating a brand new angular package format project:

```<code>ng new proj --create-application=false
cd proj
ng g library x
</code>```

And if I delete all the tsconfig files associated with the project and the library, ts-node runs fine.  When I leave them in I get the same error.

The tes file is inside a Angular Package Format library.  This is the top level tsconfig.json:

```<code>{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "csv": [
        "dist/csv/csv",
        "dist/csv"
      ]
    }
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

</code>```

This is the project specific tsconfig.lib.json :

```<code>{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/lib",
    "target": "es2015",
    "declaration": true,
    "inlineSources": true,
    "types": [],
    "lib": [
      "dom",
      "es2018"
    ]
  },
  "angularCompilerOptions": {
    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "enableResourceInlining": true
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

</code>```

Thoughts?

答:
It looks like your tsconfig is set to output js using esnext modules, which aren't understood by node without configuration. You can either:

Remove the "module": "esnext", line from your tsconfig.json

Or, assuming you want to keep your config for Angular, configure your node to use the new ES module syntax e.g. by following the answer here

Body:
I'm trying to run a typescript file with ts-node.  Getting a weird error:

 import { tes } from "./tes";
       ^

SyntaxError: Unexpected token {
     at Module._compile (internal/modules/cjs/loader.js:720:23)

If I copy the project ts files into an empty directory and try to run the file without any configuration it works fine, so it's something in the configuration.

I also tried generating a brand new angular package format project:

ng new proj --create-application=false
cd proj
ng g library x

And if I delete all the tsconfig files associated with the project and the library, ts-node runs fine.  When I leave them in I get the same error.

The tes file is inside a Angular Package Format library.  This is the top level tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "csv": [
        "dist/csv/csv",
        "dist/csv"
      ]
    }
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

This is the project specific tsconfig.lib.json :

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/lib",
    "target": "es2015",
    "declaration": true,
    "inlineSources": true,
    "types": [],
    "lib": [
      "dom",
      "es2018"
    ]
  },
  "angularCompilerOptions": {
    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "enableResourceInlining": true
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

Thoughts?