Is there a core method in views that can pad numbers with leading zeros?

Question: Setup
I am building a view that outputs human usable id's. These ids would look best if padded to 3 characters. For example the episodes of a tv show may look best if they are formated:
01-01 (Season 1 episode 1)
10-05 (Season 10 Episode 5)
03-22 (Season 03 Episode 22)
Problem
In my node I'm storing a simple integer (1-100). I want to keep the value as an integer not a string. How do I pad the integer with leading zeros on output.
Using simple php would be a simple str_pad($value, 2, '0', STR_PAD_LEFT); but I do not want to execute php in my views or even enable the php module/filter.
Solution
The solution must:

Use a core ability of views
Not require me to execute php stored in the database (not enable php module or anything equivalent)
Must allow me to maintain the field as an integer input/storage

I could probably write the views plugin for this but I was hoping their was already something available like a views integer format or something like that.
Comment: Welcome to Drupal Answers! Please add an explicit question in the question body, one that is not on the lines of "Any ideas?" The title doesn't count as explicit question.
Comment: Read the title of the post. It is a concise clean question.  Also i have 200 reputation on this site and more on other in network sites been using stack exchange for 5 years.
Comment: As I said,  the question title doesn't count as asking a question. You aren't asking a question, but making a request.
Comment: I don't think you know what a question is. A question is a request for information.  A good question has a correct or best answer. My title is actually a yes or no question which is about as easy as it gets. A best answer would provide an option closest to my requirements and Drupal coding standards. You are free to not like my question but it meets all the requirements of the stack exchange network. If you still don't like it flag it and go away.
Comment: Since there isn't any explicit question, the question says _I was hoping their was already something available like a views integer format or something like that._ and you aren't accepting an answer suggesting how to achieve it with code, I take the question is a request for suggesting/finding a module implementing a plugin, and I will flag it for closing it.
Answer: Go to /sites/all/themes/yourtheme/ folder and in template.php add the following code:
function yourtheme_preprocess_views_view_fields($vars){

  if ($vars['view']->name == "view_name" && $vars['view']->current_display == "block_1") {
    $vars['fields']['field_example']->content = str_pad($vars['fields']['field_example']->raw, 2, 0, STR_PAD_LEFT);
  }

}

replace yourtheme with the actual name of your theme
replace view_name with the name of your view.
replace block_1 with the machine name of your view.
replace example with the name of your field (2 replacements).

Lastly, in Drupal, clear/flush your cache to see the changes.
Comment: Requests for module recommendations is something we don't do here @danielson317, it doesn't fit the Q+A format well. That's why you'll often find that people offer practical solutions here rather than linking off to a possibility elsewhere. As an aside, I think your requirements are at odds with what's physically possible here, insofar as your solution must _Use a core ability of views_. Core views doesn't have this ability, so currently you're simply out of luck. Yes writing a custom field handler is an option, but you'll have to do that as a "php code implementation" in a custom module, so..
Comment: ..that's ruled out too by the current requirements. It's hard to see how you're going to solve this unless you relax the rules here. A views pre-render hook would also need to be a "php code implementation" in a custom module, for example
Comment: This does not use a module or views setting. And while this may be a reasonable use of theme preprocessor Im not a fan of overriding views in the theme layer. Better to do it in a view_post_query hook or something like that in a custom module. Still looking for a module.
Comment: @danielson317 you will end up with the same result...
Comment: [what is the hook to preprocess a view?](http://drupal.stackexchange.com/questions/60271/what-is-the-hook-to-preprocess-a-view)
Comment: Yes but with much cleaner code. Better to tie functionality to the code not the theme. For example if i have multiple user selectable themes this solution would require multiple implementations. In general php in the theme layer is a bad idea.
Comment: Hook views pre render is what i was trying to remember. Been a while since i used it. https://api.drupal.org/api/views/views.api.php/function/hook_views_pre_render/7.x-3.x still a abetter solution is to write a simple field formatter so its in the db instead of the code.
Comment: @danielson317 `abetter solution is to write a simple field formatter so its in the db instead of the code` might be worth trying **[Allow number to start with ZERO when stored in mysql integer field](http://stackoverflow.com/questions/2786193/allow-number-to-start-with-zero-when-stored-in-mysql-integer-field)**
Comment: I think my last statement wasn't clear. I meant it would be better to write a views field formatter. That way the views settings are in the database. Then when i update the view i don't have to remember the php code overrides i did to it. Thats why I'm looking for a module not a php code implementation.
Comment: "We dont do request for modules here" that's inconsistent with nearly half the questions i see here. How can i do x the drupal way is essentially a module request. My requirements are perfectly reasonable. No php filter is a common security requirement. Not screwing with the theme layer with custom php is drupal best practices. Id accept a good php implementation bit i havnt seen one yet. Thanks for trying but im not going to put php in mu theme layer.
Comment: @danielson317 We have a closing reason for questions asking to recommend or find a module; if you find any question about that, chances are the question will be closed, or it is closed already. Then, Drupal uses PHP in its theme layer, since preprocess functions are written in PHP.
Comment: @NoSssweat The same preprocess function could be implemented from a module, could not it be?
Comment: @kiamlaluno it can be. I still prefer not to use preprocess functions. Even if they are not in the theme they are using the theme layer. A view field formatter would be a better solution. A module or views extension snippet would be best.
Comment: `The same preprocess function could be implemented from a module` @kiamlaluno I believe that is true, if my memory is correct, I did that a long time ago and it worked. I just rather/like use template files instead of custom module.


android adding views dynamically

Question: I am adding views dynamically in a linear layout as follows:
xml:
<code><LinearLayout
            android:id="@+id/part1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="right"
            android:orientation="horizontal" >
 </LinearLayout>
</code>
java:
<code>    View linearLayout = findViewById(R.id.part1);

    ((LinearLayout) linearLayout).removeAllViews();

    for (int i = 0; i  < 15; i ++){

        TextView tv = new TextView(this);
        tv.setText(String.valueOf(i));
        LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        tv.setLayoutParams(lay);
        tv.setBackgroundResource(R.drawable.msg);
        tv.setId(i);
        ((LinearLayout) linearLayout).addView(tv);

    }
</code>
Now i have two questions:
1) the text views are added horizontally correctly but if not fitting screensize, some of them won't appear, how to force it to continue adding in a new line once the horizontal space is full ?
2) textviews are added from left to right, how to add them from right to left ?
thanks
Answer: You need to understand how ViewGroups work, in this case, LinearLayout will add items horizontally or vertically without making position calculations for you unless you explicitly specify (jumping to next line is not one of them...), think of it as an item holder that will show items only on the space you specify for it (thats why some of the elements disappear...),  by default the way LinearLayout arrange items is from left to right or up to down, if this do not fit your needs, you could go for any of the ViewGroup options android has, the most important might be:
RelativeLayout
FrameLayout
TableLayout
AbsoluteLayout(not recommended)
If you need some sort of Free Draw on the screen, you can always go for a View object, override onDraw, and play with the canvas of that object...
Regards!
Comment: Thanks Martin for passing by the question, so appreciated .. Actually i know the other types of layouts but they won't be suitable for my design .. i have just found sth similar to what i am thinking in but i will still try it, then check right to left issue, it is called flow layout ..
https://github.com/ApmeM/android-flowlayout

android adding views dynamically


"Pick 3 out of 12 statements" - is linear regression possible in this case?

Question: Suppose I'm trying to figure out what makes people shop at a particular store. I conduct a survey where they are asked:

how often they shop there
to pick 3 out of a list of 12 characteristics that they most associate with the store, e.g. "well-known brand", "good quality", "good loyalty programs", etc.

I was thinking of doing a linear regression of Q1 on Q2. in order to figure out which of these factors are significant when making a decision. Denote by $Y$ the frequency and by $X_i$ a binary variable indicating whether the respondent associated factor $i$ with the store:
$$Y \sim 1 + X_1 + ... + X_{12}$$
The problem with this seems to be perfect multicollinearity: $\sum_i X_i = 3$. Curiously, when I ran the regression in R, I checked the model with car::vif and didn't get alarmingly large values (mostly around 2 for each coefficient).
Apart from that, it's difficult to interpret the results... the values of the coefficients don't really correspond to anything, they only give a rough idea of the factors' relative importance. For example, if the average value of $Y$ is 4.5, I get an intercept of 4.0 and the coefficients all approximately 0.1-0.2, so that picking three of them would roughly correspond to the average. In a sense, the regression compares the respondents to a hypothetical person who "didn't select anything", even though that's impossible.

Is this model even appropriate at all, given the collinearity problem? If not, what might be a good alternative?
I was wondering if forcibly removing the intercept from the model might be a good idea here to try and salvage it?
Comment: You didn't need an intercept in the first place, because it's automatically included in the $X_i,$ as you observed.  Thus, "forcibly removing" it doesn't change the model--it only causes `R` to use a different model matrix.  If indeed the variables are collinear (that is, everybody has provided three answers), then `R` has already dropped one of your variables already--most likely $X_{12}.$
Comment: @whuber So do I have multicollinearity here or not? The answer says I don't, but I can't see how the definition isn't satisfied (for all observations $-3 + \sum X_i = 0$)... on the other hand `R` didn't remove any variables (I checked), and the vif doesn't indicate multicollinearity.. what gives?
Comment: If `R` removed no variables, then the situation is not as you described it!
Comment: @whuber I suppose there might've been some people who failed to actually select three options. You're saying that if this weren't the case, R would detect it?
Comment: You can easily check: tabulate the values of $\sum X_i.$  If you only get the value $3$, then you have collinearity.
Answer: First, I am uncertain you have a multicollinearity problem, and you definitely do not have perfect multicollinearity.  This would suggest that knowing one value, you know all the others.  If I selected the 1st choice, I have know idea which of the remaining 11 choices were and were not select.  Likewise, if I select two specific choices, I still have no idea of which of the remaining 10 was chosen as the 3rd and final option. That said, the proposed model is not unreasonable (though a closer analysis of your research questions and analysis plan would be warranted).
Second, depending on how Y was answered (ordered categories, counts, etc.), you may want to consider a more general model than just a linear regression (e.g., an ordinal logistic model or a count model like a Poisson).
Third, the intercept does have a meaningful interpretation (as I understand your context). Though you forced respondents to select three options, you could have run the analysis where respondents would have been allowed to select from 0 to 12 options. Thus, the intercept should indicate the frequency when none of the options are considered important.
Comment: I would agree with your interpretation...though I would say the sum of the 3 chosen coefficients gives the average deviation for such respondents from the mean frequency.  As for multicollinearity, you are correct...but you don't have that here. For example, there is no way to have $X1 = c_2·X_2 + c_3·X_3 + ··· + c_{12}X_{12}$ with the data as you've described it.
Comment: Yes, but that would be a different model then. When they are forced to choose 3 associations, I'm not convinced about that interpretation... what I tried doing is to force the intercept to be equal to Y's mean in the model. This allows the coefficients to be interpreted in the following way: the sum of the 3 chosen coefficients gives the deviation from the mean frequency... is that right?

Also, I guess I'm confused about multicollinearity - I thought that occurred when there was an exact linear dependence between the regression variables..?


Angular Directive table rows issue

Question: I am a beginner Angular programmer, but I am really close to understanding the directives.
I create a fiddle here, but I have never used fiddle before, and it is not quite rendering ...
the tr-row is a directive.  I am trying to loop through the data, and print a directive (row) per record.
HTML:

      id
      name
      description

