How can I backup / restore my Sql Server 2008 Database?

Question: I have a database that is around .. 20ish Gig. I need to (manually*) back up this database, 7zip the backup and then DOWNLOAD this backup from my server to my desktop. (Rinse, repeat sorta  often).
I knew that the size would be big, I split my tables into a number of Files and FileGroups :-

So the files XWing_Boundaries and XWing_BoundayStreets are (more or less) static data**. So I put those in their own file and filegroup so I don't need to back these up and download them. I only want to download (and restore locally) the dynamic data, which is the file XWing. I don't care about the log file either.
So, is there a trick so I can just backup/download/restore the single file/filegroup I need only, instead of grabbing -all- the data, each time?
Notes:

* We have nightly full backups, so that's all fine / not an issue.
** I manually change this data once in a blue moon. I even toyed with the idea of making these files READONLY for the suggest extra speed increase, too.
Comment: @Holocryptic - *wink* .. Oh! and this question is not a trap, either.
Comment: I'm not even going to lie.  I +1'd based solely on your database names and your avatar.
Comment: It's not a bad question, either :)
Answer: Sure, you can backup specific filegroups/files. See http://technet.microsoft.com/en-us/library/ms186865.aspx for full details.
Backing Up Specific Files or Filegroups
BACKUP DATABASE database_name FILE = XWing TO <backup_device>

You can also add WITH COMPRESSION to compress the backups prior to writing to disk.
Comment: sure? there's no mention of FILEGROUPS or FILES in that sample code, above.... Maybe want to elaborate your answer using my Filenames / filegroups?
Comment: XWing is your file's logical name, which is referenced in the sample code. I didn't know the database_name, so I left "database_name" there for you to substitute in.
Comment: but would that backup your ENTIRE db? all files/filegroups?
Comment: Sorry, I just realized I left off the "FILE =" part of the syntax - I'm a dolt and it's late.
Comment: Ahh!! u've updated your post.. kewl :) that does help now! I'll give that a go.


Perl: Adding more elements to array created from hash

Question: I have a perl script where we are converting Hash into array-
my %info = (
          'name'    => $1,
          'ip'      => $2,
          'parent'  => '',
          'isLocal' => 0,
          'clip'    => 0,
          );
my @siteinfos = ();
push( @siteinfos, \%info );

