Password reset link gives access denied

Question: Password reset link is not working in drupal 8.6. The email with the one-time login link is sent just fine, but when the user clicks on the Login button, they get "You are not authorized to access this page" message. Even I tried to open user/reset/{id} with login as admin still same error. When I figuring out the issue I found one line in user.module line no 556
\Drupal::moduleHandler()->invokeAll('user_login', [$account]); is causing the issue. If I remove this line then I can reset my password successfully. whats this line is doing? Does it load all active modules in website? than there must be issue in any module. Could I remove this line? Any alter solution?
I tried by clearing cache,cookie, browser history, session table, also $cookie_domain = $_SERVER['HTTP_HOST']; this line is not in my settings.php
Comment: Take a backup of your site, then start disabling modules one at a time, and try the password reset in between each one, until you figure out which module is causing the problem.
Answer: Had similar outcome but issue was purely that the User record was still set to Blocked rather than Active.
Answer:
whats this line is doing? Does it load all active modules in website?

It invokes a hook (hook_user_login), which other installed modules can respond to. It doesn't "load" any modules, but those other installed modules are already available at that point, yes.

Could I remove this line?

Physically, yes, but it would be a terrible idea. It's important, or it wouldn't be there.

Any alter solution?

You need to debug your site, preferably with a step-debugger like XDebug. Chances are you have a rogue custom or contributed module doing something it shouldn't be.
Comment: If it can help anybody my issue is I am redirecting user on some condition in my custom module which causing this issue
Answer: This is an issue with symfony/http-foundation v3.4.24. See https://www.drupal.org/project/drupal/issues/3045844
Source: Drupal 8 dropping session variables on password reset. Gives 403 Access Denied

Password reset link gives access denied


Add ellipsis replacement to Pathauto

Question: To handle users who like to occasionally use the custom ellipsis character from MS Word in titles, I would like to add replacement options for the character in the Punctuation settings for Pathauto using hook_pathauto_punctuation_chars_alter(). The problem is determining what value to put in the value setting in the hook. I found the T_ELLIPSIS constant in tokenizer.php, and tried using it like so:
function my_module_pathauto_punctuation_chars_alter(array &$punctuation) {
  $punctuation['ellipsis'] = array(
    'value' => T_ELLIPSIS,
    'name' => t('')
  );
}

and the value shows as Ellipsis (389) for the field label in the settings. However, when the alias is generated with the ellipsis character in the node title, it doesn't get replaced.
Is there a better/easier way to handle this? Also, should I use something like the UTF-8 code instead of the constant?
Answer: My prefered solution to scrub out extraneous characters in Pathauto URLs is this option under Configuration » Search and metadata » URL aliases » Settings.

It will scrub out all the extraneous characters that can mess up browsers/crawlers. This is assuming you're on an English language website.

Add ellipsis replacement to Pathauto


Socket não recebe nem envia mensagem para o servidor

Question: Estou implementando um servidor multi thread Java que recebe mensagens dos clientes e faz um broadcast delas para os demais. Porém estou tendo problemas. O servidor só recebe as mensagens enviadas para os usuários quando os sockets dos usuários são fechados.
A aplicação está dividida em dois módulos: client para os códigos do cliente e server para os códigos do servidor.
Aqui está o código do meu projeto no GitHub para quem se sentir mais à vontade de vê-los lá. Por favor faça o checkout no branch test e não no master
Os códigos estão todos muito bem comentados e simples de entender.
Como já fiz essa pergunta no stack overflow inglês, vou apenas colocar um link para ela aqui. Os códigos estão todos lá.
Answer: Chame PrintWriter.flush() após cada write() enviando mensagem para fazer os dados serem efetivamente transmitidos.
Answer: Toda vez que você escrever algo através de um objeto PrintWriter é super recomendado que você chame o método flush() para "desengasgar" a saída. Então como o amigo já disse: Após o socketWriter.write(line) escreva socketWriter.flush().


How to find outliers in a Data Frame by group with X and Y coordinates in R?

Question: I need to find outliers by coordinates and Groups in R.
My Data Frame looks like:
Name, Group, X, Y
Bob,  2, -300, 20
Karl, 4,  200, 10
Sara, 2, -298, 19
Hans, 4,  210, 12
Anna, 3, -700, 100
Maria,3, -690, 110
Lars, 2,  100, 50
etc.

I managed to make a ScatterPlot by Group which looks like:
22334444
22335544
11335552

But I didn't managed to get the outliers by Group with automatic Clustering (nearest neighbours?) as a new Dataframe.
(like Lars, he is far away from the other Persons from Group 2)
It would be nice to have a DF like
    Outliers_DF
    Name, Group,X,  Y
    Lars,  2,  100, 50
    Sam,   3,  900, 70
    Trixi, 11, 200 , 2

Comment: How about subsetting your data frame by group to give a matrix for each group, then running `hclust(dist())` on each? Outlying nodes will be merged at larger heights.
Comment: I tried using :hclust(dist(df[,5:6])) on one group. 5 and 6 are the X and Y colums        i only getting this:

Call:
hclust(d = dist(df[, 5:6]))

Cluster method   : complete
Distance         : euclidean
Number of objects: 2                               I don't understad what to do from here
Comment: How many observations and groups do you have? Perhaps give us a bigger sample of your df.
Comment: I have about 25000 observations per measurement and ca 70 Groups. An a Group can have between 1 and ca. 6000 observations. This means a group can be an outlier if it has for example only 1 Member. A group of 5000 who gather on two spots for example 2500 and 2500 has not outliers but if it gathers on two spots with 4995 and 5 the five people should be outliers... i this understandable? Sry English is not my motherlanguage
Comment: OK. I've added a proposed solution below. It works on the test data. See if it works on your big data frame.
Answer: How about a loop that subsets your matrix by group and performs hierarchical clustering on the distance matrix of each subset. Then you can retrieve details of the most outlying observations from each hclust object in the output. First you take the group column and find the unique number of groups, then preallocate a list of this length for the output.
grpcol <- df$Group
ngroup <- unique(grpcol)
output <- vector("list", length(unique(grpcol)))

Now we run our loop: "For every group in df, subset the corresponding matrix, run hclust on its distance matrix, and put the resultant object in the preallocated list."
for (grp in ngroup) {
  mat <- df[df$Group %in% grp, -2]
  output[[grp]] <- hclust(dist(mat))
}

I've checked it works with this data frame df (note I added two extra people so it would work):
structure(list(Group = c(2L, 4L, 2L, 4L, 3L, 3L, 2L, 3L, 4L),
X = c(-300L, 200L, -298L, 210L, -700L, -690L, 100L, -680L,
220L), Y = c(20, 10, 19, 12, 100, 110, 50, 120, 14)), .Names = c("Group",
"X", "Y"), class = "data.frame", row.names = c("Bob", "Karl",
"Sara", "Hans", "Anna", "Maria", "Lars", "Person1", "Person2"))

