Tuesday, December 29, 2015

How to get π Pi in OSX (use option-p)

It can be really hard to find the Pi character in the Special Character search in OSX. It it buried in the Greek alphabet in the Maths Symbols.

Option-P will print it.

⌥ - P

How to view the Special characters in Xcode. How to get Maths characters

I'm not sure if it has recently changed, but in Xcode when you select Edit -> Emoji & Symbols you get (I think by default) this smaller Special Chars tool.
To get access to the more familiar view with a search select the icon in the top right.



The view I am more familiar with with a search and math category.







Monday, December 28, 2015

Closures syntax simplification in Swift

I think the process of simplification for the closure syntax is quite interesting

This is an initial way of passing a function as an argument, sometime called a delegate function. The example is a simply multiplication of two numbers.



func doSomething(operation: (Double, Double) -> Double) {
    theReturnValue = operation( 2, 3)
}

func multiply(op1: Double, op2: Double) -> Double {
    return op1 * op2
}
// The main calling of the method
DoSomething(multiply)


The first step that Swift makes to simplify this is to make the multiply function an inline function, a lambda or Closure. We place the code inline, use the "in" keyword and move the curly brace.

func doSomething(operation: (Double, Double) -> Double) {
    theReturnValue = operation( 2, 3)
}

func multiply(op1: Double, op2: Double) -> Double {
    return op1 * op2
}
// The main calling of the method
DoSomething( { 
op1: Double, op2: Double) -> Double in
   return op1 * op2
}) 


But is tidied up further because Swift, like C# in this situation, is very good with type inference.  So we can remove the type declaration of the arguments and return type because it already knows them.

func doSomething(operation: (Double, Double) -> Double) {
    theReturnValue = operation( 2, 3)
}
// The main calling of the method
DoSomething(
(op1:, op2:) -> in
    return op1 * op2
}) 

Now we can tidy this up a little, by putting the return on the same line as the declaration
, but also remove the return keyword because we are returning an expression and it knows we are going to return a Double.

func doSomething(operation: (Double, Double) -> Double) {
    theReturnValue = operation( 2, 3)
}
// The main calling of the method
DoSomething(
(op1:, op2:) -> in op1 * op2 })


Cool. But Swift doesn't need you to name the arguments as it will use $0 and $1 for the first two args if names are not provided. So we can write:


func doSomething(operation: (Double, Double) -> Double) {
    theReturnValue = operation( 2, 3)
}
// The main calling of the method
DoSomething(
$0 * $1 })


One last thing. The last argument can be moved outside of the parenthesis.

func doSomething(operation: (Double, Double) -> Double) {
    theReturnValue = operation( 2, 3)
}
// The main calling of the method
DoSomething(
$0 * $1 }

Other arguments would go inside the parenthesis of DoSomething(...), but in this case there are no other args so we can remove the parenthesis entirely.

func doSomething(operation: (Double, Double) -> Double) {
    theReturnValue = operation( 2, 3)
}
// The main calling of the method
DoSomething
 $0 * $1 }

Quite interesting.





Monday, December 21, 2015

How to remove backups from Time Machine

We have a few Macs in the house and a few Time Machine devices setup. As a number of Macs share a Time Machine disk I have found that one Macs' backups can monopolise the disk through one reason or another which prevents another mac from doing a backup because of lack of space.
I could have managed this better by partitioning the disk and that's something I'll do in future, but for the time being I wanted to remove some old unwanted backups to free up some space.
This can take a very long time.

To remove a specific backup I used Time Machine Utility:

sudo tmutil delete /Volumes/drive_name/Backups.backupdb/mac_name/YYYY-MM-DD-hhmmss

I compacted the remaining sparse file by using

sudo hdiutil compact [path and name of sparsefile]


You can list the backups by using:

tmutil list backups



Unavailable

If you see the time machine is unavailable then you probably have it mounted and this will prevent you from deleting files. Unmount (eject) and try again.

tmutil docs

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/tmutil.8.html

hdiutil docs

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/hdiutil.1.html

Sunday, December 20, 2015

Python Virtualenv on OSX

Install virtualenv

Johns-MBP:~ johnroberts$ pip install virtualenv

Johns-MBP:~ johnroberts$ mkdir -p ~/Virtualenvs

Johns-MBP:~ johnroberts$ mkdir -p ~/Projects

Johns-MBP:~ johnroberts$ cd ~/Virtualenvs/

Create the virtual environments

Johns-MBP:~ johnroberts$ virtualenv foobar
New python executable in foobar/bin/python2.7
Also creating executable in foobar/bin/python

