问:
Reading in a JSON File Using Swift
I'm really struggling with trying to read a JSON file into Swift so I can play around with it. I've spent the best part of 2 days re-searching and trying different methods but no luck as of yet so I have signed up to StackOverFlow to see if anyone can point me in the right direction.....

My JSON file is called test.json and contains the following:

```<code>{
  "person":[
     {
       "name": "Bob",
       "age": "16",
       "employed": "No"
     },
     {
       "name": "Vinny",
       "age": "56",
       "employed": "Yes"
     }
  ]
}
</code>```

The file is stored in the documents directly and I access it using the following code:

```<code>let file = "test.json"
let dirs : String[] = NSSearchPathForDirectoriesInDomains(
                                                          NSSearchpathDirectory.DocumentDirectory,
                                                          NSSearchPathDomainMask.AllDomainMask,
                                                          true) as String[]

if (dirs != nil) {
    let directories: String[] = dirs
    let dir = directories[0]
    let path = dir.stringByAppendingPathComponent(file)
}

var jsonData = NSData(contentsOfFile:path, options: nil, error: nil)
println("jsonData \(jsonData)" // This prints what looks to be JSON encoded data.

var jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: nil) as? NSDictionary

println("jsonDict \(jsonDict)") - This prints nil.....
</code>```

If anyone can just give me a push in the right direction on how I can de-serialize the JSON file and put it in an accessible Swift object I will be eternally grateful!

Kind Regards,

Krivvenz.

答:
The following code works for me. I am using Swift 5

```<code>let path = Bundle.main.path(forResource: "yourJSONfileName", ofType: "json")
var jsonData = try! String(contentsOfFile: path!).data(using: .utf8)!
</code>```

Then, if your Person Struct (or Class) is Decodable (and also all of its properties), you can simply do:

```<code>let person = try! JSONDecoder().decode(Person.self, from: jsonData)
</code>```

I avoided all the error handling code to make the code more legible.

Title: Reading in a JSON File Using Swift

Body:
I'm really struggling with trying to read a JSON file into Swift so I can play around with it. I've spent the best part of 2 days re-searching and trying different methods but no luck as of yet so I have signed up to StackOverFlow to see if anyone can point me in the right direction.....

My JSON file is called test.json and contains the following:

{
  "person":[
     {
       "name": "Bob",
       "age": "16",
       "employed": "No"
     },
     {
       "name": "Vinny",
       "age": "56",
       "employed": "Yes"
     }
  ]
}

The file is stored in the documents directly and I access it using the following code:

let file = "test.json"
let dirs : String[] = NSSearchPathForDirectoriesInDomains(
                                                          NSSearchpathDirectory.DocumentDirectory,
                                                          NSSearchPathDomainMask.AllDomainMask,
                                                          true) as String[]

if (dirs != nil) {
    let directories: String[] = dirs
    let dir = directories[0]
    let path = dir.stringByAppendingPathComponent(file)
}

var jsonData = NSData(contentsOfFile:path, options: nil, error: nil)
println("jsonData \(jsonData)" // This prints what looks to be JSON encoded data.

var jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: nil) as? NSDictionary

println("jsonDict \(jsonDict)") - This prints nil.....

If anyone can just give me a push in the right direction on how I can de-serialize the JSON file and put it in an accessible Swift object I will be eternally grateful!

Kind Regards,

Krivvenz.

Body:
The following code works for me. I am using Swift 5

let path = Bundle.main.path(forResource: "yourJSONfileName", ofType: "json")
var jsonData = try! String(contentsOfFile: path!).data(using: .utf8)!

Then, if your Person Struct (or Class) is Decodable (and also all of its properties), you can simply do:

let person = try! JSONDecoder().decode(Person.self, from: jsonData)

I avoided all the error handling code to make the code more legible.


来源: stackexchange

问:
SqlDependency tries to do something with log4net and fails to start
We are using SqlDependency in a .NET web app. Intermittently in production, we receive the following error when trying to call Start:

```<code>NullReferenceException - Object reference not set to an instance of an object.
Server stack trace:
   at System.Object.GetType()
   at log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(SerializationInfo info, StreamingContext context)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
   at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeMessageParts(ArrayList argsToSerialize)
   at System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage..ctor(IMethodCallMessage mcm)
   at System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage.SmuggleIfPossible(IMessage msg)
   at System.Runtime.Remoting.Channels.CrossAppDomainSink.SyncProcessMessage(IMessage reqMsg)
Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at SqlDependencyProcessDispatcher.StartWithDefault(String connectionString, String& server, DbConnectionPoolIdentity& identity, String& user, String& database, String& service, String appDomainKey, SqlDependencyPerAppDomainDispatcher dispatcher, Boolean& errorOccurred, Boolean& appDomainStart)
   at System.Data.SqlClient.SqlDependency.Start(String connectionString, String queue, Boolean useDefaults)
</code>```

The code for starting the SqlDependency is simply:

```<code>SqlDependency.Start(ConnectionStringHelper.GetDatabaseConnectionString(), null);
</code>```

The error first appeared after upgrading log4net from v1 to v2. We also received a similar error relating to SqlDependency not being able to find the log4net DLL, which we corrected by manually copying the DLL into C:\Windows\system32\inetsrv\

```<code>SerializationException - Unable to find assembly 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'.
Server stack trace:
   at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.DeserializeObject(MemoryStream stm)
   at System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage.FixupForNewAppDomain()
   at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoDispatch(Byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, SmuggledMethodReturnMessage& smuggledMrm)
   at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoTransitionDispatchCallback(Object[] args)
Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at System._AppDomain.CreateInstance(String assemblyName, String typeName)
   at System.Data.SqlClient.SqlDependency.CreateProcessDispatcher(_AppDomain masterDomain)
   at System.Data.SqlClient.SqlDependency.ObtainProcessDispatcher()
   at System.Data.SqlClient.SqlDependency.Start(String connectionString, String queue, Boolean useDefaults)
</code>```

Why on earth would SqlDependency have anything to do with log4net?

答:
I think there a still unresolved Type(s) that causes the NullReferenceException - Object reference not set to an instance of an object..

Last two statements in stacktrace says:

```<code>Server stack trace:
   at System.Object.GetType()
   at log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(SerializationInfo info, StreamingContext context)
</code>```

My guess is that log4net is also using an external assembly reference, so try resolve it (log4net) with Fuslogvw.exe - https://learn.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer

If another assembly pops up as referenced, copy that to C:\Windows\system32\inetsrv\ also.

ContentLicense: CC BY-SA 4.0

Body:
We are using SqlDependency in a .NET web app. Intermittently in production, we receive the following error when trying to call Start:

NullReferenceException - Object reference not set to an instance of an object.
Server stack trace:
   at System.Object.GetType()
   at log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(SerializationInfo info, StreamingContext context)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
   at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeMessageParts(ArrayList argsToSerialize)
   at System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage..ctor(IMethodCallMessage mcm)
   at System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage.SmuggleIfPossible(IMessage msg)
   at System.Runtime.Remoting.Channels.CrossAppDomainSink.SyncProcessMessage(IMessage reqMsg)
Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at SqlDependencyProcessDispatcher.StartWithDefault(String connectionString, String& server, DbConnectionPoolIdentity& identity, String& user, String& database, String& service, String appDomainKey, SqlDependencyPerAppDomainDispatcher dispatcher, Boolean& errorOccurred, Boolean& appDomainStart)
   at System.Data.SqlClient.SqlDependency.Start(String connectionString, String queue, Boolean useDefaults)

The code for starting the SqlDependency is simply:

SqlDependency.Start(ConnectionStringHelper.GetDatabaseConnectionString(), null);

The error first appeared after upgrading log4net from v1 to v2. We also received a similar error relating to SqlDependency not being able to find the log4net DLL, which we corrected by manually copying the DLL into C:\Windows\system32\inetsrv\

SerializationException - Unable to find assembly 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'.
Server stack trace:
   at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.DeserializeObject(MemoryStream stm)
   at System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage.FixupForNewAppDomain()
   at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoDispatch(Byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, SmuggledMethodReturnMessage& smuggledMrm)
   at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoTransitionDispatchCallback(Object[] args)
Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at System._AppDomain.CreateInstance(String assemblyName, String typeName)
   at System.Data.SqlClient.SqlDependency.CreateProcessDispatcher(_AppDomain masterDomain)
   at System.Data.SqlClient.SqlDependency.ObtainProcessDispatcher()
   at System.Data.SqlClient.SqlDependency.Start(String connectionString, String queue, Boolean useDefaults)

Why on earth would SqlDependency have anything to do with log4net?

Body:
I think there a still unresolved Type(s) that causes the NullReferenceException - Object reference not set to an instance of an object..

Last two statements in stacktrace says:

Server stack trace:
   at System.Object.GetType()
   at log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(SerializationInfo info, StreamingContext context)

My guess is that log4net is also using an external assembly reference, so try resolve it (log4net) with Fuslogvw.exe - https://learn.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer

If another assembly pops up as referenced, copy that to C:\Windows\system32\inetsrv\ also.


问:
Detecting Malaria Cell in scan image
I use the malaria scan image to classify the image has malaria or not. The data set is downloaded from kaggle.

I achieve above 96% of accuracy.

Now, I wonder how to detect the cell in the scan image. I need to point out the malaria cell in the image or draw the outline for the malaria cell.

Sample Image which contains malaria cell

How can I achieve the detection in this problem?

答:
If I assume you want to locate the dark purple in the image, then this is one way to do it using Python/OpenCV/Numpy/Sklearn.

Read the input image without the alpha channel to remove the text guides

Do kmeans color segmentation using 3 colors (to return: black, light purple, dark purple). I use Sklearn as it is a bit simpler for me. But you can do it with OpenCV also.

Do color image thresholding on the dark purple color

(If desired add some morphology, though I did not use it here)

Get all contours and also largest contour (separately)

Save the resulting images

Input:

