N balls distributed in N boxes

Question: This question is related to the following question:
n distinguishable balls into n boxes
Suppose that $n$ balls are distributed in $n$ boxes. I have two items
a) Given that box 1 is empty what is the probability that only one box is empty.
b) Given that only one box is empty what is the probability that this box is box 1.
If $A$ is the event "box 1 is empty" and  $B=$"only one box is empty" then we are looking for
$$
P(B|A)= \frac{P(A\cap B)}{P(A)}~~~\text{and}~~P(A|B)= \frac{P(A\cap B)}{P(B)}
$$
I have no idea how to describe the event $B=$"only one box is empty" in order to calculate the probability.
Can someone help me with this?
Parcial Solution: For item b) note that  $A\subset B $ so $A\cap B=A$ and it is possible to show that
$$
\#A= [(n-1)n!]/2n^n~~\text{and that}~~\#B= n(n-1)\binom{n}{2}(n-2)!
$$
therefore:
$$
P(A|B)=\dfrac{[(n-1)n!]/2n^n}{n(n-1)\binom{n}{2}(n-2)!}
$$
Is this ok!?
Comment: Notice that if exactly one box is empty, then there is an empty box, one box with two of the $n$ balls, and each of the other boxes must receive one ball.  Count how many ways this can occur.
Comment: Event $B$ is exactly one box is empty. The way you find this is by placing one ball in each box beforehand (other than one box, of course), and then letting balls go in whatever boxes they want.
Answer: For a, you are told that all the balls go into bins $2$ through $n$.  You are asking the chance that all bins $2$ through $n$ have at least one ball.  If we consider the balls labeled, there are $(n-1)^{n}$ ways to distribute the balls.  To have all the bins with at least one ball, there are ${n-1 }$ ways to select the bin with two balls, $n \choose 2$ ways to choose the balls in that bin, and $(n-2)!$ ways to arrange the other balls, so the chance is $$\frac{(n-1){n \choose 2}(n-2)!}{(n-1)^{n}}=\frac {n!(n-1)}{2(n-1)^{n}}=\frac {n!}{2(n-1)^{(n-1)}}$$
For b, the bins are equivalent, so it is $\frac 1n$
Comment: Can you explain better this claim:"You are asking the chance that all bins 2 through  have at least one ball. If we consider the balls labeled, there are (−1) ways to distribute the balls. "?
Comment: If we are given that bin $1$ is empty, each ball has $n-1$ bins it can go in.  You make $n$ such choices, so there are $(n-1)^n$ ways to put the balls in bins $2$ through $n$
Comment: My question is realy basic, I apologise in advance. I was thinking that if we have $n$ balls and $n-1$ bins to distrubute this balls then by the multiplicative principle we have $n^{n-1}$ ways to do that. however instead, you are distribution bins to the balls. I cant understanding why...
Comment: The first ball has $n-1$ bins it can go in.  So does the second, so there are $(n-1)^2$ ways to place the first two balls.  We keep going for $n$ balls and get $(n-1)^n$
Answer: ( PART A )
Each desirable outcome is a surjection from an n set to a n-1 set. The total number of outcomes is the number of functions from an n set to an n-1 set.
There are n-1 surjections ; since we know the remaining n-1 boxes are non-empty go ahead and put a ball in each one. Then we'd only have one ball left and n-1 boxes to choose from for that remaining ball.
(n-1)^n is the total number of functions from an n set to a n-1 set.
Therefore, the desired probability is (n-1)/[(n-1)^n] which equals (n-1)^(1-n).
(PART B)
No matter what box the probability it is the only empty box is the same. These events are obviously mutually exclusive and their probabilities sum to 1. Therefore, the desired probability is 1/n.
Comment: Your count of  functions assumes the balls are distinguishable.  Your count of the ways to distribute the balls assumes they are not.
Comment: @Ross. I took another look but I'm not quit sure what you mean. That is, the order of the balls in each box doesn't matter. For example, if Box A has balls 1,2,3 then you shouldn't overcount and count again Box A with 3,2,1 separately so that you count the same thing 6 times.
Comment: I'm not overcounting.  If you number the balls $1$ to $n$ you can place them in the bins in order, so $1,2,3$ into bin $1$ is only counted once.  I agree with your $(n-1)^n$ as the number of ways to distribute the balls into bins other than $1$.  The numerator should be much higher as shown in my answer.
Comment: Does the order in each bin matter to you in your count ?
Comment: No, I am just computing based on which balls end up in which bin.
Comment: @Ross. Thank you. I see where I undercounted. As you noted I should multiply by nC2 and (n -2) ! because the balls are distinguishable. Thank you.

N balls distributed in N boxes


Allow anonymous users to load site contact form without granting admin privileges

Question: I'm including the site contact form on a page with these 2 lines.
  module_load_include('inc', 'contact', 'contact.pages');
  print render(drupal_get_form('contact_site_form'));

The page loads as expected when a user is logged in, but for an anonymous user the page returns a 403.
If I give the "Administer contact forms and contact form settings" permission to anonymous users, then the page will load for anonymous users. I gave the "Use the site-wide contact form" permissions to anonymous users, but they still get an access denied error.
Is there a way to allow the form to load for anonymous uses without giving them admin privileges?
Comment: If you go to *domain.com/contact* as anonymous, can you see the form?
Comment: Just to be sure - if you disable all modules except contact (on a copy if possible), does it persist? And do [Contact form blocks](https://drupal.org/project/contact_form_blocks) solve your issue? And I second Rotem's question above.
Answer: The Access Denied error is returned because the following code executed by code_site_form(). (Notice the call to drupal_access_denied().)
  $limit = variable_get('contact_threshold_limit', 5);
  $window = variable_get('contact_threshold_window', 3600);
  if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms')) {
    drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($window))), 'error');
    drupal_access_denied();
    drupal_exit();
  }

Users with the "administer contact forms" permission don't get that error, since the limit for the sent messages doesn't apply to them.
Check those Drupal variables have not been set to different values that lower the number of allowed messages. The default is 5 messages in 1 hour.
As side note, render(drupal_get_form('contact_site_form')) would cause a "call-time pass-by-reference" warning in PHP 5.3, since render() is expecting a variable passed by reference, but drupal_get_form() doesn't return a reference. PHP 5.4 will raise a fatal error for that same code. The code calling render() should always use a variable.
module_load_include('inc', 'contact', 'contact.pages');
$form = drupal_get_form('contact_site_form');
print render($form);

