问:
elasticsearch bool query combine must with OR
I am currently trying to migrate a solr-based application to elasticsearch.

I have this lucene query:

```<code>((
    name:(+foo +bar)
    OR info:(+foo +bar)
)) AND state:(1) AND (has_image:(0) OR has_image:(1)^100)
</code>```

As far as I understand this is a combination of must clauses combined with boolean OR:

Get all documents containing (foo AND bar in name) OR (foo AND bar in info). After that filter results by condition state=1 and boost documents that have an image.

I have been trying to use a bool query with must but I am failing to get boolean OR into must clauses. Here is what I have:

```<code>GET /test/object/_search
{
  "from": 0,
  "size": 20,
  "sort": {
    "_score": "desc"
  },
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "foo"
          }
        },
        {
          "match": {
            "name": "bar"
          }
        }
      ],
      "must_not": [],
      "should": [
        {
          "match": {
            "has_image": {
              "query": 1,
              "boost": 100
            }
          }
        }
      ]
    }
  }
}
</code>```

As you can see, must conditions for info are missing.

** UPDATE **

I have updated my elasticsearch query and got rid of that function score. My base problem still exists.

答:
This is how you can nest multiple bool queries in one outer bool query
this using Kibana,

bool indicates we are using boolean

must is for AND

should is for OR

```<code>GET my_inedx/my_type/_search
{
  "query" : {
     "bool": {             //bool indicates we are using boolean operator
          "must" : [       //must is for **AND**
               {
                 "match" : {
                       "description" : "some text"
                   }
               },
               {
                  "match" :{
                        "type" : "some Type"
                   }
               },
               {
                  "bool" : {          //here its a nested boolean query
                        "should" : [  //should is for **OR**
                               {
                                 "match" : {
                                     //ur query
                                }
                               },
                               {
                                  "match" : {}
                               }
                             ]
                        }
               }
           ]
      }
  }
}
</code>```

This is how you can nest a query in ES

There are more types in "bool" like,

Filter

must_not

Body:
I am currently trying to migrate a solr-based application to elasticsearch.

I have this lucene query:

((
    name:(+foo +bar)
    OR info:(+foo +bar)
)) AND state:(1) AND (has_image:(0) OR has_image:(1)^100)

As far as I understand this is a combination of must clauses combined with boolean OR:

Get all documents containing (foo AND bar in name) OR (foo AND bar in info). After that filter results by condition state=1 and boost documents that have an image.

I have been trying to use a bool query with must but I am failing to get boolean OR into must clauses. Here is what I have:

GET /test/object/_search
{
  "from": 0,
  "size": 20,
  "sort": {
    "_score": "desc"
  },
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "foo"
          }
        },
        {
          "match": {
            "name": "bar"
          }
        }
      ],
      "must_not": [],
      "should": [
        {
          "match": {
            "has_image": {
              "query": 1,
              "boost": 100
            }
          }
        }
      ]
    }
  }
}

As you can see, must conditions for info are missing.

** UPDATE **

I have updated my elasticsearch query and got rid of that function score. My base problem still exists.

Body:
This is how you can nest multiple bool queries in one outer bool query
this using Kibana,

bool indicates we are using boolean

must is for AND

should is for OR

GET my_inedx/my_type/_search
{
  "query" : {
     "bool": {             //bool indicates we are using boolean operator
          "must" : [       //must is for **AND**
               {
                 "match" : {
                       "description" : "some text"
                   }
               },
               {
                  "match" :{
                        "type" : "some Type"
                   }
               },
               {
                  "bool" : {          //here its a nested boolean query
                        "should" : [  //should is for **OR**
                               {
                                 "match" : {
                                     //ur query
                                }
                               },
                               {
                                  "match" : {}
                               }
                             ]
                        }
               }
           ]
      }
  }
}

This is how you can nest a query in ES

There are more types in "bool" like,

Filter

must_not


来源: stackexchange

问:
Opacity CSS that works for all browsers?
Can someone recommend the safest approach for giving an OPACITY VALUE to a DIV TAG using CSS?

Erik

答:
Straight from Css-Tricks.com (this covers everything I can think of):</p>

```<code>.transparent_class {
  /* IE 8 */
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";

  /* IE 5-7 */
  filter: alpha(opacity=50);

  /* Netscape */
  -moz-opacity: 0.5;

  /* Safari 1.x */
  -khtml-opacity: 0.5;

  /* Good browsers */
  opacity: 0.5;
}
</code>```

ContentLicense: CC BY-SA 3.0

Body:
Straight from Css-Tricks.com (this covers everything I can think of):

.transparent_class {
  /* IE 8 */
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";

  /* IE 5-7 */
  filter: alpha(opacity=50);

  /* Netscape */
  -moz-opacity: 0.5;

  /* Safari 1.x */
  -khtml-opacity: 0.5;

  /* Good browsers */
  opacity: 0.5;
}


问:
from kivy.core.window._window_sdl2 import _WindowSDL2Storage [CRITICAL] [App ] Unable to get a Window, abort
I'm a novice when it comes to computers so please go easy....

Running Python 3.7 (32 bit) on Windows 10

Using PyCharm

Using Kivy 1.10.1

Running the below code:

```<code>from kivy.app import App
App().run()
</code>```

I get the following error:

```<code>C:\Users\callu\AppData\Local\Programs\Python\Python37-32\python.exe "C:/Users/callu/OneDrive/Documents/Voting app/main.py"
[INFO   ] [Logger      ] Record log in C:\Users\callu\.kivy\logs\kivy_19-06-01_54.txt
[INFO   ] [Kivy        ] v1.10.1
[INFO   ] [Python      ] v3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)]
[INFO   ] [Factory     ] 194 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_gif (img_sdl2, img_pil, img_ffpyplayer ignored)
[CRITICAL] [Window      ] Unable to find any valuable Window provider.
sdl2 - ImportError: DLL load failed: The specified module could not be found.
  File "C:\Users\callu\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\core\__init__.py", line 59, in core_select_lib
    fromlist=[modulename], level=0)
  File "C:\Users\callu\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 26, in <module>
    from kivy.core.window._window_sdl2 import _WindowSDL2Storage

[CRITICAL] [App         ] Unable to get a Window, abort.
</code>```

I've read around 10 different questions with no luck to solving my problem. I've tried adding sdl2 and glew to PATH through the following route

```<code>C:\Users\callu\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy_deps\sdl2
</code>```

I've installed all the kivy dependencies. The odd thing is whenever I try to install the dependencies again it says requirement filled, already installed....but for sdl2 I can keep installing it and it never says this, it just installs the file as though it's the first time.

答:
Try adding kivy.require('1.10.1') and import kivy

```<code>import kivy
kivy.require('1.10.1')
from kivy.app import App
App().run()

</code>```

ContentLicense: CC BY-SA 4.0

Body:
I'm a novice when it comes to computers so please go easy....

Running Python 3.7 (32 bit) on Windows 10

Using PyCharm

Using Kivy 1.10.1

Running the below code:

from kivy.app import App
App().run()

I get the following error:

C:\Users\callu\AppData\Local\Programs\Python\Python37-32\python.exe "C:/Users/callu/OneDrive/Documents/Voting app/main.py"
[INFO   ] [Logger      ] Record log in C:\Users\callu\.kivy\logs\kivy_19-06-01_54.txt
[INFO   ] [Kivy        ] v1.10.1
[INFO   ] [Python      ] v3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)]
[INFO   ] [Factory     ] 194 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_gif (img_sdl2, img_pil, img_ffpyplayer ignored)
[CRITICAL] [Window      ] Unable to find any valuable Window provider.
sdl2 - ImportError: DLL load failed: The specified module could not be found.
  File "C:\Users\callu\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\core\__init__.py", line 59, in core_select_lib
    fromlist=[modulename], level=0)
  File "C:\Users\callu\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 26, in <module>
    from kivy.core.window._window_sdl2 import _WindowSDL2Storage

[CRITICAL] [App         ] Unable to get a Window, abort.

I've read around 10 different questions with no luck to solving my problem. I've tried adding sdl2 and glew to PATH through the following route

C:\Users\callu\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy_deps\sdl2

I've installed all the kivy dependencies. The odd thing is whenever I try to install the dependencies again it says requirement filled, already installed....but for sdl2 I can keep installing it and it never says this, it just installs the file as though it's the first time.

Body:
Try adding kivy.require('1.10.1') and import kivy

import kivy
kivy.require('1.10.1')
from kivy.app import App
App().run()


问:
Running Kubernetes across cloud providers
Our goal is to run kubernetes in AWS and Azure with minimal customization (setting up kubernetes managed env), support and maintenance. We need portability of containers across cloud providers.

Our preferred cloud provider is AWS. We are planning on running containers in EKS. We wanted to understand the customization effort required to run these containers in AKS.

Would you recommend choosing a container management platform like Pivotal Cloud Foundry or Redhat OpenShift or run them on AWS EKS or AKS where customization is less to run containers across different cloud providers.

答: You need to define a common set of storage classes that map to similar volume types on each provider. If you are using some kind of provider based Ingress controller those can vary so I would recommend using an internal one like nginx or traefik. If you are using customization annotations for things like networking those can vary, but using those is pretty rare. Others k8s is k8s.</p>


问:
opencv solvePnP, all axes look great except Y
I am running solvePnPRansac on an image dataset, with 2d feature points and triangulated 3d landmark points. It runs great, and the results in rotation, and in the forward and side axes, look great. The Y axis though, is completely wrong.

I am testing the output against the ground truth from the data set, and it goes up where it should go down, and drifts off the ground truth very quickly. The other axes stay locked on for much much longer.

this strikes me as strange, how can it be correct for the other axes, and wrong for one? Surely that is not possible, I would have thought that either every axis was bad, or every axis was good.

What could i possibly be doing wrong to make this happen? And how can i debug this weirdness? My PnP code is very standard:

```<code>  cv::Mat inliers;
    cv::Mat rvec = cv::Mat::zeros(3, 1, CV_64FC1);

    int iterationsCount = 500;        // number of Ransac iterations.
    float reprojectionError = 2.0; //2.0   // maximum allowed distance to consider it an inlier.
    float confidence = 0.95;          // RANSAC successful confidence.
    bool useExtrinsicGuess = false;
    int flags = cv::SOLVEPNP_ITERATIVE;

    int num_inliers_;
    //points3D_t0
    cv::solvePnPRansac(points3D_t0, points_left_t1, intrinsic_matrix, distCoeffs, rvec, translation_stereo,
        useExtrinsicGuess, iterationsCount, reprojectionError, confidence,
        inliers, flags);
</code>```