```<code>import cv2
import numpy as np
from sklearn import cluster

# read image
image = cv2.imread("purple_cell.png")
h, w, c = image.shape

# convert image to float in range 0-1 for sklearn kmeans
img = image.astype(np.float64)/255.0

# reshape image to 1D
image_1d = img.reshape(h*w, c)

# compute kmeans for 3 colors
kmeans_cluster = cluster.KMeans(n_clusters=3)
kmeans_cluster.fit(image_1d)
cluster_centers = kmeans_cluster.cluster_centers_
cluster_labels = kmeans_cluster.labels_

# need to scale back to range 0-255
newimage = (255*cluster_centers[cluster_labels].reshape(h, w, c)).clip(0,255).astype(np.uint8)

# Set BGR color ranges
lowerBound = np.array([170,90,120]);
upperBound = np.array([195,110,140]);

# Compute mask (roi) from ranges in dst
thresh = cv2.inRange(newimage, lowerBound, upperBound);

# get largest contour and all contours
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
area_thresh = 0
result1 = image.copy()
for c in contours:
    cv2.drawContours(result1, [c], -1, (0, 255, 0), 1)
    area = cv2.contourArea(c)
    if area > area_thresh:
        area_thresh=area
        big_contour = c

# draw largest contour only
result2 = image.copy()
cv2.drawContours(result2, [big_contour], -1, (0, 255, 0), 1)

cv2.imshow('image', image)
cv2.imshow('newimage', newimage)
cv2.imshow('thresh', thresh)
cv2.imshow('result1', result1)
cv2.imshow('result2', result2)
cv2.waitKey()

cv2.imwrite('purple_cell_kmeans_3.png', newimage)
cv2.imwrite('purple_cell_thresh.png', thresh)
cv2.imwrite('purple_cell_extracted1.png', result1)
cv2.imwrite('purple_cell_extracted2.png', result2)
</code>```

Kmeans Image:

Thresholded Image:

All Contours Image:

Largest Contour Image:

Title: Detecting Malaria Cell in scan image

Body:
If I assume you want to locate the dark purple in the image, then this is one way to do it using Python/OpenCV/Numpy/Sklearn.

Read the input image without the alpha channel to remove the text guides

Do kmeans color segmentation using 3 colors (to return: black, light purple, dark purple). I use Sklearn as it is a bit simpler for me. But you can do it with OpenCV also.

Do color image thresholding on the dark purple color

(If desired add some morphology, though I did not use it here)

Get all contours and also largest contour (separately)

Save the resulting images

Input:

import cv2
import numpy as np
from sklearn import cluster

# read image
image = cv2.imread("purple_cell.png")
h, w, c = image.shape

# convert image to float in range 0-1 for sklearn kmeans
img = image.astype(np.float64)/255.0

# reshape image to 1D
image_1d = img.reshape(h*w, c)

# compute kmeans for 3 colors
kmeans_cluster = cluster.KMeans(n_clusters=3)
kmeans_cluster.fit(image_1d)
cluster_centers = kmeans_cluster.cluster_centers_
cluster_labels = kmeans_cluster.labels_

# need to scale back to range 0-255
newimage = (255*cluster_centers[cluster_labels].reshape(h, w, c)).clip(0,255).astype(np.uint8)

# Set BGR color ranges
lowerBound = np.array([170,90,120]);
upperBound = np.array([195,110,140]);

# Compute mask (roi) from ranges in dst
thresh = cv2.inRange(newimage, lowerBound, upperBound);

# get largest contour and all contours
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
area_thresh = 0
result1 = image.copy()
for c in contours:
    cv2.drawContours(result1, [c], -1, (0, 255, 0), 1)
    area = cv2.contourArea(c)
    if area > area_thresh:
        area_thresh=area
        big_contour = c

# draw largest contour only
result2 = image.copy()
cv2.drawContours(result2, [big_contour], -1, (0, 255, 0), 1)

cv2.imshow('image', image)
cv2.imshow('newimage', newimage)
cv2.imshow('thresh', thresh)
cv2.imshow('result1', result1)
cv2.imshow('result2', result2)
cv2.waitKey()

cv2.imwrite('purple_cell_kmeans_3.png', newimage)
cv2.imwrite('purple_cell_thresh.png', thresh)
cv2.imwrite('purple_cell_extracted1.png', result1)
cv2.imwrite('purple_cell_extracted2.png', result2)

Kmeans Image:

Thresholded Image:

All Contours Image:

Largest Contour Image:


问:
Mocking a service in Jest
I am trying to follow a tutorial on Jest. I am trying to make a test that includes a mock of my REST service. According to the tutorial I need to add a folder with the name mocks (with two underscore before and after - SO removes those in this editor) in the same level as the src folder. In that folder I create a const with same name as my REST service:

```<code>const CrawlService = jest.fn(() => 42);
</code>```

Now in my test I have following:

```<code>import { CrawlService } from "../../../services/CrawlService";
import "regenerator-runtime/runtime";

describe("CardsButtonContainer", () => {
  it("dsfsdf", async () => {
    var jobsResult = await CrawlService.getJobs(0, 1, 1, 10, 0);
  });
});
</code>```

My expectation would be that Jest would replace my REST service with the mock service, have I misunderstood something here? I get following error:

Request failed with status code 404

It seems like it is my REST service that is called and not the mocked one.

I guess I somehow must add the function getJobs but I am not sure how to do that when it is from a class? Any pointers are welcome.

I am trying to follow this sample:

答: The example you show in the screen shot mocks the entire isomorphic-fetch module. I suggest you instead use a library such as fetch-mock or nock. Both of these provide an API that allows you to customize responses for testing purposes.

Title: Mocking a service in Jest

Body:
I am trying to follow a tutorial on Jest. I am trying to make a test that includes a mock of my REST service. According to the tutorial I need to add a folder with the name mocks (with two underscore before and after - SO removes those in this editor) in the same level as the src folder. In that folder I create a const with same name as my REST service:

const CrawlService = jest.fn(() => 42);

Now in my test I have following:

import { CrawlService } from "../../../services/CrawlService";
import "regenerator-runtime/runtime";

describe("CardsButtonContainer", () => {
  it("dsfsdf", async () => {
    var jobsResult = await CrawlService.getJobs(0, 1, 1, 10, 0);
  });
});

My expectation would be that Jest would replace my REST service with the mock service, have I misunderstood something here? I get following error:

Request failed with status code 404

It seems like it is my REST service that is called and not the mocked one.

I guess I somehow must add the function getJobs but I am not sure how to do that when it is from a class? Any pointers are welcome.

I am trying to follow this sample:


问:
JMX Admin Client for Thread Hung Notifications
I have created a Admin Client implementaion for Notifications for Hung Thread. I have sucessfully connect to server through Adminclient.

```<code>adminClient = AdminClientFactory.createAdminClient(connectProps);
</code>```

When I use below query to fetch the MBean for Node, I am able to get the Notifications when server is started or stopped.

```<code>String query = "WebSphere:type=NodeAgent,node=" + nodeName + ",*";
</code>```

But I am only looking for the Notifications related to Hung Thread, Can you please suggest what MBean i sould use for the same.

I have tried below to get the MBean.

```<code>String queryString = "WebSphere:type=ThreadPool,process="+nodeName+",name=WebContainer,*" ;
String queryString = "WebSphere:type=ThreadPool,process="+nodeName+",name=ThreadPool,*" ;
String queryString = "WebSphere:type=ThreadPool,process="+nodeName+",name=ORB.thread.pool,*" ;
</code>```

But I am not getting JMX notification though I can see in SysetemOut.log Hung thread error is present.

```<code> adminClient.addNotificationListener(nodeAgent, this, null, null);
</code>```

I have used above to bind the notification to the MBean.

答: The Server MBean produces hung thread events TYPE_THREAD_MONITOR_THREAD_HUNG and TYPE_THREAD_MONITOR_THREAD_CLEAR. Modify the MBean query to "WebSphere:type=Server,*", and then add a notification listener for each server for which the client will handle hung thread events. The client should now be enabled to handle notifications for hung thread events.

Body:
I have created a Admin Client implementaion for Notifications for Hung Thread. I have sucessfully connect to server through Adminclient.

adminClient = AdminClientFactory.createAdminClient(connectProps);

When I use below query to fetch the MBean for Node, I am able to get the Notifications when server is started or stopped.

String query = "WebSphere:type=NodeAgent,node=" + nodeName + ",*";

But I am only looking for the Notifications related to Hung Thread, Can you please suggest what MBean i sould use for the same.

I have tried below to get the MBean.

String queryString = "WebSphere:type=ThreadPool,process="+nodeName+",name=WebContainer,*" ;
String queryString = "WebSphere:type=ThreadPool,process="+nodeName+",name=ThreadPool,*" ;
String queryString = "WebSphere:type=ThreadPool,process="+nodeName+",name=ORB.thread.pool,*" ;

But I am not getting JMX notification though I can see in SysetemOut.log Hung thread error is present.

 adminClient.addNotificationListener(nodeAgent, this, null, null);

I have used above to bind the notification to the MBean.


问:
ValueError: time data 'Date' does not match format '%m/%d/%Y' (Python)
```<code>import glob
import pandas as pd
import datetime

my_dates = ['Date']
sorted(my_dates, key=lambda x :datetime.datetime.strptime(x,'%m/%d/%Y'))
</code>```

Getting the error in title. Not sure why. the date format in excel sheet is 1/2/2020 (m/d/yyyy)
Tried many methods off google search and getting same error. Checked excel sheet to see if any dates written wrong but nope.

Python lists the column as object/list

