Create a new user in LDAP, not wherever Drupal normally creates it

Question: We have an already existing LDAP of users, that we wish to use for our new site. Amazingly, the login seems to be only finding the users we want it to, and not the users we don't. We have to figure out why, because we'll probably want to change this in the future. (I mention, in case it's related to my main issue.)
However, the more important problem is that, when we create a new user, we want it to be created in the LDAP, not wherever Drupal creates it. As I understand, when it authenticates against LDAP, it simply copies the user to it's own database, and uses that from then on, which is also not what we want, in case the user changes their data. However, we'd still like the admin user to work, even if the LDAP dies or something. The documentation isn't much help with this.
So, how do I make Drupal (I'm using 7, but I don't know if it's 7 specific) use the LDAP for the creation of new users, and for existing LDAP users? (Hopefully, without totally removing the ability to use the users already in the Drupal db.)
Answer: You will never be able to use Drupal's core user module without using the database's user table (and many others), without seriously modifying core Drupal.
It sounds like the only reason you want Drupal to not use it's own user + roles + permissions management (and all of the other contributed Drupal modules that depend on the user module) is so that if a user updates some data in LDAP it's also updated in Drupal. If this is the case, find a method for synchronizing your LDAP user info with Drupal's user info.
You can use the hook_user_ functions in Drupal to perform whatever actions you want to happen when a user account is created, updated, etc., in Drupal. For example, if a new user account is created in the Drupal user registration form you'd be performing whatever logic you need (via PHP, or potentially something else) to insert a new record into your LDAP server.
Comment: Make sure to check out the [LDAP module](http://drupal.org/project/ldap), it may have solved some of your problems already!
Comment: Ah. So I should be catching changes and programatically copying and translating them to the LDAP, instead. That sounds like it will do what I want. I'll leave this question open for a bit, though, just in case. It's only been open 30 mins. :)
Comment: @marcvangend: It only has options for when there's a user conflict between the LDAP and the db. I haven't found any option for how to create LDAP users.
Answer: To configure LDAP with Drupal 7, you'll have to install and configure LDAP module (including ldap_authentication and ldap_user). Once enabled, you'll find the configuration at /admin/config/people/ldap.
First of all, you'll have to add and configure your 'LDAP Server Configuration' depending on your LDAP configuration (Servers tab at /admin/config/people/ldap/servers).
To configure LDAP Authentication in Drupal, choose the LDAP Server Configuration in Authentication tab at /admin/config/people/ldap/authentication.
At the end, to create LDAP entries on Drupal user creation, turn on 'Create or Synch to LDAP entry when a Drupal account is created or updated.' option and the other which you think are necessary and configure Provisioning from Drupal to LDAP Mappings according to your LDAP configuration (User tab at /admin/config/people/ldap/user).
See the configuration below:

Here is example configuration exported into code using Features:
<?php
/**
 * Implements hook_default_ldap_servers().
 */
function foo_ldap_default_ldap_servers() {
  $export = array();
$ldap_servers_conf = new stdClass();
$ldap_servers_conf->disabled = FALSE; /* Edit this to true to make a default ldap_servers_conf disabled initially */
$ldap_servers_conf->api_version = 1;
$ldap_servers_conf->sid = 'ldap_pv';
$ldap_servers_conf->name = 'LDAP: Local Provisioning';
$ldap_servers_conf->status = TRUE;
$ldap_servers_conf->ldap_type = 'openldap';
$ldap_servers_conf->address = 'example.com';
$ldap_servers_conf->port = 389;
$ldap_servers_conf->tls = TRUE;
$ldap_servers_conf->followrefs = FALSE;
$ldap_servers_conf->bind_method = 1;
$ldap_servers_conf->binddn = 'cn=drupal-test,ou=Services,dc=example,dc=com';
$ldap_servers_conf->bindpw = '';
$ldap_servers_conf->basedn = array(
  0 => 'ou=People,dc=example,dc=com',
  1 => 'ou=External,ou=Access,ou=Group,dc=example,dc=com',
);
$ldap_servers_conf->user_attr = 'uid';
$ldap_servers_conf->account_name_attr = '';
$ldap_servers_conf->password_attr = 'userPassword';
$ldap_servers_conf->password_attr_encoding = 'clear';
$ldap_servers_conf->mail_attr = 'mail';
$ldap_servers_conf->mail_template = '';
$ldap_servers_conf->picture_attr = '';
$ldap_servers_conf->unique_persistent_attr = '';
$ldap_servers_conf->unique_persistent_attr_binary = FALSE;
$ldap_servers_conf->user_dn_expression = '';
$ldap_servers_conf->ldap_to_drupal_user = '';
$ldap_servers_conf->testing_drupal_username = 'admin';
$ldap_servers_conf->testing_drupal_user_dn = '';
$ldap_servers_conf->grp_unused = FALSE;
$ldap_servers_conf->grp_object_cat = 'groupofnames';
$ldap_servers_conf->grp_nested = FALSE;
$ldap_servers_conf->grp_user_memb_attr_exists = FALSE;
$ldap_servers_conf->grp_user_memb_attr = '';
$ldap_servers_conf->grp_memb_attr = 'member';
$ldap_servers_conf->grp_memb_attr_match_user_attr = 'dn';
$ldap_servers_conf->grp_derive_from_dn = FALSE;
$ldap_servers_conf->grp_derive_from_dn_attr = '';
$ldap_servers_conf->grp_test_grp_dn = 'ou=External,ou=Access,ou=Group,dc=example,dc=com';
$ldap_servers_conf->grp_test_grp_dn_writeable = '';
$ldap_servers_conf->search_pagination = FALSE;
$ldap_servers_conf->search_page_size = 1000;
$ldap_servers_conf->weight = 0;
$export['ldap_pv'] = $ldap_servers_conf;
  return $export;
}

/**
 * Implements hook_default_ldap_authorization().
 */
function foo_ldap_default_ldap_authorization() {
  $export = array();
  $consumer_type = new stdClass();
  $consumer_type->disabled = FALSE; /* Edit this to true to make a default consumer_type disabled initially */
  $consumer_type->api_version = 1;
  $consumer_type->sid = 'ldap_pv';
  $consumer_type->consumer_type = 'drupal_role';
  $consumer_type->consumer_module = 'ldap_authorization_drupal_role';
  $consumer_type->status = TRUE;
  $consumer_type->only_ldap_authenticated = TRUE;
  $consumer_type->use_first_attr_as_groupid = FALSE;
  $consumer_type->mappings = 'a:1:{i:0;a:6:{s:12:"user_entered";s:18:"OV4 Pro Repository";s:4:"from";s:70:"cn=example-group-pro,ou=External,ou=Access,ou=Group,dc=example,dc=com";s:10:"normalized";s:18:"OV4 Pro Repository";s:10:"simplified";s:18:"OV4 Pro Repository";s:5:"valid";b:0;s:13:"error_message";s:106:"Role OV4 Pro Repository_name does not exist and role creation is not enabled.";}}';
  $consumer_type->use_filter = TRUE;
  $consumer_type->synch_to_ldap = FALSE;
  $consumer_type->synch_on_logon = TRUE;
  $consumer_type->revoke_ldap_provisioned = TRUE;
  $consumer_type->create_consumers = TRUE;
  $consumer_type->regrant_ldap_provisioned = TRUE;
  $export['drupal_role'] = $consumer_type;
  return $export;
}
?>

Read more:

https://drupal.org/project/ldap
https://drupal.org/node/997082
https://drupal.org/node/1920682


VueJS re-read file input when selecting the same file

Question: What i am trying to achieve is to select a file and read its contents in a new dialog box.
I have it working until the point i try and select the same file as the previous loaded one, selecting a different file works perfectly.
I have tried clearing the file input which appears to work but it still doesn't select the file again.
See my example below, any help much appreciated!
Codepen

new Vue({
  el: "#app",
  data: () => ({
    importedData: null,
    dialogImport: false
  }),
  watch: {
    importedData: {
      handler() {
        this.checkData();
      },
      deep: true
    }
  },
  methods: {
    openFileExplorer() {
      this.$refs.fileInputImport.value = "";
      this.$refs.fileInputImport.click();
    },
    selectFile(e) {
      const self = this;
      const file = e.target.files[0];
      let reader = new FileReader();
      reader.onload = e => {
        self.importedData = reader.result;
        self.checkedData = true;
      };
      reader.readAsText(file);
    },
    checkData() {
      // check the file
      this.dialogImport = true;
    }
  }
});
<link href="https://email@example.com/dist/vuetify.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons" rel="stylesheet" />

  <v-app id="inspire">

      <input ref="fileInputImport" type="file" name="fileInputImport" @change="selectFile">

      <v-btn color="primary" @click.stop="openFileExplorer()">Choose file</v-btn>

      <v-dialog v-model="dialogImport" fullscreen transition="dialog-bottom-transition" :overlay="false" scrollable>
        <v-container class="ma-0 pa-0 white" style="max-width:100%">
          <v-layout row wrap justify-left>

            <v-card style="width:100%;">
              <v-toolbar class="amber lighten-1 elevation-0">
                <v-toolbar-title>Imported data</v-toolbar-title>
                <v-spacer></v-spacer>

                  <v-flex xs12>
                    <v-container fluid>
                      <v-layout row align-center justify-center>

                        <v-flex>
                          <v-tooltip bottom close-delay="0">
                            <v-btn icon slot="activator" @click.native="dialogImport = false">
                              <v-icon>close</v-icon>
                            </v-btn>
                            Close
                          </v-tooltip>
                        </v-flex>

                      </v-layout>
                    </v-container>
                  </v-flex>

              </v-toolbar>

{{ importedData }}

            </v-card>
          </v-layout>
        </v-container>
      </v-dialog>

  </v-app>

Answer: I don't think watch is required. Here is the updated codepen
It was able to read the text and assigns it to importedData. But the importedData has the same content when you select same file, watch is not tried the this.checkData().
Either you reset importedData when your close the dialog or remove the watch as i did.


No Solution Set for augmented coefficient matrix $[A\mid b]$.

Question: Let there be $Ax=b$ a set of m equations with n unknowns.
If $\operatorname{rank}(A) \neq \operatorname{rank}[A|b] $ the system of equations does not have a solution
Does it mean that: there is a pivot number in b $\iff$ there is no solution?
Answer: Yes, it means that the row-reduced matrix of $A$ has at least one row of all zeros, for which the corresponding row in coefficient matrix $[A\mid b]$ does not: i.e., after row-reduction of $[A\mid b]$, there exists at least one row with all zeros except for a non-zero entry in the right-most column ($b$).
Such a system of equations is inconsistent and so no solution exists.
Answer: Yes. More precisely, if you encode $A x = b$ as an adjoined matrix $[A|b]$ and row-reduce, there is a solution iff the last nonzero row is not $[0 \dots 0 | 1]$, that is, if there is no pivot of $[A|b]$ in the last column. This is easy to see: If the last row is not $[0 \dots 0 | 1]$ there are nonzero entries left of the last column, so that the last row, and hence the whole system, admits a solution. If that is the last row, the original system is equivalent to one that contains the equation $0 = 1$, and so it has no solution.


Integral inequality with sines

Question: I am trying to show that there exists some constant $\mathcal{C}>0$ such that: $$\mathcal{C}\leq \int_0^1 |\sin (2\pi n x)-\sin (2\pi m x)|\;dx$$
For all distinct $m,n\in\mathbb{N}$. The constant appears to be a little above $4/5$. I have the feeling I am lacking knowledge of a certain integral inequality though. Maybe knowledge of the constant would shed light on how to proceed. Any ideas would be appreciated.
Comment: Do you want a constant or it is enough to you to prove that it exists?
Answer: An Idea: If you put $$A=\frac{1}{2}|\sin(2\pi nx)-\sin(2\pi mx)|$$ then you have $0\leq A\leq 1$. Hence $A\geq A^2$, and you can integrate $\displaystyle \int_0^1 A^2dx$. (I have not done the computations)

Integral inequality with sines


Permission to edit a node by multiple users

Question: Suppose there is one node (Like node/4) which the Admin can edit. My requirement is to give permission to multiple users to edit the same node i.e. node/4. Please help how to accomplish this task.
Answer: The Nodeaccess module might also help:

Nodeaccess is a Drupal access control module which provides view, edit and delete access to nodes. Users with the 'grant node permissions' permission will have a grant tab on node pages which allows them to grant access to that node by user or role. Administrators can set default access controls per content type, and also define which roles are available to grant permissions to on the node grants tab.
Comment: Thank you for your reply. This is the module that i really looking for. Again thank you a lot.
Answer: The best option is to install a node access module that allow the admin to:

Create access control lists (ACLs) to individual nodes; and
Add and remove individual users from an ACL.

The module named Flexi Access lets you do exactly that.

The Flexi Access module will provide a simple and flexible interface to the ACL (Access Control List) module. It will let you set up and mange ACLs naming individual users that are allowed access to a particular node.
The module let you manage access to individual nodes. The administrator may grant individual users the right to: View (read), update (edit/write), and delete the node.

It depends on the ACL module.  (It actually provides an administrative interface to ACL.)
Disclosure: I am one of the maintainers of Flexi Access.
Comment: Thank you for reply. I will try this and let you know if encounters any problem.


Simulacrum: macro implementation not found

Question: I've got a simple code in Scala to try simulacrum lib:
import simulacrum._
@typeclass trait Semigroup[A] {
  @op("|+|") def append(x: A, y: A): A
}

But this doesn't work. Compiler says

Error:(3, 2) macro implementation not found: macroTransform (the most
  common reason for that is that you cannot use macro implementations in
  the same compilation run that defines them) @typeclass trait
  Semigroup[A] {

What can cause this error?
I do not create a macro, I just reuse an existing one.
My build.sbt file is simple:
name := "Macr"
version := "0.1"
scalaVersion := "2.12.5"
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
libraryDependencies += "com.github.mpilquist" %% "simulacrum" % "0.12.0"

Comment: http://www.scala-lang.org/news/2.12.5
Comment: Yes, this is the reason. Thanks! Changing Scala back to 2.12.4 or JDK to 1.8 works
Answer: As noted by Oleg Pyzhcov in the comments, macros don't work with Scala 2.12.4 and 2.12.5 when compiling on Java 9 or 10. However, this has been fixed in Scala 2.12.6, so upgrading should solve the problem.


Converter números escritos com separador de milhar para valor numérico no R

Question: Tenho o seguinte número que estou importando de um site, coloquei da maneira a seguir para não precisar colocar todo o código de importação dos dados
a <- as.character("353.636.000.000")

Gostaria de transformar isso em um número, porém não estou conseguindo
Tentei fazer da seguinte maneira, separar os termos pelo . e depois unir novamente e transformar em número, porém, não deu certo
temp2 <- strsplit(temp2, ".")

Preciso que resulte em um objeto a com valor de 353636000000
Answer: Com stringr você pode fazer isso:
library(tidyverse)

b <- a %>%
  str_replace_all(pattern = '[.]', '') %>%
  as.numeric()

class(b)
[1] "numeric"

Comment: excelente, muito obrigado
Answer: Também é possível resolver o problema com R base:
a <- as.character("353.636.000.000")
as.numeric(gsub("\\.", "", a))
## [1] 3.53636e+11

A função gsub é equivalente a um comando do tipo Localizar e substituir. Sua sintaxe permite que

procuremos uma string (no caso, ., mas temos que colocar os caracteres \\ para evitar que o R entenda isso como uma expressão regular)
substituamos a string procurada por nada (neste exemplo, "" é o indicativo disso)
em um determinado objeto, que no exemplo é o caracter a
Comment: muito bom, obrigado


Kingfisher and swift 3 - cannot set image with url

Question: Checking the documentation and the migration guide, I should be able to set a new image using this code:
imageView.kf.setImage(with:url ...)

but actually I cannot find this method in the library, I only see:
imageView.kf.setImage(with:Resource... )

I don't know exactly how this resource shoud work though since I cannot find anything in the documentation.
Answer: Resource is a protocol. URL has been extended to conform to this protocol. So you can do:
let url = URL(string: ...)!
imageView.kf.setImage(with: url)

If you want some control over what Kingfisher uses for the key in its cache, you can use ImageResource:
let identifier = "..."
let url = URL(string: "http://example.com/images/identifier=\(identifier)")!
let resource = ImageResource(downloadURL: url, cacheKey: identifier)

imageView.kf.setImage(with: resource)

Answer: For Swift 4.2
import Kingfisher

extension UIImageView {
    func setImage(with urlString: String){
        guard let url = URL.init(string: urlString) else {
            return
        }
        let resource = ImageResource(downloadURL: url, cacheKey: urlString)
        var kf = self.kf
        kf.indicatorType = .activity
        self.kf.setImage(with: resource)
    }
}

How to use
self.imgVw.setImage(with: your image url)

Answer: I fixed that issue using this:
PhotoHelper.shared.imagePickedBlock = { [weak self] (image, url) in
      self?.imageView.kf.setImage(with: url, placeholder: image, options: nil, progressBlock: nil, completionHandler: { imageResult, error, type, cache in
        self?.imageView.image = image
      })
    }

PhotoHelper is wrapper on native Image Picker:
    func imagePickerController(_ picker: UIImagePickerController,     didFinishPickingMediaWithInfo info: [String : Any]) {
          currentVC.dismiss(animated: true, completion: {
            if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
              let url = info[UIImagePickerControllerReferenceURL] as! URL
              self.imagePickedBlock?(image, url)
             }
          })
      }


JGit: BundleWriter Error when including Refs

Question: I am trying to create a git bundle of a repository using JGit's BundleWriter. I am attempting to include all Refs in my bundle like so:
Repository repo; // valid repo from elsewhere
BundleWriter bundleWriter = new BundleWriter(repo);

Map<String, Ref> refMap = repo.getAllRefs();
for (Ref ref:refMap.values()) {
    bundleWriter.include(ref);
}

JGit throws an exception for the symbolic HEAD ref, which refers to the default branch (refs/head/master)
java.lang.IllegalArgumentException: Invalid ref name: HEAD
at org.eclipse.jgit.transport.BundleWriter.include(BundleWriter.java:132)

If I exclude the HEAD symbolic ref the bundle is created fine but, when the bundle is cloned from, the missing HEAD reference causes the following git error:
warning: remote HEAD refers to nonexistent ref, unable to checkout.

I can manually checkout master to get things back to normal but it's not ideal behaviour as the bundle is being sent to other people who will expect it to work like any other git repository and include the HEAD reference pointing to the default branch.
Can anyone offer any advice or workarounds so that the default branch is set when the bundle is cloned?
Thanks
Comment: To me this looks like a bug in JGit, I'd open a [bugzilla](https://bugs.eclipse.org/bugs/enter_bug.cgi?product=JGit) or ask on the [mailing list](https://dev.eclipse.org/mailman/listinfo/jgit-dev). In the meanwhile, to work around the issue, I see no other way than to adopt the code and remove the ref-validity-check. `BundleWriter` seems to be relatively small and self-contained.
Comment: @RüdigerHerrmann thank you for the advice. I think I will patch BundleWriter locally to work around the issue and report it to the project.
Answer: The bug has been reported to JGit in bug 446813 (by the OP I assume). It has already been fixed and will be released in the next JGit version (3.6).
Answer: Note: the origin of the error is now explained in Git 2.11 fix:
See commit eb39879, commit 63b747c (09 Sep 2016) by Jonathan Tan (jhowtan).
Helped-by: Jonathan Nieder (artagnon).
See commit 55e4f93 (09 Sep 2016) by Jonathan Nieder (artagnon).
Helped-by: Jonathan Nieder (artagnon).
(Merged by Junio C Hamano -- gitster -- in commit 07d8724, 21 Sep 2016)

connect: advertized capability is not a ref

since [JGit] v220.127.116.11309270735-rc1~22 (Advertise capabilities
with no refs in upload service., 2013-08-08, commit ae1f469), JGit's ref advertisement includes a ref named capabilities^{} to advertise its capabilities on, while git's ref advertisement is empty in this case.
This allows the client to learn about the server's capabilities and is needed, for example, for fetch-by-sha1 to work when no refs are advertised.

Here, this affects also git bundle.
As mentioned above, it is fixed on JGit side.

when the bundle is cloned from, the missing HEAD reference causes the following git error:
warning: remote HEAD refers to nonexistent ref, unable to checkout.

That is what is now (Git 2.11, Q4 2016) different:

Git advertises the same capabilities^{} ref in its ref advertisement for push but since it never did so for fetch, the client didn't need to handle this case.
Handle it.

The code to parse capability list for v0 on-wire protocol fell into an infinite loop when a capability appears multiple times, which has been corrected with Git 2.41 (Q2 2023).
See commit 7ce4c8f, commit c471623, commit d6747ad, commit 20272ee, commit 13e67aa, commit e6c4309, commit aa962fe (14 Apr 2023) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 80d268f, 25 Apr 2023)

v0 protocol: fix sha1/sha256 confusion for capabilities^{}
Signed-off-by: Jeff King

Commit eb39879 ("connect: advertized capability is not a ref", 2016-09-09, Git v2.11.0-rc0 -- merge listed in batch #4) added support for an upload-pack server responding with:
0000000000000000000000000000000000000000        capabilities^{}

followed by a NUL and the actual capabilities.
We correctly parse the oid using the packet_reader's hash_algo field, but then we compare it to null_oid(), which will instead use our current repo's default algorithm.
If we're defaulting to sha256 locally but the other side is sha1, they won't match and we'll fail to parse the line (and thus die()).
This can cause a test failure when the suite is run with GIT_TEST_DEFAULT_HASH=sha256, and we even do so regularly via the linux-sha256 CI job.
But since the test requires JGit to run, it's usually just skipped, and nobody noticed the problem.
The reason the original patch used JGit is that Git itself does not ever produce such a line via upload-pack; the feature was added to fix a real-world problem when interacting with JGit.
That was good for verifying that the incompatibility was fixed, but it's not a good regression test:

hardly anybody runs it, because you have to have jgit installed; hence this bug going unnoticed
we're depending on jgit's behavior for the test to do anything useful.
In particular, this behavior is only relevant to the v0 protocol, but these days we ask for the v2 protocol by default.
So for modern jgit, this is probably testing nothing.
it's complicated and slow.
We had to do some fifo trickery to handle races, and this one test makes up 40% of the runtime of the total script.

Instead, let's just hard-code the response that's of interest to us.
That will test exactly what we want for every run, and reveals the bug when run in sha256 mode.
And of course we'll fix the actual bug by using the correct hash_algo struct.


Menu items in Orchard (duplicate items not shown)

Question: I have a site in orchard where the same page should appear multiple time in navigation tree (menu).
I have created several items pointing to the same Content Item and named in the same way.
Now only the first one is actually shown.
To display the second and the third I have to rename it in a different way.
It seems that if I have two menu items that display the same text just the first item is shown.
Any other has the same problem?
Have you found a workaround?
Thanks
Answer: This is this bug: https://orchard.codeplex.com/workitem/19205, fixed last December. Upgrade.
Comment: Thanks... I'm just curious about this: "We still want to remove duplicates under a given parent, but not in the menu as a whole."
Why do you want this? If I put 2 menus with same text on same menu it is my fault or my reason... why did you need to "correct" this in code?


Why does the export of a styled pandas dataframe to Excel not work?

Question: I would like to apply the same background color to cells that have for each PEOPLE instance the name and the related name. I have tried to df.style.applymap, it does not return an error but it does not seem to work.  Anyone has any ideas why? Thank you.
   clrs = list(mcolors.CSS4_COLORS.keys())
   for k in range(len(PEOPLE)):
        if PEOPLE[k].attribute == 'child':
            df1_data = [PEOPLE[k].name, PEOPLE[k].related]

            df.style.applymap([lambda x: 'background-color: yellow' if x in df1_data else 'background-color: red'])

    df.to_excel('styledz.xlsx', engine='openpyxl')

Comment: applymap accepts a function, not a list
Comment: also `df.style` is the object, not `df`, so you need to use `df.style.to_excel`. And also you need not create multiple `df.style` instances. Basically there is a lot wrong with your code
Comment: Thank you for your answer. If i cannot put df.style in a loop, how can i color multiple cells with different colors?
Comment: have you tried reading the user guide for Styling at pandas documentation. it has many examples.
Comment: @ChristinaJ If you find my answer helpful, I'd appreciate it if you accept it or upvote it!
Answer: Here is some more info on df.style. Here I'm using some simple example because I don't have your data available:
import pandas as pd
import numpy as np

df = pd.DataFrame({'a': np.random.randint(0, 10, 10), 'b': np.random.randint(0, 10, 10), 'late': np.random.choice([0, 1], 10).astype(np.bool)})

def highlight_late(s):
    return ['background-color: red' if s['late'] else 'background-color: green' for s_ in s]
df = df.style.apply(highlight_late, axis=1)
df.to_excel('style.xlsx', engine='openpyxl')

Looks in the excel file like this:

For cell based coloring use:
def highlight_late(s):
    return ['background-color: red' if s_ else 'background-color: green' for s_ in s]

df = df.style.apply(highlight_late, subset=["late"], axis=1)

This gives you:
Comment: Thank you for your answer. I would like to know how to  color only certain cells with different colors not a whole axis.
Comment: @ChristinaJ I updated the answer to only use one cell for coloring
Comment: Thank u! One last comment, what if i want to color cell a[5] and b[2]. I mean what if they are not in the same column? Lets say a[5] and b[2] have the same string written to it.
Comment: `apply([...], axis=1)` works on the whole row, you can simply check in the function `highlight_late` for a keyword and skip the `subset=["late"]` in the `apply`
Answer: Basically your solution will be a modification of the following:
df = DataFrame([['mark', 2], ['mike', 4], ['manny', 6]], columns=['name', 'attribute'])
def style_row(row, people):
    output = Series("", index=row.index)
    if row["name"] in people:
        output['attribute'] = "background-color:red;"
    return output
styler = df.style.apply(style_row, axis=1, people=['mark', 'manny'])
styler


A correlation heatmap with only negative values?

Question: Putting aside the fact that this are, on the whole, fairly weak correlations, does this correlation heatmap of products used by various users, which has ONLY negative values, seem strange to anyone?
I'm new to this type of analysis but something about this seems odd: when a raise in the value of everything correlates to a lowering in the value of EVERYTHING else. I guess it could be that a use of one means NOT using another but that seems a little "zero sum".
Am I overthinking this?
Comment: A standard term for M. Hunter's "pairwise scatterplot" is [scatterplot matrix](https://stats.stackexchange.com/search?q=scatterplot+matrix).  Although all-negative correlation matrices (off diagonal) are unusual, this one at least is mathematically possible.  In fact, it is a minor perturbation of the examples given recently at https://stats.stackexchange.com/questions/631550/: one eigenvalue is nearly zero while the others are nearly identical.  This gives us (considerable) insight into what might be going on: namely, it looks like your variables sum to a value that is nearly constant.
Comment: Pairwise scatterplots are usually the best way to understand otherwise baffling correlational results.
Comment: Cheers for the suggestion, @MikeHunter . Would a Pairwise scatterplot be primarily a pairplot or a scatterplot? I'm using seaborn and out-of-the-box neither seem to give me what I need, but I imagine I could get there with some digging.
Comment: Are you conditioning on anything for this plot?
Answer: "Putting aside the fact that ... weak correlations" seems wrong.  What this looks like is a collection of random deviations from 0 (except maybe for item 2).
And you say that these are "products used by various users" -- well, if the items serve similar purposes, then using one might well lead to less use of another. It would help if you told us what the products were.
Finally, if use of a variable is "yes/no" then I'm not sure a correlation is the best way to capture the relationship. It might be, but .. why did you choose that measure?
Comment: I warmly recommend performing some analysis before answering questions.  See my comment to the question for what that can achieve.
Comment: The products are web services, so this data indicates when a user has used that service in the period in question. There's no reason why someone couldn't use all of the products.

I'm not married to the idea of using correlation. In fact, if there's something that explains the relationship between these things - if there is one - better, I'm happy to use that.
Comment: There are a whole bunch of ways of looking at the association in a 2x2 table. Odds ratios, risk ratios, $\phi$, Cramer's V, and more.


Big O notation in practice

Question: if $P_{n}$ is the $n$'th prime I have that for a constant $a$ $$n\geq \frac{aP_{n}}{\log(P_{n})}$$
and it implies that $$\log(P_{n})=O(\log(n))$$
I'm unsure of how this is implied and where the $\log(n)$ has come from.
Comment: A marker might dock you a mark for forgetting to mention that $a>0.$
Answer: Simply take the natural log of both sides of the inequality to get
$$\log(n)\geq \log\left(\frac{a P_n}{\log{P_n}}\right)=\log(a)+\log(P_n)-\log(\log(P_n))$$
Since $\log(\log(P_n))$ grows much slower than $\log(P_n)$, for large enough $n$
$$\log(a)+\log(P_n)-\log(\log(P_n))>\frac{1}{2}\log(P_n)$$
We conclude there exists a constant $c$ such that
$$\log(n)>c\log(P_n)$$
Thus
$$\log(P_n)=O(\log(n))$$
Comment: +1/ You should say there exists $c>0$ such that $\log (n)>c\log (P_n)$ for all sufficiently large $n.$
Comment: My edit was for a typo  $P_N$ in the 2nd-last line.
Answer: From your inequality and the monotonicity of the logarithm,
$$ \log n \geq \log(P_n) + \log(a) - \log \log(P_n) \text{.}  $$
Once $P_n$ is large enough that $\log \log P_n > 0$, the right-hand side is $ > \log(P_n) + \log(a)$.  Now you have
$$ \log n \geq \log(P_n) + \log(a)  \text{,}  $$
which is sufficient to show that $\log n$ is greater than or equal to a constant multiple of $\log(P_n)$ for all $n$.  (For instance any constant $\geq 2$ works once $P_n > a$.)

Another method.  After taking logs, factor out the (given or recognized) leading term.
$$  \log n \geq \log(P_n) \left( 1 + \frac{\log{a}}{\log P_n} - \frac{\log\log P_n}{\log P_n} \right)  \text{.}  $$
Set
$$ c(x) =  1 + \frac{\log{a}}{\log x} - \frac{\log\log x}{\log x}  \text{.}  $$
We can check that we have the correct leading order term factored out.  Set $L = | \lim_{x \rightarrow \infty} c(x)|$.  If $L = 0$, the factored term grows too quickly.  If $L$ is finite, the correct leading term has been factored out.  If $L$ diverges to $\infty$, the factored term grows too slowly.  If $L$ is not a positive, finite number, go back and factor out the correct leading term.  Here we don't need the absolute values (because everything is positive for large $n$) and we find \begin{align*}
\lim_{x \rightarrow \infty} c(x) &=  \lim_{x \rightarrow \infty} \left(1 + \frac{\log{a}}{\log x} - \frac{\log\log x}{\log x} \right)   \\
  &= 1 + 0 - 0  \\
  &= 1  \text{.}
\end{align*}
So $\log P_n$ is the correct leading term.  Now two ways to finish:

For any $\varepsilon > 0$, there is an $n_0$ such that for all $n > n_0$, $c(n) < 1 + \varepsilon$ (directly from the definition of limit), so $\log n \geq (1+\varepsilon)\log P_n$ for $n > n_0$, showing $\log P_n \in O(\log n)$.
$\max_{x \in [1,\infty)} c(x) = 1-\frac{1}{a \mathrm{e}}$ (assuming natural logs, although a different base of logarithm just changes the details of this constant, not that it is a constant).  So $\log n \geq (1-\frac{1}{a \mathrm{e}})\log P_n$ for all $n$, showing $\log P_n \in O(\log n)$.

In the one case, we show that there is a tail for any constant $> 1$, which is what is in the definition of Big-O.  In the second, we are lucky enough that there is a universal constant that works for all $n \geq 1$.

Finally, a third way.  After taking logs, notice
$$  \log n - \log a \geq \log P_n - \log \log P_n \text{.}  $$
Also, \begin{align*}
\log n &\in O(\log n)  \text{,}  \\
\log a &\in O(\log n) \text{, and }  \\
\log \log P_n &\in O(\log P_n)  \text{.}
\end{align*}
Therefore,
\begin{align*}
\log P_n - \log \log P_n &\in O(\log P_n)  \\
&\subseteq O(\log n + \log a)  \\
&\subseteq O(\log n) + O(\log n)  \\
&= O(\log n)  \text{.}
\end{align*}
This says there are $n_1$ and $n_2$ as well as $c_1$ and $c_2$ such that for all $n > \max\{n_1, n_2\}$,
$$  c_1 \log n \geq c_2 \log P_n  \text{.}  $$
But this says
$$  \log n \geq \frac{c_2}{c_1} \log P_n  $$
for $n$ sufficiently large, so $\log P_n \in O(\log n)$.

Big O notation in practice


I am another plateau victim

Question: I could use some advise from an advanced musician (I am a guitar player, who could have guessed). I have reached a plateau, I am trying to write music, I will occasionally noodle around and hear something I like, write it down, move on.. It is not getting me anywhere currently, I don't know if it is a creativity stump, or if I need more music theory knowledge.
I practice my scales daily, but am having a hard time putting that to use. If this question is not direct enough I can revise, but I hope this makes sense.
Comment: Go to see local bands playing out and talk to people. Compliment them on their gear and/or their music if you like it. Be chill, just meet people. Go see your favorite local bands over an over. Get in the scene. Tell people that you're looking to form or join a band. Sooner or later, someone will ask you to jam with them or ask to jam with you.
Comment: Have you tried forming or joining a band?
Comment: I really want to, and i gave that up like an idiot when i was younger to pursue a career that i have come to find out, I don't care about it. I am 29 now, and not sure how to meet people to collaborate with. Craigslist? Facebook groups?
Comment: What's your actual aim (or aims), specifically?
Answer: Players plateau when they keep doing the same thing... the same outcome happens.
So you need to make a change. Probably any big change will work, but let's go with the three that most musicians swear by (me included):

Start or find a band - it doesn't matter how good, or whether you expect to gig live or just play in a garage; just playing with others forces you to learn and change
Listen and play along to new stuff - buy some music in genres you wouldn't normally touch, or by bands you haven't heard of, and play along. You'll have to pick up new ideas and techniques.
Get a tutor - developing you is what a good instructor will do. Technique, style, theory - all should be kickstarted!
Answer: What else is in your practice routine, besides scales? Try practicing modes, exotic scales, intervals, and simple etudes. You should also have a piece of music to be working on.
Comment: I should have included modes in my comment. I find my self starting a piece, and abandoning, then starting another. Is this perhaps a mental thing? Of coarse your advice is great, I will practice a wider range of things. I practice other peoples songs, general exercises for warming up, etc. I am not really interested in being able to sweep, or play a scale super fast, i'm more interested in writing punky dirty rock music, that's the kind of stuff i play. Someone similarly to how I play would be Mario Lalli from Fatso Jetson. I could just be thinking about it too hard though... thoughts?
Comment: Everyone has a different approach to songwriting, but the key is always to focus and follow through - very much a mental thing. As suggested, having another person to bounce ideas back and forth could definitely help.  Try recording yourself playing, and then listening to it.  When you hear riffs you like, make note of them. When you have three riffs, try stringing them together into a song. Try different structures like verse/bridge/chorus (or ABABC... if you prefer thinking that way.)
Answer: I find this part interesting

I will occasionally noodle around and hear something I like, write it
  down

Am I correct in thinking that you play stuff - perhaps not randomly, but not with any structure - and wait until what you play sounds (in some way) right, and then you write it down?
Just thinking about applying the same process to writing a book:  throw some words onto the page, then read them back and keep them if you like what they say.  In the context of a book it doesn't feel to me as though it would work.  I would expect to think about what I wanted to say and then write it down.  Probably re-read and amend it a bit later.
So I'm wondering if perhaps you should try the same thing with the music.  Think of a musical idea first, then play it, and then write it down.  Don't know if it would help but it might be interesting to try.
Good luck.
Comment: I have been applying this more in the past couple weeks. I am seeing a pattern now, and I need more time allocated in my week to music. It's too hard to make progress when not enough time is dedicated, and I fully know this!
Answer: Try not practicing scales or doing anything relating to music theory (for a short while).
As far as creativity goes, it can (after some time of application) begin to make rigid that which is intended to be pliable.
It can also help to change your musical location; Dev Townsend uses C Major tuning, and that's something you could try to experiment with that will take you far enough out of your comfort zone (I'm making some assumptions here...) that you might be able to get some creativity going.
Answer: Some thoughts ...

Practicing scales: Don't practice! Just play. Play what you like,
what you want. Most importantly: play because you want to, not to get better. getting better will happen automatically :-)
Inspiration: You could try listening to something utterly different
to your 'norm' like if you enjoy metal, try classical or folk.
More fun: Find a jam night. Facebook is great for this, or google
more generally. It depends on the area you live in, but if you can
find a pub / bar which does a regular jam, go along. The jams
local to me work by getting 4 musicians (2 x guitar, bass, drums) on the stage fo 3 songs at a time. They may never have met. They just 'come up with something' :
sometimes a song, sometimes just literally a jam. This is a great way
of pushing your boundaries without it feeling like work/a chore.
Comment: @Mike Yep, fair enough :-) I'd say just go for it, but it's easy for me to sit here and bulldoze your confidence. You have to enjoy it so if it doens't feel right yet, then fair enough. Maybe anothe rthign you could try is playing a piece of music from your musical memory, by which I mean: Think of a song or piece (ideally fairly simple) and just see if you can 'find' it on your guitar without listening to the original. You'll probably get a few bits wrong as we all do, but the point is you're going about finding a key and a chord structure all from your own self, without studying up.
Comment: That might help with you being able to find keys etc when joining in with other people. Or it could lead to you inventing something that might be inspired by the oriognal song but you've taken your owwn way.. "that chord doesn't sound right .. but I quite like it .."

