问:
Unable to find bundled Java version on Flutter
I have Android Studio version 3.0. When I use the command flutter doctor it shows me the following:

Unable to find bundled Java version

My current Java version is (build 1.8.0_131-b11) on Windows 10.

I don't have any older versions of Android Studio on my computer.

答:
I was getting error as below,

Execute below steps to resolve this issue,

Visit Google folder in user directory

Delete the AndroidStudio4.1 directory ( For which I was getting error )

Run "flutter doctor" again. My problem was fixed


来源: stackexchange

问:
In JavaScript, does using await inside a loop block the loop?
Take the following loop:

```<code>for(var i=0; i<100; ++i){
    let result = await some_slow_async_function();
    do_something_with_result();
}
</code>```

Does await block the loop? Or does the i continue to be incremented while awaiting?

Is the order of do_something_with_result() guaranteed sequential with regard to i? Or does it depend on how fast the awaited function is for each i?

答:
Let me clarify a bit because some answers here have some wrong information about how Promise execution works, specifically when related to the event loop.

In the case of the example, await will block the loop. do_something_with_result() will not be called until await finishes it's scheduled job.

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await#handling_asyncawait_slowdown

As for the other points, Promise "jobs" run before the next event loop cycle, as microtasks. When you call Promise.then() or the resolve() function inside new Promise((resolve) => {}), you creating a Job. Both await and async are wrapper, of sorts, for Promise, that will both create a Job. Microtasks are meant to run before the next event loop cycle. That means adding a Promise Job means more work before it can move on to the next event loop cycle.

Here's an example how you can lock up your event loop because your promises (Jobs) take too long.

let tick = 0;
let time = performance.now();
setTimeout(() => console.log('Hi from timeout'), 0);
const tock = () => console.log(tick++);
const longTask = async () => {
  console.log('begin task');
  for(let i = 0; i < 1_000_000_000; i++) {
    Math.sqrt(i);
  }
  console.log('done task');
}
requestAnimationFrame(()=> console.log('next frame after', performance.now() - time, 'ms'));
async function run() {
  await tock();
  await tock();
  await longTask(); // Will stall your UI
  await tock();   // Will execute even though it's already dropped frames
  await tock();   // This will execute too
}
run();
// Promise.resolve().then(tock).then(tock).then(longTask).then(tock).then(tock);
```
<p>In this sample, 5 total promises are created. 2 calls for <code>tock</code>, 1 for <code>longTask</code> and then 2 calls for <code>tock</code>. All 5 will run before the next event loop.</p>
<p>The execution would be:</p>
<ul>
<li>Start JS execution</li>
<li>Execute normal script</li>
<li>Run 5 scheduled Promise jobs</li>
<li>End JS execution</li>
<li>Event Loop Cycle Start</li>
<li>Request Animation Frame fire</li>
<li>Timeout fire</li>
</ul>
<p>The last line commented line is scheduling without <code>async</code>/<code>await</code> and results in the same.</p>
<p>Basically, you will stall the next event loop cycle unless you tell your JS execution where it can suspend. Your Promise jobs will continue to run in the current event loop run until <strong>it finishes its call stack</strong>. When you call something external, (like <code>fetch</code>), then it's likely using letting the call stack end and has a callback that will resolve the pending Promise. Like this:</p>
<pre class="lang-js prettyprint-override"><code>function waitForClick() {
  return new Promise((resolve) => {
    // Use an event as a callback;
    button.onclick = () => resolve();
    // Let the call stack finish by implicitly not returning anything, or explicitly returning `undefined` (same thing).
    // return undefined;
  })
}
</code>```

If you have a long job job that want to complete, either use a Web Worker to run it without pausing, or insert some pauses with something like setTimeout() or setImmediate().

Reshaping the longTask function, you can do something like this:

const longTask = async () => {
  console.log('begin task');
  for(let i = 0; i < 1_000_000_000; i++)
    if (i && i % (10_000_000) === 0) {
      await new Promise((r) => setTimeout(r,0));
    }
    Math.sqrt(i);
  console.log('done task');
}
```

Basically, instead of doing 1 billion records in one shot, you only do 10 million and then wait until the next event (setTimeout) to run the next one. The bad here is it's slower because of how much you hand back to the event loop. Instead, you can use requestIdleCallback() which is better, but still not as good as multi-threading via Web Workers.

But be aware that just slapping on await or Promise.resolve().then() around a function won't help with the event loop. Both will wait until the function returns with either a Promise or a value before letting up for the event loop. You can mostly test by checking to see if the function you're calling returns an unresolved Promise immediately.

ContentLicense: CC BY-SA 4.0

Body:
Take the following loop:

for(var i=0; i<100; ++i){
    let result = await some_slow_async_function();
    do_something_with_result();
}

Does await block the loop? Or does the i continue to be incremented while awaiting?

Is the order of do_something_with_result() guaranteed sequential with regard to i? Or does it depend on how fast the awaited function is for each i?

Body:
Let me clarify a bit because some answers here have some wrong information about how Promise execution works, specifically when related to the event loop.

In the case of the example, await will block the loop. do_something_with_result() will not be called until await finishes it's scheduled job.

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await#handling_asyncawait_slowdown

As for the other points, Promise "jobs" run before the next event loop cycle, as microtasks. When you call Promise.then() or the resolve() function inside new Promise((resolve) => {}), you creating a Job. Both await and async are wrapper, of sorts, for Promise, that will both create a Job. Microtasks are meant to run before the next event loop cycle. That means adding a Promise Job means more work before it can move on to the next event loop cycle.

Here's an example how you can lock up your event loop because your promises (Jobs) take too long.

let tick = 0;
let time = performance.now();
setTimeout(() => console.log('Hi from timeout'), 0);
const tock = () => console.log(tick++);
const longTask = async () => {
  console.log('begin task');
  for(let i = 0; i < 1_000_000_000; i++) {
    Math.sqrt(i);
  }
  console.log('done task');
}
requestAnimationFrame(()=> console.log('next frame after', performance.now() - time, 'ms'));
async function run() {
  await tock();
  await tock();
  await longTask(); // Will stall your UI
  await tock();   // Will execute even though it's already dropped frames
  await tock();   // This will execute too
}
run();
// Promise.resolve().then(tock).then(tock).then(longTask).then(tock).then(tock);

In this sample, 5 total promises are created. 2 calls for tock, 1 for longTask and then 2 calls for tock. All 5 will run before the next event loop.

The execution would be:

Start JS execution

Execute normal script

Run 5 scheduled Promise jobs

End JS execution

Event Loop Cycle Start

Request Animation Frame fire

Timeout fire

The last line commented line is scheduling without async/await and results in the same.

Basically, you will stall the next event loop cycle unless you tell your JS execution where it can suspend. Your Promise jobs will continue to run in the current event loop run until it finishes its call stack. When you call something external, (like fetch), then it's likely using letting the call stack end and has a callback that will resolve the pending Promise. Like this:

function waitForClick() {
  return new Promise((resolve) => {
    // Use an event as a callback;
    button.onclick = () => resolve();
    // Let the call stack finish by implicitly not returning anything, or explicitly returning `undefined` (same thing).
    // return undefined;
  })
}

If you have a long job job that want to complete, either use a Web Worker to run it without pausing, or insert some pauses with something like setTimeout() or setImmediate().

Reshaping the longTask function, you can do something like this:

const longTask = async () => {
  console.log('begin task');
  for(let i = 0; i < 1_000_000_000; i++)
    if (i && i % (10_000_000) === 0) {
      await new Promise((r) => setTimeout(r,0));
    }
    Math.sqrt(i);
  console.log('done task');
}

Basically, instead of doing 1 billion records in one shot, you only do 10 million and then wait until the next event (setTimeout) to run the next one. The bad here is it's slower because of how much you hand back to the event loop. Instead, you can use requestIdleCallback() which is better, but still not as good as multi-threading via Web Workers.

But be aware that just slapping on await or Promise.resolve().then() around a function won't help with the event loop. Both will wait until the function returns with either a Promise or a value before letting up for the event loop. You can mostly test by checking to see if the function you're calling returns an unresolved Promise immediately.


问:
What is the proper design pattern for a callback in destructor which uses a pointer which may or may not point to valid object?
Let's say we have the following scenario.

We have a ImageManager class which is used to internally store and manage image data.
The ImageManager class has a public member populateImage, which will read an image into memory, then return a populated MyImage which is a std::shared_ptr around an Image object. This object will contain a uuid which maps to the image data which is managed by ImageManager.

Finally, we define a callback function which gets called in the Image destructor and properly cleans up the image data stored by the ImageManager.

Here is the example:

```<code>#include <functional>
#include <vector>
#include <unordered_map>
#include <memory>

class Image {
public:
    Image() = default;
    ~Image(){
        if (m_deleter) {
            m_deleter();
        }
    }
private:
    friend class ImageManager;
    std::function<void()> m_deleter = nullptr;
    unsigned int m_uuid;
};

using MyImage = std::shared_ptr<Image>;

class ImageManager {
public:
    void removeImage(unsigned int uuid) {
        auto iter = m_imageMap.find(uuid);
        if (iter == m_imageMap.end()) {
            throw std::runtime_error("Unable to find image for UUID: " + std::to_string(uuid));
        }
        m_imageMap.erase(iter);
    }

    void populateImage(MyImage& image) {
        image = std::make_shared<Image>();

        static unsigned int uuid = 0;
        ++uuid;

        image->m_uuid = uuid;

        auto img = std::vector<uint8_t>(1000, 0);
        m_imageMap[uuid] = img;

        unsigned int currentUUID = uuid;
        auto callbackFunc = [this, currentUUID]() {
            this->removeImage(currentUUID);
        };

        image->m_deleter = callbackFunc;
    }
private:
    std::unordered_map<unsigned int, std::vector<uint8_t>> m_imageMap;
};