答: df = pd.DataFrame(ph, columns = ['Date']) ph['Date'] = pd.to_datetime(df['Date''])

Body:
import glob
import pandas as pd
import datetime

my_dates = ['Date']
sorted(my_dates, key=lambda x :datetime.datetime.strptime(x,'%m/%d/%Y'))

Getting the error in title. Not sure why. the date format in excel sheet is 1/2/2020 (m/d/yyyy)
Tried many methods off google search and getting same error. Checked excel sheet to see if any dates written wrong but nope.

Python lists the column as object/list


问:
Website skipping when new element is loaded
I have a fixed header at my page which apears when user scroll down the page (170 pixels down when page width is less then 992 pixels and 320 pixels down when page is greater then 992 pixels width).

In the moment when header apears there is a "jump" / "skips" of the whole page on the height of this header so it's looks bad - content goes rapiddly down.
How to remove this jump and display smoothly fixed header?

This is a code :

```<code>
   function responsivecolumn(){

     if ($(document).width() <= 991)
     {
       $('.container #columns_inner #left-column').appendTo('.container #columns_inner');
       // ---------------- Fixed header responsive ----------------------
       $(window).bind('scroll', function () {
         if ($(window).scrollTop() > 170) {
           $('.header-top').addClass('fixed');
         } else {
           $('.header-top').removeClass('fixed');
         }
       });
     }
     else if($(document).width() >= 992)
     {
       $('.container #columns_inner #left-column').prependTo('.container #columns_inner');
       $(window).bind('scroll', function () {
         if ($(window).scrollTop() > 320) {
           $('.header-top').addClass('fixed');
         } else {
           $('.header-top').removeClass('fixed');
         }
       });
     }
   }
   $(document).ready(function(){responsivecolumn();});
   $(window).resize(function(){responsivecolumn();});

</code>```

```<code>#header .header-top.fixed {
   position: fixed;
   top: 0;
   width: 100%;
   margin: 0px;
   background: #FFF;
   padding: 3px 0px;
   z-index: 9999;
   left: 0px;
   border-bottom: 1px solid #ddd;
   -webkit-transition: all 0.3s ease;
   -moz-transition: all 0.3s ease;
   -o-transition: all 0.3s ease;
   transition: all 0.3s ease;
   -webkit-box-shadow: 0 0px 2px 1px rgba(0, 0, 0, 0.1);
   box-shadow: 0 0px 2px 1px rgba(0, 0, 0, 0.1);

</code>```

```<code>#header .header-top {
   padding-bottom: 0px;
   min-height: 48px;
}
</code>```

答: I believe the problem is that .header-top is in the flow of the page (meaning it takes up space) when the page is first loaded. However, as the page is scrolled and .fixed is added, .header-top is taken out of the flow. Then the rest of the page jumps up to take up the spot where .header-top used to be. I'd need to see the HTML to be sure though.

Body:
I have a fixed header at my page which apears when user scroll down the page (170 pixels down when page width is less then 992 pixels and 320 pixels down when page is greater then 992 pixels width).

In the moment when header apears there is a "jump" / "skips" of the whole page on the height of this header so it's looks bad - content goes rapiddly down.
How to remove this jump and display smoothly fixed header?

This is a code :

   function responsivecolumn(){

     if ($(document).width() <= 991)
     {
       $('.container #columns_inner #left-column').appendTo('.container #columns_inner');
       // ---------------- Fixed header responsive ----------------------
       $(window).bind('scroll', function () {
         if ($(window).scrollTop() > 170) {
           $('.header-top').addClass('fixed');
         } else {
           $('.header-top').removeClass('fixed');
         }
       });
     }
     else if($(document).width() >= 992)
     {
       $('.container #columns_inner #left-column').prependTo('.container #columns_inner');
       $(window).bind('scroll', function () {
         if ($(window).scrollTop() > 320) {
           $('.header-top').addClass('fixed');
         } else {
           $('.header-top').removeClass('fixed');
         }
       });
     }
   }
   $(document).ready(function(){responsivecolumn();});
   $(window).resize(function(){responsivecolumn();});

#header .header-top.fixed {
   position: fixed;
   top: 0;
   width: 100%;
   margin: 0px;
   background: #FFF;
   padding: 3px 0px;
   z-index: 9999;
   left: 0px;
   border-bottom: 1px solid #ddd;
   -webkit-transition: all 0.3s ease;
   -moz-transition: all 0.3s ease;
   -o-transition: all 0.3s ease;
   transition: all 0.3s ease;
   -webkit-box-shadow: 0 0px 2px 1px rgba(0, 0, 0, 0.1);
   box-shadow: 0 0px 2px 1px rgba(0, 0, 0, 0.1);

#header .header-top {
   padding-bottom: 0px;
   min-height: 48px;
}


问:
Bing maps Silverlight Zoom slider
Is there a way to change the style or position of the bing maps silverlight defualt zoom silder?</p>

答: yes you can hide the default slider and create your own attaching to the map events to get zoom / pan etc working with your control</p>

ContentLicense: CC BY-SA 2.5

Title: Bing maps Silverlight Zoom slider

ContentLicense: CC BY-SA 3.0


问:
How do I combine 2 DataFrames and add 2 column values together if both values exist or else return the non-null value instead if one is null?
Edit: I've realized that I did not ask my question in the right way. I'm not going to accept one answer over another, but am going to leave all content here for anyone's future use.

I have 2 DataFrames that I want to combine. I'd like to merge them only on the Name column so that all Name values from both DataFrames are included in the resulting DataFrame. I'm having trouble with null values, however. If an Inches value exists in both DataFrames, I'd like to add them together to be a result in df3. If an Inches value only exists in one of the two DataFrames, I'd like the non-null value chosen to be as the df3 Inches value.

ie. 'Silvers' in df1 + 'Silvers' in df2 = 14 for 'Silvers' in df3. Likewise: 'Merole' in df1 + 'Merole' in df2 = 6 for 'Merole' in df3. Or: 'Jones' in df1 + 'Jones' in df2 = 7 for 'Jones' in df3

I have df1:

```<code>Name    | Inches
---------------------
'Silvers'|   7
'Jones'  |   7
'Jackson'|   4
'Merole' |   NaN
'Kanoff' |   3
'Walker' |   1
'Smith'  |   8
</code>```

And df2:

```<code>Name    | Inches
---------------------
'Vickers' |   7
'Lederman'|   7
'Silvers' |   7
'Jones'   |   NaN
'Merole'  |   6
'Wilde'   |   2
'Nichols' |   5
</code>```

I want df3:

```<code>Name    | Inches
---------------------
'Vickers' |   7
'Lederman'|   7
'Silvers' |   14
'Jones'   |   7
'Merole'  |   6
'Wilde'   |   2
'Nichols' |   5
'Jackson' |   4
'Kanoff'  |   3
'Walker'  |   1
'Smith'   |   8
</code>```

Additionally, how would this be possible if there was a 3rd column with all null values that I want to keep as null?

答:
You can merge both the dataframes and then add the columns.

Code:

df1=pd.DataFrame({'name':['Silvers','Jackson','Jones'],'inches':[7,4,7]})
df2=pd.DataFrame({'name':['Silvers','Merole','Jones'],'inches':[7,4,np.nan]})

df3=pd.merge(df1,df2,on='name',how='outer')
df3.fillna(0,inplace=True)
df3['inches']=df3['inches_x'] + df3['inches_y']
df3.drop(columns=['inches_x','inches_y'],inplace=True)
```

<p>df1:</p>

<pre class="lang-py prettyprint-override"><code>    name    inches
0   Silvers 7
1   Jackson 4
2   Jones   7
</code>```

df2:

    name    inches
0   Silvers 7.0
1   Merole  4.0
2   Jones   NaN
```

<p>df3:</p>

<pre class="lang-py prettyprint-override"><code>    name    inches
0   Silvers 14.0
1   Jackson 4.0
2   Jones   7.0
3   Merole  4.0
</code>```

EDIT

Editing based on OP comments --
I have created 2 new dataframes showcasing all the possible cases.

New Code:

import pandas as pd
import numpy as np

df1=pd.DataFrame({'name':['Silvers','Jackson','Jones','Gary','test'],'inches':[7,4,7,0,np.nan]})
df2=pd.DataFrame({'name':['Silvers','Merole','Jones','Gary','test'],'inches':[7,4,np.nan,np.nan,np.nan]})

df3=pd.merge(df1,df2,on='name',how='outer')
df4=df3.copy()             #Keeping a copy of df3 as we will modify it soon.
df3.fillna(0,inplace=True)
df3['inches']=df3['inches_x'] + df3['inches_y']
df3.drop(columns=['inches_x','inches_y'],inplace=True)

for i in range(0,len(df3['inches'])):
    if df3['inches'][i] == 0:
        if (df4['inches_x'][i] == 0) or (df4['inches_y'][i] == 0):
            continue
        else:
            df3['inches'].loc[i] = np.nan
```

<p><strong>Output:</strong></p>

<p>df1:</p>

<pre class="lang-py prettyprint-override"><code>    name    inches
0   Silvers 7.0
1   Jackson 4.0
2   Jones   7.0
3   Gary    0.0
4   test    NaN
</code>```

df2:

    name    inches
0   Silvers 7.0
1   Merole  4.0
2   Jones   NaN
3   Gary    NaN
4   test    NaN
```

<p>df3 (<strong>final output</strong>):</p>

<pre class="lang-py prettyprint-override"><code>    name    inches
0   Silvers 14.0
1   Jackson 4.0
2   Jones   7.0
3   Gary    0.0
4   test    NaN
5   Merole  4.0
</code>```

df4 (which is an earlier copy of df3, which we use to check which should be 0 and which should be NaN):

    name    inches_x    inches_y
0   Silvers 7.0         7.0
1   Jackson 4.0         NaN
2   Jones   7.0         NaN
3   Gary    0.0         NaN
4   test    NaN         NaN
5   Merole  NaN         4.0
```

Body:
Edit: I've realized that I did not ask my question in the right way. I'm not going to accept one answer over another, but am going to leave all content here for anyone's future use.

I have 2 DataFrames that I want to combine. I'd like to merge them only on the Name column so that all Name values from both DataFrames are included in the resulting DataFrame. I'm having trouble with null values, however. If an Inches value exists in both DataFrames, I'd like to add them together to be a result in df3. If an Inches value only exists in one of the two DataFrames, I'd like the non-null value chosen to be as the df3 Inches value.

ie. 'Silvers' in df1 + 'Silvers' in df2 = 14 for 'Silvers' in df3. Likewise: 'Merole' in df1 + 'Merole' in df2 = 6 for 'Merole' in df3. Or: 'Jones' in df1 + 'Jones' in df2 = 7 for 'Jones' in df3

I have df1:

Name    | Inches
---------------------
'Silvers'|   7
'Jones'  |   7
'Jackson'|   4
'Merole' |   NaN
'Kanoff' |   3
'Walker' |   1
'Smith'  |   8

And df2:

Name    | Inches
---------------------
'Vickers' |   7
'Lederman'|   7
'Silvers' |   7
'Jones'   |   NaN
'Merole'  |   6
'Wilde'   |   2
'Nichols' |   5

I want df3:

Name    | Inches
---------------------
'Vickers' |   7
'Lederman'|   7
'Silvers' |   14
'Jones'   |   7
'Merole'  |   6
'Wilde'   |   2
'Nichols' |   5
'Jackson' |   4
'Kanoff'  |   3
'Walker'  |   1
'Smith'   |   8

Additionally, how would this be possible if there was a 3rd column with all null values that I want to keep as null?

Body:
You can merge both the dataframes and then add the columns.

Code:

df1=pd.DataFrame({'name':['Silvers','Jackson','Jones'],'inches':[7,4,7]})
df2=pd.DataFrame({'name':['Silvers','Merole','Jones'],'inches':[7,4,np.nan]})

df3=pd.merge(df1,df2,on='name',how='outer')
df3.fillna(0,inplace=True)
df3['inches']=df3['inches_x'] + df3['inches_y']
df3.drop(columns=['inches_x','inches_y'],inplace=True)

df1:

    name    inches
0   Silvers 7
1   Jackson 4
2   Jones   7

df2:

    name    inches
0   Silvers 7.0
1   Merole  4.0
2   Jones   NaN

df3:

    name    inches
0   Silvers 14.0
1   Jackson 4.0
2   Jones   7.0
3   Merole  4.0

EDIT

Editing based on OP comments --
I have created 2 new dataframes showcasing all the possible cases.

New Code:

import pandas as pd
import numpy as np

df1=pd.DataFrame({'name':['Silvers','Jackson','Jones','Gary','test'],'inches':[7,4,7,0,np.nan]})
df2=pd.DataFrame({'name':['Silvers','Merole','Jones','Gary','test'],'inches':[7,4,np.nan,np.nan,np.nan]})

df3=pd.merge(df1,df2,on='name',how='outer')
df4=df3.copy()             #Keeping a copy of df3 as we will modify it soon.
df3.fillna(0,inplace=True)
df3['inches']=df3['inches_x'] + df3['inches_y']
df3.drop(columns=['inches_x','inches_y'],inplace=True)

for i in range(0,len(df3['inches'])):
    if df3['inches'][i] == 0:
        if (df4['inches_x'][i] == 0) or (df4['inches_y'][i] == 0):
            continue
        else:
            df3['inches'].loc[i] = np.nan

Output:

df1:

    name    inches
0   Silvers 7.0
1   Jackson 4.0
2   Jones   7.0
3   Gary    0.0
4   test    NaN

df2:

    name    inches
0   Silvers 7.0
1   Merole  4.0
2   Jones   NaN
3   Gary    NaN
4   test    NaN

df3 (final output):

    name    inches
0   Silvers 14.0
1   Jackson 4.0
2   Jones   7.0
3   Gary    0.0
4   test    NaN
5   Merole  4.0

df4 (which is an earlier copy of df3, which we use to check which should be 0 and which should be NaN):

    name    inches_x    inches_y
0   Silvers 7.0         7.0
1   Jackson 4.0         NaN
2   Jones   7.0         NaN
3   Gary    0.0         NaN
4   test    NaN         NaN
5   Merole  NaN         4.0


问:
Kafka consumer receive null value when sending customer object
So i want to implement application which reads data from json format files. And I have created customer object for the data in json. And I want to send these object through kafka topic. So far i have successfully send String message to producer to consumer. But when i try to send object, in the consumer side, when I do .value().toString(). I got null value. The following is the code I have used:

This is producer:

```<code>public class MyProducer {

    public static void main(String[] args) throws Exception {

        Properties properties = new Properties();
        properties.put("bootstrap.servers", "kafka.kafka-cluster-shared.non-prod-5-az-scus.prod.us.walmart.net:9092");
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "xxxxxxxxx.KafkaJsonSerializer");
        properties.put("acks", "1");
        properties.put("retries", "2");
        properties.put("batch.size", "16384");
        properties.put("linger.ms", "1");
        properties.put("buffer.memory", "33554432");

        KafkaProducer<String, pharmacyData> kafkaProducer = new KafkaProducer<String, pharmacyData>(
                properties);

        String topic = "insights";

        //try {
            Gson gson = new Gson();

            Reader reader = Files.newBufferedReader(Paths.get("......./part.json"));

            List<pharmacyData> pdata = new Gson().fromJson(reader, new TypeToken<List<pharmacyData>>() {}.getType());

            //pdata.forEach(System.out::println);

            reader.close();
        //} catch (Exception e) {
            //e.printStackTrace();
        //}

        for (pharmacyData data : pdata) {
            kafkaProducer.send(new ProducerRecord<String, pharmacyData>(topic, data), new Callback() {
                @Override
                public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                    if (e == null) {
                        System.out.println(recordMetadata.partition() + "--" + recordMetadata.serializedValueSize());
                    } else {
                        e.printStackTrace();
                    }
                }
            });
        }
        kafkaProducer.close();
    }
}
</code>```

This is the customer object class:

```<code>public class pharmacyData {
    private String load_date;
    private String store_nbr;
    private String state;
    private String pmp_flag;
    private String zero_flag;
    private String submit_ts;

    public pharmacyData(String load_date, String store_nbr, String state, String pmp_flag, String zero_flag, String submit_ts) {
        this.load_date = load_date;
        this.store_nbr = store_nbr;
        this.state = state;
        this.pmp_flag = pmp_flag;
        this.zero_flag = zero_flag;
        this.submit_ts = submit_ts;
    }

    public String getLoad_date() {
        return load_date;
    }

    public void setLoad_date(String load_date) {
        this.load_date = load_date;
    }

    public String getStore_nbr() {
        return store_nbr;
    }

    public void setStore_nbr(String store_nbr) {
        this.store_nbr = store_nbr;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getPmp_flag() {
        return pmp_flag;
    }

    public void setPmp_flag(String pmp_flag) {
        this.pmp_flag = pmp_flag;
    }

    public String getZero_flag() {
        return zero_flag;
    }

    public void setZero_flag(String zero_flag) {
        this.zero_flag = zero_flag;
    }

    public String getSubmit_ts() {
        return submit_ts;
    }

    public void setSubmit_ts(String submit_ts) {
        this.submit_ts = submit_ts;
    }

    @Override
    public String toString() {
        return "pharmacyData{" +
                "load_date='" + load_date + '\'' +
                ", store_nbr='" + store_nbr + '\'' +
                ", state='" + state + '\'' +
                ", pmp_flag='" + pmp_flag + '\'' +
                ", zero_flag='" + zero_flag + '\'' +
                ", submit_ts='" + submit_ts + '\'' +
                '}';
    }
}
</code>```

this is the customer Serializer:

```<code>public class KafkaJsonSerializer implements Serializer {

    private Logger logger = LogManager.getLogger(this.getClass());
    @Override
    public void configure(Map map, boolean b) {

    }

    @Override
    public byte[] serialize(String s, Object o) {
        byte[] retVal = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            retVal = objectMapper.writeValueAsBytes(o);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return retVal;
    }

    @Override
    public void close() {

    }
}
</code>```

This is the customer Deserializer:

```<code>public class KafkaJsonDeserializer implements Deserializer {

    @Override
    public void configure(Map map, boolean b) {

    }

    @Override
    public Object deserialize(String s, byte[] bytes) {
        ObjectMapper mapper = new ObjectMapper();
        pharmacyData pdata = null;
        try {
            pdata = mapper.readValue(bytes, pharmacyData.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pdata;
    }

    @Override
    public void close() {

    }
}
</code>```

This is consumer:

```<code>public class MyConsumer {
    public static void main(String[] args) {

        Properties properties = new Properties();
        properties.put("bootstrap.servers", "kafka.kafka-cluster-shared.non-prod-5-az-scus.prod.us.walmart.net:9092");
        properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("value.deserializer", "xxxxxxxx.KafkaJsonDeserializer");
        properties.put("group.id", "consumer-group-1");
        properties.put("enable.auto.commit", "true");
        properties.put("auto.commit.interval.ms", "1000");
        properties.put("auto.offset.reset", "earliest");
        properties.put("session.timeout.ms", "30000");

        KafkaConsumer<String, pharmacyData> consumer = new KafkaConsumer<>(properties);

        String topic = "insights";

        consumer.subscribe(Collections.singletonList(topic));
            while (true) {
                ConsumerRecords<String, pharmacyData> consumerRecords = consumer.poll(100);

                for (ConsumerRecord<String, pharmacyData> consumerRecord : consumerRecords) {
                    System.out.println(consumerRecord.key() + "--" + consumerRecord.toString());
                    //System.out.println(consumerRecord.offset() + "--" + consumerRecord.partition());
                }
            }
    }
}
</code>```

Can someone please help me with the issues? Thank you very much!

答:
Problem solved:

The solution of this issue just add a default constructor as below:

```<code>public pharmacyData() {

}
</code>```

See this page for more details.

Body:
So i want to implement application which reads data from json format files. And I have created customer object for the data in json. And I want to send these object through kafka topic. So far i have successfully send String message to producer to consumer. But when i try to send object, in the consumer side, when I do .value().toString(). I got null value. The following is the code I have used:

This is producer:

public class MyProducer {

    public static void main(String[] args) throws Exception {

        Properties properties = new Properties();
        properties.put("bootstrap.servers", "kafka.kafka-cluster-shared.non-prod-5-az-scus.prod.us.walmart.net:9092");
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "xxxxxxxxx.KafkaJsonSerializer");
        properties.put("acks", "1");
        properties.put("retries", "2");
        properties.put("batch.size", "16384");
        properties.put("linger.ms", "1");
        properties.put("buffer.memory", "33554432");

        KafkaProducer<String, pharmacyData> kafkaProducer = new KafkaProducer<String, pharmacyData>(
                properties);

        String topic = "insights";

        //try {
            Gson gson = new Gson();

            Reader reader = Files.newBufferedReader(Paths.get("......./part.json"));

            List<pharmacyData> pdata = new Gson().fromJson(reader, new TypeToken<List<pharmacyData>>() {}.getType());

            //pdata.forEach(System.out::println);

            reader.close();
        //} catch (Exception e) {
            //e.printStackTrace();
        //}

        for (pharmacyData data : pdata) {
            kafkaProducer.send(new ProducerRecord<String, pharmacyData>(topic, data), new Callback() {
                @Override
                public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                    if (e == null) {
                        System.out.println(recordMetadata.partition() + "--" + recordMetadata.serializedValueSize());
                    } else {
                        e.printStackTrace();
                    }
                }
            });
        }
        kafkaProducer.close();
    }
}

This is the customer object class:

public class pharmacyData {
    private String load_date;
    private String store_nbr;
    private String state;
    private String pmp_flag;
    private String zero_flag;
    private String submit_ts;

    public pharmacyData(String load_date, String store_nbr, String state, String pmp_flag, String zero_flag, String submit_ts) {
        this.load_date = load_date;
        this.store_nbr = store_nbr;
        this.state = state;
        this.pmp_flag = pmp_flag;
        this.zero_flag = zero_flag;
        this.submit_ts = submit_ts;
    }

    public String getLoad_date() {
        return load_date;
    }

    public void setLoad_date(String load_date) {
        this.load_date = load_date;
    }

    public String getStore_nbr() {
        return store_nbr;
    }

    public void setStore_nbr(String store_nbr) {
        this.store_nbr = store_nbr;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getPmp_flag() {
        return pmp_flag;
    }

    public void setPmp_flag(String pmp_flag) {
        this.pmp_flag = pmp_flag;
    }

    public String getZero_flag() {
        return zero_flag;
    }

    public void setZero_flag(String zero_flag) {
        this.zero_flag = zero_flag;
    }

    public String getSubmit_ts() {
        return submit_ts;
    }

    public void setSubmit_ts(String submit_ts) {
        this.submit_ts = submit_ts;
    }

    @Override
    public String toString() {
        return "pharmacyData{" +
                "load_date='" + load_date + '\'' +
                ", store_nbr='" + store_nbr + '\'' +
                ", state='" + state + '\'' +
                ", pmp_flag='" + pmp_flag + '\'' +
                ", zero_flag='" + zero_flag + '\'' +
                ", submit_ts='" + submit_ts + '\'' +
                '}';
    }
}

this is the customer Serializer:

public class KafkaJsonSerializer implements Serializer {

    private Logger logger = LogManager.getLogger(this.getClass());
    @Override
    public void configure(Map map, boolean b) {

    }

    @Override
    public byte[] serialize(String s, Object o) {
        byte[] retVal = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            retVal = objectMapper.writeValueAsBytes(o);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return retVal;
    }

    @Override
    public void close() {

    }
}

This is the customer Deserializer:

public class KafkaJsonDeserializer implements Deserializer {

    @Override
    public void configure(Map map, boolean b) {

    }

    @Override
    public Object deserialize(String s, byte[] bytes) {
        ObjectMapper mapper = new ObjectMapper();
        pharmacyData pdata = null;
        try {
            pdata = mapper.readValue(bytes, pharmacyData.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pdata;
    }

    @Override
    public void close() {

    }
}

This is consumer:

public class MyConsumer {
    public static void main(String[] args) {

        Properties properties = new Properties();
        properties.put("bootstrap.servers", "kafka.kafka-cluster-shared.non-prod-5-az-scus.prod.us.walmart.net:9092");
        properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("value.deserializer", "xxxxxxxx.KafkaJsonDeserializer");
        properties.put("group.id", "consumer-group-1");
        properties.put("enable.auto.commit", "true");
        properties.put("auto.commit.interval.ms", "1000");
        properties.put("auto.offset.reset", "earliest");
        properties.put("session.timeout.ms", "30000");

        KafkaConsumer<String, pharmacyData> consumer = new KafkaConsumer<>(properties);

        String topic = "insights";

        consumer.subscribe(Collections.singletonList(topic));
            while (true) {
                ConsumerRecords<String, pharmacyData> consumerRecords = consumer.poll(100);

                for (ConsumerRecord<String, pharmacyData> consumerRecord : consumerRecords) {
                    System.out.println(consumerRecord.key() + "--" + consumerRecord.toString());
                    //System.out.println(consumerRecord.offset() + "--" + consumerRecord.partition());
                }
            }
    }
}

Can someone please help me with the issues? Thank you very much!

Body:
Problem solved:

The solution of this issue just add a default constructor as below:

public pharmacyData() {

}

See this page for more details.


问:
Java functional approach to join identical elements into a single one
I would like to use Streams API to process a call log and calculate the total billing amount for the same phone number. Here's the code that achieves it with a hybrid approach but I would like to use fully functional approach:</p>

```<code>List<CallLog> callLogs = Arrays.stream(S.split("\n"))
                    .map(CallLog::new)
                    .sorted(Comparator.comparingInt(callLog -> callLog.phoneNumber))
                    .collect(Collectors.toList());

            for (int i = 0; i< callLogs.size() -1 ;i++) {
                if (callLogs.get(i).phoneNumber == callLogs.get(i+1).phoneNumber) {
                    callLogs.get(i).billing += callLogs.get(i+1).billing;
                    callLogs.remove(i+1);
                }
            }
</code>```

答:
You can save yourself the sorting -> collection to list -> iterating the list for values next to each other if you instead do the following

Create all CallLog objects.

Merge them by the phoneNumber field

combine the billing fields every time

Return the already merged items

This can be done using Collectors.toMap(Function, Function, BinaryOperator) where the third parameter is the merge function that defines how items with identical keys would be combined:

Collection<CallLog> callLogs = Arrays.stream(S.split("\n"))
  .map(CallLog::new)
  .collect(Collectors.toMap( //a collector that will produce a map
    CallLog::phoneNumber,    //using phoneNumber as the key to group
    x -> x,                  //the item itself as the value
    (a, b) -> {              //and a merge function that returns an object with combined billing
      a.billing += b.billing;
      return a;
    }))
  .values(); //just return the values from that map
```

In the end, you would have CallLog items with unique phoneNumber fields whose billing field is equal to the combination of all billings of the previously duplicate phoneNumbers.

Body:
I would like to use Streams API to process a call log and calculate the total billing amount for the same phone number. Here's the code that achieves it with a hybrid approach but I would like to use fully functional approach:

List<CallLog> callLogs = Arrays.stream(S.split("\n"))
                    .map(CallLog::new)
                    .sorted(Comparator.comparingInt(callLog -> callLog.phoneNumber))
                    .collect(Collectors.toList());

            for (int i = 0; i< callLogs.size() -1 ;i++) {
                if (callLogs.get(i).phoneNumber == callLogs.get(i+1).phoneNumber) {
                    callLogs.get(i).billing += callLogs.get(i+1).billing;
                    callLogs.remove(i+1);
                }
            }

Body:
You can save yourself the sorting -> collection to list -> iterating the list for values next to each other if you instead do the following

Create all CallLog objects.

Merge them by the phoneNumber field

combine the billing fields every time

Return the already merged items

This can be done using Collectors.toMap(Function, Function, BinaryOperator) where the third parameter is the merge function that defines how items with identical keys would be combined:

Collection<CallLog> callLogs = Arrays.stream(S.split("\n"))
  .map(CallLog::new)
  .collect(Collectors.toMap( //a collector that will produce a map
    CallLog::phoneNumber,    //using phoneNumber as the key to group
    x -> x,                  //the item itself as the value
    (a, b) -> {              //and a merge function that returns an object with combined billing
      a.billing += b.billing;
      return a;
    }))
  .values(); //just return the values from that map

In the end, you would have CallLog items with unique phoneNumber fields whose billing field is equal to the combination of all billings of the previously duplicate phoneNumbers.


问:
ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
I have written a spring batch application using Spring boot. When I am trying to run that application using command line and classpath on my local system it is running fine. However, when I tried to run it on linux server it is giving me following exception

```<code>Unable to start web server; nested exception is
org.springframework.context.ApplicationContextException:
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
</code>```

Below is the way I am running it:

```<code>java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log
</code>```

答:
My solution had to do with a bad dependency. I had:

```<code><dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>
</code>```

In my pom and I had to comment out the exclusion to get it working. It must look for this tomcat package for some reason.

Body:
I have written a spring batch application using Spring boot. When I am trying to run that application using command line and classpath on my local system it is running fine. However, when I tried to run it on linux server it is giving me following exception

Unable to start web server; nested exception is
org.springframework.context.ApplicationContextException:
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

Below is the way I am running it:

java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log

Body:
My solution had to do with a bad dependency. I had:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>

In my pom and I had to comment out the exclusion to get it working. It must look for this tomcat package for some reason.


答:
Consider using Index alignment with DataFrame.add. Setting fill_value=0 will make the sums work if values exist in only one DataFrame.

```<code>df1.set_index('Name').add(df2.set_index('Name'), fill_value=0)

          Inches
Name
Jackson      4.0
Jones        7.0
Kanoff       3.0
Lederman     7.0
Merole       6.0
Nichols      5.0
Silvers     14.0
Smith        8.0
Vickers      7.0
Walker       1.0
Wilde        2.0
</code>```

pd.concat + groupby

Create one long DataFrame then sum within each name:

```<code>pd.concat([df1, df2]).groupby('Name')['Inches'].sum()
</code>```

Title: How do I combine 2 DataFrames and add 2 column values together if both values exist or else return the non-null value instead if one is null?

Body:
Consider using Index alignment with DataFrame.add. Setting fill_value=0 will make the sums work if values exist in only one DataFrame.

df1.set_index('Name').add(df2.set_index('Name'), fill_value=0)

          Inches
Name
Jackson      4.0
Jones        7.0
Kanoff       3.0
Lederman     7.0
Merole       6.0
Nichols      5.0
Silvers     14.0
Smith        8.0
Vickers      7.0
Walker       1.0
Wilde        2.0

pd.concat + groupby

Create one long DataFrame then sum within each name:

pd.concat([df1, df2]).groupby('Name')['Inches'].sum()


问:
Invalid blob name length. The blob name must be between 1 and 1024 characters long
I have been working with an Azure blob storage, in which I have tons of documents (all .pdf format). When working with cognitive services (index, indexer and skillsets), running it with the storage it works fine after it appears the error in the question name. But when I check and the length is less than 1024 (I think so).
I would like to know if there's a way to find out how Cognitive services is acquiring those blobs so I know what is happening and try to change it.
Example: Tons of pdfs with any type of name (normal characters (a-z), numbers and "-") but after like 600 docs it appears the error described before.
Curiously, as I was checking which document was related with the error, the same name appeared (i.e. NAME SURNAME 1.pdf and so on for like 9 documents). Once deleted, the error appeared again with another document, completely different.

Sorry for the large description.

答:
As far as valid blob names go, see Naming and Referencing Containers, Blobs, and Metadata - Blob names:

A blob name must conforming to the following naming rules:

A blob name can contain any combination of characters.

A blob name must be at least one character long and cannot be more than 1,024 characters long, for blobs in Azure Storage.

The Azure Storage emulator supports blob names up to 256 characters long. For more information, see Use the Azure storage emulator for development and testing.

Blob names are case-sensitive.

Reserved URL characters must be properly escaped.

The number of path segments comprising the blob name cannot exceed 254. A path segment is the string between consecutive delimiter characters (e.g., the forward slash '/') that corresponds to the name of a virtual directory.


问:
Error doing db2 insert with PHP parameterized query
I'm trying to run a db2 parameterized query in PHP and upon the execution of the insert, I get the error:
Invalid parameter number., SQL state S1002 in SQLDescribeParameter

This is my script:

```<code>        $getItems = "
        SELECT
            ID,
            EXPIRATION_TIMESTAMP
        FROM table1
    ";

    $stmt = odbc_exec($DB2connDEV, $getItems);

    $prepInsert = odbc_prepare($DB2connPROD, "INSERT INTO table2 (originalID, expiration_timestamp) VALUES(?,?)");

    while($gettingDevItems = odbc_fetch_array($stmt)){

        $rows[] = $gettingDevItems;
    }

    foreach($rows as $row){

        $originalID = $row['ID'];
        $expiration_timestamp = $row['EXPIRATION_TIMESTAMP'];

        $getIdentity = "SELECT IDENTITY_VAL_LOCAL() AS LASTID FROM SYSIBM.SYSDUMMY1";

        $insertTable = odbc_execute($prepInsert, array($originalID, $expiration_timestamp));//error at this line
        $insertTable = odbc_exec($DB2connPROD, $getIdentity);
        $row = odbc_fetch_array($stmt);
        $ret = $row['LASTID'];
    }
</code>```

When I do a var_dump on the array of params, I get this:

```<code>array(2) {
  [0]=>string(1) "2"
  [1]=>string(26) "2019-10-03 00:00:00.000000"
}
</code>```

What am I doing wrong here? Even if I take one value out to only insert one or the other I still get it, so It's not specific to one column.

答:
Maybe odbc can't support reuse of prepared statement, or your driver, or other part of your code, or another thing.
Anyway, move the prepared statement inside your foreach loop to make sure you will rebuild it:</p>

```<code>foreach($rows as $row){
   $prepInsert = odbc_prepare($DB2connPROD, "INSERT INTO table2 (originalID, expiration_timestamp) VALUES(?,?)");
...
</code>```

Body:
I'm trying to run a db2 parameterized query in PHP and upon the execution of the insert, I get the error:
Invalid parameter number., SQL state S1002 in SQLDescribeParameter

This is my script:

        $getItems = "
        SELECT
            ID,
            EXPIRATION_TIMESTAMP
        FROM table1
    ";

    $stmt = odbc_exec($DB2connDEV, $getItems);

    $prepInsert = odbc_prepare($DB2connPROD, "INSERT INTO table2 (originalID, expiration_timestamp) VALUES(?,?)");

    while($gettingDevItems = odbc_fetch_array($stmt)){

        $rows[] = $gettingDevItems;
    }

    foreach($rows as $row){

        $originalID = $row['ID'];
        $expiration_timestamp = $row['EXPIRATION_TIMESTAMP'];

        $getIdentity = "SELECT IDENTITY_VAL_LOCAL() AS LASTID FROM SYSIBM.SYSDUMMY1";

        $insertTable = odbc_execute($prepInsert, array($originalID, $expiration_timestamp));//error at this line
        $insertTable = odbc_exec($DB2connPROD, $getIdentity);
        $row = odbc_fetch_array($stmt);
        $ret = $row['LASTID'];
    }

When I do a var_dump on the array of params, I get this:

array(2) {
  [0]=>string(1) "2"
  [1]=>string(26) "2019-10-03 00:00:00.000000"
}

What am I doing wrong here? Even if I take one value out to only insert one or the other I still get it, so It's not specific to one column.

Body:
Maybe odbc can't support reuse of prepared statement, or your driver, or other part of your code, or another thing.
Anyway, move the prepared statement inside your foreach loop to make sure you will rebuild it:

foreach($rows as $row){
   $prepInsert = odbc_prepare($DB2connPROD, "INSERT INTO table2 (originalID, expiration_timestamp) VALUES(?,?)");
...


问:
jQuery - 2 different functions for 2 different submit buttons in one form
I have had a search around at other suggestions for what i want to do, but none seems to have what i need.

Basically i have a form with 2 submit buttons, i want one to save the data to the database, which i have done and works perfect ... the second submit button ( preview ) i need to open in a new window, take all the form data and display it on a form while leaving the previous page as it is ... if that makes sense.

I figure i cant do this with PHP as to get the post data it needs to basically reload a page ... i thought there might be a way with jQuery.

Any help?

答:
It sounds like your preview button isn't a submit button, but should just be a regular button that you set a new click function for.

In that function you can open the new window with the fields from the form.

If you want the new window to do a round trip to the server, you could use an ajax call as John suggested or perhaps when the 2nd button is clicked set a flag in a hidden field and your server side script can check that flag to determine if it should save the data or generate the preview page.


问:
How to remove duplicates from an array of objects
I have an JSON of data, but I need to remove the duplicates.

The only constraint is If there are duplicated messages, we would like
them to be deduplicated if the uuid and content are the same.

Currently this is my attempt, but I'm stuck:

```<code>const result = test['messages'].reduce((accu,cur)=>{
   if(! accu[curr.uuid] && accu[curr.content]){
    return accu.push(cur)
   }
},[])
</code>```

Here is the test example:

```<code>   let test=  {
      "messages": [
        {
          "sentAt": "2012-11-13T17:29:37.003Z",
          "uuid": "435453",
          "content": "1",
          "senderUuid": "2"
        },
        {
          "sentAt": "2015-05-22T13:55:10.542Z",
          "uuid": "4354353",
          "content": "2",
          "senderUuid": "2"
        },
        {
          "sentAt": "2012-11-20T01:31:33.751Z",
          "uuid": "4354353",
          "content": "3",
          "senderUuid": "1"
        },
        {
          "sentAt": "2016-02-17T10:13:03.115Z",
          "uuid": "435453",
          "content": "4",
          "senderUuid": "2"
        },
        {
          "sentAt": "2015-05-22T13:55:10.542Z",
          "uuid": "4354353",
          "content": "2",
          "senderUuid": "1"
        },
        {
          "sentAt": "2018-07-05T10:19:07.713Z",
          "uuid": "4354353",
          "content": "6",
          "senderUuid": "2"
        },
        {
          "sentAt": "2016-11-09T03:24:54.612Z",
          "uuid": "4354353",
          "content": "7",
          "senderUuid": "1"
        },
        {
          "sentAt": "2013-06-21T16:39:08.630Z",
          "uuid": "43543353",
          "content": "8",
          "senderUuid": "1"
        },
        {
          "sentAt": "2013-08-24T01:55:38.167Z",
          "uuid": "43521314353",
          "content": "9",
          "senderUuid": "1"
        },
        {
          "sentAt": "2012-11-05T11:37:00.472Z",
          "uuid": "43532134353",
          "content": "10",
          "senderUuid": "2"
        },
        {
          "sentAt": "2017-09-26T17:01:10.949Z",
          "uuid": "43321315433",
          "content": "11",
          "senderUuid": "2"
        },
        {
          "sentAt": "2012-12-02T13:55:30.626Z",
          "uuid": "43543321353",
          "content": "12",
          "senderUuid": "1"
        },
        {
          "sentAt": "2015-05-03T08:54:02.530Z",
          "uuid": "43542233",
          "content": "13",
          "senderUuid": "2"
        },
        {
          "sentAt": "2013-08-24T01:55:38.167Z",
          "uuid": "43521314353",
          "content": "9",
          "senderUuid": "1"
        },
        {
          "sentAt": "2012-02-19T09:42:11.913Z",
          "uuid": "435353",
          "content": "15",
          "senderUuid": "2"
        },
        {
          "sentAt": "2018-07-06T20:31:01.649Z",
          "uuid": "4354543353",
          "content": "16",
          "senderUuid": "2"
        },
        {
          "sentAt": "2017-09-25T04:35:18.647Z",
          "uuid": "4354353",
          "content": "17",
          "senderUuid": "1"
        },
        {
          "sentAt": "2012-12-17T15:08:37.988Z",
          "uuid": "43545433353",
          "content": "18",
          "senderUuid": "1"
        },
        {
          "sentAt": "2016-02-03T05:20:52.506Z",
          "uuid": "435454www35353",
          "content": "19",
          "senderUuid": "2"
        },
        {
          "sentAt": "2018-04-07T06:30:38.178Z",
          "uuid": "43545435353",
          "content": "20",
          "senderUuid": "2"
        }
      ]
    }
</code>```

答:
You can use Set.

```<code>const unique = new Set(test['messages'].map(e => JSON.stringify(e)));
</code>```

About Set:

Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set's collection.

Body:
I have an JSON of data, but I need to remove the duplicates.

The only constraint is If there are duplicated messages, we would like
them to be deduplicated if the uuid and content are the same.

Currently this is my attempt, but I'm stuck:

const result = test['messages'].reduce((accu,cur)=>{
   if(! accu[curr.uuid] && accu[curr.content]){
    return accu.push(cur)
   }
},[])

Here is the test example:

   let test=  {
      "messages": [
        {
          "sentAt": "2012-11-13T17:29:37.003Z",
          "uuid": "435453",
          "content": "1",
          "senderUuid": "2"
        },
        {
          "sentAt": "2015-05-22T13:55:10.542Z",
          "uuid": "4354353",
          "content": "2",
          "senderUuid": "2"
        },
        {
          "sentAt": "2012-11-20T01:31:33.751Z",
          "uuid": "4354353",
          "content": "3",
          "senderUuid": "1"
        },
        {
          "sentAt": "2016-02-17T10:13:03.115Z",
          "uuid": "435453",
          "content": "4",
          "senderUuid": "2"
        },
        {
          "sentAt": "2015-05-22T13:55:10.542Z",
          "uuid": "4354353",
          "content": "2",
          "senderUuid": "1"
        },
        {
          "sentAt": "2018-07-05T10:19:07.713Z",
          "uuid": "4354353",
          "content": "6",
          "senderUuid": "2"
        },
        {
          "sentAt": "2016-11-09T03:24:54.612Z",
          "uuid": "4354353",
          "content": "7",
          "senderUuid": "1"
        },
        {
          "sentAt": "2013-06-21T16:39:08.630Z",
          "uuid": "43543353",
          "content": "8",
          "senderUuid": "1"
        },
        {
          "sentAt": "2013-08-24T01:55:38.167Z",
          "uuid": "43521314353",
          "content": "9",
          "senderUuid": "1"
        },
        {
          "sentAt": "2012-11-05T11:37:00.472Z",
          "uuid": "43532134353",
          "content": "10",
          "senderUuid": "2"
        },
        {
          "sentAt": "2017-09-26T17:01:10.949Z",
          "uuid": "43321315433",
          "content": "11",
          "senderUuid": "2"
        },
        {
          "sentAt": "2012-12-02T13:55:30.626Z",
          "uuid": "43543321353",
          "content": "12",
          "senderUuid": "1"
        },
        {
          "sentAt": "2015-05-03T08:54:02.530Z",
          "uuid": "43542233",
          "content": "13",
          "senderUuid": "2"
        },
        {
          "sentAt": "2013-08-24T01:55:38.167Z",
          "uuid": "43521314353",
          "content": "9",
          "senderUuid": "1"
        },
        {
          "sentAt": "2012-02-19T09:42:11.913Z",
          "uuid": "435353",
          "content": "15",
          "senderUuid": "2"
        },
        {
          "sentAt": "2018-07-06T20:31:01.649Z",
          "uuid": "4354543353",
          "content": "16",
          "senderUuid": "2"
        },
        {
          "sentAt": "2017-09-25T04:35:18.647Z",
          "uuid": "4354353",
          "content": "17",
          "senderUuid": "1"
        },
        {
          "sentAt": "2012-12-17T15:08:37.988Z",
          "uuid": "43545433353",
          "content": "18",
          "senderUuid": "1"
        },
        {
          "sentAt": "2016-02-03T05:20:52.506Z",
          "uuid": "435454www35353",
          "content": "19",
          "senderUuid": "2"
        },
        {
          "sentAt": "2018-04-07T06:30:38.178Z",
          "uuid": "43545435353",
          "content": "20",
          "senderUuid": "2"
        }
      ]
    }

Body:
You can use Set.

const unique = new Set(test['messages'].map(e => JSON.stringify(e)));

About Set:

Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set's collection.


答:
You can use Collectors.groupingBy to group CallLog object by phoneNumber with Collectors.summingInt to sum the billing of grouped elements

```<code>Map<Integer, Integer> likesPerType = Arrays.stream(S.split("\n"))
                                           .map(CallLog::new)
                 .collect(Collectors.groupingBy(CallLog::getPhoneNumber, Collectors.summingInt(CallLog::getBilling)));
</code>```

Title: Java functional approach to join identical elements into a single one

Body:
You can use Collectors.groupingBy to group CallLog object by phoneNumber with Collectors.summingInt to sum the billing of grouped elements

Map<Integer, Integer> likesPerType = Arrays.stream(S.split("\n"))
                                           .map(CallLog::new)
                 .collect(Collectors.groupingBy(CallLog::getPhoneNumber, Collectors.summingInt(CallLog::getBilling)));


问:
In Ansible loop, test existence of files from registered results
I have several files that I need to backup in different directories.  I have tried the code below and not working for me.</p>

```<code>  vars:
    file_vars:
      - {name: /file1}
      - {name: /etc/file2}
      - {name: /etc/file/file3}

  tasks:

  - name: "Checking if config files exists"
    stat:
      path: "{{ item.name }}"
    with_items: "{{ file_vars }}"
    register: stat_result

  - name: Backup Files
    copy: src={{ item.name }} dest={{ item.name }}{{ ansible_date_time.date }}.bak
    with_items: "{{ file_vars }}"
    remote_src: yes
    when: stat_result.stat.exists == True
</code>```

答:
The problem is the condition

```<code>when: stat_result.stat.exists == True
</code>```

There is no attribute stat_result.stat. Instead, the attribute stat_result.results is a list of the results from the loop. It's possible to create a dictionary of files and their statuses. For example

```<code>   - set_fact:
        files_stats: "{{ dict(my_files|zip(my_stats)) }}"
      vars:
        my_files: "{{ stat_result.results|json_query('[].item.name') }}"
        my_stats: "{{ stat_result.results|json_query('[].stat.exists') }}"
</code>```

Then simply use this dictionary in the condition

```<code>when: files_stats[item.name]
</code>```

Below is a shorter version which creates the dictionary more efficiently

```<code>   - set_fact:
       files_stats: "{{ dict(stat_result.results|
                        json_query('[].[item.name, stat.exists]')) }}"
</code>```

Body:
I have several files that I need to backup in different directories.  I have tried the code below and not working for me.

  vars:
    file_vars:
      - {name: /file1}
      - {name: /etc/file2}
      - {name: /etc/file/file3}

  tasks:

  - name: "Checking if config files exists"
    stat:
      path: "{{ item.name }}"
    with_items: "{{ file_vars }}"
    register: stat_result

  - name: Backup Files
    copy: src={{ item.name }} dest={{ item.name }}{{ ansible_date_time.date }}.bak
    with_items: "{{ file_vars }}"
    remote_src: yes
    when: stat_result.stat.exists == True

Body:
The problem is the condition

when: stat_result.stat.exists == True

There is no attribute stat_result.stat. Instead, the attribute stat_result.results is a list of the results from the loop. It's possible to create a dictionary of files and their statuses. For example

   - set_fact:
        files_stats: "{{ dict(my_files|zip(my_stats)) }}"
      vars:
        my_files: "{{ stat_result.results|json_query('[].item.name') }}"
        my_stats: "{{ stat_result.results|json_query('[].stat.exists') }}"

Then simply use this dictionary in the condition

when: files_stats[item.name]

Below is a shorter version which creates the dictionary more efficiently

   - set_fact:
       files_stats: "{{ dict(stat_result.results|
                        json_query('[].[item.name, stat.exists]')) }}"


问:
How do I get Elasticsearch to highlight a partial word from a search_as_you_type field?
I'm having trouble setting up a search_as_you_type field with highlighting following the guide here https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-as-you-type.html

I'll leave a series of commands to reproduce what I'm seeing. Hopefully somebody can weigh in on what I'm missing :)

create mapping

```<code>PUT /test_index
{
  "mappings": {
    "properties": {
      "plain_text": {
        "type": "search_as_you_type",
        "index_options": "offsets",
        "term_vector": "with_positions_offsets"
      }
    }
  }
}
</code>```

insert document

```<code>POST /test_index/_doc
{
  "plain_text": "This is some random text"
}
</code>```

search for document

```<code>GET /snippets_test/_search
{
  "query": {
    "multi_match": {
      "query": "rand",
      "type": "bool_prefix",
      "fields": [
        "plain_text",
        "plain_text._2gram",
        "plain_text._3gram",
        "plain_text._index_prefix"
      ]
    }
  },
  "highlight" : {
    "fields" : [
      {
        "plain_text": {
          "number_of_fragments": 1,
          "no_match_size": 100
        }
      }
    ]
  }
}
</code>```

response

```<code>{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "rLZkjm8BDC17cLikXRbY",
        "_score" : 1.0,
        "_source" : {
          "plain_text" : "This is some random text"
        },
        "highlight" : {
          "plain_text" : [
            "This is some random text"
          ]
        }
      }
    ]
  }
}
</code>```

The response I get back does not have the highlighting I expect
Idealy the highlight is: This is some random text

答:
In order to achieve highlighting of n-grams (chars) you'll need:

a custom ngram tokenizer. By default the maximum difference between min_gram and max_gram is 1, so in my example highlighting will work only for the search terms with length 3 or 4. You can change this and creating more n-grams by setting a higher value for index.max_ngram_diff .

a custom analyzer based on the custom tokenizer

in mapping add "plain_text.highlight" field

Here's the configuration:

```<code>{
  "settings": {
    "analysis": {
      "analyzer": {
        "partial_words" : {
          "type": "custom",
          "tokenizer": "ngrams",
          "filter": ["lowercase"]
        }
      },
      "tokenizer": {
        "ngrams": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 4
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "plain_text": {
        "type": "text",
        "fields": {
          "shingles": {
            "type": "search_as_you_type"
          },
          "ngrams": {
            "type": "text",
            "analyzer": "partial_words",
            "search_analyzer": "standard",
            "term_vector": "with_positions_offsets"
          }
        }
      }
    }
  }
}
</code>```

the query:

```<code>{
  "query": {
    "multi_match": {
      "query": "rand",
      "type": "bool_prefix",
      "fields": [
        "plain_text.shingles",
        "plain_text.shingles._2gram",
        "plain_text.shingles._3gram",
        "plain_text.shingles._index_prefix",
        "plain_text.ngrams"
      ]
    }
  },
  "highlight" : {
    "fields" : [
      {
        "plain_text.ngrams": { }
      }
    ]
  }
}
</code>```

and the result:

```<code>    "hits": [
        {
            "_index": "test_index",
            "_type": "_doc",
            "_id": "FkHLVHABd_SGa-E-2FKI",
            "_score": 2,
            "_source": {
                "plain_text": "This is some random text"
            },
            "highlight": {
                "plain_text.ngrams": [
                    "This is some <em>rand</em>om text"
                ]
            }
        }
    ]
</code>```

Note: in some cases, this config might be expensive for memory usage and storage.

Body:
I'm having trouble setting up a search_as_you_type field with highlighting following the guide here https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-as-you-type.html

I'll leave a series of commands to reproduce what I'm seeing. Hopefully somebody can weigh in on what I'm missing :)

create mapping

PUT /test_index
{
  "mappings": {
    "properties": {
      "plain_text": {
        "type": "search_as_you_type",
        "index_options": "offsets",
        "term_vector": "with_positions_offsets"
      }
    }
  }
}

insert document

POST /test_index/_doc
{
  "plain_text": "This is some random text"
}

search for document

GET /snippets_test/_search
{
  "query": {
    "multi_match": {
      "query": "rand",
      "type": "bool_prefix",
      "fields": [
        "plain_text",
        "plain_text._2gram",
        "plain_text._3gram",
        "plain_text._index_prefix"
      ]
    }
  },
  "highlight" : {
    "fields" : [
      {
        "plain_text": {
          "number_of_fragments": 1,
          "no_match_size": 100
        }
      }
    ]
  }
}

response

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "rLZkjm8BDC17cLikXRbY",
        "_score" : 1.0,
        "_source" : {
          "plain_text" : "This is some random text"
        },
        "highlight" : {
          "plain_text" : [
            "This is some random text"
          ]
        }
      }
    ]
  }
}

