Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, 12 September 2013

Django - Site Matching Query Error

Site matching query does not exist. Lookup parameters were {'pk': 1}
Open Project Shell
python manage.py shell

Create Site Object
from django.contrib.sites.models import Site
Site.objects.create(pk=1, domain='example.com', name='example')
Best Practise
python manage.py syncdb --noinput
python manage.py migrate
python mange.py createsuperuser

Sunday, 8 September 2013

Python - Fix Non-ASCII Character in File

Encode in ANSI


Additional Headers (On Top of code)
#! /usr/bin/env python
# -*- coding: utf-8 -*-

EXTRA: For Windows (Optional)

Python - Windows UTF-8 Cmd Input

Create win32_unicode_argv.py
Import
import sys
import win32_unicode_argv 
Reference:
http://code.activestate.com/recipes/572200/
http://stackoverflow.com/a/846931

Sunday, 1 September 2013

Python - Sip2sip Outgoing call with Pjsua

Error code
sip:ID@sip2sip.info
Failed to send Request msg INVITE/cseq=19833 err=171060 (Unsupported transport (PJSIP_EUNSUPTRANSPORT))
Call with sip:ID@sip2sip.info is DISCONNCTD last code = 503 (Unsupported t
ransport (PJSIP_EUNSUPTRANSPORT)) 
Add TLS support
sip:ID@sip2sip.info;transport=tls

Reference:

Friday, 30 August 2013

Python - Sip2sip with Pjsua

Rebuild Python Pjsua TLS Support with VS2008

config_site.h
#include <pj/config_site_sample.h>
#define PJ_HAS_SSL_SOCK 1
VC++ Header and Library Path

Build then Install
pjproject\pjsip-apps\lib\_pjsua.pyd
python pjsip-apps\src\python\setup-vc.py install

Sip2Sip.info Authentication Script
try:
    ua_cfg = pj.UAConfig()
    ua_cfg.nameserver = ['8.8.8.8', '8.8.4.4'] # Example: Google Public DNS
    lib.init(ua_cfg=ua_cfg, log_cfg=pj.LogConfig(level=4, callback=log_cb))
    transport = lib.create_transport(pj.TransportType.TLS, pj.TransportConfig(port=5060))
    lib.start()
 
    acc_cfg = pj.AccountConfig()
    acc_cfg.id = "sip:ID@sip2sip.info"
    acc_cfg.reg_uri = "sip:sip2sip.info;transport=tls"
    acc_cfg.auth_cred = [pj.AuthCred(
        realm="sip2sip.info",
        username="USERNAME",
        passwd="PASSWORD",
    )]
    acc_cb = MyAccountCallback(acc_cfg)
    acc = lib.create_account(acc_cfg, cb=acc_cb)
except Exception, err:
    print 'Error creating account:', err
References:
http://trac.pjsip.org/repos/wiki/Getting-Started/Installing-OpenSSL-Windows
http://trac.pjsip.org/repos/wiki/TLS

Thursday, 29 August 2013

Python - Pjsua 1.16 Build Fixes for Windows

Build Python2.7 x32 Windows 7 x64
pjsua==1.16-svn
http://svn.pjsip.org/repos/pjproject/branches/1.x/
Python2.7 <- Visual Studio 2008

VC++ Include & Library Directory (MenuBar -> Tools -> Options)



Replace python24.lib to python27.lib
py_pjsua.dsp
py_pjsua.vcproj
python_pjsua.vcproj
python_pjsua.dsp
Save as UTF-8 or Unicode (Optional)
g7221.h
Backup compiled pjsua 1.16:
pjproject\pjsip-apps\lib\_pjsua.pyd
python pjsip-apps\src\python\setup-vc.py install
https://www.dropbox.com/s/sgdrp8yxk2v6xe6/_pjsua.pyd

References: 
http://trac.pjsip.org/repos/wiki/Getting-Started/Download-Source
http://trac.pjsip.org/repos/wiki/Python_SIP/Build_Install
http://trac.pjsip.org/repos/wiki/Getting-Started/Windows

Wednesday, 28 August 2013

Python - Virtualenvwrapper on Ubuntu

Install pip
sudo apt-get install python-pip

Install virtualenvwrapper
pip install virtualenvwrapper
export WORKON_HOME=~/Envs
mkdir -p $WORKON_HOME
sudo echo 'source /usr/local/bin/virtualenvwrapper.sh' >> ~/.bashrc
# Restart terminal
Test
mkvirtualenv env1

