Deferred rendering how to exclude meshes from light contribution

Question: I've set up a deferred rendering pipeline using Qt3D. So far so good, but now I'd like to add selection boxes around the meshes rendered in the scene.
Unfortunately, the selection box mesh is affected by lights and I don't want that (See "shaded" picture below). For example I might want to set the ambient light intensity to 0 and the selection boxes should still be visible. Another example is if I want to show the typical X/Y/Z indicator in one of the viewport corners.
I've got the typical light pass shader for light contribution calculation, which is (in short) this:
void main()
{
    vec2 texCoord = gl_FragCoord.xy / winSize;
    vec4 col = texture(color, texCoord);
    vec3 pos = texture(position, texCoord).xyz;
    vec3 norm = texture(normal, texCoord).xyz;
    ... calculate lights contribution...
    fragColor = vec4(col.rgb * lightColor, col.a);
}

If I replace the fragColor line with:
fragColor = vec4(col.rgb, col.a);

I get the non shaded picture below, which shows the color I'd want for the selection box.
Is there any trick or common practice to exclude specific meshes from the light pass ?
I already tried to "trick" the shader with special values like color alpha = 0.123 but it doesn't help at all.
Shaded

Non shaded
Answer: You basically have two options:

Store somewhere in the gbuffer when a pixel should not be lit. This can either be done by a flag in one of the color attachments or by storing a special value in the depth or stencil buffer.
Render all primitives that don't need lighting after the illumination stage over the illuminated scene rendering.

Both options have their advantages/disadvantages. With a flag, it can sometimes be more efficient if you have large region that don't need illumination because you can skip these calculations in the shader. On the other hand, this solution might need more memory because you have to store this information somewhere. In the second version, you have to make sure that you still respect the depth buffer created while generating the gbuffer (otherwise they would not be hidden behind illuminated objects), which might be as simple as attaching the same depth attachment or quite complex (if the illuminated data is rendered directly to the backbuffer).
Comment: BDL, thanks, I think I will opt for the second. 10 minutes after I posted this, I realized I could forward render those meshes with a separate shader. My only concern is how they will look when mixed with the gbuffer fragments. I will post an update if I achieve something good.


PHP How to get function parameters, when function is passed as parameter

Question: I want to do something like this:
function func($callback) {
    $result = $callback(???);    // $callback must be called here
    //...
}

//...
func(function(['foo' => 'buu']) {
    $a = func_get_arg(0);

    // do something with $a and return something...
    return $something;
})

It is possible in php?
I can do something like below, but this is not what I want to do:
function func($args, $callback) {
    $result = $callback($args);
    //...
}

func(['foo' => 'boo'], function($args) {
     $a = $args; // etc.
})

Comment: Does this help? https://stackoverflow.com/questions/52200682/check-if-callable-can-receive-class-as-parameter-in-php/52200757#52200757
Comment: It seems to me like `['foo' => 'boo']` is being set where the callback is set, so why not just assign `$a` to `['foo' => 'boo']` in the callback instead of trying to use `func_get_arg(0);`?
Comment: @Davіd: You're right. But this is only stupid example.
Comment: @Barry. Thank You, this solved my problem :)
Answer: I use anonymous function this ways :
$mySuperFunction = function($arg)
{
    echo ("Hello " . $arg);
};

function AnotherFunction($args)
{
    echo ("Another hello " . $args);
}

function WrappingAnonymous($callback, $args)
{
    $callback($args);
}

function WrappingAnonymousWithoutArgs($callback)
{
    $callback();
}

WrappingAnonymous($mySuperFunction, "World");
WrappingAnonymous("AnotherFunction", "World");
WrappingAnonymous(function($someArgs)
{
    echo "Yet another Hello " . $someArgs;
}, "World");

WrappingAnonymousWithoutArgs(function($someArgs = "World")
{
    echo "Now, a 4th other Hello " . $someArgs;
});

Outputs :

Hello World
Another hello World
Yet another Hello World
Now, a 4th other Hello World


core data relationship retrieve using key