javascript:

var myapp = angular.module('myApp', [])
.controller('fiddleCtrl', ['$scope', function ($scope) {

$scope.data = [
     { id: 1, name: 'Fred',   description: 'not the best worker' },
     { id: 2, name: 'Wilma',  description: 'Freds Wife'},
     { id: 3, name: 'Barney', description: 'Freds best friend'},
     { id: 4, name: 'Louise', description: 'Never heard of Fred'},
     { id: 5, name: 'Tracy',  description: 'Some Chick'},
     { id: 6, name: 'Foo',    description: 'Inventer of bar'}
];
}]).directive('trRow', function ($compile) {
return {
    restrict: "E",
    replace: true,
    link: function (scope, element, attrs) {
        scope.id = scope.d.id;
        scope.name = scope.d.name;
        scope.desc = scope.d.description;

        var tmpl = '
{{id}}	{{name}}	{{desc}}
';
        element.html(tmpl).show();
        //var e =$compile(tmpl)(scope);
        //element.replaceWith(e);
        var e = $compile(element.contents())(scope);
    },
    scope: {
        d: "="
    }
};
});

should be easy. (le sigh)
any help would be appreciated, I REALLY need to understand this.
What is happening in my code is the tr-row directive has replace the table.  I get a list of them,
(with a tr INSIDE of a tr-row element but there is no table to display them in.  I know this means I am close, but I cant think of any new combinations to try.
I just need a simple table with rows in it.
I appologise if this has been asked a million times, I seem to be unsure what to search for. I have tried so many things.
Answer: Firstly, a tag name can't contain dash char. So you can't use tr-row as tag name, but you can use it as attribute.
Then, you can simply write a directive like that:
.directive('trRow', function () {

    return {
        template: '

'
    };
});

And usage is like that:

A working example in fiddle: http://jsfiddle.net/T7k83/85/
Comment: @Murat Corlu, you can have tag names which contain dash characters please see this fiddle - https://jsfiddle.net/Heartless141/u00351mk/1/
Comment: that is the easiest answer, and right to the point.  works great. thank you so much!
Comment: So, you can click tick on the left to set this as answer. Thanks, and welcome to stackoverflow :)
Comment: @Murat Corlu in the context of AngularJS https://docs.angularjs.org/guide/directive#directive-types
Comment: Dunno what changed but the fiddle example does not show any data in the table
Answer: Actually this problem is specific to
 elements.
Browser parsing engines don't like invalid tags inside
 so they will try to throw your directive out of the table (you can see that by inspecting the element), before your directive has a chance to replace itself with valid elements. This applies even if your directive doesn't contain dash in the name.
The way to solve this would be using directive type A instead of type E, which is suggested by @MuratCorlu.
For other elements such as
, you can pretty much replace it with custom tags with names containing dash. For example ng-repeat can be used as a tag.

Angular Directive table rows issue


Hadoop-Sqoop import without an integer value using split-by

Question: I am importing data from memsql to Hdfs using Sqoop. My source table in Memsql doesn't have any integer value, I created a new table including a new column 'test' with the existing columns.
FOllowing is the query
sqoop import  --connect jdbc:mysql://XXXXXXXXX:3306/db_name --username XXXX --password XXXXX   --query "select closed,extract_date,open,close,cast(floor(rand()*1000000 as int) as test from tble_name  where \$CONDITIONS" --target-dir /user/XXXX--split-by test;
this query gave me following error :
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as int) as test from table_name  where  (1 = 0)' at line 1
I tried it another way as well:
sqoop import  --connect jdbc:mysql://XXXXX:3306/XXXX --username XXXX --password XXXX   --query "select closed,extract_date,open,close,ceiling(rand()*1000000) as test from table_name  where \$CONDITIONS" --target-dir /user/dfsdlf --split-by test;
With the following query the job gets executed, but there is no data being transferred. It says split-by column is of float type and change it to integer type strictly.
Please help me with this to change split-by column as integer type from float type
Answer: The problem mostly seems to be related with the use  of alias as the --split-by parameter.
If it's required to use the particular column in the query , you can run the query
 'select closed,extract_date,open,close,ceiling(rand()*1000000)  from table_name' in the console, get the column name thus coming for the  table in the console and use it in --split-by 'complete_column_name_from_console' (here it should be --split-by 'ceiling(rand()*1000000)') .


How can I fix not all arguments converted during string formatting?

Question: I want to change some binary streams to decimal but I got not all arguments converted during string formatting. The point is I have the result when using binaryToDecimal (10100000) which 10100000 is the first value of my list. But when I use binaryToDecimal (instream_chunks[0]) which is my for loop I got the above error. How can I fix that?
I am new to python sorry for my simple question.......
def binaryToDecimal(binary):

    binary1 = binary
    decimal, i, n = 0, 0, 0
    while(binary != 0):
        dec = binary % 10
        decimal = decimal + dec * pow(2, i)
        binary = binary//10
        i += 1
    print(decimal)

    instream = '1010000000011000000110000000001'

    instream_chunks = [instream[i:i+8]for i in range (0, len(instream), 8)]

for i in range (len(instream_chunks)):

     img_value = binaryToDecimal (instream_chunks[i])

Answer: You need to convert the strings to integers before using your function on them. Like so:
def binaryToDecimal(binary):

    binary1 = binary
    decimal, i, n = 0, 0, 0
    while(binary != 0):
        dec = binary % 10
        decimal = decimal + dec * pow(2, i)
        binary = binary//10
        i += 1
    print(decimal)

instream = '1010000000011000000110000000001'

instream_chunks = [instream[i:i+8]for i in range (0, len(instream), 8)]

for i in range (len(instream_chunks)):
     img_value = binaryToDecimal (int(instream_chunks[i]))


Create a new region within an existing one

Question: I want to create a new region within an existing region which is by default or is custom made. I have created a custom region called footer_top. I want to create a new region within this region, i.e, a nested region. What should i do...what code should i write in page.tpl.php and how?
Thanks
Comment: @SGhosh...i just want to learn
Comment: Why do you need to have nested region? I think you could simply have three regions instead, rendered in that order.
Answer: You can use the region.tpl.php
And add a template_preprocess_region to add the new region variable with in this region.
You can use the following code to add the region variable to the region.tpl.php
$variables['custom'] = block_get_blocks_by_region('custom');

So the over all process would be as follows.
first you would create a preprocess_region function as given below
function theme_name_preprocess_region(&$variables) {
  $variables['region_name'] = block_get_blocks_by_region('region_name');
}

next you would create a region template file called region--region_name.tpl.php
and print the variable $variables['region_name'];
Comment: So this will print another region inside one region. Both regions will be available in the blocks page for placing blocks right?


Trying to filter a table in order to get the newest product

Question: can give me a hand with this?
First of all, this is not homework something like that. I do not know how to solve this.
I tried so many times without success. I really dont know how to solve this.
Take a look at this picture:
https://gyazo.com/a8bde055c05874f031cde53c802aa145
Can please explain me what to do? I've searched, i tried but i dont know how to solve this
Comment: So, what have you tried?
Comment: Dude, i've been reading different threads, tutorials, own queries but it i dont know how to do it :(
Comment: I've been dealing with this since Friday :/
Comment: @a_horse_with_no_name can please help me, i really dont know what to do :/
Comment: https://stackoverflow.com/a/13228365/1127428
Comment: I assume that you are looking for the newest product in relation to the description right?
Answer: I assume that you are looking for the newest item in relation to the description.
The query below gets you a list of descriptions with their new update.
SELECT description, MAX(last_update_date)
FROM a
GROUP BY description

Then you have to join the table back onto itself to get the other columns back.
SELECT a.item_num, a_newest.description, a_newest.last_update_date, a.request_id, a.category
FROM (
    SELECT description, MAX (last_update_date)
    FROM  a
    GROUP BY description
) a_newest
LEFT JOIN a on a_newest.description = a.description
    AND a_newest.last_update_date = a.last_update_date

Comment: Thank you for replying!!
Comment: No, im looking for the newest item in relation to itself.. compare to their ítem_number..
Comment: I mean, i have to filter the table comparing the different dates of the ítems.. i mean, i have to do a query that bring me the ítems that have the newest last_update_date
Comment: The new table should contains only 1 ítem with a unique ítem:number.. not a duplicate with the same data but with an inferior last_update_date
Comment: item_number looks like a unique primary key, so this wouldn't make sense, but if that is the case then you can replace the a.description in the code above with item_num
Answer: You can do it with ROW_NUMBER
SELECT
    [ITEM_NUM],
    [DESCRIPTION],
    [LAST_UPDATE_DATE],
    [RESQUEST ID],
    [CATEGORY],
FROM (
    SELECT
        [ITEM_NUM],
        [DESCRIPTION],
        [LAST_UPDATE_DATE],
        [RESQUEST ID],
        [CATEGORY],
        ROW_NUMBER() OVER (
            PARTITION BY
                [ITEM_NUM]
            ORDER BY
                [LAST_UPDATE_DATE] DESC
        ) [RN]
    FROM [A]
) [A]
WHERE
    [A].[RN] = 1

Comment: Thank you for replying!!
Comment: Dude, are you sure this query will bring a new table with no ítem_number duplicates.?


Como conectar duas Aplicações em uma mesma instancia do LocalDb

Question: Estou com um problema na hora de conectar com uma mesma instancia do localDb em outra aplicação na mesma solução.
Detalhando o projeto, tenho uma aplicação ASP .NET MVC5 na solução e também uma outra aplicação de console na mesma solução.
Como construi o banco com code first na aplicação MVC, fix database first no console. Ele me gerou as classes das tabelas tudo certo, porém quando tento fazer alguma ação no banco da o seguinte erro:

SqlException: Cannot attach file 'C:\Users\E555555\source\repos\PGP.WebSolution\Web\App_Data\aspnet-Web.mdf' as database 'Web' because this database name is already attached with file 'C:\Users\E555555\source\repos\PGP.WebSolution\Console\bin\Debug\aspnet-Web.mdf'

Pelo que entendi fala que não da para usar o mesmo no de banco de dados porque ja esta sendo usado por outro arquivo .mdf.
Porém não quero criar outro arquivo mdf, quero usar o existente ja.
E a connection string das duas aplicações são a mesma. Como posso usar o mesmo arquivo .mdf em mais de uma aplicação?
Quem puder ajudar agradeço.
<code>//ConnectionString em ambas aplicações

<connectionStrings>
    <add name="Default" connectionString="data source=(LocalDb)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\aspnet-BradescoPGPWeb.mdf;initial catalog=BradescoPGP.Web;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>
</code>
Comment: tenta tirar a configuração initial catalog da sua string de conexão
Comment: Lucas Miranda, eu consegui resolver criando a base no SQLServer instalado na máquina mesmo, sem ser o .mdf, assim consegui acessar as bases.
Answer: Consegui resolver essa questão usando uma instancia SQLServer instalada localmente na maquina. Assim consegui acessar sem problema a mesma base em dois projetos diferentes.


Looking for official docs for all Javascript (BOM, DOM, built-in) Objects

