Proving that a function on a $\sigma$-finite measure space is continuous

Question:
Let $(Y,\mu)$ be a $\sigma$-finite measure space, $U \subseteq
\mathbb{R}$ open and $g \in \mathcal{L}^1(Y)$. Let $f: U \times Y \to
\mathbb{R}$ be such that $f^y(x) := f(x,y)$ is continuous $\mu$-a.e.
  and that $\int_Yf(x,y)d\mu(y)$ exists for any $x \in U$. Now define
  $h:U \to \mathbb{R}$ by $$h(x) := \int_Yf(x,y)d\mu(y)$$ and show that
  $h$ is continuous.

My idea was to use the definition of continuity. Since $f^y$ is continuous $\mu$-a.e. we have for any $x_0 \in U$ and $\varepsilon > 0$ that there exists $\delta > 0$ such that $$|x - x_0| < \delta \Rightarrow |f^y(x) - f^y(x_0)| < \varepsilon$$ holds $\mu$-a.e.
Let $Y = \bigcup_{n \in \mathbb{N}} Y_n$ with $Y_n$ increasing and $\mu(Y_n) < \infty$. Then we have for any $n \in \mathbb{N}$ and $|x - x_0| < \delta/\mu(Y_n)$ $$|h(x)\chi_{Y_n} - h(x_0)\chi_{Y_n}| \leq \int_Y|f(x,y) - f(x_0,y)|\chi_{Y_n}d \mu(y) < \varepsilon $$ By $\lim_{n \to \infty}h\chi_{Y_n} = h$ we get that $$|h(x) - h(x_0)| < \varepsilon$$ whenever $|x - x_0| < \lim_{n \to \infty} \varepsilon/\mu(Y_n)$. Now my problem is, that $\delta = 0$ might occur which is really bad. Is there any way to fix the proof?
Remark. I want to use $\delta-\epsilon$-definition of continuity.
Comment: You mention $g$ once, but never use it. I expect that its use will be crucial (in an application of the dominated convergence theorem).
Comment: Yes, that is a good point. Totally forgot. It should be that $|f(x,y)| \leq g(y)$ for $\mu$-a.e $y$ and all $x \in U$. Crucial indeed.
Answer: It's simpler to work with sequences. Let $x$ be a fixed point in $U$, and $x_n$ be a sequence that converges to $x$. Let us prove that $h(x_n)$ converges to $h(x)$.
Let $f_n: y\mapsto f(x_n,y)$ and $f':y\mapsto f(x,y)$. The hypothesis "$|f(x,y)| \leq g(y)$ for $\mu$-a.e $y$ and all $x\in U$" translates to $|f_n(y)|\leq g(y)$ for $\mu$-a.e $y$.
Moreover, note that since $f^y(x) := f(x,y)$ is continuous $\mu$-a.e, $f_n$ converges pointwise to $f'$ $\mu$-a.e.
Lebesgue dominated convergence applies and yields: $$\lim_n\int f_n d\mu = \int f' d\mu$$ which rewrites as $\lim_n h(x_n) = h(x)$. Hence continuity.


Why does the Ciphar 10 tutorial on TensorFlow crop the images to be 24x24?

Question: I was going over the cifar 10 tutorial in tensorflow and was trying to understand why the guys in tensorflow/google decided to crop the images. The only reason I could justify it to myself is because they wanted to possibly decrease the computation time when training the neural net, otherwise it seems rather random.
I've talked with colleges and they imply it has to do with data set augmentation, however, data set augmentation can be done, regardless the size of the original image (to my understanding).
My initial guess of why they might be cropping and then augmenting the data set (with flips, brightness changes and contrasts) is because they want to get the actual image object and ignore the surroundings and then apply the transformation. If that were the goal, then for me what would have made sense is to not apply a random crop as they are doing (otherwise how do you know you are getting the actual object?) and then do the data set augmentation. Since thats not whats happening, I have my doubts, that my guess is correct.
Can someone clarify why the cropping is being done? Maybe I am just overthinking it but it would be great to clarify whats going on. For me it seems its just some (arbitrary) preprocessing that they are doing god knows why.
Answer: The cropping is indeed performed for data augmentation (i.e., cropping is one data augmentation strategy), since if one applies two different crops to the same image, one obtains two different images. E.g., from {1}:

For data augmentation, we randomly crop input images into 24 × 24 pixels.

As a side note, the CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. This means that 24x24 cropping keeps most of the image.

References:

{1} Xie, Lingxi, Jingdong Wang, Zhen Wei, Meng Wang, and Qi Tian. "DisturbLabel: Regularizing CNN on the Loss Layer." CVPR 2016.  https://scholar.google.com/scholar?cluster=11976349396086878628&hl=en&as_sdt=0,22 ; https://arxiv.org/abs/1605.00055
Comment: @Pinocchio If you do it twice, you have two different images: for that reason it is often regarded as data augmentation.
Comment: @Pinocchio That's correct. It's ok, I like clear definitions as well. Answer edited.
Comment: maybe this is a semantics issue I have with the phrase "data set augmentation" but just randomly cropping an image is **not** augmenting the data set in any way.
Comment: I see. However, it only really counts as data set augmentation if you add **both** images to the "new" data set. Otherwise, its more of a pre-processing step, right? (sorry if I am being overly pedantic but I finding a bit confusing)
Comment: Also, maybe you should include in your answer more explicitly why cropping would be considered data set augmentation (since it seems to be the main issue of my question).
Comment: as a random side comment, randomly cropping doesn't seem like a great way to augment the data set without any further transformations. I guess its sort of making the algorithm robust to seeing the same object as if it was being viewed from different windows, but it seems rather limited. Say we did add multiple crops, I wonder if this type of transformation makes it more likely to lead to overfitting since we are essentially "memorizing" the same object.


Asymptotic Approximation for a Derangement Algorithm

Question: Asymptotic Approximation for a Derangement Algorithm
I have been analyzing the complexity of a simple algorithm for randomly generating a derangement, i.e., a permutation $\pi$ with no fixed points: $\pi_i \ne i,\, i = 1,2, \ldots,n.$
The expected total work of this (Las Vegas) algorithm is
$$
    W_T(n) = W_s(n) + \frac{1-p(n)}{p(n)}W_f(n),
$$
where $p(n)$ is the probability of success (a derangement is generated), $W_s(n)$ the expected work to generate a success, and $W_f(n)$ is the expected work to generate a failure.
I know that $W_s(n) = n-1$, and $p(n) = 1/e \approx 0.368$, but I'm having difficulty with $W_f(n)$. Here is what I have so far:
$$
    W_f(n) =\sum_{k=1}^{n-2}k p_{nk}= \sum_{k=1}^{n-2}\,\prod_{i=0}^{k-1}\left(1-\frac{1}{n-i}\right).
$$
$W_f(n)=4.4, 49.49, 499.499, 4999.4999, 49999.49999$, for $n = 10^1, 10^2, 10^3,10^4,10^5.$
This suggests that
$$
\lim_{n\rightarrow \infty} \sum_{k=1}^{n-2}\,\prod_{i=0}^{k-1}\left(1-\frac{1}{n-i}\right) = \frac{1}{2}n.
$$
We now have all the pieces of the first equation equation and we get
$$
    W_T(n) = n-1 + \frac{(1-1/e)}{1/e}\frac{n}{2}  = \frac{\left( e+1\right)}{2}\,n-1 \approx 1.86n .
$$
Testing the algorithm shows that this is roughly correct, but I have not been able to prove the $\frac{1}{2}n$ limit above. The best I have come up with so far is
$$\ln(kp_{nk}) = H_n-H_{n-k},\quad W_f(n) = \sum_{k=1}^{n-2} e^{H_n-H_{n-k}}$$
which goes to something like $2n-\ln n$. This does not seem correct. Any help would be very appreciated.
Derek O'Connor
Comment: By pure coincidence I came across this post again and realized you made an edit to include another question you need help with. Unfortunately I can't help you right now, but my advice is that you should make a completely new thread on the new question. Barely anyone would be opening this question anymore, its many pages from the front page.
Comment: @Ragib, Thanks for your advice. I have found a flaw in the Matlab function above. A small patch fixes it but I don't like that so I'm going to delete it.
Answer: Observe
$$\prod_{i=0}^{k-1}\left(1-\frac{1}{n-i}\right)=\left(1-\frac{1}{n}\right)\left(1-\frac{1}{n-1}\right)\cdots\left(1-\frac{1}{n-(k-1)}\right)$$
$$=\frac{n-1}{n}\cdot\frac{n-2}{n-1}\cdot\frac{n-3}{n-2}\cdot\cdots\frac{n-(k-1)-1}{n-(k-1)} $$
$$=\frac{1}{n}\cdot\frac{n-k}{1}$$ due to massive cancellation in consecutive numerator/denominators. Hence the sum is
$$\sum_{k=1}^{n-2}\left(1-\frac{k}{n}\right)=\left(\sum_{k=1}^{n-2}1\right)-\frac{1}{n}\left(\sum_{k=1}^{n-2}k\right)$$
$$=(n-2)-\frac{1}{n}\frac{(n-2)(n-1)}{2}$$
$$=\frac{n-1}{2}-\frac{1}{n}$$
which matches the given calculations exactly. Note the formula for the triangle numbers above.
Comment: Damn, you beat me to it! I had the same answer half typed up =[ lol.
Comment: @Ragib: The moment I saw the pattern I knew this would end up a race to type it out. ;)
Comment: @anon: Superb! Thank you very much. Why didn't I see that?


.Net MVC Database first model with stored procedures

Question: I'm developing a MVC5 web project in VS 2013 and I have to use an already existing database and its Stored Procedures so I'm looking forward to using Entity Framework database first approach to help me model the classes.
My question is, should I create the classes (the model) directly from the tables using EF? i mean should my classes represent a table in the database exactly the way they are? - given that some stored procedures return a combination of different attributes from different tables, I'm confused as what the classes on the code should represent exactly.
Also i want to have my own form to let users upload and read their info, so scaffolding the views to create the read/update/delete won't come handy for this task, will it?
Thanks!
Answer: If it is code first then you can use the EF tools to scaffold your database for you from your existing database.  If it's database first, all of the database models are generate for you anyway and whenever you update your database the models can be updated to reflect the changes for you.
If you are using stored procedures for code first, you'll need to create objects for each stored procedure so that the return values can be mapped back to an object.  These should really match precisely the data that is being returned back in both type and naming:
<code>this.Database.SqlQuery<YourEntityType>("storedProcedureName",params);
</code>
As for having your views scaffolded for you, I think you should take one step at a time and see what works for your use case or not.


Split words from datraframe by space to rows while duplicating the info from other columns ( python,pandas)

Question: I have a df which is composed by 5 columns one being a comment column where people leave a comment.
What I would like to do is split that comment column by space into multiple rows while duplicating the other columns:
df:

r_id
start
comments

1
2021-01-01
i am the text that needs splitting by space to rows

2
2021-01-02
hello hello

wanted result:

r_id
start
comments

1
2021-01-01
i

1
2021-01-01
am

1
2021-01-01
the

1
2021-01-01
text

2
2021-01-02
hello

2
2021-01-02
hello

I've tried anything from str.split() to regex without result.
-- le with code:
df = df.apply(lambda x: x.str.lower() if x.dtype == "object" else x)
(df
 .assign(comments=df['comments'].str.split())
 .explode('comments')
)
print(df)

df['comments'] = df['comments'].str.replace('ă','a')
df['comments'] = df['comments'].str.replace('â','a')
df['comments'] = df['comments'].str.replace('î','i')
df['comments'] = df['comments'].str.replace('ș','s')
df['comments'] = df['comments'].str.replace('ț','t')
df.replace('[^a-zA-Z0-9]', ' ',regex=True)
df.dropna(inplace=True)
print(df)

