Leverage browser caching external files

Question: I used google pagespeed Insights to test the performance of my nodejs website. For some of external files it is saying to leverage browser caching but I don't know how to do this ?
Leverage browser caching
Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.
Leverage browser caching for the following cacheable resources:
http://maps.googleapis.com/…kwPPoBErK_--SlHZI28k6jjYLyU&sensor=false (30 minutes)
http://www.google-analytics.com/analytics.js (2 hours)
Anyone please help me on this.
Comment: Its ironic that google pagespeed complains about resources downloaded from google server...
Comment: Google can't cache their APIs for too long, they would never be able to push new versions or fixes... don't take pagespeed like a bible. Besides, the load of these files occurs on their servers, not yours :)
Comment: Too long??? 30 minutes, 2 hours... That really doesnt help.. Load or not but performance degrades if user is gonna be there for more than 30 mins.. And how can you say that if they cant increase the time period to something like 6 hours or a day.. That will not allow them to push new updates.. Do you really need update hourly?
Comment: I ended up serving all css and js files from my server rather than CDN/Google. Got 100/100 on page speed with that approach :)
Answer: One solution is to reverse proxy the Google resources. Then you can add Cache-Control and other caching headers. If you're using Apache you can accomplish it as follows in your httpd.conf file:
<code>ProxyRemote http://www.google-analytics.com http://yourinternalproxy:yourport

<Location /analytics.js>
  ProxyPass http://www.google-analytics.com/analytics.js
  ProxyPassReverse http://www.google-analytics.com/analytics.js
  Header set Cache-Control "max-age=86400"
</Location>
</code>
The drawbacks of this are that:

You'll funnel a lot of additional traffic through your servers.
Obviously updates made by Google will take longer to appear for the user's of your site.
Comment: there is also the drawback that if a user already has analytics cached after visiting a different website (which is almost certain, given how popular it is), you don't get to take advantage of the fact that it's already in the cache.
Answer: If you don't have access to httpd.conf file as rudolfv's answer there are several options here:

the easiest one is you could copy its content each day to make sure your up to date
we can employ the powers of cron, there is nice sample script using php posted here
use a php script to generate the google analytics script on every request on the fly:
<code>    $context = stream_context_create(['http' => ['Content-Type' => 'text/javascript', 'enable_cache' => true, 'enable_optimistic_cache' => true, 'read_cache_expiry_seconds' => 86400,]]);
    echo file_get_contents("http://www.google-analytics.com/analytics.js", false, $context);
</code>
use the power of .htaccess if your hosting provider allowing mod_headers & mod_proxy
<code>    RewriteEngine On
    Header set Cache-Control "max-age=86400"
    RewriteRule ^js/analytics.js http://www.google-analytics.com/analytics.js [P]
</code>

Leverage browser caching external files


How do you compare the performance of wicket keepers?

Question: Given two wicket keepers, how would you compare which one was better?
With bowlers you can use statistics like number of wickets taken and with batsmen, you can use number of runs scored but with wicket keepers, what could you use to evaluate the performance of a wicket keeper?
You couldn't use anything like number of catches or stumping because these things are generally dependent on other players.
Answer: Most achievements in cricket depend to some extent on other players. Runs scored depends on the bowler faced, and wickets taken depends on the efforts of the fielders.
Therefore it's reasonable to use catches taken and stumpings made to evaluate wicketkeepers. In addition, byes conceded is a useful measure: lower is better.
Comment: @Orge - Also, in bowling friendly pitches bowlers get wickets easily while in batting pitches batsmen get runs easily. Do anyone take this into account while comparing batsmen or bowlers? Even different pitches help different bowlers like slow pitches (like in sub-continents) help spinners while fast pitches (like in Aus-NZ) help fast bowlers. So you can't compare bowlers properly.
Comment: I disagree. Using catches, stumpings or even byes conceded would be a terrible way to evaluate a wicketkeeper. Due to the nature of team sports, most stats are influenced by other players, but the ones you've mentioned are almost entirely influenced by other players and the wicket keeper is just finishing the job. For example, 99% of catches made by one wicket keeper would be made by another one - a good wicket keeper isn't one whose made a high number of catches, it's one who could achieve a positive result in the 1% of times where the other 99% couldn't.
Comment: @Orge - You are guessing/interpreting it wrongly. There are several occasions where wicket keepers drops very easy chances of catching and stumping or catch or stump very difficult one. But the one who misses the chance very less is a good wicket keeper. Why are taking wicket keepers' statistics? The same happen with bowlers and batsmen as well. Many bowlers get wickets due to either's batsmen's wrong shot or fielder's better efforts or sometimes luck. Same for batsmen some batsmen get runs by edges. Have you calculated which batsman got most number of runs by edges?
Answer: The more telling statistics for a wicket keeper in the field are the number of dropped catches and missed stumpings per match. In the professional game, you would expect the wicketkeeper to catch all but the most difficult chances, and to cleanly gather the ball and attempt a stumping whenever possible.
An example of this is when the England wicketkeeper Matt Prior was dropped for the series against New Zealand in 2008. The explanation by the Chairman of selectors was

the selectors feel that he needs to further improve his wicket-keeping in order to realise his full potential in international cricket.

but the more telling explanation was from the BBC cricket expert and former fast bowler Jonathan Agnew:

It's harsh but fair on Matt... he simply dropped too many catches.

I couldn't find any statistics on drops by wicketkeepers. Maybe the coaches keep statistics, but in doubt you can always ask the bowlers, who don't forget drops too quickly.
Comment: yeah i agree dropped catches would be a better statistic than catches taken. I reckon there are no official stats on it cause dropped catches are hard to define. Is it anytime where you drop a catch after getting a hand to it? Sometimes, the catch is so hard that its the better wicket keeper who was able to get a hand to it where other keepers would've missed it entirely. I think coaches / selectors would probably informally identify a couple of incidences during the match and evaluate how they performed in them.
Answer: From abstract of Performance measures for Wicket Keepers in Cricket by Hermanus H. LEMMER

A wicket keeper should not only perform well behind the wickets in terms of taking catches, executing stumpings and limiting the number of byes, but should also perform well as a batsman. The purpose of this study is to combine the dismissal rate (based on the number of catches and stumpings) and a measure of batting performance into measuring wicket-keeping performance.

But the one that I find interesting to measure the performance of wicket keeper is in thesis by Danielle Catherine MacDonald named Performance analysis of fielding and wicket-keeping.....

A data of wicket-keeper for following activity like: crouch, catch, missed take/reception, jump, dive, take, reception from field, sprint, underarm throw, run up to stumps, lateral steps and lateral shuffle could help in determining performance of wicket-keeper.

With the help of data for above mentioned activities, we can compare performance of wicket keepers.


Ordenar por custom fields e por data de publicação

Question: Meu problema é o seguinte, tenho um post_type no nome de "colecoes" e dentro dele várias postagens com campos personalizados "situacao-da-peca" e "colecao-da-peca". O resultado desses dois campos são números, para ficar mais fácil de ordenar.
Consegui ajuda pra ordernar esses campos personalizados através de um javascript, como no código abaixo, e funciona perfeitamente. Porém, preciso também que dentro dessa ordenação os posts também fiquem ordenados por data de publicação. No Firefox essa ordem está certinha, porém no Chrome e IE, a ordem por data (padrão do wordpress, por sinal), não funciona. Sei que é o javascript que pode estar desordenando elas, mas não sei como corrigir...
Segue o link que isso está acontecendo: https://malumacedo.com.br/marca/basico-brasil/

<?php $args = array( 'post_type' => 'colecoes');
$wp_query = new WP_Query($args);
if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<li class="foto" data-char="<?php if( get_field('situacao-da-peca') == '1' ):?>c<?php the_field('colecao-da-peca');?><?php endif;?><?php if( get_field('situacao-da-peca') == '2' ):?>b<?php the_field('colecao-da-peca');?><?php endif;?><?php if( get_field('situacao-da-peca') == '3' ):?>a<?php the_field('colecao-da-peca');?><?php endif;?>">

<?php endwhile; endif;?>

Answer: Você pode fazer essa ordenação no Wordpress da seguinte maneira:
$args  = array( 'post_type'=>'colecoes', 'orderby'=>'date', 'order'=>'DESC' ); // order pode ser ASC ou DESC
$query = new WP_Query( $args );

Mais informações sobre quais campos você pode utilizar na ordenação no link abaixo.
Documentação
Você falou que conseguiu ordenar pelos custom fields com javascript e que funciona perfeitamente, mas se você quiser fazer essa ordenação pelo Wordpress é só dar uma olhada nessa documentação.
Comment: Já havia tentando isso, esse comando não funciona também, no Google Chrome nem no IE. Elas deveriam ficar ordenadas como está no Firefox (que está certinho pela ordem de postagem), esse é o link https://malumacedo.com.br/todas-as-colecoes/ . É como se eles ordenassem aleatoriamente e só seguissem o comando do javascript mesmo. Estou estudando essa ordenação por custom fields, pra ver se acho um caminho. Obrigada


Determining which Ciphers clients are using from RoundCube mail

Question: I have RoundCube installed on RHEL 7 server, and after Qualys vuln check, there are some vulnerabilities with ciphers. After patching, DES & 3DES cannot be used anymore. TLS handshakes cannot be made with devices without AES-128 or newer algorithms (iOS 7+, Android newer than version 3). But, some clients have problems connecting, even though they are on supported systems with AES-128.
I want to see which Ciphers clients are using. I can't find any logs that reveal this information directly. Where else can I look?
Answer: The client sends a list of his supported cipher suites alongside the client_hello message of the TLS handshake.
Check the debug logging functionality of your web server, it should be able to store that information.
For the concrete configuration options for nginx, check out this answer.
Comment: Where is that configured on nginx ?
Comment: I updated my answer
Comment: Also note that wireshark will show the ciphersuites offered by the client,


Linear independent sets

Question: Let $S_1\in\mathbb{R}^{n}$ and $S_2\in\mathbb{R}^{n}$ be two subspaces of $\mathbb{R}^{n}$
Suppose $x_1\in S_1$, $x_1\notin S_1\cap S_2$. $x_2\in S_2$, $x_2\notin S_1\cap S_2$. Show that $x_1$ and $x_2$ are linearly independent.
proof: I am trying to come up with a contradiction by assuming they are dependent and have the fact that either $x_1\in S_1\cap S_2$ or $x_2\in S_1\cap S_2$ but I am not sure how to show this, any suggestions is greatly appreciated
Answer: Let $x_1$ and $x_2$ are dependent.
So $\alpha x_1+\beta x_2=0$. If $\alpha\ne0$, $x_1=-\frac{\beta}{\alpha}x_2$ and $x_1\in S_2$.
If $\alpha=0$ then $x_2=0$ which contradicts that $x_2\notin S_1\cap S_2$
Comment: You need $\alpha\neq 0$ which might not be the case, but you can assume it wlog.
Comment: If α is not $0$. You didn't say why.
Answer: Let it be that $\lambda.x_1+\mu.x_2=0$ and $(\lambda,\mu)\neq(0,0)$ (or equivalently $\lambda\neq0\vee\mu\neq0$).
If e.g. $\lambda\neq0$ then $x_1=\alpha.x_2$ where $\alpha=\frac{\mu}{\lambda}$.
Then $x_1\in S_2$ so that $x_1\in S_1\cap S_2$ and a contradiction is found.
Likewise you can find a contradiction in the other case.

Linear independent sets


In general, is doing inference more difficult than making prediction?

Question: My question comes from the following fact. I have been reading posts, blogs, lectures as well as books on machine learning. My impression is that machine learning practitioners seem to be indifferent to many things that statisticians/econometrics care about. In particular, machine learning practitioners emphasize prediction accuracy over inference.
One such example occurred when I was taking Andrew Ng's Machine Learning on Coursera. When he discusses Simple Linear Model, he mentioned nothing about the BLUE property of the estimators, or how heteroskedasticity would "invalidate" confidence interval. Instead, he focuses on gradient descent implementation and the concept of cross validation/ROC curve. These topics were not covered in my econometrics/statistics classes.
Another example occurred when I participated in Kaggle competitions. I was reading other people's code and thoughts. A large part of the participants just throw everything into SVM/random forest/XGBoost.
Yet another example is about stepwise model selection. This technique is widely used, at least online and on Kaggle. Many classical machine learning textbooks also cover it, such as Introduction to Statistical Learning. However, according to this answer (which is quite convincing), stepwise model selection faces lots of problem especially when it comes down to "discovering the true model". It seems to be that there are only two possibilities: either machine learning practitioners do not know the problem with stepwise, or they do but they do not care.
So here are my questions:

Is it true that (in general) machine learning practitioners focus on prediction and thus do not care about a lot of things which statisticians/economists care about?
If it is true, then what is the reason behind it? Is it because inference is more difficult in some sense?
There are tons of materials on machine learning (or prediction) online. If I am interested in learning about doing inference, however, what are some resources online that I can consult?

Update: I just realized that the word "inference" could potentially mean lots of stuff. What I meant by "inference" refers to questions such as

Did $X$ cause $Y$ or $Y$ caused $X$? Or more generally, what's the causal relations among $X_1,X_2,\cdots,X_n$?
Since "all models are wrong", how "wrong" is our model from the true model?
Given the information of a sample, what can we say about the population and how confident can we say that?

Due to my very limited statistics knowledge, I am not even sure whether those questions fall into the realm of statistics or not. But those are the types of questions which machine learning practitioners do not seem to care about. Perhaps statisticians do not care neither? I don't know.
Comment: Brian D Ripley is cited on useR! 2004 with "To paraphrase provocatively, machine learning is statistics minus any
checking of models and assumptions." The phrase has become part of the `fortunes` package on CRAN. This just to say, you are not alone with the Impression, that mathematical rigor is not always the main concern in machine learning.
Comment: Leo Breiman tackles exactly this question in his 2001 paper ["Statistical Modeling: the two cultures"](http://projecteuclid.org/euclid.ss/1009213726), which is a great read.
Answer: First, I would have different perspective for machine learning. What you mentioned, Andrew Ng's Coursera lecture and Kaggle competition are not 100% of machine learning but some branches that targeted on practical applications. Real machine learning research should be the work that invent the random forest / SVM / gradient boosting model, which is fairly close to statistics / math.
I would agree machine learning practitioners focus more on accuracy comparing to statisticians / economists. There are reasons that people interested in getting better accuracy, rather than "inference about the true distribution." The major reason is the way we collect data and use the data has been changed over past decades.
Statistics was established for hundred years, but in the past, no one would think about you have billions of data for training and other billions of data for testing. (For example, number of images on Internet). Therefore, with relatively small amount of data, assumptions from domain knowledge are needed to do the work. Or you can think about to "regularize" the model. Once the assumptions were made, then there are inferences problems about the "true" distribution.
However, if we carefully think about it, can we make sure these assumptions are true, and the inferences are valid? I would like cite George Box:

All models are wrong but some are useful

Now, let's back to think about the practical approach to put more emphasis on accuracy than assumption / inference. It is a good approach, when we have huge amount of data.
Suppose we are building a model for all images contain human faces on pixel level. First, it is very hard to propose the assumptions on pixel level for billion of images: no one has that domain knowledge. Second, we can think about all possible ways to fit the data, and because the data is huge, all the models we have may not be sufficient (almost impossible to over fit).
This is also why, "deep learning / neural network" got popular again. Under the condition of big data, we can pick one model that really complex, and fit it as best as we can, and we may still OK, because our computational resources are limited, comparing to all the real data in the word.
Finally, if the model we built are good in huge testing data set, then they are good and valuable, although we may not know the underline assumption or true distribution.

I want to point out the word "inference" has different meanings in different community.

In statistics community, it usually means getting information of the true distribution in parametric or non-parametric way.
In machine learning community, it usually means computing certain probabilities from a given distribution. See Murphy's Graphical Models Tutorial for examples.
In machine learning, people use the word "learning" to represent "getting the parameters of the true distribution", which is similar to the "inference" in statistics community.

So, you can see, essentially, there are many people in machine learning are also doing "inference".
In addition, you may also think about people in academia like to "re-brand their work and re-sell": coming up with new terms may be helpful to show the novelty of the research. In fact, there are many overlaps among artificial intelligence, data mining and machine learning. And they are closely related to statistics and algorithm design. Again there are no clear boundaries for doing "inference" or not.
Comment: I can see where you are coming from. An alternate take might be: prediction = focus on observed variables, inference = focus on hidden variables. So in a sense inference is trying to produce new *types* of measurements, while prediction is more about new *realizations* of measurements that could *in principle* be observed? (This is compatible with your answer, of course)


Overriding does not work on virtual method in ASP.net Core MVC

Question: namespace ProjectA.Controllers
public partial class CustomerController : Controller
{
 public virtual IActionResult Index()
 {
  ...Some code here
 }

 public virtual IActionResult Login()
 {
  ...Some code here
 }
}

namespace ProjectB.Controllers
public partial class CustomerController : ProjectA.Controllers.CustomerController
{
 public override IActionResult Login()
 {
  ...Some code here
 }
}

When I try to override as above it gives me below error

AmbiguousActionException: Multiple actions matched.
  The following actions matched route data and had all constraints satisfied:
  ProjectA.Controllers.CustomerController.Index (ProjectA)
  ProjectB.Controllers.CustomerController.Index (ProjectB)

Though there is no Index action in ProjectB. When I change the name of the conroller of ProjectB as:
namespace ProjectB.Controllers
public partial class CustomCustomerController : ProjectA.Controllers.CustomerController
{
 public override IActionResult Login()
 {
  ...Some code here
 }
}

AmbiguousActionException is gone but still the override doesnt work. I dont get call in overriden action.
Is it like .Net core doesnt support overriding any more because it works like charm without .net core with the same name of controllers.
Please help..
Answer: As you have both class name as "CustomerController". And both may have using default routing. So for both controllers url should be same as /{controller}/{action}.
So for same url there will be 2 actions available for each controllers. That is why you are getting

"AmbiguousActionException" exception.

When you change your child controller to "CustomCustomerController" then url will be changed for both controllers so no such ambiguity happen so it worked.
As you said override not working. In this case you have to use url as "/CustomCustomer/login" then it will go to Login action of CustomCustomerController.
EDIT 1
You can update your code as below and it should work fine. Create your controllers as below:
namespace ProjectA.Controllers {
    public partial class CustomerController : Controller
    {
        public virtual IActionResult Index()
        {
            ...Some code here
        }

        public virtual IActionResult Login()
        {
            ...Some code here
        }
    }
}

namespace ProjectB.Controllers {
    public partial class CustomCustomerController : ProjectA.Controllers.CustomerController
    {
        public override IActionResult Login()
        {
            ...Some code here
        }
    }
}

And Update app.UseMvc(routes => part into startup.cs as below. Add customer route before default route.
app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "customer",
        template: "customer/{action}/{id?}",
        defaults: new { controller = "CustomCustomer", action = "Index" });

    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

Comment: Hello,
Thank you for answer but I cannot use url as you said because in View page it is written fixed "Customer/login" and I do not want to move towards customizing view pages as far as it is working from C# code.
This is working fine as I said in MVC5 project but not in .Net MVC Core project..
Comment: @NopAdvance Have you checked routeconfig.cs are exact same in both projects?
Comment: Hi @karan,
Have you ever did this in .net core mvc?
Comment: @NopAdvance, Yes I have worked with core. Have you checked in your core project inside `startup.cs` `app.UseMvc(routes =>` have same `routes.MapRoute(` as on your old project's `RouteConfig.cs`?
Comment: Hi.. thank you. I will check and update that shortly
Comment: This is not working with override. What you provided is only routing solution but we are not looking for that..


data leakage when scaling time series

Question: Suppose I want to forecast future values of $y$ past values of features $x$.
In this example I am using:

the training set goes from $t_0$ to $t_{15}$
values from $x_{t_0}$ to $x_{t_{10}}$ to forecast $y_{t_{11}}$
values from $x_{t_1}$ to $x_{t_{11}}$ to forecast $y_{t_{12}}$
and so on until I use $x_{t_6}$ to $x_{t_{15}}$ to forecast $y_{t_{16}}$

I scale the my feature $x$ using only data in the training set (up to $t_{15}$)
Nevertheless when I try to predict $y_{t_{17}}$ you can see from the picture below that I use some data points that have also been used for scaling.
Is this leakage?
Answer: Formally speaking, for the prediction of $y_{t_{16}}$ and onwards, there is no leakage because the standardisation does not include information that would be unavailable at the time of prediction.
That said, the training does have some leakage in itself though as for example when we predict $t_{10}$ we use info from parts of the training set that occur from $t_9$ onwards. For any particular prediction at $t_i$, using information based on $t_{i+1}$ should be strictly avoided. To fix the training leakage, it makes sense, during training, to standardise the sample only with data that have been seen up until the point of prediction.
Rob Hyndman has a pretty authoritative blog post on the matter of cross-validation for time series. For something slightly more formal, Cerqueira et al. (2019) Evaluating time series forecasting models offers an excellent recent overview of many approaches (cross-validation in a blocked forms, out-of-sample methods, etc.) As more general reference for cases where the assumption of independence is compromised or where model extrapolation is likely it worth looking at Roberts et al. (2016) Cross‐validation strategies for data with temporal, spatial, hierarchical, or phylogenetic structure: it views the issue from an Ecology lens but it really detailed and accessible.

data leakage when scaling time series


C# parsing of Freebase RDF dump yields only 11.5 million N-Triples instead of 1.9 billion

Question: I'm working on building a C# program to read the RDF data in the Google Freebase data dump. To start out, I've written a simple loop to simply read the file and get a count of the Triples. However, instead of getting the 1.9 billion count as stated in the documentation page (referred above), my program is counting only about 11.5 million and then exiting. The relevant portion of the source code is given below (takes about 30 seconds to run).
What am I missing here?
// Simple reading through the gz file
try
{
    using (FileStream fileToDecompress = File.Open(@"C:\Users\Krishna\Downloads\freebase-rdf-2014-02-16-00-00.gz", FileMode.Open))
    {
        int tupleCount = 0;
        string readLine = "";

        using (GZipStream decompressionStream = new GZipStream(fileToDecompress, CompressionMode.Decompress))
        {
            StreamReader sr = new StreamReader(decompressionStream, detectEncodingFromByteOrderMarks: true);

            while (true)
            {
                readLine = sr.ReadLine();
                if (readLine != null)
                {
                    tupleCount++;
                    if (tupleCount % 1000000 == 0)
                    { Console.WriteLine(DateTime.Now.ToShortTimeString() + ": " + tupleCount.ToString()); }
                }
                else
                { break; }
            }
            Console.WriteLine("Tuples: " + tupleCount.ToString());
        }
    }
}
catch (Exception ex)
{ Console.WriteLine(ex.Message); }

(I tried using GZippedNTriplesParser in dotNetRdf to read the data by building on this recommendation, but that seems to be choking on an RdfParseException right at the beginning (Tab delimiters? UTF-8??). So, for the moment, trying to roll my own).
Comment: A bug report to the dotNetRDF mailing list or the issue tracker wrt the parser choking on the Freebase output would be appreciated
Answer: The Freebase RDF dumps are built by a map/reduce job that outputs 200 individual Gzip files. Those 200 files are then concatenated into one final Gzip file. According to the Gzip spec, concatenating the raw bytes from multiple Gzip files will produce a valid Gzip file. A library that adheres to the spec should produce a single file with concatenated content of each input file when uncompressing that file.
Based on the number of triples that you're seeing, I'm guessing that your code is only uncompressing the first chunk of the file and ignoring the other 199. I'm not much of a C# programmer but from reading another Stackoverflow answer it seems like switching to DotNetZip will solve this problem.
Comment: Shawn thanks for pointing out what I was missing! The 200 files seems to explain it.  I experimented with DotNetZip (and read their documentation) following your suggestion, but have so far not been able to make it work to produce a combined all-files Stream from which I can do things such as ReadLine(). It seems to read only the first file, similar to the .NET GZipStream. It apparently allows it more easily for zip (not gzip) files.  I'll continue more experiments; in case anybody can post any relevant code samples, that'll be super helpful!
Answer: I'm use DotNetZip and create decoration class GzipDecorator for "gzipped chunks" workaround.
sealed class GzipDecorator : Stream
{
    private readonly Stream _readStream;
    private GZipStream _gzip;
    private long _totalIn;
    private long _totalOut;

    public GzipDecorator(Stream readStream)
    {
        Throw.IfArgumentNull(readStream, "readStream");
        _readStream = readStream;
        _gzip = new GZipStream(_readStream, CompressionMode.Decompress, true);
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        var bytesRead = _gzip.Read(buffer, offset, count);
        if (bytesRead <= 0 && _readStream.Position < _readStream.Length)
        {
            _totalIn += _gzip.TotalIn + 18;
            _totalOut += _gzip.TotalOut;
            _gzip.Dispose();
            _readStream.Position = _totalIn;
            _gzip = new GZipStream(_readStream, CompressionMode.Decompress, true);
            bytesRead = _gzip.Read(buffer, offset, count);
        }
        return bytesRead;
    }
}

Comment: you have explain how above answer  address the question
Comment: Libs for ungzip concatenated gzip files doesn't work. They're ungzip only first file in sequence. You can read answer and comment before my answer with explanation about problem source. Simple switch to DotNetZip doesn't work.
Comment: I've same problem with read from gzipped nginx logs - only small piece of log was read by common libs. And my searches brings me to questions without answers. And to bug reports from year 2002 with resolve "this is not a bug".
Comment: I tried this example, but got "Bad GZIP header" exception
Answer: I managed to solve the problem by repacking dump using "7-zip" archiver. Maybe it helps you.


Python: How to extract a List thats inside an array, which is inside a list?

Question: Been trying to figure this out everyday for a week...
I am receiving data, then using json.loads and variable-ing it.
Here is the print output that I cant seem to further variable-ize without error codes:
[548, {'a': ['2364.66000', 16, '16.94656538'], 'b': ['2364.65000', 8, '8.45377850'], 'c': ['2364.66000', '0.35264111'], 'v': ['494.53876892', '54702.28957251'], 'p': ['2363.99238', '2359.02527'], 't': [404, 31739], 'l': ['2355.99000', '2258.59000'], 'h': ['2375.91000', '2453.03000'], 'o': ['2369.82000', '2338.83000']}, 'ticker', 'somerandontextcharacters']
Trying to assign one or more of these boldly-specific numbers to variables, you know, so I can have my way with them.
P.S. x = [548,{thewholething-etc...]
P.S.S. the data is received from a websocket connection response using .recv command, then json.loads'ed. Some of the printed outputs are in {etc,etc,} but some are in the form of [etc,{etc,etc,}]
Comment: I suggest u should add the output example,It hard to see what's u really want
Comment: x = json.loads(response)
print(x) # displays the above
Comment: Only seeing error codes when trying to extract & assign variables from x
Comment: That isn't an error, just a list of values.
Answer: It's pretty easy actually:
test = [{'a' : [1,2,3], 'b': [4,5,6]}] # this is just an example

var1 = test[0]['a'][1] # should be 2
var2 = test[0]['b'][2] # should be 6

print(var1)
print(var2)

Output:
2
6

Comment: if x[0] == 548?
Comment: You can filter by checking the type of each: `if type(x) == list`.
Comment: with x = json.loads(pub.recv())
 print(x[1]['a'][1])
Comment: Let me post a few lines, one reply at a time, 1 sec
Comment: x = json.loads(pub.recv())    print(x)
Comment: Heres the Response:
Comment: {'connectionID': 17234373327, 'event': 'systemStatus', 'status': 'online', 'version': '1.8.6'}
Comment: {'channelID': 548, 'channelName': 'ticker', 'event': 'subscriptionStatus', 'pair': 'ETH/USD', 'status': 'subscribed', 'subscription': {'name': 'ticker'}}
Comment: [548, {'a': ['2379.87000', 8, '8.30000000'], 'b': ['2379.20000', 7, '7.61166173'], 'c': ['2379.20000', '0.10575000'], 'v': ['1488.42961203', '55035.29415629'], 'p': ['2367.56985', '2359.44388'], 't': [1083, 31756], 'l': ['2354.00000', '2258.59000'], 'h': ['2380.83000', '2453.03000'], 'o': ['2369.82000', '2346.98000']}, 'ticker', 'ETH/USD']
{'event': 'heartbeat'}
Comment: Above are three different "ticks" of a the websocket response.... I get the error when trying to print(x[1]['a'][1]). Is it because the responses have different forms?
Comment: :'(   < me pulling my hair out for the past week
Comment: keyerror when trying print(x[1]['a'][1]) instead of print(x). Print statement is in a "While True: Try:" if that matters. Is there a way to filter all responses for "beginning with [548.." ?
Comment: OMG YES it successfully did it on the third message, BUT threw key errors for every response that didnt start with [
Comment: It worked thank you so much. Now if only I had a way to filter responses that start in { or in [
Comment: You sir deserve a reward. Im going to your profile.
Comment: For anyone who has read this far down, the problem has been solved. The problem itself was that websockets feed LISTs and DICTs, and the program wouldnt run at all when trying to print contents from a list without IF type(x) == list. Thank you for immediately responding, and thank you for sticking with me the whole way to the solution @Have_a_nice_day. All of your responses helped solve this!


Can I use Monitor.Enter/Exit (c# lock) with WPF without fear of reentrancy bugs?

Question: If I use Monitor.Enter/Exit (through the c# lock syntax) in a WPF application, can the dispatcher cause re-entrance?
In the sample below, presuming OnTextChanged is called when the text in a textbox changes, could the call to _worker.RunWorkerAsync() be called incorrectly?
public class SomeClass
{
    private object _locker = new object();
    private bool _running = false;
    private BackgroundWorker _worker;

    public void SomeClass()
    {
        // initialize worker...
    }

    void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        lock (_locker)
            _running = false;
    }

    void _worker_DoWork(object sender, DoWorkEventArgs e)
    {
        // ... do something time consuming ...
    }

    private void OnTextChanged()
    {
        lock(_locker)
        {
            if (!_running)
            {
                _worker.RunWorkerAsync();
                _running = true;
            }
        }
    }
}

I believe it's possible, but I've not been able to reproduce this. Does WPF somehow prevent the dispatcher from invoking waiting tasks when waiting on monitor?
Answer: Not sure what you fear.  Both OnTextChanged and RunWorkerCompleted run on the UI thread.  It won't be re-entrant, you don't need the lock either.  Either method can only start running when the UI thread is idle, pumping the message loop.
Comment: That's not possible.  The `_running` flag is only ever set by code that runs on your main thread.  RunWorkerCompleted can *never* run at the same time as OnTextChanged.  That's the point of using BGW in the first place.  It's pretty well documented in the MSDN library.
Comment: It's pretty simple: when a thread is executing code, it cannot execute other code.  A thread can do only one thing at a time.  If you want to do more than one thing at the same time then you need more than one thread.  BGW helps you avoid shooting your leg off doing so.  The apparent behavior of a UI thread in a GUI program is a carefully crafted way of making it look *to the user* of doing more than one thing at the same time.  That illusion disappears if that "one thing" takes more than 50 milliseconds.  And then you use a BGW.
Comment: I'm afraid of a race condition. Presume the background task is sometimes fast, or is skipped altogether. It's possible that the line in OnTextChanged to set _running = true is run after the call to _running = false. My background task will never run when OnTextChanged is called, b/c _running will forevermore be true. This is especially true if my code can be preempted by the dispatcher (to run some other unrelated task) after the line _worker.RunWorkerAsync() and before _running = true.
Comment: Yes, both run on the the UI thread. Background worker invokes the completed delegate so it's run on the thread it was created on, which of course is the point of BGW. What prevents the background worker's callback from being invoked while the OnTextChanged method would be called, or vice versa? Nothing as far as I'm aware.
Answer: While not directly related to your question, you could run into register caching issues if you don't mark _running as volatile.
Actually this isn't strictly true, as you are not using a double-checked lock. I've left the information related to volatile there anyway, for your reference.


Montar tela em Android

Question: Estou desenvolvendo uma aplicação em android e estou em uma parte que preciso colocar muitos itens na tela e para que não fique muito bagunçado, pensei em montar uma tela com o estilo da imagem que esta abaixo

Selecionando os itens a esquerda e mudando a direta, procurei na internet mas não achei, aguem poderia me ajudar como que posso montar neste estilo.
Estou utilizando o Android Studio, para desenvolver.
Answer: Essa tela usa a classe Fragments.
Você pode combinar vários fragments em apenas uma activity para construir uma interface gráfica com vários painéis. Além do mais você pode reutilizar seus Fragments em múltiplos activities.

Você pode pensar em um fragment como uma seção modular de um activity, que tem seu próprio cilco de vida, recebe seus próprios eventos de entrada, e que você pode adicionar ou remover enquanto o activity está rodando. É um tipo de sub activity que pode ser reusado em diferentes activities.
Fonte: Fragments | Android Developers
Fonte extra: Multi-pane development in Android with Fragments - Tutorial
Comment: Valeu...Vou começar a estudar aqui para implementar.
Answer: Embora a abordagem de Fragments seja correta, o print que você anexou parece ser de uma tela de Preferências.
Se você estiver realmente utilizando PreferenceActivity ou PreferenceFragment, a opção recomendada é usar PreferenceHeaders: https://developer.android.com/guide/topics/ui/settings.html#PreferenceHeaders

Montar tela em Android


Counting the number of falses before a true in an ArrayList (Java)

Question: I am trying to count the number of falses before each true in an ArrayList in Java.
For example, if I have [true, false, false, true, true]
I would get [0,2,0]
and if a false at the end of the list is not followed by a true, it would not count
so if I have [false, true, false, false]
I would get [1]
I tried this:
int incorrect_attempts = 0;
        ArrayList<Integer> falses_before_a_true = new ArrayList<Integer>();

        for (int i=0;i<=attempts_list.size()-1;i++){
            if (attempts_list.get(i).equals("false"))
                incorrect_attempts++;
            else
                falses_before_a_true.add(incorrect_attempts);

        }

but I would get [1, 1, 1] for [false, true, true, true] which should return [1,0,0]
I think I should reset the incorrect_attempts counter when there are two consecutive trues, but not sure how to do that.
Any help is appreciated. Thanks!
Comment: Ok! is that a wish or have u written some code as well?
Comment: Thanks for the note, @JunedAhsan. I added the code I'm trying to fix.
Comment: caught your bug...check my answer
Answer: You need to reset incorrect_attempts to 0 when you get a true. So change this:
        else
            falses_before_a_true.add(incorrect_attempts);

to
    else {
        falses_before_a_true.add(incorrect_attempts);
        incorrect_attempts = 0;
    }


rails_admin empregado pode somente editar quem não é administrador

Question: Rails 5
gens rails_admin, devise, cancancan
Tenho um model User {name:string, ..., admin_role:boolean, employee_role:boolean, user_role:boolean}
<code>class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)
    can :read, :all                 # allow everyone to read everything
    cannot :manage, [Gender]
    return unless user.admin_role? || user.employee_role?
    can :access, :rails_admin       # only allow admin and employee users to access Rails Admin
    can :dashboard, :all            # allow access to dashboard
    if user.admin_role?
      can :manage, :all             # allow superadmins to do anything
    elsif user.employee_role?
      can :update, [User], admin_role: false
    end
  end
end
</code>
como o "can :update, [User], admin_role: false" consigo "editar" somente os que não são administradores, mas não consigo salvar a edição..
O que estou fazendo errado?
Answer: troquei o can :update, [User], admin_role: false por can :manage, [User], admin_role: false
Assim fica bem mais completa a regra.


Is $f(x) = \frac{x + 1}{x + 2}$ a function?

Question: My discrete mathematics book has the following problem:

22. Determine whether each of these functions is a bijection from $\mathbb{R} \to  \mathbb{R}.$
c) $f (x) = \frac{x + 1}{x + 2}$

Since one element of the domain doesn't have an image, namely when $x = -2$, is $f(x) = \frac{x + 1}{x + 2}$ even a function?
Comment: But a bijection from R to R must be defined on every R (this isn't) and every y in R must be y = f(x) for some x.  This isn't a bijection from R to R.  But it is a *function*.  A function of a subset of R to a subset of R.
Comment: A function is a special relationship where each input has a single output. It does not have to be continuous for all points. if you can find at least 1 point where $f(x1)= v1$ and $f(x1)=v2$ where $v1$ is not equal to $v2$, then the relation is not a function.
Comment: We can define its domain to not include that point.  Does your book have another example that is similar that is worked out?
Comment: Which discrete mathematics book?
Comment: @RodrigodeAzevedo Please don't make nonsubstantive edits to old questions. That moves them to the active queue, which wastes the time of folks like me who watch that queue for new things.
Answer: The mapping you specify cannot be a function $\mathbb{R}\rightarrow\mathbb{R}$ since is it not defined for $x=-2$ It is injective on its domain but not onto since the equation $f(x) = 1$ is insoluable.  It is, however a bijection from $\mathbb{R}-\{2\}$ to $\mathbb{R}-\{1\}$.
Specification of a function must include a domain and codomain. This example here shows why you must do that.
Comment: No. Consider the mapping $x\mapsto x^2$. This is a bijection of $[0,\infty)$ onto itself but it is not a bijection $[0,\infty)\rightarrow \mathbb{R}$.
Comment: It's still a *function* from f:R/{2} -> R.  The OP asked if it was "even a function".  The answer is yes.  Just not on all of R.
Comment: I don't make that assumption.  You must specify domain and codomain.
Comment: Actually, since domain and codomain are not specified, a function here has not been properly defined.
Comment: f: R->R means that R is both the domain and the codomain, no?
Comment: That sort of is my point.  This *is* a function and it was properly defined.  That it's domain is not the entire reals was not explicitly stated but can be determined by observation.  For the purpose of the textbook I'm sure there's a blanket "unless stated otherwise all functions are from real to real".  It'd be very misleading to say, especially to a student,  "this isn't a function" or it isn't a properly defined function of the reals, even if that can be technically argued.
Comment: I'll grant that.  In this case the real concept is to determined if its a bijection from R to R which it isn't as not all points of R are mapped.  However I think we'd be leading the student astray if we said the it isn't a *function* at all.  It is.  And it's a real valued function from some reals to some other reals.
Comment: In mathematics abuse of notation is so common that I think it's essential to admit that when most mathematicians talk about $f(x)=(x-1)/(x+2)$ they will consider it a function with the correspondingly shrunk domain.
Comment: I'm not 100% sure stating f:R -> R, nescessarily implies that f is defined on all R.  I think it just means the domain is a subset of R.  But I could be mistaken.
Comment: "Actually, since domain and codomain are not specified, a function here has not been properly defined." Isn't it acceptable to simply state the function maps real values to real values without specifying the exact domain and codomain?  I think we can safely call these functions.
Comment: No.  This function fails to admit $2$ to its domain.  How you specify domain and range is critical to telling if a function is injective or surjective.
Comment: Every function is a bijection if you restrict its domain and codomain to the right points.
Comment: "I don't make that assumption. You must specify domain and codomain"  Who must?  The teacher or the student?  I've never seen the codomain explicitly stated for  non-surjective functions.  That's always been left to be determined.  I'll give you that the domain must/should be specified.  Although I seen a blanket "All these functions map space X to Y" and leave the precise domain to be determined.
Comment: "Every function is a bijection if you restrict its domain and codomain to the right points"  true.  But if you claim you have to specify the domain and codomain and it's not acceptable to say "from all reals where it works" we'd have to throw out nearly every calculus textbook I've ever seen.
Comment: If you must define the domain and codomain, then by definition all functions are surjective as the codomain must be specified not to have any points not mapped to it.
Comment: @fleablood: That's not true. There is no requirement that a function's codomain equal its range. (That said, in most situations it doesn't really matter what codomain you define a function with, as long as it's a superset of the range, where the latter is the image of the domain you define it with. The domain, by contrast, usually *does* matter, though it's usually obvious from context without needing to be specified explicitly.)
Comment: @fleablood I agree with you that in many applications the domain and codomain are left somewhat vague (eg f is a function from *some* reals to some other set of reals...but we'll leave it to you to figure it out). However, I think that since this is a textbook question specifically about this fundamental concept, they are likely to be more pedantic about the domain/codomain definition, just so the student can demonstrate they understand these basic definitions.
Answer: What is a function?
A popular definition, which can be directly mapped to set theory, is that a function is a set of input / output pairs. E.g.:
$f(x) = \frac{x + 1}{x + 2}$
could represent the set of pairs:

(-1, 0)
(0, 1/2)
(1, 2/3)

But it could also represent the set of pairs:

(0, 1/2)
(1, 2/3)
(2, 3/4)

Both of these are two completely different functions.
Morale: a formula like $f(x) = \frac{x + 1}{x + 2}$ is not a function.
A formula + a domain ($\mathbb{R}-\{2\}$ here) may represent a function if the formula is well defined over the domain.
But what a function really is, is the a set of pairs. You just have to come up with method that clearly describes that set of pairs.
For your specific case, you could take the domain as $\mathbb{R}-\{2\}$ and the set of points is specified.
Furthermore, you could also add a new pair (-2, 1234) to the function, and you'd have a function defined over $\mathbb{R}$.
Also worth noting: in this case we cannot make the function continuous by choosing any value at -2, but in some cases we can. E.g.:
$f(x) = \frac{x}{x}$
can be made continuous at 0 by adding the pair (0, 1).
The big advantage of such a set theoretical definition is that it can be used easily in formal proof systems: What does "formal" mean?


What is the equivalent for query_posts() in WP e-commerce

Question: I am using WP e-commerce for my worpress blog. I want to display a particular product in my template. I wrote the code like this.
<code>          query_posts('p=186');
          while (wpsc_have_products()) :  wpsc_the_product();
                 echo wpsc_the_product_title();
                       ................
                       ................
          endwhile;
</code>
But it is not working. I think the problem is with query_posts(). Is there any equivalent function for query_posts or How can I display a product with it's id in WP e-commerce.
Answer: See if it works for you:
<code>$wpsc_query = new WPSC_query("category_id=3");
</code>


How to add a submission handler to a form class on Drupal 8?

Question: I would like to add a submission handler to my form class so that the submitted values are saved. The parent class does not save them but only displays a message. I use Drupal 8! Here is my form class:
<?php

namespace Drupal\greeting\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class GreetingForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'greeting_form';
  }

/**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form = parent::buildForm($form, $form_state);

    $config = $this->config('greeting.settings');

    $form['page_text'] = array(
      '#type' => 'textarea',
      '#title' => $this->t('Content of the Greeting page'),
      '#default_value' => $config->get('page_text'),
      '#description' => $this->t('Allows you to define the text of the Greeting page'),
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $config = $this->config('greeting.settings');

    $config->set('page_text', $form_state->getValue('page_text'));

    $config->save();

    return parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'greeting.settings',
    ];
  }

}

$config = \Drupal::config('greeting.settings');
$page_title = $config->get('page_text');

How do I do it?
Comment: What does "save" mean here? Does your code not save the value to the active configuration?
Comment: The code you've appended at the end of your class will not show the value you've saved. If that's how you're determiningg it is not saving, you are incorrectly determining that. From all appearances, your class should save fine.
Comment: @cilefen Yes, my code was already storing the value in the active configuration.
Comment: @Jaypan Yes, I deleted the code at the end of the class. Thanks a lot!
Answer: It looks like the problem reported in the question details have been resolved and the code in the question seems to be working.
However for anybody reaching this page trying to find an answer to the question in the title, here is the answer.
You can attach custom submit handlers to submit buttons in forms by setting the #submit property on the submit button.
$form['actions']['button_name'] = [
  '#type' => 'submit',
  '#value' => $this->t('Button Text'),
  '#submit' => ['::customSubmitHandler1', '::customSubmitHandler2'],
];

customSubmitHandler1 and customSubmitHandler2 would be functions defined inside your form class.
If instead you want to call a function defined in a module, you can add 'custom_module_submit_handler' which can be a function defined in a module.


O que é ViewHolder Pattern?

Question: Estou implementando um RecyclerView no Android e notei que é necessário criar uma classe chamada ViewHolder. Esta classe parece ser um padrão de projeto chamado ViewHolder Pattern, e minhas dúvidas surgem exatamente neste ponto.

Dúvidas

O que é ViewHolder Pattern?
Qual é a finalidade do ViewHolder Pattern?
Quais são seus benefícios?
Answer:
O que é ViewHolder Pattern?

Abordagem utilizada para guardar um conjunto de views para que possam ser eficientemente acedidas e reutilizadas, quando necessário.
A finalidade da classe ViewHolder é guardar as views.

Qual é a finalidade do ViewHolder Pattern?

Evitar o uso repetido de findViewById() para obter as referências às views.
Ao serem guardadas num objecto ViewHolder as referencias estão disponíveis de imediato.

Quais são seus benefícios?

Evitar a perda de performance devido ao uso repetido findViewById().
Comment: Uma dúvida @ramaral, ButterKnife tem uma função semelhante que evita a repetição de chamar o findViewById()... Então podemos dizer que ele está dentro deste padrão ?
Comment: @Matheus Não. O que o ButterKnife faz é gerar, para sim, as chamadas ao método `findViewById()`. Veja a [página principal do ButterKnife](http://jakewharton.github.io/butterknife/). Lá, ao ser referido o padrão ViewHolder, é dito que "simplifica", o que é diferente de substituir.
Comment: @ramaral, falando em *Butterknife*, usar o padrão ViewHolder numa activity, e passar ela como parâmetro pra uma classe auxiliar, é uma má prática em relação a eficiência? ou seria melhor passar os objetos em si? ou seria ainda melhor fazer tudo que tem que fazer nas views dentro da própria activity (independente de usar ou não viewholder)?
Comment: @GR_ Lamento mas não consigo perceber o que está perguntando, talvez por falta de contexto. O que quer dizer com "usar o padrão ViewHolder numa activity"?
Comment: @ramaral eu vou criar uma pergunta e deixo o link aqui pra você compreender. Acho mais fácil que explicar tudo em um comentário haha
Comment: @ramaral https://pt.stackoverflow.com/q/270541/4306

O que é ViewHolder Pattern?


Ensuring a prerequisite exists for a pattern rule

Question: I understand that an "explicit" pattern rule will take precedence on its implicit counterpart when its prerequisites can be made.
all: src/foo.o

src/%.o: makefile my_haeder.h src/%.c
    echo Do something with those source files
If there is a typo for "my_header.h", the implicit rule for %.o will take precedence. Not only my recipe will not be executed, but touching the prerequisites will not trigger the rule. Actually it is the second point which is of interest for me.
The make documentation offers a verification using static pattern rules:
SET_OF_FILES=src/foo.o

all: src/foo.o

$(SET_OF_FILES): src/%.o: makefile my_haeder.h src/%.c
    echo Do something with those source files
This results in:
gmake: *** No rule to make target `src/my_haeder.h', needed by `src/foo.o'.  Stop.
Though a larger rule, that solution is nice, as long as I don't have to add a rule for which the stem could overlap:
SET_OF_FILES=src/foo.o src/subsrc/bar.o

all: src/foo.o

$(SET_OF_FILES): src/%.o: makefile my_header.h src/%.c
    echo Do something with those source files

$(SET_OF_FILES): src/subsrc/%.o: makefile my_header.h src/subsrc/%.c
    echo Do something with those other source files
Which results in:
makefile:8: target `src/foo.o' doesn't match the target pattern
makefile:9: warning: overriding commands for target `src/foo.o'
makefile:6: warning: ignoring old commands for target `src/foo.o'
makefile:9: warning: overriding commands for target `src/subsrc/bar.o'
makefile:6: warning: ignoring old commands for target `src/subsrc/bar.o'
The first message is here because I didn't bother $(filter)ing SET_OF_FILES. I don't know how to solve the next warnings, which for any reviewer would mean "something's wrong".
Is there another (more elegant) way to verify that the prerequisites are actually feasible, in order to avoid dropping the explicit pattern rule?
(using GNU Make 3.79.1 win32)
Comment: Your "overlap" example doesn't make sense to me, because the first clause is supposed to specify the target files for which the pattern rule applies.
You're lumping the two targets together: which says that both rules should be used for each target.
Answer: Add a separate rule to check for your prerequisites
all: prereqs src/foo.o

prereqs: Makefile my_header.h

src/%.o: src/%.c Makefile my_header.h
    echo Do something with those source files

src/subsrc/%.o: src/subsrc/%.c Makefile my_header.h
    echo Do something with those other source files

which will give you:
make
make: *** No rule to make target 'my_header.h', needed by 'prereqs'.  Stop

Comment: I think filtering SET_OF_FILES was indeed the culprit in my problem. In your solution, make is probably issuing an error because target `all` requires target `prereqs`, which in turn requires `Makefile` and `my_header.h`. It does not even check the two filtered rules, which are the ones which should be verified.


Como funciona e onde se utiliza Bag of Words?

Question: Recentemente tenho pesquisado sobre inteligência artificial e achei alguns artigos falando de um tal "bag of words", mas não sei o que é e não achei nada em português falando sobre isso.
Gostaria de saber, o que é o tal "bag of words", e em quais casos ele se aplica? Se possivel, deixe as fontes.
Comment: Conjuntos: `{0, 1, 2} U {2, 4} = {0, 1, 2, 4}`; _bags_: `{0, 1, 2} U {2, 4} = {0, 1, 2, 2, 4}`; também _bags_: `{0, 1, 3, 1, 2} - {0, 1} = {3, 1, 2}`
Comment: @JeffersonQuesado Legal, obrigado por me mostrar um caminho. Parece ter sim uma relação com o termo "bag of words", mas ainda não é o que eu queria.
Comment: @JeffersonQuesado Acho que consegui chegar onde eu queria, publiquei uma resposta, acho que ilustra bem o assunto.
Comment: `bag` é um conceito matemático. Algo mais ou menos como `conjunto, porém pode repetir elementos e ignora ordenação`
Comment: Tem algum desses artigos com link fácil para ajudar a contextualizar o uso desse `bag`?
Comment: Infelizmente não :/
Comment: Não entendi direito sua explicação, poderia dar exemplos de casos que se aplicam? @JeffersonQuesado
Comment: Para aplicações no mundo real, vide https://en.wikipedia.org/wiki/Bag_(mathematics)?wprov=sfsi1
Comment: Google Acadêmico retorna algo para "bag of words"?
Comment: Sem querer achei isso: https://en.m.wikipedia.org/wiki/Bag-of-words_model; parece relevante
Comment: Parece que a terceira seção desse relatório descreve algo sobre "bag of words": http://conteudo.icmc.usp.br/CMS/Arquivos/arquivos_enviados/BIBLIOTECA_113_RT_209.pdf
Comment: @JeffersonQuesado Não sabia que existia esse Google Acadêmico, lol. Esse Wikipedia tem algumas coisas interessantes, mas ainda não achei o que eu queria...
Comment: o relatório técnico que eu enviei o link depôs do link da Wiki, foi mais direto ao assunto? Lá ele fala em classificação de documentos e PLN usando "bag of words", inclusive tem uma tabela lá de uns documentos de exemplo com as palavras "cas", "filh" e outras
Comment: boa resposta =)
Answer: Explicação
O modelo bag-of-words é uma representação simplificada usada no processamento de linguagem natural e na recuperação de informações (IR). Neste modelo, um texto (como uma frase ou um documento) é representado como o saco (multiset) de suas palavras, desconsiderando a gramática e mesmo a ordem das palavras, mas mantendo a multiplicidade.
Exemplo de Implementação
Os seguintes modelos são um documento de texto usando bag-of-words.
Aqui estão dois documentos de texto simples:
(1) John gosta de assistir filmes. Mary também gosta de filmes.

(2) John também gosta de assistir jogos de futebol.

Com base nesses dois documentos de texto, uma lista é construída da seguinte forma:
[
    "John" ,
    "gosta" ,
    "de" ,
    "assistir" ,
    "filmes" ,
    "Mary" ,
    "também" ,
    "futebol" ,
    "jogos"
]

Também é comum calcular a frequência de aparecimento das palavras:
linear(tj) = 1 − d(tj)/N

Onde tj é a palavra que deseja encontrar a frequência, d(tj) a quantidade de vezes em que a palavra aparece e N é a quantidade de documentos ou frases.
Conclusão
De forma simples, o bag-of-words é uma forma de representação de texto. E comumente é usado para machine learning, sentiment analysis, chatbot e topic model.
Fonte: Wikipedia


how to set CSS for MPDF in yii2 on generating pdf file

Question: I am using yii2 basic and I need to generate a pdf report file which is styled with CSS file, I get the pdf well but CSS is not working on the generated file. Here is how I have implemented.
in my controller.php file
 public function actionPrintReport($student_id,$exam_id,$total_subjects,$exam_date){
        $model = new Results();
        $examReportData = $model->getExamReport($student_id,$exam_id,$exam_date);

        $htmlContent = $this->renderPartial('_report',
        [
            'model' => $model,
            'total_subjects' => $total_subjects,
            'examReportData' => $examReportData
        ]);
        $pathfile = "Student_exam_report";
        $mpdf = new \Mpdf\Mpdf([
            'tempDir' => Yii::getAlias('@runtime/') . 'mpdf2/tmp',
            'format' => 'A4-L',
            'margin_right' => 5,'margin_left' => 5,
            'defaultFont' => 'Calibri',
        ]);
        $stylesheet = '<link rel="stylesheet" type="text/css" href="'.Yii::getAlias('@web').'\css\exam_report.css'.'"/>';
        $mpdf->WriteHTML($stylesheet, \Mpdf\HTMLParserMode::HEADER_CSS);
        $mpdf->WriteHTML($htmlContent, \Mpdf\HTMLParserMode::HTML_BODY);
        $mpdf->Output($pathfile, 'I');
    }

The _report.php file is located in view/ with the below minimal code
<?php
    use yii\helpers\Html;
?>

Left Pan

Right Pan

And CSS file is located at project_folder/web/css/exam_report.css with the minimal code like this.
 .rightleft{
    height: 150px;
    float: left;
    border-style: dotted dashed solid double;
}
.rightpan{
    height: 150px;
    float: right;
    border-style: dotted dashed solid double;
}

I have stacked here for two days :), I would appreciate for your support.
Comment: Are you using kartik-mPDF?
Comment: @InsaneSkull yes I am using Kartik-mPDF
Comment: You can use `cssFile` property while creating Pdf Object.
Answer: You can use "cssFile" option for external css files. In my case the bootstrap css file that is included in the plugin was not working so I created my own in the web folder. You have to check the path of the file as well when adding.
$pdf = new Pdf([
          'mode' => Pdf::MODE_CORE,
          'format' => Pdf::FORMAT_A4,
          'orientation' => Pdf::ORIENT_PORTRAIT,
          'filename' => '/backend/web/invoices/invoice.pdf',
          'destination' => Pdf::DEST_FILE,
          'content' => $content,
          'cssFile' => 'css/invoicetemplate.css',
          'cssInline' => '.heading{font-size:18px}',
          'options' => ['title' => 'Invoice'],
        ]);


What does it mean by: Taylor series of $f(1+x)$ converges to $f(1+x)$?

Question: I have $f(1)=0$ and $f'(x)=1/x$. Consider Taylor series of $f(1+x)$ centered at $x=0$, I need to show that it converges to $f(1+x)$.
I got $f(1+x) = \sum (-1)^{n}*x$.
I don't know what to do from here and I am not sure what "Taylor series of $f(1+x)$ converges to $f(1+x)$" means.
Comment: Write down the Taylor series of order $N$ $T_N(x)$ for $f(1+x) = \log(1+x)$ about $x=0$ ($T_N(x) = \sum_{n=0}^N \frac{f^{(n)}(1)x^n}{n!}$). Then use [Taylor's theorem with the remainder term](http://en.wikipedia.org/wiki/Taylor%27s_theorem#Explicit_formulae_for_the_remainder) to show that the Taylor series for $f(1+x)$ converges to $\log(1+x)$ (if $|x| <1 $) when $N\to \infty$.
Answer: You say:

I got $f(1+x) = \sum (-1)^{n}\cdot x$.

I am guessing that is not what you have done. From what you know about $f(1+x)$, you can build a related series, known as the Taylor series centered at $0$ (or Maclaurin series for short). If you do this correctly, you get $$\sum_{n=1}^{\infty}(-1)^{n-1}\frac{x^n}{n}$$ But you do not yet have the right to say that $f(1+x)$ equals this series. In fact it doesn't. But for $x$-values in the appropriate window, which turns out to be $(-1,1]$, this series does converge to the same value that $f(1+x)$ takes. This is what you are being asked to prove: that for $x\in(-1,1]$, $\sum_{n=1}^{\infty}(-1)^{n-1}\frac{x^n}{n}$ converges to $f(1+x)$. Once we all understand this we can be loose and say $f(1+x)=\sum_{n=1}^{\infty}(-1)^{n-1}\frac{x^n}{n}$ for $x\in(-1,1]$.
The way to prove such things is to understand and use Taylor's inequality.
Comment: Can you help with little more? I am not understanding it at all. Can you show me little more steps to get correct function?
Comment: Ok, I was stupid. I got the right summation.


Como colocar InterstitialAD no aplicativo?

Question: Como coloco aquele anúncio que ocupa a tela inteira no meu aplicativo , e só é fechado quando a pessoa clicar no X?
Answer: Anúncios de tela cheia, chamados de  Anúncios intersticiais cobre toda tela da aplicação, como um modo FullScreen. Veja abaixo como é indicado e descrito na própria documentação:
Primeiro deve-se instanciar um objeto InterstitialAd
...
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
requestNewInterstitial();
...

No ação do botão você deve verificar seu ad foi carregado usando isLoad(). Veja:
if (mInterstitialAd.isLoaded()) {
     mInterstitialAd.show();
} else {
     beginPlayingGame();
}

Veja o código completo de como deve ficar sua main:
public class MainActivity extends ActionBarActivity {

    InterstitialAd mInterstitialAd;
    Button mNewGameButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNewGameButton = (Button) findViewById(R.id.newgame_button);

        mInterstitialAd = new InterstitialAd(this);

        // aqui o seu adUnitId
        mInterstitialAd.setAdUnitId("seu adUnitID aqui");

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                requestNewInterstitial();
                beginPlayingGame();
            }
        });

        requestNewInterstitial();

        mNewGameButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    beginPlayingGame();
                }
            }
        });

        beginPlayingGame();
    }

    private void requestNewInterstitial() {
        AdRequest adRequest = new AdRequest.Builder()
                  .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
                  .build();

        mInterstitialAd.loadAd(adRequest);
    }

    private void beginPlayingGame() {
        // Play for a while, then display the New Game Button
    }
}


How many distinct arrangements of the letters in the word 'Mississippi' have no S's in the first six places?

Question: CONTEXT: Question made up by uni lecturer.
How many distinct arrangements of the letters in the word 'Mississippi' have no S's in the first six places?
My first attempt of the question involved treating the S's as a single block of letters (i.e. you find arrangements of elements of $\{M, I, I, I, I, P, P, SSSS\}$), however, this doesn't work because if the first six places can't contain S's, then the last five places will, which means the S's could be separated (i.e. in 'PIMIPISSISS', the block of S's is separated by a 'I').
My second attempt involved subtracting the complement (all distinct arrangements with S's only in the first six places) from the total number of distinct arrangements which is $\frac{11!}{4!4!2!}=34650$, but I realised this would require a lot more calculations than just considering the original case.
I'm a bit stuck on how to approach this question; any guidance would be greatly appreciated.
Comment: The last five letters, unordered, have to consist of the four $S's$ plus one other letter, either $M,I$ or $P$ of course.  So you have three cases to consider.
Answer: The word MISSISSIPPI has eleven letters.  Therefore, if no S's appear in the first six places, they must occupy four of the last five positions.  Choose which four of those five positions they occupy.  That leaves seven positions to fill with four I's, two P's, and one M.  Choose four of those seven positions for the I's, two of the remaining three positions for the P's, then fill the final position with the M.

 $$\binom{5}{4}\binom{7}{4}\binom{3}{2}\binom{1}{1}$$
Comment: A particular arrangement of the letters of the word MISSISSIPPI is completely determined by which letter is placed in which position.  There are $\binom{11}{4}$ ways to choose the positions of the I's, $\binom{7}{4}$ ways to choose four of the remaining positions for the S's, $\binom{3}{2}$ ways to choose two of the remaining three positions for the P's and one way to place the M.  That gives $$\binom{11}{4}\binom{7}{4}\binom{3}{2}\binom{1}{1} = \frac{11!}{7!4!} \cdot \frac{7!}{4!3!} \cdot \frac{3!}{2!1!} \cdot \frac{1!}{1!0!} = \frac{11!}{4!4!2!1!}$$ distinguishable arrangements as you found.
Comment: How can you just choose which four of the five positions the S's occupy? Don't you need to consider all possible arrangements of the S's within those five positions?


c++ new C array allocation, RAII or simple shared_ptr / boost::shared_array

Question: I am learning c++ and i stumbled once again on a new issue.
I do need to allocate a C array for a library to use, but in a safe way, ofcourse. I already found that delete[]; at the end of method fails miserably.
OLD, not that good:
float *buf;

try {
    buf = new float[daswidth*chans_info.chans*sizeof(float)];
}
catch (std::bad_alloc& ba) // sometimes it throws ! so i do need to stop my execution.
{
    if (DEBUG) tekstasFormat(L"WARNING: bad alloc caught: %s", ba.what());
    return; // skip this iteration then.
}

//... OUR CODE

delete[] buf;

So what i tryed to use which works perfectly instead of my old allocation and deletion:
float *buf;

std::shared_ptr<float> safe_buf(new float[daswidth*chans_info.chans*sizeof(float)], [](float *p) { delete[] p; });

// OR BOOST EQUIVALENT
boost::shared_array<float> safe_buf(new float[daswidth*chans_info.chans*sizeof(float)]);

buf = safe_buf.get();

And we do never leak, everyone is happy.
But how to catch new_alloc throws now ?!
if i will allocate shared_ptr in a new{} scope after new it gets destroyed... Explain me and noobs like me for the future a bit more.
How to handle exceptions in this case ?
Comment: Is there a reason you cant use `std::vector`?
Comment: `std::vector` actually contains an `T*` that is created when you initialize it. There will be some small overhead with the checks and functions called when using it but its definitely worth it.
Comment: That `sizeof(float)` seems suspicious here. Don't know what is your use case, but `new float[n*sizeof(float)]` allocates `n*sizeof(float)` floats, not `n*sizeof(float)` bytes. Just so you know.
Answer: Prefer std::unique_ptr to shared_ptr.  It's far faster.
std::unique_ptr<float[]> buf; //construct

try {
    buf.reset(new float[daswidth*chans_info.chans*sizeof(float)]); //give memory
}
catch (std::bad_alloc& ba) // sometimes it throws ! so i do need to stop my execution.
{
    if (DEBUG) tekstasFormat(L"WARNING: bad alloc caught: %s", ba.what());
    return; // skip this iteration then.
}

However, as Ben said, there's pretty much no reason to use a float[] like this.  Instead, use std::vector.  Yes, even for most* C interop.
std::vector<float> buf;

try {
    buf.resize(daswidth*chans_info.chans*sizeof(float)); //give memory
}
catch (std::bad_alloc& ba) // sometimes it throws ! so i do need to stop my execution.
{
    if (DEBUG) tekstasFormat(L"WARNING: bad alloc caught: %s", ba.what());
    return; // skip this iteration then.
}

function_expecting_float_pointer(buf.data()); //use .data() to get the `float*`

*don't use std::vector if the C code will ever "reallocate" the pointer for you.  Instead, use std::unique_ptr, and (1) release the memory, (2) pass to C, (3) reset with pointer returned from C.
Comment: @Arnas: Only in one very specific way: C arrays don't initialize data by default, and `vector` will try to initialize it's elements by default.  Correct and careful use of `vector` will make them the same speed, but if you don't know what you're doing, it can theoretically sometimes be ever so slightly slower.  However, it's always significantly easier and safer.  Use it.  Always.
Comment: Thanks duck, .reset is what i needed. Resetting the size of ptr when allocating new data is the way to go which i didnt knew about. But, one question - isnt c array faster than vector within same usage as c array ?
Comment: Well, in my case i would prefer unique_ptr then, since it would initialize whole array at a 100Hz rate which is not necessary at all.
Comment: @Arnas: (1) There's few reasons you need code to run at 100Hz.  Usually 60Hz is more of a max.  (2) The only time it would be unnecessary is if the C code is initializing the array for you.  Is that the case?


Creamy vs Crunchy Distortion

Question: I'm hearing that you should have a crunchy distortion for rhythm playing and a creamer distortion for lead. In general, I'm usually more a clean player, but I'm trying to learn. I've been playing with my two main pedals, a Washburn Soloist and DigiTech Bad Monkey, at unity gain into my solid-state Frontman amp, and I'm finding it hard to distinguish between them.
By playing chords, I get harmonic clash and "crunch". By playing single-note licks and being careful to avoid double-stops or sloppy playing, I get  sustaining "creamy" notes. I can't get "creamy" chords or intervals and, without a fuzz pedal, I don't seem to have "crunchy" single notes. (I know "Satisfaction" so I know it's possible.)
Are there settings to these? Or is it all how you play, not how you stomp?
Comment: Well yeah - that's why I added the "adoring fans" part...
Comment: Why not adjust the sound quality to what you (or your adoring fans :-) ) like?
Comment: I'm not always tasked to play music I like.
Answer: To me it has mainly to do with the amount of gain. One other important factor is midrange. The sound of Satisfaction is comparably low in gain, and has some nice bite in the mids. If you compare to e.g. Santana, who could be considered to have a creamy solo sound, he has quite a lot of gain, and not so much midrange.
Playing on the neck pickup will produce a smoother sound, whereas the bridge will have more bite, or crunch.
I would play with extreme settings. Put quite a low amount of gain and crank the middle on your amp. Use the bridge pickup. Getting crunchier? Max out the gain on your pedal, dial back the midrange, use the neck pickup. Strike a chord. Should be somewhat creamy,
Comment: Well try that out next time my amp and I can have some meaningful alone time.

Creamy vs Crunchy Distortion


Is $I(X,Y;Z)$ connected with $I(X;Z)$ and $I(Y;Z)$?

Question: Let $I(X,Y;Z)$ be the mutual information between the tuple $X,Y$ and the variable $Z$:
$$I(X,Y;Z) = H(X,Y) - H(X,Y|Z)$$
From the data processing inequality it follows trivially that:
$$I(X;Z) \le I(X,Y;Z),\quad I(X;Y) \le I(X,Y;Z)$$
Is there a more specific connection between $I(X,Y;Z)$ and $I(X;Z)$ and $I(Y;Z)$? For example, if $I(X;Z)=0$, I can see intuitively that $I(X,Y;Z) = I(Y;Z)$, and this is not a consequence of the data processing inequalities; so there must be another connection.
Answer: $X$ and $Y$ independent
The simplest case is when $X$ and $Y$ are independent. In this case $H(X,Y) = H(X) + H(Y)$ and $H(X,Y|Z) = H(X|Z) + H(Y|Z)$ (note that I am assuming that $X,Y$ are independent even when $Z$ is given). Then it follows from the identities:
$$I(X,Y;Z) = H(X,Y) - H(X,Y|Z)$$
$$I(X;Z) = H(X) - H(X|Z)$$
$$I(Y;Z) = H(Y) - H(Y|Z)$$
that
$$I(X,Y;Z) = I(X;Z) + I(Y;Z)$$
$X$ and $Y$ not independent
In general, $X$ and $Y$ may not be independent, and we have:
$$H(X,Y)=H(X)+H(Y)-I(X;Y)$$
$$H(X,Y|Z)=H(X|Z)+H(Y|Z)-I(X;Y|Z)$$
so it follows that:
$$I(X,Y;Z) = I(X;Z) + I(Y;Z) + I(X;Y;Z)$$
where $I(X;Y;Z) = I(X;Y|Z) - I(X;Y)$ is the interaction information, and measures the information shared by all 3 variables. It can be positive, zero, or negative, so not much else can be said. Intuitively, $I(X;Z) + I(Y;Z)$ only measures the information that $X$ and $Y$ each give by itself, but misses the additional information gained by considering $X$ and $Y$ together (if $I(X;Y;Z)$ is positive), or over counts the common information that both $X$ and $Y$ give about $Z$ (if $I(X;Y;Z)$ is negative); in reality, $I(X;Y;Z)$ measures the balance between these two effects.
Another way to write this result is using the chain rule for conditional entropy:
$$I(X,Y;Z) = I(X;Z) + I(Y;Z|X)$$
However in this last form the explicit connection between $I(X,Y;Z)$ and $I(X;Z),I(Y;Z)$ is lost.
Answer: I just want to add one thing to becko's nice answer.

For example, if $I(X;Z)=0$,I can see intuitively that $I(X,Y;Z)=I(Y;Z)$ and this is not a consequence of the data processing inequalities; so there must be another connection.

This intuitive statement is false! The classic example is if Z = X xor Y, and X and Y are independent random binary variables. Each of the mutual information is zero, $I(X;Z)=I(Y;Z)=0$, but $(X,Y;Z)=1$. This is the classic case of "synergistic" variables. I.e., X and Y together have information that neither has individually. A different (and more useful, I think) breakdown of these types of relationships is given by Beer & Williams in their classic paper introducing "Partial Information Decomposition". Though, please note, that the measure of redundancy that they propose their is not unique, some followup papers from Griffith & Koch, and others, consider this.
Comment: Thanks. It also follows from the equations in my answer that my original example is wrong :)
Comment: Right! I didn't catch that. The condition for additivity, $I(X,Y;Z) = I(X;Z) + I(Y;Z)$, requires that something called the interaction information: https://en.wikipedia.org/wiki/Interaction_information is zero.


How to overwrite some assignments in a python script file?

Question: I have a python file describing a set of objects/variables. Example with file.py:
a = 5
b = a+1

And I would like to import the file's content with the ability to overwrite the value of some variables: parse_with_overwrite(file.py, {"a": 6})
would make the current locals() contain a=6 and b=7.
Of course, like any python file, not all variables are described in one single line… so it's not possible to split each line on the = sign. Moreover, order of the variables matters since some variables may depend on other.
Is there any builtin or library that could help me achieve this?
Currently, I get the content of file.py with:
context = {}
with open("file.py", "r") as f:
    # the magic should occur here :-)
    exec(f.read(), None, context)

Answer: A safe way to do it is to use the ast module to parse the file. You can then modify the nodes in the ast and finally compile it.
Here is a start:
import ast

def replace_top_assignments(tree, map):
    for i, node in enumerate(tree.body):
        if not isinstance(node, ast.Assign):
            continue

        for nt in node.targets:
            if not isinstance(nt, ast.Name):
                # deal with tuple assignments a, b = 1, 2
                continue
            if nt.id in map:
                # ok we have to replace this
                if not isinstance(node.value, ast.Constant):
                    # warning it is not assigned a constant
                    continue
                # change value for all targets. this will break a=b=2 with a replace of a = 5
                node.value.value = map[nt.id]

src = """
a = 23
b = a
# todo handle these
# b = c = 2
# r, s = 1, a
# conf = usion = 1  what should happen here if you replace usion with 4 ? is conf 4?

print(a)
print(b)
"""

tree = ast.parse(src)
replace_top_assignments(tree, {'a': 4})
code_obj = compile(tree, 'aaa', mode='exec')
exec(code_obj)

Comment: By the way, my question phrasing might not be very accurate... feel free to update the question if you feel it can describe better my use case :-)
Comment: Sweet, everything works fine now :-) thanks for your help


from bs4 import BeautifulSoup Error

Question: I have this error when importing Beautiful Soup.
from bs4 import BeautifulSoup
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    from bs4 import BeautifulSoup
  File "C:\Users\pood1\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\__init__.py", line 30, in <module>
    from .builder import builder_registry, ParserRejectedMarkup
  File "C:\Users\pood1\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\builder\__init__.py", line 308, in <module>
    from . import _htmlparser
  File "C:\Users\pood1\AppData\Local\Programs\Python\Python36\lib\site-    packages\bs4\builder\_htmlparser.py", line 7, in <module>
    from html.parser import (
ImportError: cannot import name 'HTMLParseError'
Python:3.6.1 , BeautifulSoup:4-4.3.2

How do I solve this?
Comment: Possible duplicate of [Importing bs4 in Python 3.5](https://stackoverflow.com/questions/28745153/importing-bs4-in-python-3-5)
Comment: I solved this problem. Thank you. The problem was my python version. After I change the version to 3.5.3, This error was go away!!
Comment: If you solved the problem, please add the solution as an answer and mark it accepted. Thanks.
Answer: Can you upgrade to the latest version of Beautiful Soup?
pip3 install --upgrade beautifulsoup4

This error should be fixed in the latest version. BeautifulSoup 4.4.0 has been updated to be python3.5 compatible
Comment: Thank you. I sovle this problem. Thank you very much!!
Comment: If my answer answered your question can you please accept it and up vote?

from bs4 import BeautifulSoup Error


My VS 2022 doesn't handle over than 32 bit bitset

Question: I am trying to convert 45 bit binary number into a hex number but when compiling, I get overflow error, but when applying the code on online C++ compiler, it works. My platform is X64. Any help please.
int main()
{
    stringstream ss;
    string binary_str("111000000100010010100000110101001000100011000");
    bitset<45> n(binary_str);

    string f;
    ss << hex << n.to_ulong() << endl;  // error happens here
    f = ss.str();
    cout << f;
    return 0;
}

When compile this code above on online C++ compiler I get a correct result which is OX1c08941a9118.
Comment: Do you really have set your compiler to x64? Maybe you are compiling for 32bit?
Comment: @Klaus My VS-2022 gives the error when using a string longer than 32 chars, and it is set for 64-bit build.
Comment: Windows uses the [LLP64 data model](https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models), so ulong has 32-bits.  You must use [to_ullong()] (https://cplusplus.com/reference/bitset/bitset/to_ullong) instead.
Comment: I was confused by the sentence, that a online compiler delivers a different result/behavior. I thought it was also msvs compiler... if it is a different compiler... ok :-)
Comment: @Klaus I used https://www.onlinegdb.com/online_c++_compiler to run the code
Answer: unsigned long is 32bit with MSVC. Also when compiling for x64. You need unsigned long long to get a 64bit integer, so in this case you can use to_ullong:
ss << hex << n.to_ullong() << endl;

Comment: @Code_Student usually yes, but you can also configure individual projects to compile for a different platform than the active solution platform
Comment: Nailed it! Answers are so obvious when someone else tells you them. :)
Comment: Thats exactly why it is a good idea to use uint64_t instead :-) ... which not helps in that case as there is no n.to(uint64_t&) version... bitset should improve the interface :-)
Comment: @harold   thank you so much! It works now with to_ullong()
Comment: @Klaus when I go to Configuration Manager on MSVS the Active solution platform is x64. Is that where to make sure I am compiling on 64-bit or not? Thanks


Distribution for simulating machine learning algorithm errors

Question: I want to simulate machine learning algorithm, that solves binary classification problem. This algorithm should output not just $\{0, 1\}$ labels, but probability of class $1$ (that of course is in $[0; 1]$ range). The question is what distribution should I choose for simulating errors produced by this algorithm. As I understand it should be some one-sided bounded distribution, but I don't know which one.
Comment: Could you be more specific on what exactly do you want to simulate?
Answer: If I understand correctly, you have some $p \in (0,1)$ and you would like to add some error $\epsilon$ to it such that $p+\epsilon \in (0,1)$.  The problem being, how to chose $\epsilon$ such that we don't escape the bounds $(0,1)$.
One solution may be to sample $\epsilon$ from any distribution with support over $\mathbb{R}$ (e.g Normal distribution), and add this error to the log-odds for $p$.  You then calculate your new $p^*$ to have the corresponding log odds.
To clarify, since $p \in (0,1)$,
$$
\ln\frac{p}{1-p}\in(-\infty,\infty)
$$
So we can sample our errors such that $\epsilon\in(-\infty,\infty)$ without changing the original range upon addition.  This gives us,
$$
\ln\frac{p^*}{1-p^*}=\ln\frac{p}{1-p}+\epsilon
$$
It can be shown that if
$$
\ln\frac{q}{1-q} = k
$$
then,
$$
q = \frac{e^k}{1+e^k}
$$
in this case $q=p^*$ and $k=\ln\frac{p}{1-p}+\epsilon$, and so we have
$$
p^* = \frac{e^{\ln\frac{p}{1-p}+\epsilon}}{1+e^{\ln\frac{p}{1-p}+\epsilon}}
$$
$$
 = \frac{\frac{p}{1-p}e^\epsilon}{1+\frac{p}{1-p}e^\epsilon}
$$
Which will stay within $(0,1)$
Edit:
The last line can be further simplified to
$$
 = \frac{e^\epsilon}{\frac{1-p}{p}+e^\epsilon}
$$


How to test any features that require authentication?

Question: I am just getting started with BeHat and I'm able to successfully run tests that do not require authentication. But, anything that requires authentication is failing.
STATEMENT  (or any logged in statement)
Fails with ERROR MESSAGE .
But, if I add 'not-logged-in' as a possible class for logged in, then the test passes. This tells me that I am not successfully logging in with my tests. The test is actually finding a class 'not-logged-in' when it is testing to see if it's logged in.
I am getting email notification that a user has been created when the test runs. So, I know that a user is being created, but not successfully logging in.
Feature: Authentication
  In order to make sure Behaviour testing is working
  As a behat test user
  I want to make sure I can log in

  Scenario: Anonymous access
    Given I am on the homepage
    Then I should see "Login"

@api
  Scenario: Test Being Logged In
    Given I am logged in as a user with the "administrator" role
    Then I should see the link "Test Link"

My behat.yml file looks like this:
default:
  suites:
    default:
      contexts:
        - FeatureContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
  extensions:
    Behat\MinkExtension:
      goutte: ~
      selenium2: ~
      base_url: http://mysite.localhost
    Drupal\DrupalExtension:
      blackbox: ~
      api_driver: 'drupal'
      drush:
        alias: 'local'
      drupal:
        drupal_root: '/Users/myfolder/sites/mysite'
      region_map:
        footer: "#footer"

Answer: As mentioned in my question, it was pretty clear that Behat was failing to login, so I began to look for modules that might interfere with the login process.
It turns out that we are using the module AntiBot (https://www.drupal.org/project/antibot) and when we disabled this module, we were able to run tests that involved successfully logging into Drupal.


Can`t change the height on a image in javascript

Question: I want to create a function for for my header when its changing his size.
I have a logo img inside the header.I want when i change the headers height to be for example 80px the image to be 80 px as well.
I initialized a function in Javascript but doesnt work( i`m a beginner at Javascript). Please tell me what i m doing wrong in js and maybe show me the right way to do it.

Home

Bio

Training

Academy

Gallery

Store

Contacts

.header {
    background: blue;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 20;
  height: 80px;}
.header-content{
  display:flex;
}
.menu-links{
  display:flex;
}
.links-list{
  display:flex;
  color:white;
}

const mainNav = document.querySelector('.header');
const img = document.querySelector('.logo');

if (mainNav.style.height == '80px') {
    img.style.height = '80px';
} else {
    img.style.height = '100px';
}

Answer: The .style property can only return style information that has been set directly into the HTML element, either statically in the HTML as in:
 or via the .style being set in JavaScript as in: element.style = something;. Your code is based on an if condition that checks mainNav.style.height, but the style property hasn't been set in HTML or JavaScript at that point.
Instead, use .getComputedStyle(), which returns the final value for the provided style after all computations (regardless of where they were applied) are taken into account.
I've added a red border to your image in the code below to show that it is expanding the height to be the same size as the header.

const mainNav = document.querySelector('.header');
const img = document.querySelector('.logo');

// You can't check the .style property if the element
// hasn't had that attribute or property set yet.
if (getComputedStyle(mainNav).height == '80px') {
    img.style.height = '80px';
} else {
    img.style.height = '100px';
}
.logo { border:1px dashed red; }
.header {
    background: blue;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 20;
  height: 80px;}
.header-content{
  display:flex;
}
.menu-links{
  display:flex;
}
.links-list {
  display:flex;
  color:white;
}

.links-list a { color: white; }

Home

Bio

Training

Academy

Gallery

Store

Contacts

Comment: Man... i don`t have words to thank you for your answer...was everything that i was looking for 2 days.
Comment: @DConstantin Glad to help. Good luck!
Answer: The style property in Javascript only gives you the element's inline styles - those defined directly in a style attribute on it.
You need getComputedStyle to access the actual properties used, taking into account all CSS rules.


How to create a special autoincrement field?

Question: I have a "Property" content type created for a real estate web site. The Property content type has a taxonomy field called "Type" where type can be House, Office, Apartment and so on...
I want to create a "code" field that has to be something like "H-1" where H is for Home and 1 must be an incremental number, so houses will have each a different code: "H-1", "H-2", "H-3" ... "H-188", etc. And so on for Offices "O-1", "O-2"..."O-479". I see that there is a Serial module, but that module helps me only creating autoincremental numbers for each content type, not based on other fields of that content type.
I was planning to use auto_nodetitle + serial module + rules to create the letter+number format for my field, but I will have the same problem: If I create "H-1" and then create an Office, it will have "O-2" code, and not "O-1" code.
Is there a module than can help me? If not, how can I make it?
Comment: I can voice for the serial module. Works very well.
Comment: What do you want to happen if a node gets deleted ? do you want the increment to carry on ? like node ids
Comment: Hello Yaazkai, have you solved your problem? I would be interested in your solution for a similar case. Thanks
Answer: The obvious option would be to have a serial field for each type. Whether or not that is feasible based on your business rules (are there lots of types, are the types likely to change, etc.) is another question.
Another module that may be very useful in these types of problems is the computed field module.
Another option (probably the one I would choose) is to make it completely custom.
Either a custom entity property or a custom field of some sort that does database queries to determine the next value instead of auto_nodetitle + serial module + rules.
Be careful of race conditions when doing something like this though.
Answer: Just thinking out loud.
In rules (after saving content), set data value of the field. To set the field (direct input), use php code that counts all nodes of that particular type with 'Type" as house for example and echo H-(count+1).
Comment: Hi andrew, thanks for comment. But this solution has a problem, if a node is deleted then I will have the same code for two different Houses and if more nodes are deleted so it will decrease and repeat. How can I garantee that it is unique and incremental? Regards !
Comment: Hello @andrew. Thanks again. Could be, but what if there are 1000+ nodes? will take time to save a node while check all.

I think I have the answer, but it is a half answer.

Create a rule that:
Check if the saved node has "field_code"
Then, fetch the previous recent node that has the same 'type' (is a tax term) and just increment it in the recent node, if there is no previous node, then is the first. This will be incremental and unique, And didn't care to fill empty codes, dosen't matter. But I don't know **how to** make the rule :( can you tell me how to do it? Regards !
Comment: Hi andrew, that is my proposition and my first option. **But**, you know, clients/users want to have the properties codes in the other way :)
Comment: Hi andrew !

Well, I was thinking in make a substring eliminating the two first characters and just make the increment. But if I have to add another field just to make it easy dosen't matter. I'm using D7. I'm trying with rules but is the first time to me with this module so I guess is question of learning it. Thanks.
Answer: The comments were turning into a discussion, so here's some code that will retrieve the 'latest' node and retrive the code ($code) for that node.
<code>    <?
$terms = taxonomy_get_term_by_name($home);
$tid = $terms[0]->tid;
$query = new EntityFieldQuery();
$query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'your_content_type')
  ->fieldCondition('term_fieldname', 'tid', $tid)
  ->propertyCondition('status', 1)
  ->propertyOrderBy('created', 'DESC');
$result = $query->execute();
$nid = array_keys($result['node']);
$picked_node = $nid['0'];
$node = node_load($picked_node);
$wrapper = entity_metadata_wrapper('node', $node);
$current_code = $wrapper->field_code->value();
$new_code += $current_code;
$field_to_set = "H-$new_code";
?>
</code>
Here I'm assuming that you are going with the idea of having an extra integer field. This is pretty rough and untested so try it out on a simple page with php input format. echo the vars to see what you get...
Comment: If you only want the first result of your query don't waste resources selecting and building an array of all results. Use EntityFieldQuery::range() to limit the results. Also, whenever you are selecting a value from the database then incrementing it then saving the incremented value back to the database, be very aware of potential race conditions. What if 2 processes run at the same time and both select the same current code? You end up with multiple nodes having the same new code.


A system that conserved specific quantity

Question: Consider the system of $n$ linear equations $x'=Ax$. Suppose the system conserved the quantity $$\sum_{k=1}^n a_k x_k(t)=C \ \ \ \ \ (\star) $$
 for some $C \in \mathbb{R}.$
for some $a =(a_1,...,a_k)\in \mathbb{R}^n$. Prove that $A$ has a zero eigenvalue.
I do not know even how to start with this less information, I feel that the vector $a$ causing the existence of the zero eigenvalue, but how to show that. I appreciate any help or hints with that.
Thank you.
Answer: Hint: Differentiating both sides of $(\star)$, you get
$$ \sum_{k=1}^{n} a_{k} x'_{k}(t)= \sum_{k=1}^{n} a_k [Ax(t)]_k = \mathbf a \cdot A\mathbf x(t)=0. $$


Tooltip não funciona dinâmico

Question: Observe essa linha:
response.write ""

Essa linha esta dentro de um arquivo que é chamado via ajax. Se eu coloco dentro do html:

Funciona, mas via ajax não.
Já entendi por que, mas ainda não resolvi. Quando inicializa a função tooltip, tudo que tem title="" passa a ser data-original-title="" e por isso não pega o que vem dinâmico.
Já tentei vir direto com o data-original, mas também não foi.
A página que tem o tooltip eu chamo assim:
function PegarSemanaConsultaFinan(DataProcura){

    dados = "DataProcura="+DataProcura

    $.ajax({
        url: "financeiro-procura-semana.asp?"+dados,
        Type: "POST",
        success: function(result){
            $("#results-div-finan").html(result);
        },
        error: function(){
        }
    });
}

Alguém tem alguma dica?
Comment: Você está usando qual linguagem de programação?
Comment: ASP, mas chamo assim:
Answer: O problema que você está tendo é que seu tooltip está sendo adicionado dinâmicamente em sua página.
Tente isso:
$("body").tooltip({
selector: '[data-toggle="tooltip"]'
});

Para mim isso funciona corretamente, mas caso não funcionar, tente usar isso:
$(document).on('mouseenter','[data-toggle=tooltip]', function(){
    $(this).tooltip('show');
});

$(document).on('mouseleave','[data-toggle=tooltip]', function(){
    $(this).tooltip('hide');
});

Comment: Bingo. funcionou o primeiro com o body. muito obrigado pela ajuda. valeu mesmo
Comment: Funcionou perfeitamente, parabens.

Tooltip não funciona dinâmico