To get the individual clusters, use [ on the group you want, for example output[2].
You'll want to extract values from $height, $order and $labels. (You can easily find how to do this by searching SO.) Also, you'll have to think of a criterion to decide what's an outlier, you could check out Cross Validated for good opinions on this.
Comment: I had to change the line : mat <- df[df$Group %in% grp, -2] because it didn't drop Group Column in mat <- df
 mat$Group <- NULL and now i have this Problem: Error: cannot allocate vector of size 27.8 Gb
Comment: Maybe you'll need to break down the data into smaller chunks. I can't really say without seeing the full data.
Comment: ok i figured out how to manage smaller chunks, but this line:                  mat <- df[df$Group %in% grp, -2]            doesn't work and i don't know why i always get : > show(mat)
[1] -2 is it possible to use another expression df[df$Group %in% grp, -2]? With your DF it works perfect
Comment: the dplyr DF was the Problem! Now I can reproduce your solution with my DF. I just have to figure out what to do with my output file. Thanks
Comment: I'm glad you're getting there with it. You can get all the heights out using `unlist(sapply(output, "[", 2))`. The ones with bigger heights have further outliers. Also, if you do `plot(hclustobject)` you can get the dendrogram and check the outliers visually.
Comment: Also, if you think it's solved, could you accept the answer? (Or at least upvote if helpful!!!) :-)


Undefined reference error for a function

Question: I am building a program that calculates the surface area of a sphere. I also made an additional feature that allows the user to input a positive number only. Before setting my loop, I defined the function for the surface area, but it wouldn't compile because the function is not defined. The code is displayed:
include <stdio.h>
#define PI 3.1415
float sa_sphere(float r);
int main()
{
    float r;
    do{
        printf("Enter a positive radius: ");
        scanf("%f", &r);
        if (r<0){
            printf("Error: number entered is not positive.\n");}
    }
    while (r<0);{

        printf("The surface area is: %.2f\n",sa_sphere(r));}
    return 0;
}

I am using Linux Mint to compile it — gcc gg.c then ./a.outwhere gg is my file name.
/tmp/ccRUfp76.o: In function `main':
gg.c:(.text+0x6e): undefined reference to `sa_sphere'
collect2: error: ld returned 1 exit status

I would appreciate any tip to solve it. Please don't display the code in the answer, though.
Comment: You didn't define the function `sa_sphere()`; you _declared_ it, which tells the compiler that it exists somewhere.  Unfortunately, you've not actually supplied the definition — and you need to write the code `float sa_sphere(float r) { …calculation…; return surface_area; }` or similar, except you'll provide the details of the calculation.  That can be in the same source file as `main()` — or possibly in a separate source file, but then you should declare the function in a header file and include the header in both `gg.c` and wherever you define `sa_sphere()`, and your compilation is harder.
Comment: Exactly that. The function is not defined, there is no implementation of the function available.
Answer: You did not define function  float sa_sphere(float r); so during linking compiler raised linking error. You must define the function in your code. If your are new please check what is function definition means. GOOD LUCK.


Rules for key changes

Question: On reading my new theory book I have come across a very interesting passage on Keys Change.

Here are the rules:
When the music changes keys, it changes to a closely related key
Closest related key is the relative minor
The other close keys to modulate to are next to the original key on the key circle
Other modulation could include the parallel, Maj C to Min C.

Source: Music Theory 101, Brian Boone and Marc Schonburn
I am not disputing this, and it makes sense especially when you look at the circle of keys but would changing to a different key completely be such a bad thing? I must admit, sadly I am not at the stage where I can 'hear' music like you guys can (or even read it all that well as a matter of fact), but would modulating to a key that's not either of the above be a bad move?
We hear of great composers, Sibelius, Nielsen, Stravinsky and Elgar who frequently composed symphonies (and other works) that were so incomprehensible, no set way of playing something, flexible, stormy, tempo not settled, melodies and harmonies abandoned and so on. Could one way to do this be to change the key to something that broke the rules or is this vital rule implemented for a reason my book has not disclosed?
Comment: Don't expect "Music Theory 101" to apply to real works by real composers. It only applies to what it says on the cover: Music Theory 101. It's like applying "Physics 101" to driving a car.
Comment: Do you want ideas and patterns for composing? Check out Mel Torme's Christmas Song, it has a brief modulation from C major to E major. It doesn't stay there for long though. So, the "rule" has exceptions ... It might be more useful for your studying to look at example cases and then try to replicate the same patterns in your own compositions. Pat Metheny's song books are good source material for finding different modulation patterns.
Comment: @cml: nothing will just "apply". You need theory 101, then theory 201, then 301 and maybe 401, and then a lot of experience that is really hard to put into words and sometimes is only reachable through metaphors. It's exactly the same as using theory of literature to analyze works of real writers.
Comment: @fdreger longing to find a book that applies real works by real composers :(
Comment: This is probably to get the beginner a feel for common, easy to digest, key changes.  In fact you can modulate form any key to any other key any way you like.  I would recommend reading Modulations by Max Reger for more.
Comment: Something that nobody seems to have touched on in answers is that this can be a component of musical form. Looking at a huge amount of music from the classical and romantic periods, you can make a set of generalizations that are described as sonata form. One of the characteristics of sonata form is that you modulate up a fifth and then later return to the original key.
Comment: @cmp, there is probably no such book, because (since the music theory is not very "rigorous", as compared to e. g. math) everyone's take on the theory is different and they're quite untransferable. The best way to get a good grasp is probably to play works of composers that you like on your instrument (and if you like a passage, you can stop there and see what makes it good) and to compose yourself. I'm a purely amateur composer, but composing made my understanding of the theory unbelievably better.
Comment: 'These are the rules'. But not for all music. Just the ground rules for this first exercise in modulation. Yes, of course music does much more than this! But not in this introductory lesson.
Answer: WARNING: This got a bit out of control. Please don't be intimidated by the diagrams and the wall of text. Also please note that in the following (and in the music, generally) the word "modulation" means just a change of key.

Obviously, these modulations are the most common ones. However, you can modulate from any key to any other key without much hassle. The only thing you need to do is to find a path between the two keys. (There are many, many possible paths between any two keys, so it's your choice. It can be shorter or longer, whatever you like.)
The book talks about the modulations in the terms of the circle of fifths, which is, in my opinion, a great approach, so I'll stick to it in this post.
As your book correctly notes, the safest modulation is to the neighboring keys or to the relative minor/major keys. So for now, the possible paths look like this (see the left circle):

I drew the paths only for one key (C major), but it works just the same way for all 11 other keys. (The drawing would also get messy very fast.)
Another, very common and useful way of modulating, is using the dominant seventh chord in the minor keys (for instance, Ami - E7). This makes it possible to modulate up by 4 sharps very fast. I drew the paths that use this in blue on the right image.
However, we can extend this logic as far as we want. Basically: whenever we find two chords that sound well one after another, we can use them to modulate. The only thing we need to do is to "hammer it home" after modulating, so there is no doubt that we really are in the new key.
So say that we would like to modulate from C major to E major. One possible path would be going through A minor. So we play chords: C -- Ami -- E. But with this, the modulation is not yet done (in fact, if we stopped with E, it would look like we ended in A minor and that E was used to "hammer the A minor key home"). We need to play something that will firmly root us in E major, and that needs to be some chord that 1) pulls strongly to the E major chord, and 2) is not contained in A minor, so that it's really clear we're not in A minor anymore. A good choice would be B7 (with its D# and F#, it makes it very clear it does not belong in A minor). So we would play something like: C -- Ami -- E -- B7 -- and now we are in E major, so we can continue with anything from that key, for instance F#mi -- G#mi -- A -- B -- E. We're just firmly there.
But this mechanism can be used for any transition. So let's say we found out that playing Ab major after a C major sounds good. We can immediately use it to modulate: C -- Ab -- Eb7 -- Ab and we're there. We can also target other chords than the tonic: so if I wanted to go from Bb major to G major, I can do Bb -- Eb -- D (here we ended up at the dominant of the target G major) -- G. (Or Bb -- Gmi -- D7 -- G. Or anything.) If you can play the guitar (or any other instrument capable of harmonies), I think you should try these simple progressions out.
And now, there are lots of chord pairs that sound good. We can, for instance, use the idea of secondary dominants: we can prepend any chord from our key by its own dominant chord and it will just sound good. So we can "embellish" the progression C -- G to C -- D -- G (D major is a dominant to G, and it works out in spite of the fact that it does not belong into C major.) Or we can play: Emi -- F#7 -- B7 instead of just Emi -- B7.
So let's draw that into our diagram (with magenta arrows, see the left circle below). And while we are at it, we can also use modal progressions (for instance a minor to D major, making it sound like the Dorian mode, or C major to Bb major, which sounds like the Mixolydian mode, etc. -- green arrows). Or we can use the simple modulation to the minor/major key of the same name (using the dominant chord, for instance C -- G7 -- Cmi; goldish arrows). Or we can use any trick we can think of (some of my favorite are shown in orange arrows -- also note that sometimes you can make up two or more reasons for drawing a single arrow between two keys!).

The diagram is pretty confusing by now, but it illustrates my point: you can easily go from anywhere to anywhere just in a couple of steps. And all of them can be made pretty smoothly, so don't think that half of those arrows corresponds to some cringeworthy modulations that are maybe possible in theory but ugly in practice. It's exactly the other way around: there are many more possible ways to make beautiful modulations, but drawing the arrows wouldn't really serve more purpose :--). And mind you, the same set of arrows is going from each of the other 11 minor and major keys.
And these are only the possibilities of modulating "by harmony". You can also modulate with melody only. It's also possible to use a modulating sequence (a simple lick that sounds well if followed with its own transposed copy), or a device that I would call "chromatic mess", essentially starting to use weird chords that do not evoke any particular key (like the diminished 7ths, or maybe some quartal/quintal harmonies), chaining them chromatically, and emerging in a totally different key. Really, the sky is the limit here!
Comment: @Ramillies It is, or even up two semitones, but with no subtleties whatsoever, i.e. you simply end one chorus in C and then start another in D.
Comment: I know you feared this would be difficult to understand, but I found it very clear.  The final circle covered with colored lines illustrates well that there are many more ways to modulate than one could fit on the circle.  Nice answer, thanks for taking the time to write it.
Comment: And this doesn't even touch on the "trucker's key change"!
Comment: @chrylis-onstrike-, it doesn't, because I don't know what that is! :--) (If I had to guess, would it be moving up/down a semitone?)
Comment: Ah so. I know this under the name of "pop modulation" :--).
Answer: I wouldn't say this is a "rule" so much as a guideline for the most common modulations.  Most theory textbooks have something called "closely-related keys" or a similar term.  They generally include (1) the relative major/minor, (2) keys within one sharp or flat (including their relative majors/minors), and (3) the parallel major or minor.  That's the same list you quoted there.
This is by no means a "rule."  It's just a list of keys that are relatively easily accessible through basic modulations by common chords and such.  It's quite easy to modulate to more "distantly related" keys too.  It is just generally accomplished by different, more "advanced" techniques (e.g., common-tone modulation, enharmonic modulation or reinterpretation, tritone substitution, etc.).
I'd also note there's a kind of historical bias to these guidelines.  Originally, instruments were not necessarily equally tempered.  That is to say, some keys were more "in-tune" than others.  Many historical instruments were designed to play in specific keys or closely related ones, and notes outside those keys were more difficult to execute and/or increasingly out-of-tune.  Thus, the concept of "closely-related keys" was important historically, as in some cases it defined limits on reasonable modulations that could still be performed well and relatively in-tune.
However, with the gradual push toward equal temperament that occurred from roughly 1700 to 1900, all keys on most instruments became roughly playable.  So abrupt modulations connecting far-away keys also become more reasonable and occurred more frequently.  Still, introductory theory textbooks generally begin with "closely-related key" modulations, as they are the most frequently encountered and often the easiest to accomplish convincingly in beginner exercises.
Answer: I'd put those 'key changes' quoted more as modulations. Moving, in some cases so subtly that some listeners might be unaware - and also retaining the facility to return, subtly again, to the original key.
They missed the two most common key changes in moders music - upa one semitone, and up one tone. Neither of which, particularly the former, leaves a simple and mussical way to return.
We really have to find a more exact word that 'rules' for these sort of questions. 'What has been found to work already' springs to mind, but it hardly trips off the tongue, let alone the fingertips! And the penalties for breaking these 'rules' - progress! If we all stuck religiosly to those 'rules', music would stagnate. Rant over!
Basically, use those 'rules' as and when you feel the need, but why not try out other ways and targets to change key? And, as I said in an earlier question, all the fuss is over within two bars, maximum, by which time the listener is involved in the new key, and cannot continue to be in the old key, mentally, physically, or musically.
The theory book quoted has started at the beginning, with basics. It's the start of the journey, and hasn't gone far. Bit like, when you were three, you couldn't take 5 away from 2. Good enough at that time?
Answer:
Rules in music have been developed of the practice - if not a real Pope has postulated some rules.
There have always been music popes - theorists -
which have their disciples and followers and composers - practitioners - who were founder of a new theory with  their own community and their own school.
Modulation is referring to the term mode  - and a change of mode was historically changing within modes of a common scale.
It makes sense that a theory book is oriented on the historical development of harmony (as-well as melody and counterpoint).
The history of music is an endless story of the wrestling of tradition and revolution.

Btw: There are many harmony books treating modulation with examples of original works of great composers.
Just as example: The most recent I've found are the books by Percy Goetschius!
https://archive.org/search.php?query=creator%3A%22Goetschius%2C+Percy%2C+1853-1943%22

Here are some pages of the book:
The theory and practice of tone-relations : a condensed course of harmony conducted upon a contrapuntal basis
https://archive.org/details/theorypracticeof0000goet
Comment: Yes! I have found an actual PDF. Thanks so very much. I can already tell this will be a good read :))))))))))
Comment: Wow, that actually looks like a really good book!
Comment: @Ramillies if you google Percy Goetschius there's a good PDF result for the whole book!!!
Answer: The term for a changing of key is 'Modulation'.
The truth is you can modulate to any diatonic step in the scale.
You would modulate to the type of chord the scale degree has, let me explain what I exactly mean by that.
If we take C major as an example
Modulation to the 2nd note.
You can modulate to the second step (called the Super Tonic). So the key of D, the super tonic chord in a major key is a minor chord so if your parent key is C major and you modulate to the super tonic, you go to d minor.
You can modulate to the 3rd note
You can modulate to the third step (called the Mediant). So a key starting on E in this example. The mediant chord in major keys is a minor chord so if your parent key is C major and you are modulating to the mediant you are heading towards e minor.
You can modulate to the 4th note
You can modulate to the 4th note (called the Sub Dominant) So a key starting of F if our parent key is C. The 4 chord in a major key is a Major chord so in this instance we would go the  F major if we modulate to the Sub Dominant
You can modulate to the 5th note
You can modulate to the 5th note (called the Dominant) So a key starting of G if our parent key is C. The 5 chord in a major key is a Major chord so in this instance we would go the  G major if we modulate to the Dominant
You can modulate to the 6th note
You can modulate to the 6th note (called the Sub Mediant) So a key starting of A if our parent key is C. The 6 chord in a major key is a minor chord so in this instance we would go the a minor if we modulate to the Sub Mediant
You can modulate to the 7th note
You can modulate to the 7th note (called the Leading Tone) So a key starting of B if our parent key is C. The 7 chord in a major key is a diminished chord, but the minor third in that chord tells us we should be going to b minor if our parent key is C major and we are modulating to the Leading Tone key
off course you can go to parallel keys and the relative keys
There is off course modulations that are easier to make convincing than others, but all these in this answer is possible and if you go along the theory path long enough you will learn to do them all.
Comment: There are many more modulations than these. And to change key often means to go from major to another tonic major, as in from C maj. to D maj. Key C going to B minor sounds quite unconvincing - you use the leading note to be the tonic of a key. It really isn't.
Comment: The second line statement just isn't true. Pieces modulate up a semitone as much as anywhere else. Say from E to F. F isn't diatonic in E, so there's a misleading statement.
Answer: As Max Reger brilliantly demonstrated, there isn't really any rule. You may go from any tonic region to any other directly, just knowing how to voicing the changes. The "rules" we encounter all over the web are more stylistic and didactic than practical. For example, in Mozart's time, the style employed banned too much tensions and modulations to far related keys. His famous quartet dubbed "Dissonances" is highly dissonant for his contemporaries, but not dissonant at all, not to all styles that came after.