Comment: Thanks for the clear answer and the bonus info. :) This solved my problem. The epilogue: I ended up needing form customization and swapped out the site wide form with a [Webform](https://drupal.org/project/webform).
Answer: The permission you should be looking at is "Use the site-wide contact form"
Comment: A good thought. I do have "Use the site-wide contact form" checked for anonymous users and still get these symptoms.


How to add column before or after existing column

Question: It looks to me that Postgresql deosn't support to add column before or after existing column, and it only adds the column as the last position. Often I need to add a column, but I don't want to add it at last, I have to drop the table and recreate the table, and insert the data that the table already has.
I would ask if there is an appoach to add a column at the desired position.
Comment: Not sure if it helps, but check [this](https://stackoverflow.com/a/285740). I'm curious about why you want it in a specific order.
Comment: View could be a good and fast solution for that.
Comment: @anupsabraham, good for you if you don't need this.
Comment: We have 4 columns, created_by, created_date, last_updated_by, last_updated_date.By convention, they are the last 4 columns, the other columns are business columns and before the 4 columns.
Comment: Why do you care? And Yes, you're right, it's not supported.
Comment: @FrankHeikens. OK, great.
Answer: Use the CREATE TABLE statement to create a new table with the specified column order and the new column.
CREATE TABLE new_table (
    id serial PRIMARY KEY,
    new_column datatype,
    old_column1 datatype,
    old_column2 datatype
);

Copy data from the old table to the new table using INSERT INTO
INSERT INTO new_table (id, new_column, old_column1, old_column2)
SELECT id, new_column, old_column1, old_column2 FROM old_table;

Give the new table the name of the old table, and rename the old table to a temporary name.
ALTER TABLE old_table RENAME TO old_table_temp;
ALTER TABLE new_table RENAME TO old_table;

Comment: Thanks, currently, I am using this approach, which can' t be more ugly.


DispatchTimer - Prevent the tick event to be triggered if the previous tick is still running

Question: In a Silverlight app, I have a block of code that has to run every 500ms. I am planning o use a DispatcherTimer to achieve this (see code below).
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds
dt.Tick += new EventHandler(dt_Tick);
dt.Start();

However, it may happen that the block of code takes longer than 500ms to execute (the block of code does some webservice calls). How do I make sure that if a call is currently in progress, the DispatcherTimer doesn't trigger another event? What are the options and what is the best way? Using locks?
Answer: The DispatcherTimer only runs on the dispatcher thread - so there's no way you could have two handlers running at the same time. It's possible they'll be queued up and run one directly after another, of course - you should check.
However, you shouldn't be making a web service call in a DispatcherTimer anyway. Do it in a background thread, otherwise you're blocking the UI for updating all the time that you're waiting for the web service. Basically you shouldn't do any long-running work in the UI thread. Use one of the various other timers (e.g. System.Timers.Timer) to regularly perform work on a thread pool thread and use the dispatcher to call back to the UI thread when you've got some data which needs to be displayed on the UI.
Of course, now you've got the potential problem of the new kind of timer firing multiple times concurrently, on multiple threads. One option to avoid this is to set the AutoReset property to false, and just schedule the next timer tick at the end of the current one.
Comment: I forgot to mention that I am using Silverlight for Windows Phone. I can't find System.Timers.Timer.
Comment: @Martin: Okay, so use System.Threading.Timer instead. Same basic principle :)
Comment: One last question Jon. If I use the System.Timers.Timer, is there a chance that two tick events can be running at the same time?
Answer: I would say you skip a tick if it takes too long, otherwise you will get a huge queue because of the lock.
So in the eventhandler say:
if(!busy) {
  busy = true;

  // some code which could take longer than 500 ms

  busy = false;
}

Answer: I don't know if DispatchTimer has any clever way to do this but what I would do in this situation is not to try to get the timer to not fire the event but to get the event to do nothing if it has not finished the previous run.
You can use locks to do this by getting a lock at the beginning of your event handler. If the lock is not available then exit the function (its already running) if you get the lock do the work and then once you've finished the work release the lock.
The method you want is Monitor.TryEnter and you'll want to make sure that you do your error trapping correctly as with any use of locks.
Answer: In order make the event run successfull without getting a call from your DispatcherTimer again with in the previous tick completes stop the dispatcher timer after entering in to dt_Tick event and at the end of the tick event call the start again which will initializes the IsEnabled of DispatcherTimer again to true.


Intercept a Print Job to add extra data

Question: Is it possible in Windows to intercept a Print Job parse the contents and add some extra data to the Print job?
Comment: Yes, you would write a device driver. Would you care to give any further insight as to your level of experience or what sort of extra data you want to add?
Comment: This is why I asked, because for a "generic desktop laser/inkjet printer" it would be printing postscript from the windows drivers. Those are different from the kinds of things printed by receipt printers (which are often serial streams of data and proprietarily formatted). I don't think you're going to be able to create a one size fits all printer driver for this, you're going to have to tailor it to each printer system you're going to work with. Additionally, there is a specific thing about creating "generic" systems. You have to write one or two and make sure where they overlap.
Comment: So what you need to do is to write the first one. Then write the second one. Then figure out how and where they overlap. There is no magic to this. Time to take a trip down the rabbit hole and see how far down it goes.
Comment: So you need to start with the type of printer you're going to be working with initially. That's step 1. Step 2 is finding out what software SDK they have available for this sort of thing. Step 3 is finding other printers you're likely to work with in the future.
Comment: I haven't done anything like this before. What I am trying to do is add a string to the receipt that gets printed from different 3rd systems.
Comment: Is this for a receipt printer?
Comment: It could be. I am trying to come up with a generic solution. One other thing that I forgot to mention was I might have to parse the text getting printed that will allow me to decide what the text needs to be..
Comment: Can you point me to some resources to write the first one? Thanks for your help..
Comment: Yes probably you are right as you can see I haven't done driver development at all. I have started looking at the WDK and will try to ask some more intelligent questions..
Answer: The AddJob function adds a print job to the list of print jobs that can be scheduled by the print spooler. The function retrieves the name of the file you can use to store the job.

Intercept a Print Job to add extra data


I'm new to HSMs, how are keys copied from one physical HSM to another?

Question: This scenario has come up in production, where they have to copy the keys generated in one HSM into other HSMs so that all the nodes in a cluster use the same keys. So if while copying, they will have to import right? So once keys are copied will the HSM have to be restarted or can it be done with out down time. The HSM is Thales HSM. This is all the info I have.
Comment: Thales is a large company that sells multiple HSM's that can be quite different (they were made by different organizations that got acquired or merged into Thales). So saying that the HSM is a Thales HSM is not saying much.
Answer: Following on from Pras' answer -- it is highly vendor dependent.  Keys might be able to be "backed up" and "restored" -- assuming two HSMs have the same LMK (or MBK or ... vendor dependent).
Backup/Restore is different from "export wrapped" and "import wrapped key".  Now, it isn't the LMK/MBK/... that determines whether it can be done.  Now, it is probably a local configuration policy, or a key's own policy, that determines whether it can be exported wrapped.
If you are the owner/operator of the two HSMs, and you have set them up with the same LMK/MBK/..., then in theory (assuming your HSM allows it!) you should be able to backup the key on the one, and restore it to the other.
Comment: Thanks for the explanation about how they are copied. This was really helpful.
Answer: HSM protects the keys by encrypting them with local master keys. Outside the HSM the keys are always accessed in the encrypted form under LMK
So you cant just move the encrypted keys form HSM1 to HSM2. First you need to figure out if both these HSM has same local master key
If they have same LMKs, the keys might be moved ie the keys from HSM1 can be used with HSM2.
If they have diffrent LMKs, you might have to export the keys from HSM1 into a transport key common across both the HSMs, then import them into HSM2.
Comment: So once the keys are copied will the HSM have to be restarted for the new keys to sync in or can it be used on the go without a restart?
Comment: And thank you for the detailed explanation involving the copying of keys!! Very very helpful


What is TCP-over-TCP and how does OpenVPN under TCP mode avoid the issue?

Question: This article explains why TCP-over-TCP could be a performance disaster.
My understanding about the issue is that the 'outer' TCP connection deals with packet loss and congestion of the network and acts accordingly by increasing timeouts (and thus reducing throughputs). However, the 'inner' TCP connection does not see these network conditions because they are 'fixed' by the outer TCP. And therefore, the 'inner' TCP keeps sending packets at previous speed and thus explodes the internal sending buffer of the 'outer' TCP connection.
My questions are:

Is my understanding correct?
It seems that the TCP-over-TCP meltdown is only internal (i.e., it only affects local buffers) but does it also affect the network as well? Does it cause more congestions in the network and does it degrade other connections on the same network?
How does TCP-based VPNs solve this problem? OpenVPN has an article on this but it does not say why it is not a problem in practice (or is it?)

Many thanks for any answer!
Comment: The OpenVPN article you linked to says: "We therefore instead recommend that you use UDP"
Comment: It does. You just have been very fortunate to not have noticed it yet.
Comment: Yes, but for some non technique reason I can't use UDP and OpenVPN over TCP works well for me. The question here is that why TCP-over-TCP doesn't have a noticeable effect on TCP based VPN?
Comment: [This](https://networkengineering.stackexchange.com/questions/76925/can-udp-traffic-be-transmitted-over-tcp-ports-on-openvpn-to-avoid-the-tcp-meltdo) discussion on Network Engineering might help.
Answer: In my understanding, the "tcp meltdown" problem is not difficult to solve: you only need to set a large retransmission timeout for the inner tcp connection.
By greatly increasing the minimum retransmission timeout of the inner TCP connection, we have effectively disabled the timeout retransmission mechanism of the inner TCP. Therefore, the TCP meltdown problem is avoided.
For example, in linux, you can use ip route replace 192.168.168.1/24 via 192.168.168.2 rto_min 12s to increase the minimum retransmission timeout of all internal connections established through OpenVPN from 0.2 seconds to 12 seconds (It is assumed that 192.168.168.1/24 is your OpenVPN network segment).
You can set the above command as OpenVPN's up event callback. In this way, we have actually avoided the tcp meltdown problem in a simple way.
We use this method to optimize the tcp-over-tcp link. Even on the line with high latency (hundreds of milliseconds) and high packet loss, we have not found any adverse effects.
PS: On a line with high latency, high packet loss, and high bandwidth, it is obvious that you need to prepare a large window for the inner tcp connections to take up the full bandwidth.
UPDATE:

The question here is that why TCP-over-TCP doesn't have a noticeable effect on TCP based VPN?

Because on a high-quality line that rarely loses packets, the TCP meltdown phenomenon is not prominent.
Comment: So, in other words. The inner TCP connection (of the application) does not need a retransmission because retransmission are already handled by the outer TCP connection (of the VPN tunnel). Thus, one can disable the retransmission of the inner TCP connection to avoid the TCP meltdown problem.
Answer: I would argue that this has more to do with the way that TCP works, and not with OpenVPN per se. Here comes a long rant and my few cents:

Is my understanding correct?

Roughly yes, but the inner TCP connection isn't "protected". If the outer drops a packet and the throughput gets lower or latency higher, the inner connection would also be limited by this, notice that it can't use full performance and start to back off.

It seems that the TCP-over-TCP meltdown is only internal (i.e., it only affects local buffers) but does it also affect the network as well?

You will only have one TCP connection to the server, so this will only affect that particular connection and whatever is in it. What the meltdown refers to is what I describe in the previous answer.

Does it cause more congestions in the network and does it degrade other connections on the same network?

No, but I would need to have "network" being defined here. If you have a bad connection to the Internet, then yes, all would suffers from packet drops or other transmission problems. If you mean there is only a problem with your client<=>server connection, then your non-VPN connections wouldn't be affected.

How does TCP-based VPNs solve this problem?

By using a single connection to the server, send the traffic inside that connection and hope for the best.

OpenVPN has an article on this but it does not say why it is not a problem in practice, is it a problem in practice.

Yes. TCP has much higher overhead in terms of packet size than UDP, so you are always paying a size penalty by having two TCP headers (inner plus outer) in your connection. Resends and TCP rampup will also impact your performance. If you have a good connection, i.e. no drops, low latency and high bandwidth, then you won't see much of the rampup/backoff/resends, and hence not notice this. If you have a bad connection, then the first answer comes in to play, the outer connections could ramp down, this will impact the inside traffic which will ramp down, packets can become out of order and being resent and so on, which will definitely impact the tunnel performance.
Long answer is long. I hope this made some sense to someone more than me.


How to superimpose two images and get SSIM (similarity index) value for these two images?

Question: I have a clean image, and a noisy image. I created a denoiser and applied it to the noisy image, that was my final output. Now to compare how much this image is close to a clean image I need to compare it using PSNR and SSIM, but due to different positions of the image I am unable to compare.
Now I am getting SSIM as 0.5, which is very low, due to the improper placement of both the images. If the images are registered properly, then I guess SSIM should come around 0.80+. But I have not been able to accomplish this.
How can I align these two images to obtain a good SSIM value?
I have two coin images, 1st image (CLEAN), 2nd image (IMPROVED a NOISY IMG), for comparison.
Clean Img:

Noisy Img:

Due to positions of images at different positions ssim(img1,img2) is giving incorrect output. I tried cropping but that did not work.
Here is what I have tried so far:
Attempt 1:
function [valPSNR,valSSIM,badpict]=getSSIM(clean_img,img2)
% pad reference image since object is so close to edges
refpict = padarray(mat2gray(clean_img),[20 20],'replicate','both');
% crop test image down to extract the object alone
badpict = imcrop(mat2gray(img2),[2.5 61.5 357 363]);
% maximize normalized cross-correlation to find offset
szb = size(badpict);
c = normxcorr2(badpict,refpict);
[idxy idxx] = find(c == max(c(:)));
osy = idxy-szb(1);
osx = idxx-szb(2);
% crop the reference pict to the ROI
refpict = refpict(osy:idxy-1,osx:idxx-1);
%imshow(imfuse(badpict,refpict,'checkerboard'));
%imagesc(badpict);
valSSIM=ssim(badpict,refpict);
valPSNR=getPSNR(badpict,refpict);
img2=badpict;
clean_img=refpict;
figure; imshowpair(clean_img,img2);
figure; montage({mat2gray(clean_img),mat2gray(img2)}, 'Size', [1 2], 'BackgroundColor', 'w', 'BorderSize', [2 2]);
end

Attempt 2:
function [valPSNR,valSSIM,badpict]=getSSIM2(clean_img,img2)
% pad reference image since object is so close to edges
bw1 = im2bw(mat2gray(clean_img));
bw2 = imclose(im2bw(mat2gray(img2),0.3),strel('disk',9));
bw2 = bwareafilt(bw2,1);
% make same size
[r,c] = find(bw1);
clean_img = clean_img(min(r):max(r),min(c):max(c));
[r,c] = find(bw2);
img2 = img2(min(r):max(r),min(c):max(c));
img2= imresize(img2, size(clean_img),'bilinear');
valPSNR=getPSNR(mat2gray(clean_img),mat2gray(img2));
valSSIM=ssim(mat2gray(clean_img),mat2gray(img2));
badpict=img2;
figure; imshowpair(clean_img,img2);
figure; montage({mat2gray(clean_img),mat2gray(img2)}, 'Size', [1 2], 'BackgroundColor', 'w', 'BorderSize', [2 2]);
end

Comment: https://uk.mathworks.com/discovery/image-registration.html
Comment: If you have an algorithm made by you that cleans the image and then puts it in a different location, that means that you know the mechanism that misaligns the two images. Just make sure you don't, or you correct it back using exactly the same method. Don't try to register the images, as that will always have some error.
Comment: My algo don't puts it in different location,  it already in different location
Comment: So you are comparing your denoised image to a different image that is clean? If that is the case you need to do some image registration
Comment: exactly, both are different images of a same coin, I'm trying from 3 days, I have done my research on algo generation and its complete to finally evaluate I need to validate it using ssim
Comment: But getting issue in this,  ERRORIMG: https://www.yogile.com/ecurfj6mxby/024455462l/share/?vsc=233c7df09
Comment: NOISY IMG: https://www.yogile.com/ecurfj6mxby/024455460l/share/?vsc=233c7df09
Comment: CLEAN IMG: https://www.yogile.com/ecurfj6mxby/024455458l/share/?vsc=233c7df09
Answer: As others have pointed out, the resampling required by registration will have some non-zero error. But, here is some sample code that will take you through the registration part that is the crux of your question.
% SSIM isn't defined on RGB images, convert to grayscale.
ref = rgb2gray(imread('https://i.stack.imgur.com/tPKEJ.png'));
X = rgb2gray(imread('https://i.stack.imgur.com/KmU4y.png'));

% The input image data has bright borders at the edges that create
% artifacts in resampling, best to just crop those or maybe there are
% aquisitions that don't have these borders?
X = X(3:end-2,3:end-2);
ref = ref(4:end-3,4:end-3);

figure
montage({X,ref});

tform = imregcorr(X,ref,"translation");

Xreg = imwarp(X,tform,OutputView=imref2d(size(ref)),SmoothEdges=true);

figure
imshowpair(Xreg,ref)

ssim(Xreg,ref)

Comment: my ultimate task is to get higher ssim value on superimposing, using above two code I'm getting higher ssim values around 50%
Answer: Maybe you can refer to my github.
I implemented a template matching algorithm by OpenCV which you can use NCC-Based Pattern Matching to find targets, and then get a score (similarity).
You can then use this score to decide if it is clean.
Besides, tranforming c++ code may be an issue for you, but just find the all corresponded function in matlab version.
Here are effects (red blocks are areas with similarity higher than threshold 0.85 in comparison with golden sample):

The whole function is too long to be posted here.
Part of the function:
for (int i = 0; i < iSize; i++)
{
    Mat matRotatedSrc, matR = getRotationMatrix2D (ptCenter, vecAngles[i], 1);
    Mat matResult;
    Point ptMaxLoc;
    double dValue, dMaxVal;
    double dRotate = clock ();
    Size sizeBest = GetBestRotationSize (vecMatSrcPyr[iTopLayer].size (), pTemplData->vecPyramid[iTopLayer].size (), vecAngles[i]);
    float fTranslationX = (sizeBest.width - 1) / 2.0f - ptCenter.x;
    float fTranslationY = (sizeBest.height - 1) / 2.0f - ptCenter.y;
    matR.at<double> (0, 2) += fTranslationX;
    matR.at<double> (1, 2) += fTranslationY;
    warpAffine (vecMatSrcPyr[iTopLayer], matRotatedSrc, matR, sizeBest);

    MatchTemplate (matRotatedSrc, pTemplData, matResult, iTopLayer);

    minMaxLoc (matResult, 0, &dMaxVal, 0, &ptMaxLoc);

    vecMatchParameter[i * (m_iMaxPos + MATCH_CANDIDATE_NUM)] = s_MatchParameter (Point2f (ptMaxLoc.x - fTranslationX, ptMaxLoc.y - fTranslationY), dMaxVal, vecAngles[i]);

    for (int j = 0; j < m_iMaxPos + MATCH_CANDIDATE_NUM - 1; j++)
    {
        ptMaxLoc = GetNextMaxLoc (matResult, ptMaxLoc, -1, pTemplData->vecPyramid[iTopLayer].cols, pTemplData->vecPyramid[iTopLayer].rows, dValue, m_dMaxOverlap);
        vecMatchParameter[i * (m_iMaxPos + MATCH_CANDIDATE_NUM) + j + 1] = s_MatchParameter (Point2f (ptMaxLoc.x - fTranslationX, ptMaxLoc.y - fTranslationY), dValue, vecAngles[i]);
    }
}
FilterWithScore (&vecMatchParameter, m_dScore-0.05*iTopLayer);


Change in Angle Between Vector and Positive x axis

Question: I have a vector in the $xy$ plane, $\vec{A}$, defined by $\|\vec{A}\|=8$, and $\theta=130^\circ$ with the positive $x$ axis. This gives the component form $\langle 8\cos130^\circ, 8\sin130^\circ, 0\rangle$. Now I need to find the spherical coordinates for $\vec{A}+3\hat{k}$, which are $\langle\|\vec{A}+3\hat{k}\|, \theta,\phi\rangle$. In order to evaluate $\theta$, I used the formula $$\theta=\cos^{-1}\left(\frac{A_x}{\|\vec{A}+3\hat{k}\|}\right)=\cos^{-1}\left(\frac{8\cos130^\circ}{\sqrt{(8\cos130^\circ)^2+(8\sin130^\circ)^2+3^2}}\right)\approx 127^\circ.$$ Why is it that I do not get the original $\theta=130^\circ$? Does the angle made with the positive $x$ axis change with the addition of a $z$ component?
Comment: because what you calculated is the angle in the plane made by axis $x$ and the "3D" vector, which is obviously different from the angle that its projection on $xy$ plane makes with $x$ axis.
Comment: @GCab For spherical coordinates, which angle is the correct $\theta$?
Comment: In spherical coordinates, as I know for standard, you have the angle with $x$ axis of the projection of the vector on the $x,y$ plane, the $\theta$ in your case, and then the angle that the vector makes with the $z$ axis (or depending on conventions, with its projection on the $x,y$ plane, in your case $arctan(3/8)$).
Answer: If $\theta$ in your spherical system is going to have the same meaning as $\theta$ in the polar coordinates, then it should be $130$. The change formula is
$$
\theta_{\rm spherical}=\arctan\left(\frac yx\right)=\arctan\left(\frac{8\sin 130}{8\cos 130}\right)=\arctan(\tan(130))=130.
$$


Put repeating codes in a variable in Lilypond

Question: I want to put some commands into a variable, so that I can write the variable instead of repeating the commands over and over:
myvar = \once \override Beam.beam-thickness = #tremBeamThickness \override Beam.gap = #3

This is throwing an error when I use it like \myvar in my ly file. What am I doing wrong?
Answer: Two \overrides are separate elements. Your code is an assignment myvar = \once \override Beam.beam-thickness = #tremBeamThickness followed by a spurious override \override Beam.gap = #3 that is at the top level, trying to make up an entire score by itself. You should put braces around them to unite them into a single "sequential expression" that you can then put into a variable, i.e.,
myvar = {
  \once \override ...
  \once \override ...
}


How to add unique class to Wordpress menu anchor?

Question: I have this function:
function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
    // you can put your if statements in here (use item, depth and args in conditions)
    $item_output = preg_replace('/<a /', '<a class="your-class" ', $item_output, 1);
    return $item_output;
}
add_filter('walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4);

Which is working fine, because it is setting a class on all my anchors in the menu. The problem is: it's setting the same class on all anchors. I want different classes per anchor. Is that possible? If so, how can I do this?
Edit --> I want to have it like this:

Comment: If all your classes are to be unique, use the `id` attribute instead: ``
Comment: I am using icon-fonts, so it has to be a class.... If I add a particular class, it adds an icon before the anchor, that's why I am using classes.
Answer: Assuming $item contains your values (bird, book, red, etc):
function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
    $item_output = preg_replace('/<a /', '<a class="item-' . $item . '"', $item_output, 1);
    return $item_output;
}
add_filter('walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4);

Comment: Thanks, but doesn't this add the same class on all of those anchors? Where do I select the second and third anchor to put a different class to?
Comment: To me it looks like `my_walker_nav_menu_start_el` is called for every item in a collection that's external to the code you've supplied. The function itself doesn't create all your menu items at once, but instead creates one per invocation.
Comment: That's true... But how can we make that happen? Can you help me with that? This is the only thing I have.
Answer: I used above code and make some changes and it's work for me , you can try with the below steps:
you can follow the below Steps :
Step 1 : In the wp-admin, go to Appearance -> Menus,put the class on the menu item here "CSS Classes (optional)". If you can't see the "CSS Classes (optional)" in the menu item, then there is "Screen Option" in the right top of the screen and there are the options "CSS Class" under "Show advanced menu properties".
Step 2: Under your them folder/function.php add below code :
 function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
    $class= $item->classes['0'];
    $item_output = preg_replace('/<a /', '<a class="'.$class.'"', $item_output, 1);
    return $item_output;
 }
add_filter('walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4);

Hope above code works for you , let me know if still it doesn't work.


System Center Endpoint Protection remote scan

Question: Could I use psexec to start a remote scan of a clients System Center Endpoint Protection ? I can't find any switches. Or Could I use System Center?
Answer: Yes, you can use whatever remote execution tool you want.
You want to run c:\program files\Microsoft Security Client\MPCmdRun.exe -Scan 0
Run it without any parameters to see all the options - you can choose what scan level (0 is default), force it to update antivirus signatures, gather files for support logs, etc.
Comment: Thanks. The only concern I have is that this .exe is for Windows defender right? Not system security end point? I need system security to run the scan please.
Comment: @benjaminjones it is part of SCEP.  The same exe is probably used in defender and security essentials because they are all based on the same code, but this is definitely part of endpoint protection.
Comment: Great thanks. Do you know how I can create a log file for the scan? .-getfile is not producing a log file.


fread() issue with Stream Context

Question: My application sends iOS notification. I use fread() to check if there was an error in the response from the Apple server. However, the code gets stuck in a loop or just keeps on loading.
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsCert = 'j_.pem';
$apnsPort = 2195;
$apnsPass = '';
$notification = "hey";

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $apnsPass);
$apns = stream_socket_client('ssl://'.$apnsHost.':'.$apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

$payload['aps'] = array('alert' => $notification, 'sound' => 'default','link'=>'https://google.com','content-available'=>"1");
$output = json_encode($payload);
$token = pack('H*', str_replace(' ', '', "device_token"));
$apnsMessage = chr(0).chr(0).chr(32).$token.chr(0).chr(strlen($output)).$output;
fwrite($apns, $apnsMessage);
$response = fread($apns,6);
fclose($apns);

What am I doing wrong?
Comment: And your question is...
Comment: My question , why the process doesn't get complete. It keeps processing .
Comment: I would throw in some debug lines to figure out how far it gets and where it stops/get stuck. When I use a PHP script that does not display on screen, I will send myself emails instead from different points in the script and then I can figure out more specifically what is happening.
Answer: You're most likely blocking on$response = fread($apns,6); as is explained in similar questions, on success no bytes are returned to be read so it'll sit there forever waiting for 6 bytes to read.
It's better to do like ApnsPHP has done in the past, and use select_stream() to determine if there is anything to read, before trying to read it. Try replacing $response = fread($apns,6); with:
$read = array($apns);
$null = NULL;
//wait a quarter second to see if $apns has something to read
$nChangedStreams = @stream_select($read, $null, $null, 0, 250000);
if ($nChangedStreams === false) {
    //ERROR: Unable to wait for a stream availability.
} else if ($nChangedStreams > 0) {
    //there is something to read, time to call fread
    $response = fread($apns,6);
    $response = unpack('Ccommand/Cstatus_code/Nidentifier', $response);
    //do something with $response like:
    if ($response['status_code'] == '8') { //8-Invalid token
        //delete token
    }
}

fread() issue with Stream Context


AppBarLayout with weird shadow at borders

Question: I need my AppBarLayout to have a transparent background.
When I change the background of the AppBarLayout to have @null or @android:color/transparent I got a weird shadow at the border of the Toolbar.
If I set the background as a color the toolbar looks normal.
This is my layout xml:
 <android.support.design.widget.AppBarLayout
      android:id="@+id/appBarLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#4999E2"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent">

      <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize">

        <ImageView
          android:id="@+id/imageViewLogo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="end|center_vertical"
          android:layout_marginEnd="16dp"
          android:importantForAccessibility="no"
          app:srcCompat="@drawable/ic_calendar_check"/>

      </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

AppBarLayout with a color as background:
(android:background="#4999E2")

AppBarLayout with a null background
(android:background="@null")

What I'm doing wrong?
Thanks!!
Answer: This happens, because AppBarLayout has a default stateListAnimator which applies elevation in a collapsed state.
If you want to get rid of this effect, do this
 <android.support.design.widget.AppBarLayout
    ...
    android:stateListAnimator="@null"
    ...
 />


classe mysqli não é encontrada

Question: O PHP está dando o seguinte erro: Fatal error: Uncaught Error: Class 'loja\model\mysqli' not found in, ele não esta encontrando a classe mysqli, mas eu já verifiquei o php.ini e não está comentado. Estou utilizando o autoLoad do composer, quando eu tiro esse o trecho do código que instancia o mysqli a classe funciona normalmente.
O que poderia ser isso, abaixo irei colocar os códigos
 namespace loja\model;
class Banco
{
    public function conectar()
    {

        $con = new mysqli("127.0.0.1","root","","ganha_tempo");

        if($con->connect_errno){
            echo "Erro ao se conectar ao banco de dados";
            mysqli_close();
        }
        return $con;
    }
}

classe UserSistema

  namespace loja\model\controleUsuariosSistemasModel;
 session_start();
 use loja\model\usuarioGeralModel\Usuarios;
 use loja\model\Banco;
class UserSistema extends Usuarios
 {
    private $unidade;
    private $senha;
public function setUnidade($unidade)

{
      $this->unidade = $unidade;
  }
public function getUnidade()
  {
      return $this->unidade;
  }
public function setSenha($senha)
  {
      $this->senha = $senha;
  }
public function getSenha()
  {
      return $this->senha;
  }
public function logarSistema($cpfUsuario,$senhaUsuario)
  {
    $banco = new Banco();
$con = $banco->conectar();

$resultado = $con->query("SELECT nome,senha,cpf,nome_unidade,nome_funcoes FROM usuarios_sistemas,unidade,funcoes WHERE cpf = '$cpfUsuario' AND senha = '$senhaUsuario' AND id_unidade = fk_unidade AND id_funcoes = fk_funcao");

$controle = false;

while ($consulta = $resultado->fetch_object())
{
   if($consulta->cpf == $cpfUsuario && $consulta->senha == $senhaUsuario)
   {
    $nomeUsuario = $consulta->nome;
    $_SESSION['usuario'] = $nomeUsuario;
    $_SESSION['cpf'] = $cpfUsuario;
    $_SESSION['unidade'] = $consulta->nome_unidade;
    $_SESSION['funcao'] = $consulta->nome_funcoes;
    $controle = true;
   }else{
    $controle = false;
   }
}
$con->close();
return $controle;

}
public function controleLogin($cpf)
  {
    if($cpf==null || $cpf=="")
    {
        header('LOCATION:../../../index.php');
    }
  }
public function restricaoAcesso($cpf,$moduloMenu)
  {
    $bd = new banco();
$con = $bd->conectaBD();

$resultado = $con->query("SELECT codigo_menu,modulo_menu FROM menu,usuarios_sistemas,acesso_menu WHERE cpf = fk_usuarios_sistemas AND id_menu = fk_menu AND cpf = $cpf");

while($consulta = $resultado->fetch_object()){
      if($consulta->modulo_menu == $moduloMenu){
        echo $consulta->codigo_menu;
      }
}
$con->close();

}
public function criptografaCpf($cpf)
{
    $cripto = base64_encode($cpf);

    return $cripto;
  }

 }

Comment: Isso é problema de escopo, pois o PHP está procurando a classe `loja\model\mysqli`, dentro do seu *namespace*. Adicione um `use mysqli` no início do seu arquivo para corrigir o escopo.
Answer: Utilize o "use":
namespace loja\model;

use mysqli;

class Banco
{
    public function conectar()
    {

    $con = new mysqli("127.0.0.1","root","","ganha_tempo");

    if($con->connect_errno){
           echo "Erro ao se conectar ao banco de dados";
            mysqli_close();
        }

        return $con;
    }
}

Ou a barra "\":
namespace loja\model;

class Banco
{
    public function conectar()
    {

    $con = new \mysqli("127.0.0.1","root","","ganha_tempo");

    if($con->connect_errno){
           echo "Erro ao se conectar ao banco de dados";
            mysqli_close();
        }

        return $con;
    }
}

Comment: muito obrigado, resolveu o problema.

classe mysqli não é encontrada


Conditional Inner jopin logic

Question: I currently have a table as (Sample snapshot):
Table1
Employee ID Old Value      New Value         Update Date
1           Analyst        non-employee     10/1/2012
1           non-employee   Senior Analyst   10/1/2012
2           Analyst        non-employee      9/1/2012
2           non-employee   Assistant Mgr.    9/1/2012
2           Assistant Mgr. non-employee     10/1/2015
2           non-employee   Manager          10/1/2015
3           Analyst        non-employee     10/1/2015
3           non-member     Manager          10/1/2015
4           Analyst        non-employee      9/1/2012
4           non-employee   Senior Analyst    9/1/2012
4           Senior Analyst non-employee     10/1/2015
4           non-employee   Assistant Mgr.   10/1/2015
4           Assistant Mgr. non-employee     10/6/2015
4           non-employee   Manager          10/6/2015

From this table, I need to pick out those employee IDs that transitioned from analyst to manager directly,i.e. without becoming a assistant manager or senior analyst.
Desired Result:
Employee ID Old Value   New Value   Update Date
3           Analyst     Manager     10/1/2015

I have been trying to figure out a logic to use conditional inner join to get the desired result.. But I have had no luck so far. Any help would be much appreciated!
Comment: How do you distinguish the ordering of values where the dates are the same?
Comment: Hi Gordon, thanks for the response and I apologize for the date issue. I corrected the snapshot data. Whenever a person's title changes, it goes to non-employee first and then to the new title. I hope this clarifies your doubt and apology again for the incorrect data.
Answer: If I understand correctly, then this query might work:
select t.id, t.oldvalue, tnext.newvalue, t.updatedate
from table t join
     table tnext
     on t.employeeid = tnext.employeeid and
        t.updatedate = tnext.updatedate and
        t.newvalue = 'non-employee' and
        tnext.oldvalue = 'non-employee'
where t.oldvalue = 'Analyst' and tnext.newvalue = 'Manager';

It should work on your sample data but (4) is troublesome because there are four changes on the same date.
Comment: Hi Gordon,
I am using access to run the query.Hence I am using INNER JOIN syntax to run the query. I get the error as JOIN Expression not supported for the part t.[New Value] = 'Non-Employee'. I was reading through other similar questions and was wondering if I should use Union function?
Comment: @HimanshuAgrawal . . . Your question is tagged MySQL.  If you want an answer with a different database, you should tag the question correctly.
Comment: Thanks for the suggestion Gordon. Will keep that in mind for future.

Conditional Inner jopin logic


Is there better alternative to Princeton Companion to Mathematics, because I can't make sense of it?

Question: I am currently reading first chapter of this book, and here are few quotes from the book and I can't make sense of these,they are -
1.) Historically, the abstract structures emerged as generalizations from concrete instances. For instance, there are important analogies between the set of all integers and the set of all polynomials with rational (for example) coefficients, which are brought out by the fact that they are both examples of algebraic structures known as Euclidean domains. If one has a good understanding of Euclidean domains, one can apply this understanding to integers and polynomials.
2.) This highlights a contrast that appears in many branches of mathematics, namely the distinction between general, abstract statements and particular, concrete ones. One algebraist might be thinking about groups, say, in order to understand a particular rather complicated group of symmetries, while another might be interested in the general theory of groups on the grounds that they are a fundamental class of mathematical objects.
3.)A supreme example of a theorem of the first kind is the insolubility of the quintic [V.24]—the result that there is no formula for the roots of a quintic polynomial in terms of its coefficients. One proves this theorem by analyzing symmetries associated with the roots of a polynomial, and understanding the group that is formed by them. This concrete example of a group (or rather, class of groups, one for each polynomial) played a very important part in the development of the abstract theory of groups.
What I mean by "I can't make sense of it" is I can't comprehend what author wants reader to understand,I have read these few times and I don't know how to link symmetries with roots of polynomial or how to apply Euclidean domains to integers and polynomials and I don't know what are groups of symmetries? So the problem is I can't understand what author wants its readers to grasp.
So what should I do?
Do you know a better alternative to this book? or Should I read this book differently? If yes,then how?
Basically, I want a book for broad overview of mathematics.
Comment: You have asked a question that is primarily opinion based, and such questions are often closed. Suggest you ask for clarification of one or two of your items. Maybe explanations will change your mind. Most important, if people want to guide you to their preferred sources for explanation, they will not hesitate to do so.
Answer: A brief word from Timothy Gowers (Preface, 'Who is The Companion aimed at?'):

