Will managed files be deleted if no corresponding entry exists in file_usage?

Question: I am using the services module to implement a REST API.  One resource allows POSTs with uploaded files.  During a services pre-process function I save the file to the temp directory.  During a services post-process function, I move the file to a private directory, call file_usage_add, and save the host entity.  When a validation error occurs between saving the temp file and moving the temp file to a permanent location, I am not explicitly deleting the temp file.  I thought drupal cron would take care of this for me, since no entry for this file exists in file_usage.  However, it doesn't appear that cron is taking care of this for me.  Any thoughts as to why this is?
When reviewing file_managed, I see the temporary file that I'd like deleted.  When reviewing file_usage, I don't see any corresponding record.
Update -- One additional piece of info: in most cases, the file is actually missing.  I believe this is due to OS reboot clearing out the /tmp directory.  Regardless, would system_cron still remove the file_managed entry for a file if the actual file were no longer found?
This problem arose since I started uploading files from a native mobile app. The filename is the same every upload.  There are some cases where the temp filename doesn't exist in the /tmp directory, but a record in file_managed still exists with a URI of that filename.  So I'm getting an integrity error when the file_managed table is being saved.  I plan on updating the app to create a random filename.  In the meantime, I'd like to clean up the database and surrounding "glue" logic that manages these files.  If system_cron will do it all for me then great.  But from what I can tell, system_cron is not removing old, completely unused (and unreferenced) records in file_managed.
Answer: Drupal automatically deletes temporary files in system_cron().
Permament files which have no file_usage are not deleted, however.
Comment: And additionally, must be older than 6 hours.
Comment: @lkiss80 The file won't be deleted unless it has a `status` of zero in the `file_managed` table
Comment: That's what I thought.  However, entries in file_managed are still hanging around, even though no corresponding entries exist in file_usage, and no actual file exists anymore.  I must be missing something here...
Comment: @Clive - Thanks. Your response prompted me to double checked the status bit. I originally referenced a book that incorrectly flipped the meaning of this bit.  Thanks again!


Colocar checkbox em apenas dois itens - JSTREE

Question: Gostaria de saber como eu faço para que os nós Child 1 e o Child 2 seja do tipo checkbox em que eu possa marcar e desmarcar?
Obs: mas somente esses dois nós.
Código JSFiddle
Answer: Você pode ativar a opção para utilizar checkbox no plugin e ocultar com CSS aqueles que não forem descendentes de .jstree-children, exemplo:
HTML

Folder 1

Child 1

Child 2

Folder 2

CSS
ul:not(.jstree-children) > li.jstree-node > a.jstree-anchor > i.jstree-checkbox
{
    display:none;
}

jQuery
$(function () {
    $('#jstree').jstree({
      "checkbox": {
          "keep_selected_style": true
      },
      "plugins": ["checkbox"]
  });
});

Veja exemplo no JSFiddle


Regex capturando exatamente x digitos de uma string

Question: Estou tentando fazer uma expressão regular em que ela capture exatamente x dígitos(especificamente no meu caso x=6).
Exemplo:
"teste 1 n°123456 fim do teste"
"teste 2 n°7890123 fim do teste"
Quero que retorne na expressão regular apenas o "123456", ou seja, apenas números que contém exatamente 6 dígitos. Como poderia fazê-lo?
Answer: Pode utilizar a seguinte regex:
\b\d{6}\b

O \d é o meta caratere para digito, procurando um digito de 0 a 9.
O {6} indica que tem de encontrar 6 desses seguidos.
Os \b indicam que é inicio e fim de palavra, o que implica que tem de ser os 6 números seguidos, fazendo com que 7 ou mais números já não seja válido.
Veja a funcionar no regex101
Funciona perfeitamente para o caso que mencionou. Se necessitar de algo mais amplo que funcione mesmo com os números seguidos ao texto, pode utilizar por exemplo esta regex, que já é um pouco mais complexa:
^|\D(\d{6})\D|$

Explicação:
^|\D    - Inicio de linha ou algo que não seja um digito
(\d{6}) - 6 dígitos seguidos e a serem capturados no grupo de captura 1
\D|$    - Seguido de algo que não seja um digito ou fim de linha

