paulgoetze / elixir-python

Examples for running Python code from Elixir

Home Page:http://paulgoetze.com/2017/04/19/building-a-city-search-with-elixir-and-python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Examples for using Python with Elixir

The GeoLite2 example app use erlport via export in order to leverage the Whoosh text indexing and search Python package.

It creates a search index from the free GeoLite2 city database and provides an Elixir module to search the city index.

Installation

  • Install Python v3.x
  • Install Elixir v1.4+
  • Setup a virtualenv and activate it
  • Run pip install -r requirements.txt to install the python dependencies
  • Run mix deps.get to install the Hex dependencies

Creating the index

Step into the interactive Elixir console with iex -S mix.

Then run the following function to create the city index:

ElixirPython.GeoLite2.create_index()

Searching

You can search your city index with the following code:

iex(1)> ElixirPython.GeoLite2.search("Berlin")
[%{city: "Berlin", country: "Germany", state: "Land Berlin"},
 %{city: "Berlingen", country: "Belgium", state: "Flanders"},
 %{city: "Falkenberg", country: "Germany", state: "Land Berlin"},
 %{city: "Gosen", country: "Germany", state: "Land Berlin"},
 %{city: "Bernau bei Berlin", country: "Germany", state: "Brandenburg"},
 %{city: "Berlingen", country: "Switzerland", state: "Thurgau"},
 %{city: "Treptow Bezirk", country: "Germany", state: "Land Berlin"},
 %{city: "Heinersdorf", country: "Germany", state: "Land Berlin"},
 %{city: "Stirling", country: "Canada", state: "Alberta"},
 %{city: "New Berlin", country: "United States", state: "Illinois"}]

The default number of returned results is 10.
You can also pass the number of wanted results as the second argument:

iex(2)> ElixirPython.GeoLite2.search("Berlin", 3)
[%{city: "Berlin", country: "Germany", state: "Land Berlin"},
 %{city: "Berlingen", country: "Belgium", state: "Flanders"},
 %{city: "Falkenberg", country: "Germany", state: "Land Berlin"}]

About

Examples for running Python code from Elixir

http://paulgoetze.com/2017/04/19/building-a-city-search-with-elixir-and-python


Languages

Language:Elixir 52.3%Language:Python 47.7%