问:
How to make camera movement in a pygame 3d engine from scratch?
I am new to matrices and matrix transformations and other things. I am making a 3d engine of my own in pygame using 2d, but I ran into a problem. I can't get my engine to implement camera movement and it only does camera rotation. Here is my code:

window.py: The custom window class

```<code>from .general_imports import *

class Py3dWindow:
    def __init__(self, title=None, width=800, height=600,):
        self.title = title
        self.window = None
        self.width = width
        self.height = height
        self.meshes = []
        self.events_assinged = False

        self.projection_matrix = np.matrix([
            [1, 0, 0, 0],
            [0, 1, 0, 0],
            [0, 0, 1, 0],
            [0, 0, 0, 1]
        ])
        print(self.projection_matrix)

        self.camera = [
            [0, 0, 0, 0],
            [0, 0, 0],
            75, # fov
            1, # near
            1000 # far
        ]

    def add_mesh(self, mesh):
        self.meshes.append(mesh)

    def translate_matrix(self):
        angle_x = math.radians(self.camera[1][0])
        angle_y = math.radians(self.camera[1][1])
        angle_z = math.radians(self.camera[1][2])

        pos_x = self.camera[0][0]
        pos_y = self.camera[0][1]
        pos_z = self.camera[0][2]

        rotation_z = np.matrix([
            [int(np.cos(angle_z)), -int(np.sin(angle_z)), 0, 0],
            [int(np.sin(angle_z)), int(np.cos(angle_z)), 0, 0],
            [0, 0, 1, 0],
            [0, 0, 0, 1]
        ],)

        rotation_y = np.matrix([
            [int(np.cos(angle_y)), 0, int(np.sin(angle_y))],
            [0, 1, 0, 0],
            [-int(np.sin(angle_y)), 0, int(np.cos(angle_y))],
            [0, 0, 0, 1]
        ])

        rotation_x = np.matrix([
            [1, 0, 0, 0],
            [0, int(np.cos(angle_x)), -int(np.sin(angle_x)), 0],
            [0, int(np.sin(angle_x)), int(np.cos(angle_x)), 0],
            [0, 0, 0, 1],
        ])

        position_matrix = np.matrix([
            [1, 0, 0, pos_x],
            [0, 1, 0, pos_y],
            [0, 0, 1, pos_z],
            [0, 0, 0, 1],
        ])

        self.projection_matrix = np.dot(rotation_z, np.dot(rotation_y, rotation_x)) * position_matrix

        print(self.projection_matrix)

    def init_window(self):
        pygame.init()
        self.window = pygame.display.set_mode((self.width, self.height), pygame.RESIZABLE)
        pygame.display.set_caption(self.title)
        self.running = False

    def run(self,):
        self.running = True
        while self.running:
            self.draw()
            self.events(pygame.event.get())
            self.update()
            self.translate_matrix()
        pygame.quit()

    def events(self, events):
        for event in events:
            if event.type == pygame.QUIT:
                self.running = False
        if self.events_assinged:
            self._events(events)

    def update(self,):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running = False

    def draw(self,):
        self.window.fill((0, 0, 0))
        for mesh in self.meshes:
            mesh.draw(self.window)
        pygame.display.update()
        time.sleep(0.001)

    def on_events(self, func):
        self._events = func
        self.events_assinged = True

    def on_update(self, func):
        self.update = lambda: func()

    def on_draw(self, func):
        self.draw = lambda: func()

</code>```

mesh.py: Mesh class

```<code>from .general_imports import *

class Mesh:
    def __init__(self, parnet):
        self.points = []
        self.parent = parnet

    def add_point(self, position):
        position = [position[0] * 100, position[1] * 100, position[2] * 100, 1]
        self.points.append(np.matrix(position))

    def draw(self, window):
        for point in self.points:
            p = (point - self.parent.camera[0])
            p = np.reshape(p, (4, 1))
            projection = np.dot(self.parent.projection_matrix, p)
            projection = projection.tolist()
            x = projection[0][0]
            y = projection[1][0]
            pygame.draw.circle(window, (255, 255, 255), (int(x) + window.get_width() / 2 - 100, int(y) + window.get_width() / 2- 100) , 1)

</code>```

demo.py: The demo

```<code># Py3D demo

from Py3D.__init__ import *
import random

win = Py3dWindow(title="Py3D")
win.init_window()

mesh_ = Mesh(win)

side = 1

for i in range(-side, side):
    for j in range(-side, side):
        for k in range(-side, side):
            mesh_.add_point((i, j, k))

win.add_mesh(mesh_)

def move(events):
    for i in events:
        if i.type == 771:
            if i.text == 'w':
                win.camera[0][2] -= 1
            if i.text == 's':
                win.camera[0][2] += 1
            if i.text == 'a':
                win.camera[0][0] -= 1
            if i.text == 'd':
                win.camera[0][0] += 1

def rotate():
    win.camera[1][0] = 45
    win.camera[1][1] = 45
    win.camera[1][2] = 45

win.on_events(move)
win.on_update(rotate)
win.run()

</code>```

Errors

```<code>ValueError: shapes (4,4) and (1,4) not aligned: 4 (dim 1) != 1 (dim 0)
</code>```

答:
As you pointed out, in the translate_matrix function, your rotation_y variable is declared incorrectly:

```<code>rotation_y = np.matrix([
    [int(np.cos(angle_y)), 0, int(np.sin(angle_y))], # <--- missing an item
    [0, 1, 0, 0],
    [-int(np.sin(angle_y)), 0, int(np.cos(angle_y))], # <--- missing an item
    [0, 0, 0, 1]
])
</code>```

The first and third items are declared with only 3 items, while the 2nd and third items, and all of the items of rotation_x and rotation_z, are made with 4 items. Obviously that can't work, as a matrix is supposed to be rectangular.

Body:
I am new to matrices and matrix transformations and other things. I am making a 3d engine of my own in pygame using 2d, but I ran into a problem. I can't get my engine to implement camera movement and it only does camera rotation. Here is my code:

window.py: The custom window class

from .general_imports import *

class Py3dWindow:
    def __init__(self, title=None, width=800, height=600,):
        self.title = title
        self.window = None
        self.width = width
        self.height = height
        self.meshes = []
        self.events_assinged = False

        self.projection_matrix = np.matrix([
            [1, 0, 0, 0],
            [0, 1, 0, 0],
            [0, 0, 1, 0],
            [0, 0, 0, 1]
        ])
        print(self.projection_matrix)

        self.camera = [
            [0, 0, 0, 0],
            [0, 0, 0],
            75, # fov
            1, # near
            1000 # far
        ]

    def add_mesh(self, mesh):
        self.meshes.append(mesh)

    def translate_matrix(self):
        angle_x = math.radians(self.camera[1][0])
        angle_y = math.radians(self.camera[1][1])
        angle_z = math.radians(self.camera[1][2])

        pos_x = self.camera[0][0]
        pos_y = self.camera[0][1]
        pos_z = self.camera[0][2]

        rotation_z = np.matrix([
            [int(np.cos(angle_z)), -int(np.sin(angle_z)), 0, 0],
            [int(np.sin(angle_z)), int(np.cos(angle_z)), 0, 0],
            [0, 0, 1, 0],
            [0, 0, 0, 1]
        ],)

        rotation_y = np.matrix([
            [int(np.cos(angle_y)), 0, int(np.sin(angle_y))],
            [0, 1, 0, 0],
            [-int(np.sin(angle_y)), 0, int(np.cos(angle_y))],
            [0, 0, 0, 1]
        ])

        rotation_x = np.matrix([
            [1, 0, 0, 0],
            [0, int(np.cos(angle_x)), -int(np.sin(angle_x)), 0],
            [0, int(np.sin(angle_x)), int(np.cos(angle_x)), 0],
            [0, 0, 0, 1],
        ])

        position_matrix = np.matrix([
            [1, 0, 0, pos_x],
            [0, 1, 0, pos_y],
            [0, 0, 1, pos_z],
            [0, 0, 0, 1],
        ])

        self.projection_matrix = np.dot(rotation_z, np.dot(rotation_y, rotation_x)) * position_matrix

        print(self.projection_matrix)

    def init_window(self):
        pygame.init()
        self.window = pygame.display.set_mode((self.width, self.height), pygame.RESIZABLE)
        pygame.display.set_caption(self.title)
        self.running = False

    def run(self,):
        self.running = True
        while self.running:
            self.draw()
            self.events(pygame.event.get())
            self.update()
            self.translate_matrix()
        pygame.quit()

    def events(self, events):
        for event in events:
            if event.type == pygame.QUIT:
                self.running = False
        if self.events_assinged:
            self._events(events)

    def update(self,):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running = False

    def draw(self,):
        self.window.fill((0, 0, 0))
        for mesh in self.meshes:
            mesh.draw(self.window)
        pygame.display.update()
        time.sleep(0.001)

    def on_events(self, func):
        self._events = func
        self.events_assinged = True

    def on_update(self, func):
        self.update = lambda: func()

    def on_draw(self, func):
        self.draw = lambda: func()

mesh.py: Mesh class

from .general_imports import *

class Mesh:
    def __init__(self, parnet):
        self.points = []
        self.parent = parnet

    def add_point(self, position):
        position = [position[0] * 100, position[1] * 100, position[2] * 100, 1]
        self.points.append(np.matrix(position))

    def draw(self, window):
        for point in self.points:
            p = (point - self.parent.camera[0])
            p = np.reshape(p, (4, 1))
            projection = np.dot(self.parent.projection_matrix, p)
            projection = projection.tolist()
            x = projection[0][0]
            y = projection[1][0]
            pygame.draw.circle(window, (255, 255, 255), (int(x) + window.get_width() / 2 - 100, int(y) + window.get_width() / 2- 100) , 1)

demo.py: The demo

# Py3D demo

from Py3D.__init__ import *
import random

win = Py3dWindow(title="Py3D")
win.init_window()

mesh_ = Mesh(win)

side = 1

for i in range(-side, side):
    for j in range(-side, side):
        for k in range(-side, side):
            mesh_.add_point((i, j, k))

win.add_mesh(mesh_)

def move(events):
    for i in events:
        if i.type == 771:
            if i.text == 'w':
                win.camera[0][2] -= 1
            if i.text == 's':
                win.camera[0][2] += 1
            if i.text == 'a':
                win.camera[0][0] -= 1
            if i.text == 'd':
                win.camera[0][0] += 1

def rotate():
    win.camera[1][0] = 45
    win.camera[1][1] = 45
    win.camera[1][2] = 45

win.on_events(move)
win.on_update(rotate)
win.run()

Errors

ValueError: shapes (4,4) and (1,4) not aligned: 4 (dim 1) != 1 (dim 0)

Body:
As you pointed out, in the translate_matrix function, your rotation_y variable is declared incorrectly:

rotation_y = np.matrix([
    [int(np.cos(angle_y)), 0, int(np.sin(angle_y))], # <--- missing an item
    [0, 1, 0, 0],
    [-int(np.sin(angle_y)), 0, int(np.cos(angle_y))], # <--- missing an item
    [0, 0, 0, 1]
])