答: I encountered similar problem for images taking by a drone  – sometimes the Y value (camera line of sight axis – the height axis in my case) was BELOW the ground. If you think about it – for a plane view (or close to a plane) - there are two possible ‘y’ solutions : before the plane and away to the plane (below and under the ground in my case). So both are a legal solutions.   </p>

Body:
I am running solvePnPRansac on an image dataset, with 2d feature points and triangulated 3d landmark points. It runs great, and the results in rotation, and in the forward and side axes, look great. The Y axis though, is completely wrong.

I am testing the output against the ground truth from the data set, and it goes up where it should go down, and drifts off the ground truth very quickly. The other axes stay locked on for much much longer.

this strikes me as strange, how can it be correct for the other axes, and wrong for one? Surely that is not possible, I would have thought that either every axis was bad, or every axis was good.

What could i possibly be doing wrong to make this happen? And how can i debug this weirdness? My PnP code is very standard:

  cv::Mat inliers;
    cv::Mat rvec = cv::Mat::zeros(3, 1, CV_64FC1);

    int iterationsCount = 500;        // number of Ransac iterations.
    float reprojectionError = 2.0; //2.0   // maximum allowed distance to consider it an inlier.
    float confidence = 0.95;          // RANSAC successful confidence.
    bool useExtrinsicGuess = false;
    int flags = cv::SOLVEPNP_ITERATIVE;

    int num_inliers_;
    //points3D_t0
    cv::solvePnPRansac(points3D_t0, points_left_t1, intrinsic_matrix, distCoeffs, rvec, translation_stereo,
        useExtrinsicGuess, iterationsCount, reprojectionError, confidence,
        inliers, flags);


问:
Check if positive value exist in dataframe
I have dataframe:

```<code>A B C D
1 0 0 2
0 1 0 0
0 0 0 0
</code>```

I need to select all values which are greater then 0 and put them in a list.
if row doesnt contain any positive value 0 should be written to list.

So, the output for given dataframe should look like this:

```<code> [1,2,1,0]
</code>```

How this can be resolved?

答:
You could apply a custom function which will process each row of the DataFrame and return a list. Then to sum returned lists.</p>

```<code>In [1]: import pandas as pd

In [2]: df = pd.read_clipboard()

In [3]: df
Out[3]:
   A  B  C  D
0  1  0  0  2
1  0  1  0  0
2  0  0  0  0

In [4]: def get_positive_values(row):
   ...:     # If all elements in a row are zeros
   ...:     # then return a list with a single zero
   ...:     if row.eq(0).all():
   ...:         return [0]
   ...:     # Else return a list with positive values only.
   ...:     return row[row.gt(0)].tolist()
   ...:
   ...:

In [5]: df.apply(get_positive_values, axis=1).sum()
Out[5]: [1, 2, 1, 0]
</code>```

Body:
I have dataframe:

A B C D
1 0 0 2
0 1 0 0
0 0 0 0

I need to select all values which are greater then 0 and put them in a list.
if row doesnt contain any positive value 0 should be written to list.

So, the output for given dataframe should look like this:

 [1,2,1,0]

How this can be resolved?

Body:
You could apply a custom function which will process each row of the DataFrame and return a list. Then to sum returned lists.

In [1]: import pandas as pd

In [2]: df = pd.read_clipboard()

In [3]: df
Out[3]:
   A  B  C  D
0  1  0  0  2
1  0  1  0  0
2  0  0  0  0

In [4]: def get_positive_values(row):
   ...:     # If all elements in a row are zeros
   ...:     # then return a list with a single zero
   ...:     if row.eq(0).all():
   ...:         return [0]
   ...:     # Else return a list with positive values only.
   ...:     return row[row.gt(0)].tolist()
   ...:
   ...:

In [5]: df.apply(get_positive_values, axis=1).sum()
Out[5]: [1, 2, 1, 0]


问:
Why a thrown exception from Task is treates as Canceled?
Why a thrown exception from Task is treated as Canceled (even though I didn't press 'x' to cancel it) and not as Faulted?
The output of the below code is:

```<code>Press 'x' to cancel
Job has been canceled
Task has been canceled
Press Enter to exit
</code>```

CODE

```<code>static void Main(string[] args)
    {
        try
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = cancellationTokenSource.Token;

            var jobTask = Task.Run(() =>
            {
                if (cancellationToken.IsCancellationRequested)
                    cancellationToken.ThrowIfCancellationRequested();

                while (true)
                {
                    if (cancellationToken.IsCancellationRequested)
                        cancellationToken.ThrowIfCancellationRequested();

                    Thread.Sleep(2000);

                    if (cancellationToken.IsCancellationRequested)
                        cancellationToken.ThrowIfCancellationRequested();

                    throw new Exception("Test Exception");
                }

            }, cancellationToken)
            .ContinueWith((t) =>
            {
                Console.WriteLine("Job has been completed");
            }, TaskContinuationOptions.OnlyOnRanToCompletion)
            .ContinueWith((t) =>
            {
                Console.WriteLine("Job has been canceled");
            }, TaskContinuationOptions.OnlyOnCanceled)
            .ContinueWith((t) =>
                {
                    Console.WriteLine("Exception thrown: {0}", t.Exception.InnerException);
                }, TaskContinuationOptions.OnlyOnFaulted);

            Task.Run(() =>
            {
                Console.WriteLine("Press 'x' to cancel");
                while (Console.ReadKey(true).KeyChar != 'x')
                {
                    Thread.Sleep(200);
                }

                cancellationTokenSource.Cancel();
            });

            try
            {
                jobTask.Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var v in ex.InnerExceptions)
                {
                    if (v is TaskCanceledException)
                        Console.WriteLine("Task has been canceled");
                    else
                        Console.WriteLine("Exception: {0} - {1}", v.GetType().Name, v.InnerException);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex.Message);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("E - {0}", ex.Message);
        }

        Console.WriteLine("Press Enter to exit");
        Console.ReadKey();
    }
}
</code>```

答:
The Task.ContinueWith method returns a new Task object that represents the continuation. If the criteria specified in the continuationOptions parameter are not met, then the continuation task is cancelled.

Your code has three calls to continueWith chained together. This actually creates four tasks:

A Task for the execution of the delegate passed to Task.Run that throws an exception after two seconds

The first continuation Task that runs only if the first Task completes successfully

The second continuation Task that runs only if the first continuation is cancelled

The third continuation Task that runs only if the second continuation is faulted

When the exception is thrown, the first Task is faulted, the first continuation is cancelled because the Task it's continuing didn't complete successfully, the second continuation runs because the first continuation cancelled, and the third continuation is cancelled because the second continuation doesn't fault.

The try block is calling Wait on jobTask, which holds the Task for the third continuation, which is always cancelled.

Microsoft documentation reference: Task.ContinueWith

Body:
Why a thrown exception from Task is treated as Canceled (even though I didn't press 'x' to cancel it) and not as Faulted?
The output of the below code is:

Press 'x' to cancel
Job has been canceled
Task has been canceled
Press Enter to exit

CODE

static void Main(string[] args)
    {
        try
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = cancellationTokenSource.Token;

            var jobTask = Task.Run(() =>
            {
                if (cancellationToken.IsCancellationRequested)
                    cancellationToken.ThrowIfCancellationRequested();

                while (true)
                {
                    if (cancellationToken.IsCancellationRequested)
                        cancellationToken.ThrowIfCancellationRequested();

                    Thread.Sleep(2000);

                    if (cancellationToken.IsCancellationRequested)
                        cancellationToken.ThrowIfCancellationRequested();

                    throw new Exception("Test Exception");
                }

            }, cancellationToken)
            .ContinueWith((t) =>
            {
                Console.WriteLine("Job has been completed");
            }, TaskContinuationOptions.OnlyOnRanToCompletion)
            .ContinueWith((t) =>
            {
                Console.WriteLine("Job has been canceled");
            }, TaskContinuationOptions.OnlyOnCanceled)
            .ContinueWith((t) =>
                {
                    Console.WriteLine("Exception thrown: {0}", t.Exception.InnerException);
                }, TaskContinuationOptions.OnlyOnFaulted);

            Task.Run(() =>
            {
                Console.WriteLine("Press 'x' to cancel");
                while (Console.ReadKey(true).KeyChar != 'x')
                {
                    Thread.Sleep(200);
                }

                cancellationTokenSource.Cancel();
            });

            try
            {
                jobTask.Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var v in ex.InnerExceptions)
                {
                    if (v is TaskCanceledException)
                        Console.WriteLine("Task has been canceled");
                    else
                        Console.WriteLine("Exception: {0} - {1}", v.GetType().Name, v.InnerException);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex.Message);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("E - {0}", ex.Message);
        }

        Console.WriteLine("Press Enter to exit");
        Console.ReadKey();
    }
}


问:
How/Where to apply business rules to POCO objects?
Let's say I have a POCO with the following:

```<code>    [DataMember]
    public Nullable<int> MetricId
    {
        get { return _metricId; }
        set
        {
            if (_metricId != value)
            {
                _metricId = value;
                OnPropertyChanged("MetricId");
            }
        }
    }
    private Nullable<int> _metricId;
</code>```

I want to validate that the MetricId is strictly greater than 0

Obivously, if I put this rule as a data annotation in this class it will be overwritten the next time I regen the poco.  Where do I put this logic?

Thanks!

答:
After reading the comments and responses, it seems that creating another class is fine, but by making it partial, it ties my business logic directly to the Entity Framework and the generated POCO code.  This is worrisome because as EF4 changes into EF5 and the T4 template changes to the T5 template what will happen to my code? Plus I just don't feel comfortable using partial classes as normal classes.

Instead, and someone can still provide a better answer (please do), I think creating a framework independent object (one not tied to EF) is better.  Then I can map it to a generic business object.  Something like:

```<code>    static Customer Map(CustomerPOCO poco)
    {
        return new Customer
        {
            CustomerId = poco.CustomerId
            ...
            ...
        };
    }
</code>```

Body:
Let's say I have a POCO with the following:

    [DataMember]
    public Nullable<int> MetricId
    {
        get { return _metricId; }
        set
        {
            if (_metricId != value)
            {
                _metricId = value;
                OnPropertyChanged("MetricId");
            }
        }
    }
    private Nullable<int> _metricId;

