问:
Gaussian Blur Glitching/Segmentation
I'm trying to implement a Gaussian Blur from scratch (using C++). In the code below I've hard-coded the Gaussian kernel I'm using. I only kept one dimension as I'm trying to use the optimization I've read about where you can do a horizontal convolution pass and a vertical one over that to make your blur more efficient. Unfortunately, I'm running into some issues. Here is my code:

```<code>float gKern[5] = {0.05448868, 0.24420134, 0.40261995, 0.24420134, 0.05448868};
int** gaussianBlur(int** image, int height, int width) {
    int **ret  = new int*[height];
    for(int i = 0; i < height; i++) {
        ret[i] = new int[width];
    }

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (i == 0) {
                ret[i][j] = (gKern[0] * image[2][j]) + (gKern[1] * image[1][j]) + (gKern[2] * image[0][j]) + (gKern[3] * image[1][j]) + (gKern[4] * image[2][j]);
            } else if (i == 1) {
                ret[i][j] = (gKern[0] * image[1][j]) + (gKern[1] * image[0][j]) + (gKern[2] * image[1][j]) + (gKern[3] * image[2][j]) + (gKern[4] * image[3][j]);
            } else if (i == (height - 2)) {
                ret[i][j] = (gKern[0] * image[i - 2][j]) + (gKern[1] * image[i - 1][j]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i + 1][j]) + (gKern[4] * image[i][j]);
            } else if (i == (height - 1)) {
                ret[i][j] = (gKern[0] * image[i - 2][j]) + (gKern[1] * image[i - 1][j]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i - 1][j]) + (gKern[4] * image[i - 2][j]);
            } else {
                ret[i][j] = (gKern[0] * image[i - 2][j]) + (gKern[1] * image[i - 1][j]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i + 1][j]) + (gKern[4] * image[i + 2][j]);
            }
        }
    }

    int** temp = image;
    image = ret;

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (j == 0) {
                ret[i][j] = (gKern[0] * image[i][2]) + (gKern[1] * image[i][1]) + (gKern[2] * image[i][0]) + (gKern[3] * image[i][1]) + (gKern[4] * image[i][2]);
            } else if (j == 1) {
                ret[i][j] = (gKern[0] * image[i][1]) + (gKern[1] * image[i][0]) + (gKern[2] * image[i][1]) + (gKern[3] * image[i][2]) + (gKern[4] * image[i][3]);
            } else if (j == (width - 2)) {
                ret[i][j] = (gKern[0] * image[i][j - 2]) + (gKern[1] * image[i][j - 1]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i][j + 1]) + (gKern[4] * image[i][j]);
            } else if (j == (width - 1)) {
                ret[i][j] = (gKern[0] * image[i][j - 2]) + (gKern[1] * image[i][j - 1]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i][j - 1]) + (gKern[4] * image[i][j - 2]);
            } else {
                ret[i][j] = (gKern[0] * image[i][j - 2]) + (gKern[1] * image[i][j - 1]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i][j + 1]) + (gKern[4] * image[i][j + 2]);
            }
        }
    }

    image = temp;

    return ret;
}
</code>```

The first pass (the first for block) seems to work fine as when I comment out the second block I do get a slightly blurred image. But when I use both I get a choppy "weird" image, as shown below (the first image is my grayscale input, the second is the choppy output):

答:
The problem is with the pointers you use.

The function starts with image as input and ret as the intermediate result of the first step.

The second step must use ret as input, and write either to the original input (overwrite the input image) or to a new image. Instead, you do:

```<code>int** temp = image;
image = ret;
// read from image and write to ret
image = temp;
return ret;
</code>```

That is, going into the second pass, both image and ret point to the same data, you then read and write to the same data. Next you do a pointer assignment that has no effect (image is never used after this) and return the intermediate buffer.

If you want to write to the input image, simply swap the image and ret pointers before the second pass:

```<code>std::swap(image, res);
</code>```

If you don’t want that, you’ll have to new another image to write into.

It is bad practice to use an array of arrays to store an image. If you look into the source code of any image processing library, you’ll see they allocate a single large memory block for the image, which stores all image rows concatenated. Knowing the width of the image, you know how to index: image[x + y*width].

This not only simplifies code (no loops to allocate a single image), but it also greatly speeds up code: there is no pointer lookup any more, and all data is close together to best use the cache.

This whole code can be simplified significantly by following the advice above: the two passes can be done with the same code. Write a function that filters one line of the image. It takes a pointer to the first pixel, a line length, and a step (which is 1 for horizontal lines, and width for vertical lines). This 1D function is then called in a loop over the lines in another function.  This second function is then called once to do the horizontal pass, and once to do the vertical pass. (See here for details.)

In this situation, it is easy to avoid intermediate images by using a buffer of the size of a single image line. Write into that buffer, then copy the whole line back into the input image after it is filtered. This means you have a single buffer of size max(width,height) rather than a buffer of size width*height.

The 1D filter function can also be simplified. That loop should not have any if statements, they will significantly slow down. Execution. Instead, special-case the first two and last two pixels, and loop only over the bulk of the pixels where you don’t have to worry about the image edge.

Title: Gaussian Blur Glitching/Segmentation

Body:
I'm trying to implement a Gaussian Blur from scratch (using C++). In the code below I've hard-coded the Gaussian kernel I'm using. I only kept one dimension as I'm trying to use the optimization I've read about where you can do a horizontal convolution pass and a vertical one over that to make your blur more efficient. Unfortunately, I'm running into some issues. Here is my code:

float gKern[5] = {0.05448868, 0.24420134, 0.40261995, 0.24420134, 0.05448868};
int** gaussianBlur(int** image, int height, int width) {
    int **ret  = new int*[height];
    for(int i = 0; i < height; i++) {
        ret[i] = new int[width];
    }

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (i == 0) {
                ret[i][j] = (gKern[0] * image[2][j]) + (gKern[1] * image[1][j]) + (gKern[2] * image[0][j]) + (gKern[3] * image[1][j]) + (gKern[4] * image[2][j]);
            } else if (i == 1) {
                ret[i][j] = (gKern[0] * image[1][j]) + (gKern[1] * image[0][j]) + (gKern[2] * image[1][j]) + (gKern[3] * image[2][j]) + (gKern[4] * image[3][j]);
            } else if (i == (height - 2)) {
                ret[i][j] = (gKern[0] * image[i - 2][j]) + (gKern[1] * image[i - 1][j]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i + 1][j]) + (gKern[4] * image[i][j]);
            } else if (i == (height - 1)) {
                ret[i][j] = (gKern[0] * image[i - 2][j]) + (gKern[1] * image[i - 1][j]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i - 1][j]) + (gKern[4] * image[i - 2][j]);
            } else {
                ret[i][j] = (gKern[0] * image[i - 2][j]) + (gKern[1] * image[i - 1][j]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i + 1][j]) + (gKern[4] * image[i + 2][j]);
            }
        }
    }

    int** temp = image;
    image = ret;

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (j == 0) {
                ret[i][j] = (gKern[0] * image[i][2]) + (gKern[1] * image[i][1]) + (gKern[2] * image[i][0]) + (gKern[3] * image[i][1]) + (gKern[4] * image[i][2]);
            } else if (j == 1) {
                ret[i][j] = (gKern[0] * image[i][1]) + (gKern[1] * image[i][0]) + (gKern[2] * image[i][1]) + (gKern[3] * image[i][2]) + (gKern[4] * image[i][3]);
            } else if (j == (width - 2)) {
                ret[i][j] = (gKern[0] * image[i][j - 2]) + (gKern[1] * image[i][j - 1]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i][j + 1]) + (gKern[4] * image[i][j]);
            } else if (j == (width - 1)) {
                ret[i][j] = (gKern[0] * image[i][j - 2]) + (gKern[1] * image[i][j - 1]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i][j - 1]) + (gKern[4] * image[i][j - 2]);
            } else {
                ret[i][j] = (gKern[0] * image[i][j - 2]) + (gKern[1] * image[i][j - 1]) + (gKern[2] * image[i][j]) + (gKern[3] * image[i][j + 1]) + (gKern[4] * image[i][j + 2]);
            }
        }
    }

    image = temp;

    return ret;
}

The first pass (the first for block) seems to work fine as when I comment out the second block I do get a slightly blurred image. But when I use both I get a choppy "weird" image, as shown below (the first image is my grayscale input, the second is the choppy output):

Body:
The problem is with the pointers you use.

The function starts with image as input and ret as the intermediate result of the first step.

The second step must use ret as input, and write either to the original input (overwrite the input image) or to a new image. Instead, you do:

int** temp = image;
image = ret;
// read from image and write to ret
image = temp;
return ret;

That is, going into the second pass, both image and ret point to the same data, you then read and write to the same data. Next you do a pointer assignment that has no effect (image is never used after this) and return the intermediate buffer.

If you want to write to the input image, simply swap the image and ret pointers before the second pass:

std::swap(image, res);

If you don’t want that, you’ll have to new another image to write into.

It is bad practice to use an array of arrays to store an image. If you look into the source code of any image processing library, you’ll see they allocate a single large memory block for the image, which stores all image rows concatenated. Knowing the width of the image, you know how to index: image[x + y*width].

This not only simplifies code (no loops to allocate a single image), but it also greatly speeds up code: there is no pointer lookup any more, and all data is close together to best use the cache.

This whole code can be simplified significantly by following the advice above: the two passes can be done with the same code. Write a function that filters one line of the image. It takes a pointer to the first pixel, a line length, and a step (which is 1 for horizontal lines, and width for vertical lines). This 1D function is then called in a loop over the lines in another function.  This second function is then called once to do the horizontal pass, and once to do the vertical pass. (See here for details.)

In this situation, it is easy to avoid intermediate images by using a buffer of the size of a single image line. Write into that buffer, then copy the whole line back into the input image after it is filtered. This means you have a single buffer of size max(width,height) rather than a buffer of size width*height.

The 1D filter function can also be simplified. That loop should not have any if statements, they will significantly slow down. Execution. Instead, special-case the first two and last two pixels, and loop only over the bulk of the pixels where you don’t have to worry about the image edge.


来源: stackexchange

问:
I'm trying to build a Neural Network in python
I'm trying to build a neural network and am following a tutorial.

What do those two lines mean?

```<code>    syn0 = 2*np.random.random((3,4)) -1
    syn1 = 2*np.random.random((4,1)) -1
</code>```

Specifically, Those values (3,4 | 4,1)

I just don't get it...
I think I know what the first synapse's values mean, but not the 2nd one...

答:
np.random.random creates an array of random values between 0 and 1 - the parameter it takes is the desired shape of the array, which is what (3,4) and (4,1) are in your example.

Simple random weight initialisation is sufficient to train your neural network, but initialising them with a mean of 0 speeds up training, which is what 2*np.random.random((3,4)) -1 does:

```<code>np.random.random((3,4)) // array with values in range [0, 1) and mean of 0.5
</code>```

```<code>2 * np.random.random((3,4)) // array with values in range [0, 2) and mean of 1
</code>```

