Java Memory management and when to destroy object

Question: I have written a small Java program where I create 10 String Objects.
Can someone explain or answer the following questions.

What are the best practices to create a Object. ie; When I create a Object should I also make sure I delete the object once its used. If, How do I delete it?
If I don't delete the object, is that the object will be lying until the program ends?
Is there a way to check the number of active objects when a program is running?

<code>public class Test{

    static public void main(String[] args){
        for (Integer i = 0; i < 10; i ++){
            String s1 = new String("Creating new String");
        }
        System.out.println("Program COmpleted");
    }

}
</code>
Comment: you should probably read up on garbage collection http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
Comment: To answer in part, you cannot explicitly delete objects in Java. Java uses a Garbage Collection (or GC) system, which automatically collects (and deletes) objects.

Objects may very well be deleted before the program completes.
Comment: Don't create String objects by constructor, because then you create a new object every time. It should be like that: String s1 = "Creating new String";
Answer:
When I create a Object should I also make sure I delete the object once its used.

No. You can not explicitly delete an Object in Java. For certain memory-intensive use cases, it might make sense to explicitly nullify references to your objects, to help the garbage collector with the cleanup process, but the object itself is not deleted until it is collected by the garbage collector.

If I don't delete the object, is that the object will be lying until the program ends?

No. The garbage collector will take care of deleting it, once there are no references to it anymore. The point in time when this happens is up to the garbage collector - this might also be at the end of your application.

Is there a way to check the number of active objects when a program is running?

Yes, you can use a profiler such as YourKit.
See also

When does Java's garbage collection free a memory allocation?
Garbage Collection in the Java HotSpot Virtual Machine
Java Garbage Collection Basics
Comment: So, In the above case.. I have created 15 String Objects..and they have a reference until the program finishes. When will the GC run in this case?
Comment: It is up to the garbage collector. In your (simple) use case, it might be that the GC is not running at all since there is still enough memory available.
Comment: Ok, If there is no memory in my program, these 15 String Objects won't be GC'd, Correct?..It will be GC'd only if I set a object to NULL?, Correct?
Comment: Correct. As long as an Object is reachable through a reference (not counting cyclical references within a web of objects), it can not be garbage collected
Comment: So, how do I use a finalize method incase its not guaranteed that it will be run. If its not guaranteed to run, why do they exists in Java?
Comment: See http://stackoverflow.com/questions/8682061/when-should-be-we-use-finalize-method-in-java - thats also the only use case I came across so far, where finalizers can reliably be used: as a **last-ressort safety net**, when it is otherwise non-critical when they are **not** executed.
Answer:
What are the best practices to create a Object. ie; When I create a Object should I also make sure I delete the object once its used. If, How do I delete it?

The best practices to create a Object is just when you want to use it.
Also you need to learn about variable scoping.

If I don't delete the object, is that the object will be lying until the program ends?

No, GC will kill un referenced objects

Is there a way to check the number of active objects when a program is running?

You can use any Java profiling tool such as vesualVM
Answer: Java is a Garbage Collected language, and therefore, most memory management and cleanup is done for you. This contrary to languages like C++ where one must manually free unused memory and generally be more cautious of possible memory leaks.
To address your specific questions:

Sometimes, it is considered good practice to "null out" unused object references. This is usually only true in specific instances, for example see: Does setting Java objects to null do anything anymore?.
No, not necessarily. If there is nothing pointing to the Object in memory, then the Garbage Collector should free up the unused memory.
You could use a debugger to keep track of the number of objects that are created as you step through your code line by line (I am not sure if this is robust enough a solution for your purposes).
Comment: How would you use a debugger for that?
Comment: Well, I guess I was just thinking that if you stepped through your code line by line, you could keep track of the variables/objects that are being created using a debugger.
Answer: Java has garbage collector. Garbage collector destroys objects when it's no longer used.
you can find gc basics here](http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html)
If you want to profile application use jvisualvm , which is packaged with JDK.   If you want to count objects programmatically , use JMX API's (read oracle documentation about JMX)
Answer: The JVM automatically recycles objects that are no longer in use (this is called "garbage collection").  This happens when it feels like it, and when running out of previously unallocated memory, and returns the memory used by the unused objects to the unallocated memory pool.
All you have to do is to ensure that there is no variable through which you can use the objects any more - typically by assigning another value - and the rest happens automatically.
The simplest way to see how many objects are used by your program, is to use a profiler.  You can use visualvm in the JDK or the one in Netbeans for free.


Can a 7/8 scale guitar be detrimental to my playing ability?

