Monday, 2 September 2013

how to delete an array which contains a specified string

how to delete an array which contains a specified string

I have an array called "VALUES" that contains multiple arrays. In these
array there is a field named "test", I only want the arrays pointed out
that contains the number 4 in the test field.
my current output for Values array:
Array (
[0] => Array ( [entry_id] => 41149 [o_number] => 000001 [test1] => 000001
[test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[1] => Array ( [entry_id] => 41142 [o_number] => 000202[test1] => 000202
[test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[2] => Array ( [entry_id] => 41103 [o_number] => 000003 [test1] => 000003
[test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[3] => Array ( [entry_id] => 41101 [o_number] => 000044 [test1] => 000044
[test2] => 1234 [lev] => Ja [fak] => Manuel/brev [beta] => 10 [test] => 2
)
[4] => Array ( [entry_id] => 41100 [o_number] => 000542 [test1] => 000542
[test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[5] => Array ( [entry_id] => 41088 [o_number] => 001231 [test1] => 001231
[test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 3 ))
desired output:
Array (
[0] => Array ( [entry_id] => 41149 [o_number] => 000001 [test1] => 000001
[test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[1] => Array ( [entry_id] => 41142 [o_number] => 000202[test1] => 000202
[test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[2] => Array ( [entry_id] => 41103 [o_number] => 000003 [test1] => 000003
[test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[3] => Array ( [entry_id] => 41100 [o_number] => 000542 [test1] => 000542
[test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 ))
I tried with a foreach but it didn't work
foreach ($values as $key)
{
if($key === 4)
{
print_r($key);
}
}

Django rest framework overwriting initial for custom logging bypasses OAuth initialisation

Django rest framework overwriting initial for custom logging bypasses
OAuth initialisation

I'm sure there must be something I don't understand about type hierarchies
and initialisation in python... I wanted to log post bodies with django
rest framework like suggested here on stackoverflow: by overriding initial
and finalize_response.
This is how my mixin looks like:
class LoggingMixin(object):
"""
Provides full logging of requests and responses
"""
def finalize_response(self, request, response, *args, **kwargs):
# do the logging
if settings.DEBUG:
logger.debug("[{0}] {1}".format(self.__class__.__name__,
response.data))
return super(LoggingMixin, self).finalize_response(request, response,
*args, **kwargs)
def initial(self, request, *args, **kwargs):
# do the logging
if settings.DEBUG:
try:
data = request._data
logger.debug("[{0}] {1}".format(self.__class__.__name__, data))
except exceptions.ParseError:
data = '[Invalid data in request]'
super(LoggingMixin, self).initial(self, request, *args, **kwargs)
And my view:
class BulkScan(LoggingMixin, generics.ListCreateAPIView):
"""
Provides get (list all) and post (single) for scans.
"""
queryset = Scan.objects.all()
serializer_class = ScanSerializer
authentication_classes = (OAuth2Authentication,)
permission_classes = (IsAuthenticated,)
# insert the user on save
def pre_save(self, obj):
for scan in obj:
scan.user = self.request.user
def post(self, request, *args, **kwargs):
serializer = ScanSerializer(data=request.DATA, many=True)
if serializer.is_valid():
self.pre_save(serializer.object)
self.object = serializer.save(force_insert=True)
self.post_save(self.object, created=True)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED,
headers=headers)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Yet, a post or get request fails, complaining that the request.user
property is not present. This must automagically be injected there. If I
don't overwrite initial then everything is fine and the user is set when
APIView.initial is called.
I don't get why the user property is not set when I override the method.
Many thanks

How to show animated gif using Bootstrap button when clicked

How to show animated gif using Bootstrap button when clicked

i know how to show loading... text when user click on Bootstrap button.
this way i do it
http://jsfiddle.net/YCst4/1/
add two dependency files
1) bootstrap.min.css
2) bootstrap.min.js
<button class="btn" id="button" data-text-loading="Loading...">Press
Me</button>
$('#button').button();
$('#button').click(function() {
$(this).button('loading');
});
now how could i show this animated gif
http://www.bba-reman.com/images/fbloader.gif at the place of button when
user will click on button.
please guide me how to do with sample code if possible. thanks

Sunday, 1 September 2013

What's wrong with this C++ function?

What's wrong with this C++ function?

I am trying to get the root cause of memory corruption in a C++
multi-threaded application. When I turn on gflags with "Enable Page Heap",
I am getting access violation consistently, when the following function
returns. This function is being called in many different threads at the
same time. Here is a modified version of the same function:-
std::string CNetWorkerThreadB::TranslateFunctionNumberToText(int
iFunction)
{
std::string strFunctText = "";
if (iFunction & 1)
{
strFunctText = "DPF, ";
}
if (iFunction & 2)
{
strFunctText.append("TC, ");
}
if (iFunction & 3)
{
strFunctText.append("PRV, ");
}
if (strFunctText.length() < 2)
{
return strFunctText;
}
return strFunctText.substr(0, strFunctText.length() - 2);
}

How to update the azimuth with the rotation matrix from gyroscope?

How to update the azimuth with the rotation matrix from gyroscope?

Suppose I have my current orientation as (azimuth, pitch, roll). Now I
wish to update my orientation with the gyroscope. According to the codes
given by the Android development web, I can obtain the so-called
deltaRotationMatrix as follows:
// Create a constant to convert nanoseconds to seconds.
private static final float NS2S = 1.0f / 1000000000.0f;
private final float[] deltaRotationVector = new float[4]();
private float timestamp;
public void onSensorChanged(SensorEvent event) {
// This timestep's delta rotation to be multiplied by the current rotation
// after computing it from the gyro sample data.
if (timestamp != 0) {
final float dT = (event.timestamp - timestamp) * NS2S;
// Axis of the rotation sample, not normalized yet.
float axisX = event.values[0];
float axisY = event.values[1];
float axisZ = event.values[2];
// Calculate the angular speed of the sample
float omegaMagnitude = sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
// Normalize the rotation vector if it's big enough to get the axis
// (that is, EPSILON should represent your maximum allowable margin of
error)
if (omegaMagnitude > EPSILON) {
axisX /= omegaMagnitude;
axisY /= omegaMagnitude;
axisZ /= omegaMagnitude;
}
// Integrate around this axis with the angular speed by the timestep
// in order to get a delta rotation from this sample over the timestep
// We will convert this axis-angle representation of the delta rotation
// into a quaternion before turning it into the rotation matrix.
float thetaOverTwo = omegaMagnitude * dT / 2.0f;
float sinThetaOverTwo = sin(thetaOverTwo);
float cosThetaOverTwo = cos(thetaOverTwo);
deltaRotationVector[0] = sinThetaOverTwo * axisX;
deltaRotationVector[1] = sinThetaOverTwo * axisY;
deltaRotationVector[2] = sinThetaOverTwo * axisZ;
deltaRotationVector[3] = cosThetaOverTwo;
}
timestamp = event.timestamp;
float[] deltaRotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(deltaRotationMatrix,
deltaRotationVector);
// User code should concatenate the delta rotation we computed with
the current rotation
// in order to get the updated rotation.
// rotationCurrent = rotationCurrent * deltaRotationMatrix;
}
}
How should I proceed with this snippet so as to update my orientation?

how save my spinner item

how save my spinner item

i am using spinner and my spinner is working good but item of spinner not
saving for example when i choose font2 for my text my texts font is change
and my change is saved for next but in spinner show font1 after close my
program my code is : in this code i add item to spinner
package com.testfont.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
public class font extends Activity {
public static String font="tahoma.ttf";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.font);
final List<String> list = new ArrayList<String>();
String item1 = "font1";
String item2 = "font2";
//String item3 = "ÝæäÊ Óå";
list.add(item1);
list.add(item2);
//list.add(item3);
Arrayadapt ad = new Arrayadapt(getApplicationContext(), 0, list);
Spinner sp = (Spinner)findViewById(R.id.spinner1);
sp.setAdapter(ad);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View
selectedItemView,
int position, long id) {
// TODO Auto-generated method stub
//boolean selected = false;
if(position == 0){
font="tahoma.ttf";
}else if(position == 1){
font="QuranTaha.ttf";
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
// TODO Auto-generated method stub
public String getFont() {
// TODO Auto-generated method stub
return font;
}
}
in this code i addapt to spinner
package com.testfont.test;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class Arrayadapt extends ArrayAdapter{
Typeface tf;
List<String> _list;
Context context;
LayoutInflater mInflater;
public Arrayadapt(Context _context, int _resource,
List<String> _items) {
super(_context, _resource, _items);
// TODO Auto-generated constructor stub
this.context = _context;
this.tf =
Typeface.createFromAsset(_context.getAssets(),"font/Yekan.ttf");
this._list = _items;
this.mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = mInflater;
View row = inflater.inflate(R.layout.tspinner, parent,
false);
TextView v = (TextView) row.findViewById(R.id.textView1);
v.setTypeface(tf);
v.setText(Farsi.Convert(_list.get(position)));
return row;
}
public View getDropDownView(int position, View convertView, ViewGroup
parent) {
LayoutInflater inflater = mInflater;
View row = inflater.inflate(R.layout.tspinner, parent,
false);
TextView v = (TextView) row.findViewById(R.id.textView1);
v.setTypeface(tf);
v.setText(Farsi.Convert(_list.get(position)));
return row;
}
}

How does facebook update the feeds dynamically?

How does facebook update the feeds dynamically?

How does facebook update the feeds automatically without even refreshing
the page , moreover the feeds get added up on the top of the page. Which
language tho they use to make that possible?