问:
Android: Uploading image without losing Exif data
In our app users have been uploading millions of images for years using (roughly) this code:

```<code>BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(postFilePath, bmOptions);
Bitmap roughBitmap = BitmapFactory.decodeFile(postFilePath, bmOptions);

ByteArrayOutputStream stream = new ByteArrayOutputStream();

roughBitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
InputStream fis = new ByteArrayInputStream(stream.toByteArray());

int fileSize = stream.toByteArray().length;
conn.setRequestProperty("Content-Length", Integer.toString(fileSize));
conn.setFixedLengthStreamingMode(fileSize);

...

if (fis != null) {
    byte[] buf = new byte[10240];

    int read;

    while ((read = fis.read(buf)) > 0) {
        os.write(buf, 0, read);
        totalBytesRead += read;
        if (uploadProgressListener != null) {
            try {
                uploadProgressListener.onBytesUploaded(read);
            } catch (Exception e) {
                Log.e(e);
            }
        }
    }

    fis.close();
}
</code>```

Recently we saw the need to preserve the Exif data of uploaded images. The problem is that the image Exif data is lost when compressing the bitmap. I thought of using ExifInterface for extracting this data from the original file:

```<code>ExifInterface oldExif = new ExifInterface(postFilePath);
String value = oldExif.getAttribute(ExifInterface.TAG_DATETIME);
</code>```

..and then adding it to the InputStream fis and then continue uploading the file. The problem is that ExifInterface cannot save Exif data to an InputStream.

How can Exif data be retained in the images when they'er uploaded to the server?

It's not a duplicate:
Just to clarify deeper, I've tried using the suggested duplicate question by using this method:

```<code>public static void copyExif(String originalPath, InputStream newStream) throws IOException {

    String[] attributes = new String[]
            {
                    ExifInterface.TAG_DATETIME,
                    ExifInterface.TAG_DATETIME_DIGITIZED,
                    ExifInterface.TAG_EXPOSURE_TIME,
                    ExifInterface.TAG_FLASH,
                    ExifInterface.TAG_FOCAL_LENGTH,
                    ExifInterface.TAG_GPS_ALTITUDE,
                    ExifInterface.TAG_GPS_ALTITUDE_REF,
                    ExifInterface.TAG_GPS_DATESTAMP,
                    ExifInterface.TAG_GPS_LATITUDE,
                    ExifInterface.TAG_GPS_LATITUDE_REF,
                    ExifInterface.TAG_GPS_LONGITUDE,
                    ExifInterface.TAG_GPS_LONGITUDE_REF,
                    ExifInterface.TAG_GPS_PROCESSING_METHOD,
                    ExifInterface.TAG_GPS_TIMESTAMP,
                    ExifInterface.TAG_MAKE,
                    ExifInterface.TAG_MODEL,
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.TAG_SUBSEC_TIME,
                    ExifInterface.TAG_WHITE_BALANCE
            };

    ExifInterface oldExif = new ExifInterface(originalPath);
    ExifInterface newExif = new ExifInterface(newStream);

    if (attributes.length > 0) {
        for (int i = 0; i < attributes.length; i++) {
            String value = oldExif.getAttribute(attributes[i]);
            if (value != null)
                newExif.setAttribute(attributes[i], value);
        }
        newExif.saveAttributes();
    }
}
</code>```

.. but got the exception java.io.IOException: ExifInterface does not support saving attributes for the current input. after newExif.saveAttributes(); because I'm trying to save the attributes to an InputStream. How else can I do it?

答:
My solution:

As @amuttsch and @CommonsWare suggested, I:

saved the scaled/compressed bitmap to a temp file

copied the exif from the original file to the temp file

converted the temp file to a byte array and sent it to upload

.. then I found out that the server strips the Exif again while generating image variants :-P but that's another story which server guys are now working to correct.

Main code:

```<code>...
// Copy original Exif to scaledBitmap
String tempFilePath = getTempFilePath(postFilePath);
try {
    FileOutputStream out = new FileOutputStream(tempFilePath);
    scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
    copyExif(postFilePath, tempFilePath);
} catch (Exception e) {
    e.printStackTrace();
}

// Get stream from temp (exif loaded) file
File tempFile = new File(tempFilePath);
byte[] byteFile = readFile(tempFile);
fis = new ByteArrayInputStream(byteFile);

// Remove the temp file
boolean deleted = tempFile.delete();

// Finalize
int fileSize = byteFile.length;
conn.setRequestProperty("Content-Length", Integer.toString(fileSize));
conn.setFixedLengthStreamingMode(fileSize);
...
</code>```

getTempFilePath():

```<code>private String getTempFilePath(String filename) {
    String temp = "_temp";
    int dot = filename.lastIndexOf(".");
    String ext = filename.substring(dot + 1);

    if (dot == -1 || !ext.matches("\\w+")) {
        filename += temp;
    } else {
        filename = filename.substring(0, dot) + temp + "." + ext;
    }

    return filename;
}
</code>```

copyExif():

```<code>public static void copyExif(String originalPath, String newPath) throws IOException {

    String[] attributes = new String[]
            {
                    ExifInterface.TAG_DATETIME,
                    ExifInterface.TAG_DATETIME_DIGITIZED,
                    ExifInterface.TAG_EXPOSURE_TIME,
                    ExifInterface.TAG_FLASH,
                    ExifInterface.TAG_FOCAL_LENGTH,
                    ExifInterface.TAG_GPS_ALTITUDE,
                    ExifInterface.TAG_GPS_ALTITUDE_REF,
                    ExifInterface.TAG_GPS_DATESTAMP,
                    ExifInterface.TAG_GPS_LATITUDE,
                    ExifInterface.TAG_GPS_LATITUDE_REF,
                    ExifInterface.TAG_GPS_LONGITUDE,
                    ExifInterface.TAG_GPS_LONGITUDE_REF,
                    ExifInterface.TAG_GPS_PROCESSING_METHOD,
                    ExifInterface.TAG_GPS_TIMESTAMP,
                    ExifInterface.TAG_MAKE,
                    ExifInterface.TAG_MODEL,
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.TAG_SUBSEC_TIME,
                    ExifInterface.TAG_WHITE_BALANCE
            };

    ExifInterface oldExif = new ExifInterface(originalPath);
    ExifInterface newExif = new ExifInterface(newPath);

    if (attributes.length > 0) {
        for (int i = 0; i < attributes.length; i++) {
            String value = oldExif.getAttribute(attributes[i]);
            if (value != null)
                newExif.setAttribute(attributes[i], value);
        }
        newExif.saveAttributes();
    }
}
</code>```

readFile():

```<code>public static byte[] readFile(File file) throws IOException {
    // Open file
    RandomAccessFile f = new RandomAccessFile(file, "r");
    try {
        // Get and check length
        long longlength = f.length();
        int length = (int) longlength;
        if (length != longlength)
            throw new IOException("File size >= 2 GB");
        // Read file and return data
        byte[] data = new byte[length];
        f.readFully(data);
        return data;
    } finally {
        f.close();
    }
}
</code>```

Body:
In our app users have been uploading millions of images for years using (roughly) this code:

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(postFilePath, bmOptions);
Bitmap roughBitmap = BitmapFactory.decodeFile(postFilePath, bmOptions);

ByteArrayOutputStream stream = new ByteArrayOutputStream();

roughBitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
InputStream fis = new ByteArrayInputStream(stream.toByteArray());

int fileSize = stream.toByteArray().length;
conn.setRequestProperty("Content-Length", Integer.toString(fileSize));
conn.setFixedLengthStreamingMode(fileSize);

...

if (fis != null) {
    byte[] buf = new byte[10240];

    int read;

    while ((read = fis.read(buf)) > 0) {
        os.write(buf, 0, read);
        totalBytesRead += read;
        if (uploadProgressListener != null) {
            try {
                uploadProgressListener.onBytesUploaded(read);
            } catch (Exception e) {
                Log.e(e);
            }
        }
    }

    fis.close();
}

Recently we saw the need to preserve the Exif data of uploaded images. The problem is that the image Exif data is lost when compressing the bitmap. I thought of using ExifInterface for extracting this data from the original file:

ExifInterface oldExif = new ExifInterface(postFilePath);
String value = oldExif.getAttribute(ExifInterface.TAG_DATETIME);

..and then adding it to the InputStream fis and then continue uploading the file. The problem is that ExifInterface cannot save Exif data to an InputStream.

How can Exif data be retained in the images when they'er uploaded to the server?

It's not a duplicate:
Just to clarify deeper, I've tried using the suggested duplicate question by using this method:

public static void copyExif(String originalPath, InputStream newStream) throws IOException {

    String[] attributes = new String[]
            {
                    ExifInterface.TAG_DATETIME,
                    ExifInterface.TAG_DATETIME_DIGITIZED,
                    ExifInterface.TAG_EXPOSURE_TIME,
                    ExifInterface.TAG_FLASH,
                    ExifInterface.TAG_FOCAL_LENGTH,
                    ExifInterface.TAG_GPS_ALTITUDE,
                    ExifInterface.TAG_GPS_ALTITUDE_REF,
                    ExifInterface.TAG_GPS_DATESTAMP,
                    ExifInterface.TAG_GPS_LATITUDE,
                    ExifInterface.TAG_GPS_LATITUDE_REF,
                    ExifInterface.TAG_GPS_LONGITUDE,
                    ExifInterface.TAG_GPS_LONGITUDE_REF,
                    ExifInterface.TAG_GPS_PROCESSING_METHOD,
                    ExifInterface.TAG_GPS_TIMESTAMP,
                    ExifInterface.TAG_MAKE,
                    ExifInterface.TAG_MODEL,
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.TAG_SUBSEC_TIME,
                    ExifInterface.TAG_WHITE_BALANCE
            };

    ExifInterface oldExif = new ExifInterface(originalPath);
    ExifInterface newExif = new ExifInterface(newStream);

    if (attributes.length > 0) {
        for (int i = 0; i < attributes.length; i++) {
            String value = oldExif.getAttribute(attributes[i]);
            if (value != null)
                newExif.setAttribute(attributes[i], value);
        }
        newExif.saveAttributes();
    }
}

.. but got the exception java.io.IOException: ExifInterface does not support saving attributes for the current input. after newExif.saveAttributes(); because I'm trying to save the attributes to an InputStream. How else can I do it?

Body:
My solution:

As @amuttsch and @CommonsWare suggested, I:

saved the scaled/compressed bitmap to a temp file

copied the exif from the original file to the temp file

converted the temp file to a byte array and sent it to upload

.. then I found out that the server strips the Exif again while generating image variants :-P but that's another story which server guys are now working to correct.

Main code:

...
// Copy original Exif to scaledBitmap
String tempFilePath = getTempFilePath(postFilePath);
try {
    FileOutputStream out = new FileOutputStream(tempFilePath);
    scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
    copyExif(postFilePath, tempFilePath);
} catch (Exception e) {
    e.printStackTrace();
}

// Get stream from temp (exif loaded) file
File tempFile = new File(tempFilePath);
byte[] byteFile = readFile(tempFile);
fis = new ByteArrayInputStream(byteFile);

// Remove the temp file
boolean deleted = tempFile.delete();

// Finalize
int fileSize = byteFile.length;
conn.setRequestProperty("Content-Length", Integer.toString(fileSize));
conn.setFixedLengthStreamingMode(fileSize);
...

getTempFilePath():

private String getTempFilePath(String filename) {
    String temp = "_temp";
    int dot = filename.lastIndexOf(".");
    String ext = filename.substring(dot + 1);

    if (dot == -1 || !ext.matches("\\w+")) {
        filename += temp;
    } else {
        filename = filename.substring(0, dot) + temp + "." + ext;
    }

    return filename;
}

copyExif():

public static void copyExif(String originalPath, String newPath) throws IOException {

    String[] attributes = new String[]
            {
                    ExifInterface.TAG_DATETIME,
                    ExifInterface.TAG_DATETIME_DIGITIZED,
                    ExifInterface.TAG_EXPOSURE_TIME,
                    ExifInterface.TAG_FLASH,
                    ExifInterface.TAG_FOCAL_LENGTH,
                    ExifInterface.TAG_GPS_ALTITUDE,
                    ExifInterface.TAG_GPS_ALTITUDE_REF,
                    ExifInterface.TAG_GPS_DATESTAMP,
                    ExifInterface.TAG_GPS_LATITUDE,
                    ExifInterface.TAG_GPS_LATITUDE_REF,
                    ExifInterface.TAG_GPS_LONGITUDE,
                    ExifInterface.TAG_GPS_LONGITUDE_REF,
                    ExifInterface.TAG_GPS_PROCESSING_METHOD,
                    ExifInterface.TAG_GPS_TIMESTAMP,
                    ExifInterface.TAG_MAKE,
                    ExifInterface.TAG_MODEL,
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.TAG_SUBSEC_TIME,
                    ExifInterface.TAG_WHITE_BALANCE
            };

    ExifInterface oldExif = new ExifInterface(originalPath);
    ExifInterface newExif = new ExifInterface(newPath);

    if (attributes.length > 0) {
        for (int i = 0; i < attributes.length; i++) {
            String value = oldExif.getAttribute(attributes[i]);
            if (value != null)
                newExif.setAttribute(attributes[i], value);
        }
        newExif.saveAttributes();
    }
}

readFile():

public static byte[] readFile(File file) throws IOException {
    // Open file
    RandomAccessFile f = new RandomAccessFile(file, "r");
    try {
        // Get and check length
        long longlength = f.length();
        int length = (int) longlength;
        if (length != longlength)
            throw new IOException("File size >= 2 GB");
        // Read file and return data
        byte[] data = new byte[length];
        f.readFully(data);
        return data;
    } finally {
        f.close();
    }
}


来源: stackexchange

问:
Change database when logging in with devise
I am making an application in which the login screen the user can select a company in a selectbox and each company has a different database, at the moment of pressing the login button I want to change database depending Of the company and to log in the user must be in that database. I have been looking for a bit and it is possible to change the database by placing in the model establish_connection ("otherbase") but I do not know how to apply it to the whole system and at the beginning of session with devise

答:
What you want to do is after sign in check in which database the user is, but therefore you need to connect to every database.

What could be a solution is create a database with all users and a field to which database the user belongs to.
Then after the sign in check which database to connect with.

ContentLicense: CC BY-SA 3.0


问:
Redgate migration scripts not running on deployment
I've been reading through the Redgate documentation on migration scripts and I'm trying to add a new column to a table that has a foreign key from another table.

Here's what I have done:

Added the new column, made it null-able and created the
relationship to a new table then I've committed the changes.

I then add static data to the new table so that the migration can
run. I commit this static data.I then add a blank migration script,
and set all null values on the column I've created in the last
commit to be the Id of one of the records in the related table. I
then commit this change.

I then run a deployment of both commits to my testing environment where
records already exist.

The problem I'm having is that the column gets created but the script seems like its not running as the column values stay null. I've verified that the script should actually change the columns as I've attempted to run it manually and it executes successfully.

Am I doing something wrong when using these scripts? Thanks.

答: I was creating blank migration scripts which lead to SQL Compare to set the column as not null. You have to specifically create a migration script on the schema change that requires it or SQL Compare will override all changes.</p>


问:
Is Stripe Checking Customer Card or Source is Valid, After I changed it?
My question is Stripe handling customer card is valid or not. Do I have to try my own 1$ payment. And if stripe checking it how I will get informed. I am always using 424242.. test card soo it always saves card succesfuly.</p>

答: Stripe runs a $0/$1 authorization with the bank when you create a customer or a card. This ensures the card is valid upfront and if it isn't the customer or card creation would fail.</p>


问:
php array_filter if time is within 1 hour
I am trying to figure out how to filter an object based on a comparison.

I get an object that looks something like this, although with 15 results....

```<code>stdClass Object
(
    [FlightInfoExResult] => stdClass Object
        (
            [next_offset] => 15
            [flights] => Array
                (
                    [0] => stdClass Object
                        (
                            [faFlightID] => SWA2078-1499232401-airline-0885
                            [ident] => SWA2078
                            [aircrafttype] => B738
                            [filed_ete] => 03:00:00
                            [filed_time] => 1499232401
                            [filed_departuretime] => 1499477700
                            [filed_airspeed_kts] => 423
                            [filed_airspeed_mach] =>
                            [filed_altitude] => 0
                            [route] =>
                            [actualdeparturetime] => 0
                            [estimatedarrivaltime] => 1499489100
                            [actualarrivaltime] => 0
                            [diverted] =>
                            [origin] => KPHX
                            [destination] => KMKE
                            [originName] => Phoenix Sky Harbor Intl
                            [originCity] => Phoenix, AZ
                            [destinationName] => General Mitchell Intl
</code>```

i need to filter this object so only return the [flights] where [filed_departuretime] is within 1 hour of Query result $time = strtotime($item['departure_time']);

by doing something like this:

```<code>abs($object->filed_departuretime - $time) <= 3600)
</code>```

hopefully this all makes sense, thanks for your help :) I dont really know how array_filter works so this is what I have so far....

```<code>//set flight identity
$ident = $item['airline'].$item['flight_number'];
$date = date('Y-m-d H:i:s');
$time = strtotime($item['departure_time']);

//get flightaware results
$flightAwareResult = FlightInfoEx($ident);

/*$flightResult = array_filter(
  $flightAwareResult,
);*/
</code>```

答:
Array filter takes an array and passes each element through a function. It only keeps elements for which the function returned a true value. Example:</p>

```<code>//set flight identity
$ident = $item['airline'].$item['flight_number'];
$date = date('Y-m-d H:i:s');
$time = strtotime($item['departure_time']);

//get flightaware results
$flightAwareResult = FlightInfoEx($ident);
$filtered_array = array_filter($flightAwareResult->FlightInfoExResult->flights, function ($value) use ($time) {
      return  $time - $value->filed_departuretime) <= 3600;
});
</code>```

Body:
I am trying to figure out how to filter an object based on a comparison.

