Skip to content

Instantly share code, notes, and snippets.

@shaunhess
shaunhess / gist:4192072
Created December 3, 2012 01:39
Python - Pretty Printing JSON
>>> import json
>>> print(json.dumps(data)) # No indention
{"status": "OK", "count": 2, "results": [{"age": 27, "name": "Oz", "lactose_intolerant": true}, {"age": 29, "name": "Joe", "lactose_intolerant": false}]}
>>> print(json.dumps(data, indent=2)) # With indention
{
"status": "OK",
"count": 2,
"results": [
{
"age": 27,
@shaunhess
shaunhess / gist:4192086
Created December 3, 2012 01:42
Python - Quick and Dirty Web Service
# Server Side
from SimpleXMLRPCServer import SimpleXMLRPCServer
def file_reader(file_name):
with open(file_name, 'r') as f:
return f.read()
server = SimpleXMLRPCServer(('localhost', 8000))
server.register_introspection_functions()
#!/usr/bin/env python
#
# Python Timer Class - Context Manager for Timing Code Blocks
# Corey Goldberg - 2012
#
from timeit import default_timer
@shaunhess
shaunhess / Test_Prime_Num_PowerShell
Created October 20, 2013 19:20
Testing for Prime Numbers Using Powershell
So what is a prime number anyway. Lets see what Wikipedia has to say:
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime, as only 1 and 5 divide it, whereas 6 is composite, since it has the divisors 2 and 3 in addition to 1 and 6. The fundamental theorem of arithmetic establishes the central role of primes in number theory: any integer greater than 1 can be expressed as a product of primes that is unique up to ordering. This theorem requires excluding 1 as a prime.
Since there are infintely number of primes, it's always fun to see how fast and how many we can find. The largest know prime number has nearly 13 million decimal digits!
Now thats out of the way lets see how we can test to see whether a number is prime or not using Powershell. Today we will look at a few different methods, including my favorite, Sieve of Eratosthenes.
@shaunhess
shaunhess / Newton-Raphson Method in Powershell
Created October 20, 2013 19:22
Newton-Raphson Method in Powershell
In the last post we looked at the Bisection Method to solve a simple problem, finding the square root of a real number. This week we are going to use the same exact problem, but use a better algorthim.
Newton's method (also known as the Newton–Raphson method), named after Isaac Newton and Joseph Raphson, is a method for finding successively better approximations to the roots (or zeroes) of a real-valued function. The algorithm is first in the class of Householder's methods, succeeded by Halley's method.
The Newton-Raphson method in one variable:
Given a function ƒ(x) and its derivative ƒ '(x), we begin with a first guess x0 for a root of the function. Provided the function is reasonably well-behaved a better approximation x1 is
x_{1} = x_0 - \frac{f(x_0)}{f'(x_0)}.\,\!
Geometrically, x1 is the intersection with the x-axis of a line tangent to f at f(x0).
@shaunhess
shaunhess / Successive Approximation in Powershell
Created October 20, 2013 19:23
Successive Approximation in Powershell
In the last few posts we talked about exhaustive enumeration where we try all "possible" values until we find the solution. But what if there is no exact answer? What if we cannot enumerate all of the guesses? What we need is an ability to improve our guesses.
Enter Successive Approximation:
Start with a guess
Iterate
Improve our guess
So how do we do that? We are going to start with something called the Bisection Method. Lets see what Wikipedia has to say:
The bisection method in mathematics is a root-finding method which repeatedly bisects an interval and then selects a subinterval in which a root must lie for further processing. It is a very simple and robust method, but it is also relatively slow. Because of this, it is often used to obtain a rough approximation to a solution which is then used as a starting point for more rapidly converging methods.[1] The method is also called the binary search method[2] and is similar to the computer science Binary Search, where the range of possible solutions is ha
@shaunhess
shaunhess / PowerShell Recursion
Created October 20, 2013 19:23
Recursion in PowerShell
Google recursion and besides for the joke "Did you mean: recursion" you'll find a plethora of examples, definitions, and people showing you how clever they are.
Put simply, recursion is broken down like this:
Base case - simpliest possible solution
Inductive step - break problem into a simplier version of the same problem with some other steps to execute.
Ok clear as mud. So as always lets take a problem and break it down.
A lot of examples show recursion using the Fibonacci sequence. However I always liked the "Blastoff!" example from How to Think Like a Computer Scientist.
@shaunhess
shaunhess / Brute Force in Powershell
Created October 20, 2013 19:24
Brute Force in Powershell
Most of you have heard some variation of this problem:
A farmer has pigs and chickens. When the farmer walks out in the yard he sees 20 heads and 56 legs. How many pigs and chickens does the farmer have?
Based on the information given we can determine a few basic truths:
20 heads. I think we can assume we have no decapitated pigs or chickens so the number of pigs + number of chickens is equal to 20.
56 legs. 4*number of pigs + 2*number of chickens is equal to 56.
So how are we going to solve this in a programmatic way? Lets try a brute force algorithm. That's just another way to say we are going to enumerate and check over and over until we hit a solution. Remember this code is for learning and doesn't follow best practices.
@shaunhess
shaunhess / Exhaustive Enumeration in Powershell
Created October 20, 2013 19:25
Exhaustive Enumeration in Powershell
I'm often overheard at work and customer engagements spreading the gospel about Powershell, encouraging everyone from the Junior SA to the most senior "engineer" to take advantage of this powerful language. Anyone can pick Powershell up and become quite productive in no time. One of the things I noticed however was a lack of fundamentals in the newly initiated. So I figured why not do my part and provide a few posts I'm calling "Back To Basics". These are foundational to any langauge, not just Powershell, so pick your poison. Feel free to post your samples in the comments.
Now lets get started with our first lesson, Exhaustive Enumeration.
So what is it? Put simply, exhaustive enumeration is trying all "possible" values until you find the solution.
I always find a problem useful for learning so lets use something simple we can all understand. Finding the square root of a perfect square.
So whats a square and a square root?
@shaunhess
shaunhess / Reading the LastWriteTime of a registry key using Powershell
Created October 20, 2013 19:26
Reading the LastWriteTime of a registry key using Powershell
At work recently I needed to pull together some information from the registry of a few thousand machines and include the last time the key had been updated. Lately I've been turning to Powershell more and more for my day to day tasks and this time was no different. However this simple task turned out to not be so easy, and it all revolved around acquiring the LastWriteTime of the registry keys.
Digging through WMI and .NET proved less fruitful than I had hoped, so off to Google I went. It seemed everyone had the solution if you wanted to query the machine locally, but with thousands of hosts in my Enterprise that wasn't going to work. Plus, who doesn't enjoy a good challenge. One particular script was very useful in pointing my team in the right direction, posted by Tim Medin over at blog.securitywhole.com. So we decided to adapt and modify his script to work with remote hosts.
I've only tested against a few machines at home, but I wanted to share it while I had time. I will update the post if I find issues