问:
how to extract fields from mongo db change stream with C libbson
I have a bson_t "b" from a mongo db change stream which is printed like this by bson_as_canonical_extended_json :

```<code>{ "_id" : { "_data" : "825ECC4FEA0000002E2B022C0100296E5A10044A1DE2ECB8554397B8F
03E803FB80F1F463C5F6964003C3030313330323637000004" },
"operationType" : "update",
"clusterTime" : { "$timestamp" : { "t" : 1590448106, "i" : 46 } },
"ns" : { "db" : "test", "coll" : "my_collection" },
"documentKey" : { "_id" : "00130267" },
"updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" },
 "removedFields" : [  ] } }
</code>```

I can get to the "documentKey" fields like this :

```<code>bson_iter_t iter;
if (bson_iter_init_find(&iter, b, "documentKey"))
bson_iter_t child;
if (bson_iter_recurse(&iter, &child))
{
    while (bson_iter_next(&child))
    {
        const bson_value_t *value = bson_iter_value(&child);
        printf("documentKey sub-key %s value %s\n", bson_iter_key(&child), (char*)value->value.v_utf8.str);
    }
}
</code>```

which prints "_id" : "00130267"

how to access the fields of "updateDescription", are there any examples anywhere

this structure is not of my choosing it's from a mongo db change stream

the later answer by bauman is more rigorous, for me this works :

```<code>bson_iter_t iter1;
if (bson_iter_init_find(&iter1, b, "updateDescription")) // doc
{
    bson_iter_t child1;
    if (bson_iter_recurse(&iter1, &child1))
    {
        while (bson_iter_next(&child1))// updatedFields doc
        {
            bson_iter_t child2;
            bson_iter_recurse(&child1, &child2);
            while (bson_iter_next(&child2))
            {
                const bson_value_t *value = bson_iter_value(&child2);
                printf("updateDescription arr %s value %s\n", bson_iter_key(&child2), (char*)value->value.v_utf8.str);// key and value of the array
            }
        }
    }
}
</code>```

答:
You'll need to descend twice, first into the outer document and then into the inner document.

libbson documentation is really good.  props to the mongodb staff

here is a full example

```<code>#include <bson.h>

void descending_to_updateDescription(bson_t *b){
    bson_iter_t change_iter;
    if (bson_iter_init_find(&change_iter, b, "updateDescription"))
    {
        bson_iter_t desired_field;
        if (bson_iter_recurse(&change_iter, &desired_field))
        {
           // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
           //        (key)          ^   (value)
           // pointer is now here   |  (at the beginning of the document

           // confirm the (value) is type DOCUMENT as we expect (containing  keys, "updateFields" and "removedFields"
           if BSON_ITER_HOLDS_DOCUMENT(&change_iter){
               while (bson_iter_next(&desired_field))
               {
                   // printing the outer key (expect 2, shown below)
                   printf("%s\n", bson_iter_key(&desired_field));
                   // first loop
                   // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                   //                             (key)       ^   (value) -- another document
                   // pointer is now here   ----------------- |
                   //
                   // next loop
                   // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                   //                                                                                (key)     ^   (value) - a list
                   // pointer is now waaaaaaaaaayyyyyyyy ovvvvvveeeeerrr  here  --------------------------------------------------- |
                   // next loop
                   // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                   //                                                                                ^
                   // pointer is now waaaaaaaaaayyyyyyyy ovvvvvveeeeerrr  here  ---------------------------------------------------------- | (end of document
                   // iter_next returns false, because no other keys in the outer dict

                   // descend into the next level down
                   bson_iter_t changefields_iter;
                   // SUPER IMPORTANT, recurese from desired_field, not from the original change iter!
                   if (bson_iter_recurse(&desired_field, &changefields_iter)){
                       // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                       //                             (key)         ^   (value) - a document
                       // pointer is now here  -------------------- |
                       // expect update fields to hold a documnet, and removeFields to hold a list, skip processing remove fields
                       while (bson_iter_next(&changefields_iter)) {
                           // first loop
                           // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                           //                                               (key)  ^   (value) - a string in this example
                           // pointer is now here  --------------------------------|
                           // second  loop
                           // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                           //                                                                       (key)  ^   (value) - a string in this example
                           // pointer is now here  --------------------------------------------------------|
                           // third  loop
                           // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                           //                                                                                ^
                           // pointer is now here  ----------------------------------------------------------------------| (end of document)

                           // Example looks like we expect string values, so check for it
                           if (BSON_ITER_HOLDS_UTF8(&changefields_iter)){
                               uint32_t length; // iterating the string object can give you length, we don't need it though because we're not strncopying
                               printf(
                                       "\tKey: %s \t Value: %s\n",
                                       bson_iter_key(&changefields_iter),
                                       bson_iter_utf8(&changefields_iter, &length)
                                       );
                           }
                       }
                   }
               }
           }
        }
    }
}

int main(){
    // just setting up
    char * json_data = "{ \"_id\" : { \"_data\" : \"825ECC4FEA0000002E2B022C0100296E5A10044A1DE2ECB8554397B8F03E803FB80F1F463C5F6964003C3030313330323637000004\" }, \"operationType\" : \"update\", \"clusterTime\" : { \"$timestamp\" : { \"t\" : 1590448106, \"i\" : 46 } }, \"ns\" : { \"db\" : \"test\", \"coll\" : \"my_collection\" }, \"documentKey\" : { \"_id\" : \"00130267\" }, \"updateDescription\" : { \"updatedFields\" : { \"0x000F\" : \"25.006001\", \"0x0010\" : \"24.976000\" }, \"removedFields\" : [  ] } }" ;
    bson_error_t error;
    bson_t      *b;
    b = bson_new_from_json ((uint8_t*)json_data, -1, &error);
    descending_to_updateDescription(b);
    bson_destroy(b);
    b = NULL; // explicit nulled in case this is extended later
    return 0;
}
</code>```

this is the output

```<code>updatedFields
    Key: 0x000F      Value: 25.006001
    Key: 0x0010      Value: 24.976000
removedFields

Process finished with exit code 0
</code>```

The important fact is that second descent.

When you find and recurse into updateDescription, you need to descend again into the updatedFields/updatedFields documents as well to go print them off.

Also, if you really just want to check the "updateFields" you can always turn your explicit while loop into an implicit (library performed) while loop use dot notation and the find_descendant function. Your code looks cleaner, but you're not saving any performance by doing it, the library is doing the same loop you did in the above example

```<code>#include <bson.h>
void descending_direct_to_updateFields(bson_t *b){
    bson_iter_t change_iter;
    bson_iter_t updateFields_iter;
    // use the find_descendant function
    if (bson_iter_init(&change_iter, b) && bson_iter_find_descendant(&change_iter, "updateDescription.updatedFields", &updateFields_iter)) {
        // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
        //                                  (key)  ^   (value) - document with 2 keys
        // pointer is now here  -------------------|
        printf("%s\n", "updateDescription.updatedFields"); // just print because you looked for it
        if BSON_ITER_HOLDS_DOCUMENT(&updateFields_iter){
            bson_iter_t desired_field;
            bson_iter_recurse(&updateFields_iter, &desired_field);
            while (bson_iter_next(&desired_field)) {
                if (BSON_ITER_HOLDS_UTF8(&desired_field)) {
                    uint32_t length; // iterating the string object can give you length, we don't need it though because we're not strncopying
                    printf(
                            "\tKey: %s \t Value: %s\n",
                            bson_iter_key(&desired_field),
                            bson_iter_utf8(&desired_field, &length)
                    );
                }
            }
        }
    }
}

int main(){
    // just setting up
    char * json_data = "{ \"_id\" : { \"_data\" : \"825ECC4FEA0000002E2B022C0100296E5A10044A1DE2ECB8554397B8F03E803FB80F1F463C5F6964003C3030313330323637000004\" }, \"operationType\" : \"update\", \"clusterTime\" : { \"$timestamp\" : { \"t\" : 1590448106, \"i\" : 46 } }, \"ns\" : { \"db\" : \"test\", \"coll\" : \"my_collection\" }, \"documentKey\" : { \"_id\" : \"00130267\" }, \"updateDescription\" : { \"updatedFields\" : { \"0x000F\" : \"25.006001\", \"0x0010\" : \"24.976000\" }, \"removedFields\" : [  ] } }" ;
    bson_error_t error;
    bson_t      *b;
    b = bson_new_from_json ((uint8_t*)json_data, -1, &error);
    descending_direct_to_updateFields(b);
    bson_destroy(b);
    b = NULL; // explicit nulled in case this is extended later
    return 0;
}
</code>```

this is the output

```<code>updateDescription.updatedFields
    Key: 0x000F      Value: 25.006001
    Key: 0x0010      Value: 24.976000
</code>```

Stack overflow defaults to a ridiculously over-protective license for these answers. To the extent allowed, I place all code and words within this answer into the public domain.  Where public domain is not allowed or recognized, the code and words is licensed under the same license as libbson itself.

Body:
I have a bson_t "b" from a mongo db change stream which is printed like this by bson_as_canonical_extended_json :

{ "_id" : { "_data" : "825ECC4FEA0000002E2B022C0100296E5A10044A1DE2ECB8554397B8F
03E803FB80F1F463C5F6964003C3030313330323637000004" },
"operationType" : "update",
"clusterTime" : { "$timestamp" : { "t" : 1590448106, "i" : 46 } },
"ns" : { "db" : "test", "coll" : "my_collection" },
"documentKey" : { "_id" : "00130267" },
"updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" },
 "removedFields" : [  ] } }

