Bulk deletion of node which are unpublished and which are in between the given dates

Question: I unpublished all the nodes which are created between January 1, 2012 and January 1, 2015. Then, I published 800 nodes.
Now I want to delete all the nodes I first unpublished. I have almost 28,000 nodes. How do I bulk delete them?
I also want to delete the images uploaded along with the nodes.
Answer: I suggest to use the Rules module for that.
Bulk deletion is explained in one of the first tutorial video's in this series:
https://drupalize.me/videos/introduction-rules?p=1157
What you should do:

enable Rules and Rules UI
create a new Component of type "action set"
actions to add: a loop thru the nodes and if the date matches, the node should be deleted

The Views Rules module might be usefull to use views to send the nid's of the nodes to delete to the rules component.
I learned a lot about this via the accepted answer to "How to pass arguments to a view from Rules?".
An alternative could be the Views bulk operations (VBO) module, which has a "bulk deletion" out of the box. You add a VBO-field in your view and select the "delete" as the action. Maybe this is even better than the Rules (in the way that it will take less time to understand and implement).
Comment: I created a dummy text variable to send to the Component, then when I click the "execute-link" in the Components-list, some character has to be entered in this field, and the actions can get executed by clicking the button "execute".
Comment: I am building some new functonality to my site using rules and views_rules, but the bulk delete is not yet ready. However I do use a Component of type Action Set with a dummy text variable to develop and test the view loop. I think it will be confusing if I add the export of my work in progress that does something else than deleting.
Answer: Using the VBO module (together with the "bulk deletion" out of the box) as suggested in the prior answer is indeed the way to go, BUT ... if you add the Rules module to that also, then the possibilities for what you can do are virtually only limited by your own imagination. Read on for more details ...
Step 1: Create a Rules Component
Have a look at this example of a Rules Component (in Rules export format):
{ "rules_delete_nodes_containing_a_specific_value_in_a_text_field" : {
    "LABEL" : "Delete nodes containing a specific value in a text field",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "node" : { "label" : "Node", "type" : "node" },
      "text_field_value" : { "label" : "Text Field Value", "type" : "text" }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_free_format_text" } },
      { "data_is" : {
          "data" : [ "node:field-free-format-text" ],
          "value" : "[text-field-value:value]"
        }
      }
    ],
    "DO" : [
      { "entity_delete" : { "data" : [ "node" ] } },
      { "drupal_message" : { "message" : "Deleted node with id \u0022[node:nid]\u0022 and title \u0022[node:title]\u0022 (which contained the string \u0022[text-field-value:value]\u0022 in field \u0022field_free_format_text\u0022)." } }
    ]
  }
}

Some more details to further explain the above above example:

This Rules Component will be used in the subsequent step.
In my case I have a text field with machine name field_free_format_text. Even though the question here does not mention anything about some extra requirements for the nodes to be deleted, you may want to consider adding some relevant additional requirements, as further explained in what follows.

Note that I also added the Rules Condition Entity has field to make that field available for further processing in this Rules Component. That's also what is mentioned in the Rules UI when you're adding a "data comparison" condition: somewhere it says "The data selector helps you drill down into the data available to Rules. To make entity fields appear in the data selector, you may have to use the condition 'entity has field' (or 'content is of type')". For a video tutorial that illustrates the importance of this Entity has field condition, refer to Data types and data selection, especially what is shown between about 13:30 and 17:30 in that video.
Note that I've not added a Rules condition here about selected content types, that's possibly an extra condition you might want to add also.
If you want, just copy the exported rule above, change all occurrences of the machine name of that field to match the machine name of your field, and then you can simply import this Rules Component in your own environment.
After you imported this Rules Component, you can navigate to admin/config/workflow/rules/components in your own site, and then use the hyperlink "execute" to qa-test it in your own site. After you use that link, use the button that says Switch to direct input mode. Then you'll be able to enter some Node identifier and hit the Execute mode. Depending on the node id you selected (if it has that field, and if the Rules Condition is satisfied), your node will be deleted.
After you're confident your rule works perfect for all variations of your nodes, move on to the next step below.
Here is an example of the (2) Drupal messages I got when executing this Rules component for a node with ID = 48, in which I used the string "GetRidOfThisNode":

Deleted node with id "48" and title "Demo field_free_format_text replacement" (which contained the string "GetRidOfThisNode" in field "field_free_format_text").
Component rule: Delete nodes containing a specific value in a text field has been executed.

Variation of the display of that message could be that you use Rules to create a (similar) log record ...
Attention: you always get the 2nd message. But the first one only shows up if you have an actual match ... and hence the delete gets performed also.
Step 2: Let VBO do the bulk of the work
This step is pretty easy ... Just create an appropriate view of all your nodes to be processed, and transform it into a Views Bulk Operations view. The clue to what you want VBO to do as an operation is ... you guessed it: Execute the Rules Component from Step 1.
If you're not familiar with how to do so, have a look at the video about Using Rules components with VBO". If you want even more, then also watch the subsequent "Using Rules components with more than one parameter" video.
Of course there are multiple variations of the above approach, like processing by component type, narrowing down your Views output to only "a" specific value, etc. But that should be straight forward, and up to your own imagination.
Attention: to process many nodes (such as "almost 28,000 nodes" ...) at once is quite a challenge (memory requirements, etc). Therefor you might want to also watch the video about "More details about VBO settings" ... Before you are tempted to use VBO for processing them all at once ...
Happy VBO-ing while Rules-ing!
Remark: Your additional question like "I also want to delete the images uploaded along with the nodes." is really a different question. A possible answer to that is what is suggested in the accepted answer to "Find orphaned unused files and images".


Erro ao registrar usuario

Question: Quando tento criar um segundo usuário, com ASP.NET MVC, me apresenta um erro:

Violation of PRIMARY KEY constraint 'PK_dbo.AspNetUsers'. Cannot insert duplicate key in object 'dbo.AspNetUsers'. The duplicate key value is (00000000-0000-0000-0000-000000000000).
  The statement has been terminated.

Meu controller está assim:
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new Usuario { Id = new Guid(), UserName = model.UserName, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

e minha ViewModel:
 public class RegisterViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Usuário")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

O que estou fazendo de errado ?
Answer: Tente trocar Id = new Guid() porId = Guid.NewGuid(). O new Guid vai criar uma chave nova com zeros, e como você já deve ter um usuário cadastrado acontece a exception de chave primária duplicada.
Veja funcionando no dotnet fiddle.

Erro ao registrar usuario


how can i play small music file when i tuch an image in android?

Question: I am New to android, My task is to playing a small audio file when we touch an image?
Comment: Looking at your other questions, you really should be a good community member and vote on good answers, and pick one that works for you. That lets other people who come along know what worked.
Answer: Depending on your target API version there are a different ways of doing this.
Assuming you have something like this inside your layout xml file.
<ImageView
    android:id="@+id/ImageThatWillPlaySound"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    androidL:onClick="playSound">
</ImageView>

Note androidL:onClick="playSound" which will run the function in your activity that matches the definition:
public void playSound(View v) {
}

Inside that function, you would use MediaPlayer as xandy suggested.
Something like this
public void playSound(View v) {
    MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file);
    mp.start();
}

Where inside your assets folder you had a file called sound_file.
Answer: Use MediaPlayer to play.


How to use Task.WhenAll to run 2 calculations at once

Question: I'm trying to learn how to use async tasks and am really struggling just to get a simple example working. I provide a very simplified version of what I'm trying to achieve below.
public void Calculate()
{
    int a1 = 1;
    int a2 = 2;
    int b1 = 3;
    int b2 = 4;
    int c3 = 5;
    int c4 = 6;
    int c5 = 7;
    int c6 = 8;
    int c7 = 9;

    // Do next 2 lines in parallel
    int rank1 = Evaluate(a1, a2, c3, c4, c5, c6, c7);
    int rank2 = Evaluate(b1, b2, c3, c4, c5, c6, c7);

    // wait for above 2 lines and use values to get result
    int result = EvaluateOutcome(rank1, rank2);
}

public static int Evaluate(int i1, int i2, int i3, int i4, int i5, int i6, int i7)
{
    // do something complicated and return value - simulate here with random number
    Random rand = new Random();
    return rand.Next(0,10);
}

public static int EvaluateOutcome(int rank1, int rank2)
{
    return rank1 * rank2;
}

Calculating rank1 and rank2 is a long process in my real code but the calculations are independent of each other. I'd like to try to run both calculations at once to hopefully half the processing time. I need to await both calculations completing prior to calculating result.
I think I should be able to do something like:
private async Task Evaluate(int a1, int a2, int b1, int b2, int c3, int c4, int c5, int c6, int c7)
{
    Task task1 = Evaluate(a1, a2, c3, c4, c5, c6, c7);
    Task task2 = Evaluate(b1, b2, c3, c4, c5, c6, c7);

    await Task.WhenAll(task1, task2);
}

But this does not compile and I'm unsure how to get the results from the Task and put them into my rank1 and rank2 int variables. I think this should be incredibly simple but I can't find a clear example to follow. Some help would be appreciated.
Comment: use `Task.Run()` ... Add `static Task EvaluateAsync(int i1, ...) => Task.Run((i1, ..). => Evaluate(i1,...);` then `Task task1 = EvaluateAsync(a1, ...);` and then after `await Task.WhenAll` you can get `task1.Result` as `rank1` similar stuff with `task2`
Comment: That sounds good Selvin. I'd really appreciate if you could put the full code in an answer. Many thanks
Comment: Or better: a list of parameters sets, and pass it to `Parallel.ForEach`
Comment: could you show me how please charlie
Comment: @Selvin it would be great if you could help with the detail of the code you suggested. I think thats the answer but I'm unable to figure it out myself TIA
Answer: You can use the Task.Run method to offload each calculation to a ThreadPool thread, and the Task.WaitAll method in order to block the current thread until the two calculations are complete:
Task<int> task1 = Task.Run(() => Evaluate(a1, a2, c3, c4, c5, c6, c7));
Task<int> task2 = Task.Run(() => Evaluate(b1, b2, c3, c4, c5, c6, c7));

Task.WaitAll(task1, task2);

int rank1 = task1.Result;
int rank2 = task2.Result;

A more efficient alternative is to use the Parallel.Invoke method. The core difference is that the current thread will participate in the work too, by doing one of the two calculations. So only one additional thread (from the ThreadPool) will be used:
int rank1 = default;
int rank2 = default;

var parallelOptions = new ParallelOptions()
{
    MaxDegreeOfParallelism = Environment.ProcessorCount
};

Parallel.Invoke(parallelOptions,
    () => rank1 = Evaluate(a1, a2, c3, c4, c5, c6, c7),
    () => rank2 = Evaluate(b1, b2, c3, c4, c5, c6, c7)
);