Reference:
http://virtualenvwrapper.readthedocs.org/en/latest/install.html
http://roundhere.net/journal/virtualenv-ubuntu-12-10/

Python - Ubuntu Py-audio Compilation Fix

setup.py

Post Compilation Script (after python setup.py -v build)

*Final: python setup.py install

Reference:
https://code.google.com/p/py-audio/
https://code.google.com/p/py-audio/issues/detail?id=2

Thursday, 25 July 2013

Python - VoIP with SIP & Others

Public SIP Server List
https://code.google.com/p/sipml5/wiki/Public_SIP_Servers

SIP Client (Debug Tool)
http://icanblink.com/index.phtml

SIP response codes
http://en.wikipedia.org/wiki/List_of_SIP_response_codes

SIP

SIPpy
http://www.b2bua.org/

PJSIP Python
http://trac.pjsip.org/repos/wiki/Python_SIP_Tutorial
http://blog.pjsip.org/2008/07/24/python-sip-voip-softphone-client/

SIP SIMPLE
http://sipsimpleclient.org/
https://code.google.com/p/moshimoshi/
https://github.com/grengojbo/python-sipsimple

SIPVicious
https://code.google.com/p/sipvicious/

Divmod Sine
http://bazaar.launchpad.net/~divmod-dev/divmod.org/trunk/files/head:/Sine/
https://github.com/rcarmo/divmod.org

Shtoom (Last updated 2006)
http://sourceforge.net/projects/shtoom/
https://github.com/jrydberg/natmap

D-Bus
http://telepathy.freedesktop.org/wiki/
http://telepathy.freedesktop.org/doc/book/sect.services.python.html
https://github.com/PabloCastellano/telepathy-python

Asterisk server
https://pypi.python.org/pypi/django-asterisk/0.1

PyAudio (One way)
http://people.csail.mit.edu/hubert/pyaudio/
http://sharewebegin.blogspot.com/2013/06/real-time-voice-chat-example-in-python.html
http://www.ugw.name/?page_id=157

Twilio
https://www.twilio.com/
http://www.twilio.com/docs/libraries
https://developers.google.com/appengine/articles/twilio

GoogleVoice
https://code.google.com/p/pygooglevoice/

HTML5 SIP Client
http://sipml5.org/

P2P-SIP
http://39peers.net/
https://code.google.com/p/p2p-sip/

WebRTC (HTML5)
http://www.webrtc.org/reference/getting-started
https://code.google.com/p/py-webrtc/

Web Audio API (HTML 5)
https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html
http://www.html5rocks.com/en/tutorials/webaudio/intro/
http://updates.html5rocks.com/2012/09/Live-Web-Audio-Input-Enabled

Currently testing on: P2P-SIP (39Peers)

Tuesday, 23 July 2013

Heroku - Django Project Startup

settings.py
Troubleshooting for dj-static (In case static files error)
OSError: [Errno 2] No such file or directory: '/app/PROJECT_NAME/static'
heroku run python manage.py collectstatic --dry-run --noinput
wsgi.py
Procfile
runtime.txt
requirements.txt

Related Cmd

ssh-keygen -t rsa
# Generate ssh pub key (id_rsa)
ssh -v git@github.com
# Check your default .pub location for troubleshooting 
# Make sure both files included in the directory (id_rsa & id_rsa.pub)

heroku login
heroku keys
heroku keys:remove xxx@xxx

git init
echo local_settings.py>> .gitignore
# Remember to exclude local_settings.py in .gitignore
More info: https://devcenter.heroku.com/articles/gitignore
git add .
git commit -m 'Initial commit'

git remote -v
git remote rm heroku
More info: https://devcenter.heroku.com/articles/git

heroku create APP_NAME OR heroku git:remote -a EXISTING_APP_NAME

git push heroku master

heroku addons | grep POSTGRES
heroku addons:add heroku-postgresql:dev
heroku pg:wait
heroku config | grep HEROKU_POSTGRESQL
heroku pg:promote HEROKU_POSTGRESQL_XXX_URL
# Replace XXX with info from heroku config
More info: https://devcenter.heroku.com/articles/heroku-postgresql

heroku open

heroku run python manage.py syncdb
heroku run python manage.py migrate APP
heroku run python manage.py shell

More info: https://devcenter.heroku.com/articles/django

Thursday, 18 July 2013

Windows - Fix Virtualenv No Site Packages

*Using virtualenv-win