I am another plateau victim


Target population for power analysis of ML model A/B test

Question: We are working on an ML model that predicts a numeric result (call it $\hat{x}$). Eventually, we will perform an A/B test, where the metric is a function that takes $\hat{x}$ as an input (call it $f(\hat{x})$). Our control group will get the input from the old source (call it $x$, so its metric is calculated as $f(x)$), and the variant group will get the input as an output from the model (thus its metric is calculated as $f(\hat{x})$). We will then test to see whether the variant group metric's mean is higher by a specified effect size than that of the control group.
We want to perform the experiment's power analysis right away, even before the model is finished, so that we understand the experiment and know whether the required sample size is feasible and thus whether the improvement promised by the model is testable in this way. We have a reasonable effect size in mind (call it $\bar{x}$), as well as the standard $\alpha$ (0.05) and power (80%).
We can calculate $f(x)$ on past data, and it is from that data that we hope to shape the power analysis. One thing I am realizing is that the choice of past population from which to get the standard deviation $s$ with which to calculate the power analysis input (Cohen's $d = \bar{x}/s$) can make an enormous impact on what sample size is required for the future experiment. The sample size output is extremely sensitive to that $s$, in our case. So my question is, what is a good population from which to get the $s$? Some of us feel we should use the initial training data for the model. Others feel that constitutes some sort of leakage and we should use the initial test data. It may be the answer varies, depending on other factors. If the train / test choice is not clear cut, what are some guidelines to keep in mind?
Comment: Please can you clarify: 1) Whether the ML model in your treatment and the old source in control take the same or a disjoint input dataset to produce $\hat{x}$ and $x$ respectively? 2) Whether the train/test data refers to the input to the ML model, and why there is a significantly different sample standard deviation between these two sets of data? In general though, for experiments with multiple layers input/output that has its own level of variability, it often helps to perform power calculations using some simulations rather than relying on formulas.
Comment: $s$ here must be the standard deviation of your metric, which is a function of the completed model, yes? $s$ will depend on your model, so the only hope of getting a sample size before the model is finished is to decide to bound the out-of-sample sd of your metric.
Comment: 1) Disparate input sets; 2) Train/test refers to the ML input. The significant std dev differentiations are in the past populations we've been analyzing for the power analysis. This std dev is itself highly variant... one month's worth of data can have a very different std dev from another's.
Comment: Yes, you are correct about $s$. But it can be calculated on any past data, the model is just predicting the input for calculating it. The question is which past data to use: training, test, or something else.
Answer: Here is a related thread on power and prediction.  The short answer is you should use/borrow data from a source that is representative of your target.  If you are not sure which source is representative then perform several sensitivity analyses (each time borrowing from a different source) to see how the results vary, and give more importance to the more pessimistic results.
I also recommend you take into consideration the estimand you are targeting.  There is a recent ICH E9 addendum on estimands in clinical drug development.  This is important even if your work is not in clinical drug development.  Here is a related thread on this topic.  The idea is that there will be post-baseline events like treatment switching or discontinuation called "intercurrent events" that can introduce confounding.  How these intercurrent events are addressed define the estimand you are estimating, and can impact the conclusions drawn from the analysis.  Are you imagining a world where such intercurrent events would not occur and censoring endpoint observations in your data set?  Are you considering a world where such intercurrent events would occur and incorporating  these events into the treatment definition?  If you are incorporating estimation and inference from earlier studies, make sure their estimand definition and methods for handling missing data align with your planned approach.
If I am understanding your question and comments correctly the estimated standard deviation varies from one sample to the next.  This could be due to natural sampling variability, it could be sampling bias, or you are simply sampling from different populations.  You could investigate this with confidence intervals and p-values.
Here is a paper on transfer learning for inference on a population quantity such as the mean.  It discusses the idea that there may be multiple sources to borrow from and it may not be clear which is representative of the target.  Here is a paper for predicting a future experimental outcome.  Combining the ideas of these two papers should address your situation.  Let me know if you need more details and I can edit my response.
Comment: Yes, one option is to assume the test data are representative of the target population.  Another option is to assume the test data are not representative of the target population.    I added two more links to papers on transfer learning and prediction.
Comment: Re censoring: luckily, all samples have a definitive outcome. In the modeled business process, inaction after 30 days marks both the sample as expired. We have to wait for samples lasting beyond that period to complete, so we don't perform the final hypothesis test until all collected samples have completed one way or another.
Agreed re sampling from a "source representative of [the] target", and the potential causes you cite for the variability in such sources. I will perform the investigations you suggest. By "representative", would the test data used for scoring the model be appropriate?