The response I get back does not have the highlighting I expect
Idealy the highlight is: This is some random text

Body:
In order to achieve highlighting of n-grams (chars) you'll need:

a custom ngram tokenizer. By default the maximum difference between min_gram and max_gram is 1, so in my example highlighting will work only for the search terms with length 3 or 4. You can change this and creating more n-grams by setting a higher value for index.max_ngram_diff .

a custom analyzer based on the custom tokenizer

in mapping add "plain_text.highlight" field

Here's the configuration:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "partial_words" : {
          "type": "custom",
          "tokenizer": "ngrams",
          "filter": ["lowercase"]
        }
      },
      "tokenizer": {
        "ngrams": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 4
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "plain_text": {
        "type": "text",
        "fields": {
          "shingles": {
            "type": "search_as_you_type"
          },
          "ngrams": {
            "type": "text",
            "analyzer": "partial_words",
            "search_analyzer": "standard",
            "term_vector": "with_positions_offsets"
          }
        }
      }
    }
  }
}

the query:

{
  "query": {
    "multi_match": {
      "query": "rand",
      "type": "bool_prefix",
      "fields": [
        "plain_text.shingles",
        "plain_text.shingles._2gram",
        "plain_text.shingles._3gram",
        "plain_text.shingles._index_prefix",
        "plain_text.ngrams"
      ]
    }
  },
  "highlight" : {
    "fields" : [
      {
        "plain_text.ngrams": { }
      }
    ]
  }
}

