Subclassing UITableViewController for PopoverController

Question: I have a ViewController that is composed of a few different views on my screen.  A scrollView for text data, a TableView for some other data, etc.  In my app, I want to add a UIPopOverController to show a list of my data.  The current ViewController I am in is not a subclass of UITableViewController.  Do I have to create a separate subclass of UITableViewController in another file, and use an instance of that class in this ViewController?  Thanks.
Comment: Do you mind clarifying your question a little? I've tried to answer, but I'm not sure what you're trying to subclass or thinking about subclassing.
Answer: You can show any kind of UIViewController in a UIPopoverController. A table view is not at all required for displaying a Popover controller. If you want to display a UITableviewController, you most certainly can. Just pass it in to the popover controller.


Unwanted Characters in CSV

Question: Trying to save a csv file from spark but when I open up the file in vim I get the following characters and I don't understand what they mean or how they are getting into the file.
Here is my writer from spark:
df.write.partitionBy(partitionCol).format("csv").mode(writeMode)
  .option("sep", ",")
  .option("encoding", "UTF-8")
  .option("quote", "")
  .option("escape", "\\")
  .option("escapeQuotes", true)
  .option("quoteAll", true)
  .option("header", hasHeader)
  .option("nullValue", "")
  .option("dateFormat", "yyyy-MM-dd")
  .option("timestampFormat", "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
  .option("compression", "gzip")
  .save(outPath)

Comment: Is your csv file encoding unicode or something else? try ro set encode option when you write your dataframe.
Comment: how are you saving? and what is the data before you save? provide all the details
Answer: From the documentation:

"quote – sets a single character used for escaping quoted values where the separator can be part of the value. If None is set, it uses the default value, ". If an empty string is set, it uses u0000 (null character)."

So, as you're setting quote to be an empty string, you're getting null bytes as quotes (traditionally represented as "^@" in caret notation.)

Unwanted Characters in CSV


Continuous injections of nice spaces into $\ell^2$

Question: If X is a separable, first countable Hausdorff space, then does there exist a continuous map from X to $\ell^2$?
Intuitively put, can you always fill in the holes of nice spaces?
Comment: If your conclusion is true, then the continuous functions on the space separate the points. I don't think that this is true of every separable first countable Hausdorff space, but I might be wrong. You should look for Urysohns theorem and the context to see conditions under which this holds.
Comment: You cannot have an embedding, but you want the map to be injective? "into" is ambiguous. You always have a constant map to $\ell^2$ so you need some condition.
Comment: No, not an embedding, just a continuous injection
Answer: Let $X$ be the Double Arrow space, $[0,1] \times \{0,1\}$ in the lexicographic order topology. Then $X$ is a non-metrisable, compact, first-countable, separable, Hausdorff (even hereditarily normal, being an ordered topological space) and if a 1-1 continuous function $f: X \to Y$ existed where $Y$ is metrisable, then $f$ would be a homeomorphism (as $Y$ is Hausdorff and so $f$ is closed etc.) and hence metrisable too. So from $X$ there can be no injective map into $\ell^2$.
Comment: @AIM_BLB It's the easiest: a space $X$ has an injection into $\ell^2$ implies it has a coarser metrisable topology (i.e. it's submetrisable, as this is called). It is well-known that every separable metrisable space has an embedding into $\ell^2$.
Comment: @AIM_BLB the embedding fact is quite well-known, it's in Bessaga and Pelczynski's book, or in van Mill's book on infinite-dimensional topology. It's originally due to Banach IIRC. It uses a countable family of separating functions.
Comment: Nice counter example, I'm wondering if metrizability essential for a counterexample to be possible?
Comment: Oh yea?  Is it possible to have a link to this paper?  This would help so much
Comment: Perfect, thanks so much Henno; this makes a big difference for me :)


AngularJS - Directive inside ng-switch not working

Question: I have a directive inside ng-switch as follows:

        <zippy></zippy>

    //Other ng-switch-when directives

The directive is defined as:
app.directive('zippy', function() {
    return {
        scope: false,
        template: "Hello FooBar"
    };
});

Now, when the page loads, although myModel does become to foo, the template does not load. Following is the state of the element in the borwser:

    <zippy></zippy>

The class ng-scope gets appended - thats it. The element does not change - the directive is not replaced with the template. What am I doing wrong? How can I get the directives to work?
Comment: By default, directives are "attribute only", so at least you need to add `restrict: 'E'` to your directive definition.
Comment: My bad! I knew that there is a default value for `restrict` but I was assuming that it is element and not attribute.
Comment: i am having the exact same problem but i have 'EA' for the restrict property. The ng-scope class gets applied to my custom directive element but it does not get rendered. Appears to be a bug in angular.
Answer: You will need to add the restrict to your directive:
Demo:  http://plnkr.co/edit/Jc77jrY4U0xGalN5QkZA
app.directive('zippy', function() {
    return {
        scope: false,
        template: "Hello FooBar",
        restrict: 'E'
    };
});

Documentation:  http://docs.angularjs.org/guide/directive
restrict - String of subset of EACM which restricts the directive to a specific directive declaration style. If omitted directives are allowed on attributes only.
E - Element name:
A - Attribute:
C - Class:
M - Comment:


Somar +1 Variável a cada objeto criado

Question: estou criando uma classe Candidato, onde passarei um nome e um número do candidato.
Nela criei um vetor, para armazenar o número do candidato. Para que não que não aja um número de candidato igual.
Criei uma variável, aux e uma cont, para varrer o for e assim verificar se aquele número já possui. Porem, eu digito número iguais e ele não acusa, quebrei a cabeça e nada. Mandei imprimir as variáveis aux e cont e percebi que todas as vezes que crio um objeto, os valores sempre são iguais. Sei que é para serem iguais, porem eu queria saber uma forma de somar +1 a cada objeto criado. Para que varra o vetor e verifique se há número do candidato igual.
import java.util.Scanner;

public class Candidato {

    Scanner s = new Scanner(System.in);

    private String nome;
    private int numeroCand;
    private int votos =0;
    private int num[] = new int[100];
    private static int cont =1;
    private static int aux=0;

    Candidato(){
        System.out.println("Digite o nome do candidato");
        this.setNome(s.nextLine());
        System.out.println("Digite o número do candidato");
        this.setNumeroCand(s.nextInt());
        verificarNum(this.getNumeroCand());

    }

    public void verificarNum(int n){

        if(cont == 1){
            num[cont-1] =numeroCand;
            aux++;
            cont++;

            System.out.println("Candidato cadastrado com sucesso!");
            System.out.println("_________________________________________________________________");

        }else{

            for(int i=0;i<=aux;i++){
                if(this.getNumeroCand() == num[i]){
                    System.out.println("Número válido, pois esse já é de outro candidato.");
                    while(this.getNumeroCand() == num[i]){
                        System.out.println("Digite o número do candidato");
                        this.setNumeroCand(s.nextInt());
                    }
                }
            }
        }

        /*for(int i=0;i<cont;i++){
                if(this.getNumeroCand() == num[i]){
                System.out.println("Digite um número válido, pois esse já é de outro candidato.");

                    }
                }else{
                    System.out.println("Candidato cadastrado com sucesso!");
                    System.out.println("_________________________________________________________________");
                    num[cont-1] =numeroCand;
                    }
            num[cont-1] =numeroCand;

        }
        cont++;*/
        System.out.println("Cont: " + cont + "aux: " + aux);
    }

Comment: Se é **um** candidato porque tem um vetor com números nele? Me parece que a classe está conceitualmente errada, aí tudo o que fizer estará potencialmente errado. Até dá para arrumar uma solução para o perguntado, mas será arrumar a seta de um carro que nem anda direito.
Answer: Não pode ser o caso com o qual você esteja tendo problemas. Mas há um problema aqui:
 while(this.getNumeroCand() == num[i]){
                    System.out.println("Digite o número do candidato");
                    this.setNumeroCand(s.nextInt());
 }

O laço while só está fazendo comparação com o número no índice i, o que possibilita o usuário entrar com o número que já foi inserido e checado pelo laço for, ou seja qualquer número menor do que i dentro da array num.

Somar +1 Variável a cada objeto criado


Update initialValues in redux-form asyncronously without using redux

Question: The examples in the documentation show only how to update initialValues with redux state (see http://redux-form.com/6.0.0-alpha.4/examples/initializeFromState/). I don't want to use redux because it's really messy for the application I'm building. I'd rather use component state.
I've tried using an async self-executing function in the redux-form connector, but that didn't work:
ApplicationNewForm = reduxForm({
    form: 'appapp',
    returnRejectedSubmitPromise: true,
    initialValues: (async function() { ... }()),
    handleSubmit
})(ApplicationNewForm)

that didn't work (that one should be obvious why, the component doesn't care if the assignment is async...)...
I also tried making the api call in an async componentDidMount and using this.state to set the value prop of my <Field />'s.
In each case the values don't seem to make it to the redux store, and so don't update the values in the fields.
Any ideas?
EDIT:
The solution is to manually call this.props.initialize in the async componentDidMount with the data from the api call. This DOES work:
async componentDidMount() {
    try {
        const res = await axios.get('/my/user/api/')
        const { data: user } = res

        this.props.initialize({...user})
    } catch(err) {}
}

Comment: I'm thinking I could just use the `change` prop in my form component (see http://redux-form.com/6.5.0/docs/api/Props.md/#-change-field-string-value-any-function-) when the async function is resolved... Will update if that works
Answer: I think the problem lies in the async call. The async function returns a promise right away (which is later fulfilled with the result). You assign this promise to initialValue, and the promise resolution does nothing.
The docs for ReduxForm say that initialValues can only be set once without manually doing an INITIALIZE, so I doubt that using a promise (or redux state update) will do the trick, but I havent used or tried ReduxForm myself so I'm not sure.
Comment: Thanks for clarifying about async/await. I'm just learning about generators and such. I found a solution, updated in my question.
Answer: As of the current version, v6.5.0, the initialValues config parameter takes an object, not an async function. That might be messing with your call to this.props.initialize(). Are you seeing the INITIALIZE action being dispatched?
Comment: Thanks for your reply. I was just confused about async/await, that was removed and I now have an object there. This was solved by just using the `initialize` redux-form method and `initialValues` as intended. Please see the EDIT in my post, it _is_ working so maybe I should close this question.
Comment: Yep. I think you can.


Bar number featuring this tone row by Schoenberg in the sheet music

Question: Given is an image of the tone row used by Schoenberg in his Variations for Orchestra op. 31. This is what was given in a music theory workbook.

I found the sheet music for the Schoenberg piece. But I'm not able to find out exactly in which bar of the piece is this tone row used. I can find the use of some of the notes of the tone row in the first two pages of the sheet music. I would appreciate if anyone could tell me in which bars does this tone row appear in.
Comment: are we doing your homework?
https://en.wikipedia.org/wiki/Variations_for_Orchestra_(Schoenberg) . But thank you anyway for the inspiration. You can also translate this site:
https://www.schoenberg.at/index.php/de/joomla-license-sp-1943310035/variationen-fuer-orchester-op-31-1926-1928
Comment: @AlbrechtHügli Neither of those links are any help here
Comment: "Variations" is a huge piece. If you are starting off analysing 12 tone music by yourself, you would probably find it much easier to start with one of Schoenberg's string quartets
Comment: As Hugli points out- we prefer people do their own research on specialized topics like this.  In this case, the entire score is available at imslp.org
Comment: @CarlWitthoft I assume OP already has the score. There seems to be no good analysis of this work available online which makes the question a good fit on SE
Comment: We don't usually track down measure numbers for sheet music excerpts. This would have been better for chat, or another site.
Comment: @AlbrechtHügli No, this isn't part of my homework! I am curious to understand Schoenberg's style of music, since this is totally new to me. I thought of analysing his style by looking at the sheet music myself, but was kind of lost. Hence thought of asking the question.
Answer: The first two pages show Schoenberg constructing the tone row from its component parts. He starts by exploring the tritone interval of the first two notes for the first four bars, adding the tritone G-C# (an inversion) in bar 3. Then he adds the major second interval (the second two notes). In bar 8 he starts the first five notes of the tone row (fl/harp) together with the same notes inverted and transposed (bcl/bsn/harp). Then he starts a transposed inversion of the tone row in bars 9 and 10 (bcl/bsn/cb). The first complete statement is in bars 10 to 13 (fl/harp/vln1 taken over by  bcl/bsn/vcl)


How do I play this section from wedding march?

Question: I have been struggling with this for so long, and I cant find any answers. Plz tell me how do I play it? I would be thankful if anyone could explain what that chord below  G means and what Tr means. Plz also write the chord, thank you!
Edit - This is a keyboard solo piece from Trinity London grade 7 - electronic keyboard book! Also as to specify my question more, I dont understand how am I supposed to play the notes below G chord, like F , D together and then G? ANd if so in how many beats? I also tried searching on youtube but I couldnt find any answers. I also have a link to a reference video on youtube.
I am sorry if I seem silly, but I am self taught so I really have no idea sweat smile
The song played by some other youtuber-
Comment: What is unclear about the G chord?
Comment: Glad to see that OP made the effort to see whether their question was a duplicate, but unfortunately they mean it's possibly a duplicate in that the underlying questions about understanding that type of chord voicing and the meaning of "tr" in sheet music have probably been asked here before.
Comment: Did you 'Youtube' it, and watch how others do it?
Comment: Further to @Aaron's comment: is this an arrangement for piano solo? (If so, where's the bass line?) Or is it piano duet or piano + other instrument(s)? If so, even though all the notes are on the top stave, your left hand could play some of the lower notes, seeing as it has nothing else to do.
Comment: Although I've voted to reopen this question, it is likely a duplicate. I have not yet found the identical question, however.
Comment: A duplicate? Hmm, I don't think so. I searched if people were having trouble with this piece like me, but I didn't find anything.
Answer: 'Tr' means 'trill'. Play the note and the diatonic note above it alternately. As in DCDCDCDCD, quite fast, while the lower two notes are held only for the short quaver shown.  I'd be playing it with two hands (left hand is doing nothing else), so E and G l.h., trill,r.h., whichever fingers you are better with - the suggestion here is 5434, but there are other combinations.
The end of the trill is BC, up to D - that chord being basically G7, which can all be played easily with r.h.
EDIT: I've just realised where the question is coming from! Although the heads are on either side of the stem (F and D on left, G on right), all three are played at the same time - it's the only way the F and G can be written! Were all the dots on the same side, it'd look like an indecipherable blob.
Comment: Trill starts on D and ends on C.
Comment: @Aaron - thanks, hence the 54 fingering. But the 34 after only makes sense with DCBC notes, doesn't it? I rarely trill these days, leave that up to budgies, but I tend to start with the lower note if I have to. Edited so far.
Comment: Hmm. I see now I misread the fingering initially. I saw 5343, which is common. I agree that 34 only makes sense at the end of the trill, except then the given fingering for the following chord doesn't work. Musescore strikes again, perhaps? Leaving comment for OP....
Answer: The chord below G is G7 but without the 3rd (B) in it.  It hints at the B in the notes running up to it (B-C).
Tr is short for Trill the act of playing 2 notes repeatedly and quickly.  You're struggling because it calls for the 2 weakest fingers to pull off this particular trill (4,5).  Play it slowly and your fingers will eventually obey you :-)
Comment: Or use both hands, so the trill can be done with sensible fingers...