Module for Integrating jQuery UI Multiselect Widget

Question: I am using the jQuery multiselect widget on another non-Drupal project and I would love to be able to use it with CCK in Drupal 7. I have found this module for CCK multiselect but it is not exactly what I want. I realize I could try to write something, but I was curious if anyone has used the above with Drupal before or if there is a module out there I'm missing.
Answer: Not quite what you are requesting but thinking that chosen might be a good alternative to what your searching for.
Comment: I think this is the best solution in most cases
Answer: Here you are: jQuery UI MultiSelect Widget
Comment: awesome. this didn't exist when I started this thread
Answer: Since this is Drupal 7 isn't it easier just adding the .js files to your theme's js folder and adding this in your info file?
scripts[] = path/to/js/jquery.multiselect.min.js

Answer: Add the jquery.multiselect.js and jquery.multiselect.css files to your theme folder, such as /sites/all/themes/your-theme/js/jquery-multiselect/
Then add this to your template.php
drupal_add_js(path_to_theme().'/js/jquery-multiselect/jquery.multiselect.min.js');
drupal_add_css(path_to_theme().'/js/jquery-multiselect/jquery.multiselect.css');
drupal_add_js('$(document).ready(function(){$("#example").multiselect();});', array('type' => 'inline', 'scope' => 'footer', 'weight' => 6));

