How to use GraphFrames on EMR serverless

Question: Summary of steps executed:

Uploaded the python script to S3.
Created a virtualenv that installs graphframes and uploaded it to S3.
Added a VPC to my EMR application.
Added graphframes package to spark conf.

The error message was:

22/09/11 18:44:49 INFO Utils: /home/hadoop/.ivy2/jars/org.slf4j_slf4j-api-1.7.16.jar has been previously copied to /tmp/spark-d0c75876-b210-4ce2-b2f8-cd65e59d00db/userFiles-1aef56b8-1c39-4158-b9a2-ee810a844314/org.slf4j_slf4j-api-1.7.16.jar
22/09/11 18:44:49 INFO Executor: Fetching file:/tmp/spark-ae6aefff-23d3-447e-a743-5ed41621fad0/pyspark_ge.tar.gz#environment with timestamp 1662921887645
22/09/11 18:44:49 INFO Utils: Copying /tmp/spark-ae6aefff-23d3-447e-a743-5ed41621fad0/pyspark_ge.tar.gz to /tmp/spark-7f9cf09a-dc6e-41e0-b187-61f1b5a80670/pyspark_ge.tar.gz
22/09/11 18:44:49 INFO Executor: Unpacking an archive file:/tmp/spark-ae6aefff-23d3-447e-a743-5ed41621fad0/pyspark_ge.tar.gz#environment from /tmp/spark-7f9cf09a-dc6e-41e0-b187-61f1b5a80670/pyspark_ge.tar.gz to /tmp/spark-d0c75876-b210-4ce2-b2f8-cd65e59d00db/userFiles-1aef56b8-1c39-4158-b9a2-ee810a844314/environment
22/09/11 18:44:49 INFO Executor: Fetching spark://[2600:1f18:61b4:c700:3016:ab0c:287a:ccaf]:34507/jars/org.slf4j_slf4j-api-1.7.16.jar with timestamp 1662921887645
22/09/11 18:44:50 ERROR Utils: Aborting task
java.io.IOException: Failed to connect to /2600:1f18:61b4:c700:3016:ab0c:287a:ccaf:34507
at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:288)

Could anybody point me to a solution/documentation that helps with this problem?
ANNEX
I also tried to add just graphframes package but got a "numpy not found error".
More detailed steps

python script:

import sys
from operator import add
from pyspark import SparkConf, SparkContext
from pyspark.sql import SQLContext
from graphframes import *

print('job started')

conf = (SparkConf()
    .setMaster("local")
    .setAppName("GraphDataFrame")
    .set("spark.executor.memory", "1g"))
sc = SparkContext(conf = conf)
sqlContext = SQLContext(sc)

if __name__ == "__main__":
    # Create a Vertex DataFrame with unique ID column "id"

    v = sqlContext.createDataFrame([
        ("a", "Alice", 34),
        ("b", "Bob", 36),
        ("c", "Charlie", 30),
        ], ["id", "name", "age"])

    # Create an Edge DataFrame with "src" and "dst" columns
    e = sqlContext.createDataFrame([
        ("a", "b", "friend"),
        ("b", "c", "follow"),
        ("c", "b", "follow"),
        ], ["src", "dst", "relationship"])

    # Create a GraphFrame
    from graphframes import *
    g = GraphFrame(v, e)

    # Query: Get in-degree of each vertex.
    print(g.inDegrees.show())

    # Query: Count the number of "follow" connections in the graph.
    g.edges.filter("relationship = 'follow'").count()

    # Run PageRank algorithm, and show results.
    results = g.pageRank(resetProbability=0.01, maxIter=20)
    print(results.vertices.select("id", "pagerank").show())

    sc.stop()

Virtualenv Dockerfile compiled and copied to S3.

FROM --platform=linux/amd64 amazonlinux:2 AS base
RUM yum install -y python
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install venv-pack==0.2.0
RUN python3 -m pip install graphframes
...

Just added a default generated VPC with NAT.
Conf graphframes added:

--conf spark.jars.packages=graphframes:graphframes:0.8.2-spark3.2-s_2.12

Conf for virtualenv

--conf spark.archives=s3://bucket_name/venv.tar.gz#environment
--conf spark.emr-serverless.driverEnv.PYSPARK_DRIVER_PYTHON=./environment/bin/python
--conf spark.emr-serverless.driverEnv.PYSPARK_PYTHON=./environment/bin/python
--conf spark.executorEnv.PYSPARK_PYTHON=./environment/bin/python

Comment: There's already a thread solving this issue: https://stackoverflow.com/questions/39261370/unable-to-run-a-basic-graphframes-example/40661407#40661407
Answer: Found the solution:
We can't use a context like the one I used:
conf = (SparkConf()
    .setMaster("local")
    .setAppName("GraphDataFrame")
    .set("spark.executor.memory", "1g"))
sc = SparkContext(conf = conf)
sqlContext = SQLContext(sc)

Use this instead:
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("HelloGraphFrame").getOrCreate()


Erro de Submit de App Store - Itunes Connect

Question: Quando clico em <code>"Submit for Review"</code> no iTunes Connect, aparece a seguinte mensagem:

The Newsstand app's Info.plist must contain UINewsstandApp=true. For more information, see the Information Property List Key Reference.

Alguém já passou por algo parecido?
Comment: vai no seu arquivo info.plist do projeto procura por UINewsstandApp se não tiver acrescenta
Answer: Dando uma pesquisado no stack, achei uma resposta para o seu erro. Só tem que deletar o Newsstand Icon.

Espero que tenha ajudado.
Comment: Não funcionou para mim... Mas valeu pela ajuda, vou aguardar para ver se alguém já passou por isso...
Answer: O cadastro que você preencheu para o review obriga que seu App esteja habilitado para aparecer na banca de jornal.
Adicione a tag UINewsstandApp na raiz do seu info.plist e set como YES.
Irá ficar assim:
Comment: Funcionou perfeitamente com essa solução. Muito obrigado :) !


Windows Server 2003 Blocking Outgoing FTP Passive Mode

Question: I have two machines behind the same firewall/router - one is a Windows 7 workstation and the other is a Windows 2003 server. The problem is with outgoing FTP access using the FileZilla FTP client (note: not server) on the Windows 2003 server.
Using FileZilla client on the Win7 workstation I am able to connect to an external FTP site using passive mode without issue. However, when I try the same thing from FileZilla client on the Windows 2003 server it hangs when attempting to retrieve the directory list. The log output is as follows:
Status: Resolving address of xxxxx.com
Status: Connecting to xxx.xxx.xxx.xxx:21...
Status: Connection established, waiting for welcome message...
Response:   220---------- Welcome to ...
Command:    USER ...
Response:   331 User ... OK. Password required
Command:    PASS *************
Response:   230 OK. Current directory is /
Command:    SYST
Response:   215 UNIX Type: L8
Command:    FEAT
Response:   211-Extensions supported:
Response:    EPRT
Response:    IDLE
Response:    MDTM
Response:    SIZE
Response:    REST STREAM
Response:    MLST type*;size*;sizd*;modify*;UNIX.mode*;UNIX.uid*;UNIX.gid*;unique*;
Response:    MLSD
Response:    ESTP
Response:    PASV
Response:    EPSV
Response:    SPSV
Response:   211 End.
Status: Connected
Status: Retrieving directory listing...
Command:    PWD
Response:   257 "/" is your current location
Command:    TYPE I
Response:   200 TYPE is now 8-bit binary
Command:    PASV
Response:   227 Entering Passive Mode (xxx,xxx,xxx,xxx,164,24)
Command:    MLSD
Error:  Connection timed out
Error:  Failed to retrieve directory listing

As you can see, a connection over port 21 is established and a request to enter passive mode results in port 42008 being established. But the attempt to retrieve data over that port fails. This works flawlessly on the Win7 machine.
I understand passive vs. active mode fairly well. It appears in this case that Windows Server 2003 is blocking traffic on certain ports. The Windows Firewall service isn't running on this device so I can eliminate that as the culprit. I would appreciate any other suggestions on how to track this down.
UPDATE
Turns out that it's specific to one FTP site. I can connect to another site using passive mode just fine. The difference appears to be port number. The passive port number on the site I can reach is 4693. The passive port number on the site that I can't reach is over 42000. Does this have any bearing on the situation?
UPDATE
I wrote a little socket server program running on the Win7 workstation that listened on port 50000, then from the Windows 2003 box I ran telnet to the address/port of the workstation and...it worked. So within the LAN Windows 2003 is allowing the traffic. Somehow it's not allowing the traffic out over the Internet. Baffling.
UPDATE
Problem was with FTP server. See answer below.
Comment: Typically, I'd blame the server for not opening the passive ports, but it looks like you've covered that angle. In your shoes, I'd install wireshark and watch traffic to the server. It might give you a better idea of what the client is attempting to do that's different from the Win 7 box (if anything)
Comment: Thanks SmallClanger. I'm familiar with WireShark, although hardly expert, so I didn't think of that. Will give it a try.
Comment: @SmallClanger - Well, I tried WireShark. I can see the packet that sends the MLSD command over the passive port, but it doesn't get a response. It just times out. When I do this from a workstation, no problem. To another site with passive port < 5000, no problem. So WireShark just reports what I already know. I just can't figure out why the high ports are blocked.
Answer:
On Windows Server 2003 and Windows XP
  the default range of ephemeral ports
  used by client applications is from
  1025 through 5000. Under certain
  conditions it is possible that the
  available ports in the default range
  will be exhausted.

netstat -n or netstat -b to see active connections
Increase the upper range of ephemeral ports that are dynamically allocated to client TCP/IP socket connections.

Start Registry Editor.
Browse to, and then click the following key in the registry:
  HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
On the Edit menu, click New, DWORD Value, and then add the following registry value to increase the number of ephemeral ports that can by dynamically allocated to clients:
  Value name
  MaxUserPort
  Value data

Close Registry Editor.

Note:
You must restart your computer for this change to take effect.
Note: Increasing the range of ephemeral ports used for client TCP/IP connections consumes Windows kernel memory. Do not increase the upper limit for this setting to a value higher than is required to accommodate client application socket connections so as to minimize unnecessary consumption of Windows kernel memory.
Comment: Still no joy. Is it possible that this setting only applies to Windows 2003 server R2?
Comment: Thanks alexm. I had already ran across and tried this KB article (http://support.microsoft.com/kb/196271) that outlined the fix you noted. I added the registry key, but it doesn't appear to correct the issue. Still, this appears to be exactly the cause of the problem. I'll continue to investigate.
Answer: So now it appears that this was a problem with the FTP server (PureFTPd). The admin tweaked something and restarted the server and, voilá, everything started working. I have to stop assuming the problem is with my code/configuration all the time.
Thanks to everyone that contributed.


inserting item after getting list from DBContext for view binding

Question: I'm trying to insert some object after querying from the database, but during view binding, a nullreferenceexception was thrown.
In my view, i did some simple looping and binding, the only thing i observed was that, the objects queried from db had a dynamicproxy object during runtime, but the object inserted didn't had any.
var items = ctx.items.Take(10).ToList();
var item = new Item();
item.name = "test";

items.Insert(2, item);
return (ActionResult)PartialView("_FetchPartial", items);

Answer: omg i found the problem...
in the view, i've embedded some server side values on the  tag
@Model[i].CreatedBy.UserName">

Stepping through the debugger skipped this line totally and i only manage to spot it when i move the @if(Model[i].ID != 0) check all the way at the start of the loop


How to determine whether a class has a particular templated member function?

Question: I was wondering if it's possible to extend the SFINAE approach to detecting whether a class has a certain member function (as discussed here:
"Is there a Technique in C++ to know if a class has a member function of a given signature?"
Check if a class has a member function of a given signature
) to support templated member functions? E.g. to be able to detect the function foo in the following class:
struct some_class {
   template < int _n > void foo() { }
};