yet it wouldn't  split the comment
Answer: You need to split and explode:
df2 = (df
 .assign(comments=df['comments'].str.split())
 .explode('comments')
)

output:
   r_id        start    comments
0      1  2021-01-01           i
0      1  2021-01-01          am
0      1  2021-01-01         the
0      1  2021-01-01        text
0      1  2021-01-01        that
0      1  2021-01-01       needs
0      1  2021-01-01   splitting
0      1  2021-01-01          by
0      1  2021-01-01       space
0      1  2021-01-01          to
0      1  2021-01-01        rows
1      2  2021-01-02       hello
1      2  2021-01-02       hello

Comment: @hectic can you update the example in the question? comments are not really good for code
Comment: thanks! unfortunately it doesn t spilt it :|

df.replace('[^a-zA-Z0-9]', ' ',regex=True)
df.dropna(inplace=True)

(df
 .assign(comments=df['comments'].str.split())
 .explode('comments')
)
print(df) --- and it wouldnt split the text
Comment: edited  the question and added a screenshot
Comment: @hectic I don't see the the difference, you said "*What I would like to do is split that comment column by space into multiple rows while duplicating the other columns*", isn't it what my code is doing? Can you edit the expected output?
Comment: i posted a screenshot with that the code is doing, and unfortunately it does not split the text...which for some reason got posted on top of the question...
Comment: @hectic did you assign the output of my code? ;) Check the update, I made it explicit


Using AJAX to get response - but page is still refreshing

Question: I am having issues with filling in a form and pressing submit to send my information to a database. The problem is that my page refreshes even though I am using AJAX to stay on the same page and receive a reply in "show_label".
I have tested to see if addCustomerFunc gets called by changing the show_label, and it does change! But still refreshes and I lose the message after it refreshes. I was hoping if someone could catch my mistake(s).
This is in my customer.php

Add New Customer:

Last Name:

First Name:

Phone Number:

Date of Birth:

            <form>
                <input type="text" name="add_LN" id="add_LN" />

                <input type="text" name="add_FN" id="add_FN" />

                <input type="text" name="add_PN" id="add_PN" />

                <input type="text" name="add_DOB" id="add_DOB" />

                <input type="submit" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(add_LN, add_FN, add_PN, add_DOB)"/>
            </form>

addCustomer.php
<?php
$username="****";
$password="****";
$database="****";

$lastName=$_POST['add_LN'];
$firstName=$_POST['add_FN'];
$phone=$_POST['add_PN'];
$dob=$_POST['add_DOB'];
//$membership==$_POST['membership'];

mysql_connect("******",$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO customer (last_name, first_name, phone_no, date_of_birth, membership) VALUES ('$lastName', '$firstName', '$phone', '$dob', 'T')";

if (mysql_query($query)){
    echo "Thanks";
} else
{
    echo "Failed to insert customer into database";
}

mysql_close();

?>
Also, am I passing the variables correctly in addCustomerFunc?
Am I using "Post" correctly?
Thanks!
Edit: I am now having issues with passing the variables because it says:
Undefined index: add_LN in C:\Users\ej\Desktop\cpsc471\presentation\addCustomer.php
It adds blanks to my database though.
Answer: Since you are using a form, you need to make sure that you capture the submit event before it is allowed to proceed and refresh the page.
This should do the trick.  (notice the return false)
<input type="submit" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(add_LN, add_FN, add_PN, add_DOB); return false;"/>

Comment: THANKS! :D it stays on the same pagee!!!!
... But now im having another issue:
Undefined index: add_LN in C:\Users\ej\Desktop\cpsc471\presentation\addCustomer.php

Also, it adds blank elements to my database even though I filled in the field form.

Should I create a new question for this because if I mark your answer as correct people will ignore this after?
Comment: to remove that 'Undefined index' change the 'ERROR_REPORTING' if php.ini file
Comment: Undefined index is a warning; this in particular case, it means the last name field is empty. You can use the isset() function to check if the index exists before using it, or do like most people and ignore that error since it's quite minor (see ERROR_REPORTING or ERROR_LEVEL). Of course, if the last name is required, you need to have some data validation in there (i.e. check that the last name field isn't blank) before you put anything in the db.
Answer: Change the type of your button from:
submit to button
That way you stay on 1 page and the refresh will be gone :-)
Answer: why not to use jQuery $.post() or $.get()?
btw, use:
<input type="button" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(add_LN, add_FN, add_PN, add_DOB)"/>

instead of :
<input type="submit" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(add_LN, add_FN, add_PN, add_DOB)"/>


Transitivity on a set

Question: I have a question about equivalence relationships.
I know that a relationship is an equivalence relationship when the reflexive property, symmetric property and the transitive property exists on a set.
Ok...! ;-)
I have the following set
A = {0, 1, 2, 3, 4, 5, 6}

With the following relationship:
R = {(3, 3),(4, 0),(0, 0),(0, 4),(1, 1),(1, 6),(6, 1),(2, 2),(4, 4),(5, 5),(6, 6),(5, 3),(3, 5)}

With this data, we can draw the digraph:
Digraph
Then it's easy to check that the transitive property isn't exist in this case.
is it true?
and.., if is it true the question is... what kind of relationship is?
Answer: It is an equivalence relation, since it is reflexive, symmetric and transitive. If you think that it is not transitive, then please provide an example of elements $a,b,c\in\{0,1,2,3,4,5,6\}$ such that $(a,b),(b,c)\in R$, but $(a,c)\notin R$.
Comment: Hi! Thanks for your fast answer! did you see the digraph? I think that is reflexive, symmetric but... I can't "see" the transitive property. Why do you say it is transitive?
Comment: I mean, I didn't find any relationship where $${\displaystyle \forall a,b,c\in A:\quad aRb\quad \land \quad bRc\longrightarrow \quad aRc}$$ Then, this particular case, is not an equivalence relationship, but I am not pretty sure...
Comment: I wrote in my answer that is *is* an equivalence relation. Do you think that I am going to change my mind all of a sudden? Besides, I have no clue about the reason you think otherwise. Not no mention that I have no idea about the meaning of the first sentence from your previous comment.
Answer: If you add for example (1, 0) and (0, 1) to R then it won't be transitive since (0, 1), (1, 6) ∈ R but (0, 6) ∉ R. That way it will not be an equivalence relation.
Comment: That's true, but, with the current digraph posted in my question, the set doesn't satisfy the transitivity property, really?
Comment: Your diagraph visually satisfies transitive property.  Remember that elements a, b and c in the definition of transitive property doesn't need to be different.

Transitivity on a set


C++ write to csv, performance

Question: This is probably a simple question, but I have not been able to find specific information on this, or atleast information in a readable format. Most of the information I have found relates to reading data from a .csv.
I am have a function that have to save data to a .csv file. This is not an ideal format in a performance perspective, but let us assume that this cannot change. My data is stored in a r x c x s data structure and has to be outputed in the form r,c,s,value and saved to the .csv. At the moment i have:
char delimiter = ',';
ofstream ofs(file, ofstream::out);

for (int r = 0; r < P.n_rows; r++)
{
    for (int c = 0; c < P.n_cols; c++)
    {
        for (int s = 0; s < P.n_slices; s++)
        {
            ofs << r + 1 << delimiter << c + 1 << delimiter << s + 1 << delimiter << P(c, s, s) << endl;
            count++;
        }
    }
}
ofs.close();

