How to concatenate arrays from multiple documents in MongoDB?
Let's say I have a collection called 'people' with the following documents:
{
"name": "doug",
"colors": ["blue", "red"]
}
{
"name": "jack",
"colors": ["blue", "purple"]
}
{
"name": "jenny",
"colors": ["pink"]
}
How would I get a concatenated array of all the colors subarrays, i.e.?
["blue", "red", "blue", "purple", "pink"]
Monday, 2 September 2013
How to match list of urls with a list of patterns (a regex)
How to match list of urls with a list of patterns (a regex)
I have a list of urls and I want to find the name of service from given
list of url pattern and names, Currently I pick a url and match it with
all the patterns, Since both list can be huge, what the best way for
url(s) pattern matching and finding the service name? Current
Implementation is below.
urls
http://www.facebook.com
http://0.facebook.com
http://m.facebook.com
http://www.linkedin.com
Pattern service name
facebook.com Facebook
linkedin.com LinkedIn
def get_service_name(url, services_details):
url = url.rsplit('?')
# urls pattern matching
for service in services_details:
if len(url) > 1:
if service[0] in url[0]:
return service[1]
else:
if service[0] in url:
return service[1]
return "Unknown Service"
I have a list of urls and I want to find the name of service from given
list of url pattern and names, Currently I pick a url and match it with
all the patterns, Since both list can be huge, what the best way for
url(s) pattern matching and finding the service name? Current
Implementation is below.
urls
http://www.facebook.com
http://0.facebook.com
http://m.facebook.com
http://www.linkedin.com
Pattern service name
facebook.com Facebook
linkedin.com LinkedIn
def get_service_name(url, services_details):
url = url.rsplit('?')
# urls pattern matching
for service in services_details:
if len(url) > 1:
if service[0] in url[0]:
return service[1]
else:
if service[0] in url:
return service[1]
return "Unknown Service"
Should I fix Xcode 5 'Semantic issue: undeclared selector'?
Should I fix Xcode 5 'Semantic issue: undeclared selector'?
I'm trying to upgrade my app with Xcode5 but encountered a number of
'Semantic issues' in a third party library (the MagicalRecord one). The
quickest way to do 'fix' this might be using the:
#pragma GCC diagnostic ignored "-Wundeclared-selector"
(from: How to get rid of the 'undeclared selector' warning)
compiler directive, but my gut-feeling says this is not the appropriate
way to do this. A small code sample with the above error:
+ (NSEntityDescription *)
MR_entityDescriptionInContext:(NSManagedObjectContext *)context {
if ([self respondsToSelector:@selector(entityInManagedObjectContext:)])
{
NSEntityDescription *entity = [self
performSelector:@selector(entityInManagedObjectContext:)
withObject:context];
return entity;
}
else
{
NSString *entityName = [self MR_entityName];
return [NSEntityDescription entityForName:entityName
inManagedObjectContext:context];
}
}
where the entityInManagedObjectContext: method is not defined anywhere.
Any suggestions on how to best fix these types of errors, thanks in advance?!
I'm trying to upgrade my app with Xcode5 but encountered a number of
'Semantic issues' in a third party library (the MagicalRecord one). The
quickest way to do 'fix' this might be using the:
#pragma GCC diagnostic ignored "-Wundeclared-selector"
(from: How to get rid of the 'undeclared selector' warning)
compiler directive, but my gut-feeling says this is not the appropriate
way to do this. A small code sample with the above error:
+ (NSEntityDescription *)
MR_entityDescriptionInContext:(NSManagedObjectContext *)context {
if ([self respondsToSelector:@selector(entityInManagedObjectContext:)])
{
NSEntityDescription *entity = [self
performSelector:@selector(entityInManagedObjectContext:)
withObject:context];
return entity;
}
else
{
NSString *entityName = [self MR_entityName];
return [NSEntityDescription entityForName:entityName
inManagedObjectContext:context];
}
}
where the entityInManagedObjectContext: method is not defined anywhere.
Any suggestions on how to best fix these types of errors, thanks in advance?!
Dynamically added link button disappeared on button click leaving an empty column
Dynamically added link button disappeared on button click leaving an empty
column
The datagrid reads from several xml files, so I create the columns
dynamically, and added a templatefield as the last column.
A link button is added in the templatefield using RowDataBound.
Private Sub GridItem_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridItem.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow Then
Dim linkb As New LinkButton
linkb.Text = "Delete"
linkb.ID = "LinkDeleteItem"
linkb.OnClientClick = "javascript:DeleteItem('" &
Convert.ToString(e.Row.RowIndex) & "')"
e.Row.Cells(GridItem.Columns.Count - 1).Controls.Add(linkb)
End If
Catch ex As Exception
lblMessage.Text = ex.Message
End Try
End Sub
Everything works fine.
But when I click a button outside the gridview, to open a window to add a
new item to the grid, the linkbuttons disappear. But the column is still
there.
If I just close the new window without saving a new data (which will
prompt the grid to rebind), the column remains empty. I had to reload the
gridview for the linkbuttons to appear.
Is it because the linkbuttons are created on rowdatabound? How should I
solve this?
column
The datagrid reads from several xml files, so I create the columns
dynamically, and added a templatefield as the last column.
A link button is added in the templatefield using RowDataBound.
Private Sub GridItem_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridItem.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow Then
Dim linkb As New LinkButton
linkb.Text = "Delete"
linkb.ID = "LinkDeleteItem"
linkb.OnClientClick = "javascript:DeleteItem('" &
Convert.ToString(e.Row.RowIndex) & "')"
e.Row.Cells(GridItem.Columns.Count - 1).Controls.Add(linkb)
End If
Catch ex As Exception
lblMessage.Text = ex.Message
End Try
End Sub
Everything works fine.
But when I click a button outside the gridview, to open a window to add a
new item to the grid, the linkbuttons disappear. But the column is still
there.
If I just close the new window without saving a new data (which will
prompt the grid to rebind), the column remains empty. I had to reload the
gridview for the linkbuttons to appear.
Is it because the linkbuttons are created on rowdatabound? How should I
solve this?
Sunday, 1 September 2013
Checking if authorized access on http get response
Checking if authorized access on http get response
I'm trying to check whether the user and password is correct. I'm
consuming an API and here's my code :
String username = user.getText().toString(), password =
pass.getText().toString();
String URL = "MyUrl";
String authData = "Basic " + Base64.encodeToString((username + ":" +
password).getBytes(), Base64.NO_WRAP);
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(URL);
httpget.setHeader("Authorization", authData);
HttpResponse response = null;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
String s = EntityUtils.toString(entity);
Toast.makeText(getBaseContext(), s,
Toast.LENGTH_SHORT).show();
Intent i = new Intent(Login.this,DriverLogin.class);
startActivity(i);
finish();
}
else
Toast.makeText(getBaseContext(), "Fail",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// Auto-generated catch block
e.printStackTrace();
}
finally {
}
I've got no problem if the username and password is correct. My problem is
when the user inputted an incorrect login credentials. I have a line there
checking if entity is not null. As I observed, whether the login
credentials is right or wrong it goes in this condition. When correct, it
displays the details I need. When wrong, it displays a message that
details are not authorize. I want to catch that message in order to say
that login credentials is wrong. I thought that if the details is wrong,
entity will be null. Any ideas? Thanks!
I'm trying to check whether the user and password is correct. I'm
consuming an API and here's my code :
String username = user.getText().toString(), password =
pass.getText().toString();
String URL = "MyUrl";
String authData = "Basic " + Base64.encodeToString((username + ":" +
password).getBytes(), Base64.NO_WRAP);
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(URL);
httpget.setHeader("Authorization", authData);
HttpResponse response = null;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
String s = EntityUtils.toString(entity);
Toast.makeText(getBaseContext(), s,
Toast.LENGTH_SHORT).show();
Intent i = new Intent(Login.this,DriverLogin.class);
startActivity(i);
finish();
}
else
Toast.makeText(getBaseContext(), "Fail",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// Auto-generated catch block
e.printStackTrace();
}
finally {
}
I've got no problem if the username and password is correct. My problem is
when the user inputted an incorrect login credentials. I have a line there
checking if entity is not null. As I observed, whether the login
credentials is right or wrong it goes in this condition. When correct, it
displays the details I need. When wrong, it displays a message that
details are not authorize. I want to catch that message in order to say
that login credentials is wrong. I thought that if the details is wrong,
entity will be null. Any ideas? Thanks!
Recursion formatting php node->child->child level
Recursion formatting php node->child->child level
After a day of trying to solve this problem, I have to admit defeat.
Need to format a hierarchy it into a report/csv, where it is
indented/hierarchy properly, I need a php function that takes the array,
and creates the hierarchy as below. I know that the start node is "Sales";
//[so] [activity] [milestone] [compleated]
array(
array("Sales", "Motor", "Documentation", "10%"),
array("Sales", "Motor", "Communication", "15%"),
array("Sales", "Motor", "Metro file", "30%"),
array("Sales", "Motor", "Training", "33%"),
array("Sales", "Motor", "Client Services", "23%"),
array("Motor", "Documentation", "Doc revamp", "42%"),
array("Motor", "Documentation", "SLA", "33%"),
array("Motor", "Documentation", "KRA", "25%")
)
Sales
Motor
Documentation 10%
Doc revamp 43%
SLA 33%
KRA 25%
Communication 15%
Metro file 30%
Training 33%
Client Services 23%
Sales
xx Motor
xxx Documentation 10%
xxxx Doc revamp 43%
xxxxx SLA 33%
xxxxx KRA 25%
xxxx Communication 15%
xxxx Metro file 30%
xxxx Training 33%
xxxx Client Services 23%
After a day of trying to solve this problem, I have to admit defeat.
Need to format a hierarchy it into a report/csv, where it is
indented/hierarchy properly, I need a php function that takes the array,
and creates the hierarchy as below. I know that the start node is "Sales";
//[so] [activity] [milestone] [compleated]
array(
array("Sales", "Motor", "Documentation", "10%"),
array("Sales", "Motor", "Communication", "15%"),
array("Sales", "Motor", "Metro file", "30%"),
array("Sales", "Motor", "Training", "33%"),
array("Sales", "Motor", "Client Services", "23%"),
array("Motor", "Documentation", "Doc revamp", "42%"),
array("Motor", "Documentation", "SLA", "33%"),
array("Motor", "Documentation", "KRA", "25%")
)
Sales
Motor
Documentation 10%
Doc revamp 43%
SLA 33%
KRA 25%
Communication 15%
Metro file 30%
Training 33%
Client Services 23%
Sales
xx Motor
xxx Documentation 10%
xxxx Doc revamp 43%
xxxxx SLA 33%
xxxxx KRA 25%
xxxx Communication 15%
xxxx Metro file 30%
xxxx Training 33%
xxxx Client Services 23%
How to enable jQuery, javascript, CSS files compression
How to enable jQuery, javascript, CSS files compression
It is my first time I want to enable jQuery, Javascript and CSS files
compression
but I don't exactly know where to start I searched the net but there was
not a detailed example that I could implement that in my project in Mamp.
Please Help me that how can I enable jQuery, Javascript and CSS files
compressions?
1-In windows servers?
2-In Appache Servers?
also can I enable it through .htaccess files?
It is my first time I want to enable jQuery, Javascript and CSS files
compression
but I don't exactly know where to start I searched the net but there was
not a detailed example that I could implement that in my project in Mamp.
Please Help me that how can I enable jQuery, Javascript and CSS files
compressions?
1-In windows servers?
2-In Appache Servers?
also can I enable it through .htaccess files?
Subscribe to:
Posts (Atom)