I can get to the "documentKey" fields like this :

bson_iter_t iter;
if (bson_iter_init_find(&iter, b, "documentKey"))
bson_iter_t child;
if (bson_iter_recurse(&iter, &child))
{
    while (bson_iter_next(&child))
    {
        const bson_value_t *value = bson_iter_value(&child);
        printf("documentKey sub-key %s value %s\n", bson_iter_key(&child), (char*)value->value.v_utf8.str);
    }
}

which prints "_id" : "00130267"

how to access the fields of "updateDescription", are there any examples anywhere

this structure is not of my choosing it's from a mongo db change stream

the later answer by bauman is more rigorous, for me this works :

bson_iter_t iter1;
if (bson_iter_init_find(&iter1, b, "updateDescription")) // doc
{
    bson_iter_t child1;
    if (bson_iter_recurse(&iter1, &child1))
    {
        while (bson_iter_next(&child1))// updatedFields doc
        {
            bson_iter_t child2;
            bson_iter_recurse(&child1, &child2);
            while (bson_iter_next(&child2))
            {
                const bson_value_t *value = bson_iter_value(&child2);
                printf("updateDescription arr %s value %s\n", bson_iter_key(&child2), (char*)value->value.v_utf8.str);// key and value of the array
            }
        }
    }
}

Body:
You'll need to descend twice, first into the outer document and then into the inner document.

libbson documentation is really good.  props to the mongodb staff

here is a full example

#include <bson.h>

void descending_to_updateDescription(bson_t *b){
    bson_iter_t change_iter;
    if (bson_iter_init_find(&change_iter, b, "updateDescription"))
    {
        bson_iter_t desired_field;
        if (bson_iter_recurse(&change_iter, &desired_field))
        {
           // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
           //        (key)          ^   (value)
           // pointer is now here   |  (at the beginning of the document

           // confirm the (value) is type DOCUMENT as we expect (containing  keys, "updateFields" and "removedFields"
           if BSON_ITER_HOLDS_DOCUMENT(&change_iter){
               while (bson_iter_next(&desired_field))
               {
                   // printing the outer key (expect 2, shown below)
                   printf("%s\n", bson_iter_key(&desired_field));
                   // first loop
                   // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                   //                             (key)       ^   (value) -- another document
                   // pointer is now here   ----------------- |
                   //
                   // next loop
                   // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                   //                                                                                (key)     ^   (value) - a list
                   // pointer is now waaaaaaaaaayyyyyyyy ovvvvvveeeeerrr  here  --------------------------------------------------- |
                   // next loop
                   // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                   //                                                                                ^
                   // pointer is now waaaaaaaaaayyyyyyyy ovvvvvveeeeerrr  here  ---------------------------------------------------------- | (end of document
                   // iter_next returns false, because no other keys in the outer dict

                   // descend into the next level down
                   bson_iter_t changefields_iter;
                   // SUPER IMPORTANT, recurese from desired_field, not from the original change iter!
                   if (bson_iter_recurse(&desired_field, &changefields_iter)){
                       // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                       //                             (key)         ^   (value) - a document
                       // pointer is now here  -------------------- |
                       // expect update fields to hold a documnet, and removeFields to hold a list, skip processing remove fields
                       while (bson_iter_next(&changefields_iter)) {
                           // first loop
                           // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                           //                                               (key)  ^   (value) - a string in this example
                           // pointer is now here  --------------------------------|
                           // second  loop
                           // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                           //                                                                       (key)  ^   (value) - a string in this example
                           // pointer is now here  --------------------------------------------------------|
                           // third  loop
                           // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
                           //                                                                                ^
                           // pointer is now here  ----------------------------------------------------------------------| (end of document)

                           // Example looks like we expect string values, so check for it
                           if (BSON_ITER_HOLDS_UTF8(&changefields_iter)){
                               uint32_t length; // iterating the string object can give you length, we don't need it though because we're not strncopying
                               printf(
                                       "\tKey: %s \t Value: %s\n",
                                       bson_iter_key(&changefields_iter),
                                       bson_iter_utf8(&changefields_iter, &length)
                                       );
                           }
                       }
                   }
               }
           }
        }
    }
}

int main(){
    // just setting up
    char * json_data = "{ \"_id\" : { \"_data\" : \"825ECC4FEA0000002E2B022C0100296E5A10044A1DE2ECB8554397B8F03E803FB80F1F463C5F6964003C3030313330323637000004\" }, \"operationType\" : \"update\", \"clusterTime\" : { \"$timestamp\" : { \"t\" : 1590448106, \"i\" : 46 } }, \"ns\" : { \"db\" : \"test\", \"coll\" : \"my_collection\" }, \"documentKey\" : { \"_id\" : \"00130267\" }, \"updateDescription\" : { \"updatedFields\" : { \"0x000F\" : \"25.006001\", \"0x0010\" : \"24.976000\" }, \"removedFields\" : [  ] } }" ;
    bson_error_t error;
    bson_t      *b;
    b = bson_new_from_json ((uint8_t*)json_data, -1, &error);
    descending_to_updateDescription(b);
    bson_destroy(b);
    b = NULL; // explicit nulled in case this is extended later
    return 0;
}

this is the output

updatedFields
    Key: 0x000F      Value: 25.006001
    Key: 0x0010      Value: 24.976000
removedFields

Process finished with exit code 0

The important fact is that second descent.

When you find and recurse into updateDescription, you need to descend again into the updatedFields/updatedFields documents as well to go print them off.

Also, if you really just want to check the "updateFields" you can always turn your explicit while loop into an implicit (library performed) while loop use dot notation and the find_descendant function. Your code looks cleaner, but you're not saving any performance by doing it, the library is doing the same loop you did in the above example

#include <bson.h>
void descending_direct_to_updateFields(bson_t *b){
    bson_iter_t change_iter;
    bson_iter_t updateFields_iter;
    // use the find_descendant function
    if (bson_iter_init(&change_iter, b) && bson_iter_find_descendant(&change_iter, "updateDescription.updatedFields", &updateFields_iter)) {
        // "updateDescription" : { "updatedFields" : { "0x000F" : "25.006001", "0x0010" : "24.976000" }, "removedFields" : [  ] }
        //                                  (key)  ^   (value) - document with 2 keys
        // pointer is now here  -------------------|
        printf("%s\n", "updateDescription.updatedFields"); // just print because you looked for it
        if BSON_ITER_HOLDS_DOCUMENT(&updateFields_iter){
            bson_iter_t desired_field;
            bson_iter_recurse(&updateFields_iter, &desired_field);
            while (bson_iter_next(&desired_field)) {
                if (BSON_ITER_HOLDS_UTF8(&desired_field)) {
                    uint32_t length; // iterating the string object can give you length, we don't need it though because we're not strncopying
                    printf(
                            "\tKey: %s \t Value: %s\n",
                            bson_iter_key(&desired_field),
                            bson_iter_utf8(&desired_field, &length)
                    );
                }
            }
        }
    }
}

int main(){
    // just setting up
    char * json_data = "{ \"_id\" : { \"_data\" : \"825ECC4FEA0000002E2B022C0100296E5A10044A1DE2ECB8554397B8F03E803FB80F1F463C5F6964003C3030313330323637000004\" }, \"operationType\" : \"update\", \"clusterTime\" : { \"$timestamp\" : { \"t\" : 1590448106, \"i\" : 46 } }, \"ns\" : { \"db\" : \"test\", \"coll\" : \"my_collection\" }, \"documentKey\" : { \"_id\" : \"00130267\" }, \"updateDescription\" : { \"updatedFields\" : { \"0x000F\" : \"25.006001\", \"0x0010\" : \"24.976000\" }, \"removedFields\" : [  ] } }" ;
    bson_error_t error;
    bson_t      *b;
    b = bson_new_from_json ((uint8_t*)json_data, -1, &error);
    descending_direct_to_updateFields(b);
    bson_destroy(b);
    b = NULL; // explicit nulled in case this is extended later
    return 0;
}

this is the output

updateDescription.updatedFields
    Key: 0x000F      Value: 25.006001
    Key: 0x0010      Value: 24.976000

Stack overflow defaults to a ridiculously over-protective license for these answers. To the extent allowed, I place all code and words within this answer into the public domain.  Where public domain is not allowed or recognized, the code and words is licensed under the same license as libbson itself.


来源: stackexchange

问:
Parsing Google Analytics API XML Response
I cannot seem to parse out the metrics for an organic keyword. I am using simplexml_load_string to load the whole response string and then when I print_r the object I can see this:

```<code>[0] => SimpleXMLElement Object
                (
                    [id] => http://www.google.com/analytics/feeds/data?ids=ga:480&ga:keyword=cats&start-date=2011-04-10&end-date=2011-07-24
                    [updated] => 2011-07-23T17:00:00.001-07:00
                    [title] => ga:keyword=cats
                    [link] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [rel] => alternate
                                    [type] => text/html
                                    [href] => http://www.google.com/analytics
                                )

                        )

                )
</code>```

But when I look at the actual (pre-formatted) response I see some extra data that didn't survive the format:

```<code><entry gd:etag="W/"hdSFEs."" gd:kind="analytics#datarow">
<id>http://www.google.com/analytics/feeds/data?ids=ga:430&ga:keyword=cats&start-date=2011-04-10&end-date=2011-07-24</id>
<updated>2011-07-23T17:00:00.001-07:00</updated>
<title>ga:keyword=cats</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
<dxp:dimension name="ga:keyword" value="cats"/>
<dxp:metric confidenceInterval="0.0" name="ga:organicSearches" type="integer" value="2"/>
</entry>
</code>```

I need that metric value at the bottom, in this case it's two. How can I get it?

Thanks!

答:
Edit: the easy way out is this:

var_dump($object->xpath('dxp:metric'));

The problem is that simplexml has a 'gotcha' with namespaces. As kudo's to squirrel, you indeed need to use namespaces, but I have a hunch that you only posted a part of the xml response and not the whole response.

There is an excellent post about it here: http://blogs.sitepoint.com/simplexml-and-namespaces/

In order to fetch the value of the metric (and the metric itself), you can get started with this piece of code.
```<code>var_dump($object->children('<a href="http://schemas.google.com/analytics/2009" rel="nofollow noreferrer">http://schemas.google.com/analytics/2009</a>'));
```
Be aware that there are some attributes in there that are again in a different namespace.

The rest also has been answered here: Parse XML with Namespace using SimpleXML

ContentLicense: CC BY-SA 3.0

Body:
I cannot seem to parse out the metrics for an organic keyword. I am using simplexml_load_string to load the whole response string and then when I print_r the object I can see this:

[0] => SimpleXMLElement Object
                (
                    [id] => http://www.google.com/analytics/feeds/data?ids=ga:480&ga:keyword=cats&start-date=2011-04-10&end-date=2011-07-24
                    [updated] => 2011-07-23T17:00:00.001-07:00
                    [title] => ga:keyword=cats
                    [link] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [rel] => alternate
                                    [type] => text/html
                                    [href] => http://www.google.com/analytics
                                )

                        )

                )

But when I look at the actual (pre-formatted) response I see some extra data that didn't survive the format:

<entry gd:etag="W/"hdSFEs."" gd:kind="analytics#datarow">
<id>http://www.google.com/analytics/feeds/data?ids=ga:430&ga:keyword=cats&start-date=2011-04-10&end-date=2011-07-24</id>
<updated>2011-07-23T17:00:00.001-07:00</updated>
<title>ga:keyword=cats</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
<dxp:dimension name="ga:keyword" value="cats"/>
<dxp:metric confidenceInterval="0.0" name="ga:organicSearches" type="integer" value="2"/>
</entry>

I need that metric value at the bottom, in this case it's two. How can I get it?

Thanks!

Body:
Edit: the easy way out is this:

var_dump($object->xpath('dxp:metric'));

The problem is that simplexml has a 'gotcha' with namespaces. As kudo's to squirrel, you indeed need to use namespaces, but I have a hunch that you only posted a part of the xml response and not the whole response.

There is an excellent post about it here: http://blogs.sitepoint.com/simplexml-and-namespaces/

In order to fetch the value of the metric (and the metric itself), you can get started with this piece of code.

var_dump($object->children('http://schemas.google.com/analytics/2009'));

Be aware that there are some attributes in there that are again in a different namespace.

The rest also has been answered here: Parse XML with Namespace using SimpleXML


问:
Is vector.push_back(vector.back()+1) undefined behavior?
I want to append a new value to a c++ std::vector and this new value is relative to the last value in the vector. Can this code result in undefined behaviour?</p>

```<code>std::vector<int> vector;
vector.push_back(1);
vector.push_back(vector.back() + 1);
</code>```

答:
The vector isn't empty, so you're not invoking undefined behavior because of that.

The parameter to push_back is fully evaluated before the call is made, so that's not undefined behavior either.

ContentLicense: CC BY-SA 4.0

Body:
I want to append a new value to a c++ std::vector and this new value is relative to the last value in the vector. Can this code result in undefined behaviour?

std::vector<int> vector;
vector.push_back(1);
vector.push_back(vector.back() + 1);


问:
python unit test with mock for checking file path
I have following python function in 'au.py' :

```<code>import os

def resolv_conf_audit():
    ALT_PATH = "/etc/monitor/etc/resolv.conf.{}".format(os.uname()[1])
    RES_PATH = "/data/bin/resolvconf"
    if os.path.isfile(RES_PATH):

        return "PASSED", "/data/bin/resolvconf is present"

    elif os.path.isfile(ALT_PATH):
        return "PASSED", "/etc/monitor/etc/resolv.conf. is present"

    else:
        return "FAILED"
</code>```

I need to write a unit test with mock which can check the path exists or not
following is the unit test which I wrote

```<code>from au import resolv_conf_audit
import unittest
from unittest.mock import patch

class TestResolvConf(unittest.TestCase):
    @patch('os.path.isfile.ALT_PATH')
    def test_both_source_files_not(self, mock_os_is_file):
        mock_os_is_file.return_value =  False
        assert resolv_conf_audit() == "FAILED"
</code>```

but I am getting following error

```<code>AttributeError: <function isfile at 0x10bdea6a8> does not have the attribute 'ALT_PATH'
</code>```

How do I mock to check the presence of ALT_PATH and RES_PATH so that I can validate the function. In future this unit test should have the capability to mock removal some files, before writing that I am testing this simple one

答:
Mocks by definition is a way to simulate beahvior of objects. You are trying to handle a variable (ALT_PATH) inside your function.

All you need is to mock just the os.path.isfile method.

```<code>class TestResolvConf(unittest.TestCase):

    @patch('os.path.isfile')
    def test_both_source_files_not(self, mock_os_is_file):
        mock_os_is_file.return_value =  False
        assert resolv_conf_audit() == "FAILED"

    @patch('os.path.isfile')
    def test_both_source_files_exists(self, mock_os_is_file):
        mock_os_is_file.return_value =  True
        assert resolv_conf_audit() == "PASSED"
</code>```

Body:
I have following python function in 'au.py' :

import os

def resolv_conf_audit():
    ALT_PATH = "/etc/monitor/etc/resolv.conf.{}".format(os.uname()[1])
    RES_PATH = "/data/bin/resolvconf"
    if os.path.isfile(RES_PATH):

        return "PASSED", "/data/bin/resolvconf is present"

    elif os.path.isfile(ALT_PATH):
        return "PASSED", "/etc/monitor/etc/resolv.conf. is present"

    else:
        return "FAILED"

I need to write a unit test with mock which can check the path exists or not
following is the unit test which I wrote

from au import resolv_conf_audit
import unittest
from unittest.mock import patch

class TestResolvConf(unittest.TestCase):
    @patch('os.path.isfile.ALT_PATH')
    def test_both_source_files_not(self, mock_os_is_file):
        mock_os_is_file.return_value =  False
        assert resolv_conf_audit() == "FAILED"

but I am getting following error

AttributeError: <function isfile at 0x10bdea6a8> does not have the attribute 'ALT_PATH'

How do I mock to check the presence of ALT_PATH and RES_PATH so that I can validate the function. In future this unit test should have the capability to mock removal some files, before writing that I am testing this simple one

Body:
Mocks by definition is a way to simulate beahvior of objects. You are trying to handle a variable (ALT_PATH) inside your function.

All you need is to mock just the os.path.isfile method.

class TestResolvConf(unittest.TestCase):

    @patch('os.path.isfile')
    def test_both_source_files_not(self, mock_os_is_file):
        mock_os_is_file.return_value =  False
        assert resolv_conf_audit() == "FAILED"

    @patch('os.path.isfile')
    def test_both_source_files_exists(self, mock_os_is_file):
        mock_os_is_file.return_value =  True
        assert resolv_conf_audit() == "PASSED"


问:
How to display first ViewController values in Second ViewController tableview with Model in swift
I have created address model with NSObject. in first view controller i have button to push second viewcontroller.

Address Model look like this:

```<code> class ProfileModelUserAddress : NSObject, NSCoding{
var pincode : String!
var city : String!
var streetName : String!

init(fromDictionary dictionary: [String:Any]){
    pincode = dictionary["pincode"] as? String
    city = dictionary["city"] as? String
    streetName = dictionary["streetName"] as? String
}

func toDictionary() -> [String:Any]
{
    var dictionary = [String:Any]()

    if pincode != nil{
        dictionary["pincode"] = pincode
    }
    if city != nil{
        dictionary["city"] = city
    }
    if streetName != nil{
        dictionary["streetName"] = streetName
    }
    return dictionary
}

@objc required init(coder aDecoder: NSCoder)
{
    pincode = aDecoder.decodeObject(forKey: "pincode") as? String
    city = aDecoder.decodeObject(forKey: "city") as? String
    streetName = aDecoder.decodeObject(forKey: "streetName") as? String
}

@objc func encode(with aCoder: NSCoder)
{
    if pincode != nil{
        aCoder.encode(pincode, forKey: "pincode")
    }
    if city != nil{
        aCoder.encode(city, forKey: "city")
    }
    if streetName != nil{
        aCoder.encode(streetName, forKey: "streetName")
    }
}
}
</code>```

in first view controller how to add below values to model to display in second viewcontroller table view:

```<code>self.localityName = placemark.locality ?? ""
self.sublocalityName = placemark.subLocality ?? ""
self.zipName = placemark.postalCode ?? ""
</code>```

In second view controller how to display first view controller values in table view and its count