Question: I was at the guitar shop today, and fell in love with one of their acoustic guitars (Steelstring, Parlour, with Cutaway). For some reason, I like small guitars.
However, I found out that it only has a 7/8 scale (61.5 cm = 24.2 inches). Since I'm planning to use this as my main practice guitar, how much should I worry that this might be detrimental to my playing?
(Note: I'm not a concert guitarist or something like that, but I still want to be able to grab any full-size guitar and play it without having to recalibrate too much.)
Answer: The answer to this is probably going to depend on the individual.  I'm inclined to say it shouldn't be a problem except that you say it will be your main practice guitar.
I'm not sure what other guitars you'll be playing and for what purpose, but in my experience, it's best to practice on the guitar you will be playing in other (non-practice) situations.  So if you're going to get together with your friends and play or perform, use the same guitar for both, or at least a guitar with similar geometry.
On the other hand, it generally shouldn't be a huge problem.  Your fingers are used to dealing with variations in the spacing between frets.   Think about what a longer scale guitar is like if you stick a capo on the third or fifth fret.  There could be issues with string tension, however, unless you accommodate by putting lighter gauge strings on the full size guitar.
I think you'll get better results sticking with one particular guitar or scale length and reserving something like the Parlour guitar for travel or other situations where its smaller size is an additional advantage over a full-size guitar - unless you plan to use the parlour guitar for everything and treat full-sized guitars as novelties.
FWIW, I believe the two most common scale lengths are 24 3/4" and 25 1/2". So a "full-size" guitar you go to grab might have either of those scale lengths, although I believe 25 1/2 is more common.
Answer: I sought advice on this site last year, before buying a short-scale bass guitar: What are the disadvantages of buying a short-scale bass guitar
I've found the short-scale bass a wonderful instrument to practice, learn and perform on. One of my concerns was that if I was handed a full-scale bass, my skills wouldn't transfer.
I've found that it's fine. I probably wouldn't attempt anything at the more challenging end of my repertoire on a full-length bass (I have small hands anyway) but most things are fine. It's actually great to practice without having to stretch too far; then when I move to a larger instrument it's just a matter of stretching a big further.
All of this will apply to a 7/8 scale guitar.
I think it's worth practising with a variety of guitars; I have a steel-strung acoustic, a nylon-strung classical, and an electric, and each has a different scale length and neck width. It means that you'll be able to adapt if you find yourself at a party and someone hands you a guitar.
On the other hand, there's something to be said for playing the more difficult pieces on an instrument you know intimately.
Answer: If it's a high-quality guitar, and it's the one you're going to be performing with, who cares about its size? If it feels right to you and makes you a better performer, perhaps the rest of us are all in the wrong!
Don't however, think that people with small hands can't play big guitars. I have taught many young and adult students with tiny hands to play comfortably on full-size guitars of all types. Much of it boils down to posture and technique.
That said, there is nothing wrong with liking or playing a smaller guitar. Music, after all, is about creating sound, not fitting some socially-acceptable model.
Answer: I built guitars and basses in the 80's and 90's and have been playing for well over 30 years.
I've tended towards shorter scale guitars (Gibson) and actually think nothing is wrong with playing even 7/8 and possibly 3/4. If you enjoy the guitar, it is easier to play and it sounds good by all means go for it.
Too often someone says, "Don't do that." Jeff Healy, the blind guitarist, had an early teacher
tell him he had to play the guitar normally and not on his his lap.
Answer: Get a guitar that fits, has good action and intonation, then play it. Use it at recital and gig with it.
Anyone who plays a regular guitar and a bass transitions scale lengths. Anyone who plays a Gibson and a Fender transitions scale lengths.
Professionals play short scales frequently. Check out Sting's Christmas special on the net. That little guitar makes me lust. He has no problem making the transition.
I love short scale. My favorite guitar is a 50 year old 3/4 scale Epiphone Centurion.I have vintage short scale basses, ES/Archtops and acoustic. Friends with much better skills than I love them and have no trouble making the transition. If you have smaller hands and work good technique on a short scale you can develop strength and agility that works on a long scale before you give up due to frustration or pain.
Most short scale acoustics will not have the bass and volume of a bigger guitar. They suffer from the perception they are kids guitars and may be equipped with poor quality nuts, bridges, tuners and strings. A parts upgrade and setup transforms many of them. The problem is the parts and labor may double the price of the guitar. String sets sold for full sized guitars may not balance in volume or tension on a short scale.
I know this a late response, but I found this chain today.
Comment: Regarding the bass and volume, probably the biggest factor is that the bodies are often smaller as well.
Regarding the tension, since the string length is smaller, the tension is less, which for me at least, is a plus.
Answer: I read somewhere that your hand span - distance between your thumb and last finger stretched out will determine the guitar scale length that you should play. Anyone with a hand span of 9 inches should find playing 650cm scale length easy and anything smaller will feel a bit crowded in the finger department. If you have smaller hands, then the 7/8 scale is a better option as you can reach the frets without damaging your hands. You should play what you feel comfortable and not bother about what others say about having to play a 650cm scale just cos you're an adult. I heard this bull story before and I believed it when I was younger but not anymore. Your hands are more precious than other people's opinion.
Comment: I would say that everyone can benefit from a shorter string scale.  It allows fingerings and chording that is difficult or impossible with longer string lengths.
Answer: Back in the 80's, a company called Valley Arts Guitar used to make high quality 7/8 scale electric guitars, and some big names like Steve Lukather even had signature models.  The difference isn't that much, (25.5" for a Fender style, vs 24.75" Gibson scale or 24" Mustang/Jaguar scale on the 7/8.)  It shouldn't be a problem.


How to remove comments from all nodes

Question: I want to remove the comment section from all my articles. Can this be done with an sql query or something so I don't have to do it manually by setting comments to disabled ?
Answer: Delete the comment fields from the Article content type and uninstall the Comment module.
Comment: Is there another way so I can still use comments for certain pages if I ever want to ?
Comment: You asked how to remove them entirely.

How to remove comments from all nodes


Link between variance and pairwise distances within a variable

Question: Please, prove that if we have two variables (equal sample size) $X$ and $Y$ and the variance in $X$ is greater than in $Y$, then the sum of squared differences (i.e., squared Euclidean distances) between data points within $X$ is also greater than that within $Y$.
Comment: Assuming the foregoing:
$$
\sum_{i,j} (x_i - x_j)^2 = \sum_{i \neq j} ((x_i - \bar{x}) - (x_j - \bar{x}))^2 = 2 n \sum_{i=1}^n (x_i - \bar{x})^2 \> ,
$$
by carefully accounting for elements in the cross term.
I imagine you can fill in the (small gaps). The result then follows trivially.
Comment: There is also a way to do this "without" any computation by considering the fact that if $X_1$ and $X_2$ are iid from $F$ (with a well-defined variance), then $\mathbb E (X_1 - X_2)^2 = 2 \mathrm{Var}(X_1)$. It requires a slightly firmer grasp on probability concepts, though.
Comment: Please clarify: When you say *variance*, do you mean *sample variance*? When you say *sum of squared differences* do you mean $\sum_{i,j} (x_i - x_j)^2$?
Comment: For a related question, I used a visualization of what's going on here in a reply at http://stats.stackexchange.com/a/18200: the squared differences are areas of squares.
Comment: @whuber: Very nice. Somehow I had missed this answer of yours along the way.
Comment: For a more extensive discussion of this relationship and its applications, visit http://en.wikipedia.org/wiki/Variogram#Empirical_variogram.
Comment: @cardinal why is the foregoing true? i fail to understand why $\sum_{i \neq j} ((x_i - \bar{x}) - (x_j - \bar{x}))^2 = 2n \sum_{i=1}^n (x_i - \bar{x})^2$
Answer: Just to provide an "official" answer, to supplement the solutions sketched in the comments, notice

None of $\operatorname{Var} ((X_i))$, $\operatorname{Var} ((Y_i))$, $\sum_{i,j}(X_i-X_j)^2$, or $\sum_{i,j} (Y_i-Y_j)^2$ are changed by shifting all $X_i$ uniformly to $X_i-\mu$ for some constant $\mu$ or shifting all $Y_i$ to $Y_i-\nu$ for some constant $\nu$.  Thus we may assume such shifts have been performed to make $\sum X_i = \sum Y_i = 0$, whence $\operatorname{Var}((X_i)) = \sum X_i^2$ and $\operatorname{Var}((Y_i)) = \sum Y_i^2$.
After clearing common factors from each side and using (1), the question asks to show that $\sum X_i^2 \ge \sum Y_i^2$ implies $\sum_{i,j} (X_i-X_j)^2 \ge \sum_{i,j} (Y_i-Y_j)^2$.
Simple expansion of the squares and rearranging the sums give $$\sum_{i,j}(X_i-X_j)^2 = 2\sum X_i^2 - 2\left(\sum X_i\right)\left(\sum X_j\right) = 2\sum X_i^2 = 2\operatorname{Var}((X_i))$$ with a similar result for the $Y$'s.

The proof is immediate.
Comment: Ah... i see, assuming a zero mean transformation make sense, sorry about that
Comment: why is $2(\sum X_i)(\sum X_j) = 0$ ?  in the 3 point, rearranging the sums
Comment: @yupbank Please read *all* of this answer, especially the part beginning "we may assume such shifts have been performed."  Then substitute the values: $2\left(\sum X_i\right)\left(\sum X_j\right) = 2(0)(0)=0.$


How is a reference pixel independent of a hardware pixel?

Question: I am going through a responsive design course on Lynda and the author defined reference pixel as being independent of hardware pixel; however, he did not go any further to explain how and/or why. I have googled to no avail. I do not understand how that works. Thank you.
Answer: here' short answer: what you call 'reference' pixels could contain 2x2, 3x3 etc physical pixels. when you set div 100x100 it will rely on 'reference' pixels, but if you put text/image inside it could be rendered much clearly as real pixels amount will be e.g. 9 in 1 'reference' pixel. also there are devices with other device pixel ratio
more details about device pixel ratio
Answer: If I can say in one line, Reference pixel is the pixel in reference to the user's eye position and hardware pixel is the smallest point on the screen which is a result of rgb combination. They are very much identical in nature. Now in details,
Hardware Pixels - This is the pixels we use everyday. It is the smallest unit upon which everything is built on. Very much like a point makes a line
Reference Pixel - This is changing over the period of time. The thing they use is called optical reference unit.
Whenever we use a device, the distance of the device and our eyes can vary. As the hardware pixels can not be changed in their size, we can only see a stretched version of it in our eyes. This may not be very much different from the actual one.
But, in case of optical reference or reference pixel, it changes the color depth by using inertial sensors of the device. As a result we can see the exact color at every viewing position of our eyes.
For android devices there is a term called density-independent pixels or dip. Because we are using devices with different screen color depth, the color density varies from device to device. To make it standardized, Android introduced it.
In short, the reference pixel is to make the pixel color independent of the the viewer's optical view point irrespective of the pixel density of the devices.
Wikipedia Page of Inertial Reference Unit
Official Android Docs


Word interop using tables

Question: I have a word document with text paragraphs as well as tables. I want to search for a table which text starts with "This Act has been update to". The table has one one cell. row 1, column 1. How do i find this table using code. Not familiar with using tables and word interop. Thanks.
Answer: i have partially copied this example from one of my projects
(replaced/removed some code - so it may contains syntax errors), but if you are already working with interop and early binding - it might be helpfull
<code>using Word = Microsoft.Office.Interop.Word;

var wordApplication = new Word.Application();
var filename = "C:\test.doc";
Word.Application wordApp = null;

if (wordApplication != null)
    wordApp = wordApplication as Word.ApplicationClass;

Word.Document wordDoc = null;
if (File.Exists(fileName.ToString()) && wordApp != null)
        {
            object readOnly = isReadonly;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;
            wordDoc = wordApp.Documents.Open(ref fileName, ref missing,
                                             ref readOnly, ref missing, ref missing, ref missing,
                                             ref missing, ref missing, ref missing, ref missing,
                                             ref missing, ref isVisible, ref missing, ref missing, ref missing,
                                             ref missing);
        }

Word.Document wordDocument = wordDoc as Word.Document;
int tablecount = wordDocument.Tables.Count;
wordDocument.Activate();
for (int i = 1; i <= tablecount; i++)
{
Word.Table wTable = wordDocument.Tables[i];
Word.Cell pCell = wTable.Cell(1, 1);
if (pCell.Range.Text == "This Act has been update to")
    {
        MessageBox.Show("Bingo !!!");
        break;
    }
}
</code>
Comment: The Requested Component does not exist at wordDocument.Tables[0] eventhough the tables count is 1
Answer: NetOffice has something to offer for you. Another solution is to save your word file in HTML format then use HTML Agility Pack.

Word interop using tables


Swift3 optional chaining and unwrapping with error handling

Question: I am attempting to unwrap an optional using the guard let syntax. The goal with guard let is to unwrap optionals, aborting the procedure if .none/nil is ever encountered.
I had thought that this meant that an optional could not be legally returned from an expression on the right-hand-side of a guard let. However, it seems if you combine two optional-producing expressions into the RHS expression, only one of those optional-producing expressions is evaluated for the purposes of the guard let. For example, the parsedResponse variable below will store an optional:
guard let parsedResponse = try? (JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String:Any]) else {
                displayError(withString: "Error parsing data as JSON")
                return
            }

The try? keyword produces an optional, as does the conditional cast operator as?. If you change either of these two operators to try! or as!, you get the expected result.
Is this the expected behavior? I had assumed that attempting to do something like this would produce behavior similar to optional chaining, and the guard let would execute the 'else' code the moment it encountered .none/nil anywhere along the execution path of the evaluation of the RHS. However, this does not appear to be the case.
What does it even mean to have an optional evaluate into a constant/variable introduced by a guard let?
Answer: I think if you rearrange the parens it will work.
guard let parsedResponse = (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)) as? [String:Any] else {

Comment: Looks like the dup is accurate
Comment: Thank you. Can confirm this works, but they are right about the tedious syntax! :) . My bad about the dupe -- thanks guys!


nginx config file line continuation?

Question: I've got an nginx config stanza that looks like:
server {
    listen *:80;
    server_name domain1.com domain2.com domain3.com domain4.com .... domainN.com;
    rewrite ^(.*) http://my_canonical_domain.com permanent;
}

with lots of different domains.  Is there some way to break this up over multiple lines?  I don't see anything in the nginx config docs which address this.
Comment: I think it's unfortunately not possible. See here:

http://serverfault.com/questions/571579/splitting-long-lines-in-a-nginx-configuration-file
Answer: There is no need to. This works perfectly:
server_name domain1
    domain2
    domain3
    ...
    domainN;

Also you could use multiple server_name directives.
Comment: @ZYinMD, of course. I even sometimes put semicolon on a separate line :-)
Comment: does not work for regex style though
Comment: Can I put `domain1` on its own line too? That'll look even nicer.
Answer: I'm not allowed to post a comment; otherwise I would have just added a comment to @Alexey Ten's answer in response to @roothan's comment.  Regular expressions also work using this method (at least with nginx/1.16.1):
server_name thisdomain.com
    ~^(www\.)thatdomain.com
    ~^(www\.)theotherdomain.com;

I had some trouble being careless with copy/paste; just make sure you don't have any semicolons except after the very last name in the directive.

nginx config file line continuation?


What pickup time signature is this

Question: How can you have an eighth note quadruplet and three eighth notes after. And how can you have a quadruplet with a dotted half note and two quarter notes?
Comment: Some additional context would be very helpful here. Title? Composer? Measure(s) before and after?
Comment: What is the beat listed at the start of this piece?  What do other measures look like?
Answer: The underlying beat is most likely a beat of 3/8 base. If the excerpt makes up a whole measure I propose that would be 6/8:
Based on the head alignment and the beaming it looks to me like the last three right hand notes and the last two left hand notes should align, such as if they were written as a duplet under three eights. The quadruplet eighth notes in the right hand look like they should take the same time as the three eighths after. The quadruplet 4ths in the left hand I suppose read as "4 quarters in place of three", which divides the two dotted quarter beats into 4 beats. Personally I'd notate this like this (if my reading is correct):

or like this

or in a 4 base time signature like this
Comment: @Aaron As you've deleted your answer I've removed any now broken references to it. I think we can safely say that the person engraving this did not spend a lot of time thinking about clarity ...
Comment: I like your answer better.
Comment: The second image looks like a drum part to me. The A-E-A-E then would be kick-snare-kick-snare. Not something you would play with the left hand on a piano.