I want to validate that the MetricId is strictly greater than 0

Obivously, if I put this rule as a data annotation in this class it will be overwritten the next time I regen the poco.  Where do I put this logic?

Thanks!

Body:
After reading the comments and responses, it seems that creating another class is fine, but by making it partial, it ties my business logic directly to the Entity Framework and the generated POCO code.  This is worrisome because as EF4 changes into EF5 and the T4 template changes to the T5 template what will happen to my code? Plus I just don't feel comfortable using partial classes as normal classes.

Instead, and someone can still provide a better answer (please do), I think creating a framework independent object (one not tied to EF) is better.  Then I can map it to a generic business object.  Something like:

    static Customer Map(CustomerPOCO poco)
    {
        return new Customer
        {
            CustomerId = poco.CustomerId
            ...
            ...
        };
    }


问:
Reading specific chunks pandas / not reading all chunks in pandas
I am trying to use accordingly to this question and answer reading a large csv file by chunks and processing it. Since I'm not native with python I got an optimization problem and looking for a better solution here.

What my code does:

I read in the line count of my csv with

```<code>with open(file) as f:
    row_count = sum(1 for line in f)
</code>```

afterwards I "slice" my data in 30 equal sized chunks and process it accordingly to the linked answer with a for loop and pd.read_csv(file, chunksize). Since plotting 30 graphs in one is pretty unclear, I plot it every 5 steps with modulo (which may be variated). For this I use an external counter.

```<code>chunksize = row_count // 30
counter = 0
for chunk in pd.read_csv(file, chunksize=chunksize):
    df = chunk
    print(counter)
    if ((counter % 5) == 0 | (counter == 0):
        plt.plot(df["Variable"])
    counter = counter +1
plt.show()
</code>```

Now to my question:

It seems like, this loop reads the chunk size in before processing the loop, which is reasonable. I can see this, since the print(counter) steps are also fairly slow. Since I read a few million rows of a csv, it takes some time every step. Is there a way to skip the not wanted chunks in the for loop, before reading it in? I was trying out something like:

```<code>wanted_plts <- [1,5,10,15,20,25,30]
for i in wanted_plts:
   for chunk[i] in pd.read_csv(file, chunksize=chunksize):
   .
   .
</code>```

I think I have understanding issues how I can manipulate this syntax of the for loop range. There should be an elegant way to fix this.

Also: i found the .get_chunk(x) by pandas but this seems to create just one chunk of size x.

Another attempt by me is trying to subset the reader object of pd.read_csv like pd.read_csv()[0,1,2] but it seems that's not possible too.

Amendment: I'm aware plotting a lot of data in matplotlib is slow. I preprocess it earlier, but for making this code readable I removed all unnecessary parts.

答:
I've toyed with your setup, trying to find a way to skip chunks, using another rendering library like pyqtgraph or using matplotlib.pyplot subroutines instead of plot(), all to no avail.

So the only fair advice I can give you is to limit the scope of read_csv to only the data you're interested in by passing the usecols parameter.

Instead of:

```<code>for chunk in pd.read_csv(file, chunksize=chunksize):
    plt.plot(chunk['Variable'])
</code>```

Use:

```<code>for chunk in pd.read_csv(file, usecols=['Variable'], chunksize=chunksize):
    plt.plot(chunk)
</code>```

And, if you haven't already, definitely limit the number of iterations by going for the biggest chunksize you possibly can (so in your case the lowest row_count divider).

I haven't quantified their respective weight but you will gain on both the csv_read() and the plot() method overheads, even ever so slightly due to the fact that your current chunks are already quite big.

With my test data, quadrupling the chunksize cuts down processing time in half:

chunksize=1000 => executed in 12.7s

chunksize=2000 => executed in 9.06s

chunksize=3000 => executed in 7.68s

chunksize=4000 => executed in 6.94s

And specifying usecols at read time also cuts down processing time in half again:

chunksize=1000 + usecols=['Variable'] => executed in 8.33s

chunksize=2000 + usecols=['Variable'] => executed in 5.27s

chunksize=3000 + usecols=['Variable'] => executed in 4.39s

chunksize=4000 + usecols=['Variable'] => executed in 3.54s

Body:
I am trying to use accordingly to this question and answer reading a large csv file by chunks and processing it. Since I'm not native with python I got an optimization problem and looking for a better solution here.

What my code does:

I read in the line count of my csv with

with open(file) as f:
    row_count = sum(1 for line in f)

afterwards I "slice" my data in 30 equal sized chunks and process it accordingly to the linked answer with a for loop and pd.read_csv(file, chunksize). Since plotting 30 graphs in one is pretty unclear, I plot it every 5 steps with modulo (which may be variated). For this I use an external counter.

chunksize = row_count // 30
counter = 0
for chunk in pd.read_csv(file, chunksize=chunksize):
    df = chunk
    print(counter)
    if ((counter % 5) == 0 | (counter == 0):
        plt.plot(df["Variable"])
    counter = counter +1
plt.show()

Now to my question:

It seems like, this loop reads the chunk size in before processing the loop, which is reasonable. I can see this, since the print(counter) steps are also fairly slow. Since I read a few million rows of a csv, it takes some time every step. Is there a way to skip the not wanted chunks in the for loop, before reading it in? I was trying out something like:

wanted_plts <- [1,5,10,15,20,25,30]
for i in wanted_plts:
   for chunk[i] in pd.read_csv(file, chunksize=chunksize):
   .
   .

I think I have understanding issues how I can manipulate this syntax of the for loop range. There should be an elegant way to fix this.

Also: i found the .get_chunk(x) by pandas but this seems to create just one chunk of size x.

Another attempt by me is trying to subset the reader object of pd.read_csv like pd.read_csv()[0,1,2] but it seems that's not possible too.

Amendment: I'm aware plotting a lot of data in matplotlib is slow. I preprocess it earlier, but for making this code readable I removed all unnecessary parts.

Body:
I've toyed with your setup, trying to find a way to skip chunks, using another rendering library like pyqtgraph or using matplotlib.pyplot subroutines instead of plot(), all to no avail.

So the only fair advice I can give you is to limit the scope of read_csv to only the data you're interested in by passing the usecols parameter.

Instead of:

for chunk in pd.read_csv(file, chunksize=chunksize):
    plt.plot(chunk['Variable'])

Use:

for chunk in pd.read_csv(file, usecols=['Variable'], chunksize=chunksize):
    plt.plot(chunk)

And, if you haven't already, definitely limit the number of iterations by going for the biggest chunksize you possibly can (so in your case the lowest row_count divider).

I haven't quantified their respective weight but you will gain on both the csv_read() and the plot() method overheads, even ever so slightly due to the fact that your current chunks are already quite big.

With my test data, quadrupling the chunksize cuts down processing time in half:

chunksize=1000 => executed in 12.7s

chunksize=2000 => executed in 9.06s

chunksize=3000 => executed in 7.68s

chunksize=4000 => executed in 6.94s

And specifying usecols at read time also cuts down processing time in half again:

chunksize=1000 + usecols=['Variable'] => executed in 8.33s

chunksize=2000 + usecols=['Variable'] => executed in 5.27s

chunksize=3000 + usecols=['Variable'] => executed in 4.39s

chunksize=4000 + usecols=['Variable'] => executed in 3.54s


问:
Spatial data with mongodb or cassandra
I am considering a Proof of concept for handling large volumes of data like > 10 G which requires atleast 200+ writes per second and about 50+ reads per second of spatial related data. This is a growing system as well. Currently I am considering moving this big volume data into a NoSql big table kind of db for performance reasons.

I have considered and taken some closer look at MongoDB and cassandra. As far as my reading goes,

Mongodb:
  - seems to have a writer lock problem
  - one of the posts in stackoverflow suggested this db if there is no need for multiple servers
  - indexes kept on memory. So the bigger the index growth, the performance is said to deteriorate
  - advantage is Mongodb has direct support for spatial data & indexing along with features like finding nearby locations etc.,
  - I see this post Cassandra Or MongoDB For Our Location Based Application suggesting mongodb as the best choice

Cassandra:

  - Seems to be the best of among the related dbs
  - Seems to have great write as well as read performance
  - Does not natively support spatial indexing but this can be extended via geohashing

My heart actually goes out for mongodb because of its good documentation and direct support for spatial data. Has any body had bad experience using mongodb for such big systems? I actually see lot of posts on mongodb iostat for performance.

If mongodb is not suited, can someone give some pointers on geohashing using cassandra? I saw the link http://code.google.com/p/geospatialweb/ for creating the hashes. But there are questions on how to query etc.?

答:
tl;dr

Elassandra a combination out of Cassandra and ElasticSearch.

A little update from the future.

I'm currently on creating a concept for a Big Data Real-time system and also need to store geospatial data and do queries at scale. The last days I did a lot of research how to arrange the data properly and be able to support a geospatial index and queries like a bounding box.

The first I read about was PostgreSQL + Postgis but the biggest instance is limited to max 200k writes/sec.

The second was a geospatial database, Tile38, which is able to scale queries but not the writes. The only way with this would be to shard the data manually.

The third was MongoDB because there you can find a good documentation supporting the geospatial functionality I need, but it was hard to decide, if you are able to scale the writes.

So the last database was Cassandra. This database is well known for the horizontal write scaling and failure-takeover. The trade-off with Cassandra is, that querying the data has not good performance and does not support geo spatial out of the box. For querying the data at scale ElasticSearch is a good solution, as Tracker1 already suggested. Today I found a new database made up of Cassandra and ElasticSearch, called Elassandra which allows writes at scale and also reading data at scale in near-realtime. So far for me the best solution, with a minimum effort for setup and maintenance.

Title: Spatial data with mongodb or cassandra


问:
How to make std::istream_iterator read only until the end of line?
Having the following piece of code:

std::vector<int64> values;
std::copy(
  std::istream_iterator<int64>(std::cin),
  std::istream_iterator<int64>(),
  std::back_inserter(values)
);
```

I'd like to make it read the input stream only until the end of line. How can I do that with std::istream_iterator?

答:
If you want the functionality without adding a std::getline or std::stringstream, you can change the character classification of the stream so that a newline is not considered discardable whitespace. Here is a minimal example:

struct set_newline_as_ws : std::ctype<char> {
  static const mask* make_table( std::ctype_base::mask m ) {
    static std::vector<mask> v(classic_table(), classic_table() + table_size);
    v['\n'] &= m;
    return &v[0];
  }
  set_newline_as_ws( bool skip, std::size_t refs = 0 ) : ctype(make_table(skip ? ~space : space), false, refs) {}
};

std::istream& skipnewline( std::istream& is ) {
  is.imbue(std::locale(is.getloc(), new std::ctype<char>));
  return is;
}

std::istream& noskipnewline( std::istream& is ) {
  is.imbue(std::locale(is.getloc(), new set_newline_as_ws(true)));
  return is;
}

int main() {
  std::vector<int64> values;
  std::cin >> noskipnewline;
  std::copy(
    std::istream_iterator<int64>(std::cin),
    std::istream_iterator<int64>(),
    std::back_inserter(values)
  );
  std::cin >> skipnewline;
}
```

Body:
Having the following piece of code:

std::vector<int64> values;
std::copy(
  std::istream_iterator<int64>(std::cin),
  std::istream_iterator<int64>(),
  std::back_inserter(values)
);

I'd like to make it read the input stream only until the end of line. How can I do that with std::istream_iterator?


问:
How to get properties of an object stored in a List
I retrieve data from a firebase realtime database which I then store into a List, so that I can display the data.

My Problem is, that the displayed data doesn't show the stored object with its properties, but the so called name/label or something simliar to this.

What is being displayed:

com.example.husiapp2.Homework@1abbfc6

What should be displayed (Object with properties):

```<code>Homework
-Title,
-Class
</code>```

My code:

```<code>public class Homework {
    public String Title;
    public String Class;
    public String ExpireDate;

    public Homework(){}

    public Homework(String title, String aClass,String expireDate) {
        Title = title;
        Class = aClass;
        ExpireDate = expireDate;
    }

}
</code>```

```<code> DatabaseReference ref;
 Homework homework;
 final List<Homework> hw = new ArrayList<>();

 ListView hwlist;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_hw);
        Log.d("HWAC122",hw.toString());
        hwlist = findViewById(R.id.hwlist);
        ref = FirebaseDatabase.getInstance().getReference("User").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Homework");

        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds:dataSnapshot.getChildren()){
                    homework = new Homework();
                    homework.Title = ds.getValue(Homework.class).Title;
                    homework.Class = ds.getValue(Homework.class).Class;
                    homework.ExpireDate = ds.getValue(Homework.class).ExpireDate;
                    Log.d("test123",homework.toString());
                    hw.add(homework);
                }
                SetAdapter();
            }

        });

    }
 public void SetAdapter(){
        ArrayAdapter adapter = new ArrayAdapter<Homework>(this,android.R.layout.simple_list_item_1,hw);
        hwlist.setAdapter(adapter);
    }
</code>```

答:
com.example.husiapp2.Homework@1abbfc6 is the standard string format for objects that don't override toString().  It's composed of the class of the object and its location in the JVM's memory.  If you want toString() to generate something else, you'll have to override it and make it return what you want to print.

```<code>public class Homework {
    public String Title;
    public String Class;
    public String ExpireDate;

    public Homework(){}

    public Homework(String title, String aClass,String expireDate) {
        Title = title;
        Class = aClass;
        ExpireDate = expireDate;
    }

    // Override toString
    @Override
    public String toString() {
        return "Whatever you want it to be";
    }
}
</code>```

It's not always best to override toString().  Many times it's better to just write some other function, pass it the object, and have it return the formatted string. Either way, you'll have to write that code that generates the string.

Body:
I retrieve data from a firebase realtime database which I then store into a List, so that I can display the data.

My Problem is, that the displayed data doesn't show the stored object with its properties, but the so called name/label or something simliar to this.

What is being displayed:

com.example.husiapp2.Homework@1abbfc6

What should be displayed (Object with properties):

Homework
-Title,
-Class

My code:

public class Homework {
    public String Title;
    public String Class;
    public String ExpireDate;

    public Homework(){}

    public Homework(String title, String aClass,String expireDate) {
        Title = title;
        Class = aClass;
        ExpireDate = expireDate;
    }

}

 DatabaseReference ref;
 Homework homework;
 final List<Homework> hw = new ArrayList<>();

 ListView hwlist;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_hw);
        Log.d("HWAC122",hw.toString());
        hwlist = findViewById(R.id.hwlist);
        ref = FirebaseDatabase.getInstance().getReference("User").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Homework");

        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds:dataSnapshot.getChildren()){
                    homework = new Homework();
                    homework.Title = ds.getValue(Homework.class).Title;
                    homework.Class = ds.getValue(Homework.class).Class;
                    homework.ExpireDate = ds.getValue(Homework.class).ExpireDate;
                    Log.d("test123",homework.toString());
                    hw.add(homework);
                }
                SetAdapter();
            }

        });

    }
 public void SetAdapter(){
        ArrayAdapter adapter = new ArrayAdapter<Homework>(this,android.R.layout.simple_list_item_1,hw);
        hwlist.setAdapter(adapter);
    }