Question: I have the following data model relationship (attributes in parenthesis)
Day (date) ----------->> Event (date, name)
There is a one to many relationship from Day to Event. There is no inverse relationship (i.e the Event entity doesn't have a reference to Day)
The Events are created first and saved. The Day gets created later.
Is there a way when retrieving the Day entity to have core data also go and retrieve every Event that has the same date as the date of that Day? I have searched some other questions, but all of them include manually setting the Events when the Day is created.
Answer: You query into CoreData with NSFetchRequest, which can only query one specific entity type (hence the initWithEntityName(_:) initializer).  If you do not have a relationship set between the two entities, you cannot retrieve both of them with a single NSFetchRequest instance (you may be able to use the same NSPredicate, given that you have a date attribute on both).
Having said that, as the answers to the other questions probably indicate, if you have a relationship set you can set the property relationshipKeyPathsForPrefetching on the NSFetchRequest entity to whatever your relationship key is to fetch the entities for a relationship when you fetch the initial entities.  Assuming your goal is to avoid multiple trips to the database, you may also want to consider setting returnsObjectsAsFaults to false (only do this if you have a relatively small number of objects).
Comment: From the documentation, that will retrieve Events that are already associated with a Day entity. My question is how to retrieve them without manually associating them
Comment: @robola As I said in my answer "If you do not have a relationship set between the two entities, you cannot retrieve both of them with a single `NSFetchRequest`."
Comment: I do have a relationship. Unless I'm misunderstanding what you mean, I have a one to many relationship between the Day entity and the Event entity
Comment: @robola I'm not referring to the relationship in your data model, I'm referring to a particular Day instance and one or many Event instances being assigned to one another (via the relationship in your data model).  If you haven't set that, then there is no way to do what you ask with one fetch request.
Comment: Ah. I understand now. Thanks. Marking as answer
Answer: As Charles said, just make two fetch requests. The first request to retrieve the Day, then a second request to retrieve the Events for the same date.
What I would do, though, is to set up the inverse relationship and change the timing of object creation. When creating an Event, if there is no Day object to link it to, then create a new Day object at the same time. If you did it this way, you could then just fetch a Day by itself, and its Events would be automatically available via the relationship.
Comment: I marked Charles' as the answer since he answered first, but two fetch requests is what I ended up doing. I was trying to see if core data could do it. There are two reasons I can't do the inverse suggestion, the first is that there is not guaranteed to be a Day object for every date, and even if there was, at the time the Event is created, I don't have enough information to create it. This way I can avoid having to check if the Day object is valid when I retrieve it.


Can I resolve a Promise from the outside?

Question: Using ES2015, can I resolve a promise from the outside i.e. trigger a resolution after its creation?
Like
const promise = new Promise();
promise.then(() => foo());
promise.resolve(); // foo() gets executed

Comment: *After* does not necessarily relate to *outside*? In general, no, and there's no need to. Also a `promise.resolve` method would definitely be an antipattern.
Comment: You might want to have a look at [Promises for promises that are yet to be created](http://stackoverflow.com/q/37426037/1048572)
Answer: Yes you can.
let resolvePromise = null;
const promise = new Promise(resolve => resolvePromise = resolve);
promise.then(foo => console.log(foo));
resolvePromise('bar');

Answer: Sure we can. just take the reference of the function outside and call it.
Since functions are object(which are stored as reference in variables) we can call resolve function from outside after taking its reference outside.
 var a;
    function b(){
        var c = new Promise( function(resolve, reject){
            a=resolve;
        });
        return c;
    }
    b().then((data) => {
    console.log(data);
    }
    );
    a("hai");

Comment: It's basically identical with the other answer.


Como iterar com dicionários em Python 3?

Question: Eu tenho dois arquivos e preciso gerar um terceiro a partir deles.
Primeiro arquivo chamado "de_para".

de_para.txt

Dentro desse arquivo tenho o seguinte conteúdo de exemplo:
Ordem   Campo_para  Campo_de
1   nome_emp    RAZA
2   ende_emp    ENDE
3   cepe_emp    NCEP
4   cida_emp    CIDA

"Ordem" seria a ordem em que os dados serão impressos nas linhas no terceiro arquivo;
"Campo_para" é o nome da coluna que será gravada no terceiro arquivo.
"Campo_de" é o nome da coluna que será procurada nas chaves do segundo arquivo chamado "empresas.txt"

O segundo arquivo "empresas.txt" tem a seguinte composição:
RAZA    ENDE    NCEP    CIDA
empresa1    rua1    cep1    cidade1
empresa2    rua2    cep2    cidade2
empresa3    rua3    cep3    cidade3
empresa4    rua4    cep4    cidade4
empresa5    rua5    cep5    cidade5

Como posso fazer a iteração dessa situação?
Com esse código eu criaria a lista com as colunas do primeiro arquivo:
for dict_col_geempre in read_geempre:
    hash_geempre.append(dict_col_geempre.get("Campo_de"))

Não estou conseguindo fazer a verificação da seguinte maneira:
Preciso verificar se a coluna "RAZA" do segundo arquivo existe no hash_geempre, caso exista, pegue o valor dessa chave no segundo arquivo "empresas.txt" e assim por diante e depois grave tudo isso num terceiro arquivo de nome "empresas_validas.txt".
Daria para fazer de modo diferente. Setando uma variável para cada coluna do segundo arquivo "empresas.txt", mas pensei em fazer com um arquivo "de_para" para ficar melhor penso eu.
Como posso fazer isso?
Answer: Vamos por partes. Primeiro leia o arquivo de_para, e guarde cada linha (exceto a primeira) em uma lista (usando with para abrir o arquivo, pois ele garante que o arquivo é fechado ao final do bloco):
de_para = []
with open('de_para.txt', 'r') as de_para_arq:
    next(de_para_arq) # pula primeira linha
    for linha in de_para_arq:
        de_para.append(linha.split())
# ordena a lista de acordo com o campo ordem
de_para = sorted(de_para, key=lambda x: int(x[0]))

Eu uso split para separar a linha por espaços, e o resultado é uma lista, onde o primeiro elemento é a coluna ordem, o segundo é a coluna "para" e o terceiro é a coluna "de". Cada linha do arquivo será uma lista assim, e de_para será uma lista contendo todas essas listas (uma para cada linha do arquivo).
Em seguida ordenei a lista de_para com base na primeira coluna (usando int para converter a string para número), para garantir que os elementos estarão na ordem correta (se essa coluna existe, estou assumindo que as linhas não estarão necessariamente em ordem no arquivo, caso contrário não faria sentido essa coluna existir).

Agora vamos ler o arquivo de empresas:
empresas = []
with open('empresas.txt', 'r') as empresas_arq:
    headers = next(empresas_arq).split() # nomes dos headers
    for linha in empresas_arq:
        empresa = dict()
        for header, valor in zip(headers, linha.split()):
            empresa[header] = valor
        empresas.append(empresa)

Primeiro eu faço split na primeira linha para pegar os nomes das colunas.
Depois, para cada linha do arquivo, eu percorro tanto a lista de headers quanto os elementos da linha (o uso de zip permite percorrer as duas listas ao mesmo tempo, assim eu tenho o valor correspondente ao nome de cada coluna). Essa solução assume que o arquivo é bem formado e sempre tem todas as colunas.
No fim eu tenho uma lista de empresas, sendo que cada empresa é um dicionário, que mapeia os nomes das colunas com os respectivos valores. Por exemplo, o primeiro elemento da lista de empresas será:
{'RAZA': 'empresa1', 'CIDA': 'cidade1', 'ENDE': 'rua1', 'NCEP': 'cep1'}

Assim, eu tenho o nome da coluna atual e o respectivo valor. Cada linha do arquivo de empresas será um dicionário assim, e a lista empresas terá todos esses dicionários (um para cada linha do arquivo).

Agora é só escrever o terceiro arquivo:
with open('empresas_validas.txt', 'w') as out:
    # escreve os headers
    out.write(' '.join(para for _, para, _ in de_para))
    out.write("\n")
    for emp in empresas:
        out.write(' '.join(emp[de] for _, _, de in de_para))
        out.write("\n")

Primeiro eu escrevo os headers, usando os nomes que estão na lista de_para. Eu uso join para unir os nomes, separando-os por espaço. Para facilitar, use a sintaxe de list comprehension, bem mais sucinta e pythônica.
Depois, para cada empresa, eu pego o valor da coluna "de" que está na lista de_para (como a lista foi ordenada, eu garanto que os campos são escritos na ordem desejada). O resultado será o arquivo:
nome_emp ende_emp cepe_emp cida_emp
empresa1 rua1 cep1 cidade1
empresa2 rua2 cep2 cidade2
empresa3 rua3 cep3 cidade3
empresa4 rua4 cep4 cidade4
empresa5 rua5 cep5 cidade5

Se quiser, também pode trocar os loops que leem os arquivos por list e dict comprehensions:
with open('de_para.txt', 'r') as de_para_arq:
    next(de_para_arq) # pula primeira linha
    de_para = [ linha.split() for linha in de_para_arq ]
# ordena a lista de acordo com o campo ordem
de_para = sorted(de_para, key=lambda x: int(x[0]))

with open('empresas.txt', 'r') as empresas_arq:
    headers = next(empresas_arq).split() # nomes dos headers
    empresas = [ { header: valor for header, valor in zip(headers, linha.split()) } for linha in empresas_arq ]

with open('empresas_validas.txt', 'w') as out:
    # escreve os headers
    out.write(' '.join(para for _, para, _ in de_para))
    out.write("\n")
    out.write("\n".join( ' '.join(emp[de] for _, _, de in de_para) for emp in empresas))

Comment: Muito Obrigado @hkotsubo, funcionou perfeitamente do modo que eu preciso.
Parabéns pela dedicação e tempo disposto a explicar muito bem detalhado o processo.
Comment: @CleberNandi Depende do que quer fazer. Se quer testar uma condição, pode fazer `out.write(' '.join(emp[de] for _, _, de in de_para if condicao))` (a condição pode ser qualquer expressão válida de um `if`, aí ele só vai pegar as chaves que satisfazem a condição), ou então `out.write(' '.join(emp.get(de, valor_default) for _, _, de in de_para))` - nesse caso, `emp.get(de, valor_default)` retorna o valor_default se a chave `de` não existir.
Comment: Cada elemento do de_para é uma lista com 3 elementos, então `for a, b, c in de_para` já faz com que a, b e c sejam os elementos de cada lista. Geralmente usa-se `_` para indicar que aquele valor não interessa e só vou usar os outros (é uma convenção da linguagem, poderia ser qualquer nome, mas usa-se `_` para indicar que aquele valor não será usado)
Comment: Tenho uma dúvida quanto a essa linha:

out.write(' '.join(emp[de] for _, _, de in de_para))

Que fica na parte da escrita do arquivo.

Preciso validar ou tratar o campo "de" antes da gravação, como faria nesse caso?
Comment: Tentei entender sozinho mas não consegui.
Poderia me ajudar a entender a linha:

out.write(' '.join(emp[de] for _, _, de in de_para))

Especificamente o "for _, _, de in de_para".
Não entendi o por que desses dois underlines, o "de" entendi, mas os underlines não.

Outra coisa que preciso fazer, tentei e me enrolei é alterar essa linha que passei acima para criar uns validadores antes de gravar a linha.
Comment: Tentei isso mans não deu:

                for _, _, Campo_Concorrente in geempre_list:
                    campo = emp.get(Campo_Concorrente, "NULO")
                    empresa_linha_list.append(campo)
                # out.write("\n".join(emp.get(Campo_Concorrente, "NULO") for _, _, Campo_Concorrente in geempre_list))
                    out.write("\t".join(linha for linha in empresa_linha_list))
                    out.write("\n")
Grato.
Comment: @CleberNandi De qualquer forma, não entendi exatamente qual o problema que está tendo. Eu sugiro que faça [outra pergunta](https://pt.stackoverflow.com/questions/ask) com o problema específico. É que o site funciona assim: uma pergunta para cada problema específico. No caso - até onde entendi - já é algo diferente do problema relatado aqui (embora relacionado, mas diferente). É até melhor outra pergunta porque tem mais espaço que nos comentários, e ainda dá pra formatar o código (ainda mais em Python, que identação faz toda a diferença para entender o código)
Comment: Grato.
Farei outra pergunta.
Comment: @CleberNandi Outra vantagem de fazer outra pergunta, é que ela fica visível na página principal para todos os usuários, e aumenta suas chances de ter uma resposta. Já aqui nos comentários, menos gente vai ver... Não esqueça de colocar todo o contexto necessário (nem todos vão saber ou lembrar que vc já fez essa pergunta aqui, faça a outra pergunta de forma que ela seja "independente" dessa).
Comment: Farei isso, grato. Muito agradecido.


Term for two notes played together only a half step apart

Question: What is the term for two notes played on a piano that are only a half step apart, such as a C and a C# being played together?
Comment: I didn't vote to close at first, but the variety of the answers should show that the question needs clarification.
Answer: It's just called a half step, or a minor second. (Technically, a minor second would be C and Db.) More generally, it could be called a dissonance. Also generally it could be called a chord, though more often than not chords are defined as having three notes rather than two — some people call it a "simultaneity" for clarity.
Comment: I wouldn't call it a *"chord"*.  I usually think that chords need at least three notes.  I would call it an interval.  And a minor second interval.  (I realize that an *"open fifth chord"* is the same as a perfect fifth interval.)
Answer: To sum up what others have already said, C and C# are not notes you're likely to see together. If anything, you're much more likely (and even that is not particularly often) to see C and Db. Which is the interval of a minor second.
Other examples of minor seconds are E and F, or F# and G, or A and Bb, or C# and D, and so on. B# and C# (which is even less common than C and Db) would also make for a minor second.
C and C# would be called an augmented unison, but as noted, it's rare.
Saying "semitone", "half tone" or "half step" is also correct.
Comment: "Lionel [rejected premiering the Walton Viola concerto because he] couldn't imagine that a chord containing [both] C sharp and C natural could possibly sound well." - Lionel Tertis' wife Lilian.
Answer: Usually, this is called a minor second. Rarely it's notated as an augmented unison or doubly diminished third. The label depends on the use.
Comment: I think it could even more rarely be a doubly diminished second, e.g. B# to Cb.
Comment: @BobBroadley: Such a thing could plausibly occur in a transition from C# minor to Gb major.  I think an editor would probably strongly encourage using F# major instead of Gb, but there could be reasons for favoring the six-flats key signature.
Comment: @supercat, while I *love* your comment, I'd also love to see you find one in a real piece…!
Answer: C and C♯ played together doesn't have a specific name - as both notes are Cs.
Change that C♯ to a D♭, though, and there is an interval name available. Any C>D is a 2nd, so here it will become minor 2nd
Another very slight possibility is that the upper note may be an E of some sort, and E♭♭♭ would fit the bill enharmonically. In that remote case, the interval C>E♭♭♭ would be called double diminished. A rare beast indeed!
One could also take the C♮ note to actually be (enharmonically) B♯, with similar implications. But the two notes played together will 99% of the time be written C and D♭.
But, as you say, half-step will suffice, most will understand that it's a semitone between them.
Comment: @Divizna - I got fed up by the end, but certainly, technically, C and Bx would be yet another (weird) interval. Probably named ... haven't a clue, as intervals are calculated from the lower note - and which would that be..? Theory gone mad!
Comment: What about C and Bx? (Really ugly. But impossible?)
Comment: Doubly diminished second? It would be a very wtf interval anyway.
Comment: C to C♯ is called an augmented unison; it's just that in 12-TET it's enharmonically equivalent to the minor second.
Answer: If it's C and C♯ we can label it as an Augmented Unison.  If C and D♭, a Minor 2nd.   Just two notes aren't enough for a chord name, though C and D♭ could be part of D♭maj7.  Quite likely it's not harmonic at all,just a percussive effect.


Relationship between extended diatonic chords and modes?

Question: If we have C major, the diatonic chords are:
C, Dm, Em, F, G, Am, Bdim, C
If we turn each of these chords to diatonic 13th chord versions of themselves:
Cmaj13, Dmin13, Emin13, Fmaj13, G13, Amin13, Bdim13
The notes of these chords are now equivalent to the modes of each starting root.
C Ionian, D Dorian, E Phrygian, F Lydian, G Mixolydian, A Aeolian, B Locrian.
Here's a picture of what I mean from one of my other questions. So I was wondering if this is a known relationship in harmonic theory and are people using it. For example, using extended chords to modulate to modes and vice versa.
Comment: I don't quite understand the question, but it seems that you're confusing whether chords arise out of modes or modes arrive out of chords. The answer was originally that chords were natural consequences of several notes from a given mode sounding together, but more recently a mode could be derived from a specific chord or pitch collection construction (e.g. Scriabin's Mystic Chord: https://en.wikipedia.org/wiki/Mystic_chord)
Comment: Major 13th chords are a rarity. Dominant 13ths are commonplace. Usually with 13th chords, some of the thirds on the way are omitted. Just gets too busy. I think you're barking up the wrong tree.
Comment: The names of the 13th chords aren't quite right. For example, it should be Fmaj13#11, since the B natural of C major is the sharp version of the 11th over F.
Answer: You can see relationships between just about anything.  The real question is: is the relation meaningful?
For example, there's a strong correlation between murder rates and ice cream sales.  Will banning ice cream reduce the murder rate?  Answer: of course not, because the relationship is indirect - it's hot weather that raises both.
That's what you've got here.  Since all of the modes related to one scale have the same notes, all of the 13th chords formed with seven tones from the tertian series will have the same notes.  The relationship between both the chords and the modes are dependent on our definitions of mode and chord.
The reason it's not meaningful is that chords with seven notes are rarely voiced with all seven, and there's certainly no requirement to always voice a chord in root position.  Your Cmaj13 chord might be voiced as root-seventh-third-thirteenth, which would make it identical to first inversion voicings of Am(add9).  And because notes are left out of chord voicings in practice, even if you've identified C as the root in the context of a progression you could play in C Lydian over it without a problem, since the particular voicing lacks any F note.
Answer: Stacking all the thirds to the 13th gives you all the notes of the major scale, since all those modes you mention are derived from C Ionian, then it stands to reason that you will have that relationship.


How to calculate performance metrics when my model returns an interval?

Question: I'm doing time series forecasting using Prophet. My forecasting model returns not only a prediction y_hat, but also an uncertainty interval y_hat_lower and y_hat_upper.
I wanted to calculate some metrics, like RMSE, MAE, etc. to evaluate the model's performance but don't know how to incorporate the interval in the calculation.
Should I be trying to do that? Normally, how do people evaluate these kinds of predictions?
Comment: Hi @CarlosGDCJ, welcome to CV. What is `y_hat_lower/upper`? Is it a 95% confidence interval? Prediction interval? Mean +/- one standard error?
Comment: @AlexJ I edited the question adding more context.
Comment: Hm. I'm not sure. I am not familiar with an "uncertainty interval" - it appears to be something Bayesian but I don't know Prophet so can't say for sure. Usually, to calculate RMSE/MAE, you would calculate it with the [usual formula](https://en.wikipedia.org/wiki/Root-mean-square_deviation), which should be available if you have the raw data and the prediction `y_hat` from the model.
Answer: If these are prediction intervals (and thus not confidence intervals, there is a difference!) for a prespecified coverage probability, then one can use interval scores. These can be scaled to be comparable between different time series (if this is what you are working with).
If these are confidence intervals, then you are probably out of luck, because CIs by definition pertain to unobservable parameters.
Comment: My background in statistics is too weak for me to say I understood every word in your answer, but it was helpful nonetheless. I'm doing time series forecasting using Prophet, and my "interval" is Prophet's uncertainty interval. I initially thought it was a confidence interval, but after reading some of your references I'm leaning towards a prediction interval. If it isn't too much trouble, can you help me find out? [This is a short explanation about Prophet's interval](https://facebook.github.io/prophet/docs/uncertainty_intervals.html#uncertainty-in-the-trend)
Comment: That is a wonderful question, which I don't know the answer to (although it does sound a little more like it's PIs), so I posted it as such: [Are Prophet's "uncertainty intervals" confidence intervals or prediction intervals?](https://stats.stackexchange.com/q/619860/1352)
Comment: Thank you! It seems I'm dealing with prediction intervals. I'll look into interval scores then.


Is this statement about smooth functions true?

Question: Let $f: U \subseteq \mathbb{R}^2 \to \mathbb{R}^2$ where $U$ is an open subset of $\mathbb{R}^2$ be a $C^\infty$ map and let $g: U \to \mathbb{R}$ be a $C^\infty$ map with $g(u) \neq 0$ for all $u \in U$. Is it true that $f/g: U \to \mathbb{R}^2: u \mapsto f(u)/g(u)$ is a $C^\infty$-map?
A simple yes or no suffices.
Answer: The answer is yes. You can prove this using a common criterion from calculus: if $f$ and $g$ are continuously differentiable on $U$ and $g$ is nonvanishing on $U$, then
$$ \frac{d}{dx}\bigg(\frac{f}{g}\bigg)=\frac{f'g-fg'}{g^2}.$$
Now if you know that a sum and product of smooth functions is smooth, you can apply induction to conclude that $f/g$ is arbitrarily many times differentiable, i.e. of class $\mathscr{C}^\infty$.


Reduce an array based on closeness of numbers within objects

Question: I have an array that looks like this:
[
  { "begin": 870, "end": 889, "spanType": ["plan", "gt-plan"] },
  { "begin": 890, "end": 925, "spanType": ["plan", "gt-plan"] },
  { "begin": 926, "end": 938, "spanType": ["plan", "gt-plan"] },
  { "begin": 939, "end": 958, "spanType": ["plan", "gt-plan"] },
  { "begin": 7732, "end": 7790, "spanType": ["plan", "gt-plan"] },
  { "begin": 7791, "end": 7879, "spanType": ["plan", "gt-plan"] }
]

I need to loop through this and create an array that looks like this:
[
  { "begin": 870, "end": 958, "spanType": ["plan", "gt-plan"] },
  { "begin": 7732, "end": 7879, "spanType": ["plan", "gt-plan"] }
]

Basically, if a span.end is within 3 of the next span.begin, merge the two spans together.
Here is what I have now (not working) see fiddle:
spans.forEach(function(d,i) {
    if (i+1 <= spans.length - 1) {
    if (spans[i+1].begin <= d.end + 3) {
    d.end = spans[i+1].end;
     newSpans.push(d);
  }
    else {
        newSpans.push(spans[i]);
    }
  }
});

see fiddle
Comment: Use `reduce` instead?
Comment: do you need to check for sameness of `spanType`?
Answer: At first i would sort the spans so that we don't need to check all elements over and over:
spans.sort((a,b) => a.begin - b.begin);

Now we can easily go through and merge:
const result = [];
result.push(spans.reduce((prev,curr) => {
 if(prev.end < curr.begin - 1){
  result.push(prev);
  return Object.assign({},curr);
 }
 prev.end = Math.max(prev.end, curr.end);
 return prev;
}));

Try it
Comment: Thank you! I went with the other answer because they took into account >=3 like I had requested.
Comment: @mcestone yeah well just change `-1` to `-4` and thats it.
Answer: With sorted data, you could check the last inserted element with the actual element and if the delta is smaller than the wanted number, then adjust the end value.
This proposal mutates the original array. If that is not wanted, you need to get a copy of the object on pushing.

var array = [{ begin: 870, end: 889, spanType: ["plan", "gt-plan"] }, { begin: 890, end: 925, spanType: ["plan", "gt-plan"] }, { begin: 926, end: 938, spanType: ["plan", "gt-plan"] }, { begin: 939, end: 958, spanType: ["plan", "gt-plan"] }, { begin: 7732, end: 7790, spanType: ["plan", "gt-plan"] }, { begin: 7791, end: 7879, spanType: ["plan", "gt-plan"] }],
    result = array.reduce(function (r, o, i) {
        if (!i || o.begin - r[r.length - 1].end >= 3) {
            r.push(o);
        } else {
            r[r.length - 1].end = o.end;
        }
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:

const data = [
  { "begin": 870, "end": 889, "spanType": ["plan", "gt-plan"] },
  { "begin": 890, "end": 925, "spanType": ["plan", "gt-plan"] },
  { "begin": 926, "end": 938, "spanType": ["plan", "gt-plan"] },
  { "begin": 939, "end": 958, "spanType": ["plan", "gt-plan"] },
  { "begin": 7732, "end": 7790, "spanType": ["plan", "gt-plan"] },
  { "begin": 7791, "end": 7879, "spanType": ["plan", "gt-plan"] }
];

// your range, representing how close an end has to be to a begin to merge
const RANGE = 3;

// iterate through the data still available
for (let i = 0; i < data.length - 1; i++) {
    // check if we should merge the current data object with the next one
    // based on the defined range
    if (data[i].end >= data[i+1].begin - RANGE) {
        // we'll merge the current object into the next one.
        // first, we'll set the begin value of the next object to
        // the one that's being merged into it
        data[i+1].begin = data[i].begin;

        // now we push the current object's spanType entries into the next object
        Array.prototype.push.apply(data[i+1].spanType, data[i]);

        // finally we remove the current object from the list as it has been
        // fully merged
        data.splice(i, 1);

        // we removed an element, so we'll go one back in the list
        i--;
    }
}
console.log(data);


How do I require consumers a of class library to initialize static variables

Question: I am creating a DLL with lots of classes (also subclass).One of them (kind of main class) has public static property like:
public static Guid Token { get; set; }

Then I send the compiled file to another person. The person connects the DLL as a reference to his / her project and then uses the particular class.
Is there an pattern / elegant way to force user to fill static property with value before using any class?
Comment: Is this some sort of licensing or authentication system? If so, strongest advice is to not try to build it yourself.
Comment: Just write an explicit getter.  Complain by throwing an InvalidOperationException if it is still Guid.Empty, he'll wisen-up quickly.
Comment: Nope, it is not. `Guid Token` is just a example. This static can be also string, int or other object.
Comment: Not elegant but acceptable. I will still wait for another offer. ;)
Comment: Is the whole class a static class or is this only (an example of) a static property of a non-static class? Or could there be both?
Comment: there is no way to force someone to fill a property. The only close way to do so is throwing exception when value is not set.
Comment: @Fidor Whole dll is non-static. Only one property is static.
Comment: @Franck Hans Passant already suggest this.
Answer: If you have to work with static properties then you could require consumers to call an Initialize method:
public void InitializeThisGuidThatYouMustInitialize(Guid guid)
{
    // throw an exception if it's empty. Maybe throw an exception
    // if it's already been initialized.
    _theStaticGuid = guid;
}

Then, within your library, when you need that Guid, call a method that returns the Guid if it's been initialized and throws an exception if it isn't.
Something like this
internal Guid GetTheGuidINeed()
{
    if(_theStaticGuid == Guid.Empty) // or use nullable
        // Don't make them guess.
        throw new Exception("Really clear, helpful exception.");
    return _theStaticGuid;
}

This is only if you're tied to the static implementation. If there are lots of these values then you could use an immutable settings class. Require the consumer to initialize with an instance of the class, and validate the values in the class's constructor.

Another approach which eliminates the static property altogether is if you can write a class to configure your library for a DI container which is used by the consumer. Here's an example using Windsor:
public interface IMyLibrarySettings
{
    Guid TheGuidINeed { get; }
}

public class MyLibrarySettings : IMyLibrarySettings
{
    public MyLibrarySettings(Guid theGuidINeed)
    {
        TheGuidINeed = theGuidINeed;
    }

    public Guid TheGuidINeed { get; }
}

public class MyLibraryWindsorFacility : AbstractFacility
{
    private readonly IMyLibrarySettings _settings;

    public MyLibraryWindsorFacility(IMyLibrarySettings settings)
    {
        _settings = settings;
    }

    protected override void Init()
    {
        // validate the settings, make sure they didn't leave stuff out
        // throw awesome clear exception messages.
        Kernel.Register(Component.For<IMyLibrarySettings>().Instance(_settings));
        // register the other dependencies in your library.
    }
}

Now the consumer would call
var settings = new MyLibrarySettings(guidThatTheySupply);
var facility = new MyLibraryWindsorFacility(settings);
container.AddFacility(facility);

You can do the same thing with Unity, Autofac, etc.
Now you're killing lots of birds with one stone. Your library configures its dependencies in the consumer's container, and in order to do so it requires the consumer to provide the needed settings values. When individual classes need those settings, just inject that interface into their constructors, which you can do because that type is registered with the container to return the instance that the consumer provided.
I prefer this over requiring the consumer to pass that value into the constructor unless it's a class that the consumer explicitly creates. I don't want the consumer to know what all of the various classes depend on.
Comment: Secound approach. Can you show me the example how to inject that interface into sub classes constructor? It is posible get those setting from sub class level when setting was only passed on main class init?
Comment: It's a little bit more involved. The question there is really "how do I use a dependency injection container."  Once configured, the container creates objects for you. So an interface would get injected into the class just by the class requiring it in its constructor. I know how strange that sounds. In order to point you to some good information, what is the type of the application that requires this class library - like ASP.NET MVC, ASP.NET Core, WCF, Windows Forms, etc.?
Comment: To go a little further - it would change the way you write classes some. Each interface would be injected into the class that needs it. One class wouldn't usually have to pass it to another class. That's part of what makes it great, because it means that each class just depends on other interfaces, but it doesn't know what the implementations of those interfaces are or what they depend on. The hard part is the chicken-and-egg problem. It's hard to understand why it's so useful until you use it, but hard to start using it when it's not immediately clear what it's for.
Comment: I am creating DLL for win forms.
Comment: So can you please edit your example to include subclass?
Answer: You cannot force somebody to do this.
You can document that it needs to be done, but we all know how well that works.
You can throw an exception if it isn't done, preferably with a descriptive error message and a description how to solve this.
Even better would be to actually pass this Token to all methods that need it. That way it's really obvious they need to pass it. In addition, it's even threadsafe and they can pass multiple tokens at different times to different methods.
Comment: @Dominik I think what nvoigt is suggesting is to get rid of the static property on the whole and instead pass that value to any function / method that actually needs it.
Comment: Guys in comments already sugested exception and passing parameter in constructor. Ill use exeption for now. Threadsafe is not the problem in my case.
Comment: @Fildor I understand. Right now i am using "passing parameter in constructor" and this is what I want to avoid.
Answer: A simple solution could be to just add a constructor that accepts this guid as a parameter. That way, the user is required to set it through the constructor, and the class cannot be instantiated without a valid guid:
 public class YourClass
 {
      public YourClass(Guid token)
      {
           // Validate token and throw exception if not valid
           Token = token;
      }

      public static Guid Token { get; set; }
 }

With this approach, the dependencies become clear as well. Additionally, you can put a check in the set as well to make sure it cannot be set to Guid.Empty.
Comment: Not in my case with a lot of classes. Using constructor force me to pass parameter when using sub classes. The point is to set static property *once* and use it internaly in each classes.
Comment: Well then, just create this class as I described, create one instance of it, and inject that instance into your other classes.
Comment: I have seen a pattern in C-Libs: They tend to have a dedicated "Initiate" Function that needs to be called first before using any other functions ... not a perfect solution, but maybe an idea.
Comment: But then you rely on the consumer to actually call it. They can choose not to call it, and then it fails. Better to enforce it, by making sure you cannot initiate any class if the guid is not set.
Comment: @L-Four absolutely agreed. That's why I would prefer nvoigt's proposition to just pass the value to any function that actually needs it and drop having it as a static property altogether.
Comment: Right now all my classes and sub-classes are init by passing parameter in constructor. I am trying to avoid that, because it scale bad. That's why i am asking for another solution.
Comment: Not sure what you mean, but passing dependencies in constructor is considered best practice :) and I have no idea why you say it 'scales bad'? Anyway, I hope you find a solution that works for you :)


bulk-load: don't import duplicate values

Question: I need to import everyday a file containing the yesterday's snapshot of a database. To import I use the following command in the shell:
<code>./bin/hbase org.apache.hadoop.hbase.mapreduce.ImportTsv \
    '-Dimporttsv.separator=|' \
    -Dimporttsv.columns=HBASE_ROW_KEY,info:date,info:author,info:text \
    tableName \
    inputFile.tsv
</code>
The problem is that each line contains all the values and not just the updated ones, resulting to have several versions for each column but with the same value.
There is any other way to import this daily snapshot ignoring the duplicate values? Or any suggestion to workaround this?
Thank you!
Answer: I guess that if you really want to ignore existing values you'd need to write your own map/reduce instead of using the import program.
However, what's the problem with multiple versions? First off you can set the number of version hbase keeps (when you define a column family) secondly when you read you can read just the latest version and lastly, if you are worried about storage you can set up hbase to use compression
Comment: My concern is not about disk space. I'm working in a project that needs to know what is the state of the "world" at some point in time, eg: "for this cell, what is the value a month ago?". For this I need the cell be versioned. But if I update my cell every single day without being really updated (because is the same value for example), I'm wasting versions :/ Do you think is not a problem, in terms of Hbase performance, to set the maximum versions of a column family to be, lets say, 365 (enough to hold a year history)? Thank you for your help!
Comment: I'm getting closer to that answer, writing my own map/reduce function to import my data.
About the multiple versions it is not a real problem, as you said, I can save every version just expanding my disk capacity. My question was how to avoid saving the save value twice, having this way several versions of the same value on the same cell.
Comment: What I said that is that if you  use compression you don't have to worry much about capacity an if you set version to be 1 for the column family hbase will remove duplicates upon compactization.
Comment: for hbase every "cell" (key+column family+ qualifier + timestamp + version + value) is a new row and as far as I can tell it shouldn't matter. In any event you can always add the update time (max - update time actuallu) as part of the key and don't use hbase versions. a get by prefix would get you the latest, a full key would get you a specific version and you won't be limited by 365 versions (or you can set time to live of 365 days and hbase will clear old value)


display a taxonomy terms as autocomplete using form API

Question: I am writing my own module. I want to display a taxonomy terms as a autocomplete field in my form using form API. How can I do that?
I am using Drupal7.
Comment: Have you read and tried to use [Form API reference section about autocomplete](http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#autocomplete_path)? What exactly is a problem?
Answer: Hope this will help you,
In Drupal 7, it can be done in 2 ways

By using existing taxonomy field(like TAG field), for this example I'm using field_tag

$node=node_load($nid);
$tags = array();
foreach ($node->field_tags['und'] as $item) {
  $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
}
$form['tags'] = array(
  '#type' => 'textfield',
  '#default_value' => taxonomy_implode_tags($tags),
  '#title' => 'Add Tags',
  '#autocomplete_path' => 'taxonomy/autocomplete/field_tags',
  '#maxlength' => 1024,
  '#element_validate' => array('taxonomy_autocomplete_validate'),page
);

If you dont want to use any field then it can also be using custom autocomplete

//custom field
$form['taxonomy_term_field'] = array(
  '#type' => 'textfield',
  '#autocomplete_path' => 'get_TAXONOMY_NAME/autocomplete',
);
//menu callback for autocomplete
function HOOK_menu() {
  $items = array();
  ....
  $items['get_TAXONOMY_NAME/autocomplete/%'] = array(
    'page callback' => 'TAXONOMY_NAME_autocomplete_callback',
    'page arguments' => array(2),
    'type' => MENU_CALLBACK,
  );
  return $items;
}
//autocomplete callback function
function TAXONOMY_NAME_autocomplete_callback($str = 0) {
  $matches = array();
  //vocabulary id
  $vid = 2;
  $result = db_select('taxonomy_term_data', 't')
    -> fields('t', array('tid', 'name'))
    -> condition('vid', $vid, '=')
    -> condition('name', $str.'%%', 'LIKE')
    -> range(0, 10)
    -> execute();
  foreach ($result as $term) {
    $matches[$term -> tid] = check_plain($term -> name);
  }
  drupal_json_output($matches);
}

References : Existing taxonomy field and Custom autocomplete field
Answer: $form['nameField'] = array(
    '#type' => 'textfield',
    '#autocomplete_path'=>'example/autocomplete'
);

//HOOK_MENU
 function MODULENAME_menu() {
    $items['example/autocomplete'] = array(
    'title' => '',
    'description' => '',
    'page callback' => 'example_autocomplete',
    'access callback' => TRUE,
);

 function example_autocomplete() {
    $termes = taxonomy_vocabulary_get_names();
    $vocabulary = taxonomy_get_tree($termes['MACHINENAME']->vid);
    $list = array();
    foreach ($vocabulary as $taxonomy){
           $list[$taxonomy->tid] = $taxonomy->name;
    }
    print drupal_json_encode($list);
    exit();
}

Comment: It is not working in my end.
Answer: 1) Custom Module name : common_operations
2)

/**
 *Implements hook_menu()
 */
function common_operations_menu() {
  $items['autocomplete/taxonomy'] = array(
    'title' => 'Menu Title',
    'description' => 'Menu description',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('common_operations_autocomplete_taxonomy_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  $items['path/autocomplete'] = array(
    'title' => 'Custom Autocomplete',
    'page callback' => 'common_operations_custom_autocomplete',
    'access arguments' => array('access taxonomy autocomplete'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
  *Defines autocomplete_taxonomy_form()
  *@param $form
  *@param &$form_state
  *@return $form
  */
function common_operations_autocomplete_taxonomy_form($form, &$form_state) {
  $form['taxonomy'] = array(
    '#type' => 'textfield',
    '#title' => t('Campus'),
    '#autocomplete_path' => 'path/autocomplete',
    '#maxlength' => 60,
    '#size' => 15,
    '#required' => 0,
  );

  //--------------------Others------------------

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Lets Go!'),
  );

  return $form;
}

/**
 *Page callback for menu 'path/autocomplete'
 */
function common_operations_custom_autocomplete($string) {
  $matches = array();
  $result = db_select('taxonomy_term_data', 'ttd');
  $result->fields('ttd', array('tid', 'name'));
  $result->condition('vid', array(VID1, VID2,...), 'IN');
  $result->condition(db_or()->condition('ttd.name', '%' . db_like($string) . '%', 'LIKE'));
  $query = $result->execute();

  foreach ($query as $row) {
    $matches[$row->tid] = $row->name;
  }

  // Return the result to the form in json
  drupal_json_output($matches);
}

/**
 * Implements hook_permission().
 */
function common_operations_permission() {
  return array(
    'access taxonomy autocomplete' => array(
      'title' => t('Access Custom Taxonomy Autocomplete'),
        'description' => t('Access permission of Custom Taxonomy Autocomplete.'),
    ),
  );
}

3) Replace VID1, VID2, ... with the Vocabulary ID's
4) Clear drupal cache
5) Set Permission
6) Access path 'autocomplete/taxonomy' & try
Hope it helps


Do I have a memory leak? (nodejs)

Question: I have an app written in nodejs that runs on a server. I have noticed (by manually checking the server load from time to time) that my app is starting with around 50MB total allocated RAM (from which aprox. 10 MB are HEAP) but after 6 days it is using a total of 100 MB (although the HEAP remains more or less constant).
First I've tried to debug it with Chrome but the debugger only monitors the HEAP portion of the allocated RAM (and I have not noticed any problems there). On the other hand I watched the debugger for a limited amount of time.
So, to further monitor the RAM usage, I've written the following code inside my app
var maxRecordedRam = 0;
ramCheck = setInterval(() => {
    let ram = process.memoryUsage();
    let ramRss = Math.round(ram.rss / 1024 / 1024 * 100) / 100;
    let ramHeap = Math.round(ram.heapUsed / 1024 / 1024 * 100) / 100;
    if ( maxRecordedRam < ramRss ) {
        // memory load is higher then the last recorded value
        maxRecordedRam = ramRss;
        modulUtile.log(
                "RAM: " + ramRss + " MB (Heap: " + ramHeap + " MB)",
                "INFO"
        );
    }
}, 30000);

Please note that modulUtile.log() method is a kind of wrapper for console.log, that logs messages to a file.
After 6 days I have the following output in the log file (the file is larger, I've only picked some values):
[2019-08-23 07:10:19]    RAM: 44.27 MB (Heap: 10.03 MB)
.....
[2019-08-23 07:25:19]    RAM: 56.81 MB (Heap: 11.57 MB)
.....
[2019-08-23 09:13:19]    RAM: 65.85 MB (Heap: 15.72 MB)
.....
[2019-08-23 14:47:49]    RAM: 90.97 MB (Heap: 19.14 MB)
.....
[2019-08-25 09:49:52]    RAM: 93.9 MB (Heap: 10.66 MB)
.....
[2019-08-29 12:39:30]    RAM: 97.02 MB (Heap: 17.23 MB)

The HEAP allocation is up to 20MB during work hours and around 10 mb during the night and in the morning (when the app is kind of idle). But although the HEAP is going up and down, the total RAM is only climbing.
My question: Is it possible to have a memory leak outside the HEAP area (because as far as I know, only HEAP area is used for storing variables)?
Answer: It's not possible for heap memory to "leak" into stack memory.
It might help to also print out the heapTotal and external values of memoryUsage().
If the heapTotal accounts for most of the spare memory, then the JVM is making more and more heap available to your program without relinquishing it back to the system.  This is not a memory leak in your code, and the memory may be returned to the system when the unused memory gets large enough to bother with.
If the external portion of the memory is growing, then some unmanaged .dll is consuming more and more memory, possibly due to a memory leak within it, or possibly because of thread mismanagement.  Debugging external libraries, especially those you have no control over, can be a pain.
If neither of those is growing, then your own code's stack is growing.  This could be caused by two things:  reiteration and thread mismanagement.
Are the number of threads used by your code increasing?  Then threads are hanging at some point, and new ones are spawning.  Each thread contains a small amount of stack that isn't returned until the thread properly exits its lifetime.
Reiteration should not be the culprit unless it's obviously possible, e.g., some functions can loop back into the main work flow through function calls instead of returning.
It's also possible, though unlikely, that the JVM is not properly tracking used heap.  The way you'd detect such a bug in the JVM is to run the program on a different JVM and see whether the numbers are roughly the same, or whether the new JVM starts reporting much larger heapUsed values.
Without further information, my suspicion would first fall on "the JVM is holding unused memory to avoid releasing and reacquiring it," followed by "an external .dll has a memory leak."
Answer: So basically a heap memory is allocated as a part of RAM which is more of a hardware memory component, whereas a heap is a logical component allocated or created by a specific service or a set of services, in this case, your NodeJS service.
Now in your case, it can very well be that pos the garbage collection the heap memory is being cleared but since your RAM has already allocated space for this memory in RAM and given the fact that usage is not much viz. 100mb out of 2Gb or so is less. So the system does not think that it should swap this out which the hard disk or any other memory storage available.
Also as stated in this article, the JS V8 engine allocatees memory and separates it into A Stack and Heap memory allocation. The article will explain what different type of operations will cause either the stack or the heap memory to increase.
Given the above, I would encourage you to stress test your code to make it reach peak memory utilization where your heap and RAM usage is more than 80% of total RAM available to your device or VM, etc.

There is an inherent trade-off between garbage collection throughput,
  latency, and memory consumption. For example, garbage collection
  latency (which causes user-visible jank) can be reduced by using more
  memory to avoid frequent garbage collection invocations. For
  low-memory mobile devices, i.e. devices with under 512 MB of RAM,
  prioritizing latency and throughput over memory consumption may result
  in out-of-memory crashes and suspended tabs on Android.

The above lines are from this brilliant article, which say that the memory allocation and deallocation can be controlled partially by prioritizing the GC or code execution latency. Which means you can prioritize when you want to destroy arrays or objects or set them null or how fast you want your requests to be processed and the concurrency allowed or in short amount of data which should be stored in memory and process concurrently thus reducing latency.
So all in all, although heap memory leaks may cause the rise in RAM usage, it won't be at the expense of each other as heap is part of RAM. So it may also be possible that RAM usage may be increased either because of stack memory or some other related service which may or may not be related to your NodeJS service. But all this will be evident once you can perform and benchmark multiple stress tests and analyze the results for yourself. As the behavior is very specific to your application code and how your code is structured.
About visualization, While you stress test your NodeJS application you can use this tool to analyze your code execution and the article mentioned above also gives a brief idea about what to watch out for.
Comment: Hi and thanks for the answer. I've read the articles but I still don't know if it is possible to have a memory leak outside of the heap (the original question). All the debugging tools I've found are only for heap portion of the ram (including the one you suggested) and I don't have a problem there, as the heap is fluctuating with the server load.
Comment: I think that's what the article and answer(last couple of para) is trying to explain, a memory leak is conceptually not actually a leak from a virtual memory space to another memory space like a stack or in your case RAM. So your RAM usage suppose to say is 100Mb is including the heap memory of 20Mb and the other 80Mb is your NodeJS stack memory and other services running on your system. Thus the possible memory rise in RAM may be caused by other services. to check that you use `htop`. refer - https://askubuntu.com/a/138685
---
OR
---
In short - NO memory leaks don't work like that.
Comment: I know that a memory leak is not a transfer from one virtual space to another. A leak happens when the software keeps on creating elements without unreferencing them (and not allowing GC to free the memory). And there is my problem because, as far as I know, these elements are only created inside the HEAP area. About the `htop`, that is how I've first notice the problem (as I stated in the question). The memory can not be used by other services on the system, as `process.memoryUsage()` only reports the usage for the actual running process.
Comment: Doesn't that mean it is being used by the Stack memory .. as your heap is constant and in some way or the other your stack memory is raising...
Comment: That is right somehow. If the HEAP is not the problem then the stack zone can have problems. But, is that even possible? Because all of the examples I've found are only referencing the heap area. Even Chrome's memory debugging tool is only monitoring the heap portion. More or less, my question is something like: Is is possible to have a memory leak OUTSIDE the heap area? Or is it just that the operating system is not emptying the memory (somehow...) because there is no need for additional ram for other processes.
Comment: maybe you can try restarting the NodeJs process and benchmark each run with different load sizes, to see if this is happening again and that if similar behavior is observed in each run. This will at least give clarity to fact that there some side effect of the way NodeJS service is running which is causing the non-heap memory to rise.
Also, it can be the later as the numbers mentioned in ur case are very low for any RAM cleanup or GC to take place. testing it on higher stress will provide a more results ideally at par ~80 - 90 % available memory usage...
Comment: Also can u try and log out the `ram.external` usage
Comment: And the RSS - resident code segment is inclusive of stack and code and heap.. and it makes sense either the stack or code is casuing this .. https://nodejs.org/api/process.html#process_process_memoryusage

Do I have a memory leak? (nodejs)


How to solve error installing kivymd in Pydroid in NOX?

Question: I was testing my Kivymd app with Pydroid 3, and then I thought it will be better if I can use Pydroid 3 on PC. So I installed the Nox emulator and installed Pydroid 3 app, and I tried to install kivymd with pip.
Then it said Pydroid repository plugin is missing, so I installed this plugin too.
And then it gives me so many errors, and the start of error, it says
ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'Pillow-7.12.dist-info'`, and end of error, it says `FileNotFoundError: [Errno 2] No such file or directory: 'kivymd'

It's the same when I uncheck [Use prebuilt libraries repository] or reinstall Pydroid and plugin. How to solve this error?
I'm using Windows 10.
Answer: Use Terminal instead of Pip menu. Open Pydroid, then open the side menu, tap on Terminal, then type "pip install kivymd==0.104.1".
Comment: solved my problem .
Answer: you can simply use kivymd on windows along with kivy, there is no need for an emulator, but it you want to use an emulator, I recommend BlueStacks


Melodic minor: Does 6 get raised when descending and then ascending immediately?

Question: I found this embellished suspension in C minor:

The notes between S and R embellish the dissonant S. You can see that the leading tone 7 is raised in both descending and ascending directions, as well as 6 is raised. This is a descent followed by an ascent. It looks weird that the leading tone is raised so early – in the descent – already, carrying the accidental over to the ascending leading tone because the measure didn't end yet. I tried it out myself.
A test in A minor:
3 2 1 7 6 7 1 – Sounds good, but the pull from 7 to 1 is missing
3 2 1 7 6 #7 1 – With raised LT the Pull to tonic is better, but makes 6 sound totally out of place
3 2 1 7 #6 #7 1 – With raised 6 the augmented second is fixed, but disturbs the descent
3 2 1 #7 #6 #7 1 – Sound fixed, yet unfamiliar, with LT also raised when descending
Two questions are now:

In the situation of a descent followed by an ascent, what does one raise?
Is this choice of raising somehow made by chromaticism in order to intensify the dissonance of the suspension?
Answer: Although the pitches initially move downward, this isn't a descending scale in the sense of a melodic passage. That is, the idea behind melodic minor of creating smooth passages applies to melodies but not to ornaments, which are considered "extra" and not part of the melody itself.
Instead, there are two ways to consider this:

It's the leading tone that is being embellished, so it's necessary to use the raised 6. That is, the actual melody is C-B-C, with the ornament only applying within the context of B.
The chord being embellished — the V chord — can be thought of as a (very!) brief modulation, so the embellishment would be in that key; i.e. G major.
Comment: Great, clear now. But what if this situation occurs in a reasonable melody, not being an ornamentation?
Comment: @Mozartovic That's a good question. My initial thought is that the answer would change depending on the specific context.
Answer:
It looks weird that the leading tone is raised so early – in the descent...

There is nothing weird about it. This passage is totally normal.
The mistake you are making is common, thinking the sixth and seventh scale degrees in minor are lowered for all descending lines and raised for all ascending lines, and that melodic lines will somehow always use those degrees in discrete ascending or descending fashion.
More that the direction of the line, the prevailing harmony is what determines the raised or lowered tones. This passage the B and A are part of a dominant chord. The B natural is the raised form of the seventh degree to form a proper dominant with a leading tone. The A is raised so the neighbor motion with B is a whole step rather than an augmented second.
The largeness of an augmented second is considered awkward in some contexts, like a neighbor motion, in other contexts it isn't a problem. You can find plenty a descending harmonic minor scales with the augmented second, over dominant chords. Suffice to say it isn't a matter of simply direction, it's a matter of the harmony and the overall line.
The same treatment of the sixth scale degree in minor can be seen with a neighbor motion from the fifth to the sixth. While the neighbor motion from the leading tone uses the raised sixth, the neighbor motion from the fifth uses the lowered sixth. The former is to avoid an augmented second, the later to preserve the minor sixth quality in the minor mode.
This kind of neighbor motion is the perfect example to illustrate why direction is not the primary issue regarding the sixth and seventh degrees in minor. Such neighbor motions are simultaneously both ascending and descending. In the example passage the A occurs only once! There is no way to apply the mistaken ascending/descending rule. How could you apply a lowered sixth for the descent and a raised sixth for the ascent when there is only one sixth played? You can't, and the raised/lowered treatment is decided by factors other than direction.
Answer: This is just the clausula i-ii-V-i. The lead tone B is advanced already above ii and  A (6) is just a changing tone, and not a descending scale (agreeing with Aaron.)
Comment: I don't think you mean "clausula" (a newly composed descant over an existing organum). Maybe "cadence" or "clause"?
Comment: clausula
noun
1. The conclusion of a passage; cadence.
2. The close or end of a historical period; clause.

Source: https://www.wordnik.com/words/clausula
Comment: @Mozartovic That helps. I see why I was confused — I wasn't familiar with its Latin meaning. But the link you included gives enough examples to clarify its use in Latin phrases.


How to set a condition for the background color of a CheckBox in Mouse Over state?

Question: I needed a CheckBox that acts as follows

Unchecked - The default style is applies
Checked - The background is blue and the check mark glyph is white

I created a template like this:
<Application.Resources>

    <SolidColorBrush x:Key="OptionMark.Static.Glyph" Color="#FFF"/>
    <SolidColorBrush x:Key="OptionMark.MouseOver.Background" Color="#FF3C77DD"/>
    <SolidColorBrush x:Key="OptionMark.MouseOver.Border" Color="#FF3C77DD"/>
    <SolidColorBrush x:Key="OptionMark.MouseOver.Glyph" Color="#FFF"/>
    <SolidColorBrush x:Key="OptionMark.Pressed.Background" Color="#FF3C77DD"/>
    <SolidColorBrush x:Key="OptionMark.Pressed.Border" Color="#FF3C77DD"/>
    <SolidColorBrush x:Key="OptionMark.Pressed.Glyph" Color="#FFF"/>
    <SolidColorBrush x:Key="OptionMark.Disabled.Background" Color="#FF3C77DD"/>
    <SolidColorBrush x:Key="OptionMark.Disabled.Border" Color="#FF3C77DD"/>
    <SolidColorBrush x:Key="OptionMark.Disabled.Glyph" Color="#FFF"/>
    <ControlTemplate x:Key="checkBoxTemplate" TargetType="{x:Type CheckBox}">
        <Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Border x:Name="checkBoxBorder" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                <Grid x:Name="markGrid">
                    <Path x:Name="optionMark" Data="F1 M 9.97498,1.22334L 4.6983,9.09834L 4.52164,9.09834L 0,5.19331L 1.27664,3.52165L 4.255,6.08833L 8.33331,1.52588e-005L 9.97498,1.22334 Z " Fill="{StaticResource OptionMark.Static.Glyph}" Margin="1" Opacity="0" Stretch="None"/>
                    <Rectangle x:Name="indeterminateMark" Fill="{StaticResource OptionMark.Static.Glyph}" Margin="2" Opacity="0"/>
                </Grid>
            </Border>
            <ContentPresenter x:Name="contentPresenter" Grid.Column="1" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="HasContent" Value="true">
                <Setter Property="FocusVisualStyle" Value="{StaticResource OptionMarkFocusVisual}"/>
                <Setter Property="Padding" Value="4,-1,0,0"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.MouseOver.Background}"/>
                <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.MouseOver.Border}"/>
                <Setter Property="Fill" TargetName="optionMark" Value="{StaticResource OptionMark.MouseOver.Glyph}"/>
                <Setter Property="Fill" TargetName="indeterminateMark" Value="{StaticResource OptionMark.MouseOver.Glyph}"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Background" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.Disabled.Background}"/>
                <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.Disabled.Border}"/>
                <Setter Property="Fill" TargetName="optionMark" Value="{StaticResource OptionMark.Disabled.Glyph}"/>
                <Setter Property="Fill" TargetName="indeterminateMark" Value="{StaticResource OptionMark.Disabled.Glyph}"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="true">
                <Setter Property="Background" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.Pressed.Background}"/>
                <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.Pressed.Border}"/>
                <Setter Property="Fill" TargetName="optionMark" Value="{StaticResource OptionMark.Pressed.Glyph}"/>
                <Setter Property="Fill" TargetName="indeterminateMark" Value="{StaticResource OptionMark.Pressed.Glyph}"/>
            </Trigger>
            <Trigger Property="IsChecked" Value="true">
                <Setter Property="Opacity" TargetName="optionMark" Value="1"/>
                <Setter Property="Opacity" TargetName="indeterminateMark" Value="0"/>
                <Setter Property="Background" Value="#FF3C77DD"/>
                <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="#FF3C77DD"/>
            </Trigger>
            <Trigger Property="IsChecked" Value="{x:Null}">
                <Setter Property="Opacity" TargetName="optionMark" Value="0"/>
                <Setter Property="Opacity" TargetName="indeterminateMark" Value="1"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Application.Resources>