```<code>2 * np.random.random((3,4)) - 1 // array with values in range [-1, 1) and mean of 0
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I'm trying to build a neural network and am following a tutorial.

What do those two lines mean?

    syn0 = 2*np.random.random((3,4)) -1
    syn1 = 2*np.random.random((4,1)) -1

Specifically, Those values (3,4 | 4,1)

I just don't get it...
I think I know what the first synapse's values mean, but not the 2nd one...

Body:
np.random.random creates an array of random values between 0 and 1 - the parameter it takes is the desired shape of the array, which is what (3,4) and (4,1) are in your example.

Simple random weight initialisation is sufficient to train your neural network, but initialising them with a mean of 0 speeds up training, which is what 2*np.random.random((3,4)) -1 does:

np.random.random((3,4)) // array with values in range [0, 1) and mean of 0.5

2 * np.random.random((3,4)) // array with values in range [0, 2) and mean of 1

2 * np.random.random((3,4)) - 1 // array with values in range [-1, 1) and mean of 0


问:
Flutter old app upgrading to use it today
I've found this code for flutter home automation application.
but it was wrote in an older version of flutter, which i tried many times to start it but i couldn't make it,
last best thing that i have ever came to was that i could could mergrated the application to Android X and the application was starting but when i try to sign up a new mail inside it it says,
Error fetching data.
It dosen't use Firebase to store email it just uses a SQL package.
if any one can help to git the code and upgrade it for me to use it i will be very thankful :D.
Here is the link,
https://github.com/mlinarevicMateo/home-automation-flutter</p>

答: The app is dependant on a webservice to run. Check lib/utils/network_util.dart. So you won't be able to get it run unless you write a new backend.</p>


问:
Multithread python count week days several dataset
Hi everyone I'm trying to use multithread in python

```<code>def get_weekday_data(t_index,file_name):
    """
   get dates (columna Date/Time).
    """
    #
    logging.info("[T{}]\tStarted with url {}".format(t_index, file_name))
    data=pd.read_csv(path+file_name)
    data['Date/Time']=pd.to_datetime(data['Date/Time'],
    infer_datetime_format=True)
    week_day_data=pd.DataFrame(data['Date/Time'].apply(lambda x:
    x.strftime('%A')))
    week_day_data_2=pd.DataFrame(week_day_data['Date/Time'].value_counts())
    week_day_data_2.reset_index(inplace=True)
    week_day_data_2.sort_values('index',inplace=True)
    print(week_day_data_2)

  logging.info("[T{}]\tEnd".format(t_index))
  return week_day_data_2
</code>```

I have this function and I wnat to apply the function to 6 data sets using multithread
I try the following but I dont know how to concat de results of each thread. I m learning how to use multithread and multiprocesses.
But I dont know how to obtain the output of each thread

```<code>%%time
threads = []

#  6 threads execute get_weekday_data

for i, num in enumerate(files_to_read):
    print('now')
    thread = Thread(target= get_weekday_data, args=(i,num))
    threads.append(thread)
    thread.start()

# Espera a que los 6 threads finalicen
for i, thread in enumerate(threads):
    logging.info("[M]\tWaiting to join thread {}".format(i))
    thread.join()
    logging.info("[M]\tThread {} joined!".format(i))

logging.info("[M]\tDONE!")
</code>```

答:
I really recommend using joblib, it's much easier to use. You can install it using:

```<code>pip install joblib
</code>```

And you can use it simply like so:

```<code>from joblib import Parallel, delayed

result = Parallel(n_jobs=4, backend="threading")(
            delayed(get_weekday_data)(idx, file_name) \
                for idx, file_name in enumerate(files_to_read))
</code>```

result is a list of all data sorted based on insertion. You can even control how many threads running together from the n_jobs argument

Body:
Hi everyone I'm trying to use multithread in python

def get_weekday_data(t_index,file_name):
    """
   get dates (columna Date/Time).
    """
    #
    logging.info("[T{}]\tStarted with url {}".format(t_index, file_name))
    data=pd.read_csv(path+file_name)
    data['Date/Time']=pd.to_datetime(data['Date/Time'],
    infer_datetime_format=True)
    week_day_data=pd.DataFrame(data['Date/Time'].apply(lambda x:
    x.strftime('%A')))
    week_day_data_2=pd.DataFrame(week_day_data['Date/Time'].value_counts())
    week_day_data_2.reset_index(inplace=True)
    week_day_data_2.sort_values('index',inplace=True)
    print(week_day_data_2)

  logging.info("[T{}]\tEnd".format(t_index))
  return week_day_data_2

I have this function and I wnat to apply the function to 6 data sets using multithread
I try the following but I dont know how to concat de results of each thread. I m learning how to use multithread and multiprocesses.
But I dont know how to obtain the output of each thread

%%time
threads = []

#  6 threads execute get_weekday_data

for i, num in enumerate(files_to_read):
    print('now')
    thread = Thread(target= get_weekday_data, args=(i,num))
    threads.append(thread)
    thread.start()

# Espera a que los 6 threads finalicen
for i, thread in enumerate(threads):
    logging.info("[M]\tWaiting to join thread {}".format(i))
    thread.join()
    logging.info("[M]\tThread {} joined!".format(i))

logging.info("[M]\tDONE!")

Body:
I really recommend using joblib, it's much easier to use. You can install it using:

pip install joblib

And you can use it simply like so:

from joblib import Parallel, delayed

result = Parallel(n_jobs=4, backend="threading")(
            delayed(get_weekday_data)(idx, file_name) \
                for idx, file_name in enumerate(files_to_read))

result is a list of all data sorted based on insertion. You can even control how many threads running together from the n_jobs argument


问:
how to derive values from foreign key field? django
model.py

```<code>class Authors(models.Model):
  name = models.CharField(max_length=50)

class Books(models.Model):
  date_pub = models.DateField()
  name = models.CharField(max_length=50)
  author = models.ForeignKey('Authors')
</code>```

I need to derive the names of authors from a foreign key, but the Books.objects.all().values_list('author', 'name') command infers the id from the Authors model. how to derive author names from the Books model

答:
You can use:

```<code>Books.objects.all().values_list('author__name')
</code>```

It is mentioned clearly in Django documentation: https://docs.djangoproject.com/en/3.0/ref/models/querysets/#values-list

Body:
model.py

class Authors(models.Model):
  name = models.CharField(max_length=50)

class Books(models.Model):
  date_pub = models.DateField()
  name = models.CharField(max_length=50)
  author = models.ForeignKey('Authors')

I need to derive the names of authors from a foreign key, but the Books.objects.all().values_list('author', 'name') command infers the id from the Authors model. how to derive author names from the Books model

Body:
You can use:

Books.objects.all().values_list('author__name')

It is mentioned clearly in Django documentation: https://docs.djangoproject.com/en/3.0/ref/models/querysets/#values-list


问:
How to find fast the number of files in directory?
I have thousands log files in a few directories in my system. Mostly I need to know just number of these files in directory. I fear that this attitude:

```<code>$files = count(scandir("logs")) - 2;
</code>```

Is not very effective (memory, filesystem). I am considering some global counter (incremented after file is added), unless there is some other effective way?

答:
```<code>$files = count(glob("logs/*",GLOB_NOSORT));
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I have thousands log files in a few directories in my system. Mostly I need to know just number of these files in directory. I fear that this attitude:

$files = count(scandir("logs")) - 2;

Is not very effective (memory, filesystem). I am considering some global counter (incremented after file is added), unless there is some other effective way?


问:
Azure Logic App won't listen for updates on certain SQL Server Tables
I'm having issues with a logic app that is listening every 3 seconds for a table update on an Azure SQL database. It seems like the issue is that the logic app only successfully listens to tables that appear in this autocomplete drop down here, there are only 3. The rest must be entered as a "custom value".

It is unsuccessful for any other table, other than the three in the dropdown, there are about 30 or so more tables that don't appear here. When entering tables as a custom value, the logic app has knowledge of column names, etc. as dynamic content, so it's not a typo or anything like that

As an FYI this is a very large, enterprise level database with millions of records, so I'm not sure if the logic app is having trouble with schema introspection on such a large database, but I'm at a loss at this point what the issue is, or how to get more tables to appear, or if my intuition is incorrect and there is some other issue causing this.

Thanks for any advice and help.

答:
Its an old question, but if this helps somebody.

This SQL trigger to get invoked the table should have an identity column (a column like serial number or something which increases or decreases in a progression). Only those tables with identity keys will be listed in the drop down.


问:
Buildozer APK crash "Consumer closed input channel or an error occurred. events=0x9"
I've build my first apk using buildozer without error, but now it crash on android.
I found in logs "Consumer closed input channel or an error occurred.  events=0x9" but I don't know why I get this error:
Here are the logslogs and here is buildozer.specspec

Thanks for your help!

答:
I had the error you mentioned, but it turned out I had a different problem. That "Consumer closed..." message is just a signal that something else is wrong.

Now, as John Anderson already stated, the actual problem you have is that Python never loads when your app starts. On GitHub, you said that you have Python 3.7 installed, so not sure why, it doesn't load in your case.

I have the same errors, but have Python 3.8 installed, so it loads and everything works as expected.


问:
C++ concept check vs inheritance
What is the relationship between using virtual functions and C++ inheritance mechanisms versus using templates and something like boost concepts?

It seems like there is quite an overlap of what is possible. Namely, it appears to be possible to achieve polymorphic behavior with either approach. So, when does it make sense to favor one over the other?

The reason why I bring this up is because I have a templated container, where the containers themselves have a hierarchical relationship. I would like to write algorithms that use these containers without caring about which specific container it is. Also, some algorithms would benefit from knowing that the template type satisfied certain concepts (Comparable, for example).

So, on one hand, I want containers to behave polymorphicly. On the other, I still have to use concepts if I want to correctly implement some algorithms. What is a junior developer to do?

答:
As a simple example of the difference between compile-time and run-time polymorphism consider the following code:

```<code>template<typename tType>
struct compileTimePolymorphism
{ };

// compile time polymorphism,
// you can describe a behavior on some object type
// through the template, but you cannot interchange
// the templates
compileTimePolymorphism<int> l_intTemplate;
compileTimePolymorphism<float> l_floatTemplate;
compileTimePolymorphism *l_templatePointer; // ???? impossible

struct A {};
struct B : public A{};
struct C : public A{};

// runtime polymorphism
// you can interchange objects of different type
// by treating them like the parent
B l_B;
C l_C:
A *l_A = &l_B;
l_A = &l_C;
</code>```

Compile-time polymorphism is a good solution when the behavior of one object depends on some other object. Run-time polymorphism is necessary where the behavior of an object needs to be changed.

The two can be combined by defining a template which is polymorphic:

```<code>template<typename tType>
struct myContainer : public tType
{};
</code>```

The question then is where the behavior of your container needs to change (runtime polymorphism), and where the behavior depends on the objects it contains (compile time polymorphism).

OwnerDisplayName: Voltaire

ContentLicense: CC BY-SA 2.5

Title: C++ concept check vs inheritance

Body:
As a simple example of the difference between compile-time and run-time polymorphism consider the following code:

template<typename tType>
struct compileTimePolymorphism
{ };

// compile time polymorphism,
// you can describe a behavior on some object type
// through the template, but you cannot interchange
// the templates
compileTimePolymorphism<int> l_intTemplate;
compileTimePolymorphism<float> l_floatTemplate;
compileTimePolymorphism *l_templatePointer; // ???? impossible