If you have embarked on a university-level mathematics course, you may find that you are presented with a great deal of difficult and unfamiliar material without having much idea why it is important and where it is all going. Then you can use The Companion to provide yourself with some perspective on the subject. (For example, many more people know what a ring is than can give a good reason for caring about rings. But there are very good reasons, which you can read about in RINGS, IDEALS AND MODULES [III.81] and ALGEBRAIC NUMBERS [IV.1].)

How I use the book: It may be the case that you are approaching the book as if you want to study it, or read it straight through, from front to back. I don't believe this is how the book was intended to be used; at least, it's not how I use it. Rather, when I feel like looking at the book again:

I go to the contents page first;
then I choose a topic that sounds interesting at the time;
then, I try to read those pages.

I'll start at the beginning of that section, and continue reading; if I run into difficulties understanding some paragraph, the first thing I check is "if I skip this paragraph, can I still make some sense of most of the rest of the exposition?" I definitely do not read the book like a textbook. If a section concerns mathematics that I don't understand, then I have two options:

learn about that mathematics (I very rarely go for this);
read some other section of the book (this is much easier, and if I wanted my reading time to be difficult then I would have chosen a book by Serge Lang rather than The Companion).

The Princeton Companion to Mathematics is just that: a companion. The point of the book is to be something to "dip into" every now and then during your studies. The point is not to actually learn a whole lot of new mathematics, but rather to come to understand the motivations of a subject, or to experience the general flavour of something.
Why use the book this way?
The Companion is not really designed to teach mathematics in any kind of formal sense; yes, there are sections introducing some very technical concepts, and there is a section at the beginning of the book devoted solely to explaining the mathematical definitions that you'll definitely need to know to understand all of the rest of the book. However, there are two important things to take note of:

