Service Locator for DI Container

Question: I have created a very simple dependency injection container. I can create an instance of a class by saying:
$foo = $container->get(Foo::class);

This works well and allows me to inject dependencies in Foo's constructor. Now I wish to create an instance of a class by saying:
$user = new User();

I need to be able to access a service from the container within the User class but i'm not sure the best way to do it. Two ways I'd like to avoid is one passing the container into the constructor and secondly using the container's get method as shown above to create an instance of Foo.
I'd appreciate it if someone could show me the correct way to achieve this. Thanks
Answer: You don't have to inject into the constructor. A couple of ways to inject would be via a setter function
public function setUser(User $user) {
    $this->user = $user;
}

Or you can inject directly into the function that needs to interact with the class
public function someFunction(User $user) {
    $val = $user->doSomething();
}

Comment: Thanks but I don't really want do it this way either as otherwise I have to change my call to create the user instance. Ideally I'd like a static accessor but I'm not sure how to do it so that it shares the same instances (stored as an array) within my container.
Answer: I've come up with a neat way of doing this. First I added a static property to my container which points to the current instance. For example:
class Container {
    protected static $instance;

    public function __construct() {
        static::$instance = $this;
    }

    ...
}

Then all I have to do is create a static get method e.g.:
public static function getInstance($name) {
    return static::$instance->get($name);
}

Unfortunately it cannot have the same name. Click here for a hacky way of achieving it.
Now I can say the following in my User class:
var foo = Container::getInstance(Foo::class);

Service Locator for DI Container


AWS ELB - downstream connections higher than workers count

Question: This might be something I fundamentally don't understand about AWS application elastic load balancer set up.
It's very simple. The load balancer is a public front end, forwarding all traffic to a single target group.
There are N containers in a target group. Each container running some number of workers, say X. Per Amazon documentation it is suggested to have containers with keep-alive enabled and set to a higher value than ELB inbound timeout.
But that essentially means that there is absolutely no point in creating more than N*X connections from ELB to targets, because requests coming on extra connections would just wait in the queue on the target, potentially forever.
From my experiments that is exactly what is happening. ELB opens as many connections to targets as it receives, and during spikes in traffic these extra requests fail.
Kind of defeats the purpose of ELB, does not it?
Ideally there would be an option that would limit how many connections to open per target and ELB would keep extras in a wait queue on its side, before forwarding to targets on their open connections (or by new targets launched by auto-scaling).
I've read all of Amazon ELB documentation and FAQs, and searched extensively here, but did not find any solution other than disabling keep-alives.
Am I missing something basic?
Answer: So my basic understanding is, that ELB is opening the connection from the front end application while there are no backend connections available. If that is correct,
what you're missing is this:
ELB is supposed to open as many connections as the requests. Its the basic concept of ELB to load balance your requests to as many targets registered to it. So the flaw is not with your ELB obviously it should accept all traffic that is being directed at it. The point that you're missing here is that you should have enough targets registered with the ELB to make sure that ELB can load balance all the traffic that it receives without overloading the targets what it means in simple terms is that you need to set up a proper auto scaling policy to account for the spikes in traffic and making sure that enough backend targets are available to handle the load.
Hope this helps!


Interpret multiple logistic regression

Question: I have two groups of patients who were part of an intervention many years ago, and some covariates about their characteristics e.g., age, BMI, and years since the intervention. I have conducted a logistic regression in order to see the association of the intervention (yes/no) with a clinical outcome (yes/no), controlling for these known individual factors such that
$Outcome = \beta_0 + \beta_1 Intervention + \beta_2 Age + \beta_3 BMI + \beta_3 YearsSinceInt$
I have obtained some coefficients from my logistic regression but now when it comes to interpret the results I am being asked by a physician to interpret each coefficient in order to see the effect of each variable on having the binary outcome. I am a bit afraid of running into the Table 2 Fallacy so I argue that we should only interpret the Intervention variable. Nonetheless, it is a cross-sectional study so I am a bit torn whether we can actually give interpretation to each coefficient and only talk about association and never about causal effects.
What do you suggest? Can I give interpretation to each coefficient or I should only interpret the Intervention coefficient? Is there a way to see the effect of each variable on the outcome without running the risk of falling into Table 2 Fallacy and over-interpretating control variables?
Comment: FYI, this is [tag:multiple-regression], NOT [tag:multivariate-regression]
Comment: The real problem is not how you should show the data but how the readers will interpret them. Most of the mdeical literature, such as trials published on NEJM or The Lancet, now use this type of representation (and, thus, readers are aware of what table 2 represents... Or at least they should). You could try to use more models, including also variables not significant in the univariable regression as predictors, to show the relative effects in each model, demonstrating how these variables change.
Answer: The interpretation of fitted regression coefficients $\beta_i$ as causal effect requires some additional insight into your scenario. E.g., if you know for sure that your variable Intervention is not causally affecting any of the other regressors Age, BMI, or YearsSinceInt, then you can interpret $\beta_1$ as the total causal effect of Intervention on the (log-odds of) Outcome. Otherwise, it is only the direct causal effect.
For your variables other than Intervention you first have to convince yourself, probably through domain knowledge, that they are causing Outcome and not the other way around, if at all. The fitted values don't tell you that. Once you have done that, it is similar to the interpretation of the fitted coefficients of your Intervention variable: If you know, through domain knowledge, that any variable is causally influencing another variable, its coefficient is only the direct causal effect, not the total. At least, you don't have to bother about your variables having a causal effect on the variable Intervention because, being intervened on, it is not causally affected by anything.
In general, if you don't know anything about the "causal directions", the fitted coefficients cannot be interpreted as causal effects. They only help you to obtain the covariance between the pertaining variable and the dependent variable if all the other variables are fixed. And, of course, you can use them for prediction: predict the value of Outcome for given values of your independent variables.

Interpret multiple logistic regression


Which Formula One races did Scuderia Ferrari not take part in?

Question: Scuderia Ferrari are the only team to have competed in every season of the Formula One World Championship since its inception in 1950. However, during the 2019 Chinese Grand Prix, the 1000th F1 race, a TV graphic appeared indicating that Ferrari had only taken part in around 970-something. I was surprised to learn that they'd missed that many.
As of the 2019 Mexican GP (the 1015th F1 race), Wikipedia records the Scuderia as having entered 990 races, and started 987. That leaves 25 races that they didn't enter, and another three that they entered but never started.
What were these 28 races, and why did Scuderia Ferrari not compete in them?
Answer: After having this question in my head for months, I decided to consult Wikipedia's comprehensive table of Ferrari's results.
The 25 races in which Ferrari did not compete at all were:

Ten of the 11 Indy 500 races that counted towards the F1 championship between 1950-1960. It was common for European F1 teams not to enter the Indy 500 during its time on the F1 calendar, due to the significant distance involved. Ferrari did enter Alberto Ascari in 1952, but he span off after 40 laps.
The 1950 British GP, the first-ever F1 championship race. Ferrari initially planned to take part, but according to Sky F1's David Croft at the 70th Anniversary GP, there was "an argument over money" and they ultimately did not attend.
The 1959 British GP: a strike in Italy prevented Ferrari from attending.
The 1960 and 1961 US GPs. In both years, this was the final race of the season. The team did not enter in 1960 as they could not win the title, and Enzo Ferrari wanted more time to prepare for 1961, when the engine formula would change to 1.5 litres. They did not enter in 1961 as they had already won both titles at the previous race in Monza, but that race had also seen the horrific death of Ferrari driver Wolfgang von Trips along with 15 spectators.
The 1962 French GP: a metalworkers' strike in Italy prevented Ferrari from attending.
The 1962 US and South African GPs. Ferrari had a poor season in 1962, finishing just 6th in the championship (out of 8) with no race wins, despite having dominated the previous season. They decided not to bother showing up for the final two races.
The 1966 British GP: another metalworkers' strike in Italy prevented Ferrari from attending.
The 1966 Mexican GP. This was the final race of the season, and both championships had already been won by Jack Brabham and his eponymous team. With Ferrari having also lost to Ford at Le Mans that year, they decided to upgrade their Maranello base to give them a better shot at both competitions in '67. The ongoing upgrades gave Ferrari a good reason to skip the last race, as they had in '60 and '62.
The 1967 South African GP. This race took place at the unusually early date of January 2nd, just two months after the previous season's end and four months before the next race at Monaco. Ferrari were still busy with the Maranello upgrades and were forced to sit the race out.
The 1968 Monaco GP. When the '67 Monaco GP eventually did arrive (see above), Ferrari driver Lorenzo Bandini was killed in a fiery crash. Safety improvements were made to the track for '68, but Ferrari felt they were inadequate and refused to attend.
The 1969 German GP: Ferrari had intended to enter, but withdrew at the last minute for unknown reasons. It's worth noting that Enzo had scaled back the team's operations that year in order to focus on rebuilding and restructuring, making 1969 a bit of a write-off for the team in general.
The 1973 Dutch and German GPs: Ferrari were using the ageing 312B that season, which was slow and unreliable; there were multiple other races that year in which they only entered one car. Several other teams withdrew from the German GP after the horrific death of Roger Williamson at the preceding Dutch GP.
The 1976 Austrian GP. The FIA had initially disqualified James Hunt's McLaren from that year's Spanish GP, but later overturned the decision and allowed Hunt to keep his race win. Hunt's title rival that year was Ferrari's Niki Lauda, who had just suffered his infamous fiery crash at the preceding German GP. Ferrari thus refused to enter the Austrian GP - Lauda's home race - to protest Hunt's reinstatement.

The three races that they entered, but did not start, were:

The 1950 French GP: Ferrari withdrew their two drivers prior to qualifying, for reasons unknown. (Peter Whitehead, a privateer, competed and finished third in a Ferrari 125, but this does not count as a Scuderia Ferrari entry.)
The 1982 Belgian GP: Ferrari driver Gilles Villeneuve was killed during qualifying, and Ferrari immediately withdrew from the race.
The 1982 Swiss GP: With Villeneuve's teammate, Didier Pironi, having suffered a career-ending accident two races earlier, Ferrari were down to one driver - Patrick Tambay, who'd replaced Villeneuve - and he was unable to start after suffering a pinched nerve in his back.

Honourable mentions:

The 1957 Pescara GP: Enzo Ferrari believed that the 25.7 km street circuit was too dangerous, especially after 13 people had been killed at that year's Mille Miglia, and refused to enter. Driver Luigi Musso insisted on taking part, and Enzo eventually allowed him to, but refused to count it as an official Scuderia Ferrari race entry. Most modern sources do count it, but I figured it was worth noting.
The 1980 Spanish GP and 1981 South African GP: Ferrari boycotted these races as part of an ongoing dispute between governing body FISA, which Ferrari and the other manufacturer teams were aligned with, and FOCA, a union of privateer teams. The former was post-facto declared a non-championship event, while the latter was run to Formula Libre regulations.
Comment: Good research. Indy500 (in the years when it counted as part of F1) was my initial instinct, but I'm surprised to see that it's less than half of their no-shows.


SQL LIMIT returns no results where no LIMIT returns results

Question: SELECT * FROM mm_tfs
WHERE product_slug LIKE '%football%'
    AND schoolid = '8' AND category_id ='21'
LIMIT 4

Returns 4 values like I'm asking, but the following statement returns 0 - is there a rule about using the OR statement that I'm not familiar with?  My assumption is that it should return all of the values in 1 (or more if it weren't for the limit).
SELECT * FROM mm_tfs
WHERE (product_slug LIKE '%football%'
   AND schoolid = '8' AND category_id ='21')
OR (product_description LIKE '%football%'
   AND schoolid = '8' AND category_id ='21')
LIMIT 4

NOTE by cyberkiwi The first OR portion of Q2 is exactly the same as the WHERE clause on Q1
product_description LIKE '%football%' AND schoolid = '8' AND category_id ='21

Without the OR Statement by itself does produce the desired result as long as it does not have the limit.  When the OR statement is used with the LIMIT as well, there are 0 values returned.
SELECT * FROM mm_tfs
WHERE product_description LIKE '%football%'
    AND schoolid = '8' AND category_id ='21'
LIMIT 4

^-- That produces 0 results
SELECT * FROM mm_tfs
WHERE product_description LIKE '%football%'
    AND schoolid = '8' AND category_id ='21'

^-- That produces results
The strangest part of this is that all of these queries product the right effect in my PHPMYADMIN SQL query window, but not in the application itself.
Comment: This is very interesting. Can someone explain why it's wrong instead of giving alternate approach? +1
Comment: @JonH: Yeah... another example of assumptions based on incomplete data. Sorry, just had to.
Comment: MySQL - 5.0.77-log if that helps
Comment: Looks to be a boolean logic error rather then specific to OR.  Wrap the entire thing around braces, that is your entire condition around ( and ).
Comment: I edited the question but can you confirm again that the queries are as shown? Are the queries formatted by PHP or are you running from console directly?
Comment: @Nishant - I dont have mySQL, but to me it is probably some boolean logic error.  That is why I suggested to strip down the query by removing some conditions, testing, and only then readding the stripped parts given the issue goes away.
Comment: @JonH I donot think so. I visualize this way, MySQL engine goes to a row and the compare against first of OR block. It maches and result returned. Since both OR condition are same this _should_ work, unless OP did some mistake in copying the exact query from his console to SO.
Comment: I cannot reproduce the problem. Try posting your table definition.
Comment: I would post that bug on MySQL. Attach your Db if possible, and include the exact MySQL version
Comment: Is LIKE '%variable%' case sensitive?
Comment: @cyberkiwi I cant reproduce this.
Comment: Another reason not to use mySQL.  Sorry just had to...
Comment: @Bob Caveza: We'd need your MySQL version, table definition and if possible some data from that table to see if it can be reproduced.
Comment: Can you do an EXPLAIN for `all queries`? I suspect MySQL is doing something funny.  You're not using this a s subquery somewhere else are you?
Comment: @Mchl - I don't know I remember mySQL having a bug in the number of days in a month....
Comment: @JonH: Congratulations on sweet memories (I'd be grateful if you provided a bug #). Notice however, that in the case on hand nobody was able to reproduce the behaviour so far.
Comment: @Mchl - Thanks for the congrats it made me smile :)
Comment: @Bob Cavezza: I reviewed changelogs for a few versions following 5.0.77 and didn't see anything indicating there was a bug like this. Please add the output of `SHOW CREATE TABLE mm_tfs` to your question (you should be able to edit it)
Comment: Are school_id and category_id really text, not integers?
Comment: Wow! This question has gotten way longer since I favorited it. (however, all my favorites are temporary. I did up vote the question though)
Comment: No, I was wrong about school_id (it is integer), but categoryid is varchar - Comes from affiliate program api using letters as well as numbers.
Answer: Try to put ( ) arround the entire condition
( (...AND...) OR (...AND...) )

Like so:
  SELECT * FROM mm_tfs WHERE
  (  (product_slug LIKE '%football%' AND schoolid = '8' AND category_id ='21')
   OR (product_description LIKE '%football%' AND schoolid = '8' AND category_id
   ='21')
  ) LIMIT 4

Comment: Nope, doesn't change result set from 0
Comment: Would he need these brackets if he didn't have `LIMIT 4` statement?
Comment: @Bob Cavezza - Eliminate small pieces to see the issue.  For example, get rid of the 2 ANDs from the OR portion and have it just or product_description LIKE '%football%'...Then see if any rows come back.  I would make use of  ( and ) some more.  These AND's and ORs could screw this up for ya...
Comment: I did - that second statement returns expected values just as the first statement did - which makes this even more perplexing
Answer: You are repeating some of the conditions which is not necessary. Try this instead:
SELECT * FROM mm_tfs
WHERE
   (product_slug LIKE '%football%'
 OR product_description LIKE '%football%')
   AND schoolid = '8'
   AND category_id ='21'
LIMIT 4

UPDATE:
I have created the following table:
create table mm_tfs2 (schoolid varchar(2), categoryid varchar(2),
                      description varchar(20), slug varchar(20));

And 5 times:
insert into mm_tfs2 values (8, 21, '', 'football');

And finally the query:
select * from mm_tfs2 where
(slug like '%football%' and schoolid = 8 and categoryid = 21)
or (description like '%football%' and schoolid = 8 and categoryid = 21)
limit 4;
+----------+------------+-------------+----------+
| schoolid | categoryid | description | slug     |
+----------+------------+-------------+----------+
| 8        | 21         |             | football |
| 8        | 21         |             | football |
| 8        | 21         |             | football |
| 8        | 21         |             | football |
+----------+------------+-------------+----------+
4 rows in set (0.00 sec)

So I'm sorry to say that I'm not able to recreate the problem.
Comment: While that is true, does it explain why the query doesn't work?
Comment: @cyberwiki: lets see your answer, smarty pants :p
Comment: @VoodooChild - we're trying to figure out why OR doesn't work. This answer doesn't solve that
Comment: @cyberkiwi: Yes, I am interested to know why it doesn't work as well.
Comment: Yeah, you're right... it doesn't explain why the OP's query doesn't work. Hmmmm...
Comment: @cyberkiwi, I'm not able to reproduce the problem on my own mysql server.
Comment: @Bob Cavezza, it works fine on my server with and without the `LIMIT`. I wonder what your table definition looks like.
Comment: @Klaus everything is setup as varchar utf8_general_ci - is this what you're referring to?
Comment: @Bob, having changed the ids from ints to varchar I'm still not able to reproduce the problem :-(
Comment: Wow, definitely going above and beyond, but I'm afraid it may be a lost cause.  I really appreciate the help.  :-)
Answer: I think you could clean it up a little bit, you're getting only rows for one school, and one category ID, so there's no reason that you should have to check for those both times:
SELECT *
FROM mm_tfs
WHERE schoolid = '8'
    AND category_id ='21'
    AND (product_slug LIKE '%football%'
         OR product_description LIKE '%football%')
LIMIT 4;

Comment: Hmm, beaten by Klaus.  Need to be quicker next time.  :P
Comment: LOL @galador, you have all the time in the world to explain WTH going on here (see the comments in OP). I'll upvote for nailing it.


Configuration for ALSA Receiving and Playing Raw PCM on Raspberry Pi Analogue Output

Question: I have successfully played audio on the 3.5mm headphone jack analogue output on the Raspberry Pi using PulseAudio and Qt5.4's QAudioOutput. The audio is successfully being transported from a remote microphone over a XBee link at 8KHz with 8-bit samples.
There was a huge latency with PulseAudio so I decided to link against libasound ( ALSA ) and play the audio directly. My code is below and successfully opens and plays sound but it is almost unrecognizable, there is lots of crackling and squeaking. If I speak into the remote microphone, I very quickly hear elevated scratching and squeaking in the headphones from the Pi ( but it is not good audio ) . I think I have my parameters messed up.
1.) The data is transmitted in BigEndian - QAudioOutput allows you to inform it that the samples are BigEndian. But these are U8 samples so do I need to worry about endianness?
2.) Can you see anything wrong with my configuration below?
3.) How do I figure out Fragment Size for ALSA for the output on the Pi?
4.) Can someone explain how I should write my buffer to the audio device?
Thanks!
Here is my code:
UdpReceiver::UdpReceiver(QObject *parent) :
    QObject(parent)
{

    // Debug
    qDebug() << "Setting up a UDP Socket...";

    // Create a socket
    m_Socket = new QUdpSocket(this);

    // Bind to the 2616 port
    bool didBind = m_Socket->bind(QHostAddress::Any, 0x2616);
    if ( !didBind ) {
        qDebug() << "Error - could not bind to UDP Port!";
    }
    else {
        qDebug() << "Success binding to port 0x2616!";
    }

    // Get notified that data is incoming to the socket
    connect(m_Socket, SIGNAL(readyRead()), this, SLOT(readyRead()));

    // Init to Zero
    m_NumberUDPPacketsReceived = 0;

}

