Category Archives: Python Development

Python ipcam library for Foscam and other ip web cameras

Here is a very helpful, easy to use, python library for Foscam and other ip web cams. I have been using it with a Raspberry Pi.

IPCam Python Library

Here’s an example of how it works:

Instantiate a camera connection.

from ipcam import IPCam

cam = IPCam(host='http://192.168.1.121',
            username='mylogin',
            password='mypass')

Get a snapshot from the camera.

snapshot = cam.get_snapshot()

Save the snapshot to a file.

from datetime import datetime

file_name_suffix = datetime.now().strftime("%Y%m%d_%H%M%S")
file_name = 'snapshot_{}.jpg'.format(file_name_suffix)
f = open(file_name, 'wb')
f.write(snapshot)
f.close()

Additionally, you can set camera parameters easily as well, they’re all by name, and right on the connection object.

cam.set_param('resolution',32)

Installing Psycopg2 on Mac OS X – pg_config missing – libssl not loaded

Issue

Psycopg2 installation not finding pg_config from Postgresql. Also, upon using psycopg2, it fails with Library not loaded: libssl.1.0.0.dylib.

Configuration

  • Mac OS X Mavericks
  • Postgresql from Mac OS X Installer on Postgresql.org (Using 9.3 in this case)
  • Python 2 or Python 3

Errors

  • Missing pg_config
  • Library not loaded: libssl.1.0.0.dylib

Fix

There is no need to add postgresql via homebrew just to get libraries. The application installer already installs them.

Additionally, you do not want to redefine DYLD_LIBRARY_PATH. This will break other dynamic links even though the default value is empty.

You simply need to add 1 environment variable to resolve the missing pg_config on installing psycopg2.

Then add 2 symbolic links to resolve the location of libssl.1.0.0.dylib and libcrypto.1.0.0.dylib when using psycopg2.

export PATH=/Library/PostgreSQL/9.3/bin:$PATH

ln -s /Library/PostgreSQL/9.3/lib/libssl.1.0.0.dylib /usr/lib/libssl.1.0.0.dylib
ln -s /Library/PostgreSQL/9.3/lib/libcrypto.1.0.0.dylib /usr/lib/libcrypto.1.0.0.dylib

Python 3 Lines of Code Counter

Based on the python 2 lines of code counter here, this version works with Python 3, and separates blank lines, comments and, properly, doesn’t count a single line broken with backslashes as separate ‘lines of code.’ Additionally, it breaks out the TODOs in the comments.

The final report’s “Lines of code” is as close to REAL functional, individual lines of code as any LOC counter we’ve seen.

By default, it will count recursively from the path it is run from, however, optionally, you may pass a single argument with a starting path and it will recursively check all files from the path specified.

Example usage:

./analytics.py myproject

Here it is:

import os, sys

exts = ['.py']

if len(sys.argv) > 0:
    use_path = os.path.basename(sys.argv[1])
else:
    use_path = os.path.dirname(sys.argv[0])

here = os.path.abspath(use_path)

def read_line_count(fname):
    line_cnt = 0
    blank = 0
    comment = 0
    todo = 0
    for line in open(fname).readlines():
        if len(line.strip()) > 0:
            if line.strip()[0] is not '#':
                if line[-2] is not '\\':
                    line_cnt += 1
            else:
                comment += 1
                if line.find('TODO') > -1:
                    todo += 1
        else:
            blank += 1

    return {'line':line_cnt,'comment':comment,'todo':todo,'blank':blank}

if __name__ == '__main__':
    line_count = 0
    comment_count = 0
    todo_count = 0
    blank_count = 0
    file_count = 0
    for base, dirs, files in os.walk(here):
        for file in files:
            # Check the sub directorys            
            if file.find('.') < 0:
                continue
            ext = (file[file.rindex('.'):]).lower()
            try:
                if exts.index(ext) >= 0:
                    file_count += 1
                    path = (base + '/'+ file)
                    c = read_line_count(path)
                    print(path[len(here):]+" : "+str(c['line']))
                    line_count += c['line']
                    comment_count += c['comment']
                    todo_count += c['todo']
                    blank_count += c['blank']
            except:
                pass

    print()
    print('Files            : '+str(file_count))
    print()
    print('Lines of code    : '+str(line_count))
    print('Comments (TODOs) : '+str(comment_count)+' ('+str(todo_count)+')')
    print('Empty lines      : '+str(blank_count))
    print()
    print('Total lines      : '+str(line_count + comment_count + blank_count))
    print()