Extjs access one view from another

Question: In Extjs, I am trying to access some data set on one view, from another view. Both the views are rendered on the same page. I created a small app in sencha fiddle that shows what I am trying to do. When you click on the second textbox, it should show data from the first textbox.
I want to pull it up using its reference value
Ext.application({
    name: 'Fiddle',

    launch: function() {

        Ext.create('Ext.panel.Panel', {
            title: 'Hello',
            reference: 'HelloPanel',
            width: 200,
            html: '<input type="text">',
            renderTo: Ext.getBody()
        });

        Ext.create('Ext.panel.Panel', {
            title: 'Hello2',
            reference: 'fooGrid',
            width: 200,
            html: '<input type="text">',
            renderTo: Ext.getBody(),
            listeners: {
                click: {
                    element: 'el',
                    fn: function() {
                        alert('Show data from HelloPanel1');
                    }
                }
            }

        });

    }
});

Comment: If it's for a form consider you could use the record to update the value of the textbox. Out of the box it should bind the new value to the textbox. Or use a viewmodel, but I don't know which version you are using.
Answer: Look at this. Just made some changes:
Since there is only View presented you need to define referenceHolder.
Below example is too trivial to show power of reference. For that you need to make ViewController. Bind presented view to controller with controller config. Then in controller you can do something like this this.lookupReference('superAwesomeTextbox');. And now you have hook to that component in controller. It should be noted that referenceHolder is not required for ViewController. Simple as it is.
Ext.application({
    name: 'Fiddle',

    launch: function() {

        var panel1 = Ext.create('Ext.panel.Panel', {
            title: 'Hello',
            referenceHolder: true,
            reference: 'HelloPanel',
            width: 300,
            items:[{
                xtype: 'textfield',
                fieldLabel: 'textbox',
                reference: 'superAwesomeTextbox'
            }],
            renderTo: Ext.getBody()
        });

        var panel2 = Ext.create('Ext.panel.Panel', {
            title: 'Hello2',
            reference: 'fooGrid',
            referenceHolder: true,
            width: 300,
            items:[{
                xtype: 'textfield',
                fieldLabel: 'textbox',
                reference: 'superAwesomeTextbox2',
                listeners: {
                    focus: function(comp){
                        var textbox = panel1.lookupReference('superAwesomeTextbox');
                        comp.setValue(textbox.getValue());
                    }
                }
            }],
            renderTo: Ext.getBody(),
        });

    }
});

Extjs access one view from another


docker: Error response from daemon: could not select device driver "" with capabilities: [[gpu]]

Question: Despite having 4 GPUs each with ~20GB vRAM, docker is not able to run with the following command. How can I solve this?
[20:08:28] jalal@echo:~/research/code$ docker run --shm-size 2GB -it --gpus all docurdt/heal
docker: Error response from daemon: could not select device driver "" with capabilities: [[gpu]].
ERRO[0000] error waiting for container: context canceled

[20:08:20] jalal@echo:~/research/code$ nvidia-smi
Fri Apr  1 20:08:28 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 510.47.03    Driver Version: 510.47.03    CUDA Version: 11.6     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  Off  | 00000000:01:00.0 Off |                  N/A |
| 31%   41C    P8    23W / 350W |    301MiB / 24576MiB |      1%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   1  NVIDIA GeForce ...  Off  | 00000000:21:00.0 Off |                  N/A |
| 30%   39C    P8    18W / 350W |     14MiB / 24576MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   2  NVIDIA GeForce ...  Off  | 00000000:4A:00.0 Off |                  N/A |
| 30%   32C    P8    23W / 350W |     14MiB / 24576MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   3  NVIDIA GeForce ...  Off  | 00000000:4B:00.0 Off |                  N/A |
| 30%   40C    P8    18W / 350W |     14MiB / 24576MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

I also have:
$ uname -a
Linux echo 5.4.0-99-generic #112-Ubuntu SMP Thu Feb 3 13:50:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ lsb_release -a
LSB Version:    core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    20.04
Codename:   focal

$ docker -v
Docker version 20.10.7, build 20.10.7-0ubuntu5~20.04.2

Also,
$ df -h | grep /dev/shm
tmpfs                                126G  199M  126G   1% /dev/shm

and

and
$  cat /boot/config-$(uname -r) | grep -i seccomp
CONFIG_SECCOMP=y
CONFIG_HAVE_ARCH_SECCOMP_FILTER=y
CONFIG_SECCOMP_FILTER=y

and
[20:33:17] (dpcc) jalal@echo:~$ lspci -vv | grep -i nvidia
01:00.0 VGA compatible controller: NVIDIA Corporation Device 2204 (rev a1) (prog-if 00 [VGA controller])
    Kernel driver in use: nvidia
    Kernel modules: nvidiafb, nouveau, nvidia_drm, nvidia
01:00.1 Audio device: NVIDIA Corporation Device 1aef (rev a1)
21:00.0 VGA compatible controller: NVIDIA Corporation Device 2204 (rev a1) (prog-if 00 [VGA controller])
    Kernel driver in use: nvidia
    Kernel modules: nvidiafb, nouveau, nvidia_drm, nvidia
21:00.1 Audio device: NVIDIA Corporation Device 1aef (rev a1)
4a:00.0 VGA compatible controller: NVIDIA Corporation Device 2204 (rev a1) (prog-if 00 [VGA controller])
    Kernel driver in use: nvidia
    Kernel modules: nvidiafb, nouveau, nvidia_drm, nvidia
4a:00.1 Audio device: NVIDIA Corporation Device 1aef (rev a1)
4b:00.0 VGA compatible controller: NVIDIA Corporation Device 2204 (rev a1) (prog-if 00 [VGA controller])
    Kernel driver in use: nvidia
    Kernel modules: nvidiafb, nouveau, nvidia_drm, nvidia
4b:00.1 Audio device: NVIDIA Corporation Device 1aef (rev a1)

Answer:
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)       && curl -s -L https://nvidia.github.io/libnvidia-container/gpgkey | sudo apt-key add -       && curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

$ sudo apt-get update

$ sudo apt-get install -y nvidia-docker2

$ sudo systemctl restart docker

$ docker run --shm-size 2GB -it --gpus all docurdt/heal (base) root@9f66ed7b7c1b:/Workspace#

Thanks a lot grym for link.
Comment: All the steps mentioned above worked for me except the **step-1**. Please refer to this for more details about step 1: https://nvidia.github.io/nvidia-docker/.
Also, you might need to do `sudo apt-get install -y nvidia-container-toolkit` for **step-3**.
Answer: the following three commands solved my problem
sudo apt install -y nvidia-docker2
sudo systemctl daemon-reload
sudo systemctl restart docker

here is the source
https://forums.developer.nvidia.com/t/could-not-select-device-driver-with-capabilities-gpu/80200


Measuring OpenCL kernel's memory throughput

Question: I read about global memory optimization in OpenCL. In one of the slide-shows, a very simple kernel (below) has been used to demonstrate the importance of memory coalescing.
__kernel void measure(__global float* idata, __global float* odata, int   offset) {

    int xid = get_global_id(0) + offset;
    odata[xid] = idata[xid];

}

Please see my code below which measures the running time of the kernel
    ret = clFinish(command_queue);
    size_t local_item_size = MAX_THREADS;

    size_t global_item_size = INPUTSIZE;
    struct timeval t0,t1;
    gettimeofday(&t0, 0 );

    //ret = clFinish(command_queue);
    ret = clEnqueueNDRangeKernel(command_queue, measure, 1, NULL,
                                            &global_item_size, &local_item_size, 0, NULL, NULL);

    ret = clFlush(command_queue);
    ret = clFinish(command_queue);

    gettimeofday(&t1,0);
    double elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + (t1.tv_usec-t0.tv_usec);

    printf("time taken = %lf microseconds\n", elapsed);

