What does a triangle become?

Question: I'll get to the point.
Imagine a right triangle in 2D.
If you move one of the points (not the 90 degree vertex) very far away, then the corresponding angle will become smaller as the point moves further away.
The 90 degree angle will stay constant. The other angle will become bigger, approaching 90, keeping the total angle at 180, by definition.
What happens of the point is at "infinity," will you be left with two 90 degree angles? This is impossible as those sides would be parallel, and therefore ending up with a seemingly impossible shape (it can't be a triangle anymore).
This similar problem can be applied to different shapes and things, as well.
Thank you very much!
Answer: You get a side and two parallel half lines originating from the endpoints. Nothing extraordinary.
In standard geometry, the third vertex does not exist. In projective geometry, it becomes a point at infinity.
Answer: In the usual Euclidean plane there are no points "at" infinity, so you can never have two angles of a triangle that are exactly 90 degrees each.
All you can do is put the a vertex arbitrarily far away in order to get
the second angle arbitrarily close to a right angle.
In a projective plane there are points at infinity, and parallel lines meet at those points, so again no contradiction.
There are other possible kinds of geometry to consider,
but I suspect I've already gone a step too far by introducing
projective geometry.
Answer: Great question. The problem you are having I think is caused because of the fact that "infinity" isn't a place a point can be. We can talk about what happens as a point gets arbitrarily far apart, and as you mentioned, the other angle will get increasingly close to 90 degrees. But what if we decided to add a point at infinity? Well, this might cause some problems because all lines would intersect there(so what does parallel mean?) it ends up though if you add the point at infinity, a plane becomes a sphere(this is called the one-point compactification of the plane) On a sphere it ends up that all triangles have degrees adding up to MORE than 180 degrees, so there wouldn't be anything wrong with your triangle having two right angles.
Hopefully this helps with your thinking about infinity. It is a truly tricky concept to grasp fully.

What does a triangle become?


How to handle different error types in node.js

Question: In node world, Error object is passed as a first argument to callback.
What is the proper way to distinguish between error types in node?
Use some attribute (.errorCode, or .userNotFound)?
Have separate (Error-inherited) class and use .constructor / instanceof?
Any other convention I am missing?
Answer: You can pass anything, as long as it's not null or undefined it should represent an error.
You could use it any way you like, inherit Error is one way but you could pass an error Number or even an error String, it's really up to you.
A bit too "loose" IMHO but that's how it is.


How do I handle socket requests in separate threads, thread seems to be blocking main process

Question: I am trying to create a simple HTTP server in C++. I want each request to be handled simultaneously in separate threads, but when I create a thread and put a simple sleep(10) at the start to cause some delay, another request to the server cannot be made until the first thread finishes. What am I doing wrong?
Here is my code so far:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string>
#include <ostream>
#include <iostream>
#include <thread>

using namespace std;

void send_response (int socket)
{
    sleep (10);

    char buffer[2048] = {0};

    int request = recv (socket, buffer, 2048, 0);

    if (request == -1) {
        perror ("error");
    }

    string message = "Hello from server";
    string length = to_string (message.length ());

    string hello = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: " + length + "\n\n" + message;

    send (socket, hello.c_str (), hello.length (), 0);

    close (socket);
}

int main(int argc, char const *argv[])
{
    struct sockaddr_in address;
    int opt = 1;
    socklen_t addrlen = sizeof (address);

    int server_fd = socket (AF_INET, SOCK_STREAM, 0);

    if (server_fd == 0) {
        perror ("socket failed");
        exit (EXIT_FAILURE);
    }

    if (setsockopt (server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof (opt))) {
        perror ("setsockopt");
        exit (EXIT_FAILURE);
    }

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons (8080);

    if (bind (server_fd, (struct sockaddr *)(&address), sizeof (address)) < 0) {
        perror ("bind failed");
        exit (EXIT_FAILURE);
    }

    if (listen (server_fd, 3) < 0) {
        perror ("listen");
        exit (EXIT_FAILURE);
    }

    while (true) {
        int new_socket = accept (server_fd, (struct sockaddr *)(&address), &addrlen);

        if (new_socket < 0) {
            perror ("accept");
            exit (EXIT_FAILURE);
        }

        thread response (&send_response, new_socket);

        response.detach ();

        cout << "request handled" << endl;
    }

    return 0;
}

I don't need to wait for the thread to finish or get any data back from the thread, I just want it to start, do some work, and send something back to the client. I expect each thread will be doing a decent amount of work, that's why I used sleep.
Edit: It seems the loop is ready and waiting for a second connection and will successfully accept a new connection and create a new thread to handle it, but only if the second connection is from a different IP address. So if I visit the server at 127.0.0.1:8080 and from a different local IP address like 192.168.1.91, both connections will go through with two threads running simultaneously. However, if I simply open two tabs, both at 127.0.0.1:8080, only one connection will be accepted at a time, the other will stall. It seems I need the code to handle multiple connections from the same IP address, but a search for that kind of problem on Google hasn't given me any good results.
Comment: You're right, the function doesn't need to be passed by address. I did it because the documentation says the first parameter is a "pointer to function, pointer to member, or any kind of move-constructible function object", so I passed the address. It doesn't make a difference to the code working or not, though.
Comment: Can you attach a debugger and break while the second request is hanging? That should tell you what the main thread is up to.
Comment: ***I expect each thread will be doing a decent amount of work, that's why I used sleep*** what do you mean?
Comment: I think the issue, is the scope of the thread object - it's inside the while loop. So after one iteration, the object goes out of scope.
Comment: I mean that each thread will take a few seconds to finish, and I'd rather not have clients waiting just because one core on a 12 system is busy handling one request.
Comment: * 12 core system
Comment: I don't know what you mean by "object goes out of scope", does the loop scope affect the context the thread executes in? I've now tried adding the threads to a vector of threads, that made no difference.
Comment: Yeah sorry, I thought scope may affect but I was wrong there.
Comment: BTW, why do you provide address of send_response? Without that thread should accept the function.
Comment: Using gdb what command(s) should I use?
Comment: See my edit. I think I'm narrowing down the problem.
Comment: ***It seems I need the code to handle multiple connections from the same IP address,*** for that you may need different clients, try different browsers.
Comment: What does the other side look like, i.e. the client side? What did you do and observe which brought you to the conclusion that you present above?
Comment: The client side is a web browser (Chrome) and it just sits there loading for 10 seconds. If I open two tabs, the first tab loads for 10 seconds, the second tab loads for 20 seconds because it can only start the 10 second sleep after the first tab is finished. What I did was place a `cout` before and after the `accept` line in the loop, to see if the loop is still running with two tabs, and I observed that the loop runs only once with two tabs with the same IP, but runs twice with two tabs with different IPs.
Answer: Ok, my bad, everything is working as expected. Using wget to send multiple requests from the same IP causes multiple threads to be created and for the requests to be processed simultaneously. In Chrome, for some reason opening multiple tabs from the same IP causes them to be sent in serial, but in Firefox they are sent in parallel and arrive and are processed at the same time. That is weird, but I'll chock it up to an oddity of Chrome.


How to force manifest.json to refresh on existing Android device

Question: Within my PWA, I want to ensure any changes to the manifest.json file are forced to be applied to any device that either already has the PWA added to home screen or ensure Chrome does not cache the file so old settings are not used when adding to home screen
I've tried changing the name of the manifest file and the <link> value but this does not seem to work on the next visit
Is there a definitive way to ensure the up to date manifest file is used?
Comment: This is probably rather late, but if you are using ReactJS, there is a great guide on their site for this. https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
Answer: In most cases, changes should be reflected within a day or two of the PWA being launched, after certain fields manifest file have been updated. There are minor differences between Chrome on desktop and Chrome on Android on which fields will trigger an update. One of the easiest things to do is change the icons or the theme_color.
The key point being that the user has to open the PWA, in order for Chrome to check if the manifest has been updated.
Full details are at How Chrome handles updates to the web app manifest


jQuery simple dropdown menu

Question: I want to make a simple dropdown menu but I can't make it so the sub-menus open one at a time. For example, when you open the second menu the first one closes and so on.

$('#menu div').click(function() {
  $(this).next().slideToggle();
})

Menu 1

 SOME TEXT

Menu 2

 SOME TEXT

Menu 3

 SOME TEXT

Menu 4

 SOME TEXT

Menu 5

 SOME TEXT

Answer: To do what you need simply call slideUp() on all the p elements which are not related to the div which was clicked:

$('#menu div').click(function() {
  let $target = $(this).next().slideToggle();
  $('#menu p').not($target).slideUp();
})
p { display: none; }

Menu 1

 SOME TEXT

Menu 2

 SOME TEXT

Menu 3

 SOME TEXT

Menu 4

 SOME TEXT

Menu 5

 SOME TEXT

jQuery simple dropdown menu


Teamcity buildagent on mac, environment variable not visible to Teamcity?

Question: Having problems setting up a Mac as a buildagent for TeamCity. It works for basic builds, but I'm unable to meet build requirements for our main build.
Unmet requirements shown in Teamcity:
env.NGINX_EXE exists
(a few others too, but I'm gonna handle those when I get this one working)
I've installed NGINX, and it's running. The problem is that the environment variable I've created, doesn't register with TeamCity. What I did was add the following line to /etc/profile:
export NGINX_EXE=/usr/local/bin/nginx
- and restarted (several times).
Typing env in a terminal window lists the system variables as far as I know, and that list shows among other things:
NGINX_EXE=/usr/local/bin/nginx
I would have thought this was what was needed by TeamCity, but it still shows the requirement as unmet.
I don't really know Macs, I work with windows (and to a lesser degree - linux), so if you have any tips for me, bear that in mind.
Answer: There is a file buildAgent/launcher/conf/wrapper.conf which passes variables to the JVM which the teamcity agent runs under.
Search for wrapper.conf on the teamcity site. For example this page


Método para editar dados com Entity

Question: Estou desenvolvendo um gestor de ficheiros e já implementei alguns métodos como addArquivo, listarArquivo, listarVersoes, etc.
Porém preciso editar o mesmo, vejam meus métodos do DAL:
Adicionar:
 internal void AddArquivo(Model.Arquivo arquivo)
    {
        using (var ctx = new TESTEntities())
        {
            var versao = arquivo.ArquivoVersoes[0];
            ctx.ARQUIVO.Add(new ARQUIVO()
            {
                ARQUIVO_GUID = arquivo.ARQUIVO_GUID,
                ARQUIVO_VERSAO = new ARQUIVO_VERSAO()
                {
                    ARQUIVO_GUID = arquivo.ARQUIVO_GUID,
                    ARQUIVO_VERSAO_GUID = Util.Util.GUIDs.GetGUID(),
                    ARQUIVO = versao.ARQUIVO,
                    DATAHORA = versao.DATAHORA,
                    TAMANHO = versao.TAMANHO,
                    USUARIO_PESSOA_GUID = versao.USUARIO_PESSOA_GUID
                },
                DIRETORIO_GUID = arquivo.DIRETORIO_GUID,
                EXTENSAO = arquivo.EXTENSAO,
                IS_STREAM = arquivo.IS_STREAM,
                TAG = arquivo.TAG,
                TIPO_DE_ARQUIVO_GUID = arquivo.TipoDeArquivo.TIPO_DE_ARQUIVO_GUID,
                ULT_ARQUIVO_VERSAO_GUID = arquivo.ULT_ARQUIVO_VERSAO_GUID,
                URL = arquivo.URL,
                XARQUIVO = arquivo.XARQUIVO
            });
            ctx.SaveChanges();
        }

Obter um arquivo:
///
    /// Método que busca um arquivo conforme parametro
    ///
    /// <param name="termo"></param>
    /// <returns></returns>
    internal Arquivo GetArquivo(string termo)
    {
        using (var ctx = new TESTEntities())
        {
            var arquivo = (from arq in ctx.ARQUIVO
                           where arq.ARQUIVO_GUID == termo
                           || arq.XARQUIVO == termo
                           select new Arquivo()
                           {
                               ARQUIVO_GUID = arq.ARQUIVO_GUID,
                               DIRETORIO_GUID = arq.DIRETORIO_GUID,
                               EXTENSAO = arq.EXTENSAO,
                               IS_STREAM = arq.IS_STREAM,
                               TAG = arq.TAG,
                               TIPO_DE_ARQUIVO_GUID = arq.TIPO_DE_ARQUIVO_GUID,
                               ULT_ARQUIVO_VERSAO_GUID = arq.ULT_ARQUIVO_VERSAO_GUID,
                               URL = arq.URL,
                               XARQUIVO = arq.XARQUIVO,
                           }).FirstOrDefault();
            arquivo.ArquivoVersoes.Add(GetArquivoVersao(arquivo.ULT_ARQUIVO_VERSAO_GUID));
            return arquivo;
        }
    }

Mapeamento:
    public DbSet<ARQUIVO> ARQUIVO { get; set; }
    public DbSet<ARQUIVO_PRODUTO> ARQUIVO_PRODUTO { get; set; }
    public DbSet<ARQUIVO_VERSAO> ARQUIVO_VERSAO { get; set; }
    public DbSet<DIRETORIO> DIRETORIO { get; set; }
    public DbSet<DIRETORIO_PASTA> DIRETORIO_PASTA { get; set; }
    public DbSet<TIPO_DE_ARQUIVO> TIPO_DE_ARQUIVO { get; set; }

O que eu fiz com ajuda da resposta do Thiago Custodio:
internal void UpdateArquivo(String termo, Arquivo arquivoEditado)
    {

        var arquivo = GetArquivo(termo);
        using (var ctx = new TESTEntities())
        {

            arquivo.XARQUIVO = arquivoEditado.XARQUIVO;
            arquivo.TAG = arquivoEditado.TAG;
            arquivo.EXTENSAO = arquivoEditado.EXTENSAO;
            arquivo.URL = arquivoEditado.URL;
            arquivo.IS_STREAM = arquivoEditado.IS_STREAM;

            ctx.Entry(arquivo).State = System.Data.EntityState.Modified;
            ctx.SaveChanges();

    }
    }

Porem ta dando essa exception:
the entity type arquivo not part of the model for the current context

Comment: Já percebeu que suas perguntas sempre precisam ser editadas? Dê uma olhada, procure melhorar. Você está recebendo grande ajuda aqui no *site* ajude-nos manter tudo organizado, evite dar trabalho extra para os outros usuários que te ajudam. Especialmente cuidado para colocar *tags*, quase sempre usar uma existente é melhor que criar outra, especialmente com o nome errado. Programar exige cuidado. Cuidado se obtêm treinando em tudo na vida.
Comment: Tudo bem, vou tentar melhorar.
Comment: @WarLock, antes de começar a trabalhar com uma ferramenta, tente conhecer a mesma primeiro, vejo que está tentando utilizar o EF, sem ter o menor conhecimento do seu funcionamento. Neste caso te aconselho ler o seguinte artigo: http://www.macoratti.net/10/11/c_efcrud.htm
Comment: @TobyMosque atualizei a pergunta, com o que tentei com a ajuda do amigo thiago, valeu pelo link tenho que estudar, mas ver o que pode me ajudar no momento.
Answer: algo assim:
var termo = "";
var arquivo = GetArquivo(termo);

arquivo.TAG  = "outra tag";

using (var ctx = new TESTEntities())
{
    ctx.Entry(arquivo).State = System.Data.Entity.EntityState.Modified;
    ctx.SaveChanges();
}

Comment: esse outra tag seria o q?
Comment: voce colocou arquivo.tag , como exemplo de q? da alteração de um dado? no caso eu teria que alterar todos os atributos?
Comment: uma propriedade da sua classe Arquivo, não!?
Comment: vc pode setar qualquer prop que vc queira alterar...dei só este exemplo de como alterar a propriedade TAG.
Comment: eu sei, mas no caso eu teria que fazer em todas as propriedades setadas neh?
Comment: mas tipo, ai esta manual neh, qual seria a melhor forma de editar o que o usuario passou?
Comment: eu teria que trazer do cliente, um objeto com tudo ja setado neh?]
Comment: vou testar aqui e te retorno valeu
Comment: ta dando essa exception: The entity type Arquivo is not part of the model for the current context.
Comment: sabe o que pode ser?
Answer: Consegui usando da forma seguinte:
internal void UpdateArquivo(Model.Arquivo arquivoEditado)
    {
        using (var ctx = new TESTEntities())
        {
            var file = ctx.ARQUIVO.FirstOrDefault(a => a.ARQUIVO_GUID == arquivoEditado.ARQUIVO_GUID);
            if (file == null)
                throw new ArquivoException("Arquivo não encontrado");

            file.XARQUIVO = arquivoEditado.XARQUIVO;
            file.TAG = arquivoEditado.TAG;
            file.EXTENSAO = arquivoEditado.EXTENSAO;
            file.URL = arquivoEditado.URL;
            file.IS_STREAM = arquivoEditado.IS_STREAM;

            ctx.Entry(file).State = System.Data.EntityState.Modified;
            ctx.SaveChanges();

        }

Foi so usar o :
 var file = ctx.ARQUIVO.FirstOrDefault(a => a.ARQUIVO_GUID == arquivoEditado.ARQUIVO_GUID);

que assim ele pegava exatamente o arquivo que eu queria editar, mas obrigado a todos.
Comment: no lugar de usar o .FirstOrDefault(), utilize o .Find(), pois o mesmo tem um nome mais apropriado para esta situação e é cerca de duas vezes mais rapido.
Comment: poderia entrar no seguinte chat? http://chat.stackoverflow.com/rooms/70597/mvc-wcf-e-entity-framework
Comment: Vou entrar, galera de .net?
Comment: brasileiro?....
Comment: tinha criado este canal para tentar te dá algumas dicas.
Comment: se puder entrar la agora

Método para editar dados com Entity


How to plot a histogram of a Column 'Applicant_Income' based on a condition of other column 'Education"?

Question: I have a csv file with columns 'Applicant_Income' and 'Education'. I can plot the 'Applicant_Income' but how do i plot a hist of 'Applicant_Income' where 'Education' = 'Graduate'.
I tried the something in boxplot, would like something similar on histogram.
g1=df.boxplot('ApplicantIncome', by = 'Education')

Comment: Welcome to SO! Try to supply a [Minimal Complete Working Example](https://stackoverflow.com/help/minimal-reproducible-example) showing your data, what you have tried so far, and what you would like to achieve. For me it is not clear if you want to plot *only* the data where `'Education' == 'Graduate'`, or *all* data, but *grouped by* `'Education'`.
Answer: Generally, you will want to use plt.hist for histograms. From your question it's not completely clear whether you want to filter the data ('Education' == 'Graduate') or plot a single histogram for every group in Education.
Lets's generate example data
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.DataFrame({'ApplicantIncome': np.random.normal(500, 2000, 500),
                   'Education': np.random.choice(['Graduate', 'High School', 'Undergrad'], 500)})

    ApplicantIncome Education
0   617.660998      Undergrad
1   1335.209849     Graduate
2   -966.902454     Undergrad
3   1775.496777     High School
4   -819.677673     Undergrad

Now if you just want to plot the filtered data:
df_grad = df[df['Education'] == 'Graduate']
plt.hist(df_grad['ApplicantIncome'])

If you want to plot one histogram for each education status:
for name, group in df.groupby('Education'):
    plt.hist(group['ApplicantIncome'], label=name)


Eighth-note missing from score?

Question: In this version of sheet music "The Rain" by Joe Hisaishi, how should I read this line?
It seems that voice y in measure 42 is missing one eighth note (between eighth note B in treble clef and quarter note F in bass clef) and has one redundant quarter rest?

original image:
Answer: Treat the B in m. 42 beat 1 as though it is a quarter-note.
Two alternative notations are given below.

The first keeps "voice y" intact and uses left- and right-hand brackets to show how to play it.
The second adds a quarter-note stem to the right-hand chord and removes the quarter rest.
Answer: It appears that the typesetter took the upper section of the bass clef as a single part, and this part has a crotchet rest for the first beat of bar 42. The low B in the right hand is regarded as part of the right hand part. The diagonal lines do not show where the part moves from staff to staff (as would naturally understand them), but instead show how the melody moves from one part to another. This approach is consistent mathematically.
I would prefer to regard that B as a note in a part that moves between staves. If the music is understood this way the crotchet rest should not be present, and the low B in the right hand is a crotchet. It only appears to be a quaver, because the extension of the stem coincides with the stem of the quaver in the right hand accompaniment part. Aaron's answer shows two other ways of notating it.
Answer: I would play the music as notated and not treat that "Voice Y" B in Bar 42 as a quarter note.
That "Voice Y" B 8th note is immediately followed by another 8th note on the exact same B. This means you need to re-strike it an 8th note in.
Using lines to show the voice is noble, but don't always assume the voice has to be continuous, with no rests.
Comment: Isn't it B *natural*?
Comment: I wonder if you misunderstood my post. Of course one re-strikes the eighth note, but musically, it's a quarter note.
Comment: @Tim - Whoops, it is a B natural. Edited my answer accordingly.
Answer: There are three musical 'voices' going on here.  The bass line, the mid-range melody and the 8th-note accompaniment in the upper stave.
Ideally, this would be played by three instruments.  A cello or double bass on the bottom, a tenor-range melody instrument, a keyboard on top.    At the point in question, the melody instrument could then hold a full quarter note on that B, while the keyboard could also play its 8th note.
But we're covering all three 'instruments' with one piano keyboard!   We must compromise. It is technically impossible to literally hold the melody note full length while also playing the figuration above.  But it's quite possible to give the impression that this is happening!   This happens a lot in piano music.  Don't worry about it.

Eighth-note missing from score?


Finding the probability density function and the distribute accumulate function

Question: For a random variable $X$, $(X^3-1)$ is uniformly distributed in the interval $[0,7]$

I need to find the probability density function and the cumulative distribution function of $X$

My attempt:
$(X^3-1)=7\Longrightarrow X=\color{blue}2$
cumulative distribution function:
$$F_{X}(x)=
\begin{cases}
0&&;x<2\\
\frac{x^3-1}{7}&&;0\leq x \leq 2\\
1&&;x>2\\
\end{cases}$$
probability density function
$$F'_{X}(x)=f_{X}(x)=
\begin{cases}
0&&;x<2\\
\frac{3x^2}{7}&&;0\leq x \leq 2\\
0&&;x>2\\
\end{cases}$$

Is it correct?
Comment: There are many notational errors involving switching the roles of $X$ and $x$. There are also some incorrect intervals. Substantial rewriting is needed.
Comment: ..I will edit..
Comment: For the cdf (cumulative distribution function) $F_X(x)$, it is $0$ if $x\lt 1$, and $\frac{x^3-1}{7}$ for $1\le x\lt 2$.
Comment: .and  if  $x>2$.?
Comment: You had that part right, so I did not comment on it.
Answer: cumulative distribution function:
$$F_{X}(x)=
\begin{cases}
0&;x < 1\\
\frac{x^3-1}{7}&;1\leq x \leq 2\\
1&;x>2\\
\end{cases}$$
probability density function:
$$f_{X}(t)=
\begin{cases}
0&;t \not\in (1,2)\\
\frac{x^2}{7}&; t \in (1,2)\\
\end{cases}$$


If folders don't exist in URI path, file_save_data() fails

Question: I am programmatically importing images and saving them via file_save_data();
$fileContentsPath = 'http://mypathtofile.com';
$uri = 'public://myfolder/mysecondfolder/filename.jpg';
$image = file_get_contents($fileContentsPath);
$file = file_save_data($image, $uri, FILE_EXISTS_REPLACE);

When I run this I get the following error: The specified file temporary://fileJ4LL4o could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.
I am pretty sure my temporary directory is configured correctly and the files folder is assigned to the apache user.
If I go into the files directory and make the directories (myfolder/mysecondfolder) via command line and rerun it works correctly.
So the problem is that if the folders don't exist from the $uri path, it fails instead of creating the folders.
Answer: In drupal 8.7.0 and above (Drupal 9), file_prepare_directory() is deprecated.
Instead, use FileSystemInterface::prepareDirectory() : https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21File%21FileSystemInterface.php/function/FileSystemInterface%3A%3AprepareDirectory/8.8.x
In my case, I did :
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$directory = 'temporary://exports/';
$file_system->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);

Answer: I Noticed Similar issue while saving files using
 file_save_data();

In Drupal 8.
First I gave only first level directory to save the file as
$file = file_save_data($object->vertex_data, 'public://vertex_data.txt', FILE_EXISTS_REPLACE);

This worked fine.
But when I tried saving the file inside a subfolder as
$file = file_save_data($object->vertex_data, 'public://objects/vertex_data.txt', FILE_EXISTS_REPLACE);

it gave me the following Error :
File temporary://fileh1Gpl2 could not be moved/copied because the destination directory public://objects/ is not configured correctly.

The Solution for this is simple. Just use file_prepare_directory();
$directory = 'public://objects/'
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
$file = file_save_data($object->vertex_data,  $directory . 'vertex_data.txt', FILE_EXISTS_REPLACE);

This, first creates the folder if it does not exists and then proceeds to save the respective file.
Answer: I ended up using a function that creates the folders if needed. I copied what Patrick had here.
/**
 * Download external file.
 *
 * @param string $url       Full url to file to download.
 * @param string $uri       Drupal uri of where to save file
 *                          public://archive/test.pdf.
 * @param int $save_mode    File save mode from drupal core
 *                          Ex: FILE_EXISTS_REPLACE.
 *
 * @return mixed $new_file  The file object.
 */
function xcms_import_download_external_file($url, $uri, $save_mode = FILE_EXISTS_RENAME, $manage_file = TRUE) {

  $url_info = parse_url($url);
  $url_path_info = pathinfo($url_info['path']);

  // This helps with filenames with spaces
  $url = $url_info['scheme'] . '://' . $url_info['host']  . $url_path_info['dirname'] .'/'. rawurlencode($url_path_info['basename']);

  // Need to remove the filename from the uri
  $uri_target = file_uri_target($uri);
  $uri_scheme = file_uri_scheme($uri);
  $uri_path_info = pathinfo($uri_target);
  $directory = file_stream_wrapper_uri_normalize($uri_scheme . "://" . $uri_path_info['dirname']);

  if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
    $drupal_result = drupal_http_request($url);
    if (!empty($drupal_result->data)) {
      $path = file_stream_wrapper_uri_normalize($uri);
      if ($manage_file) {
        $new_file = file_save_data($drupal_result->data, $path, $save_mode);
      }
      else {
        return file_unmanaged_save_data($drupal_result->data, $path, $save_mode);
      }
    }
    else {
      drupal_set_message("Error downloading file, no data recieved for " . $url);
      return FALSE;
    }

    $new_file->display = 1;
    return (array)$new_file;
  }
  else {
    return FALSE;
  }
}


How do I create a derivative?

Question: I would like to create a queue derivative, where I can use a single QueueWorker class to process multiple queues, but I am not able to achieve it. Any help would be greatly appreciated.
The module structure is the following.
queue_examples/
├── queue_examples.info.yml
├── queue_examples.routing.yml
└── src
    ├── Form
    │   └── QueueDataImportForm.php
    └── Plugin
        ├── Derivative
        │   └── QueueDataProcessorDerivative.php
        └── QueueWorker
            └── QueueDataProcessor.php

The code I am using is the following.
QueueDataImportForm.php
namespace Drupal\queue_examples\Form;

use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\ClientInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Queue\QueueWorkerManager;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Component\Datetime\Time;
use Drupal\Component\Utility\Unicode;

/**
 * Configure queue_examples settings for this site.
 */
class QueueDataImportForm extends FormBase {

  /**
   * The HTTP client to fetch the feed data with.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * Batch Builder.
   *
   * @var \Drupal\Core\Batch\BatchBuilder
   */
  protected $batchBuilder;

  /**
   * Drupal\Component\Datetime\Time instance.
   *
   * @var \Drupal\Component\Datetime\Time
   */
  public $time;

  /**
   * Drupal\Core\Queue\QueueFactory instance.
   *
   * @var \Drupal\Core\Queue\QueueFactory
   */
  protected $queueFactory;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * LocationsBatchImportForm constructor.
   */
  public function __construct(ClientInterface $http_client, QueueFactory $queue_factory, QueueWorkerManager $queue_manager, ModuleHandlerInterface $module_handler, Time $time) {

    $this->httpClient = $http_client;
    $this->batchBuilder = new BatchBuilder();
    $this->queue_factory = $queue_factory;
    $this->queue_manager = $queue_manager;
    $this->moduleHandler = $module_handler;
    $this->time = $time;

  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('http_client'),
      $container->get('queue'),
      $container->get('plugin.manager.queue_worker'),
      $container->get('module_handler'),
      $container->get('datetime.time')

    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'queue_examples_batch_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['help'] = [
      '#markup' => $this->t('This form is used to get the location data from remote URL and push data to queue in Batches.'),
    ];

    $sourceUrl = 'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/countries.json';

    $link = Link::fromTextAndUrl('here', Url::fromUri($sourceUrl, ['attributes' => ['target' => '_blank']]));

    $locations = [
      'countries' => $this->t('Countries'),
      'states' => $this->t('States'),
      'cities' => $this->t('Cities'),
      'cities_one' => $this->t('Cities One'),
    ];

    $form['locations_list'] = [
      '#title' => $this->t('Select Location Type.'),
      '#type' => 'select',
      '#options' => $locations,
      '#empty_option' => $this->t('Choose a Location'),
      '#required' => TRUE,
    ];

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['run'] = [
      '#type' => 'submit',
      '#value' => $this->t('Import Locations'),
      '#button_type' => 'primary',
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // Check whether the Location Type is valid or not.
    $location = $form_state->getValue('locations_list');
    if (!in_array($location, ['countries', 'states', 'cities'], TRUE)) {
      $form_state->setErrorByName('locations_list', $this->t('The selected location type %location is invalid.', ['%location' => $location]));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $data = [];

    $link = Link::fromTextAndUrl('here', Url::fromRoute('queue_examples.queue_data_import_form'));

    $location = $form_state->getValue('locations_list');

    if (!empty($location)) {

      $remoteUrl = 'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/' . $location . '.json';

      $data = $this->fetchData($remoteUrl);

      if (!empty($data)) {

        $decodedData = Json::decode($data);

        $this->batchBuilder
          ->setTitle($this->t('Processing @location Batch...', ['@location' => Unicode::ucfirst($location)]))
          ->setInitMessage($this->t('Initializing Batch...'))
          ->setProgressMessage($this->t('Completed @current of @total. Estimated remaining time: @estimate.'))
          ->setErrorMessage($this->t('An error has occurred.Click @link to return to form', ['@link' => $link->toString()]));
        $this->batchBuilder->addOperation([$this, 'processItems'], [$location, $decodedData]);
        $this->batchBuilder->setFinishCallback([$this, 'finished']);
        batch_set($this->batchBuilder->toArray());
      }
    }

  }

  /**
   * {@inheritdoc}
   */
  public function fetchData(string $url) {
    $data = [];
    try {

      // @todo check whether implementation of 'timeout' => 600
      // is correct or not in httpClient.
      $response = $this->httpClient->get($url, ['headers' => ['Content-Type' => 'application/hal+json']]);
      if ($response->getStatusCode() == 200) {
        $data = (string) $response->getBody();
      }

    }
    catch (RequestException $exception) {

      $this->messenger()->addError('Failed to download JSON data from URL due to an error check logs for more details.');

      $this->logger('queue_examples')->warning('Failed to download JSON data from URL due to "%error".', ['%error' => $exception->getMessage()]);
    }
    return $data;
  }

  /**
   * Processor for batch operations.
   */
  public function processItems($location, $items, array &$context) {
    // Elements per operation.
    $limit = 50;

    // Set default progress values.
    if (empty($context['sandbox']['progress'])) {
      $context['sandbox']['progress'] = 0;
      $context['sandbox']['max'] = count($items);
    }

    // Save items to array which will be changed during processing.
    if (empty($context['sandbox']['items'])) {
      $context['sandbox']['items'] = $items;
    }

    $counter = 0;
    if (!empty($context['sandbox']['items'])) {
      // Remove already processed items.
      if ($context['sandbox']['progress'] != 0) {
        array_splice($context['sandbox']['items'], 0, $limit);
      }

      foreach ($context['sandbox']['items'] as $item) {
        if ($counter != $limit) {
          $this->processItem($location, $item);
          $counter++;
          $context['sandbox']['progress']++;

          $context['message'] = $this->t('Now Processing Data Item :progress of :count', [
            ':progress' => $context['sandbox']['progress'],
            ':count' => $context['sandbox']['max'],
          ]);

          // Increment total processed item values. Will be used in finished
          // callback.
          $context['results']['processed'] = $context['sandbox']['progress'];
        }
      }
    }

    // If not finished
    // If not finished all tasks, we count percentage of process. 1 = 100%.
    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
  }

  /**
   * Process single item.
   *
   * @param string $location
   *   Location Type.
   * @param array $item
   *   Single Location Data.
   */
  public function processItem(string $location, array $item) {

    if (!empty($item)) {

      // Create new queue item.
      $queue = $this->queue_factory->get('queue_data_processor:' . $location);
      $queue->createItem($item);
      // $queue->deleteItem($data);
      $this->logger('queue_examples')
        ->notice($this->t('@location Data Item Pushed to Queue: @data',
          [
            '@data' => implode(',', $item),
            '@location' => Unicode::ucfirst($location),
          ])
        );
    }

  }

  /**
   * Finished callback for batch.
   */
  public function finished($success, $results, $operations) {
    $message = $this->t('Number of Items processed by batch: @count', [
      '@count' => $results['processed'],
    ]);

    $this->messenger()->addStatus($message);
    $this->logger('queue_examples')->info($message);
  }

}

QueueDataProcessorDerivative.php
namespace Drupal\queue_examples\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;

/**
 * The simple derivative example for Queue.
 */
class QueueDataProcessorDerivative extends DeriverBase {

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {

    $locations = ['countries', 'states', 'cities'];

    foreach ($locations as $location) {

      $this->derivatives[$location] = $base_plugin_definition;
      $this->derivatives[$location]['title'] = $location . 'Queue';

    }

    return $this->derivatives;
  }

}

### QueueDataProcessor.php
namespace Drupal\queue_examples\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Processes Locations Data.
 *
 * @QueueWorker(
 *   id = "queue_data_processor",
 *   title = @Translation("Queue Data Processor."),
 *   cron = {"time" = 10},
 *   deriver = "Drupal\queue_examples\Plugin\Derivative\QueueDataProcessorDerivative",
 * )
 */
class QueueDataProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  use StringTranslationTrait;

  /**
   * The Messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Logger service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $logger;

  /**
   * Database service.
   *
   * @var \Drupal\Core\Database\Connection
   */

  protected $database;

  /**
   * {@inheritdoc}
   */
  public function __construct(LoggerChannelFactoryInterface $logger, MessengerInterface $messenger, Connection $connection) {
    $this->logger = $logger->get('queue_examples');
    $this->messenger = $messenger;
    $this->database = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $container->get('logger.factory'),
      $container->get('messenger'),
      $container->get('database')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($data) {

    $id = $this->getDerivativeId();

    // @todo if id = countries insert data into countries table.
    // if id = states insert data into states table.
    // if id = cities insert data into cities table.
    $this->logger->info($id . 'Queue Processed.');

    if (!empty($data)) {
      // $query = $this->database->insert($id);
      // $query->fields($data);
      // $query->execute();
      $this->logger->info($this->t('Data available to Process.'));

    }
    else {
      $this->logger->warning($this->t('No Data available to Process.'));

    }

  }

}

What's wrong in the code I am using?
I would like to insert countries data into countries table, states data into states table, and cities data into cities table. All using single queue processor file using derivatives.
I know of other way where you can conditionally check data and process the data accordingly, but I would like to achieve it using queue derivative.
I can see 3 Queues in the list when I run drush queue:list.
 ------------------------------------ ------- ---------------------------------
  Queue                                Items   Class
 ------------------------------------ ------- ---------------------------------
  queue_data_processor:countries       0       Drupal\Core\Queue\DatabaseQueue
  queue_data_processor:states          0       Drupal\Core\Queue\DatabaseQueue
  queue_data_processor:cities          0       Drupal\Core\Queue\DatabaseQueue
 ------------------------------------ ------- ---------------------------------

In case it helps, I got this idea reading Drupal 8: Derivatives — множественные экземпляры плагина.
Edit 1:
The issue I am having is Id is Null in  QueueDataProcessor::processItem($data)
Answer: With the Help of Niklan I was able to resolve the issue .
The reason for $Id is NULL because of missing parent constructor call. $Id value is available once parent constructor is called.
Full code of Queue Controller as follows :
<?php

namespace Drupal\queue_examples\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Component\Utility\Unicode;

/**
 * Processes Locations Data.
 *
 * @QueueWorker(
 *   id = "queue_data_processor",
 *   title = @Translation("Queue Data Processor."),
 *   cron = {"time" = 10},
 *   deriver = "Drupal\queue_examples\Plugin\Derivative\QueueDataProcessorDerivative",
 * )
 */
class QueueDataProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  use StringTranslationTrait;

  /**
   * The Messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Logger service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $logger;

  /**
   * Database service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = new static($configuration, $plugin_id, $plugin_definition);
    $instance->logger = $container->get('logger.factory')->get('queue_examples');
    $instance->messenger = $container->get('messenger');
    $instance->database = $container->get('database');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($data) {

    $plugin_id = $this->getPluginId();
    $id = $this->getDerivativeId();

    if (!empty($data)) {
      // $query = $this->database->insert($id);
      // $query->fields($data);
      // $query->execute();
      $this->logger->notice($this->t('@id Data Inserted : @data',
          [
            '@id' => Unicode::ucfirst($id),
            '@data' => implode(',', $data),
          ]
        ));

    }
    else {
      $this->logger->warning($this->t('No Data available to Process.'));

    }

  }

}

How do I create a derivative?


Client connects to server randomly

Question: I am trying to write a simple client/server program where the server first connects to the client, and sends the client a message. The client then echoes back the message to the server in uppercase.
The thing is, the connection is random; sometimes the client connects and sometimes it doesn't.
EDIT: when it doesn't connect, I get an "Address already in use" error. Is there some way to free the address on the server side?
SERVER.C
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>

int main(){
  int welcomeSocket, newSocket, portNum, clientLen, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;
  int counter = 0;

  welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

  portNum = 7891;

  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(portNum);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

  bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  /*---- Listen on the socket, with 5 max connection requests queued ----*/
  if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  /*---- Accept call creates a new socket for the incoming connection ----*/
  addr_size = sizeof serverStorage;
  while(1){
      newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
/*    counter ++ ;*/
/*    printf("client connected: %d\n",counter);*/
      /*fork a child process to handle the new connection*/
      if(!fork()){
          /*---- Send message to the socket of the incoming connection ----*/
          strcpy(buffer,"Hello World\n");
          send(newSocket,buffer,13,0);
          recv(newSocket,buffer,13,0);

          /*---- Print the received message ----*/
          printf("Data received: %s",buffer);
          close(newSocket);
          exit(0);
      }
      /*if parent, close the socket and go back to listening new requests*/
      else{
          close(newSocket);
      }
  }

  return 0;
}

CLIENT.C
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <ctype.h>

int main(){
  int clientSocket, portNum, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  socklen_t addr_size;

  clientSocket = socket(PF_INET, SOCK_STREAM, 0);

  portNum = 7891;

  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(portNum);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

  /*---- Connect the socket to the server using the address struct ----*/
  addr_size = sizeof serverAddr;
  connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

  /*---- Read the message from the server into the buffer ----*/
  nBytes = recv(clientSocket, buffer, 1024, 0);

  /*---- Print the received message ----*/
  printf("Data received: %s",buffer);

  for (int i=0;i<nBytes-1;i++){
     buffer[i] = toupper(buffer[i]);
  }

  send(clientSocket,buffer,nBytes,0);

  return 0;
}

Comment: "I cannot figure out why" - with one solitary exception, you don't check whether a single one of your system calls succeeds. Every system call which can fail gives you a way to check whether it did, and if it did, gives you a way to find out *why* it failed. If you deliberately neglect to make use of this information, you don't really have a moral right to expect your program to work, or to ask for help finding out why it doesn't. Check each of your system calls for success, and there's a high chance you'll answer your own question.
Comment: You seem to know what socket functions to call and how they work.  So you must have read the man pages.  And if you read the man pages, you should have also read the sections on what these functions return and how they indicate an error.
Comment: @PaulGriffiths can you give me some ideas as to how i can check why it failed?  I am new to socket programming
Comment: Calling `perror()` after it's failed is the easiest way. If you need more flexibility, you can work directly with `errno` and `strerror()`. For simple programs like this, you'll also generally want to terminate the program on failure, rather than just attempting to carry on regardless in a broken state.
Comment: @PaulGriffiths thanks for your help!
Comment: looking at posted client code.  needs `#include ` for the function `fork()` and function: `close()`.  there are three unused variables: `counter`, `nBytes` and `clientLen`  Also, `serverAddr.sin_port - htons( portNum);` is expecting `portNum` to be type: `uint16_t`  but it is declared as `int`,  Modifying byte order with different length values (usually) will not work as the posted code expects
Comment: for ease of readability and understanding: 1) separate code blocks (for, if, else, while, do...while, switch, case, default) via a single blank line  2) 2 space indent levels is not wide enough to be visible when using variable width fonts, suggest 4 spaces  per indent level. 3) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.*
Comment: error messages should be output to `stderr` not `stdout` and when the error indication is from a system function, should also output the reason the OS thinks the error occurred.  Therefore, rather than `printf()` suggest using `perror()`
Comment: regarding this code: `if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");` when the error occurs should NOT continue executing the code, as if the operation were successful,  Suggest: `if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else { perror( "listen failed" ); exit( EXIT_FAILURE ); }`
Comment: these two lines: `send(newSocket,buffer,13,0);
          recv(newSocket,buffer,13,0);`  use a 'magic' number 13. Strongly suggest letting the compiler handle the length calculation similar to :  `send(newSocket,buffer, strlen(buffer),0);
          recv(newSocket,buffer, strlen(buffer),0);`
