F9K9e

    Niska: How did you come to that conclusion?
    Millican: You told me.. That's how I know there's
    something really going on in there. Human communication.
    Language is just the top level. Then there's all the stuff
    you try to say with your body, then you get into the deep shit.
    Everything you're trying not to say
    in the space between words and looks.
    You've already been really talking to me.

    Humans 2015, s01e06, 07:00

How to use Eclipse with Python?
-------------------------------

Note: 64-bit setup crashed using pyHook:

  runtime error R6031
  - Attempt to initialize the CRT more than once.
  This indicates a bug in your application.

So, it's a bug in PyHook/HookMouse for 64 bit Python installations. The quickest solution is to switch to 32 bit Python [7].

Tested with prerequesites: Java JRT 1.8  (jre-8u60-windows-i586.exe)
                           Python 2.7.10 (python-2.7.10.msi)
                           VPython 6.11  (VPython-Win-32-Py2.7-6.11.exe)

Go to an actual (not before redirection, might be shown on page) URL:

  http://www.pydev.org/updates/

Copy it to a clipboard and add it to Eclipse (Mars)

  Help -> Install New Software -> Add:

    Name:     Python Plugin
    Location: http://www.pydev.org/updates/

Now select in combo:

  Python Plugin - http://www.pydev.org/updates/

Select:

  [v] PyDev
    [v] PyDev For Eclipse
  [ ] Pydev Mylyn Integration (optional)
    [ ] Pydev Mylyn Integration

Install it and start using:

  File -> New -> PyDev project
    Project name: test
    Project contents: [v] Use default
    Project type: (+) Python
    Grammar Version: 2.7
    Interpreter:
      Interpreter Name: Python 2.7
      Interpreter Executable: C:\Python27\bin\python.exe

    (+) Create default src folder and add it to pythonGraph

Open The Python perspective
[v] Remember my decision

  in src folder (right click) New -> File -> test.py

In Editor window with test.py (right click) -- set filter to "font"

  Color and Fonts -> Text Font -> Edit:

    Lucida Console, 14

Now write a code

  from __future__ import print_function
  print( "hello")

Run your code with "Play" button on toolbar.

Annex A. Lib/__future__.py
--------------------------

  feature           optional in  mandatory in  effect

  division          2.2.0a2      3.0           / vs //
  absolute_import   2.5.0a1      3.0           from Tkinter import (Tk,
                                                   Frame, Button, Entry,
                                                   Canvas, Text, LEFT,
                                                   DISABLED, NORMAL,
                                                   RIDGE, END)
  print_function    2.6.0a2      3.0           Make print a function
  unicode_literals  2.6.0a2      3.0           b'Hello world'
                                               b'\x7fELF\x01\x01\x01\0'
Annex B. Pillow, win32 stuff
----------------------------

Install win32 extensions:

  http://freefr.dl.sourceforge.net/project/pywin32/pywin32
  /Build%20219/pywin32-219.win32-py2.7.exe

Download Pillow from

  http://www.lfd.uci.edu/~gohlke/pythonlibs/

Install with PIP

  c:\Python27\Scripts\pip install Pillow-3.0.0-cp27-none-win32.whl

Download PyHook from

  http://www.lfd.uci.edu/~gohlke/pythonlibs/

Install with PIP

  c:\Python27\Scripts\pip install pyHook-1.5.1-cp27-none-win32.whl

Example

  import win32gui
  import win32ui
  import win32con
  from PIL import ImageGrab
  from visual import *

  ball = sphere(color=color.orange)
  while True:
      x, y = win32gui.GetCursorPos()
      image = ImageGrab.grab()
      c = image.getpixel((x, y))
      c = ( c[0] / 256., c[1] / 256., c[2] / 256.)
      ball.color = c
      sleep(.1)

Annex C. PyDev "Undefined variable from import" phantom errors
--------------------------------------------------------------

  Window -> Preferences -> PyDev -> Editor -> Code Analysis ->
  Undefined -> Undefined Variable From Import -> Ignore

  Window -> Preferences -> PyDev -> Editor -> Code Analysis ->
  Imports -> Import not found -> Ignore

Annex D. Stopping pythoncom.PumpMessages
----------------------------------------

Working 32-bit Example (Python 2.7.10, Windows 10547)