struct A {};
struct B : public A{};
struct C : public A{};

// runtime polymorphism
// you can interchange objects of different type
// by treating them like the parent
B l_B;
C l_C:
A *l_A = &l_B;
l_A = &l_C;

Compile-time polymorphism is a good solution when the behavior of one object depends on some other object. Run-time polymorphism is necessary where the behavior of an object needs to be changed.

The two can be combined by defining a template which is polymorphic:

template<typename tType>
struct myContainer : public tType
{};

The question then is where the behavior of your container needs to change (runtime polymorphism), and where the behavior depends on the objects it contains (compile time polymorphism).

OwnerDisplayName: SmokingRope


问:
Nested if or switch case statement
From the point of view of optimizing the code run time, is there a thumb rule for where to use "nested if" statement and when to use "switch case" statements ?</p>

答:
I doubt you will ever find a real-life application where the difference between a nested if and a switch case is even worth measuring.  Disk access, web access, etc. take many many orders of magnitude more time.

Choose what is easiest to read and debug.

Also see What is the difference between IF-ELSE and SWITCH? (possible duplicate) as well as Advantage of switch over if-else statement.  Interestingly, a proponent of switch writes

In the worst case the compiler will
  generate the same code as a if-else
  chain, so you don't lose anything. If
  in doubt put the most common cases
  first into the switch statement.

In the best case the optimizer may
  find a better way to generate the
  code. Common things a compiler does is
  to build a binary decission tree
  (saves compares and jumps in the
  average case) or simply build a
  jump-table (works without compares at
  all).

Title: Nested if or switch case statement


问:
How to return Azure Eventhub primary and secondary connection keys as ARM template output?
I have prepared an ARM template for deploying an Azure Eventhub instance and wonder how to access the both connection keys for returning them as output?

I would like to return a string in the form:

Endpoint=sb://my-eventhub.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ojZMQcJD7uYifxJyGeXG6tNDdZyaC1/h5tmX6ODVfmY=

Here is my current template:

```<code>{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "clusterName": {
            "type": "string",
            "defaultValue": "eventhub",
            "metadata": {
                "description": "Name for the Event Hub cluster."
            }
        },
        "namespaceName": {
            "type": "string",
            "defaultValue": "namespace",
            "metadata": {
                "description": "Name for the Namespace to be created in cluster."
            }
        }
    },
    "variables": {
        "clusterName": "[concat(resourceGroup().name, '-', parameters('clusterName'))]",
        "namespaceName": "[concat(resourceGroup().name, '-', parameters('namespaceName'))]"
    },
    "outputs": {
        "MyClusterName": {
            "type": "string",
            "value": "[variables('clusterName')]"
        },
        "PrimaryConnectionString": {
            "type": "string",
            "value": "WHAT TO USE HERE PLEASE?"
        },
        "SecondaryConnectionString": {
            "type": "string",
            "value": "WHAT TO USE HERE PLEASE?"
        }
    },
    "resources": [
        {
            "type": "Microsoft.EventHub/clusters",
            "apiVersion": "2018-01-01-preview",
            "name": "[variables('clusterName')]",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "Dedicated",
                "capacity": 1
            }
        },
        {
            "type": "Microsoft.EventHub/namespaces",
            "apiVersion": "2018-01-01-preview",
            "name": "[variables('namespaceName')]",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[resourceId('Microsoft.EventHub/clusters', variables('clusterName'))]"
            ],
            "sku": {
                "name": "Standard",
                "tier": "Standard",
                "capacity": 1
            },
            "properties": {
                "isAutoInflateEnabled": false,
                "maximumThroughputUnits": 0,
                "clusterArmId": "[resourceId('Microsoft.EventHub/clusters', variables('clusterName'))]"
            }
        }
    ]
}
</code>```

I have tried the following:

```<code>"value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/AuthorizationRules'), variables('namespaceName'), 'RootManageSharedAccessKey'),'2018-01-01-preview').primaryConnectionString]"
</code>```

but get deployment error:

[error]ParentResourceNotFound: Can not perform requested operation on nested resource. Parent resource 'my-rg-namespace' not found.

UPDATE:

The following has worked for me as suggested by Jesse (thank you!):

```<code>"variables": {
    "clusterName": "[concat(resourceGroup().name, '-', parameters('clusterName'))]",
    "namespaceName": "[concat(resourceGroup().name, '-', parameters('namespaceName'))]",
    "defaultSASKeyName": "RootManageSharedAccessKey",
    "authRuleResourceId": "[resourceId('Microsoft.EventHub/namespaces/authorizationRules', variables('namespaceName'), variables('defaultSASKeyName'))]"
},
"outputs": {
    "MyClusterName": {
        "type": "string",
        "value": "[variables('clusterName')]"
    },
    "PrimaryConnectionString": {
        "type": "string",
        "value": "[listkeys(variables('authRuleResourceId'), '2015-08-01').primaryConnectionString]"
    },
    "SecondaryConnectionString": {
        "type": "string",
        "value": "[listkeys(variables('authRuleResourceId'), '2015-08-01').secondaryConnectionString]"
    }
},
</code>```

UPDATE 2:

Also, Jesse has noticed that my ARM template is wrong in 2 ways, because it does not create an Event Hub, but a cluster and it is outside my namespace and provided this valuable comment:

The Event Hubs cluster is basically a way of reserving dedicated compute. It's not something that most scenarios need and it is... not cheap. Think of something on the scale of Xbox Live where you're seeing nearly 5 millions of events per second and which have higher performance needs. If you're not looking at that kind of scale or that sensitivity around timing, you probably want to rethink the need for a dedicated cluster.

Normally, you'd just provision an Event Hubs namespace which will use shared infrastructure with certain guarantees to minimize noisy neighbors and similar. This is adequate for the majority of scenarios, even those with high throughput needs. If you're not sure, this is probably the place that you want to start and then upgrade to a dedicated cluster if your needs justify the cost.

An Event Hubs namespace is the container for a set of Event Hub instances grouped together by a unique endpoint. Each Event Hub is made of a set of partitions. When you're publishing or consuming events, the partitions of an Event Hub are where the actual data is. When you're working with one of the SDKs, you'll start by telling it about the endpoint of your namespace and the Event Hub that you're interested in. You'll need a general awareness of partitions, but most of the "Getting Started" scenarios handle that detail for you, as do a fair portion of the real-world ones.... but, the concept is an important one.

答:
It looks like you may be using an incorrect resource id, pulling from Microsoft.ServiceBus rather than Microsoft.EventHub where the failure is because there is no Service Bus namespace with the correct name.

You may want to try using a form similar to the following to identify your resource:

```<code>"variables": {
    "location": "[resourceGroup().location]",
    "apiVersion": "2015-08-01",
    "defaultSASKeyName": "RootManageSharedAccessKey",
    "authRuleResourceId": "[resourceId('Microsoft.EventHub/namespaces/authorizationRules', parameters('namespaceName'), variables('defaultSASKeyName'))]"
},
</code>```

Which should allow it to be returned using listkeys as you you detailed above:

```<code>"outputs": {
    "NamespaceConnectionString": {
        "type": "string",
        "value": "[listkeys(variables('authRuleResourceId'), variables('apiVersion')).primaryConnectionString]"
    }
}
</code>```

A full example for a simple deployment can be found in the Event Hubs sample template.

Body:
I have prepared an ARM template for deploying an Azure Eventhub instance and wonder how to access the both connection keys for returning them as output?

I would like to return a string in the form:

Endpoint=sb://my-eventhub.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ojZMQcJD7uYifxJyGeXG6tNDdZyaC1/h5tmX6ODVfmY=

Here is my current template:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "clusterName": {
            "type": "string",
            "defaultValue": "eventhub",
            "metadata": {
                "description": "Name for the Event Hub cluster."
            }
        },
        "namespaceName": {
            "type": "string",
            "defaultValue": "namespace",
            "metadata": {
                "description": "Name for the Namespace to be created in cluster."
            }
        }
    },
    "variables": {
        "clusterName": "[concat(resourceGroup().name, '-', parameters('clusterName'))]",
        "namespaceName": "[concat(resourceGroup().name, '-', parameters('namespaceName'))]"
    },
    "outputs": {
        "MyClusterName": {
            "type": "string",
            "value": "[variables('clusterName')]"
        },
        "PrimaryConnectionString": {
            "type": "string",
            "value": "WHAT TO USE HERE PLEASE?"
        },
        "SecondaryConnectionString": {
            "type": "string",
            "value": "WHAT TO USE HERE PLEASE?"
        }
    },
    "resources": [
        {
            "type": "Microsoft.EventHub/clusters",
            "apiVersion": "2018-01-01-preview",
            "name": "[variables('clusterName')]",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "Dedicated",
                "capacity": 1
            }
        },
        {
            "type": "Microsoft.EventHub/namespaces",
            "apiVersion": "2018-01-01-preview",
            "name": "[variables('namespaceName')]",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[resourceId('Microsoft.EventHub/clusters', variables('clusterName'))]"
            ],
            "sku": {
                "name": "Standard",
                "tier": "Standard",
                "capacity": 1
            },
            "properties": {
                "isAutoInflateEnabled": false,
                "maximumThroughputUnits": 0,
                "clusterArmId": "[resourceId('Microsoft.EventHub/clusters', variables('clusterName'))]"
            }
        }
    ]
}

I have tried the following:

"value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/AuthorizationRules'), variables('namespaceName'), 'RootManageSharedAccessKey'),'2018-01-01-preview').primaryConnectionString]"

but get deployment error:

[error]ParentResourceNotFound: Can not perform requested operation on nested resource. Parent resource 'my-rg-namespace' not found.

UPDATE:

The following has worked for me as suggested by Jesse (thank you!):

"variables": {
    "clusterName": "[concat(resourceGroup().name, '-', parameters('clusterName'))]",
    "namespaceName": "[concat(resourceGroup().name, '-', parameters('namespaceName'))]",
    "defaultSASKeyName": "RootManageSharedAccessKey",
    "authRuleResourceId": "[resourceId('Microsoft.EventHub/namespaces/authorizationRules', variables('namespaceName'), variables('defaultSASKeyName'))]"
},
"outputs": {
    "MyClusterName": {
        "type": "string",
        "value": "[variables('clusterName')]"
    },
    "PrimaryConnectionString": {
        "type": "string",
        "value": "[listkeys(variables('authRuleResourceId'), '2015-08-01').primaryConnectionString]"
    },
    "SecondaryConnectionString": {
        "type": "string",
        "value": "[listkeys(variables('authRuleResourceId'), '2015-08-01').secondaryConnectionString]"
    }
},

UPDATE 2:

Also, Jesse has noticed that my ARM template is wrong in 2 ways, because it does not create an Event Hub, but a cluster and it is outside my namespace and provided this valuable comment:

The Event Hubs cluster is basically a way of reserving dedicated compute. It's not something that most scenarios need and it is... not cheap. Think of something on the scale of Xbox Live where you're seeing nearly 5 millions of events per second and which have higher performance needs. If you're not looking at that kind of scale or that sensitivity around timing, you probably want to rethink the need for a dedicated cluster.