Redirect as a backup trick? w/o modifying DNS?

Question: I specifically looked up how to do something like this ( Can you set a backup ip for your server in DNS? ) and the answer basically was you can't. If i say specify 2 ip addresses could i somehow use a HTTP response header to ignore it temporary (say 5mins) and go to the other IP address? Or maybe i can play dead however i'm unsure how to play dead using nginx. I then would like to be available after my box notice the other box is down and be some kind of readonly server.
I'm sure something like this has been implemented i am just wondering how i might implement it with 2 boxes. I'm sure it isn't very difficult? How might i redirect traffic from a backup box to my main server without modifying the DNS?
Answer:
How might i redirect traffic from a backup box to my main server
  without modifying the DNS?

It's not possible.

I specifically looked up how to do something like this
  (Can you set a backup ip for your server in DNS?) and the answer basically was you
  can't

Unfortunately, that answer is correct. The best you can have is a normal HTTP redirect, but the server that is dead must be up in order to issue that redirect.
Essentially, you need to understand how the process works:

Client looks up (via DNS) the IP(s) associated with example.com
It uses the first one, and caches it (DNS servers won't return IPs in the same order every time, they use round robin)
The client attempts to open a connection to that IP, on port 80. If it's dead, it's dead.  Client gives up.

You are looking for a method that, after the client gives up, they are 'notified' of a new IP they should try. There's simply no way to do this: the server that is supposedly dead must be up in order to issue the information that it should redirect elsewhere.
The way the DNS methods work is that the bad server is taken out of the DNS pool, so clients don't receive it. They also set the cache time to low, so when the user hits refresh, they are likely given a new IP.
Comment: Cache? Do you mean TTL?
Comment: Yes - how long it caches it for depends on the TTL that the server specifies.


Group By results as individual column names

Question: How to get the groupby result as column names
if staff table is there when we use

select count(*) from staff group by gender; gives me as

but i need them as columns Male  | Female | None
Comment: Consider handling issues of data display at the application level (e.g. with a bit of PHP)
Answer: <code>select sum(case when gender = 'Male' then 1 else 0 end) as 'Male',
sum(case when gender = 'Female' then 1 else 0 end) as 'Female',
sum(case when gender not in ('Male','Female') or gender is null then 1 else 0 end) as 'None'
from staff;
</code>


For a fixed $k$, $\hat\rho_n(k) \longrightarrow 1$ as $n \longrightarrow \infty$

Question: I posted this question on MSE and have not found any answer, so I cross-post it here. I will notify when one of them gets an answer.

I'm trying to do the following exercise:

Let $(x_i)_{1 \le i \le n}$ be a time series. Then the empirical auto-correlation function $\hat\rho: \{1,\ldots,n-1\} \to \mathbb R$ is defined by $$\hat\rho_n(k) = \frac{\sum_{i=1}^{n-k}(x_i -\bar x)(x_{i+k}-\bar x)}{\sum_{i=1}^{n} (x_i-\bar x)^2}$$ where $\bar x = (1/n) \sum_{i=1}^{n} x_i$.
Fix $d \in \mathbb N_{>0}$ and $(a_0,\ldots,a_d) \in \mathbb R^{d+1}$. Now consider a particular time series in which $x_i = \sum_{j=0}^d a_j i^j$ for all $1 \le i \le n$. Prove that

For a fixed $k$, $\hat\rho_n(k) \longrightarrow 1$ as $n \longrightarrow \infty$.

I try to plug $$\bar x = \frac{1}{n} \sum_{j=0}^d a_j \sum_{i=1}^n i^j$$ in $(x_i -\bar x)(x_{i+k}-\bar x)$ and get $$\begin{aligned}(x_i -\bar x)(x_{i+k}-\bar x) &= \left (\sum_{j=0}^d a_j i^j -\frac{1}{n} \sum_{j=0}^d a_j \sum_{i=1}^n i^j \right) \left(\sum_{j=0}^d a_j (i+k)^j-\frac{1}{n} \sum_{j=0}^d a_j \sum_{i=1}^n i^j \right) \\ &= \left (\sum_{j=0}^d a_j \left ( i^j -\frac{1}{n}  \sum_{i=1}^n i^j \right) \right) \left(\sum_{j=0}^d a_j \left ( (i+k)^j-\frac{1}{n}  \sum_{i=1}^n i^j \right ) \right)\end{aligned}$$
Then I'm stuck at simplifying this expression and can not move on. Could you please elaborate on how to solve this problem?
Comment: What is "$\bar x$"? Because it does not explicitly depend on anything, one would think it would be the mean of the time series $(x_i);$ but in almost all such cases, that time series does not have a mean -- and when it does, $x_i=0$ for all $i$ and so $\hat \rho$ is not defined.  It looks like you intend it to be the mean of the first $n$ values in $(x_i),$ in which case it would help to include "$n$" somewhere in its notation. As a hint, assume $a_d\ne 0$ and write the terms in $\hat \rho$ as Riemann sums.
Comment: @whuber Yeah, I meant $\bar x = (1/n) \sum_{i=1}^{n} x_i$. Edited
Answer: Partial answer for $j=1$ and $k=1$ with, additionally, assuming that $a_0=0$ and hence also not fitting a constant in an AR(1) model (recall that the $k=1$st autocorrelation coefficient equals the estimated AR(1) coefficient):
So we consider $y_t=\delta t+ \epsilon_t$, fit an $AR(1)$ model without constant to $y_t$, and show that $\hat{\rho}\to_p1$.
\begin{eqnarray*}
  \hat{\rho}&=&\frac{\sum_ty_{t-1}y_{t}}{\sum_ty_{t-1}^2}\\
  &=&\frac{\sum_t(\delta(t-1)+\epsilon_{t-1})(\delta t+\epsilon_{t})}{\sum_t(\delta(t-1)+\epsilon_{t-1})^2}\\
  &=&\frac{\sum_t\delta^2(t-1)t+\delta(t-1)\epsilon_{t}+\delta t\epsilon_{t-1}+\epsilon_{t-1}\epsilon_{t}}{\sum_t\delta^2(t-1)^2+2\delta(t-1)\epsilon_{t-1}+\epsilon_{t-1}^2}\\
  &=&\frac{\frac{1}{T^3}\sum_t\delta^2t^2-\delta^2t+\delta(t-1)\epsilon_{t}+\delta t\epsilon_{t-1}+\epsilon_{t-1}\epsilon_{t}}{\frac{1}{T^3}\sum_t\delta^2(t^2-2t+1)+2\delta(t-1)\epsilon_{t-1}+\epsilon_{t-1}^2}\\
  &=&\frac{\frac{1}{T^3}\sum_t\delta^2t^2-\delta^2t+\delta(t-1)\epsilon_{t}+\delta t\epsilon_{t-1}+\epsilon_{t-1}\epsilon_{t}}{\frac{1}{T^3}\sum_t\delta^2(t^2-2t+1)+2\delta(t-1)\epsilon_{t-1}+\epsilon_{t-1}^2}\\
  &=&\frac{\frac{\delta^2}{3}}{\frac{\delta^2}{3}}+o_p(1)\\
  &=&1+o_p(1),
  \end{eqnarray*}
where the second-to-last line follows because all other sums vanish when divided by $T^3$. For $\delta(t-1)\epsilon_{t}$ see e.g. Hamilton, Prop. 17.1(c).


PostgreSQL 9.0 implicit transactions not rolling back

Question: I'm using PostgreSQL 9.0 in production, and have started noticing that statement errors abort an implicit transaction that never gets rolled back. This same issue doesn't appear on my development box, which is at 9.2. I'm not sure if it's a bug, feature, or configuration parameter at this point.
Here's an example of the problem:
$ psql test
test=> SELECT foo;
ERROR:  column "foo" does not exist
LINE 1: select foo;
               ^
test=> SELECT VERSION()
ERROR:  current transaction is aborted, commands ignored until end of transaction block

Of course, I don't have a transaction to begin with. I realize that PostgreSQL wraps all statements in an implicit single-statement transaction, but in my experience it's never needed to be rolled back when there's an error. I don't require an explicit rollback on my local development instance of PostgreSQL.
The only relevant information I've been able to find online indicated it's a result of AUTOCOMMIT being off, but it's set to on in both environments.
So what is this? A bug? A misconfiguration? How do I resolve it?
Comment: Interestingly, it works if I connect with the `psql` client from my development box. Is this possibly a buggy version of libpq?
Comment: Upgraded libpq and psql, no luck.
Answer: Run this command in the psql client:
\echo :AUTOCOMMIT

If it says "off", then look for a .psqlrc file that contains either "\set AUTOCOMMIT off" or "\unset AUTOCOMMIT".


Drupal Join Query with entityCondition

Question: I have two content type A and B in which B content type node is reference in A content type node. In B content type node there is status field. I want to fetch the A content type node in which the status of B content type node is something.
Can you please guide me how can I use this?
Thanks
Answer: This is the best solution for you ==> https://www.drupal.org/node/1343708
EFQ an beautifull tools for manipulate the entities in drupal.
Do not hesitate if you need precision but this doc is verry good.
In the Entity Reference case
The column name for an entity reference field is target_id, so your code should be:
<code>$query = new EntityFieldQuery();

$query
  ->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('type', 'custom_node_type', '=')
  ->fieldCondition('field_reference_name', 'target_id', $id, '=');

$entities = $query->execute();
</code>
For follow the relation of your entities you need to reference the A into B or B into A with a reference field. When you use EFQ to search the relation in node A with B you need to use fieldCondition in this reference field in my example is field_reference_name. If you list the content B (ids) into an array and you user this
<code>$query = new EntityFieldQuery();

$query
  ->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('type', 'a', '=')

$entities_a = $query->execute();
</code>
For search the relation A to B into A you need to use this :
<code>$query = new EntityFieldQuery();

$query
  ->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('type', 'b', '=')
  ->fieldCondition('field_reference', 'target_id', $entities_a, 'IN');

$entities_b_refered_into_a = $query->execute();
</code>
Good chance.
Comment: From where I get the ID Variable in your query .

This was my query :

$query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle',  array('candidate','interview'), 'IN')
  ->propertyCondition('status', 1);
$query->fieldCondition('field_interview_status', 'value', 'new', 'IN'); // This field is in interview content type with status dropdown
$result = $query->execute();

This gives me the B content type nodes. But I need A content type node.
Comment: EFQ operates on one entity/bundle type at a time, it can't follow relationships nor extend queries based on related entities' properties @Leo
Comment: The propertyCondition in EFQ support multiple args ex :  $name = ['a', 'b', 'c']; $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
        ->propertyCondition('type', '$name', 'IN');
  $result = $query->execute();

Drupal Join Query with entityCondition


Different URLs for downloading and uploading with paperclip on S3 storage

Question: For local development I am using a localstack Docker Container as AWS Sandbox with this Paperclip configuration:
config.paperclip_defaults = {
  storage:        :s3,
  s3_credentials: {
    access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  },
  bucket:         'my-development',
  s3_region:      'localhost-region',
  s3_host_name:   'localhost:4572',
  url:            ':s3_path_url',
}

Links for download content are generated correctly and are working:

http://localhost:4572/my-development/files/downloads/be-fl-che-spezialtiefbau-mischanlage-750_ae0f1c99d8.pdf

But when I want to upload new files I get an Aws::Errors::NoSuchEndpointError based on a different URL:

https://my-development.s3.localhost-region.amazonaws.com/files/downloads/_umschlag-vorlage_c534f5f25e.pdf

I searched and debugged some hours but couldn't find out where this url is generated and why it uses amazonaws.com as host.
Any hint where to look at?
Comment: Your `paperclip_defaults` object is initiates `:s3` actually. Your upload always will be focus on s3 at that point.
Comment: Localstack provides a S3 Service too and it works, so it is fine. I just want to change where (on which host) files get uploaded.
Comment: I still do not get, how do u get download content without uploading it [NoSuchEndpointError] ? Nor does your upload content work properly ? And if localstack is development, whats S3 production ?
Comment: I mirrored the "live AWS S3" into the localstack Container with some terminal commands. It got nothing to do with paperclip, ruby nor the AWS Gem. In production I'm using the "real" AWS S3.
Answer: I found a way to get it working.
Add explicit endpoint url to configuration
# config/environments/development.rb
config.paperclip_defaults = {
  storage:        :s3,
  s3_credentials: {
    access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  },
  s3_options:     {
    endpoint: 'http://localhost:4572/my-development',
  },
  bucket:         'my-development',
  s3_region:      'localhost-region',
  s3_host_name:   'localhost:4572',
  url:            ':s3_path_url',
}

As the url will be renamed with the bucket name by AWS Gem, the resulting domain will be my-development.localhost. I didn't found any other solution yet than to add this subdomain into my /etc/hosts:
127.0.0.1   localhost
127.0.0.1   my-development.localhost
255.255.255.255 broadcasthost
::1             localhost
::1             my-development.localhost

This is not very clean but works. Maybe I found a better work around later.
Answer: This could help others. You can update the aws config in your environment specific config file.
Aws.config.update(
    endpoint: 'http://localhost:4572',
    force_path_style: true
  )


Protect Question feature

Question: A very quick newb question from me related to moderating tools.
How do you protect a question if it is eligible to be protected? I don't see a button for it or anything? Is it only an option if answers have been flagged for it or something? I'm just completely in the dark on how the tool works.
Answer: This answer on meta.SE is quite informative about how the protection system works in general.
On this site, only moderators and users with 3,500+ reputation can protect a question.
For the uninitiated: "protecting" a question means that only users with more than 10 reputation can answer it. Protection is designed to prevent new people (who don't understand the norms of stack exchange) from adding low-quality or spurious answers to high-visibility questions they have stumbled upon.
For now, if you see something you think you needs protecting (i.e. a question that is attracting spurious answers from low-rep users) then my suggestion would be that you flag it for moderator attention and we can protect it. The same goes for a protected question where the protection is no longer needed and which should therefore be unprotected.
The guidelines on protection say the following:

Do protect questions that are attracting a lot of non-answers or very poor answers (spam, etc.) from new users.

Don't protect questions just because they're linked to on a high-traffic news site like Reddit or Ars Technica. While there's certainly some correlation between sudden spikes in popularity and associated non-answers, not all popular questions suffer from this.

Do unprotect questions that aren't currently attracting a lot of attention and don't have a long history of unproductive answers.
Comment: @denesp I saw that, and the part about needing at least one answer, but I think when checking for where to look I misclicked and was one a page not eligible for protection. So, silly me.
Comment: On the Econ.SE (here) it says that you only need 3500 points to protect questions (which I just got), so that's where my confusion is coming from. http://economics.stackexchange.com/help/privileges/protect-questions
Comment: @KitsuneCavalry I see. I guess we have a lower rep requirement while in beta. I am able to access the protect feature by clicking on the "mod" link, which is next to "share  edit  delete  flag" under each post. But I am not sure if the mod link is only there for moderators.
Comment: I don't have a mod link, so I am unsure of where to find it.
Comment: @KitsuneCavalry perhaps you could ask on meta.stackexchange.com ?
Comment: I'll give it a shot. Thanks!
Comment: I missed something very obvious, and now I see the feature. Thanks again.
Comment: @KitsuneCavalry You can only protect questions that are at least 24 hours old, so that may have been the issue as well.

Protect Question feature


Too many notes in this measure

Question: It seems there are too many notes in the treble clef below. The time signature is 4/4 yet there it starts with a quarter rest, then a dotted quarter, then 6 eighth notes. This adds up to more than four quarter notes. What does this mean? And what is the meaning of the eighth rest below the quarter rest at the beginning of the measure?
Comment: @alephzero Honestly I find this more readable than the notation you suggest because in this notation each voice has its own rests. But that might depend on which style you are used to.
Comment: I feel this is almost certainly a duplicate, but my searching abilities are failing me.
Comment: This is poor quality engraving (i.e. it's the sort of junk produced by "market leading" software when used by people who don't know any better!). The first 8th-rest applies to the staff as a whole, so it should be at its normal position in the middle of the staff. There should then be a second 8th-rest  above the dotted-quarter E. If it had been notated like that, the OP wouldn't have had a question to ask! Even if two completely separate voices were preferred for some reason, there is no reason to raise the quarter rest *above* its normal position when the 8th rest is moved so low.
Comment: As endorph pointed out, this is on of many questions we have on this exact topic. We need to do a better job of linking and closing duplicates and make them easier to find.
Comment: @Dom Perhaps we should edit titles to contain the correct terminology once a question is answered?
Comment: @endorph while I agree that would help us find them, it won't necessarily help new users find them if they don't know what to look for hence why duplicate questions are treated much differently than any other close reason. I'm thinking we can do a site wide cleanup soon and talk about how we can make this information easier for everyone to find.
Answer: What you're looking at is two separate voices. In more complicated music, we sometimes write out two independent lines (or voices) on a single stave.
The first voice in this piece comprises the crotchet rest, followed by the quavers C+G, C+G tied to another C+G, then C, D and D again. This adds up to four crotchets.
The second voice comprises the quaver rest, the dotted crotchet E, the crotchet E, the quaver rest, and the G+E quaver that is tied into the next bar. This also adds up to four crotchets.
There is only a single voice on the bass stave.
The reason I know this, is because of the direction of the note stems. See how the first voice only has stems that point upwards? The second voice all points downwards. I know which rests to associate with each voice through the vertical positioning of the rests on the stave, and also because there's only one interpretation which will allow the bar to add up.
Comment: This is also what I see when I read this measure.  It's pretty normal, but hard to read at a glance.
Answer: The music notation is correct, but possibly a bit odd if your not used to it. Seperated out it would look like this:

You can here it at This link Some guesses were made obviously. I I left out some of the stuff, but you get the point.
Answer: I believe there are two different voices in the top staff; The higher voice starts with a quarter rest then synchs up with the lower voice on the "c" and plays six more eighth notes making a full measure.

Too many notes in this measure


Joining two tables having two same columns

Question: I am making a school project with mysql database.
Having this table ready, I need to create a query that will join ReciverID and SenderID with accounts.Email column. I have tried many solutions, but all my atempts resulted into duplicates or errors. The result should look like this.
I have table "accounts"
CREATE TABLE IF NOT EXISTS `accounts` (
  `AccountID` INT(11) NOT NULL AUTO_INCREMENT,
  `Email` VARCHAR(128) NOT NULL,
  `Password` VARCHAR(128) NOT NULL,
  `Balance` DOUBLE(10, 5) NOT NULL DEFAULT 10,
  `VerifyCode` INT(6) NOT NULL,
  PRIMARY KEY (`AccountID`),
  UNIQUE INDEX `Email_UNIQUE` (`Email`),
  UNIQUE INDEX `VerifyCode_UNIQUE` (`VerifyCode`)
)

And table "transactions"
CREATE TABLE IF NOT EXISTS `mydb`.`transactions` (
  `TransactionID` INT(11) NOT NULL AUTO_INCREMENT,
  `SenderID` INT(11) NOT NULL,
  `ReciverID` INT(11) NOT NULL,
  `Date` INT(32) NOT NULL,
  `Note` VARCHAR(256) NULL DEFAULT NULL,
  `Amount` DOUBLE(10, 5) NOT NULL,
  PRIMARY KEY (`TransactionID`),
  INDEX `Sender_idx` (`SenderID`),
  INDEX `reciver_fk_idx` (`ReciverID`),
  CONSTRAINT `reciver_fk` FOREIGN KEY (`ReciverID`) REFERENCES `accounts` (`AccountID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `sender_fk` FOREIGN KEY (`SenderID`) REFERENCES `accounts` (`AccountID`) ON DELETE NO ACTION ON UPDATE NO ACTION
)

Thanks for any reply!
Comment: Please post the code you have developed so far.
Comment: [**Please Never** post images of code, data or error messages](https://meta.stackoverflow.com/a/285557/2310830). Please edit your question and include copy/paste the text into the question, formatted.
This is so that we can try to reproduce the problem without having to re-type everything, and your question can be properly indexed or read by screen readers.
Answer: You can try working with alias.
Something like
Select T.TransactionID, S.email, R.email, T.date, T.note, T.Amount
From Transactions T, Accounts S, Accounts R
Where T.SenderID = S.AccountID AND T.ReceiverID = R.AccountID AND
--The rest of your conditions

PS: I wouldn't recommend naming a column date... It's a key word usually resulting in errors

EDIT
Base on @HoneyBadger comment the comma joining style is out dated... So here's another way to do it
Select T.TransactionID, S.email, R.email, T.date, T.note, T.Amount
From Transactions T
Join Accounts S On T.SenderID = S.AccountID
Join Accounts R On T.ReceiverID = R.AccountID
--add your conditions

Comment: Thanks for your solution and for the recommendation!
Comment: Please don't teach people antiquated, comma separated, join style. `join...on...` Is the standard for 30-odd years, it's easier to write, easier to read and easier to maintain


Drupal 7 subfolder multisite

Question: I'm trying to setup a drupal 7 multisite using subfolders for example:
example.com/site1
example.com/site2
In my drupal sites folder I have sites1 and sites2 according to sites.php I should be able to set it up as the following:
<code>$sites['example.com/site1'] = 'site1';
$sites['example.com/site2'] = 'site2';
</code>
However this doesn't work, I can definitely see a site at example.com but am unable to see any of the sites in the sub folder.
Comment: Do you _really_ need to expose the subsites as directories? It is usually far better to setup a subdomain, such as site1.example.com
Answer: If you want to do it this way then you have to symlink the folder to the root of the drupal install:
<code>cd /var/www/drupal
ln -s . site1
</code>
Then within sites.php use the following:
<code>$sites = array(
    "example.com.site1" => "site1",
);
</code>
Comment: Can you provide an example where you have "/subdirect" and "sitename.com", both being called site1 doesnt let us know what to do when you want diff path's and sites :)
Comment: Answered my own question. If you have /subdir and site.com then it would be create a sym link for subdir in drupal root, then in sites.php. $domain . '.subdir' => 'site.com' the / must be a . - very important and tripped me up before i found this!
Comment: PS! I think this question is answered quite good in the documentation on Drupal.org - see https://www.drupal.org/docs/7/multisite-drupal/multi-site-in-subdirectories

Drupal 7 subfolder multisite


Stats question concerning standard deviation

Question: The volume to which a soda bottle is filled is Normally distributed. 80% of soda bottles are within 0.02 ml of the mean volume of all bottles. Determine the standard deviation of the volumes.
This problem seems to be leaving out crucial information. How can you solve this without knowing the mean?
Answer: $Z$ is the standardized normally distributed random variable:$Z\sim \mathcal N(0.1)$. Then you are looking for
$P(-z\leq Z \leq z)=2\Phi(z)-1$
$\Phi(z)$ is the function of the standard normal distribution. This is the area of the pdf from $-z$ to $z$
And $z=\frac{x-\mu_x}{\sigma_x}=\frac{0.02}{\sigma_x}$
$x-\mu$ is $0.02$, because the deviations are at most $0.02$
$2\Phi(z)-1=0.8$
$2\Phi(z)=1.8$
$\Phi(z)=0.9$
If you look at a table of a standard normal distribution you´ll see that
$z=\Phi^{-1}(0.9)=1.28$
Thus the equation is $\frac{0.02}{\sigma_x}=1.28$
Comment: @robjohn Yes, you´re right. Thanks for the comment.
Answer: If $X \sim \operatorname{Normal}(\mu,\sigma^2)$ is the random fill volume of a single bottle, then the given condition is expressed mathematically as $$\Pr[\mu - 0.02 \le X \le \mu + 0.02] = 0.80,$$ where $\mu$ is the mean fill volume in milliliters.  It follows that $$\Pr[-0.02 \le X - \mu \le 0.02] = 0.80,$$ and dividing by the (as-yet unknown) standard deviation $\sigma$, we get  $$\Pr\left[\frac{-0.02}{\sigma} \le \frac{X - \mu}{\sigma} \le \frac{0.02}{\sigma} \right] = 0.80.$$  But we also know that $$Z = \frac{X - \mu}{\sigma} \sim \operatorname{Normal}(0,1),$$ hence our above equation can be written as $$\Phi(0.02/\sigma) - \Phi(-0.02/\sigma) = 0.80,$$ where $\Phi(z) = \Pr[Z \le z]$ is the CDF of the standard normal distribution.  But since $\Phi(-z) = 1 - \Phi(z)$ due to the symmetry of the standard normal distribution, we now have $$2 \Phi(0.02/\sigma) - 1 = 0.80,$$ or $$\frac{0.02}{\sigma} = \Phi^{-1}(0.9).$$  $\Phi^{-1}(q)$ is the inverse CDF (or the quantile function):  It represents the value $z^*$ such that $\Pr[Z \le z^*] = q$, so $\Phi^{-1}(0.9)$ in particular represents the $90^{\rm th}$ percentile of the standard normal.  We can either look this up in a table or use a calculator, giving $$\sigma = \frac{1}{50 \Phi^{-1}(0.9)} \approx \frac{1}{50(1.28155)} \approx 0.0156$$ milliliters.
This makes sense if you are familiar with the 68-95-99.7 rule, which states that approximately $68\%$ of observations of a normal random variable will lie within $1$ standard deviation of the mean; $95\%$ will lie within $2$ standard deviations of the mean; and $99.7\%$ will lie within $3$ standard deviations of the mean.  Since $80\%$ of observations lie within $0.02$ milliliters of the mean, this suggests to a very crude approximation that $0.02 \approx 1.5 \sigma$; i.e., $80\%$ is very roughly $1.5$ standard deviations if we "eyeball" this rule.  (What $\Phi^{-1}(0.9) \approx 1.28155$ tells us, in fact, is that $80\%$ of observations of a normal random variable will be within $1.28$ standard deviations of the mean.)


Effects of right-censoring on Poisson IRR estimates

Question: I have some time-to-event data with right-censoring.
Details Added: Each subject is followed up until first event (no recurrent events) or loss to follow-up. Let's assume there are no competing risks.
Given that rates are constant (and other Poisson assumptions hold), and censoring is non-informative, am I correct in thinking that Poisson regression gives unbiased estimates of the incidence rate ratio in the following model?
<code>glm(status ~ trt, offset = log(time), family = poisson(link = "log"), data = df)
</code>
A few test datasets have yielded very similar results between the Cox HR and Poisson IRR, but I want to understand whether this is general.
My logic is that any bias in the estimation of the rates is balanced between both groups, leaving ratios unaffected?
Comment: If censoring is non-informative, how exactly is either model providing a biased estimation of rates? How is the Cox model providing an estimate of rates *at all*?
Comment: It's not clear to me from your description where the right censoring comes from and how you are handling right-censored observations in your model. Is this a situation where an individual can have at most one event, and you are simply not recording an event at the last observation time? What determines that last observation time? Or might an individual experience more than one event, and you are recording the number of events over an observation duration?
Comment: @EdM Good point - added more info for clarity. Hope that helps.
Comment: I think that [this page](https://stats.stackexchange.com/a/8118/28500) provides the theoretical justification that you're looking for. Please look that over and edit this question further if it doesn't do so. @AdamO I think it's the _rate ratio_ between `trt` groups that's being estimated in the Cox model.
Comment: @EdM a hazard is an instantaneous rate. If the hazards are non-proportional, I can believe that these models estimate fundamentally different things, but otherwise they should be consistent for the same value. Correct?
Comment: @AdamO, Yes, given the Poisson assumption of constant incidence rate, hazards will be proportional => the HR and the IRR should be consistent (and I understand that Cox does not estimate the hazard function, but rather the parametric component; i.e., the betas).
Modelling the hazard parametrically with an exponential distr yields the *exact* same values as the Poisson IRR, but Cox yields ever so slightly larger effect sizes in my simulations. Therefore, I became unsure of whether Poisson / parametric survival modelling is vulnerable to censoring, or if this is just a random finding.
Comment: And thanks @EdM. I think I found something closer to what I was looking for: ["Limited Dependent Poisson Regression" by Kurt Brännäs](https://www.jstor.org/stable/2349006?seq=8). Bias from censoring shouldn't be a big problem with large lambda -- but I'd love to know if anyone has any resources on individually varying censoring (i.e., where the censoring threshold isn't the same for everyone, which it often isn't with time-to-event). Otherwise, I guess I'll have to start simulating a bit harder.
Comment: @ZKA you still haven't clarified how bias would show up due to non-informative censoring. I have my ideas of what it might be, but I'm curious what you are expecting or projecting from your particular analysis.
Comment: @AdamO Non-mathematically, large values are more affected, so marginal effects will be attenuated. E.g., assume all subjects experience the event *at some point*, one group has (on avg) longer time-to-event (lower rate) than the other, and the censoring condition has equal and constant rate. Then, both groups will be equally censored for counts but the low-rate group'll have more follow-up time shaved off. Therefore, low-rate group's rate is overestimated relative to high-rate group => bias towards the null

See also [here](https://journals.sagepub.com/doi/pdf/10.1177/1536867X1101100106)
Comment: I suppose reading these papers* gave me my answer: there will be bias, but magnitude all depends. Wanna be completely sure, perform censored PR.

*Also:
[Famoye](http://jakewestfall.org/misc/famoye2004.pdf),
[Trinh](https://hal.science/hal-01811949/document), and
[Saffari](https://www.researchgate.net/publication/257142544_Zero-Inflated_Poisson_Regression_Models_with_Right_Censored_Count_Data#fullTextFileContent)
Answer: McCullagh and Nelder show in Section 13.3.1 of "Generalized Linear Models" (Second Edition; Chapman & Hall/CRC, 1989) that an exponential survival model is equivalent to a Poisson model with an offset, like you show. If the Poisson model is correct, there should be no inherent bias other than what's already the case for fitting a finite data set by maximum likelihood.
Several of the references you cite note additional bias problems (often important in practice) that arise in count models with things like over-dispersion. The risk of informative censoring can't be forgotten in the context of survival analysis. And I haven't completely thought through the implications of finite-sample bias if there are different numbers of events among treatment groups.
Nevertheless, there is a very close connection between a Cox model and a Poisson model with piecewise-constant baseline hazards. See these course notes by Rodríguez. That should help to explain your observations.


Positioning two floating blocks in absolute positioned division

Question: here is the code
http://jsfiddle.net/77NBM/12/
two grey squeares ("float 1" and "float 2") have to be positioned beside each other in absolute positioned division "submenu_container" related to
 "menu 3".
Can plz somebody help me to get it? :(
Thx!
Comment: Your menu `li`'s width is 100px and both the floats inside it are alos 100px wide. You can't have them side by side inside a 100px container. Please explain or correct what you're trying to do. Maybe post a screenshot of the end result?
Comment: http://imageshack.us/photo/my-images/846/resultr.jpg/
Answer: If you'll always have two elements ("float1 and float2") of 100px each, you can set the width of .submenu_container to 220px and they will float beside each other. like this http://jsfiddle.net/77NBM/13/
If the width of the elements ("float1 and float2") is dynamic, I suggest you set the width of .submenu_container dynamicly width javascript or jQuery. for example:
$('#main_menu li').each(function(index) {
    var menuWidth=0;
    $(this).children().children().each(function(index) {
         menuWidth+=$(this).outerWidth();
    }
    $(this).children('.submenu_item').width(menuWidth)
});

Comment: Ok, i think the JavaScript solution is the best way for me. Just wanted to figure it out with oure html. Thx!
Answer: The width of your submenu is being constrained by the width of your li element, just add some width to your submenu and they should float:left naturally
CSS
#top{
    width:500px;
    height:300px;
    position:relative;
    margin:0px auto;
    background:#ccc;
}

#navigation{
    position:absolute;
    width:100%;
    height:50px;
    top:250px;
    left:0px;
    background:#f00
}
#main_menu{
    width:auto;
    height:50px;
    margin-left: 0;
    padding-left: 0;
    list-style-type: none;
}

#main_menu li{
    width:100px;
    height:50px;
    margin-right:10px;
    float:left;
    background:#ff0;
    position:relative;
}

#main_menu li a{
    display:block;
    width:inherit;
    height:inherit;
}

.submenu_container{
    position:absolute;
    background:#00f;
    padding:5px;
    left:-5px;
    width:275px;
}

.submenu_item{
    width:100px;
    height:100px;
    float:left;
    background:#eee;
    margin:5px;
}

http://jsfiddle.net/77NBM/17/
By the way, your html markup is full of errors, you need to take a look at that.
HTML

        Menu 1

        Menu 2

        Menu 3

            Float 1

            Float 2

        Menu 4

Comment: Thx, but i need the "submenu_container" div width to be calculated dependent on it's content width (there may be one, or two or three floating divisions inside). Is it possible? Do you mean line breaks, speaking of html errors?
Comment: then increase the size of your #top div, it is currently set to 500px, if you try to make your submenu open up dependently it will break out of your "top" div.
Comment: Sure, in the original markup the width of the #top div is set wide enough. The problem is to set the position of submenu_container at the right place and to set it's width automatic dependent on it's content. Do you mean it's possible without JavaScript?
Comment: what is the final width of your #top div going to be? Because think about it for a second, imagine that if you have six elements in your drop down menu at the current 500px width of your top div, are you ok with them breaking out of that container? take a look at this example to show you what i mean: http://jsfiddle.net/77NBM/18/
Comment: Yes, it's ok in my case :) (ok, six is a little to much, i'll have 3 max and it's ok)
Comment: If so, then you can be a bit more specific about the size, like this: http://jsfiddle.net/77NBM/20/  (restructured a bit your submenu)
Comment: It's ok... the size is: full width: 960, the navigation block is about 600, placed on a left side. Subemnu can overlap the content on the right. But you still set the submenu container width manually. Is it the only way? Then i have to use JavaScript...
Comment: In this case, yeah, you would have to specify the width of your sub menu manually in order for the elements inside to float left.
Answer:     #top{
    width:500px;
    height:300px;
    position:relative;
    margin:0px auto;
    background:#ccc;
}

#navigation{
    position:absolute;
    width:100%;
    height:50px;
    top:250px;
    left:0px;
    background:#f00
}
#main_menu{
    width:auto;
    height:50px;
    margin-left: 0;
    padding-left: 0;
    list-style-type: none;
}

#main_menu li{
    width:100px;
    height:50px;
    margin-right:10px;
    float:left;
    background:#ff0;
}

#main_menu li a{
    display:block;
    width:inherit;
    height:inherit;
}

.submenu_container{
    width:auto;
    position:absolute;
    background:#00f;
    padding:5px;
    left:-5px;
}

.submenu_item{
    width:100px;
    height:100px;
    float:left;
    background:#eee;
    margin:5px;
}


Flask Plotly Responsive in Browser

Question: How to make this plotly chart responsive in index.html - I create a flask app and js function for rendering the plot, however, I cannot figure out, how to make it responsive. What is wrong with variable config - it seems not to work.
Thank you
Flask:
@app.route('/')
def index():

    locations = sorted(df['location'].unique())
    rooms = sorted(df['rooms'].unique())

    # Visualisation
    import json
    import plotly
    import plotly.express as px
    df.rooms = df.rooms.astype(str)
    fig = px.scatter(df, x="m2", y="price", trendline="ols", color="rooms", symbol='rooms',
                                marginal_x="histogram",
                                marginal_y="rug",
                                template='plotly_dark', hover_data=['price', 'rooms', 'm2', 'location'],
                                title="Real Estate in Vienna")

    graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)

    return render_template('index.html', locations=locations, rooms=rooms, graphJSON=graphJSON)

JavaScript:

                    {%block content%}

                    {% endblock %}

Answer: I think you are using Plotly.newPlot() wrong. By reading the API reference you will see, that there are to different signatures for this function:

Plotly.newPlot(graphDiv, data, layout, config)
Plotly.newPlot(graphDiv, obj)

Looks you were mixing both up since the Python function
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
returns a single object with keys for data, layout, config and frames with defaults to {data: [], layout: {}, config: {}, frames: []}.
Thus everything should work if you use the following JavaScript that extracts the parameters data and layout from the object charts and then use
Plotly.newPlot(graphDiv, data, layout, config)
instead of
Plotly.newPlot(graphDiv, obj):

Flask Plotly Responsive in Browser


Pandas Python muito lento

Question: Alguem pode me ajudar? estou fazendo a leitura de um arquivo, faço algumas alterações e em seguida salvo em outra pasta. mas isso leva 2 horas, o arquivo tem 15 milhoes de linhas, teria algum metódo diferente e mais eficaz?
# LER ARQUIVO NA PASTA STAGING
arq5 = pd.read_csv(r'C:\Users\Usuário\staging\arquivo5.txt',delimiter='\t',encoding='cp1252',engine='python')

# FAZ ALTERAÇÕES NO ARQUIVO
columns = ['PERIODO', 'CRM', 'CAT', 'MERCADO', 'MERCADO_PX', 'CDGLABORATORIO', 'CDGPRODUTO', 'PX']
arq5.drop(columns, inplace=True, axis=1)

# SALVA O ARQUIVO 5 COMO CSV NA PASTA ALPHA
arq5.to_csv(r'C:\Users\Usuário\alpha\arquivo5.txt', index=False)

Comment: Realmente é um arquivo muito grande, não conheço outro modo de fazer isso. Seu hardware não está limitando o processamento?
Answer: O pandas carrega o arquivo inteiro pra memória, e isso pode ser lento, no caso de arquivos muito grandes.
Tente não carregar o arquivo inteiro. O código abaixo faz o mesmo que o seu, porém sem usar pandas e sem carregar o arquivo inteiro na memória - ele vai lendo o arquivo de origem linha a linha, em seguida modificando, e salvando direto no destino:
colunas_remover = ['PERIODO', 'CRM', 'CAT', 'MERCADO',
    'MERCADO_PX', 'CDGLABORATORIO', 'CDGPRODUTO', 'PX']
nome_arquivo = r'C:\Users\Usuário\staging\arquivo5.txt'
destino = r'C:\Users\Usuário\alpha\arquivo5.txt'

# LER ARQUIVO JA GRAVANDO O RESULTADO EM OUTRA PASTA
with open(nome_arquivo, encoding='cp1252', newline='') as f:
    cf = csv.DictReader(f, delimiter='\t')
    with open(destino, 'w', encoding='cp1252', newline='') as fw:
        colunas_manter = [c for c in cf.fieldnames if c not in colunas_remover]
        cw = csv.DictWriter(fw, colunas_manter, delimiter='\t',
            extrasaction='ignore') # ignora o que nao esta em "manter"
        cw.writeheader()
        cw.writerows(cf)

Comment: Obrigado pela ajuda! o script funcionou perfeitamente, o problema agora é que o nome da coluna não esta vindo e as linhas estão alternando com uma linha com registro e outra não.
Comment: @VitorTenório Coloquei na resposta o `writeheader()` que faltou para colocar o nome das colunas. Agora quanto à alternação, existem pequenos ajustes no tratamento das quebras de linha dependendo da versão do python - qual python está usando?
Comment: Outra coisa @VitorTenório, a título de curiosidade, ficou mais rápido? Quanto tempo está demorando para rodar tudo agora?
Comment: deu certo o nome da coluna, perfeito! estou usando a versão 3.6 do python. muito mais rapido, leva em torno de 5min.
Comment: Que ótimo, redução considerável hein @VitorTenório, quase duas ordens de magnitude. Adicionei o `newline=''` nos dois `open()`s  o que deve resolver o problema das linhas duplas para o python 3.6; qualquer coisa me fale!
Comment: certinho :).. uma duvida, preciso fazer um group by em uma coluna, se não for demais pode me mostrar como faço?
Comment: eu tinha pensando em fazer pelo pandas, mas isso levaria mais tempo de processamento e voltarimos ao fundo do poço kk
Comment: @VitorTenório se os dados estiverem ordenados, você pode usar o `itertools.groupby()`... Sugiro abrir nova pergunta com exemplo de entrada e de saída
Comment: Legal!! muito obrigado pela ajuda.
Comment: @VitorTenório Não se esqueça de colocar na nova pergunta um exemplo de como é o seu arquivo de origem, e um exemplo de como você gostaria que ficasse o arquivo de saída. Essas informações são essenciais para te ajudar com groupby

Pandas Python muito lento


local sphinx generates autodoc members from docstrings, readthedocs does not

Question: I have a small project at github. The package does not have external dependencies, and all the source is included in a single __init__.py file. In the ./docs folder, I just have 2 rst files: an index.rst with some intro and a nix_shell_utils.rst file where the package members are automatically described from their docstrings:
nix_shell_utils.rst file
nix_shell_utils package
=========================

.. automodule:: nix_shell_utils
   :members:
   :undoc-members:
   :show-inheritance:

The ./docs/conf.py is quite standard:
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
import os
import sys

sys.path.insert(0, os.path.abspath('..'))

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'nix_shell_utils'
copyright = '2022, Alberto Garcia Izquierdo'
author = 'Alberto Garcia Izquierdo'
release = '0.0.1'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.viewcode',
    'sphinx.ext.autosummary',
    'sphinx.ext.napoleon'
]

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'sphinx_rtd_theme'
#html_static_path = ['_static']

# -- Options for autodoc ----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#configuration

# Automatically extract typehints when specified and place them in
# descriptions of the relevant function/method.
autodoc_typehints = "description"

# Don't show class signature with the class' name.
autodoc_class_signature = "separated"

autodoc_member_order = 'bysource'

when generating documentation locally with make html the process goes OK: when clicking on the nix_shell_utils link in the index.html page, I get to the nix_shell_utils.html page, where all the members of the package are rendered correctly by :automodule: from their docstrings:
However, when I tried to do the same in readthedocs.org, the build process terminates OK, but when clicking on the nix_shell_utils link at the documentation index, I just get a page with the title, "Module Contents" header, and nothing else. Readthedocs does not render the documentation of the package members from their docstrings.
My .readthedocs.yaml is pretty standard
version: 2

build:
  os: "ubuntu-22.04"
  tools:
    python: "3.10"

python:
  install:
    - method: pip
      path: .

And the log from the readthedocs building process does not indicate any problem:
Running Sphinx v5.3.0
making output directory... done
[autosummary] generating autosummary for: index.rst, nix_shell_utils.rst
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 2 source files that are out of date
updating environment: [new config] 2 added, 0 changed, 0 removed
reading sources... [ 50%] index
reading sources... [100%] nix_shell_utils

looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [ 50%] index
writing output... [100%] nix_shell_utils

generating indices... genindex py-modindex done
highlighting module code... [100%] nix_shell_utils

writing additional pages... search done
copying static files... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
Updating searchtools for Read the Docs search... build succeeded.

The HTML pages are in _build/html.

I have looked at similar questions in stackoverflow, but for most of them either the package to be documented has not been installed prior to run the doc build (not my case), or they have external dependencies that have not been installed (I don't have any). I've tried to set up exactly the same environment locally and in readthedocs (ubuntu 22.04, Python 3.10, Sphinx v5.30) and also run the same command as in readthedocs locally
python -m sphinx -T -E -b html -d _build/doctrees -D language=en . _build/html

But again, locally autodoc members are rendered correctly, while on readthedocs they don't.
Any idea of why this is happening?
Comment: https://nix-shell-utils.readthedocs.io/en/latest/nix_shell_utils.html looks OK to me.
Comment: @mzjn - yes, as the asnwer below suggested, it was just a browser cache issue :-S Thanks for checking!
Answer:
However, when I tried to do the same in readthedocs.org, the build
process terminates OK, but when clicking on the nix_shell_utils link
at the documentation index, I just get a page with the title, "Module
Contents" header, and nothing else. Readthedocs does not render the
documentation of the package members from their docstrings.

Maybe it was a cache issue? I do see the documentation properly at https://nix-shell-utils.readthedocs.io/en/latest/nix_shell_utils.html
It shows the docs for nix_shell_utils.mkdir, nix_shell_utils.cp and many other functions.
Comment: Ouch! I was scratching my head for over 30 builds as to why this was happening - just cleaning my browser cache did the trick (I'm new to readthedocs) ... Thanks a lot Manuel! :-)


Argument of type 'Blob' is not assignable to parameter of type 'string | Blob'

Question: Just got an error:
Argument of type 'Blob' is not assignable to parameter of type 'string | Blob'.
  Type 'import("buffer").Blob' is not assignable to type 'Blob'.
    The types returned by 'stream()' are incompatible between these types.
      Type '{}' is missing the following properties from type 'ReadableStream<any>': locked, cancel, getReader, pipeThrough, and 2 more.

And can't understand what's going on. Tried to restart vscode but didn't help. Blob is not assignable to param of type Blob. ?! So Blob returned by stream is not the same with usual Blob?
This is my code
    const audioBuffer = fs.readFileSync(`./uploads/audio/${filePath}`)
    const audioBlob = new Blob([audioBuffer])
    const formData = new FormData()
    formData.append('file', audioBlob)

Error is showing when i'm trying to append file to formdata
Comment: Are you importing Blob or FormData from any module?
Comment: @RichieBendall i'm importing Blob this way `import { Blob } from 'buffer'` but formdata -no
Comment: What module are you using for FormData
Comment: @RichieBendall i'm using nodejs so it's already in ig ?
Comment: FormData is not a built-in in nodejs. Maybe the question could be expanded to include the motivation for using FormData? There's probably a conventionally different approach to doing this in node.
Answer: You're using two different types called Blob
The one you are importing is from node:
import { Blob } from 'buffer' // this is from node

While the apis you are calling (FormData) are from the DOM and expect the Blob type defined in the DOM typings. These two Blob types are not the same
The fix is to remove the import for Blob. This should use the global Blob type defined in the DOM typings (provided you have explicitly excluded the DOM typings from your project)


Econometrics online learning course

Question: i would kindly like to ask you for recommendations regarding introductory, but more importantly advanced online econometrics courses (theory as well as applied, preferably in R). I would be most interested in free courses if possible.
Thanks a lot in advance.
Best,
Phil
Answer: Hi Phil as a fellow economics disciple, I would like to say that book is always the best material for the learning, but the online course is the complimentary or an enhancement for learning process.
I give you both the Youtube channels that pretty much sum up all the econometric course for undergrad and master and the books. The links are below, if you prefer mathematic you can just go ahead and watch master course.
For the R programming, if you are already familiar with the STATA, you can just learn R from any where that related to the statistical model used in econometrics. But I would recommend books instead.
Applied Econometrics with R
The greatest one for econ student to learn R, this will take a lot of time to digest into you brain so give it a time. If you are new with R programming you should learn the material separately by going through econometric theory first and then R.
Introduction to statistical learning with R
This one pretty much give you the intuition of how to create and build the Logistic regression, and linear regression but not account for the violation of Gauss Markov assumption. It is not that good for econ student but it give you much more insight about data.
Applied predictive model
This one is pretty much straight to the point for forecasting
[master]https://www.youtube.com/watch?v=GMVh02WGhoc&list=PLwJRxp3blEvaxmHgI2iOzNP6KGLSyd4dz
[undergrad]https://www.youtube.com/watch?v=M_5SLG7sUa0&list=PLwJRxp3blEvZyQBTTOMFRP_TDaSdly3gU
[statistics in R]https://www.youtube.com/watch?v=riONFzJdXcs&list=PLqzoL9-eJTNBDdKgJgJzaQcY6OXmsXAHU
Answer: Hint: there is a lot of courses branded as "statistics" or "data science", while not many as "econometrics", so try the two former keywords. The topics covered by such courses would overlap to great extent with econometrics. Moreover, such courses would cover a general set of skills for working with data, data analytics, and modelling, so they would surely be useful for econometrics as well.

Econometrics online learning course


jbutton does not work

Question: Hi I have the following code where I have a start and stop button . when start is pressed canvas starts painting but I cannot press stop button until the start operation is done . I need to stop and resume the paint on the button press . Once the start button is pressed the other buttons cannot be pressed till the canvas is painted.

<code>import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.Timer;

public abstract class JUIApp extends JPanel implements ActionListener {
    public static CACanvas cacanvas= null;
    public ArrayList<int[]> genL;
    public JFrame frame = null;
    protected JPanel mainPanel = null;
    private JButton btn0 = null;
    private JButton btn1 = null;
    private JButton btn2 = null;
    @SuppressWarnings("rawtypes")
    DefaultComboBoxModel rules = null;
    @SuppressWarnings("rawtypes")
    JComboBox rulesCombo =null;
    JScrollPane ruleListScrollPane=null;
    JLabel lable=null;
    JTextField generation=null;
    JLabel gLable=null;
    public static String Rule;
    public  static String Generations;
    public boolean isChanged =false;
    public int gridCellSize=4;
    private static final long serialVersionUID = 1L;
    public int genCurrent=0;
    public int posCurrent=0;
    public int i;
    public Color cellColor= null;
    public Timer waitTimer;
    public static boolean waitFlag;
    public static boolean alert1Flag=false;
    public boolean stopFlag=false;
    public JLabel Alert1=new JLabel();
    public int genCheck=0;
    //private List<Point> fillCells;
    public JUIApp() {
        initGUI();

    }
    public void initGUI() {
        //fillCells = new ArrayList<>(25);
        frame = new JFrame();
        frame.setTitle("Cellular Automata Demo");
        frame.setSize(1050, 610);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLayout(new BorderLayout());
        frame.add(getMainPanel(),BorderLayout.NORTH);
        frame.setVisible(true);
        Toolkit.getDefaultToolkit().setDynamicLayout(false);
         cacanvas=new CACanvas();
            frame.add(cacanvas);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public JPanel getMainPanel() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());
        //cacanvas=new CACanvas();

        btn0 = new JButton("Start");
        btn0.addActionListener(this);
        //waitTimer = new Timer(1000, this);
        mainPanel.add(btn0);

        JButton btn2 = new JButton("Stop");
        btn2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent s)
            {
                stopFlag=true;
                //cacanvas.repaint();
            }
        });
        mainPanel.add(btn2);
        btn1 = new JButton("Clear");
        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                CACanvas.clearFlag=true;
                generation.setText("");
                alert1Flag=true;
                rulesCombo.setSelectedIndex(0);
                  Alert1.setText("Please enter the number of generations");
                  Alert1.setBounds(30, 20, 5, 5);
                  Alert1.setVisible(alert1Flag);
                  mainPanel.add(Alert1);
                   cacanvas.repaint();
                   frame.setSize(1060, 610);

            }
    });
        mainPanel.add(btn1);
        lable=new JLabel();
        lable.setText("Select Rule :");
        mainPanel.add(lable);
        rules=new DefaultComboBoxModel();
        for (int i=0;i<=100;i++)
        {
            String p=String.valueOf(i);
        rules.addElement(p);
        }
        rules.addElement("250");
        rules.addElement("254");
        rulesCombo = new JComboBox(rules);
        rulesCombo.setSelectedIndex(0);

         ruleListScrollPane = new JScrollPane(rulesCombo);
        mainPanel.add(ruleListScrollPane);
        //mainPanel.revalidate();
        gLable=new JLabel();
        gLable.setText("Enter the number of Generations (Max 64)");
        generation=new JTextField(2);

        mainPanel.add(gLable);
        mainPanel.add(generation);
       // mainPanel.add(cacanvas);
        return mainPanel;
    }

    public abstract void run();

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Alert1.setVisible(false);
        waitFlag=false;
        System.out.println("We received an ActionEvent " + arg0);
         Generations=generation.getText();
        System.out.println(Generations);

         Rule = "";
        if (rulesCombo.getSelectedIndex() != -1) {
           Rule =
                   (String) rulesCombo.getItemAt
                (rulesCombo.getSelectedIndex());
        }
        System.out.println(Rule);
        int rule=Integer.parseInt(Rule);
     Hashtable<String,Integer> rules= new Hashtable<String,Integer>();
        CARule ruleClass=new CARule();
        rules=ruleClass.setRule(rule);
        CAGenetationSet sa =new CAGenetationSet(100, false,rules);
         genL=new ArrayList<int[]>();
         genL=sa.runSteps(Integer.parseInt(Generations));
         System.out.println("calling pattern set");
        for(int i=0;i<=genL.size()-1;i++)
        {
            System.out.println("Painting generation :"+i);

            if(stopFlag==false)
            {

               cacanvas.repaint();
            }

            //genPaint();
            //sleep();
            int[] newCell=genL.get(i);
            for(int r=0;r<newCell.length;r++)
            {

                if(newCell[r]==1)
                {

                    System.out.println("Generaton is"+i+"CellLife is"+r);
                    cacanvas.fillCell(i,r);

        }
            }

        }

                    /*cacanvas.patternSet(genL);
                    waitFlag=true;

                  System.out.println("run completed");
                 // cacanvas.clearFlag=true;
*/
    }

    public void genPaint()
    {
        cacanvas.repaint();
    }

    public void sleep()
    {
        try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        JUIApp app = new JUIApp() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println("Run method");

            }

    };
}
}
</code>
Comment: The likely reason is, you're blocking the Event Dispatching Thread, which is responsible for, among other things, handling paint event. You could use a Swing `Timer` or `SwingWorker` to resolve the issue.  See [How to use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for more details
Comment: Thanks for your reply . I went through the Concurrency notes but an example would be much easier to explain the concept . I am a beginner so do you mind editing the above code with a few lines of the concurrency concept.
Comment: Since you code is incomplete and not compilable or runnable, it's hard to make suggestions about how best to implement either of the solutions'
Comment: For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/).
Answer: Seems like you are using the current Thread on that task, you should create another Thread and add one listeners code to the new Thread.

jbutton does not work


Eval é mocinho ou bandido?

Question: Falam muito mal do eval, mas, as vezes, me pergunto se ele realmente é o problema ou é a pessoa que está sentada na frente do computador (o suposto programador).
Minha vida inteira (na programação) tenho ouvido críticas, piadinhas, zoeiras em cima dessa função eval, mas o curioso é que, mesmo aqueles que zoam, acabam por usar bibliotecas que a utilizam internamente, sem saber.
Um exemplo disso é Laravel 3, que no sistema de views, utiliza eval.
Exemplo (removi os comentários do código original):
    public function get()
    {
        $__data = $this->data();

        $__contents = $this->load();

        ob_start() and extract($__data, EXTR_SKIP);

        try
        {
            eval('?>'.$__contents);
        }
        catch (\Exception $e)
        {
            ob_get_clean(); throw $e;
        }
        $content = ob_get_clean();

        if (Event::listeners('view.filter'))
        {
            return Event::first('view.filter', array($content, $this->path));
        }

        return $content;
    }

GITHUB - view.php - linha 385
É verdade, o mesmo já está descontinuado, mas não está numa era tão distante assim (a versão dele é para PHP 5.3). Porém não seria surpresa se um dos críticos de eval tivesse utilizando esse framework sem saber dessa informação.
E ainda eu dou outro exemplo, que muitos programadores também usam, mas sem saber. O SuperClosure que é utilizado no Laravel 4 e Laravel 5.
Essa biblioteca tem como finalidade prover uma possibilidade de serializar closures no PHP, já que nativamente as closures no PHP não têm suporte a serialização. Essa biblioteca se torna útil para quem precisa utilizar sistemas de filas, como o Beanstalkd, onde as informações passadas geralmente são uma string, e, portanto, os dados em PHP precisam ser serializados.
Exemplo:
    /**
     * Reconstruct a closure.
     *
     * HERE BE DRAGONS!
     *
     * The infamous `eval()` is used in this method, along with the error
     * suppression operator, and variable variables (i.e., double dollar signs) to
     * perform the unserialization logic. I'm sorry, world!
     *
     * This is also done inside a plain function instead of a method so that the
     * binding and scope of the closure are null.
     *
     * @param array $__data Unserialized closure data.
     *
     * @return Closure|null
     * @internal
     */
    function __reconstruct_closure(array $__data)
    {
        // Simulate the original context the closure was created in.
        foreach ($__data['context'] as $__var_name => &$__value) {
            if ($__value instanceof SerializableClosure) {
                // Unbox any SerializableClosures in the context.
                $__value = $__value->getClosure();
            } elseif ($__value === Serializer::RECURSION) {
                // Track recursive references (there should only be one).
                $__recursive_reference = $__var_name;
            }
            // Import the variable into this scope.
            ${$__var_name} = $__value;
        }
        // Evaluate the code to recreate the closure.
        try {
            if (isset($__recursive_reference)) {
                // Special handling for recursive closures.
                @eval("\${$__recursive_reference} = {$__data['code']};");
                $__closure = ${$__recursive_reference};
            } else {
                @eval("\$__closure = {$__data['code']};");
            }
        } catch (\ParseError $e) {
            // Discard the parse error.
        }
        return isset($__closure) ? $__closure : null;
    }

GitHub - SerializableClosure.php - linha 187.
Pensando na utilidade das classes das bibliotecas apresentadas acima, lembrando que são só dois exemplos, mas deve haver outros casos, fiquei me questionando se eu deveria ainda continuar falando mau do eval ou simplesmente fazer uma análise e avaliar quando é bom ou ruim usá-lo.
Por outro lado, se eu pensar que "todo eval, independente de como é implementado, é um risco", provavelmente vou ter que parar de usar as bibliotecas que citei acima, e fazer um trabalho todo manual...

Quais são os riscos de usar eval num projeto?

Devo sempre evitá-lo?

Se a resposta for sim para "devo evitá-lo", então eu também deveria deixar de usar bibliotecas que o implementam? Afinal de contas, isso não pode pôr meu código em risco?

O eval deve ser utilizado em alguns casos bem específicos (como por exemplo, somente em geração de código) ou posso utilizá-lo livremente, desde que eu tenha consciência do que estou fazendo?

Nota: Favor não editar e colocar tags como php ou laravel, pois os exemplos foram apenas para mostrar casos onde eval foi "bem recebido". Se houver exemplos de outras linguagens que o usem, será bem-vindo.
Comment: Eu poderia [dar esta resposta](http://pt.stackoverflow.com/a/27688/129) aqui, apesar dela ser sobre JavaScript. Mas não sei bem que tipo de resposta procuras. O `eval` existe, e não há um veredicto usa/não usa, cad um faz como quer.
Comment: @GuilhermeNascimento para dar ênfase: "O **mau uso** do eval pode levar a ataques de injeção de código"
Comment: Um "evalzinho" de vez enquanto não faz mal a ninguém, hehehe
Comment: @Sergio quero deixar essa pergunta aqui mais para quebrar o preconceito (principalmente o meu, que aprendi mecanicamente). Mas ao ver essas libraries, realmente me incomodou lembrar essas críticas
Answer: Vou começar dizendo que as pessoas usam bibliotecas sem saber se elas têm qualidade ou não. Assim, como usam técnicas, ferramentas, metodologias, paradigmas, tecnologias, cursos, etc. sem saber se é bom para ele naquele contexto.

Quais são os riscos de usar eval num projeto?

Com a injeção de código ou dados maliciosos é possível comprometer todo o sistema de execução. O eval() executará o texto que receber sem nenhuma preocupação. Frequentemente o programador imagina que virá algo confiável, mas costuma acontecer o oposto.

Devo sempre evitá-lo?

Sim, deve evitá-lo "sempre". Mas se tiver uma necessidade real pode usar se souber o que está fazendo.

Se a resposta for sim para "devo evitá-lo", então eu também deveria deixar de usar bibliotecas que o implementam? Afinal de contas, isso não pode pôr meu código em risco?

Se a biblioteca for bem feita não há um grande problema. Fico pensando se a biblioteca usou isso porque os programadores dela são preguiçosos ou havia um motivo real para seu uso. Se for o primeiro, esse motivo por si só deveria ser um alerta para não usar essa biblioteca.
A decisão deve passar por uma análise do código e ver se eles sanitizaram toda entrada da função de forma adequada. Se isto foi feito o perigo é pequeno. Sempre pode falhar. Pode ser que fizeram quase direito, mas deixaram um brecha que alguma alteração no código pode abrir caminho para uma vulnerabilidade.

O eval deve ser utilizado em alguns casos bem específicos (como por exemplo, somente em geração de código) ou posso utilizá-lo livremente, desde que eu tenha consciência do que estou fazendo?

Se for usar geração de código do jeito correto, tenho dúvidas se o eval() realmente é necessário. Se for algo que não fica exposto, já é algo positivo. Até pode usar livremente se tiver consciência do que está fazendo, mas se tiver mesmo, não usará a não ser que seja fundamental. Sanitizar pode ser mais complicado do que a pessoa imagina. Claro que sempre tem exceções, mas quando há alguma, normalmente era simples de resolver sem o eval() já que não precisa sanitizar nada.
Utilidade do eval()
Não me lembro de um problema que tive que lidar que o eval() fosse necessário a não ser quando usei uma linguagem muito ruim que não permitia outras formas. Só para ter uma ideia era uma linguagem que não tinha array, função, e referência. O dia que essa linguagem passou ter essas coisas, tudo se resolveu sem avaliar código fonte em tempo de execução.
Fazer certo com ele é tão complicado que fazer o certo sem ele é tão ou mais fácil. Quase todos os usos da função só fica mais simples se deixar brechas.
No fundo o eval() em si não é o problema, pelo menos não mais que o SQL. Claro que o eval() permite causar estrago maior (mas pode até ser menor em certos casos). O perigo é não sanitizar o texto.
É óbvio que em scripts próprios, internos, coisas muito simples, ou mesmo em casos que a fonte de texto é controlada 100% pela aplicação, o seu uso pode ser minimamente adequado, ainda que provavelmente ainda não necessário.
Além do problema de segurança há problemas de performance e manutenibilidade. Por isso, mesmo nos casos que a segurança não seja problema, convém não abusar.
O perigo dele é que é viciante. A pessoa gosta de ter o trabalho aparentemente simplificado, e começa abusar.
JavaScript
Curiosamente o pessoal fala muito mal do eval() em JavaScript e de fato é melhor evitá-lo, mas pensa bem, que diferença faz ter um eval() em um código que o usuário tem total controle? O problema do JS é mais embaixo. Claro que falo do uso no browser. Se usar Node, Deno ou outra tecnologia para uso no backend, o eval() deve ser evitado, ou pelo menos use com perfeita sanitização.
Coloquei no GitHub para referência futura.
Comment: @rray Seria. E dos mais poderosos, fácil de usar e abusar. Difícil de usar certo, difícil de usar quando as coisas não saem como o esperado. É uma Kombi sem freio na ladeira. Tudo muto bom, se nada atravessar seu caminho.
Comment: Fala mal de `eval` no Javascript, mas usa `new Function`... Tá de brincadeira, né
Comment: O NPM nunca foi confiável.As pessoas usam, ams o fato da arquitetura ter as permissões que são descritas nesse artigo (emque um ataque simples, e destruidor é descrito, mas é só um bônus) não é seguro. Só que as pessoas usam. por que é o que "tem" para Javascript e o que os develeopers front-end sabem usar.
Comment: Acho que o problema em geral não é do recurso `eval`, mas sim de quem está usando. Até hoje eu nunca tive que usar o `eval`, mas vivia vendo códigos como `eval('abc = 3')`, onde as pessoas saiam usando um recurso desnecessariamente somente porque aprendeu ele.
Comment: A ideia do `eval()` seria adicionar um suporte bem básico/ ou caracteristicas de metaprogramming na linguagem?
Comment: O NPM também era confiável. https://www.kb.cert.org/vuls/id/319816
Comment: @jsbueno a intenção do comentário é justamente este, as pessoas acreditam que as coisas são confiáveis, sem ser. Note que o início da resposta vai nesse sentido.
Comment: SIm - o comentário era concordando - na verdade, ainda estou meio alterado depoi de ler esse artigo- não conheço bem o NPM - sabia que era ruim, ms não tinah idéia de que era algo *tão* ruim.
Comment: @JedaiasRodrigues JS tem inúmeros problemas, mas esse especificamente é que ele roda sob controle do usuário, tudo pode ser mudado facilmente. E outro problemas dele é que atrai muito programador ruim que não entende o que está fazendo. Curiosamente a maioria das linguagens não sofrem tanto com programadores ruins, JS sofre, dá para dar muita margem pra estrago com ele, usando `eval()` ou não. Tem sites que está sendo usado pra coisas até criminosas há anos e quem fez ou quem é dono nem sabe, porque é tudo feito nas coxas.
Comment: Bigown, o interessante das "bibliotecas serem de confiança ou não" é que no Laravel essas bibliotecas são instaladas "sem nossa escolha", pois são dependência do projeto. Por isso, **quero acreditar** que sejam confiáveis... Também, né, de onde o PHP tirou essa ideia de não poder serializar uma closure.. Enfim, é isso aí
Comment: @Maniero muito obrigado por sua resposta, ela está muito boa. Só fiquei extremamente curioso em relação ao problema do JS, você poderia me ajudar nisso por favor?
Answer: Considere que cada técnica, padrão ou filosofia de programação que você aprende é uma arma.
Algoritmos com estruturas de dados são uma pistola, você precisa aprender a manejar pelo menos isso se quer ir pra frente de batalha. Daí vem a orientação a objetos, que é um fuzil - você só se torna um soldado eficiente quando consegue montar e desmontar seus objetos de olhos fechados. Padrões de projeto são fuzis de franco-atirador, você destrói problemas específicos com muita precisão. Análise de algoritmos (entender complexidade e notação O) é um lançador de mísseis - não importa o quanto o problema cresça, você vai gastar a mesma munição para acabar com ele.
O eval é como um míssil nuclear balístico intercontinental:

Tem o potencial de resolver problemas que seriam muito difíceis de resolver de outra forma. Nesse caso, inclusão de arquivos externos, como o Laravel faz. Porém...
A opinião pública vai te odiar. Você vai ouvir de muita gente que o que você está fazendo é uma atrocidade;
Vai ter muito dano colateral: você está potencialmente abrindo seu código para vulnerabilidades de injeção de Javascript. No lado do cliente (navegador) isso não é tão grave pois em geral todo o código que roda no navegador está exposto a pessoas mal intencionadas. Mas se você usa JS no servidor (node.js, por exemplo) e realiza eval em algo que venha do lado cliente... Que o Deus da programação tenha piedade da sua alma;
A radiação demora séculos para se dissipar - a manutenção do seu código vai ficando cada vez mais complicada, já que:
** Praticamente nenhuma ferramenta de análise de código vai querer encostar no eval;
** A depuração se torna muito mais complicada;
** Os fluxos ficam mais difíceis de serem mapeados.

Pra finalizar, vou falar por experiência própria. Na maioria das vezes em que as pessoas usam eval, elas poderiam ter resolvido seus problemas por outros meios. Na tentativa de amenizar os problemas citados acima, acabam fazendo mais coisas que não são recomendáveis e os custos de manutenção viram uma bola de neve. Se você pensar em usar o eval, certifique-se de que seus problemas não podem ser resolvidos de nenhuma outra forma antes de levar a ideia adiante.

Eval é mocinho ou bandido?


Is it valid to use the algebra of limits backwards?

Question: I'm aware that we can say that the limit of, for example, (f + g) at a is equal to the limit of f at a summed with the limit of g at a, given that these two limits exist, but is it true that...
assuming lim (f + g) at a exists, then lim f at a + lim g at a = lim(f + g). Or, is this nonsense, It's very late at night and I've spun myself into a web.
Comment: Consider $f(x)=1/x^2$ and $g(x)=-1/x^2$ as $x\to0$.
Answer: No, you need to additionally know that at least one of the individual limits exists.  For instance, let $f(x)$ be your favorite function such that the limit $\lim_{x\to a}f(x)$ does not exist, and let $g(x)=-f(x)$.  Then $f+g$ is identically $0$ so $\lim_{x\to a}(f+g)(x)=0$, but neither individual limit exists.
On the other hand, if you know that the limits of $f$ and $f+g$ (or $g$ and $f+g$) both exist, then the limit of $g$ must exist since $g=(f+g)+(-f)$, and so you get that the sum of the limits is the limit of the sum.


Solving "Square" PDE

Question: I'm trying to find all solutions to:
$$f_{xx}+2f_{xy}+f_{yy} = 0$$
I noticed that if we let $D = \frac{\partial}{\partial x} + \frac{\partial}{\partial y}$ then we are looking for the kernel of $D^2$. I'm not sure where to go from here though.
Comment: Hence every function such that $f_x=-f_y$ Is a solution, but are they all of this form?
Answer: Make a change of variables:
$$u=x-y\\v=x+y$$
Then
$$\frac{\partial}{\partial x}=\frac{\partial}{\partial u}+\frac{\partial}{\partial v}\\
\frac{\partial}{\partial y}=-\frac{\partial}{\partial u}+\frac{\partial}{\partial v}
$$
and the equation becomes much simpler to solve
$$4\frac{\partial^2 f}{\partial v^2}=0\iff f(u,v)=vG(u)+H(u)$$
where $G,H$ are arbitrary functions. Back-substituting yields the most general solution for the PDE:
$$f(x,y)=(x+y)G(x-y)+H(x-y)$$

Solving "Square" PDE


Split Elements and their values dynamicaly using xslt1 1.0

Question: I want to split these Elements:
<?xml version="1.0" encoding="UTF-8"?>
<CTP>
    <name>ABSA bank</name>
    <BAs>
        <BA>bank</BA>
        <BA>sector</BA>
        <BA>issuer</BA>
    </BAs>
    <altIDs>
        <altID>
            ABSB|BBG
            ASB|Reuters
        </altID>
    </altIDs>
</CTP>

into this format:
<?xml version="1.0" encoding="UTF-8"?>
    <CTP>
    <name>ABSA bank</name>
    <BAs>
        <BA>bank</BA>
        <BA>sector</BA>
        <BA>issuer</BA>
    </BAs>
    <altIDs>
        <aldID>
            ABSB
            <altSystem>BBG</altSystem>
        </aldID>
        <aldID>
            ASB
            <altSystem>Reuters</altSystem>
        </aldID>
    </altIDs>
</CTP>

im new with Xslt and im trying to solve this exercice, how can we do it in XSLT 1.0?
Highlighting the transformed part:
Original
<altID>
    ABSB|BBG
    ASB|Reuters
</altID>

Transformed
<aldID>
    ABSB
    <altSystem>BBG</altSystem>
</aldID>
<aldID>
    ASB
    <altSystem>Reuters</altSystem>
</aldID>

Comment: How is this different from: http://stackoverflow.com/questions/40678961/split-xml-element-into-many
Comment: its different because that algorithm is made for creating new elements and not spliting the element and their values and give each part of the element a part of their value. @michael.hor257k
Answer: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*[contains(name(), '_')]" name="nest">
        <xsl:param name="name" select="name()" />
        <xsl:param name="delimiter" select="'_'" />
        <xsl:param name="text" select="." />
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
        <xsl:choose>
            <xsl:when test="contains($name, $delimiter)">
                <xsl:element name="{substring-before($name, $delimiter)}">
                    <xsl:value-of select="$token" />

                </xsl:element>
                <xsl:element name="{substring-after($name, $delimiter)}">
                    <xsl:variable name="token" select="substring-after($text, $delimiter)" />
                    <xsl:value-of select="$token" />

                    <xsl:call-template name="nest">
                        <xsl:with-param name="name" select="substring-after($name, $delimiter)" />
                        <xsl:with-param name="text" select="substring-after($text, $delimiter)" />
                    </xsl:call-template>
                </xsl:element>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>


How to add new summernote toolbar without updating all pages?

Question: I have summernote toolbar in several pages, I have specified toolbars in all pages, so if i need to add new toolbar I want to update all pages , how can i add toolbar in summernote.js so that i dont want to update all pages ?
Answer: Maybe you should init several text editors. For example on one page it will be something like:
<code>$('#page1Element').summernote({
  toolbar: [
    ['style', ['style]],
    ['color', ['color']]
  ]
});
</code>
and for another page:
<code>$('#page2Element').summernote({
  toolbar: [
    ['style', ['style]],
    ['insert', ['link', 'picture']]
  ]
});
</code>
Comment: Thanks for the replay Alex but you are asking me to update all pages , but i dont want to update all pages , i just need to add the toolbar in summernote.js file so i can access the toolbar in all pages. (i am not using the default toolbars , so i cant specify it in summernote.js default toolbar method )