and the result:

    "hits": [
        {
            "_index": "test_index",
            "_type": "_doc",
            "_id": "FkHLVHABd_SGa-E-2FKI",
            "_score": 2,
            "_source": {
                "plain_text": "This is some random text"
            },
            "highlight": {
                "plain_text.ngrams": [
                    "This is some <em>rand</em>om text"
                ]
            }
        }
    ]

Note: in some cases, this config might be expensive for memory usage and storage.


问:
Randomly spawning an enemy
I am in the progress of making a small 2d game where the objective is to eat as much poop as possible, but I'm having trouble spawning the poop at random times. I want the poop to spawn at the enemy's y location but then shoot forwards like an rpg.

import pygame
from pygame.locals import *
from numpy.random import rand

pygame.init()
pygame.display.set_caption('STINKY BEETLE')

screen_width = 800
screen_height = 600
game_running = True
pl_x = int(screen_width/10)
pl_y = int(screen_height/2)
pl_width = 80
pl_height = 40
pl_vel = 30
en_width = 80
en_height = 40
en_x = screen_width - screen_width/10 - en_width
en_y = int(screen_height/2)
en_yvel = -10
po_width = 50
po_height = 30
po_x = 720
po_y = en_y
po_xvel = 15

screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()

while game_running:
    clock.tick(10)

    po_delay = rand(1)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False

        if event.type == MOUSEBUTTONDOWN:
            if event.button == 4 and pl_y > pl_vel:
                pl_y -= pl_vel

            elif event.button == 5 and pl_y < screen_height - pl_width:
                pl_y += pl_vel

    if po_delay < 0.01:
        poop(po_x, po_y)

    en_y += en_yvel
    if en_y <= 0 or en_y >= screen_height - en_height:
        en_yvel =- en_yvel

    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (105, 255, 125), (pl_x, pl_y, pl_width, pl_height))
    pygame.display.update()

    pygame.draw.rect(screen, (255, 125, 115), (en_x, en_y, en_width, en_height))
    pygame.display.update()