</code>```

The issues arise when our instance of the ImageManager falls out of scope before our instances of Image, for example in the following driver code:

```<code>int main() {
    MyImage img1, img2;
    {
        ImageManager manager;
        manager.populateImage(img1);
        manager.populateImage(img2);
    }
}
</code>```

Running this program prints:

```<code>terminate called after throwing an instance of 'std::runtime_error'
  what():  Unable to find image for UUID: 2

</code>```

But ultimately I understand that this is undefined behavior as the this pointer in m_deleter no longer points to a valid object.

What is the proper design pattern in order to avoid this problem?

答:
Overall the design smells. If the ImageManager is a local object, and MyImage are outside of the scope, why do you need to delete the items in the map?

Anyway, I promised you to show the shared/weak idiom. Wrap the map into a shared_ptr. That would mean that the map will be destroyed together with the ImageManager (if you wouldn't copy this shared pointer):

std::shared_ptr<std::unordered_map<unsigned int, cv::Mat>> m_imageMap;
```
<p>Store a weak pointer in the lambda:</p>
<pre class="lang-cpp prettyprint-override"><code>    std::weak_ptr<std::unordered_map<unsigned int, cv::Mat>> weakPtr = m_imageMap;
    auto callbackFunc = [weakPtr, uuid]() {
        auto imageMap = weakPtr.lock();
        if (imageMap)
            auto iter = imageMap.find(uuid);
            if (iter == imageMap.end()) {
                throw std::runtime_error("Unable to find image for UUID: " + std::to_string(uuid));
            }
            imageMap.erase(iter);
        }
    };
    image->m_deleter = callbackFunc;
</code>```

Weak pointer will know whether the shared pointer counterpart is destroyed or not.

Keep in mind that you shouldn't use std::make_shared in this case: otherwise the memory associated with the map will be freed only when the last image is destroyed.

Body:
Let's say we have the following scenario.

We have a ImageManager class which is used to internally store and manage image data.
The ImageManager class has a public member populateImage, which will read an image into memory, then return a populated MyImage which is a std::shared_ptr around an Image object. This object will contain a uuid which maps to the image data which is managed by ImageManager.

Finally, we define a callback function which gets called in the Image destructor and properly cleans up the image data stored by the ImageManager.

Here is the example:

#include <functional>
#include <vector>
#include <unordered_map>
#include <memory>

class Image {
public:
    Image() = default;
    ~Image(){
        if (m_deleter) {
            m_deleter();
        }
    }
private:
    friend class ImageManager;
    std::function<void()> m_deleter = nullptr;
    unsigned int m_uuid;
};

using MyImage = std::shared_ptr<Image>;

class ImageManager {
public:
    void removeImage(unsigned int uuid) {
        auto iter = m_imageMap.find(uuid);
        if (iter == m_imageMap.end()) {
            throw std::runtime_error("Unable to find image for UUID: " + std::to_string(uuid));
        }
        m_imageMap.erase(iter);
    }

    void populateImage(MyImage& image) {
        image = std::make_shared<Image>();

        static unsigned int uuid = 0;
        ++uuid;

        image->m_uuid = uuid;

        auto img = std::vector<uint8_t>(1000, 0);
        m_imageMap[uuid] = img;

        unsigned int currentUUID = uuid;
        auto callbackFunc = [this, currentUUID]() {
            this->removeImage(currentUUID);
        };

        image->m_deleter = callbackFunc;
    }
private:
    std::unordered_map<unsigned int, std::vector<uint8_t>> m_imageMap;
};

The issues arise when our instance of the ImageManager falls out of scope before our instances of Image, for example in the following driver code:

int main() {
    MyImage img1, img2;
    {
        ImageManager manager;
        manager.populateImage(img1);
        manager.populateImage(img2);
    }
}

Running this program prints:

terminate called after throwing an instance of 'std::runtime_error'
  what():  Unable to find image for UUID: 2

But ultimately I understand that this is undefined behavior as the this pointer in m_deleter no longer points to a valid object.

What is the proper design pattern in order to avoid this problem?

Body:
Overall the design smells. If the ImageManager is a local object, and MyImage are outside of the scope, why do you need to delete the items in the map?

Anyway, I promised you to show the shared/weak idiom. Wrap the map into a shared_ptr. That would mean that the map will be destroyed together with the ImageManager (if you wouldn't copy this shared pointer):

std::shared_ptr<std::unordered_map<unsigned int, cv::Mat>> m_imageMap;

Store a weak pointer in the lambda:

    std::weak_ptr<std::unordered_map<unsigned int, cv::Mat>> weakPtr = m_imageMap;
    auto callbackFunc = [weakPtr, uuid]() {
        auto imageMap = weakPtr.lock();
        if (imageMap)
            auto iter = imageMap.find(uuid);
            if (iter == imageMap.end()) {
                throw std::runtime_error("Unable to find image for UUID: " + std::to_string(uuid));
            }
            imageMap.erase(iter);
        }
    };
    image->m_deleter = callbackFunc;

Weak pointer will know whether the shared pointer counterpart is destroyed or not.

Keep in mind that you shouldn't use std::make_shared in this case: otherwise the memory associated with the map will be freed only when the last image is destroyed.


问:
Mongo DB show all collections except one
in Mongo DB how can I retrieve all documents except one which is Test

my code shows all the documents

```<code>db.getCollectionNames().forEach(function(collection) {
    var result = db[collection];
    if(result != 'Test') {
        print("All the documents: " + " for collection: "+ collection);
    }
});
</code>```

答:
The correct is:</p>
```<code>  db.getCollectionNames().forEach(function(collection){
     if(collection != 'Test') {
         print("All the documents: " + " for collection: "+ collection);
        }
       }
      );
</code>```

Body:
in Mongo DB how can I retrieve all documents except one which is Test

my code shows all the documents

db.getCollectionNames().forEach(function(collection) {
    var result = db[collection];
    if(result != 'Test') {
        print("All the documents: " + " for collection: "+ collection);
    }
});

Body:
The correct is:

  db.getCollectionNames().forEach(function(collection){
     if(collection != 'Test') {
         print("All the documents: " + " for collection: "+ collection);
        }
       }
      );


问:
Is there any way to represent any number as sum of 4 squares?
Is there any way to represent any number as sum of 4 squares.

For example 29 can be represented as 5^2+2^2+0^2+0^2

I tried the following code but some numbers giving 5terms for example 23 as 4^2+2^2+1^2+1^2+1^2

the code i tried is :

```<code> x=0;
 while(num!=0){
          x=(int)Math.floor(Math.sqrt(num));
         num=num-(x*x);
}
</code>```

答:
Here is the algorithm code for you

This will give you all possible combinations ...

```<code>int n, t1, t2, t;
        n = 29;//Your number

        for (int i = (int) Math.sqrt(n / 4); i * i <= n; i++) {
            t1 = n - i * i;
            for (int j = (int) Math.sqrt(t1 / 3); j <= i && j * j <= t1; j++) {
                t2 = t1 - j * j;
                for (int k = (int) Math.sqrt(t2 / 2); k <= j && k * k <= t2; k++) {
                    t = (int) Math.sqrt(t2 - k * k);
                    if (t <= k && t * t == t2 - k * k) {
                        System.out.println("(" + i + "^2) + (" + j + "^2) + ("+ k + "^2) + ("+ t +"^2)");
                    }
                }
            }
        }
</code>```

ContentLicense: CC BY-SA 3.0

Body:
Is there any way to represent any number as sum of 4 squares.

For example 29 can be represented as 5^2+2^2+0^2+0^2

I tried the following code but some numbers giving 5terms for example 23 as 4^2+2^2+1^2+1^2+1^2

the code i tried is :

 x=0;
 while(num!=0){
          x=(int)Math.floor(Math.sqrt(num));
         num=num-(x*x);
}

Body:
Here is the algorithm code for you

This will give you all possible combinations ...

int n, t1, t2, t;
        n = 29;//Your number

        for (int i = (int) Math.sqrt(n / 4); i * i <= n; i++) {
            t1 = n - i * i;
            for (int j = (int) Math.sqrt(t1 / 3); j <= i && j * j <= t1; j++) {
                t2 = t1 - j * j;
                for (int k = (int) Math.sqrt(t2 / 2); k <= j && k * k <= t2; k++) {
                    t = (int) Math.sqrt(t2 - k * k);
                    if (t <= k && t * t == t2 - k * k) {
                        System.out.println("(" + i + "^2) + (" + j + "^2) + ("+ k + "^2) + ("+ t +"^2)");
                    }
                }
            }
        }


问:
How to stop resizing of widgets when resizing browser page in Flutter Web
How do you stop the automatic resizing/responsiveness of widgets in Flutter Web.

Is there a way to use a Widget wrapped around the Scaffold, for example, that stops widgets from resizing when page window is resized?

I have a unique use case where responsiveness is not required (although I am aware that responsive design is best usually).

答:
I know it is a bit late, but this is what worked for me:</p>
```<code>return Center(
    child: Row(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    Container(
        width: 1000, //fixed width that you need
           child: <your widget that you don't want to resize>,
              ),
             ]
            ),
           )
</code>```

Body:
I know it is a bit late, but this is what worked for me:

return Center(
    child: Row(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    Container(
        width: 1000, //fixed width that you need
           child: <your widget that you don't want to resize>,
              ),
             ]
            ),
           )


问:
How to create Twilio Studio flow from java api for call forwarding
I want to create a call forwarding flow in twilio from java api</p>

答:
Twilio developer evangelist here.

There is a tutorial on call forwarding with Java that might help you out.

Alternatively, here is a tutorial for setting up call forwarding in Studio.

If you are looking to create the call forwarding flow itself in Studio using Java, the easiest thing to do is to create the flow in the Studio drag and drop editor, then export the flow as JSON. You can then use that JSON in the API to create a flow. There is a Java example for creating flows via the API here.


问:
How can I filter data on a rails index page using columns from a linked table?
I am looking for help with adding a search filter to an existing index page that normally shows all families in my geneaology database.

The family table holds zero or more father_ids and zero or more mother_ids that point to the Persons table which describes a person.

Here is the relevant code in the families controller (simplified until I get it to work).

```<code>  def index
    if params[:search]
      @families = Family.where("Person.find('fam_fatherid').first_name like 'bob%'")
    else
      @families = Family.all.order(params[:sort])
    end
