Session not refreshing until the cache is cleared

Question: I am trying to store data entered in the drupal custom form in order to display it in next page using session.
For the first time it is running perfectly, but for the second time it contains the same value in session which was entered previously.
After clearing caches (admin/config/development/performance/clear caches) it is working for the first time, retains the same problem.
/**
 * @file
 * Contains \Drupal\resume\Form\PaymentForm.
 */
namespace Drupal\resume\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\SessionManager;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

class PaymentForm extends FormBase {

    /**
* @var \Drupal\user\PrivateTempStoreFactory
*/
protected $tempStoreFactory;

/**
* @var \Drupal\Core\Session\SessionManagerInterface
*/
private $sessionManager;

/**
* @var \Drupal\Core\Session\AccountInterface
*/
private $currentUser;

/**
* @var \Drupal\user\PrivateTempStore
*/
protected $store;

public function getFormId() {
    return 'payment_form';
}

public function __construct(PrivateTempStoreFactory $temp_store_factory, SessionManagerInterface $session_manager, AccountInterface $current_user) {
    $this->tempStoreFactory = $temp_store_factory;
    $this->sessionManager = $session_manager;
    $this->currentUser = $current_user;

    $this->store = $this->tempStoreFactory->get('mymodule.storename');
}

public static function create(ContainerInterface $container) {
    return new static(
        $container->get('user.private_tempstore'),
        $container->get('session_manager'),
        $container->get('current_user')
    );
}

  public function buildForm(array $form, FormStateInterface $form_state) {

if ($this->currentUser->isAnonymous() && !isset($_SESSION['session_started'])) {
    $_SESSION['session_started'] = true;
    $this->sessionManager->start();
}

$form['pay_first_name'] = array(
    '#type' => 'textfield',
    '#title' => ('First Name'),
);

$form['pay_last_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Last Name'),
);

$form['address'] = array(
    '#type' => 'textfield',
    '#title' => ('address'),
);

$form['email_id'] = array(
    '#type' => 'textfield',
    '#title' => t('email_id'),
);

$form['actions']['#type'] = 'actions';
$form['actions']['payment_submit'] = array(
  '#type' => 'submit',
  '#value' => $this->t('payment'),
  '#button_type' => 'primary',
);
return $form;
}

  public function submitForm(array &$form, FormStateInterface $form_state) {

  $payment_array['first_name'] = $form_state->getValue('pay_first_name');
  $payment_array['last_name'] = $form_state->getValue('pay_last_name');
  $payment_array['line1'] = $form_state->getValue('address');
  $payment_array['email'] = $form_state->getValue('email_id');

  $session = \Drupal::service('user.private_tempstore')->get('resume');
  $session->set('payment_details', $payment_array);
    $form_state->setRedirect('payment_redirect.form');
    return;
  }
}

Comment: Do you clear the session after a successful purchase? Otherwise the data will persist until your session expires (by default approx. 2 weeks).
Comment: I have tried that too, Even though it is not refreshing.
Code :      $session = \Drupal::request();
                $session->getSession()->clear();
Thanks in advance
Answer: What you store in $session is not a session. A private tempstore is a different kind of storage for large amount of data, too big to have it preloaded in memory.
If you want to store a small amount of data, that does not exceed a few Megabytes, then use a session. You find the session attached to the request object:
$session = \Drupal::request()->getSession();

In a form you can get the session from the injected request stack. Use $session->set() and $session->get() to store:
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $session = $this->getRequest()->getSession();
    $session->set('payment_details', $payment_array);

  }

and retrieve the data:
  public function buildForm(array $form, FormStateInterface $form_state) {

    $session = $this->getRequest()->getSession();
    $payment_array = $session->get('payment_details', []);

    // make sure the form is not cached and this code is run on every request
    $form['#cache']['max-age'] = 0;

  }

You don't need to start the session. This is done by Drupal in the background for each request:
When finishing the response, Drupal checks if there is new session data and then starts a session to store the data (which implies setting a cookie). When the user with the session cookie comes back, then the session is restored from the database and is attached to the request.
More info Drupal 8 and Session Management
Comment: Is there any library should be used for this?? I have tried this but while submitting the from it displays "website encounters an error"... Thanks in advance.
Comment: No, only the two lines of code, no services or libraries necessary. The request stack is already injected by extending FormBase. The error could be a typo, enable full  error messages in /admin/config/development/logging
Comment: The website encountered an unexpected error. Please try again later.
Error: Call to a member function getCurrentRequest() on null in Drupal\resume\Form\PaymentForm->submitForm() (line 128 of modules/resume/src/Form/PaymentForm.php).
Comment: Found the problem, the request stack is not set when you run submit. By using getRequest() it should work, see the edit.
Comment: Same problem....   Error: Call to a member function getRequest() on null in Drupal\resume\Form\PaymentForm->submitForm() (line 128 of modules/resume/src/Form/PaymentForm.php).
Comment: I've tested getRequest() in a form submit inside of a form class, it worked. But when it doesn't work for you, use the static `$session = \Drupal::request()->getSession();` instead, this should work everywhere.
Comment: By using this static, for the first time it is working but for the next time data's were not updating, What to do for this problem? please guide me...
Comment: You mean when you try to get the session data in buildForm()? This is probably because the form is coming from the cache and your code is not run. Try to disable caching of the form: `$form['#cache']['max-age'] = 0;`
Comment: I am trying to display the stored value in a custom block..
Code:
 public function build() {
 $session = \Drupal::request()->getSession();
  $array = $session->get('payment_details');
$data  = '.$array['first_name'].';
return array(
            '#type' => 'inline_template',
            '#template' => '{{ content }}'.$data,
            '#context' => [
                'content' => ''
        ]);
Comment: then you have to disable caching of the block `return [ ..., '#cache' => ['max-age' => 0]];`
Comment: wow super, it is working and updating in the block for both admin and anonymous, Thank you so much!!!!!
But one thing, I have printed that stored value while loading the block. For admin it is updating but for anonymous user it is not updating. Why it is so??
Comment: For anonymous users without a session the block still gets cached by the page cache. But as soon as the user inputs data and gets a session cookie the block cache max-age should work.
Comment: getRequest should be available as `$this->getRequest()` unless you did not extend FormBase.
Comment: I just checked two custom forms I had built, and this works inside of `buildForm()`: `$uri = $this->getRequest()->getRequestUri();`.
Comment: Only due to page cache the printed array value is not updating ??
Can I use the same session array for further process (append the array data to API). It should be updating right ???
Comment: This is not drupal specific, anonymous traffic without a cookie is cached in many places, most times the request is not even leaving your pc, because the browser loads the page from its cache.
Comment: All code in Drupal share the same session. So you should be careful to name the session parameter to have no conflicts with other modules.


How to fetch html string of XPath results?

Question: Considering this code:

foo

bar

If I want to fetch all the values of divs with class a, I'll do the following query:
$q = $xpath->query('//div[@class="a"]');

However, I'll get this result:
foo
bar

But I want to get the actual value including the children tags. So it'll look like that:
foo

bar

How can I accomplish that with XPath and DOMDocument only?

Solved by the function provided here.
Comment: [See](http://stackoverflow.com/questions/7156054/getting-the-inner-html-of-a-domelement-in-php) [countless](http://stackoverflow.com/questions/4879946/domdocument-savehtml-without-html-wrapper) [other](http://stackoverflow.com/questions/3615389/innerhtml-in-xpath) [questions](http://stackoverflow.com/questions/2087103/innerhtml-in-phps-domdocument) asking the same thing.
Answer: You can try to use
$xml = '<?xml version=\'1.0\' encoding=\'UTF-8\' ?>
    <root>

foo

bar

    </root>';

$xml = simplexml_load_string($xml);
var_dump($xml->xpath('//div[@class="a"]'));

But in this case you will have to iterate objects.
Output:
array(2) {
  [0]=>
  object(SimpleXMLElement)#2 (2) {
    ["@attributes"]=>
    array(1) {
      ["class"]=>
      string(1) "a"
    }
    [0]=>
    string(3) "foo"
  }
  [1]=>
  object(SimpleXMLElement)#3 (2) {
    ["@attributes"]=>
    array(1) {
      ["class"]=>
      string(1) "a"
    }
    ["div"]=>
    string(3) "bar"
  }
 }
Comment: Sorry, you were right. However, it has no the `id` attribute (or am I missing something?). I mean, the ``. And the structure is pretty complex. Even if it was showing me the `id` attribute, I'd still have to transform all that stuff to a plain text.
Comment: But it also retrieves only bar in my case.
Answer: PHP DOM has an undocumented '.nodeValue' attribute which acts exactly like .innerHTML in a browser. Once you've used XPath to get the node you want, just do $node->nodeValue to get the innerhtml.
Comment: That's **exactly** what I do. But nodeValue does not interpret the children as actual value of the element.
Answer: Try something like:
$doc = new DOMDocument;
$doc->loadHTML('
Your HTML here.
');
$xpath = new DOMXpath($doc);
$node = $xpath->query('//div[@class="a"]')->item(0);
$html = $node->ownerDocument->saveHTML($node); // Get HTML of DOMElement.


True/False - Are these sets and sequences open sets or closed sets?

Question: Question: Suppose $(X,d)$ is a metric space and  $\{x_n\},\{y_n\}$ are sequences in $X$. $\{x_n\}$ has no convergent sub-sequence in $X$ and $\{y_n\}$ has only one convergent subsequence in $X$:- $\{y_{m_n}\}$ . Let $S$ be a subset of $X$.
Then,which of the following is true:
$1$.  If $z \in S$ is an isolated point of $S$, then it is possible that $z$ may be a boundary point of $S$.
$2.$ Every subset $H$ of $\{x_n\}$ consists entirely of isolated points of $H$ and is hence open in $X$.
$3.$ Every subset $H$ of $\{x_n\}$ consists entirely of isolated points of $H$ and is hence closed in $X$.
$4.$ The set $\{y_{m_n}\}$ is closed in $\{y_n\}$.
$5.$ The set $\{y_n\} - \{y_{m_n}\}$ is open in $X$.
Attempt:
$1.$ True. An isolated point in $S$ may be at $0$ distance from $S^c$. Example: the set of Natural numbers in Real Numbers.
$2.$ False. Every subset $H$ of $\{x_n\}$ consists entirely of isolated points of $H$ and each isolated point $i$ is open in $H$ and so is their union. . But, $\bigcup i$ may not be isolated in $X$ and hence may not be open. ( As then, the set itself becomes a part of the boundary of the set).
$3.$ True. Since, Every subset $H$ of $\{x_n\}$ consists entirely of isolated points in $X$, they possess no accumulation point in $X$. Therefore, no element from $H^c$ can be at $0$ distance from $H$ and hence, cannot be a part of the boundary. Thus, the boundary is included in $H$ itself and is hence closed in $X$.
$4.$ False. The limit point of $\{y_{m_n}\}$ may not belong to $\{y_{m_n}\}$. Therefore, it may not be closed.
$5.$ False. The set $\{y_n\} - \{y_{m_n}\}$ consists entirely of isolated points of $y_n$ since $y_n$ has only one convergent sub-sequence. From point $3$, these set of isolated points are closed in $X$.
Am I correct in my attempt? Thanks a lot for your help.
Comment: I don't see any flaws. In 1., when you say $S=\mathbb N$, you should also specify $X$.
Comment: @MathMan Then definitely not. Consider $H = \mathbb N$ in $X = \mathbb R$.
Comment: @amsmath thank you
Comment: @amsmath Would it be right to say that every subset $H$ consists entirely of points which are isolated in $X$?
Comment: What do you mean by "isolated in $X$"?
Comment: $dist(h, X-\{h\})>0~\forall~h \in H$
Comment: Is it not the case that $(y_n)$ must be constant? Since any subsequence of $(y_{m_n})$ (with any finite collection of excised $(y_n)$ terms added back in) must also be convergent, does it not follow?
Comment: @Bungo if $y_{m_n}$ is the constant sequence, then, it's possible
Answer: I'm not convinced by your answer to 4 or 5. Here's why:
Claim: $(y_n)_n$ is constant.
Proof: If $\# \{y_{m_n}\}_n \geq 2$, then we can create a different convergent subsequence of $(y_n)$ by simply taking out all the terms $(y_{m_n})_n$ which have a particular value, provided that there are infinitely many terms left over. (Such a value is guaranteed by the infinite pigeonhole principle).
Likewise, if there is a value $a = y_N \in \{y_{n}\}\backslash \{y_{m_n}\}$, then adding on this $N$th term to $(y_{m_n})$ yields another distinct convergent sequence! $\qquad\square$
From this, it's clear that
4) $\{y_{m_n}\}=\{\ast\}=\{y_{n}\}$ is open in $\{y_{n}\}$, and
5) $\{y_{n}\}\backslash \{y_{m_n}\}=\emptyset$ is open in $X$.
$ $
Otherwise, I have no problems!