I thought it might be possible to do this for a particular instantiation of foo, (e.g. check to see if void foo< 5 >() is a member) as follows:
template < typename _class, int _n >
class foo_int_checker {

  template < typename _t, void (_t::*)() >
  struct sfinae { };

  template < typename _t >
  static big
  test( sfinae< _t, &_t::foo< _n > > * );

  template < typename _t >
  static small
  test( ... );

public:

  enum { value = sizeof( test< _class >( 0 ) ) == sizeof( big ) };

};

Then do foo_int_checker< some_class, 5 >::value to check whether some_class has the member void foo< 5 >(). However on MSVC++ 2008 this always returns false while g++ gives the following syntax errors at the line test( sfinae< _t, &_t::foo< _n > > );
test.cpp:24: error: missing `>' to terminate the template argument list
test.cpp:24: error: template argument 2 is invalid
test.cpp:24: error: expected unqualified-id before '<' token
test.cpp:24: error: expected `,' or `...' before '<' token
test.cpp:24: error: ISO C++ forbids declaration of `parameter' with no type

Both seem to fail because I'm trying to get the address of a template function instantiation from a type that is itself a template parameter. Does anyone know whether this is possible or if it's disallowed by the standard for some reason?
EDIT: It seems that I missed out the ::template syntax to get g++ to compile the above code correctly. If I change the bit where I get the address of the function to &_t::template foo< _n > then the program compiles, but I get the same behaviour as MSVC++ (value is always set to false).
If I comment out the ... overload of test to force the compiler to pick the other one, I get the following compiler error in g++:
test.cpp: In instantiation of `foo_int_checker':
test.cpp:40:   instantiated from here
test.cpp:32: error: invalid use of undefined type `class foo_int_checker'
test.cpp:17: error: declaration of `class foo_int_checker'
test.cpp:32: error: enumerator value for `value' not integer constant

where line 32 is the enum { value = sizeof( test< _class >( 0 ) ) == sizeof( big ) }; line. Unfortunately this doesn't seem to help me diagnose the problem :(. MSVC++ gives a similar nondescript error:
error C2770: invalid explicit template argument(s) for 'clarity::meta::big checker<_checked_type>::test(checker<_checked_type>::sfinae<_t,&_t::template foo<5>> *)'

on the same line.
What's strange is that if I get the address from a specific class and not a template parameter (i.e. rather than &_t::template foo< _n > I do &some_class::template foo< _n >) then I get the correct result, but then my checker class is limited to checking a single class (some_class) for the function. Also, if I do the following:
template < typename _t, void (_t::*_f)() >
void
f0() { }

template < typename _t >
void
f1() {
  f0< _t, &_t::template foo< 5 > >();
}

and call f1< some_class >() then I DON'T get a compile error on &_t::template foo< 5 >. This suggests that the problem only arises when getting the address of a templated member function from a type that is itself a template parameter while in a SFINAE context. Argh!
Comment: C++ have chaged in 5 years :D Take a look at  http://stackoverflow.com/questions/36780867/i-neet-to-know-at-compilation-time-if-class-a-has-a-function-member-called-b/36780868#36780868
Answer: There is something similar already implemented in Boost.MPL, it is called "BOOST_MPL_HAS_XXX_TRAIT_DEF". See:
http://www.boost.org/doc/libs/1_41_0/libs/mpl/doc/refmanual/has-xxx-trait-def.html
It can detect if the class have a given named type.
Also, for your specific case, instead of passing the function pointer as a parameter (void (_t::*)()), try to use it in the body of your method, i.e., something like:
template < typename _t >
static big test( sfinae<_t> )
{
  &_t::foo<_n>;
}

Comment: Thanks for the Boost MPL link. Although they don't seem to detect functions, their method is similar and may work-around the problem I've got. I've just got to pick apart their macros now :)

I think the function pointer has to be obtained as part of the method signature for the compiler to use it for overload resolution. When I move it into the method, the compiler always picks the "big" overload of test so "value" is true even when the function does not exist.


Find $u, v$ such that $F\left[x, \frac{1}{x+y}, \frac{1}{y} \right]$ is integral over $F[u, v]$

Question:
Let $F$ be a field and let $R= F\left[x, \frac{1}{x+y}, \frac{1}{y} \right]$. Find $u, v \in R$ such that $R$ is integral over $F[u,v]$.

Let $y'=\frac{1}{y}$. I considered the polynomial $f$
$$f(t)=(t-x)(t-y')\left( t-\frac{x}{xy'+1}\right)\left( t-\frac{y'}{xy'+1}\right).$$
I couldn't think of $u, v$ such that the coefficients of $f$ (checked it using wolfram) are in $F[u,v]$. Can anyone help me on this?
Answer: Let $F$ be a field, and let $R=F[a,b,c]$ where
$$
\begin{cases}
a=x\\[4pt]
b={\large{\frac{1}{x+y}}}\\[4pt]
c={\large{\frac{1}{y}}}\\
\end{cases}
\qquad\;\;\;\;\;
$$
and let $u,v,w\in R$ be given by
$$
\begin{cases}
u=a+b+c\\[4pt]
v=ab+bc+ca\\[4pt]
w=abc\\
\end{cases}
$$
Then $a,b,c$ satisfy the equation $p(t)=0$, where
$$p(t)=t^3-ut^2+vt-w$$
hence $a,b,c$ are ntegral over $F[u,v,w]$.

But identically, for $a,b,c$ defined as above, we have
$$
w^6
+(6v-2u^2)w^4
+(u^4+9v^2-6u^2v+27)w^2
+(4u^3-18uv)w
+(4v^3-u^2v^2)
=
0
$$
hence $w$ is integral over $F[u,v]$.

It follows that $R$ is integral over $F[u,v]$.
Comment: oh, we can do it for arbitrary three variables $a, b, c$ in general. Thanks for your help!
Comment: @Andrew: No, not arbitrary. It's not automatic that $abc$ is integral over $F[a+b+c,ab+bc+ca]$, but for the given $a,b,c$, it is.


Linux/UNIX install data files

Question: I understand that when installing a C++ command line program on Linux/UNIX, it is customary instead of leaving it in its original directory, to move it to a directory that is already on the path, so I have a make install entry:
mv ayane /usr/local/bin

Looking a bit further ahead, I'm going to end up with a directory or two full of configuration, data and script files, which the program needs to read, some at startup, some later on demand, and in some cases subsequently modify and save again.
This leads to the question of how the program can know where its data files are located. Looking in /bin on my Ubuntu Linux virtual machine, it seems to live up to its name in containing only binary files, so the data files are not typically placed in the same directory as the program.
What's the usual solution for putting data files in a location that can be known to the program?
Comment: Relevant: http://stackoverflow.com/questions/528399/what-should-linux-unix-make-install-consist-of
Answer: There are some filesystem standards, reading up on FHS or something like that (note that there are different approaches to filesystem layout).
Basically, you put your executable binaries into $prefix/bin/, per-host configuration goes into $prefix/etc/, per-user into user's home directory, arch-independent static data into $prefix/share, arch-dependent data and libraries into $prefix/lib and the mutable data normally goes into /var/lib/.
It's better to have this stuff configurable both at compile-time and runtime. And it's also customary not to write Makefiles with install targets by hand, you may want to look at the autotools suite or similar (a matter of taste).
Comment: It makes more sense to put data into $prefix/share/ and $prefix/lib/. And if there's more than one configuration file: /etc/
Comment: PiedPiper, right, when I refer to `$prefix/lib` and `$prefix/share` I did not imply that the files should necessarily be immediate descendants of the said directories.
Answer: You should look at the File System Hierarchy standard.
In brief, if you program needs system-wide files, then config should go in /etc. Resources, such as scripts and image files go in /usr/share. Runtime data goes in /var/run.
Per-user files live in the user's home directory, of course.

Linux/UNIX install data files


Correcting for a covariate in a Kaplan-Meier curve

Question: I wish to perform a drug survival analysis. Specifically, I wish to see if there are sex differences in drug survival in my dataset. One advantage of my dataset is that it is a large cohort, since fifteen different countries contributed to its creation. However, this also results in statistically significant heterogeneity. One option for me would be to create a KM-curve and stratify by country because of this heterogeneity. However, I was wondering if an option exists to "correct" for countries inside the KM-curve.
Would love to hear if something like this is possible and/or whether this is wise.
Answer: To do an adequate job on the problem requires significant background study in the field of survival analysis.  In particular you should get familiar with the Cox proportional hazards model as a generalization of the Kaplan-Meier approach.  Then you would know how to adjust for sex as a covariate, and how to take regional differences into account.  If outcomes are different across regions, ignoring region would be a mistake.  You have two ways to model variables with the Cox model: as covariates and as stratification factors.
Comment: Thank you for your response. I would like to share this link for anyone who is looking to adjust survival curves using a cox proportional hazard model using R: http://rpkgs.datanovia.com/survminer/reference/ggadjustedcurves.html
Comment: Thanks for the response. Also heard it is possible to create a propensity matched or weighted cohort based on country as a covariate. Then use KM approach. Would your advice be this approach or the Cox proportional hazard approach?
Comment: Weighted estimates are less informative than country-conditional estimates, and propensity scores only need to be considered when you have a very large number of adjustment variables in comparison to the number of outcome events.
Answer: Frank's answer mentions stratification, and that was the route I was pursuing when I suggested you move your earlier closed question to CV.com. As I suggested in a comment you did get an answer from a smarter respondent than I am. If you ran separate models on each country (simple to do in R) you might get a better idea of the distribution of estimates. I would also think that using age as a covariate would be essential. Frank's text "Regression Modeling Strategies" has a wealth of material on dealing with non-linear effects of covariates.
Your question had been how to build a model with weighting in a manner that might recognize the heterogeneity. Here is a stratified model build based on the tiny test set you offered
library(survival)
gender <- as.factor(c("Female", "Male", "Female", "Male", "Male", "Male", "Female", "Female", "Female", "Male"))
country <- as.factor(c("US", "US", "GB", "GB", "GB", "US", "GB", "US", "GB", "US"))
time <- c(5, 10, 12, 15, 20, 9, 14, 18, 24, 20)
event <- c(1, 1, 1, 1, 1, 0, 0, 1, 0, 1)
df <- data.frame(gender, country, time, event)

km.model2
Call:
coxph(formula = Surv(time = time, event = event) ~ gender + strata(country),
    data = df)

              coef exp(coef) se(coef)      z    p
genderMale -0.2321    0.7929   0.8327 -0.279 0.78

Likelihood ratio test=0.08  on 1 df, p=0.7807
km.model2 <- survfit(Surv(time = time, event = event) ~ gender +strata(country), data = df)

The usual assumptions about Cox models with a simple continuous covariate apply, but here two separate models have been constructed and a weighted "average" of their coefficients have been returned. This will not account for all forms of non-proportionality. For testing you might also need to assess whether the effect is constant over time as the model assumes using the cox.zph function. Therneau & Gambsch's text "Modeling Survival Data" has an excellent chapter.


Configuração de seleção por cursor no visual studio code

Question:
Ao segurar o mouse e arrastar o vs code esta selecionando o texto como na imagem abaixo. Fazendo uma coluna de seleção, em vez do padrão que seria linha por linha selecionando todo o texto. Como faço para ele voltar para a configuração padrão?
Answer: Acessar a barra de ferramentas, na parte superior da tela: "Selection" > desativar o "Column Selection Mode"


Does the java date.before function take into account the timezone?

Question: I have the timezone set on the server to BST (British Daylight Savings Time), and the date being passed into the function was calculated in UTC.
On the Java Oracle site, it simply refers to comparing the time as "now", but will this "now" time be the same timezone as set or will it be the one set on the server it is running on?
Answer: There is no timezone in the java.util.Date, it's effectively the timezone of the JVM it's running in.
My advice, if you need to work with timezones, use Joda
Answer: It's implied that java.util.Date always stores time in UTC. For instance if you call System.currentTimeMillis() or create new Date(), internal time will be in UTC. but Date.toString() will give different time for different default timezones.
So if you pass UTC date into before function, you will get correct result regardless of server time zone.


Como criar um grupo de rotas no Blazor com parâmetros?

Question: Estou tentando criar uma estrutura no meu Blazor Client-Side assim:

Pages

Users

Index.razor
Show.razor

Na minha página Index está assim no começo:
@page "/users/"
E na minha página Show está assim:
@page "/users/{id}"
Quando eu navego na rota users funciona normalmente. Mas quando tem parâmetro e está em outra página .razor, não funciona.
Não encontrei nada que me ajudasse.
E só pra constar, essa é a primeira pergunta oficial de uma dúvida sobre Blazor no PT StackOverflow.
Comment: Eu não sei o que eu fiz, talvez tenha reiniciado o server, não sei. Sei que nessa estrutura que postei acima está funcionando agora.
Comment: O que não tem solução, solucionado está.
Answer: Tente criar um parâmetro na página Show.razor
@page "/users/{id:int}"
@page "/users/{id}"

@code {
    [Parameter] public int? id { get; set; }
}

Certifique-se de que você está navegando para a página Show.razor passando o parâmetro "id" corretamente. Você pode fazer isso usando um link ou redirecionando programaticamente.
Por exemplo, você pode criar um link na página Index.razor para navegar para a página Show.razor com um parâmetro "id" assim:
Ir para a página de usuário 123

Agora, na Show.razor, você pode acessar o valor do parâmetro "id" da seguinte forma:
    @if (id.HasValue)
{

O valor do parâmetro "id" é: @id

}
else
{

O parâmetro "id" não foi fornecido na rota.

}

Comment: Como está escrito atualmente, sua resposta não é clara. Por favor, pode [edit] para adicionar mais detalhes, que ajudarão outras pessoas a entender como isso resolve a pergunta feita. Você pode encontrar mais informações sobre como escrever boas respostas [na Central de Ajuda](/help/how-to-answer).
Answer: Tente colocar sua rota no Program.cs, neste exemplo tem duas rotas
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=namedocontroller}/{action=metodo}/{id?}");
//endpoints.MapControllerRoute(
//name: "menu",
//pattern: "{controller=Home}/{action=Index}/{id?}");
});
Comment: Sua resposta pode ser melhorada adicionando informações fundamentais. Basta [edit] para adicionar mais detalhes, como citações ou documentação para que outras pessoas possam confirmar que sua resposta está correta. Você pode encontrar mais informações sobre como escrever boas respostas [na Central de Ajuda](/help/how-to-answer).


Django ORM how to use case when and use other objects value with filter?

Question: so i have 2 models:

label, label models is used to define what is the label of a "type" code example coupon type code 2, has label expired.

class Labels(models.Model):
    label_id= models.AutoField(primary_key=True)
    label_type = models.CharField(null=False, max_length=50)
    label_code= models.IntegerField()
    label_name = models.CharField(max_length=50)

example data:

coupon model used for coupon data

class Coupons(models.Model):
    coupon_id = models.AutoField(primary_key=True, null=False, unique=True)
    coupon_name = models.CharField(max_length=200, null=False)
    coupon_desc= models.CharField(max_length=200, null=False)
    valid_date = models.DateField(null=False)
    status = models.IntegerField(null=False)

example data:

so i want to use Case When from django to filter the data and get status_name example if the coupon date is more than today then it's "expired" else it try to match the Coupons.status with Labels.label_code, here is my current attempt
label_status = Labels.objects.filter(label_type='coupon_status').all()
data = Coupons.objects.
            annotate(
                coupon_status_name=Case(
                    When(status=0, valid_date__lte=date.today(), then=Value(label_status.filter(label_code="2").first().label_name)),
                    default=Value(label_status.filter(label_code=status).first().label_name),
                    output_field=CharField()
                )
            ).all()

i am able to filter the first line on the When() function but i am not able to filter the label objects in the default parameter of case function that reference the coupon dynamically, so how can i do this ? i tried using OuterRef() like
default=Value(label_status.filter(label_code=OuterRef(status)).first().label_name)

but it gives me this error:

This queryset contains a reference to an outer query and may only be used in a subquery.

The expected output is there will be a new dynamic field called coupon_status_name with condition
If coupon valid_date less than today date the value is 'expired'
Else the value will try to match label_code from labels model  and status from coupon model.
Both condition get their value from the label model.
So TLDR; im trying to achive this Postgree SQL with django ORM:
SELECT CASE  WHEN coupons.status = '0' AND coupons.valid_date <= CURRENT_DATE THEN (SELECT label_name FROM labels WHERE labels.label_type= 'coupon_status' AND labels.label_code='2')
ELSE (SELECT label_name FROM labels WHERE labels.label_type= 'coupon_status' AND labels.label_code=coupon.status)
END coupon_status_name

I am actually able to accomplish this using extra like this:
data = Coupons.objects.extra(
    select={
        'coupon_status_name':
        """
            SELECT CASE
                WHEN coupons.status = '0' AND coupons.valid_date <= CURRENT_DATE
                    THEN (SELECT label_name FROM labels WHERE labels.label_type= 'coupon_status' AND labels.label_code='2')
                ELSE (SELECT label_name FROM labels WHERE labels.label_type= 'coupon_status' AND labels.label_code=coupon.status)
                END coupon_status_name
        """,
    }
).all()

But i can't use django .filter() on extra fields and i read on the docs that django is starting to deprecate this extra method, so if anyone can help me convert this to Case() When from django, i would appreaciate it thanks.
Any help is appreaciated, and please don't ask me to change the database structure thank you.
Comment: Can you please add the `Vouchers` and `Labels` models to your question and some minimal examples of input data and desired result
Comment: @IainShelvington i update the post with more detailed information, thank you.
Answer: You try to use unrelated object in a query that ask for relation that's the reason why you received an error. It's seems a bit counter intuitive to me to use a Model like this whereas Django is used to query relational database.
It's seems that Label and Coupon interact to each other. As Label is a kind of Coupon status ?
you should try to add a Foreign Key between the two. It will be a way more easier.
Something like this :
class Coupons(models.Model):
coupon_name = models.CharField(max_length=200, null=False)
coupon_desc= models.CharField(max_length=200, null=False)
valid_date = models.DateField(null=False)
status = models.IntegerField(null=False)
label_status = models.ForeignKey(Label)

Also you shouldn't overwrite primary_key, Django don't really like that, it can cause some errors.
Comment: A label purpose is to translate code like 1,2,3 to their respective name, example coupon_status, page_status, i actually able to acomplish what i want with select() extra from django queryset but the dynamically generated field is not filterable by django .filter(), so what i do is manually filter the field by python list but it is a lot slower
Comment: Well... if it's work but i still think you could improve your database by creating realtion between object or think what are attributes objects should have... Database is the cement of your project, don't hesitate to improve it
Answer: so i found a solution for this using subquery() and OuterRef()
label_status = Labels.objects.filter(label_type='coupon_status').all()
data = Coupons.objects.
            annotate(
                coupon_status_name=Case(
                    When(status=0, valid_date__lte=date.today(), then=Subquery(label_status.filter(label_code="2").values('label_name'))),
                    default=Subquery(label_status.filter(label_code=OuterRef('status').values('label_name')),
                    output_field=CharField()
                )
            ).all()


Let $x=\begin{bmatrix}3\cr4\end{bmatrix}$ and $A=\begin{bmatrix}0&x^T\cr x&0\end{bmatrix}$ is A diagonizable?

Question: I had a problem: let $x=\begin{bmatrix}3\cr4\end{bmatrix}$ and $A=\begin{bmatrix}0&x^T\cr x&0\end{bmatrix}$ is A diagonizable?
But when I plug in the matrix x and its transpose into A the dimensions don't work out and we have empty slots where there should be elements and I was wondering if I'm just tripping or if this problem is unsolvable?
Comment: You should have a symmetric $3\times 3$ matrix. The lower right $0$ is representing a $2\times 2$ block $0$-matrix.
Comment: [WA](https://www.wolframalpha.com/input/?i=JordanDecomposition%5B%7B%7B0%2C+3%2C+4%7D%2C+%7B3%2C+0%2C+0%7D%2C+%7B4%2C+0%2C+0%7D%7D%5D) says diagonalizable with characteristic polynomial $25 \lambda - \lambda^3$.
Comment: In fact, without too much effort we can show that the eigenvalues of this matrix will be $0, \pm \|x\|$. For instance, we could note that $A = BC$, with
$$
B = \pmatrix{x & 0\\0 & 1}, \quad C = \pmatrix{0&1\\x^T & 0},
$$
and $BC$ has the same non-zero eigenvalues as $CB$.
Answer: Presumably the bottom right $0$ is actually a $2 \times 2$ matrix of $0$, i.e.
$$A = \left[\matrix{0 & 3 & 4\cr 3 & 0 & 0\cr 4 & 0 & 0\cr} \right]$$
Comment: And the characteristic polynomial of $A$ is $ch_A(x)=x(x-5)(x+5)$ and therefore $A$ is diagonizable.


How to access the links variable from a template?

Question: I have created a views template for a block and I would like to insert the list of links that normally appears at the bottom of teasers and/or pages (the $links variable) into the template like it is done in page.tpl.php.
However I can't find how to get this information given a node's id.
How do I accomplish this? Is there a Drupal 7 command that accomplishes this?
Answer: I have found the solution to this problem. You need to do the following in your template:
<?php $node = node_load($fields['nid']->raw);
global $language;
node_build_content($node, 'full', $language->language);
if (!empty($node->content['links'])): ?>

      <?php print render($node->content['links']); ?>

<?php endif; ?>


How to compute an angle in specific counter-clockwise direction between vectors

Question: I have one incoming vector and multiple outgoing vectors in 2D.
I need compute an angle in this way: Imagine an incoming vector parallel to the x-axis. Then the angle-value "starts" below the incoming vector and increases counter-clockwise.
Please see the example image:

The incoming vector is $\overrightarrow{AB}$. Outgoing vectors are $\overrightarrow{BC}$, $\overrightarrow{BD}$ and $\overrightarrow{BE}$. By the definition above: $\alpha$ is the smallest angle and $\gamma$ is the biggest angle in this situation.
The incoming vector is $\overrightarrow{FG}$. Outgoing vectors are $\overrightarrow{GH}$ and $\overrightarrow{GJ}$. $\delta <\epsilon$.

I need algorithmicaly select the outgoing vector with the smallest angle (defined above).
How can I compute this specific angle?
Comment: What's "Outcoming"? Shouldn't it be "Outgoing"?
Comment: @SanathDevalapurkar Yes, you are right. I corrected it.
Answer:
Translate the picture so the incoming vector ends at the origin. (Subtract the coordinates of its "tip" from every tip and tail.)
Reverse the tip and tail of the incoming vector so it has its tail at the origin.
Scale each vector by its length, so each has length 1. (Divide each point's coordinates by the length of the vector it's an endpoint of.)
Find the angle to the translated and scaled incoming vector measured counter-clockwise from the origin. Because it's now a unit vector with tail at the origin, its tip is at $(\cos\theta,\sin\theta)$ for a unique angle $0\le\theta<2\pi$.
Rotate everything clockwise around the origin by this angle $\theta$, using the standard rotation transformation taking $(x,y)$ to $(x\cos\theta+y\sin\theta,-x\sin\theta+y\cos\theta)$.
Now the tip of each vector is $(\cos\theta,\sin\theta)$ for a different unique angle $0\le\theta<2\pi$. These are the angles you want.


Training a machine learning model to recognize sentiment in text

Question: I am just getting started with TensorFlow and machine learning. I have watched the following demo video showing how TensorFlow can be used to train a model to recognize sentiment in the text.
In the demo, a dataset containing news headlines, classified as sarcastic or not, is used to train a model. The model is built with a binary_crossentropy loss, with the test data is_sarcastic value set to 1 if the headline is sarcastic and 0 if it is not.
Assuming I want to build a model to detect the "happiness" sentiment in a sentence and have tens of thousands of sentences to build my model with, including:

Today is the best day of my life.

I am feeling good.

I hate my life and want to disappear.

I am doing okay.

I am a little sad.

My dog is 7 years old.

If I am interested in determining the degree of happiness associated with a given sentence, should I classify each of the sentences with a binary value (ex: 1 and 0 - Happy / Not Happy) like in the TensorFlow example, or should I use a more "precise" way to categorize each of the demo sentences, with a "degree" of happiness? For example, 2 is very happy, 1 is happy, 0 is neutral or N/A, -1 is sad, -2 is very sad. For the given sentences, the degree of happiness would be:

Today is the best day of my life (2)

I am feeling good (1)

I hate my life and want to disappear (-2)

I am doing okay (0)

I am a little sad (-1)

My dog is 7 years old (0)

If the first option is used, how should I categorize neutral or N/A sentences (4 and 6) to build my model with?
If the second option is used, would it make sense to have a sparse_categorical_crossentropy loss and multiply the weight of each of the predicted "happiness degree" for a test sentence. For example, the predicted happiness level for the sentence "I am doing great" would be something like: $(0.68 * +2) + (0.24 * +1) + (0.04 * 0) + (0.04 * - 1) + (0.02 * -2) = 1.52,$ which would indicate a very happy feeling.
Answer: This looks more like an ordinal regression problem as it has only a few categories with a numeric relationship between them.
There has been a lot of research in the steam, and one of the implementations is spacecutter ordinal regression.
https://www.ethanrosenthal.com/2018/12/06/spacecutter-ordinal-regression/
Comment: While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference.  Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/265430)
Comment: Thanks for the feedback mdewey. I will consider this approach in the future.


If $f$ is non negative measurable, $ \int f \lt \infty $ if and only if $\sum_{n=-\infty}^{\infty} 2^{n} m{(f\gt 2^n)} \lt\infty $

Question: Let $f$ be non negative measurable. Prove that $ \int f \lt \infty $ if and only if  $\sum_{n=-\infty}^{\infty} 2^{n} m{(f\gt 2^n)} \lt\infty $.
This is a very popular question in Lebesgue integration. I have seen similar type of question so frequently in which sum varies from $0$ to $\infty$. But I do not have valid ideas to prove  the current above one.
Comment: Asked and answered on the site already how many times?
Answer: The sum $s = \sum_{2^n<a} 2^n$ evaluates to $2^n$ where $n$ is the smallest integer such that $2^n\ge a$. Hence, $s/2\le a\le 2s$.
The above observation made, the proof consists of introducing $$g=\sum_{n}2^n \chi_{\{f>2^n\}}$$
and using the inequalities
$$g/2 \le f \le 2g $$


Using webform-form-[nid].tpl.php to change default template

Question: On drupal 7, using webform 7.x-4.11, I am trying to modify the template of a specific webform.
So, in /web/themes/bartik/templates (I am using bartik theme) I created a file named webform-form-23.tpl.php
As "23" is the nid of my webform. But the template is not applying at all...
If a create a file webform-form.tpl.php it is well applying to all my webforms...
This is not a matter of caches as it is well applying when using a file named "webform-form.tpl.php" (with no ID) and despite of that the cache has been cleared at every changes.
Can someone help me on this one?
Answer: Have you made a clear cache in admin/config/development/performance?
Or have you looked if in the template there is a error in the code? To verify this you can try to copy all code of "webform-form.tpl.php" in your "webform-form-23.tpl.php" and at the top of the code, before the first TEST!!! , refresh the cache and look if TEST!!! is displayed. If you display TEST!!! in your previous code there is an error.
Comment: Sure! And the template is changing with no problems with a file with no ID (webform-form.tpl.php)
Comment: @Fumble : good to know, I suggest you ADD that to your actual question also ...
Comment: Have you looked if in the template there is a error in the code? Try to copy all code of "webform-form.tpl.php"  in your "webform-form-23.tpl.php"  and at the top of the code, before the first `TEST!!!` , refresh the cache and look if TEST!!! is displayed.
Comment: obviously it was an issue in the code inside the tpl file, it is working well now... Tank you!


O que são Raw Types?

Question: Lendo e estudando um pouco sobre o Kotlin, encontrei a seguinte afirmativa:

Os Raw Types são um grande problema, mas por motivos de
  compatibilidades eles tiveram que ser mantidos no Java, mas o Kotlin
  por ser uma linguagem nova não possui mais esse problema.

O que são exatamente os Raw Types? E porque eles são um grande problema?
Comment: Quem negativou a pergunta poderia pelo menos explicar o motivo para que eu possa edita-la e tentar colaborar mais com nossa comunidade!
Comment: [Documentação da Oracle](https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html) Um tipo bruto é o nome de uma classe genérica ou interface sem qualquer tipo de argumento.
Answer: O que são exatamente Raw Types?
De acordo com a especificação da linguagem, em tradução livre:

Mais precisamente, um tipo bruto é definido como sendo um dos
  seguintes:

O tipo de referência que é formado tomando o nome de uma declaração de
  tipo genérico sem uma lista de argumento de tipo de acompanhamento.
Um tipo de matriz cujo tipo de elemento é um tipo bruto.
Um tipo de membro não estático de um tipo bruto R que não é herdado de
  uma superclasse ou superinterface de R.

O termo Raw Types ficou conhecido com a introdução do Generics.
Para exemplificar, irei utilizar o Framework Collections:
Quando o Collections foi concebido na versão 1.2 do Java, uma coleção não especificava qual tipo de objeto manipularia, ou seja, a coleção poderia receber qualquer tipo, como no exemplo:
List lista = new ArrayList();
lista.add(1);
lista.add("teste");

Manipular uma coleção desbalanceada como está é custoso, pois quando você tira um elemento da coleção, você deve atribui-lo ao seu respectivo tipo, alem de ser inconveniente, não é seguro.
O fato de não informar um tipo/argumento ao List é conhecido como um Raw Type, ou em tradução livre, um tipo bruto.
Solucionando o Raw Type:
Para resolver esse problema, na versão 1.5 do Java foi introduzido o Generics, com isso, possibilitou-se informar um tipo a coleção, conforme o exemplo:
List<String> lista = new ArrayList<String>();
lista.add("teste_0");
lista.add("teste_1");

Sendo assim, uma vez que o compilador conhece o tipo de elemento da coleção, o compilador pode verificar se você usou a coleção de forma consistente e pode inserir os moldes corretos sobre os valores que estão sendo retirados da coleção.
E porque eles são um grande problema?
De fato, os Raw Types são um grande problema, pois, mesmo com a introdução do Generics não seria conveniente remover os Raw Types, pois as aplicações legadas precisam continuar em funcionamento.
Comment: O único detalhe que noto é que enquanto o Java 5 e 6 exigem `List lista = new ArrayList();` para não dar uma warning do tipo rawtypes, a partir do Java 7 pode-se usar `List lista = new ArrayList<>();`.
Comment: @VictorStafusa , operador diamante é lindeza e alegria, evita muita repetição de nome do tipo

O que são Raw Types?


Why does the $\sum_{n=1}^x \frac {1}{n} \sim \mathrm {ln} x + \frac {1}{2}$

Question: I was playing around with the Harmonic Series and I noticed that: $$\sum_{n=1}^x \frac {1}{n} \sim \mathrm {ln} x + \frac {1}{2}$$

I wanted to know if this is just some coincidence or if it is caused by something deeper. Thanks in advance
Comment: I can say more digits: $0.577\ldots$) See [wiki](https://en.wikipedia.org/wiki/Euler–Mascheroni_constant)
Comment: Yes, as per the above comment, the your 0.5 is should be the Euler Mascheroni constant.
Comment: Yes, no "coincidences" in math, summation is a discrete version of the integral, integral of $1/x$ is $\ln x$ and the Euler-Mascheroni constant is in fact defined to be the difference between these values. The connection between discrete and continuous is quite interesting and deep - read more also about the Euler-MacLaurin formula, Riemann Zeta function and summability calculus.
Answer: $$\sum_{k=2}^{n}\frac1k <\int_1^n\frac {dt}t<\sum_{k=1}^{n-1}\frac1k$$
Then, if $H_n=\sum_{k=1}^n\frac1k$ we have
$$\frac1n<H_n-\ln n<1$$
Comment: I don't see how this answers the question...
Comment: Well, noting that we're using the notation very loosely, no it doesn't even show that. It shows that $H_n-\log(n)$ is between $0$ and $1$, but it doesn't show that $H_n-\log(n)$ has a limit, which the other answers do. Including the one that got two downvotes - I'm curious about that...
Comment: @DavidC.Ullrich Me neither. Doesn't it just shows that $H_n \approx \ln n + C$, with $C$ between $0$ and $1$?
Answer: Well, I see an answer that works something out that doesn't seem to me to quite answer the question, and another answer that answers the question with no proof (yes, there's a link). And of course several more now that I've typed this...
Here's the story:
Say $R_n$ is the rectangle in the plane with base equal to the interval $[n,n+1]$ on the $x$-axis and height $1/n$. In other words, $R_n=[n,n+1]\times[0,1/n]$. So the area of $R_n$ is exactly $1/n$. Now split $R_n$ into two pieces $$R_n=L_n\cup E_n,$$where $L_n$ is the part of $R_n$ lying below the curve $y=1/x$ and $E_n$ is the part above the curve. So the area of $L_n$ is $\int_n^{n+1}\frac{dt}t,$. Say $\delta_n$ is the area of $E_n$. So we have $$\frac1n=\delta_n+\int_n^{n+1}\frac{dt}t.$$Add those equations and you get $$H_n=\log(n+1)+\sum_{j=1}^n\delta_j.$$Subtract $\log(n)$ from both sides: $$H_n-\log(n)=\log\left(1+\frac1n\right)+\sum_{j=1}^n\delta_j.$$So we get $$\lim_{n\to\infty}(H_n-\log(n))=\sum_{j=1}^\infty\delta_j.$$Now the question is why that sum converges.
Look at the picture and you see that in fact $$E_j\subset[j,j+1]\times[1/(j+1),1/j].$$So since $\delta_j$ is the area of $E_j$ we have $$0\le\delta_j\le\frac1j-\frac1{j+1}.$$ So the sum converges; the value of the sum is known as $\gamma$, and so $$H_n-\log(n)\to\gamma\quad(n\to\infty).$$
Comment: This is curious. More upvotes than any of the other answers except one, and also the only two downvotes. Someone explain this...
Answer: As you know, the logarithm is the antiderivative of the inverse function, and it is no surprise that the sum is well approximated by the integral.
More generally, a precise relation between a sum and integral is established by the Euler-Maclaurin formula.
More terms are
$$H_n=\ln(n)+\gamma+\frac1{2n}-\frac1{12n^2}+\frac1{120n^4}-\frac1{252n^6}\cdots$$
Answer: We have:
$$ H_N-\log(N+1) = -\int_{1}^{N+1}\frac{dx}{x}+\sum_{n=1}^{N}\frac{1}{n} = \int_{1}^{N+1}\left(\frac{1}{\lfloor x\rfloor}-\frac{1}{x}\right)\,dx $$
but the last integral is clearly convergent to some constant as $N\to +\infty$, since:
$$\frac{\{x\}}{x\lfloor x\rfloor}=O\left(\frac{1}{x^2}\right).$$
Answer: Since $\displaystyle\sum_{k=1}^n\frac{1}{k}=\frac{1}{n}+\int_1^n\frac{1}{\lfloor x\rfloor}dx$ and $\displaystyle\ln(n)=\int_1^n\frac{1}{x}dx$ we conclude
$$\begin{array}{ll} \displaystyle H_n-\ln(n)-\frac{1}{n} & \displaystyle =\int_1^n\left(\frac{1}{\lfloor x\rfloor}-\frac{1}{x}\right)dx \\ & \displaystyle =\int_1^n\frac{x-\lfloor x\rfloor}{\lfloor x\rfloor x}dx \\ & \displaystyle \le \int_1^n\frac{1}{\lfloor x\rfloor^2}dx \\ & \displaystyle = \int_1^2\frac{1}{\lfloor x\rfloor^2}dx+\int_2^n\frac{1}{\lfloor x\rfloor^2}dx \\ & \displaystyle \le 1+\int_1^\infty\frac{1}{x^2}dx \\ & = 2. \end{array} $$
Since $H_n-\ln(n)-\frac{1}{n}$ is increasing (the integrand in the first equation is nonnegative) and bounded, it has some limit. This implies $\lim\limits_{n\to\infty}\left[H_n-\ln(n)\right]$ exists.
The constant is not $\frac{1}{2}$, though. It's known as the Euler-Mascheroni constant, about $\gamma\approx 0.577\dots$
Answer: By partial summation $$\sum_{n=1}^{N}\frac{1}{n}=1+\int_{1}^{N}\frac{\left\lfloor t\right\rfloor }{t^{2}}dt=1+\log\left(N\right)-\int_{1}^{N}\frac{\left\{ t\right\} }{t^{2}}dt
 $$ where $\left\lfloor t\right\rfloor
 $ is the integer part of $t
 $ and $\left\{ t\right\}
 $ is the fractional part. The last integral is convergent because $$\int_{1}^{N}\frac{\left\{ t\right\} }{t^{2}}dt\leq \int_{1}^{N}\frac{1}{t^{2}}dt\leq1 .
 $$


what R function fits a smoothing spline regression model with correlated errors?

Question: I want to fit a smoothing spline regression model with correlated errors (it's a time series) using R.  All I could find is function ssr, from library assist, which is not on CRAN anymore -- I am pretty lukewarm about installing a package from source when it does not appear it's maintained.  So I am looking for an alternative, and I would be obliged for any suggestion.
BTW, my model is meant to look like something like
raw_measurement = parameter_estimate + {time of day spilne} + {other splines} + {AR(1) distributed error}

Comment: Yes, it's a pity that `assist` is no longer maintained, I used that package myself.
Answer: You might be able to cajole your problem to fit into gam routines provided in package mcgv, because one of the smoothers is for Markov Random Fields (MRF).  Although it appears that it is designed for geographical regions, your error still have markov structure with an AR(1) process.  Try
library(mcgv)
?mrf

and see if that might not work for you.


Replace and overwrite instead of appending

Question: I have the following code:
import re
#open the xml file for reading:
file = open('path/test.xml','r+')
#convert to string:
data = file.read()
file.write(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>",r"<xyz>ABC</xyz>\1<xyz>\2</xyz>",data))
file.close()

where I'd like to replace the old content that's in the file with the new content. However, when I execute my code, the file "test.xml" is appended, i.e. I have the old content followed by the new "replaced" content. What can I do in order to delete the old stuff and only keep the new?
Comment: http://stackoverflow.com/questions/2424000/read-and-overwrite-a-file-in-python
Comment: When you say *"replace the old content that's in the file with the new content"*, you need to read in and transform the current contents `data = file.read()`. You don't mean "blindly overwrite it without needing to read it first".
Answer: You need seek to the beginning of the file before writing and then use file.truncate() if you want to do inplace replace:
import re

myfile = "path/test.xml"

with open(myfile, "r+") as f:
    data = f.read()
    f.seek(0)
    f.write(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>", r"<xyz>ABC</xyz>\1<xyz>\2</xyz>", data))
    f.truncate()

The other way is to read the file then open it again with open(myfile, 'w'):
with open(myfile, "r") as f:
    data = f.read()

with open(myfile, "w") as f:
    f.write(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>", r"<xyz>ABC</xyz>\1<xyz>\2</xyz>", data))

Neither truncate nor open(..., 'w') will change the inode number of the file (I tested twice, once with Ubuntu 12.04 NFS and once with ext4).
By the way, this is not really related to Python. The interpreter calls the corresponding low level API. The method truncate() works the same in the C programming language: See http://man7.org/linux/man-pages/man2/truncate.2.html
Comment: is there a drawback of using the "f.seek() ..." approach over the "with open( ...)" approach?
Comment: `Neither truncate nor open(..., 'w') will change the inode number of the file` why is it important?
Comment: @rok if the inode changes or not is not relevant in most cases. Only in edge cases where you use hard-links, but [I advice to avoid hard links](https://github.com/guettli/programming-guidelines/blob/master/README.md#avoid-magic-or-uncommon-things).
Comment: @Frankstar I think it's even the other way around as you don't create two file objects but only one with the `f.seek()` approach, otherwise both behave the same, I think.
Answer: file='path/test.xml'
with open(file, 'w') as filetowrite:
    filetowrite.write('new content')

Open the file in 'w' mode, you will be able to replace its current text save the file with new contents.
Comment: This is a good way to clear a file and write something new to it, but the question was about reading the file, modifying the contents and overwriting the original with the new contents.
Comment: @Boris, what is the problem with reading the file first and then using the code in this answer?
Comment: it's simple and efficient, does the job in a perfect way.
Comment: @Rayhunter : it's inefficient
Answer: Using truncate(), the solution could be
import re
#open the xml file for reading:
with open('path/test.xml','r+') as f:
    #convert to string:
    data = f.read()
    f.seek(0)
    f.write(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>",r"<xyz>ABC</xyz>\1<xyz>\2</xyz>",data))
    f.truncate()

Comment: `seek` *and* `truncate`!!! I couldn't figure out why `seek` alone was not working.
Comment: @Almabud, I just tested `with open(...) as f: f.truncate() f.write(...)` (no `seek(0)`) and it does indeed seem to replace the file contents.
Comment: @conner.xyz Maybe I am wrong but seek is responsible to change the **cursor** position. And `write` is responsible for writing into the file from the **cursor** position. **write** doesn't care about after writing a file is there any content remain or not. `truncate` here do the job to remove the rest of the content from the **cursor** position of the file.
Comment: @conner.xyz recently I was trying your solution as it is simpler but it wasn't working as expected.
`
file =
pyexiv_img = pyexiv2.ImageData(file.read())
pyexiv_img.clear_exif()
file.truncate()
file.write(pyexiv_img.get_bytes())
`
This is not working. I need to add `file.seek(0)` after then it works fine.
Answer: import os#must import this library
if os.path.exists('TwitterDB.csv'):
        os.remove('TwitterDB.csv') #this deletes the file
else:
        print("The file does not exist")#add this to prevent errors

I had a similar problem, and instead of overwriting my existing file using the different 'modes', I just deleted the file before using it again, so that it would be as if I was appending to a new file on each run of my code.
Answer: See from How to Replace String in File works in a simple way and is an answer that works with replace
fin = open("data.txt", "rt")
fout = open("out.txt", "wt")

for line in fin:
    fout.write(line.replace('pyton', 'python'))

fin.close()
fout.close()

Answer: in my case the following code did the trick
with open("output.json", "w+") as outfile:  #using w+ mode to create file if it not exists. and overwrite the existing content
    json.dump(result_plot, outfile)

Answer: Using python3 pathlib library:
import re
from pathlib import Path
import shutil

shutil.copy2("/tmp/test.xml", "/tmp/test.xml.bak") # create backup
filepath = Path("/tmp/test.xml")
content = filepath.read_text()
filepath.write_text(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>",r"<xyz>ABC</xyz>\1<xyz>\2</xyz>", content))

Similar method using different approach to backups:
from pathlib import Path

filepath = Path("/tmp/test.xml")
filepath.rename(filepath.with_suffix('.bak')) # different approach to backups
content = filepath.read_text()
filepath.write_text(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>",r"<xyz>ABC</xyz>\1<xyz>\2</xyz>", content))


c fscanf for ints causes segfault

Question: DISCLOSURE: This is homework.
The code below is meant to read a command file formatted like:
ADD 6 6 5
ADDTERM 0 1 1
MULTIPLY 2 40
and call the appropriate command using the given parameters.  For some reason, while ADD and ADDTERM work as expected, I get a segfault when the multiply line is read.
    int arg_1 = 0, arg_2 = 0, arg_3 = 0;

    while(fscanf(commands, "%s", command) != EOF)
    {
            if(strcmp(command, "ADDTERM") == 0)
            {
                    /*The following line runs fine!*/
                    fscanf(commands, "%d %d %d",
                                    &arg_1, &arg_2, &arg_3);
                    printf("ADDTERM, Poly: %d, Coeff: %d, Exp: %d\n",
                                    arg_1, arg_2, arg_3);
                    if(polys[arg_1] == NULL)
                    {polys[arg_1] = CreatePolynomial();}
                    AddTermToPoly(polys[arg_1], arg_2, arg_3);
            }

            else if(strcmp(command, "MULTIPLY") == 0)
            {
                    /*The following line results in a segfault*/
                    fscanf(commands, "%d %d", &arg_1, &arg_2);

                    printf("MULTIPLY, Poly: %d, Multiplier: %d\n",
                                    arg_1, arg_2);
                    MultiplyPoly(polys[arg_1], arg_2);
            }

            /*...*/
   }

This is also only a problem on my school's Linux server.  My home computer runs it just fine, using "gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)", whereas my school uses "gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)"
Any ideas why this would be?  Your help is appreciated!
Comment: What is the size of the string "command"? You may have forgotten to leave a space for '\0'
Comment: What is the exact value of the `commands` string when the code runs?
Comment: commands isn't a string, but a *FILE.  Is that not how it's supposed to be?
Comment: it always helps if you can post the minimal program that will enable others to reproduce your error.
Comment: it's possible that something earlier in the program is corrupting memory, have you tried rerunning with a file that only contains the multiply line?
Comment: jonsca, that was it. I can't believe I forgot the null! I'm utterly baffled as to why this code worked properly on my home computer though.  Do different compilers handle whitespace differently?
Comment: @user373374: There's a reason it's called "undefined behavior" :)
Comment: @user373374: where exactly your code get crashed? are you sure that the `polys` array is large enough?
Answer: You need to leave room for a null terminus '\0' in your string.  You were okay with ADDTERM because it has 1 less letter than MULTIPLY.

c fscanf for ints causes segfault


Query no YII 2 obtendo resultado diferente da clausula Where

Question: Estou realizando uma consulta, onde contém uma cláusula where onde a flag Status é que deve ser aplicada durante a execução.
Porém o resultado sempre vem todos, ignorando minha cláusula.
<code>Veiculo::find()->select([Veiculo::tableName() . '.id', "CONCAT(descricao, ' - ', placa) AS text"])
            ->innerJoin(ModeloVeiculo::tableName(), ModeloVeiculo::tableName() . '.id = ' . Veiculo::tableName() . '.fk_modelo_veiculo')
            ->where(['<>',Veiculo::tableName() . '.status' ,  4])
            ->andWhere(['like', 'placa', $q])
            ->orWhere(['like', 'descricao', $q])->funcaoFilter()->asArray()->all();
</code>

->where(['!=',Veiculo::tableName() . '.status' ,  4]) // Flag para filtrar veiculos com status diferente do tipo 4
Comment: Eu imagino que o código seja PHP, mas, para evitar problemas, seria bom você [edit] o post e adicionar a tag da linguagem.
Answer: Reorganize seus where.
<code>  ->where(['like', 'placa', $q])
            ->orWhere(['like', 'descricao', $q])
            ->andWhere(['<>' ,Veiculo::tableName() .'.status' ,  4])->funcaoFilter()->asArray()->all();
</code>
Havia um erro no momento de checar. O orWhere, estava sobrescrevendo sua clausula, fazendo que o resultado da consulta checassem apenas o OR.


Semidirect product and group action

Question: I want to understand the following lemma:
Let $G$ be a finite group satisfying $G = P \rtimes F$, where $P$ is a cyclic $p$-group for some prime $p$, $|F| > 1$ and $(p, |F|) = 1$. Then each element of $F$ acts on $P$ either trivially or fixed-point-freely.
In the article "An exact upper bound for sums of element orders in
non-cyclic finite groups" (Marcel Herzog, Patrizia Longobardi, Mercede Maj)  the authors show as follows:

This is the result mentioned above:

But I don't understand the connection between the two results.
Thanks in advance for any help.
Answer: Mercede Maj answered me. She said that the right reference is the Theorem 2.4 (in the section 5, Finite Groups - Daniel Gorenstein).

Semidirect product and group action


Como ler um arquivo .Json pelo Delphi

Question: Estou com dificuldade em ler um arquivo .Json pelo Delphi.
a minha intenção é ler e colara seu conteúdo no banco de dados.
já visualizei alguns links mais nem um atendeu ao que eu queria.
Seque o conteúdo do .Json. Todos os dados aqui são frutos de um gerador de um fake name generator sendo todos fictícios.
<code>[
    {
        "nome": "Mariana Isadora Carvalho",
        "idade": 26,
        "cpf": "12956405543",
        "rg": "212055148",
        "data_nasc": "06\/02\/1993",
        "signo": "Aquário",
        "mae": "Ana Aline Esther",
        "pai": "Mateus Eduardo Lucas Carvalho",
        "email": "email@example.com",
        "senha": "ohaNN4OuyC",
        "cep": "44075169",
        "endereco": "Rua Índia",
        "numero": 694,
        "bairro": "Ponto Central",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7528936096",
        "celular": "75992503534",
        "altura": "1,59",
        "peso": 45,
        "tipo_sanguineo": "AB-",
        "cor": "verde"
    },
    {
        "nome": "Bruno Márcio Vicente Pires",
        "idade": 62,
        "cpf": "56006359588",
        "rg": "432665389",
        "data_nasc": "18\/07\/1957",
        "signo": "Câncer",
        "mae": "Flávia Sônia Ana",
        "pai": "Daniel Murilo Iago Pires",
        "email": "email@example.com",
        "senha": "sQSR7UXnq8",
        "cep": "44006162",
        "endereco": "Caminho Q - XXI",
        "numero": 304,
        "bairro": "Muchila",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7528175977",
        "celular": "75981475744",
        "altura": "1,92",
        "peso": 109,
        "tipo_sanguineo": "A-",
        "cor": "azul"
    },
    {
        "nome": "Simone Tânia Figueiredo",
        "idade": 72,
        "cpf": "28366831558",
        "rg": "231064068",
        "data_nasc": "05\/12\/1947",
        "signo": "Sagitário",
        "mae": "Rafaela Larissa Nair",
        "pai": "Cláudio Bernardo Figueiredo",
        "email": "email@example.com",
        "senha": "RjcfZuHBnf",
        "cep": "44089188",
        "endereco": "Caminho 17",
        "numero": 676,
        "bairro": "Brasília",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7526970676",
        "celular": "75989897771",
        "altura": "1,78",
        "peso": 67,
        "tipo_sanguineo": "AB+",
        "cor": "laranja"
    },
    {
        "nome": "Emanuelly Regina Cecília da Paz",
        "idade": 55,
        "cpf": "54543221567",
        "rg": "285107148",
        "data_nasc": "19\/11\/1964",
        "signo": "Escorpião",
        "mae": "Sara Raimunda Julia",
        "pai": "Murilo Tomás da Paz",
        "email": "emanuellyreginaceciliadapaz..email@example.com",
        "senha": "rsnRKJsha6",
        "cep": "44035320",
        "endereco": "Caminho 18",
        "numero": 915,
        "bairro": "Campo Limpo",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7525611263",
        "celular": "75983883740",
        "altura": "1,59",
        "peso": 87,
        "tipo_sanguineo": "B-",
        "cor": "roxo"
    },
    {
        "nome": "Maria Amanda Marina Santos",
        "idade": 45,
        "cpf": "99588863597",
        "rg": "483480502",
        "data_nasc": "04\/05\/1974",
        "signo": "Touro",
        "mae": "Márcia Luna Aparecida",
        "pai": "César Renato Santos",
        "email": "firstname.lastname@example.com",
        "senha": "B6E96l0ycd",
        "cep": "44089144",
        "endereco": "Caminho 16",
        "numero": 613,
        "bairro": "Brasília",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7536737134",
        "celular": "75987872328",
        "altura": "1,76",
        "peso": 75,
        "tipo_sanguineo": "A-",
        "cor": "preto"
    },
    {
        "nome": "Cláudio Miguel Nicolas da Costa",
        "idade": 61,
        "cpf": "68846188500",
        "rg": "478358246",
        "data_nasc": "09\/04\/1958",
        "signo": "Áries",
        "mae": "Teresinha Tânia",
        "pai": "Lucca Francisco Lorenzo da Costa",
        "email": "email@example.com",
        "senha": "b6aCySfcZS",
        "cep": "44024216",
        "endereco": "Rua Juazeiro",
        "numero": 994,
        "bairro": "Jardim Cruzeiro",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7538665016",
        "celular": "75989844202",
        "altura": "1,94",
        "peso": 60,
        "tipo_sanguineo": "A-",
        "cor": "azul"
    },
    {
        "nome": "Letícia Alessandra Josefa Gonçalves",
        "idade": 76,
        "cpf": "14808239531",
        "rg": "497986814",
        "data_nasc": "11\/10\/1943",
        "signo": "Libra",
        "mae": "Eduarda Fabiana",
        "pai": "Pietro Benedito Gonçalves",
        "email": "email@example.com",
        "senha": "MrCPiRjezT",
        "cep": "44065566",
        "endereco": "Rua Patu",
        "numero": 346,
        "bairro": "Conceição",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7525664076",
        "celular": "75987524022",
        "altura": "1,85",
        "peso": 67,
        "tipo_sanguineo": "A+",
        "cor": "vermelho"
    },
    {
        "nome": "Vicente Edson de Paula",
        "idade": 65,
        "cpf": "21584986506",
        "rg": "180703833",
        "data_nasc": "26\/05\/1954",
        "signo": "Gêmeos",
        "mae": "Marcela Luciana Cecília",
        "pai": "Juan Pietro de Paula",
        "email": "email@example.com",
        "senha": "qun83MxRKo",
        "cep": "44088300",
        "endereco": "Rua Almirante Tamandaré",
        "numero": 325,
        "bairro": "Brasília",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7529594328",
        "celular": "75984827098",
        "altura": "1,91",
        "peso": 90,
        "tipo_sanguineo": "B+",
        "cor": "amarelo"
    },
    {
        "nome": "Davi Gustavo Emanuel Brito",
        "idade": 29,
        "cpf": "57539349514",
        "rg": "420785851",
        "data_nasc": "08\/04\/1990",
        "signo": "Áries",
        "mae": "Stella Eloá",
        "pai": "Gabriel Renan Brito",
        "email": "firstname.lastname@example.com",
        "senha": "opBBnWajgg",
        "cep": "44090520",
        "endereco": "Rua Tabapuã",
        "numero": 103,
        "bairro": "Tomba",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7539158521",
        "celular": "75983442194",
        "altura": "1,82",
        "peso": 93,
        "tipo_sanguineo": "O+",
        "cor": "laranja"
    },
    {
        "nome": "Analu Isis Freitas",
        "idade": 50,
        "cpf": "73517836591",
        "rg": "239773512",
        "data_nasc": "05\/01\/1969",
        "signo": "Capricórnio",
        "mae": "Rosa Jaqueline",
        "pai": "Raul Giovanni Freitas",
        "email": "email@example.com",
        "senha": "9dSsx33euM",
        "cep": "44033349",
        "endereco": "Rua Campo Belo",
        "numero": 414,
        "bairro": "Campo Limpo",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7539745298",
        "celular": "75995780430",
        "altura": "1,53",
        "peso": 70,
        "tipo_sanguineo": "AB+",
        "cor": "azul"
    },
    {
        "nome": "Pedro Henrique Felipe Rodrigo Lima",
        "idade": 26,
        "cpf": "99068101579",
        "rg": "170376758",
        "data_nasc": "02\/09\/1993",
        "signo": "Virgem",
        "mae": "Luiza Ayla",
        "pai": "Rodrigo Ruan Kaique Lima",
        "email": "pedrohenriquefeliperodrigolima..email@example.com",
        "senha": "KIUEDbq2js",
        "cep": "44092684",
        "endereco": "Rua Salmo 29",
        "numero": 535,
        "bairro": "Tomba",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7537369181",
        "celular": "75981661177",
        "altura": "1,75",
        "peso": 53,
        "tipo_sanguineo": "AB-",
        "cor": "preto"
    },
    {
        "nome": "Martin Alexandre Aparício",
        "idade": 56,
        "cpf": "29085293510",
        "rg": "101685099",
        "data_nasc": "17\/05\/1963",
        "signo": "Touro",
        "mae": "Valentina Betina Sandra",
        "pai": "Raul Giovanni Aparício",
        "email": "firstname.lastname@example.com",
        "senha": "jqH0nIrNWr",
        "cep": "44053286",
        "endereco": "Caminho Nazaré",
        "numero": 430,
        "bairro": "Cidade Nova",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7527894365",
        "celular": "75984239491",
        "altura": "1,90",
        "peso": 58,
        "tipo_sanguineo": "A+",
        "cor": "preto"
    },
    {
        "nome": "Sarah Simone Duarte",
        "idade": 53,
        "cpf": "01841232599",
        "rg": "450643542",
        "data_nasc": "11\/01\/1966",
        "signo": "Capricórnio",
        "mae": "Bianca Natália Laura",
        "pai": "Benjamin Lorenzo Duarte",
        "email": "firstname.lastname@example.com",
        "senha": "e3CiC38JLU",
        "cep": "44053450",
        "endereco": "Caminho Ilhéus",
        "numero": 743,
        "bairro": "Cidade Nova",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7537498152",
        "celular": "75996913514",
        "altura": "1,83",
        "peso": 50,
        "tipo_sanguineo": "O+",
        "cor": "roxo"
    },
    {
        "nome": "Igor Benedito Rezende",
        "idade": 31,
        "cpf": "33134585561",
        "rg": "473634867",
        "data_nasc": "22\/12\/1988",
        "signo": "Capricórnio",
        "mae": "Agatha Daniela Analu",
        "pai": "Miguel Leandro Rezende",
        "email": "igorbeneditorezende..email@example.com",
        "senha": "MbUiwg3MBw",
        "cep": "44002068",
        "endereco": "Rua Joaquim Pitombo",
        "numero": 965,
        "bairro": "Centro",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7537886021",
        "celular": "75986897117",
        "altura": "1,73",
        "peso": 56,
        "tipo_sanguineo": "B+",
        "cor": "azul"
    },
    {
        "nome": "Emanuelly Maria Cecília Assis",
        "idade": 66,
        "cpf": "69748459527",
        "rg": "208004014",
        "data_nasc": "10\/10\/1953",
        "signo": "Libra",
        "mae": "Débora Stella",
        "pai": "Kauê Igor Rodrigo Assis",
        "email": "firstname.lastname@example.com",
        "senha": "zmPciB5Zfg",
        "cep": "44028132",
        "endereco": "Rua Pedra Sobre Pedra",
        "numero": 637,
        "bairro": "Gabriela",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7525123101",
        "celular": "75982401371",
        "altura": "1,79",
        "peso": 69,
        "tipo_sanguineo": "O+",
        "cor": "verde"
    },
    {
        "nome": "Simone Agatha Mirella da Silva",
        "idade": 49,
        "cpf": "47097225521",
        "rg": "250982109",
        "data_nasc": "18\/08\/1970",
        "signo": "Leão",
        "mae": "Teresinha Laura",
        "pai": "Julio Carlos Gustavo da Silva",
        "email": "firstname.lastname@example.com",
        "senha": "he2CP7ADNR",
        "cep": "44052163",
        "endereco": "Praça da Serra",
        "numero": 681,
        "bairro": "CASEB",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7538641105",
        "celular": "75993339289",
        "altura": "1,73",
        "peso": 59,
        "tipo_sanguineo": "O+",
        "cor": "roxo"
    },
    {
        "nome": "Mariah Jaqueline Silva",
        "idade": 76,
        "cpf": "30764782576",
        "rg": "364397354",
        "data_nasc": "11\/01\/1943",
        "signo": "Capricórnio",
        "mae": "Márcia Vera Antonella",
        "pai": "Nicolas Kauê Carlos Silva",
        "email": "mariahjaquelinesilva..firstname.lastname@example.com",
        "senha": "An5WsnBHX4",
        "cep": "44006050",
        "endereco": "Caminho H - XXV",
        "numero": 574,
        "bairro": "Muchila",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7525440777",
        "celular": "75982560255",
        "altura": "1,54",
        "peso": 82,
        "tipo_sanguineo": "A+",
        "cor": "verde"
    },
    {
        "nome": "Raimundo Pedro Moreira",
        "idade": 20,
        "cpf": "80247362506",
        "rg": "420456107",
        "data_nasc": "05\/11\/1999",
        "signo": "Escorpião",
        "mae": "Gabrielly Alana",
        "pai": "Cláudio Anderson Moreira",
        "email": "email@example.com",
        "senha": "XT1zBNePFK",
        "cep": "44089138",
        "endereco": "Caminho 36",
        "numero": 493,
        "bairro": "Brasília",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7528891832",
        "celular": "75982344425",
        "altura": "1,91",
        "peso": 60,
        "tipo_sanguineo": "O+",
        "cor": "azul"
    },
    {
        "nome": "Manuel Daniel dos Santos",
        "idade": 26,
        "cpf": "84718261511",
        "rg": "420334282",
        "data_nasc": "20\/08\/1993",
        "signo": "Leão",
        "mae": "Catarina Camila",
        "pai": "Bernardo Samuel dos Santos",
        "email": "manueldanieldossantos..firstname.lastname@example.com",
        "senha": "sT0itjslxX",
        "cep": "44003186",
        "endereco": "Rua Presidente Lincoln",
        "numero": 697,
        "bairro": "Serraria Brasil",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7537837958",
        "celular": "75997610132",
        "altura": "2,00",
        "peso": 87,
        "tipo_sanguineo": "A-",
        "cor": "roxo"
    },
    {
        "nome": "Cláudia Hadassa Alícia Carvalho",
        "idade": 26,
        "cpf": "10981995535",
        "rg": "186568551",
        "data_nasc": "07\/10\/1993",
        "signo": "Libra",
        "mae": "Silvana Emanuelly Betina",
        "pai": "Geraldo Cláudio Mateus Carvalho",
        "email": "email@example.com",
        "senha": "9gxKgq8rm3",
        "cep": "44094516",
        "endereco": "Rua Torneira",
        "numero": 139,
        "bairro": "35º BI",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7527692439",
        "celular": "75997020636",
        "altura": "1,54",
        "peso": 59,
        "tipo_sanguineo": "A+",
        "cor": "laranja"
    },
    {
        "nome": "Lorena Aparecida Agatha Cardoso",
        "idade": 48,
        "cpf": "88791755565",
        "rg": "265730399",
        "data_nasc": "02\/07\/1971",
        "signo": "Câncer",
        "mae": "Jaqueline Emilly Antonella",
        "pai": "Luiz Thiago Osvaldo Cardoso",
        "email": "email@example.com",
        "senha": "LhoBaZ6hKg",
        "cep": "44013270",
        "endereco": "Via Pedestre 7",
        "numero": 270,
        "bairro": "CIS",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7537872565",
        "celular": "75982619266",
        "altura": "1,81",
        "peso": 86,
        "tipo_sanguineo": "A+",
        "cor": "vermelho"
    },
    {
        "nome": "Luzia Jaqueline Isabela Cardoso",
        "idade": 58,
        "cpf": "44206232504",
        "rg": "445488207",
        "data_nasc": "17\/01\/1961",
        "signo": "Capricórnio",
        "mae": "Nina Josefa Maria",
        "pai": "Benedito Benício Severino Cardoso",
        "email": "email@example.com",
        "senha": "iNnAq6DTJZ",
        "cep": "44078150",
        "endereco": "Rua São Miguel do Sul",
        "numero": 555,
        "bairro": "Santa Mônica",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7536957008",
        "celular": "75992949604",
        "altura": "1,55",
        "peso": 88,
        "tipo_sanguineo": "B-",
        "cor": "vermelho"
    },
    {
        "nome": "Melissa Adriana Assunção",
        "idade": 45,
        "cpf": "32126187527",
        "rg": "364499394",
        "data_nasc": "13\/07\/1974",
        "signo": "Câncer",
        "mae": "Helena Ayla Brenda",
        "pai": "Kaique Ruan Assunção",
        "email": "email@example.com",
        "senha": "F4hYHtou3h",
        "cep": "44066288",
        "endereco": "Travessa Boa Esperança",
        "numero": 367,
        "bairro": "Conceição",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7528259495",
        "celular": "75988994634",
        "altura": "1,69",
        "peso": 65,
        "tipo_sanguineo": "B-",
        "cor": "vermelho"
    },
    {
        "nome": "Martin Matheus Rodrigo da Paz",
        "idade": 22,
        "cpf": "59758112520",
        "rg": "362671266",
        "data_nasc": "19\/10\/1997",
        "signo": "Libra",
        "mae": "Milena Maria Lara",
        "pai": "Anthony João da Paz",
        "email": "email@example.com",
        "senha": "5sLdm79cIJ",
        "cep": "44050084",
        "endereco": "Rua Carlos Valadares",
        "numero": 265,
        "bairro": "Queimadinha",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7536930884",
        "celular": "75988813922",
        "altura": "1,79",
        "peso": 110,
        "tipo_sanguineo": "O+",
        "cor": "roxo"
    },
    {
        "nome": "Nathan Márcio Galvão",
        "idade": 74,
        "cpf": "50142434523",
        "rg": "497678172",
        "data_nasc": "09\/07\/1945",
        "signo": "Câncer",
        "mae": "Clarice Flávia",
        "pai": "Joaquim Antonio Galvão",
        "email": "firstname.lastname@example.com",
        "senha": "ZdT1pxNIjg",
        "cep": "44056518",
        "endereco": "Rua Japurá",
        "numero": 924,
        "bairro": "Mangabeira",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7536099837",
        "celular": "75995878841",
        "altura": "1,88",
        "peso": 53,
        "tipo_sanguineo": "A-",
        "cor": "verde"
    },
    {
        "nome": "Filipe Igor Breno Almeida",
        "idade": 49,
        "cpf": "11799683524",
        "rg": "432668299",
        "data_nasc": "23\/10\/1970",
        "signo": "Escorpião",
        "mae": "Clarice Lorena Patrícia",
        "pai": "Bryan Sebastião Almeida",
        "email": "email@example.com",
        "senha": "6UhGhUEd95",
        "cep": "44023054",
        "endereco": "Rua Ubatã",
        "numero": 225,
        "bairro": "Rua Nova",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7527936589",
        "celular": "75982142592",
        "altura": "1,74",
        "peso": 73,
        "tipo_sanguineo": "AB-",
        "cor": "azul"
    },
    {
        "nome": "Otávio José Thiago Nunes",
        "idade": 74,
        "cpf": "84799949594",
        "rg": "154312617",
        "data_nasc": "10\/12\/1945",
        "signo": "Sagitário",
        "mae": "Fernanda Heloise Marcela",
        "pai": "Anthony Paulo André Nunes",
        "email": "otaviojosethiagonunes..firstname.lastname@example.com",
        "senha": "uEOjg6fc19",
        "cep": "44044377",
        "endereco": "Travessa Valter Carneiro",
        "numero": 711,
        "bairro": "Asa Branca",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7539062051",
        "celular": "75997770966",
        "altura": "1,76",
        "peso": 100,
        "tipo_sanguineo": "AB-",
        "cor": "roxo"
    },
    {
        "nome": "Noah Victor Fernando Oliveira",
        "idade": 61,
        "cpf": "85853859528",
        "rg": "332980649",
        "data_nasc": "13\/08\/1958",
        "signo": "Leão",
        "mae": "Tereza Francisca",
        "pai": "José Carlos Eduardo Oliveira",
        "email": "email@example.com",
        "senha": "LpXALMkeDH",
        "cep": "44080636",
        "endereco": "Rua Tabuleiro do Norte",
        "numero": 819,
        "bairro": "Subaé",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7537730634",
        "celular": "75997027492",
        "altura": "1,88",
        "peso": 93,
        "tipo_sanguineo": "AB+",
        "cor": "roxo"
    },
    {
        "nome": "Lucas Yuri Isaac Silva",
        "idade": 28,
        "cpf": "53766516574",
        "rg": "422942777",
        "data_nasc": "02\/07\/1991",
        "signo": "Câncer",
        "mae": "Raimunda Andreia Isis",
        "pai": "Luís Fernando Silva",
        "email": "firstname.lastname@example.com",
        "senha": "Raa2xMfasl",
        "cep": "44097776",
        "endereco": "Rua Brejo de Madre de Deus",
        "numero": 252,
        "bairro": "Limoeiro",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7527389404",
        "celular": "75985947164",
        "altura": "1,91",
        "peso": 60,
        "tipo_sanguineo": "B+",
        "cor": "vermelho"
    },
    {
        "nome": "Clarice Pietra Farias",
        "idade": 21,
        "cpf": "15510327596",
        "rg": "395591405",
        "data_nasc": "05\/07\/1998",
        "signo": "Câncer",
        "mae": "Nair Josefa Ester",
        "pai": "Marcos Benjamin Carlos Farias",
        "email": "firstname.lastname@example.com",
        "senha": "P14bZdlcx2",
        "cep": "44086390",
        "endereco": "Rua Itapetim",
        "numero": 235,
        "bairro": "SIM",
        "cidade": "Feira de Santana",
        "estado": "BA",
        "telefone_fixo": "7529393054",
        "celular": "75995736537",
        "altura": "1,67",
        "peso": 79,
        "tipo_sanguineo": "B+",
        "cor": "roxo"
    }
]
</code>
Esse .Json tem o cadastro de 30 Clientes tenho que pegar um a um e por no banco de dados só não sei como proceder nos tópos visto aqui no pt.stackoverflow vi como abrir o .json mais pegar os cada um dos clientes não consegui.
Seque o código que já tenho
<code>procedure TfrmClienteJSON.BitBtn1Click(Sender: TObject);
var
  JSArray  : TJSONArray;
  JSValue  : TJSONValue;
begin
  jsonObject := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(JSonObjectAsString), 0) as TJSONObject;
end;

procedure TfrmClienteJSON.btnAbrirClick(Sender: TObject);
var
   StringList : TStringList;
begin
  StringList := TStringList.Create;

  if OpenDialog.Execute then
  begin
    try
      StringList.LoadFromFile(OpenDialog.FileName);
      // Joga arquivo carregado na StringList na sua variável;
      JSonObjectAsString := StringList.Text;
    finally
      FreeAndNil(StringList);
    end;
  end;
end;
</code>
Comment: Em tempo, esses dados que você postou no json são dados reais? Se for, por favor, retire-os e substitua por dados descaracterizados. Não deixe dados reais que sejam pessoais ou sensíveis expostos na internet. Lembre-se da LGPD.
Comment: Olá. Qual é a versão do delphi que você usa?
Comment: Uso Delphi 10.2
Não são dados gerados em um Fake Name Generator.
Answer: Tenta armazenar o conteúdo json utilizando um memo e um Listbox pra listar o conteúdo
<code>var
  jsonObj, jSubObj: TJSONObject;
  JSArray  : TJSONArray;
  JSValue  : TJSONValue;
  i: Integer;
  j: Integer;
begin
  jsonObject := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Text), 0) as TJSONObject;
  for j := 0 to jsonObject.Size - 1 do
  begin
    JSValue := jsonObj.Get(j).JsonValue;
    JSArray := JSValue as TJSONArray;
  end;

  ListBox1.Clear;

  for i := 0 to JSArray.Size - 1 do
  begin
    jSubObj := (JSArray.Get(i) as TJSONObject);
    JSValue := jSubObj.Get(0).JsonValue;
    ListBox1.Items.Add(JSValue.Value);
  end;
end;
</code>
Comment: tinha faltado passar um parâmetro no get, testa agora, ajustei o código
Comment: Modei a linha `jsonObject := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Text), 0) as TJSONObject;` por que `ASCII` não estava mostrando os nomes com os acentos, fora isso tudo deu certo. agora eu já sei como fazer a leitura desse tipo de arquivo. Valeu.
Comment: Tales, está apresentando essa Msg:

[dcc32 Error] U.pas(166): E2250 There is no overloaded version of 'Get' that can be called with these arguments
[dcc32 Warning] U.pas(166): W1000 Symbol 'Get' is deprecated:

Como ler um arquivo .Json pelo Delphi


Is there a way to pass variable itself(not the value) as argument in Python?

Question: I have a class with several data member and I want to write a function to compare different data member depend on the input of that function. Is there a way to pass variable itself into the function?
For example:
class someClass:
    self.a = 1
    self.b = 1.1
    self.c = 1

def someFunction(x, lt, var):  #lt is a list of someClass, var is the variable I want to choose to compare with
    for v in lt:
        if x.var < v.var:
            ...

I think there must be some distinct characteristic between same variable in deferent instance, but not sure what it is.
Thank you!
Answer: Yes, but it's probably not what you want.
If you find yourself accessing things by (string) names a lot, you're looking for a dictionary. Rather than having three separate fields self.a, self.b, and self.c, consider having just one: self.data.
class SomeClass:
    def __init__(self):
        self.data = {
            "a": 1,
            "b": 1.1,
            "c": 1,
        }

def some_function(x, some_class_instances, var):
    for y in some_class_instances:
        if x.data[var] < y.data[var]:
            ...

Generally, you want to use dictionaries if you want this form of access.
That being said, what you asked for can be done. Pretty much anything can be done in Python, with enough effort. To be clear, I strongly recommend using a dictionary, but for completeness, getattr is a built-in Python function that performs dot-notation style access with a string argument. This won't just get slots on __dict__; it'll also catch propertys, other descriptors, or anything else; it's literally equivalent to using a dot to access the field, with all the caveats that come with that.
def some_function(x, some_class_instances, var):
    for y in some_class_instances:
        if getattr(x, var) < getattr(y, var):
            ...

Comment: I see!! `getattr` is what I've been looking for!! Thanks a lot! But I would try the dictionary later, I think it is better
Comment: Is this a Python only thing? I don't think c/c++ have something similar?
Comment: It's a reflection feature. Lots of dynamic languages, like Python, Javascript, and Ruby, have reflection. It's less common in compiled languages. There's no way to do this in C or C++.
Comment: I see! Thank you very much ~


Best to way execute a file internal to a java project

Question: Suppose my project structure is:
/project
  /src
    /java
      Util.java
    /cpp
  /bin
    a.out

I'd like to execute a.out from within Util.java without hard-coding any absolute paths in my java file. What's the best way to go about doing this?
EDIT -- Here's what I ended up doing: I happen to be using autoconf as most of the code is c++.  I defined a substitution variable like AC_SUBST([project_root], [$(pwd)]) in configure.ac and substituted it in a Config.java.in file.
Comment: Are you deploying the project as a JAR?
Comment: Actually, I may want to deploy as a JAR in the future.
Answer: Perhaps using a properties file to be loaded on deployment/running time depending on the nature of your app.
More about its use in this thread
How to use Java property files?
Comment: Don't I still need to store an absolute path in the property file?
Comment: Perhaps you can get the context path when your app starts, so you add the relative route from the properties. The absolute path is also an option
Answer: You're file path to a.out could be ../bin/a.out. And then execute the file using that path.
Answer: Below is some pseudo code that might help you.
// look for the executable in the current working directory
File executable = new File("a.out");
if( executable.exists()){
   System.exec() .... etc
} else {
   String location = YourMainClass.class.getProtectionDomain().getCodeSource().getLocation();
   // write code to form a path name to the a.out based on the location of .jar file
}


How to fit a Gaussian distribution with outlier data points?

Question: I have a data set which consists of large number of data points. However, there are some outlier points that can be considered be noise. If I include all data points to approximate the Gaussian distribution, the standard deviation is apparently larger than expected. How can I fit this data set with a Gaussian distribution and get accurate mean and standard deviation ignoring these noise points.
Thanks
Comment: The question to ask yourself: *how do you know, that they are outliers?*
Comment: is it a univariate or a multivariate gaussian you are trying to fit?
Comment: a univariate gaussian, not mixture gaussian
Comment: [multivariate](http://en.wikipedia.org/wiki/Multivariate_normal_distribution) is not the same as mixture. Assuming you meant univariate, then, your question is a duplicate of this [one](http://stats.stackexchange.com/a/72980/603)
Comment: Does this answer your question? [How to define the multiplier range for variance test based outliers detection algorithm?](https://stats.stackexchange.com/questions/72973/how-to-define-the-multiplier-range-for-variance-test-based-outliers-detection-al)
Answer: Try https//mycurvefit.com.  It's an online tool that will fit a curve to your data.  (AFAIK its free for smaller data sets but there might be a charge for large data sets).  With the tools just enter your data, select the fit.  You can also mark points as outliers by clicking to "flag" then remove those outliers and recalculate the new curve, e.g.
Comment: -1 This has *nothing* to do with fitting distribution to data. Fitting distribution to data $\ne$ fitting curve to points.


Symfony 5 + Doctrine - Add a Filter to every request, based on current user

Question: I'm trying to dynamically set a "tenant_id" filter on some actions of my Controllers.
I've built a class which extends SQLFilter and put inside addFilterConstraint the logic to do that.
The problem is the dynamic "tenant" parameter:
If I put this piece of code inside each of my Controllers actions it works:
$em->getFilters()->getFilter('tenant')->setParameter('tenant_id', $security->getUser()->getTenant()->getId());

Of course this is not maintainable so I'm trying to move this logic somewhere else to make it cleaner and easier to maintain.
I'm thinking to something like an Event, but I would need to dispatch an Event on every Request, when the Security has already done it's job and then I'd need to modify the EntityManager.
Any idea?
Thanks
Comment: You are looking for an event subscriber: https://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber this can be ran on every `onKernelRequest` for example..
Comment: I've found this too, but I don't know how to access EntityManager and Security since these classes only receives the Event, am I wrong?
Answer: You are looking for an event subscriber (Ref)
Here is an example you can use showing access to the entity manager and security classes. place this file in src/EventSubscriber/TenantFilterEventSubscriber.php this will then run on every request.
namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\Security\Core\Security;
use Doctrine\ORM\EntityManagerInterface;

class TenantFilterEventSubscriber implements EventSubscriberInterface
{
    protected $security;

    public function __construct(Security $security, EntityManagerInterface $entityManager)
    {
        $this->security = $security;
        $this->entityManager = $entityManager;
    }

    public function onKernelController(ControllerEvent $event)
    {
        $controller = $event->getController();

        if (!is_array($controller)) return;

        if ($controller[0] instanceof YourController) {
            $user = $this->security->getUser();

            if (null !== $user) {
                // Do stuff
            }
        }
    }

    public static function getSubscribedEvents()
    {
        return array(
            'kernel.controller' => 'onKernelController',
        );
    }
}


If $X^{T}\cdot A \cdot X = 0$ for some $X$, what is the condition must impose to $A$?

Question: If $X$ is a vector and is a solution of the equation $X^T\cdot A \cdot X = 0$, what is the condition must impose to $A$ to $X$ be a non-trivial solution, excluding the possibility that $A$ be a skew symmetric matrix ?
Comment: Is $X$ a vector or a matrix? If it's a vector, the equation is saying $X\perp AX$.
Comment: @Berci It's a condition for $A$ and $X$.
Comment: If $A$ is positive definite than that condition implies $X$ is trivial. So the condition is that the symmetric part of $A$ is not definite.
Answer: Guess you mean $x$ as a vector, $A$ being a skew symmetric matrix would be sufficient for all x to make $x^TAx = 0$ as the expression $x^TAx$ is actually $\Sigma_i^n \Sigma_j^n A_{ij}x_ix_j$, for general case, the condition of $A$ depends on $x$, like when $x$ is the column vector with all entries equals 1, $A$ need to be the matrix for which all of this entries sum up to 0.
Answer: WLOG we can assume that $A$ is symmetric (as the antisymmetric part of $A$ does not count). If $A$ is positive (or negative)  definite then
$$x^T Ax =0 \ \ \Longrightarrow \ \ x=0$$
So a necessary condition is that $A$ is not definite. This condition is also sufficient.
In fact let us diagonalize $A=U^T D U$ (possible since $A$ is symmetric). Call $y = Ux$.
Then
$$
x^T Ax =0 \ \ \Longrightarrow \sum_i y_i^2 d_i =0
$$
Where $d_i$ are the eigevalues of $A$. But this latter equation can be satisfied only if some $d_i$'s have opposite sign.

To summarize, a necessary and sufficient condition for the claim is that the symmetric part of $A$ is not definite.


Using a rest plugin or a service how can one tell the token module the response is not cachable

Question: I have a rest plugin which returns an array and sends a mail. When commenting the Token::replace all works fine, the token replace function creates a BubbleableMetadata object (from what I understand of it) and than a exception is thrown:
LogicException: The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early.

Answer: See https://www.drupal.org/node/2513810.
Also, whenever you encounter an exception, put a breakpoint on the line of code that throws that exception. You'll always find more helpful information there. In this case, that's in \Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.

Better yet, an issue with this same exact problem (when using a REST resource) on d.o, including the concrete answer with a solution: https://www.drupal.org/node/2568423
(But the above is still valid, it's the general answer.)
Comment: Thanks I have set up a context for the token replacement.

See "Setting up a render-context for the rare-case" in https://www.drupal.org/node/2513810

I also had a service that simply returned the one time login link. For that one it was sufficient enough to disable the collecting of metadata in user_pass_reset_url. So I made a copy of user_pass_reset_url and added ", TRUE" to the generating of the Url object, which fixed that issue.
Answer: This is part which rendering happen on replace function
 // If a local $bubbleable_metadata object was created, apply the metadata
  // it collected to the renderer's currently active render context.
  if (!$bubbleable_metadata_is_passed_in && $this->renderer
    ->hasRenderContext()) {
    $build = [];
    $bubbleable_metadata
      ->applyTo($build);
    $this->renderer
      ->render($build);
  }

based on the documentation https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Utility%21Token.php/function/Token%3A%3Areplace/9.2.x
you can make this condition false and prevent running bypassing a bubbleable_metadata to it.
I would suggest new BubbleableMetadata()
$token->replace($meta_tag, ['node' => $entity],  ['clear' => TRUE], new BubbleableMetadata());