问:
Contentful Content Management API Error 422 When Creating Asset
I am trying to create and publish an image as an asset using the Contentful Content Management API and have been unsuccessful so far, I have been able to successfully create and publish an entry and I cannot figure out why asset is not working. Here is my code for creating an asset. The eventual goal is to make this work for an array of images/attachments but I am trying to get it working with just one before I handle multiple.

```<code>  makeAsset = async (item) => {
    try {
      var MClient = createClient({
        accessToken: process.env.REACT_APP_CONTENTFUL_MANAGEMENT_ACCESS_TOKEN,
      });
      let space = await MClient.getSpace(
        process.env.REACT_APP_CONTENTFUL_API_SPACE
      );
      let environment = await space.getEnvironment(
        process.env.REACT_APP_CONTENTFUL_ENVIRONMENT
      );

      const asset = await environment.createAsset({
        fields: {
          title: {
            "en-US": item.imgNames[0],
          },
          file: {
            "en-US": {
              fileName: item.images[0].name,
              contentType: item.images[0].type,
              uploadFrom: item.imgUrls[0],
            },
          },
        },
      });

      const processedAsset = await asset.processForAllLocales();
      processedAsset.publish();
</code>```

And the error in the console is this:

```<code>ValidationFailed: {
  "status": 422,
  "statusText": "",
  "message": "Validation error",
  "details": {
    "errors": [
      {
        "name": "type",
        "value": "data:image/png;base64,iVBORw0KGg(The rest of the image URL from above)
        "type": "Object",
            "details": "The type of \"value\" is incorrect, expected type: Object",
            "path": [
            "fields",
            "file",
            "en-US",
            "uploadFrom"
            ]
         }
       ]
     },
"request": {
    "url": "assets",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/vnd.contentful.management.v1+json",
      "X-Contentful-User-Agent": "sdk contentful-management.js/5.26.1; platform browser; os Windows;",
      "Authorization": "Bearer ...7ijJE"
    },
    "method": "post",
    "payloadData": "{\"fields\":{\"title\":{\"en-US\":\"scuba_mask\"},\"file\":{\"en-US\":{\"fileName\":\"scuba_mask.png\",\"contentType\":\"image/png\",\"uploadFrom\":\"data:image/png;base64,iVBORw0KG(The rest of image URL)
</code>```

I have been trying to search for other example because the documentation is patchy and none of the examples are working for me. One solution I tried was using the fs.createReadStream() function but that was removed from React.

答:
@stefan judis, I used the code from the example and one of the functions is being deprecated in newer versions so I figured I would go out on a limb and replace something and it worked. For future reference for anyone else with this issue here is the solution that fixed my problem:</p>

```<code>makeAsset = async (item) => {
try {
  //Contentful API Call
  var MClient = createClient({
    //Fetch access token from environment variables
    accessToken: process.env.REACT_APP_CONTENTFUL_MANAGEMENT_ACCESS_TOKEN,
  });
  //API call that requests the specified space
  let space = await MClient.getSpace(
    process.env.REACT_APP_CONTENTFUL_API_SPACE
  );
  let environment = await space.getEnvironment(
    process.env.REACT_APP_CONTENTFUL_ENVIRONMENT
  );

  const file = item.images[0];
  const contentType = file.type;
  const fileName = file.name;
  let asset = await environment.createAssetFromFiles({
    fields: {
      title: {
        "en-US": fileName,
      },
      file: {
        "en-US": {
          contentType,
          fileName,
          file,
        },
      },
    },
  });
  asset = await asset.processForAllLocales();
  asset.publish();
} catch(error) {
  console.log(error);
}
}
</code>```