The problem is when the mouse is over the CheckBox and it's unchecked, the background changes to blue, and if I change:
<SolidColorBrush x:Key="OptionMark.MouseOver.Background" Color="#FF3C77DD"/>

to white, when it's checked and the mouse is over the CheckBox the background changes to white. How can I change the template so that the background is blue, if the CheckBox is checked and the mouse is over it and set the background to white, if it is unchecked and the mouse is over it?
Comment: Use a MltiTrigger that triggers on IsMousOver and IsChecked
Answer: As @BionicCode already mentioned in the comments, you should use a MultiTrigger, which can use a set of conditions. The setters are only applied if all of the confitions are met.
<MultiTrigger>
   <MultiTrigger.Conditions>
      <Condition Property="IsMouseOver" Value="True"/>
      <Condition Property="IsChecked" Value="True"/>
   </MultiTrigger.Conditions>
   <Setter Property="Background" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.MouseOver.Background}"/>
    <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.MouseOver.Border}"/>
    <Setter Property="Fill" TargetName="optionMark" Value="{StaticResource OptionMark.MouseOver.Glyph}"/>
    <Setter Property="Fill" TargetName="indeterminateMark" Value="{StaticResource OptionMark.MouseOver.Glyph}"/>
</MultiTrigger>


How to configure multisites for the console?

