Neha Narula: The future of money

There are actually people all over the world running this software, and we call them Bitcoin miners. Anyone can become a Bitcoin miner. You can go download the software right now and run it in your computer and try to collect some bitcoins. I can’t say that I would recommend it, because right now, the puzzle is so hard and the network is so powerful, that if I tried to mine Bitcoin on my laptop, I probably wouldn’t see any for about two million years. The miners, professional miners, use this special hardware that’s designed to solve the puzzle really fast. Now, the Bitcoin network and all of this special hardware, there are estimates that the amount of energy it uses is equivalent to that of a small country. So, the first set of cryptocurrencies are a little bit slow and a little bit cumbersome. But the next generation is going to be so much better and so much faster.
Neha Narula

View POST Data using Chrome Developer Tools

When figuring out how to formulate the contents of a POST request it’s often useful to see the “typical” fields submitted directly from a web form.

  1. Open Developer Tools in Chrome. Select the Network tab (at the top).
  2. Submit the form. Watch the magic happening in the Developer Tools console.

    The Network tab in Chrome Developer Tools.

  3. Click on the first document listed in the Developer Tools console, then select the Headers tab.

    The Headers tab showing the details of the POST payload.

That’s just scratching the surface of the wealth of information available on the Network tab. Read this to find out more.

Deleting All Nodes and Relationships

Seems that I am doing this a lot: deleting my entire graph (all nodes and relationships) and rebuilding from scratch. I guess that this is part of the learning process.

sample-graph

Route 1: Delete Relationships then Nodes

A relationship is constrained to join a start node to an end node. Every relationship must be associated with at least one node (a relationship may begin and end on the same node). No such constraint exists for nodes. As a result relationships must be deleted before nodes are deleted.

Delete all relationships using either

$ START r = RELATIONSHIP(*) DELETE r;

or

$ MATCH ()-[r]-() DELETE r;

Then delete the nodes with

$ MATCH (n) DELETE n;

Route 2: Detach and Delete

Using DETACH DELETE it’s possible to delete relationships and nodes at once.

$ MATCH (n) DETACH DELETE n;

Check

Confirm that all nodes and relationships have gone.

$ MATCH (n) RETURN COUNT(n);
$ MATCH ()-[r]->() RETURN COUNT(r);

Or, alternatively:

$ START n = NODE(*) return COUNT(n);
$ START r = RELATIONSHIP(*) return COUNT(r);

Running Cypher Queries from File on Windows

lazywebcypher

Recent packages of Neo4j for Windows do not include neo4j-shell. The Neo4j browser will only accept one statement at a time, making scripts consisting of multiple Cypher commands a problem.

Have a look at LazyWebCypher which will allow you to execute an arbitrary volume on Cypher code on your local Neo4j server.

There are multiple avenues to upload your script: provide a URL, select a file or simply paste the code. Then move to the other tab and hit the Import button.

Voila!

Remote Access to Neo4j on Windows

Accessing the Neo4j server running on your local machine is simple: just point your browser to http://localhost:7474/. But with the default configuration the server is not accessible from other machines. This means that other folk can share in the wonder of your nodes edges.

Enabling remote access is simple.

  1. Shut down your running Neo4j server.
  2. Press the Options button, which will bring up a dialog like this:
    neo4j-options

  3. Press the top Edit button, which will open the neo4j.conf file in an editor.
  4. Browse to the HTTP Connector section and add the line highlighted below.
    # HTTP Connector
    #
    dbms.connector.http.type=HTTP
    dbms.connector.http.enabled=true
    #
    # dbms.connector.http.address=0.0.0.0:#{default.http.port}
    dbms.connector.http.address=0.0.0.0:7474
    

Your server will now be available to other machines via port 7474. From the local machine it’ll still be at http://localhost:7474/.

The above solution works (at least) for Neo4j 3.0.4.

Using virtualenvwrapper with Python 3