Rules for key changes


PHP Regex Remove Periods That Appear Next to Each Other

Question: I am working with a body of text that sometimes includes periods that occur right next to each other. Example:
Today is a nice day....."It is almost time for lunch"..
How can I turn that into this:
Today is a nice day. "It is almost time for lunch".
I have tried using $_input = preg_replace("/.+/",".",$_input); but this seems to remove everything except a bunch of periods, which it leaves there.
Any help at all is appreciated, Thanks!
Answer: The . character is a meta character in regular expressions meaning "match any character except newline (by default)". So if you want to match a literal period you need to escape it in your regex:
$_input = preg_replace("/\.+/",".",$_input);

Comment: Beat me to it, to see a list of all reserved characters: http://www.regular-expressions.info/characters.html
Comment: Thanks so much guys! That fixed most of the problem, however I am still having this occur a lot ".. (one quote followed by 2 periods) and the `$_input = preg_replace("/\.+/",".",$_input);` doesn't seem to get rid of it. Any ideas?


Email does not contain a link to the file attached to the contact form

Question: I am using the following code that programmatically creates a file and attaches it to a file field when a contact form is submitted.
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'custom_submit_handler';
}

function custom_submit_handler(&$form, &$form_state) {
  $file_contents = '777';
  $filename = 'my_file.txt';
  $file = File::create([
    'uri' => 'public://' . $filename,
    'filename' => $filename,
  ]);

  $file->save();
  file_put_contents($file->getFileUri(), $file_contents);

  $entity = $form_state->getFormObject()->getEntity();
  $entity->get('my_field_file')->appendItem($file);
  $entity->save();
}

The code works as expected, but there is no link to the new file in the sent email.
What should I do to change the email message?
Answer: The solution is to use hook_entity_presave() instead of hook_form_alter()
function mymodule_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity->getEntityTypeId() == 'contact_message') {
    //create and save file
            ...
    //then
    $entity->set('my_field_file', $file);
  }
}

Answer: In order to enhance the content of our outgoing emails, I strongly recommend utilizing the hook_mail_alter function. This function allows us to modify the email content dynamically just before it is sent. By leveraging this feature, we can personalize and optimize our messages to better engage our recipients.
To learn more about hook_mail_alter and how to implement it effectively, I encourage you to visit the following link: hook_mail_alter.
The provided documentation will guide you through the process of using hook_mail_alter to tailor the content of our emails to suit specific scenarios and recipients. It's a powerful tool that can help us improve the effectiveness of our communication efforts.
 function hook_mail_alter(&$message) {
   if ($message['id'] == 'modulename_messagekey') {
    if (!example_notifications_optin($message['to'], $message['id'])) {

     // If the recipient has opted to not receive such messages, cancel
     // sending.
     $message['send'] = FALSE;
     return;
   }
   $message['body'][] = "--\nMail sent out from " . \Drupal::config('system.site')
  ->get('name');
    }
 }

Comment: The question is about the *contact_message* entity and a field in that entity that needs to be changed. `hook_mail_alter()` does not allow to change that entity. Furthermore, an answer should be tailored for the asked question. Providing a generic hook implementation does not answer the question.


Passing callback to a JSNI function in GWT

Question: My GWT app is designed to have an iframe with another web app (not GWT this time) embedded in it.  This embedded app expects to be able to call some APIs in my parent app like so:
window.parent.f(function(result) { console.log("Result == " + result); });

How do I declare a JSNI function that can take that function as a parameter?
private static final void f(Consumer<String> onsuccess) {
    onsuccess.accept("abcd");
}

public static native void installApi() /*-{
    $wnd.f = function(onsuccess) {
        @my.package.client.Example::f(...); // What goes in here?
    }
}-*/;

Answer: Look at JsInterop. It's currently best and easiest approach to communicate between Java and JavaScript in GWT.
http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJsInterop.html
Beside that, there are security limitations when Iframe is loaded from different domain / port.
Calling a parent window function from an iframe
EDIT:
Try this:
@JsType(name = "Foo", namespace = JsPackage.GLOBAL)
public class Foo {
    @JsFunction
    public interface Callback {
        public abstract void onCallback(String msg);
    }

    public void test(Callback callback) {
        GWT.log("Message from Java");
        callback.onCallback("Massage passed from Java");
    }
}

And call this by:
var foo = new $wnd.Foo();
foo.test(function(msg) {
    console.log("Message from JavaScript");
    console.log("Received: " + msg);
});

And most important that it's hidden in docs, you need to add this option to compile and superdev mode params.
-generateJsInteropExports

Comment: I've been reading the jsinterop docs, but i've not managed to get to grips with it yet.  The security limitations are well understood, in this case the child app is loaded from the same domain


OpenSSL for making iOS certs on a windows machine - do I need a server?

Question: I'm running Windows 7 Pro, not a server, and am wondering if I need a server to run openssl for the purpose of setting up my iOS certs for PhoneGap?
I've installed opensll, and am following instructions for making the cert here:
http://www.iandevlin.com/blog/2012/11/phonegap/building-an-ios-signing-key-for-phonegap-in-windows#more-2582
But when I enter
    openssl genrsa -des3 -out ios.key 2048
