Variable access across multiple files

Question: I was trying out programs based on extern and as I understand, this is helpful when accessing variables across multiple files having only one definition.
But I tried a simple program as below without extern and  thing seem to work when I expected it would fail during linking process
file5.c:
#include <stdio.h>
#include "var.h"

int a = 20;

int main() {
  printf("\n File5.c a = %d", a);
  test();
  return 0;
}

file6.c:
#include <stdio.h>
#include "var.h"

int test() {
  printf("\n File6.c a = %d",a);
}

var.h
int a;

As I have included var.h in all header files without extern, int a would be included in both the .c file and during linking, compiler should have thrown a warning or error message but it compiles file without any issue.
Shouldn't var.h have the following extern int a?
Comment: @WhozCraig: more or less.  The multiple declarations with at most one initialized is identified as a 'common extension' in §J.5.11 of the 2011 standard.
Comment: @JonathanLeffler I knew the translation unit rules weren't identical in C and C++, but never considered they may differ with *this*. Is this in the standard as implementation-dependent? It would seem somewhat odd if it were.
Comment: @JonathanLeffler yeah I think I get it already. The differences in C and C++ between declarations, definitions, and when something is one, the other, or both. Ex. In C I can throw `int a;` into *all* my source files, with *one* (or none) having an initializer, and it all links. Add an initializer to a second one and kerboom, multi-symbol link error. In C++ it will link-error regardless unless all-but-one is `extern`.
Answer: It is generally best if the header uses extern int a;.  See also How do I share a variable between source files in C?
The standard says:

ISO/IEC 9899:2011 §6.9.2 External object definitions
Semantics
  ¶1 If the declaration of an identifier for an object has file scope and an initializer, the
  declaration is an external definition for the identifier.
¶2 A declaration of an identifier for an object that has file scope without an initializer, and
  without a storage-class specifier or with the storage-class specifier static, constitutes a
  tentative definition. If a translation unit contains one or more tentative definitions for an
  identifier, and the translation unit contains no external definition for that identifier, then
  the behavior is exactly as if the translation unit contains a file scope declaration of that
  identifier, with the composite type as of the end of the translation unit, with an initializer
  equal to 0.

Thus, what's in the header is a tentative definition of the variable.  At the end of the translation unit (TU) for file5.c, you have no longer got a tentative definition; the 'external definition' specified by int a = 20; has specified that.  At the end of the TU for file6.c, you have a definition equivalent to int a = 0;.
When you try to link file5.c and file6.c, you should run into multiple definitions of a.  However, there is a common extension, documented in the standard in Annex J:

J.5.11 Multiple external definitions
¶1 There may be more than one external definition for the identifier of an object, with or
  without the explicit use of the keyword extern; if the definitions disagree, or more than
  one is initialized, the behavior is undefined (6.9.2).

Your compiler is providing the extension identified by §J.5.11, and therefore (legitimately) not complaining.
580
Comment: +1 Helluva nice citation, btw. Can't get more applicable than that. Thanks.
Comment: +1 for this "The multiple declarations with at most one initialized is identified as a 'common extension'" .thanks
Comment: Thanks for the explanation. This definitely seems to be gcc specific thing and might not be applicable on different compiler. Quite a  tricky thing.
Comment: Actually, just for once, this isn't so much GCC-specific as Unix-specific.  The C standard officially supports a strict ODR (One Definition Rule) like C++ does, but the legacy Unix behaviour was more usually the §J.5.11 behaviour.  The C standard recognizes this by including it as a documented 'Common Extension' behaviour.  GCC is merely following the native compilers on Unix systems (where GCC was not originally the native compiler), and provides it on Linux for compatibility with other Unix systems.

Variable access across multiple files


Direct ethernet connection to Raspberry PI running Arch linux

