rewrite interview questions
Top rewrite frequently asked interview questions
I want to rewrite the contents of a file.
What I have thought of so far is this:
- Save the file name
- Delete the existing file
- Create a new empty file with the same name
- Write the desired content to the empty file
Is this the best way? Or is there a more direct way, that is, not having to delete and create files, but simply change the content?
Source: (StackOverflow)
I just read amending a single file in a past commit in git but unfortunately the accepted solution 'reorders' the commits, which is not what I want. So here's my question:
Every now and then, I notice a bug in my code while working on an (unrelated) feature. A quick git blame
then reveals that the bug has been introduced a few commits ago (I commit quite a lot, so usually it's not the most recent commit which introduced the bug). At this point, I usually do this:
git stash # temporarily put my work aside
git rebase -i <bad_commit>~1 # rebase one step before the bad commit
# mark broken commit for editing
vim <affected_sources> # fix the bug
git add <affected_sources> # stage fixes
git commit -C <bad_commit> # commit fixes using same log message as before
git rebase --continue # base all later changes onto this
However, this happens so often that the above sequence is getting annoying. Especially the 'interactive rebase' is boring. Is there any shortcut to the above sequence, which lets me amend an arbitrary commit in the past with the staged changes? I'm perfectly aware that this changes the history, but I'm doing mistakes so often that I'd really love to have something like
vim <affected_sources> # fix bug
git add -p <affected_sources> # Mark my 'fixup' hungs for staging
git fixup <bad_commit> # amend the specified commit with staged changes,
# rebase any successors of bad commit on rewritten
# commit.
Maybe a smart script which can rewrite commits using plumbing tools or so?
Source: (StackOverflow)
I see the Nginx HttpRewriteModule documentation has an example to rewrite a www-prefixed domain to a non-www-prefixed domain:
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains '/foo', not 'www.mydomain.com/foo'
}
How can I do the reverse-- rewrite a non-www-prefixed domain to a www-prefixed domain? I thought maybe I could do something like the following but Nginx doesn't like the nested if statement.
if ($host !~* ^www\.) { # check if host doesn't start with www.
if ($host ~* ([a-z0-9]+\.[a-z0-9]+)) { # check host is of the form xxx.xxx (i.e. no subdomain)
set $host_with_www www.$1;
rewrite ^(.*)$ http://$host_with_www$1 permanent;
}
}
Also I wanted this to work for any domain name without explicitly telling Nginx to rewrite domain1.com -> www.domain1.com, domain2.com -> www.domain2.com, etc. since I have a large number of domains to rewrite.
Source: (StackOverflow)
I've got some automatic emails that are sent out upon signup completion for my site.
Until recently, they worked fine. Now Google's new system is rewriting the images and storing them in it's cache (supposedly)
However, Google's new rewriting of my image links are completely breaking them, giving a 500 error and a broken link image.
Lets say my normal image url is:
http://www.mysite.com/images/pic1.jpg
Google is rewriting this to:
https://ci5.googleusercontent.com/proxy/vI79kajdUGm6Wk-fjyicDLjZbCB1w9NfkoZ-zQFOB2OpJ1ILmSvfvHmE56r72us5mIuIXCFiO3V8rgkZOjfhghTH0R07BbcQy5g=s0-d-e1-ft#http://www.mysite.com/images/pic1.jpg
However, there is nothing at that URL.

