Concatenate or Append Multiple Files in Numerical Order to New File with Python

Question: I will start off by saying that I have seen the answers to a similar question. I have attempted to use this for my problem, but I am unable to get it to function properly.
I have multiple .xyz files with the same prefix and suffix starting from 0. There is potential to be 100+ files, so not defining them individually is essential. I need to merge/concatenate/append these files in numerical order to a merged.xyz. For example:
output0.xyz:
H 1.0 1.0 1.0

output1.xyz:
H 2.0 2.0 2.0

merged.xyz:
H 1.0 1.0 1.0
H 2.0 2.0 2.0

I have tried this as a simple test case:
tempfiles = ['output0.xyz', 'output1.xyz']

f = open("bigtextfile.xyz", "w")
for tempfile in tempfiles:
    f.write(tempfile.read())

But I get:
Traceback (most recent call last):
  File "Test.py", line 28, in <module>
    f.write(tempfile.read())
AttributeError: 'str' object has no attribute 'read'

An alternative I have attempted:
prefix = "output"
suffix = ".xyz"

count = 0
while count <= 1:
    for filename in os.listdir('./'):
        if filename.endswith(suffix):

            xyzFile = open(prefix + str(count) + suffix, "r")
            lines = xyzFile.readlines()
            xyzFile.close()

            merged = open("merged" + suffix, "a")
            for line in lines:
                merged.write(line)
            merged.close()
    count += 1

But it copies the first file twice, and the second three times:
H 1.0 1.0 1.0
H 1.0 1.0 1.0
H 2.0 2.0 2.0
H 2.0 2.0 2.0
H 2.0 2.0 2.0

How do you suggest I move forward? I am leaning towards the second solution, as it already automatically gets the contents of the files in numerical order. Please let me know if you need any additional details and how to improve the question.
Answer: You have:
tempfiles = ['output0.xyz', 'output1.xyz']

f = open("bigtextfile.xyz", "w")
for tempfile in tempfiles:
    f.write(tempfile.read())

But tempfile in your loop is being assigned the name of a file, such as 'output0.xyz', and being a string it has no method read; you need to use that as an argument to open:
tempfiles = ['output0.xyz', 'output1.xyz']

with open("bigtextfile.xyz", "w") as f:
    for tempfile in tempfiles:
        with open(tempfile) as infile:
            f.write(infile.read())

Note that I am using with context managers, which will automatically close the files when the blocks they govern terminate.
Update
This code will try to process files ouput0.xyz, output1.xyz, ... and will terminate until it gets an open error. It does not explicitly read the directory but rather assumes that the files are contiguously number 0, 1, 2 ... N:
from itertools import count

with open("bigtextfile.xyz", "w") as f:
    for suffix in count(0): # suffix takes on values 0, 1, 2, ...
        try:
            with open(f'output{suffix}.xyz') as infile:
                f.write(infile.read())
        except Exception:
            break # get out of loop when we can no longer open outputN.xyz for some value N

Comment: Ah yes, that makes sense to use with.
Comment: Any recommendations on automatically reading in the files in numerical order?
Comment: Ooh, clever. Thanks so much!
Answer: tempfiles = ['output0.xyz', 'output1.xyz']

with open("bigtextfile.xyz", "w") as f:
    for tempfile in tempfiles:
        with open(tempfile, 'r') as temp:
            f.write(temp.read())

Comment: And how is this different from my answer?
Comment: While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply.


Solving $\lim\limits_{n\to \infty}\frac{1\times3+3\times5+\dots+(2n-1)\times(2n+1)}{n^2}$

Question:
Evaluate
  $\lim\limits_{n\to \infty}\frac{1\times3+3\times5+\dots+(2n-1)\times(2n+1)}{n^2}$ using Stolz-Cesaro theorem .