The first and third items are declared with only 3 items, while the 2nd and third items, and all of the items of rotation_x and rotation_z, are made with 4 items. Obviously that can't work, as a matrix is supposed to be rectangular.


来源: stackexchange

问:
Azure Data Explorer-Compute calculated field values at ingestion time
We have this table from an event with column A, but users always use column B, which is computed from function f(A). I don't want users to keep calling extends B = f(A) all the time.

How do I use update policy to create column B values at ingestion time instead? Thank you.

EDIT 1: I want column A and column B to be in the same table

答:
Using an update policy is an appropriate solution.

The official documentation includes a complete example for how to define the policy: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/management/updatepolicy

You can find an additional example in this blog post: https://y0nil.github.io/kusto.blog/blog-posts/update-policies.html

ContentLicense: CC BY-SA 4.0


问:
Python - Access items of a Set by index
So I created a set and I need to get items of that set with index like

```<code>myList = {Baseball,Basketball,Football}
print(myList.getitem(3)) #this should print item 3 of myList
</code>```

Can anyone help me?

答:
So, first off lists in python are bounded by square brackets. Also, I'm assuming Basketball and Football are variables you've already defined in your script, so you'll have something that looks like this:

```<code>Basketball = 5
Football = 'a'

myList = [Basketball, Basketball, Football]
</code>```

You can get the value of the third item of this list using an index! Indexing counts from 0, so we'll be calling for index 2 rather than 3

```<code>myList[2]
</code>```

which will return

```<code>'a'
</code>```

Title: Python - Access items of a Set by index

Body:
So I created a set and I need to get items of that set with index like

myList = {Baseball,Basketball,Football}
print(myList.getitem(3)) #this should print item 3 of myList

Can anyone help me?

Body:
So, first off lists in python are bounded by square brackets. Also, I'm assuming Basketball and Football are variables you've already defined in your script, so you'll have something that looks like this:

Basketball = 5
Football = 'a'

myList = [Basketball, Basketball, Football]

You can get the value of the third item of this list using an index! Indexing counts from 0, so we'll be calling for index 2 rather than 3

myList[2]

which will return

'a'


问:
Struts2 Wildcard Mapping - more specific one is being handled by generic one
I'm currently playing around with my Struts2 config for wildcard testing and I'm stuck with this one.

    <action name="/*/*" class="checkBlogUrl" method="testing">
        <param name="blogSiteUrl">{1}</param>
        <param name="test">{2}</param>
        <result name="success">/WEB-INF/jsp/cmsPages/index.jsp</result>
    </action>

    <action name="/*/postPreview1" class="blogPostAction" method="test">
        <param name="blogSiteUrl">{1}</param>
        <result name="success">/WEB-INF/jsp/cmsPages/templatePicker.jsp</result>
    </action>
```

If I access myurl.com/hello/hi I will be redirected to index.jsp.

But if I access myurl.com/hello/postPreview1 I will also be redirected to index.jsp instead of templatePicker.jsp.

Am I doing something wrong here? The struts wildcard doc said that the last one will win

EDIT:

Just tried to switch them around and it worked! Am I misreading the doc?

答:
In your case,from the official docs,

Mappings are matched against the request in the order they appear in the framework’s configuration file. If more than one pattern matches the last one wins, so less specific patterns must appear before more specific ones. However, if the request URL can be matched against a path without any wildcards in it, no wildcard matching is performed and order is not important.

https://struts.apache.org/core-developers/wildcard-mappings.html#wildcards

Body:
I'm currently playing around with my Struts2 config for wildcard testing and I'm stuck with this one.

    <action name="/*/*" class="checkBlogUrl" method="testing">
        <param name="blogSiteUrl">{1}</param>
        <param name="test">{2}</param>
        <result name="success">/WEB-INF/jsp/cmsPages/index.jsp</result>
    </action>

    <action name="/*/postPreview1" class="blogPostAction" method="test">
        <param name="blogSiteUrl">{1}</param>
        <result name="success">/WEB-INF/jsp/cmsPages/templatePicker.jsp</result>
    </action>

If I access myurl.com/hello/hi I will be redirected to index.jsp.

But if I access myurl.com/hello/postPreview1 I will also be redirected to index.jsp instead of templatePicker.jsp.

Am I doing something wrong here? The struts wildcard doc said that the last one will win

EDIT:

Just tried to switch them around and it worked! Am I misreading the doc?


问:
Can't make my example using std::ranges::copy_if(R&& r, O result, Pred pred, Proj proj = {}) compile
I've reduced the code failing to compile to the one below:

[Demo]

#include <algorithm>  // copy_if
#include <iostream>  // cout
#include <iterator>  // back_inserter
#include <ranges>
#include <string>
#include <vector>

struct A
{
    std::string p{};
    std::string d{};
};

int main()
{
    std::vector v{{"/usr/bin/cat", "blah"}, {"/usr/lib", "foo"}};
    std::vector<std::string> o{};
    std::ranges::copy_if(
        v,
        std::back_inserter(o),
        [](std::string& p){ return (p.size() > 10); },
        &A::p);
}
```

Visual Studio's error output is quite short: error C7602: 'std::ranges::_Copy_if_fn::operator ()': the associated constraints are not satisfied. It basically points you to check the constraints of copy_if at the algorithm header.

gcc and clang are more detailed. Having a look at a bit of both outputs:

First, the copy_if overload with a range, an output iterator, a predicate, and a projection, is considered.