C:\Python27\workon.bat
C:\Python27\toggleglobalsitepackages.bat

Wednesday, 19 June 2013

Python - Headless Web Browsing Development

PhantomJS

Technology:
QtWebKit

Remark:
Common way to interact with PhantomJS by using subprocess

Link:

Ghost.py

Technology:
Qt4

Remark:
PyQt4 (GPL) or PySide (LGPL)

Link:

spynner

Technology:
Qt4

Remark:
PyQt4

Link:

phantompy

Technology:
Qt5

Remark:
Support Python >= 3.3 & Partially Support Python >= 2.7
# https://phantompy.readthedocs.org/en/latest/install.html#python-compatibility

Link:
https://github.com/niwibe/phantompy

python-zombie


Technology:
Zombie.js, Node.js

Remark: -

Link:
https://github.com/ryanpetrello/python-zombie

Tuesday, 4 June 2013

Python - PIL Import Error on Windows

# For Invidual case, Windows 7 x64, Python 2.7, PIL 1.1.7 Windows Binary

CMD (Virtual Env)
easy_install PIL-1.1.7.win32-py2.7.exe
Python Shell
from PIL import Image
change to
import Image
Try & Catch
try:
  from PIL import Image
except ImportError:
  try:
    import Image
OR

Change Directory Name
\Lib\site-packages\PIL-1.1.7-py2.7-win32.egg
to
\Lib\site-packages\PIL

Referencehttps://code.djangoproject.com/ticket/6054

Saturday, 1 June 2013

Python - snippetscream fix on Windows

snippetscream\_186.py
Replace
self.tmpfile = tempfile.NamedTemporaryFile()
To
self.tmpfile = tempfile.NamedTemporaryFile(delete=False)
# If delete is true (the default), the file is deleted as soon as it is closed. (New in version 2.6: The delete parameter.) [1]
Reference: http://docs.python.org/2/library/tempfile.html#tempfile.NamedTemporaryFile
Download: https://github.com/shaunsephton/django-snippetscream

Wednesday, 29 May 2013

Python - Virtualenv Windows Wrapper

Install
pip install virtualenvwrapper-win
CMD
mkvirtualenv project_A
workon project_A
# Create New Virtualenv for each New Project

More Info: https://github.com/davidmarble/virtualenvwrapper-win/

Python - Dictionary to List with Key only

Dictionary
key_and_item = dict([(x,0) for x in range(10)])
{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}

List
key_only = list(key_and_item)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# OrderedDict will also have the same effect

Monday, 27 May 2013

Python - Install PyGraphviz on Windows

Download
http://www.graphviz.org/Download_windows.php
https://pypi.python.org/pypi/pygraphviz/
# MinGW; if error - "Unable to find vcvarsall.bat"
# mingw-get-inst-20100831.exe, support mno-cygwin argument
# Remember to add MinGW\bin to System Variable - PATH
http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/mingw-get-inst-20100831/

Python27\Lib\distutils\unixccompiler.py
# If "TypeError: ‘NoneType’ object is unsubscriptable", Change
compiler = os.path.basename(sysconfig.get_config_var("CC"))
# To
compiler = "gcc"
# Change it back later on

pygraphviz-1.1\setup.py
# Change
library_path=None
include_path=None
# To
library_path = r"C:\Graphviz2.30\bin"
include_path = r"C:\Graphviz2.30\include\graphviz"
# If the library_path didn't work, try library_path="C:/Graphviz2.30/lib/release/lib"

cmd
pygraphviz-1.1>python setup.py build -c mingw32
# Copy pygraphviz-1.1\build\lib.win32-2.7\pygraphviz into Python27\libs\site-packages\pygraphviz

Test
import pygraphviz as pg
G = pg.AGraph()
G.add_node(u"Main", shape="rect", style="rounded", fontsize=18)
G.add_node(u"A")
G.add_node(u"B")
G.add_node(u"C")
G.add_edge(u"A", u"Main")
G.add_edge(u"B", u"Main")
G.add_edge(u"C", u"Main")
G.draw('test.png', format='png', prog='neato')

Tuesday, 21 May 2013

Python - Shortcut Conditional Expressions

If...Else Statement in one line

foo = 1
A = 'A'
B = 'B'
C = 'C'

x = A if foo > 0 else B
# A
 x = lambda: A if foo < 0 else B
x()
# B
x = A if not foo else B if foo < 0 else C
# C
Source: http://www.python.org/dev/peps/pep-0308/