it tells me 'opensll is an invalid command', displays a list of standard commands, and I cannot find the file ios.key on my machine.
I'm running the command window by shift-right-clicking on the bin folder in OpenSSL-Win64
Am I on the right track?
Thanks for your time and help.
Comment: You probably need to put the OpenSSL binaries on path. See, for example, [Adding directory to PATH Environment Variable in Windows](https://stackoverflow.com/questions/9546324/adding-directory-to-path-environment-variable-in-windows).
Answer: Never mind - for anyone else looking, I found a great guide to the whole process here:
http://support.brightcove.com/en/app-cloud/docs/step-step-guide-publishing-apple-app-store-using-windows
I was running the Shining Light installation of openssl from my own command window instead of running openssl.exe, which is why I was getting the 'opensll is an invalid command' error. But even so, errors about missing equal signs from the openssl.cnf (which I hadn't touched) kept displaying when running openssl.exe. I instead used a different openssl installation, found in the above link, which worked fine.
Comment: this is essentially a link only answer. While its great that you took the time to share the fix, you should explicitly state what fixed your particular problem so others can benefit.


How do you use the build_native.sh tool?

Question: I followed this tutorial on raywenderlich.com and I came into a stop on the part where I need to build the native C++ libraries for Android Development.
I am using Mac OS,Cocos2d-x v2.0.1 (latest version), have my Android SDK and NDK set up, and my Eclipse (Helios) with the correct plug-ins needed for C/C++. Now I'm have trouble building the Cocos2d-x libraries for Android development. I'm using the build-native.sh tool. If I use it on the terminal like so:
xxxxx-xxxxx-imac:proj.android xxxxx$ ./build-native.sh
I get the following:
Using prebuilt externals
Android NDK: WARNING: Ignoring unknown import directory: /Users/xxxxx/Desktop/LANCE/COCOS2DX/cocos2d-2.0-rc2-x-2.0.1/cocos2dx/platform/third_party/android/prebuilt
Android NDK: /Users/xxxxx/Desktop/LANCE/COCOS2DX/cocos2d-2.0-rc2-x-2.0.1/CocosDenshion/android/Android.mk: Cannot find module with tag 'cocos2dx' in import path
Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined ?
Android NDK: The following directories were searched:
Android NDK:
make: Entering directory `/Users/xxxxx/Desktop/LANCE/COCOS2DX/cocos2d-2.0-rc2-x-2.0.1/testproj2/proj.android'
jni/Android.mk:19: *** Android NDK: Aborting.    .  Stop.
make: Leaving directory `/Users/xxxxx/Desktop/LANCE/COCOS2DX/cocos2d-2.0-rc2-x-2.0.1/testproj2/proj.android'

I looked at /Users/xxxxx/Desktop/LANCE/COCOS2DX/cocos2d-2.0-rc2-x-2.0.1/cocos2dx/platform/third_party/android/prebuilt but this directory does not exist (the platform folder is missing from the cocos2dx folder)
How do I fix this?
Thanks in advance.
EDIT: After looking through the scripts, I noticed that the cocos2dx folder in the cocos2dx root folder does not have an Android.mk file.
Answer: A re-unpacking of the Cocos2d-x ZIP did the trick. Turns out the files needed were deleted (for some reason) in the Cocos2d-x root folder.
Answer: build_native tool is obsolete now, Cocos2d-Console is used instead.


How to store swift enum in core data?

Question: Swift allows you to define enum but core data doesn't support (out of the box) how to save them.
The recommended solution I have seen on the internet (and used thus far) is to use a private variable:
class ManagedObjectSubClass : NSManagedObject
{
  enum Cards : Int
  {
    case Diamonds, Hearts
  }
   @nsmanaged var cardRaw: Int

   var card : Cards {
     set { self.cardRaw = newValue.rawValue }
     get { return Cards(RawValue:cardRaw)! }
   }
 }

An alternate solution is given in the answer below.
Answer: Another approach is to use the primitive functions. This avoid having to define two variables. In the model editor card is defined as an Int.
class ManagedObjectSubClass : NSManagedObject
{
  enum Cards : Int
  {
    case Diamonds, Hearts
  }

   var card : Cards {
     set {
        let primitiveValue = newValue.rawValue
        self.willChangeValueForKey("card")
        self.setPrimitiveValue(primitiveValue, forKey: "card")
        self.didChangeValueForKey("card")
     }
     get {
        self.willAccessValueForKey("card")
        let result = self.primitiveValueForKey("card") as! Int
        self.didAccessValueForKey("card")
        return Cards(rawValue:result)!
    }
   }
 }

edit:
The repetitive part can be moved to an extension on NSManagedObject.
func setRawValue<ValueType: RawRepresentable>(value: ValueType, forKey key: String)
{
    self.willChangeValueForKey(key)
    self.setPrimitiveValue(value.rawValue as? AnyObject, forKey: key)
    self.didChangeValueForKey(key)
}

func rawValueForKey<ValueType: RawRepresentable>(key: String) -> ValueType?
{
    self.willAccessValueForKey(key)
    let result = self.primitiveValueForKey(key) as! ValueType.RawValue
    self.didAccessValueForKey(key)
    return ValueType(rawValue:result)
}

How to store swift enum in core data?


Definição do "margin-top" em porcentagem de uma "div" funciona de forma estranha

Question: Possuo uma página .xhtml e o primeiro elemento a ser colocado é uma div(content) e eu defino para ela uma margem superior(margin-top) de 30%.
Supostamente, pelo que entendo, esta porcentagem deveria ser tirada a altura da minha tela, no meu caso tenho uma tela de 1600 × 795 e os 30% seriam tirados dos 795px.
Vejam o que acontece:

#content
{
  width: 300px;
  height: 200px;
  margin: 0 auto;
  margin-top: 50%;
  background: red;
}

No exemplo eu coloque 50% de margin-top para poder passar a percepção do que ocorre. Ele deveria estar a 50% do topo mais parece ter descido muito mais.
Comment: O Fiddle não está passando a ideia a 100% mas acredito que já ajuda.
Answer: Realmente se você for simplesmente pela lógica daria a entender que o margin-top e margin-bottom quando especificados em % seriam um valor referente a altura do elemento pai, porém não é assim que funciona.
Quando a margem é especificada em '%' ela sempre vai ser referente a largura (width) do elemento pai, você pode conferir isso facilmente redimensionando a janela, você vai ver que o elemento vai mudar de posição apenas quando você alterar a largura da janela.
Comment: É isso mesmo que ocorre, como se pode ler na especificação: http://www.w3.org/TR/CSS2/box.html#margin-properties
Answer: Mexi um pouco no seu CSS e ta ai:

#content
{
width: 300px;
height: 200px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
background: red;
}

Creio que assim já resolva seu problema
Comment: obrigado, mas a minhq questão não é fazer a coisa funcionar é perceber como ela funciona. A tua solução centraliza totalmente o elemento (se percebi bem), mas no meu exemplo eu queria perceber porquê o `margin-top` funcionou do jeito que funcionou.
Comment: então realmente sua pergunta não foi muito clara ... ou devo ter algum problema mental rs
Answer: adiciona no css da div #content
display: table-caption;

Comment: A sua resposta ficaria mais completa se você adicionasse uma pequena explicação em como isso resolve o problema.
Answer: Como já foi dito, margin e padding com porcentagem, utilizam da largura do elemento. O margin se baseia no elemento pai. E o padding se baseia no próprio elemento.
Se sua intenção era que o elemento, ou seja, seu #content fique centralizado, você deve fazer como no exemplo a seguir.
 #content {
     position: absolute;
     display: block;
     width: 200px;
     height: 200px;
     top: calc(50% - 100px);
     left: calc(50% - 100px);
 }

no exemplo eu digo que meu #content terá uma distância de 50% do topo do próximo elemento pai que tenha position: relative, menos a metade da altura do meu elemento, pois quando usamos o atributo top, ele se referencia pelo topo do elemento e não pelo centro. Do mesmo jeito para o left, só que a distância é de 50% menos a metade da largura do elemento.


Alternating AUC curve. What does it mean?

Question: Why do I see my ROC curve crossing the line from (0,0) to (1,1) (i.e. the 0-1 line)?
I have the following test data as a tab-separated testdata.txt file. Running my R code (given below) multiple times, I get a few ROC curves that cross the 0-1 line multiple times. What may be the possible explanation for this, other than not enough data?
Thanks.
testdata.txt
pred1   pred2   pred3   response
25  3.8 13.4    0
29  5.3 23.5    1
30  4.3 14.1    1
29  3.8 16.1    0
26  5.7 22.9    0
20  4   13.6    0
28  3.2 14.8    0
23  5.1 16.9    0
24  4.3 15.2    0
25  5   15.2    0
29  4.1 13.7    0
24  3.7 14.7    0
29  4.7 13.8    0
28  3.9 14.2    0
25  4.6 15.4    0
24  3.8 13.9    0
33  4.3 17.1    0
33  5   16.6    1
28  4.6 15.2    0
29  4.5 15.6    0
26  4.1 14.3    0
31  4.4 12.6    0
24  4.7 15.2    0
21  5   16.1    0
24  4.1 13.3    0
29  4.1 17.7    1
29  4.2 18.4    1
27  4.5 17.3    0
27  4.4 16.4    1
28  4.5 17.6    1
29  4.7 17.8    1
25  4.1 15.2    0
29  4   13.3    0
29  3.9 13      1
27  3.9 17.9    0
25  3.2 13.6    0
22  4.8 13.4    0
26  5.5 14.2    0
24  4   16.6    0
38  3.2 14.8    0
33  3.8 13.6    1
24  3.1 16.5    1
23  4.8 14.5    0
27  5.4 13.5    0
27  4.1 12.8    1
30  3.9 13.1    0
24  5.4 13      0
25  4   12.3    0
28  4   16.5    0
30  4.6 13.5    0
18  3.6 13.7    0
24  3.2 13      0
27  4   14.3    0
33  3.6 17.1    0
24  3.2 15.5    0
24  3.2 13.3    0
24  4   14.2    0
26  4.2 14.3    0
23  3.2 16.9    0
16  3.4 17.1    0
21  4.3 21.1    0
27  4.5 13.5    0
29  4.1 13.5    0
27  3.9 14.1    0
27  4.1 13.6    0
27  4.6 12.7    0
23  4   15.2    0
29  3.7 15.1    0
33  3.6 17.1    1
16  5   16.6    0
21  5.4 16.7    0
27  4   11.5    0
29  3.7 12.7    0
26  3.7 11.7    0
16  4.3 16.6    0
25  4.3 13.5    0
24  3.8 12.9    0
33  3.2 14.1    0
24  3.4 14.2    0
26  4.1 13.9    0
26  5.3 14.4    0
16  4.3 21.1    0
20  5.4 17.2    0
29  4.1 13.1    1
32  3.1 13.7    0
31  3.8 12.8    0
29  4.8 16.7    0
33  4   14.7    0
16  5   17.1    0
29  4.6 13.2    0
23  3.4 12.7    0
26  4.5 12.2    0
25  4   13.4    0
31  3.6 13.5    0
32  5.3 13.9    1
33  4.3 16.6    0
29  4.1 15.3    0
18  4.2 14.9    0
24  5.9 16.7    0
25  4.3 11.8    0
29  4.3 13.7    0
27  5.7 13.6    0
29  4   12.4    0
24  4.5 13.8    0
27  3.8 14.1    0
34  4.2 12.4    0
30  4.3 13.1    0
26  3.4 15.9    0
22  4.3 15.6    1
33  4.2 13.2    0
25  3.2 13.4    0
28  4.6 15.5    0
24  4.8 14.6    0
24  3.9 12      0
29  4.9 14.7    0
28  4.5 13.5    0
25  5   15.2    0
28  4.8 14      0
35  3   15.1    0
27  4.5 12.8    0
31  4.6 15.6    0
22  4.7 14.3    0
25  3.9 13.6    0
28  5.6 14.7    0
21  4.3 19.9    1
31  3.8 16.1    1
26  4   15      1
21  4.8 14.4    0
30  4.3 14.7    0
25  5.3 12.1    0
30  4.8 13      0
29  3.9 15.1    0
32  4.4 14.7    1
24  4.3 14.2    0
29  4   14.2    0
29  4.3 12.6    0
23  3.7 17.5    0
29  4.2 14.8    0
29  4.8 15.2    0
31  6.1 13.6    0
27  4.3 13.1    0
27  4.4 12.3    0
28  5.1 13.9    1
32  4.2 11.9    0
30  4.3 12.5    0
26  3.7 12.2    0
28  4   12.5    0
29  2.8 13.5    0
30  3.6 12.2    0
29  4.1 13.5    0
27  5.2 16.5    0
20  6.4 16.7    0
29  4.6 12.1    0
24  4   14.2    0
22  3.8 13.3    0
34  4.6 13.4    1
31  4.5 13.6    0
28  4.9 12.5    0
33  4   12.6    0
29  4   13.9    0
29  4   12.7    0
28  3.8 12.5    0
27  4.2 14.2    0
29  3.8 15.2    0
26  3.8 13.5    0
27  5.8 13.8    0
22  3.8 11.7    0
28  3.7 13.2    0
25  4.1 13.9    0
29  4.1 14.1    0
13  4.3 19.3    0
27  4.5 18.3    0
24  4.6 13.8    0
37  3.8 12.3    0
25  4.8 11.9    0
30  4.1 12.5    0
23  3.7 13      0
26  3.8 14.1    0
34  4.1 12.9    0
24  4.5 16.9    0
25  3.7 12.7    0
30  4   12.7    0
25  5   12.3    1
29  3.9 14.2    0
25  3.9 13.6    0
27  4.5 12.8    0
29  4.1 13.6    0
28  4   13.4    0
28  4.1 13.9    0
27  3.5 13.9    0
26  2.7 18.8    1
30  4.6 12  0
25  4   13.6    0
26  4.9 14.7    0
24  4.1 13.2    0
26  4   13.2    0
16  4.3 15      0
33  5.3 15.7    0
24  4.8 15.5    0
29  3.2 13.6    0
30  4.4 12.6    0
28  3.8 13.5    1
24  4.3 13.3    0
33  4.2 13.8    0
30  4.2 13.2    0
26  3.6 12.5    0
24  4.1 13.5    1
16  3.1 13      0
24  3.4 17.6    0
24  3.2 17.6    0
24  5.4 16.9    0
29  4   16.2    0
25  4.2 13.2    0
21  6   19.6    1
25  4.3 13.5    0
28  3.7 12.2    0
27  4.1 14      0
16  4.1 19.7    0
29  4.4 14.6    0
32  4.1 16.9    1
29  3.6 15.6    1
30  4   15.7    0
23  4.4 14.4    0
30  2.9 12.8    0
27  4.5 11.8    0
24  4   17.4    0
30  4   13.1    0
30  4   12      0
24  5   14.2    0
24  4.5 13      0
26  3.7 16.1    0
27  3.2 15.1    1
27  3.5 12      0
30  4.6 13      0
24  4   14.9    0
31  3.9 13.8    0
29  4.1 14.6    0
25  6.4 19.6    0
25  4.1 14.8    0
33  3.6 15.4    0
32  3.9 14.4    0
25  4.1 12.1    0
24  3.7 13.3    0
23  4.1 14.3    0
28  4.7 13.5    0
33  4.1 15      0
26  3.8 12.4    0
32  4.2 11.7    0
23  3.7 13.8    0
30  3.4 12      0
32  3.6 11.8    1
26  4.1 12.4    0
24  4.3 12.5    0
24  4.5 15.5    1
26  4.7 13.5    0
26  4.4 13.3    0
33  4.3 18.3    0
28  4.2 13.3    0
23  4.5 23.9    0
25  3.8 12.4    0
22  5.5 15.5    1
30  3.5 12.8    0
30  4.1 11.8    0
29  4.5 12.1    0
28  3.8 13.6    0
25  3.9 12.9    0
29  4.2 13.2    0
29  3.8 13.8    0
25  4.1 12.7    0
27  4.4 12.2    0
24  3.4 13.9    1
24  4.1 16.4    1
21  5.9 22.7    0
21  5   16.6    0
26  4.7 13.4    0
24  3.9 12.7    0
27  4.2 12.2    0
23  4   13.5    0
24  3.8 14.2    0
22  3.9 12.6    1
30  5.1 13.7    0
25  3.9 14.2    0
25  4.4 12.1    0
23  4.1 13.8    0
30  4   12.9    0
27  4.6 21      0
33  3.1 17.1    0
26  3.1 13.6    0
26  5.7 17.1    0
28  4   14.2    1
26  4.3 12.2    0
25  4.1 13.3    0
28  3.4 14.6    0
24  3.9 13.6    0
27  3.9 12.5    0
24  3.4 13.9    0
27  4   12.5    0
24  4.3 13.9    0
25  4.1 13.4    0
24  5.3 19.6    0
31  4.9 13.3    0
25  4.2 13.1    0
21  3.7 13.9    0
26  4.1 11.7    0
24  4   13.3    0
27  3.7 13.2    0
26  4.6 13      0
24  4   13.6    1
25  3.7 15.7    0
26  4.3 13      1
23  3.7 15      0
28  4.1 14.4    0
22  5.4 13.7    0
27  5.3 13.8    0
25  4.2 13.1    0
26  4.3 13.9    0
29  4.8 16      0
23  3.4 16.7    0
27  4.2 15.7    0
29  3.8 12.1    0
27  4.2 13.9    0
24  3.9 13.5    0
27  5.7 17.8    0
29  4.1 14.3    0
29  5   13.2    0
34  4.5 11.8    0
26  5.7 17.1    0
28  3.8 18.6    0
29  4.5 14.2    0
31  3.8 21.4    0
30  4.7 13.7    0
27  4   18.6    0
24  4   14.7    0
29  4   15.2    0
26  3.4 11.9    1
27  4.3 13.4    0
30  4.6 13.7    0
29  4.2 13.1    0
22  4.3 13.2    0
28  4.4 13.1    0
21  3.7 14.2    0
25  3.8 17.4    0
28  4   18      0
26  3.6 17.3    0
24  4.3 13.3    0
32  4   12.1    1
31  4.2 12.5    1
38  3.1 19.6    1
28  4.3 13.1    1
22  4.5 17.1    0
24  3.2 13.8    0
30  3.9 13.7    0
26  4.2 12.2    0
25  3.2 17.1    0
26  4.5 13.2    0
31  4   13.6    0
33  4.3 17.1    0
30  4.1 13      0
21  4.3 14.7    1
25  3.9 13.3    0
16  3.2 13.6    0
26  4.3 16.6    0
21  4.3 16.6    0
28  3.8 11.6    0
24  4.4 12.8    0
30  4   12.4    0
30  5.6 14.9    0
26  4.5 13.6    0
27  4.3 11.8    0
27  4.2 11.5    0
26  3.6 13.6    0
24  3.1 19.3    0
24  4   14.8    0
25  3.8 12      0
23  4.2 12.8    0
23  4.1 14.3    0
30  4.2 12.4    0
28  4   13      0
29  4.7 13.2    1
23  4.9 14.7    0
22  5.3 14.9    1
25  5.5 15.1    1
34  4.3 14.7    0
26  4.1 12.3    0
24  4   16.2    0
29  4.5 12.5    0
21  4.3 17.1    0
30  4.2 12.1    0
25  4.1 13.6    1
26  4   14.2    0
33  3.9 11.7    0
24  3.2 13.2    0
25  3.6 12.5    0
27  4.3 13.5    0
25  3.4 15.2    0
29  4.3 13.4    1
24  4.2 13.5    0
21  5.9 19.6    0
26  3.4 25.6    0
26  4.3 15.4    0
29  4.5 15.6    0
28  4.1 14.6    0
28  4.4 16.9    0
21  3.9 14.2    0
24  3.4 13.9    0
25  4.3 13.5    0
33  5.5 19.9    0
29  3.9 16.9    0
24  3.4 14      0
24  3.9 16.7    0
23  4   13.4    0
30  4.6 15.1    0
32  4   13.3    0
16  3.2 19.6    0
29  4.5 16.6    1
28  5   14.8    0
29  4.1 15.3    0
25  4.2 14      0
26  3.5 11.9    0
30  4   12.2    1
25  4   13.8    0
24  3.4 14.2    0
25  3.4 12.4    0
24  4.3 14.5    0
25  4   13      1
35  4.3 14.9    0
24  5.3 14.7    0
25  3.4 13.6    0
22  3.8 15.6    0
24  4.2 14.3    0
33  8.3 21.2    0
30  4.2 13.8    0
25  3.8 13.1    0
32  3.5 14.5    0
29  3.7 11.7    0
23  4   14      1
24  4   12      0
32  4.2 13.8    0
24  4   12.1    0

R code
myData = read.table("testdata.txt", sep="\t", header=TRUE)
nr = length(myData[,1])
nr
testidx = sort(sample(1:nr, 0.3*nr, replace=FALSE))
dataTraining <- tmpData[-testidx,]
dataTesting <- tmpData[testidx,]

(fmla <- response ~ pred1 + pred2 +pred3)
model <- glm(formula = fmla, family=binomial(), na.action=na.exclude, data=dataTraining )
summary(model)

yTest = dataTesting[,'response']  #actual values on testing data
yhatTest = predict(model, newdata=dataTesting, type = "response")    #values predicted by the model on testing data
plot.roc(yTest, yhatTest,  col = "red", print.auc = TRUE, print.auc.y = 0.05)

One such ROC curve is given below:
Answer: The ROC curve looks pretty similar to random guessing to me.  Occasionally the model get lucky, but it doesn't stay lucky.
/edit to expand a little bit.  Lets say we have 3 completely random classifiers, that are unrelated to our outcome of interest.  In R, we can do this as follows:
library(caTools)
set.seed(42)
rows <- 100
cols <- 3

predictors <- matrix(runif(rows*cols), ncol=cols)
obs <- sample(0:1, rows, replace=TRUE)

colAUC(predictors, obs, plotROC=TRUE)
lines(0:1, 0:1)

You can see, with a small number of examples, these random classifiers occasionally get lucky (or unlucky), and fall above or below the .50 line.  These fluctuations are random and as we increase the number of observations we see that the 3 ROC curves get closer to the line:
library(caTools)
    set.seed(42)
    rows <- 10000
    cols <- 3

    predictors <- matrix(runif(rows*cols), ncol=cols)
    obs <- sample(0:1, rows, replace=TRUE)

    colAUC(predictors, obs, plotROC=TRUE)
    lines(0:1, 0:1)

Comment: thanks. That true for the data. But why the curve is alternating going sometimes above and below. I am expecting a little above or little below the nondiscriminating line.
Comment: @user1140126 It looks like you have ~ 16 instances of the positive class.  At the points where these instances fall on your predicted probabilities, you will get a vertical jump in the ROC curve.  If you have several instances with similar probabilities, you will get a bunch of vertical jumps, which may cross the line.
Answer: I had the same problem. As pointed above by Zach, this means your classifier has similar performances to a random classifier.
However, there can also be another bottleneck: your ROC algorithm. In my case, my computation of the ROC curve was wrong, and thus it was not my classifier which was random, but the way the ROC curve was computed, leading to this kind of result.
To avoid this problem, you should use a few sanity checks. A good one I have used was to compute:

the total number of real Negative examples (eg: Y==0)
the total number of real Positive examples (eg: Y==1)
at (1.0, 1.0) of the ROC curve, the total number of Negative examples predicted by your classifier (eg: False Positive + True Negative)
at (1.0, 1.0) of the ROC curve, the total number of Positive examples predicted by your classifier (eg: True Positive + False Negative)

You should then compare 1 with 3, and 2 with 4. If they are not equal (1 == 3 and 2 == 4), then your ROC curve is wrong.


DatabaseMetada.getImportedKey is empty even if foreign are existing

Question: For my project i try to get all foreign key in my table using DatabaseMetada Object form jdbc but when i execute the following code the result set is always empty even if my table contain foreign key ?
ResultSet v_resultPrimaryKey = p_metadata.getImportedKeys(null, Tools.getDBName(), "bookmarks_tags");

        if (v_resultPrimaryKey.next()) {
            System.out.println("test");
            v_resultPrimaryKey.beforeFirst();
            v_resultPrimaryKey.beforeFirst();

            while (v_resultPrimaryKey.next()) {
                if (p_att.equals(v_resultPrimaryKey.getString("FKCOLUMN_NAME"))) {
                    v_fk = v_resultPrimaryKey.getString("PKTABLE_NAME") + "."
                           + v_resultPrimaryKey.getString("PKCOLUMN_NAME");
                    v_fkName = v_resultPrimaryKey.getString("FK_NAME");
                }
            }

            if(!v_fk.equals("")){
                v_foreignKey = new ForeignKey(v_fkName, v_fk);
            }

        }

Comment: _"sgbd : mysql driver : the lat jdbc driver for mysql"_ <- What does that mean?
Comment: Which database and which driver + version? Also, try using `"BOOKMARKS_TAGS"` as the table name  if your database stores unquoted table names in upper case, and `null` for the schema name
Comment: sgbd : mysql driver : the lat jdbc driver for mysql and i have already try with null for the schema name i will try the uppercase tommorow when i will get back at work
Answer: stumbled upon this and this may help in case anyone may see it
DatabaseMetaData p_metadata= connection.getMetaData();
ResultSet v_resultPrimaryKey = p_metadata.getImportedKeys(null, Tools.getDBName(), "bookmarks_tags");

   if (v_resultPrimaryKey.next()) {
       System.out.println("test");
       v_resultPrimaryKey.beforeFirst();
       v_resultPrimaryKey.beforeFirst();

            while (v_resultPrimaryKey.next()) {
                if (p_att.equals(v_resultPrimaryKey.getString("FKCOLUMN_NAME"))) {
                    String v_fk = v_resultPrimaryKey.getString("PKTABLE_NAME") + "."
                           + v_resultPrimaryKey.getString("PKCOLUMN_NAME");
                    String v_fkName = v_resultPrimaryKey.getString("FK_NAME");
                }
            }

            if(!v_fk.equals("")){
                v_foreignKey = new ForeignKey(v_fkName, v_fk);
            }

        }


How to pass correctly a UITextlabel's value to another view

Question: I have a UITableView that using a custom table view cell, the cell contains a UITextLabel and a UIButton. I would like to pass the the textlabel's value to the another view when the UIButton tapped. I'm using a segue called goToNewView to pass the data. In the destination view controller i have a button that should display an NSLog with the passed value when the dataTestButton tapped. Actually i'm getting no errors, when i tap the dataTestButton, but the desired data (from the mySimpleContactUsernameLabel) does not appears. Only the The string = shown up.
I'm sure that something in my prepareForSegue method is incorrect, but i couldn't figure out how to tell to the method, that i wanna pass the data from the same label where the button was pressed. So i would really appreciate if somebody could show me the right way.
Here's the code of the view controller from where i want to pass the data:
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.simpleContact = [[PFUser currentUser] objectForKey:@"simpleContact"];

}

