Is this load balancing / horizontal scaling scheme naive?

Question: I offer my solution to the following problem, and ask you networking and server admin professionals to validate it or poke holes in it. I am interested in any obvious attack vectors or scalability issues you may see. Thanks!
Requirements:

HTTPS support, handled by each application server independently
near-linear horizontal scalability
bandwidth distributed across servers (response data does not all return through the LBs or proxies)
something like failover for application servers and load balancers
client-server affinity
Linux-friendly (solution not closed source)
bootstrap-friendly! (i.e. low initial cost)

The Scheme:
<code>            PUBLIC NETWORK
+-----+------+--------+-----+------->
      |      |        |     |
      v      v        v     v
    +---+  +---+     +--+  +--+
    |LB1|  |LB2| ... |S1|  |S2| ...
    +---+  +---+     +--+  +--+
</code>
Redundant load balancers (LB*, via something like DNS RR, or just failover): their only purpose is to offer clients the URI to some application server instance, which the client would then use perpetually for its requests. The distribution would be random or round robin, initially.
Application Server instances (S*) each independently handle requests directly from clients.
Stateless architecture lets individual servers go down. Clients request a new server from the load balancers if their assigned server fails.
New application servers could spin up, register with the load balancers, and be assigned to clients very quickly. All S* would have a subdomain DNS entry to share a wildcard certificate.
A naive implementation could be done entirely on one server with zero redundancy, and delegate responsibilities to expand as needed.
Immediate Concerns
Firewall and DDoS protection would have to be managed at each server, instead of centrally like you have with load balancing reverse proxies. Centralized configuration management is as far as I've thought into this.
This scheme does not take advantage of geographic location or server response time, as something like Anycast DNS would. It's a conscious trade-off for greater likelihood of server affinity, and can possibly be shoehorned in later.
Answer: On a high level it looks sound but there are some gaps in the scheme.

Explain how the client knows to fall back if a server goes down. (biggest problem I think)
Explain how your load balancers just provide a URI. Are these just
web servers?
How do you handle stateful data such as session cookies which may
impart implicit data from a previous server. Else, you use normal
cookies?
How do you register with the load balancer?
How would storage work in this design? How does that scale?
How does a load balancer actually balance load in this scheme? Since
all it offers are referrals there is no means for it to know when a
session has ended on the server side.
How does a load balancer know if a server is down?
Comment: I imagined simple solutions. Clients can listen for specific 4xx/5xx errors to refresh from the LBs, something like heartbeat could monitor server health and update the LB configs automatically, the LBs are indeed a kind special-purpose web server, there is no application state to deal with (no sessions, like Amazon S3), and registering a new server with the LB is an admin task (update configs manually, or through some admin API; should not happen often). The "balancers" ensure initial spread, but S* resource usage polling could influence LB* distribution if needed. Spread may be good enough.
Comment: If you're listening on the LB's via some form of poll/websocket its not truly horizontal seeing as all connections ultimately pool on the LB's anyway. May as well get the LB's to do proper load balancing in that case.
Comment: As for storage: if storage is necessary at all, isn't choosing the right distributed storage system pretty application-dependent? There are so many options (PostgreSQL, MongoDB, Riak, etc.). I think it may end up being the bottleneck for IO-heavy applications, but I don't know. Presuming the rest of the scheme isn't a terrible idea, I'd build out a few things and load test them to see what falls apart first.
Comment: Re pooling on the LBs: having all the client-server data funnel through the LBs is a NO-GO for me. Server affinity is a goal, and I didn't find any open source Layer 4 balancers that did that. Client connections wouldn't pool at the LBs. The LBs still only git hit initially, and when a server dies. Resource usage would only influence server assignment in those two scenarios, so balancing would be loose but present. And I still have hope that simple random assignment or round robin would be sufficient for most cases most of the time. I really appreciate the feedback.


What is similarities and difference between statistical inference and hypothesis testing?

Question: What is similarities and difference between statistical inference and hypothesis testing?
I am confused between those
Comment: The [second](https://en.wikipedia.org/wiki/Statistical_inference#Examples_of_frequentist_inference) is a subset of the [first](https://en.wikipedia.org/wiki/Statistical_inference).
Answer: inferentiel statistic is a type of statistic, on statistic we have two types descriptive statistics and inferentiel statistics. the last one is used to draw conclusion from population. statistic testing is the way used to draw conclusion. to recap descriptive statistic to describe population using graphs and tables while inferentiel statistic used to draw conclusion by setting hypothesis and testing it to make some conclusion


Divergent sequences whose sum diverges?

Question: Are there any divergent sequences whose sum also diverges? Can anyone please explain with an example? Thank you.
Comment: $1,-1,1,-1,...$. This sequence diverges and obviously its sum also diverges.
Comment: What about $\;\{1,2,3,...,n,...\}\;$ ?
Comment: What do you mean with "divergent sequences whose sum also diverges" ?Most of the so called divergent series have a partial sum which diverges. I want to point out the fact that the sum mentioned by @KittyL diverges but it's partial sum doesn't (it's always 0 or 1 depending if you sum an odd or an even number of terms).
Comment: @Renato "diverge" is the opposite of "converge." A sequence that alternates between 0 and 1 does not converge, hence it diverges.
Answer: The sum of every divergent sequence diverges. An infinite sum can only converge if its terms converge to 0, and even that is not enough. For example, $1/n$ as $n\to\infty$ converges to 0 but its sum diverges.
Answer: The n-th term test for divergence of a series is "If $x_n$ does not converge to zero, then $\sum_n x_n$ does not exist." Thus, if $x_n$ diverges, it does not converge to zero and $\sum_n x_n$ does not exist.
Note that the converse of the first statement is not true (take $x_n = n^{-1}$, for example).

Divergent sequences whose sum diverges?


How to analyze generated components from partial least squares in SAS

Question: I am using PROC PLS in SAS with multiple independent variables and multiple dependent variables. I would like to know how my independent variables are contributing to the scores for the first couple of generated components.
For example, if I was using principal components instead of partial least squares, I would look at the eigenvectors (principal component loadings) to determine this. Is there something similar I can look at for partial least squares? In the SAS documentation it refers to something called a "weight vector" that seems to be roughly what I want - but there does not appear to be any way of extracting it.
General suggestions for approaching this problem would also be appreciated!
Answer: The weight vectors from PROC PLS can be extracted using the "details" option.
However, they are not particularly interpretable because they relate the components to the "deflated" X matrix, which changes after each component is computed. I found that using the SIMPLS method (done by specifying method=SIMPLS) gives me exactly what I want - variation in the dependent variables is accounted for when constructing the components, but the components are a linear combination of the original X matrix, and thus there are interpretable loading vectors to look at.


Prove that the Brouwer theorem is false for the open ball $|x|^2 <a$

Question: Prove that the Brouwer theorem is false for the open ball $|x|^2 <a$
Brouwer Theorem: Any smooth map $f$ of the close unit ball $B^n \subset R^n$ tin to it self must have a fixed point.
I need to find a counter example, so I let $f: B^k \to B^k$ be a composition function
$$B^k \to^g R^k \to^h R^k \to^{g^{-1}} B^k$$
where $h$ be define as $h(x) =x+a$ for $a \not =0$ . Note that $h: R^k \to R^k$ has no fixed point, so $f: B^k \to B^k$ doesn't have any fixed point either.
Is my reasoning acceptable? or it need more work? If so, please help me improve it.
Comment: This function maps $B^k$ diffeomorphically to all of $\Bbb R^k$. Can you find a function on $\Bbb R^k$ with no fixed point and transport it back to $B^k$?
Comment: Induction would not be useful. Try to think of a map of the closed ball whose only fixed point is on the boundary. Then take its restriction to the open ball.
Comment: @TedShifrin you mean this function $g(x)= \frac {ax}{\sqrt {a^2 +|x|^2}}$?
Comment: Except for the typo, yes :)
Comment: However, $f(0, \ldots, 0) = (0, \ldots, 0)$, so you need to find a different function.
Comment: Looks good, but you deleted the definition of $g$ when you edited!
Answer: As has been established in the comments, your construction works because $f$ has a fixed point if and only if $h$ does, but $h$ was chosen so that it doesn't have a fixed point.
Another map that works is $f : B(0, a) \to B(0, a)$ given by $f(x) = \frac{1}{2}(x+ae_1)$ where $e_1 = (1, 0, \dots, 0)$. This sends a point $x \in B(0, a)$ to the midpoint of the line segment between $x$ and $ae_1 \in \partial B(0, a)$. It can be verified either algebraically or geometrically that $f$ has no fixed point. If the map were extended to $\overline{B(0, a)}$, it would have a fixed point at $ae_1$.


Confidence intervals for expected value of almost independent variables

Question: How may I calculate (or approximate with reasonable error) confidence intervals of expected value of non-normally distributed random variable $Y$ given its almost independent samples?
I have $n = 100$ samples of a random variables
$$Y_j = \sum_{i=1}^{m} X_{i, j}^2 ,$$
where $X_{i, j}$ are almost independent random values and $m \gt 100$.
By "almost independent" I mean that
$$
\sum_{j=1}^{n} \sum_{i=1}^{m} X_{i, j} = 0 .
$$
For a given $i$ all $X_{i, j}$ have the same (possibly non-normal) distribution.
Were $Y_j$ independent, then from central limit theorem I would assume, that
$$
\bar{Y} \sim \mathcal{N}(\mu_Y,\,\frac{\sigma_Y^{2}}{n}) .
$$
Unfortunately I know neither $\mu_Y$ nor $\sigma_Y^{2}$, so I have to approximate them by $\bar{Y}$ and $s_Y^2 = \frac{\sum_{j=1}^n (Y_j - \bar{Y})}{n - 1}$.
Was $Y$ normally distributed (and independent), confidence intervals would be given by $t$ distribution of $n - 1$ degrees of freedom.
If there is no formula for exact CIs, then how [un]safe would it be to ignore the dependence and/or non-normality of the distribution?
Comment: The trouble is that the process that enforces the constraint may be very stringent, e.g., each "even-indexed" observation is the negative of the immediately preceding "odd-indexed" observation ($X_{i,2} = -X_{i,1}$), for example.   This example is far more stringent than is needed to ensure the constraint is met, but, without knowing how that constraint is enforced, you're making a leap of faith if you ignore the correlation.
Comment: @jbowman the constraint originates from $X_{i, j}$ being kind of residuals in a model fitted to data from m distributions, each distribution sampled up to n times (in about 30% cases sampling failed which I am interpreting as zero-residual).
Answer: The Berry Esseen theorem gives you an idea of how far off you are going to be from normality. This result is exact and does not rely on large $N$ asymptotics
Comment: The theorem seems to be very useful indeed. How do I deal with ignorance of the true variance and not complete independence of samples?


Determine if the improper integral converges $\sin(1/x)$

Question: I tried comparing it with $\sin(x)$ but $\sin(1/x)$ is $\geq$ than $\sin(x)$ and the integer of $\sin(x)$ converges so that doesn't help me
$$\int_0^{1} \sin (\frac{1}x) dx$$
Comment: $\sin\leq1$ seems to be enough here.
Comment: If you only need to determine whether the integral exists, and not necessarily what it might evaluate to, then you need to go into partitions of the interval $[0,1]$ and upper and lower sums.
Answer: It is not an improper integral. $\sin(1/x)$ is continuous and bounded on $(0,1]$, so it is integrable.
Comment: Now I'm curious: is there an analytic way to compute it?
Answer: The function $x\mapsto \sin(1/x)$ is bounded, and like any bounded function, its integral over a bounded set converges (assuming the integral makes sense, but here your function is continuous on $]0,1]$, so both Lebesgue or Riemann's framework would agree here that your function is integrable).


Is there a way to render a node with view mode via token?

Question: Is there a way to render a node with view mode via token? Like:
[node:5:teaser]

Comment: Do you mean you want to know the API which can get the output from the token string? I guess you are looking for [Token::replace](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Utility%21Token.php/function/Token%3A%3Areplace/8.2.x)
Comment: @JimmyKo na I wanted to know if there was an existing text filter for that token, like insert view and insert block
Comment: Did you try [token_filter](https://www.drupal.org/project/token_filter)?
Answer: So the question is how to insert something like [node:5:teaser] into a formatted text area and get the teaser view of node 5.
This can be done with a combination of modules:
Token Filter (D7,D8)

Token Filter is a very simple module to make token values available as an input filter.

So (after adding that filter to the text format), for the current node, we can use e.g. [node:title] in formatted text.
Token Entity Render (D7, D8 Issue)

Provides tokens ([entity_type:render:view_mode_name]) that render full entities using view mode specified.

Advanced Entity Tokens (D7, D8 Issue)

Through AET you can query the DB for entities using Tokens.
  Examples: [aet:node:1]

Putting it together
In the end you should be able to use [aet:node:5:render:teaser] in your formatted text.
Comment: I'm amazed this is possible with tokens too, but I'd also suggest to consider the [Entity Embed module](https://www.drupal.org/project/entity_embed) for a use case like this. I'm using it successfully for media entities.
Comment: entity_embed - good point. Isn't most of this in media core these days anyway?
Comment: Advanced Entity Tokens seems like a good idea for textual data, say if I want a specific Term ID for a link [based on its UUID](https://www.drupal.org/project/aet/issues/2038423).  I'll see if I can get a proper D8 version rolling based on the existing patch.


Does triply transitive mean there are three orbits?

Question: If a group action is transitive, it possesses a single orbit. Does that mean a group action that is doubly transitive has two orbits, and so on?
http://mathworld.wolfram.com/TransitiveGroupAction.html
Comment: Doubly transitive actions are transitive, so ...
Comment: It might be more interesting to assume that the action of $G$ on $X$ is $n$-transitive and try to count the number of orbits of the induced action of $G$ on $X^n$.
Comment: https://groupprops.subwiki.org/wiki/Doubly_transitive_group_action
Answer: No. Multiple transitivity is more restrictive than single transitivity, rather than a broader concept. If we can send three arbitrary elements to three elements we choose, we can certainly send a single element anywhere we want it to go.
There are significant restrictions on the form of multiply transitive groups as a result - and some of the special ones (eg Matthieu Groups) are at the heart of the 26 exceptional simple groups.


Printing Form in Landscape using PrintForm Function

Question: i have a question i am trying print the form in landscape so it can print all of the content of the form.
Here is the print preview look like it is cut.

I am using this code to print it and also use the landscape mode to true i search in google.
<code> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = True
    Me.PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
    Me.PrintForm1.Print()
End Sub
</code>
But it has the same result.
Thank you.
Comment: It looks like it's in landscape.  The margins look too thick though.
Comment: I replace the screenshot i cut it on paint thats why it looks like in landsacpe
Answer: I did this along time ago from something I found on a Google search. It will give you a printersettings dialog where you can select landscape before printing. If memory serves, the Printform.PrinterSettings.DefaultPageSettings.Landscape is read only or has an issue. Can't remember, anyway, you will need to add a PageSetupDialog and a PrintDocument to the form. Then change your code to this:
<code>    PageSetupDialog1.Document = PrintDocument1
    If PageSetupDialog1.ShowDialog = DialogResult.OK Then
        PrintForm1.PrinterSettings = PageSetupDialog1.PrinterSettings
        If PrintForm1.PrinterSettings.IsValid Then
            PrintForm1.Print()
        End If
    End If
</code>
When you click your button1, you should be prompted with a page setup dialog where you can select landscape. Click OK and your form should print landscape.
I Guess if you don't want the Page Setup dialog, you could just have it print by setting the PageSetupDialog1's settings to Landscape.
<code>    PageSetupDialog1.Document = PrintDocument1
    PageSetupDialog1.PrinterSettings.DefaultPageSettings.Landscape = True
    PrintForm1.PrinterSettings = PageSetupDialog1.PrinterSettings
    If PrintForm1.PrinterSettings.IsValid Then
        PrintForm1.Print()
    End If
</code>
Comment: Just a simple question. What is the printdocument1 ?? It says not declared thank you.
Comment: by default, it should be located in the printing section of your Toolbox pane. If you've already found the PageSetupDialog, it should have been in that same section.


Unramified extension of Dedekind domains implies etale covering

Question: I'm using the following (nonstandard?) definition of an etale ring homomorphism: $f: A \rightarrow B$ is etale if $B$ is finite, projective, and separable as an $A$-module.  Here separable is defined as saying that the trace map $B \rightarrow A$ is nondegenerate, as in these notes that I'm reading.  The picture below is from the same set of notes (pg. 23).

$B$ is a finitely generated module over $A$ (maybe we need $L/K$ separable for this, not sure), and since $A_{\mathfrak p}$ is a PID, $B$ is projective as an $A$-module.  I'm trying to see why $B/A$ being unramified implies nondegeneracy of the trace, and moreover how a prime of $A$ being ramified in $B$ would prevent this from happening.
Comment: Assuming $L/K$ separable, is this not the standard criterion for primes of $K$ ramifying in $L$ ? Namely, primes dividing the discriminant (which is the determinant of the traces of $e_i e_j$, $e_i$ basis for $B$, at least locally)?
Comment: BTW, 'unimodular' is probably more appropriate than the 'nondegenerate' above: https://en.wikipedia.org/wiki/Bilinear_form#unimodular (the notions differing if one is not talking about vector spaces)
Answer: Let $\mathfrak d$ be the different of $L/K$ (assumed finite and separable), which controls ramification.
The trace pairing $\mathfrak d^{-1} \times B \rightarrow A$ induces $\mathfrak d^{-1} \cong B^{\vee} = \text{Hom}_A(B,A)$, and no fractional ideal $I \subsetneq \mathfrak d^{-1}$ satisfies $I \cong B^{\vee}$ via the trace pairing.
Comment: How do you know that $\mathfrak d^{-1} \rightarrow \operatorname{Hom}_A(B,A)$ is surjective?
Comment: Since $L/K$ is separable, the trace induces $L \cong \text{Hom}_K(L,K)$; every $f:B \rightarrow A$ extends to $\tilde f: L \rightarrow K$.


cd - results in "-bash: cd: OLDPWD not set"

Question: I tried following these instructions to go back to the previous directory, but that results in "-bash cd: OLDPWD not set".

How could I do what I want?
Thank you.
Comment: `cd -` can't be the first command in the shell, you would have needed to `cd somewherelse` first so you have a thing to `cd` back to
Answer: I had the same problem. Add an alias for exit to write pwd to a file and then read it on the next start:
alias exit='pwd > ~/.lwd && exit;'
test -f ~/.lwd && export OLDPWD='head -1 ~/.lwd'

This solved the problem for me
Answer: When you open a new terminal the PWD is not set as the terminal opens in the /home/ directory of the user. If you change the current directory and then use this command then you will not face this problem. Actually, this is not even a problem this is how the terminal should actually behave.
Comment: This is the right answer. This is the default behaviour of bash.
Answer: What actually helped in my case was using
cd /

instead of
cd -

Answer: yeah.. it works for me too.
you should use
cd /
instead of
cd -
believe me it will fix the error


.NET How to Stop a user from deleting the file?

Question: I got this exam question "How to Stop a user from deleting the file?" I assume I should use some permission attribute, but which?
Comment: Are you allowed to get other people to do your exams for you? And if so, can we have the resulting qualifications?
Comment: David I am studying for an exam, I'm not doing the exam at the moment and asking You to help me cheat.
Comment: Just hold the handle to this file open.
Answer: Use FileInfo and set its .IsReadOnly property.
<code>        Dim fi As New FileInfo("c:\temp\temp.txt")
        fi.IsReadOnly = value
</code>
Answer: You cannot prevent a user with administrative rights from deleting a file. After all, he can overtake the file's ownership and change the permissions.
Comment: You can at least make it very difficult while your process is running :D
Comment: @Vladislav: an administrator can kill the process, or just restart the computer.
Comment: Well... that's the thing, your process doesn't _have_ to show up in task manager :P And I'm not even going to comment on your start-up options. But I believe this has gone off-topic.


FROM_UTC_TIMESTAMP giving null pointer exception when converting date to EST

Question: I am trying to convert a Date string to Timezone EST /EDT
I have a table as follows:
CREATE EXTERNAL TABLE IF NOT EXISTS myschema.timeZoneCheck1 (timeCol String)
row format delimited fields terminated by ','
lines terminated by '\n'
LOCATION '/users/TestTimeZone/'
tblproperties ("skip.header.line.count"="1");

and data in the HSFS file is as follows:
timeZoneDateCol
2015/12/08
2015/07/06
This is the required format needed:
If date is 2015/12/08 the I should get value like 2015120811 ( as it falls in EST)
If date is 2015/07/06 then I should get value like 2015070610(as it falls in EDT)
TRied this with hive function:
Select timeCol,FROM_UTC_TIMESTAMP(timeCol, 'EST') from myschema.timeZoneCheck1;

and this is the Stacktrace of error:
Error: java.lang.RuntimeException: org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error while processing row {"timecol":"2015/12/08"}
        at org.apache.hadoop.hive.ql.exec.mr.ExecMapper.map(ExecMapper.java:179)
        at org.apache.hadoop.mapred.MapRunner.run(MapRunner.java:54)
        at org.apache.hadoop.mapred.MapTask.runOldMapper(MapTask.java:453)
        at org.apache.hadoop.mapred.MapTask.run(MapTask.java:343)
        at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:163)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:415)
        at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1671)
        at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)
Caused by: org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error while processing row {"timecol":"2015/12/08"}
        at org.apache.hadoop.hive.ql.exec.MapOperator.process(MapOperator.java:507)
        at org.apache.hadoop.hive.ql.exec.mr.ExecMapper.map(ExecMapper.java:170)
        ... 8 more
Caused by: org.apache.hadoop.hive.ql.metadata.HiveException: Error evaluating Converting field timecol from UTC to timezone: 'EST'
        at org.apache.hadoop.hive.ql.exec.SelectOperator.processOp(SelectOperator.java:82)
        at org.apache.hadoop.hive.ql.exec.Operator.forward(Operator.java:815)
        at org.apache.hadoop.hive.ql.exec.TableScanOperator.processOp(TableScanOperator.java:95)
        at org.apache.hadoop.hive.ql.exec.MapOperator$MapOpCtx.forward(MapOperator.java:157)
        at org.apache.hadoop.hive.ql.exec.MapOperator.process(MapOperator.java:497)
        ... 9 more
Caused by: java.lang.NullPointerException
        at org.apache.hadoop.hive.ql.udf.generic.GenericUDFFromUtcTimestamp.evaluate(GenericUDFFromUtcTimestamp.java:90)
        at org.apache.hadoop.hive.ql.exec.ExprNodeGenericFuncEvaluator._evaluate(ExprNodeGenericFuncEvaluator.java:185)
        at org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator.evaluate(ExprNodeEvaluator.java:77)
        at org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator.evaluate(ExprNodeEvaluator.java:65)
        at org.apache.hadoop.hive.ql.exec.SelectOperator.processOp(SelectOperator.java:77)
        ... 13 more

FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1   HDFS Read: 0 HDFS Write: 0 FAIL
Total MapReduce CPU Time Spent: 0 msec

Answer: Did you read the documentation of that function carefully? And did you ever work with the binary DATE and TIMESTAMP Hive data types?
OK, these are rhetorical questions. In a nutshell, Hive recognises date and time literals (i.e. Strings containing a date/time representation) using ODBC format exclusively:

'2015-12-31' can be compared to a DATE (implicit conversion)
date'2015-12-31' is even more explicit and is more or less a SQL
standard
'2015-12-31 23:59:59' can be compared to a TIMESTAMP (implicit
conversion)

I could have said ISO instead of ODBC but, actually, ISO 8601 uses a "T" between date and time. ODBC uses a space, more human-readable, more popular.
So you just have to reformat your outlandish date format into an ISO/ODBC date, paste a dummy "midnight" time, and voila:
select OUTLANDISH_DATE,
  from_utc_timestamp(concat(regexp_replace(OUTLANDISH_DATE, '/', '-'), ' 00:00:00'), 'EST') as ZONED_TIMESTAMP
from WHATEVER

-- outlandish_date  zoned_timestamp
-- 2015/12/08       2015-12-07 19:00:00

Comment: BTW, the linux `date` utility works the same way, although you cannot change the default TZ... and the "midnight" trick is not necessary e.g. `date --date '2015-12-08 Z' '+%F %T %Z'` yields `2015-12-08 01:00:00 CET` on my Dev box.
Comment: thanks Samson! I am a newbie ..Is there some way to check if the current time stamp lies in EST or EDT?
Comment: The system clock does not care about timezones, the ticks refer to the "Unix Epoch" (milliseconds since 1970-01-01 00:00:00 GMT) and resynchronize with atomic clocks from time to time using NTP. And since a computer has no way to know on which part of the world it is running, the system time zone is **set arbitrarily** by the SysAdmin. If you run a server in India for a bank in Los Angeles, maybe it's set for Los Angeles time, maybe for India time, maybe for UTC to avoid misunderstandings. Bottom line, **you cannot know for sure in which time zone lies your server**.


Trying to find the index of the column name called `Customers' inside a table result -1

Question: Trying to find the index of the column name called Customers inside a table using jQuery/javascript, but this is always giving me -1
let tableDatacy = "Results_Table";
let columnName = "Customer";
let tableHeaders = [];
tableHeaders = Cypress.$(`[data-cy=${tableDatacy}] > thead:nth-child(0) > tr:contains(${columnName})`);
let columnPosition = tableHeaders.toString().indexOf(columnName);

       Customers

       Machine

Answer: If you only want to use jQuery, the equivalent would be
const index = Cypress.$(`[data-cy="Results_Table"]`)
  .find(`th:has(a:contains("Customers"))`)
  .index()

Note: the page should be loaded before attempting to use jQuery, e.g
beforeEach(() => {
  cy.visit(...)
})

it('...', () => {

  const index = Cypress.$(`[data-cy="Results_Table"]`)
    .find(`th:has(a:contains("Customers"))`)
    .index()
  ...
})

Comment: `.find('th:contains('Customers')')` should be `.find('th:has(a:contains('Customers'))`) because jQuery contains() only looks a the current element text.
Answer: You can use the invoke('index')to invoke the index value of your columns. You read more about it from the Jquery Docs.
cy.get('[data-cy="Results_Table"]')
    .find(`th:contains('Customers')`)
    .invoke('index')
    .then((index) => {
        cy.log(index); // Will Print 1
    });

In case you want to use the index value later in the same test, you can use .as()
cy.get('[data-cy="Results_Table"]')
    .find(`th:contains('Customers')`)
    .invoke('index')
    .as('indexValue')

cy.get('@indexValue').then((indexValue) => {
    expect(indexValue).to.eq(1)
    //Use the index value here
})

Comment: Thank you Alapan Das, In this approach I need to write rest of the code inside cypress promise part, later the scope index will be a problem..
Comment: I have updated my answer. You can use `.as()` aliases to access the values later in the same test. One thing to remember is that cypress resets all aliases before each test.


DBT pass wrong value in cte models

Question: Problem
I use a macros function to modify value in cte. How ever, it's always pass wrong value. The result from print always is:
*Running some_macro: 0, level
        final_table as (
        select
            toDate(`log_time`, 'Asia/Ho_Chi_Minh') as `report_date`,
            `social_type`,
            `platform_id`,
            {{ map_level_to_level_range("level") }} as `segment_level`,
            `level`
        from last_user_action

my macros:
{% macro map_level_to_level_range(level) %}
{% set numeric_level = level|int %}
{{ print("Running some_macro: " ~ numeric_level ~ ", " ~ level) }}
{% if numeric_level in range(0, 5) %} '00 - 04'
{% elif numeric_level in range(5, 10) %} '05 - 09'
{% elif numeric_level in range(10, 15) %} '10 - 14'
{% elif numeric_level in range(15, 20) %} '15 - 19'
{% elif numeric_level in range(20, 25) %} '20 - 24'
{% elif numeric_level in range(25, 30) %} '25 - 29'
{% elif numeric_level in range(30, 35) %} '30 - 34'
{% elif numeric_level in range(35, 40) %} '35 - 39'
{% elif numeric_level in range(40, 45) %} '40 - 44'
{% elif numeric_level in range(45, 50) %} '45 - 49'
{% elif numeric_level in range(50, 55) %} '50 - 54'
{% elif numeric_level in range(55, 60) %} '55 - 59'
{% elif numeric_level in range(60, 65) %} '60 - 64'
{% elif numeric_level in range(65, 75) %} '65 - 74'
{% elif numeric_level in range(75, 85) %} '75 - 84'
{% elif numeric_level in range(85, 100) %} '85 - 99'
{% elif numeric_level in range(100, 115) %} '100 - 114'
{% elif numeric_level in range(115, 130) %} '115 - 129'
{% elif numeric_level in range(130, 150) %} '130 - 149'
{% elif numeric_level >= 150 %} '150+'
{% else %} '-1'
{% endif %}
{% endmacro %}

Wrong result
You can compare it's with the result table follow:
enter image description here
Answer: The error is because you're passing a static string "level" to the macro, not the level value itself.
Since the level value is from table, you will need return a SQL statement from your macro to make the comparison occur in the database:
{% macro map_level_to_level_range(column_name) %}
CASE
  WHEN {{ column_name }} BETWEEN 0 AND 5 THEN '00 - 04'
  WHEN {{ column_name }} BETWEEN 5 AND 10 THEN '05 - 09'
  WHEN {{ column_name }} BETWEEN 10 AND 15 THEN '10 - 14'
  WHEN {{ column_name }} BETWEEN 15 AND 20 THEN '15 - 19'
  WHEN {{ column_name }} BETWEEN 20 AND 25 THEN '20 - 24'
  WHEN {{ column_name }} BETWEEN 25 AND 30 THEN '25 - 29'
  WHEN {{ column_name }} BETWEEN 30 AND 35 THEN '30 - 34'
  WHEN {{ column_name }} BETWEEN 35 AND 40 THEN '35 - 39'
  WHEN {{ column_name }} BETWEEN 40 AND 45 THEN '40 - 44'
  WHEN {{ column_name }} BETWEEN 45 AND 50 THEN '45 - 49'
  WHEN {{ column_name }} BETWEEN 50 AND 55 THEN '50 - 54'
  WHEN {{ column_name }} BETWEEN 55 AND 60 THEN '55 - 59'
  WHEN {{ column_name }} BETWEEN 60 AND 65 THEN '60 - 64'
  WHEN {{ column_name }} BETWEEN 65 AND 75 THEN '65 - 74'
  WHEN {{ column_name }} BETWEEN 75 AND 85 THEN '75 - 84'
  WHEN {{ column_name }} BETWEEN 85 AND 100 THEN '85 - 99'
  WHEN {{ column_name }} BETWEEN 100 AND 115 THEN '100 - 114'
  WHEN {{ column_name }} BETWEEN 115 AND 130 THEN '115 - 129'
  WHEN {{ column_name }} BETWEEN 130 AND 150 THEN '130 - 149'
  WHEN {{ column_name }} >= 150 THEN '150+'
  ELSE '-1'
END
{% endmacro %}

DBT pass wrong value in cte models


Subdomain on another host (another IP, another provider)

Question: Let's say my main domain is domain.net and i'm using it on a Media Temple (gs) server.
I have my blog on the subdomain blog.domain.net
Now I want to host my blog on my new VPS from Linode.
So, I need to keep domain.net on Media Temple and blog.domain.net on Linode.
Is this possible? Any advice?
Answer: Yes, this is possible. All you have to do is create an A record in your domain.net DNS for blog.domain.net which points to the IP address of the server at Linode.
Here is a link to the Media Temple documentation for this.


Dependency Injection with classes other than a Controller class

Question: At this point I'm injecting things into my Controllers with ease, in some cases building my own ResolverServices class. Life is good.
What I cannot figure out how to do is get the framework to automatically inject into non-controller classes. What does work is having the framework automatically inject into my controller IOptions, which is effectively the configuration for my project:
public class MessageCenterController : Controller
{
    private readonly MyOptions _options;

    public MessageCenterController(IOptions<MyOptions> options)
    {
        _options = options.Value;
    }
}

I'm thinking whether I can do the same for for my own classes. I assume I'm close when I mimic the controller, like this:
public class MyHelper
{
    private readonly ProfileOptions _options;

    public MyHelper(IOptions<ProfileOptions> options)
    {
        _options = options.Value;
    }

    public bool CheckIt()
    {
        return _options.SomeBoolValue;
    }
}

I think where I'm failing is when I call it like this:
public void DoSomething()
{
    var helper = new MyHelper(??????);

    if (helper.CheckIt())
    {
        // Do Something
    }
}

The problem I have tracking this down is practically everything that talks about DI is talking about it at the controller level. I tried hunting down where it happens in the Controller object source code, but it gets kinda crazy in there.
I do know I can manually create an instance of IOptions and pass it to the MyHelper constructor, but it seems like I should be able to get the framework do that since it works for Controllers.
Comment: When you use dependency injection you don't call `new`. Never for objects that should be resolved
Comment: Yes, that's the whole point of dependency injection (especially if using inversion of control containers which manage and do this instantiation). To push the instantation outside of your services/classes up to the point where the ioc container does that internally. In cases where you can't inject it via constructor you create a factory and pass the factory's interface to your service. the implementation of it uses the container to resolve it, in ASP.NET Core case injecting `IServiceProvider` in your factory and calling `IMyHelper helper = services.RequestService()`
Comment: When I'm trying to create an instance of MyHelper, I don't call new? (1) That sounds too easy, (2) It's a syntax error. :-)
Answer: Below is a working example of using DI without anything that involves MVC Controllers. This is what I needed to do to understand the process, so maybe it will help somebody else.
The ShoppingCart object gets, via DI, an instance of INotifier (which notifies the customer of their order.)
using Microsoft.Extensions.DependencyInjection;
using System;

namespace DiSample
{
    // STEP 1: Define an interface.
    ///
    /// Defines how a user is notified.
    ///
    public interface INotifier
    {
        void Send(string from, string to, string subject, string body);
    }

    // STEP 2: Implement the interface
    ///
    /// Implementation of INotifier that notifies users by email.
    ///
    public class EmailNotifier : INotifier
    {
        public void Send(string from, string to, string subject, string body)
        {
            // TODO: Connect to something that will send an email.
        }
    }

    // STEP 3: Create a class that requires an implementation of the interface.
    public class ShoppingCart
    {
        INotifier _notifier;

        public ShoppingCart(INotifier notifier)
        {
            _notifier = notifier;
        }

        public void PlaceOrder(string customerEmail, string orderInfo)
        {
            _notifier.Send("firstname.lastname@example.com", customerEmail, $"Order Placed", $"Thank you for your order of {orderInfo}");
        }

    }

    public class Program
    {
        // STEP 4: Create console app to setup DI
        static void Main(string[] args)
        {
            // create service collection
            var serviceCollection = new ServiceCollection();

            // ConfigureServices(serviceCollection)
            serviceCollection.AddTransient<INotifier, EmailNotifier>();

            // create service provider
            var serviceProvider = serviceCollection.BuildServiceProvider();

            // This is where DI magic happens:
            var myCart = ActivatorUtilities.CreateInstance<ShoppingCart>(serviceProvider);

            myCart.PlaceOrder("firstname.lastname@example.com", "2 Widgets");

            System.Console.Write("Press any key to end.");
            System.Console.ReadLine();
        }
    }
}

Comment: What if I want to instantiate the `ShoppingCart` in another class or method that we don't access the `serviceProvider` object?
Comment: Thank you, I had to search far too wide to get to ActivatorUtilities.CreateInstance.
Comment: Thank you!  I'm using it with Microsoft.AspNetCore.TestHost, Creating a TestServer and calling ActivatorUtilities.CreateInstance(_server.Host.Services);
Comment: @GonzaloBustamante Feel free to name a child after me. (Preferably one of yours)
Comment: what if I don't have serviceProvider??!!
Comment: You could use `AddScope` too.
Comment: What is ***ActivatorUtilities*** ?
Comment: this also defeats the purpose, using the ActivatorUtilities is probably the same as "new" code

but I needed to see it to believe it
Comment: @HelloWorld If you don't have a service provider you cannot use dependency injection. It's required by definition. The question "What if I don't have a ServiceProvider?" is similar to asking "What if I don't have milk?" in a discussion about how to pour a glass of milk. The ServiceProvider is the thing that holds your services - without it you have no services to "pour" into your new object.
Comment: Why `AddTransient`? This creates a new instance each time the type is requested. Do you really need a new instance?
Comment: Can someone separate the 'create service provider' and using that class in another service class and not inside the main method.
Comment: I wish I could upvote this multiple times, bless you.
Answer: Let's say MyHelper is used by MyService which in turn is used by your controller.
The way to resolve this situation is:

Register both MyService and MyHelper in Startup.ConfigureServices.
services.AddTransient<MyService>();
services.AddTransient<MyHelper>();

The controller receives an instance of MyService in its constructor.
public HomeController(MyService service) { ... }

MyService constructor will in turn receive an instance of MyHelper.
public MyService(MyHelper helper) { ... }

The DI framework will be able resolve the whole object graph without problems. If you are worried about new instances being created every time an object is resolved, you can read about the different lifetime and registration options like the singleton or request lifetimes.
You should be really suspicious when you think you have to manually create an instance of some service, as you might end up in the service locator anti-pattern. Better leave creating the objects to the DI Container. If you really find yourself in that situation (let's say you create an abstract factory), then you could use the IServiceProvider directly (Either request an IServiceProvider in your constructor or use the one exposed in the httpContext).
var foo = serviceProvider.GetRequiredService<MyHelper>();

I would recommend reading the specific documentation about the ASP.Net 5 DI framework and about dependency injection in general.
Comment: The problem I have with this is that I use background services that interact with the database, so the scoped lifetime with dbcontext doesnt work, right?  How do you use DI properly with EF core and background services?
Comment: Apply to minimal API ?
Answer: Unfortunately there is no direct way. The only way I managed to make it work is by creating a static class and using that everywhere else as below:
public static class SiteUtils
{

    public static string AppName { get; set; }

    public static string strConnection { get; set; }

}

Then in your startup class, fill it in as below:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    //normal as detauls , removed for space
    // set my variables all over the site

    SiteUtils.strConnection = Configuration.GetConnectionString("DefaultConnection");
    SiteUtils.AppName = Configuration.GetValue<string>("AppName");
}

Although this is bad pattern, as this will stay for the whole life cycle of the application and I couldn't find better way to use it outside controller.
Comment: For me this worked well.  Helpful and very simple.
Answer: Here's a more complete example to directly answer the OP's question, based on the current .NET Core 2.2 DI documentation here. Adding this answer since it may help someone that's new to .NET Core DI, and because this question is Google's top search result.
First, add an interface for MyHelper:
public interface IMyHelper
{
    bool CheckIt();
}

Second, update the MyHelper class to implement the interface (in Visual Studio, press ctrl-. to implement the interface):
public class MyHelper : IMyHelper
{
    private readonly ProfileOptions _options;

    public MyHelper(IOptions<ProfileOptions> options)
    {
        _options = options.Value;
    {

    public bool CheckIt()
    {
        return _options.SomeBoolValue;
    }
}

Third, register the interface as a framework-provided service in the DI service container. Do this by registering the IMyHelper service with the concrete type MyHelper in the ConfigureServices method in Startup.cs.
public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddScoped<IMyHelper, MyHelper>();
    ...
}

Fourth, create a private variable to reference an instance of the service. Pass the service as an argument in the constructor (via constructor injection) then initialize the variable with the service instance. Reference any properties or call methods on this instance of the custom class via the private variable.
public class MessageCenterController : Controller
{
    private readonly MyOptions _options;
    private readonly IMyHelper _myHelper;

    public MessageCenterController(
        IOptions<MyOptions> options,
        IMyHelper myHelper
    )
    {
        _options = options.value;
        _myHelper = myHelper;
    }

    public void DoSomething()
    {
        if (_myHelper.CheckIt())
        {
            // Do Something
        }
    }
}

Answer: TL;DR: You can save a singleton in a static var and then access it form other classes, but this an anti-pattern, use with caution.
Long version:
As per this question Resolving instances with ASP.NET Core DI from within ConfigureServices

Any services registered in ConfigureServices() can then be injected
into the Configure() method

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<FooService>();
}

public void Configure(IApplicationBuilder app, FooService fooService)
{
    FooServiceInstance = fooService;
}

public static FooService FooServiceInstance { get; private set; }

And then call it from your other code MyStartupClass.FooService.DoStuff()
Comment: if I want to initialize an event listener which will then poll events since no controller invokes this listener, how do I bring it up, especially when using a container like unity which only injects classes when requested?
Answer: You may use Activator.CreateInstance(). Here is a wrapper function for it. The way you use this is as follows.
var determinedProgrammatically = "My.NameSpace.DemoClass1"; // implements IDemo interface
var obj = CreateInstance<My.NameSpace.IDemo, string>(determinedProgrammatically, "This goes into the parameter of the constructor.", "Omit this parameter if your class lives in the current assembly");

Now you have an instance of obj which is instantiated from type determined programmatically. This obj can be injected into non controller classes.
public TInterface CreateInstance<TInterface, TParameter>(string typeName, TParameter constructorParam, string dllName = null)
{
    var type = dllName == null ? System.Type.GetType(typeName) :
            System.AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName.StartsWith(dllName, System.StringComparison.OrdinalIgnoreCase)).GetType(typeName);
    return (TInterface)System.Activator.CreateInstance(type, constructorParam);

}

PS: You may iterate through System.AppDomain.CurrentDomain.GetAssemblies() to determine the name of the assembly that houses your class. This name is used in the 3rd parameter of the wrapper function.


Row makes Contact Form 7 fields unclickable

Question: On my Wordpress site the hero section is split into 2. On the left, I have the logo, an image and optional headings. On the right, you have your contact form.
My issue is - I had to move the form down with a large margin in order to be able to fill in the form. The top input fields weren't clickable. Moving it down made them clickable again. But they are only clickable to the point where the logo row intersects with the input fields. So, it's kind of like the row is blocking the input fields. I tried adding z-index stuff in Chrome Console - but that gave me no results - except that I made the content disappear when I set it to -1.
I would like the form fields to be all clickable - and be decrease the massive margin I had to put in - but my very limited knowledge doesn't allow me to achieve this.
https://ibb.co/swcbxbc - logo row blocking form field submission.
https://ibb.co/sPbXmBk - huge margin above form
Is there anything I can do to move the form up, but have the input fields clickable.
Comment: Using position relative or absolute width z-index. Please add your code
Comment: Upon further inspection - I noticed the right column moved up behind the header row. The max-width thing did help - but only to a small extent. I also tried position and z-index on some of the elements, but without any success. As it's a WordPress template it's hard, for me at least, to isolate the exact code that's causing the problem. It's going to be much faster to rebuild the site than to further troubleshoot. The help is appreciated though.
Answer: In the div with the class logo-container-off
put this in css
<code>max-width: 300px;
</code>


Complex function with real coefficients and only complex roots, with property $f(1-z) = f(z)$

Question: Consider the real value function $f(x) = 2x^2 - 2x - 6 = (x-1)^2 + x^2 - 5$.
$f(x)$ is symmetric around $x = 1/2$.
However, if we consider a complex function with real coefficients, $f(z) = (z-1)^2 + z^2$, we cannot say that this is symmetric about the line $Re(z) = 1/2$. In this case, the zeros $(1/2 \pm i/2)$ are on the line $z = 1/2$ and symmetric about the real axis.
Consider another complex function formed out of four complex roots $1/2 \pm i, 1/2\pm 2i$. I am choosing it like this to make $f(1-z) = f(z)$. Here $f(z) = (z-1/2)^4 + 5(z-1/2)^2 + 4$. The zeros are symmetric about the real axis and on the line $z = 1/2$.
I claim that all complex functions with real coefficients and with only complex roots that exhibit $f(1-z) = f(z)$ have zeros on the line $z = 1/2$. Functions like $\sin (2\pi z)$ satisfy $f(1-z) = f(z)$ but have real zeros.
Question: Can someone show a counter example to my claim?
Thanks
Answer: Consider the function
$$f(z) = \left( z - \frac{1}{2} \right)^4 + 1.$$
The equation $f(z) = 0$ is equivalent to $\left( z - \frac{1}{2} \right)^4 = -1$, so the solutions are the four vertices of a tilted square centered at $\frac{1}{2}$, namely
$$\begin{align*}
& \frac{1}{2}  + \frac{\phantom{-}1+i}{\sqrt{2}}, \qquad \frac{1}{2} + \frac{-1+i}{\sqrt{2}}, \\[1ex]
& \frac{1}{2} + \frac{-1-i}{\sqrt{2}}, \qquad \frac{1}{2} + \frac{\phantom{-}1-i}{\sqrt{2}}.
\end{align*}$$
So $f$ has no real roots and $f(z) = f(1-z)$.

Also note that each polynomial $f(z)$ with real coefficients such that $f(z) = f(1-z)$ will have roots both symmetric with respect to the line $\Im z = 0$ (because of the real coeffiecients $f(z) = 0 \iff f(\overline{z}) = 0$) and symmetric with respect to the point $z = \frac{1}{2}$, which makes for a symmetry with repsect to the line $\Re z = \frac{1}{2}$.


Expected an intended block error, what am i doing wrong

Question: I don't know why but i can't run this program in each if or elif there's going to be a couple of functions but i cant seem to be able to make it work, i keep getting the indent error at the elif i==2:
menu1=['1-Jugar','2-Ver Top 10','3-Cargar,generar o actualizar archivo Usuarios','0-Salir']
menu2=['1-Reset archivo Usuarios','2-Cargar nuevo arhivo Usuarios','3-Actualizar archivo Usuarios','4-Generar Usuarios aleatorio','0-Volver al Menu Principal']
i=1
while i!=0:
    for k in menu1:
        print (menu1[k],'\n')
    j=1
    i=imput('Por favor ingresar la accion a realizar acorde al numero correspondiente')
    if i==1:
        ...
    elif i==2:
        while j!=0:
            j=imput('Presione 0 para volver al menu principal')
    elif i==3:
        while j!=0:
            for k in menu2:
                print (menu2[k],'\n')
            j=imput('Por favor ingresar la accion a realizar acorde al numero correspondiente')
            if j==1:
                  ...
            elif j==2:
                  ...
            elif j==3:
                  ...
            elif j==4:
                  ...
            elif j==0:
                  ...
            else:
                  print ('Por favor ingresar un numero valido')
    elif i==0:
        ...
    else:
        print ('Por favor ingresar un numero valido')

Comment: If you're leaving those conditions blank, add `pass` to them. Those conditions need at least 1 statement inside them.
Comment: In your `if i==1` block you need some code or atleast `pass`. Same for other blank `if`, `elif` block
Answer: There are 3 main issues in your code.

Indent. Follow consistent indentation, and make sure you have at least 1 statement inside your conditions
You are iterating over the list elements, not over the indices. menu1[k] is invalid, because k is not an index. If menu1 = ['hi', 'hello'] then k can take on 'hi' and 'hello', not 0 and 1.
The function you call for taking input is misspelled as imput. If you're using python2, use raw_input, or input for python3.

Fixed your code.
#Programa Principal, desde aqui haremos el menu
menu1=['1-Jugar','2-Ver Top 10','3-Cargar,generar o actualizar archivo Usuarios','0-Salir']
menu2=['1-Reset archivo Usuarios','2-Cargar nuevo arhivo Usuarios','3-Actualizar archivo Usuarios','4-Generar Usuarios aleatorio','0-Volver al Menu Principal']
i=1
while i!=0:
    for k in menu1:
        print (k,'\n')
    j=1
    i=input('Por favor ingresar la accion a realizar acorde al numero correspondiente')
    if i==1:
        #Se inicia el juego
        pass
    elif i==2:
        #Se imprime el top 10 y vuelve
        #Aca va el programa de top 10
        while j!=0:
            j=input('Presione 0 para volver al menu principal')
    elif i==3:
        #Va a menuUsuarios
        while j!=0:
            for k in menu2:
                print(k, '\n')
            j=input('Por favor ingresar la accion a realizar acorde al numero correspondiente')
            if j==1:
                #Resetea Usuarios, se borra el anterior
                pass
            elif j==2:
                #Carga nuevo Usuarios, se borra el anterior
                pass
            elif j==3:
                #Permite crear un nuevo novUsuarios y realiza el apareo
                pass
            elif j==4:
                #Genera un Usuarios aleatorio, se borra el anterior
                pass
            elif j==0:
                #Vuelve al menu principal
                pass
            else:
                #Accion invalida
                print ('Por favor ingresar un numero valido')
    elif i==0:
        #Como no hace nada sale del programa
        pass
    else:
        #Accion invalida
        print ('Por favor ingresar un numero valido')


Swapping X and Y in a regression that contains a grouping predictor?

Question: Suppose I'm doing a linear regression and I want to investigate how the association between a predictor X and a response Y changes according to levels of a 2-level factor G. The model would look like this:
$$E(Y) = \beta_0 + \beta_1 G + \beta_2 X + \beta_3 (G \times X)$$
My question is, why do I get a different result for the t-test on $\beta_3$ if I swap X and Y in the above model? Conceptually, these tests seem to be addressing the same question: does the X-Y association change according to levels of G? Yet, in practice, I know the tests give different results.
My intuition is that the difference has something to do with whether X and/or Y are correlated with G, but I cannot think of a formal explanation. Does anyone have a suggestion? Thanks!
Comment: The linear interpolation of $E(Y_i |X_i, G_i)$ does not have to agree substantively with that of $E(X_i|Y_i, G_i)$. For example, if

$$ Y_i = \beta_0 + \beta_1 X_i + \beta_2 G_i + \beta_3 X_i G_i + \varepsilon_i $$

is the correct model then rearranging terms will show you that $E(X_i | Y_i)$ depends non-linearly on $G_i$, so there's no reason to think inference about the coefficient of $Y_i G_i$ in a linear model for $X_i$ should agree with that of $\beta_3$ above.
Comment: see this question and answer  http://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues/2700#2700
Comment: Hi @whuber, I thought it was sufficiently vague that it did not qualify as an answer, particularly since the two hypothesis tests often do agree. This is something that interests me and I actually spent some time on this trying to characterize situations where the two are almost guaranteed to disagree. I found some examples but not a characterization and then I got sidetracked. When/if I get a chance to come back to that, I'll post an answer. Maybe in the meantime someone else will post a good answer :)
Comment: @Macro That reads like a good answer to me :-)  (To be complete, you might also point out that the simpler model $Y_i=\beta_0+\beta_1X_i+\varepsilon_i$ *can* be rearranged into a linear dependence of $X$ on $Y$.)
Comment: Well yes, I understand that regressing X on Y is technically not the same as regressing Y on X, but it _is_ the same thing if you're only interested in the statistical test. Put differently, in a correlation test it doesn't matter what is X and what is Y. You will get the same _t_-statistic and _p_-value whichever way you flip them. My question is why this is no longer true as soon as you add a grouping variable.
Answer: Now that I've run a couple of tests in R, I think I understand the nature of the problem a little better, so I'll make an attempt at answering my own question. To rephrase the original problem, basically the test of interest boils down to comparing an association between two independent groups. The way I see it there are three approaches to this comparison:

Fisher's test for comparing independent correlations
Regressing $Y$ on $X$ with a grouping variable $G$ and testing the interaction (as above)
Regressing $X$ on $Y$ with a grouping variable $G$ and testing the interaction (as above)

Conceptually, all three tests ask whether some $XY$ association is significantly different in one group versus another. Having run many examples, however, I found that these three tests do not always agree, and sometimes even diverge dramatically. This seems to be something of a paradox to me.
Approaches 2 and 3 will only produce identical results if the $XY$ data are standardized within each group, prior to running the regression. This makes the regression analysis somewhat similar to Fisher's test (to my intuition, at least). For unstandardized data, the two regression tests usually agree but diverge more depending on two conditions:

Larger $Y$ variance in one group versus another
Moderate divergence of $XY$ correlation in the two groups

From what I've run, condition 1 seems to have an impact only to the extent that condition 2 is also an issue. When the two tests diverge strongly, one of the two models usually shows heteroscedasticity problems. The fastest way to spot something is wrong is by comparing the regression output to Fisher's test, though. I have added a picture of some data below, where the slopes differ dramatically between groups depending on whether you regress on $X$ or $Y$.
If someone could weigh in with some theoretical insight it would be most appreciated!
Comment: The issue has been explained in a comment to the question by @Macro: that your models 1, 2, and 3 are all different is made clear by writing them in a way that makes the nature of random variation explicit. This is no different than noticing that regressing $Y$ on $X$ differs from regressing $X$ on $Y$.


Is java:openjdk-8-jre an offical docker image

Question: I could pull java:openjdk-8-jre with docker pull java:openjdk-8-jre command. But I can't find it out in https://hub.docker.com/. I can't find a page for java like other official images, e.g. openjdk. When I went to https://hub.docker.com/_/java, it was redirected to https://hub.docker.com/_/openjdk. So I guess it's renamed with openjdk. Where can I find the related infos about java:openjdk-8-jre in official docker hub.
Answer: As of the are two more or less official java distributions - the OracleJDK and OpenJDK (there are as well some derivatives of OpenJDK). Thus you either pull from https://hub.docker.com//oracle-jdk or https://hub.docker.com//openjdk. The java:openjdk-8-jre is pre-split version on official repo and shouldn't e use as is outdated:
$ docker inspect --format='{{index .Created}}'  java:openjdk-8-jre
2017-01-17T00:53:21.516522468Z

$ docker inspect --format='{{index .Created}}'  openjdk:8-jre
2020-05-15T21:05:09.719485985Z

Note: To use Oracle JDK requires subscription.


How to separate out display and data mxgraph ports example

Question: I am working with mxgraph and using the mxgraph ports example as a starting point for my application. https://jgraph.github.io/mxgraph/javascript/examples/ports.html
The example passes the content of each cell through the value property of an mxcell object. The display content is an html label which is then rendered inside the cell. Example code below shows the creation of an draggable icon on the sidebar the parameters are graph, sidebar, label, image.
addSidebarIcon(graph, sidebar,
                '
Website

'+
                ''+
                '
'+
                'Browse',
                'images/icons48/earth.png');

The addSideBarIcon function then creates the vertex by passing it's label argument as the value property of the MxCell.
v1 = graph.insertVertex(parent, null, label, x, y, 120, 120);

I would like to be able to add a JSON object to store non display data along with the html label for display purposes in the value property of each cell. I would like to do this without modifying the underlying prototypes.
addSidebarIcon(graph,
    sidebar, {
        display: '
Process

' +
            '' +
            '
<select><option>Value1</option><option>Value2</option></select>
',
        data: {
            key1: val1
        },
    }

When I do this he code is interpreting the value as an html label. where as I would like it to interpret only value.display as the label. I'm not sure how to go about combining the html label and json data together and would love some suggestions.
Answer: You have the implementation of the addSidebarIcon in the same file...
When you find the:
v1 = graph.insertVertex(parent, null, label, x, y, 120, 120);

You have to use there not the "label", but extract the display value from your JSON.
Answer: You have to change the implementation of mxGraph.getLabel, which is called by mxGraph to display label.
graph.getLabel = function(cell) {
    // extract label from cell.value
    // and return a DOM node for html or string for plain text
    return createDOMNode(cell.value.display);
}

See also: mxGraph.getLabel


jquery get or post methods for drupal block module

Question: Is it possible to use jquery get or post methods to fetch data dynamic in a a drupal7.x block module. I am a newbie to drupal.
Here is my event_calendar.module file
      function event_calendar_help($path, $arg)
      {
          switch ($path)
          {
          case "admin/help#event_calendar":
          return '
'.  t("A block module that creates events and lists them in a event calendar") .'
';
      break;
          }
    }
     /**
    * Implements hook_block_info().
    */
    function event_calendar_block_info() {
      $blocks['event_calendar'] = array(
        'info' => t('Event calendar'),
        'cache' => DRUPAL_CACHE_PER_ROLE, //Default
      );
      return $blocks;
    }

    /**
    * Implements hook_block_view().
    *
    * Prepares the contents of the block.
    */
    function event_calendar_block_view($delta = '')
    {
      switch($delta)
      {
        case 'event_calendar':
          $block['subject'] = NULL;
          if(user_access('access content'))
           {
            $items = array();
            $basepath = drupal_get_path('module', 'event_calendar');
            $markup = '

                        Week :

                             -

Lane 1

Lane 2

Lane 3

Lane 4

';
      $block['content'] = array(
        '#markup'   => $markup,
        '#attached' => array
            (
            'css' => array($basepath . '/css/event_calendar.css',$basepath . '/css/smoothness/jquery-ui-1.8.16.custom.css'),
            'js'  => array($basepath . '/javascript/event_calendar.js',$basepath . '/javascript//jquery-ui-1.8.16.custom.min.js'),
            ),
        );

          }
return $block;
      }

    }

I want to know how to write functions inside above .module file to access some content dynamically through jquery get or post method?
Or should I use something like 'hook_menu' other than block?
Answer: For using get data by jquery ajax you need first seta menu link for listen to your ajax requst, and in callback of your menu you passed back a block or any data you want.
function yourmodule_menu () {
   $items=array();
   $items['youruniqepath'] =array(
        'title' => 'my menu',
        'description' => 'A menu link to handle ajax request',
        'page callback' => 'yourhandlerfunction',
        'access callback' => TRUE, //you can set it with your permission
      );
   return $items;
   }

and in yourhalderjust need pass a block :
function yourhandlerfunction () {
  //with any method you want get your block,
  // I suggest something like this
    $block = module_invoke('module_name', 'block_view', 'block_delta');
    print render($block);
}

and in your clientside get it with something like this
$.ajax({
    type: "POST",
    url: 'youruniqepath',
    //data: {}, you can also pass block name and act more dynamicly
    success: function (data){
        $('#yourplace').html(data);
    }
});


Notation convention for $\{1,\ldots,n\}$

Question: Is there any convention for a notational shorthand for the set $\{1,\ldots,n\}$ (defined as $\{k\in\mathbb{N} \mid k \le n\}$), where $n\in\mathbb{N}$, that the majority of mathematicians are familiar with?
I find that in some cases in which these sets appear often in the same expression, which can reduce readability, or at least aesthetic cleanness; using some sort of abbreviation would alleviate that.
Comment: In combinatorics it is sometimes written as $[n]$.
Comment: In combinatorial settings $[n]=\{1,2,\ldots ,n\}$ is commonly used.
Comment: Sometimes, $\overline{1,n}$ is used.
Answer: I don't know how popular this is but I've seen the convention:
$$[n]\equiv\{1,2,3,4,\ldots n\} $$
See for example:
http://www.math.cmu.edu/~lohp/docs/math/mop2013/combin-sets-soln.pdf
Answer: In homotopy theory, both $[n]$ and $\mathbf{n}$ are common and, to a lesser extent, $\underline{n}$.  None of this matters too much, as long as you define your choice of notation clearly in your writing.
Answer: It depends on the context, but a couple of equivalent formulations I've seen:

You could say $\{k\}_{k=1}^n$. I saw this often when considering sets of data points, like below, but I see no reason the notation couldn't extrapolate to any set.

$$\{(x_1,y_1) \; , \; (x_2,y_2) \; , \; ... \; , \; (x_n,y_n)\} = \{(x_i,y_i)\}_{i=1}^n$$

In combinatorics, apparently $[n]$ can be used to represent $\{1,...,n\}$ as touched on in the comments and by Archimedesprinciple.
Comment: In general the notation $\left\{ f(k) \right\}_{k = 1}^{n}$ is used to denote a Sequence rather than a Set per sae. There is no absolute way of defining a set but conventional analysis texts tend to use the notation that you have used.


Dynamic NSOutlineView data source

Question: So I have implemented a PXSourceList data source that pretty much is a duplicate of Apple's example of a NSOutlineView data source.
This is how it goes...
- (NSUInteger)sourceList:(PXSourceList*)sourceList numberOfChildrenOfItem:(id)item; {
    if (item == nil) {
        // item is nil so it's part of the very top hierarchy.
        // return how many sections we need.
        return 2;
    }
    else {
        if ([item class] == [TSFileSystemItem class] ) {
            return [item numberOfChildren];
            // if item isn't nil and it's a TSFileSystemItem, then return it's children.
        }
        if ([item class] == [TSWorkspaceItem class]) {
            return 2; // i don't know, random items.
        }
        else {
            NSLog(@"this is a special object.");
        }
    }
}

- (BOOL)sourceList:(PXSourceList *)aSourceList isItemExpandable:(id)item {
    if (item == nil) {
        return YES;
    }
    else {
        // if the number of children of the item is -1
        BOOL gibberhook = ([item numberOfChildren] != -1);
        return gibberhook;
    }
}

-(id)sourceList:(PXSourceList *)aSourceList child:(NSUInteger)index ofItem:(id)item {
    if (item == nil) {
        return [TSFileSystemItem rootItem];
    }
    else {
        return [(TSFileSystemItem *)item childAtIndex:index];
    }
}

- (id)sourceList:(PXSourceList *)aSourceList objectValueForItem:(id)item {
    if (item == nil) {
        return @"/";
    } else {
        if (item == [TSFileSystemItem rootItem]) {
            return PROJECT_FILES;
        }
        else {
            return [item relativePath];
        }
    }
}

The mysterious TSFileSystemItem is from here: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/OutlineView/Articles/UsingOutlineDataSource.html.
All of this is OK, except I want to divide my source list to have multiple sections (root cells). One displaying a file hierarchy (check) and the other...
Well the other is going to contain a NSMutableArray that I add items to from the other section. Sounds complicated? Better explanation. Click an item from section with file hierarchy, and it is added to the other section.
I have tried to solve this mess with the help of Apple's docs, but I still can't find a simple, efficient, stable way of making 2 sections with the functions I mentioned above. If only it was as easy as configuring a data source for UITableView...
Can anybody kindly help me out?
Comment: Did you notice `- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item` in the `NSOutlineViewDelegate`?
Answer: When the childrenForItem delegate method is called and item in nil, this requests the root of the tree.  If you return and array then the tree will have a root node for each element in that array.
- (NSArray *)childrenForItem:(id)item {
    if (item == nil) {
        return [self.rootTreeNode childNodes];
    } else {
        return [item childNodes];
    }
}

Dynamic NSOutlineView data source


Best methods for instrument appraisal?

Question: Looking for the best practices for appraising instruments - especially vintage? I realize there are TONS of factors that go into determining the value.
I ask, mainly, because I've acquired a 1970 Slingerland trap-kit from my father; from before the Slingerland family was bought out. The drums are not in very good condition but, I want to get an honest appraisal, obviously... Comparing against ebay pricing just ain't cutting it. :P
I've found this site: http://www.appraisersassociation.org/index.cfm?fuseaction=page.viewPage&pageID=741&nodeID=1, and some others. But, appraisers can be hundreds of miles away. Hoping not to pay through the nose for it either... Why is this so hard?
Comment: If looking at eBay and reverb.com sales won't suffice I'm curious about why you are looking for an authoritative value on the kit. For insurance, you usually can name a replacement cost, which would be what you'd have to pay to buy it again used like on eBay. For setting a selling price, it's what the market will bear and now you're back to eBay and reverb.com. Some other reason?
Comment: I can't believe I've been playing drums, on & off, for more than 40 years & had to Google what a trap-kit was :/ Ref, for anyone else who didn't know - http://www.sweetwater.com/insync/trap-kit/
Comment: I've removed the request for people or places, as that would be off topic here. If there is a way to answer the best practices question, please focus on that. That said, Todd has a good point...
Comment: Honestly, I don't even want to sell it. Just curious as to the actual, stand-alone value... I've wondered about how to appraise basses that I've owned or passed in my travels (I'm a bassist,) but getting this kit has become the catalyst to instigate finding the answer.
Answer: When it comes to selling something, it's worth what people will pay for it. Unfortunately, people don't make purchasing decisions based solely on the cold hard facts. There isn't a formula that you can use.
So, how can you get a ballpark figure? Unfortunately, I think your best bet is comparison. Remember that your buyer probably doesn't have any sentimental attachment, so it may be worth less to them than you. Ideally, you'd compare it to an identical item, but that will be very difficult to find. So, you need to be looking at other kits. Why would someone buy yours instead of the other ones?
If I were trying to sell the thing, I'd make a spreadsheet of similar instruments, and the prices they sold for. I'd then pick a ballpark price, and if I felt I was happy with getting that, I'd put it on the market. If it doesn't sell, I might reduce the price after a couple of weeks. If no one bites, it's obviously not worth that much. If it goes very quickly, it was probably underpriced, but I'd still be happy with the amount I got. Don't waste time worrying about lost money; it's not going to change anything.
This isn't a precise approach , but it should get you somewhere. I would not use a professional appraiser unless I was pretty sure it was worth a couple of grand at least. Otherwise you'll probably spend more selling the thing than it's worth. And, at the end of the day, you can say it's worth $x, but if no one will actually pay you, is it really worth that much?
Answer: I see you are from the US. I know the famous Norm Rare Guitar shop does appraisals mostly for guitars but a quick email could get an answer whether they would be comfortable to do one for drums.
SEE MORE HERE.
Comment: Yes, I'm from the US. Hadn't considered inundating any or all appraisers with the old "Hey, what do you think this is worth?" just to see what I get back.....

Best methods for instrument appraisal?


ListView.builder items not getting aligned in center inside a container with scroll direction horizontal

Question: This is my Widget:
<code>// the category selector widget

class CategorySelector extends StatefulWidget {
  const CategorySelector({Key? key}) : super(key: key);

  @override
  _CategorySelectorState createState() => _CategorySelectorState();
}

class _CategorySelectorState extends State<CategorySelector> {
  // categories
  List<String> categories = ['Messages', 'Online', 'Groups', 'Requests'];

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.blue,
      height: 80,
      alignment: Alignment.centerLeft, // FIXME: doesn't work
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20.0),
        child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: categories.length,
          itemBuilder: (context, index) {
            return Text(categories[index]);
          },
        ),
      ),
    );
  }
}
</code>
Current Output

The issue is that ListView.builder items not getting aligned in the center inside a container with scroll direction horizontal. I want to align the ListView items in the center left. May I know how can I do it?
Comment: Try wrapping `Text` with a `Center` widget.
Comment: So the ListView took the height of the container and when I was trying to center it, it was already at the center because of that same height, hence I needed to align the content in the center instead?
Comment: @esentis Thanks! It worked. May I know the reason that why it happened?
Comment: The starting point of the widgets are top left, that's where `Text` starts in your `ListView`. I always do sanity check of widgets by wrapping the items with `Container` and giving them a distinct color, like red, to see where they are actually aligned. This helps to get a better grasp how widgets are aligned.
Answer: As I know, ListView always get the full size of her parent height/width depends on scrollDirection. That's we need to define listview height/width. For yours, scrollDirection are horizontal, so the solution is
<code>      Container(
        color: Colors.blue,
        height: 80,
        alignment: Alignment.center, // FIXME: does it work for you?
        child: Container(
          height: 14,
          child: ListView.builder(
            shrinkWrap: true,
            scrollDirection: Axis.horizontal,
            itemCount: categories.length,
            itemBuilder: (context, index) {
              return Text(categories[index], style: TextStyle(fontSize: 14),);
            },
          ),
        ),
      )
</code>


Best practice to update dataframe columns with multiple conditions and joins

Question: Let's say I have 2 dataframes:
df1 = pd.DataFrame([["SAP       ", 1, 'CHANGE_NEEDED', 10, 11],
                       ["SAP123", 1, 'CORRECT_VALUE', 10, 11],
                       ["NOTSAP123", 1, 'INCORRECT_VALUE', 10, 11],
                       ["NOTSAP456", 1, 'INCORRECT_VALUE', 10, 11]],
                      columns = ["RCD_ORIG_CD", "VEN_ID", "VLC_VEN_ID", "CNTRY_CD", "CO_CD",])
df2 = pd.DataFrame([["SAP_CORRECT_VALUE", 1, 'CORRECT_VALUE', 12, 13],
                        ["SAB123",1, "THIS_WONT_BE_USED", 12,13],
                        ["NOTSAP123", 1, 'CORRECT_VALUE', 10, 11],
                        ["NOTSAP457", 1, 'INCORRECT_VALUE', 10, 11]],
                        columns = ["RCD_ORIG_CD", "VEN_ID", "VLC_VEN_ID", "CNTRY_CD", "CO_CD",])

df1:
  RCD_ORIG_CD  VEN_ID       VLC_VEN_ID  CNTRY_CD  CO_CD
0  SAP              1    CHANGE_NEEDED        10     11
1      SAP123       1    CORRECT_VALUE        10     11
2   NOTSAP123       1  INCORRECT_VALUE        10     11
3   NOTSAP456       1  INCORRECT_VALUE        10     11

df2:
         RCD_ORIG_CD  VEN_ID         VLC_VEN_ID  CNTRY_CD  CO_CD
0  SAP_CORRECT_VALUE       1      CORRECT_VALUE        12     13
1             SAB123       1  THIS_WONT_BE_USED        12     13
2          NOTSAP123       1      CORRECT_VALUE        10     11
3          NOTSAP457       1    INCORRECT_VALUE        10     11

I want to update the records of df1 based on the conditions below (pseudocode-ish for easier understanding). You can imagine this logic as it needs to be applied for all the records in df1 one by one (obviously that would be really slow):
if df1["RCD_ORIG_CD"].str[:3] == "SAP":
    if df1["VEN_ID"] == df["VEN_ID"] and df2["RCD_ORIG_CD"].str[:3] == "SAP":
        df1["VLC_VEN_ID"] = df2["VLC_VEN_ID"]
        if df1["RCD_ORIG_CD"] == "SAP    ":
            df1["RCD_ORIG_CD"] = df2["RCD_ORIG_CD"]
    else:
        df1["VLC_VEN_ID"] = "        "