By default the version of virtualenvwrapper on my machine uses Python 2. Not terribly useful since I’m focusing on using Python 3. To get around this I first installed virtualenvwrapper via pip (rather than using the package manager) to ensure that I got the most recent version.

$ sudo pip install virtualenvwrapper

If you haven’t used virtualenv before, then add the following content to your .bashrc:

export WORKON_HOME=$HOME/.virtualenv
source /usr/local/bin/virtualenvwrapper.sh

That will select a folder in which your virtual environments will be stored and also load a bunch of shell functions.

Creating a Virtual Environment

Then, to create a new virtual environment using the Python 3 interpreter, do the following:

$ mkvirtualenv --python=`which python3` test-project

Specifying the path to the Python 3 interpreter is the critical point.

You’ll find that the interpreter you get in the resulting environment will automatically be Python 3.

(test-project) $ python
Python 3.5.2 (default, Jul  5 2016, 12:43:10) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Attaching and Detaching the Virtual Environment

To enter that virtual environment in the future, do

$ workon test-project

To exit the virtual environment, simply do

$ deactivate

Associating a Project with a Virtual Environment

To associate the current working folder with the active virtual environment just do

$ setvirtualenvproject

A full reference to virtualenvwrapper functionality can be found here.

Django: Recreating the Database

In the early stages of a project I have found that I break the database fairly often. Rather than figuring out just how to fix and rebuild, it makes more sense to just start again from scratch. There are a couple of ways to do this.

Using django-extensions

There’s a reset_db command in django-extensions that will do the job in one simple step.

$ python3 manage.py reset_db

Direct Approach

If the above does not solve your problems then you’ll need to man up and interact with the database directly.

  1. Use DROP DATABASE to delete the entire database. If you do have any important data stashed in there then, for God’s sake, back it up first!
  2. Use CREATE DATABASE to recreate the database.
  3. Delete all numbered migration files.
  4. Do the makemigrations and migrate dance.

You should be sorted.

Django: Foreign Keys and Initial Migration

If you run into a Cannot add foreign key constraint error when applying your initial Django migrations then it’s probably because of the order in which they are being applied. It’s important that the migrations for auth are done before you start creating tables for other applications.

This issue appears to be most common when you are rebuilding your database from scratch.

Give this a try:

$ python3 manage.py makemigrations
$ python3 manage.py migrate auth
$ python3 manage.py migrate 

Installing Neo4j on Ubuntu 16.04

Some instructions for installing Neo4j on Ubuntu 16.04. More for my own benefit than anything else.

neo4j-logo

First you need to become root.

$ sudo /bin/bash
#

Installing Java

Neo4j is implemented in Java, so you’ll need to have the Java Runtime Environment (JRE) installed. If you already have this up and running, go ahead and skip this step.

# apt install default-jre default-jre-headless

Check whether you can now run the java executable.

# java

If that works for you, great! It didn’t immediately work on one of my machines. Strangely there were some dangling links in the alternatives system (which, to be honest, I was not even aware of until then!). It took a bit of Googling to figure this out, but the issue was resolved with the following:

# update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/bin/java
# update-alternatives --set javac /usr/lib/jvm/java-8-openjdk-amd64/bin/javac

Installing Neo4j

First we’ll add the repository key to our keychain.

# wget --no-check-certificate -O - https://debian.neo4j.org/neotechnology.gpg.key | apt-key add -

Then add the repository to the list of apt sources.

# echo 'deb http://debian.neo4j.org/repo stable/' >/etc/apt/sources.list.d/neo4j.list

Finally update the repository information and install Neo4j.

# apt update
# apt install neo4j

The server should have started automatically and should also be restarted at boot. If necessary the server can be stopped with

# service neo4j stop

and restarted with

# service neo4j start

At this stage you can drop the root privilege.

# exit
$

Accessing Neo4j

You should now be able to access the database server via http://localhost:7474/browser/.

I had some problems logging in with the default username and password (neo4j and neo4j), but this was easily resolved by deleting the file /var/lib/neo4j/data/dbms/auth and restarting the server.