Repository: quantling/pyndl

Issue:
get version information via function
We should provide a function to show information about the system, the installed packages, etc. like in [pandas](https://github.com/pandas-dev/pandas/blob/master/pandas/_version.py).

This may especially useful for issue reporting as it is much more precise than `pip freeze` 

Discussion:

Patch:
diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md
index ddcc33e..f678cfa 100644
--- a/.github/ISSUE_TEMPLATE.md
+++ b/.github/ISSUE_TEMPLATE.md
@@ -14,8 +14,4 @@
 * Step 3
 
 ### System details
-* Operating System:
-* Python Version:
-<!-- run `python --version` for version details -->
-* Installed python packages
-<!-- run `pip freeze` for a list of installed packages -->
+<!-- run `python -c "import pyndl; pyndl.sysinfo()"` and copy the output -->
diff --git a/pyndl/__init__.py b/pyndl/__init__.py
index b1632c8..97671e8 100644
--- a/pyndl/__init__.py
+++ b/pyndl/__init__.py
@@ -1,7 +1,13 @@
+import os
+import sys
+import multiprocessing as mp
+from pip._vendor import pkg_resources
+
+
 __author__ = ('David-Elias Künstle, Lennard Schneider, '
               'Konstantin Sering, Marc Weitz')
 __author_email__ = 'konstantin.sering@uni-tuebingen.de'
-__version__ = '0.3.3'
+__version__ = '0.3.4'
 __license__ = 'MIT'
 __description__ = ('Naive discriminative learning implements learning and '
                    'classification models based on the Rescorla-Wagner '
@@ -28,6 +34,46 @@ __doc__ = """
 :version: %s
 :author: %s
 :contact: %s
-:date: 2017-04-18
+:date: 2017-12-27
 :copyright: %s
 """ % (__description__, __version__, __author__, __author_email__, __license__)
+
+
+def sysinfo():
+    """
+    Prints system the dependency information
+    """
+    pyndl = pkg_resources.working_set.by_key["pyndl"]
+    dependencies = [str(r) for r in pyndl.requires()]
+
+    header = ("Pyndl Information\n"
+              "=================\n\n")
+
+    general = ("General Information\n"
+               "-------------------\n"
+               "Python version: {}\n"
+               "Pyndl version: {}\n\n").format(sys.version.split()[0], __version__)
+
+    uname = os.uname()
+    osinfo = ("Operating System\n"
+              "----------------\n"
+              "OS: {s.sysname} {s.machine}\n"
+              "Kernel: {s.release}\n"
+              "CPU: {cpu_count}\n").format(s=uname, cpu_count=mp.cpu_count())
+
+    if uname.sysname == "Linux":
+        names, *lines = os.popen("free -m").readlines()
+        for identifier in ["Mem:", "Swap:"]:
+            memory = [line for line in lines if identifier in line][0]
+            ix, total, used, *rest = memory.split()
+            osinfo += "{} {}MiB/{}MiB\n".format(identifier, used, total)
+
+    osinfo += "\n"
+
+    deps = ("Dependencies\n"
+            "------------\n")
+
+    deps += "\n".join("{pkg.__name__}: {pkg.__version__}".format(pkg=__import__(dep))
+                      for dep in dependencies)
+
+    print(header + general + osinfo + deps)
diff --git a/requirements.txt b/requirements.txt
index 0cabe05..a89ce96 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,3 +6,4 @@ xarray
 netCDF4
 numpydoc
 docopt
+pip


Test patch:
diff --git a/tests/test_pyndl.py b/tests/test_pyndl.py
new file mode 100644
index 0000000..fc511d2
--- /dev/null
+++ b/tests/test_pyndl.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+# pylint: disable=C0111
+
+import sys
+import re
+from io import StringIO
+from contextlib import redirect_stdout
+
+import pyndl
+
+
+def test_sysinfo():
+    out = StringIO()
+    with redirect_stdout(out):
+        pyndl.sysinfo()
+    out = out.getvalue()
+
+    pattern = re.compile("[a-zA-Z0-9_\. ]*\n[\=]*\n+([a-zA-Z0-9_ ]*\n[\-]*\n"
+                         "([a-zA-Z0-9_ ]*: [a-zA-Z0-9_\.\-/ ]*\n+)+)+")
+    assert pattern.match(out)