Question: Most of console commands are not listed when using
drupal list

Drupal Console version 0.11.3
It issues a warning like so:
[WARNING] In order to list all of the available commands you should install
       drupal first.

Drupal is installed , as multisites.
   drupal multi:debug

Tells me this error:
 [ERROR] There aren't multisites configured

How can I configure multisites for the console ?
     https://hechoendrupal.gitbooks.io/drupal-console/content/hu/using/how-to-use-drupal-console-in-a-multisite-installation.html

EDIT:
a mapping must be done in sites/sites.php for
drupal multisite:debug
to display the list
sites.php
 /**
 * Multi-site directory aliasing:
 *
 * Edit the lines below to define directory aliases. Remove the leading hash
 * signs to enable.
 */
$sites = array (
    'my_domain_name.fr'=>'my_folder_name_in_sites?'
);

What is the exact format of this array values i dont know
i dont have drupal multi:debug error nomore mais drupal list is still restricted
Answer: a mapping must be done in sites/sites.php for
   drupal multisite:debug

to display the list
sites.php
 /**
 * Multi-site directory aliasing:
 *
 * Edit the lines below to define directory aliases. Remove the leading hash
 * signs to enable.
 */
$sites = array (
    'my_domain_name.fr'=>'my_folder_name_in_sites?'
);