And to make it only show up on certain nodes types, you can use a preprocess node hook in your template.php:
function your_theme_preprocess_node(&$variables) {
  if ($node->type == 'my_content_type') {
    drupal_add_js(path_to_theme().'/js/jquery-multiselect/jquery.multiselect.min.js');
    drupal_add_css(path_to_theme().'/js/jquery-multiselect/jquery.multiselect.css');
    drupal_add_js('$(document).ready(function(){$("#example").multiselect();});', array('type' => 'inline', 'scope' => 'footer', 'weight' => 6));
  }
}


How to build not executable jar (library) with Spring Boot Gradle Plugin 2.1.*

Question: I'm trying to build a library. I have Spring Boot Gradle Plugin in my build.gradle file:
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
}

My library without the main class. To prevent 'Main class not configured ...' error I add to build.gradle:
bootRepackage {
enabled = false
}

but got an error:

A problem occurred evaluating root project 'mysuperlibrary'.

Could not find method bootRepackage() for arguments [build_3886uiniuyuorta9lpa9n4f5c$_run_closure3@1975ec48] on root
    project 'mysuperlibrary' of type org.gradle.api.Project.

All works fine with Spring boot Gradle Plugin 1.5.16 but don't works with 2.1.3.
UPDATE: This plugin I use because without it I can`t resolve testCompile 'org.springframework.boot:spring-boot-starter-test' . When I delete the plugin I can build project bit dependency for test disappears.
Comment: You can use this line in *build.gradle*: `apply plugin: 'java-library'`. Spring Boot Plugin automatically will not create start up class for output jar. Also, plugin will still control dependencies from this sub-project.
Answer: Now I'm using spring plugin with 2.1.* version in all my libs. It is necessary to add next before 'dependencies' section:
bootJar {
    enabled = false
}

jar {
    enabled = true
}

Answer: Update:
Just remove the plugin and add the testing dependencies directly. E.g. with JUnit 5:
dependencies {
      testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.1'
      ....
      testImplementation(
            "org.junit.jupiter:junit-jupiter-api:$jUnitVersion",
            "org.junit.jupiter:junit-jupiter-params:$jUnitVersion"
      )
      testRuntimeOnly(
              "org.junit.jupiter:junit-jupiter-engine:$jUnitVersion"
      )
      test {
          useJUnitPlatform()
      }
}

Old (suited for multi modul project):
You can use the apply flag:
plugins {
   id 'org.springframework.boot' version '2.1.3.RELEASE' apply: false
}

and write a function
def isAbstract(project) {
    return ['mysuperlibrary'].contains(project.name)
}

subprojects { project ->
    if (isAbstract(project) {
        return
    }

    apply plugin: 'org.springframework.boot'

}

Comment: I solved my problem when explicitly specified test dependency: testCompile 'org.springframework.boot:spring-boot-starter-test:2.1.7.RELEASE' . I removed the plugin also. Your answer was not exactly what I wanted, but was very useful. Thanks.
Comment: I think the second code doesn't have sense, because I use it in my "mysuperlibrary". Yes, I build the project, but I can do the same by deleting the plugin. I didn't say that this plugin I use because without it I can`t resolve testCompile 'org.springframework.boot:spring-boot-starter-test' . When I delete the plugin, dependency for test disappears.
Comment: Ah sorry, I thought you are having a multi module project. Then just add the dependencies you need directly. Like `testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.1'` Why would you use the spring-boot-starter-test dependency, when the whole project isn't a springboot application.
I updated my answer.
Answer: May be like this:
tasks {
    bootJar { mainClassName = "NONE" }
}