The documentation of the Parallel.Invoke is a bit confusing, by stating that it executes each of the provided actions, possibly in parallel. Don't worry. The only case that the calculations will not happen in parallel is if the ThreadPool is saturated. In which case the first approach (Task.Run+Task.WaitAll) will fail to parallelize the calculations too, while contributing even more to the saturation of the pool. If you want to take the ThreadPool out of the equation, you could pass a custom TaskScheduler with the ParallelOptions, that executes the tasks on a dedicated thread per task, like the one found here. Or use the TaskCreationOptions.LongRunning flag.
Comment: Thanks Theodor. With the second approach int result = EvaluateOutcome(rank1, rank2); fails to compile when placed after the Parallell Invoke due to unassigned rank1,rank2. Is it OK just to initialise each to zero where you declared them?
Comment: Strangely, both approaches makes my code run about 15x slower. Any thoughts why that might be?
Comment: @SteveW yeap, the variables must be initialized for the `Parallel.Invoke` version to compile. It is safe to initialize them in this example, but it certainly makes the code less robust. Future modifications could introduce bugs more easily. I don't know how it can be robustified in this aspect honestly.
Comment: @SteveW as for the parallel version being slower, it's probably because the overall work is too lightweight. Check [this](https://stackoverflow.com/questions/6036120/parallel-foreach-slower-than-foreach "Parallel.ForEach Slower than ForEach") out for more details.


React Button with linkto functionality add option

Question: I have a React Button component which currently has a prop where you add a page to link for.
This works fine but I want to add a new prop called 'type' so the button can either link to another page (like it does now),
or execute an onclick event ... for example execute a function when clicked.
Here's the current code:
import React, { Component } from 'react';
import { Link } from 'react-router-dom';

class Button extends Component {
  render() {
    return (

        <Link to={this.props.linkto}>{this.props.text}</Link>

    );
  }
}

export default Button;

Usage:
<Button linkto={'about'} text={'my button'} />

This button currently will only navigate to another page using the react-router.
I'd like to have to option to add a 'Type' property so that I can either have the button Navigate like it is now or 'onclick' so I can execute a function.
How can I do this?
Comment: Do you also need to pass the function with type prop?
Comment: yes, if possible?
Answer: You can pass 'type' prop with a function or link. Then you can check with ternary operator whether the passed prop was the function, and invoke it if true. Just make sure that you bind your function in the component where you are including the Button component.
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import isfunction from 'lodash.isfunction';

    class Button extends Component {
      render() {
        return (

            {isfunction(this.props.type) ? <button onClick={() => this.props.type()} /> : <Link to={this.props.linkto}>{this.props.text}</Link>}

        );
      }
    }

    export default Button;

Will render Link component:
<Button linkto={'about'} text={'my button'} type={link}/>

Will render button with on click funtion:
<Button linkto={'about'} text={'my button'} type={someFuntion} />

Comment: I was thinking of using 'type' just to pass the 'type' for example: type={link} or type={click} and depending on the type use linkto or onclick ..does it make sense?
Comment: For example for click type I would do:
Comment: Or if type - link ...
Comment: Yes it can be done either by checking whether the passed prop is function type, or adding a new prop to let the component know which type it is.
Comment: I have updated my answer you will need to add Lodash isfunction module. https://www.npmjs.com/package/lodash.isfunction


Prove basis for product topology is collection of all sets with finitely many factors restricted to open sets

Question: Prove a basis for the product topology on $\prod_{a \in \lambda}X_{a}$ is the collection of all sets of the form $\prod_{a \in \lambda}U_a$ where $U_a$ is open in $X_a$ for each $a$ and $U_a=X_a$ for all but finitely many $a$. Given that the product topology is defined as the topology generated by the subbasis $S=\bigcup\limits_{B \in \lambda}S_B$. Where $S_B=\{\pi_B^{-1}(U_B)|U_B \ \ \text{open in}\ \ X_B \}$
Attempt:Let $S=\bigcup\limits_{B \in Y}S_B$ be a subbasis for the product topology, where $S_B=
\{ \pi^{-1}_{B}(U_B)|U_{B} \ \ \text{open in}\ \ X_B \}$. Then each of the factors of elements of $S$ are of the form $U_a=X_a$ for all $a \neq B$ and $U_B$ for $B \in Y$.So only one factor is not necessarily equal to $X_a$. Then a basis element is a finite intersection of subbasis elements $\pi^{-1}_i(U_i)$ where $i=i_1,...,i_k$. So $U_a=X_a$ for all $i\neq i_1,...,i_k$.
Comment: It seems like the answer to that uestion depends highly on how you define the product topology. Some books even define it as the topology generated by the basis that you wrote
Answer: Yes, that's essentially the idea: more succinctly:
(standard fact about bases and subbases): if $\mathcal{S}$ is a subbase, then the set of finite intersection of elements from $\mathcal{S}$ form a base.
A finite intersection from $\mathcal{S}$ looks like $\pi_{a_1}^{-1}[U_1] \cap \pi_{a_2}^{-1}[U_2] \ldots \cap \pi_{a_n}^{-1}[U_n]$, where $a_1, \ldots a_n$ form a finite subset of $\lambda$ (all distinct, because we can use $p_a^{-1}[U] \cap \pi_a^{-1}[V]= \pi_a^{-1}[U \cap V]$ to take sets with the same projection together, a minor point that you omitted) and this is just the set $$\prod_{a \in \lambda} O_a$$ where $O_a = U_i$ if $a = a_i$ for some $i \in \{1,\ldots n\}$ (the finite exception set) and $O_a = X_a$ otherwise. So these form exactly the base you had to prove to be a base: it's just the standard base from the subbase.


Show $f$ is Lebesgue-Integrable

Question: Let $f$ be a function defined over $[0,1]$, $$f(x) = \begin{cases}1 , & x\in \Bbb R-\Bbb Q \\ 0,  & x\in \Bbb Q\end{cases}$$
Show that $f$ is Lebesgue-Integrable but not Riemann-Integrable.
I know that $f$ is Lebesgue-Integrable iff $|f|$ is Lebesgue-Integrable. But not sure how this can be proved by the function. I'm also stuck on how to prove it is not Riemann-Integrable. Could someone help to provide a complete explanation please? Thanks.
Answer: Observe that $\mathbb{R} \setminus \mathbb{Q}$ is measurable because $\mathbb{Q}$, being a countable union of singletons, is measurable. Hence
$$ f = \chi_{\mathbb{R} \setminus \mathbb{Q}} + 0\chi_{\mathbb{Q}}
$$
$f$ is simple (it is in fact only a characteristic function), and thus integrable.
$f$ is not Riemann integrable on $[0,1]$ because its discontinuity set has positive Lebesgue measure. This can be proved by noticing the fact that a rational number and an irrational number exist inbetween every two real numbers.
Answer: $f=\chi_{(\mathbb R\setminus\mathbb Q)\cap[0,1]}$ and so is Lebesgue integrable by definition, with integral $$\int f\ \mathsf d m = m(\mathbb R\setminus\mathbb Q)\cap[0,1])=m([0,1])=1. $$
$f$ is not Riemann integrable, because any subinterval of $[0,1]$ contains both rational and irrational numbers, so the lower sum of any partition would be $0$ and the upper sum $1$.

Show $f$ is Lebesgue-Integrable


Como colocar variável PHP com valor menor e maior (min e max)

Question: Olá, pessoal!
Gostaria de saber se há a possibilidade de fazer uma variável em PHP com um numeral min e max, por exemplo:
$var = /um numeral maior ou igual a 10 OU um numeral menor ou igual a 20/;
Existe alguma forma de fazer isto direto na variável sem ter que usar if ou semelhantes?
Agradeço desde já. Obrigado! :D
Answer: Se quiser um número aleatório:
$var = rand( 10, 20 );

Manual:

int rand ( int $min , int $max )

Se quiser aplicar a um valor já existente
$var = max( min( $var, 20 ), 10 );

Manual:

mixed min ( mixed $value1 , mixed $value2 [, mixed $... ] )
mixed max ( mixed $value1 , mixed $value2 [, mixed $... ] )
Comment: obrigado, mas me diga: está dando o seguinte erro: Notice: Undefined variable: rma_d in /storage/emulated/legacy/www/CTS/decode.php on line 55
Comment: Esse erro não tem relação nem com o que foi perguntado, nem com o que eu respondi. Você está tentando usar uma variável que não foi definida no seu código.
Comment: Hahaha saquei. Posso te passar o código para que possa entender melhor minha situação? Seria algo do tipo: http://pastebin.com/mrMmracG
Comment: Seguinte... seu código não tem muito a ver tambem. Quando fizer uma pergunta, não adianta querer simplificar demais. Poderia ter posto o código logo na pergunta, e dado uma explicação detalhada do que quer (com um exemplo do que é pra ter na saída). Até a parte de separar os números em grupos, deu pra entender mais ou menos o que quer, sobre a parte de pegar os nomes depois, já não ficou muito claro. Sugiro você criar uma nova pergunta explicando o que você precisa fazer exatamente (explicando o problema, e não o jeito que você acha que vai resolver, pois pode não ser o melhor caminho)
Comment: Umas dicas de leitura, antes de fazer a pergunta: [Tour], [Ask] e com muita atenção, [O que é o problema XY](http://meta.pt.stackoverflow.com/q/499).
Comment: Tudo bem. Muito obrigado, amigo :D


When did the practice of exiting-reentering during applause in classical concerts start?

Question: As the title suggests, I'm looking for some information on when the practice (concerning soloists and directors) of exiting and reentering the stage during an applause after a classical performance started. I have tried searching on the web, but to no avail. There seem to be many articles and general documentation on why we don't clap in between movements (a rule I personally find abhorrent) but none on this end-of-concert practice.
I can see that ego of the performer and/or director is showered with renewed applause is this way, but was there a period in which this coming in and out started being mainstream?
Comment: May I ask why the downvote? The question seems to me pretty precise, not off topic, and conformant to guidelines.
Comment: If I had to guess, I'd surmize that it followed the curtain-call practice of stage plays.
Comment: Spelling in the title: reeintering! As to the downvotes, perhaps someone was (somewhat unreasonably) upset by your opinion on clapping between movements (surely depends if people want to maintain a rapt mood) or by a sense that you grudge the performers their bath in applause.
Comment: Ah good intuition! I'll check out more on that and eventually post an answer myself if I find something
Comment: @PJTraill thanks for pointing out the typo! Edited. As for the downvotes, if that's the case, I'm relieved to know that they were breaking site rules and I wasn't
Answer: You have to be good and confident to leave and return during applause! What if you were not so good? It does create a renewed wave of appreciation from the audience and if a singer has planned an encore they can grab a quick drink and mentally rehearse it. Accompanists can set up the encore music.
Comment: Sure, I don't question that it has its advantage/purpose. I was just wondering if it has always been like this (unlike no clapping between movements, which is known to not have always been this way)


PHP arrays in a loop not clearing

Question: I am writing a utility to do some data loading from text files.  I have a collection of dictionary files that i am processing into arrays that define the structure of the data files.  Yes, i could use classes or otherwise, but using arrays, i have been stumped.
Let's say i have 3 files that i read and load into arrays.  When i read the arrays, the 3rd instance has elements of the second despite trying to use unset and other things.  What am I missing?  I am on cygwin using php 5.3.16
Following are examples of list, but not the real lists.  so, please disregard the substr statements as they are not real
fname c 1 16
lname c 17 30

addr c 1 20
city c 21 30
state c 31 40
zip n 41 45

bday d 1 9
ssn c 10 18

when loading with the code below, the 3rd array has elements from the second, namely bday, ssn, state and zip.
$cnt = 0;
while ($s = fgets($fp, 1024)) {
    $fldprops = array();
    $fldprops[0] = trim(substr($s,0,8));
    $fldprops[1] = trim(substr($s,9,1));
    $fldprops[2] = trim(substr($s,11,3));
    $fldprops[3] = trim(substr($s,15,3));
    $flds[$cnt] = $fldprops;
    $cnt++;
    unset($fldprops);
}

I had thought either one of $fldprops = array(); or unset() would clear the array but it is not working.
Update:  I mistook the point of failure.  it is not apparently at writing to the outer array, but in reading.  As i mentioned in a comment, later in the code, i have a foreach loop and here it is failing:
foreach ($flds as $fldprop) {
     var_dump($fldprop);
}

here, i get bday, ssn, state and zip (the last two entries of the second array merged with the 3rd array).
Comment: Can you please post one value of `$s`?
Comment: `$fldprops = array()` creates a new empty array, so how do you determine that it's not working?
Comment: Can you please rephrase the question. I have a hard time understanding what you are trying to do and where it goes wrong.
Comment: @user1723406 how do you want the output to look like ?
Comment: It's because you forgot the vowels in the word `field`. I'm sure that's it.
Answer: This is the output I am getting:
<?php
    $flds = array();
    $cnt = 0;
    $s = "HelloWorldNiceToSeeYou";
    $fldprops = array();
    $fldprops[0] = trim(substr($s,0,8));
    $fldprops[1] = trim(substr($s,9,1));
    $fldprops[2] = trim(substr($s,11,3));
    $fldprops[3] = trim(substr($s,15,3));
    $flds[$cnt] = $fldprops;
    $cnt++;
    unset($fldprops);
    var_dump($flds, $cnt, $fldprops);
?>

Output:

Notice:  Undefined variable: fldprops in /code/MQpnac on line 13

array(1) {
  [0]=>
  array(4) {
    [0]=>
    string(8) "HelloWor"
    [1]=>
    string(1) "d"
    [2]=>
    string(3) "ice"
    [3]=>
    string(3) "oSe"
  }
}
int(1)
NULL

PHP Notice:  Undefined variable: fldprops in /code/MQpnac on line 13

So PHP is doing it right. Check out live at: http://codepad.viper-7.com/MQpnac
Comment: Nevermind - stupid inner/outer loop issue...   thanks for your help.
Comment: Thank you - turns out i wasn't testing the failure in the right place.   In creating the array and assigning it to the outer array, the values are fine.  the issue, as @Jack points out is in the use.  So, later in the code, i have this:   foreach($arr as $arrelem) and when i hit the 3rd one, i am getting old references to the previous entry.
Comment: Praveen, do you have any insight into why in my using a foreach loop, i am somehow getting references to elements in earlier arrays.  See my update above - i did a var_dump on the creation side and things seem find, but then a var_dump in the midst of the foreach loop and in the 3rd iteration, i see properties from the 2nd nested array.  thanks.
Answer: Why even bother with the temporary variable?
$cnt = 0;
while ($s = fgets($fp, 1024)) {
    $flds[$cnt] = array(
        trim(substr($s,0,8)),
        trim(substr($s,9,1)),
        trim(substr($s,11,3)),
        trim(substr($s,15,3)),
    )
    $cnt++;
}

Comment: No worries, I must have misunderstood the problem. I still don't think I understand what the problem was, but it sounds as though Praveen Kumar has sorted it anyway for you. So you should click on the tick next to his answer to mark it as accepted.
Comment: This is helpful, but not a solution to my problem.  Thanks.

PHP arrays in a loop not clearing


read memory of another process using address of variable

Question: I want to read memory of process A but when the process A is disposed. I have run A, it displays address of some variable, I closed it.
I have run B where I input address of A's variable. It causes error "Access violation...".
I use Borland C++ builder and Windows 7.
Is there any way to watch ram when process is closed? Maybe some tools will help me. Could you give me names of tools to read memory by absolute address after memory deallocation?
Maybe it should be some sort of leak detectors?
Answer: I don't know after it is closed, but while running, you can use ReadProcessMemory() with CreateRemoteThread
Comment: Yes, you also have to spawn the process manually to get a handle, or use external mechanisms such as `HPROCESS` from window name.
Comment: `ReadProcessMemory()` is the only safe option. Once you have found and opened the process's handle, the process will not disapear from memory completely until you close your handle to it. You can use any of the `WaitFor...()` family of functions, or the `GetExitCodeProcess()` function, to detect when the process has exited, then read from it and close your handle to it.
Comment: I know about ReadProccesMemory but it use pointer(or handle) of proccess
Comment: @RemyLebeau true true and true.
Answer: On most systems, separate processes exist in completely separate virtual address spaces.  The pointers you see in one process are completely meaningless in another.
Consequently, you have to explicitly share memory if you want to do this; I'm no Windows expert, but I believe that CreateSharedMemory() may be what you need.
Comment: I want to read memory of some existing application (i.e. Notepad),so i can't to modify it.
Comment: There has to be some solution similar to attaching a debugger to a process (an equivalent to linux `ptrace`), right? It's likely to be fairly complicated, though... [edit: wait, nvm, just saw that OP wants to read the other process' memory AFTER it has finished running...]


non convex optimisation

Question: \begin{eqnarray}
  {\textbf{maximise}} \hspace{2mm} Ar^{-(a+b)} + Br^{-(a+b+c)}-C \nonumber
 \end{eqnarray}
such that,
 \begin{eqnarray}
c= l(h-m_{0}) \nonumber\\
m_{1} \leq h \leq m_{2} \nonumber\\
a+ b \geq m_{3} \nonumber \\
a\leq l(h-m_{4}) \nonumber \\
b \leq l(h-m_{5}) \nonumber \\
\tan\theta=\frac{m_{6}+m_{7}a/b}{m_{8}+m_{9}a/b}  \nonumber \\
h= \frac{(n_{1}-n_{2}\cos(\theta-k_{0}))+a/b(n_{3}-n_{4}\cos(\theta-k_{2}))}{Z(1+a/b)} \nonumber \\
0 \leq \theta \leq 2\pi \nonumber
 \end{eqnarray}