Body:
I am trying to create and publish an image as an asset using the Contentful Content Management API and have been unsuccessful so far, I have been able to successfully create and publish an entry and I cannot figure out why asset is not working. Here is my code for creating an asset. The eventual goal is to make this work for an array of images/attachments but I am trying to get it working with just one before I handle multiple.

  makeAsset = async (item) => {
    try {
      var MClient = createClient({
        accessToken: process.env.REACT_APP_CONTENTFUL_MANAGEMENT_ACCESS_TOKEN,
      });
      let space = await MClient.getSpace(
        process.env.REACT_APP_CONTENTFUL_API_SPACE
      );
      let environment = await space.getEnvironment(
        process.env.REACT_APP_CONTENTFUL_ENVIRONMENT
      );

      const asset = await environment.createAsset({
        fields: {
          title: {
            "en-US": item.imgNames[0],
          },
          file: {
            "en-US": {
              fileName: item.images[0].name,
              contentType: item.images[0].type,
              uploadFrom: item.imgUrls[0],
            },
          },
        },
      });

      const processedAsset = await asset.processForAllLocales();
      processedAsset.publish();

And the error in the console is this:

ValidationFailed: {
  "status": 422,
  "statusText": "",
  "message": "Validation error",
  "details": {
    "errors": [
      {
        "name": "type",
        "value": "data:image/png;base64,iVBORw0KGg(The rest of the image URL from above)
        "type": "Object",
            "details": "The type of \"value\" is incorrect, expected type: Object",
            "path": [
            "fields",
            "file",
            "en-US",
            "uploadFrom"
            ]
         }
       ]
     },
"request": {
    "url": "assets",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/vnd.contentful.management.v1+json",
      "X-Contentful-User-Agent": "sdk contentful-management.js/5.26.1; platform browser; os Windows;",
      "Authorization": "Bearer ...7ijJE"
    },
    "method": "post",
    "payloadData": "{\"fields\":{\"title\":{\"en-US\":\"scuba_mask\"},\"file\":{\"en-US\":{\"fileName\":\"scuba_mask.png\",\"contentType\":\"image/png\",\"uploadFrom\":\"data:image/png;base64,iVBORw0KG(The rest of image URL)

I have been trying to search for other example because the documentation is patchy and none of the examples are working for me. One solution I tried was using the fs.createReadStream() function but that was removed from React.

Body:
@stefan judis, I used the code from the example and one of the functions is being deprecated in newer versions so I figured I would go out on a limb and replace something and it worked. For future reference for anyone else with this issue here is the solution that fixed my problem:

makeAsset = async (item) => {
try {
  //Contentful API Call
  var MClient = createClient({
    //Fetch access token from environment variables
    accessToken: process.env.REACT_APP_CONTENTFUL_MANAGEMENT_ACCESS_TOKEN,
  });
  //API call that requests the specified space
  let space = await MClient.getSpace(
    process.env.REACT_APP_CONTENTFUL_API_SPACE
  );
  let environment = await space.getEnvironment(
    process.env.REACT_APP_CONTENTFUL_ENVIRONMENT
  );

  const file = item.images[0];
  const contentType = file.type;
  const fileName = file.name;
  let asset = await environment.createAssetFromFiles({
    fields: {
      title: {
        "en-US": fileName,
      },
      file: {
        "en-US": {
          contentType,
          fileName,
          file,
        },
      },
    },
  });
  asset = await asset.processForAllLocales();
  asset.publish();
} catch(error) {
  console.log(error);
}
}


来源: stackexchange

问:
 not working in Opera 11?
I am testing my noscript tags which display content when javascript is disabled, this works in Safari, Chrome, Firefox, Camino, IE6, IE7, IE8, IE9, basically everything but Opera (I'm running version 11, not sure if its isolated to that version).

In Opera 11 nothing is displayed... is the noscript tag not supported? and what is the alternative?

Nothing surprising:

```<code><noscript>Please enable JavaScript.</noscript>
</code>```

Located between the body tags.

```<code><html>
<body>
<script>alert('Hello World');</script>
<noscript>Hello World!</noscript>
</body>
</html>
</code>```

答:
This works well for me... (Tested in IE, Opera, and FireFox)

```<code><p id="js_disabled">
<script type="text/javascript">
  document.getElementById('js_disabled').style.display = 'none';
</script>
  Javascript is disabled or not supported by your browser.<br/>
  Javascript must be enabled...
</p>
</code>```

The JavaScript runs immediately so the noscript message never appears.

The idea is to place the JavaScript code immediately following the noscript's opening tag, in this case the paragraph tags.

ContentLicense: CC BY-SA 2.5

Title: <noscript> not working in Opera 11?

Body:
I am testing my noscript tags which display content when javascript is disabled, this works in Safari, Chrome, Firefox, Camino, IE6, IE7, IE8, IE9, basically everything but Opera (I'm running version 11, not sure if its isolated to that version).

In Opera 11 nothing is displayed... is the noscript tag not supported? and what is the alternative?

Nothing surprising:

Please enable JavaScript.

Located between the body tags.

Hello World!

Body:
This works well for me... (Tested in IE, Opera, and FireFox)

<p id="js_disabled">
<script type="text/javascript">
  document.getElementById('js_disabled').style.display = 'none';
</script>
  Javascript is disabled or not supported by your browser.<br/>
  Javascript must be enabled...
</p>

The JavaScript runs immediately so the noscript message never appears.

The idea is to place the JavaScript code immediately following the noscript's opening tag, in this case the paragraph tags.

ContentLicense: CC BY-SA 3.0


问:
Creating an async method which throws an exception after a specified amount of time unless a certain condition is met outside of that function
I am currently working on a Ruby script which is supposed to perform different tasks on a pretty long list of hosts. I am using the net-ssh gem for connectivity with those hosts. The thing is, there seem to exist some conditions under which net-ssh times out without throwing an exception. As of know, the script was only once able to finish a run. Most of the time, the scripts just hangs at some point without ever throwing an exception or doing anything.

I thought about running all tasks that may timeout in different threads, passing them a pointer to some variable they can change when the tasks finished successfully, and then check that variable for a given amount of time. If the task has not finished by then, throw an exception in the main thread that I can catch somewhere.

This is the first time I am writing something in Ruby. To give a clear demonstration of what I want to accomplish, this is what I'd do in C++:

    void perform_long_running_task(bool* finished);
    void start_task_and_throw_on_timeout(int secs, std::function<void(bool*)> func);
    int seconds_to_wait {5};
    int seconds_task_takes{6};

    int main() {
        start_task_and_throw_on_timeout(seconds_to_wait, &perform_long_running_task);
        // do other stuff
        return 0;
    }

    void perform_long_running_task(bool* finished){
        // Do something that may possible timeout..
        std::this_thread::sleep_for(std::chrono::seconds(seconds_task_takes));

        // Finished..
        *finished = true;
    }

    void start_task_and_throw_on_timeout(int secs, std::function<void(bool*)> func){
        bool finished {false};
        std::thread task(func, &finished);

        while (secs > 0){
            std::this_thread::sleep_for(std::chrono::seconds(1));
            secs--;
            if (finished){
                task.join();
                return;
            }
        }

        throw std::exception();
    }

```

Here, when 'seconds_task_takes' is bigger than 'seconds_to_wait', an exception is thrown in the main thread. If the task finishes in time, everything goes on smoothly.

However, I need to write my piece of software in a dynamic scripting language that can run anywhere and needs not to be compiled. I would be super glad for any advice about how I could write something like the code above in Ruby.

Thanks alot in advance :)

edit: in the example ,I added a std::function parameter to start_task_and_throw_timeout so it's reusable for all similar functions

答:
I think module timeout has everything you need to do. It allows you to run the block for a while and raise an exception if it was not fast enough.

Here is a code example:

require "timeout"

def run(name)
  puts "Running the job #{name}"
  sleep(10)
end

begin
  Timeout::timeout(5) { run("hard") }
rescue Timeout::Error
  puts "Failed!"
end
```

You can play with it here: https://repl.it/repls/CraftyUnluckyCore. The documentation for the module lives here: https://ruby-doc.org/stdlib-2.5.1/libdoc/timeout/rdoc/Timeout.html. Notice that you can customize not only the timeout, but also error class and message, so different jobs may have different kinds of errors.

ContentLicense: CC BY-SA 4.0

Body:
I am currently working on a Ruby script which is supposed to perform different tasks on a pretty long list of hosts. I am using the net-ssh gem for connectivity with those hosts. The thing is, there seem to exist some conditions under which net-ssh times out without throwing an exception. As of know, the script was only once able to finish a run. Most of the time, the scripts just hangs at some point without ever throwing an exception or doing anything.

I thought about running all tasks that may timeout in different threads, passing them a pointer to some variable they can change when the tasks finished successfully, and then check that variable for a given amount of time. If the task has not finished by then, throw an exception in the main thread that I can catch somewhere.

This is the first time I am writing something in Ruby. To give a clear demonstration of what I want to accomplish, this is what I'd do in C++:

    void perform_long_running_task(bool* finished);
    void start_task_and_throw_on_timeout(int secs, std::function<void(bool*)> func);
    int seconds_to_wait {5};
    int seconds_task_takes{6};

    int main() {
        start_task_and_throw_on_timeout(seconds_to_wait, &perform_long_running_task);
        // do other stuff
        return 0;
    }

    void perform_long_running_task(bool* finished){
        // Do something that may possible timeout..
        std::this_thread::sleep_for(std::chrono::seconds(seconds_task_takes));

        // Finished..
        *finished = true;
    }

    void start_task_and_throw_on_timeout(int secs, std::function<void(bool*)> func){
        bool finished {false};
        std::thread task(func, &finished);

        while (secs > 0){
            std::this_thread::sleep_for(std::chrono::seconds(1));
            secs--;
            if (finished){
                task.join();
                return;
            }
        }

        throw std::exception();
    }

Here, when 'seconds_task_takes' is bigger than 'seconds_to_wait', an exception is thrown in the main thread. If the task finishes in time, everything goes on smoothly.

However, I need to write my piece of software in a dynamic scripting language that can run anywhere and needs not to be compiled. I would be super glad for any advice about how I could write something like the code above in Ruby.

Thanks alot in advance :)

edit: in the example ,I added a std::function parameter to start_task_and_throw_timeout so it's reusable for all similar functions

Body:
I think module timeout has everything you need to do. It allows you to run the block for a while and raise an exception if it was not fast enough.

Here is a code example:

require "timeout"

def run(name)
  puts "Running the job #{name}"
  sleep(10)
end

begin
  Timeout::timeout(5) { run("hard") }
rescue Timeout::Error
  puts "Failed!"
end

You can play with it here: https://repl.it/repls/CraftyUnluckyCore. The documentation for the module lives here: https://ruby-doc.org/stdlib-2.5.1/libdoc/timeout/rdoc/Timeout.html. Notice that you can customize not only the timeout, but also error class and message, so different jobs may have different kinds of errors.


问:
spring R2DBC query by enum type
My PostgreSQL contains an enum like

```<code>create type my_type as enum('VAL1', 'VAL2')
</code>```

in Spring boot app, it is represented by MyType.class enum

I'm trying to run a simple query using DatabasClient

```<code>client.exectute("select * from table where type = :type")...
</code>```

As an error I'm getting:

```<code>ceptionFactory$PostgresqlBadGrammarException: operator does not exist: type = character varying
</code>```

Casting type to my_type doesn't work (both with CAST and ::)

I've already registered a specific codec for MyType.class which works - querying all without conditions works with the relevant @ReadingConverter

答:
Neither Spring Data R2DBC nor R2DBC Postgres can map your enum type to Postgres enum types.

You can still use Postgres enum types represented as strings if you properly cast the bind value/column value on retrieval in your SQL statement.

Example:

```<code>client.exectute("select type::text from table where type = :type::my_type")
</code>```

Title: spring R2DBC query by enum type

Body:
My PostgreSQL contains an enum like

create type my_type as enum('VAL1', 'VAL2')

in Spring boot app, it is represented by MyType.class enum

I'm trying to run a simple query using DatabasClient

client.exectute("select * from table where type = :type")...

As an error I'm getting:

ceptionFactory$PostgresqlBadGrammarException: operator does not exist: type = character varying

Casting type to my_type doesn't work (both with CAST and ::)

I've already registered a specific codec for MyType.class which works - querying all without conditions works with the relevant @ReadingConverter

Body:
Neither Spring Data R2DBC nor R2DBC Postgres can map your enum type to Postgres enum types.

You can still use Postgres enum types represented as strings if you properly cast the bind value/column value on retrieval in your SQL statement.

Example:

client.exectute("select type::text from table where type = :type::my_type")


问:
Correct way to loop through std::array<char*, N>
Consider the following std::array of pointers to char:

std::array<char*, 10> m_arr;
```

<p>I know I can loop through the array using the following code</p>

<pre class="lang-c prettyprint-override"><code>for(size_t i {0}; i < m_arr.size(); i++) {
            std::cout << m_arr.at(i) << std::endl;
    }
</code>```

But this approach throws "Access violation reading location" exception, when the i-th element is not assigned correctly. For example the following code assigns the first two elements but the third element, m_arr.at(3), raises the aforementioned error:

    // test variables
    int x {100};
    double y {3.14};
    int* x_ptr {&x};
    double* y_ptr {&y};

    // std::array of pointer to char
    std::array<char*, 10> m_arr;

    // set the first two elements of m_arr
    char buf1[16];
    sprintf_s(buf1, "%p", x_ptr);
    m_arr.at(0) = buf1;
    char buf2[16];
    sprintf_s(buf2, "%p", y_ptr);
    m_arr.at(1) = buf2;

    for(size_t i {0}; i < m_arr.size(); i++) {
            std::cout << m_arr.at(i) << std::endl;
    }

```

<p>I found out a quick fix around this by checking the i-th element with the last element of the array to skip unassigned elements but obviously that's not a clean answer</p>

<pre class="lang-c prettyprint-override"><code>for(size_t i {0}; i < m_arr.size(); i++) {
        if(m_arr.at(i) != m_arr.back()) {
            std::cout << m_arr.at(i) << std::endl;
        }
    }
</code>```

I believe there is a better way to loop through this array and avoid the error. Thank you in advance for your help.

答:
Initialize your array:

```<code>std::array<char*, 10> m_arr{}; // nullptr initialized
</code>```

then you might check safety for non-nullptr value:

```<code>for (auto ptr : m_arr) {
    if (ptr) std::cout << ptr << std::endl;
}
</code>```

Body:
Consider the following std::array of pointers to char:

std::array<char*, 10> m_arr;

I know I can loop through the array using the following code

for(size_t i {0}; i < m_arr.size(); i++) {
            std::cout << m_arr.at(i) << std::endl;
    }

But this approach throws "Access violation reading location" exception, when the i-th element is not assigned correctly. For example the following code assigns the first two elements but the third element, m_arr.at(3), raises the aforementioned error:

    // test variables
    int x {100};
    double y {3.14};
    int* x_ptr {&x};
    double* y_ptr {&y};

    // std::array of pointer to char
    std::array<char*, 10> m_arr;

    // set the first two elements of m_arr
    char buf1[16];
    sprintf_s(buf1, "%p", x_ptr);
    m_arr.at(0) = buf1;
    char buf2[16];
    sprintf_s(buf2, "%p", y_ptr);
    m_arr.at(1) = buf2;

    for(size_t i {0}; i < m_arr.size(); i++) {
            std::cout << m_arr.at(i) << std::endl;
    }

I found out a quick fix around this by checking the i-th element with the last element of the array to skip unassigned elements but obviously that's not a clean answer

for(size_t i {0}; i < m_arr.size(); i++) {
        if(m_arr.at(i) != m_arr.back()) {
            std::cout << m_arr.at(i) << std::endl;
        }
    }

I believe there is a better way to loop through this array and avoid the error. Thank you in advance for your help.

Body:
Initialize your array:

std::array<char*, 10> m_arr{}; // nullptr initialized

then you might check safety for non-nullptr value:

for (auto ptr : m_arr) {
    if (ptr) std::cout << ptr << std::endl;
}


问:
Having troubles cleaning data from JSON into Python
I have only just started learning python recently and I have no experience with JSON files.

When I import my JSON file it seems like an entire dictionary of items is in one column? I have been trying to split it but it doesn't seem to work.
Here's a screenshot of how the data looks like,  but this is how the original data looks.

I tried np.split but there's a TypeError: cannot do slice indexing on <class 'pandas.core.indexes.range.RangeIndex'> with these indexers [,] of 'class 'str'

I also tried

MRT = pd.read_json('mrt_stations.json')
MRT_Stations = MRT['Possible Locations'].map(str).apply(json.loads)
MRT_Stations['Address'] = jsonO.apply(lambda x: x['Address'])

as I saw this on Python2.7: How to split a column into multiple column based on special strings like this?

However, it returns a JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

Would appreciate some help and thanks in advance!

答:
Does this throw an error when reading the file?</p>
```<code>import json
import pd

with open('mrt_stations.json') as json_file:
  mrt_json = json.load(json_file)

mrt_df = pd.DataFrame(data=mrt_json)
print(mrt_df)

mrt_stations = mrt_df['Possible Locations']
mrt_stations['Address'] = mrt_stations.apply(lambda x: x['Address'])
</code>```

Body:
Does this throw an error when reading the file?

import json
import pd

with open('mrt_stations.json') as json_file:
  mrt_json = json.load(json_file)

mrt_df = pd.DataFrame(data=mrt_json)
print(mrt_df)

mrt_stations = mrt_df['Possible Locations']
mrt_stations['Address'] = mrt_stations.apply(lambda x: x['Address'])


问:
What is the most robust HTTP library for android?
I'm looking for a library that handles HTTP POST, multipart etc. Is there a de-facto standard library to make these requests easier on android?</p>

答:
Take a look at DroidFu, and in particular the DroidFu HTTP components. They're a fairly thin wrapper around the Apache Commons HTTP stuff, but they fit most needs pretty well. It includes some niceties like optional HTTP and model caching, and even "why isn't this built in to the platform" stuff like GZip. (An aside: android.net.AndroidHttPClient is a pretty good upgrade to the older stock DefaultHttpClient stuff when you need to drop down for a little more control, but it's Android 2.2+ only and is fairly underdocumented).

If you just need multipart with a minimum hassle, you can try android_multipart_post, though I've never tried it.

EDIT:

DroidFu is now discontinued. These days if I was starting a new project I'd almost certainly use Volley, with OKHttp if I needed more control (you can even use OKHttp as the transport layer for volley if you want to do both).


问:
Python youtube-dl It is possible to get merging file's extension?
When I run it:

```<code>ydl_opts = {
    'outtmpl': files_path + '%(id)s.%(ext)s',
}
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([video_url])
</code>```

I get it in logs:

```<code>[ffmpeg] Merging formats into "/home/tmp/0uBOtQOO70Y.mkv"
[wsgi:error] Deleting original file /home/tmp/0uBOtQOO70Y.f137.mp4 (pass -k to keep)
[wsgi:error] Deleting original file /home/tmp/0uBOtQOO70Y.f251.webm (pass -k to keep)
</code>```

How can I get /home/tmp/0uBOtQOO70Y.mkv before or after download?

If I make it:

```<code>ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
     info = ydl.extract_info(video_url, download=False)
</code>```

Then in info was mp4 extension

答:
I had the same problem, from what I can tell youtube-dl doesn't return extension of the output file. The extension from ydl.extract_info() is useless if formats are merged into another format. You can check if formats will be merged by ydl.extract_info()['format_id'] – if there is "+" symbol between numbers, they will be merged. However we still can't tell what the output format will be.
I created a clumsy workaround for this. Since we can control the name of the output file (without an extension, which is very hard to predict) I just checked if files with most probable extensions exits.

```<code>url = 'https://linktovideofile.com/'
filename = './example'
ytdl_opts = {'outtmpl': filename+'.%(ext)s'}
with youtube_dl.YoutubeDL(ytdl_opts) as ytdl:
    ytdl.download([url])

if os.path.isfile(filename+'.mkv'):
    extension = '.mkv'
    filename= filename+extension
elif os.path.isfile(filename+'.mp4'):
    extension = '.mp4'
    filename= filename+extension
elif os.path.isfile(filename+'.webm'):
    extension = '.webm'
    filename= filename+extension
</code>```

I hope this helps, it works for my simple script but may be insufficient for more advanced one.

Body:
When I run it:

ydl_opts = {
    'outtmpl': files_path + '%(id)s.%(ext)s',
}
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([video_url])

I get it in logs:

[ffmpeg] Merging formats into "/home/tmp/0uBOtQOO70Y.mkv"
[wsgi:error] Deleting original file /home/tmp/0uBOtQOO70Y.f137.mp4 (pass -k to keep)
[wsgi:error] Deleting original file /home/tmp/0uBOtQOO70Y.f251.webm (pass -k to keep)

How can I get /home/tmp/0uBOtQOO70Y.mkv before or after download?

If I make it:

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
     info = ydl.extract_info(video_url, download=False)

Then in info was mp4 extension

Body:
I had the same problem, from what I can tell youtube-dl doesn't return extension of the output file. The extension from ydl.extract_info() is useless if formats are merged into another format. You can check if formats will be merged by ydl.extract_info()['format_id'] – if there is "+" symbol between numbers, they will be merged. However we still can't tell what the output format will be.
I created a clumsy workaround for this. Since we can control the name of the output file (without an extension, which is very hard to predict) I just checked if files with most probable extensions exits.

url = 'https://linktovideofile.com/'
filename = './example'
ytdl_opts = {'outtmpl': filename+'.%(ext)s'}
with youtube_dl.YoutubeDL(ytdl_opts) as ytdl:
    ytdl.download([url])

if os.path.isfile(filename+'.mkv'):
    extension = '.mkv'
    filename= filename+extension
elif os.path.isfile(filename+'.mp4'):
    extension = '.mp4'
    filename= filename+extension
elif os.path.isfile(filename+'.webm'):
    extension = '.webm'
    filename= filename+extension

I hope this helps, it works for my simple script but may be insufficient for more advanced one.


答:
The problem isn't so much in the way that you iterate the array, but rather what the array contains, and what you do with those contents: The issue is that you only initialised two of the pointers in the array. Reading the value, and indirecting through the uninitialised pointers results in undefined behaviour.

Here is a correct way to loop through only the initialised elements, relying on the knowledge that you had initialised first two:

```<code>for(size_t i {0}; i < 2; i++) {
</code>```

Another approach is to initialise the other pointers to null, and check for that in the loop condition:

```<code>std::array<char*, 10> m_arr{}; // value initialisation
...
for(size_t i {0}; i < m_arr.size() && m_arr[i]; i++) {

// or with range-for:
for (char* ptr : m_arr) {
    if (!ptr)
        break;
    std::cout << ptr << '\n';
</code>```

Title: Correct way to loop through std::array<char*, N>

Body:
The problem isn't so much in the way that you iterate the array, but rather what the array contains, and what you do with those contents: The issue is that you only initialised two of the pointers in the array. Reading the value, and indirecting through the uninitialised pointers results in undefined behaviour.

Here is a correct way to loop through only the initialised elements, relying on the knowledge that you had initialised first two:

for(size_t i {0}; i < 2; i++) {

Another approach is to initialise the other pointers to null, and check for that in the loop condition:

std::array<char*, 10> m_arr{}; // value initialisation
...
for(size_t i {0}; i < m_arr.size() && m_arr[i]; i++) {

// or with range-for:
for (char* ptr : m_arr) {
    if (!ptr)
        break;
    std::cout << ptr << '\n';


问:
Logging not writing logging messages to log file but printing on console
I am trying to learn logging module in Django. I want to write exceptions to the log file so that I can see them later. Printing log messages on console is working fine but I am unable to write the same log messages to the log file. Below is my code.

My settings.py

```<code>LOGGING = {
    'version': 1,
    'loggers': {
        'django':{
            'handlers': ['file'] ,
            'level':'INFO'
        }
    },
    'handlers':{
        'file':{
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
        }
    }
}
</code>```

My views.py

```<code>def Helloworld(request):
    try:
        s = HttpResponse("this is life")
        logging.debug("this is debug log - in hw page")
        logging.critical("this is critical log - in hw page")
        logging.error("this is error log - in hw page")
    except Exception as e:
        print(e)
        logging.critical("this is critical log - in hw page - exception")
        logging.exception(e)
    return s
</code>```

My console output

```<code>Django version 3.0.6, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
CRITICAL:root:this is critical log - in hw page
ERROR:root:this is error log - in hw page
INFO:django.server:"GET /hw HTTP/1.1" 200 12
</code>```

My output of log file

```<code>Watching for file changes with StatReloader
"GET /hw HTTP/1.1" 200 12
C:\Users\....\views.py changed, reloading.

    Watching for file changes with StatReloader
    Watching for file changes with StatReloader
    "GET /hw HTTP/1.1" 200 12
    "GET /hw HTTP/1.1" 200 12
    C:\Users\...\views.py changed, reloading.
    Watching for file changes with StatReloader
    C:\Users\...\views.py changed, reloading.
    Watching for file changes with StatReloader
    "GET /hw HTTP/1.1" 200 12
</code>```

My Urls.py

```<code>urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('', include(router.urls)),
    url(r'^hw$', Helloworld),
    url('__debug__/', include(debug_toolbar.urls)),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
</code>```

Please let me know where I am going wrong. I want the same text which is printed in the console to be written in the log file.

答:
My logging configuration in settings.py was incorrect. The correct configuration is below.</p>

```<code>LOGGING = {
    'version': 1,
    'formatters': {
         'standard': {
             'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
         },
     },
    'loggers': {
        '':{
            'handlers': ['file'] ,
            'level':'DEBUG'
        }
    },
    'handlers':{
        'file':{
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
            'formatter': 'standard'
        }
    }
}
</code>```

Body:
I am trying to learn logging module in Django. I want to write exceptions to the log file so that I can see them later. Printing log messages on console is working fine but I am unable to write the same log messages to the log file. Below is my code.

My settings.py

LOGGING = {
    'version': 1,
    'loggers': {
        'django':{
            'handlers': ['file'] ,
            'level':'INFO'
        }
    },
    'handlers':{
        'file':{
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
        }
    }
}

My views.py

def Helloworld(request):
    try:
        s = HttpResponse("this is life")
        logging.debug("this is debug log - in hw page")
        logging.critical("this is critical log - in hw page")
        logging.error("this is error log - in hw page")
    except Exception as e:
        print(e)
        logging.critical("this is critical log - in hw page - exception")
        logging.exception(e)
    return s

My console output

Django version 3.0.6, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
CRITICAL:root:this is critical log - in hw page
ERROR:root:this is error log - in hw page
INFO:django.server:"GET /hw HTTP/1.1" 200 12

My output of log file

Watching for file changes with StatReloader
"GET /hw HTTP/1.1" 200 12
C:\Users\....\views.py changed, reloading.

    Watching for file changes with StatReloader
    Watching for file changes with StatReloader
    "GET /hw HTTP/1.1" 200 12
    "GET /hw HTTP/1.1" 200 12
    C:\Users\...\views.py changed, reloading.
    Watching for file changes with StatReloader
    C:\Users\...\views.py changed, reloading.
    Watching for file changes with StatReloader
    "GET /hw HTTP/1.1" 200 12

My Urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('', include(router.urls)),
    url(r'^hw$', Helloworld),
    url('__debug__/', include(debug_toolbar.urls)),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Please let me know where I am going wrong. I want the same text which is printed in the console to be written in the log file.

Body:
My logging configuration in settings.py was incorrect. The correct configuration is below.

LOGGING = {
    'version': 1,
    'formatters': {
         'standard': {
             'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
         },
     },
    'loggers': {
        '':{
            'handlers': ['file'] ,
            'level':'DEBUG'
        }
    },
    'handlers':{
        'file':{
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
            'formatter': 'standard'
        }
    }
}


问:
Determine Median Residual Lifetime of Lognormal Distribution
Stefan Gelissen provides and excellent overview on how to determine median residual lifetime of a lognormal distribution in his blog here

However, when running his code, I stumbled upon this line

(mdrlhat <- uniroot(mdrl, c(0, 1e+9), t=t, Xmu=Xmu, Xsigma=Xsigma)$root)

where Stefan uses a hard coded interval of c(0, 1e+9), which works fine for the example data set from Meeker and Escobar (1998).

However, it is unclear to me how this interval should be determined in another case using different data.

Can anyone help? Thank you.

答: The lower bound of the interval c(0, 1e+9) can not be lower than "zero" and the upper bound should be a reasonably large number, which is the case for 1e+9. Theoretically, you could even chose an even larger number. However, there is no exact way of determining the upper-bound. </p>


问:
How to select all rows from a Dask dataframe with value equal to minimal value of group
So I have following dask dataframe grouped by Problem column.

```<code>| Problem | Items   | Min_Dimension | Max_Dimension | Cost  |
|-------- |------   |---------------|-------------- |------ |
| A       | 7       | 2             | 15            | 23    |
| A       | 5       | 2             | 15            | 38    |
| A       | 15      | 2             | 15            | 23    |
| B       | 11      | 6             | 10            | 54    |
| B       | 10      | 6             | 10            | 48    |
| B       | 18      | 6             | 10            | 79    |
| C       | 50      | 8             | 25            | 120   |
| C       | 50      | 8             | 25            | 68    |
| C       | 48      | 8             | 25            | 68    |
| ...     | ...     | ...           | ...           | ...   |
</code>```

The goal is to create a new dataframe with all rows where the Cost values is minimal for this particular Problem group. So we want following result:

```<code>| Problem | Items   | Min_Dimension | Max_Dimension | Cost  |
|-------- |------   |---------------|-------------- |------ |
| A       | 7       | 2             | 15            | 23    |
| A       | 15      | 2             | 15            | 23    |
| B       | 10      | 6             | 10            | 48    |
| C       | 50      | 8             | 25            | 68    |
| C       | 48      | 8             | 25            | 68    |
| ...     | ...     | ...           | ...           | ...   |
</code>```

How can I achieve this result, i already tried using idxmin() as mentioned in another question on here, but then I get a ValueError: Not all divisions are known, can't align partitions. Please use set_index to set the index.

答:
What if you create another dataframe that is grouped by Problem and Cost.min()? Let's say the new column is called cost_min.

```<code>df1 = df.groupby('Problem')['Cost'].min().reset_index()
</code>```

Then, merge back this new cost_min column back to the dataframe.

```<code>df2 = pd.merge(df, df1, how='left', on='Problem')
</code>```

From there, do something like:

df_new = df2.loc[df2['Cost'] == df2['cost_min']]

Just wrote some pseudocode, but I think that all works with Dask.

Body:
So I have following dask dataframe grouped by Problem column.

| Problem | Items   | Min_Dimension | Max_Dimension | Cost  |
|-------- |------   |---------------|-------------- |------ |
| A       | 7       | 2             | 15            | 23    |
| A       | 5       | 2             | 15            | 38    |
| A       | 15      | 2             | 15            | 23    |
| B       | 11      | 6             | 10            | 54    |
| B       | 10      | 6             | 10            | 48    |
| B       | 18      | 6             | 10            | 79    |
| C       | 50      | 8             | 25            | 120   |
| C       | 50      | 8             | 25            | 68    |
| C       | 48      | 8             | 25            | 68    |
| ...     | ...     | ...           | ...           | ...   |

The goal is to create a new dataframe with all rows where the Cost values is minimal for this particular Problem group. So we want following result:

| Problem | Items   | Min_Dimension | Max_Dimension | Cost  |
|-------- |------   |---------------|-------------- |------ |
| A       | 7       | 2             | 15            | 23    |
| A       | 15      | 2             | 15            | 23    |
| B       | 10      | 6             | 10            | 48    |
| C       | 50      | 8             | 25            | 68    |
| C       | 48      | 8             | 25            | 68    |
| ...     | ...     | ...           | ...           | ...   |

How can I achieve this result, i already tried using idxmin() as mentioned in another question on here, but then I get a ValueError: Not all divisions are known, can't align partitions. Please use set_index to set the index.

Body:
What if you create another dataframe that is grouped by Problem and Cost.min()? Let's say the new column is called cost_min.

df1 = df.groupby('Problem')['Cost'].min().reset_index()

Then, merge back this new cost_min column back to the dataframe.

df2 = pd.merge(df, df1, how='left', on='Problem')

From there, do something like:

df_new = df2.loc[df2['Cost'] == df2['cost_min']]

Just wrote some pseudocode, but I think that all works with Dask.


问:
c# Regex problems
I have this Regex which I'm working on

```<code>string addressstart = Regex.Escape("<a href=\"/url?q=");
                string addressend = Regex.Escape("&amp");
                string regAdd = addressstart + @"(.*?)" + addressend;
</code>```

I'd like it to give me the url from this html

```<code><a href="/url?q=https://www.google.com/&sa=U&ved=0ahUKEwizwPy0yNHSAhXMDpAKHec7DAsQFgh6MA0&usg=AFQjCNEjJILXPMMCNAlz5MN1IIzjpr79tw">
</code>```

so it should return "https://www.google.com/"

Any ideas Why it isnt working?
thanks!

Title: c# Regex problems

Body:
I have this Regex which I'm working on

string addressstart = Regex.Escape("<a href=\"/url?q=");
                string addressend = Regex.Escape("&amp");
                string regAdd = addressstart + @"(.*?)" + addressend;

I'd like it to give me the url from this html

so it should return "https://www.google.com/"

Any ideas Why it isnt working?
thanks!

Body:
You can use:

(?<=a href="\/url\?q=)[^&]+


问:
Can't access Hapi server inside Docker Container
I built a simple NodeJS server with Hapi and tried to run it inside a Docker container.

It runs nicely inside Docker, but I can't get access to it (even though I have done port mapping).

```<code>const hapi = require("@hapi/hapi");

const startServer = async () => {
   const server = hapi.Server({
    host: "localhost",
    port: 5000,
  });

    server.route({
        method: 'GET',
        path: '/sample',
        handler: (request, h) => {

            return 'Hello World!';
        }
    });
     await server.start();
     console.log(`Server running on port ${server.settings.port}`);
 };

 startServer();

</code>```

Docker file is as follows:

```<code>FROM node:alpine

WORKDIR /usr/app

COPY ./package.json ./
RUN npm install

COPY ./ ./

CMD [ "npm","run","dev" ]
</code>```

To run docker, I first build with:

```<code>docker build .
</code>```

I then run the image I get from above command to do port mapping:

```<code>docker run -p 5000:5000 <image-name>
</code>```

When I try to access it via postman on http://localhost:5000/sample or even localhost:5000/sample, it keeps saying Couldn't connect to server and when I open in chrome, it says the same Can't display page.

PS. When i run the code as usual without Docker container, with simply npm run dev from my terminal, the code runs just fine.

So, I am confident, the API code is fine.

Any suggestions??

答:
As mentioned by @pzaenger on your HAPI server configuration change localhost to 0.0.0.0.</p>

```<code>host: 'localhost' to host: '0.0.0.0',
</code>```

Body:
I built a simple NodeJS server with Hapi and tried to run it inside a Docker container.

It runs nicely inside Docker, but I can't get access to it (even though I have done port mapping).

const hapi = require("@hapi/hapi");

const startServer = async () => {
   const server = hapi.Server({
    host: "localhost",
    port: 5000,
  });

    server.route({
        method: 'GET',
        path: '/sample',
        handler: (request, h) => {

            return 'Hello World!';
        }
    });
     await server.start();
     console.log(`Server running on port ${server.settings.port}`);
 };

 startServer();

Docker file is as follows:

FROM node:alpine

WORKDIR /usr/app

COPY ./package.json ./
RUN npm install

COPY ./ ./

CMD [ "npm","run","dev" ]

To run docker, I first build with:

docker build .

I then run the image I get from above command to do port mapping:

docker run -p 5000:5000 <image-name>

When I try to access it via postman on http://localhost:5000/sample or even localhost:5000/sample, it keeps saying Couldn't connect to server and when I open in chrome, it says the same Can't display page.

PS. When i run the code as usual without Docker container, with simply npm run dev from my terminal, the code runs just fine.

So, I am confident, the API code is fine.

Any suggestions??

Body:
As mentioned by @pzaenger on your HAPI server configuration change localhost to 0.0.0.0.

host: 'localhost' to host: '0.0.0.0',


问:
How do I copy a row of data to a new sheet with a different number of columns?
I know how to copy data from one sheet to another.
I'm currently trying to do it for a new work project and I'm stumped.

I keep getting an error of 'The coordinates of the target range are outside the dimensions of the sheet'

The same columns exist between all 4 sheets that I'm working with, BUT some are hidden.
Is this why I am getting the message? That's the only thing I can think of. If so, how do I get around that?

Code:

```<code>function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var RequestSheet = ss.getSheetByName('Request');
  var InProgressSheet = ss.getSheetByName('In Progress');
  var ReviewSheet = ss.getSheetByName('Ready for Review');
  var CompletedSheet = ss.getSheetByName('Completed');
  var HoldSheet = ss.getSheetByName('On Hold');

  var status = RequestSheet.getActiveCell().getValue();
  var row = RequestSheet.getActiveCell().getRow();
  var column = RequestSheet.getActiveCell().getColumn();

  var a1notation = ("A" + row + ":" + "X" + row);
  var newRange = RequestSheet.getRange(a1notation);

  if(status == 'In Progress') {
     newRange.copyTo(InProgressSheet.getRange(InProgressSheet.getLastRow()
 + 1, 1))
  } else if (status == 'On Hold') {
      Browser.msgBox('On Hold was selected')
  } else if (status == 'Completed') {
      Browser.msgBox('Completed was selected')
  } else
      return
}
</code>```

答:
Here's what worked for me.
The sheets that I was copying data to had certain cells with list items built-in to them. Shown here:

So when

```<code>newRange.copyTo(InProgressSheet.getRange(InProgressSheet.getLastRow()
 + 1, 1))
</code>```

was called, getLastRow() + 1, 1 was out of bounds because all of the rows technically had data in them. I cleared all of those out and it worked like a charm.

Body:
I know how to copy data from one sheet to another.
I'm currently trying to do it for a new work project and I'm stumped.

I keep getting an error of 'The coordinates of the target range are outside the dimensions of the sheet'

The same columns exist between all 4 sheets that I'm working with, BUT some are hidden.
Is this why I am getting the message? That's the only thing I can think of. If so, how do I get around that?

Code:

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var RequestSheet = ss.getSheetByName('Request');
  var InProgressSheet = ss.getSheetByName('In Progress');
  var ReviewSheet = ss.getSheetByName('Ready for Review');
  var CompletedSheet = ss.getSheetByName('Completed');
  var HoldSheet = ss.getSheetByName('On Hold');

  var status = RequestSheet.getActiveCell().getValue();
  var row = RequestSheet.getActiveCell().getRow();
  var column = RequestSheet.getActiveCell().getColumn();

  var a1notation = ("A" + row + ":" + "X" + row);
  var newRange = RequestSheet.getRange(a1notation);

  if(status == 'In Progress') {
     newRange.copyTo(InProgressSheet.getRange(InProgressSheet.getLastRow()
 + 1, 1))
  } else if (status == 'On Hold') {
      Browser.msgBox('On Hold was selected')
  } else if (status == 'Completed') {
      Browser.msgBox('Completed was selected')
  } else
      return
}

Body:
Here's what worked for me.
The sheets that I was copying data to had certain cells with list items built-in to them. Shown here:

So when

newRange.copyTo(InProgressSheet.getRange(InProgressSheet.getLastRow()
 + 1, 1))

was called, getLastRow() + 1, 1 was out of bounds because all of the rows technically had data in them. I cleared all of those out and it worked like a charm.


问:
Error when trying to use SELECT...FOR UPDATE in MySql
I've been trying to execute this line command:

```<code>START TRANSACTION; SELECT * FROM users where uid = 1 FOR UPDATE
</code>```

in phpMyAdmin. It is throwing an error when I do so:

SQL query:

SELECT * FROM users where uid = 1 FOR UPDATE LIMIT 0, 25

MySQL said:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 0, 25' at line 1

What I am trying to do is to implement exclusive record locking when an update query is executed.

答:
Per https://dev.mysql.com/doc/refman/8.0/en/select.html, LIMIT needs to come before FOR UPDATE. So changing your query to the following should get you past this error:</p>

```<code>START TRANSACTION; SELECT * FROM users WHERE uid = 1 LIMIT 0, 25 FOR UPDATE;
</code>```

Body:
I've been trying to execute this line command:

START TRANSACTION; SELECT * FROM users where uid = 1 FOR UPDATE

in phpMyAdmin. It is throwing an error when I do so:

SQL query:

SELECT * FROM users where uid = 1 FOR UPDATE LIMIT 0, 25

MySQL said:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 0, 25' at line 1

What I am trying to do is to implement exclusive record locking when an update query is executed.

Body:
Per https://dev.mysql.com/doc/refman/8.0/en/select.html, LIMIT needs to come before FOR UPDATE. So changing your query to the following should get you past this error:

START TRANSACTION; SELECT * FROM users WHERE uid = 1 LIMIT 0, 25 FOR UPDATE;


问:
Is it possible to split subtargets from a dynamic branching target such that later dynamic branching targets get just one subsubtarget?
I have a plan like:

plan = drake::drake_plan(

    targ1 = target(
        f1(input)
        , dynamic = map(input)
    )

    , targ2 = target(
        f2(targ1)
        , dynamic = map(targ1)
    )
)
```

<p>Where the function <code>f1</code> actually yields multiple chunks of output (say, in a list), and I'd like these multiple chunks to be processed separately when <code>targ2</code> is computed. Is this possible?</p>

<p>Here's a minimal example:</p>

<pre class="lang-r prettyprint-override"><code>f1 = function(x){
    return(list(x,x+1))
}

f2 = function(x){
    return(x*2)
}

input = c(1,99)

plan = drake::drake_plan(
    targ1 = target(
        f1(input)
        , dynamic = map(input)
    )
    , targ2 = target(
        f2(targ1)
        , dynamic = map(targ1)
    )
)
drake::make(
    plan
)
</code>```

Where as coded, drake gets an error in processing targ2 because the list in each subtarget from targ1 hasn't been broken apart yet. Obviously I could rewrite f2 to iterate over the list, but this was for demonstration purposes and in my actual use case there are good reasons for wanting to simply split out the results from targ1.

I thought I had it solved with:

f1 = function(x){
    return(list(x,x+1))
}

f2 = function(x){
    return(x*2)
}

input = c(1,99)

plan = drake::drake_plan(
    targ1 = target(
        f1(input)
        , dynamic = map(input)
    )
    , targ2 = target(
        unlist(targ1)
    )
    , targ3 = target(
        f2(targ2)
        , dynamic = map(targ2)
    )
)
```

<p>But in my real use case each subtarget takes up a lot of memory, and the computation of <code>targ2</code> appears to necessitate bringing them all into memory, causing a lock up as my machine runs out of memory.</p>

<p>I've worked out a hack where I save the individual list elements from each subtarget in targ1 to file then do a <code>list_files()</code> search for all such files as input to later targets, but maybe there's a simpler? </p>

<p>Here's the hack that's "working" but surely less than ideal:</p>

<pre class="lang-r prettyprint-override"><code>library(drake)

f1 = function(x){
    out = list(x,x+1)
    for(i in 1:length(out)){
        a = out[[i]]
        save(a,file=paste0(digest::digest(a),'.rda'))
    }
    return(digest::digest(out))
}

f2 = function(x){
    list.files(pattern='.rda')
}

f3 = function(this_rda){
    load(this_rda)
    return(a)
}

f4 = function(x){
    return(x*2)
}

input = c(1,99)

plan = drake::drake_plan(
    targ1 = target(
        f1(input)
        , dynamic = map(input)
    )
    , targ2 = target(
        f2(targ1)
    )
    , targ3 = target(
        f3(targ2)
        , dynamic = map(targ2)
    )
    , targ4 = target(
        f4(targ3)
        , dynamic = map(targ3)
    )
)
drake::make(plan)
readd(targ4)

</code>```

答:
drake does not support dynamic branching within dynamic sub-targets, but you can combine static branching with dynamic branching to achieve something very similar.

library(drake)
input_values <- c(1, 99)
plan <- drake_plan(
  targ1 = target(
    f1(input),
    transform = map(input = !!input_values)
  ),
  targ2 = target(
    f2(targ1),
    transform = map(targ1),
    dynamic = map(targ1)
  )
)

drake_plan_source(plan)
#> drake_plan(
#>   targ1_1 = f1(1),
#>   targ1_99 = f1(99),
#>   targ2_targ1_1 = target(
#>     command = f2(targ1_1),
#>     dynamic = map(targ1_1)
#>   ),
#>   targ2_targ1_99 = target(
#>     command = f2(targ1_99),
#>     dynamic = map(targ1_99)
#>   )
#> )
```

Created on 2020-05-28 by the reprex package (v0.3.0)

Body:
I have a plan like:

plan = drake::drake_plan(

    targ1 = target(
        f1(input)
        , dynamic = map(input)
    )

    , targ2 = target(
        f2(targ1)
        , dynamic = map(targ1)
    )
)

Where the function f1 actually yields multiple chunks of output (say, in a list), and I'd like these multiple chunks to be processed separately when targ2 is computed. Is this possible?

Here's a minimal example:

f1 = function(x){
    return(list(x,x+1))
}

f2 = function(x){
    return(x*2)
}

input = c(1,99)

plan = drake::drake_plan(
    targ1 = target(
        f1(input)
        , dynamic = map(input)
    )
    , targ2 = target(
        f2(targ1)
        , dynamic = map(targ1)
    )
)
drake::make(
    plan
)

Where as coded, drake gets an error in processing targ2 because the list in each subtarget from targ1 hasn't been broken apart yet. Obviously I could rewrite f2 to iterate over the list, but this was for demonstration purposes and in my actual use case there are good reasons for wanting to simply split out the results from targ1.

I thought I had it solved with:

f1 = function(x){
    return(list(x,x+1))
}

f2 = function(x){
    return(x*2)
}

input = c(1,99)

plan = drake::drake_plan(
    targ1 = target(
        f1(input)
        , dynamic = map(input)
    )
    , targ2 = target(
        unlist(targ1)
    )
    , targ3 = target(
        f2(targ2)
        , dynamic = map(targ2)
    )
)

But in my real use case each subtarget takes up a lot of memory, and the computation of targ2 appears to necessitate bringing them all into memory, causing a lock up as my machine runs out of memory.

I've worked out a hack where I save the individual list elements from each subtarget in targ1 to file then do a list_files() search for all such files as input to later targets, but maybe there's a simpler?

Here's the hack that's "working" but surely less than ideal:

library(drake)

f1 = function(x){
    out = list(x,x+1)
    for(i in 1:length(out)){
        a = out[[i]]
        save(a,file=paste0(digest::digest(a),'.rda'))
    }
    return(digest::digest(out))
}

f2 = function(x){
    list.files(pattern='.rda')
}

f3 = function(this_rda){
    load(this_rda)
    return(a)
}

f4 = function(x){
    return(x*2)
}

input = c(1,99)

plan = drake::drake_plan(
    targ1 = target(
        f1(input)
        , dynamic = map(input)
    )
    , targ2 = target(
        f2(targ1)
    )
    , targ3 = target(
        f3(targ2)
        , dynamic = map(targ2)
    )
    , targ4 = target(
        f4(targ3)
        , dynamic = map(targ3)
    )
)
drake::make(plan)
readd(targ4)

Body:
drake does not support dynamic branching within dynamic sub-targets, but you can combine static branching with dynamic branching to achieve something very similar.

library(drake)
input_values <- c(1, 99)
plan <- drake_plan(
  targ1 = target(
    f1(input),
    transform = map(input = !!input_values)
  ),
  targ2 = target(
    f2(targ1),
    transform = map(targ1),
    dynamic = map(targ1)
  )
)

drake_plan_source(plan)
#> drake_plan(
#>   targ1_1 = f1(1),
#>   targ1_99 = f1(99),
#>   targ2_targ1_1 = target(
#>     command = f2(targ1_1),
#>     dynamic = map(targ1_1)
#>   ),
#>   targ2_targ1_99 = target(
#>     command = f2(targ1_99),
#>     dynamic = map(targ1_99)
#>   )
#> )

Created on 2020-05-28 by the reprex package (v0.3.0)


问:
Python - Regex (Re.Escape, Re.Findall); How To: Find sub-strings + a number of characters beyond the sub-strings within a string?
This might be an simple question.  I'm learning how to use Regex and I am having trouble performing a specific task on a string.

For example:

example_string = "; One, one; Two, two; Three, three; Four, four"

desired_output = ["One, o", "Two, t", "Three, t", "Four, f"] #list output is OK

With the following, i can get ["One" , "Two" , "Three"]:

```<code>def findStringsInMiddle(a, b, text):
    return re.findall(re.escape(a)+"(.*?)"+re.escape(b),text)

desired_output = findStringsInMiddle('; ' , ',' , example_string)
</code>```

But I am having trouble figuring out how to configure this properly to grab the comma+space+any_type_of_character that I also want.

Any advice?

Thanks!

答:
Here a solution:

```<code>example_string = "; One, one; Two, two; Three, three; Four, four"
def findStringsInMiddle(text):
    return re.findall("; (.+?, [a-z])",text)

desired_output = findStringsInMiddle(example_string)
desired_output
</code>```

Output:

```<code>['One, o', 'Two, t', 'Three, t', 'Four, f']
</code>```

Body:
This might be an simple question.  I'm learning how to use Regex and I am having trouble performing a specific task on a string.

For example:

example_string = "; One, one; Two, two; Three, three; Four, four"

desired_output = ["One, o", "Two, t", "Three, t", "Four, f"] #list output is OK

With the following, i can get ["One" , "Two" , "Three"]:

def findStringsInMiddle(a, b, text):
    return re.findall(re.escape(a)+"(.*?)"+re.escape(b),text)

desired_output = findStringsInMiddle('; ' , ',' , example_string)

But I am having trouble figuring out how to configure this properly to grab the comma+space+any_type_of_character that I also want.

Any advice?

Thanks!

Body:
Here a solution:

example_string = "; One, one; Two, two; Three, three; Four, four"
def findStringsInMiddle(text):
    return re.findall("; (.+?, [a-z])",text)

desired_output = findStringsInMiddle(example_string)
desired_output

Output:

['One, o', 'Two, t', 'Three, t', 'Four, f']


问:
How to refresh icons color on CurvedNavigationBar
I'm trying to use the curvedNavigationBar package. Here is my code :

```<code> Widget build(BuildContext context) {
     int currentTab = 2;
    return Scaffold(
      key: _scaffoldKey,
      bottomNavigationBar: CurvedNavigationBar(
        key: _bottomNavigationKey,
        index: currentTab,
        height: 50.0,
        items: <Widget>[
          Icon(Icons.home, color:currentTab==0?Colors.yellow:Colors.black, size: 30),
          Icon(Icons.wifi_tethering, color:currentTab==1?Colors.yellow:Colors.black, size: 30),
          Icon(Icons.flag, color:currentTab==2?Colors.yellow:Colors.black, size: 65),
          Icon(Icons.mail, color:currentTab==3?Colors.yellow:Colors.black, size: 30),
          Icon(Icons.account_circle, color: currentTab==4?Colors.yellow:Colors.black, size: 30),
        ],
        color: Colors.yellow,
        buttonBackgroundColor: Colors.black,
        backgroundColor: Colors.black,
        animationCurve: Curves.bounceInOut,
        animationDuration: Duration(milliseconds: 200),
        onTap: (index) {
          setState(() {
            currentTab = index;
          });
        },
      ),
      body: Container(
        color: Colors.black,
        child: Center(
          child: Column(
            children: <Widget>[
              Text(_page.toString(), textScaleFactor: 10.0),
              RaisedButton(
                child: Text('Go To Page of index 1'),
                onPressed: () {
                  final CurvedNavigationBarState navBarState =
                      _bottomNavigationKey.currentState;
                  navBarState.setPage(1);
                },
              ),
            ],
          ),
        ),
      ),
      appBar: AppBar(
        backgroundColor: Colors.yellow,
        leading: Padding(
          child: GestureDetector(
            onTap: () {
              _scaffoldKey.currentState.openDrawer();
            },
            child: CircleAvatar(
              backgroundImage: ExactAssetImage('images/profile.jpg'),
              radius: 30,
            ),
          ),
          padding: const EdgeInsets.all(8.0),
        ),
        title: Text("Title"),
      ),
      drawer: DrawerCustom("Vogan", "@jbbo"),
      drawerEdgeDragWidth: 0,
    );
</code>```

on the setState methode, I'm giving currentTab the index's value. But the icons are not being refresh, the color stay the same. I have watched for fixedColor but the package doesnt not handle this kind of properties. Same for activeIcons inside a navigationbarItem, because items property of curvednavigationBar is a list of widget. So I can not use a navigationbarItem.

How can I fix this problem?

edit

Thought it was maybe because I didn't initialize state so I did add this:

```<code> @override
  void initState(){
    super.initState();
  }
</code>```

It is still not working, I don't get it. The onPressed is working well, when I click on an icon, I go inside the method, currentTab value is updated but the icon's colors stay unchanged.

答: Founded it ! It was because my variable currentTab was not a global variable of the class, i declare it inside the build method, that was a mistake </p>

Body:
I'm trying to use the curvedNavigationBar package. Here is my code :

 Widget build(BuildContext context) {
     int currentTab = 2;
    return Scaffold(
      key: _scaffoldKey,
      bottomNavigationBar: CurvedNavigationBar(
        key: _bottomNavigationKey,
        index: currentTab,
        height: 50.0,
        items: <Widget>[
          Icon(Icons.home, color:currentTab==0?Colors.yellow:Colors.black, size: 30),
          Icon(Icons.wifi_tethering, color:currentTab==1?Colors.yellow:Colors.black, size: 30),
          Icon(Icons.flag, color:currentTab==2?Colors.yellow:Colors.black, size: 65),
          Icon(Icons.mail, color:currentTab==3?Colors.yellow:Colors.black, size: 30),
          Icon(Icons.account_circle, color: currentTab==4?Colors.yellow:Colors.black, size: 30),
        ],
        color: Colors.yellow,
        buttonBackgroundColor: Colors.black,
        backgroundColor: Colors.black,
        animationCurve: Curves.bounceInOut,
        animationDuration: Duration(milliseconds: 200),
        onTap: (index) {
          setState(() {
            currentTab = index;
          });
        },
      ),
      body: Container(
        color: Colors.black,
        child: Center(
          child: Column(
            children: <Widget>[
              Text(_page.toString(), textScaleFactor: 10.0),
              RaisedButton(
                child: Text('Go To Page of index 1'),
                onPressed: () {
                  final CurvedNavigationBarState navBarState =
                      _bottomNavigationKey.currentState;
                  navBarState.setPage(1);
                },
              ),
            ],
          ),
        ),
      ),
      appBar: AppBar(
        backgroundColor: Colors.yellow,
        leading: Padding(
          child: GestureDetector(
            onTap: () {
              _scaffoldKey.currentState.openDrawer();
            },
            child: CircleAvatar(
              backgroundImage: ExactAssetImage('images/profile.jpg'),
              radius: 30,
            ),
          ),
          padding: const EdgeInsets.all(8.0),
        ),
        title: Text("Title"),
      ),
      drawer: DrawerCustom("Vogan", "@jbbo"),
      drawerEdgeDragWidth: 0,
    );

on the setState methode, I'm giving currentTab the index's value. But the icons are not being refresh, the color stay the same. I have watched for fixedColor but the package doesnt not handle this kind of properties. Same for activeIcons inside a navigationbarItem, because items property of curvednavigationBar is a list of widget. So I can not use a navigationbarItem.

How can I fix this problem?

edit

Thought it was maybe because I didn't initialize state so I did add this:

 @override
  void initState(){
    super.initState();
  }

It is still not working, I don't get it. The onPressed is working well, when I click on an icon, I go inside the method, currentTab value is updated but the icon's colors stay unchanged.


答:
You may re-organize the pattern a bit by including the right-hand delimiter and appending an optional (?:\s*.)?  group:

```<code>def findStringsInMiddle(a, b, text):
    return re.findall(re.escape(a)+"(.*?"+re.escape(b) + r"(?:\s*.)?)",text, flags=re.S)
</code>```

The pattern will look like ;(.*?,(?:\s*.)?) (see the regex demo) and will match:

;  - the left-hand delimiter

(.*?,(?:\s*.)?) - Group 1:

.*?  - any zero or more chars, as few as possible

, - a comma

(?:\s*.)? - an optional non-capturing group matching 1 or 0 occurrences of 0+ whitespaces and then any char.

Note I added re.S flag to make . match line break chars, too.

Seee full Python snippet below:

```<code>import re
example_string = "; One, one; Two, two; Three, three; Four, four"
desired_output = ["One, o", "Two, t", "Three, t", "Four, f"] #list output is OK

def findStringsInMiddle(a, b, text):
    return re.findall(re.escape(a)+"(.*?"+re.escape(b) + r"(?:\s*.)?)",text, flags=re.S)

desired_output = findStringsInMiddle('; ' , ',' , example_string)
print(desired_output)
# => ['One, o', 'Two, t', 'Three, t', 'Four, f']
</code>```

Title: Python - Regex (Re.Escape, Re.Findall); How To: Find sub-strings + a number of characters beyond the sub-strings within a string?

Body:
You may re-organize the pattern a bit by including the right-hand delimiter and appending an optional (?:\s*.)?  group:

def findStringsInMiddle(a, b, text):
    return re.findall(re.escape(a)+"(.*?"+re.escape(b) + r"(?:\s*.)?)",text, flags=re.S)

The pattern will look like ;(.*?,(?:\s*.)?) (see the regex demo) and will match:

;  - the left-hand delimiter

(.*?,(?:\s*.)?) - Group 1:

.*?  - any zero or more chars, as few as possible

, - a comma

(?:\s*.)? - an optional non-capturing group matching 1 or 0 occurrences of 0+ whitespaces and then any char.

Note I added re.S flag to make . match line break chars, too.

Seee full Python snippet below:

import re
example_string = "; One, one; Two, two; Three, three; Four, four"
desired_output = ["One, o", "Two, t", "Three, t", "Four, f"] #list output is OK

def findStringsInMiddle(a, b, text):
    return re.findall(re.escape(a)+"(.*?"+re.escape(b) + r"(?:\s*.)?)",text, flags=re.S)

desired_output = findStringsInMiddle('; ' , ',' , example_string)
print(desired_output)
# => ['One, o', 'Two, t', 'Three, t', 'Four, f']


问:
Referencing an external library in an Ant build
We have an ant build script for a project we're developing for a PDA.  In eclipse we have a load of referenced libraries and I know how to get them to work when we run the jar on the PDA because we have a .lnk file where you can add the external libraries simply by adding the following:

```<code>512#"\J9\PPRO11\bin\j9.exe" -jcl:ppro11 -cp "\dist\WiFiTest.jar;\placelab\lib\placelab.jar" j2medemo.wifi.WiFiTest
</code>```

Adding whatever libraries we need after the -cp argument.  However, I have no idea how to replicate this in the ant file (no idea is actually an exaggeration but after reading all the documentation I still don't understand it).

We have the following .

```<code><target name="jar" depends="prepare">
 <delete file="${dist}/${appname}.jar"/>
 <delete file="${dist}/MANIFEST.MF"/>
 <manifest file="${dist}/MANIFEST.MF">
    <attribute name="Built-By" value="${user.name}"/>
    <attribute name="Main-Class" value="j2medemo.GUI.MapPanel"/>
</manifest>
<jar destfile="${dist}/${appname}.jar"
     basedir="${classes}"
     includes="**/*.class, **/*.jpg, **/*.gif,**/*.png,**/*.xml"
     manifest="${dist}/MANIFEST.MF"
     />
</target>
</code>```

I have tried adding  everywhere I can think of but it doesn't help.  Can I just add it to the the include part?

Secondly, we have a run target:

```<code><target name="run" description="run" depends="jar">
    <exec dir="${j9bin}" executable="${j9bin}\j9.exe">
        <arg line="-jcl:ppro11 -cp '${dist}\${appname}.jar;placelab\lib\placelab.jar' ${mainclass}"/>
        <env key="JAVA_HOME" path="${j9home}"/>
    </exec>
</target>
</code>```

And I wanted to change it to run a different class so I created the following two targets:

```<code><target name="jarSAX" depends="prepare">
 <delete file="${dist}/SAXParse.jar"/>
 <delete file="${dist}/MANIFEST.MF"/>
 <manifest file="${dist}/MANIFEST.MF">
    <attribute name="Built-By" value="${user.name}"/>
    <attribute name="Main-Class" value="j2medemo.Routes.SAXParse"/>
</manifest>
<jar destfile="${dist}/SAXParse.jar"
     basedir="${classes}"
     includes="**/*.class, **/*.jpg, **/*.gif,**/*.png,**/*.xml"
     manifest="${dist}/MANIFEST.MF"
     />
</target>
</code>```

And

```<code><target name="runSAXParser" description="run" depends="jarSAX">
        <exec dir="${j9bin}" executable="${j9bin}\j9.exe">
            <arg line="-jcl:ppro11 -cp '${dist}\SAXParse.jar' j2medemo.Routes.SAXParse"/>
            <env key="JAVA_HOME" path="${j9home}"/>
        </exec>
    </target>
</code>```

But if I try and run this I get the following error:

```<code>Exception in thread "main" java.lang.NoClassDefFoundError: j2medemo.Routes.SAXParse
</code>```

If anyone has any ideas/solutions for either questions that would be most helpful.

答:
From what I can tell, you need to add the classpath elements to the manifest. Should look something like this:

```<code> <manifest file="${dist}/MANIFEST.MF">
    <attribute name="Built-By" value="${user.name}"/>
    <attribute name="Main-Class" value="j2medemo.Routes.SAXParse"/>
    <attribute name="Class-Path" value="WiFiTest.jar;placelab.jar"/>
 </manifest>
</code>```

The classpath entries should be relative to the location of the Jar in the distributable.  From there, you should be able to execute the jar directly (using the -jar command line option, I'm not sure what this is for J9).  Should be similar to the analogous Java call:

```<code>java -jar MainJar.jar
</code>```

OwnerDisplayName: Adam Taylor

Body:
We have an ant build script for a project we're developing for a PDA.  In eclipse we have a load of referenced libraries and I know how to get them to work when we run the jar on the PDA because we have a .lnk file where you can add the external libraries simply by adding the following:

512#"\J9\PPRO11\bin\j9.exe" -jcl:ppro11 -cp "\dist\WiFiTest.jar;\placelab\lib\placelab.jar" j2medemo.wifi.WiFiTest

Adding whatever libraries we need after the -cp argument.  However, I have no idea how to replicate this in the ant file (no idea is actually an exaggeration but after reading all the documentation I still don't understand it).

We have the following .

<target name="jar" depends="prepare">
 <delete file="${dist}/${appname}.jar"/>
 <delete file="${dist}/MANIFEST.MF"/>
 <manifest file="${dist}/MANIFEST.MF">
    <attribute name="Built-By" value="${user.name}"/>
    <attribute name="Main-Class" value="j2medemo.GUI.MapPanel"/>
</manifest>
<jar destfile="${dist}/${appname}.jar"
     basedir="${classes}"
     includes="**/*.class, **/*.jpg, **/*.gif,**/*.png,**/*.xml"
     manifest="${dist}/MANIFEST.MF"
     />
</target>

I have tried adding  everywhere I can think of but it doesn't help.  Can I just add it to the the include part?

Secondly, we have a run target:

<target name="run" description="run" depends="jar">
    <exec dir="${j9bin}" executable="${j9bin}\j9.exe">
        <arg line="-jcl:ppro11 -cp '${dist}\${appname}.jar;placelab\lib\placelab.jar' ${mainclass}"/>
        <env key="JAVA_HOME" path="${j9home}"/>
    </exec>
</target>

And I wanted to change it to run a different class so I created the following two targets:

<target name="jarSAX" depends="prepare">
 <delete file="${dist}/SAXParse.jar"/>
 <delete file="${dist}/MANIFEST.MF"/>
 <manifest file="${dist}/MANIFEST.MF">
    <attribute name="Built-By" value="${user.name}"/>
    <attribute name="Main-Class" value="j2medemo.Routes.SAXParse"/>
</manifest>
<jar destfile="${dist}/SAXParse.jar"
     basedir="${classes}"
     includes="**/*.class, **/*.jpg, **/*.gif,**/*.png,**/*.xml"
     manifest="${dist}/MANIFEST.MF"
     />
</target>

And

<target name="runSAXParser" description="run" depends="jarSAX">
        <exec dir="${j9bin}" executable="${j9bin}\j9.exe">
            <arg line="-jcl:ppro11 -cp '${dist}\SAXParse.jar' j2medemo.Routes.SAXParse"/>
            <env key="JAVA_HOME" path="${j9home}"/>
        </exec>
    </target>

But if I try and run this I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: j2medemo.Routes.SAXParse

If anyone has any ideas/solutions for either questions that would be most helpful.

Body:
From what I can tell, you need to add the classpath elements to the manifest. Should look something like this:

 <manifest file="${dist}/MANIFEST.MF">
    <attribute name="Built-By" value="${user.name}"/>
    <attribute name="Main-Class" value="j2medemo.Routes.SAXParse"/>
    <attribute name="Class-Path" value="WiFiTest.jar;placelab.jar"/>
 </manifest>

The classpath entries should be relative to the location of the Jar in the distributable.  From there, you should be able to execute the jar directly (using the -jar command line option, I'm not sure what this is for J9).  Should be similar to the analogous Java call:

java -jar MainJar.jar

OwnerDisplayName: James Van Huis


问:
How do I access subclass variables in a baseclass vector? (for an entity component system)
So I have a vector with "Components". I can access the Component variables easily from the vector, but when I add a subclass to the vector, I cant access those variables.

Maybe this is a really bold question and the solution is probably really complicated, but I don't know where to start and what to try so any help is appreciated.

```<code>struct Component {};
std::vector<Component> components;

struct Transform : Component
{
    int x, y;
};

void Start()
{
    components.push_back(Transform());
    //can't acces x below
    std::cout << components[0].x;
}
</code>```

SOLUTION

make the vector just for unique pointers:

```<code>std::vector<std::unique_ptr<Component>> components;
</code>```

step 2:

```<code>void Start()
{
    //add component (pointer to vector)
    components.push_back(std::make_unique<Transform>());
    //cast to Transform
    Transform const* transform = static_cast<Transform*>(components[0].get());

    std::cout << transform->x;
}
</code>```

答:
The problem is, that your vector stores objects of class Component.
What happens in push_back is, that the vector creates a new Component which is copied from your Transform() object.

So the first big thing to note here is, that you did not really push a Transform object into the vector, thus there is no such .x.

Note that in C++, just the line Transform x{}; creates an object on the stack. Thus Component y=x; creates another object and copies its data (which are none in this case, as Component is empty) from x.

And this is essentially what happens in your code, just a bit hidden with in the vector.

Body:
So I have a vector with "Components". I can access the Component variables easily from the vector, but when I add a subclass to the vector, I cant access those variables.

Maybe this is a really bold question and the solution is probably really complicated, but I don't know where to start and what to try so any help is appreciated.

struct Component {};
std::vector<Component> components;

struct Transform : Component
{
    int x, y;
};

void Start()
{
    components.push_back(Transform());
    //can't acces x below
    std::cout << components[0].x;
}

SOLUTION

make the vector just for unique pointers:

std::vector<std::unique_ptr<Component>> components;

step 2:

void Start()
{
    //add component (pointer to vector)
    components.push_back(std::make_unique<Transform>());
    //cast to Transform
    Transform const* transform = static_cast<Transform*>(components[0].get());

    std::cout << transform->x;
}


问:
Unable to get the desired result if password contains dollar ($) character while running openssl passwd -apr1 command
I am running the following command to generate the hash for a given password on the Linux terminal.

```<code>openssl passwd -apr1 'Test123$Pwd'
$apr1$lWLP.Qc0$f/YdAVqsYFglu1EnLvzUS.
</code>```

I have written a simple shell script that generates a hash for the given password. Here is my shell script:  (Should I modify anything here in shell script to include single quotes surrounding $1)

echo -n "$1" | openssl passwd -apr1 $1

Now, the problem is, when I call the same shell script from java program, for some reason if the password contains a dollar($) character, it is not working as expected.

Here is my java code which calls the above shell script:

```<code>void test(String password){
    String script = "sh test.sh %s";
    String command = String.format(script, password);
    - - -
    execute(command); // execute using ProcessBuilder
    - - -
 }
</code>```

The above code works fine if the password doesn't contain a dollar character. But, if the password contains $ char, it ignores the text after the $ symbol and generates a hash for the remaining password only up to $.

For example: If I pass the password as Test$Pwd

The hash is getting generated for only Test instead of Test$Pwd

I tried to set a single quote in java code as below but this also didn't work. When executed using java for some reason it still generates the hash for Test123 instead of Test123$Pwd

Modified java code to include a single quote as below:

```<code>"sh test.sh '%s'";
</code>```

Observed the followed command in logs which has single quotes, and this also didn't work when I ran shell script from java.

```<code>openssl passwd -apr1 'Test123$Pwd'
</code>```

From the Linux terminal when I run the script it works fine.

```<code>sh test.sh 'Test123$Pwd'  -- this works
sh test.sh Test123$Pwd    -- this doesn't work (generates hash only for Test123 even when run directly on the linux terminal)
</code>```

I am confused about whether I need to modify the shell script or something in java?

Any pointers would be helpful. Thanks!

答:
In shell scripts, double quotes is a way to get around spaces being seen as delimiters. It doesn't exclude variable substitution. So that's what you want here; single quotes would pass the literal $1 as in, the dollar, then the 1, and not 'substitute the first argument of the script here'. However, you pass $1 straight up without quotes for the second.

But, this is putting rather a lot of dependency on the exact shell in use and opens up doors to passwords with certain symbols messing up your program.

Why not forego the shell script entirely and call openssl directly? You can replace the standard in (and thus, send what echo is sending in your script), and the args passed via ProcessBuilder's List<String> / String... options (never use the single string option, really, it's hard to read and is asking java to attempt to turn spaces into separators, bad idea here) - as now you're passing the raw options without bash or whatever shell you have attempting to 'interpret' that password which you don't want it to do.

Or, better yet, why are you invoking a shell script?

apr1 is, if memory serves, the crypt key indicating 'MD5, done 1000 times'. This is a HORRIBLE, in that it is trivially rainbow tabled and broken, way of hashing passwords. It's also something you don't need openssl for at all, you can easily do that from java.

So, here's my advice:

Reconsider apr1. It's insecure as heck, you don't want it. Use bcrypt, scrypt, or pbkdf2. Probably bcrypt.

Stop farming this job out via an exec, do it in java. For example, use jBCrypt.

Body:
I am running the following command to generate the hash for a given password on the Linux terminal.

openssl passwd -apr1 'Test123$Pwd'
$apr1$lWLP.Qc0$f/YdAVqsYFglu1EnLvzUS.

I have written a simple shell script that generates a hash for the given password. Here is my shell script:  (Should I modify anything here in shell script to include single quotes surrounding $1)

echo -n "$1" | openssl passwd -apr1 $1

Now, the problem is, when I call the same shell script from java program, for some reason if the password contains a dollar($) character, it is not working as expected.

Here is my java code which calls the above shell script:

void test(String password){
    String script = "sh test.sh %s";
    String command = String.format(script, password);
    - - -
    execute(command); // execute using ProcessBuilder
    - - -
 }

The above code works fine if the password doesn't contain a dollar character. But, if the password contains $ char, it ignores the text after the $ symbol and generates a hash for the remaining password only up to $.

For example: If I pass the password as Test$Pwd

The hash is getting generated for only Test instead of Test$Pwd

I tried to set a single quote in java code as below but this also didn't work. When executed using java for some reason it still generates the hash for Test123 instead of Test123$Pwd

Modified java code to include a single quote as below:

"sh test.sh '%s'";

Observed the followed command in logs which has single quotes, and this also didn't work when I ran shell script from java.

openssl passwd -apr1 'Test123$Pwd'

From the Linux terminal when I run the script it works fine.

sh test.sh 'Test123$Pwd'  -- this works
sh test.sh Test123$Pwd    -- this doesn't work (generates hash only for Test123 even when run directly on the linux terminal)

I am confused about whether I need to modify the shell script or something in java?

Any pointers would be helpful. Thanks!


问:
How to parse the AWS S3 Path (s3://<bucket name>/<key>) using the AWSSDK.S3 in C# in order to get the bucket name & key
I have a s3 path => s3://[bucket name]/[key]

```<code>s3://bn-complete-dev-test/1234567890/renders/Irradiance_A.png
</code>```

and I need get the bucket_name and the key separately:

```<code>var s3PathParsed = parseS3Path("s3://bn-complete-dev-test/1234567890/renders/Irradiance_A.png");

s3PathParsed.BucketName == "bn-complete-dev-test"
s3PathParsed.Key == "1234567890/renders/Irradiance_A.png"
</code>```

how to I could parse in the correct way using the AWS SDK?

1) I am parsing manually (using a regular expression) and work fine but i am not comfortable:

```<code>public class S3Path : IS3Path
{
    private const string _s3PathRegex = @"[s|S]3:\/\/(?<bucket>[^\/]*)\/(?<key>.*)";

    public S3Path(string s3Path)
    {
        Path = s3Path;

        var rx = new Regex(_s3PathRegex).Match(s3Path);

        if (!rx.Success || rx.Groups.Count != 3)
            throw new Exception($"the S3 Path '{s3Path}' is wrong.");

        BucketName = rx.Groups[1].Value;
        Key = rx.Groups[2].Value;
    }

    public string Path { get; }

    public string BucketName { get; }

    public string Key { get; }
}
</code>```

2) I used the AmazonS3Uri from AWWSDK.S3:

```<code>string GetBucketNameFromS3Uri(string s3Uri)
{
    return new AmazonS3Uri(s3Uri).Bucket;
}
</code>```

I called the method:

```<code>GetBucketNameFromS3Uri("s3://sunsite-complete-dev-test/1234567890/renders/Irradiance_A.png");
</code>```

and i have the following error:

System.ArgumentException: 'Invalid S3 URI - hostname does not appear to be a valid S3 endpoint'

3) Also I try

```<code>string GetBucketNameFromS3Uri(string s3Uri)
{
    return new AmazonS3Uri(new Uri(s3Uri)).Bucket;
}
</code>```

with the same error.

I created a new thread in AWS Forum with this issue: https://forums.aws.amazon.com/thread.jspa?threadID=304401

答:
If you have an object URL (https://bn-complete-dev-test.s3.eu-west-2.amazonaws.com/1234567890/renders/Irradiance_A.pnlet), you can use AmazonS3Uri:

// using Amazon.S3.Util

var uri = new AmazonS3Uri(urlString);

var bucketName = uri.Bucket;
var key = uri.Key;
```
<p>If you have an S3 URI (<code>s3://bn-complete-dev-test/1234567890/renders/Irradiance_A.png</code>) then it is a bit more involved:</p>
<pre class="lang-cs prettyprint-override"><code>using System;

public static class S3
{
    public static Tuple<string, string> TryParseS3Uri(string x)
    {
        try
        {
            var uri = new Uri(x);

            if (uri.Scheme == "s3")
            {
                var bucket = uri.Host;
                var key = uri.LocalPath.Substring(1);

                return new Tuple<string, string>(bucket, key);
            }

            return null;
        }
        catch (Exception ex)
        {
            var ex2 = ex as UriFormatException;

            if (ex2 == null)
            {
                throw ex;
            }

            return null;
        }
    }
}
</code>```

Here's an F# version:

open System

let tryParseS3Uri (x : string) =
  try
    let uri = Uri x

    if uri.Scheme = "s3"
    then
      let bucket = uri.Host
      let key = uri.LocalPath.Substring 1

      Some (bucket, key)
    else
      None

  with
    | :? UriFormatException -> None
    | exn -> raise exn
```

Body:
I have a s3 path => s3://[bucket name]/[key]

s3://bn-complete-dev-test/1234567890/renders/Irradiance_A.png

and I need get the bucket_name and the key separately:

var s3PathParsed = parseS3Path("s3://bn-complete-dev-test/1234567890/renders/Irradiance_A.png");

s3PathParsed.BucketName == "bn-complete-dev-test"
s3PathParsed.Key == "1234567890/renders/Irradiance_A.png"

how to I could parse in the correct way using the AWS SDK?

1) I am parsing manually (using a regular expression) and work fine but i am not comfortable:

public class S3Path : IS3Path
{
    private const string _s3PathRegex = @"[s|S]3:\/\/(?<bucket>[^\/]*)\/(?<key>.*)";

    public S3Path(string s3Path)
    {
        Path = s3Path;

        var rx = new Regex(_s3PathRegex).Match(s3Path);

        if (!rx.Success || rx.Groups.Count != 3)
            throw new Exception($"the S3 Path '{s3Path}' is wrong.");

        BucketName = rx.Groups[1].Value;
        Key = rx.Groups[2].Value;
    }

    public string Path { get; }

    public string BucketName { get; }

    public string Key { get; }
}

2) I used the AmazonS3Uri from AWWSDK.S3:

string GetBucketNameFromS3Uri(string s3Uri)
{
    return new AmazonS3Uri(s3Uri).Bucket;
}

I called the method:

GetBucketNameFromS3Uri("s3://sunsite-complete-dev-test/1234567890/renders/Irradiance_A.png");

and i have the following error:

System.ArgumentException: 'Invalid S3 URI - hostname does not appear to be a valid S3 endpoint'

3) Also I try

string GetBucketNameFromS3Uri(string s3Uri)
{
    return new AmazonS3Uri(new Uri(s3Uri)).Bucket;
}

with the same error.

I created a new thread in AWS Forum with this issue: https://forums.aws.amazon.com/thread.jspa?threadID=304401

Body:
If you have an object URL (https://bn-complete-dev-test.s3.eu-west-2.amazonaws.com/1234567890/renders/Irradiance_A.pnlet), you can use AmazonS3Uri:

// using Amazon.S3.Util

var uri = new AmazonS3Uri(urlString);

var bucketName = uri.Bucket;
var key = uri.Key;

If you have an S3 URI (s3://bn-complete-dev-test/1234567890/renders/Irradiance_A.png) then it is a bit more involved:

using System;

public static class S3
{
    public static Tuple<string, string> TryParseS3Uri(string x)
    {
        try
        {
            var uri = new Uri(x);

            if (uri.Scheme == "s3")
            {
                var bucket = uri.Host;
                var key = uri.LocalPath.Substring(1);

                return new Tuple<string, string>(bucket, key);
            }

            return null;
        }
        catch (Exception ex)
        {
            var ex2 = ex as UriFormatException;

            if (ex2 == null)
            {
                throw ex;
            }

            return null;
        }
    }
}

Here's an F# version:

open System

let tryParseS3Uri (x : string) =
  try
    let uri = Uri x

    if uri.Scheme = "s3"
    then
      let bucket = uri.Host
      let key = uri.LocalPath.Substring 1

      Some (bucket, key)
    else
      None

  with
    | :? UriFormatException -> None
    | exn -> raise exn


问:
In NHibernate ID Generator section in mapping files, What is the meaning of assigned and select?
In NHibernate ID Generator section in mapping files, What is the meaning of assigned and select?

答:
There's identity/sequence and assigned. From the NH documentation:

5.1.5.1. generator

For databases which support identity
  columns (DB2, MySQL, Sybase, MS SQL),
  you may use identity key generation.
  For databases that support sequences
  (DB2, Oracle, PostgreSQL, Interbase,
  McKoi, SAP DB) you may use sequence
  style key generation. Both these
  strategies require two SQL queries to
  insert a new object.

                  uid_sequence

            For cross-platform development,
  the native strategy will choose from
  the identity, sequence and hilo
  strategies, dependent upon the
  capabilities of the underlying
  database.

5.1.5.7. Assigned Identifiers

If you want the application to assign
  identifiers (as opposed to having
  NHibernate generate them), you may use
  the assigned generator. This special
  generator will use the identifier
  value already assigned to the object's
  identifier property. Be very careful
  when using this feature to assign keys
  with business meaning (almost always a
  terrible design decision).

Due to its inherent nature, entities
  that use this generator cannot be
  saved via the ISession's
  SaveOrUpdate() method. Instead you
  have to explicitly specify to
  NHibernate if the object should be
  saved or updated by calling either the
  Save() or Update() method of the
  ISession.


问:
Memory efficient way for list comprehension of pandas dataframe using multiple columns
I want to run a function on rows of a pandas dataframe in list comprehension.
Dataframe can have varying number of columns.
How to make use these columns of dataframe?

import  pandas as pd

df = {'chrom': ['chr1', 'chr1','chr1'], 'start': [10000, 10100, 12000], 'end':[10150,10120,12250], 'S1':[1, 1, 1],'S2':[2, 2, 2],'S3':[3, 3, 3] }
df = pd.DataFrame(data=df)
print(df)

def func(row):
    print(row)

[func(row) for row in zip(df['chrom'],df['start'],df['S1'],df['S2'],df['S3'])]
```

How to do this in a memory efficient way? So that we do not get any memory error for big dataframes.

答:
The shown code is extremely memory efficient, and should be faster than an iterrow based solution.

But from your comment, it is not the code that causes the memory error... The problematic codes are:

```<code>df[list(df.columns.values)].values()
</code>```

or:

```<code>df[list(df.columns.values)].to_numpy(copy=False)
</code>```

because both involves a full copy of the dataframe values unless all columns have the same dtype.

If you want to process an unknown number of columns, the safe way is:

```<code>[func(row) for row in zip([df[i].values for i in df.columns])]
</code>```

No copy is required here because df[i].values will return the underlying numpy arrays.

By the way, if you only need to use once the values of the returned list you could even save some memory by using a generator instead of a list:

```<code>(func(row) for row in zip([df[i].values for i in df.columns]))
</code>```

Body:
I want to run a function on rows of a pandas dataframe in list comprehension.
Dataframe can have varying number of columns.
How to make use these columns of dataframe?

import  pandas as pd

df = {'chrom': ['chr1', 'chr1','chr1'], 'start': [10000, 10100, 12000], 'end':[10150,10120,12250], 'S1':[1, 1, 1],'S2':[2, 2, 2],'S3':[3, 3, 3] }
df = pd.DataFrame(data=df)
print(df)

def func(row):
    print(row)

[func(row) for row in zip(df['chrom'],df['start'],df['S1'],df['S2'],df['S3'])]

How to do this in a memory efficient way? So that we do not get any memory error for big dataframes.

Body:
The shown code is extremely memory efficient, and should be faster than an iterrow based solution.

But from your comment, it is not the code that causes the memory error... The problematic codes are:

df[list(df.columns.values)].values()

or:

df[list(df.columns.values)].to_numpy(copy=False)

because both involves a full copy of the dataframe values unless all columns have the same dtype.

If you want to process an unknown number of columns, the safe way is:

[func(row) for row in zip([df[i].values for i in df.columns])]

No copy is required here because df[i].values will return the underlying numpy arrays.

By the way, if you only need to use once the values of the returned list you could even save some memory by using a generator instead of a list:

(func(row) for row in zip([df[i].values for i in df.columns]))


问:
Correct way to loop through std::array<char*, N>
Consider the following std::array of pointers to char:

std::array<char*, 10> m_arr;
```

<p>I know I can loop through the array using the following code</p>

<pre class="lang-c prettyprint-override"><code>for(size_t i {0}; i < m_arr.size(); i++) {
            std::cout << m_arr.at(i) << std::endl;
    }
</code>```

But this approach throws "Access violation reading location" exception, when the i-th element is not assigned correctly. For example the following code assigns the first two elements but the third element, m_arr.at(3), raises the aforementioned error:

    // test variables
    int x {100};
    double y {3.14};
    int* x_ptr {&x};
    double* y_ptr {&y};

    // std::array of pointer to char
    std::array<char*, 10> m_arr;

    // set the first two elements of m_arr
    char buf1[16];
    sprintf_s(buf1, "%p", x_ptr);
    m_arr.at(0) = buf1;
    char buf2[16];
    sprintf_s(buf2, "%p", y_ptr);
    m_arr.at(1) = buf2;

    for(size_t i {0}; i < m_arr.size(); i++) {
            std::cout << m_arr.at(i) << std::endl;
    }

```

<p>I found out a quick fix around this by checking the i-th element with the last element of the array to skip unassigned elements but obviously that's not a clean answer</p>

<pre class="lang-c prettyprint-override"><code>for(size_t i {0}; i < m_arr.size(); i++) {
        if(m_arr.at(i) != m_arr.back()) {
            std::cout << m_arr.at(i) << std::endl;
        }
    }
</code>```

I believe there is a better way to loop through this array and avoid the error. Thank you in advance for your help.

答:
I'm more of a fan of this type of iteration:

```<code>#include <iostream>
#include <array>
using namespace std;
// ...
for (auto&& object : m_arr)
{
    cout << (object != nullptr ? object : "Null pointer") << endl;
}
</code>```

Don't forget to initialize your array too, it'll save you some segmentation faults:

```<code>std::array<char*, 10> m_arr {};
</code>```

Body:
Consider the following std::array of pointers to char:

std::array<char*, 10> m_arr;

I know I can loop through the array using the following code

for(size_t i {0}; i < m_arr.size(); i++) {
            std::cout << m_arr.at(i) << std::endl;
    }

But this approach throws "Access violation reading location" exception, when the i-th element is not assigned correctly. For example the following code assigns the first two elements but the third element, m_arr.at(3), raises the aforementioned error:

    // test variables
    int x {100};
    double y {3.14};
    int* x_ptr {&x};
    double* y_ptr {&y};

    // std::array of pointer to char
    std::array<char*, 10> m_arr;

    // set the first two elements of m_arr
    char buf1[16];
    sprintf_s(buf1, "%p", x_ptr);
    m_arr.at(0) = buf1;
    char buf2[16];
    sprintf_s(buf2, "%p", y_ptr);
    m_arr.at(1) = buf2;

    for(size_t i {0}; i < m_arr.size(); i++) {
            std::cout << m_arr.at(i) << std::endl;
    }

I found out a quick fix around this by checking the i-th element with the last element of the array to skip unassigned elements but obviously that's not a clean answer

for(size_t i {0}; i < m_arr.size(); i++) {
        if(m_arr.at(i) != m_arr.back()) {
            std::cout << m_arr.at(i) << std::endl;
        }
    }

I believe there is a better way to loop through this array and avoid the error. Thank you in advance for your help.

Body:
I'm more of a fan of this type of iteration:

#include <iostream>
#include <array>
using namespace std;
// ...
for (auto&& object : m_arr)
{
    cout << (object != nullptr ? object : "Null pointer") << endl;
}

Don't forget to initialize your array too, it'll save you some segmentation faults:

std::array<char*, 10> m_arr {};


问:
Yii2 ActiveForm TextField Number Max
I have fields like below

```<code><?= $form->field($model, 'phone')
->textInput(['type' => 'number', 'maxlength' => 13])
->label('Phone')
?>
</code>```

why the 'maxlength' is not work? and how to make it work?

thank you before

答: try for  type number -> 'type'=>'number', 'min' => 1, 'max' => 999 </p>

Title: Yii2 ActiveForm TextField Number Max

Body:
I have fields like below

<?= $form->field($model, 'phone')
->textInput(['type' => 'number', 'maxlength' => 13])
->label('Phone')
?>

why the 'maxlength' is not work? and how to make it work?

thank you before


问:
Create your own data structure class or use existing implementation?
So I've been going through data structure, I can create my own linkedlist class with its functions ( insert, remove, length etc ) but why should I do that when I can simply do the following</p>

```<code>LinkedList<>list = new LinkedList<>();
</code>```

答:
```<code>Generally, this question asked in an interview, design the functionality for LinkedList.
If you have deep knowledge of LinkedList, then you can design the LinkedList for different languages.
Custom LinkedList implementation will check your deep understanding, how it works internally.
</code>```

Body:
So I've been going through data structure, I can create my own linkedlist class with its functions ( insert, remove, length etc ) but why should I do that when I can simply do the following

LinkedList<>list = new LinkedList<>();