```<code>var addressModel: ProfileModelUserAddress?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return modeladdress.count // here how to add count
 }
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

     let cell: EditAddressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell
     let addr = addressModel.[indexPath.row]
     let street = addr?.streetName
     city   = addr?.city
     pincode = addr?.pincode

     cell.addressLabel.text = "\(city!) \(pincode!) \(street)"
     print("add address tableview cll \(cell.addressLabel.text)")
     return cell
 }
</code>```

Edit:

if i add like below answer city, pincode, etc.. like every field coming seperatley like below: i need all fields in cell.addressLabel.text this label

答:
If your requirement is to use a single String for each row then, here's how:

```<code>class SecondViewController: UITableViewController {

    var addressModelArray = [ProfileModelUserAddress]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.reloadData()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        addressModelArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: EditAddressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell
        let addressModel = addressModelArray[indexPath.row]
        var text = addressModel?.locality ?? ""
        text.append(addressModel?.subLocality ?? "")
        text.append(addressModel?.postalCode ?? "")
        cell.textLabel.text = text
        return cell
    }
}
</code>```

Body:
I have created address model with NSObject. in first view controller i have button to push second viewcontroller.

Address Model look like this:

 class ProfileModelUserAddress : NSObject, NSCoding{
var pincode : String!
var city : String!
var streetName : String!

init(fromDictionary dictionary: [String:Any]){
    pincode = dictionary["pincode"] as? String
    city = dictionary["city"] as? String
    streetName = dictionary["streetName"] as? String
}

func toDictionary() -> [String:Any]
{
    var dictionary = [String:Any]()

    if pincode != nil{
        dictionary["pincode"] = pincode
    }
    if city != nil{
        dictionary["city"] = city
    }
    if streetName != nil{
        dictionary["streetName"] = streetName
    }
    return dictionary
}

@objc required init(coder aDecoder: NSCoder)
{
    pincode = aDecoder.decodeObject(forKey: "pincode") as? String
    city = aDecoder.decodeObject(forKey: "city") as? String
    streetName = aDecoder.decodeObject(forKey: "streetName") as? String
}

@objc func encode(with aCoder: NSCoder)
{
    if pincode != nil{
        aCoder.encode(pincode, forKey: "pincode")
    }
    if city != nil{
        aCoder.encode(city, forKey: "city")
    }
    if streetName != nil{
        aCoder.encode(streetName, forKey: "streetName")
    }
}
}

in first view controller how to add below values to model to display in second viewcontroller table view:

self.localityName = placemark.locality ?? ""
self.sublocalityName = placemark.subLocality ?? ""
self.zipName = placemark.postalCode ?? ""

In second view controller how to display first view controller values in table view and its count

var addressModel: ProfileModelUserAddress?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return modeladdress.count // here how to add count
 }
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

     let cell: EditAddressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell
     let addr = addressModel.[indexPath.row]
     let street = addr?.streetName
     city   = addr?.city
     pincode = addr?.pincode

     cell.addressLabel.text = "\(city!) \(pincode!) \(street)"
     print("add address tableview cll \(cell.addressLabel.text)")
     return cell
 }

Edit:

if i add like below answer city, pincode, etc.. like every field coming seperatley like below: i need all fields in cell.addressLabel.text this label

Body:
If your requirement is to use a single String for each row then, here's how:

class SecondViewController: UITableViewController {

    var addressModelArray = [ProfileModelUserAddress]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.reloadData()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        addressModelArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: EditAddressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell
        let addressModel = addressModelArray[indexPath.row]
        var text = addressModel?.locality ?? ""
        text.append(addressModel?.subLocality ?? "")
        text.append(addressModel?.postalCode ?? "")
        cell.textLabel.text = text
        return cell
    }
}


问:
Get label for input field
I'm doing a validation with Jquery and need to get the $label from each element with their own label. Now the alert() gives med  [object object]. The best thing for me here is to get an alert() with all fields lined up that is not filled out. And not an alert() for each.

Here is a fiddle: http://jsfiddle.net/s7pYX/

How is this accomplished?

HTML:

```<code><div>
<label for="txtName">Name</label>
<input type="text" id="txtName" class="form-control" name="txtName">
</div>

<div>
<label for="txtEmail">E-mail</label>
<input type="text" id="txtEmail" class="form-control" name="txtEmail">
</div>
</code>```

Jquery:

```<code> $('input').each(function(){
 if ($(this).val() == '') {

 $element = $(this)

 var $label = $("label[for='"+$element.attr('id')+"']")

 alert($label)

 }

 });
</code>```

In the alert() I expect like this "You need to fill out: Name, E-mail"

答:
Improving upon the answer you could wrap it in a function:

```<code>function getLabel(name) {
    var $label = $("label[for='" + name + "']")
    return ($label.text());
}

var label = getLabel(this.id);
console.log(label);
</code>```

This makes it much easier to reuse the code.

Title: Get label for input field

Body:
I'm doing a validation with Jquery and need to get the $label from each element with their own label. Now the alert() gives med  [object object]. The best thing for me here is to get an alert() with all fields lined up that is not filled out. And not an alert() for each.

Here is a fiddle: http://jsfiddle.net/s7pYX/

How is this accomplished?

HTML:

Name
<input type="text" id="txtName" class="form-control" name="txtName">

E-mail
<input type="text" id="txtEmail" class="form-control" name="txtEmail">

Jquery:

 $('input').each(function(){
 if ($(this).val() == '') {

 $element = $(this)

 var $label = $("label[for='"+$element.attr('id')+"']")

 alert($label)

 }

 });

In the alert() I expect like this "You need to fill out: Name, E-mail"

Body:
Improving upon the answer you could wrap it in a function:

function getLabel(name) {
    var $label = $("label[for='" + name + "']")
    return ($label.text());
}

var label = getLabel(this.id);
console.log(label);

This makes it much easier to reuse the code.


问:
Schema hasn't been registered for model in Schema but not others from the same source
I am trying to populate a node express route with information from Schemas, and I keep getting this error.  What I cannot understand is that I am referencing three different fields in the same exact Schema and for some reason, I am only getting this error for one of those fields.

This is my route function where I am getting "MissingSchemaError: Schema hasn't been registered for model "completed_by_user""

```<code>// Schemas
const Transaction = require ("../models/transaction");
User              = require ("../models/user");
Ticket            = require ("../models/ticket");
Job               = require ("../models/job");
Client            = require ("../models/client");

// Functions
let numberWithCommas = require("../functions/numberWithCommas");

module.exports = function(app) {

    // =======================Tickets

// index
app.get("/tickets", function(req, res){
    Ticket.find({}).populate("created_by", "assigned_user", "completed_by_user").exec(function(err, tickets){  //This is where it happens
        if(err){
            console.log(err)
        } else {
            res.render("tickets", {tickets: tickets});
        }
    });
});
</code>```

And this is the Ticket Schema itself:

```<code>const Transaction = require ("./transaction");
User              = require ("./user");
Ticket            = require ("./ticket");
Job               = require ("./job");
Client            = require ("./client");

// =======================Ticket Schema

var ticketSchema = new mongoose.Schema({
    ticket_name: String,
    description: String,
    created_by: [{type: mongoose.Schema.Types.ObjectID, ref: "User"}],
    assigned_user: [{type: mongoose.Schema.Types.ObjectID, ref: "User"}],
    completed_by_user: [{type: mongoose.Schema.Types.ObjectID, ref: "User"}],
    due_date: {type: Date},
    completed_date: {type: Date},
    completed_description: String,
    date_added: {type: Date, default: Date.now}
});

module.exports = mongoose.model("Ticket", ticketSchema);
</code>```

I am not getting this error for "created_by" or for "assigned_user" but I am getting it for "completed_by_user."  I don't understand why this would be since they are all populated in the same Schema.  This is not the only occurrence of this type of issue I am having, but I am sure it is for the same reason.  I even tried changing the order I require the schemas and that has not helped either.  Is there something obvious I am missing?  Thanks.

答:
```<code>.populate(["created_by", "assigned_user", "completed_by_user"])
</code>```

Try this because .populate function takes only one argument by putting it into array it takes multiple

Body:
I am trying to populate a node express route with information from Schemas, and I keep getting this error.  What I cannot understand is that I am referencing three different fields in the same exact Schema and for some reason, I am only getting this error for one of those fields.

This is my route function where I am getting "MissingSchemaError: Schema hasn't been registered for model "completed_by_user""

// Schemas
const Transaction = require ("../models/transaction");
User              = require ("../models/user");
Ticket            = require ("../models/ticket");
Job               = require ("../models/job");
Client            = require ("../models/client");

// Functions
let numberWithCommas = require("../functions/numberWithCommas");