Installing setuptools, pip, wheel...done.

Johns-MBP:~ johnroberts$ virtualenv -p python3 foobar-py3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5'
New python executable in foobar-py3/bin/python3.5
Also creating executable in foobar-py3/bin/python

Installing setuptools, pip, wheel...done.


This should get Virtualenv in place.

Activate an environment

We made the Python environment, now we need to activate it.

Johns-MBP:~ johnroberts$ source foobar/bin/activate
(foobar)Johns-MBP:~ johnroberts$ 

Notice the "(foobar)" which shows that we are now in the environment.

What version of python are we using

We can check which version of python
(foobar)Johns-MBP:~ johnroberts$ which python
/Users/johnroberts/foobar/bin/python

This shows that we are using python from our environment.

(foobar)Johns-MBP:~ johnroberts$ python --version
Python 2.7.10

We can also check pip
(foobar)Johns-MBP:~ johnroberts$ which pip
/Users/johnroberts/foobar/bin/pip

Use pip to see what packages are here

(foobar)Johns-MBP:~ johnroberts$ pip list
pip (7.1.2)
setuptools (18.2)

wheel (0.24.0)

If I did pip list outside of this environment I'd get a lot of packages (I won't list them here)

Install the packages for this environment
(foobar)Johns-MBP:~ johnroberts$ pip install numpy
Collecting numpy
  Downloading numpy-1.10.2-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.7MB)
    100% |████████████████████████████████| 3.7MB 143kB/s 
Installing collected packages: numpy
Successfully installed numpy-1.10.2

Cool as a cucumber.

List of Dependencies

Make a list of dependencies for this project and view it using cat
(foobar)Johns-MBP:~ johnroberts$ pip freeze --local > requirements.txt
(foobar)Johns-MBP:~ johnroberts$ cat requirements.txt 
numpy==1.10.2
wheel==0.24.0

The list of dependencies is very useful for recreating other environments using pip. Create the environment and then use pip and the requirements.txt file to install the dependencies.

    pip install -r requirements.txt

Get out of the environment - deactivate

We just type deactivate
(foobar)Johns-MBP:~ johnroberts$ deactivate
Johns-MBP:~ johnroberts$ 

Notice that "(foobar)" has been removed. We are no longer in that environment.

Remove the Virtual Environment
Once deactivated, we can remove:
Johns-MBP:~ johnroberts$ rm -rf foobar/

Simple.


Saturday, December 19, 2015

Latest Python on El Capitan

El Capitan OSX comes with Python 2.7 but you might like to get the latest version and ensure you keep it updated.

I use Homebrew which was installed before I updated Python so I won't write how to install it.
Find http://brew.sh


GCC

You will need the latest GCC compiler. For that you can instal Xcode. If you instal a fresh version on Xcode you might need to run the following to install the command line tools:

xcode-select --install


Python 2.7

Run the following:
brew install python

pip all be installed by brew


Python 3

Run the following:
 brew install python3

Virtualenv

If you're running two versions of Python on the same system you may benefit from looking at virtualenv


Saturday, October 24, 2015

Installing numpy and scipy for OSX - using Homebrew

The place I found for instructions on how best to do this, and it has been edited recently, is
https://joernhees.de/blog/2014/02/25/scientific-python-on-mac-os-x-10-9-with-homebrew/

I have copied the script from Jörn's page to here for my personal reference. But all credit is Jörn's 
I did need X11 installed and I thought this was possible via Homebrew but I had a problem so I resorted to downloading it from Quartz page. It took a while. It used to be distributed by Apple but no longer.

Notice that some files are downloaded from pip and not brew.

Also, I installed the brew install Caskroom/cask/mactex so that I could run matpltlib with latex fonts and formulas. This does take a while to download. Not sure why.

Possible errors when installing pip pyquery and lxml
You might find compile errors fatal error: 'libxml/xmlversion.h' file not found
You can google solutions to this and this link has a few approaches to solve the problem:




# install PIL, imagemagick, graphviz and other
# image generating stuff
brew install libtiff libjpeg webp little-cms2
pip install Pillow
brew install imagemagick --with-fftw --with-librsvg --with-x11
brew install graphviz --with-librsvg --with-x11
brew install cairo
brew install py2cairo # this will ask you to download xquartz and install it
brew install qt pyqt

# install virtualenv, nose (unittests & doctests on steroids)
pip install virtualenv
pip install nose