What is the isomorphism between $\mathbb Z[x]/(2,x)$ and $\mathbb Z/2 \mathbb Z$?

Question: I am trying to show that $\mathbb Z[x]/(2,x)$ is a field by showing that it is isomorphic to
$\mathbb Z/2 \mathbb Z$ but I do not know how to write this isomorphism explicitly. could someone help me in this please?
Answer: Just unwind the following isomorphisms:
$$\newcommand{\Z}{\Bbb Z}
\Z[x]/(2,x) \color{red}{\cong} \frac{\Z[x]/(x)}{(2,x)/(x)} = \frac{\Z[x]/(x)}{(2+(x))} \color{blue}{\cong} \frac{\Z}{(2)} = \Z/2\Z,
$$
where '$\color{red}{\cong}$' is by the third isomorphism theorem, and '$\color{blue}{\cong}$' follows from the isomorphism $\Z[x]/(x) \to \Z$ which induces the map $\Z[x] \to \Z$ defined by $p \mapsto p(0)$.
Answer: The kernel of the surjective morphism
$$\mathbb Z[X]\to\mathbb Z/2 \mathbb Z,\quad P(X)\mapsto\overline{P(0)}$$
is $(2,X).$


Can this nonlinear model be transformed to a linear one?

Question: Consider the model
$$y=\frac{1}{1-\theta_1x^{\theta_2}}+\theta_3+\epsilon$$
(assume Gaussian iid errors, even though this is not relevant for the question). Is it possible to transform it into a linear one? I fiddled with it a bit, without success. Am I missing something, or is it just impossible to convert it to a linear model?
Comment: Do you mean linear in some function of $x$, linear in some function of $(\theta_1,\theta_2,\theta_3)$, or both?
Comment: @whuber I mean linear in the parameters, thus according to this definition $y=\beta_0+\beta_1\sin(x)$ would be linear but $y=\beta_0+\sin(\beta_1x)$ wouldn't. Transforming $x$ and $y$ to get to a linear model is ok. Ideally, I would also like to understand how the errors transform, since I'll have to fit the model to data, but let's say that for now the most urgent point is to understand whether this model is equivalent to a linear one or not.
Answer: Using Taylor series with the Lagrange explicit form of the remainder,
$$
f(x) = f(0) + f'(0)x + \frac{f''(\xi)}{2}x^2, (\xi \in [0, x]).
$$
If you apply this to the geometric-series expansion of $\frac{1}{1 - \theta_1x^{\theta_2}}$, and then apply on the result the exponential function expansion, it is easy to see that there is no possible form that is linear in $\theta_1$ and $\theta_2$. Otherwise, you would have two equivalent polynomials, where one has a nonzero $\theta_1 \theta_2$ term, but the other doesn't, which is impossible.
Comment: @DeltaIV Those are excellent points if you're after a more formal proof I'll try to answer tomorrow (pressed for time now).
Comment: thanks, I was looking for a proof. UnfortunatelyI can't understand it :( I think the fastest/simplest solution would be to substitute the various links with the actual steps of your proof, making it self-contained. Anyway, I'll list my doubts. 1) If I'm not mistaken, you want to use the fact that two real polynomials are identical if and only if they have the same coefficients. I know this is true for polynomials of one variable and I guess it's true for multivariate polynomials, too. However, the Taylor formula with the Lagrange explicit formula of the remainder is not a polynomial...
Comment: ...because $\xi$ depends on $x$, so I don't understand why your argument holds. 2) You propose to use the geometric series expansion: does this mean you're making the substitution $z=\theta_1 x^{\theta_2}$ and then expanding $\frac{1}{1-z}$?


How to change the logo image dynamically when user change language?

Question: How to change the logo image dinamycally when user change language by clicking in the flag?
Exist an easy way to resolve this problems?
Answer: paste in page.tpl.php some condition like
<code>if ($user-> language == 'ru') $logo_url = '../rus.jpg'
</code>
etc
Comment: This should really go in a preprocess method in template.php


Manual language translation in drupal website?

Question: I am developing the job portal website in multilingual. right now site have two (German&English) languages. and have static & dynamic content. I have to use manual language translator text (client will provide the content for static). not a auto translating option like google translator.

How to translate the entire site in multilanguage, Handling dynamic data, menus etc.
What are the modules will you suggest for this

Please help us on this, and let me know if you need further details.Thanks in advance.
Comment: This question is way too broad; as per this site's FAQ, questions should be specific and should not be the kind that could be answered by an entire book.  In your case, the book is *Drupal Multilingual Websites* by Kristin Pol.
Answer: You need to enable the core modules "Locale" and "Content Translation". There's additional contributed modules like Internationalization or in short i18n
I'm not quite sure what you mean by "dynamic data". So I can't answer that part of your question.


Security risk (xss) with style attributes

Question: In our app we allow the display of html from an external source, so before we display it, we sanitize it. The source is somewhat trusted but we want to add another layer.
We remove the style tag but wish to preserve style attributes. I know scripts can be placed in that attribute and was wondering to what extent can these be used for XSS. In other words what are the specific risks with allowing style tags?
Answer: Many of the same risks are shared with HTML emails. If you're showing your HTML email in a web-based reader, such as Gmail, you want to ensure it can't escape its container and try to mess with the mail interface itself. Because of this, many styles are ripped out before the email is served to the user. Campaign Monitor has a good guide as to what CSS is allowed and disabled in different mail clients. This may be a good starting point.
Comment: Thanks for your answer, the resource you linked seems very helpful! However I was wondering more about potential XSS risks, that I have read can exist with style attributes in some cases. I was wondering what those cases are...
Comment: See: http://stackoverflow.com/questions/3607894/cross-site-scripting-in-css-stylesheets


How to set SMOTE parameters in R package DMwR?

Question: For different imbalanced data-sets which rare class' proportion differ from 30% (rare) to 5% (rare), what is the best way to define the Perc.Over and Perc.Under in SMOTE method of DMwR?
Currently I'm using this formula:
Over = ( (0.6 * COMMON_NO) - RARE_NO ) / RARE_NO

Under = (0.4 * COMMON_NO) / (RARE_NO * Over)

and finally transform it to percentage proportion:
Over_Perc = round(Over, 1) * 100

Under_Perc = round(Under, 1) * 100

and then feed it to SMOTE as following:
newData <- SMOTE(Label ~., df, perc.over=Over_Perc, perc.under=Under_Perc)

The thing is that, if the there is 30% rare data in the training set, performing classification without SMOTE gives a better performance than using SMOTE!
While by having 5%-20% of rare data in the training set, using SMOTE, the classification algorithm gives a much more better performance,
p.s. I'm using Matthew's correlation and AUC for performance measurement.
Comment: @Glen_b The title edited to be more explicit, thanks.
Comment: You're talking about the SMOTE *function* in the *R package* DMwR? It would be better to be more explicit, including in your title.
Answer: I would be surprised if you did see improvement when you had 30% 'rare' data in the training set. 30% isn't really all that rare in the context of machine learning. What you could do is cross validate with various levels of synthetic data to determine what's giving you the best accuracy on your hold-out data (pretty standard approach to parameter tuning) and then go with that for your final model build. But I would be very surprised based upon personal experience if you saw significant gains in accuracy when you SMOTE past 20-25% positive class instances in your training set.


Differentiation of power series, problem

Question: I have the power series
$$u(x) = \sum_{k=1}^{\infty} \frac{x^{2k+1}}{k(2k+1)} $$
with radius of convergence $R \geq 1 $ and I want to perform termwise derivation for $|x| \lt 1$, but it isn't working for me.
Normally, I would rewrite $u(x)$ so that it is defined for $ k=0,1,2,...$ instead of $k=1,2,3,...$, and then use that
$$u(x) = \sum_{k=0}^{\infty} a_k x^k \Rightarrow u'(x) = \sum_{k=1}^{\infty} ka_k x^{k-1}$$
but in this case that gives me
$$u(x) = \sum_{k=0}^{\infty} \frac{x^{2k+3}}{(k+1)(2k+3)} \Rightarrow u'(x) = \sum_{k=1}^{\infty} \frac{kx^{2k+2}}{(k+1)(2k+3)}.$$
The answer is supposed to be
$$u'(x) = \sum_{k=1}^{\infty} \frac{x^{2k}}{k}$$
but as you can see, I'm nowhere near close. What am I doing wrong?
Answer: $$u'(x) = \frac{d}{dx}\sum_{k=1}^{\infty} \frac{x^{2k+1}}{k(2k+1)}$$
$$=\sum_{k=1}^{\infty} \frac{d}{dx} \frac{x^{2k+1}}{k(2k+1)}=\sum_{k=1}^{\infty} \frac{(2k+1)x^{2k}}{k(2k+1)}=\sum_{k=1}^{\infty} \frac{x^{2k}}{k}$$