Question: when I make Web Application.
As far as I know, There are three objects(BOM, DOM, built-in Objects) to javascript.
If I want to manipulate DOM object. I refer DOM official documentation in w3c.org following this.
https://www.w3.org/DOM/DOMTR
If I want to know built-in objects. I can refer ECMA-262 documentation. It's very hard to read but it's official documentation.
https://www.ecma-international.org/publications/standards/Ecma-262.htm
If I want to control browser. I am googleing about BOM. I wonder where is offical BOM Object documentation. I think Google and Mozilla have to provide official documentation. but I couldn't find Where it is.
first question :
Is there BOM standard like DOM?
Second question :
Where is Chrome official BOM documentation?
Comment: [Unfortunately](https://www.w3schools.com/js/js_window.asp), *There are no official standards for the Browser Object Model (BOM).*
Comment: In what way do you want to control the browser?
Comment: Where do you refer when you need to control browser?
Comment: whatever related BOM
Answer: I think maybe the closest thing to what you are looking for is a list of Web API interfaces - these are things like the Clipboard API and Navigator, which are implemented different on each browser, but accessible via a shared Javascript interface. The list on MDN Docs is pretty comprehensive.
Browsers can also expose extra internal controls, like adding extra options to the right click menu, by providing APIs that are callable from "extensions". However, unlike the Web APIs, these are often very different browser-to-browser, and it is best to refer to the documentation of that specific browser. For example, to add something to the right click menu in Chrome, that would be through the chrome.contextMenus API.

Chrome Extension APIs: here
Firefox Extension APIs: here

Finally, if you really want to go in-depth, you could always look at the source-code for Chromium or Firefox/Gecko. One of the great benefits of open-source!
Answer: As I explain in my other answer, the term "BOM" (browser object model) is not widely used, the more common name is the Web APIs.
The way the web standards are organized, there's no single specification describing all the APIs, instead there is a separate spec for each group of related APIs. For example the DOM, XMLHttpRequest, Full screen, etc. are all separate specs, usually worked on at WHATWG and/or W3C.
The (browser-agnostic) documentation for web developers is hosted on MDN (developer.mozilla.org). A Google Chrome team's post from 2017 indicates they have "been transitioning [their] web documentation efforts to MDN" and invide to use MDN Web Docs as "the centralized source of web API documentation".


What is the symbol above this note?

Question: What does the following symbol mean?
Comment: Please add the instrument involved to your question. That will help for others who have the same question.
Comment: I can barely read music, but I think that indicated a down-bow on a stringed instrument
Comment: This might be of interest, from a few years back: https://music.stackexchange.com/q/20154/9198
Answer: This is a "down bow" symbol. It means that the note should be played by drawing the bow from the frog toward the tip.

An "up bow" — drawing the bow from tip toward the frog — is a "V"-shaped symbol.

IMAGES SOURCE: http://www.the-violin.com/up-and-down-bow
Comment: This notation, probably adapted from bowed instruments, is also used for the guitar, where   ⃩ stands for down picking (towards treble strings), and V upstroke (towards bass strings).
Comment: @BrianTHOMAS I've heard different theories about the reason. One was that they were originally letters, with **n** denoting _nobilis_ and **v** _vilis_, which did actually not specify bowing direction as such but basically _importance_ of notes – and at some point players would be playing all weak notes with an upbow so it because synonymous. Another explanation was that they're simply icons depicting the frog and tip of a bow. Anyway on viols, cellos and basses the terms "down" and "up" don't really apply, they just spread over from the violins.
Comment: It always puzzled me that V, a downward arrow should mean Up Bow, whilst the ∏ symbol which has a line at the top means Down Bow.
Comment: @leftaroundabout never really thought about the fact that you don't pick/bow up/down on a cello/bass... it's sideways.. sort of :) - the first theory sounds pretty plausible though, interesting info.

What is the symbol above this note?


Estou precisando quebrar uma string que recebo e preciso dividi-la. Essa divisão se dará a cada virgula. Estou conseguindo ele reconhecer as virgula

Question: O que eu tenho feito até agora é isso:
#include <stdio.h>
#include <string.h>

 int cont = 0;
 int pos[6];
char buffer[107] = "A!#NanoGsmA)85996161991,B)85996161991,C)85996161991,D)85996161991,E)85986854563,DATA: 09/06/21-09:50:00!!";
char buff[105];

int main(){

    for (int i = 0; i < strlen(buffer); i++) {

      buff[i] = buffer[i];
      if (buff[i] == ',') {
        cont++;
    if (cont <6) {
          pos[cont] = i+1 ;
        }
      }
    }
      int var1 = pos[1] - pos[0];
      int var2 = pos[2] - pos[1];
      int var3 = pos[3] - pos[2];
      int var4 = pos[4] - pos[3];
      int var5 = pos[5] - pos[4];

printf("%d\n,%d\n,%d\n,%d\n,%d\n",var1,var2,var3,var4,var5);
 }

Mas ele não mostras os números que está sendo separado.
Comment: Vc não pode usar a [função`strtok`](https://www.cplusplus.com/reference/cstring/strtok/)?
Comment: a mensagem pode ser que venha apenas com um numero e o restante dos numero devem ficar iguais.Ex: A!#NanoGsm A)85996161991.
Comment: da certo sim o strtok, mas eu tentei criar algo automático, onde ele conta o numero de ',' e dividi-las a partir dessa contagem. Se tiver uma ideia como posso fazer isso vai ser de grande ajuda.
Comment: O problema é que assim vc tem que percorrer a string 2 vezes: uma pra contar e outra pra dividir. É mais simples percorrer a string uma vez só: começa da primeira posição e avança até encontrar a vírgula, mas já guardando os caracteres em uma substring. Quando achar a vírgula, guarda essa substring e começa outra: seta a posição inicial para depois da vírgula e vai até encontrar outra, guardando os caracteres na nova substring, e assim por diante...
Comment: Sugestão: https://ideone.com/R7SKFc (exemplo simples, que só imprime, mas se vc quiser guardar as strings, teria que adapatar e ir colocando as strings parciais em um outro array, por exemplo). Lembrando que usar `strlen` em um loop não é eficiente: https://pt.stackoverflow.com/q/167528/112052
Answer: Não sei se entendi a lógica do que está tentando fazer em seu programa, mas é algo como um scanf() imagino.
Se vai ler apenas uma tabela de valores e sempre tem 6 pode usar uma chamada a sscanf() e ler tudo de uma vez. Um comando só.
Sobre seu programa
Talvez concorde que
    char buffer[107] = "A!#NanoGsmA)85996161991,B)85996161991,C)85996161991,D)85996161991,E)85986854563,DATA: 09/06/21-09:50:00!!";

é ruim de ler. Pode escrever
    char    buffer[] = "\
A!#NanoGsmA)85996161991,\
B)85996161991,\
C)85996161991,\
D)85996161991,\
E)85986854563,\
DATA: 09/06/21-09:50:00!!";

usando ' \ ' para indicar que continua na próxima linha. É bem mais fácil. E não precisa declarar o tamanho de buffer já que o compilador pode contar pra você.
usando sscanf()
Veja esse programa
#include <stdio.h>
#include <string.h>

int main(void)
{
    char    buffer[] = "\
A!#NanoGsmA)85996161991,\
B)85996161991,\
C)85996161991,\
D)85996161991,\
E)85986854563,\
DATA: 09/06/21-09:50:00!!";

    char campo[6][107];
    const char* mascara =  "%[^,]%*c%[^,]%*c%[^,]%*c%[^,]%*c%[^,]%*c%[^,]";
    int res = sscanf ( buffer, mascara,
        &campo[0][0],        &campo[1][0],        &campo[2][0],
        &campo[3][0],        &campo[4][0],        &campo[5][0]
         );
    printf( "sscanf() leu %d\n", res);
    if ( res == 6 )
        for( int i=0; i<6; i+=1 )
            printf( "#%d: \"%s\"\n", 1+i, campo[i] );
    return 0;
 }

que só tem uma instrução e usa sscanf() para fazer esse serviço. Entenda que '[^,]' consome tudo que tiver no buffer até mas não incluindo a ',' e o '*c%' pula a óbvia vírgula que definiu o campo anterior e assim explica a variável mascara usada na chamada.
O programa mostra
sscanf() leu 6
#1: "A!#NanoGsmA)85996161991"
#2: "B)85996161991"
#3: "C)85996161991"
#4: "D)85996161991"
#5: "E)85986854563"
#6: "DATA: 09/06/21-09:50:00!!"

Não entendi porque está copiando buffer para buf se não está usando. Recomendo muito usar nomes mais significativos. buffer e buf é ruim de ler.
E se são strings pode copiar usando strcpy() como abaixo:
  char original[] = "coisas";
  char copia[100];
  strcpy( copia, original) ;

Não declare variáveis fora de main(). É um desastre.
Sobre esse trecho
      int var1 = pos[1] - pos[0];
      int var2 = pos[2] - pos[1];
      int var3 = pos[3] - pos[2];
      int var4 = pos[4] - pos[3];
      int var5 = pos[5] - pos[4];

Isso imagino que deveria dar os tamanhos dos campos. Note que não inicializou pos
Exemplo
Esse programa acho que faz o que quer
#include <stdio.h>
#include <string.h>

int main(void)
{

    char buffer[] = "\
A!#NanoGsmA)85996161991,\
B)85996161991,\
C)85996161991,\
D)85996161991,\
E)85986854563,\
DATA: 09/06/21-09:50:00!!";
// 23 14 14 14 14 25
    int     pos[107] = {0};
    int     i = 0;
    int     n = 0;
    while ( buffer[i] != 0 )
    {   if ( buffer[i] == ',' ) pos[++n] = i; // conta esse
        i += 1;
    };
    pos[++n] = strlen(buffer) - 1;
    printf("lidos %d campos\npos: ", n );

    // para conferir
    for( int i = 0; i<n; i+=1 ) printf("%d ", pos[i] );
    printf("\ntamanhos: ");
    for( int i = 0; i<n; i+=1 ) printf("%d ", (pos[i+1] - pos[i]) );
    printf("\nOs separadores: ");
    for( int i = 1; i<n; i+=1 ) printf("%c ", buffer[ (int)pos[i] ]);
    printf("\n");
    return 0;
 }

E mostra
lidos 6 campos
pos: 0 23 37 51 65 79
tamanhos: 23 14 14 14 14 25
Os separadores: , , , , ,

Uma outra maneira
Talvez seja mais útil ler a string e dividir em outras já prontas para usar...
Considere uma função assim:
EXEMPLO
char**      quebra(const char*);

Que você chama passando a linha e ela devolve uns ponteiros para os campos já separados, e que você pode chamar direto, por exemplo:
    char** uma = quebra("stack,overflow");

    for( int i=0; uma[i]!=NULL; i+=1 ) printf("%d:\t\"%s\"\n", 1 + i, uma[i]);

e ver na tela
1:      "stack"
2:      "overflow"

E é o normal em C porque é mais prático.
como seria uma função assim?
Eis um exemplo comentado em C
char**      quebra(const char* original)
{
    const char delim = ','; // fixo
    // agora aloca um vetor de ponteiros para as substrings
    char** subs = (char**)malloc(sizeof(char*)*(2 + strlen(original)) );
    const char* p = original;
    const char* pIn = p; // inicio da substring
    int N = 0; // N = total de substrings
    while (*p != 0)
    {   // ate o fim da string
        if (*p == delim) // achou uma sub
        {
            subs[N] = (char*)malloc(1 + p - pIn);
            memcpy(subs[N], pIn, p - pIn); // copia
            subs[N][p - pIn] = 0; // termina a string
            N += 1; // conta essa
            pIn = 1 + p; // inicio da proxima
        };  // if()
        p++;
    };  // while()
    subs[N] = (char*)malloc(1 + p - pIn); // a ultima
    memcpy(subs[N], pIn, p - pIn + 1); // copia a ultima
    N += 1; // conta essa
    subs[N] = NULL; // indica o final
    return subs;
}

A lógica é linear: um único loop varre a entrada e conforme encontra os delimitadores vai montando as strings. Note que as strings são cópias dos dados e podem ser usadas em separado, e a string original, declarada como const, é preservada.
um programa de teste completo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char**      quebra(const char*);

int main(void)
{
    char    buffer[] = "\
A!#NanoGsmA)85996161991,\
B)85996161991,\
C)85996161991,\
D)85996161991,\
E)85986854563,\
DATA: 09/06/21-09:50:00!!";

    char* teste[] =
    {
        "",
        "teste",
        buffer,
        ",,,",
        "1,2,3,4"
    };  // uns testes basicos

    // agora testa com todas as strings em 'outras'
    int n_testes = sizeof(teste) / sizeof(char*);
    printf("\n+%d testes:\n\n", n_testes);

    char** uma = quebra("stack,overflow");
    for (int i = 0; uma[i] != NULL; i += 1)
    {
        printf("%d:\t\"%s\"\n", 1 + i, uma[i]);
        free(uma[i]);
    };  // for()

    char** subs = NULL;
    for (int i = 0; i < n_testes; i += 1)
    {
        printf("\n==> #%d de %d:\nEntrada:\t\"%s\"\n", 1 + i, n_testes, teste[i]);
        subs = quebra(teste[i]);
        int     N = 0;
        while( subs[N] != NULL)
        {
            printf("%d:\t\"%s\"\n", 1 + i, subs[N]);
            free(subs[N]); // nao vai usar mais
            N += 1;
        }
        printf("\nEncontrada (s) %d substring (s)\n\n", N);
        free(subs); // nao vai usar mais
    };  // for()
    return 0;
}

char**      quebra(const char* original)
{
    const char delim = ','; // fixo
    // agora aloca um vetor de ponteiros para as substrings
    char** subs = (char**)malloc(sizeof(char*)*(2 + strlen(original)) );
    const char* p = original;
    const char* pIn = p; // inicio da substring
    int N = 0; // N = total de substrings
    while (*p != 0)
    {   // ate o fim da string
        if (*p == delim) // achou uma sub
        {
            subs[N] = (char*)malloc(1 + p - pIn);
            memcpy(subs[N], pIn, p - pIn); // copia
            subs[N][p - pIn] = 0; // termina a string
            N += 1; // conta essa
            pIn = 1 + p; // inicio da proxima
        };  // if()
        p++;
    };  // while()
    subs[N] = (char*)malloc(1 + p - pIn); // a ultima
    memcpy(subs[N], pIn, p - pIn + 1); // copia a ultima
    N += 1; // conta essa
    subs[N] = NULL; // indica o final
    return subs;
}
// fim

Que testa a função com um vetor de strings de teste, que inclui seu exemplo.
1:      "stack"
2:      "overflow"

+5 testes:

==> #1 de 5:
Entrada:        ""
1:      ""

Encontrada (s) 1 substring (s)

==> #2 de 5:
Entrada:        "teste"
2:      "teste"

Encontrada (s) 1 substring (s)

==> #3 de 5:
Entrada:        "A!#NanoGsmA)85996161991,B)85996161991,C)85996161991,\\...
D)85996161991,E)85986854563,DATA: 09/06/21-09:50:00!!"
3:      "A!#NanoGsmA)85996161991"
3:      "B)85996161991"
3:      "C)85996161991"
3:      "D)85996161991"
3:      "E)85986854563"
3:      "DATA: 09/06/21-09:50:00!!"