-(void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    PFQuery *queryMyContacts = [self.simpleContact query];
    [queryMyContacts orderByAscending:@"username"];
    [queryMyContacts findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
        else {
            self.allMyContact = objects;

              [tableViewTwo reloadData];
    }
}];
}
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

   return [self.allMyContact count];

}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CustomTableCell *cell = [tableViewTwo dequeueReusableCellWithIdentifier:@"devCell"];
    PFUser *user = [self.allMyContact objectAtIndex:indexPath.row];
    cell.mySimpleContactUsernameLabel.text = user.username;

    return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"goToNewView"]) {
        if ([[segue destinationViewController] isKindOfClass:[NewDestinationViewController class]]) {
            NSIndexPath *indexPath = [tableViewTwo indexPathForSelectedRow];
            NewDestinationViewController *dataToNew = [segue destinationViewController];
            dataToNew.messageRecipient = [self.allMyContact objectAtIndex:indexPath.row];
        }
    }
}

Here's the destination view controller's .m file:
- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (IBAction)dataTestButton:(id)sender {

    NSString *messageRecipient = [[NSString alloc] init];
    NSLog (@"The string = %@", messageRecipient);
}

.h file:
@property (nonatomic, strong) NSString *messageRecipient;
- (IBAction)dataTestButton:(id)sender;

Comment: yes, that's the point.
Comment: Check `[self.allMyContact objectAtIndex:indexPath.row]` before passing it to your next ViewController.
Comment: @Larme what do you mean exactly on "check `[self.allMyContact objectAtIndex:indexPath.row]` before passing.." Is the problem with this part?
Comment: Log it before you set this value to `dataToNew.messageRecipient`.
Comment: No matter what you do NSLog will always print only "The string =". Because you just created a new instance of NSString called "messageRecipient", which is empty when you print it.
Comment: @Larme  The log displays always the first row's content. No matter which button i tap.
Comment: @x4h1d i should use `self.messageRecipient` and delete the `NSString *messageRecipient = [[NSString alloc] init];`?
Comment: @x4h1d now i can pass the data, but it passes always the first label from the table view. Do you have any idea how can i send the label from the same cell where the button was tapped?
Comment: @sabin, prepareForSegue is not a good idea in this case. I suggest you to implement tableView: didSelectRowAtIndexPath method and inside that method implement feature pass the object rather than using prepareForSegue. By the way, don't alloc destination view controller every time. I believe it's a single view controller you call every time. So alloc it in more secure place.
Comment: Thanks, actually I'm using the didSelectRowAtIndexPath: with a segue, i've removed the button, but I will try it without segue too. What do you mean on secure place? Is there any security issues with segues that cause problems?
Answer: I suggest you to use this small library to help you out with table segues (I hope you're familiar with CocoaPods)
https://github.com/stefanomondino/SMQuickSegue
If you you have a cell segue to the next view controller, it should be possible achieve your goal with this
#import <UIViewController+SMQuickSegue.h>

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

CustomTableCell *cell = [tableViewTwo dequeueReusableCellWithIdentifier:@"devCell"];
PFUser *user = [self.allMyContact objectAtIndex:indexPath.row];
cell.mySimpleContactUsernameLabel.text = user.username;
[cell setSegueParameters:@{@"messageRecipient":user.name}]; //will set messageRecipient on the destination view controller with the name property of user.
return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    [super prepareForSegue:segue sender:sender];
    [self quickPrepareForSegue:segue sender:sender];
}

