Calculating Network Addresses
I have frequently run up against the problem of generating network addresses (strictly speaking IPv4) but I am positive that the principles demonstrated below can carry over into IPv6.
The biggest issue is that in calculating addresses, the best way to find the "Nth address from here" is to convert to decimal and add. Well it's not so difficult to convert 4 octets into a 32-bit binary number, and crunch that into a single decimal number, add "N", convert back to binary, divide into octets, and convert back to decimal... but it takes forever. So why take forever when you don't need to. The trick? Divide & Conquer.
After walking through the theory, masks, addresses, ranges, compliments... I finally put together the following as a helper. Feel free to comment.
RegEx – Lists & Hashes with Escape Characters
I recently ran across a need to match escape characters within a regular expression. The key here is that you need to make use of a fun little RegEx feature called "Zero-Width, Negative, Look-Back Assertion". Its a loaded title, but crams a LOT of functionality into a little space... None. This all stems from a post I made on Python-Forum...
Just FYI, so that nobody hassles me later, you need to include the Python Regular Expression module in order to use any of these examples.
import re
Problem #1: How do you represent an "escaped character" in a Regular Expression?
Solution #1: Use positive look-back assertion on the existence of a single backslash prior to any character.
text=r'\f\o\o' regex = r'(.(?:(?<=\\).))' i = re.finditer(regex, text) for m in i: print m.groups()
Problem #2: It is not possible to put the previous regex inside a character set. So how then do you create a set of "escaped characters + non-terminators"?
Solution #2: Use the "?" on each "character" in the set.
text='{foo:bar}{a:\/foo\.bar}'
regex = r'(\{(?:(?:(?<=\\).)?[^\{\}\[\]\/\.]?)+\})'
i = re.finditer(regex, text)
for m in i: print m.groups()
Problem #3: How do you keep multiple occurrences from matching?
Solution #3: Explicit match using anchors. In other words, use "^" and "$" to bound your matching appropriately.
text='{a:\/foo\.bar}'
regex = r'^(\{(?:(?:(?<=\\).)?[^\{\}\[\]\/\.]?)+\})$'
i = re.finditer(regex, text)
for m in i: print m.groups()
Problem #4: How do you match practically any character that could occur within 2 bounding characters (like /regex/ in a sed-esque fashion)?
Solution #4: Exactly like before, except now, the only character we cant have in the middle is one of the plain (non-escaped) bounding characters.
text='/g[@#$%\/{foo:bar}\/^&*()]/{a:\/foo\.bar}'
regex = r'(\/(?:(?:(?<=\\).)?[^\/]?)+\/)'
i = re.finditer(regex, text)
for m in i: print m.groups()
Problem #5: How do we avoid matching when two valid matches are split? Like "/foo/{a:b}/bar/"
Solution #5: Explicitly define the possible combinations of each regex subsection
text1='{a:\/foo\.bar}/foo\/bar/'
text2='/foo\/bar/{a:\/foo\.bar}'
regex=r'^(\{(?:(?:(?<=\\).)?[^\{\}\[\]\/\.]?)+\})(\/(?:(?:(?<=\\).)?[^\/]?)+\/)$'
i = re.finditer(regex, text1)
for m in i: print m.groups()
regex=r'^(\/(?:(?:(?<=\\).)?[^\/]?)+\/)(\{(?:(?:(?<=\\).)?[^\{\}\[\]\/\.]?)+\})$'
i = re.finditer(regex, text2)
for m in i: print m.groups()
Code Highlight
Just a small test of a new code-highlighting feature.
#!/usr/bin/env python
def test():
try:
print "Trying something..."
int("foo") # Should cause an exception
return "TRY"
except:
print "Excepting something..."
return "EXCEPT"
finally:
print "Finalizing something..."
return "FINALLY"
return "FUNCTION"
if __name__ == "__main__":
print test()
In the business of costumes…
So I dont know how I got here, but it looks like I have officially entered the business of making costumes. Honestly, after 3 straight Halloween costumes that (at least to me) have seemed excessively over-the-top, I find it hard to imagine what chaos next year will bring.
I guess it was a hit. I don't quite remember much, given my extreme lack of sleep finally catching up to me. The only question I had was why nobody gave me the memo that there was a special "sit down" lunch today at work... needless to say, that went over splendedly.
So for all the critics out there, YES, I know the power-button is on the wrong side. I was lacking sleep when I went to make the cut. There I was, on the inside of the costume, carefully measuring and tracing out the locations on the inside of the costume, and cutting out the shapes from the inside of the costume... opposite side? mirror image? Who knew, right?
Anyway, I won the "most creative costume" (aka "Best in Show") costume, although I personally thought some of the other costumes were absolutely hilarious, and well thought out. Personally, I am just glad that a good majority of the company got involved this year instead of just QA & Engineering (a la last year).
Finally just a shoutout to my boss's boss, Geoff, who provided a relic give-away item: a blue LED ice-cube (which as you can see, obviously ties the whole thing together)
Now for the cleanup, as I think my room-mate may kill me soon if the living room is not soon emptied of the piles of foam, fabric, and empty glue cans.
Halloween Time Again
It is Halloween again, and around that time of year, I always get some crazy idea in my head for some monstrocity of a costume. Although this year there are multiple things adding to the pressure of the costume.
I guess it all started my senior year of college, when having nothing better to do (just a joke), I cranked out a 2200uF Electrolytic Capacitor costume from a simple foam mattress-topper, some felt, and a TON of glue. The geeky creativity even won me a Nintendo DS Lite at a Costume Competition.
The next year, I already had plans brewing for a GIGANTIC bobble-head doll. And then on Halloween day, there I was, in my brand new job, wearing a HUGE costume in to the office. A little less geeky this time, I was dressed as an 8-foot-tall, bobble-head, version of one of the monsters from the childrens book "Where the Wild Things Are". I was decked, head to foot, in the hottest thing I have ever worn before, and on a harness, strapped to my chest was a 6-foot pole reaching upward to suspend the bobble-head above me. Claws & everything I was ready... it was pretty sweet.
Now this year, I have (unfortunately) to face the same people in the office, with the expectation that I will somehow top last year's costume. With this added pressure, a current high-stress project underway at work, and the added bonus of being alternated into a Cheerleading competition routine 5 days prior, I am feeling the burn.
So what am I this year? Trust me, no guess you have could even come close. Im going to wait till I can get some pictures of it though before I spoil the fun. ;-)