So, either there is something wrong with the links that are being created by Google or the images are just not being uploaded to the googleusercontent server, but I have no idea how to solve the issue.
Im using PHP, the phpmailer library and a Ubuntu server on Amazon EC2, but Im not sure that is related to the issue.
Source: (StackOverflow)
i'm trying to optimize my 'location' directives and cannot find a good way of determining if a specific location match is even attempted. "echo" inside the location block doesn't help here.
this documentation is somewhat confusing.
http://wiki.nginx.org/HttpCoreModule#location
To use regular expressions, you must use a prefix:
- "~" for case sensitive matching
- "~*" for case insensitive matching
Then how the match is performed:
- Directives with the "=" prefix that match the query exactly. If found, searching stops.
- All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
- Regular expressions, in the order they are defined in the configuration file.
- If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.
number 2 here says "conventional strings" but then says it can be used with the "^~" prefix. doesn't "~" imply a regex? if not, how does it determine what is an isn't a regex?
specifically i want the following:
- serve anything out of literal /assets directly. STOP SEARCH.
- serve anything matching regex \.php$|/$ via fast-cgi STOP SEARCH.
- serve everything else directly via literal /
this way there is only a / match attempt for non-dynamic files served from outside of assets.
i have
location ^~ /assets {} # search-terminating literal? or regex?
location ~ \.php$|/$ {}
location / {} # is this match always attempted?
from the document, it looks as though the actual order would be 1-3-2, always running the literal / match. yes, this optimization wont make any diff to real performance, but just want to clear up some ambiguity.
thanks!
Source: (StackOverflow)
I am very unfamiliar with nginx, as a forewarning, and also can't find any actual references on the regex system they use. So right now it's a black box to me.
All I want to do is redirect a user trying to go to www.mydomain.com/mydirectory/X to www.myotherdomain.com/X .
Seems like I should be using the rewrite command but the syntax of the regex is eluding me.
Thanks in advance.
Source: (StackOverflow)
I'm very much against rewriting an application if it can be avoided. I understand the rule that 9 times out of 10, it's better to refactor, but I'm in a situation where it might be the one time in ten, and I'm looking to find that line.
The current situation is:
- I took over the maintenance of a VB6/SQL application.
- The total lines of code is 75-100k (code-behinds, modules, and classes).
- The original developer left, so it's just me, and there's no opportunity to expand the team, at least for a few years.
- There was no architecture to the program (just straight SQL calls in plain text in the form code-behinds).
- Doesn't try to follow the DRY or OAOO principles.
- Database had some primary keys, but no foreign keys.
- Before this system was in place, everything was managed in big spreadsheets, so this system really is a huge improvement over what they had, but doesn't do what they're envisioning.
- I was able to write myself some tools to replace all literal instances of table names and column names with constants and look-ups, and I wrote a quick code gen script to generate those constants and look-ups from the database, so now I can safely make database changes and see everywhere that broke. I have started normalizing the database "around the edges", but it's like 3% of the way there.
- There are no unit tests, so every time I change the database, I basically have to rewrite whatever logic sits on top of it, and I use the two versions to compare functionality and make sure it's the same. So far so good.
- I started by just trying to fix critical bugs to stop the bleeding, and I can safely say that's mostly done, so now I'm stepping back for a moment to look at the big picture.
- Management is supportive and reasonable in their expectations.
- The long-term goal is to convert it to .NET anyway...
So, I'm weighing these options:
- Continue normalizing the database and modifying the VB6 app as I go (ends up being a piece by piece rewrite)
- Put the VB6 one into a maintenance-only state (no new features), pick one functional module at a time and rewrite that part in .NET on top of a normalized database structure.
My thought is that if I choose option 1, then at the end I just have a VB6 app that they still want to upgrade to .NET, and I've looked into that and it's costly and time consuming, and even with the tools you'll still get something that's somewhat of a Frankenstein. If I go with option 2, I believe I can be done sooner, and I'll jump right to the target technology.
In the small scale pieces that I've already rewritten during my normalization process, the result has been an improved module over what was already there, so there is value being added during the rewrite.
The existing app, for all its flaws, is a great talking point for discussion. The people using it can tell me what's working for them and what isn't, so there's certainly a lot of value there that way.
So, does this qualify as one of those "one in ten" times, or not?
Source: (StackOverflow)
I found this Perl script while migrating my SQLite database to mysql
I was wondering (since I don't know Perl) how could one rewrite this in Python?
Bonus points for the shortest (code) answer :)
edit: sorry I meant shortest code, not strictly shortest answer
#! /usr/bin/perl
while ($line = <>){
if (($line !~ /BEGIN TRANSACTION/) && ($line !~ /COMMIT/) && ($line !~ /sqlite_sequence/) && ($line !~ /CREATE UNIQUE INDEX/)){
if ($line =~ /CREATE TABLE \"([a-z_]*)\"(.*)/){
$name = $1;
$sub = $2;
$sub =~ s/\"//g; #"
$line = "DROP TABLE IF EXISTS $name;\nCREATE TABLE IF NOT EXISTS $name$sub\n";
}
elsif ($line =~ /INSERT INTO \"([a-z_]*)\"(.*)/){
$line = "INSERT INTO $1$2\n";
$line =~ s/\"/\\\"/g; #"
$line =~ s/\"/\'/g; #"
}else{
$line =~ s/\'\'/\\\'/g; #'
}
$line =~ s/([^\\'])\'t\'(.)/$1THIS_IS_TRUE$2/g; #'
$line =~ s/THIS_IS_TRUE/1/g;
$line =~ s/([^\\'])\'f\'(.)/$1THIS_IS_FALSE$2/g; #'
$line =~ s/THIS_IS_FALSE/0/g;
$line =~ s/AUTOINCREMENT/AUTO_INCREMENT/g;
print $line;
}
}
Some additional code was necessary to successfully migrate the sqlite database (handles one line Create table statements, foreign keys, fixes a bug in the original program that converted empty fields ''
to \'
.
I posted the code on the migrating my SQLite database to mysql Question
Source: (StackOverflow)
We own server A, and on some occasions we rewrite requests to our partner on server B.
In some cases server B will respond with a redirect to a completely different website, which is what is expected, let's say server C. However, when that redirect form server B comes back to server A, server A interprets it as a redirect to a location on iteself (A) and not as a redirect to other website - server C.
How do I take note of the redirect to the external website and actually go there, rather than assuming that redirect is on my server (and 404ing)? We're running IIS 7 with the rewrite module.
Any help much appreciated.
Source: (StackOverflow)
I have to redirect port 80 to 2368 in htaccess but I'd like to keep the requested protocol intact so that SSL doesn't break.
I currently have this:
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
RewriteRule ^ http://sub.domain.com:2368%{REQUEST_URI} [P,QSA,L]
which works correctly but I'd like the protocol to be taken from the %{HTTP_HOST} condition if possible.
Is there a way to get this to be more dynamic without hard coding domains and protocols?
It seems very slow as is.
Source: (StackOverflow)
I want to overwrite/reuse the existing output directory when I run my Hadoop job daily.
Actually the output directory will store summarized output of each day's job run results.
If I specify the same output directory it gives the error "output directory already exists".
How to bypass this validation?
Source: (StackOverflow)
I am trying to get nginx
to work with my pushState
-based URI handling that backbone.js
manages for me in an Javascript app.
Right now accessing URI's with one level, eg. example.com/users
works well, but not two-level or deeper URI's, such as example.com/users/all
, which is mentioned in the Backbone documentation:
For example, if you have a route of /documents/100, your web server
must be able to serve that page, if the browser visits that URL
directly
So, being far from acquainted with nginx's rewrite options, I am still sure that I can do something like rewrite ^ /index.html;
to redirect everything to my index.html
, but loosing out on any eventual static files (images, javascript & css) stored on the same server which I need to be able to access.
So what should I do instead with the below shown, current configuration, to make this work?
server {
listen 80;
server_name example.com;
location / {
root /var/www/example.com;
try_files $uri /index.html;
}
}
Source: (StackOverflow)
I'm having issues keeping the parameters of the url working after an htaccess url rewrite.
My htaccess rewrite is as follows:
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?lang=$1&page=$2
Which means:
domain.com/index.php?lang=en&page=product
displays as domain.com/en/product
For some reason, when I add a ?model=AB123&color=something
at the end of my URLs I am not able to retrieve those parameters in php using $_GET['model']
and $_GET['color']
even though they are present in the displayed URL.
Why aren't the variables passed along?
Source: (StackOverflow)