Encontrada (s) 6 substring (s)

==> #4 de 5:
Entrada:        ",,,"
4:      ""
4:      ""
4:      ""
4:      ""

Encontrada (s) 4 substring (s)

==> #5 de 5:
Entrada:        "1,2,3,4"
5:      "1"
5:      "2"
5:      "3"
5:      "4"

Encontrada (s) 4 substring (s)


linux x11 window manager - blocking mouse events

Question: Let's assume that a few windows are visible - I need to implement mechanism which will block events for a selected windows, for example when user click a button in window_nr1 and window_nr2 nothing happens but when a user click a button in window_nr3 event is processed. Compositing window manager renders textures and a window location which is actual for XServer may be different, but this is some kind of workaround so let's also assume that a window texture is rendered without transformations. In Compiz XGrabPointer is use to capture mouse events:
XGrabPointer (privateScreen.dpy, privateScreen.eventManager.getGrabWindow(), true,
              ButtonReleaseMask | ButtonPressMask | PointerMotionMask ,
              GrabModeAsync, GrabModeAsync, privateScreen.rootWindow(),
              cursor, CurrentTime);

where a creation of grab window looks like this:
grabWindow = XCreateWindow (dpy, root, -100, -100, 1, 1, 0,
                            CopyFromParent, InputOnly, CopyFromParent,
                            CWOverrideRedirect | CWEventMask,
                            attrib);

In that case mouse events are captured (i.e. redirected only to a window manager event loop) for the whole screen and it looks like that XGrabPointer can't be used for a capture mouse events for a particular window.
Do you have some ideas how to implement such mechanism ?
Answer: Grab the mouse pointer and pass on the events to the destination window.
Comment: @Irbis: Yes XSendEvent. And yes, if your WM is not running on the same machine as the X11 server a noticable lag may be introduced. However if the WM and X11 server are on the same machine the added latency should be way below 1ms (only a few couple 10µs or so if the WM is efficiently written).
Comment: Do you mean XSendEvent ? I know windows parameters  so I can resend mouse events only to the windows which are not blocked, but I wonder if it doesn't introduce some lag...


URL redirect for changed url structure

Question: I changed my Drupal 6 website tag structure and got a new URL however old URLs are indexed in Google and are generating 404 errors.
My old URLs are
www.mydomain.com/topics/12345/widget
www.mydomain.com/topics/5783/widget2

numeral - 12345 in this URL was tag no in the database
new URL structure is
www.mydomain.com/topics/widget
www.mydomain.com/topics/widget2

Now I wish to create a redirect so that
mydomain.com/topics/12345/widget

goes to
www.mydomain.com/topics/widget

and remove the random numeral nos in URL.. How to do it?
Answer: You can use Path Redirect module. Module page says -

This module allows you to specify a redirect from one path to another
  path or an external URL, using any HTTP redirect status.

As I see you are talking about taxonomy terms, so alternatively you can use Taxonomy Redirect.

This module allows the administrator to change the destination of
  Taxonomy Term links.

The module can now handle redirections at the term level.
You can specify a replacement character for spaces and + characters.
You can specify a list of characters or strings to remove from the path
You can enter php code that will create your path at runtime (In 5.x-1.3, variables for term ID ($tid) and term name ($tname) are available to use in your php code).
Comment: other than this is there any htaccess redirect also possible?
Comment: You can look into documentation for .htaccess redirect at https://drupal.org/node/38960
Comment: can u give specific example as what to do as i am a newbie and cant figure out
Comment: @Deepblue You can through the documentation of https://drupal.org/project/path_redirect. It's pretty much self-explanatory..

URL redirect for changed url structure


How many tries on average before I see the same value $N$ times in a row?

Question: If I repeatedly roll a fair, $X$-sided die, on average how many rolls should I expect to make before I happen to roll the same value $N$ times in a row?
I've found questions on here with answers when $X=2$, or where $N=2$, or where you're looking for a specific result $N$ times in a row; I'm interested in the situation where I don't care what the specific value is, I just want it to be $N$ times in a row. The closest I can find is Suppose we roll a fair $6$ sided die repeatedly. Find the expected number of rolls required to see $3$ of the same number in succession., but I can't quite figure out how to generalize it beyond $N=3$.
Comment: Are you interested in seeing a specific value repeated $N$ times or merely any value repeated $N$ times?
Comment: Any value, doesn't matter what it is.
Answer: Let $E_k$ denote the expected number of rolls that are still needed if $k$ rolls have passed with equal result. We write recurrence relations for $E_k,0\leq k\leq N-1$ and calculate the wanted expectation $E=E_0$ from them.

We obtain
  \begin{align*}
E_0&=E_1+1\tag{1}\\
E_1&=\frac{1}{X}(E_2+1)+\frac{X-1}{X}(E_1+1)\\
E_2&=\frac{1}{X}(E_3+1)+\frac{X-1}{X}(E_1+1)\\
E_3&=\frac{1}{X}(E_4+1)+\frac{X-1}{X}(E_1+1)\tag{2}\\
&\ \ \ \vdots\\
E_{N-3}&=\frac{1}{X}(E_{N-2}+1)+\frac{X-1}{X}(E_1+1)\\
E_{N-2}&=\frac{1}{X}(E_{N-1}+1)+\frac{X-1}{X}(E_1+1)\\
E_{N-1}&=\frac{1}{X}+\frac{X-1}{X}(E_1+1)\tag{3}\\
\end{align*}
Comment:

In (1) we note that any roll of the $X$-sided die will do.
In (2) we are in the situation that a run of length $3$ is given. With probability $\frac{1}{X}$ we  get a run of length $4$ and with probability $\frac{X}{X-1}$ we get a different result and start with a run of length $1$.
In (3) we get a run of length $n$ with probability $\frac{1}{X}$ and can stop or we are back at the beginning and start again with a run of length $1$.

This system can be easily solved. We iteratively obtain be respecting all equations from above besides the last one (tagged with (3)).
  \begin{align*}
E_1&=E_0-1\\
E_2&=E_0-(1+X)\\
E_3&=E_0-(1+X+X^2)\\
E_4&=E_0-(1+X+X^2+X^3)\\
&\ \ \ \vdots\\
E_{N-1}&=E_0-(1+X+X^2+\cdots+X^{N-2})\\
&=E_0-\frac{X^{N-1}-1}{X-1}\tag{4}\\
\end{align*}
From equation (4) and (3) we finally conclude
  \begin{align*}
E_{N-1}&=E_0-\frac{X^{N-1}-1}{X-1}\\
(1-X)E_0+XE_{N-1}&=1
\end{align*}
  from which
  \begin{align*}
\color{blue}{E=E_0=\frac{X^N-1}{X-1}}
\end{align*}
  follows.

Note: This approach is a generalisation of @lulu's answer of this MSE question. In fact we get as small plausibility check with $N=3$ and $X=6$ the result
\begin{align*}
E=\frac{6^3-1}{6-1}=\frac{215}{5}=43.
\end{align*}
in accordance with @lulu's result.

[Add-on] The strongly related problem: Rolling a fair $X$-sided die until we get a  run of length $N$ of  a  specific  value can be solved by the system above with the first equation (1) replaced with
  \begin{align*}
E_0&=\frac{1}{X}(E_1+1)+\frac{X-1}{X}(E_0+1)
\end{align*}
  which gives the result
  \begin{align*}
\color{blue}{E=E_0=\frac{X\left(X^N-1\right)}{X-1}}
\end{align*}
  This is also plausible compared with the result above, since the probability to roll a specific value is $\frac{1}{X}$.