How does Drush figure out modules that have updates?

Question: How does Drush get information about installed and disabled modules and  updates for them?
I'm facing issues since drush is trying to update modules that AREN'T in the modules directory nor in the system table anymore.
I had to remove them since they were causing problems.
But in the end I got nothing but more problems.
How could I get drush to understand these do not exist anymore and do not need to be a)checked and b)especially not updated.
I've tried drush rr to "make it clear" but it keeps trying these causing errors that crash all other updates etc.
Update:
These the modules.
Simplenews [simplenews-7.x-1.1],
Simplenews Bounced Mail Handler [Simplenews_BMH-7.x-2.x-dev],
Simplenews Statistics [simplenews_statistics-7.x-1.0-alpha1]
I can confirm that I can remove the simplenews rows from system table but they come back as soon as I run:              drush rr OR drush pm-list --pipe --type=module --status=disabled--no-core
Comment: Have you tried clearing Drush's cache? `drush cc drush` ...
Comment: Another thought... are you using drush aliases? e.g. `drush @stage up -n` Is it possible you are looking at a different env that still has those modules installed? Similarly, you could have this if you are using a multisite
Comment: drush cc drush was a new command but sadly it didn't have any effect. And no, there is no multisite or aliases.
Comment: Whoopsie. This was my mistake. I don't know how but there was a folder undes modules that had a tens of modules from some other installation. Therefore drush was picking them constantly. Removed and solved.
Answer: Use pm-uninstall to completely remove a module from your system.
If you have already deleted the code for the modules in question, the safest and best thing to do is put the code back and run pm-uninstall.  If you just want to get rid of the reference to the module in the system table, you could take matters into your own hands, and just do this:

drush sqlq "delete from system where name in ('moduleone', 'moduletwo');"

If you do that, though, then the module won't have a chance to remove any other unneeded assets that would be deleted if you used pm-uninstall.
Comment: Hi! i'm not sure if this works currently since I've removed those already "identified" modules. I believe that in order to remove them completely Drush would need files in place. But I'll give it a try...
Comment: Updated the answer.


How to assign value of Text Asset to String and display it in Text()?

Question: I want to modify data from a text asset (.txt file) with a function and display it in a Text() widget. myFunction Takes datatype String as parameter and return datatype String.
I've read the documentation. Loading Asset from Image, this didn't worked. I've also tried solution from Suragch's Answer.
This maybe is a case where I should use FutureBuilder but I'm not able to understand how to make it work here (I'm new). I'm going to use another function to modify the data from file and then display.
This is one of the things I tried:
Widget build(BuildContext context) {
  Future<String> aloadAsset(BuildContext context) async {
    return await DefaultAssetBundle.of(context).loadString('assets/help.txt');
  }

  String helps = myFunction(await aloadAsset(context));
  return Scaffold(
    body: Text(helps)
  );
}

When assigning value from await aloadAsset(context) to String, I get these errors: Unexpected text 'await'. & A value of type 'Future<String>' can't be assigned to a variable of type 'String'.
Answer: This is how to read the text from your file, you have to modify your Build function to include a FutureBuilder. Then you have to move the aloadAsset function out of the build:
@override
Widget build(BuildContext context) {
  return FutureBuilder(
      future: aloadAsset(),
      builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
        if(if(snapshot.hasData)){
            return Scaffold(
              body: Text(snapshot.data)
            );
        }
            return Scaffold(
              body: Text('No Available data') //This will be returned in case you didn't receive data yet or in the case of a file error.
            );
      }
}

Future<String> aloadAsset(BuildContext context) async {
     return await DefaultAssetBundle.of(context).loadString('assets/help.txt');

}

Comment: kindly explain `if(snapshot.connectionState == ConnectionState.done)` vs `if(snapshot.hasData)`. Which is better to use?
Comment: for your case it is better to use the latter : if(snapshot.hasData) to handle the event of wrong file name or nonexistent one. I will edit the answer.
Comment: I am not the OP, just read your answer and wanted to clarify; thanks for the edit.
Comment: I know, and by the way thank you for notifying me for this issue which led me to correct my answer to suit the post case.
Comment: Thanks for the answer. Can you explain how can I assign the data from "assets/help.txt" file to a String?
Comment: You can define a class-level var and assign the value to it inside `aloadAsset()`
Answer: await keyword can only be used in an async function. What you can do here is:
Make the widget as StatefulWidget and then in the state class:
String helps = "";

in the initState() method:
aloadAsset();

and change your function to:
aloadAsset() async {
    helps = myFunction(await rootBundle.loadString('assets/help.txt'));
    setState((){});
  }

Don't forget to add the import
import 'package:flutter/services.dart' show rootBundle;


Rx.Net : perform asynchronous side-effect when subscription is disposed