Question: I need a good tutorial that will explain on how to setup a ssh connection via a direct ethernet connection to a  Raspberry Pi with a static IP running Arch Linux. I have tried a few but all seem to fail. Any Suggestions?
Comment: The Arch wiki is always a good place to start. Take a look at [netctl](https://wiki.archlinux.org/index.php/netctl#Configuration). As it stands, this question is not appropriate for SO. Please see: https://stackoverflow.com/help/on-topic
Answer: What kind of ethernet connection? Do you have a crossover cable? If yes you can connect it directly to your computer and assign it an IP address. If not, you can connect your Pi to the same router as your computer is connected to, and install nmap on your machine. You can run the following command to identify your raspberry pi's IP address (assuming your router assigns IP addresses in 192.168.1.x space):

nmap -sn 192.168.1.1/24

The Raspberry Pi will show up with its ethernet MAC address and most likely also that the address belongs to RPI foundation. You can then ssh into it.
Comment: Got the same question, but I am stuck at the "assign it an IP address" part. How do I do that?


SSL ACME Manager / Relay / Deployment

Question: I would like to ask if anyone is aware of a tool, which can obtain and manage certificates from an upstream instance (like Let's Encrypt via HTTP-01 or DNS-01) and make them available trough eg. local ACME. Furthermore I would wish I could add a deployment mechanism, to push the certificates to devices such as Printers or Switches, a hook script seems sufficient.
I am looking for this intermediate cartificate management as I ran into cases, where I want a hostname issued to multiple machines, e.g.

Host A: hosta.intern. + service.intern.
Host B: hostb.intern. + service.intern.

These domains are of cause insede the scope of let's say example.com, so the domain names themselfs are public and under my control. Just the servers aren't reachable from WAN.
While I could acchieve this by using DNS-01 validation, I'm not so sure every DNS provider offers the possibility to create such granulated API accounts.
Answer: It is not possible to use Let's Encrypt certificates for internally facing websites/services.  Due to the nature of certificate trusts, you'll need to create a self-signed CA that is trusted by browsers, etc. within your LAN.
Additionally, there is no automation (at least that I'm aware of) that would deploy certificates to printers, switches, etc.  Most of these are written internally by enterprises due for audit reasons, etc.
Keep in mind the scope of Let's Encrypt: to help the public internet become more secure through enforced SSL connections.  What you're describing is outside that scope.
Comment: This is not entirely accurate. It's true that Let's Encrypt cannot be used on domains that are not externally valid, but if I set up in public DNS a CNAME from *.internal.example.com to tls-manager.example.com, I could build a solution where an internal host foo asks tls-manager for a certificate for foo.internal.example.com and tls-manager obtains it and provides it to foo. I found this question looking for precisely such a solution.

SSL ACME Manager / Relay / Deployment


New disks not recognized on Solaris 10 update 11

Question: We are installing Solaris 10 update 11 (x86-64) and have a small problem.
new disks added (or disks resized) are not being recognized by the system automatically.
We have to reboot and configure them manually.
While on Solaris 10 update 9 & update 10, it was working fine.
as long as we added a new disk, we could immediately see it in "format" (or after a small "devfsadm -c disks").
On the update 11, nothing.
I have tried all things like :

cfgadm -al
devfsadm
devfsadm -c disks
drvconfig
disks
svcadm enable hotplug
etc..

Still can't see any disks added.
(nb : it's virtual machines on vmware)
and it was working fine with builds 9 & 10.
Any heard of that bug ?
Also, what else can I try to force a re scan of disks ? except a reboot.
Comment: More details would help.  What kind of disks?  (iSCSI, etc)
Comment: hi, regular SCSI disks.
Comment: Which VMWare SCSI controller are you using for your VMs?  Do the disks appear under `raidctl` or, if you have it and are using an LSI controller, `lsiutil`?
Comment: hello, thanks for your answer.
Comment: hi, can't see them in raidctl unfortunately (only the first 3 disks, i can't see the additional one). also, controler is LSI Logic Parallel. i can try to add a new controler (different one) with new disks. but the LSI should work (i don't have lsiutil, gonna try to download it).
Comment: I'd also recommend that you open a trouble ticket with Oracle if you have a support contract.
Comment: its apparently a bug with LSI Logic Parallel SCSI Controller. LSI Logic SAS works fine. i'll create a ticket @ Oracle to see if they have a patch
Answer: Answering my old question.
It was back in the days a bug with the "LSI Logic Parallel" SCSI Controller (not compliant with build 11). "LSI Logic SAS" worked fine.
Its worth mentionning that it was on an old VMware ESX 6.0 and we just had to change the controller type to make it work.


Valid method to analyze spatial correlations in images?

Question: Is there a reasonable way to quantify the amount of local correlations in an image?  For example, I want to justify the correlations between a neighbourhood of pixels is much higher than the correlations between pixels in entirely different regions of the image.
Would showing the xcorr2(A,A) as the 2-d autocorrelation of the image be a valid way to show this?  ie: if if there are large values mostly located at the center of the matrix.
Comment: It sounds to me like you might be interested in geographically weighted regression. http://ncg.nuim.ie/ncg/GWR/whatis.htm
Comment: [Local Moran's I](http://pysal.org/1.1/library/esda/moran.html) statistic
Answer: One straightforward way of doing this is to consider arbitrarily-sized patches of the image.  For example, let's say we are interested in all 9*9 regions of pixels that can be taken from the image.  Extract each of these image patches, and transform each image patch to a row vector.  Consider the entire set of image patches (8464 row vectors for a 100*100 pixel image) as a matrix M.
Compute the correlation (or covariance, corr(M) and cov(M) in Matlab) between each of the columns in M.  For your specific question, look at the three columns of the correlation/covariance matrix corresponding to the central pixel in the image.  Reshape these back to the size of the image patch, and plot these.  For natural images, you should find that the central pixel is highly correlated with adjacent pixels, and that the correlation decreases as distance from the central pixel increases across each of the three color channels.


The difference between a set and a subset.

Question: I am currently taking real analysis, and I'm afraid this might be a very elementary question, but the further I go in the course the more confused I become about the difference between a set in $\mathbb{R}^p$ and a subset of $\mathbb{R}^p$.
Furthermore, sometimes the notes simply say, for example, "$U_n \subseteq \mathbb{R}^p $". Is $U_n$ a subset or a set?
Finally, what is the notation for a set and a subset, respectively?
Thank you in advance!
Comment: A subset is a set; "to be a subset of" is a binary relation : $A \subseteq B$ iff every element of $A$ is also an element of $B$.
Comment: No difference - a subset simply assumes there is a second set to be a subset of, if you see what I mean.
Answer: By definition, a set is simply a collection of elements.
By definition, a set $A$ is a subset of a set $B$ if and only if every element in $A$ is also in $B$.  In other words, $A \subseteq B$ iff $x \in A$ implies $x \in B$.
Note that every set is in fact a subset.  Because, trivially, $A \subseteq A$ for all sets $A$.  In words, "every set is a subset of itself."
Also, the empty set $\varnothing$ is a subset of every set $A$, because the empty set vacuously satisfies the definition of subset.  In words, "the empty set is a subset of every set."
Oftentimes questions will ask you for examples of nontrivial subsets.  This typically means any subset that is neither the entire set itself nor the empty set.

I am currently taking real analysis, and I'm afraid this might be a very elementary question, but the further I go in the course the more confused I become about the difference between a set in $\mathbb{R}^p$ and a subset of $\mathbb{R}^p$.

"A set in $\Bbb R^p$" and "a subset of $\Bbb R^p$" mean exactly the same thing.
To further expand on that:
If $A$ is "a set in $\Bbb R^p$" then that means all the elements of $A$ are also in $\Bbb R^p$.  So $A$ satisfies the definition of being a subset of $\Bbb R^p$, and we have $A \subseteq \Bbb R^p$.
If $A$ is "a subset of $\Bbb R^p$" then, well, this is just a more direct way of saying exactly the same thing:  $A \subseteq \Bbb R^p$

Furthermore, sometimes the notes simply say, for example, "$U_n \subseteq \mathbb{R}^p $". Is $U_n$ a subset or a set?

Both.  $U_n$ is itself a set.  And $U_n$ is a subset of $\Bbb R^p$.  That's actually exactly what the notation means.  When you see something like "$A \subseteq B$" you can read it as "$A$ is a subset of $B$."

Finally, what is the notation for a set and a subset, respectively?

I'm not entirely sure what you're getting at here but there really isn't any notation for a set, at least not in the same way that there is for subsets.  We generally denote sets with capital letters (except for the empty set, typically denoted $\varnothing$ or $\emptyset$), but this isn't really a hard rule.
Subset-related notation:
$A \subseteq B$ means $A$ is a subset of $B$.
$A \supseteq B$ means $A$ is a superset of $B$ (which is just another way of saying $B$ is a subset of $A$).
Some authors use $\subset$ and $\supset$ instead of $\subseteq$ and $\supseteq$, respectively.
The notations $A \nsubseteq B$ and $A \not\subset B$ can be used to indicate $A$ is not a subset of $B$.  There are corresponding superset notations $\nsupseteq$ and $\not\supset$.
The notation $A \subsetneq B$ means $A$ is a proper subset of $B$.  This just means that $A \subseteq B$ and $A \ne B$.  There is a corresponding superset notation $\supsetneq$.
Note: Unfortunately, some authors (although I think this is rare these days) use $A \subseteq B$ to indicate $A$ is a subset of $B$ and use $A \subset B$ to indicate $A$ is a proper subset of $B$.
Answer: All subsets are sets. But to be a subset is a relation between sets, like numbers being less than or equal to each other. The proper way to say it is "$A$ is a subset of $B$", as it doesn't make sense to call a set a subset without specifying what it's a subset of.


Laravel search function to filter foreign key data by keywords

Question: I have a table in my database, which contains only foreign keys. I want to create a search function to filter the data using keywords.
Can anyone solve this ?
Thank you.
This is my table:
vehicle_table :
id | model_id | variant_id
1  | 1        | 1

model_table :
id | make_id | name
1  | 1       | Beat

make_table :
id | name
1  | Chevrolet

variant_table :
id | name
1  | 1.0 PS

Comment: Post your code what you tried for this!
Comment: Can you add details about the foreign key and tables structures in details ?
Answer: Please provide what have you tried till now to understand your problem. According to what I've understood, you can build a query for vehicle_table and join your others table and do your filters like below:
$vehicles = DB::table('vehicle_table')
            ->select('model_table.name as model_name', 'make_table.name as make_name', 'variant_table.name as variant_name')
            ->leftJoin('model_table','model_table.id','=', 'vehicle_table.model_id')
            ->leftJoin('make_table','make_table.id','=', 'model_table.make_id')
            ->leftJoin('variant_table','variant_table.id','=', 'vehicle_table.variant_id');
if($make_name){
    $vehicles->where('make_table.name', 'like', '%' . $make_name . '%');
}

if($model_name){
    $vehicles->where('model_table.name', 'like', '%' . $model_name . '%');
}

if($variant_name){
    $vehicles->where('variant_table.name', 'like', '%' . $variant_name . '%');
}

$results = $vehicles->get();

$make_name, $model_name, $variant_name would be your search form values.
--UPDATE--
If you want to search string(more than one word) through all tables you can do the following:
$words = explode(' ', $search);
$vehicles = DB::table('vehicle_table')
                ->select('model_table.name as model_name', 'make_table.name as make_name', 'variant_table.name as variant_name')
                ->leftJoin('model_table','model_table.id','=', 'vehicle_table.model_id')
                ->leftJoin('make_table','make_table.id','=', 'model_table.make_id')
                ->leftJoin('variant_table','variant_table.id','=', 'vehicle_table.variant_id');
foreach($words as $word){
    $vehicles->where('make_table.name', 'like', '%' . $word. '%');
    $vehicles->orWhere('model_table.name', 'like', '%' . $word. '%');
    $vehicles->orWhere('variant_table.name', 'like', '%' . $word. '%');
}
$results = $vehicles->get();

Comment: This is working. But I need one parameter to search make, model and variant. instead of $make_name, $model_name, $variant_name. How can I do this?
Comment: $users->where('make_table.name', 'like', '%' . $search . '%')
        ->orWhere('model_table.name', 'like', '%' . $search . '%')
        ->orWhere('variant_table.name', 'like', '%' . $search . '%');

I'm doing like this, but it's working for single word. I want to search make, model & variant all at once. it's not taking multiple words.


How to alter the value used to sort a view?

Question: I have properties that have a rental price. The rental price is a field collection with a value (price) and period (per month, per week). You can sort the properties by price (low to high, or high to low) using an exposed filter. The problem is that if someone enters 2000 per month it is being registered as more expensive than a property that is 600 per week (2400 per month). So I want to divide the monthly values by 4 in order to have equal calculation.
What hook do I need to use in order to process the value field just before it is used to sort the properties?
Answer: you can change query of view by this
hook_views_query_alter(&$view, &$query)
This hook is a core hook of views api but you can override it in you custom moduel like MYMODULENAME_views_query_alter
In this hook you can get parameters by exposed filter's and change views query based on these parameters.


Calling `sed` from inside a python program

Question: I have the following code snippet in bash which I use for processing a JSON file.I want to do the exactly same functionality in python. Can I call sed from python? Or how do I achieve the same thing.
for file in /home/Work/ts_parts/*
do
        event=$(cat $file | jq '.Event')
        name="$(basename "$file")"
        if [[ "$event" =~ "aaa.bbb.ccc.dddd" ]]  || [[ "$event" =~ "eeee.ffff.gggg.hhhh" ]]; then
                echo "Coming Here"
                jq ".url = \"$(jq '.Msg' $file | sed 's/.*proxy=\([^&]*\).*/\1/')\"" $file  > ts_parts/out_$name
        else
                jq ".url = null" $file > ts_parts/out_$name
        fi
        rm -f $file
done

Answer: You could use regular expressions:
import re

s = "abc proxy=123&456"                 # Test string

mat = re.search(r'proxy=([^&]*)', s)
print(mat.group(1))                     # 123

This code only does what your sed replacement (extraction) does.  Anything else from the script you'd also have to implement, or call programs directly.
Comment: -how do I add a new key url to the json data and dump it to the file in python?
Comment: @liv2hak you'll want to look into the [`json`](https://docs.python.org/2/library/json.html) module -- `json.load` to load a json into a dictionary from a file, manipulate the dictionary however you want, then `json.dump` to save the modified json.


Probability and deductible

Question: For a certain health insurance policy, losses are uniformly distributed on the interval
[0, b]. The policy has a deductible of 180 and the expected value of the unreimbursed
portion of a loss is 144.
Question is on what does it mean unreimbursed portion,is that portion less than the deductible?
Comment: Example:  say that $b=500$.  Then if your claim is $180≤x≤500$ your unreimbursed loss is $x-180$.  The measure here is $\frac {dx}{500}$ so the expected value of the unreimbursed loss is $\int_{180}^{500} (x-180)\frac {dx}{500}=102.4$.  (so we see that $b>500$).
Comment: Say you file a claim of size $x$ (drawn uniformly from $[0,b]$).  then your payout is $\max(x-180,0)$.  Therefore, your unreimbursed loss is $x-\max(x-180,0)$.  That is a variable which you can compute the expectation of.  I assume the point is to then solve for $b$
Comment: Question is what have you tried in attempting to resolve this problem on your own? In addition, please spend a moment of your time in order to provide the mathematical meaning of 'The policy has a deductible' and 'unreimbursed portion'.
Answer: Let Y'=Unreimbursed portion and X=the total loss incurred.
Y' =144 = X, for X<180
     or 180,for X>180.
144 = integral of (X/b)dx+180*prb(X>180)dx with limit 0 to 180.
prb(X>180) = (b-180)/b, this is done using CDF of the uniform distribution.
Doing the integration and evaluation gives b=450
Comment: Please use [MathJax](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference).

Probability and deductible


replace specific columns values

Question: I want to loop through dataset and replace specific columns value with one the same [value]

The whole dataset has 91164 rows.
The case here i need to replace vec_red ,vec_greem, vec_blue with new_data
new_data has shape of (91164,) and its number of appearance equals index of my dataframe.
For e.g. last item is

This 1 need to be value in val_red , val_blue, val_green.
So I want to loop through whole dataframe and replace the calues in columns from 3 to 5.
What I have is :
label_idx = 0
for i in range(321):
    for j in range(284):
      (sth here)   = new_data[label_idx]
        label_idx += 1

The case here is that I am updating my pixel values after filtration. Thank you.
The shape of 91164 is result of multiplication 321 * 284. These are my pixel values in an RGB image.
Answer: Looping over rows of a dataframe is a code smell. If the 3 columns must receive the same values, you can do it in one single operation:
df[['vec_red', 'vec_green', 'vec_blue']] = np.transpose(
    np.array([new_data, new_data, new_data]))

Demo:
np.random.seed(0)

nx = 284
ny = 321
df = pd.DataFrame({'x_indices': [i for j in range(ny) for i in range(nx)],
                   'y_indices': [j for j in range(ny) for i in range(nx)],
                   'vec_red': np.random.randint(0, 256, nx * ny),
                   'vec_green': np.random.randint(0, 256, nx * ny),
                   'vec_blue': np.random.randint(0, 256, nx * ny)
                   })

new_data = np.random.randint(0, 256, nx * ny)
print(df)
print(new_data)
df[['vec_red', 'vec_green', 'vec_blue']] = np.transpose(
    np.array([new_data, new_data, new_data]))
print(df)

It gives as expected:
       x_indices  y_indices  vec_red  vec_green  vec_blue
0              0          0      172        167       100
1              1          0       47         92       124
2              2          0      117         65       174
3              3          0      192        249        72
4              4          0       67        108       144
...          ...        ...      ...        ...       ...
91159        279        320       16        162        42
91160        280        320      142        169       145
91161        281        320      225         81       143
91162        282        320      106         93        68
91163        283        320       85         65       130

[91164 rows x 5 columns]
[ 32  48 245 ...  26  66  58]
       x_indices  y_indices  vec_red  vec_green  vec_blue
0              0          0       32         32        32
1              1          0       48         48        48
2              2          0      245        245       245
3              3          0        6          6         6
4              4          0      178        178       178
...          ...        ...      ...        ...       ...
91159        279        320       27         27        27
91160        280        320      118        118       118
91161        281        320       26         26        26
91162        282        320       66         66        66
91163        283        320       58         58        58

[91164 rows x 5 columns]

Comment: How can I display it as an image?

replace specific columns values


Disambiguation of numerical base and index variable in mathematics

Question: Is there a proper way to distinguish base from index variable?
For this instance, let's say we have a paper that deals with cryptographic keys both in different bases and with different indices.
For different bases we might say that our binary function outputs a binary key and our ternary function outputs a ternary key:
$$f_2(x) = K_2$$
$$f_3(x) = K_3$$
For indices we might later describe a process in which we have sub-functions that generate sub-keys (think hash family):
$$f_1(x) = K_1$$
$$f_2(x) = K_2$$
$$f_3(x) = K_3$$
Some things I've considered:

Specifying in english that the subscript is either base or index ($i$).
Prefixing base with 'b': $K_{b3}$.
Prefixing the index with 'i': $K_{i3}$
Using lowercase for keys with indices and uppercase for keys with base. I don't like this because it leads a reader to mistakenly infer that a based key can be described in some form of sub-keys.

My favorite of the options I listed is 1.
Comment: Mathematicians use subscripts and superscripts for many, many different things. Usually the meaning is clear from the context. For example, in algebra if you saw "$x^i$", you would expect this to be some number $x$ raised to the $i$-th power. But in differential geometry, you would expect it to be the $i$-th coordinate in some coordinate system. When the context of your subscript or superscript is not sufficient to distinguish between possible meanings, one should always explain which is meant, or if needed, use a different notation than subscripts or superscripts.
Comment: There is temptation to provide a key that is both based and indexed, but as I see no immediate use for this complication, I will deem it out of scope. Shots welcome, not required.
Comment: No one has offered a differing opinion since your posting. I'd like to select this as the answer. Would you mind posting it as such?
Comment: Okay. There it is.
Answer: Mathematicians use subscripts and superscripts for many, many different things. Usually the meaning is clear from the context. For example, in algebra if you saw "$x^i$", you would expect this to be some number $x$ raised to the $i$-th power. But in differential geometry, you would expect it to be the $i$-th coordinate in some coordinate system.
When the context of your subscript or superscript is not sufficient to distinguish between possible meanings, one should always explain which is meant (your option 1), or if needed, use a different notation than subscripts or superscripts.


What is the correspondence between application, process, and Java virtual machine?

Question: In Android, we can use android:process attribute to specify a process for the <activity> component. So what is the correspondence between application, process, and Java virtual machine? And is there any difference between common Java application and Android application about this question?
Answer: The Java of Android is an implementation of the Java language (which is just a standard) but it doesn't use the JVM (which is another implementation of the Java language).
Android actually compiles Java to a different bytecode and interprets this other bytecode at runtime using the ART (Android Runtime).
The "Java" of Android is completely different than the actual Java.

So what is the correspondence between application, process, and Java virtual machine?

Android is a fancy Linux distribution for smartphones. It is like Ubuntu for smartphones. Instead of using pre-existing desktop environments, Google wrote their own targeting smartphones and tablets. Ubuntu is just a fancy package manager. You can make your own Ubuntu in 5 hours. It is called Linux From Scratch (LFS).
The Java virtual machine is probably written in C or C++. It makes system calls just like any other C program out there. It is really nothing fancy. Just a very big software which interprets bytecode and translates it to actual syscalls.


Suppressing output in python subprocess call

Question: For the following command:
subprocess.call(shlex.split(
            """/usr/local/itms/bin/iTMSTransporter -m lookupMetadata
              -apple_id %s -destination %s"""%(self.apple_id, self.destination))

It prints the entire output into the Terminal window. How would I suppress ALL output here? I tried doing subprocess.call(shlex.split(<command> > /dev/null 2&1)), but it didn't produce the required results. How would I do this here?
Answer: You can use the stdout= and stderr= parameters to subprocess.call() to direct stdout or stderr to a file descriptor of your choice.  So maybe something like this:
import os

devnull = open(os.devnull, 'w')
subprocess.call(shlex.split(
    '/usr/local/itms/bin/iTMSTransporter -m lookupMetadata '
    '-apple_id %s -destination %s' % (self,apple_id, self.destination)),
  stdout=devnull, stderr=devnull)

Using subprocess.PIPE, if you're not reading from the pipe, could cause your program to block if it generates a lot of output.
Update
As @yanlend mentions in a comment, newer (3.x) versions of Python include subprocess.DEVNULL to solve this problem in a more convenient and portable fashion.  In that case, the code would look like:
subprocess.call(shlex.split(
    '/usr/local/itms/bin/iTMSTransporter -m lookupMetadata '
    '-apple_id %s -destination %s' % (self,apple_id, self.destination)),
  stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

Comment: you can just use os.devnull
Comment: Using a predefined constant like this means that if the null device were something other than `/dev/null`, this would continue to work without needing to modify your code.  For example, I believe this returns something different but still useful under Windows.
Comment: Hey look I learned something.
Comment: You can directly use `subprocess.DEVNULL` to suppress the output, no need to use os.devnull and open it.
`subprocess.call(shlex.split(
    '/usr/local/itms/bin/iTMSTransporter -m lookupMetadata '
    '-apple_id %s -destination %s' % (self,apple_id, self.destination)),
  stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)`

see https://docs.python.org/3/library/subprocess.html#subprocess.DEVNULL
Comment: what is the workaround for `subprocess.run` instead of `subprocess.call`? In that case it doesn't suppress the output
Comment: You would need to tell `subprocess.run` to collect the output.  See the documentation for the `stdout` and `stderr` parameters.
Answer: What worked for me is appending 2>/dev/null at the end of the command.
Comment: For future readers: this approach isn't guaranteed to work on all platforms/shells, even though it'll probably work on anything recent and POSIX-y.
Comment: (also, this only work with `shell=True`, which you want to avoid when possible)
Comment: @month See [the docs for details](https://docs.python.org/2/library/subprocess.html#frequently-used-arguments). In addition to the security issues noted there, you can also get unexpected behavior if your command string includes any shell metacharacters, and you can also run into annoying quoting issues.
Comment: @larsks why should I avoid `shell=true`? This solution is simple as hell and worked for my case.
Comment: @larsks thanks! In my case, there is no vulnerability through shell injection, since my command string is not constructed but hard coded (it actually just starts a necessary docker). But I'm definitely aware of the risk, now.
Comment: How? provide an example


Anonymous LDAP query - Windows Server 2012

Question: so I have Windows Server 2012 which is running on my virtual machine. I want to have possibility to make anonymous query against LDAP. For example if I'm using software like Softerra LDAP Browser I need to run this program as domain user to be able browse AD groups/users etc. But my point is that I want to be able to do it also as non-domain user(anonymous), or at least using domain user credentials from machine that is not part of the domain.
I was looking for this option in user/group policies, I've grant my user with all privileges that I found - but always with the same result - I was unable to browse LDAP.
So my question is - what have to bet set/changed to be able make anonymous queries against Windows Server LDAP?
Answer: I don't know about anonymous queries, but you said that  you would also be happy using a domain user from a non-domain machine.
That should work fine.  Create a domain user with sufficient privileges to see what you want.
Install a certificate on your LDAP server (or use Kerberos, but that's more annoying on the non-domain machine).
Then do LDAP authentication using the username and password of that domain user.
It works well.
Comment: I tried that. But i got "Invalid username or password". But if I run this software("Run as..." from context menu) as this same user - everything works fine. But that's my issue - I want to be able to use same credentials when this program is running in my local machine user context.
Comment: I'd recommend debugging this using an LDAP client that does not use domain credentials; that uses username and password you specify to the client and is entirely uneffected by run-as


Determine the limit of a piecewise defined function

Question:
In the piecewise function below, $k$ is a constant.
  $$f(x)=\begin{cases}\dfrac{x^2-k^2}{x-k},&x\neq k,\\ 4-k,&x=k.\end{cases}$$ What is the value of the limit $\lim_{x\to k^-}f(x)$?

$-2k$.
$2k$.
$0$.
Limit does not exist.

(The above text was originally posted as this image.).
I have a question from a released exam that I don't understand. I know the answer which is $2k$ but then I don't understand how to get the answer. It would be great if someone could explain it.
Comment: Please type up the question instead of asking people to click through.
Comment: **Welcome to Mathematics Stack Exchange!** Link only questions invite down votes and close votes. A quick [tour](https://math.stackexchange.com/tour) will enhance your experience. Here are helpful tips to [write a good question](https://math.meta.stackexchange.com/questions/9959/how-to-ask-a-good-question/9960#9960) and [write a good answer](https://stackoverflow.com/help/how-to-answer). For equations, use [MathJax](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference).
Comment: Welcome to MSE.  Please don't post images when you can easily type the material, as here.
Comment: What is it that you don't understand about the question?
Comment: I don't understand how the answer is 2k
Comment: Hint.  $(x + k)(x - k) = x^2 - k^2$
Answer: Hint: Cancelling out the numerator and denominator in the first equation, you get $x+k$ when x does not equal to k.
You are also approaching k from the left.
Hope that helps


How do SYSCALL/SYSRET instructions perform across x86 CPUs?

Question: SYSCALL and SYSRET (and their 32-bit-only Intel counterparts SYSENTER and SYSEXIT) are usually described as a "generally faster" way to enter and exit supervisor mode in x86 processors than call gates or software interrupts, but the exact figures underlying this claim remain largely undocumented. In particular, all of the Intel or AMD optimization guides I was able to find contain no mention of these instructions at all. So:

How many cycles (estimated) do SYSCALL and SYSRET take across recent Intel 64 microarchitectures? This is probably measurable by direct experimentation, but there are quite a few of different CPUs to test.

Depending on the order of magnitude of this number, more detailed questions may be relevant:

Do they incur a complete pipeline stall, or any other kind of stall?
How, if at all, do they interact with branch prediction (e.g. the return stack buffer) and fetch logic?
What about latencies, data dependencies, serialization?
&tc.

Assume 64-bit code on the userspace side, no additional address-space switches (writes to CR3) and even matching SYSCALL and SYSRET pairs if it matters.
Comment: There is a paper from 2010 about real syscall costs: http://www.cs.cmu.edu/~chensm/Big_Data_reading_group/papers/flexsc-osdi10.pdf "FlexSC: Flexible System Call Scheduling with Exception-Less System Calls". They show that syscalls has negative impact on IPC.
Comment: https://lkml.org/lkml/2002/12/9/13 - that's the orig posting with the benchmarks. These numbers would vary somewhat these days, I guess. Agner Fog's latency/throughput tables should give you an idea as well.
Comment: (The point about branch prediction across `SYSCALL` turned out to be much more security-relevant than I imagined in 2013...)
Comment: @FrankH. I'd expect these figures to vary _considerably:_ P4's pipeline is much less friendly to context switches than that of e.g. Sandy Bridge. And the 600-something cycles for `getpid()` look doubtful when [Bachmann and Walfield](http://www.walfield.org/papers/200804-bachmann-fast-capability-transfer.pdf) report 250 or so for _two_ system calls. Sadly, Agner Fog hasn't measured the `SYS*` instructions.
Comment: I said I do expect them to vary - the reference above is almost 11 years old. The difference between _somewhat_ and _considerable_ I'd leave to the eye of the beholder :)  In that sense, I've merely given the link because it describes the benchmark done _back then_ - which means you could repeat it, _right now_, on current CPUs, if you like / if you have them available. Not aware of anyone having done that lately, though.
Comment: new copy of osgx's now-dead link: [FlexSC: Flexible System Call Scheduling with Exception-Less System Calls](https://www.usenix.org/legacy/event/osdi10/tech/full_papers/Soares.pdf) is a paper that has some measurements/simulations of lowered IPC *after* a system-call returns, due to cache eviction and the out-of-order window having to refill.  (And these days also maybe worse branch prediction if Spectre mitigation wiped it out.)
Answer: I was curious too so I've written some basic bare-metal code to benchmark it: just a loop that calls syscall 1000000 times in a loop, with the syscall handler just running sysret and nothing else. On my Ryzen 7 3700X it averages 78 cycles for the call+return.
Obviously that's an artificial benchmark, because a real system call handler will likely need to do some things like switch stacks and perform Spectre mitigations. But it gives an idea of the order-of-magnitude, which is less than a cache miss.
Comment: Indeed, Spectre / Meltdown mitigation increased the overhead of an invalid syscall under Linux by something like an order of magnitude.  (~100 to ~700 cycles on Skylake) - [Fastest Linux system call](https://stackoverflow.com/posts/comments/90359576).  Bare `syscall` / `sysret` without `swapgs` and a load should be even faster.


Dúvida em relação a métodos de dimensionamento em um Frame

Question: Qual é a diferença entre o método setSize() e o método setPrefferedSize() em um Frame, e em qual situação deve se utilizar cada um?
Comment: É interessante sempre procurar a documentação antes, suas ultimas perguntas estão respondidas nela.
Answer: O método setPreferredSize() é herdado da classe Component, e como o próprio nome sugere, ele define um tamanho preferido para o JFrame. É recomendado sua utilização quando estamos fazendo uso dos Gerenciadores de Layout, pois esta é a forma que informamos a ele de que queremos que o componente seja, dentro do possível, daquele tamanho. Claro que ele pode ignorar este valor, dependendo de seu funcionamento e do espaço necessário para a distribuição de outros componentes na tela, e até devido a resolução do monitor também, mas normalmente os gerenciadores utilizam esse tamanho como base e o respeitam na hora de renderizar o componente.
Outros métodos que podem ser usados em conjunto com este para proporcionar maior controle do tamanho do componente são o setMaximumSize() e setMinimumSize(), que definem, respectivamente, o tamanho máximo e minimo do componente.
Já o método setSize() define de forma da janela, de forma manual, diferente do método anterior que apenas sugere um tamanho ao gerenciador de layouts, mas também pode ser ignorado conforme o gerenciador utilizado. Você poderá utiliza-lo se o container do componente que for definir o tamanho não possuir um gerenciador de layouts ativo, caso contrário, poderá também ser ignorado.
Em via de regra, deve-se sempre utilizar o primeiro, haja visto que a não utilização de um gerenciador de layout pode trazer complicações muito maiores do que o gerenciamento de tamanho dos componentes.
Fontes:

Java: Difference between the setPreferredSize() and setSize() methods in components
Documentação das classes Component e Window.


Custom entity custom cache tags

Question: I've created a custom entity and having trouble providing proper caching.
When you create custom entity, by default it has only one cache tag entity_type:id.
Problem is, I need to display same entity to different users (by role) in a different way.
So I think I need cache tags, such as entity_type:id:user_role? How do I go with this?
Comment: You want to set cache context by role: https://www.drupal.org/docs/8/api/cache-api/cache-contexts
Comment: Yes, this is a context. The permission based context is set by default, so you rarely need to set an extra role context, unless you have users with different roles resulting in the same permissions.
Comment: Can you explain that a little more 4k4? Are you saying that the cache context is automatically set by permissions by default, and therefore does not require explicit cache contexts to be set?
Answer: To add a cache dependency on user roles you don't need a cache tag, but a cache context user.roles.
However, you rarely need this context, because there is already a default render context for the hashed user permissions user.permissions, set in the container variable required_cache_contexts, which is applied on all render arrays:
default.services.yml:
parameters:
  renderer.config:
    # Renderer required cache contexts:
    #
    # The Renderer will automatically associate these cache contexts with every
    # render array, hence varying every render array by these cache contexts.
    #
    # @default ['languages:language_interface', 'theme', 'user.permissions']
    required_cache_contexts: ['languages:language_interface', 'theme', 'user.permissions']

Custom entity custom cache tags


Horizontal scroll using scrollLeft on document.body not working in Firefox/Safari

Question: I'm building a horizontal scroll with css grid and I change the offsetLeft of (document.documentElement || document.body.parentNode || document.body) when I trigger a mousewheel event. It's working fine in Chrome but it's not in Firefox or Safari. Here's a pen I created to reproduce the problem: https://codepen.io/rluijten/pen/ewRpyM

class HorizontalScroll {
  constructor() {
    this.scrollContainer = document.querySelector('.js-scroll');
    this.target = (document.documentElement || document.body.parentNode || document.body);

    this.state = {
      moving: false,
      scrollDir: '',
      scrollPos: this.target.scrollLeft,
      scrollTop: 0,
      speed: 90,
      smooth: 12
    };

    this.rAF = null;

    this.scroll = this.scroll.bind(this);
    this.updateScrollPosition = this.updateScrollPosition.bind(this);
  }

  scroll(e) {
    e.preventDefault();

    // update scroll direction
    if (this.state.scrollPos > this.state.scrollTop) this.state.scrollDir = 'down';
    else this.state.scrollDir = 'up';

    this.state.scrollTop = this.state.scrollPos <= 0 ? 0 : this.state.scrollPos;

    console.log(this.target.scrollLeft);

    // smooth scroll
    let delta;
    if (e.detail) {
      if (e.wheelDelta) delta = e.wheelDelta / e.detail / 40 * (e.detail > 0 ? 1 : -1);
      else delta = -e.detail / 3;
    } else {
      delta = e.wheelDelta / 120;
    }

    this.state.scrollPos += -delta * this.state.speed;
    this.state.scrollPos = Math.max(
      0,
      Math.min(this.state.scrollPos, this.target.scrollWidth - this.target.clientWidth)
    );

    if (!this.state.moving) this.updateScrollPosition();
  }

  updateScrollPosition() {
    this.state.moving = true;

    const delta = (this.state.scrollPos - this.target.scrollLeft) / this.state.smooth;

    console.log(delta);

    this.target.scrollLeft += delta;

    if (Math.abs(delta) > 0) window.requestAnimationFrame(this.updateScrollPosition);
    else this.state.moving = false;
  }

  init() {
    window.addEventListener('mousewheel', this.scroll, { passive: false });
    console.log(this.target);
  }
}

const horizontalScroll = new HorizontalScroll();
horizontalScroll.init();
body {
  margin: 0;
}

.scroll {
  display: flex;
  align-items: center;
  height: 100vh;
}

.scroll__container {
  display: grid;
  grid-template-rows: repeat(2, 1fr);
  grid-row-gap: 15px;
  grid-auto-flow: column;
}

.scroll__content {
  display: flex;
  align-items: flex-end;
}

.scroll__element {
  display: inline-block;
  position: relative;
  margin-right: 15px;
}

.scroll__image {
  width: 400px;
  height: 200px;
  object-fit: cover;
}

Any ideas?
Comment: ` Mousewheel` event is not working on firefox, `window.addEventListener('mousewheel', this.scroll, { passive: false });` , you need to replace it with  `window.addEventListener('DOMMouseScroll', this.scroll, { passive: false });` . You can check here more: https://www.sitepoint.com/html5-javascript-mouse-wheel/
Comment: In the future, please include all related code *in the question itself*; that way if your external dependency (CodePen in this case) goes down, the question remains useful to future readers. I've gone ahead and done this for you here but just a note moving forward.
Comment: Thanks @TylerRoper
Comment: @mocni_T thanks for your quick reply! It works in FF now but doesn't seem to fix the issue in Safari.
Comment: Best to use the `wheel` event, it's been the cross-browser standard for a while now.
Answer: Event mousewheel is not working on firefox, you need to replace it with DOMMouseScroll, so to do that you need to check if user is using Chrome or Firefox and based on that set event listeners. Or you can add both listeners on your init() like this.
// Chrome
 window.addEventListener('mousewheel', this.scroll, { passive: false });
 // Firefox
 window.addEventListener("DOMMouseScroll", this.scroll, { passive: false });


What is object lifetime in javascript code that uses prototype inheritance?

Question: I am currently reading 'Javascript Good Parts', and I came across the following paragraph

If we try to retrieve a property value from an object, and if the
  object lacks the property name, then JavaScript attempts to retrieve
  the property value from the prototype object. And if that object is
  lacking the property, then it goes to its prototype, and so on until
  the process finally bottoms out with Object.prototype.

If I create an object obj2 from obj1 as prototype, does that mean obj1 cannot be destroyed until obj2 also goes out of scope?
Comment: I really don't know how different JavaScript engines implement prototype, but I don't think it's like a **reference** to an object. It's more like an inheritance mechanism, so I would be surprised if an **instance** of obj1 is needed for obj2 to exists.
Answer: As long as you've built your object's inheritance (linked the prototypes), I don't think that the browser relies on your references to that object.
ex1 :
var a = function(){};
a.prototype.toString = function(){return "I'm an A!";};
var b = new a();
a = undefined;
var c = new a();// error => a is not a function any more!
b.toString();// it works because the prototype is not destroyed,
             // only our reference is destroyed

ex2 :
var a = function(){};
a.prototype.toString = function(){return "I'm an A!";};
var b = function(){};
b.prototype = new a();
a = undefined;
var c = new b();
console.log(c+'');// It still works, although our
                  // initial prototype `a` doesn't exist any more.

UPDATE:
This behaviour might be related to the fact that in javascript you can't exactly destroy an object; you can only remove all references to it. After that, the browser decides how to deal with the unreferenced objects through it's Garbage collector.
Comment: Hm, wasn't `a` just a reference to that object, just like `b.prototype` is? Seems like we still have a reference.
Comment: `b.prototype` is an **instance** of `a`. (feel the `new` keyword)
Comment: As a **name** `b.prototype` is certainly a **reference**, `new a()` returnes an unnamed reference to an object, which could be assigned to a named reference, like `b.prototype`, or could be used once and after been used that way it becomes inaccessible (literally goes out of scope).


Basic questions for MeteorJS

Question: I'm planning to write some native applciation with web application method. I've searched quite some pages and was planning to use node.js + angular + something... but now would like to try Meteor since I've already used MongoDb and I don't want to learn too many frameworks "frontend" or "backend". Simple and easy to learn is core to me.
Few questions:
     Is MeteorJS fit for desktop application?
Or,
     Could MeteorJS app be packaged with node-webkit? Any example to follow?
I would also like to get suggestions if someone uses Python/C++ extension in backend for data manipulation or what ever. Is MeteorJS good for such application?
Thanks,
Comment: I haven't tried Meteor with note-webkit, but it's a tempting idea and it should work. As for working with Python / C++ / other native scripts, keep in mind that Meteor lies on top of Node, and you've got full access to Node methods and packages - so it's as easy to work with native apps as in plain Node.
Comment: I second it. Meteor is running in node.js environment and should be relatively easy to wrap into node-webkit. MongoDB might be difficult though, unless you are willing to ship a mongodb binary and instantiate a local db in your desktop app. Or if you don't need much persistence.
Answer: Meteor and node-webkit make for an incredibly powerful combination, because they allow you to:

rapidly develop a desktop app with meteor/js/html/css goodness
dynamically update your app after shipping it to the user
fully use the node API on a user's machine

You could probably use these two technologies in a few ways including:

a fairly static app which talks to a remote server via DDP
a completely dynamic app which receives all of its code every time it starts (see below)
a self-hosted meteor server on the user's machine (I have not tried this approach)

Here is an example package.json which demonstrates how to load your app completely dynamically (just like a web page) from myapp.example.com:
{
  "name": "myApp",
  "node-remote": "myapp.example.com",
  "main": "https://myapp.example.com/",
  "version": "0.0.1",
  "window": {
    "icon": "logo.png",
    "toolbar": false,
    "frame": true,
    "resizable": false,
    "position": "center",
    "width": 500,
    "height": 500
  }
}

There are a couple of things I'll caution you about:

While the application code can be updated at any time, if you require node modules or other libraries, you will need to package those ahead of time along with the nw binary. For example, if you want your users to be able to view h264 video, you'll need to ship with the necessary libraries.
If you load your application code from the internet, be aware of the potential dangers. If someone were to gain control of your DNS or your servers, all of your users could have a really bad day. The proper way to deal with this is to sign your application code before it can be loaded by node-webkit but that's beyond the scope of this answer.
Comment: Thanks, David for the details. I actually would update application with new package only so this is not a problem for me. What I want to do is to develop a app quickly with web tech since it's fancier than most of desktop tools. And I plan to start a meteor sever on user side and that's the major concern for me to select technology. Actually my plan was to use web frontend and python back end but there is no tool like node-webkit. Anyone happens to see this thread and have some solution, please drop a line.
Answer: GitHub (itself) makes Electron: http://electron.atom.io/, which like node-webkit allows you to make desktop applications with node.js and a Chromium-based browser.
I think that there's a bit more separation of the node.js and browser code in Electron than there is with node-webkit, which is probably a good thing.
Here's a Meteor package to help you integrate Meteor into an Electron-based app.
https://atmospherejs.com/jrudio/electron
I haven't tried or tested this solution, but it should give you a good start.

Basic questions for MeteorJS


Why does jslint shout at me?

Question: My code is the following (also available at jsfiddle):
<code>$button = $('<button />', {
    text: 'my button',
    class: 'button'
});
</code>
The jslint error message is:
<code>Problem at line 3 character 5: Expected an identifier and instead saw 'class' (a reserved word).
class: 'button'
</code>
Comment: I think it's complaining that class is a reserved keyword, so to use it you might need quotation marks to distinguish it from the keyword. But not sure...
Answer: Class is a reserved word for Javascript 2.0, thus you should not use it as a key in an object without putting it between quotes to mark it as a string.
Comment: And `class: "myString"` to `'class': 'myString'` works.  [fiddle](http://jsfiddle.net/rufwork/qv46A/)

Why does jslint shout at me?


Enable Filtering and sorting in a protected sheet using Apache POI

Question: I have created a workbook with protected sheets because I only need a very few columns as editable. Although I want to let the user to be able to sort and filter columns.
Google searches have failed me as of yet. Any kind of help will be appreciated.
Comment: Depends on whether this is a `XSSFSheet` or not. If it is a `XSSFSheet`, then [XSSFSheet.lockAutoFilter(false)](https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html#lockAutoFilter-boolean-) and [XSSFSheet.lockSort(false)](https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html#lockSort-boolean-).
Comment: Hi Axel. It is an XSSFSheet. Although, lockAutoFilter and lockSort didn't work. It stays grayed out in the protected sheet.
Answer: If it is a XSSFSheet, then XSSFSheet.lockAutoFilter(false) and XSSFSheet.lockSort(false) will set the properties for enabling auto-filtering and sorting in protected sheets.
Of course the auto-filter itself must be set before protecting the sheet. The setting lockAutoFilter(false) does only enabling the usage the auto-filter in protected sheets.
And for using sorting there must be set a range which is enabled for users to edit. This is because while sorting the cell values will be changed since contents of rows and so of all cells in that rows probably must be exchanged while sorting.
In Excel GUI this is made via Review tab -> Allow Users to Edit Ranges. in apache poi we have to add a CTProtectedRange to the CTWorksheet.
Note the usage of CTProtectedRange needs the full jar of all of the schemas ooxml-schemas-1.3.jar as mentioned in faq-N10025.
Complete example:
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.ss.util.CellRangeAddress;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTProtectedRange;

import java.util.Arrays;

public class CreateExcelXSSFProtectedSheetAllowFilteringAndSorting {

 public static void main(String[] args) throws Exception {

  Workbook workbook = new XSSFWorkbook();

  Sheet sheet = workbook.createSheet();
  Row row;
  Cell cell;

  row = sheet.createRow(0);
  for (int c = 0 ; c < 4; c++) {
   cell = row.createCell(c);
   cell.setCellValue("Field " + (c+1));
  }

  for (int r = 1; r < 10; r++) {
   row = sheet.createRow(r);
   for (int c = 0 ; c < 4; c++) {
    cell = row.createCell(c);
    cell.setCellValue("Data R" + (r+1) + "C" + (c+1));
   }
  }

  sheet.setAutoFilter(CellRangeAddress.valueOf("A1:D10"));
  ((XSSFSheet)sheet).lockAutoFilter(false);

  CTProtectedRange protectedRange = ((XSSFSheet)sheet).getCTWorksheet()
   .addNewProtectedRanges()
   .addNewProtectedRange();
  protectedRange.setName("enableSorting");
  protectedRange.setSqref(Arrays.asList(new String[]{"A1:D10"}));

  ((XSSFSheet)sheet).lockSort(false);

  sheet.protectSheet("");

  for (int c = 0 ; c < 4; c++) {
   sheet.autoSizeColumn(c);
  }

  FileOutputStream out = new FileOutputStream("CreateExcelXSSFProtectedSheetAllowFilteringAndSorting.xlsx");
  workbook.write(out);
  out.close();
  workbook.close();

 }

}

Comment: It works, but with CTProtectedRange columns are unlocked. How to enable sorting to locked columns?
Comment: @A M: what do you mean by "columns are unlocked". In the sheet resulting from my code, columns cannot be inserted, moved, or deleted. Or do you mean that the content of the cells in the non-protected area can be changed? But this is necessary to enable sorting. Sorting leads to changing cell contents when rows change their order. How else should sorting be possible?
Comment: As "unlocked" I mean modifiable (not read-only). So, the only way to enable sorting for locked (read-only) columns is to unlock the same columns. Isn't it?


What "access argument" or permission-string to use for a custom module?

Question: When developing a custom module that has a configuration area, what is the correct or preferred permission to use? In core, you see a mixture of:

administer site configuration
access administration pages
administer {modulename}

They appear to be applied almost at random, and searching the documentation does not show me a unambiguous "Best Practice" or "HUG" for this.
In this case, the module is small, has only one configuration-page and is related to Taxonomy, so a fourth option would be to re-use administer taxonomy.
Is there an unambiguous, or distinctive guideline on this matter?
Answer: If you set a custom permission for your module you get finer control over who can configure your module's features. If your module is strictly related to Taxonomy you may stick to Administer taxonomy. You may find this link useful for your decision.


Authentication providers: no token_bearer

Question: I created a custom resource for a Drupal 8 REST call. For some reason, I can't see either basic_auth or token_bearer from the authentication providers. I installed simple_oauth and still can't see either basic_auth or token_bearer.

The code I am using is the following one.
config/install/rest.resource.sample_index_resource.yml
id: sample_index_resource
plugin_id: sample_index_resource
granularity: resource
configuration:
GET:
  supported_formats:
    - json
  supported_auth:
    - basic_auth
    - cookie
    - token_bearer

src/Plugin/rest/resource/SampleResource.php
/**
 * Provides a Sample Index Resource
 *
 * @RestResource(
 *   id = "sample_index_resource",
 *   label = @Translation("Sample Index Resource"),
 *   uri_paths = {
 *     "canonical" = "/api/sample-response/{category}/{alias}"
 *   }
 * )
 */
class SampleResource extends ResourceBase {
/**
* Responds to entity GET requests.
* @return \Drupal\rest\ResourceResponse
*/
public function get($category, $alias) {
  // Return response
}

The HTTP Basic Authentication module needs to be enabled to be able to use basic_auth as authentication provider. I don't know which module I should enable to be able to use token_bearer.
What module do I need to install to get token_bearer as authentication provider?
Answer: I think your yml syntax is wrong (or out-dated) look at rest.resource.entity.node.yml for a correct example.
Or use the "REST UI" module to specify your resources.
It should look more like:
configuration:
  methods:
    - GET
    - POST
    - PATCH
    - DELETE
  formats:
    - hal_json
  authentication:
    - basic_auth

Comment: Looks interesting. Thanks Rainer. I'll give it a try and will get back to you.
Comment: I didn't know that I needed to enable "HTTP Basic Authentication" module to be able to use "basic_auth". Though, I have changed the syntax as per the updated one. Just looking which module should I use to be able to use "token_bearer" for the authentication providers. Thanks Rainer
Comment: hi rainer, you might know which module should I enable to be able to use "token_bearer"?
Comment: Should work with "Simple Oauth" module - take care for that issue: https://www.drupal.org/project/simple_oauth/issues/2957081
Comment: oh.. got it. Thank you so much for your help Rainer :)


EJS Template: Using forEach + include

Question: I have many files in a Folder in want to include in a Template (EJS).
I try this:
<code> <% var test = ["foo.ejs"];
 _forEach(test, function(item){ %>
      <% include slides/item %>
  <% }); %>
</code>
I would use Nodejs to fill the Array by looking in a folder.
But i get the error:
<code> Error: ENOENT, no such file or directory 'app/views/slides/item.ejs'
</code>
It seems that ejs dont use "item" as a variable.
Comment: I don't think you can do it. Reading from ejs [code](https://github.com/visionmedia/ejs/blob/master/lib/ejs.js#L160), anything after 'include ' is treated as file template location
Comment: [Here you can find an answer to this problem](http://stackoverflow.com/questions/20775710/dynamic-templates-in-ejs-node-js)
Comment: There is a solution here: http://stackoverflow.com/questions/20775710/dynamic-templates-in-ejs-node-js
Answer: Looks like this is now supported in EJS. It was introduced in v2.0.1: 2015-01-02. The new syntax looks like this
<%- include(yourVariable) %>
Answer: There is an issue for that problem in github.
https://github.com/visionmedia/ejs/issues/93

EJS Template: Using forEach + include


Integral with Dirac and Heaviside function

Question: I want to prove that
$$ \int_{-\infty}^\infty \delta (x) f(\theta (x)) dx = \int_0 ^1 f(y)dy $$
for smooth functions f(y)
where $$\delta (x)$$ is Dirac delta and $$ \theta(x) $$ is the Heaviside function.
I know that the derivative of the Heaviside function is the Dirac function, which seems the only identity that I can use, along with other properties of the Dirac function, but I cannot work the integral.
Can someone help?
Thanks in advance.
Comment: https://en.wikipedia.org/wiki/Integration_by_substitution
Comment: Note that $f(H(x))=f(0)+(f(1)−f(0))H(x)$. Inasmuch as the distribution $\langle \delta,H\rangle $ does not exist, the distribution $\langle\delta,foH\rangle $ does not exist either.
Answer: I'm not too sure whether it makes sense or not, because $f(\theta(x)) = f(1), \ x > 0, \ = \ f(0), \ x < 0,$ in general will not have finite support and is not smooth at $x = 0$.
The idea of the exercise is probably to use integration by substitution. Since $\delta(x) = \theta(x)'$, you can do a change of variables $y = \theta(x)$, $dy = \theta(x)'dx = \delta(x)dx.$ Then $$\int_{-\infty}^{\infty} \delta(x)f(\theta(x))dx = \int_{\theta(-\infty)}^{\theta(\infty)} f(y)dy = \int_0^1 f(y)dy.$$
Comment: Thanks for the reply.
That's what I tried to do, but then I realized that while the Heaviside function is smooth, we can't be certain about the f( /theta(x)).
Comment: @DVader Ok, no problem. Where did you find the question?
Comment: A friend sent it to me to check it out a couple of days back.
Comment: @DVader Ah, ok.
Comment: @ÉtienneBézout Your first statement is correct. Here as you pointed out, $f(H(x))=f(0)+(f(1)−f(0))H(x)$. Inasmuch as the distribution $\langle \delta,H\rangle $ does not exist, the distribution $\langle\delta,foH\rangle $ does not exist either.


Optimising SQL Query with repeated case statements

Question: I have the following setup:
CREATE TABLE table_name
(
perf_id INT,
build_id INT,
series_id INT,
client_id INT,
measurement INT
);

INSERT INTO table_name VALUES (1, 1, 1, 1, 10);
INSERT INTO table_name VALUES (2, 1, 2, 1, 15);
INSERT INTO table_name VALUES (3, 2, 1, 1, 10);
INSERT INTO table_name VALUES (4, 2, 2, 1, 15);
INSERT INTO table_name VALUES (5, 2, 2, 1, 20);
INSERT INTO table_name VALUES (6, 3, 1, 1, 10);
INSERT INTO table_name VALUES (7, 3, 2, 1, 20);

I then Query this with the following:
SELECT
build_id,
CONCAT(
MIN(CASE WHEN series_id IN (1) THEN measurement ELSE NULL END),
';',
AVG(CASE WHEN series_id IN (1) THEN measurement ELSE NULL END),
';',
MAX(CASE WHEN series_id IN (1) THEN measurement ELSE NULL END)
),
CONCAT(
MIN(CASE WHEN series_id IN (2) THEN measurement ELSE NULL END),
';',
AVG(CASE WHEN series_id IN (2) THEN measurement ELSE NULL END),
';',
MAX(CASE WHEN series_id IN (2) THEN measurement ELSE NULL END)
)
FROM table_name GROUP BY build_id;

You can find a sqlFiddle here http://sqlfiddle.com/#!9/efef8/17
I would like to optimise my query, mostly to reduce the repeated code. I will be calling this from PHP, and binding in values for the IN operator.
I figured a function could replace the bulk of repeated code:
CREATE FUNCTION data(s INT)
RETURNS VARCHAR(50)
RETURN CONCAT(
MIN(CASE WHEN series_id IN (s) THEN measurement ELSE NULL END),
';',
AVG(CASE WHEN series_id IN (s) THEN measurement ELSE NULL END),
';',
MAX(CASE WHEN series_id IN (s) THEN measurement ELSE NULL END)
)

But this complains of inappropriate use of aggregate function. I'm also not sure how I'd pass in an array for the IN operator.
NB:

The concat operator doesn't seem to work on SQLFiddle, but does on my machine. I don't know if I've messed up some syntax?
The number of columns will be variable.
I will also be controlling a WHERE statement before the GROUP BY, however I have this working, I just don't wish to repeat my concat a million times.
Comment: SQL doesn't have arrays. You can create a temporary table, and then use `WHEN series_id IN (SELECT id FROM temptable)`
Comment: But you can't use aggregate functions like `MIN` and `AVG` in the function, because they operate on the result set of a query, not on individual values.
Comment: I suppose the term array came from that being the PHP variable I shall be binding to it. I take it that I cannot pass a variable length "array" like "(1)" or "(1,2,3,4,5)" into a function? Can I pass a table into a function? And create this temporary table, then pass in?
Comment: No, you can't put a string there, it won't automatically split it up. Variable substitution doesn't work by interpolating text and reparsing the query, it just treats it as a single value to compare with the column.
Answer: You need to have a select from in your function. Try this:
DELIMITER $$

create FUNCTION data(s INT, id int)
RETURNS VARCHAR(50)
BEGIN
    DECLARE res VARCHAR(50);
    SET res = (SELECT CONCAT(
MIN(CASE WHEN series_id IN (s) THEN measurement ELSE NULL END),
';',
AVG(CASE WHEN series_id IN (s) THEN measurement ELSE NULL END),
';',
MAX(CASE WHEN series_id IN (s) THEN measurement ELSE NULL END)
)
from table_name
where build_id = id);
    RETURN res;
END$$

select
t.build_id
, data(1, t.build_id)
, data(2, t.build_id)
from table_name t
group by t.build_id

Comment: This looks perfect! I guess the only downside is that any filters I'd want to put on the main query would also be passed down to the function. But that doesn't seem like too much of an issue. (i.e. if I wanted to limit all my results by client id I'd have to add that limit on the function and pass it in. In fact I think the WHERE client_id=X is only needed in the inner query now?)
Comment: @Deoxyribonucleic If I understand you correctly then if you change the function to `FUNCTION data(s INT, id int, clientid int)` and change the WHERE inside the function to add `client_id = clientid` then outer query will only need to change to `, data(2, t.build_id, t.client_id)
from table_name t
group by t.build_id`
Comment: Yes, exactly what I was thinking. So it still isn't repeated as it's only inside the function that I must define my constraints
Comment: Accepted as this works perfectly. However it ended up that the performance was less than stellar. I ended up creating the repeated code  when generating the query, and then binding the values multiple times.
Answer: If you don't care about rows where the values are 0:
SELECT build_id,
       CONCAT_WS(';', MIN(measurement), AVG(measurement), MAX(measurement)),
FROM table_name
WHERE series_id = 2
GROUP BY build_id;

To get the other rows:
SELECT build_id,
       CONCAT_WS(';', MIN(measurement), AVG(measurement), MAX(measurement)),
FROM table_name
WHERE series_id = 2
GROUP BY build_id
UNION ALL
SELECT build_id, ''
FROM table_name
GROUP BY build_id
HAVING MAX(series_id = 2) = 0;

Comment: CONCAT_WS looks perfect! But I'm not sure I understand how the second half of your code works? I shall experiment with it and see if that makes it clearer.
Comment: The first query gets the build_id that have a `series_id = 2`.  The second query has a second subquery that gets the rest of the rows (note:  I fixed the logic in the `having` so it is correct).


Starting a service that start an activity on device boot

Question: im trying to start an application on device boot.
my code is as follow
1- firstly the main class which contain a background thread (asynctask) that send data to a mysql database.
2- Service class
package com.seven.ex.helper;

import com.seven.ex.AndroidGPSTrackingActivity;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class service extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
    return null;
}
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

@Override
public void onStart(Intent intent, int startid)
{
    Intent intents = new Intent(getBaseContext(),AndroidGPSTrackingActivity.class);
    intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intents);
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
}
}

3- BootUpReceiver class
package com.seven.ex.helper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootUpReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
    Intent intent = new Intent(arg0,service.class);
    arg0.startService(intent);
    Log.i("Autostart", "started");
}

}

4- the android manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpstracking"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

   <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.seven.gpstracking.AndroidGPSTrackingActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".service"
              android:label="@string/app_name"
              >
    </service>

    <receiver android:enabled="true" android:name=".BootUpReceiver">

    <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
   </receiver>

    </application>

   </manifest>

The main method is working correctly before i tried to start it on boot.
Also the app still working after i ignore the android error on boot (The App Closed)
in the log cat am getting a class not found  exception
can you please help ?
Comment: Does the code fail to locate the Service or the AndroidGPSTrackingActivity class?
Comment: when i boot up the emulator a force closed error appear. then if i open the application by clicking on the icon it work smoothly. the logcat show nothing until i opened the app by clicking on the icon
Comment: I understand, but you write "in the log cat am getting a class not found exception" and I would like to know what class it fails to find.
Comment: logcat: 09-24 12:15:41.524: E/AndroidRuntime(365): java.lang.RuntimeException: Unable to instantiate receiver com.example.ex.BootUpReceiver: java.lang.ClassNotFoundException: com.example.ex.BootUpReceiver
 ..... it can't find the BootUpReceiver class
Answer: Your Service won't work like it is at the moment. You will have to move the stuff from onStart() to onStartCommand() and then return whether the service is sticky or not. The problem is that the onStart()-method has a good chance of not getting called at all (as it is deprectaed by now).
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {

        //do your stuff here

    return Service.START_NOT_STICKY;
}

Answer: In your manifest you have assigned package: com.example.gpstracking. When u then define .BootUpReceiver. The system should expect to locate the class at com.example.gpstracking.BootUpReceiver.
Please try and change the package .BootUpReceiver to the full path com.seven.ex.helper.BootUpReceiver. The same goes for AndroidGPSTrackingActivity as this should be com.seven.ex.AndroidGPSTrackingActivity as far as I can tell.
Comment: copy&paste is a b****
Comment: @bofredo idd can be :) especially in xml and the like - no compiler or lint to save you. Not in cases like this anyway...
Comment: it works ... thank you .. i downloaded a plain source code which read gps data and start working on it , then i refactor(rename) the package name and i thought eclipse will change the name everywhere even in the manifest file but it didn't. Thank You Very Much ; i'll accept your answer @cYrixmorten
Comment: You are welcome. Things like that happen to all of us and is not easy to spot.
Comment: The answer above remove the force closed error but when i boot up the emulator the application won't start automatically?
Comment: I do not know what the exact purpos of your activity is. But if it has an UI and you want it to be started with the device, one possible option is to add:  together with the MAIN and LAUNCHER part. This gives the user an option to start your app, or to start the default home screen. If it is just background processing, that logic might aswell be in the service running in the background.


Normalization constant for uniform distribution over categorical distributions

Question: Suppose we have a uniform distribution over all categorical distributions p for m categories, where the pdf has the form
$$
f(x) = \left\{\begin{aligned}
&c, && 0 \le p_i \le 1, i = 1, ..., m, \textstyle \sum_{1}^{m}p_i = 1 \\
&0, && \textrm{otherwise}
\end{aligned}
\right.$$
How do you show that
$$
c = (m - 1)!
$$
Answer: As a quick update, I have since found the answer to my own question.
Since we are concerned with finding the density of a uniform distribution, 1/c is the normalization constant for the summation across the set of all categorical distributions of m such that
$$
\textstyle \sum_{1}^{m}p_i = 1
$$
But this set of m probabilities is simply an (m-1)-dimensional unit simplex. Therefore the normalization constant 1/c is just the volume of the (m-1)-dimensional unit simplex, which is equal to
$$
\frac{1}{(m-1)!}\text{det}(v_i - v_0, v_2 - v_0, ..., v_{m-1}-v_0)) = \frac{1}{(m-1)!}
$$
by placing the first vertex at the origin.
Therefore
$$
c = (m-1)!
$$


Consulta com dois resultados de soma a partir de uma condição

Question: Preciso realizar uma consulta em uma tabela de itens vendidos onde há um campo TipoItem que diz se o item é Produto(1) ou Serviço(2), eu gostaria que ele me trouxesse dois SUM's a partir de uma do campo Ocisubtot, onde se fosse produto(1) ele me trás um SUM e se for serviço(2) ele me traga outra coluna com SUM para serviços. Seria possível?
Exemplo:
SELECT
  SUM(Ocisubtot) AS 'Total Produto',
  SUM(Ocisubtot) AS 'Total Serviço'
FROM Orvendalevel1

TipoItem tem 1 e 2 para condição.
Estou utilizando SQL Server, não sei se deve utilizar IF ou CASE neste caso e como aplicar.
Comment: Pivot com case  https://stackoverflow.com/questions/5846007/sql-query-to-pivot-a-column-using-case-when
Answer: Isso seria possível utilizando sub-query para cada atributo, por exemplo
SELECT
    (
        SELECT SUM(Ocisubtot) FROM Orvendalevel1 WHERE cod_product = 1
    ) AS 'Total Produto'
    (
        SELECT SUM(Ocisubtot) FROM Orvendalevel1 WHERE cod_product = 2
    ) AS 'Total Serviço'
;

Comment: Muito obrigado, era exatamente o que eu precisava !!!


Prove that the sequence $x_n=n$ is Cauchy for the metric $d(x,y)=\frac{\vert x-y \vert}{\sqrt {1+x^2} \sqrt{1+y^2}}$

Question: Could you please prove that the sequence $x_n=n$ is Cauchy for the metric $d(x,y)=\frac{\vert x-y \vert}{\sqrt {1+x^2} \sqrt{1+y^2}}$?
Answer: $$d(n,n+1)\leqslant\frac1{n^2}\ \text{and}\ \sum_n\frac1{n^2}\ \text{converges}$$
Answer: Hint:
Suppose $m > n $, then
$$ d(x_m,x_n) = \frac{ |m-n|}{\sqrt{1+n^2} \sqrt{1+m^2}} \leq\frac{|m-n|}{nm} < \frac{|m-n|}{n^2}$$
and you can make RHS as small as you want.


Initialize a boost::accumulator_set at runtime

Question: Is there a way to declare at runtime a boost::accumulator_set with features determined as runtime?
Something like:
accumulator_set *acc;

if (SomeUserInput1)
{
  acc = new accumulator_set< double, features< tag::min >>;
}

if (SomeUserInput2)
{
  acc = new accumulator_set< double, features< tag::min, tag::max, tag::mean, tag::... >>;
}

Answer: There isn't. You would need to write a type-erased accumulator set wrapper. That would perform badly at runtime, which is why it isn't supported our off the box.


SQL Server CPU Usage

Question: How to find CPU usage idle, user, system, IO for Analyses using SQL Query in SQL Server
Comment: There's not a lot to this question. What have you even tried? Why is Activity Monitor in SSMS not enough?
Comment: using exec sp_who2 am not getting  idle, user, system, IO.
Comment: If status is sleeping it's idle, Hostname or SPID or Login will give you user, program name will give you system, DiskIO gives you IO, CPUTime will give you CPU usage.
What more are you wanting?
Answer: The command you are looking for in SQL Server is this:
<code>exec sp_who2
</code>
CPU Usage - CPUTime
Idle - Status (Sleeping)
User - Login / Hostname / SPID (Depending on what you mean by user)
System - ProgramName
IO - DiskIO

SQL Server CPU Usage


Load dynamic .jsp page on pageload with specific servlet

Question: I would like to create a .jsp page that is to be loaded on pageload, but the content of the page is dynamically created by calling my local database.
My question is. When a user request the index.jsp page, how do i "say".
Before displaying the index.jsp page call servletX to get content, and send this back to the index.jsp page?
I have mapping my jsp and servlet together as follows
<servlet>
    <servlet-name>intname</servlet-name>
    <servlet-class>ServletBooks</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>intname</servlet-name>
    <url-pattern>/index.jsp</url-pattern>
</servlet-mapping>

But dont know how i send the dispather, back to self?
getServletContext().getRequestDispatcher(???).forward(request, response);
Edit.
Okey so now the site works as follows.
I request the index.jsp page, which is mapped in the web.xml to a servlet.
But if i set the requestDispatcher to the index.jsp page, the whole site hangs. If i set it to another page like result1.jsp it works fine. Execpt it isint the index page displaying the content.
How do i say to the servlet, send output to initiator?
Comment: Rather than going from jsp -> servlet, i would map a servlet to that jsp's path, and use the servlet to include your jsp.
Comment: Do you have any example of how to do so and the result?
Comment: Try something like this https://zack6849.com/uploads/N2VmYjA3Y2Y0.png
 and this https://zack6849.com/uploads/ZTZjMzlhNGU0.png
Comment: Im not sure i follow, you ALWAYS need to call a index page before anything else can be called? And how do you in a servlet say "send the output back to initiator" instead of calling a new page like a result.jsp page.
Answer:
But if i set the requestDispatcher to the index.jsp page, the whole
  site hangs. If i set it to another page like result1.jsp it works
  fine.

Yes, because it is obviously going into an infinite loop as index.jsp is mapped to the Servlet you are forwarding from.
You need to have a look at the ModelViewController pattern. Essentially you never allow direct access to JSP but route through a controller, in your case a simple Servlet which will load any data required for the view and then dispatch to the view for rendering.
<servlet>
    <servlet-name>intname</servlet-name>
    <servlet-class>ServletBooks</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>intname</servlet-name>
    <url-pattern>/loadBooks.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>loadBooks.do</welcome-file>
</welcome-file-list>

In your servlet load the data and now forward to index.jsp.
http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/
Comment: So the /loadBooks.do is a servlet?
Why does it have a [dot]do?
Comment: You can make it whatever you want but why .do? See: http://stackoverflow.com/questions/3597582/why-do-java-webapps-use-do-extension-where-did-it-come-from
Comment: Im no JSP pro and i dont quite get it yet. What does loadBooks.do represent. Is it a servlet that passes the request and response objects to another servlet that then creates that page? Is it a fiction value that does not exist as a physical file? Something else?
Comment: Thanks for all the info i got it working following the example, after realising the url-pattern is just a fictitious value that act as a link between two things.


Maps Public API KEY

Question: Estou a desenvolver uma app que usa o Google Maps. Quando eu faço run a partir do Eclipse para o smartphone, o Google Maps não apresenta qualquer erro.
Quando publico a app na Play Store, o Google Maps fica com erro, não aparece o mapa.
Comment: Existe a `keystore` de *debug* e uma de produção. Ambas devem estar registradas no [console](https://cloud.google.com/console/project), em **APIs & Auth > Credentials**. Isso já está ok?
Answer: Verifique se a api-key que voce esta utilizando é a versão correta e se ela é a versão de release e não a de debug.
de uma olhada nesse tutorial:
https://developers.google.com/maps/mobile-apps?hl=pt-br

Maps Public API KEY


Compact embedding of anisotropic Sobolev space

Question: I am interested to know if the following result for anisotrpic Sobolev spaces is correctly presented. Also, does anyone have a good reference for this? Or can maybe confirm this result in its current form below.
\begin{align}
W^{1,\overrightarrow{p},\epsilon}_{0}(\Omega) \Subset L^{q}(\Omega),
\end{align}
holds for $1 \leq q < p^{\#}$, where $p^{\#}:= \frac{N\bar{p}}{N-\bar{p}}$ and $\frac{1}{\bar{p}} := \frac{1}{N}\sum_{i=1}^{N}\frac{1}{p^{i}}$.
Thanks a lot for any assistance.
Comment: What is the norm $\| \cdot\|_{1,p, p_0}$? Please post all this information in the question!
Comment: What is $W^{1,p, \epsilon}_0(\Omega)$? Please give the definition in the question.
Comment: @Jose27 $W^{1,\overrightarrow{p}}_{0}$ is defined as the closure of $C^{\infty}_{c}(\Omega)$ with respect to the norm $\Vert \cdot \Vert_{1,\overrightarrow{p},p_{0}}$.
Comment: @Jose27 Refer to [this post](http://math.stackexchange.com/questions/1064764/verification-and-presentation-of-anisotropic-sobolev-space-results) for all you need to know about anisotropic Sobolev spaces.
Answer: You should put more information into your question. Anisotropic Sobolev spaces have different definitions according to different sources, so a definition of the spaces (rather than linking to one in a comment) is a must. Additionally, I'm assuming $\Omega\subset\mathbb{R}^N$ is open and bounded, although you haven't stated as such.
Under certain conditions, the result you stated is true, although it is possible more general versions of this theorem exist. Specifically, assume $p_1\le\ldots\le p_N$ and that $\Omega$ is semirectangular with respect to $\vec{p}$. By this I mean that if $N_1,\ldots,N_L$ are the multiplicities of each of the values in $\vec{p}$, then there exist bounded Lipschitz domains $\Omega_i$ such that $\Omega=\Omega_1\times\ldots\times\Omega_L$. We have the following theorem:

Theorem: Let $\Omega\subset\mathbb{R}^N$ be open, bounded and semirectangular with respect to $\vec{p}$ and assume $N>\bar{p}$. Suppose further that one of the follows conditions holds:

$p_0\ge p_N$; or
$p_0\ge p_1$ and for all $k\in\{1,\ldots,N-1\}$ we have $q_k>\max\{p_{k+1},q_{k+1}\}$ where
  $$q_k:=\begin{cases}
\frac{k}{\sum_{i=1}^k\frac1{p_i}-1}&\text{if}\ \sum_{i=1}^k\frac1{p_i}>1,\\
\infty&\text{otherwise.}
\end{cases}$$

Then $W_0^{1,\vec{p},p_0}(\Omega)\hookrightarrow L^q(\Omega)$ for all $1\le q<p^\#$.

There are other similar theorems. I would look at "A note on the anisotropic generalizations of the Sobolev and Morrey embedding theorems" by Haskovec and Schmeiser as a first reference. You can find it online at Springer.
Comment: Dear Jason, that embedding you posted is compact?


Slideshow Question - Slider for images uploaded to a node

Question: I am building a review web page. I want to be able to upload 3-5 pictures for the product to be reviewed and have those images displayed as a slideshow within the product node. Pictures are uploaded and stored as a CCK field.
I have been able to get the Views Nivo Slider to get to work for my homepage slider, but I'm not sure it if is possible to use it to build a slideshow for fields within the same node.
Is there a way to use Views Nivo Slider to showcase the uploaded images for the product node? If not, can you recommend another slideshow module?
Answer: While I haven't specifically used Views Nivo Slider, I have used Views Slideshow to display uploaded CCK images in a view. The trick here is to use Arguments (or Contextual Filters, depending on which Views UI you're using) from the current nid, with a view that is set to display image fields.


Bifactor model and infit statistics?

Question: Good afternoon,
I am currently in the process of calibrating an item bank using a GPCM model. So, am I right to assume that the bifactor model allows me to work with my general factor by assimilating it to a one-factor model, without taking into account group factors? That is, I can estimate my item parameters from my factor loadings on the general factor only?
If so, I have some questions about evaluating the fit of my model. The calculation of infit statistics is specific to unidimensional models. Can I compute infit statistics using the general factor or do I have to do this separately for each of the group factors? Or is there a more appropriate method to evaluate the fit of my model when calibration an item bank using a GPCM model?
Thank you in advance.
Answer: Good question. As I understand it, you are interested in estimating item parameters for the bifactor scoring model, with the intent of eventually using these parameters to provide scores on the general factor (note the general factor is often also referred to as the primary factor) only. This makes sense because, in almost all applications of the bifactor model I am familiar with, researchers are most interested in the general factor, as opposed to group factors (note group factors may also be referred to as nuance, minor, or specific factors). However, see Caspi et al. (2014) as a notable exception.
So, given you are interested in only the general factor, your question is if there is a way to test the fit of only the general factor. The short answer to this is no. Bifactor models have slope parameters corresponding to both the general factor (what you are interested in) and the group factor (what you are not interested in). So, if you were to assess the fit of the general factor only, you would first need to remove the influence of the group factor from your general factor slopes. There is literature on how to do this (e.g., see Stucky & Edelen 2014; Stucky & Thissen 2013; Ip 2010a; Ip 2010b); however, none touch on model fit (your question).
So, in summary, you will need to assess the fit of your entire model$^1$, not just parameters that pertain only to the general dimension.
$^1$ Not much research exists on bifactor model fit, though see Reise, Scheines, Widaman & Haviland (2013) as a notable exception.
References
Caspi A, Houts RM, Belsky DW, Goldman-Mellor SJ, Harrington H. 2014. The p factor: One general psychopathology factor in the structure of psychiatric disorders? Clin. Psychol. Sci. 2:119-37.
Ip, E. H. (2010). Empirically indistinguishable multidimensional IRT and locally dependent unidimensional item response models. British Journal of Mathematical and Statistical Psychology, 63(2), 395-416.
Ip, E. H. (2010). Interpretation of the three-parameter testlet response model and information function. Applied Psychological Measurement, 34(7), 467-482.
Reise, S. P., Scheines, R., Widaman, K. F., & Haviland, M. G. (2013). Multidimensionality and structural coefficient bias in structural equation modeling: A bifactor perspective. Educational and Psychological Measurement, 73(1), 5-26.
Stucky, B. D., & Edelen, M. O. (2014). Using hierarchical IRT models to create unidimensional measures from multidimensional data. Handbook of Item Response Theory Modeling, 201-224.
Stucky, B. D., Thissen, D., & Orlando Edelen, M. (2013). Using logistic approximations of marginal trace lines to develop short assessments. Applied Psychological Measurement, 37(1), 41-57.

Bifactor model and infit statistics?


How to pass and receive an http request body in GET?

Question: I have two Drupal sites within one project.
I want to send a GET request but I need to pass a lot of parameters. So instead of passing them in the query (which will lead to an extremely long URL), I want to include them in the request's body.
I know that using POST is preferable in this case but I cannot change this due to the specification of the project.
So my question is how exactly can this be done?
I am using a code like this on the sending side:
$options['query']['parameters'] = 'my parameters';
$options['body'] = 'the body';
\GuzzleHttp\Client::request('GET', $uri, $options);

On the receiving side I use this code:
$parameters = \Drupal::request()->get('parameters');

So how can I catch $options['body'] on the receiving side too?
Comment: This is technically impossible, HTTP GET does not work this way, it has no body by spec.
Comment: Some systems see a GET request with a body as a hacking attempt. This is not normal and cannot be advised.
Comment: OK, I see. Well, it seems that I need to implement POST in this case.
Answer: I don't know if this is allowed by spec, see the comment from @Hudri and https://stackoverflow.com/questions/978061/http-get-with-request-body, but it works.
When adding a body in a Guzzle GET request it ends up in a Drupal controller:
public function build(Request $request) {
  $body = $request->getContent();
  return ['#markup' => $body];
}

Answer: You can get the body content using Request->getContent() method.
Next I leave a full running example:
<?php

namespace Drupal\test_api\Plugin\rest\resource;

use Drupal\Core\Session\AccountProxyInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\Request;
use Psr\Log\LoggerInterface;
use Drupal\node\Entity\Node;

/**
 * Launches the given game
 *
 * @RestResource(
 *   id = "test_sample",
 *   label = @Translation("Test Sample"),
 *   uri_paths = {
 *     "canonical" = "/test/api/v1/sample"
 *   }
 * )
 */
class GameLaunchResource extends ResourceBase {

  /**
   *  A curent user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $currentRequest;

  /**
   * Constructs a Drupal\rest\Plugin\ResourceBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param array $serializer_formats
   *   The available serialization formats.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user instance.
   * @param Symfony\Component\HttpFoundation\Request $current_request
   *   The current request
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    array $serializer_formats,
    LoggerInterface $logger,
    AccountProxyInterface $current_user,
    Request $current_request
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
    $this->currentUser = $current_user;
    $this->currentRequest = $current_request;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->getParameter('serializer.formats'),
      $container->get('logger.factory')->get('sample_api'),
      $container->get('current_user'),
      $container->get('request_stack')->getCurrentRequest()
    );
  }

  /**
   * Responds to entity GET requests.
   *
   * @return \Drupal\rest\ResourceResponse
   */
  public function get() {
    $json = '';
    $content = $this->currentRequest->getContent();
    if (!empty($content)) {
      $json = json_decode($content);
    }

    return new ResourceResponse($json);
  }
}

About general specifications for GET, there is no specification for the BODY. So you can use it or not, accordingly to your needs. For example, when I have to retrieve a collection filtered by complex conditions I usually send all the conditions in the body, as the URI doesn't have room enough for all the data I need to send.
About specific specifications for GET in Drupal, it's not considered by default, but as I showed in previous example, you can get it through the Request object.


How to use the string bool expression to filter a list

Question: I have a bool expression age_str = "age<=3"
and a age list age_list = [1,2,3,4],how to use the age_stri to filter the age_list?
the expected output is [1,2,3]
Comment: Since there are already two answers with `eval` you should be aware of possible problems: [Eval really is dangerous](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html)
Comment: so is there any other better way? In fact,I have more than one bool expression, such as 'age<=3', 'day>10','salary>1000' , i want to use these string to filter some data
Answer: You can use eval for this:
age_str = "age<=3"
age_list = [1,2,3,4]
res = []

for age in age_list:
    if eval(age_str):
        res.append(age)

print(res)

result:
[1, 2, 3]

Answer: A list comprehension with eval will do the trick:
age_str = "age<=3"
age_list = [1,2,3,4]
ages = [age for age in age_list if eval(age_str)]
print(ages)


Android async-http-client: Add parameters to callbacks?

Question: I want to use async-http-client to post data to a server and keep a SQLite on device in sync with this data. So basically i want the whole thing to:

Inert new data to the SQLite
Post the new data to the server using async-http-client
Update the SQLite row in the onSuccess/onFailure Callbacks with additional data

My Question is: How can i get the right row id in the AsyncHttpResponseHandler?
Lets create an easy example (no database connection to keep it simple but same problem).
private void addPerson(name){

  //using a global client created like this in activity onCreate():
  //AsyncHttpClient client = new AsyncHttpClient();

  //here is a database insert creating a new row, returning the inserted id
  int rowId = <some id returned by the insert>;

  RequestParams param = new RequestParams();
  param.put("name", name);

  client.post("http://www.my.service.url", param, new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response) {
      //The response i get contains an additional value, lets say userkey. I want to update the right database row with that information.
      //How to get the right rowId here?
    }
  });

}

So what is the best way to achieve this? Overriding AsyncHttpResponseHandler to somehow add a parameter to the callback functions?
Answer: Make your variable final, like
final int rowId = <some id returned by the insert>;

Answer: It's quite simple, you can send rowId.
param.put("_id", rowId);
and, server send it back to the client.
It's good way, because it allows you to make only one
AsyncHttpResponseHandler instance. (It's good to instantiate as less as possible)
Comment: I also implemented this as one way to do this but i was not sure if this is good practice: is it? And: What if i do not have access to the server then this might be a problem.
Comment: @homtg If you don't have aceess, you can check by changing parameters like private void addPerson(rowId, name)