Answer: Suppose $N\ge2$, otherwise the problem is trivial. We need at least two rolls. Let $Y_1,Y_2,\ldots$ be the values of the rolls, that
is independent random variables each uniformly distributed in
$\{1,2,\ldots,X\}$. Define $Z_j\in\{0,1,\ldots,X\}$ by $Z_j\equiv Y_j-Y_{j+1}\pmod X$. Then the $Z_j$ are also independent and uniformly distributed. In effect we are looking for the expectation of $T$, which is
the least $t$ with $Y_t=Y_{t-1}=\cdots= Y_{t-N+1}$, equivalently
the least $t$ such that $Z_{t-1}=Z_{t-2}=\cdots= Z_{t-N+1}=0$. Thus $E(T)=1+E(U)$ where $U$ is the number of rolls needed to get $N-1$
consecutive occurrences of a specific value. You can use the Markov
chain method in the linked solution to find that.
Answer: $N_1$ - No of tosses for 1st Heads
$N_2$ - No of tosses for 2 consecutive Heads
$N_3$ - No of tosses for 3 consecutive Heads
$\mathbb E[N_1] = 2$
$\mathbb E[N_2] = (1+\mathbb E[N_1])2 = 6$
$\mathbb E[N_3] = (1+\mathbb E[N_2])2 = 14$
A general formula can be created
$\mathbb E(1) = X$
$\mathbb E(2) = (X+1)X$
$\mathbb E(3) = \big((X+1)X+1\big)X$
and so on.

$$\mathbb E(N) = \sum_{i=1}^N X^i = \frac{X(X^N-1)}{X-1} $$

I have made a video explanation and made a simple formula at the end
https://youtu.be/d72rcsBblRE
Comment: Please don't answer old questions if your answers have nothing more to add to the ones already existing.


Should the Drupal 8 sites/default/files/config folder be stored in git with its hash suffix?

Question: New question title: "Should the sites/default/files/config folder be stored in git with it's hash suffix?"

" Reason I ask this is that this might cause 2 issues:

May be a hindrance when comparing code bases of sites, the diff comparison program would not be able to compare the contents of the config folder directly, if the suffixes are different.
If the site is a open collaboration project, where the code is put on github or bitbucket publicly, then the config hash suffix which is normally secret would be visible to the public and therefore the folder can be determined but the intent was to use the hash to make the folder harder to guess for security reasons.

Original question title: "Why does the Drupal 8 config folder have a long gibberish-like name, e.g.: config_wNOLcm... etc?"

Answer to the question about the 'gibberish' is:

This sequence is a random hash generated during the installation of
  your Drupalvsite. It is used to add some protection to your
  configuration files. Additionally to the default restriction enforced
  by the .htaccess file within the subdirectories of the config
  directory that prevents unauthorized users from seeing the content of
  the directories. As a result, would be really hard for someone to
  guess the folder's name.

Credit: Drupal 8 Configuration Management, First published March 2015, page 29
Some general references:
https://www.drupal.org/node/2360331#how-do-i-version-my-
Answer:
In the site install process, Drupal will write the configuration directory path to settings.php.
Any other deployments of the site would then make use of the directory defined in settings.php.
The hash itself is not information that needs protection, but it helps make the configuration harder to find in case it were not properly protected from public access.
If you're making your entire configuration public in a repository, it is important to check that it does not contain any compromising information (e.g. API keys).

The configuration should be protected by .htaccess (which core does by default) if it is in a publicly accessible directory, making it impossible to read and not just difficult to find.
It is possible to move the sync directory to another location.
Pantheon places it in sites/default/config, with an .htaccess file to protect it.
Alternately, setting up your repository so that Drupal is not in the root folder (as Acquia does) will allow creating a configuration directory that is outside of the document root.
/
|-- docroot
|    |-- core
|    |-- modules
|    |-- ...
|
+-- config
     +-- sync

Comment: +1 on your answer for your time. So does that mean that the hash suffix is no longer required, given that .htaccess prevents access? Their idea of using a suffix for making a folder hard to guess seems a bit peculiar to me, as if this really was a good idea then they'd want to do it to every folder. From your answer it sounds like this is no longer required. And perhaps now if I revisit the now released Drupal 8 since asking this question, I may find that the hash is not used and your answer is actively being used instead.
Comment: Since the hash is random per-installation, it should make an automated attack to scan Drupal sites for unprotected configuration prohibitively expensive.  It may not be likely for sites to not have the .htaccess protections in-place, but it would offer a thin layer of protection should that occur.  For repository-managed code bases, I would definitely recommend placing the sync directory outside of the document root, and a hash would not be necessary.
Comment: Thanks gapple. Why would configuration in particular be vulnerable? Why not all the other Drupal files? Related to my original question: how should config be stored in git along with the rest of the Drupal code?
Comment: Most of the configuration is not remarkable (e.g. entity and field configurations), but there are likely items within configuration that should remain private such as API keys for external services.
Comment: Ah sure that makes sense regarding API keys, certainly I have experience of that in D7, and have put that in settings.php for D7 and it is listed in .gitignore I suppose it might make sense to group all external API keys / credentials in one place and git ignore just that file and keep the rest of the config in git source. Thoughts?
Comment: Having a settings.local.php file is a common pattern that's now included  in core's default.settings.php (though commented by default).  The [example.settings.local.php](https://api.drupal.org/api/drupal/sites!example.settings.local.php/8) included in core is development focused, but you can use it in production with just deployment specific settings like database config or API keys.


User password age/complexity policy

Question: Has anyone got any resources for determining a reasonable password policy for my users?  My personal leaning is to ratchet up password complexity and allow them to change them less often as a kind of compromise.  It seems that my average user has a higher tolerance for mixing in some numbers and special characters than they had 5 or 10 years ago.
I'm looking for rules of thumb and/or resources I can use to back up my proposed policy changes.  Or even anecdotal info from those with more experience.
(I'm far from a security guru, so if that's just to vague to deal with, let's narrow the question to apply just to internal Windows networking passwords, though I'd be interested in what people are doing in terms of VPN and web service policy)
Comment: Can you tell us more about the software environment you need to tailor this to? Windows, *nix, Plan9 ... ???
Comment: More a policy question than technology-specific.  Probably borders on a religious discussion to get into whether you'd need a different policy for windows vs. *nix boxes?  ;)
Answer: In today's world of random brute-force password attacks, I tend to agree with the statement that: a good password written down is better than a memorized password that is easy to guess
Comment: No, no. You write down the correct password and store it in your wallet, then write down the wrong password and stick it under your keyboard. If you find your account locked out, call your security personnel.
Comment: For people that keep forgetting their passwords then I would recommend that if they have to write it down then they do so and put it in their wallet or purse. People don't tend to leave those lying around and also notice if they have disappeared ;)
Comment: Actually, I WOULD INCLUDE "written down and taped to the monitor in plain sight" - simply because so many attacks come remotely, that a poor password is a MUCH greater risk.
Comment: You have forgotten: ...written down and kept at a secret place. No, putting it under the keyboard does not count ;-)
Comment: Whenever I write down a password I make a practice of adding a couple of characters to the beginning and end.  I know they are extra, but anyone else wouldn't.  I guess this wouldn't work so well with dictionary words though.
Comment: Obviously written down and kept hidden is better.  Nathan has a good suggestion.
Answer: Here's a good comparison of password strength:
http://www.lockdown.co.uk/?pg=combi
Comment: Could you please write something about the link into your answer, for example why you think this link is a good read regarding the question?
Comment: Why is this getting voted down? Seems to me this is what the asker was wanting.
Comment: Since it's exactly the kind of information I was looking for, maybe my question was flawed?  I'm guessing people object to bare-hyperlink answers, but I'm not sure what else needed to be said.
Answer: You're doing the right thing by considering what your users are willing to work with. If you force highly complex passwords that must change frequently, you'll find your post-it note consumption will skyrocket.
Comment: @tomjerdz: thanks. I did take a screenshot, though ;)
Comment: that's what my employer does now: 60 days, "3-of-4", and no repeats in the last 24. So everybody comes-up with easy-to-remember "variations on a theme". Not nearly as "secure" as it should be, sadly.
Answer: There's a sensible contribution to this topic from Gene Spafford at Purdue's CERIAS.  Here's a partial quote:

So where did the "change passwords
  once a month" dictum come from?  Back
  in the days when people were using
  mainframes without networking, the
  biggest uncontrolled authentication
  concern was cracking.  Resources,
  however, were limited.  As best as I
  can find, some DoD contractors did
  some back-of-the-envelope calculation
  about how long it would take to run
  through all the possible passwords
  using their mainframe, and the result
  was several months.  So, they
  (somewhat reasonably) set a password
  change period of 1 month as a means to
  defeat systematic cracking attempts.
  This was then enshrined in policy,
  which got published, and largely
  accepted by others over the years.  As
  time went on, auditors began to look
  for this and ended up building it into
  their "best practice" that they
  expected.  It also got written into
  several lists of security
  recommendations.
This is DESPITE the fact that any
  reasonable analysis shows that a
  monthly password change has little or
  no end impact on improving security!
  It is a "best practice" based on
  experience 30 years ago with
  non-networked mainframes in a DoD
  environment—hardly a match for today's
  systems, especially in academia!
Comment: +1 Interesting. I always wondered whose idea that was.
Answer: I'm much more in favour of longer password (which, realistically, means as in multi-word phrases in you're going to remember them) instead of mixing in words and numbers. If you force people to add numbers, they're more likely to do simple things like replace i with 1 etc. which doesn't gain you very much security.
http://www.iusmentis.com/security/passphrasefaq/
Comment: Passphrases are a definite improvement over passwords in terms of memorability and complexity.
Answer: There are tons on material on Password Policies. I recommend taking a look at

The wikipedia article on Password Policy
SANS Password Policy document.
NIST sp800-118 draft titled Guide to Enterprise Password Management

Now, something must be said about password sanity. The fact is that password that are hard to remember are written down. The same goes for passwords that must be changed too often. The bottom line, it depends on what you're protecting and your userbase.
Answer: The policy should reflect what the users are using the computers for, how sensitive information the user handle on it. If it is just to contain the users preferences you do not really need it heavily fortified if everything else is in place to repel attacks. If the opposite is the case I would rather have annoyed users moaning about complex passwords to remember.
I have about 20-some complex passwords in my head at all times, and I have started creating them using patterns on the keyboard to remember them by. It makes it easier as I only need to know the starting point and what kind of pattern to make. Everybody hates changing passwords, but this technique allows me to change them around easily and remember them, so security is tight.. for me at least. I can even note down one letter on a piece of paper and still recall what pattern to make, and I don't really remember things too well. If this has any use for anyone however.. I don't know.
Writing down a complex password without obfuscating it seems just wrong to me. If someone is trying to break into a computer physically you can be sure they will look for some tell-tale.
Answer: I believe, when I worked for a healthcare institution, that some of our requirements came from HIPAA.  I haven't looked at the SOX regs (which I guess I now adhere to in the insurance world), but they may have some similar language as a basis for this sort of thing.
Beyond that, if you're part of a larger IT organization (sub-division in a larger corporation) the corporation may have rules that could be adhered to.
Bottom line needs to be that, whatever policy gets implemented, it be blessed by senior management, and preferably written up in a security or IT policy that all staff need to be made aware of/adhere to.  It doesn't need to be draconian (change password every 180 days, combination of alpha and numerics), but it should be company policy so everyone is crystal clear on the requirement.
Answer: Been some good suggestions so I'm just going to add this:
As long as you're able to make sure that you can be exempt from the policy... I have a good memory, so personally I prefer to be able to use a 18 character password that's ridiculously complex for 6 months or more than a simple one that I have to change once a month.
Keep in mind that a 16 character password that uses only lowercase and uppercase letters and spaces gives 53^16 combinations or 3.876*10^27, whereas 10 character passwords that use lowercase and uppercase letters, numbers and symbols only gives 95^10 or 5.987*10^19.