Now I am able to accesss the array by-
foreach my $site(@siteinfos) {
if ( $site->{'name'} eq  $sitename)  {
 .....
}

First I am not sure how this conversion actually works.Secondly,Now I want to add more elements to this array in (key,pair) format.How can I do so?
Comment: Possible duplicate of [Perl: array and hash](http://stackoverflow.com/questions/40049853/perl-array-and-hash)
Comment: Where did `sinfos` var come from?
Comment: ... and besides that one (see [this answer](http://stackoverflow.com/a/40050190/2877364)), try [this question](https://stackoverflow.com/q/32771192/2877364).
Comment: edited the code
Answer: First, there is no conversion.
my @siteinfos = ();

defines an array @siteinfos and sets to the empty list. Of course, you could have achieved the same result by using just
my @siteinfos;

Next, the statement
push( @siteinfos, \%info );

pushes a reference to the hash %info to @siteinfos as the sole element of that array.
Next, the loop
foreach my $sinfo (@siteinfos) { # assuming original is a typo

ends up aliasing $sinfo to that sole hash reference in @siteinfos. In the body of the loop, you access an element of the original hash via the reference you pushed to @siteinfos.
Now, things would have been different if you had done:
push @siteinfos, %info;

That would have set @siteinfos to a list like the following (the order is non-deterministic):
$VAR1 = [
          'ip',
          undef,
          'clip',
          0,
          'isLocal',
          0,
          'name',
          undef,
          'parent',
          ''
        ];

The undef are there because the match variables were empty when I ran the script. Now, if you want to push another key-value pair to this array, that's trivial:
 push @siteinfos, $key, $value;

But of course, by assigning the hash to an array, you are losing the ability to simply look up values by key. In addition, you can keep pushing duplicate keys to an array, but if you assign it back to another hash:
my %otherinfo = @siteinfos;

then only the last version of an even-indexed element (key) and its corresponding odd-indexed element (value) will survive.
Comment: Nonono, you only need 5k more rep for your t-shirt. ;) We can all have a Stack Overflow t-shirt party at YAPC::EU in Amsterdam this summer.
Comment: We not only have the same kind of answer, we even used the same way to format it. :D
Comment: @simbabque If you want to combine the second part of my answer regarding pushing more key-value pairs to the array, I'll delete mine.
Answer: You are not converting anything. You are making a new array named @siteinfos.
my @siteinfos = ();

Then you add one entry, which is a reference to the hash %info.
push( @siteinfos, \%info );

The array now holds a reference to %info. That's $siteinfo[0].
In your loop you iterate all the elements in @siteinfos. The hashref now goes into $sinfo.
If you want to add more keys and values to that hashref, just put them in.
foreach my $sinfo (@siteinfos) {
    if ( $sinfo->{'name'} eq  $sitename)  {
        $sinfo->{foo} = "bar"; # as simple as this
    }
}


php date format conversion from d/m/Y to date('D, j M Y H:i:s')

Question: I'm trying to convert the date in php but not getting any success. Any help will be really appreciable.
My code:
$playing_dates='06/03/2013|13/03/2013|20/03/2013|27/03/2013|03/04/2013|10/04/2013|17/04/2013|24/04/2013|01/05/2013|08/05/2013|15/05/2013|22/05/2013|29/05/2013';

$playing_dates = explode("|", $playing_dates);
for($i=0; $i<count($playing_dates);$i++){
    $playing = strtotime($playing_dates[$i]);
    echo $playing_dates[$i]." --> ".$playing." -->";
    echo date('D, j M Y H:i:s', $playing)."
";
}

Actual Output:
06/03/2013 --> 1370214000 -->Mon, 3 Jun 2013 00:00:00
13/03/2013 --> -->Thu, 1 Jan 1970 01:00:00
20/03/2013 --> -->Thu, 1 Jan 1970 01:00:00
27/03/2013 --> -->Thu, 1 Jan 1970 01:00:00
03/04/2013 --> 1362355200 -->Mon, 4 Mar 2013 00:00:00
10/04/2013 --> 1380841200 -->Fri, 4 Oct 2013 00:00:00
17/04/2013 --> -->Thu, 1 Jan 1970 01:00:00
24/04/2013 --> -->Thu, 1 Jan 1970 01:00:00
01/05/2013 --> 1357344000 -->Sat, 5 Jan 2013 00:00:00
08/05/2013 --> 1375657200 -->Mon, 5 Aug 2013 00:00:00
15/05/2013 --> -->Thu, 1 Jan 1970 01:00:00
22/05/2013 --> -->Thu, 1 Jan 1970 01:00:00
29/05/2013 --> -->Thu, 1 Jan 1970 01:00:00

Expected output:
06/03/2013 --> 1370214000 -->Wed, 6 Mar 2013 00:00:00
13/03/2013 --> -->Wed, 13 Mar 2013 00:00:00
20/03/2013 --> -->Wed, 20 Mar 2013 00:00:00
27/03/2013 --> -->Wed, 27 Mar 2013 00:00:00
03/04/2013 --> 1362355200 -->Wed, 3 April 2013 00:00:00
10/04/2013 --> 1380841200 -->Wed, 10 April 2013 00:00:00
17/04/2013 --> -->Wed, 17 April 2013 00:00:00
24/04/2013 --> -->Wed, 24 April 2013 00:00:00
01/05/2013 --> 1357344000 -->Wed, 1 May 2013 00:00:00
08/05/2013 --> 1375657200 -->Wed, 8 May 2013 00:00:00
15/05/2013 --> -->Wed, 15 May 2013 00:00:00
22/05/2013 --> -->Wed, 22 May 2013 00:00:00
29/05/2013 --> -->Wed, 29 May 2013 00:00:00

Comment: This might help you: http://stackoverflow.com/questions/5597833/strange-behaviour-of-my-code-with-strtotime/5597905#5597905
Comment: close to an exact duplicate of [PHP strtotime returning false](http://stackoverflow.com/questions/15687822/php-strtotime-returning-false) asked less than an hour ago and with comprehensive answers
Answer: try with this and see if it works:
for($i=0; $i<count($playing_dates);$i++){
    $date = DateTime::createFromFormat('d/m/Y', $playing_dates[$i]);
    $newformat = $date->format('D, j M Y H:i:s');
    $playing = strtotime($date);

    echo $playing_dates[$i]." --> ".$playing." -->";
    echo $newformat ."
";
}

EDIT:
<?php
$playing_dates='06/03/2013|13/03/2013|20/03/2013|27/03/2013|03/04/2013|10/04/2013|17/04/2013|24/04/2013|01/05/2013|08/05/2013|15/05/2013|22/05/2013|29/05/2013';
$playing_dates = explode("|", $playing_dates);
for($i=0; $i<count($playing_dates);$i++){
    $date = DateTime::createFromFormat('d/m/Y', $playing_dates[$i]);
    $newformat = $date->format('D, j M Y H:i:s');
    $playing = strtotime($date->format("Y-m-d"));

    echo $playing_dates[$i]." --> ".$playing." -->";
    echo $newformat ."
";
}
?>

06/03/2013 --> 1362524400 -->Wed, 6 Mar 2013 18:31:10
13/03/2013 --> 1363129200 -->Wed, 13 Mar 2013 18:31:10
20/03/2013 --> 1363734000 -->Wed, 20 Mar 2013 18:31:10
27/03/2013 --> 1364338800 -->Wed, 27 Mar 2013 18:31:10
03/04/2013 --> 1364940000 -->Wed, 3 Apr 2013 18:31:10
10/04/2013 --> 1365544800 -->Wed, 10 Apr 2013 18:31:10
17/04/2013 --> 1366149600 -->Wed, 17 Apr 2013 18:31:10
24/04/2013 --> 1366754400 -->Wed, 24 Apr 2013 18:31:10
01/05/2013 --> 1367359200 -->Wed, 1 May 2013 18:31:10
08/05/2013 --> 1367964000 -->Wed, 8 May 2013 18:31:10
15/05/2013 --> 1368568800 -->Wed, 15 May 2013 18:31:10
22/05/2013 --> 1369173600 -->Wed, 22 May 2013 18:31:10
29/05/2013 --> 1369778400 -->Wed, 29 May 2013 18:31:10

Comment: Also note, should have `!` as the first modifier to the format, to reset all time fields to `0`, after that [it will work fine](http://viper-7.com/GijAtL).
Comment: Hey Akram thanks alot for your effort mate but I'm afraid it's not really working for me.
Answer: The strtotime() function takes in argument in the mm/dd/yyyy format
Refer here for the various date formats.
So use 03/06/2013 to get Wed, 6 Mar 2013 00:00:00
Answer: strtotime() takes most formats and turns it into a unix timestamp.  However, it's going to mix up MM/DD/YYYY and DD/MM/YYYY.
There are a lot of solutions here... one is to just explode the datestamp, using the format you're expecting.
foreach($playingdates as $datestamp) {
    list($day, $month, $year) = explode('/', $datestamp);
    $playtime = mktime(0, 0, 0, $month, $day, $year);
    // do whatever you want with $playtime
}


MomentJS timestamp parsing shows wrong date

Question: I am using moment JS in my Angular project and I tried to parse a timestamp I got from server.
the problem is that moment outputs : January 18, 1970 4:03 PM for the timestamp : 1526636521. which is not what I get form the online epoch converter. ( Friday, May 18, 2018 9:42:01 AM)
this is my moment call : moment.utc(data.TimeStamp).format('LLL') ;
Answer: You have to use moment.unix instead of moment.utc

To create a moment from a Unix timestamp (seconds since the Unix Epoch), use moment.unix(Number).

var data = {
  TimeStamp: 1526636521
};
console.log( moment.unix(data.TimeStamp).format('LLL') );

Answer: As the number you are using is number of seconds from 1970 Jan 1st,
moment.utc takes in number of milliseconds,
So either use,
moment.unix(1526636521).toString()  // moment.unix takes in number of seconds
moment.utc(1526636521000).toString()  // Add three zeros to number.


How should I display fields for a content type?

Question: I have a complex content type with several user-defined fields. In Drupal 6, I used Contemplate to display the fields in an HTML table. I've just upgraded to Drupal 7 and I'm wondering how to best achieve this now. The Contemplate project seems to suggest that Drupal 7's field display features are the way to go. But how do I create and populate an HTML table with a node's fields in Drupal 7? Do I extend/override a theme?
Update: To clarify, let's say I have several nodes with the same content type history. How do I display each of these nodes in the same specific way, including some of the user-defined fields I've attached to that content type. In addition, I use the pathauto module to give each of these nodes a path like history/<year>.
Answer: You can use display suite module to theme your node.

Display Suite allows you to take full control over how your content is
  displayed using a drag and drop interface. Arrange your nodes, views,
  comments, user data etc. the way you want without having to work your
  way through dozens of template files. A predefined list of layouts (D7
  only) is available for even more drag and drop fun!

If you want to theme your node using code, then you should create template for your node. Check this link for reference.
Answer: Show fields for a content type, you have to create a new file node.tpl.php inside your theme.
And you have to use the function var_dump, like that:
var_dump(array_keys($content)) //so you can see all the fields availables
After that, you can print all the fields inside the node.tpl.php in using the function render:
print render($content[FIELDNAME])
Check this url
https://api.drupal.org/api/drupal/modules!node!node.tpl.php/7
Comment: I ended up using this answer. While Views seemed promising, in the end, the quickest way for me to replicate my `contemplates` was to extend an existing theme with files such as `node--history.tpl.php`.
Answer: As as option you can check views module, it basically provides ui to query and display your entities(nodes,users etc.) in different formats(table is one of them) out of the box.
Comment: Thanks @arpitr. I've enabled the Views (and Views UI) modules and had an initial look at creating a view. It seems this module is geared towards displaying groups of nodes. How do I use it to override the default display for a content type? For example, if I have content type called `test` and a node of type `test`, how do I configure a view to display this node in the way I want, in particular by placing fields where I want them?
Comment: views have filters, you just have to add a filter your view to show results filtered by nid
Comment: is this a `filter criteria` or a `contextual filter`? I've tried both and selected `Content: nid` as the filter, but what do I set the `operator` and `value` to for the former or the settings for the latter?
Comment: filter criteria is what you need to add , as the name suggest we use context filter when we need to filter results on basis of some context for dynamic behaviour but in your case we know the node id. use operator as is equal to and value as the node id, to get the node id for your node, you can go and edit your node and see the url it will be like node/edit/%nid% so this %nid% is the value you need to put into your view filter.
Comment: Actually @arpitr I'm not sure I know the node id. Either that or I'm misunderstanding something. I have many nodes of the same content type and I want to apply the view to all of them when I display the nodes individually. So, for example, if nodes 3 and 4 are both of type `test` and I visit `/node/3` or `/node/4` I want the view to apply. I've added a filter criteria with the operator set to `equal to` and the value set to `%nid%` but it does not have any effect. The default display for these nodes is still used.
Answer: As arpitr already wrote, the best way would be to use Views and its contextual filters. Add contextual filter: node id (nid) and provide default value nid. Add the fields you want and enable block display.
E. g. you have an article with "address" field. If you enter the address, it will be displayed in that block only on that page (the one that has address entered). You can display data in table format, grid, list, unformatted etc.
You can find out more about contextual filters here:
https://drupal.org/node/1578558


Java Spark/Velocity Templates/SQL2o

Question: I'm using the Spark Java Web Framework with Apache's Velocity Template Engine in order to help design a responsive web application that pulls data from a SQL database. Using SQL2o I've created some Java objects of custom class types, i.e. user, group, site, etc.
I've checked and the list of objects created is populated. When I then go to put my object list into a hashmap and return a ModelandView, for some reason my list is there but I can't use any of its properties in vtl.
Relevant portion of main method and Spark code:
public static void main(String[] args) {

        WEB_LOGMGR loggr = new WEB_LOGMGR(true);
        WEB_DBMGR dbmgr = new WEB_DBMGR(true, loggr);
        Model backend = new ScadaModel(dbmgr, loggr);

        System.out.println(dataToJson(backend.getUsers()));

        staticFiles.location("/");
        staticFiles.externalLocation("/");
        String layout = "/templates/layout.vtl";
        //secure("public/keystore.jks", "password", null, null);

        before("/form", (request, response) -> {
            boolean authenticated = false;
            // ... check if authenticated
            if (!authenticated) {
                halt(401, "You are not welcome here");
            }
        });

        get("/", (req, res) -> {
            HashMap pdata = new HashMap();
            pdata.put("template", "/templates/main.vtl");
            return new ModelAndView(pdata, layout);
        }, new VelocityTemplateEngine());

        get("/users", (req, res) -> {
            HashMap pdata = new HashMap();
            pdata.put("template", "/templates/users.vtl");
            pdata.put("users", backend.getUsers());
            return new ModelAndView(pdata, layout);
        }, new VelocityTemplateEngine());

Relevant portion of Parsed User VTL

SCADA Users

            #foreach( $user in $users )

                        $user.firstName

            #end

Relevant portion of Layout VTL

#parse( $template )

Answer: I have found my answer! The class that defined my user object was not defined as "public" and as such the data was inaccessible to the template file.
To anyone that has followed the tutorials over on the sparkjava site and tried the annotation processor Project Lombok, you'll have created a java file with multiple class definitions, none of which are declared public. Lombok should take care of this for you. If however you're like me and you didn't like how hacky Lombok felt, you may have created class files and copied and pasted the code out of your single java file into individual class definition files. Make sure to declare your classes public!

Java Spark/Velocity Templates/SQL2o


"Conditional types" won't work with real function?

Question: I'm reading https://www.typescriptlang.org/docs/handbook/2/conditional-types.html
I'm wondering why this won't work?
interface IdLabel {
  id: number
}

interface NameLabel {
  name: string
}

type NameOrId<T extends number | string> = T extends number
  ? IdLabel
  : NameLabel;

function createLabel<T extends number | string>(idOrName: T): NameOrId<T> {
  if (typeof idOrName === 'number') {
    return { id: 1 }
  } else {
    return { name: 'foo' }
  }
}

However, this will work (which is the example given by the doc), but this is totally useless it even not returns any value...
function createLabel<T extends number | string>(idOrName: T): NameOrId<T> {
  throw "unimplemented";
}

Does the conditional types only works with "type definition"..?
Comment: See https://github.com/microsoft/TypeScript/issues/33912
Answer:
The short a answer is you can't. No value will be assignable to an unresolved conditional type (a conditional type that still depends on a free generic type variable). The only thing you can do is use a type assertion.

From: https://stackoverflow.com/a/52144866/12397250
So what you can do is:
1. Type assertion
function createLabel<T extends number | string>(idOrName: T): NameOrId<T> {
  if (typeof idOrName === 'number') {
    return { id: 1 } as NameOrId<T>
  } else {
    return { name: 'foo' } as NameOrId<T>
  }
}

2. Overload the method
function createLabel<T extends number | string>(idOrName: T): NameOrId<T>;
function createLabel(idOrName: number | string): IdLabel | NameLabel {
  if (typeof idOrName === 'number') {
    return { id: 1 }
  } else {
    return { name: 'foo' }
  }
}


Compress Images that are already uploaded

Question: I working on a website which has a content type. The content type contains an image field. Thousands of images uploaded already. Because image size is very big, there is requirement to compress the size of images.
I found a module Image API Optimize, but this module works only when a new image is uploaded. But my requirement is that I want to compress the images which are already uploaded.
Can anybody help me solve this problem?
Answer: A module like Max Image Size will resize the original images that have already been uploaded, but I don't believe it is possible to also use an optimization on the images when they are resized.  You can only specify a new maximum width and height.


Override the page title with a value from an API response

Question: I built a custom Drupal module. The module fetches data from a 3rd-party API and builds a block. I need to use a value from the API response to override the title (and head_title) of the page that the block is placed on.
I tried saving the value to the node in the Block controller:
$node = $this->getContextValue('node');
$node->page_title_override = $apiValue;

And then using that from hook_preprocess_html().
function myModule_preprocess_html(&$variables) {
  $pageTitleOverride = $node->page_title_override;
  $variables['head_title']['title'] = $pageTitleOverride;
}

It appears that the preprocess hook executes before the block controller, so $pageTitleOverride is empty.
Do you have any idea on a better approach?
Comment: Outsource the API fetching into a custom [service class](https://www.drupal.org/docs/drupal-apis/services-and-dependency-injection/structure-of-a-service-file), and then both block and preprocess function should retrieve their data from that service. Drush has a generate command to create the boilerplate code required for a service class.
Comment: @Hudri that works, thank you!
Answer: You are saving the value to the node in the Block controller only temporary, but blocks are not necessarily rendered in the same request. The block could have been cached a long time ago before the page is built or, if the block is not cacheable, it can be cached in the page as a placeholder so that the preprocess hook runs in a later request.
So what you can do depends on how cacheable the block and the title are. The module metatag provides more tools to set the page title dynamically, either through attached html_head metadata or tokens.


A floating button fixed at the bottom of UITableview with scrollViewDidScroll not working properly

Question: I am using this piece of code to make a UIButton floating fixed at
bottom of the UITable, but initially the UIButton will append exactly below
the last cell of the UITableView, only when I scroll the page the UIButton will
get to the bottom, what can I do to make the button come to the bottom at first?
Thanks in advance!
  - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        CGRect frame = self.chatButtonView.frame;
        frame.origin.y = scrollView.contentOffset.y + self.tableView.frame.size.height - self.chatButtonView.frame.size.height;
        self.chatButtonView.frame = frame;

        [self.view bringSubviewToFront:self.chatButtonView];
    }

Comment: As per your requirement you should add button to the footerView of UITableView
Comment: I called this piece of code on viewdidload but also did the same thing, not sure why:-(void) setUpChatButton {
    CGRect frame = self.chatButtonView.frame;
    float height = [[UIScreen mainScreen] bounds].size.height;
    frame.origin.y = height - self.chatButtonView.frame.size.height;
    self.chatButtonView.frame = frame;

//    [self.view bringSubviewToFront:self.chatButtonView];
    [self.tableView.tableFooterView addSubview: self.chatButtonView];
}
Answer: Here, i have added floating button in tableview, same way you can add any other control.
// Create button in `ViewDidLoad` and add to tableview.
- (void)viewDidLoad
{
    [super viewDidLoad];
    // *** Creat a button, calculate Y position at bottom of table, assign frame & add to tableview ***
    _btnFloating = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat yPos = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(_btnFloating.bounds);
    [_btnFloating setFrame:CGRectMake(0, yPos, [UIScreen mainScreen].bounds.size.width, 50)];
    [self.tableView addSubview:_btnFloating];

    // *** Adjust Inset and scroll indicator of tableview ***
    self.tableView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(_btnFloating.bounds), 0.0);
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(_btnFloating.bounds), 0.0);

    // *** Add observer on tableview frame change to update floating button frame ***
    [self.tableView addObserver:self
                     forKeyPath:@"frame"
                        options:0
                        context:NULL];
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    if([keyPath isEqualToString:@"frame"]) {
        [self adjustFloatingViewFrame];
    }
}

- (void)adjustFloatingViewFrame
{
    CGRect newFrame = _btnFloating.frame;

    newFrame.origin.x = 0;
    newFrame.origin.y = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(_btnFloating.bounds);

    _btnFloating.frame = newFrame;

    [self.tableView bringSubviewToFront:_btnFloating];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self adjustFloatingViewFrame];
}

// Don't forget to remove Observer on Tableview otherwise it will lead to crash.
-(void)dealloc
{
    [self.tableView removeObserver:self forKeyPath:@"frame"];
}

Comment: There is great Magic of these piece of code, the layout seems to be fine, the button is now happy to stay at the bottom, but when I pop that uitableview, the APP crashes, and here is the error message:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x14085d200 of class UITableView was deallocated while key value observers were still registered with it. Current observation info:
Comment: @CodeFarmer i have updated my answer and added `dealloc` method to remove observer, Add `dealloc` method and code snippet from answer to your code. Happy coding :)
Comment: Love life, love coding, love stackoverflow!
Answer: You can do something like,
  UIButton *chatBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
[chatBtn setTitle:@"Chat" forState:UIControlStateNormal];
[chatBtn setBackgroundColor:[UIColor lightGrayColor]];

[chatBtn addTarget:self action:@selector(chatbuttonClick) forControlEvents:UIControlEventTouchUpInside];  //chatbuttonClick is method that handle button's action

self.myTableView.tableFooterView = chatBtn;

There is no need to change frame in scrollViewDidScroll. You have to set tableFooterView instead of adding subview to it.
Comment: I tried you new solution, but it stay at the bottom of all the cells instead of bottom of the "screen", in this case, if I scroll the tableview, the button is also move with it.
Comment: Actually I have also tried tableFooterView, but the button is still append exactly below the last cell of the UITableView instead of the bottom of the screen, is details that I have dismissed?
Comment: Yes, I just want to let it floating at the bottom of the screen permanently
Comment: then it is very simple. Add button yo your view (i.e `self.view`) and button frame should be `(0,self.view.frame.size.height - 30,self.view.frame.size.width,30)`
Answer: Another solution for this question is by using a container view, which may be more clear to work with.
Answer: Using addChildViewController make tableView above.


Trocar logo de acordo com a página

Question: Olá.
Preciso que o logo do site troque de acordo com a página que o usuário visita.
Tenho um site feito em PHP no qual o topo é um include. Por isso pensei em trocar o logo via Javascript.
Coloquei o ID="logo" na img e um ID no body das páginas que terão um logo diferente. Assim esperava que através do If/Else, verificando se havia ou não o ID no body, o src da imagem se alterasse. Mas não está rolando. Não sou programador e cheguei até aqui pesquisando no Google. rs
Eis meus códigos:
HTML
<img id="logo" src="<?=$img_dir?>/logo_ultraclimber-resgate.png" alt="Ultra Climber Treinamentos e Serviços" />
JS

Trocar logo de acordo com a página


Google visualization and y-axis values

Question: This might be very simple but I have been unable to find any answer.
I have this visualization:

For some reason the API chooses to make the Y-axis as 1.5, 3.0, 4.5, etc. Why, oh why? 1, 2, 3, 4 would be nice. I can format it using
vAxis: {format: '##'}

but that just rounds the labels - they still represent their original number.
Can someone help?
Comment: possible duplicate:
http://stackoverflow.com/questions/5979123/google-charts-visualization-column-chart-axis-formatting-and-column-colors
Answer: Replace your packages with:
'linechart'

will work.
Answer: It's not exact, but you can specify a maxValue for the vAxis, which will change the intervals. So, try it like this:
chart.draw(data, {vAxis: {maxValue: 8}});

You might need to mess around with the maxValue number to make it show correctly.
Good luck!
Answer: Try format: '#' instead of format: '##'

Google visualization and y-axis values


Laravel using alternative path for views

Question: I'am trying to return view from alternative folder

Project Name

themes

admin

views
manage_users.blade.php

I tried
View::addLocation('/themes/admin/views');

View::addNamespace('theme', 'themes/admin/views');

return View::make('theme::manage_users');

But i keep getting error
View [manage_users] not found.
Answer: I fixed my issue by removing the keyword theme so it became return View::make('manage_users');
Comment: Cool, pls mark your answer as the correct answer to close the issue.


English to Hindi translation LSTM

Question: I'm trying to implement English to Hindi translation using Deep Learning LSTM. But when I train the model it shows 'nan' loss in both actual and validation.
Link of text file containing translation pairs-: http://www.manythings.org/anki/
Below is my Jupyter notebook code-:
import string
import re
from numpy import array, argmax, random, take, delete
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense, LSTM, Embedding, Bidirectional, RepeatVector, TimeDistributed
from keras.preprocessing.text import Tokenizer
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.sequence import pad_sequences
from keras.models import load_model
from keras import optimizers
import matplotlib.pyplot as plt

# function to read raw text file
def read_text(filename):
    # open the file
    file = open(filename, mode='rt', encoding='utf-8')
    # read all text
    text = file.read()
    file.close()
    return text

# split a text into sentences
def to_lines(text):
    sents = text.strip().split('\n')
    sents = [i.split('\t') for i in sents]
    return sents

data = read_text("/content/drive/My Drive/Colab Notebooks/Language Translator New/hin.txt")
eng_hin = to_lines(data)
eng_hin = array(eng_hin)

eng_hin = delete(eng_hin, 2, axis=1)

# Remove punctuation
eng_hin[:,0] = [s.translate(str.maketrans('', '', string.punctuation)) for s in eng_hin[:,0]]
eng_hin[:,1] = [s.translate(str.maketrans('', '', string.punctuation)) for s in eng_hin[:,1]]

# convert to lowercase
for i in range(len(eng_hin)):
    eng_hin[i,0] = eng_hin[i,0].lower()
    eng_hin[i,1] = eng_hin[i,1].lower()

# empty lists
eng_l = []
hin_l = []

# populate the lists with sentence lengths
for i in eng_hin[:,0]:
    eng_l.append(len(i.split()))

for i in eng_hin[:,1]:
    hin_l.append(len(i.split()))

print(max(eng_l))
print(max(hin_l))

# function to build a tokenizer
def tokenization(lines):
    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(lines)
    return tokenizer

# prepare english tokenizer
eng_tokenizer = tokenization(eng_hin[:, 0])
eng_vocab_size = len(eng_tokenizer.word_index) + 1

eng_length = 25
print('English Vocabulary Size: %d' % eng_vocab_size)

# prepare Hindi tokenizer
hin_tokenizer = tokenization(eng_hin[:, 1])
hin_vocab_size = len(hin_tokenizer.word_index) + 1

hin_length = 25
print('Hindi Vocabulary Size: %d' % hin_vocab_size)

# encode and pad sequences
def encode_sequences(tokenizer, length, lines):
    # integer encode sequences
    seq = tokenizer.texts_to_sequences(lines)
    # pad sequences with 0 values
    seq = pad_sequences(seq, maxlen=length, padding='post')
    return seq

# Model Building
from sklearn.model_selection import train_test_split
train, test = train_test_split(eng_hin, test_size=0.2, random_state = 12)

# prepare training data
trainX = encode_sequences(eng_tokenizer, eng_length, train[:, 0])
trainY = encode_sequences(hin_tokenizer, hin_length, train[:, 1])

# prepare validation data
testX = encode_sequences(eng_tokenizer, eng_length, test[:, 0])
testY = encode_sequences(hin_tokenizer, hin_length, test[:, 1])

# build NMT model
def build_model(in_vocab, out_vocab, in_timesteps, out_timesteps, units):
    model = Sequential()
    model.add(Embedding(in_vocab, units, input_length=in_timesteps, mask_zero=True))
    model.add(LSTM(units))
    model.add(RepeatVector(out_timesteps))
    model.add(LSTM(units, return_sequences=True))
    model.add(Dense(out_vocab, activation='softmax'))
    return model

model = build_model(hin_vocab_size, eng_vocab_size, hin_length, eng_length, 512)
rms = optimizers.RMSprop(lr=0.001)
model.compile(optimizer=rms, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

filename = '/content/drive/My Drive/Colab Notebooks/Language Translator New/Englis_Hindi_Checkpoints/model.h1.31_dec_19'
checkpoint = ModelCheckpoint(filename, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

history = model.fit(trainX, trainY.reshape(trainY.shape[0], trainY.shape[1], 1),
          epochs=100, batch_size=64,
          validation_split = 0.2,
          callbacks=[checkpoint], verbose=1)
model.save('/content/drive/My Drive/Colab Notebooks/Language Translator New/Englis_Hindi_Checkpoints/eng2hin.h5')

When I try to fit the model, it runs but shows 'nan' in loss. Please help me to resolve my issue.
Comment: Did you double-check that your data is properly encoded. That is given word ids in the encoded sequences, you can recover the original?
Comment: I ran your code with vocab_sizes swapped at the line calling function build_model. This time loss values were showing fine. From this, I doubts that your code was written for eng-hin instead of hin-eng. There is also shape mismatch between the trainY and shape of last dense layer. Correcting these things may solve your problem.
Comment: Thanks @sreagm. I've  solved by swapping vocab_sizes, as earlier I was doing Hindi to English but I want English to Hindi. Now another problem rises of validation loss keeps on increasing but training loss gets decreased. What can I do to reduce validation loss?
Comment: Are you sure, the data is fed as eng-hin not the other way around. Also, when I checked the shape of last dense layer was (None, x, y) but your trainY had shape (x,y,1) . Swapping the vocab_sizes worked because it was written for hin-eng. I think swapping the data might be the right solution. Also double checking the number of samples in the dataset and building the model accordingly.
Answer: In simple words, it usually happens because the loss function / optimizer isn't suiting the network calculations. I recently used this network to create a calculator. Try using loss='categorical_crossentropy' and optimizer='adam' and see if it works.

English to Hindi translation LSTM


Music practice for "by ear" musicians

Question: To those who has played by ear instead of reading music, what were your exercises and practices to improve, especially other genres and improvement of the left hand.
Edit: drop some songs as well that can be used for practice, especially those with a lot of dynamics
Comment: When I found out  the cadence I-IV-V-I for the first time I transposed it in all keys and in all rhythms and times as Waltzes and school songs, accompanied my self to simple songs. Then I found out the I-vi-ii-V progression and did the same. Also the blues pattern and the few songs a knew at that time like O when the saints go marching in, Hang down your head, Tom Dooley, the House of the rising sun etc. Later I started to play Boogie Woogie and Rock'n Roll.
Answer: My practice was:

Sit down at the piano and reproduce elements of songs in terms of (1) chords, (2) melody, (3) rhythm, until it sounded reasonably close to what I wanted.
Improvise changes to the tunes. Chord substitutions, melody improvisation, rhythmic changes. For example, whenever I learned a new variation of a chord that works as a minor chord in a song, say, m7 or m9, I wanted to apply that everywhere. Or that you can play a dim7 chord instead of a dominant seventh - I applied that everywhere. All sorts of tricks. Tritone substitutions, bass inversions, walking bass, comping patterns, modulations, everything had to be applied all over. I had to turn songs upside down and make them something different. The driving motivation was fun and skills.
Play everything in different keys. Every time I learned a new trick, I wanted to play it in different keys, because if I could play something only in one key, I felt that I didn't really know the trick.

And I think that was it. The level of how close I could get to the things I was hearing, got better and better together with my ability to play various tricks. When I learned to play something, it was added to the set of things I could distinguish by ear. Playing by ear can be translated to the question: what would I need to do with instrument X to make it sound like that. You need to babble with all sorts of things.
At some point I started to do the same on the guitar and computer sequencers, which opened my view somewhat. Reproducing something with a sequencer can be a good analytical exercise.
One exercise I did quite mechanically was, play one verse of a song in a key, and for each consecutive verse, do a modulation trick to a different key. Repeat until you've played it in all keys. If the modulation trick moves the key by a whole-step, do a half-step modulation after six keys, so you get all twelve.
Answer: Here's some exercises that have helped me in no particular order:

Try to figure out any random song you come across on youtube. Use the play settings in bottom right corner to adjust playback speed. For example, you can make it twice as slow. Tip: first thing to figure out is what scale it's in. That way you can worry about a lot less notes when you play it back.
Practice scales. Especially do arpeggios of the diatonic chords from the scales this'll help when you do your left hand chords. Also practice modes, I personally like the minor modes: phrygian, dorian, and natural minor (aeolian).
Take any song and modulate it to different keys. For example, in major, this will get you familiar with the sound of chords (esp I IV V vi ii) in all keys.
Take a major song and modulate it to C major. Any song in major you can play in C major, same goes for any minor song you can play in A minor. This enables you to more clearly see what types of chords you're playing with the left hand.
Don't look at your fingers as you play. This'll take a lot of practice but I personally feel that it is detrimental to look at your fingers because then you're not focusing on the sound and muscle memory. Use a blindfold or turn off the lights or just don't look down. This also helps with recognizing scale degrees and diatonic chords by ear. For instance, you'll start recognizing the sound of I IV V chords being played in songs regardless of key.
Improvise on top of songs you hear. You can also do this with backing tracks. Lastly get a looping device (either software or hardware) that way you can improvise with yourself. For example, play a chord progression and when it's looping back improvise a melody on top of it. Try this for different keys.
As far as particular songs go. Just make sure you can play any song that is in your head, that you know by heart. If you can whistle it, then you should know how to play it. And when you play it, don't just play the melody add chords to it. Don't use tabs, sheet music, etc. when you do this. You should figure all that out by ear and from the particular scale you're in.
Comment: @foreyez Hi, I know this is kind of late, but what are the advantages, and uses of practicing, and knowing the different scales/modes?
Comment: @DanielValle scales are probably the single most important thing in music. every song you hear falls under a scale. in one octave you have 12 notes. a scale is just selecting 7 of those notes and those are the notes the song will use. some songs have different scales than others. for example, one song might be in C major, another song will be in B minor. all that means is that one song is played using one set of seven notes, and another song is played using another set of seven notes. stick to the major scale first though as most songs use the major scale. feel free to ask me anything else.
Answer: To learn piano by ear is one methods among others. People who are born blind  in the past could only learn this way.
1. Orientation on the keyboard:
First you have to learn the pattern of the black and white keys and find the octaves. You should do this like you were blind, but of course you can control it with your eyes.
Likes this you will also learn the names of the scales and recognize the triads and chords like major, manor, diminished. Listen carefully to the different sound and the impression they evoke.
2.Chord progressions:
Play the chord progression I-IV-V-I in C major: that means the triads of the degrees C, F, G. Play the Bass notes with the left hand and the triads with the right hand. Now you can already play your "1. symphony op 1" in C. Play the same chords in other sequences like I-V-V-I IV-I-V-I etc. By changing the time and rhythm in 3/4 you can play your first Waltz (Opus 2).
You are now already able to accompany yourself singing some songs or a friend playing  another instrument.
Look up the Blues pattern: you can play it also with these 3 chords:
C,C,C,C
F,F,C,C
G,F,C,C
(last bar will be G to repeat the whole progression)
Play the same progression using tetrads as minor 7 chords with the r. hand:
by adding to the triads the 7th: C7 = C,E,G,Bb  F7 = F,A,C,Eb  G7 = G,B,D,F
Learn the Roman number for this progression Ib7, IVb7, V7
One of the most used patterns is the progression I,vi,ii,V (also with the 7)
 which is C,a7,d7,G7 (note capital letters mean major, a7 = a minor, also written as Am.
Play the progression VI V IV III (or in the parallel minor scale i,VII,VI,v
in C major this will be am, G, F, em
3. Scales and keys - Circle of fifths:
The next you'll have to do is learning the other scales and keys:
Start with G major scale up and F major scale down. You will find out that you always have to add a # or a flat. Play the chords and degrees, the patterns you have learnt in C-major in these other keys.
You will develop yourself the circle of fifths and if not you can look it up.
You'll find out that you can use the V7 of G7 also in your songs you've played in
C like e.g.: C,C,F,F,C,C,C,C,F,F,C,C,D7,D7,G7,G7,
You will do these exercises also playing the inversions of all triads in the right hand, and also playing the whole chord accompaniment with the left hand alone.
First only triads in root position, later the Bass note on the first beat and the upper chord tones on the beats 2,3,4 or 2,3, when playing a Waltz.
4.Arpeggios:
This means you don't play the full chord at once but like picking on a Guitar or Harp
you play each note after the other up and down:
left h.:c,e,g,e or c,g,e,g or l.h.c, r.h. e,g,c,e,g,c,e
In a 6/8 time this will be r.h. e,g,c,g,e etc. together with the root tone in the l.h.
4. Play the songs you know:
Now will be longing to play a song!
(I hope you did long ago - in exercise 1.)
Here are some titles you can start with:
Joyful, joyful (Beethoven Symph. 9)
This old House
O when the saints
Good night ladies
What shall we do with the drunken sailor
Happy birthday
Jingle bells
A groovy kind of love (Phil Collins)
I can feel it in the air tonight
Another day in Paradise
Lady in black
Several songs by Michael Jackson and the Beatles are quite easy to play by chords
and singing the melody.
try out also Rock'n roll, Blues and Boogie (Bass-riffs and melody licks)
Arpeggios:
The house of the rising sun
Silent night
Well - I wish you a merry Christmas and hope you start playing not only by ear!
You will have much more success if you are not a musical illiterate!
To explain and read these exercises above you need only  5 - 10 minutes but to understand them and being able to apply them might need you months and years! My advice is to learn sight reading and my personal recipe is: reading by writing! This means notate everything I've told you here in numbers (1,3,5), Arabic letters (c,e,g), Roman numbers (I-III-IV-V), writing lead- sheets and drawing the keyboard patterns of new chords you're learn  together with the notation in a staff system).
There are lots of good questions here about sight reading and  many helpful answers. Look them up and look up all bold written titles (tags) above.

Music practice for "by ear" musicians


Query and print out random nodes in drupal

Question: How to query random nodes in Drupal 6 without views?
I can print out last nodes but random nodes are the problem.
Answer: Not sure where you are doing this, but this would be one dull ax way of doing it:
$query=db_query('SELECT nid, RAND() as _random FROM {node} WHERE status = 1 ORDER BY _random ASC LIMIT 1');
$result=db_fetch_object($query);

and then you can do whatever you want with $result->nid, another dull ax:
drupal_goto('node/' . $result->nid);

or
$node=node_load($result->nid);
// do whatever you want with it

etc
Comment: The SQL can just be "SELECT nid FROM {node} WHERE status = 1 ORDER BY RAND() LIMIT 1", unless you really need the random number back.


Advantages of NET::SCP over system"scp ..." or exec "scp ..."

Question: I was wondering if there were any advantages about using the gem Net::SCP over using a simple --system 'scp' '-options' "#{source}" "#{remote_host}"--
We have more options on the second method but I feel that people wouldn't have made a NET::SCP gem if there was not a necessity.
So my questions are

Advantages of Net::SCP over a system "command"?
Is there a big security flaw with Ruby system shell command?

Thank you
Answer: Portability, for one. Windows systems don't ship with any SSH tools.
Also, ability to control behavior from code - for example, controlling policy for handling new host fingerprints is (relatively) easy with a library, but a real pain if you're executing an external process.


Analog to digital sampling rate affected by String() function on ESP8266?

Question: I'm using an ESP8266 NodeMCU 12-E development board to capture audio from a pre-amplified electret microphone, then I upload it to the web where it will be converted to a wav file.   My first thought was to cast the integer values of analogRead(A0) on the ESP8266 as String type, then concatenate them into a longer string payload which I can publish to an MQTT broker.
My MQTT client subscribers didn't seem to be getting proper sound files, because all I heard were series of rhythmic pops.
I decided to investigate if my code on the ESP8266 board was even capturing things properly. I stripped the code down to these few lines which seem to cause problems:
#include <ESP8266WiFi.h>

const char *ssid =  "____";  // Change it
const char *pass =  "____";  // Change it

void setup()
{
  Serial.begin(115200);
  Serial.println(0);      //start
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
}

void loop()
{
    int analog = analogRead(A0);

    if (analog > 255) {
      analog = 255;
    }
    else if (analog < 0){
      analog = 0;
    }

    Serial.print(String(analog));
    Serial.print(" ");

}

Here's how I use the code above to produce a wav file to check if the sound is what I expect:
- I start up the ESP8266 development board
- I turn on the Serial Monitor and clear all previous output
- I power up my electret microphone and speak into it
- I power down my electret microphone
- I copy the contents of the Serial Monitor (which is a series of integers) into a text file called `audio.raw`
- I copy `audio.raw` to a linux machine that has ffmpeg installed
- I issue the command `ffmpeg -f u8 -ar 11111 -ac 1 -i audio.raw -y audio.wav` on the linux machine

When I listen to the audio.raw file, I hear my voice, but the speed is maybe 5-10 times faster than normal.  (I also get a lot of noise and distortion, but that might be a separate issue with the input signal quality.)
I then tried changing this one line of code Serial.print(String(analog)) to Serial.print(analog).  Then I repeated the steps above.  But this time, my voice sounds like it is about 2 times faster than normal.
Why does changing this one line from Serial.print(String(analog)) to Serial.print(analog) make such a big difference?
Is it because the String() function is a very expensive operation that takes up a lot of time?  And when the script needs more time to process each line of code, the script then has less time to capture enough analogRead(A0) data points?  And if I run the same ffmpeg command using all the same flags, then ffmpeg will try to meet the -ar 11111 requirement by speeding up the audio play?  Which would imply that my sampling rate is dependent on execution speed of my script?  Which means I have to consider variable execution speeds across other boards of the same model due to variability in manufacturing precision, environmental temperature, etc...?
Answer: Your sampling rate is coupled to your loop implementation (as you have discovered). This will also cause jitter in your sampling rate as different code paths will take different amounts of time and interrupt service routines will also steal CPU cycles.
This jitter will be one of the causes of distortion in your output.

When I listen to the audio.raw file, I hear my voice, but the speed is maybe 5-10 times faster than normal.

The ESP8266 has a hardware UART so the code can potentially load the UART's FIFO buffer faster than it can output. This would be a source of the perceived faster sampling rate but also cause jitter or data loss when the buffer fills up. Depending on the implementation, when the buffer fills it will drop data or alternatively block (causing jitter).

Why does changing this one line from Serial.print(String(analog)) to Serial.print(analog) make such a big difference?
Is it because the String() function is a very expensive operation that takes up a lot of time? And when the script needs more time to process each line of code, the script then has less time to capture enough analogRead(A0) data points?

Yes, yes and yes.
One of the reasons for the performance difference is that String() involves allocating and managing memory on the heap to store the characters.
Serial.print(analog) uses a fixed size buffer on the stack as the code knows the maximum number of characters required to display an int.

And if I run the same ffmpeg command using all the same flags, then ffmpeg will try to meet the -ar 11111 requirement by speeding up the audio play?

Yes. ffmpeg assumes that the samples have a fixed sampling rate but this does not match the samples that are being printed out.

Which would imply that my sampling rate is dependent on execution speed of my script?

Yes!

Which means I have to consider variable execution speeds across other boards of the same model due to variability in manufacturing precision, environmental temperature, etc...?

Yes. There will be a multitude of variables that affect execution speeds.
What can you do?
Decouple the sampling of data from the code execution.
This can be done by implementing an Interrupt Service Routine. Tie the ISR to a hardware timer so it executes at a fixed sampling rate and avoiding jitter.
The ISR can write to a buffer which the code in loop() transmits over the serial connection. The ISR and serial transmission code need to manage the buffer to ensure that neither overrun the other. One means of doing this is to use alternate buffers that the ISR and transmission code use.
Comment: So something like the Ticker.h library is what I can use to asynchronously capture and write analog data to a buffer?  I just read about that library here: https://www.google.com/amp/s/circuits4you.com/2018/01/02/esp8266-timer-ticker-example/amp/
Comment: Ticker.h uses the os_timer API which is still software based and only has precision down to 500 microseconds. You need to use the hardware timers instead. Try https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/ which references the example in https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/Timer/RepeatTimer/RepeatTimer.ino
Answer: Since you use Serial.begin(115200) ESP8266 Microcontroller will transfer 115200 bits per second through serial port. Which is 115200 / 8 = 14400 bytes per second and that means since you use u8 (unsigned 8 bit) format for audio, each sample consists of a single byte. Just change the ffmpeg -ar parameter to 14400.
I don't any have microphones which i can connect to MCU for testing but it should work properly this way. The other -ac parameter is correct since it is mono channel audio.
Edit : Also don't use String() constructor while printing out to Serial.
While using Serial() constructor sound speeds up about 5 times because String converts your 1 byte value to 3 bytes, example ; byte : 255 -> String : "2", "5", "5" , you don't have to consider execution speed of Microcontroller, it will output 115200 bits per second as if you defined. You just need to consider it's output.
Finally delete the line

Serial.print(" ");

Also change

int analog = analogRead(A0);

to

byte analog = (byte)analogRead(A0);

since int consists of 4 bytes, you would not want to send extra 3 bytes to serial.
And after changing int to byte you can get rid this code block
if (analog > 255) {
  analog = 255;
}
else if (analog < 0){
  analog = 0;
}

If you connect ESP8266 to linux device through usb which has ffmpeg on it you can use

ttylog -b 115200 -d /dev/ttyUSB0 | ffmpeg -f u8 -ar 14400 -ac 1 -i - -y audio.wav

to capture audio data in realtime from ESP8266.


What is a "death cap"?

Question: I have often heard/read people mentioning a "death cap" when talking about older Fender amps.
Is this a capacitor?
What does it do?
Why is it called the death cap?
Comment: A "death cap" is an Amanita phalloides, or one of the related species of poisonous basidiomycete fungus, common to North America and Eurasia. This is one of mushrooms responsible for the saying "there are old mushroom hunters, and there are bold mushroom hunters, but there are no old bold mushroom hunters." It should also be pointed out that there's a great webforum, _Badcaps,_ exclusively dedicated to bad capacitors. They helped me fix my washing machine. https://www.badcaps.net/index.php Sorry, I'll delete this presently.
Answer: On older, two prong power cord Fenders, there's a 0.047 uF cap that you can switch in across one of the power leads and ground. It filters out some high frequency noise on the line. If the cap fails it can result in your amp's chasis, and your guitar, being connected directly to the mains. Possibly resulting in death.
Here's a diagram for converting a 2-prong Fender to a 3-prong setup with a fuse, getting rid of the cap in the process.
Comment: In your diagram, the capacitor does not go across the two leads, it goes between one of the leads and the chassis.
Comment: @Anthony good catch. Fixed text.
Answer: All caps should be properly discharged, but that's not why it's the death cap. As said above, when it fails you're really rockin' with fire. In fact, I believe the history of rock n roll is rich with anecdotes of guitarists electrocuting themselves on stage. As a passive consumer of these rock myths, I always felt it added a certain romantic quality to the music. But, nah...a cheap amp from yesteryear can, and has, killed. The Death Cap!
Answer: The above is wrong.. although it adds a ground to an otherwise ungrounded unit. Many old tube radios and amps, the chassie itself is part of the circuit. The "death cap" is a large capacitor in the amps circuit that can store thousands of volts like a battery even when unplugged. Touching the connections to it can result in a very unpleasant shock.  It must be safely discharged before repairs can be made.
Comment: Hi, welcome to music, and thanks for contributing.  Are you saying that @Ian C. is incorrect that a failure of the 0.047 mfd cap would be dangerous?  I agree with his answer that it would be dangerous: If that caps fails "short," then it looks to me like it will put line voltage on the chassis (which is ground in that old equipment).  Or are you just saying that the "death cap" is not that 0.047 mfd cap, but a larger capacitor somewhere else in the circuit?  It is true that large caps must be treated with respect and caution.  Is there one in the old Fender amps?
Comment: Mark - your comment is incorrect. A capacitor in a mains circuit will only store charge based on the voltage that was across it. So no, you will not store thousands of volts. Sure, a big cap can kill - I have used them to demonstrate arc welding - but that's not what is going on here.
Comment: Mark, you are confusing the death capacitor with a normal PSU capacitor. And neither will store thousands of volts. The chassis is part of the circuit of most electronics before double-insulation became a thing.

What is a "death cap"?


AADSTS65005 error on adal.js multi-tenant authentication

Question: I am trying to integrate adal.js into our SPA with multi-tenant enabled. I followed  the instruction here to configured the AAD App.
I enabled MULTI-TENANT to yes and set oauth2AllowImplicitFlow to true.
Here is the code I used to setup the adal.js
<code>adalProvider.init(
            {
                clientId: sessionStorage.getItem(Constant.adalHelper.AadAppId),
                endpoints: angular.fromJson(sessionStorage.getItem(Constant.adalHelper.AdalResourceMap)),
                extraQueryParameter: "nux=1",
                instance: sessionStorage.getItem(Constant.adalHelper.AadInstance),
                popUp: false,
                redirectUri: window.location.origin + "/login",
                requireADLogin: true,
                tenant: sessionStorage.getItem(Constant.adalHelper.AadTenant)
            }, $httpProvider);
</code>
When I set the tenant to the tenant name of AAD app, everything works just fine. However, as soon as I change the tenant to "Common". The authentication failed the error as below:
AADSTS65005:The application 'https://xxxxxxx/xxxxxx' asked for permissions to access a resource that has been removed or is no longer available. Contact the app vendor.
Comment: What is the resource you are trying to access? Is it also an application you created in the "developer tenant"?
Answer: Based on the description you were developing multi-tier applications for the multi-tenant.
There are two scenarios for the multi-tier applications.
One is the multiple tiers in a single tenant(both the client and resource app are on the developer's tenant). In this scenario, we need to add the client to the resource's knowClientApplications.
Second scenario is that multiple tiers in multiple tenants(the client and resource app are in different tenant). In this scenario, the users need to grant the permission separately. First he/she need to grant the permission of resource app and then he/she can using grant the client app.
Based on the error message, please ensure that the resource app is available to make it work for the client app. More detail about the multi-tenant apps scenario, you can refer this link.
Comment: The first scenario is indeed the case. We already set both the resource app and client app to multi-tenant mode and added the client app to resource's knowClientApplications
Comment: I also suggest that you check that whether the client have grant other resources to see the issue was caused by the specific resource which is not available for other tenant. If there is no such resource, you may also need to check the `requiredResourceAccess` in the manifest of client to see the resource is only the one you want to grant.


Showing $(a,b) \sim (c,d)$ iff $a+d=b+c$ is transitive on $\mathbb{N}$

Question: I am given:

$a,b,c,d \in \mathbb{N}$, $a \neq c$, and $b \neq d$. The relation $\sim$ on $\mathbb{N}\times\mathbb{N}$ is defined by $(a, b) \sim (c, d)$ iff $a+d=b+c$ for all $(a,b), (c,d) \in \mathbb{N}\times\mathbb{N}$.

I need to prove transitivity, but can't figure out where to start. I understand transitivity is basically $a=b$, $b=c$, then $a=c$, but with an even pair of elements, I'm not sure where to go from here.
Answer: Let $(a,b),(c,d)$ and $(e,f)$ be such that $(a,b)\sim(c,d)$ and $(c,d)\sim(e,f)$. To show $(a,b)\sim(e,f)$, observe that
$
a+d=b+c
$
is equivalent to $a-b=c-d$. So by assumption you have both $a-b=c-d$ and $c-d=e-f$, which implies $a-b=e-f$. But this means $a+f=b+e$, i.e. $(a,b)\sim(e,f)$.
Answer: In this case it's not $a=b, b=c$ then $a=c$.  That is the statement of transitivity for $=$. But you need to show not that $=$, but that $\sim$ is transitive.  So you need to show that if $(a_L, a_R)\sim (b_L, b_R)$ and $(b_L, b_R) \sim (c_L, c_R)$, then $(a_L, a_R)\sim(c_L, c_R)$.
Using the definition of $\sim$, You can translate the conditions $(a_L, a_R)\sim (b_L, b_R)$ and  and $(b_L, b_R) \sim (c_L, c_R)$ into the more primitive conditions on the numbers $a_L, a_R, b_L, b_R, c_L, c_R$.  Then you can use those primitive conditions and the definition of $\sim$ to show that $(a_L, a_R)\sim(c_L, c_R)$.


Sum of squares proof where $N=n_{UC}+n_{TX}$

Question: $TX$ is variable that indicates treatment status ($TX=1$ if the patient gets the new treatment, and $0$ otherwise, and $UC = 1 - TX$ indicates they got the standard treatment).
Of $N$ patients, $n_{UC}$ get usual care and $n_{TX}$ get new treatment.
If $n_{UC} + n_{TX} = N$, show that $\sum_i(TX_i-\overline{TX} )(TX_i-\overline{TX}) = n_{UC}\cdot n_{TX}/N$.
Comment: Your notation is a bit confusing. (a) What's UC and TX? (and if it doesn't matter what they are, why not use a simpler notation?) (b) Are these values counts or something? (c) Is this for some subject?
Comment: Nick: the notation you used is correct.
Comment: TX is an indicator variable.  Of N patients, nUC get usual care and nTX get new treatment.
Comment: Please check my new edits are correct.
Comment: Can you address part (c) in my comment above? More specifically, this looks like a standard textbook question, and as such it probably should have the `self-study` tag. We treat those question [differently](http://stats.stackexchange.com/tags/self-study/info), providing hints in return for a clear explanation of what you've already tried and where you got stuck. If this is for some subject, or even just an exercise for personal study, you should add the tag.
Comment: How far did you get? Did you try expanding the square in the sum and simplifying that? i.e. did you trying taking $$(TX_i-\overline{TX} )(TX_i-\overline{TX})=$$ and expanding the product out?
Comment: Don't jump so far ahead, that's probably why you're lost. So you can do this much: $\sum_i(TX_i-\overline{TX} )(TX_i-\overline{TX})=\sum_i TX_i^2-2\overline{TX}\sum_i TX_i +N\,\overline{TX}^2 =\sum_i TX_i^2-N\,\overline{TX}^2$? Then what's $TX_i^2$ (remember, it's always 0 or 1 - what happens in each case)? After that, what's $N\,\overline{TX}^2$? Once you simplify both terms, you can make progress
Comment: Is this an equivalent phrasing? "If $nUC + nTX = N$, show that: $\sum(TX_i-\bar{TX})^2 = nUC\times nTX \div N$"
Comment: No prob; your $\LaTeX$-fu is stronger, thus I am learning. :)
Comment: They are. Thanks for your help in posting the question correctly!
Comment: Another way of expressing the opening statement is: Tx indicates treatment status.  For patients receiving new treatment TX = 1, for patients receiving usual care, TX = 0.
Comment: This is a question from a health economics course.  I will add the self-study tag.  Thanks again for your guidance.
Comment: I comfortable with showing the proof for expanding this so that it is equal to sum(Tx)^2 - (sumTx)^2/N.  I am stuck on the nuc*ntx/N relationship.
Comment: As a follow-up, I wanted to let you know that I finished solving the problem.  Thanks for your help!
Comment: I am glad you got it sorted out. I didn't see your comment when you posted it because you didn't @name me (which notifies me). I'll post a complete answer now.
Answer: I'm going to use a more concise notation (my $T$ is your $TX$) but you can convert it.
Let $p$ be the proportion of the patients with $T=1$ for a binary variable $T$, i.e. let $p = T/N$. Then:
\begin{eqnarray}
\sum_i(T_i-\overline{T} )(T_i-\overline{T})&=&\sum_i T_i^2-2\overline{T}\sum_i T_i +N\,\overline{T}^2 \\
&=&\sum_i T_i^2-N\,\overline{T}^2 \\
&=&Np - Np^2 \\
&=& Np\cdot(1-p) \\
&=& n_T \cdot (N-n_T)/N
\end{eqnarray}
After converting back to your notation (and a minor triviality), that's the required result.


If $G/N$ and $H/N$ are isomorphic, does it imply $G$ and $H$ are isomorphic?

Question: If $N$ is a subgroup of both $G$ and $H$, and $G/N \cong H/N$, then is $G \cong H$?  (Or more generally, if $N$ and $M$ are subgroups of $G$ and $H$ respectively, and $N \cong M$ and $G/N \cong H/M$, then is $G \cong H$?)
I suspect the answer is no, but if the answer is yes, is there a natural isomorphism between $G$ and $H$ given an isomorphism $\phi:G/N \rightarrow H/N$?
Also, just making sure, is $(G \times H)/G \cong H$ and $(G \times H)/H \cong G$?
Comment: If $G$ and $H$ are different groups, how can $N$ be a subgroup of each? It must live in one but not the other. Do you mean that $G$ and $H$ and each contained in some common larger group?
Comment: @Potato: Think categorically. $N,G,H$ are groups and $N \to G$ and $N \to H$ are given regular monomorphisms. The question makes sense, even if we don't use the pushout $G \sqcup_N H$.
Comment: @Potato, there is no obstacle for a group to be a subgroup of two different groups, even if the latter are not both contained in a larger group!
Comment: @MartinBrandenburg:  Funny, my interpretation of "think categorically" would definitely consider "N is a subgroup of G" to be different from "N is isomorphic to a subgroup of G."
Answer: Your first/second question can be reformulated as asking whether the isomorphism class of the normal subgroup $N$ of the group $G$, and that of the quotient group $G/N$, together determine the isomorphism class of $G$.
The short answer is no, as shown by @Jim. A fuller answer is that you are actually inquiring about the intricate problem of group extensions.
As to your third question, I recommend a bit of care. If you write
$$
(\mathbf{Z} \times \mathbf{Z}) / \mathbf{Z},
$$
the $\mathbf{Z}$ at the denominator refers to which of the many copies of $\mathbf{Z}$ in $\mathbf{Z} \times \mathbf{Z}$? If you choose for instance
$$
\{ (2x, 0) : x \in \mathbf{Z} \} \cong \mathbf{Z}
$$
as your copy of $\mathbf{Z}$ at the denominator, then the quotient is definitely not isomorphic to $\mathbf{Z}$, but to $\mathbf{Z}/2 \mathbf{Z} \times \mathbf{Z}$.
So you should probably say something like
$$
1 \to H \to G \times H \to G \to 1
$$
is an exact sequence, where $H \to G \times H$ is the map $h \mapsto (1, h)$ and $G \times H \to G$ is $(g, h) \mapsto g$.
Answer: $N = \mathbb Z/2$ is isomorphic to a subgroup of $G = \mathbb Z/2 \times \mathbb Z/2$ and of $H = \mathbb Z/4$.  These groups are not isomorphic but $G/N \simeq \mathbb Z/2 \simeq H/N$.
Answer: Let $G=S_3$, $H=\mathbb Z_6$, $N_1=\langle(1~2~3)\rangle$ and $N_2=\langle[2]\rangle$. We have $$N_1\leq G,~~~N_2\leq\ H,~~~N_1\cong N_2$$ and $G/N_1\cong H/N_2$ but $G\ncong H$.
Comment: I like your counterexample! +1
Comment: How are you writing $N_2$ as a subgroup of $H$?  If you mean there's a copy of $\mathbb{Z}_2$ (or $\mathbb{Z}_3$) with isomorphic quotient in $G$ and in $H$, but that $G$ and $H$ aren't isomorphic, that's correct, but your notation is getting in the way of it...
Comment: @StevenStadnicki: Yes! I mean there is a copy of $N_1$ in $G$ and the same story for $N_2$ and $H$ as exactly you kindly noted. Thanks Steven for your time.
Comment: the problem is that the generator given for $N_2$ makes it hard to see how $N_2$ is a subgroup of $H$ - you've written it as though it were a subgroup of $G(=S_3)$, in permutation notation, but $\mathbb{Z}_6$ isn't a permutation group per se.
Comment: @StevenStadnicki: I see what I made the mistake. I fixed it


Conditional probability for Bayse rule.

Question: So I have the following problem. There are 300 million people and 2 million of them are green. Say there are 10 people who are terrorists. 9 out of 10 of these terrorists are green.
What is the probability that a random selected green person is a terrorist?
My reasoning:
$P(Green|Terrorist)=9/10$
$P(green)=2/300$
$P(notgreen)=298/300$
$P(not green|Terrorist)=1/10$
So I have almost everything, except $P(green| not terrorist)$. I am not sure how to find this... I was thinking that perhaps it is (2 million-9)/(300 million-10) is this correct? My book states otherwise, it claims it is 2/300 ...
I would appreciate any help
Comment: The question is not clear "What is the probability that a random selected green person is a terrorist?".  This suggests that we are picking someone randomly from the green people.  Did you mean  "What is the probability that a random selected person is a green terrorist?"
Comment: @NazimJ No, I am looking for $P(terrorist|green)$
Comment: Edited to clear confusion
Comment: Did you give a try assuming that there are $300$ million innocent people $+$ $10$ terrorists?
Comment: The 10 terrorists must be in the 300 figure. 300 is population not innocent
Answer: \begin{array}{|c|c|c|c|}
\hline
0 & terrorist & no terrorist & total \\ \hline
green & 9& 1.999.991&2.000.000 \\ \hline
nogreen &1&297.999.999& 298.000.000\\ \hline
total &10&299.999.990&300.000.000 \\ \hline
\end{array}
Does this table help?

Conditional probability for Bayse rule.


revive an old QLogic IBA7322 Infiniband card on CentOS8.2

Question: I have a few HP Blades Gen7 equipped with QLogic Infiniband cards IBA7322 which I would like to use with CentOS 8. The problem I have is that I cannot find the right drivers for them. All the information I find is either old, or the links don't work (e.g. the marvell download links).
I can see the card with lspci but other than that I didn't have any luck turning it on (so to say).
Can anyone point me in some sort of direction with this problem?
Cheers.
Edit:
results from lspci -vv:
01:00.0 InfiniBand: QLogic Corp. IBA7322 QDR InfiniBand HCA (rev 02)
        Subsystem: Hewlett-Packard Company Device 178a
        Physical Slot: 0
        Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr+ Stepping- SERR+ FastB2B- DisINTx-
        Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx-
        Interrupt: pin A routed to IRQ 10
        Region 0: Memory at fd400000 (64-bit, non-prefetchable) [size=4M]
        Capabilities: [40] Power Management version 3
                Flags: PMEClk- DSI- D1- D2- AuxCurrent=0mA PME(D0-,D1-,D2-,D3hot-,D3cold-)
                Status: D0 NoSoftRst- PME-Enable- DSel=0 DScale=0 PME-
        Capabilities: [70] Express (v2) Endpoint, MSI 00
                DevCap: MaxPayload 512 bytes, PhantFunc 0, Latency L0s <64ns, L1 <1us
                        ExtTag- AttnBtn- AttnInd- PwrInd- RBE+ FLReset- SlotPowerLimit 0.000W
                DevCtl: Report errors: Correctable+ Non-Fatal+ Fatal+ Unsupported+
                        RlxdOrd+ ExtTag- PhantFunc- AuxPwr- NoSnoop-
                        MaxPayload 128 bytes, MaxReadReq 4096 bytes
                DevSta: CorrErr- UncorrErr- FatalErr- UnsuppReq- AuxPwr- TransPend-
                LnkCap: Port #0, Speed 5GT/s, Width x8, ASPM L0s, Exit Latency L0s <4us
                        ClockPM- Surprise- LLActRep- BwNot- ASPMOptComp-
                LnkCtl: ASPM Disabled; RCB 64 bytes Disabled- CommClk+
                        ExtSynch- ClockPM- AutWidDis- BWInt- AutBWInt-
                LnkSta: Speed 5GT/s, Width x8, TrErr- Train- SlotClk+ DLActive- BWMgmt- ABWMgmt-
                DevCap2: Completion Timeout: Not Supported, TimeoutDis+, LTR-, OBFF Not Supported
                         AtomicOpsCap: 32bit- 64bit- 128bitCAS-
                DevCtl2: Completion Timeout: 50us to 50ms, TimeoutDis-, LTR-, OBFF Disabled
                         AtomicOpsCtl: ReqEn-
                LnkCtl2: Target Link Speed: 5GT/s, EnterCompliance- SpeedDis-
                         Transmit Margin: Normal Operating Range, EnterModifiedCompliance- ComplianceSOS-
                         Compliance De-emphasis: -6dB
                LnkSta2: Current De-emphasis Level: -6dB, EqualizationComplete-, EqualizationPhase1-
                         EqualizationPhase2-, EqualizationPhase3-, LinkEqualizationRequest-
        Capabilities: [b0] MSI-X: Enable- Count=32 Masked-
                Vector table: BAR=0 offset=00008000
                PBA: BAR=0 offset=00009000
        Capabilities: [100 v1] Advanced Error Reporting
                UESta:  DLP- SDES- TLP- FCP- CmpltTO- CmpltAbrt- UnxCmplt- RxOF- MalfTLP- ECRC- UnsupReq- ACSViol-
                UEMsk:  DLP- SDES- TLP- FCP- CmpltTO- CmpltAbrt- UnxCmplt- RxOF- MalfTLP- ECRC- UnsupReq+ ACSViol-
                UESvrt: DLP- SDES+ TLP- FCP- CmpltTO- CmpltAbrt- UnxCmplt- RxOF- MalfTLP- ECRC- UnsupReq- ACSViol-
                CESta:  RxErr- BadTLP- BadDLLP- Rollover- Timeout- NonFatalErr-
                CEMsk:  RxErr- BadTLP- BadDLLP- Rollover- Timeout- NonFatalErr-
                AERCap: First Error Pointer: 00, ECRCGenCap+ ECRCGenEn- ECRCChkCap+ ECRCChkEn-
                        MultHdrRecCap- MultHdrRecEn- TLPPfxPres- HdrLogCap-
                HeaderLog: 00000000 00000000 00000000 00000000

Comment: Did you check to see if the drivers were dropped due to the hardware's age? RHEL often removes drivers for ancient hardware from its latest kernels.
Comment: I didn't see that card on the list... If it is still supported how would i know?
Answer: The first thing to do is to get the PCI vendor and device IDs for the hardware in question. Your card appears to be 1077:7322. A quick look at the Linux Driver Database tells me this card uses the ib_qib driver.
I took a look through the RHEL documentation of removed drivers in RHEL 8, but did not see this driver. However, I fired up a RHEL 8 VM and the driver is no longer present and is not enabled in the corresponding kernel configuration. It is present in RHEL 7 though.
What people usually do in such a circumstance, in order to use the old hardware, is to use the elrepo repository, which for RHEL 8 contains the missing ib_qib driver (and several other drivers that Red Hat dropped). For example:
dnf install https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm
dnf install kmod-ib_qib

Comment: thank you for your help! I did install the driver but I still cannot see the infiniband card as active. How do I activate it after the driver installation?
Comment: this is what I get from `ibstatus`: `Fatal error:  No devices`
Comment: Is the module loaded? Does `lspci -vv` show it being used?
Comment: I am editing my original post to show you what `lspci -vv` says.
Comment: Oh, you have an HP-branded card, not an OEM QLogic card. What's the actual vendor and device ID for your card?
Comment: This is what I get from `lspci -nn`: `01:00.0 InfiniBand [0c06]: QLogic Corp. IBA7322 QDR InfiniBand HCA [1077:7322] (rev 02)`
Comment: Well, that's the right vendor and device ID. So, again, did you load the module?
Comment: i have tried `modprobe ib_qib` but that leads to an error message: # modprobe ib_qib
modprobe: ERROR: could not find module by name='ib_qib'
modprobe: ERROR: could not insert 'ib_qib': Unknown symbol in module, or unknown parameter (see dmesg)
modprobe: ERROR: Error running install command for ib_qib
modprobe: ERROR: could not insert 'ib_qib': Operation not permitted.
Comment: I tried this: `# insmod ib_qib.ko` but got this error:
`insmod: ERROR: could not insert module ib_qib.ko: Unknown symbol in module`. The file is located here: `/usr/lib/modules/4.18.0-193.6.3.el8_2.x86_64/extra/ib_qib/ib_qib.ko`
Comment: Well, that's interesting. I'd have a chat with the elrepo people about that.
Comment: I have found this similar problem but I have no clue how to use the [info](https://elrepo.org/bugs/bug_view_page.php?bug_id=1015&history=1). How do I get in touch with them?
Comment: Probably the same way they did.
Comment: Ok I will do that! Thank you for your help!
Answer: after many many trials my friend threw a hail Mary and recompiled the 4.19 kernel on Rocky 8.5 and enabled the qib driver. It was there all along it was just not compiled in the default kernel.
Now we got the old QLogic card working again. It is connected to a (gasp!) Mellanox Infiniband Switch that is running the Subnet Manager.
The nodes are able to communicate at 40 Gbps between them and they are also able to communicate with the much newer 100Gbps Mellanox ConnectX5 cards too.
All is good in infiniband-land now!
Hope this helps someone and if there are any questions I'll do my best to answer them. Just keep in mind that I am in no way an expert in these things. Just a guy that had to figure something out and make an old equipment relevant again!


How to prevent same site forgery?

Question: I have a page which will fire an ajax request to the server to increment a value after user shared a post:
FB.ui({
   method: 'feed',
   name: 'Share',
   link: 'http://{{ request.META.HTTP_HOST }}{% url "facebook_competition" %}',
   }, function(response){
      if (response && response.post_id) {
         $.post('http://mysite.net/shared/', {post_id:response.post_id});
      }
});

My site is protected by CSRF tokens, as below:
jQuery(document).ajaxSend(function(event, xhr, settings) {
    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
        var csrftoken = $('meta[name="csrf-token"]').attr('content');
        //var csrftoken = $.cookie('csrftoken');
        xhr.setRequestHeader("X-CSRFToken", csrftoken);
    }
})

but how do I effectively prevent the user loading the page then firing ajax requests in console, such as:
for (var i=0; i<100; i++){
    $.post('http://mysite.net/shared/', {post_id:1234});
}

I have considered IP, frequency checking etc etc, but they are very ineffective.
Comment: For clarity: can you add how the etc. methods are "ineffective"? From personal experience I know there is not a lot you can do about someone stealing your data; if it's on the web and anyone can read it, they can steal it. (And "it's on the web so it must be free" is their usual defense...)
Answer: There probably isn't a lot you can do to prevent it entirely, but you can combine the methods you've tried to make it more difficult. Ultimately you can consider generating some unique, one-time-use, token with each refresh that should be posted together with the post_id of the sharing action, so that your users would have to reload your page to update your counter a second time.
Also, (perhaps obviously) if your users are not anonymous, you can ignore their counter-increments of they have already caused one.

How to prevent same site forgery?


Compute probability of a grouping being correct

Question: I have an exemplar grouping of objects (each with their own feature vector) into categories. I am then given a new grouping of compeltely different objects, and Iw would like to compute the probability of this grouping being 'correct'.
I realise this is a tricky problem, so I have tried to break it down a little.
My plan so far is to:

For the training grouping, compute the mean variance of each feature within each cluster.
Next, re-assign the training groups into random clusters, and again compute the mean variance of each feature.
This should allow me to compute the probability of an unseen clustering being a true clustering, by comparing the variance of each feature with those in the 'true' and 'random' training examples

Does this sound like a sensible method? I am a little wary of the concept of taking means of variances.
Comment: You say you want to compute "the probability of this grouping being correct". By "correct" do you mean that it will naturally be separated from the rest of the data in an unsupervised sense (i.e. it will be a cluster)?
Comment: Yes that's correct. I suppose there are two elements to this problem - clusters being entities within themselves, and also clusters being well-separated from each other. I suppose I could treat each of these problems separately.
Comment: Actually I think well-separated is the only criterion. "Being entities" means you are assuming that they are supposed to have some type of distribution (e.g. Gaussian), if I understand correctly. Do you really expect clusters to have a specific distribution?
Comment: I suppose it depends on the assumptions made about the underlying data. I agree on the separation though. I think I really need a test for each, so that a split/merge operation can be evaluated for the change in overall likelihood.
Comment: It might go on forever, you need to have optimization schema
Answer: First, I should say that evaluating whether data is "really" clustered is in general impossible because there are many definitions of what "good" clustering is and they usually make some assumptions about the distribution of each cluster, which is something that you don't have information about (otherwise you would have fit these specific distributions). Also, it doesn't make sense getting a probability as output unless you are assuming something about the distributions.
That being said, there are several such measures of "good" clustering, you can see some here.
Regarding you method, the way see it is like this: You have in mind some statistics for each cluster, and you want to see how those statistics change if you add your new data. If they change "significantly", you say that you have a correct grouping.
I think this approach is ok, just realize its limitations:

The choice of statistics is everything and will change your results
drastically. Make sure you understand what your statistic captures.
By using a certain statistic you are assuming something about the
distribution of your clusters.
Imagine that only 10% of your data is really "separated". The statistics might change significantly but is your data truly a new group? How about if only one object in it is separated? What if it contains two groups that are very distant from each others?
Specifically regarding the variance, I think that is not a good statistic. I you meant the mean AND variance, this would be similar to doing a T-test for each feature. That is reasonable but it then means you are assuming a Gaussian distribution of each cluster. You might also consider statistics that are robust to outliers such as the median, to avoid extreme outlier effects as I proposed in (3). Think of it as if you are doing a statistical test for each feature - what do you want your test to be (e.g. parametric/non-parametric)?
If you are trying to determine significance by looking at the p-values of all tests, don't forget to do multiple hypothesis correction.

Good luck!
Answer: There is no true measurement of clustering quality.
The problem is that by the very nature of the clustering task, you use clustering to find something new on your data. You do not use it to minimize a mathematical measure such as variance (which btw. heavily depends on the data set and normalization, and is sensitive to e.g. the number of clusters).
I see the strong risk in your approach that you might not measure whether it is a true clustering or not, but whether it is a variance-optimized clustering. So you might be able to say whether this was generated by k-means or not, but you might consider a non-k-means-clustering to be invalid.
You really need to be careful to not overfit your method to particular clustering concepts and algorithms (and in particular, not to k-means).


Rate of eigenvalue decay for Erdős–Rényi random graph

Question: How fast do eigenvalues decay in a large Bernoulli  random  graph for a fixed large $n$ (number of nodes) and fixed $p$ (probability of dropping an edge)? Also known as Erdős–Rényi random graph. Empirically, if we sort eigenvalue in decreasing order, their magnitudes seem to decay exponentially with $i$=position in the sorted list. Can someone point me to a reference or a way to justify this?

This graph plots log of absolute value of eigenvalues of a large Erdős–Rényi random graph, arranged in decreasing order.
ListLogPlot@Rest@Reverse@Sort@Abs@Eigenvalues@N@AdjacencyMatrix@RandomGraph@BernoulliGraphDistribution[2000, 0.5]
Edit eigenvalues follow the shape of semicircle CDF

notebook
Comment: "sometimes called binomial random graph" ...and much more commonly called an [Erdős–Rényi random graph](https://en.wikipedia.org/wiki/Erd%C5%91s%E2%80%93R%C3%A9nyi_model).
Comment: For constant average degree? Or for which dependence on $n$?
Comment: @md5 I'm interested in the rate of decay of $i$th largest eigenvalue as $i$ increases, for given $n$ and average degree $p$. An exponential or super-exponential upper bound would be pretty useful. The shape in the graph looks pretty similar for a range of $n$ and $p$
Comment: Are you sure your graph doesn't show a result for $n=2000$?
Comment: Usually $p$ denotes the probability to select an edge, so I guess you mean that $p=d/n$, where $d$ is a constant independent of $n$? One could also look at different regimes of $p$, and the rate of decay of the eigenvalues might be different
Comment: @md5 in my case, d and n are some fixed constants, so the decay is in terms of `i` rather than `n`
Comment: The range of eigenvalues you're interested it is a bit ambiguous to me. I was also further thrown off off by not being well versed with Mathemtica, and assuming the x-axis in your graph was the eigenvalue magnitude -- could you clarify the axis meanings?
Comment: @jwimberley I sort absolute values of eigenvalues in decreasing order. This gives me a list of rapidly decreasing numbers, can you tell me how fast they decrease?
Answer: As $n \to \infty$ (with constant $p$) the eigenvalues of $G_{n,p}$ are essentially described by Wigner's semicircle law.
For this purpose, let $(Y_n)_{n=1}^\infty$ be a sequence of random $n \times n$ symmetric matrices, where

all off-diagonal entries of all matrices follow the same distribution with mean $0$ and variance $1$;
all diagonal entries of all matrices follow the same distribution with mean $0$ and finite variance.

Let $X_n = Y_n/\sqrt n$. As $n \to \infty$, the eigenvalues of $X_n$ converge to a distribution with PDF $f(t) = \frac1{2\pi}\sqrt{4-t^2}$, in the sense that the number of eigenvalues of $X_n$ in the range $[a,b] \subseteq [-2,2]$ converges to $\displaystyle\int_a^b f(t)\,dt$.

For $G_{n,p}$, we almost satisfy the hypotheses of the theorem, except that the distribution of the off-diagonal entries is Bernoulli, which does not have mean $0$. This only affects the leading eigenvalue, which is $\approx np$; the other eigenvalues follow a semicircle law.
Here is an example with $n=2000$ and $p = \frac12$, where I plot a histogram of the actual eigenvalues against the theoretical semicircle (centered at $(0,0)$ with radius $\sqrt{2000}$, then normalized to integrate to $2000$). You don't see the leading eigenvalue of $\approx 1000$ here.

Mathematica code:
Show[
   Histogram[Eigenvalues@N@AdjacencyMatrix@
      RandomGraph@BernoulliGraphDistribution[2000, 0.5], {1}],
   Plot[Sqrt[2000 - x^2]/(1000 Pi)*2000, {x, -45, 45}]]

Comment: the tricky thing is to connect semi-circle law to the graph I'm seeing
Comment: The graph you're seeing doesn't have a nice formula, because you have to go from the PDF to the CDF and then invert it.
Comment: ok, I'll tinker with it some more. Basically I'm trying to figure out how fast the eigenvalues in my graph decay. CDF of the semi-circle does not resemble the graph I'm seeing, it's symmetric around y=x line, but graph of eigenvalue magnitude vs position in the list is not
Comment: GraphicsColumn[{Plot[
   CDF[WignerSemicircleDistribution[1], x], {x, -1, 1}],
  ListPlot@Rest@
    Reverse@Sort@
      Abs@Eigenvalues@
        N@AdjacencyMatrix@
          RandomGraph@BernoulliGraphDistribution[2000, 0.5]
  }]
Comment: When you plot the magnitude of the $i^{\text{th}}$ largest eigenvalue, you are looking at the inverse CDF rather than the CDF. In other words, the $250^{\text{th}}$ eigenvalue out of $2000$ will have magnitude about $a$ if $\int_{|x|>a} f(x)\,dx = \frac{250}{2000}$.
Comment: Ah, I was missing the absolute value on |x|. Indeed this seems like the same distribution
Answer: Edit: This answer currently does not address the underlying question, because it mistook the graph for a histogram and and was answering the unasked question about the distribution of large eigenvalues -- e.g. $p(\lambda)$ for large $\lambda$, and pointing out that in fact $p(\lambda) = 0$ for large enough $\lambda$, much narrower of course than an exponential distribution.
It's a basic fact that the maximum eigenvalue of an adjacency matrix is bounded above by the maximum degree of any vertex in the graph -- are you sure your plot does not show the distribution for $n=2000$? You can find a proof of this on Wikipedia. Here's a simple proof of the weaker property that the eigenvalue's magnitudes are bounded by the number of vertices in the graph. The sum of squared eigenvalues $\sum_i \left| \lambda_i \right|^2$ is the trace of the square of the adjacency matrix $A^2$. For one, this implies a hard upper bound on the magnitude of any eigenvalue. Since the elements of the (symmetric) adjacency matrix are either 0 or 1, an element $(A^2)_{ii}$ on the diagonal of the squared adjacency matrix is
$$
(A^2)_{ii} = \sum_{j} A_{ij} A_{ji} = \sum_{j} A_{ij}^2 = \sum_{j} A_{ij} = d(i).
$$
Since the degree of the $i$-th vertex is absolutely bounded by $n-1$, where $n$ is the number of vertices in the graph, the upper bound on any eigenvalue's magnitude is
$$
\left| \lambda_i \right|^2 \leq \sum_{i} \left| \lambda_i^2 \right| = \mathrm{tr} \left( A^2 \right) = \sum_{i} (A^2)_{ii}\leq n(n-1).
$$
When the edges to the graph are added randomly, the degrees of separate vertices are of course correlated. Assuming as an approximation they are independent, then each vertice's degree is $p(n-1)$ on average and
$$
E \left[ \sum_{i} \left| \lambda_i^2 \right| \right] \approx pn(n-1).
$$
Comment: I dropped the largest eigenvalue from the plot, it seems to be an outlier, probably easier to model it separately from the rest of the spectrum which seems to decay exponentially


Como adicionar a uma propriedade de um objeto usando drop-down list no ASP.NET MVC?

Question: Estou tentando adicionar um valor a propriedade Quantidade do objeto Moeda, selecionando dentro de uma lista de objetos atraves de uma drop down list.
Essa é minha model class:
public class Moeda
{
        [Key]
        public int Id { get; set; }

        [Display(Name = "Tipo de Moeda")]
        public string Tipo { get; set; }
        public int Quantidade { get; set; }

        [DisplayFormat(DataFormatString = "R$ {0:0.00}")]
        public float Valor { get; set; }
}

E estes são meus controllers methods:
public IActionResult Add()
{
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Add(String tipo, int quantidade)
{
    var moeda = await _context.Moeda
                              .FirstOrDefaultAsync(m => m.Tipo == tipo);

    moeda.Quantidade += quantidade;

    _context.Update(moeda);

    await _context.SaveChangesAsync();

    return RedirectToAction(nameof(Index));
}

E essa a View:
@model MaquinaDeTrocoMVC.Models.Moeda
<!DOCTYPE html>

The select element

    <form action="/moedas/add">
        Escolha uma moeda
        <select name="tipo" id="tipo">
            <option value="1 Centavo">1 Centavo</option>
            <option value="5 Centavos">5 Centavos</option>
            <option value="10 Centavos">10 Centavos</option>
            <option value="25 Centavos">25 Centavos</option>
            <option value="50 Centavos">50 Centavos</option>
            <option value="1 Real">1 Real</option>
        </select>

            <input asp-for="Quantidade" class="form-control" />

        <input type="submit" value="Submit">
    </form>

Quando clico no botão submit, ele altera a url para conter o Tipo e a Quantidade vinda da View, porem não executa o Post (Não volta para o Index nem atualiza o banco).
O que estou fazendo de errado?
Comment: Por favor, edite a pergunta para limitá-la a um problema específico com detalhes suficientes para identificar uma resposta adequada.
Answer: Quando você define na sua Action um atributo do tipo ValidateAntiForgeryToken torna obrigatório que seja passado no seu Form um valor para ele.
Acrescente o código @Html.AntiForgeryToken() logo após definir o seu Form
<form action="/moedas/add">
    @Html.AntiForgeryToken()
</form>

Esse código irá gerar um input do tipo Hidden, contendo um Token que sua aplicação gerou para essa requisição. Assim evitando que o envio de dados para o servidor seja falsificado.
Exemplo de Input gerado.
<input name="__RequestVerificationToken" type="hidden"
value="valor do token gerado pela aplicação" />

Comment: Obrigado pela ajuda, não tinha me atentado a isso, funcionou!


SQLSTATE[HY000]: General error: 1005 Ca n't create table `auth`.`order_items`

Question: SQLSTATE[HY000]: General error: 1005 Ca n't create table auth.order_items
    <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('description');
            $table->integer('category_id')->unsigned();
            $table->string('image')->default('default.png');
            $table->double('purchase_price', 8, 2);
            $table->double('sale_price', 8, 2);
            $table->integer('stock');
            $table->timestamps();
            // $table->userstamps();
            // $table->softUserstamps();

           $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->softDeletes();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
             $table->bigIncrements('id');

            $table->string('order_number');
            $table->unsignedBigInteger('user_id');
            $table->enum('status', ['pending','processing','completed','decline'])->default('pending');
            $table->float('grand_total');
            $table->integer('item_count');
            $table->boolean('is_paid')->default(false);
            $table->enum('payment_method', ['cash_on_delivery', 'paypal','stripe','card'])->default('cash_on_delivery');

            $table->string('shipping_fullname');
            $table->string('shipping_address');
            $table->string('shipping_city');
            $table->string('shipping_state');
            $table->string('shipping_zipcode');
            $table->string('shipping_phone');
            $table->string('notes')->nullable();

            $table->string('billing_fullname');
            $table->string('billing_address');
            $table->string('billing_city');
            $table->string('billing_state');
            $table->string('billing_zipcode');
            $table->string('billing_phone');

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
}

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOrderItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
       Schema::create('order_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('order_id');
            $table->unsignedBigInteger('product_id');

            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');

            $table->float('price');
            $table->integer('quantity');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('order_items');
    }
}

Comment: Is there more to the error message?
Comment: `categories.id` is probably not an unsigned int.
Answer: You are creating foreign key on product_id in your order_items table using  $table->unsignedBigInteger('product_id') but  it is definend integer in your products table $table->increments('id'); that is why you are getting this error.
To create a foreign key child column data type must be same as of parent column datatype
To resolve this error change unsignedBigInteger to unsignedInteger


Should I use api.mydomain.com or mydomain.com/api/ for my API?

Question: Im developing a web-based application, using Angular2 as front-end and Django 1.10 as back-end, and Nginx as web-servers interface.
Should Angular2 access the Django API through api.mydomain.com or mydomain.com/api/?
I have seen examples of both, and I want to know the pros and cons.
Answer: I generally prefer using api.example.com instead of a URL path. Doing this allows you to de-couple your web application infrastructure from your API infrastructure. Perhaps they're running on the same server now, but by using a sub-domain, you can easily separate them in the future without requiring major code changes.