Normally, you'd just provision an Event Hubs namespace which will use shared infrastructure with certain guarantees to minimize noisy neighbors and similar. This is adequate for the majority of scenarios, even those with high throughput needs. If you're not sure, this is probably the place that you want to start and then upgrade to a dedicated cluster if your needs justify the cost.

An Event Hubs namespace is the container for a set of Event Hub instances grouped together by a unique endpoint. Each Event Hub is made of a set of partitions. When you're publishing or consuming events, the partitions of an Event Hub are where the actual data is. When you're working with one of the SDKs, you'll start by telling it about the endpoint of your namespace and the Event Hub that you're interested in. You'll need a general awareness of partitions, but most of the "Getting Started" scenarios handle that detail for you, as do a fair portion of the real-world ones.... but, the concept is an important one.

Body:
It looks like you may be using an incorrect resource id, pulling from Microsoft.ServiceBus rather than Microsoft.EventHub where the failure is because there is no Service Bus namespace with the correct name.

You may want to try using a form similar to the following to identify your resource:

"variables": {
    "location": "[resourceGroup().location]",
    "apiVersion": "2015-08-01",
    "defaultSASKeyName": "RootManageSharedAccessKey",
    "authRuleResourceId": "[resourceId('Microsoft.EventHub/namespaces/authorizationRules', parameters('namespaceName'), variables('defaultSASKeyName'))]"
},

Which should allow it to be returned using listkeys as you you detailed above:

"outputs": {
    "NamespaceConnectionString": {
        "type": "string",
        "value": "[listkeys(variables('authRuleResourceId'), variables('apiVersion')).primaryConnectionString]"
    }
}

A full example for a simple deployment can be found in the Event Hubs sample template.


问:
Getting two buttons next to each other
I am having a problem with my design.

I have two buttons on my product page. However because one is in a form, they are on top of each other(see image ).

I want to get the two buttons next to each other. Can someone help me out, please ? Below I added my HTML and CSS code.

Thanks in advance!

HTML:

```<code><!DOCTYPE HTML>
<div class="container text-center">
            <div class="sale">
                <h1>On Sale</h1>
                <p class="text-secondary">
                    We have new sales promotions online for a short period of time every day. We offer products from
                    popular labels and luxury brands in the lifestyle segment up to 75% cheaper than the
                    recommended retail price.
                </p>
            </div>
</div>

        <section class="on-sale">
            <div class="container">
                <div class="row">

                <?php
                    for($i = 0; $i < $countProductItems && $i < 4; $i++){
                        ?>
                        <div class="col-md-3">
                        <div class="product-top">
                        <a href="products/product-page.php?product=<?php echo ($saleProducts[$i]['strProductNaam']); ?>" onclick="POST"><img src="<?php echo ($saleProducts[$i]['imgProduct1']);?>" class="img-fluid" alt="Product1"/></a>
                            <div class="overlay-right text-center">
                                <button type="button" class="btn btn-secondary" title="Quick Shop">
                                <a href="products/product-page.php?product=<?php echo ($saleProducts[$i]['strProductNaam']); ?>" onclick="POST"><i class="fa fa-eye text-white"></i></a>
                                </button>
                                <form action="includes/shopping-cart.inc.php?action=add&id=<?php echo ($saleProducts[$i]['ID'])?>" method="post">
                                    <input class="text-center" type="hidden" name="quantity" value="1"/>
                                    <input type="hidden" name="index-page" value="index">
                                    <input type="hidden" name="product-name" value="<?php echo ($saleProducts[$i]['strProductNaam']); ?>">
                                    <input type="hidden" name="product-price" value="<?php echo ($saleProducts[$i]['strSalePrijs']); ?>">
                                    <button type="submit" class="btn btn-secondary " title="Add to cart" name="add_to_cart"><i class="fa fa-shopping-cart"></i></button>
                                </form>
                            </div>
                        </div>
                        <div class="product-bottom text-center">
                        <p>
                                <?php rating_star(($saleProducts[$i]['RatingStar'])) ?>
                            </p>
                            <p><h3><?php echo ($saleProducts[$i]['strProductNaam']); ?></h3></p>
                            <?php sale_product(($saleProducts[$i]['intPrijs']), ($saleProducts[$i]['strSalePrijs']))?>
                        </div>
                    </div>
                    <?php
                    }
                ?>

                </div>
            </div>
        </section>
</html>
</code>```

CSS:

```<code>.container .sale{
    padding: 3rem 0;
    }

    .container .sale{
        font-family: var(--gugi);
    }

    .container .sale{
        padding: 0.5% 25%;
        font-size: 0.9em;
    }

.on-sale{
    margin: 50px 0;
}

.on-sale img{
    width: 100%;
    padding: 20px;
    transition: 1s;
    cursor: pointer;
}

.on-sale img:hover{
    transform: scale(1.1);
}

.product-top{
    width:100%;
    display: inline-block;

}

.product-bottom .fa{
    color: orange;
    font-size: 10px;
}

.product-bottom h3{
    font-size: 20px;
    font-weight: bold;
}

.product-bottom h5{
    font-size: 15px;
    padding-bottom: 10px;
}

.make_red {
        color: red;
}
</code>```

// codefilling text[error] - nfnjsnfjsfnbjkdsnfdsfndjsfbjabsdjhbgdgabadsg

答:
try use display:flex</p>

```<code>.overlay-right text-center{
    display: flex;
    justify-content: space-between;
    width: 200px;
    margin: 0 auto;
    align-items: center;

}
</code>```

Title: Getting two buttons next to each other

Body:
I am having a problem with my design.

I have two buttons on my product page. However because one is in a form, they are on top of each other(see image ).

I want to get the two buttons next to each other. Can someone help me out, please ? Below I added my HTML and CSS code.

Thanks in advance!

HTML:

<!DOCTYPE HTML>

On Sale

                    We have new sales promotions online for a short period of time every day. We offer products from
                    popular labels and luxury brands in the lifestyle segment up to 75% cheaper than the
                    recommended retail price.

                <?php
                    for($i = 0; $i < $countProductItems && $i < 4; $i++){
                        ?>

                        <a href="products/product-page.php?product=<?php echo ($saleProducts[$i]['strProductNaam']); ?>" onclick="POST"><img src="<?php echo ($saleProducts[$i]['imgProduct1']);?>" class="img-fluid" alt="Product1"/>

                                <button type="button" class="btn btn-secondary" title="Quick Shop">
                                <a href="products/product-page.php?product=<?php echo ($saleProducts[$i]['strProductNaam']); ?>" onclick="POST">
                                </button>
                                <form action="includes/shopping-cart.inc.php?action=add&id=<?php echo ($saleProducts[$i]['ID'])?>" method="post">
                                    <input class="text-center" type="hidden" name="quantity" value="1"/>
                                    <input type="hidden" name="index-page" value="index">
                                    <input type="hidden" name="product-name" value="<?php echo ($saleProducts[$i]['strProductNaam']); ?>">
                                    <input type="hidden" name="product-price" value="<?php echo ($saleProducts[$i]['strSalePrijs']); ?>">
                                    <button type="submit" class="btn btn-secondary " title="Add to cart" name="add_to_cart"></button>
                                </form>

                                <?php rating_star(($saleProducts[$i]['RatingStar'])) ?>

<?php echo ($saleProducts[$i]['strProductNaam']); ?>

                            <?php sale_product(($saleProducts[$i]['intPrijs']), ($saleProducts[$i]['strSalePrijs']))?>

                    <?php
                    }
                ?>

CSS:

.container .sale{
    padding: 3rem 0;
    }

    .container .sale{
        font-family: var(--gugi);
    }

    .container .sale{
        padding: 0.5% 25%;
        font-size: 0.9em;
    }

.on-sale{
    margin: 50px 0;
}

.on-sale img{
    width: 100%;
    padding: 20px;
    transition: 1s;
    cursor: pointer;
}

.on-sale img:hover{
    transform: scale(1.1);
}

.product-top{
    width:100%;
    display: inline-block;

}

.product-bottom .fa{
    color: orange;
    font-size: 10px;
}

.product-bottom h3{
    font-size: 20px;
    font-weight: bold;
}

.product-bottom h5{
    font-size: 15px;
    padding-bottom: 10px;
}

.make_red {
        color: red;
}

// codefilling text[error] - nfnjsnfjsfnbjkdsnfdsfndjsfbjabsdjhbgdgabadsg

Body:
try use display:flex

.overlay-right text-center{
    display: flex;
    justify-content: space-between;
    width: 200px;
    margin: 0 auto;
    align-items: center;

}


问:
I installed tensorflow 2.0 but jupyter doesn't see it?
I seem to have a problem with installing tensorflow 2.0. After many attempts finally command "conda list" shows tensorflow 2.0 package successfully installed but when I try to import it in jupyter I get the error:

```<code>ImportError: Traceback (most recent call last):
  File "C:\Users\giuse\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58,
in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\giuse\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py",
line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\giuse\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py",
line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\giuse\anaconda3\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\giuse\anaconda3\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.
</code>```

Can anybody help please?

答:
If you have still below error:

ImportError: DLL load failed: The specified module could not be found.

Try to install the proper version of:

visual c++ redistributable for visual studio 2019

Or try virtual environment:

Creation of the environment

```<code>conda create -n test_env python=3.x anaconda
</code>```

Activation

```<code>conda activate test_env
</code>```

Deactivation

```<code>conda deactivate
</code>```

Getting rid of it

```<code>conda remove -n test_env --all
</code>```

Body:
I seem to have a problem with installing tensorflow 2.0. After many attempts finally command "conda list" shows tensorflow 2.0 package successfully installed but when I try to import it in jupyter I get the error:

ImportError: Traceback (most recent call last):
  File "C:\Users\giuse\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58,
in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\giuse\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py",
line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\giuse\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py",
line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\giuse\anaconda3\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\giuse\anaconda3\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.

Can anybody help please?

Body:
If you have still below error:

ImportError: DLL load failed: The specified module could not be found.

Try to install the proper version of:

visual c++ redistributable for visual studio 2019

Or try virtual environment:

Creation of the environment

conda create -n test_env python=3.x anaconda

Activation

conda activate test_env

Deactivation

conda deactivate

Getting rid of it

conda remove -n test_env --all


问:
Web scraper crawler using Breadth First Search in Python
I want to create web crawler for a wikipedia page( all the links within the page gets opened and saved too) which needs to be implemented in Breadth First Search way. I have been looking at a lot sources and stackoverflow codes/problems but unable to implement it.
I tried the following code :

```<code>import requests
from parsel import Selector

import time
start = time.time()

### Crawling to the website fetch links and images -> store images -> crawl more to the fetched links and scrape more images
all_images  = {} # website links as "keys" and images link as "values"
# GET request to recurship site
response = requests.get('https://en.wikipedia.org/wiki/Plant')
selector = Selector(response.text)
href_links = selector.xpath('//a/@href').getall()
image_links = selector.xpath('//img/@src').getall()

for link in href_links:
    try:
        response = requests.get(link)
        if response.status_code == 200:
            image_links = selector.xpath('//img/@src').getall()
            all_images[link] = image_links
    except Exception as exp:
        print('Error navigating to link : ', link)

print(all_images)
end = time.time()
print("Time taken in seconds : ", (end-start))
</code>```