Comment: Thanks for the suggestion, but unfortunately i can not use CocoaPods actually because Parse is a bit buggy with it.
Comment: You can also use it without cocoapods, just clone the library and drag the two files (UIViewController+SMQuickSegue.h and UIViewController+SMQuickSegue.m) in your project.
Answer: Your problem is when you alloc the local variable messageRecipient you are then not reading the property you declared in your header file.
Change your destination method to this, and it should work (Assuming you are using auto synthesis for your properties)...
- (IBAction)dataTestButton:(id)sender {

    NSLog (@"The string = %@", _messageRecipient);

}


How to disable SELINUX for systemd script?

Question: I have a systemd script in CentOS 7 which doesn't run properly unless I disable SELINUX. Is it possible to somehow have SELINUX enabled on the system but disable it only for this systemd script?
The systemd script:
[Unit]
Description=Tractor Blade Service
Wants=network.target network-online.target autofs.service
After=network.target network-online.target autofs.service
RequiresMountsFor=/22.214.171.124/pipeline/

[Service]
Type=simple
User=IRUser
ExecStart=/opt/pixar/Tractor-2.1/bin/tractor-blade --debug --log /220.127.116.11/pipeline/logs/tractor/tractor-blade-%H.log --engine=111.222.333.444 --supersede --pidfile=/var/run/tractor-blade.pid

[Install]
WantedBy=multi-user.target

Comment: To clarify your question, do you want SELINUX to work but not with this specific script or you want to disable SELINUX when you launch this script ?
Comment: I'd like to have SELINUX work, but not with this specific script.
Comment: This looks like a third party program. Did it come with an SELinux policy?
Answer: You could run that process as unconfined so it would have the same rights as if SELinux was disabled.
# This will setup the executable to be unconfined. Temporarily
chcon -t unconfined_exec_t /opt/pixar/Tractor-2.1/bin/tractor-blade
# This command will make that permanent
semanage fcontext -a -t unconfined_exec_t /opt/pixar/Tractor-2.1/bin/tractor-blade

You can read more about unconfined processes in Red Hat documentation: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security-Enhanced_Linux/sect-Security-Enhanced_Linux-Targeted_Policy-Unconfined_Processes.html
Answer: Try to use the semanage fcontext command
semanage fcontext -a -t <YourLabel> -f f <YourPath> should work.
-a = add a record for the fcontext object type
-t = SELinux type for the object
-f = file type
Comment: This doesn't really explain how to accomplish anything.
Answer: Security-Enhanced_Linux-Systemd_Access_Control (https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/SELinux_Users_and_Administrators_Guide/chap-Security-Enhanced_Linux-Systemd_Access_Control.html)
Also you can update selinux status as permissive
setenforce 0
getenforce or sestatus


BlogEngine Razor Pages Not Rendering

Question: I'm using Web site project version 2.7 with VS2010 with IIS7.5 on both my workstation and server, and neither the Dashboard or Appearance .cshtml razor pages were rendering - I was getting a 404.
I added .cshtml as an extension under Mime Types, with a MIME type of text/html, and installed MVC4. This fixed it on my local but I cannot get this to work on the server (Windows 2008 R2).
Can anyone help?
Answer: I finally found the answer here:
http://www.adamwlewis.com/articles/iis-7-not-serving-files-4047-error

BlogEngine Razor Pages Not Rendering


How to delete the message sent by Webhook in google hangout chat

Question: I followed incoming bot python to send a message with webhook and I want to delete it with Method:delete.
But I get error 401 and following is the error message.
{'error': {'code': 401,
           'message': 'Request is missing required authentication credential. '
                      'Expected OAuth 2 access token, login cookie or other '
                      'valid authentication credential. See '
                      'https://developers.google.com/identity/sign-in/web/devconsole-project.',
           'status': 'UNAUTHENTICATED'}}

Webhook seems to use key and token as authorization. But I cannot delete the message with the same url (with the target "data-id").
Comment: I have the same problem, but I want to "update" message that was sent through webhook! I get the same error: 401.
Answer: You must have downloaded the credential file. If not then follow this.
Once you download then follow the below steps to generate Auth token.

Set the environment variable
export GOOGLE_APPLICATION_CREDENTIALS=
Run this command on your terminal.
gcloud auth application-default print-access-token
Use generated token in your Postman -> Authorization -> Type OAuth 2.0 -> Access token
OR
In your headers as
headers = {
    'Content-Type': "application/json",
    'Authorization': "Bearer ",
    ...
}
Answer: It is not possible to delete the messages sent via Webhook in google hangouts chat, or at least that feature is not yet built.
If you are posting a message into a room or sending someone a DM via the project's Appscript code itself (without using Webhook, i.e via spaces.messages.create), then it is possible to delete it using this method spaces.messages.delete (check here)
If you try using the above method (spaces.messages.delete) to delete the message posted using Webhook, then you will get the following error
{
    "error": {
        "code": 403,
        "message": "Request had insufficient authentication scopes.",
        "status": "PERMISSION_DENIED"
    }
}

This is because the message you are trying to delete was posted by a webhook and not via the appscript code base or using spaces.messages.create and thus the access token generated does not have the privilege to delete the message since it belongs to the appscript project and not the webhook bot.
Hope that clears your question!
Comment: I don't think the OP mentions anything about Apps Script. I guess there's no way to delete webhook messages for now.
Comment: It mentions using the python code to achieve the same functionality which I did via AppScript. Makes no difference, really.


use of String.matches(regex) in smartgwt

Question: I am working with smartgwt since three months.
I have encountered a problem with the specific method of the string class.
It seems that the matches never works, even with the simplest one:
String regex = "CEDD";
String input = "CEDD";
input.matches(regex);

this will always returns false. Such piece of code is within a class extending the smartgwt Layout class, and therefore got converted to js and used in front end.
The same fragment obviously works when used in a simple java main standalone class.
Could you point out what to investigate to solve this problem ?
thanks
Answer: It looks like GWT doesn't support normal Java regular expressions (i.e. doesn't support Pattern, Matcher and classes/methods that use it.
There is a RegExp class that provides those features, however.
Comment: One other possible solution, altough a bit weird, is to write a native method and write javascript regex in it.


Is $f:\mathbb{R}\to\mathbb{R}_+: x\mapsto\sqrt{x}$ bijective?

Question: I'm pretty sure it's not bijective since the negative values of the domain have no corresponding value in the codomain but it does qualify as surjective since every value in the codomain has values in the domain but also as injective since values in the domain correspond to no more than one value in the codomain unless I'm misunderstanding something. If not, then what would this function be classified as?
Comment: $\sqrt x$ is not defined on $\mathbb R.$
Answer: The function (and question) is ill-defined: $f(x)$ is not defined for negative values.
Discussing whether it is injective, surjective, or bijective is entirely irrelevant -- it's not even a function.
Comment: This should be a comment.
Comment: @UnripeMango A function must have exactly one value at each point in the domain. $f: \mathbb{R}^+ \to \mathbb{R}, x \mapsto x^2$ is a function since every positive number has a square. The "unused" values in the codomain just mean it's not surjective.
Comment: The definition of function means there are no domain values with less than (or more than) one point in the codomain.
Comment: I disagree, @ThomasAndrews. It's exactly what's needed to answer the question.
Comment: I've seen the example f:R+ -> R: -> x^2 been used as an example of a function that is injective but not surjective. Isn't this not a function either since all negative values of the codomain aren't defined?   Edit: my bad a function is define by its domain so it can have parts of the codomain be undefined and still be a function but not the other way around? @PrincessEev
Comment: @aschepler so the definition of injectivity being that "values in the domain must have no more than one value in the codomain" means that a function cannot have domain values with less than one point in the codomain?


Parsing JSON Data in Swift 3

Question: I am trying to parse JSON data from the Google Civic Information API with Swift 3 and keep running into trouble.
This is the code that I have now for my parseData function:
private func parseData(JSONData: Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String : Any]

        if let officials: Any = readableJSON["officials"] as? [String : Any] {

            for official in officials as? [String : Any] {

                let name = official["name"] as? String

                if let address = official["address"] as? [String : String] {

                    let line1 = address["line1"] as? String
                    let line2 = address["line2"] as? String
                    let line3 = address["line3"] as? String
                    let city = address["city"] as? String
                    let state = address["state"] as? String
                    let zipCode = address["zip"] as? String

                }

                let party = official["party"] as? String
                let phones = official["phones"] as? [String: Any]
                let urls = official["urls"] as? [String : Any]
                let photoURL = official["photoUrl"] as? String
                let emails = official["emails"] as? [String : Any]

                if let channels = official["channels"] as? [String : Any] {

                    for j in 0..<channels.count {

                        let channel = channels[j] as? [String : Any]
                        let type = channel["type"] as String
                        let id = channel["type"] as String

                    }
                }
            }
        }
    }
    catch { print(error) }
}