Comment: when calling `send()` and when calling `recv()`,  always check the returned value to assure the operation was successful.   Also, use the returned value from the `recv()` to place a NUL byte at the end of the data in the buffer[] before calling `printf("Data received: %s",buffer);`
Comment: the server code needs to check the returned value from the call to `bind()` and from the call to `socket()`  the posted code also has some other 'magic' numbers: 1024 and 7891.   'magic' numbers are numbers with no basis.  'magic' numbers make the code much more difficult to understand, debug, maintain.  Suggest using a `enum` statement or `#define` statements to give those 'magic' numbers meaningful names, then use those meaningful names throughout the code.
Comment: when calling `connect()`, always check the returned value to assure the operation was successful. when initializing the `serverAddr` structure, the first statement should initialize the whole structure to '\0' then set the 3 fields of interest,  Then the code, amongst other things, does not need to address all the fields of the structure
Comment: this line: `nBytes = recv(clientSocket, buffer, 1024, 0);` would be much better written as: `nBytes = recv(clientSocket, buffer, sizeof(buffer), 0);`
Comment: the `recv()` function does not automatically NUL terminate the input data, so the returned value from the call to `recv() should be used to terminate the char array,  similar to: `buffer[ nBytes ] = '\0';`
Answer: The "address already in use" error occurrs on a call to bind when a socket is already bound to that port.  In the case of a listening TCP socket, that can happen when the program is restarted due to old connected sockets not being completely closed yet.
When binding a listening socket, you should set the SO_REUSEADDR socket option.  This will allow you to bind a TCP listening socket in these situations.
int option = 1;
if (setsockopt(welcomeSocket, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) == -1) {
    perror("setsockopt for SO_REUSEADDR failed");
    exit(1);
}

This function should be called after socket but before bind.

Client connects to server randomly


Issues recalling an animation function whilst making a jQuery image gallery. Next/Previous buttons

Question: this is my first ever post to Stack Overflow and it's because I've been tearing my hair out over this issue for the past day.
I'm pretty new to jQuery and have a basic understanding of how things work but I'm really struggling with getting a function to loop back on itself after it has processed. It's a simple if/else function that runs through a series of five images to see if they are visible or not.
If an image is visible (in other words, the current image in a gallery) then the next and previous buttons will control which image is next to be faded in and subsequently fade out the previous image.
I have the Next/Previous links working for the first image but I can't get it to work once the second image has faded in. I'm guessing it's because I need to recall the script for it to check if/else the images are visible. My problem is that I don't know where to place the code. I've tried the code in various places and I've been researching into jQuery delays and callbacks to see if I can try and work out whether it's the timings not working etc. But I just can't work it out!
If anyone can help get this working or even suggest a simpler way of creating this that I don't know, I would be greatly appreciative!
The images are simply listed within a div:

    Previous
    Next

And the jQuery is:
window.addEventListener( 'DOMContentLoaded', function(){

function galleryLoop(){

        if( $('div.images img:nth-child(1)').css({ 'display' : 'inline' }) ){

            //#NEXT CLICK
            $('a.gallery-button#next').bind('click', function(){
                $('div.images img:nth-child(1)').fadeOut( '100' , function(){
                    $('div.images img:nth-child(2)').fadeIn();
                });//END FADE OUT
            }); //END #NEXT CLICK

            //#PREVIOUS CLICK
            $('a.gallery-button#previous').bind('click', function(){
                $('div.images img:nth-child(1)').fadeOut( '100' , function(){
                    $('div.images img:nth-child(5)').fadeIn();
                });//END FADE OUT
            }); //END #PREVIOUS CLICK

        } //END IF

        else if( $('div.images img:nth-child(2)').css({ 'display' : 'inline' }) ){

            //#NEXT CLICK
            $('a.gallery-button#next').bind('click', function(){
                $('div.images img:nth-child(2)').fadeOut( '100' , function(){
                    $('div.images img:nth-child(3)').fadeIn();
                });//END FADE OUT
            }); //END #NEXT CLICK

            //#PREVIOUS CLICK
            $('a.gallery-button#previous').bind('click', function(){
                $('div.images img:nth-child(2)').fadeOut( '100' , function(){
                    $('div.images img:first-child').fadeIn();
                });//END FADE OUT
            }); //END #PREVIOUS CLICK

        }; //END IF

    };

galleryLoop();

});//END SCRIPT
Answer: Take the function out of the event listener and you're gonna be able to call it anytime.
Answer: Ok I spent a lot of time on this so I hope it solves your problem!
Here is the link to the JSFiddle: http://jsfiddle.net/ekUvj/22/
What you want to do is change you javascript to this:
var imgNum = 1;
$("#1").fadeIn(300);

function prev () {
    newImageNum=(imgNum == 1)?5:imgNum-1;
    $("#".concat(imgNum)).fadeOut(300, function () {$("#".concat(newImageNum)).fadeIn(300);});
    imgNum = newImageNum;
}

function next () {
    newImageNum=(imgNum == 5)?1:imgNum+1;
    $("#".concat(imgNum)).fadeOut(300, function () {$("#".concat(newImageNum)).fadeIn(300);});
    imgNum = newImageNum;
}

And then make sure your buttons call the functions:

    <button onclick="prev()">Previous
    <button onclick="next()">Next

Then make sure your sure your images are hidden at first in the CSS:
.gallery-img {
    display: none;
}


Interaction term negative when both its components are positive

Question: I am running a negative binomial regression to obtain an interaction term for two variables. Both of the variables independently significantly increase the outcome variable, however according to this output below, they interact to significantly reduce the outcome variables score. I have attached the output below, dum2 is an IV, along with emotdys5. The outcome is sdq_conduct2, while the interaction term is something like dumXemotdys5.
My question is how is it possible for two variables to independently increase the score of the outcome variable, but together significantly reduce it?
Thanks for any help.
Comment: Look at the magnitude of the interaction effect, compared to the main effects.  Also, write out the prediction for a few different values - does it still increase as you increase the explanatory variable?
Answer: I think your confusion arises from trying to interpret the interaction term independently of the main effects - when you have interactions you have to interpret the effects of one variable conditional on the value of the other.
So in your case, you would say that - when emotdys5 is equal to zero - the slope of the relationship between _Idum2_1 and the outcome is 0.36 (and vice versa, when _Idum2_1 is equal to zero the slope of the relationship between emotdys5 and the outcome is 1.25).
When the value of one variable increases by one unit, the slope of the relationship between the other variable and the outcome increases by the estimated interaction term (or in this case decreases, because you have a negative interaction). So when emotdys5 is equal to one, the slope of the relationship between _Idum2_1 and the outcome is 0.36 - 0.14 = 0.22.
Basically, the correct interpretation is that the positive relationships between the two IVs and the outcome are smaller/weaker when the values of the other one is high - not that they 'together significantly reduce it'.
As an aside - make sure you choose meaningful zero values for your variables, since if your zero values aren't meaningful, it will be substantially harder to interpret your results.


Como converter byte[] para mp3

Question: Eu estou escrevendo um app onde eu tenho uma lista de byte[] e preciso criar arquivos .mp3 a partir desses vetores de bytes. Quando solicitado, o app zipa todos esses arquivos em um arquivo .zip, porém posteriormente o player não é capaz de reproduzir qualquer um desses áudios. O byte[] é originário de uma openStream(), onde é transformado num vetor de bytes para que o app possa ter todos os áudios "in memory", para que só depois todos sejam transformados em um arquivo.
Cria arquivo
 private File createFile(String name, byte[] data) {
        try {
            FileOutputStream out = getContext().openFileOutput(name, Context.MODE_PRIVATE);
                             out.write(data);
                             out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return getContext().getFileStreamPath(name);
    }

Seria preciso escrever um header para o arquivo? Se sim, como?
https://stackoverflow.com/questions/22319372/to-convert-byte-array-to-mp3-file
Answer: Para MP3 as coisas funcionam de maneira diferente, arquivos neste formato sofrem encode(codificação), essa codificação permite que os dados raw(bytes) sofra compressão, esta ação vai causar perda de dados, o processo todo demanda entendimento de psicoacústica, processamento digital de sinais, matemática discreta, etc, etc, tudo isso é aplicado para que as perdas durante a codificação tenha a mínima percepção humana possível, portanto meu caro, o header não arranha nem a superfície da complexidade para se criar um arquivo .mp3, a maneira mais fácil, caso você não tenha interesse em saber a fundo como tudo isso funciona será usar alguma lib de terceiro para efetuar o processo de encode.
Um arquivo WAV é simples, não existe nenhum tipo de codificação e perda dos dados de áudio.
Comment: Eu tinha uma noção que um arquivo mp3 era algo complexo, mas não tanto assim. Me deixou interessado em pesquisar sobre. Sobre o app: estou considerando a possibilidade de mudar o formato para wav
Answer: Isso resolveu meu problema.
try {
                ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
                InputStream stream               = pssu.openStream();
                File mp3                         = new File(getFilesDir(), fileName.toLowerCase());
                OutputStream os                  = new FileOutputStream(mp3);

                byte[] buffer = new byte[4096];

                int len;
                while ((len = stream.read(buffer)) > 0) {
                    os.write(buffer, 0, len);
                }

                audios.add(mp3);

                byteBuffer.close();
                stream.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

Como converter byte[] para mp3


C++ enum by step

Question: I'm trying to write the equivalent of an enum in C++ going in steps of eight instead of one, like
enum
{
    foo,
    bar = 8,
    baz = 16,
};

There will be a lot of entries, new ones will be added at intervals, and for clarity they really want to be written in an order other than order of entry, so it would be nice not to have to keep updating all the numbers by hand. I've tried mucking around with macro preprocessor tricks, but no dice so far. Is there a way to do this that I'm overlooking?
Answer: #define NEXT_ENUM_MEMBER(NAME) \
    NAME##_dummy, \
    NAME = NAME##_dummy - 1 + 8

enum MyEnum {
   Foo,
   NEXT_ENUM_MEMBER(Bar),
   NEXT_ENUM_MEMBER(Baz),
   ...
};

Answer: I prefer something like this:
enum {
    foo = (0 << 3),
    bar = (1 << 3),
    baz = (2 << 3),
};

It's not automated, but it doesn't require much thinking when adding a new enum constant.
Answer: I'm not quite sure what you're asking, but this approach will autogenerate values at steps of 8, and make it relatively easy to insert new values in the middle of the enum and have all the following values update to accomodate the change:

  enum
  {
     foo,
     bar = foo + 8,
     baz = bar + 8
  }

After editing to add a "newvalue" you would have:

  enum
  {
     foo,
     bar = foo + 8,
     newvalue = bar + 8,
     baz = newvalue + 8
  }

You could also use a "Step" constant so that you can (a) change your mind about the step later, and (b) stop anyone accidentally adding the wrong step:

  const int EnumStep = 8;

  enum
  {
     foo,
     bar = foo + EnumStep,
     baz = bar + EnumStep
  }
Answer: You could do the following:
#define STEP 8
#define ADD_ENUM(X, M) X = STEP * (M)
enum {
    ADD_ENUM(Foo, 0),
    ADD_ENUM(Bar, 1),
    ADD_ENUM(Baz, 2),
    //.. and so on
};

Answer: #include <stdio.h>
enum{
  foo = 0,
  bar = foo+8,
  baz = bar+8,
};
int main(){
  printf("foo: %i  bar: %i  baz: %i\n", foo, bar, baz );
}
Answer: Another approach which requires a separete header file for the enum, eg entries.h:
enum
{
    ENTRY(foo),
    ENTRY(bar),
    ENTRY(baz)
};

Then, you could use the following
#define ENTRY(E) _ ## E
#include "entries.h"
#undef ENTRY
#define ENTRY(E) E = _ ## E * 8
#include "entries.h"
#undef ENTRY

to generate
enum
{
    _foo,
    _bar,
    _baz
};

enum
{
   foo = _foo * 8,
   bar = _bar * 8,
   baz = _baz * 8
};

Answer: #define ENUM_ENTRY_PASTE( x )   x

#define ENUM_ENTRY_8_SPACING( x ) \
    x,      \
    ENUM_ENTRY_PASTE(x)2,   \
    ENUM_ENTRY_PASTE(x)3,   \
    ENUM_ENTRY_PASTE(x)4,   \
    ENUM_ENTRY_PASTE(x)5,   \
    ENUM_ENTRY_PASTE(x)6,   \
    ENUM_ENTRY_PASTE(x)7,   \
    ENUM_ENTRY_PASTE(x)8

Its not ideal as you'll have all those extras but you'll have your enums in the right place.
Answer: Yet another possibility that avoids using (MISRA-prohibited) ## operators or creating extra enums/entries would be to do:
#define USE_MYENUM(value) (value*8)

enum
{
    foo = 0,
    bar,
    baz
} MyEnum = bar;

CallFunction(USE_MYENUM(MyEnum));

Of course, it would probably be preferable to put USE_MYENUM (or more likely MyEnum*8) into CallFunction() so that the type checking is done.
Answer: Following Pavel Minaev solution and some other ideas from the answers, you could do something like this
#define BASE 0
#define STEP 8
#define NEXT_ENUM_MEMBER(NAME,PREVIOUS) \
    NAME = PREVIOUS + STEP

enum MyEnum {
   Foo = BASE,
   NEXT_ENUM_MEMBER(Bar, Foo),
   NEXT_ENUM_MEMBER(Baz, Bar)
};

Pros

Small memory footprint.
You can easily change the step and the base without update each entry.
You can easily delete and add an entry anywhere.

Cons

You must write the previous entry.
Answer: int foo, bar, baz;

baz =
8 + bar =
8 + foo;

Comment: They won't be constant expressions then.
Comment: That's the idea:  he wants to be able to "slip" one in any time he wants and have it fit into the +8 scheme.

C++ enum by step


cloning/copying a dojo data store

Question: Hi can some one please tell me how to copy one data store to another in dojo. I tried it in following way but it doesn't work. Here I'm try to copy data from jsonStore to newGridStore.
jsonStore.fetch({query:{} , onComplete: onComplete});

var onComplete = function (items, request) {
    newGridStore = null;
    newGridStore =  new dojo.data.ItemFileWriteStore({
        data : {}
    });
    if (items && items.length > 0) {
    var i;
    for (i = 0; i < items.length; i++) {
        var item = items[i];
        var attributes = jsonStore.getAttributes(item);
        if (attributes && attributes.length > 0) {
            var j;
            for (j = 0; j < attributes.length; j++) {
                var newItem = {};
                var values = jsonStore.getValues(item, attributes[j]);
                if (values) {
                    if (values.length > 1) {
                    // Create a copy.
                    newItem[attributes[j]] = values.slice(0, values.length);
                    } else {
                        newItem[attributes[j]] = values[0];
                    }
                }
            }
        newGridStore.newItem(newItem);
        }
        }
    }
}

Comment: Before you do it that way. Can you explain why you are trying to do this.
Comment: Becouse After updating the jsonStore with new values, I need to compare those with old values
Comment: Then you are doing it wrong. See my answer.
Answer: Based on the comments asked above. You are trying to copy values to a new Store for the single reason to be able to detect which values have changes and then save them individually, without having to send the entire store.
This approach is totally wrong.
Dojo has isDirty() and offers you the ability to revert() a store back to it's original values. It knows which values have changed and you don't need to do this.
Take a look at the bog standard IFWS here: http://docs.dojocampus.org/dojo/data/ItemFileWriteStore
Make sure you read everything from here: http://docs.dojocampus.org/dojo/data/ItemFileWriteStore#id8
What you want to do is create your own _saveCustom method which you will override your store with, and then when you save, you will be able to see which values have changed.
Click on the demo at the very bottom of the page. It shows you EXACTLY how do to it using _saveCustom

cloning/copying a dojo data store


How to join two tables with dates conditions with SQL and Python?

Question: I have a SQL db, with two tables called users and user_activities (see below). I´m trying to get a dataframe from a query with the id_user and the number of sessions that a user makes after the second day of its registration. To identify a session, we have the activity "session" in the table user_activities.
For that, I need to combine two tables. The first one, users, provides the user_id and the registration_date:
users table:

user_id
registration_date

1
2021-01-10 04:37:14

1
2021-01-10 10:37:24

2
2021-01-10 20:37:44

3
2021-01-10 20:10:14

2
2021-01-10 10:37:04

The other one, user_activities, tracks all the activities that each user makes:
user_activities table

user
activity
date

1
session
2021-01-10 04:37:14

1
mainPage
2021-01-10 10:37:24

2
session
2021-01-10 20:37:44

3
session
2021-01-10 20:10:14

4
session
2021-01-11 00:02:04

2
session
2021-01-12 00:03:04

4
session
2021-01-13 00:31:04

5
session
2021-01-14 20:23:04

2
session
2021-01-15 10:36:52

2
mainPage
2021-01-15 10:37:04

What I am trying to get
I would like to get a df with the user_id and the number of sessions made after the second day of their registration. Only the users with more than 0 sessions would be included in that df. It would be as follows:

user_id
n_sessions

2
2

4
1

5
1

To get the number of sessions per user, I made before:
import mysql.connector
import pandas as pd

mydb = mysql.connector.connect(host="localhost", user="root", password="", database="users")
mycursor = mydb.cursor()

#sesiones por usuario
mycursor.execute("SELECT user_id, COUNT(*) FROM user_activities WHERE name = 'session' GROUP BY user_id;")
sessions_per_user = pd.DataFrame(mycursor, columns=['user_id','n_sessions'])

But I don´t know how to join with the registration_date condition. Does anyone know how to do it?
Answer: This is a join and group by.  Something like this:
SELECT u.user_id, COUNT(*)
FROM users u JOIN
     user_activities ua
     on ua.user = u.user_id
WHERE ua.name = 'session' AND
      ua.date > u.registration_date + interval 1 day
GROUP BY u.user_id;

I'm not sure exactly what you mean by "the number of sessions made after the second day of their registration."  This interprets that as "at least 24 hours after the registration".  The logic can be tweaked for other definitions.
Based on your comment, you want:
      ua.date > date(u.registration_date) + interval 2 day

Comment: yeah...  "the number of sessions made after the second day of their registration." means that if someone was registered on the 10th, I want to see the number of sessions after the 12th. I think your solution can already do it with the interval day at 2 instead of 1


Python - Looping through a list of strings to form new strings

Question: Say I have a list of strings like:
list_str = ["Blah1", "Blah2", "Blah3", "Bad", "Yada1", "Yada2", "Yada3"]

How do I loop through this list such that, when I encounter "Bad", I can put all the strings before "Bad" into a separate string Blah and all the strings after "Bad" will be in another string called Yada
Essentially, when I print Blah I should get:
Blah1
Blah2
Blah3

Each blah has to be separated by a newline from the previous one.
Comment: The [official tutorial](https://docs.python.org/3.4/tutorial/) is quite good.
Comment: What. Have. You. Tried. ?.
Comment: @septi: http://i.imgur.com/J5M9HeW.png
Comment: New to python, dunno where to start! Any hint?
Answer: list_str_blah = list_str[:list_str.index("Bad")]
list_str_yadda = list_str[list_str.index("Bad")+1:]

is probably the easiest way ... although you could always just call index once and save it
bad_idx = list_str.index("Bad")

Comment: Do the ":" and "+1:" indicate ranges of some sort?
Comment: of coarse they do ... it is standard python slicing
Answer: You can use itertools.takewhile, which is set up to do just this.
>>> import itertools
>>> list(itertools.takewhile(lambda i: i != "Bad", list_str))
['Blah1', 'Blah2', 'Blah3']

To get both before and after the "Bad"
>>> iter_str = iter(list_str)
>>> front, back = list(itertools.takewhile(lambda i: i != "Bad", iter_str)), list(iter_str)
>>> front
['Blah1', 'Blah2', 'Blah3']
>>> back
['Yada1', 'Yada2', 'Yada3']

Comment: +1 since i guess he never said that he needed anything after bad ... but by using an `iter(list_str)` you could easily get everything after aswell
Comment: hehe I already gave you a +1 but you could do `iter(lambda:next(iter_str),"Bad"),list(iter_str)` just for fun :P
Answer: Not that I would actually recommend programming this way, but...
>>> SEP='---'
>>> [x.split(SEP) for x in SEP.join(list_str).partition(SEP + "Bad" + SEP)][0]
['Blah1', 'Blah2', 'Blah3']

Comment: (This is what happens when you start trying to learn Haskell in earnest...)
Comment: aweful ... but awesome at the same time
Comment: That's why I factored it out; it would have to be chosen--if possible--as something that *isn't* in the list. In practice, there's probably *something* that's safe to use (pick one of the more obscure ASCII control characters if it's a list of otherwise readable text).
Comment: Lol! We can have a list of unknown separators!  Then test each of them :P But hey, haskell is a good language then!


django + apache2, each user takes up a lot of memory

Question: I'm running my django app on apache2 (with mpm prefork) with a mysql database. It works fine, except when multiple users use it, and it takes around 40-45m RES for each user.
httpd.conf:
WSGIScriptAlias / /home/myproject/apache/django.wsgi
Alias /blog /var/www/blog/
Alias /maintain_wp_database/phpmyadmin_secure /usr/share/phpmyadmin

django_wsgi:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

path2 = '/home'
if path2 not in sys.path:
sys.path.append(path2)

path = '/home/myproject'
if path not in sys.path:
sys.path.append(path)

config file:
<VirtualHost *:80>
ServerName myproject.com
    ServerAlias www.myproject.com
    #
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.myproject\.com
    RewriteRule (.*) http://myproject.com$1 [R=301,L]
    #
        DocumentRoot /home

    WSGIScriptAlias / /home/myproject/apache/django.wsgi

    <Directory /home/myproject/static>
    Order deny,allow
    Allow from all
    </Directory>
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    Alias /static/ /home/myproject//static/
    Alias /robots.txt /home/myproject/templates/robots.txt
    Alias /favicon.ico /home/myproject/static/images/favicon.ico

    AliasMatch ^/([^/]*\.css) /home/myproject/styles/$1

    Alias /media/ /home/myproject/media/

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    <Directory /home/myproject/apache>
    Order allow,deny
    Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

</VirtualHost>

apache2.conf:
KeepAlive On
Timeout 300
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule mpm_prefork_module>
    StartServers        5
    MinSpareServers     5
    MaxSpareServers     10
    MaxClients          150
    MaxRequestsPerChild 0

output of top:
top - 11:24:10 up 3 days,  1:08,  3 users,  load average: 0.00, 0.01, 0.05
Tasks: 140 total,   1 running, 134 sleeping,   5 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1011248k total,   491532k used,   519716k free,    11380k buffers
Swap:   262140k total,   119884k used,   142256k free,   127244k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 7678 www-data  20   0  228m  42m 6812 S    0  4.3   0:01.16 apache2
 7665 www-data  20   0  232m  41m 4956 S    0  4.2   0:00.75 apache2
 7676 www-data  20   0  212m  40m 4628 S    0  4.1   0:00.27 apache2
 7956 www-data  20   0  230m  39m 4416 S    0  4.0   0:00.65 apache2
 7677 www-data  20   0  230m  39m 4452 S    0  4.0   0:00.69 apache2
 7667 www-data  20   0  224m  36m 4836 S    0  3.7   0:00.48 apache2
 2539 mysql     20   0  321m  28m 2584 S    0  2.9   0:12.32 mysqld
 7661 root      20   0  181m 9772 4588 S    0  1.0   0:00.12 apache2
 7724 www-data  20   0  183m 9412 2228 S    0  0.9   0:00.01 apache2
 7666 www-data  20   0  183m 9360 2240 S    0  0.9   0:00.01 apache2
 7668 www-data  20   0  183m 9108 2108 S    0  0.9   0:00.00 apache2
 7981 www-data  20   0  183m 8596 1768 S    0  0.9   0:00.00 apache2
 4645 root      20   0 71120 3580 2532 S    0  0.4   0:00.07 sshd
 7993 root      20   0 70664 3296 2556 S    0  0.3   0:00.02 sshd

Do I need to optimize my config settings or is this a memory leak issue (something wrong in the code maybe?). Because of each user taking 40-45m (is this normal??) in apache, most of the memory in my 1gb linode gets consumed pretty fast. Any help on this would be great! Thanks
Answer: Try lowering the MaxSpareServers down to around 5, and MinSpareServers to 1. This will reduce the number of apache processes. ~40 MB is normal in Apache. This configuration is optimal for a low memory server:
StartServers 1
MinSpareServers 1
MaxSpareServers 5
ServerLimit 50
MaxClients 50
MaxRequestsPerChild 5000
Comment: Thanks for the response. I have a 1gb ram server. If I do free -m, I get free memory to be around 600mb, so shouldn't the MaxClients be equal to 15? And on a different note, should I use prefork or worker?
Comment: prefork is the preferred method. The setup I gave varies on your environment, but don't set the MaxClients too low or you'll run out of workers.
Answer: Read:

http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html

and watch:

http://lanyrd.com/2013/pycon/scdyzk/
http://lanyrd.com/2012/pycon/spcdg/

In short. Don't use mod_wsgi in embedded mode with Apache if you are using prefork MPM unless you know how to configure Apache properly for Python web applications.
Comment: Thanks, I already changed to worker and using mod_wsgi in the daemon mode. These are my daemon settings - WSGIDaemonProcess abc.com processes=2 threads=3 stack-size=1048576 maximum-requests=5000 inactivity-timeout=900 display-name=%{GROUP} WSGIProcessGroup abc.com worker settings - StartServers 2 ServerLimit 12 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 25 ThreadsPerChild 25 MaxClients 300 MaxRequestsPerChild 0 ThreadStackSize 1048576

every apache instance takes up 40-50 mb, how to reduce this?
Comment: Using 40-50 MB for a Django process isn't particularly abnormal. Having addressed Apache configuration issues to ensure you don't have more copies than you need, then you need to work on what your code itself is doing.


In Python, what is the easiest way to add a list consisting of keyword pairs to a dictionary?

Question: I have a homework problem in Python.
I am using Python version 3.4.0 on Linux.
The design document states that I am to read a CSV file using built in functions, specified as names.dat, that is in the format:
name:name2, name:name3, name2:name4, name3:name5\n (etc)

I am then to add these keyword pairs to a dictionary, which is the part I'm stuck on.
The code I have thus far is this:
dictionary = dict()
database = open('names.dat', 'r')
data = database.read()
data = data.rstrip('\n')
data = data.split(',')
for item in range(len(data)):
    dictionary.update(data[item-1])

My thinking being that if I have a list element in the format "name:name2", and I call the dictionary update function with that element as an argument, it will properly map to a keyword pair in the dictionary.
However, this is not the case, as I get this error when I run this script:
File "MyName.py", line 7, in <module>
    dictionary.update(data[item-1])
ValueError: dictionary update sequence element #0 has length 1; 2 is required

This and This seem similar, but I feel that this is enough of a different question to warrant a separate response.
What am I doing wrong here, and how can I fix it?
Is there a simpler way to do this?
Answer: @Paulo Scardine has a great answer if you want to create an exact dataset from the given csv. If you want to combine the values based on the key one could use this:
changes = {}
with open('test.csv', 'r') as f:
    for row in f:
        for e in row.rstrip('\n').split(", ") : #split lines by column
            print (e) #just to show what is being generated here
            (k,v) = e.split(":") #split further into key, value pairs
            changes.setdefault(k, []).append(v)
            #creates empty list if new key, adds value to list

print (changes)

Data will look like:
{'name3': ['name5'], 'name2': ['name4', 'name6', 'name5'], 'name1': ['name', 'name4'], 'name': ['name2', 'name3']}

This could be further simplified but I think this gives the good example that someone learning can follow.
Edit: added setdefault method following @Paulo Scardine comment
Comment: This idiom is so common in Python that there is some syntax sugar for it: see `dict.setdefault` and `collections.defaultdict`.
Comment: Neat, I've been moving from R to Python for my AI class and those methods might save me some headaches along the road
Answer: Try this:
data = []
with open('names.dat') as database:
    for line in database:
        if line.strip():  # skip blank lines
            data.append(
                dict(i.split(":") for i in line.rstrip('\n').split(","))
            )

If your file is:
name:name2,name:name3,name2:name4,name3:name5
name:name2,name:name3,name2:name4,name3:name5
name:name2,name:name3,name2:name4,name3:name5
name:name2,name:name3,name2:name4,name3:name5

data will be:
[{'name': 'name3', 'name2': 'name4', 'name3': 'name5'},
 {'name': 'name3', 'name2': 'name4', 'name3': 'name5'},
 {'name': 'name3', 'name2': 'name4', 'name3': 'name5'},
 {'name': 'name3', 'name2': 'name4', 'name3': 'name5'}]

Perhaps you want a dict of list instead of a list of dict:
data = {}
with open('names.dat') as database:
    for line in database:
        if line.strip():  # skip blank lines
            for k, v in (i.split(":") for i in line.rstrip('\n').split(",")):
                data.setdefault(k, []).append(v)

Resulting:
{'name': [ 'name2', 'name3', 'name2', 'name3', 'name2', 'name3', 'name2', 'name3'],
 'name2': ['name4', 'name4', 'name4', 'name4'],
 'name3': ['name5', 'name5', 'name5', 'name5']}


Remotely click Power button for equipments after blackout

Question: I am wondering on what improvements to make to our server room, as to we won't be in the office for quite a while because of COVID.
I want something like an auto clicker (physical device) that I can remotely access so that I can click the power button of a mac mini, a router, a switch if possible.
Any recommendations?
Comment: People usually attach devices, that don't come with an out-of-band management module to control them, to a managed power switch/pdu . You then remotely control the  electrical socket their power cable is attached to. That only requires that the  devices attached either have physical switch that stays in a "power on" setting , or bios support to automatically power on after a "power failure"
Comment: Do you have any brand or specific power manager? Is it like smart outlet or something?
Comment: APC managed (rack  mount) PDU's are one brand I'm familiar with
Comment: Wake on LAN (WoL) is a thing.
Answer: First of all, routers, switches, etc, should simply be turned on. If power gets cut and then restored for some reason, they'll automatically power up and proceed to work as usual after their regular boot sequence.
On the Mac, under System Preferences/Energy Saver, find the option Start up automatically after a power failure which will do exactly what it says on the tin.

Of course if you expect power to be unreliable in your office, it's good for your equipment to purchase one or more Uninterruptible Power Supplies and connect them in front of switches and servers. A good one will level out power surges and let the equipment handle short power interruptions. I don't know how it works in macOS, but it's usually possible to configure connected servers to shut down in a controlled manner once the battery level in the UPS reaches a certain point, and then turn back on once power has been back for a number of seconds (basically using the aforementioned Start up automatically after a power failure setting in the server's firmware or operating system).
Comment: Yes, I have that already figured out. The problem is the cheap modem/router that comes with the ISPs service. Do you think smart outlets or sockets will do?
Comment: I really wouldn't let my income be dependent on janky gear that's clearly not intended for business use - or on "smart" outlets at the state of reliability they are in 2020. How will you send commands to them if they have no Internet connection? Do yourself and your business a favor and weigh the cost of downtime against the cost of appropriate equipment.


Problem with changing the header background with page scroll

Question: I am implementing a fixed transparent header which turns black when the page is scrolled more than 100 pixels.
Currently, in main.js I have a function:
// Header scroll class
   $(window).scroll(function() {
    if ($(this).scrollTop() > 100) {
     $('.header').addClass('header-scrolled');
    } else {
      $('.header').removeClass('header-scrolled');
    }
  });

in main.css I have:
/*-------
# Header
---------*/
#header {
  padding: 3px 0;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  transition: all 0.5s;
  z-index: 997;
  background-color: transparent;
}

.header-scrolled {
  background-color: black;
}

and the html page is formatted as follows:

{% trans "Home" %}

{% trans "Contacts"
           %}

Currently, the header is fixed and transparent. Yet, when I scroll down, it doesn't turn black (although in the inspector it seems that the header-scrolled is being added).
I cannot seem to find any solution to the problem (none of the provided other suggestion of similar questions work)... Perhaps I am missing something?
Comment: You are missing how _CSS specificity_ works, so please go read up on that.
Comment: You need to check your markup. I don't see an ending tag for ``
Comment: And how is this a django question exactly ? (tag removed)
Answer: Hope it will help.
#header.header-scrolled {
  background-color: black;
}

Answer: I will post the jQuery solution here as separate answer for better reading, please try the following code for your index.js
index.js
$(window).scroll(() => {
 if (this.pageYOffset >= 100) {
    $('#navigation').addClass('scrolled');
  }

  if (this.pageYOffset < 100) {
    $('#navigation').removeClass('scrolled');
  }
})

just for the sake of completion also the html & css
index.html
<!DOCTYPE html>

  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./style/main.css">
  <title>Scroll CSS</title>

Navigation

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

main.scss
(if you want i can also post the resulting css)
body {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#navigation {
  position: fixed;
  height: 100px;
  width: 100%;
  background: transparent;
  text-align: center;

  transition: all 0.2s;

  &.scrolled {
    color: white;
    background: black;
  }
}

p {
  &:nth-of-type(1) {
    margin-top: 100px;
  }
  width: 30vw;
}

Comment: The same. Nothing changes. It seems that the problem is with me somewhere. Nevertheless, I am voting your answer up, as I am confident that it usually works and others can use it.
Comment: Do you mean the whole code? I am using a bootstrap theme and the whole code is structured in multiple files most of which are more than 1000 lines...
Comment: oh okay. Then I think there is something wrong in the css.

In which order do you load your css?
Comment: Order:

.header-scrolled is in main.css, loaded last.
Comment: did you try to track with the dev-tools of your browser, where the

```css
 #header.header-scrolled {