but this throws an error saying "Error Navigating to link". How do I go about it ? I am a total newbie in this field.

答:
Your href_links will be relative path for wiki links.

You must append the baseUrl of wikipedia.

```<code>base_url = 'https://en.wikipedia.org/'

href_links = [base_url + link for link in selector.xpath('//a/@href').getall()]
</code>```

Note that this will work for wiki links if you have external links in href use something like this:

```<code>href_links = []
for link in selector.xpath('//a/@href').getall():
    if not link.startswith('http'):
        href_links.append(base_url + link)
    else:
        href_links.append(link)
</code>```

Body:
I want to create web crawler for a wikipedia page( all the links within the page gets opened and saved too) which needs to be implemented in Breadth First Search way. I have been looking at a lot sources and stackoverflow codes/problems but unable to implement it.
I tried the following code :

import requests
from parsel import Selector

import time
start = time.time()

### Crawling to the website fetch links and images -> store images -> crawl more to the fetched links and scrape more images
all_images  = {} # website links as "keys" and images link as "values"
# GET request to recurship site
response = requests.get('https://en.wikipedia.org/wiki/Plant')
selector = Selector(response.text)
href_links = selector.xpath('//a/@href').getall()
image_links = selector.xpath('//img/@src').getall()

for link in href_links:
    try:
        response = requests.get(link)
        if response.status_code == 200:
            image_links = selector.xpath('//img/@src').getall()
            all_images[link] = image_links
    except Exception as exp:
        print('Error navigating to link : ', link)

print(all_images)
end = time.time()
print("Time taken in seconds : ", (end-start))

but this throws an error saying "Error Navigating to link". How do I go about it ? I am a total newbie in this field.

Body:
Your href_links will be relative path for wiki links.

You must append the baseUrl of wikipedia.

base_url = 'https://en.wikipedia.org/'

href_links = [base_url + link for link in selector.xpath('//a/@href').getall()]

Note that this will work for wiki links if you have external links in href use something like this:

href_links = []
for link in selector.xpath('//a/@href').getall():
    if not link.startswith('http'):
        href_links.append(base_url + link)
    else:
        href_links.append(link)


问:
get array values in python
I have the values arr1  as 25,26 and arr2 values as A,B

Its always that the number of values in arr1 and arr2 are equal

My question is that

```<code>          for i in arr1.split(","):
                print i //prints 25 and 26
</code>```

is it not possible to get the values of arr2 in the same loop or should  another loop be written only for this purpose.Basically the idea is that map the values of arr1 and arr2

答:
You can use zip() function:

```<code>for zipped in zip(arr1.split(",") , arr2.split(",")):
    someDictionary[zipped[0]] = zipped[1]
</code>```

zip() creates tuple for each pair of items in collections, then you map one to another. If your 'arrays' have diffrent length, you can use map():

```<code>a = [1,3,4]
b = [3,4]
print map(None, a, b)
[(1, 3), (3, 4), (4, None)]
</code>```

Title: get array values in python

Body:
I have the values arr1  as 25,26 and arr2 values as A,B

Its always that the number of values in arr1 and arr2 are equal

My question is that

          for i in arr1.split(","):
                print i //prints 25 and 26

is it not possible to get the values of arr2 in the same loop or should  another loop be written only for this purpose.Basically the idea is that map the values of arr1 and arr2

Body:
You can use zip() function:

for zipped in zip(arr1.split(",") , arr2.split(",")):
    someDictionary[zipped[0]] = zipped[1]

zip() creates tuple for each pair of items in collections, then you map one to another. If your 'arrays' have diffrent length, you can use map():

a = [1,3,4]
b = [3,4]
print map(None, a, b)
[(1, 3), (3, 4), (4, None)]


问:
Jenkins use client certificate when calling other endpoints
I have a Jenkins instance from which I need to call other services/endpoints which reside behind a load balancer. This load balancer requires and does SSL client certificate validation.

Is it possible to make Jenkins use an SSL client certificate for the calls it makes towards those endpoints residing behind that load balancer?
Something like below:

Jenkins call ---present_SSL_client_cert---> LB(verify client cert) ---> endpoint

Thank you in advance!

答:
To get Jenkins to use a client cert for other endpoints start Jenkins with

JAVA_OPTS="-Djavax.net.ssl.keyStorePassword=changeme -Djavax.net.ssl.keyStoreType=pkcs12 -Djavax.net.ssl.keyStore=/cert/jenkins.p12"

I used a normal client cert:

openssl req -nodes -newkey rsa:4096 -days 365 -keyout cert/jenkins.key -out cert/jenkins.csr -subj "/C=DE/ST=somewhere/L=inCity/O=someOrg/OU=someBla/CN=jenkins"

signed it:

openssl x509 -req -days 365 -in ../jenkins/cert/jenkins.csr -CA ca/ca.crt -CAkey ca/ca.key -out ../jenkins/cert/jenkins.crt -passin env:CA_KEY

and converted it to PKCS12:

openssl pkcs12 -nodes -export -in cert/jenkins.crt -inkey cert/jenkins.key -out cert/jenkins.p12 -certfile ../nginx/ca/ca.crt -passout pass:changeme

It was important use a non empty keyStorePassword, otherwise Jenkins threw an Exception java.security.UnrecoverableKeyException: Get Key failed: null

Furthermore I added the server cert to the Java keystore:

keytool -import -alias server.domain.de -keystore /usr/local/openjdk-8/jre/lib/security/cacerts -file /root/server.crt -noprompt -storepass changeit

I've tested this setup with jenkins:lts docker container and keycloak, both behind an nginx reverse proxy using client cert auth.

Using env-variables like JENKINS_HTTPS_KEYSTORE which one can sometime see for configuring Jenkins to use https itself did NOT work for client auth. I did not investigate further, but I assume the plugin OpenId Connect Authentication Plugin which I used did not honor this variable and uses basic java functionality.


问:
Tomcat9 Failed at step NAMESPACE
My OS is Debian 10 (Buster), 4.19 kernel, and Tomcat9 seems to be unstable.

I don't want to see any error messages when I run the 'systemctl status tomcat9.service' command.

but, 'systemctl status tomcat9.service' command is print an error message.

```<code>● tomcat9.service - Apache Tomcat 9 Web Application Server
   Loaded: loaded (/lib/systemd/system/tomcat9.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2020-03-03 21:02:15 KST; 12min ago
     Docs: https://tomcat.apache.org/tomcat-9.0-doc/index.html
  Process: 25999 ExecStartPre=/usr/libexec/tomcat9/tomcat-update-policy.sh (code=exited, status=226/NAMESPACE)

Mar 03 21:02:15 doonas.com systemd[1]: Starting Apache Tomcat 9 Web Application Server...
Mar 03 21:02:15 doonas.com systemd[25999]: tomcat9.service: Failed to set up mount namespacing: No such file or directory
Mar 03 21:02:15 doonas.com systemd[25999]: tomcat9.service: Failed at step NAMESPACE spawning /usr/libexec/tomcat9/tomcat-update-policy.sh
Mar 03 21:02:15 doonas.com systemd[1]: tomcat9.service: Control process exited, code=exited, status=226/NAMESPACE
Mar 03 21:02:15 doonas.com systemd[1]: tomcat9.service: Failed with result 'exit-code'.
Mar 03 21:02:15 doonas.com systemd[1]: Failed to start Apache Tomcat 9 Web Application Server.
</code>```

However, despite the error message, I have confirmed that the webpage is working properly.

I don't know why this is happening.

I tried to solve the problem.

shutdown.sh, startup.sh have been activated.

but, it still doesn't work.

```<code>root@doonas:/usr/share/tomcat9/bin# bash shutdown.sh
Using CATALINA_BASE:   /usr/share/tomcat9
Using CATALINA_HOME:   /usr/share/tomcat9
Using CATALINA_TMPDIR: /usr/share/tomcat9/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/share/tomcat9/bin/bootstrap.jar:/usr/share/tomcat9/bin/tomcat-juli.jar
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/jav                                                                                a.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Mar 03, 2020 9:14:13 PM org.apache.catalina.startup.Catalina stopServer
SEVERE: No shutdown port configured. Shut down server through OS signal. Server not shut down.
root@doonas:/usr/share/tomcat9/bin# bash startup.sh
Using CATALINA_BASE:   /usr/share/tomcat9
Using CATALINA_HOME:   /usr/share/tomcat9
Using CATALINA_TMPDIR: /usr/share/tomcat9/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/share/tomcat9/bin/bootstrap.jar:/usr/share/tomcat9/bin/tomcat-juli.jar
Tomcat started.
</code>```

I modified the symbolic link by referring to the this link.

He said "Tomcat is sandboxed by systemd and only has write access to the following directories"

but, it still doesn't work.

```<code>root@doonas:/var/lib/tomcat9# ls -l
total 12
drwxr-xr-x 2 root   root   4096 Mar  3 21:00 conf
drwxr-xr-x 2 tomcat tomcat 4096 Jun 14  2019 lib
lrwxrwxrwx 1 root   root     16 Mar  3 20:48 logs -> /var/log/tomcat9
drwxr-xr-x 2 root   root   4096 Feb 18 13:31 policy
lrwxrwxrwx 1 root   root     19 Mar  3 20:48 work -> /var/cache/tomcat9/
root@doonas:/var/lib/tomcat9# ls -l conf/Catalina
lrwxrwxrwx 1 root root 21 Mar  3 21:00 conf/Catalina -> /etc/tomcat9/Catalina
</code>```

Here's my Tomcat9 information.

CATALINA_BASE is /usr/share/tomcat9.

```<code>root@doonas:/usr/share/tomcat9# ls -l
total 24
drwxr-xr-x 2 root root 4096 Feb 17 10:37 bin
lrwxrwxrwx 1 root root   13 Mar  3 21:14 conf -> /etc/tomcat9/
-rw-r--r-- 1 root root 1017 Jun  3  2019 default.template
drwxr-xr-x 2 root root 4096 Feb 17 10:37 etc
drwxr-xr-x 2 root root 4096 Feb 17 10:37 lib
-rw-r--r-- 1 root root  133 Apr  1  2019 logrotate.template
lrwxrwxrwx 1 root root   21 Mar  3 21:10 logs -> /var/lib/tomcat9/logs
lrwxrwxrwx 1 root root   23 Mar  3 21:10 policy -> /var/lib/tomcat9/policy
drwxr-xr-x 5 root root 4096 Feb 17 10:37 skel
lrwxrwxrwx 1 root root   21 Mar  3 21:10 work -> /var/lib/tomcat9/work
root@doonas:/usr/share/tomcat9# ls -l /etc/tomcat9/
total 208
drwxrwxr-x 3 root tomcat   4096 Feb 18 13:26 Catalina
-rw-r----- 1 root tomcat   7483 Feb  4  2019 catalina.properties
-rw-r----- 1 root tomcat   1400 Jul  5  2017 context.xml
-rw-r----- 1 root tomcat   1149 Nov 21  2017 jaspic-providers.xml
-rw-r----- 1 root tomcat   2799 Jun 14  2019 logging.properties
drwxr-xr-x 2 root tomcat   4096 Feb 17 10:37 policy.d
-rw-r----- 1 root tomcat   7638 Feb 18 13:08 server.xml
-rw-r----- 1 root tomcat   2286 Feb 18 10:49 tomcat-users.xml
-rw-r----- 1 root tomcat 170202 Nov 18  2017 web.xml
root@doonas:/usr/share/tomcat9# ls -l /var/lib/tomcat9/
total 12
drwxr-xr-x 2 root   root   4096 Mar  3 21:00 conf
drwxr-xr-x 2 tomcat tomcat 4096 Jun 14  2019 lib
lrwxrwxrwx 1 root   root     16 Mar  3 20:48 logs -> /var/log/tomcat9
drwxr-xr-x 2 root   root   4096 Feb 18 13:31 policy
lrwxrwxrwx 1 root   root     19 Mar  3 20:48 work -> /var/cache/tomcat9/
root@doonas:/usr/share/tomcat9# ls -l /etc/tomcat9/
Catalina/             context.xml           logging.properties    server.xml            web.xml
catalina.properties   jaspic-providers.xml  policy.d/             tomcat-users.xml
</code>```