i am not sure for it to be correctly configured
my_folder_name_in_sites?

must be
my_folder_name

or
sites/my_folder_name

Comment: I think you should mark your own answer as accepted, no?
Comment: well no, coz although drupal multisite:debug do not display error message anymore , i am not sure if what i enter in the site.php is in the correct format , and the drupal list command still display restricted list
Comment: ok, fair enough. But in that case I'd integrate your prior comment in your actual answer also.


Proving the Bessel function formula by expanding its generator function.

Question: I am trying to show that the Bessel functions $J_n(x)$  have the form
$$J_n(x)=\sum_{k=0}^{\infty}\frac{(-1)^k}{k!(n+k)!}\bigg( \frac{x}{2} \bigg)^{n+2k},$$ from its generator function
$$G(x,t)=\sum_{n=-\infty}^{\infty}t^nJ_n(x)=e^{\frac{x}{2}(t-\frac{1}{t})}.$$
Expanding G(x,t) in a power series with respect to its exponent we get
$$G(x,t)=\sum_{n=0}^{\infty}\frac{1}{m!} \bigg( t - \frac{1}{t}\bigg)^m\bigg( \frac{x}{2} \bigg)^{m}.$$
By the binomial expansion, we have
$$G(x,t)=\sum_{m=0}^{\infty}\sum_{k=0}^{m}\frac{(-1)^k}{k!(m-k)!} t^{m-2k}\bigg( \frac{x}{2} \bigg)^{m}.$$
By setting $n=m-2k$, the $a_{mk}$ factor of the sum becomes
$$\frac{(-1)^k}{k!(n+k)!} t^{n}\bigg( \frac{x}{2} \bigg)^{n+2k}.$$
So I seem to be in the correct way, yet I don't know how to change the $$\sum_{m=0}^{\infty}\sum_{k=0}^{m}$$ expressed in terms of $k$ and $n$. Any ideas?
Comment: If it makes sense to intervert the summations (Fubini Theorem), then you can change it to $$\sum_{k=0}^{\infty}\sum_{m=k}^{\infty}$$
Comment: alternativly you could do a contour integral $\int_C dt t^m G(x,t)$ where $C$ is a contour encirceling the origin
Answer: You want to identity the coefficients of $t^n$. Your formula includes
$t^{m-2k}$. So you want to set $m-2k=n$. Eliminating $m$ your formula
is the sum of
$$\frac{(-1)^k}{k!(n+k)!}t^n\left(\frac x2\right)^{n+2k}$$
over $n$ and $k$ with $0\le k\le n+2k$, that is $k\ge\max(0,-n)$.
So for $n\ge0$ you get
$$J_n(x)=\sum_{k=0}^\infty\frac{(-1)^k}{k!(n+k)!}\left(\frac x2\right)^{n+2k}.$$