Now, my attempt is :( i am not sure if it is correct)
$$\lim\limits_{n\to \infty}\frac{\sum\limits_{k=1}^n4k^2-1}{n^2}$$
$$=\lim\limits_{n\to \infty}\frac{4\sum\limits_{k=1}^nk^2-\sum\limits_{k=1}^n 1}{n^2}$$
$$=\lim\limits_{n\to \infty}\frac{\frac{4n(n+1)(2n+1)}{6}-n}{n^2}$$
= ( and now i've applied the Stolz-Cesaro theorem)
$$\lim\limits_{n\to \infty}\frac{\frac{4n(n+2)(2n+2)-6-4(n+1)(2n+1)+6}{6}}{n+1-n}$$
= (which, after jumping a few steps, lead me to the following answer)
 $$2(n-1)$$
Is this correct ?
Comment: Did you mean $(2n-1)\times(2n\color{red}+1)$ in the title and body?
Comment: You give $2(n-1)$ as result of some limit $\lim_{n\to\infty}f(n)$. But in such limit $n$ cannot have a place, because $n$ is a *ranging* variable.
Comment: Did you mean $n^3$ in the denominator?
Comment: Working out the expression gives $\frac13[4n+6-\frac1{n}]$. If $n\to\infty$ then this diverges to $+\infty$
Comment: So, nothing is being said in my book other than to solve this problem using Stolz-Cesaro theorem . Also, what i've posted is my attempt which i am not sure if it is correct, my book doesn't give the end result.
Comment: Yes, sorry. I will correct it right now
Comment: If this is correct, the the limit does not exist, but $2(n-1)$ is only the function describing the asymptotic behaviour. Is that a regularization how in "$1+2+3+\cdots=-1/12$" ?
Comment: Done, now do you think my attempt is the correct one ?
Comment: I see, so i've looked again in my book and the problem initially looks like this : $a_n=\frac{1\times3+3\times5+\dots+(2n-1)\times(2n+1)}{n^2}$ and it says "Solve the following". I figured i should take the limit of it since this problem is at the lesson :  Stolz-Cesaro theorem
Comment: No, it is $n^2$
Comment: Then the limit is obviously $\infty$.
Answer: If you let
$$a_n = 1\times 3 + 3\times 5 + \cdots + (2n-1)\times(2n+1), b_n = n^2,$$
Then
$$\lim\frac{a_{n+1}-a_n}{b_{n+1}-b_n} = \lim\frac{(2n+1)(2n+3)}{(n+1)^2-n^2}=\infty.$$
So by Stolz-Cesaro theorem,
$$\lim\frac{1\times 3 + 3\times 5 + \cdots + (2n-1)\times(2n+1)}{n^2}=\infty$$
Answer: It makes little sense to use the Cesaro theorem if you "know" that $$\sum_{k=1}^n k^2 = \frac{n(n+1)(2n+1)}{6}$$
The point of the exercise is most likely to see that Stolz-Cesaro allows you to conclude without that information. Indeed:
$$\lim_{n\to \infty}\frac{\sum_{k=1}^n (4k^2-1)}{n^2}=\lim_{n\to \infty}\frac{4n^2-1}{n^2-(n-1)^2}=\lim_{n\to \infty}\frac{4n^2}{2n}=+\infty$$


Count rows and exclude row in next count

Question: I am trying to count the number of rows within a group that meet a condition, but exclude those row counts if they appear in the consequent count for another level. Essentially, I am trying to count new and unique occurences of individuals within consequent censuses so I can create a cumulative distribution by census.
Please note that the sample dataset is from a sample from a larger dataset that has unequal n() values by treatment. Additionally, individuals may appear in census but not the next.
Sample Dataset
sampledata <- data.frame(treatment = c("A", "A", "H", "H", "H", "A", "A", "H", "H", "H",
                                       "A", "A", "H", "H", "H", "A", "A", "H", "H", "H"),
                         individual = c("1", "2", "3", "4", "5", "1", "2", "3", "4", "5",
                                        "1", "2", "3", "4", "5", "1", "2", "3", "4", "5"),
                         census = c("1", "1", "1", "1", "1", "2", "2", "2", "2", "2",
                                    "3", "3", "3", "3", "3", "4", "4", "4", "4", "4"),
                         value = c("0", "0", "1", "0", "0", "0", "0", "4", "3", "0",
                                   "0", "0", "8", "5", "2", "0", "1", "15", "7", "4"))

This is my code which I tried using

total is the total number of individuals of treatment and census.
count is the number of individuals with a value>0 for that treatment and census.
cum.sum is the cumulative individuals with value>0 for that treatment and beginning from census 1

If I was able to only get unique individuals (not counted in a previous census), then the cum.sum should never be greater than n.
Unfortunately as you can see, the output produces a cumulative number that is greater than the actual sample size n.
library(dplyr)
sampledata$value <- as.numeric(sampledata$value)
sampledata2 <- sampledata %>%
  group_by(treatment, census) %>%
  summarise(count=sum(value>0), total=n()) %>%
  ungroup() %>%
  group_by(treatment) %>%
  mutate(cum.sum=cumsum(count))

Expected Output
          treatment census count total cum.sum
1         A         1      0     2       0
2         A         2      0     2       0
3         A         3      0     2       0
4         A         4      1     2       1
5         H         1      1     3       1
6         H         2      1     3       2
7         H         3      1     3       3
8         H         4      0     3       3

EDIT/EXPLANATION FOR EXPECTED OUTPUT
For treatment=H, census=4, the variable count=0. The reason why it is 4 is because the 3 individuals that a value above 0 throughout the census had already been detected. I think naming the count variable as it is, may have confused readers.
The count variable is supposed to DETECT rows in the census (by treatment) that have a value>0 and HAVE NOT BEEN DETECTED BEFORE.
Comment: I don't quite understand your expected output. Why do you say that the number of individuals with value > 0 for treatment = H and group = 4 is 0? It should be 3, right?
Comment: I have added an explanation at the end of my original post. The count variable is supposed to DETECT rows in the census (by treatment) that have a value>0 and have NOT been detected before. I hope this helps!
Answer: You can just calculate an additional variable to adjust how count is defined.
Adding never_detected_before to your code:
library(dplyr)

sampledata2 <- sampledata %>%
  mutate(value = as.numeric(as.character(value))) %>%

  group_by(treatment, individual) %>%
  arrange(treatment, individual, census) %>%
  mutate(never_detected_before = cumsum(value>0) <= 1) %>%

  group_by(treatment, census) %>%
  summarise(count=sum(value>0 & never_detected_before), total=n()) %>%
  group_by(treatment) %>%
  mutate(cum.sum=cumsum(count))

Also note that group_by() by default overrides existing groups so that ungroup() is not needed.


Quais são as vantagens de se trabalhar diretamente com stdin, stdout e stderr?

Question: Existe alguma vantagem em termos de velocidade ou algum outro aspecto?
Mostra as conexões:
showConnections(all = TRUE)

Exemplos individuais:
stdin()
stdout()
stderr()

Comment: João, você poderia esclarecer um pouco mais a pergunta? Existe vantagem em relação a que, em quais circunstâncias?
Comment: Então acho que seria interessante explicar este contexo na pergunta! Abs
Comment: Possuo um script em Python que envia ao R um JSON simplificado em uma linha o qual recebo por stdin, efetuo o que tiver que fazer e depois devolvo no mesmo formato pelo stdout.
Já tentei argumentar que poderia me enviar o JSON por outro modo mas insistem que esta forma é a melhor.
Answer: Para responder à sua pergunta, vamos entender qual é o seu problema e quais são as soluções alternativas. O seu problema é transferir dados entre dois processos, o R e o Python. Eis algumas soluções para este problema:

Salvar os dados em arquivos que são acessados por ambos os processos. Acesso a disco é relativamente lento. Por outro lado, você não precisará reexecutar seus programas para gerar os dados uma vez que eles estejam gravados em disco.
Usar sockets de rede. Se os dois processos estiverem na mesma máquina, a comunicação é rápida. A vantagem é que seu código estará preparado para transferir dados entre os processos caso eles sejam colocados em máquinas diferentes.
Usar stdin e stdout. Nesse caso só vai funcionar se os dois processos estiverem na mesma máquina. A comunicação é rápida. A vantagem é que o código para ler da entrada padrão e escrever na saída padrão é bem simples.


Select rows in SQL that do not exist, given the existence of their references

Question: Suppose that I have a table rating which is unique to the pair user and client.
The schema of rating would hold user_id and client_id along with some other information (like score and comment).
At some point I might have the following situation where a * represents the existence of a rating for the corresponding user_id and client_id.
        user1 user2 user3 user4
client1   *     *     *     *
client2   *     *     *     *
client3   *           *
client4   *           *     *

In pure SQL, how can one select the rows with elements of user_id and client_id where a * does not exist?
In my example, the correct result should be
user_id  client_id
-------- ---------
2        3
2        4
4        3

Answer: Try something like:
SELECT *
FROM users
CROSS JOIN clients
WHERE NOT EXISTS (SELECT 1
                  FROM rating
                  WHERE rating.user_id = users.user_id
                    AND rating.client_id = clients.client_id)

Answer: The way I would approach this would be by constructing a query, involving LEFT JOIN, which included every combination whether valid or not, and then filtering to just those which have no data from the rating part of the JOIN.
In this case, we want a row for every combination of user and client, so that part requires a CROSS JOIN, with the ratings table LEFT JOINed onto that.
Here's the query with all possible combinations, and ratings where available (Live Demo):
SELECT
  U.user_name,
  C.client_name,
  R.rating
FROM
  users as U
CROSS JOIN
  clients as C
LEFT JOIN
  ratings as R
  On R.user_id = U.user_id
  And R.client_id = C.client_id
ORDER BY
  U.user_name,
  C.client_name;

From that output, it's hopefully clear that to find combinations where no rating exists, we can just add WHERE R.rating IS NULL (Live Demo):
SELECT
  U.user_name,
  C.client_name,
  R.rating
FROM
  users as U
CROSS JOIN
  clients as C
LEFT JOIN
  ratings as R
  On R.user_id = U.user_id
  And R.client_id = C.client_id
WHERE
  R.rating IS NULL
ORDER BY
  U.user_name,
  C.client_name;

(Note that there is no specific syntax in SQL to say "where the Left Join didn't match anything in the Right table", you just have to pick a non-nullable column, like I have with rating in this example.)
Comment: There is `NOT EXISTS` or `NOT IN` for the cases `where the Left Join didn't match anything in the Right table`
Comment: @IgorRomanchenko I don't know of any way to use those with a `LEFT JOIN`, only with a sub-query as shown in your answer. In this case, it comes to the same thing, but sometimes you want to perform complex logic (an `OR`, or a `CASE`, for instance) which uses data from the `LEFT JOIN`ed table if available, but knows for sure if there is not. It just feels ugly sometimes having to test a primary key against `NULL`.


The sum of integers in each row / column / major diagonal of a Magic Square of order $N$ is $\dfrac{N(N^2+1)}{2}$

Question:
Prove that the sum of integers in each row / column / major diagonal of a Magic Square of order $N$ is equal to $$\dfrac{N(N^2+1)}{2}$$

I have tested the formula on Durer's "1514" magic square and it holds (where N=4).
Answer: Add all the numbers in the square. It's the sum from 1 to $n^2$, which is $n^2(n^2+1)/2$. This is equal to the sum of $n$ rows, so each row is $n(n^2+1)/2$.


How to choose the number of categories when transforming a numeric variable into a factor?

Question: Sorry if this is a basic question but I have not been able to find any meaningful answers so far.
I have a numerical variable that I would like to convert into a factor, in order to do profiling and other things later on. My questions are, based on what criteria do I decide how many categories to create? Are there even any criteria?
Thanks a lot!
Comment: Converting a numerical variable into a factor is usually a bad idea, unless there is strong theoretical reason for choosing certain factors (e.g. if you were interested in the drinking behavior of adolescents, you might use an age factor of the legal drinking age).  What is your variable and why do you want to cut it into factors, exactly?
Comment: This depends on your problem, and is entirely context specific. There is no right or wrong statistically.
Answer: The way I see it, there are several ways to look at it:
Because theoretical reasons, that's why - In most cases a theoretical reason is the only reason. Sometimes our raw data comes in a certain form that has no use or makes little sense to us. If the categories have more meaning than the numbers, use categories, and the number of categories will be the number of meaningful dividers +1:

Age in years will be much more useful as categorical if we want to
compare pre-adolescents to adolescents to adults. If we want to
measure aggression, than we might not care about what happens between
ages 3-4, or 30-31.
Years of schooling will be more helpful as categorical if we are
interested in the effect of a graduate degree on the probability of
finding a high-paying job. Here we want to know if graduating high
school has any proportional effect, not individual school years
(meaning that not all years carry the same weight)

Because our measurement is iffy - Sometimes we have a continuous variable that happens to have very few 'levels'. If such a variable has less than 5, it is oftentimes wise to use it as an ordinal variable instead. The flip-side are ordinal variables with 5 or more equidistant levels, that can be used as continuous (aka a Likert scale). It would be pointless to talk about how much each additional year of schooling adds to our wages, if our sample has only individuals with 12 or 13 years of school. Here you would create as many categories as you have levels in the original variable.
Because our results are not good enough - Although it happens more than you might think, when creating models, it is usually bad form to force a theory from a model, as the famous adage goes - "it all lies, terrible lies and statistics". Make sure that the want for better results, does not cause us to wiggle our data this way and that so we get them. The inference may very well be wrong. Although this happens, don't do it.


Vscode - variable expansion in .env file

Question: Running python tests in VSCode.  I have a .env file that is loading (I can add new vars, and see the change reflected in a print(... of os.environ.
However, variable expansion within that file is doing something odd.  It seems to expand ${workspaceFolder} in some cases (specifically, PYTHONPATH=) but not others. ??
Here's my .env file.  Again, it is loading, as I can see changes if i add vars, or change the contents of the (fully working) PYTHONPATH var:
FOOBAR1=${workspaceFolder}/foobar1.txt
PYTHONPATH=${workspaceFolder}/backend/src:/tmp/foo-2:${env:PYTHONPATH}

however print('ing all the os.environ's shows:
...
        ?
        v
FOOBAR1=/foobar1.txt
PYTHONPATH=/home/-omitted-/backend/src:/tmp/foo-2:
...

${workspaceFolder} is expanded for one var, but not the other??  I've tried re-ordering.  Don't see anything here or on google.
Help !?  Thank you!
Answer: There is no variable expansion in vscode .env files at all. Any ${workspaceFolder} or similar variables are replaced by empty strings.

Note: Variable substitution is only supported in VS Code settings files, it will not work in .env environment files.
https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file

Something else is probably prepending the /home/-omitted- to PYTHONPATH.


Can I use a single ADFS server for both office 365 and SharePoint SSO

Question: Can I use a single ADFS server for both office 365 and SharePoint SSO if they use different SSL's and domain names?
Enviroment consists of:
2 DC's --
ADFS Server --
ADFS Proxy --
Sharepoint 2010 Server (portal.companyname.com) --
hosted o365 (companynameMail.com)
o365 SSO is currently working flawlessly (after a LOT of work) I want to add SSO for our SharePoint server.
I'm unclear if its possible, and stuck at adding in a second SSL on the ADFS server.
(And yes, I will be adding redundent servers as time and budget permits)
Comment: Your question isnt very clear. You mention "adding a second SSL" on ADFS and then in the comment you mention SharePoint too. Its not clear whether you want a Yes/No answer to know if its possible to achieve what you are trying to do. Or whether you need specific help on ADFS or SharePoint or both in configuring this setup. The more detail you provide explaining exactly what you have, what you have done and what you need help with will help us help you.
Answer: I am not sure I understand the problem here. I have a lab environment where my AD FS 2.0 is used for many relying parties including O365 and a SP2010 server.
Are you asking if its possible to use the existing ADFS infrastructure used for O365 with the on-premise SharePoint 2010 implementation? Yes thats possible. You will end up with two relying party trusts (one for O365 and the other for SP2010).
Edit 28/12:
I assume you know how to configure SP2010 for federated authentication. Please see http://blogs.technet.com/b/speschka/archive/2010/07/30/configuring-sharepoint-2010-and-adfs-v2-end-to-end.aspx as it should help.
Comment: by second SSL I mean an additional SSL certificate for the SP server


How do I $.post an array with jQuery so I can use the array in my PHP file?

Question: I have the following piece of jQuery code:
function saveSchedule()
{
    var events = [];

         $('ul#schedule-list').children().each(function() {
             events.push($(this).attr('value'));
         });

         jQuery.each(events, function()
         {
             alert(this);
         });

         $.post("schedule_service.php?action=save_schedule",
         { events: events, studentid:  $('#student').val() },
         function(data) {
             alert(data);
         }, 'json');
     }

Which gets all the 'values' from a list on my page, and puts them into the events array.
Then, below I pass in that array plus a studentid into the data section of my $.post call.
However, when I receive this array on my PHP side, it seems to be a singular value:
<?php

    include_once('JSON.php');
    $json = new Services_JSON();

    if ($_GET['action'] == 'save_schedule')
    {
        $event_list = $_POST['events'];

        echo $json->encode($event_list);
        exit;
    }
?>

(note: I'm using an older version of PHP, hence the JSON.php library.)
Now, all this ever returns is "14", which is the last event in the events array.
Post:

Response:

How am I handling passing the array in my $.post wrong?
Answer: It may seem silly, but try this:
$.post("schedule_service.php?action=save_schedule",
         { 'events[]': events, studentid:  $('#student').val() },
         function(data) {
             alert(data);
         }, 'json');

PHP will parse the key name events[] into an actual array inside php automatically...
Comment: Great answer @gnarf. If I hadn't tested first I would have beat you to the answer ;) +1
Comment: @dcneiner - I was 99% sure it would work, since thats what you use for `` - but since you tested it, *s/should/will/*
Answer: You can pass the array elements as a string comprising of all the elements concatenated by a character. Then you can split the array in your php code and extract the values there.
Use join() to join the elements of an array.
join(separator);
separator:

Specifies a string to separate each
  element of the array. The separator is
  converted to a string if necessary. If
  omitted, the array elements are
  separated with a comma.

Use explode() on the PHP side to turn the **join()**ed string back together.
Answer: Why not use JSON? Pass the JavaScript array as a JSON object and parse it in PHP.
Answer: Believe it or not, just use 'events[]': events in your $.post request instead of events: events (without the brackets).


Ag grid does not render properly (Angular 2 - webpack2)

Question: I'm currently trying to inject ag-grid in my angular2 project and I'm having an issue rendering it.
I'm trying to use this ag-grid package and to follow the tutorial with a .net projet generated with the command 'dotnet new angular'.
Here is my agGrid.component.ts :
import { Component } from '@angular/core';
import { GridOptions } from "ag-grid/main";

@Component({
    selector: 'ag-grid-component',
    templateUrl: './agGrid.component.html'
})
export class AgGridComponent {

    private gridOptions: GridOptions;
    public rowData: any[];
    private columnDefs: any[];

    constructor() {

        this.gridOptions = <GridOptions>{};
        this.createRowData();
        this.createColumnDefs();
        this.gridOptions.defaultColDef = {
            headerComponentParams: {
                menuIcon: 'fa-bars'
            }
        }

    }

    private createRowData() {
        this.rowData = [
            { make: "Toyota", model: "Celica", price: 35000 },
            { make: "Ford", model: "Mondeo", price: 32000 },
            { make: "Porsche", model: "Boxter", price: 72000 }
        ];
    }

    private createColumnDefs() {
        this.columnDefs = [
            { headerName: "Make", field: "make" },
            { headerName: "Model", field: "model" },
            { headerName: "Price", field: "price" }
        ];
    }

}

And my agGrid.component.html :
<ag-grid-angular #agGrid style="width:500px;height:200px" class="ag-fresh"
                    [gridOptions]="gridOptions"
                    [columnDefs]="columnDefs"
                    [rowData]="rowData">
</ag-grid-angular>

I carefully imported AgGridModule from "ag-grid-angular/main", but the result is quite awful :

Can someone enlighten me please ? Thank you in advance.
Comment: Looks like you aren't getting the css files, are those being imported somewhere?
Answer: New to Angular 2/4 and Visual Studio 2017, but had the same issue.  Reading the two answers above helped tremendously.
I did the following to get the same sample in the original question working

Copied the CSS files from MyApp/node_modules/ag-grid/dist/styles to a folder under MyApp/wwwroot ( MyApp/wwwroot/node_modules/ag-grid/dist/styles ).  Note: copy only the CSS files.
Edit your Index.cshtml file to include the the two "link" tags.
@{
    ViewData["Title"] = "Home Page";
}
@Loading...@
Loading...
@section scripts {
<link href="node_modules/ag-grid/dist/styles/ag-grid.css" rel="stylesheet"/>
<link href="node_modules/ag-grid/dist/styles/theme-fresh.css" rel="stylesheet" />

}

Would like feedback on a more elegant solution...thanks
Answer: This is a problem of a CSS styles loading, make sure you have imported the CSS files properly.
Answer: Please add below lines to your main html file header.(start with < symbol)
link href="/node_modules/ag-grid/dist/styles/ag-grid.css" rel="stylesheet"/>
   link href="/node_modules/ag-grid/dist/styles/theme-fresh.css" rel="stylesheet"/>


Fatal error Uncaught PDOException: General error: mode must be an integer

Question: Estou obtendo este erro quando tento fazer um insert no banco com PDO:
> >
 Fatal error:  Uncaught PDOException: SQLSTATE[HY000]: General error: mode must be an integer in
> C:\Users\Usuário.NB-0307\Documents\Site\Admin
> 2\db\enviarexercicio.php:51 Stack trace:
> #0 C:\Users\Usuário.NB-0307\Documents\Site\Admin 2\db\enviarexercicio.php(51): PDO->query('INSERT INTO tb_...',
> Array)
> #1 {main}   thrown in C:\Users\Usuário.NB-0307\Documents\Site\Admin 2\db\enviarexercicio.php on line 51

Não consigo achar oque esta causando, estou utilizando esta classe para conexão com banco:
    <?php
/**
 * Classe de conexão ao banco de dados usando PDO no padrão Singleton.
 * Modo de Usar:
 * require_once './Database.class.php';
 * $db = Database::conexao();
 * E agora use as funções do PDO (prepare, query, exec) em cima da variável $db.
 */
class Database
{
    # Variável que guarda a conexão PDO.
    protected static $db;
    # Private construct - garante que a classe só possa ser instanciada internamente.
    private function __construct()
    {
        # Informações sobre o banco de dados:
        $db_host = "******";
        $db_nome = "*****";
        $db_usuario = "****";
        $db_senha = "****";
        $db_driver = "***";
        # Informações sobre o sistema:
        $sistema_titulo = "***";
        $sistema_email = "*****";
        try
        {
            # Atribui o objeto PDO à variável $db.
            self::$db = new PDO("$db_driver:host=$db_host; dbname=$db_nome", $db_usuario, $db_senha);
            # Garante que o PDO lance exceções durante erros.
            self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            # Garante que os dados sejam armazenados com codificação UFT-8.
            self::$db->exec('SET NAMES utf8');
        }
        catch (PDOException $e)
        {
            # Envia um e-mail para o e-mail oficial do sistema, em caso de erro de conexão.
            mail($sistema_email, "PDOException em $sistema_titulo", $e->getMessage());
            # Então não carrega nada mais da página.
            die("Connection Error: " . $e->getMessage());
        }
    }
    # Método estático - acessível sem instanciação.
    public static function conexao()
    {
        # Garante uma única instância. Se não existe uma conexão, criamos uma nova.
        if (!self::$db)
        {
            new Database();
        }
        # Retorna a conexão.
        return self::$db;
    }
}

E ela funciona normal quando faço select mas quando tento fazer insert da erro
<?php
    require_once './Database.class.php';
    $db = Database::conexao();
    date_default_timezone_set('America/Sao_Paulo');
    $errors = array(); //To store errors
    $form_data = array(); //Pass back the data to `form.php`

    /* Validate the form on the server side */
    if (empty($_POST['Enunciado'])) {
        $errors['erro'] = 'EnunciadoName cannot be blank';
    }
    if (empty($_POST['Pergunta'])) {
        $errors['erro'] = 'Pergunta cannot be blank';
    }
    if (empty($_POST['resposta1'])) {
        $errors['erro'] = 'resposta1 cannot be blank';
    }
    if (empty($_POST['resposta2'])) {
        $errors['erro'] = 'resposta2 cannot be blank';
    }
    if (empty($_POST['resposta3'])) {
        $errors['erro'] = 'resposta3 cannot be blank';
    }
    if (empty($_POST['respostacorreta'])) {
        $errors['erro'] = 'respostacorreta cannot be blank';
    }
    if (empty($_POST['unidade'])) {
        $errors['erro'] = 'unidade cannot be blank';
    }
    if (!empty($errors)) { //If errors in validation
        $form_data['success'] = false;
        $form_data['erros']  = $errors;
    }
    else { //If not, process the form, and return true on success

        $Enunciado = $_POST['Enunciado'];
        $Pergunta = $_POST['Pergunta'];
        $resposta1 = $_POST['resposta1'];
        $resposta2 = $_POST['resposta2'];
        $resposta3 = $_POST['resposta3'];
        $respostacorreta = $_POST['respostacorreta'];
        $unidade = $_POST['unidade'];

        $resultado = $db->query("INSERT INTO tb_exercicios(enunciado,pergunta,resposta1,resposta2,resposta3,respostacorreta,unidade) VALUES(:ENUNCIADO, :PERGUNTA, :RESPOSTA1, :RESPOSTA2, :RESPOSTA3, :RESPOSTACORRETA, :UNIDADE)",array(
                        ":ENUNCIADO"=>$Enunciado,
                        ":PERGUNTA"=>$Pergunta,
                        ":RESPOSTA1"=>$resposta1,
                        ":RESPOSTA2"=>$resposta2,
                        ":RESPOSTA3"=>$resposta3,
                        ":RESPOSTACORRETA"=>$respostacorreta,
                        ":UNIDADE"=>$unidade
                    ));
        if($resultado){
            $form_data['success'] = true;
            $form_data['enviado'] = 'Cadastrado';
        }else{
            $form_data['success'] = false;
            $errors['erro'] = 'Erro ao fazer o cadastro no banco';
            $form_data['erros']  = $errors;
        }
    }

    //Return the data back to form.php
    echo json_encode($form_data);
 ?>

Answer: O método query() do PDO não suporta prepared statements. Ao passar placeholders para o insert eles serão entendidos como strings.
Para usar prepared statements o primeiro passo é chamar o método prepare() e em seguida excute() passando o array com os valores para os repectivos placeholders.
Mude:
 $resultado = $db->query("INSERT INTO tb_exercicios(enunciado,pergunta,resposta1,resposta2,resposta3,respostacorreta,unidade) VALUES(:ENUNCIADO, :PERGUNTA, :RESPOSTA1, :RESPOSTA2, :RESPOSTA3, :RESPOSTACORRETA, :UNIDADE)",array(
                        ":ENUNCIADO"=>$Enunciado,
                        ":PERGUNTA"=>$Pergunta,
                        ":RESPOSTA1"=>$resposta1,
                        ":RESPOSTA2"=>$resposta2,
                        ":RESPOSTA3"=>$resposta3,
                        ":RESPOSTACORRETA"=>$respostacorreta,
                        ":UNIDADE"=>$unidade
                    ));

Para:
$resultado = $db->prepare("INSERT INTO tb_exercicios(enunciado,pergunta,resposta1,resposta2,resposta3,respostacorreta,unidade) VALUES(:ENUNCIADO, :PERGUNTA, :RESPOSTA1, :RESPOSTA2, :RESPOSTA3, :RESPOSTACORRETA, :UNIDADE)");

$sucesso = $resultado->execute( array(":ENUNCIADO"=>$Enunciado,
       ":PERGUNTA"=>$Pergunta,
       ":RESPOSTA1"=>$resposta1,
       ":RESPOSTA2"=>$resposta2,
       ":RESPOSTA3"=>$resposta3,
       ":RESPOSTACORRETA"=>$respostacorreta,
       ":UNIDADE"=>$unidade));

if(!$sucesso){
    echo '
';
    print_r($resultado->errorInfo());
}


SQL Server becomes slow after restart

Question: I already posted this one on stackoverflow but someone gave me the hint to that I might have more luck on serverfault.
We use SQL Server 2005 on an Windwos Server 2008. Ther Server has 48 GB RAM. SQL Server is configured to use 40 GB RAM. There is only one database hosted (About 70 GB). The only app beside SQL Server is our App-Server which connects the clients to the database.
Now we encounter the following problem:
After a restart of the server our the performance is great. The server grabs the 40 GB RAM wich it is allowed to and then runs fast as hell. But after about 4 weeks the system becomes slower and slower. The execution of statements (seen in the profiler) is raising slowly. But I cannot see that there is something going wrong on the server.

CPU usage is at about 20%
I/O also seems to be no Problem
The process monitor does also not show that there are strange apps or something like that.
Eventlog does also have no interessting messages
No open transactions or blockings to see
We do not use cursors in our app

We tried already the following things without effect:

Droped the cache by using the
statements
<code>DBCC FreeProcCache
DBCC FREESYSTEMCACHE('ALL')
DBCC DropCleanbuffers
</code>
Restarted the Appserver we are using.
Restart the sql server service

But nothing did help exept restarting the whole server. Any ideas?
Comment: Have you tried to set up a trace and see if something strange is going on(may be monthly)?
Answer: Call Microsoft. Seriously - this is funny. Unless you overlooked something this should not happen. Not after WEEKS.

We can assume the server is getting hot within a day, right? As in: fully utilizing it's resources and handling normal load (a cold server is slower because obviously the caches are mpty).
Any sort of caching should not come to pass after WEEKS. I could understand come pollution from bad query plans in stored procedures coming around after some days, but not 4 weeks....

...except: do you do something in a monthyl interval that is RELATED To the time the server gets slow?
Someone will definitely need to take a look at the complete performance metrics from the server and spend some time finding that one out then. Nice caese for PSS (Professional Support Services) ;)
How do you do index reorganization?
I would look for a monthly (30 day interval) process you start that then makes the server slow for some reason.
Comment: We have set up a weekly job (every sunday) to rebuild all indexes. But there is no monthly job. I really think I will follow your recommendation and call the MS Support.
Comment: This sounds a little crazy - exactly the stuff I would have loved when working at MS (and yes, I did 3rd level support). Expect some heavy lifting there - MS has a nice tool that collects performance parametrics for support in one location. Hoipe you get to 3rd level support fast ;)

SQL Server becomes slow after restart


Does the Bias of the Model Only Depend on Model Class?

Question: In machine learning with statistical approach, does the bias of the model solely depend on the selection of the model class without considering the training data? There is a claim regarding the bias-variance decomposition in a technical report (Settles Burr, Active Learning Literature Survey, 2009) saying:

The second term is the bias, which represents the error due to the model class itself, e.g., if a linear model is used to learn a function that is only approximately linear. This component of the overall error is invariant given a fixed model class. The third term is the model's variance, which is the remaining component of the learner's mean squared error with respect to the true regression function. Minimizing the variance, then, is guaranteed to minimize the future generalization error of the model (since the learner itself can do nothing about the noise or bias components)

I'm wondering why this is true or not. And why does the variance depend on the training data instead? I search online for a while but there is no answer for such doubt. I also consult with GPT, well, its answer resonate with the claim in the paper, and saying "no formal proof can be given as it is conceptual understanding of what bias represents in the bias-variance decomposition and how it relates to the choice of model class" (by GPT).
Comment: My conclusion to this raised question is simple, according to the the selected answer by @Richard, the bias is treated as invariant if and only if the model is assumed to be unbiased. I.e., the model has zero bias, thus remain invariant regarding the dataset.
Answer: Consider a data generating process
$$Y=f(X)+\varepsilon$$
where $\varepsilon$ is independent of $x$ with $\mathbb E(\varepsilon)=0$ and $\text{Var}(\varepsilon)=\sigma^2_\varepsilon$. According to Hastie et al. "The Elements of Statistical Learning" (2nd edition, 2009) Section 7.3 p. 223, we can derive an expression for the expected prediction error of a regression fit $\hat g(X)$ at an input point $X=x_0$, using squared-error loss:
\begin{align}
\text{Err}(x_0)
&=\mathbb E[(Y-\hat g(x_0))^2|X=x_0]\\
&=(\mathbb E[\hat g(x_0)−f(x_0)])^2+\mathbb E[(\hat g(x_0)−\mathbb E[\hat g(x_0)])^2]+\sigma^2_\varepsilon\\
&=\text{Bias}^2\ \ \ \quad\quad\quad\quad\quad\;\;+\text{Variance } \quad\quad\quad\quad\quad\quad+ \text{ Irreducible Error}
\end{align}
(where I use the notation $\text{Bias}^2$ instead of $\text{Bias}$ and $\hat g$ instead of $\hat f$). As noted further on p. 224 (and referred to in this post), for linear models the averaged-over-$x_0$ bias can be further decomposed neatly into model bias and estimation bias,
$$
\mathbb E_{x_0}\big[ (\mathbb E[\hat g(x_0)−f(x_0)])^2 \big] =
\mathbb E_{x_0}\big[ (f(x_0)-x_0^{\top}\beta_*)^2 \big] +
\mathbb E_{x_0}\big[ (x_0^{\top}\beta_* - \mathbb E_{x_0}[ x_0^{\top}\beta_\alpha ])^2 \big]
$$
where $\beta_*=\arg\min_\beta \mathbb E[(f(X)-X^\top\beta)^2]$ are the parameters of the best-fitting linear approximation to $f$ and $\beta_\alpha$ are the estimated parameters. This shows that even if a model class is unbiased ($\text{Bias}^2=0$ for $\beta_*$), having a biased estimator $\beta_\alpha$ will result in $\text{Bias}^2\neq 0$. It is possible that the authors of your quote consider model class to define not only the model but also the estimator, and then they would be right, but without such a qualification I do not think they are.
Comment: Thanks for the detailed reply! Can we just make it more simple? Like, just assume our model is unbiased! Then the bias term just taken as Zero.

I see most of the empirical results showing that the models of high complexity normally have Zero bias, but relatively higher variance.

Do you think these empirical results can somehow support the Unbiased model assumption to hold for high complexity models, e.g., Neural Network?
Comment: @sharp_flyingrain, is that a new question? If so, please ask it on a separate thread.  The comments here can be used for clarifying your original question and my answer to it.


How do I prove $\frac 34\geq \frac{1}{n+1}+\frac {1}{n+2}+\frac{1}{n+3}+\cdots+\frac{1}{n+n}$

Question: How do I prove the following inequality
$$\frac 34\geq \frac{1}{n+1}+\frac {1}{n+2}+\frac{1}{n+3}+\cdots+\frac{1}{n+n}$$
without the help of induction?
Thanks for any help!!
Comment: Related : http://math.stackexchange.com/questions/525749/for-each-positive-integer-n-let-xn-1-n11-n2-1-2n-prove-that-the-s
Answer: You don't need calculus here (although it would make things easier). Let $$S=\frac{1}{n+1}+\frac {1}{2n}+\frac{1}{n+3}+...+\frac{1}{2n}$$
and write it both ways, à la Gauss:
$$\begin{align}
2S &= \frac{1}{n+1}+\frac {1}{n+2}\,\,\,+\,\,\frac{1}{n+3}\,+...+\,\,\,\frac{1}{2n} \\
&\,\,\,\,+\frac{1}{2n}\,\,\;+\frac {1}{2n-1}+\frac{1}{2n-2}+...+\frac{1}{n+1}\\
&=\sum_{k=1}^n\left(\frac{1}{n+k}+\frac{1}{2n-(k-1)}\right)\\
&<\sum_{k=1}^n\left(\frac{1}{n+k}+\frac{1}{2n-k}\right)\\
&=\sum_{k=1}^n\frac{3n}{(n+k)(2n-k)}\\
&\le\sum_{k=1}^n\frac{3n}{2n^2} \,\,\,\,\text{because}\, (n+k)(2n-k) \ge 2n^2 \text{ if } 1 \le k \le n\\
&=\frac{3}{2n}\sum_{k=1}^n1\\
&=\frac32
\end{align}$$
Hence $S < \dfrac{3}{4}$.
Answer: A variation on the inequality that @TonyK already mentioned. For $1\leq k\leq n$ $$2n^2\leq 2n^2+k(n-k) = (n+k)(2n-k)$$ and so, dividing both sides by $2n^2(n+k)$, $$\frac1{n+k}\leq\frac1n-\frac{k}{2n^2}.$$ Then $$\sum_{k=1}^n\frac1{n+k}\leq 1 - \sum_{k=1}^n\frac{k}{2n^2}=1-\frac{n(n+1)}{4n^2}=\frac34-\frac1{4n}.$$
Comment: I prefer this to my answer.
Answer: Your sum can be expressed as a Riemann Sum.  Specifically

$$\frac 1{n+1}+\cdots+\frac 1{2n}<\int_n^{2n}\frac {1}{x}~\mathrm dx=\ln(2n)-\ln(n)=\ln(2)\sim.693.$$
Comment: The definite integral is the area under the curve for $n≤x≤2n$.  So...draw a picture.  the graph of $f(x)=\frac 1x$ is a strictly decreasing function of $x$.  divide the interval $n≤x≤2n$ by putting points at $\{n+1,n+2,\dots,n+n\}$ and approximate the area by drawing rectangles with height given by the right hand endpoints. so you have rectangles with area $\{\frac 1{n+1}, \frac 1{n+2},\dots,\frac 1{n+n}\}$.  If you've drawn the picture you'll see that each of these rectangles, hence the sum of their areas, lies entirely under the graph, so their combined area is less than the total area.
Comment: I am too confident with calculus...it would be helpful if the answer is a little more explanatory....thanks..
Comment: @lulu What do you mean by total area and combined area...please?
Comment: Do you know any integral calculus?  This is a very basic example.  See, for example the usual (calculus based) proof of the divergence of the sum $\frac 11 +\frac 12+\dots$.  You can find that proof in any first year calculus text.  Or you can read about it here https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)  I note that the wikipefia article includes a picture of the region.  But, if you've never seen any calculus, this might not be the best place to start.
Comment: I am a beginner in calculus...not too proficient in it.....I know the basics of calculus....I could  not understand what you were saying as you had not included a figure....now I can understand it...thanks for your help...
Comment: Had an exciting time learning Riemann sum and integrals....I accepted your answer after so many days because I had to utilise the time in learning Riemann sums....thanks a lot!!
Comment: No problem.  Fascinating stuff, no?
Comment: Yeah...really...
Answer: Expanding on the idea of the other answer:
$$\frac1{n+1}+\frac1{n+2}+\ldots+\frac1{2n}=\frac1n\left(\frac1{1+\frac1n}+\frac1{1+\frac2n}+\ldots+\frac1{1+\frac nn}\right)\rightarrow$$
$$\rightarrow\int_0^1\frac{dx}{1+x}=\log2<\frac34$$
Answer: $f(x)=\frac{1}{n+x}$ is a convex function on $\mathbb{R}^+$, hence:
$$ \frac{f(1)+f(2)+\ldots+f(n)}{n}\geq f\left(\frac{1+2+\ldots+n}{n}\right) \tag{1} $$
implies:
$$ \frac{1}{n+1}+\frac{1}{n+2}+\ldots+\frac{1}{n+n}\geq \frac{n}{n+\frac{1+2+\ldots+n}{n}}=\frac{2n}{3n-1}.\tag{2}$$
On the other hand, the Hermite-Hadamard inequality gives:
$$ \frac{1}{n+1}+\frac{1}{n+2}+\ldots+\frac{1}{n+n}\leq \int_{n+\frac{1}{2}}^{2n+\frac{1}{2}}\frac{dx}{x} = \log\left(\frac{4n+1}{2n+1}\right)\leq \log 2.\tag{3}$$
An alternative approach is to prove first that the sequence $\{a_n\}_{n\geq 1}$ given by $a_n=\frac{1}{n+1}+\frac{1}{n+2}+\ldots+\frac{1}{2n}$ is increasing, then to compute its limit at $n\to +\infty$
and check it is less than $\frac{3}{4}$.
Comment: Learnt something new from your answer....+1!!


Computing marginal probability density function - integration question

Question: Just stuck trying to evaluate a certain integral for the following question.

Let $(X,Y)$ be a bivariate random variable with joint probability density function:
$$
f_{X,Y}(x,y)
 = \begin{cases}
\frac{6y}{x^2} &\text{for }\quad 0<x<1,\quad 0<y<x^2\\
0,             &\text{elsewhere}
\end{cases}
$$
Compute the marginal probability density functions for $X$ and $Y$.

I'm able to compute the marginal pdf for $X$ but when I try to compute the marginal pdf for $Y$ using the following: $$f_Y(y) = \int_{-\infty}^{\infty}f_{X,Y}(x,y)\:dx=\int_{0}^{1}\frac{6y}{x^2}\:dx$$
I find that the rightmost integral diverges...is this, in fact, true or is there some conceptual misunderstanding on my part here?
Edit: fixed typo in the pdf
Answer: HINT

Your pdf does not integrate to $1$, you need to rescale or check for typos.
There are 2 way to parameterize your region $D$: for integrating $dy$ first, you have $0 < x < 1, 0 < y < x^2$. For integrating $dx$ first, $0 < y < 1, \sqrt{y} < x < 1$.

Can you finish?
Comment: Right! I see, the bounds I had in that integral for x are incorrect. Got it now! Thanks for that.


EntityFieldQuery to search Content Type for text and select matching Nodes

Question: I have created Search page which searches Products (Content type) by selecting few option drop down menus (those are selecting taxonomy terms), but also i have an text input field, which is supposed to narrow or define search by keywords.
Now problem is that i need to search several fields at same time.
This script works perfectly (snippet):
<code>$keywords=$_POST ['keywords'];
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'product')
->fieldCondition('field_main_category', 'tid', $category, 'IN')
->fieldCondition('field_sku', 'value','%'. $keywords .'%', 'LIKE');
$result = $query->execute();
</code>
I got all the product that are matching user entry, even if partial (those %).
But i need to query some other fields too (description and name), to search for terms that user may enter.
But, problem is that i must have one input field, and user may target 3 different fields, so three fieldConditions for same term will cause nothing to be found, because selection is excluding all  if term is not found in one field.
Could I,some way, write EntityFieldQuery query in a way to tell it to "forget" condition if keyword is not found ? (like in bellow example)
<code>$keywords=$_POST ['keywords'];
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'product')
->fieldCondition('field_main_category', 'tid', $category, 'IN')
->fieldCondition('field_sku', 'value','%'. $keywords .'%', 'LIKE');
->fieldCondition('field_name', 'value','%'. $keywords .'%', 'LIKE');
->fieldCondition('field_description', 'value','%'. $keywords .'%', 'LIKE');
$result = $query->execute();
</code>
Other workaround could be to look all three fields in one place, if i could get combined fields in one condition query (this could be done by hand, by adding another field only for search terms that will be filled with tags that will be searchable summary of all 3 fields, but i am hoping to find the way not to create more field and content, if possible).
Any suggestions or experiences with that ?
Thanks!
Comment: maybe this will help http://drupal.stackexchange.com/questions/45785/entityfieldquery-inner-join
Answer: Do you mean something like this;
<code>$keywords=$_POST ['keywords'];