Your formula will not work, keep in mind that
$$\frac{x^{2k+3}}{(k+1)(2k+3)}\ne a_kx^k$$
so you cannot skip the first term, as it won't be equal to $0$.
You made a mistake when differentiating, note that $$\frac{d}{dx} x^{2k+3}=(2k+3)x^{2k+2}\ne kx^{2k+2}$$
Comment: Thank you, it seems obvious now, apparantly I forgot to use my brain. :/
Answer: Notice that
$$
 \frac{\text{d}}{\text{d}x} \sum_{k=0}^\infty a_k x^\color{red}{k}
 = \sum_{k=1}^\infty a_k \color{red}{k} x^{\color{red}{k}-1}.
$$
The rule is not to just put a $k$ in front, but to pull the exponent down — just like it's done for polynomials (if you don't know how to differentiate a polynomial you absolutely need to look this up).
Answer: You make a mistake in the term-wise differentiation.
$$\frac{d}{dx} \frac{x^{2k+3}}{(k+1)(2k+3)} = \frac{(2k+3) x^{2k+2}}{(k+1)(2k+3)}=\frac{x^{2k+2}}{k+1}$$
Therefore, in your question, it should be
$$u(x) = \sum_{k=0}^{\infty} \frac{x^{2k+3}}{(k+1)(2k+3)} \Rightarrow u'(x) = \sum_{k=0}^{\infty} \frac{x^{2k+2}}{k+1}=\sum_{k=1}^{\infty} \frac{x^{2k}}{k}.$$


Hadoop jar command error for multiple mapper inputs and 1 reducer output (Join 2 values from 2 files)

Question: Here is my sample program joining 2 datasets.
The program has 2 mappers and 1 reducer joining the values obtained from 2 different mappers having 2 different files as input.
I am getting an error in the hadoop jar command.
command:

hadoop jar /home/rahul/Downloads/testjars/datajoin.jar DataJoin
  /user/rahul/cust.txt /user/rahul/delivery.txt /user/rahul/output