Body:
com.example.husiapp2.Homework@1abbfc6 is the standard string format for objects that don't override toString().  It's composed of the class of the object and its location in the JVM's memory.  If you want toString() to generate something else, you'll have to override it and make it return what you want to print.

public class Homework {
    public String Title;
    public String Class;
    public String ExpireDate;

    public Homework(){}

    public Homework(String title, String aClass,String expireDate) {
        Title = title;
        Class = aClass;
        ExpireDate = expireDate;
    }

    // Override toString
    @Override
    public String toString() {
        return "Whatever you want it to be";
    }
}

It's not always best to override toString().  Many times it's better to just write some other function, pass it the object, and have it return the formatted string. Either way, you'll have to write that code that generates the string.


问:
React Native Expo Push notification for specific user
Expo allows you to send notification by device id which is ambiguous and not great for my use case, how can i send notification that only specific user will receive? for example can i pass user id or username besides device id?

this is the body request body:

```<code>{
    to: pushToken, // user id or topic?
    body: 'test notification',
    data: { withSome: 'data' }
}
</code>```

答: You have to create a db table where you'll record user_id / username & device_token in order to be able to send notification to a user by his user_id or username.</p>

Body:
Expo allows you to send notification by device id which is ambiguous and not great for my use case, how can i send notification that only specific user will receive? for example can i pass user id or username besides device id?

this is the body request body:

{
    to: pushToken, // user id or topic?
    body: 'test notification',
    data: { withSome: 'data' }
}


问:
How to delete an event on FullCalendar?
I am trying to delete an event when it is being clicked.

This is what I have and I have no idea

```<code>        eventClick: function(calEvent, jsEvent, view) {

            alert('Event: ' + calEvent.title);
            alert('Coordinates: ' + jsEvent.pageX + ',' + jsEvent.pageY);
            alert('View: ' + view.name);
            alert('This needs to be deleted');

        // change the border color just for fun
        $(this).css('border-color', 'red');
        $('#calendar').fullCalendar(
            'removeEvents'
            [this] );

    },
</code>```

The solution is actually here: https://fullcalendar.io/docs/event_data/removeEvents/
but I still cant follow.
Please Help!

答:
For anyone using react, this is what I ended up doing.

```<code>eventRender = ({ event, el }) => {
  const duration = moment.duration(moment(event.end).diff(event.start))
  const hours = duration.asHours()

  el.style.border = `1px solid ${event.backgroundColor}`
  el.className = `${el.className} event-class` // allows showing the edit and remove buttons only when hovering over the event

  if (!event.extendedProps.published && !event.allDay) {
      el.className = el.className + ' unpublished'  //it grays out the event if it hasn't been published
  }

  const child = document.createElement('div')
  child.innerHTML = `
      <div>
        <div  style="${styles.title}" class="pt-4">
          <strong>${hours} hrs </strong>
        </div>

        <div class="event-actions">
          <button style="${styles.editStyle}" data-event-id=${event.id}>
            <i class='fa fa-edit'></i>
          </button>
          <button style="${styles.removeStyle}" data-event-id=${event.id}>
            <i class='fa fa-trash-o'></i>
          </button>
        </div>
    </div>`

  el.appendChild(child)
  const btns = el.getElementsByTagName('button')
  const self = this
  btns[0].addEventListener('click', e => {
    self.onEditEvent(e)
  })
  btns[1].addEventListener('click', e => {
    self.onRemoveEvent(e)
 })
}
</code>```

and on my stylesheets:

```<code> .fc-time-grid .fc-event {
   overflow: auto;
 }

 .event-class .event-actions {
   display: none;
 }

 .event-class:hover .event-actions {
   display: flex;
   justify-content: space-around;
   font-size: 1.75rem;
   padding-top: 4px;
 }

.unpublished {
  background-image: url('../assets/img/unpublish.png');
}
</code>```

And this is how it looks:

Hope you find this helpful.
Peace

Title: How to delete an event on FullCalendar?

Body:
I am trying to delete an event when it is being clicked.

This is what I have and I have no idea

        eventClick: function(calEvent, jsEvent, view) {

            alert('Event: ' + calEvent.title);
            alert('Coordinates: ' + jsEvent.pageX + ',' + jsEvent.pageY);
            alert('View: ' + view.name);
            alert('This needs to be deleted');

        // change the border color just for fun
        $(this).css('border-color', 'red');
        $('#calendar').fullCalendar(
            'removeEvents'
            [this] );

    },

The solution is actually here: https://fullcalendar.io/docs/event_data/removeEvents/
but I still cant follow.
Please Help!

Body:
For anyone using react, this is what I ended up doing.

eventRender = ({ event, el }) => {
  const duration = moment.duration(moment(event.end).diff(event.start))
  const hours = duration.asHours()

  el.style.border = `1px solid ${event.backgroundColor}`
  el.className = `${el.className} event-class` // allows showing the edit and remove buttons only when hovering over the event

  if (!event.extendedProps.published && !event.allDay) {
      el.className = el.className + ' unpublished'  //it grays out the event if it hasn't been published
  }

  const child = document.createElement('div')
  child.innerHTML = `
      <div>
        <div  style="${styles.title}" class="pt-4">
          <strong>${hours} hrs </strong>
        </div>

        <div class="event-actions">
          <button style="${styles.editStyle}" data-event-id=${event.id}>
            <i class='fa fa-edit'></i>
          </button>
          <button style="${styles.removeStyle}" data-event-id=${event.id}>
            <i class='fa fa-trash-o'></i>
          </button>
        </div>
    </div>`

  el.appendChild(child)
  const btns = el.getElementsByTagName('button')
  const self = this
  btns[0].addEventListener('click', e => {
    self.onEditEvent(e)
  })
  btns[1].addEventListener('click', e => {
    self.onRemoveEvent(e)
 })
}