I get an object that looks something like this, although with 15 results....

stdClass Object
(
    [FlightInfoExResult] => stdClass Object
        (
            [next_offset] => 15
            [flights] => Array
                (
                    [0] => stdClass Object
                        (
                            [faFlightID] => SWA2078-1499232401-airline-0885
                            [ident] => SWA2078
                            [aircrafttype] => B738
                            [filed_ete] => 03:00:00
                            [filed_time] => 1499232401
                            [filed_departuretime] => 1499477700
                            [filed_airspeed_kts] => 423
                            [filed_airspeed_mach] =>
                            [filed_altitude] => 0
                            [route] =>
                            [actualdeparturetime] => 0
                            [estimatedarrivaltime] => 1499489100
                            [actualarrivaltime] => 0
                            [diverted] =>
                            [origin] => KPHX
                            [destination] => KMKE
                            [originName] => Phoenix Sky Harbor Intl
                            [originCity] => Phoenix, AZ
                            [destinationName] => General Mitchell Intl

i need to filter this object so only return the [flights] where [filed_departuretime] is within 1 hour of Query result $time = strtotime($item['departure_time']);

by doing something like this:

abs($object->filed_departuretime - $time) <= 3600)

hopefully this all makes sense, thanks for your help :) I dont really know how array_filter works so this is what I have so far....

//set flight identity
$ident = $item['airline'].$item['flight_number'];
$date = date('Y-m-d H:i:s');
$time = strtotime($item['departure_time']);

//get flightaware results
$flightAwareResult = FlightInfoEx($ident);

/*$flightResult = array_filter(
  $flightAwareResult,
);*/

Body:
Array filter takes an array and passes each element through a function. It only keeps elements for which the function returned a true value. Example:

//set flight identity
$ident = $item['airline'].$item['flight_number'];
$date = date('Y-m-d H:i:s');
$time = strtotime($item['departure_time']);

//get flightaware results
$flightAwareResult = FlightInfoEx($ident);
$filtered_array = array_filter($flightAwareResult->FlightInfoExResult->flights, function ($value) use ($time) {
      return  $time - $value->filed_departuretime) <= 3600;
});


问:
Appcelerator: Detect when user presses home button (iOS & Android)
I want to detect when the user presses the home button in Android and iOS and then logout from the session. Is there any way to do that?

On iOS I tried to use the Titanium.App.pause listener and it works when I press the home button, but the problem is that this listener is also triggered when I do other things inside the app, like opening the gallery or the camera from the app or even using the control center.

On Android I tried a similar thing. But I'm facing the same problem.

答:
I think there is no event to detect home button pressed then to use pasue/resume event.

You can do one thing though, use boollean global variable to store whether gallery or camera or other events done which pauses app to background. That way you can catch home button clicked.


问:
Entering data from form into database: sqlalchemy.orm.exc.UnmappedInstanceError
Please ignore my hack job, I'm just trying to get data from a form inserted into SQL Server. </p>

```<code>@app.route("/contact", methods = ['GET', 'POST'])
def contact():
   form = PayRoll_Form()

   if request.method == 'POST':
       if form.validate() == False:
           flash('All fields are required.')
           return render_template('contact.html', form = form)
       else:
           N = engine.execute(PayRoll.insert(),
               Name = request.form['Name'],
               CreateUserPK = request.form['CreateUserPK'],
               PeriodStart = request.form['PeriodStart'],
               PeriodStop = request.form['PeriodStop'],
               DueDate = request.form['DueDate'])
           session.add(N)
           session.commit()
           flash('Submited')
           return redirect("/contact")
   if request.method == 'GET':
       return render_template('contact.html', form = form)
if __name__ == '__main__':
    app.run(debug=True,port=5000)
</code>```

答:
Solved the issue here:</p>

```<code>@app.route("/contact", methods = ['GET', 'POST'])
def contact():
   form = PayRoll_Form()

   if request.method == 'POST':
       if form.validate() == False:
           flash('All fields are required.')
           return render_template('contact.html', form = form)
       else:
           ins = PayRoll.insert().values(
               Name = request.form['Name'],
               CreateUserPK = request.form['CreateUserPK'],
               PeriodStart = request.form['PeriodStart'],
               PeriodStop = request.form['PeriodStop'],
               DueDate = request.form['DueDate'])
           result = connection.execute(ins)
           flash('Submited')
           return redirect("/contact")
   if request.method == 'GET':
       return render_template('contact.html', form = form)
if __name__ == '__main__':
    app.run(debug=True,port=5000)
</code>```

Body:
Please ignore my hack job, I'm just trying to get data from a form inserted into SQL Server.

@app.route("/contact", methods = ['GET', 'POST'])
def contact():
   form = PayRoll_Form()

   if request.method == 'POST':
       if form.validate() == False:
           flash('All fields are required.')
           return render_template('contact.html', form = form)
       else:
           N = engine.execute(PayRoll.insert(),
               Name = request.form['Name'],
               CreateUserPK = request.form['CreateUserPK'],
               PeriodStart = request.form['PeriodStart'],
               PeriodStop = request.form['PeriodStop'],
               DueDate = request.form['DueDate'])
           session.add(N)
           session.commit()
           flash('Submited')
           return redirect("/contact")
   if request.method == 'GET':
       return render_template('contact.html', form = form)
if __name__ == '__main__':
    app.run(debug=True,port=5000)

Body:
Solved the issue here:

@app.route("/contact", methods = ['GET', 'POST'])
def contact():
   form = PayRoll_Form()

   if request.method == 'POST':
       if form.validate() == False:
           flash('All fields are required.')
           return render_template('contact.html', form = form)
       else:
           ins = PayRoll.insert().values(
               Name = request.form['Name'],
               CreateUserPK = request.form['CreateUserPK'],
               PeriodStart = request.form['PeriodStart'],
               PeriodStop = request.form['PeriodStop'],
               DueDate = request.form['DueDate'])
           result = connection.execute(ins)
           flash('Submited')
           return redirect("/contact")
   if request.method == 'GET':
       return render_template('contact.html', form = form)
if __name__ == '__main__':
    app.run(debug=True,port=5000)


问:
How to format unicode strings to utf-8 in Python?
I'm reading in a JSON string which is littered with u'string' style strings.  Example:

```<code> [
     {
      "!\/award\/award_honor\/honored_for": {
        "award": {
          "id": "\/en\/spiel_des_jahres"
        },
        "year": {
          "value": "1996"
        }
      },
      "guid": "#9202a8c04000641f80000000003a0ee6",
      "type": "\/games\/game",
      "id": "\/en\/el_grande",
      "name": "El Grande"
    },
    {
      "!\/award\/award_honor\/honored_for": {
        "award": {
          "id": "\/en\/spiel_des_jahres"
        },
        "year": {
          "value": "1995"
        }
      },
      "guid": "#9202a8c04000641f80000000000495ec",
      "type": "\/games\/game",
      "id": "\/en\/settlers_of_catan",
      "name": "Settlers of Catan"
    }
  ]
</code>```

If I assign name = result.name.  Then when I log of pass that value to a Django template, it displays as u'Dominion'

How do I format it to display as Dominion?

++ UPDATE ++

I think the problem has to do with printing values from a list or dictionary.  For example:

```<code>result = freebase.mqlread(query)

games = {}
count = 0
r = result[0]
name = r.name
games["name"] = name,
self.response.out.write(games["name"])
self.response.out.write(name)
</code>```

This displays as:

```<code>(u'Dominion',)  // saved response to dictionary, and then printed
Dominion        // when calling the value directly from the response
</code>```

I need to iterate through an array of JSON items and the values are being shown with the unicode.  Why?

答:
```<code>>>> # example
>>> s = u"Jägermütze"
>>> s.encode("utf-8")
'J\xc3\xa4germ\xc3\xbctze'
>>> print s.encode("utf-8") # on a utf-8 terminal
Jägermütze
</code>```

Don't know much about Django, but not accepting snicode strings seems unpythonic to me.

ContentLicense: CC BY-SA 2.5

Body:
I'm reading in a JSON string which is littered with u'string' style strings.  Example:

 [
     {
      "!\/award\/award_honor\/honored_for": {
        "award": {
          "id": "\/en\/spiel_des_jahres"
        },
        "year": {
          "value": "1996"
        }
      },
      "guid": "#9202a8c04000641f80000000003a0ee6",
      "type": "\/games\/game",
      "id": "\/en\/el_grande",
      "name": "El Grande"
    },
    {
      "!\/award\/award_honor\/honored_for": {
        "award": {
          "id": "\/en\/spiel_des_jahres"
        },
        "year": {
          "value": "1995"
        }
      },
      "guid": "#9202a8c04000641f80000000000495ec",
      "type": "\/games\/game",
      "id": "\/en\/settlers_of_catan",
      "name": "Settlers of Catan"
    }
  ]

If I assign name = result.name.  Then when I log of pass that value to a Django template, it displays as u'Dominion'

How do I format it to display as Dominion?

++ UPDATE ++

I think the problem has to do with printing values from a list or dictionary.  For example:

result = freebase.mqlread(query)

games = {}
count = 0
r = result[0]
name = r.name
games["name"] = name,
self.response.out.write(games["name"])
self.response.out.write(name)

This displays as:

(u'Dominion',)  // saved response to dictionary, and then printed
Dominion        // when calling the value directly from the response

I need to iterate through an array of JSON items and the values are being shown with the unicode.  Why?

Body:
>>> # example
>>> s = u"Jägermütze"
>>> s.encode("utf-8")
'J\xc3\xa4germ\xc3\xbctze'
>>> print s.encode("utf-8") # on a utf-8 terminal
Jägermütze

Don't know much about Django, but not accepting snicode strings seems unpythonic to me.


问:
Numpy stack with unequal shapes
I've noticed that the solution to combining 2D arrays to 3D arrays through np.stack, np.dstack, or simply passing a list of arrays only works when the arrays have same .shape[0].

For instance, say I have:

```<code>print(arr)
[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
</code>```

it easy easy to get to:

```<code>print(np.array([arr[2:4], arr[3:5]])) # same shape
[[[4 5]
  [6 7]]

 [[6 7]
  [8 9]]]
</code>```

However, if I pass a list of arrays of unequal length, I get:

```<code>print(np.array([arr[:2], arr[:3]]))
[array([[0, 1],
       [2, 3]])
 array([[0, 1],
       [2, 3],
       [4, 5]])]
</code>```

How can I get to simply:

```<code>[[[0, 1]
  [2, 3]]
 [[0, 1]
  [2, 3]
  [4, 5]]]
</code>```

What I've tried: a number of other Array manipulation routines.

Note: ultimately want to do this for more than 2 arrays, so np.append is probably not ideal.

答:
The function np.stack joins multiple arrays along a new axis, not an existing one.  See:

```<code>>>> import numpy as np
>>> arr = np.array(range(10)).reshape((5,2))
>>> print arr
[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
>>> t1 = np.array([arr[2:4], arr[3:5]])
>>> print t1.shape
(2, 2, 2)
</code>```

It's not creating a new array of shape (4,2) which I think you're intending.  Look at np.concatenate for that.

Note if you really want to use stack, the docs require all input arrays be the same shape:

Parameters:    arrays : sequence of array_like Each array must have the
  same shape.

So what you're doing is going to have undefined behavior.

EDIT: I read too quickly.  You are trying to add an axis.  Still, you can't pass uneven shapes to stack.  You would have to pad them all the the same shape.  Example:

```<code>arr = np.array(range(10)).reshape((5,2))
print arr
arr_p1 = np.zeros(arr[0:3].shape)
arr_p1_src = arr[0:2]
arr_p1[:arr_p1_src.shape[0],:arr_p1_src.shape[1]] = arr_p1_src
t2 = np.array([arr_p1, arr[0:3]])
print t2
</code>```

Output:

```<code>[[[ 0.  1.]
  [ 2.  3.]
  [ 0.  0.]]

 [[ 0.  1.]
  [ 2.  3.]
  [ 4.  5.]]]
</code>```

Title: Numpy stack with unequal shapes

Body:
I've noticed that the solution to combining 2D arrays to 3D arrays through np.stack, np.dstack, or simply passing a list of arrays only works when the arrays have same .shape[0].

For instance, say I have:

print(arr)
[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]

it easy easy to get to:

print(np.array([arr[2:4], arr[3:5]])) # same shape
[[[4 5]
  [6 7]]

 [[6 7]
  [8 9]]]

However, if I pass a list of arrays of unequal length, I get:

print(np.array([arr[:2], arr[:3]]))
[array([[0, 1],
       [2, 3]])
 array([[0, 1],
       [2, 3],
       [4, 5]])]

How can I get to simply:

[[[0, 1]
  [2, 3]]
 [[0, 1]
  [2, 3]
  [4, 5]]]

What I've tried: a number of other Array manipulation routines.

Note: ultimately want to do this for more than 2 arrays, so np.append is probably not ideal.

Body:
The function np.stack joins multiple arrays along a new axis, not an existing one.  See:

>>> import numpy as np
>>> arr = np.array(range(10)).reshape((5,2))
>>> print arr
[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
>>> t1 = np.array([arr[2:4], arr[3:5]])
>>> print t1.shape
(2, 2, 2)

It's not creating a new array of shape (4,2) which I think you're intending.  Look at np.concatenate for that.

Note if you really want to use stack, the docs require all input arrays be the same shape:

Parameters:    arrays : sequence of array_like Each array must have the
  same shape.

So what you're doing is going to have undefined behavior.

EDIT: I read too quickly.  You are trying to add an axis.  Still, you can't pass uneven shapes to stack.  You would have to pad them all the the same shape.  Example:

arr = np.array(range(10)).reshape((5,2))
print arr
arr_p1 = np.zeros(arr[0:3].shape)
arr_p1_src = arr[0:2]
arr_p1[:arr_p1_src.shape[0],:arr_p1_src.shape[1]] = arr_p1_src
t2 = np.array([arr_p1, arr[0:3]])
print t2

Output:

[[[ 0.  1.]
  [ 2.  3.]
  [ 0.  0.]]

 [[ 0.  1.]
  [ 2.  3.]
  [ 4.  5.]]]

ContentLicense: CC BY-SA 4.0


答:
You can use cumcount with [::-1] for change order of columns (rows):

```<code>g = df.groupby('cuts').cumcount(ascending=False)
df = pd.pivot(index=df['cuts'], columns=g, values=df['summary_x']).iloc[:,::-1]
       .reset_index(drop=True)
df.columns = np.arange(len(df.columns))
print (df)
     0    1    2    3    4    5    6    7    8    9
0  3.0  3.0  4.0  4.0  3.0  3.0  3.0  3.0  3.0  3.0
1  NaN  NaN  NaN  4.0  4.0  3.0  3.0  3.0  3.0  3.0
2  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  5.0
</code>```

Another solution:

```<code>L = df[::-1].groupby('cuts')['summary_x'].apply(list).values.tolist()
df = pd.DataFrame(L).iloc[:, ::-1]
df.columns = np.arange(len(df.columns))
print (df)
     0    1    2    3    4    5    6    7    8    9
0  3.0  3.0  4.0  4.0  3.0  3.0  3.0  3.0  3.0  3.0
1  NaN  NaN  NaN  4.0  4.0  3.0  3.0  3.0  3.0  3.0
2  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  5.0
</code>```

But if NaNs can be in the end also:

```<code>g = df.groupby('cuts').cumcount()
df = pd.pivot(index=df['cuts'], columns=g, values=df['summary_x']).reset_index(drop=True)

print (df)
     0    1    2    3    4    5    6    7    8    9
0  3.0  3.0  4.0  4.0  3.0  3.0  3.0  3.0  3.0  3.0
1  4.0  4.0  3.0  3.0  3.0  3.0  3.0  NaN  NaN  NaN
2  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  5.0
</code>```

Title: Reshape GroupBy in Pandas and pad with nan if missing

Body:
Given a data frame with various numbers of elements in each group ('groupby' by some variable), I need to reshape into a matrix with predefined number of columns. For example:

    summary_x  participant_id_x response_date cuts
0         3.0                11    2016-05-05    a
1         3.0                11    2016-05-06    a
2         4.0                11    2016-05-07    a
3         4.0                11    2016-05-08    a
4         3.0                11    2016-05-09    a
5         3.0                11    2016-05-10    a
6         3.0                11    2016-05-11    a
7         3.0                11    2016-05-12    a
8         3.0                11    2016-05-13    a
9         3.0                11    2016-05-14    a
13        4.0                11    2016-05-22    b
14        4.0                11    2016-05-23    b
15        3.0                11    2016-05-24    b
16        3.0                11    2016-05-25    b
17        3.0                11    2016-05-26    b
18        3.0                11    2016-05-27    b
19        3.0                11    2016-05-28    b
20        3.0                11    2016-06-02    c
21        3.0                11    2016-06-03    c
22        3.0                11    2016-06-04    c
23        3.0                11    2016-06-05    c
24        3.0                11    2016-06-06    c
25        3.0                11    2016-06-07    c
26        3.0                11    2016-06-08    c
27        3.0                11    2016-06-09    c
28        3.0                11    2016-06-10    c
29        5.0                11    2016-06-11    c

Each group (by'cuts'), contains 10 elements, but the group 'b' contains only 7. I would like to have a matrix from 'summary_x' reshaped as (3,10), where the missing values will be filled in with nans:

pd.DataFrame(df.summary_x.values.reshape((-1,10)))

      0    1    2    3    4    5    6    7    8    9
0   3.0  3.0  4.0  4.0  3.0  3.0  3.0  3.0  3.0  3.0
1   nan  nan  nan  4.0  4.0  3.0  3.0  3.0  3.0  3.0
2   3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  5.0

any solutions?

Body:
You can use cumcount with [::-1] for change order of columns (rows):

g = df.groupby('cuts').cumcount(ascending=False)
df = pd.pivot(index=df['cuts'], columns=g, values=df['summary_x']).iloc[:,::-1]
       .reset_index(drop=True)
df.columns = np.arange(len(df.columns))
print (df)
     0    1    2    3    4    5    6    7    8    9
0  3.0  3.0  4.0  4.0  3.0  3.0  3.0  3.0  3.0  3.0
1  NaN  NaN  NaN  4.0  4.0  3.0  3.0  3.0  3.0  3.0
2  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  5.0

Another solution:

L = df[::-1].groupby('cuts')['summary_x'].apply(list).values.tolist()
df = pd.DataFrame(L).iloc[:, ::-1]
df.columns = np.arange(len(df.columns))
print (df)
     0    1    2    3    4    5    6    7    8    9
0  3.0  3.0  4.0  4.0  3.0  3.0  3.0  3.0  3.0  3.0
1  NaN  NaN  NaN  4.0  4.0  3.0  3.0  3.0  3.0  3.0
2  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  5.0

But if NaNs can be in the end also:

g = df.groupby('cuts').cumcount()
df = pd.pivot(index=df['cuts'], columns=g, values=df['summary_x']).reset_index(drop=True)

print (df)
     0    1    2    3    4    5    6    7    8    9
0  3.0  3.0  4.0  4.0  3.0  3.0  3.0  3.0  3.0  3.0
1  4.0  4.0  3.0  3.0  3.0  3.0  3.0  NaN  NaN  NaN
2  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  5.0


问:
Custom menu content
I'm trying to build a custom menu (meaning I want to set the template for the menu item)

```<code><Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding Source={StaticResource Locator}, Path=Main}">
    <Window.Resources>
        <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
            <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <Grid VerticalAlignment="Center">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
                    <Path x:Name="GlyphPanel" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="#FF212121" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/>
                    <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="Icon" Value="{x:Null}">
                    <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
                    <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter Property="Background" TargetName="templateRoot" Value="#3D26A0DA"/>
                    <Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
                    <Setter Property="Fill" TargetName="GlyphPanel" Value="#FF707070"/>
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsHighlighted" Value="True"/>
                        <Condition Property="IsEnabled" Value="False"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" TargetName="templateRoot" Value="#0A000000"/>
                    <Setter Property="BorderBrush" TargetName="templateRoot" Value="#21000000"/>
                </MultiTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:MenuItemViewModel}"
                                  ItemsSource="{Binding MenuItems}"
                                  >
            <TextBlock Text="{Binding Caption}" />
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <Menu ItemsSource="{Binding MenuItems}"
              >
            <Menu.ItemContainerStyle>
                <Style TargetType="{x:Type MenuItem}">
                    <Setter Property="Padding" Value="0" />
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                    <Setter Property="BorderThickness" Value="0" />
                    <Setter Property="Template" Value="{StaticResource MenuItemControlTemplate1}" />
                </Style>
            </Menu.ItemContainerStyle>
        </Menu>
    </Grid>
</Window>
</code>```

and the view model

```<code>public class MainViewModel : ViewModelBase
    {
        public ObservableCollection<MenuItemViewModel> MenuItems { get; set; }

        public MainViewModel()
        {
            MenuItems = new ObservableCollection<MenuItemViewModel>
            {
                new MenuItemViewModel("File")
                {
                    MenuItems = new ObservableCollection<MenuItemViewModel>
                    {
                        new MenuItemViewModel("New"),
                        new MenuItemViewModel("Open"),
                        new MenuItemViewModel("Close"),
                    }
                },
                new MenuItemViewModel("Edit")
                {
                    MenuItems = new ObservableCollection<MenuItemViewModel>
                    {
                        new MenuItemViewModel("Cut"),
                        new MenuItemViewModel("Copy"),
                        new MenuItemViewModel("Paste"),
                    }
                },

            };

        }
    }
</code>```

the problem is that I see the first hierarchy - "File" and "Edit"
but not their children...

In snoop I can see they have children.

And if I remove this line:

```<code><Setter Property="Template" Value="{StaticResource MenuItemControlTemplate1}" />
</code>```

it works also as expected...
The problem is that I do need to set template for the menu item...

答:
Your custom template has nothing in it to display the child items. Any time you want to create a custom control template, you should look up the example template first to make sure you aren't missing anything important.

In the example template, you will notice it contains this this:

```<code><StackPanel
    IsItemsHost="True"
    KeyboardNavigation.DirectionalNavigation="Cycle" />
</code>```

Any ItemsControl (which MenuItem extends) needs a Panel element to act as its  ItemsHost. So, add this element somewhere in your template.

If all else fails, try using the example template exactly as-is. Then, you can play with it to understand it better.

Title: Custom menu content

Body:
I'm trying to build a custom menu (meaning I want to set the template for the menu item)

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding Source={StaticResource Locator}, Path=Main}">
    <Window.Resources>
        <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
            <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <Grid VerticalAlignment="Center">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
                    <Path x:Name="GlyphPanel" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="#FF212121" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/>
                    <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="Icon" Value="{x:Null}">
                    <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
                    <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter Property="Background" TargetName="templateRoot" Value="#3D26A0DA"/>
                    <Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
                    <Setter Property="Fill" TargetName="GlyphPanel" Value="#FF707070"/>
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsHighlighted" Value="True"/>
                        <Condition Property="IsEnabled" Value="False"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" TargetName="templateRoot" Value="#0A000000"/>
                    <Setter Property="BorderBrush" TargetName="templateRoot" Value="#21000000"/>
                </MultiTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:MenuItemViewModel}"
                                  ItemsSource="{Binding MenuItems}"
                                  >
            <TextBlock Text="{Binding Caption}" />
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <Menu ItemsSource="{Binding MenuItems}"
              >
            <Menu.ItemContainerStyle>

            </Menu.ItemContainerStyle>
        </Menu>
    </Grid>