pygame.quit()
```

答:
If you want to mange multiple "poops", then you have to create a list. And each "poop" is an object (an instance of a class).

Crate a class Poop, which can update the position and draw the poop:

```<code>class Poop:
    def __init__(self, x, y, w, h):
        self.rect = pygame.Rect(x, y-h//2, w, h)
        self.vel = -15
    def update(self):
        self.rect.x += self.vel
    def draw(self, surf):
        pygame.draw.rect(surf, (255, 200, 125), self.rect)

poops = []
</code>```

Use a timer event to spawn the poops. Use pygame.time.set_timer() to repeatedly create an USEREVENT. The time is set  in milliseconds. Set a random time by random.randint(a, b) e.g. set a time between 0.5 and 4 seconds (of course you can choose your own time interval):

min_time, max_time = 500, 4000 # 0.5 seconds to to 4 seconds
spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
```

<p>Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to start at <code>pygame.USEREVENT</code>. In this case <code>pygame.USEREVENT+1</code> is the event id for the timer event, which spawns the poops.</p>

<p>Create a new poop when the event occurs in the event loop and set a new random time:</p>

<pre class="lang-py prettyprint-override"><code>for event in pygame.event.get():
    # [...]
    if event.type == spawn_event:
        pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
        poops.append(Poop(en_x, en_y+en_yvel+en_height//2, 50, 30))
</code>```

Change the positions of the poops in a loop and remove them from the list, if they leave the window on the left:

for poop in poops[:]:
    poop.update()
    if poop.rect.right <= 0:
        poops.remove(poop)
```

<p>Draw them ina loop</p>

<pre class="lang-py prettyprint-override"><code>for poop in poops:
    poop.draw(screen)
</code>```

See the example:

import pygame
from pygame.locals import *
import random

pygame.init()
pygame.display.set_caption('STINKY BEETLE')

class Poop:
    def __init__(self, x, y, w, h):
        self.rect = pygame.Rect(x, y-h//2, w, h)
        self.vel = -15
    def update(self):
        self.rect.x += self.vel
    def draw(self, surf):
        pygame.draw.rect(surf, (255, 200, 125), self.rect)

screen_width = 800
screen_height = 600
game_running = True
pl_x, pl_y = screen_width//10, screen_height//2
pl_width, pl_height, pl_vel = 80, 40, 30
en_width, en_height, en_yvel = 80, 40, -10
en_x, en_y,  = screen_width - screen_width//10 - en_width, screen_height//2

screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()

min_time, max_time = 500, 4000 # 0.5 seconds up to 4 seconds
spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
poops = []

while game_running:
    clock.tick(10)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False

        if event.type == MOUSEBUTTONDOWN:
            if event.button == 4 and pl_y > pl_vel:
                pl_y -= pl_vel
            elif event.button == 5 and pl_y < screen_height - pl_width:
                pl_y += pl_vel

        if event.type == spawn_event:
            pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
            poops.append(Poop(en_x, en_y+en_yvel+en_height//2, 50, 30))

    en_y += en_yvel
    if en_y <= 0 or en_y >= screen_height - en_height:
        en_yvel =- en_yvel

    for poop in poops[:]:
        poop.update()
        if poop.rect.right <= 0:
            poops.remove(poop)

    screen.fill((0, 0, 0))
    for poop in poops:
        poop.draw(screen)
    pygame.draw.rect(screen, (105, 255, 125), (pl_x, pl_y, pl_width, pl_height))
    pygame.draw.rect(screen, (255, 125, 115), (en_x, en_y, en_width, en_height))
    pygame.display.update()

pygame.quit()
```

Title: Randomly spawning an enemy

Body:
If you want to mange multiple "poops", then you have to create a list. And each "poop" is an object (an instance of a class).

Crate a class Poop, which can update the position and draw the poop:

class Poop:
    def __init__(self, x, y, w, h):
        self.rect = pygame.Rect(x, y-h//2, w, h)
        self.vel = -15
    def update(self):
        self.rect.x += self.vel
    def draw(self, surf):
        pygame.draw.rect(surf, (255, 200, 125), self.rect)

poops = []

Use a timer event to spawn the poops. Use pygame.time.set_timer() to repeatedly create an USEREVENT. The time is set  in milliseconds. Set a random time by random.randint(a, b) e.g. set a time between 0.5 and 4 seconds (of course you can choose your own time interval):

min_time, max_time = 500, 4000 # 0.5 seconds to to 4 seconds
spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))

Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to start at pygame.USEREVENT. In this case pygame.USEREVENT+1 is the event id for the timer event, which spawns the poops.

Create a new poop when the event occurs in the event loop and set a new random time:

for event in pygame.event.get():
    # [...]
    if event.type == spawn_event:
        pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
        poops.append(Poop(en_x, en_y+en_yvel+en_height//2, 50, 30))

Change the positions of the poops in a loop and remove them from the list, if they leave the window on the left:

for poop in poops[:]:
    poop.update()
    if poop.rect.right <= 0:
        poops.remove(poop)

Draw them ina loop

for poop in poops:
    poop.draw(screen)

See the example:

import pygame
from pygame.locals import *
import random

pygame.init()
pygame.display.set_caption('STINKY BEETLE')

class Poop:
    def __init__(self, x, y, w, h):
        self.rect = pygame.Rect(x, y-h//2, w, h)
        self.vel = -15
    def update(self):
        self.rect.x += self.vel
    def draw(self, surf):
        pygame.draw.rect(surf, (255, 200, 125), self.rect)

screen_width = 800
screen_height = 600
game_running = True
pl_x, pl_y = screen_width//10, screen_height//2
pl_width, pl_height, pl_vel = 80, 40, 30
en_width, en_height, en_yvel = 80, 40, -10
en_x, en_y,  = screen_width - screen_width//10 - en_width, screen_height//2

screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()

min_time, max_time = 500, 4000 # 0.5 seconds up to 4 seconds
spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
poops = []

while game_running:
    clock.tick(10)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False

        if event.type == MOUSEBUTTONDOWN:
            if event.button == 4 and pl_y > pl_vel:
                pl_y -= pl_vel
            elif event.button == 5 and pl_y < screen_height - pl_width:
                pl_y += pl_vel

        if event.type == spawn_event:
            pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
            poops.append(Poop(en_x, en_y+en_yvel+en_height//2, 50, 30))

    en_y += en_yvel
    if en_y <= 0 or en_y >= screen_height - en_height:
        en_yvel =- en_yvel

    for poop in poops[:]:
        poop.update()
        if poop.rect.right <= 0:
            poops.remove(poop)

    screen.fill((0, 0, 0))
    for poop in poops:
        poop.draw(screen)
    pygame.draw.rect(screen, (105, 255, 125), (pl_x, pl_y, pl_width, pl_height))
    pygame.draw.rect(screen, (255, 125, 115), (en_x, en_y, en_width, en_height))
    pygame.display.update()

pygame.quit()