I transfer around 0.5 GB of data:
#define INPUTSIZE 1024 * 1024 * 128
int main (int argc, char *argv[])
{

   int offset = atoi(argv[1]);
   float* input = (float*) malloc(sizeof(float) * INPUTSIZE);

Now, the results are a bit random. With offset =0, I get times as low as 21 usecs. With offset = 1, I get times ranging between 53 usecs to 24400 usecs.
Can someone please tell me what is going on. I thought that offset=0 will be the fastest, because all the threads will access consecutive locations, hence the minimum number of memory transactions will take place.
Comment: Are you checking the error codes from the `clEnqueueNDRangeKernel` and `clFinish` functions? 21 microseconds is far too low for a 500MB transfer, so perhaps the function is actually just failing to execute properly.
Answer: Bandwidth is a measure of how fast data can be transferred, and is typically measured in bytes/second in these situations (usually GB/s for GPU memory bandwidth).
To compute the bandwidth of a compute kernel, you just need to know how much data the kernel reads/writes from/to memory, and then divide that by the time your kernel took to execute.
Your example kernel has each work-item (or CUDA thread) read a single float, and write a single float. If you launch this kernel to copy 2^10 floats, then you will be reading 2^10 * sizeof(float) bytes, and writing the same amount (so 8MB in total). If this kernel takes 1ms to execute, then you have achieved bandwidth of 8MB / 0.001s = 8GB/s.

Your new code snippet that shows your kernel timing approach indicates that you are only timing the kernel enqueue, not the amount of time it actually takes to run the kernel. This is why you are getting very low kernel timings (0.5GB / 0.007ms ~= 71TB/s!). You should add calls to clFinish() to obtain proper timing. I typically also take timings over several runs, to allow the device to warm-up, which usually gives more consistent timing:
// Warm-up run (not timed)
clEnqueueNDRangeKernel(command_queue, ...);
clFinish(command_queue);

// start timing
start = ...

for (int i = 0; i < NUM_RUNS; i++)
{
  clEnqueueNDRangeKernel(command_queue, ...);
}
clFinish(command_queue);

// stop timing
end = ...

// Compute time taken, bandwidth etc
average_time = (end-start)/NUM_RUNS;
...

Question from comment:

Why does offset=0 perform better than offset=1,4 or 6?

On NVIDIA GPUs, work-items are grouped into 'warps' of size 32, which execute in lockstep (other devices have similar approaches, just with a different sizes). Memory transactions are aligned to multiples of the cacheline size (e.g. 64 bytes, 128 bytes etc). Consider what happens when each work-item in a warp attempts to read a single 4-byte value (assuming they are contiguous, as per your example), with a cacheline size of 64 bytes.
This warp is reading a total of 128 bytes of data. If the start of this 128-byte chunk is aligned to a 64-byte boundary (i.e. if offset=0), then this can serviced in two 64-byte transactions. However, if this chunk is not aligned to the a 64-byte boundary (offset=1,4,6,etc), then this will require three memory transactions to fetch all of the data. This is where your performance difference comes from.
If you set the offset to be a multiple of the cacheline size (e.g. 64), then you will likely get performance equivalent to offset=0.
Comment: Ok, made the changes. Now, I am getting times in the range of 24483 microseconds and 27000 microseconds. There is no pattern to it. Also, some times I get an odd 31 microseconds.
Comment: @user1274878 You might need to warm-up the device first, and average timings over several runs to get more consistent results (updated answer).
Comment: I added the loop, but for offset = 0, I am still getting a large variation between the runs. My code, including the kernel is here, http://pastebin.com/YgwgAFsA. Can you please check
Comment: @user1274878 What device are you using? On my own Mac system with an NVIDIA GPU, your code fails to run properly, since the buffer size is too large (256MB is the maximum for me). If you check the error code from the `clEnqueueNDRangeKernel` calls, it may indicate if you are having the same problem.
Comment: can you tell me why certain offsets would do better than others. After all, the threads are accessing sequential addresses. For my card, offset = 0, is better than 1,4, and 6. It is not very clear why though.
Comment: @user1274878 This is due to alignment. I've updated my answer again with a more detailed answer to this question.


Insert query not inserting no error message

Question: I can't get an INSERT query to INSERT in db.  I am not getting any error message and was following a tutorial any help will be greatly appreciated.
$query = "INSERT INTO rooms (room_title,room_description,monthly_rate,prop_name,prop_description) VALUES ( ?, ?, ?, ?, ?)";

$stmt = mysqli_prepare($dbc,$query);
//$stmt = mysqli_query($dbc, $query);
if($stmt == false) {
  die("
".mysqli_error($dbc).PHP_EOL.$query."
");
}

mysqli_stmt_bind_param($stmt,"ssiss",$pn,$d,$p,$ppn,$ppd);
mysqli_stmt_execute($stmt);
//mysqli_stmt_close($stmt);
// Check the results...
if (mysqli_stmt_affected_rows($stmt) == 1)

  {

    echo'
The room has been added.
';
    // Clear $_POST:
    $_POST = array();

  }

mysqli_stmt_close($stmt);

} // End of $errors IF.
// End of the submission IF.

Because it does not echo "The room has been added"  I suspect the problem is with the mysqli_stmt_affected_rows($stmt) == 1
Comment: check the return of mysqli_stmt_execute
Comment: If `mysqli_stmt_execute` returns false, call `mysqli_error()` to get the error message.
Answer: try this
    if ($stmt = mysqli_prepare($dbc, "INSERT INTO rooms (room_title,room_description,monthly_rate,prop_name,prop_description) VALUES ( ?, ?, ?, ?, ?)")) {

 mysqli_stmt_bind_param("ssiss",$pn,$d,$p,$ppn,$ppd);

mysqli_stmt_execute($stmt);

} printf("Error: %s.\n", mysqli_stmt_error($stmt));


Importing Flask application to Elastic Beanstalk error: can't open file 'mod_wsgi': [Errno 2] No such file or directory

Question: I am attempting to put my Flask application on Elastic Beanstalk (Amazon Web Services, EC2), but after uploading the files I receive a 500 error. Any thoughts on what could be the issue? The error log:
/opt/python/run/venv/bin/python3: can't open file 'mod_wsgi': [Errno 2] No such file or directory
[Fri Sep 08 03:23:05.754341 2017] [:error] [pid 13619] [remote 220.127.116.11:31824] mod_wsgi (pid=13619): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Fri Sep 08 03:23:05.754577 2017] [:error] [pid 13619] [remote 126.96.36.199:31824] mod_wsgi (pid=13619): SystemExit exception raised by WSGI script '/opt/python/current/app/application.py' ignored.
[Fri Sep 08 03:23:05.754729 2017] [:error] [pid 13619] [remote 220.127.116.11:31824] Traceback (most recent call last):
[Fri Sep 08 03:23:05.754992 2017] [:error] [pid 13619] [remote 22.214.171.124:31824]   File "/opt/python/current/app/application.py", line 11, in <module>
[Fri Sep 08 03:23:05.755007 2017] [:error] [pid 13619] [remote 188.8.131.52:31824]     application.run(debug=True)
[Fri Sep 08 03:23:05.755116 2017] [:error] [pid 13619] [remote 22.214.171.124:31824]   File "/opt/python/run/venv/lib/python3.4/site-packages/flask/app.py", line 772, in run
[Fri Sep 08 03:23:05.755131 2017] [:error] [pid 13619] [remote 22.214.171.124:31824]     run_simple(host, port, self, **options)
[Fri Sep 08 03:23:05.755242 2017] [:error] [pid 13619] [remote 18.104.22.168:31824]   File "/opt/python/run/venv/lib/python3.4/site-packages/werkzeug/serving.py", line 737, in run_simple
[Fri Sep 08 03:23:05.755257 2017] [:error] [pid 13619] [remote 184.108.40.206:31824]     reloader_type)
[Fri Sep 08 03:23:05.755344 2017] [:error] [pid 13619] [remote 22.214.171.124:31824]   File "/opt/python/run/venv/lib/python3.4/site-packages/werkzeug/_reloader.py", line 265, in run_with_reloader
[Fri Sep 08 03:23:05.755358 2017] [:error] [pid 13619] [remote 126.96.36.199:31824]     sys.exit(reloader.restart_with_reloader())
[Fri Sep 08 03:23:05.755429 2017] [:error] [pid 13619] [remote 184.108.40.206:31824] SystemExit: 2

My file structure is as follows:
file hierarchy
My WSGI path is
config
Answer: Turns out I needed to wrap application.run() within a if __name__ == '__main__': block. This ensures that a local WSGI server is not loaded by default.
Source: http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/


didReadRSSI event stops working when entering background with background modes declared

Question: I am trying to develop an app that can discover and connect to devices while in the background, and when the connected peripheral enters a certain rssi range, i would like to send a message to the peripheral.
<code>func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) {
        // Get the current RSSI value
        let rssi = Int(truncating: RSSI)

        print("Current RSSI value: \(String(describing: rssi))")

        let positiveRssi = rssi * -1
        if (positiveRssi <= 50)
        {
            //here i send the message
        }

        // Call the readRSSI() method again to get updated RSSI values
        peripheral.readRSSI()
    }
</code>
when i am in the foreground it works perfectly but when i enter the background it gives me a few more answers and then suddenly stops.
i can see when it is working and when it stops working through the console.
i have also enabled the "Uses Bluetooth LE Accessories" background mode.
Comment: Apps don't continue to run once they are suspended without some sort of background mode. Receiving location updates, playing audio, receiving a CoreBluetooth notification etc.  You can't just keep polling rssi.
Answer: As @Paulw11 has already mentioned, mobile operating systems can limit the use of dedicated hardware and resources depending on the state of the application.
Therefore, it might be easier to use standardised Bluetooth LE profiles and services, such as:

Proximity Profile 1.0.1
Link Loss Service 1.0.1


Is it possible to play Minecraft with ocean water turned off?