What pickup time signature is this


Download from private GitLab without API

Question: I need to download a file from a private GitLab.
I already saw this post:
    Download from a GitLab private repository
But I cannot use the API, since I dont have the needed IDs to download the files.
In fact, I need to download them by theirs HTTP raw urls, like:
http://gitlab.private.com/group/repo_name/raw/master/diagrams/test.plantuml

Since I turned on authentication, every time I try to access something programatically, I am redirected to login page.
I wrote a Python script to mimic the login process, obtain the authenticity_token and the _gitlab_session cookie, but still not working.
If I grab a session cookie from my Chrome browser after a successful login, everything works like a charm (from the file download perspective) on Python and even curl.
So, any help is apreciated to obtain this cookie, os a different approach. To use the API I would first need to struggle among all repos performing strings matches so I can find the proper IDs. This is the last option.
Tks
Marco
Comment: Do you really need a Python script to fake login ? You can login through the API: send a `POST /session` with login & password as parameters; and obtain the `private_token`. Take a look [here](http://doc.gitlab.com/ce/api/session.html)
Comment: Yes, I know I can use the API. But if I do that, I need to download the file via API. Then, I will need to parse groups/repos to transform my groups/repos names to IDs. FakeLogin is getting worse as time passes...
Answer: First generate a Personal Access Token from your settings page: /profile/personal_access_tokens. Make sure it has the read_repository scope.
Then you can use one of two methods, replacing PRIVATETOKEN with the token you  acquired:

Pass a Private-Token header in your request. E.g.
curl --header "Private-Token: PRIVATETOKEN" http://gitlab.private.com/group/repo_name/raw/master/diagrams/test.plantuml

Add a private_token query string to your request. E.g.
curl 'http://gitlab.private.com/group/repo_name/raw/master/diagrams/test.plantuml?private_token=PRIVATETOKEN'

Comment: can anyone verify that this actually works? for me it doesnt


Android - UDP Client Error

Question: I have a little C++/Java - Socket Server (UDP) running on my PC.
Now, i want to connect with my Android App to the Server. But when i send a package my App crash.
public void Socketinit() {

    // 1. Socket erstellen!
    try {
        serverAddr = InetAddress.getByName("192.168.0.101");
        socket = new DatagramSocket();

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    createListeners();

}

and
entprivate void createListeners() {

    confirm.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            buf = input.getText().toString().getBytes();
DatagramPacket packet = new DatagramPacket(buf,buf.length, serverAddr, SERVERPORT);

            try {
                socket.send(packet);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

It crashs on "socket.send(packet);"
I can connect to my Server via C++ so the Server is up and running. Where is the Clientproblem in my code ?
thanks
Answer: You are propably getting the NetworkOnMainThreadException (see Logcat or check in the debugger).
You can use an AsyncTask to resolve this.
Propably you are also missing the Internet permission in the manifest.
Here are further details: How to fix android.os.NetworkOnMainThreadException?
Comment: Yep! i getting the NetworkOnMainThreadException! I not missing the internet permission but AsyncTask solve the Poblem.! THANK YOU!

Android - UDP Client Error


Pursuit of True Active Directory Integration

Question: Before you laugh at me and say, "If you want Active Directory, use Windows" or tell me to use Google, hear me out.
My company relies very heavily on AD. Nay, we are married to it at this point, and as a Fortune 10 company, that's not changing. However, we have a lot of *nix systems in our environment (mostly RHEL and SLES), and I have yet to find a good mechanism for integrating with Active Directory as an identity source. At the very least, I need something to provide the following:

Authentication via AD credentials (letting a user in the door)
Authorization once authenticated (granting a user access to areas of the system)
Audit (being able to tie user actions back to their AD credentials)
Support for AD groups (not just vanilla LDAP and having to add/remove individual users on systems)
Not a duplicate/mirrored identity source based on a trust of AD (I don't need two huge systems)

The top solutions that I have found are as follows:

Centrify
PowerBroker Open (PBIS Open, formerly Likewise-Open)
SSSD+SELinux

Centrify . . . is just ugly. I've never been a real fan. Also, for my company's needs, we can't use Centrify-Express, so it's not free, and there's no unlimited license. However, it's the best solution that we've found, and I'm desperate to find something else.
PBIS Open is what I'm leaning toward. It's what VMware uses in the backend of vShield, and it works pretty well. It only requires a few commands to get set up, it supports AD groups, and there's no secondary identity management system - it talks directly to AD. The only reason for me not going that route is that I like native solutions, and if there's a better way to do it that is already included in modern distros, I'm all for it.
SSSD+SELinux sounded great. It's nasty to set up, but it's flexible, native, and supported by most modern distros. The only thing it lacks (from what I understand) is support for AD groups. Many articles suggest leveraging FreeIPA or something similar to add this functionality, but upon further reading, this violates requirement 5 and basically creates a middle-man identity service. I'm not interested in basically duplicating AD or setting up trust to a secondary identity service.
Other kludge options I've tossed around include using Puppet (which we use) to push out /etc/password,shadow,group files to endpoints, but that requires development, it's incredibly indirect, and I could see something going south badly. A better option would be adding SSSD+SELinux to the Puppet idea. While it would simplify the disaster, it's still a disaster.
What am I missing, what are you using, and what is the "new hotness" that I haven't accounted for to solve the Linux AD integration headache?
Comment: is this some sort of marketing question?
Answer: Your solutions here are either FreeIPA or Centrify/PowerBroker. FreeIPA is part of your standard RHEL subscription so there is already some savings in place.
FreeIPA can be run in a mode where all users and groups can come from Active Directory. You would only keep mapping of those users and groups to POSIX-specific environments in FreeIPA, like SUDO rules, public SSH keys, host-based access control definitions, SE Linux context assignments and so on. To do so you would need to map some of your AD users/groups to some groups in FreeIPA but that is not a duplication of the information, it is amending it with the parts that are not AD-specific.
The way FreeIPA implements compatibility with Active Directory is by presenting itself as an Active Directory-compatible forest, of sorts. It is enough to allow FreeIPA resources to be consumed by AD users via cross-forest trust but not enough to allow FreeIPA users to access Windows systems on the other side of the trust. You seem to be interested in the first part so this should be fine.
With FreeIPA 4.1 which is part of RHEL 7.1 beta already (hopefully, RHEL 7.1 will be out 'soon'), we have a powerful mechanism to keep the overrides for AD users and groups in FreeIPA and SSSD is capable to discover all of them on per-server granularity.
Answer: I would really like to hear what you mean by "real AD groups" when talking about SSSD. The newer versions of SSSD don't require the groups to have POSIX attributes and mostly read the group memberships from TokenGroups if the AD provider is used.
Also, in RHEL-7.1 (upstream 1.12+), the SSSD gained the capability to do access control checks using GPO policies.
Feel free to come and write to the sssd-users list if you have a specific question.
Comment: "Security Groups" are simply predefined groups that have special meaning for predefined resource types. Like the Backup Operators group has ability to back up and restore files so any member of this group will have this ability.

In case of UNIX systems there are no predefined groups like that on a global scheme. However, one can define such groups and map access to them depending on your specific environment and software in use as there seems to be quite a variety among commercial and Open source backup tools (again, just as an example, to continue an analogy).
Comment: I suppose the terminology is "Active Directory Security Groups:"
https://technet.microsoft.com/en-us/library/cc781446(v=ws.10).aspx

From what I have found (and from a RH rep, but he could have been wrong), SSSD doesn't support them. Likewise and Centrify do. I'm not sure if other solutions do.
Comment: So, in our environment, we have groups down to the team level. For instance, if I need to grant an entire team access to one of our Windows servers, I just grant permission to "domain\team_group." We're good about removing users from groups when they switch teams, so it's far easier than managing individual access to resources since groups are dynamic.
Answer: The Redhat offering is covered well here:
Common wisdom about Active Directory authentication for Linux Servers?
In my recent installations, this has been done via SSSD (built-in) and ldap or sssd.conf group filters.
What exactly do your Linux users need to do on the systems?
Comment: It *does* sound like SSSD is the solution here. Not sure why or where SELinux should factor in here, though. For auditing I'd rather suggest https://github.com/a2o/snoopy.
Answer: I use the Open Source version of PBIS. In version 6, I found that if I was not logging on to a machine for a while (like months), when I would log on, it would time out. At least it was the case for many machines. I haven't had that issue with version 8.
I found PBIS good enough. It allowed me to set the options I cared about (default home dir, assume domain for logon, set group with access, etc).
I never tried anything else. But I can say I'm happy with PBIS.
There is a Puppet module to install it (my version of it: https://github.com/etlweather/puppet-pbis ).
Answer: what about winbind + samba + kerberos?

Authentication via AD credentials (letting a user in the door)

checked

Authorization once authenticated (granting a user access to areas of
the system)

checked

Audit (being able to tie user actions back to their AD credentials)

/var/log/secure? checked

Support for AD groups (not just vanilla LDAP and having to add/remove
individual users on systems)

it allows both ad groups or ad user in local groups, checked

Not a duplicate/mirrored identity source based on a trust of AD (I
don't need two huge systems)

freeipa is not required, checked
Comment: do you have a good link to a configuration guide that addresses group access?
Answer: i'm using Winbind + Kerberose as well and works fine for years, but now i'm migrating all to Pbis as i've been using it on some machines for years as well and i'm satisfied with it. but as you i like to have native solutions as well, there for i will start testing sssd integrations as mentioned in this document from RedHat.
https://www.redhat.com/en/files/resources/en-rhel-intergrating-rhel-6-active-directory.pdf
it has different scenarios of AD Integration.


Smooth representatives of two free homotopy classes of loops on a surface that intersect transversally

Question: I am reading a paper on closed curves of a closed-oriented surface, where the following Fact comes without proof.

Fact: Let $\Sigma$ be closed connected orientable surface. Let $\widehat\pi(\Sigma)$ be the free homotopy classes of loops on
$\Sigma$. Consider $x,y\in  \widehat\pi(\Sigma)$. Then there is a
representative $\alpha$ of $x$ and a representative $\beta$ of $y$
such that the following hold:
$(1)$ Both $\alpha$ and $\beta$ are the images of some smooth maps
$\Bbb S^1\to \Sigma$.
$(2)$ Each point of $\alpha\cap \beta$ is a point of transversal
intersection, i.e., each point $p\in\alpha\cap \beta$ has a small open neighborhood $U_p$ such that $U_p\cap (\alpha\cup \beta)$ is homeomorphic to $(\Bbb R\times 0)\cup (0\times \Bbb R)$.

My question is how to prove the above Fact. My idea is to use geodesic representatives of $x$ and $y$.
Answer: It's probably not proved in that paper because it is a standard elementary theorem of differential topology known as the transversality theorem.
You should be able to find the proof in most differential topology textbooks. I also found these lecture notes by a search for transversality theorem.
Comment: Sorry, but I want to say that $x$ and $y$ are free homotopy classes of closed curves, and these closed curves may not be embedded (simple) closed curves.
Comment: Well, transversality is a local property, and the proof should therefore still work.


Combining Multiple Years of Survey Data - Adjusting Survey Weights

Question: I am working with the HCUP NIS dataset and have aggregated 5 years of data (2009-2013) for analysis. I am interested in doing trend analysis by the year (percent mortality each year, etc) as well as analysis that spans all 5 years of data (overall percent mortality, etc).
Would it be correct to use the original survey weights for the yearly trend analysis and original survey weights divided by 5 (number of years aggregated) for the analysis that spans all 5 years?
Thanks in advance!
Answer: Dividing by 5 won't affect any means (percentages, etc), only totals, so you can equally well use the original weights or the weights divided by 5.  Dividing by 5 is a good practice because you might want to compute some totals at some point.
However, if you want to compute overall percent mortality and the population size might be changing, you do need to decide whether to weight individuals equally or years equally.  To take an extreme example: suppose the population expanded ten-fold in the last year. Using original survey weights (divided by 5 or not) would give ten times as much weight to the last year, but in some situations you might prefer to give the same weight to each year.  To weight years equally, you'd need to scale the weights for each year to the same total.


I'm a Flamenco guitar player, how to start playing Blues?

Question: I'm an amateur Flamenco guitar player (and a little bit classical guitar) and I want to learn how to play some Blues.
What do I need to start playing Blues? what do I need to know?
I checked this question:
Resources for learning the Blues
But I'm wondering if I'm ready to play some Blues since I already play Flamenco guitar.
Any similarity between Flamenco guitar music and Blues guitar music?
For now I'm sticking with my guitar, I'm not going to buy Blues guitar.
Answer: Fingerstyle blues is very satisfying; it's about 80% of what I do.
I always recommend the same first step for the aspiring blues player...Listen to the blues.
Go to the masters.  Go to YouTube and listen to the old Delta and Chicago and Texas bluesmen that were responsible for inventing the style.
Robert Johnson, Skip James, Mississippi John Hurt, Muddy Waters, Little Walter, Son House, etc.  Videos and recordings of all these guys are all over YouTube and they will link you further to dozens more greats.
Listen enough to get the feel and rhythms and timing down, and decide what style you want to explore.
THEN go looking for instructional stuff.   There's plenty.
Answer: You don't need to buy a new guitar - any acoustic, classical or flamenco guitar will be fine. If you are an experienced flamenco guitarist you will already have the ability to pick the strings and notes you want, so the difference is going to be mostly about the feel of the music.
Both flamenco and blues are very emotional styles, but where flamenco can be very upbeat, blues stems originally from melancholy and sadness - you'll really want to spend a lot of time listening to and playing along with some of the classics because although playing blues can be very simple technically, if you don't feel it you won't enjoy it, and your audience won't appreciate it.
Comment: Broadly, yes I agree. But there are plenty of upbeat blues subgenres.
Comment: Totally- almost all my blues based music is incredibly upbeat, but getting to the root of the feeling is key.
Answer: There are no so many similarities between blues and flamenco.
You should start on standart blues scheme (it is available even on Wiki - it's quite easy to learn - and after that just learn some songs of various groups, until you can't improvise youself.


Use a field value to update another field in same document

Question: I am using Firebase Firestore and have documents with two fields:
<code>field X
field Y
</code>
They are both numbers. Now I want to copy the value from X to Y. Of course I can just read the document to have the value X and then do a write operation on the same document and update field Y.
My question is, if there is a possibility to do this with just an update-operation as I couldn't find anything related but it seems to me like this might be something that's implemented by Firebase.
Thank you
Answer: Unfortunately, something like you described is not possible. One would simply read the document and update it with the read value.
Answer: Depending on your needs, you can use transactions. For instance, I used it for the rating functionality, and actually, you might find this thread  helpful.
Comment: As it's currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).


temporal disaggregation time series using regression

Question: I originally posted this question on stackoverflow and was recommended to post here instead.
I am trying to derive high frequency data from low frequency data. I also have a number of other related datasets at this higher frequency that I am trying to use to assist with the temporal disaggregation. I am not sure what the best way to approach this is!
For example:
Suppose you are trying to convert from: annual volume of gas used (for heating) to monthly volume of gas used.
You also have temperature, population, etc. on a monthly level. You know that there will be a relationship between these independent variables and the dependent variable that you are trying to estimate at a higher frequency (monthly volume of gas used).
Does anyone know the best approach here? For example, can you average/aggregate your independent variables to the lower frequency (say annual level), perform a regression, and then reduce the dependent variable to the higher frequency subject to this relationship ?
Any help would be greatly appreciated !
Answer: There are many traditional statistical approaches here, and none are a silver bullet for every circumstance. Sax and Steiner (2013; PDF link) collect several methods in the R package tempdisagg, namely Denton, Denton-Cholette, Chow-Lin, Fernandez and Litterman.

[...] On the one hand, Denton (Denton, 1971) and Denton-Cholette (e.g. Dagum and Cholette, 2006) are primarily
  concerned with movement preservation, generating a series that is similar to the indicator series
  whether or not the indicator is correlated with the low frequency series. Alternatively, these methods
  can disaggregate a series without an indicator. On the other hand, Chow-Lin, Fernandez and Litterman
  use one or several indicators and perform a regression on the low frequency series. Chow-Lin (Chow
  and Lin, 1971) is suited for stationary or cointegrated series, while Fernandez (Fernández, 1981) and
  Litterman (Litterman, 1983) deal with non-cointegrated series.

Also see Montazerin and Zarandi (2015, abstract) which discusses temporal disaggregation of natural gas time series specifically.


HTML in square brackets -> output as normal HTML

Question: We do import (using Feeds_ex) some data into Drupal (we do not control data-source we use to import data) and these data contains HTML in a brackets.
so far I have found these 3 brackets:
<code> 1. [b]bla bla bla[/b]
 2. [i]my text[/i]
 3. [url=http://www.google.com/[/url]
</code>
I'm wondering if there is a good approach to deal with these values:
<code> - Convert them during import?
 - Somehow define in Drupal that these tag are equal to <b>, <i> or <a
   href>?
</code>
At the very end I want to output these data as HTML.
I would be happy to get any suggestion.
Thanks.
Answer: That's BBCode.
One option would be to simply install the BBCode module, which contains a BBCode filter. You can then apply that filter to the text format you're using for the body field, and it'll be converted to HTML automatically when rendered.


Embed Python in C: unable to read python return integer value

Question: I am trying to embed python in my C code. So the objective is that I want to run a python code inside C program, and when the python code finish running, it should return a integer value back to my C program.
In my python code "try.py", I specify return 10 at the end of the python file.
However, I tried the following, but the return value is always 0. Why?
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{    int sys;
     Py_Initialize();
     PyObject* PyFileObject = PyFile_FromString("try.py", "r");
     sys=PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "try.py", 1);
     Py_Finalize();
     printf("\n\n the return value is %d\n\n",sys);
     return 0;
}

Comment: It has to be because `PyRun_SimpleFileEx` returns `0` on success, think about it, a python program returning an integer that way doesn't make sens, Please post the `python` code too. [Read this](https://docs.python.org/2/c-api/veryhigh.html#c.PyRun_SimpleStringFlags)
Comment: You say "_I specify `return 10` at the end of the python file._" Don't you get a `SyntaxError: 'return' outside function` there?
Answer: The PyRun_SimpleFileEx returns 0 on success and -1 on error.
This was extracted from here

int PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Executes the Python source code from command in the main module according to the flags argument. If main does not already exist, it is created. Returns 0 on success or -1 if an exception was raised. If there was an error, there is no way to get the exception information. For the meaning of flags, see below.
Note that if an otherwise unhandled SystemExit is raised, this function will not return -1, but exit the process, as long as Py_InspectFlag is not set.


Symfony2 internal route in Twig render function

Question: My layout.html.twig:
{{ render(controller('AcmeDemoBundle:Page:mainmenu')) }}

The Page controller retrieves all pages from the Doctrine and renders mainmenu.html.twig with a set of pages.
My mainmenu.html.twig:
{% if menu_pages is defined %}
    {% for page in menu_pages %}

{{ page.title|e }}

    {% endfor %}
{% endif %}

But no active class is displayed. As far as I understand the problem is in internal routing. How to fix that?
Comment: What about `app.request.attributes.get('_route')` ?
Comment: Doesn't work. Because it displays the current route of main page, not internal issued by the ``render(controller( .... ))`` Twig's method
Answer: Better do not use {{ render(controller('AcmeDemoBundle:Page:mainmenu')) }}. It work more fast and comfortable when you use services instead. You can create a service which will show menu on any page where you include them. And in service you can easy get access to current _route for add active class.
But if you really need to use {{ render(controller('AcmeDemoBundle:Page:mainmenu')) }}, so you need pass to them a current request like:
{{ render(controller('AcmeDemoBundle:Page:mainmenu', {'request': app.request})) }}

and then in action pass request to twig:
public function mainmenuAction($request) {

    return $this->render('AcmeDemoBundle:Page:mainmenu.html.twig', array('request' => $request));
}

and in twig use this request:
{% if menu_pages is defined %}
    {% for page in menu_pages %}

{{ page.title|e }}

    {% endfor %}
{% endif %}

Comment: @Victor, why do you think that `{{ render(controller()) }}` is slow? And what about  `{{ render_hinclude(controller()) }}` - do you have any experience with hinclude? I noticed, that it is slower than `render(controller())`, but it's still recomended by Symfony. Last but not least, how you want to use service instead? Do you mean about controller as service?
Comment: @Victor, you probably meant ``return $this->render`` instead just ``$this->render``? It should be a responce.
Comment: `render_hinclude` I don't use, but has experience with `render(controller())` and it work bit slower before I reorganized this to service. I create a `twig simple function` that return any service by name. And I can get access in twig to any service directly by name.
Comment: Why? :) If I need to pass in many templates of defferent actions the service, I can or pass it to each action or get them callable from twig template.
Comment: @Victor - The request will automatically be injected into magic variable `$_request` if you  add it as method parameter in the controller. there's no need to pass `app.request` from the template! Further ... controllers as services aren't generally faster ... and you can still render a controller that is a service using this syntax: `{{ render(controller('controller_service:mainmenu')) }}`. Be aware of the fact that your answer will not work when using a reverse proxy like symfony's or an external one (i.e. varnish).
Comment: @nifr The `request` in `render(controller('::'))` is a `subrequest` and is not same as `request` in a first controller, and also some parameters may be different in them.
Comment: @nifr And I don't said that `controllers as services` is more faster than a `{{ render(controller('controller_service:mainmenu')) }}`, I said that `service` is faster then `{{ render(controller('controller_service:mainmenu')) }}`, because execute less code for it and last but not least service can to solve this tasks more conveniently.


Complex number modulus identity (on unit circle)

Question: For any three complex numbers $z_1, z_2, z_3$ on the unit circle,
$|z_1 + z_2 + z_3| = |z_1 z_2 + z_1 z_3 + z_2 z_3|$.
I am able to prove this by putting each number in modulus-argument form and then expanding algebraically, but this is somewhat inelegant and tedious (supposing the arguments are $\alpha, \beta, \gamma$ respectively):
$$
LHS^2 =(\cos \alpha + \cos \beta + \cos \gamma)^2 + (\sin \alpha + \sin \beta + \sin \gamma)^2 = \cos^2\alpha + \cos^2\beta+ \cos^2\gamma + 2(\cos \alpha \cos \beta + \cos \alpha \cos \gamma+ \cos \gamma \cos \beta) +\sin^2\alpha + \sin^2\beta+ \sin^2\gamma + 2(\sin \alpha \sin \beta + \sin \alpha \sin \gamma+ \sin \gamma \sin \beta) =3+2(\cos (\alpha -\beta) + \cos (\alpha -\gamma) + \cos (\beta -\gamma))
$$
And similarly for RHS.
Question: What other methods are there to prove this identity?
Comment: on the right hand side take out a factor of $z_1z_2z_3$ then note that $\frac{1}{z_1} = z_1$ conjugate etc.
Comment: @Paul That definitely deserves to be posted as an answer in my opinion, would you prefer to repost as an answer and for me to retract my post, since you posted first?
Comment: @Sherlock - not at all, don't worry about it:)
Answer: $RHS$$=|z_1z_2+z_2z_3+z_3z_1|=|z_1z_2z_3||\frac{1}{z_1}+\frac{1}{z_2}+\frac{1}{z_3}| \\ =|\bar{z_1}+\bar{z_2}+\bar{z_3}|=|\overline{z_1+z_2+z_3}|=|z_1+z_2+z_3|=LHS \\ \square$
Edit: Kudos to Paul, who posted the general principle as a comment first .


Extract rates near to the percent sign

Question: Having a vector characters like this
  c("E_B_V_BNOW_UN_3%_01-02-2016",
    "E_B_V_XX_3%_20%_01-02-2016",
    "E_B_YY_25%_3%_20%_01-02-2016")

I would like to extract the rates near to the %?
I can do it using a combination of strsplit and gsub but is it a way to do it using a single regular expression?
The expect output is :
list(c(3),c(3,20),c(25,3,20))

Answer: regmatches(s, gregexpr("\\d+(?=%)", s, perl = TRUE))
# [[1]]
# [1] "3"
#
# [[2]]
# [1] "3"  "20"
#
# [[3]]
# [1] "25" "3"  "20"

Comment: "[0-9.]+(?=%)" would allow decimal valued numbers and "[0-9.E]+(?=%)" would allow an even wider range at the expense of possible errors. I think there are SO threads on what would constitute a safe alternative.
Comment: as written, the answer doesn't match the expected output (list of numeric vectors)
Answer: Use stringr::str_extract_all.
s <- c("E_B_V_BNOW_UN_3%_01-02-2016",
      "E_B_V_XX_3%_20%_01-02-2016",
      "E_B_YY_25%_3%_20%_01-02-2016")

library("stringr")
str_extract_all(s, "\\d+%")

Or to get rid of the percentages and make it numeric, you can do:
lapply(str_extract_all(s, "\\d+(?=%)"), as.numeric)

As has been pointed out by other answerers.  This uses a positive lookahead assertion.
Comment: @DavidArenburg right, edited to get rid of percentages
Comment: also note that `as.numeric` is required to return numeric (not character) results

Extract rates near to the percent sign


Local IP aliases routed through local instead of eth0

Question: I'm wondering one thing. I'm playing with an EC2 instance and would like to use some local subnet in the 192.168.1.0/29 range. So I've setup aliases on eth0 as follow:
[root@server jvehent]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 12:25:8B:0a:B1:EB
          inet addr:10.28.118.95  Bcast:10.28.118.255  Mask:255.255.254.0
[...]
eth0:1    Link encap:Ethernet  HWaddr 12:25:8B:0a:B1:EB
          inet addr:192.168.1.1  Bcast:192.168.1.7  Mask:255.255.255.248
[...]
eth0:2    Link encap:Ethernet  HWaddr 12:25:8B:0a:B1:EB
          inet addr:192.168.1.2  Bcast:192.168.1.7  Mask:255.255.255.248
[...]
eth0:3    Link encap:Ethernet  HWaddr 12:25:8B:0a:B1:EB
          inet addr:192.168.1.3  Bcast:192.168.1.7  Mask:255.255.255.248
[...]
eth0:4    Link encap:Ethernet  HWaddr 12:25:8B:0a:B1:EB
          inet addr:192.168.1.4  Bcast:192.168.1.7  Mask:255.255.255.248
[...]
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0

Very well, so I get the following routing table
[root@server jvehent]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0    0.0.0.0         255.255.255.248 U     0      0        0 eth0
10.28.118.0     0.0.0.0         255.255.254.0   U     0      0        0 eth0
0.0.0.0         10.28.118.1     0.0.0.0         UG    0      0        0 eth0

Now, If I want two services to talk to each other using the local aliases, I can set netcat as follow:
listening netcat:
nc -l 192.168.1.4 1664

connecting netcat:
nc -s 192.168.1.1 192.168.1.4 1664

And, from what I see, that traffic goes through the localhost interface. I'm checking with tcpdump -i lo and I see packets going through. Nothing on eth0.
My basic knowledge of the kernel tell me that since the kernel owns all the ip, it does all the operations internally and copy the packets from a socket to another without passing it to the NIC. correct ?
Is there a way to force the kernel to send those packets on the network ?
Comment: Can you tell me why you would want to force it?
Comment: I don't. I'm purely to satisfy my curiosity :)
Answer: Yes, No. Yep the kernel short circuits it by passing it to the loopback interface. No you couldn't send them out to the network because the network wouldn't send them back. The NIC can send or receive a packet, it can't both send the packet and receive it back at the same time. (Perhaps a switch could be programmed to simulate this, but you're on an EC2 instance, and there is no switch in the first place; heck there's no NIC)
Comment: Honestly I'm not sure how NIC hardware/firmware or a switch would handle a packet that has the same source and destination, it certainly wouldn't make sense. Further, neither are real in the case of EC2, you can't send a packet on the wire because there is no wire. In EC2 you're dealing with a virtual machine, that means there is no actual machine, no actual NIC, no actual switch; it's all 'virtual' bits running around a hypervisor.
Comment: you probably mean that the NIC cannot loop on itself ? I wanted to send the packet over the wire directly to the switch and back.


Combination of AJAX Views and infinite pager

Question: In Drupal 7, I load an AJAX View (in response to user clicking a link) with the following code (source: How to load and display a view with Ajax in Drupal 7?)
<code> jQuery.ajax({
    url: Drupal.settings.basePath + "/views/ajax",
    type: "post",
    data: {
    view_name: "books",
    view_display_id: "page", //your display id
    view_args: identifier, // your views arguments
  },
    dataType: "json",
    success: function (response) {
    if (response[1] !== undefined) {
      jQuery("#bloque-" + identifier).html(response[1].data);
    }
   }
 });
</code>
However, I cannot combine AJAX Views with Infinite scroll module, and even a basic pagination does not work, as it does not override the pager link code.
Answer: You can try this -
On your page template where you want to load this view use following sample code (with your view specific values such as arguments etc.) which will add all the required JS to your page -
<code>$view = views_get_view('books');
$view->set_display('page');
$view->set_use_ajax(TRUE);
$view->set_arguments(array($tid));
$settings = array(
    'views' => array(
        'ajax_path' => url('views/ajax'),
        'ajaxViews' => array(
            array(
                'view_name' => $view->name,
                'view_display_id' => $view->current_display,
                'view_args' => check_plain(implode('/', $view->args)),
                'view_path' => check_plain($_GET['q']),
                'view_base_path' => $view->get_path(),
                'view_dom_id' => 1,
                'pager_element' => $view->pager['element'],
            ),
        ),
    ),
);
drupal_add_js($settings, 'setting');
views_add_js('ajax_view');
</code>
In your JavaScript code use Drupal.attachBehaviors() which will attach Infinite Scroll to your View's HTML -
<code>jQuery.ajax({
    url: Drupal.settings.basePath + "/views/ajax",
    type: "post",
    data: {
        view_name: "books",
        view_display_id: "page", //your display id
        view_args: identifier, // your views arguments
    },
    dataType: "json",
    success: function (response) {
        if (response[1] !== undefined) {
            jQuery("#bloque-" + identifier).html(response[1].data);
        }
        Drupal.attachBehaviors();
    }
});
</code>


Correlation coefficient for transformation of variables $Y=gS+T$

Question: I have a random variable $Y$ which is related to another random variable $S$ by the relation $Y=gS+t$, where $g$ and $t$ are constants.
Let $X$ be another random variable. Then it is claimed that $\rho_{YX}=g\frac{\sigma_S}{\sigma_Y}\rho_{SX}$.
Can anyone please help me understand how this expression is true, or refer me to relevant literature? Thank you.
Answer: To understand this you need to understand the properties of the variance for a random variable $S$ with $Variance(S)=\sigma^2_S$ and standard deviation $\sigma_S$.
Then, assuming $g,t$ being two constants the random variable $Y= gS+t$ has variance $Variance(Y)= g^2Variance(S)=g^2\sigma^2_S$. The $t$ disappears because the variance is invariant of shifts of the distribution and you can get this result by writing down the formula for the variance of $Y$. Besides, I would not use a capital letter for a constant because it's easy to confuse with the notation for a random variable, so I wrote $t$ in place of $T$.
Assuming the correlation coefficient $\rho_{X,Y}= \frac{Covariance(X,Y)}{\sqrt{Var(X)}\sqrt{Var(Y)}}$, now you can try to derive the equality in your question.
If you can't manage, I'll write down the full derivation but if I was you I would give it a try first ;)
One more hint: $Covariance(X,Y)=Covariance(X,gS+t)=gCovariance(X,S)$.
As a reference, look at the Wikipedia page (here) under the section Propagation and you will find some derivations of these formulas.
Comment: Thank you! The mistake I made was that $T$ is not a constant, but a sum of random variables $\sum_{i=1}^{n} t_i$. These being independence of $S$, your covariance expression works. The variance is now $V(Y)=g^2V(S)+\sum_{i=1}^n \sigma_{ti}^2$, which gives me my required expression!


How to centrally manage routing info?

Question: I'm trying to build a VPN setup, where there are several Road-Warriors (various OSes) want to communicate to internal network behind a VPN gateway. The key facts are these:

Internal network is 10.254.0.0/24
No DHCP
Internal hosts are all RHEL/CentOS 6
Road-Warriors are assigned virtual addresses within 10.2.0.0/24
VPN gateway is a RHEL 6 box with:

Internal address 10.254.0.111
External address X.X.X.X

VPN gateway is NOT the default gateway for internal hosts
VPN is implemented using Strongswan

So far Road-Warriors successfully connect to VPN gateway. The issue is that on every internal host I have to setup routing tables to send 10.2.0.0/24 traffic through 10.254.0.111 if I want them be accessible for RWs.
What are other options? How can I centrally manage routing info so that it is autodiscovered by internal hosts?
Answer: There's a few ways.  The easiest way is to set up an intermediate router (that acts as the default gateway for both networks) with the two subnets on different interfaces.  The VPN server could be behind one of them, it doesn't have to act as one.
So something like this:
RWs - VPN Server -default gateway for vpn server- Router -default gateway- Internal network
And the router would have third interface that hooks up with the WAN
Alternatively you could have a log on script runs on the local boxes that adds the 10.2.0.0/24 route with the 10.254.0.111 gateway. This one could get complicated if they don't all have the same interface number for internal traffic.

How to centrally manage routing info?


Hiding an item using ngClass expression in Ionic using Angular

Question: I'm trying to apply ion-hide class to ion-col. For some reason, it is not applying when I try to use ngClass. Could anyone please help?
<ion-col [ngClass]="{ 'ion-hide': !flag }">

Content

</ion-col>

Comment: Try to check if `flag` is being set with the desired value. Also, if you're using `OnPush` strategy for change detection, it may need some manual work to make it work. There's nothing wrong with your syntax.
Answer: use this syntax:
<ion-col [class.ion-hide]="!flag">

Content

</ion-col>


Can't restore profile-installed site after D7.12 core upgrade

Question: I have just attempted to do another minor core upgrade from 7.9 to 7.12. While the former went rather smooth, the latter is giving me a headache. I constantly get fatal errors like these below:
<code>Notice: Undefined index: schema_fields_sql in /includes/entity.inc on line 265

Catchable fatal error: Argument 2 passed to SelectQuery::fields() must be an array, null given, called in /includes/entity.inc on line 284 and defined in /includes/database/select.inc on line 1300
</code>
I am now in the middle of yet another attempt to restore the site
The difference in that website, compared to that which successfully passed the core upgrade, is that this was originally installed using commerce kickstart profile. And my questions are:

Has anyone successfully performed core upgrade within a
profile-originated site?
What are safety rules to observe when doing
such upgrades (apart from obvious backing up the files and
database?)?
What approach you prefer for upgrading live sites? Do you usually create a new directory, which you test, and when ready, you redirect root catalog to that directory?

I hope your insights will bring back my confidence as per having chosen Drupal for commercial sites.
Answer: Did you remember to run update.php?
Also, Drupal 7.11 and 7.12 are both security updates. 7.12 includes bug fixes, while 7.11 only addresses the security problems. You might try 7.11 and see how it goes.
Also 2: Here's an issue thread about it.
Comment: Thanks paul-m, I have actually googled and bumped into the thread, but I judged it was too old to be still valid. I managed to restore to a previous restore point thanks to my ISP, but will definitely make another attempt when I'm done with my development. The update.php  address is something I try to use after each module installation, but chances are I missed it this time. Anyway thank you.
Comment: Sure thing. You should run update.php on core updates, too.


If a subgroup $H\le S_n$ acts transitively on transpositions then $H=S_n$

Question: Let $H \le S_n$ acts on the set of transpositions:
$X=\{(i \space j)\space | \space i\ne j\}\subseteq S_n$
by conjugation and assume the action is transitive.

Assume $(1 \space 2)\in H$. Prove: $H=S_n$

My try:
I know from the Orbit-Stabilizer theorem that $|X|=\frac{n\cdot(n-1)}{2}$ divides $|H|$.
I know that the transpositions generate $S_n$. So I was thinking maybe I could show that all transpositions are in $H$.
Will appreciate any hint or direction.
Comment: The fact that the conjugation action on $X$ is transitive tells you immediately that all transpositions are in $X$.
Answer: Yes, you can show that all transpositions are in $H$. You have $(1 \space 2) \in H$, and every $(i \space j)$ is of the form $h (1 \space 2) h^{-1}$ for some $h \in H$.  Hence $(i \space j) \in H$.
Comment: @user750041 Yes, that would be sufficient.
Comment: Since a generating set of $S_n$ is $X':=\{(1 \space j), j=2,\dots,n)\}\subset X$, wouldn't it suffice in the OP to assume that $H$ acts transitively by conjugation on $X'$?


Java CardLayout Show Problem

Question: Ok me again i need help. For some reason when i click a button the button dissapears but not the panel
Here is the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainMenu extends JPanel implements ActionListener{

ImageDirectory ID = new ImageDirectory();
PlayMenu PM = new PlayMenu();
JButton Play;
CardLayout CL;
Image BG;

    public MainMenu(){

    CL = new CardLayout(180,220);
    setLayout(CL);
    Play = new JButton("play");
    Play.addActionListener(this);
    ImageIcon BGSource = new ImageIcon(ID.Background);
    BG = BGSource.getImage();
    add(Play, "Play");
    add(PM, "PMenu");
    }

    public void actionPerformed(ActionEvent e){

        if(e.getSource() == Play){

            CL.show(this, "PMenu");
            }
        }

        public void paintComponent(Graphics g){

            g.drawImage(BG,0,0,500,500,this);
            }
 }

Here is the class that i tried to show when button is clicked:
import javax.swing.*;
import java.awt.*;

public class PlayMenu extends JPanel{

public PlayMenu() {}

}

Comment: We're pretty poor at reading minds, and I think that you will want to give more information if you want this question to be one that we can help you with. Consider creating and posting an [SSCCE](http://sscce.org]
Answer: It's probably working as it should.  Your new PlayMenu is replacing the original MainMenu.  Try adding setBackground(Color.RED) in the PlayMenu constructor to see if it does replace the previous MainMenu.

Java CardLayout Show Problem


Flexbox: Fill clicked row and wrap elements below/above to fill space

Question: I am trying to set up a responsive grid so that when you click on a grid-item, it fills the horizontal space of its row and pushes the other items either above it or below it. To fill the remaining empty space.
Here's my current code which does fill the row but leaves empty space.
JSFIDDLE

$(".box").click(function() {
  $(this).toggleClass("enlarge");
});
body {
  margin: 0;
}

.enlarge {
  flex-basis: 100% !important;
}

.boxes {
  display: flex;
  flex-wrap: wrap;
}

.boxes .box {
  flex-basis: calc((100% / 3) - 2rem);
  margin: 1rem;
  height: 200px;
  box-sizing: border-box;
  text-align: center;
  transition: all 1s;
  background-color: rgb(220, 220, 220);
}

Comment: Your code is working. What is your question tho?
Comment: Which empty space are you talking about?? Everything seems to be fine
Comment: @flppv If you click the second box, it leaves only the first box in that top row and then empty space. Vikas solved it by having it stretch to fill, but ultimately I'd like a different box in the grid to fill that space.
Answer: Try this...

$(".box").click(function(){
    $('.box').removeClass("enlarge");
    $(this).toggleClass("enlarge");
});
body {
    margin: 0;
}

.enlarge {
    flex-basis: 100% !important;
}

.boxes {
    display: flex;
    flex-wrap: wrap;
}
 .box {
        flex-basis: calc((100% / 3) - 2rem);
        flex-grow:1;
        display:flex;
        margin: 1rem;
        height: 200px;
        box-sizing: border-box;
        text-align: center;
        transition: all 1s;
        background-color: rgb(220, 220, 220);
        align-items:center;
        justify-content:center;

    }

1

2

3

4

5

6

Comment: That works nicely, thanks!

Is it possible to not have the other boxes stretch to fill the space, but simply wrap? I'm guessing that would require reordering of elements?

For example, you click on box 2 and it pushes boxes 1 and 3 below it.


Environment variables not being appearing correctly in bash script

Question: Trying to add a database by the name of abc but postgresql instead creates a database called NEW_DATABASE.
export NEW_DATABASE=abc
psql -U postgres -c 'create database "$NEW_DATABASE";'

Omitting the double quotes gives an error
ERROR:  syntax error at or near "$"
LINE 1: create database $NEW_DATABASE;

Answer: Replace:
psql -U postgres -c 'create database "$NEW_DATABASE";'

With:
psql -U postgres -c "create database \"$NEW_DATABASE\";"

The problem is that shell variables are not expanded inside single-quotes.  If you want shell variables expanded, use double-quotes.


Are there any conventions or usual patterns on how brass is commonly used in classical music, precisely, in a symphonic orchestra?

Question: I hear many people tell me that each brass instrument has its own unique timbre and one cannot be confused with another. However, I often see them used together and they seem to blend together perfectly well - almost as if they were all one type of brass.
Here some of the usual patterns that I have noticed (I have no education in music, so please bear with me):

The tuba is typically and unfairly employed in the lower register more often than not, even though its range is rather wide and it can potentially go high;

Trumpets occupy the upper range;

It's very common to find two trumpets playing in octaves;

Brass instruments are often featured in louder sections and seldom in softer passages, possibly because they are challenging to play softly, unlike strings or woodwinds;

Brass instruments tend not to handle fast-paced sections, such as those with sixteenth and thirty-second notes, which are effortlessly managed by violins, violas, and woodwinds, due to the slower attack in their sound;

Are my observations here correct?
Are there any other patterns?
Comment: Even as a bass instrument, the tuba is traditionally misused; acoustically and technically it would belong with the horns, but for a long time it was used as the bass of the trombone group, because true bass trombones were hard to construct.
Comment: Just as a note, many passages now commonly played with tubas are not original for said instrument (because it's very new). For example, Berlioz's _Symphonie Fantastique_ is commonly played with two tubas (which even have some solis), but it was originally written for two ophicleids.
Comment: @ComputerUser121212 - Didn't know that. Thank you!
Answer: Have a listen to this performance of Paganini Variations.
You will conclude :

Brass instruments can play fast and quiet (and simultaneously).
They all sound slightly different - you should be able to distinguish cornets, horns, trombones and tubas in the performance above.

This is a thrilling piece (in my opinion).
Comment: You blew up my mind with that piece! What happens there, especially toward the very end, is just incredible. Never knew that brass could be so flexible. The only thing I failed to do there is differentiate between different instruments by simply hearing them - I am still not so well versed in classical music.
Answer: The answer to this question depends on what is meant by "classical." Both the technology and the idiomatic use of brass instruments in the orchestra has changed over the decades and so the answer is dependent on the time period.
In the early classical period, the tuba would not have been in the orchestra at all, and the trumpet would have been a novelty. The trumpet used in Haydn's trumpet concerto would have have had keys, not pistons, and would have had different capabilities than a modern trumpet or cornet. Horns in general would have had fixed-length tubes, which could be changed manually to for pieces in different keys, but not quickly. All of these factors limited the way these instruments could have been used in an orchestral arrangement. You will not see French horns making rapid key changes, for example.
As we move into the romantic period we start finding a greater variety of brass instruments and they start looking more like their modern counterparts. But there were still conventions on their use. In Rimsky-Korsokov's book on orchestration, he stated that brass and strings should not be combined except in sections that were tutti (everybody playing including the woodwinds). He also encouraged using the french horns for notes that were passing tones or dissonant, e.g. as the seventh in a dominant chord, when possible.
Also, there remained some physical constraints on the instruments. For example, certain keys are more difficult to play in tune due to the which pistons need to be used and how long they are compared to the overall bore length. In general, parts for brass should prefer key signatures on the flat side if possible. Three flats (Eb) is much more common for a trumpet, for example, than three sharps (A).
When we move to twentieth-century, we find a lot of composers who want to experiment with different sounds. They want to hear what a tuba sounds like trying play high notes; they to hear what trumpets sound like playing pedal notes that are technically below the instruments' range. So all the rules get broken. A lot of the music from this period is extremely challenging to perform, and also rather odd. My favorite work is the "The Conversation" by John Williams which is a duet between clarinet and tuba.
Comment: Very interesting! Thank you. Aren't valves and pistons one and the same thing? If not, what's the essential difference?
Comment: I might not be using the best terminology. In Haydn's period the trumpet would have been [one of these](https://en.wikipedia.org/wiki/Keyed_trumpet).
Comment: Ah, I see now. Thank you.
Comment: @brilliant unrelated to John Wu's answer, but, to answer your question, [rotary] valves and pistons are not in fact the same thing. Sometimes, however, pistons are called _piston valves_. Both archieve the same purpose but not in the same manner, and some players prefer playing on one over the other. Pistons are _generally_ consider more agile, whereas rotary valves are _generally_ easier to play due to their shorter travel. On some instruments almost always only one type is used (french horns almost always use rotary valves), while in others, like tubas, models with either system are (+)
Comment: @brilliant available, coming down to an individuals player's preference which one do they use. See [the relevant Wikipedia page](https://en.wikipedia.org/wiki/Brass_instrument_valve) for details on the different mechanisms.
Comment: @ComputerUser121212 - Thanks for the link. I realize now that my knowledge about valves was that of a zero.
Comment: Horns and trumpets don't usually have key signatures.
Answer: You are correct in your observations on how brass instruments are typically and traditionally used in the 'classical' orchestra. (Though composers have always known that trombones, though capable of being the loudest instrument in the orchestra, are also effective in 'pp', and are quite agile.  Think how trombones have often been used to support the bass, tenor and alto voices in choral works.)  So if you want to write 'classical' pastiche (and that's a perfectly legitimate thing to do) stick to the 'classical' style.
But it's also correct that the brass CAN be (and are) used more, and in many ways.
If you're worried about technical limitations, just listen to any Brass Band work.  And listen to this:

It isn't wild virtuoso stuff, every intermediate trumpet player studies this piece!
There is a trend in 'modern classical' writing towards requiring ever more ingenious 'advanced techniques' from every instrument, brass included.  It can be as self-conscious as the rejection of tonality over 100 years ago, but at least it reminds us there are possibilities!
Comment: We all _study_ the piece - we don't all play it nearly as well as the soloist in that video! At 7:45 I've never seen that "play it with one finger" technique before. Very impressive!
Comment: @BrianTHOMAS I'm tempted to ask "just... _why_" though! I mean, it's not like this frees the other fingers to doing something else that could be done on the trumpet, does it?
Comment: Thank you. Very informative!
Comment: @leftaroundabout - it's the brass equivalent of playing guitar behind your head or with your teeth. Or sticking daggers in your keyboard…


Is it possible to dynamically select between two different template files for the same content type?

Question: We are rolling out changes on a content type with a new theme and field structures for existing content.
Is it possible to dynamically select one of two template files for a content type?
For example, if a field on the content type is checked then use node--product.html.twig but otherwise use node--product-old.html.twig.
Answer: Yes. You can implement hook_theme_suggestions_HOOK_alter() to set up different templates based on the field.
In your case, you want to add templates for nodes, so the theme hook is node. As such, you would implement the following:
function hook_theme_suggestions_node_alter(array &$suggestions, array $variables) {
  $node = $variables['node'];
  if ($node->hasField('some_field') && $node->some_field->value == 1) {
    $suggestions[] = 'node__product';
  }
}

With this code, when some_field does not exist, or does not have a value of 1, the default node.html.twig template will be used, and when some_field has a value of 1, the template node--product.html.twig will be used. Adjust accordingly.
Comment: It's exactly what they're used for :) It's a very powerful part of the theming layer. Good luck!
Comment: Awesome. I did not know theme suggestions could be used in that way. Thank you!


Hash dentro de outro Hash retorna sempre vazio se não for usado uma chave

Question: Bom criei um método para agrupar todos os commits de um usuário por mês em um Hash:
def calculate_month_ranking(commits)
  rank = Hash.new(Hash.new(0))
  commits.each do |commit_yml|
    commit = YAML.load commit_yml.data

    commit[:commits].each do |local_commit|
      time = Time.parse(local_commit[:timestamp])
      month = "#{time.month}/#{time.year}"
      rank[month][commit[:user_name]]+= 1
      binding.pry # para debug
    end

  end

  rank
end

O resultado é que ao recuperar os dados pelo mês os dados aparecem, mas a variável rank está vazia como podem ver no debug abaixo
[1] pry(main)> rank[month]
=> {"user1"=>4, "user2"=>1}
[2] pry(main)> rank
=> {}

Answer: Nesse caso você está alterando o fallback do seu hash para chaves que não existem (quando você faz: rank = Hash.new(Hash.new(0))).
Ou seja, sempre que você tentar acessar uma chave que não existe no rank você acaba acessando um novo hash (Hash.new(0)), e o problema é que esse novo hash é que está recebendo o valor da atribuição rank[month][commit[:user_name]] += 1, e o hash rank acaba não recendo essa atribuição internamente -- é meio complicado explicar isso, e para falar a verdade nem eu sei como isso funciona internamente XD.
Mas você pode checar todos os valores da seguinte maneira:
[1] pry(main)> rank.default
=> {"junho"=>{"user1"=>4, "user2"=>1}}

Leia mais na API:

Hashes have a default value that is returned when accessing keys that do not exist in the hash. If no default is set nil is used. You can set the default value by sending it as an argument to ::new:


Multiple instances of an Activity - is this bad practice?

Question: I am creating an app which will have multiple 'grids' containing a title and image thumbnails in each grid square.
Each Grid will have different content stored in it.
I have so far created one activity that initialises an instance of GridView, and uses a custom GridAdapter. (See photo for what it currently looks like) I was planning to swipe left to create a new empty grid in which the user can upload content. There may be anywhere up to 50 grids.
I'm just learning how to implement the gesture, and how to create a new instance of the activity, but from what I've read, I am thinking I have designed it badly.
I was planning for each grid to be an Activity (each takes up the full screen).
I envisaged an Activity as being like a Class in java that you can create instances from a blueprint. I thought if I created one 'Grid' I could create a new instance of it each time. Fragments didn't seem appropriate at the time, as the android tutorials often described them as being purposed to add components to activities.
I'm starting to think though that I am using the wrong methodology here and I need to change it? Can someone guide me in the right direction? I have written all the code already - if I need to change it, do Fragments and Activities share any methods, meaning I can retain some work?
Comment: So, currently you are listening for swipe gestures and then starting activities?
Comment: Hi @Shaishav - that's the bit I'm just about to do. I have created one activity that initialises an instance of GridView, and uses a custom GridAdapter. I'm just learning how to implement the gesture, and how to create a new instance of the activity, but from what I've read, I am thinking I have done it wrong.....
Comment: I have tried to clarify it a bit better
Answer: Like you mentioned, using activities for holding content in your use case where switching may be triggered using gestures will definitely be resource heavy and cumbersome. Since, you mentioned swipe gestures, I believe fragments would be much lightweight in this situation. In fact, I would suggest you even look at ViewPager which even recycles fragments for you and optimizes user experience by loading the next fragment for a smoother experience. It will also handle swipe gestures for you!
[UPDATE]
Based on your updated explanation of the user flow, I'm certain that the ViewPager would fare as a better option mainly because it allows for a much better control and user navigation. It will also take care of handling swipe gestures and memory issues that come with these types of flows. Moreover, it will even allow for a page titles and bottom tab indicators in case you need them.
It will require each of its pages to be a fragment (your ViewPager will itself reside in an Activity). Once the user clicks on a grid cell, you can show a dialog window from where user input can be captured. This setup should be optimal for you resource wise in my opinion.
Comment: Hi @Shaishav. Thanks for helping me. So do you recommend each grid is one fragment, there are multiple fragments, and just one activity? So each time you swipe it stays on the same activity, but loads a new fragment? Each grid square contains a different View object. Would ViewPager work in that situation? I have added a photo above to help me explain. Thank you so much for your advice
Comment: @javapalava I guess, I misunderstood. I thought initially your layout will look like your image and then when the user clicks on a grid cell, it'd go to full screen and user can swipe left/right to check other cells in full screen. I'm confused, how will user interact with the image above. Whats the user flow here?
Comment: Let's maybe ignore the gesture element - I think I am confusing matters, and it is maybe not even relevant for what I am trying to figure out. There will be lots (up to 50) of these grids (however the user accesses them) stored in the app, and each will have different content contained within them. I have written the code for one, but now I need to enable the user to create many, and am just trying to figure out what should be a fragment, and what an activity.
Comment: @javapalava So, something like - The user sees the above grid which is [possibly] vertically scrollable and user can add more cells via, lets say a grid cell that has a "+" sign and enter its info. Clicking on any cell may show an expanded information about it. Is this understanding correct?
Comment: So, the user will see the following grid - it is vertically scrollable, but has a fixed length - currently only 5 squares long. So each grid has 15 squares in total (3 x 5). Users can't add more cells, they only ever have 15. They can click on a cell, which opens a pop up menu, and they can choose an image from their gallery, which is resized and stored in a cell. Once an image is in there, they can't expand it. Each 'grid' or 'board' will have a theme - i.e. thumbnails of family, house, etc. There will be multiple 'Grids' or 'Boards' - so lots like the one above.
Comment: They will all have different titles, and each will likely contain different thumbnails, but will have the same grid structure, as shown above. So there will be lots of that picture, but with different things stored in them!
Comment: I need to get better at explaining :)
Comment: Thanks so much @Shaishav! I've already done the dialogue window :) So, are you suggesting that each 'grid/board' is a fragment? And that each one shares the same Activity?
Comment: @javapalava great! Yeah each one shares the same `ViewPager` which would reside in an `Activity`.
Comment: OK great, will look up ViewPager! Thanks so much! Have lots of work to do :)
Comment: @javapalava Cool. All the best!
Comment: @javapalava If this works out for you don't forget to accept my answer. :)
Comment: Of course! You were a great help. I am just figuring out how to implement now :)


Interview Question: Virtualization and software quality assurance

Question: How do you automate (in testing) the process of making your system fail and recover from the failure, and also you must make sure time stamp is maintained each time the system recovers?
I was asked this question in an interview. I know this question is little vague, I guess interviewer was trying to test my virtualization skills.
Comment: This is very, very difficult to answer because the steps and scenarios vary wildly. VMWare have a product called Site Recovery Manager which automates all of this for you, but if you're using, say, Xeon or KVM then the procedure is entirely different.
Comment: The interviewer was much interested in Xen hypervisor, he was more interested in test automation of system failure
Comment: That question is very broad, you would need a lot more information to find out exactly what it is they were looking for. I'm guessing the interviewer wanted to see if you would probe for more information or not.
Comment: @cpgascho yeah agree, i probed him lot information, like i said we can run monitor process which has time stamp or modifying the code where the process can die.
Answer: In general, I'd use the hypervisors' API to monitor the VM status, while killing it's process, to simulate failure. The API based monitoring will write logs with timestamps.
This can be the broad answer, as for specifics - it would depend on what hypervisor and management are used


How to build select lists for search_api views

Question: I am building a small website for a charity and am trying to integrate search_api to search the list of projects.
I have a content type named "Project" with the following fields:

Title ( Project Name )
Body
Project Image ( Image field )
Project Countries ( Multi valued taxonomy of all Countries this project operates in )

I need to build a search page that has:

A free text search box that will search Title/Body as a fulltext search
A select list or autocomplete field that allows you to select the country you are searching

So far I have:

Created a new solr search server using search_api
Indexed the Project content type in a new search index
Added all taxonomy values and fields to the search index ( in the hope one would work )
Created a view linked to this index
Added all of the above taxonomy fields as exposed group search filters but none of them pick up the values of the taxonomy.

I can happily freetext search on the indexed fields but if I want to use dropdown filters or even checkboxes to filter the results it just doesn't work.
Clearly I am doing something wrong here, what is the best way to achieve what I am trying to do?
Answer: In this case I would create a Facet. You'll need to enable Search Facets (shipped with Search API) and Facet API.
Once you have that in place go to Configuration -> Search and metadata -> Search API and click edit on your Search index. In the dropdown there is a new choice called Facets now.

Enable the facet for you field Project Countries. That field also needs to be indexed under your fields settings for your search index. Facet API will provide a block for you with the Facet that you need to place on your search page.
Facets work in a way that they only show options available in your search result. If "Sweden" is not present in the field Project Countries in any of the nodes you have indexed it will neither be available in the Facet. So facets work in a way that they narrow down search results and usually doesn't show options that will result in an empty search. That's the biggest difference I see between a facet and a normal filter in Views.
Facets are normally displayed as checkboxes or links (see display settings for your Facet). You mentioned in your question that you have it now in the View as a dropdown. You you want to keep it as a dropdown instead of checkboxes / links you could checkout Facetapi select


C question related to pointers and arrays

Question: Basically in the code below, my final array does not seem to have the contents from function1(). Any ideas on why I can't get this to work ? Thanks.
    #include <stdio.h>
    #include <string.h>
    #include<stdlib.h>

  unsigned char *function1()
 {
  unsigned char array2[] = { 0x4a,0xb2 };
  return (array2 );

  }

   main()

     {
unsigned char temp[] = { 0xaa, 0x0b, 0x03,0x04,0x05,0x06,0x07,0x08,0x09 };
unsigned char x[2];
unsigned char *packet;
int pkt_len;
pkt_len = sizeof(temp) + sizeof(x);
packet = (unsigned char*) malloc ( pkt_len +1);

memset( packet, 0x00, pkt_len +1);
unsigned char *pointer1 = malloc ( sizeof(temp) + 1);

memset( pointer1, 0x00, sizeof(temp) +1);
memcpy (pointer1, temp, sizeof(temp) );

memcpy (packet, pointer1, sizeof(temp) );
printf("\nPacket before copy is 0x%x\n", packet[8]);

    unsigned char *array2 = malloc ( sizeof (x) + 1)  ;
    array2 = (char *)function1();
printf("\nArray2 is 0x%x\n", array2[0]);
    memcpy (packet + sizeof(temp), array2, sizeof(x) );
printf("After copy, Packet contents are 0x%x\n", packet[9]);
  }

Comment: I'm sorry, but that's very horrible code.  It looks like you just copied it, mistakes and all, and now are trying to get it working.
Comment: After getting this to work, you should understand pointers pretty well.
Comment: @talk2vogon - This is Vogon poetry
Comment: There's serious bug in `function1()` you return a pointer to a local variable, that's a big nono, since the next function you call will overwrite that stack space with it's own local variables.
Answer: Following are the mistakes that I observed in your code.
You wrote
  char temp [] = { a,s,d,f,g,h};
  char * pointer1, *array1;
  pointer1 = &temp;
  memcpy (array1, pointer1, sizeof( temp) );

Now there is no need to do this pointer1 = &temp, name of any array itself is a pointer.
Hence you can simply do

  char temp [] = { a,s,d,f,g,h};
  char *pointer1;
  memcpy (pointer1, temp , sizeof( temp) );

But wait!
Does pointer1 has enough space to store the contents of temp[]? In your code you have not assigned any space to pointer1, which is likely to crash your program.
The correct way to do it is

  char temp [] = { 'a','s','d','f','g','h'};
  char *pointer1 = malloc( sizeof(char) * (sizeof( temp) + 1) );
  memset( pointer1, 0x00, sizeof( temp) + 1 );
  memcpy (pointer1, temp , sizeof( temp) );

here before copying any value into pointer1 we made sure that it has got enough space.
No need to cast malloc() retrun value. In sizeof( temp) + 1 1 is added for null character. Then we did memset() which filled the memory pointed to by  pointer1 with null. Just good and healthy practice.
Then you
memcpy ( pointer1 + sizeof(temp), pointer3, sizeof ( the temp array)

Again, does pointer1 has enough space for contents of pointer3? Do you own the memory area pointed by pointer1 + sizeof(temp)? It too will crash your program.
Now you either use realloc() or assign a bigger space to pointer1 with malloc() at earlier stage.
Why sizeof ( the temp array) here? Don't you think it should have the number of bytes in pointer3?
Finally in the definition of function1()
char *pointer2, *array2;
 // Now i need to have pointer2 point to contents of array2.
 pointer2 = &temp2;
 return pointer2

What does array2 do? Nothing! Then it should be removed.
To return just use
return temp2;

which means pointer2 is also useless.
Hope it helps.
Comment: Andrew, thanks for your excellent reply. Cleared up so many things.So I have added some new code here? Could you advise me on why packet[9] does not display the right contents even though array2[0] does have the right content ?
Answer: It's not really clear precisely what you're trying to do, but I think this is what you're after.  I've commented it to show what it's doing.
void function1(char *dest, size_t len);

int main()
{
  /* Allocate an array 'temp' */
  char temp[] = { 'a', 's', 'd', 'f', 'g', 'h' };

  /* Allocate an array 'array1', the same size as 'temp' */
  char array1[sizeof temp];

  /* Copy the contents of 'temp' into 'array1' */
  memcpy(array1, temp, sizeof temp);

  /* Call a function to copy new contents into 'array1' */
  function1(array1, sizeof array1);

  return 0;
}

void function1(char *dest, size_t len)
{
  char temp2[] = { 1, 2, 3, 4, 5 };

  /* Determine how much to copy - the _minimum_ of 'len' and 'sizeof temp2' */
  if (len > sizeof temp2)
  {
      len = sizeof temp2;
  }

  /* Copy contents of 'temp2' into 'dest' */
  memcpy(dest, temp2, len);
}

Comment: Thanks caf. The only diff. is I want to merge the contents of the array in the main loop and not in the function1 loop. ( Look at the changed code I have uploaded). Thanks for your time.
Answer: I cant seem to understand your code. The problem is simple but the code wont work.
Next is this piece of code
memcpy ( pointer1 + sizeof(temp), pointer3, sizeof ( the temp array)
I guess you want the final array to contain { a,b,c,d,e,1,2,3,4,5}.
Well i would advise you to check pointers and arrays from K&R and then attempt this problem again.