But then, the expression *__o = (std::forward<_Tp>(__t) is evaluated as invalid; where: '_Out&& __o', '_Tp&& __t' [with _Tp = A&; _Out = std::back_insert_iterator<std::vector<std::string>>]

Since we are using a projection (from A& to std::string&), why is it trying to copy an A& instead of a std::string& to o?

The full error output is accessible through the Demo link above.

答:
The projection applies only to the predicate, not to the result. There's already a way to transform the data entirely—std::views::transform:

```<code>std::ranges::copy_if(
    v | std::views::transform(&A::p),
    std::back_inserter(o),
    [](const std::string& p){ return (p.size() > 10); });
</code>```

(This compiles in GCC and MSVC, but not Clang. I'm not 100% confident saying it's correct, but it should be close if nothing else, and it illustrates the point.)

Projections are for when you want to test a transformation (projection) of the data while carrying the originals through. In this example, that would mean you'd output to std::vector, yet test a std::string in the predicate. Not super useful here since you could just return (a.p.size() > 10);, but useful if passing a pre-written function instead of using a lambda. Also useful for some other cases such as easily sorting by a member—simply use the default comparator and pass &A::p as the projection.

Body:
I've reduced the code failing to compile to the one below:

[Demo]

#include <algorithm>  // copy_if
#include <iostream>  // cout
#include <iterator>  // back_inserter
#include <ranges>
#include <string>
#include <vector>

struct A
{
    std::string p{};
    std::string d{};
};

int main()
{
    std::vector v{{"/usr/bin/cat", "blah"}, {"/usr/lib", "foo"}};
    std::vector<std::string> o{};
    std::ranges::copy_if(
        v,
        std::back_inserter(o),
        [](std::string& p){ return (p.size() > 10); },
        &A::p);
}

Visual Studio's error output is quite short: error C7602: 'std::ranges::_Copy_if_fn::operator ()': the associated constraints are not satisfied. It basically points you to check the constraints of copy_if at the algorithm header.

gcc and clang are more detailed. Having a look at a bit of both outputs:

First, the copy_if overload with a range, an output iterator, a predicate, and a projection, is considered.

But then, the expression *__o = (std::forward<_Tp>(__t) is evaluated as invalid; where: '_Out&& __o', '_Tp&& __t' [with _Tp = A&; _Out = std::back_insert_iterator<std::vector<std::string>>]

Since we are using a projection (from A& to std::string&), why is it trying to copy an A& instead of a std::string& to o?

The full error output is accessible through the Demo link above.

Body:
The projection applies only to the predicate, not to the result. There's already a way to transform the data entirely—std::views::transform:

std::ranges::copy_if(
    v | std::views::transform(&A::p),
    std::back_inserter(o),
    [](const std::string& p){ return (p.size() > 10); });

(This compiles in GCC and MSVC, but not Clang. I'm not 100% confident saying it's correct, but it should be close if nothing else, and it illustrates the point.)

Projections are for when you want to test a transformation (projection) of the data while carrying the originals through. In this example, that would mean you'd output to std::vector<A>, yet test a std::string in the predicate. Not super useful here since you could just return (a.p.size() > 10);, but useful if passing a pre-written function instead of using a lambda. Also useful for some other cases such as easily sorting by a member—simply use the default comparator and pass &A::p as the projection.


问:
Rename more than one column using withColumnRenamed
I want to change names of two columns using spark withColumnRenamed function. Of course, I can write:

data = sqlContext.createDataFrame([(1,2), (3,4)], ['x1', 'x2'])
data = (data
       .withColumnRenamed('x1','x3')
       .withColumnRenamed('x2', 'x4'))
```

<p>but I want to do this in one step (having list/tuple of new names). Unfortunately, neither this:</p>

<pre class="lang-py prettyprint-override"><code>data = data.withColumnRenamed(['x1', 'x2'], ['x3', 'x4'])
</code>```

nor this:

data = data.withColumnRenamed(('x1', 'x2'), ('x3', 'x4'))
```

is working. Is it possible to do this that way?

答:
You should use this function:

```<code>def spark_rename_from_dict(df, rename_dict):
    newcols = [rename_dict.get(i,i) for i in df.columns]
    df = df.toDF(*newcols)
</code>```

Here your rename dict is a mapping over a subset of df.columns. This approach is recommended since it does not create multiple dataframes

Body:
I want to change names of two columns using spark withColumnRenamed function. Of course, I can write:

data = sqlContext.createDataFrame([(1,2), (3,4)], ['x1', 'x2'])
data = (data
       .withColumnRenamed('x1','x3')
       .withColumnRenamed('x2', 'x4'))

but I want to do this in one step (having list/tuple of new names). Unfortunately, neither this:

data = data.withColumnRenamed(['x1', 'x2'], ['x3', 'x4'])

nor this:

data = data.withColumnRenamed(('x1', 'x2'), ('x3', 'x4'))

is working. Is it possible to do this that way?

Body:
You should use this function:

def spark_rename_from_dict(df, rename_dict):
    newcols = [rename_dict.get(i,i) for i in df.columns]
    df = df.toDF(*newcols)

Here your rename dict is a mapping over a subset of df.columns. This approach is recommended since it does not create multiple dataframes


问:
Programmatically localize a String?
Asp.net 4 and C#.

I have a string in my Code Behind. I would like assign a value depending on the User's Browser region.

I'm trying with this code, no error, but does not work (no value to a String has been assigned).

Any idea how to solve it? Thanks

```<code>   string textPrevious = "<%$ Resources:Global, CategoryListSubCategories %>";
</code>```

答:
I found out the solution myself:
Just use Resources.Global.

```<code>string textPrevious = Resources.Global.CategoryListSubCategories;
</code>```

Useful article:
http://shan-tech.blogspot.com/2007/02/aspnet-20-localization-of-string.html

ContentLicense: CC BY-SA 3.0

Title: Programmatically localize a String?

Body:
Asp.net 4 and C#.

I have a string in my Code Behind. I would like assign a value depending on the User's Browser region.

I'm trying with this code, no error, but does not work (no value to a String has been assigned).

Any idea how to solve it? Thanks

   string textPrevious = "<%$ Resources:Global, CategoryListSubCategories %>";

Body:
I found out the solution myself:
Just use Resources.Global.

string textPrevious = Resources.Global.CategoryListSubCategories;

Useful article:
http://shan-tech.blogspot.com/2007/02/aspnet-20-localization-of-string.html


问:
Beautifulsoup Facebook Login
I am trying to use Beautifulsoup to scrape the post data by using the below code,
but I found that the beautifulsoup fail to login, that cause the scraper return text of all the post and include the header message (text that ask you to login).

Might I know how to modify the code in order to return info for the specific post with that id not all the posts info. Thanks!

```<code>import requests
from bs4 import BeautifulSoup

class faceBookBot():
    login_basic_url = "https://mbasic.facebook.com/login"
    login_mobile_url = 'https://m.facebook.com/login'
    payload = {
            'email': 'XXXX@gmail.com',
            'pass': "XXXX"
    }
    post_ID = ""

    # login to facebook and redirect to the link with specific post
    # I guess something wrong happen in below function
    def parse_html(self, request_url):
        with requests.Session() as session:
            post = session.post(self.login_basic_url, data=self.payload)
            parsed_html = session.get(request_url)
        return parsed_html

    # scrape the post all <p> which is the paragraph/content part
    def post_content(self):
        REQUEST_URL = f'https://m.facebook.com/story.php?story_fbid={self.post_ID}&id=7724542745'
        soup = BeautifulSoup(self.parse_html(REQUEST_URL).content, "html.parser")
        content = soup.find_all('p')
        post_content = []
        for lines in content:
            post_content.append(lines.text)
        post_content = ' '.join(post_content)
        return post_content

bot = faceBookBot()
bot.post_ID = "10158200911252746"
</code>```

答:
@AnsonChan yes, you could open the page with selenium, login and then copy it's cookies to requests:</p>
```<code>from selenium import webdriver
import requests

driver = webdriver.Chrome()
driver.get('http://facebook.com')
# login manually, or automate it.
# when logged in:
session = requests.session()
[session.cookies.update({cookie['name']: cookie['value']}) for cookie in driver.get_cookies()]
driver.quit()
# get the page you want with requests
response = session.get('https://m.facebook.com/story.php?story_fbid=123456789')
</code>```

Title: Beautifulsoup Facebook Login

Body:
I am trying to use Beautifulsoup to scrape the post data by using the below code,
but I found that the beautifulsoup fail to login, that cause the scraper return text of all the post and include the header message (text that ask you to login).

Might I know how to modify the code in order to return info for the specific post with that id not all the posts info. Thanks!

import requests
from bs4 import BeautifulSoup

class faceBookBot():
    login_basic_url = "https://mbasic.facebook.com/login"
    login_mobile_url = 'https://m.facebook.com/login'
    payload = {
            'email': 'XXXX@gmail.com',
            'pass': "XXXX"
    }
    post_ID = ""

    # login to facebook and redirect to the link with specific post
    # I guess something wrong happen in below function
    def parse_html(self, request_url):
        with requests.Session() as session:
            post = session.post(self.login_basic_url, data=self.payload)
            parsed_html = session.get(request_url)
        return parsed_html

    # scrape the post all
 which is the paragraph/content part
    def post_content(self):
        REQUEST_URL = f'https://m.facebook.com/story.php?story_fbid={self.post_ID}&id=7724542745'
        soup = BeautifulSoup(self.parse_html(REQUEST_URL).content, "html.parser")
        content = soup.find_all('p')
        post_content = []
        for lines in content:
            post_content.append(lines.text)
        post_content = ' '.join(post_content)
        return post_content

bot = faceBookBot()
bot.post_ID = "10158200911252746"

Body:
@AnsonChan yes, you could open the page with selenium, login and then copy it's cookies to requests:

from selenium import webdriver
import requests

driver = webdriver.Chrome()
driver.get('http://facebook.com')
# login manually, or automate it.
# when logged in:
session = requests.session()
[session.cookies.update({cookie['name']: cookie['value']}) for cookie in driver.get_cookies()]
driver.quit()
# get the page you want with requests
response = session.get('https://m.facebook.com/story.php?story_fbid=123456789')


问:
How to validate file type in ASP.NET MVC
In my registry of professionals I must allow them to upload their CV but how could I validate that the file type is only .docx and .pdf. Also, which has a maximum weight of 5mb?</p>
```<code>    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult RegistroProfesional(HttpPostedFileBase CV, TB_Profesionales p)
    {
        string n = Path.GetFileName(CV.FileName);
        string Folder = Path.Combine(Server.MapPath("~/ArchivosCV"), n);
        CV.SaveAs(Folder);
        int cant = db.SP_REGISTRAR_PROFESIONAL(p.TIPO, p.NOMBRE, p.APELLIDO, p.DNI,p.EMAIL, p.USUARIO, p.CLAVE, p.SEXO, p.FECHANAC, p.DISTRITO, p.IDSERVICIO, p.DESCRIPCIÓN, p.CV = n, p.PROMEDIOCAL);
        if (cant > 0)
        {
            return RedirectToAction("Login", "Login");
        }
        else
        {
            return RedirectToAction("ListadoProfesional");
        }
    }
</code>```

答:
Try below code</p>
```<code>        try
        {
            var supportedTypes = new[] { "docx","pdf"};
            var fileExt = System.IO.Path.GetExtension(CV.FileName).Substring(1);
            if (!supportedTypes.Contains(fileExt))
            {
                ErrorMessage = "File Extension Is Invalid - Only Upload DOCX and PDF";
                return ErrorMessage;
            }
            else if (CV.ContentLength > (filesize * 1024))
            {
                ErrorMessage = "File size Should Be UpTo " + filesize + "KB";
                return ErrorMessage;
            }
            else
            {
                ErrorMessage = "File Is Successfully Uploaded";
                return ErrorMessage;
            }
        }
        catch (Exception ex)
        {
            ErrorMessage = "Invalid file";
            return ErrorMessage;
        }
</code>```

Body:
In my registry of professionals I must allow them to upload their CV but how could I validate that the file type is only .docx and .pdf. Also, which has a maximum weight of 5mb?

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult RegistroProfesional(HttpPostedFileBase CV, TB_Profesionales p)
    {
        string n = Path.GetFileName(CV.FileName);
        string Folder = Path.Combine(Server.MapPath("~/ArchivosCV"), n);
        CV.SaveAs(Folder);
        int cant = db.SP_REGISTRAR_PROFESIONAL(p.TIPO, p.NOMBRE, p.APELLIDO, p.DNI,p.EMAIL, p.USUARIO, p.CLAVE, p.SEXO, p.FECHANAC, p.DISTRITO, p.IDSERVICIO, p.DESCRIPCIÓN, p.CV = n, p.PROMEDIOCAL);
        if (cant > 0)
        {
            return RedirectToAction("Login", "Login");
        }
        else
        {
            return RedirectToAction("ListadoProfesional");
        }
    }

Body:
Try below code

        try
        {
            var supportedTypes = new[] { "docx","pdf"};
            var fileExt = System.IO.Path.GetExtension(CV.FileName).Substring(1);
            if (!supportedTypes.Contains(fileExt))
            {
                ErrorMessage = "File Extension Is Invalid - Only Upload DOCX and PDF";
                return ErrorMessage;
            }
            else if (CV.ContentLength > (filesize * 1024))
            {
                ErrorMessage = "File size Should Be UpTo " + filesize + "KB";
                return ErrorMessage;
            }
            else
            {
                ErrorMessage = "File Is Successfully Uploaded";
                return ErrorMessage;
            }
        }
        catch (Exception ex)
        {
            ErrorMessage = "Invalid file";
            return ErrorMessage;
        }


答:
Since we are using a projection (from A& to std::string&), why is it trying to copy an A& instead of a std::string& to o?

Because that's what the algorithm does. copy_if always copies the source range into the provided iterator, for each element that satisfies the predicate.

The projection only affects the predicate - it's basically just doing function composition here - but it doesn't affect what the overall algorithm does.

This is true for all projections.

If what you want to copy are just the p subobjects, what you want to do instead is

ranges::copy_if(v | views::transform(&A::p), /* ... */);
```

Which now changes the source range that you're copying from.

For more on this topic, I wrote a whole post about it.

Title: Can't make my example using std::ranges::copy_if(R&& r, O result, Pred pred, Proj proj = {}) compile

Body:
Since we are using a projection (from A& to std::string&), why is it trying to copy an A& instead of a std::string& to o?

Because that's what the algorithm does. copy_if always copies the source range into the provided iterator, for each element that satisfies the predicate.

The projection only affects the predicate - it's basically just doing function composition here - but it doesn't affect what the overall algorithm does.

This is true for all projections.

If what you want to copy are just the p subobjects, what you want to do instead is

ranges::copy_if(v | views::transform(&A::p), /* ... */);

Which now changes the source range that you're copying from.

For more on this topic, I wrote a whole post about it.


问:
Apache Beam FileIO match - What's better/more efficient way to match files?
I'm just wondering - does the use of wildcard have an impact on how Beam matches files?
For instance, if I want to match a file with Apache Beam, is there an advantage if I'd specify a direct path to a file (i.e. folder/subfolder/file.txt).
Or, if I'd give just a wildcard to match() method as an input, would it be as efficient or worse, in terms of frameworks's performance?

Thanks

答: Compared to the cost of reading the file (and spinning up workers, if running on a distributed runner), the cost of matching will be negligible. On the other hand, multiple reads (with distinct direct paths) will generally be more overhead than reading a wildcard match.</p>


问:
Error: <path> attribute d: Expected number, "MNaN,36.393574100…
I am trying to build a simple line graph with d3.js and render it inside of a react component. Unfortunately, I get the following error:

```<code>Error: <path> attribute d: Expected number, "MNaN,36.393574100…"
</code>```

The stackoverflow similar questions have unfortunately not been very helpful to me. My theory is I am not reading in the data object correctly (data.forEach) to my "d" attribute when I append the path to the chart. I have tried doing this both WITH and WITHOUT the parseDate function. When I do it WITHOUT it my dates look how they should in the console. If I include the parseDate function later on in the code while I am declaring my x axis and/or inside the x function of the path d attribute...nothing really changes. I'm also not entirely sure if my data object is nested correctly or not or if I should be serving the entire data object to the datum() function ....

Here is my code:

```<code>import * as d3 from "d3";
import React, { useRef, useEffect } from "react";

function LineBarChart({ width, height, data }) {

const ref = useRef();

useEffect(() => {
 d3.select(ref.current)
  .attr("width", width)
  .attr("height", height)
  // .style("border", "1px solid black");
  }, [height, width]);

 useEffect(() => {
  draw();
 }, [data]);

const draw = () => {

const parseDate = d3.timeFormat("%Y-%m-%d");

data.forEach(function (d) {
 d.dateM = d.dateM;
 // d.dateM = parseDate(d.dateM);
 d.closeM = +d.closeM;
 console.log("WHAT-DO-I-LOOK-LIKE", d.dateM)
});

// const dataNest = d3.nest()
//   .key((d) => d.dateM)
//   .key((d) => d.closeM)
//   .map(data);

const margin = 80;
const width = 1400 - 2 * margin;
const height = 800 - 2 * margin;

const svg = d3.select(ref.current);

const chart = svg
  .append("g")
  .attr("class", "linechart")
  .attr("transform", `translate(${margin}, ${margin})`);

 var x = d3.scaleTime()
  .domain(d3.extent(data, function(d) { return d.dateM; }))
  //.domain(d3.extent(data, function(d) { return parseDate(d.dateM); }))
  .range([ 0, width ]);

 chart.append("g")
  .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(x));

 var y = d3.scaleLinear()
   .domain([0, d3.max(data, function(d) { return +d.closeM; })])
   .range([ height, 0 ]);

 chart.append("g")
  .call(d3.axisLeft(y));

 chart.append("path")
  .datum(data)
  .attr('class', 'lineChartClass')
  .attr("fill", "none")
  .attr("stroke", "steelblue")
  .attr("stroke-width", 1.5)
  .attr("d", d3.line()
    // .x(function(d) { return parseDate(d.dateM) })
    .x(function(d) { return x(d.dateM) })
    .y(function(d) { return y(d.closeM) })

  )

}; //draw

return (
 <div className="chartSixPointFive">
  <svg ref={ref}></svg>
 </div>

);
}

export default LineBarChart;
</code>```

My React app uses Axios to hit a Flask endpoint. There isn't anything fancy going on here, I just return the linechart like so.

```<code>....
....
<LineBarChart width={1350} height={1000} data={data} />
</code>```

I can include the full react code if necessary (although I don't think it's needed)

Here is what my data object looks like:

```<code>    "results": [
    {
        "date": "2022-02-07",
        "open": 172.860001,
        "high": 173.949997,
        "low": 170.949997,
        "close": 171.660004,
        "volume": 77045100,
        "adjClose": 171.660004
    },

    rinse & repeat ....

    ]
</code>```

I appreciate ANY help I can get!

答:
d3.timeFormat formats a date object and returns a string. What you want is d3.timeParse instead, which will do the opposite: it will parse your strings into date objects, that can be used by the time scale:

```<code>const parseDate = d3.timeParse("%Y-%m-%d");
</code>```

PS: the M command of the "d" attribute uses (as pretty much everything in every language) the coordinate pair x,y (horizontal/vertical). Thus, when you have the error:

```<code>MNaN,42...
</code>```

...you know the problem is in your x scale, while the error:

```<code>M42,NaN...
</code>```

...shows you the problem is in the y scale.

Body:
I am trying to build a simple line graph with d3.js and render it inside of a react component. Unfortunately, I get the following error:

Error: <path> attribute d: Expected number, "MNaN,36.393574100…"

The stackoverflow similar questions have unfortunately not been very helpful to me. My theory is I am not reading in the data object correctly (data.forEach) to my "d" attribute when I append the path to the chart. I have tried doing this both WITH and WITHOUT the parseDate function. When I do it WITHOUT it my dates look how they should in the console. If I include the parseDate function later on in the code while I am declaring my x axis and/or inside the x function of the path d attribute...nothing really changes. I'm also not entirely sure if my data object is nested correctly or not or if I should be serving the entire data object to the datum() function ....

Here is my code:

import * as d3 from "d3";
import React, { useRef, useEffect } from "react";

function LineBarChart({ width, height, data }) {

const ref = useRef();

useEffect(() => {
 d3.select(ref.current)
  .attr("width", width)
  .attr("height", height)
  // .style("border", "1px solid black");
  }, [height, width]);

 useEffect(() => {
  draw();
 }, [data]);

const draw = () => {

const parseDate = d3.timeFormat("%Y-%m-%d");

data.forEach(function (d) {
 d.dateM = d.dateM;
 // d.dateM = parseDate(d.dateM);
 d.closeM = +d.closeM;
 console.log("WHAT-DO-I-LOOK-LIKE", d.dateM)
});

// const dataNest = d3.nest()
//   .key((d) => d.dateM)
//   .key((d) => d.closeM)
//   .map(data);

const margin = 80;
const width = 1400 - 2 * margin;
const height = 800 - 2 * margin;

const svg = d3.select(ref.current);

const chart = svg
  .append("g")
  .attr("class", "linechart")
  .attr("transform", `translate(${margin}, ${margin})`);

 var x = d3.scaleTime()
  .domain(d3.extent(data, function(d) { return d.dateM; }))
  //.domain(d3.extent(data, function(d) { return parseDate(d.dateM); }))
  .range([ 0, width ]);

 chart.append("g")
  .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(x));

 var y = d3.scaleLinear()
   .domain([0, d3.max(data, function(d) { return +d.closeM; })])
   .range([ height, 0 ]);

 chart.append("g")
  .call(d3.axisLeft(y));

 chart.append("path")
  .datum(data)
  .attr('class', 'lineChartClass')
  .attr("fill", "none")
  .attr("stroke", "steelblue")
  .attr("stroke-width", 1.5)
  .attr("d", d3.line()
    // .x(function(d) { return parseDate(d.dateM) })
    .x(function(d) { return x(d.dateM) })
    .y(function(d) { return y(d.closeM) })

  )

}; //draw

return (

  <svg ref={ref}></svg>

);
}

export default LineBarChart;

My React app uses Axios to hit a Flask endpoint. There isn't anything fancy going on here, I just return the linechart like so.

....
....
<LineBarChart width={1350} height={1000} data={data} />

I can include the full react code if necessary (although I don't think it's needed)

Here is what my data object looks like:

    "results": [
    {
        "date": "2022-02-07",
        "open": 172.860001,
        "high": 173.949997,
        "low": 170.949997,
        "close": 171.660004,
        "volume": 77045100,
        "adjClose": 171.660004
    },

    rinse & repeat ....

    ]

I appreciate ANY help I can get!

Body:
d3.timeFormat formats a date object and returns a string. What you want is d3.timeParse instead, which will do the opposite: it will parse your strings into date objects, that can be used by the time scale:

const parseDate = d3.timeParse("%Y-%m-%d");

PS: the M command of the "d" attribute uses (as pretty much everything in every language) the coordinate pair x,y (horizontal/vertical). Thus, when you have the error:

MNaN,42...

...you know the problem is in your x scale, while the error:

M42,NaN...

...shows you the problem is in the y scale.


问:
ruby install rails with Error ---Failed to build gem native extension.（Windows platform）
Newbie to Ruby, and just started rails journey.

Have searched stackoverflow but many these questions came too long ago. My English is not good, please bear with my description. Here's my situation, hope I make myself clearly.

I'm on windows.
And when I took ruby lessons online I had installed an older envir-- ruby2.7.4 with  rails 7.0.1.

Yesterday I want to finish a new project with a newer ruby envir-- so i installed ruby3.1.0, but when I tried to install rails for this new Ruby verison, the ERROR occurred.

here is my envir:

```<code>C:\Users\Administrator>uru ls
    274p191     : ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x64-mingw32]
    310p0       : ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x64-mingw-ucrt]

C:\Users\Administrator>ruby -v
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x64-mingw-ucrt]

C:\Users\Administrator>gem -v
3.3.6

C:\Users\Administrator>d:

D:\>cd RubyDevkit

D:\RubyDevkit>ruby dk.rb install
[INFO] Skipping existing gem override for 'D:/Ruby27-x64'
[WARN] Skipping existing DevKit helper library for 'D:/Ruby27-x64'
[INFO] Skipping existing gem override for 'D:/Ruby31-x64'
[WARN] Skipping existing DevKit helper library for 'D:/Ruby31-x64'

D:\RubyDevkit>
</code>```

as you can see, my current ruby version is 3.1.0 and I think I have installed Devkit for both my Rubies successfully.

and I have intalled MSYS2 as well (although I dont know why ruby need it, but I have met another ERROR which remind me to install MSYS and add it to PATH)

I think I have done everything correctly before I met this ERROR:

```<code>C:\Users\Administrator>gem install rails
Building native extensions. This could take a while...
ERROR:  Error installing rails:
        ERROR: Failed to build gem native extension.

    current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
D:/Ruby31-x64/bin/ruby.exe -I D:/Ruby31-x64/lib/ruby/site_ruby/3.1.0 -r ./siteconf20220128-11340-9w9ez5.rb extconf.rb
creating Makefile

current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\= clean
current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\=
make failedNo such file or directory - make

Gem files will remain installed in D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5 for inspection.
Results logged to D:/Ruby31-x64/lib/ruby/gems/3.1.0/extensions/x64-mingw-ucrt/3.1.0/websocket-driver-0.7.5/gem_make.out
</code>```

There is no exceptions when I installed rails for ruby2.7.4(the same command just finished with no error or warning).

I cant find any similar situation online. Anyone could help?

-------------------01/29-----------------divide----------------------------

thank for comments, really thought-provking. Following some plans(Although failed)
i tried to install gem  websocket-driver separately, got the same error:

```<code>C:\Users\Administrator>gem install websocket-driver:0.7.5
Building native extensions. This could take a while...
ERROR:  Error installing websocket-driver:
        ERROR: Failed to build gem native extension.

    current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
D:/Ruby31-x64/bin/ruby.exe -I D:/Ruby31-x64/lib/ruby/site_ruby/3.1.0 -r ./siteconf20220129-3864-wqfhn2.rb extconf.rb
creating Makefile

current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\= clean
current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\=
make failedNo such file or directory - make

Gem files will remain installed in D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5 for inspection.
Results logged to D:/Ruby31-x64/lib/ruby/gems/3.1.0/extensions/x64-mingw-ucrt/3.1.0/websocket-driver-0.7.5/gem_make.out
</code>```

(delete this gem from root directory and reInstall got the same result as well)

Then I tired got this gem form bundler.rubygems.org, and install it locally. Got the same error:

```<code>C:\Users\Administrator>d:

D:\>cd rubyDownloads

D:\rubyDownloads>gem install websocket-driver-0.7.5.gem --local
Building native extensions. This could take a while...
ERROR:  Error installing websocket-driver-0.7.5.gem:
        ERROR: Failed to build gem native extension.

    current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
D:/Ruby31-x64/bin/ruby.exe -I D:/Ruby31-x64/lib/ruby/site_ruby/3.1.0 -r ./siteconf20220129-5896-885p1m.rb extconf.rb
creating Makefile

current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\= clean
current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\=
make failedNo such file or directory - make

Gem files will remain installed in D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5 for inspection.
Results logged to D:/Ruby31-x64/lib/ruby/gems/3.1.0/extensions/x64-mingw-ucrt/3.1.0/websocket-driver-0.7.5/gem_make.out
</code>```

Now the problem becomes: the gem websocket-driver and Any other gem that rely on it couldn't install successfully, and the ERROR is as shown above.

答:
Just back from a vacation, and this problem has been solved during the holiday. Forgive me for not remembering to close this issue until now.XD

With the help of Josien's comments, the problem has narrowed down to gem install websocket-driver failed. It's easier to find this problem on stackoverflow-Error when trying to install gem websocket-driver-0.7.3

Finally, I solved this problem just by typing ridk install (to make MSYS2 work for ruby3.0.1)——I have installed MSYS2 and make it work under ruby2.7.4, but if you want to make it work for ruby3.0.1 samely , you need to excute this command under ruby3.0.1.

After installed MSYS2 for ruby3.0.1, rails installed sucessfully.

```<code>C:\Users\Administrator>Uru ls
    274p191     : ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x64-mingw32]
    310p0       : ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x64-mingw-ucrt]

C:\Users\Administrator>ruby -v
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x64-mingw-ucrt]

C:\Users\Administrator>ridk install
 _____       _           _____           _        _ _         ___
|  __ \     | |         |_   _|         | |      | | |       |__ \
| |__) |   _| |__  _   _  | |  _ __  ___| |_ __ _| | | ___ _ __ ) |
|  _  / | | | '_ \| | | | | | | '_ \/ __| __/ _` | | |/ _ \ '__/ /
| | \ \ |_| | |_) | |_| |_| |_| | | \__ \ || (_| | | |  __/ | / /_
|_|  \_\__,_|_.__/ \__, |_____|_| |_|___/\__\__,_|_|_|\___|_||____|
                    __/ |           _
                   |___/          _|_ _  __   | | o __  _| _     _
                                   | (_) |    |^| | | |(_|(_)\^/_>

   1 - MSYS2 base installation
   2 - MSYS2 system update (optional)
   3 - MSYS2 and MINGW development toolchain

Which components shall be installed? If unsure press ENTER [1,3] 1,3

> sh -lc true
MSYS2 seems to be properly installed
Install MSYS2 and MINGW development toolchain ...
> pacman -S --needed --noconfirm autoconf autogen automake-wrapper diffutils file
...
...
...
Install MSYS2 and MINGW development toolchain succeeded

   1 - MSYS2 base installation
   2 - MSYS2 system update (optional)
   3 - MSYS2 and MINGW development toolchain

Which components shall be installed? If unsure press ENTER []

C:\Users\Administrator>gem install rails
Building native extensions. This could take a while...
Successfully installed websocket-driver-0.7.5
Building native extensions. This could take a while...
Successfully installed nio4r-2.5.8
Successfully installed actioncable-7.0.2
Successfully installed rails-7.0.2
Parsing documentation for websocket-driver-0.7.5
Installing ri documentation for websocket-driver-0.7.5
Parsing documentation for nio4r-2.5.8
Installing ri documentation for nio4r-2.5.8
Parsing documentation for actioncable-7.0.2
Installing ri documentation for actioncable-7.0.2
Parsing documentation for rails-7.0.2
Installing ri documentation for rails-7.0.2
Done installing documentation for websocket-driver, nio4r, actioncable, rails after 2 seconds
4 gems installed

C:\Users\Administrator>rails -v
Rails 7.0.2

</code>```

Body:
Newbie to Ruby, and just started rails journey.

Have searched stackoverflow but many these questions came too long ago. My English is not good, please bear with my description. Here's my situation, hope I make myself clearly.

I'm on windows.
And when I took ruby lessons online I had installed an older envir-- ruby2.7.4 with  rails 7.0.1.

Yesterday I want to finish a new project with a newer ruby envir-- so i installed ruby3.1.0, but when I tried to install rails for this new Ruby verison, the ERROR occurred.

here is my envir:

C:\Users\Administrator>uru ls
    274p191     : ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x64-mingw32]
    310p0       : ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x64-mingw-ucrt]

C:\Users\Administrator>ruby -v
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x64-mingw-ucrt]

C:\Users\Administrator>gem -v
3.3.6

C:\Users\Administrator>d:

D:\>cd RubyDevkit

D:\RubyDevkit>ruby dk.rb install
[INFO] Skipping existing gem override for 'D:/Ruby27-x64'
[WARN] Skipping existing DevKit helper library for 'D:/Ruby27-x64'
[INFO] Skipping existing gem override for 'D:/Ruby31-x64'
[WARN] Skipping existing DevKit helper library for 'D:/Ruby31-x64'

D:\RubyDevkit>

as you can see, my current ruby version is 3.1.0 and I think I have installed Devkit for both my Rubies successfully.

and I have intalled MSYS2 as well (although I dont know why ruby need it, but I have met another ERROR which remind me to install MSYS and add it to PATH)

I think I have done everything correctly before I met this ERROR:

C:\Users\Administrator>gem install rails
Building native extensions. This could take a while...
ERROR:  Error installing rails:
        ERROR: Failed to build gem native extension.

    current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
D:/Ruby31-x64/bin/ruby.exe -I D:/Ruby31-x64/lib/ruby/site_ruby/3.1.0 -r ./siteconf20220128-11340-9w9ez5.rb extconf.rb
creating Makefile

current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\= clean
current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\=
make failedNo such file or directory - make

Gem files will remain installed in D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5 for inspection.
Results logged to D:/Ruby31-x64/lib/ruby/gems/3.1.0/extensions/x64-mingw-ucrt/3.1.0/websocket-driver-0.7.5/gem_make.out

There is no exceptions when I installed rails for ruby2.7.4(the same command just finished with no error or warning).

I cant find any similar situation online. Anyone could help?

-------------------01/29-----------------divide----------------------------

thank for comments, really thought-provking. Following some plans(Although failed)
i tried to install gem  websocket-driver separately, got the same error:

C:\Users\Administrator>gem install websocket-driver:0.7.5
Building native extensions. This could take a while...
ERROR:  Error installing websocket-driver:
        ERROR: Failed to build gem native extension.

    current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
D:/Ruby31-x64/bin/ruby.exe -I D:/Ruby31-x64/lib/ruby/site_ruby/3.1.0 -r ./siteconf20220129-3864-wqfhn2.rb extconf.rb
creating Makefile

current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\= clean
current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\=
make failedNo such file or directory - make

Gem files will remain installed in D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5 for inspection.
Results logged to D:/Ruby31-x64/lib/ruby/gems/3.1.0/extensions/x64-mingw-ucrt/3.1.0/websocket-driver-0.7.5/gem_make.out

(delete this gem from root directory and reInstall got the same result as well)

Then I tired got this gem form bundler.rubygems.org, and install it locally. Got the same error:

C:\Users\Administrator>d:

D:\>cd rubyDownloads

D:\rubyDownloads>gem install websocket-driver-0.7.5.gem --local
Building native extensions. This could take a while...
ERROR:  Error installing websocket-driver-0.7.5.gem:
        ERROR: Failed to build gem native extension.

    current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
D:/Ruby31-x64/bin/ruby.exe -I D:/Ruby31-x64/lib/ruby/site_ruby/3.1.0 -r ./siteconf20220129-5896-885p1m.rb extconf.rb
creating Makefile

current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\= clean
current directory: D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5/ext/websocket-driver
make DESTDIR\=
make failedNo such file or directory - make

Gem files will remain installed in D:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/websocket-driver-0.7.5 for inspection.
Results logged to D:/Ruby31-x64/lib/ruby/gems/3.1.0/extensions/x64-mingw-ucrt/3.1.0/websocket-driver-0.7.5/gem_make.out

Now the problem becomes: the gem websocket-driver and Any other gem that rely on it couldn't install successfully, and the ERROR is as shown above.

Body:
Just back from a vacation, and this problem has been solved during the holiday. Forgive me for not remembering to close this issue until now.XD

With the help of Josien's comments, the problem has narrowed down to gem install websocket-driver failed. It's easier to find this problem on stackoverflow-Error when trying to install gem websocket-driver-0.7.3

Finally, I solved this problem just by typing ridk install (to make MSYS2 work for ruby3.0.1)——I have installed MSYS2 and make it work under ruby2.7.4, but if you want to make it work for ruby3.0.1 samely , you need to excute this command under ruby3.0.1.

After installed MSYS2 for ruby3.0.1, rails installed sucessfully.

C:\Users\Administrator>Uru ls
    274p191     : ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x64-mingw32]
    310p0       : ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x64-mingw-ucrt]

C:\Users\Administrator>ruby -v
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x64-mingw-ucrt]

C:\Users\Administrator>ridk install
 _____       _           _____           _        _ _         ___
|  __ \     | |         |_   _|         | |      | | |       |__ \
| |__) |   _| |__  _   _  | |  _ __  ___| |_ __ _| | | ___ _ __ ) |
|  _  / | | | '_ \| | | | | | | '_ \/ __| __/ _` | | |/ _ \ '__/ /
| | \ \ |_| | |_) | |_| |_| |_| | | \__ \ || (_| | | |  __/ | / /_
|_|  \_\__,_|_.__/ \__, |_____|_| |_|___/\__\__,_|_|_|\___|_||____|
                    __/ |           _
                   |___/          _|_ _  __   | | o __  _| _     _
                                   | (_) |    |^| | | |(_|(_)\^/_>

   1 - MSYS2 base installation
   2 - MSYS2 system update (optional)
   3 - MSYS2 and MINGW development toolchain

Which components shall be installed? If unsure press ENTER [1,3] 1,3

> sh -lc true
MSYS2 seems to be properly installed
Install MSYS2 and MINGW development toolchain ...
> pacman -S --needed --noconfirm autoconf autogen automake-wrapper diffutils file
...
...
...
Install MSYS2 and MINGW development toolchain succeeded

   1 - MSYS2 base installation
   2 - MSYS2 system update (optional)
   3 - MSYS2 and MINGW development toolchain

Which components shall be installed? If unsure press ENTER []

C:\Users\Administrator>gem install rails
Building native extensions. This could take a while...
Successfully installed websocket-driver-0.7.5
Building native extensions. This could take a while...
Successfully installed nio4r-2.5.8
Successfully installed actioncable-7.0.2
Successfully installed rails-7.0.2
Parsing documentation for websocket-driver-0.7.5
Installing ri documentation for websocket-driver-0.7.5
Parsing documentation for nio4r-2.5.8
Installing ri documentation for nio4r-2.5.8
Parsing documentation for actioncable-7.0.2
Installing ri documentation for actioncable-7.0.2
Parsing documentation for rails-7.0.2
Installing ri documentation for rails-7.0.2
Done installing documentation for websocket-driver, nio4r, actioncable, rails after 2 seconds
4 gems installed

C:\Users\Administrator>rails -v
Rails 7.0.2


问:
How can i create fake functions to test the components?
I have to test my component, but that component have a function for that it can work. This function need a key environment.

My component has a onMounted function

```<code> onMounted(async () => {

    const available = await getAvailableBills()
 }
</code>```

and the implementation is:

```<code>export const getAvailableBills = async () => {
  try {
    const bill = `${process.env.VAR_ENV}`
</code>```

I am getting this error:

```<code>Error: Provided address {VAR_ENV} is invalid
</code>```

But I Think I don't need to execute the real functions. I would like to create fake functions in my test

```<code>import { shallowMount } from '@vue/test-utils'
import { createApp } from 'vue'
import { createStore } from 'vuex'
import App from '@/components/tables'

const store = createStore({
  state() {
    return {
      user: {},
    }
  },
  mutations: {},
})

let wrapper
const app = createApp(App)
app.use(store)
beforeEach(() => {
  wrapper = shallowMount(App, {
    propsData: {
      whitelist: [1,2,3],
    },
    global: {
      plugins: [store],
    },
  })
})

describe('Tables', () => {
  it('test 1', () => {
    expect(wrapper.props().list).toEqual([1,2,3])
  })
})
</code>```

答:
You have to mock it. It seems like you're using jest. You can use jest.spyOn method to mock a particular object, or mock the whole file using jest.mock.

For example, if you have

```<code>src /
- app.js
- app.spec.js
- services /
--- bills.js
</code>```

In your src/app.spec.js, if you're exported function getAvailableBills is in src/services/bills.js, just do this :

import { shallowMount } from '@vue/test-utils'
import { createApp } from 'vue'
import { createStore } from 'vuex'
import App from '@/components/tables'

import { getAvailableBills } from './services/bills'
jest.mock('./services/bills', {
  getAvailableBills: jest.fn().mockResolvedValue({ bills: ['...'] }) // you can mock the response directly here for the whole file
})

getAvailableBills.mockResolvedValue({ bills: ['...'] }) // or you can mock the response when you need to mock it

const store = createStore({
  state() {
    return {
      user: {},
    }
  },
  mutations: {},
})

let wrapper
const app = createApp(App)
app.use(store)
beforeEach(() => {
  wrapper = shallowMount(App, {
    propsData: {
      whitelist: [1,2,3],
    },
    global: {
      plugins: [store],
    },
  })
})

describe('Tables', () => {
  it('test 1', () => {
    expect(wrapper.props().list).toEqual([1,2,3])
  })
})
```

Notice that I used mockResolvedValue because it's returning a Promise (async method), but if it returns a direct value and not a promise, use mockReturnValue. You can also mock only once when needed with mockResolvedValueOnce and mockReturnValueOnce, or mock a rejected promise with mockRejectedValue.

Btw, you better encapsulate the tests inside describe, it avoids some errors, and it makes more readable & writable tests. You should also mock inside beforeAll / beforeEach methods to mock once for multiple tests when needed.

If needed, you can also add a setup files where you instantiate fake env values inside jest.config. That's not the point but it may help you one day.

Body:
I have to test my component, but that component have a function for that it can work. This function need a key environment.

My component has a onMounted function

 onMounted(async () => {

    const available = await getAvailableBills()
 }

and the implementation is:

export const getAvailableBills = async () => {
  try {
    const bill = `${process.env.VAR_ENV}`

I am getting this error:

Error: Provided address {VAR_ENV} is invalid

But I Think I don't need to execute the real functions. I would like to create fake functions in my test

import { shallowMount } from '@vue/test-utils'
import { createApp } from 'vue'
import { createStore } from 'vuex'
import App from '@/components/tables'

const store = createStore({
  state() {
    return {
      user: {},
    }
  },
  mutations: {},
})

let wrapper
const app = createApp(App)
app.use(store)
beforeEach(() => {
  wrapper = shallowMount(App, {
    propsData: {
      whitelist: [1,2,3],
    },
    global: {
      plugins: [store],
    },
  })
})

describe('Tables', () => {
  it('test 1', () => {
    expect(wrapper.props().list).toEqual([1,2,3])
  })
})

Body:
You have to mock it. It seems like you're using jest. You can use jest.spyOn method to mock a particular object, or mock the whole file using jest.mock.

For example, if you have

src /
- app.js
- app.spec.js
- services /
--- bills.js

In your src/app.spec.js, if you're exported function getAvailableBills is in src/services/bills.js, just do this :

import { shallowMount } from '@vue/test-utils'
import { createApp } from 'vue'
import { createStore } from 'vuex'
import App from '@/components/tables'

import { getAvailableBills } from './services/bills'
jest.mock('./services/bills', {
  getAvailableBills: jest.fn().mockResolvedValue({ bills: ['...'] }) // you can mock the response directly here for the whole file
})

getAvailableBills.mockResolvedValue({ bills: ['...'] }) // or you can mock the response when you need to mock it

const store = createStore({
  state() {
    return {
      user: {},
    }
  },
  mutations: {},
})

let wrapper
const app = createApp(App)
app.use(store)
beforeEach(() => {
  wrapper = shallowMount(App, {
    propsData: {
      whitelist: [1,2,3],
    },
    global: {
      plugins: [store],
    },
  })
})

describe('Tables', () => {
  it('test 1', () => {
    expect(wrapper.props().list).toEqual([1,2,3])
  })
})

Notice that I used mockResolvedValue because it's returning a Promise (async method), but if it returns a direct value and not a promise, use mockReturnValue. You can also mock only once when needed with mockResolvedValueOnce and mockReturnValueOnce, or mock a rejected promise with mockRejectedValue.

Btw, you better encapsulate the tests inside describe, it avoids some errors, and it makes more readable & writable tests. You should also mock inside beforeAll / beforeEach methods to mock once for multiple tests when needed.

If needed, you can also add a setup files where you instantiate fake env values inside jest.config. That's not the point but it may help you one day.


问:
Setting permission for shared memory created by boost
We open a boost shared memory that was created by another process like this

```<code>  boost::interprocess::managed_shared_memory segment(boost::interprocess::open_only, "SharedMem");
</code>```

But if the process that created the shared memory was a root user, then the process reading it, if it was a normal user, will fail with the reason as:

```<code>terminate called after throwing an instance of 'boost::interprocess::interprocess_exception'
what():  Permission denied
</code>```

What should i do to avoid this? that is to give permission to the shared memory to all?

答:
Below is a sample code snippet, to grant unrestricted permission to shared memory during creation</p>
```<code>boost::interprocess::permissions  unrestricted_permissions;
unrestricted_permissions.set_unrestricted();

shared_mem = new managed_shared_memory(open_or_create, name.c_str(), size, 0, unrestricted_permissions);

</code>```

Body:
We open a boost shared memory that was created by another process like this

  boost::interprocess::managed_shared_memory segment(boost::interprocess::open_only, "SharedMem");

But if the process that created the shared memory was a root user, then the process reading it, if it was a normal user, will fail with the reason as:

terminate called after throwing an instance of 'boost::interprocess::interprocess_exception'
what():  Permission denied

What should i do to avoid this? that is to give permission to the shared memory to all?

Body:
Below is a sample code snippet, to grant unrestricted permission to shared memory during creation

boost::interprocess::permissions  unrestricted_permissions;
unrestricted_permissions.set_unrestricted();

shared_mem = new managed_shared_memory(open_or_create, name.c_str(), size, 0, unrestricted_permissions);


问:
How to implement a PUT correctly in Spring boot?
Attempt to correctly implement a Put, according to RFC 2616.

"The PUT method requests that the enclosed entity be stored under the
supplied Request-URI. If the Request-URI refers to an already existing
resource, the enclosed entity SHOULD be considered as a modified
version of the one residing on the origin server. If the Request-URI
does not point to an existing resource, and that URI is capable of
being defined as a new resource by the requesting user agent, the
origin server can create the resource with that URI."

In short, if the resource exists, it is modified; if it does not exist it is created according to the provided id.
Now I have such an implementation, RFC 2616 states that when an element is created it must also return the location URI.
And I am left like this.

```<code>@PutMapping("/{id}")
public ResponseEntity<Item> update(
       @PathVariable("id") Long id,
       @RequestBody Item updatedItem) {

   Optional<Item> updated = service.update(id, updatedItem);

   return updated
           .map(value -> ResponseEntity.ok().body(value))
           .orElseGet(() -> {
               Item created = service.create(updatedItem);
               URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                       .path("/{id}")
                       .buildAndExpand(created.getId())
                        .toUri();
                   return ResponseEntity.created(location).body(created);
            });
}
</code>```

The problem is that the Optional, so for good practices it should be consumed and checked in the services.

The other problem is the mapping of the entity, which should go in the service, but I don't know how to separate it if the URI location goes in between...

Any solution on how to refactor it?
Considering that the optionals are consumed in the service and the same with the mapping, but the URI goes in the controller.

答:
The http protocol rules are not strictly applied when it comes to implements REST Api with Data Access Layer. Lets say, in the case of Spring Boot or any another framework, the URI you execute just trigger a Service that access to your database.

The reality is that, you can even use a GET or a POST in place of the PUT, it will work.
Here, your resources is a database or your records (in your database), and your http calls don't directly interact with your database, but it is your Service Layer throught your Spring Data Api.

In short, if the resource exists, it is modified; if it does not exist
it is created according to the provided id

In this case this is your save method that will create or update your record.

I would suggest you this refactoring :

```<code>@PutMapping("/items")
public ResponseEntity<Item> update(
       @PathVariable("id") Long id,
       @RequestBody Item itemToUpdate) {

   Optional<Item> updatedItem = service.update(id, itemToUpdate);

   return updatedItem
           .map(value -> ResponseEntity.ok().body(value))
           .orElseGet(() -> {
               Item createdItem = service.create(itemToUpdate);
               URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                       .path("/{id}")
                       .buildAndExpand(createdItem.getId())
                        .toUri();
                return ResponseEntity.created(location).body(createdItem);
        });
}
</code>```

Body:
Attempt to correctly implement a Put, according to RFC 2616.

"The PUT method requests that the enclosed entity be stored under the
supplied Request-URI. If the Request-URI refers to an already existing
resource, the enclosed entity SHOULD be considered as a modified
version of the one residing on the origin server. If the Request-URI
does not point to an existing resource, and that URI is capable of
being defined as a new resource by the requesting user agent, the
origin server can create the resource with that URI."

In short, if the resource exists, it is modified; if it does not exist it is created according to the provided id.
Now I have such an implementation, RFC 2616 states that when an element is created it must also return the location URI.
And I am left like this.

@PutMapping("/{id}")
public ResponseEntity<Item> update(
       @PathVariable("id") Long id,
       @RequestBody Item updatedItem) {

   Optional<Item> updated = service.update(id, updatedItem);

   return updated
           .map(value -> ResponseEntity.ok().body(value))
           .orElseGet(() -> {
               Item created = service.create(updatedItem);
               URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                       .path("/{id}")
                       .buildAndExpand(created.getId())
                        .toUri();
                   return ResponseEntity.created(location).body(created);
            });
}

The problem is that the Optional, so for good practices it should be consumed and checked in the services.

The other problem is the mapping of the entity, which should go in the service, but I don't know how to separate it if the URI location goes in between...

Any solution on how to refactor it?
Considering that the optionals are consumed in the service and the same with the mapping, but the URI goes in the controller.

Body:
The http protocol rules are not strictly applied when it comes to implements REST Api with Data Access Layer. Lets say, in the case of Spring Boot or any another framework, the URI you execute just trigger a Service that access to your database.

The reality is that, you can even use a GET or a POST in place of the PUT, it will work.
Here, your resources is a database or your records (in your database), and your http calls don't directly interact with your database, but it is your Service Layer throught your Spring Data Api.

In short, if the resource exists, it is modified; if it does not exist
it is created according to the provided id

In this case this is your save method that will create or update your record.

I would suggest you this refactoring :

@PutMapping("/items")
public ResponseEntity<Item> update(
       @PathVariable("id") Long id,
       @RequestBody Item itemToUpdate) {

   Optional<Item> updatedItem = service.update(id, itemToUpdate);

   return updatedItem
           .map(value -> ResponseEntity.ok().body(value))
           .orElseGet(() -> {
               Item createdItem = service.create(itemToUpdate);
               URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                       .path("/{id}")
                       .buildAndExpand(createdItem.getId())
                        .toUri();
                return ResponseEntity.created(location).body(createdItem);
        });
}


问:
How to read large csv from Azure container using Python Azure Function?
I need to read a larger csv efficiently from container using Python Azure Function.

I am using the below code for reading csv, it works fine for small csv but there must be some other way to read larger csv efficiently.

```<code># Container Connection.
container_client1 = ContainerClient.from_connection_string(
    conn_str=conn_str,
    container_name=container_name
    )

# Reading File.
downloaded_blob = container_client.download_blob(file_name.csv)
df = pd.read_csv(StringIO(downloaded_blob.content_as_text()))
</code>```

The Above function taking too much time to read ~2gb file.
I need help to efficiently read the larger csv using Python Azure Function.

答:
One of the workaround is to process the file in chunks, resulting in lower memory use while parsing.

chunksize = 10 ** 6
for chunk in pd.read_csv(filename, chunksize=chunksize):
    process(chunk)
```

NOTE:- chunksize parameter refers to number of rows per chunk.

Here is a thread that you can refer to.

REFERENCES:
pandas.read_csv

Body:
I need to read a larger csv efficiently from container using Python Azure Function.

I am using the below code for reading csv, it works fine for small csv but there must be some other way to read larger csv efficiently.

# Container Connection.
container_client1 = ContainerClient.from_connection_string(
    conn_str=conn_str,
    container_name=container_name
    )

# Reading File.
downloaded_blob = container_client.download_blob(file_name.csv)
df = pd.read_csv(StringIO(downloaded_blob.content_as_text()))

The Above function taking too much time to read ~2gb file.
I need help to efficiently read the larger csv using Python Azure Function.

Body:
One of the workaround is to process the file in chunks, resulting in lower memory use while parsing.

chunksize = 10 ** 6
for chunk in pd.read_csv(filename, chunksize=chunksize):
    process(chunk)

NOTE:- chunksize parameter refers to number of rows per chunk.

Here is a thread that you can refer to.

REFERENCES:
pandas.read_csv


问:
How to install VirtualBox Guest Addition on TinyCore
When I run "sudo sh VBoxLinuxAdditions.run" just like on Ubuntu or CentOS, TinyCore throws errors and failed, and the /var/log/vboxadd-install.log shows that:

```<code>/tmp/vbox.0/Makefile.include.header:97: *** Error:
unable to find the sources of your current Linux kernel.
Specify KERN_DIR=<directory> and run Make again.  Stop.
</code>```

I have used tce tools installed some packets such as gcc, make, linux-kernel-sources-env.tcz, linux-3.16.2_api_headers.tcz, and then the VBoxLinuxAdditions.run can get the KERN_DIR, but no KERN_INC at this time.

If anybody has done this before could you please give me some points? I really don't know which packages should be installed in TinyCore to make VBoxAdditions work. My VBox and TinyCore is up to date. Thanks.

答:
Found a repo for this issue, but have yet to verify:

https://github.com/MSumulong/vmware-tools-on-tiny-core-linux

Prior to that, I attempted to follow this tutorial, but it is incomplete:

https://www.gilesorr.com/blog/tcl641-guest-additions.html

Prior to that, attempted to build the kernel headers (/lib/modules/5.15.10-tinycore) with no luck.

Copying the Guest Additions to home folder and running sudo VBoxLinuxAdditions.run returns "Kernel headers not found for target kernel 5.15.10-tinycore. Please install them and execute /sbin/rcvboxadd setup"

Body:
When I run "sudo sh VBoxLinuxAdditions.run" just like on Ubuntu or CentOS, TinyCore throws errors and failed, and the /var/log/vboxadd-install.log shows that:

/tmp/vbox.0/Makefile.include.header:97: *** Error:
unable to find the sources of your current Linux kernel.
Specify KERN_DIR=<directory> and run Make again.  Stop.

I have used tce tools installed some packets such as gcc, make, linux-kernel-sources-env.tcz, linux-3.16.2_api_headers.tcz, and then the VBoxLinuxAdditions.run can get the KERN_DIR, but no KERN_INC at this time.

If anybody has done this before could you please give me some points? I really don't know which packages should be installed in TinyCore to make VBoxAdditions work. My VBox and TinyCore is up to date. Thanks.


问:
Convert PST to UTC using Moment?
Is it possible to convert
Mon, Feb 07, 2022 08:30:30 PM Pacific Time
to UTC time format using moment?

I've tried doing this

```<code>const endTime = timezone(filteredTimeLeft, "ddd, MMM DD, YYYY hh:mm:ss a").utcOffset(-8);'
</code>```

But how does Moment know the inserted date parameter is pacific time ?

答:
Moment is EOL so I'll start with its successor luxon.

PST is an ambiguous definition  of a timezone so not supported. Use UTC-8 if you meant the exact offset or an IANA America/Los_Angeles etc if you want to cater for day light savings.

```<code>const { DateTime } = require('luxon')
const filteredTimeLeft = "Mon, Feb 07, 2022 08:30:30 PM"

// Create a DateTime in the default zone
const local = DateTime.fromFormat(filteredTimeLeft, "EEE, MMM dd, yyyy hh:mm:ss a")
console.log(local.toLocaleString(DateTime.DATETIME_FULL))
//=> 7 February 2022, 8:30 pm GMT+1

// Set a custom zone, don't adjust the time
const local_set_pst = local.setZone("UTC-8", { keepLocalTime: true })
console.log(local_set_pst.toLocaleString(DateTime.DATETIME_FULL))
//=> 7 February 2022, 8:30 pm GMT-8

// Adjust to UTC
const pst_adjust_utc = local_set_pst.toUTC()
console.log(pst_adjust_utc.toLocaleString(DateTime.DATETIME_FULL))
//=> 8 February 2022, 4:30 am UTC
</code>```

In one step

```<code>const pst_utc = DateTime.fromFormat(
  filteredTimeLeft,
  "EEE, MMM dd, yyyy hh:mm:ss a",
  { zone: "UTC-8" }
).toUTC()
console.log(pst_utc.toLocaleString(DateTime.DATETIME_FULL))
//=> 8 February 2022, 4:30 am UTC
</code>```

Similar in moment, which is a good example of why the API changed for luxon:

```<code>const moment = require('moment-timezone')
const filteredTimeLeft = "Mon, Feb 07, 2022 08:30:30 PM"

// Read time, setting a zone
const endTime = moment.tz(filteredTimeLeft, "ddd, MMM DD, YYYY hh:mm:ss a", "America/Los_Angeles")
console.log(endTime.format("ddd, MMM DD YYYY, h:mm:ss a"))
// Mon, Feb 07 2022, 8:30:30 pm

// UTC
const utcEndTime = endTime.clone().utc()
console.log(utcEndTime.format("ddd, MMM DD YYYY, h:mm:ss a"))
// Tue, Feb 08 2022, 4:30:30 am

// be careful with moment though, if you don't clone(), endTime will be modified
endTime.utc()
console.log(endTime.format("ddd, MMM DD YYYY, h:mm:ss a"))
// Tue, Feb 08 2022, 4:30:30 am

</code>```

Title: Convert PST to UTC using Moment?

Body:
Is it possible to convert
Mon, Feb 07, 2022 08:30:30 PM Pacific Time
to UTC time format using moment?

I've tried doing this

const endTime = timezone(filteredTimeLeft, "ddd, MMM DD, YYYY hh:mm:ss a").utcOffset(-8);'

But how does Moment know the inserted date parameter is pacific time ?

Body:
Moment is EOL so I'll start with its successor luxon.

PST is an ambiguous definition  of a timezone so not supported. Use UTC-8 if you meant the exact offset or an IANA America/Los_Angeles etc if you want to cater for day light savings.

const { DateTime } = require('luxon')
const filteredTimeLeft = "Mon, Feb 07, 2022 08:30:30 PM"

// Create a DateTime in the default zone
const local = DateTime.fromFormat(filteredTimeLeft, "EEE, MMM dd, yyyy hh:mm:ss a")
console.log(local.toLocaleString(DateTime.DATETIME_FULL))
//=> 7 February 2022, 8:30 pm GMT+1

// Set a custom zone, don't adjust the time
const local_set_pst = local.setZone("UTC-8", { keepLocalTime: true })
console.log(local_set_pst.toLocaleString(DateTime.DATETIME_FULL))
//=> 7 February 2022, 8:30 pm GMT-8

// Adjust to UTC
const pst_adjust_utc = local_set_pst.toUTC()
console.log(pst_adjust_utc.toLocaleString(DateTime.DATETIME_FULL))
//=> 8 February 2022, 4:30 am UTC

In one step

const pst_utc = DateTime.fromFormat(
  filteredTimeLeft,
  "EEE, MMM dd, yyyy hh:mm:ss a",
  { zone: "UTC-8" }
).toUTC()
console.log(pst_utc.toLocaleString(DateTime.DATETIME_FULL))
//=> 8 February 2022, 4:30 am UTC

Similar in moment, which is a good example of why the API changed for luxon:

const moment = require('moment-timezone')
const filteredTimeLeft = "Mon, Feb 07, 2022 08:30:30 PM"

// Read time, setting a zone
const endTime = moment.tz(filteredTimeLeft, "ddd, MMM DD, YYYY hh:mm:ss a", "America/Los_Angeles")
console.log(endTime.format("ddd, MMM DD YYYY, h:mm:ss a"))
// Mon, Feb 07 2022, 8:30:30 pm

// UTC
const utcEndTime = endTime.clone().utc()
console.log(utcEndTime.format("ddd, MMM DD YYYY, h:mm:ss a"))
// Tue, Feb 08 2022, 4:30:30 am

// be careful with moment though, if you don't clone(), endTime will be modified
endTime.utc()
console.log(endTime.format("ddd, MMM DD YYYY, h:mm:ss a"))
// Tue, Feb 08 2022, 4:30:30 am


问:
MariaDB THREADS_CONNECTED build up with php-fpm static

The Problem

My app is written in PHP, on top of Laravel.

Every hour, I have to restart php-fpm to prevent the MariaDB database from hitting max_connections = 150 and disabling the app since no more connections can be created.

Diagnostic Information

PHP-FPM is configured as static, with a max child count of 39.

DB connections are not configured as persistent.

Raising max_connections above 150 only delays the issue.

There are three DB nodes and three app nodes. The app nodes only talk to their partner DB node in the same region.

The DB nodes are replicating to each other via Galera.

The DB nodes have too many connections independently, not as a cluster.

Checking show full processlist shows me that the vast majority of connections are in SLEEP state and doing nothing.

Using the remote port from the processlist and ss on the app node as well as the php-fpm status page, I've determined that the children holding the connections open are themselves in idle state.

Attempted solutions

I've switched php-fpm to dynamic and set the idle-timout to 10s. The children do not quit, and I can't see any errors.

I've turned down the number of requests a php-child can handle before it is reaped from 100 to 1 with no effect.

I've registered a shutdown handler with PHP that checks if my DB connection is open and produces an alert. No alerts have been sent.

I've set up a cronjob to systemctl restart php7.4-fpm every hour. This alleviates the issue, but obviously isn't a good solution.

Questions

Under what circumstance does php-fpm maintain a DB connection beyond the end of a script or request?

How do I stop it from doing that?

Thanks for reading and any idea that might help.

答:
"I've registered a shutdown handler with PHP that checks if my DB connection is open and produces an alert. No alerts have been sent."

Regardless of whether you're getting the alert, are you certain that this is running explicitly?

```<code>DB::disconnect('yourdatabase');
</code>```

If you think you've got that covered, looking at the PDO object that Laravel is using internally may help. Instead of just debugging via an alert, which may be getting lost somewhere, logging a positive assertion that the connection is closed could be helpful.

```<code>$pdo = DB::connection()->getPdo();
</code>```
```<code>// My "positive assertion" comment is because this can be deceiving:
alertThatDoesNotWork();

// Instead, with odd problems like this, I prefer to do
logStateOfDatabaseConnection();

</code>```

Without more information, my best guess is related to "not guilty" !== "innocent". The log-regardless-of-state approach reduces the outputs to "innocent" and "guilty" with no ambiguity.

2022-02-09 Edit:
Have you checked for a persistence setting? I think this may conventionally be in a database.php or somesuch:

```<code>  'options' => [
    \PDO::ATTR_PERSISTENT => true
  ]
</code>```

If you can find a use of "PTO::ATTR_PERSISTENT" in setting up the database, then that may be the culprit. If you're able to test after switching it off, it may at least clearly define the problem at the cost of some latency.

It's still a bug, but potentially one you can't do much about if the PDO object's destructor is never running. From the manual:

The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.

Perhaps the script is ending without destroying your PDO object, and the harder-stop of the process ending (and thus freeing the db resource) isn't happening because the fpm process is persistent. A hail-Mary attempt would be throwing a gc_collect_cycles(); into your shutdown function with the hope that it works around a resource leak. If there are still references to the PDO object that Laravel hasn't cleaned up, then to some extent, it's their bug. If you can hunt down references to the object, including those in your debug code, and destroy them, perhaps you can drive the ref-count to 0 so that it gets cleaned up properly. I'm not saying that we should all go back to malloc() and free(), but sometimes this "convenience features" aren't so convenient. :-/

Body:
"I've registered a shutdown handler with PHP that checks if my DB connection is open and produces an alert. No alerts have been sent."

Regardless of whether you're getting the alert, are you certain that this is running explicitly?

DB::disconnect('yourdatabase');

If you think you've got that covered, looking at the PDO object that Laravel is using internally may help. Instead of just debugging via an alert, which may be getting lost somewhere, logging a positive assertion that the connection is closed could be helpful.

$pdo = DB::connection()->getPdo();

// My "positive assertion" comment is because this can be deceiving:
alertThatDoesNotWork();

// Instead, with odd problems like this, I prefer to do
logStateOfDatabaseConnection();

Without more information, my best guess is related to "not guilty" !== "innocent". The log-regardless-of-state approach reduces the outputs to "innocent" and "guilty" with no ambiguity.

2022-02-09 Edit:
Have you checked for a persistence setting? I think this may conventionally be in a database.php or somesuch:

  'options' => [
    \PDO::ATTR_PERSISTENT => true
  ]

If you can find a use of "PTO::ATTR_PERSISTENT" in setting up the database, then that may be the culprit. If you're able to test after switching it off, it may at least clearly define the problem at the cost of some latency.

It's still a bug, but potentially one you can't do much about if the PDO object's destructor is never running. From the manual:

The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.

Perhaps the script is ending without destroying your PDO object, and the harder-stop of the process ending (and thus freeing the db resource) isn't happening because the fpm process is persistent. A hail-Mary attempt would be throwing a gc_collect_cycles(); into your shutdown function with the hope that it works around a resource leak. If there are still references to the PDO object that Laravel hasn't cleaned up, then to some extent, it's their bug. If you can hunt down references to the object, including those in your debug code, and destroy them, perhaps you can drive the ref-count to 0 so that it gets cleaned up properly. I'm not saying that we should all go back to malloc() and free(), but sometimes this "convenience features" aren't so convenient. :-/


问:
How to transform text categories into ones and zeros vector in Python for classification?
I'm preparing data for multilabel classification and I want to transform text categories into zeros/ones vector.

Example input (dataframe):

```<code>['a']
['b']
['e', 'd']
['a', 'g', 'x']
</code>```

etc.
example output:

```<code>[1 0 0 0 0 0 0 ... 0]
[0 1 0 0 0 0 0 ... 0]
[0 0 0 1 1 0 0 ... 0]
[1 0 0 0 0 0 1 ... 0]
</code>```

etc.

There's 200 categories.
I haven't found how to do it when you have to deal with multilabel classification.
Any help would be appreciated.

答:
Let us just try MultiLabelBinarizer from sklearn

```<code>from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

s = mlb.fit_transform(df['a'])
s
Out[239]:
array([[1, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0],
       [1, 0, 0, 0, 1, 1]])
</code>```

Body:
I'm preparing data for multilabel classification and I want to transform text categories into zeros/ones vector.

Example input (dataframe):

['a']
['b']
['e', 'd']
['a', 'g', 'x']

etc.
example output:

[1 0 0 0 0 0 0 ... 0]
[0 1 0 0 0 0 0 ... 0]
[0 0 0 1 1 0 0 ... 0]
[1 0 0 0 0 0 1 ... 0]

etc.

There's 200 categories.
I haven't found how to do it when you have to deal with multilabel classification.
Any help would be appreciated.

Body:
Let us just try MultiLabelBinarizer from sklearn

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

s = mlb.fit_transform(df['a'])
s
Out[239]:
array([[1, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0],
       [1, 0, 0, 0, 1, 1]])


问:
Authoring Gradle task with int[] input property
I am authoring a Gradle task that needs to work on an array of integers (dispatching a job for each value).

I don't understand how to properly use Property<T> to accept a Groovy array syntax.

The following does not work

Inside Task.java

```<code>/**
 * Element IDs to download
 */
@Input
protected abstract Property<Integer[]> getTaxonomyElementIds();
</code>```

Inside build.gradle

```<code>taxonomyElementIds = [200, 201, 202, 203, 204, 205, 206, 207, 208]
</code>```

Error

```<code>Cannot set the value of task ':downloadMasterDb' property 'taxonomyElementIds' of type [Ljava.lang.Integer; using an instance of type java.util.ArrayList.
</code>```

The following workaround works, but I prefer a cleaner Groovy syntax

```<code>    taxonomyElementIds = [200, 201, 202, 203, 204, 205, 206, 207, 208].toArray(Integer[]::new)
</code>```

I also tried to change the type of the property to List<Integer> (which essentially doesn't change my task code) but doesn't work.

How can I change my property in order to accept the Groovy array/list syntax?

答:
Use ListProperty<Integer> instead:

```<code>plugins {
    id("java")
}

repositories {
    mavenCentral()
}

abstract class MyTask extends DefaultTask {

    @Input
    protected abstract ListProperty<Integer> getTaxonomyElementIds();

    @TaskAction
    void generate() {
        taxonomyElementIds.get().each {
            println it
        }
    }

}

tasks.register("myTask", MyTask) {
    taxonomyElementIds = [200, 201, 202, 203, 204, 205, 206, 207, 208]
}
</code>```

Output of the above:

```<code>> Task :myTask
200
201
202
203
204
205
206
207
208
</code>```

Body:
I am authoring a Gradle task that needs to work on an array of integers (dispatching a job for each value).

I don't understand how to properly use Property<T> to accept a Groovy array syntax.

The following does not work

Inside Task.java

/**
 * Element IDs to download
 */
@Input
protected abstract Property<Integer[]> getTaxonomyElementIds();

Inside build.gradle

taxonomyElementIds = [200, 201, 202, 203, 204, 205, 206, 207, 208]

Error

Cannot set the value of task ':downloadMasterDb' property 'taxonomyElementIds' of type [Ljava.lang.Integer; using an instance of type java.util.ArrayList.

The following workaround works, but I prefer a cleaner Groovy syntax

    taxonomyElementIds = [200, 201, 202, 203, 204, 205, 206, 207, 208].toArray(Integer[]::new)

I also tried to change the type of the property to List<Integer> (which essentially doesn't change my task code) but doesn't work.

How can I change my property in order to accept the Groovy array/list syntax?

Body:
Use ListProperty<Integer> instead:

plugins {
    id("java")
}

repositories {
    mavenCentral()
}

abstract class MyTask extends DefaultTask {

    @Input
    protected abstract ListProperty<Integer> getTaxonomyElementIds();

    @TaskAction
    void generate() {
        taxonomyElementIds.get().each {
            println it
        }
    }

}

tasks.register("myTask", MyTask) {
    taxonomyElementIds = [200, 201, 202, 203, 204, 205, 206, 207, 208]
}

Output of the above:

> Task :myTask
200
201
202
203
204
205
206
207
208


问:
Custom windows hint on buttons in Delphi
I would like to make a custom formatted Hint for some buttons in my application.
I use to make a tadvtoolpanel and display it on the onmousemove event, but it is not very clean.

Is there a button (from TMS component for example) which enables this directly ?
I just want the user to have a quick resume windows on the mousemove on it.

Hope i manage to explain clearly.

Thanks
Gwenael

答: Use TAdvOfficeHint from TMS and just drop it on your mainform. All the TMS buttons have an OfficeHint property that lets you specify the title, text and icon of the hint. If no office hint is specified, the normal Hint property is used.