Based on
  http://stackoverflow.com/questions/23727539/runtime-error-in-python

  import pythoncom, pyHook
  import ctypes
  import sys

  def OnMouseEvent(event):
      # called when mouse events are received
      print('MessageName: {0} Message: {1}'). \
          format(event.MessageName, event.Message)
      print('Window: {0} WindowName: {1}'). \
          format(event.Window, event.WindowName)
      print('Position:', event.Position)
      print('---')
      return True

  def OnKeyboardEvent(event):
      print('MessageName: {0} Message: {1}'). \
          format(event.MessageName, event.Message)
      print('Window: {0} WindowName: {1}'). \
          format(event.Window, event.WindowName)
      print('Ascii:', event.Ascii, chr(event.Ascii))
      print('Key: {0} KeyID: {1}'). \
          format(event.Key, event.KeyID)
      print('===')
      if chr(event.Ascii) == 'q':
          ctypes.windll.user32.PostQuitMessage(0)
      return False # Block event

  print('Python version: {0}').format((sys.version))

  hm = pyHook.HookManager()

  hm.MouseAll = OnMouseEvent
  hm.HookMouse()

  hm.KeyDown = OnKeyboardEvent
  hm.HookKeyboard()

  pythoncom.PumpMessages()

  hm.UnhookMouse()
  hm.UnhookKeyboard()

  print("Exiting..")

Annex E. Simulators output coordinates
--------------------------------------

UnoArduSim v.1.21

  676  690  703  717  731  744  762  776  789  803  817  830  844  858
    +14  +13  +13  +14  +13  +18  +14  +13  +14  +14  +13  +14  +14
  0    14   27   40   54   67   95   109  122  136  150  163  177  191

Siemens Logo v.8

  394   423   452   481
     +29   +29   +29
    0    29    58    87

Logisim v.2.7.1

  218  248  278  308
    +30  +30  +30
  0    30   60   90

Annex F. Grab color from simulator output
-----------------------------------------

Usage:

  1. Select point of interest with mouse cursor
  2. Click left mouse button -- now selected point is monitored

Code:

  import win32gui
  import win32ui
  import win32con
  from PIL import ImageGrab
  from visual import *
  import pythoncom, pyHook
  import ctypes
  import sys

  def OnMouseEvent(event):
      global x, y, fOnHook
      x, y = (event.Position[0], event.Position[1])
      if event.Message == win32con.WM_LBUTTONDOWN :
          fOnHook = False
          ctypes.windll.user32.PostQuitMessage(0)
          return False
      return True

  def OnKeyboardEvent(event):
      if chr(event.Ascii) == 'q':
          ctypes.windll.user32.PostQuitMessage(0)
      return False # Block event

  def getPixelState():
      global c
      image = ImageGrab.grab()
      c = image.getpixel((x, y))
      c = ( c[0] / 256., c[1] / 256., c[2] / 256.)

  c = color.orange
  ball = sphere(color=c)
  sleep(0.0001)

  hm = pyHook.HookManager()

  hm.MouseAll = OnMouseEvent
  hm.HookMouse()

  hm.KeyDown = OnKeyboardEvent
  hm.HookKeyboard()

  x = 0; y = 0; fOnHook = True
  while fOnHook == True:
      getPixelState()
      print("Processing x={0} y={1} color={2}").format( x, y, c)
      pythoncom.PumpWaitingMessages()
      sleep(.01)

  hm.UnhookMouse()
  hm.UnhookKeyboard()

  print("Elected")
  while True:
      getPixelState()
      ball.color = c
      sleep(.01)