User password age/complexity policy


How to theme the views exposed form?

Question: I've created a view with a number of exposed filters, but it is pretty ugly looking.

I'd like to improve the theming, including wrapping the whole thing in a fieldset as well as grouping some of the other elements (like pairing the published and updated inputs), but not sure how to go about this.
I tried to var_dump the form, but it seems to go on forever and my browser locks up, so I can't easily learn anything about the form that way.
I also tried putting the form as a fieldset child in another form, but getting all the form ID information and so on proved problematic (although, I did get the fieldset styling).
Anyone got any pointers?
Update:
I copied over the template from the module to my site's theme directory, and made a start.
<fieldset>
    <legend>Filters</legend>

            <?php foreach($widgets as $id => $widget): ?>

                    <?php if (!empty($widget->label)): ?>
                        <label for="<?php print $widget->id; ?>">
                            <?php print $widget->label; ?>

                    <?php endif; ?>
                    <?php if (!empty($widget->operator)): ?>

                            <?php print $widget->operator; ?>

                    <?php endif; ?>

                        <?php print $widget->widget; ?>

            <?php endforeach; ?>

                <?php print $button ?>

</fieldset>

I can't seem to figure out how to pair up the date fields - I need to somehow modify the widget properties so I can wrap them in HTML (using hook_form_alter doesn't work because the #prefix and #suffix are added to $widget->widget so they break the output)
Answer: Theming Views' exposed form is hard because it doesn't behave like most forms and use its own label mechanism. So, you can't simply use a form_alter hook to add #prefix and #suffix to elements. But using THEME_preprocess_views_exposed_form, you can craft your own suffix/prefix mechanism:
function THEME_preprocess_views_exposed_form(&$variables) {
  if ($variables['form']['#id'] == 'views-exposed-form-VIEWNAME-DISPLAYID') {
    foreach ($variables['widgets'] as $id => &$widget) {
      switch ($id) {
        case 'first_date_id':
          $widget->prefix = '
';
          break;
        case 'last_date_id':
          $widget->suffix = '
';
          break;
      }
    }
  }
}

and in the template file
<fieldset>
    <legend>Filters</legend>

            <?php foreach($widgets as $id => $widget): ?>
                <?php if (!empty($widget->prefix)) print $widget->prefix; ?>

                    <?php if (!empty($widget->label)): ?>
                        <label for="<?php print $widget->id; ?>">
                            <?php print $widget->label; ?>

                    <?php endif; ?>
                    <?php if (!empty($widget->operator)): ?>

                            <?php print $widget->operator; ?>

                    <?php endif; ?>

                        <?php print $widget->widget; ?>

                <?php if (!empty($widget->suffix)) print $widget->suffix; ?>
            <?php endforeach; ?>

                <?php print $button ?>

</fieldset>

Answer: You can copy views-exposed-form.tpl.php from sites/all/modules/views/theme to your theme path to override the template.
Comment: You can also override for specific views only with patterns like `views-exposed-form--view_id.tpl.php` or `views-exposed-form--view_id--display_id.tpl.php`, [more info](http://drupal.stackexchange.com/questions/201143/how-to-theme-one-specific-views-3-exposed-filter-form-template-and-html)
Answer: Also take a look at the Better Exposed Filters module. You'll need the -dev release to get the collapsible filters option, though I'm hoping to get a proper 1.1 release out soon...
Answer: You should look into what's in the form['#theme'] item. Better way to do this (better than var_dump()) would be using dsm($form) or even kpr($form), these functions will be availible after you install the devel module (http://drupal.org/project/devel). form['#theme'] should be an array of about 7 elements. These elements are names of theme hooks that are called when this form is being rendered. you can use them to implement you own way of themeing a form.
One of the items will be called views_exposed_form__VIEW_NAME__DISPLAY_ID.
In your module (if you have one) there should be an implementation of hook_theme()
function MYMODULE_theme($existing, $type, $theme, $path) {
  return array(
    'views_exposed_form__VIEW_NAME__DISPLAY_ID' => array(
      'function' => 'my_exposed_form_theme_function',
      'render element' => 'form',
    )
  );
}

// somwhere later in code

function my_exposed_form_theme_function($vaiables) {
  //$vaiables['form'] contains a form array
  //to display a form element call drupal_render($vaiables['form']['some element'])
  //don't forget to call drupal_render($form)
  //after you are done with rendering individual elements
  //
  //you can use template_preprocess_views_exposed_form() from
  // views/theme/theme.inc as an example

  return "concatenated results of multiple drupal_render() calls";
}

Answer: In previous situations I have used Hook_form_alter() to add prefix and suffix to form elements to wrap them up in div which can then be styled. Not sure about wrapping in fieldsets though.
eg.
$form['submitted']['full_name']['#prefix'] = '
';
$form['submitted']['message']['#suffix'] = '
';

Answer: Use Drupal's hook_form_FORM_ID_alter to modify the form. This is what solved my problem of prefixing a £ character to my price field (put this in your custom module):
function THEME_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
   // check this is the right form
   if ($form['#theme'][0] == 'REPLACE_THIS') {
      // add the prefix
      $form['price']['min']['#prefix'] = "£";
   }
}