</Window>

and the view model

public class MainViewModel : ViewModelBase
    {
        public ObservableCollection<MenuItemViewModel> MenuItems { get; set; }

        public MainViewModel()
        {
            MenuItems = new ObservableCollection<MenuItemViewModel>
            {
                new MenuItemViewModel("File")
                {
                    MenuItems = new ObservableCollection<MenuItemViewModel>
                    {
                        new MenuItemViewModel("New"),
                        new MenuItemViewModel("Open"),
                        new MenuItemViewModel("Close"),
                    }
                },
                new MenuItemViewModel("Edit")
                {
                    MenuItems = new ObservableCollection<MenuItemViewModel>
                    {
                        new MenuItemViewModel("Cut"),
                        new MenuItemViewModel("Copy"),
                        new MenuItemViewModel("Paste"),
                    }
                },

            };

        }
    }

the problem is that I see the first hierarchy - "File" and "Edit"
but not their children...

In snoop I can see they have children.

And if I remove this line:

<Setter Property="Template" Value="{StaticResource MenuItemControlTemplate1}" />

it works also as expected...
The problem is that I do need to set template for the menu item...

Body:
Your custom template has nothing in it to display the child items. Any time you want to create a custom control template, you should look up the example template first to make sure you aren't missing anything important.

In the example template, you will notice it contains this this:

<StackPanel
    IsItemsHost="True"
    KeyboardNavigation.DirectionalNavigation="Cycle" />

Any ItemsControl (which MenuItem extends) needs a Panel element to act as its  ItemsHost. So, add this element somewhere in your template.

If all else fails, try using the example template exactly as-is. Then, you can play with it to understand it better.


问:
Recursion issue when writing a "Pretext" aware version of Lens.para
I've been trying to build a replacement for Lens.para that provides lensed contexts to the para function as it does its work. However, I seem to have made an error in the recursion somewhere.

According to my understanding of it, Lens.para is a paramorphism function over values in a recursive algebraic data type. That is, it uses plated and takes a function that explodes an options list to be used for traversing across the "self-similar syntax space" of a piece of data, all the while making its traversal data-context available to the function as it does its work. Its type is Lens.Plated a => (a -> [r] -> r) -> a -> r, where [r] is the list of data-context values, and a is the type of each value which plated knows how to "look into" successive levels of.

The extremely simple toy example data type I'm using to proof-of-concept this is as follows:

data EExp a
  = ELit a
  | EAdd (EExp a) (EExp a)
  deriving (Show, Eq)

So, here is my code, including both the existing working version of showOptions and my new version of it, showOptions' which uses my custom Lens.para which is called paraApp. The difference is that this one passes a Pretext along with the data as it does its work so that later I can adjust my code to make use of this Pretext to adjust the original data structure if need be.