and on my stylesheets:

 .fc-time-grid .fc-event {
   overflow: auto;
 }

 .event-class .event-actions {
   display: none;
 }

 .event-class:hover .event-actions {
   display: flex;
   justify-content: space-around;
   font-size: 1.75rem;
   padding-top: 4px;
 }

.unpublished {
  background-image: url('../assets/img/unpublish.png');
}

And this is how it looks:

Hope you find this helpful.
Peace


问:
Is There A Way To Make "Invisible" User Defaults?
My last question was going this path before, but I think this warrants a new question because I want to to go for this path now (instead of attempting to do what I was trying to do in my other question).

What I want to do is to have "Invisible" values in NSUserDefaults. I don't care about Apple's guidelines because my app is aimed at Jailbroken phones. This is what I have so far:

The root.plist in settings.bundle looks like this (so far, because I want to add a password field later):

first_launch is a value that I don't want the user to modify directly. As it's name may imply, it's value will be "false" once the user launches the app once and goes through my small configuration wizard.

Like you can see in the plist, first_launch has TRUE/YES as it's initial value, yet this code:

```<code>NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"%d", [defaults boolForKey:@"first_launch"]);
</code>```

ALWAYS show me it's 0. What can I do to have "invisible" values in my settings bundle? I don't want the user to modify first_launch for obvious reasons, and I always want to add a password field that will only be accessible from the app itself.

答:
NSUserDefaults values are invisible to the Settings app unless they are added in a Settings.bundle.

You can store information in NSUserDefaults by using this code:

```<code>[[NSUserDefaults standardUserDefaults] setObject:someObject forKey:@"stringKey"];
</code>```

So, in your app delegate, try this:

```<code>if([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"] != NO){

  // Do first run stuff here...
  // Then, set the firstRun flag to NO
  [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
}
</code>```

EDIT:

Switches and Grouped data and the like must be initialized in code or set by the user for them to work.

Edit2:

You need to  manually change value of the @"firstLaunch" key.

```<code>if([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"] == NO){
  NSLog(@"The app has never been launched before.");
  // Notice the changed value here:
  [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
} else {
  NSLog(@"The app was launched before.");
}
</code>```

Body:
My last question was going this path before, but I think this warrants a new question because I want to to go for this path now (instead of attempting to do what I was trying to do in my other question).

What I want to do is to have "Invisible" values in NSUserDefaults. I don't care about Apple's guidelines because my app is aimed at Jailbroken phones. This is what I have so far:

The root.plist in settings.bundle looks like this (so far, because I want to add a password field later):

first_launch is a value that I don't want the user to modify directly. As it's name may imply, it's value will be "false" once the user launches the app once and goes through my small configuration wizard.

Like you can see in the plist, first_launch has TRUE/YES as it's initial value, yet this code:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"%d", [defaults boolForKey:@"first_launch"]);

ALWAYS show me it's 0. What can I do to have "invisible" values in my settings bundle? I don't want the user to modify first_launch for obvious reasons, and I always want to add a password field that will only be accessible from the app itself.

Body:
NSUserDefaults values are invisible to the Settings app unless they are added in a Settings.bundle.

You can store information in NSUserDefaults by using this code:

[[NSUserDefaults standardUserDefaults] setObject:someObject forKey:@"stringKey"];

So, in your app delegate, try this:

if([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"] != NO){

  // Do first run stuff here...
  // Then, set the firstRun flag to NO
  [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
}

EDIT:

Switches and Grouped data and the like must be initialized in code or set by the user for them to work.

Edit2:

You need to  manually change value of the @"firstLaunch" key.

if([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"] == NO){
  NSLog(@"The app has never been launched before.");
  // Notice the changed value here:
  [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
} else {
  NSLog(@"The app was launched before.");
}


问:
How to reliably disable the ZZ map in vim
I'm trying to disable the ZZ mapping in vim, as I found myself accidentally exiting from vim too much by it when trying to use >> (I use a spanish keyboard).

My attempt is quite obvious:

```<code>nnoremap ZZ <Nop>
</code>```

While it works for my use case, I found that doing Z<wait 1 second>Z still triggers the "save and quit" behavior.

As I understand Vim waits timeoutlen milliseconds and, if that time passes, the map doesn't fire and Vim uses the default ZZ behavior.

My question is, how can I reliably disable ZZ even when timeoutlen passes without changing the timeout config?

答:
While writing the question I began tinkering with an idea, and finally come to this:

```<code>nnoremap Z <Nop>
nnoremap ZZ <Nop>
</code>```

This works always, no matter how you press ZZ; if you press it and waits, the first map fires and clears the Z, if you don't wait the second fires.

I hope this is useful to someone else.

Body:
I'm trying to disable the ZZ mapping in vim, as I found myself accidentally exiting from vim too much by it when trying to use >> (I use a spanish keyboard).

My attempt is quite obvious:

nnoremap ZZ <Nop>

While it works for my use case, I found that doing Z<wait 1 second>Z still triggers the "save and quit" behavior.

As I understand Vim waits timeoutlen milliseconds and, if that time passes, the map doesn't fire and Vim uses the default ZZ behavior.

My question is, how can I reliably disable ZZ even when timeoutlen passes without changing the timeout config?

Body:
While writing the question I began tinkering with an idea, and finally come to this:

nnoremap Z <Nop>
nnoremap ZZ <Nop>

This works always, no matter how you press ZZ; if you press it and waits, the first map fires and clears the Z, if you don't wait the second fires.

I hope this is useful to someone else.


问:
Is is possible to transform JSON data in an Elasticseach Painless script, and perform further operations on it?
We have a large corpus of JSON-formatted documents to search through to find patterns and historical trends. Elasticsearch seems like the perfect fit for this problem. The first trick is that the documents are collections of tens of thousands of "nested" documents (with a header). The second trick is that these nested documents represent data with varying types.

In order to accommodate this, all the value fields have been "encoded" as an array of strings, so a single integer value has been stored in the JSON as "[\"1\"]", and a table of floats is flattened to "[\"123.45\",\"678.9\",...]" and so on. (We also have arrays of strings, which don't need converting.) While this is awkward, I would have thought this would be a good compromise, given the way everything else involved in Elasticsearch seems to work.

The particular problem here is that these stored data values might represent a bitfield, from which we may need to inspect the state of one bit. Since this field will have been stored as a single-element string array, like "[\"14657\"], we need to convert that to a single integer, and then bit-shift it multiple times to the desired bit (or apply a mask, if such a function is available).

With Elasticsearch, I see that I can embed "Painless" scripts, but examples vary, and I haven't been able to find one that shows how I can covert the arbitrary-length string-array data field to appropriate types, for further comparison. Here's my query script as it stands.

```<code>{
  "_source" : false,
  "from" : 0, "size" : 10,
  "query": {
    "nested": {
      "path": "Variables",
      "query": {
        "bool": {
          "must": {
            "match": {"Variables.Designation": "Big_Long_Variable_Name"}
          },
          "must_not": {
            "match": {"Variables.Data": "[0]"}
          },
          "filter": {
            "script": {
              "script": {
                "source":
                "
                  def vals = doc['Variables.Data'];
                  return vals[0] != params.setting;
                ",
                "params": {
                  "setting": 3
                }
              }
            }
          }
        }
      },
      "inner_hits": {
        "_source": "Variables.Data"
      }
    }
  }
}
</code>```

I need to somehow transform the vals variable to an array of ints, pick off the first value, do some bit operations, and make a comparison to return true or false. In this example, I'm hoping to be able to set "setting" equal to the bit position I want to check for on/off.

I've already been through the exercise with Elasticsearch in finding out that I needed to make my Variables.Data field a keyword so I could search on specific values in it. I realize that this is getting away from the intent of Elasticsearch, but I still think this might be the best solution, for other reasons. I created a new index, and reimported my test documents, and the index size went up about 30%. That's a compromise I'm willing to make, if I can get this to work.

What tools do I have in Painless to make this work? (Or, am I crazy to try to do this with this tool?)

答:
I would suggest that you encode your data in elasticsearch provided types wherever possible (and even when not) to make the most out of painless. For instance, for the bit strings, you can encode them as an array of 1 and 0's for easier operations with Painless.

Painless, in my opinion, is still primitive. It's hard to debug. It's hard to read. It's hard to maintain. And, it's a horrible idea to have large functions in Painless.

To answer your question, you'd basically need to parse the array string with painless and have it in one of the available datatypes in order to do the comparison that you desire. For example, for the list, you'd use something like the split function, and then manually case each item in the results as int, float, string, etc...

Use the execute API to test small bits before adding this to your scripted field:

```<code>POST /_scripts/painless/_execute
{
  "script": {
    "source": """
    ArrayList arr = []; //to start with
    // use arr.add(INDEX, VALUE) to add after parsing
    """,
    "params": {
      "foo": 100.0,
      "bar": 1000.0
    }
  }
}
</code>```

On the other hand, if you save your data in ElasticSearch provided datatypes (note that ElasticSearch supports saving lists inside documents), then this task would be far easier to do in Painless.

For example, instead of having my_doc.foo = "[\"123.45\",\"678.9\",...]" as a string to be parsed later, why not saving it as a native list of floats instead like my_doc.foo = [123.45, 678.9, ...]?

This way, you avoid the unnecessary Painless code required to parse the text document.

Body:
We have a large corpus of JSON-formatted documents to search through to find patterns and historical trends. Elasticsearch seems like the perfect fit for this problem. The first trick is that the documents are collections of tens of thousands of "nested" documents (with a header). The second trick is that these nested documents represent data with varying types.

In order to accommodate this, all the value fields have been "encoded" as an array of strings, so a single integer value has been stored in the JSON as "[\"1\"]", and a table of floats is flattened to "[\"123.45\",\"678.9\",...]" and so on. (We also have arrays of strings, which don't need converting.) While this is awkward, I would have thought this would be a good compromise, given the way everything else involved in Elasticsearch seems to work.

The particular problem here is that these stored data values might represent a bitfield, from which we may need to inspect the state of one bit. Since this field will have been stored as a single-element string array, like "[\"14657\"], we need to convert that to a single integer, and then bit-shift it multiple times to the desired bit (or apply a mask, if such a function is available).

With Elasticsearch, I see that I can embed "Painless" scripts, but examples vary, and I haven't been able to find one that shows how I can covert the arbitrary-length string-array data field to appropriate types, for further comparison. Here's my query script as it stands.

{
  "_source" : false,
  "from" : 0, "size" : 10,
  "query": {
    "nested": {
      "path": "Variables",
      "query": {
        "bool": {
          "must": {
            "match": {"Variables.Designation": "Big_Long_Variable_Name"}
          },
          "must_not": {
            "match": {"Variables.Data": "[0]"}
          },
          "filter": {
            "script": {
              "script": {
                "source":
                "
                  def vals = doc['Variables.Data'];
                  return vals[0] != params.setting;
                ",
                "params": {
                  "setting": 3
                }
              }
            }
          }
        }
      },
      "inner_hits": {
        "_source": "Variables.Data"
      }
    }
  }
}

I need to somehow transform the vals variable to an array of ints, pick off the first value, do some bit operations, and make a comparison to return true or false. In this example, I'm hoping to be able to set "setting" equal to the bit position I want to check for on/off.

I've already been through the exercise with Elasticsearch in finding out that I needed to make my Variables.Data field a keyword so I could search on specific values in it. I realize that this is getting away from the intent of Elasticsearch, but I still think this might be the best solution, for other reasons. I created a new index, and reimported my test documents, and the index size went up about 30%. That's a compromise I'm willing to make, if I can get this to work.

What tools do I have in Painless to make this work? (Or, am I crazy to try to do this with this tool?)

Body:
I would suggest that you encode your data in elasticsearch provided types wherever possible (and even when not) to make the most out of painless. For instance, for the bit strings, you can encode them as an array of 1 and 0's for easier operations with Painless.

Painless, in my opinion, is still primitive. It's hard to debug. It's hard to read. It's hard to maintain. And, it's a horrible idea to have large functions in Painless.

To answer your question, you'd basically need to parse the array string with painless and have it in one of the available datatypes in order to do the comparison that you desire. For example, for the list, you'd use something like the split function, and then manually case each item in the results as int, float, string, etc...

Use the execute API to test small bits before adding this to your scripted field:

POST /_scripts/painless/_execute
{
  "script": {
    "source": """
    ArrayList arr = []; //to start with
    // use arr.add(INDEX, VALUE) to add after parsing
    """,
    "params": {
      "foo": 100.0,
      "bar": 1000.0
    }
  }
}

On the other hand, if you save your data in ElasticSearch provided datatypes (note that ElasticSearch supports saving lists inside documents), then this task would be far easier to do in Painless.

For example, instead of having my_doc.foo = "[\"123.45\",\"678.9\",...]" as a string to be parsed later, why not saving it as a native list of floats instead like my_doc.foo = [123.45, 678.9, ...]?

This way, you avoid the unnecessary Painless code required to parse the text document.


问:
Not found constructor AndroidDriver with Appium in eclipse
Code with errors:

```<code>package TestCase;

import java.net.MalformedURLException;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.gargoylesoftware.htmlunit.javascript.host.URL;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;

public class TestWebBrowser {
    //AppiumDriver driver = new IOSDriver();
    public static AndroidDriver driver;

    public static void main(String[] args) throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }
}
</code>```

The message error is:

The constructor URL(string) is undefined

The constructor AndroidDriver(URL, DesiredCapabilities) is undefined

AndroidDriver is a raw type

I have tried with different versions of java-client and still the problem persists

答:
@Lorena, hi.

1. Firstly, could You please double check the imports ? Sharing code snippet below with correct ones

```<code>package tests.web;

import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileBrowserType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
public class AndroidWebTest {
   private static final String ACCESS_KEY = System.getenv(“SEETEST_IO_ACCESS_KEY”);
   private static final String CLOUD_URL = “https://cloud.seetest.io:443/wd/hub”;
   private static final String TITLE = “Testing Website on Android Chrome with Java”;
   private AndroidDriver driver = null;

   @Before
   public void setUp() throws MalformedURLException {
       DesiredCapabilities dc = new DesiredCapabilities();
       dc.setCapability(“testName”, TITLE);
       dc.setCapability(“accessKey”, ACCESS_KEY);
       dc.setBrowserName(MobileBrowserType.CHROME);
       driver = new AndroidDriver(new URL(CLOUD_URL), dc);
   }

   @Test
   public void testAppiumOnChrome() {
       driver.get(“https://amazon.com”);
       System.out.println(driver.getTitle());
       if (driver.getCapabilities().getCapability(“device.category”).equals(“TABLET”)) {
           driver.findElement(By.xpath(“//*[@name=’field-keywords’]”)).sendKeys(“iPhone”);
           driver.findElement(By.xpath(“//*[@text=’Go’]”)).click();
       } else {
           driver.findElement(By.xpath(“//*[@name=’k’]”)).sendKeys(“iPhone”);
           driver.findElement(By.xpath(“//*[@value=’Go’]”)).click();
       }
   }

   @After
   public void tearDown() {
       if (driver != null) {
           driver.quit();
       }
   }
}
</code>```

Please see the Comparing and combining web and mobile test automation drivers article for more details.

If You project is maven-based, could You please also  double check the dependencies?
For example, please see latest appium updates here

Appropriate maven repo to check for (latest) java client:
https://mvnrepository.com/artifact/io.appium/java-client

Body:
Code with errors:

package TestCase;

import java.net.MalformedURLException;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.gargoylesoftware.htmlunit.javascript.host.URL;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;

public class TestWebBrowser {
    //AppiumDriver driver = new IOSDriver();
    public static AndroidDriver driver;

    public static void main(String[] args) throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }
}

The message error is:

The constructor URL(string) is undefined

The constructor AndroidDriver(URL, DesiredCapabilities) is undefined

AndroidDriver is a raw type

I have tried with different versions of java-client and still the problem persists

Body:
@Lorena, hi.

1. Firstly, could You please double check the imports ? Sharing code snippet below with correct ones

package tests.web;

import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileBrowserType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
public class AndroidWebTest {
   private static final String ACCESS_KEY = System.getenv(“SEETEST_IO_ACCESS_KEY”);
   private static final String CLOUD_URL = “https://cloud.seetest.io:443/wd/hub”;
   private static final String TITLE = “Testing Website on Android Chrome with Java”;
   private AndroidDriver driver = null;

   @Before
   public void setUp() throws MalformedURLException {
       DesiredCapabilities dc = new DesiredCapabilities();
       dc.setCapability(“testName”, TITLE);
       dc.setCapability(“accessKey”, ACCESS_KEY);
       dc.setBrowserName(MobileBrowserType.CHROME);
       driver = new AndroidDriver(new URL(CLOUD_URL), dc);
   }

   @Test
   public void testAppiumOnChrome() {
       driver.get(“https://amazon.com”);
       System.out.println(driver.getTitle());
       if (driver.getCapabilities().getCapability(“device.category”).equals(“TABLET”)) {
           driver.findElement(By.xpath(“//*[@name=’field-keywords’]”)).sendKeys(“iPhone”);
           driver.findElement(By.xpath(“//*[@text=’Go’]”)).click();
       } else {
           driver.findElement(By.xpath(“//*[@name=’k’]”)).sendKeys(“iPhone”);
           driver.findElement(By.xpath(“//*[@value=’Go’]”)).click();
       }
   }

   @After
   public void tearDown() {
       if (driver != null) {
           driver.quit();
       }
   }
}

Please see the Comparing and combining web and mobile test automation drivers article for more details.

If You project is maven-based, could You please also  double check the dependencies?
For example, please see latest appium updates here

Appropriate maven repo to check for (latest) java client:
https://mvnrepository.com/artifact/io.appium/java-client


问:
UnrecognizedImageError - image insertion error - python-docx
I am trying to insert an wmf file to docx using python-docx which is producing the following traceback.

```<code>Traceback (most recent call last):
  File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 79, in <module>
    read_ppt(path, file)
  File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 73, in read_ppt
    write_docx(ppt_data, False)
  File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 31, in write_docx
    document.add_picture(slide_data.get('picture_location'), width=Inches(5.0))
  File "C:\Python34\lib\site-packages\docx\document.py", line 72, in add_picture
    return run.add_picture(image_path_or_stream, width, height)
  File "C:\Python34\lib\site-packages\docx\text\run.py", line 62, in add_picture
    inline = self.part.new_pic_inline(image_path_or_stream, width, height)
  File "C:\Python34\lib\site-packages\docx\parts\story.py", line 56, in new_pic_inline
    rId, image = self.get_or_add_image(image_descriptor)
  File "C:\Python34\lib\site-packages\docx\parts\story.py", line 29, in get_or_add_image
    image_part = self._package.get_or_add_image_part(image_descriptor)
  File "C:\Python34\lib\site-packages\docx\package.py", line 31, in get_or_add_image_part
    return self.image_parts.get_or_add_image_part(image_descriptor)
  File "C:\Python34\lib\site-packages\docx\package.py", line 74, in get_or_add_image_part
    image = Image.from_file(image_descriptor)
  File "C:\Python34\lib\site-packages\docx\image\image.py", line 55, in from_file
    return cls._from_stream(stream, blob, filename)
  File "C:\Python34\lib\site-packages\docx\image\image.py", line 176, in _from_stream
    image_header = _ImageHeaderFactory(stream)
  File "C:\Python34\lib\site-packages\docx\image\image.py", line 199, in _ImageHeaderFactory
    raise UnrecognizedImageError
docx.image.exceptions.UnrecognizedImageError
</code>```

The image file is in .wmf format.

Any help or suggestion appreciated.

答:
python-docx identifies the type of an image-file by "recognizing" its distinctive header. In this way it can distinguish JPEG from PNG, from TIFF, etc. This is much more reliable than mapping a filename extension and much more convenient than requiring the user to tell you the type. It's a pretty common approach.

This error indicates python-docx is not finding a header it recognizes. Windows Metafile format (WMF) can be tricky this way, there's a lot of leeway in the proprietary spec and variation in file specimens in the field.

To fix this, I recommend you read the file with something that does recognize it (I would start with Pillow) and have it "convert" it into the same or another format, hopefully correcting the header in the process.

First I would try just reading it and saving it as WMF (or perhaps EMF if that's an option). This might be enough to do the trick. If you have to change to an intermediate format and then back, that could be lossy, but maybe better than nothing.

ImageMagick might be another good choice to try because it probably has better coverage than Pillow does.

Body:
I am trying to insert an wmf file to docx using python-docx which is producing the following traceback.

Traceback (most recent call last):
  File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 79, in <module>
    read_ppt(path, file)
  File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 73, in read_ppt
    write_docx(ppt_data, False)
  File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 31, in write_docx
    document.add_picture(slide_data.get('picture_location'), width=Inches(5.0))
  File "C:\Python34\lib\site-packages\docx\document.py", line 72, in add_picture
    return run.add_picture(image_path_or_stream, width, height)
  File "C:\Python34\lib\site-packages\docx\text\run.py", line 62, in add_picture
    inline = self.part.new_pic_inline(image_path_or_stream, width, height)
  File "C:\Python34\lib\site-packages\docx\parts\story.py", line 56, in new_pic_inline
    rId, image = self.get_or_add_image(image_descriptor)
  File "C:\Python34\lib\site-packages\docx\parts\story.py", line 29, in get_or_add_image
    image_part = self._package.get_or_add_image_part(image_descriptor)
  File "C:\Python34\lib\site-packages\docx\package.py", line 31, in get_or_add_image_part
    return self.image_parts.get_or_add_image_part(image_descriptor)
  File "C:\Python34\lib\site-packages\docx\package.py", line 74, in get_or_add_image_part
    image = Image.from_file(image_descriptor)
  File "C:\Python34\lib\site-packages\docx\image\image.py", line 55, in from_file
    return cls._from_stream(stream, blob, filename)
  File "C:\Python34\lib\site-packages\docx\image\image.py", line 176, in _from_stream
    image_header = _ImageHeaderFactory(stream)
  File "C:\Python34\lib\site-packages\docx\image\image.py", line 199, in _ImageHeaderFactory
    raise UnrecognizedImageError
docx.image.exceptions.UnrecognizedImageError

The image file is in .wmf format.

Any help or suggestion appreciated.


问:
Index page of Wordpress is not SSL secured
Runing vHost nginx on ubuntu. After I added a wordpress SEO friendly urls my main page (index) became non secured. However all other pages are

Index non secured.

All other pages are secured.

Can someone point me at the right direction? How do i get index page secured?

```<code>server {

    listen 80;

    listen [::]:80;

    listen 443 ssl;

    listen [::]:443 ssl;

    ssl on;
    ssl_certificate /etc/nginx/ssl-certs/MYSITE.com.crt;
    ssl_trusted_certificate /etc/nginx/ssl-certs/MYSITE.com.crt;
    ssl_certificate_key /etc/nginx/ssl-certs/MYSITE.com.key;
    ssl_prefer_server_ciphers on;

        server_name MYSITE.com www.MYSITE.com;

        root /var/www/MYSITE.com/public_html;
        index index.php index.html;
        autoindex on;
        include /etc/nginx/macros/macro;
        include /etc/nginx/snippets/phpmyadmin.conf;

                ####Please add below lines for SEO Friendly URLs
                location / {
                index index.php;
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        location ~ \.php$ {
 try_files $uri =404;
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 fastcgi_index index.php;
 include fastcgi_params;
 }
}
</code>```

After I added a wordpress SEO friendly urls my main page (index) became non secured. However all other pages are

Tried to clean cookies, tried on different PC, same thing everywhere.

答:
Do you have a serverside valid ssl? (do you have ssl running on your host?)
then use really simple ssl to fix all the content to go through https.

you can also use cloudflare and use HTTPS rewrite feature.

Body:
Runing vHost nginx on ubuntu. After I added a wordpress SEO friendly urls my main page (index) became non secured. However all other pages are

Index non secured.

All other pages are secured.

Can someone point me at the right direction? How do i get index page secured?

server {

    listen 80;

    listen [::]:80;

    listen 443 ssl;

    listen [::]:443 ssl;

    ssl on;
    ssl_certificate /etc/nginx/ssl-certs/MYSITE.com.crt;
    ssl_trusted_certificate /etc/nginx/ssl-certs/MYSITE.com.crt;
    ssl_certificate_key /etc/nginx/ssl-certs/MYSITE.com.key;
    ssl_prefer_server_ciphers on;

        server_name MYSITE.com www.MYSITE.com;

        root /var/www/MYSITE.com/public_html;
        index index.php index.html;
        autoindex on;
        include /etc/nginx/macros/macro;
        include /etc/nginx/snippets/phpmyadmin.conf;

                ####Please add below lines for SEO Friendly URLs
                location / {
                index index.php;
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        location ~ \.php$ {
 try_files $uri =404;
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 fastcgi_index index.php;
 include fastcgi_params;
 }
}

After I added a wordpress SEO friendly urls my main page (index) became non secured. However all other pages are

Tried to clean cookies, tried on different PC, same thing everywhere.


问:
Why doesn't it match
I'm confused about the macro pattern of scheme

```<code>(define-syntax test

  (syntax-rules ()

    ((_ (head ... (x y) . tail))

     (syntax-error

      'tail

      "tail is"))

    ((_ any ...)

     (syntax-error "fallback"))))
</code>```

why (test (1 7 (2 4) 34)) dosen't match the first rule

答:
The syntax rules pattern matcher is greedy and does not backtrack on the same pattern. head ... matches (1 7 (2 4)), but 34 does not match (x y) and therefor it fails. If you expect only one element after you can do this:

```<code>(define-syntax test
  (syntax-rules ()
    ((_ (head ... (x y) last-element))
     (syntax-error
      'last-element
      "last element is"))
    ((_ any ...)
     (syntax-error "fallback"))))
</code>```

If you are trying to get zero or more elements after the last list of two then you need to massage it a bit is you still want to use syntax-rules:

```<code>(define-syntax test
  (syntax-rules (build)
    ((_ build m post (x y) . tail)
     (test build #t () . tail))
    ((_ build m (post ...) any . tail)
     (test build m (post ... any) . tail))
    ((_ build #t post)
     (syntax-error
      'post
      "tail is"))
    ((_ (head ...))
     (test build #f () head ...))
    ((_ any ...)
     (syntax-error "fallback"))))
</code>```

A symbol in the patytern matches anything. This is why the pattern for (x y) needs to go first since any does match (2 4) as well. eg. for (test (1 7 (2 4) 34 (3 4 5))) the post will be (34 (3 4 5)) since neither of those elements matches (x y) but both matches any.

Note that syntax-error is not part of Scheme.

Title: Why doesn't it match

Body:
I'm confused about the macro pattern of scheme

(define-syntax test

  (syntax-rules ()

    ((_ (head ... (x y) . tail))

     (syntax-error

      'tail

      "tail is"))

    ((_ any ...)

     (syntax-error "fallback"))))

why (test (1 7 (2 4) 34)) dosen't match the first rule

Body:
The syntax rules pattern matcher is greedy and does not backtrack on the same pattern. head ... matches (1 7 (2 4)), but 34 does not match (x y) and therefor it fails. If you expect only one element after you can do this:

(define-syntax test
  (syntax-rules ()
    ((_ (head ... (x y) last-element))
     (syntax-error
      'last-element
      "last element is"))
    ((_ any ...)
     (syntax-error "fallback"))))

If you are trying to get zero or more elements after the last list of two then you need to massage it a bit is you still want to use syntax-rules:

(define-syntax test
  (syntax-rules (build)
    ((_ build m post (x y) . tail)
     (test build #t () . tail))
    ((_ build m (post ...) any . tail)
     (test build m (post ... any) . tail))
    ((_ build #t post)
     (syntax-error
      'post
      "tail is"))
    ((_ (head ...))
     (test build #f () head ...))
    ((_ any ...)
     (syntax-error "fallback"))))

A symbol in the patytern matches anything. This is why the pattern for (x y) needs to go first since any does match (2 4) as well. eg. for (test (1 7 (2 4) 34 (3 4 5))) the post will be (34 (3 4 5)) since neither of those elements matches (x y) but both matches any.

Note that syntax-error is not part of Scheme.


答:
You can't do it with std::istream_iterator.

But it is relatively easy to write an input iterator.

```<code>#include <iterator>
#include <iostream>
#include <sstream>
#include <vector>
#include <cctype>

template<typename T>
class istream_line_iterator: public std::iterator<std::input_iterator_tag, T>
{
    std::istream*   stream;
    public:
        // Creating from a stream or the end iterator.
        istream_line_iterator(std::istream& s): stream(&s)      {dropLeadingSpace();}
        istream_line_iterator():                stream(nullptr) {}

        // Copy
        istream_line_iterator(istream_line_iterator const& copy): stream(copy.stream)   {}
        istream_line_iterator& operator=(istream_line_iterator const& copy) {stream = copy.stream;return *this;}

        // The only valid comparison is against the end() iterator.
        // All other iterator comparisons return false.
        bool operator==(istream_line_iterator const& rhs) const {return stream == nullptr && rhs.stream == nullptr;}
        bool operator!=(istream_line_iterator const& rhs) const {return !(*this == rhs);}

        // Geting the value modifies the stream and returns the value.
        // Note: Reading from the end() iterator is undefined behavior.
        T  operator*() const    {T value;(*stream) >> value;return value;}
        T* operator->() const;  // Not sure I want to implement this.

        // Input streams are funny.
        // Does not matter if you do a pre or post increment. The underlying stream has changed.
        // So the effect is the same.
        istream_line_iterator& operator++()     {dropLeadingSpace();return *this;}
        istream_line_iterator& operator++(int)  {dropLeadingSpace();return *this;}

    private:
        void dropLeadingSpace()
        {
            // Only called from constructor and ++ operator.
            // Note calling this on end iterator is undefined behavior.

            char c;
            while((*stream) >> std::noskipws >> c) {
                if (c == '\n') {
                    // End of line. So mark the iterator as reaching end.
                    stream = nullptr;
                    return;
                }
                if (!std::isspace(c)) {
                    // Found a non space character so put it back
                    stream->putback(c);
                    return;
                }
            }
            // End of stream. Mark the iterator as reaching the end.
            stream = nullptr;
        }
};

int main()
{
    std::stringstream    s{"0 1 2 3 4 5 6 7 8 9 10\n11 12 13 14 15 16\n17 18 19"};

    std::vector<int>    d{istream_line_iterator<int>(s), istream_line_iterator<int>()};
    for(auto v: d) {
        std::cout << "V: " << v << "\n";
    }
}
</code>```

Running:

```<code>> g++ -std=c++17 main.cpp
> ./a.out
V: 0
V: 1
V: 2
V: 3
V: 4
V: 5
V: 6
V: 7
V: 8
V: 9
V: 10
</code>```

Title: How to make std::istream_iterator read only until the end of line?

Body:
Having the following piece of code:

std::vector<int64> values;
std::copy(
  std::istream_iterator<int64>(std::cin),
  std::istream_iterator<int64>(),
  std::back_inserter(values)
);

I'd like to make it read the input stream only until the end of line. How can I do that with std::istream_iterator?

Body:
You can't do it with std::istream_iterator.

But it is relatively easy to write an input iterator.

#include <iterator>
#include <iostream>
#include <sstream>
#include <vector>
#include <cctype>

template<typename T>
class istream_line_iterator: public std::iterator<std::input_iterator_tag, T>
{
    std::istream*   stream;
    public:
        // Creating from a stream or the end iterator.
        istream_line_iterator(std::istream& s): stream(&s)      {dropLeadingSpace();}
        istream_line_iterator():                stream(nullptr) {}

        // Copy
        istream_line_iterator(istream_line_iterator const& copy): stream(copy.stream)   {}
        istream_line_iterator& operator=(istream_line_iterator const& copy) {stream = copy.stream;return *this;}

        // The only valid comparison is against the end() iterator.
        // All other iterator comparisons return false.
        bool operator==(istream_line_iterator const& rhs) const {return stream == nullptr && rhs.stream == nullptr;}
        bool operator!=(istream_line_iterator const& rhs) const {return !(*this == rhs);}

        // Geting the value modifies the stream and returns the value.
        // Note: Reading from the end() iterator is undefined behavior.
        T  operator*() const    {T value;(*stream) >> value;return value;}
        T* operator->() const;  // Not sure I want to implement this.

        // Input streams are funny.
        // Does not matter if you do a pre or post increment. The underlying stream has changed.
        // So the effect is the same.
        istream_line_iterator& operator++()     {dropLeadingSpace();return *this;}
        istream_line_iterator& operator++(int)  {dropLeadingSpace();return *this;}

    private:
        void dropLeadingSpace()
        {
            // Only called from constructor and ++ operator.
            // Note calling this on end iterator is undefined behavior.

            char c;
            while((*stream) >> std::noskipws >> c) {
                if (c == '\n') {
                    // End of line. So mark the iterator as reaching end.
                    stream = nullptr;
                    return;
                }
                if (!std::isspace(c)) {
                    // Found a non space character so put it back
                    stream->putback(c);
                    return;
                }
            }
            // End of stream. Mark the iterator as reaching the end.
            stream = nullptr;
        }
};

int main()
{
    std::stringstream    s{"0 1 2 3 4 5 6 7 8 9 10\n11 12 13 14 15 16\n17 18 19"};

    std::vector<int>    d{istream_line_iterator<int>(s), istream_line_iterator<int>()};
    for(auto v: d) {
        std::cout << "V: " << v << "\n";
    }
}

Running:

> g++ -std=c++17 main.cpp
> ./a.out
V: 0
V: 1
V: 2
V: 3
V: 4
V: 5
V: 6
V: 7
V: 8
V: 9
V: 10


问:
Passing Parameters in 64 bit Assembly Function from C language. Which Register Receive These Parameter?
I want to pass a parameter to an assembly function from C.

On a UNIX-like system, the first six parameters go into rdi, rsi, rdx, rcx, r8, and r9.

On Windows, the first four parameters go into rcx, rdx, r8, and r9.

Now, my question is: On the BIOS- or DOS programming level, which registers receive these parameters? If the number of parameter are more than 6, how do I write the assembly to handle these parameters?

答:
64-bit UEFI uses the Windows convention.

The BIOS and DOS APIs are defined in assembly language.

Traditionally in 16-bit and 32-bit x86 all the arguments are stored on the stack.


问:
How to update messages from a chat whose sent on date is earlier than the start of the chat?
I have two tables: 1. chats (containing the start_date of a chat), 2. messages (containing the sent_on date of the chat messages).

chats:

```<code>id, start_date
1, 2015-06-01
2, 2014-06-01
3, 2013-06-01

</code>```

messages:

```<code>id, message, sent_on, chat_id
1,  messag1,  2015-05-01, 1
2,  messag2,  2015-04-01, 1
3,  messag3,  2015-03-01, 1
4,  messag4,  2015-02-01, 1

5,  messag5,  2014-06-01, 2
6,  messag6,  2014-06-01, 2
7,  messag7,  2014-06-01, 2
8,  messag8,  2014-06-01, 2

9,  messag9,  2013-04-01, 3
10, messag10, 2013-03-01, 3
11, messag11, 2013-02-01, 3
12, messag12, 2013-01-01, 3

</code>```

The problem is that the records from messages with chat_id = 1 and 3 are with earlier sent_on dates than chats.id equaling 1 and chats.id equaling 3.

How can we update the chats table setting start_date of chats.id 1 equaling the earliest date from messages with chat_id 1 (in this case 2015-02-01 ) and chats.id 3 with the the earliest date from messages with chat_id 3 ( equaling in this case 2013-01-01)?

答:
We can try doing an update join to a subquery which finds the earliest date for each chat in the messages table:

```<code>UPDATE chats c
INNER JOIN
(
    SELECT chat_id, MIN(sent_on) AS min_sent_on
    FROM messages
    GROUP BY chat_id
) m
    ON c.id = m.chat_id
SET
    c.start_date = m.min_sent_on;
</code>```

Body:
I have two tables: 1. chats (containing the start_date of a chat), 2. messages (containing the sent_on date of the chat messages).

chats:

id, start_date
1, 2015-06-01
2, 2014-06-01
3, 2013-06-01

messages:

id, message, sent_on, chat_id
1,  messag1,  2015-05-01, 1
2,  messag2,  2015-04-01, 1
3,  messag3,  2015-03-01, 1
4,  messag4,  2015-02-01, 1

5,  messag5,  2014-06-01, 2
6,  messag6,  2014-06-01, 2
7,  messag7,  2014-06-01, 2
8,  messag8,  2014-06-01, 2

9,  messag9,  2013-04-01, 3
10, messag10, 2013-03-01, 3
11, messag11, 2013-02-01, 3
12, messag12, 2013-01-01, 3

The problem is that the records from messages with chat_id = 1 and 3 are with earlier sent_on dates than chats.id equaling 1 and chats.id equaling 3.

How can we update the chats table setting start_date of chats.id 1 equaling the earliest date from messages with chat_id 1 (in this case 2015-02-01 ) and chats.id 3 with the the earliest date from messages with chat_id 3 ( equaling in this case 2013-01-01)?

Body:
We can try doing an update join to a subquery which finds the earliest date for each chat in the messages table:

UPDATE chats c
INNER JOIN
(
    SELECT chat_id, MIN(sent_on) AS min_sent_on
    FROM messages
    GROUP BY chat_id
) m
    ON c.id = m.chat_id
SET
    c.start_date = m.min_sent_on;


问:
How to do IO Write or file delete in J2ME
I am making an app for a library

1) The library wants users to install app such that user does not have to access internet after installing app to get list of available books....

2) so what they want is that upon first installation the user will have a set of records distributed along with the jar & if he intends to update the list then he can access the internet

so as per this description i thought of following

1) i will put the list of records inside a text file & store it in /res & distribute the jar

2) upon installing & running the app first time....the data from the text file will be read & store using RMS

3) further any updation can be directly store inside RMS

Now my Q is after running the app first time my text file becomes redundant.....so i want to know

1) how can i delete this text file inside /res programmatically

2) else how can i delete all the contents inside text file....ie how do i do IO write in a text file

答:
You cannot delete a resource file from your application.

You cannot write to a resource file either.

If you feel the file size is too big, then either compress it or download it separately upon first launch of the application.


问:
How to continue loop every time an exception is encountered and break upon completion?
I'm trying to receive a promise and update the DOM with it, but there is 3 in 10 chance that the promise throws an exception. What I'm trying to do is to keep trying every time an exception is thrown and break when no exception is encountered.

I tried making a while loop that should continue upon an exception and break upon success, but it repeats anyway. I can't figure out how to make a while loop work in an asynchronous environment.

      for (var i = 1; i < 10; i++) {
        while (true) {
          try {
        this.props.someServer.allMessages(this.state.someId,this.state.input);
        this.setState({input: ''});
          }
          catch (Exception) {
            console.log ("error, exception encountered, trying again.");
            continue;
          }
          break;
          console.log ("success!");
        }
      }
```

I expect it to try every time an exception is encountered, and break out of the loop if no exception is thrown (with a maximum of 10 tries to avoid an infinite loop).

答:
You should use await before the asynchronous function to wait for it to finish before you can execute the next statements.

```<code>await this.props.allMessages(..)
</code>```

And make sure to wrap the while loop in an async function.

Body:
I'm trying to receive a promise and update the DOM with it, but there is 3 in 10 chance that the promise throws an exception. What I'm trying to do is to keep trying every time an exception is thrown and break when no exception is encountered.

I tried making a while loop that should continue upon an exception and break upon success, but it repeats anyway. I can't figure out how to make a while loop work in an asynchronous environment.

      for (var i = 1; i < 10; i++) {
        while (true) {
          try {
        this.props.someServer.allMessages(this.state.someId,this.state.input);
        this.setState({input: ''});
          }
          catch (Exception) {
            console.log ("error, exception encountered, trying again.");
            continue;
          }
          break;
          console.log ("success!");
        }
      }

I expect it to try every time an exception is encountered, and break out of the loop if no exception is thrown (with a maximum of 10 tries to avoid an infinite loop).

Body:
You should use await before the asynchronous function to wait for it to finish before you can execute the next statements.

await this.props.allMessages(..)

And make sure to wrap the while loop in an async function.


问:
Shell script not running via crontab, but runs fine manually
I have a script that checks if the PPTP VPN is running, and if not it reconnects the PPTP VPN. When I run the script manually it executes fine, but when I make a cron job, it's not running.

```<code>* * * * * /bin/bash /var/scripts/vpn-check.sh
</code>```

Here is the script:

#!/bin/sh
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$$'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi
```

答:
In my case, the issue was that the script wasn't marked as executable. To make sure it is, run the following command:

chmod +x your_script.sh

Body:
I have a script that checks if the PPTP VPN is running, and if not it reconnects the PPTP VPN. When I run the script manually it executes fine, but when I make a cron job, it's not running.

* * * * * /bin/bash /var/scripts/vpn-check.sh

Here is the script:

#!/bin/sh
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$$'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi