TheShed

A comparison of sorting algorithms

Category: Programming
#Sorting #algorithms

From an internal DL. A comparison of sorting algorithms on different types of data

Sorting algorithms

Or, through the medium of Hungarian folk dancing

Virtual Python

Category: Programming
#python

A quick starter on Python Virtual Environment

Create:

mkdir workingdir
cd workingdir
virtualenv venv

On Unix/Mac OS X use:

source venv/bin/activate

On Windows use:

venv\Scripts\activate.bat

Finish:

deactivate

To move the environment somewhere else:

  1. In existing (active) environment; grab a list of dependencies with pip freeze > requirements.txt
  2. In new (active) environment; install from the dependency list with pip install -r requirements.txt

To set a specific Python version. On Mac:

  1. First find the path to the version we want with which python3
  2. Activate the environment and run mkvirtualenv --python=<path to python> <venv> For example mkvirtualenv --python=/usr/local/bin/python3 myenv

Excel Macro Munging

Category: Programming
#Excel #VBA #macro

The problem:

A set of items in VSO where we wanted the ID as part of the title text.

(It's a long-ish story, but the default VSO board shows only title and assigned to so we adopted a convention of adding the ID to the title text too).

Over time, we'd gathered a number of MVPs on the board where the ID wasn't included. Rather than hand editing, I slurped the query into Excel using the TFS team extensions and ran a little macro to update the items. Here's the code:

Sub fixMissingIDInTitle()

Dim MyCell As Range, MyRange As Range
Dim count, total As Long
Dim sTitle As String
Dim sID As String
Dim iIDLength As Integer


' grab the starting point and then extend to get the range of items
Set MyRange = Sheets("All MVPs").Range("C3")
Set MyRange = Range(MyRange, MyRange.End(xlDown))

total = 0
count = 0

' iterate over the range
For Each MyCell In MyRange

    sTitle = MyCell.Value
    sID = MyCell.Offset(0, -2).Value
    iIDLength = Len(sID)

    If (Left(sTitle, iIDLength) = sID) Then
        ' do nothing - ID already at start
        ' Q: but is it the right ID?
    Else
        MyCell.Value = sID + " - " + sTitle
        count = count + 1
    End If

    total = total + 1

Next MyCell

MsgBox ("Updated " + CStr(count) + " of " + CStr(total) + " items.")

End Sub

Python on Unix

Category: Programming
#python

A fragment to let Unix boxes (and Mac OS X) know that this is a python script:

1
#!/usr/bin/env python

Add at the start of the script.

Make sure the script is executable with:

chmod +x scriptname.py

Application Insights — Web Application Monitoring

Category: Programming
#Visual Studio #Web #Application Insights #Azure

Visual Studio supports Application Insights. This provides an mechanism to add monitoring support into a web application. There's also a version for Azure.

Application Insights lets you monitor your live application for: Availability - We'll test your URLs every few minutes from around the world. Performance - Detect and diagnose perf issues and exceptions. * Usage - Find out what users are doing with your app, so that you can make it better for them.

Brackets Editor

Category: Programming
#Text #Editor

In a HN thread on editors (Lighttable and that teams plans to do stuff with Eve) someone mentioned Brackets in the context of installed and haven't looked back since. Right now I'm happy with Sublime...

...but you never know.

WinDBG

Category: Programming
#Windows #debugging

I saw a BSOD today, oh boy, first in a while.

  1. Grab WinDBG(x64) — install the Windows Software Development Kit (from here for 8.1) and select the debugging tools in the installer.
  2. Launch WinDbgx64
  3. Set the Symbol Search Path to SRV*C:\Windows\symbol_cache*http://msdl.microsoft.com/download/symbols
  4. Save the workspace
  5. Load the .dmp file
  6. !analyze -v

For a windbg cheat sheet try https://labs.snort.org/awbo/windbg.txt

WinDbg howto and another

For memory related suspicions try memtest for memteat.org — you'll need a bootable something with it on...

For driver relateds suspicions follow the instructions to verify the drivers.

Git Cheats

Category: Programming
#Git #Source Control

A post to capture Git related goodness. The secret sauce? Branch early and often but merge and kill branches too.

Updated: 2014-10-03

Fragments

Basics

Create a directory with your project

e.g. ~\project

git init

To create a Git repro. If you want a bare repro (i.e. because you're going to push to it from somewhere else) use git --bare init.

git add <filename.c>

To add a file(s) to the repro or to stage a file (staged files are like a snapshot of what you're going to commit before you actually commit (commit to committing!). This means you can stage files as you work on them and then commit an atomic piece of work.

Git will flag untracked files. If you want to ignore files, add them to a .gitignore file. For example:

\output

Will ignore anything in the \output subdirectory. You might want to ignore *.jpg for example, but binary files and Git is a whole other story (search for GitMedia for example).

git status

To see what's what.

git commit -m "Commit message"

Commit whatever is staged to the repro.

git rm <file>

remove a file (from repro) and local directory

git log

Get useful stuff from git like a log...

...loads of options like for example

git log -p -2

which shows the diffs (-p) for the last 2 (-2) commits.

git log --pretty=oneline

What a GUI? use:

gitk

Remote Repositories

git remote -v

Show remote repositories

git remote show <remotename>

extra info about remotename

git remote add <remotename> <URL>

Connect to a remote

git fetch <remotename>

Grab everything from a remote but won't merge

git pull

Works if you have a branch setup to track a remote branch (although in my tests it just worked??!?) got

git push <remotename> <branchname>

Push your changes back to the remote - but beware if someone else has also pushed!

git clone <url>

Creates a local repro in a new directory with remotename=origin

git remote rm <remotename>

remove remote

note: you can use UNC paths on windows, but use forward slahes, i.e.

git remote add origin //home-server/git/Crowmarsh/Crowmarsh.git

Tagging

git tag -a v0.1 -m "Tag: version 0.1 alpha"

Tag with annotation (v0.1)

then:

git tag show v0.1

Important: git won't push tags to remote servers automagically. In that sense they're like branches, so you do:

git push <remotename> <tag>

e.g. git push origin v0.1

git push --tags

will push all tags...!

Branching

Branches are lightweight. Effectively they are a pointer to the git object that represents the commit.

git branch <branchname>

Creates a new branch pointing to the last commit.

git checkout <branchname>

Switches to the branch. Switches is a significant piece of terminology; think of it as switching rather than checking out.

git branch 
git branch -v

Details of branches....

Basic Merging

First, checkout the branch you want to merge into, then:

git merge <branchname>

If you get something like CONFLICT (content): Merge conflict in readme.txt then you'll need to open the file(s) and edit to resolve the conflicts.

Then commit as normal...

Note that merge does a 3-way merge between the two latest branch snapshots and the most recent common ancestor of the two.

git commit -a -m "Message"

Mark has a useful blog post covering why you should

git fetch
git merge

rather than

git pull

Remote Branches

There's a branch on your remote repros to, typically initially this is origin/master (i.e. the origin of your remote and the master (or HEAD) branch). If you do work locally and then push to the remote all is fine. Except that is, if someone else has pushed to the remote in the meantime so that origin/master is now ahead. To you need to fetch...

git fetch origin

You can checkout a local tracking branch that git knows is related to a remote branch, and so automagically pushes changes to the right place without you having to tell it.

git checkout --track <remote>/<branch>

If you have a local repo and want to push to a (bare) remote repo, use

git remote add <name> <url>
git push --set-upstream -u <name> <branch>

Rebasing

There are two ways to integrate changes from one branch to another. merge (see above) and rebasing. Instead of the 3-way merging, rebasing takes all the changes committed on one branch and replays them (rebases) on another.

Switch to branch then

git rebase master

to rebase from master onto the branch you checked out.

Merging

Merging from a remote branch (i.e. two developers have pushed changes to the server in the same branch).

git fetch
git rebase origin/master

See http://stackoverflow.com/questions/7200614/how-to-merge-remote-master-to-local-branch

Tracking

Tracking branches means Git is aware of the relationship between a local branch and a remote branch, for example, when tracked git status automagically tells you whether you're ahead or behind the remote branch.

git checkout --track origin/development

Will checkout a remote branch (origin/development) locally and set the relationship between them. Alternatively, you can set a tracking relationship when pushing your local branch. i.e.

git push -u origin development

git branch -vv

Provides details of tracked relationships known to Git.

Misc

Fix a detached head:

git checkout master

From http://stackoverflow.com/questions/10228760/fix-a-git-detached-head

Overwrite local files

If you want to overwrite local, uncommitted, files you can use:

git fetch --all
git reset --hard origin/master

(From Stackoverflow)

Reference material

Executing a process on Windows

Category: Programming
#Python

A fragment to execute a process on Windows:

from subprocess import Popen
proc = Popen("vi \"" + filename + "\"", shell=True )
print(proc)

Which opens filename with vi (whatever that might be on a Windows box). Note that while Python is pretty good from a crossplatform perspective, it's not the best when it comes to executing other system processes. So, on other platforms, use with care.

Writing to a file in Python

Category: Programming
#Python #filesystem

A fragment for writing to a file in Python:

f = open(filename,'w')
f.write('Hey, I'm saying Hello file! \n')
f.close()

Note that this will overwrite an existing file. Use open(filename,'a') to open in append mode. The Python documentation has details.