</code>```

This results in a syntax error.  I have also tried several other ways to access the data, but to no avail.
Example : @families = Family.where ("fam_fatherid.firstname like 'bob%'").  This doesn't work because fam_fatherid is an integer and not an object (I assume). The error I get is:
SQLite3::SQLException: no such column: fam_fatherid.first_name

FWIW, here is the schema for the people table, just to verify that first_name is a valid column.

```<code>CREATE TABLE "people" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name_prefix" varchar, "first_name" varchar, "middle_name" varchar, "last_name" varchar, "name_suffix" varchar, "date_of_birth" varchar, "date_of_death" varchar, "gender" varchar, "notes" varchar, "sync_outlook" varchar, "sync_phone" varchar, "flags" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL, "place_of_birth" varchar, "aliases" varchar);
</code>```

This is the model:

```<code>class Family < ApplicationRecord
    has_many :childlinks, foreign_key: "child_family"
    has_many :people, through: :childlinks

    # attempting to link parents
    has_many :people, foreign_key: "fam_fatherid"
    has_many :people, foreign_key: "fam_motherid"
end
</code>```

If someone can just point me in the right direction, I would appreciate it.  I just started with Rails a few months ago, and I don't mind doing the research, but so far, I have spent many hours and just seem to be going in circles. I've been able to add a search to the people index page because the first_name (etc.) is a column in the Persons table and not a link to another table.
Thanks in advance, and I hope I've provided enough information.
Jere

答:
Just join with related table and use where to search

```<code>def index
  if params[:search].present?
    @families = Family.joins(:people).where("people.first_name LIKE ?", params[:search])
  else
    @families = Family.order(params[:sort])
  end
end
</code>```

There is useful gem ransack. Using it you can quite easily create more powerful search fields

Body:
I am looking for help with adding a search filter to an existing index page that normally shows all families in my geneaology database.

The family table holds zero or more father_ids and zero or more mother_ids that point to the Persons table which describes a person.

Here is the relevant code in the families controller (simplified until I get it to work).

  def index
    if params[:search]
      @families = Family.where("Person.find('fam_fatherid').first_name like 'bob%'")
    else
      @families = Family.all.order(params[:sort])
    end

This results in a syntax error.  I have also tried several other ways to access the data, but to no avail.
Example : @families = Family.where ("fam_fatherid.firstname like 'bob%'").  This doesn't work because fam_fatherid is an integer and not an object (I assume). The error I get is:
SQLite3::SQLException: no such column: fam_fatherid.first_name

FWIW, here is the schema for the people table, just to verify that first_name is a valid column.

CREATE TABLE "people" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name_prefix" varchar, "first_name" varchar, "middle_name" varchar, "last_name" varchar, "name_suffix" varchar, "date_of_birth" varchar, "date_of_death" varchar, "gender" varchar, "notes" varchar, "sync_outlook" varchar, "sync_phone" varchar, "flags" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL, "place_of_birth" varchar, "aliases" varchar);

This is the model:

class Family < ApplicationRecord
    has_many :childlinks, foreign_key: "child_family"
    has_many :people, through: :childlinks

    # attempting to link parents
    has_many :people, foreign_key: "fam_fatherid"
    has_many :people, foreign_key: "fam_motherid"
end

If someone can just point me in the right direction, I would appreciate it.  I just started with Rails a few months ago, and I don't mind doing the research, but so far, I have spent many hours and just seem to be going in circles. I've been able to add a search to the people index page because the first_name (etc.) is a column in the Persons table and not a link to another table.
Thanks in advance, and I hope I've provided enough information.
Jere

Body:
Just join with related table and use where to search

def index
  if params[:search].present?
    @families = Family.joins(:people).where("people.first_name LIKE ?", params[:search])
  else
    @families = Family.order(params[:sort])
  end
end

There is useful gem ransack. Using it you can quite easily create more powerful search fields


问:
How to get powershell or pwsh exe file path in debian/linux
Need to run build script in both the environment (windows and Linux).Build script is pointing to the powershell.exe path in window environment and working fine. But at the same time, same build script is not getting the powershell path and throwing error.

Executing task in folder root: PowerShell  /home/userName/FolderName/Assetmonitor/scripts/build/client/build-client-dev.ps1 <

The terminal process failed to launch: Path to shell executable "PowerShell" does not exist.

答:
It sounds like your script is trying to call the PowerShell executable, but "powershell" (technically powershell.exe) only exists on Windows and is the executable for powershell 5.x and earlier. The executable for PowerShell 7, which is the only version I'm familiar with that's available on Linux, is named "pwsh"(pwsh.exe on Windows).

I would recommend either: 1) installing PowerShell 7 on your Windows boxes and changing the script to call for pwsh instead of "PowerShell" (remember that Linux is case-sensitive), or 2) creating a symbolic link to somewhere in your existing $PATH environment variable (like /usr/local/bin/) that points "PowerShell" to wherever your PowerShell 7 is installed on Linux.

The default path to the actual pwsh program, if you use the latest build files from Microsoft, is /opt/microsoft/powershell/7/pwsh, but that's already symlinked to /usr/bin/pwsh so you could link there instead:

```<code>    sudo ln -s /opt/microsoft/powershell/7/pwsh /usr/local/bin/PowerShell
</code>```

Body:
It sounds like your script is trying to call the PowerShell executable, but "powershell" (technically powershell.exe) only exists on Windows and is the executable for powershell 5.x and earlier. The executable for PowerShell 7, which is the only version I'm familiar with that's available on Linux, is named "pwsh"(pwsh.exe on Windows).

I would recommend either: 1) installing PowerShell 7 on your Windows boxes and changing the script to call for pwsh instead of "PowerShell" (remember that Linux is case-sensitive), or 2) creating a symbolic link to somewhere in your existing $PATH environment variable (like /usr/local/bin/) that points "PowerShell" to wherever your PowerShell 7 is installed on Linux.

The default path to the actual pwsh program, if you use the latest build files from Microsoft, is /opt/microsoft/powershell/7/pwsh, but that's already symlinked to /usr/bin/pwsh so you could link there instead:

    sudo ln -s /opt/microsoft/powershell/7/pwsh /usr/local/bin/PowerShell


答:
One solution for this would be to remove the callbacks when the ImageManager is destroyed.

In the ImageManager destructor you can loop through m_imageMap and set all deleters to null.

Title: What is the proper design pattern for a callback in destructor which uses a pointer which may or may not point to valid object?


问:
Oracle: How to estimate the size of a view?
In Oracle I can get the size of a table. I would like to estimate the size of a view (non materialized). Is it possible?

I know that views don't have any data per se, but we are moving the data to our data lake and would like to estimate it. Knowing the size we will be able to optimize our resources and speed up the process

答:
You can use EXPLAIN PLAN to estimate the number of bytes and rows that will be returned by reading the entire view. But keep in mind that these numbers are only estimates, they depend on having current statistics, and they will be less accurate for more complicated queries.

For example, on my system, EXPLAIN PLAN estimates that a somewhat complicated metadata view will return 34 MB and 75,590 rows. Whereas the actual values are roughly 14 MB and 85,402 rows.

Commands:

```<code>explain plan for select * from dba_objects;

select * from table(dbms_xplan.display);
</code>```

Results:

```<code>Plan hash value: 3423780594

------------------------------------------------------------------------------------------------------------
| Id  | Operation                              | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                       |                   | 75590 |    34M|   134K  (1)| 00:00:06 |
|   1 |  VIEW                                  | DBA_OBJECTS       | 75590 |    34M|   134K  (1)| 00:00:06 |
|   2 |   UNION-ALL                            |                   |       |       |            |          |
...
</code>```

Estimate multiple views in a single query

With a few tricks you can create estimates for multiple views all within a single query. This solution requires Oracle 12.1 or higher. The WITH FUNCTION syntax is a bit odd, and some IDEs struggle with it, so you might have to play around with the semicolon and slash at the end.

```<code>--Create sample views from data dictionary views.
create or replace view view1 as select * from all_tables;
create or replace view view2 as select * from all_tab_privs;
create or replace view view3 as select * from all_objects;