答:
The error message doesn't originate from tomcat but from systemd. This unspecific message (GitHub Issue 10972) is fixed with Systemd 243.

What is the problem:
In the unit file is the option ReadWritePath= specified, but the folder/path doesn't exist.

To fix this run systemctl cat tomcat9 to get a the complete content of the unit file and all maybe existing override files.
Then check all lines with ReadWritePath= and ensure the specified directory really exist.

Title: Tomcat9 Failed at step NAMESPACE

Body:
My OS is Debian 10 (Buster), 4.19 kernel, and Tomcat9 seems to be unstable.

I don't want to see any error messages when I run the 'systemctl status tomcat9.service' command.

but, 'systemctl status tomcat9.service' command is print an error message.

● tomcat9.service - Apache Tomcat 9 Web Application Server
   Loaded: loaded (/lib/systemd/system/tomcat9.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2020-03-03 21:02:15 KST; 12min ago
     Docs: https://tomcat.apache.org/tomcat-9.0-doc/index.html
  Process: 25999 ExecStartPre=/usr/libexec/tomcat9/tomcat-update-policy.sh (code=exited, status=226/NAMESPACE)

Mar 03 21:02:15 doonas.com systemd[1]: Starting Apache Tomcat 9 Web Application Server...
Mar 03 21:02:15 doonas.com systemd[25999]: tomcat9.service: Failed to set up mount namespacing: No such file or directory
Mar 03 21:02:15 doonas.com systemd[25999]: tomcat9.service: Failed at step NAMESPACE spawning /usr/libexec/tomcat9/tomcat-update-policy.sh
Mar 03 21:02:15 doonas.com systemd[1]: tomcat9.service: Control process exited, code=exited, status=226/NAMESPACE
Mar 03 21:02:15 doonas.com systemd[1]: tomcat9.service: Failed with result 'exit-code'.
Mar 03 21:02:15 doonas.com systemd[1]: Failed to start Apache Tomcat 9 Web Application Server.

However, despite the error message, I have confirmed that the webpage is working properly.

I don't know why this is happening.

I tried to solve the problem.

shutdown.sh, startup.sh have been activated.

but, it still doesn't work.

root@doonas:/usr/share/tomcat9/bin# bash shutdown.sh
Using CATALINA_BASE:   /usr/share/tomcat9
Using CATALINA_HOME:   /usr/share/tomcat9
Using CATALINA_TMPDIR: /usr/share/tomcat9/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/share/tomcat9/bin/bootstrap.jar:/usr/share/tomcat9/bin/tomcat-juli.jar
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/jav                                                                                a.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Mar 03, 2020 9:14:13 PM org.apache.catalina.startup.Catalina stopServer
SEVERE: No shutdown port configured. Shut down server through OS signal. Server not shut down.
root@doonas:/usr/share/tomcat9/bin# bash startup.sh
Using CATALINA_BASE:   /usr/share/tomcat9
Using CATALINA_HOME:   /usr/share/tomcat9
Using CATALINA_TMPDIR: /usr/share/tomcat9/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/share/tomcat9/bin/bootstrap.jar:/usr/share/tomcat9/bin/tomcat-juli.jar
Tomcat started.

I modified the symbolic link by referring to the this link.

He said "Tomcat is sandboxed by systemd and only has write access to the following directories"

but, it still doesn't work.

root@doonas:/var/lib/tomcat9# ls -l
total 12
drwxr-xr-x 2 root   root   4096 Mar  3 21:00 conf
drwxr-xr-x 2 tomcat tomcat 4096 Jun 14  2019 lib
lrwxrwxrwx 1 root   root     16 Mar  3 20:48 logs -> /var/log/tomcat9
drwxr-xr-x 2 root   root   4096 Feb 18 13:31 policy
lrwxrwxrwx 1 root   root     19 Mar  3 20:48 work -> /var/cache/tomcat9/
root@doonas:/var/lib/tomcat9# ls -l conf/Catalina
lrwxrwxrwx 1 root root 21 Mar  3 21:00 conf/Catalina -> /etc/tomcat9/Catalina

Here's my Tomcat9 information.

CATALINA_BASE is /usr/share/tomcat9.

root@doonas:/usr/share/tomcat9# ls -l
total 24
drwxr-xr-x 2 root root 4096 Feb 17 10:37 bin
lrwxrwxrwx 1 root root   13 Mar  3 21:14 conf -> /etc/tomcat9/
-rw-r--r-- 1 root root 1017 Jun  3  2019 default.template
drwxr-xr-x 2 root root 4096 Feb 17 10:37 etc
drwxr-xr-x 2 root root 4096 Feb 17 10:37 lib
-rw-r--r-- 1 root root  133 Apr  1  2019 logrotate.template
lrwxrwxrwx 1 root root   21 Mar  3 21:10 logs -> /var/lib/tomcat9/logs
lrwxrwxrwx 1 root root   23 Mar  3 21:10 policy -> /var/lib/tomcat9/policy
drwxr-xr-x 5 root root 4096 Feb 17 10:37 skel
lrwxrwxrwx 1 root root   21 Mar  3 21:10 work -> /var/lib/tomcat9/work
root@doonas:/usr/share/tomcat9# ls -l /etc/tomcat9/
total 208
drwxrwxr-x 3 root tomcat   4096 Feb 18 13:26 Catalina
-rw-r----- 1 root tomcat   7483 Feb  4  2019 catalina.properties
-rw-r----- 1 root tomcat   1400 Jul  5  2017 context.xml
-rw-r----- 1 root tomcat   1149 Nov 21  2017 jaspic-providers.xml
-rw-r----- 1 root tomcat   2799 Jun 14  2019 logging.properties
drwxr-xr-x 2 root tomcat   4096 Feb 17 10:37 policy.d
-rw-r----- 1 root tomcat   7638 Feb 18 13:08 server.xml
-rw-r----- 1 root tomcat   2286 Feb 18 10:49 tomcat-users.xml
-rw-r----- 1 root tomcat 170202 Nov 18  2017 web.xml
root@doonas:/usr/share/tomcat9# ls -l /var/lib/tomcat9/
total 12
drwxr-xr-x 2 root   root   4096 Mar  3 21:00 conf
drwxr-xr-x 2 tomcat tomcat 4096 Jun 14  2019 lib
lrwxrwxrwx 1 root   root     16 Mar  3 20:48 logs -> /var/log/tomcat9
drwxr-xr-x 2 root   root   4096 Feb 18 13:31 policy
lrwxrwxrwx 1 root   root     19 Mar  3 20:48 work -> /var/cache/tomcat9/
root@doonas:/usr/share/tomcat9# ls -l /etc/tomcat9/
Catalina/             context.xml           logging.properties    server.xml            web.xml
catalina.properties   jaspic-providers.xml  policy.d/             tomcat-users.xml


答:
You could use the map function of ThreadPoolExecutor or ProcessPoolExecutor

Here's a possible implementation

from concurrent.futures.thread import ThreadPoolExecutor

with ThreadPoolExecutor() as executor:
    results = executor.map(get_weekday_data, range(len(files_to_read)), files_to_read)

```

Title: Multithread python count week days several dataset

Body:
Hi everyone I'm trying to use multithread in python

def get_weekday_data(t_index,file_name):
    """
   get dates (columna Date/Time).
    """
    #
    logging.info("[T{}]\tStarted with url {}".format(t_index, file_name))
    data=pd.read_csv(path+file_name)
    data['Date/Time']=pd.to_datetime(data['Date/Time'],
    infer_datetime_format=True)
    week_day_data=pd.DataFrame(data['Date/Time'].apply(lambda x:
    x.strftime('%A')))
    week_day_data_2=pd.DataFrame(week_day_data['Date/Time'].value_counts())
    week_day_data_2.reset_index(inplace=True)
    week_day_data_2.sort_values('index',inplace=True)
    print(week_day_data_2)

  logging.info("[T{}]\tEnd".format(t_index))
  return week_day_data_2

I have this function and I wnat to apply the function to 6 data sets using multithread
I try the following but I dont know how to concat de results of each thread. I m learning how to use multithread and multiprocesses.
But I dont know how to obtain the output of each thread

%%time
threads = []

#  6 threads execute get_weekday_data

for i, num in enumerate(files_to_read):
    print('now')
    thread = Thread(target= get_weekday_data, args=(i,num))
    threads.append(thread)
    thread.start()

# Espera a que los 6 threads finalicen
for i, thread in enumerate(threads):
    logging.info("[M]\tWaiting to join thread {}".format(i))
    thread.join()
    logging.info("[M]\tThread {} joined!".format(i))

logging.info("[M]\tDONE!")


问:
HTML colspan in CSS
I'm trying to construct a layout similar to the following:

```<code>+---+---+---+
|   |   |   |
+---+---+---+
|           |
+-----------+
</code>```

where the bottom is filling the space of the upper row.

If this were an actual table, I could easily accomplish this with , but as I'm simply creating a table-like layout, I cannot use
 tags. Is this possible using CSS?

答:
I came here because currently the WordPress table block doesn't support the colspan parameter and i thought i will replace it using CSS. This was my solution, assuming that the columns are the same width:

table {
  width: 100%;
}

table td {
  width: 50%;
  background: #dbdbdb;
  text-align: center;
}

table tr:nth-child(2n+1) {
  position:relative;
  display:block;
  height:20px;
  background:green;
}

table tr:nth-child(2n+1) td {
  position:absolute;
  left:0;
  right:-100%;
  width: auto;
  top:0;
  bottom:0;
  background:red;
  text-align:center;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><table>
    <tr>
        <td>row</td>
    </tr>
    <tr>
        <td>cell</td>
        <td>cell</td>
    </tr>
    <tr>
        <td>row</td>
    </tr>
    <tr>
        <td>cell</td>
        <td>cell</td>
    </tr>
</table></code>```

Title: HTML colspan in CSS

Body:
I'm trying to construct a layout similar to the following:

+---+---+---+
|   |   |   |
+---+---+---+
|           |
+-----------+

where the bottom is filling the space of the upper row.

If this were an actual table, I could easily accomplish this with , but as I'm simply creating a table-like layout, I cannot use
 tags. Is this possible using CSS?

Body:
I came here because currently the WordPress table block doesn't support the colspan parameter and i thought i will replace it using CSS. This was my solution, assuming that the columns are the same width:

table {
  width: 100%;
}

table td {
  width: 50%;
  background: #dbdbdb;
  text-align: center;
}

table tr:nth-child(2n+1) {
  position:relative;
  display:block;
  height:20px;
  background:green;
}

table tr:nth-child(2n+1) td {
  position:absolute;
  left:0;
  right:-100%;
  width: auto;
  top:0;
  bottom:0;
  background:red;
  text-align:center;
}

<table>
    <tr>
        <td>row</td>
    </tr>
    <tr>
        <td>cell</td>
        <td>cell</td>
    </tr>
    <tr>
        <td>row</td>
    </tr>
    <tr>
        <td>cell</td>
        <td>cell</td>
    </tr>
</table>


问:
XmlWriter outputs unexpected encoding when printing to console
While learning the C# XmlWriter class I came across some strange behavior. When outputting to a file it uses UTF-8 as expected but when outputting to the Console it uses my system code-page (862) instead of UTF-8. I know consoles don't support UTF-8, so I'm wondering if XmlWriter defaults to system code-page if the stream is a console?

Code to reproduce behavior:

using System;
using System.Xml;

namespace xmlwriterTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var settings = new XmlWriterSettings
            {
                Indent = true
            };

            using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("users");

                writer.WriteStartElement("user");
                writer.WriteAttributeString("age", "42");
                writer.WriteString("John Doe");
                writer.WriteEndElement();

                writer.WriteStartElement("user");
                writer.WriteAttributeString("age", "39");
                writer.WriteString("Jane Doe");

                writer.WriteEndDocument();
                writer.Close();
                Console.WriteLine("\n\n");
            }
        }
    }
}
```

Expected output:

<?xml version="1.0" encoding="utf-8"?>

Actual output:

<?xml version="1.0" encoding="Codepage - 862"?>

edit
My question is not how to achieve the expected output in the console. It is why this happens in the first place

答:
Update as per comment

Why this happens in the first place : because the default Encoding for Console.Out is not UTF-8, I'm doing a test like this:

1.1 - Default Encoding :

```<code>TextWriter textWriter = Console.Out;