box-shadow: none;
}
```
style is coming from?
Comment: or just diable all .*css files and enable them one after another to find the causing .*css file
Comment: It must be in main.css ... I disabled all of the other css files and the problem stayed. After removing all 'box-shadow: none;' elements from the main.css file, the same thing remained. This is getting really mysterious for me.
Comment: hmmm okay, that`s strange. Maybe as a next step, disable all rules/selectors in your main.css and enable on after another.
Comment: Okay, I did that (disabling all selectors in main.css), nothing changed. So, it is not from the main.css file itself. More interestingly, when I disabled the header class, nothing changed as well. I think that now there is no doubt that it is a specificity of the way bootstrap works. There is a folder named "scss". In it there are various scss files (including files such as main.scss and _header.scss). Unfortunately, nothing that I do to those files makes a difference as well.
Answer: I have build a plain JavaScript solution. Hopefully you can use it.
index.html
<!DOCTYPE html>

  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./style/main.css">
  <title>Scroll CSS</title>

Navigation

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga tempore obcaecati voluptas earum alias enim, inventore eaque minus possimus delectus maxime soluta provident veritatis totam aspernatur cupiditate explicabo numquam aperiam eum vel? Perferendis, culpa laudantium? Ut, esse voluptates animi aut, beatae error blanditiis nisi voluptas id illo, consequatur accusantium nihil.

main.scss
(if you want i can also post the resulting css)
body {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#navigation {
  position: fixed;
  height: 100px;
  width: 100%;
  background: transparent;
  text-align: center;

  transition: all 0.5s;

  &.scrolled {
    color: white;
    background: black;
  }
}

p {
  &:nth-of-type(1) {
    margin-top: 100px;
  }
  width: 50vw;
}

index.js
window.addEventListener('scroll', () => {
  const navBar = document.querySelector('#navigation')
  const { pageYOffset } = window;

  if (pageYOffset >= 100) {
    navBar.classList.add('scrolled');
  }

  if (pageYOffset < 100) {
    navBar.classList.remove('scrolled');
  }
})

Comment: I tried incorporating this in my case, but it still has no effect.
I ques that it really is CSS specificity connected (something is getting overwritten in my css files) because in the inspector when I hover over the scrolled heather it shows:
 #header.header-scrolled {
    box-shadow: none;
}
Clearly don't have the box-shadow element in what I have defined. In addition, the background color is missing.
When I add it manually there:

#header.header-scrolled {
    box-shadow: none;
    background-color: black;
}
 it works as it should/is expected.
Comment: When I have some time tonight I will try out this stuff with jQuerry.