$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'product')
->fieldCondition('field_main_category', 'tid', $category, 'IN');

if(isset($keywords)) {
 $query
   ->fieldCondition('field_sku', 'value','%'. $keywords .'%', 'LIKE');
}
$result = $query->execute();
</code>
UPDATE
You can do an OR for each of the field. SO to do this you add a tag to the query which you are building and then you can alter the query using the hook_query_TAG_alter
<code>    $keywords=$_POST ['keywords'];

    $query = new EntityFieldQuery();
    $entities = $query->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'product')
    ->addTag('something');

    $result = $query->execute();

function modulename_query_**something**_alter(QueryAlterableInterface $query) {
  $or = db_or()
    ->fieldCondition('field_sku', 'value','%'. $keywords .'%', 'LIKE')
    ->fieldCondition('field_name', 'value','%'. $keywords .'%', 'LIKE');

  $query
      ->condition($or);
}
</code>
This is just and example and you will have to make changes as per your requirements but this OR will definitely help.
Refer to the following links for a good example;
http://treehouseagency.com/blog/tim-cosgrove/2012/02/16/entityfieldquery-let-drupal-do-heavy-lifting-pt-1
http://treehouseagency.com/blog/neil-hastings/2011/09/06/building-energygov-without-views
http://treehouseagency.com/blog/fredric-mitchell/2012/02/21/or-queries-entityfieldquery
Comment: @HoverFusion updated the answer about the semi-colon but I am not sure if it will work at the template level have never tried it before. But give it a try and let me know your findings.
Comment: No luck from template side, i am doing module implementation. but got this error: Fatal error: Call to undefined method DatabaseCondition::fieldCondition()
Comment: I am not searching exactly for this, as three fields must take $keywords variable. ($keywords is set and must be checked against 3 fields, and if not found in some field, somehow tell query not to take that condition in account). But it opens a way to new approach for me, in some conditions (I was not aware i can split queries in this way!) upovoted, thanks! I update my question to clarify a bit more what i am searching for.
Comment: @HoverFusion I have updated the answer please have a look.
Comment: This is definately good lead to the answer. Is there anything that could prevent doing this hook from template file ( iwould like to do it from template.php) ? And i think you do not need semicolon after first fieldCondition.
Comment: @HoverFusion sorry it should not be fieldCondition. IT should be condition. So it will be something like this ->condition('a.field_archive_tid', array($tid), 'NOT IN');