```<code>{-# LANGUAGE RankNTypes, TemplateHaskell, ExplicitForAll, DeriveDataTypeable, StandaloneDeriving #-}

module StepThree where

import qualified Control.Lens as Lens
import qualified Data.Data as DD
import qualified Data.Data.Lens as DDL
import qualified Data.Maybe as DM
import qualified Data.List as DL
import Text.Read (readMaybe)
import StepThreeGrammar (EExp(..), pretty, run)

import Control.Comonad.Store.Class (pos, peek, ComonadStore)
import Control.Lens.Internal.Context (Pretext(..), sell)

import qualified Language.Haskell.Interpreter as I
import Language.Haskell.Interpreter (Interpreter, GhcError(..), InterpreterError(..))

instance DD.Data a => Lens.Plated (EExp a)
deriving instance DD.Data a => DD.Data (EExp a)

eg3' :: EExp Int
eg3' = EAdd (EAdd (EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)) (ELit 5)) (ELit 0)

showOptions :: (Lens.Plated a, Show a) => (a -> String) -> a -> [String]
showOptions showFn = Lens.para $ \a xs ->
    let
      sa = showFn a
      (_,is) = DL.mapAccumL mapAccumFn (0, sa) xs
    in
      sa : concat is
  where
    mapAccumFn (n, acc) x =
      let
        i = pfxIndex (head x) acc
      in
        ( (n+i+length (head x)
          , drop (i+length (head x)) acc)
        , map (replicate (n+i) ' ' ++) x)

showOptions' :: (Lens.Plated a, Show a) => (a -> String) -> a -> [String]
showOptions' showFn = paraApp $ \(a, ctx) xs ->
    let
      sa = showFn a
      (_, is) = DL.mapAccumL mapAccumFn (0, sa) xs
    in
      sa : concat is
  where
    mapAccumFn (n, acc) x =
      let
        i = pfxIndex (head x) acc
      in
        ( (n+i+length (head x)
          , drop (i+length (head x)) acc)
        , map (replicate (n+i) ' ' ++) x)

paraApp :: Lens.Plated a => ((a, Pretext (->) a a a) -> [r] -> r) -> a -> r
paraApp f x = go id (x, makePretextFocussingOnSelfFor x)
  where
    go p a =
      let p' = Lens.plate . p
          holes = Lens.holesOf p' x
      in f a (go p' <$> (map (\c -> (pos c, c)) holes))
    makePretextFocussingOnSelfFor x = Pretext ($ x)

pfxIndex :: Eq a => [a] -> [a] -> Int
pfxIndex x y = maybe 0 id (DL.findIndex (x `DL.isPrefixOf`) (DL.tails y))
</code>```

If I go into GHCi and execute the following code, it provides the intended output:

```<code>*Main EditorTest StepThree Control.Lens> mapM_ putStrLn $ StepThree.showOptions show eg3'
EAdd (EAdd (EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)) (ELit 5)) (ELit 0)
      EAdd (EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)) (ELit 5)
            EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)
                  EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)
                        EAdd (ELit 11) (ELit 9)
                              ELit 11
                                        ELit 9
                                                  ELit 3
                                                            ELit 1
                                                                      ELit 5
                                                                                ELit 0
</code>```

Which is fine for the case when I don't want to do anything with a context (say updating a particular piece of the original value)

So when I attempt the replacement function, the following happens (it should be identical to the above):

```<code>    *Main EditorTest StepThree Control.Lens> mapM_ putStrLn $ StepThree.showOptions' show eg3'
    EAdd (EAdd (EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)) (ELit 5)) (ELit 0)
          EAdd (EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)) (ELit 5)
                EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)
                      EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)
                            EAdd (ELit 11) (ELit 9)
                                  ELit 11
                                            ELit 9
                                                      ELit 3
                                                      ELit 11
                                                             ELit 9
                                                                ELit 1
                                                                EAdd (ELit 11) (ELit 9)
                                                                      ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                          ELit 5
                                                                          EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 1
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 0
                                                                                EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)
                                                                                EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 1
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 5
                                                                                EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 1
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
</code>```

Obviously I have my recursion wrong somewhere, but I can't work it out. As always, any help would be greatly appreciated.

If you're not familiar with the original definition of Lens.para, it can be found at https://hackage.haskell.org/package/lens-4.15.2/docs/src/Control.Lens.Plated.html#para

答:
This has taken me on a very interesting journey which I'm still on. I'm pretty sure the answer lies in creating a new function that merges the functionality of Lens.paraOf plate with that of Lens.contexts. At least I know the problem now and much more about Context and recursion schemes. I'd suggest anyone who is interested in writing this function should look into the source of those.

So, to answer the question, the error in the recursion lies in the fact that I'm using fmap (<$>) to map every single upper lens over every single child in the lower part of structure. That means that each subtree, rather than only getting the recursion into its particular part of the tree, is getting the full recursion into every part of the tree.

A correct implementation would take this into consideration.

Body:
I've been trying to build a replacement for Lens.para that provides lensed contexts to the para function as it does its work. However, I seem to have made an error in the recursion somewhere.

According to my understanding of it, Lens.para is a paramorphism function over values in a recursive algebraic data type. That is, it uses plated and takes a function that explodes an options list to be used for traversing across the "self-similar syntax space" of a piece of data, all the while making its traversal data-context available to the function as it does its work. Its type is Lens.Plated a => (a -> [r] -> r) -> a -> r, where [r] is the list of data-context values, and a is the type of each value which plated knows how to "look into" successive levels of.

The extremely simple toy example data type I'm using to proof-of-concept this is as follows:

data EExp a
  = ELit a
  | EAdd (EExp a) (EExp a)
  deriving (Show, Eq)

So, here is my code, including both the existing working version of showOptions and my new version of it, showOptions' which uses my custom Lens.para which is called paraApp. The difference is that this one passes a Pretext along with the data as it does its work so that later I can adjust my code to make use of this Pretext to adjust the original data structure if need be.

{-# LANGUAGE RankNTypes, TemplateHaskell, ExplicitForAll, DeriveDataTypeable, StandaloneDeriving #-}

module StepThree where

import qualified Control.Lens as Lens
import qualified Data.Data as DD
import qualified Data.Data.Lens as DDL
import qualified Data.Maybe as DM
import qualified Data.List as DL
import Text.Read (readMaybe)
import StepThreeGrammar (EExp(..), pretty, run)

import Control.Comonad.Store.Class (pos, peek, ComonadStore)
import Control.Lens.Internal.Context (Pretext(..), sell)

import qualified Language.Haskell.Interpreter as I
import Language.Haskell.Interpreter (Interpreter, GhcError(..), InterpreterError(..))

instance DD.Data a => Lens.Plated (EExp a)
deriving instance DD.Data a => DD.Data (EExp a)

eg3' :: EExp Int
eg3' = EAdd (EAdd (EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)) (ELit 5)) (ELit 0)

showOptions :: (Lens.Plated a, Show a) => (a -> String) -> a -> [String]
showOptions showFn = Lens.para $ \a xs ->
    let
      sa = showFn a
      (_,is) = DL.mapAccumL mapAccumFn (0, sa) xs
    in
      sa : concat is
  where
    mapAccumFn (n, acc) x =
      let
        i = pfxIndex (head x) acc
      in
        ( (n+i+length (head x)
          , drop (i+length (head x)) acc)
        , map (replicate (n+i) ' ' ++) x)

showOptions' :: (Lens.Plated a, Show a) => (a -> String) -> a -> [String]
showOptions' showFn = paraApp $ \(a, ctx) xs ->
    let
      sa = showFn a
      (_, is) = DL.mapAccumL mapAccumFn (0, sa) xs
    in
      sa : concat is
  where
    mapAccumFn (n, acc) x =
      let
        i = pfxIndex (head x) acc
      in
        ( (n+i+length (head x)
          , drop (i+length (head x)) acc)
        , map (replicate (n+i) ' ' ++) x)

paraApp :: Lens.Plated a => ((a, Pretext (->) a a a) -> [r] -> r) -> a -> r
paraApp f x = go id (x, makePretextFocussingOnSelfFor x)
  where
    go p a =
      let p' = Lens.plate . p
          holes = Lens.holesOf p' x
      in f a (go p' <$> (map (\c -> (pos c, c)) holes))
    makePretextFocussingOnSelfFor x = Pretext ($ x)

pfxIndex :: Eq a => [a] -> [a] -> Int
pfxIndex x y = maybe 0 id (DL.findIndex (x `DL.isPrefixOf`) (DL.tails y))

If I go into GHCi and execute the following code, it provides the intended output:

*Main EditorTest StepThree Control.Lens> mapM_ putStrLn $ StepThree.showOptions show eg3'
EAdd (EAdd (EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)) (ELit 5)) (ELit 0)
      EAdd (EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)) (ELit 5)
            EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)
                  EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)
                        EAdd (ELit 11) (ELit 9)
                              ELit 11
                                        ELit 9
                                                  ELit 3
                                                            ELit 1
                                                                      ELit 5
                                                                                ELit 0

Which is fine for the case when I don't want to do anything with a context (say updating a particular piece of the original value)

So when I attempt the replacement function, the following happens (it should be identical to the above):

    *Main EditorTest StepThree Control.Lens> mapM_ putStrLn $ StepThree.showOptions' show eg3'
    EAdd (EAdd (EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)) (ELit 5)) (ELit 0)
          EAdd (EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)) (ELit 5)
                EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)
                      EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)
                            EAdd (ELit 11) (ELit 9)
                                  ELit 11
                                            ELit 9
                                                      ELit 3
                                                      ELit 11
                                                             ELit 9
                                                                ELit 1
                                                                EAdd (ELit 11) (ELit 9)
                                                                      ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                          ELit 5
                                                                          EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 1
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 0
                                                                                EAdd (EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)) (ELit 1)
                                                                                EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 1
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 5
                                                                                EAdd (EAdd (ELit 11) (ELit 9)) (ELit 3)
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 1
                                                                                EAdd (ELit 11) (ELit 9)
                                                                                ELit 11
                                                                                ELit 9
                                                                                ELit 3
                                                                                ELit 11
                                                                                ELit 9