--Get the estimated size of each query. The actual values will differ for your database.
with function get_bytes(p_view_name varchar2) return number is
    v_bytes number;
    --(Needed because "explain plan" is technically DML, which normally shouldn't be executed inside a query.)
    pragma autonomous_transaction;
begin
    --Create an explain plan for reading everything from the view.
    execute immediate replace(q'[explain plan set statement_id = '#VIEW_NAME#' for select * from #VIEW_NAME#]', '#VIEW_NAME#', p_view_name);

    --Get the size in bytes.
    --Latest plan information. (In case the explain plan was generated multiple times.)
    select max(bytes)
    into v_bytes
    from
    (
        --Plan information.
        select bytes, timestamp, max(timestamp) over () latest_timestamp
        from plan_table
        where statement_id = p_view_name and id = 0
    )
    where latest_timestamp = timestamp;

    --As part of the AUTONOMOUS_TRANSACTION feature, the function must either commit or rollback.
    rollback;

    return v_bytes;
end;
select view_name, round(get_bytes(view_name) / 1024 / 1024, 1) mb
from user_views
order by mb desc, view_name;
/
</code>```

Results:

```<code>VIEW_NAME            MB
------------ ----------
VIEW3               2.4
VIEW1                .8
VIEW2                .7
</code>```

Body:
You can use EXPLAIN PLAN to estimate the number of bytes and rows that will be returned by reading the entire view. But keep in mind that these numbers are only estimates, they depend on having current statistics, and they will be less accurate for more complicated queries.

For example, on my system, EXPLAIN PLAN estimates that a somewhat complicated metadata view will return 34 MB and 75,590 rows. Whereas the actual values are roughly 14 MB and 85,402 rows.

Commands:

explain plan for select * from dba_objects;

select * from table(dbms_xplan.display);

Results:

Plan hash value: 3423780594

------------------------------------------------------------------------------------------------------------
| Id  | Operation                              | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                       |                   | 75590 |    34M|   134K  (1)| 00:00:06 |
|   1 |  VIEW                                  | DBA_OBJECTS       | 75590 |    34M|   134K  (1)| 00:00:06 |
|   2 |   UNION-ALL                            |                   |       |       |            |          |
...

Estimate multiple views in a single query

With a few tricks you can create estimates for multiple views all within a single query. This solution requires Oracle 12.1 or higher. The WITH FUNCTION syntax is a bit odd, and some IDEs struggle with it, so you might have to play around with the semicolon and slash at the end.

--Create sample views from data dictionary views.
create or replace view view1 as select * from all_tables;
create or replace view view2 as select * from all_tab_privs;
create or replace view view3 as select * from all_objects;

--Get the estimated size of each query. The actual values will differ for your database.
with function get_bytes(p_view_name varchar2) return number is
    v_bytes number;
    --(Needed because "explain plan" is technically DML, which normally shouldn't be executed inside a query.)
    pragma autonomous_transaction;
begin
    --Create an explain plan for reading everything from the view.
    execute immediate replace(q'[explain plan set statement_id = '#VIEW_NAME#' for select * from #VIEW_NAME#]', '#VIEW_NAME#', p_view_name);

    --Get the size in bytes.
    --Latest plan information. (In case the explain plan was generated multiple times.)
    select max(bytes)
    into v_bytes
    from
    (
        --Plan information.
        select bytes, timestamp, max(timestamp) over () latest_timestamp
        from plan_table
        where statement_id = p_view_name and id = 0
    )
    where latest_timestamp = timestamp;

    --As part of the AUTONOMOUS_TRANSACTION feature, the function must either commit or rollback.
    rollback;

    return v_bytes;
end;
select view_name, round(get_bytes(view_name) / 1024 / 1024, 1) mb
from user_views
order by mb desc, view_name;
/

Results:

VIEW_NAME            MB
------------ ----------
VIEW3               2.4
VIEW1                .8
VIEW2                .7


问:
c++ getservbyport returning wrong information
When I run this code:

```<code>#include <iostream>
#include <netdb.h>

int main(){
    struct servent* serv = getservbyport(22, "tcp");
    if(serv != NULL){
        std::cout<<"name: "<<serv->s_name<<" type: "<<serv->s_proto<<std::endl;
    }
    return 0;
}
</code>```

The result is:

```<code>name: pcanywherestat type: tcp
</code>```

If I were to run getservbyport(21, "tcp"); the variable serv would return NULL.
When I go to the the /etc/services file, it includes these lines:

```<code>ssh              22/udp     # SSH Remote Login Protocol
ssh              22/tcp     # SSH Remote Login Protocol

ftp              21/tcp     # File Transfer [Control]
</code>```

If I search the file for pcanywherestat it doesn't exist within the file. Why am I getting this result from running that code?

答:
getservbyport(3) and family store numeric values in Big Endian (aka, network byte order).  As a result, you need to convert any int's, in this case with htons(), thusly:

```<code>struct servent* serv = getservbyport(htons(22), "tcp");
</code>```

hton*(3) convert values between host and network byte order

Edit: the functions htons, htonl, htonll convert from host to short int, long int, and long long int, respectively.

Body:
When I run this code:

#include <iostream>
#include <netdb.h>

int main(){
    struct servent* serv = getservbyport(22, "tcp");
    if(serv != NULL){
        std::cout<<"name: "<<serv->s_name<<" type: "<<serv->s_proto<<std::endl;
    }
    return 0;
}

The result is:

name: pcanywherestat type: tcp

If I were to run getservbyport(21, "tcp"); the variable serv would return NULL.
When I go to the the /etc/services file, it includes these lines:

ssh              22/udp     # SSH Remote Login Protocol
ssh              22/tcp     # SSH Remote Login Protocol

ftp              21/tcp     # File Transfer [Control]

If I search the file for pcanywherestat it doesn't exist within the file. Why am I getting this result from running that code?

Body:
getservbyport(3) and family store numeric values in Big Endian (aka, network byte order).  As a result, you need to convert any int's, in this case with htons(), thusly:

struct servent* serv = getservbyport(htons(22), "tcp");

hton*(3) convert values between host and network byte order

Edit: the functions htons, htonl, htonll convert from host to short int, long int, and long long int, respectively.


问:
Configuring the Health Check of a Kubernetes Ingress with Terraform
We are using an ingress (kubernetes_ingress.db_admin_ingress) to expose the service (kubernetes_service.db_admin) of a deployment (kubernetes_deployment.db_admin) in Google Kubernetes Engine (GKE) with Terraform.

When Terraform creates the ingress, a Level 7 Load Balancer is automatically created with a default health check:

port: 80

path: /

protocol: HTTP(S)

Our deployment (kubernetes_deployment.db_admin) does not respond to the path / with a 200, so the health check fails.

How can we change the path in the health check configuration?

resource "google_compute_managed_ssl_certificate" "db_admin_ssl_certificate" {
  provider = google-beta

  name = "db-admin-ssl-certificate"

  managed {
    domains = ["db.${var.domain}."]
  }
}

resource "kubernetes_deployment" "db_admin" {
  metadata {
    name = "db-admin"
    labels = {
      App = "db-admin"
    }
  }

  spec {
    replicas = 1
    selector {
      match_labels = {
        App = "db-admin"
      }
    }
    template {
      metadata {
        labels = {
          App = "db-admin"
        }
      }
      spec {
        container {
          image = "dpage/pgadmin4:2022-01-10-1"
          name  = "db-admin"
          env {
            name = "PGADMIN_DEFAULT_EMAIL"
            value = "test@test.com"
          }
          env {
            name = "PGADMIN_DEFAULT_PASSWORD"
            value = "test"
          }

          port {
            container_port = 80
          }

          resources {}
        }
      }
    }
  }
}

resource "kubernetes_service" "db_admin" {
  metadata {
    name = "db-admin"
  }
  spec {
    selector = {
      App = kubernetes_deployment.db_admin.spec.0.template.0.metadata[0].labels.App
    }
    port {
      protocol    = "TCP"
      port        = 80
      target_port = 80
    }

    type = "NodePort"
  }
}

resource "kubernetes_ingress" "db_admin_ingress" {
  wait_for_load_balancer = true

  metadata {
    name = "db-admin-ingress"
    annotations = {
      "ingress.gcp.kubernetes.io/pre-shared-cert"   = google_compute_managed_ssl_certificate.db_admin_ssl_certificate.name
    }
  }

  spec {

    rule {
      http {
        path {
          backend {
            service_name = "db-admin"
            service_port = 80
          }

          path = "/*"
        }

      }
    }

  }
}
```

答:
According to Google Kubernetes Engine (GKE) official documentation here, you are able to customize ingress/Level 7 Load Balancer health checks through either:

the readinessProbe for the container within the pod your ingress is serving traffic to

Warning: this method comes with warnings here

a backendconfig resource

I would highly recommend creating a backendconfig resource.

Unfortunately, the kubernetes Terraform provider does not seem to support the backendconfig resource based on this GitHub issue.  This means that you can either:

use the kubernetes-alpha provider (found here) to transcribe a YAML backendconfig manifest to HCL with the manifest argument for the only kubernetes-alpha resource: kubernetes-manifest (more on that here)

use an unofficial provider (such as banzaicloud/k8s found here)

check the backendconfig manifest (as either JSON or YAML) into SCM

A sample backendconfig YAML manifest:

apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
  name: db-admin
  namespace: default
spec:
  healthCheck:
    checkIntervalSec: 30
    timeoutSec: 5
    healthyThreshold: 1
    unhealthyThreshold: 2
    type: HTTP
    requestPath: /v1/some/path
    port: 80
```
<p><strong>Note</strong>: a <code>service</code> is needed to associate a <code>backendconfig</code> with an <code>ingress</code>/Level 7 Load Balancer:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: db-admin-ingress-backend-config
  labels:
    app: db-admin
  annotations:
    cloud.google.com/backend-config: '{"ports": {"80":"db-admin"}}'
    cloud.google.com/neg: '{"ingress": true}'
spec:
  type: NodePort
  selector:
    app: db-admin
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
</code>```

You can learn more about the backendconfig resource and the service it requires here.

Body:
According to Google Kubernetes Engine (GKE) official documentation here, you are able to customize ingress/Level 7 Load Balancer health checks through either:

the readinessProbe for the container within the pod your ingress is serving traffic to

Warning: this method comes with warnings here

a backendconfig resource

I would highly recommend creating a backendconfig resource.

Unfortunately, the kubernetes Terraform provider does not seem to support the backendconfig resource based on this GitHub issue.  This means that you can either:

use the kubernetes-alpha provider (found here) to transcribe a YAML backendconfig manifest to HCL with the manifest argument for the only kubernetes-alpha resource: kubernetes-manifest (more on that here)

use an unofficial provider (such as banzaicloud/k8s found here)

check the backendconfig manifest (as either JSON or YAML) into SCM

A sample backendconfig YAML manifest:

apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
  name: db-admin
  namespace: default
spec:
  healthCheck:
    checkIntervalSec: 30
    timeoutSec: 5
    healthyThreshold: 1
    unhealthyThreshold: 2
    type: HTTP
    requestPath: /v1/some/path
    port: 80

Note: a service is needed to associate a backendconfig with an ingress/Level 7 Load Balancer:

apiVersion: v1
kind: Service
metadata:
  name: db-admin-ingress-backend-config
  labels:
    app: db-admin
  annotations:
    cloud.google.com/backend-config: '{"ports": {"80":"db-admin"}}'
    cloud.google.com/neg: '{"ingress": true}'
spec:
  type: NodePort
  selector:
    app: db-admin
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80

You can learn more about the backendconfig resource and the service it requires here.


问:
Are Interfaces used for the sole purpose of generating ABI's in Solidity?
I have been following Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial (https://www.youtube.com/watch?v=M576WGiDBdQ&t=28658s).

At a certain point in the tutorial (around hour 9), the following line of code exists:

```<code>weth=interface.IWeth(SomeAddress)

tx=weth.deposit({"from":account, "value": 0.01*10**18})
</code>```

My understanding is that interfaces get compiled into ABI for use by some external entity.

the IWeth interface is as follows:

```<code>pragma solidity ^0.4.19;

/*this is the interface for the WETH contract */
interface IWeth {
    function allowance(address owner, address spender)
        external
        view
        returns (uint256 remaining);

    function approve(address spender, uint256 value)
        external
        returns (bool success);

    function balanceOf(address owner) external view returns (uint256 balance);

    function decimals() external view returns (uint8 decimalPlaces);

    function name() external view returns (string memory tokenName);

    function symbol() external view returns (string memory tokenSymbol);

    function totalSupply() external view returns (uint256 totalTokensIssued);

    function transfer(address to, uint256 value)
        external
        returns (bool success);

    function transferFrom(
        address from,
        address to,
        uint256 value
    ) external returns (bool success);

    function deposit() external;

    function withdraw(uint256 wad) external;
}
</code>```

The above interface is not implemented anywhere in the project but the project compiles and runs just fine. In my limited understanding of OOP, shouldn't an interface be implemented somewhere? if so, where is it implemented? If not, does the interface ( in this case IWeth) exist for the sole purpose of generating an ABI?

Also, I'm confused about how the interface is used as in weth=interface.IWeth(SomeAddress). In general, how can we write ...InterfaceName(someArg) when it is not even declared in the interface file?

Thanks in advance!

答:
You can use an interface in your contract to call functions in another contract. Interfaces are most useful when you need to add functionality but instead of adding complexity to your application you use it from another contract. They reduce code duplication.

brownie is already loading interfaces. This is from docs

The InterfaceContainer object (available as interface) provides access
to the interfaces within your project’s interfaces/ folder.

interface.IWeth(SomeAddress) this tells Ethereum virtual machine to  create a contract instance of given contract address with the functionalities of the interface. And then your app is calling deposit of that contract instance

Body:
I have been following Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial (https://www.youtube.com/watch?v=M576WGiDBdQ&t=28658s).

At a certain point in the tutorial (around hour 9), the following line of code exists:

weth=interface.IWeth(SomeAddress)

tx=weth.deposit({"from":account, "value": 0.01*10**18})

My understanding is that interfaces get compiled into ABI for use by some external entity.

the IWeth interface is as follows:

pragma solidity ^0.4.19;

/*this is the interface for the WETH contract */
interface IWeth {
    function allowance(address owner, address spender)
        external
        view
        returns (uint256 remaining);

    function approve(address spender, uint256 value)
        external
        returns (bool success);

    function balanceOf(address owner) external view returns (uint256 balance);

    function decimals() external view returns (uint8 decimalPlaces);

    function name() external view returns (string memory tokenName);

    function symbol() external view returns (string memory tokenSymbol);

    function totalSupply() external view returns (uint256 totalTokensIssued);

    function transfer(address to, uint256 value)
        external
        returns (bool success);

    function transferFrom(
        address from,
        address to,
        uint256 value
    ) external returns (bool success);

    function deposit() external;

    function withdraw(uint256 wad) external;
}

The above interface is not implemented anywhere in the project but the project compiles and runs just fine. In my limited understanding of OOP, shouldn't an interface be implemented somewhere? if so, where is it implemented? If not, does the interface ( in this case IWeth) exist for the sole purpose of generating an ABI?

Also, I'm confused about how the interface is used as in weth=interface.IWeth(SomeAddress). In general, how can we write ...InterfaceName(someArg) when it is not even declared in the interface file?

Thanks in advance!


答:
Since the callback depends on the manager, it should have  ownership.

It cannot have proper ownership because the manager already owns the (object that contains) the callback, but it can have weak shared ownership. A working adjustment of your example:

```<code>class ImageManager : std::enable_shared_from_this<ImageManager> {
// ...

    auto callbackFunc = [this_w = weak_from_this(), currentUUID]() {
        if (auto this_ = this_w.lock())
            this_->removeImage(currentUUID);
    };
// ...

    auto manager = std::make_shared<ImageManager>();
    manager->populateImage(img1);
    manager->populateImage(img2);
</code>```

Body:
Since the callback depends on the manager, it should have  ownership.

It cannot have proper ownership because the manager already owns the (object that contains) the callback, but it can have weak shared ownership. A working adjustment of your example:

class ImageManager : std::enable_shared_from_this<ImageManager> {
// ...

    auto callbackFunc = [this_w = weak_from_this(), currentUUID]() {
        if (auto this_ = this_w.lock())
            this_->removeImage(currentUUID);
    };
// ...

    auto manager = std::make_shared<ImageManager>();
    manager->populateImage(img1);
    manager->populateImage(img2);


问:
Android: how to center horizontally a ConstraintLayout inside a ListView row?
I am trying to center a ConstraintLayout inside a ListView's row (the ListView contains 6 rows, each containing 6 squares).

I have written this inside the view XML:

```<code><!-- Grid view -->
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/list_view_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/top_view"
    app:layout_constraintBottom_toTopOf="@id/bottom_view" >

    <ListView
        android:id="@+id/list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:listSelector="@android:color/transparent"
        android:divider="@null"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</code>```

The row XML:

```<code><?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/test_id"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:maxWidth="306dp">

    <FrameLayout
        android:id="@+id/box1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/box2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toEndOf="@id/box1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/box3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toEndOf="@id/box2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/box4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toEndOf="@id/box3"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/box5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toEndOf="@id/box4"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/box6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toEndOf="@id/box5"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
</code>```

When I look at the design version of it in Android Studio, it looks like this:

So it looks like well centered, but when I compile the app, nothing appears on screen.

If I replace test_id's layout_width with match_parent, the list view appears but the content is sticked to the left of the screen instead of centered:

What should I do here?

Thank you for your help

答:
Specifying constant size for a view is usually wrong. Your layout is not in right format. Please specify what you want to achieve. And try to remove maxWidth="306dp" from test_id and add these lines to it:

```<code>android:layout_width="0dp"
app:layout_constraintWidth_percent="0.7"
</code>```

Body:
I am trying to center a ConstraintLayout inside a ListView's row (the ListView contains 6 rows, each containing 6 squares).

I have written this inside the view XML:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/list_view_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/top_view"
    app:layout_constraintBottom_toTopOf="@id/bottom_view" >

    <ListView
        android:id="@+id/list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:listSelector="@android:color/transparent"
        android:divider="@null"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

The row XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/test_id"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:maxWidth="306dp">

    <FrameLayout
        android:id="@+id/box1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/box2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toEndOf="@id/box1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/box3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toEndOf="@id/box2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/box4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toEndOf="@id/box3"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/box5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toEndOf="@id/box4"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/box6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.166666"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toEndOf="@id/box5"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/black" />

    </FrameLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

When I look at the design version of it in Android Studio, it looks like this:

So it looks like well centered, but when I compile the app, nothing appears on screen.

If I replace test_id's layout_width with match_parent, the list view appears but the content is sticked to the left of the screen instead of centered:

What should I do here?

Thank you for your help

Body:
Specifying constant size for a view is usually wrong. Your layout is not in right format. Please specify what you want to achieve. And try to remove maxWidth="306dp" from test_id and add these lines to it:

android:layout_width="0dp"
app:layout_constraintWidth_percent="0.7"


问:
Can't choose the correct end date from the datepicker in a Python script
I'm using Google Chrome browser. I'm running a python script to choose the correct dates in the datepicker. It is unable to select the correct date. It keeps selecting the end date to be "02/01/2022" but I want to choose the date of five (5) days ago from today's date every time I run the script. For example, today is "02/08/2022" so it should choose "02/03/2022" as the end date. The start date, "12/01/2021" is correct.

Here's my code:

```<code>from selenium import webdriver
import time
import os.path
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import calendar
import datetime
from datetime import date

chrome_options = Options()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome("/Users/myname/Documents/chromedriver", options=chrome_options)

todays_date = date.today()
print(todays_date)

driver.get("https://accessdata.broadridge.com/node952064/")
try:
    myElem = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, '//*[@id="main"]/table/tbody/tr[2]/td[2]/form/table[2]/tbody/tr[3]/td[2]/input')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")

driver.find_element(By.XPATH, '//*[@id="main"]/table/tbody/tr[2]/td[2]/form/table[2]/tbody/tr[6]/td[2]/span/img').click()

driver.find_element(By.XPATH, '//*[@id="span8"]').click()
time.sleep(2)
driver.find_element(By.XPATH, '//*[@id="ext-gen157"]/div[3]/table/tbody/tr/td[4]/div/img').click()
driver.find_element(By.XPATH, '//*[@id="ext-gen209"]').click()
driver.find_element(By.XPATH, '//*[@id="main"]/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td[11]/a').click()
driver.find_element(By.XPATH, '//*[@id="dateAnchor1_0"]/img').click()
driver.find_element(By.XPATH, '//*[@id="caldiv"]/table/tbody/tr/td/center/table[2]/tbody/tr[2]/td[4]/a').click()
driver.find_element(By.ID, 'dateAnchor2_0').click()
driver.find_element(By.XPATH, '//*[@id="caldiv"]/table/tbody/tr/td/center/table[2]/tbody/tr[2]/td[3]/a').click()
</code>```

How can I select the correct date for five days ago from today?

Here's the HTML code:

```<code><td align="left" nowrap="">
                <table border="0" cellspacing="0" cellpadding="0">
                    <tbody><tr>

                            <td align="right" nowrap="">
                                <div id="firstValue1_0" style="visibility: visible">
                                    <input type="text" name="values1" value="12/01/2021" size="11" maxlength="10" onkeypress="if(document.getElementById('firstCalendar1_0').style.visibility == 'visible' ) return processKeyPress(this); else return true;" onfocus="self.status='Date Format is ' + dtFormat;" onblur="self.status=' ';" onchange="if(document.getElementById('firstCalendar1_0').style.visibility == 'visible' ){if(isValidDate(this)) valueOnChange(0);} else valueOnChange(0);">

                                    <span id="firstValue3_0" style="display: none">% may be used as a wildcard character. </span>

                                            <div id="firstCalendar1_0" style="visibility: visible;display:inline">

                                        <a id="dateAnchor1_0" name="dateAnchor1_0" onclick="dateSelect(0, 'values1');" style="vertical-align:middle"><img border="0" src="images/calendar.gif"></a>
                                    </div>
                                </div>

                            </td>
                            <td valign="center" nowrap="">
                                <div id="firstValue2_0" style="display: none">
                                    <span id="search0" class="search_sm" onclick="popupSearch('810', 'values1', 'Trade Date', 0)" onmouseover="doImgSwapOver('search_sm',0,true)" onmouseout="doImgSwapOut('search_sm',0,true)">
                                        <img name="search_sm" src="images/btn_search_sm.gif" width="24" height="22" alt="Search Trade Date" border="0" align="absmiddle">
                                    </span>
                                    <a name="searchAnchor0" id="searchAnchor0"></a>
                                </div>
                            </td>

                        <td valign="center" nowrap="">

                                <div id="secondValue1_0" style="visibility: visible">

                                  and
                                </div>
                        </td>

                            <td valign="center" nowrap="">

                                    <div id="secondValue2_0" style="visibility: visible">

                                        <input type="text" name="values2" value="02/03/2022" size="11" maxlength="10" onkeypress="return processKeyPress(this);" onfocus="self.status='Date Format is ' + dtFormat;" onblur="self.status=' ';" onchange="if(isValidDate(this)) valueOnChange(0);">

                                        <a id="dateAnchor2_0" name="dateAnchor2_0" onclick="dateSelect(0, 'values2');">
                                            <img border="0" src="images/calendar.gif">
                                        </a>
                                    </div>
                            </td>

                        <td>

                        </td>

                        <td valign="center" nowrap="">

                        <!-- visibility -->

                        <div id="tinMask_0" style="visibility:hidden">

                        <!-- Tin Mask Selection Box -->
                        <table>
                            <tbody><tr>
                                <td>
                                    <!-- Previously set to SHOW -->

                                    <!-- Previously set to HIDE  -->

                                        <!-- Not previously set -->

                                            <!-- Tin Privileges -->

                                            <!-- No Tin Privileges -->

                                                <select name="tinMaskFlgs" onchange="flagOnChange(0,document.forms[0].tinMaskFlgs,document.forms[0].columnFilterTinMaskFlgs);">
                                                    <option value="false">Show Tin Values</option>
                                                    <option value="true" selected="">Hide Tin Values </option>
                                                </select>

                                         <!-- true -->
                                     <!-- false -->
                                </td>
                            </tr>
                        </tbody></table>
                        </div>
                        </td>

                    </tr>
                </tbody></table>
            </td>
</code>```

答:
```<code>driver.find_element(BY_NAME,"values2").send_keys("02/03/2022")
</code>```

You can try sending keys to the input tag with the name values2.

Body:
I'm using Google Chrome browser. I'm running a python script to choose the correct dates in the datepicker. It is unable to select the correct date. It keeps selecting the end date to be "02/01/2022" but I want to choose the date of five (5) days ago from today's date every time I run the script. For example, today is "02/08/2022" so it should choose "02/03/2022" as the end date. The start date, "12/01/2021" is correct.

Here's my code:

from selenium import webdriver
import time
import os.path
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import calendar
import datetime
from datetime import date

chrome_options = Options()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome("/Users/myname/Documents/chromedriver", options=chrome_options)

todays_date = date.today()
print(todays_date)

driver.get("https://accessdata.broadridge.com/node952064/")
try:
    myElem = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, '//*[@id="main"]/table/tbody/tr[2]/td[2]/form/table[2]/tbody/tr[3]/td[2]/input')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")

driver.find_element(By.XPATH, '//*[@id="main"]/table/tbody/tr[2]/td[2]/form/table[2]/tbody/tr[6]/td[2]/span/img').click()

driver.find_element(By.XPATH, '//*[@id="span8"]').click()
time.sleep(2)
driver.find_element(By.XPATH, '//*[@id="ext-gen157"]/div[3]/table/tbody/tr/td[4]/div/img').click()
driver.find_element(By.XPATH, '//*[@id="ext-gen209"]').click()
driver.find_element(By.XPATH, '//*[@id="main"]/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td[11]/a').click()
driver.find_element(By.XPATH, '//*[@id="dateAnchor1_0"]/img').click()
driver.find_element(By.XPATH, '//*[@id="caldiv"]/table/tbody/tr/td/center/table[2]/tbody/tr[2]/td[4]/a').click()
driver.find_element(By.ID, 'dateAnchor2_0').click()
driver.find_element(By.XPATH, '//*[@id="caldiv"]/table/tbody/tr/td/center/table[2]/tbody/tr[2]/td[3]/a').click()

How can I select the correct date for five days ago from today?

Here's the HTML code:

                                    <input type="text" name="values1" value="12/01/2021" size="11" maxlength="10" onkeypress="if(document.getElementById('firstCalendar1_0').style.visibility == 'visible' ) return processKeyPress(this); else return true;" onfocus="self.status='Date Format is ' + dtFormat;" onblur="self.status=' ';" onchange="if(document.getElementById('firstCalendar1_0').style.visibility == 'visible' ){if(isValidDate(this)) valueOnChange(0);} else valueOnChange(0);">

                                    % may be used as a wildcard character.

                                  and

                                        <input type="text" name="values2" value="02/03/2022" size="11" maxlength="10" onkeypress="return processKeyPress(this);" onfocus="self.status='Date Format is ' + dtFormat;" onblur="self.status=' ';" onchange="if(isValidDate(this)) valueOnChange(0);">

                                                <select name="tinMaskFlgs" onchange="flagOnChange(0,document.forms[0].tinMaskFlgs,document.forms[0].columnFilterTinMaskFlgs);">
                                                    <option value="false">Show Tin Values</option>
                                                    <option value="true" selected="">Hide Tin Values </option>
                                                </select>

Body:
driver.find_element(BY_NAME,"values2").send_keys("02/03/2022")

You can try sending keys to the input tag with the name values2.


问:
Moving to new Mac, how to move Cocoapods trunk credentials
First, forgive me as I'm a newbie to Cocoapods/trunks.

I've created a pod recently, and I'm moving to a new Mac (I won't migrate, I'll do a clean install). I know that the credentials to update/push to a trunk is saved with a session key on my Mac. Where is it, and how can I move that to the new Mac?

答:
Starting a new session would work for sure. But to answer your exact question of where the credentials are:

Check out your ~/.netrc file. There should be a few lines like

```<code>machine trunk.cocoapods.org
  login <your_email>
  password <secret>
</code>```

Body:
Starting a new session would work for sure. But to answer your exact question of where the credentials are:

Check out your ~/.netrc file. There should be a few lines like

machine trunk.cocoapods.org
  login <your_email>
  password <secret>


问:
Flex: Modify an embedded icon and use it in a button?
Just that, if you embed an icon:

```<code>[Embed(source='icons/checkmark.png')]
private static var CheckMark:Class;
</code>```

You end up with a dynamic class. You can pretty easily assign the icon to a button at runtime by calling the setStyle method:

```<code>var btn:Button = new Button();
btn.setStyle("icon", CheckMark);
</code>```

But what if you wanted to alter the icon at runtime, like changing it's alpha value or even redrawing pixels, before assigning it to the button?

So far I can't find a satisfactory answer...

答:
The big problem I found with programmatic skins is that the button refuses to measure the width/height.  I easily got around this by overriding the get methods for each:

override public function get width():Number { return WIDTH; }
override public function get height():Number { return HEIGHT; }

In my case I needed to modify buttons in a TabNavigator, hence no easy way to subclass the button.  Thankfully, the parent of each skin is the button, so using static methods within your skin, you can identify the instance of the Button to which the icon skins belong.

If you're using the cover-all "icon" style, a new skin object will be created for each state.  So you'll need to keep this in mind when changing the state of the icons.

OwnerDisplayName: Aaron H.

ContentLicense: CC BY-SA 2.5

Body:
Just that, if you embed an icon:

[Embed(source='icons/checkmark.png')]
private static var CheckMark:Class;

You end up with a dynamic class. You can pretty easily assign the icon to a button at runtime by calling the setStyle method:

var btn:Button = new Button();
btn.setStyle("icon", CheckMark);

But what if you wanted to alter the icon at runtime, like changing it's alpha value or even redrawing pixels, before assigning it to the button?

So far I can't find a satisfactory answer...

LastEditorDisplayName: Aaron H.


问:
Sum values in column based on values in other column and append to df - python
I have a df that looks like this:
df here

The column Count gives values that are either 0 or stretches of higher numbers separated by zeros e.g. 0,0,0,0,4,4,4,4,4,4,0,0,0,0,0,0,6,6,6,6,6,6,6,0,0,0,0.

I would like to sum the values of the column "angle_1frame_abs" for each stretch and add it as new column to the df e.g.

Count: 0,0,0,0,4,4,4,4,4,4,...

angle_1frame_abs: 2,1,3,4,2,2,2,3,4,3,...

new column: 10,10,10,10,16,16,16,16,16,16,...

Thank you all!

答:
Seems what you need is to divide your df into segments with the same consecutive value of Count, and sum over the angle_1frame_abs within each segment, and copy the sum of each segment to each row of the segment.

In your case we cannot use Count as the key for groupby, so we need a new one:

```<code>new_key = (df['Count'].diff().abs() > 0).cumsum()
</code>```

Using the new key, we can do the sum:

```<code>the_sum = df.groupby( new_key )['angle_1frame_abs'].sum()
</code>```

And copy the summation results back to the table by the new key

```<code>df['NewColumn'] = new_key.map(the_sum)
</code>```

Note that we use diff() to find out the boundary of segments, and used abs() > 0 to convert boundary values to True/False or 1/0, then use cumsum to get the new key by leveraging the fact that the cumsum doesn't change outside the boundary (i.e. within the segment).

I tested my code using the following fake data

```<code>df = pd.DataFrame({
    'Count': [2,0,0,0,4,4,4,4,4,4,0,0,0,0,0,0,6,6,6,6,6,6,6,0,0,0,1],
    'angle_1frame_abs': [np.nan,0,0,0,4,4,4,4,4,4,1,1,1,1,1,1,6,6,6,6,6,6,6,2,2,2,2],
})
</code>```

Body:
Seems what you need is to divide your df into segments with the same consecutive value of Count, and sum over the angle_1frame_abs within each segment, and copy the sum of each segment to each row of the segment.

In your case we cannot use Count as the key for groupby, so we need a new one:

new_key = (df['Count'].diff().abs() > 0).cumsum()

Using the new key, we can do the sum:

the_sum = df.groupby( new_key )['angle_1frame_abs'].sum()

And copy the summation results back to the table by the new key

df['NewColumn'] = new_key.map(the_sum)

Note that we use diff() to find out the boundary of segments, and used abs() > 0 to convert boundary values to True/False or 1/0, then use cumsum to get the new key by leveraging the fact that the cumsum doesn't change outside the boundary (i.e. within the segment).

I tested my code using the following fake data

df = pd.DataFrame({
    'Count': [2,0,0,0,4,4,4,4,4,4,0,0,0,0,0,0,6,6,6,6,6,6,6,0,0,0,1],
    'angle_1frame_abs': [np.nan,0,0,0,4,4,4,4,4,4,1,1,1,1,1,1,6,6,6,6,6,6,6,2,2,2,2],
})


问:
StructuredStreaming - foreach/foreachBatch not working
I'm structured Streaming to read data from Kafka, write to BigQuery(though currently, i'm writing to console).
I'm trying to use foreach (or foreachBatch) to make transformations to a record,
however i'm running into issues.

Here is the code :

```<code>df_stream = spark.readStream.format('kafka') \
    .option("kafka.security.protocol", "SSL") \
    .option("kafka.ssl.truststore.location", ssl_truststore_location) \
    .option("kafka.ssl.truststore.password", ssl_truststore_password) \
    .option("kafka.ssl.keystore.location", ssl_keystore_location) \
    .option("kafka.ssl.keystore.password", ssl_keystore_password) \
    .option("kafka.bootstrap.servers",kafkaBrokers)\
    .option("subscribe", topic) \
    .option("kafka.group.id", consumerGroupId)\
    .option("startingOffsets", "earliest") \
    .option("failOnDataLoss", "false") \
    .option("maxOffsetsPerTrigger", 10) \
    .load()

print("df_stream -> ", df_stream, type(df_stream))
#df_stream ->  DataFrame[key: binary, value: binary, topic: string, partition: int, offset: bigint, timestamp: timestamp, timestampType: int] <class 'pyspark.sql.dataframe.DataFrame'>

def convertToDict(self,row):
    print(" IN CONVERT TO DICT ", row, " currentTime ", datetime.datetime.now())

query = df_stream.selectExpr("CAST(value AS STRING)").writeStream \
    .format("console") \
    .outputMode("append") \
    .trigger(processingTime='10 seconds') \
    .option("numRows",10)\
    .option("truncate", "false") \
    .option("checkpointLocation", "/Users/karanalang/Documents/Technology/gcp/DataProc/checkpoint/") \
    .foreach(convertToDict) \
    .start()

query.awaitTermination()

</code>```

Error when i use - foreach

```<code>22/02/07 12:36:49 ERROR Executor: Exception in task 1.0 in stage 0.0 (TID 1)
org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "/Users/karanalang/Documents/Technology/spark-3.2.0-bin-hadoop3.2/python/lib/pyspark.zip/pyspark/worker.py", line 619, in main
    process()
  File "/Users/karanalang/Documents/Technology/spark-3.2.0-bin-hadoop3.2/python/lib/pyspark.zip/pyspark/worker.py", line 609, in process
    out_iter = func(split_index, iterator)
  File "/Users/karanalang/Documents/Technology/spark-3.2.0-bin-hadoop3.2/python/lib/pyspark.zip/pyspark/sql/streaming.py", line 1037, in func_without_process
TypeError: convertToDict() missing 1 required positional argument: 'row'

    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:545)
    at org.apache.spark.api.python.PythonRunner$$anon$3.read(PythonRunner.scala:703)
    at org.apache.spark.api.python.PythonRunner$$anon$3.read(PythonRunner.scala:685)
    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:498)
    at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
    at org.apache.spark.sql.execution.python.PythonForeachWriter.close(PythonForeachWriter.scala:66)
    at org.apache.spark.sql.execution.streaming.sources.ForeachDataWriter.close(ForeachWriterTable.scala:168)
    at org.apache.spark.sql.execution.datasources.v2.DataWritingSparkTask$.$anonfun$run$9(WriteToDataSourceV2Exec.scala:457)
    at org.apache.spark.util.Utils$.tryWithSafeFinallyAndFailureCallbacks(Utils.scala:1518)
    at org.apache.spark.sql.execution.datasources.v2.DataWritingSparkTask$.run(WriteToDataSourceV2Exec.scala:457)
    at org.apache.spark.sql.execution.datasources.v2.V2TableWriteExec.$anonfun$writeWithV2$2(WriteToDataSourceV2Exec.scala:358)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
    at org.apache.spark.scheduler.Task.run(Task.scala:131)
    at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:506)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1462)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:509)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)

</code>```

How is the 'row' passed to the function, and how do i access the row to do transformations ?

When i use foreachBatch (in the code above), the epochId(or is it the batchId ?) gets printed. How do i use this to access the rows and make transformations ?

```<code>IN CONVERT TO DICT  44  currentTime  2022-02-07 12:50:27.074291
 IN CONVERT TO DICT  45  currentTime  2022-02-07 12:50:36.591686
 IN CONVERT TO DICT  46  currentTime  2022-02-07 12:50:40.316790
 IN CONVERT TO DICT  47  currentTime  2022-02-07 12:50:50.322389
 IN CONVERT TO DICT  48  currentTime  2022-02-07 12:51:00.346152
 IN CONVERT TO DICT  49  currentTime  2022-02-07 12:51:10.302129
 IN CONVERT TO DICT  50  currentTime  2022-02-07 12:51:20.350064
 IN CONVERT TO DICT  51  currentTime  2022-02-07 12:51:30.313543

</code>```

Pls note :

I need to examine each row, and make transformations to the row
So, should i be using foreach or foreachBatch ?

答:
It looks like you are not implementing the correct interface. You need to extend the ForEachWriter like it is mentioned here in the docs -

```<code>datasetOfString.writeStream.foreach(new ForeachWriter[String] {

  def open(partitionId: Long, version: Long): Boolean = {
    // open connection
  }

  def process(record: String) = {
    // write string to connection
  }

  def close(errorOrNull: Throwable): Unit = {
    // close the connection
  }
})
</code>```

https://spark.apache.org/docs/latest/structured-streaming-programming-guide.html#foreach

Alternatively, you could use foreachBatch if you want to apply the transformations one batch at a time. This also gives you the batchid that you can use for deduplication yourself.

Body:
I'm structured Streaming to read data from Kafka, write to BigQuery(though currently, i'm writing to console).
I'm trying to use foreach (or foreachBatch) to make transformations to a record,
however i'm running into issues.

Here is the code :

df_stream = spark.readStream.format('kafka') \
    .option("kafka.security.protocol", "SSL") \
    .option("kafka.ssl.truststore.location", ssl_truststore_location) \
    .option("kafka.ssl.truststore.password", ssl_truststore_password) \
    .option("kafka.ssl.keystore.location", ssl_keystore_location) \
    .option("kafka.ssl.keystore.password", ssl_keystore_password) \
    .option("kafka.bootstrap.servers",kafkaBrokers)\
    .option("subscribe", topic) \
    .option("kafka.group.id", consumerGroupId)\
    .option("startingOffsets", "earliest") \
    .option("failOnDataLoss", "false") \
    .option("maxOffsetsPerTrigger", 10) \
    .load()

print("df_stream -> ", df_stream, type(df_stream))
#df_stream ->  DataFrame[key: binary, value: binary, topic: string, partition: int, offset: bigint, timestamp: timestamp, timestampType: int] <class 'pyspark.sql.dataframe.DataFrame'>

def convertToDict(self,row):
    print(" IN CONVERT TO DICT ", row, " currentTime ", datetime.datetime.now())

query = df_stream.selectExpr("CAST(value AS STRING)").writeStream \
    .format("console") \
    .outputMode("append") \
    .trigger(processingTime='10 seconds') \
    .option("numRows",10)\
    .option("truncate", "false") \
    .option("checkpointLocation", "/Users/karanalang/Documents/Technology/gcp/DataProc/checkpoint/") \
    .foreach(convertToDict) \
    .start()

query.awaitTermination()

Error when i use - foreach

22/02/07 12:36:49 ERROR Executor: Exception in task 1.0 in stage 0.0 (TID 1)
org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "/Users/karanalang/Documents/Technology/spark-3.2.0-bin-hadoop3.2/python/lib/pyspark.zip/pyspark/worker.py", line 619, in main
    process()
  File "/Users/karanalang/Documents/Technology/spark-3.2.0-bin-hadoop3.2/python/lib/pyspark.zip/pyspark/worker.py", line 609, in process
    out_iter = func(split_index, iterator)
  File "/Users/karanalang/Documents/Technology/spark-3.2.0-bin-hadoop3.2/python/lib/pyspark.zip/pyspark/sql/streaming.py", line 1037, in func_without_process
TypeError: convertToDict() missing 1 required positional argument: 'row'

    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:545)
    at org.apache.spark.api.python.PythonRunner$$anon$3.read(PythonRunner.scala:703)
    at org.apache.spark.api.python.PythonRunner$$anon$3.read(PythonRunner.scala:685)
    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:498)
    at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
    at org.apache.spark.sql.execution.python.PythonForeachWriter.close(PythonForeachWriter.scala:66)
    at org.apache.spark.sql.execution.streaming.sources.ForeachDataWriter.close(ForeachWriterTable.scala:168)
    at org.apache.spark.sql.execution.datasources.v2.DataWritingSparkTask$.$anonfun$run$9(WriteToDataSourceV2Exec.scala:457)
    at org.apache.spark.util.Utils$.tryWithSafeFinallyAndFailureCallbacks(Utils.scala:1518)
    at org.apache.spark.sql.execution.datasources.v2.DataWritingSparkTask$.run(WriteToDataSourceV2Exec.scala:457)
    at org.apache.spark.sql.execution.datasources.v2.V2TableWriteExec.$anonfun$writeWithV2$2(WriteToDataSourceV2Exec.scala:358)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
    at org.apache.spark.scheduler.Task.run(Task.scala:131)
    at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:506)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1462)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:509)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)