Console.WriteLine(textWriter.Encoding.BodyName);
</code>```

1.2 - Result :

```<code>ibm850
</code>```

2.1 - Custom Encoding of Console.Out :

```<code>Console.OutputEncoding = Encoding.UTF8;
TextWriter textWriter = Console.Out;

Console.WriteLine(textWriter.Encoding.BodyName);
</code>```

2.2 - Result :

```<code>utf-8
</code>```

The whole code :

```<code>XmlWriterSettings settings = new XmlWriterSettings
{
    Indent = true
};

Console.OutputEncoding = Encoding.UTF8;

using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
{
    writer.WriteStartDocument();
    writer.WriteStartElement("users");

    writer.WriteStartElement("user");
    writer.WriteAttributeString("age", "42");
    writer.WriteString("John Doe");
    writer.WriteEndElement();

    writer.WriteStartElement("user");
    writer.WriteAttributeString("age", "39");
    writer.WriteString("Jane Doe");

    writer.WriteEndDocument();
    writer.Close();

    Console.WriteLine();
}
</code>```

Result:

```<code><?xml version="1.0" encoding="utf-8"?>
<users>
  <user age="42">John Doe</user>
  <user age="39">Jane Doe</user>
</users>
</code>```

Old code

You can create a custom StringWriter by using StringBuilder and override Encoding property like :

1 - Custom class:

```<code>public class EncodedStringWriter : StringWriter
{
    public EncodedStringWriter(StringBuilder sb, Encoding encoding)
        : base(sb)
    {
        _Encoding = encoding;
    }

    private readonly Encoding _Encoding;

    public override Encoding Encoding => _Encoding;
}
</code>```

2 - Construct the XML, by using the custom stringWriter :

```<code>StringBuilder sb = new StringBuilder();
EncodedStringWriter stringWriter = new EncodedStringWriter(sb, Encoding.UTF8);

XmlWriterSettings settings = new XmlWriterSettings
{
    Indent = true
};

using (XmlWriter writer = XmlWriter.Create(stringWriter, settings))
{
    writer.WriteStartDocument();
    writer.WriteStartElement("users");

    writer.WriteStartElement("user");
    writer.WriteAttributeString("age", "42");
    writer.WriteString("John Doe");
    writer.WriteEndElement();

    writer.WriteStartElement("user");
    writer.WriteAttributeString("age", "39");
    writer.WriteString("Jane Doe");

    writer.WriteEndDocument();
    writer.Close();

    Console.WriteLine(sb.ToString());
}
</code>```

Result

```<code><?xml version="1.0" encoding="utf-8"?>
<users>
  <user age="42">John Doe</user>
  <user age="39">Jane Doe</user>
</users>
</code>```

I hope you find this helpful.

Body:
While learning the C# XmlWriter class I came across some strange behavior. When outputting to a file it uses UTF-8 as expected but when outputting to the Console it uses my system code-page (862) instead of UTF-8. I know consoles don't support UTF-8, so I'm wondering if XmlWriter defaults to system code-page if the stream is a console?

Code to reproduce behavior:

using System;
using System.Xml;

namespace xmlwriterTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var settings = new XmlWriterSettings
            {
                Indent = true
            };

            using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("users");

                writer.WriteStartElement("user");
                writer.WriteAttributeString("age", "42");
                writer.WriteString("John Doe");
                writer.WriteEndElement();

                writer.WriteStartElement("user");
                writer.WriteAttributeString("age", "39");
                writer.WriteString("Jane Doe");

                writer.WriteEndDocument();
                writer.Close();
                Console.WriteLine("\n\n");
            }
        }
    }
}

Expected output:

<?xml version="1.0" encoding="utf-8"?>

Actual output:

<?xml version="1.0" encoding="Codepage - 862"?>

edit
My question is not how to achieve the expected output in the console. It is why this happens in the first place

Body:
Update as per comment

Why this happens in the first place : because the default Encoding for Console.Out is not UTF-8, I'm doing a test like this:

1.1 - Default Encoding :

TextWriter textWriter = Console.Out;

Console.WriteLine(textWriter.Encoding.BodyName);

1.2 - Result :

ibm850

2.1 - Custom Encoding of Console.Out :

Console.OutputEncoding = Encoding.UTF8;
TextWriter textWriter = Console.Out;

Console.WriteLine(textWriter.Encoding.BodyName);

2.2 - Result :

utf-8

The whole code :

XmlWriterSettings settings = new XmlWriterSettings
{
    Indent = true
};

Console.OutputEncoding = Encoding.UTF8;

using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
{
    writer.WriteStartDocument();
    writer.WriteStartElement("users");

    writer.WriteStartElement("user");
    writer.WriteAttributeString("age", "42");
    writer.WriteString("John Doe");
    writer.WriteEndElement();

    writer.WriteStartElement("user");
    writer.WriteAttributeString("age", "39");
    writer.WriteString("Jane Doe");

    writer.WriteEndDocument();
    writer.Close();

    Console.WriteLine();
}

Result:

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user age="42">John Doe</user>
  <user age="39">Jane Doe</user>
</users>

Old code

You can create a custom StringWriter by using StringBuilder and override Encoding property like :

1 - Custom class:

public class EncodedStringWriter : StringWriter
{
    public EncodedStringWriter(StringBuilder sb, Encoding encoding)
        : base(sb)
    {
        _Encoding = encoding;
    }

    private readonly Encoding _Encoding;

    public override Encoding Encoding => _Encoding;
}

2 - Construct the XML, by using the custom stringWriter :

StringBuilder sb = new StringBuilder();
EncodedStringWriter stringWriter = new EncodedStringWriter(sb, Encoding.UTF8);

XmlWriterSettings settings = new XmlWriterSettings
{
    Indent = true
};

using (XmlWriter writer = XmlWriter.Create(stringWriter, settings))
{
    writer.WriteStartDocument();
    writer.WriteStartElement("users");

    writer.WriteStartElement("user");
    writer.WriteAttributeString("age", "42");
    writer.WriteString("John Doe");
    writer.WriteEndElement();

    writer.WriteStartElement("user");
    writer.WriteAttributeString("age", "39");
    writer.WriteString("Jane Doe");

    writer.WriteEndDocument();
    writer.Close();

    Console.WriteLine(sb.ToString());
}

Result

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user age="42">John Doe</user>
  <user age="39">Jane Doe</user>
</users>

I hope you find this helpful.


问:
Converting a json object to an associative array
I have this array which is retrieved by a json_encode().When I execute

```<code>$.getJSON('beta.php' , function(data){
    console.log(data);
});
</code>```

I get the result as follows

```<code>[
Object { StuId="1", fName="Saman", more...},
Object { StuId="2", fName="Marry", more...},
Object { StuId="3", fName="Navjoth", more...},
Object { StuId="4", fName="Jassu", more...}
]
</code>```

I'm trying to iterate through this result using

```<code>$.each(data, function(key, value){

            for(var key in value){

                if(value.hasOwnProperty(key)){

                        $("#article tbody").html(
                        "<tr><td>" + value.StuId +
                        "</td><td>" + value.fName +
                        "</td><td>" + value.lName +
                        "</td><td>" + value.age +
                        "</td><td>" + value.grade +
                        "</td></tr>");

                        $("article tbody").appendTo("document.body");

                }

                }

        });
</code>```

.I guess It is impossible because of the above format of the array.

If someone could explain why this is happening and how to correct it I would be really grateful.I want to know how to convert the above into the following format.

```<code>[
{"StuId":"1","fName":"Saman","lName":"Kumara","age":"14","grade":"A"},
{"StuId":"2","fName":"Marry","lName":"Vass","age":"12","grade":"B"},
{"StuId":"3","fName":"Navjoth","lName":"Bogal","age":"32","grade":"A"},
{"StuId":"4","fName":"Jassu","lName":"Singh","age":"22","grade":"E"}
]
</code>```

答:
You are overwriting the tables html with different values each time.

```<code>$(document).ready(function() {
  var html = "";
  var data = [
          { StuId:"1", fName:"Saman"},
          { StuId:"2", fName:"Marry"},
          { StuId:"3", fName:"Navjoth"},
          { StuId:"4", fName:"Jassu"}
          ]
  $.each(data, function(key, value){
    console.log(value + "--" + key);
    html += "<div><span>" + value.StuId + "</span><span>" + value.fName + "</span></div>";
  });
  $('body').html(html)
});
</code>```

Loops in JavaScript are different from other languages. You need closures.

Have a look here:

http://www.mennovanslooten.nl/blog/post/62

Body:
I have this array which is retrieved by a json_encode().When I execute

$.getJSON('beta.php' , function(data){
    console.log(data);
});

I get the result as follows

[
Object { StuId="1", fName="Saman", more...},
Object { StuId="2", fName="Marry", more...},
Object { StuId="3", fName="Navjoth", more...},
Object { StuId="4", fName="Jassu", more...}
]

I'm trying to iterate through this result using

$.each(data, function(key, value){

            for(var key in value){

                if(value.hasOwnProperty(key)){

                        $("#article tbody").html(
                        "
" + value.StuId +
                        "	" + value.fName +
                        "	" + value.lName +
                        "	" + value.age +
                        "	" + value.grade +
                        "
");

                        $("article tbody").appendTo("document.body");

                }

                }

        });

.I guess It is impossible because of the above format of the array.

If someone could explain why this is happening and how to correct it I would be really grateful.I want to know how to convert the above into the following format.

[
{"StuId":"1","fName":"Saman","lName":"Kumara","age":"14","grade":"A"},
{"StuId":"2","fName":"Marry","lName":"Vass","age":"12","grade":"B"},
{"StuId":"3","fName":"Navjoth","lName":"Bogal","age":"32","grade":"A"},
{"StuId":"4","fName":"Jassu","lName":"Singh","age":"22","grade":"E"}
]

Body:
You are overwriting the tables html with different values each time.

$(document).ready(function() {
  var html = "";
  var data = [
          { StuId:"1", fName:"Saman"},
          { StuId:"2", fName:"Marry"},
          { StuId:"3", fName:"Navjoth"},
          { StuId:"4", fName:"Jassu"}
          ]
  $.each(data, function(key, value){
    console.log(value + "--" + key);
    html += "<div><span>" + value.StuId + "</span><span>" + value.fName + "</span></div>";
  });
  $('body').html(html)
});

Loops in JavaScript are different from other languages. You need closures.

Have a look here:

http://www.mennovanslooten.nl/blog/post/62


问:
Python: coursera assignments - Dictionary chapter
I'm trying to solve the assignments from coursera - Python.

Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.

```<code>name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)