Obviously I have my recursion wrong somewhere, but I can't work it out. As always, any help would be greatly appreciated.

If you're not familiar with the original definition of Lens.para, it can be found at https://hackage.haskell.org/package/lens-4.15.2/docs/src/Control.Lens.Plated.html#para


问:
How to bind a click event to the document so that it fires only when no object is clicked
I would like an event to fire whenever something other than a DOM element is clicked, and a separate event when an image is clicked.

Right now I have:

```<code>$( document ).click( function() { /*do whatev*/ } );
</code>```

and in another place:

```<code>$( "img" ).click( function( e ) {
  e.stopPropagation();
  /*do whatev*/
} );
</code>```

it does not work. both events are fired. any other ideas?

答:
Simple and concise:

```<code>jQuery(document).click(function(event) {
  if (jQuery(event.target).is('img'))
  {
    alert('img');
  }
  else
  {
    // Reject event
    return false;
  }
});
</code>```

If the user clicks on an img element 'img' is alerted, otherwise the click event is stopped.

Body:
I would like an event to fire whenever something other than a DOM element is clicked, and a separate event when an image is clicked.

Right now I have:

$( document ).click( function() { /*do whatev*/ } );

and in another place:

$( "img" ).click( function( e ) {
  e.stopPropagation();
  /*do whatev*/
} );

it does not work. both events are fired. any other ideas?

Body:
Simple and concise:

jQuery(document).click(function(event) {
  if (jQuery(event.target).is('img'))
  {
    alert('img');
  }
  else
  {
    // Reject event
    return false;
  }
});

If the user clicks on an img element 'img' is alerted, otherwise the click event is stopped.


问:
How to get the LDAP payload size from wireshark trace
i have a *.pcap file with some LDAP traffic in it.
How can i figure out how many bytes the payload (only the payload) has?

BR
Manu

答:
According to : https://ask.wireshark.org/questions/12431/how-to-add-data-length-column-in-wireshark-display-or-plot-payload-length-vs-packet-no

You can do that with tshark :

payload bytes

```<code>tshark -r input.cap -T fields -e frame.number -e tcp.data -E header=y -E separator=;
</code>```

payload length

```<code>tshark -r input.cap -T fields -e frame.number -e frame.len -e ip.len -e tcp.len  -E header=y -E separator=;
</code>```

Body:
According to : https://ask.wireshark.org/questions/12431/how-to-add-data-length-column-in-wireshark-display-or-plot-payload-length-vs-packet-no

You can do that with tshark :

payload bytes

tshark -r input.cap -T fields -e frame.number -e tcp.data -E header=y -E separator=;

payload length

tshark -r input.cap -T fields -e frame.number -e frame.len -e ip.len -e tcp.len  -E header=y -E separator=;


问:
Is it possible to limit use of 3rd party libraries within own solution?
Is there a way to limit the use of 3rd party libraries such as NewtonSoft or AutoMapper when you want to add them to your own widely used libraries?

E.g. Let's say we have a class library that several other projects use for some arbitrary thing, this library in turn uses another 3rd party library.
This will of course require all other projects that -only- want to use our own project to also reference NewtonSoft or AutoMapper whether it needs it or not.

Is there a way to build around this somehow (other than the obvious part, not using 3rd party libraries)?

EDIT

Let's say I have a web project A, that need to reference my business logic in a class library B, the class library uses AutoMapper, I would like a way to not have to reference AutoMapper in project A. I believe this is not possible but if it is I would like to know how.

It would solve issues like if project A and C use different versions of the same library it would conflict, I know this can get sorted in webconfig by explicitly stating which version to use. But if there is a cleaner way I would like to know how.

EDIT # 2

What if I marked the area that uses AutoMapper in class library B as internal for instance? there would be no need for a reference to AutoMapper in project A, right?

Could that work?

答:
You could split up your library. One part (A) uses Newtonsoft and AutoMapper, the other part (B) does not.

Probably A will have a dependency to B.

All other projects can decide if to use A or B. If they requiere functionality based on Newtonsoft and AutoMapper they will choose A.


问:
How to manage hardcoded (VBA) EU date formats in Access when system locality is US
This is probably a shot in the dark and a little odd but here it goes.. We use an Access 2003 application built by a 3rd party.  It's REALLY horribly written but it calculates behavior measures with tons of scientific research and validation behind it.  This means that we CANNOT modify the application whatsoever.  To be clear we have requested even SMALL changes to be made or to allow us to make and we were denied (changing s to z in wording to be more US format)... based on the grounds that the application is validated and that any change in the application would make the outcome data void per that validation.

The above is important because we CANNOT make any direct changes to any asset inside the MDB file.  Any solutions MUST be a change in the behavior of Access and not a system wide change like changing locality.  Maybe a command line switch or something.

The problem exists that this application was written in a EU country and therefore conforms to EU locality standards hard coded into the VBA.  If there were a command line switch to force EU locality on Access during a single session this would be a great solution... I cannot find one however.

This issue only exposes itself with DATES in this application.

There is an access form that is used whenever date entry is required.  It pops up and requests month by NAME, day and year each it it's own field.  For example a birth date.  Since these fields are filled in separately the VBA code then concatenates them into a date... using EU format only..  Publisher did not take locality into consideration at all. So it places DAY FIRST instead of Month as the US format does.

This is a problem because the date conversions that Microsoft uses apparently doesn't consider EU formatted dates as invalid US dates and will happily convert them..  WRONG 30% of the time.

So what occurs is that a date of May 10, 2000 becomes October 10, 2000 because the date is converted to "DD/MM/YYYY' or 5/10/2000 becomes 10/5/2000.  10/5/2000 is a valid US formatted date but it's not the date entered by the user.  MS considers this a proper date for US, since technically it is,  but does not consider this an error in our date when a number above 12 is placed into the MONTH position.  13/5/2000 is not invalid when MS coverts it.. since there are only 12 months the MS algorithm apparently simply just identifies the date as EU format and converts it to US format... So, 13/5/2000 becomes May 13, 2000 even tho 13 is in a position that should be invalid for US locality but it is turned into the day of month and the 5 is turned into the Month number.  These types of dates come out correct because of this.. 5/13/2000 becomes 13/5/2000 but MS converts it back to 5/13/2000 however 5/10/2000 becomes 10/5/2000 and MS does NOT convert it back to 5/10/2000.

Hope you've followed that so far.

So, as I said, I need a solution that does not involve changing the application (forms, queries, vba or anything else because legally and scientifically we cannot) and does not involve changing system settings like locality.  Any solution needs to affect access only and also only affect it during the use of this single database ... ie: command line switch.

答:
We use an Access 2003 application built by a 3rd party. It's REALLY horribly written but it calculates behavior measures with tons of scientific research and validation behind it. This means that we CANNOT modify the application whatsoever.

Not. This means you have a crying need for unit tests. Make a backup copy, and then cover all the complex scientific functions with a thorough test suite (Rubberduck could help with that).

Once you have a proper test suite with green tests that thoroughly validate that the code does what it's supposed to be doing, you are free to make all the changes you want, including refactoring the complex logic if you need to: if you've covered all execution paths then every test will either remain green meaning the code still does what it was written for, or some will turn red meaning you made a change that modified something that shouldn't have been modified. With each test making one single assertion, a single red test means you know exactly what broke, where, and how.

"The application is validated" - I call BS. If there are no unit tests, there is no validation. Someone ran the code, tried various inputs, and rubberstamped [APPROVED]. That's no validation. Heck, that's no testing. Not useful testing anyway. Useful tests specifically enable making changes to an application, because they validate that the code does what it does the way it should do it.

Read Working Effectively With Legacy Code, by Michael C. Feathers. Locking down a critical application that's badly written just because it's critical, is outright dumb.

If you can't add proper unit tests, and can't modify anything whatsoever, then don't bother reporting bugs: that "validated" program is treating its bugs as features - it works that [broken] way because that's how it is and there's nothing you can do about it.


问:
What improvements to do before deploying website?
I am currently working on my first small comercial projects in web development.

Performance is always crucial, so I would like to know what steps should I follow before putting my website on the server to make sure the performance is just the best it can be.

I have heard there are some tools which rename all variables in a project to one letter variables and also delete new line characters, so that only the minimum of data is sent via the Internet and that improves performance

I like to create smaller files as some modules and then put them in  index.php like this:

```<code><html>
  <head>
    <?php include index_head.php ?> // here
  </head>
  <body>
    <nav>
    <?php include index_nav.html ?> // and here
    </nav>
  // and so on...
</code>```

what that gives is of course you write the code once and then link it where you want... Does such use of include change performance?

Any other suggestions?

答:
I have heard there are some tools which rename all variables in a project to one letter variables and also delete new line characters, so that only the minimum of data is sent via the Internet and that improves performance