Question: Is it possible to play Minecraft without any water in ocean biomes?
Comment: @Suunnn I'm afraid it's still not clear what you're looking for. Do you just not want squids?
Comment: Your title says "no water in ocean biomes", your question body says "no water spawns" which rather sounds like "no squids" as @RavenDreamer suggests - so, which one is it?
Comment: @Zommuter Occam's razor says that the title and body match because the OP means literally no spawning of water blocks.
Comment: I meant no water spawns in ocean biomes, ancient plateau style.  I don't care about squid.
Comment: It's not clear: are you aiming to eliminate ocean biomes entirely, or are you wanting ocean biomes to generate but be "dry" with no water (which I can imagine would be pretty cool, and very post-apocalypse feeling)?
Comment: I'd like them to still generate, yes.
Comment: @Joe I think Suunnn should clarify this, since the original question body can also be understood as "How to get rid of squids"
Comment: @SevenSidedDie +1 for bringing [Science](http://en.wikipedia.org/wiki/Occam%27s_razor) into it :-D
Comment: @Suunnn You're just going to keep confusing people talking like that. In minecraft "water spawns" mean *squid*. Only creatures "spawn" in minecraft; blocks are "generated".
Comment: Well then, water blocks in ocean biomes, and the removing thereof.
Answer: You can always install Singleplayer Commands, if that's still up-to-date, and use the <code>//drain</code> command with a huge radius.
Comment: This will also drain rivers, right? I don't think the OP is looking for that.
Comment: what drain command, be more clear, as I've never heard of this option and I'm sure I'm not the only one.
Comment: Is this really part of SPC? It is not mentioned [here](http://www.minecraftforum.net/topic/94310-125-single-player-commands-v322-official-download/#commandlist) - you need [WorldEdit](http://wiki.sk89q.com/wiki/WorldEdit/Utilities#Draining_pools), it's named `//drain`. (@Ender too FYI). Also note that **this will not empty the entire ocean**, and it might kill Minecraft if your radius is too large - either a lot of chunks get generated or only the existing ones are drained, I didn't check. Anyway, at some point there will be a wall of water waiting for you to trigger it's propagation...
Comment: @Zommuter it is part of WorldEdit which is bundled with SPC.
Comment: @Joe thanks for telling, that explains why WorldEdit commands are listed there :-7
Comment: @SevenSidedDie I think the OP left a drive-by question and will never clarify... `//drain` will eliminate any water blocks connected to the one below you within a certain radius. So it will dry nearby rivers connected to your ocean, but leave the ocean further away intact. It is a solution, but it involves too much manual work for my taste...
Comment: I must agree with @Zommuter, it's a solution, but only if a better one cannot be found.
Answer: edit To obtain "oceans" without water, I think Terrain Control offers what you're looking for, and much more:

There's both a bukkit and a SSP version, and even better, if it doesn't achieve what you want, the source is hosted at github, so you can simply modify it!

If you want to get rid of the water in oceans: Mystcraft allows you to create different worlds, maybe if you create one without water symbols you get what you want, you can e.g. use Desert or Frozen Ozeans/Rivers.
If you want to get rid of squids instead, the only thing I know about are the bukkit plugins NoSpawn and EntitySupressor, i.e. you'd have to run a bukkit server for yourself.
Answer: In vanilla Minecraft, there's no setting to generate the ocean dry.
There are flat worlds that don't have oceans, but I doubt that is what you are looking for.
I know of no mods that do this either.


Vimperator stops accepting input sometimes

Question: I have yet to figure out what I am doing (if anything) or what sites I'm visiting to cause this, but sometimes vimperator goes into a trance and stops listening for commands. It appears to still be running since the status bar is still there and updates the title when I change tabs. And it only seems to affect one window, not all of them.
How can I wake it up and get it to start working again? I'm not doing anything crazy like hitting ignore-all-keys (S-ESC) or ignore-key (i). The sure fire, but painful, way to fix this is to restart Firefox. A couple of times I've had some tabs that wanted me to save something before quitting, and if I clicked the Stay On Page button and didn't actually quit Firefox, that was somehow enough to get vimperator to start listening again.
I've seen this question asked a couple of times on #vimperator on Freenode, but have yet to see an answer.
EDIT: Another workaround that I've found recently is to attempt to close the problem window (but have the "Warn me when closing multiple tabs" options set) and then click "Cancel" when asked if you really want to close multiple tabs. I'm still looking for why vimperator gets into this situation and how to get out of it in the future.
Comment: Not sure if this is related but I seem to hit a similar issue anytime I go to a repository page on github (e.g. https://github.com/arc90/git-sweep). Do you observe that behavior too?
Comment: May be related to this issue if you're on Ubuntu: https://github.com/vimperator/vimperator-labs/issues/39
Answer: Apparently there is a bug for it in Vimperator bug tracker: https://github.com/vimperator/vimperator-labs/issues/39.
Right-clicking on the page works for me as a workaround.
Comment: Right clicking does work. Thanks a ton :) Saved me from the painful restart :)
Answer: I was having the same issue until i accidentally figured that it happens if i'm in input mode. If i press Insert it starts working again. Hope that helps
Comment: Thanks a lot, sometimes it's the simple things. For some reason my vimperator did not show that insert mode was active.


Standard deviation of the mean of sample data

Question: I can't quite understand what this formula means:
$$\sigma_{\overline{x}}=\frac{\sigma}{\sqrt n}$$
I know what standard deviation $\sigma$ is - it's the average distance of my data points (samples) from the mean. But this part is confusing:

For example, suppose the random variable $X$ records a randomly selected
  student's score on a national test, where the population distribution
  for the score is normal with mean $70$ and standard deviation $5$
  ($N(70,5)$). Given a simple random sample (SRS) of $200$ students, the
  distribution of the sample mean score   has mean $70$ and standard
  deviation $$\frac{5}{\sqrt{200}} \approx \frac{5}{14.14} \approx 0.35$$

Source
I thought the standard deviation $\sigma = 5$ means that if I take the scores of all students and calculate the mean, then the average distance of a score from that mean will be equal to $5$. The set of all scores is called the 'population', right? But here it says the more students' scores I take, the lower the standard deviation - thus the closer the number of samples gets to the size of population, the lower the standard deviation (and its get further from $5$).
Answer: First, the standard deviation is not the average distance to the mean, that is always zero. It is however, a value to measure how far the points are from the mean or not. Assuming the values are normally distributed, we know that ~68% of the values are between $\mu-\sigma$ and $\mu+\sigma$, for example.
Suppose we weigh potatoes with average weight 100 g and stadard deviation 5 g. What does hold for the average of the average weight of a group of 4 potatoes?
I hope you see that the average of the average weight is still 100 g. But what is the standard deviation of this average weight? That is where you use the formula
$$\sigma_{\bar{X}} = \frac{\sigma}{\sqrt{n}} = \frac{5}{\sqrt{4}} =2.5$$
Feel free to ask if you still don't understand.

Proof that the average distance between the actual data and the mean is $0$:
$$\frac{\sum^n_{i=1} (x_i-\mu)}{n} = \frac{(\sum^n_{i=1} x_i)-\mu n}{n} = \frac{\sum^n_{i=1} x_i}{n}-\mu =  \mu - \mu = 0$$
Comment: 'The standard deviation is the average distance between the actual data and the mean.' - quote from: https://controls.engin.umich.edu/wiki/index.php/Basic_statistics:_mean,_median,_average,_standard_deviation,_z-scores,_and_p-value
Comment: Then that site is wrong. Note that the average distance between the actual data and the mean is 0. I will write why this in my answer, because I'm getting sick of the fact that pressing enter in a comment saves the comment.
Answer: You are computing the standard deviation of the mean, $\sigma_{\bar X}$, not that of the individual samples, $\sigma_X$.
When the variables are independent, the variances do add up. So $$\text{var}_{\sum_i Xi}=n\text{var}_X,$$
and dividing by $n^2$ (the variance is quadratic),
$$\text{var}_{\bar X}=\frac1n\text{var}_X.$$
Hence taking the square root
$$\sigma_{\bar X}=\frac1{\sqrt n}\sigma_X.$$
Comment: It's true assuming the variances of $x_i$ are equal.
Comment: @user4205580: homoscedasticity is obvious from the problem statement, there is no need to trouble the OP.
Answer: Look carefully at the last sentence in the quote: in it 'standard deviation' refers to that of the sample mean. Thus one is essentially looking at all possible samples of 200 students, given that the population's standard deviation is 5. wythagoras' answer provides the formula for the sample mean's standard deviation.
Comment: How to understand 'standard deviation of the mean of sample data'? That we take all possible samples of 200 students, calculate mean of every such set ($x_i$), calculate the mean $\overline{x}$ of the means $x_i$ and then calculate standard deviation using $x_i$ and $\overline{x}$?
Comment: Yes, but in actually computing the formula you can extract the denominator of 200 from the sum of each sample mean, which saves you from having to deal with the mean of every such set (see the step involving dividing by $n^2$ in Yves Daoust's answer), which leads to a nice simple relation between the sample mean's standard deviation and the standard deviation of an individual's score.


Mounting guitar hangers to a wall batten – how deep should the batten be?

Question: I've had my garden shed converted into a home office. The walls were already studded out with a wooden board covering, and the builders I hired simply insulated these walls and then added plasterboard.
I want to wall-mount seven guitars on one wall in a similar way to this:

As I understand it, the process here is to mount the battens to the studs (perhaps with some glue to help bond it), and then mount the guitar hangers to the wooden battens. This avoids having to screw the guitar hangers directly into the plasterboard, which I'm keen to avoid.
I've been looking at different timber options for the battens and I'm unclear what depth they should be. The screws that came with the guitar hangers are 45mm – should I be using a batten which is thicker than this, so the screws don't penetrate through to the plasterboard? Or is it fine to use a 20mm batten, assuming I don't care about marks on the plasterboard underneath if I ever remove the batten?
UPDATE to add: thanks to the advice of the kind folks here I was able to get the batten mounted securely, then add the guitar stands. Here's the end result:

Thanks everyone.
Comment: Presumably too late now, but you can get just the hanger part of the guitar hangers (for considerably less than the hanger with part to screw to the wall) and make the battens directly do the job of the little part that comes with the hanger kit by installing Tee-nuts in it from the back.
Comment: Hate to tell you, but that's _six_ guitars and a banjo. ;)
Comment: @Ecnerwal IDK about too late?  It will look really nice, and if you're designing and building a display for that many guitars it's worth pursuing.  If this was my question and that was your answer, it would get the check.
Comment: Ah yes, too late for me as I already bought seven hangers! But I'm happy enough mounting them as-is, and it allows a little more flexibility if I decide to reposition them on the batten (or elsewhere) later, I guess.
Comment: ... and a bassist is going to come along soon and complain it's not a guitar either!
Answer: Unless your guitars are made from lead, a 20mm ( aprox.3/4") batten will be fine.
The screws will leave holes in the plasterboard. If you wish to avoid that get shorter screws or thicker boards.
Answer: I am guessing that the screws that came with your hangers are designed to go through a layer of plaster and then penetrate studs reasonably.  If that's not what you're doing you should not use them.  Buy shorter screws.  If there are two or three screws per hanger, 20 mm screws in a 20 mm board should work great for most normal guitars if you're not hiding your gold bars inside them.
If you use a 20 mm board with a million screws in it, be careful and pre-drill everything or it'll split.
Comment: Thanks – I double checked and the screws are actually 25mm, the website for the product lists the wrong size. Appreciate the pre-drill tip!


Given an emacs command name, how would you find key-bindings ? (and vice versa)

Question: If I know an emacs command name, says, "goto-line"; what if I want to query whether if there are any key-sequences bound to this command ?
And vice versa, given a key sequence, how can I find its command name ?
Answer: To just find key bindings for a command, you can use emacs help's "where-is" feature
C-h w command-name

If multiple bindings are set for the command they will all be listed.
For the inverse, given a key sequence, you can type
C-h k key-sequence

To get the command that would run.
You can get detailed information about a command, also any non-interactive function defined, by typing
C-h f function-name

Which will give you detailed information about a function, including any key bindings for it, and
C-h v variable-name

will give you information about any (bound) variable. Key-maps are kept in variables, however the key codes are stored in a raw format.  Try C-h v isearch-mode-map for an example.
For more help on getting help, you can type
C-h ?

Comment: You can also do `C-h w ` to know just where the key is, not the full documentation. Conversely, you can also do `C-h c ` to just know what function is bound to a key sequence.
Comment: also:  C-h w is typically bound to where-is.
Comment: So, I can where-is where-is when I forget C-h w. That's great ! ;)
Comment: For those who have bound `C-h` to something else in their window managers:
`C-h w` is equivalent to `M-x where-is`.
`C-h k` is equivalent to `M-x describe-key`.
`C-h f` is equivalent to `M-x describe-function`.
`C-h v` is equivalent to `M-x describe-variable`.
`C-h ?` is equivalent to `M-x help-for-help`.
Also note that `f1` can be used instead of `C-h`.
Comment: It would be good if one could generate a list of all current key bindings...must be possible.
Comment: @ftravers - the `which-key` package on MELPA (also part of the Spacemacs config.) does a fine job of this.  It shows the available key bindings after an initial prefix is entered. (e.g. `C-x`)  a list of top level bindings can be shown using `M-x which-key-show-top-level`
Comment: minor typo correction: s/`C-h f variable-name`/`C-h v variable-name`
Answer: For interactively getting the command bound to a keyboard shortcut (or a key sequence in Emacs terms), see the selected answer.
For programmatically getting the command bound to a given key sequence, use the function key-binding or lookup-key that takes a key sequence and returns its bound command. The function key-binding is what C-h k uses.
(key-binding (kbd "C-h m"))

returns the command bound to C-h m by searching in all current keymaps. The function lookup-key searches in a single keymap:
(lookup-key (current-global-map) (kbd "TAB")) ; => indent-for-tab-command
(lookup-key org-mode-map (kbd "TAB")) ; => org-cycle
(lookup-key text-mode-map (kbd "TAB")) ; => nil
(lookup-key isearch-mode-map (kbd "TAB")) ; => isearch-printing-char

For programmatically getting all key sequences bound to a given command, where-is-internal is probably the function to use. The name of the function ending with internal seems to suggest that it's not for Emacs users to use in their init files but this function having a docstring seems to suggest otherwise. Anyone considering use of where-is-internal should first check if remapping keys instead can achieve their goal.
An alternative for finding the keys that are bound to a specific command (e.g., forward-char) is substitute-command-keys (e.g., (substitute-command-keys "\\[forward-char]")).
That is especially useful in larger texts.
Comment: Usage of suffix `-internal` is described there: https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Names.html#Function-Names. It should be used for internal C functions.
Comment: Looking at the documentation of `where-is-internal` suggests it's OK to use it. Also, conventionally we use a format like `where-is--internal` (note the double dash) for "private" functions.
Answer: C-h w (or F1-w): where-is RET somecommandname RET
Does just what you're asking - lists bound keys with no additional information. :)
Answer: An old question, but for the benefit of new readers, there are some other nice ways to see key bindings
M-x describe-bindings
Lists all the bindings currently available, use isearch, occur, etc. to make good use of this list.
M-x describe-prefix-map
This shows all the bindings available from the current mode, you can use the display buffer as you would any other readonly Emacs buffer, for example you can search freely for strings etc.
M-x describe-mode
As well as giving you general info about the current mode, it will also list all the key bindings available.
M-x describe-minor-mode
You will be prompted to enter the name of a minor mode, and then be shown info and key bindings for that minor mode.
NOTE : The examples below use additional packages (available from MELPA)
Which key
This shows you bindings just before you need them.  Enter a prefix, for example C-x or C-c and a list of the bindings available in that prefix will be shown.
You can also view a list of key bindings available from the current mode by using:
M-x which-key-show-top-level
It is useful to bind which-key-show-top-level to a key chord of your choice, so you can view the keys available from anywhere.
For example, C-s (isearch-forward) has an extensive key map which often unknown.  e.g. M-s o starts occur using the current search string, I didn't know about this for many many years of using Emacs.  Having which-key around has helped me discover many rare gems in Emacs.
https://github.com/justbur/emacs-which-key
Guide key
Guide key works in much the same way as which-key I'd recommend taking a look at it to compare features.
https://github.com/kai2nenobu/guide-key
Answer: The lookup-key function works well, but there's no function for finding the key binding programmatically from a command AFAIK. The following does that and is based on the slime-cheat-sheet command.
(defun lookup-function (keymap func)
  (let ((all-bindings (where-is-internal (if (symbolp func)
                                             func
                                           (cl-first func))
                                         keymap))
        keys key-bindings)
    (dolist (binding all-bindings)
      (when (and (vectorp binding)
                 (integerp (aref binding 0)))
        (push binding key-bindings)))
    (push (mapconcat #'key-description key-bindings " or ") keys)
    (car keys)))

(lookup-key (current-global-map) (kbd "C-x C-b"))    ; => list-buffers
(lookup-function (current-global-map) 'list-buffers) ; => "C-x C-b"

Interactively, as @ShreevatsaR suggested, you can use C-h w <function name> (where-is) and C-h c <key sequence> (describe-key-briefly). See here for more information.


Web scraping with Scrapy and BeautifulSoup

Question:

  Bal Harbour

Hi everyone, I'm trying to collect the 'data-nid' and the 'data-localisation' but when I write my code :
'geocoordinates':['class','result-item-name','data-localisation']

I always get a None response.
Can you guys help me ? I am new to BeautifulSoup and I am not at ease with it.
Thank you very much !
Comment: Please propvide a reproducible example and the rest of your code. It`s quite hard to help you without getting a grasp of what you`ve done so far.
Comment: Hi and welcome to Stack Overflow!. Please follow these guidelines to create a [Minimal, Complete and Verifiable example](https://stackoverflow.com/help/mcve) in order to get much better answers.
Answer: In BeautifulSoup you can access the attributes using key-value. Just like a dictionary
Ex:
from bs4 import BeautifulSoup
html = """
Bal Harbour
"""

soup = BeautifulSoup(html, "html.parser")
h2 = soup.find("h2", class_= "result-item-name")
print(h2["data-nid"])
print(h2["data-localisation"])

Output:
117
25.88872, -80.12488

Answer: from bs4 import BeautifulSoup

html = """

Bal Harbour

"""
soup = BeautifulSoup(html, 'html.parser')

h2 = soup.find('h2', {'class': 'result-item-name'})
print(h2['data-nid'])
print(h2['data-localisation'])

Output
117
25.88872, -80.12488


Are there any ways to decrease the impressively long save times for Sims 3 worlds?

Question: I've been watching my wife play Sims 3, and I've noticed the save times are impressively long. (Load times are fine.)
The save times start off fine on a new world but after a few hours probably take longer to save than her computer would take to install an operating system a few times.
She has a fair number of expansions, but the save times are literally upwards of 10-20 minutes once a world gets somewhat developed.
This is on a brand new i5 processor with 8GB of memory, a Samsung SSD, a fairly good 1GB graphics card with nothing else but Windows 8, Chrome, Steam, and TF2 installed.
Is there any way to fix the unreasonably long save times with this game?
Comment: Maybe ensure the save location is excluded from virus scanning too.
Comment: Does this only occur in Sims 3? What is the general size of the file? What happens if you try to save a text file using notepad or something around the same size?
Comment: I'm not sure about notepad specifically, but other file operations work fine. It copies things, extracts, etc. as fast as an SSD would be expected to.
Comment: I don't plays Sims, but the Q intrigued me. I did a quick google search and I see people reporting saves as large as *1.5 GB* (!) And a possible hint that Sims' "Memories" (specifically the photos) may be the root of the issue.
Comment: Well, she doesn't have that one installed, but she has quite a few add-ons. I just have never heard of a game taking so long to save.
Comment: That's a good note, no anti-virus in this case.
Answer: This is probably not going to fix the entire issue since I've never encountered save times as long as you are describing, but I've noticed that the save time also depends on what is currently displayed on your screen.
What I mean is that performing a maximum zoom in the middle of a room or in the garden seemed to reduce the save time.
In my case it was maybe going down from 1 minute to 30 seconds, so I'm not sure about the impact it would have on your side, but it's still worth a try.
Answer: The general view I got from a quick google search would be that Sims3 is quite RAM intensive whilst saving, but that doesn't seem to be an issue for you with 8GB of RAM available.
However, I have seen a couple of forum topics come up in search results that, although are aimed more at loading times, might help with save times as well.
Link 1
Link 2
Comment: Links to forums where they recommend useless programs to boost a computers speed should be taken with a big grain of salt.
Answer: First of all the reason for long save time can either you are using game mods that are heavy or the game is fragmented or  you have a low config system
To solve this problem you n ed to download razer game booster(Razor game booster]1)
when installed go to utilities and defrag you games and download advanced system care  that will tune up your system
http://download.iobit.com/asc-setup.exe


How does PHP memory actually work

Question: I've always heard and searched for new php 'good writing practice', for example: It's better (for performance) to check if array key exists than search in array, but also it seems better for memory too:
Assuming we have:
$array = array
(
    'one'   => 1,
    'two'   => 2,
    'three' => 3,
    'four'  => 4,
);

this allocates 1040 bytes of memory,
and
$array = array
(
    1 => 'one',
    2 => 'two',
    3 => 'three',
    4 => 'four',
);

requires 1136 bytes
I understand that the key and value surely will have different storing mechanism, but
please can you actually point me to the principle how does it work?
Example 2 (for @teuneboon):
$array = array
(
    'one'   => '1',
    'two'   => '2',
    'three' => '3',
    'four'  => '4',
);

1168 bytes
$array = array
(
    '1' => 'one',
    '2' => 'two',
    '3' => 'three',
    '4' => 'four',
);

1136 bytes
consuming same memory:

4 => 'four',
'4' => 'four',
Comment: Using comment cause this is more of an assumption than an actual answer: I assume this is because when you use integers as an index PHP "assumes" the array is just a list, thus saves it something like this: $array = (empty, 'one', 'two', 'three', 'four'). So it doesn't have to store the actual integers 1,2,3 and 4.
Comment: If you're specifically interested in arrays, read [this blog post](http://nikic.github.io/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html) from one of PHP's core devs
Comment: My assumption would be that the keys are hashed (because PHP arrays are hash maps), so their length is pretty irrelevant. The second example simply has *larger values* which take up more memory.
Comment: @deceze I'd assume PHP does not _only store the hashes_ for the keys ;-)
Comment: @zerkms sure man, I don't pass anything to the function, so it's  `false` by default
Comment: @deceze - you're right that the keys are hashed, a PHP array is a hashmap; plus there's a bucket overhead to allow for hash clashes
Comment: @Mark OK, I *know* that keys are hashed; my assumption was actually in the latter part of that statement. :o)
Comment: @deceze Hmm, you're probably right. Judging by example 2 I was wrong :)
Comment: @George: Now I'm curious, what if you change the 1-4 to e-h? That should surely be the same memory usage right?
Comment: @MarkBaker that is a really useful read and I actually think it isn't limited to arrays as far as what it teaches.
Comment: @teuneboon ya that would be same but in my example I've not did anything unclear
Comment: What happens if you add '9' => 'x' and 'x' => '9' to your third example? In that order. a number in the character keys and a character in the number keys
Comment: @Hammerstein I've changed last row with what you suggested but with no difference
Comment: How do you check the memory consumption?
Comment: @zerkms with `memory_get_usage`
Comment: @George Garchagudashvili: with or without `true` as a first argument? I hope - without?
Answer: Note, answer below is applicable for PHP prior to version 7 as in PHP 7 major changes were introduced which also involve values structures.
TL;DR
Your question is not actually about "how memory works in PHP" (here, I assume, you meant "memory allocation"), but about "how arrays work in PHP" - and these two questions are different. To summarize what's written below:

PHP arrays aren't "arrays" in classical sense. They are hash-maps
Hash-map for PHP array has specific structure and uses many additional storage things, such as internal links pointers
Hash-map items for PHP hash-map also use additional fields to store information. And - yes, not only string/integer keys matters, but also what are strings themselves, which are used for your keys.
Option with string keys in your case will "win" in terms of memory amount because both options will be hashed into ulong (unsigned long) keys hash-map, so real difference will be in values, where string-keys option has integer (fixed-length) values, while integer-keys option has strings (chars-dependent length) values. But that may not always will be true due to possible collisions.
"String-numeric" keys, such as '4', will be treated as integer keys and translated into integer hash result as it was integer key. Thus, '4'=>'foo' and 4 => 'foo' are same things.

Also, important note: the graphics here are copyright of PHP internals book
Hash-map for PHP arrays
PHP arrays and C arrays
You should realize one very important thing: PHP is written on C, where such things as "associative array" simply does not exist. So, in C "array" is exactly what "array" is - i.e. it's just a consecutive area in memory which can be accessed by a consecutive offset. Your "keys" may be only numeric, integer and only consecutive, starting from zero. You can't have, for instance, 3,-6,'foo' as your "keys" there.
So to implement arrays, which are in PHP, there's hash-map option, it uses hash-function to hash your keys and transform them to integers, which can be used for C-arrays. That function, however, will never be able to create a bijection between string keys and their integer hashed results. And it's easy to understand why: because cardinality of strings set is much, much larger that cardinality of integer set. Let's illustrate with example: we'll recount all strings, up to length 10, which have only alphanumeric symbols (so, 0-9, a-z and A-Z, total 62): it's 6210 total strings possible. It's around 8.39E+17. Compare it with around 4E+9 which we have for unsigned integer (long integer, 32-bits) type and you'll get the idea - there will be collisions.
PHP hash-map keys & collisions
Now, to resolve collisions, PHP will just place items, which have same hash-function result, into one linked list. So, hash-map would not be just "list of hashed elements", but instead it will store pointers to lists of elements (each element in certain list will have same hash-function key). And this is where you have point to how it will affect memory allocation: if your array has string keys, which did not result in collisions, then no additional pointers inside those list would be needed, so memory amount will be reduced (actually, it's a very small overhead, but, since we're talking about precise memory allocation, this should be taken to account). And, same way, if your string keys will result into many collisions, then more additional pointers would be created, so total memory amount will be a bit more.
To illustrate those relations within those lists, here's a graphic:

Above there is how PHP will resolve collisions after applying hash-function. So one of your question parts lies here, pointers inside collision-resolution lists. Also, elements of linked lists are usually called buckets and the array, which contains pointers to heads of those lists is internally called arBuckets. Due to structure optimization (so, to make such things as element deletion, faster), real list element has two pointers, previous element and next element - but that's only will make difference in memory amount for non-collision/collision arrays little wider, but won't change concept itself.
One more list: order
To fully support arrays as they are in PHP, it's also needed to maintain order, so that is achieved with another internal list. Each element of arrays is a member of that list too. It won't make difference in terms of memory allocation, since in both options this list should be maintained, but for full picture, I'm mentioning this list. Here's the graphic:

In addition to pListLast and pListNext, pointers to order-list head and tail are stored. Again, it's not directly related to your question, but further I'll dump internal bucket structure, where these pointers are present.
Array element from inside
Now we're ready to look into: what is array element, so, bucket:
typedef struct bucket {
    ulong h;
    uint nKeyLength;
    void *pData;
    void *pDataPtr;
    struct bucket *pListNext;
    struct bucket *pListLast;
    struct bucket *pNext;
    struct bucket *pLast;
    char *arKey;
} Bucket;

Here we are:

h is an integer (ulong) value of key, it's a result of hash-function. For integer keys it is just same as key itself (hash-function returns itself)
pNext / pLast are pointers inside collision-resolution linked list
pListNext/pListLast are pointers inside order-resolution linked list
pData is a pointer to the stored value. Actually, value isn't same as inserted at array creation, it's copy, but, to avoid unnecessary overhead, PHP uses pDataPtr (so pData = &pDataPtr)

From this viewpoint, you may get next thing to where difference is: since string key will be hashed (thus, h is always ulong and, therefore, same size), it will be a matter of what is stored in values. So for your string-keys array there will be integer values, while for integer-keys array there will be string values, and that makes difference. However - no, it isn't a magic: you can't "save memory" with storing string keys such way all the times, because if your keys would be large and there will be many of them, it will cause collisions overhead (well, with very high probability, but, of course, not guaranteed). It will "work" only for arbitrary short strings, which won't cause many collisions.
Hash-table itself
It's already been spoken about elements (buckets) and their structure, but there's also hash-table itself, which is, in fact, array data-structure. So, it's called _hashtable:
typedef struct _hashtable {
    uint nTableSize;
    uint nTableMask;
    uint nNumOfElements;
    ulong nNextFreeElement;
    Bucket *pInternalPointer;   /* Used for element traversal */
    Bucket *pListHead;
    Bucket *pListTail;
    Bucket **arBuckets;
    dtor_func_t pDestructor;
    zend_bool persistent;
    unsigned char nApplyCount;
    zend_bool bApplyProtection;
#if ZEND_DEBUG
    int inconsistent;
#endif
} HashTable;

I won't describe all the fields, since I've already provided much info, which is only related to the question, but I'll describe this structure briefly:

arBuckets is what was described above, the buckets storage,
pListHead/pListTail are pointers to order-resolution list
nTableSize determines size of hash-table. And this is directly related to memory allocation: nTableSize is always power of 2. Thus, it's no matter if you'll have 13 or 14 elements in array: actual size will be 16. Take that to account when you want to estimate array size.

Conclusion
It's really difficult to predict, will one array be larger than another in your case. Yes, there are guidelines which are following from internal structure, but if string keys are comparable by their length to integer values (like 'four', 'one' in your sample) - real difference will be in such things as - how many collisions occurred, how many bytes were allocated to save the value.
But choosing proper structure should be matter of sense, not memory. If your intention is to build the corresponding indexed data, then choice always be obvious. Post above is only about one goal: to show how arrays actually work in PHP and where you can find the difference in memory allocation in your sample.
You may also check article about arrays & hash-tables in PHP: it's Hash-tables in PHP by PHP internals book: I've used some graphics from there. Also, to realize, how values are allocated in PHP, check zval Structure article, it may help you to understand, what will be differences between strings & integers allocation for values of your arrays. I didn't include explanations from it here, since much more important point for me - is to show array data structure and what may be difference in context of string keys/integer keys for your question.
Answer: Although both arrays are accessed in a different way (i.e. via string or integer value), the memory pattern is mostly similar.
This is because the string allocation either happens as part of the zval creation or when a new array key needs to be allocated; the small difference being that numeric indices don't require a whole zval structure, because they're stored as an (unsigned) long.
The observed differences in memory allocation are so minimal that they can be largely attributed to either the inaccuracy of memory_get_usage() or allocations due to additional bucket creation.
Conclusion
How you want to use your array must be the guiding principle in choosing how it should be indexed; memory should only become an exception to this rule when you run out of it.
Answer: From PHP manual Garbage Collection http://php.net/manual/en/features.gc.php
gc_enable(); // Enable Garbage Collector
var_dump(gc_enabled()); // true
var_dump(gc_collect_cycles()); // # of elements cleaned up
gc_disable(); // Disable Garbage Collector

PHP does not return released memory very well; Its primary usage online does not require it and effective garbage collection takes time away from providing the output; When the script ends the memory is going to be returned anyway.
Garbage collection happens.

When you tell it to
int gc_collect_cycles ( void )
When you leave a function
When the script ends

Better understanding of PHP's Garbage collection from a web host, (no affiliation).
http://www.sitepoint.com/better-understanding-phps-garbage-collection/
If you are considering byte by byte how the data is set in memory. Different ports are going to effect those values. 64bit CPUs performance is best when data sits on the first bit of a 64bit word. For the max performance a specific binary they would allocate the start of a block of memory on the first bit, leaving up to 7 bytes unused. This CPU specific stuff depends on what compiler was used to compile the PHP.exe. I can not offer any way to predict exact memory usage, given that it will be determined differently by different compilers.
Alma Do, post goes to the specifics of the source which is sent to the compiler. What the PHP source requests and the compiler optimizes.
Looking at the specific examples you posted. When the key is a ascii letter they are taking 4 bytes (64 bits) more per entry ... this suggests to me, (assuming no garbage or memory holes, ect), that the ascii keys are greater than 64 bits, but the numeric keys are fit in a 64bit word. It suggests to me your using a 64bit computer and your PHP.exe is compiled for 64bit CPUs.
Comment: @GeorgeGarchagudashvili Looking at the specific examples you posted. When the key is a ascii letter they are taking 4 bytes (64 bits) more per entry ... this suggests to me, assuming no garbage or memory holes, that the ascii keys are greater than 64 bits, but the numeric keys are fit in a 64bit word. It suggests to me your using a 64bit computer and your PHP.exe is compiled for 64bit CPUs.
Comment: I understand the subject, but I don't believe this 'inaccuracy' caused me to ask this question
Comment: @GeorgeGarchagudashvili If you are considering byte by byte how the data is set in memory. Different ports are going to effect those values. 64bit CPUs performance is best when data sits on the first bit of a 64bit word. To max the performance a specific binary would allocate the start of a block of memory on the first bit, leaving up to 7 bytes unused. This CPU specific stuff depends on what compiler was used to compile the PHP.exe. I can not offer any way to predict exact memory usage, given that it will be determined differently by different compilers.
Comment: Thanks, you are absolutely right. Can you include your last comment in your answer? I'll give you my bounty because you caught the case I fell in, but accept @Alma Do's as answer because it does really deal with How does PHP memory actually work?
Comment: Sorry I was a bit confused, I was interested exactly how `key` => `value` was applied to php memory (first example), rest examples I've posted in reply to comments under my question, and somehow examples lead me to another way out of my main question. Thank you very much, much much appreciated
Comment: @GeorgeGarchagudashvili, I understand your reason for bounty the answers only acknowledged that the bytes are unaccounted for. I assumed the question was related to additional memory being used, IE Garbage remaining. Alma Do answer is specific to how PHP is programmed to handle the memory.
Answer: Arrays in PHP are implemented as hashmaps. Hence the length of the value you use for the key has little impact on the data requirement. In older versions of PHP there was a significant performance degradation with large arrays as the hash size was fixed at array creation - when collisions starting occurring then increasing numbers of hash values would map to linked lists of values which then had to be further searched (with an O(n) algorithm) instead of a single value, but more recently the hash appears to either use a much larger default size or is resized dynamically (it just works - I can't really be bothered reading the source code).
Saving 4 bytes from your scripts is not going to cause Google any sleepless nights. If you are writing code which uses large arrays (where the savings may be more significant) you're probably doing it wrong - the time and resource taken to fill up the array could be better spent elsewhere (like indexed storage).
Comment: If you're working with large enumerated arrays, with sequential keys from 0 up, and know how large they're going to be in advance, then SPLFixedArray is a major memory saving
Comment: How does the use of a hashmap degrades on the memory usage for the key values?
Comment: The sense of a hash-map _is_ to have multiple keys to map on the same hash - with only one key at each hash it would be non-sense
Comment: @fast: No. The point of a hash map is to get an order of log(n) in lookups and insertions/deletions. See http://en.wikipedia.org/wiki/Hash_table take particular note of the graph half way down.
Comment: @symcbean thanks for answering, the point here is not to save some bytes in memory but to know how does it take care of the things...

How does PHP memory actually work


How to send a MVC Model in a Ajax Call

Question: I am facing challenges for posting a Model to invoke an action in a controller.
i have a main view where i make a web service call and with certain criteria and get the results.Once i have the results i am showing the results into a partial view as a pop up on the original view and then in the partial view pop i have  a search button which should search in the results shown in the partial view.So for this i need to send the model to the controller to search in the results set that is being displayed in the partial view.
Can this be achieved
How can be this achieved ?
Need your help guys....
Thanks in Advance
Comment: In my understanding you have results display on the page and when the user searches that page will only show the result based on his keyword or criteria?
Answer: Don't really get why you have problem here. If your controller action looks like
[HttpPost]
public ActionResult index(int param1, string param2...)
{

}

You need to do POST request to this action with parameters specified. So request body will have param1=123&param2=somestring&...
Or using JSON: {"param1": 123, "param2":"somestring"...}. Almost same thing for GET request.
ajax call itself can look like:
$.ajax({
    type: 'POST',
    url: 'your_url',
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(your_model),
    success: function(response) {
    }
});

How to send a MVC Model in a Ajax Call


Pass a Number Value into a Shell Script

Question: I regularly run a .sh script that has a list of menu options and expects a value from 1-7 that corresponds to a list of menu options. 99% of the time I use the same option (#6) so I'm trying to figure out a way pass the value of '6' into the script so that every time the script runs it sends the keystroke '6' once then just runs the script normally.
this is the command I've come up with so far:
echo '6' | /example/folder/script.sh

that sort of works except it repeatedly sends '6' every few seconds and never stops until I kill the script. I just want it to send 6 once then just run the script normally. what am I missing here?
the system I'm on is actually AIX using the ksh shell if that makes any difference
Comment: `echo '6'` sends `6` just _once_.  Is there some loop that you haven't shown us?
Comment: @John1024 The script is ridiculously complex 5000+ lines. from the end user perspective it's just a list of menus that and you enter a number to select the menu you want. But I guess from what you and wk are telling me what I'm doing should work so it must be something weird going on with this script then.. oh well...
Comment: If you want to send simulated input to a complex script, the right tool is [expect](https://en.wikipedia.org/wiki/Expect) or pexpect.  It allows you to send the right inputs and the right times.  You can use `expect` _without_ changing your complex script in any way.
Comment: Without knowing what the script is doing, we're all just guessing but it could be the trailing newline from you echo command that's messing you up. It would be worth trying: echo –n  '6' or printf '6' just to see if that's the case.
Comment: If it's an AIX system, why are you asking this question on AskUbuntu and not [Unix & Linux](http://unix.stackexchange.com/)?
Answer: I would use commandline arguments, so you could run it like that
/example/folder/script.sh 6

Then, inside your script you could refer to it as $1
Comment: I don't really understand what you mean by that but, but either way I didn't create the script so I would rather not make change to it if possible.
Comment: @ryan, your solution should still work fine. It seems there is something wrong with how you run the command.

Pass a Number Value into a Shell Script


php statistics z-score normal distributions

Question: how do I compute the z-score of a array of numbers using PHP? I need to compute the z-score and then find the percentile (CDF)! what PHP functions can I use? thanks!
Answer: The following code will give a good approximation of the CDF (Abramowitz & Stegun (1964))
function normal_pdf($x) {
    return exp(-$x * $x / 4) / sqrt(2 * M_PI);
}

function normal_cdf($x) {
    $b = array(0.2316419, 0.319381530, -0.356563782, 1.781477937, -1.821255978, 1.330274429);
    $t = 1 / (1 + $b[0] * $x);
    $result = 0;
    for ($i = 1; $i < 6; $i++) {
        $result += $b[$i] * pow($t, $i);
    }
    return 1 - normal_pdf($x) * $result;
}

This assumes a standard normal distribution. Recall that to standardize, use z = (x - mean) / (standard deviation)
Comment: The normal_cdf function is not correct. It gives negative values for some values of x.
Answer: /* Mean */
function mean($input_array)
{
  $total = 0;
  foreach ($input_array as $value)
  {
    $total += $value;
  }
  return ($total / count($input_array));
}

/* Standard Deviation */
function std($arr)
{
  if (!count($arr))
  return 0;
  $mean = mean($arr);
  $sos = 0; // Sum of squares
  for ($i = 0; $i < count($arr); $i++)
  {
    $sos += ($arr[$i] - $mean) * ($arr[$i] - $mean);
  }
  return sqrt($sos / (count($arr) - 1));
}

/* Z Scores */
function z($var, $arr)
{
  return ($var -mean($arr)) / std($arr);
}

Answer: There are a few functions in the PHP statistics extension that could help you — You probably want stats_standard_deviation for a start.
Comment: I need  stats_cdf_binomial  it seems but that function is not well documented.. I don't even know what the arguments for that function is..
Answer: $control=array(15,7);
$treatment=array(46,8);
$confidence=number_format(cumnormdist(zscore($control, $treatment))*100,0);

function cr($t)
{
    return $t[1]/$t[0];
}

function zscore($c, $t)
{
    $z = cr($t)-cr($c);
    $s = (cr($t)*(1-cr($t)))/$t[0] + (cr($c)*(1-cr($c)))/$c[0];
    return $z/sqrt($s);
}

function cumnormdist($x)
{
  $b1 =  0.319381530;
  $b2 = -0.356563782;
  $b3 =  1.781477937;
  $b4 = -1.821255978;
  $b5 =  1.330274429;
  $p  =  0.2316419;
  $c  =  0.39894228;

  if($x >= 0.0) {
      $t = 1.0 / ( 1.0 + $p * $x );
      return (1.0 - $c * exp( -$x * $x / 2.0 ) * $t *
      ( $t *( $t * ( $t * ( $t * $b5 + $b4 ) + $b3 ) + $b2 ) + $b1 ));
  }
  else {
      $t = 1.0 / ( 1.0 - $p * $x );
      return ( $c * exp( -$x * $x / 2.0 ) * $t *
      ( $t *( $t * ( $t * ( $t * $b5 + $b4 ) + $b3 ) + $b2 ) + $b1 ));
    }
}


Purpose of assigning the return object to the function name after the function definition

Question: I find the generated Python wrapper for a C++ function by swig has the following lines:
def my_func(arg):
    return _cpp_mod.my_cpp_func(arg)
my_func = _cpp_mod.my_cpp_func

The source code in the .i file is as follows:
%module cpp_mod
... ...
%inline %{
MyObj& my_cpp_func(arg) {
    return *new MyObj(arg);
}
%}

All functions of the generated code seem OK.
What I want to know is the purpose of the third line for the
generated python code. Thanks in advance.
Comment: The real question is what the *first two* lines are for!  The function is being *replaced* immediately after being created.
Answer: It just the way SWIG has decided to wrap functions. The first part
def my_func(arg):
  return _cpp_mod.my_cpp_func(arg)

reveals the number of input arguments and if commments are generated, they will be inserted here.
The second part
my_func = _cpp_mod.my_cpp_func

is redefining the function to the generated library function


Java - random digits inside an array of 5x5

Question: int[][] array = new int[5][5];
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array.length; j++) {
            System.out.print(1);
        }
        System.out.println();
    }

Hi guys, i have this array which prints a 5x5 1's
11111
11111
11111
11111
11111

What i would like to do is to make three (3) of those 1's as 0 randomly. Example
11101
11111
11011
10111
11111

How do I do that? Thank you in advance!
Answer: For random stuff, you can use Math.random(). It returns a random double between 0 included and 1 excluded.
You could do this :
int w = 5, h = 5;
int[][] array = new int[w][h];
for (int i = 0; i < w; i++) {
  for (int j = 0; j < h; j++) {
    array[i][j] = 1;
  }
}
for (int c = 0; c < 3; c++) {
  int i, j;
    do {
      int index = (int)(Math.random() * w * h);
      i = index / w;
      j = index % w;
    } while (array[i][j] == 0);
    array[i][j] = 0;
  }

This solution is acceptable only if the number of 0 is low compared to the total size of the array. If the number of 0 is too high this code is going to be very inefficient because we will search randomly for a value of 1
Comment: This answer might not be correct as at least 49% of 0s distribution is uniform, therefore I wouldn't consider it as random... Trailing 0's 111....000, 11..000...111 etc...
Comment: This version should do it normally, am I wrong ? @MS90
Answer: You would have to firstly initialize the array with 1s:
int[][] array = new int[5][5];
for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array.length; j++) {
        array[i][j] = 1;
    }
}

Then set 0s in 3 random positions:
Random random = new Random();
int a = 0;
while (a < 3) {
    int i = random.nextInt(array.length);
    int j = random.nextInt(array[0].length);
    if(array[i][j] != 0) {
        array[i][j] = 0;
        a++;
    }
}

Using this approach each entry in the array has the same chance of becoming a 0. Finally you should print the array:
for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array.length; j++) {
        System.out.print(array[i][j]);
    }
    System.out.println();
}

Answer: Here is a slightly different solution for fun. Create an list with numbers from 0 to 24, shuffle the list and pick the the 3 first elements from it to update the 2D array. The sequence of numbers means that not the same position in the array will be set to 0 more than once.
List<Integer> sequence = IntStream.rangeClosed(0, 24)
    .boxed()
    .collect(Collectors.toList());

Collections.shuffle(sequence);

for (int i = 0; i < 3; i++) {
    int value = sequence.get(i);
    int row  = value / 5;
    int  column = value % 5;

    array[row][column] = 0;
}

The code for generating the sequence came from this answer
Comment: You shouldn't remove 1 to column because your numbers are already in a 0-based index


Bizarre MySQL Wildcard Behavior

Question: I've run into a strange behaviour that I've been unable to figure out in my PHP code
using this code (omitting the entirety for the moment) -->
WHERE ss.showName LIKE '%".$searchFor."%'

$searchFor is defined by user input and sent via an AJAX request. The result is stored in $result as $tvLine. It is then displayed in html with $tvLine['showName'];
Now let's say I'm looking for the ss.showName "Joni and Friends"...
If I type any amount of the letters used in "Joni and" I receive the correct result --> "Joni and Friends"
As well, if I type any amount of the letters used in "riends" I receive the correct result --> "Jonie and Friends".
However, if I type simply "F" or "f" I receive no results.
How and why could this be?
Thank you
if (  isset($_GET['userInput']))  {

        $searchFor = $_GET['userInput']; // the actual user search content
        $searchDate = $_GET['sDate']; // ascertains the correct day for search
        $searchTz = $_GET['sTz'];   // ascertains the correct timezone for search
        // conditional statement to check time difference for MySQL
            if ($searchTz == "UK"){ $searchTz = 0;}
                else { $searchTz = 5; }

        global $wpdb;
        $now = date("Y-m-d H:i:s");
        $params = array($now);
        $sql = "SELECT se.*, ss.showName, TIMEDIFF(NOW(), UTC_TIMESTAMP) as server_date
            FROM showpress_episodes
            AS se LEFT JOIN showpress_shows AS ss
            ON se.showId=ss.id
            WHERE ss.showName LIKE '%".$searchFor."%'
            AND DATE(episodeStartTime) = CURRENT_DATE() + INTERVAL $searchDate DAY
            AND episodeStartTime >= NOW() - INTERVAL $searchTz HOUR
            ORDER BY episodeStartTime
            ";
        $result = $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);

?>

Search Results     X

<?php
// create table to hold SQL
        if (count($result) < 1){
            echo ('

Show Name

NO RESULTS

'.$searchFor.'

');
        }
        else{
            $display_string .= '

';
                    //$display_string .= "
".$searchFor."
	";
            foreach ($result as $tvLine) {

                $display_string .= "
";
                //$display_string .= $tvLine[server_date];
                $display_string .= "".$tvLine['showName']."";
                $display_string .= "
";

            }
            $display_string .= "

";
        }
        echo $display_string;

        exit();

Comment: Your code is at risk from SQL Injection. You should [use prepared statements](https://stackoverflow.com/questions/18527659/php-mysqli-prepared-statement-like) to correct this ASAP.
Comment: Output the final, fully escaped query and check. My suspicion is you're running it through something like sprintf somewhere and %F is seen as a control character.
Comment: This is where seeing the code would help us answer it.
Comment: Added code: Ok.. i think the control character issue must be what's happening as when I add a second % to the beginning.. then the search functions well. I guess I don't understand control characters if you can explain. thanks
Answer: You should read: wpdb::prepare( string $query, mixed $args )
The code should look like this (untested, I don't have a wordpress to test with):
    global $wpdb;
    //$now = date("Y-m-d H:i:s");
    $params = array("%".$searchFor."%", $searchDate, $searchTz);
    $sql = "SELECT se.*, ss.showName, TIMEDIFF(NOW(), UTC_TIMESTAMP) as server_date
        FROM showpress_episodes
        AS se LEFT JOIN showpress_shows AS ss
        ON se.showId=ss.id
        WHERE ss.showName LIKE %s
        AND DATE(episodeStartTime) = CURRENT_DATE() + INTERVAL %d DAY
        AND episodeStartTime >= NOW() - INTERVAL %d HOUR
        ORDER BY episodeStartTime
        ";
    $result = $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);

Bizarre MySQL Wildcard Behavior


Renamed react native app, Activity class MainActivity does not exist

Question: For my app to be accepted on the play store, I have had to rename my app.
I have changed the applicationId in build.gradle
defaultConfig {
    applicationId "com.tcapp" // from
    applicationId "com.rg.tcapp" // to

When I run react-native run-android I get the error:

Error type 3
  Error: Activity class {com.tcapp/com.tcapp.MainActivity} does not exist.

What other files do I need to change to rename my app and get it running?
Comment: Have you also changed all the ``com.tcapp`` references in your manifest? And have you also changed the namespace used in your MainActivity?
Comment: If you are using Android Studio you must set your app name in the strings XML under     App Name Here
Answer: If you want to rename an application, you can use react-native-rename to do it
Answer: I've recently changed my package name in react native due to which the entire folder structure needed to be changed in android. react-native-rename didnt help me, but i've followed the steps of the verified answer of the below link, please do check that out, it will help you. Follow all those steps. Rename android package and application name


How to automatically load previous form selections into itself and make one query for all selected types?

Question: On my website I have a search-bar. It works as a form that submits and searches my database for the value inputted in the text bar, in the column where the type is selected. My form looks like this
<form action=" " method="POST" >
 <select name="search_type">
<option value="fullname">People</option>
<option value="username" >Username</option>
<option value="firstname">First Name</option>
<option value="lastname" >Last Name</option>
<option value="email" >Email</option>
</select><input type="text" name="value" >
<input type="submit" value="Search">
</form>

After every search it refreshes and users have to reselect data and put it back in… is there a way to have the last item searched be reselected?
Then from that form I use PHP to check what type was selected and look at my database… Currently I have a bunch of conditionals which I feel is too inefficient, is there a way to get rid of all those conditionals and directly search specific columns in my databases from one call? (For the record my db column names are user_fullname, user_username, user_firstname, user_lastname, and user_email).
My PHP script looks like this (I have one block conditional for each type variable)
If($_POST['search_type'] == 'fullname'){
$results ="SELECT `user_id`, `user_username`, `user_firstname`, `user_lastname` FROM `mytable` WHERE user_fullname = {$_POST['value']}";
while($results_row = mysql_fetch_assoc($results)){
$returned_results[] = array( ///returning stuff///);
}}
///etc

I have been looking for hours trying to find a way to only run one query for any type but I could have not got anything to work. Please any advice or help would be very much appreciated.
Comment: By 'automatically' do you mean you want to use AJAX to reload your form data or are you fine with the page fully reloading?
Comment: I am fine if the page reloads, by automatic I just mean when the pages loads can the information be put back in without the user having to click or type anything.
Answer: To add onto Devon's answer I figured out how to combine the other conditional functions within that main call for stuff like email verification.
if($_POST['value'] != ''){
if($_POST['search_type'] == 'user_email'){
if(!filter_var($_POST['value'], FILTER_VALIDATE_EMAIL))
    {
        die("Invalid E-Mail Address");
    }
}
$where = " {$_POST['search_type']} LIKE '%{$_POST['value']}%'";
$results ="SELECT `user_id`, `user_username`, `user_firstname`, `user_lastname` FROM `mytable` WHERE $where";

}

Answer: To remove the conditionals in your mysql query I think the simplest method would be to just set the values of your search type to your actual column names so then you can just run your query for that value. For example your option values would be "user_fullname","user_username" etc in your form and for your PHP script you could try this:
$where = " {$_POST['search_type']} LIKE '%{$_POST['value']}%'";
$results ="SELECT `user_id`, `user_username`, `user_firstname`, `user_lastname` FROM `mytable` WHERE $where";

I think the easiest method for you to refresh your form data and pre-select fields would be some PHP conditionals:
<form action="search.php" method="POST" >
 <select name="search_type">
<option value="user_fullname"<?php if($_POST['search_type'] == 'user_fullname'){echo 'selected';} ?>>People</option>
<option value="user_username" <?php if($_POST ['search_type'] == 'user_username'){echo 'selected';} ?>>Username</option>
<option value="user_firstname" <?php if($_POST ['search_type'] == 'user_firstname'){echo 'selected';} ?>>First Name</option>
<option value="user_lastname" <?php if($_POST ['search_type'] == 'user_lastname'){echo 'selected';} ?>>Last Name</option>
<option value="user_email" <?php if($_POST ['search_type'] == 'user_email'){echo 'selected';} ?>>Email</option>
</select><input type="text" name="value" value="<?php echo $_POST['email']; ?>">
<input type="submit" value="Search">
</form>

Comment: One question, I see you putting where in the query but what do the % in %{$_POST['value']}% mean?
Comment: The % means that you can have anything on any-side of the variable. for example if you search 'jo' in your form you could get results for 'joe', 'banjo', or 'pajon'. It doesn't matter what is on the left or right if any query column value contains something similar to your search value it will be returned.
Comment: Oh alright that is cool. I guess that is probably even more helpful that just searching exactly what someone is looking for.


How can I find the function $f(n)$

Question: Find the function $f(n)$ if,
 $$\lim_{n\to\infty}\frac{-2^{2 n - 1} + 2^{8 n - 3} - 3}{-2^{2 f(n) - 1} + 2^{2 f(n) + 3} - 3} = 1$$
Comment: Are you sure about the expression ?
Comment: Sorry..I  fixed. Now limit is correct
Comment: I think it means $n\rightarrow-\infty$.
Comment: For example: you can try $k=3$, $k=4$
Comment: @ClaudeLeibovici yes of course...
Comment: Maybe I wrote wrong.Now I'm fixing.
Answer: After discarging neglectible terms, the numerator is asymptotic to $2^{8n-3}$, while the denominator is asymptotic to $2^{2f(n)}(-2^{-1}+2^3)$.
By identification of the base $2$ logarithms,
$$8n-3=2f(n)+\log_2\frac{15}2.$$

It is improper to speak of the sequence, as any sequence asymptotic to this $f(n)$ can do.

How can I find the function $f(n)$


C++ `typedef Ptr(*Constructor)(LayerParams &params);`

Question: I posted this question on OpenCV forum but since that seems to be a less active place than SO, I'm still trying to see if anyone can help me understand this line of code without much background in OpenCV source code.
Basically, I'm having trouble understanding this line of code from the OpenCV source code:
typedef Ptr<Layer>(*Constructor)(LayerParams &params);

In particular, I can't see how typedef work here. This seems very different from the typical typedef unsigned long ulong; example I saw online. Which type is being defined as which here?
If it helps, Ptr is a template class for smart pointers with shared ownership.
Constructor seems to be defined HERE in this exact code.
LayerParams is a data structure defined in OpenCV.
Thanks!
Comment: `Constructor` is being defined as a type that is a pointer to a function.
Comment: @NeilButterWorth Thanks for pointing this out!
Answer: To figure out what a typedef definition means, you look at what variable would be declared if the typedef keyword weren't there:
Ptr<Layer>(*Constructor)(LayerParams &params);

This is like a function declaration, except that in place of the function name, we have (*Constructor).  This means that the variable Constructor is a pointer to a function.  The function it points at takes a reference to LayerParams as parameter, and returns a Ptr<Layer>.  See also the "Clockwise Spiral Rule". (Note that page is about C. In C++ you also have lvalue reference (&) and rvalue reference (&&) tokens involved, but those work like pointer (*) tokens.)
So putting the typedef back on,
typedef Ptr<Layer>(*Constructor)(LayerParams &params);

defines the type Constructor to be the type of a pointer to function with parameter LayerParams& and return type Ptr<Layer>.
All this confusion is one good reason to prefer the newer using-style type alias syntax in new code (if it does not need to be compatible with C++03).  The same type could be defined:
using Constructor = Ptr<Layer>(*)(LayerParams &params);

Comment: @yuqli it is not true that "order of syntax in typedef changes because of the use of the function pointer".  The `using` version just moves the identifier being declared to the front, nothing else changes in the "order of syntax".
Comment: Thanks so much. I figure it out now. I was confused as 1) I don't know about function pointer and 2) the order of syntax in `typedef` changes because of the use of a function pointer. The tip on using alias is also helpful - I just find it in the book "Effective Modern C++". Thanks again & I love Stackoverflow :)
Comment: @M.M Thanks for point this out. Speaking of this, how about `typedef Ptr(*)(LayerParams &params) Constructor`? Is this legal in defining a typedef on function pointer?
Comment: No, that's not legal. These are treated rather differently by the C++ syntax. An ordinary declaration or `typedef` definition uses a "declarator", in which function arguments (or array bounds) always come after the identifier. The part of a `using` type alias after the `=` is a "type-id" which uses an "abstract-declarator", which has no main identifier at all.
Comment: @aschepler Thanks for pointing it out. I now see it's the constraint that "function arguments (or array bounds) always come after the identifier" is what makes this `typedef` looks weird, because for non-function the identifier should come in the end, not in the middle.


Django model joining a one to many relationship for displaying in a template

Question: Not sure what the best way to describe the problem is. I have 2 tables contact and attribute. The contact table has 1 entry per person and the attribute table has 0, 1, or many entries per person. They are joined currently with a "fake" foreign key that isn't really a foreign key. If I need to add the foreign key I will it is not a big deal just dealing with old data and there was no foreign key originally. So the tables are laid out as follows:
contact:
class contact(models.Model):
    contactId = models.AutoField(primary_key=True, db_column='contactId')
    firstName = models.CharField(max_length=255, null=True, db_column='firstName')
    middleName = models.CharField(max_length=255, null=True, db_column='middleName')
    lastName = models.CharField(max_length=255, null=True, db_column='lastName')

attribute:
class attribute(models.Model):
    attributeId = models.AutoField(primary_key=True, db_column='attributeId')
    contactId = models.IntegerField(db_index=True, null=True, db_column='contactId')
    attributeValue = models.TextField(null=True, db_column='attributeValue')

So I have correctly set up the Django models to represent these tables. Now what I need to accomplish is a view and template to loop over these tables such that it generates an xml doc in the following format:
<contacts>
    <contact>
        <contactId></contactId>
        <firstName></firstName>
        <lastName></lastName>
        <attributes>
            <attribute>
                <attributeId></attributeId>
                <attributeValue></attributeValue>
            </attribute>
        </attributes>
    </contact>
</contacts>

So there would be a listing of all contacts and all of the attributes associated with each contact.
I am sure there is a simple way to accomplish this. In other languages I would simple write two looping queries to loop over the contact and then loop over the attributes for each contact. However the company I work for is migrating to a new platform and want the new application written in django/python both of which I am still trying to learn.
Any help that anyone can provide is appreciated.
Comment: What do your models look like? Is there any reason not to define `attribute.contactId` as a `ForeignKey`?
Comment: The only reason it doesn't have a foreign key set is because it is an older database model. Nothing against foreign keys the company has just never them. I added the actual models in the original post.
Answer: Assuming you've set up your django models to use your current database setup, I'd do the following if I didn't have a foreign key set up.
contacts = Contact.objects.all()

for contact in contacts:
    contact.attributes = Attribute.objects.filter(contactId=contact.pk)

return render_to_response("mytemplate.html", {'contacts': contacts })

Alternately, save some queries;
attributes_map = dict(
    [(attribute.contactId, attribute) for attribute in \
        Attribute.objects.filter(contactId__in=[contact.pk for contact in contacts])]
    )

for contact in contacts:
    contact.attributes = attributes_map.get(contact.pk)

return render_to_response("mytemplate.html", {'contacts': contacts })

Template
<contacts>
{% for contact in contacts %}
    <contact>
        <contactId>{{ contact.pk }}</contactId>
        <firstName>{{ contact.firstName }}</firstName>
        <lastName>{{ contact.lastName }}</lastName>

        {% if contact.attributes %}
            <attributes>
            {% for attribute in contact.attributes %}
                <attribute>
                    <attributeId>{{ attribute.pk }}</attributeId>
                    <attributeValue>{{ attribute.attributeValue }}</attributeValue>
                </attribute>
            {% endfor %}
            </attributes>
        {% endif %}

    </contact>
{% endfor %}
</contacts>

Comment: This looks like exactly want I needed. I hadn't realized I could add properties to the queryset that had not originally been defined in the model's class. Is there another method that I should take if I add the foreign keys to the table?
Comment: Yes! Everything is just a python object -- it's a great concept to grasp. You can put random things on your request object, a form passed into a view you don't have control over, etc. Interestingly, I would still use my second method even if you had a foreign key -- the reason is that "backwards" relationships are not automatically queried so to save DB queries, I'd use the 2 step process.
Comment: If it were a situation where we had only one contact, there is a convenience method for reverse foreignkeys which is `fieldname_set`. Every model that has a foreign key pointing to it automatically gets an attribute, `fieldname_set` that you can query in the same manner as `filter`. e.g. `contact.attribute_set.all()` there are some quirks that you should check the docs for if you want to explore further
Answer: It sounds like you are in legacy db hell.
As such, you will probably find it difficult to use standard django model stuff like ForeignKey, but you should still strive to keep code like that out of the view code.
So it would probably be better to define some methods on your contact class, something like...
def attributes(self):
    return Attribute.objects.filter(contactId=self.contactId)

then, you can use that in your templates.
<attributes>
    {% for attribute in contact.attributes.all %}
        <attribute>
            ...
        </attribute>
    {% endfor %}
</attribute>

Though, contact.attributes.all in a template might not be ideal(nailing the relationship in the template). but it would be simple to add into your view to stuff something into the context.
Answer: Yuji's answer is a lot like Nick Johnson's prefetch_refprops for App Engine:
http://blog.notdot.net/2010/01/ReferenceProperty-prefetching-in-App-Engine
I found a way to optimize this code:
attributes_map = dict(
    [(attribute.contactId, attribute) for attribute in \
        Attribute.objects.filter(contactId__in=[contact.pk for contact in contacts])]
    )

Replace it with:
attributes_map = dict(Attribute.objects
    .filter(contactId__in=set([contact.pk for contact in contacts]))
    .values_list('contactId', 'attribute'))

This substitution saves time in the query by:

compacting the list of keys into a set(), and
retrieving only the two fields used by the dict().

It also takes advantage of the fact that .values_list() returns the list of tuples expected by dict(), but this only works if the first field (i.e., 'contactId') is unique.
To extend this solution for multiple foreign keys, create a map for each key field using the code above.  Then, inside of this loop:
for contact in contacts:
    contact.attributes = attributes_map.get(contact.pk)

add a line for each foreign key.  For example,
attributes_map = dict(Attribute.objects
    .filter(contactId__in=set([contact.pk for contact in contacts]))
    .values_list('contactId', 'attribute'))
names_map = dict(Name.objects
    .filter(nameId__in=set([contact.nameId for contact in contacts]))
    .values_list('nameId', 'name'))

for contact in contacts:
    contact.attributes = attributes_map.get(contact.pk)
    contact.name = names_map.get(contact.nameId)

If you've ever had a template generate lots of extra SQL calls for each row of a result set (in order to look up each foreign key's value), this approach will make a huge savings by pre-loading all the data before sending it to the template.


Are there any valid layout and writing programs for ubuntu?

Question: I'm beginning my phd thesis and really need good writing, layout and reference management programms. I would love to do it with Ubuntu but I'm not sure if it'll work.
I tried Lyx & Bibtex/Jabref but it seems to be better for sciences not for humanites (citations are a mess! I tried jurabib but it didn't work). I really, really don't want to use some version of Office.
Do you have suggestions?
Answer: I'm an avid fan of using TexMaker but if you didn't find Lyx and Bibtex to your liking it might not be to your taste. If you're willing to give Latex editors another try This answer on Latex editors will most likely be complete enough to help you out.
Otherwise if you're looking for less scientific orientated I'd suggest one of the following:

Libre Office Writer

Which is readily available in the Ubuntu Software Centre, you just search for LibreOffice and install it, I can't give you an opinion on the referencing as I've never had to use it, but I'd assume it works well enough.

Kingsoft Writer

Which has an interface that looks similar to Microsoft Office as you can see in the following screenshot:

I've personally found Kingsoft seems to be preferred by people used to working on Windows as their main operating system. Although I prefer using Latex personally, the referencing in Kingsoft is quite intuitive and easy to follow as you can see here in the reference tab:

You can download the .deb file to install it via software centre by clicking on the button below:

Please note: Installing Kingsoft will install a PPA so if you have any doubts on security please read: Are PPA's safe to add to my system and what are some "red flags" to watch out for? if you're unsure of what this implies.


Kubernetes Helm with Gitlab Charts in AWS - how to configure the serviceType

Question: I"m new to Kubernetes and AWS, treat me like a noob.
I've got Kubernetes running in AWS with the following names:
>  kube kubectl get pods --all-namespaces
NAMESPACE       NAME                                                                READY   STATUS    RESTARTS   AGE
ingress-nginx   nginx-ingress-controller-76c86d76c4-s6kvk                           1/1     Running   0          28h
kube-system     calico-node-xxzzz                                                   1/1     Running   0          28h
kube-system     dns-controller-5czzzzzzfbd-t7pf8                                    1/1     Running   0          28h
kube-system     etcd-manager-main-ip-11-11-11-11.eu-west-1.compute.internal         1/1     Running   0          28h
kube-system     kube-apiserver-ip-11-11-11-11.eu-west-1.compute.internal            1/1     Running   2          28h
kube-system     kube-controller-manager-ip-11-11-11-11.eu-west-1.compute.internal   1/1     Running   0          28h
kube-system     kube-dns-111116bb49-pbt2l                                           3/3     Running   0          28h
kube-system     kube-dns-autoscaler-11111111-x8                                     1/1     Running   0          28h
kube-system     kube-proxy-ip-11-11-11-11.eu-west-1.compute.internal                1/1     Running   0          28h
kube-system     kube-scheduler-ip-10-84-37-60.eu-west-1.compute.internal            1/1     Running   0          28h

My goal is to install Gitlab via Charts on Kubernetes. However, the issue i'm up against is the routing. Here it states I need to set a serviceType field in a file.
But how can I determine the correct value specified in that file?
Do I need to create a loadbalancer in AWS? Or is it already present somewhere, e.g. is the nginx Ingress controller?
I can install Gitlab via helm
helm upgrade --install gitlab gitlab/gitlab \
  --timeout 600 \
  --set global.hosts.domain=my_domain.com \
  --set global.hosts.externalIP=126.96.36.199 \
  --set email@example.com \
  --namespace=gitlab \
  --debug

However, the domain I've provided is not reachable via my browser, because I didn't provide an serviceType for the loadbalancer. Also, I'm uncertain if my external IP is correct.
Answer: You have an nginx ingress controller running already.  Is it working?  If so, you should probably use that instead of a new load balancer.
1) Configure your domain so that it is pointing to your ingress load balancer.  If you are using route53, you can set a wildcard A Record so that *.mydomain.com goes to the load balancer.
2) Add the appropriate ingress section to your values.yaml: https://gitlab.doc.ic.ac.uk/help/install/kubernetes/gitlab_chart.md#ingress-routing
3) Use serviceType=ClusterIP.
If you can't or don't want to use that Ingress Controller, then yes, serviceType=LoadBalancer is appropriate.  It will create an AWS ELB for you.  You'll need to add an A record for your domain pointing to that ELB.
Comment: It's working, on port 80! I got no clue how to ensure HTTPS works, but I'll find that out later... thanks.


Can I nondimensionalize the model only when the total population is constant?

Question: I have the following ODE model that I nondimensionalized and I received feedback on it saying that "Author scale the population of human by divide each class with the total of
human population (N_h). Author can do this only if the total of human population is
constant. As equation (10) shows, there exist a death rate due to diseases in their
model. Hence, all calculation which use a scale of the human population should be
recalculated." I am wondering if the reviewer wants me to not do nondimensionalization and use equations 1-9 to do further analysis instead of using equations 15-19. Any help would be appreciated.
Answer: Yes, that is a grave error. You either use constants in the non-dimensionalisation, which means that $N_h$ and $N_v$ reduce to non-constant dependent variables $n_h$ and $n_v$. Or you need to apply the quotient rule
$$
\frac{d}{dt}\frac{X}{N}=\frac{\dot X}{N}-\frac{\dot N}{N}·\frac{X}{N},
$$
You can not leave out the second term, you would get a different dynamic.
Comment: Can I take b_h=mu_h so that N_h becomes constant and then use nondimensionalization?
Comment: It would only become constant if the derivative were zero. But that does only happen in the non-interesting case where $I=0$ for all times and variants.