Posts tagged python
How to make the simplest unittests in Python
0Testing your code is nearly a requirement (even more so in Ruby). Unittests are now the most vital elements for evaluating the quality/viability of a project.
I was a little jealous of Ruby where you don’t have so much to write to implement unittests. Here is a simple example:
1 2 3 4 5 6 7 8 9 | require "mymodule" require "test/unit" class TestMyModule < Test::Unit::TestCase def test_simple assert_equal(1, 1 ) end end |
Now, using Nose, you can get even shorter code. If you do standard Python projects, you’ll use a setup.py file. To use nose, you do not even need to specify the path where to find the tests, just add two lines (tests_require and test_suite) to call nosetest:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | from setuptools import setup, find_packages import sys, os import mymodule version = mymodule.__version__ setup(name='myproject', version=version, description="Module to display blah blah blah.", long_description=""" """, classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers keywords='mymodule foobar', author='Luc Stepniewski', author_email='[email protected]', url='', license='GPL', packages=find_packages(exclude=['ez_setup', 'examples', 'tests']), include_package_data=True, tests_require='nose', test_suite='nose.collector', zip_safe=False, install_requires=[ # -*- Extra requirements: -*- 'simplejson', ], entry_points=""" # -*- Entry points: -*- [console_scripts] mymodule = mymodule.mainmodule:main """, ) |
Now, to add tests, you just have to create a directory named tests (in the root of your project, where your setup.py resides, and then add a python file()s. No need to add a __init__.py to set the directory as a module. Now just add simple python files, like my-tests.py :
1 2 3 4 5 6 7 8 9 10 11 | import mymodule class TestAstInfoCli(object): def setup(self): pass def teardown(self): pass def test_annuaire_inverse(self): assert 1 == 1 |
As you can see, no need to import anything for doing unittests, not even the standard python unittest module! That’s better than ruby! The downside of this is that nose is an ‘external’ package, so you’ll have to install it first (or set it as a dependency in your setup.py file, as shown above).
If you don’t use a setup.py, you can call nose directly from the command line, with ‘nosetest’.
Now, let’s find an equivalent to the really cool rspec ruby module!
How to dial a number using Asterisk and Python
2I didn’t find any example in Python on how to Dial a number from an Asterisk server and link it to another channel. So here’s a quick code to do it. You just need to have a manager defined in your /etc/asterisk/manager.conf defined.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import socket
HOST="192.168.1.116"
PORT=5038
p = """Action: login
Events: off
Username: %(username)s
Secret: %(password)s
Action: originate
Channel: SIP/%(local_user)s
WaitTime: 60
CallerId: 600
Exten: %(phone_to_dial)s
Context: default
Priority: 1
Action: Logoff
"""
def click_to_call(phone_to_dial, username, password, local_user):
pattern = p % {
'phone_to_dial': phone_to_dial,
'username': username,
'password': password,
'local_user': local_user}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
data = s.recv(1024)
for l in pattern.split('\n'):
print "Sending>", l
s.send(l+'\r\n')
if l == "":
data = s.recv(1024)
print data
data = s.recv(1024)
s.close()
if __name__ == '__main__':
click_to_call(phone_to_dial='123456789',
username='manager_login', password='yourpass',
local_user='600') |
Making and deploying a Twisted project as a service under Windows
0What is the goal?
The goal is to implement a program in Python+Twisted (using PB for network access) under Windows XP or 2000+, that can be run before a user logs on, so it has to be a windows service, launched automatically, at boot. Another goal is to show some developement patterns in Twisted. You will find a lot of ‘theoretical’ patterns about how to make singletons/borgs, proxy, and stuff, but I never found patterns about ‘Twisted code’, except for the wonderful ‘finger tutorial’ by the squishy moshez. This tutorial can be split in two parts: The first one is about writing a good skeleton for your Twisted development. The second part is about making a Windows service.
Note that these instructions (the service building part) will not work under Windows 98, 95 or older. (more…)









Recent Comments