AutoLISP: Select text inside closed polylines and define block atributtes with that text

Question: I have a floor-plan drawing with several rooms defined by enclosed polylines. There are several text annotations inside each polyline (room-number, room-name, etc).
How can i make a routine thats scans the drawing, builds a selection-set that finds each polyline (resides on a specific layer) and inserts a block (room_stamp) with attributes corresponding to those annotations.
So for example, for every room it would insert the block "room_stamp" inside the polyline, grab the text inside the polyline which belongs to the layer "room_name" and change the attribute "room_name" inside the block to match it.
Any help would be much apreciatted as I am just starting with LIPS routines
Answer: The first step would be to obtain a selection set of all closed polylines residing on the target layer - for this you would use the ssget function with the X mode string argument (to scan the entire drawing database with no user input), and a filter list argument filter on entity type (DXF group 0), and layer (DXF group 8), and optionally on the bit-coded DXF group 70 to restrict the selection to closed polylines only - you may wish to refer to my ssget Function Reference to assist with the construction of this expression.
On obtaining a selection set of polylines on the target layer, you would then iterate over this set (there are many methods to do this), and for each polyline you have one of two options:

Obtain a window polygon/crossing polygon selection set of all text (perhaps restricted to a target layer) by supplying the ssget function with the WP or CP mode string, the set of polyline vertex coordinates, and an appropriate filter list.