How is the 'row' passed to the function, and how do i access the row to do transformations ?

When i use foreachBatch (in the code above), the epochId(or is it the batchId ?) gets printed. How do i use this to access the rows and make transformations ?

IN CONVERT TO DICT  44  currentTime  2022-02-07 12:50:27.074291
 IN CONVERT TO DICT  45  currentTime  2022-02-07 12:50:36.591686
 IN CONVERT TO DICT  46  currentTime  2022-02-07 12:50:40.316790
 IN CONVERT TO DICT  47  currentTime  2022-02-07 12:50:50.322389
 IN CONVERT TO DICT  48  currentTime  2022-02-07 12:51:00.346152
 IN CONVERT TO DICT  49  currentTime  2022-02-07 12:51:10.302129
 IN CONVERT TO DICT  50  currentTime  2022-02-07 12:51:20.350064
 IN CONVERT TO DICT  51  currentTime  2022-02-07 12:51:30.313543

Pls note :

I need to examine each row, and make transformations to the row
So, should i be using foreach or foreachBatch ?

Body:
It looks like you are not implementing the correct interface. You need to extend the ForEachWriter like it is mentioned here in the docs -

datasetOfString.writeStream.foreach(new ForeachWriter[String] {

  def open(partitionId: Long, version: Long): Boolean = {
    // open connection
  }

  def process(record: String) = {
    // write string to connection
  }

  def close(errorOrNull: Throwable): Unit = {
    // close the connection
  }
})