module.exports = function(app) {

    // =======================Tickets

// index
app.get("/tickets", function(req, res){
    Ticket.find({}).populate("created_by", "assigned_user", "completed_by_user").exec(function(err, tickets){  //This is where it happens
        if(err){
            console.log(err)
        } else {
            res.render("tickets", {tickets: tickets});
        }
    });
});

And this is the Ticket Schema itself:

const Transaction = require ("./transaction");
User              = require ("./user");
Ticket            = require ("./ticket");
Job               = require ("./job");
Client            = require ("./client");

// =======================Ticket Schema

var ticketSchema = new mongoose.Schema({
    ticket_name: String,
    description: String,
    created_by: [{type: mongoose.Schema.Types.ObjectID, ref: "User"}],
    assigned_user: [{type: mongoose.Schema.Types.ObjectID, ref: "User"}],
    completed_by_user: [{type: mongoose.Schema.Types.ObjectID, ref: "User"}],
    due_date: {type: Date},
    completed_date: {type: Date},
    completed_description: String,
    date_added: {type: Date, default: Date.now}
});

module.exports = mongoose.model("Ticket", ticketSchema);

I am not getting this error for "created_by" or for "assigned_user" but I am getting it for "completed_by_user."  I don't understand why this would be since they are all populated in the same Schema.  This is not the only occurrence of this type of issue I am having, but I am sure it is for the same reason.  I even tried changing the order I require the schemas and that has not helped either.  Is there something obvious I am missing?  Thanks.

Body:
.populate(["created_by", "assigned_user", "completed_by_user"])

Try this because .populate function takes only one argument by putting it into array it takes multiple


问:
power bi how to order dates in line and stacked column chart where dates are not unique
example data behind this visual is as follows:

```<code>Order Date         team  count
1      Jan-20      A     6
2      Feb-20      A     7
3      Feb-20      B     10
4      Mar-20      A     8
:
80     Dec-20      A      12
</code>```

The date is stored in text format then I have added an index so in the chart the dates are ordered on the x axis. But as the date is not unique in the data the dates are not ordered on the x axis.

How can I get the dates ordered from Jan-Dec

答:
You could create a separate sorting table. First, add a new column to existing table:

```<code>Month = LEFT([Date], 3)
</code>```

Then create a new table, let's call it SortTable:

```<code>Month | Sort
Jan   | 1
Feb   | 2
...
Dec   | 12
</code>```

Then set the SortingTable Month to sort by column Sort. Lastly, set a relation between the tables from Orders[Month] to SortTable[Month] and use SortTable[Month] on the graph x axis.

Body:
example data behind this visual is as follows:

Order Date         team  count
1      Jan-20      A     6
2      Feb-20      A     7
3      Feb-20      B     10
4      Mar-20      A     8
:
80     Dec-20      A      12

The date is stored in text format then I have added an index so in the chart the dates are ordered on the x axis. But as the date is not unique in the data the dates are not ordered on the x axis.

How can I get the dates ordered from Jan-Dec

Body:
You could create a separate sorting table. First, add a new column to existing table:

Month = LEFT([Date], 3)

Then create a new table, let's call it SortTable:

Month | Sort
Jan   | 1
Feb   | 2
...
Dec   | 12

Then set the SortingTable Month to sort by column Sort. Lastly, set a relation between the tables from Orders[Month] to SortTable[Month] and use SortTable[Month] on the graph x axis.


问:
Is there an optimal number of elements for a tfrecords file?
This is follow up to these SO questions

What is the need to do sharding of TFRecords files?

optimal size of a tfrecord file

and this passage from this tutorial

For this small dataset we will just create one TFRecords file for the
  training-set and another for the test-set. But if your dataset is very
  large then you can split it into several TFRecords files called
  shards. This will also improve the random shuffling, because the
  Dataset API only shuffles from a smaller buffer of e.g. 1024 elements
  loaded into RAM. So if you have e.g. 100 TFRecords files, then the
  randomization will be much better than for a single TFRecords file.

https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/18_TFRecords_Dataset_API.ipynb

So there is an optimal file size, but I am wondering, if there's an optimal number of elements? Since it's the elements itself that's being distributed to the GPUs cores?

答:
Are you trying to optimize:

1 initial data randomization?
2 data randomization across training batches and/or epochs?
3 training/validation throughput (ie, gpu utilization)?

Initial data randomization should be handled when data are initially saved into sharded files. This can be challenging, assuming you can't read the data into memory. One approach is to read all the unique data ids into memory, shuffle those, do your train/validate/test split, and then write your actual data to file shards in that randomized order. Now your data are initially shuffled/split/sharded.

Initial data randomization will make it easier to maintain randomization during training. However, I'd still say it is 'best practice' to re-shuffle file names and re-shuffle a data memory buffer as part of the train/validate data streams. Typically, you'll set up an input stream using multiple threads/processes. The first step is to randomize the file input streams by re-shuffling the filenames. This can be done like:

```<code>train_files = tf.data.Dataset.list_files('{}/d*.tfr'.format(train_dir),
                                            shuffle=True)
</code>```

Now, if your initial data write was already randomized, you 'could' read the entire data from one file, before going to the next, but that would still impact re-randomization throughout the training process, so typically you interleave file reads, reading a certain number of records from each file. This also improves throughput, assuming you are using multiple file read processes (which you should do, to maximize gpu throughput).

```<code>blocksize  = 1000     # samples read from one file before switching files
train_data  = train_files.interleave(interleaveFiles,
              block_length=blocksize,
              num_parallel_calls=tf.data.experimental.AUTOTUNE)
</code>```

Here, we're reading 1000 samples from each file, before going on to the next. Again, to re-shuffle the training data each epoch (which may or may not be critical), we re-shuffle the data in memory, setting a memory buffer based on what's available on the machine and how large our data items are (note - before formatting the data for gpu).

```<code>buffersize = 1000000  # samples read before shuffling in memory
train_data  = train_data.shuffle(buffersize,
                                 reshuffle_each_iteration=True)
train_data  = train_data.repeat()
</code>```

The repeat() call is just to allow the data set to 'wrap around' during training. This may or may not be important, depending on how you set up your training process.

To optimize throughput, you can do 2 things:

1 alter the order of operations in the data input stream. Typically, if you put your randomization operations early, they can operate on 'low weight' entities, like file names, rather than on tensors.
2 use pre-fetching to let your cpu processes stream data during gpu calculations

```<code>train_data  = train_data.map(mapData,
                  num_parallel_calls=tf.data.experimental.AUTOTUNE)
train_data  = train_data.padded_batch(batchsize)
train_data  = train_data.prefetch(10)
</code>```

So, mapping and batching happens last (this is usually preferred for maximizing gpu throughput, but it can depend on other factors, like data size (pre and post-tensorizing), and how computationally expensive your map function is).

Finally, you can tune the prefetch size to maximize gpu throughput, constrained by system memory and memory speed.

So, how does this all impact the 'optimal' number of data items in each sharded file?

Obviously, if your data/file size is > your blocksize, blocksize becomes irrelevant, and you might as well read each file completely. Typically, if you are going to use this paradigm, you wand blocksize << data/file. I use 10x; so if my blocksize is 1000, I have ~10,000 data items in the file. This may not be optimal, but so far I can maintain >90% gpu usage using this approach on my specific hardware. If you want to tune for your hardware, you could start somewhere at ~10x and adjust, based on whatever you are specifically trying to optimize.

If you have very large numbers of files, you may run into problems maintaining good file read streams, but on a modern system you should be able to get to 100,000 files or more and still be fine. Moving large numbers of files around can be difficult, but usually easier than having very small numbers of very big files, so there are some (broad) constraints on file sizes that can impact how many data items/file you end up with. Generally speaking, I'd say having on the order of 100s of files would be ideal for a large dataset. That way you can easily stream files across a network efficiently (again, that will depend on your network). If the data set is small, you'll have 10s to 50s of files, which is fine for streaming, depending on file size (I typically try to hit 100-300MB/file, which works well for moving things around a LAN or WAN).

So, I think file-size and number-of-files places much stronger constraints on your process than number of data items/file, so long as you have an appropriate number of data items/file, given your file read blocksize. Again, you could hyper-shard your files (1 data item/file?), and read entire files into memory, without using file blocking. That might work, and it would certainly be lightweight to shuffle file names, rather than data items. But you might also end up with millions of files!

To really optimize, you'll need to set up an end-to-end training system on a particular machine, and then tweak it to see what works best for your particular data, network, and hardware. So long as your data are effectively randomized and your data files are easy to store/use/share, you just want to optimize gpu throughput. I would be surprised if reordering the data input stream and pre-fetching doesn't get you there.

Body:
Are you trying to optimize:

1 initial data randomization?
2 data randomization across training batches and/or epochs?
3 training/validation throughput (ie, gpu utilization)?

Initial data randomization should be handled when data are initially saved into sharded files. This can be challenging, assuming you can't read the data into memory. One approach is to read all the unique data ids into memory, shuffle those, do your train/validate/test split, and then write your actual data to file shards in that randomized order. Now your data are initially shuffled/split/sharded.

Initial data randomization will make it easier to maintain randomization during training. However, I'd still say it is 'best practice' to re-shuffle file names and re-shuffle a data memory buffer as part of the train/validate data streams. Typically, you'll set up an input stream using multiple threads/processes. The first step is to randomize the file input streams by re-shuffling the filenames. This can be done like:

train_files = tf.data.Dataset.list_files('{}/d*.tfr'.format(train_dir),
                                            shuffle=True)

Now, if your initial data write was already randomized, you 'could' read the entire data from one file, before going to the next, but that would still impact re-randomization throughout the training process, so typically you interleave file reads, reading a certain number of records from each file. This also improves throughput, assuming you are using multiple file read processes (which you should do, to maximize gpu throughput).

blocksize  = 1000     # samples read from one file before switching files
train_data  = train_files.interleave(interleaveFiles,
              block_length=blocksize,
              num_parallel_calls=tf.data.experimental.AUTOTUNE)

Here, we're reading 1000 samples from each file, before going on to the next. Again, to re-shuffle the training data each epoch (which may or may not be critical), we re-shuffle the data in memory, setting a memory buffer based on what's available on the machine and how large our data items are (note - before formatting the data for gpu).

buffersize = 1000000  # samples read before shuffling in memory
train_data  = train_data.shuffle(buffersize,
                                 reshuffle_each_iteration=True)
train_data  = train_data.repeat()

The repeat() call is just to allow the data set to 'wrap around' during training. This may or may not be important, depending on how you set up your training process.

To optimize throughput, you can do 2 things:

1 alter the order of operations in the data input stream. Typically, if you put your randomization operations early, they can operate on 'low weight' entities, like file names, rather than on tensors.
2 use pre-fetching to let your cpu processes stream data during gpu calculations

train_data  = train_data.map(mapData,
                  num_parallel_calls=tf.data.experimental.AUTOTUNE)
train_data  = train_data.padded_batch(batchsize)
train_data  = train_data.prefetch(10)

So, mapping and batching happens last (this is usually preferred for maximizing gpu throughput, but it can depend on other factors, like data size (pre and post-tensorizing), and how computationally expensive your map function is).

Finally, you can tune the prefetch size to maximize gpu throughput, constrained by system memory and memory speed.

So, how does this all impact the 'optimal' number of data items in each sharded file?

Obviously, if your data/file size is > your blocksize, blocksize becomes irrelevant, and you might as well read each file completely. Typically, if you are going to use this paradigm, you wand blocksize << data/file. I use 10x; so if my blocksize is 1000, I have ~10,000 data items in the file. This may not be optimal, but so far I can maintain >90% gpu usage using this approach on my specific hardware. If you want to tune for your hardware, you could start somewhere at ~10x and adjust, based on whatever you are specifically trying to optimize.

If you have very large numbers of files, you may run into problems maintaining good file read streams, but on a modern system you should be able to get to 100,000 files or more and still be fine. Moving large numbers of files around can be difficult, but usually easier than having very small numbers of very big files, so there are some (broad) constraints on file sizes that can impact how many data items/file you end up with. Generally speaking, I'd say having on the order of 100s of files would be ideal for a large dataset. That way you can easily stream files across a network efficiently (again, that will depend on your network). If the data set is small, you'll have 10s to 50s of files, which is fine for streaming, depending on file size (I typically try to hit 100-300MB/file, which works well for moving things around a LAN or WAN).

So, I think file-size and number-of-files places much stronger constraints on your process than number of data items/file, so long as you have an appropriate number of data items/file, given your file read blocksize. Again, you could hyper-shard your files (1 data item/file?), and read entire files into memory, without using file blocking. That might work, and it would certainly be lightweight to shuffle file names, rather than data items. But you might also end up with millions of files!

To really optimize, you'll need to set up an end-to-end training system on a particular machine, and then tweak it to see what works best for your particular data, network, and hardware. So long as your data are effectively randomized and your data files are easy to store/use/share, you just want to optimize gpu throughput. I would be surprised if reordering the data input stream and pre-fetching doesn't get you there.


问:
What can make jQuery's unbind function not work as expected?
Have a look at the following code (additionally, you will need jquery.js, jquery.viewport.js and jquery.scrollTo.js).

The behaviour I would expect from this script is that whenever I scroll the page, the red rows (
 elements with class alwaysVisible) should be inserted just underneath the top-most visible row (
 element) of that table. Then, the page should be scrolled so that the first of these red rows appears "exactly" at the top of the viewport. What actually happens is that makeVisibleWhatMust(); is called repeatedly until I reach the end of the page. I thought $(window).unbind('scroll'); would keep makeVisibleWhatMust(); from being called again, but apparently this doesn't work.

Any ideas why?

Here is the JavaScript I wrote:

```<code>function makeVisibleWhatMust()
{
  $('#testContainer').text( $('#testContainer').text() + 'called\n');
  $('table.scrollTable').each
  (
    function()
    {
      var table = this;
      $($('tr.alwaysVisible', table).get().reverse()).each
      (
    function()
    {
      $(this).insertAfter( $('tr:in-viewport:not(.alwaysVisible)', table)[0] );
    }
      );
      $(window).unbind('scroll');
      $(window).scrollTo( $('tr.alwaysVisible')[0] );
      $(window).bind('scroll', makeVisibleWhatMust);
    }
  );
}

$(document).ready
(
  function()
  {
    $(window).bind('scroll', makeVisibleWhatMust);
  }
);
</code>```

And here is an HTML page to test it on:

```<code><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Scroll Tables Test Page</title>

    <script type="text/javascript" src="jQuery.js"></script>
    <script type="text/javascript" src="jquery.viewport.js"></script>
    <script type="text/javascript" src="jquery.scrollTo.js"></script>
    <script type="text/javascript" src="scrollTable.js"></script>

    <style type="text/css">
      table th, table td
      {
    border: 1px solid #000;
    padding: .3em;
      }
      .alwaysVisible
      {
    background: #F66;
      }
    </style>
  </head>
  <body>
    <table class="scrollTable">
      <thead>
    <tr class="alwaysVisible">
      <th>Row name</th>
      <th>Content</th>
    </tr>
    <tr class="alwaysVisible">
      <th>Row 2</th>
      <th>Row 2</th>
    </tr>
      </thead>
      <tbody>
    <script type="text/javascript">
      for(var i = 0; i < 50; ++i)
      {
        document.writeln("<tr><td>Row " + i + "</td><td>Content</td></tr>");
      }
    </script>
      </tbody>
      <tfoot>
    <tr>
      <td>Footer</td>
      <td>Footer 2</td>
    </tr>
      </tfoot>
    </table>
    <div id="testContainer">TEST CONTAINER</div>
  </body>
</html>
</code>```

答:
I think you're problem is that scrollTo uses animate:

```<code>// From the plugin's source
function animate( callback ){
    $elem.animate( attr, duration, settings.easing, callback && function(){
        callback.call(this, target, settings);
    });
};
</code>```

And animate uses a timer to perform the animation. The result is that .scrollTo will return before the scrolling has completed and you'll rebind your scroll handler while the scrollTo is still scrolling. Hence the events when you're not expecting them.

An easy solution would be to use a flag to tell makeVisibleWhatMust that scrollTo is scrolling and use a scrollTo callback to clear the flag when it is done, something like this:

```<code>function makeVisibleWhatMust() {
  // Ignore the event if we're doing the scrolling.
  if(makeVisibleWhatMust.isScrolling)
    return;
  $('#testContainer').text( $('#testContainer').text() + 'called\n');
  $('table.scrollTable').each(function() {
      var table = this;
      $($('tr.alwaysVisible', table).get().reverse()).each(function() {
        $(this).insertAfter( $('tr:in-viewport:not(.alwaysVisible)', table)[0] );
      });
      makeVisibleWhatMust.isScrolling = true;
      $(window).scrollTo($('tr.alwaysVisible')[0], {
        onAfter: function() { makeVisibleWhatMust.isScrolling = false; }
      });
    }
  );
}
makeVisibleWhatMust.isScrolling = false;
</code>```

And here's a live version that seems to work: http://jsfiddle.net/ambiguous/ZEx6M/1/

Body:
Have a look at the following code (additionally, you will need jquery.js, jquery.viewport.js and jquery.scrollTo.js).

The behaviour I would expect from this script is that whenever I scroll the page, the red rows (
 elements with class alwaysVisible) should be inserted just underneath the top-most visible row (
 element) of that table. Then, the page should be scrolled so that the first of these red rows appears "exactly" at the top of the viewport. What actually happens is that makeVisibleWhatMust(); is called repeatedly until I reach the end of the page. I thought $(window).unbind('scroll'); would keep makeVisibleWhatMust(); from being called again, but apparently this doesn't work.

Any ideas why?

Here is the JavaScript I wrote:

function makeVisibleWhatMust()
{
  $('#testContainer').text( $('#testContainer').text() + 'called\n');
  $('table.scrollTable').each
  (
    function()
    {
      var table = this;
      $($('tr.alwaysVisible', table).get().reverse()).each
      (
    function()
    {
      $(this).insertAfter( $('tr:in-viewport:not(.alwaysVisible)', table)[0] );
    }
      );
      $(window).unbind('scroll');
      $(window).scrollTo( $('tr.alwaysVisible')[0] );
      $(window).bind('scroll', makeVisibleWhatMust);
    }
  );
}

$(document).ready
(
  function()
  {
    $(window).bind('scroll', makeVisibleWhatMust);
  }
);

And here is an HTML page to test it on:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Scroll Tables Test Page</title>

      Row name
      Content

      Row 2
      Row 2

      Footer
      Footer 2

TEST CONTAINER

Body:
I think you're problem is that scrollTo uses animate:

// From the plugin's source
function animate( callback ){
    $elem.animate( attr, duration, settings.easing, callback && function(){
        callback.call(this, target, settings);
    });
};

And animate uses a timer to perform the animation. The result is that .scrollTo will return before the scrolling has completed and you'll rebind your scroll handler while the scrollTo is still scrolling. Hence the events when you're not expecting them.

An easy solution would be to use a flag to tell makeVisibleWhatMust that scrollTo is scrolling and use a scrollTo callback to clear the flag when it is done, something like this:

function makeVisibleWhatMust() {
  // Ignore the event if we're doing the scrolling.
  if(makeVisibleWhatMust.isScrolling)
    return;
  $('#testContainer').text( $('#testContainer').text() + 'called\n');
  $('table.scrollTable').each(function() {
      var table = this;
      $($('tr.alwaysVisible', table).get().reverse()).each(function() {
        $(this).insertAfter( $('tr:in-viewport:not(.alwaysVisible)', table)[0] );
      });
      makeVisibleWhatMust.isScrolling = true;
      $(window).scrollTo($('tr.alwaysVisible')[0], {
        onAfter: function() { makeVisibleWhatMust.isScrolling = false; }
      });
    }
  );
}
makeVisibleWhatMust.isScrolling = false;

And here's a live version that seems to work: http://jsfiddle.net/ambiguous/ZEx6M/1/


问:
Solr Server-Side Query-Preprocessing
I want solr to do a fuzzy search and also find the query as subword. Matches of complete words shall be prefered.

This works well with the following query:

http://localhost:8983/solr/select?qt=my_qt&q=*myword*~0.8+myword~0.8
�

Is there a possibility to get rid of all the wildcards and fuzzy paramters in the request.

I would love to configure solr an a way so that it does the same search as for the above query when receiving the following query:

```<code>http://localhost:8983/solr/select?qt=my_qt&q=myword
</code>```

I though of adding something like the following to the reqeustHandler in the solrconf.xml

```<code><str name="qf">title~0.8+*title*~0.8</str>
</code>```

(this does not work)

答: See Lucid Imagination's blog post. You'd probably want to just have one field be the ngram field, and the other field be normal.</p>

Title: Solr Server-Side Query-Preprocessing

Body:
I want solr to do a fuzzy search and also find the query as subword. Matches of complete words shall be prefered.

This works well with the following query:

http://localhost:8983/solr/select?qt=my_qt&q=*myword*~0.8+myword~0.8
�

Is there a possibility to get rid of all the wildcards and fuzzy paramters in the request.

I would love to configure solr an a way so that it does the same search as for the above query when receiving the following query:

http://localhost:8983/solr/select?qt=my_qt&q=myword

I though of adding something like the following to the reqeustHandler in the solrconf.xml

<str name="qf">title~0.8+*title*~0.8</str>

(this does not work)


问:
Close subnav once a different subnav is open with jQuery
Currently I have this working almost perfectly, but the user has the ability to open multiple sub-nav's at once, and I need any open sub-nav's to close when another one is open.

Basically, the jQuery that's beloe, I need to classes to be removed if a new sub-nav is clicked on, currently it only removes them if the same one is clicked.

HTML

```<code><nav class="st-menu" id="menu-4a">
    <ul>
        <li>
            <a href="#">Guidance Manual</a>
        </li>
        <li>
            <a href="#">Resource Directory</a><div class="toggle-arrow"><img src="/assets/images/chevron-up-solid.svg"/></div>
            <ul class="reg-subnav">
                <li><a href="#">Stormwater Plan Review Resources</a></li>
                <li><a href="#">Pilot Projects</a></li>
                <li><a href="#">Proprietary Products</a></li>
                <li><a href="#">Additional Resources</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Stormwater 101</a><div class="toggle-arrow"><img src="/assets/images/chevron-up-solid.svg"/></div>
            <ul class="reg-subnav">
                <li><a href="#">Regulations</a></li>
                <li><a href="#">Stormwater Management</a></li>
                <li><a href="#">Stormwater Billing & Retrofits</a></li>
                <li><a href="#">Green City, Clean Waters</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Contact Us</a><div class="toggle-arrow"><img src="/assets/images/chevron-up-solid.svg"/></div>
            <ul class="reg-subnav">
                <li><a href="#">About Us</a></li>
                <li><a href="#">Development Review Contacts</a></li>
            </ul>
        </li>
    </ul>
</nav>
</code>```

jQuery

```<code>$(".toggle-arrow").click(function(){
    $(this).closest('li').find(".reg-subnav").toggleClass('open-sub');
    $(this).closest('li').find('.arrow-image').toggleClass("flip");
});
</code>```

答:
Could be done with removeClass

$(".toggle-arrow").click(function(){
    $(".reg-subnav").not($(this).closest("li").find(".reg-subnav")).removeClass("open-sub");
    $(this).closest('li').find(".reg-subnav").toggleClass('open-sub');
    $(this).closest('li').find('.arrow-image').toggleClass("flip");
});
```

<p>And you could clean up your code even more by using the siblings selector:</p>

<pre class="lang-js prettyprint-override"><code>$(".toggle-arrow").click(function(){
    $(".reg-subnav").not($(this).siblings(".reg-subnav")).removeClass("open-sub");
    $(this).siblings(".reg-subnav").toggleClass('open-sub');
    $(this).siblings('.arrow-image').toggleClass("flip");
});
</code>```

EDIT:
Per @epascarello, This could also be made more performant by storing the subnav so it's not queried for twice:

$(".toggle-arrow").click(function(){
    var $thisSubnav = $(this).siblings(".reg-subnav");
    $(".reg-subnav").not($thisSubnav).removeClass("open-sub");
    $thisSubnav.toggleClass('open-sub');
    $(this).siblings('.arrow-image').toggleClass("flip");
});
```

Body:
Currently I have this working almost perfectly, but the user has the ability to open multiple sub-nav's at once, and I need any open sub-nav's to close when another one is open.

Basically, the jQuery that's beloe, I need to classes to be removed if a new sub-nav is clicked on, currently it only removes them if the same one is clicked.

HTML

            Guidance Manual

            Resource Directory

Stormwater Plan Review Resources

Pilot Projects

Proprietary Products

Additional Resources

            Stormwater 101

Regulations

Stormwater Management

Stormwater Billing & Retrofits

Green City, Clean Waters

            Contact Us

About Us

Development Review Contacts

jQuery

$(".toggle-arrow").click(function(){
    $(this).closest('li').find(".reg-subnav").toggleClass('open-sub');
    $(this).closest('li').find('.arrow-image').toggleClass("flip");
});

Body:
Could be done with removeClass

$(".toggle-arrow").click(function(){
    $(".reg-subnav").not($(this).closest("li").find(".reg-subnav")).removeClass("open-sub");
    $(this).closest('li').find(".reg-subnav").toggleClass('open-sub');
    $(this).closest('li').find('.arrow-image').toggleClass("flip");
});

And you could clean up your code even more by using the siblings selector:

$(".toggle-arrow").click(function(){
    $(".reg-subnav").not($(this).siblings(".reg-subnav")).removeClass("open-sub");
    $(this).siblings(".reg-subnav").toggleClass('open-sub');
    $(this).siblings('.arrow-image').toggleClass("flip");
});

EDIT:
Per @epascarello, This could also be made more performant by storing the subnav so it's not queried for twice:

$(".toggle-arrow").click(function(){
    var $thisSubnav = $(this).siblings(".reg-subnav");
    $(".reg-subnav").not($thisSubnav).removeClass("open-sub");
    $thisSubnav.toggleClass('open-sub');
    $(this).siblings('.arrow-image').toggleClass("flip");
});


问:
Flutter gesturedetector not working in stack widget
I am using the stack widget to show the back arrow button on an image. And its showing but the problem is it's not tappable mean Gesturededector is not working on Stack.

My code

```<code>  Stack(
      children: <Widget>[

        Container(
          height: height * 0.4,
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(
                'assets/images/place2.jpg',
              ),
              fit: BoxFit.fill,
            ),
          ),
        ),
        GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () => print("first container"),
          child: Align(
            alignment: Alignment.topLeft,
            child: Container(
              margin: EdgeInsets.only(left: width * 0.03),
              padding: EdgeInsets.only(top: statusBarHeight * 2),

              child: Icon(
                Icons.arrow_back_ios,
                size: 25,
                color: Colors.white,
              ),
            ),
          ),
        ),

        SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.only(top: height * 0.3),
            child: SingleChildScrollView(
              child: ClipRRect(
                borderRadius: BorderRadius.only(
                    topRight: Radius.circular(30),
                    topLeft: Radius.circular(30)),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      SizedBox(
                        height: height * 0.05,
                      ),
                      Container(
                        width: width,
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Text(
                          'NYC Food Festival',
                          textAlign: TextAlign.left,
                          style: TextStyle(fontSize: 30),
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Row(
                          children: <Widget>[
                            Icon(
                              Icons.calendar_today,
                              size: 20,
                              color: Color(0xff808080),
                            ),
                            SizedBox(width: width * 0.02), // give it width
                            Column(
                              children: <Widget>[
                                Text(
                                  'Sat, May 25, 2020',
                                  style: TextStyle(
                                      color: Color(0xff000000),
                                      fontWeight: FontWeight.bold,
                                      fontSize: 15),
                                ),
                              ],
                            )
                          ],
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Row(
                          children: <Widget>[
                            Icon(
                              Icons.attach_money,
                              size: 20,
                              color: Color(0xff808080),
                            ),
                            SizedBox(width: width * 0.02), // give it width
                            Column(
                              children: <Widget>[
                                Text(
                                  '25,000 PKR',
                                  style: TextStyle(
                                      color: Color(0xff000000),
                                      fontWeight: FontWeight.bold,
                                      fontSize: 15),
                                ),
                              ],
                            )
                          ],
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                        width: width,
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Text(
                          'Snaps',
                          textAlign: TextAlign.left,
                          style: TextStyle(fontSize: 25),
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                          child: Column(
                            children: <Widget>[
                              CarouselSlider(
                                options: CarouselOptions(
                                  autoPlay: true,
                                  aspectRatio: 2.0,
                                  enlargeCenterPage: true,
                                ),
                                items: imageSliders,
                              ),
                            ],
                          )),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                        width: width,
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Text(
                          'About',
                          textAlign: TextAlign.left,
                          style: TextStyle(fontSize: 25),
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                          padding: EdgeInsets.only(
                              right: width * 0.03, left: width * 0.03),
                          child: DescriptionTextWidget(
                              text:
                                  "Flutter is Google’s mobile UI framework for crafting high-quality native interfaces on iOS and Android in record time. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source.")
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                        width: width,
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Text(
                          'Included',
                          textAlign: TextAlign.left,
                          style: TextStyle(fontSize: 25),
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Included(),
                      SizedBox(
                        height: height * 0.01,
                      ),
                      Included(),
                      SizedBox(
                        height: height * 0.01,
                      ),
                      Included(),
                      SizedBox(
                        height: height * 0.01,
                      ),

                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Container(
            height: 100,
            decoration: BoxDecoration(
              // color: Color.fromARGB(110, 255, 255, 255),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(20),
                topRight: Radius.circular(20),
              ),
              gradient: LinearGradient(
                colors: [
                  Color.fromARGB(30, 255, 255, 255),
                  Color.fromARGB(255, 255, 255, 255),
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
              ),
            ),
            child: Center(
              child: InkWell(
                child: Container(
                  padding: EdgeInsets.symmetric(
                    horizontal: 80,
                    vertical: 20,
                  ),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(30),
                    color: Colors.blueAccent,
                  ),
                  child: Text(
                    "BOOK NOW",
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 20,
                        color: Colors.white),
                  ),
                ),
                onTap: () {
                  return print("Tap");
                },
              ),
            ),
          ),
        ),
      ],
    )
</code>```

I need to use it as a back button when I navigate but it's not working then for testing I just print a value onTap but its also not working I try to add behaviour also.

答:
Can you try putting GestureDectector after SingleChildScrollView in a stack.

I guess SingleChildScrollView is lying above GestureDetector since it is a stack.

```<code>SingleChildScrollView(),
GestureDetector(),
</code>```

Body:
I am using the stack widget to show the back arrow button on an image. And its showing but the problem is it's not tappable mean Gesturededector is not working on Stack.

My code

  Stack(
      children: <Widget>[

        Container(
          height: height * 0.4,
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(
                'assets/images/place2.jpg',
              ),
              fit: BoxFit.fill,
            ),
          ),
        ),
        GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () => print("first container"),
          child: Align(
            alignment: Alignment.topLeft,
            child: Container(
              margin: EdgeInsets.only(left: width * 0.03),
              padding: EdgeInsets.only(top: statusBarHeight * 2),

              child: Icon(
                Icons.arrow_back_ios,
                size: 25,
                color: Colors.white,
              ),
            ),
          ),
        ),

        SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.only(top: height * 0.3),
            child: SingleChildScrollView(
              child: ClipRRect(
                borderRadius: BorderRadius.only(
                    topRight: Radius.circular(30),
                    topLeft: Radius.circular(30)),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      SizedBox(
                        height: height * 0.05,
                      ),
                      Container(
                        width: width,
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Text(
                          'NYC Food Festival',
                          textAlign: TextAlign.left,
                          style: TextStyle(fontSize: 30),
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Row(
                          children: <Widget>[
                            Icon(
                              Icons.calendar_today,
                              size: 20,
                              color: Color(0xff808080),
                            ),
                            SizedBox(width: width * 0.02), // give it width
                            Column(
                              children: <Widget>[
                                Text(
                                  'Sat, May 25, 2020',
                                  style: TextStyle(
                                      color: Color(0xff000000),
                                      fontWeight: FontWeight.bold,
                                      fontSize: 15),
                                ),
                              ],
                            )
                          ],
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Row(
                          children: <Widget>[
                            Icon(
                              Icons.attach_money,
                              size: 20,
                              color: Color(0xff808080),
                            ),
                            SizedBox(width: width * 0.02), // give it width
                            Column(
                              children: <Widget>[
                                Text(
                                  '25,000 PKR',
                                  style: TextStyle(
                                      color: Color(0xff000000),
                                      fontWeight: FontWeight.bold,
                                      fontSize: 15),
                                ),
                              ],
                            )
                          ],
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                        width: width,
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Text(
                          'Snaps',
                          textAlign: TextAlign.left,
                          style: TextStyle(fontSize: 25),
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                          child: Column(
                            children: <Widget>[
                              CarouselSlider(
                                options: CarouselOptions(
                                  autoPlay: true,
                                  aspectRatio: 2.0,
                                  enlargeCenterPage: true,
                                ),
                                items: imageSliders,
                              ),
                            ],
                          )),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                        width: width,
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Text(
                          'About',
                          textAlign: TextAlign.left,
                          style: TextStyle(fontSize: 25),
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                          padding: EdgeInsets.only(
                              right: width * 0.03, left: width * 0.03),
                          child: DescriptionTextWidget(
                              text:
                                  "Flutter is Google’s mobile UI framework for crafting high-quality native interfaces on iOS and Android in record time. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source.")
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Container(
                        width: width,
                        margin: EdgeInsets.only(left: width * 0.03),
                        child: Text(
                          'Included',
                          textAlign: TextAlign.left,
                          style: TextStyle(fontSize: 25),
                        ),
                      ),
                      SizedBox(
                        height: height * 0.02,
                      ),
                      Included(),
                      SizedBox(
                        height: height * 0.01,
                      ),
                      Included(),
                      SizedBox(
                        height: height * 0.01,
                      ),
                      Included(),
                      SizedBox(
                        height: height * 0.01,
                      ),

                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Container(
            height: 100,
            decoration: BoxDecoration(
              // color: Color.fromARGB(110, 255, 255, 255),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(20),
                topRight: Radius.circular(20),
              ),
              gradient: LinearGradient(
                colors: [
                  Color.fromARGB(30, 255, 255, 255),
                  Color.fromARGB(255, 255, 255, 255),
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
              ),
            ),
            child: Center(
              child: InkWell(
                child: Container(
                  padding: EdgeInsets.symmetric(
                    horizontal: 80,
                    vertical: 20,
                  ),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(30),
                    color: Colors.blueAccent,
                  ),
                  child: Text(
                    "BOOK NOW",
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 20,
                        color: Colors.white),
                  ),
                ),
                onTap: () {
                  return print("Tap");
                },
              ),
            ),
          ),
        ),
      ],
    )

I need to use it as a back button when I navigate but it's not working then for testing I just print a value onTap but its also not working I try to add behaviour also.

Body:
Can you try putting GestureDectector after SingleChildScrollView in a stack.

I guess SingleChildScrollView is lying above GestureDetector since it is a stack.

SingleChildScrollView(),
GestureDetector(),


问:
Why am i getting an exception at dynamic_cast after filling object with zeros using memset
I am getting this weird runtime exception with dynamic_cast, but only if i fill the the object i'm casting with zeros using memset, or just copying some data into it with memcpy . Here's an example that generates the exception.

```<code>class Base
{
public:
    virtual void func() { }
};

class Derived : public Base
{
public:
    void func() override { }
};

int main()
{
    Derived derived;
    Base* base_ptr = &derived;
    memset(base_ptr, 0, sizeof(Derived));

    Derived* derived_ptr = dynamic_cast<Derived*>(base_ptr);
}
</code>```

And the exception message is:

I get the same exception if i copy one Derived object to another using memcpy.
Anyone got any idea whats going on, is it messing up the RTTI that dynamic_cast uses?

I am making a Game Engine and in it is a entity component system, and at one point in the program i load all the Objects and their components from a file. The way i implemented different components is using inheritance (each component derives from one base component). When i load the objects, and its components i cant just allocate the amount of memory that the component takes, i have to use new component name, because only then dynamic_cast works.

I am using Visual Studio 2019.

答:
A class with virtual methods often has a hidden pointer member for the vtable.  By using memset you are destroying that pointer.

The hidden pointer is not required by the C++ language specification, but it is by far the most common way of implementing virtual methods and RTTI.

Body:
I am getting this weird runtime exception with dynamic_cast, but only if i fill the the object i'm casting with zeros using memset, or just copying some data into it with memcpy . Here's an example that generates the exception.

class Base
{
public:
    virtual void func() { }
};

class Derived : public Base
{
public:
    void func() override { }
};

int main()
{
    Derived derived;
    Base* base_ptr = &derived;
    memset(base_ptr, 0, sizeof(Derived));

    Derived* derived_ptr = dynamic_cast<Derived*>(base_ptr);
}

And the exception message is:

I get the same exception if i copy one Derived object to another using memcpy.
Anyone got any idea whats going on, is it messing up the RTTI that dynamic_cast uses?

I am making a Game Engine and in it is a entity component system, and at one point in the program i load all the Objects and their components from a file. The way i implemented different components is using inheritance (each component derives from one base component). When i load the objects, and its components i cant just allocate the amount of memory that the component takes, i have to use new component name, because only then dynamic_cast works.

I am using Visual Studio 2019.


问:
Command rsync in crontab not syncing
I need to use rsync in crontab task, but it's not doing the task. I placed my code as follows:
* * * * * rsync -ar --delete --suffix=date +.%F_%H-%M-%S /home/user1/Desktop /backup

答:
If your crontab task is read from the global crontab file under /etc, you need to specify the user by which the task is executed.

The other thing I see is that the suffix needs to be set by a subexpression.
So it would look something like:

rsync -ar --delete --suffix=$(date +.%F_%H-%M-%S) /home/user1/Desktop /backup
```

Title: Command rsync in crontab not syncing