Error: Invalid number of arguments Datajoin
It is actually expecting only 1 input path and 1 output path whereas in my command I have 2 inputs for 2 different mappers and 1 output.
Can anyone help me out ?
Code:
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class DataJoin {

    public static class TokenizerMapper1 extends Mapper {

        private Text word = new Text();

        public void map(Object key, Text value, Context context)
                throws IOException, InterruptedException {

            String itr[] = value.toString().split("::");
            word.set(itr[0].trim());
            context.write(word, new Text("CD~" + itr[1]));
        }
    }

    public static class TokenizerMapper2 extends Mapper {

        private Text word = new Text();

        public void map(Object key, Text value, Context context)
                throws IOException, InterruptedException {

            String itr[] = value.toString().split("::");
            word.set(itr[0].trim());
            context.write(word, new Text("DD~" + itr[1]));
        }
    }

    public static class IntSumReducer extends Reducer {
        private Text result = new Text();

        public void reduce(Text key, Iterable values, Context context)
                throws IOException, InterruptedException {
            String sum = "";
            for (Text val : values) {
                sum += val.toString();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args)
                .getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println("Usage: DataJoin ");
            System.exit(2);
        }
        Job job = new Job(conf, "Data Join");
        job.setJarByClass(DataJoin.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        MultipleInputs.addInputPath(job, new Path(otherArgs[0]),
                TextInputFormat.class, TokenizerMapper1.class);
        MultipleInputs.addInputPath(job, new Path(otherArgs[1]),
                TextInputFormat.class, TokenizerMapper2.class);
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[2]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

Comment: format your code and question better
Answer: You have error in this portion
if (otherArgs.length != 2) {
   System.err.println("Usage: DataJoin ");
   System.exit(2);
}

Your argument is of length 3. 2 inputs and 1 output.
Argument count starts from 1,2... not from 0,1....
Change to
if (otherArgs.length != 3) {
   System.err.println("Usage: DataJoin ");
   System.exit(0);
}

This solves your issue.


Disponibilizar download de arquivo do servidor

Question: Existe uma funcionalidade de cadastrar os dados de um cliente.
O usuário seleciona um arquivo do computador dele, e este arquivo é importado para o servidor em uma pasta separada, sendo que esse arquivo pode ser um PDF, JPEG, PNG.
Existe uma tela de edição deste cliente, onde ele terá um botão para fazer o download deste documento. Minha dúvida é: Como implementar a funcionalidade do download do arquivo armazenado no servidor?
Answer: Assim:
public FileResult Download(int id)
{
    var caminhoDaImagem = /* Aqui você usa id que vem por parâmetro pra fazer alguma operação que seleciona o caminho da imagem de algum lugar */
    byte[] dadosArquivo = System.IO.File.ReadAllBytes(caminhoDaImagem);
    return File(dadosArquivo, System.Net.Mime.MediaTypeNames.Image.Jpeg, "meuarquivo.jpg");
}

Veja aqui os MediaTypes para imagens disponíveis nativamente.
Uso:
http://localhost:porta/MeuController/Download/1

Comment: Prefiro esta resposta do que a outra por não deixar nenhum _stream_ aberto (mesmo que seja por pouco tempo). Mas ainda assim, não sei se é realmente necessário abrir o arquivo só pra isso. +1 =D
Comment: Funcionou perfeitamente, muito obrigado !
Comment: amigo, no caso na linha  using (var fs = new FileStream(caminhoDaImagem))  ele me pede como parametro um FileMode, no meu caso que é para downlaod o correto é usar qual? FileMode.Open?
Comment: Exato. Atualizei a resposta.
Comment: outra coisa, acusa erro em fs.ToArray(), diz que filestream nao possui um toarray
Comment: Sim, funcionaria pra `MemoryStream`, não pra `FileStream`. Veja minha edição.
Comment: Cara funcionou perfeitamente porem o nome do arquivo esta vindo com o nome do meu método la na action tem como eu nomear o arquivo que esta baixando?
Comment: Sim, veja minha edição. O nome do arquivo é o terceiro argumento.
Answer: Se o arquivo existe no disco e você sabe o caminho dele, seria melhor retorná-lo sem precisar se preocupar em abri-lo, certo?
public FileResult Download(int id)
{
    string caminho = /*Buscar caminho do arquivo */;
    var result = new FilePathResult(caminho, "image/jpeg")
    {
        FileDownloadName = "nomeParaDownload.jpg"
    };

    return result;
}

Comment: No servidor....
Comment: Entendo, mas nesse caso em disco vc diz no servidor correto ? ou na maquina da pessoa que subiu o arquivo ?
Comment: Muito obrigado pela ajuda !!
Answer: Você já deve ter visto que os métodos dos controllers que atendem às requisições das páginas retornam objetos do tipo ActionResult.
Normalmente você retorna conteúdo HTML através do método View do próprio controller. Você pode mudar o tipo do retorno para um arquivo facilmente, basta retornar um objeto FileStreamResult ao invés de chamar o método View.
Segue a documentação oficial.
E um exemplo:
public ActionResult BoloDeFuba()
{
    FileStream arquivo = new FileStream(@"c:\bolo de fubá.doc");
    FileStreamResult download = new FileStreamResult(arquivo, "application/msword"); // O segundo parâmetro é o Mime type
    download.FileDownloadName = "bolo de fubá.doc";
    return download;
}

Note que o FileStreamResult recebe qualquer objeto do tipo Stream. Você pode montar um arquivo em memória ou carregar do banco de dados ao invés de carregar de uma pasta, quando lhe for necessário ou mais conveniente.
Comment: Jovem, `FileStream` implementa `IDisposable`. Boa prática usar `using` para não causar um pico de uso de memória na aplicação.
Comment: @CiganoMorrisonMendez eu fiquei com uma dúvida, pois fechar a `Stream` impediria seu correto retorno pelo controller. E de fato, testei e usar o `using` nesse caso provoca erro. Nesse caso especial, não precisamos nos preocupar, pois o método `FileStreamResult` chama o disposing da `stream` internamente. Fonte: http://stackoverflow.com/questions/26275764/does-filestreamresult-close-stream
Comment: @CiganoMorrisonMendez valeu!
Comment: Verdade, porque `FileStreamResult` supõe que sua conexão com o *stream* vai ficar aberta depois do retorno. Não acho essa abordagem boa pra arquivo normal.
Comment: Muito obrigado pela ajuda !


How can I apply Akaike Information Criterion and calculate it for Linear Regression?

Question: I tried to develop a Linear Regression model and want to understand how to measure models with a different combination of variables with Akaike's Information Criterion.
Consider the Linear Regression below:
$ChildenScore = \alpha + \beta_1FatherEducation + \beta_2MotherEducation + \beta_3FatherIncome + \beta_4MotherIncome$
I'd like to try AIC to choose the best model.
Hope to hear some explanations.
Comment: Here, I think you can find a much more extensive discussion on the subject: http://stats.stackexchange.com/questions/215654/what-does-the-akaike-information-criterion-aic-score-of-a-model-mean
Answer: A simple formula for the calculation of the AIC in the OLS framework (since you say linear regression) can be found in Gordon (2015, p. 201):
$$\text{AIC} = n *\ln\Big(\frac{SSE}{n}\Big)+2k $$
Where SSE means Sum of Squared Errors ($\sum(Y_i-\hat Y_i)^2$), $n$ is the sample size, and $k$ is the number of predictors in the model plus one for the intercept. Although AIC values are not generally interpretable, differences between values for different models can be interpreted (A number of questions on CV covers this issue, for example in here). So, the model with the smallest AIC is usually selected. It is easy to see why this is the case in the above formula: All else being equal, as the SSE decreases, AIC also decreases.
In other sources, you may find a more general, maximum-likelihood formula. For example, in Applied Regression Analysis and Generalized Linear Models, Fox provides:
$$\text{AIC}_j \equiv - \text{log}_eL(\hat \theta_j)+2s_j$$
where $L(\hat \theta_j)$ is the maximized likelihood under model $M_j$ ($\theta_j$ is the vector of parameters of the model, $\hat \theta_j$ is the vector of maximum-likelihood estimates of the parameters) and $s_j$ is the number of parameters (Fox 2016, p. 673-674). In OLS framework, this second formula simplifies to the first one. Using this first formula, I think it is not difficult to calculate and use AIC for the comparison of linear regression models.

Fox, J. (2016). Applied Regression Analysis and Generalized Linear Models (3rd ed.). Los Angeles: Sage Publications.
Gordon, R. A. (2015). Regression Analysis for the Social Sciences. New York and London: Routledge.
And the original article:
Akaike, H. (1998). Information Theory and an Extension of the Maximum Likelihood Principle. In E. Parzen, K. Tanabe, & G. Kitagawa (Eds.), Selected Papers of Hirotogu Akaike (pp. 199–215). New York: Springer.
Comment: Thank you for the explanation and it helps me step further into it.
Comment: @PyRsquared sorry for late reply. Honestly, I don't know. I am not experienced with tree regressor models, but I'll let you know if I can find something useful.
Comment: Can you use the same formula from Gordon (2015, p. 201) with a tree regressor model or is that invalid?
Comment: Looking at https://en.wikipedia.org/wiki/Akaike_information_criterion I find the well known $\ln\mathcal{L}(\mu,\sigma)
    \, = \, -\frac{n}{2}\ln(2\pi) - \frac{n}{2}\ln\sigma^2 - \frac{1}{2\sigma^2}\sum_{i=1}^n (x_i-\mu)^2$ for assuming that the errors scatter with a normal distribution around the model. If AIC =\ln\mathcal{L} +2k why do you and e.g. statsmodels https://github.com/statsmodels/statsmodels/blob/2eac8066b068a88f00a29f8ff728b04e58248375/statsmodels/regression/linear_model.py#L595 compute AIC using log likelihood function llf=-log(SSR)+... shouldn't it be llf=-SSR+...(no log)
Comment: I see you have the answer here: https://stats.stackexchange.com/questions/583479/why-is-aic-computed-with-a-term-containing-logssr-instead-of-ssr . So, I removed my comment. Just a note: wiki page you linked gives an idea about this under "Comparison with least squares."


Find Max Value in a field of a shapefile

Question: I have a shapefile (mich_co.shp) which I try to find the county with max population. My idea is to use max() function it's not possible. Here is my code so far:
from osgeo import ogr
import os

shapefile = "C:/Users/root/Python/mich_co.shp"
driver = ogr.GetDriverByName("ESRI Shapefile")
dataSource = driver.Open(shapefile, 0)
layer = dataSource.GetLayer()

for feature in layer:
    print(feature.GetField("pop"))
layer.ResetReading()

The code above however only print all values of "pop" field like this:
10635.0
9541.0
112039.0
29234.0
23406.0
15477.0
8683.0
58990.0
106935.0
17465.0
156067.0
43868.0
135099.0

I tried:
print(max(feature.GetField("pop")))

but it returns TypeError: 'float' object is not iterable. For this, I've also tried:
for feature in range(layer):

and it returns TypeError: 'Layer' object cannot be interpreted as an integer.
Any helps of hints would be much appreciated.
Thanks you!
Answer: max() needs an iterable, such as a list. Try to build a list:
pops = [ feature.GetField("pop") for feature in layer ]
print(max(pops))


Moment JS how to set is between and equal between 2 dates?

Question: Hi I would like to check if given date is between 2 dates with hours of course. So if we check if 13:00 is between 13:00 and 15:00 I would like to get true.
I have this code:
 let timeSlotStartTime = moment.utc(timeSlot.startTime);
    let startDayTime =  moment.utc(timeSlotStartTime.format('YYYY-MM-DD') + "T" + moment.utc(condition.parameters.timeFrom).format('HH:mm:00'));
    let endDayTime =  moment.utc(timeSlotStartTime.format('YYYY-MM-DD') + "T" + moment.utc(condition.parameters.timeTo).format('HH:mm:59'));
    return timeSlotStartTime.isBetween(startDayTime, endDayTime);

So I am checking if it is between but it works weird.
If I set hours of startDayTime = 14:59 and endDayTime = 17:00
and timeSlotStartTime is 14:15 it stills gives me true.(this is the bigger problem)
and if timeSlotStartTime = 17:00 gives me false (which I want to give me true)
Is there any possibility I can check between and equal within 2 moments?
thanks
Comment: [documentation here](https://momentjscom.readthedocs.io/en/latest/moment/05-query/06-is-between/) indicates that you can pass an argument to `isBetween` that allows it to include the ends
Comment: double check the strings you're forming, if `isBetween` is giving unexpected results then there is probably an issue with your dates
Comment: .isBetween() compares by milliseconds by default but maybe if you put 'minute' as a 3rd argument it will return false in your first example instead of true.
Comment: OP, it would be nice if your example code filled out the timeSlot object for us so if someone is trying to test a solution, they don't have to guess what values you put in there.
Comment: You need to pass in 3rd and 4th parameter to `isBetween` timeSlotStartTime.isBetween(startDayTime, endDayTime, 'minutes', '[]');
Comment: I ran timeSlotStartTime.isBetween(startDayTime, endDayTime) with the times you provided and received false. I think @RobbieMilejczak is correct. Its probably a issue with your dates
Answer: https://momentjscom.readthedocs.io/en/latest/moment/05-query/06-is-between/
You can use the 4th argument to include edges
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '[)'); //true
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '(]'); //true
moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', null, '[]'); //true

Where square brackets means 'include this end' and parentheses means 'exclude this end'
Comment: "If I set hours of startDayTime = 14:59 and endDayTime = 17:00 and timeSlotStartTime is 14:15 it stills gives me true.(this is the bigger problem)" this only answers a small part of OP's question
Answer: isBetween takes a third parameter (minutes, hours,days etc) and fourth parameter [] to include inclusivity
You should pass the third parameter as 'minutes' since you are comparing minutes
var timeSlotStartTime  = moment({ hour:14, minute:15 });
var startDayTime  = moment({ hour:14, minute:59 });
var endDayTime   = moment({ hour:17, minute:00 });

var result = timeSlotStartTime.isBetween(startDayTime, endDayTime, 'minutes', '[]');

console.log(result); // false

Also isBetween match is exclusive.
moment('2010-10-20').isBetween('2010-01-01', '2012-01-01', 'year'); // false as it is exclusive

Version 2.13.0 introduces inclusivity. A [ indicates inclusion of a value. A ( indicates exclusion. If the inclusivity parameter is used, both indicators must be passed.
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '[)'); //true
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '(]'); //true
moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', null, '[]'); //true

Comment: "If I set hours of startDayTime = 14:59 and endDayTime = 17:00 and timeSlotStartTime is 14:15 it stills gives me true.(this is the bigger problem)" this only answers a small part of OP's question


Como chamar duas funções no onClick (ReactJS)?

Question: Aqui estão minhas duas funções,
chamaLogin = () => {
            this.setState({
                redirect: true
            });
        };

-----------------------------------------------------------------------------
handleSubmit = event => {
            event.preventDefault();

            const arrInsert = {
              nome: this.state.nome,
              apelido: this.state.apelido
            };

            axios.post("http://localhost:3000", arrInsert)
              .then(res => {
                console.log(res);
                console.log(res.data);
              })

          }

Como posso chamar essas duas funções no onClick ?
Comment: Insira como está o seu onClick e o que de fato ele está chamando agora.
Comment: apenas : onClick={this.handleSubmit}
Comment: Queria chamar as duas funções ao mesmo tempo ou uma após a outra?
Comment: pode ser uma após a outra, pra ficar melhor o carregamento...
Então seria: primeiro a handleSubmit depois a chamaLogin
Answer: Tens de chamar uma dentro da outra. Podes encontrar um nome mais genérico e chamar as duas dentro dessa outra função de nome mais genérico mas só podes ter um onClick por elemento.
Por vezes faz sentido ter um auscultador de evento no elemento/componente pai e outro no filho, ou ter um em capture e outro quando a propagação vem do elemento... mas na maioria dos casos chamar duas funções é o que faz mais sentido:
No JSX:
onClick={this.handleClick}

e no componente:
handleClick(e){
    this.chamaLogin();
    this.handleSubmit(e);
}

Se as quiseres encadear:
No caso da Promise seria :
.then(() => this.chamaLogin());

No caso do setState seria teoricamente:
chamaLogin(e){
    this.setState({
        redirect: true
    }, () => this.handleSubmit(e));

mas ter uma referência assíncrona ao objeto do evento (e) não vai fazer o que esperas pois o .preventDefault() já não vai funcionar, o evento já foi "consumido".
Comment: Obrigada!!! funcionou
Answer: Eu fiz de uma forma bem parecida do que o Sérgio sugeriu, mas sem usar o this. Aqui, eu chamo uma view (goToSendPage) com o click do botão e também envio dados para um endpoint (apiCallData). Minha aplicação é SPA (Single Page App).
Meu componente:
const SendButton = (props) => {
  const { goToSendPage, id, clientNumber, countNumber } = props;

  const classes = useStyles();

  const handleClick = (event) => {
    goToSendPage();
    const key = "sendData";
    const value = `cliente: ${clientNumber},${countNumber}`;
    apiCallData(id, key, value);
  };

  return (
    <Container>
      <Button
        variant="contained"
        classes={{ root: classes.root }}
        target="_blank"
        style={{ justifyContent: "space-between", alignItems: "center" }}
        onClick={handleClick}
      />
    </Container>
  );
};

export default SendButton;

Na documentação do React tem isso bem explicadinho, caso voce queira ler depois :)


Piano tuning hammer gets stuck

Question: I have a 100 year old German baby grand and I need to tweak some of the tuning on a few things. I bought a square entry tuning hammer but once I bring the tuning up (it's on 430 because not holding concert pitch) the hammer gets stuck on the pin and inevitably once I've managed to get it off again I've moved the pin.
Should I be using a different shape/size tuning hammer or prepping in some way (they are all a bit rusty etc)
I realise tuning is a skill and I could get a pro in but I would like to do this myself (at least the little tweaks). And I'm not up for ditching the piano because it is beautiful and not ready for the scrap heap.
Comment: @Tim I'd be surprised if there were only 4. Most standardisations like this occurred somewhere halfway the 20th century, so after the OP's grand was made. Regardless I would not be surprised if all factories still had proprietary pin heads.
Comment: Are you certain you bought the correct size key? There's about 4 different sizes.
Comment: Not at all. I'm happy to get another one but not sure what I should be getting. Most of the ones that aren't cheap Chinese ones (which seem generic) are shipped from the US and I'm not 100% how to ensure I get the right one this time
Answer: Yes, from what you've told us it seems clear that you have the wrong size key.
Answer: From a purely mechanical perspective the rust definitely wont be helping you. It will help cause binding under tension, so I would recommend trying to remove the rust if you can.
Obviously it's going to be fiddly collecting up the rust, but one technique is to use a vacuum cleaner while rubbing the rust off the pins so that rust doesn't fall into awkward places.
Comment: With emery paper? I can give that a go. There are only a few notes that are in need of work so I'm not going to have to do the full set luckily
Comment: Yes, emery paper is probably your best bet - nice and fine

Piano tuning hammer gets stuck


Rounding to unit above with C#

Question: How to I round a decimal upwards to the nearest int.
i.e.
1.2 ---> 2
5.6 ---> 6
79.9 ---> 80
85.01 ---> 86

(int)Math.Round(count / (decimal)rows, MidpointRounding.AwayFromZero)
Answer: Math.Ceiling is your friend

Rounding to unit above with C#


Write a rotate_left(char *) function in C

Question: How can we write a rotate_left(char*) function that takes a null-terminated string and rotate it to the left one character. For ex: "abcdef" --> "bcdefa"
So far i have:
 rotate_left(char* str){
  int i, lin;
   char buff[lin+1], temp;

   lin = strlen(str);
   strcpy(buff,str);

 }

Can anyone help me complete this function. Really appreciated.
Comment: For starters, you don't assign a value to `lin` before using it.
Answer: I don't think I will use much of the question code.  Here is an alternate solution:
rotate_left(char* str){
   char ch;

1) Store the character that will to be rotated from the left side, to the right side.
   ch=*str;

2) Shift all characters in the string left one byte.
   memmove(str, str+1, strlen(str)-1);

3) Copy the rotated character to the end of 'str'.
   str[strlen(str)] = ch;
   }

Answer: char last = str[0];
int len = strlen(str);

for(i=0; i< len-1; i++) { str[i] = str[i+1]; }
str[len-1] = last;

Answer: You could do that rotation in place, like this:
char * rotate_left1 (char *str) {
    char ch;
    int i;

    ch = str[0];
    for (i = 0; str[i+1]; i++) {
        str[i] = str[i+1];
    }
    str[i] = ch;

    return str;
}