https://spark.apache.org/docs/latest/structured-streaming-programming-guide.html#foreach

Alternatively, you could use foreachBatch if you want to apply the transformations one batch at a time. This also gives you the batchid that you can use for deduplication yourself.


问:
How do i declare good routes in angular?
Im using angular and wanted to try lazy loading with multiples modules,  and i getting this error Error: Cannot match any routes. URL Segment: 'home/users/profile'

So well i been like a few long hours trying to solve this problem but i foundno answer

APP MODULE

```<code>{
    path: 'home',
    component: HomeComponent,
    loadChildren: () =>
      import('./components/home/home.module').then((m) => m.HomeModule),
  },
  {
    path: 'auth',
    loadChildren: () =>
      import('./components/auth/auth.module').then((m) => m.AuthModule),
  },
  {
    path: '',
    pathMatch: 'full',
    redirectTo: 'home',
  },
</code>```

HOME ROUTING MODULE

```<code> {
    path: 'users',
    loadChildren: () =>
      import('../users/users.module').then((m) => m.UsersModule),
  },
</code>```

USERS ROUTING MODULE

```<code> {
    path: '',
    outlet: 'child',
    component: UsersComponent,
  },
  {
    path: 'user/:id',
    outlet: 'child',
    component: UserComponent,
  },
  {
    path: 'profile',
    outlet: 'child',
    component: ProfileComponent,
  },
</code>```