# install numpy and scipy
# there are two ways to install numpy and scipy now: via pip or via brew.
# PICK ONE, i prefer pip for proper virtualenv support and more up-to-date versions.
pip install numpy
pip install scipy
# OR:
# (if you want to run numpy and scipy with openblas also remove comments below:)
#brew install openblas
brew install numpy # --with-openblas
brew install scipy # --with-openblas

# test the numpy & scipy install
python -c 'import numpy ; numpy.test();'
python -c 'import scipy ; scipy.test();'

# some cool python libs (if you don't know them, look them up)
# matplotlib: generate plots
# pandas: time series stuff
# nltk: natural language toolkit
# sympy: symbolic maths in python
# q: fancy debugging output
# snakeviz: cool visualization of profiling output (aka what's taking so long?)
#brew install Caskroom/cask/mactex  # if you want to install matplotlib with tex support and don't have mactex installed already
brew install matplotlib --with-cairo --with-tex  # cairo: png ps pdf svg filetypes, tex: tex fonts & formula in plots
pip install pandas
pip install nltk
pip install sympy
pip install q
pip install snakeviz

# ipython with parallel and notebook support
brew install zmq
pip install ipython[all]

# html stuff (parsing)
pip install html5lib cssselect pyquery lxml BeautifulSoup

# webapps / apis (choose what you like)
pip install Flask Django tornado

# semantic web stuff: rdf & sparql
pip install rdflib SPARQLWrapper

# graphs (graph metrics, social network analysis, layouting)
pip install networkx
brew install graph-tool

# maintenance: updating pip libs
pip install pip-tools  # you'll then have a pip-review command, see Updating section below


Sunday, August 9, 2015

Problem with NodeJs - node --debug server.js:72 - throw er; // Unhandled 'error' event


When playing around with debugging NodeJs and entering the following:

$ node --debug server.js

I got this error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:905:11)
    at Server._listen2 (net.js:1043:14)

    at listen (net.js:1065:10)

This is caused by starting and stopping these debug processes, but leaving one running.


Run the following to identify the running process and then kill it.


Johns-MBP:server john$ ps aux | grep node

This will present something like this:
john      2799   0.0  0.3  3129308  52852   ??  S    11:38AM   0:01.30 /usr/local/Cellar/nvm/0.18.0/v0.10.33/bin/node --debug-brk=49961 --nolazy server.js
john      3023   0.0  0.0  2432772    660 s002  S+    2:37PM   0:00.00 grep node

This has told us the processId to kill. So run kill -9 [the process id]
eg

Johns-MBP:server johnroberts$ kill -9 2799






Sunday, June 14, 2015

A great slice of Steve Jobs history in video





A quite historic talk from Steve Jobs from 1980

If you're lucky enough to now this time in history, what Steve says and his vision is quite legendary.





Just hear the ovation when Steve Jobs returns to Apple, January 7 1997

Steve joins the stage at around 7:25 into the video.


January 7, 1997

also, August 1997 at the Boston Macworld Expo


Quite a searing analysis of the state Apple was in.


Steve Jobs presenting to Cupertino City Council, June 7 2011


Steve presents his proposal for the new campus.




The great Jonathan Ive giving a tribute to Steve Jobs




He believed there was a gravity,  almost a civic responsibility to care way beyond any functional imperative. He cared the most. He worried the most.


An amazing insight to the quality at the heart of Steve's work

Keith Yamashita worked for Steve Jobs at Next.







Tuesday, May 19, 2015

Visual studio project fails to build when using Entity Framework Package manager console

Trying to run Entity Framework commands from the Package Manager Console resulted in "compile errors". This was driving me insane. Sometimes it built, but most of the time it didn't.
I was using Visual Studio 2010 so perhaps it's fixed in other version but I found that if you suspend ReSharper the problem goes away.

How to Suspend ReSharper: Tools - Options - Resharper - General.

Happy now.

Saturday, May 16, 2015

How to find slow Sql Server queries

We can run the queries
EXEC dbo.sp_who 
or 
EXEC dbo.sp_who2
These are the typical commands where who2 is a newer version that gives us some more info.

who2 tells us the SPID of any blocking process and we can then run,
 DBCC INPUTBUFFER(put SPID here)
to tell us what it was doing.

But better than that...

A cool free command written by Adam Mechanic called sp_WhoIsActive

It has some parameters you can switch on but be careful about running this with all execution plans being returned on a busy server.

Tuesday, April 28, 2015

Where does Xcode put compiled C++ executable

I needed to make a quick C++ console app the other day and thought I'd use Xcode. It worked fine but I wondered where the file was placed and it is miles away!

The program was called traindata

In fact, it is here:


/Users/johnr/Library/Developer/Xcode/DerivedData/traindata-frnrpuzfjjiemzhbqbxvjomrwamj/Build/Products/Debug

Sunday, March 22, 2015

Changing the Technicolor tg582n router to use OpenDNS

OpenDNS - the free and ever so handy service that enables us to protect our home networks from the worst of the Internet.

http://www.opendns.com

How does Open DNS work? Essentially, it replaces the DNS lookups that the  Internet Service Provider (ISP) gives us so that when we request a Website, OpenDNS can decide if it we want it blocked or not. To do this we point our router and the lookup servers provided by OpenDNS and not to the servers provided by our ISP.

I recently changed my router provided by my Internet Service Provider (ISP) and for some reason imagined that OpenDNS was still configured. Of course it wasn't. I checked the control panel of the router and found I was using the ISP's DNS servers and found no way to change them. So the solution is to use Telnet.

First, check you are using OpenDNS.
Goto, https://www.opendns.com/welcome/
You should see a big tick if you are already configured.
If you see the tick - all is good. Ignore this blog post.

I needed the Username and password for the router,
and the OpenDNS ip addresses. These are they:
  • 208.67.222.222
  • 208.67.220.220
On a Mac you simply  open Terminal, (CMD-Space then type terminal is one way to open it.).
Not sure what Telnet client you'd use on Windows.

I am assuming the IP address of the router is 192.168.1.254, if not, use the correct one instead.
type: Telnet 192.168.1.254

JMBP:~ jr$ Telnet 192.168.1.254


Enter the Administrator's user name and password and you should see something like this:


------------------------------------------------------------------------

                             ______  Technicolor TG582n
                         ___/_____/\ 
                        /         /\\  8.4.4.J.AS
                  _____/__       /  \\ 
                _/       /\_____/___ \  Copyright (c) 1999-2011, Technicolor
               //       /  \       /\ \
       _______//_______/    \     / _\/______ 
      /      / \       \    /    / /        /\
   __/      /   \       \  /    / /        / _\__ 
  / /      /     \_______\/    / /        / /   /\
 /_/______/___________________/ /________/ /___/  \ 
 \ \      \    ___________    \ \        \ \   \  /
  \_\      \  /          /\    \ \        \ \___\/
     \      \/          /  \    \ \        \  /
      \_____/          /    \    \ \________\/
           /__________/      \    \  /
           \   _____  \      /_____\/
            \ /    /\  \    /___\/
             /____/  \  \  /
             \    \  /___\/
              \____\/

------------------------------------------------------------------------

Command prompt,
You will see the following prompt, type commands after the prompt.
{Administrator}=>

Look at the list of DNS servers you currently have. Mine are for ZEN. 
Typing, dns server route list
{Administrator}=>dns server route list

will list your current server list.
{Administrator}=>dns server route list

DNS Server Entries:
  DNS Server     Source                Label              Metric Intf         State  Domain            
D 212.23.6.100                                            10     Internet      UP      *             
D 212.23.3.100                                            10     Internet      UP      *   

Flush the list, thus:
{Administrator}=>dns server route flush

Now add the two Open DNS server addresses:
{Administrator}=>dns server route add dns=208.67.222.222 metric=10 intf=Internet
{Administrator}=>dns server route add dns=208.67.220.220 metric=10 intf=Internet

Now list them to see if they are there:
{Administrator}=>:dns server route list  

And we see:
DNS Server Entries:
  DNS Server     Source                Label              Metric Intf         State  Domain            
S 208.67.222.222                                          10     Internet      UP      *             
S 208.67.220.220                                          10     Internet      UP      *    


Now Save and exit:
{Administrator}=>saveall
{Administrator}=>exit

Worked for me. Good luck.


Saturday, March 21, 2015

GIT - pushing and pulling in an easy to understand image

Drawn by Oliver Steele, this helps when moving to GIT from SVN.
SVN has two ends: the workplace and the Repo. GIT has four. The idea of staging.
Brilliant idea.


Saturday, March 7, 2015

Show all files in OS X

Want to see all hidden files in Finder.


Show them:
defaults write com.apple.finder AppleShowAllFiles 1 && killall Finder



Hide them:

 defaults write com.apple.finder AppleShowAllFiles 0 && killall Finder


Saturday, January 10, 2015

How to get the # (hash) key in Windows, using VMWare Fusion on a Mac from a UK keyboard

There is no key for the # in a Mac UK keyboard so when I need it in Mac application I use Alt - 3. This is fine but not when running a Windows Virtual on the Mac via VMware Fusion. For that I use, Cmd - \ (Command backslash)