Obtain a selection set of all candidate text objects within the active layout/viewport and use a ray-casting algorithm or similar to test whether the insertion point of each text object resides within the boundary of the polyline.

Method (1) is the easiest, but could also be the least reliable, as the graphical selection methods of the ssget function will only consider objects visible within the drawing area at the time of function evaluation - hence, for greater accuracy, you would first need to zoom to each polyline before acquiring the selection of the objects it encloses, and then restoring the previous view.
Method (2) is more reliable in that it is independent of the state of the graphical display in AutoCAD, but will be slower as it requires more calculation and iteration over the entire set of candidate text objects (unless you implement a divide & conquer technique through clever use of pre-sorting the set).
Upon acquiring the set of text objects residing within the polyline, you'll need some way to differentiate each text object in the set, so that you populate each attribute with the appropriate value. This will be completely dependent on your data - e.g. whether each item of data resides on its own layer (for example, do all of the room numbers reside on their own layer, and all room names on another?), or can you use pattern matching on the text content to identify a room number from a room name and other information?
With the appropriate values determined, it is relatively straightforward to insert a new attributed block reference (perhaps at the centroid of the polyline, or at the insertion point of one of the existing text objects) and populate the block attributes with the values acquired from the text - there are many existing examples of this.
Comment: Many thanks Lee Mac! Followed your tips to arrive to a solution.
Comment: You're welcome @user14693180 ! Please mark my answer as the solution if it sufficiently answered your question, so that the question appears as resolved for others viewing the site.


Normalizing or detrending groups of samples

Question: How do I detrend or normalize multiple series of data so that I can inter-compare between the series?

Specifics below may not be appropriate for this forum. Please let me know and I can remove or re-phrase, but I think it might be helpful to fully understand the generic question above.
I have a data set that I would like help analyzing. I think this question belongs here and not in at https://gis.stackexchange.com/
Specifically, I have the following situation: Each series is collected from an airplane flying a path, and has a variable number of (value, lat,lon,time) tuples. I have several of these flightpaths, each at a different time, and flying different paths (sometimes crossing, sometimes not). Flights happened months apart, at different times of day, and due to natural phenomena, the data (thermal in this case) varies.
Part of the region flown over by multiple flights may or may not have an anomalous temperature signature. This is what I want to investigate. I am seeking an algorithm to detrend or normalize all of the flights so that I can increase my SNR and determine if there is a temperature anomaly over a sub-region.
Comment: It seems to me no valid comparison is possible among these series unless there is some persistent spatial phenomenon producing the "temperature signature."  Otherwise they all measure different things.  If there is a spatial pattern, then you really have a problem of spatial interpolation, not of time series normalization.
Comment: It is complex and messy real-world data. The "thermal anomaly" is (I hypothesize) variable in time but consistent in location. Overlaid on this is a different large-scale regional temperature gradient, seasonal and day/night variations, noise, instrument variability, etc. The flight paths come and go in different directions (so the mean temp of two flight paths should not be equal, even if season+time-of-day are removed, because of the large-scale regional gradient, etc.)...
Answer: Multi-level modelling where your data are grouped by flight as a random variable sounds like a good analysis method for this problem.  In R the code might be
library(lme4) #load the package)
lmer(temp ~ region + (1|flight))

This is doable in a variety of statistics packages.  If region is simply in region or outside of region then a logistic form should be used.
To directly address your question of normalizing you might like
temp - (mean_temp_for_flight - mean_temp)

This zeros the temperatures at the overall mean corrected for the individual flight mean.  So, if a flight had a mean temp of 20 over the region, and the mean temp of the region is 18, and your sample is 22, then the normalized value would be
22 - (20 - 18) = 20

Essentially... by flight variability is eliminated.
Comment: This is helpful, thank you. And addresses the simplified problem stated above. Unfortunately, as commented above, the real world is much more complex than what I initially described. I'm still working on it...


Como capturar cada tecla pressionada em uma Entry com tkinter?

Question: Utilizando a biblioteca tkinter, eu gostaria de realizar uma pesquisa no banco de dados a cada tecla apertada pelo usuário numa Entry, para que o resultado seja mostrado num Listbox automaticamente conforme vou digitando. Como posso fazê-lo ?
Answer: Você só precisa criar um evento usando Entry.bind() chamado "<Any-KeyPress>" passando a função que deseja chamar. Dessa forma, sempre que o usuário apertar qualquer tecla, tendo a Entry foco, a função será chamada. Veja esse exemplo que eu criei abaixo:
from tkinter import *

def checkDatabase(event = None):
    root.after(100, lambda: print("Procurando por", entry.get()))

root = Tk()
Label(root, text = "Digite abaixo o que procura").pack()

entry = Entry(root)
entry.bind("<Any-KeyPress>", checkDatabase)
entry.pack()

root.mainloop()

Uma coisa bem interessante que podemos observar nesse exemplo acima, é que dentro da função checkDatabase, eu estou utilizando o método after(). Eu utilizei esse método, para que a função não seja executada antes do caractere ser inserido no Entry, já que o evento é detectado antes.
Comment: ah ta então eu posso colocar nessa função para pesquisar o que to querendo no banco de dados em tempo real , entendi obrigado vou tentar fazer


Extend Session Time On Button Click Event Server Side

Question: Is there a way to extend the session of a user the moment they click a button that fires up code behind logic. Example I need to 100 people to a gridview and each time I add a person I want the session to be reset to 30 minutes.
I need to set the timeout on a certain page not the whole site. When a user makes a button click fire the timeout resets. So if the timeout on the page is 30 min, you wait 29 minutes then click the button the timeout is 30 min again.
Comment: The sesssion expiration time is reset when there is a postback. What's not working of that?
Comment: @Redi I need to set the timeout on a certain page not the whole site. When a user makes a button click fire the timeout resets. So if the timeout on the page is 30 min, you wait 29 minutes then click the button the timeout is 30 min again.
Answer: It happens automatically. Every time the user submits a request to the server, the server extends the session. The session is "sliding".
In case you're asking how to set it to 30 minutes instead of the default (20) - there are various places to change the default, for example web.config or IIS.


SqlDataReader returns empty