those "Fundamental Mathematical Definitions" cannot be learned all at once, and are best learned from other sources. A key thing to note is that there are no exercises, and mathematics is learned by doing!
there is no reason to expect to come to understand "the whole of the rest of the book" all at once. Rather, it is probably much better to "dip in" every now and then, and come to understand as much as you can section-by-section.

What does this mean for you?
By the sound of it, you aren't acquainted with a lot of higher mathematics (you mention that you don't know about groups of symmetries). That's perfectly fine, and it's also fine that you're reading The Companion. However, you cannot expect a book like The Companion to do everything; it has good explanations on a lot of complicated topics, but understanding any particular section requires a certain level of prior understanding of mathematics. With that in mind, I recommend that you read the book just as I do (there are large swaths of the "Fundamental Definitions" section that I haven't properly looked into yet), but with the understanding that it's okay if you have to stop reading at some point, or skip a few paragraphs that involve lots of strange symbols.
If you're after a book that has a different intention, i.e., if you want a book that, for example, has exercises, and that you can really learn mathematics from, then that's another matter.
Answer: For me, the Companion serves the purpose of increasing the range of math I'm interested in. There's a ton of really cool math out there, but only a very small amount of it is directly related to the kind of math I'm doing research in, and this is the case for the vast majority of mathematicians. That means it's easy for me to lose sight of all the really cool math happening outside my own field - especially since there are very few good descriptions of advanced topics which are readable by outsiders (even mathematically trained outsiders).
The Companion helps me find awesome things in math that I would otherwise not run across. The way I use it is:

Flip to a random article.
If I can't understand it, or I just don't find it interesting: flip to another article.
If I hit on an article that really grabs me: wait, where did the afternoon go? I had things I was supposed to do, darn it! On the plus side, generating functions are pretty sweet.

Now, like any book, the Companion isn't for everyone. It sounds like right now, you won't find the Companion super useful, because it assumes some background you don't have yet. One book that fills a similar role (or at least, did for me) is Ian Stewarts From Here to Infinity; this was one of the books that got me seriously interested in mathematics. Unlike the Companion, by the way, it can be read front-to-back.
Answer: The Princeton Companion is a quite wonderful book: but it is for those already embarked on a university level maths course (as is made clear from the quotation given by @WillR). And perhaps we should remember that the editor is a professor at Cambridge, and the university level maths courses that he and a number of the contributors are most familiar are the Cambridge tripos, or equivalents -- among the tougher courses on planet earth. So in fact, by many standards, the book is not really for those recently embarked on their courses but rather for those already a year or two into their university studies. For such readers, however, the book is an unbeatable resource. But you do indeed need to bring something to the party by way of "mathematical maturity" if you are going to get out of it all you could.
To return then to the OP's question, is there a lower-level alternative? Not really, as far as I know. Though there are some fine introductory invitations to university mathematics (one of Ian Stewart's books is mentioned in another answer). And in a different vein, it is worth remarking that actually, Wikipedia entries on topics at entry level (although of course variable in quality and detail) can be very good.
Answer: Well, you can't expect to learn Algebra from a few paragraphs talking about the general philosophy of mathematics. If you knew "how to link symmetries with roots of polynomial" or "how to apply euclidean domains to integers and polynomials" and "what are groups of symmetries" without studying Galois Theory, Ring Theory and Group Theory first, then you'd be a prodigy!
I have never read this book, but from those paragraphs alone, it seems that the author is giving a broad overview of what mathematics is, they are not actually trying to teach you mathematics.


How do I configure all Binaries to Publish to the File System in SDL Tridion 2011?

Question: I am using DD4T on an SDL Tridion project and am using the following configuration in the storage config in order to publish Binaries (binaries in this case being anything store in Multimedia Components) to the filesystem but keep Pages in the Content Delivery database.
I am finding that as requirements change for what Binary files are needed e.g. the customer wants to offer Adobe Illustrator files for download, I am needing to add more types to the list by changing the config and restarting the deployer which is not ideal.
<ItemTypes defaultStorageId="defaultdb" cached="false">
    <Item typeMapping="Page" cached="false" storageId="defaultFile"/>

    <Publication Id="9" defaultStorageId="defaultdb" cached="true">
        <Item typeMapping="Binary" itemExtension=".jpg" storageId="defaultFile" cached="true"/>
        <Item typeMapping="Binary" itemExtension=".gif" storageId="defaultFile" cached="true"/>
        <Item typeMapping="Binary" itemExtension=".pdf" storageId="defaultFile" cached="true"/>
        <Item typeMapping="Binary" itemExtension=".doc" storageId="defaultFile" cached="true"/>
        <Item typeMapping="Binary" itemExtension=".docx" storageId="defaultFile" cached="true"/>
        <Item typeMapping="Binary" itemExtension=".xls" storageId="defaultFile" cached="true"/>
        <Item typeMapping="Binary" itemExtension=".xlsx" storageId="defaultFile" cached="true"/>
        <Item typeMapping="Binary" itemExtension=".xlsm" storageId="defaultFile" cached="true"/>
        <Item typeMapping="Binary" itemExtension=".jpeg" storageId="defaultFile" cached="true"/>
        <Item typeMapping="Binary" itemExtension=".png" storageId="defaultFile" cached="true"/>
        <Item typeMapping="Binary" itemExtension=".ico" storageId="defaultFile" cached="true"/>
    </Publication>
</ItemTypes>