void UdpReceiver::readyRead() {

    // When data comes in
    QByteArray buffer;
    buffer.resize(m_Socket->pendingDatagramSize());

    QHostAddress sender;
    quint16 senderPort;

    // Cap buffer size
    int lenToRead = buffer.size();
    if ( buffer.size() > NOMINAL_AUDIO_BUFFER_SIZE ) {
        lenToRead = NOMINAL_AUDIO_BUFFER_SIZE;
    }

    // Read the data from the UDP Port
    m_Socket->readDatagram(buffer.data(), lenToRead,
                         &sender, &senderPort);

    // Kick off audio playback
    if ( m_NumberUDPPacketsReceived == 0 ) {

        qDebug() << "Received Data - Setting up ALSA Now....";

        // Error handling
        int err;

        // Device to Write to
        char *snd_device_out  = "hw:0,0";

        if ((err = snd_pcm_open (&playback_handle, snd_device_out, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
            fprintf (stderr, "cannot open audio device %s (%s)\n",
                    snd_device_out,
                    snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
            fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
            fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
            fprintf (stderr, "cannot set access type (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_U8)) < 0) { // Unsigned 8 bit
            fprintf (stderr, "cannot set sample format (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        uint sample_rate = 8000;
        if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &sample_rate, 0)) < 0) { // 8 KHz
            fprintf (stderr, "cannot set sample rate (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 1)) < 0) { // 1 Channel Mono
            fprintf (stderr, "cannot set channel count (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
            fprintf (stderr, "cannot set parameters (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        snd_pcm_hw_params_free (hw_params);

        // Flush handle prepare for playback
        snd_pcm_drop(playback_handle);

        if ((err = snd_pcm_prepare (playback_handle)) < 0) {
            fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        qDebug() << "Done Setting up ALSA....";

    }

    // Grab the buffer
    m_Buffer = buffer.data();

    // Write the data to the ALSA device
    int error;
    for (int i = 0; i < 10; ++i) {
        if ((error = snd_pcm_writei (playback_handle, m_Buffer, NOMINAL_AUDIO_BUFFER_SIZE)) != NOMINAL_AUDIO_BUFFER_SIZE) {
            fprintf (stderr, "write to audio interface failed (%s)\n",
                     snd_strerror (error));
            exit (1);
        }
    }

    // Count up
    m_NumberUDPPacketsReceived++;

}

Answer:
No.
snd_pcm_hw_params_set_rate_near() will change the rate to the nearest supported rate. You probably don't want that.
ALSA has no fragment size.
It has buffer and period sizes; you need to set them according to your timing requirements (see ALSA: Relation between period size of speaker and mic).
You cannot simply output the received samples; you must resample them to the speed of the playback device (which is not exactly the same even if it uses the same nominal sample rate).
Comment: Could you please comment more on how I would resample my UDP received samples? Can I not just setup ALSA to my configuration and then write to it's buffer every time I receive more Audio data from UDP? Thank you.
Comment: The sender's and receiver's clocks will run at slightly different rates.


Using two transaction managers and session factories with spring & hibernate annotations

Question: I have two transactions managers defined in applicationContext, referencing two completely different sessionFactories. I am attempting to use annotations with one of the transactionmanagers however it seems to be applying all annotations to both transaction managers.
The new db I am migrating to has  <property name="hbm2ddl.auto">create</property> and the other which I am migrating from has  <property name="hbm2ddl.auto">validate</property>
I have annotated my new domain objects with @Entity hibernate is attempting to validate these classes on the legacy database, which I thought I had set up to ignore annotations (by not specifying annotations). Here are the relevant snippets of appContext:
    <tx:annotation-driven transaction-manager="transactionManager1" />
    <context:property-placeholder location="file:${catalina.home}/conf/database.properties" ignore-unresolvable="true"/>

    <bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="dataSource1" p:configLocation="WEB-INF/classes/hibernate.cfg.xml" p:packagesToScan="com.mycompany"/>

<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:driverClass="${app.driverClassName}" p:jdbcUrl="${app.url}"
p:user="${app.username}" p:password="${app.password}"

    <bean id="transactionManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory1" />

and the legacy db in same appContext :
<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"    p:dataSource-ref="dataSource2" p:configLocation="WEB-INF/classes/hibernateTraveller.cfg.xml" p:packagesToScan="com.mycompany"/>

<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
p:driverClass="${app2.driverClassName}" p:jdbcUrl="${app2.url}"
p:user="${app2.username}" p:password="${app2.password}"

<bean id="transactionManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
            p:sessionFactory-ref="sessionFactoryTraveller">
<qualifier value="legacyDB"/>
</bean>

any pointers ?
Answer: You should use LocalSessionFactoryBean instead of AnnotationSessionFactoryBean for your legacy database that does not use annotations.  The whole purpose of the AnnotationSessionFactoryBean is to use annotations.  If you don't want to use annotations, LocalSessionFactoryBean is the way to go.


How to debug a delete query?

Question: I'm trying to debug an issue with a delete query not happening. I've added logging before the query, after the query and I'm logging the return value of the query. Everything looks right, the only problem is that the rows are still in the database.
It's a simple query, I can't see anything wrong with it:
<code>$result = db_delete('cloud_files')
  ->condition('uri', $uri)
  ->execute();
</code>
I've checked the $uri in every way I possibly can and it's right. I've tried executing the delete query by hand and it works just fine. The table name is correct, the field name is correct, the $uri is correct. I don't know what else to check.
I've used the Devel module to log the queries and while it logs queries that happen both before and after this query, it doesn't log this query at all. However, I've got error_log() calls before and after the query so I know this code is being run and it reports that it is running successfully.
I've even added code to select the data before deleting it and it works!
<code>$select = db_select('cloud_files', 'cf')
                ->fields('cf')
                ->condition('uri', $uri)
                ->execute();

$entry = $select->fetchAssoc();

error_log("entry: ".print_r($entry, true));
</code>
That works, it prints out the row I'm trying to delete.
dqp() doesn't work for delete queries, so I don't know of any way of getting the truly final sql being executed.
I've made sure that if I connect using the mysql command line client from the drupal host with the user/password in the drupal config that all of the commands work.
How do I debug this? What is going wrong?
EDIT:
Is there any kind of transaction happening during a delete action? Could this behavior be the result of those delete queries happening in a transaction that gets aborted?
Comment: Perhaps you could use some hook like `hook_query_alter()` to display the final query that will be executed?
Comment: You can cast the query object to a string before calling execute on it to get the raw SQL string, that might help to shed some light
Comment: I tried that, but it doesn't resolve the parameter replacements, so its not the real query. I've gotten sql statement you suggest above and plugged my values into it and it works, but that's not surprising.
Comment: @KristerAndersson: Delete queries don't invoke that hook.
Comment: Wild stab in the dark...any chance you've created the table in question manually and forgotten to grant `DELETE` permissions to the user that Drupal is using to connect to the db? Also, try running a `db_query('DELETE FROM...');` directly and see if you get the same problem. That runs a flat query string against the db, if it doesn't work I'd say the problem is with the MySQL server somewhere
Comment: @Berdir - Good to know, it was just a though=)
Comment: It turns out that my guess was right about a db transaction getting aborted. node_delete() begins a transaction and the action was being aborted after 2 minutes (there are a LOT of files to delete). So, while the delete queries were issued, they were issued inside a transaction that got automatically rolled back.
Answer: With a problem like this, you may need to turn on MySQL query logging.  Once logging is enabled, put a noticeable query right before and after your DELETE query (do you can have something easy to search for), check the logs, and go from there.  Just be sure to disable logging when you are done.  It can kill performance and also fill up the disk pretty quickly.
Comment: You can also enable the devel query log, that is as late as possible and allows to switch between displaying placeholders and the actual values.
Comment: @KennyWyland mentioned using the Devel log already.  This suggestion would be the next step to see what is actually going on.
Comment: @KennyWyland, I take it you saw the transaction here and not the drupal devel log?
Comment: I started trying to just trace through the node delete process and in node_delete() I saw the db_transaction() call. I didn't find it using the query logging, but I felt your answer was the actual answer to "how to debug a delete query in drupal" even though it wasn't the thing that truly solved my problem.
Answer: It turns out that my guess was right about a db transaction getting aborted. node_delete() begins a transaction and the action was being aborted after 2 minutes (there are a LOT of files to delete from CloudFiles and it took too long and the php process was killed). So, while the delete queries were issued, they were issued inside a transaction that got automatically rolled back.

How to debug a delete query?


Custom charting solution for Metro Style Apps - Which path would you recommend?

Question: I'm looking to build a Windows 8 Metro Style application (C# with XAML) that requires the ability to create various types of charts (line, bar, pie, bubble etc.) that are responsive and dynamic with the ability to evolve with user interaction (e.g. touching a particular data point gives you info about that point, rotating pie charts with your finger, resizing charts on the fly with the use of two fingers).
I've done some research around the types of built-in/third party solutions available and came across the Microsoft Chart Controls packaged with .NET 4. Unfortunately, this API is not natively available with WinRT. Thus, I will most likely be building my own custom solution.
So, my question is this, which path would you recommend and why for creating a custom charting solution for Metro Apps? Should I go with a Silverlight solution? A Canvas solution leveraging the built-in Windows.UI.XAML API? DirectX 11 possibly? Something else? Please provide pros and cons for the solution you recommend.
Comment: This question is not a good fit for SO.  Polling is highly discouraged.
Answer: What platforms are you targeting ? I don't know if ARM platforms will have GPUs good enough to handle a DX11 solution, for example.
The dx11 solution is powerful, but seems a bit overkill for charts. Silverlight is a subset of WPF, so you'd better use the XAML API directly : with a godd canvas, you can do what you want. And I wouldn't expect Microsoft to support Silverlight too long.
Concretely : XAML or JS. Both are powerful. And will be touch-ready.
Comment: as of right now, the target is any tablet/PC that can run Windows 8


Why am I getting SON::ParserError trying to validate response using json_matchers?

Question: app/models/author.rb:
<code>                class Author < ApplicationRecord
                  validates :name, presence: true,
                                   uniqueness: { case_sensitive: false }
                end

</code>
app/serializers/author_serializer.rb:
<code>              class AuthorSerializer < ActiveModel::Serializer
                  attributes :id, :name, :bio
                end

</code>
spec/spec_helper.rb:
<code>    #...

require 'json_matchers/rspec'
JsonMatchers.schema_root = 'spec/support/api/schemas.author.json':
 #...
</code>
spec/support/api/schemas/authors/show.json:
<code>{
  "id": "file:/authors/show.json#",
  "type": "object",
  "definitions": {
    "authors": {
      "description": "A collection of authors",
      "example": [{ "id": "1", "name": "Name" }],
      "type": "array",
      "items": {
        "$ref": "file:/author.json#"
      }
    }
  },
  "required": ["authors"],
  "properties": {
    "authors": {
      "$ref": "#/definitions/authors"
    }
  }
}
</code>
spec/requests/authors_show_request_pec.rb:
<code>    RSpec.describe 'Authors', type: :request do
      setup { host! 'api.example.com' }

      describe 'GET /author/:id' do
        let!(:author) { create(:author, id: 13, name: 'Name', bio: 'bio') }
        it 'returns requested author' do
          get author_path(13)
          expect(response).to have_http_status(200)
          author_from_response = JSON.parse(response.body)
          expect(author_from_response['name']).to eq(author.name)
          expect(author_from_response['bio']).to eq(author.bio)
          expect(response).to match_json_schema('author')
        end
  end
</code>
end
Response body contains all expected data, but spec if falinig to validate matching response to json schema.
Json_matchers gem seems to be configured according to manual.
Error that appears:
<code>JsonMatchers::InvalidSchemaError:
       783: unexpected token at '}
       '
</code>
Answer: Try removing the trailing commas. The ruby JSON parser does not like them.
Your JSON response should be:
<code>{
  "id": "file:/authors/index.json#",
  "type": "object",
  "definitions": {
    "authors": {
      "description": "A collection of authors",
      "example": [{ "id": "1", "name": "Name" }],
      "type": "array",
      "items": {
        "$ref": "file:/author.json#"
      }
    }
  },
  "required": ["authors"],
  "properties": {
    "authors": {
      "$ref": "#/definitions/authors"
    }
  }
}
</code>
Notice no trailing commas after the "items", "authors" nor "properties".
Comment: Thanks! But unfortunately this leads me to another error:JsonMatchers::InvalidSchemaError:
       783: unexpected token at '}
       '
Comment: still the problem was with the commas. Thank you very much for your help! The problem has been solved.


Works properly, but not when running as administrator

Question: My batch file makes another batch file. It works when you run it normally.
<code>@echo off
type NUL > batchfile.bat
ECHO @echo off >> batchfile.bat
ECHO set hostspath=%%windir%%\System32\drivers\etc\hosts >> batchfile.bat
ECHO exit >> batchfile.bat
exit
</code>
However, when you run it as an administrator, it doesnt work. I need to make it run properly also when running as administrator. What is the correct way to do it ?
Answer: When you run as administrator, it changes the current context directory. I'm not sure where it changes to, but you can avoid that problem by specifying the full output path to the new batch file, like so:
<code>@echo off
type NUL > "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
ECHO @echo off >> "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
ECHO set hostspath=%%windir%%\System32\drivers\etc\hosts >> "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
ECHO exit >> "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
exit
</code>
UPDATE: I just discovered that there's a way to dynamically change the current directory to the same one as the currently executing batch file. So, the following is probably a cleaner solution. It just involves adding one line at the top of the original script:
<code>cd %~dp0
@echo off
type NUL > batchfile.bat
ECHO @echo off >> batchfile.bat
ECHO set hostspath=%%windir%%\System32\drivers\etc\hosts >> batchfile.bat
ECHO exit >> batchfile.bat
exit
</code>
Comment: Ah, it changes the current directory to _C:\Windows\System32_. I should've figured that.


proof regarding limit of function of 2 variable along different paths

Question: I tried to proof a theorem that is wrong.
Now am trying to figure out what's wrong with my proof.
Theorem :- If f(x,y) is a real valued function , such that it has the same limit L at (0,0), along every line y=m*x , then it has limit L at (0,0).
PROOF :-

for any $\epsilon >0$ , no matter however small, $\exists$ an $\delta >0$ such that $\mid f(x,mx)-L \mid < \epsilon $ whenever $\sqrt{x^2(1+m^2)} < \delta$
Above statement holds for any $m$.
Choose any $\epsilon$ , say $\epsilon_0$, now corresponding to this :-
Let the set of all $\delta$ corresponding to various $m$ be $\{\delta(m)\}$ and let $\xi(\epsilon_0) = $  inf{δ(m)|m∈R} :-
for all $m$ , $\mid f(x,mx)-L \mid < \epsilon $ whenever $\sqrt{x^2(1+m^2)} < \xi(\epsilon_0)$
Now consider the region $ R=\{(x,y) : \sqrt{x^2+y^2} < \xi(\epsilon_0))\}$
Corresponding to any element of this set an $m$ can be found such that $y=mx$
which means whole region is same as the region  $\sqrt{x^2(1+m^2)} < \xi(\epsilon_0)$

The proof is completed using 5th point and definition of limit.
The case of (x,y) approaching (0,0) along X-axis is considered separately.
Please help me in finding what is wrong in this proof.
Answer: For each $\varepsilon>0$, you take a $\delta>0$ such that… But the $\delta$ should actually be written as $\delta(m)$, since it depends upon the choice of $m$. And then you take the minimum of all $\delta(m)$. That's where the problem lies. In general, the minimum doesn't exist and furthermore $\inf\{\delta(m)\,|\,m\in\mathbb{R}\}$ may well be $0$.
Comment: @KavitaJuneja Yes, you are right.
Comment: if minimum does not exist we can take inf{δ(m)|m∈R} as $\xi$ . so the only problem is the case where inf{δ(m)|m∈R} is 0 for some $\epsilon$ ?am I right?


Scrapy - Messy text in a csv format

Question: I am successfully extracting the text I need from a list of websites. The issue is when I am saving it in a csv format some of the rows gets messy because of long texts and breaks between rows within the text.
For example:
(couldn't upload the image :( )
So the lines starting with 0s/1s are of different websites but the last website in this image is starting several new rows in the csv file. This blocks me from continuing with the text analysis.
Any help will be highly appreciated as couldn't find a solutions so far.
Many thanks
Edit - adding code:
neither this line:
data = "".join(sel.select("//body//text()").extract()).strip()

nor this code line:
data = " ".join(" ".join(sel.select("//body//text()").extract()).strip().split())

didn't work
Comment: could you add some more details about that extracted text or provide some sample link and entities you are extracting from that page ?
Answer: You could remove the new line characters and all by performing both join() and split() on that given text. Before yielding the items make sure that you have properly cleaned the extracted text.
Suppose I want to fetch some sports news of the following url, and will be look like this,
In [1]: text = response.xpath('//div[@id="page-1"]/p//text()').extract()

In [2]: text
Out[2]:
[u'\nThe retirement of Jonathan Trott from international cricket last night cast \nfurther doubt on the position of Peter Moores, who admitted he was uncertain \nabout his own future as head coach. The decision to recall Trott as an \nopening batsman for the series against West Indies, 18 months after his \nbreakdown in Australia, backfired spectacularly as England slid to a defeat \nin Bridgetown on Sunday that enabled the home side to level the Test series \n1-1.\n',
 u'\nThe defeat in Bridgetown added to the pressure on Moores after a disastrous \nWorld Cup earlier this year. The head coach conceded yesterday that']

In [3]: cleaned_text = ' '.join(' '.join(text).split())

In [4]: cleaned_text
Out[4]: u'The retirement of Jonathan Trott from international cricket last night cast further doubt on the position of Peter Moores, who admitted he was uncertain about his own future as head coach. The decision to recall Trott as an opening batsman for the series against West Indies, 18 months after his breakdown in Australia, backfired spectacularly as England slid to a defeat in Bridgetown on Sunday that enabled the home side to level the Test series 1-1. The defeat in Bridgetown added to the pressure on Moores after a disastrous World Cup earlier this year. The head coach conceded yesterday that'

hope this might help
Comment: Thank you very much for replying. I'm sorry for not copying my code at first but here it is:
data = "".join(sel.select("//body//text()").extract()).strip()
and after your suggestion I changed it to:
data = " ".join(" ".join(sel.select("//body//text()").extract()).strip().split())
Unfortunately still causing the same problem (though in a bit friendlier way)
Comment: can I get the url please ?
Comment: Still doesn't work though I think I found the problem - cell length limit in a CSV file:
http://stackoverflow.com/questions/18842866/is-there-a-cell-length-limit-writing-csv-files-with-textcsv
will try to break / cut the text after exceeding this limit. Thanks

Scrapy - Messy text in a csv format


Can connect to sftp server through loopback, but not from the outside

Question: I've set up an SFTP server, and the only way I seem to be able to connect to it is from the local machine itself through loopback, which is quite confounding.
The SFTP server is not the standard linux sftp server. It's an apache sftp server running inside a spring-boot application which also has common FTP as well as https endpoints. Ubuntu's SSH is additionally runnig on the system on port 22. The SFTP endpoint of the application is currently mapped to port 2222, though that is easily changed.
The machine the application is running on is an AWS EC2 instance, though after becoming aware of the problem I realised that I can reproduce it on my development machine. Inbound traffic on port 2222 is allowed for all IPs.
The server has a keypair, though only to establish its identity to clients. Clients authenticate by password.
If I connect to the SFTP endpoint from the machine the server is running on via loopback, everything is working nicely:
sftp -oPort=22222 -vvv email@example.com
OpenSSH_7.2p2 Ubuntu-4ubuntu2.8, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "domainname.net" port 22222
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to domainname.net [127.0.0.1] port 22222.
debug1: Connection established.

A bit more logging follows, then the password prompt, all very normal.
If I however try to connect from another machine, or from the same machine but using the public IP instead of the domain name so it doesn't get resolved as 127.0.0.1, all I get is this:
sftp -oPort=12 -vvv user@<pu.bl.ic.ip>
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "domainname.net" port 2222
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to domainname.net [<pu.bl.ic.ip>] port 12.
debug1: connect to address <pu.bl.ic.ip> port 2222: Connection refused
ssh: connect to host domainname.net port 2222: Connection refused

As mentioned earlier, the firewall for the port is open. The behaviour when it is closed and the port can't be reached at all is different, so I'm pretty sure that's not the problem.
The apache sshd server doesn't log anything about the interaction, not even on TRACE, which makes me suspect that it never actually gets the request and it's shot down by something else beforehand.
I tried to do a port forwarding, so that I connect to port 12 from the outside and then it gets routed to 2222, which is how we handle normal FTP connections, but that behaves the exact same way.
Does anybody have any ideas what could be shooting down that connection, or any idea how I can diagnose the issue further?
Comment: Did you check the logfile(s) of the sftp service? Could the port be blocked by a firewall?
Comment: sftp normally connects to ssh (ie port 22) on the server, and the sshd configuration on that server determines what subsystem will handle the sftp.  In my case, I have "Subsystem sftp /usr/lib/openssh/sftp-server".  What do you have configured?  Concerning the firewall, if ssh is working, you're good to go.
Comment: As mentioned, this is a separate server to the ubuntu ssh service, and therefore using a different port. The firewall is open though, the behaviour when running into a closed firewall is quite different from what I'm observing (also I've checked a couple dozen times).
Answer: So, the mystery is solved. It had to do with an apache sshd configuration which I accidentally copied over from an example.
In short, the SshServer.host property does not configure anything about the host of the server, rather it configures another host as a valid client, so only that host is able to connect.
Once I removed that configuration, connection from anywhere worked just fine.


Compute the distribution of $S_{N_t}$

Question:
In a Poisson process, we define $S_n$ is the arrival epoch of the $n$th occurence, $N_t$ is the number of occurence in $(0,t]$. Compute the distribution of $S_{N_t}$.

I made many trials to this problem. We need to compute the conditional probability of $S_k\le u$ given $N_t=k$.
We have $P(N_t=k)=\frac{(\lambda t)^k}{k!}e^{-\lambda  t}$, thus we only need to compute $P(S_k\le u, N_t=k)$.
The event $\left\{S_k\le u\right\}$ is the same as $\left\{N_u\ge k\right\}$, thus $P(S_k\le u, N_t=k)=P(N_u- N_t\ge 0)=1-P(N_t-N_u>0)$. Note that $N_t-N_u>0$ implies $t>u$, thus $P(N_t-N_u>0)=P(N_{t-u}>0)=1-P(N_{t-u}=0)$, and we can compute this probability easily.
I'm not sure whether my proof is correct. I will appreciate any help in case my proof is incorrect. Thanks in advance.
Answer: Fix $t\in [0,\infty)$, and note that for $x \geq 0$, we have that
\begin{align*}
P(S_{N_t}\leq x)
&= \sum_{k=0}^\infty P(S_{N_t}\leq x,N_t=k) \\
&=\sum_{k=0}^\infty P(S_{k}\leq x,N_t=k) \\
&=\sum_{k=0}^\infty P(N_x\geq k,N_t=k) \\
&=1_{\{x\geq t\}}(x)\sum_{k=0}^\infty P(N_{x}-N_{t}\geq 0,N_t=k)+ 1_{\{x< t\}}(x)\sum_{k=0}^\infty P(N_{t}-N_{x}\leq 0,N_t=k)\\
&=1_{\{x\geq t\}}(x)\sum_{k=0}^\infty P(N_{x}-N_{t}\geq 0,N_t=k)+ 1_{\{x< t\}}(x)\sum_{k=0}^\infty P(N_{t}-N_{x} = 0, N_t=k)\\
&=1_{\{x\geq t\}}(x)\sum_{k=0}^\infty P(N_{x}-N_{t}\geq 0,N_t=k)+ 1_{\{x< t\}}(x)\sum_{k=0}^\infty P(N_t-N_{x} = 0, N_x=k)\\
&= (*)
\end{align*}
Note that since $(N_t)$ has independent increments we have that for $x\leq y $ that $N_y -N_x$ is independent of $N_x=N_x-N_0$. Therefore
\begin{align*}
(*) &=1_{\{x\geq t\}}(x)\sum_{k=0}^\infty P(N_{x}-N_{t}\geq 0)P(N_t=k)+ 1_{\{x< t\}}(x)\sum_{k=0}^\infty P(N_{t}-N_{x} = 0) P(N_x=k)\\
&=1_{\{x\geq t\}}(x)\sum_{k=0}^\infty P(N_{x-t}\geq 0)P(N_t=k)+ 1_{\{x< t\}}(x)\sum_{k=0}^\infty P(N_{t-x}= 0) P(N_x=k)\\
&=1_{\{x\geq t\}}(x)\sum_{k=0}^\infty P(N_t=k)+ 1_{\{x< t\}}(x)\sum_{k=0}^\infty \frac{(\lambda( t-x))^0}{0!}e^{-\lambda (t-x)} \frac{(\lambda x)^k}{k!}e^{-\lambda x}\\
&=1_{\{x\geq t\}}(x)+ 1_{\{x< t\}}(x) e^{-\lambda (t-x)} \sum_{k=0}^\infty  \frac{(\lambda x)^k}{k!}e^{-\lambda x}\\
&=1_{\{x\geq t\}}(x)+ 1_{\{x< t\}}(x) \frac{e^{\lambda x}}{e^{\lambda t}} \\
\end{align*}
and for $x<0$ we have that $P(S_{N_t} \leq x)=0$.
Comment: It is $1$ if $x\geq t$ and 0 otherwise. The correct notation should be
$$
1_{\{x \geq t\}}(x)= \left\{  \begin{array}{cc} 1 & \text{if } x\geq t \\ 0 & \text{if } x<t \end{array} \right.
$$
Comment: Excuse me, what does the notation $1_{\left\{x\ge t\right\}}$ mean?

Compute the distribution of $S_{N_t}$


Find all lines in a file that have two strings on the same line

Question: I need to find a regular expression that will work with the following dummy input:
Lorem ipsum dolor sit -V amet, consectetuer adipiscing elit.
Aenean commodo ligula Charge Tip eget dolor. Aenean massa.
Cum sociis natoque penatibus et magnis dis parturient montes.
Nascetur ridiculus Charge Tip mus. Donec quam felis,-Vultricies nec.
Pellentesque eu, pretium quis, sem. Nulla consequat massa.
Quis enim. DonecCharge Tippede justo, fringilla vel, aliquet nec.-V
Vulputate eget, arcu. In enim justo, rhoncus ut.

And produce the following result:
Charge Tip -V
Charge Tip -V

Some notes:

Charge Tip is always before -V
Charge Tip may be attached to other data on the left and right.
-V may be attached to other data on the left and right

I tried many links here already, such as:
Regular expression to find two strings anywhere in input
I was unable to get any solution to work as the solutions fail to work on the RegExr site, which I am using to test the regex.
The final result I need is for the regex to work inside Agent Ransack 2014, as I need to search log files on a Windows XP machine.
Comment: Is this what you want https://regex101.com/r/OjdUd2/1 ? Matches are in capture group 1 and 2.
Comment: Use `(?-s)Charge Tip(?=.*-V)`
Answer: Agent Ransack regex engine is Perl compatible. So you can easily search for:
(?-s)Charge Tip(?=.*-V)|(?!\A)\G.*\K-V

Live demo
Comment: Thank you. If possible, please modify it slightly to highlight the -V, as there are many lines with charge Tip without -V, and your solution looks like Charge Tip only.
Comment: You're welcome. You may consider up-voting this answer as well if it helped you.
Comment: Done. It most certainly did.
Answer: Try the following regex, you can see it live here:
^[\w \,\.]*(Charge Tip)[\w \,\.]*(\-V)[\w \,\.]*$

As seen here:
Comment: Unfortunately this doesn't give any matches, on regex101 or regexr.
Comment: @wickyd can you please explain where it doesn't match? See my edit for what I see on Regex101.
Comment: @wickyd Looks like it didn't save correctly. See the `g` on the far right of the regex line, if you add an `m` (multiline) it should work. IE: `/^[\w \,\.]*(Charge Tip)[\w \,\.]*(\-V)[\w \,\.]*$/gm`
Comment: This way you highlight whole line in Agent Ran. not just `Charge Tip` and `-V`
Comment: @revo I'm not sure what you are talking about? The regex will provide two matches - "Charge Tip" and "-V".
Comment: No it doesn't. It matches *whole line* and *captures* `Charge Tip` and `-V` accordingly
Comment: The question asks to produce the `Charge Tip -V` result. This can be done with `\1` and `\2`.
Comment: Where in Agent Ran. should OP use these back-references? Is he trying to do a find / replace? Does the program provide a find / replace functionality at all?


Redirecionamento Autenticado/Não Autenticado

Question: Quanto protegemos uma rota por login e tentamos acessar essa rota sem estar logado o laravel nos manda para a tela de login. Dai se vc autentica ele manda pra quela rota que vc tentou acessar sem estar logado.
Estou com um problema pois não gostaria que o laravel guardasse essa tentativa de acesso e redirecionamento. Gostaria de saber como tratar isso?
Comment: `Estou com um problema pois não gostaria que o Laravel guardasse essa tentativa de acesso e redirecionamento. Gostaria de saber como tratar isso?` o que você quer fazer?
Comment: Gostaria de controlar isso de forma que só fosse redirecionado pra rota que eu coloquei como rota padrão exemplo. Apos Login vai para /home e boas só isso
Answer: O laravel trata isso de diversas formas. A maneira recorrente é a utilização da trait: Illuminate\Foundation\Auth\AuthenticatesUsers se você estiver usando a autenticação padrão fornecida pelo framework: php artisan make:auth. O Laravel gerou o controlador de login:  App\Http\Controllers\Auth\LoginController Lá existe a propriedade protected $redirectTo = '/rotapararedirecionaraposologin'


For what reason, the surface measure represents the surface area?

Question: Let $||\cdot||$ be a norm on $\mathbb{R}^n$ (It's an arbitrary norm, not 2-norm)
Define $S^{n-1}=\{x\in\mathbb{R}^n : ||x||=1\}$
Let $\mu$ be the n-dimensional Lebesgue measure.
Define $\sigma(E)=n\mu((0,1]\cdot E), \forall E\in\mathscr{B}_{S^{n-1}}$
This measure $\sigma_{n-1}:\mathscr{B}_{S^{n-1}}\rightarrow [0,\infty]$ is said to be the surface measure on $S^{n-1}$.
(It has a property that $\mu(\Phi^{-1}(A\times B))=(\int_A r^{n-1}dr)\cdot\sigma_{n-1}(B)$ where $\Phi:\mathbb{R}^n\setminus\{0\}\rightarrow (0,\infty)\times S^{n-1}:x\mapsto (||x||,\frac{x}{||x||})$ is a homeomorphism and $A\in\mathscr{B}_{(0,\infty)}$ and $B\in\mathscr{B}_{S^{n-1}}$)
I'm curious to know, for what reason this $\sigma_{n-1}$ represents the usual sense of the area of a surface, and why is it defined in a such way? why $n$ is multiplied? It may have a geometrical meaning.
For example, when $||\cdot||$ is the max-norm, $\sigma_2(S^2)=24$. This is the "intuitive" area of the surface of a cube of length 2.
Another example, when $||\cdot||$ is the 2-norm, $\sigma_2(S^2)=4\pi$. This is also the usual sense of the area of the surface of a ball.
I wonder why this result is not really surprising,  but rather it has to have these values.
I have another question whether there is a way to expand this idea of measuring the surface of a given measurable set,(e.g. area of the surface of a star-shaped subset, which cannot be represented as $S^{n-1}$) but i think it deserves another post..
Answer: Let $V(r) $ be the measure of the ball of radius $r$. The surface area of this ball can be intuitively understood (or even defined) as $V'(r) = \lim_{h\to 0 }(V(r+h)-V(r))/h$. Think of covering  $V(r)$ by a  coat of paint with thickness $h$. The volume of paint used, divided by $h$, gives the surface area.
Since $V(r)=r^n V(1)$, it follows that $V'(r)=nr^{n-1}V(1)$ and in particular $V'(r)=nV(1)$. This explains the factor of $n$.

whether there is a way to expand this idea of measuring the surface of a given measurable set

Yes, Minkowski content.
Comment: This is the best answer i got recently. Thank you very much ! by the way, i wonder why it is divided by 2 in the definition the Minkowski content..
Comment: @Number8 There are two definitions in the wiki. One is for the case when $A$ is the "solid" set whose surface (boundary) we want to measure. Then you argue as above: paint $A$ with thickness $h$, etc. The other is for the case when $A$ is itself the surface we want to measure, e.g., a sphere. When we paint all of it, both the inside and outside get covered by layer $h$, so the total thickness is $2h$.


Subjects with different order of magnitude values in panel data

Question: I have quarterly sales data for a variety of stores and would like to estimate the effect of a regulation on sales.  A panel type model would appear to be appropriate in this case, with the regulation as a dummy variable.
However, the quantity of sales varies by two-orders of magnitude between stores.  How should I account for this?
Apologies for any problems with the question, I am something of a beginner.
Comment: using logs sometimes helps. Log-log models are popular in econometrics, because they are easy to interpret, that is another bonus of using them.
Answer: Actually the @mpiktas comment is the answer to your particular question. Sales models are usually multiplicative by the nature (some intuition could be found in Market response  models book). There is also a number of reasons for logs discussed for ARIMA models in my earlier post. In your case it is the scale effect that troubles you, therefore log transformation works well here. Another useful trick is to divide by some size variable (plot of the store, number of workers, etc.), so moving to fractions could help also.
In addition to your question. What you have to pay attention to are other important explanatory variables: location variables or density of the population, size, variety of products (categories) and there average prices, number of workers, distances to the rival shops etc. that will matter (omitting them will cause you some estimates with poor properties: probably biased and inconsistent). Regulation can't be put as the solo explanatory variable in this context.
Comment: Given that a small minority of my sales data is in the range `0.0 to 1.0`, should I add `1` to all my sales figures before using a log transformation?
Comment: @fmark, if you don't have exactly zeroes than there is no problem to take log transformation (the only case for zero is if the store sells nothing during a quarter!, but it is a strange case). You have this small range probably due to units you measure your sales. In log-log regression this will be absorbed into intercept term. However, if you do have some true zeroes then we will think about other useful tricks (by the way no need for logs in dummies).


Get the Index of an Element in a List in Terraform

Question: How can the index of the elements in a list be calculated in Terraform?
Example:
If we have this variable:
variable "domains" {

  type = "list"

  default = [
    "tftesting.io",
    "tftesting.co",
  ]
}

How can we calculate that "tftesting.io" has index "0" and "tftesting.co" has index "1"?
Answer: And the winner is: index(list, element)
variable "domains" {
  type = "list"

  default = [
    "tftesting.io",
    "tftesting.co",
  ]
}

output "co_index" {
  value = "${index(var.domains, "tftesting.co")}"
}

output "io_index" {
  value = "${index(var.domains, "tftesting.io")}"
}

The end result:
$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

co_index = 1
io_index = 0


iframe not loading the html text

Question: I have a string variable. This string variable contains HTML content.
I want to use this string variable to load data in to iFRAME.
iFRAME "SRCDOC" property doesn't support IE.
Please any one help on this , how to load html content to iframe.
Thanks,
Answer: You can write directly into IFRAME document.
Let's say you have an IFRAME:

You can write code:
var ifr = document.getElementById("bla")
var doc = ifr.contentDocument || ifr.contentWindow.document;

var str = "
Hello World!
"

doc.open();
doc.write(str);
doc.close();

Ordinary it is not recommended to use document.write, but in this case it does the job.
Demo: http://jsfiddle.net/U6q7T/2/
Comment: thanks for quick reply. How to send string from code behind to js
Comment: @user3680114 that's a different question. You did not mention any ASP.NET in your original question. But you can have a hidden field - assign its value in code-behind and read it in JS code.

iframe not loading the html text


Accessing outer classes fields from inner class

Question: I use custom renderer for ContextMenuStrip. It works fine, but I can't figure out how can I use fields from outer class Form1 inside of class MyColors? I should get customColor from Form1 instead of "hardcoded" Color.Green. How can I achieve this?
public partial class Form1
{
    private Color customColor = Color.Red;

    private class MyRenderer : ToolStripProfessionalRenderer
    {
        public MyRenderer() : base(new MyColors()) { }
    }

    private class MyColors : ProfessionalColorTable
    {
        public override Color MenuItemSelected
        {
            get { return Color.Green; }
        }
    }
}

Thanks in advance.
Comment: I think you're looking at it the wrong way. A `ProfessionalColorTable` is the replaceable part. A `ToolStripProfessionalRenderer` uses the values provided by a `ProfessionalColorTable`. This table(s) should contain the definitions. Not the Form class.
Comment: https://stackoverflow.com/questions/185124/whats-the-best-way-of-accessing-field-in-the-enclosing-class-from-the-nested-cl
Answer: In general, I agree with other posters that a class should stand alone. I think it gets a little fuzzier when dealing with a private class, though.
In the few cases I've needed something like this, I pass the parent object into the private class' constructor:
public partial class Form1
{
    public Color customColor = Color.Red;

    private class MyRenderer : ToolStripProfessionalRenderer
    {
        public MyRenderer(Form1 form) : base(new MyColors(form)) { }
    }

    private class MyColors : ProfessionalColorTable
    {
        Form1 _form;

        public MyColors(Form1 form)
        {
            _form = form;
        }

        public override Color MenuItemSelected
        {
            get { return _form.customColor; }
        }
    }
}

Note that I had to make Form1.customColor public for this to work. That's a bit smelly, especially since it can now be assigned from the outside. In real work, I'd make it a property with a public getter and a private setter, so outside classes couldn't modify it.
Comment: Yes, this works great! I left `customColor` private, and added just a public getter `CustomColor` to retrieve `customColor`.
Answer: You would be breaking encapsulation that way. If you need a contained class to know something from its containing class, you should provide such information in the contained class' constructor.
Otherwise, you would be letting another class change your class' properties, losing control over what happens inside Form1.
Considering your code, it seems ProfessionalColorTable has a parameterless constructor, so you should include a validation in case MyColors' private variable pointing to the containing class' color is not set (as a result of using the wrong constructor).
Comment: This is a good way to look at it. A class should be thought of as an independent unit (typically, IMO). If it directly depends on outside "stuff", that's just not right. Kind of if you had a `Car` inside of a `Garage` class, and the `Garage` affected how the `Car` functioned. It's just...weird.


Q: How to check the extension of an URL website?

Question: I would like to know if there is a way to check the extension of an URL website ? For example, do something when the website is like http://example.es/ and do another thing when the website is like http://example.fr/
I have check that there is something like
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

which returns the current URL of the web page.
Thanks for help.
Comment: I think this is what you want https://www.php.net/manual/en/function.parse-url.php and this https://www.php.net/reserved.variables.server
Answer: Use parse_url() function to get host part of the url then explode by . and get last element of an array
Example below:
    $url =  'http://' . $_SERVER['SERVER_NAME'];
    echo end(explode(".", parse_url($url, PHP_URL_HOST)));
    // echos "com"

Comment: Thanks for your answer, that exactly what i need. I was trying with trim function to take only the end of the URL  but explode worked fine.
Comment: NOTICE Only variables should be passed by reference
Answer: From your example I assume that you are using PHP, then you can use parse_url to get the components.
https://www.php.net/parse-url
For example you can get the host example.fr and example.com, then do explode on host string to get the tld, .fr or .com, which should help you to do further if-else.


Homology groups and embeddings

Question: I'm working on the following problem:
Let $A$ be the image of an embedding $g: S^{1} \times D^{2} \rightarrow S^{3}$ . If $Y= S^{3} - $Int$(A)$
be the complement of its interior. Compute the homology groups $H_{*}(Y; \mathbb{Z}$) and $H_{*}(Y, \partial Y ; \mathbb{Z} $).
Since I'm a beginner in homology theory I have only learned to compute homology groups for very simple examples. Can someone help me go through this problem?
Comment: You say $Y=S^3-$Int$(V)$, but don't tell us what $V$ is, typo?
Comment: @albert Yes, that was a mistake. I meant to write $A$.
Answer: For the first one, use the Mayer-Vietoris sequence for homology, the relevant piece of which is
$$H_2(S^3) \longrightarrow H_1(T^2) \longrightarrow H_1(A) \oplus H_1(Y) \longrightarrow H_1(S^3).$$ The first and the last groups are trivial, and $H_1(T^2) \simeq \mathbb{Z}^2,$ so $H_1(Y) \simeq \mathbb{Z}.$
Your second question can be done similarly, but I leave it to you.
Comment: Maybe it is me who doesn't see it, but are you suggesting that $A\cap Y=Y$ is the $2$-torus?
Comment: @DavideTrono That is precisely what I am suggesting.
Comment: I see, but I don't understand why, at least I can't see it from a CW-complexes perspective.
Comment: @DavideTrono Well, $S^1 \times D$ is a solid torus. Its boundary is a torus.

Homology groups and embeddings


Is there setClientBoundingRect()?

Question: getClientBoundingRect is a function that gets a position of element relative to viewport.
Is there a function that is able set value instead?
Comment: I don't think so. You can use position: absolute or fixed and set height/width to simulate the above.
Answer: That's what CSS is for. You use CSS to position your elements in the viewport, and since getting the position of an element relative to a viewport and/or other elements is more problematic, you have getClientBoundingRect (and a lot of other element-specific DOM API methods).

Is there setClientBoundingRect()?


Will an SVM break down for sufficiently small values of C?

Question: Consider the soft-margin SVM formulation.
$$
\begin{align}
\mbox{min}_{\mathbf{w},b,\boldsymbol{\xi}} & & \frac{1}{2}\|\mathbf{w}\|_2^2 + C\sum_i \xi_i
\\ \mbox{s.t.} & & y_i(\mathbf{w}^Tx_i+b) \geq1-\xi_i
\\ & & \xi_i\geq0
\end{align}
$$
If $C$ is sufficiently small, the $\xi_i$'s can grow large, and therefore the constraints can be honored by a plethora of grossly misclassified points. Therefore, the minimization of $\|\mathbf{w}\|^2_2$ no longer seems that helpful in the pursuit of a separating hyperplane.
I have done a quick test with scikit-learn's SVC and a toy (and admittedly a bit pathological) dataset.

So in this case I think we can say it breaks down badly (unless it is due to the implementation).
The SVM objective (minimize $\|\mathbf{w}\|$) arises from the consideration of the constraints in the original, natural objective (maximize the margin to the closest point). Doesn't it cease to be a natural formulation once we introduce the slack variables? Is there something intrinsic to the minimization of $\|\mathbf{w}\|$ that provides better separating hyperplanes in less extreme cases, or will this pathology happen in general?
EDIT: The corresponding slack variables for each of the above cases are:
<code>y  point     xi
1  [ 0.  1.] 0.000276941610509
1  [ 1.  1.] 9.22304236668e-05
1  [ 2.  1.] 9.24807631741e-05
1  [ 3.  1.] 0.000277191950016
-1 [ 0. -1.] 0.000461843532213
-1 [ 1. -1.] 0.000277132345372
-1 [ 2. -1.] 9.24211585298e-05
-1 [ 3. -1.] 9.22900283107e-05
1  [ 1.5 -0.9] 1.90017553166

1  [ 0.  1.] 2.38418573772e-09
1  [ 1.  1.] 3.57627882863e-09
1  [ 2.  1.] 9.53674317294e-09
1  [ 3.  1.] 1.54972079613e-08
-1 [ 0. -1.] 0.779999997616
-1 [ 1. -1.] 0.780000003576
-1 [ 2. -1.] 0.780000009537
-1 [ 3. -1.] 0.780000015497
1  [ 1.5 -0.9] 1.15899999344

1  [ 0.  1.] 2.38418618181e-10
1  [ 1.  1.] 1.5894552341e-10
1  [ 2.  1.] 5.56309887045e-10
1  [ 3.  1.] 9.53674250681e-10
-1 [ 0. -1.] 1.87799999976
-1 [ 1. -1.] 1.87800000016
-1 [ 2. -1.] 1.87800000056
-1 [ 3. -1.] 1.87800000095
1  [ 1.5 -0.9] 0.115899999642
</code>
Comment: For reference, what are the $\xi_i$ produced for each plot?
Comment: @AlexR. Good point. I added them.
Answer: As $C \rightarrow 0$, $w \rightarrow 0$ (independent of the data, of course the precise results for specific values of $C$ will be data dependent). Note that your implementation though will generally be $\epsilon$-accurate for some expression (this may be the objective, or it may be on something related, like a bound on the duality-gap), and it might be sensitive to how it started the optimization.
Comment: This answer certainly introduces interesting elements. However, can we expect data-independent $\epsilon$-accuracy considering the $\xi$'s can grow arbitrarily?
Comment: This depends on the optimization approach. For example, some algorithms explicitly take $\epsilon$ as a parameter and thus guarantee this bound - i.e, its guaranteed that the objective is within $\epsilon$ of optimal - however the precise value that you exceed the objective by is definitely data dependent; if the algorithm is stochastic it would even be dependent on the randomization there


External drive switching from /dev/sda to /dev/sdb

Question: We have a small box with external drives attached for backing up configuration sets.
I found out the problem is simply that on reboot or whatever unknown event the drive switches from /dev/sda1 to /dev/sdb1.
How can I circumvent this or configure fstab to try both locations?
/dev/sda1       /stor           ext4    defaults,noatime,noexec  0       0

You can see I did quite some debugging:
root@nsa:~# mount /stor
mount: special device /dev/sda1 does not exist

As you can see below I did quite some debugging while writing the question.
syslog:
Jul 16 09:48:48 nsa kernel: [167416.395274] EXT4-fs warning (device sda1):
         __ext4_read_dirblock:908: error reading directory block (ino 2, block 0)

#df -h before umount:
/dev/sda1       1.4T  185G  1.1T  15% /stor

#dmesg:
[   170.431715] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null)
[119366.019470] usb 1-1.2: USB disconnect, device number 4
[119366.297601] usb 1-1.2: new high-speed USB device number 6 using dwc_otg
[119366.398979] usb 1-1.2: New USB device found, idVendor=1058, idProduct=1021
[119366.399013] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[119366.399029] usb 1-1.2: Product: Ext HDD 1021
[119366.399047] usb 1-1.2: Manufacturer: Western Digital
[119366.399063] usb 1-1.2: SerialNumber: 5743415A4132343133343334
[119366.400719] usb-storage 1-1.2:1.0: USB Mass Storage device detected
[119366.403826] scsi1 : usb-storage 1-1.2:1.0
[119367.398875] scsi 1:0:0:0: Direct-Access     WD       Ext HDD 1021     2002 PQ: 0 ANSI: 4
[119367.401608] sd 1:0:0:0: Attached scsi generic sg0 type 0
[119367.402505] sd 1:0:0:0: [sdb] 2930272256 512-byte logical blocks: (1.50 TB/1.36 TiB)
[119367.403524] sd 1:0:0:0: [sdb] Test WP failed, assume Write Enabled
[119367.404593] sd 1:0:0:0: [sdb] Asking for cache data failed
[119367.404623] sd 1:0:0:0: [sdb] Assuming drive cache: write through
[119367.407157] sd 1:0:0:0: [sdb] Test WP failed, assume Write Enabled
[119367.410142] sd 1:0:0:0: [sdb] Asking for cache data failed
[119367.410185] sd 1:0:0:0: [sdb] Assuming drive cache: write through
[119374.143786]  sdb: sdb1
[119374.146995] sd 1:0:0:0: [sdb] Test WP failed, assume Write Enabled
[119374.149166] sd 1:0:0:0: [sdb] Asking for cache data failed
[119374.149203] sd 1:0:0:0: [sdb] Assuming drive cache: write through
[119374.149231] sd 1:0:0:0: [sdb] Attached SCSI disk
[153689.349544] EXT4-fs error (device sda1): ext4_find_entry:1309: inode #2: comm rsync: reading directory lblock 0

#lsusb:
Bus 001 Device 006: ID 1058:1021 Western Digital Technologies, Inc. Elements 2TB

Comment: You'd be better off using UUIDs rather than the device node. Can you run `grub-probe --target=drive --device /dev/sda` to see what the UUID is and use that for fstab instead ?
Comment: @Lawrence Nice! Feel free to post this as an answer (using UUID) it works well! I don't have grub bootloader and used `ls /dev/disk/by-uuid` instead.
Comment: What is assigned to sda then?
Comment: @ThorbjørnRavnAndersen nothing
Comment: Sure?  Looked at your log again, the first line refers to sda1.
Comment: @ThorbjørnRavnAndersen That's because the device has always been mounted via sda1. I think because of the drive going to sleep mode or something, it switched to sdb1 and blocked sda1 or something.
Answer: As has been mentioned in the comments, grab the disk's UUID and stick this in your fstab;
UUID=66a7ba58-b1e2-4d91-9b5e-085064a954ab  /stor   ext4    defaults    0 0
(replace the UUID value with that of your own). As simple ls -la /dev/disk/by-uuid (which is just a collection of symlinks named the UUID of the disk which point to the real device's identifer (sda, sdb, etc). In my case;
lrwxrwxrwx 1 root root   9 Jul  7 08:45 66a7ba58-b1e2-4d91-9b5e-085064a954ab -> ../../sdf
The disk with that UUID is /dev/sdf, but I don't have to know that. The device's location could, will, and does change. It's UUID is baked into the filesystem's table of contents, which will never change unless you format it clean.
Comment: The direct way to retrieve the UUID is with the `blkid` command
Comment: Ah cool, didn't know that one!


JPA spring boot dados customizados não funciona

Question:    public interface ProdutoRepositorio extends JpaRepository<Produto, Long> {

     @Query(value="SELECT  new br.com.thiago.objcustom.SomaDosProdutos(e.nome, sum(preco)) FROM sitethiagodois.produto p inner join sitethiagodois.empresa e on e.id = p.empresa_id where e.id = 1;")
    SomaDosProdutos somaById();
}

com nativequery = true tbm não funcionou
O objeto é:
package br.com.thiago.objcustom;

public class SomaDosProdutos{

    private String nome;
    private Long soma;

    public SomaDosProdutos(String nome, Long soma){
        this.nome = nome;
        this.soma = soma;
    }

    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public Long getSoma() {
        return soma;
    }
    public void setSoma(Long soma) {
        this.soma = soma;
    }

}

O erro aparece ao inicializar
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'produtoControler': Unsatisfied dependency expressed through field 'prodService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'produtoService' defined in file [C:\Users\Thiago_2\Documents\workspaceSpring\apiweb\target\classes\br\com\thiago\service\ProdutoService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'produtoRepositorio': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract br.com.thiago.objcustom.SomaDosProdutos br.com.thiago.repository.ProdutoRepositorio.somaById()!
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at br.com.thiago.ApiwebApplication.main(ApiwebApplication.java:10) [classes/:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'produtoService' defined in file [C:\Users\Thiago_2\Documents\workspaceSpring\apiweb\target\classes\br\com\thiago\service\ProdutoService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'produtoRepositorio': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract br.com.thiago.objcustom.SomaDosProdutos br.com.thiago.repository.ProdutoRepositorio.somaById()!
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    ... 19 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'produtoRepositorio': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract br.com.thiago.objcustom.SomaDosProdutos br.com.thiago.repository.ProdutoRepositorio.somaById()!
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    ... 32 common frames omitted
Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract br.com.thiago.objcustom.SomaDosProdutos br.com.thiago.repository.ProdutoRepositorio.somaById()!
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:62) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:144) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:212) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    ... 43 common frames omitted
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: unexpected char: ';' [SELECT  new br.com.thiago.objcustom.SomaDosProdutos(e.nome, sum(preco)) FROM sitethiagodois.produto p inner join sitethiagodois.empresa e on e.id = p.empresa_id where e.id = 1;]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1679) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:294) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_92]
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at com.sun.proxy.$Proxy76.createQuery(Unknown Source) ~[na:na]
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    ... 56 common frames omitted
Caused by: org.hibernate.QueryException: unexpected char: ';' [SELECT  new br.com.thiago.objcustom.SomaDosProdutos(e.nome, sum(preco)) FROM sitethiagodois.produto p inner join sitethiagodois.empresa e on e.id = p.empresa_id where e.id = 1;]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
    ... 63 common frames omitted

este é o erro aparece na hora de inicializar.
Eu gostei do JPA para muitas coisas mas ele não parece dar muita liberdade de criar query e customizar objetos
Comment: Não deveria ser sum(p.preco)?
Comment: Sim mas ainda há um problema quando inicializa
Answer: Descobri, o que eu não sabia é que eu tinha que usar JPQL.
Então o a query fica:
@Query(value="SELECT NEW br.com.thiago.objcustom.SomaDosProdutos(e.nome, Sum(p.preco)) FROM Produto p INNER JOIN p.empresa e  WHERE e.id = 1 GROUP BY e.nome")
    SomaDosProdutos testeById();

Answer: Ao utilizar uma função de agregação como sum é necessário fazer o group by.

Pode ainda remover o argumento value que já o default atribuído quando há apenas um argumento;
Não tenho certeza se gera problema colocar o ; no final do select, mas nunca usei assim, então removi;

Código ajustado:
public interface ProdutoRepositorio extends JpaRepository<Produto, Long> {

    @Query("SELECT  new br.com.thiago.objcustom.SomaDosProdutos(e.nome, sum(preco)) FROM sitethiagodois.produto p inner join sitethiagodois.empresa e on e.id = p.empresa_id group by e.nome")
    SomaDosProdutos somaById();
}

Comment: eu não posso colocar 'nativeQuery = true'?
Comment: Mas você não está usando native query. Palavras como new não existem no native query.


Python Subprocess Module and Awk

Question: I am trying to implement the following code using subprocess module in python.
for i in range(9,17):
    process=s.Popen("awk -v var=$(i) '$6==var' 1GT8.pdb", shell=True, stdout=s.PIPE)

    matched=process.communicate()[0].split()
    print matched

But i that is being matched is a string entity and not the integer value that I want. Please help me out.
Comment: As usual, the code would be much more elegant *and* efficient if you implemented this logic in pure Python.
Answer: Python does not expand variables inside strings, so the command will contain $(i) as is. You'll have to insert the value explicitly, like this:
command = "awk '$6==\"{}\"' 1GT8.pdb".format(i)

Comment: You need to escape the double quotes that are around the brackets, I think.

Python Subprocess Module and Awk


Maximum Likelihood Function with X having the same distribution

Question: Given all $X_i$ have the same distribution (say, Normal distribution), when we are calculating the maximum likelihood function, we obtain:
$= . . .= \frac{1}{{2\pi}^{\frac{n}{2}}}\cdot e^{\frac{-1}{2}} \sum_{i=1}^n X_i^2 \cdot g (t(x), \theta)$
Can we simply multiply the $X_i^2$ with $n$, as they have the same distribution ?
(i.e., into this:  $\frac{1}{{2\pi}^{\frac{n}{2}}}\cdot e^{\frac{-1}{2}} \cdot n X^2$)
why or why not?
Comment: sorry, it's a typo
Comment: X represents a single normal distribution, I guess they are the same would be the dependence relationship ?
Comment: Ummm.. I'm not sure. I actually don't really understand the marginal relationship part. Is there any reference that I can read?
Comment: No I guess :( since we have taken the log and derivation with respect to \mu and \sigma
Comment: Welcome to the site. Are the $X_i$s your observations? What does $X^2$ mean?
Answer: No, you can't do that. You haven't defined what is $X$.
We compute likelihood based on the collected data.
Also, $X_1 + X_2$ is also different from $2X_1$. In the first case, you are summing up two observations, in the second case, you are discarding a reading and just multiply the first one by $2$.  These two statistics behave differently. For example if $X_1$ and $X_2$ are i.i.d, $Var(X_1) + Var(X_2)= 2Var(X_1)$ but $Var(2X_1)=4Var(X_1)$.


How to interpret mean of this estimated AR(1) process

Question: I estimated an AR(1) process, my data looks like this:

Making usual unit root test, they suggest that an estimated AR(1) from this data is stationary. Estimating the AR(1) over this data, these are the results:
z test of coefficients:

           Estimate Std. Error z value  Pr(>|z|)
ar1        0.728652   0.085601  8.5121 < 2.2e-16 ***
intercept 20.176618   0.809543 24.9235 < 2.2e-16 ***

The purpose of this estimation is to get the stationary mean of the process, i.e.:
$x_t=c+\phi x_{t-1}+\varepsilon_t$
$E\{x_t\}=\frac{c}{1-\phi}$
With the estimated values this turns out to be, given the significance of estimators:
$E\{x_t\}=\frac{20.176618}{1-0.728652}\approx74.3569$
Which clearly is outside the range the values data take, and is much larger than expected. What is wrong? Maybe I misinterpreted the unit root tests or something similar?
Comment: Be careful. Some estimation routines do not give you estimates $\hat c$ and $\hat \phi$ for  $y_t = c + \phi y_{t-1} + e_t$ but instead returns the estimates of $ \mu =  c/(1-\phi)$. This could very well be the case since your series seems to be reverting to $20$ the intercept estimate.
Comment: @JesperforPresident Thanks for your answer, I'm using R's ```arima()``` from ```stats``` package, and also checked with ```Arima()``` from ```forecast```, do you know if those have the mentioned by you representation? Also if that's the case, then $\hat\mu=20.176618$?
Answer: I  made the same mistake several years ago. When in doubt read the documentation or do a simulation. Here is a simulation
T <- 10000
y <- rep(0,T)
c <- 5.6
phi <- 0.72
y[1] <- rnorm(1) + c/(1-phi)
for (t in 2:T)
    {
        y[t] <- phi*y[t-1] + c + rnorm(1)
    }

arima(y,order=c(1,0,0))

for which arima call return 20 as intercept
Coefficients:
ar1  intercept
0.7213    20.0361
s.e.  0.0069     0.0358
so the intercept reported is
$$\mu = \frac{c}{1-\phi}$$