Answer: Views exposed form layout (D7) module will help you with views exposed form theming.
Answer: in the edit page of a view we can check theme information (bottom right corner).
It gives the information regarding the template files used by Drupal to render the different parts of the view on screen. By default the template files provided by the views module(inside its theme folder) is used, but in case, where we want to modify the way a view is displayed we can override those template files with custom template files that we can put inside our own custom theme folder (or the theme that is going to be used to display this view)
This theme information shows us how to name a file according to the hierarchy (or the scope of the effect of the template file you are going to override the views own templates.)
Now, this theme information may not show all the template files that the view uses to render this specific view.
so,
go to sites/all/modules/views/theme and copy the views-exposed-form.tpl.php (because in this case we want to override the default for rendering filters!)and paste it inside your own theme folder,
now you have to follow the same method to control the effect of this overriding template file that you have put inside your own theme folder by renaming it accordingly. e.g. views-exposed-form--your view name-pane name.tpl.php
(you will understand the nomenclature by looking at theme:information in the view's edit page)
now you can add css to individual widgets (generated by filter handlers) in this template file and those settings will be applied to the filters.
visit http://eureka.ykyuen.info/2011/07/25/drupal-theme-the-exposed-filter-in-views/

How to theme the views exposed form?


Elegant way to match and replace part of string

Question: I have two data tables like namely dt and dt1
dt <- data.table(s=c("AA-AA-1", "BB-BB-2", "CC-CC-3"))
        s
1 AA-AA-1
2 BB-BB-2
3 CC-CC-3

dt1 <- data.table(x=c(1,2,3), name=c("AA", "BB", "CC"))
   x   name
1: 1   AA
2: 2   BB
3: 3   CC

I need to replace part of string in s column of dt  with name column of dt1 by matching number after last hyphen of s & x col in dt1 column in dt, so that dt becomes like this.
      s
1: AA-AA-AA
2: BB-BB-BB
3: CC-CC-CC

I know  we can do it by splitting s and matching
split <- lapply(strsplit(as.character(dt$s), split="-"), tail, n=1)
dt1$name[match(dt$split, dt1$x)

Is there any way to speed it up in elegant way?
Comment: As you are asking for speeding up the code, how many rows do `dt` and `dt1` have, resp., in your production setting?
Comment: The rows in dt could reach upto a million and has It has lots columns. Its repeated operation so I need to save whatever time I could.
Answer: Here is a base R approach.  We can create an x column in the first dt data table, using the digit appearing to the right of the final dash.  Then, we can merge the two data tables on x, and finally concatenate the s result you expect.
dt$x <- sub(".*-", "", dt$s)
result <- merge(dt, dt1, by="x")
result$s <- paste0(sub("\\d+", "", result$s), result$name)

result$s
[1] "AA-AA-AA" "BB-BB-BB" "CC-CC-CC"

Demo
Answer: base R, sprintf + sub
mapply(sprintf, sub("[^-]+$", "%s", dt$s), dt1$name)
#   AA-AA-%s   BB-BB-%s   CC-CC-%s
# "AA-AA-AA" "BB-BB-BB" "CC-CC-CC"

I presumed that both data frames are in a matching order (as they are in the example). If not, you need to match them before, for example:
mapply(sprintf, sub("-.?$", "-%s", dt$s), dt1$name[match(gsub("[^0-9]","", dt$s), dt1$x)])

Comment: I like the idea, but OP is pretty explicit about matching `x` to the number
Comment: @eddi thanks for the edit. I added a line that does matching before `sprintf`ing ..
Answer: I'd take the straightforward approach:
dt1[dt[, .(x = as.integer(sub('.*-', '', s)), str = sub('[^-]+$', '', s))],
    on = .(x), .(s = paste0(str, name))]
#          s
#1: AA-AA-AA
#2: BB-BB-BB
#3: CC-CC-CC

Answer: Here is a slightly more general approach.
mapply(function(pat, repl, src){ sub(pat, repl, src) }, pat = dt1$x, repl = dt1$name, src = dt$s )

#[1] "AA-AA-AA" "BB-BB-BB" "CC-CC-CC"

If you say you always want to replace after last - (hyphen), then you can simplify as:
mapply(function(repl, src){ sub("(?<=-)[^-]+$", repl, src, perl = T) }, repl = dt1$name, src = dt$s )

Please note: My solution works only if dt and dt1 are ordered like in the example. This means each first rows are related, ... and so on. If this is not the case consider a combination of @Tims (the merging ...) and my solution.

So here is a rock-solid solution using some of Tim's ideas:
dt  <- data.table(s=c("AA-AA-1", "BB-BB-2", "CC-CC-3"))
dt1 <- data.table(x=3:1, name=c("CC", "BB", "AA"))  # note the order is not right.

dt$x <- sub(".*-", "", dt$s)
dt   <- merge.default(dt, dt1, by="x")
dt$endResult <- mapply(function(repl, src){ sub("(?<=-)[^-]+$", repl, src, perl = T) }, repl = dt$name, src = dt$s )

Comment: my dt and dt1 may not be in order. your solution looks good it doest not introduce new column in dt table. I need that. can you please look into it likewise.
Answer: If they are sorted appropriately as in your example you can use stringr::str_replace:
library(stringr)
dt[,s := str_replace(s,as.character(dt1$x),dt1$name)]
dt
#           s
# 1: AA-AA-AA
# 2: BB-BB-BB
# 3: CC-CC-CC


How to rebase feature branch on dev branch and then update dev

Question: I have this
a -- b -- c -- f                   <-- dev
       \
        d -- e -- g              <-- feature

I want this
a -- b -- c -- f -- d -- e -- g  <-- dev

I know this is related to the rebase command. However, when I type git rebase dev feature, commits go to feature branch and dev is not updated. If I use git rebase feature dev then last commits are those from master and I have duplicates commits. How can I achieve this ?
Comment: Yes, so you want to rebase feature _onto_ dev, then fast-forward "merge" feature _into_ dev. Note you'd typically write that c-f-d'-e'-g' to make it clear those commits are _rewritten_, because they have new parents (d had b, d' has f). A merge doesn't always mean a merge commit, it could be a squash or fast-forward.
Comment: _Do_ you want that? Shouldn't you have c-f-d'-e'-g' on _feature_, so it's trivial to "merge" into dev with a fast-forward?
Comment: I would like to avoid the ugly 'Merge' commit if possible and report all the history made on the feature branch on the dev branch
Comment: That's my point, if you rebase feature on dev (as the title says) you don't need a merge commit, you can fast-forward. But the thing you say you want _isn't_ that.
Comment: My goal is to have all the commits of the feature branch reported in the dev branch. Sorry for misunderstanding
Comment: Perfect, it works ! Thank you very much for the explanations, I better understand now
Answer: Assuming I want feature to remain where it is without moving it (as showing in the final chart), I would do it like this:
git checkout dev
git cherry-pick dev..feature

That's it
If you want to delete feature branch after that:
git branch -D feature

In general, a cleaner approach (but with more steps) is:
git checkout feature
git rebase dev
git checkout dev
git merge feature # (this fill fast-forward to the tip of feature)
git branch -d feature # no need to use -D this time

Comment: This answer is awesome. @bobier2 this is the correct answer, if you don't need feature, then `git branch -D feature` to delete it after cherry-pick.
Comment: My bad, I do not need to keep the feature branch. The final goal is to have the "final" dev branch containing all the commits


Proof: $x (y - x + 1) > y$ if $y > x$

Question: I'm working on a computation which depends on the idea that given two natural numbers $x$ and $y$ where $y > x$, the product $x(y - x)$ will always be greater than $y$.
Is there a proof of this ? My elementary math is a bit rusty.
The simple evaluation gives : $xy - x^2$
I can't seem to formalize this relation with respect to $y$. Could somebody give me a refresher on the proof strategies for such a problem ?
*EDIT: *
Apologies. I got lost in writing it here. It's actually $x (y - x + 1)$. So basically given two numbers this would result in the value above plus a summation series that is solvable using $x(x+1)/2$. Does that make more sense ?
(P.S: thank you for the quick response.)
Comment: Let $y = 2$ and $x = 1$. Then $y > x$, but $x (y - x) = 1 \cdot (2-1) = 1$ and $y = 2$. Your statement is wrong.
Answer: $$x(y-x+1)>y\iff xy-y>x(x-1)$$
$$\iff y(x-1)>x(x-1)\iff (y-x)(x-1)>0$$
which will be true if $(y>x$ and $x>1)$ or if $(y<x$ and $x<1)$
i.e.,  if $y>x>1$ or $y<x<1$
Comment: Yippee... I just saved a variable cause of this. Thanks.
Comment: @SaadFarooq, my pleasure.
Answer: Try $y=x+1$, then the product is $x$, which is not greater than $y$. Hence you try to prove something wrong.
Answer: Try $x=2$ and $y=3$. Then $x(y-x)=2<y$.
Answer: A different way to look at the problem:
$$x(y-x+1)=-x^2+(y+1)x$$
with fixed $y$ is a quadratic function in $x$. The coefficient by $x^2$ is negative, so the maximal value of it will be at $x=\frac{y+1}{2}$, which is smaller than $y$ if $y>1$, and the value is equal to $\frac{(y+1)^2}{4}$, which is clearly larger than $y$ for $y\geq 3$, for example.

Proof: $x (y - x + 1) > y$ if $y > x$


Extrapolating the effect of covariable changes in Cox proportional hazards models

Question: I have a Cox proportional hazards model in R (see made-up example below) that models the effect of some variable, say weight. From this model, I'd like to extrapolate what a change in weight from say 90 to 60 would mean to survival, taking into account the fact that for such a change occurring at say age 40, certain amount of risk has already accumulated (and assuming weight change is instantaneous).
I've attached some code which involves

fitting the Cox model (using age as the time scale);
extracting the predicted cumulative survival $S(t)$ using survfit for weight=90 and 60;
getting the cumulative hazard $H(t) = -\log(S(t))$;
getting the "instantaneous" hazard $h(t)$ via differencing $H(t)$ (plus small fudge factor to avoid zero hazard), which seems to do the job but probably a bit hacky;
adding a constant to the $\log(h(t))$ for all timepoints after the change, equivalent to the $\beta$ coefficient from the Cox regression times the difference in weights (90-60=30);
get the new survival functions $S^\prime(t)$ as $\exp(-{\rm cumsum}(\exp(\log(h^\prime(t)))))$.

This procedure produces reasonable results (plotted as $1 - S(t)$), but is it correct or am I just lucky?

library(survival)
set.seed(1)
rm(list=ls())

# Simulate some semi-realistic data
n      <- 1e3
age    <- round(runif(n, 1, 60))
weight <- round(rnorm(n, 70, 10))
height <- round(runif(n, 1.3, 1.9), 2)
sex    <- sample(c("M", "F"), length(age), replace=TRUE, prob=c(0.7, 0.3))
d.time <- ceiling(rexp(n, weight / 1e4))
cens   <- round(runif(n, 1, 60))
death  <- d.time <= cens
d.time <- pmin(d.time, cens)
d      <- data.frame(age=age, weight=weight, height=height, difftime=d.time,
                     time=d.time + age, sex=sex, death=death)

s     <- coxph(Surv(age, time, death) ~ height + weight, data=d)
d.new <- data.frame(weight=c(60, 90), height=1.7)
sf    <- survfit(s, d.new)

# The cumulative hazard function H is -log(S(t)) where S(t) is the survivor function
# (aka cumulative survival)
S <- sf$surv[,2]

# Assume we start off with high weight
H <- -log(S)

# The hazard is the derivative (here, finite difference) of the cumulative hazard H
# But the hazard can't be zero exactly as when we take log hazard, won't make sense
h <- diff(c(0, H)) + 1e-6

# We introduce a changepoint in the hazard, but must make sure that the
# hazard does not become negative - this is naturally achieved because the
# Cox model is linear in the log-hazard. This means that the final survivor
# function will always be monotonically decreasing for any value of delta in
# (-Inf, +Inf); delta > 0 increases hazard, delta < 0 decreases hazard
delta <- coef(s)["weight"] * (d.new$weight[1] - d.new$weight[2])
logh  <- log(h)
age   <- 40
logh[sf$time > age] <- logh[sf$time > age] + delta
h     <- exp(logh)

# Get the new cumulative hazard and new survivor functions
H <- cumsum(h)
S <- exp(-H)

# Compare original survivor function with modified one
plot(sf, lwd=5, col=1:2, conf.int=FALSE, mark=NA, fun="event",
     xlab="Age", ylab="Cumulative risk")
lines(c(0, sf$time), 1 - c(1, S), type="s", col=3, lwd=5)
abline(v=age, lty=2)
legend(x="topleft", legend=c("Weight=60", "Weight=90", "Weight decreased 90 to 60"),
       col=1:3, lwd=5)

Comment: What would be the purpose of the plot? To visualize the model in some way? Or to estimate survival functions?
Comment: @swmo The purpose is to do a counterfactual analysis: given that I've estimated the effect of weight on the log hazard scale to be $\beta$, what do I predict the effect on survival scale to be when I remove that effect at a given age (as opposed to generating two survival curves where a person has _always_ been at that weight, the red and black curves). I only have weight at baseline plus eventual outcome.
Comment: weight at baseline is at what age? Age at entry? Does entry occur at different ages? Right now, I find your plot a bit odd as there's a positive chance of dying before reaching 10 years of age - but kids don't weigh 90 kgs, meaning that they wouldn't die weighing 90 kgs. Could you in general provide more information on the data?
Comment: @swmo This is _totally simulated data_, just to illustrate the point, so no need to worry about kids weighing 90k etc. For the sake of the example, people enter at different ages (variable _age_) and their weight recorded at this baseline, and are followed up until their first event/censoring (variable _death_ and time of event _time_). Hope this makes sense.
Answer: I would suggest you do it non-parametrically. The procedure as you describe it imposes assumptions on the way the failure functions can relate to each other, basically because the Cox model introduces the assumption of proportional hazards. Therefore, I would argue that the red and black curves in the plot are a visualization of the model, more than they are estimates of failure functions. Not that those two things couldn't coincide, but why make this further assumption?
If you want to do something similar but non-parametrical, I would suggest using the Kaplan-Meier estimates instead. You would have to divide the weight variable into groups (assuming it's continuous), e.g. "low" and "high". You would still be able to do the counterfactual analysis that you want, simply by making a "conditional" KM plot similar to the green one above. So the green curve would be the KM of the "high" group until age $40$. At age $40$ the KM of the "low" kgs group (for $+40$ years) would continue, pasted onto the "high" ending at $40$. The KM estimate is the estimated probability of reaching age $t$, thus, for the hypothetical individual changing weight groups we can think of the probability of reaching age $40 + s$ as the probability of living from $40$ to $40 + s$ in the low weight group given survival until $40$ times the probability of living from $0$ to $40$ in the high weight group. This will exactly correspond to "pasting" the KM estimates together at age $40$. Note that the KM estimates themselves are products of conditional probabilities (conditional on survival until some time point). In symbols and if $X$ is a stochastic variable describing the time of failure of this hypothetical individual:
$$
P(X > 40 + s)  = P(X > 40 + s | X > 40)P(X > 40), \ s \geq 0.
$$
In conclusion, this amounts to the KM plot for "high" until age $40$ and at $40$ we use the conditional survival history of "low" (conditional on survival until $40$). To show it on a plot:

Some code to produce the plot, using built-in functions in R
library(ggplot2)
library(survMisc)
library(survival)

X1 <- rexp(n = 20)*50
X2 <- rexp(n = 20)*100

Sfit1 <- survfit(Surv(time = X1) ~ 1)
Sfit2 <- survfit(Surv(time = X2[X2 > 40]) ~ 1)

v  <- autoplot(Sfit1)$plot
p1 <- tail(v$data$surv[v$data$time < 40], 1)
t1 <- tail(v$data$time[v$data$time < 40], 1)

u <- autoplot(Sfit2)$plot
x <- c(t1, as.vector(u$data$time)[-1])

Sdata <- data.frame(x = x, y = p1*as.vector(u$data$surv), st = "2")

autoplot(Sfit1, title=NULL)$plot + geom_step(data=Sdata, aes(x=x, y=y, st=st))

However, one should probably still consider what the purpose of the plot really is. We're not really describing any of our subjects and it's not clear that we're describing a hypothetical (but plausible) subject either. You would want to remember that you're assuming that the hazard changes instantaneously, not only that the weight changes instantaneously. I'm no expert on human physiology, but a sudden weight loss probably entails other side-effects that are not appropriately modelled.
This is simulated data, but one should also keep in mind that the weight covariate is time-dependent, especially since we're also modelling young people and children. Treating it as time-independent is probably a bad idea. Also, the heavy people will be the ones that entered to study as adults as weight is measured at entry. The OP seems to be aware of this, though, but I thought I'd mention it anyway.
Comment: Note that the \$ for $\LaTeX$ markdown & the code markdown interfere w/ each other (see [here](http://meta.stats.stackexchange.com/a/2403/7290)).


db_query gives PDO Exception "General error: 1366 Incorrect integer value:" when I try to update table

Question: I'm trying to execute the following SQL query
$tweet_id_int = intval($tweet->id);
$result = db_query("INSERT INTO tweetsDB (handle,tweet,time_string,tweetid) VALUES     ('%v1','%v2','%v3','%v4')", array('%v1'=>$twitter_handle,'%v2'=>$tweet->text,'%v3'=>$tweet->created_at,'%v4'=> $tweet_id_int));

from inside a Drupal module and getting the error
    PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '%v4' for column 'TweetID' at row 1: INSERT INTO tweetsDB (handle,tweet,time_string,tweetid) VALUES ('%v1','%v2','%v3','%v4'); Array ( [%v1] => davecohen [%v2] => taking it easy - Rosh Hashana [%v3] => Fri Sep 14 08:06:08 +0000 2007 [%v4] => 267918272 )

handle, tweet and time_string are VARCHAR
tweetid is BIGINT
The "TweetID" is the value I received from calling the Twitter API earlier to fetch some tweets. Twitter API returns the Tweet ID as a string, so I called 'intval' before passing it to the SQL query. If I try running the same query into the MySQL database directly via the Sequel Pro console
 INSERT INTO tweetsDB (handle,tweet,time_string,tweetid) VALUES ('davecohen','taking it easy - Rosh Hashana','Fri Sep 14 08:06:08 +0000 2007','267918272')

Then I get no problems at all.
Any help here would be MUCH appreciated!
Answer: From the Drupal API on db_query:

Do not use this function for INSERT, UPDATE, or DELETE queries. Those should be handled via db_insert(), db_update() and db_delete() respectively.

The recommended method:
// Insert values returned from Twitter API.
$tweetsdb_id = db_insert('tweetsDB')
->fields(array(
  'handle' => $handle,
  'tweet' => $tweet,
  'time_string' =>$time_string,
  'tweetid' => $id,
))
->execute();


Condicionais para verificação

Question: Estou Criando um arquivo js com um jquery.
Neste arquivo preciso fazer uma condicional ou várias condicionais que verificam uma certa quantidade de números.
Se a variável x for um dos números 1,3,5,6,7,9,10, deve-se mostrar a palavra Sim, mas se os números forem 2,4,8,11,12,17, deve-se mostrar a palavra nâo.
Como posso fazer isso?
Comment: São exatamente esses números que vão mostrar "Sim" e "Não" ou tem algum significado por trás dos números? E também, por que usar jQuery para comparar números? Não poderia ser feito em javascript puro?
Answer: Use a função $.inArray() do jQuery

var sim = [1, 3, 5, 6, 7, 9, 10];
var nao = [2, 4, 8, 11, 12, 17];

var x = 2;

if ($.inArray(x, sim) != -1) {
  document.write('sim');
} else if ($.inArray(x, nao) != -1) {
  document.write('nao');
}

Answer: Pode usar o .indexOf(), não precisa de jQuery.
Se vai repetir esta verificação várias vezes pode fazer uma função para isto.
Sugiro guardar esses numeros em arrays:
var numerosA = [1,3,5,6,7,9,10];
var numerosB = [2,4,8,11,12,17];

e depois verificar o novo numero com esses:
numerosA.indexOf(1) != -1 // true
numerosA.indexOf(2) != -1 // false
numerosB.indexOf(1) != -1 // false
numerosB.indexOf(2) != -1 // true

O .indexOf() dá a posição desse numero dentro da array. Se o numero não existir na array vai dar -1.
Pode também usar um ternário para isto. Nesse caso podeia fazer:
var resultado = numerosA.indexOf(seuNumero) != -1 ? 'Sim' : 'Não';

Exemplo aplicado a uma função, neste exemplo dando uma terceira resposta para casos erróneos:

var numerosA = [1, 3, 5, 6, 7, 9, 10];
var numerosB = [2, 4, 8, 11, 12, 17];

function verificar(nr) {
    if (numerosA.indexOf(nr) != -1) return 'Sim!';
    else if (numerosB.indexOf(nr) != -1) return 'Não!';

    return 'Não existe em nenhuma...';
}

alert(verificar(1)); // Sim!
alert(verificar(2)); // Não!
alert(verificar(500)); // Não existe em nenhuma...
Comment: Boa resposta. Usar jQuery.inArray() pra uma coisa simples me pareceu matar formiga com bala de canhão.
Answer: Se você preferir um solução em JQuery, você pode usar $.inArray()
if ($.inArray(x, [1,3,5,6,7,9,10]) !== -1){
    console.log('Sim');
}else if($.inArray(x, [2,4,8,11,12,17]) !== -1){
    console.log('Não');
}

Condicionais para verificação


How to test for multicollinearity among non-linearly related independent variables

Question: I have a health outcome (measured as a rate of cases per 10,000 people in an administrative zone) that I'd like to associate with 15 independent variables (social, economic, and environmental measures of those same administrative zones) through some kind of model (I'm thinking a Poisson GLM or negative binomial if there's overdispersion). From scatterplot investigation I know there is multicollinearity among the variables that I need to investigate and deal with. I am not against removing variables, and within any problematic combinations can justify choosing one variable over the other(s) for reasons of interest or cost/ease of collection.
In the past I used the correlation matrix to detect multicollinearity, but I've been reading around on this site and discovered VIF and the condition index/number, which seem to be generally accepted as better options. My question is how any of these measures work with non-linear correlations, which is what I have between nearly all of my variables (determined graphically). What are my options for evaluating multicollinearity besides Spearman correlations?
Aside: I realize this is probably a separate question, but in case it's relevant here, I also have a lot of non-monotonic correlations among my variables that I don't know what to do with...
Thanks for any help!
Comment: Non-monotonic relationships (a better word here than "correlations") should be dealt with on their merits: e.g. try a quadratic if it looks quadratic. Note that such changes may increase the degree of multicollinearity.
Answer: Multicolinearity is all about the linear relationship among you independent/explanatory/right-hand-side/x-variables. That you want to use those variables in a non-linear model does not matter. The logic behind that is that if you want to add both variables to your model then you have te be able to distinguish between a unit change in one variable and a unit change in the other. If the variables are linearly related then a unit change in one coincides with $k$ units increase in the other variables, where $k$ is some constant, so we cannot determine the separate effects of both variables. If the relationship is non-linear a unit change in one variable coincides with a variable number of units change in the other, so we are able to distinguish between the variables. So if you graphically determined that there is a relationship but that relationship is non-linear then that fact alone has already solved most of your problems.
Consider the following example: if we add a quadratic curve, that is, we add a variable $x$ and a variable $x^2$ to our model, then the relationship between the variables $x$ and $x^2$ is extremely strong. Still we can estimate that model. The reason is that that relationship is non-linear.
I find it informative to see a situation where this can break. Consider we have a study where we want to consider year of birth, which ranges between 1950 and 1990. If we just add that and its square then you might get into trouble as the relationship between birthyear and birthyear$^2$ is almost linear, as you can see below. You can solve this by centering at a meaningful variable within the range of your data, e.g. 1960. As you can see in the second graph the relationship is now non-linear and that is usually enough to solve the problem.

I created that graph with Stata using the following code:
<code>twoway function xsquare = x^2, range(1950 1990) ///
    name(a,replace) title(uncentered) ytitle("x{sup:2}")
twoway function xsquare = (x-1960)^2, range(1950 1990) ///
    name(b, replace) title(centered) ytitle("(x-1960){sup:2}")
graph combine a b, ysize(3)
</code>
Comment: So the "linear" part of "multicollinearity" should have been a clue, huh? Thanks! Clear and helpful.
Comment: Overall this answer makes sense. However, how do you deal with the edge case in practice? If you have hundreds of features, that would require looking at tens of thousands of charts to determine where to recenter a feature. And recentering a feature like you're describing may not be feasible if you want to recenter in a way that addresses skew, such as min-max or IQR.
Answer: The best way to detect colinearity is not with correlations but with condition indexes. See, e.g. this thread for more detail.
Comment: I realize correlations aren't the way to go. I'm trying to figure out if the nonlinear relationships between my variables affect my ability to use tests like the condition index. Or, based on Maarten's answer, whether I even need to worry about collinearity if the relationships are non-linear.


IPTables causes conflict with Fail2Ban

Question: OS: Ubuntu Server 18.04
Fail2ban: v0.10.2
I am trying to understand a bug with my Fail2ban installation (I believe from my own making). I have a cron job that populates iptables with a predefined list using:
iptables -A INPUT -s <ip address> -j DROP
iptables -A INPUT -s <ip address> -j DROP
etc
etc

At the beginning of this script, it runs:
iptables -F INPUT to flush the INPUT chain.
This cron job was present on the server before I installed Fail2ban to dynamically ban bad actors. However, I noticed I started getting a lot of notices stating that a particular IP was already banned. When I stopped the iptables cron job from running, this error disappeared, and has been runnning without any issues whatsoever.
My question is: What sort of conflict am I creating by running iptables -F INPUT while Fail2ban is still running? Shouldn't this command be fine since banned IPs are placed into their respective f2b-<jail> chain, or am I misunderstanding? If this iptables script needs to run, should I also do something with the Fail2ban database (purge, etc)?
Thank you.
Answer: fail2ban must also place rules in the INPUT chain to call the chains it defines for each individual jail. By removing those, you are causing those rules to not apply, and the IP addresses that should be banned still get through, and fail2ban warns you about this by saying they were already banned.
You should not have any reason to flush the INPUT chain like this. Instead your cron job should create its own user-defined chain, and only add to the INPUT chain a rule to call that user-defined chain. Better yet, you should use an ipset, which has better performance and also can be atomically swapped when you want to change it. (And for that same reason you should have fail2ban use ipsets as well.)
Comment: fully correct answer! just a small amend: to be really concurrent safe if altering iptables one have to specify option -w (to wait for xtables lock) if supported (see man iptables), at least if INPUT chain gets modified/extended.
Comment: This is a fantastic answer. I'm coming in to a place where the former Sys Admin is retiring. The iptables script is old, so I will work on updating it. I'll have to do some research on how to get fail2ban to use ipset.

IPTables causes conflict with Fail2Ban


How to upgrade PHP version of wordpress site in FTP?

Question: Could you please guide me how to change PHP version of wordpress site in FTP?
I can only access into FTP via WinSCP.  Thank you.
Answer: You cannot update the PHP version of your hosting through the FTP. You can do it by accessing your cPanel or Plesk panel, if you do not have access to it I suggest you to contact your hosting provider.
Comment: @Maxim you need command line access or an admin system already set up that has that level of access, you cant do it via FTP or  wordpress admin. even with command line access you will need root permission.
Comment: It's in local server. not in CPanel such as Plesk. How do I access into it and upgrade PHP version and DB. Thanks a lot.
Comment: @Maxim how did you install php in the first place? a wamp stack? or ?
Comment: @tim It's old site launched on intranet. I asked host and she doesn't know about it. It's hard to trace without permission to seeing server for me. It's a pain actually. I can go into wordpress admin panel and FTP folder.
Comment: @tim Thanks a lot, Tim. I will try to deal with it.
Answer: You can modify PHP version of your FTP directory by adding .htaccess directive:
<FilesMatch ".php">
SetHandler "proxy:unix:/usr/local/phpXX-fpm/sockets/serwerXXXXX.sock|fcgi://localhost"
</FilesMatch>

it is working fine on lh.pl hosting.
phpXX is a version of PHP to configure and serwerXXXX is a server name.


Views to show all child terms without showing parent

Question: Vocabulary has 5 parents, each containing 10 child.
Through views I want to display all child terms in a block, but to hide/skip showing parent terms in result.
Creating view that displays both parents and child was easy, but this I cant figure out.
Comment: you mean you want only all the 50 children being displayed?
Comment: Yes, only children terms.
Answer: You can add a relationship with your parent term. After that you can set a filter with your parent term and filter all terms, which have a parent term id (parent taxonomy term: term id(not null)).