Can anyone tell me if there is a way to change the above so that the contents of all Multimedia Components for the given publication go to the filesystem? I can't seem to find this in the documentation.
Answer: As far as I know, the less you specify, the less you filter. Have you tried omitting the itemExtension attribute altogether? I would expect this then to apply to any extension.
Answer: We use the following in our configuration and it picks up any new binary file types we add without require a change to the storage configuration:
<Item typeMapping="Binary" storageId="defaultStaticFile" cached="false"/>

You would just need to update to use your storage end point and desired cache setting:
<Item typeMapping="Binary" storageId="defaultFile" cached="true"/>

Answer: Just strip out the itemExtension node, and it will deploy all binaries to that storage id. The extensions are really only for exceptions.


Hyper-V Ubuntu 10.04, Filesystem suddenly becomes Read-Only?

Question: We are running a Ubuntu 10.04 VM  on a Hyper-V system,
The VM is dedicated to running one of our web applications.
We have enabled the Hyper-V drivers in /etc/initramfs-tools/modules like so:
hv_vmbus
hv_storvsc
hv_blkvsc
hv_netvsc

And updated the kernel image like so:
$ update-initramfs -u

And all was good... until..
This morning i got a support request that our web application was throwing an error 500, so i checked the logs and nothing was there.
Then I remembered that I had seen this on another of our ubuntu servers so I...
$ touch foo.txt