Here is the repo if you need to see something else https://github.com/ginebras/user-system

答:
Welcome Alejø.

Most likely you need to remove this line

component: HomeComponent,
```

You need to use component or loadChildren but not both. You can put the HomeComponent as an empty path in the home routing module. Don't forget pathMatch: 'full' on all your empty paths.

Body:
Im using angular and wanted to try lazy loading with multiples modules,  and i getting this error Error: Cannot match any routes. URL Segment: 'home/users/profile'

So well i been like a few long hours trying to solve this problem but i foundno answer

APP MODULE

{
    path: 'home',
    component: HomeComponent,
    loadChildren: () =>
      import('./components/home/home.module').then((m) => m.HomeModule),
  },
  {
    path: 'auth',
    loadChildren: () =>
      import('./components/auth/auth.module').then((m) => m.AuthModule),
  },
  {
    path: '',
    pathMatch: 'full',
    redirectTo: 'home',
  },

HOME ROUTING MODULE

 {
    path: 'users',
    loadChildren: () =>
      import('../users/users.module').then((m) => m.UsersModule),
  },

USERS ROUTING MODULE

 {
    path: '',
    outlet: 'child',
    component: UsersComponent,
  },
  {
    path: 'user/:id',
    outlet: 'child',
    component: UserComponent,
  },
  {
    path: 'profile',
    outlet: 'child',
    component: ProfileComponent,
  },

Here is the repo if you need to see something else https://github.com/ginebras/user-system

Body:
Welcome Alejø.

Most likely you need to remove this line

component: HomeComponent,

You need to use component or loadChildren but not both. You can put the HomeComponent as an empty path in the home routing module. Don't forget pathMatch: 'full' on all your empty paths.


问:
Get last day in month in AppleScript and holidays in current month
I am writing an AppleScript which generates an MS Excel spreadsheet every month. I need to know how many days are in the current month, then go trough all months and decide if a given day is a weekday or a holiday.

I suppose I would save holidays (from this web http://kalendar365.cz/statni-svatky/2016 as table and access them locally) for this.

My question is:

How do I get the last day in a month?

How do I efficiently loop trough all days in the current month and decide if it is Sunday, Saturday or a day specified somewhere else (for example, in a text file of xlsx spreadsheet)?

答:
How about a simple no-function-needed method?

```<code>set currdate to the current date
set nextmonth to currdate + (30 * days)
set {year:y, month:m, day:d, time:t} to nextmonth
set ms to m as string --month as string
set ys to y as string --year as string
set firstday to date (m & " 1," & y as string) --midnight at the first day of next month
set lastday to (firstday - 1) --minus one second
return lastday -- last day of the this month at 11:59 PM
</code>```

As for weekends and holidays in Excel, doesn't Excel provide its own functions for that, as dirk-reichel mentioned?

However, if you want to use AppleScript to filter out these days using a pre-specified list of holidays, maybe this example will help. It loops through October through December of 2022, with USA holidays Halloween, Thanksgiving and Christmas Day specified. Note that Christmas Day happens to land on a Sunday in 2022.

```<code>set weekend to {"Saturday", "Sunday"}