Thanks in advance for any help
Comment: Keep running into what trouble exactly? Compiler error? Runtime error? Unexpected results? Computer on fire?
Answer: if let officials: Any = readableJSON["officials"] as? [String : Any] {

    for official in officials as? [String : Any] {

You are trying to loop over an object of type Any. Your casting properly on the right side, but then overriding it with a less specific type on the left. You can omit the left side type altogether, as Swift's type inference will know you are getting [String : Any]? from the right side.

Parsing JSON Data in Swift 3


Exibir o ID de outra tabela no DataGridView

Question: Como faço para exibir o ID de outra tabela no meu DataGridView?
Consigo exibir todos os campos menos o ID.
 var novaListaPessoaFisica = listaPessoaFisica.Select(
           pessoaFisica => new
       {

           PessoaFisicaID = pessoaFisica.Pessoa.IDPessoa,
           Nome = pessoaFisica.Nome,
           CPF = pessoaFisica.CPF,
           RG = pessoaFisica.RG,
           DataNascimento = pessoaFisica.DataNascimento
       }).ToList();

    dgvPessoaFisica.DataSource = null;
    dgvPessoaFisica.DataSource = pessoaFisicaCollection;

    dgvPessoaFisica.Update();
    dgvPessoaFisica.Refresh();
    }

    public class PessoaFisica
    {
        public Pessoa Pessoa { get; set; }
        public String Nome { get; set; }
        public String CPF { get; set; }
        public String  RG { get; set; }
        public DateTime DataNascimento { get; set; }
    }

Comment: amigo nos explique mais sobre oque quer fazer, de exemplos, nos mostre como popula o `datagrid`
Comment: você esta criando como o grid na tela?
Comment: Eu quero carregar o meu dataGrid porém quero exibir os dados de outra tabela concretamente o ID por ex: pessoaFisica.Pessoa.ID.
Comment: Não seria só criar mais um campo no seu select?
Answer: Sua pergunta não esta clara, mas se sua intenção é ter mais um campo no seu grid e esse campo já esta na sua listaPessoaFisica, então basta que você altere sua consulta com esse campo, IDPessoa = pessoaFisica.Pessoa.ID, .
var novaListaPessoaFisica = listaPessoaFisica.Select(
           pessoaFisica => new
       {
           IDPessoa = pessoaFisica.Pessoa.ID,
           PessoaFisicaID = pessoaFisica.Pessoa.IDPessoa,
           Nome = pessoaFisica.Nome,
           CPF = pessoaFisica.CPF,
           RG = pessoaFisica.RG,
           DataNascimento = pessoaFisica.DataNascimento
       }).ToList();

Agora se o campo não existe na sua lista e esta em outra tabela você pode usar o fazer um select dentro do seu select para buscar na tabela Pessoa os dados correspondentes, Pessoa = ctx.Pessoa.FirstOrDefault(P => P.IDPessoa == pessoaFisica.Pessoa.IDPessoa)
var novaListaPessoaFisica = listaPessoaFisica.Select(
                   pessoaFisica => new
                   {
                       Pessoa = ctx.Pessoa.FirstOrDefault(P => P.IDPessoa == pessoaFisica.Pessoa.IDPessoa)
                       PessoaFisicaID = pessoaFisica.Pessoa.IDPessoa,
                       Nome = pessoaFisica.Nome,
                       CPF = pessoaFisica.CPF,
                       RG = pessoaFisica.RG,
                       DataNascimento = pessoaFisica.DataNascimento
                   })
                   .Select(Pf => new
                   {
                       IDPessoa = Pf.Pessoa.IDPessoa
                       PessoaFisicaID = Pf.PessoaFisicaID,
                       Nome = Pf.Nome,
                       CPF = Pf.CPF,
                       RG = Pf.RG,
                       DataNascimento = Pf.DataNascimento
                   }).ToList();


regexp parsing in matlab

Question: I have a cell array 3x1 like this:
name1 = text1
name2 = text2
name3 = text3

and I want to parse it into separate cells 1x2, for example name1 , text1. In future I want to treat text1 as a string to compare with other strings. How can I do it? I am trying with regexp and tokens, but I cannot write a proper formula for that, if someone can help me with it please, I will be grateful!
Comment: (\w+)\s=\s(\w+) <- Do you need something like this?
Answer: This code
input = {'name1 = text1';
         'name2 = text2';
         'name3 = text3'};

result = cell(size(input, 1), 2);
for row = 1 : size(input, 1)
    tokens = regexp(input{row}, '(.*)=(.*)', 'tokens');
    if ~isempty(tokens)
        result(row, :) = tokens{1};
    end
end

produces the outcome
result =
    'name1 '    ' text1'
    'name2 '    ' text2'
    'name3 '    ' text3'

Note that the whitespace around the equal sign is preserved. You can modify this behaviour by adjusting the regular expression, e.g. also try '([^\s]+) *= *([^\s]+)' giving
result =
    'name1'    'text1'
    'name2'    'text2'
    'name3'    'text3'

Edit: Based on the comments by user1578163.
Matlab also supports less-greedy quantifiers. For example, the regexp '(.*?) *= *(.*)' (note the question mark after the asterisk) works, if the text contains spaces. It will transform
input = {'my name1 = any text1';
         'your name2 = more text2';
         'her name3 = another text3'};

into
result =
    'my name1'      'any text1'
    'your name2'    'more text2'
    'her name3'     'another text3'

Comment: is there any way to remove white spaces in your first approach? second one is not working taht good, but first is ideal just need to remove the spaces to be able to compare strings.
and by the way, how do you recommend me to learn regular expressions and tokens? help on matlab is not enough in this manner, too few examples to understand how to combine all the 'signs'.
Comment: I know this expression will do the trick for a single string, but how to get into the cell of these strings? *regexprep(s,'[^\w'']','')*
Comment: @user1578163: Try `tokens{1}{1}` and `tokens{1}{2}` to get directly to the tokens. Why isn't the second regexp working as expected? What strings are failing?
Comment: @user1578163: I learned regular expressions from the SED tutorial http://www.grymoire.com/Unix/Sed.html.

regexp parsing in matlab


Content profile fields on registration form by role

Question: I have two main users roles: authenticated and agent.
First role uses content profile fields on registration form.
Users with the agent role can only be created by admins but agent doesn't need content profile fields.
In user_add_form the content profile fields is added as well so the admin must fill them.
How can I disable those fields when an admin want to create user with the agent role?
Answer: You can use hook_form_alter to alter the form. This is how the profile fields is added and they can be removed this way as well.
You have to make sure that your custom module is run after the profile module, which may require that you alter the weight of the module. This is merely a value in the system table.
Answer: There is two checkboxes on 'Content profile' tab of the node type:
"Use on Registration" and "Use on administrative user creation form"
See image the below image


preencher arraylist com informações de atributo arraylist de outra classe

Question: tenho uma classe Evento com um atributo  arraylist<string>lista. Preencho esse atributo em um JFrame TelaLista e utilizo um  evento.setLista("informaçao"); para adicionar a string do textField no arraylist da classe Evento. Gostaria de saber como posso utilizar essa informação que foi salva pelo set em outra classe. Tentei fazer o seguinte:
        ArrayList<String> st = new ArrayList();
        Evento ev = new Evento();
        st = ev.getLista();

      for (int i = 0; i < st.size(); i++) {
            System.out.println("teste" + st.get(i) );
        }
    }

mas esse array st fica vazio...
O JFrame que comentei que está preenchendo com o setLista está dessa forma:
Evento listaTipo = new Evento();
listaTipo.setLista(textFieldNome.getText());

alguem tem alguma ideia de como posso fazer com que o arraylist st da outra classe em questao fique preenchido com as informações que salvei no jframe?
Answer: João, claramente a lista virá vazia pois você tem duas instâncias diferentes. Suas instâncias são:
Evento ev = new Evento();

E a outra instância que você preencheu a lista é essa:
Evento listaTipo = new Evento();

Para recuperar os valores da listaTipo, você deve utilizar a mesma instância. Ou seja, você deveria fazer algo do tipo:
Evento listaTipo = new Evento();
listaTipo.setLista(textFieldNome.getText());

ArrayList<String> st = new ArrayList();
st = listaTipo.getLista();


How can you return the last value in common lisp

Question: I know that the values function helps control REPL but I want to explicitly return the last value from a function.
The only thing close to that I managed to find to what i was looking for is
 (setf (values q a) (floor 1.5))

But I am only interested in the 0.5 that it returns.
My question is how can I only return the last value of a function not just the second but the n-th value(whichever it maybe)?
Answer: There is no standard operator to get the last value, or even to iterate over values, but you can work around that using MULTIPLE-VALUE-CALL:
(defun last-arg (&rest args)
  (first (last args)))