Question: Here's my code:
static void Main(string[] args)
        {
            SqlConnection _sqlConnection = new SqlConnection (@"Data Source=(local)\SQLEXPRESS;Integrated Security=SSPI;" + "Initial Catalog=OnlineBanking");
            _sqlConnection.Open();
            var cmdSelect = new SqlCommand("Select * from BankAccounts where AccountId = 1", _sqlConnection);
            using (SqlDataReader dataReader = cmdSelect.ExecuteReader())
            {
                if (dataReader.HasRows)
                {
                    dataReader.Read();
                    float AccountBalance = (float)dataReader["Balance"];
                    Console.WriteLine("{0}", AccountBalance);
                }
            }

This line
 float AccountBalance = (float)dataReader["Balance"];

causes problems, as dataReader returns empty. How can I fix it? What's the issue?
Comment: And what happens when you step through the code line by line?
Comment: What do you mean with _datareader returns empty_ ? Do you have an exception with a meaningfull error message?
Comment: When run in SSMS, what gets returned? Please post data results that you think you should get.
Comment: You are using SqlConnection incorrectly. It implements [IDisposable](https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx). You need to wrap it in a using statement.
Comment: @Steve, I mean that in the debugging mode I can see: dataReader->Results View -> Empty.
The exception I get is "System.InvalidCastException "
Comment: What is the datatype of the field "Balance" on the database table. You are casting it to a float but it tells you that this is not possible.
Comment: @WEI_DBA in SSMS it returns a row with the AccountId being 1 and balance being some number. The Balance column is set to float in the database.
Comment: @Steve [Balance]    FLOAT (53) DEFAULT ((0)) NOT NULL,
Comment: sql float is mapped to net double https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings but as explained below by Marc use a decimal type to store and handle monetary values
Answer: System.InvalidCastException (comments) is not "empty" - it is an invalid cast. So presumably the data isn't a float; it could be double, or - and I suspect this is the case: decimal. Note that decimal is a much better choice for numbers that represent money.
So: find out which it is, and use the correct cast. I suspect that this will work:
decimal accountBalance = (decimal)dataReader["Balance"];
Console.WriteLine("{0}", accountBalance);

You can also use the various methods on Convert to be more forgiving, i.e.
decimal accountBalance = Convert.ToDecimal(dataReader["Balance"]);

Comment: Thank you very much! What a dumb rookie mistake :(. That works indeed - casting to decimal. Do you perhaps know why in debugging mode it shows "dataReader->Results View -> Empty"?
Comment: @AlicjaKrzeszowiec probably because that is iterating it as an enumerable, and you've already iterated to the end (the last row) by the time it fails? Note that you can only iterate a data-reader *once*

SqlDataReader returns empty


Finding Similar Sequences

Question: Can we find two sequences:
$$\{a (b^0), a (b^1), a (b^2), a (b^3), \dots, a(b^n)\} \bmod p_1$$
$$\{c (d^0), c (d^1), c (d^2), c (d^3), \dots, c(d^n)\} \bmod p_2$$
that differ by only one number?
AN EXAMPLE
For example, I start with:
$$\{4^0, 4^1, 4^2, 4^3\} \equiv \{1, 4, 1, 4\} \bmod 5$$
I then take the sum of a similar sequence.  For example, the same sequence modulo 10 is:
$$\{4^0, 4^1, 4^2, 4^3\} \equiv \{1, 4, 6, 4\} \bmod 10$$
Note that the sequences, for length 4, differ by only one number.
Is it possible to find two longer sequences that differs by only one number?
IF WE CAN GET A LONGER SEQUENCE, WHAT IS IT?  HOW LONG CAN WE MAKE IT?
Comment: @JMCF125:  Thanks for the compliment.  I've tried to find longer sequences, but I'm not really sure how to approach this.  Any help or suggestions would be greatly appreciated.  I have a feeling that it may be possible.  One idea I have is to get all the numbers to be the same except for the last one, and use modulus to create a difference.
Comment: Although this is an interesting question, your general expression is kind of confusing (I'd be lost without an example). Why don't you think it is possible, BTW? Did you try many more?
Comment: I think as it is very general (imagine the amount of options even limiting everything before ten!), maybe you can simply make some tries with random numbers. Otherwise, I suspect this will be easy to answer (unless you made another question like: "is there an infinite amount of these longer sequences? can it be proven?").
Comment: @JMCF125:  I've extended the question to see if we can get a very long sequence.  It's ok if it's an easy answer.  More power to the ones who can solve this!
Answer: As long as you wish,
In the end it is pretty easy:
$$
\forall b, \forall x, \forall y, \forall z : \{b;x;y;z\}\subset \mathbb N\land
\{y;z\}\not\subset \bigcup_{n=0}^\infty\{b^n\}\cup \{\forall n>b^{x-1}\}\\
\bigcup_{n=0}^x\{b^n\} \equiv
\bigcup_{n=0}^{x-1}\{b^n\} \cup\{y\}\  \text {mod}\ { b^{x-1}-y}\equiv
\bigcup_{n=0}^{x-1}\{b^n\} \cup\{z\}\ \text {mod}\ { b^{x-1}-z}\\
$$

Explaining a little better, all numbers should be naturals (obviously), $y$ and $z$ shouldn't be powers of $b$ (powers of $b$ are already included) nor shall they be greater than $b^x-(b-1)(b^{x-1})=b^{x-1}$ (otherwise, the the exponent in the modulus
could be reduced - got this from the example below, only using $b=3$). The conclusion (and, in fact, many premises as well) is drawn from mathematical induction.
Here's an example (actually, this is one  of the firsts I used to get to the generalization - from the above, the concretized variables are $b=2;\ x=5;\ y=3; z=2$):
$$
\{2^0; 2^1; 2^2; 2^3; 2^4\} \equiv \{2^0; 2^1; 3; 2^2; 2^3\} \mod{ 2^4-3}\\
\{2^0; 2^1; 2^2; 2^3; 2^4\} \equiv \{2^0; 2^1; 2^2; 5; 2^3\} \mod{ 2^4-5 }\\
$$
Comment: I'm glad to get you interested!  Thanks for the help.
Comment: Although you already marked this accepted I'll attempt to generalize for anything (like $m_1, m_2,$ etc. in any position) before tomorrow (if I'm not available, by Monday max). You got me addicted to math again!
Comment: I found the remainders repeat after $y$ and $z$ (seemingly  I haven't confirmed it yet) from $b^{y-1}$; meaning ALL "modulated" sequences are finite. I'll search on and update the answer (eventually). Though first I'll clear up my answer (it has currently more holes than a Swiss cheese).
Comment: Just a note: I used $\bigcup$ because I don't like set-builder notation.

Finding Similar Sequences


Independent sub σ-algebra measured by the same random variable

Question: Let $(\Omega,\mathcal{A},Pr)$ be a probability space and let $\mathcal{F}$ and $\mathcal{G}$ be two sub-$\sigma$-algebras of $\mathcal{A}$. Suppose that they are independent, and suppose that  is real valued random variable on $\Omega$ which is measurable with respect to both $\mathcal{F}$ and $\mathcal{G}$. Show that  is almost surely constant; that is there is real number $c \in \mathbb{R}$, so that $Pr(X=c)=1$.
Firstly, I am not sure what independent $\sigma$-algebra means here and how I can connect the independence of $\sigma$-algebra with random variable.
Secondly, since $\mathcal{F}$ and $\mathcal{G}$ are different $\sigma$-algebra, is it allowed to be measured by the same random variable? What does this imply? Maybe my question is a little bit vague, but I always think that different $\sigma$-algebra should be measured by different random variable somehow. And I don't even know why I take it as granted.
There is a solution somewhere. But I feel it makes no sense.
Answer: Hint: Consider $A= [X \le \alpha]$, this is both ${\cal F,G}$ measurable and they are independent so
$P[A \cap A] = P[A]= P[A]^2$, and the only solutions are $0,1$. What does this say about the distribution function of $X$?
Answer:
$\mathcal{F}$ and $\mathcal{G}$ being independent means $P(A_1 \cap A_2) = P(A_1) P(A_2)$ for any $A_1 \in \mathcal{F}$ and $A_2 \in \mathcal{G}$.
$X : \Omega \to \mathbb{R}$ being measurable with respect to $\mathcal{F}$ and $\mathcal{G}$ means that for any Borel set $B \subseteq \mathbb{R}$, the preimage $X^{-1}(B)$ is in both $\mathcal{F}$ and $\mathcal{G}$. It is not unreasonable for a random variable to be measurable with respect to different sigma-algebras. For instance, a constant random variable is measurable with respect to any sigma-algebra.

Let $c_1 \ne c_2$ and let $A_1 = X^{-1}(\{c_1\})$ and $A_2 = X^{-1}(\{c_2\})$. What is $A_1 \cap A_2$? What does that say about $P(X=c_1)$ and $P(X=c_2)$?


How to recenter UserControl(s) in Window

Question: We have BaseDialogView with next xaml code:
<Window x:Class="Test.BaseDialogView"
    Height="475"
    WindowStartupLocation="CenterOwner"
    SizeToContent="Height"
    ResizeMode="CanResize"
    SizeChanged="Window_SizeChanged">
  <ContentControl Content="{Binding ContentPage}" />
</Window>

BaseDialogViewModel  class:
public class BaseDialogViewModel : AbstractNotifyPropertyChangedItem
{
    private UserControl contentPage;
    public UserControl ContentPage
    {
        get { return this.contentPage; }
        set
        {
            if (this.contentPage != value)
            {
                this.contentPage = value;
                this.RaisePropertyChanged(() => this.ContentPage);
            }
        }
    }
}

The usage is very simple:
BaseDialog dialog = new BaseDialog();
BaseDialogViewModel dialogVm = new BaseDialogViewModel();

dialog.Owner = Application.Current.MainWindow;
dialog.DataContext = dialogVm ;

dialogVm.ContentPage = new ActivationView();
dialogVm.ContentPage.DataContext = new ActivationViewModel();

So basically once you have an instance of BaseDialog, you just set ContentControl (by setting dialog.ContentPage and dialog.ContentPage.DataContext).
ActivationView is very simple. For example:
 <UserControl x:Class="Test.ActivationView" d:DesignHeight="400" d:DesignWidth="700" MaxWidth="700">
    <Grid> .... what ever you need
 </UserControl>

The problem is that different UserControls windows are set, which have different width and height. When the first UserControl is shown it's place in the center of the MainWindow, which is ok. Then each new userControl is shown, but it's not centered. How do I center the BaseDialog window for each usercontrol?
I tried this (BaseDialogView):
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
  Window w = sender as Window;
  this.Top = (Application.Current.MainWindow.Height - w.ActualHeight) / 2;
}

but does not work ok (Some usercontrols are still not pixel centered). I also tried adding this to BaseDialogView Xaml
<Window .... VerticalAlignment="Center">

but it seems to be working only for initial instance.
Comment: A general note: a view-model should not contain any view items. Your `BaseDialogViewModel` class contains a property of type `UserControl` which breaks the MVVM pattern.
Comment: could you add a screenshot of your issue?
Comment: I agree, thank you for this, but the code was developed by someone else. Although there must be some pattern for `template window`
Comment: @broadband that's a good opportunity for you to fix it and do it correctly then. Part of the issue you're having here is your wonky use of the platform. I think what you're looking for is a dependency property that is bound in your xaml. Doing this work in the view model creates a lot of unnecessary code that makes it hard to diagnose
Comment: Is having a reference to control (UserControl, TextBlock, . . ) from view model a bad thing. I mean we violate the mvvm pattern? View model must have only reference to controls values (textblock value) or actions (button command)
Comment: @broadband your view is where all the references to UI elements go. Your view model is where all your logic goes. Your models are your data representations. The view model shouldn't know anything about, reference, or manipulate any, ui elements. Your view controls how to show it. Your view model controls what should be shown. Your model says what your showing is.
Answer: First of you should really consider propperly implementing the MVVM pattern.
It will make your live easier, also instead of centering the element manually in the size change event you should set its owner and WindowStartupLocation by using
Window win = new Window();
win.Content = new MyUserControl();
win.Owner = this;
win.WindowStartupLocation = WindowStartupLocation.CenterOwner;

Instead of having one window where you keep changing the content i would consider having different windows..but that may vary on your specific case


Automorphic group of a cyclic group

Question: Suppose $G$ is a cyclic group of order $n.$ Then $Aut(G)\cong (\mathbb{Z}/n\mathbb{Z})^\times$. Why is this true?
Comment: That's true, but I prefer hints over full answers.
Comment: Then you have certainly come to the right place.
Comment: @S.Sheng: Good for you.
Comment: Hint: consider maps of the form $a\mapsto a^k$.
Comment: You can find many answers if you write this to Google uncle.
Answer: let $a$ be a generator for $G$. any automorphism $\gamma$ must map $a$ to $a^g$ where $g$ is  relatively prime to $n$ (an automorphism must preserve orders of elements). the rest follows
Answer: Hint: $f:G\to G$ by $f(a)=a^k$ is an isomorphism if and only if $(k,n)=1$ where $G$ is a cylic group of order $n$.
First try to show above lemma after that try to show the converse i.e any isomorphism correspond to a such $f$ for some $k$.
Answer: Hint: First use the face that $\varphi:\, a\mapsto a^{k}$ is an automorphism
of a cyclic group iff $(k,n)=1$.
Then, for a general group $G$,
$$
\varphi(\langle x\rangle)=\langle x\rangle\iff(k,n)=1
$$
and note that
$$
G=\cup_{x\in G}\langle x\rangle
$$

Automorphic group of a cyclic group


Constant sheaf takes disconnected set to product

Question: Let $S$ be a discrete topological space, and let $\underline{S}$ be the constant sheaf on a topological space $X$, meaning $\underline{S}(U)$ is the collection of continuous maps $U\to S$.
Am I correct in thinking that $\underline{S}(U)\cong S^n$ where $n$ is the number of connected components of $U$? Say $U=V\cup W$ where $V\cap W=\emptyset$, then we can have $s|_V:V\to \{p\}$ and $s|_W:W\to \{q\}$ where this is clearly continuous.
Answer: If the connected components of $U$ are open sets, then this is correct, since a continuous map $U\to S$ is uniquely determined by choosing a constant value on each connected component of $U$.  In particular, this is always the case if $U$ has only finitely many connected components.
However, if the connected components are not open, then not every choice of a constant value on each connected component will give a continuous map $U\to S$.  For instance, if $U$ is a Cantor set, then the connected components are just singletons, but not every map $U\to S$ is continuous.
Comment: Perhaps a more familiar example than the Cantor set is the set of rational numbers (as a subspace of the real line).
Comment: Thanks for your answer. As a side question, how would one show that the stalks are all $S$?
Comment: The idea is that every section of $\underline{S}$ is constant in some neighborhood of $x$, so every germ of functions at $x$ is the germ of a constant function.
Comment: So an equivalent claim would be that every $x\in X$ belongs to some *open* connected set?
Comment: Equivalent to what?


how to print struct timeVal

Question: i have this line in my code
`printf("Rcvd pkt from %s:%d at <%ld.%06ld>\n", inet_ntoa(servAddr.sin_addr),  ntohs(servAddr.sin_port), timeVal.tv_sec, timeVal.tv_usec);`

this is the warning i get with gcc while compiling it
`cc1: warnings being treated as errors
`client12.c: In function 'main':
`client12.c:131: warning: format '%06ld' expects type 'long int', but argument 5 has type '__darwin_suseconds_t'
`client12.c:131: warning: format '%06ld' expects type 'long int', but argument 5 has type '__darwin_suseconds_t'

what am i doing wrong??
PS - i have included time.h  and sys/time.h
Answer: The type of the members of struct timeval will vary from system to system.  As with many other C data types, the safe and portable thing to do is to cast the values when printing them:
printf("Rcvd pkt from %s:%d at <%ld.%06ld>\n",
    inet_ntoa(servAddr.sin_addr),  ntohs(servAddr.sin_port),
    (long) timeVal.tv_sec, (long) timeVal.tv_usec);

This will work correctly for any data type smaller than or equal in size to a long.  And this idiom is so common that people will think twice about making any of these common data types longer than a long, although be careful with data types that refer to file sizes (like off_t); those could be long long in some cases.
To be maximally safe, you'd cast to long long and use a %lld format, but that swaps one portability problem for another, since not all printf implementations support %lld yet.  And I've not seen an implementation where those time values require that treatment.
Answer: Cast the numbers to the correct types:
printf("Rcvd pkt from %s:%d at <%ld.%06ld>\n", inet_ntoa(servAddr.sin_addr),  ntohs(servAddr.sin_port), (long int)(timeVal.tv_sec), (long int)(timeVal.tv_usec));

Comment: Mainly because C is intended to be portable to a fairly wide range of systems.  It runs on systems where the maximum data size is 32 bits and ones where it could be 128 bits or more.  For 64-bit systems, you want a 64-bit `time_t` and related types (so that you don't run out of time in 2038), but the systems that C was originally written for couldn't (easily) address data types that large.
Comment: Thanks everyone. Casting it to long int worked. Why can't we standardize these across all systems.
Answer: See sys/types.h for an explanation of suseconds_t. Apparently, it is

a signed integral type capable of storing values at least in the range [-1, 1,000,000].

This means it might defined as an int on your system. Try removing the long specifier l from the format string and just print it as a regular decimal.
Edit
As per rra's answer, this will not be portable. It will only work on systems that define suseconds_t in the same way. We know from the spec that the type is a signed integral type of at least 32 bits. The most portable way is to cast it to the biggest signed integral intrinsic that you can get away with.

how to print struct timeVal


Field names returned in search snippet. How to hide?

Question: Field names are being returned in the search snippets on my search results page, like so:

Academic Culture
Body Text: The academic culture of your host country may be quite
  different from what you experience here at ...

The bit that reads "Body Text:" is the field name. What's weird is that the field name is only returned when the snippet is at the beginning of the field. So, depending on the search it could return something like this:

Academic Culture
... academic culture of your host country may be quite
  different from what you experience here at ...

Note the absence of "Body Text:".
Is there a way to disable/hide field names in the search snippets?
Answer: There is usually a view mode for Search results, you can control the field label visibility settings there. See Admin >> Structure >> Content Types >> [Content Type] >> Manage Display >> Custom Display Settings. Activate the view modes for search and customize away.
Comment: @fullerja Any idea how to customize the Search Snippet field? I can't find a way to edit this. And it still shows for example the publish date or other fields that i don't need. Thanks in advance!


Error node:events:498 - Server listening but with the error message

Question: I am practising the node js HTTP method. The server response is there but nodemon is getting crashed each time I called. When I restart the node app and get a response from server below error message is shown. I have deleted node modules, cleared cache, and did npm install nothing worked for me. enter image description here
const http = require('http')

const server = http.createServer((req, res) => {
  if (req.url === '/') {
    res.end('Home Page')
  }
  if (req.url === '/about') {

    res.end('About Page')
  }
  res.end('Error Page')
})

server.listen(5000, () => {
  console.log('Server listening on port : 5000...')
})`

Answer: Both res.end('Home Page') and res.end('Error Page') will be executed in your code. Obviously, you can't call twice the res.end method. Put a return to each ifs.
if (req.url === '/') {
    res.end('Home Page')
    return
  }

Comment: Thank you @bmz1 it worked. while i use if,elseif and else also the error is not throwing. I got 2 ways to solve.


Acessar o this dentro de evento

Question: Como posso acessar o this relativo ao meu objeto, quando o mesmo chama um evento? Quando o evento é chamado, o this passa a ser o próprio evento, o  window.event
Ex:
function Foo() {
     this.bar = function() {
        console.log('Hi!');
     }

    document.onkeypress = function(e) {
        // Aqui, o "this" não é mais o objeto Foo, mas sim o "e" ou "window.event"
        this.bar();
        // Erro: this.bar não está definido...
    }
}

Como posso acessar o meu objeto de dentro do callback do evento keypress?
Answer: Você precisa "setar" o escopo da função anônima que o evento recebe, para fazer isso use o .bind(), segue exemplo:
function Foo() {
     this.bar = function() {
        console.log('Hi!');
     }
    //Com esse método você passa pelo parâmetro o escopo que a função tera,que no caso é this mesmo.
    document.onkeypress = function(e) {
        this.bar();
    }.bind(this);
}

Answer: Se você estiver usando ES6 ou algum transpiler, a Arrow Function pode ser uma solução também. Elas tem o this do contexto que elas estão vinculadas, basicamente o contexto de onde ela foi declarada.
function Foo() {
  this.bar = function() {
    console.log('Hi!');
  }
  document.onkeypress = () => { this.bar() }
}

ou se precisar do parâmetro de eventoe
document.onkeypress = e => { this.bar() }

Gosto bastante desse approach por deixar o código mais clean

Acessar o this dentro de evento


How to 'Group By' values in a column in DataGridView in Vb.Net

Question: For example I have a datagridview1 with data imported from a text file and there are 3 columns: ID, Subject, Grade.
What I want to do is to Group By the ID Column and put it in a ListBox.
I dont have any database here so I can't use sql queries or is there a way to manipulate datagridview using sql queries? Just like this:
SELECT ID FROM DataGridView1 Group By ID

Any response would really be appreciated.
Comment: http://stackoverflow.com/questions/7325278/group-by-in-linq and https://msdn.microsoft.com/en-us/library/bb531412.aspx
Comment: If your data only exists in the DGV rather than something like a List or DataTable, it will be harder to filter, sort and group the data.  Design your app around the data not the UI controls.  If the data file is a CSV it is pretty simple to import it to a DataTable
Answer: You can't directly use SQL queries, but using LINQ it is possible to query data in a similar way.
The following code gets all IDs into a ListBox (as if you had used the SQL statement SELECT ID FROM DataGridView1 Group By ID):
    For Each v As String In
        From row
        In DataGridView1.Rows
        Group By val = DirectCast(row, DataGridViewRow).Cells("ID").Value
        Into Group
        Where val IsNot Nothing
        Select str = val.ToString

        ListBox1.Items.Add(v)
    Next

Comment: that's exactly what i want, thank you so much but there is error when executing that code. error says "Object reference not set to an instance of an object." which the highlight is in the "val.ToString"
Comment: Which version of Visual Studio / .NET Framework are you using? With VS2013 / .NET 4.5 it's working without an error. You could also try to remove the `.ToString` from the affected line and see if it works then.
Comment: tried to remove that and it has an error "Items collection cannot be modified when the DataSource property is set."
Comment: You did't set by any chance a DataSource for the ListBox, did you?
Comment: i don't know how to. i'm sorry. I'm just a beginner in vb.net
Comment: Look at the designer view of your form and select ListBox1. In the properties window (press F4 if it isn't visible) there is an entry labled "DataSource". Its value _should_ be set to sth. like 'none'... And btw. which version of Visual Studio are you using?
Comment: I've edited my answer to prevent empty rows from being taken into account as well. This should solve your first issue (the null reference exception).
Comment: oh God, thank you so much. that solved the issue. million thanks :)


Make a SOAP API in Rails

Question: I would like to make a SOAP API based on OpenAxisGroup Configuration. Can anyone help me with a gem or tutorial for the same? I have already seen wash_out, but would like a more frequently maintained and modified gem.
Thanks.
Comment: https://github.com/intridea/grape
Comment: Grape is for Restful API's, I want to make a SOAP API
Answer: You can try Savon. It is a popular SOAP client for the Ruby programming language.

Github
Documentation
RailCast
Comment: Thanks for the suggestion, but I want to make a SOAP Server, not a client.
Comment: Savon is a client for *consuming* a SOAP service.

Make a SOAP API in Rails


Transferindo imagem via socket, a imagem fica preta

Question: O meu servidor manda um comando pra o cliente e o cliente manda uma PrintScreen para o servidor usando esse código:
Image printScreen = new Bitmap(monitorWidth, monitorHeight);
Graphics graphics = Graphics.FromImage(printScreen);
graphics.CopyFromScreen(0, 0, 0, 0, printScreen.Size);
printScreen.Save("Test1.jpg"); //Aqui sai uma linda imagem jpg
byte[] bufferTempTemp = imageToByteArray(printScreen);
byteArrayToImage(bufferTempTemp).Save("Test2.jpg"); //Aqui sai uma imagem jpg meio danificada mas tudo bem
byte[] bufferTemp = new byte[4 + bufferTempTemp.Length];
Buffer.BlockCopy(BitConverter.GetBytes(1), 0, bufferTemp, 0, 4);
Buffer.BlockCopy(bufferTempTemp, 0, bufferTemp, 4, bufferTempTemp.Length);
socketOfClient.Send(bufferTemp);

Pronto, o server recebe o pacote tudo normal: (vale lembrar que os primeiros 4bytes do pacote representam um inteiro que é o ID do pacote, 1 significa que é uma imagem chegando)
byte[] bufferTemp = new byte[bufferReadOfServer.Length - 4];
Buffer.BlockCopy(bufferReadOfServer, 4, bufferTemp, 0, bufferReadOfServer.Length - 4);
Image image = byteArrayToImage(bufferTemp);
image.Save("Test3.jpg"); //Aqui sai uma imagem preta :/

Agora quando eu vou ver a imagem que foi salva, ela tem 4Kb e é totalmente preta, o tamanho dela é exatamente o tamanho do meu monitor, 1920 x 1080
As funções que convertem bytes em imagem e vice-versa:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

Eu já testei a imagem antes do envio, ele realmente printa uma imagem válida, o problema está no recebimento.
Comment: Ícaro, no momento não consigo te responder, mas já adianto uma pergunta para você: Você já tentou fazer a conversão para byte e novamente para Image sem enviar através da socket? É bem possível que o problema seja na conversão e não na transmissão em si. Aguardo seu retorno e a noite terei o tempo para responder caso ainda não tenha a solução.
Comment: Olá Ícaro. Que bom que resolveu. Mas observe que este site não é um fórum. O ideal é que você mesmo responda à sua pergunta com a solução desse problema, de forma a também ajudar outras pessoas com a mesma dificuldade no futuro, e abra uma nova pergunta para a sua nova dúvida. Nesse caso (da nova pergunta) prepare um [mcve], que vai facilitar o entendimento de quem for responder e também facilita você a explicar a dificuldade.
Comment: Como você pode ver no primeiro código, eu testo antes da conversão e depois da conversão :/
Comment: Agora eu fiz uma conversão igualzinha à do servidor no final do código que envia do cliente e funcionou, o problema está no servidor, tem alguma coisa que limita a transferência de bytes?
Comment: Resolvi, era o tamanho do buffer de recebimento do cliente, 1024... Tive que colocar em 900000, será que há uma forma de fazer o buffer se extender quando os dados forem maiores que 1024?
Answer: Resolvido! O buffer do server estava limitado a 1024 bytes, então quando a mensagem era enviada, mal cabia as primeiras linhas de sua matriz dentro de 1024 bytes :)
Comment: Legal que você se preocupou em responder. Mas, eu preciso comentar que a resposta está com baixa qualidade. Basicamente você menciona o que mencionou no comentário. Se você pudesse colocar ao menos algum trecho de código relacionado, com o antes e depois, ficaria mais certo de ajudar também alguém com o mesmo problema no futuro. Caso contrário, a resposta ajuda a sua pergunta a receber voto pra ser fechada como mero "erro de digitação". Entende?


Encontrar todas as ocorrências de um padrão em uma String

Question: Necessito usar expressões regulares para encontrar padrões em um texto. Seria melhor para mim caso existisse um método igual ao search() do Python, que retorna um vetor com todas as ocorrências desse padrão. Existe algum método parecido em Java?
Comment: [O `search` do Python não faz isso que você está pedindo](https://docs.python.org/2/library/re.html#re.search). Você quis dizer `findAll`?
Answer: A classe Pattern serve exatamente para isso, ela representa uma regex. A classe auxiliar Matcher é usada para controlar a busca.
Para pesquisar por uma expressão regular no meio de um texto:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class TesteRegex {

    private static final Pattern ABC = Pattern.compile("A+B+C+");

    public static void main(String[] args) {
        String texto = "123 456 7890 ABx AAACCC AABBCC hjkhkk ABBBBCCC djsdhj ABC kdjk.";
        Matcher m = ABC.matcher(texto);
        while (m.find()) {
            System.out.println("Achou nas posições " + m.start() + "-" + m.end() + ": "
                    + texto.substring(m.start(), m.end()));
        }
    }
}

Eis a saída:
Achou nas posições 24-30: AABBCC
Achou nas posições 38-46: ABBBBCCC
Achou nas posições 54-57: ABC

Veja aqui funcionando no ideone.


Submitting all forms on a page with JavaScript using ajax at once to Django

Question: I have a page with multiple forms that have a guid attached to the id. I wrote this JavaScript from an amalgamation of stack post. I'm not that great at JavaScript.
When I submit my form some of my form text boxes disappear, and the data passed to django has no post values. If I log the form right before I send it serialized with ajax using console.log(formData[0]); it logs the form data and all of the elements.
When i print out the post values, and return the post values to the page using django its completely blank. The submission isn't serializing the forms. I assume this is because they exist as an array of forms attached to the main generated form frmCollector. When I submit its null to django. How can I combine all forms and use an ajax submit to send as a object to django. I would be ok with it building a json object. The forms have unique uuids so I don't know what those are. I could build a list but I'm sure there is a more efficient way to do this in javascript.
Here is the full script code so far:

here is an example of the dynamic html that is loaded

Auto Scaling Group

Launch Config

User Data

Block Devices

FStack Connections

Software

Administration

STACKNAME

Delete

            <form id="AutoScalingGroup-UUID">
                AutoScalingGroup

name
                <input type="name" class="form-control" id="name-UUID" placeholder="ExampleASGConfig" data-toggle="tooltip" data-placement="top" title="name (str) – Name of the auto scaling group to create.">

                availability_zones
                <select class="form-control" id="availability_zones-UUID" placeholder="us-west-2" data-toggle="tooltip" data-placement="top" title="availability_zones (list) – List of availability zones (required).">
                <option value="us-east-1">us-east-1:US East (N. Virginia)</option>
                <option value="us-east-2">us-east-2:US East (Ohio)</option>
                <option value="us-west-1">us-west-1:US West (N. California)</option>
                <option value="us-west-2">us-west-2:US West (Oregon)</option>
                <option value="ca-central-1">ca-central-1:Canada (Central)</option>
                <option value="eu-west-1">eu-west-1:EU (Ireland)</option>
                <option value="eu-central-1">eu-central-1:EU (Frankfurt)</option>
                <option value="eu-west-2">eu-west-2:EU (London)</option>
                <option value="ap-northeast-1">ap-northeast-1:Asia Pacific (Tokyo)</option>
                <option value="ap-northeast-2">ap-northeast-2:Asia Pacific (Seoul)</option>
                <option value="ap-southeast-1">ap-southeast-1:Asia Pacific (Singapore)</option>
                <option value="ap-southeast-2">ap-southeast-2:Asia Pacific (Sydney)</option>
                <option value="ap-south-1">ap-south-1:Asia Pacific (Mumbai)</option>
                <option value="sa-east-1">sa-east-1:South America (São Paulo)</option>
                </select>

                default_cooldown
                <input type="default_cooldown" class="form-control" id="default_cooldown-UUID" placeholder="300" data-toggle="tooltip" data-placement="top" title="default_cooldown (int) – Number of seconds after a Scaling Activity completes before any further scaling activities can start.">

                desired_capacity
                <input type="desired_capacity" class="form-control" id="desired_capacity-UUID" placeholder="1" data-toggle="tooltip" data-placement="top" title="desired_capacity (int) – The desired capacity for the group.">

                health_check_period
                <input type="health_check_period" class="form-control" id="health_check_period-UUID" placeholder="300" data-toggle="tooltip" data-placement="top" title="health_check_period str – Length of time in seconds after a new EC2 instance comes into service that Auto Scaling starts checking its health.">

                health_check_type
                <input type="health_check_type" class="form-control" id="health_check_type-UUID" placeholder="EC2" data-toggle="tooltip" data-placement="top" title="health_check_type str – The service you want the health status from, Amazon EC2 or Elastic Load Balancer.">

                launch_config
                <input type="launch_config" class="form-control" id="launch_config-UUID" placeholder="launch_config_name" data-toggle="tooltip" data-placement="top" title="launch_config str or LaunchConfiguration – Name of launch configuration required.">

                load_balancers
                <input type="load_balancers" class="form-control" id="load_balancers-UUID" placeholder="" data-toggle="tooltip" data-placement="top" title="load_balancers list – List of load balancers.">

                max_size
                <input type="max_size" class="form-control" id="max_size-UUID" placeholder="1" data-toggle="tooltip" data-placement="top" title="max_size (int) – Maximum size of group required.">

                min_size
                <input type="min_size" class="form-control" id="min_size-UUID" placeholder="1" data-toggle="tooltip" data-placement="top" title="min_size (int) – Minimum size of group required.">

                placement_group
                <input type="placement_group" class="form-control" id="placement_group-UUID" placeholder="t1.micro" data-toggle="" data-placement="top" title="placement_group str – Physical location of your cluster placement group created in Amazon EC2.">

                vpc_zone_identifier
                <input type="vpc_zone_identifier" class="form-control" id="vpc_zone_identifier-UUID" placeholder="sub-00000,sub-11111" data-toggle="tooltip" data-placement="top" title="vpc_zone_identifier str or list – A comma-separated string or python list of the subnet identifiers of the Virtual Private Cloud.">

I had to truncate some of the html to fit the post but you get the idea.
Answer: Your collect function can be simplified and then you can post all the data as json either with serializeArray or serialize, for example:
var alldata = {};

$("form").each(function(i, form){
  var current_data = $(form).serializeArray();
  alldata[$(form).prop("id")]  = current_data;
});
alert(JSON.stringify(alldata));

$.ajax({
  //your parameters here: url, etc.
  url: 'save_stacks',
  type: 'post',
  dataType: 'json',
  data: alldata,
  success: function(response) {
    //handling code
  }
});

https://jsfiddle.net/1pLn4hw1/
You will likely have to adapt your handling code in the backend.
Comment: amazing, I haven't written the back-end code yet I've just been trying to get a combined form first. Thank you. This is just my lack of knowledge about javascript.
Comment: so the alert is just blank values.
Comment: I guess I should mention that the forms are loaded via dynamic html that gets replaced on load. I load from a local file and import html dynamically and change the id as its loaded. The code isn't called until after that however when a button is clicked.
Comment: Displayed data in the alert is empty due to the fact that fields need to be filled in.  You can place the code in the event handler associated to the button that triggers the submission.
Comment: I did I have it onclick call the function. I fill in the data and its still blank.
Comment: Save Stack
Comment: I updated the fiddle, the values are essentially the ones in the fields https://jsfiddle.net/1pLn4hw1/1/
Comment: look at my code above @james_bond. I have a feeling its something I'm doing. I'm a total noob at javascript.
Comment: check my updated answer, look at the dataType parameter.
Comment: I found the problem, I had to change the types i must have been out to lunch I changed them to text, and then realized i didn't name the inputs. its working now thank you.