set holidays to {date "October 31, 2022", date "November 24, 2022", date "December 25, 2022"}

set workdays to {}
set my_date to (date "Saturday, October 1, 2022 at 12:00:00 AM")
repeat
    set dy to the weekday of my_date
    set mydy to dy as string
    if weekend does not contain mydy and holidays does not contain my_date then
        set workdays to workdays & my_date
    end if
    set my_date to my_date + (1 * days)

    if my_date contains (date "January 1, 2023") then --specify an end date for your loop
        exit repeat
    end if
end repeat
return workdays
</code>```

Body:
How about a simple no-function-needed method?

set currdate to the current date
set nextmonth to currdate + (30 * days)
set {year:y, month:m, day:d, time:t} to nextmonth
set ms to m as string --month as string
set ys to y as string --year as string
set firstday to date (m & " 1," & y as string) --midnight at the first day of next month
set lastday to (firstday - 1) --minus one second
return lastday -- last day of the this month at 11:59 PM

As for weekends and holidays in Excel, doesn't Excel provide its own functions for that, as dirk-reichel mentioned?

However, if you want to use AppleScript to filter out these days using a pre-specified list of holidays, maybe this example will help. It loops through October through December of 2022, with USA holidays Halloween, Thanksgiving and Christmas Day specified. Note that Christmas Day happens to land on a Sunday in 2022.

set weekend to {"Saturday", "Sunday"}

set holidays to {date "October 31, 2022", date "November 24, 2022", date "December 25, 2022"}

set workdays to {}
set my_date to (date "Saturday, October 1, 2022 at 12:00:00 AM")
repeat
    set dy to the weekday of my_date
    set mydy to dy as string
    if weekend does not contain mydy and holidays does not contain my_date then
        set workdays to workdays & my_date
    end if
    set my_date to my_date + (1 * days)

    if my_date contains (date "January 1, 2023") then --specify an end date for your loop
        exit repeat
    end if
end repeat
return workdays