Python - Accept POST (raw body) data

Question: I am completely new to Python. I am using GitLab which offers system hook feature wherein i can specify a URL and it will send event details in the form of JSON POST data. When i create a RequestBin URL and provide that URL in GitLab's system hook, then in case of any event such as project creation, it sends the event details and i can see the same in RequestBin as shown in the snapshot below.
Now, i want to fetch this JSON data in some variable so i can process it as per my need but i'm not sure how to do read that data.
I've seen some posts which explain how to read JSON data but as you can see below in the screenshot, the FORM/POST PARAMETERS is showing as None. It's the raw body that contains all the details (in JSON format):

I have tried reading the data using Java and it works with the code shown below:
String recv;
String recvbuff="";
BufferedReader buffread = new BufferedReader(new InputStreamReader(request.getInputStream()));

while ((recv = buffread.readLine()) != null)
recvbuff += recv;
buffread.close();

System.out.println(recvbuff);
out.println(recvbuff);

Looking for something similar in Python.
Comment: Voting to close as you have no specific question
Comment: So if I understand correctly you are looking for a way to set up a simple Python server to process some JSON-POST requests? I would suggest CherryPy for that, it's pretty easy to set up some functions accepting calls with JSON http://docs.cherrypy.org/en/latest/basics.html#dealing-with-json
Comment: Is CherryPy an alternative to Apacher server? Also, the request is in JSON format but it's being sent as raw data so i'm not really sure whether what you've suggested will work.
Comment: @AlastairMcCormack: How come there is no specific question? It's written in the subject itself i.e., to receive raw data from POST request. The problem is i'm not aware how to accept data in Python. I did it using Tomcat + JSP + jq + shell but that doesn't looks like a good approach so i wanted to try using Python.
Comment: Python doesn't handle HTTP requests, pe se, frameworks/modules/libraries do. You've updated your question with some incomplete Flask code but not shown how you're testing it. Your question is too broad, too haphazard, too scattergun and too vague to able to write a good answer. As someone with a SO reputation as high as yours, you should be asking good questions already, but for the benefit of others and you,  please see: http://stackoverflow.com/help/how-to-ask
Comment: Stick with one framework, like Flask, and get to understand it fully rather than jumping from one to the next. Use simple tools like `curl` to test it and try to understand the underlying technologies like HTTP and Python before moving on. I fear that you're trying to run before you can walk
Comment: I agree with what you've said but since i am new to it, i wanted to know what framework should i stick to and whether that would suffice what i'm looking for. Also, i'm not sure whether what Apache provides (for my need i.e., web server functionality. Looks like the framework will but not sure) will be take care by the frameworks such as Flask besides getting the content. Apologies for my ignorance.
Comment: @AlastairMcCormack: Thanks for the inputs about my original post and apologies for the same. I hope my latest edit is better.
Answer: I would suggest using CherryPy. It's a neat Python library that allows you to build a simple webserver application, it's fits pretty nicely in your use case: it can easily accept JSON requests (http://docs.cherrypy.org/en/latest/basics.html#dealing-with-json).
If you write a file called myserver.py with the following code:
#!/usr/bin/python3
import cherrypy

class Root(object):
    @cherrypy.expose
    @cherrypy.tools.json_in()
    def index(self):
        data = cherrypy.request.json
        # You can manipulate here your json data as you wish
        print(data['name'])

if __name__ == '__main__':
    cherrypy.quickstart(Root(), '/')

You can simply launch the server with the command line:
python3 myserver.py

And test it with the following curl command:
curl -H "Content-Type: application/json" -POST http://127.0.0.1:8080 -d '{"name": "test", "path": "/"}'

You will then see test printed in your server log.
Comment: I tried your suggestion and _your_ curl command works but when i provide this URL to my application's web hook, the log doesn't show anything. As i mentioned in AlastairMcCormack's comment, i have successfully tested the web hook using Java but there too many tools involved and i wanted a _better_ way to do the same job. :( The response is definitely sent as JSON (as shown in screenshot) by the application but it's as raw data (again as shown in the screenshot). So, may be i have to read all  the data that is sent and not just JSON body (which doesn't contain the actual output).
Comment: Well is your application running the webhook on the same server as your myserver.py file? Because 127.0.0.1:8080 is a local address and cannot be accessed from outside your local network. If you want to bind your server IP address just look at the second answer in https://stackoverflow.com/questions/115773/how-do-i-configure-the-ip-address-with-cherrypy
Comment: Yes, the application is running on the same IP and i have tried using both public and private IP but to no avail.
Comment: There isn't even a line showing that there was some kind of request? What if you use ```nc -l 8080``` instead of myserver.py, do you see any sign of the request? If not (and curl works) it looks as if your webhook doesn't work, maybe your Java code gets it's information in another way?
Comment: There will not be any such kind of request because i am not sending any request; it's the GitLab application that triggers the hook when any event happens in it such as project creation, project deletion etc. I have updated my post with the Java code that works. Hope that might give you some clue as to what i'm trying to convey.
Answer: Your Flask application doesn't return any data so you're not going to see anything returned. You need to return something like:
return "test data"

Your screenshot is only showing the request not the response. You sent no form encoded parameters, which is why it's showing "None".
The correct Content-type for JSON is: application/json
Comment: I'm not sure what you meant when you mentioned about screenshot showing _only_ the response. I created a link in RequestBin and assigned it in the web hook of my application. Whenever any event is triggered such as project creation etc., it sends details regarding that event in the form of JSON and that's sent as raw data and it's that data which i want to accept in some variable so that i can process it.
Comment: Please re-read my answer. It says "only shows the **request** not the response". I don't how how RequestBin works but all the elements of the screenshot are contexts of a request **not a response**

Python - Accept POST (raw body) data


Solve simple indefinite integral using basic table

Question: I am trying to find the indefinite integral of $\frac{1}{\sqrt{3x^2-1}}$ using the table of basic indefinite integrals (no substitution or integration by parts). I considered using the formula primitive of $\frac{1}{\sqrt{a^2-x^2}} = \arcsin\bigl(\frac x a\bigr) + K$ with $a$ greater than $0$ for this case but the a is negative and there is a $3$ multiplying the $x^2$. Can someone help me solve this?
Thank you!
Answer: It seems like based on the results, trig functions (and their inverses) are not going to suit you well. This requires the knowledge of Hyperbolic Functions (Hyperbolic Sine, Hyperbolic Cosine etc) and their inverses. As a general rule, we find that for the inverse Hyperbolic Cosine:
$$\frac{d}{dx}\cosh^{-1}ax=\frac{a}{\sqrt{(ax)^2-1}}$$
Thus, the solution is given as
$$\int\frac{1}{\sqrt{3x^2-1}}dx=\frac{1}{\sqrt{3}}\cosh^{-1}(\sqrt{3} x)$$
Answer: $\frac{1}{\sqrt{3x^2-1}}=\frac{1}{\sqrt3} \frac{1}{\sqrt{x^2-\frac{1}{3}}} $ and you need formula $\frac{1}{\sqrt{x^2-a^2}} = \log(x+\sqrt{x^2-a^2}) + K$


Is there a simple abstract reason why a profinite group is an inverse limit of finite groups?

Question: Let $G$ be a profinite group (defined as a Hausdorff, compact, totally disconnected topological group). Suppose you know that as a profinite set, it's an inverse limit of finite sets. Is there an abstract category-theoretic way to deduce $G$ is an inverse limit of finite groups, or must the proof have more content?
I know how to prove it using more content, but I've been advised (if I understood correctly) that there's a simple abstract reason. I can't work out what it is.
Comment: @AlexKruckman: There is indeed, but it is in Chapter 6, specifically section VI.2.  In particular VI.2.8 and VI.2.9 give some (fairly complicated) syntactic criteria for the two senses of "profinite algebra" to agree.
Comment: There is a lot of information about when the two senses of  "profinite algebra" agree in Johnstone's book *Stone Spaces*, Chapter 8 if I remember right.
Comment: Welcome to [math.se] SE. Take a [tour]. You'll find that simple "Here's the statement of my question, solve it for me" posts will be poorly received. What is better is for you to add context (with an [edit]): What you understand about the problem, what you've tried so far, *etc.*; something both to show you are part of the learning experience and to help us guide you to the appropriate help. You can consult [this link](https://math.meta.stackexchange.com/q/9959) for further guidance.
Comment: @EricWofsey Thanks for correcting my reference! I didn't have my copy of the book close to hand.
Answer: As evidence that you cannot expect an "abstract nonsense" proof of this, the corresponding statement for Jónsson-Tarski algebras is false.  A Jónsson-Tarski algebra is a set $X$ together with a binary operation $w:X\times X\to X$ and a pair of unary operations $p,q:X\to X$ such that $w$ and $(p,q):X\to X\times X$ are inverse to each other.  So, a topological Jónsson-Tarski algebra can be described as a topological space $X$ together with a homeomorphism $X\times X\to X$.  In particular, for instance, $\{0,1\}^\mathbb{N}$ is a profinite set which admits a topological Jónsson-Tarski algebra structure.  However, $\{0,1\}^\mathbb{N}$ cannot be an inverse limit of finite Jónsson-Tarski algebras, since a finite Jónsson-Tarski algebra has at most one element (if $X$ is a finite set with more than one element then $|X\times X|\neq |X|$!).
So, any proof of this statement for groups is going to need to use something special about groups beyond just that they are a type of algebraic structure.
(As Alex Kruckman commented on the question, you can find more examples and discussion of this question in general in Peter Johnstone's book Stone spaces, specifically in section VI.2.  In particular, VI.2.8-9 explores some sufficient conditions on a variety of algebras under which you can generalize the usual argument for profinite groups to show that inverse limits of finite algebras coincide with topological algebras whose underlying space is a profinite set.)


Adicionar mirror/espelho de repositório Debian 9 Stretch

Question: Qual a forma de incluir o http://ftp.br.debian.org/debian/ na minha sources.list do Debian 9 (Stretch)?
Eu preciso incluir com o contrib non-free também.
Eu preciso que seja explicado o porquê também, pois as URLs da sources.list (/etc/apt/sources.list) não são nada iguais às do espelho FTP online.
Exemplo:
URL: http://ftp.br.debian.org/debian/dists/stretch/non-free/
sources.list [Não está certo, só um exemplo mesmo]: deb-src http://ftp.br.debian.org/stretch stretch main contrib non-free
Qual a forma correta de incluir o espelho brasileiro do Debian 9?
Answer: Consegui achar neste link.
deb http://ftp.br.debian.org/debian stretch main contrib non-free
O que é quase igual ao que eu havia postado na pergunta, mudando somente a primeira palavra stretch para debian.


Mean Value Theorem Integrals

Question: Let $f,g:[a,b]\to\mathbb{R}$ be smooth and integrable. Then, there exists an $x_0\in[a,b]$ with
$$ \int_a^b f(x)g(x)dx=f(x_0)\cdot\int_a^b g(x)dx.$$
Is there any way of approximating $x_0$, without evaluating $\int_a^b f(x)g(x)dx$?
We may assume $g$ to be positive and monotone increasing and $\int_a^b g(x)dx$ to be known.

Setting $g(x)\equiv1$, we obtain
$$ \int_a^b f(x)dx=f(x_0)\cdot(b-a),$$
so finding this $x_0$ numerically may be a strong tool in approximating any integral?
Comment: @Thomas I'm sorry but I think you're not being fair. The question has not changed at all! Okay, I forgot to mention continuity and you thankfully pointed that out. I am indeed grateful because it was my mistake! But the question has always been _how can I find/approximate $x_0$ whose existence we know from the mean value theorem_? You just pointed to a technical detail which I regrettably missed but you never answered the _key question_ which has been there from the very beginning.
Comment: @mathworker21 I'm very upset that this turns into an argument. That's not what comments are for. I am very grateful to Thomas indeed!! He spotted an error an my original formulation that I acknowledged. Nonetheless, the question has always been (first version of the question): "Is there any way of finding $x_0$, _at least approximately_?" Okay, Thomas said it's not possible to know $x_0$. Okay, I accept (and expected) this. I however do not see how this outlines why there can't be any numerical algorithm which approximates $x_0$.
Comment: @David Thanks for the idea, so just solving $\int_a^bf(x)g(x)dx-f(x_0)\int_a^b g(x)dx=0$. That would indeed work but I hoped I could approximate $x_0$ without evaluating the integral $\int_a^b f(x)g(x)dx$.
Comment: Any constrained root-finding or minimisation method should work, depending on the specific functions.
Comment: I have a question? why is it the case that you would like to avoid solving that integral? Is it a partial lack of knowledge of the function? If so, what data do you have on $f$?
Comment: @TyJensen I indeed believe that Cauchy Schwarz is perhaps the most useful inequality of integrals of products but I do not think it helps me in finding $x_0$ which in turn approximates $\int_a^b f(x)g(x)dx$ (or $\int_a^b f(x)dx$ if we assume $g(x)=1$).
Comment: @Alex no argument needed. You said "You just pointed to a technical detail which I regrettably missed but you never answered the key question which has been there from the very beginning." That was in my view false. That's it. Best of luck with your question.
Comment: @DinosaurEgg I had an application where I needed to compute such an integral (i.e. $\int_a^b f(x)g(x)dx$) but $f$ was so nasty that no analytical solution seemed to exist. The integral of $g$ was very nice though. Thus, I used quadrature and got my result. I wondered whether the mean value theorem would offer an alternative to standard numerical approximations
Comment: One (maybe not so useful) note is that if you assume $g(x)=1$ and the integral evaluates positive, you can apply Cauchy-Schwarz for integrals to get $f(x_0) \leq \frac{\sqrt{\left(\int_a^bf(x)^2dx\right)}}{\sqrt{b-a}}$
Comment: editing a question after receiving an answer, to the extent that the answer becomes invalid, is quite unfair in my opinion.
Comment: @Alex what you're saying is patently false. Thomas said "Apart from that (even if  is continuous) there is, in general, no way to determine 0 in advance (i.e. without knowing  and  and actually calculating the values in question)" which answers your main question.
Answer: Going off Alex's comment: we are looking for $x_0$ such that
$$f(x_0)=\frac{\int_a^b f(x)g(x)\,dx}{\int_a^b g(x)\,dx}.$$
The right hand side of the equality is the average of $f$ with respect to $g$ (or you could thing of $g$ as a density function for the wire starting at $a$ and ending at $b$). For example, taking $g=1$ as in the question, we get precisely the average value of $f$ on $[a,b]:$
$$f(x_0)= \frac{1}{b-a}\int_a^b f(x)\,dx.$$
Unfortunately, even with such a simple $g$, this heavily depends on choice of $f$. For example, pick any $x_0$ in $(a,b);$ you can find $f$ depending on $x_0$ such that $f(x_0)$ equals the average of $f$ on $[a,b].$ Even assuming $f$ is monotone increasing won't be enough: the claim of the previous sentence still holds.
I think David's suggestion of a root-finding algorithm might work (depending on $f$), but there may be cases it doesn't converge (like Newton's method). Note that there may be multiple solutions for $x_0$ and such an algorithm may oscillate between them in some way.
Comment: You certainly got the question right! So you say there's no chance of finding $x_0$ in general, game over. The issue with the root-finding approach is that it requires me to evaluate $\int_a^b f(x)g(x)dx$, which is what I want to avoid at all cost. Indeed, that's the reason why I seek $x_0$ in the first place. I want to approximate $\int_a^b f(x)g(x)dx$ by only evaluating $\int_a^b g(x)dx$ and $f$ at $x_0$. So, I wouldn't mind (care) if there are several $x_0$ which do the job (as long as I can find at least one).
Comment: But you argue there's no way to approximate $x_0$? You seem to suggest that there can't be any algorithm which finds $x_0$ for a general $f$ because the problem is too difficult? I don't understand that. For a general $f$, the problem $f(x)=0$ is also difficult. Indeed, it's often impossible to solve in analytical form. Yet we have plenty of algorithms which can find zeros. So, why is my problem so much worse that there's no hope of finding $x_0$?
Comment: We have plenty of algorithms that can find zeroes given certain hypotheses. I didn't say your problem is worse; I said "depending on $f$"- I don't see how it's better. My guess is any root-finding algorithm that exists will work for this problem, and NONE of those methods work for ALL cases. So if you want a method that works for a specific case, then you should be more specific. There is no such algorithm that finds roots in all cases all the time. I'm not saying there is no hope for specific cases. You are trying to solve $f(x)=a$ for arbitrary $f.$ That could be extremely difficult, or not.
Comment: I didn't read the comments above, but I can easily imagine how the comments above turned into an argument, given your tone. Every sentence sounds sarcastic... By the way, $f(x_0)$ is Equal to the quotient in my answer. So even if you know $g(x)$ this problem is equivalent to finding $\int_a^b f(x)g(x)\,dx,$ which you say is too hard. Do you even know what $f$ is? If not, there are infinitely many functions for which the integral of $f\cdot g$ is equal.
Comment: I'm very sorry if you think I was rude. Very sorry indeed! I did not mean to sound unfriendly. Neither to you or anyone else here. English isn't my first language. Sorry if I said something in an unintended (!) harsh way. I know $f$. The function is merely very nasty and no analytical solution for the integral $\int_a^b f(x)g(x)dx$ exists. That's why I want to compute $f(x_0)\int_a^b g(x)dx$ instead. Thus, I seek $x_0$. You reformulated that probelm in solving $f(x)=a$, where $a=\frac{\int_a^b f(x)g(x)dx}{\int_a^b g(x)dx}$ which I however cannot solve because I don't know the numerator.
Comment: That by the way, is where my _initial_ interest in the question came from. This resulted in a more general question. Given that $\int_a^b f(x)dx=f(x_0)(b-a)$, we could compute integrals very easily if we knew $f(x_0)$. This may be easier than quadrature or other numerical integration techniques. But I couldn't find any algorithms using this mean value theorem approach and I wondered why. Finding this $x_0$ (for a general $f$) occurs to be a very difficult problem - more difficult than integrating the function numerically.
Comment: I see, thank you. Let me think about it a little more!
Comment: By the way, I don't see how it approximates "any" integral, as $x_0$ depends on the function $g.$ So You would need one $x_0$ to approximate $\int_a^b f(x)\,dx$ and another $x_0$ to approximate $\int_a^b f(x)g(x) \,dx$... What do you mean approximates "any integral?"
Comment: Yea, I'm convinced you really need to know more about $f$. If you don't know anything about $f,$ not even its average value, then how are you supposed to find $x_0$ satisfying $f(x_0)=$ average value of $f.$ Functions can behave any crazy way you want and have the same average values. For example, say $f$ has average value $0.$ You need to know two things: $f$ indeed has this average value, then you need to make sure one of the root finding algorithms actually works. Really, $g$ is arbitrary, so it's at least as hard as finding roots of $f.$

Mean Value Theorem Integrals


How to create a single column DataFrame in sparkR?

Question: How can I create a single-column DataFrame in SparkR? Suppose we have the following column x, how can I create a DataFrame out of it?
## Assumes a running sparkR shell
data("ChickWeight")
cwDF <- createDataFrame(sqlContext, ChickWeight)
x <- cwDF$Chick

There seems to be a generic toDF function but no methods. Any idea how to change x into a single-column DataFrame object?
Answer: Just use select function:
chicks <- select(cwDF, cwDF$Chick)
head(chicks)

##   Chick
## 1     1
## 2     1
## 3     1
## 4     1
## 5     1
## 6     1

Answer: You have to use select function,
x <- select(cwDF,"Chick")

instead of x <- cwDF$Chick which is a r code.
We get the result as in image below in sparkR shell,
Answer: x.df <- data.frame(Chick = x)


Get nested object in structure in gorm

Question: I have two structures:
type GoogleAccount struct {
     Id     uint64
     Token  string
}

It represent my custom PostgreSQL object type (i created myself):
CREATE TYPE GOOGLE_ACCOUNT AS
(
  id    NUMERIC,
  token TEXT
);

And next structure is table in DB:
type Client struct {
     IdClient       uint64          `gorm:"primary_key"`
     Name           string
     PhotoUrl       string
     ApprovalNumber uint16
     Phone          string
     Password       string
     HoursOfNotice  int8
     Google         GoogleAccount
}

And my custom object nested in type Client and named as google. I've tried to read data by the next way:
var users model.Client
db.First(&users)

But unfortunately I can't read field google (have a default value). I don't want to create separate table with google_account, or make this structure as separated fields in client table or packed it as json (created separate entity, because this structure used not only in this table and I'm searching new ways, that get the same result, but more gracefully). The task is not to simplify the presentation of data in the table. I need to make the correct mapping of the object from postgres to the entity.
Right now I found one solution - implement Scanner to GoogleAccount. But value in the input method is []uint8. As I can suppose, []uint8 can cast to string, and after that I can parse this string. This string (that keep in db) look like (x,x) - where x - is value. Is the right way, to parse string and set value to object? Or is way to get this result by ORM?
Is the possible way, to read this data as nested structure object?
Comment: Do you have column named `Id` & `Token`?
Comment: have you tried something yourself to solve it... please post of the many tries or describe a few to show effort.
Comment: Why don't you want to create a separate table for your separate data structure? Why not put Google ID and token directly in the Client struct then, instead of creating a separate entity which you don't want? Don't normalize things that you don't want normalized?
Comment: @aerokite Yes, in nested field `google` which represent himself a separate structure
Comment: @G_V I just want to use all possibility of PostresSQL. I created separate entity, because this structure used not only in this table. And my decision - it is not denormalization. Merely I'm using new ways, that get the same result, but more gracefully
Comment: @ZF007 Right now I found one solution - implement Scanner to `GoogleAccount`. But value in the input method is []uint8. I've tried to make object from byte, but it was without result
Comment: @KirillZyusko... add the info from your trail not in comment but ad it to the question itself. Its useful information that is now overlooked by others. Good to see effort ;-) (I did triage review your question.. not an expert on your topic... so I look at grammer, formatting and makes the content sense in general).
Comment: @KirillZyusko - How is it more graceful? Why can't you get GoogleAccount fields directly from the Client to which it is tied? If you need a separate table for the data but don't make one, how is that not denormalization? What does graceful mean in this context? New ways?You can't use a term to explain the definition of a term and expect us to know what you're thinking. It really helps to write out what you want, what you've tried, what you don't want and why.
Comment: @G_V Yeah, of course I can separate this data in client table and don't create new type of data. But here is small example, that described a problem with reading object from database to nested object in structure. Big difference between separate field in table or make it as object type I don't see (in the performance angle). Separate table - I want use this field not only in this table, the same it will be use in company table. If create separate table I need to keep pairs id_client and id_company, and one from this always will be null. At addition - performance of 2 query in db will be always
Comment: @G_V more costly, than one query with directly data in table. It's obviously. From point of view traditional SQL database this way looks like "undesired" (made new data type). But I repeat himself again - I just want to try new way. And if Postgres it allow, why i shouldn't to try it? In this example I just wanna to logically group data. Personally for me structure of `client.google.id` looks more suitable, than `client.google_id`. It's a matter of habit. And finally: separate fields in db it's normalization (in my case), but grouped the same data in one field (just object) - denormalization?
Comment: https://play.golang.org/p/q2DFAqyyrkP . Will it work?
Comment: @aerokite It will work, if table contain field `Id` and `Token` as usually fields of table. But I have an object type. The task is not to simplify the presentation of data in the table. I need to make the correct mapping of the object from postgres to the entity.
Answer: It looks like you'll want to do two things with what you have: (1) update the model so you have the right relationship binding, and (2) use the .Preload() method if you're trying to get it to associate the data on read.
Model Changes
Gorm automatically infers relationships based on the name of the attributes in your struct and the name of the referenced struct. The problem is that Google attribute of type GoogleAccount isn't associating because gorm is looking for a type Google struct.
You're also missing a foreign key on GoogleAccount. How would the ORM know which GoogleAccount to associate with which Client? You should add a ClientId to your GoogleAccount struct definition.
Also, I would change the primary keys you're using to type uint since that's what gorm defaults to (unless you have a good reason not to use it)
If I were you, I would change my struct definitions to the following:
type Client struct {
     IdClient       uint            `gorm:"primary_key"`
     Name           string
     PhotoUrl       string
     ApprovalNumber uint16
     Phone          string
     Password       string
     HoursOfNotice  int8
     GoogleAccount  GoogleAccount    // Change this to `GoogleAccount`, the same name of your struct
}

type GoogleAccount struct {
     Id             uint
     ClientId       uint             // Foreign key
     Token          string
}

For more information on this, take a look at the associations documentation here: http://gorm.io/associations.html#has-one
Preloading associations
Now that you actually have them properly related, you can .Preload() get the nested object you want:
db.Preload("GoogleAccount").First(&user)

Using .Preload() will populate the user.GoogleAccount attribute with the correctly associated GoogleAccount based on the ClientId.
For more information on this, take a look at the preloading documentation: http://gorm.io/crud.html#preloading-eager-loading
Comment: Thanks, it works when GoogleAccount is table. But in my case GoogleAccount is ObjectType in Posgres

`CREATE TYPE GOOGLE_ACCOUNT AS
(
  id    NUMERIC,
  token TEXT
);`

And I would like to not separate this type in other table.
Comment: @Kirill I had the same situation, however I used nested JSON. but I have no idea how to use custom types in postgresql with gorm!
Answer: Right now I found one solution - implement Scanner to GoogleAccount. At input of the Scan method I got []uint8, that I cast to string and parse in the end. This string (that keeping in db) look like (x,x) - where x - is value. Of course, it is not correct way to achieve my goal. But I couldn't found other solution.
I highly recommended use classical binding by relationship or simple keeping these fields in table as simplest value (not as object).
But if you would like to experiment with nested object in table, you can look at my realization, maybe it would be useful for you:
type Client struct {
    // many others fields
    Google          GoogleAccount   `json:"google"`
}

type GoogleAccount struct {
    Id      uint64      `json:"id"`
    Token   string      `json:"token"`
}

func (google GoogleAccount) Value() (driver.Value, error) {
    return "(" + strconv.FormatUint(google.Id, 10) + "," + google.Token + ")", nil
}

func (google *GoogleAccount) Scan(value interface{}) error {
    values := utils.GetValuesFromObject(value)
    google.Id, _ = strconv.ParseUint(values[0], 10, 64)
    google.Token = values[1]
    return nil
}

Comment: @LucioMollinedo thank you for your comment. I added code snippet.
Comment: Next time, don't just put a link to your repo. Add the actual code here as well. Your link is broken.
Comment: Holy smokes, the issue I had was putting a pointer in the method for `Value()`. Thanks so much!

Get nested object in structure in gorm


Utilizing admin panel values elsewhere in a module

Question: So I've got a custom admin panel page in a module I've written, and I want to get a value out of it.
Let's say I have a text box on this admin page, with a value, and I want to get that at some other point.
Do I need to use variable_set and put that into the database and then variable_get it in the function I want to use it in - or do I call it some other way?
Basically this thread:
https://drupal.org/node/2074589
Essentially, I just want to have a user be able to select a picklist value in the admin, and then call that picklist value later on in the actual module, for display.
Answer: You need to utilize system_settings_form when you write your admin form.
function mymodule_admin_form($form, &$form_state) {
  $form['my_textfield'] = array(
    '#type' => 'text',
    '#title' => 'Title',
    '#default_value' => isset($form_state['values']['my_textfield']) ? $form_state['values']['my_textfield'] : '',
  );

  return system_settings_form($form);
}

Submitting this form will call system_settings_form_submit which saves all the form fields as a variable, so you need to be careful when naming the form fields.
Now all you need to do is call:
variable_get('my_textfield', 'default value to return if no value is set');
from any function after drupal is bootstrapped.
Here's more information on how to create a configuration form: Creating the configuration form
Comment: Is the second argument optional?
Comment: for `variable_get`? Second argument is optional, as it defaults to `NULL`. See [`variable_get`](https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/variable_get/7)


How to get the coefficients of a vector?

Question:
Question: Suppose we have a subspace $V$ of $\mathbb{R}^n$ given by a basis $v_1,\ldots,v_k\in\mathbb{R}^n$. Suppose we are given a vector $v\in\mathbb{R}^n$ which we know lies in $V$. Is there a general procedure for computing the coefficients $a_i\in\mathbb{R}$ for which
  $$v=a_1v_1+\cdots+a_kv_k?$$

I know how to do it when $V=\mathbb{R}^n$: We just construct a matrix
$$A=\begin{pmatrix}| & & | \\ v_1 & \cdots & v_n \\ | & & |\end{pmatrix}$$
with the vectors $v_i$ as columns and then get
$$\begin{pmatrix}a_1\\ \vdots\\a_n\end{pmatrix}=A^{-1}v.$$
But if $k<n$, how to do it?

Example: Let $V$ be the $3$-dimensional subspace of $\mathbb{R}^5$ given by
  $$V=\mathrm{span}\{(0,1,2,3,4),(-1,-2,-3,-4,-5),(1,0,1,0,1)\}=:\mathrm{span}\{v_1,v_2,v_3\}$$
  and let
  $$v=(1, -3, -1, -5, -3).$$
  Find, in a systematic way, $a_i\in\mathbb{R}$ such that
  $$v=a_1v_1+a_2v_2+a_3v_3.$$
Answer: This problem is the very general problem of solving an $n\times k$ system of linear equations $Ax=v$ where the columns of $A$ are rthe coordinates of your family of vectors $v_1, \dots, v_k$.
The method consists in using Gauß's pivot method. In detail, consider the  matrix $(A|v)$ of size $n\times (k+1)$ augmented with the coordinates of $v$.
Use elementary operations on rows until the submatrix $A$ of $(A|v)$  is upper triangular. It will have rank $r\le k$

If the augmented matrix has rank $>r$, $v$ is not in $V$, so there's no solution.
If the augmented matrix has rank $r$, $v$ is in $V$ and $r$ vectors of $\{v_1, \dots, v_k\}$ are a basis for $V$. Consider the $r\times r$-submatrix $B$ made up of  the $r$ corresponding columns, plus the last column, and the $r$ first rows. You then have an augmented matrix $(B|w)$, where $w$ is the transform of $v$, truncated after the $r$-th row.
Continue pivoting until $B$ becomes the $r\times r$ unit matrix.
The $r$ first coefficients are the last column and the $n-r$ last coefficients are $0$.
The solution is unique if $r=k$. If $r<k$ oy may choose coefficients for $v_{r+1},\dots, v_k$ as you please, and deduce the coefficients for $v_1, \dots, v_r$.

In your example, the system has rank $3$, but is not compatible, i.e. $v$ in not in $V$, if my computations are exact.
Comment: Yes $v\in V$. We have $v=v_1+2v_2+3v_3$.
Comment: @ Spenser: I did the calculations by hand and could not guarantee them :o)
Comment: I used Muphrid's method, it works perfectly well.
Comment: I'm sure. I gave the *general* method, which works over any field, and is an illustration of the Rouché-Capelli theorem.
Answer: When you find the components by $A^{-1}v$, you're converting $v$--as expressed in the canonical orthonormal basis--into the basis you want to describe it in.
So for this case, you can use the same approach--if you can express $v$ in terms of one basis that is intrinsic to the subspace $V$ and then write the change of basis matrix $A$.
That probably doesn't help you, since you don't have $v$ in terms of any basis intrinsic to the subspace.
Instead, consider what happens when you take inner products of $v$ with the basis vectors:
$$\langle v, v_i \rangle = a_1 \langle v_1, v_i \rangle + a_2 \langle v_2, v_i \rangle + a_3 \langle v_3, v_i \rangle$$
Here, the inner product is that of $\mathbb R^5$. Using $i \in \{1, 2, 3\}$, do you see how you can use these inner products (which you can compute, given that you know the components of these vectors with respect to the standard basis) to derive three equations for the unkonwn coefficients $a_1, a_2, a_3$?


How to know the z-index of two elements

Question: I have two carousels which one of them is set to absolute and a toggle button above them which switches them out when I switch to the second one.
I can't click it, I feel the error is from z-index but I didn't set any z-index on any of the elements, is there a plugin to  view their z-index?
Comment: You should be able to use javascript to get the elements z-index like so; console.log(document.getElementById("element").style.zIndex);
Comment: Please show us your code. The button could be overlayed by one of those carousels or some other element.
Comment: You can try this: https://chrome.google.com/webstore/detail/devtools-z-index/bcnpmhefiohkpmjacfoanhbjhikegmoe?hl=en
Comment: Notice that while all answers so far show you how to find whether an element has a `z-index` applied, none of the approaches will tell you which [stacking context](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context) an element belongs to
Answer: If you are using chrome, you can check it in developer tools.
Right click on the element you want to check and inspect element. Switch to elements tab in developer tool then click on computed tab. It shows you all computed styles in alphabetical order.
Answer: Maybe I missed the point of the question, but simply using 'inspect' on Firefox or Chrome will allow you to see all styling properties on all element.
To get there:

Right click the element you want
Select 'inspect element'

as you can see the header of this very page has a z-index value of 5050
Answer: You can get the element, then extract the styles. Then you can get the value of style property by using camelcase for the css attribute name.
<code>const style = getComputedStyle((document.getElementById("element"));

const zIndex = style.zIndex;

console.log(zIndex);
</code>

How to know the z-index of two elements


Calculating error metrics on log10(y) bayesian ridge regression model. Why does model perform better when trained on log10(y)?

Question: I am using scikit learn's Bayesian ridge regression model and am training my model on log10(y), exponentiating (10 ** y_i) my predictions back to their original value, then calculating my error metrics to assess my model's performance. The error metrics I use are R^2 and MAE.
I chose to do this because (1) I have a log tail in my dependent variable's distribution and logging it makes the distribution closer to normal and (2) there is an increase in the standard deviation of my residual plots as with y.
However, I am not sure why logging my dependent variable, training my model on the log10(y), then exponentiating my predictions back seems to result in slightly increased model performance. Does anyone have an explanation for this?
My first intuition is that linear models perform better when the dependent variable is normally distributed and, thus, logging the dependent variable to make it normal results in better training.... but I am not sure if this is right.
P.S: Please let me know if I need to make this question clearer
Answer: Let us assume your target variable is conditionally normally distributed on the log scale (equivalent to residuals being normal). Then your point predictions on the log scale are the expectation and the median (which coincide for the normal distribution), again on the log scale.
Exponentiating the log-expectation and the residuals does not result in a normal distribution on the original scale, but in a lognormal distribution. For this distribution, the expectation and the median are no longer the same, the median is lower than the expectation. The exponentiation turns your point predictions on the log scale into median predictions on the original scale. They are no longer expectation predictions here. (If you want an expectation forecast, see here under "bias adjustments" for $\lambda=0$.)
The final piece of the puzzle is that the MAE is minimized precisely by the median, not the expectation. This is why your exponentiated predictions (which aim at the conditional median on the original scale) perform better under MAE than modeling directly on the original scale, because (presumably and almost certainly) your model on the original scale targets the expectation and not the median.
Comparing the two predictions does not make sense, because they aim at minimizing two different loss functions: modeling on the original scale (presumably and almost certainly) aims at an unbiased expectation prediction, which minimizes the MSE, while modeling on the log scale and exponentiating aims at a median forecast, which minimizes the MAE. I have written a little paper on this (Kolassa, 2020, IJF).
If you want an expectation forecast, you should do a bias-adjusted back-transformation (see the link above) and assess both models using the MSE. If you want a median forecast, you can go the route you took, and/or model on the original scale using the MAE as a loss function, and in any case evaluate the predictions using the MAE. Per this argument, expanded in the paper, it does not make sense to evaluate a forecast using multiple different error metrics.
There are illustrations of how optimal forecasts differ by error metric for different conditional distributions here and here.
Comment: Hi, Stephan, thank you! Wow, this makes sense. I am essentially making median predictions when I log and exponentiate back. And thank for for letting my know about the comparison, I do want to minimize MAE and, thus, I would rather a median forecast over an expectation forecast.


What kind of null hypothesis can a permutation test consider?

Question: From what I have seen, permutation tests are always used to compare the means (or median) of two different groups in the experiment. Even Wikipedia says

A permutation test involves two or more samples. The null hypothesis is that all samples come from the same distribution.

Is it possible to extend permutation test for just a single sample to estimate a population parameter? If not, why?
Comment: Does this answer your question? [Permutation test comparing a single sample against a mean](https://stats.stackexchange.com/questions/65831/permutation-test-comparing-a-single-sample-against-a-mean)
Comment: Bootstrap confidence intervals might be what you're looking for if you just want to estimate a parameter for a single distribution.
Answer: Permutation tests can be done (contrary to the implication of the quoted statement [1]) for single samples and intervals can be constructed from them. Indeed, you may well have done one without realizing it. Further, permutation tests can be performed for a very large array of situations beyond those connected to means or medians [2].

You can use permutation tests to construct intervals for parameters. The set of hypothesized population parameter values that would not be rejected by the test at a level not exceeding $\alpha$ will form an interval with coverage at least $1-\alpha$ for the parameter, under the same assumptions as needed for the test.
For example, a two-sample permutation test of means can be used to form an interval for the difference in means. A Wilcoxon-Mann-Whitney two sample test can be used to obtain an interval for the population median of pairwise cross-population differences, and so on.

One sample permutation tests certainly exist. Intervals for corresponding population quantities can be constructed from them.
For example you could do a permutation test for $H_0: \mu=\mu_0$ against the usual two-sided inequality alternative. This requires some assumptions under $H_0$; the usual test constructs exchangeable quantities by assuming symmetry about the assumed location parameter $\mu_0$, so that $X-\mu_0$ and $\mu_0-X$  may be substituted for each other (i.e. the sign of $X-\mu_0$ may be flipped) without changing the distribution when $H_0$ is true. This is not the only possible such assumption and corresponding exchangeable quantity.
We can then consider the set of possible $\mu_0$ values that would not lead to rejection of $H_0$ using this test, at some level $\alpha$. This non-rejection region (which some authors have called a consonance region) may be used as an interval for the parameter.
Similarly, intervals may be constructed for many other parameters.

At least one such one-sample permutation-test interval is built into many statistics packages; that from a Wilcoxon signed rank test (almost all rank-based tests are simply permutation tests based on rank-transformed data). Here's an example of it in R.
<code>  x = round(rlogis(10,15,3),2)  # create some data
  x
  [1]  6.87  8.93 18.73  9.41 11.88 15.11 26.92  6.81 18.46 15.41

  wilcox.test(x, conf.int=TRUE)

         Wilcoxon signed rank exact test

 data:  x
 V = 55, p-value = 0.001953
 alternative hypothesis: true location is not equal to 0
 95 percent confidence interval:
   9.17 18.46
 sample estimates:
 (pseudo)median
         13.495
</code>
Here we see that the estimate of the population pseudomedian is 13.495; the 95% interval for the population pseudomedian is (9.17, 18.46). In this case we know the population pseudomedian because we generated the data -- it was 15. The interval happened to overlap the population value in this instance.

[1]: Beware taking everything about statistics on Wikipedia as gospel; while many of its stats articles are pretty good (for example the articles on individual distributions are often quite good), not everyone who edits Wikipedia knows what they're talking about and many of the stats articles (particularly the ones that cover less technical material) are frequently edited by people whose desire to help exceeds their grasp of the subject.  Worse, there's a lot of books and articles about statistics that contain many serious errors so it's also easy for those helpful people to provide references (albeit wrong or misleading ones) for the claims in their edits.
[2]: However, I would add that often it's a good idea to construct a scaled (e.g. studentized)  version of the statistic or similarly scaled/normalised quantity rather than use a raw statistic, such as using a t-statistic rather than a raw difference in means; the properties of the test are sometimes improved. One example would be with a test for the Pearson correlation


Angularjs Elemento duplicando

Question: Fiz um gráfico com Morris.js https://morrisjs.github.io/morris.js/donuts.html
e coloquei ele dentro de um retorno de uma consulta, até ai tudo certo, porem quando chamo essa consulta novamente para gerar outros dados no gráfico, ele gera outro gráfico, está duplicando, ao invés de apenas trocar as informações, ele cria outro gráfico e fica 2

Conforme vou trocando o option, ele vai gerando um novo gráfico...
Meu código
<code> $scope.selectAction = function(myOption) {
       $http.get('http://localhost/Angularjs/php/TotalAlunos.php?cod=' + $scope.myOption.id).then(function(result){
        Morris.Donut({
          element: 'donut-example',
          data: [
            {label: "In-Store Sales", value: result.data.total},
            {label: "Mail-Order Sales", value: 20}
          ]
        });
      });
};
</code>
Answer: Não conheço esse morrisjs, mas dando uma pesquisada eu encontrei algo que talvez possa ajudar.
Tente criar um gráfico só e apenas atualizar seus dados em cada chamada.
Algo tipo isso:
<code>var donut = Morris.Donut({
      element: 'donut-example',
      data: [
          {label: "In-Store Sales", value: 0},
          {label: "Mail-Order Sales", value: 0}
      ]
    });

 $scope.selectAction = function(myOption) {
   $http.get('http://localhost/Angularjs/php/TotalAlunos.php?cod=' + $scope.myOption.id).then(function(result){
    donut.setData([
        {label: "In-Store Sales", value: result.data.total},
        {label: "Mail-Order Sales", value: 20}
      ]);
  });
};
</code>

Angularjs Elemento duplicando


Bitmap region is already locked

Question: string pathFile = @"C:\Users\user\Downloads\CaptchaCollection\Small\Sorted\";

var files = Directory.GetFiles(pathFile).Select(nameWithExtension => Path.GetFileNameWithoutExtension(nameWithExtension)).Where(name => { int number; return int.TryParse(name, out number); }).Select(name => int.Parse(name)).OrderBy(number => number).ToArray();
List<int> fileList = files.ToList();

image1 = new Bitmap(pathFile + fileList[0].ToString() + ".png");
image2 = new Bitmap(pathFile + fileList[1].ToString() + ".png");

if (compare(image2, image2))
{
    // if it's equal
    File.Delete(image2.ToString());
}

So basically what I have right now is that every file is numeric (without extension). I created an array then converted it to a list.
I use my global variables image1 and image2 which are used to compare if they are the same.
image1 and image2 change as we go along the search. So it changes the index.
If my compare() method returns a true, it will delete the second image.
However, on the compare() method, I seem to be getting this exception error on this line:
BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.ReadOnly, bmp2.PixelFormat);

Comment: Show us a complete reproducable code
Comment: @AlvinWong It's on the link I posted (Code Review) http://codereview.stackexchange.com/questions/39980/is-there-a-faster-way-to-compare-if-2-images-are-the-same/39987#39987
Comment: Have you locked the bits somewhere but forgot to call UnlockBits?
Comment: @AlvinWong On the bottom of the code of the method it would say `bmp1.UnlockBits(bmpData1);`
`bmp2.UnlockBits(bmpData2);` And I believe that exception came on the 2nd part of the method
Answer: Well, actually it took me quite a while to notice this line:
if (compare(image2, image2))

You are passing the same Bitmap to the function compare, so when the function attempts to call LockBits on the same Bitmap twice, this exception occurs.
So in order to fix it, don't pass the same bitmaps to the function.

Bitmap region is already locked


How do you apply timezone formatting to a date?

Question: I'm trying to build my own output for a date field *see Group multiple times by day (multi-value date field) *, but the problem I'm having is how to apply the timezone?
This article -> Date value to different date format and nearly every other reference out there tells me that it should be applied by default. But it isn't.
What I'm doing is theming the field with:
function theme_field__field_times($vars){
    $dates = $vars["element"]["#values"];
    foreach($dates as $item){ // there are many dates, so we'll cycle through each one
        $date = ('U', strtotime($item['value'])); // convert to unix time
        print('This date renders as: '.format_date('$date', 'custom', 'D d/m/Y - g:ia', '', date_default_timezone_get());
    }
}

Which outputs, for example, 27/07/2011 -1:10pm. The value should be 27/07/2011 - 11:10pm, or the date that my function spat out +10hours - the timezone ('Australia/NSW') that should have been applied...
date_default_timezone_get() is returning the correct timezone.
If I let Drupal render the dates using it's default field theming, the dates are right.
So why isn't the timezone being applied? How do I fix it?
Answer: I did something along the lines of this for a date field coming in as UTC that I wanted output using my site's timezone.
print format_date(strtotime($row->field_data_field_event_field_event_value.' '.$row->_field_data['nid']['entity']->field_event['und'][0]['timezone_db']));

Answer: The arguments you pass to the function are wrong; in particular, what you pass as last argument should be the one before it.
The arguments the function accepts are format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL), but your fourth argument is an empty string, and then you pass the timezone identifier as last argument, which should be the language identifier.
You are also using '$date' as first argument.
Try with the following call:
format_date($date, 'custom', 'D d/m/Y - g:ia', date_default_timezone_get()).

Comment: Thanks - fixed that but the time is still not being adjusted.

I think, because the site is 'Australia/NSW', and the time as supplied in the 'value' doesn't have a timezone, it's assuming it is already Australia/NSW and not applying any adjustments.

The array the date field returns looks like:
`[value] => 2011-07-27T23:10:00
[timezone] => Australia/NSW
[timezone_db] => UTC
[date_type] => date`

I would assume there's some pre-prepared function in the Date API for doing this, but I can't find it.
Comment: As far as I can see, it works when the server is using the UTC timezone; Drupal core modules normally use just three parameters, and the date is automatically converted basing on the Drupal site settings, though.
Answer: When you convert the time-string to a timestamp using the strtotime function, it already applies the default timezone. Later, when calling format_date, the timezone is applied again - which breaks the calculation.
The solution is to add "UTC" to your time-string, which sets the "neutral" timezone. So you get a timestamp in UTC, which is then formatted and converted to the default timezone by format_date().


Error: Socket failed: Connection refused ltsp + update-rc.d errors

Question: I am installing a LTSP server, everything works, the pxe.linux.0 gets found, but the client gets stuck at this error:
Modprobe: module overlayfs not found in modules.dep
Error: socket failed: connection refused
Exiting.
update-rc.d error cannot find a LSB script for lightdm
update-rc.d error no runlevel symlinks to modify, aborting!
Syncronising state for gdm3.service with sysvinit using update-rc.d...
Executing /usr/sbin/update-rc.d gdm3 defaults
Executing /usr/sbin/update-rc.d gdm3 disable
isserv warning current start runlevels empty of script 'gdm3' overrides SB defaults (2 3 4 5)
isserv warning current stop runlevels (0 1 2 3 4 5 6) of script 'gdm3' overrides LSB defaults (0 1 6)
update-rc.d error cannot find a LSB script for autofs

Attached is a picture of the errors.
Answer: Found out that nfs-common was failing. Was a directory problem in /etc/idmapd.conf. For ubuntu it is:
Pipefs-Directory = /var/lib/nfs/rpc_pipefs

And for debian it should be:
Pipefs-Directory = /run/rpc_pipefs

This solved the issues!


Angular 6 pipe operations remove typing from observable

Question: I just updated from Angular5 to 6. After updating I ran the code to migrate to rxjs6 and it changed my code where I was using takeWhile. Now in order to subscribe to a service my code looks like this:
this.menuService.currentMenu.pipe(takeWhile(() => this.isAlive))
   .subscribe(result => {
      if(result && result.name)
      {
         //do stuff
      }
   }
);

using this import statement
import { takeWhile } from 'rxjs/operators';

After looking at the docs for rxjs6 it looks like this is the correct way to use takewhile now; however I am getting errors saying:

Property 'name' does not exist on type '{}'

It looks like the takewhile operation is stripping the observable of its typing. It also happens with filter so I assume that pipe is the issue.
When I set up a test in stackblitz I dont get the error, I expect this is might be do to the project in stackblitz not being on the same typescript version?
This is the version I am using:
     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/

Angular CLI: 6.0.8
Node: 10.4.1
OS: darwin x64
Angular: 6.0.5
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.6.8
@angular-devkit/build-angular      0.6.8
@angular-devkit/build-optimizer    0.6.8
@angular-devkit/core               0.6.8
@angular-devkit/schematics         0.6.8
@angular/cdk                       6.2.1
@angular/cli                       6.0.8
@angular/material                  6.2.1
@angular/material-moment-adapter   6.2.1
@ngtools/webpack                   6.0.8
@schematics/angular                0.6.8
@schematics/update                 0.6.8
rxjs                               6.2.1
typescript                         2.7.2
webpack                            4.8.3

Edit:
Explicitly specifying a type fixes the issue. But I don't see why I should have to, why it cant just keep its typing.
this.menuService.currentMenu.pipe(takeWhile(() => this.isAlive))
   .subscribe((result: ResultClass) => {
      if(result && result.name)
      {
         //do stuff
      }
   }
);

Comment: Might be some typescript version issue. subscribe(result: any) will fix the issue
Comment: Adding `: any` to the subscription does not fix the issue.
Comment: If using `any` like `subscribe((result: any) => ...)` doesn't help then the problem is somewhere else and not in `takeWhile`.
Comment: @martin ok doing `subscribe((result: any) => ...)` worked. (with the extra brackets). But why do I need to specify the type in my project when on Stackblitz it is automatically carried over from the observable?
Comment: I doubt it's the `takeWhile`, as `const source = of(1).pipe(takeWhile(() => true))` sees `source` inferred to be `Observable`. I think it depends upon the type of `this.menuService.currentMenu` and that's not included in the question.
Comment: @cartant the example shows the classes. As for takewhile... if I remove takewhile and leave pipe empty or remove pipe alltogether and subscribe directly it works. It's only when I pipe AND add takewhile that I lose typing.
Comment: I don't know what you mean. The StackBlitz example does not appear to exhibit the problematic behaviour you've detailed in the question.
Comment: @cartant I explained in my post that the example works it stackblitz but does not work in my own environment. Most likely because of version differences.
Comment: I have just tested using filter instead of takewhile and it has the same issue. So the issue seems to be with pipe. I wasnt sure originally because, like i said earlier, if pipe is empty `observable.piple().subscribe(v => {...})` the error disappears. I will update my post for clarity.
Comment: Also, @cartant after adding `const source = of(new ResultData()).pipe(takeWhile(() => true));` to my code... It showed up as `Observable<{}>` in my project. So it is stripping the typing... Again, in stackblitz it works as expected with type of `Observable`
Answer: Seems to have been cause by a combination of me changing my node/npm installation to be handled by nvm while using an old version of Visual Studio Code.
Once I updated to the new Visual Studio Code 1.24.1 the problem went away.

Full explanation:
I think the old version of Visual Studio Code didn't know where to look for the npm installation. I got an error (not right away, it was about a day after updating to nvm) saying something about npm and Automatic Type Acquisition and found this article on how to fix the issue. I could use npm through the terminal fine, it just seems to have been causing an issue with this Automatic Type Acquisition thing (this is my guess).
I tried using the which npm command in terminal to find the installation path and put that in the settings file as directed by the article. It didn't seem to fix the issue. This might have been because I used the incorrect path, or I might have forgotten to restart Visual Studio Code to have the settings take effect (I think I did restart though), either way I decided to just revert my settings file and update Visual Studio Code to see if the new version would play nice with nvm.
So my guess is the new Visual Studio Code knows about nvm and can find the npm installation.
It is interesting that it could properly keep the typing if I did a direct subscribe on the observable. Perhaps upgrading to the new rxjs was part of the issue along with the other two things.
Comment: What you say about `nvm` is interesting. In my case, I checked yarn/npm, node, ng version issues but I did not check nvm at all. In the end, out of desperation, I did a git rollback and continued with my previous Angular 5 version.


Complexity of searching this article database

Question: Let's say I have a database:

k articles
each article has a title with l words in it
my search query has m tokens

To search for each of my tokens in the titles of all articles is m * k * l
Am I right in thinking that this is O(n) ?
Answer: In what follows, I'm going to assume that each word has length w or less.
It's not mathematically valid to talk about O(n) time unless n is defined somewhere.  If you interpret n to be the total length of the input given to you (the amount of bits needed to write out all the articles and the search query), then you'd get that n = (kl + m)w.
Notice that your algorithm does not run in time O(mlk) unless w is a constant.  More accurately, it's O(mlkw).  Since n = lkw + mw, your runtime would not be O(n) under this interpretation of what n is.
That said - you can improve the runtime of your algorithm significantly by using better data structures.  If you build a trie containing all of your words (which takes time mw), then you can look up each word in time O(w).  This means that since there are kl total words to consider, your search time would be O(mw + lkw), which is linear.
Hope this helps!


ISA Server 2006 SSL Certificate Dilemma

Question: I'm making so great headway in offering our services over https with help from a Go Daddy certificate, later to be upgraded to Thawte SSL123 certs. But, I've just run into one whopper of a problem.
Here's my setup: I run an ISA 2006 firewall. Our web services are distributed over 2 servers. One is Windows 2000 (www.domain.com) and the other is Windows 2003 (services.domain.com). So, I'll need to purchase 2 certs for both www and services, import them into IIS6 on their respective machines, then export them with the primary key (making sure to Include all certificates in the certification path if possible... that had me stumped for a while), and then to finally import them into ISA's local computer Personal store. The problem I've just run into is that I have separate firewall rules for services.domain.com and www.domain.com... because requests need to be forwarded to different web servers. Each of these firewall rules use the same httplistener. I have just found out that you can only use 1 certificate per httplistener. To make matters worse you can only have a single httplistener per ip / port. Is this correct? I can only use a single certificate for a single ip address? This would seem to be a severe limitation. Am I wrong? If I'm not then I've got a whole lot more work ahead of me as I'll have to set up extra ip's, add them to the firewall's network interface, create new listeners using that ip, etc...
Can someone please confirm that I'm doing this correctly / incorrectly? Once I got my head wrapped around it all it seemed easy... then this.
Thanks in advance.
Edit: To explain what I believe I have to do now is: set up www.domain.com to use one ip address and set services.domain.com to use another ip address. Then create separate httplisteners (well... create one because one already exists) for each. One with the www cert installed, the other with the services cert installed. How does this sound?
Answer: I didn't think you had to specify an IP address for a weblistener, which would force it to use host headers and perform your mapping that way.
Otherwise, you might want to think about a wildcard certificate (*.domain.com)
Comment: You don't have to specify an ip address but unfortunately the host headers in an SSL request are encrypted and as such are not available for interpretation by the web listener.

ISA Server 2006 SSL Certificate Dilemma


Elements appended to SVG with D3 are not appearing

Question: I am trying to display some simple bar charts with D3 where each bar is a div, but nothing is being displayed.

var chartData =  [4, 8, 15, 16, 23, 42];
var body = d3.select('body');

var svg = body.append('svg')
   .attr("width",800)
   .attr("height",500);

var div = svg.append('div').attr('class', '.chart');

for (var i = 0; i < chartData.length; i++) {
   div.append('div')
     .attr("height", 20)
     .attr("width", chartData[i]*10)
     .html(chartData[i]);
}
.chart div {
 font: 10px sans-serif;
  background-color: steelblue;
 text-align: right;
 padding: 3px;
  margin: 1px;
 color: white;
}

When using inspecting the SVG I see the newly created elements and the svg is highlighted but the inner divs are not.
Answer: In a nutshell: you cannot append a HTML element to a SVG. The reason is simple:
,
,
,
, ,
 and so on are not valid SVG elements.
This has been asked and answered many, many times. However, I'd like to answer this question because of a specific part of it (which is normally not asked):

When using chrome's inspect I see the newly created elements.

Yes, the elements are there. But this doesn't mean it's going to work.
Let's show this in the following snippet, in which I'm appending
s to the SVG. look at the console.log, it shows the SVG structure:

var svg = d3.select("svg");

var data = d3.range(5);

var divs = svg.selectAll("foo")
 .data(data)
 .enter()
 .append("div")
 .html("Where are you?")

var mySVG = (new XMLSerializer()).serializeToString(svg.node());
console.log(mySVG)

<svg></svg>

You can see that the divs are in the DOM. However, there is nothing on the screen...
The fact that you see the element in the DOM means nothing, because you can append anything!
For instance, let's append an element named CharlesDarwin:

var svg = d3.select("svg");

var data = d3.range(5);

var divs = svg.selectAll("foo")
 .data(data)
 .enter()
 .append("CharlesDarwin")
 .html("I'm a strange tag!")

var mySVG = (new XMLSerializer()).serializeToString(svg.node());
console.log(mySVG)

<svg></svg>

You can see <charlesDarwin> in the DOM. However, it's obvious that nothing will show up in the SVG.
PS: Regarding your chart:

Get rid of that for loop. You don't need that when using D3;
Set the class without the dot;
Append the divs to the body, as discussed above.

This is the working code:

var chartData = [4, 8, 15, 16, 23, 42];

var body = d3.select('body');

var div = body.selectAll("foo")
  .data(chartData)
  .enter()
  .append('div')
  .attr('class', 'chart')
  .style("width", d => d * 10 + "px")
  .html(d => d);
.chart {
  font: 10px sans-serif;
  background-color: steelblue;
  text-align: right;
  padding: 3px;
  margin: 1px;
  color: white;
}

Comment: Good answer, but you haven't really explained *why* it won't work. The reason is because `` is not a valid SVG element.  So has no effect inside an ``.
Comment: I have coppied and pasted your code into index.html, app.js, and app.css and still I see nothing. I am very confused about how to get this to work.
Comment: I can't see anything either - was that your settings?
Comment: Indeed, @PaulLeBeau, you're right! As I already explained that in the previous dozens of times a similar question has been asked, I forgot to repeat the explanation in this one. Edited.
Comment: Thank you Gerardo. For the through explanation. I learned something new from your response. Thanks.
Comment: @qwerty What do you mean by *"I can't see anything"*? Just click "Run code snippet".
Comment: I can see the snippet correctly, but when I copied and pasted it into own file, I couldn't see anything. I managed to fix it - turned out I added the path to my js file at the top :facepalm:


Band at the bottom and the top of the screen of my React-native application

Question: I'm creating an application using React-native but I have a problem. There is a band at the bottom and at the top of the screen. I think that the "ScreenBase" element is the cause of this mistake, but I can't remove it because it's very important.
Here is a screenshot of my application. You can see the band at the top and at the bottom.
Here the code of my screen :
<code>return(
        <ScreenBase style={styles.container}>
            <ScrollView>
                <View style={styles.field}>
                    <Text style={styles.fieldName}>{t('contact')} : </Text>
                    <Text style={styles.fieldValue}>{inventory.contact}</Text>
                </View>
                {isModified && <View style={styles.field}>
                    <Button
                        title={t('uploadToServer')}
                        buttonStyle={[styles.syncBtn]}
                        containerStyle={styles.syncBtnContainer}
                        titleStyle={{ color: styles.syncBtn.color }}
                        onPress={syncSelfToServer}
                        icon={
                            {
                                name:'cloud-upload',
                                type:'font-awesome',
                                color:styles.syncBtn.color,
                                style: {paddingLeft: 5}
                            }
                        }
                        iconRight={true}
                    />
                </View>}
                <View style={styles.field}>
                    <Text style={styles.fieldName}>{t('lotsPlural')} : </Text>
                    <View style={styles.pictureList}>
                        {/* <TouchableOpacity
                            style={styles.pictureBtn}
                            onPress={() => navigation.navigate('LotForm', {pId: inventory.id})}
                        >
                            <Icon
                                name='plus'
                                type='font-awesome'
                                color={styles.pictureBtn.color}
                                style={styles.icon}
                                size={100}
                            />
                            <Text style={styles.pictureTitle}>{t('addLot')}</Text>
                        </TouchableOpacity> */}

                        {displayedLots}
                    </View>
                </View>
            </ScrollView>

            {/* BUTTON TO ADD A LOT */}
            <TouchableOpacity style={[styles.button, styles.addLotBtn]} onPress={() => navigation.navigate('LotForm', {pId: inventory.id})}>
                <Icon
                        name='plus'
                        type='font-awesome'
                        color={styles.iconAdd.color}
                        style={styles.iconAdd}
                        size={styles.iconAdd.fontSize}
                />
            </TouchableOpacity>

        </ScreenBase>
    )
</code>
Do you have a solution to resolve my problem please ?
Thank you !
I tried to remove the ScreenBase element. It works but it's a bad practice because this element is very important.
Comment: Is it fixed height?
Comment: ScreenBase have a minHeight of 100%
Answer: I have resolved my problem by adding overflow: 'visible' to the ScrollView.


Instalação do git não está sendo reconhecida pelo Windows

Question: Não sou familiarizado com o bower e instalação de pacotes, pois bem...
Fui instalar o Foundation Zurb, para e-mail marketing instalou corretamente, mas quando dou o comando foundation new para criar um novo projeto, ele me retorna a seguinte mensagem:

You need Git installed to get started. Download it here: http://git-scm.com/downloads

Eu tenho o git instalado em minha maquina, por que isso acontece?
Answer: Você provavelmente não setou o PATH (caminho) de instalação corretamente para o Windows.
Você pode determinar o caminho da instalação dessa maneira:

Clique com o botão direito do seu mouse em "Meu Computador" (My Computer)
Selecione Propriedades (Properties)
Selecione Avançado (Advanced)
Clique em Variaveis de ambiente (Environment Variables)
Selecione a variavel Caminho (Path)
Clique em editar (Edit)
Adicione o caminho ao seu Git na frente do campo valor de variavel (Variable value) e separe com um ; do próximo registro (Obs. não adicione espaços entre ; e o próximo valor).
Comment: Não amigo, você deve colocar o path para git.exe ou seja, na pasta que colocou do Git você deve descer mais um nível e colocar Git/bin que é o caminho correto para o executável
Comment: Não funcionou por favor da uma olhada se eu fizer certo
https://i.imgur.com/ffV8e4D.png
https://i.imgur.com/aupCn6r.png
https://i.imgur.com/NXmdsNC.png
Comment: valeuu colega , funcionou perfeitamente


General solution to "Time and work" questions?

Question: If "a" number of machines can  complete a task in "x" days.
   "b" of them them could do the same task in "y" days.
how many days are required for "c" number of machines to do the same task?
example: 7 trucks can do the task in 4 days.
         3 trucks do it in  10 days.
         how many trucks are needed to do it in 5 days?
Comment: What? Is it the same task? Use your words!
Comment: Linear interpolation.
Comment: If it is the *same* task, ax = by = cz. Find z.
Comment: Instead of trying to find the number of days that it takes one machine to complete a single task, try to find the number of tasks that a machine can complete in on day. Ask yourself, "what is the difference between $days/task$ and $tasks/day$?". https://www.youtube.com/watch?v=-_xZZt99MzY
Answer: Increased machines and increased work per time per machine means decreased time, so
$$z = \frac{ax}c$$
For example, if it took $5$ machines $3$ days to complete a task, it would take $10$ machines
$$z=\frac{ax}c=\frac{(5)(3)}{(10)}=\frac{15}{10}=1\frac12 days$$
to complete that same task, assuming the relationship was proportional. Note that
$$ax=by=cz$$
However, the newly example provided example "7 trucks can do the task in 4 days. 3 trucks do it in 10 days. how many trucks are needed to do it in 5 days?" suggests that work per time is not proportional (varying efficiency?), so the machine-efficiency relationship would need to be known.


How do I create a view with some customization?

Question: I like to create a view to display properties information as shown below.

View

View page
7676 - SQFT
7    - BR

I created a view and added the fields based on my design. I made progress, but I am not able to create the second column of data (SQFT, BR).
How do I add the second column in this view?
Comment: Ever heard of CSS?
Answer: I wouldn't use Fields setting for this case.
Try to override template for specific content type and set your view to display your content type in specific 'view mode' for example teaser.
In your view change 'Format' to 'unformated list' and change 'Show' setting to 'teaser' for example. This will display your content type in specific view mode, so now you can override your template for this custom content type using:
node--my_content_type_machine_name--display_mode.html.twig
be aware of correctly using double dashes, otherwise your template won't be registered by drupal. Then you can code custom HTML template for your content type in this specific view mode and you can customize your layout according to your needs.


Discount by time and not by date

Question: How can I achieve the discount by time on not by date functionality.
I have to provide discount on 8 March from 8AM to 8PM.
Answer: Currently, Commerce Discount doesn't support specific times in it's discount rules.
You could use PHP evaluation to make this work, though.
Answer: If you want to show date-time you can try this:
https://www.drupal.org/project/stanford_date_timepicker
alter file commerce_discount.admin.inc
<code>$element['date']['#title'] = t('Start date');
// New class to show date time like 03/22/2017 12:32
$element['date']['#attributes']['class'][] = 'stanford-timepicker';
$element['date']['#description'] = t('Starts at 12:00:00 AM');
</code>
Note: need to change the tables accordingly.

Discount by time and not by date


linq update/delete/insert record, say my Me.txtxxxx.text is not a part of gridview, but thay are.

Question: Im trying to make my first linq connection to a gridview, (im not using linqdatasource) and i get the records fine to my gridview.
But my AddNewTask, UpdateTask, And DeleteTask gives me and error about my Me.txtxxxx.text or Me.lblxxxx.text is not a part of my gridview.
But the labels and textboxs are a part of my gridview.
Can someone help/guide me, what to do, im new to this linq, but have read alot of tutorials, but cant find any fix for my problem.
Code_Behind
Imports System.Data.Linq

Partial Class gridview
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Call linqdb()
    End If
End Sub

Protected Sub linqdb()
    Using db As New ThedatabaseconnectionDataContext()
        Dim tblTest As Table(Of testtable) = db.GetTable(Of testtable)()
        Me.GridView1.DataSource = tblTest
        Me.GridView1.DataBind()
    End Using
End Sub

Protected Sub AddNewTask(ByVal sender As Object, ByVal e As EventArgs)
    Using db As New ThedatabaseconnectionDataContext()
        Try
            Dim tbltest As Table(Of testtable) = db.GetTable(Of testtable)()
            Dim rtest As testtable = New testtable With {.test_cat = Me.txttestcat.Text, .test_info = Me.txttestinfo.Text, .test_number = Me.txttestnumber.Text, .test_datetime = Me.txttestdate.Text}
            tbltest.InsertOnSubmit(rtest)
            db.SubmitChanges()
            'Me.lblMsg.Text = "Added Successfully"
        Catch ex As Exception
            'Me.lblMsg.Text = ex.Message
        End Try
    End Using
End Sub

Protected Sub EditTask(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    GridView1.EditIndex = e.NewEditIndex
    linqdb()
End Sub

Protected Sub CancelEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
    GridView1.EditIndex = -1
    linqdb()
End Sub

Protected Sub UpdateTask(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    Using db As New ThedatabaseconnectionDataContext()
        Try
            Dim tbltest As Table(Of testtable) = db.GetTable(Of testtable)()
            Dim objtest As testtable = tbltest.SingleOrDefault(Function(p) p.test_id = Me.txttestid.Text)
            If objtest IsNot Nothing Then
                objtest.test_cat = Me.txttestcat.Text
                objtest.test_info = Me.txttestinfo.Text
                objtest.test_number = Me.txttestnumber.Text
                objtest.test_datetime = Me.txttestdate.Text
                db.SubmitChanges()
                ' Me.lblMsg.Text = "Updated Successfully"
            Else
                'Me.lblMsg.Text = "Employee not found"
            End If
        Catch ex As Exception
            'Me.lblMsg.Text = ex.Message
        End Try
    End Using
End Sub

Protected Sub DeleteTask(ByVal sender As Object, ByVal e As EventArgs)
    Using db As New ThedatabaseconnectionDataContext()
        Try
            Dim tbltest As Table(Of testtable) = db.GetTable(Of testtable)()
            Dim objtest As testtable = tbltest.SingleOrDefault(Function(p) p.test_id = Me.lbltestid.text)
            If objtest IsNot Nothing Then
                tbltest.DeleteOnSubmit(objtest)
                db.SubmitChanges()
                'Me.lblMsg.Text = "Deleted Successfully"
            Else
                'Me.lblMsg.Text = "Employee not found"
            End If
        Catch ex As Exception
            'Me.lblMsg.Text = ex.Message
        End Try
    End Using
End Sub
End Class

Main_Code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="gridview.aspx.vb" Inherits="gridview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<title></title>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
        CellPadding="4" DataKeyNames="test_id" ForeColor="Black" GridLines="Horizontal" ShowFooter="true"
        onrowediting="EditTask" onrowupdating="UpdateTask"  onrowcancelingedit="CancelEdit">
    <Columns>
        <asp:TemplateField ItemStyle-Width="30px" HeaderText="test_id">
            <ItemTemplate>
                <asp:Label ID="lbltestid" runat="server" Text='<%# Eval("test_id")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="100px" HeaderText="test_cat">
            <ItemTemplate>
                <asp:Label ID="lbltestcat" runat="server" Text='<%# Eval("test_cat")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttestcat" runat="server" Text='<%# Eval("test_cat")%>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txttestcat" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="150px" HeaderText="test_info">
            <ItemTemplate>
                <asp:Label ID="lbltestinfo" runat="server" Text='<%# Eval("test_info")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttestinfo" runat="server" Text='<%# Eval("test_info")%>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txttestinfo" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="150px" HeaderText="test_number">
            <ItemTemplate>
                <asp:Label ID="lbltestnumber" runat="server" Text='<%# Eval("test_number")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttestnumber" runat="server" Text='<%# Eval("test_number")%>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txttestnumber" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="150px" HeaderText="test_date">
            <ItemTemplate>
                <asp:Label ID="lbltestdate" runat="server" Text='<%# Eval("test_datetime")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttestdate" runat="server" Text='<%# Eval("test_datetime")%>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txttestdate" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("test_id")%>' OnClientClick="return confirm('Do you want to delete?')" Text="Delete" OnClick="DeleteTask"></asp:LinkButton>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddNewTask" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
    <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
    <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
    <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F7F7F7" />
    <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
    <SortedDescendingCellStyle BackColor="#E5E5E5" />
    <SortedDescendingHeaderStyle BackColor="#242121" />
    </asp:GridView>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>

</form>

testtableRecords.vb
Imports Microsoft.VisualBasic
Imports System.Web
Imports System.Data.Linq.Mapping

 _
Public Class testtableRecords
'DB Fieldname and type = new name _name..
Private _testId As Integer
<Column(IsPrimaryKey:=True)> _
Public Property test_id() As Integer
    Get
        Return _testId
    End Get

    Set(ByVal value As Integer)
        _testId = value
    End Set
End Property

'DB Fieldname and type = new name _name..
Private _testCat As String
<Column()> _
Public Property test_cat() As String
    Get
        Return _testCat
    End Get

    Set(ByVal value As String)
        _testCat = value
    End Set
End Property

'DB Fieldname and type = new name _name..
Private _testInfo As String
<Column()> _
Public Property test_info() As String
    Get
        Return _testInfo
    End Get

    Set(ByVal value As String)
        _testInfo = value
    End Set
End Property

'DB Fieldname and type = new name _name..
Private _testNumbers As Integer
<Column()> _
Public Property test_numbers() As Integer
    Get
        Return _testNumbers
    End Get

    Set(ByVal value As Integer)
        _testNumbers = value
    End Set
End Property

'DB Fieldname and type = new name _name..
Private _testDate As Date
<Column()> _
Public Property test_datetime() As Date
    Get
        Return _testDate
    End Get

    Set(ByVal value As Date)
        _testDate = value
    End Set
End Property

End Class

Answer: The problem is that txttestcat, txttestinfo et al. are not properties of your class gridview.  They are the IDs of objects deep in the object hierarchy of the page, specifically that of GridView1.
To access them, you need to use the FindControl method of the GridViewRow.  I'm working from memory, here, but if you replace Me.txttestcat.Text with
CType(e.Row.FindControl("txttestcat"),TextBox).Text

... and convert the other lines similarly, you should be fine.


Inner join union not same amounth of columns

Question: <code>    @Query(value="
    select * from (select * from Post WHERE Post.id < :lastPostId) as p
    JOIN Follower f ON(f.to_id = p.user_id) WHERE f.from_id = :#{#currentUser.id}
    UNION
    select * from (select * from Post WHERE Post.id < :lastPostId) as p
    JOIN public.user u ON(u.group_id = :#{#currentUser.group.id})"
    List<Post> getNewsFeedPosts(User currentUser,Long lastPostId);
</code>
Hello im trying to union two queries, but im getting exception so union doesnt have same amounth of columns.
Can  anybody help, maybe how to improve query
Comment: As Ruud states, when you UNION two select statements the columns returned from the two statements have to be identical in number and order. Try running the two statements independently and you should see that the columns returned from each are not the same.
Comment: I am guessing it is because you are joining columns to both tables that do not give you the same in your select *. Try to specify the clumns in your select.
Answer: Based on previous answers i came up with this solution
<code>   @Query(nativeQuery = true, value =
            "WITH posts as (SELECT * FROM post p WHERE p.id < :lastPostId) " +
            "select p.id,p.user_id,p.created_at,p.description,p.name FROM posts p JOIN Follower f ON(f.to_id = p.user_id) WHERE f.from_id = :#{#currentUser.id} " +
            "UNION " +
            "SELECT p.id,p.user_id,p.created_at,p.description,p.name FROM posts p JOIN public.user u ON(u.group_id = :#{#currentUser.group.id} ) ORDER BY id DESC LIMIT 5")
List<Post> getNewsFeedPosts(User currentUser,Long lastPostId);
</code>


number of 6 digit combination using M 0s and N 1s.

Question: I have seen this question which lead me ask this following question.
What is the number of unique possibilities to generate a binary string that has M 0s and N 1s.?
For example,
<code>If M=3, N=4, then possible combinations are
0001111
0101010
etc.
</code>
Answer: I think this is equivalent to say, we have $M+N$ positions from where we choose $N$ positions to fill with $1$. The number of ways is clearly then
$$
\binom{M+N}{N} = {(M+N)! \over M! \cdot N!}
$$
Answer: OK, so you have $n$ digits, of which $r$ are 0 and $s$ are 1. Obviously, $r + s \le n$.
Think of it in the following steps, which are independent:

Select $r + s$ of the $n$ positions for the 0 and 1. This can be done in $\binom{n}{r + s}$ ways.
Select $r$ of the 0/1 positions for the 0s, this can be done in $\binom{r + s}{r}$ ways
Fill the $n - (r + s)$ positions left over with one of the remaining $8$ digits, in order. This gives $8^{n - (r + s)}$ options.

Pulling all together:
$$\binom{n}{r + s} \cdot \binom{r + s}{r} \cdot 8^{n - (r + s)}$$


Redux Persist state is empty after rehydration

Question: I'm facing a strange issue. The rehydration worked in my app until I switched to remote dev tools that are being ran on a local server. Now I can see that the REHYDRATE is indeed called but all of my states remain empty after! What could be the possible cause of this? If I don't use the devtools it doesn't work either. All of my reducers return unmodified state by default. here is the configuration file:
`
const rootReducer = combineReducers({
    appState: appStateReducer,
    entries: entriesReducer,
    listHelper: listHelperReducer
})

const persistConfig = {
    key: 'root',
    storage,
    stateReconciler: autoMergeLevel2
}

const pr = persistReducer(persistConfig, rootReducer)

const configureStore = () => {
    const store = createStore(pr, devToolsEnhancer({ realtime: true, hostname: 'localhost', port: 8082 }))

    const persistor = persistStore(store)
    return { persistor, store }
}

export default configureStore

Anyone else facing similar issue?
Reducers:
const initialState = {

loggedInUser: 0,
pickedEntry: null,
bulkPicker: false,
bulkPicked: []
}
const appStateReducer = (state = initialState, action) => {
    switch (action.type) {

    case LOG_IN:
        return {
            ...state,
            loggedInUser: action.user
        }
    case PICK_ENTRY:
    return{
        ...state,
        pickedEntry: {...action.entry}
    }
    case ACTIVATE_BULK_PICKER:
    return{
        ...state,
        bulkPicker: true
    }
    case DEACTIVATE_BULK_PICKER:
    return{
        ...state,
        bulkPicker: false,
        bulkPicked: []
    }
    case ADD_TO_BULK_PICKED:
    return{
        ...state,
        bulkPicked: [...state.bulkPicked, action.entry]
    }
    case REMOVE_FROM_BULK_PICKED:
    //remove from list
    //check if anything left then change state of bulkPicker
    let index = state.bulkPicked.findIndex((entry) => entry.key === action.entry.key)

    return{
        ...state,
        bulkPicked: [
            ...state.bulkPicked.slice(0, index),
            ...state.bulkPicked.slice(index+1)
        ]
    }

    default:
        return state
    }
}

export default appStateReducer

const initialState = {
workingCopy : [],
serverCopy: [],
pendingOperations: [],
activities: [],
issueIds: []
}
const entriesReducer = (state = initialState, action) => {
    switch (action.type) {
    case SET_SERVER_COPY:
        return {
            ...state,
            serverCopy: [...action.data]
        }
    case SET_WORKING_COPY:
    return {
        ...state,
        workingCopy: [...action.data]
    }
    case NEW_ENTRY:
    return {
        ...state,
        workingCopy: [...state.workingCopy, action.entry]
      }
    case STOP_ENTRY:
      const updatedList = listHelperReducer(null, {
        ...action,
        type: "END_ENTRY_UPDATE_LIST",
        entry: action.entry,
        list: state.workingCopy
      })
      return{
          ...state,
          workingCopy: [...updatedList]
      }
    case ADD_ACTIVITIES:
    return{
        ...state,
        activities: [...action.list]
    }
    case ADD_ISSUEIDS:
    return{
        ...state,
        issueIds: [...action.list]
    }

    default:
        return state
    }
}

export default entriesReducer

const listHelperReducer = (state = [], action) => {
    switch (action.type) {
    case END_ENTRY_UPDATE_LIST:

    //find index
    const index = action.list.findIndex(
        entry => entry.key === action.entry.key
      )

    //modify list
    let listToReturn = [];
    if(index === 0)
    {
        listToReturn = [
            {
                ...action.list[0],
                endDateTime: moment(),
                totalTime: getTotal(action.list[0].startDateTime.valueOf(), moment().valueOf())
            },
            ...action.list
        ]
    }
    if(index > 0 )
    {
        listToReturn = [
            ...action.list.slice(0, index),
        {
          ...action.list[index],
          endDateTime: moment(),
          totalTime: getTotal(action.list[index].startDateTime.valueOf(), moment().valueOf())
        },
        ...action.list.slice(index + 1)
        ]
    }
    //return
     return listToReturn

    default:
        return state
    }
}

export default listHelperReducer

Comment: How do your reducers look like? You need to restore the data there
Comment: I never had to restore any data to get it to work. my understanding is that it all 'just happens' for you in redux persist starting from v5. I will post the reducers as well
Answer: To anyone trying to fix similar problem:
My issue was caused by the use of timer in my app (multiple setInterval functions). This might cause your debugger to drift away in time which is apparently causing the state not to be persisted. A restart and rebuild of the app fixed it all.


How to change password via REST when validating email address (first login link)

Question: Is there a REST api way to allow the user to set a new password on email confirmation / first time login?
I tried the same code as for changing the password when you're already logged in and adding the pass-reset-token as a GET param but without any luck. Server always returns the following message:
{"message":"Users can only update their own account, unless they have the \u0027administer users\u0027 permission."}
Does anyone have any idea? If not, I will write some custom code inspired from the core module but I hoped that the rest api should handle such things.
Comment: Just to get your point, you want to the user set their new password through the REST endpoint? If so I could provide you with an example. Althought reseting password that way you should enforce security such as using SSL for connections and JWT for payload signing to avoid MiTM attacks.
Comment: Just to avoid all that I just displayed the actual Drupal form when the user changes the password for the first time, styled it to look like the js app and added a redirect on form submission to the actual js app page
Comment: I made a module for this you can use
https://www.drupal.org/project/rest_password
Answer: I'm not sure there is a pure Drupal core way of doing this, but there are contrib modules which can help. These modules involve resetting the password when it has been forgotten but may help with understanding the process.
Password reset REST allows resetting the password via REST.

Small module to help with resetting password through rest.
This attempts to provide the same logic present with a
  pass-reset-token as you'd do with the normal interface.

REST Password Request provides an alternative method which involves generating a temporary password so the user can change their password via rest, and then log in as normal.
REST Register User with Email Verification supports email validation, but may not be exactly what you're after.
It is also worth reviewing this issue regarding anonymous REST and user entities: A REST resource's "auth" configuration MUST be non-empty, which prevents configuring it for anon access only
Comment: Was the suggested edit the reason for -1?
Answer: To avoid all security implications I just displayed the actual Drupal form when the user changes the password for the first time, styled it to look like the js app and added a redirect on form submission to the actual js app page. Works fine so far.


SuHosin configuration for Drupal 7?

Question: My web hosting provider decided to use the suhosin php extension to protect my websites.
For some Drupal 7 sites, I am now getting tons of errors and can't login:
Warning: ini_set() has been disabled for security reasons in drupal_environment_initialize() (line 675 of /home/reseller1/example.com/includes/bootstrap.inc).
Warning: ini_set() has been disabled for security reasons in drupal_environment_initialize() (line 678 of /home/reseller1/example.com/includes/bootstrap.inc).
Warning: ini_set() has been disabled for security reasons in drupal_environment_initialize() (line 679 of /home/reseller1/example.com/includes/bootstrap.inc).
Warning: ini_set() has been disabled for security reasons in drupal_environment_initialize() (line 680 of /home/reseller1/example.com/includes/bootstrap.inc).
Warning: ini_set() has been disabled for security reasons in drupal_environment_initialize() (line 682 of /home/reseller1/example.com/includes/bootstrap.inc).
Warning: ini_set() has been disabled for security reasons in drupal_environment_initialize() (line 684 of /home/reseller1/example.com/includes/bootstrap.inc).
Warning: ini_set() has been disabled for security reasons in include_once() (line 300 of /home/reseller1/example.com/sites/default/settings.php).
Warning: ini_set() has been disabled for security reasons in include_once() (line 301 of /home/reseller1/example.com/sites/default/settings.php).
Warning: ini_set() has been disabled for security reasons in include_once() (line 309 of /home/reseller1/example.com/sites/default/settings.php).
Warning: ini_set() has been disabled for security reasons in include_once() (line 316 of /home/reseller1/example.com/sites/default/settings.php).
Warning: ini_set() has been disabled for security reasons in drupal_settings_initialize() (line 772 of /home/reseller1/example.com/includes/bootstrap.inc).

Is there an alternative bootstrap without those ini_set statements?
Can we put them on Drupal?
Answer: I imagine you could try and override the setting in a local
php.ini
file.
For example.
/var/www/my-site/php.ini
disable_functions = system

Adding a single function instead of the list your host is using by default.
If that works, you could add other functions there, e.g
disable_functions = exec,shell_exec,popen,passthru,proc_open,system,pcntl_exec

Otherwise, you'll need to hack Drupal to remove those ini_set declarations and move them to the local php.ini file. ( not recommended )
You could also ask your webhost for help ( recommended )
Alternatively, find a new webhost.

SuHosin configuration for Drupal 7?


Missing Hadoop installation in Windows

Question: I have installed Hadoop 2.x and its running fine in Windows 8.
 G:\hadoop\hive2.1\bin>jps
 10916 NameNode
 1588 DataNode
 3332 Jps
 4200 ResourceManager
 2444 NodeManager

And I have installed Hive also in Windows, But when I start Hive it's throwing an error saying:
  G:\hadoop\hive2.1\bin>hive
 "Missing hadoop installation: G:\hadoop\winutils must be set"

HADOOP_HOME is already set to G:\hadoop\winutils in env variables.
Please help here.
Answer: You have wrongly set HADOOP_HOME.
Try below..
In User variables, configure HADOOP_HOME with following value.
HADOOP_HOME-->D:\Hadoop-2.8.1
In System variables, add following value in addition of existing path value.
path-->;D:\Hadoop-2.8.1\bin;
If you not ok with that above configurations just try below way.
Open cmd prompt, Just set home by setting path and home.
C:>set HADOOP_HOME=D:\Hadoop-2.8.1
C:>set PATH=%PATH%;%HADOOP_HOME%\bin
Now start hadoop services from same cmd prompt and then go to hive shell.
Hope this helpful for you.

Missing Hadoop installation in Windows