ajuda com o codigo abaixo

Question: Boa tarde, pessoal!
Estou tentando fazer um app e não consigo fazer a programação para que não passe para o campo seguinte com o campo vazio.
Do jeito que o código esta se deixar em branco e apertar em calcular o sistema fecha.
Acredito que por conta da conversão, pois, se deixo de maneira normal o teclado por ser português não aceita a virgula, então tive que fazer a conversão.
Se algum puder me ajudar, agradeço!
Abaixo o código, completo.
import UIKit

class MediasViewController: UIViewController {

    @IBOutlet weak var Litros: UITextField!
    @IBOutlet weak var MKhini: UITextField!
    @IBOutlet weak var Mkhfin: UITextField!
    @IBOutlet weak var MresultadoKh: UITextField!

    @IBAction func btnMedia(_ sender: UIButton) {

        let LitrosGeral = Double(self.Litros.text!)!
        let formatter = NumberFormatter()

        func convertToDouble (_ string: String) -> Double {
            formatter.numberStyle = .none
            return formatter.number(from: string)!.doubleValue
        }

        formatter.usesGroupingSeparator = true
        let Mkhini = convertToDouble(MKhini.text!)

        let Mkhfina = convertToDouble(Mkhfin.text!)
        var resultadoMediakh = (Mkhini - Mkhfina) * 17.8 * LitrosGeral

        if resultadoMediakh / 79.8 < 0 {
            (resultadoMediakh = 0)
        } else {
            resultadoMediakh = (resultadoMediakh / 79.8) / 3
        }
        MresultadoKh.text = String (format: "%.1f", resultadoMediakh)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let alerta = UIAlertController (title: "Alerta", message: "teste", preferredStyle: .alert)

        let confirmar = UIAlertAction(title: "OK", style: .default, handler: nil)
        alerta.addAction(confirmar)

        present(alerta, animated: true, completion: nil)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
}

Answer: Sim o erro ocorre na conversão pois não consegue converter um campo vazio para um numero e voce esta forçando isso utilizando o !, o que você pode fazer é trocar
   let LitrosGeral = Double(self.Litros.text!)!

por
   let LitrosGeral = Double(self.Litros.text!) ?? 0

Assim toda vez que o campo estiver vazio ele vai considerar como 0
Agora para que não passe para o campo seguinte com o campo atual vazio você deverá implementar a função
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {...}

do protocolo
UITextFieldDelegate,
esta função irá ser chamada cada vez que for digitado um caractere em um textfield que esteja setado o delegate com sua ViewController, por tanto, não esqueça de setar desta maneira:
   Litros.delegate = self

A seguir o código com a validação de cada textfield para habilitar o próximo:
import UIKit

class MediasViewController: UIViewController {

