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