Question: I have a device that communicates with the computer via the serial port. Upon sending a "START" command, the device responds with an acknowledgement and it starts monitoring some external activity. It then asynchonousely transmitts some messages on the serial port depending on that external activity. When the device receives a "STOP" command, it responds with an acknowledgement, and then it stops sending more messages (representing the external activity).
I've implemented the Start/Stop commands with cold observables, that perform side-effects (send command on the serial port), and emits a single Unit.Default if an ackowledge is received on the SerialPort. I want to construct an IObservable that emits messages coresponding to the external activity, and perform "START" side-effect when it is subscribed to, and "STOP" side-effect when the subscription is disposed.
"START" is easy, i just have to do a `SelectMany', but i dont know how to perform "STOP".
  class MonitoringDevice
{
    private SerialPort _sp;
    private IObservable<byte> _receivedBytes;

    public IObservable<ExternalActivity> ActivityStream { get; }

    public  MonitoringDevice()
    {
        _sp = new SerialPort("COM1");
        _receivedBytes = Observable
                         .FromEventPattern<SerialDataReceivedEventHandler, SerialDataReceivedEventArgs>(
                          h =>
                          {
                              _sp.DiscardInBuffer();
                              _sp.DataReceived += h;
                          },
                          h =>
                          {
                              _sp.DataReceived -= h;
                          })
                         .SelectMany(x =>
                         {

                             byte[] buffer = new byte[1024];
                             var ret = new List<byte>();
                             int bytesRead = 0;
                             do
                             {
                                 bytesRead = _sp.Read(buffer, 0, buffer.Length);
                                 ret.AddRange(buffer.Take(bytesRead));
                             } while ((bytesRead >= buffer.Length));
                             return ret;

                         })
                         .Publish()
                         .RefCount();

        ActivityStream = StartMonitoringAsync()
                         .SelectMany( _receivedBytes.ToActivity());
                         // we need to execute StopMonitoringAsync
                         // when a subscription to ActivityStream is disposed

        _sp.Open();
    }

    private IObservable<Unit> StartMonitoringAsync()
    {
        return Observable
               .Create<Unit>(
                obs =>
                {
                    _sp.Write("START");
                    return _receivedBytes
                           .ToAcknowledge()
                           .FirstAsync()
                           .Timeout(TimeSpan.FromMilliseconds(1000))
                           .Subscribe(obs);
                });
    }

    private IObservable<Unit> StopMonitoringAsync()
    {
        return Observable
               .Create<Unit>(
                obs =>
                {
                    _sp.Write("STOP");
                    return _receivedBytes
                           .ToAcknowledge()
                           .FirstAsync()
                           .Timeout(TimeSpan.FromMilliseconds(1000))
                           .Subscribe(obs);
                });
    }

}

ExternalActivity is just a POCO.
ToAcknowledge is an extension method returning an IObservable that emits Unit.Default when the device transmitts an acknowledgement. - this is working as expected;
ToActivity is an extension method returning an IObservable that parses the incomming serial data and emits ExternalActivity objects. - this is working as expected;

Edit : Added implementation for ToAcknowledge and ToActivity extension methods.
public static IObservable<Unit> ToAcknowledge(this IObservable<byte> source)
    {
        return source.Buffer(3, 1)
               .Where(bfr => bfr.SequenceEqual(new byte[] { 65, 67, 75 })) // ACK
               .Select(x => Unit.Default);

    }

    public static IObservable<ExternalActivity> ToActivity(this IObservable<byte> source)
    {
        return source
               .Publish(ps => ps.Buffer(ps.Where(x => x == 1),             // SOH
                                           bo => ps.Where(x => x == 4)))   // EOT
               .Select(bfr => bfr.Take(bfr.Count - 1).Skip(1))
               .Where(bfr => bfr.Count() == 12)
               .Select(bfr =>
               {
                   var timestamp = BitConverter.ToInt64(bfr.Take(8).ToArray(), 0);
                   var id = BitConverter.ToInt32(bfr.ToArray(), 8);
                   return new ExternalActivity(timestamp, id);
               });
    }

Comment: Hmm I dont think in this case STOP and Dispose should be conflated to the same thing. I think you want to model Stop as a command. And as such accept that it can succeed, fail, timeout etc. Disposal is then just purely, disconnecting and trashing the subscription.
Comment: Could you please show the code for `ToAcknowledge` & `ToActivity`? I can't give you an answer without them.
Comment: @Enigmativity - See my edited question.
Comment: @LeeCampbell - That's a very good observation. There will be Start and Stop buttons on te UI side, so is logical to have those trigger some commands and react on success, fail, etc,  as you pointed out. There's actually no need to trigger those commands when subscribing/disposing to/from 'ActivityStream'. I wasn't seeing the big picture. If you could post your comment as an answer i will accept it. Thank you!
Answer: You can modify your StartAsync to be this:
private IObservable<Unit> StartAsync(Action unsubscribe)
{
    return
        Observable
            .Create<Unit>(o =>
            {
                var subscription =
                    Observable
                        .Timer(TimeSpan.FromSeconds(1))
                        .Select(_=> Unit.Default)
                        .Subscribe(o);
                return new CompositeDisposable(
                    subscription,
                    Disposable.Create(unsubscribe));
            });;
}

Then you can inject in any Action unsubscribe you like.
Try testing with this code:
var subscription =
    StartAsync(() => Console.WriteLine("Done"))
    .Subscribe();

Thread.Sleep(3000);

subscription.Dispose();

You'll see "Done" written to the console after 3 seconds.
Comment: Thanks. The action to be performed on subscription disposal is an asynchronous one. It is implemented as a cold observable that performs side-effect (send command through SerialPort) and emits single value on response arrival. In my code example it's emulated with an `Observable.Timer`. I need to await this observable inside main subscription disposal.
Comment: @francezu13k50 - Could you not put an observable within the `Action`? However, it's never a good idea to rely on a timer to allow an asynchronous action to complete. Rx is very good at handling these situations properly. Can you show your real code?
Comment: I dont know how to put an observable within the `Action`. The `Observable.Timer` is there only to simulate the asynchronous response of the device, in effect to a "STOP" command; i'm not relying on a timer for action completeness, it's actually the device acknowledge that indicates the operation success.
Comment: @francezu13k50 - Then you should write your code to respond to the acknowledgement. Can you show your real code?
Comment: I've edited my question and added a more detailed code example. I hope that clarifies my situation.


Erro ao importar módulo em JavaScript

Question: Estou tendo um problema ao importar um modulo em JavaScript. Estou utilizando um servidor HTTPS em Python para criar meus sites.
Arquivo: modules/index.js:
export function soma (c,d){
    return c + d
}

Arquivo index.js:
import {soma} from "./modules/index"

let value = soma(3,5)

console.log(value)

Quando vou no console, aparece este erro:

Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

Como posso resolvê-lo?
Comment: A sua function de soma está errada!
Answer: Assumindo que seu index.js está sendo importado em uma tag script, creio que o erro seja por faltar a especificação do tipo MIME do arquivo que você está tentando importar.
Nesse caso, tente fazer a importação informando a extensão do arquivo .js no seu index.js:
//                                    ↓↓ informe a extensão
import { soma } from "./modules/index.js";

let value = soma(3, 5); // 5

console.log(value);

Strict MIME type checking is enforced for module scripts per HTML spec.

Para não receber esse erro, é obrigatório informar a extensão do arquivo .js para scripts do tipo type="module".
Com a excelente ajuda @AugustoVasques que nos forneceu um bom link para consulta, alguns pontos tem que ser levados em conta:

a preferência é que módulos receberem a extensão .mjs pois na fase de projeto lints olham essa extensão e tratam o arquivo adequadamente e interpretadores standalone procuram preferencialmente por essa extensão.

Nesse caso, para tag script com type="module", é melhor reescrevermos a extensão do arquivo para o tipo module. Neste caso, vamos mudar de .js para .mjs no código:
//                                    ↓↓↓ informe a extensão .mjs
import { soma } from "./modules/index.mjs";

let value = soma(3, 5); // 5

console.log(value);

Comment: @AugustoVasques Boa informação, vale a pena editar a reposta e acrescentar esse extra.
Comment: Se pudesse dava outro +1

Erro ao importar módulo em JavaScript


Guessing at pivots

Question: I'm trying to get a better idea of the intuition behind finding pivotal quantities.  In the Casella & Berger statistical inference text, we have a $beta(\theta,1)$ pdf, $f_X(x)=\theta x^{\theta-1}$, $0 < x < 1$.  The text says that $X^\theta$ is a good guess for a pivot, and then proceeds to verify that it is a pivotal quantity.  My question is, what is the intuition that would lead one to believe that $X^\theta$ is a good guess?
Answer: In that case, just by looking at the density we can see the cdf (within the region where the density is nonzero) is $x^\theta$.
i.e. we know that $P(X\leq x) = x^\theta$ for $0<x<1$
But we also know that for continuous r.v.s $F_X(X)\,$* is standard uniform.
*  (here suppressing the dependence on parameters, but they're still there of course)
So in the cases where we can easily write our pivot $Q=t(X;\theta)=F_X(X)$ we will automatically have that the transformed variable will have a distribution that doesn't depend on $\theta$ (since it's just $U(0,1)$).
Answer: Because the transformation theorem.
Equivalent to the answer above, but less pretty.
Define $Y = X^{\theta}$. Then $X = Y^{1/\theta}$, then
$$
f_Y(y) = \frac{\theta}{\theta} [y^{1/\theta}]^{\theta-1}y^{1/\theta - 1} = 1.
$$
It's free of $\theta$.

Guessing at pivots


Can we get the title by node ID in a template file?

Question: Is it possible to retrieve the title by node ID, based on the interface language.
I found drupal_url('/node/101', {absolute: true}).
I wish to have a similar function that returns a title passing a node ID.
Answer: Anyway I had to do this in preprocess function
function template_preprocess_field(&$variables) {
    $lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $variables['node_101'] = \Drupal\node\Entity\Node::load(101)->getTranslation($lang)->getTitle();
    $variables['node_75'] = \Drupal\node\Entity\Node::load(75)->getTranslation($lang)->getTitle();
    $variables['node_103'] = \Drupal\node\Entity\Node::load(103)->getTranslation($lang)->getTitle();
}


Multiple imputation with the Amelia package

Question: I have a general question about the Amelia package. I'm no mathematician or statistician, but I had to use R and impute and analyze some data, and Amelia showed results that fitted my expectations. I'll have to defend my choice soon, but I haven't totally grasped what Amelia does.
I'm particularly interested in a simple as possible explanation in how Amelia imputation works. I've read that it uses a bootstrapping-based algorithm, but how does it chose the values?
The data had mainly value >0 (chemical concentrations, water temperature and pH-value).
Comment: I'd start with the package author's website, which has links to papers about the operation of Amelia.
[Gary King at Harvard](http://gking.harvard.edu/amelia/) Also, the [vignette](http://cran.r-project.org/web/packages/Amelia/vignettes/amelia.pdf) (pdf) has a fairly comprehensive introduction to the package and algorithm.
Comment: Thank you for your fast answer. I've allready read the vignette, but as I said I'm no mathematician. It's just to technical for me. I guess a simplification of 2.2 from the vignette, including the EM algorithm with bootstraping would be good.
Answer: Amelia assumes that the data follow a multivariate normal distribution, so all information about the relations in the data can be summarized by just means and covariances. When data are incomplete, Amelia uses the well-known EM algorithm to find corrected estimates of the means and covariances. See Little and Rubin (2002) for more detail.
In their original form the EM estimates cannot be used to create multiple imputations, as the estimates do not reflect the fact that they have been estimated from a finite sample. In order to solve this, Amelia first takes m bootstrap samples, and applies the EM-algorithm to each of these bootstrap samples. The m estimates of means and variances will now be different. The first set of estimates is used to draw the first set of imputed values by a form of regression analysis, the second set is used to calculate the second set of imputed values, and so on.
As Amelia assumes a multivariate normal distribution, it will work best when your data are approximately normally distributed (possibly after a transformation), and when the statistics you calculate from the data in your complete-data analysis are near the center of the distribution, like means, modes or regression weights.
Comment: @Martin. Yes, that's right. The imputed value is calculated as the predicted value + a random draw from the residual distribution.
Comment: I'm no expert, but it sounds like this may raise some caution flags, since the input values are constrained, while the normal distribution is not. You could thus end up with negative imputed values, etc. People often use normal approximation when it's not totally appropriate, and get lucky because things are far enough away from the boundaries, but the OP may need to look closely at the results.
Comment: @Wayne: Thats true, and it indeed happened. But you can set boundaries with Amelia, which I did after I had this problem.
Comment: @Stef van Burren: Thank you! Especially the information that it is a regression analysis helps me. So to summarize it for a amateur like me: The bootstraping creates samples based on the distribution, and the EM-algorhytm fits those samples to the original data. The decision for which value will be put in place of the missing value is made by regression.
Comment: Great explanation, upvoted. Can the random draw from the residual distribution be omitted? Would that be a sensible thing to do if all I care about is filling in missing values, and no further statistical analysis is required?


UPDATE OR INSERT function when uniqueness is determined by the combination of two columns

Question: Expanding on the usual INSERT OR UPDATE conondrum
I have a function that serves normal UPDATE OR INSERT, and it looks like this:
CREATE OR REPLACE FUNCTION updateslave ( varchar(7), smallint ) RETURNS void AS
$$
 BEGIN
   LOOP
     UPDATE consist SET
       master = $1,
       time = now() WHERE slave = $2;
     IF found THEN
       RETURN;
     END IF;
     BEGIN
       INSERT INTO consist(master, slave, time) VALUES ( $1, $2, now() );
       RETURN;
     EXCEPTION WHEN unique_violation THEN
       -- do nothing, then loop and retry
     END;
   END LOOP;
 END;
##
LANGUAGE plpgsql;

Now, the issue is that i'm trying to rewrite it for a similar operation in a different, table. However, the difference is that in this other table there is no single unique column, but the combination of two columns only exists in one row. Is it possible to declare unique_violation based on the combination of two columns instead?
For the sake of keeping examples simple, let's assume that the table looks exactly the same as the one i use the above function for, but with master and slave being the two columns that together produce uniqueness:
 Column |            Type             |                  Modifiers                   | Storage  | Description
--------+-----------------------------+----------------------------------------------+----------+-------------
 master | character varying(7)        | not null default 'unused'::character varying | extended |
 slave  | smallint                    | not null                                     | plain    |
 time   | timestamp without time zone | default now()                                | plain    |

Answer: The best approach is to define a unique constraint on the table, that way no matter how the update happens, ie if your proc is used or not, everything is OK.
The easiest way to do that is to create a unique index over the two columns:
create unique index any_mame on mytable(col1, col2);

You can also alter the table to add a unique constraint, but there's not much difference.
Comment: In other words, if the unique constraint is created, it would throw a unique_violation in this case, hence i can use basically the same function? (with table and column names altered, obviously)


Spectral radius of a matrix in a Banach algebra

Question: Let $A$ be a $n \times n $ matrix.
Let $S(A)= \{ T: T= S^{-1} A S\ , $ for some invertible  $ n\times n $ matrix $ S\}$.

Show that there exists $T \in \overline{S(A)}$ such that $\| T\|= \rho(T)= \rho(A) $, where $\rho(A)= \sup \{ |\lambda| : \lambda \in \sigma(A)\}$ is the spectral radius of $A$.

Is it possible to find $T \in S(A)$ such that  $\| T\|= \rho(T)= \rho(A) $?

Things I know:
We know that the set of $n \times n$ matrices, $\mathbb{M}_{n \times n}$, can be identified as a  Banach algebra.
Let $A \in \mathbb{M}_{n \times n}$.
By the spectral radius formula, $\rho(A) = \lim_{n \rightarrow \infty}\| A ^n\|^{1/n}$.
I'm not sure how to construct such $T \in \overline{S(A)}$, as I'm not sure what the closure of $S(A)$ is like.
Thank you in advance!
Answer: Without loss of generality we may assume that $A$ is in canonical Jordan form,  namely
$$
  A =
  \pmatrix{
    A_1 & 0 & \ldots & 0 \cr
    0 & A_2 & \ldots & 0 \cr
    \vdots & \vdots & \ddots  & \vdots  \cr
    0 & 0 & \ldots &  A_k
  },
  $$
where each $A_i$ is a Jordan block, that is
$$
  A_i=\pmatrix{
    \lambda _i & 0 & 0 & \ldots & 0 & 0 \cr
    1 & \lambda _i & 0 & \ldots & 0 & 0 \cr
    0 & 1 & \lambda _i & \ldots & 0 & 0 \cr
    \vdots & \vdots & \ddots & \ddots  & \vdots  & \vdots  \cr
    0 & 0 & 0 & \ldots & \lambda _i &  0 \cr
    0 & 0 & 0 & \ldots & 1 &  \lambda _i
  }
  $$
The Jordan decomposition is not guaranteed to take place over an orthonormal basis but, by conjugating $A$ with a
matrix sending its Jordan basis to the canonical basis of $\mathbb C^n$, we may assume that $A$ is indeed in Jordan form
relative to the orthonormal canonical  basis.
Choose a parameter $\alpha >0$ and, for each Jordan block $A_i$, consider the invertible matrix
$$
  U^\alpha _i=
  \pmatrix{
    \alpha  & 0 & \ldots & 0 \cr
    0 & \alpha ^2 & \ldots & 0 \cr
    \vdots & \vdots & \ddots  & \vdots  \cr
    0 & 0 & \ldots &  \alpha ^p
  },
  $$
where $p$ is the size of $A_i$, and define $U^\alpha $ to be the block-diagonal matrix
$$
  U^\alpha  =
  \pmatrix{
    U^\alpha _1 & 0 & \ldots & 0 \cr
    0 & U^\alpha _2 & \ldots & 0 \cr
    \vdots & \vdots & \ddots  & \vdots  \cr
    0 & 0 & \ldots &  U^\alpha _k
  }.
  $$
We then have that $U^\alpha A(U^\alpha )^{-1}$ is the block diagonal matrix with blocks
$$
  U^\alpha _iA_i(U^\alpha _i)^{-1} =
  \pmatrix{
    \lambda _i & 0 & 0 & \ldots & 0 & 0 \cr
    \alpha  & \lambda _i & 0 & \ldots & 0 & 0 \cr
    0 & \alpha  & \lambda _i & \ldots & 0 & 0 \cr
    \vdots & \vdots & \ddots & \ddots  & \vdots  & \vdots  \cr
    0 & 0 & 0 & \ldots & \lambda _i &  0 \cr
    0 & 0 & 0 & \ldots & \alpha  &  \lambda _i
  },
  $$
where one should note that the 1's in $A_i$ have been changed to $\alpha $'s.
Taking the limit of $U^\alpha A(U^\alpha )^{-1}$ as $\alpha \to 0$, we have that the limit matrix lies in the closure of $S(A)$ and it has all
of the desired properties, including the fact that its spectrum is the same as that of $A$ (with different multiplicities).

The answer to question 2 is no,  a counter example being
$$
  A =   \pmatrix{
    1 & 0 \cr
    1 & 1
  }.
  $$
The reason is that
$$
  N:= A-I
  $$
is a nonzero nilpotent matrix,  and we claim that  for any invertible matrix $S$, one
has that the matrix $T:= SAS^{-1}$ has norm strictly bigger than 1.
To see this, notice that
$$
  T =   SAS^{-1} =   S(I+N)S^{-1} =  I+SNS^{-1} = I+S',
  $$
where $S'=SNS^{-1}$ is likewise a nonzero nilpotent matrix.
Arguing  by contradiction,  if $\|T\|\leq 1$,  then also $\|T^k\|\leq 1$, for all $k$, but
$$
  T^k = (I+S')^k = I+kS',
  $$
whose norm tends to infinite as $k\to \infty $.
Comment: If $A'  = UAU^{-1}$ then $S(A)=S(A')$, $\sigma(A)=\sigma(A')$, and $\rho(A)=\rho(A')$.  Thus, if you solve the problem for $A'$, you have also solved it for $A$.
Comment: Yes for your last question.
Comment: The norm is the operator norm, and since the limit is diagonal, its norm is the supremum of the diagonal entries.
Comment: For the last equality you should try some low values of $k$ and it will become evident.
Comment: Sorry I'm still not quiet sure about the reason why we may assume that $A$ is in Canonical Jordan Form. So by the Jordan Canonical Form Theorem, we know that there exists an invertible $S$ such that $S^{-1}A S$ is in the Jordan canonical form. Are we replacing $A$ by $S^{-1}A S$ in this case? And we're choosing $S$ to be a matrix sending its Jordan basis to the canonical basis of $\mathbb{C}$? Why we can choose $S$ to be such matrix? Thanks!
Comment: When you say $p$ is the size of $A_i$, do you mean if $A_i$ is a $n times n$ matrix, then $p=n$?
Comment: Thank you! And one more question, how do we know that $\| T\| = \rho(T)$, since we don't know what norm it is?
Comment: How did you get the last equality $(I+S')^k= I+ kS'$?


Orthonormal bases for Hilbert spaces

Question: In Reed and Simon (Functional Analysis) Theorem II.6 states that, given an orthonormal basis $\{ x_\alpha \}_{\alpha \in A}$ (not necessarily countable)for a Hilbert space $H$, every $y \in H$ can be written as a sum
$$
\sum_{\alpha \in A} (x_{\alpha}, y) x_\alpha
$$
where $(\cdot,\cdot)$ denotes the inner product).
The proof uses a conclusion that I cannot quite follow:
By Bessel's inequality we know that for any finite subset $A' \subset A$, $\sum_{\alpha \in A'} |(x_\alpha ,y )|^2 \le \|y\|^2$. Thus $(x_\alpha,y) \ne 0$ for at most a countable number of $\alpha's$ in $A$.
Why can we deduce the last statement ?
Answer: For each $n \in \mathbb{N}$, consider the set
$$
\{x_{\alpha} : |(x_{\alpha}, y)| \geq 1/n\}
$$
Show that this set is finite, so the union over all the $n$'s is countable, which is the set you are talking about.

Orthonormal bases for Hilbert spaces


Computing probability mass function for a game

Question:
You are dealt one card at random from a full deck and your opponent is dealt 2 cards (without any replacement). If you get an Ace, he pays you \$10, if you get a King, he pays you \$5 (regardless of his cards). If you have neither an Ace nor a King, but your card is red and your opponent has no red cards, he pays you \$1. In all other cases you pay him \$1. Determine your expected earnings. Are they positive?

I was able to compute all but one of the probabilities, specifically $P(X=-1)$.
According to my text $P(X=-1)=1-\frac{2}{13}-\frac{11}{102}$.
What exactly were they counting for this outcome?
I see that we want no Kings, no aces and no red cards, while our opponent also gets no red cards (or can they?).
$$P(X=10)=P(\text{You get an ace})=\frac{4}{52}$$
$$P(X=5)=P(\text{You get a King})=\frac{4}{52}$$
$$P(X=1)=P(\text{You get a red, and your opponent draws no reds})=\frac{(26-4)}{52}\frac{\binom{26}{2}}{\binom{51}{2}}$$
Comment: So this is $\frac{11}{102}$. The probability that you get an ace or a king is $\frac{1}{13}+\frac{1}{13}$. So the probability that none of these cases occur is $1-\frac{2}{13}-\frac{11}{102}$.
Comment: Show us how you computed the probabilities $\mathbb P(X=10)$, $\mathbb P(X=5)$ and $\mathbb P(X=-2)$.
Comment: I'll add those now.
Answer: Sample Space:

$A$: your card is ace
$B$: your card is king
$C$: your card is neither, but is red and both your opponent's cards are black
$D$: all other cases

Probable Profits:

$A$: $+10$ dollars
$B$: $+5$  dollars
$C$: $+1$  dollar
$D$: $-1$  dollar

Probabilities:

$P(A)=\binom{4}{1}/\binom{52}{1}=\frac{1}{13}$
$P(B)=\binom{4}{1}/\binom{52}{1}=\frac{1}{13}$
$P(C)=\binom{22}{1}/\binom{52}{1}\cdot\binom{26}{2}/\binom{51}{2}=\frac{11}{102}$
$P(D)=1-P(A)-P(B)-P(C)=\frac{979}{1326}$

Expected Profit:

$10\cdot\frac{1}{13}+5\cdot\frac{1}{13}+1\cdot\frac{11}{102}-1\cdot\frac{979}{1326}\approx52$ cents


check uploaded file size in function

Question: i make a function to check uploaded file size. However when i upload a file smaller than permitted size it always output size is over permit. Here is my function:
$max=150000;
    function checkSize($size)
    {
        global $max;
        if($size >= $max)
        {
            return false;
            //echo 'size > max size';
        }
    }

    $checkSize=checkSize($_FILES['image']['size']);
    if($checkSize==false)
            {
                echo 'size is over permit';
            }
            else
            {
                echo 'ok';
            }

Comment: That should be `return true;` since you're checking if it's over using  `if($size >= $max)` - if true, show error.
Comment: please return true in else part for `checkSize` method
Comment: print and check this `$_FILES['image']['size']`
Answer: Your checkSize() doesn't return ANYTHING if the size is ok, which means PHP will auto-return a null value. null == false is TRUE in php, so you falsely say the size isn't ok:
php > var_dump(null == false);
bool(true)

Your code should just be:
function checkSize($size) {
    global $max;
    return ($size <= $max);
}

This way you'll return either true or false.
Alternatively, you can use the "strict" equality test, ===:
php > var_dump(null === false);
bool(false)

Answer: It might help to use meaningful names in your functions so you know what to expect.  for example is you named your function something like uploadIsAllowableSize() then you would better understand it's expected behavior of returning false if the size givens exceeds the max.  You also need to return true if the size is acceptable. You could do this easily in one line like:
return ($size >= $max);

You might also consider defining a variable for the max file size rather then using global.  This is preferred way of dealing with thing like this that shouldn't change at run time.

check uploaded file size in function


How to center objects within a panel that is already laid out with borderlayout

Question: I have searched this site, other sites, and my textbook and cannot come up with a solution so here goes:
I have a simple temp. conversion program which functions just fine however I would like to make my layout more appealing and more similar to what is asked for in the procedures.
Right now it looks like this:

And I want it to look like this:

Difference being the Input and Output fields are centered in the desired output.
Here is my program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Convert extends JFrame{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private JPanel convertFrom, convertTo, top, bottom;
    private JLabel label3, label4;
    private JTextField temperature1, temperature2;
    private ButtonGroup radioFrom, radioTo;
    private JRadioButton celsiusBoxTo, fahrenheitBoxTo, kelvinBoxTo, celsiusBoxFrom, fahrenheitBoxFrom, kelvinBoxFrom;

    public Convert(){

        fahrenheitBoxFrom = new JRadioButton( "Fahrenheit", true );
        celsiusBoxFrom = new JRadioButton( "Celsius", false );
        kelvinBoxFrom = new JRadioButton( "Kelvin", false );

        radioFrom = new ButtonGroup();
        radioFrom.add( fahrenheitBoxFrom );
        radioFrom.add( celsiusBoxFrom );
        radioFrom.add( kelvinBoxFrom );

        label3 = new JLabel( "Input" );
        label4 = new JLabel( "Output" );

        fahrenheitBoxTo = new JRadioButton( "Fahrenheit", false );
        celsiusBoxTo = new JRadioButton( "Celsius", true );
        kelvinBoxTo = new JRadioButton( "Kelvin", false );

        radioTo = new ButtonGroup();
        radioTo.add( fahrenheitBoxTo );
        radioTo.add( celsiusBoxTo );
        radioTo.add( kelvinBoxTo );

        convertFrom = new JPanel();
        convertFrom.setLayout( new GridLayout( 4, 1 ) );
        convertFrom.add(new JLabel( "Input Scale" ));
        convertFrom.add( fahrenheitBoxFrom );
        convertFrom.add( celsiusBoxFrom );
        convertFrom.add( kelvinBoxFrom );

        convertTo = new JPanel();
        convertTo.setLayout( new GridLayout( 4, 1 ) );
        convertTo.add(new JLabel( "Output Scale" ));
        convertTo.add( fahrenheitBoxTo );
        convertTo.add( celsiusBoxTo );
        convertTo.add( kelvinBoxTo );

        temperature1 = new JTextField( 10 );
        top = new JPanel();
        top.setLayout(new GridLayout(1, 2));
        top.add(label3);
        top.add(temperature1);
        temperature1.addActionListener( new ActionListener(){

            public void actionPerformed( ActionEvent event ){
                int convertTemp, temp;
                temp = Integer.parseInt( ( ( JTextField ) event.getSource() ).getText() );

                if ( fahrenheitBoxFrom.isSelected() && celsiusBoxTo.isSelected() ){
                    convertTemp = ( int ) ( 5.0f / 9.0f * ( temp - 32 ) );
                    temperature2.setText( String.valueOf( convertTemp ) );
                }

                else if ( fahrenheitBoxFrom.isSelected() && kelvinBoxTo.isSelected() ){
                    convertTemp = ( int ) ( 5.0f / 9.0f * ( temp - 32 ) + 273 );
                    temperature2.setText( String.valueOf( convertTemp ) );
                }

                else if ( celsiusBoxFrom.isSelected() && fahrenheitBoxTo.isSelected() ){
                    convertTemp = ( int ) ( 9.0f / 5.0f * temp + 32 );
                    temperature2.setText( String.valueOf( convertTemp ) );
                }

                else if ( celsiusBoxFrom.isSelected() && kelvinBoxTo.isSelected()){
                    convertTemp = temp + 273;
                    temperature2.setText( String.valueOf( convertTemp ) );
                }

                else if ( kelvinBoxFrom.isSelected() && celsiusBoxTo.isSelected()){
                    convertTemp = temp - 273;
                    temperature2.setText( String.valueOf( convertTemp ) );
                }

                else if ( kelvinBoxFrom.isSelected() && fahrenheitBoxTo.isSelected()){
                    convertTemp = ( int ) ( 9.0f / 5.0f * ( temp - 273 ) + 32 );
                    temperature2.setText( String.valueOf( convertTemp ) );
                }
            }
            });

        temperature2 = new JTextField( 10 );
        temperature2.setEditable( false );

        bottom = new JPanel();
        bottom.setLayout(new GridLayout(1, 2));
        bottom.add(label4);
        bottom.add(temperature2);

        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add( top, BorderLayout.NORTH );
        container.add( convertFrom, BorderLayout.WEST );
        container.add( convertTo, BorderLayout.EAST );
        container.add( bottom, BorderLayout.SOUTH );
        setSize( 350,250);
        setVisible( true );

    }

    public static void main ( String args[] ){
        Convert application = new Convert();
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
}

Comment: What you want can be done with `GridLayout`.
Answer: BoxLayout can place components centered. BoxLayout respects the setAlignmentX property of a component (see also the answers to this question).
One caveat though: you'll have to manage a component's maximum size if it is configured to take the full available width of the window (like a text-field is). Here is how you can do it for the "bottom" panel:
javax.swing.Box bottomBox = Box.createHorizontalBox();
bottom = new JPanel();
bottomBox.add(bottom);
bottom.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
bottom.add(label4);
temperature2.setMaximumSize(temperature2.getPreferredSize());
bottom.add(temperature2);
...
container.add( bottomBox, BorderLayout.SOUTH );

You can do a similar thing for the "top" panel.
Comment: Thank you very much, I applied this to both top and bottom panels and all is well!
Answer: You can use GridBagLayout for this task. it will easily place your components in a 4x6 grid and you can pretty much specify the desired spacing between components using GridBagConstraints. I would use this layout.


How do I play this part in 'Portrait of Tracy'?

Question: I'm trying to learn the song 'Portrait of Tracy' by Jaco Pastorius on electric bass, based on the notation of the book The Essential Jaco Pastorius, but there is this part that I don't understand.

As you can see, the first note of the bar is a D# harmonic, with a 'H.H.' written on top.
I cannot understand exactly what the note below is trying to say. Playing the B on the 2nd fret of the A string and the harmonic of the 6th fret is nearly impossible. Not only for me, but also for a bassist friend that has huge fingers.
Is this the only way to play this part?
Comment: Is that a typo where it says "2nd fret of the **H** string"?
Comment: @NReilingh I have no idea
Comment: `H.H.` - I believe from my violin pedagogy class there's a "Half Harmonic", notated similarly.  `It is important not to produce any harmonics here; the result should be a veiled, almost immaterial and hardly perceptible coloring of the dominating string sound produced by the stopped note [...] only lightly touched, in conjunction with the "flautato" bowing.` - from [here](http://abraham.cs.uml.edu/~heines/91.212/Resources/GraphicNotationExamples/Lachenmann/GranTorso-Key_Binder.pdf)
Comment: H.H. presumably means 'half harmonic' here. The main note **and** the harmonic sound simultaneously.
Answer: If you check out Jaco's video "Modern Electric Bass"

, around 35:00 you can see that he does actually finger 2nd fret with his left hand on the A string while stretching his pinky finger up to 6th fret. Really interesting part in the video as he talks about harmonic technique in general. This guy also shows you how to do it

.
If it's too far to stretch, you can just finger the B at 7th fret on the E string and stretch your pink up to 11th fret to get the harmonic. Same note, shorter stretch. This is what I do, but I do find it a little harder to get a clean sounding note probably because the string is shorter.
Answer: My expectation is that indeed Jaco was able to stretch to the 6th fret. Two options you may consider are:

Fret overhand instead of under. You can probably even use your thumb to hit the harmonic in this position.
Change it to a pinch harmonic by fretting the 2nd fret B, and pinching with your plucking hand such that the pinch point is equidistant from the bridge as the 6th fret was from the 2nd. (Harmonics are symmetrical around the center point of the vibrating length of string.)
Comment: Your first idea will work, but messes up the next bar. The 'opposite' harm. is better, and could even apply to the next pair of notes, also harmonics.
Answer: I don't know how Pastorius played it but two standard techniques would be:

Use your right-hand index finger to touch the A string above the sixth fret (well, actually the sweet spot is a bit lower), and pluck the string with your right-hand pinky.
Same as above, but use your right-hand thumb to pluck the string.

These techniques are very useful anyway, because they are often the only option if you want a harmonic different from the major third of the fretted note.
Comment: Seems a long way to move the right hand, when the same (equivalent) nodes are about where it would normally be plucking.
Comment: @Tim: It's of course the player's choice, but there are two advantages to what I've described: 1. it's usually easier to find the sweet spot because you can use the frets as orientation. 2. It works for all harmonics, not only for those that can also be found close to the bridge.
Comment: Maybe I'm not understanding, but all harmonics have places up and down the string - apart from 12th fret! 7th is at 19, 5th is at 24, etc. I use pups etc to locate them at the bridge end.
Comment: @Tim: Yeah, you're right. I didn't mean that you can't find them between 12th fret and bridge, but sometimes they're not that close to the bridge as in this case. Of course you can use PU location etc. but that's different for every guitar and it only works if you've checked that out for a particular case, but the location by frets always works, also when improvising, i.e. without having checked out the exact location of the harmonic beforehand.
Comment: That's fine, I suppose it's what you get used to. Heard the harmonics solo by JP in Birdland ?I suppose you just move both hands in a sort of parallel motion.
Comment: @Tim: Yes, that's exactly what's happening. There JP just touches the strings with his thumb relatively close to the bridge while moving accordingly in sync with the fretting hand to achieve harmonics 2 octaves above the fretted notes.
Answer: Stretching to the 6th fret isn't impossible, it becomes so if your bass is not on the optimal setting to really get that D# to ring out. Assuming you can shift pickups on your bass, shift to the bridge pickup, turn your bass low and treble about mid-range. Now you don't have to stretch to the absolute end of the 6th fret, it buys you about 4cm of space. Test your bass amp settings with the intro run of harmonics Jaco plays before the main riff, he seemed to do the same from the live videos. If you can hear the F# harmonic (played on the D string, again, during the intro) as clearly as the other notes, iit will ring when you eventually have to stretch to play the D#


Why am I receiving 67 code from QOSStartTrackingClient method?

Question: I am receiving error code 67 from the code below, which means ERROR_BAD_NET_NAME.
Why is it happening? How can I fix it?
SOCKADDR address;
strcpy_s(address.sa_data, "220.127.116.11");
address.sa_family = AF_INET;

if (!QOSStartTrackingClient(QoSHandle, &address, 0))
    cout << GetLastError();

Comment: of course you initialize *SOCKADDR* wrong. by fact this must be *sockaddr_in*. it not accept string for ip. only in binary form as *ULONG S_addr*
Answer: you initialize SOCKADDR wrong :
strcpy_s(address.sa_data, "184.108.40.206"); - this is mistake.
really SOCKADDR is only place holder

Winsock functions using sockaddr are not strictly interpreted to be
  pointers to a sockaddr structure. The structure is interpreted
  differently in the context of different address families. The only
  requirements are that the first u_short is the address family and the
  total size of the memory buffer in bytes is namelen.

and from here

To actually fill in values for each part of an address, you use the
  SOCKADDR_IN data structure, which is specifically for this address
  format. The SOCKADDR and the SOCKADDR_IN data structures are the same
  size. You simply cast to switch between the two structure types.

in your case you need use SOCKADDR_IN
    SOCKADDR_IN sa = { AF_INET };
    sa.sin_addr.s_addr = inet_addr("22.214.171.124");
    if (!QOSStartTrackingClient(QoSHandle, (SOCKADDR*)&sa, 0))
        cout << GetLastError();

Comment: it looks like inet_addr is deprecated.
Comment: I used inet_pton(AF_INET, "22.214.171.124", &sa.sin_addr); instead but I still receive 67
Comment: @MatinLotfaliee but `SOCKADDR_IN sa = { AF_INET };` you execute before `inet_pton(AF_INET, "184.108.40.206", &sa.sin_addr);` ?
Comment: Yes. I execute it before inet_pton


Efeito Pulse não trabalha corretamente quando tem 2 elementos

Question: Tenho o seguinte código:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     xml:space="preserve">
     <g>
     <circle class="pulse" fill="none" stroke="#FF0000" stroke-miterlimit="5" cx="40" cy="40" r="2" />
    <circle class="" fill="#FF9C00" cx="40" cy="40" r="2" />
    </g>

    <g>
     <circle class="pulse" fill="none" stroke="#FF0000" stroke-miterlimit="5" cx="65" cy="65" r="2" />
    <circle class="" fill="#FF9C00" cx="65" cy="65" r="2" />
    </g>
</svg>

E o efeito:
@-webkit-keyframes svg_pulse {
     0% {
        -webkit-transform: matrix(1,0,0,1,0,0);
        stroke-width: 1;
        opacity: 1;
     }

     50% {
        opacity: 1;
     }
     100% {
        -webkit-transform: matrix(4,0,0,4,-120,-120);
        stroke-width: 0.25;
        opacity: 0;
     }
}

.pulse {
    -webkit-animation: svg_pulse 1s ease;
    -webkit-animation-iteration-count: infinite;
}

Em funcionamento: https://jsfiddle.net/4ej6msb9/1/
O problema é que o pulse funciona corretamente no primeiro <circle> mas no segundo, ao invés de funcionar corretamente, ele joga o pulse para baixo toda vez que pulsa. Não entendo o porque disso.
Answer: A transformação ocorre, por default, em relação à origem das coordenadas DO SVG (ponto 0,0 do SVG - canto superior esquerdo). Como o círculo não está na origem, você compensou movendo a origem durante a transformação (através de um translate simples -últimos dois parâmetros de matrix()). O problema é que são dois os círculos e eles estão em coordenadas diferentes. Para obter o efeito desejado neste caso, você teria que mover as duas origens e os valores seriam diferentes.
A solução mais simples é reposicionar a origem de coordenadas para cada transformação no centro do círculo correspondente. Isto pode ser feito definindo transform-origin: 50% 50%; para .pulse e eliminando o translate:
@-webkit-keyframes svg_pulse {
     ...
     100% {
        -webkit-transform: matrix(4,0,0,4,0,0); /* remova o translate 120,120 */
        stroke-width: 0.25;
        opacity: 0;
     }
}

.pulse {
    -webkit-transform-origin: 50% 50%;  /* acrescente esta linha */
    -webkit-animation: svg_pulse 1s ease;
    -webkit-animation-iteration-count: infinite;
}

Você adicionalmente pode trocar o matrix(a,0,0,a,0,0) por scale(a) que é equivalente e deixa seu código mais legível:
-webkit-transform: scale(4);

JSFiddle


Structuring a user rating/rebuttal system

Question: I am looking to create a really basic rating system (user reference and fivestar content type). But I would like to be able to have users rebut ratings if need be. I am just trying to figure out, structurally, how to do it. Would I have to create another content type called rebuttal and reference the rating? What about adding a field to the rating content type with the ability for the referenced user to fill in a text field? Limit comment access to just the rating author and the referenced user?
This may be the wrong forum to ask this, since its an open ended and subjective question. But I guess my question would be "How would you allow users to make selective edits to a node or limit comments to only the node author or referenced user?"
Comment: This is an English-speaking website @Pierre.Vriens, you should expect English words to be used, regardless of your own linguistic background. Non-English speakers most certainly should expect to need a dictionary here, that's a given. There's no reason to ask people to dumb down their posts, Google will easily translate this for you if you're struggling with it
Answer: Use the Flag module to create a Rebut flag, but do NOT make it a 'global' flag though. The flag is to be created (configured) by an admin, but each user (authorized to use the flag) will then be able to "rebut rating" using such flag. If it would make sense, you may want to have multiple flags configured for this (eg: Rebut-low , Rebut-medium, Rebut-high, whatever that would "mean" in your case).
Then create a view (with the views module) for each flag, whereas you create a "views relationship" with such flag(s).
If you're not familiar with the Flag module, checkout the set of 8 video tutorials about the flag module.
You might also want to look at some of the Flag related modules, such as (partial list):

Views, to create lists of flagged content, users, etc, such as in the video about Creating global flags and listing flagged content in Views.
Rules, to trigger rules for anything that is flagged, such as in the video about Triggering rules with Flag, or to set flags with the Rules module, such as in the video about Setting flags with Rules.

Refer to Flag's Community documentation for (way) more details.
Note: Via Rules it should be possibly to add logic to verify the "usage" of such flag.
Comment: That is an interesting take on this. I would however have to look into only showing the flag if a user has been referenced on the node. Thanks for the suggestion.


Tractrix tangent segment (from Baby Do-Carmo)

Question: I need some help with question 4 in section 1.3 in Baby Do-Carmo textbook in DG.
The question asks:
Let $\alpha(t):(0,\pi)\rightarrow R^2 $ be given by:
$$ \alpha(t)= (\cos(t), \cos(t) +\log(\tan(t/2)) $$ its image is called the tractrix.
Question b, asks to prove that the length of the segment of the tangent of the tractrix between the point of tangency and the y axis is constantly 1.
Now the angle between $\alpha$ and the y axis is t.
So basically if I were to use the sine theorem from trig, where $$\frac{S}{\sin(t)} = \frac{|\alpha(t)|}{\sin(\pi-(t+\angle \alpha(t) \alpha '(t)))}$$
Where S is the required line segment I am looking for.
Now I am only left with calculating the angle between $\alpha(t)$ and $\alpha '(t)$, is this about right, or I am way off here?
It's hell of a calculation if I am right (and it's really rare when I am).
Thanks.
Comment: Are you sure it is the right parametrizazion for a tractrix http://en.wikipedia.org/wiki/Tractrix ? When I plot $t\mapsto (\cos t,\cos t + \log(\tan t/2))$ I obtain something totally different...
Comment: (You can also find a visual explanation of why the relation you are looking for actually holds, in the same wiki page) http://upload.wikimedia.org/wikipedia/en/2/27/Tractrixtry.gif
Answer: Anyway, for completeness: start with the (now correct) parametrization
$$\begin{align*}x&=a\sin\,t\\y&=a\left(\cos\,t+\log\tan\frac{t}{2}\right)\end{align*}$$
(I prefer my tractrices to have the horizontal axis as the asymptote, but oh well...)
It is easy to construct the slope corresponding to any $t$:
$$\frac{\mathrm dy}{\mathrm dx}=\dfrac{\frac{\mathrm dy}{\mathrm dt}}{\frac{\mathrm dx}{\mathrm dt}}=\frac{-\sin\,t+\csc\,t}{\cos\,t}$$
and thus the equation of the tangent line as well:
$$\frac{y-a\left(\cos\,t+\log\tan\frac{t}{2}\right)}{x-a\sin\,t}=\frac{-\sin\,t+\csc\,t}{\cos\,t}$$
and the expression for the y-intercept of this line is $\log\tan\frac{t}{2}$; it is now easy to see that the distance from the point $(x,y)$ of the tractrix to the point $\left(0,\log\tan\frac{t}{2}\right)$ is $a$.
Answer: You can find the tangent line of a curve at a point $\alpha(t)=(\alpha_1(t),\alpha_2(t))$ by the formula
$$
\det\begin{pmatrix} X-\alpha_1(t) &  \alpha_1'(t) \\
Y-\alpha_2(t) & \alpha_2'(t)\end{pmatrix}
$$
(for the sake of completeness, with your curve it is the locus of $(X,Y)$ such that
$-\sin t \; (\log  \tan \frac{t}{2} +X-Y) +X \csc t-\cot t$).
Now it's only a matter of computation of the length of the segment between $\alpha(t)$ and the $Y$-axis intercept of the former line. Am I right?
Comment: How do you get this formula with the det?
Comment: OK, I solved it. Thanks, in the end it's really a simple question in high school analytic question, and your'e right, the x-component of alpha is sin(t) and not cos(t), though in the edition I am using it's written as I first typed.
Comment: You're welcome. :) Can you please accept my answer if it helped you? The "formula with the det" is simply the standard linear-alegbraic-formula to obtain a line between two given points in the affine plane.
Answer: It is easier to prove this one backwards.  The original definition of the tractrix is the curve created when a constant length "leash" is pulled along a straight line.  Thus the slope of the line is always tangent to the curve and leads directly to the differential equation that creates the tractrix.  Wikipedia has the equations nicely type-set.
That is, the answer to your question is "by definition" of the curve itself.


Show that the function $x\mapsto x\sin x$ is continuous at $x = 0$

Question:
Use the definition of continuity to show that the function: $f: \mathbb R \to\mathbb R$ defined by $f(x) = x\sin x$ is continuous at $x = 0$.

Def: A real valued function $f$  is called  continuous at $a \in \mathbb R$ if the $\lim_{x \to a}f(x) = f(a)$
So continuous if $\lim_{x \to 0}x\sin x = f(0) = 0\sin 0 = 0$
By inspection when $x = 0$,  $x\sin x = 0\sin 0 = 0\cdot0 = 0$
Which means $f$ is continuous at $0$.
Comment: Do you know __the definition of continuity__?
Comment: __The definition of continuity:__ A function $f$ is continuous at a point $a$ if for every $\varepsilon >0$, there exists $\delta >0$ such that $|x-a| < \delta$ implies that $|f(x) - f(a)| < \varepsilon$
Comment: My guess would be that they want something a little more first principles than this but maybe not. If they do want something more first principles, I would suggest using the squeeze theorem.
Comment: Oh typo how's this Portakal? I meant to say the F(x) is continuous at "a" ir  $\lim_{x \to a}F(x) = F(a)$
Comment: I just don't understand why if this is the definition of continuity, how does my original answer fit into the picture? How do i use this to work with gamma and delta.
Answer: Note that $|\sin x|\leqslant 1$ for all $x$. So given $\varepsilon>0$, if $|x|<\varepsilon$, then $$|x\sin x| \leqslant |x| <\varepsilon$$
which means that $x\mapsto x\sin x$ is continuous at $0$.


Making a randomly generated 2d map in python is taking too long to process all of the map generation

Question: import random
l =  "lava"
d = "dessert"
f = "forest"
v = "village"
s = "sect"
w = "water"
c = "city"
m = "mountains"
p = "plains"
t = "swamp"
map_list = [l,d,f,v,s,w,c,m,p,t]
map = []
for i in range(50):
    map.append([])

def rdm_map(x):
    for i in range(50):
        map[x].append(random.choice(map_list))

def map_create():
    x = 0
    while x <= len(map):
        rdm_map(x)
        x + 1

map_create()
print(map[2][1])

I'm not getting anything for output not even an error code.I'm trying to create a randomly generated game map of descent size but when i went to run it nothing happened i'm thinking since my computer isn't that great its just taking way to long to process but i just wanted to post it on here to double check. If that is the issue is there a way to lessen the load without lessening the map size?
Answer: You have the following bugs:

Inside the map_create you must change x + 1 to x += 1. For this reason your script runs for ever.
After that you should change the while x <= len(map): to while x < len(map):. If you keep the previous, you will get a Index Error.

In any case, your code can be further improved. Please try to read some pages of the tutorial first.
Comment: Interesting i thought that having that x <= len(map) part would keep it from running forever. I'll definitely be doing more research on the subject so I can understand the "why" of why i need those changes thank you so much for your help!
Comment: Yes, you are right, the `x <= len(map)`, it will prevented from running for ever. But your initial code does not increment the `x`
Comment: The little light bulb just went on lol so just saying x + 1 does nothing i have to actually use the += to do the incrementation that i need for it to work and as far as the <= im guessing that the way it works that final x would actually be 1 longer than the actual len throwing that error?
Comment: Yes that's right. You are initiating the `map` list for 0 to 49, so you need to iterate with the same indexes and you should not go to the 50 item, because it does not exist


Stored routine out parameter stub

Question: I have a mysql 5.5 stored routine:
PROCEDURE get_stuff(IN po_id BIGINT, OUT po_name VARCHAR(32), OUT po_value VARCHAR(32)) ...

I want call this procedure from other routine, but I dont need all out parameters of previous routine:
PROCEDURE do_work(IN po_id BIGINT)
BEGIN
    DECLARE stuff_value VARCHAR(32) DEFAULT NULL;

    CALL get_stuff(po_id, /* dont need this out param */, stuff_value);
    ...

Is there a way to stub out parameter of stored routine?
Answer: Well, you can't set optional parameters in MySQL stored procedures. There isn't currently a way to define a default value for a parameter and therefore all parameters need to be passed a value.

Stored routine out parameter stub


Getting the limit for a ratio of logarithms functions with different base

Question: I know similar questions have been asked in the past, but I need some clarification to know if what I'm doing is correct:
let $f(x) = log_2x$, and $g(x)= log_3x$
Now I need to take the limit as $x$ approaches $\infty$ from the ratio:
$f(x)/g(x)$
I have applied the base change formula for $g(x)$ which yielded $log_2x/log_23$
now, when I divide $log_2x/log_2x/log_23$ I get:
$log_23$
so, how would I go about the taking the limit of $log_23$ since I could not find a rule regarding logarithms so far. and I'm doing this exercises as a part of algorithms class I'm taking.
Comment: "how would I go about the taking the limit of $\log_2 3$?"   $\log_2 3 $ is a constant.  A constant doesn't vary.  $\lim c = c$ always.  So $\lim \log_2 3 = \log_2 3 \approx 1.5849625.....$.
Answer: Taking the limit here doesn't involve any specific rules regarding logarithms. This is because since $\log_{2}3$ is a constant, i.e., doesn't involve $x$ at all, the limit of it as $x \to \infty$ would be the same value, i.e., $\log_{2}3$.


Add values of repeated items from list to a dictionary

Question: I have the list
list1 = [['P', '2.1'], ['C', '4.1'], ['S', '4.2'], ['M', '5.3'], ['A', '4.1'], ['C', '3.8'], ['C', '3.9'], ['M', '5.2'], ['A', '4.3'], ['P', '2.3'], ['C', '4.2'], ['A', '4.4']]
and i have to create a dictionary adding the values of repeated items.
so far I have this:
for line in carsList:
     model = line[0]
     carsDict.update({model:[]})
     if model in carsDict:
         carsDict[model].append(float(line[1]))

But when I print what i got is:
{'P': [2.3], 'C': [4.2], 'S': [4.2], 'M': [5.2], 'A': [4.4]}
Thanks.
Comment: What is your expected output?
Answer: You cleared the list every time by updating it to an empty list, so you only gets the last element.
for line in carsList:
     model = line[0]
     carsDict.update({model:[]}) # It clears out your list every time!
     if model in carsDict:
         carsDict[model].append(float(line[1]))

This is what you should do. You create a new list when it does not exist, and append to it if there is already a list.
for line in carsList:
    model = line[0]
    if not model in carsDict:
        carsDict[model]=[]
    carsDict[model].append(float(line[1]))
# {'A': [4.1, 4.3, 4.4], 'P': [2.1, 2.3], 'C': [4.1, 3.8, 3.9, 4.2], 'M': [5.3, 5.2], 'S': [4.2]}

If you want to calculate a sum of all the floats:
for line in carsList:
    model = line[0]
    if not model in carsDict:
        carsDict[model]=0
    carsDict[model]+=float(line[1])
# {'A': 12.8, 'P': 4.4, 'C': 16.0, 'M': 10.5, 'S': 4.2}

Answer: Use dict.setdefault
Ex:
list1 = [['P', '2.1'], ['C', '4.1'], ['S', '4.2'], ['M', '5.3'], ['A', '4.1'], ['C', '3.8'], ['C', '3.9'], ['M', '5.2'], ['A', '4.3'], ['P', '2.3'], ['C', '4.2'], ['A', '4.4']]
result = {}

for k, v in list1:
    result.setdefault(k, []).append(v)

print(result)

or collections.defaultdict
from collections import defaultdict
result = defaultdict(list)

Output:
{'A': ['4.1', '4.3', '4.4'],
 'C': ['4.1', '3.8', '3.9', '4.2'],
 'M': ['5.3', '5.2'],
 'P': ['2.1', '2.3'],
 'S': ['4.2']}

Answer: Every time you run through the for loop, you reset the value of model to an empty list in your carsDict.
what you can do is:
for line in carsList:
    model = line[0]
    if model in carsDict:
        carsDict[model].append(float(line[1]))
    else:
        carsDict[model] = [float(line[1])]

Answer: You can use a collections.defaultdict to group by the first item of each sublist:
from collections import defaultdict

list1 = [['P', '2.1'], ['C', '4.1'], ['S', '4.2'], ['M', '5.3'], ['A', '4.1'], ['C', '3.8'], ['C', '3.9'], ['M', '5.2'], ['A', '4.3'], ['P', '2.3'], ['C', '4.2'], ['A', '4.4']]

d = defaultdict(list)
for x, y in list1:
    d[x].append(float(y))

print(dict(d))
# {'P': [2.1, 2.3], 'C': [4.1, 3.8, 3.9, 4.2], 'S': [4.2], 'M': [5.3, 5.2], 'A': [4.1, 4.3, 4.4]}

print({k: sum(map(float, v)) for k, v in d.items()})
# {'P': 4.4, 'C': 16.0, 'S': 4.2, 'M': 10.5, 'A': 12.799999999999999}

If we are only interested in summing the floats, using a defaultdict(float) is probably more faster than collecting all the floats in a list then applying sum():
d = defaultdict(float)
for x, y in list1:
    d[x] += float(y)

print(dict(d))
# {'P': 4.4, 'C': 16.0, 'S': 4.2, 'M': 10.5, 'A': 12.799999999999999}

Also note that defaultdict is a subclass of dict, so the dict() cast is not needed.
You could also sort by the first item then apply itertools.groupby:
from itertools import groupby

list1 = [['P', '2.1'], ['C', '4.1'], ['S', '4.2'], ['M', '5.3'], ['A', '4.1'], ['C', '3.8'], ['C', '3.9'], ['M', '5.2'], ['A', '4.3'], ['P', '2.3'], ['C', '4.2'], ['A', '4.4']]

print({k: [float(x[1]) for x in g] for k, g in groupby(sorted(list1), key=lambda x: x[0])})
# {'A': [4.1, 4.3, 4.4], 'C': [3.8, 3.9, 4.1, 4.2], 'M': [5.2, 5.3], 'P': [2.1, 2.3], 'S': [4.2]}

print({k: sum(float(x[1]) for x in g) for k, g in groupby(sorted(list1), key=lambda x: x[0])})
# {'A': 12.799999999999999, 'C': 16.0, 'M': 10.5, 'P': 4.4, 'S': 4.2}

Both solutions above show how to group and get the sum of the floats. The groupby solution is slower for grouping because it is using O(NLogN) sorting, whereas the defaultdict groups in O(N) time. We could also replace key=lambda x: x[0] with operator.itemgetter(0), which is slightly faster. More information about the speed difference between the two in this answer.
Answer: I've interpreted your question as meaning you want to sum the values for each key. There's a couple of ways you can achieve this. Using a defaultdict is probably the simplest:
from collections import defaultdict

carsDict = defaultdict(float)

for l in list1:
    carsDict[l[0]] += float(l[1])

print({ k : v for k, v in carsDict.items() })

but you can also implement it as a dictionary comprehension:
carsDict = { k : sum(float(l[1]) for l in list1 if l[0] == k) for k in set(l[0] for l in list1) }
print(carsDict)

In both cases the output is
{'C': 16.0, 'M': 10.5, 'S': 4.2, 'P': 4.4, 'A': 12.799999999999999}

Comment: I have asked OP to clarify the expected result as this entire answer could be wrong. Your answer is the best here though (it got my vote anyway) as it covers both bases.
Comment: Yeah I also recommended `defaultdict(float)`. +1
Comment: Yeah I was unsure either. Thought it wouldn't hurt  to include both cases. Hopefully the OP can clarify :)


Logistic Regression using glmnet(): accuracy measure from mean() returns 0

Question: I am building a Logistic Regression Model using glmnet() package:
> # Prep Training and Test data.
> trainDataIndex <- sample(1:nrow(df), 0.7*nrow(df))  # 70% training data
> trainData <- df[trainDataIndex, ]
> testData <- df[-trainDataIndex, ]
> set.seed(100)
> trainData <-
+   trainData %>%
+   dplyr::mutate(CUST_REGION_DESCR =
+                   forcats::fct_relabel(CUST_REGION_DESCR, ~ trimws(.x)))
> testData <-
+   testData %>%
+   dplyr::mutate(CUST_REGION_DESCR =
+                   forcats::fct_relabel(CUST_REGION_DESCR, ~ trimws(.x)))
> str(trainData)
'data.frame':   693843 obs. of  4 variables:
 $ cust_prog_level  : Factor w/ 14 levels "B","C","D","E",..: 9 7 10 9 10 9 10 5 10 5 ...
 $ CUST_REGION_DESCR: Factor w/ 8 levels "CORPORATE REGION",..: 2 6 7 6 8 8 4 7 7 6 ...
 $ Sales            : num  92.7 2356 39 239.6 26 ...
 $ New_Product_Type : Factor w/ 2 levels "0","1": 1 1 1 1 2 1 1 1 1 1 ...
> str(testData)
'data.frame':   297362 obs. of  4 variables:
 $ cust_prog_level  : Factor w/ 14 levels "B","C","D","E",..: 9 5 9 9 9 9 3 3 5 3 ...
 $ CUST_REGION_DESCR: Factor w/ 8 levels "CORPORATE REGION",..: 3 3 6 6 7 6 7 2 2 4 ...
 $ Sales            : num  150.2 68.5 68.1 72.1 60.1 ...
 $ New_Product_Type : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...

> x = model.matrix(New_Product_Type ~.,data=trainData)

> cvfit = cv.glmnet(x, y=as.factor(trainData$New_Product_Type), alpha=1, family="binomial",type.measure = "mse")

> lambda_1se <- cvfit$lambda.1se

> coef(cvfit,s=lambda_1se)
23 x 1 sparse Matrix of class "dgCMatrix"
                                                1
(Intercept)                            0.02946581
(Intercept)                            .
cust_prog_levelC                       0.14012975
cust_prog_levelD                       .
cust_prog_levelE                       0.13339906
cust_prog_levelG                      -0.05325043
cust_prog_levelI                       0.21440592
cust_prog_levelL                       0.26273503
cust_prog_levelM                       .
cust_prog_levelN                       0.26620261
cust_prog_levelP                      -0.05166799
cust_prog_levelR                      -0.33054803
cust_prog_levelS                       .
cust_prog_levelX                       0.57508875
cust_prog_levelZ                       1.20748454
CUST_REGION_DESCRMOUNTAIN WEST REGION -0.20993854
CUST_REGION_DESCRNORTH CENTRAL REGION -0.04035331
CUST_REGION_DESCRNORTH EAST REGION     0.01082858
CUST_REGION_DESCROHIO VALLEY REGION    0.03077584
CUST_REGION_DESCRSOUTH CENTRAL REGION  .
CUST_REGION_DESCRSOUTH EAST REGION     0.10606213
CUST_REGION_DESCRWESTERN REGION       -0.17587036
Sales                                 -0.01223843

> #get test data
> x_test <- model.matrix(New_Product_Type~.,data = testData)
> #predict New_Product_Type, type="New_Product_Type"
> lasso_prob <- predict(cvfit,newx = x_test,s=lambda_1se,type="response")

> #translate probabilities to predictions
> lasso_predict <- rep("neg",nrow(testData))
> lasso_predict[lasso_prob>.5] <- "pos"
> #confusion matrix
> table(pred=lasso_predict,true=testData$New_Product_Type)
     true
pred       0      1
  neg 207840  60865
  pos   8697  19960
> #accuracy

> lasso_predict[lasso_prob>.8] <- "pos"
> #confusion matrix
> table(pred=lasso_predict,true=testData$New_Product_Type)
     true
pred       0      1
  neg 207840  60865
  pos   8697  19960

When I test the accuracy, the return value is 0
> #accuracy
> mean(lasso_predict==testData$New_Product_Type)
[1] 0

So does it mean my model have ZERO accuracy?
Answer: If you look at your data set, your target vector is encoded as zeros an ones
New_Product_Type : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...

but when you make your vector of class predictions, you use a completely different encoding
lasso_predict <- rep("neg",nrow(testData))
lasso_predict[lasso_prob>.5] <- "pos"

and then you count how often these vectors are equal
mean(lasso_predict==testData$New_Product_Type)

These two vectors can never be equal, as one contains zeros and ones, and the other contains the strings "pos" and "neg".  You need to be much more careful in your programming, and use the same labels to represent the same concept in both vectors.
As a side note, this is probably not a good way to evaluate your model.  Unless you have very good reason, you should be wary of using the raw accuracy to make decisions about model fit or predictive power.  A quick search of this site will turn up lots of information.  For example:
Why is accuracy not the best measure for assessing classification models?
Comment: Another thing is: Should I down_size both the train and test data? Since I have a mis-proportion between "0" (has more observations) and "1", some advise to downsize the population of "0".
Comment: Also, I corrected the mean() issue. But how do I interpret the result as a predictive model?
Comment: No, there's almost certainly no need to downsample your data.  Those techniques are meant to be applied when there are very, very few example of one class.  On the order of a fraction of a percent.  Im not sure I understand your last quesiton.
Comment: Let me rephrase my last question, I apologize for the confusion. So in the Linear Regression Model, say I want to see the correlation between car_speed and the number of accidents. That is easy for me to understand. Now for this model, my response variable is either 0 or 1. How do I understand this model? Specifically, 0 relates to "Nobody buys this product/They buy others" and "1" relates to "They buy the House-Product". There're three predictors for this mode. How can I interpret the result summary(cvfit)? The mean() now returns " 0.7660865". What does it imply?
Comment: Ahhh, that's really a totally different question, so I would create a new question for something like that.
Comment: Can we chat in a forum?


PHP 5.4.9 Mysqli issue

Question: On Ubuntu 12.04 server I had PHP 5.4.9 installed from the source:

./configure --prefix=/etc/php --with-apxs2=/etc/apache2/bin/apxs --with-config-file-path=/etc/php --with-config-file-scan-dir=/etc/php/conf.d --with-libxml-dir=/usr/local/libxml2 --with-xsl=/usr/local/libxslt --with-mysql --with-zlib --with-pdo-mysql --enable-calendar --with-gd --with-iconv-dir --enable-mbstring --enable-soap --enable-sockets --enable-zip --with-curl --with-openssl --with-kerberos --with-tidy'

Then, using apt-get, I had mysql server and phpMyAdmin installed. Unfortunatelly phpMyAdmin keep saying that 'mysqli' and 'mcrypt' not installed. php -m | grep mysqli just confirms it.
So I tried to install mysqli with "apt-get install php5-mysqli", but just got message to do it by means of "php5-mysqlnd" or "php5-mysql". Even though they are already installed (according to phpinfo()) I tried - doesn't work. However, in php.ini, there's mysqli staff like "extension=php_mysqli.dll", but no "extension=mysqli.so". And block [MySQLi] with some uncommented settings also present.
Since this is my first attempt to build php from source I reckon I did some silly mistake.
Any help is greatly appreciated.
UPD: PROBLEM SOLVED
1. To enable mcrypt extension (http://sourceforge.net/projects/mcrypt/files/)
1.1 download, unpack, config, compile and install Libmcrypt library

wget http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
  tar xf libmcrypt-2.5.8.tar.gz
  cd libmcrypt-2.5.8
  ./configure
  make && make install

1.2 download, unpack, config (I added desirable path: ----prefix=/usr/local/mcrypt), compile and install mcrypt

wget http://downloads.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
  tar xf lmcrypt-2.6.8.tar.gz
  cd lmcrypt-2.6.8
  ./configure --prefix=/usr/local/mcrypt
  make && make install

2.to install mysqli I followed proposed options of configuring php taken from http://php.net/manual/en/mysqli.installation.php

./configure --with-mysql=/usr/bin/mysql_config  \
  --with-mysqli=mysqlnd \
  --with-pdo-mysql=mysqlnd
  [other options]

So after installing Libmcrypt, mcrypt in /usr/local/mcrypt and adding changes to configuration of php I had finally succeeded. The final config looks this way:
<code>./configure --prefix=/etc/php --with-apxs2=/etc/apache2/bin/apxs --with-config-file-path=/etc/php --with-config-file-scan-dir=/etc/php/conf.d --with-libxml-dir=/usr/local/libxml2 --with-xsl=/usr/local/libxslt --with-mysql --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mcrypt=/usr/local/mcrypt --with-zlib --enable-calendar --with-gd --with-iconv-dir --enable-mbstring --enable-soap --enable-sockets --enable-zip --with-curl --with-openssl --with-kerberos --with-tidy --with-pcre-regex
</code>
P.S: Note that to run this you would need download, config, compile and install libxml, libxslt, prce, mcrypt, Libmcrypt
Comment: If you've resolved your problem, please accept the answer that solved your problem (or post your own answer, if the solution was different, and accept it). This way others know that the problem is resolved.
Answer: If you need PHP with mcrypt and mysql support, where are --with-mcrypt --with-mysqli options?
Comment: Please ignore my previous comment (which I've deleted, but I know it still shows up in your feed) as I made a mistake

PHP 5.4.9 Mysqli issue


Laravel 5.4: Exclude a route with parameters from CSRF verification

Question: As per Laravel 5.4 Docs, you can exclude a route from CSRF verification by adding the route to $except property at VerifyCsrfToken middleware. But for some reason, a route with parameters couldn't be excluded with exact route name unless excluded from main route itself.
Expected route to be excluded:
protected $except = [
    'main/{id}/sub/*'
];

Only works through:
protected $except = [
    'main/*'
];

How do you exclude a route with parameters from CSRF verification?
Answer: Since under the hood this feature uses request()->is() method, maybe this will work for you:
protected $except = [
    'main/*/sub/*'
];


como ativar o botão direito do mouse para exibir as configurações de câmera no plugin scriptcam?

Question: Estou utilizando o plugin scriptcam para capturar imagem pela webcam, estou utilizando 2 câmeras e preciso selecionar a câmera quando necessário, o problema é que o botão direito do mouse não ativa as configurações de câmera do flash como especificado em macromidia linha "Como exibir este painel novamente?", alguém sabe me dizer onde faço essa configuração para que quando eu clicar com o botão direito do mouse exiba as opções para eu poder escolher a câmera? isso porque se eu clicar em permitir e lembrar ele não exibe mais a opção de escolha da câmera.
Answer: O próprio plugin oferece um método para solucionar esse problema:
<code>function onWebcamReady(cameraNames,camera,microphoneNames,microphone,volume) {
    $.each(cameraNames, function(index, text) {
        $('#cameraNames').append( $('<option></option>').val(index).html(text) )
    });
    $('#cameraNames').val(camera);
}

<select id="cameraNames"></select>

$('#capiture-image').scriptcam({
        onWebcamReady:onWebcamReady,
        path:'/js/scriptcam/'
    });
</code>


Find total duration of many overlapping times

Question: I have a list of dates and times for employee time sheets.  The times begin in column F, and end in column G. Sometimes there are overlapping times for projects.  The employee does not get paid for overlapping projects, yet we need to track each project separately.  I would like to be able to look at columns E, F and G and find any overlapping projects, and return a single time entry.  In the example below, notice that line 1 does NOT overlap with the others, but that there is a series of overlapping entries in lines 2-6.  They don't necessarily all overlap, but are more like a "chain." I want to write a formula (not a script) to solve this.
+---+------------+------------+----------+
|   |     E      |       F    |    G     |
+---+------------+------------+----------+
| 1 | 10/11/2017 | 12:30 PM   |  1:00 PM |
| 2 | 10/11/2017 |  1:00 PM   |  3:00 PM |
| 3 | 10/11/2017 |  2:15 PM   |  6:45 PM |
| 4 | 10/11/2017 |  2:30 PM   |  3:00 PM |
| 5 | 10/11/2017 |  2:15 PM   |  6:45 PM |
| 6 | 10/11/2017 |  3:00 PM   |  6:45 PM |
+---+------------+------------+----------+

I would want to evaluate these columns and return the total duration of each "chain" on the final line of the series of overlaps. In my example below, we'll put that in column H. It finds 5.75 hours for the series that begins in row 2 and ends in row 6 (1 pm to 6:45 pm).
+---+------------+------------+----------+------------+
|   |     E      |       F    |    G     |      H     |
+---+------------+------------+----------+------------+
| 1 | 10/11/2017 | 12:30 PM   |  1:00 PM |    0.5     |
| 2 | 10/11/2017 |  1:00 PM   |  3:00 PM |  overlap   |
| 3 | 10/11/2017 |  2:15 PM   |  6:45 PM |  overlap   |
| 4 | 10/11/2017 |  2:30 PM   |  3:00 PM |  overlap   |
| 5 | 10/11/2017 |  2:15 PM   |  6:45 PM |  overlap   |
| 6 | 10/11/2017 |  3:00 PM   |  6:45 PM |   5.75     |
+---+------------+------------+----------+------------+

I've tried writing queries, but keep finding myself back at the beginning.  If anyone has a suggestion, I'd love to know it!  Thank you in advance.
Neill
Answer: My Solution
To solve this I need 2 extra columns:

Step 1. Return "overlap" or "ok"
Two lines overlap when one end is inside the other:

I made a query formula to check this:
=if(QUERY(ArrayFormula({value(E1:E+F1:F),VALUE(E1:E+G1:G)}),
"select count(Col1) where
    Col1 < "&value(G1+E1-1/10^4)&"
and Col2 > "&value(F1+E1+1/10^4)&" label Count(Col1) ''",0)>1,"overlap","ok")
Drag the formula down. The result is column:
ok
overlap
overlap
overlap
overlap
ok
ok
overlap
overlap
overlap
overlap
ok

In the formula:

value is used to compare numbers. Must compare each pare: date + time.
-1/10^4 and +1/10^4 is used because of imprecision in query

Step 2. Get Time Chains
This part is tricky. My solution will only work if data is sorted like in the example.
Enter 1 in cell I1. In cell I2 enter the formula:
=if(or(and(H1=H2,H2="overlap"),and(H2="ok",H1="overlap")),I1,I1+1)
Drag the formula down. The result is column:
1
2
2
2
2
2
3
4
4
4
4
4

Step3. Get Durations
In J4 paste and copy down the formula:
=if(H1="ok",
round(QUERY(ArrayFormula({value(E:E+F:F),VALUE(E:E+G:G),I:I}),
"select max(Col2) - min(Col1) where Col3 = "&I1
&" label max(Col2) - min(Col1) ''")*24,2),"")
The query gets max durations by groups, found in step2.

round is used because of imprecision in query
Comment: Absolutely amazing! Thank you Max.  Not only did your solution work, but I appreciate the explanation you included so I can understand how it works.  I am very appreciative!


Unclosed element '' in Ember.js 1.12.0 using if/else conditional helper

Question: I have a newly-added section of code in my Ember.js/Handlebars template that looks like:
      {{#each day in week.days}}
        {{#if day.today}}

        {{else}}

        {{/if}}

{{day.mileage}}

          {{day.dayStr}}

      {{/each}}

Where the day.today comes from an object populated like:
{
    ...
    today: dateEquals(new Date(), currentDate)
}

And I would like (in the little calendar I'm drawing) for that date to be highlighted if it's today.
But, when attempting to run this, the following error is emitted:
[18191:0927/082737:INFO:CONSOLE(12363)] "Uncaught Error: Unclosed element `td` (on line 14).", source: file:///path/to/project/js/libs/ember-template-compiler-1.12.0.js (12363)

Is this error coming from the pre-rendered template? Surely at runtime ("rendertime") there will only be either  or , but never both.
How can I resolve this issue?
Comment: What version of Ember?
Comment: Currently using v1.12.0
Comment: A sidenote: `{{#each week.days as |day|}}` is the current syntax.
Answer: As of Ember v1.11 you can use inline-if helper in combination with bound attribute syntax:
{{#each day in week.days}}

{{day.mileage}}

    {{day.dayStr}}

{{/each}}

Comment: This is not quite an issue, HTMLBars simply doesn't know that you close the tag because it's inside another conditional branch. If you want to do if/else, contain the whole tag inside each branch — I believe this leads to less bugs/confusion down the line as well.
Comment: Thanks Daniel, this *does* work. But is there a reason why, for more complicated logic in the future, I can't use `if/else` to build my DOM? Are these kind of "unclosed element" errors always going to ignore the template logic?
Comment: I think you could submit issue in ember repo, because what you do should work in my opinion, but - to be honest - you won't encounter an issue where you have to do what you did. I never encountered such issue where I couldn't rewrite my element to inline-if helper. This is issue of HTMLBars probably which performs check.
Comment: Check first if it would work in Ember 1.13 or Ember 2.0, maybe it's solved already.
Comment: Valuable comment. So it seems like it could be design decision.
Comment: How would you rewrite this one: `{{#if active}}{{else}}<div class="nonclickable>{{/if}}`
Answer: To solve this problem, contain the whole tag inside the conditional branch so HTMLBars can "see" that you close the tag, and not leave it hanging. Like so:
{{#each week.days as |day|}}
  {{#if day.today}}

{{day.mileage}}

      {{day.dayStr}}

  {{else}}

{{day.mileage}}

      {{day.dayStr}}

  {{/if}}
{{/each}}

This seems like a lot of repetition, but I've found it usually leads to less bugs when modifying the layout down the road. It might also be an indication that you might want to extract it to a component, and either employ the inline-if Daniel mentions in the other answer, or make your own helper.
Comment: I thought about doing it this way, but as you mention, this is way too much repetition.
Comment: That's why I also mentioned extracting the repeating HTML to a component, either way your structure will remain explicit.