    @IBOutlet weak var Litros: UITextField!
    @IBOutlet weak var MKhini: UITextField!
    @IBOutlet weak var Mkhfin: UITextField!
    @IBOutlet weak var MresultadoKh: UITextField!
    @IBOutlet weak var calculateButton: UIButton!

    @IBAction func btnMedia(_ sender: UIButton) {

        let LitrosGeral = Double(self.Litros.text!) ?? 0
        let Mkhini = Double(MKhini.text!) ?? 0
        let Mkhfina = Double(Mkhfin.text!) ?? 0

        var resultadoMediakh = (Mkhini - Mkhfina) * 17.8 * LitrosGeral

        if resultadoMediakh / 79.8 < 0 {
            (resultadoMediakh = 0)
        } else {
            resultadoMediakh = (resultadoMediakh / 79.8) / 3
        }
        MresultadoKh.text = String (format: "%.1f", resultadoMediakh)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        Litros.delegate = self
        MKhini.delegate = self
        Mkhfin.delegate = self
        MresultadoKh.delegate = self

        MKhini.isUserInteractionEnabled = false
        Mkhfin.isUserInteractionEnabled = false
        MresultadoKh.isUserInteractionEnabled = false
        calculateButton.isUserInteractionEnabled = false
    }

    // função para liberar o próximo TextField conforme ir completando
    func checkTextField() {
        let litrosCompleted = Litros.text!.count >= 1
        let iniCompleted = MKhini.text!.count >= 1
        let finCompleted = Mkhfin.text!.count >= 1

        if (litrosCompleted && !iniCompleted && !finCompleted) {
            MKhini.isUserInteractionEnabled = true
        } else if (litrosCompleted && iniCompleted && !finCompleted) {
            Mkhfin.isUserInteractionEnabled = true
        } else if (litrosCompleted && iniCompleted && finCompleted) {
            calculateButton.isUserInteractionEnabled = true
        }
    }
}

extension MediasViewController: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        checkTextField()
        return true
    }
}

Lembrando que este é apenas um exemplo simples de como pode ser feito e que este código não trata caso o usuário volte ao primeiro campo e o apague, porém caso o usuário faça isso o app ira rodar normalmente porque todo campo vazio esta sendo tratado com o valor 0, Um próximo passo seria voce tratar cada vez que seja apagado todo campo o aplicativo o completar com 0.
Certifique-se também que o Content Type do teclado seja do tipo numérico para que não se possa digitar letras no TextField

ajuda com o codigo abaixo


Applying a function element-wise to multiple numpy arrays

Question: Say I have two numpy arrays of the same dimensions, e.g.:
a = np.ones((4,))
b = np.linspace(0,4,4)

and a function that is supposed to operate on elements of those arrays:
def my_func (x,y):
   # do something, e.g.
   z = x+y
   return z

How can I apply this function to the elements of a and b in an element-wise fashion and get the result back?
Comment: You need to provide more data about your function. As is the answer is trivially `a+b`.
Comment: Or trivially `my_func(a,b)`.
Answer: It depends, really. For the given function; how about 'a+b', for instance? Presumably you have something more complex in mind though.
The most general solution is np.vectorize; but its also the slowest. Depending on what you want to do, more clever solutions may exist though. Take a look at numexp for example.


update in django with image field

Question: I am trying to use django orm to update image field only i.e user will later add image and will be updated in primary table
i tried
MerchantProfile.objects.filter(id='%s'%(cd['id'])).update(photo='%s'%(cd['photo']))

but it will update the caption only and image in not saved in folder
Then i tried following
p1=MerchantProfile()
p1.id=cd['id']
if cd['photo']:
       p1.photo=cd['photo']
else:
       p1.photo=cd['uploaded_photo']
p1.save()

It will save the image but will show error like payment_card cannot be null
My models is as below
class Merchantprofile:
   user = models.OneToOneField(UserProfile, related_name="merchant_profile")
   payment_card = models.OneToOneField(PaymentCard, related_name="merchant_profile")
   current_state = models.IntegerField('State', choices=STATE_CHOICES)
   name = models.CharField('Merchant Name', max_length=64)
   photo=models.ImageField(upload_to='logo',blank=True)

form
class LogoForm(forms.Form):
     id = forms.CharField(widget=forms.HiddenInput,required=False)
     photo = forms.ImageField(
        required=False,
        label='Upload photo',
        initial=True,
        help_text='max. 4 megabytes'
    )

so what is the best way to update photo field only with uploading photo on relevant folder, as i cannot update all field everytime
Answer: Try this:
p1 = MerchantProfile.objects.filter(id=cd['id'])
if cd['photo']:
   p1.photo=cd['photo']
else:
   p1.photo=cd['uploaded_photo']
p1.save()

Comment: @JoelleTillman I hope cd['photo'] (and other) is file wrapper or similar object.
Comment: it says 'QuerySet' object has no attribute 'save'
Comment: @JoelleTillman _cd['photo']_ doesn't seem to be django File type object. Isn't it taken from _request.FILES? You can only save file object in _imagefield_.

update in django with image field


Fatal Error: PDOException: SQLSTATE[42000] [1049] Unknown database

Question: I have a development site (site1) and created a second (site2) using drush, copied over all modules/themes and continued development. Both were working and I continued module updates in both.  When I returned to site1, I found an error message as follows:

PDOException: SQLSTATE[42000] [1049] Unknown database 'mydevelopmentDB' in lock_may_be_available() (line 167 of mywebsite/includes/lock.inc).

There features and nodes I would like to use in site2 but I cannot access it. So I created a new site (site3) using drush, copied the contributed modules & themes from site2, and imported the database from site1 into site3, only to get the same error message as from site1.
As I interpret this, situation, the core and contributed module files in site3 should be good, the core is a fresh copy and contributed modules are taken from site2, which is working fine. The settings.php files are identical except for the database, username and password settings match the individual database name. The only difference is the database itself.  Can anyone help identify the table(s) that might have a setting that might cause this problem?
Answer: This isn't a problem with tables, it's a problem with the name of the database.
Drupal is successfully connecting to your database server, but it isn't finding a database with the name mydevelopmentDB (which must be the name of the database specified in settings.php).
To isolate the problem:

Open up PHPMyAdmin for the server in question and run this query:
SELECT * FROM mydevelopmentDB.users

If you get a similar error then you know that you've definitely got the wrong database name.
Make sure that the name of the database on the server matches the case of the database name in settings.php. Depending on your MySQL settings the server may or may not be enforcing case sensitivity on database/table names.


Lua xor function code

Question: Does this code work as a xor function in Lua?
function xor (a,b)
if a ~= b then return true else return false end
end

i = false
j = false
if xor(not i, not j) then
  print("one exclusive")
else
  print("both or none")
end

Comment: `function xor(a,b) return not a ~= not b end`
Comment: i dunno does it?
Answer: Yes, your code works.
If a and b contain boolean values, then a XOR b is the same as not(a == b), which of course is the same as
 a ~= b.

Lua xor function code


How add a css id selector to a views row

Question: I need to add a css id selector to a views row. Of course this is straight forward with a class selector such as .my-class but how do I insert something like #my-class at the individual row level?
I realize that this probably invalid CSS; however, the ID appears to be needed to trigger some JavaScript.
Some background: I want to use a component from the Bourbon Refills CSS  framework for a 'fade in' effect and apply this to all the rows in a views. See http://refills.bourbon.io/components/#fade-in.
In order the make this work I need to add a CSS ID #js-fadeInElement to each row.
Note
After a bit more digging, it seems I may need a custom row.tpl file in my theme. Is this correct?
Answer: Solution 1:
No need to add ID for each row, you can achieve it by each row CSS class it self. Instead of document.getElementById("js-fadeInElement"); you can use document.getElementsByClassName("js-fadeInElement");
Solution 2:

You can use Views theme templates to modify the HTML output and add your id attribute.
Under the Advanced section, you will find Theme: information.

This link will show you possible file name for templates and a sample
  content of that file.

You probably want to copy and modify the "Style output". You can put your custom template in your own theme directory. You probably want a name like views-view-list--your-view--page.tpl.php

Please follow this link to override the template.
Comment: Just an addition: In your view, you can easily find the exact name you need   for the template by going to Advanced > Theme Information .


ActiveRecord associations with two different models

Question: I'm having a tough time wrapping my head around how I should be configuring my tables + associations.
I have a Lawsuit model. A lawsuit has_many parties (defendants, plaintiffs, attorneys, etc.). A party, in turn, can either be a Person or a Company. Ultimately, I want to be able to get:

A person's lawsuits (@person.lawsuits);
A company's lawsuits (@company.lawsuits); and
A lawsuit's parties (@lawsuit.parties), which can be either people or companies.