(defmacro multiple-value-last (form)
  `(multiple-value-call #'last-arg ,form))

(multiple-value-last (floor 1.5))
; => 0.5
(multiple-value-last (values 10 20 30))
; => 30

At least on SBCL the standard MULTIPLE-VALUE-LIST seems to be implemented this way.
Comment: Curiously I couldn't immediately see that this is safe according to the spec (obviously it *is* safe): it looks as if there is nothing constraining `multiple-values-limit` to be smaller or equal to `call-arguments-limit`.  I have probably misread the spec, but if that's right then I'm sure that's a deficiency in the spec because there really ought to be such a constraint.
Comment: I think my comment above is wrong: if `values` is a function (it's an accessor, but what it isn't is a special operator) then there *is* a constraint on the number of multiple values which is that it can be no more than the number of arguments you can pass to it, which is `call-arguments-limit`.
Comment: well now I know one thing for sure it's time to move on to macros thanks!
Comment: When I see `(last-value expr)` it doesn't hint at all that it is a macro nor that it has something to do with multiple values and not eg. lists or arrays. Perhaps naming it `multiple-value-last` will make it less surprising and simpler to assimilate. (Yes, I know of `nth-value` and I think Pierson/Pitman choose a bad name)
Comment: @Sylwester Yeah, I see your point. I chose the name originally to be consistent with `NTH-VALUE` (and same with `VALUES` vs `MULTIPLE-VALUES`), but `MULTIPLE-VALUE-LAST` is probably a better choice.


Perl regex to tag the entire term

Question: I am trying to tag terms using a list of annotations. What I am specifically trying to achieve is that if the Perl regex identifies a term from a sentence it should tag the term with tags.
For example:
This drug has adverse effect on Lymphocytes, Lymphnodes, Lymph and pre-lymphocytes.

My list has the word Lymph, I am trying the following script.
open IN, "clean_cells.txt" or die "import file absent";
@array=<IN>;
foreach $words(@array)
{
    @cells=split/\t/,$words;
    $value=$cells[0];
    $replace=$cells[1];
    foreach my $fp (glob("$Directory/*.txt"))
    {
        @id=split('/',$fp);
        $id[1]=~s/.txt//ig;
        $Pub=$id[1];
        open FILE, "<",$fp or die "Can't open $fp: $!";
        open OUT, ">C:\\Users\\Desktop\\TM\\Files\\$Pub" or die "Check      output status";
        while(<FILE>)
        {
            chomp $_;
            $line=$_;
            s/\b[\w\-]*$value[\w\-]*\b/<$replace>$&<\\$replace>/gi;
            # $string[$i]=$line;
            # while(($string[$i]=~m/\Q$value\E/i)||                                      ($string[$i]=~m/\Q$value(\w+)\E/i)||($string[$i]=~m/\Q(\w+)$value\E/i))
            # # if ($string[$i] =~ m/\b\w*$value\w*\b/i)
            # {
                # $value=~s/$value/<$replace>$value<\$replace>/i;
            # }
            print OUT "$line\n";
        }
        last;

    }
    last;
}

I am hoping the final sentence should look like this:
This drug has adverse effect on tag Lymphocytes tag, tag Lymphnodes tag, tag Lymph tag and tag pre-lymphocytes tag.

tag: represents $replace in the above script.
The program tags the base word lymoh and not the entire term Lymphocytes, pre-lymphocytes.
Comment: And so ? Is it working ? What do you expect ? Do you have errors ? Finally, what is the question ?
Comment: Edit it more, because I still can't see a question.
Comment: @sputnick : Sorry it is not doing anything
Comment: Have you checked the file paths ?
Comment: YES. It's working. I am sorry I checked the path correctly. I had a small error in the path. Thanks man. I really appreciate your help
Comment: Just a small thing, I would like to clear. The code print next line before '>' .
Comment: Got, it I chomped $replace. Thanks for all your help. :)
Answer: Try doing this (script rewriten):
use strict; use warnings;

my $pub = "SOMETHING I HAVE TO CHANGE FOR THE SCRIPT TO WORK";
my $fp = "clean_cells.txt";
my $of = "C:\\Users\\Hrishi\\Desktop\\TM\\Files\\$Pub";
my $replace = 'tag';
my $word = "Lymph";

open FILE, "<", $fp or die "Can't open $fp: $!";
open OUT,  ">", $of or die "Check output status";

while (<FILE>) {
    chomp;
    s/\b[\w\-]*$word[\w\-]*\b/<$replace>$&<\\$replace>/gi;
    print OUT $_; print;
}

EDIT:

\b means word boundary
$& means the full matched part of the last regex
[\w\-] is a class of characters : alphanumerics + _, and -
the final gi modifiers means respectively all occurences and case insensitive
Comment: Nope doesn't help. I am using while loop to make sure that entire line gets scanned, I think using an 'if' would exit the loop just after one match in the sentence. I want the program to tag all possible matches in the sentence
Comment: See my edited post with the full script
Comment: I have provided my full code.
Comment: Sorry, the code didn't anything.
Comment: Have you modified the filepath and see the output of the script ? The script display the result on STDOUT (terminal)
Comment: It prints OUT but without changing $value.
Comment: I have modified the script to create your wanted file.
Comment: I know what's wrong, you lack the declaration of `$Pub` used in your path. `my $pub = "something"`
Answer: You need to keep your words together. The tricky part with that is determining what characters can make up words. A simpler approach (but perhaps not as exact) is to determine what makes up the delimiters. For example, you can use \S+ to match consecutive non-whitespace characters:
use strict;
use warnings;

while (<DATA>) {
    for (split /(\S+)/) {    # The parens make the split keep the delimiters
        $_ = "<tag>$_</tag>" if /lymph/i;
        print;
    }
}

__DATA__
Lymphocytes, Lymphnodes, Lymph and pre-lymphocytes.

Output:
<tag>Lymphocytes,</tag> <tag>Lymphnodes,</tag> <tag>Lymph</tag> and <tag>pre-lymphocytes.</tag>

Note that this is a non-destructive method, because the parens in the split regex will capture and return all the parts of the string.
This simplistic code will preserve your whitespace, though as you can see, it will put commas and other such separator characters inside your tags. This can be fixed by using another character class, such as [^\s,.!?]+ (not whitespace, comma, period, exclamation point or question mark).
If you replace <DATA> with <>, you can use this script with redirection and skip the code about opening input and output files.
perl script.pl input > output

I would personally prefer such functionality, rather than hard-coded file paths, and it is often the way *nix programs work.
Comment: I think you misunderstood OP needs, you keep only tagged strings, but OP still need non matched ones. re-read his expected output.
Comment: That is an incorrect statement. I keep everything.
Comment: OK, sorry, you have changed sample INPUT
Comment: Thanks for the reply. I cannot split at a delimiter as there are words like Vitamin E which are both part of the list and the sentence, splitting would miss words like Vitamin E as Vitamin would be considered as one unit and E as the other unit.
Comment: @Pink Ok.. is your list of words very long?
Comment: @TLP : Yes its almost 35,000 words
Comment: Do you have overlapping words, which have different values? Such as: `car => Ford, carpet => Rug`. This will create some confusion, as carpet would become: `carpet`. And worse, if you have tags which coincide with words, e.g. `xxx => foobar, foo => yyy`. In the latter case, you'd need to exclude the tags when doing your search and replace.
Comment: I have taken care of above cases. Each term is unique

Perl regex to tag the entire term


Keeping old site live while testing new site?

Question: I'm helping my university department upgrade their Drupal website from 6 to 7 (university doesn't support 8 yet).  So far, I've been able to install and test the new site on my local machine, but now it's time to move it to the actual host so that users (mainly the department secretary) can start to populate pages with content.  Can I have a Drupal 7 site live on a subdomain like test.dept.university.edu while keeping the Drupal 6 version live on dept.university.edu?
Apologies if this is a naive question - I've read a lot about creating clones of existing sites for testing, but this assumes that one is upgrading within the same Drupal version.
Answer: In short, yes you can, but this has a little to do with drupal.
To clone a Drupal site is nothing more than copying the drupal root directory (codebase) and taking a database dump. It's quite easy then to transfer all these on any other environment and with some settings.php tweaking you will have an up and running clone of your site. The hard part is to migrate from D6 to D7, but you have already done this as far as I understand.
I would propose you to setup a staging environment by using a clone created from your local environment and start adding/editing content there. It dosen't really matter under which hostname that (staging) will be, it may well be the test.dept.university.edu  because you can make the production's url (dept.university.edu) to point back (redirect) to the staging machine when all things are in place and the time to go live has come.
This is a rough idea but I guess that the meaning is quite clear.
Let the mods to decide if this question should be close or not.


elegant way to extract a certain member from a list of objects

Question: I have a container with elements of some arbitrary structure/class.
Is there an elegant way to extract a single property (e.g. member or member.function) into a own array (e.g. to pass it to another algorithm)?
#include <iostream>
#include <string>
#include <vector>

int main()
{
    struct A{
      int n;
      int B()const;
    };

    std::vector   manyOfA{{1,1}, {2,2},{3,3}};
    std::vector<int> allNs;

    // is there a standard way to do this kind of extraction?
    for(const auto& a:manyOfA){
      allNs.push_back(a.n);
      // or maybe allNs.push_back(a.B());
    };

    std::cout << "result Vector: ";
    for(const auto& n: allNs){
      std::cout << n << ", ";
    };
    std::cout << std::endl;
}

Comment: [std::transform](http://en.cppreference.com/w/cpp/algorithm/transform) ?
Answer: You can use std::transform to apply a function to every element of an iterator range and collect the results in a result range. In you case, the result range can be a std::back_inserter iterator. This is a special iterator that calls v.push_back on a given container when assigning a value.
std::vector   manyOfA{{1,1}, {2,2},{3,3}};
std::vector<int> allNs;

std::transform(manyOfA.begin(), manyOfA.end(), std::back_inserter(allNs), [](A const& a) {return a.n;});

Or even better with a range library, e.g. boost.range. This will save the creation of a new vector just to hold the values of a.n:
template <typename RangeType>
void doSomethingWithRange(const RangeType &range) { ... }

auto allNs = manyOfA | transformed([](A const& a) {return a.n;});
doSomethingWithRange(allNs);

Answer: Additionally you could also use for_each
for_each(begin(manyOfA), end(manyOfA), [&](auto a) { allNs.emplace_back(a.n); });


readObject method only reads the first object it receives

Question: Just to give the background of my issue; I'm writing a client server application. when a client initially connects, the server accesses the database and send all the items in a particular table to the client by writing the objects to an objectoutputstream in a for loop.
The issue is, the client reads only the first object that is being sent. Even if I create a new object and send it to the server for verification the client does not pick it up when the server sends it back after inserting to database... extract from code:
Server:
public void loadClients()
{

    ArrayList <Client> list = dBCon.loadClients();
    try
    {
        for (int i =0; i<list.size();i++)
        {
            sendtoClient(list.get(i));
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

}

public void sendtoClient(Object obj) throws IOException
{
    _out.writeObject(obj);
    _out.flush();
    _out.reset();

}

Client:
socket = new Socket("localhost", 4447);
ois = new ObjectInputStream(socket.getInputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
Object objIn = ois.readObject();
Client client = (Client)objIn;
switch(client.actionType)
{
    case ("ADD"):
    {
        if(clientFrame.tbDeals!=null)
        {
            clientFrame.tbDeals.addClient(client);
            break;
        }
        else
        {
            clientList.add(client);
            System.out.println(client.clientName);
            break;

        }
    }

}

Edit: I have tried with a while loop on the client side as well. then ois.available() returns 0....
Comment: You're reading *exactly* one object on the client side. Why would you expect there to be more than one? Yes, you use a `while` loop, and no, you rarely if ever use `available()` - it doesn't do what you think it does in most cases. Basic IO in Java is blocking.
Comment: available() isn't a valid test for end of stream. See the Javadoc. It isn't much use for anything else either.
Comment: Hi, Thanks for the answer, I actually worked it out myself after asking the question. I created another wrapper class with an object array in it and included all the objects that I originally needed to send in an instance of the wrapper class. now it is working fine!!! For anyone else who come across this issue: Don't try to send objects separately as I did, but put them in an array and serialize the array. TY very much for all the people who answered!
Comment: The real issue here is that your statement 'I create a new object' is false. You didn't create a new object, you re-populated an existing object, and then *re*-sent it, so the Serialization 'handle' mechanism kicked in. If you really had created a new object, it would have worked. Or if you had used writeUnshared(), or reset().
Answer: You're reading exactly one object on the client side. That's all you're going to get.
available() is rarely if ever used when doing basic IO in Java, and generally isn't going to do what you think it does.
Reading from a ObjectInputStream is blocking, just like all other basic Java IO. Using readObject() you will either get an object, or an exception as noted in the javadoc
The most simplistic example of how you would handle this in a loop if you have no idea the number of objects you'll be receiving and expect the connection to stay open is:
while (true) {

    try {
        Object o = ois.readObject();
        /// do something with the object you just read
    } catch ( EOFException e) {
        System.out.println("remote connection closed!");
        e.printStackTrace();
        break;
    }
}

Comment: +1, but there is no need for betting. You should catch EOFException separately, when it is a dead certainty.
Comment: @EJP - I was going off the Javadoc. Does it throw an `EOFException` explicitly or wrapped in an `IOEXception`?
Comment: I don't know what you mean by 'explicitly', but every readXXX() method except readLine() throws EOFException at end of stream. readLine() returns null. The plain read() methods return -1, for completeness.
Comment: (I did a quick edit to clarify "explicitly") Basically ... I never use Java's built in serialization, so I'm just reading Javadocs on this one and it doesn't list EOFException on readObject- I'm guessing because it's a subclass now that I think about it, duh.
Comment: @EJP Sorry - it's obviously past my bedtime. It was at least a good answer :-D


If $X_t = Y_t$ in distribution, for any $t \in T$ (compact), is it true that $\mathbb E \sup_{t \in T} X_t = \mathbb E\sup_{t \in T} Y_t$?

Question: Let $T$ be a compact topological space and for any $t \in T$, let $X_t$ and $Y_t$ be random variables which have the same distribution. We may assume that $X_t$ and $Y_t$ depend on $t$ in continuous / smooth manner.

Question. Under what minimal additional conditions can we conclude that $\mathbb E \sup_{t \in T} X_t = \mathbb E \sup_{t \in T} Y_t$ ?

My rough guess is that the answer to the above question is No!

The scenario I have in mind is the following. Let $T$ be the unit-ball in $\mathbb R^n$. Let $V$ be uniformly distributed over the grassmannian of $k$-dimensional subspaces of $\mathbb R^n$, and let $(v_j)$ be an orthonomal basis for $V$. Let $P_V:\mathbb R^n \to V$ be the orthogonal projector for $V$. Finally, let $P_k$ be the random $k \times n$ matrix by obtained selecting uniformly selecting (without replacement) $n-k+1$ rows of the $n \times n$ identity matrix and deleting them. Let $(u_j)_j$ be an orthonormal basis for the row space of $P_k$. Fix a nonnegative scalars $\lambda_1,\ldots,\lambda_k$.
Now, by rotation-invariance, it is clear that $X_t := \sum_{j=1}^k\lambda_j \langle v_j,P_Vt\rangle^2$ and $Y_t := \sum_{j=1}^k\lambda_j \langle u_j,P_k t\rangle^2$ are equal in distribution, for any $t \in T$. The question then is

Question 2. Is $\mathbb E \sup_{t \in T}X_t = \mathbb E\sup_{t \in T} Y_t$ ?
Answer: Let $T= {1, 2}$ and $X_t, Y_t$ be symmetric Bernoulli random variables. Let $X_1$ and $X_2$ satisfy $P(X_1X_2=1)=1$, and $Y_1$ and $Y_2$ satisfy $P(Y_1Y_2=-1)=1$.
Then we have $P(\max_{t \in T}X_t=1)=P(\max_{t \in T}X_t=-1)=1/2$ and $P(\max_{t\in T}Y_t = 1)=1$. So, this gives a counterexample to your question.
Comment: I think Slepian's inequality is somewhat relevant to your interest. You can find it in Roman Vershynin's HDP book (Theorem 7.2.1). https://www.math.uci.edu/~rvershyn/papers/HDP-book/HDP-book.pdf
Comment: Indeed. I suspect this would be the case, thanks for the example which proves this (upvoted). As, stated in my question, I was wondering if there can be equality under mild additional conditions.
Comment: Indeed, I'd forgotten about those funky comparison inequalities for gaussian processes. Definitely relevant here! Thanks for the reminder !