lst = list()
for line in handle:
    line = line.strip()
    if line.startswith("From"):
        words = line.split()
        email = words[1]
        lst.append(email)

dct = dict()
for email in lst:
    dct[email] = dct.get(email,0)+1

bigcount = None
email_address = None
for key,value in dct.items():
    if bigcount is None or value > bigcount:
        bigcount = value
        email_address = key

print email_address, bigcount
</code>```

My code runs, but the desired output should be: cwen@iupui.edu 5  , but i get somehow "doubled" output: cwen@iupui.edu 10

Did anyone solved this problem? Could you please give me a hint what have i missed? Thank you a lot!

答:
You could try this code snippet:

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
counts=dict()
handle = open(name)
for line in handle:
    if line.startswith('From '):
        line=line.split()
        counts[line[1]]=counts.get(line[1],0)+1
bigword=None
bigcount=0
for key,val in counts.items():
  if bigcount==0 or val>bigcount :
        bigcount=val
        bigword=key
print(bigword,bigcount)
```

Body:
I'm trying to solve the assignments from coursera - Python.

Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)

lst = list()
for line in handle:
    line = line.strip()
    if line.startswith("From"):
        words = line.split()
        email = words[1]
        lst.append(email)

dct = dict()
for email in lst:
    dct[email] = dct.get(email,0)+1

bigcount = None
email_address = None
for key,value in dct.items():
    if bigcount is None or value > bigcount:
        bigcount = value
        email_address = key

print email_address, bigcount

My code runs, but the desired output should be: cwen@iupui.edu 5  , but i get somehow "doubled" output: cwen@iupui.edu 10

Did anyone solved this problem? Could you please give me a hint what have i missed? Thank you a lot!


问:
Is it fine to use javascript sort instead of mongoose sort?
I have a messaging app, and a route to fetch a user's messages. A user can have 1000+ threads. I use Mongoose and Express.

I basically find a user by id, populate its messages array, and then apply a reverse() on it so it displays all the threads from new to old. I do it because I  don't know how to sort a populated field. Is it fine to use native js instead of mongoose sort method form a performance point of view?

 const user = await User.findById({ _id: userId }).populate("messages");
    return res.json({ messages: user.messages.reverse() });```

If it's crap, how to sort such a populated field?

答:
if you want to sort in order of creation you can use the _id field of each message. MongoDB treats sorting by _id the same as sorting by creation time. So your mongoose query would look like:

```<code>const user = await User
  .findById({ _id: userId })
  .populate({path: 'messages', options: { sort: { '_id': -1 } } });

return res.json({ messages: user.messages });
</code>```

I am not quite sure as to the performance comparison of sorting using JS or Using mongoose though...

Body:
I have a messaging app, and a route to fetch a user's messages. A user can have 1000+ threads. I use Mongoose and Express.

I basically find a user by id, populate its messages array, and then apply a reverse() on it so it displays all the threads from new to old. I do it because I  don't know how to sort a populated field. Is it fine to use native js instead of mongoose sort method form a performance point of view?

 const user = await User.findById({ _id: userId }).populate("messages");
    return res.json({ messages: user.messages.reverse() });

If it's crap, how to sort such a populated field?

Body:
if you want to sort in order of creation you can use the _id field of each message. MongoDB treats sorting by _id the same as sorting by creation time. So your mongoose query would look like:

const user = await User
  .findById({ _id: userId })
  .populate({path: 'messages', options: { sort: { '_id': -1 } } });

return res.json({ messages: user.messages });

I am not quite sure as to the performance comparison of sorting using JS or Using mongoose though...


问:
SELECT no of columns dynamically from DUAL
I have a requirement like below :

I have to show 10 columns every time.

Suppose I am selecting from a table tab which has 7 columns, I have to add 3 extra columns like below:

```<code>select * from tab,(select 'dummy' d1 , 'dummy' d2 , 'dummy' d3 from dual);
</code>```

If the no of columns in the table increases to 8 then:

```<code>select * from tab,(select 'dummy' d1 , 'dummy' d2  from dual);
</code>```

Is there any way to achieve this in Oracle 11g without using PL/SQL?

答:
I think this cannot be done.

I'd solve it with a view. You can generate/update the view automatically, but only using PL/SQL which violates your requirement:

```<code>DECLARE
  a CONSTANT VARCHAR2(100) := 'CREATE OR REPLACE VIEW v AS SELECT ';
  b CONSTANT VARCHAR2(100) := ' FROM TAB';
  c VARCHAR2(3000);
  i NUMBER;
  max_cols CONSTANT NUMBER := 10;
BEGIN
  SELECT listagg(column_name,',') WITHIN GROUP (ORDER BY column_id) INTO c
    FROM user_tab_columns
   WHERE table_name = 'TAB';
  SELECT count(*) INTO i
    FROM user_tab_columns
   WHERE table_name = 'TAB';
  WHILE (i < max_cols) LOOP
    i := i + 1;
    c := c || ', ''dummy'' AS col_'||i;
  END LOOP;
  c := a || c || b;
  DBMS_OUTPUT.PUT_LINE(C);
  EXECUTE IMMEDIATE c;
END;
/
</code>```

The result is a view that selects all columns of table tab and fills up columns containing dummy until 10 columns are reached:

```<code>CREATE OR REPLACE VIEW v AS
SELECT C1,C2,C3,C4,C5,C6,C7,
       'dummy' AS col_8, 'dummy' AS col_9, 'dummy' AS col_10
  FROM TAB;
</code>```

Next idea is a pipelined table function to fill up the columns of your table, but that violates again your no PL/SQL requirement.

Thirdly, polymorphic table functions come to mind, but they are only available from Oracle 18.

Body:
I have a requirement like below :

I have to show 10 columns every time.

Suppose I am selecting from a table tab which has 7 columns, I have to add 3 extra columns like below:

select * from tab,(select 'dummy' d1 , 'dummy' d2 , 'dummy' d3 from dual);

If the no of columns in the table increases to 8 then:

select * from tab,(select 'dummy' d1 , 'dummy' d2  from dual);

Is there any way to achieve this in Oracle 11g without using PL/SQL?

Body:
I think this cannot be done.

I'd solve it with a view. You can generate/update the view automatically, but only using PL/SQL which violates your requirement:

DECLARE
  a CONSTANT VARCHAR2(100) := 'CREATE OR REPLACE VIEW v AS SELECT ';
  b CONSTANT VARCHAR2(100) := ' FROM TAB';
  c VARCHAR2(3000);
  i NUMBER;
  max_cols CONSTANT NUMBER := 10;
BEGIN
  SELECT listagg(column_name,',') WITHIN GROUP (ORDER BY column_id) INTO c
    FROM user_tab_columns
   WHERE table_name = 'TAB';
  SELECT count(*) INTO i
    FROM user_tab_columns
   WHERE table_name = 'TAB';
  WHILE (i < max_cols) LOOP
    i := i + 1;
    c := c || ', ''dummy'' AS col_'||i;
  END LOOP;
  c := a || c || b;
  DBMS_OUTPUT.PUT_LINE(C);
  EXECUTE IMMEDIATE c;
END;
/

The result is a view that selects all columns of table tab and fills up columns containing dummy until 10 columns are reached:

CREATE OR REPLACE VIEW v AS
SELECT C1,C2,C3,C4,C5,C6,C7,
       'dummy' AS col_8, 'dummy' AS col_9, 'dummy' AS col_10
  FROM TAB;

Next idea is a pipelined table function to fill up the columns of your table, but that violates again your no PL/SQL requirement.

Thirdly, polymorphic table functions come to mind, but they are only available from Oracle 18.


问:
How can I get top 5 values from json which stored in one column?
I have table name log_requests where I store json array into request column, which is like following

{"game_id":"1","score":"820",}

game_ids are 1 to 20, now I want to know which game_id is used many time. To getting count of a game_id I am using following query which is working fine

SELECT * FROM log_requests WHERE json_contains(request, '{"game_id" : "1"}')

But how can I get top 5 game_id from this table?

答:
As Your Common Sense said its not optimal, but you can solve this by making a temporary table.

Something like this:

```<code>CREATE TEMPORARY TABLE
  temp_log_requests
SELECT
  JSON_EXTRACT(request, '$.game_id') AS game_id,
  ... -- Include the columns you want to use from log_requests here
FROM
  log_requests
LIMIT 0;
</code>```

Then in the same session you can query the temporary table like any other.

See: https://dev.mysql.com/doc/refman/8.0/en/create-temporary-table.html

Body:
As Your Common Sense said its not optimal, but you can solve this by making a temporary table.

Something like this:

CREATE TEMPORARY TABLE
  temp_log_requests
SELECT
  JSON_EXTRACT(request, '$.game_id') AS game_id,
  ... -- Include the columns you want to use from log_requests here
FROM
  log_requests
LIMIT 0;

Then in the same session you can query the temporary table like any other.

See: https://dev.mysql.com/doc/refman/8.0/en/create-temporary-table.html


问:
How can I get the console output in Angular webpage?
I have a Django project which is using Angular as frontend. I have a button which on clicking is scanning the tables in the database. I have some print statements views.py file which is printing the scanned results constantly in the IDE console. I want that output in the webpage. I want that live printing of the console output in the frontend. Can any one know how i can achieve this?</p>

答:
You can try calling the following function with the information needed to be displayed.

```<code>addItem(val:any) {
   let node = document.createElement("li")
   let textnode = document.createTextNode(val)
   node.appendChild(textnode)
   document.getElementById("output").appendChild(node)
}
</code>```

Make sure to have an element with the id="output".

Body:
You can try calling the following function with the information needed to be displayed.

addItem(val:any) {
   let node = document.createElement("li")
   let textnode = document.createTextNode(val)
   node.appendChild(textnode)
   document.getElementById("output").appendChild(node)
}

Make sure to have an element with the id="output".