Aqui também usei o meta caratere \D que é o inverso do \d, e significa algo que não um digito.
Uma diferença subtil desta regex para a anterior é que nesta o digito encontra-se no grupo de captura 1, ao invés de na captura completa da regex. Por isso independentemente da forma como utilizar a regex em código, o obter do numero será sempre diferente.
Veja no regex101
Em território de regex é sempre assim. As mais simples regra geral são mais restritas e não controlam todos os casos, mas por vezes são exatamente o que precisa. Tem sempre de tentar encontrar um equilíbrio entre o que pretende validar e a complexidade/eficiência que está disposto a usar/abdicar.
Comment: Quanto mais estudo regex, mais percebo que o que vc disse no último parágrafo é verdade: achar o equilíbrio entre a exatidão e a complexidade é fundamental - e muitas vezes é difícil achar este ponto de equilíbrio. Se o sistema permitisse, eu daria mais um voto só por esse comentário :-) Aliás, outra alternativa é usar lookaheads e lookbehinds negativos: `(?<!\d)(\d{6})(?!\d)` - [veja](https://regex101.com/r/isuWza/2)
Comment: @hkotsubo É mesmo. Há muitos casos em que simplesmente não compensa a complexidade e perda de eficiência para controlar mais um ou outro caso que quase nunca acontecem, com regexs monstruosas com eu já vi algumas. Curiosamente eu comecei por fazer exatamente essa regex com lookahead negativo, mas acabei por trocar pela que tenho na resposta porque achei que ficava mais simples. Para alem disso alguns engines são mais restritivos em *lookbehind*
Answer: Ótima resposta do @Isac que aplica o meta-caractere \b, porém, usando o \b só vai servir no seu caso específico. Eu gostaria de deixar aqui uma alternativa mais genérica.
O \b funciona no seu caso porque os 6 dígitos estão precedidos por um caractere especial ° e sucedida por um espaço, ou seja, a string de captura entre dois \b's só vai ser encontrada se ela estiver entre dois caracteres especias (exceto underlined _) ou espaço.
Por exemplo, se na primeira string citada na pergunta não tivesse o ° antes do número (fosse assim: n123456), o número 123456 não seria encontrado com \b. Veja:

var str = "teste 1 n123456 fim do teste";
console.log(/\b\d{6}\b/.test(str));

Isso porque o \b, como dito, exige que a string procurada esteja entre caracteres especiais.
Minha sugestão não é das mais elegantes, mas irá retornar o número (independente de onde esteja) que contenha 6 dígitos.
Veja:

var string1 = "teste 1 n123456 fim do teste";
var string2 = "teste 2 n°7890123 fim do teste";

var teste1 = string1.match(/\d{6,}/);

if(teste1 && teste1[0].length == 6){
   console.log("'"+ teste1[0] +"' encontrado na string1");
}

var teste2 = string2.match(/\d{6,}/g);

if(teste2 && teste2[0].length == 6){
   console.log(teste2[0]);
}else{
   console.log("Nada encontrado na string2");
}

O que o código faz? Ele procura um número com 6 ou mais dígitos e depois faz a comparação se esse número tem apenas 6 caracteres; se tiver, OK, caso contrário, não faz nada.


What is the biggest margin with which a team has won an NBA game?

Question: What is the biggest margin with which a team has won an NBA game ?
Comment: I've reopened this - but also downvoted it as I believe it is a poor question. The tooltip for downvoting a question says "This question does not show any research effort", and this question currently clearly falls into that category.
Answer: This is very easily searched for:
The biggest margin in the history of the NBA is 68 and was achieved in 1991, when the Cleveland Cavaliers beat Miami Heat with 148-80 (game report). In there is also a link to this page showing lots of other NBA records. Here is another source.


Project Euler 215 - Solving with Sparse Matrices and Vector Multiplication

Question: I wrote a program to solve Project Euler Problem #215 (see below for description) using memoization, and when I got access to the PE forums, I saw everyone else wrote programs that also used dynamic programming/memoization techniques.
However, from this stackoverflow answer, Dietrich Epp solved it in Haskell using a sparse matrix of NxN dimensions (an adjacency matrix) and then multiplied the matrix by a 1xN vector to arrive at the solution.
Quote from the poster Dietrich Epp:

You compute each possible way to tile a row. Let's say there are N ways to tile a row. Make an NxN matrix. Element i,j is 1 if row i can appear next to row j, 0 otherwise. Start with a vector containing N 1s. Multiply the matrix by the vector a number of times equal to the height of the wall minus 1, then sum the resulting vector.

How come the matrix x vector solution is viable for this problem, as in what property is it exploiting to solve the question?
Thanks in advance!

Problem Description
Consider the problem of building a wall out of 2x1 and 3x1 bricks (horizontal and vertical dimensions) such that, for extra strength, the gaps between horizontally-adjacent bricks never line up in consecutive layers, i.e. never form a "running crack".
For example, the following 9x3 wall is not acceptable due to the running crack shown in red:

There are eight ways of forming a crack-free 93 wall, written W(9,3) = 8.
Calculate W(32,10).
Comment: See my answer at stackoverflow.com: http://stackoverflow.com/questions/7788462/project-euler-215-in-java/7788862#7788862
Answer: This is a nice way of keeping track of the linked recurrence relations.  The vector initially starts out with all $1$'s indicating there is one way to make a height $1$ wall with each type of layer on the bottom.  After one multiplication by the matrix position $j$ in the vector is the number of ways to have a wall of height $2$ with the bottom layer of type $j$.  Each successive multiply adds one layer because position $j$ will have the sum of all the other types of layer that can come above it.  So after $n-1$ multiplies you have walls of height $n$.  Then at the end you sum over all the types of bottom layer.
Comment: Great, thanks for the explanation!


Fast way to find Javascript source file in debugger?

Question: RStudio uses Chrome Devtools for debugging support in its Viewer pane.  (It is labelled as "RStudio Devtools", but I'm not sure to what extent it is customized besides that).  You can open its window using Right-click "Inspect Element".
When developing a display that uses Javascript, the source pane in Devtools is very useful for setting breakpoints, single stepping, etc.
One thing that is fairly inconvenient is finding the line where I want to set a breakpoint.  Once I have the source file open searching it is easy, but getting to the right source file is hard.  Typically after I go to the source pane, I see something like this:

I know the name of the file I want to debug, but it's buried several levels below index.html in the list of files on the left.  Is there a way to quickly search this hierarchy of files for a filename, so that I don't need to open all the levels down to the file I want?  For example, I might want to debug htmlwidgets.js:
Answer: You'll note in the "sources" pane that, if you have no open files, you'll see a message in the middle that indicates you can type Command + P (or Ctrl + P on Windows/Linux) to open a file.  When clicked, this will open a text box that will fuzzy search across the names of all source files.  Type the filename there and it should be in the first results.  You can navigate to it with the keyboard and select it with <Enter>/<return>, or simply clicked with the mouse.
Comment: Thanks!  I've never noticed that message before:  I think RStudio usually automatically opens `rstudio-0.js`.
Comment: No problem-- happy coding!


Debugging kmalloc-64 slab allocations / memory leak

Question: I have two virtual servers that are configured very similarly: Debian bullseye (testing), 5.6.0 kernel, 512 MB RAM. They're both running a similar workload: MySQL, PowerDNS, WireGuard, dnstools.ws worker, and Netdata for monitoring.
On just one of the servers, the unreclaimable slab memory is growing linearly over time, until it hits a maximum (when the server's memory is 100% allocated):

The drop is when I rebooted the VPS.
slabtop on the bad server:
 Active / Total Objects (% used)    : 1350709 / 1363259 (99.1%)
 Active / Total Slabs (% used)      : 25358 / 25358 (100.0%)
 Active / Total Caches (% used)     : 96 / 124 (77.4%)
 Active / Total Size (% used)       : 113513.48K / 117444.72K (96.7%)
 Minimum / Average / Maximum Object : 0.01K / 0.09K / 8.00K

  OBJS ACTIVE  USE OBJ SIZE  SLABS OBJ/SLAB CACHE SIZE NAME
1173504 1173504 100%    0.06K  18336       64     73344K kmalloc-64
 22512  18020  80%    0.19K   1072       21      4288K dentry
 16640  16640 100%    0.12K    520       32      2080K kernfs_node_cache
 15872  15872 100%    0.01K     31      512       124K kmalloc-8
 15616  15616 100%    0.03K    122      128       488K kmalloc-32
 11904  11283  94%    0.06K    186       64       744K anon_vma_chain
 10218   9953  97%    0.59K    786       13      6288K inode_cache
  9867   8354  84%    0.10K    253       39      1012K buffer_head
  9272   9111  98%    0.20K    488       19      1952K vm_area_struct
  6808   6808 100%    0.09K    148       46       592K anon_vma
  5632   5632 100%    0.02K     22      256        88K kmalloc-16
  5145   5145 100%    0.19K    245       21       980K kmalloc-192
  4845   3758  77%    1.05K    651       15     10416K ext4_inode_cache
  4830   3795  78%    0.57K    347       14      2776K radix_tree_node
  4144   3380  81%    0.25K    259       16      1036K filp
  3825   3825 100%    0.05K     45       85       180K ftrace_event_field
  3584   3072  85%    0.03K     28      128       112K ext4_pending_reservation
  2448   2448 100%    0.04K     24      102        96K ext4_extent_status
  2368   1058  44%    0.06K     37       64       148K vmap_area

slabtop on the good server:
 Active / Total Objects (% used)    : 236705 / 264128 (89.6%)
 Active / Total Slabs (% used)      : 7119 / 7119 (100.0%)
 Active / Total Caches (% used)     : 96 / 124 (77.4%)
 Active / Total Size (% used)       : 31422.39K / 36341.06K (86.5%)
 Minimum / Average / Maximum Object : 0.01K / 0.14K / 8.00K

  OBJS ACTIVE  USE OBJ SIZE  SLABS OBJ/SLAB CACHE SIZE NAME
 35551  27123  76%    0.05K    487       73      1948K buffer_head
 30368  19821  65%    0.12K    949       32      3796K dentry
 19872  19872 100%    0.12K    621       32      2484K kmalloc-128
 18666  18666 100%    0.08K    366       51      1464K kernfs_node_cache
 17664  17664 100%    0.02K     69      256       276K kmalloc-16
 16128  16128 100%    0.03K    126      128       504K anon_vma_chain
 15872  15872 100%    0.01K     31      512       124K kmalloc-8
 11136  11136 100%    0.03K     87      128       348K kmalloc-32
 10330  10112  97%    0.38K   1033       10      4132K inode_cache
  9945   9597  96%    0.10K    255       39      1020K vm_area_struct
  7798   4753  60%    0.71K    710       11      5680K ext4_inode_cache
  7008   7008 100%    0.05K     96       73       384K anon_vma
  5859   5668  96%    0.19K    279       21      1116K filp
  5120   2894  56%    0.03K     40      128       160K jbd2_revoke_record_s
  4680   3510  75%    0.30K    360       13      1440K radix_tree_node
  4224   4224 100%    0.03K     33      128       132K vmap_area
  3392   3392 100%    0.06K     53       64       212K kmalloc-64
  3264   3264 100%    0.04K     32      102       128K jbd2_inode
  3145   3145 100%    0.05K     37       85       148K trace_event_file

Noticeably, there's something allocating a lot of kmalloc-64 slabs.
How do I debug what's causing these allocations?
Answer: Read through some of the kernel docs and figured out I could add slub_debug=U to the GRUB Kernel command line to track slab allocations.
After doing this on both servers and rebooting both of them, something that stood out in /sys/kernel/slab/kmalloc-64/alloc_calls on the 'bad' server that wasn't present on the 'good' server at all is this:
  13212 kvm_async_pf_task_wake+0x6e/0x100 age=3329/480991/1729176 pid=0-13255

Searched around and I found a post where someone encountered exactly the same problem: https://darkimmortal.com/debian-10-kernel-slab-memory-leak/. This post documents a workaround of adding no-kvmapf to the Linux command line. Not sure if this will have any other side effects.


How can I add a second submission handler to a form to run after the default handler node_form_submit

Question: I need to generate a node of a dependent content type when creating a node of the primary content type.
I have successfully created the function to be called and attached it to the form and it creates the node as desired, but since the function that generates the secondary node runs first, it cannot be populated with the node id of the primary node, nor with any other data elements from the primary node.
How do I ensure that the submit handlers are executed in the proper order?
<code>   $form['#submit'][] = 'nttac_custom_tta_submit';
   $form['actions']['submit']['#submit'][] .= 'nttac_custom_tta_submit';
   $form['actions']['submit']['#submit'][] = array('node_form_submit','nttac_custom_tta_submit');
</code>
None of the above methods seem to work although they do get the handlers in the proper order
Answer: All the methods you have mentioned should be calling your custom submit handler at the end. However check your $form array when the page is loaded to confirm your custom submit handler is at the right place.
If not try
<code>MYMODULE_entity_insert($entity, $type) {

//Paste your secondary node creation code here

}
</code>
See https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_entity_insert/7 for more details on this hook
Answer: you should user a hook instead :
https://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/hook_node_submit/7


Restrict object's creation

Question: I have a very strange question here. I have Class A, Class B and Class C. I want to create object of Class B which can create the object of A. But how to restrict Class C from creating object of Class A.
One way is define Class A inside Class B, so that Only Class B can create the instance of Class A and Class C cannot access Class A.
But, I think nesting is a wrong way.
Do we have any way to restrict the object creation? Is it possible to use attribute and reflection to help to restrict? If possible is there a recommended way?
Please share your thoughts. Thanks in advance.
Comment: If by design **only** class B can access class A, nesting is a good solution. What you don't like about that?
Comment: in Real Word, purpose of Class A and Class B have two different purposes. When we define A inside B, then B's responsibility is not only to have Class A but also to have original purpose. Is it not violating SRP. Thank you.
Comment: Having class A doesn't mean *maintaining* it. Class A remains autosufficient like before. The *only* thing that changes is A class *accessibility*. That is *architecture* issue and not OOP, imo.
Comment: OK. But, just for accessibility, is it a good way to Defined A inside B. Thank you
Answer: You can place class A and B in a single assembly and mark the constructor of class A as internal.
This way, only classes within that assembly can create instances of class A.
Comment: Thanks for your answer. But All my classes are in the same Assembly.
Comment: There is no other solution possible. In C# there is not such a thing as a C++ 'friend' concept.
Answer: You can make constructor private, and create fabric method that retuns instance of A, and parameter in this method will be instance of B.
<code>public class A
{
  private A (){}

    public static Instance(B BInstance)
    {
       A result = null;
       if (BInstance != null)
       {
         result = new A();
         //create here
       }

       return result;

    }

    }
</code>
Comment: This way a B is NOT creating an instance of A. You are only checking whether or not there is a B.
Comment: Thank you Aexandr and Erno. This is a good solution. But, there are few ways cheat the system. one way is:                                                       Class C
{

 pubic void SomeMethod()
 {
    B b = new B();
    A a=A.Instance(b)
 }
}                                                                                what is your opinion about defining Class A inside Class B. is not violating Singe Responsibility Principle.

Restrict object's creation


How do I ask apt-get to skip all post-install configuration steps?

Question: When installing some packages (like RabbitMQ) on Debian/Ubuntu machines, the post-install step defaults to starting the service.
While this is great in most cases, in some cases (when you already have a service running on the needed port for example) this fails which makes apt-get fail completely.
Is there a way to tell apt-get to skip all those post-install steps so it can still install properly?
Note This is not about questions during install, even in interactive sessions these installs fail because the post-install step fails failure is not handled correctly
Comment: Possible duplicate of [How do I ask apt-get to skip any interactive post-install configuration steps?](https://serverfault.com/questions/227190/how-do-i-ask-apt-get-to-skip-any-interactive-post-install-configuration-steps)
Comment: That question is similar but different. This is about skipping all steps, not just the interactive ones. Some tasks automatically execute scripts which are not always wanted.
Comment: http://serverfault.com/search?q=policy-rc.d
Answer: How do I ask apt-get to skip any interactive post-install configuration steps?
export DEBIAN_FRONTEND=noninteractive
apt-get install -y [packagename]

Edit: yes, that was for skipping (well, automating) post-install questions, true - sorry.
There is no way to skip post-install scripts.  The official Debian policy is to edit the script to return a non-terminal error, or fix the script.  See http://www.debian-administration.org/articles/251
It sounds like you're automating this for many servers, in which case, you'll probably want to make your own version of the package and deploy that.  Another option is to email the package maintainer and discuss whether a failure-to-start should be a terminal error for post-install (I don't believe it is for other packages like apache or lighttpd, for example, but I could be wrong.)
Comment: That's just to skip interactive steps. Those are not the problem here, these are the non-interactive `post-install` steps that fail if something like `service rabbitmq-server start` fails.
Comment: Updated. Does that help?
Comment: it does, better to know that it's not possible than wondering if it is :) Thanks for the help.
Comment: If you're using sudo the export of the environment variable won't work, but you can do it this way: `sudo DEBIAN_FRONTEND=noninteractive apt-get -y [packagename]`
Answer: What you may want to do is something with the policy-rc.d.  See the man page for invoke-rc.d.  You can set a policy so services will not be automatically started.
Comment: That's a way to work around it, but that would be a temporary measure I assume? Atleast... I think that way you would globally disable all service starts.
Answer: this should work
echo exit 101 > /usr/sbin/policy-rc.d
chmod +x /usr/sbin/policy-rc.d
apt-get install <packagename>
rm -f /usr/sbin/policy-rc.d

Comment: A bit hacky and very specific to this case, but it does help a lot! Thanks :)


Table form valueCallback strange behaviour

Question: I am creating a table form using the table element. I do not push sub elements into #rows. I work as with children elements. But valueCallback does not map values right. I am looking inside. It looks very strange. Especially:
return is_array($input) ? array_combine($input, $input) : [];

$input always is array. So combining result is weird with "array" key
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21Table.php/function/Table%3A%3AvalueCallback/8.5.x
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {

  // If #multiple is FALSE, the regular default value of radio buttons is used.
  if (!empty($element['#tableselect']) && !empty($element['#multiple'])) {

    // Contrary to #type 'checkboxes', the default value of checkboxes in a
    // table is built from the array keys (instead of array values) of the
    // #default_value property.
    // @todo D8: Remove this inconsistency.
    if ($input === FALSE) {
      $element += [
        '#default_value' => [],
      ];
      $value = array_keys(array_filter($element['#default_value']));
      return array_combine($value, $value);
    }
    else {
      return is_array($input) ? array_combine($input, $input) : [];
    }
  }
}

For me $input already is ready to move into #value. It does not need altering here.
I hope it works well and I do something wrong. But not it seems weird for. Clarify it, please.
Answer: My solution is below. It is a weird solution because It requires small changes.
Define a table value callback.
    $form['links'] = [
      '#type' => 'table',
      ...
      '#value_callback' => array($this, 'valueCallback'),
    ];

Implement the callback.
 /**
   * {@inheritdoc}
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    return $input;
  }

The data has structure as it has been sent. So the standard data processing works badly for my case.


Can I vectorize this Python code?

Question: I'm kind of new to Python and I have to implement "fast as possible" version of this code.
s="<%dH" % (int(width*height),)
z=struct.unpack(s, contents)

heights = np.zeros((height,width))
for r in range(0,height):
    for c in range(0,width):
        elevation=z[((width)*r)+c]
        if (elevation==65535 or elevation<0 or elevation>20000):
            elevation=0.0

        heights[r][c]=float(elevation)

I've read some of the python vectorization questions... but I don't think it applies to my case. Most of the questions are things like using np.sum instead of for loops. I guess I have two questions:

Is it possible to speed up this code...I think heights[r][c]=float(elevation) is where the bottleneck is. I need to find some Python timing commands to confirm this.
If it possible to speed up this code. What are my options? I have seen some people recommend cython, pypy, weave. I could do this faster in C but this code also need to generate plots so I'd like to stick with Python so I can use matplotlib.
Comment: [RunSnakeRun](http://www.vrplumber.com/programming/runsnakerun/) is an excellent Python profile viewer that shows time usage in a treemap format. Get the profile by turning `dostuff()` into `profile.runctx('dostuff()', globals(), locals(), filename='out.profile')`
Answer: As you mention, the key to writing fast code with numpy involves vectorization, and pushing the work off to fast C-level routines instead of Python loops.  The usual approach seems to improve things by a factor of ten or so relative to your original code:
def faster(elevation, height, width):
    heights = np.array(elevation, dtype=float)
    heights = heights.reshape((height, width))
    heights[(heights < 0) | (heights > 20000)] = 0
    return heights

>>> h,w = 100, 101; z = list(range(h*w))
>>> %timeit orig(z,h,w)
100 loops, best of 3: 9.71 ms per loop
>>> %timeit faster(z,h,w)
1000 loops, best of 3: 641 µs per loop
>>> np.allclose(orig(z,h,w), faster(z,h,w))
True

That ratio seems to hold even for longer z:
>>> h,w = 1000, 10001; z = list(range(h*w))
>>> %timeit orig(z,h,w)
1 loops, best of 3: 9.44 s per loop
>>> %timeit faster(z,h,w)
1 loops, best of 3: 675 ms per loop

Comment: Beat me to it!  You also might want to mention reading the data in using `np.fromstring(contents, dtype=np.uint16)` (or `fromfile` if it's originally in a file) instead of `struct.unpack`.  It's usually significantly faster than unpacking into a tuple using `struct` and then converting to an array for large datasets.

Can I vectorize this Python code?


How can I bypass modules in apache web server?

Question: I have an apache server running on an embedded device that serves webpages to clients using a bunch of custom modules.
I'm now trying to load some webpages from the device using http://127.0.0.1/blah.html
These pages don't need any of the custom modules since they don't use any of their processing.
The problem is apache still runs through all the modules when loading the page and this causes the page to load very slowly even though it's coming from the local filesystem. I've tried using file: to retrieve the pages but then some pages don't display correctly because we are using jquery.
All the pages that don't need the modules are in a specific directory but not in the 'ServerRoot' or 'DocumentRoot'
I've tried putting checks in all the modules to disable the processing but it's still isn't good enough because it still runs through the modules.
Is there a way for me to add another module that makes a check and then stops all the rest from running?
Other ideas?
Answer: Answer found: Someone on the apache forum suggesting running a seperate instance of apache on a different port and only loading the modules needed.  I've tried that approach and it gets me the behavior I want.  Just wanted to update with an answer if anyone else is having the same issue.
Comment: There are much better ways of handling this. Have something like nginx in front of Apache to handle the simple requests, for example.


Qual formato mais apropriado para campo tipo LocalDate em JSON?

Question: Já vi campos de data serem retornados das seguintes formas:
"2012-04-23"   //Padrão ISO 8601
"23/04/2012"   //Pronto para renderização no front-end

Considerando o uso e flexibilidade no front-end, como realizar pesquisa em um grid  com AngularJS, fazer ordenação, etc, qual formato é mais indicado ou existe o formato certo?
Comment: Em qual linguagem está seu backend?
Comment: O backend está em Java, mas me refiro ao uso no front-end.
Answer: Eu acho ideal que o backend envie timestamp para o front e la você se adequa ao necessário.
Ordenação é melhor usar com timestamp pois é apenas um número, no Java será do tipo long ou do wrapper Long.
Exemplo:
var datasOrdenadas = datasEmTimestamp.sort(function(data1, data2) {
  return data1 > data2
});

Ja caso você queria filtrar no front-end você pode usar o poder e flexibilidade do javascript e seus frameworks como o AngularJS que você mesmo comentou, para ajustar seus objetos a suas necessidades.
Exemplo:
Supondo que recebeu timestamp do seu webservice e usando AngularJS.
var datasTratada = datasEmTimestamp.map(function(data) {
  var date = new Date(data);
  var dataTratada = {
    formatada: $filter('date')(data, 'dd/MM/YYYY - HH:mm'),
    dia: date.getDate(),
    mes: date.getMonth(),
    ano: date.getFullYear(),
    hora: date.getHours(),
    minuto: date.getMinutes()
  }
  return dataTratada;
});

Ai de uma lista de datas em timestamp você consegue uma infinidade de informações sobre as datas e pode trabalhar como quiser. No caso do AngularJS, de uma olhada na documentação do filtro de datas dele, é muito completo: https://docs.angularjs.org/api/ng/filter/date.
Uma ótima biblioteca para se trabalhar com datas no javascript também é a moment.js.


Como remover um item específico de uma lista duplamente encadeada?

Question: Dado a classe No, sendo:
public class No<T> {

T elemento;
No<T> proximo;
No<T> anterior;

public No(T elemento, No<T> anterior, No<T> proximo) {
    this.elemento = elemento;
    this.proximo = proximo;
    this.anterior = anterior;
}

public No(T elemento) {
    this.elemento = elemento;
    proximo = null;
    anterior = null;
}

}
Devo criar um método boolean remover(T item), o qual tentei fazer da seguinte maneira:
public boolean remover(T item) {

    if (primeiro.elemento.equals(item)) {
        primeiro = primeiro.proximo;
        primeiro.anterior = null;
        return true;
    }

    No<T> n = primeiro;
    T aux1 = null;
    T aux2 = null;

    while (n.proximo != null) {
        if (item.equals(n.proximo.elemento)) {
            aux1 = n.proximo.elemento;
            n.proximo = n.proximo.proximo;
            break;
        }

        n = n.proximo;
    }

    n = n.proximo;

    while (n.anterior != null) {
        if (item.equals(n.anterior.elemento)) {
            aux2 = n.anterior.elemento;
            n.anterior = n.anterior.anterior;
            break;
        }

        n = n.anterior;
    }

    if (aux1 == aux2) {
        tamanho--;
        return true;
    }

    return false;
}

Desse modo, eu consegui remover quase todos os itens, menos o último antes do null, quando o programa dá erro.
Existe algo a se fazer pra consertar isso ou até mesmo uma construção melhor para esse método?
Comment: O programa também dá `NullPointerException` se você tenta remover um item de uma lista vazia, correto?
Comment: Sim, isso mesmo
Comment: Caso geral para remover um item de uma lista duplamente encadeada: Encontre o nodo a ser excluído e faça o `proximo` do anterior se tornar o `anterior` do próximo. Há dois casos especiais, que são a remoção no início ou no fim da lista. Para remover do início, basta tornar `null`o `anterior` do segundo elemento. Para excluir do fim, basta tornar `null`o `proximo` do penúltimo. Se a lista estiver vazia, o método `remover` pode ser retornado logo no começo.
Comment: Não tenho acesso ao Java no momento, então não posso criar nenhum código para lhe ajudar, nem rodar o seu código. Por isso me limitei a explicar apenas em texto.
Comment: Tudo bem, mas se você puder me mostrar depois, quando estiver disponível, eu ficaria agradecido. Fica difícil para eu visualizar assim.
Comment: Ok, se ninguém tiver respondido até lá, eu te ajudo.
Answer: Tem dois problemas código:
if (primeiro.elemento.equals(item)) {
    primeiro = primeiro.proximo;
    primeiro.anterior = null; //<-- esta linha (1)
    return true;
}
...
while (n.proximo != null) {
    if (item.equals(n.proximo.elemento)) {
        aux1 = n.proximo.elemento;
        n.proximo = n.proximo.proximo;
        break;
    }

    n = n.proximo;
}

n = n.proximo; //<-- e esta linha (2)

while (n.anterior != null) {
    if (item.equals(n.anterior.elemento)) {
        aux2 = n.anterior.elemento;
        n.anterior = n.anterior.anterior;
        break;
    }

    n = n.anterior;
}
...

primeiro.anterior = null; Se o proximo acabou de avançar com primeiro = primeiro.proximo; pode ficar num null o que irá dar NullPointerException quando fizer primeiro.anterior.
Pode resolver este problema facilmente com um teste:
if (primeiro.elemento.equals(item)) {
    primeiro = primeiro.proximo;
    if (primeiro != null){
        primeiro.anterior = null;
    }
    return true;
}

n = n.proximo; Se o while avançou até ao ultimo (aquele em que o proximo é null) então avançar para o próximo vai fazer com que n fique a null, que irá dar NullPointerException na comparação do while que vem logo a seguir:
while (n.anterior != null) {

Esta instrução está mesmo a mais e basta apenas remove-la.

Veja o seu código a funcionar no Ideone com estas duas alterações

Eu pessoalmente transformaria a remoção em algo mais simples:
public boolean remover(T item) {
    if (primeiro == null) return false; //se está vazia não faz nada

    if (primeiro.elemento.equals(item)) {
        primeiro = primeiro.proximo;
        if (primeiro != null){
            primeiro.anterior = null;
        }
        return true;
    }

    No<T> n = primeiro;
    while (n != null && !n.elemento.equals(item)){ //achar o elemento a remover
        n = n.proximo;
    }

    if (n == null) return false; //se não achou sai com false

    n.anterior.proximo = n.proximo; //ajusta o proximo do anterior
    if (n.proximo != null){ //ajusta o anterior do proximo se existir
        n.proximo.anterior = n.anterior;
    }
    tamanho--;
    return true;
}

Veja este exemplo também no Ideone
Note que contemplei adicionalmente o caso da lista já estar vazia no primeiro if, para não dar erro se tentar remover elementos quando já não tem nenhum. Uma das ideias por detrás das listas duplamente ligadas é que é fácil saber o elemento anterior e próximo, logo podemos diretamente navegar até ao elemento a remover e fazer os ajustes somente a partir desse.


pandas dataframe does not recognize columns

Question: So I am trying to add a column to a dataframe and use another column to calculate its value.
import pandas as pd
import pandasql as pdsql
import csv

def filter_by_regular(filename):
    turnstile_data = pd.read_csv(filename)
    turnstile_data = pd.DataFrame(turnstile_data)
    q = "SELECT * FROM turnstile_data WHERE 'DESCn == REGULAR';"
    return turnstile_data

turnstile_regular = filter_by_regular('master_file.txt')
turnstile_regular.head()

turnstile_regular.columns

Index([u'C/A', u' UNIT', u' SCP', u' DATEn', u' TIMEn', u' DESCn',
   u' ENTRIESn', u' EXITSn'],
  dtype='object')

Then when i try to access the ENTRIESn column to use its value to add another column, python does not recognize it.
import pandas

def get_hourly_entries(df):
    df['ENTRIESn_hourly'] = df.ENTRIESn.diff(1)
    df.ENTRIESn_hourly.fillna(1, inplace = True)
    return df

turnstile_hourly = get_hourly_entries(turnstile_regular)
turnstile_hourly.head()

    AttributeError                            Traceback (most recent call last)
<ipython-input-70-890cc0bc29bd> in <module>()
      6     return df
      7
----> 8 turnstile_hourly = get_hourly_entries(turnstile_regular)
      9 turnstile_hourly.head()

<ipython-input-70-890cc0bc29bd> in get_hourly_entries(df)
      2
      3 def get_hourly_entries(df):
----> 4     df['ENTRIESn_hourly'] = df.ENTRIESn.diff(1)
      5     df.ENTRIESn_hourly.fillna(1, inplace = True)
      6     return df

/Users/flmlopes/anaconda3/envs/py2/lib/python2.7/site-packages/pandas/core/generic.pyc in __getattr__(self, name)
   3079             if name in self._info_axis:
   3080                 return self[name]
-> 3081             return object.__getattribute__(self, name)
   3082
   3083     def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'ENTRIESn'

So anyone know how can i solve this?
Answer: This is your index:
Index([u'C/A', u' UNIT', u' SCP', u' DATEn', u' TIMEn', u' DESCn',
   u' ENTRIESn', u' EXITSn'],
  dtype='object')

Note the leading space:
 u' ENTRIESn'

Therefore, change:
df['ENTRIESn_hourly'] = df.ENTRIESn.diff(1)

to:
df['ENTRIESn_hourly'] = df[u' ENTRIESn'].diff(1)

Alternatively, fix you columns first:
turnstile_regular.columns = [x.strip() for x in turnstile_regular.columns]

Comment: Great that it helped. BTW, you can [accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) an answer if it solves your problem. Just click on the check mark (✓) next to the answer. This will toggle it from grayed-out to filled-in.


How to programatically get the machine name of a node?

Question: I want to add the machine name of every node into my page.tpl.php template as a class on the body.  How can I get that machine name?
I have access to the node object, but I don't see the machine name in there.  Is there an api call I can use?
Comment: what do you mean by machine name?
Comment: after reading Chapabu's answer I see that I meant 'path alias'
Answer: Individual nodes don't have machine names in Drupal 6 or 7, so you can't.
If you want a unique ID for each node, and Node ID won't do (for staging or migration for example), then try taking a look at the UUID module.

This module provides an API for adding universally unique identifiers
  (UUID) to Drupal objects, most notably entities.

Also of note...

Features in Drupal 6 version

Automatic UUID generation for nodes, node revisions, users and taxonomy.
Allow choosing which content types and vocabularies to create UUIDs for.
Option to batch create UUIDs for objects which don't have it already.

If, however, you want the node TYPE to be available to your template, and you have the node object, it should be under $node->type, or possibly $vars['type'].
Comment: ah right, thanks for that reminder.  I guess I'm more interested in the url path alias than the node itself.  And I see that I can just get the path with `$node->path`. Thanks.


Why is returning a structure implementing a bound trait not allowed?

Question: Can u please explain the rust error for this?
pub trait OrderEvent {}

#[derive(Debug)]
pub struct OrderCreatedEvent {
    pub order_id: String,
}

impl OrderEvent for OrderCreatedEvent {}

pub fn handle_create<E: OrderEvent>(_state: OrderState, command: CreateOrderCommand) -> Vec<E> {
    let events = vec![OrderCreatedEvent {
        order_id: command.order_id,
    }];

    events
}

Rust tells me:
mismatched types [E0308] expected type parameter `E`,
found struct `OrderCreatedEvent` Note: expected struct `Vec<E>` found struct `Vec<OrderCreatedEvent>`

Event implements the OrderEvent trait and it is trait bound. Why is this not allowed?
Answer: Use this signature instead:
pub fn handle_create(_state: OrderState, command: CreateOrderCommand) -> Vec<impl OrderEvent>

In Rust, using impl Trait as a type has opposite meanings in a parameter type vs in the return type. See https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits
Comment: thanks! what is the type signature for the returned Vec? `let events: Vec = handle_create(...)` is wrong of course. this is not correct either `let events: Vec`
Comment: You need not specify the type.
Comment: @kind_robot Because the bracket generic parameter is meant to be **variadic** over the **input**. "Variadic" meaning for different input types, the compiler must generate different function bodies (and for the same input type, the compiler must generate the same function body). Here your return value is not variadic over the input. What you want is an **opaque** return type. Opaque means the signature only tells part of the type information but the actual type is **determined** somewhere else. And the bracket generic parameter does not have this meaning.


Explain how to obtain a field with $125$ elements using polynomials by using concrete example provided by a polynomial

Question: I have this problem and don't know how to solve it. Any one can help? Thanks
Comment: Welcome to Math SE! When you have a minute, please take the [tour of the site](http://math.stackexchange.com/tour)
and look at [how to format mathematics](http://math.stackexchange.com/help/notation) on Math SE. Furthermore, I suggest
that you bookmark this [very useful MathJax link](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference)
for quick reference. Cheers!
Answer: Take $\mathbb{F}_5[x] / (p(x))$ where $p(x)$ is an irreducible polynomial of degree $3$.
One example of an irreducible polynomial of degree $3$ in $\mathbb{F}_5[x]$ is $x^3 + x + 1$. We can check this by evaluating that $x^3 + x + 1$ has no roots.
$\mathbb{F}_5 [x]$ is a PID since $\mathbb{F}_5$ is a field. Since $p(x)$ is irreducible in $\mathbb{F}_5[x]$, $\mathbb{F}_5[x] / (p(x))$ is a field.
Comment: A quadratic polynomial with degree $3$?
Comment: Sorry bad mistake.
Comment: This is the canonical answer for constructing finite fields of a given order.


Alternative proof that the half space $H^n$ is not homeomorphic to $\mathbb{R}^n$

Question: The proofs of this post are perfectly fine, but I thought there was another proof using the "invariance of domain" theorem. I tried to write it explicitly but it does not seem to work.
By absurd, let $\ \varphi:H^n \longrightarrow \mathbb{R}^n$ be a homeomorphism and let's call $\psi: \mathbb{R}^n \longrightarrow H^n$ its inverse which does go from an open subset of $\mathbb{R}^n$ to $\mathbb{R}^n$ and is injective as required by the invariance of domain theorem.
The problem now is that  $\psi$ is not continuous as a map $\mathbb{R}^n \longrightarrow \mathbb{R}^n $ but only as a map $\mathbb{R}^n \longrightarrow H^n$ (recall: $\psi^{-1}(open)$ should be open, but $U$ open in $H^n$ means $\exists\ V\subset\mathbb{R}^n$ open such that $U = H^n\cap V$, i.e. $U$ is not necessarily open in $\mathbb{R}^n$).
If $\psi$ were continuous then one would obtain a contradiction: by the theorem $\psi(\mathbb{R}^n)=H^n$ should be open in $\mathbb{R}^n$ but it is not, so there cannot be an homeomorphism $H^n\cong \mathbb{R}^n$.
Question: can one use the invariance of domain to proove that there is no homeomorphism between $H^n$ and $\mathbb{R}^n$?
Comment: I think it is ok and it works as an alternative proof. If $\psi$ is continuous as a map to $H^n$ and the inclusion $H^n\subseteq \Bbb R^n$ is obviously continuous, then the composition is continuous too.
Comment: Yeah you're right!! if we use the $\forall \epsilon$... characterization of continuity it will work.... Oh and your edit is even better!!! I validate your answer!!
Comment: The continuity issue is a good remark but since you have injectivity I don't see what could stop you from using this method...
Answer: If $Z$ is a subspace of the topological space $X\subset Y$, then $f: X \rightarrow Z$ is continuous if and only if the extended domain $f:X \rightarrow Y$ is continuous. The reason is that, if $U \subset Z$ is open in $Z$, then $U=V\cap Z$ for some $V$ that is open in $Y$, meaning $f^{-1}(U)=f^{-1}(V \cap Z)=f^{-1}(V)$ (because $f(X)\subset Z$),  so continuity of the "extension" implies continuity of the original function, and if $V$ is open in $Y$, $f^{-1}(V)=f^{-1}(V\cap Z)$ with $V\cap Z$ being open in $Z$, hence continuity of the original function implies continuity of the extension.
So yes, your argument is perfectly valid.


Find IP address of docker container running inside docker swarm

Question: I have a docker swarm cluster.  I deployed a Elassandra docker image.  Now I want to find that the docker container's IP address for the seed node:
The following are my services in my swarm:

docker service ls
ID                  NAME
  yjehoql7l976       elassandra_seed

I want find the IP address of the container for the Elassandra_seed node by its name to be used in my other docker compose file.  Is that possible?
Answer: It is possible to find the IP address but remember it will change everytime the service restarts. Let's move onto finding the ip address.

Find the node where your service is running. Run docker service ps stack_service. Check the node column to find the name of the node.
Go to the previously found worker node. Run docker network ls. Usually the stack's network is named like this $stackname_network. Grab the ID of the network.
Describe the network by running docker inspect $networkid. This will show you the service name and its assigned IP address.
Answer: Following @rodrigo-loza idea, what I did is:

Use docker service ps for getting the list of services running on my swarm. Add a filter condition to have only those that are actually running (--filter desired-state=running) and format the output to only show service ID and Name
For each service returned by previous command, do a docker inspect and get the address that you need. This, can be done using format (check documentation about Format command and log output for more details regarding the syntax). It is something like --format '{{range $conf := (index (index .NetworksAttachments))}}{{.Addresses}}{{end}}'

Putting all together in bash should look like:
docker service ps --filter desired-state=running \
        --format "{{.ID}} {{.Name}}" \
        elassandra_seed | while read id name ignore
        do
            echo "$name $(docker inspect \
                      --format '{{range $conf := (index (index .NetworksAttachments))}}{{.Addresses}}{{end}}' \
                      $id)"
        done
elassandra_seed.1 [10.0.0.137/24][172.16.238.181/24]
elassandra_seed.2 [10.0.0.141/24][172.16.238.189/24]
elassandra_seed.3 [10.0.0.147/24][172.16.238.196/24]
elassandra_seed.4 [10.0.0.138/24][172.16.238.183/24]
elassandra_seed.5 [10.0.0.148/24][172.16.238.197/24]
elassandra_seed.6 [10.0.0.139/24][172.16.238.185/24]
elassandra_seed.7 [10.0.0.149/24][172.16.238.198/24]
elassandra_seed.8 [10.0.0.150/24][172.16.238.199/24]


Show that $0<e-\sum\limits_{k=0}^n\frac{1}{k!}<\frac{1}{n!n}$.

Question:
Show that $0<e-\sum\limits_{k=0}^n\frac{1}{k!}<\frac{1}{n!n}$, where $n>0$.
Hint:
Show that $y_m:=\sum\limits_{k=n+1}^{m+n}\frac{1}{k!}$ has the limit $\lim\limits_{m\to\infty}y_m=e-\sum\limits_{k=0}^n\frac{1}{k!}$ and use that $(m+n)!y_m<\sum\limits_{k=1}^m\frac{1}{(n+1)^{k-1}}$.

If I use the hint then the problem is pretty straightforward.
However, I don't understand why the inequality of the hint is true? A simple example shows that the inequality doesn't hold! There are no further information given.
What am I missing? Is $(m+n)!y_m<\sum\limits_{k=1}^m\frac{1}{(n+1)^{k-1}}$ simply nonsense? Or did the professor forgot to mention some more assumptions?
Comment: The formula should be something like $(n+1)!y_m<\sum_{k=1}^m\frac 1{(n+1)^{k-1}}$, otherwise as you already observed that it was wrong.
Comment: Well, do you know that $e=\sum_{k=0}^{\infty}\frac{1}{k!}$?
Comment: @richrow, yes sure, but this doesn't help.
Comment: There is typo in the hint.
Comment: @Pythagoras, I don't see the typo. Where?
Comment: Answered at least [here](https://math.stackexchange.com/questions/1044424/deduce-that-e-is-irrational-from-the-following-inequality) (actually there's [a lot of duplicates](https://approach0.xyz/search/?q=OR%20content%3A%240%3Ce-%5Csum%5Climits_%7Bk%3D0%7D%5En%5Cfrac%7B1%7D%7Bk!%7D%3C%5Cfrac%7B1%7D%7Bn!n%7D%24&p=1)).
Answer: The hint was not correctly stated. The estimate suggested in my comment is a possible fix for the typo, but the following estimate is even better: $$(n+1)!y_m\leq \sum_{k=1}^m \frac 1{(n+2)^{k-1}}.$$ Taking limit on both sides as $m\rightarrow \infty $ yields $$(n+1)!\left(e-\sum_{k=0}^n\frac 1 {k!}\right)\leq \frac 1{1-\frac 1{n+2}}=\frac {n+2}{n+1}$$
$$\Rightarrow e-\sum_{k=0}^n\frac 1{k!}\leq\frac {n+2}{(n+1)^2}\cdot \frac 1{n!}<\frac 1{n\cdot n!}.$$ Since the left inequality trivially holds, this proves the result.
Answer: We have
$$\begin{align}y_m &= \frac{1}{n!}\left[ \frac{1}{n+1} + \frac{1}{(n+1)(n+2)} + \ldots + \frac{1}{(n+1)(n+2)\cdots(n+m)}\right] \\&<\frac{1}{n!}\left[ \frac{1}{n+1} + \frac{1}{(n+1)^2} + \ldots + \frac{1}{(n+1)^m}\right]\end{align}$$
Evaluating the finite geometric sum on the RHS it follows that
$$y_m  < \frac{1}{n!}\frac{\frac{1}{n+1}- \frac{1}{(n+1)^{m+1}}}{1 - \frac{1}{n+1}}= \frac{1}{nn!}\left[1 - \frac{1}{(n+1)^{m}} \right] \underset{ m \to \infty}\longrightarrow
 \frac{1}{nn!}  $$
Answer: I think I got it right this time!!
$e-\sum_{0}^{n}\frac{1}{k!}<\frac{1}{n!n}$ is equivalent to $ e-e+\sum_{n+1}^{\infty }\frac{1}{k!}<\frac{1}{n!n}$ equivalent to
$\frac{1}{(n+1)!}+\frac{1}{(n+1)!(n+2)}+.....<\frac{1}{n!n}$ and hence to
$\frac{1}{n!}({\frac{1}{(n+1)}}+\frac{1}{(n+1)(n+2)}+\frac{1}{(n+1)(n+2)(n+3)}+....)<\frac{1}{n!n}$
But $\frac{1}{(n+1)(n+2)}<\frac{1}{(n+1)^{2}}, \,\,\frac{1}{(n+1)(n+2)(n+3)}<\frac{1}{(n+1)^{3}} $ etc so it suffices to show setting $a=\frac{1}{n+1}$
that $a+a^{2}+a^{3}+....\leq \frac{1}{n}\Leftrightarrow \frac{1}{1-a}-1\leq \frac{1}{n}$. But this is
$\frac{n+1}{n}-1=\frac{1}{n}$.   QED
Comment: If you learn MathJax, you might get some upvotes.


How to access ItemsHost from a ItemsControl derived custom control

Question: I'm creating a custom control called FooControl derived from ItemsControl have a default style defined for the same in themes\generic.xaml.
The default style for FooControl sets ItemsPanel property to another custom panel called FooPanel as below (I don't think the usage of custom panel matters for this question).
<Setter Property="ItemsPanel">
  <Setter.Value>
    <ItemsPanelTemplate>
      <local:FooPanel IsItemsHost="True"/>
    </ItemsPanelTemplate>
  </Setter.Value>
</Setter>

In the code behind of my FooControl, I want to get access to the instance of FooPanel that was created automatically. By looking in reflector I found that ItemsControl does have a property called ItemsHost but I cannot access it from FooControl as ItemsHost is internal
Can someone plz suggest me a reliable way to get reference to the instance of FooPanel?
Answer: Unfortunately the ItemsControl control does not expose a reference to this element (in fact, I have a feeling that the ItemsControl doesn't even have access to it itself).
Using VisualTreeHelper.GetChild() as suggested in the previous post may work, but it will break if the items control is restyled in such a way that causes the items panel do be nested further down.
A slightly less fragile (but still far from perfect) way to get a reference is to get the parent of the container for the first child (assuming that your items control contains at least one item)
Panel itemsPanel;

if (Items.Count == 0)
{
   itemsPanel = null;
}
else
{
   var firstContainer = ItemContainerGenerator.ContainerFromIndex(0);

   itemsPanel = VisualTreeHelper.GetParent(firstContainer) as Panel;
}

Note: This only works in SL3 and above since it uses the ItemContainerGenerator property (I believe a work around may exist for SL2)
Answer: You can use VisualTreeHelper.GetChild(...).  I believe it should be the first or second child of your ItemsControl subclass.


Keyboard covers an EditText in a BottomSheetDialog

Question: I have a common problem with the keyboard just a little overlapping EditText. I searched, but all the answers hadn't worked.
I have android:windowSoftInputMode="adjustResize|stateAlwaysHidden"in AndroidManifest.
Here's my layout:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <import type="ru.express.app.domain.model.SendCheckBy" />

        <variable
            name="vm"
            type="ru.express.app.presentation.orders.dialogs.PayAllBottomSheetVM" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_pop_up_rounded_19"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        tools:context=".android.presentation.main.geo_location_dialog.GeoLocationDialogFragment"
        tools:ignore="SpUsage">

        <View
            android:layout_width="30dp"
            android:layout_height="3dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/bg_paging_indicator" />

        <TextView
            android:id="@+id/titleTextView"
            style="@style/Theme.ExpressRu.Text.Bold.21sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:lineSpacingExtra="10dp"
            android:text="@{vm.titleText}"
            tools:text="Оплатить все" />

        <TextView
            android:id="@+id/selectPaymentTitleTextView"
            style="@style/Theme.ExpressRu.Text.Bold.16sp"
            android:layout_marginTop="25dp"
            android:layout_marginHorizontal="13dp"
            android:layout_width="match_parent"
            android:gravity="start"
            android:text="@string/order_finished_select_payment_option" />

        <TextView
            android:id="@+id/cashPaymentTextView"
            style="@style/Theme.ExpressRu.Text.Regular.17sp"
            android:layout_width="match_parent"
            android:layout_marginHorizontal="13dp"
            android:layout_marginTop="20dp"
            android:text="@string/order_finished_cash"
            android:textColor="@drawable/bg_text_pressed"
            app:drawableEndCompat="@drawable/ic_round_red" />

        <TextView
            android:id="@+id/cardPaymentTextView"
            style="@style/Theme.ExpressRu.Text.Regular.17sp"
            android:layout_width="match_parent"
            android:layout_marginHorizontal="13dp"
            android:layout_marginTop="15dp"
            android:text="@string/order_finished_card"
            android:textColor="@drawable/bg_text_pressed"
            app:drawableEndCompat="@drawable/ic_round_gray" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="20dp"
            android:background="@color/gray_white_line" />

        <TextView
            android:id="@+id/chooseNumberOfChecksTitleTextView"
            style="@style/Theme.ExpressRu.Text.Bold.16sp"
            android:layout_marginTop="20dp"
            android:layout_marginHorizontal="13dp"
            android:layout_width="match_parent"
            android:gravity="start"
            android:text="@string/order_finished_select_number_of_checks" />

        <TextView
            android:id="@+id/oneCheckTextView"
            style="@style/Theme.ExpressRu.Text.Regular.17sp"
            android:layout_width="match_parent"
            android:layout_marginHorizontal="13dp"
            android:layout_marginTop="20dp"
            android:text="@string/order_finished_one_check"
            android:textColor="@drawable/bg_text_pressed"
            app:drawableEndCompat="@drawable/ic_round_red" />

        <TextView
            android:id="@+id/numberOfInvoicesTextView"
            style="@style/Theme.ExpressRu.Text.Regular.17sp"
            android:layout_width="match_parent"
            android:layout_marginHorizontal="13dp"
            android:layout_marginTop="15dp"
            android:text="@string/order_finished_by_number_of_invoices"
            android:textColor="@drawable/bg_text_pressed"
            app:drawableEndCompat="@drawable/ic_round_gray" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="20dp"
            android:background="@color/gray_white_line" />

        <TextView
            android:id="@+id/sendChecksTitleTextView"
            style="@style/Theme.ExpressRu.Text.Bold.16sp"
            android:layout_marginTop="20dp"
            android:layout_marginHorizontal="13dp"
            android:layout_width="match_parent"
            android:gravity="start"
            android:text="@string/order_finished_select_send_request" />

        <TextView
            android:id="@+id/phoneTextView"
            style="@style/Theme.ExpressRu.Text.Regular.17sp"
            android:layout_width="match_parent"
            android:layout_marginHorizontal="13dp"
            android:layout_marginTop="20dp"
            android:text="@string/order_finished_send_by_phone"
            android:textColor="@drawable/bg_text_pressed"
            app:drawableEndCompat="@drawable/ic_round_gray" />

        <EditText
            android:id="@+id/phoneEditText"
            style="@style/Theme.ExpressRu.TextInputEditText.Default"
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_marginHorizontal="13dp"
            android:padding="10dp"
            android:inputType="phone"
            android:maxLength="12"
            android:onTextChanged="@{(text, start, before, count) -> vm.updateInput(text)}"
            android:text="@{vm.inputRequestText}"
            android:hint="@string/order_finished_send_by_phone_hint"
            android:backgroundTint="@color/white_blue_background"
            app:visible="@{vm.selectedSendBy == SendCheckBy.PHONE ? true : false}" />

        <TextView
            android:id="@+id/mailTextView"
            style="@style/Theme.ExpressRu.Text.Regular.17sp"
            android:layout_width="match_parent"
            android:layout_marginHorizontal="13dp"
            android:layout_marginTop="15dp"
            android:text="@string/order_finished_send_by_email"
            android:textColor="@drawable/bg_text_pressed"
            app:drawableEndCompat="@drawable/ic_round_gray" />

        <EditText
            android:id="@+id/mailEditText"
            style="@style/Theme.ExpressRu.TextInputEditText.Default"
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_marginHorizontal="13dp"
            android:padding="10dp"
            android:inputType="textEmailAddress"
            android:onTextChanged="@{(text, start, before, count) -> vm.updateInput(text)}"
            android:text="@{vm.inputRequestText}"
            android:hint="@string/order_finished_send_by_email_hint"
            android:backgroundTint="@color/white_blue_background"
            app:visible="@{vm.selectedSendBy == SendCheckBy.EMAIL ? true : false}" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="25dp"
            android:background="@color/gray_white_line" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/confirmButton"
            style="@style/Theme.ExpressRu.Button.Permission"
            android:layout_width="match_parent"
            android:layout_marginStart="13dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="13dp"
            android:backgroundTint="@color/red_main"
            android:text="@string/confirm"
            android:textColor="@color/white" />

        <Space
            android:id="@+id/space"
            android:layout_width="wrap_content"
            android:layout_height="23dp" />

    </LinearLayout>
</layout>

I tested recommendations from:
Keyboard hides BottomSheetDialogFragment
Bottom Sheet Fragment comes up with keyboard
Answer: This one solved my problem. I've put it into OnViewCreated method in the BottomSheetDialogFragment.
    binding.root.addGlobalLayoutListener(true) {
        val keyboardHeight =
            dialog?.window?.decorView?.rootWindowInsets?.getInsets(
                WindowInsetsCompat.Type.ime()
            )?.bottom
        binding.root.setPadding(0, 0, 0, keyboardHeight ?: 0)
    }


NestJS Monorepo Microservices Deployment to Heroku with Shared Files

Question: Currently, I am building a monorepo microservices with an apigateway using Redis to communicate.  I am planning to deploy each services into different dynos in Heroku. Currently, it's pretty straightforward since every service has its own package.json, tsconfig.json, and Procfile. I could deploy them using git subtree The problem occurs when I want to have a shared files. For example I want to share the same DTOs across microservices to minimize bug and error. Does anyone have any idea on how to approach this?
Current File Structure:
Project
|    .git
|
|____Apigateway
|    |  Procfile
|    |  package.json
|
|____Microservice 1
|    |  Procfile
|    |  package.json
|
|____Microservice 2
|    |  Profile
|    |  package.json

Desired File Structure:
Project
|    .git
|    create.dto.ts
|    delete.dto.ts
|
|____Apigateway
|    |  Procfile
|    |  package.json
|
|____Microservice 1
|    |  Procfile
|    |  package.json
|
|____Microservice 2
|    |  Profile
|    |  package.json

Answer: A possible way to do this is to create a Nest.js library and then add the library as a dependency to the individual services in their respective package.json files using a local path.


energy function of surfaces

Question: The arc length of a curve $g(t)$ on a Riemannian manifold $M$ is
\begin{align}
L(g)=\int_a^b <\dot{g},\dot{g}>^{\frac 1 2}dt
\end{align}
Its variational problem is equivalent to that of the energy function
\begin{align}
E(g)=\int_a^b <\dot{g},\dot{g}>dt
\end{align}
For surface on $M$, can we have a similar conclusion?
The variational problem of area is equivalent to that of the energy of the surface?
Answer: I am not sure if there is a corresponding energy functional, but there does exist generalized length functional.
For any submanifold $N$ in $(M,g),$ we can define the area functional $A(N)$ as we know. This is a natural generalization of your $L,$ since the critical points of $L$ are geodesics, and the critical points of $A$ are minimal submanifolds.
By the way, when calculating the derivative of $A,$ we use $\sqrt{\det g}$ in its local expression, where $g$ is the Riemannian metric. Maybe we can take the square root out to get an energy (not sure if this quantity has been investigated).

energy function of surfaces


Django smart_selects chained field coming up empty

Question: I am trying to implement chained dropdown list using Django smart_selects. But its not working as expected. After selecting the first dropdown, the second dropdown is not populating.
model.py:
<code>class ServerInfo(models.Model):
    server = models.CharField(max_length=255, verbose_name='Server')
    def __str__(self):
       return self.server

class Database(models.Model):
    database = models.CharField(max_length=255, verbose_name='Database')
    server = models.ForeignKey(ServerInfo,related_name='database', on_delete=models.CASCADE)
    def __str__(self):
       return self.database

class Tables(models.Model):
    table = models.CharField(max_length=255, verbose_name='Table')
    database = models.ForeignKey(Database,related_name='table', on_delete=models.CASCADE)
    def __str__(self):
       return self.table

class TransferJob(models.Model):
    source_server = models.ForeignKey(ServerInfo, related_name='source_server', verbose_name='Source Server', on_delete=models.CASCADE)
    source_db = ChainedForeignKey(
        Database,
        chained_field="source_server",
        chained_model_field="server",
        show_all=False,
        auto_choose=True,
        sort=True)
source_table = models.ForeignKey(Tables, related_name='source_table',on_delete=models.CASCADE, verbose_name='Source Table')

    dest_server = models.ForeignKey(ServerInfo, related_name='destination_server', verbose_name='Destination Server', on_delete=models.CASCADE)
    dest_db = models.ForeignKey(Database, related_name='destination_database', verbose_name='Destination Database', on_delete=models.CASCADE)

</code>
Update:
I just realised that this is working as expected in the django admin. But how do I use this outside the admin?
Answer: Was able to solve this by adding these scripts in template:
<code>    <script type="text/javascript" src="{% static '/smart-selects/admin/js/chainedfk.js' %}"></script>
    <script type="text/javascript" src="{% static '/smart-selects/admin/js/bindfields.js' %}"></script>
</code>
There was no mention about these in docs.


Why can't this conditional with generic keyof be eagerly resolved?

Question: function broken<T>(value: T) {
  type Test = keyof T extends keyof T ? 1 : 0
}

Why can't Test in the example above be eagerly resolved to 1? There's something I don't understand about the compiler that should answer this question for me.
Interestingly enough, there is a workaround:
function workaround<T>(value: T) {
  type Test = [keyof T] extends [keyof T] ? 1 : 0
}

Playground here
Answer: The answer to the question probably involves a bug fix introduced in TypeScript 3.3.  The description of the new way of evaluating conditional types is:

For a conditional type T extends U ? X : Y, the algorithm we use to determine whether to defer resolution of a conditional type is:

we resolve to Y when T is not assignable to U considering all type parameters referenced in T and U related (i.e. T is definitely not assignable to U),
otherwise we resolve to X when T is assignable to U considering all type parameters referenced in T and U unrelated (i.e. T is definitely assignable to U),
otherwise we defer resolution.

So it looks like keyof T extends keyof T ? 1 : 0 is deferred, because keyof T1 is not assignable to keyof T2 when T1 and T2 are unrelated types.  This deferral isn't exactly wrong, but it does seem like a limitation.
Indeed if I try your code in TS 3.2.1 it is eagerly resolved, so this was a change introduced for TS3.3.
Curiously my reading of that bug fix's algorithm description looks like it should apply to your workaround also.  I'm not sure why [keyof T] extends [keyof T] would change things, since [keyof T1] isn't definitely assignable to [keyof T2], if T1 and T2 are unrelated.  So some mystery still remains here.  Haven't been able to crack it yet, so I'll probably just give up now.  Oh well!

It also looks like someone reported this exact issue where X extends X ? 1 : 0 is not eagerly resolved (and they also noticed that wrapping in a one-tuple "fixes" it), and the issue was tagged as a bug, so maybe it will be addressed?

Okay, hope that helps; good luck!


How would i redirect a user after registration to his subdomain site

Question: I want to redirect a user after successful registration maindomain.com to open his site in a new tab.
His site url will look like this newuser.maindomain.com. (username.maindomain)
Any Suggestions?
Comment: How are you setting up/creating the subdomains?
Comment: @PatrickKenny: i am using domain rules module to create sub domains. using rules i create new domains, when user registers i am creating like user-name.domain.com.
Answer: Use Rules again to do the redirect.
On Event Drupal is initializing or User has logged in add a condition if you need that (eg check that user role is not anoymous) and then add an Action of Redirect. In there use some php for the redirect path using the global variable $user (you may need some formation also if the username has spaces).
Notice that you must run this Rule after the rule that creates the subdomain. Or you can simply add the redirect action on the same domain_rules rule after the component that creates the subdomain.
You must also have a common user database along sites (domain and subdomain). The Bakery module is a good option here.
Comment: A walkthrough of how RULES implement it can be found at http://drupalize.me/videos/login-redirects, where it also deals with the small bug in the redirect.
Answer: i feel you have to use SSO for authentication among multiple domains, check this bakery module!


How can I theme the Views Exposed Filter Results page?

Question: I'm trying to theme this page
http://188.8.131.52:50000/admin/rooms/bookings/list/kiosk/3?name=rest

which is the result of an exposed views filter. I have themed the node the views block is on but I'm not sure how to theme this page since it's not using node/NID.
Thanks.
Answer: You can use the devils module to get theme help. Also there is the <?php print $classes; ?> function that i have for instance on my div  with the class container div. And there I see for example the class page-searchresults. Giving away that I can use page--searchresults.tpl.php as a template file for theming that page.


compare top half matrix with bottom half

Question: If I have the following data:
mat1           <- matrix( c(0,2,3,1,0,1,1,1,1), nrow=3 )
rownames(mat1) <- LETTERS[1:3]
colnames(mat1) <- LETTERS[1:3]

mat1
#  A B C
#A 0 1 1
#B 2 0 1
#C 3 1 1

how do get to a data.frame where I know which rowise matrix entries are bigger than the column entries? E.g. I would want for as a result for mat1 is some sort of data.frame indicating:
A < B
A < C
B > A
B = C
C > A
C = B

I have been messing around with  upper.tri etc using subtractions but this is not helping.
Answer: To compare each element mat1[i,j]
with the corresponding element on the other side of the diagonal mat1[j,i],
you can use the transpose t.
r <- sign( mat1 - t(mat1) )
r
#   A  B  C
# A 0 -1 -1
# B 1  0  0
# C 1  0  0

paste(
  colnames(r)[col(r)],
  ifelse(r>0, ">", ifelse(r<0, "<", "=")),
  rownames(r)[row(r)]
)
# [1] "A = A" "A > B" "A > C" "B < A" "B = B" "B = C" "C < A" "C = B" "C = C"

Comment: Nice: Subject, of course, to the usual caveats about using "==" with numeric results. I would suggest this as the initial step if this were being applied to `double` rather than `integer` results: `sign(zapsmall(mat1 -t(mat1) ) )`


Setting an Image to a label in Qt

Question: I need to get a label image into a Qt Widget
I have tried putting a label on the widget then just on its properties on the right under text i selected pixmap and selected the image I wanted to use.
However when I run the program no image appears any ideas why this is?
I have also tried :
QLabel label("");
label.show();

But had no luck do not think I was using it right
Comment: You can also do it modifying the style of the `QLabel` using `CSS` syntax.
Answer: Official way of "adding image to QLabel" provided by Nokia Corp. A QPixmap is used.
QLabel label;
QPixmap pixmap(":/path/to/your/image.jpg");
label.setPixmap(pixmap);
label.setMask(pixmap.mask());

label.show();

Comment: @AngryDuck Add your test code to the question.  The above code is correct, but the poster's example image dir is located in a resource file - have you just copied that and your image is not in a resource file?
Comment: @AngryDuck If you do it in the designer, does the designer at least show the image? If the application doesn't show it, then it might just be that the application cannot find the image (maybe it's a relative file path?). The easiest way to circumvent this (and pack all images with the application) is to use Qt's resource mechanism to include it directly with the application binary. This is the recommended aproach especially for such UI related images that always need to be present anyway (and usually don't span much memory either).
Comment: Yea it does show the image in the designer but not when the application is compiled, i have selected the image just from the drop down menu under pixmap in label properties. and the image is located im my project directory?
Comment: @AngryDuck *"the image is located im my project directory"* - But can the application find it there? Is it given as a relative path in the final setting code (that generated by the designer) and if yes, does this relative path match the working directory of the application when it is actually run (which doesn't neccessarily need to match the project directory exactly)? Seriously, I think this is just some directory mismatch. Like said, the easiest way would be to put this whole image into a Qt resource.
Comment: sorted it was a directory issue
Answer: QIcon searchIcon = Icon::fromTheme("edit-find");
searchLabel->setPixmap( searchIcon.pixmap(16,16) );

Answer: Another way is to use Qt Style Sheets.
QLabel label;
label.setStyleSheet("background-image: url(:/images/assets/your.png)")

Setting an Image to a label in Qt


Why does creating training sets with replacement lead to better performance?

Question: Pasting generally suffers from lower performance than bagging, because it training sets without replacement. Why does creating training sets with replacement lead to better performance?
Answer: According to this Powerpoint from The University of Waikato:

The idealized version of bagging improves performance because it eliminates the variance component of the error.

In this case, they are assuming Total Error = Bias + Variance.


Why I am getting different $R^2$ from R LM and manual calculation?

Question: I did a toy experiment with linear regression, but getting different results for $R^2$, could any one help me?
library(ggplot2)
fit=lm(price~carat+depth+table+x+y+z-1,data=diamonds)
summary(fit)
sse=crossprod(diamonds$price-fit$fitted.values)
sst=crossprod(diamonds$price-mean(diamonds$price))
1-sse/sst

Answer: I found the problem.
I am not adding the intercept in the model and without intercept SST is calculated differently, where it should be
crossprod(diamonds$price)
but not
crossprod(diamonds$price-mean(diamonds$price))
I think most books are confusing that just give us the formula to SST as
$$
\|y-\bar y\|^2
$$
but not mention such formula only holds for model with intercept term.
Comment: You're correct but I believe this effectively duplicates a number of questions already on site.


What is the best way to track an "app opened" metric for Android?

Question: I'm integrating with Mixpanel, which lets you track arbitrary events for your app.  I want to track how often the Android app is opened.
Is there a central place where I can put code that runs every time the app is opened, regardless of activity?
Answer: You can track if it was started from the launcher by tracking Intent parameters in your Main Activity.
Or if you want to track how many times this screen was started, u could put your tracking code inside onStart of your activity.
Comment: If an app has multiple activities, using onStart won't work. Also using intent parameters won't work if app is sent to background with home key and then resumed (intent is the same).


Why does $form_state['input'] not correspond to the html of the form upon submittal?

Question: I have a drupal form with repeatable fields allowing to type in as many phone numbers as one wants. Simplifying to the extreme, the code looks like this:
function form_test($form, $form_state) {
    $form = array(
        '#attributes' => array(
            'enctype' => 'multipart/form-data'
        ),
        'phoneNumbers' => array(
            '#type' => 'fieldset',
            '#attributes' => array(
                'class' => array('repeatable_container')
            ),
            '#tree' => true,
            0 => array(
                '#type' => 'fieldset',
                '#attributes' => array(
                    'class' => array('repeatable')
                ),
                'phoneNumber' => array(
                    '#type' => 'textfield'
                )
            )
        ),
        'enregistrer' => array(
            '#type' => 'submit',
            '#value' => t('Enregistrer'),
            '#submit' => array('form_test_enregistrer')
        )
    );

    return $form;
}

function form_test_validate($form, &$form_state) {
    drupal_set_message("
" . print_r($form_state['input']['phoneNumbers'], true) . "
", 'warning');
}

function form_test_enregistrer($form, &$form_state) {
    // Whatever
}

As you can see, the validation function simply prints out the phone numbers that are submitted through the form.
The classes 'repeatable_container' and 'repeatable' are used by the following javascript (simplified here) to allow the addition or reletion of phone numbers:
jQuery.noConflict();
(function ($) {
    "use strict";

    var getNextIdentifier, repeatButton, deleteButton;

    getNextIdentifier = function getNextIdentifier(original) {
        var re_lastNumber = /\d+(?!.*\d+)/, // One or more digits NOT followed by anything (.*) plus a number (\d+ : one or more digits). Basically, the last number of the string.
            m,
            newIdentifier = original;
        m = original.match(re_lastNumber);
        if (m !== null) {
            newIdentifier = original.replace(re_lastNumber, parseInt(m[0], 10) + 1);
        }
        return newIdentifier;
    };

    $.prototype.hasAttr = function (attrName) {
        var attr = $(this).attr(attrName);
        return (typeof(attr) !== 'undefined' && attr !== false);
    }

    $.prototype.increaseID = function (element) {
        return element.attr('id', getNextIdentifier(element.attr('id')));
    }

    $.prototype.increaseName = function (element) {
        return element.attr('name', getNextIdentifier(element.attr('name')));
    }

    $.prototype.increaseClassNames = function (element) {
        var classes = element.attr('class').split(' '),
            nbClasses = classes.length,
            i;
        for (i = 0; i < nbClasses; i += 1) {
            classes[i] = getNextIdentifier(classes[i]);
        }
        return element.attr('class', classes.join(' '));
    }

    $.prototype.increaseIdentifiers = function () {
        var attrs = [
                { attrName: 'id',           func: $(this).increaseID},
                { attrName: 'name',     func: $(this).increaseName },
                { attrName: 'class',    func: $(this).increaseClassNames }
            ],
            nbAttrs = attrs.length,
            i;
        for (i = 0; i < nbAttrs; i += 1) {
            if (this.hasAttr(attrs[i].attrName)) {
                attrs[i].func(this);
            }
            $('[' + attrs[i].attrName + ']', this).each(function () {
                attrs[i].func($(this));
            });
        }
        return this;
    };

    repeatButton = function (eventObject) {
        eventObject.preventDefault();
        var parent = $(this).prev(),
            element = $('.repeatable', parent).last(),
            clone = element.clone(true, true).increaseIdentifiers(true).appendTo(parent);
    };

    deleteButton = function (eventObject) {
        eventObject.preventDefault();
        $(this).closest('.repeatable').remove();
    };

    $('document').ready(function () {
        $('.repeatable_container').append($('+'));
        $('.repeatable').append($('x'));
        $('a.repeater').click(repeatButton);
        $('a.deleter').click(deleteButton);
    });
}(jQuery));

The problem arises when I create multiple phone numbers and I erase the very first one. For example, if I duplicate the phone number field and erase the first one, I would expect the validation function to obtain one phone number with id of 1 (the id 0 having been erased). But that's not what happens. In fact, what I get are two phone number, the first with id 1 (expected) and the second with id 0 (which should have been erased), in that order.
If I inspect the dom, I see that the javascript part works perfectly, so I'm pretty sure drupal is doing something here that I don't understand. Any clues on what's happening here?
Comment: As side note, you add form elements to the `$form` argument the form builder receives; you don't initialize `$form` to an new array.
Comment: @Shawn I recommend you install the devel module (drupal.org/project/devel) as it will let you quickly print out variables, arrays, objects, etc., via the `dpm()` function; for example, `dpm($form_state)` in your validate or submit handler will give you a navigable $form_state array that you can browse in the messages area on the next page view. Very helpful!
Comment: I realise my question may be hard to understand, so please do ask questions...
Comment: @Shawn regarding your question – if you alter a form by adding elements that don't exist on the server-side, those elements will not be usable to you when processing the form. This is a validation / security thing. If you want to add elements / alter a form, check out the #ajax property in the form API http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#ajax. If this is indeed the issue, I can post an answer that includes some sample code to duplicate a form element via an "add another" button.
Comment: Even if you remove the form item using Javascript the actual value is still stored in the form state.
Comment: @MohammedShameem Why is that the case? How does that work?
Comment: @CharlieS The elements I add with javascript are accessible through $form_state['input'] (but not through $form_state['values'])
Comment: @kiamlaluno Could you post a link to an example?
Comment: You see that in every form builder, such as [path_admin_form()](http://api.drupal.org/api/drupal/modules%21path%21path.admin.inc/function/path_admin_form/7).
Answer: Generally, you can let Drupal handle more of this. I don't think I've ever had a need to touch $form_state['input'], since $form_state['values'] always holds user submitted data that is considered "acceptable" – e.g. if a user added an option to a <select> via javascript and then picked it, I don't want that value!
Of course, that means that you yourself can't add options or entirely new form elements via javascript, right? The solution lies in altering the $form on the server side and letting Drupal return the new markup the user via ajax. This markup could be the entire form, rewritten to have new or different elements based on some user activity, or it could just be a new piece of the form. This is done via the #ajax property on any form element (textfield, checkbox, button, etc.).
Consider this code:
function my_form($form, &$form_state) {
  // Wrap our form in a div with an ID that we know – this way we can
  // replace the entire form with a new version that has modified fields.
  $form['#prefix'] = '
';
  $form['#suffix'] = '
';

  $form['phone_numbers'] = array(
    '#type' => 'fieldset',
    '#title' => 'Phone Numbers',
  );

  // Use a custom property in the $form_state to track how many numbers we should allow.
  if (!isset($form_state['#num_phone_numbers'])) {
    $form_state['#num_phone_numbers'] = 1;
  }

  // Add as many phone number fields as the user has requested.
  for ($i=1;$i<=$form_state['#num_phone_numbers'];$i++) {
    $form['phone_numbers'][$i] = array(
      '#type' => 'textfield',
      '#title' => 'Phone Number ' . $i,
    );
  }

  // Button that has an #ajax callback which will be run whenever the user clicks it.
  $form['phone_numbers']['add'] = array(
    '#type' => 'button',
    '#value' => 'Add Another Phone Number',
    '#ajax' => array(
      'callback' => 'my_form_ajax_addPhoneNumber',
      'wrapper' => 'my_form_wrapper',
    ),
  );
}

function my_form_ajax_addPhoneNumber(&$form, &$form_state) {
  // All we need to do in this ajax callback is increase the number of
  // phone numbers that we want the form to include. By calling
  // drupal_rebuild_form() the original form builder will run again,
  // see the new count, and add an additional phone number field.
  $form_state['#num_phone_numbers']++;
  return drupal_rebuild_form('my_form', $form_state, $form);
}

function my_form_submit(&$form, &$form_state) {
  foreach ($form_state['values']['phone_numbers'] as $key => $phone_number) {
    drupal_set_message("Phone Number #{$key}: {$phone_number}");
  }
}

Note that not a single line of javascript is needed. Post any questions!
Comment: Won't my_form_rebuild_form have undesirable side effects? It seems like it would erase the data the user had entered (but not yet saved), remove validation error messages and highlights, mess up the other repeatable fields in the form, mess up which tab is currently selected (part of my form uses tabs built with javascript), etc.
Comment: How would I go about doing something similar without reloading the entire page?
Comment: Because we also pass $form_state to the rebuild function, it prefills all form fields with whatever the user entered. It does not remove validation messages because they will be included at the top of the form. It just reloads the form (via ajax) the rest of the page is untouched.
Comment: Let me also add that, if you were to have other ajax functions run to duplicate other fields, as long as you used $form_state to handle some storage of what needs doing, your original form builder will always know how to act.
Comment: OK, well the real form is actually quite huge with all sorts of different things happening in it, so it will take me a while to try this out... I will report back when I run into problems or when I get everything working without javascript. In the meantime, I'm still slightly confused about how Drupal's validate function can get fields that are no longer in the html (my original question)... Care to elaborate?
Comment: A possible guess is because they existed in the $form_state and Drupal didn't receive different $_POST data to tell it otherwise. It may sound crazy, but it's not a good idea to use javascript to touch the actual `$_POST['key']=>value` data that will be sent back to Drupal; if it doesn't get what it knows it last sent out, it's not going to behave as expected.
Comment: Let me rephrase this to see if I understand: Drupal is keeping track of the original form somewhere in the session in order to avoid being victim of an altered form, correct?
Comment: Actually, I don't think any of the form data is stored in $_SESSION (which is stored in the `sessions` MySQL table) but rather exists in the `cache_form` table. Drupal uses the `form_build_id` and `form_token` to track what it has cached of the form along with what your browser submits.
Comment: Interesting. This explanation seems to make sense, but it doesn't hold true in another case where the form element duplication happens slightly differently (through the use of tabs). In other words, when I use the tabbed method, there is no problem, even though the same behaviour which you describe occurs and should produce a similar problem...


Proving inequality: For $0<\varepsilon<1 \in \mathbb{R},$ and $0<x\in\mathbb{R}$, then $x^n-n\varepsilon x^{n-1}\leq (x-\varepsilon)^n$.

Question: This inequality is in a proof to show that for any positive real number, y , there exists a positive real number, x, such that $y^n = x$.
So, he says that you can deduce $x^n-n\varepsilon x^{n-1}\leq (x-\varepsilon)^n$ from the following inequality: $1-n\varepsilon\leq (1-\varepsilon)^n$. Also note that the professor sets $\varepsilon = \frac{1}{nNx^{n-1}}$. Can someone list the steps for proving this inequality from the other? I already proved the one that we are using to deduce the new one.
Comment: Multiply both sides by $x^n$ and use $\varepsilon'=\varepsilon x$.
Comment: No, you'll need to change your $\varepsilon$, but that doesn't really matter since $\varepsilon$ is universally quantified.
Comment: Are the conditions in the theorem correct?  In particular, should $x$ be greater than $1$?  Otherwise $(x-\varepsilon)$ could be negative...
Comment: So after you multiple both sides by $x^n$, then do you just have to show that $x^n (1-\varepsilon)^n \leq (x-\varepsilon )^n$ by expanding out each one using the binomial theorem?
Answer: Observation:
There may be an error with the conditions.  When $x=\frac{1}{4}$, $\varepsilon$ is close to $1$, and $n=3$, then the LHS is close to $-\frac{11}{64}$ while the RHS is close to $-\frac{27}{64}$.  This issue does not arise when $x>1$.
Sketch (when $x>1$):
Starting with
$$
1-n\varepsilon\leq(1-\varepsilon)^n,
$$
multiply both sides by $x^n$ to get
$$
x^n-n\varepsilon x^n\leq (x-\varepsilon x)^n.
$$
Now, replace $\varepsilon x$ by $\varepsilon'$ to get
$$
x^n-n\varepsilon'x^{n-1}\leq (x-\varepsilon')^n.
$$
Now you need to do is to check the conditions of the theorem, but you're helped by the fact that $\varepsilon$ in the original inequality can vary and $\varepsilon'>\varepsilon$ (by the condition on $x$).
Comment: Thank you so much for the sketch. I figured out my lapse in judgement now, and can finish the proof. :)
Comment: Clearly you're good at analysis. As a student, how should I approach learning this subject? For me, I struggle with the inequalities a lot! The theory almost always makes sense to me, but the technical inequalities really are hell for me. As I'm going through the text and I see steps in inequalities that I don't quite understand, how should I approach that? Should I just take the step in the proof for granted and continue on? I'm afraid this is a bad approach because it must be an important skill to proficiently handle inequalities.


How many instances of rails can I run on one server?

Question: I'm a developer trying to determine the best way to approach a rails app. I need to provide some options for deployment and am trying to minimize both development costs and long term hosting costs.
Consider a site that allows multiple authors to collaborate on a single "work", whether that be a novel, technical manual or even source code. "Users" are allowed to "check out" chapters, make modifications and merge changes back in. Users rate each others modifications with higher ratings allowing access to more and more content.Let's call it "social version control"
While it's possible to write a single rails app that can discriminate between "works", it may be easier from a coding perspective for each work to be hosted as it's own rails app. A "master" app would create/control each works server instance, allow browsing works in search of authors, and be a reporting hub for the site owner. For now, assume the simplest of schemes where each work uses a port specific url:
Master - http://collabowork.it
My Novel - http://collabowork.it:3001
Some Manual - http://collabowork.it:3002
Another work - http://collabowork.it:3003

My question is: how many instances of rails will I be able to run on a single box before it is overwhelmed? Is there a way to determine how many instances can run on a commodity server? Is there another option that would allow scaling out additional works inexpensively (perhaps on EC2 or something similar). Because this isn't being designed as a money making venture, what's the least expensive way to deploy this (short of additional coding effort). I would like to have some sort of cost analysis before I offer alternatives but I don't know how to begin sizing something like this.
Any ideas or thoughts are appreciated.
Answer: I have something very similar to what you have.
We have one application located in one directory. This application is set up for mutliple different clients. The only difference between each client is the database that they use. When we set it up, I decided that having a different port for each client was not very pretty therefore we took a different approach.
I know Apache is not very popular in the Rails scene but it is still a great piece of software. We used Apache's name-based virtualhost to tackle this problem along with Passenger3. In the new version, you can define a RailsEnv per virtualhost using the same application directory. The directive is called "PassengerAppGroupName".
You get a virtualhost looking something like this :
<VirtualHost _default_:80>
        ServerName preprod.xxxx.fr
        DocumentRoot /var/xxxx/current/public/
        TimeOut 5000

        # Passenger directives
        PassengerHighPerformance on
        PassengerAppGroupName "preprod"
        RailsEnv preprod
        # Logging
        ErrorLog /var/log/apache2/preprod-error.log
        CustomLog /var/log/apache2/preprod-access.log combined

</VirtualHost>

I think this example should clear things up. This solution allows you to separate your applications using the HTTP Host used to connect to it.
To sum up, if the "works" have the same code base all you can use this IMHO clean solution.
EDIT : In terms of performance, Apache/Passenger needs 512MB of RAM. I found that anything below that will lead to bad performance but YMMV. I don't believe it requires more RAM if you add more virtual host.


Attaining the expected value over iterations

Question: Is my implementation of the expected value correct? I conclude that the average number of iteration of this algorithm will be 1/3, but seems completely wrong.
Rand() outputs either 0 or 1 (uniform)
Algorithm:
Call Rand() 5 times and translate the results as a binary number between 0 and 31. If this number is < 30 it is returned as the result, otherwise it is discarded and Rand() is called 5 more times.Repeat this process until a binary number between 0 and 29 is found. What is the expected running time of this algorithm in terms of iterations of calling Rand()?
My approach:
Random Variable X = number of iterations
Probability
P(X = 5) = 30/32
P(X = 10) = 2/32 * 30/32
P(X = 15) = 2/32 * 2/32 * 30/32
P(X = 5n) = (2/32)^n * 30/32

Expected Value $$ E[x] = x * p(x) =  5n * (2/32)^n * 30/32 $$
$$ 150/32*\sum\limits_{n=0}^\infty n (2/32)^n = 1/3 $$
Comment: $P(X=5n) = (2/32)^{n\color{red}{-1}} * (30/32)$ so $E(X)=(150/32)\sum\limits_{n=1}^\infty n~(2/32)^{n\color{red}{-1}}= \color{red}{16}/3$
Answer: The probability that you obtain a number that is either $30$ or $31$ will be $p = 2/32$, since each of the integers $\{0, \ldots, 31\}$ have an equal probability of occurring.  Therefore, the random number $N$ of sets of 5 draws that are required until a number in $\{0, \ldots, 29\}$ is obtained is a geometric random variable, with probability mass function $$\Pr[N = k] = p^{k-1} (1-p), \quad k = 1, 2, \ldots.$$  The expected value of $N$ is $$\operatorname{E}[N] = \sum_{k=1}^\infty k \Pr[N = k] = \sum_{k=1}^\infty k p^{k-1} (1-p) = \frac{1}{1-p} = \frac{16}{15}.$$  The expected number of Rand() calls is five times this number, or $16/3$.


Android Maps control

Question: If I use the Intent to fire up default maps app on emulator/device, how can I gain control on the clicks/touch on the map.
e.g. I want to find all Pizza places in my location. It is damn easy if I use  geo URI.
                String uri = "geo:"+ 39.967571 + "," + -75.532123 + "?q=pizza";
                startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
Once the map is started I loose control, I want to be able to extract some values based on where the user has clicked.
If I extend MapActivity I am not getting what I desire i.e. list of business based on a parameter.
Intent myIntent = new Intent(YYYYY.this, XXXX.class);
                YYYYY.this.startActivity(myIntent);
This will fire up XXXX activity which extends MapActivity. How do I get the list of Pizza places based on current location. I tried using geocoder with parameters but results weren't reliable at all.
Answer:
If I use the Intent to fire up default maps app on emulator/device, how can I gain control on the clicks/touch on the map

You don't. That's not your app.

If I extend MapActivity I am not getting what I desire i.e. list of business based on a parameter.

You're going to have to write the "list of business based on a parameter" logic yourself, with some third-party source of "list of business" data.

Android Maps control


Converting a pandas dataframe column values from string to float

Question: I have a data frame with columns A, B, and C as follows:
df:
A    B    C

1990 1  1,00,000
2000 2  2,00,000
2001 3  3,00,000

I want to convert the column C from object to float. For that, I run the following command:
df["C"].astype(float)
It gives the error:
ValueError: could not convert string to float: '1,00,000'

How do I convert the column to float or int?
Comment: Its just me or the commas placed on the wrong place..
Answer: Try this:
df["C"].str.replace(",", "").astype(float)