For a data structure of size 100 x 100 x 50 this take roughly 6 sec, which I fell is an necessary long time. I would much appreciate if you could provide some information on how to speed this up.
Comment: [First up, ditch `endl`. You really don't need to flush every line.](https://kuhllib.com/2012/01/14/stop-excessive-use-of-stdendl/)
Comment: There must be a lot of csv writer libraries out there. Did you try any if it gave you better results? How big is `P`'s share of the runtime?
Comment: No I have not tried any csv libraries yet, but I will check it out to see if i find anything usefull.
Comment: Is this typo: `P(c, s, s)` (slice used twice) only in this post or in your code too?
Comment: Ohh I see, no it is just a type in the example above. It should be `P(r, c, s)`.
Answer: You should note that endl is more than a newline - it actually flushes data to the disk.

Inserts a newline character into the output sequence os and flushes it as if by calling os.put(os.widen('\n')) followed by os.flush().

This might slow down things considerably. You should try replacing it with a newline.
Comment: To go deeper why it's so much faster: flushing means sending data immediately to stream target, but writing to harddrives is VERY slow, so filestreams use buffering to collect data in RAM first and push bigger chunks fewer times. These chunks are even adjusted to sizes which are good for the harddrive. And even clearing a buffer may cost a bit. It's best to let the stream flush itself with full buffer and on close. Standard streams flush always because the user expects too see the data without further waiting.
Comment: Thanks for the reply, replacing `endl` with `\n` at the end helped to improve the prerformane in this specific case by a roughly 50-60 %.
Comment: You're very welcome. Good luck on making it even faster!
Answer: As was stated (and accepted) above, dropping endl reduces the time by 50-60% (in my case - from 7 seconds to 2 seconds, more than 70%).
However, there is still room for improvement: the general stream formatting. The following code further reduces run time by another 75%, to 500 ms:
int a[100][100][50];
int main(int argc, char** argv)
{
    char buff[64];
    memset(a, 1, 100 * 100 * 50 * sizeof(int));
    int count(0);
    char delimiter = ',';
    auto start = std::chrono::steady_clock::now();
    std::ofstream ofs("test.csv", std::ofstream::out);
    for (int r = 0; r < 100; r++)
    {
        for (int c = 0; c < 100; c++)
        {
            for (int s = 0; s < 50; s++)
            {
                sprintf_s(buff, "%d,%d,%d,%d\n", r, c, s, a[c][r][s]);
                ofs << buff;
                //ofs << r + 1 << delimiter << c + 1 << delimiter << s + 1 << delimiter << a[c][r][s] << '\n';
                count++;
            }
        }
    }
    ofs.close();
    auto end = std::chrono::steady_clock::now();
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << endl;
    return count;
}

Answer: Write to a stringstream first, then write it entirely to the output file:
char delimiter = ',';
stringstream ss;

for (int r = 0; r < P.n_rows; r++)
{
    for (int c = 0; c < P.n_cols; c++)
    {
        for (int s = 0; s < P.n_slices; s++)
        {
            ss << r + 1 << delimiter << c + 1 << delimiter << s + 1 << delimiter << P(r, c, s) << endl;
        }
    }
}
ofstream ofs(file, ofstream::out);
ofs << ss.str();
ofs.close();

Actually replacing endl with "\n" solves the problem already:
char delimiter = ',';
ofstream ofs(file, ofstream::out);

for (int r = 0; r < P.n_rows; r++)
{
    for (int c = 0; c < P.n_cols; c++)
    {
        for (int s = 0; s < P.n_slices; s++)
        {
            ofs << r + 1 << delimiter << c + 1 << delimiter << s + 1 << delimiter << P(r, c, s) << "\n";
        }
    }
}
ofs.close();

This gives about 2X speed up on my machine:
string delimiter = ",";
string ss;
ss.reserve(P.n_rows * P.n_cols * P.n_slices * 20);

int max_idx = max(P.n_rows, max(P.n_cols, P.n_slices));
vector<string> idx_str(max_idx);
for(int i=0;i<max_idx;++i) idx_str[i] = std::to_string(i+1);

for (int r = 0; r < P.n_rows; r++)
{
  auto& rstr = idx_str[r];
    for (int c = 0; c < P.n_cols; c++)
    {
      auto& cstr = idx_str[c];
      string thisline = rstr + delimiter + cstr + delimiter;

        for (int s = 0; s < P.n_slices; s++)
        {
          auto& sstr = idx_str[s];
            ss += thisline
                + sstr + delimiter
                + std::to_string(data[r][c][s]) + "\n";
        }
    }
}
ofstream ofs(file, ofstream::out);
ofs.write(ss.c_str(), sizeof(char)*ss.size());
ofs.close();

Comment: But the flushing is the problem in the first place! (Probably)
Comment: What makes you think this would be faster? (Not necessarily disagreeing, but a good answer should explain, and include some evidence). There's a lot more allocation overhead. Also, why are you using `std::stringstream` not `std::ostringstream`, and why are you flushing it, that does nothing useful at all?
Comment: Basically just aggregating lots of disk IO operations into a single one. In my experience, could be 5X to 10X faster.
Comment: A good point, which should be part of the answer, not a comment. And also, solved much more simply by simply removing the superfluous flushes in the original.
Comment: flushing is just to keep the same output format as the original code.
Comment: You are right... Replace endl with "\n" solves the problem already. I've also tried mmap before, could be slightly faster.
Comment: I tried this, but with only limited success. Im not sure why, since this seems like a reasonable argument.
Comment: Well, replacing endl with "\n" is close to the limit of this approach (using c++ streams). If you need even faster speed, you may want to consider messing with raw buffer directly, if you don't have to stick with csv file format.

C++ write to csv, performance


How to call asynchronous function conditionally in JavaScript

Question: I would like to make a conditional asynchronous function call in JavaScript/ES5.
Say I have a module myService with a function myAsyncFunction, which is doing some logic inside and returns result with a callback like the following:
myService.myAsyncFunction(param1, param2, (error, success) => {
    if (error) {
        // handle error
        return;
    }
    // handle success
});

But in certain conditions, I would like it to directly fall into the "handle success" without running the logic inside myService.myAsyncFunction, which may take some time and resources.
The most straightforward way that I imagine is to create a dummy function which imitates myService.myAsyncFunction and calls the callback with no error like the following:
var myGenericFunction;

if ( /* I would like to skip unnecessary logic */) {
    myGenericFunction = function (param1, param2, callback) {
        callback(null);
    }
} else {
    myGenericFunction = myService.myAsyncFunction;
}

myGenericFunction(param1, param2, (error, success) => {
    if (error) {
        // handle error
        return;
    }
    // handle success
});

This is what I have ended up with but I guess there should be a better way in terms of both syntax and performance. Do you have any suggestion for it?
Note: Promise.resolve on ES6 can propose a solution to directly fall into success case but I wonder what would be ES5 solution.
Comment: I don't understand, do you want to use Promises or not?  You have ~2 choices: use callbacks or use Promises (and maybe async/await).  Please pick one.  No matter what, if you choose an asynchronous approach, you have to be async the whole way down.
Comment: @zero298 No, I don't want to use Promise. I mentioned it since I would like you to know that I know 'Promise' way but prefer ES5 solution.
Comment: So, I removed 'Promise' from the title of the question in order not to cause confusion if that was the reason for the downvote.
Answer: First, it's super easy to pull in a Promise polyfill for javascript environments that don't support it. It should work all the way back to Ecmascript 3.
Ignoring that, if you have an async function such as this:
function myAsync(arg1, callback) {

}

But for some value or arg1 you want to immediately call the callback, but you can't change the function, the reasonable way to do this, is indeed to just wrap it:
function myAsyncWrapped(arg1, callback) {
   if (arg1 === SOME_VALUES) {
     callback(null, true);
   } else {
     myAsync(arg1, callback);
   }
}

If you need to do this for many async functions, it could be generalized as:
function asyncWrapper(innerFunction) {
   return (arg1, callback) {
     if (arg1 === SOME_VALUES) {
       callback(null, true);
     } else {
       innerFunction(arg1, callback);
     }
   }
}

so you can call this as:
myAsyncWrapped = asyncWrapper(myAsync);

Answer: I can think of a couple of alternate approaches to rolling your own Promise implementation.
You can use an existing one, such as Bluebird - http://bluebirdjs.com/docs/getting-started.html
You can also use a transpiler, such as Babel, which would allow you to write all of the next-gen ES that you wish and have it converted to ES5 pre-runtime - https://babeljs.io/docs/en
Comment: Thanks for suggestions @mattezell. I still look for non-Promise solution anyway.
Comment: No problemo.

I think that there may be a bit of confusion introduced to the question by the statement 'So you can summarize the question as "How can I use Promise.resolve on ES5?" if a better/different method is not available.'.

I think the assumption is "of course there's async because it's JavaScript" - and I think another widely held assumption is "using promises is cleaner/better than rolling your own alternative async implementation that behaves like promises".
Comment: Ok, I have also updated the note to remove "summary of question" stuff as the question is all on the title.


What does it mean for netstat -t

Question: I see the help of netstat -t option says Displays the current connection offload state.
And I tried it in the windows command console.
The Offload State value display InHost.
What does Offload means to netstat?
Thanks.
Answer: It refers to the TCP Chimney Offload feature which you can enable and disable on a network adapter. InHost means that the feature is disabled and offloading is not occuring. Offload means that TCP Chimney Offload is enabled on the NIC on which that connection is established.

TCP Chimney Offload overview
TCP Chimney Offload is a networking
  technology that helps transfer the workload from the CPU to a network
  adapter during network data transfer. In Windows Server 2008, TCP
  Chimney Offload enables the Windows networking subsystem to offload
  the processing of a TCP/IP connection to a network adapter that
  includes special support for TCP/IP offload processing.

To view the current status:

What does it mean for netstat -t


OnSave event on a custom entity form in CRM 2015 enters in a recursive trigger

Question: I have an event registered onSave on form of a custom entity on a crm 2015 on premise organization, which seems to enter in a recursive call. After i hit save the event keeps call it self, until i kill IE through task manager or the IE crashes with memory stack full. I have checked and it doesn't seem to be related to what i have in the function, meaning i am not triggering in any way the on save event again.
Here is the function code:
function contract_OnSave(context) {

    var eventArgs = context.getEventArgs();
    if (eventArgs.getSaveMode() == 1) {

        if (Xrm.Page.getAttribute("new_field").getValue() != null) {
            var mort = Xrm.Page.getAttribute("new_field").getValue();

            if (mort == true) {

                Xrm.Utility.confirmDialog("Would you like to open a new window for a Case?",
                    saveFormOnOpenCase,
                    callback)
            }
        }
    }
}
function saveFormOnOpenCase()
{
    Xrm.Page.data.save();
    var parameters = {};
    parameters["new_subjectnew"] = 100000002;
    // Open the window.
    var windowOptions = {
        openInNewWindow: true
    };
    Xrm.Utility.openEntityForm("incident", null, parameters, windowOptions);
}
function callback() {

}

It seemed to me like it is a bug in CRM 2015, after which i checked the version of the CRM, and noticed it is the RTM version. I suggested a CRM upgrade to Update 1 and it seemed as the most logical step to take. But even after the upgrade, the issue is still there.
I tried registering the same function on OnChange event on the field, and it was working fine, except the openInNewWindow option, of the openEntityForm function, which does not open a new window, but that is a different issue.
I am using IE 11.
Can someone please confirm if this is happening in another CRM 2015 environment as well, or if i am missing something?
Answer: In the OnSave event you ask the user for confirmation:
Xrm.Utility.confirmDialog("Would you like to open a new window for a Case?",
                saveFormOnOpenCase,
                callback)

When the user selects OK, this dialog executes function saveFormOnOpenCase. The first instruction this function executes is:
Xrm.Page.data.save();

... triggering the save event again.
Comment: Thank you for the answer, it has given me a clue. I'd forgotten to remove that Xrm.Page.data.save(); save form line, which i put after i put the event on change of the field. I will check again.
Comment: The actual problem is that,

Xrm.Utility.openEntityForm("incident", null, parameters, windowOptions);

opens the case record form in the same window, instead of a new one, as noted in MSDN online,

which actually, i believe, triggers the auto-save on the form, which causes the recursion on the event.So i will need to use pure JavaScript code to open the new window, instead of the sdk code (which does not work as intended), or prevent the default event if i can...
Answer: The actual problem which makes the OnSave function go into a recursive call is in the following:
var windowOptions = {
    openInNewWindow: true
};
Xrm.Utility.openEntityForm("incident", null, parameters, windowOptions);

As mentioned in the link
https://msdn.microsoft.com/en-us/library/jj602956.aspx.
The windowOptions parameter in the openEntityForm should open the new form in a new window but for some reason it doesn't, although the CRM is updated with update 1 for CRM 2015. Because the form doesn't open in a new window, when leaving the current form, an auto-save event is fired which causes the form to enter the recursion and initiate the OnSave event.
I fixed the issue by using pure a javascript function (not using the CRM SDK) to open the form in a new window, which doesn't initiate the OnSave event.


Sort by name in batch file

Question: I have a list of .sql files named by version
EG:
22.214.171.124.sql
126.96.36.199.sql
18.104.22.168.sql
184.108.40.206.sql
But when looping over them using:
for %%i in (*.sql) do (
    echo %%i
)

They come out in the following order:
184.108.40.206.sql
188.8.131.52.sql
188.8.131.52.sql
220.127.116.11.sql
Is there any way to get them to come out in the order listed first, which is like the windows file system? Without having to rename all the files.
Comment: First order is clearly not "correct" order - lexically 5.10 is less than 5.2.
Comment: Reworded the question to reflect this.
Answer: I managed to sort the problem by sorting the files by creation date.
for /f %%i in ('dir /b /o:d *.sql') do (
    echo %%i
)

Comment: Only works because your releases-of-interest increase the numbers in the customary manner. It would break if you had a maintenance release of an earlier version (say 184.108.40.206.sql) appearing after the next main release.
Answer: If you ever find yourself in a position where neither file creation date nor file last modified date help, then you could use my JSORT.BAT utility. It is a hybrid JScript/batch script that has an /N option that sorts numbers numerically instead of lexically. The utility is pure script that runs natively on any Windows machine from XP onward.
for /f %%F in ('dir /b /a-d *.sql^|jsort /n') do (
  echo %%F
)

Sort by name in batch file


Should I choose MyISAM for notification tables?

Question: Using Drupal6 with a MariaDB 10 InnoDB engine for all tables, I have noticed that insert queries into notifications_* tables are very inefficient on InnoDB tables. For example, these frequently in my slow_query_log:
INSERT INTO notifications_event (module, type, action, oid, uid, params, created, counter)
VALUES ('XXX', 'XXX', 'XXX', XXX, XXX, 'XXX', XXX, XXX);

INSERT INTO notifications_fields (sid, field, value, intval)
VALUES(XXX, 'XXX', 'XXX', XXX);

QUESTIONS

Would be a good idea to convert such tables into MyISAM?
Any downside for using such mix of storage engines?
Is there a better solution to shorten the insert time?
Comment: Is your MariaDB config optimal?  What does mysqltuner say?
Comment: Yes, the configuration is close to optimal. There is plenty of ram and Mysqltuner does not much to complaint.
Answer: DISCLAIMER : Pure MySQL Perspective
IMHO, you should leave it as InnoDB. MyISAM is not good in high-write situations. Why ?
LOCKING GRANULARITY
Each time you issue an INSERT, UPDATE, or DELETE query on a MyISAM table

The query performs a full table lock
All other queries needing to do the INSERT, UPDATE, or DELETE queries are handled on a first-come first-serve basis

InnoDB has row-level locking. In order to accommodate this, InnoDB caches INSERTs, UPDATEs, and DELETEs in the Buffer Pool, Log buffer, and Undo Logs.
DATA CACHING
MyISAM caches index pages from each MyISAM table's .MYI file in the key buffer.
MyISAM does not cache data. It has to piggyback from the OS for that. You can change the row format of the MyISAM to increase read speed from disk at the expense of double the table's disk usage. Write speed may be adjusted with concurrent INSERTs.
InnoDB caches data and indexes pages in the InnoDB Buffer Pool. You may need a decent size to accommodate data and indexes.
EPILOGUE
You just have to tune some vital settings. Here are some of my past suggestions from the DBA StackExchange.
In favor of InnoDB

Jan 11, 2013 : Very poor insert performance MySQL / Percona Server
Dec 06, 2012 : InnoDB insertion faster
Aug 03, 2012 : Slow insert performance in mysql innodb table for solr indexing
Jul 04, 2012 : Capabilities of InnoDB INSERT Performance

In favor of MyISAM

May 03, 2012 : Which is faster, InnoDB or MyISAM?
Jan 03, 2012 : Optimized my.cnf for high-end and busy server
Sep 20, 2011 : Best of MyISAM and InnoDB
Aug 12, 2011 : Which DBMS is good for super-fast reads and a simple data structure?

Although I lean towards InnoDB, I will leave it to you to decide which storage engine you want to use and tune for.
Answer: Using SQL database for everything is well known Drupal problem. A lot of data, like notifications and watchdog, does not need SQL anyway, they are just a key-value pairs.
InnoDB uses a much more sophisticated locking strategy. It's perfect for tables you often read and write at the same time, but slower for any single task. MyISAM is simpler, and way faster if you only add occasional and seldom try to read at the same time. I.e. you write to it at night and read it all day.
Joins between different engines are slow, keep that in mind.
If feasible, move Notifications' table to nosql solution. RAM-based if you have a plenty of RAM, just mirrored to disk. It might need hacking the very module, though, so it might end up next to impossible.
Comment: Would you please elaborate on how to move notification tables to nosql? I have plenty of unused ram and already use memcached, but in a indiscriminate way.
Comment: If all, or at least most, calls to a database are set and receive by key, you can use [MemcacheDB](http://memcachedb.org/) - Memcache's fork/sister that uses RAM for operations, but synchronizes with HDD so it's persistent. You write and read as if it was plain memcached.

It should be relatively easy to find all db_select and db_merge. If there's only condition on ID, you're good. As I told - looks like module hacking.
Comment: I'd love to use MemcacheDB, but I'm not a developer. So appreciate any ready-made solution
Comment: @alfish - sad point is, there are no ready made sql to nosql connectors. SQL layer is the expensive part and any connector would need to implement it. Good point is, if there are no joins, php developer should be able to hack it together in about one day, two with testing. For watchdog it'd be easier, there is a backend support using hooks.
Comment: I inquired memsql and they said ' 256GB subscription based license is $35000 USD'. This takes this option out of my scope. I don't want to bond the future of my database to such an expensive solution :)
Comment: @alfish well, sorry to hear that. MemSQL was only no-coding drop-in replacement that I've seen it might help. Other RAM based storages uses their own API for optimal results, or like VoltDB are paid-only licences.

In a moments like this I wish Drupal's abstraction was "data abstraction", not "database abstraction". Now each module needs to take care on it's own, no way to write universal handler for nosql solutions.

About bonding - as long as you stay api- and sql-compatible with MySQL, you are not bounded to anything, really. But I get your point.
Answer: You say you have plenty of RAM. If you feel like risking a bit, try MemSQL. It removes main bottleneck - HDD - from the equation. It also claims to be lock-free and optimized for frequent inserts.
And it's developers says transitioning from MySQL is easy as it should be drop-in API-compatible replacement.
Idea taken from syedrakib's question.
Comment: Does any Drupaler actually use MemSQL? Does it work as a drop-in integration? I had downloaded and tried but had difficulty to import sql dump.
Comment: @alfish - what difficulty, exactly? I played with it a bit (curiosity) and if I remove all ENGINE declarations and intro / outro sections from my dumps it seems to swallow them pretty all right. Did not connected Drupal to it... yet! ;)
Comment: While tryin to import a drupal dump, I got 'ERROR 1153 (08S01) at line 8747: Got a packet bigger than 'max_allowed_packet' bytes
' error several times. Despite the fact that I adjusted the parameter to be bigger than the dump file's size. Also I see plenty of 'WARN: Feature...unsupported'  in memsqlbin/data/memsql_tracelog
Comment: @alfish it looks like your error about max_allowed_bytes comes from client, you need to set both to a common value. I only encountered unsupported features in intro and outro blocks, but that's only a warnings and shouldn't hurt. Again - maybe it was just my luck, I don't have Drupal on it yet.


plot level lines (2 circumferences) on a 3d plot

Question: I'm trying to plot the two circumferences with dimensions xy together with the 3d plot and colour the intersection of the two circles, how can I do that?
<code># objective function
x <- seq(-1,1,.1)
y <- seq(-1,1,.1)
z <- x^2 + y^2

library(scatterplot3d)
library(plotrix)
scatterplot3d(x,y,z,pch=19,color="royalblue4")
draw.circle (1,1,1)
draw.circle (1,-1,1)
</code>
Comment: If you want to plot the two `draw.circle`s with the `scatterplot3d`, I don't think you'll succeed, because `draw.circle` "sees" 2D (if you call `axis(1)`, you'll see where the two `draw.circle`s where plotted). I'm not sure, though, of what you're trying to achieve. You want to plot 2 circles in 3D? What is the connection between the call to `scatterplot3d(x, y....` and the two last circles you want to plot?
Answer: I'm not really into mathematic stuff, but I'll post as answer because it might be of use and, also, is too big for a comment. Excuse any ignorance of mine, though, if I post nonsense.
<code>#your data
library(scatterplot3d)
x <- seq(-1,1,.1)
y <- seq(-1,1,.1)
z <- x^2 + y^2

ang = 60 #angle of the 3D plot. experiment with different values

#your 3D plot, with extended xx', yy' limits
sp3d <- scatterplot3d(x, y, z, pch=19, color="royalblue4",
           xlim = c(-1, 3), ylim = c(-3, 3), angle = ang)

#to use parametric equations of circles
f <- seq(-2*pi, 2*pi, 0.1)

#circle1
sp3d$points(x = 1 + 1*cos(f), y = 1 + 1*sin(f), z = rep(0, length(f)), type = "l")
#circle2
sp3d$points(x = 1 + 1*cos(f), y = -1 + 1*sin(f), z = rep(0, length(f)), type = "l")
</code>
The plot is:


Taking two actions in monit

Question: My monit script works to detect an outage with a process and inform me when the rule is:
  IF DOES NOT EXIST THEN ALERT

My monit script works to detect an outage and automatically fix it when the rule is:
  IF DOES NOT EXIST THEN START

But, what I want it to do is inform me AND fix it.
Two rules in a row seems to make it ignore all but the last:
  IF DOES NOT EXIST THEN ALERT
  IF DOES NOT EXIST THEN START
  # No alert given.

I could use a custom script that does both, and
  IF DOES NOT EXIST THEN EXEC "my_handwritten_script"

but I was trying to move away from a mess of hand-written scripts towards a clean Monit configuration.
Can I configure Monit to take two actions?
Comment: monit should alert anyway when it starts something.
Comment: Please show your Monit config...
Answer: Monit should send you an email by default when it takes an action.
Does not exist Service atd

    Date:        Thu, 03 May 2012 14:37:19 -0400
    Action:      restart
    Host:        abc.xyzgroup.com
    Description: process is not running

Your faithful employee,
Monit

My Monit config for this service:
check process atd
        with pidfile "/var/run/atd.pid"
        start program = "/sbin/service atd start"
        stop program = "/sbin/service atd stop"
        if 10 restarts within 11 cycles then timeout
        if cpu usage > 95% for 11 cycles then restart

Comment: The problem *appears to be that Gmail changed its mind, and no longer likes "monit@server1" as the FROM address. Alerts (which had been getting through) stopped when I made unrelated changes to the monit config. I went and researched the MAIL-FORMAT setting, changed the FROM address, and the alerts are now getting through again.
Comment: I've been following this up since @Mike's comment. Looks like ALL outgoing emails are being lost since I last tested it with an alert... so monit's not to blame. Still checking what's really going on. Sigh, if only I had a monitoring tool to check for this... :-)

Taking two actions in monit


Local isomorphism question in logics

Question: The definition of a local isomorphism between structures:

a local isomorphism between structures $\mathcal{A}$ and $\mathcal{B}$ over an alphabet $L$ is a finite relation  $$\{ (a_1,b_1),...,(a_n,b_n)\} \subset A \times B $$
  such that the simple extensions $(\mathcal{A},a_1,...,a_n)$ and $(\mathcal{B},b_1,...,b_n)$ are elementary equivalent.

In a Ehrenfeucht-Fraïssé game the goal of the duplicator is to make a local isomorphism. Between $\mathcal{A}$ and $\mathcal{B}$.
A theorem of these games is that  $$\mathcal{A} \equiv^n \mathcal{B} \Leftrightarrow \mathcal{A} \sim_n \mathcal{B} $$
We have if $\mathcal{A} \sim_n \mathcal{B}$, then $(\mathcal{A},a_1,...,a_n)$ and $(\mathcal{B},b_1,...,b_n)$ are elementary equivalent.
But doesn't this mean that also $\mathcal{A}$ and $\mathcal{B}$ are equivalent and so the previous theorem doesn't say anything in the $\Leftarrow$ direction?
Comment: Where did you find this definition of local isomorphism? The usual definition is that $(\mathcal{A},a_1,\dots,a_n)$ and $(\mathcal{B},b_1,\dots,b_n)$ satisfy the same _quantifier-free_ sentences.
Comment: Yeah thanks, apparently my book gives the wrong definition.
Answer: Long Comment
It seems to me that the symbolism for local isomorphism is not so "stable"; thus, I'm not sure about the equivalences...
I'll refer to :

Jouko Vaananen, Models and Games (2011), page 81-on.

We have :

$\mathcal{A} \equiv \mathcal{B}$, i.e. elementary equivalence, when the two structures satisfy the same FO sentences,

and :

$\mathcal{A} \equiv_n \mathcal{B}$, when they satisfy the same FO sentences of quantifier rank $\le n$.

Next we have [page 69] :

$\mathcal{A} \sim^n_p \mathcal{B}$ if there is a back-and-forth sequence of length $n$ for $\mathcal{A}$ and $\mathcal{B}$.

Some results :

page 70 :

Proposition 5.27 The following are equivalent:

$\mathcal{A} \sim^n_p \mathcal{B}$
II [Duplicator] has a winning strategy in $\text{EF}_n(\mathcal{A},\mathcal{B})$.

page 81 :

Proposition 6.4 Suppose $L$ is an arbitrary vocabulary. Suppose $\mathcal{A}$ and $\mathcal{B}$ are $L$-structures and $n \in \mathbb N$. Consider the conditions:

(i) $\mathcal{A} \equiv_n \mathcal{B}$
(ii) $\mathcal{A}\upharpoonright_{L'} \sim^n_p \mathcal{B}\upharpoonright_{L'}$ , for all finite $L' \subseteq L$.

We have always (ii) $\Rightarrow$ (i) and if $L$ has no function symbols, then (ii) $\Leftrightarrow$ (i).

Thus, "cooking all together", It seems to me that :

if $\mathcal{A} \sim^n_p \mathcal{B}$, for all $n$, then $\mathcal{A} \equiv \mathcal{B}$, i.e. $\mathcal{A}$ and $\mathcal{B}$ are elementary equivalent.

Local isomorphism question in logics


SaltStack: Getting Up and Running Minion on EC2

Question: I am working through the SaltStack walk through to set up salt on my ec2 cluster.  I just edited /etc/salt/minion and added the public dns of my salt master.
master: ec2-54-201-153-192.us-west-2.compute.amazonaws.com

Then I restarted the minion.  In debug mode, this put out the following
$ sudo salt-minion -l debug
[DEBUG   ] Reading configuration from /etc/salt/minion
[INFO    ] Using cached minion ID: localhost.localdomain
[DEBUG   ] loading log_handlers in ['/var/cache/salt/minion/extmods/log_handlers', '/usr/lib/python2.6/site-packages/salt/log/handlers']
[DEBUG   ] Skipping /var/cache/salt/minion/extmods/log_handlers, it is not a directory
[DEBUG   ] None of the required configuration sections, 'logstash_udp_handler' and 'logstash_zmq_handler', were found the in the configuration. Not loading the Logstash logging handlers module.
[DEBUG   ] Configuration file path: /etc/salt/minion
[INFO    ] Setting up the Salt Minion "localhost.localdomain"
[DEBUG   ] Created pidfile: /var/run/salt-minion.pid
[DEBUG   ] Chowned pidfile: /var/run/salt-minion.pid to user: root
[DEBUG   ] Reading configuration from /etc/salt/minion
[DEBUG   ] loading grain in ['/var/cache/salt/minion/extmods/grains', '/usr/lib/python2.6/site-packages/salt/grains']
[DEBUG   ] Skipping /var/cache/salt/minion/extmods/grains, it is not a directory
[DEBUG   ] Attempting to authenticate with the Salt Master at 172.31.21.27
[DEBUG   ] Loaded minion key: /etc/salt/pki/minion/minion.pem
[DEBUG   ] Loaded minion key: /etc/salt/pki/minion/minion.pem

Sure enough, 172.31.21.27 is the private ip of the master.  So far this looks ok.  According to the walkthrough, the next step is to accept the minions key on the master:
"Now that the minion is started it will generate cryptographic keys and attempt to
connect to the master. The next step is to venture back to the master server and
accept the new minion's public key."

However, when I go to the master node and look for new keys I don't see any pending requests.
$ sudo salt-key -L
Accepted Keys:
Unaccepted Keys:
Rejected Keys:

And the ping test does not see the minion either:
$ sudo salt '*' test.ping

This is where Im stuck, what should I do next to get up and running?
Comment: *"The Salt Master needs to bind to 2 TCP network ports on the system, these ports are 4505 and 4506"* (from the walk-through)... are you allowing access to these in the security group?  If all machines are in the same security group, you should be able to specify the security group (sg-xxxxxxxx) as the "source" address instead of an IP address.  Also, you need to not have iptables or anything else blocking connections.
Comment: Ah, of course, I forgot to flush ip tables when I spun up my ops box.  Thanks very much, I just accepted the key for the first minion and the ping test passes.
Answer: Turn off iptables and do salt-key -L to check if the key shows up. If it does, then you need to open port 4505 and 4506 on the master for the minion to be able to connect to it. You could do lokkit -p tcp:4505 -p tcp:4506 to open these ports.
Answer: You likely need to add rules for 4505/4506 between the salt master and minion security group.  Salt master needs these ports to be able to communicate with the minions.


Solving Combinatorial Problem - Red Ball Distribution in Three Bins

Question: I'm reaching out to the community for help in tackling a combinatorial problem that I'm currently stuck on. The problem revolves around distributing twelve identical red balls into three bins, labeled $A$, $B$, and $C$, while satisfying specific conditions:

Bin $A$ must contain 1, 3, or 5 balls.
Bin $B$ must have at least two balls.
Bin $C$ must contain an odd number of balls.

The goal is to calculate the number of ways to achieve this distribution.
I've established that bin $B$ must have an even number of balls, as bins $A$ and $C$ are required to contain an odd number of balls to sum up to 12 (an even number).
We've begun exploring possibilities by considering different cases for bin $A$:

When bin $A$ contains 1 ball, we deduced that bin $B$ could range from 2 to 10 balls, and the corresponding number of balls in bin $C$ can be calculated accordingly.
However, we're uncertain about formulating a general formula to streamline this process, as brute-forcing through all possibilities could be time-consuming.
Answer: You can simplify the problem quite a bit by using the specific conditions that you have already deduced.  The trick is to think about pairs of balls, rather than individual balls.
Start by placing $1$ ball in bin A, $2$ balls in bin B, and $1$ ball in bin C.  There are $8$ balls remaining, meaning $4$ pairs of balls remaining.  And since you know the parity of each bin in a solution can't change, all you have to place is pairs of balls.
Count unconstrained solutions to $y_1+y_2+y_3=4$.  By stars and bars there are $\binom 64=15$ unconstrained solutions.
Of the $15$ unconstrained solutions, there are $2$ solutions that don't work because $y_1=3$ (corresponding to $7$ balls in bin A), and $1$ solution that doesn't work because $y_1=4$ (corresponding to $9$ balls in bin A).  Or to do this a little more systematically, identify the unacceptable solutions (those in which $y_1 \geq 3$) by placing $3$ more pairs in bin A and then counting solutions to $z_1+y_2+y_3=1$.  There are $3$ solutions to that equation so there are $3$ unacceptable solutions to the unconstrained equation.  There are, therefore, a total of $12$ solutions that satisfy your constraints.
Answer: You can simply make use of ordinary generating functions.

The generating function for bin A is : $$x^1+x^3+x^5=\frac{x-x^7}{1-x^2}$$

The generating function for bin B is : $$x^2+x^3+x^4+...=\frac{x^2}{1-x}$$

The generating function for bin C is : $$x^1+x^3+x^5+...=\frac{x}{1-x^2}$$

Realize that the exponentials represent the number of balls that the bins can contain. Then find the coefficient of $x^{12}$ in the expansion of $$\frac{x-x^7}{1-x^2}\frac{x^2}{1-x}\frac{x}{1-x^2}$$
,which is $12$
Answer: Place 1, 3, or 5 balls in box A, so you will have three cases. Next place 2 balls in box B so there are at least two there. Then distribute the remaining balls (9, 7, or 5 depending on the case) between boxes B and C such that box C receives an odd count.
To do that last bit, pull two boxes out of your hat, sort those odd remaining balls in those boxes.  As you noticed, one will contain an odd count and the other an even count, so just select which will mete the requirements to fill box B and C.
Answer: Plot and count the possibilities for the pair of numbers (Balls in A, Balls in C).


Using a eigenvector to figure out the kernel of a linear map

Question: We have the linear map $f:\mathbb{R}^4 \longrightarrow \mathbb{R}^4$ of wich matrix from a basis $B$ to a basis $B'$ is
$$A=M(f;B,B')=\begin{bmatrix}
1& 1& 0&0\\
-2&0&4&4\\
1&1&1&0\\
0&1&3&2 \end{bmatrix}$$
In the question we're told that $(2,-2,0,1)$ is an eigenvector of A, and asked to determine the kernel of A. We can just solve the homogenous system $Av=0_{\mathbb{R}^4}$, but I would like to know how I can use the eigenvector fact to arrive at the answer
Answer: $(2,-2,0,1)$ lies in the kernel of $A$ and the last three columns of $A$ are easily seen to be linearly independent. Therefore by the Rank–nullity theorem,which states that for a linear map $f:V \longrightarrow W, dim(V)=dim(\mathrm{im} f)+dim(\ker(f))$, the kernel of $A$ is the span of $(2,-2,0,1)$.


Can't use dollar ($) instead of parens

Question: I always try to get rid of parens where posible. Here is simple code:
groupElems :: Eq a => [a] -> [[a]]
groupElems [] = []
groupElems (x:xs) = (fst spaned) : groupElems (snd spaned) where
  spaned = span (== x) (x:xs)

It takes a list and returns list of grouped elems. Like this -
groupElems [1,2,2,2,4]
> [[1],[2,2,2],[4]]

And thas works fine. Now I try to replace parens with dollar:
groupElems :: Eq a => [a] -> [[a]]
groupElems [] = []
groupElems (x:xs) = (fst spaned) : groupElems $ snd spaned where
  spaned = span (== x) (x:xs)

But then I get strange (to me) error:
hello.hs:83:21: error:
    • Couldn't match expected type '[a] -> [[a]]'
                  with actual type '[[a]]'
    • The first argument of ($) takes one argument,
      but its type '[[a]]' has none
      In the expression: (fst spaned) : groupElems $ snd spaned
      In an equation for 'groupElems':
          groupElems (x : xs)
            = (fst spaned) : groupElems $ snd spaned
            where
                spaned = span (== x) (x : xs)

Is it possible to use dollar here?
Comment: Note that `(fst spaned)` doesn't need parentheses above. You can remove those parentheses without any `$`. For `snd spaned` instead, I don't think they can be removed easily.
Comment: Also, have to point this out every time: `$` has _nothing_ whatsoever to do with parentheses. You can _incidentally_ also use it to simplify expressions so they no longer require parentheses sometimes, but that's just because `$` is a low precedence operator.
Comment: @Cubic I know it, but nevertheless I try to get rid of parentheses throught this not trivial way, and this leads me to desperate attempts to put dollar almost everywhere.
Answer: You get the error because of operator precedences. $ has the lowest possible precedence, while function application has the highest possible. I.e. your original code:
(fst spaned) : groupElems (snd spaned)

is equivalent to:
(fst spaned) : (groupElems (snd spaned))

While your attempt to remove parens is grouped like:
((fst spaned) : groupElems) $ (snd spaned)

See also this question.
Answer: Redefine (or rather replace) spaned, and you don't need to call fst or snd, eliminating the parentheses and the need for ($).
groupElems :: Eq a => [a] -> [[a]]
groupElems [] = []
groupElems all@(x:_) = s1 : groupElems s2 where
  (s1, s2) = span (== x) all

Can't use dollar ($) instead of parens


Can different sessions of the same linux user have different local repos for the same public repo?

Question: We have a program with only one license on a linux machine, so we have to work with different sessions of the same user.
Is it still possible to have 2 working local repositories on that machine that push and pull to/from the same public repo?
Could there be any workaround to make that work?
Comment: Clone the remote repo to two different locations. Git is not centralised, so you can have as many local "copies" as you like.
Comment: Also, consider using `git worktree` to save yourself some effort. It's not clear whether the restrictions that `git worktree` adds would be any issue for you, but if not, it saves you disk usage and makes new commits in each worktree accessible from the other working tree (but not part of it, just view-able, cherry-pick-able, etc).
Answer: Everything about a git repository in Linux is stored remotely in the repository's directory, inside the hidden directory .git
This means you can have two directories that are repositories with their own history that can push/pull/fetch/etc without affecting the other one.
Let's say you have a public GitHub repository. You could create two directories:
mkdir workingDirOne
mkdir workingDirTwo
Then, clone the public repository into each one
cd workingDirOne
git clone (GitHub URL)
cd ..
cd workingDirTwo
git clone (GitHub URL)
In this case, both workingDirOne and workingDirTwo will have their own versions of the repository. Now two instances of the same user could modify one without affecting the other.


Auto-mapper mapping overwriting nested child entity properties

Question: I just started using Automapper and blocked with one particular behavior regards to nested child entities.
While updating the child objects of a parent object, Automapper is replacing the child object in destination object with a new instance created using data from Target data instead of copying property by property.  To better explain the problem, I have copied the test code below:
class Program
{
    static void Main(string[] args)
    {

        Mapper.CreateMap<PersonDTO, Person>();
        Mapper.CreateMap<UserLoginDTO, UserLogin>();

        Person pmodel = new Person()
        {
            Id = 10,
            FullName = "XYZ",
            LoginInfo = new UserLogin() { Id = 10, UserName = "xyz" }
        };

        PersonDTO pdto = new PersonDTO()
        {
            Id = 10,
            FullName = "XYZ",
            LoginInfo = new UserLoginDTO() { Id = 10, UserName = "abc" }
        };

        pmodel.Initiated();
        pmodel.LoginInfo.Initiated();
        Console.WriteLine("model => LoginInfo => IsInitiated : {0}", pmodel.LoginInfo.IsInitiated);
        Mapper.Map<PersonDTO, Person>(pdto, pmodel);
        Console.WriteLine("model => LoginInfo => IsInitiated : {0}", pmodel.LoginInfo.IsInitiated);

    }
}

public class PersonDTO
{
    public int Id
    {
        get;
        set;
    }

    public String FullName
    {
        get;
        set;
    }

    public UserLoginDTO LoginInfo { get; set; }
}

public class UserLoginDTO
{

    public int Id
    {
        get;
        set;
    }

    public String UserName
    {
        get;
        set;
    }
}

public class Person : BaseEntity
{
    private int _id;
    private string _name;
    private UserLogin _loginInfo;

    public int Id {
        get { return _id;  }
        set { _id= value; }
    }

    public String FullName
    {
        get { return _name; }
        set { _name = value; }
    }

    public UserLogin LoginInfo { get { return _loginInfo; } set { _loginInfo = value; } }

}

public class UserLogin : BaseEntity
{
    private int _id;
    private string _userName;

    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }

    public String UserName
    {
        get { return _userName; }
        set { _userName= value; }
    }
}

public abstract class BaseEntity
{
    private bool _isInitiated = false;

    public bool IsInitiated { get { return _isInitiated; } }

    public void Initiated()
    {
        this._isInitiated = true;

    }
}

Output of the above code is:
Before mapping : model => LoginInfo => IsInitiated : True
After mapping : model => LoginInfo => IsInitiated : False
My requirement is to retain the IsInitiated property after mapping.  Am I missing something here.
Comment: Check out any of the related questions in order to find a work-around.
Comment: I checked related posts and none of them are close to my problem.  My friend suggested a solution to ignore the nested objects and in AfterMap event process the nested objects.  I posted this question to find better solution from experts.
Comment: Ah, I misinterpreted some of the linked questions. Looking at the wiki for Automapper, it appears that the Ignore() method might accomplish what you're looking for. Something like Mapper.CreateMap().ForMember("IsInitiated", src => src.Ignore());
Comment: Thank you. Unfortunately this is not working.  From the behavior what I understand is, while updating the LoginInfo property in Person, AutoMapper, replacing this with a new instance of UserLogin object.
Comment: When I copy and run your exact code, I get model => LoginInfo => IsInitiated : True
model => LoginInfo => IsInitiated : True
Comment: However, from my testing, it appears that you can choose to ignore the mapping of the entire child object, or individual properties on that object.
Comment: I got it. I was using a old version of Automapper (1.x).  I verified the behavior using latest version and as mentioned by you, it is working as expected.  My bad, I missed this basic check.
Answer: I had the same issue. i ended up using the return value of that mapping method.

var result = Mapper.Map<PersonDTO, Person>(pdto, pmodel);

and added an explicit ignore for the properties i didnt want overwritten.

  Mapper.CreateMap<PersonDTO, Person>()
.ForMember(d => d.IsInitiated, op => op.Ignore());
Comment: This method is useful, when the source and destination objects have same property, but we want Automapper not to copy that specific property from source to destination.  In my case, the nested child object in destination was replaced by object from source instead of mapping that child object using a definition.  However that behavior seems to be defect in older version but fixed in latest version.


iOS view.hidden = NO not always working

Question: My viewController is receiving notifications from background threads and updates the UI accordingly.
Using this piece of code
dispatch_async(dispatch_get_main_queue(), ^{
    self.myButton.hidden = NO;
    [self.view addsubview:someView];
});

What I observe every now and then (2/5 times) is that myButton is not visible. someView have been added. If I press the area where myButton is it becomes visible.
What is the correct way of updating the UI like this when called from a background thread, I thought this was it but it is obviously not working as I expected.
Comment: You should take a look at where else you might be setting `self.myButton.hidden = YES;`
Comment: This code looks fine - that is the proper way to do it. I'd take a good look at other place where you set hidden to YES. Any UIKit messaging on other threads than the main one will cause all kinds of weird things to happen. Ask me how I know this to be a fact :-)
Comment: The button I'm having a problem with is starting out hidden in the nib. At one point in my code I set the hidden property to NO. At no other point is the hidden property altered. I don't think it is an issue of this not happening or it is being overridden somewhere else as the button is responding to touch. It is there, working just not properly redrawn. Also if I swipe my finger over the area it becomes visible too. If the property had been set to hidden somewhere the button would not become visible and it would not respond to touch.
Answer: There is not any issue with this code, You should check that you are not hiding it elsewhere.
So just search for other part or your code where you have code self.myButton.hidden
Comment: And `self.myButton setHidden:`, just in case.
Comment: ohk.. check that your button is not nil.
Comment: @yes , but according to his code, i thing he is implementing .hidden
Comment: I am having the same problem; I am setting it hidden in the code but when I do .hidden = YES, the buttom is not appearing

iOS view.hidden = NO not always working


Adding META tag to nodes with a specific topic

Question: If a node has a term reference to a specific term (say termid 123) I want to add this META tag to prevent search engine spiders from indexing the page:
<meta name="robots" content="noindex">

Is hook_page_build() the right hook for this?
Answer: I think you definitely could use hook_page_build() (it would work), but since that hook is really intended for use with page.tpl.php, and meta tags are outputted in html.tpl.php, I think it might not be the 'Drupal' way.
Another option might be to use hook_html_head_alter(), which allows you to alter the  tags before they're rendered:
function MYMODULE_html_head_alter(&$head_elements) {
  if ($node = menu_get_object()) {
    $tid = function_to_get_term_id();
    $related_tids = field_get_items('node', $node, 'field_FIELD_NAME');

    if (!empty($related_tids) && $related_tids[0]['tid'] == $tid) {
      $head_elements['MYMODULE_meta_robots'] = array(
        '#tag' => 'meta',
        '#type' => 'html_tag',
        '#attributes' => array(
          'name' => 'robots',
          'content' => 'noindex'
        )
      );
    }
  }
}

You might also want to look at adding rel="nofollow" attributes on the links to those node in teaser displays, it might help speed up the process a bit.
Comment: thanks Clive, that's awesome! I like the rel="nofollow" ideas as well!!!


How do I add a pager for the following code?

Question: On the page--product.tpl.php I use node_load() to get all the nodes of a content type. I want to shows 15 post titles per page. How do I add a pager to the page?
I am using the following code.
$terms = taxonomy_get_children(arg(2));
foreach ($terms as $term) {
  $whole = taxonomy_select_nodes($term->tid, TRUE, 1, array('t.created' => 'DESC'));
  foreach ($whole as $nid) {
    $node_this = node_load($nid);
    echo '
'.$node_this->title.'
';
  }
}

Comment: That is not code I would put in a template file.
Answer: I think you are trying to do a custom pager, not based on db query something like this, but it is D6 code.
For D7, check Extenders and what the Database Abstraction Layer page describes is

Most Drupal database SELECT queries are performed by a call to
  db_query() or db_query_range(). Module authors should also consider
  using the PagerDefault Extender for queries that return results that
  need to be presented on multiple pages

You may also need to check theme_pager which displays the pager result.
When calling theme('pager') outside a pager query, it's necessary to initialize the pager first with a call to pager_default_initialize.
Comment: I have deleted the comments, which are not for extended discussion. Please add the information that is useful to answer the question in the question.
Comment: [taxonomy_select_nodes()](http://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.module/function/taxonomy_select_nodes/7) is using the `PagerDefault` extender, and `db_select()`. That is surely not the issue.
Comment: @kiamlaluno, before I posted this answer, OP mentioned only the inner most loop `foreach ($whole as $nid)`. I added a comment that `taxonomy_select_nodes()` uses the `PagerDefault', but it was got deleted.
Comment: It's not your fault. This is what I would call a "moving target" question: a question that is changed basing on the feedback given from the other users. I added my comment to avoid future readers think that using `taxonomy_select_nodes()` doesn't work with `theme('pager')` because it is not using `PagerDefault`.
Answer: Execute the following code, and you should get a pager.
function mymodule_get_children($tid) {
  $query = db_select('taxonomy_term_data', 't');
  $query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
  $query->addField('t', 'tid');
  $query->condition('h.parent', $tid);
  $query->addTag('term_access');
  $query->orderBy('t.weight');
  $query->orderBy('t.name');
  $tids = $query->execute()->fetchCol();

  return $tids;
}

function mymodule_select_nodes(array $tids, $pager = TRUE, $limit = FALSE, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC')) {
  if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
    return array();
  }
  $query = db_select('taxonomy_index', 't');
  $query->addTag('node_access');
  $query->condition('tid', $tids);
  if ($pager) {
    $count_query = clone $query;
    $count_query->addExpression('COUNT(t.nid)');

    $query = $query->extend('PagerDefault');
    if ($limit !== FALSE) {
      $query = $query->limit($limit);
    }
    $query->setCountQuery($count_query);
  }
  else {
    if ($limit !== FALSE) {
      $query->range(0, $limit);
    }
  }
  $query->addField('t', 'nid');
  $query->addField('t', 'tid');
  foreach ($order as $field => $direction) {
    $query->orderBy($field, $direction);
    // ORDER BY fields need to be loaded too, assume they are in the form
    // table_alias.name
    list($table_alias, $name) = explode('.', $field);
    $query->addField($table_alias, $name);
  }
  return $query->execute()->fetchCol();
}

$tids = mymodule_get_children(arg(2));

if (!empty($tids)) {
  $nids = mymodule_select_nodes($tids, TRUE, 1, array('t.created' => 'DESC'));

  if (!empty($nids)) {
    $nodes = node_load_multiple($nids);
    foreach ($nodes as $node) {
      echo '
'.$node->title.'
';
    }
  }

  echo theme('pager');
}

In my test site, replacing arg(2) with an existing taxonomy term ID, I get the following output.

Appellatio Fere Huic Metuo

Pages

1

2

3

4

5

6

7

8

9

…

next ›

last »

I used my own functions for the following reasons:

taxonomy_get_children() returns an array of term objects while you are interested only in the taxonomy term IDs. My code gets directly the taxonomy term IDs.
taxonomy_select_nodes() accepts a single taxonomy term ID, while you need the nodes using one or more taxonomy terms. My code returns all the nodes using one or more taxonomy terms matching an array of taxonomy terms.

I also created the page--taxonomy.tpl.php file for the currently enabled theme, and the code works, when I visit the taxonomy page for a taxonomy term with child terms.

In my case, the taxonomy term doesn't have nodes using it, but it has child terms used in nodes.
Update
The problem is that both the code you want to add in the template file, and the code executed from Drupal are trying to do the same thing, and this causes a conflict between the two. As result, when a taxonomy term has nodes using it, the output is just the one from Drupal.

When there aren't nodes using the taxonomy term, the output is from the custom template file, and from Drupal.

Both the screenshots has been taken using your theme as default theme.
To avoid this, the code needs to be changed to the following one.
function mymodule_get_children($tid) {
  $query = db_select('taxonomy_term_data', 't');
  $query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
  $query->addField('t', 'tid');
  $query->condition('h.parent', $tid);
  $query->addTag('term_access');
  $query->orderBy('t.weight');
  $query->orderBy('t.name');
  $tids = $query->execute()->fetchCol();

  return $tids;
}

function mymodule_select_nodes(array $tids, $pager = TRUE, $limit = FALSE, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC')) {
  if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
    return array();
  }
  $query = db_select('taxonomy_index', 't');
  $query->addTag('node_access');
  $query->condition('tid', $tids);
  if ($pager) {
    $count_query = clone $query;
    $count_query->addExpression('COUNT(t.nid)');

    $query = $query->extend('PagerDefault');
    $query->element(10);
    if ($limit !== FALSE) {
      $query = $query->limit($limit);
    }
    $query->setCountQuery($count_query);
  }
  else {
    if ($limit !== FALSE) {
      $query->range(0, $limit);
    }
  }
  $query->addField('t', 'nid');
  $query->addField('t', 'tid');
  foreach ($order as $field => $direction) {
    $query->orderBy($field, $direction);
    // ORDER BY fields need to be loaded too, assume they are in the form
    // table_alias.name
    list($table_alias, $name) = explode('.', $field);
    $query->addField($table_alias, $name);
  }
  return $query->execute()->fetchCol();
}

$tids = mymodule_get_children(arg(2));

if (!empty($tids)) {
  $nids = mymodule_select_nodes($tids, TRUE, 1, array('t.created' => 'DESC'));

  if (!empty($nids)) {
    $nodes = node_load_multiple($nids);
    foreach ($nodes as $node) {
      echo '
'.$node->title.'
';
    }
  }

  echo theme('pager', array('element' => 10));
}

The difference is that now the code is setting the "element" property for the pager to 10. It should the conflict between the pagers, but it changes the value for the GET parameter "page" used from Drupal to identify which page of the pager should be shown, which become similar to "page=0,0,0,0,0,0,0,0,0,1"; when shown in the URL, it is probable the commas are encoded.
As I commented before, this is not the code I would put in a template file. If you want to change the output of a page, you change its page callback, which means implementing code similar to the following one. (The following is just partial code.)
function mymodule_menu(&$items) {
  if (isset($items['taxonomy/term/%taxonomy_term'])) {
    $items['taxonomy/term/%taxonomy_term']['page callback'] = 'mymodule_term_page';
  }
}

Even better, it would be using the Views module, and using a view for the taxonomy term page. In this way, it would be possible to alter the output without to write code, and without altering any template file.
In both the cases, Drupal would not be adding a pager while another pager is being added to the same page from a module.
As side note, you are putting in template files things that should be in a module, or that can be easily obtained changing settings for already enabled modules.


Apache Virtual Host by name

Question: Is it possible to replace "*" with a domain name or subdomain?
<VirtualHost *:443>
update
The problem is that I get this error on booting up apache:
[Mon Aug 16 13:42:48 2010] [warn] _default_ VirtualHost overlap on port 443, the first has precedence

I have a virtualHost on :443 for a subdomain and one for a primary domain.  When I remove the subdomain I no longer get that error.
as a side note, if this configuration can be more efficient, please let me know how
domain.com config
<VirtualHost *:80>
 ServerAdmin email@example.com
 ServerName  www.domain.com
 ServerAlias domain.com
 ServerAlias xx.xxx.xxx.xx

 # Directory Root.
 DocumentRoot /sites/domain.com/www/

 # Logfiles
 ErrorLog  /sites/domain.com/logs/error.log
 CustomLog /sites/domain.com/logs/access.log combined
</VirtualHost>

<VirtualHost *:443>
 ServerName  www.domain.com

 # Directory Root.
 DocumentRoot /sites/domain.com/www/

 # Enable SSL
 SSLEngine On
 SSLCertificateFile  /sites/domain.com/ssl/star_domain_com.crt
 SSLCertificateKeyFile /sites/domain.com/ssl/ikeyless.key
 SSLCertificateChainFile /sites/domain.com/ssl/DigiCertCA.crt
 SetEnvIf User-Agent ..*MSIE.*. nokeepalive ssl-unclean-shutdown
</VirtualHost>

support.domain.com config
<VirtualHost *:80>
 ServerName support.domain.com

 # Directory Root.
 DocumentRoot /sites/support.domain.com/www/

 # Logfiles
 ErrorLog  /sites/support.domain.com/logs/error.log
 CustomLog /sites/support.domain.com/logs/access.log combined
</VirtualHost>

<VirtualHost *:443>
 ServerName support.domain.com

 # Directory Root.
 DocumentRoot /sites/support.domain.com/www/

 # Logfiles
 ErrorLog  /sites/support.domain.com/logs/error.log
 CustomLog /sites/support.domain.com/logs/access.log combined

 # Enable SSL
 SSLEngine On
 SSLCertificateFile  /sites/domain.com/ssl/star_domain_com.crt
 SSLCertificateKeyFile /sites/domain.com/ssl/domain.key
 SSLCertificateChainFile /sites/domain.com/ssl/DigiCertCA.crt
 SetEnvIf User-Agent ..*MSIE.*. nokeepalive ssl-unclean-shutdown
</VirtualHost>

When I try to access support.domain.com it points to domain.com and won't load our support site when in https, it works fine in http.
Comment: Did my answer resolve the issue? If so, please mark is as the answer, or let me know what still doesn't work.
Comment: The general answer is yes, but there are some sub-cases.
Which type of vhosting are you trying to do?  IP, port, name, or mass name-based vhosting?
Answer: Yes, this is a very powerful part of apache's configuration.
For example, suppose that you are serving the domain www.domain.tld and you wish to add the virtual host www.otherdomain.tld, which points at the same IP address. Then you simply add the following to httpd.conf:
NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.domain.tld
    ServerAlias domain.tld *.domain.tld
    DocumentRoot /www/domain
</VirtualHost>

<VirtualHost *:80>
    ServerName www.otherdomain.tld
    DocumentRoot /www/otherdomain
</VirtualHost>

See full documentation here: http://httpd.apache.org/docs/2.2/vhosts/
The final answer:
Add the NameVirtualHost *:443 directive to your default config.
Comment: Why does nobody upvote the question? Please do!
Comment: The problem I'm having is that I'm defining a subdomain and primary domain to *:443 but the subdomain continues to forward to the primary domain on that port.
Comment: Can you post a copy of your conf file to the case? Replace any sensitive information, and explain the desired behavior.
Comment: It looks like you're missing the overall "NameVirtualHost" directive as well. See here: http://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost
Comment: I'm trying to find it, there's about 20 config files here for various domains and subdomains..... https works on our primary domain though, so wouldn't that mean that it's defined somewhere?
Comment: That is works is the _default_ kicking in, and it's not defined. Run a grep on all files for it, and if it's not there, simply add it to the default config.
Answer: You can specify a specific IP in place with the asterisk, as long as the IP is specified with NameVirtualHost.  The name is specified in ServerName and ServerAlias.
The asterisk is matching all IP addresses that Apache binds to in the VirtualHost.
Comment: (+1) Right on... straight to the point. The asterisk refers to the IP apache should listen on for that vhost. You can listen on all IPs apache is bound to (*) or a specific one.
Answer: Not for SSL:

You cannot use name based virtual
  hosts with SSL because the SSL
  handshake (when the browser accepts
  the secure Web server's certificate)
  occurs before the HTTP request, which
  identifies the appropriate name based
  virtual host. If you plan to use
  name-based virtual hosts, remember
  that they only work with your
  non-secure Web server.

Update:
Apparently latest web servers supports this.. Check the link provided by Warner.
Comment: This is no longer the case.  See: http://serverfault.com/questions/126072/ssl-certificate-selection-based-on-host-header-is-it-possible/126075#126075
Comment: @Warner: It was for some time, and even with Apache, you need 2.2.12 or newer to support it.
Answer: Have you done anything with default virtual host it makes?
If you don't do any customization apache will make a separate config file for some SSL stuff in conf.d/ssl.conf, and in there it declares a virtual host named _default_:443.
If I add a vhost as *:443 in my main config it gives the same error, and if I remove the _default_:443 vhost in the conf.d/ssl.conf it doesn't.
Answer: We resolved this issue by putting all of our SSL on a specific IP address and then all other sites on a secondary IP.  When we did this, everything worked.

Apache Virtual Host by name


Comando remove não funcionando ionic

Question: Estou tendo problema com o navCtrl.push. O fluxo é o seguinte:

Nas paginas 1.1, 1.2 e 1.3 posso ir e voltar quantas vezes eu quiser porém quando clicar no botão voltar preciso necessariamente voltar na pagina 1.
Para ir para próxima pagina utilizo o evento click do botão assim:
(click)="quickAccess('Pagina1.1')"

E para voltar adiciono navPop ao meu botão do navbar.
O ERRO
Quando clico em voltar está voltando partes por pastes das paginas 1.2, 1.2 e 1.3 suponhamos se eu naveguei 10 vezes entre estas pastes ele irá voltar as 10 vezes.
TENTATIVAS
Estou tentando seguir o seguinte pensamento quando navego por exemplo da pagina 1.1 para 1.2 eu devo remover a 1.1 e assim por diante, porém estou tendo este problema, de como fazer isto, ja tentei fazer por index pelo nome da view mas nenhuma deles funciona.
VALE LEMBRAR
É importante saber que existem outras paginas anteriores a PAGINA 1 o fluxo com problema é apenas neste caso, nos restante tudo anda conforme o esperado.
Answer: Encontrei o problema !
Segundo esta inssue no ionic team:
https://github.com/ionic-team/ionic/issues/12238

Está ocorrendo um erro na estrutura, a solução foi a instalação da correção via npm como informado na inssue:
npm install ionic-angular@nightly --save

Assim meu .remove() funciona perfeitamente agora.

Comando remove não funcionando ionic


How to parse configuration options from conf file in boolean format in python automatically?

Question: Assume that a I have such configuration options under [Bulutkare] section:
[Bulutkare]
title = www.bulutkare.com
subtitle = www.bulutkare.com
id = 127
web_site = www.bulutkare.com
boolean_option = True

How can I parse boolean_option row as a boolean variable in my python module AUTOMATICALLY? I know, it can be parsed by using such code:
boolean_opt = config_parser.get("Bulutkare", "boolean_option")

but boolean_opt takes the option as String. My question is "how can my python code automatically recognize this option is a boolean?". So, my code can initialize boolean_opt as a boolean variable.
Is that possible?
Comment: this is not what ı am asking. in this example you know "boolean_option" is boolean. In my question, I don't know that boolean_option is a boolean or not. Code must recognize it. Not me. anyway thankyou.
Comment: `getboolean` should be what you want? https://docs.python.org/2/library/configparser.html#ConfigParser.RawConfigParser.getboolean
Answer: I think you want to use getboolean method:
boolean_opt = config_parser.getboolean("Bulutkare", "boolean_option")

If you cannot have consistent type of the config option you can wrap the get call like that:
def get_boolean_or_string(option):
    if option == 'True':
        return True
    elif option == 'False':
        return False
    else:
        return option

boolean_opt = get_boolean_or_string(config_parser.get("Bulutkare", "boolean_option"))

If you don't mind importing ast:
import ast

def get_boolean_or_string(option):
    try:
        return ast.literal_eval(option)
    except ValueError:
        return option

boolean_opt = get_boolean_or_string(config_parser.get("Bulutkare", "boolean_option"))

Comment: this is  not what ı am asking. in this example you know "boolean_option" is boolean. In my question, I don't know that boolean_option is a boolean or not. Code must recognize it. Not me. anyway thankyou.
Comment: thanks @Nebril. I wonder that is there a built-in function for that.
Comment: You can wrap the `get` method in some other function, which would analyze the string you are getting from it and if it is one of 'boolean' values (True, False, Yes, No, etc) it returns boolean, and if not it returns string. But I wouldn't do it, you probably want to have your config options to have consistent types.
Comment: There it is. Try using `literal_eval` from `ast` module.
Comment: Bulut Kare: if you found my answer useful, feel free to accept it (tick next to the answer).


Html.DisplayFor() não quebrando linha de string

Question: Nesse código, a ideia é gerar uma lista de registros buscando do banco de dados e mostrando num cshtml as variáveis, para cada item nesse Model.
No Banco, as strings contêm \n (line breaks) só que o HTML.DisplayFor não quebra a linha, o texto fica em uma só linha.
Eu resolvi esse problema com a tag
 mas aí o estilo de fonte fica diferente.
Tem outro jeito de fazer ele usar os \n ? Ou tem outro jeito de estruturar o html para funcionar?

             @foreach (var item in Model.Objeto.ObjetoDetalhes.AsEnumerable().Where(x => x.Tipo.ToUpper() == "RESUMO DA VISITA"))
                {

                            @Html.DisplayFor(modelItem => item.Registro);

                }

Comment: tenta adicionar esse estilo `white-space: pre-line` na ``
Comment: descobri e estava respondendo na hora. mas obrigado.
Answer: Inseri o Html.DisplayFor() dentro de um
 (parágrafo) dentro do .
Removi e usei o mesmo id para o parágrafo e escrevi um CSS para o id.
Assim ele sempre quebra a linha das frases escritas.

@Html.DisplayFor(modelItem => item.Registro)

#tableRegistro {
    white-space: pre-wrap;
}

Para mais informações: MDN Web Doc: white-space


How to change the Drupal Commerce Stock formatter?

Question: Drupal Commerce Stock displays the correct number of stock. For example:
Product Name, Price: $ 1.00, Stock: 20.
I would like to change the stock levels to 1, 2, 3, 4 and 4+. So if the stock level is 5 or more, the displayed stock will be 4+. What's the best way to do this?
Answer: If you want to display the stock of a product with 4+ when it's >5, assuming that you've got stock a field attached to the product and exposed to the node display (default behavior) you can use hook_entity_view_alter or hook_node_view_alter to alter the #markup of $build['product:field_stock']


Fast calculation of quadratic form in matlab

Question: I have the following case:
<code>    Y(i) - m x 1 vecotr , i = 1,...,N
    A(i) - m x m symmetric matrix , i = 1,...,N
    H(i,j) = 0.5*(Y(i)-Y(j))'( A(i)^-1+A(j)^-1)(Y(i)-Y(j)) |i,j = 1,...,N
</code>
Currently I calculated the inverse of A(i) separately and H with two 'for' loops:
<code>    for i= 1:N
       A_inv(:,:,i) = inv(A(:,:,i));
    end

    H= zeros(N,N);
    for j=1:N
        for i=(j+1):N
            x = Y(:,1,j)-Y(:,1,i);
            H(j,i) = 0.5*(x'*(A_inv(:,:,i)+A_inv(:,:,j))*x);
            H(i,j) = H(j,i);
        end
    end
</code>
I have not found a way to optimize the code, the answers I've seen in the forums are for cases which A is constant and does not depend on the indexes.
Is there a more efficient way to compute it?
Answer: I do not know what sizes are involved, but the following approach should almost always be both faster and more accurate, since it does not require a matrix inversion (which is something one should always try to avoid):
<code>clearvars

% Build sample inputs

N = 100;
m = 500;

Y = randn(m, 1, N);
for i = N:-1:1
    t = randn(m);
    A(:,:,i) = t' * t;
end

% original method

tic
for i= 1:N
   A_inv(:,:,i) = inv(A(:,:,i));
end

H= zeros(N,N);
for j=1:N
    for i=(j+1):N
        x = Y(:,1,j)-Y(:,1,i);
        H(j,i) = 0.5*(x'*(A_inv(:,:,i)+A_inv(:,:,j))*x);
        H(i,j) = H(j,i);
    end
end
toc

H_old = H;

clear A_inv H x

% proposed method

tic

for i = N:-1:1
   Ai_invY = A(:,:,i) \ Y(:,:);
   H(i,:) = Y(:,i)' * Ai_invY(:,i) ... % Yi' * Ai_inv * Yi
          - 2 * Y(:,i)' * Ai_invY ...  % - Yi' * Ai_inv * Yj - Yj' * Ai_inv * Yi
          + sum(Y(:,:) .* Ai_invY, 1); % + Yj' * Ai_inv * Yj
end
H = (H + H')/2;

toc

% check difference

plot(H_old(:) - H(:))
</code>
In my laptop I get 10.3 seconds for the original method, and 0.44 seconds for the proposed one.


Programmatically Place View With Defined Fixed Contextual Filter

Question: I'm currently using the starter module which adds the Twig function place_view(). This works well in most cases, however I'd like to be able to pass another parameter to populate the fixed value given in a contextual filter.
This would be my contextual filter settings:

Here's the place_view() Twig function:
public function place_view($name, $display_id = 'default') {
  $drupal = \Drupal::service('renderer');
  $view = views_embed_view($name, $display_id);

  if(! is_null($view)) {
    return $drupal->render($view);
  }

  return null;
}

Preferably I'd like to be able to use place_view() with a parameter to adjust that fixed value. It'd be even better if I could somehow reference the contextual filter I wanted to adjust the fixed value of.
Hopefully that all made sense, thanks in advance.
Answer: Use views_embed_view:
function views_embed_view($name, $display_id = 'default') {
  $args = func_get_args();
  // Remove $name and $display_id from the arguments.
  unset($args[0], $args[1]);

  $view = Views::getView($name);
  if (!$view || !$view->access($display_id)) {
    return;
  }

  return [
    '#type' => 'view',
    '#name' => $name,
    '#display_id' => $display_id,
    '#arguments' => $args,
  ];
}

Btw. Twig Tweak maps this function one to one:
  {# This accepts views arguments as well. #}

View:

{{ drupal_view('view_name', 'page_1') }}

Example
So if you want to filter the node 123 in the contextual filter you've shown in the question, you use this as third argument:
PHP
views_embed_view('view_name', 'block_1', '123');

TWIG (using the module Twig Tweak)
{{ drupal_view('view_name', 'block_1', '123') }}

Comment: Thanks, this is useful. I'm still having trouble actually accessing these extra arguments inside the view though. As shown in my screenshot, I'd like to alter the 'Default Value' contextual filter, or use a different filter that allows me to pass a value from my template.
Comment: I've added an example.
Comment: In my screenshot I have the default value set to 'Fixed'. Surely passing a random parameter of some numbers won't replace my given value in the view settings with the one provided in the `drupal_view()` function?
Comment: Yes, it will replace the default value.


IIS 404 error on existing text file

Question: I'm running a project on a server which runs IIS 7.5
There's a strange error that I have never seen. The project is written with Codeigniter. I'm converting videos with ffmpeg.exe and writing the results in separate text files for each video. And in the client side it reads the text file for progress.
But the server throws a 404 error for existing txt file. It keeps throwing error until I restart the server. After restarting the server, it can be able to show the text file.
Everything else works perfect. Please help me.
EDIT:
.txt files are reachable after amount of time. But it's not certain how much time should pass.
Answer: I found a solution. This isn't a solution to the source of the problem but works for me.
I think, IIS was throwing the 404 error because the file is being processed and locked by ffmpeg. I was getting the file contents by file_get_contents() via HTTP address and it returns an empty string or error message. So I decided to use the physical address of the file and to use read_file() function with output buffering.
It's now working without any error.

IIS 404 error on existing text file


how to use oninput in Angular?

Question: I want to limit the input maximum number.
data = [
   {
     qq:[30]
   },
   {
     qq:[100]
   }

      max{{data.qq[0]}}

      <input type="number" id="test" oninput="if(value > data.qq[0]) value = data.qq[0]">

Seem make sense.but have a error "data is not defined"
how to fix it.
I try ngModel but effect is not very good.
After exceeding the maximum value, the user can continue to input.
https://stackblitz.com/edit/angular-s5uwe6?file=src%2Fapp%2Fapp.component.ts
Answer: If someone is still interested, you can use (input)
<input type="number" id="test" (input)="yourFunction()">

Answer: simply you can use max attribute
  <input type="number" id="test" [max]="data.qq[0]">

stackblitz demo
Another way something like this
  <input type="number" id="test" #v  (change)="+v.value > data.qq[0]? v.value = data.qq[0]:0">

Comment: (ngModelChange)="+v.value > data.qq[0]? v.value = data.qq[0]:0"

That is work for me. Thanks
Answer: You can use keyup method in HTML and send $event.target.value as input to your required method.
<input (keyup)="filterData($event.target.value)">

filterData(e){
   console.log(e);
}

Answer:
You are using same variable for the array and iteration var.
qq[1] is not defined cause it has only 1 member of index 0
There is no oninput event in angular, use ngModel
The code in oninput will not work anyway
Read the tutorial about form and validation: https://angular.io/guide/form-validation#template-driven-validation
Comment: why wouldn't there be an `oninput` in angular? It's valid JS :)
Comment: I try ngModel but effect is not very good.
After exceeding the maximum number, user can continue input.
https://stackblitz.com/edit/angular-s5uwe6?file=src%2Fapp%2Fapp.component.html

how to use oninput in Angular?


Without using if statement, how to generate a username in java

Question: Okay, my class assignment is to write a code to generate a username.  But, it can be no more than 7 letters of the last name.  if there are fewer than 7 letters in the last name, then all the letters would be used.  But the prof says no if statements.  Any ideas ? The one I wrote works fine for names 7 or more letters but sends an error for short last names.  Here it is:
    //find first initial of firstName
    char firstInitial = firstName.charAt(0);

    //limit last name in userName to 7 characters
    String shortLastName = lastName.substring(0, 7);

    //create a username using the first letter of firstName and lastName (but no more than 7 letters)
    String userName = (firstInitial + shortLastName);

    //print username in lowercase
    System.out.println((firstName + " " + lastName + "'s standard username is:" + userName).toLowerCase())

Really just need an idea about how to proceed.  Possibly an example to look at. I've given up....
Comment: Does the ternary operator count as an if statement?
Comment: Professor said only string and math class.
Comment: Then that is OK, as it is Java syntax...
Answer: Use the Math class min function to decide if you should use the full last name or just the first 7 characters:
String username = firstName.charAt(0) + lastName.substring(0, Math.min(7, lastName.length()));

Answer: You could use the ternary operator (which is nothing but a if statement basically):
String shortened = name.length() > 7 ? name.substring(0, 7) : name;

[edit] To your edit "only string and math"; you could also use Math.min with maximum/actual string length.
Answer: You can achieve this using java.lang.Math#min(int, int)
//limit last name in userName to 7 characters
String shortLastName = lastName.substring(0, Math.min(7, lastName.length()));

Answer: You could catch the error it throws.
String shortLastName;
try
{
   shortLastName = lastName.substring(0, 7);
}
   catch(IndexOutOfBoundsException e) // If this is the error, I'm not positive on that
{
   shortLastName = lastName;
}

But using Min is more elegant.
Answer: If at all you do not want to use any comparison this is the way
    try {
        System.out.println(firstName.charAt(0) + lastName.substring(0, 7));
    } catch (StringIndexOutOfBoundsException e) {
        System.out.println(firstName.charAt(0) + lastName.substring(0, lastName.length()));
    }


Erro de sql server no servidor de hospedagem

Question: Estou fazendo um site, pelo template do Visual Studio. Comigo funciona, mas ao subir para o servidor e quando eu me logo, me dá o erro abaixo. Usei como template o SPA e Visual Studio 2015. Preciso subir mais alguma coisa? Fiz um publish e subi meu publish, mas pelo visto preciso de mais alguma coisa.

A network-related or instance-specific error occurred while
  establishing a connection to SQL Server. The server was not found or
  was not accessible. Verify that the instance name is correct and that
  SQL Server is configured to allow remote connections. (provider: SQL
  Network Interfaces, error: 52 - Unable to locate a Local Database
  Runtime installation. Verify that SQL Server Express is properly
  installed and that the Local Database Runtime feature is enabled.)]

A página de login sobe normalmente, mas eu vou remover a página de login e acho que pode funcionar, mas até agora nada.
Comment: Você alterou o caminho do banco de dados em sua `ConnectionString` antes de publicar?
Answer: Ao que parece, seu SQL Server não está configurado para receber conexões.
Verifica, por favor, o SQL Server Configuration Manager:

Você precisa habilitar o acesso.
Verifique o protocolo usado no web.config e faça a liberação de acordo.
Espero que ajude.