This is how I have my tables + models set up currently:
people
| id | fname  | lname | date_of_birth |
| -- | ------ | ----- | ------------- |
|  1 | John   | Smith |    1974-02-04 |
|  2 | George | Glass |    1963-07-29 |

companies
| id | name      | duns      | ticker | address      |
| -- | --------- | --------- | ------ | ------------ |
|  1 | Acme Inc. | 239423243 | ACME   | 123 Main St. |

lawsuits
| id | jurisdiction | court | case_no    | title                       |
| -- | ------------ | ----- | ---------- | --------------------------- |
|  1 |      federal | SDNY  | 18-CV-1234 | Smith v. Glass, Acme, et al |

lawsuit_parties
| id | lawsuit_id | person_id | company_id | role      |
| -- | ---------- | --------- | ---------- | --------- |
|  1 |          1 |         1 |            | plaintiff |
|  2 |          1 |         2 |            | defendant |
|  3 |          1 |           |          1 | defendant |

# models/lawsuit.rb:
class Lawsuit < ApplicationRecord
    has_many :lawsuit_parties

    def parties
        self.lawsuit_parties
    end

    def defendants
        self.parties(where(lawsuit_parties: {role: 'defendant'})
    end

    def plaintiffs
        self.parties(where(lawsuit_parties: {role: 'plaintiff'})
    end

    def attorneys
        self.parties(where(lawsuit_parties: {role: 'attorney'})
    end
end

# models/lawsuit_party.rb
class LawsuitParty < ApplicationRecord
    belongs_to :person
    belongs_to :company
end

# models/person.rb
class Person < ApplicationRecord
    has_many :lawsuit_parties
    has_many :lawsuits, through: :lawsuit_parties
end

# models/company.rb
class Company < ApplicationRecord
    has_many :lawsuit_parties
    has_many :lawsuits, through: :lawsuit_parties
end

Any help you would be much appreciated…
Answer: You're on the right track, but you'll need to introduce a polymorphic relationship onto your Join Model to get this type of modeling to work.  An Enum can handle differentiating between Defendants and Plaintiffs, as well as provide several scopes/methods you're asking for for free.
class LawsuitParty < ApplicationRecord
    belongs_to :lawsuit
    belongs_to :partiable, polymorphic: true

    enum role: [:defendant, :plaintiff]
end

You'll need to write a migration to change your lawsuit_parties table to the following columns (all Rails convention names):
partiable_id   = Integer
partiable_type = String
role           = String

lawsuit_parties

| id | lawsuit_id | partiable_id | partiable_type | role      |
| -- | ---------- | ------------ | -------------- | ----------|
|  1 |          1 |            1 | Person         | defendant |
|  2 |          1 |            2 | Company        | plaintiff |
|  3 |          1 |            1 | Company        | defendant |

Next, tell Rails that Person and Company records are associated with many Lawsuit's using has_many's :as option.
class Person < ApplicationRecord
    has_many :lawsuit_parties, as: :partiable
    has_many :lawsuits, through: :lawsuit_parties
end

Add the same has_many :lawsuit_parties, as: :partiable to Company, or any other models that may come later (i.e. Judge or JuryMember).
Once you have a LawsuitParty setup like this, you should be all set.
Comment: I think this is getting me there, thank you!

To create an association, `@lawsuit.lawsuit_parties << Person.first` gets me the following error:

```ActiveRecord::AssociationTypeMismatch (LawsuitParty(#70357266387440) expected, got # which is an instance of Person(#70357238653680))
```

Do I need to create the `LawsuitParty` record manually (`@lawsuit.lawsuit_parties.create(paritable_type: "Person", role "plaintiff"), or are there shortcut methods for this?
Comment: Glad to help.  You defined `parties` to map to LawsuitParty records somewhere.  That error is telling you that you're trying to shovel a record of the wrong type onto that association.  I don't know your DSL fully, but perhaps you meant to `@lawsuit.people << Person.first` ??
Comment: Ah, typo on my part. I had added a `parties` method to the Lawsuit model that returns `self.lawsuit_parties`). So my question is that once the relationships and tables are established, what is the standard way to add a person, company, etc. to a lawsuit's parties. E.g., `@lawsuit.lawsuit_parties << Person.first` (but that's what returns the above error).
Comment: def parties; self.lawsuit_parties; end - is the problem.  Simple thing to do so you don't have to fiddle with Rails too much is to define an "add_party_as(partiable:, role:)" method on Lawsuit.  Define how to add a party there.  It would be something like `self.lawsuit_parties.create(args)`.


Problem with getting objects based on ManyToManyField relation

Question: I have Meal model and Ingredient, Meal have ManyToMany relation to Ingredient.
I try to get objects that match by Ingredients
models.py
class Meal(models.Model):
    name = models.CharField(max_length=250)
    description = models.TextField(blank=True, null=True)
    recipe = models.TextField()
    is_published = models.BooleanField(default=False)
    user = ForeignKey(User, verbose_name='User', on_delete= models.CASCADE)
    difficulty = ForeignKey('Difficulty', on_delete=models.PROTECT, null=True)
    ingridients = models.ManyToManyField('Ingridient')
    ico = models.CharField(max_length=500, blank=True, null=True)

class CategoryIngridients(models.Model):
    name = models.CharField(max_length=100, db_index=True)

    def __str__(self):
        return self.name

class Ingridient(models.Model):
    name = models.CharField(max_length=100, db_index=True)
    category = ForeignKey('CategoryIngridients', on_delete=models.CASCADE, null=True)

    def __str__(self):
        return self.name

serializers.py
class MealSerializer(serializers.ModelSerializer):
    category = serializers.StringRelatedField()
    difficulty = serializers.StringRelatedField()
    #ingridients = serializers.StringRelatedField()
    user = serializers.StringRelatedField()
    class Meta:
        model = Meal
        fields = "__all__"

class IngrigientSerializer(serializers.ModelSerializer):
    category = serializers.StringRelatedField()
    class Meta:
        model = Ingridient
        fields = "__all__"

views.py
class TestCraft(APIView):
    def post(self, request):
        ingridients_tmp = request.data['ingredients']
        ingrid = Ingridient.objects.filter(id__in=ingridients_tmp)
        print(ingrid)
        queryset = Meal.objects.filter(ingridients__in=ingrid)
        print(queryset)
        serializer = MealSerializer(queryset, many=True)
        return Response(serializer.data)

If I POST json:
{"ingredients":[1,2]}

This view return, but they are repeating:
[
    {
        "id": 1,
        "difficulty": "Medium",
        "user": "vleo",
        "name": "gfgd",
        "description": "",
        "recipe": "...",
        "is_published": true,
        "ico": "",
        "ingridients": [
            1,
            2,
            3,
            4,
            5,
            6
        ]
    },
    {
        "id": 5,
        "difficulty": "Easy",
        "user": "vleo",
        "name": "залупа",
        "description": "[eqyz",
        "recipe": "n.,sr",
        "is_published": false,
        "ico": "",
        "ingridients": [
            1,
            2
        ]
    },
    {
        "id": 1,
        "difficulty": "Medium",
        "user": "vleo",
        "name": "gfgd",
        "description": "",
        "recipe": "...",
        "is_published": true,
        "ico": "",
        "ingridients": [
            1,
            2,
            3,
            4,
            5,
            6
        ]
    },
    {
        "id": 5,
        "difficulty": "Easy",
        "user": "vleo",
        "name": "sdf",
        "description": "[eqyz",
        "recipe": "n.,sr",
        "is_published": false,
        "ico": "",
        "ingridients": [
            1,
            2
        ]
    }
]

Also can you advise: How do i make class TestCraft(APIView), so by one path i can use ingredients from json and also retrive meals(queryset) using axios?
Answer: In case of a query with filter on many-to-many field,  entries can appear repetedly in the result. Just change to
queryset = Meal.objects.filter(ingridients__in=ingrid).distinct()

to remove multible entries with .distinct()


Easily change service reference endpoints and local vars per server build automated

Question: I have a project that needs to be deployed on multiple servers.  Its the same code, only the endpoints are different.  Currently, I have to do the following steps:
1) Configure the service reference to point to the correct server
2) Comment out the current server, uncomment the next server (a local parameter for the binding):
//private string serverName = @"http://server1/service.asmx";
//private string serverName = @"http://server2/service.asmx";
//private string serverName = @"http://server3/service.asmx";
private string serverName = @"http://server4/service.asmx";
....

ServiceClass.ServiceClassSoapClient Mgr = new ServiceClass.ServiceClassSoapClient (basicHttpBinding, new EndpointAddress(serverName));

3) Rebuild
4) Publish
There are many (16) servers that I need to do this to, and it is taking a LONG time to manually do this to all of them.  Is there a way to do this automatically?
Answer: If all you need to do is to store a string which is system-dependent, why not use the built in Application Settings?  This stores data on a per-system (or per-user) basis in AppData which is accessible via the My.Settings object.
http://msdn.microsoft.com/en-us/library/a65txexh.aspx
http://msdn.microsoft.com/en-us/library/c9db58th.aspx
For example, you would create a string type application setting called serverName and at run-time you could configure this from within your application by reading/writing to My.Settings.serverName.  The value of My.Settings.serverName is then stored for each system independently and returns a value unique to that system (or user) depending on where your application is running and who is running it.
Comment: Its not storing a string, its using a string to create a service reference, then a soap client in the code.   Then the compiled code is published to a server for general consumption.  Multiple servers are running the same code, but they have different user data, depending on the task the team wishes to store.
Comment: If I follow correctly, then this will work - instead of using `new EndpointAddress(serverName)` to create the service reference you would use `new EndpointAddress(My.Settings.serverName)` which would dynamically return (at runtime) a different string for `My.Settings.serverName` depending on the system where the compiled program is running.  The value for the setting is loaded from (or written to) disk on the system at runtime from the application/user's AppData area.
Comment: Sorry, meant to add also that the `My` namespace is not exposed in C# - have to access it directly from `Properties.Settings` etc.  Following the links should help.
Comment: So then I would need to modify the settings for every server as well?  I dont understand how this is faster.  What about the service reference?  It wont change.  (One of the servers is a test server and WILL have a different signature for testing before deploying to the other 15 servers)  Just saying the reference needs to be updated too
Comment: No, you set it up once - that's it.  Think of it like a registry setting (it isn't, but it works in a similar way).  Then any time you run any version of your application on that system it will draw from the locally stored values for your `Settings` variables.  Like a global variable which stays with a user or system apart from your compiled executable but which it can access.
Comment: I use this for control systems, for example, where I have one application which deploys across a number of manufactuing systems.  I don't have to worry about individual machine settings - I recompile a new version and run it.  It draws machine dependent settings from the `Settings` stored on each system and keeps their individual configurations consistent from build to build (ie: which equipment is physically installed on each system, what server to report to, etc).
Comment: Recompiling an application in order to change settings is ... unusual, to say the least. Most people prefer to edit a config file.