Annex G. Pelican Crossing
-------------------------

  // Pelican Crossing (arduino side)

  void setup() {
    pinMode(13, OUTPUT);
    pinMode(12, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode( 9, OUTPUT);
    pinMode( 8, OUTPUT);
  }

  void loop() {
    digitalWrite(13, HIGH);
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite( 9, LOW);
    digitalWrite( 8, HIGH);
    delay(1000);

    digitalWrite(13, LOW);
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
    digitalWrite( 9, HIGH);
    digitalWrite( 8, LOW);
    delay(1000);

    digitalWrite(13, LOW);
    digitalWrite(12, LOW);
    digitalWrite(11, HIGH);
    digitalWrite( 9, HIGH);
    digitalWrite( 8, LOW);
    delay(1000);

    digitalWrite(13, LOW);
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
    digitalWrite( 9, HIGH);
    digitalWrite( 8, LOW);
    delay(1000);
  }

  # Pelican Crossing (python side)

  import win32gui
  import win32ui
  import win32con
  from PIL import ImageGrab
  from visual import *
  import pythoncom, pyHook
  import ctypes
  import sys

  def OnMouseEvent(event):
      global x, y, fOnHook
      x, y = (event.Position[0], event.Position[1])
      if event.Message == win32con.WM_LBUTTONDOWN :
          fOnHook = False
          ctypes.windll.user32.PostQuitMessage(0)
          return False
      return True

  def OnKeyboardEvent(event):
      if chr(event.Ascii) == 'q':
          ctypes.windll.user32.PostQuitMessage(0)
      return False # Block event

  def getPixelState():
      global cR, cY, cG, cPR, cPG
      image = ImageGrab.grab()
      cR = image.getpixel((x, y))
      cR = ( cR[0] / 256., cR[1] / 256., cR[2] / 256.)
      if cR[0] > 0.8 : cR = color.red
      else : cR = (.2,.2,.2)
      cY = image.getpixel((x+14, y))
      cY = ( cY[0] / 256., cY[1] / 256., cY[2] / 256.)
      if cY[0] > 0.8 : cY = color.yellow
      else : cY = (.2,.2,.2)
      cG = image.getpixel((x+14+13, y))
      cG = ( cG[0] / 256., cG[1] / 256., cG[2] / 256.)
      if cG[0] > 0.8 : cG = color.green
      else : cG = (.2,.2,.2)
      cPR = image.getpixel((x+14+13+13+14, y))
      cPR = ( cPR[0] / 256., cPR[1] / 256., cPR[2] / 256.)
      if cPR[0] > 0.8 : cPR = color.red
      else : cPR = (.2,.2,.2)
      cPG = image.getpixel((x+14+13+13+14+13, y))
      cPG = ( cPG[0] / 256., cPG[1] / 256., cPG[2] / 256.)
      if cPG[0] > 0.8 : cPG = color.green
      else : cPG = (.2,.2,.2)

  ballRC = sphere(color=color.red, radius=5, pos=(0,27,0))
  ballYC = sphere(color=color.yellow, radius=5, pos=(0,17,0))
  ballGC = sphere(color=color.green, radius=5, pos=(0,7,0))

  ballRP = sphere(color=color.red, radius=5, pos=(0,-13,0))
  ballGP = sphere(color=color.green, radius=5, pos=(0,-23,0))

  boxCL = box( pos=(-10, 17, 0), length=30, height=2, width=10, axis=(0,1,0))
  boxCR = box( pos=(+10, 17, 0), length=30, height=2, width=10, axis=(0,1,0))
  boxPF = box( pos=(  0, -18, +10), length=20, height=10, width=2, axis=(0,1,0))
  boxPR = box( pos=(  0, -18, -10), length=20, height=10, width=2, axis=(0,1,0))
  sleep(0.0001)

  hm = pyHook.HookManager()

  hm.MouseAll = OnMouseEvent
  hm.HookMouse()

  hm.KeyDown = OnKeyboardEvent
  hm.HookKeyboard()

  x = 0; y = 0; fOnHook = True
  while fOnHook == True:
      getPixelState()
      print("Processing x={0} y={1} color={2}").format( x, y, cR)
      pythoncom.PumpWaitingMessages()
      sleep(.01)

  hm.UnhookMouse()
  hm.UnhookKeyboard()

  print("Elected")
  while True:
      getPixelState()
      ballRC.color = cR
      ballYC.color = cY
      ballGC.color = cG
      ballRP.color = cPR
      ballGP.color = cPG
      sleep(.01)

More links.
-----------

  1. Nice PyHook example: https://gist.github.com/jdiscar/9144923

  2. How do I fix PyDev "Undefined variable from import" errors?
       http://stackoverflow.com/questions/2112715
            /how-do-i-fix-pydev-undefined-variable-from-import-errors

  3. PyWin32. http://sourceforge.net/projects/pywin32/

  4. PyHook.  http://http://sourceforge.net/projects/pyhook/
              http://www.lfd.uci.edu/~gohlke/pythonlibs/

  5. Stop pumping.
      http://stackoverflow.com/questions/19629428
           /i-want-to-stop-pythoncom-working
      http://stackoverflow.com/questions/6023172
           /ending-a-program-mid-run
      http://stackoverflow.com/questions/32692202
           /how-to-stop-this-program/32692891

  6. Block events
      http://code.activestate.com/recipes
           /553270-using-pyhook-to-block-windows-keys/

  7. http://stackoverflow.com/questions/23727539/runtime-error-in-python
