SQLServer 2005 Generate all INDEX as CREATE statement?

Question: I have two database with same structures, but one of them is missing INDEXES (i think i've missed out), i mean the table didn't have any INDEXES yet.
I was plan to generate CREATE INDEX for the database which have indexes but can't found any method available in Management Studio, yes we can generate script for tables, view, etc, but i was only need to script the INDEXES not anything else.
I know if we generate a script using IF EXIST can be solution, so if object already exist it doesn't recreate, but again for simplicity shake, i just want script the INDEXES.
Comment: [Generate script of All the indexes in a database](http://stackoverflow.com/questions/9089700/generate-script-of-all-the-indexes-in-a-database)
Comment: possible duplicate of [Generate CREATE INDEX statements in SQL Server](http://stackoverflow.com/questions/12672006/generate-create-index-statements-in-sql-server)
Answer: Try this:

Or just right-click on an index in Management Studio and select "Script Index as - Create To -" and then as appropriate.
Comment: yes i know that, infact i already use it... it generate all statement which included table, view(if any), stored procedure (if any), indexes, etc... what i need was only generate indexes
Comment: It provides a way to choose object types to script.
Comment: yes but it need to create tables script before indexes, no indexes only script
Answer: Not specific to just your need for Indexes, but a good way to keep multiple DBs in sync structure wise is to use a tool such as Redgate's SQL Compare, or AdeptSQL Diff, or similar.  These will show you the differences between 2 databases and generate and even run the required scripts to get them into sync.  Much easier than trying to do this manually.
Comment: yeah but it's not free, mean another cost, i need this only for a fix (which is my mistake not creating INDEX in second dataase)
Answer: Check out http://samsudeenb.blogspot.com/2007/11/scripts-to-drop-and-recreate-indexes-in.html - I think that is what you're looking for.
Or - as another answer also suggests - you might want to invest in a tool like Redgate.


Java method to return string from JTextField

Question: There is a main method which have different textfields with the same KeyListener class
 textfield1.addKeyListener( new TextfieldKeyListener() );
 textfield2.addKeyListener( new TextfieldKeyListener() );
 textfield3.addKeyListener( new TextfieldKeyListener() );

The KeyListener class is something like this.
public class TextfieldKeyListener implements KeyListener
{

 public void keyTyped( KeyEvent e )
 {

  //This method should get a string from any given textfield.
  system.out.println(textfield.getText().toString() );
 }

}

Is it possible that a KeyListener class takes a string from a textfield and print it?
One way is that three textfields should have three KeyListener classes but here should be one KeyListener class for all the textfields.
Comment: You might want to use the `getSource()` method of the `KeyEvent` object to get the source (the `JTextField` in this case) where the event occured.
Comment: Ok. But how to do that? Thank you.
Comment: `e.getSource()` returns the reference to the `JTextField` instance where the event occured. You can cast the return value to `JTextField` and access the methods like `getText()`. You can write code like `((JTextField)e.getSource()).getText()`.
Answer: public class TextfieldKeyListener implements KeyListener {
    public void keyTyped(KeyEvent e) {
        Object src = e.getSource();
        if (src instanceof JTextField) {
            JTextField textField = (JTextField) src;
            System.out.println(textField.getText());
        }
    }
}

Refer to the javadoc


Best way to call one javascript file from another

Question: I am a JavaScript newb but am familiar with the procedural/dynamic workings of Ruby. What is the simplest way to start executing code from a different JavaScript file? All I know how to do is execute code from one file.
Comment: You're looking for `require()`.
Comment: Perhaps you're looking for node's module system (there are frameworks that provide this for front-end as well.) Stack Overflow questions should be concerning a specific issue and including the relevant code. I suggest deleting this question as it will be closed.
Comment: @AlexMills What you mean by `execute` is quite unclear. You get access to the code from another file, then make calls to it or use it however you wish.
Comment: There is no concept of "loading" code in Javascript.
Comment: You need to read http://nodejs.org/api/modules.html
Comment: yes i think so, but does require just load the code but not execute the code from the other file?
Comment: express and other frameworks seem to do the require() stuff implicitly am i right?
Comment: are there any other ways besides require()? this question is legit. coming from java, the scripting language world is way different in a f'ed up way
Comment: @AlexMills Different doesn't necessarily mean f'ed up.  Different for sure though.  For what it's worth, JavaScript is flexible enough that you can structure your applications however you want.  You could easily recreate the structure of Enterprise FizzBuzz if you wanted.  https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition  I wouldn't though.  And remember, there are plenty of languages that compile to JavaScript these days.  Perhaps you'd appreciate something like TypeScript.  http://www.typescriptlang.org/  Or, JSIL:  http://jsil.org/
Answer: Node.js uses what is called the Common.JS module pattern.  A module exports an object, which is loaded by whatever required the module.
For instance, suppose I have a file called SomeClass.js:
exports = function SomeClass () {
  this.func = function () {
    console.log('func!');
  }
}

Now in my main app.js:
var SomeClass = require('./SomeClass.js');
var someClass = new SomeClass();
someClass.func(); // outputs "func!" to console

You can read more about this in the official docs:  http://nodejs.org/api/modules.html
Comment: @AlexMills Sorry, I have updated the example to include `./`.  Yes, this is needed for Node.js apps regardless of platform.  I think the docs said otherwise, but I've always needed it.  I also use Windows for Node.js development, and test on both Windows and Ubuntu.
Comment: yes, thanks. perhaps I need to include some nodeJS libraries to get the "require()" to work. Right now, I have two .js files in the same directory, and "var x = require('otherJSfile.js');" causes an error. The error being "cannot find module x.js"...I will figure this out, but still not always straightforward is it
Comment: VERY strangely, as I am on a Windows machine....this worked "var xz = require('./xz.js');" .... "./" is usually a solution on a unix machine...oh well...it works


If $P(A\cap B)=P(A)P(B)$ then $P(A\cap B^c)=P(A)P(B^c)$

Question: If $A$ and $B$ are independent such that $P(A\cap B)=P(A)P(B)$, how can I show $P(A\cap B^c)=P(A)P(B^c)$?
I have been thinking of showing this using something like:
$P(A\cap B^c)=1-P(A^c\cup B)=1-P(A^c)-P(B)+P(A^c\cap B)$
but I am not sure how to proceed.
Answer: Hint
$$\mathbb P(A)=\mathbb P((A\cap B^c)\cup (A\cap B))=\mathbb P(A\cap B^c)+\mathbb P(A\cap B)$$
Answer: Hint:
Note that $P(A)=P(A\cap B)+P(A\cap B^C)$.  Can you use what you already know about $P(A\cap B)$ to rearrange this usefully?


Conditional PHP works in Node Template but not in Views Custom Fields

Question: This code in my node template checks a CCK field and prints some text dependent on the value:
<?php if ($node->field_stock[0]['view'] == 'No' ) : ?>

Out of Stock

<?php endif; ?>

How can I do something similar with Views Custom Field's PHP filed type?
Thanks
Answer: If 'm not mistaken you are trying to move this logic out of your tpl.php template file by using views custom field.
You first need to get the name of the accessible fields with this code.
<?php print var_export($data, TRUE); ?>

Then you should be able input in the custom field something similar to this.
<?php if ($data->field_stock == 'No' ) : ?>

Out of Stock

<?php endif; ?>

There is more info here http://drupal.org/node/467190.
Answer: in templates of views you can seek/use the id of the node (nid).
for example, in this case I will use views-view-unformatted.tpl.php
<?php
$nodeIdItem = $view->result[$id]->nid; //get the id of the current item, in this case using $view->result inside the view results loop.
$node = node_load($nodeIdItem);
?>
<?php if ($node->field_stock[0]['view'] == 'No' ) : ?>

Out of Stock

<?php endif; ?>


Passively monitor IO wait spike on process level

Question: We use munin and monit to keep track of general stats about our vps, in last a couple of weeks, we have been running into issues where random IO wait spike is killing our server performance.
Since then we have been checking cron for possible suspect, but haven't found one that match spike patterns. Arriving on time to check ps aux for staled process isn't always possible, and result can vary even during the event.
So I am wondering if there are a better way to setup passive monitoring, preferably via munin/monit, that keep track of processes that are experiencing/causing IO wait the most?
(PS: I have used some of the suggestions in this Q&A, but haven't been able to pinpoint the cause yet.)
Answer: You might want to use Linux process accounting. If it's included in your kernel (I'm not sure about the current status), you can enable it with accton. The command sa will report the following data per process:
cpu - sum of system and user time in cpu seconds
re - "real time" in cpu seconds
k - cpu-time averaged core usage, in 1k units
avio - average number of I/O operations per execution
tio - total number of I/O operations
k*sec - cpu storage integral (kilo-core seconds)
u - user cpu time in cpu seconds
s - system time in cpu seconds

You might be interested in the avio and tio values of the processes.
The GNU accounting utilities manual as well as the Enabling Process Accounting on Linux HOWTO gives more details.
Answer: There may not be a process that's causing this. There's more likely an I/O subsystem or resource contention issue. I wouldn't be looking for a single process or group of processes that cause this. I'd be noting the conditions under which it happens.
Do you have the full specifications of your server, including the details of the disk setup? Please post them along with OS version/kernel - uname -a
edit -
I guess this is a VPS. Call or email your provider's support. Show them a graph and explain your situation.
Answer: You may be exceeding vps limits:
Even if you caught the processes with high io values, it will possibly be a generic apache-php or other web site related process. But that is not enough, you would need the actual url behind it. And when you find the url, it will NOT be the reason of the io problem, it will be a victim of it. Most cms's need ~1000 files to run, and any problem in hdd or cache will cause them to have huge io spikes. The actual reason for io could be on some other vps account, or you hitting the vps limits. Exceeding vps limits does cause io spikes with the limitations they have in place.
Other accounts or machine processes hogging the vps:
You could consider changing your provider!
I had a similar issue, spent weeks trying to diagnose it, then exchanging communication with deaf support bureaucracy, and finally got fed up and changed to another host. Now everything is suddenly fine and has been fine for a year! What happened? Same vps size, same code, even a slower machine, even a busier site and everything is smooth.
My guess at that time was, some other account hogging the hdd, with some huge daily forum backup, found this through Google. My other guess was some inefficient server backup software. Don't know. The similarity to your situation was, it started at a certain date, and it was quite periodic, and there were practically little change on my vps code or load itself. You can neither catch nor prove this with monitoring software restricted to vps walls.
That was a year ago. Nowadays there are some new headaches around.
Core dumps: This is the new fashion now, core dumping instead of error logging. Server histeria. When something goes wrong, the server starts core dumping a 200M file, and this keeps io high, so other things go wrong in a chain, and one gets 3-5 200M files saved at the busiest time of the day, stalling hdd.
imagick image processor: For some reason, imagick started working with 1G-50G temporary files. If that happens, it stalls hdd. I suspect Imagick is used by munin, what an irony.
Another practical suggestion: Keep munin open in a window, while you are working on something else. It auto refreshes every 5 minutes.  That way you will catch the spike. I used to catch io spikes like that. Your spike lasts for 30 minutes or so. You can easily catch it. Then you can do ps aux etc.


How do I replace words in a string that matches a keywords stored in an array with data from a table?

Question: I want to replace words in a string that matches a keywords stored in an array with data from matching column in a table.
My model is People
public class People()
{
public int Id { get; set; }
public string Title { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
}

The method i have tried is :
public void ProcessString(string message)
{
 using (DBEntities db = new DBEntities())
            {
                var people = db.People.ToList();

                foreach(var person in people)
                {
                    string[] keyword = {"#Title", "#Name", "#Surname"};

                        for (int i=0; i<keyword.Length; i++)
                    {
                        string updatedString  = Regex.Replace(body, keyword[i], matchingcolumndata);

                    }

                }
}

So instead of matchingcolumndata in the  string updatedString  = Regex.Replace(body, keyword[i], matchingcolumndata); line i want to put the data that is matching the column in my table People. Please help with the question if its not straight to the point.
Comment: You need to replace the keywords by what exactly ?
Comment: So you want to replace all of your string "columns"?
Comment: I want to replace the words in a string that matches the keywords stored in  the array by column's data. For example if there's **#Title** word in a string then replace it with **people.Title** data from the People table
Comment: Check this one: [http://stackoverflow.com/questions/12226691/replace-multiple-words-in-a-string-from-a-list-of-words](http://stackoverflow.com/questions/12226691/replace-multiple-words-in-a-string-from-a-list-of-words) [http://stackoverflow.com/questions/20220913/fastest-way-to-replace-multiple-strings-in-a-huge-string](http://stackoverflow.com/questions/20220913/fastest-way-to-replace-multiple-strings-in-a-huge-string)
Answer: From my understand it may be you are looking for
foreach(var person in people)
            {
                string[] keyword = {"#Title", "#Name", "#Surname"};
                body= body.Replace(keyword[0],person.Title);
                body= body.Replace(keyword[1],person.Name);
                body= body.Replace(keyword[2],person.Surname);
            }

Answer: It doesn't make any sense to declare your keywords array inside the loop. If you want to do this in a neat way (and subject to modifications easily), you can create a map between the keyword and the property of the Person. Like this:
using (DBEntities db = new DBEntities())
{
    var people = db.People.ToList();

    // maps each keyword to a property in the Person class
    var keywordPropertyMapping = new Dictionary<string, Func<Person, string>>()
    {
        { "#Title", p => p.Title },
        { "#Name", p => p.Name },
        { "#Surname", p => p.Surname }
    };

    foreach (var person in people)
    {
        foreach(var keywordFunc in keywordPropertyMapping)
        {
            body = body.Replace(keywordFunc.Key, keywordFunc.Value(people));
        }
    }
}

For any new keyword, you just add one simple line inside the dictioanry keywordPropertyMapping, and the magic works.
If you're not familiar with Func<T, TResult> which is one critical part of the Linq magic, then read the Docs:

Encapsulates a method that has one parameter and returns a value of
  the type specified by the TResult parameter.
You can use this delegate to represent a method that can be passed as
  a parameter without explicitly declaring a custom delegate. The
  encapsulated method must correspond to the method signature that is
  defined by this delegate. This means that the encapsulated method must
  have one parameter that is passed to it by value, and that it must
  return a value.


Segue to present contents of UITableViewController and NavigationController in iPad popover

Question: I have a segue that works for iPhone code to present the contents of a tableViewController embedded in a Navigation controller.
<code>- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UINavigationController *nc = (UINavigationController *)segue.destinationViewController;
    tvc = (MyTableViewController *)[nc.viewControllers lastObject];
    tvc.managedObjectContext = managedObjectContext;
}
</code>
tvc is a storyboard item, UITableViewController subclass, embedded in a navigationController. I am trying to find the easiest way to use the same setup on iPad, but present the contents in a popover, with a navigation controller, as opposed to presenting full screen as it's doing now. Else do I need to create a new viewController without using this setup?
Answer: I created a custom UIStoryboardPopoverSegue, with the following perform code:
<code>- (void)perform
{
    UITableViewController *src = (UITableViewController *) self.sourceViewController;
    MyController *dst = (MyController *) self.destinationViewController;

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:dst];

    UITableViewCell *cell = [src.tableView cellForRowAtIndexPath:[src.tableView indexPathForSelectedRow]];

    UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:navigationController];

    dst.popoverController = pop;

    CGSize size = CGSizeMake(475, src.view.frame.size.height);
    pop.popoverContentSize = size;

    [pop presentPopoverFromRect:cell.frame
                         inView:src.tableView
       permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown
                       animated:YES];
}
</code>
The downside of this is that it requires a seque that is custom to destination view controller.  The upside is an easy to implement and support, well functioning popover segue.


Tetrahedral numbers: what is the equation omitting the $\Sigma$

Question: I have heard of the famous Johann Carl Friedrich Gauss, and I've also heard of his triangular formula. Which goes like this:
When Gauss was young, his teacher gave him and the rest of his class the job of adding all the numbers from $1$ to $100$. The teacher was trying to get some time to herself, but this turned out to be futile. Gauss was able to solve it in some $15$ seconds, using a formula he came up with. Instead of using this:
$$
\sum^{100}_1 x=y
$$
He found a cunning technique to solve it. He noticed that if you broke the sequence in half and put the two ends above each other, like so:
$$
\begin{matrix}
100&99&98&97&96&95&\cdots&51\\
1&2&3&4&5&6&\cdots&50\\
\end{matrix}
$$
If you notice the columns, you will see that they add up to $101$.since there are $50$ of these, you get one simple equation, which is this:
$$101\times 50=5050$$
After surprising his teacher, he replaced some of the numbers with $x$, to get
$$(x+1) \cdot \frac{x}{2}=\triangle x$$
(Yes, i know I'm not using the triangle correctly) anyway, I have also read that the other famous triangle, Pascal's triangle, has an unusual property; the first layer on the left is full of $1$'s, the next is the counting numbers, then the triangular numbers, and then the tetrahedral numbers. I then came up with the idea that if there is a formula for triangular numbers, then there should be a tetrahedral formula. To begin, I used the sigma function:
$$\sum^x_1 (x+1) \cdot \frac{x}{2}=\triangle \triangle x$$
(Again, I didn't use the triangle sign correctly, and two stand for tetrahedron)
To begin my idea, I simplify Gauss's formula into a quadratic formula, which is
$$\frac{x^2+x}{2}$$
This is the same as the original formula, just simplified. To then get an idea of calculating tetrahedral numbers. I took the triangular numbers to get the $8$th tetrahedral number, which are:
$$\begin{matrix}
1&3&6&10&15&21&28&36
\end{matrix}$$
Adding the ends gets
$$\frac{x^2+x}{2}+1$$
With there being $\frac{x}{2}$ of these, I redid the formula again.
$$\left(\frac{x^2+x}{2}+1\right)\cdot \frac{x}{2}=\frac{x^3+x^2+2x}{4}$$
But this formula is still missing something, a number $y$, which is some number that directly relates to x somehow. Because when I put this formula through the first few numbers;
x is number, z is formula number, c is correct number
$$\begin{align}
x=1&\rightarrow z=1 \text{ and }c=1\\
x=2&\rightarrow z=4 \text{ and }c=4\\
x=3&\rightarrow z=10.5 \text{ but }c=10\\
x=4&\rightarrow z=22 \text{ but }c=20\\
x=5&\rightarrow z=40 \text{ but }c=35\\
x=6&\rightarrow z=54 \text{ but }c=56\\
x=7&\rightarrow z=101.5 \text{ but }c=84\\
\end{align}$$
As you can see,this is wrong. The first two are accurate, but the rest are wrong, and I don't get what happened with $6$.I don't think this is the correct formula, so I'll use differential and integral calculus to solve this. If $f(x)=\triangle \triangle x$, then $f'(x)=\triangle x$ and
$$\int \frac{x^2+x}{2}=y$$
Unfortunately, I am bad at integrals. Could someone show me how to integrate this? Thanks in advance
Comment: you might want to look at binomial coefficients https://en.wikipedia.org/wiki/Binomial_coefficient
Comment: XD @hypergeometric good sigma joke
Comment: $$\int\frac{x^2+x}2~\mathrm dx=\frac{x^3+\frac32x^2}6+C$$
Comment: @SimplyBeautifulArt ... So I was way off.
Comment: Except I don't think that's the right answer to the original question, either.
Comment: @PeterShor but simplybeautifulart is one of the best calculus people on the site. It's more likely that lightning strikes the Statue of Liberty twice in a minute then he's wrong
Comment: @Alexander Day: the integral is perfectly correct. (And since that seems to be your actual question, it was answered correctly.) But I don't believe the solution to the integral gives the tetrahedral numbers.
Comment: @PeterShor what? Lemme see...
Comment: It isn't. AWWW,   MANNN!!
Comment: It's interesting to note that Gauss could solve it in $\sum_{r=1}^5 r$ seconds... :)
Answer: By a smart generalization of the pattern, you can attempt the following formula for the tetrahedral numbers:
$$T_k=\frac{k(k+1)(k+2)}{1\cdot2\cdot3}.$$
Then you test the hypothesis by using the fact that the difference between two successive tetrahedral numbers, is a triangular number:
$$T_k-T_{k-1}=\frac{k(k+1)}{1\cdot2}.$$
We indeed have
$$T_k-T_{k-1}=\frac{k(k+1)(k+2)-(k-1)k(k+1)}{1\cdot2\cdot3}=\frac{3k(k+1)}{1\cdot2\cdot3}.$$
You will understand that this generalizes to higher order ($d$ dimensions), giving the formula
$$N_{d,k}=\binom{k+d-1}d=\frac{k(k+1)\cdots(k+d-1)}{1\cdot2\cdot\cdots d}.$$
Comment: So the top is $K^3+3K^2+2K$
Comment: @AlexanderDay: say the *numerator*.
Comment: And would the denominator would be y!, for y dimensions
Comment: @AlexanderDay: as written.
Answer: Note:
$$\underbrace{1+1+1+\cdots+1}_{n}=n$$
$$1+2+3+\cdots+n=\frac{n(n+1)}{2}$$
$$1+3+6+\cdots+\frac{n(n+1)}{2}=\frac{n(n+1)(n+2)}{6}$$
$$1+4+10+\cdots+\frac{n(n+1)(n+2)}{6}=\frac{n(n+1)(n+2)(n+3)}{24}$$
Can you guess the sum formula:
$$1+5+15\cdots+\frac{n(n+1)(n+2)(n+3)}{24}=?$$
Comment: Multiply by $\frac{n+4}{5}$
Comment: That is right. Luck.
Answer: Note that
$$\begin{align}
&\sum_{r=1}^n \binom r1&=\binom {n+1}2&=\frac {n(n+1)}2\\
&\sum_{r=1}^n \binom {r+1}2&=\binom {n+2}3&=\frac {n(n+1)(n+2)}6\\
&\sum_{r=1}^n \binom {r+1}3&=\binom {n+3}4&=\frac {n(n+1)(n+2)(n+3)}{24}\\
&&\vdots&\\
&\sum_{r=1}^n \binom {r+1}m&=\binom {n+m}{m+1}&=\frac {n^{\overline{m+1}}}{m+1}\end{align}$$
Perhaps this is what you are looking for.

Useful references:
Binomial coefficients
Hockey-stick identity
Rising factorials


Google App Engine NDB FloatProperty default value not working?

Question: I'm working in Google App Engine, Python 2.7, Standard Environment, using the ndb datastore.  I'm doing some updates to some of my NDB models, and one of the updates requires me to add a new property (a floating-point number) to my "Restaurant" model.  In the past, when I've needed to add properties to a model, I've always liked how NDB lets you specify default values when defining the model, like so:
class Restaurant(ndb.Model):
    ... some properties ...
    resto_fullresyncstamp = ndb.FloatProperty(default=0.0)

My understanding of this default value was, when the entity was fetched from the datastore, if there is no resto_fullresyncstamp property for a given entity, one is added to the local copy of the model, with the default value.  That way, I don't have to keep checking to see if everything is None, and I can assume the property will always have a value in my code.  So I should be able to do something like this ...
lastsync = ...
resto = Restaurant.query()....get()
if not resto:
    logging.warning("Failed to get restauraunt: %s" % username)
    self.error(404)
    return

if lastsync != 0.0 and resto.resto_fullresyncstamp > lastsync:
    logging.warning("Requiring client to perform full resynchronization")
    self.error(510)
    return

... and expect that, even if "resto_fullresyncstamp" is a new property in my model, and the particular restaurant has never had that property set explicitly anywhere, that I will still get a valid (0.0) value to compare against.
What I'm actually getting is this error message:
Traceback (most recent call last):
....
File "/base/data/home/apps/s~virt-som/1.406133528491612487/virtsom.py", line 5399, in post
if lastsync != 0.0 and resto.resto_fullresyncstamp > lastsync:

AttributeError: 'Restaurant' object has no attribute 'resto_fullresyncstamp'

Shouldn't this work?  I've been working with NDB for ages, and I've used the "default" parameter in my Models like this successfully before.  Am I missing something?
Answer: That's not how it works.
Whenever you add a new property to a existing model you have to put the old entities again into the datastore to get that property. To get it at all no matter what the default value is.
Comment: Well @janscas, manually adding the property by assigning a value to it seems to have sorted the problem out for me, but then what is the purpose of the "default" parameter? Why would you provide the property with a "default" value and then require the developer to manually assign the property a value before it can be used? It makes the "default" parameter worse than useless, it makes it misleading.  It also makes Google's own documentation wrong - [this article](https://cloud.google.com/appengine/articles/update_schema), complete with example, is exactly what I'm trying to do.
Comment: The default parameter does what it says. If you don't set it, it gets set to the default. BUT, that ONLY works in all entities that you save AFTER you change the model.
Comment: I'm sorry @janscas, but with all due respect, [this article](https://cloud.google.com/appengine/articles/update_schema) is exactly what I'm trying to do, and according to the article, when I add a new field to my entity, there will be entities already previously saved to the datastore, that don't have this new field.  To quote the article directly: "Whenever a Picture is read from Datastore num_votes and avg_rating will be populated (either with the value from Datastore, or the default specified in the model)."  This is what I'm expecting, and it's not working in the latest version of the API.
Comment: From the article you are reading: "Note that existing entries will not be automatically modified, a read-write of the entity is required before the new properties will be persisted to Datastore." You need to re-put the old entities to the datastore with the new schema. Hope this helps...
Comment: @SteveSauder another thing you can do that is very common is to use getattr on the model. like this: 'getattr(entity, property, default)' This way you will get a default value even if you didn't re-put the entity to update the schema.
Comment: I do understand that the entities need to be rewritten in order to have the new properties persisted.  But going back to the original post, I did exactly what the article says, and got an exception instead of the default value on read.  That's my issue. Getattr is another good solution...!


Percorrer listas de tamanhos diferentes

Question: Estou percorrendo duas listas e realizando uma operação, mas quando a lista menor acaba o for para e a operação não é 100% concluida. Como faço para que quando a menor lista acabar ela retorne para o primeiro elemento e que assim seja até a lista maior acabar?
m = []
x = [1,2,3]
y = [4,5,6,7,8,9]
for e, i in zip(x, y):
   f = e
   g = i
   f = f + g
   m.append(f)

Comment: uma dica para você conseguir resolver isso é fazer o  `for` para a lista maior e na menor você fazer um `if` verificando se ela já acabou ou não
Answer:
Como faço para que quando a menor lista acabar ela retorne para o primeiro elemento e que assim seja até a lista maior acabar?

Se você precisa que os elementos de x fiquem cíclicos até que toda a lista y seja percorrida, basta você utilizar a função itertools.cycle:
from itertools import cycle

x = [1,2,3]
y = [4,5,6,7,8,9]

for a, b in zip(cycle(x), y):
  print(a, b)

Isso produzirá a saída:
1 4
2 5
3 6
1 7
2 8
3 9

Perceba que para os elementos 7, 8 e 9 de y os valores de x foram se repetindo a partir do início.
Comment: funcionou perfeitamente, obrigado!
Answer: Claro que existem outras formas de fazer isso mas tentei manter a estrutura do seu código.
Neste caso utilizei o zip_longest do pacote itertools para percorrer as listas de acordo do tamanho da maior lista, veja que os valores faltantes da primeira lista são preenchidos com valores None
import itertools

m = []
x = [1,2,3]
y = [4,5,6,7,8,9]

for e, i in itertools.zip_longest(x, y, fillvalue = None):
    if e != None:
        f = e
        g = i
        f = f + g
    else :
        f = i
    m.append(f)

Output:
print(m)
[5, 7, 9, 7, 8, 9]

Caso queira que a menor lista se repita você pode utilizar o cycle do mesmo pacote itertools
import itertools

m = []
x = [1,2,3]
y = [4,5,6,7,8,9]

for e, i in zip(itertools.cycle(x), y):
    f = e
    g = i
    f = f + g
    m.append(f)
print(m)

Output:
[5, 7, 9, 8, 10, 12]

Answer: Use itertools cycle.
Assumindo que você saiba que x é a lista menor e usando compreensao de lista em uma linha apenas:
from itertools import cycle

x = [1, 2, 3]
y = [4, 5, 6, 7, 8, 9]
m = [a + b for a, b in zip(cycle(x), y)]
print(m)

Comment: funcionou aquii, valeeu
Answer: Assim, para cada elemento da lista X, ele percorrerá toda a lista Y antes de avançar para o próximo elemento da lista X.
m = []
x = [1,2,3]
y = [4,5,6,7,8,9]
for e in x:
   for i in y:
      f = e
      g = i
      f = f + g
      m.append(f)

Answer: Pelo que percebi, você está querendo percorrer as duas listas de forma simultânea e somar os valores correspondentes das duas lista, índice por índice. Para isso você deve perceber que existe três casos distintos para resolver, que são:

Caso 1: As duas listas tem o mesmo tamanho;
Caso 2: A primeira lista tem menor tamanho;
Caso 3: A primeira lista tem maior tamanho;

Para se resolver esta questão de forma bem abrangente, isto é, que possa englobar os três casos supracitados, poderemos implementar o seguinte algoritmo:
from itertools import zip_longest

m = []
x = [1, 2, 3]
y = [4, 5, 6, 7, 8, 9]

if len(x) < len(y):
    for e, i in zip_longest(x, y, fillvalue=None):
        if e is not None:
            f = e
            g = i
            h = f + g
        else:
            h = i
        m.append(h)
elif len(x) == len(y):
    for e, i in zip(x, y):
        h = e + i
        m.append(h)
else:
    for e, i in zip_longest(y, x, fillvalue=None):
        if e is not None:
            f = e
            g = i
            h = f + g
        else:
            h = i
        m.append(h)

print(m)

Observe que o bloco if verifica se o tamanho da lista x é menor que o tamanho da lista y e, se for, estas duas listas serão percorridas com o auxílio do método zip_longest completando os elementos faltantes da lista x com o valor None. Em seguida soma cada correspondência de elementos índice a índice, armazenando-os na lista m e, em seguida, exibe a lista m.
Já o bloco elif verifica se o tamanho da lista x é igual ao tamanho da lista y e, se for, estas duas lista serão percorridas com o auxílio da função zip, somando cada correspondência de elementos índice a índice, armazenando-os na lista m e, em seguida, exibi a lista m.
E por último temos o bloco else. Este bloco será executado caso o tamanho da lista x seja maior que o tamanho da lista y. Neste caso as duas listas são percorridas com o auxílio do método zip_longest completando os elementos faltantes da lista y com o valor None. Em seguida soma cada correspondência de elementos índice a índice, armazena-os na lista m e, em seguida, exibe a lista m.
Agora, caso você precise generalizar este problema para quaisquer duas listas, você pode utilizar o seguinte algoritmo:

from itertools import zip_longest

x = list(map(int, input('Digite os valores da lista "X": ').split()))
y = list(map(int, input('Digite os valores da lista "Y": ').split()))

m = []

if len(x) < len(y):
    for e, i in zip_longest(x, y, fillvalue=None):
        if e is not None:
            f = e
            g = i
            h = f + g
        else:
            h = i
        m.append(h)
elif len(x) == len(y):
    for e, i in zip(x, y):
        h = e + i
        m.append(h)
else:
    for e, i in zip_longest(y, x, fillvalue=None):
        if e is not None:
            f = e
            g = i
            h = f + g
        else:
            h = i
        m.append(h)

print(m)

Neste segundo algoritmo, quando aparecer a seguinte frase Digite os valores da lista "X": , você deve digitar todos os elementos da lista X, na mesma linha, separados por um só espaço e, em seguida, pressionar enter. E, quando aparecer a outra frase Digite os valores da lista "Y": , você deve digitar todos os elementos da lista Y, na mesma linha, separados por um só espaço e, em seguida, pressionar enter.
A partir desse momento o algoritmo realizará o resto das operações.

Percorrer listas de tamanhos diferentes


DbGeography.PointFromText() throws 'Latitude values must be between -90 and 90 degrees' for Japan

Question: Google tells me that Japan's lat/long values are (36,138) but .NET throws the error
24201: Latitude values must be between -90 and 90 degrees.
Any ideas why?
Answer: I have had the same issue as you, I was using DbGeography.PointFromText("Point(lat lng)") when the lat & lng arguments are expected the other way around. The complete answer is this:
// From Google, Japan's latitude: 36; longitude: 138
var lat = 36;
var lng = 138;
var location = DbGeography.PointFromText($"Point({lng} {lat})");

Comment: I have this solution, but it doesn't work..
 if (geoLocation.Longitude.HasValue && geoLocation.Latitude.HasValue)
            {
                var point = string.Format(CultureInfo.InvariantCulture.NumberFormat,
                                         "POINT({0} {1})", geoLocation.Longitude,
                                         geoLocation.Latitude);

                return DbGeography.PointFromText(point, 4326);
            }
Comment: This link no longer works. Can you please put whatever content was at the link in your answer rather?
Comment: when I tried to implement the same solution, I couldn't, because PointFromText has 2 parameters.
public static System.Data.Spatial.DbGeography PointFromText (string pointWellKnownText, int coordinateSystemId);
any Idea?


Unable to install GWASpoly to R

Question: How can I install R package GWASpoly. I have been trying from last week with bundle of different methods. but all went useless. Always showing this msg:

package 'GWASpoly' is not available (for R version 3.3.3)

Can any body help me out in this?
Comment: Did you try to download from here: http://potatobreeding.cals.wisc.edu/wp-content/uploads/sites/21/2016/08/GWASpoly_download.zip?
Comment: Yes exactly i tried the same way
Comment: Then How do you installed it in R? what command have you used?
Comment: If you have installed required package, try this `install.packages('~/Downloads/GWASpoly_download/GWASpoly_download/GWASpoly_1.3.tar.gz', repos = NULL, type = 'source')`. Replace `'~/Downloads/GWASpoly_download/GWASpoly_download/GWASpoly_1.3.tar.gz'` to the path in your machine.
Comment: Got my problem solved @mt1022
Comment: So what the problem leading to that error?
Comment: thanks for the assistance and kind time
Comment: exactly it was the same error that i was neglecting.
Answer: i) Go to this website: http://potatobreeding.cals.wisc.edu/software/
ii) Download the package
iii) In R, install using the following command: install.packages("~/Downloads/GWASpoly_1.3.tar.gz", repos = NULL, type = "source")

Unable to install GWASpoly to R


Question about an inequality which seems right but not easy to prove

Question: The origin problem is as follows:
let $a,b,c,d$ are positive real numbers,and $a+b+c+d=4$
prove:$$\frac{a^2}{b}+\frac{b^2}{c}+\frac{c^2}{d}+\frac{d^2}{a}\geq 4+\frac{1}{4}[(a−b)^2+(b−c)^2+(c−d)^2+(d−a)^2]$$
The solution is easy enough, which is to plug $4 = a+b+c+d $ into to $RHS$ and then move it to $LHS$, notice:
$$[\frac{(a-b)^2}{b}+\frac{(b-c)^2}{c}+\frac{(c-d)^2}{d}+\frac{(d-a)^2}{a}](a+b+c+d) \geq [(a−b)^2+(b−c)^2+(c−d)^2+(d−a)^2]$$
which would finally lead to the proof of the problem.
However, when I tried to solve the problem, I applied the Lagrange identical equation to the $RHS$ of the inequality, then I left out some quadratic term which finally leads to the inequality:
$$\color{Blue}{\frac{a^2}{b}+\frac{b^2}{c}+\frac{c^2}{d}+\frac{d^2}{a}\geq a^2+b^2+c^2+d^2}$$
Which seems quite right, but I'm not really sure if it is. However, numerical tests imply that it is right. But I'm still not sure.
That stuff is kind of like Chebyshev inequality but it can't be directly used here.
From another point of view, my question is whether the following strengthening of the origin inequality is right or not:
let $a,b,c,d$ are positive real numbers,and $a+b+c+d=4$
then:$$\frac{a^2}{b}+\frac{b^2}{c}+\frac{c^2}{d}+\frac{d^2}{a}\geq 4+\frac{1}{4}[(a−b)^2+(b−c)^2+(c−d)^2+(d−a)^2+(a-c)^2+(b-d)^2]$$
Comment: In your second displayed formula the factor (a+b+c+d) should be erased.
Comment: I just saw the similar inequality in a book, which holds for 3 positive numbers a,b and c. However, the book doesn't give an answer to that.
Comment: @Macavity Would you please kindly give me an example where that doesn't hold?
Comment: @Macavity Sorry to reply late. There are 3 inequalities in my question. The first is proved. I can't prove the second but my guess is that holds for the same $a,b,c,d$, which is equivalent to the 3rd inequality under this circumstance. Suppose a is biggest among $a,,b,c,d$, I did some numerical work and the 2nd inequality always holds. What's more, I saw a similar inequality in a book, but there are just 3 variables and no answer is given there. Thus I hold the view that the last 2 inequalities for $a,b,c,d$.
Comment: Sorry, but I mean the last 2 are the same, and the 1st is proved, would you please give me some advice how to improve my question.
Comment: Ah  didn't notice that.
Comment: By Michael Rozenberg , since not $\frac14$ ,but 1 is ok.Even if add two members ,no problem.
Comment: @TakahiroWaki Do you mean the coefficient? But I can't see why 1 is okay by the same method, at least not obvious.
Answer: $\sum\limits_{cyc}\frac{a^2}{b}-4=\sum\limits_{cyc}\left(\frac{a^2}{b}-2a+b\right)=\sum\limits_{cyc}\frac{(a-b)^2}{b}\geq\frac{\left(\sum\limits_{cyc}|a-b|\right)^2}{4}\geq\frac{1}{4}\sum\limits_{cyc}(a-b)^2$
Comment: Still, your last step is not right.
Comment: Thank you but I think this is the answer to the original problem, which I've already post.
Comment: @cxz why my last step is not right?
Comment: take a look again, it's obvious,isn't it?
Comment: @cxz Let $|a-b|=x$, $|b-c|=y$, $|c-d|=z$ and $|d-a|=t$. Hence, $(x+y+z+t)^2\geq x^2+y^2+z^2+t^2$. Am I wrong?
Comment: oh sorry,i made a mistake, it's correct


Persistent db connection in Django with multiple management commands

Question: I'm wondering If i run eg. 100 management commands in a background:
<code>/me/env/python manage.py mycommand --key=1
/me/env/python manage.py mycommand --key=2
/me/env/python manage.py mycommand --key=3
/me/env/python manage.py mycommand --key=4
...
/me/env/python manage.py mycommand --key=100
</code>
will Django use only one db-persistent connection for all commands, or creates 100 db-persistent connection (one per command)?
DB: MySQL
Answer: Django will create and close 100 connections - one per each process.


Como instalar biblioteca no Raspberry sem internet?

Question: Tenho um Raspberry pi 3 sem acesso a internet e preciso instalar nele a biblioteca RPI.GPIO, tenho como baixar de outra máquina e passar a biblioteca por pendrive. As minhas dúvidas são: Em que pasta coloco a biblioteca e que comando eu uso para instalar esta biblioteca?
Comment: Olá Matheus, em relação a `Raspberry Pi`, tente perguntar no [Raspberry Pi Stack Exchange](https://raspberrypi.stackexchange.com/), acho que por ser voltado para a mesma é mais fácil de encontrar sua resposta lá. Além disso, pela questão não ser específica para Raspberry, tente perguntar no [Super User](https://superuser.com/)
Answer: Você pode baixar o pacote referente a biblioteca que você quer utilizar,no Github por exemplo, e utilizar o comando pip install e encontrar o arquivo localmente no seu dispositivo.
Exemplo:
pip install ./downloads/SomeProject-1.0.4.tar.gz
Caso tiver alguma dúvida quando a sintaxe, dá uma olhada na documentação sobre isso:
https://packaging.python.org/tutorials/installing-packages/#installing-from-local-archives


Windows Server 2008 Super Slow

Question: I am on Windows Server 2008 OS Std, 32 bit. It started to slow down yesterday around 3pm. Opening files, restarting OS, opening any applications takes more then 5 minutes. then it started to work fine by 9 pm.
Now today again around 1 pm, it started to slow down....same issue as yesterday.
Things that I have tried.
Disconnected from Network totally.
Checked CPU & RAM usage within Task manager, and all is good.
Stoped the Word Wide Web Service to STOP IIS from serving sites.
Checked the event log, but could not find anything, unless i dont know where to look?
Comment: Since you said restarting the OS is slow, it could be a failing disk. Whats your disk configuration like, is RAID in use?
Comment: And what is safe mode like? Any different?
Comment: I'm with John.  I've seen RAID 5 with a dying disk seize up and become unresponsive during backups, for example.
Comment: its a staging server with a simple single  SATA HDD.
Answer: I would replace the disk as soon as possible, as it sounds like it could be failing. Since you have a single SATA disk, I hope you have a backup somewhere. The process of reading all data from the existing disk could cause it to fail completely and you could have an even bigger problem on your hands.
Comment: @highwingers Anything could happen, depending on what areas of the disk are being accessed etc. The thing is, its not slowing down at the same time each day. By the sounds of it, your restarts are slow too which suggests a hardware issue.
Comment: is it normal for HDD to be dead slow and then be normal again?
Comment: I just swap RAM stick...its working fine, i will wait until tomorrow to check what happens.
Comment: @highwingers. Sounds like you're in the right track, what prompted you to swap the ram out. Did you do a memtest?
Answer: To got back to the basics:
Did anything change?
New software installation?
Are there any server apps installed in addition to the standard installs (ie. Exchange)?
Is there potentially a dramatic load increase midday?

Windows Server 2008 Super Slow


Slow logon with "Applying your personal settings..." message- only occurs when domain user is member of a security group

Question: Domain user accounts on our network can log in in ~2 seconds when they are not a member of any security groups.
However, this logon time increases to ~35 seconds when the are a member of any security groups, with the following message displaying while logging in:

There is only 1 PC at this remote site (XP Professional), connecting to an SBS 2003 server over a LogMeIn Hamachi VPN link.
Once logged in, everything works as it should in terms of access to server resources and Active Directory integration.
DNS settings appears to be correct- at least as far as I can see:

I'm assuming this is related to drive mapping- but can't quite figure out why it's happening (only a problem at remote site, not the main office) or how to give these users access to certain resources without adding over half a minute to their logon time.
Does anyone have any ideas what might be going on here?
Answer: A good place to start would be to use the Resulatant Set of Policy wizard in the Group Policy management console. This will show you which Group Policies are being applied to the user. Run it with the user both in and out of the groups. This will show you which policies are being applied. Focus on the differences.
If that doesn't yield anything, investigate any login scripts running because of Group Policy. There may be conditional drive mappings based on group membership. If you can't pin down the source just by looking at the script, try running each line manually to see which one takes so long.
Comment: I hadn't thought of that- will try that now. Thanks
Comment: I am having problems getting RSOP to work. It says "RPC server unavailable" when I try to access the client from the server. All necessary services are running (RPC service and Remote Registry service), and the Windows firewall was temporarily disabled for testing. I just can't seem to get it to work. Perhaps I will have to skip using RSOP and find another way to troubleshoot this, unless I can find a way around this problem within the next couple of days.
Comment: Have you tried running `gpresult /r` from the machine in question while logged in as the problem user?
Comment: That doesn't work either. I will do some more troubleshooting tonight to see if I can get some more information about this problem.


Javascript Print not working in IE9

Question: Why is this code (Print iFrame) not working in IE.  What's a workable solution?

Comment: Just focus to the correct element: `getMyFrame.contentWindow.focus();`
Comment: I was trying to use `` but this would not work either do you know whats wrong?
Answer: getMyFrame.print(); should work to print the iframe.
Comment: print does not work on the iframe element, but the iframe's window

Javascript Print not working in IE9


ASP.NET Boilerplate MySql ApplySoftDeleteFilter

Question: I switch database of existing ASP.NET Boilerplate project from MS SQL to MySql. I have used Pomelo.EntityFrameworkCore.MySql. While switching I test that soft delete is working but when I call GetAll or GetAllIncluding methods over Repository, ApplySoftDeleteFilter not working. What could be a problem?
Comment: What is the current and expected behavior? Show relevant code.
Comment: @aaron no exception. Just when calling Repository.GetAllIncluding() method, IsDeteled=True rows also returned. But when use MS SQL database, all are working properly.
Answer: try to update to the latest ABP version.
And capture the t-sql to see what's running in MySql server.
Comment: I update latest ABP but did not help. I capture LINQ generated SQL, but there is no condition for "IsDeleted != true";
Comment: Alper I look at both MS SQL and My SQL generated SQLs. They differ. For MS SQL queries it consider IsDeleted = 0, but for MySQL queries did not. What could be a problem. Have you checked ABP framework core version with MySQL?


How to parse YouTube XML with PHP, including namespace descriptions

Question: Im trying to use PHP's SimpleXMLElement to parse a YouTube channel feed and include the description. I can do this easily to get the title and video url.
Here is what I have that can get the descriptions
$channel_id = 'UCtNjkMLQQOX251hjGqimx2w';
$yt_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=';
$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
$xml->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
$result = $xml->xpath("//media:description");
foreach($result as $r){
 print $r;
 print '
';
}

The Raw xml from YouTube looks something like this.
 <entry>
  <id>yt:video:OTYFJaT-Glk</id>
  <yt:videoId>OTYFJaT-Glk</yt:videoId>
  <yt:channelId>UCtNjkMLQQOX251hjGqimx2w</yt:channelId>
  <title>Guitar E.R. - Setting up wood shop for O Positive custom speaker cabinets.</title>
  <link rel="alternate" href="https://www.youtube.com/watch?v=OTYFJaT-Glk"/>
  <author>
   <name>Guitar ER</name>
   <uri>https://www.youtube.com/channel/UCtNjkMLQQOX251hjGqimx2w</uri>
  </author>
  <published>2020-10-18T16:38:51+00:00</published>
  <updated>2020-10-20T01:04:59+00:00</updated>
  <media:group>
   <media:title>Guitar E.R. - Setting up wood shop for O Positive custom speaker cabinets.</media:title>
   <media:content url="https://www.youtube.com/v/OTYFJaT-Glk?version=3" type="application/x-shockwave-flash" width="640" height="390"/>
   <media:thumbnail url="https://i4.ytimg.com/vi/OTYFJaT-Glk/hqdefault.jpg" width="480" height="360"/>
   <media:description>In this video Doctor John Moran of Guitar E.R. gives us a glimpse into the new wood shop where he will be building custom guitar and bass cabinets, likely under the name O Positive Cabinets.</media:description>
   <media:community>
    <media:starRating count="0" average="0.00" min="1" max="5"/>
    <media:statistics views="22"/>
   </media:community>
  </media:group>
 </entry>
 <entry>

As you can see the title and uri and pretty easy to get, but I'm having trouble combining my code to get the Title, URI and Description.
Here is my code to get the Title and URI
$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
foreach($xml->entry as $entry){
    print $entry->title;
    print '
';
    print $entry->author->uri;
    print '
';
}

After working on this for awhile I was able to come up with a solution, not sure if this is the best answer though.
<?
$channel_id = 'UCtNjkMLQQOX251hjGqimx2w';
$yt_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=';

$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
$xml->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');

$yt_data = array();
foreach($xml->entry as $entry){

    $videoid = (string)$entry->children('yt', true)->videoId;
    $yt_data[] = array(
        'id' => $videoid,
        'title' => (string)$entry->title,
        'description' => (string)$entry->children('media', true)->group->description,
        'img' => (string)'https://img.youtube.com/vi/'.$videoid.'/maxresdefault.jpg',
        'thumb' => (string)'https://img.youtube.com/vi/'.$videoid.'/mqdefault.jpg',
        'published' => (string)$entry->published,
        'updated' => (string)$entry->updated,
        'channel' => (string)$entry->children('yt', true)->channelId,
        'author' => (string)$entry->author->name,
        'uri' => (string)$entry->author->uri,
        'views' => (string)$entry->children('media', true)->group->community->statistics->attributes()['views'],
        'ratings_count' => (string)$entry->children('media', true)->group->community->starRating->attributes()['count'],
        'ratings_avg' => (string)$entry->children('media', true)->group->community->starRating->attributes()['average'],
    );

}

foreach($yt_data as $yt){
    print '
';
    print '';
    print '';
    print '
'.$yt['title'].'
';
    print '
'.$yt['description'].'
';
    print '
'.$yt['views'].' views • '.date('M j, Y',strtotime($yt['published'])).'
';
    print '';
    print '
';
}
?>

Answer: Your code seems to take the URL of author's channel ID which is the same for every video as opposed to the video title which is different for each video.
There's a parser library:
https://github.com/gbuckingham89/youtube-rss-parser/blob/master/src/Parser.php#L139
reading the code:
$video->id = (string) $video_xml->children('http://www.youtube.com/xml/schemas/2015')->videoId;
$video->url = (string) $video_xml->link->attributes()->href;
$video->title = (string) $video_xml->title;
$video->description = (string) $video_xml->children('http://search.yahoo.com/mrss/')->group->description;


append to multi-dimensional subdocument

Question: I want to add a new filed like "email" for each sub document in groups
<code>{ "_id" : 10,
  "groups" : [
        {

                "members" : ["Mark"],
                "name" : "My Friends"
        },
        {

                "name" : "Colleagues"
        },
        {

                "name" : "Buddies"
        },.....
],.. }
</code>
Please suggest me. Thanks in advance.
I am trying tis but its not working
db.users.update({},{$push: {"groups.$.email": 'abc'} }, true, true);
Comment: The question needs more information like what values would you set for each sub document's `email` field?
Comment: db.users.update({},{$push: {"groups.$.email": 'abc'} }, false, true);
Answer: To accomplish this you need to use the $elemMatch operator.  Specifically, you want:
<code>db.users.update({_id:10,groups:{$elemMatch:{name:"My Friends"}}},{$push: {"groups.$.email":"abc"}},false,true);
</code>
The important part is that the $elemMatch will set the array pointer to the exact element that matched, so you can push at that point.

append to multi-dimensional subdocument


"Object doesn't support this property or method" occurs, when I try to call method from applet

Question: "Object doesn't support this property or method" occurs from javascript, and stops at line, where process a call of method from applet, for Liferay Portal
javascript snippet:

html's snippet for applet:
<APPLET name="SignApplet" mayscript code="SignApplet.class" archive="<%=renderResponse.encodeURL(renderRequest.getContextPath() + "/lib/SignApplet.jar")%>, <%=renderResponse.encodeURL(renderRequest.getContextPath() + "/lib/crypto.tsp.jar")%>, <%=renderResponse.encodeURL(renderRequest.getContextPath() + "/lib/crypto.gammaprov.jar")%>" height=500 width=500 style="display:none;">
    <PARAM name="boxmessage" value="Please wait, while applet is loading">
</APPLET>

Comment: This may sound silly, but to prevent those kinds of issues, I usually verify that the object exists. Something like: if(document.applets["SignApplet"]){// do stuf . . .}
Comment: at what line the error occurs?
Comment: at this "applet.profileTestPKCS12(path_to_certificate, pass);"
Comment: What if it doesn't exist, but I must use this applet?
Answer: <APPLET name="SignApplet" ... style="display:none;">

W3C on the The 'display' property.

(value) none
      This value causes an element to not appear in the formatting structure (i.e., in visual media the element generates no boxes and has no effect on layout). Descendant elements do not generate any boxes either; the element and its content are removed from the formatting structure entirely.
Answer: In applet u should avoid using display:none..... when u defines as none, in visual media the element generates no boxes and has no effect on layout.... So better try visiblity:hidden property instead of display:none...
Comment: actually I do not need to show the applet, it does calculations


Combine Results from Two PDO queries

Question: I have the following line:
$products = $dbh->query("SELECT piP2Components.typeID, invTypes.typeName FROM piP2Components INNER JOIN invTypes ON piP2Components.typeID = invTypes.typeID ORDER BY invTypes.typeName ASC;");

I have another table, piP3Components and I would like to run the same query on it and have the results added to the $products variable. As the result is a PDOStatement object I can't simply array_push.
How would I go about doing this? Alternatively, I'm fairly new to using JOIN queries, is there a way to accomplish this in SQL without having the piP3Components.typeID results in a different column?
Thanks.
Answer: You have two options.
First, if the columns you're picking from each table have identical column types, you can use a UNION:
SELECT foo, bar, baz FROM something WHERE ...
UNION ALL
SELECT qux AS foo, meta AS bar, syntactic AS baz FROM elsewhere WHERE ...

Second, you can run both queries, then fetch the results for each and place them in a single array, using that array later instead of the statement handle when processing the results:
$results = array();

$sth_a = $pdo->prepare(...);
$sth_a->execute(...);
while($row = $sth_a->fetch(PDO::FETCH_ASSOC))
    $results[] = $row;

$sth_b = $pdo->prepare(...);
$sth_b->execute(...);
while($row = $sth_b->fetch(PDO::FETCH_ASSOC))
    $results[] = $row;

print_r($results);

Combine Results from Two PDO queries


Algorithm for computing current speed (not just average)?

Question: Often, one wants to determine the current speed of some process (computation, download, network transfer, rendering, ...) consisting of many steps, the duration of which is measured.  However, when taking just the last measurement, the display is unstable, so a common way is to smooth over some period.  On the other hand, this leads to the display "lagging behind" when the speed suddenly changes, so the displayed speed changes smoothly, while the underlying process has changed it speed very suddenly.
Given a long sequence of measurements of such steps (e.g. transferred blocks, iterations of a computation), I want to

display the current speed,
averaged over as long as possible, but
not averaging over periods during which the speed changed.

Graphically, this means I want to compute the current slope of a piecewise linear curve with noise, while automatically detecting the segment ends (slope/speed changes).
I can come up with algorithms myself, I can also apply some knowledge from geometrical curve fitting, but I wonder if there are non-trivial go-to solutions to this (seemingly very common) particular problem?  Any references, textbook examples, open source implementations that stand out?
Comment: The key phrase is "the current slope of a piecewise linear curve with noise", and some other comments apparently didn't recognize that.
Comment: The problem you're left with here is, in fact, the reason we're averaging on the first place - how to distinguish (a) a "spike" that corresponds to something spurious from (b)  a spike due to an actual change in the underlying trend?  As we can't predict the future, the trade-off we generally make is the latency you're talking about.
Comment: Get a volume of last piece of processed task. Divide it by taken time. This is called derivative https://en.wikipedia.org/wiki/Derivative.
Comment: I know, thanks, I already mentioned "slope", which is closely related to the first derivative.
Comment: When dealing with computers discrete world, the closest you can get to an instant speed is to take the time between two continious units of whatever you want to meassure and the difference is the instant speed meassured in units per time unit (milliseconds or nanoseconds most likely). For this not to hit performance you can choose to take the meassure every N number of processed units.
Comment: @Juan: I can come up with several possible ways to do that, that is not the problem. Simply put I wanted to know a known, optimal algorithm for computing N (not too small / too unstable, not too large / averaging over different speeds).
Answer: Not a complete solution, but maybe useful anyway:
You can do a linear regression over some accumulated samples. This gives you the current speed, and an expected value for the next sample, assuming that speed and noise didn't change.
If the next sample lies inside the expected range, update the regression parameters (can be done incrementally), thus refining the speed value and narrowing the expected range for the next sample.
If the sample lies outside of the expected, you probably had a speed change, and you restart the regression there (maybe including a few recent samples not to start from zero).
I didn't go deep into the math, but maybe the new-speed decision can be based on a degrading correlation coefficient instead of / in addition to the outlier check.
Answer: Since I am getting comments that point out obvious solutions, I'll describe my current idea(s) here, which I would like to check against literature / existing good implementations:

define a minimum period of time to average over (say, 5 seconds)
define a maximum such period (say, 1 minute)
during the process:

push new measurements into a FIFO
pop old measurements from the FIFO if their age exceeds the above maximum time
on every display update

initialize display period with minimum period (and compute speed from that period)
enlarge display period while it is smaller than the FIFO and the next sample "matches" the speed within the display period
update the display

The "matching" is the crucial part here; mathematically speaking, one would like to know the probability of the sample in question to come from the same distribution.  It is probably best to assume a normal distribution, but I have even examples in mind that are not monomodal.


How to place random objects in a scattered way in corona without overlapping

Question: I want to display three different types of object on a same screen randomly, all the objects are created randomly but they are not scattered and sometimes overlap on each other, I want these random object to be far away from each other and must not overlap on each other.... Please give any suggestions... thanks
<code>--first object to be displayed 5 times randomly on screen
math.randomseed(os.time())
for i=1, 5 do
local mrandom=math.random
local locx=mrandom(100,700)
local locy=mrandom(100,1140)
local obj1[i]=display.newImageRect('obj1.png',50,50)
obj1[i].x=locx
obj1[i].y=locy
--second object to be displayed 5 times randomly on screen
math.randomseed(os.time())
for i=1, 5 do
local mrandom=math.random
local locx2=mrandom(100,700)
local locy2=mrandom(100,1140)
local obj2[i]=display.newImageRect('obj2.png',50,50)
obj2[i].x=locx2
obj2[i].y=locy2
--third object to be displayed five times
math.randomseed(os.time())
for i=1, 5 do
local mrandom=math.random
local locx3=mrandom(100,700)
local locy3=mrandom(100,1140)
local obj3[i]=display.newImageRect('obj3.png',50,50)
obj3[i].x=locx3
obj3[i].y=locy3
</code>
Answer: Sounds like a need to detect collisions with out physics:
http://www.coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/
Basically in your for loop once you generate your object, try to place it at your random x,y then loop over a table of your other objects seeing if your new object is colliding with one of the previous.  If so, regenerate the x and y and try again.
Comment: How we can stop object on the screen to touch other object without physics after generating random object like you said and retrieving the values over a tables.. thanks for your help
Comment: when you create the new object, loop over your table of existing objects and see if they are colliding.  If they are, generate a new random x, y until it no longer overlaps.


alphanumeric sort in VIM

Question: Suppose I have a list in a text file which is as follows -
TaskB_115
TaskB_19
TaskB_105
TaskB_13
TaskB_10
TaskB_0_A_1
TaskB_17
TaskB_114
TaskB_110
TaskB_0_A_5
TaskB_16
TaskB_12
TaskB_113
TaskB_15
TaskB_103
TaskB_2
TaskB_18
TaskB_106
TaskB_11
TaskB_14
TaskB_104
TaskB_112
TaskB_107
TaskB_0_A_4
TaskB_102
TaskB_100
TaskB_109
TaskB_101
TaskB_0_A_2
TaskB_0_A_3
TaskB_116
TaskB_1_A_0
TaskB_111
TaskB_108

If I sort in vim with command %sort, it gives me output as -
TaskB_0_A_1
TaskB_0_A_2
TaskB_0_A_3
TaskB_0_A_4
TaskB_0_A_5
TaskB_10
TaskB_100
TaskB_101
TaskB_102
TaskB_103
TaskB_104
TaskB_105
TaskB_106
TaskB_107
TaskB_108
TaskB_109
TaskB_11
TaskB_110
TaskB_111
TaskB_112
TaskB_113
TaskB_114
TaskB_115
TaskB_116
TaskB_12
TaskB_13
TaskB_14
TaskB_15
TaskB_16
TaskB_17
TaskB_18
TaskB_19
TaskB_1_A_0
TaskB_2

But I would like to have the output as follows -
TaskB_0_A_1
TaskB_0_A_2
TaskB_0_A_3
TaskB_0_A_4
TaskB_0_A_5
TaskB_1_A_0
TaskB_2
TaskB_10
TaskB_11
TaskB_12
TaskB_13
TaskB_14
TaskB_15
TaskB_16
TaskB_17
TaskB_18
TaskB_19
TaskB_100
TaskB_101
TaskB_102
TaskB_103
TaskB_104
TaskB_105
TaskB_106
TaskB_107
TaskB_108
TaskB_109
TaskB_110
TaskB_111
TaskB_112
TaskB_113
TaskB_114
TaskB_115
TaskB_116

Note I just wrote this list to demonstrate the problem. I could generate the list in VIM. But I want to do it for other things as well in VIM.
Answer: With [n] sorting is done on the first decimal number
            in the line (after or inside a {pattern} match).
            One leading '-' is included in the number.

try this command:
sor n

and you don't need the %, sort sorts all lines if no range was given.
EDIT
as commented by OP, if you have:
TaskB_0_A_1
TaskB_0_A_2
TaskB_0_A_4
TaskB_0_A_3
TaskB_0_A_5
TaskB_1_A_0

you could try:
sor n /.*_\ze\d*/

or
sor nr /\d*$/

EDIT2
for newly edited question, this line may give you expected output based on your example data:
sor nr /\d*$/|sor n

Comment: oh yes, what i was trying was like this `%!sort -n`.
also can i edit my question. suppose I had my list as -
`TaskB_0_A_1
TaskB_0_A_2
TaskB_0_A_4
TaskB_0_A_3
TaskB_0_A_5
TaskB_1_A_0
` and then do the sort, it doesn't work. Can you tell me, how to achieve sorting of this, since `sort n` does sorting only on first decimal number.
Comment: @Raj pls edit your question then. but next time try to make the example better fit for your real data. this helps you get solution faster also save effort of people who will give help.

alphanumeric sort in VIM


AWS toolkit install error

Question: I am trying to install AWS toolkit by following the instructions. However, I am getting the following error with the latest version:
Cannot complete the install because one or more required items could not be found.
Software currently installed: AWS Toolkit for Eclipse Core (Required) 2.3.1.v201702201547 (com.amazonaws.eclipse.core.feature.feature.group 2.3.1.v201702201547)
Missing requirement: AWS Toolkit Core 2.3.1.v201702201547 (com.amazonaws.eclipse.core 2.3.1.v201702201547) requires 'bundle org.eclipse.m2e.core 1.5.1' but it could not be found
Cannot satisfy dependency:
From: AWS Toolkit for Eclipse Core (Required) 2.3.1.v201702201547 (com.amazonaws.eclipse.core.feature.feature.group 2.3.1.v201702201547)
To: com.amazonaws.eclipse.core [2.3.1.v201702201547]

I am using Eclipse Kepler version. I am however, able to install the older versions of AWS toolkit core. Also, for some reason, the .metadata/.plugins/com.amazonaws.eclipse.core/ directory in workspace doesn't have aws-sdk-java directory which is expected. Are these two related? Any reason why this is happening?
Answer: Thats because of your eclipse version.
You should just update your eclipse with a newer version than kepler.

AWS toolkit install error


Sort criteria is ignored in Views

Question: I have a view that shows related products by means of a contextual filter (PHP). If the returned nid values from the contextual filter is for example "20697, 20735, 20768, 20903, 20695, 21863" I want the nodes to be rendered in that order.
But they don't, I have tried all kinds of settings with no luck. The order given is in fact the delta of a certain field, but when I try to use the delta version of that field as a sort criteria, it is simply ignored, even if I add a relationship under "Advanced".
Anyone who recognizes this behaviour?
EDITED:
This is my contextual PHP filter. It fetches all the nid's entered in a certain node reference field as I explained above. And I want them to show in that same order, so that the admin user can sort the order by dragging the rows in the Node reference field:
$node = menu_get_object();
$value = null;
if(empty($node->field_lagg_till_givargrupp['und'][0]['nid'])){
    $node->field_lagg_till_givargrupp['und'][0]['nid'] = NULL;
    $value = FALSE;
         return $value;
} else {
        $limit = count($node->field_lagg_till_givargrupp['und']);
        for($i = 0; $limit > $i; ++$i){
            $group_nid = $node->field_lagg_till_givargrupp['und'][$i]['nid'];
            $group_node = node_load($group_nid);
            $limit2 = count($group_node->field_koppla_givare['und']);
            for($x = 0; $limit2 > $x; ++$x){
                $value .= $group_node->field_koppla_givare['und'][$x]['nid'].",";
            }
        }
        return rtrim($value, ",");
}

EDITED: this is the SQL query (I have added a few nids as an example);
SELECT node.nid AS nid, field_data_field_koppla_givare.delta AS field_data_field_koppla_givare_delta, field_data_field_koppla_givare.field_koppla_givare_nid AS field_data_field_koppla_givare_field_koppla_givare_nid, 'node' AS field_data_field_givargrupper_node_entity_type, 'node' AS field_data_field_huvudbild_givare_node_entity_type, 'node' AS field_data_field_tumnagel_givare_node_entity_type, 'node' AS field_data_field_underrubrik_givare_node_entity_type, 'node' AS field_data_body_node_entity_type, 'node' AS field_data_field_artikelnr_produkt_node_entity_type, 'node' AS field_data_field_pris_givare_node_entity_type
FROM
{node} node
LEFT JOIN {field_data_field_koppla_givare} field_data_field_koppla_givare ON node.nid = field_data_field_koppla_givare.entity_id AND (field_data_field_koppla_givare.entity_type = 'node' AND field_data_field_koppla_givare.deleted = '0')
WHERE (( (node.nid IN('20697', '20735', '20768', '20903', '20695') ) )AND(( (node.status = '1') )))
ORDER BY field_data_field_koppla_givare_field_koppla_givare_nid ASC

And this is the simple query without the delta field and without the order by criteria trying to sort in hook_views_pre_render() instead (see this question about that Sorting logic with anonymous function does not work):
SELECT node.nid AS nid, 'node' AS field_data_field_givargrupper_node_entity_type, 'node' AS field_data_field_huvudbild_givare_node_entity_type, 'node' AS field_data_field_tumnagel_givare_node_entity_type, 'node' AS field_data_field_underrubrik_givare_node_entity_type, 'node' AS field_data_body_node_entity_type, 'node' AS field_data_field_artikelnr_produkt_node_entity_type, 'node' AS field_data_field_pris_givare_node_entity_type
FROM
{node} node
WHERE (( (node.nid IN('20697', '20735', '20768', '20903', '20695') ) )AND(( (node.status = '1') )))

As you can see I am really stuck in all ways about this ...
Comment: Could you please add screenshot of contextual filter configuration?
Comment: If you don't use paging and the result will always be one page, you could manually sort the results in one of the [View hooks](https://api.drupal.org/api/views/views.api.php/group/views_hooks/7) that run after the query has run, ie. hook_views_pre_render. This can be done in your theme's template.php file.
Comment: Thanks for your answer, I will try that. I want them sorted in the same order as they appear in the concerned node reference field "field_koppla_givare" above.
Answer: You can use a custom sort in your module like
/**
* Implementation of hook_views_query_alter().
 */
function yourmodule_views_query_alter(&$view, &$query) {
  if ($view->name == 'your_machine_name_view') {
        $query->add_orderby('node', 'created', 'DESC');
    }
  }
}

Comment: Hi and thanks, I have tried this too and the criteria needed is still ignored. I added '$query->add_orderby('field_data_field_koppla_givare', 'delta', 'DESC');' But there is obviously something I don't understand in the views query itself.
Comment: Can you provide us your query? Go here: **admin/structure/views/settings **, and activate the option "Show SQL" , so we can see what order it is taking
Answer: PROBLEM SOLVED. Just in case anyone would have the same problem... there was in fact nothing wrong with my contextual filter. With help (thanks J. Reynolds) I solved the sorting issue with hook_views_pre_render(). See this question for the answer:
Sorting logic with anonymous function does not work

Sort criteria is ignored in Views


How do I prove that the following sum converges?

Question: I have the following problem:

Let $(X_n)$ be a sequence of i.i.d. exponential random variables with parameter $\lambda>0$. I need to show that $\forall \epsilon >0$ $$\sum_n \Bbb{P}\left(\frac{X_n}{log(n)}>\epsilon +\frac{1}{\lambda}\right)<\infty$$

My Idea was the following. Define $\Lambda_n=\left\{\frac{X_n}{log(n)}>\epsilon +\frac{1}{\lambda}\right\}=\left\{X_n\leq \log(n)\left(\epsilon +\frac{1}{\lambda}\right)\right\}^c$. Now I thought that for a fixed $n$ we have $$\Bbb{P}(\Lambda_n)=1-\Bbb{P}\left(\left\{X_n\leq \log(n)\left(\epsilon +\frac{1}{\lambda}\right)\right\}\right)=1-\int_{-\infty}^{\log(n)\left(\epsilon +\frac{1}{\lambda}\right)}\lambda e^{-\lambda x}dx$$
But somehow I get in trouble with this $-\infty$ since $\lambda >0$. So I wanted to ask, what did I oversee or is everything wrong?
Thanks for your help.
Answer: You can directly calculate.
You are actually making the mistake in the pdf of the exponential random variable.
If $X\sim \exp(\lambda) $ then the pdf $f(x)$ is given by:-
$f(x)=\begin{cases}\lambda e^{-\lambda x},\,x\geq 0\\0\,\text{elsewhere}\end{cases}$
$$P\left(\frac{X_n}{\ln(n)}>\epsilon +\frac{1}{\lambda}\right)=P(X_{n}>\ln(n)(\epsilon+\frac{1}{\lambda}))=\\\int_{\ln(n)(\epsilon+\frac{1}{\lambda})}^{\infty}\lambda e^{-\lambda x}\,dx=\exp\Big(-\lambda\ln(n)\Big(\epsilon+\frac{1}{\lambda}\Big)\Big)$$
$$=\exp\Big(\ln\Big(\frac{1}{n^{(\epsilon+\frac{1}{\lambda})}}\Big)\Big)$$
$$=\frac{1}{n^{\lambda(\epsilon+\frac{1}{\lambda})}}=\frac{1}{n^{\lambda\epsilon+1}}$$.
Now $\sum\frac{1}{n^{p}}$ converges if $p>1$ which it is in this case so you are done.
Comment: Sorry I don't see why we have $P(X_{n}>\ln(n)(\epsilon+\frac{1}{\lambda}))=\int_{\\ln(n)(\epsilon+\frac{1}{\lambda})}^{\infty}\lambda e^{-\lambda x}\,dx$
Comment: could you maybe explain this a bit more, because I only know a formula for $\Bbb{P}(X\leq a)$
Comment: You are making a mistake in the pdf.
Comment: ah yes I see thanks! I didn't see that, so my integral works but starts from $0$. then everything is correct right?
Comment: Exactly. Also you can directly use the cdf and get the value instead of integrating
Comment: perfect thanks!


Model free method for determining local structure in distances matrix

Question: Fairly broad question, but I'm really just looking for general ideas rather than specific solutions.
I have an N x N diagonally symmetrical matrix where each element is between ~1 and (realistically) ~200, and N is < 150. Each element in the matrix describes how closely coupled the the i,j indices into that position are (bigger meaning more closely coupled). There are sub-regions within the matrix that are 'islands' of closely coupled elements. Often these local regions extend out from the diagonal, but this doesn't necessarily have to be the case.
As an example see the image below (which I just found online but does a reasonable job of demonstrating the kind of dynamic range and the extent of noise).
I'm trying to figure out a method that I can pass the matrix to and essentially identify the sets of local regions that are more/most closely coupled. The challenge is I have a few of these matrices, and the signal-to-noise regarding these local clusters is highly variable, so a defined threshold value for what 'closely coupled' means doesn't really make sense as it'll vary on a case-by-case basis.
There's also quite a bit of noise, so regularization is a bit of a challenge, i.e. one or two of the elements may be ~200 while the rest are between 1 and 50, so normalizing by the max doesn't really help.
I'd originally been looking into spectral decomposition and this might be the way to go, but I'm wondering if a graph-theory style approach might work well, where a fully connected network is built and then vertices are cut in either a rank-order or a semi-stochastic way.
Both analytical or numerical ideas are welcome, efficiency is not in any way a priority. Basically I'm just trying to expand my analytical toolbox so any ideas are welcome.
Comment: One small thing: by definition local structure implies some kind of model.  Is there a particular class of "models" you have in mind when you mention "model-free"?
Answer: The islands/blocks correspond to groups of points that are similar to each other and less similar to others. So, a clustering approach might work. Many clustering algorithms can operate on similarity or dissimilarity matrices (e.g. hierarchical clustering, spectral clustering, and various graph-theoretic approaches). The details vary across algorithms, but they generally partition the data into clusters such that points within each cluster are in some sense similar to each other and/or dissimilar to others.
Answer: Use hierarchical clustering.
You may need to transform your similarity to a distance first, though.
The basic idea is to repeatedly join the two closest objects first, until you have reached a threshold or the desired number of clusters.
It is fairly standard to use this also for optimizing the matrix representation by rearranging it with best similarity.
E.g. this image:


Ridge\Lasso -- Standardization of dummy indicators

Question: Say I have a data set with say 5000 rows and about 150 columns (5000 samples, 150 predictors/features) and I'm interested in a applying a ridge or lasso regression. (Let us assume using a logit link function -- if it matters). Again, if it matters, we'll say that some features are highly correlated -- e.g., that more than half of them are actually dummy variable indicators. Let us also say we are more concerned with prediction accuracy than model interpretability (if we wanted a clean interpretation, we would not use a penalized regression!)
Now, the standard recommendation for penalized linear models is to standardize the data (I believe Tibshirani recommends this always. By standardize, I mean center and scale by the mean and standard deviation). However, this will destroy a lot of the structure inherent in the data set. It doesn't make sense to do this I think - since this will lead to what were binary indicators always being present in the model unless Lasso zeroes them out.
Is there a better way to deal with this issue? One could normalize the rows (e.g., Normalizer() in sklearn) which will preserve structure, but in practice this gives me vastly different results.
Should one standardize all inputs? Only non-binary ones? What about ordinal categories (say a column with 1,2,3,... measuring, say, years of education). Normalization or min/max scaling?
Clarification: By standardizing binary inputs, you remove the sparsity in the data set, as you must first subtract the mean. This is what I mean by "destroying the structure".
Comment: Could you say more about what you mean by "this will destroy a lot of the structure in the data set"? Best to edit the question rather than just respond in a comment, as comments sometimes disappear.
Comment: I have clarified in my original post.
Comment: upgrading a Related question to a Linked one:  https://stats.stackexchange.com/q/359015/232706
Answer: You have identified an important but perhaps under-appreciated issue: there is no single one-size-fits-all approach to normalize categorical variables in penalized regression.
Normalization tries to ensure that penalization is applied fairly across all predictors, regardless of the scale of measurement. You don't want penalization of a predictor based on length to depend on whether you measured the length in millimeters or miles. So centering by the mean and scaling by the standard deviation before penalization can make sense for a continuous predictor.
But what does one mean by the "scale of measurement" of a categorical predictor? For a binary predictor having 50% prevalence, normalization turns original values of 0 and 1 into -1 and +1 respectively, for an overall difference of 2 units on the normalized scale. For a binary predictor having 1% prevalence, original values of 0 and 1 are transformed to approximately -0.1 and +9.9, for an overall difference of 10 units on the normalized scale.
Between binary predictors having these properties, normalization thus introduces a factor of 5 into their relative transformed scales, and thus in their sensitivities to penalization, versus the case in the original 0/1 coding. Is that what you want? And are normalized categorical predictors more "scale-free" so that the binary and continuous predictors are in some sense penalized fairly with respect to each other? You have to make that decision yourself, based on knowledge of the subject matter and your goals for prediction.
Harrell's Regression Modeling Strategies covers this in section 9.10 on Penalized Maximum Likelihood Estimation. As he notes, there is a further problem with multi-category predictors, as results of normalization can differ depending on the choice of reference value. In this case, try penalizing together the difference of all coefficients for the same categorical predictor from their mean, instead of penalizing each coefficient individually.
You do have some flexibility in choosing how to penalize. Some standard software, like glmnet() in R, allows for differential penalization among predictors, which Harrell discusses as an alternative to pre-normalizing the predictor values themselves so that the net result is scale-free. But you still have to grapple with the issue of what you wish to consider as the "scale" of a categorical predictor.
If you have no useful information from subject-matter knowledge about how best (if at all) to scale your categorical predictors, why not just compare different approaches to scaling them as you build the model? You should of course validate such an approach, for example by repeating the entire model-building process on multiple bootstrap resamples of the data and testing the model predictions on the original data. With your interest in making useful predictions, this provides a principled way to see what prediction method works best for you.

I appreciate the issue of destroying the sparse structure provided by binary/dummy coding, and that can be an issue with the efficiency of handling very large data sets that are coded as sparse matrices. For the scale of your problem, with just a few thousand cases and a couple of hundred predictors, this isn't a practical problem and it will make no difference in how the regression is handled: however you might have normalized the categorical variables, each will still have the same number of categories as before, just with different numerical values (and thus different sensitivity to penalization).
Note that normalization by rows does not solve the problems discussed here and may exacerbate them. Normalization by rows can be a useful step in situations like gene expression studies, where all measurements are essentially on the same scale but there might be systematic differences in overall expression among samples. With mixes of continuous predictors measured on different scales together with categorical predictors, however, row-normalization won't be helpful.
Comment: glmnet has an option to normalise within its algorithm, so the dataset remains sparse


Ungit stopped working after updating

Question: After updating Ungit stopped working. Please tell me what is the problem? When I run ungit an error:

module.js:327
      throw err;
      ^
Error: Cannot find module 'npm'
      at Function.Module._resolveFilename (module.js:325:15)
      at Function.Module._load (module.js:276:25)
      at Module.require (module.js:353:17)
      at require (internal/module.js:12:17)
      at Object. (C:\Users\ashev_000\AppData\Roaming\npm\node_modules\ungit\src\sysinfo.js:10:11)
      at Module._compile (module.js:409:26)
      at Object.Module._extensions..js (module.js:416:10)
      at Module.load (module.js:343:32)
      at Function.Module._load (module.js:300:12)
      at Module.require (module.js:353:17)
      at require (internal/module.js:12:17)
Answer: I had a similar problem. However, I installed ungit for the very for time and experienced the same error.
It seems that this issue is specific to  v0.10.2
In my case I just wanted to see a working version of ungit. I did not need a specific feature of the latest  version, so I uninstalled the fresh install and reverted  to the prior version.
npm uninstall -g   ungit
npm install –g ungit@"v0.10.1"
There's a ticket on github for this issue
"Launching error after fresh install #766"
https://github.com/FredrikNoren/ungit/issues/766

Ungit stopped working after updating


Drag and drop + Scroll

Question: Estou desenvolvendo uma aplicação one eu tenho 2 listas com droppable areas e um side menu com draggable itens.
Quando eu uso o scroll a posição do item que estou arrastando é alterada adicionando o movimento do scroll.
Eu tentei aplicar o que encontrei nesse tópico: https://stackoverflow.com/questions/5791886/jquery-draggable-shows-helper-in-wrong-place-after-page-scrolled mas ou eu estou aplicando errado ou não está funcionando mesmo.
Eu também tentei esse: http://jsfiddle.net/7AxXE/ mas é mostrado um erro dizendo que "draggable is not a function".
Aqui vcs podem acessar um exemplo da minha aplicação e simular o erro:
https://denisorlandidesouza.outsystemscloud.com/Test/
Clicando no botão "Ok" no topo à direita será mostrado um side menu, quando arrastar o item para a área principal e usar o scroll você verá o comportamento.
Answer: O seu jsfindle esta funcionando, o unico problema 'e que vc nao colocou nenhum evento para mostrar que o drag and drop funciona visualmente.
Acredito que vc precisa de algo assim:

$('.item').draggable({
    cursor: 'pointer',
    snap: '#itens',
    revert: 'invalid',
    scroll: false,
});

$('#itens2').droppable({
    accept: '.item',
    drop: function(e, ui) {
        $(this).append(ui.draggable.css('position','static'));
    }
});

$('#itens').droppable({
    accept: '.item',
    drop: function(e, ui) {
        $(this).append(ui.draggable.css('position','static'));
    }
});

Item 1

Item 2

Item 3

Item 4

Item 5

Comment: o jsfindle era só um exemplo do que eu estava tentando implementar. Ele na verdade não é meu.

Deixei um exemplo da minha aplicação aqui: https://denisorlandidesouza.outsystemscloud.com/Test/

Vou tentar implementar o que você sugeriu.

Obrigado.
Comment: @Denis entao coloque codigo do que vc esta tentando fazer na sua aplicacao na sua pergunta. Se minha resposta responde sua duvida nao esqueca de marcar como respondido.

Drag and drop + Scroll


Error when clicked a JButton - ActionEvent is not performed

Question: Here I have a GUI window and it basically ask the user to select a JRadioButton and type something in a JTextField, then choose confirm/cancel.
It is a project which we have to make a UML-to-Java text file. User would enter class information and choose a UML relationship, and this programme have to print out the Java clsas text on a JTextField. Just like when you create a new class in eclipse.
what I want to do is make a boolean[] to store an array of booleans, when user selects JRadioButton_A it'll store true and when user select JRadioButton_B it'll store false.And also I want the things typed in JTextField to be checked by a checkName(), if the method returns false, the string will be stored in an ArrayList<String>.
Below is my code - and the error is that when user clicked confirmButton, the programme just stopped and have no reaction. Also, there's some problems in getName() method and the boolean[] for storing true and false. When user needs to input name again, it would save the discarded sting/boolean into the array. Is there any better way to make this programme? I feel like I am complicating things and there should be a simpler way to make it.
Here's my UI class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;

public class AddClass extends JFrame{
Scanner input = new Scanner(System.in);
ClassName classObject = new ClassName();

private JRadioButton publicButton, privateButton;
private JLabel className;
private JTextField inputClassName;
private JButton confirmButton;
private JButton cancelButton;

public AddClass(){
    super("Add class");
    setLayout(new FlowLayout());

    publicButton = new JRadioButton("public", true);
    privateButton = new JRadioButton("private", false);
    className = new JLabel("Class Name: ");
    inputClassName = new JTextField(10);
    confirmButton = new JButton("Confirm");
    cancelButton = new JButton("Cancel");

    add(publicButton);
    add(privateButton);
    add(className);
    add(inputClassName);
    add(confirmButton);
    add(cancelButton);

    ButtonGroup group = new ButtonGroup();
    group.add(publicButton);
    group.add(privateButton);

    Handler handler = new Handler();
//      Handler2 handler2 = new Handler2();
//      Handler3 handler3 = new Handler3();
    confirmButton.addActionListener(handler);
//      publicButton.addActionListener(handler2);
//      privateButton.addActionListener(handler3);
    }// end constructor AddClass()

private class Handler implements ActionListener{
    public void actionPerformed(ActionEvent event){
        String name = inputClassName.getText();
        classObject.addName(name);
        while (classObject.checkName(name) == true){
            JOptionPane.showMessageDialog(null, "Class name invalid. " +
                    "\nEntered name should not contain java keywords or equal to other existing names. " +
                    "\nPlease try again."); // doesn't work
            name = input.nextLine();
            classObject.addName(name);
        }// end if
        JOptionPane.showMessageDialog(null, "Class saved."); // doesn't work
        name = input.nextLine();
        classObject.addName(name);

    }// end actionPerformed()
}// end Handler class

private class Handler2 implements ActionListener{
    public void actionPerformed(ActionEvent event){
        boolean b = true;
        b = classObject.setPP();
        }
    }

private class Handler3 implements ActionListener{
    public void actionPerformed(ActionEvent event){
        boolean b = false;
        b = classObject.setPP();
        }
    }

}// end AddClass

Here's my class for storing user input
import java.util.Scanner;
import java.util.ArrayList;

public class ClassName {
Scanner input = new Scanner(System.in);
JavaKeywords keyObject = new JavaKeywords();

private ArrayList<String> className = new ArrayList<String>();
private String name = new String();
private int size = className.size();
private Boolean[] bArray = new Boolean[size];

public void addName(String name){
    this.name = name;
    className.add(name);
}// end addName()

public boolean checkName(String name){
    boolean check = true;
    for (int i=0; i<=size; i++){
        if (keyObject.containsKeyword(className.get(i)) || name.equals(className.get(i))){

            boolean o = false;
            check = o;
        }// end if
    }// end for
    return check;
}// end checkName

public boolean setPP(){
    boolean b = true;
    return b;
}

public void addPP(Boolean[] bArray){
    this.bArray = bArray;
    for (int i=0; i>=size; i++){
        bArray[i] = setPP();
    }
}// add a Array of boolean. for className[i], its class type = item[i] in bArray.
             // public = true, private = false
public String getPublicPrivate(){
    String p = "";
    for (int i =0; i<=size; i++){
        if(bArray[i]=true)
            p = "public";
        else
            p = "private";
    }
    return p;
}

public void setName(String name){
    this.name = name;
}//end setName()

public String getName(){
    return name;
}// end getName()

public ArrayList<String> getAllNames(){
    return className;
}// end getAllNames()

public void setAllNames(ArrayList<String> className){
    this.className = className;
}// end setAllNames()

}//end class

Answer: In the ActionListener for the confirm button the Scanner instance input in this statement blocks waiting on input from the user
String name = input.nextLine();

The ActionEvent won't complete until a value has been entered.
Solution: Add a JTextField as an alternative means to read input from the user.
Comment: I just changed it to  **String name = inputClassName.getText();**
and the actionEvent is performed. But then I cannot close the window using the "x" on top right corner. The programme just stopped. Also the checkName() method doesn't work.... :( Please help?
Comment: 1) You need to call `setDefaultCloseOperation` for the `JFrame` 2) `checkName()` is outside the scope of this question - please post a new question
Answer: The reason your program "stops" when the user clicks the confirm button is that the program is waiting for input on the command line.  Specifically, your input variable is tied to System.in (i.e. command line).
You can just type your answer at the command line, hit Enter nad your program will continue.
OTOH, I suspect that using the command line is not what you want as you are using a GUI.  Instead you can add an input field (e.g. a JTextBox).
Comment: Thanks! I've just corrected that, and it continued for awhile until the **showMessageDialog** popped up. After i closed the message box, the programme "stops" again :(
Comment: The next line (`name = input.nextLine();`) still waits for input from the command line


Is it possible to have nested intervals (open or closed) such that$\bigcap^{\infty}_{n=1}I_n$ contains exactly two elements?

Question: It's not a homework problem but I saw it in an Analysis book. How would I go about constructing this infinite set of nested intervals? If the intersection contains only two elements, my thinking is that the $nth$ interval has to contain just an infimum and supremum, where $inf(I_n) \leq sup(I_n)$. If anyone has any ideas I'd like to see.
Thanks!!
Comment: Hint: Let our two numbers be $a$ and $b$. Each of the intervals contains $(a+b)/2$.
Comment: It is not possible. The intersection of nested intervals is again an (possibly null) interval. It either has 0, 1 or and infinite number of points.
Comment: Are you saying that this is possible but you don't know how? Because it sounds pretty impossible to me...
Answer: Assume that it is true, i.e. $\bigcap I_{i}=\{x,y\}$ with $x\neq y$, then we have that the interval $(x,y)$ is nonempty. Take $z\in (x,y)$. Then for any $I_{i}$ we have $x,y\in I_{i}$, so $z\in (x,y)\subset I_{i}$ as $I_{i}$ is an interval. Hence $z\in I_{i}$ for all $i$, so $z\in\bigcap I_{i}=\{x,y\}$, i.e. $z=x$ or $z=y$, which is a contradiction.
Answer: If $x<y$ are such that $x\in\bigcap I_i$ and $y\in\bigcap I_i$, then for each $i$ we have $x\in I_i$ and $y\in I_i$ and therefore $[x,y]\subset I_i$ since $I_i$ is an interval. Hence $[x,y]\subset\bigcap I_i$. This proves that if two distinct points are in $\bigcap I_i$, then $\bigcap I_i$ contains every point between those two as well. As a corollary, if $\bigcap I_i$ contains at least two points, then it contains an infinite number of points.


model compare by anova test

Question: I did model test of in anova. once mod1 vs mod2 , other mod2 vs mod1. Does it matter? Can I compare two models of different df and different parameters?
anova(reg5,reg4, test="Chisq")

Analysis of Variance Table
Model 1: Gas ~ CODload
Model 2: Gas ~ Flow * CODin
  Res.Df   RSS Df Sum of Sq  Pr(>Chi)
1     65 27786
2     63 12835  2     14952 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

> anova(reg4,reg5, test="Chisq")

Analysis of Variance Table
Model 1: Gas ~ Flow * CODin
Model 2: Gas ~ CODload

  Res.Df   RSS Df Sum of Sq  Pr(>Chi)
1     63 12835
2     65 27786 -2    -14952 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Answer: 1) These tests are for comparing nested models (that is, where the smaller model could be obtained from the more general model if some of the parameters take specified values). Your models don't appear to be nested.
2) When you call anova, put the more general model (larger model d.f. & smaller residual d.f.) second. It feels backward to me but that appears to be how it works; see ?anova.lm.
Comment: @Stat Possibly. Under which conditions will that be the case?
Comment: Even if the two models are not nested, it is still possible to apply `anova`. But in this case, the test statistic would be approximately Chi-square distributed.
Comment: I read it in the paper but I cannot recall the title! I am almost sure about it ... have no idea about the conditions.
Comment: @Stat if you see it again, I'd really love to read the details. It seems like there'd be problems with this (such as the smaller model sometimes having larger model sum of squares and sometimes smaller), so I assume there's some conditions required, but I can't see what they'd be.

model compare by anova test