elif df1["RCD_ORIG_CD] == df2["RCD_ORIG_CD"] and df1["CNTRY_CD"] == df2["CNTRY_CD"] and
     df1["CO_CD"] == df2["CO_CD"] and df1["VEN_ID"] == df2["VEN_ID"]:
    df1["VLC_VEN_ID"] = df2["VLC_VEN_ID"]
else:
    df1["VLC_VEN_ID"] = "        "

I am curious what would be the best practice for this using pandas. What I was thinking about as possible options:

joining the 2 dataframes based on different parts of the above conditions. Like a left join on VEN_ID, then updating the VLC_VEN_ID column when different conditions met.
actually going record by record with iterrows().

This is not a question which expects a snippet of code as an answer (obviously it is also welcome if you want to), more like an explanation of how a problem like this should be approached.
Answer: Couple of ideas that can help:

Joining df1 and df2 would definitely be the preferred way here. Using df1.merge(df2, on=['RCD_ORIG_CD', 'VEN_ID', 'VLC_VEN_ID'], ...) (or similar) is clearer and more error-prone than matching them manually column-by-column.
If different parts of the input dataframe require different processing, you can split them and later pd.concat them together.
You can also conditionally create columns in a vectorized fashion using np.where(condition, value_if_true, value_if_false), or similar pandas methods (.where() or .mask())

A combination of these should be both more performant and easier to read.


How to get and process json data given by ajax request in prototype?

Question: This is my first attempt to prototype. I want to initiate an AJAX request that should get a JSON response and alert that. I have done following so far.
JS:

HTML:
Testing AJAX

JSON source:
function ajaxresponse() {

    // Data
    $data = array("New York", "New Yorkshire", "New Jersey");
    // encode and return json data
    echo json_encode( $data );
}

On clicking "Testing AJAX" link I am getting following result in alert box:
no JSON object

Any idea ?
Thanks
Answer: I dont see any second variable passed to onSuccess handler in prototype. Look here. There is only transport object. So this should help:
...
onSuccess: function(transport) {
      var json = transport.responseText;
      alert(json ? Object.inspect(json) : "no JSON object");
 },
...

Comment: oh. ok. It is not alerting now `no JSON object`.. But it is alerting me whole file with js code in response.. It does not look json and I want to get only cities in json.. Posting current result in next comment:
Comment: `

["New York","New Yorkshire","New Jersey"]`
Comment: its weird, what is the result of alert(transport.responseText); and alert(transport.headerJSON)?
Comment: `null` for `alert(transport.headerJSON)` and whole page html with js and json for `alert(transport.responseText)` like my 2nd comment above....
Comment: I am not a PHP expert, but I have a bad feeling that you have both parts in the same file. If not, post whole files PHP and client HTML/JS.
Comment: You realy should split it. Its a good habbit to keep backend and frontened separated. AFAIK when php is processed all content is outputed (echoed or printed out). Thats why you can see/use your HTML/js for the first time. For testing purposes you can add some parameter to the url and skip HTML/JS block in your PHP file if is present and echo out the cities array othewise.


How to programmatically set a node field as empty?

Question: I have some nodes with the following fields:

Title
Body
Image

I want to programmatically set the body field as empty.
This is the code I used.
$this_node = node_load($nid);
$this_node->body = array();
node_save($this_node);

This code causes the following exception.

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '104418' for key 'PRIMARY': INSERT INTO {node} (nid, vid, type, language, title, uid, status, created, changed, comment, promote, sticky, tnid, translate, uuid) VALUES (placeholders and their values) in drupal_write_record() (line 7170 of /var/www/rotary.local/docroot/includes/common.inc).

How can I set an empty body field?
I don't want to use field_attach_update() for caching purposes.
Answer: To remove the value from you field you should do like this before saving it:
$this_node->body[$this_node->language] = array();

Comment: After testing the code with your suggestion I found that it still gives the error. However I am able to save the node using `field_attach_update`. What could be causing this?
Comment: @DipakYadav - That seems very strange, what error do you get?
Comment: The error was being caused because of a whole lot different reason.
Answer: To make body field empty you should implement hook_node_presave() and inside this, use-
   $node->body[LANGUAGE_NONE][0]['value'] = '';


If $f=f(x(s),y(t))$, then $\frac{\partial f}{\partial t}=0$ is wrong?

Question: enter link description here
You may see through the link that someone pointed out that if$$f=f(x(t),y(t))$$then$$\cfrac{\partial f}{\partial t}=0$$So now i want to challenge this by giving an example:
Let $f=f(x,y)$, where $x=x(t)$,$y=y(s)$. Because $f$ is independent of $t$, so we have $$\frac{\partial f}{\partial t}=0\qquad(1)$$
Now, given that $x=x(t)=2t^2$, $y=y(s)=3s$, then f is given by:$$f=f(x,y)=xy\qquad(2)$$
Substitute $x=x(t)=2t^2$, $y=y(s)=3s$ into (2), we have$$f=6t^2s\qquad(3)$$It follows that $$\frac{\partial f}{\partial t}=12ts\qquad(4)$$Equation(1) and Equation (4) are not equal, what is wrong?
Answer: You are not understanding what people said in answering your other question.
The root of your confusion is that you write things like $f=f(x(t), y(t)$ without making clear what this means.
The true story is that there is a function $f:\mathbb{R}^2\to\mathbb{R}$, say $(x,y)\mapsto f(x,y)$.
Then there are two functions $x:\mathbb{R}\to\mathbb{R}$, say $t\mapsto x(t)$: and $y:\mathbb{R}\to\mathbb{R}$, say $t\mapsto y(t)$.
From these we can form a new function, $F:\mathbb{R}\to\mathbb{R}$, given by $t\mapsto  F(t)=f(x(t), y(t))$.
You can then sensibly partially differentiate $f$ with respect to $x$ and $y$; differentiate each of $x$ and $y$ with respect to $t$;  and differentiate $F$ with respect to $t$.
The Chain Rule then says
$$
\frac{dF}{dt}=\frac{\partial f}{\partial x}\frac{dx}{dt} +\frac{\partial f}{\partial y}\frac{dy}{dt}.$$
Warning
It leads to endless confusion to use the same letter for both $f$ and $F$. I know that people do this, but they are confusing values with functions.
In the "example" you give you confuse things further by jumping from $f=f(x(t),y(t))$ to $f=f(x(s),y(t))$ as though this made any sense. Here you've got yet another function, $\phi:\mathbb{R}^2\to\mathbb{R}$ given by $\phi(s,t)=f(x(s),y(t))$. Using the Chain Rule correctly will remove your confusions/supposed contradictions.
Comment: Oh my god, i completely understand now, so clear. But i still don't know the value of (1) $\frac{\partial f}{\partial t}$ if $f=f(x(t),y(t))$, is it really zero?(2) $\frac{\partial F}{\partial t}$ for $F(t)=f(x(t),y(t))$, is it also zero?
Comment: (1)$\frac{\partial f}{\partial t}$ makes no sense. (2) $\frac{\partial F}{\partial t}$ is just $\frac{d F}{d t}$, as there's only one variable, and in general it won't be zero.
Comment: Ok thank you very much
Answer: $f$ is not independent of $t$.
$f$ depends on $x$ and $y$, both of which in turn depend on $t$. So $f$ depends on $t$.
Remember the chain rule:
$$f_t = f_xx_t +f_yy_t$$
Comment: Is it true that if $f=f(x(t),y(t))$, then $\frac{\partial f}{\partial t}=0$?
Comment: Only if $f$ is constant
Answer: $t$ affects $f$. in other words $f$ and $t$ is dependent since $f$=$f$($x$($s$),$y$($t$)) hope this helps! :)
Comment: When $f=5t^2$, please evaluate $\frac{\partial f}{\partial t}$, is it zero? Or $10t$? thanks ^_^
Comment: I think you have not understood what i said,,maybe when you study more, you will be able to understand..However you can be more kind.
Comment: I am so sorry to let you feel i am rude, but i really want to know the answer of the question that i asked you. Also that i know completely what you have said, but i still confuse about the value of $\frac{\partial f}{\partial t}$. I just know $\frac{df}{dt}$ = 10t. What about $\frac{\partial f}{\partial t}$?


How to test whether a sample of data fits the family of Gamma distribution?

Question: I have a sample of data which was generated from a continuous random variable X. And from the histogram I draw using R, I guess that maybe the distribution of X obeys a certain Gamma distribution. But I do not know the exact parameters of this Gamma distribution.
My question is how to test whether the distribution of X belongs to a family of Gamma distribution? There exists some goodness of fit tests such as Kolmogorov-Smirnov test, Anderson-Darling test, and so on, but one of the restriction when using these tests is that the parameters of the theoretical distribution should be known in advance. Would anyone please tell me how to solve this problem?
Comment: Perhaps I am missing something, but if you already know of a test for testing the fit of the distribution and all you need to know are the values of the theoretical distribution, then you could simply use the maximum likelihood estimators of the parameters of the gamma distribution on your data to get estimates of the parameters. You could then use those estimates to define the theoretical distribution in your test.
Comment: David, thank you for your answer. The answer is also what i have been thinking about, but i am not sure whether there is some theories which can support this idea, could you answer it for me?
Comment: If you use R, you may be interested in taking a look at the [fitdistrplus package](http://cran.r-project.org/web/packages/fitdistrplus/index.html), which has facilities for doing this sort of thing.
Answer: I think the question asks for a precise statistical test, not for an histogram comparison. When using the Kolmogorov-Smirnov test with estimated parameters, the distribution of the test statistics under the null depends on the tested distribution, as opposed to the case with no estimated parameter. For instance, using (in R)
<code>x <- rnorm(100)
ks.test(x, "pnorm", mean=mean(x), sd=sd(x))
</code>
leads to
<code>        One-sample Kolmogorov-Smirnov test

data:  x
D = 0.0701, p-value = 0.7096
alternative hypothesis: two-sided
</code>
while we get
<code>> ks.test(x, "pnorm")

        One-sample Kolmogorov-Smirnov test

data:  x
D = 0.1294, p-value = 0.07022
alternative hypothesis: two-sided
</code>
for the same sample x. The significance level or the p-value thus have to be determined by Monte Carlo simulation under the null, producing the distribution of the Kolmogorov-Smirnov statistics from samples simulated under the estimated distribution (with a slight approximation in the result given that the observed sample comes from another distribution, even under the null).
Comment: Xi'an, your answer is exactly what i worried about. You mean that "When using the Kolmogorov-Smirnov test with estimated parameters, the distribution of the test statistics under the null depends on the tested distribution". However, we don't know the distribution of X, more precisely, we don't know the parameter of the distribution of X under the null hypothesis, hence the distribution of test statistic,therefore,we use monte carlo. Would you have some other ways of solving it by not using monte carlo to get the P value? Thank you
Comment: (+1) I don't get well why it is correct to simulate samples under the estimated distribution. I would have think that we needed a prior for the parameters, and sample from all the possible distributions... can you explain a little bit more?
Comment: To take into account the fact that "the observed sample comes from another distribution even under the null", wouldn't it be appropriate to bootstrap the sample, re-estimating the parameters at each replicate?
Comment: @Elvis (1): this is classical statistics, not a Bayesian resolution of the goodness of fit problem. For distributions with location-scale parameters, the choice of the parameters used to simulate the simulated samples does not matter.
Comment: @Elvis (2): Again something I just discussed with my students! Bootstrap would help in assessing the behaviour of the Kolmogorov-Smirnov distance under the true distribution of the data, not under the null! The Fisher-Neyman-Pearson principle is that what matters is the behaviour of the Kolmogorov-Smirnov distance under the null, so that it is rejected if the observed distance is too extreme wrt this distribution under the null.
Comment: @user8363: I just taught Monte Carlo tests to my students and they have the same problem! When I test for X to be gamma, I do not know whether or not X is gamma. I use the Monte Carlo experiment to calibrate the Kolmogorov-Smirnov distance under the null hypothesis, i.e. when assuming a gamma distribution. This simulation is unrelated with X. It is only when I have an approximation of the distribution of the Kolmogorov-Smirnov distance under the null that I check whether or not the observed Kolmogorov-Smirnov distance is likely or not.
Answer: Compute MLEs of the parameters assuming a gamma distribution for your data and compare the theoretical density with the histogram of your data. If the two are very different the gamma dstribution is a poor approximation of your data. For a formal test you could compute, for example, the Kolmogorov-Smirnoff test statistic comparing the best fitting gamma distribution with the empirical distribution and test for significance.
Comment: +1, this is a solid answer.  However, I would suggest examining a qq-plot against the theoretical gamma rather than a histogram--it will be easier to assess for deviations.
Comment: The problem is that KS test assumes the theoretical distribution to be given beforehand, not estimated from the data. Xi'an (partially) answered to that point...
Comment: @Elvis:  I do not think the exact derivation is possible in the case of the gamma distribution. The cdf itself is not available in closed form. Further, the fact that the shape parameter is neither scale nor location means that there is a different distribution for each value of the shape parameter...
Comment: you mean that we firstly use this sample of data to obtain an MLS estimater and use the value of MLS estimater in Gamma distribution, and then compare the data with Gamma distribution(with estimated parameter) by using KS test?
Comment: Elvis, would you please tell me how to solve the problem that when the parameter of the theoretical distribution is unknow and need to be estimated. In this cas, how can one use KS test to get a relatively accurate judgement of the hypothesis, thank you!
Comment: @user8363 I think Xi'an answer gives you a good way to do that. You can try to derive the exact distribution of the statistics when you estimate the parameters, this is a difficult task but it might be tractable.
Comment: Elvis - Is that true? If so, nice catch. Link/reference?
Comment: I didn't intend to be affirmative, I had no idea! The arguments of @Xi'an seem almost definitive. User8363, you can use the MC method described in Xi'an answer to get "an accurate judgement", it's not because it is based on random sampling that it is inaccurate.


HTML email in Python

Question: I'm trying to send an HTML email using smtplib. But I need the HTML content to have a table that is populated using values from a dictionary. I did look at the examples on the Python website. But it doesn't explain how to embed Python code within HTML. Any solutions/suggestions?
I also looked at this question. Can I just format it this way?
.format(dict_name)
Comment: possible duplicate of [Sending HTML email using Python](http://stackoverflow.com/questions/882712/sending-html-email-using-python)
Answer: From your link:

Here's an example of how to create an HTML message with an alternative
plain text version: 2

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "firstname.lastname@example.com"
you = "email@example.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\

Hi!

       How are you?

       Here is the link you wanted.

"""

and the sending section of it:
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

Edit 2022: for new comers, please advise with python's latest stable release docs.
Answer: What you need is a template engine. That is, you need a python library that reads a file written in both HTML and code, interprets the code written within the HTML file (e.g. code that retrieves values from a dictionary), and then produces an HTML file for you.
The python wiki seems to have some suggestions
Comment: A template engine is not required.
Comment: A template engine may not be required but does allow the dev to separate the responsibilities of the model (python code with dictionary) and the view (template for the email).

HTML email in Python


Sub-Consulta MYSQL (SELECT MIN)

Question: Eu tenho este codigo, mas o que eu quero é o SELECT MIN do "n_remadores" e só apenas o número min. Queria o clube com menos remadores. Ajudinha? :)
SELECT (clube.nome) AS clube,
       COUNT(remadores.cod_remador) AS n_remadores
FROM clube,
     remadores
WHERE clube.cod_clube=remadores.cod_clube
GROUP BY clube.nome
HAVING COUNT(remadores.cod_remador) >= ALL
  (SELECT COUNT(remadores.cod_remador) AS n_remadores
   FROM clube,
        remadores
   WHERE clube.cod_clube= remadores.cod_clube
   GROUP BY remadores.nome)

Comment: Qual o banco de dados ?: e o que exatamente vocÊ quer ? o min da sub query ? ou o apenas o numero min do resultado ?//
Comment: MySQL. Quero o min do resultado (n_remadores)
Comment: você fala uma linha com o nome do clube e o min de remadores.cod_remador ... ou você esta tentando pegar uma linha para cada clube com o valor min da sub query ?
Comment: SELECT (clube.nome) AS clube,
       COUNT(remadores.cod_remador) AS n_remadores
FROM clube,
     remadores
WHERE clube.cod_clube=remadores.cod_clube
GROUP BY clube.nome                                                                          //Quero o resultado mínimo desta consulta!
Comment: Qual bd a solução muda conforme o bd , top sqlserver, limit mysql , rownum oracle etc.
Comment: phpMyAdmin, MySql
Answer: Use tabelas temporárias.
SELECT min(cod_remador) FROM
(SELECT (clube.nome) AS clube,
       COUNT(remadores.cod_remador) AS n_remadores
FROM clube,
     remadores
WHERE clube.cod_clube=remadores.cod_clube
GROUP BY clube.nome
HAVING COUNT(remadores.cod_remador) >= ALL
  (SELECT COUNT(remadores.cod_remador) AS n_remadores
    FROM clube,
        remadores
   WHERE clube.cod_clube= remadores.cod_clube
   GROUP BY remadores.nome)) AS Temp

Sub-Consulta MYSQL (SELECT MIN)