The decision variables are $a,b,c,h,\theta$. All other variables are constants.
Objective functions is strictly convex on a,b,c.
The problem domain is not convex.
How do I solve this problem?
Comment: Hi, welcome to Math.SE. This site supports [MathJax](http://meta.math.stackexchange.com/questions/5020/tex-latex-mathjax-basic-tutorial-and-quick-reference) which will help make your question a bit more legible. Please see the link for some quick intro on how to take advantage of this capability.
Answer: You have to use dummy variables to get rid of functional inequalities and then you may use Kuhn-Tucker conditions. If you are still interested in the solution I can provide further info...

non convex optimisation


Lusin's Theorem and the connection between measurable and continuous functions

Question: I know questions similar to this have been asked on here, but I have yet to find an answer to my question.
I'm trying to prove Lusin's Theorem:

Let $f$ be a measurable real-valued function on $[a,b]$. Given $\delta>0$, there exists a continuous function $\theta$ on$[a,b]$ such that $\mu(\{x;f(x)\neq\theta(x)\})<\delta$.

And in the notes that I'm reading through, the first line of the proof goes:

Let $f(x)$ be measurable on $[a,b]$ and let $\delta>0$. For each $n$, there exists a continuous function $h_n$ on $[a,b]$ such that
  \begin{equation}\mu(\{x:|h_n(x)-f(x)|\geq\delta/2^{n+2}\}<\delta/2^{n+2}.\end{equation}

How is this true? I am not seeing how we are able to make any assumptions about continuous functions just given that we have a measurable function.
Comment: What's your reference? Maybe the author has proven something before the proof?
Comment: @tommyxu3 its from this reference : http://web.math.ucsb.edu/~jhateley/project/real_qual.pdf
Answer: Let $f:[0,1]\to\mathbb{R}$ be a measurable function and $\epsilon>0$.
First, show that there is a simple function $g:[0,1]\to\mathbb{R}$ such that
$$\{x\mid |f(x)-g(x)|>\epsilon\}$$
has measure less than $\epsilon/2$.
Now write $g$ as $\sum_{i=1}^n \alpha_i 1_{A_i}$ with all $\alpha_i$ distinct. By inner regularity, there are disjoint closed sets $C_1,\ldots,C_n$ such that $C_i\subseteq A_i$ for all $i$ and $A_i\setminus C_i$ has measure less than $\epsilon/2n$. Let $g'=\sum_{i=1}^n \alpha_i 1_{C_i}$ and note that $$\{x\mid |f(x)-g'(x)|>\epsilon\}$$
has measure less than $\epsilon$. Moreover, the restriction of $g'$ to $C$ is continuous. Now extend $g'$ from $C$ to a continuous function $h$ on all of $[a,b]$. See here for the details. That's all.

Lusin’s Theorem and the connection between measurable and continuous functions


Limit multiple fields node add form

Question: How can I (either with a module or programmatically) limit the number of values for multiple fields? For example, the user can add only a single value for field A OR field B OR field C.
The total maximum number of values behind all fields can't exceed 1.
Comment: So if user fills in B, they cannot longer enter a value for A and C?
Comment: Yes, Only one value total.
Answer: Using a contrib module:
You can use the conditional_fields module.
Doing it programatically:
You can use the #states property in a custom form or form_alter hook.
You must add this to field A, B and C:
'#states' => [
  'invisible' => [
    [':input[name="field-name"]' => ['filled' => TRUE]],
    'or',
    [':input[name="field-name-2"]' => ['filled => TRUE]],
  ],
],

Of course you have to replace "field-name" and "field-name-2" with the two field names that are not the one adding the states to, hope it makes sense.
If you can't find the name of the field you can look in the browser inspector to get the name of the input field.
Comment: It works for entity forms(node form, user form etc.). So it will automatically work in the front-end too since it will only allow you to input one field value if configured correctly.
Comment: Does conditional fields work on forms or just front-end?

Limit multiple fields node add form


Utilizando Thymeleaf em código Java Script

Question: Boa noite amigos,
Eu tinha um extrato de código javascript usando tags JSP que gerava um gráfico de pizza e funciona corretamente. Preciso agora migrar para Thymeleaf e não faço a mínima idéia de como escrever o código. O codigo javascript com o JSP está assim:

Answer: Boa tarde, você pode utilizar o Inline para Javascript, desta forma você pode acessar as variáveis da view diretamente dentro do código JS. Por exemplo:

Espero ter ajudado.
Answer: Bom dia!
Eu fiz algo parecido para passar uma base64 para o JS vindo de um thymeleaf. Veja:


Erro ao fazer o reshape de uma imagem | ValueError: cannot reshape array of size 72000 into shape (1,24000)

Question: Estou tentando utilizar Python com OpenCV para depois fazer uma classificação.
Porém, na hora que vou fazer popular o objeto, tenho o seguinte erro:

ValueError: cannot reshape array of size 72000 into shape (1,24000)

Entendo que é um erro do tamanho do array, mas o que estou fazendo de errado na hora de criar o array?
Segue o código completo:
import sys
import numpy as np
import cv2
import os

MIN_CONTOUR_AREA = 100
RESIZED_IMAGE_WIDTH = 200
RESIZED_IMAGE_HEIGHT = 120

def main():

    npaFlattenedImages =  np.empty((0, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT))
    intValidChars = [0,1,2,3]

    for npaContour in intValidChars:

        name = "characters/MANDATOR-42-"+str(npaContour)+".png";
        print(name)
        imgTrainingNumbers = cv2.imread(name)
        imgROIResized = cv2.resize(imgTrainingNumbers, (RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT))
        x,y,z = imgTrainingNumbers.shape
        print("Shape: " + str(x) +"|"+  str(y) + "|" + str(z))

        if(imgTrainingNumbers is None):
            print("Not Found")

        cv2.imshow("Imagem", imgROIResized)
        intChar = cv2.waitKey(0)
        intClassifications = []
        intClassifications.append(int(chr(intChar)))
        npaFlattenedImage = imgROIResized.reshape((1,RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT))
        npaFlattenedImages = np.append(npaFlattenedImages, npaFlattenedImage, 0)

    # end for

    fltClassifications = np.array(intClassifications, np.float32)
    npaClassifications = fltClassifications.reshape((fltClassifications.size, 1))
    print ("training complete")
    np.savetxt("classifications__.txt", npaClassifications)
    np.savetxt("flattened_images__.txt", npaFlattenedImages)
    cv2.destroyAllWindows()

    return

###################################################################################################
if __name__ == "__main__":
    main()
# end if

Comment: Reproduza a mensagem inteira do erro, assim fica mais facil tentar ajudar. Detalhe: vc esta misturando [camelCase](https://pt.wikipedia.org/wiki/CamelCase) com [snake_case](https://en.wikipedia.org/wiki/Snake_case), a convenção em pyhthon é lower_snake_case. :-)
Answer: O array da sua imagem possui 72000 posições e você quer transformar ela num array 200x120, que totaliza 24000 posições. Mas por que 72000 posições, sendo que sua imagem é 200x120?
Se sua imagem possui 200x120 pixels, você não pode esquecer que ainda possui, para cada pixel, 3 canais de cores, B (azul), G (verde) e R (vermelho). Cada pixel é formado por 3 posições na sua array. Logo, seu array tem que ter os 200x120 e ainda mais uma dimensão de 3, que são os canais de cores, ou seja: 200x120x3.
Quando você usa o 'cv2.imread()' você obtém um retorno com as seguintes dimensões: altura (no caso, 200), largura (no caso, 120), canais (no caso, 3).
Então, quando estiver trabalhando com imagens coloridas, não se esqueça dos canais de cores. Caso quiser usar uma imagem em escala de cinza, que só ocupa um canal de cor, você tem que convertê-la antes. Para tanto, você pode usar:
imagem_cinza = cv2.cvtColor(image_colorida, cv2.COLOR_BGR2GRAY)

E assim você terá uma imagem de dimensões 200x120x1.
Posto isso que eu disse, é só adequar seu código para trabalhar com as imagens considerando os canais de cores.
Comment: cara valeu, era o problema do canal mesmo, usei o imagem_cinza = cv2.cvtColor(image_colorida, cv2.COLOR_BGR2GRAY) e funcionou

valeu pela ajuda
Comment: Marca o V da resposta para indicar que foi resolvida para ajudar os que vierem aqui com o mesmo problema que você futuramente.


Getting total vote results for several nodes in a view (Drupal 7, Views 3)

Question: This is the problem: I have a Drupal 7, Views 3 site with a certain node type with a Fivestar field on it. I want to get total scores not for one node, but for all the nodes in a view.
For example, let's say our nodes are "restaurants". A person can write a review for this restaurant and give it a score.
Okay, now imagine some of these restaurants are thai restaurants. Now imagine I want the average score for all the thai restaurants in a view. Be careful about this, I don't want the average for one of the restaurants, but for ALL the restaurants in a given category. That's clearly a work for views, but how? How do I do that?
Thanks in advance.
Comment: I'm not fluent with fivestar, but can't you make a view with only vote field? Then use relations to relate votes to restaurants, restaurants to category (you mean taxonomy?) and then filter by that taxonomy? If you can, then you can turn on aggregation on vote field and you're done. You can add category field, too, but *not* any field from restaurant. Skipping review for now, but consider some clarification, like what have you tried with views and what output you got vs the one you wanted, apparently your question is not clear enough for some of higer-rep users.
Answer: Create a view:

Use Agregation.
Use fields.

Add fields in this order remove everything else:

Taxonomy or (Category) as you call it. (Aggregation settings = Group results).
field_rating (or your five star field). (Aggregation settings = Average).

Filters.

Published (YES).
Content type = (restaurants).

What you need to do in resume, is to call all the nodes of type "restaurants" group the results the taxonomy field by Aggregation and AVERAGE all the ratings.
Answer: First, thanks both for your replies. For some reason I didn't receive the notifications on your help, so after a lot of "wall-hitting" I found a solution related to Israel Morales' advice:
1 - Israel Morales was right on its explanation, but please notice that if your sort the result set by any field with unique values, such as Date, the aggregation process will never happen. That was my mistake, That was added when creating the View, I forgot to remove it in the first place.
2 - Views aggregation works fine for numeric values, but it falls short working with strings. Then i discovered VIEWS AGGREGATOR PLUS module. To work with this module you better deactivate Views own aggregator and use the one included in the module, because they are separated processes and it can become a mess. It only renders tables, but it adds quite interesting options to work with strings. For example, it can show data like this:
Who were you at the restaurant with?
Alone : 12
Friends : 25
Couple : 16
Family : 9


Alocação de memória para vetor de strings com malloc()

Question: Este código abaixo, pede um num para a quantidade de strings que eu quero armazenar no vetor que neste caso é o ponteiro char *strings[num].
#include <stdlib.h>
#include <stdio.h>

int main()
{
    int num = 0;
    char *strings[num];
    void string_alunos(char *strings[], int num);

    printf("Quantas strings voce deseja armazenar? ");
    scanf("%d", &num);
    printf("\n");

    printf("Digite as palavras:\n");
    for (int i = 0; i < num; i++)
    {
        strings[i] = (char *)malloc(sizeof(char) * 30);
        if (strings[i] != NULL)
        {
            fflush(stdin);
            scanf("%30[^\n]", strings[i]);
        }
        else
        {
            printf("*** Não foi possível alocar memoria! ***");
            exit(1);
        }
    }
    string_alunos(strings, num);
    free(strings);

    return 0;
}
void string_alunos(char *strings[], int num)
{
    printf("\n::: PALAVRAS :::\n");
    for (int i = 0; i < num; i++)
    {
        printf("%s\n", strings[i]);
    }
}

Ao utilizar o malloc() eu quero uma string de no máximo 30 caracteres. Com o malloc() na teoria ele deveria reservar 30 bytes na memória. certo? Ou não?
Enfim, eu rodei o debug mode, e as posições entre as strings dão um total de 56 bytes de diferença entre uma e outra, por que isso acontece? Não deveria ter uma diferença de 30 bytes?
Answer: O principal problema é que está inicializando o array com tamanho 0 então só vai corromper a memória colocando coisas onde nada foi reservado. Mandando a inicialização dele depois que já sabe o tamanho que ele deve ter resolve isso.
Existe um problema que não é grave em um exercício assim, mas já que foi fazer a liberação então libere algo correto. strings nunca foi alocado com malloc() então não faz sentido dar free() nela, mas todos elementos dela foram inicializado com malloc() então deve dar free() em todos. Eu até não teria criado a função para escrever na tela, porque aí teve que fazer 2 laços sem necessidade.
Ainda tinha um erro no formatador da entrada de dados que eu corrigi.
E aloquei um caractere a mais para o terminador da string, caso contrário daria problema em alguns casos.
Tirei algumas coisas que não fazem sentido ou é inócuo ou até errado fazer. Deve ter aprendido fazer assim em fontes ruins.
Não otimizei para evitar o malloc(). Já que o tamanho da string é conhecido poderia alocar tudo na pilha mesmo (embora seria bom validar o máximo), já que começou fazendo isto, e evitar o malloc() o que simplificaria o código, mas não sei bem qual é o objetivo. De qualquer forma faria mais sentido tudo na memória automática ou tudo na memória dinâmica.
#include <stdlib.h>
#include <stdio.h>

void string_alunos(char *strings[], int num) {
    printf("\n::: PALAVRAS :::\n");
    for (int i = 0; i < num; i++) printf("%s\n", strings[i]);
}

int main() {
    int num;
    printf("Quantas strings voce deseja armazenar? ");
    scanf("%d", &num);
    char *strings[num];
    printf("\n");
    printf("Digite as palavras:\n");
    for (int i = 0; i < num; i++) {
        strings[i] = malloc(31);
        if (strings[i] != NULL) scanf("%30s[^\n]", strings[i]);
        else {
            printf("*** Não foi possível alocar memoria! ***");
            exit(1);
        }
    }
    string_alunos(strings, num);
    for (int i = 0; i < num; i++) free(strings[i]);
}

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
Comment: O `s` diz que deve pegar uma *string*, sem ele não está dizendo o que pegar, tem implementação que pode funcionar por coincidência. Em C quase tudo pode funcionar por coincidência, mas precisa funcionar sempre. È o caso do `fflush()`, só funciona em certas implementações, não em todas, então não deve usar. https://pt.stackoverflow.com/q/325628/101 e https://pt.stackoverflow.com/a/453303/101 e
Comment: https://pt.stackoverflow.com/a/216450/101
Comment: O código possui algumas coisas desnecessárias, como a função, por exemplo, pois o professor pediu no exercício dessa maneira. Enfim, só uma dúvida, você comentou que havia um erro no formatador de entrada, seria pq eu não coloquei aquele 's' ao lado do %30? se sim, o que seria esse 's'? Além disso, vc acabou retirando aquele fflush(stdin), se eu retiro isso, a execução pula direto para o final do programa qnd eu informo o número de strings, isso não seria para limpar o buffer do teclado? Obrigado pela ajuda!
Comment: Opa, desculpe retornar aqui novamente.. mas eu estava testando aquilo que você havia comentado de colocar o tipo de dado que iria ler no scanf "scanf("%30s[^\n]", strings[i])" no caso aqui seria o 's' de string, porém, ao colocar isso na hora de retornar a string ela retorna somente o que está antes do espaço " ", se eu digito por exemplo, bola de basquete, retorna somente bola, se eu retiro esse 's' ele retorna a string inteira, você saberia uma forma de resolver? e para limpar o buffer do teclado agr estou utilizando o while ((c = getchar()) != '\n' && c != EOF){}
Comment: Veja os *links* que eu passei, tem várias informações sobre isso, inclusive como é ineficaz ler frases com `scanf()`.
Comment: Pois é, eu dei uma olhada e vi que para ler os dados, pelo menos em exercícios simples assim o ideal é fgets(), porém não estou conseguindo utilizar ele no programa.. qual seria a forma correta de utilizar baseado nesse programa que eu fiz? dessa forma aqui: fgets(strings[i], 31, stdin) ela lê certinho, tipo se qro armazenar 3 strings, eu digito 'pedro' 'joao' 'gustavo', na saída sai 'pedro' 'gustavo' ele sempre está pulando uma linha, saberia como resolver isso?


Discrepancy outputs from epi.2by2 and online calc for Odds Ratio, How to Interpret?

Question: I'm trying to calculate an odds ratio for a table with very few cases. I've tried epitools oddsratio which I see won't work as the uniroot function error occurs due to dividing by 0.
The table is as follows:
           Outcome
Program  Yes    No
      X    6     0
      Y 9994 10000

I've tried fishers test using epitools function, and epi2by2 also. I know it would seem to not be the right test but I need the odds ratio on this sample for comparison to later mining methods which return more higher positives, so results to show before and after mining methods applied.
Fishers test returns:
Fisher's Exact Test for Count Data

data:  data
p-value = 0.03123
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
 1.177777      Inf
sample estimates:
odds ratio
       Inf

So this is uninterpretable and obviously the too few cases will give that un-iformative wide confidence interval.
Epi.2by2 Odds Ratio test returns:
Point estimates and 95% CIs:
-------------------------------------------------------------------
Prevalence ratio                               NaN (NaN, NaN)
Odds ratio                                     NaN (NaN, NaN)
Attrib prevalence in the exposed *             0.06 (0.01, 0.11)
Attrib fraction in the exposed (%)            NaN (NaN, NaN)
Attrib prevalence in the population *          0.03 (0.01, 0.05)
Attrib fraction in the population (%)         100.00 (NaN, 100.00)
-------------------------------------------------------------------
Yates corrected chi2 test that OR = 1: chi2(1) = 4.168 Pr>chi2 = 0.041
Fisher exact test that OR = 1: Pr>chi2 = 0.031
 Wald confidence limits
 CI: confidence interval
 * Outcomes per 100 population units

I can draw no conclusions from this. HOWEVER, when I use MedCalc I get an Odds Ratio of 13, though of course a very very wide confidence interval. See below:
Odds ratio  13.0078
95 % CI:    0.7327 to 230.9443
z statistic 1.748
Significance level  P = 0.0805

I just don't know how to interpret this over the three tests. Is the very high Odds Ratio undermined completely by significance level and wide conf interval. And why is this odds so high which fits the data but the rest seems inconclusive...How do I report these results?
Comment: Odds ratio cannot be calculated in a standard way in this case. The fact that MedCalc produces a result is more a consequence of imperfect implementation in the software than it is a true answer. By the way, the MadCalc page https://www.medcalc.org/calc/odds_ratio.php also lists the formulas that were supposedly used. It clearly shows that division by zero occurred.
Comment: Have a look at [this paper](https://doi.org/10.1136/bmj.i1981).
Answer: Assuming that the zero isn't a "structural zero", with zeros or low cell counts it's common to use the Haldane-Anscombe correction, which is simply to add 0.5 to all cells.
Some software packages do this by default.
Also note that some software packages report log odds by default, or use other algorithms to calculate odds ratio.
It's not clear to me exactly which package and function you are using, but the following gives the same results as your MedCalc:
Input =("
Program  Yes    No
X          6     0
Y       9994 10000
")

Matrix = as.matrix(read.table(textConnection(Input), header=TRUE, row.names=1))

library(epitools)

Matrix2 = Matrix + 0.5

oddsratio(Matrix2, method="wald")$measure

   ### odds ratio with 95% C.I. estimate     lower    upper
   ###                       X   1.0000        NA       NA
   ###                       Y  13.0078 0.7326957 230.9321


Getting enclosing document from aggregation

Question: This is sort of an expansion on this post: Query array of nested documents for highest value of field
Say I have this document structure:
{
    "_id" : ObjectId("526d89571cd72ce9dbb6b443"),
    "array" : [
         {"text" : "this is a nested document", "value" : 1 },
         {"text" : "this is another nested document", "value" : 2 }
    ]
}

And I'm aggregating it with:
db.collection.aggregate([
    { $match: { _id: new ObjectId("526d89571cd72ce9dbb6b443") } },
    { $unwind: "$array" },
    { $group: { _id: null, value: { $max: "$array.value" } } }
]);

How would I get the document in the array "array" that contains the result of the aggregation -  "value" : 2. I would like to be able to get this:
{"text" : "this is another nested document", "value" : 2 }

Answer: You need to do an $unwind and then a $sort. If you do that, you can use $first:
db.collection.aggregate([
    { $match: { _id: new ObjectId("526d89571cd72ce9dbb6b443") } },
    { $unwind: "$array" },
    { $sort: { "array.value": -1 } },
    { $group: {
        _id: null,
        text: { $first: "$array.text" },
        value: { $max: "$array.value" }
    } }
]);

Your result is then:
{
    "result" : [
        {
            "_id" : null,
            "text" : "this is another nested document",
            "value" : 2
        }
    ],
    "ok" : 1
}

If you also need the original _id, then you could also do this in the $group:
{ $group: {
    _id: null,
    original_id: { $first: "$_id" },
    text: { $first: "$array.text" },
    value: { $max: "$array.value" }
} }

Answer: I think the following query will solve your problem.
db.collection.aggregate([
    { $match: { _id: new ObjectId("526d89571cd72ce9dbb6b443") } },
    { $unwind: "$array" },
    { $group: { _id: "$array.text", value: { $max: "$array.value" } } },
    {$limit : 1}
]);

This will print the result as follows :
"result" : [
    {
        "_id" : "this is another nested document",
        "value" : 2
    }
]

Comment: It is working. The only differece is, I set the text data into the _id field not in a text field.


How to add a background image attached to a text in a title with HTML and CSS?

Question: Do you have any idea how to do this with HTML and CSS? Adding an image around a text in a title. See screenshot.
Title with blue background image around a single word:
Comment: use background-position prop to achieve it...please paste the code to help.
Answer: Is this you want to achive ?
The CSS background properties are used to define the background effects
    for elements.
CSS background properties:

background-color
background-image
background-repeat
background-attachment
background-position

Some text with a background image

Good luck


O que vem a ser uma função binary-safe? E quais suas aplicações?

Question: Estava navegando pelo manual do php.net quando me deparei com a seguinte nota sobre a função str_replace:

Nota: Esta função é binary-safe.

Dei uma pesquisada aqui na rede e não achei nada a respeito.
O que vem a ser uma função binary-safe? E quais suas aplicações?
Answer: Basicamente significa que ela vai funcionar com qualquer sequência de bytes.
Se puser caracteres de controle, "\n", ou caractere nulo "\0", ela não vai ter nenhum problema em fazer a substituição.
Exemplo:
$str1 = 'quebra'.chr(0).'de'.chr(0).'linha';
$str2 = str_replace( chr(0), "\n", $str1 );

Isto funciona perfeitamente.
Em PHP em geral as funções são, pois internamente o PHP associa os dados a uma espécie de "variável" interna que guarda o tamanho.
Em algumas linguagens por exemplo, em vez do tamanho ser armazenado, o que indica o fim da string é o caractere nulo "\0" (que no PHP é devolvido pelo chr(0)), o que seria um exemplo de "não binary safe".
Note que isto são apenas decisões de arquitetura, é tarefa do programador conhecer a linguagem e resolver conforme necessário. Simplesmente se precisar de uma string binary safe em C, basta fazer como o PHP faz internamente: armazenando o tamanho dos dados, em vez de indicar com um caractere específico.


What is the degree of a Bezier curve?

Question: A question regarding control points in a practice exam asks to find the relationship between Bezier control points and degrees of the curves.
How many control points are required to specify a Bezier curve of degree x?
What is the degree of a Bezier curve?
Comment: $x+1$ control points are needed. The curve degree is the degree of the $x$/$y$-polynomials.
Comment: If the name "control points" is for all the points that participate to the construction: the answer is $x+1$ (for example degree 3 at most  for a cubic Bezier curve  defined by 4 points A,B,C,D).
Answer: Cubic Bezier curve is usually defined as:
$$\mathbf{B}(t)=(1-t)^3\mathbf{P}_0+3(1-t)^2t\mathbf{P}_1+3(1-t)t^2\mathbf{P}_2+t^3\mathbf{P}_3 \mbox{ , } 0 \le t \le 1.$$
When you expand and rearrange the term you will get:
$$(-\mathbf{P}_0 + 3\mathbf{P}_1 - 3\mathbf{P}_2 + \mathbf{P}_3)t^3 + (3\mathbf{P}_0 - 6\mathbf{P}_1 + 3\mathbf{P}_2)t^2 + (-3\mathbf{P}_0 + 3\mathbf{P}_1)t + \mathbf{P}_0$$
Which is a polynomial in $t$ of degree 3. In general, the "degree" of a Bezier curve is the highest exponent of $t$ if written as polynomial.
If you stare at the first equation, you will notice the are four terms near the four points, each with raising power of $t$ (i.e., $t^0, t^1, t^2, t^3$). Counting from zero, there are four points for degree 3. In general, one needs degree + 1 points to define the curve.

What is the degree of a Bezier curve?


Scala can not access field

Question: I have the following Scala Class:
class Component(val node:Int, var start:Int, var c_size:Int)

I instantiate it like this:
var comp = new Component(1, 1, 1)

Then, I try to access one of its fields:
for(neighbour <- graph[comp.node]) ...

At this point however, I am getting an error:

error: type node is not a member of Component

Why is the field not recognized?
Comment: Try using graph(comp.node).
Comment: This is one of the first things a Java programmer must forget. Scala does not use `[]` for array access.
Comment: Could you post the declaration or type of `graph`?
Comment: @Nyavro That was it.
Comment: Square brackets `[` and `]` are used for type parameters in Scala.
Answer: Assuming graph is declared as an array, you cannot access array elements using []. Use () instead:
for(neighbour <- graph(comp.node))

The error is shown because Scala thinks you are trying to parameterize a graph method, with the type parameter being comp.node. This would be valid if there was such a method and Component had an inner type named node.

Scala can not access field


What is "restricted maximum likelihood" and when should it be used?

Question: I have read in the abstract of this paper that:

"The maximum likelihood (ML) procedure of Hartley aud Rao is modified by adapting a transformation from Patterson and Thompson which partitions the likelihood render normality into two parts, one being free of the fixed effects. Maximizing this part yields what are called restricted maximum likelihood (REML) estimators."

I also read in the abstract of this paper that REML:

"takes into account the loss in degrees of freedom resulting from estimating fixed effects."

Sadly I don't have access to the full text of those papers (and probably would not understand if I did).
Also, what are the advantages of REML vs. ML? Under what circumstances may REML be preferred over ML (or vice versa) when fitting a mixed effects model?
Please give an explanation suitable for someone with a high-school (or just beyond) mathematics background!
Comment: I found [this paper](https://doi.org/10.1080/00273171.2017.1344538) to be a really nice explanation provided in a less formal language.  See the section *Maximum likelihood and associated problems*.
Comment: see http://stats.stackexchange.com/questions/99895/why-does-one-have-to-use-reml-in-stead-of-ml-for-choosing-among-nested-var-cov/171529#171529
Comment: @RTbecard: Ungated link:  https://www.researchgate.net/profile/Daniel-Mcneish/publication/317570318_Small_Sample_Methods_for_Multilevel_Modeling_A_Colloquial_Elucidation_of_REML_and_the_Kenward-Roger_Correction/links/5bca408692851cae21b43163/Small-Sample-Methods-for-Multilevel-Modeling-A-Colloquial-Elucidation-of-REML-and-the-Kenward-Roger-Correction.pdf
Answer: As per ocram's answer, ML is biased for the estimation of variance components. But observe that the bias gets smaller for larger sample sizes. Hence in answer to your questions "...what are the advantages of REML vs ML ? Under what circumstances may REML be preferred over ML (or vice versa) when fitting a mixed effects model ?", for small sample sizes REML is preferred. However, likelihood ratio tests for REML require exactly the same fixed effects specification in both models. So, to compare models with different fixed effects (a common scenario) with an LR test, ML must be used.
REML takes account of the number of (fixed effects) parameters estimated, losing 1 degree of freedom for each. This is achieved by applying ML to the least squares residuals, which are independent of the fixed effects.
Comment: Indeed, the REML estimator of a variance component is usually (approximately) unbiased, while the ML estimator is negatively biased. However, the ML estimator usually has lower mean-squared error (MSE) than the REML estimator. So, if you want to be right on average, go with REML, but you pay for this with larger variability in the estimates. If you want to be closer to the true value on average, go with ML, but you pay for this with negative bias.
Comment: In the simple case of a constant mean, and constant variance, ML is dividing SSR with $n$ while REML is dividing SSR with $(n-1)$.  So REML is a generalization of this procedure!
Comment: "ML is biased for the estimation of variance components". Does it mean the variance of the random effects or also the the standard errors of the fixed-effect coefficientes?
Answer: Here is a quick answer...

Standard illustrative example
Let $y = (y_1, \dotsc, y_n)$ be a sample from a normal distribution $\mathrm{N}(\mu, \sigma^2$). Both $\mu$ and $\sigma^2$ are unknown. The maximum likelihood estimator of $\sigma^2$, obtained by taking the derivative of the log-likelihood with respect to $\sigma^2$ and equating to zero, is
$$
\hat{\sigma}^2_{\textrm{ML}} = \frac{1}{n} \sum_{i=1}^n (y_i -\bar{y})^2
$$
where $\bar{y} = \frac{1}{n} \sum_{i=1}^n y_i$ is the maximum likelihood estimator of $\mu$. We can show that
$$
\mathrm{E}(\hat{\sigma}^2_{\textrm{ML}}) = \frac{n-1}{n} \sigma^2.
$$
[Start by rewriting $\hat{\sigma}^2_{\textrm{ML}}$ as $\frac{1}{n} \sum_{i=1}^n \left((y_i - \mu) + (\mu - \bar{y})\right)^2$]. Thus, $\hat{\sigma}^2_{\textrm{ML}}$ is biased. Note that if we had known $\mu$, then the MLE for $\sigma^2$ would have been unbiased. Hence, the problem with $\hat{\sigma}^2_{\textrm{ML}}$ appears to be linked with the fact that we have substituted $\bar{x}$ for the unknown mean in the estimation. The intuitive idea of REML estimation is to end up with a likelihood that contains all the information on $\sigma^2$ but no longer contains the information on $\mu$.
More technically, the REML likelihood is a likelihood of linear combinations of the original data: instead of the likelihood of $y$, we consider the likelihood of $Ky$, where the matrix $K$ is such that
$\mathrm{E}[Ky] = 0$.

REML estimation is often used in the more complicated context of mixed models. Every book on mixed models have a section explaining REML estimation in more details.

Edit
@Joe King: Here (and here, for English web page context) is one of my favorite books on mixed models that is fully available online. Section 2.4.2 deals with estimating variance components. Enjoy your reading :-)
Comment: Thank you - this is helpful - though I don't have easy access to books on mixed models. Please could you relate your answer to the 2 quotes in my post ?
Comment: I wonder how a multivariate Gaussian changes the story? http://stats.stackexchange.com/questions/167494/bias-in-the-mle-of-variance-component-in-a-multivariate-gaussian/
Answer: ML method underestimates the variance parameters because it assumes that the fixed parameters are known without uncertainty when estimating the variance parameters.
The REML method uses a mathematical trick to make the estimates for the variance parameters independent of the estimates for the fixed effects.
REML works by first getting regression residuals for the observations modeled by the fixed effects portion of the model, ignoring at this point any variance components.
ML estimates are unbiased for the fixed effects but biased for the random effects, whereas the REML estimates are biased for the fixed effects and unbiased for the random effects.


What is causing this Segmentation Fault: 11?

Question: Okay, so this is effectively the first C++ I have ever written. Consequently, my attempts to research this Segmentation Fault error have failed to yield a useful response that I can understand. Most suggestions say that I'm exceeding the heap memory, but I don't see how I could be in this code.  Maybe I'm trying to reference something that I can't reference?
Can someone please take a look at this code and possibly tell me what is causing this error?  The code compiles fine, it just throws this error when it runs.  I have tried commenting out all the code involving vectors, and that didn't solve the problem.  I'm guessing it's something stupid that I'm missing or just a concept about C++ that I don't understand.  Either way, some insight would be much appreciated!  Thanks in advance.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

const int DEFAULT_SIZE = 15;

//---------------------------------User----------------------------------------------------------------------------------
//User Interface Definition
class User
{
public:
    User(string nameValue);
    string getName();
    string getWall();
    string getHome();
private:
    string name;
};
//User Implementation Definition
User::User(string nameValue) : name(nameValue)
{
}
string User::getName()
{
    return name;
}

//---------------------------------Group----------------------------------------------------------------------------------
//Group Interface Definition
class Group
{
public:
    Group(string nameValue);
    string getName();
    void addMember(User newMember);
private:
    string name;
    vector<User> members;
};
//Group Implementation
Group::Group(string nameValue) : name(nameValue)
{
}
string Group::getName()
{
    return name;
}
void Group::addMember(User newMember)
{
    members.push_back(newMember);
}

//---------------------------------MessageBuffer----------------------------------------------------------------------------------
//MessgeBuffer Interface Declaration
class MessageBuffer
{
public:
    MessageBuffer();
    void insert(string user, string recipient, string message);
    string readByGroup(string groupName);
    string readByUser(string userName);
    string readPublic();
private:
    string buffer;
};
//MessageBuffer Implementation
MessageBuffer::MessageBuffer() : buffer("")
{
}
void MessageBuffer::insert(string user, string recipient, string message)
{
    buffer = "|<" + user + "::" + recipient + ">|" + message + buffer;
}
string MessageBuffer::readByGroup(string groupName)
{
    return "Something";
}
string MessageBuffer::readByUser(string userName)
{
    return "Something";
}
string MessageBuffer::readPublic()
{
    return "Something";
}

//---------------------------------System-------------------------------------------------------------------------------
//System Interface Definition
class System
{
public:
    System();
    void start();
private:
    bool running;
    User currentUser;
    MessageBuffer messageBuffer;
    vector<User> users;
    vector<Group> groups;
};
//System Implementation Definition
System::System() : running(false), currentUser(0)
{
}

void System::start()
{
    running = true;
    string userSelection;

    cout << "===========================================================" << endl;
    cout << "|\t  Welcome to the Auburn Messaging System! \t  |" << endl;
    cout << "===========================================================" << endl;
    while (running) {
        cout << "1) Create a new user\n";
        cout << "2) Broadcast a message\n";
        cout << "3) Multicast a message\n";
        cout << "4) Unicast a message\n";
        cout << "5) Display Wall page\n";
        cout << "6) Display Home page\n";
        cout << "7) Create a new group\n";
        cout << "8) Join a group\n";
        cout << "9) Switch to a different user\n";
        cout << "10) Quit Auburn Messaging System\n";
        cin >> userSelection;
        if (userSelection == "1")
        {
            running = false;
        }
    }
}
//----------------------------------------------------------------------------------------------------------------------
int main() {
    cout << "Part 1";
    System system1;
    cout << "Part 2";
    system1.start();
    cout << "Part 3";
    return 0;
}

Comment: Have you tried debugging it?
Comment: That probably should've been my first step, but honestly I've just been writing in Sublime and compiling in the terminal. Probably should figure out how to use xCode for this.
Comment: Or learn to use GDB.
Comment: I think you could easily find the problem by commenting out the declaration of `system1`.  This program is simple enough that debugging it could have been a very small process-of-elimination.  That is assuming the crash occurs during the *construction* of `currentUser` -- rather than sometime later.
Comment: @nobar In that sense, I did debug it.  I tried commenting out the declaration of system1, among other things, and it still threw the error.
Answer: In the System constructor, currentUser(0) is attempting to initialise a the first parameter of the User constructor (a string) with a literal zero which will be interpreted as a null char pointer.
The std::string constructor (look for basic_string<char> in the debugger) which constructs a string from a char* will be called with a null pointer, which it will dereference and invoke undefined behaviour.
Comment: If you want to initialize a std::string to an empty string, use an empty string: `""`. You do not want it to be null. Only pointers can be null. (Mythagel, your answer should explain more clearly that the std::string constructor that takes a char pointer is the one that gets called, and that this immediately proceeds to try and dereference a null pointer.)
Comment: That was the problem.  I thought 0 could be used as the C++ equivalent to null in Java.
Comment: Now that you know where the error is (and what it is) use a debugger to observe what the state of the program is at the point it fails.
Comment: @ordanj You sort of have it backwards, in C and C++ NULL is a macro for 0, whereas in Java it is a unique value.  C++11 adds a `nullptr_t`, which is more like the Java null value but will still cause a segfault.
Comment: @ordanj Since C++11 you should use `nullptr` whenever you mean null pointer.
Comment: 0 *is* a null pointer -- which is why this unfortunately compiles.
Comment: @CodyGray Yeah, that makes sense.  But, I was trying to initialize a new object of the User class, not an empty string.
Comment: @ordanj But the `0` ultimately gets used to initialize a string. C++ is quite complicated. It may be worth starting out with smaller, simpler code samples first.
Comment: @CodyGray clarified wrt. `std::string` constructor given later comments from @ordanj regarding confusion with `User` ctor.


Segmentation fault when reading arguments from command line

Question: I am trying to implement a duplicate cat function in c. I am getting segmentation faults, and I am unable to find why.
 #include <stdio.h>
    #include <stdlib.h>

int main(int argc,char *argv[])
{
    char* s;        /* input string */

    int c;

    if(argc==1){

        while (gets(s)){
            puts(s);
        }
    }

    else{

    FILE *file = fopen( "./argv[1]", "r" );
    while((c=fgetc(file))!=EOF){
        fputc(c,stdout);

    }

    fclose(file);

    }

    return 0;
}

Comment: Ah yes the daily I-write-stuff-to-an-address-where-nothing-is-allocated question came early today. Too localized, vote to close.
Comment: First of all, you should debug your program. If you don't know how to do that this is a perfect time learn. This will almost always give you the line of the error immediately and thus save you from long searches. Second, you never allocated memory for s. And last, you want to open `./argv[1]`, not `"./argv[1]"`.
Comment: @Lundin Yeah, I'm afraid there are yet more of this question in today's daily routine on SO... -.-
Comment: I'd use `fread` instead of `fgets`, as `cat` does not actually need to work using newlines, and that way we avoid special handling for long lines.
Answer: You are reading characters into s without allocating memory:
    while (gets(s)){
        puts(s);
    }

Allocate some memory first by using malloc (you will have to free it later):
    char *s = malloc(some_buffer_size);

or make it fixed:
    char s[some_buffer_size];

Be careful with gets by the way, as there is no way of limiting the number of characters input, which can lead to buffer overflows. Use fgets(buf, sizeof(buf), stdin) instead.
Second mistake has been pointed out by others, you cannot call fopen like that, use this instead, or use the solution from H2CO3:
 FILE *file = fopen( argv[1], "r" );

Comment: @Fazlan Because your `fopen()` returns `NULL`. Time to read a C book and to develop some common sense.
Comment: Please [don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169).
Comment: No reason not to use a fixed buffer `char s[some_buffer_size]`.
Comment: Also `fopen( "./argv[1]", "r" );` note the first argument
Comment: Oh yeah. Thanx. But now I am getting segmentation faul when trying to read from file
Comment: Using `gets` in the first place is probably more severe a fault than casting the result of `malloc`. `gets` is not part of the C language no longer, don't use it.
Comment: @Keith That's exactly what was going to be my second comment.
Comment: @H2CO3 that's what I suggest
Comment: @BartFriederichs Yap, didn't see the `f`... Sorry.
Answer: gets(s); - Here s is not initialized to point to anything.
fopen( "./argv[1]", "r" ); - This is wrong. Must be - fopen(argv[1], "r" );
Answer: fopen( "./argv[1]", "r" );

Huh, what? Assumptions, right?
char buf[0x1000];
snprintf(buf, sizeof(buf), "./%s", argv[1]);
FILE *f = fopen(buf, "r");

Also, you are using the s pointer uninitialized (you don't allocate any memory for it), so your program also invokes undefined behavior.
Answer: malloc() basically allocates memory. in the end, you need to free() your variable as well to free the allocated space.
you should use gdb() for finding out errors and debugging your program.


How to plot multiple values in CSV file in one plot?

Question: first of all please do not judge my English i am not native.
i have a large CSV  file,  i have a round 84000 column and and 5 row as follow :
filename    max     probability  size_start size_end
   file1     33     0.001         30          10
   file2     4      0.001         30          10
   file3     50     0.001         40          10
   file4     0      0.001         50          10

i would like to plot probability  against the max  for each pair of size_start and start end  , i have 10 different pair of start and end.
i think i have to do a for loop or while loop to loop over the start and the end and plot the corresponded  values for the probability and max but i do not know how to loop over csv file . please enlighten me if you have any idea about the answer.
Comment: What should the plot look like? a scatter plot, e.g. `probability` on the x axis and `max` on the y axis? or perhaps a bar plot or something else?
Comment: line plot or scatter plot are working for me
Comment: Your question is unclear. Do you need one plot for each start/end combination? Do you really have 84k _columns_ or _rows_?
Comment: 84k columns, means 84k file ...yes for each combination for example i have (10,30) start and end i want to plot the corresponded  values of  probability on x axis  and max on the y axis, and on the same plot for (20,30)  start and end i want to plot the corresponded  values of  probability on x axis  and max on the y axis .. and on for all the 10 pairs.. i hope it is clear
Answer: You could use Panda's DataFrame.plot() method, after subdividing your data into groups:
import pandas as pd

df = pd.read_csv('data.csv')
for (size_start, size_end), group in df.groupby(['size_start', 'size_end']):
    ax = group.plot(x='probability', y='max', kind='scatter')
    ax.get_figure().savefig(f'plot_{size_start}_{size_end}.png')

This will create a separate image for each size_start/size_end combination. There are many options to customize the design and layout of the plot, have a look at the documentation.
If you want to combine the plots all into the same diagram, you can simply call the plot() function multiple times:
from matplotlib import pyplot as plt

fig = plt.figure()
for (size_start, size_end), group in df.groupby(['size_start', 'size_end']):
    plt.plot('probability', 'max', data=group, label=f'{size_start} / {size_end}')

plt.xlabel('probability')
plt.ylabel('max')
plt.legend()
plt.show() # or plt.savefig('figure.png')

Comment: Thank you so much for the answer, it worked but each pair of start_size and end_size is plotted in seperet plot, is there a way to plot them all together in the same plot like in this plot        https://i.stack.imgur.com/8oCrX.png


Weird behavior of send() and recv()

Question: SORRY FOR BAD ENGLISH
Why if I have two send()-s on the server, and two recv()-s on the client, sometimes the first recv() will get the content of the 2nd send() from the server, without taking just the content of the first one and let the other recv() to take the "due and proper" content of the other send()?
How can I get this work in an other way?
Answer: This is by design.
A TCP stream is a channel on which you can send bytes between two endpoints but the transmission is stream-based, not message based.
If you want to send messages then you need to encode them... for example by prepending a "size" field that will inform the receiver how many bytes to expect for the body.
If you send 100 bytes and then other 100 bytes it's well possible that the receiver will instead see 200 at once, or even 50 + 150 in two different read commands. If you want message boundaries then you have to put them in the data yourself.
There is a lower layer (datagrams) that allows to send messages, however they are limited in size and delivery is not guaranteed (i.e. it's possible that a message will get lost, that will be duplicated or that two messages you send will arrive in different order).
TCP stream is built on top of this datagram service and implements all the logic needed to transfer data reliably between the two endpoints.
As an alternative there are libraries designed to provide reliable message-passing between endpoints, like ZeroMQ.
Comment: But how can I tell the second 'send()' to not send nothing to the first 'rec()'? Your solution hellp me with doing that "text 2" from 'send() 2' to not mixing with the "text 1" accepted in 'recv() 1'. But then I loose the data of 'send() 2' because its sent and no one wait for it to take the text from sent. how can i syncronized between the exact send and the exact recv?
Comment: @dfsfg you can't. That's the point of this answer.  The functions don't work that way.
Answer: Most probably you use SOCK_STREAM type socket. This is a TCP socket and that means that you push data to one side and it gets from the other side in the same order and without missing chunks, but there are no delimiters. So send() just sends data and recv() receives all the data available to the current moment.
You can use SOCK_DGRAM and then UDP will be used. But in such case every send() will send a datagram and recv() will receive it. But you are not guaranteed that your datagrams will not be shuffled or lost, so you will have to deal with such problems yourself. There is also a limit on maximal datagram size.
Or you can stick to TCP connection but then you have to send delimiters yourself.

Weird behavior of send() and recv()


How to pull specific data from JSON output?

Question: I've been working on a simple weather web app using flask and python.
One of the routes I have in my main.py file is the weather route. It has a function that calls the weather api, and prints the json output.
My problem is pulling specific data out of the json output. This is what the output looks like:
{'base': 'stations',
 'clouds': {'all': 90},
 'cod': 200,
 'coord': {'lat': 40.73, 'lon': -73.99},
 'dt': 1557824237,
 'id': 5128581,
 'main': {'humidity': 93,
          'pressure': 1009,
          'temp': 43.93,
          'temp_max': 46,
          'temp_min': 42.01},
 'name': 'New York',
 'sys': {'country': 'US',
         'id': 4026,
         'message': 0.0144,
         'sunrise': 1557826807,
         'sunset': 1557878678,
         'type': 1},
 'visibility': 12874,
 'weather': [{'description': 'mist', 'icon': '50n', 'id': 701, 'main': 'Mist'},
             {'description': 'light intensity drizzle',
              'icon': '09n',
              'id': 300,
              'main': 'Drizzle'}],
 'wind': {'deg': 20, 'speed': 8.05}}

I used pprint to make it a little more readable. What I am trying to do is pull the "description" data by creating a python dictionary.
weather = {
    'city': city,
    'temperature': r['main']['temp'],
    'description': r['weather'][1]['description'],
    'icon': r['weather'][1]['icon'],
    }

When I print weather, I expect it to print out description: light intensity drizzle, icon: 09n, id:300, but I get a key error. I don't get a key error when I use "0" instead of "1" but I am trying to pull the second description data, not the first. Any suggestions?
Comment: Let's say all that JSon is y, to access weather=y.weather[1], this should return what you want
Answer: Try calling the weather key then use indices to get into the list, then do gets on the items you want.
data = {'base': 'stations',
 'clouds': {'all': 90},
 'cod': 200,
 'coord': {'lat': 40.73, 'lon': -73.99},
 'dt': 1557824237,
 'id': 5128581,
 'main': {'humidity': 93,
          'pressure': 1009,
          'temp': 43.93,
          'temp_max': 46,
          'temp_min': 42.01},
 'name': 'New York',
 'sys': {'country': 'US',
         'id': 4026,
         'message': 0.0144,
         'sunrise': 1557826807,
         'sunset': 1557878678,
         'type': 1},
 'visibility': 12874,
 'weather': [{'description': 'mist', 'icon': '50n', 'id': 701, 'main': 'Mist'},
             {'description': 'light intensity drizzle',
              'icon': '09n',
              'id': 300,
              'main': 'Drizzle'}],
 'wind': {'deg': 20, 'speed': 8.05}}

print(data.get('weather')[0].get('description'))
print(data.get('weather')[0].get('icon'))
print(data.get('weather')[0].get('id'))


Elixir phoenix Logout link does not work

Question: I have the following Logout link in my app, but when i click on it, it does not do anything.

  <%= link "Logout", to: session_path(@conn, :delete), method: :delete, class: "nav-link" %>

The html element the above code generates is

  <form action="/logout" class="link" method="post">
    <input name="_method" type="hidden" value="delete">
    <input name="_csrf_token" type="hidden" value="GiA4JANYN10+JQhfUgIEARxZCRIEAAAUijC25v5Kj8j7KI6qrOtv==">
    Logout
  </form>

Does anyone see anything obviously wrong here that the logout link would not do anything when clicked?
brunch-config.js
javascripts: {
 joinTo: {
   "js/app.js": /^(web\/static\/js)/,
   "js/jquery-ujs.js.js": ["web/static/vendor/js/jquery-ujs.js.js"],
   "js/jquery.js": ["web/static/vendor/js/jquery.js"]
 },
 ...

Comment: http://stackoverflow.com/questions/34272706/delete-link-not-working-phoenix
Answer: I believe that the code that is responsible for handling such cases is in the phoenix_html package's js file so you need to make sure you have added this package to mix and have the following line in your app.js file uncommented:
import "deps/phoenix_html/web/static/js/phoenix_html"
Comment: it's not working for me, the logout still has # value on href
Answer: In my case it happened due to missing npm dependencies.
In phoenix.server logs I've noticed that app.js wasn't built due to absence of babel-preset-2015 and then babel-preset-2016.
npm i --save-dev babel-preset-2015 babel-preset-2016

Answer: One possible workaround is to do a GET request instead of a DELETE request:
Your html.eex code:

  <%= link "Logout", to: session_path(@conn, :delete), class: "nav-link" %>

An example router.ex:
resources "/sessions", SessionController, only: [:new, :create]
get "/sessions", SessionController, :delete

Comment: While this will work, it's not recommended to use this approach. Session is an entity and as such can be created, updated, viewed or destroyed. For these actions HTML has different actions, by which browser and responding server knows (and therefore can do some checks) about what are trying to click on just by checking type of a request. GET request are generally not checked, while others have to have for example proper csrf token to avoid XSS scripts.


Groups with order divisible by $d$ and no element of order $d$

Question: It occurred to me that I somehow believe the following statement without actually knowing how to prove it: for every composite natural number $d$ there is a group whose order is divisible by $d$ yet it contains no element of order $d$. This is a kind of converse to Cauchy's Theorem that every group of order divisible by a prime $p$ contains an element of order $p$.
This is obviously true when $d$ is such that there is more than a single isomorphism type of groups of order $d$; just take any such non-cyclic group and you're done. But there are composite numbers $d$ for which every group of order $d$ is cyclic (I think these numbers, along with the primes themselves, are called "Cyclic numbers").
For example, every group of order $15$ is cyclic. However, the group $A_5$ has order divisible by $15$ but contains no element of that order.
How would one proceed to construct such a group for any given composite $d$?
Comment: Just a comment: every **cyclic number** is squarefree, so you can reduce to that case.  (There is a well-known simple characterization of cyclic numbers, but it may not be directly relevant here.  I can supply a reference if anyone wants one.)
Comment: P.S.: I believe it too, without having a proof off the top of my head!  It's a fun question...
Answer: Let $d$ be your composite, squarefree number. Let $p$ be the largest prime dividing $d$. Then the symmetric group $S_p$ will have order a multiple of $d$ (since its order is $p$-factorial, a multiple of every prime up to $p$), but no element of order $d$. Every element of $S_p$ is a product of disjoint cycles; to have an element of order a multiple of $p$, one of those cycles must be a $p$-cycle; but there's no cycle disjoint from that $p$-cycle.
Comment: Perhaps the simplest example when $d$ is not squarefree is ${\bf Z}_p\times{\bf Z}_{d/p}$, where $p$ is any prime whose square divides $d$.
Comment: Great! Simple and elegant. As Pete mentioned, when $d$ is not squarefree there is clearly a non-cyclic group of order $d$ (say, a direct product of cyclic groups of prime orders with the appropriate multilplicities) so your proof handles all the remaining cases.


Match row names and column names to values in another data frame

Question: I have two data frames as follows :
df1 <- t(data.frame(seq(1,6,by=1),seq(6,1,by=-1)))
colnames(df1) <- c("A","B","C","D","E","F)
rownames(df1) <- c("a","b")
df2 <- data.frame(rep(colnames(df1),2),rep(rownames(df1),6))
colnames(df2) <- c("Vector1","Vector2")

Such that
df1
     A   B   C   D   E   F
  a  1   2   3   4   5   6
  b  6   5   4   3   2   1

df2
   Vector1    Vector2
     A           a
     B           b
     C           a
     D           b
     E           a
     F           b
     A           a
     B           b
     C           a
     D           b
     E           a
     F           b

I want to match the column values of df2 to column names and row names of df1, and fill the corresponding value to a new column in df2 as follows:
 Vector1 Vector2   Newcol
   A       a         1
   B       b         5
   C       a         3
   D       b         3
   E       a         5
   F       b         1
   A       a         1
   B       b         5
   C       a         3
   D       b         3
   E       a         5
   F       b         1

Any suggestions would be much appreciated. Thanks.
Comment: see `?toupper` and `?merge`
Comment: [A similar Q&A](http://stackoverflow.com/a/31824224/2204410)
Answer: We can use merge with melt.  The melt returns a three column data.frame, merge it with the second dataset to create the new column
library(reshape2)
merge(df2, melt(df1), by.x = c("Vector1", "Vector2"), by.y = c("Var2", "Var1"))

Or a base R option would be to get the numeric index with match after pasteing the 'df2' rowwise (do.call(paste) and get the pasted column names and row names of 'df1' using outer.  Using the numeric index, we get the values in 'df1' to create the 'Newcol'
df2$Newcol <- df1[match(do.call(paste, df2),
                       t(outer(colnames(df1), rownames(df1), FUN = paste)))]
df2$Newcol
#[1] 1 5 3 3 5 1 1 5 3 3 5 1


Undefined behaviour of right shift (a >> b) when b is greater than the number of bits in a?

Question: Apparently, the behaviour of the right shift operation:
a >> b

is undefined in C and C++ when b >= sizeof(a)*CHAR_BIT (whereas in the normal case, the "new bits" introduced from the left due to the right shift are equal to zero).
Why is this undefined behaviour better than setting the result to zero when b >= sizeof(a)*CHAR_BIT?
Comment: Because there are platform specfic details like I explain [here](http://stackoverflow.com/a/19636588/1708801) such as the size of the shift register which may make it difficult to standardize one behavior because it may be inconvenient to implement on some platforms.
Comment: @sje397: C++ 2011 Section 5.8 Shift Operators Paragraph 1: _The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand._
Comment: @user3528438: Shifting by a negative value is also undefined.
Comment: @user3528438: There is a difference between implementation defined behavior and undefined behavior. And this is undefined behavior. I quoted the relevant line in an earlier comment. In C 2011 you can also find basically the exact same line as well.
Comment: what about negative values? does right shift -1 bit same as left shift 1 bit? if you are sure your ISA can do it efficiently, you are better off just use intrinsics to force compilier to use that instruction to implement bit shift operators.
Comment: @BillLynch It's undefined because it's implementation defined. The only way to make use of those ISA specific features is to use intrinsics, or, assemblies.
Comment: possible duplicate of [Why does left shift operation invoke Undefined Behaviour when the left side operand has negative value?](http://stackoverflow.com/questions/3784996/why-does-left-shift-operation-invoke-undefined-behaviour-when-the-left-side-oper) different question but the answer is the same.
Comment: @BillLynch I meant it's not better, not that it is not undefined. I thought he was asking why it would be better to use undefined behaviour in a program rather than assign something to 0.
Answer: We can get an idea of why languages choose undefined behavior from Why Language Designers Tolerate Undefined Behavior and it says:

This answer came from two general design principles behind C:

The language should impose no unnecessary overhead on the implementation.
It should be as easy as possible to implement C on a wide variety of hardware.

in this specific case what happens when we use a shift count larger than the bit width will depend on the architecture for example as I explain in my answer here:
on some platforms the shift count will be masked to 5 bits for example on an x86 architecture we can see the Intel® 64 and IA-32 Architectures Software Developer's Manual section SAL/SAR/SHL/SHR—Shift in the IA-32 Architecture Compatibility section says:

The 8086 does not mask the shift count. However, all other IA-32 processors (starting with the Intel 286 processor) do mask the shift count to 5 bits, resulting in a maximum count of 31. [...]

So implementing shift for an arbitrary count may be burdensome on some platforms and therefore it is better to leave it undefined behavior.
Why not unspecified behavior
If we look at the Rationale for International Standard—Programming Languages—C it says:

Unspecified behavior gives the implementor some latitude in translating programs. This latitude
  does not extend as far as failing to translate the program, however, because all possible behaviors
  are "correct" in the sense that they don't cause undefined behavior in any implementation.

So there must have been a case or still exists a case where the behavior is not correct and would have bad issues.
Comment: This is a good answer, but I'd appreciate details on why choose *undefined* behavior rather than *unspecified* behavior (i.e. the result will have some value, but no velociraptors are involved) .
Comment: @sbabbi: Making it undefined behavior means that a compiler can optimize `int shift(int v, int n) { if (n >= 32) v=0; return v<=32) v=0;`, regardless of what the processor will do with the request, since if `n` is 32 or larger the C standard says the compiler can do whatever it wants.
Answer: Example to why is this undefined behavior better than setting the result to zero.
Typically a CPU has a single instruction that does the shift.  If that instruction was required to compare against an upper bound, that would take more circuitry and slow the shifting.  Instead, many CPUs' simply use the Least Significant Bits of the shift to determine how much to shift.
// Example - not specified behavior  (assume 32-bit int)
y = 1 << 34;
// same as
y = 1 << 2;

The first PCs used an 8/16 bit processor that used the least 8 bits to determine the shift, so it indeed would shift in zeros once the shift count was more than the `int width, yet less than 256.  The problem with this was that each shift took 1 clock tick.  So in a worst-case, the simple shift command could take 255 clock ticks to perform.  Of course, after 16 ticks, nothing but 0 were shifted.  This long worst case instruction was not interruptible!  Thus making that processor's worst case Interrupt latency far worst than the competition.  Intel did not make this mistake again.
Comment: I thought that even on the 80386 which added a barrel shifter for most shift/rotate instructions, the rotate-through-carry instructions behaved iteratively and did not mask the lower bits the way the barrel-shifter-based instructions do.
Comment: @supercat  See http://css.csail.mit.edu/6.858/2014/readings/i386.pdf "220.127.116.11 Shift Instructions" --> "Only the low order 5 bits of CL are used."  For _Rotate Through Carry_, the shift is only 1 bit.  Should code mask the lower bits of the shift or not isn't a function of the 386's instructions.  It is a function of the compiler.  A compiler could not mask, as you suggest or it could mask as I suggest.  The C spec allows either.  A _good_ compiler will limit the shift count somehow, else a simple `x = some_large_value(); y >> x;` would take an excessive about of time.
Comment: I must be misremembering something in that regard, though maybe there was incompatibility in the way different machines handled `RRC` and `RLC` on 8- and 16-bit operands.  I do remember I never liked Intel's ignoring of the upper bits of shift amounts.  On machines that allow it, computing the upper half of a word left-shifted by some amount via `upper = dat >> (WORDSIZE-amt);` is unlikely to be any slower than `upper = dat >> (WORDSIZE-amt-1) >> 1;`.  Maybe some compilers could optimize the latter into the former, but it would be better yet if there were standardized intrinsics which...
Comment: ...meant "yield a result equivalent to shifting the operand by one bit, (unsigned)N times"; compilers could then take advantage of whatever architectural features a particular platform supported to achieve that.
Comment: @supercat As C does not have a rotate operand nor any any concept of "carry bit", the best a platform can do is provide its own 1) `rotate(var, shift)`, 2) `int shift_with_carry(car, shift, carry_in)` 3) `shift_right(var, shift)` that considers range outside `int` width 4) etc. for unquestioned access to that platform's nifty shift variants.  Or course that may need an alternative standard code for portability.  I suspect C's undefined behavior for large shifts is precisely to cope with the wide variety of hardwares' variant shifts operations and the flags they set.
Comment: I think the reason C makes excess shifts totally "undefined behavior" rather than "indeterminate values" has to do with the INMOS Transputer, where a shift of -1 would execute as an uninterruptible instruction that took many minutes to complete (the hardware would loop with a 32-bit unsigned counter); the authors of the Standard saw no reason to distinguish between shifting by an amount one bit "too large", versus shifting by an amount which was four billion bits too large, especially since when the standard was written it was expected that platforms that could cheaply offer guarantees...
Comment: ...beyond what the Standard required were free to do so; further, I don't think the authors of the Standard saw much benefit to having a standard say that on a 32-bit machine, a shift value 0-31 will work properly, a value of 32 may behave as 32 or 0, and values that are much larger may have undesired side-effects, versus simply saying that anything over 32 was Undefined Behavior, but I would suggest that there are some common situations where code with such a behavioral guarantee could execute faster than code which had to absolutely positively avoid any attempt to shift by 32 or more.