How to handle a lot of simple unique URLs (for click's statistics)?

Question: I need to create a lot of simple unique URLs (for click's statistics).
Each such URL consist of several parameters (project_name, unique_ID, etc).
Examples:
example.com/project1_id35 or
example.com/project1/id35 or
project1.example.com/id35 ...
I plan to use Rules to decipher these URLs and to save it to database.
But how Drupal site can catch all such URLs?
It's really hard to make aliases for every case.
May be mod_rewrite can help?
Or is there any useful module for such task?
PS. It seems mod_rewrite the best solution.
PPS. Another possible solution: override 404 page.
Answer: A great module to aliases auto-generation is PathAuto. It will allow you to define patterns to your url's using tokens, things like:
article/[title-raw]
will generate:
www.example.com/article/my-blog-post
Once you install the module, go to:
Administration > Configuration > Search and metadata > URL aliases, in the "Patterns tab" (admin/config/search/path/patterns).
Comment: As I know I can use Pathauto if I have node for every case. But there are no such nodes (and no need it).


How Do You Add A Replay Button To A Game?

Question: Im trying to make a replay button for my game where when you die the game pauses until you press the space bar which resets the game so you can play again. I'm using python with pygame. If you can fix this, please do! It would make my day. If possible, please include the code for the fix and where to put it because I am new to python. The source code below is the game I'm working on.
import pygame
import random
import sys
import time

pygame.init()

WIDTH = 800
HEIGHT = 600

TRASH = (110, 110, 110)
RED = (135, 56, 47)
player_pos = [400, 475]
player_size = 50
screen = pygame.display.set_mode((WIDTH, HEIGHT))

enemy_size = 50

SPEED = 11

FPS = 120

enemy_pos = [random.randint(0, WIDTH - enemy_size), 0]
enemy_list = [enemy_pos]

game_over = False

score = 0

clock = pygame.time.Clock()

myFont = pygame.font.SysFont("monospace", 35)

pygame.display.set_caption('Crab Dodge,  By: ChanceMeteor515')

pygame.mouse.set_visible(0)

def drop_enemies(enemy_list):
    if len(enemy_list) < 9:
        x_pos = random.randint(0, WIDTH - enemy_size)
        y_pos = 10
        enemy_list.append([x_pos, y_pos])

def draw_enemies(enemy_list):
    for enemy_pos in enemy_list:
        pygame.draw.rect(screen, TRASH, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size))

def update_enemies_pos(enemy_list, score):
    for idx, enemy_pos in enumerate(enemy_list):
        if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT:
            enemy_pos[1] += SPEED
        else:
            enemy_list.pop(idx)
            score += 1
        return score

def collision_check(enemy_list, player_pos):
    for enemy_pos in enemy_list:
        if detect_collision(enemy_pos, player_pos):
            return True
        return False

def edge_detection1(player_pos):
    if player_pos[0] > 750:
        return True
    return False

def edge_detection2(player_pos):
    if player_pos[0] < 0:
        return True
    return False

def detect_collision(player_pos, enemy_pos):
    p_x = player_pos[0]
    p_y = player_pos[1]

    e_x = enemy_pos[0]
    e_y = enemy_pos[1]

    if (e_x >= p_x and e_x < (p_x + player_size)) or (p_x >= e_x and p_x < (e_x + enemy_size)):
        if (e_y >= p_y and e_y < (p_y + player_size)) or (p_y >= e_y and p_y < (e_y + enemy_size)):
            return True
        return False

while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            x = player_pos[0]
            y = player_pos[1]
            if event.key == pygame.K_LEFT:
                x -= player_size
            elif event.key == pygame.K_RIGHT:
                x += player_size
            elif event.key == pygame.K_a:
                x -= player_size
            elif event.key == pygame.K_d:
                x += player_size
            elif event.key == pygame.K_q:
                x -= player_size
            elif event.key == pygame.K_ESCAPE:
                sys.exit()

            player_pos = [x, y]
    screen.fill((194, 178, 128))

    drop_enemies(enemy_list)
    score = update_enemies_pos(enemy_list, score)

    text = "Score:" + str(score)
    label = myFont.render(text, 1, (0, 0, 0))
    screen.blit(label, (WIDTH - 200, HEIGHT - 40))

    if edge_detection1(player_pos):
       player_pos[0] = 0

    if edge_detection2(player_pos):
        player_pos[0] = 750

    if collision_check(enemy_list, player_pos):
        time.sleep(1.4)
        game_over = True
    draw_enemies(enemy_list)
    pygame.draw.rect(screen, RED, (player_pos[0], player_pos[1], player_size, player_size))
    clock.tick(FPS)
    pygame.display.update()

Answer: OK, I've done what you've wanted but it requires Tkinter cause I was a bit lazy.
import pygame
import random
import sys
import time
import os
from tkinter import *
from tkinter import messagebox
print(os.getcwd())

pygame.init()

def game():
  WIDTH = 800
  HEIGHT = 600

  TRASH = (110, 110, 110)
  RED = (135, 56, 47)
  player_pos = [400, 475]
  player_size = 50
  game_over = False
  screen = pygame.display.set_mode((WIDTH, HEIGHT))

  enemy_size = 50

  SPEED = 11

  FPS = 120

  enemy_pos = [random.randint(0, WIDTH - enemy_size), 0]
  enemy_list = [enemy_pos]

  score = 0

  clock = pygame.time.Clock()

  myFont = pygame.font.SysFont("monospace", 35)

  pygame.display.set_caption('Crab Dodge,  By: ChanceMeteor515')

  pygame.mouse.set_visible(0)

  def drop_enemies(enemy_list):
      if len(enemy_list) < 9:
          x_pos = random.randint(0, WIDTH - enemy_size)
          y_pos = 10
          enemy_list.append([x_pos, y_pos])

  def draw_enemies(enemy_list):
      for enemy_pos in enemy_list:
          pygame.draw.rect(screen, TRASH, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size))

  def update_enemies_pos(enemy_list, score):
      for idx, enemy_pos in enumerate(enemy_list):
          if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT:
              enemy_pos[1] += SPEED
          else:
              enemy_list.pop(idx)
              score += 1
          return score

  def collision_check(enemy_list, player_pos):
      for enemy_pos in enemy_list:
          if detect_collision(enemy_pos, player_pos):
              return True
          return False

  def edge_detection1(player_pos):
      if player_pos[0] > 750:
          return True
      return False

  def edge_detection2(player_pos):
      if player_pos[0] < 0:
          return True
      return False

  def detect_collision(player_pos, enemy_pos):
      p_x = player_pos[0]
      p_y = player_pos[1]

      e_x = enemy_pos[0]
      e_y = enemy_pos[1]

      if (e_x >= p_x and e_x < (p_x + player_size)) or (p_x >= e_x and p_x < (e_x + enemy_size)):
          if (e_y >= p_y and e_y < (p_y + player_size)) or (p_y >= e_y and p_y < (e_y + enemy_size)):
              return True
          return False

  while not game_over:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              sys.exit()
          if event.type == pygame.KEYDOWN:
              x = player_pos[0]
              y = player_pos[1]
              if event.key == pygame.K_LEFT:
                  x -= player_size
              elif event.key == pygame.K_RIGHT:
                  x += player_size
              elif event.key == pygame.K_a:
                  x -= player_size
              elif event.key == pygame.K_d:
                  x += player_size
              elif event.key == pygame.K_q:
                  x -= player_size
              elif event.key == pygame.K_ESCAPE:
                  sys.exit()

              player_pos = [x, y]
      screen.fill((194, 178, 128))

      drop_enemies(enemy_list)
      score = update_enemies_pos(enemy_list, score)

      text = "Score:" + str(score)
      label = myFont.render(text, 1, (0, 0, 0))
      screen.blit(label, (WIDTH - 200, HEIGHT - 40))

      if edge_detection1(player_pos):
         player_pos[0] = 0

      if edge_detection2(player_pos):
          player_pos[0] = 750

      if collision_check(enemy_list, player_pos):
          time.sleep(1.4)
          Tk().wm_withdraw() #to hide the main window
          game_over = True
      draw_enemies(enemy_list)
      pygame.draw.rect(screen, RED, (player_pos[0], player_pos[1], player_size, player_size))
      clock.tick(FPS)
      pygame.display.update()
  ans = messagebox.askretrycancel("askretrycancel", "Try again?")
  root=Tk()
  root.destroy()
  return ans
while True:
  if game() == True:
    pass
  else:
    break

Comment: Does it work? Pls let me know
Comment: Accept the answer if it does
Comment: Ummm, it started working and then it just froze. Thanks for answering tho
Comment: Nvm I changed just a little bit and it worked.
Comment: btw Tk().wm_withdraw() did not hide the main window is I just deleted it/
Comment: ok thanks for letting me know


Business Aspect of Agile

Question: As I understand Agile is more or less like an open/flexible process. Meaning I anticipate and expect stakeholders' rapid changes.
But how about the business aspect of this? What if the stakeholder has a specific budget for a product?
What if their changes go beyond the specified budget?
Is there a term as an "agile contract"?
Comment: What if you want a car you can't afford?
Comment: @BartoszKP I *always* want a car I can't afford.
Comment: @BartoszKP well, as an engineer, I'd like one of the cars as specified by the stakeholders - 0-60 in 2.3secs, carries 9 people, and still > 100 mpg. Oh yes, and I'd like it tomorrow, and under £1000 please :-)
Comment: @RogerRowland Like I said - nothing wrong with dreaming ;)
Comment: @RogerRowland Well, nothing wrong with wanting I suppose. As long as you don't yell on engineers that it's their fault that cars are so expensive ;)
Answer: One benefit of Agile is that unfinished stories are always tracked at the end of each sprint (e.g. 2 weeks).
In contrast, a more Waterfall-like approach can "encourage" the unfinished requirements to go unidentified until the end of the development phase (e.g. months).
Better to know ASAP that certain requirements are not going to be addressed on the current budget.
For these reasons, the most "difficult" requirements are often tackled first.
In short, Agile is perfectly business and budget friendly.
Answer: Other advantages of agile in your scenario are the focus on prioritizing requirements and creating finished work in increments.
When we developed software in phases with Waterfall, and the changes went past the budget, we often found out too late and were forced to choose between supplementing project funding or getting nothing for our sunk cost. Usually the choice was to keep spending so that we'd have something to show for all that effort and project budgets would quickly get out of control.
With agile, we build in order of priority and create production quality work in each increment. When we get to the point that the ask exceeds the budget we are in a better position because now we have a production quality product that includes all of the most important features that could be built for the budget we had. We can still choose to spend more money to get more features if we want to, but we also have the option to stop and we still have a product to show for it.
Answer: The business should expect a better product. Typically a large percentage of features are not used or are worked on at the expense of something more valuable.
Leaving decisions of what to build to the last responsible moment and not trying to guess all the detailed requirements up front means you are more likely to get a better product. By collaborating as a team and avoiding costly traditional negotiating over requirements upfront, sign offs, and hand overs you will be more efficient.
Agile explained through the Stacey Matrix and Cyefin helps understand why Agile works best on certain projects.
The Project Management (Iron) Triangle helps demonstrates what suffers with projects when requirements change and budget and cost remain fixed - quality.
This can is a vicious circle as technical debt slows teams down. As they are pressed to deliver more quality gets worse.

Business Aspect of Agile