That's called "minifying" (making minimum) and can yield significant performance gains.  We do that where I work as an automated build step.  Definitely take that step.  There are numerous tools available for you to use.  Google "minify" or "minifier."

Body:
I am currently working on my first small comercial projects in web development.

Performance is always crucial, so I would like to know what steps should I follow before putting my website on the server to make sure the performance is just the best it can be.

I have heard there are some tools which rename all variables in a project to one letter variables and also delete new line characters, so that only the minimum of data is sent via the Internet and that improves performance

I like to create smaller files as some modules and then put them in  index.php like this:

    <?php include index_head.php ?> // here

    <?php include index_nav.html ?> // and here

  // and so on...

what that gives is of course you write the code once and then link it where you want... Does such use of include change performance?

Any other suggestions?


问:
How to download a file from a server using Java Socket?
I have an assignment about uploading and downloading a file to a server. I managed to do the uploading part using Java Sockets however I am having a hard time doing the downloading part. I should use Range: for downloading parellel. In my request, I should have the Range: header. But I don't understand how I will receive the file with that HTTP GET request. All the examples I have seen was about uploading a file. I already did it. I can upload .exe, image, .pdf, anything and when I download them back (by my browser), there are no errors. Can you help me with the downloading part? Can you give me an example beacuse I really didn't get it.</p>

答:
This doesn't directly answer your question, but it is (IMO) worth saying anyway ...

If your homework assignment doesn't specifically tell you to use a socket directly, there are simpler, and better ways of doing HTTP file upload and download in Java:

Using java.net.URL.openConnection() on an "http:" url will give you an HttpURLConnection that you can use to make GET, PUT, POST and so on requests to the remote server.  This takes care of the basic HTTP protocol stuff for you.

The Apache HttpClient libraries do the same thing, but in a more sophisticated way, with more options and more hooks for things like handling content (including forms and MIME multiparts), connection and credential management, proxying and route finding and so on.

If the aim of your homework exercise is to teach you practical ways to talk to remote servers, then using these classes is far more practical than trying to implement a subset of the HTTP protocol from the socket level up.

(Of course, the aim could be to give you a deeper understanding of the HTTP protocol at the "wire" level ... which would make your current approach the right one.)


问:
Android AsyncTask downloading .mp3 file - info and thumbnail
I am trying to download a mp3 file from a server.
I successfully made it, and I can play the file, but there is no thumbnail and file info, such as artist and album names.

When I'm downloading the file directly from chrome, the information I stated above, DOES get downloaded.
Any ideas how to get it work?

Here is my code:

```<code>            URL url = new URL(urlDwn+urlVid);

            HttpURLConnection connection =(HttpURLConnection) url.openConnection();
            connection.connect();

            //file length
            int lengthOfFile = connection.getContentLength();
            //intput - read file
            InputStream inputStream = new BufferedInputStream(url.openStream(), 8192);

            //output - write file
            if(true) {
                System.out.println("Downloading");
                OutputStream outputStream = new FileOutputStream(root+"/"+fileName);
                System.out.println(root+"/"+fileName);
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = inputStream.read(data)) != -1) {
                    total += count;
                    //progress
                    publishProgress((int) ((total * 100) / lengthOfFile));

                    //writing to file
                    outputStream.write(data,0, count);
                }
                outputStream.flush();
                outputStream.close();
                inputStream.close();
</code>```

答:
I SOLVED IT!

it seems that the thumbnail and mp3 tag(artist and etc) were downloaded properly. the problem was in the file manager itself(or maybe the Android system), so when I restarted the device, it shown itself!

EDIT

There is no need to reboot. All you need to do is to add the lines below to the onPostExecute method, which tell the system that a new file has been added.

```<code>Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
</code>```

Body:
I am trying to download a mp3 file from a server.
I successfully made it, and I can play the file, but there is no thumbnail and file info, such as artist and album names.

When I'm downloading the file directly from chrome, the information I stated above, DOES get downloaded.
Any ideas how to get it work?

Here is my code:

            URL url = new URL(urlDwn+urlVid);

            HttpURLConnection connection =(HttpURLConnection) url.openConnection();
            connection.connect();

            //file length
            int lengthOfFile = connection.getContentLength();
            //intput - read file
            InputStream inputStream = new BufferedInputStream(url.openStream(), 8192);

            //output - write file
            if(true) {
                System.out.println("Downloading");
                OutputStream outputStream = new FileOutputStream(root+"/"+fileName);
                System.out.println(root+"/"+fileName);
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = inputStream.read(data)) != -1) {
                    total += count;
                    //progress
                    publishProgress((int) ((total * 100) / lengthOfFile));

                    //writing to file
                    outputStream.write(data,0, count);
                }
                outputStream.flush();
                outputStream.close();
                inputStream.close();

Body:
I SOLVED IT!

it seems that the thumbnail and mp3 tag(artist and etc) were downloaded properly. the problem was in the file manager itself(or maybe the Android system), so when I restarted the device, it shown itself!

EDIT

There is no need to reboot. All you need to do is to add the lines below to the onPostExecute method, which tell the system that a new file has been added.

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);


问:
AWS Firehose data transformation concurrency limits
I have a use case in which I have to collect thousands of records per second from different producers and push them to elastic search using AWS firehose. I am also using a data transformation lambda on the firehose which does a fair amount of computation before passing the records back to the firehose.

Firehose is supposed to invoke lambda with each buffered batch asynchronously before the data is buffered again to be delivered to the destination.

I ran a basic test with ingress rate of 4k records per second for 15 minutes and here is how the system responded.

Firehose CloudWatch Metrics

Lambda CloudWatch Metrics

Looking at the firehose metrics, it is obvious that it took the firehose more than an hour to process all the incoming events. As there isn't any lambda throttles (figure 2), So I am wondering why Firehose didn't run as many lambdas as possible to be able to keep up with the input rate?

As shown in the second figure, I have around 30 lambda invocations per minute and the average processing duration is 8000ms.

So I am wondering if firehose runs lambda concurrently? and is there any firehose-lambda concurrency limits that I am missing?

答:
Each Lambda eventsource will in principle be associated with 1 Lambda instance. This is to ensure correct ordering of data getting processed.

If you want to increase the throughput at the expense of not having a guarantee that the data will have correct ordering, you can use "InvokeAsync" from inside the lambda function (passing the same arguments). This way you call the lambda a second time, this time asynchronous, which will increase the throughput as expected.

A blog post detailing a similar issue can be found here: https://medium.com/retailmenot-engineering/building-a-high-throughput-data-pipeline-with-kinesis-lambda-and-dynamodb-7d78e992a02d


答:
Just try this

```<code>$object='';
$object=json_decode(json_encode($object,true),true);
$result=array_map(function ($array){
    $time = strtotime($array['departure_time']);
    if(($array['filed_departuretime'] - $time) <= 3600){
        return $array;
    }
},$object['FlightInfoExResult']['flights']);
$result=array_filter($result);
</code>```

In place of $object put your object variable name.

I thing it will help you.

Title: php array_filter if time is within 1 hour

Body:
I am trying to figure out how to filter an object based on a comparison.

I get an object that looks something like this, although with 15 results....

stdClass Object
(
    [FlightInfoExResult] => stdClass Object
        (
            [next_offset] => 15
            [flights] => Array
                (
                    [0] => stdClass Object
                        (
                            [faFlightID] => SWA2078-1499232401-airline-0885
                            [ident] => SWA2078
                            [aircrafttype] => B738
                            [filed_ete] => 03:00:00
                            [filed_time] => 1499232401
                            [filed_departuretime] => 1499477700
                            [filed_airspeed_kts] => 423
                            [filed_airspeed_mach] =>
                            [filed_altitude] => 0
                            [route] =>
                            [actualdeparturetime] => 0
                            [estimatedarrivaltime] => 1499489100
                            [actualarrivaltime] => 0
                            [diverted] =>
                            [origin] => KPHX
                            [destination] => KMKE
                            [originName] => Phoenix Sky Harbor Intl
                            [originCity] => Phoenix, AZ
                            [destinationName] => General Mitchell Intl

i need to filter this object so only return the [flights] where [filed_departuretime] is within 1 hour of Query result $time = strtotime($item['departure_time']);

by doing something like this:

abs($object->filed_departuretime - $time) <= 3600)

hopefully this all makes sense, thanks for your help :) I dont really know how array_filter works so this is what I have so far....

//set flight identity
$ident = $item['airline'].$item['flight_number'];
$date = date('Y-m-d H:i:s');
$time = strtotime($item['departure_time']);

//get flightaware results
$flightAwareResult = FlightInfoEx($ident);

/*$flightResult = array_filter(
  $flightAwareResult,
);*/

Body:
Just try this

$object='';
$object=json_decode(json_encode($object,true),true);
$result=array_map(function ($array){
    $time = strtotime($array['departure_time']);
    if(($array['filed_departuretime'] - $time) <= 3600){
        return $array;
    }
},$object['FlightInfoExResult']['flights']);
$result=array_filter($result);

In place of $object put your object variable name.

I thing it will help you.