And my suspicions were confirmed:
touch:  cannot touch `foo.txt':  Read-only file system

Why is the filesystem randomly becoming readonly?
Is this only in Ubuntu on HV? Is it a problem on RedHat or Cent?
Answer: Can you check the log?  Most likely the filesystem had errors, and went into read-only state because of it.
(Assuming you're using ext3 or ext4, as are Ubuntu's defaults:) You can adjust this behavior with the errors mount option.
errors=remount-ro is the default; errors=continue (to stay read-write at your own peril) and errors=panic (stop, drop, and roll crash) are your other options.
Answer: This is a Hyper-V  issue , hosts running Red Hat or Centos will have the same problems
Comment: On what do you base this conclusion? Supporting documentation would be very helpful.


how the server identifies the viewstate for the button control is change

Question: Hi whether the submit button click event information is stored in _viewstate property(in page source) or not. if yes, how the server identifies the button is clicked from the viewstate and perform the corresponding event handler.
Comment: Duplicate of [How the server side identifies button click event](http://stackoverflow.com/questions/10054227/how-the-server-side-identifies-button-click-event)
Answer: For button click event, it happens in two ways.
a. If javascript is available & UseSubmitBehavior is set to false, then it populates __EventTarget hidden field with the Unique id of the button. When the page is processing the postback, it looks for this __EventTarget in the Request.Forms and if available, does a FindControl for the corresponding value and type casts into IPostBackEventHandler and invokes RaisePostBackEvent method. The button class will then trigger the Click event.
b. If Javascript is not available & UseSubmitBehavior is set to true, then the browser sends the unique id of the button to the server as Form variables. The Page if it finds the UniqueId as the key in Request.Forms then it would again find the control using FindControl method and casts into IPostBackEventHandler and invokes RaisePostBackEvent method. The button class will then trigger the Click event.
Comment: Hi, thank you for your reply it is correctly matched, what is the unique id, whether the uniqueid is created only for the clicked button control or all other controls in the page also.. please reply me as soon as possible
Comment: Unique ID is created for all the server side controls while rendering the page. It is an ID which can be used for finding a control at server side. This is unique for each control.
Comment: hi thanks this is my interview question, if i have the one textbox with two submit buttons with id button1, button2 then i click the button1 how does the server knows button1 is clicked, does the uniqueid for button1 only send to the server, what about button2, please help me,, if any tutorial available please send me the link along with your response.
Comment: Please look at the .NET source and understand.


Change UID for watchdog message

Question: In Drupal 8 with DbLog logger, I am trying to log some audit messages to a different user than the currently logged-in user. Unfortunately, it looks like something is overwriting my context.
I tried \Drupal::logger("proreos")->info("bla", ['uid' => 5]); and \Drupal::logger("proreos")->info("bla", ['user' => User::load(5)]);, but neither of them worked. In both the cases, the current user ID was instead used.
How can I use a different user ID?
To clarify: This is not about logging another uid, it is about assigning the row in watchdog to a different user.
Answer: Logger will always log for the current user, except you provide your own logging service.
The context is used to replace placeholders in the message, see here, so to get the userid in the message, use this:
\Drupal::logger("proreos")->info("bla @uid", array('@uid' => 5));

Answer: The correct code is the following one.
\Drupal::logger("proreos")->info("bla %uid", ['%uid' => 5]);

This is similar to what Drupal does in user_cancel().
  if (!$account) {
    drupal_set_message(t('The user account %id does not exist.', array('%id' => $uid)), 'error');
    \Drupal::logger('user')->error('Attempted to cancel non-existing user account: %id.', array('%id' => $uid));
    return;
  }

Consider the first and the second argument of error() or info() as the first two arguments of t().
Comment: The user ID in the watchdog table will still be of the current user.
Answer: Thanks to you all. I appreciate that watchdog is not the correct table for my needs. I have created my own audit table and logging service now.

Change UID for watchdog message


como devo fazer para o código python que fiz para loteria não repetisse os números no mesmo quando coloco 5 par e 1 impar para dar o resultado do jogo

Question: from random import randint

n = int(input('Número par: '))
b = int(input('Número impar: '))
quant = int(input('quantos grupo são: '))

def method_name():
    def sorteia(lista):
        cont = 0
        c = 0
        while True:
            num = randint(1, 61)
            if num % 2 == 0 not in lista:
                lista.append(num)
                cont += 1
            if cont >= n:
                break
        while tuple:
            num = randint(1,60)
            if num % 2 == 1 not in lista:
                lista.append(num)
                c += 1
            if c >= b:
                break
        lista.sort()
        jogos.append(lista[:])
        lista.clear()

    lista = list()
    jogos = list()
    sorteia(lista)
    for i, l in enumerate(jogos):
        print(f'jogos {i + 1} : {l}')
    return i, jogos, l, lista, sorteia

i, jogos, l, lista, sorteia = method_name()

Comment: Para que serve a variável `quant`?
Answer: Pensando que você possui os números fixados de 1 até 60. Acredito que criar o conjunto de pares e o conjunto de ímpares antes, poupa muito trabalho
import random

pares = range(2, 61, 2)
impares = range(1, 61, 2)

qtd_par = int(input('Número par: '))
qtd_impar = int(input('Número impar: '))

pares_sorteados = random.sample(pares, qtd_par)
impares_sorteados = random.sample(impares, qtd_impar)

sorteados = pares_sorteados + impares_sorteados
sorteados.sort()

print(sorteados)

Caso você escolha 5 pares e 1 ímpar a saída será algo como o exemplo abaixo:
[4, 10, 16, 20, 39, 60]

Comment: O brigado por mim ajudar
Answer: Pelo que entendi você deseja gerar grupos de números que serão utilizados como palpites em jogos lotéricos, onde se deve especificar a quantidade de valores pares e, também, de valores ímpares.
Pois bem, em primeiro lugar observei que seu range é range(1, 61). Com esta informação deduzo que o jogo lotérico esperado é Mega Sena. Dessa forma a MENOR QUANTIDADE DE NÚMEROS à ser utilizada em cada aposta (grupo) deste jogo é 6. Neste caso, teremos de sortear 6 números que formarão cada grupo.
Outra coisa importante é saber que para realizar sorteios sem repetições precisamos utilizar o método sample da bilbioteca random. Além disso, devemos ter consciência que como a quantidade de números sorteados por grupo serão sempre 6, a quantidade de números ímpares sempre será a diferença entre 6 e a quantidade de números pares. Por isso, especificando a quantidade de números pares obteremos automaticamente a quantidade de números ímpares, isto é, não precisamos capturar por input() a quantidade de números ímpares.
Além disso devemos tratar a quantidade de pares e grupos. Existem diversas formas de realizar tal tratamento. Nesta questão vou utilizar um recurso que está disponível a partir do Python 3.8, que se chama Assignmente Expression.
Com todas estas observações poderemos implementar o seguinte código:
def loterias(p, g):
    for j in range(1, g + 1):
        yield sorted(sample(range(2, 61, 2), p) + sample(range(1, 61, 2), 6 - p))

if __name__ == '__main__':
    while (par := int(input('Quantidade de pares entre "1" e "6": '))) < 1 or par > 6:
        print('Valor INVÁLIDO!')

    while (grupo := int(input('Quantidade de grupos (valor maior que 0): '))) < 1:
        print('Valor INVÁLIDO!')

    resposta = loterias(par, grupo)

    for r in resposta:
        for m in r:
            print(f'{m:02}', end=' ')
        print()

Observe que quando executamos este código nos é solicitada a quantidade de valores pares que participarão de cada grupo sorteado. Esta quantidade deve ser um valor entre 1 e 6. Enquanto o valor digitado para esta quantidade for menor que 1 ou maior que 6, receberemos a mensagem Valor IINVÁLIDO! e nos é novamente solicitado o valor.
Em seguida, nos é solicitada a quantidade de grupos que serão formados. Esta quantidade deve ser maior que 0. Enquanto o valor digitado para esta quantidade for menor que 1, receberemos a mensagem Valor IINVÁLIDO! e nos é novamente solicitado o valor.
Posteriormente estas informações serão enviadas, como parâmetros, para a função loterias(). Chegando lá serão sorteados a quantidade de números pares e, também, a quantidade de números ímpares - que seria (6 - quantidade de números pares). Posteriormente, estes valores serão adicionados à uma lista, sendo os mesmos ordenados de forma crescente com a ajuda da função sorted().
Depois dessas operações, com ajuda do yield será montada uma função geradora. Em seguida, o bloco for irá exibir os resultados em ordem crescente, devidamente formatados, com dois dígitos - completando com 0 o valor faltante à esquerda.
Testando o código
Imagine que queremos sortear 4 grupos, onde cada um deles deve ter 5 valores pares e 1 valor ímpar. Para isso devemos executar o programa e quando for exibida a mensagem: Quantidade de pares: , devemos digitar:
5

...e quando for exibida a mensagem: quantidade de grupos:  devemos digitar:
4

Em seguida, o programa calculará os resultados e 4 grupos, de 6 números, com 5 números pares e 1 número ímpar.
Como o sorteio é realizado de forma aleatória, uma das possíveis saídas pode ser:
12 24 29 30 52 60
12 13 18 36 50 56
05 14 18 24 54 60
05 06 18 30 32 58

Observe que o resultado nos mostrou 4 grupos, com 6 valores, onde 5 deles são pares e 1 deles é ímpar, e todos organizados em ordem crescente e com dois dígitos.


Getting next line in a file

Question: I am reading in a file and wonder if there's a way to read the next line in a for loop?
I am currently reading the file like this:
file = open(input,"r").read()
for line in file.splitlines():
  line = doSomething()

So is there anyway I can retrieve the next line of the file in that for loop such that I can perform some operation in the doSomething() function?
Answer: Just loop over the open file:
infile = open(input,"r")
for line in infile:
    line = doSomething(line, next(infile))

Because you now use the file as an iterator, you can call the next() function on the infile variable at any time to retrieve an extra line.
Two extra tips:

Don't call your variable file; it masks the built-in file type object in python. I named it infile instead.
You can use the open file as a context manager with the with statement. It'll close the file for you automatically when done:
with open(input,"r") as infile:
    for line in infile:
        line = doSomething(line, next(infile))

Comment: @Martijin: A small query: Using `next()` will move the pointer to the next line, but when the control comes back to the for loop, will it again read the "next" line or pass on to the next-to-next line?
Comment: @MartijnPieters: Thanks a lot. So if i have to read every line in a file and process it depending on the next one, i cannot use it. Right? Just curious (and learning).
Comment: @Konstant: You'd have to use some kind of push-back buffer, yes. Load the next line, push it back into the buffer when done.
Comment: @user5359531: `next()` always gives you the next item from an iterator. You can insert an [`itertools.islice()` object](https://docs.python.org/2/library/itertools.html#itertools.islice) if you need to skip items and use the next item after that: `next(islice(infile, 2, 3))` skips to, then produces the 3rd.
Comment: this is better than my indexing solution as it actually increments the counter as well...
Comment: @Konstant: Next to next. Using a `for` loop calls `.next()` as well, and every time you do that a *new* line is returned until the file is done.
Comment: @Martijn: Good advice regarding not naming a variable `file`, but (as I'm sure you know) it also applies to `input`.
Comment: Thanks for this answer. Is there a way to make `next(infile)` skip ahead more than one place? For example, can I get the 'next' `next(infile)` to move the iterator two places ahead, or an arbitrary number of places ahead? I tried `next(next(infile))` but that did not work.
Answer: file = open(input,"r").read()
lines =  file.read().splitlines()
for i in range(len(lines)):
     line = lines[i]
     next_line = lines[i+1]

Comment: why -1? ... this does what was asked ... Martjin's answer is better granted but this gives you next line (at least now it does .. was missing read() )...
Comment: how do you close the file in this case?
Comment: file.close() ... or understand python garbage collection... or understand that opening a file for reading does not lock the file so it doesnt really matter in general ... you could just as easily use a `with open(...) as f` context if you are really worried about it
Answer: I think that you mean that if you are in line n, you want to be able to access line n+1.
The simplest way to do that is to replace
for line in file.splitlines():
with
lines = file.readlines()
for i in xrange(len(lines)):

then you can get the current line with  lines[i]  and the next line with  lines[i+1]
the more pythonic way is to use enumerate
lines = file.readlines()
for index, line in enumerate(lines):

now you have the current line in "line" like normal, but you also have the index if you want to find a different line relative to it.
Comment: Martin posted his answer while I was fighting with the editor. His is a little more svelte, but it's still worthwhile to be aware of the enumerate option.
Comment: Coming across this answer in 2016 it bears pointing out that 'xrange' does not exist in Python 3 and 'range' should be used instead.
Comment: Doesn't this approach read the entire file into memory, in contrast to the method that uses the file as an iterator (`for line in infile:`)? That may be a trivial difference to some, but could matter quite a lot if you are dealing with very large files. The iterator method is inherently more efficient.

Getting next line in a file


Two statistics called equivalenet

Question:

Here is some step that I did. However, the hint from my professor is that drop the condition $\mathcal{P}-a.s$ and just try to establish the implication as(ie, "everywhere"). You can do this by constructing the functions $f$ and $g$. How could I construct the two functions for it? could some give ideas?
Answer: $(\Leftarrow)$ Suppose that there exist functions f, g, such that T(X)=f(T*(X)) and T*(X)=g(T(X)) as. Then if you have $T(X)=T(Y)$, applying g to both sides gives you $T*(X)=T*(Y)$ as. If you have $T*(X)=T*(Y)$, applying f to both sides gives you $T(X)=T(Y)$ as.
$(\Rightarrow)$ Suppose $T(X)=T(Y) \Leftrightarrow T*(X)=T*(Y)$. This means $T(X)=T(Y)\Rightarrow T*(X)=T*(Y)$. Construct a function g such that g(T(X))=T*(X), e.g. maps T(X) to T*(X) where X is the observation. Also suppose $T*(X)=T*(Y)\Rightarrow T(X)=T(Y)$. Construct a function f such that f(T*(X))=T(X), e.g. maps T*(X) to T(X) for every observation X. So f and g have been constructed as.

Two statistics called equivalenet


Interpretation of insignificant interaction term in regression

Question: I know there are other posts very similar to this, but I think I am asking a slightly different question.
Say my model is:
$Income= \beta_0 + treatment\,\beta_1 + Z\,\beta_2 + Z*treatment\,\beta_3 + \epsilon$
Where I want to know the effect of treatment  on income, and suspect it may vary with some other variable $Z$. Define the short regression as:
$Income =\alpha_0 + treatment\,\alpha_1 +\eta$
Now say in the short regression, $\hat{\alpha_1}= 10$. and in the interaction model, say $\beta_2$ is small and statistically insignificant, but $\hat{\beta_1} \approx \hat{\alpha_1}$. and both are significant.
Since the for the first regression, $\beta_1$ is:
$\frac{\partial E[income|treatment,z=0]}{\partial treatment}$, is this then saying that the effect of treatment only exists for subsets where z = 0? or are these just saying the effect of treatment does not vary with Z? i.e. can I say since the coefficients on treatment across models are about equal, this suggests z does not influence the effect of treatment on income?
does it make sense to argue from the first model alone, that we cant reject $\beta_1 = \beta_1 + Z\beta_3$ for any value of z, that this means treatment does not vary with z, vs interpreting this as treatment only applies when z = 0?
Comment: I think you are caught up with terminologies and notations. 1) You're doing post-experiment analysis, so adding a covariate doesn't "turn off" your treatment. 2) when you fit the model, all data points are considered in your design matrix, and the treatment values of data points with Z=1 will affect your coefficient estimate for treatment. The expectation notation only means "imagine if that data point had Z=0" for all data points.
Answer: The "long" model is:
$$E[y \vert t,z]=\beta_0 + \beta_1 \cdot t + \beta_2 \cdot z + \beta_3 \cdot z \times t$$
Here, the marginal effect of treatment is a function of $z$:
$$ME_t=\frac{\partial E[y \vert t,z]}{\partial t}=\beta_1 + \beta_3 \cdot z=f(z)$$
You can ask how does the $ME_t$ vary with $z$, which you can get by
$$\frac{\partial MT_t}{\partial z}=\frac{\partial E[y \vert t,z]}{\partial z \partial t}=\beta_3$$
The size, sign, and significance of $\beta_3$ tell you whether there is a substantive heterogeneous treatment effect that depends on $z$. The coefficient of $\beta_2$ is not the one you care about.
You are right that $\beta_1$ gives the expected effect for someone with a $z$ of zero. This is usually not a very relevant number unless $z$ has been rescaled or zero is a typical value. But this effect exists for everyone, not just for those with $z$ at zero. If $z \ne 0$, there is an additional effect on top of the direct one.
The "short" model is:
$$E[y \vert t,z]=\tilde \beta_0 + \tilde\beta_1 \cdot t + 0 \cdot z + 0 \cdot z \times t = \alpha_0 + \alpha_1 \cdot t$$
There are two things different here. One is that you dropped $z$ from the model. That's probably inconsequential, assuming treatment does not depend on $z$. The second is that you've imposed that the interaction is zero. This has more bite.
The most comparable number to $\alpha_1$ is the average marginal effect from the "long" model:
$$AME_t = \frac{1}{n}\sum_i^n \left( \beta_1 + \beta_3 \cdot z_i \right)= \beta_1 +
\beta_3 \cdot \frac{1}{n}\sum_i^n z_i=\beta_1 + \beta_3 \cdot \bar z$$
In the linear case, you can think of this as the effect of treatment for someone with the sample average $z$.
In the "short" model, the $ME_t=AME_t$, since the effect doesn't depend on $z$ and is the same for everyone.
I don't think you need the short model on top of the long one to decide if there is heterogeneity and comparing them is not all that useful. The long model already gives you everything you need.
I can answer your second question with a counterexample. Here I have simulated an effect that is positive for high values of $z$ and negative for low values, but the net effect is that these cancel and make the treatment look ineffective on average. Let's simulate the data and fit the "long" model first:
<code>. clear

. set obs 1000
Number of observations (_N) was 0, now 1,000.

. gen t = mod(_n,2)

. gen z = mod(_n,3) - 1

. gen y = 3 + 0*t + 1*z + 5*t*z + rnormal(0,2)

. reg y i.t##c.z

      Source |       SS           df       MS      Number of obs   =     1,000
-------------+----------------------------------   F(3, 996)       =   1012.18
       Model |  12206.1146         3  4068.70487   Prob > F        =    0.0000
    Residual |  4003.68069       996  4.01975973   R-squared       =    0.7530
-------------+----------------------------------   Adj R-squared   =    0.7523
       Total |  16209.7953       999  16.2260213   Root MSE        =    2.0049

------------------------------------------------------------------------------
           y | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
         1.t |   .1225485   .1268035     0.97   0.334    -.1262842    .3713813
           z |   1.076669     .10987     9.80   0.000     .8610661    1.292273
             |
       t#c.z |
          1  |   4.880441   .1553797    31.41   0.000     4.575532     5.18535
             |
       _cons |   2.894459   .0896636    32.28   0.000     2.718508    3.070411
------------------------------------------------------------------------------
</code>
This is the average marginal effect:
<code>. margins, dydx(t)

Average marginal effects                                 Number of obs = 1,000
Model VCE: OLS

Expression: Linear prediction, predict()
dy/dx wrt:  1.t

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
         1.t |   .1225485   .1268035     0.97   0.334    -.1262842    .3713813
------------------------------------------------------------------------------
Note: dy/dx for factor levels is the discrete change from the base level.
</code>
Here we cannot reject the null that the overall effect is nil.
Here $z$ has three possible values:
<code>. tab z

          z |      Freq.     Percent        Cum.
------------+-----------------------------------
         -1 |        333       33.30       33.30
          0 |        334       33.40       66.70
          1 |        333       33.30      100.00
------------+-----------------------------------
      Total |      1,000      100.00
</code>
We can calculate the effect at each one of them:
<code>. margins, dydx(t) at(z = (-1 0 1))

Conditional marginal effects                             Number of obs = 1,000
Model VCE: OLS

Expression: Linear prediction, predict()
dy/dx wrt:  1.t
1._at: z = -1
2._at: z =  0
3._at: z =  1

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
0.t          |  (base outcome)
-------------+----------------------------------------------------------------
1.t          |
         _at |
          1  |  -4.757893   .2005542   -23.72   0.000     -5.15145   -4.364335
          2  |   .1225485   .1268035     0.97   0.334    -.1262842    .3713813
          3  |    5.00299   .2005542    24.95   0.000     4.609433    5.396547
------------------------------------------------------------------------------
Note: dy/dx for factor levels is the discrete change from the base level.
</code>
The effects are significant at the extremes but of opposite sign, so the treatment looks ineffective on average.
To sum up

the interaction coefficient is the canary in the coalmine of effect heterogeneity.
If you add interaction coefficients, don't just average them away. Plotting the effects can help
The long model is all you need.

Stata Code
<code>cls
clear
set obs 1000
gen t = mod(_n,2)
gen z = mod(_n,3) - 1
gen y = 3 + 0*t + 1*z + 5*t*z + rnormal(0,2)
reg y i.t##c.z
margins, dydx(t)
tab z
margins, dydx(t) at(z = (-1 0 1))
</code>


Programmatically add a file upload field to all webforms (+more file extensions)

Question: I have a set of >100 webforms in my projects.
I need to add an upload field to all webforms in my hook_form_alter().
I guess I need to do something like the following.
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form_') === 0) {
    $form['file'] = array(
      '#type' => 'file',
      '#title' => t('File'),
      '#weight' => 50,
    );
  }
}

I need to set as on certain attributes such as:

fieldkey
max upload size
allowed file extensions

How can this be achieved?
Comment: No answers yet? :). The field can upload: doc, xls, pdf, jpg mais pas des formats comme xlsx, docx.
Answer: In this way (hook_form_alter) you will have this field in existing and next created webforms, and you must have own additional _validate and _submit function to process the $form['file'] field.
Other way, the question is how to add programmatically file upload component to existing webform?
To see structure of webform file component, add manually field file to one existing webform and make your setting from the picture, and see devel output tab (enable devel module). Also you may be interested to see http://api.drupalize.me/api/drupal/function/webform_component_insert/7. Then create update script to loop webforms.


Code can not read file

Question: When i tried to run this program, it only show "Beginning of Read-in records list. But the program doesnt terminate, which means the program keep running, but it doesnt output anything. Could anyone please help?
public static void readFile(String fileName, PhoneBook book_list)
{
    if(fileName == null) return;
    File file = new File(fileName);
    if(!file.isFile())
    {
        System.out.println("File not found!");
        return ;
    }

    Person person = new Person();
    boolean invalid = false;

    System.out.println("\nInfo: Beginning of Read-in records list.\n");

    try
    {
        BufferedReader input = new BufferedReader(new FileReader(file));
        try
        {
            String line = "";
            line = input.readLine();

            while(line != null)
            {
                line = line.trim();
                String[] words = line.split("\\s+");

                if(invalid)
                {
                    if(words[0].equals(""))
                    {
                        person = new Person();
                        invalid = false;
                    }
                }
                //refer to end of a record
                else if(words[0].equals(""))
                {
                    if(person.validation())
                    {
                        book_list.addPerson(person);
                        person = new Person();
                    }
                    else
                    {
                        person = new Person();
                    }
                }

                if(words[0].equalsIgnoreCase("name"))
                {
                    if(words.length<2)
                    {
                        invalid=true;
                    }
                    else
                    {
                        String name = words[1];
                        for(int i=2; i<words.length;i++)
                        {
                            name = name + " " + words[i];
                        }
                        for(int i=0; i<name.length(); i++)
                        {
                            if((name.codePointAt(i) >= 97 && name.codePointAt(i) <= 122)/*a-z*/
                                    || (name.codePointAt(i) >= 65 && name.codePointAt(i) <= 90)/*A-z*/
                                    || name.codePointAt(i) == 32)/*space*/
                            {continue;}

                            else
                            {
                                invalid=true;
                                break;
                            }
                        }
                        if(!invalid)
                        {
                            person.setName(name);
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("birthday"))
                {
                    if(words.length !=2)
                    {
                        invalid=true;
                    }

                    else
                    {
                        try
                        {
                            person.setBirthday(words[1]);
                            book_list.addPerson(person);
                        }
                        catch (ParseException e)
                        {
                            invalid = true;
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("phone"))
                {
                    if(words.length != 2)
                    {
                        invalid = true;
                    }

                    else
                    {
                        String phone = Tools.parsePhone(words[1]);
                        if(phone!=null)
                        {
                            person.setPhone(phone);
                        }
                        else
                        {
                            invalid = true;
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("email"))
                {
                    if(words.length != 2)
                    {
                        invalid = true;
                    }
                    else
                    {
                        if(Tools.validateEmail(words[1]))
                        {
                            person.setEmail(words[1]);
                        }
                        else
                        {
                            invalid = true;
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("address"))
                {
                    String address = line.substring(words[0].length()).trim();

                    String addr="";

                    do
                    {
                        line = input.readLine();
                        if(line == null)
                        {
                            person.setAddress(address);
                        }
                        if(!invalid && person.validation())
                        {
                            book_list.addPerson(person);
                        }
                        addr = line.trim();
                        String[] adds = addr.split("\\s+");

                        if(!adds[0].equals("")
                                && !adds[0].equalsIgnoreCase("name")
                                && !adds[0].equalsIgnoreCase("birthday")
                                && !adds[0].equalsIgnoreCase("phone")
                                && !adds[0].equalsIgnoreCase("email")
                                && !adds[0].equalsIgnoreCase("address"))
                        {
                            address = address + " " + addr;
                        }

                        else break;
                    }
                    while(true);

                    if(line == null)
                        break;
                }

            }// end of while loop
        }

        finally
        {
            input.close();
        }
    }
    catch(IOException ex)
    {
        System.err.print("Error: Open records file failed");
        return;
    }
    System.out.println("\nInfo: End of Read-in records list.\n");
    return;
}

public static void main(String[] args)
{
    String personFile = null; // person contact information file
    String instFile = null; //instruction file
    String outputFile = null; //output file name;
    String reportFile = null; //report file name;

    PhoneBook book_list = new PhoneBook();
    FileIO2.readFile("C:/Users/phoenix/Desktop/sample_phonebook1.txt",  book_list);

    ArrayList<Person> a;
    a = book_list.getPersonList();

    System.out.println(a.size());

}

Comment: Have you used the debugger to see what's going on?
Comment: Uhm, is that a 200 lines long static method? I think you should redesign and refactor your code. If you did that properly you could spot the problem in no time. **[This book](http://www.e-reading-lib.org/bookreader.php/134601/Clean_Code_-_A_Handbook_of_Agile_Software_Craftsmanship.pdf)** *could help you a lot*. BTW you are only reading a line **before the loop** and when encountering address (which you won't as you don't read that far).
Answer: I think your problem is here:
// initializes "line" String
String line = "";
// Tells BufferedReader to read one line (the first line)
line = input.readLine();
// loops infinitely since String read from first line is not null in this specific case
while(line != null)
{

...
Try this:
// initializes "line" String
String line = "";
// tells the BufferedReader to read a new line _until_ the new line is null
// ... if new line is null we reached EOF
while ((line = input.readLine() != null) {

...
Comment: Thank you so much. How can i give u like a reward or appreication. cuz i just use this Stack Overflow today.
Comment: Omg, omg..How could u figure it out so fast~~Could u tell me what exactly the difference? I think they are same.
Comment: Well :) Editing and commenting my answer above now...
Comment: "tells the BufferedReader to read a new line _until_ the new line is not null" - Actually, until the new line *is* null, or in other words *while* the new line is not null. :)
Comment: You grammar troll XD - re-editing my post to comply with proper English
Comment: @Mena Hahaha! :) No offense, but it was (could have been) a littlebit misleading for beginners.
Comment: @Powerslave just kidding :) You were totally right to point that out.
Comment: Since English is not my mother tone, i might not speak very authentic English. Em, actually, i'm doing a Java Assignment, but it's real hard for a beginner like me. Although, i can read the file, thank you again, i still came across some problems. This takes me lots of time, i gonna be crazy..
Comment: English is not my mother tongue either. But it gets easy. Java gets easy. Courage and perseverance. And don't go crazy :D
Answer: You should do:
String line = null;
while ((line = input.readLine()) != null) {
     ...
     ...
}

Instead of:
String line = "";
line = input.readLine();

while(line != null){
     ...
     ...
}

Keep reading files. Cheers! :P
Answer: You have used  while & do..while loop..
And you are not reading file properly
after do..while you should include line = input.readLine()

Code can not read file